PR 21414, null pointer deref of _bfd_elf_large_com_section sym
[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
84 /* Enums for exception-handling support. */
85 enum exception_event_kind
86 {
87 EX_EVENT_THROW,
88 EX_EVENT_RETHROW,
89 EX_EVENT_CATCH
90 };
91
92 /* Prototypes for local functions. */
93
94 static void enable_delete_command (char *, int);
95
96 static void enable_once_command (char *, int);
97
98 static void enable_count_command (char *, int);
99
100 static void disable_command (char *, int);
101
102 static void enable_command (char *, int);
103
104 static void map_breakpoint_numbers (const char *,
105 void (*) (struct breakpoint *,
106 void *),
107 void *);
108
109 static void ignore_command (char *, int);
110
111 static int breakpoint_re_set_one (void *);
112
113 static void breakpoint_re_set_default (struct breakpoint *);
114
115 static void
116 create_sals_from_location_default (const struct event_location *location,
117 struct linespec_result *canonical,
118 enum bptype type_wanted);
119
120 static void create_breakpoints_sal_default (struct gdbarch *,
121 struct linespec_result *,
122 char *, char *, enum bptype,
123 enum bpdisp, int, int,
124 int,
125 const struct breakpoint_ops *,
126 int, int, int, unsigned);
127
128 static void decode_location_default (struct breakpoint *b,
129 const struct event_location *location,
130 struct program_space *search_pspace,
131 struct symtabs_and_lines *sals);
132
133 static void clear_command (char *, int);
134
135 static void catch_command (char *, int);
136
137 static int can_use_hardware_watchpoint (struct value *);
138
139 static void break_command_1 (char *, int, int);
140
141 static void mention (struct breakpoint *);
142
143 static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
144 enum bptype,
145 const struct breakpoint_ops *);
146 static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
147 const struct symtab_and_line *);
148
149 /* This function is used in gdbtk sources and thus can not be made
150 static. */
151 struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
152 struct symtab_and_line,
153 enum bptype,
154 const struct breakpoint_ops *);
155
156 static struct breakpoint *
157 momentary_breakpoint_from_master (struct breakpoint *orig,
158 enum bptype type,
159 const struct breakpoint_ops *ops,
160 int loc_enabled);
161
162 static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
163
164 static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
165 CORE_ADDR bpaddr,
166 enum bptype bptype);
167
168 static void describe_other_breakpoints (struct gdbarch *,
169 struct program_space *, CORE_ADDR,
170 struct obj_section *, int);
171
172 static int watchpoint_locations_match (struct bp_location *loc1,
173 struct bp_location *loc2);
174
175 static int breakpoint_location_address_match (struct bp_location *bl,
176 struct address_space *aspace,
177 CORE_ADDR addr);
178
179 static int breakpoint_location_address_range_overlap (struct bp_location *,
180 struct address_space *,
181 CORE_ADDR, int);
182
183 static void breakpoints_info (char *, int);
184
185 static void watchpoints_info (char *, int);
186
187 static int breakpoint_1 (char *, int,
188 int (*) (const struct breakpoint *));
189
190 static int breakpoint_cond_eval (void *);
191
192 static void cleanup_executing_breakpoints (void *);
193
194 static void commands_command (char *, int);
195
196 static void condition_command (char *, int);
197
198 static int remove_breakpoint (struct bp_location *);
199 static int remove_breakpoint_1 (struct bp_location *, enum remove_bp_reason);
200
201 static enum print_stop_action print_bp_stop_message (bpstat bs);
202
203 static int watchpoint_check (void *);
204
205 static void maintenance_info_breakpoints (char *, int);
206
207 static int hw_breakpoint_used_count (void);
208
209 static int hw_watchpoint_use_count (struct breakpoint *);
210
211 static int hw_watchpoint_used_count_others (struct breakpoint *except,
212 enum bptype type,
213 int *other_type_used);
214
215 static void hbreak_command (char *, int);
216
217 static void thbreak_command (char *, int);
218
219 static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
220 int count);
221
222 static void stop_command (char *arg, int from_tty);
223
224 static void stopin_command (char *arg, int from_tty);
225
226 static void stopat_command (char *arg, int from_tty);
227
228 static void tcatch_command (char *arg, int from_tty);
229
230 static void free_bp_location (struct bp_location *loc);
231 static void incref_bp_location (struct bp_location *loc);
232 static void decref_bp_location (struct bp_location **loc);
233
234 static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
235
236 /* update_global_location_list's modes of operation wrt to whether to
237 insert locations now. */
238 enum ugll_insert_mode
239 {
240 /* Don't insert any breakpoint locations into the inferior, only
241 remove already-inserted locations that no longer should be
242 inserted. Functions that delete a breakpoint or breakpoints
243 should specify this mode, so that deleting a breakpoint doesn't
244 have the side effect of inserting the locations of other
245 breakpoints that are marked not-inserted, but should_be_inserted
246 returns true on them.
247
248 This behavior is useful is situations close to tear-down -- e.g.,
249 after an exec, while the target still has execution, but
250 breakpoint shadows of the previous executable image should *NOT*
251 be restored to the new image; or before detaching, where the
252 target still has execution and wants to delete breakpoints from
253 GDB's lists, and all breakpoints had already been removed from
254 the inferior. */
255 UGLL_DONT_INSERT,
256
257 /* May insert breakpoints iff breakpoints_should_be_inserted_now
258 claims breakpoints should be inserted now. */
259 UGLL_MAY_INSERT,
260
261 /* Insert locations now, irrespective of
262 breakpoints_should_be_inserted_now. E.g., say all threads are
263 stopped right now, and the user did "continue". We need to
264 insert breakpoints _before_ resuming the target, but
265 UGLL_MAY_INSERT wouldn't insert them, because
266 breakpoints_should_be_inserted_now returns false at that point,
267 as no thread is running yet. */
268 UGLL_INSERT
269 };
270
271 static void update_global_location_list (enum ugll_insert_mode);
272
273 static void update_global_location_list_nothrow (enum ugll_insert_mode);
274
275 static int is_hardware_watchpoint (const struct breakpoint *bpt);
276
277 static void insert_breakpoint_locations (void);
278
279 static void tracepoints_info (char *, int);
280
281 static void delete_trace_command (char *, int);
282
283 static void enable_trace_command (char *, int);
284
285 static void disable_trace_command (char *, int);
286
287 static void trace_pass_command (char *, int);
288
289 static void set_tracepoint_count (int num);
290
291 static int is_masked_watchpoint (const struct breakpoint *b);
292
293 static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
294
295 /* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
296 otherwise. */
297
298 static int strace_marker_p (struct breakpoint *b);
299
300 /* The breakpoint_ops structure to be inherited by all breakpoint_ops
301 that are implemented on top of software or hardware breakpoints
302 (user breakpoints, internal and momentary breakpoints, etc.). */
303 static struct breakpoint_ops bkpt_base_breakpoint_ops;
304
305 /* Internal breakpoints class type. */
306 static struct breakpoint_ops internal_breakpoint_ops;
307
308 /* Momentary breakpoints class type. */
309 static struct breakpoint_ops momentary_breakpoint_ops;
310
311 /* Momentary breakpoints for bp_longjmp and bp_exception class type. */
312 static struct breakpoint_ops longjmp_breakpoint_ops;
313
314 /* The breakpoint_ops structure to be used in regular user created
315 breakpoints. */
316 struct breakpoint_ops bkpt_breakpoint_ops;
317
318 /* Breakpoints set on probes. */
319 static struct breakpoint_ops bkpt_probe_breakpoint_ops;
320
321 /* Dynamic printf class type. */
322 struct breakpoint_ops dprintf_breakpoint_ops;
323
324 /* The style in which to perform a dynamic printf. This is a user
325 option because different output options have different tradeoffs;
326 if GDB does the printing, there is better error handling if there
327 is a problem with any of the arguments, but using an inferior
328 function lets you have special-purpose printers and sending of
329 output to the same place as compiled-in print functions. */
330
331 static const char dprintf_style_gdb[] = "gdb";
332 static const char dprintf_style_call[] = "call";
333 static const char dprintf_style_agent[] = "agent";
334 static const char *const dprintf_style_enums[] = {
335 dprintf_style_gdb,
336 dprintf_style_call,
337 dprintf_style_agent,
338 NULL
339 };
340 static const char *dprintf_style = dprintf_style_gdb;
341
342 /* The function to use for dynamic printf if the preferred style is to
343 call into the inferior. The value is simply a string that is
344 copied into the command, so it can be anything that GDB can
345 evaluate to a callable address, not necessarily a function name. */
346
347 static char *dprintf_function;
348
349 /* The channel to use for dynamic printf if the preferred style is to
350 call into the inferior; if a nonempty string, it will be passed to
351 the call as the first argument, with the format string as the
352 second. As with the dprintf function, this can be anything that
353 GDB knows how to evaluate, so in addition to common choices like
354 "stderr", this could be an app-specific expression like
355 "mystreams[curlogger]". */
356
357 static char *dprintf_channel;
358
359 /* True if dprintf commands should continue to operate even if GDB
360 has disconnected. */
361 static int disconnected_dprintf = 1;
362
363 /* A reference-counted struct command_line. This lets multiple
364 breakpoints share a single command list. */
365 struct counted_command_line
366 {
367 /* The reference count. */
368 int refc;
369
370 /* The command list. */
371 struct command_line *commands;
372 };
373
374 struct command_line *
375 breakpoint_commands (struct breakpoint *b)
376 {
377 return b->commands ? b->commands->commands : NULL;
378 }
379
380 /* Flag indicating that a command has proceeded the inferior past the
381 current breakpoint. */
382
383 static int breakpoint_proceeded;
384
385 const char *
386 bpdisp_text (enum bpdisp disp)
387 {
388 /* NOTE: the following values are a part of MI protocol and
389 represent values of 'disp' field returned when inferior stops at
390 a breakpoint. */
391 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
392
393 return bpdisps[(int) disp];
394 }
395
396 /* Prototypes for exported functions. */
397 /* If FALSE, gdb will not use hardware support for watchpoints, even
398 if such is available. */
399 static int can_use_hw_watchpoints;
400
401 static void
402 show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
403 struct cmd_list_element *c,
404 const char *value)
405 {
406 fprintf_filtered (file,
407 _("Debugger's willingness to use "
408 "watchpoint hardware is %s.\n"),
409 value);
410 }
411
412 /* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
413 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
414 for unrecognized breakpoint locations.
415 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
416 static enum auto_boolean pending_break_support;
417 static void
418 show_pending_break_support (struct ui_file *file, int from_tty,
419 struct cmd_list_element *c,
420 const char *value)
421 {
422 fprintf_filtered (file,
423 _("Debugger's behavior regarding "
424 "pending breakpoints is %s.\n"),
425 value);
426 }
427
428 /* If 1, gdb will automatically use hardware breakpoints for breakpoints
429 set with "break" but falling in read-only memory.
430 If 0, gdb will warn about such breakpoints, but won't automatically
431 use hardware breakpoints. */
432 static int automatic_hardware_breakpoints;
433 static void
434 show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
435 struct cmd_list_element *c,
436 const char *value)
437 {
438 fprintf_filtered (file,
439 _("Automatic usage of hardware breakpoints is %s.\n"),
440 value);
441 }
442
443 /* If on, GDB keeps breakpoints inserted even if the inferior is
444 stopped, and immediately inserts any new breakpoints as soon as
445 they're created. If off (default), GDB keeps breakpoints off of
446 the target as long as possible. That is, it delays inserting
447 breakpoints until the next resume, and removes them again when the
448 target fully stops. This is a bit safer in case GDB crashes while
449 processing user input. */
450 static int always_inserted_mode = 0;
451
452 static void
453 show_always_inserted_mode (struct ui_file *file, int from_tty,
454 struct cmd_list_element *c, const char *value)
455 {
456 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
457 value);
458 }
459
460 /* See breakpoint.h. */
461
462 int
463 breakpoints_should_be_inserted_now (void)
464 {
465 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
466 {
467 /* If breakpoints are global, they should be inserted even if no
468 thread under gdb's control is running, or even if there are
469 no threads under GDB's control yet. */
470 return 1;
471 }
472 else if (target_has_execution)
473 {
474 struct thread_info *tp;
475
476 if (always_inserted_mode)
477 {
478 /* The user wants breakpoints inserted even if all threads
479 are stopped. */
480 return 1;
481 }
482
483 if (threads_are_executing ())
484 return 1;
485
486 /* Don't remove breakpoints yet if, even though all threads are
487 stopped, we still have events to process. */
488 ALL_NON_EXITED_THREADS (tp)
489 if (tp->resumed
490 && tp->suspend.waitstatus_pending_p)
491 return 1;
492 }
493 return 0;
494 }
495
496 static const char condition_evaluation_both[] = "host or target";
497
498 /* Modes for breakpoint condition evaluation. */
499 static const char condition_evaluation_auto[] = "auto";
500 static const char condition_evaluation_host[] = "host";
501 static const char condition_evaluation_target[] = "target";
502 static const char *const condition_evaluation_enums[] = {
503 condition_evaluation_auto,
504 condition_evaluation_host,
505 condition_evaluation_target,
506 NULL
507 };
508
509 /* Global that holds the current mode for breakpoint condition evaluation. */
510 static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
511
512 /* Global that we use to display information to the user (gets its value from
513 condition_evaluation_mode_1. */
514 static const char *condition_evaluation_mode = condition_evaluation_auto;
515
516 /* Translate a condition evaluation mode MODE into either "host"
517 or "target". This is used mostly to translate from "auto" to the
518 real setting that is being used. It returns the translated
519 evaluation mode. */
520
521 static const char *
522 translate_condition_evaluation_mode (const char *mode)
523 {
524 if (mode == condition_evaluation_auto)
525 {
526 if (target_supports_evaluation_of_breakpoint_conditions ())
527 return condition_evaluation_target;
528 else
529 return condition_evaluation_host;
530 }
531 else
532 return mode;
533 }
534
535 /* Discovers what condition_evaluation_auto translates to. */
536
537 static const char *
538 breakpoint_condition_evaluation_mode (void)
539 {
540 return translate_condition_evaluation_mode (condition_evaluation_mode);
541 }
542
543 /* Return true if GDB should evaluate breakpoint conditions or false
544 otherwise. */
545
546 static int
547 gdb_evaluates_breakpoint_condition_p (void)
548 {
549 const char *mode = breakpoint_condition_evaluation_mode ();
550
551 return (mode == condition_evaluation_host);
552 }
553
554 void _initialize_breakpoint (void);
555
556 /* Are we executing breakpoint commands? */
557 static int executing_breakpoint_commands;
558
559 /* Are overlay event breakpoints enabled? */
560 static int overlay_events_enabled;
561
562 /* See description in breakpoint.h. */
563 int target_exact_watchpoints = 0;
564
565 /* Walk the following statement or block through all breakpoints.
566 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
567 current breakpoint. */
568
569 #define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
570
571 #define ALL_BREAKPOINTS_SAFE(B,TMP) \
572 for (B = breakpoint_chain; \
573 B ? (TMP=B->next, 1): 0; \
574 B = TMP)
575
576 /* Similar iterator for the low-level breakpoints. SAFE variant is
577 not provided so update_global_location_list must not be called
578 while executing the block of ALL_BP_LOCATIONS. */
579
580 #define ALL_BP_LOCATIONS(B,BP_TMP) \
581 for (BP_TMP = bp_locations; \
582 BP_TMP < bp_locations + bp_locations_count && (B = *BP_TMP);\
583 BP_TMP++)
584
585 /* Iterates through locations with address ADDRESS for the currently selected
586 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
587 to where the loop should start from.
588 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
589 appropriate location to start with. */
590
591 #define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
592 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
593 BP_LOCP_TMP = BP_LOCP_START; \
594 BP_LOCP_START \
595 && (BP_LOCP_TMP < bp_locations + bp_locations_count \
596 && (*BP_LOCP_TMP)->address == ADDRESS); \
597 BP_LOCP_TMP++)
598
599 /* Iterator for tracepoints only. */
600
601 #define ALL_TRACEPOINTS(B) \
602 for (B = breakpoint_chain; B; B = B->next) \
603 if (is_tracepoint (B))
604
605 /* Chains of all breakpoints defined. */
606
607 struct breakpoint *breakpoint_chain;
608
609 /* Array is sorted by bp_locations_compare - primarily by the ADDRESS. */
610
611 static struct bp_location **bp_locations;
612
613 /* Number of elements of BP_LOCATIONS. */
614
615 static unsigned bp_locations_count;
616
617 /* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
618 ADDRESS for the current elements of BP_LOCATIONS which get a valid
619 result from bp_location_has_shadow. You can use it for roughly
620 limiting the subrange of BP_LOCATIONS to scan for shadow bytes for
621 an address you need to read. */
622
623 static CORE_ADDR bp_locations_placed_address_before_address_max;
624
625 /* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
626 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
627 BP_LOCATIONS which get a valid result from bp_location_has_shadow.
628 You can use it for roughly limiting the subrange of BP_LOCATIONS to
629 scan for shadow bytes for an address you need to read. */
630
631 static CORE_ADDR bp_locations_shadow_len_after_address_max;
632
633 /* The locations that no longer correspond to any breakpoint, unlinked
634 from the bp_locations array, but for which a hit may still be
635 reported by a target. */
636 VEC(bp_location_p) *moribund_locations = NULL;
637
638 /* Number of last breakpoint made. */
639
640 static int breakpoint_count;
641
642 /* The value of `breakpoint_count' before the last command that
643 created breakpoints. If the last (break-like) command created more
644 than one breakpoint, then the difference between BREAKPOINT_COUNT
645 and PREV_BREAKPOINT_COUNT is more than one. */
646 static int prev_breakpoint_count;
647
648 /* Number of last tracepoint made. */
649
650 static int tracepoint_count;
651
652 static struct cmd_list_element *breakpoint_set_cmdlist;
653 static struct cmd_list_element *breakpoint_show_cmdlist;
654 struct cmd_list_element *save_cmdlist;
655
656 /* See declaration at breakpoint.h. */
657
658 struct breakpoint *
659 breakpoint_find_if (int (*func) (struct breakpoint *b, void *d),
660 void *user_data)
661 {
662 struct breakpoint *b = NULL;
663
664 ALL_BREAKPOINTS (b)
665 {
666 if (func (b, user_data) != 0)
667 break;
668 }
669
670 return b;
671 }
672
673 /* Return whether a breakpoint is an active enabled breakpoint. */
674 static int
675 breakpoint_enabled (struct breakpoint *b)
676 {
677 return (b->enable_state == bp_enabled);
678 }
679
680 /* Set breakpoint count to NUM. */
681
682 static void
683 set_breakpoint_count (int num)
684 {
685 prev_breakpoint_count = breakpoint_count;
686 breakpoint_count = num;
687 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
688 }
689
690 /* Used by `start_rbreak_breakpoints' below, to record the current
691 breakpoint count before "rbreak" creates any breakpoint. */
692 static int rbreak_start_breakpoint_count;
693
694 /* Called at the start an "rbreak" command to record the first
695 breakpoint made. */
696
697 void
698 start_rbreak_breakpoints (void)
699 {
700 rbreak_start_breakpoint_count = breakpoint_count;
701 }
702
703 /* Called at the end of an "rbreak" command to record the last
704 breakpoint made. */
705
706 void
707 end_rbreak_breakpoints (void)
708 {
709 prev_breakpoint_count = rbreak_start_breakpoint_count;
710 }
711
712 /* Used in run_command to zero the hit count when a new run starts. */
713
714 void
715 clear_breakpoint_hit_counts (void)
716 {
717 struct breakpoint *b;
718
719 ALL_BREAKPOINTS (b)
720 b->hit_count = 0;
721 }
722
723 /* Allocate a new counted_command_line with reference count of 1.
724 The new structure owns COMMANDS. */
725
726 static struct counted_command_line *
727 alloc_counted_command_line (struct command_line *commands)
728 {
729 struct counted_command_line *result = XNEW (struct counted_command_line);
730
731 result->refc = 1;
732 result->commands = commands;
733
734 return result;
735 }
736
737 /* Increment reference count. This does nothing if CMD is NULL. */
738
739 static void
740 incref_counted_command_line (struct counted_command_line *cmd)
741 {
742 if (cmd)
743 ++cmd->refc;
744 }
745
746 /* Decrement reference count. If the reference count reaches 0,
747 destroy the counted_command_line. Sets *CMDP to NULL. This does
748 nothing if *CMDP is NULL. */
749
750 static void
751 decref_counted_command_line (struct counted_command_line **cmdp)
752 {
753 if (*cmdp)
754 {
755 if (--(*cmdp)->refc == 0)
756 {
757 free_command_lines (&(*cmdp)->commands);
758 xfree (*cmdp);
759 }
760 *cmdp = NULL;
761 }
762 }
763
764 /* A cleanup function that calls decref_counted_command_line. */
765
766 static void
767 do_cleanup_counted_command_line (void *arg)
768 {
769 decref_counted_command_line ((struct counted_command_line **) arg);
770 }
771
772 /* Create a cleanup that calls decref_counted_command_line on the
773 argument. */
774
775 static struct cleanup *
776 make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
777 {
778 return make_cleanup (do_cleanup_counted_command_line, cmdp);
779 }
780
781 \f
782 /* Return the breakpoint with the specified number, or NULL
783 if the number does not refer to an existing breakpoint. */
784
785 struct breakpoint *
786 get_breakpoint (int num)
787 {
788 struct breakpoint *b;
789
790 ALL_BREAKPOINTS (b)
791 if (b->number == num)
792 return b;
793
794 return NULL;
795 }
796
797 \f
798
799 /* Mark locations as "conditions have changed" in case the target supports
800 evaluating conditions on its side. */
801
802 static void
803 mark_breakpoint_modified (struct breakpoint *b)
804 {
805 struct bp_location *loc;
806
807 /* This is only meaningful if the target is
808 evaluating conditions and if the user has
809 opted for condition evaluation on the target's
810 side. */
811 if (gdb_evaluates_breakpoint_condition_p ()
812 || !target_supports_evaluation_of_breakpoint_conditions ())
813 return;
814
815 if (!is_breakpoint (b))
816 return;
817
818 for (loc = b->loc; loc; loc = loc->next)
819 loc->condition_changed = condition_modified;
820 }
821
822 /* Mark location as "conditions have changed" in case the target supports
823 evaluating conditions on its side. */
824
825 static void
826 mark_breakpoint_location_modified (struct bp_location *loc)
827 {
828 /* This is only meaningful if the target is
829 evaluating conditions and if the user has
830 opted for condition evaluation on the target's
831 side. */
832 if (gdb_evaluates_breakpoint_condition_p ()
833 || !target_supports_evaluation_of_breakpoint_conditions ())
834
835 return;
836
837 if (!is_breakpoint (loc->owner))
838 return;
839
840 loc->condition_changed = condition_modified;
841 }
842
843 /* Sets the condition-evaluation mode using the static global
844 condition_evaluation_mode. */
845
846 static void
847 set_condition_evaluation_mode (char *args, int from_tty,
848 struct cmd_list_element *c)
849 {
850 const char *old_mode, *new_mode;
851
852 if ((condition_evaluation_mode_1 == condition_evaluation_target)
853 && !target_supports_evaluation_of_breakpoint_conditions ())
854 {
855 condition_evaluation_mode_1 = condition_evaluation_mode;
856 warning (_("Target does not support breakpoint condition evaluation.\n"
857 "Using host evaluation mode instead."));
858 return;
859 }
860
861 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
862 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
863
864 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
865 settings was "auto". */
866 condition_evaluation_mode = condition_evaluation_mode_1;
867
868 /* Only update the mode if the user picked a different one. */
869 if (new_mode != old_mode)
870 {
871 struct bp_location *loc, **loc_tmp;
872 /* If the user switched to a different evaluation mode, we
873 need to synch the changes with the target as follows:
874
875 "host" -> "target": Send all (valid) conditions to the target.
876 "target" -> "host": Remove all the conditions from the target.
877 */
878
879 if (new_mode == condition_evaluation_target)
880 {
881 /* Mark everything modified and synch conditions with the
882 target. */
883 ALL_BP_LOCATIONS (loc, loc_tmp)
884 mark_breakpoint_location_modified (loc);
885 }
886 else
887 {
888 /* Manually mark non-duplicate locations to synch conditions
889 with the target. We do this to remove all the conditions the
890 target knows about. */
891 ALL_BP_LOCATIONS (loc, loc_tmp)
892 if (is_breakpoint (loc->owner) && loc->inserted)
893 loc->needs_update = 1;
894 }
895
896 /* Do the update. */
897 update_global_location_list (UGLL_MAY_INSERT);
898 }
899
900 return;
901 }
902
903 /* Shows the current mode of breakpoint condition evaluation. Explicitly shows
904 what "auto" is translating to. */
905
906 static void
907 show_condition_evaluation_mode (struct ui_file *file, int from_tty,
908 struct cmd_list_element *c, const char *value)
909 {
910 if (condition_evaluation_mode == condition_evaluation_auto)
911 fprintf_filtered (file,
912 _("Breakpoint condition evaluation "
913 "mode is %s (currently %s).\n"),
914 value,
915 breakpoint_condition_evaluation_mode ());
916 else
917 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
918 value);
919 }
920
921 /* A comparison function for bp_location AP and BP that is used by
922 bsearch. This comparison function only cares about addresses, unlike
923 the more general bp_locations_compare function. */
924
925 static int
926 bp_locations_compare_addrs (const void *ap, const void *bp)
927 {
928 const struct bp_location *a = *(const struct bp_location **) ap;
929 const struct bp_location *b = *(const struct bp_location **) bp;
930
931 if (a->address == b->address)
932 return 0;
933 else
934 return ((a->address > b->address) - (a->address < b->address));
935 }
936
937 /* Helper function to skip all bp_locations with addresses
938 less than ADDRESS. It returns the first bp_location that
939 is greater than or equal to ADDRESS. If none is found, just
940 return NULL. */
941
942 static struct bp_location **
943 get_first_locp_gte_addr (CORE_ADDR address)
944 {
945 struct bp_location dummy_loc;
946 struct bp_location *dummy_locp = &dummy_loc;
947 struct bp_location **locp_found = NULL;
948
949 /* Initialize the dummy location's address field. */
950 memset (&dummy_loc, 0, sizeof (struct bp_location));
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 /* Encapsulate tests for different types of tracepoints. */
1164
1165 static int
1166 is_tracepoint_type (enum bptype type)
1167 {
1168 return (type == bp_tracepoint
1169 || type == bp_fast_tracepoint
1170 || type == bp_static_tracepoint);
1171 }
1172
1173 int
1174 is_tracepoint (const struct breakpoint *b)
1175 {
1176 return is_tracepoint_type (b->type);
1177 }
1178
1179 /* A helper function that validates that COMMANDS are valid for a
1180 breakpoint. This function will throw an exception if a problem is
1181 found. */
1182
1183 static void
1184 validate_commands_for_breakpoint (struct breakpoint *b,
1185 struct command_line *commands)
1186 {
1187 if (is_tracepoint (b))
1188 {
1189 struct tracepoint *t = (struct tracepoint *) b;
1190 struct command_line *c;
1191 struct command_line *while_stepping = 0;
1192
1193 /* Reset the while-stepping step count. The previous commands
1194 might have included a while-stepping action, while the new
1195 ones might not. */
1196 t->step_count = 0;
1197
1198 /* We need to verify that each top-level element of commands is
1199 valid for tracepoints, that there's at most one
1200 while-stepping element, and that the while-stepping's body
1201 has valid tracing commands excluding nested while-stepping.
1202 We also need to validate the tracepoint action line in the
1203 context of the tracepoint --- validate_actionline actually
1204 has side effects, like setting the tracepoint's
1205 while-stepping STEP_COUNT, in addition to checking if the
1206 collect/teval actions parse and make sense in the
1207 tracepoint's context. */
1208 for (c = commands; c; c = c->next)
1209 {
1210 if (c->control_type == while_stepping_control)
1211 {
1212 if (b->type == bp_fast_tracepoint)
1213 error (_("The 'while-stepping' command "
1214 "cannot be used for fast tracepoint"));
1215 else if (b->type == bp_static_tracepoint)
1216 error (_("The 'while-stepping' command "
1217 "cannot be used for static tracepoint"));
1218
1219 if (while_stepping)
1220 error (_("The 'while-stepping' command "
1221 "can be used only once"));
1222 else
1223 while_stepping = c;
1224 }
1225
1226 validate_actionline (c->line, b);
1227 }
1228 if (while_stepping)
1229 {
1230 struct command_line *c2;
1231
1232 gdb_assert (while_stepping->body_count == 1);
1233 c2 = while_stepping->body_list[0];
1234 for (; c2; c2 = c2->next)
1235 {
1236 if (c2->control_type == while_stepping_control)
1237 error (_("The 'while-stepping' command cannot be nested"));
1238 }
1239 }
1240 }
1241 else
1242 {
1243 check_no_tracepoint_commands (commands);
1244 }
1245 }
1246
1247 /* Return a vector of all the static tracepoints set at ADDR. The
1248 caller is responsible for releasing the vector. */
1249
1250 VEC(breakpoint_p) *
1251 static_tracepoints_here (CORE_ADDR addr)
1252 {
1253 struct breakpoint *b;
1254 VEC(breakpoint_p) *found = 0;
1255 struct bp_location *loc;
1256
1257 ALL_BREAKPOINTS (b)
1258 if (b->type == bp_static_tracepoint)
1259 {
1260 for (loc = b->loc; loc; loc = loc->next)
1261 if (loc->address == addr)
1262 VEC_safe_push(breakpoint_p, found, b);
1263 }
1264
1265 return found;
1266 }
1267
1268 /* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
1269 validate that only allowed commands are included. */
1270
1271 void
1272 breakpoint_set_commands (struct breakpoint *b,
1273 command_line_up &&commands)
1274 {
1275 validate_commands_for_breakpoint (b, commands.get ());
1276
1277 decref_counted_command_line (&b->commands);
1278 b->commands = alloc_counted_command_line (commands.release ());
1279 observer_notify_breakpoint_modified (b);
1280 }
1281
1282 /* Set the internal `silent' flag on the breakpoint. Note that this
1283 is not the same as the "silent" that may appear in the breakpoint's
1284 commands. */
1285
1286 void
1287 breakpoint_set_silent (struct breakpoint *b, int silent)
1288 {
1289 int old_silent = b->silent;
1290
1291 b->silent = silent;
1292 if (old_silent != silent)
1293 observer_notify_breakpoint_modified (b);
1294 }
1295
1296 /* Set the thread for this breakpoint. If THREAD is -1, make the
1297 breakpoint work for any thread. */
1298
1299 void
1300 breakpoint_set_thread (struct breakpoint *b, int thread)
1301 {
1302 int old_thread = b->thread;
1303
1304 b->thread = thread;
1305 if (old_thread != thread)
1306 observer_notify_breakpoint_modified (b);
1307 }
1308
1309 /* Set the task for this breakpoint. If TASK is 0, make the
1310 breakpoint work for any task. */
1311
1312 void
1313 breakpoint_set_task (struct breakpoint *b, int task)
1314 {
1315 int old_task = b->task;
1316
1317 b->task = task;
1318 if (old_task != task)
1319 observer_notify_breakpoint_modified (b);
1320 }
1321
1322 void
1323 check_tracepoint_command (char *line, void *closure)
1324 {
1325 struct breakpoint *b = (struct breakpoint *) closure;
1326
1327 validate_actionline (line, b);
1328 }
1329
1330 /* A structure used to pass information through
1331 map_breakpoint_numbers. */
1332
1333 struct commands_info
1334 {
1335 /* True if the command was typed at a tty. */
1336 int from_tty;
1337
1338 /* The breakpoint range spec. */
1339 const char *arg;
1340
1341 /* Non-NULL if the body of the commands are being read from this
1342 already-parsed command. */
1343 struct command_line *control;
1344
1345 /* The command lines read from the user, or NULL if they have not
1346 yet been read. */
1347 struct counted_command_line *cmd;
1348 };
1349
1350 /* A callback for map_breakpoint_numbers that sets the commands for
1351 commands_command. */
1352
1353 static void
1354 do_map_commands_command (struct breakpoint *b, void *data)
1355 {
1356 struct commands_info *info = (struct commands_info *) data;
1357
1358 if (info->cmd == NULL)
1359 {
1360 command_line_up l;
1361
1362 if (info->control != NULL)
1363 l = copy_command_lines (info->control->body_list[0]);
1364 else
1365 {
1366 struct cleanup *old_chain;
1367 char *str;
1368
1369 str = xstrprintf (_("Type commands for breakpoint(s) "
1370 "%s, one per line."),
1371 info->arg);
1372
1373 old_chain = make_cleanup (xfree, str);
1374
1375 l = read_command_lines (str,
1376 info->from_tty, 1,
1377 (is_tracepoint (b)
1378 ? check_tracepoint_command : 0),
1379 b);
1380
1381 do_cleanups (old_chain);
1382 }
1383
1384 info->cmd = alloc_counted_command_line (l.release ());
1385 }
1386
1387 /* If a breakpoint was on the list more than once, we don't need to
1388 do anything. */
1389 if (b->commands != info->cmd)
1390 {
1391 validate_commands_for_breakpoint (b, info->cmd->commands);
1392 incref_counted_command_line (info->cmd);
1393 decref_counted_command_line (&b->commands);
1394 b->commands = info->cmd;
1395 observer_notify_breakpoint_modified (b);
1396 }
1397 }
1398
1399 static void
1400 commands_command_1 (const char *arg, int from_tty,
1401 struct command_line *control)
1402 {
1403 struct cleanup *cleanups;
1404 struct commands_info info;
1405
1406 info.from_tty = from_tty;
1407 info.control = control;
1408 info.cmd = NULL;
1409 /* If we read command lines from the user, then `info' will hold an
1410 extra reference to the commands that we must clean up. */
1411 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
1412
1413 std::string new_arg;
1414
1415 if (arg == NULL || !*arg)
1416 {
1417 if (breakpoint_count - prev_breakpoint_count > 1)
1418 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1419 breakpoint_count);
1420 else if (breakpoint_count > 0)
1421 new_arg = string_printf ("%d", breakpoint_count);
1422 }
1423 else
1424 new_arg = arg;
1425
1426 info.arg = new_arg.c_str ();
1427
1428 map_breakpoint_numbers (info.arg, do_map_commands_command, &info);
1429
1430 if (info.cmd == NULL)
1431 error (_("No breakpoints specified."));
1432
1433 do_cleanups (cleanups);
1434 }
1435
1436 static void
1437 commands_command (char *arg, int from_tty)
1438 {
1439 commands_command_1 (arg, from_tty, NULL);
1440 }
1441
1442 /* Like commands_command, but instead of reading the commands from
1443 input stream, takes them from an already parsed command structure.
1444
1445 This is used by cli-script.c to DTRT with breakpoint commands
1446 that are part of if and while bodies. */
1447 enum command_control_type
1448 commands_from_control_command (const char *arg, struct command_line *cmd)
1449 {
1450 commands_command_1 (arg, 0, cmd);
1451 return simple_control;
1452 }
1453
1454 /* Return non-zero if BL->TARGET_INFO contains valid information. */
1455
1456 static int
1457 bp_location_has_shadow (struct bp_location *bl)
1458 {
1459 if (bl->loc_type != bp_loc_software_breakpoint)
1460 return 0;
1461 if (!bl->inserted)
1462 return 0;
1463 if (bl->target_info.shadow_len == 0)
1464 /* BL isn't valid, or doesn't shadow memory. */
1465 return 0;
1466 return 1;
1467 }
1468
1469 /* Update BUF, which is LEN bytes read from the target address
1470 MEMADDR, by replacing a memory breakpoint with its shadowed
1471 contents.
1472
1473 If READBUF is not NULL, this buffer must not overlap with the of
1474 the breakpoint location's shadow_contents buffer. Otherwise, a
1475 failed assertion internal error will be raised. */
1476
1477 static void
1478 one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1479 const gdb_byte *writebuf_org,
1480 ULONGEST memaddr, LONGEST len,
1481 struct bp_target_info *target_info,
1482 struct gdbarch *gdbarch)
1483 {
1484 /* Now do full processing of the found relevant range of elements. */
1485 CORE_ADDR bp_addr = 0;
1486 int bp_size = 0;
1487 int bptoffset = 0;
1488
1489 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1490 current_program_space->aspace, 0))
1491 {
1492 /* The breakpoint is inserted in a different address space. */
1493 return;
1494 }
1495
1496 /* Addresses and length of the part of the breakpoint that
1497 we need to copy. */
1498 bp_addr = target_info->placed_address;
1499 bp_size = target_info->shadow_len;
1500
1501 if (bp_addr + bp_size <= memaddr)
1502 {
1503 /* The breakpoint is entirely before the chunk of memory we are
1504 reading. */
1505 return;
1506 }
1507
1508 if (bp_addr >= memaddr + len)
1509 {
1510 /* The breakpoint is entirely after the chunk of memory we are
1511 reading. */
1512 return;
1513 }
1514
1515 /* Offset within shadow_contents. */
1516 if (bp_addr < memaddr)
1517 {
1518 /* Only copy the second part of the breakpoint. */
1519 bp_size -= memaddr - bp_addr;
1520 bptoffset = memaddr - bp_addr;
1521 bp_addr = memaddr;
1522 }
1523
1524 if (bp_addr + bp_size > memaddr + len)
1525 {
1526 /* Only copy the first part of the breakpoint. */
1527 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1528 }
1529
1530 if (readbuf != NULL)
1531 {
1532 /* Verify that the readbuf buffer does not overlap with the
1533 shadow_contents buffer. */
1534 gdb_assert (target_info->shadow_contents >= readbuf + len
1535 || readbuf >= (target_info->shadow_contents
1536 + target_info->shadow_len));
1537
1538 /* Update the read buffer with this inserted breakpoint's
1539 shadow. */
1540 memcpy (readbuf + bp_addr - memaddr,
1541 target_info->shadow_contents + bptoffset, bp_size);
1542 }
1543 else
1544 {
1545 const unsigned char *bp;
1546 CORE_ADDR addr = target_info->reqstd_address;
1547 int placed_size;
1548
1549 /* Update the shadow with what we want to write to memory. */
1550 memcpy (target_info->shadow_contents + bptoffset,
1551 writebuf_org + bp_addr - memaddr, bp_size);
1552
1553 /* Determine appropriate breakpoint contents and size for this
1554 address. */
1555 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
1556
1557 /* Update the final write buffer with this inserted
1558 breakpoint's INSN. */
1559 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1560 }
1561 }
1562
1563 /* Update BUF, which is LEN bytes read from the target address MEMADDR,
1564 by replacing any memory breakpoints with their shadowed contents.
1565
1566 If READBUF is not NULL, this buffer must not overlap with any of
1567 the breakpoint location's shadow_contents buffers. Otherwise,
1568 a failed assertion internal error will be raised.
1569
1570 The range of shadowed area by each bp_location is:
1571 bl->address - bp_locations_placed_address_before_address_max
1572 up to bl->address + bp_locations_shadow_len_after_address_max
1573 The range we were requested to resolve shadows for is:
1574 memaddr ... memaddr + len
1575 Thus the safe cutoff boundaries for performance optimization are
1576 memaddr + len <= (bl->address
1577 - bp_locations_placed_address_before_address_max)
1578 and:
1579 bl->address + bp_locations_shadow_len_after_address_max <= memaddr */
1580
1581 void
1582 breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1583 const gdb_byte *writebuf_org,
1584 ULONGEST memaddr, LONGEST len)
1585 {
1586 /* Left boundary, right boundary and median element of our binary
1587 search. */
1588 unsigned bc_l, bc_r, bc;
1589
1590 /* Find BC_L which is a leftmost element which may affect BUF
1591 content. It is safe to report lower value but a failure to
1592 report higher one. */
1593
1594 bc_l = 0;
1595 bc_r = bp_locations_count;
1596 while (bc_l + 1 < bc_r)
1597 {
1598 struct bp_location *bl;
1599
1600 bc = (bc_l + bc_r) / 2;
1601 bl = bp_locations[bc];
1602
1603 /* Check first BL->ADDRESS will not overflow due to the added
1604 constant. Then advance the left boundary only if we are sure
1605 the BC element can in no way affect the BUF content (MEMADDR
1606 to MEMADDR + LEN range).
1607
1608 Use the BP_LOCATIONS_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1609 offset so that we cannot miss a breakpoint with its shadow
1610 range tail still reaching MEMADDR. */
1611
1612 if ((bl->address + bp_locations_shadow_len_after_address_max
1613 >= bl->address)
1614 && (bl->address + bp_locations_shadow_len_after_address_max
1615 <= memaddr))
1616 bc_l = bc;
1617 else
1618 bc_r = bc;
1619 }
1620
1621 /* Due to the binary search above, we need to make sure we pick the
1622 first location that's at BC_L's address. E.g., if there are
1623 multiple locations at the same address, BC_L may end up pointing
1624 at a duplicate location, and miss the "master"/"inserted"
1625 location. Say, given locations L1, L2 and L3 at addresses A and
1626 B:
1627
1628 L1@A, L2@A, L3@B, ...
1629
1630 BC_L could end up pointing at location L2, while the "master"
1631 location could be L1. Since the `loc->inserted' flag is only set
1632 on "master" locations, we'd forget to restore the shadow of L1
1633 and L2. */
1634 while (bc_l > 0
1635 && bp_locations[bc_l]->address == bp_locations[bc_l - 1]->address)
1636 bc_l--;
1637
1638 /* Now do full processing of the found relevant range of elements. */
1639
1640 for (bc = bc_l; bc < bp_locations_count; bc++)
1641 {
1642 struct bp_location *bl = bp_locations[bc];
1643
1644 /* bp_location array has BL->OWNER always non-NULL. */
1645 if (bl->owner->type == bp_none)
1646 warning (_("reading through apparently deleted breakpoint #%d?"),
1647 bl->owner->number);
1648
1649 /* Performance optimization: any further element can no longer affect BUF
1650 content. */
1651
1652 if (bl->address >= bp_locations_placed_address_before_address_max
1653 && memaddr + len <= (bl->address
1654 - bp_locations_placed_address_before_address_max))
1655 break;
1656
1657 if (!bp_location_has_shadow (bl))
1658 continue;
1659
1660 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1661 memaddr, len, &bl->target_info, bl->gdbarch);
1662 }
1663 }
1664
1665 \f
1666
1667 /* Return true if BPT is either a software breakpoint or a hardware
1668 breakpoint. */
1669
1670 int
1671 is_breakpoint (const struct breakpoint *bpt)
1672 {
1673 return (bpt->type == bp_breakpoint
1674 || bpt->type == bp_hardware_breakpoint
1675 || bpt->type == bp_dprintf);
1676 }
1677
1678 /* Return true if BPT is of any hardware watchpoint kind. */
1679
1680 static int
1681 is_hardware_watchpoint (const struct breakpoint *bpt)
1682 {
1683 return (bpt->type == bp_hardware_watchpoint
1684 || bpt->type == bp_read_watchpoint
1685 || bpt->type == bp_access_watchpoint);
1686 }
1687
1688 /* Return true if BPT is of any watchpoint kind, hardware or
1689 software. */
1690
1691 int
1692 is_watchpoint (const struct breakpoint *bpt)
1693 {
1694 return (is_hardware_watchpoint (bpt)
1695 || bpt->type == bp_watchpoint);
1696 }
1697
1698 /* Returns true if the current thread and its running state are safe
1699 to evaluate or update watchpoint B. Watchpoints on local
1700 expressions need to be evaluated in the context of the thread that
1701 was current when the watchpoint was created, and, that thread needs
1702 to be stopped to be able to select the correct frame context.
1703 Watchpoints on global expressions can be evaluated on any thread,
1704 and in any state. It is presently left to the target allowing
1705 memory accesses when threads are running. */
1706
1707 static int
1708 watchpoint_in_thread_scope (struct watchpoint *b)
1709 {
1710 return (b->base.pspace == current_program_space
1711 && (ptid_equal (b->watchpoint_thread, null_ptid)
1712 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1713 && !is_executing (inferior_ptid))));
1714 }
1715
1716 /* Set watchpoint B to disp_del_at_next_stop, even including its possible
1717 associated bp_watchpoint_scope breakpoint. */
1718
1719 static void
1720 watchpoint_del_at_next_stop (struct watchpoint *w)
1721 {
1722 struct breakpoint *b = &w->base;
1723
1724 if (b->related_breakpoint != b)
1725 {
1726 gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1727 gdb_assert (b->related_breakpoint->related_breakpoint == b);
1728 b->related_breakpoint->disposition = disp_del_at_next_stop;
1729 b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1730 b->related_breakpoint = b;
1731 }
1732 b->disposition = disp_del_at_next_stop;
1733 }
1734
1735 /* Extract a bitfield value from value VAL using the bit parameters contained in
1736 watchpoint W. */
1737
1738 static struct value *
1739 extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1740 {
1741 struct value *bit_val;
1742
1743 if (val == NULL)
1744 return NULL;
1745
1746 bit_val = allocate_value (value_type (val));
1747
1748 unpack_value_bitfield (bit_val,
1749 w->val_bitpos,
1750 w->val_bitsize,
1751 value_contents_for_printing (val),
1752 value_offset (val),
1753 val);
1754
1755 return bit_val;
1756 }
1757
1758 /* Allocate a dummy location and add it to B, which must be a software
1759 watchpoint. This is required because even if a software watchpoint
1760 is not watching any memory, bpstat_stop_status requires a location
1761 to be able to report stops. */
1762
1763 static void
1764 software_watchpoint_add_no_memory_location (struct breakpoint *b,
1765 struct program_space *pspace)
1766 {
1767 gdb_assert (b->type == bp_watchpoint && b->loc == NULL);
1768
1769 b->loc = allocate_bp_location (b);
1770 b->loc->pspace = pspace;
1771 b->loc->address = -1;
1772 b->loc->length = -1;
1773 }
1774
1775 /* Returns true if B is a software watchpoint that is not watching any
1776 memory (e.g., "watch $pc"). */
1777
1778 static int
1779 is_no_memory_software_watchpoint (struct breakpoint *b)
1780 {
1781 return (b->type == bp_watchpoint
1782 && b->loc != NULL
1783 && b->loc->next == NULL
1784 && b->loc->address == -1
1785 && b->loc->length == -1);
1786 }
1787
1788 /* Assuming that B is a watchpoint:
1789 - Reparse watchpoint expression, if REPARSE is non-zero
1790 - Evaluate expression and store the result in B->val
1791 - Evaluate the condition if there is one, and store the result
1792 in b->loc->cond.
1793 - Update the list of values that must be watched in B->loc.
1794
1795 If the watchpoint disposition is disp_del_at_next_stop, then do
1796 nothing. If this is local watchpoint that is out of scope, delete
1797 it.
1798
1799 Even with `set breakpoint always-inserted on' the watchpoints are
1800 removed + inserted on each stop here. Normal breakpoints must
1801 never be removed because they might be missed by a running thread
1802 when debugging in non-stop mode. On the other hand, hardware
1803 watchpoints (is_hardware_watchpoint; processed here) are specific
1804 to each LWP since they are stored in each LWP's hardware debug
1805 registers. Therefore, such LWP must be stopped first in order to
1806 be able to modify its hardware watchpoints.
1807
1808 Hardware watchpoints must be reset exactly once after being
1809 presented to the user. It cannot be done sooner, because it would
1810 reset the data used to present the watchpoint hit to the user. And
1811 it must not be done later because it could display the same single
1812 watchpoint hit during multiple GDB stops. Note that the latter is
1813 relevant only to the hardware watchpoint types bp_read_watchpoint
1814 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1815 not user-visible - its hit is suppressed if the memory content has
1816 not changed.
1817
1818 The following constraints influence the location where we can reset
1819 hardware watchpoints:
1820
1821 * target_stopped_by_watchpoint and target_stopped_data_address are
1822 called several times when GDB stops.
1823
1824 [linux]
1825 * Multiple hardware watchpoints can be hit at the same time,
1826 causing GDB to stop. GDB only presents one hardware watchpoint
1827 hit at a time as the reason for stopping, and all the other hits
1828 are presented later, one after the other, each time the user
1829 requests the execution to be resumed. Execution is not resumed
1830 for the threads still having pending hit event stored in
1831 LWP_INFO->STATUS. While the watchpoint is already removed from
1832 the inferior on the first stop the thread hit event is kept being
1833 reported from its cached value by linux_nat_stopped_data_address
1834 until the real thread resume happens after the watchpoint gets
1835 presented and thus its LWP_INFO->STATUS gets reset.
1836
1837 Therefore the hardware watchpoint hit can get safely reset on the
1838 watchpoint removal from inferior. */
1839
1840 static void
1841 update_watchpoint (struct watchpoint *b, int reparse)
1842 {
1843 int within_current_scope;
1844 struct frame_id saved_frame_id;
1845 int frame_saved;
1846
1847 /* If this is a local watchpoint, we only want to check if the
1848 watchpoint frame is in scope if the current thread is the thread
1849 that was used to create the watchpoint. */
1850 if (!watchpoint_in_thread_scope (b))
1851 return;
1852
1853 if (b->base.disposition == disp_del_at_next_stop)
1854 return;
1855
1856 frame_saved = 0;
1857
1858 /* Determine if the watchpoint is within scope. */
1859 if (b->exp_valid_block == NULL)
1860 within_current_scope = 1;
1861 else
1862 {
1863 struct frame_info *fi = get_current_frame ();
1864 struct gdbarch *frame_arch = get_frame_arch (fi);
1865 CORE_ADDR frame_pc = get_frame_pc (fi);
1866
1867 /* If we're at a point where the stack has been destroyed
1868 (e.g. in a function epilogue), unwinding may not work
1869 properly. Do not attempt to recreate locations at this
1870 point. See similar comments in watchpoint_check. */
1871 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
1872 return;
1873
1874 /* Save the current frame's ID so we can restore it after
1875 evaluating the watchpoint expression on its own frame. */
1876 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1877 took a frame parameter, so that we didn't have to change the
1878 selected frame. */
1879 frame_saved = 1;
1880 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1881
1882 fi = frame_find_by_id (b->watchpoint_frame);
1883 within_current_scope = (fi != NULL);
1884 if (within_current_scope)
1885 select_frame (fi);
1886 }
1887
1888 /* We don't free locations. They are stored in the bp_location array
1889 and update_global_location_list will eventually delete them and
1890 remove breakpoints if needed. */
1891 b->base.loc = NULL;
1892
1893 if (within_current_scope && reparse)
1894 {
1895 const char *s;
1896
1897 b->exp.reset ();
1898 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1899 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
1900 /* If the meaning of expression itself changed, the old value is
1901 no longer relevant. We don't want to report a watchpoint hit
1902 to the user when the old value and the new value may actually
1903 be completely different objects. */
1904 value_free (b->val);
1905 b->val = NULL;
1906 b->val_valid = 0;
1907
1908 /* Note that unlike with breakpoints, the watchpoint's condition
1909 expression is stored in the breakpoint object, not in the
1910 locations (re)created below. */
1911 if (b->base.cond_string != NULL)
1912 {
1913 b->cond_exp.reset ();
1914
1915 s = b->base.cond_string;
1916 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
1917 }
1918 }
1919
1920 /* If we failed to parse the expression, for example because
1921 it refers to a global variable in a not-yet-loaded shared library,
1922 don't try to insert watchpoint. We don't automatically delete
1923 such watchpoint, though, since failure to parse expression
1924 is different from out-of-scope watchpoint. */
1925 if (!target_has_execution)
1926 {
1927 /* Without execution, memory can't change. No use to try and
1928 set watchpoint locations. The watchpoint will be reset when
1929 the target gains execution, through breakpoint_re_set. */
1930 if (!can_use_hw_watchpoints)
1931 {
1932 if (b->base.ops->works_in_software_mode (&b->base))
1933 b->base.type = bp_watchpoint;
1934 else
1935 error (_("Can't set read/access watchpoint when "
1936 "hardware watchpoints are disabled."));
1937 }
1938 }
1939 else if (within_current_scope && b->exp)
1940 {
1941 int pc = 0;
1942 struct value *val_chain, *v, *result, *next;
1943 struct program_space *frame_pspace;
1944
1945 fetch_subexp_value (b->exp.get (), &pc, &v, &result, &val_chain, 0);
1946
1947 /* Avoid setting b->val if it's already set. The meaning of
1948 b->val is 'the last value' user saw, and we should update
1949 it only if we reported that last value to user. As it
1950 happens, the code that reports it updates b->val directly.
1951 We don't keep track of the memory value for masked
1952 watchpoints. */
1953 if (!b->val_valid && !is_masked_watchpoint (&b->base))
1954 {
1955 if (b->val_bitsize != 0)
1956 {
1957 v = extract_bitfield_from_watchpoint_value (b, v);
1958 if (v != NULL)
1959 release_value (v);
1960 }
1961 b->val = v;
1962 b->val_valid = 1;
1963 }
1964
1965 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1966
1967 /* Look at each value on the value chain. */
1968 for (v = val_chain; v; v = value_next (v))
1969 {
1970 /* If it's a memory location, and GDB actually needed
1971 its contents to evaluate the expression, then we
1972 must watch it. If the first value returned is
1973 still lazy, that means an error occurred reading it;
1974 watch it anyway in case it becomes readable. */
1975 if (VALUE_LVAL (v) == lval_memory
1976 && (v == val_chain || ! value_lazy (v)))
1977 {
1978 struct type *vtype = check_typedef (value_type (v));
1979
1980 /* We only watch structs and arrays if user asked
1981 for it explicitly, never if they just happen to
1982 appear in the middle of some value chain. */
1983 if (v == result
1984 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1985 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1986 {
1987 CORE_ADDR addr;
1988 enum target_hw_bp_type type;
1989 struct bp_location *loc, **tmp;
1990 int bitpos = 0, bitsize = 0;
1991
1992 if (value_bitsize (v) != 0)
1993 {
1994 /* Extract the bit parameters out from the bitfield
1995 sub-expression. */
1996 bitpos = value_bitpos (v);
1997 bitsize = value_bitsize (v);
1998 }
1999 else if (v == result && b->val_bitsize != 0)
2000 {
2001 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
2002 lvalue whose bit parameters are saved in the fields
2003 VAL_BITPOS and VAL_BITSIZE. */
2004 bitpos = b->val_bitpos;
2005 bitsize = b->val_bitsize;
2006 }
2007
2008 addr = value_address (v);
2009 if (bitsize != 0)
2010 {
2011 /* Skip the bytes that don't contain the bitfield. */
2012 addr += bitpos / 8;
2013 }
2014
2015 type = hw_write;
2016 if (b->base.type == bp_read_watchpoint)
2017 type = hw_read;
2018 else if (b->base.type == bp_access_watchpoint)
2019 type = hw_access;
2020
2021 loc = allocate_bp_location (&b->base);
2022 for (tmp = &(b->base.loc); *tmp != NULL; tmp = &((*tmp)->next))
2023 ;
2024 *tmp = loc;
2025 loc->gdbarch = get_type_arch (value_type (v));
2026
2027 loc->pspace = frame_pspace;
2028 loc->address = addr;
2029
2030 if (bitsize != 0)
2031 {
2032 /* Just cover the bytes that make up the bitfield. */
2033 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2034 }
2035 else
2036 loc->length = TYPE_LENGTH (value_type (v));
2037
2038 loc->watchpoint_type = type;
2039 }
2040 }
2041 }
2042
2043 /* Change the type of breakpoint between hardware assisted or
2044 an ordinary watchpoint depending on the hardware support
2045 and free hardware slots. REPARSE is set when the inferior
2046 is started. */
2047 if (reparse)
2048 {
2049 int reg_cnt;
2050 enum bp_loc_type loc_type;
2051 struct bp_location *bl;
2052
2053 reg_cnt = can_use_hardware_watchpoint (val_chain);
2054
2055 if (reg_cnt)
2056 {
2057 int i, target_resources_ok, other_type_used;
2058 enum bptype type;
2059
2060 /* Use an exact watchpoint when there's only one memory region to be
2061 watched, and only one debug register is needed to watch it. */
2062 b->exact = target_exact_watchpoints && reg_cnt == 1;
2063
2064 /* We need to determine how many resources are already
2065 used for all other hardware watchpoints plus this one
2066 to see if we still have enough resources to also fit
2067 this watchpoint in as well. */
2068
2069 /* If this is a software watchpoint, we try to turn it
2070 to a hardware one -- count resources as if B was of
2071 hardware watchpoint type. */
2072 type = b->base.type;
2073 if (type == bp_watchpoint)
2074 type = bp_hardware_watchpoint;
2075
2076 /* This watchpoint may or may not have been placed on
2077 the list yet at this point (it won't be in the list
2078 if we're trying to create it for the first time,
2079 through watch_command), so always account for it
2080 manually. */
2081
2082 /* Count resources used by all watchpoints except B. */
2083 i = hw_watchpoint_used_count_others (&b->base, type, &other_type_used);
2084
2085 /* Add in the resources needed for B. */
2086 i += hw_watchpoint_use_count (&b->base);
2087
2088 target_resources_ok
2089 = target_can_use_hardware_watchpoint (type, i, other_type_used);
2090 if (target_resources_ok <= 0)
2091 {
2092 int sw_mode = b->base.ops->works_in_software_mode (&b->base);
2093
2094 if (target_resources_ok == 0 && !sw_mode)
2095 error (_("Target does not support this type of "
2096 "hardware watchpoint."));
2097 else if (target_resources_ok < 0 && !sw_mode)
2098 error (_("There are not enough available hardware "
2099 "resources for this watchpoint."));
2100
2101 /* Downgrade to software watchpoint. */
2102 b->base.type = bp_watchpoint;
2103 }
2104 else
2105 {
2106 /* If this was a software watchpoint, we've just
2107 found we have enough resources to turn it to a
2108 hardware watchpoint. Otherwise, this is a
2109 nop. */
2110 b->base.type = type;
2111 }
2112 }
2113 else if (!b->base.ops->works_in_software_mode (&b->base))
2114 {
2115 if (!can_use_hw_watchpoints)
2116 error (_("Can't set read/access watchpoint when "
2117 "hardware watchpoints are disabled."));
2118 else
2119 error (_("Expression cannot be implemented with "
2120 "read/access watchpoint."));
2121 }
2122 else
2123 b->base.type = bp_watchpoint;
2124
2125 loc_type = (b->base.type == bp_watchpoint? bp_loc_other
2126 : bp_loc_hardware_watchpoint);
2127 for (bl = b->base.loc; bl; bl = bl->next)
2128 bl->loc_type = loc_type;
2129 }
2130
2131 for (v = val_chain; v; v = next)
2132 {
2133 next = value_next (v);
2134 if (v != b->val)
2135 value_free (v);
2136 }
2137
2138 /* If a software watchpoint is not watching any memory, then the
2139 above left it without any location set up. But,
2140 bpstat_stop_status requires a location to be able to report
2141 stops, so make sure there's at least a dummy one. */
2142 if (b->base.type == bp_watchpoint && b->base.loc == NULL)
2143 software_watchpoint_add_no_memory_location (&b->base, frame_pspace);
2144 }
2145 else if (!within_current_scope)
2146 {
2147 printf_filtered (_("\
2148 Watchpoint %d deleted because the program has left the block\n\
2149 in which its expression is valid.\n"),
2150 b->base.number);
2151 watchpoint_del_at_next_stop (b);
2152 }
2153
2154 /* Restore the selected frame. */
2155 if (frame_saved)
2156 select_frame (frame_find_by_id (saved_frame_id));
2157 }
2158
2159
2160 /* Returns 1 iff breakpoint location should be
2161 inserted in the inferior. We don't differentiate the type of BL's owner
2162 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2163 breakpoint_ops is not defined, because in insert_bp_location,
2164 tracepoint's insert_location will not be called. */
2165 static int
2166 should_be_inserted (struct bp_location *bl)
2167 {
2168 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
2169 return 0;
2170
2171 if (bl->owner->disposition == disp_del_at_next_stop)
2172 return 0;
2173
2174 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
2175 return 0;
2176
2177 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2178 return 0;
2179
2180 /* This is set for example, when we're attached to the parent of a
2181 vfork, and have detached from the child. The child is running
2182 free, and we expect it to do an exec or exit, at which point the
2183 OS makes the parent schedulable again (and the target reports
2184 that the vfork is done). Until the child is done with the shared
2185 memory region, do not insert breakpoints in the parent, otherwise
2186 the child could still trip on the parent's breakpoints. Since
2187 the parent is blocked anyway, it won't miss any breakpoint. */
2188 if (bl->pspace->breakpoints_not_allowed)
2189 return 0;
2190
2191 /* Don't insert a breakpoint if we're trying to step past its
2192 location, except if the breakpoint is a single-step breakpoint,
2193 and the breakpoint's thread is the thread which is stepping past
2194 a breakpoint. */
2195 if ((bl->loc_type == bp_loc_software_breakpoint
2196 || bl->loc_type == bp_loc_hardware_breakpoint)
2197 && stepping_past_instruction_at (bl->pspace->aspace,
2198 bl->address)
2199 /* The single-step breakpoint may be inserted at the location
2200 we're trying to step if the instruction branches to itself.
2201 However, the instruction won't be executed at all and it may
2202 break the semantics of the instruction, for example, the
2203 instruction is a conditional branch or updates some flags.
2204 We can't fix it unless GDB is able to emulate the instruction
2205 or switch to displaced stepping. */
2206 && !(bl->owner->type == bp_single_step
2207 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
2208 {
2209 if (debug_infrun)
2210 {
2211 fprintf_unfiltered (gdb_stdlog,
2212 "infrun: skipping breakpoint: "
2213 "stepping past insn at: %s\n",
2214 paddress (bl->gdbarch, bl->address));
2215 }
2216 return 0;
2217 }
2218
2219 /* Don't insert watchpoints if we're trying to step past the
2220 instruction that triggered one. */
2221 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2222 && stepping_past_nonsteppable_watchpoint ())
2223 {
2224 if (debug_infrun)
2225 {
2226 fprintf_unfiltered (gdb_stdlog,
2227 "infrun: stepping past non-steppable watchpoint. "
2228 "skipping watchpoint at %s:%d\n",
2229 paddress (bl->gdbarch, bl->address),
2230 bl->length);
2231 }
2232 return 0;
2233 }
2234
2235 return 1;
2236 }
2237
2238 /* Same as should_be_inserted but does the check assuming
2239 that the location is not duplicated. */
2240
2241 static int
2242 unduplicated_should_be_inserted (struct bp_location *bl)
2243 {
2244 int result;
2245 const int save_duplicate = bl->duplicate;
2246
2247 bl->duplicate = 0;
2248 result = should_be_inserted (bl);
2249 bl->duplicate = save_duplicate;
2250 return result;
2251 }
2252
2253 /* Parses a conditional described by an expression COND into an
2254 agent expression bytecode suitable for evaluation
2255 by the bytecode interpreter. Return NULL if there was
2256 any error during parsing. */
2257
2258 static agent_expr_up
2259 parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2260 {
2261 if (cond == NULL)
2262 return NULL;
2263
2264 agent_expr_up aexpr;
2265
2266 /* We don't want to stop processing, so catch any errors
2267 that may show up. */
2268 TRY
2269 {
2270 aexpr = gen_eval_for_expr (scope, cond);
2271 }
2272
2273 CATCH (ex, RETURN_MASK_ERROR)
2274 {
2275 /* If we got here, it means the condition could not be parsed to a valid
2276 bytecode expression and thus can't be evaluated on the target's side.
2277 It's no use iterating through the conditions. */
2278 }
2279 END_CATCH
2280
2281 /* We have a valid agent expression. */
2282 return aexpr;
2283 }
2284
2285 /* Based on location BL, create a list of breakpoint conditions to be
2286 passed on to the target. If we have duplicated locations with different
2287 conditions, we will add such conditions to the list. The idea is that the
2288 target will evaluate the list of conditions and will only notify GDB when
2289 one of them is true. */
2290
2291 static void
2292 build_target_condition_list (struct bp_location *bl)
2293 {
2294 struct bp_location **locp = NULL, **loc2p;
2295 int null_condition_or_parse_error = 0;
2296 int modified = bl->needs_update;
2297 struct bp_location *loc;
2298
2299 /* Release conditions left over from a previous insert. */
2300 bl->target_info.conditions.clear ();
2301
2302 /* This is only meaningful if the target is
2303 evaluating conditions and if the user has
2304 opted for condition evaluation on the target's
2305 side. */
2306 if (gdb_evaluates_breakpoint_condition_p ()
2307 || !target_supports_evaluation_of_breakpoint_conditions ())
2308 return;
2309
2310 /* Do a first pass to check for locations with no assigned
2311 conditions or conditions that fail to parse to a valid agent expression
2312 bytecode. If any of these happen, then it's no use to send conditions
2313 to the target since this location will always trigger and generate a
2314 response back to GDB. */
2315 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2316 {
2317 loc = (*loc2p);
2318 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2319 {
2320 if (modified)
2321 {
2322 /* Re-parse the conditions since something changed. In that
2323 case we already freed the condition bytecodes (see
2324 force_breakpoint_reinsertion). We just
2325 need to parse the condition to bytecodes again. */
2326 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2327 loc->cond.get ());
2328 }
2329
2330 /* If we have a NULL bytecode expression, it means something
2331 went wrong or we have a null condition expression. */
2332 if (!loc->cond_bytecode)
2333 {
2334 null_condition_or_parse_error = 1;
2335 break;
2336 }
2337 }
2338 }
2339
2340 /* If any of these happened, it means we will have to evaluate the conditions
2341 for the location's address on gdb's side. It is no use keeping bytecodes
2342 for all the other duplicate locations, thus we free all of them here.
2343
2344 This is so we have a finer control over which locations' conditions are
2345 being evaluated by GDB or the remote stub. */
2346 if (null_condition_or_parse_error)
2347 {
2348 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2349 {
2350 loc = (*loc2p);
2351 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2352 {
2353 /* Only go as far as the first NULL bytecode is
2354 located. */
2355 if (!loc->cond_bytecode)
2356 return;
2357
2358 loc->cond_bytecode.reset ();
2359 }
2360 }
2361 }
2362
2363 /* No NULL conditions or failed bytecode generation. Build a condition list
2364 for this location's address. */
2365 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2366 {
2367 loc = (*loc2p);
2368 if (loc->cond
2369 && is_breakpoint (loc->owner)
2370 && loc->pspace->num == bl->pspace->num
2371 && loc->owner->enable_state == bp_enabled
2372 && loc->enabled)
2373 {
2374 /* Add the condition to the vector. This will be used later
2375 to send the conditions to the target. */
2376 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2377 }
2378 }
2379
2380 return;
2381 }
2382
2383 /* Parses a command described by string CMD into an agent expression
2384 bytecode suitable for evaluation by the bytecode interpreter.
2385 Return NULL if there was any error during parsing. */
2386
2387 static agent_expr_up
2388 parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2389 {
2390 struct cleanup *old_cleanups = 0;
2391 struct expression **argvec;
2392 const char *cmdrest;
2393 const char *format_start, *format_end;
2394 struct format_piece *fpieces;
2395 int nargs;
2396 struct gdbarch *gdbarch = get_current_arch ();
2397
2398 if (cmd == NULL)
2399 return NULL;
2400
2401 cmdrest = cmd;
2402
2403 if (*cmdrest == ',')
2404 ++cmdrest;
2405 cmdrest = skip_spaces_const (cmdrest);
2406
2407 if (*cmdrest++ != '"')
2408 error (_("No format string following the location"));
2409
2410 format_start = cmdrest;
2411
2412 fpieces = parse_format_string (&cmdrest);
2413
2414 old_cleanups = make_cleanup (free_format_pieces_cleanup, &fpieces);
2415
2416 format_end = cmdrest;
2417
2418 if (*cmdrest++ != '"')
2419 error (_("Bad format string, non-terminated '\"'."));
2420
2421 cmdrest = skip_spaces_const (cmdrest);
2422
2423 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2424 error (_("Invalid argument syntax"));
2425
2426 if (*cmdrest == ',')
2427 cmdrest++;
2428 cmdrest = skip_spaces_const (cmdrest);
2429
2430 /* For each argument, make an expression. */
2431
2432 argvec = (struct expression **) alloca (strlen (cmd)
2433 * sizeof (struct expression *));
2434
2435 nargs = 0;
2436 while (*cmdrest != '\0')
2437 {
2438 const char *cmd1;
2439
2440 cmd1 = cmdrest;
2441 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2442 argvec[nargs++] = expr.release ();
2443 cmdrest = cmd1;
2444 if (*cmdrest == ',')
2445 ++cmdrest;
2446 }
2447
2448 agent_expr_up aexpr;
2449
2450 /* We don't want to stop processing, so catch any errors
2451 that may show up. */
2452 TRY
2453 {
2454 aexpr = gen_printf (scope, gdbarch, 0, 0,
2455 format_start, format_end - format_start,
2456 fpieces, nargs, argvec);
2457 }
2458 CATCH (ex, RETURN_MASK_ERROR)
2459 {
2460 /* If we got here, it means the command could not be parsed to a valid
2461 bytecode expression and thus can't be evaluated on the target's side.
2462 It's no use iterating through the other commands. */
2463 }
2464 END_CATCH
2465
2466 do_cleanups (old_cleanups);
2467
2468 /* We have a valid agent expression, return it. */
2469 return aexpr;
2470 }
2471
2472 /* Based on location BL, create a list of breakpoint commands to be
2473 passed on to the target. If we have duplicated locations with
2474 different commands, we will add any such to the list. */
2475
2476 static void
2477 build_target_command_list (struct bp_location *bl)
2478 {
2479 struct bp_location **locp = NULL, **loc2p;
2480 int null_command_or_parse_error = 0;
2481 int modified = bl->needs_update;
2482 struct bp_location *loc;
2483
2484 /* Clear commands left over from a previous insert. */
2485 bl->target_info.tcommands.clear ();
2486
2487 if (!target_can_run_breakpoint_commands ())
2488 return;
2489
2490 /* For now, limit to agent-style dprintf breakpoints. */
2491 if (dprintf_style != dprintf_style_agent)
2492 return;
2493
2494 /* For now, if we have any duplicate location that isn't a dprintf,
2495 don't install the target-side commands, as that would make the
2496 breakpoint not be reported to the core, and we'd lose
2497 control. */
2498 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2499 {
2500 loc = (*loc2p);
2501 if (is_breakpoint (loc->owner)
2502 && loc->pspace->num == bl->pspace->num
2503 && loc->owner->type != bp_dprintf)
2504 return;
2505 }
2506
2507 /* Do a first pass to check for locations with no assigned
2508 conditions or conditions that fail to parse to a valid agent expression
2509 bytecode. If any of these happen, then it's no use to send conditions
2510 to the target since this location will always trigger and generate a
2511 response back to GDB. */
2512 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2513 {
2514 loc = (*loc2p);
2515 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2516 {
2517 if (modified)
2518 {
2519 /* Re-parse the commands since something changed. In that
2520 case we already freed the command bytecodes (see
2521 force_breakpoint_reinsertion). We just
2522 need to parse the command to bytecodes again. */
2523 loc->cmd_bytecode
2524 = parse_cmd_to_aexpr (bl->address,
2525 loc->owner->extra_string);
2526 }
2527
2528 /* If we have a NULL bytecode expression, it means something
2529 went wrong or we have a null command expression. */
2530 if (!loc->cmd_bytecode)
2531 {
2532 null_command_or_parse_error = 1;
2533 break;
2534 }
2535 }
2536 }
2537
2538 /* If anything failed, then we're not doing target-side commands,
2539 and so clean up. */
2540 if (null_command_or_parse_error)
2541 {
2542 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2543 {
2544 loc = (*loc2p);
2545 if (is_breakpoint (loc->owner)
2546 && loc->pspace->num == bl->pspace->num)
2547 {
2548 /* Only go as far as the first NULL bytecode is
2549 located. */
2550 if (loc->cmd_bytecode == NULL)
2551 return;
2552
2553 loc->cmd_bytecode.reset ();
2554 }
2555 }
2556 }
2557
2558 /* No NULL commands or failed bytecode generation. Build a command list
2559 for this location's address. */
2560 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2561 {
2562 loc = (*loc2p);
2563 if (loc->owner->extra_string
2564 && is_breakpoint (loc->owner)
2565 && loc->pspace->num == bl->pspace->num
2566 && loc->owner->enable_state == bp_enabled
2567 && loc->enabled)
2568 {
2569 /* Add the command to the vector. This will be used later
2570 to send the commands to the target. */
2571 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2572 }
2573 }
2574
2575 bl->target_info.persist = 0;
2576 /* Maybe flag this location as persistent. */
2577 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2578 bl->target_info.persist = 1;
2579 }
2580
2581 /* Return the kind of breakpoint on address *ADDR. Get the kind
2582 of breakpoint according to ADDR except single-step breakpoint.
2583 Get the kind of single-step breakpoint according to the current
2584 registers state. */
2585
2586 static int
2587 breakpoint_kind (struct bp_location *bl, CORE_ADDR *addr)
2588 {
2589 if (bl->owner->type == bp_single_step)
2590 {
2591 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2592 struct regcache *regcache;
2593
2594 regcache = get_thread_regcache (thr->ptid);
2595
2596 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2597 regcache, addr);
2598 }
2599 else
2600 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
2601 }
2602
2603 /* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2604 location. Any error messages are printed to TMP_ERROR_STREAM; and
2605 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
2606 Returns 0 for success, 1 if the bp_location type is not supported or
2607 -1 for failure.
2608
2609 NOTE drow/2003-09-09: This routine could be broken down to an
2610 object-style method for each breakpoint or catchpoint type. */
2611 static int
2612 insert_bp_location (struct bp_location *bl,
2613 struct ui_file *tmp_error_stream,
2614 int *disabled_breaks,
2615 int *hw_breakpoint_error,
2616 int *hw_bp_error_explained_already)
2617 {
2618 enum errors bp_err = GDB_NO_ERROR;
2619 const char *bp_err_message = NULL;
2620
2621 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2622 return 0;
2623
2624 /* Note we don't initialize bl->target_info, as that wipes out
2625 the breakpoint location's shadow_contents if the breakpoint
2626 is still inserted at that location. This in turn breaks
2627 target_read_memory which depends on these buffers when
2628 a memory read is requested at the breakpoint location:
2629 Once the target_info has been wiped, we fail to see that
2630 we have a breakpoint inserted at that address and thus
2631 read the breakpoint instead of returning the data saved in
2632 the breakpoint location's shadow contents. */
2633 bl->target_info.reqstd_address = bl->address;
2634 bl->target_info.placed_address_space = bl->pspace->aspace;
2635 bl->target_info.length = bl->length;
2636
2637 /* When working with target-side conditions, we must pass all the conditions
2638 for the same breakpoint address down to the target since GDB will not
2639 insert those locations. With a list of breakpoint conditions, the target
2640 can decide when to stop and notify GDB. */
2641
2642 if (is_breakpoint (bl->owner))
2643 {
2644 build_target_condition_list (bl);
2645 build_target_command_list (bl);
2646 /* Reset the modification marker. */
2647 bl->needs_update = 0;
2648 }
2649
2650 if (bl->loc_type == bp_loc_software_breakpoint
2651 || bl->loc_type == bp_loc_hardware_breakpoint)
2652 {
2653 if (bl->owner->type != bp_hardware_breakpoint)
2654 {
2655 /* If the explicitly specified breakpoint type
2656 is not hardware breakpoint, check the memory map to see
2657 if the breakpoint address is in read only memory or not.
2658
2659 Two important cases are:
2660 - location type is not hardware breakpoint, memory
2661 is readonly. We change the type of the location to
2662 hardware breakpoint.
2663 - location type is hardware breakpoint, memory is
2664 read-write. This means we've previously made the
2665 location hardware one, but then the memory map changed,
2666 so we undo.
2667
2668 When breakpoints are removed, remove_breakpoints will use
2669 location types we've just set here, the only possible
2670 problem is that memory map has changed during running
2671 program, but it's not going to work anyway with current
2672 gdb. */
2673 struct mem_region *mr
2674 = lookup_mem_region (bl->target_info.reqstd_address);
2675
2676 if (mr)
2677 {
2678 if (automatic_hardware_breakpoints)
2679 {
2680 enum bp_loc_type new_type;
2681
2682 if (mr->attrib.mode != MEM_RW)
2683 new_type = bp_loc_hardware_breakpoint;
2684 else
2685 new_type = bp_loc_software_breakpoint;
2686
2687 if (new_type != bl->loc_type)
2688 {
2689 static int said = 0;
2690
2691 bl->loc_type = new_type;
2692 if (!said)
2693 {
2694 fprintf_filtered (gdb_stdout,
2695 _("Note: automatically using "
2696 "hardware breakpoints for "
2697 "read-only addresses.\n"));
2698 said = 1;
2699 }
2700 }
2701 }
2702 else if (bl->loc_type == bp_loc_software_breakpoint
2703 && mr->attrib.mode != MEM_RW)
2704 {
2705 fprintf_unfiltered (tmp_error_stream,
2706 _("Cannot insert breakpoint %d.\n"
2707 "Cannot set software breakpoint "
2708 "at read-only address %s\n"),
2709 bl->owner->number,
2710 paddress (bl->gdbarch, bl->address));
2711 return 1;
2712 }
2713 }
2714 }
2715
2716 /* First check to see if we have to handle an overlay. */
2717 if (overlay_debugging == ovly_off
2718 || bl->section == NULL
2719 || !(section_is_overlay (bl->section)))
2720 {
2721 /* No overlay handling: just set the breakpoint. */
2722 TRY
2723 {
2724 int val;
2725
2726 val = bl->owner->ops->insert_location (bl);
2727 if (val)
2728 bp_err = GENERIC_ERROR;
2729 }
2730 CATCH (e, RETURN_MASK_ALL)
2731 {
2732 bp_err = e.error;
2733 bp_err_message = e.message;
2734 }
2735 END_CATCH
2736 }
2737 else
2738 {
2739 /* This breakpoint is in an overlay section.
2740 Shall we set a breakpoint at the LMA? */
2741 if (!overlay_events_enabled)
2742 {
2743 /* Yes -- overlay event support is not active,
2744 so we must try to set a breakpoint at the LMA.
2745 This will not work for a hardware breakpoint. */
2746 if (bl->loc_type == bp_loc_hardware_breakpoint)
2747 warning (_("hardware breakpoint %d not supported in overlay!"),
2748 bl->owner->number);
2749 else
2750 {
2751 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2752 bl->section);
2753 /* Set a software (trap) breakpoint at the LMA. */
2754 bl->overlay_target_info = bl->target_info;
2755 bl->overlay_target_info.reqstd_address = addr;
2756
2757 /* No overlay handling: just set the breakpoint. */
2758 TRY
2759 {
2760 int val;
2761
2762 bl->overlay_target_info.kind
2763 = breakpoint_kind (bl, &addr);
2764 bl->overlay_target_info.placed_address = addr;
2765 val = target_insert_breakpoint (bl->gdbarch,
2766 &bl->overlay_target_info);
2767 if (val)
2768 bp_err = GENERIC_ERROR;
2769 }
2770 CATCH (e, RETURN_MASK_ALL)
2771 {
2772 bp_err = e.error;
2773 bp_err_message = e.message;
2774 }
2775 END_CATCH
2776
2777 if (bp_err != GDB_NO_ERROR)
2778 fprintf_unfiltered (tmp_error_stream,
2779 "Overlay breakpoint %d "
2780 "failed: in ROM?\n",
2781 bl->owner->number);
2782 }
2783 }
2784 /* Shall we set a breakpoint at the VMA? */
2785 if (section_is_mapped (bl->section))
2786 {
2787 /* Yes. This overlay section is mapped into memory. */
2788 TRY
2789 {
2790 int val;
2791
2792 val = bl->owner->ops->insert_location (bl);
2793 if (val)
2794 bp_err = GENERIC_ERROR;
2795 }
2796 CATCH (e, RETURN_MASK_ALL)
2797 {
2798 bp_err = e.error;
2799 bp_err_message = e.message;
2800 }
2801 END_CATCH
2802 }
2803 else
2804 {
2805 /* No. This breakpoint will not be inserted.
2806 No error, but do not mark the bp as 'inserted'. */
2807 return 0;
2808 }
2809 }
2810
2811 if (bp_err != GDB_NO_ERROR)
2812 {
2813 /* Can't set the breakpoint. */
2814
2815 /* In some cases, we might not be able to insert a
2816 breakpoint in a shared library that has already been
2817 removed, but we have not yet processed the shlib unload
2818 event. Unfortunately, some targets that implement
2819 breakpoint insertion themselves can't tell why the
2820 breakpoint insertion failed (e.g., the remote target
2821 doesn't define error codes), so we must treat generic
2822 errors as memory errors. */
2823 if ((bp_err == GENERIC_ERROR || bp_err == MEMORY_ERROR)
2824 && bl->loc_type == bp_loc_software_breakpoint
2825 && (solib_name_from_address (bl->pspace, bl->address)
2826 || shared_objfile_contains_address_p (bl->pspace,
2827 bl->address)))
2828 {
2829 /* See also: disable_breakpoints_in_shlibs. */
2830 bl->shlib_disabled = 1;
2831 observer_notify_breakpoint_modified (bl->owner);
2832 if (!*disabled_breaks)
2833 {
2834 fprintf_unfiltered (tmp_error_stream,
2835 "Cannot insert breakpoint %d.\n",
2836 bl->owner->number);
2837 fprintf_unfiltered (tmp_error_stream,
2838 "Temporarily disabling shared "
2839 "library breakpoints:\n");
2840 }
2841 *disabled_breaks = 1;
2842 fprintf_unfiltered (tmp_error_stream,
2843 "breakpoint #%d\n", bl->owner->number);
2844 return 0;
2845 }
2846 else
2847 {
2848 if (bl->loc_type == bp_loc_hardware_breakpoint)
2849 {
2850 *hw_breakpoint_error = 1;
2851 *hw_bp_error_explained_already = bp_err_message != NULL;
2852 fprintf_unfiltered (tmp_error_stream,
2853 "Cannot insert hardware breakpoint %d%s",
2854 bl->owner->number, bp_err_message ? ":" : ".\n");
2855 if (bp_err_message != NULL)
2856 fprintf_unfiltered (tmp_error_stream, "%s.\n", bp_err_message);
2857 }
2858 else
2859 {
2860 if (bp_err_message == NULL)
2861 {
2862 char *message
2863 = memory_error_message (TARGET_XFER_E_IO,
2864 bl->gdbarch, bl->address);
2865 struct cleanup *old_chain = make_cleanup (xfree, message);
2866
2867 fprintf_unfiltered (tmp_error_stream,
2868 "Cannot insert breakpoint %d.\n"
2869 "%s\n",
2870 bl->owner->number, message);
2871 do_cleanups (old_chain);
2872 }
2873 else
2874 {
2875 fprintf_unfiltered (tmp_error_stream,
2876 "Cannot insert breakpoint %d: %s\n",
2877 bl->owner->number,
2878 bp_err_message);
2879 }
2880 }
2881 return 1;
2882
2883 }
2884 }
2885 else
2886 bl->inserted = 1;
2887
2888 return 0;
2889 }
2890
2891 else if (bl->loc_type == bp_loc_hardware_watchpoint
2892 /* NOTE drow/2003-09-08: This state only exists for removing
2893 watchpoints. It's not clear that it's necessary... */
2894 && bl->owner->disposition != disp_del_at_next_stop)
2895 {
2896 int val;
2897
2898 gdb_assert (bl->owner->ops != NULL
2899 && bl->owner->ops->insert_location != NULL);
2900
2901 val = bl->owner->ops->insert_location (bl);
2902
2903 /* If trying to set a read-watchpoint, and it turns out it's not
2904 supported, try emulating one with an access watchpoint. */
2905 if (val == 1 && bl->watchpoint_type == hw_read)
2906 {
2907 struct bp_location *loc, **loc_temp;
2908
2909 /* But don't try to insert it, if there's already another
2910 hw_access location that would be considered a duplicate
2911 of this one. */
2912 ALL_BP_LOCATIONS (loc, loc_temp)
2913 if (loc != bl
2914 && loc->watchpoint_type == hw_access
2915 && watchpoint_locations_match (bl, loc))
2916 {
2917 bl->duplicate = 1;
2918 bl->inserted = 1;
2919 bl->target_info = loc->target_info;
2920 bl->watchpoint_type = hw_access;
2921 val = 0;
2922 break;
2923 }
2924
2925 if (val == 1)
2926 {
2927 bl->watchpoint_type = hw_access;
2928 val = bl->owner->ops->insert_location (bl);
2929
2930 if (val)
2931 /* Back to the original value. */
2932 bl->watchpoint_type = hw_read;
2933 }
2934 }
2935
2936 bl->inserted = (val == 0);
2937 }
2938
2939 else if (bl->owner->type == bp_catchpoint)
2940 {
2941 int val;
2942
2943 gdb_assert (bl->owner->ops != NULL
2944 && bl->owner->ops->insert_location != NULL);
2945
2946 val = bl->owner->ops->insert_location (bl);
2947 if (val)
2948 {
2949 bl->owner->enable_state = bp_disabled;
2950
2951 if (val == 1)
2952 warning (_("\
2953 Error inserting catchpoint %d: Your system does not support this type\n\
2954 of catchpoint."), bl->owner->number);
2955 else
2956 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2957 }
2958
2959 bl->inserted = (val == 0);
2960
2961 /* We've already printed an error message if there was a problem
2962 inserting this catchpoint, and we've disabled the catchpoint,
2963 so just return success. */
2964 return 0;
2965 }
2966
2967 return 0;
2968 }
2969
2970 /* This function is called when program space PSPACE is about to be
2971 deleted. It takes care of updating breakpoints to not reference
2972 PSPACE anymore. */
2973
2974 void
2975 breakpoint_program_space_exit (struct program_space *pspace)
2976 {
2977 struct breakpoint *b, *b_temp;
2978 struct bp_location *loc, **loc_temp;
2979
2980 /* Remove any breakpoint that was set through this program space. */
2981 ALL_BREAKPOINTS_SAFE (b, b_temp)
2982 {
2983 if (b->pspace == pspace)
2984 delete_breakpoint (b);
2985 }
2986
2987 /* Breakpoints set through other program spaces could have locations
2988 bound to PSPACE as well. Remove those. */
2989 ALL_BP_LOCATIONS (loc, loc_temp)
2990 {
2991 struct bp_location *tmp;
2992
2993 if (loc->pspace == pspace)
2994 {
2995 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
2996 if (loc->owner->loc == loc)
2997 loc->owner->loc = loc->next;
2998 else
2999 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
3000 if (tmp->next == loc)
3001 {
3002 tmp->next = loc->next;
3003 break;
3004 }
3005 }
3006 }
3007
3008 /* Now update the global location list to permanently delete the
3009 removed locations above. */
3010 update_global_location_list (UGLL_DONT_INSERT);
3011 }
3012
3013 /* Make sure all breakpoints are inserted in inferior.
3014 Throws exception on any error.
3015 A breakpoint that is already inserted won't be inserted
3016 again, so calling this function twice is safe. */
3017 void
3018 insert_breakpoints (void)
3019 {
3020 struct breakpoint *bpt;
3021
3022 ALL_BREAKPOINTS (bpt)
3023 if (is_hardware_watchpoint (bpt))
3024 {
3025 struct watchpoint *w = (struct watchpoint *) bpt;
3026
3027 update_watchpoint (w, 0 /* don't reparse. */);
3028 }
3029
3030 /* Updating watchpoints creates new locations, so update the global
3031 location list. Explicitly tell ugll to insert locations and
3032 ignore breakpoints_always_inserted_mode. */
3033 update_global_location_list (UGLL_INSERT);
3034 }
3035
3036 /* Invoke CALLBACK for each of bp_location. */
3037
3038 void
3039 iterate_over_bp_locations (walk_bp_location_callback callback)
3040 {
3041 struct bp_location *loc, **loc_tmp;
3042
3043 ALL_BP_LOCATIONS (loc, loc_tmp)
3044 {
3045 callback (loc, NULL);
3046 }
3047 }
3048
3049 /* This is used when we need to synch breakpoint conditions between GDB and the
3050 target. It is the case with deleting and disabling of breakpoints when using
3051 always-inserted mode. */
3052
3053 static void
3054 update_inserted_breakpoint_locations (void)
3055 {
3056 struct bp_location *bl, **blp_tmp;
3057 int error_flag = 0;
3058 int val = 0;
3059 int disabled_breaks = 0;
3060 int hw_breakpoint_error = 0;
3061 int hw_bp_details_reported = 0;
3062
3063 string_file tmp_error_stream;
3064
3065 /* Explicitly mark the warning -- this will only be printed if
3066 there was an error. */
3067 tmp_error_stream.puts ("Warning:\n");
3068
3069 struct cleanup *cleanups = save_current_space_and_thread ();
3070
3071 ALL_BP_LOCATIONS (bl, blp_tmp)
3072 {
3073 /* We only want to update software breakpoints and hardware
3074 breakpoints. */
3075 if (!is_breakpoint (bl->owner))
3076 continue;
3077
3078 /* We only want to update locations that are already inserted
3079 and need updating. This is to avoid unwanted insertion during
3080 deletion of breakpoints. */
3081 if (!bl->inserted || (bl->inserted && !bl->needs_update))
3082 continue;
3083
3084 switch_to_program_space_and_thread (bl->pspace);
3085
3086 /* For targets that support global breakpoints, there's no need
3087 to select an inferior to insert breakpoint to. In fact, even
3088 if we aren't attached to any process yet, we should still
3089 insert breakpoints. */
3090 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3091 && ptid_equal (inferior_ptid, null_ptid))
3092 continue;
3093
3094 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
3095 &hw_breakpoint_error, &hw_bp_details_reported);
3096 if (val)
3097 error_flag = val;
3098 }
3099
3100 if (error_flag)
3101 {
3102 target_terminal_ours_for_output ();
3103 error_stream (tmp_error_stream);
3104 }
3105
3106 do_cleanups (cleanups);
3107 }
3108
3109 /* Used when starting or continuing the program. */
3110
3111 static void
3112 insert_breakpoint_locations (void)
3113 {
3114 struct breakpoint *bpt;
3115 struct bp_location *bl, **blp_tmp;
3116 int error_flag = 0;
3117 int val = 0;
3118 int disabled_breaks = 0;
3119 int hw_breakpoint_error = 0;
3120 int hw_bp_error_explained_already = 0;
3121
3122 string_file tmp_error_stream;
3123
3124 /* Explicitly mark the warning -- this will only be printed if
3125 there was an error. */
3126 tmp_error_stream.puts ("Warning:\n");
3127
3128 struct cleanup *cleanups = save_current_space_and_thread ();
3129
3130 ALL_BP_LOCATIONS (bl, blp_tmp)
3131 {
3132 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
3133 continue;
3134
3135 /* There is no point inserting thread-specific breakpoints if
3136 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3137 has BL->OWNER always non-NULL. */
3138 if (bl->owner->thread != -1
3139 && !valid_global_thread_id (bl->owner->thread))
3140 continue;
3141
3142 switch_to_program_space_and_thread (bl->pspace);
3143
3144 /* For targets that support global breakpoints, there's no need
3145 to select an inferior to insert breakpoint to. In fact, even
3146 if we aren't attached to any process yet, we should still
3147 insert breakpoints. */
3148 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3149 && ptid_equal (inferior_ptid, null_ptid))
3150 continue;
3151
3152 val = insert_bp_location (bl, &tmp_error_stream, &disabled_breaks,
3153 &hw_breakpoint_error, &hw_bp_error_explained_already);
3154 if (val)
3155 error_flag = val;
3156 }
3157
3158 /* If we failed to insert all locations of a watchpoint, remove
3159 them, as half-inserted watchpoint is of limited use. */
3160 ALL_BREAKPOINTS (bpt)
3161 {
3162 int some_failed = 0;
3163 struct bp_location *loc;
3164
3165 if (!is_hardware_watchpoint (bpt))
3166 continue;
3167
3168 if (!breakpoint_enabled (bpt))
3169 continue;
3170
3171 if (bpt->disposition == disp_del_at_next_stop)
3172 continue;
3173
3174 for (loc = bpt->loc; loc; loc = loc->next)
3175 if (!loc->inserted && should_be_inserted (loc))
3176 {
3177 some_failed = 1;
3178 break;
3179 }
3180 if (some_failed)
3181 {
3182 for (loc = bpt->loc; loc; loc = loc->next)
3183 if (loc->inserted)
3184 remove_breakpoint (loc);
3185
3186 hw_breakpoint_error = 1;
3187 tmp_error_stream.printf ("Could not insert "
3188 "hardware watchpoint %d.\n",
3189 bpt->number);
3190 error_flag = -1;
3191 }
3192 }
3193
3194 if (error_flag)
3195 {
3196 /* If a hardware breakpoint or watchpoint was inserted, add a
3197 message about possibly exhausted resources. */
3198 if (hw_breakpoint_error && !hw_bp_error_explained_already)
3199 {
3200 tmp_error_stream.printf ("Could not insert hardware breakpoints:\n\
3201 You may have requested too many hardware breakpoints/watchpoints.\n");
3202 }
3203 target_terminal_ours_for_output ();
3204 error_stream (tmp_error_stream);
3205 }
3206
3207 do_cleanups (cleanups);
3208 }
3209
3210 /* Used when the program stops.
3211 Returns zero if successful, or non-zero if there was a problem
3212 removing a breakpoint location. */
3213
3214 int
3215 remove_breakpoints (void)
3216 {
3217 struct bp_location *bl, **blp_tmp;
3218 int val = 0;
3219
3220 ALL_BP_LOCATIONS (bl, blp_tmp)
3221 {
3222 if (bl->inserted && !is_tracepoint (bl->owner))
3223 val |= remove_breakpoint (bl);
3224 }
3225 return val;
3226 }
3227
3228 /* When a thread exits, remove breakpoints that are related to
3229 that thread. */
3230
3231 static void
3232 remove_threaded_breakpoints (struct thread_info *tp, int silent)
3233 {
3234 struct breakpoint *b, *b_tmp;
3235
3236 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3237 {
3238 if (b->thread == tp->global_num && user_breakpoint_p (b))
3239 {
3240 b->disposition = disp_del_at_next_stop;
3241
3242 printf_filtered (_("\
3243 Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3244 b->number, print_thread_id (tp));
3245
3246 /* Hide it from the user. */
3247 b->number = 0;
3248 }
3249 }
3250 }
3251
3252 /* Remove breakpoints of process PID. */
3253
3254 int
3255 remove_breakpoints_pid (int pid)
3256 {
3257 struct bp_location *bl, **blp_tmp;
3258 int val;
3259 struct inferior *inf = find_inferior_pid (pid);
3260
3261 ALL_BP_LOCATIONS (bl, blp_tmp)
3262 {
3263 if (bl->pspace != inf->pspace)
3264 continue;
3265
3266 if (bl->inserted && !bl->target_info.persist)
3267 {
3268 val = remove_breakpoint (bl);
3269 if (val != 0)
3270 return val;
3271 }
3272 }
3273 return 0;
3274 }
3275
3276 int
3277 reattach_breakpoints (int pid)
3278 {
3279 struct cleanup *old_chain;
3280 struct bp_location *bl, **blp_tmp;
3281 int val;
3282 int dummy1 = 0, dummy2 = 0, dummy3 = 0;
3283 struct inferior *inf;
3284 struct thread_info *tp;
3285
3286 tp = any_live_thread_of_process (pid);
3287 if (tp == NULL)
3288 return 1;
3289
3290 inf = find_inferior_pid (pid);
3291 old_chain = save_inferior_ptid ();
3292
3293 inferior_ptid = tp->ptid;
3294
3295 string_file tmp_error_stream;
3296
3297 ALL_BP_LOCATIONS (bl, blp_tmp)
3298 {
3299 if (bl->pspace != inf->pspace)
3300 continue;
3301
3302 if (bl->inserted)
3303 {
3304 bl->inserted = 0;
3305 val = insert_bp_location (bl, &tmp_error_stream, &dummy1, &dummy2, &dummy3);
3306 if (val != 0)
3307 {
3308 do_cleanups (old_chain);
3309 return val;
3310 }
3311 }
3312 }
3313 do_cleanups (old_chain);
3314 return 0;
3315 }
3316
3317 static int internal_breakpoint_number = -1;
3318
3319 /* Set the breakpoint number of B, depending on the value of INTERNAL.
3320 If INTERNAL is non-zero, the breakpoint number will be populated
3321 from internal_breakpoint_number and that variable decremented.
3322 Otherwise the breakpoint number will be populated from
3323 breakpoint_count and that value incremented. Internal breakpoints
3324 do not set the internal var bpnum. */
3325 static void
3326 set_breakpoint_number (int internal, struct breakpoint *b)
3327 {
3328 if (internal)
3329 b->number = internal_breakpoint_number--;
3330 else
3331 {
3332 set_breakpoint_count (breakpoint_count + 1);
3333 b->number = breakpoint_count;
3334 }
3335 }
3336
3337 static struct breakpoint *
3338 create_internal_breakpoint (struct gdbarch *gdbarch,
3339 CORE_ADDR address, enum bptype type,
3340 const struct breakpoint_ops *ops)
3341 {
3342 struct symtab_and_line sal;
3343 struct breakpoint *b;
3344
3345 init_sal (&sal); /* Initialize to zeroes. */
3346
3347 sal.pc = address;
3348 sal.section = find_pc_overlay (sal.pc);
3349 sal.pspace = current_program_space;
3350
3351 b = set_raw_breakpoint (gdbarch, sal, type, ops);
3352 b->number = internal_breakpoint_number--;
3353 b->disposition = disp_donttouch;
3354
3355 return b;
3356 }
3357
3358 static const char *const longjmp_names[] =
3359 {
3360 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3361 };
3362 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3363
3364 /* Per-objfile data private to breakpoint.c. */
3365 struct breakpoint_objfile_data
3366 {
3367 /* Minimal symbol for "_ovly_debug_event" (if any). */
3368 struct bound_minimal_symbol overlay_msym;
3369
3370 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3371 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
3372
3373 /* True if we have looked for longjmp probes. */
3374 int longjmp_searched;
3375
3376 /* SystemTap probe points for longjmp (if any). */
3377 VEC (probe_p) *longjmp_probes;
3378
3379 /* Minimal symbol for "std::terminate()" (if any). */
3380 struct bound_minimal_symbol terminate_msym;
3381
3382 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3383 struct bound_minimal_symbol exception_msym;
3384
3385 /* True if we have looked for exception probes. */
3386 int exception_searched;
3387
3388 /* SystemTap probe points for unwinding (if any). */
3389 VEC (probe_p) *exception_probes;
3390 };
3391
3392 static const struct objfile_data *breakpoint_objfile_key;
3393
3394 /* Minimal symbol not found sentinel. */
3395 static struct minimal_symbol msym_not_found;
3396
3397 /* Returns TRUE if MSYM point to the "not found" sentinel. */
3398
3399 static int
3400 msym_not_found_p (const struct minimal_symbol *msym)
3401 {
3402 return msym == &msym_not_found;
3403 }
3404
3405 /* Return per-objfile data needed by breakpoint.c.
3406 Allocate the data if necessary. */
3407
3408 static struct breakpoint_objfile_data *
3409 get_breakpoint_objfile_data (struct objfile *objfile)
3410 {
3411 struct breakpoint_objfile_data *bp_objfile_data;
3412
3413 bp_objfile_data = ((struct breakpoint_objfile_data *)
3414 objfile_data (objfile, breakpoint_objfile_key));
3415 if (bp_objfile_data == NULL)
3416 {
3417 bp_objfile_data =
3418 XOBNEW (&objfile->objfile_obstack, struct breakpoint_objfile_data);
3419
3420 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
3421 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3422 }
3423 return bp_objfile_data;
3424 }
3425
3426 static void
3427 free_breakpoint_probes (struct objfile *obj, void *data)
3428 {
3429 struct breakpoint_objfile_data *bp_objfile_data
3430 = (struct breakpoint_objfile_data *) data;
3431
3432 VEC_free (probe_p, bp_objfile_data->longjmp_probes);
3433 VEC_free (probe_p, bp_objfile_data->exception_probes);
3434 }
3435
3436 static void
3437 create_overlay_event_breakpoint (void)
3438 {
3439 struct objfile *objfile;
3440 const char *const func_name = "_ovly_debug_event";
3441
3442 ALL_OBJFILES (objfile)
3443 {
3444 struct breakpoint *b;
3445 struct breakpoint_objfile_data *bp_objfile_data;
3446 CORE_ADDR addr;
3447 struct explicit_location explicit_loc;
3448
3449 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3450
3451 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
3452 continue;
3453
3454 if (bp_objfile_data->overlay_msym.minsym == NULL)
3455 {
3456 struct bound_minimal_symbol m;
3457
3458 m = lookup_minimal_symbol_text (func_name, objfile);
3459 if (m.minsym == NULL)
3460 {
3461 /* Avoid future lookups in this objfile. */
3462 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
3463 continue;
3464 }
3465 bp_objfile_data->overlay_msym = m;
3466 }
3467
3468 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
3469 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3470 bp_overlay_event,
3471 &internal_breakpoint_ops);
3472 initialize_explicit_location (&explicit_loc);
3473 explicit_loc.function_name = ASTRDUP (func_name);
3474 b->location = new_explicit_location (&explicit_loc);
3475
3476 if (overlay_debugging == ovly_auto)
3477 {
3478 b->enable_state = bp_enabled;
3479 overlay_events_enabled = 1;
3480 }
3481 else
3482 {
3483 b->enable_state = bp_disabled;
3484 overlay_events_enabled = 0;
3485 }
3486 }
3487 }
3488
3489 static void
3490 create_longjmp_master_breakpoint (void)
3491 {
3492 struct program_space *pspace;
3493 struct cleanup *old_chain;
3494
3495 old_chain = save_current_program_space ();
3496
3497 ALL_PSPACES (pspace)
3498 {
3499 struct objfile *objfile;
3500
3501 set_current_program_space (pspace);
3502
3503 ALL_OBJFILES (objfile)
3504 {
3505 int i;
3506 struct gdbarch *gdbarch;
3507 struct breakpoint_objfile_data *bp_objfile_data;
3508
3509 gdbarch = get_objfile_arch (objfile);
3510
3511 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3512
3513 if (!bp_objfile_data->longjmp_searched)
3514 {
3515 VEC (probe_p) *ret;
3516
3517 ret = find_probes_in_objfile (objfile, "libc", "longjmp");
3518 if (ret != NULL)
3519 {
3520 /* We are only interested in checking one element. */
3521 struct probe *p = VEC_index (probe_p, ret, 0);
3522
3523 if (!can_evaluate_probe_arguments (p))
3524 {
3525 /* We cannot use the probe interface here, because it does
3526 not know how to evaluate arguments. */
3527 VEC_free (probe_p, ret);
3528 ret = NULL;
3529 }
3530 }
3531 bp_objfile_data->longjmp_probes = ret;
3532 bp_objfile_data->longjmp_searched = 1;
3533 }
3534
3535 if (bp_objfile_data->longjmp_probes != NULL)
3536 {
3537 int i;
3538 struct probe *probe;
3539 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3540
3541 for (i = 0;
3542 VEC_iterate (probe_p,
3543 bp_objfile_data->longjmp_probes,
3544 i, probe);
3545 ++i)
3546 {
3547 struct breakpoint *b;
3548
3549 b = create_internal_breakpoint (gdbarch,
3550 get_probe_address (probe,
3551 objfile),
3552 bp_longjmp_master,
3553 &internal_breakpoint_ops);
3554 b->location = new_probe_location ("-probe-stap libc:longjmp");
3555 b->enable_state = bp_disabled;
3556 }
3557
3558 continue;
3559 }
3560
3561 if (!gdbarch_get_longjmp_target_p (gdbarch))
3562 continue;
3563
3564 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
3565 {
3566 struct breakpoint *b;
3567 const char *func_name;
3568 CORE_ADDR addr;
3569 struct explicit_location explicit_loc;
3570
3571 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3572 continue;
3573
3574 func_name = longjmp_names[i];
3575 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3576 {
3577 struct bound_minimal_symbol m;
3578
3579 m = lookup_minimal_symbol_text (func_name, objfile);
3580 if (m.minsym == NULL)
3581 {
3582 /* Prevent future lookups in this objfile. */
3583 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
3584 continue;
3585 }
3586 bp_objfile_data->longjmp_msym[i] = m;
3587 }
3588
3589 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
3590 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3591 &internal_breakpoint_ops);
3592 initialize_explicit_location (&explicit_loc);
3593 explicit_loc.function_name = ASTRDUP (func_name);
3594 b->location = new_explicit_location (&explicit_loc);
3595 b->enable_state = bp_disabled;
3596 }
3597 }
3598 }
3599
3600 do_cleanups (old_chain);
3601 }
3602
3603 /* Create a master std::terminate breakpoint. */
3604 static void
3605 create_std_terminate_master_breakpoint (void)
3606 {
3607 struct program_space *pspace;
3608 struct cleanup *old_chain;
3609 const char *const func_name = "std::terminate()";
3610
3611 old_chain = save_current_program_space ();
3612
3613 ALL_PSPACES (pspace)
3614 {
3615 struct objfile *objfile;
3616 CORE_ADDR addr;
3617
3618 set_current_program_space (pspace);
3619
3620 ALL_OBJFILES (objfile)
3621 {
3622 struct breakpoint *b;
3623 struct breakpoint_objfile_data *bp_objfile_data;
3624 struct explicit_location explicit_loc;
3625
3626 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3627
3628 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3629 continue;
3630
3631 if (bp_objfile_data->terminate_msym.minsym == NULL)
3632 {
3633 struct bound_minimal_symbol m;
3634
3635 m = lookup_minimal_symbol (func_name, NULL, objfile);
3636 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3637 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
3638 {
3639 /* Prevent future lookups in this objfile. */
3640 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3641 continue;
3642 }
3643 bp_objfile_data->terminate_msym = m;
3644 }
3645
3646 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3647 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3648 bp_std_terminate_master,
3649 &internal_breakpoint_ops);
3650 initialize_explicit_location (&explicit_loc);
3651 explicit_loc.function_name = ASTRDUP (func_name);
3652 b->location = new_explicit_location (&explicit_loc);
3653 b->enable_state = bp_disabled;
3654 }
3655 }
3656
3657 do_cleanups (old_chain);
3658 }
3659
3660 /* Install a master breakpoint on the unwinder's debug hook. */
3661
3662 static void
3663 create_exception_master_breakpoint (void)
3664 {
3665 struct objfile *objfile;
3666 const char *const func_name = "_Unwind_DebugHook";
3667
3668 ALL_OBJFILES (objfile)
3669 {
3670 struct breakpoint *b;
3671 struct gdbarch *gdbarch;
3672 struct breakpoint_objfile_data *bp_objfile_data;
3673 CORE_ADDR addr;
3674 struct explicit_location explicit_loc;
3675
3676 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3677
3678 /* We prefer the SystemTap probe point if it exists. */
3679 if (!bp_objfile_data->exception_searched)
3680 {
3681 VEC (probe_p) *ret;
3682
3683 ret = find_probes_in_objfile (objfile, "libgcc", "unwind");
3684
3685 if (ret != NULL)
3686 {
3687 /* We are only interested in checking one element. */
3688 struct probe *p = VEC_index (probe_p, ret, 0);
3689
3690 if (!can_evaluate_probe_arguments (p))
3691 {
3692 /* We cannot use the probe interface here, because it does
3693 not know how to evaluate arguments. */
3694 VEC_free (probe_p, ret);
3695 ret = NULL;
3696 }
3697 }
3698 bp_objfile_data->exception_probes = ret;
3699 bp_objfile_data->exception_searched = 1;
3700 }
3701
3702 if (bp_objfile_data->exception_probes != NULL)
3703 {
3704 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3705 int i;
3706 struct probe *probe;
3707
3708 for (i = 0;
3709 VEC_iterate (probe_p,
3710 bp_objfile_data->exception_probes,
3711 i, probe);
3712 ++i)
3713 {
3714 struct breakpoint *b;
3715
3716 b = create_internal_breakpoint (gdbarch,
3717 get_probe_address (probe,
3718 objfile),
3719 bp_exception_master,
3720 &internal_breakpoint_ops);
3721 b->location = new_probe_location ("-probe-stap libgcc:unwind");
3722 b->enable_state = bp_disabled;
3723 }
3724
3725 continue;
3726 }
3727
3728 /* Otherwise, try the hook function. */
3729
3730 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3731 continue;
3732
3733 gdbarch = get_objfile_arch (objfile);
3734
3735 if (bp_objfile_data->exception_msym.minsym == NULL)
3736 {
3737 struct bound_minimal_symbol debug_hook;
3738
3739 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3740 if (debug_hook.minsym == NULL)
3741 {
3742 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3743 continue;
3744 }
3745
3746 bp_objfile_data->exception_msym = debug_hook;
3747 }
3748
3749 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
3750 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3751 &current_target);
3752 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3753 &internal_breakpoint_ops);
3754 initialize_explicit_location (&explicit_loc);
3755 explicit_loc.function_name = ASTRDUP (func_name);
3756 b->location = new_explicit_location (&explicit_loc);
3757 b->enable_state = bp_disabled;
3758 }
3759 }
3760
3761 /* Does B have a location spec? */
3762
3763 static int
3764 breakpoint_event_location_empty_p (const struct breakpoint *b)
3765 {
3766 return b->location != NULL && event_location_empty_p (b->location.get ());
3767 }
3768
3769 void
3770 update_breakpoints_after_exec (void)
3771 {
3772 struct breakpoint *b, *b_tmp;
3773 struct bp_location *bploc, **bplocp_tmp;
3774
3775 /* We're about to delete breakpoints from GDB's lists. If the
3776 INSERTED flag is true, GDB will try to lift the breakpoints by
3777 writing the breakpoints' "shadow contents" back into memory. The
3778 "shadow contents" are NOT valid after an exec, so GDB should not
3779 do that. Instead, the target is responsible from marking
3780 breakpoints out as soon as it detects an exec. We don't do that
3781 here instead, because there may be other attempts to delete
3782 breakpoints after detecting an exec and before reaching here. */
3783 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
3784 if (bploc->pspace == current_program_space)
3785 gdb_assert (!bploc->inserted);
3786
3787 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3788 {
3789 if (b->pspace != current_program_space)
3790 continue;
3791
3792 /* Solib breakpoints must be explicitly reset after an exec(). */
3793 if (b->type == bp_shlib_event)
3794 {
3795 delete_breakpoint (b);
3796 continue;
3797 }
3798
3799 /* JIT breakpoints must be explicitly reset after an exec(). */
3800 if (b->type == bp_jit_event)
3801 {
3802 delete_breakpoint (b);
3803 continue;
3804 }
3805
3806 /* Thread event breakpoints must be set anew after an exec(),
3807 as must overlay event and longjmp master breakpoints. */
3808 if (b->type == bp_thread_event || b->type == bp_overlay_event
3809 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3810 || b->type == bp_exception_master)
3811 {
3812 delete_breakpoint (b);
3813 continue;
3814 }
3815
3816 /* Step-resume breakpoints are meaningless after an exec(). */
3817 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3818 {
3819 delete_breakpoint (b);
3820 continue;
3821 }
3822
3823 /* Just like single-step breakpoints. */
3824 if (b->type == bp_single_step)
3825 {
3826 delete_breakpoint (b);
3827 continue;
3828 }
3829
3830 /* Longjmp and longjmp-resume breakpoints are also meaningless
3831 after an exec. */
3832 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3833 || b->type == bp_longjmp_call_dummy
3834 || b->type == bp_exception || b->type == bp_exception_resume)
3835 {
3836 delete_breakpoint (b);
3837 continue;
3838 }
3839
3840 if (b->type == bp_catchpoint)
3841 {
3842 /* For now, none of the bp_catchpoint breakpoints need to
3843 do anything at this point. In the future, if some of
3844 the catchpoints need to something, we will need to add
3845 a new method, and call this method from here. */
3846 continue;
3847 }
3848
3849 /* bp_finish is a special case. The only way we ought to be able
3850 to see one of these when an exec() has happened, is if the user
3851 caught a vfork, and then said "finish". Ordinarily a finish just
3852 carries them to the call-site of the current callee, by setting
3853 a temporary bp there and resuming. But in this case, the finish
3854 will carry them entirely through the vfork & exec.
3855
3856 We don't want to allow a bp_finish to remain inserted now. But
3857 we can't safely delete it, 'cause finish_command has a handle to
3858 the bp on a bpstat, and will later want to delete it. There's a
3859 chance (and I've seen it happen) that if we delete the bp_finish
3860 here, that its storage will get reused by the time finish_command
3861 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3862 We really must allow finish_command to delete a bp_finish.
3863
3864 In the absence of a general solution for the "how do we know
3865 it's safe to delete something others may have handles to?"
3866 problem, what we'll do here is just uninsert the bp_finish, and
3867 let finish_command delete it.
3868
3869 (We know the bp_finish is "doomed" in the sense that it's
3870 momentary, and will be deleted as soon as finish_command sees
3871 the inferior stopped. So it doesn't matter that the bp's
3872 address is probably bogus in the new a.out, unlike e.g., the
3873 solib breakpoints.) */
3874
3875 if (b->type == bp_finish)
3876 {
3877 continue;
3878 }
3879
3880 /* Without a symbolic address, we have little hope of the
3881 pre-exec() address meaning the same thing in the post-exec()
3882 a.out. */
3883 if (breakpoint_event_location_empty_p (b))
3884 {
3885 delete_breakpoint (b);
3886 continue;
3887 }
3888 }
3889 }
3890
3891 int
3892 detach_breakpoints (ptid_t ptid)
3893 {
3894 struct bp_location *bl, **blp_tmp;
3895 int val = 0;
3896 struct cleanup *old_chain = save_inferior_ptid ();
3897 struct inferior *inf = current_inferior ();
3898
3899 if (ptid_get_pid (ptid) == ptid_get_pid (inferior_ptid))
3900 error (_("Cannot detach breakpoints of inferior_ptid"));
3901
3902 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
3903 inferior_ptid = ptid;
3904 ALL_BP_LOCATIONS (bl, blp_tmp)
3905 {
3906 if (bl->pspace != inf->pspace)
3907 continue;
3908
3909 /* This function must physically remove breakpoints locations
3910 from the specified ptid, without modifying the breakpoint
3911 package's state. Locations of type bp_loc_other are only
3912 maintained at GDB side. So, there is no need to remove
3913 these bp_loc_other locations. Moreover, removing these
3914 would modify the breakpoint package's state. */
3915 if (bl->loc_type == bp_loc_other)
3916 continue;
3917
3918 if (bl->inserted)
3919 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
3920 }
3921
3922 do_cleanups (old_chain);
3923 return val;
3924 }
3925
3926 /* Remove the breakpoint location BL from the current address space.
3927 Note that this is used to detach breakpoints from a child fork.
3928 When we get here, the child isn't in the inferior list, and neither
3929 do we have objects to represent its address space --- we should
3930 *not* look at bl->pspace->aspace here. */
3931
3932 static int
3933 remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
3934 {
3935 int val;
3936
3937 /* BL is never in moribund_locations by our callers. */
3938 gdb_assert (bl->owner != NULL);
3939
3940 /* The type of none suggests that owner is actually deleted.
3941 This should not ever happen. */
3942 gdb_assert (bl->owner->type != bp_none);
3943
3944 if (bl->loc_type == bp_loc_software_breakpoint
3945 || bl->loc_type == bp_loc_hardware_breakpoint)
3946 {
3947 /* "Normal" instruction breakpoint: either the standard
3948 trap-instruction bp (bp_breakpoint), or a
3949 bp_hardware_breakpoint. */
3950
3951 /* First check to see if we have to handle an overlay. */
3952 if (overlay_debugging == ovly_off
3953 || bl->section == NULL
3954 || !(section_is_overlay (bl->section)))
3955 {
3956 /* No overlay handling: just remove the breakpoint. */
3957
3958 /* If we're trying to uninsert a memory breakpoint that we
3959 know is set in a dynamic object that is marked
3960 shlib_disabled, then either the dynamic object was
3961 removed with "remove-symbol-file" or with
3962 "nosharedlibrary". In the former case, we don't know
3963 whether another dynamic object might have loaded over the
3964 breakpoint's address -- the user might well let us know
3965 about it next with add-symbol-file (the whole point of
3966 add-symbol-file is letting the user manually maintain a
3967 list of dynamically loaded objects). If we have the
3968 breakpoint's shadow memory, that is, this is a software
3969 breakpoint managed by GDB, check whether the breakpoint
3970 is still inserted in memory, to avoid overwriting wrong
3971 code with stale saved shadow contents. Note that HW
3972 breakpoints don't have shadow memory, as they're
3973 implemented using a mechanism that is not dependent on
3974 being able to modify the target's memory, and as such
3975 they should always be removed. */
3976 if (bl->shlib_disabled
3977 && bl->target_info.shadow_len != 0
3978 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3979 val = 0;
3980 else
3981 val = bl->owner->ops->remove_location (bl, reason);
3982 }
3983 else
3984 {
3985 /* This breakpoint is in an overlay section.
3986 Did we set a breakpoint at the LMA? */
3987 if (!overlay_events_enabled)
3988 {
3989 /* Yes -- overlay event support is not active, so we
3990 should have set a breakpoint at the LMA. Remove it.
3991 */
3992 /* Ignore any failures: if the LMA is in ROM, we will
3993 have already warned when we failed to insert it. */
3994 if (bl->loc_type == bp_loc_hardware_breakpoint)
3995 target_remove_hw_breakpoint (bl->gdbarch,
3996 &bl->overlay_target_info);
3997 else
3998 target_remove_breakpoint (bl->gdbarch,
3999 &bl->overlay_target_info,
4000 reason);
4001 }
4002 /* Did we set a breakpoint at the VMA?
4003 If so, we will have marked the breakpoint 'inserted'. */
4004 if (bl->inserted)
4005 {
4006 /* Yes -- remove it. Previously we did not bother to
4007 remove the breakpoint if the section had been
4008 unmapped, but let's not rely on that being safe. We
4009 don't know what the overlay manager might do. */
4010
4011 /* However, we should remove *software* breakpoints only
4012 if the section is still mapped, or else we overwrite
4013 wrong code with the saved shadow contents. */
4014 if (bl->loc_type == bp_loc_hardware_breakpoint
4015 || section_is_mapped (bl->section))
4016 val = bl->owner->ops->remove_location (bl, reason);
4017 else
4018 val = 0;
4019 }
4020 else
4021 {
4022 /* No -- not inserted, so no need to remove. No error. */
4023 val = 0;
4024 }
4025 }
4026
4027 /* In some cases, we might not be able to remove a breakpoint in
4028 a shared library that has already been removed, but we have
4029 not yet processed the shlib unload event. Similarly for an
4030 unloaded add-symbol-file object - the user might not yet have
4031 had the chance to remove-symbol-file it. shlib_disabled will
4032 be set if the library/object has already been removed, but
4033 the breakpoint hasn't been uninserted yet, e.g., after
4034 "nosharedlibrary" or "remove-symbol-file" with breakpoints
4035 always-inserted mode. */
4036 if (val
4037 && (bl->loc_type == bp_loc_software_breakpoint
4038 && (bl->shlib_disabled
4039 || solib_name_from_address (bl->pspace, bl->address)
4040 || shared_objfile_contains_address_p (bl->pspace,
4041 bl->address))))
4042 val = 0;
4043
4044 if (val)
4045 return val;
4046 bl->inserted = (reason == DETACH_BREAKPOINT);
4047 }
4048 else if (bl->loc_type == bp_loc_hardware_watchpoint)
4049 {
4050 gdb_assert (bl->owner->ops != NULL
4051 && bl->owner->ops->remove_location != NULL);
4052
4053 bl->inserted = (reason == DETACH_BREAKPOINT);
4054 bl->owner->ops->remove_location (bl, reason);
4055
4056 /* Failure to remove any of the hardware watchpoints comes here. */
4057 if (reason == REMOVE_BREAKPOINT && bl->inserted)
4058 warning (_("Could not remove hardware watchpoint %d."),
4059 bl->owner->number);
4060 }
4061 else if (bl->owner->type == bp_catchpoint
4062 && breakpoint_enabled (bl->owner)
4063 && !bl->duplicate)
4064 {
4065 gdb_assert (bl->owner->ops != NULL
4066 && bl->owner->ops->remove_location != NULL);
4067
4068 val = bl->owner->ops->remove_location (bl, reason);
4069 if (val)
4070 return val;
4071
4072 bl->inserted = (reason == DETACH_BREAKPOINT);
4073 }
4074
4075 return 0;
4076 }
4077
4078 static int
4079 remove_breakpoint (struct bp_location *bl)
4080 {
4081 int ret;
4082 struct cleanup *old_chain;
4083
4084 /* BL is never in moribund_locations by our callers. */
4085 gdb_assert (bl->owner != NULL);
4086
4087 /* The type of none suggests that owner is actually deleted.
4088 This should not ever happen. */
4089 gdb_assert (bl->owner->type != bp_none);
4090
4091 old_chain = save_current_space_and_thread ();
4092
4093 switch_to_program_space_and_thread (bl->pspace);
4094
4095 ret = remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
4096
4097 do_cleanups (old_chain);
4098 return ret;
4099 }
4100
4101 /* Clear the "inserted" flag in all breakpoints. */
4102
4103 void
4104 mark_breakpoints_out (void)
4105 {
4106 struct bp_location *bl, **blp_tmp;
4107
4108 ALL_BP_LOCATIONS (bl, blp_tmp)
4109 if (bl->pspace == current_program_space)
4110 bl->inserted = 0;
4111 }
4112
4113 /* Clear the "inserted" flag in all breakpoints and delete any
4114 breakpoints which should go away between runs of the program.
4115
4116 Plus other such housekeeping that has to be done for breakpoints
4117 between runs.
4118
4119 Note: this function gets called at the end of a run (by
4120 generic_mourn_inferior) and when a run begins (by
4121 init_wait_for_inferior). */
4122
4123
4124
4125 void
4126 breakpoint_init_inferior (enum inf_context context)
4127 {
4128 struct breakpoint *b, *b_tmp;
4129 struct bp_location *bl;
4130 int ix;
4131 struct program_space *pspace = current_program_space;
4132
4133 /* If breakpoint locations are shared across processes, then there's
4134 nothing to do. */
4135 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
4136 return;
4137
4138 mark_breakpoints_out ();
4139
4140 ALL_BREAKPOINTS_SAFE (b, b_tmp)
4141 {
4142 if (b->loc && b->loc->pspace != pspace)
4143 continue;
4144
4145 switch (b->type)
4146 {
4147 case bp_call_dummy:
4148 case bp_longjmp_call_dummy:
4149
4150 /* If the call dummy breakpoint is at the entry point it will
4151 cause problems when the inferior is rerun, so we better get
4152 rid of it. */
4153
4154 case bp_watchpoint_scope:
4155
4156 /* Also get rid of scope breakpoints. */
4157
4158 case bp_shlib_event:
4159
4160 /* Also remove solib event breakpoints. Their addresses may
4161 have changed since the last time we ran the program.
4162 Actually we may now be debugging against different target;
4163 and so the solib backend that installed this breakpoint may
4164 not be used in by the target. E.g.,
4165
4166 (gdb) file prog-linux
4167 (gdb) run # native linux target
4168 ...
4169 (gdb) kill
4170 (gdb) file prog-win.exe
4171 (gdb) tar rem :9999 # remote Windows gdbserver.
4172 */
4173
4174 case bp_step_resume:
4175
4176 /* Also remove step-resume breakpoints. */
4177
4178 case bp_single_step:
4179
4180 /* Also remove single-step breakpoints. */
4181
4182 delete_breakpoint (b);
4183 break;
4184
4185 case bp_watchpoint:
4186 case bp_hardware_watchpoint:
4187 case bp_read_watchpoint:
4188 case bp_access_watchpoint:
4189 {
4190 struct watchpoint *w = (struct watchpoint *) b;
4191
4192 /* Likewise for watchpoints on local expressions. */
4193 if (w->exp_valid_block != NULL)
4194 delete_breakpoint (b);
4195 else
4196 {
4197 /* Get rid of existing locations, which are no longer
4198 valid. New ones will be created in
4199 update_watchpoint, when the inferior is restarted.
4200 The next update_global_location_list call will
4201 garbage collect them. */
4202 b->loc = NULL;
4203
4204 if (context == inf_starting)
4205 {
4206 /* Reset val field to force reread of starting value in
4207 insert_breakpoints. */
4208 if (w->val)
4209 value_free (w->val);
4210 w->val = NULL;
4211 w->val_valid = 0;
4212 }
4213 }
4214 }
4215 break;
4216 default:
4217 break;
4218 }
4219 }
4220
4221 /* Get rid of the moribund locations. */
4222 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
4223 decref_bp_location (&bl);
4224 VEC_free (bp_location_p, moribund_locations);
4225 }
4226
4227 /* These functions concern about actual breakpoints inserted in the
4228 target --- to e.g. check if we need to do decr_pc adjustment or if
4229 we need to hop over the bkpt --- so we check for address space
4230 match, not program space. */
4231
4232 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4233 exists at PC. It returns ordinary_breakpoint_here if it's an
4234 ordinary breakpoint, or permanent_breakpoint_here if it's a
4235 permanent breakpoint.
4236 - When continuing from a location with an ordinary breakpoint, we
4237 actually single step once before calling insert_breakpoints.
4238 - When continuing from a location with a permanent breakpoint, we
4239 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4240 the target, to advance the PC past the breakpoint. */
4241
4242 enum breakpoint_here
4243 breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
4244 {
4245 struct bp_location *bl, **blp_tmp;
4246 int any_breakpoint_here = 0;
4247
4248 ALL_BP_LOCATIONS (bl, blp_tmp)
4249 {
4250 if (bl->loc_type != bp_loc_software_breakpoint
4251 && bl->loc_type != bp_loc_hardware_breakpoint)
4252 continue;
4253
4254 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
4255 if ((breakpoint_enabled (bl->owner)
4256 || bl->permanent)
4257 && breakpoint_location_address_match (bl, aspace, pc))
4258 {
4259 if (overlay_debugging
4260 && section_is_overlay (bl->section)
4261 && !section_is_mapped (bl->section))
4262 continue; /* unmapped overlay -- can't be a match */
4263 else if (bl->permanent)
4264 return permanent_breakpoint_here;
4265 else
4266 any_breakpoint_here = 1;
4267 }
4268 }
4269
4270 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
4271 }
4272
4273 /* See breakpoint.h. */
4274
4275 int
4276 breakpoint_in_range_p (struct address_space *aspace,
4277 CORE_ADDR addr, ULONGEST len)
4278 {
4279 struct bp_location *bl, **blp_tmp;
4280
4281 ALL_BP_LOCATIONS (bl, blp_tmp)
4282 {
4283 if (bl->loc_type != bp_loc_software_breakpoint
4284 && bl->loc_type != bp_loc_hardware_breakpoint)
4285 continue;
4286
4287 if ((breakpoint_enabled (bl->owner)
4288 || bl->permanent)
4289 && breakpoint_location_address_range_overlap (bl, aspace,
4290 addr, len))
4291 {
4292 if (overlay_debugging
4293 && section_is_overlay (bl->section)
4294 && !section_is_mapped (bl->section))
4295 {
4296 /* Unmapped overlay -- can't be a match. */
4297 continue;
4298 }
4299
4300 return 1;
4301 }
4302 }
4303
4304 return 0;
4305 }
4306
4307 /* Return true if there's a moribund breakpoint at PC. */
4308
4309 int
4310 moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
4311 {
4312 struct bp_location *loc;
4313 int ix;
4314
4315 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
4316 if (breakpoint_location_address_match (loc, aspace, pc))
4317 return 1;
4318
4319 return 0;
4320 }
4321
4322 /* Returns non-zero iff BL is inserted at PC, in address space
4323 ASPACE. */
4324
4325 static int
4326 bp_location_inserted_here_p (struct bp_location *bl,
4327 struct address_space *aspace, CORE_ADDR pc)
4328 {
4329 if (bl->inserted
4330 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4331 aspace, pc))
4332 {
4333 if (overlay_debugging
4334 && section_is_overlay (bl->section)
4335 && !section_is_mapped (bl->section))
4336 return 0; /* unmapped overlay -- can't be a match */
4337 else
4338 return 1;
4339 }
4340 return 0;
4341 }
4342
4343 /* Returns non-zero iff there's a breakpoint inserted at PC. */
4344
4345 int
4346 breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
4347 {
4348 struct bp_location **blp, **blp_tmp = NULL;
4349
4350 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4351 {
4352 struct bp_location *bl = *blp;
4353
4354 if (bl->loc_type != bp_loc_software_breakpoint
4355 && bl->loc_type != bp_loc_hardware_breakpoint)
4356 continue;
4357
4358 if (bp_location_inserted_here_p (bl, aspace, pc))
4359 return 1;
4360 }
4361 return 0;
4362 }
4363
4364 /* This function returns non-zero iff there is a software breakpoint
4365 inserted at PC. */
4366
4367 int
4368 software_breakpoint_inserted_here_p (struct address_space *aspace,
4369 CORE_ADDR pc)
4370 {
4371 struct bp_location **blp, **blp_tmp = NULL;
4372
4373 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4374 {
4375 struct bp_location *bl = *blp;
4376
4377 if (bl->loc_type != bp_loc_software_breakpoint)
4378 continue;
4379
4380 if (bp_location_inserted_here_p (bl, aspace, pc))
4381 return 1;
4382 }
4383
4384 return 0;
4385 }
4386
4387 /* See breakpoint.h. */
4388
4389 int
4390 hardware_breakpoint_inserted_here_p (struct address_space *aspace,
4391 CORE_ADDR pc)
4392 {
4393 struct bp_location **blp, **blp_tmp = NULL;
4394
4395 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4396 {
4397 struct bp_location *bl = *blp;
4398
4399 if (bl->loc_type != bp_loc_hardware_breakpoint)
4400 continue;
4401
4402 if (bp_location_inserted_here_p (bl, aspace, pc))
4403 return 1;
4404 }
4405
4406 return 0;
4407 }
4408
4409 int
4410 hardware_watchpoint_inserted_in_range (struct address_space *aspace,
4411 CORE_ADDR addr, ULONGEST len)
4412 {
4413 struct breakpoint *bpt;
4414
4415 ALL_BREAKPOINTS (bpt)
4416 {
4417 struct bp_location *loc;
4418
4419 if (bpt->type != bp_hardware_watchpoint
4420 && bpt->type != bp_access_watchpoint)
4421 continue;
4422
4423 if (!breakpoint_enabled (bpt))
4424 continue;
4425
4426 for (loc = bpt->loc; loc; loc = loc->next)
4427 if (loc->pspace->aspace == aspace && loc->inserted)
4428 {
4429 CORE_ADDR l, h;
4430
4431 /* Check for intersection. */
4432 l = std::max<CORE_ADDR> (loc->address, addr);
4433 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
4434 if (l < h)
4435 return 1;
4436 }
4437 }
4438 return 0;
4439 }
4440 \f
4441
4442 /* bpstat stuff. External routines' interfaces are documented
4443 in breakpoint.h. */
4444
4445 int
4446 is_catchpoint (struct breakpoint *ep)
4447 {
4448 return (ep->type == bp_catchpoint);
4449 }
4450
4451 /* Frees any storage that is part of a bpstat. Does not walk the
4452 'next' chain. */
4453
4454 static void
4455 bpstat_free (bpstat bs)
4456 {
4457 if (bs->old_val != NULL)
4458 value_free (bs->old_val);
4459 decref_counted_command_line (&bs->commands);
4460 decref_bp_location (&bs->bp_location_at);
4461 xfree (bs);
4462 }
4463
4464 /* Clear a bpstat so that it says we are not at any breakpoint.
4465 Also free any storage that is part of a bpstat. */
4466
4467 void
4468 bpstat_clear (bpstat *bsp)
4469 {
4470 bpstat p;
4471 bpstat q;
4472
4473 if (bsp == 0)
4474 return;
4475 p = *bsp;
4476 while (p != NULL)
4477 {
4478 q = p->next;
4479 bpstat_free (p);
4480 p = q;
4481 }
4482 *bsp = NULL;
4483 }
4484
4485 /* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4486 is part of the bpstat is copied as well. */
4487
4488 bpstat
4489 bpstat_copy (bpstat bs)
4490 {
4491 bpstat p = NULL;
4492 bpstat tmp;
4493 bpstat retval = NULL;
4494
4495 if (bs == NULL)
4496 return bs;
4497
4498 for (; bs != NULL; bs = bs->next)
4499 {
4500 tmp = (bpstat) xmalloc (sizeof (*tmp));
4501 memcpy (tmp, bs, sizeof (*tmp));
4502 incref_counted_command_line (tmp->commands);
4503 incref_bp_location (tmp->bp_location_at);
4504 if (bs->old_val != NULL)
4505 {
4506 tmp->old_val = value_copy (bs->old_val);
4507 release_value (tmp->old_val);
4508 }
4509
4510 if (p == NULL)
4511 /* This is the first thing in the chain. */
4512 retval = tmp;
4513 else
4514 p->next = tmp;
4515 p = tmp;
4516 }
4517 p->next = NULL;
4518 return retval;
4519 }
4520
4521 /* Find the bpstat associated with this breakpoint. */
4522
4523 bpstat
4524 bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
4525 {
4526 if (bsp == NULL)
4527 return NULL;
4528
4529 for (; bsp != NULL; bsp = bsp->next)
4530 {
4531 if (bsp->breakpoint_at == breakpoint)
4532 return bsp;
4533 }
4534 return NULL;
4535 }
4536
4537 /* See breakpoint.h. */
4538
4539 int
4540 bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
4541 {
4542 for (; bsp != NULL; bsp = bsp->next)
4543 {
4544 if (bsp->breakpoint_at == NULL)
4545 {
4546 /* A moribund location can never explain a signal other than
4547 GDB_SIGNAL_TRAP. */
4548 if (sig == GDB_SIGNAL_TRAP)
4549 return 1;
4550 }
4551 else
4552 {
4553 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4554 sig))
4555 return 1;
4556 }
4557 }
4558
4559 return 0;
4560 }
4561
4562 /* Put in *NUM the breakpoint number of the first breakpoint we are
4563 stopped at. *BSP upon return is a bpstat which points to the
4564 remaining breakpoints stopped at (but which is not guaranteed to be
4565 good for anything but further calls to bpstat_num).
4566
4567 Return 0 if passed a bpstat which does not indicate any breakpoints.
4568 Return -1 if stopped at a breakpoint that has been deleted since
4569 we set it.
4570 Return 1 otherwise. */
4571
4572 int
4573 bpstat_num (bpstat *bsp, int *num)
4574 {
4575 struct breakpoint *b;
4576
4577 if ((*bsp) == NULL)
4578 return 0; /* No more breakpoint values */
4579
4580 /* We assume we'll never have several bpstats that correspond to a
4581 single breakpoint -- otherwise, this function might return the
4582 same number more than once and this will look ugly. */
4583 b = (*bsp)->breakpoint_at;
4584 *bsp = (*bsp)->next;
4585 if (b == NULL)
4586 return -1; /* breakpoint that's been deleted since */
4587
4588 *num = b->number; /* We have its number */
4589 return 1;
4590 }
4591
4592 /* See breakpoint.h. */
4593
4594 void
4595 bpstat_clear_actions (void)
4596 {
4597 struct thread_info *tp;
4598 bpstat bs;
4599
4600 if (ptid_equal (inferior_ptid, null_ptid))
4601 return;
4602
4603 tp = find_thread_ptid (inferior_ptid);
4604 if (tp == NULL)
4605 return;
4606
4607 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
4608 {
4609 decref_counted_command_line (&bs->commands);
4610
4611 if (bs->old_val != NULL)
4612 {
4613 value_free (bs->old_val);
4614 bs->old_val = NULL;
4615 }
4616 }
4617 }
4618
4619 /* Called when a command is about to proceed the inferior. */
4620
4621 static void
4622 breakpoint_about_to_proceed (void)
4623 {
4624 if (!ptid_equal (inferior_ptid, null_ptid))
4625 {
4626 struct thread_info *tp = inferior_thread ();
4627
4628 /* Allow inferior function calls in breakpoint commands to not
4629 interrupt the command list. When the call finishes
4630 successfully, the inferior will be standing at the same
4631 breakpoint as if nothing happened. */
4632 if (tp->control.in_infcall)
4633 return;
4634 }
4635
4636 breakpoint_proceeded = 1;
4637 }
4638
4639 /* Stub for cleaning up our state if we error-out of a breakpoint
4640 command. */
4641 static void
4642 cleanup_executing_breakpoints (void *ignore)
4643 {
4644 executing_breakpoint_commands = 0;
4645 }
4646
4647 /* Return non-zero iff CMD as the first line of a command sequence is `silent'
4648 or its equivalent. */
4649
4650 static int
4651 command_line_is_silent (struct command_line *cmd)
4652 {
4653 return cmd && (strcmp ("silent", cmd->line) == 0);
4654 }
4655
4656 /* Execute all the commands associated with all the breakpoints at
4657 this location. Any of these commands could cause the process to
4658 proceed beyond this point, etc. We look out for such changes by
4659 checking the global "breakpoint_proceeded" after each command.
4660
4661 Returns true if a breakpoint command resumed the inferior. In that
4662 case, it is the caller's responsibility to recall it again with the
4663 bpstat of the current thread. */
4664
4665 static int
4666 bpstat_do_actions_1 (bpstat *bsp)
4667 {
4668 bpstat bs;
4669 struct cleanup *old_chain;
4670 int again = 0;
4671
4672 /* Avoid endless recursion if a `source' command is contained
4673 in bs->commands. */
4674 if (executing_breakpoint_commands)
4675 return 0;
4676
4677 executing_breakpoint_commands = 1;
4678 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
4679
4680 scoped_restore preventer = prevent_dont_repeat ();
4681
4682 /* This pointer will iterate over the list of bpstat's. */
4683 bs = *bsp;
4684
4685 breakpoint_proceeded = 0;
4686 for (; bs != NULL; bs = bs->next)
4687 {
4688 struct counted_command_line *ccmd;
4689 struct command_line *cmd;
4690 struct cleanup *this_cmd_tree_chain;
4691
4692 /* Take ownership of the BSP's command tree, if it has one.
4693
4694 The command tree could legitimately contain commands like
4695 'step' and 'next', which call clear_proceed_status, which
4696 frees stop_bpstat's command tree. To make sure this doesn't
4697 free the tree we're executing out from under us, we need to
4698 take ownership of the tree ourselves. Since a given bpstat's
4699 commands are only executed once, we don't need to copy it; we
4700 can clear the pointer in the bpstat, and make sure we free
4701 the tree when we're done. */
4702 ccmd = bs->commands;
4703 bs->commands = NULL;
4704 this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
4705 cmd = ccmd ? ccmd->commands : NULL;
4706 if (command_line_is_silent (cmd))
4707 {
4708 /* The action has been already done by bpstat_stop_status. */
4709 cmd = cmd->next;
4710 }
4711
4712 while (cmd != NULL)
4713 {
4714 execute_control_command (cmd);
4715
4716 if (breakpoint_proceeded)
4717 break;
4718 else
4719 cmd = cmd->next;
4720 }
4721
4722 /* We can free this command tree now. */
4723 do_cleanups (this_cmd_tree_chain);
4724
4725 if (breakpoint_proceeded)
4726 {
4727 if (current_ui->async)
4728 /* If we are in async mode, then the target might be still
4729 running, not stopped at any breakpoint, so nothing for
4730 us to do here -- just return to the event loop. */
4731 ;
4732 else
4733 /* In sync mode, when execute_control_command returns
4734 we're already standing on the next breakpoint.
4735 Breakpoint commands for that stop were not run, since
4736 execute_command does not run breakpoint commands --
4737 only command_line_handler does, but that one is not
4738 involved in execution of breakpoint commands. So, we
4739 can now execute breakpoint commands. It should be
4740 noted that making execute_command do bpstat actions is
4741 not an option -- in this case we'll have recursive
4742 invocation of bpstat for each breakpoint with a
4743 command, and can easily blow up GDB stack. Instead, we
4744 return true, which will trigger the caller to recall us
4745 with the new stop_bpstat. */
4746 again = 1;
4747 break;
4748 }
4749 }
4750 do_cleanups (old_chain);
4751 return again;
4752 }
4753
4754 void
4755 bpstat_do_actions (void)
4756 {
4757 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4758
4759 /* Do any commands attached to breakpoint we are stopped at. */
4760 while (!ptid_equal (inferior_ptid, null_ptid)
4761 && target_has_execution
4762 && !is_exited (inferior_ptid)
4763 && !is_executing (inferior_ptid))
4764 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4765 and only return when it is stopped at the next breakpoint, we
4766 keep doing breakpoint actions until it returns false to
4767 indicate the inferior was not resumed. */
4768 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
4769 break;
4770
4771 discard_cleanups (cleanup_if_error);
4772 }
4773
4774 /* Print out the (old or new) value associated with a watchpoint. */
4775
4776 static void
4777 watchpoint_value_print (struct value *val, struct ui_file *stream)
4778 {
4779 if (val == NULL)
4780 fprintf_unfiltered (stream, _("<unreadable>"));
4781 else
4782 {
4783 struct value_print_options opts;
4784 get_user_print_options (&opts);
4785 value_print (val, stream, &opts);
4786 }
4787 }
4788
4789 /* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4790 debugging multiple threads. */
4791
4792 void
4793 maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4794 {
4795 if (uiout->is_mi_like_p ())
4796 return;
4797
4798 uiout->text ("\n");
4799
4800 if (show_thread_that_caused_stop ())
4801 {
4802 const char *name;
4803 struct thread_info *thr = inferior_thread ();
4804
4805 uiout->text ("Thread ");
4806 uiout->field_fmt ("thread-id", "%s", print_thread_id (thr));
4807
4808 name = thr->name != NULL ? thr->name : target_thread_name (thr);
4809 if (name != NULL)
4810 {
4811 uiout->text (" \"");
4812 uiout->field_fmt ("name", "%s", name);
4813 uiout->text ("\"");
4814 }
4815
4816 uiout->text (" hit ");
4817 }
4818 }
4819
4820 /* Generic routine for printing messages indicating why we
4821 stopped. The behavior of this function depends on the value
4822 'print_it' in the bpstat structure. Under some circumstances we
4823 may decide not to print anything here and delegate the task to
4824 normal_stop(). */
4825
4826 static enum print_stop_action
4827 print_bp_stop_message (bpstat bs)
4828 {
4829 switch (bs->print_it)
4830 {
4831 case print_it_noop:
4832 /* Nothing should be printed for this bpstat entry. */
4833 return PRINT_UNKNOWN;
4834 break;
4835
4836 case print_it_done:
4837 /* We still want to print the frame, but we already printed the
4838 relevant messages. */
4839 return PRINT_SRC_AND_LOC;
4840 break;
4841
4842 case print_it_normal:
4843 {
4844 struct breakpoint *b = bs->breakpoint_at;
4845
4846 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4847 which has since been deleted. */
4848 if (b == NULL)
4849 return PRINT_UNKNOWN;
4850
4851 /* Normal case. Call the breakpoint's print_it method. */
4852 return b->ops->print_it (bs);
4853 }
4854 break;
4855
4856 default:
4857 internal_error (__FILE__, __LINE__,
4858 _("print_bp_stop_message: unrecognized enum value"));
4859 break;
4860 }
4861 }
4862
4863 /* A helper function that prints a shared library stopped event. */
4864
4865 static void
4866 print_solib_event (int is_catchpoint)
4867 {
4868 int any_deleted
4869 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4870 int any_added
4871 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4872
4873 if (!is_catchpoint)
4874 {
4875 if (any_added || any_deleted)
4876 current_uiout->text (_("Stopped due to shared library event:\n"));
4877 else
4878 current_uiout->text (_("Stopped due to shared library event (no "
4879 "libraries added or removed)\n"));
4880 }
4881
4882 if (current_uiout->is_mi_like_p ())
4883 current_uiout->field_string ("reason",
4884 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4885
4886 if (any_deleted)
4887 {
4888 char *name;
4889 int ix;
4890
4891 current_uiout->text (_(" Inferior unloaded "));
4892 ui_out_emit_list list_emitter (current_uiout, "removed");
4893 for (ix = 0;
4894 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4895 ix, name);
4896 ++ix)
4897 {
4898 if (ix > 0)
4899 current_uiout->text (" ");
4900 current_uiout->field_string ("library", name);
4901 current_uiout->text ("\n");
4902 }
4903 }
4904
4905 if (any_added)
4906 {
4907 struct so_list *iter;
4908 int ix;
4909
4910 current_uiout->text (_(" Inferior loaded "));
4911 ui_out_emit_list list_emitter (current_uiout, "added");
4912 for (ix = 0;
4913 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4914 ix, iter);
4915 ++ix)
4916 {
4917 if (ix > 0)
4918 current_uiout->text (" ");
4919 current_uiout->field_string ("library", iter->so_name);
4920 current_uiout->text ("\n");
4921 }
4922 }
4923 }
4924
4925 /* Print a message indicating what happened. This is called from
4926 normal_stop(). The input to this routine is the head of the bpstat
4927 list - a list of the eventpoints that caused this stop. KIND is
4928 the target_waitkind for the stopping event. This
4929 routine calls the generic print routine for printing a message
4930 about reasons for stopping. This will print (for example) the
4931 "Breakpoint n," part of the output. The return value of this
4932 routine is one of:
4933
4934 PRINT_UNKNOWN: Means we printed nothing.
4935 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4936 code to print the location. An example is
4937 "Breakpoint 1, " which should be followed by
4938 the location.
4939 PRINT_SRC_ONLY: Means we printed something, but there is no need
4940 to also print the location part of the message.
4941 An example is the catch/throw messages, which
4942 don't require a location appended to the end.
4943 PRINT_NOTHING: We have done some printing and we don't need any
4944 further info to be printed. */
4945
4946 enum print_stop_action
4947 bpstat_print (bpstat bs, int kind)
4948 {
4949 enum print_stop_action val;
4950
4951 /* Maybe another breakpoint in the chain caused us to stop.
4952 (Currently all watchpoints go on the bpstat whether hit or not.
4953 That probably could (should) be changed, provided care is taken
4954 with respect to bpstat_explains_signal). */
4955 for (; bs; bs = bs->next)
4956 {
4957 val = print_bp_stop_message (bs);
4958 if (val == PRINT_SRC_ONLY
4959 || val == PRINT_SRC_AND_LOC
4960 || val == PRINT_NOTHING)
4961 return val;
4962 }
4963
4964 /* If we had hit a shared library event breakpoint,
4965 print_bp_stop_message would print out this message. If we hit an
4966 OS-level shared library event, do the same thing. */
4967 if (kind == TARGET_WAITKIND_LOADED)
4968 {
4969 print_solib_event (0);
4970 return PRINT_NOTHING;
4971 }
4972
4973 /* We reached the end of the chain, or we got a null BS to start
4974 with and nothing was printed. */
4975 return PRINT_UNKNOWN;
4976 }
4977
4978 /* Evaluate the expression EXP and return 1 if value is zero.
4979 This returns the inverse of the condition because it is called
4980 from catch_errors which returns 0 if an exception happened, and if an
4981 exception happens we want execution to stop.
4982 The argument is a "struct expression *" that has been cast to a
4983 "void *" to make it pass through catch_errors. */
4984
4985 static int
4986 breakpoint_cond_eval (void *exp)
4987 {
4988 struct value *mark = value_mark ();
4989 int i = !value_true (evaluate_expression ((struct expression *) exp));
4990
4991 value_free_to_mark (mark);
4992 return i;
4993 }
4994
4995 /* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
4996
4997 static bpstat
4998 bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
4999 {
5000 bpstat bs;
5001
5002 bs = (bpstat) xmalloc (sizeof (*bs));
5003 bs->next = NULL;
5004 **bs_link_pointer = bs;
5005 *bs_link_pointer = &bs->next;
5006 bs->breakpoint_at = bl->owner;
5007 bs->bp_location_at = bl;
5008 incref_bp_location (bl);
5009 /* If the condition is false, etc., don't do the commands. */
5010 bs->commands = NULL;
5011 bs->old_val = NULL;
5012 bs->print_it = print_it_normal;
5013 return bs;
5014 }
5015 \f
5016 /* The target has stopped with waitstatus WS. Check if any hardware
5017 watchpoints have triggered, according to the target. */
5018
5019 int
5020 watchpoints_triggered (struct target_waitstatus *ws)
5021 {
5022 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
5023 CORE_ADDR addr;
5024 struct breakpoint *b;
5025
5026 if (!stopped_by_watchpoint)
5027 {
5028 /* We were not stopped by a watchpoint. Mark all watchpoints
5029 as not triggered. */
5030 ALL_BREAKPOINTS (b)
5031 if (is_hardware_watchpoint (b))
5032 {
5033 struct watchpoint *w = (struct watchpoint *) b;
5034
5035 w->watchpoint_triggered = watch_triggered_no;
5036 }
5037
5038 return 0;
5039 }
5040
5041 if (!target_stopped_data_address (&current_target, &addr))
5042 {
5043 /* We were stopped by a watchpoint, but we don't know where.
5044 Mark all watchpoints as unknown. */
5045 ALL_BREAKPOINTS (b)
5046 if (is_hardware_watchpoint (b))
5047 {
5048 struct watchpoint *w = (struct watchpoint *) b;
5049
5050 w->watchpoint_triggered = watch_triggered_unknown;
5051 }
5052
5053 return 1;
5054 }
5055
5056 /* The target could report the data address. Mark watchpoints
5057 affected by this data address as triggered, and all others as not
5058 triggered. */
5059
5060 ALL_BREAKPOINTS (b)
5061 if (is_hardware_watchpoint (b))
5062 {
5063 struct watchpoint *w = (struct watchpoint *) b;
5064 struct bp_location *loc;
5065
5066 w->watchpoint_triggered = watch_triggered_no;
5067 for (loc = b->loc; loc; loc = loc->next)
5068 {
5069 if (is_masked_watchpoint (b))
5070 {
5071 CORE_ADDR newaddr = addr & w->hw_wp_mask;
5072 CORE_ADDR start = loc->address & w->hw_wp_mask;
5073
5074 if (newaddr == start)
5075 {
5076 w->watchpoint_triggered = watch_triggered_yes;
5077 break;
5078 }
5079 }
5080 /* Exact match not required. Within range is sufficient. */
5081 else if (target_watchpoint_addr_within_range (&current_target,
5082 addr, loc->address,
5083 loc->length))
5084 {
5085 w->watchpoint_triggered = watch_triggered_yes;
5086 break;
5087 }
5088 }
5089 }
5090
5091 return 1;
5092 }
5093
5094 /* Possible return values for watchpoint_check (this can't be an enum
5095 because of check_errors). */
5096 /* The watchpoint has been deleted. */
5097 #define WP_DELETED 1
5098 /* The value has changed. */
5099 #define WP_VALUE_CHANGED 2
5100 /* The value has not changed. */
5101 #define WP_VALUE_NOT_CHANGED 3
5102 /* Ignore this watchpoint, no matter if the value changed or not. */
5103 #define WP_IGNORE 4
5104
5105 #define BP_TEMPFLAG 1
5106 #define BP_HARDWAREFLAG 2
5107
5108 /* Evaluate watchpoint condition expression and check if its value
5109 changed.
5110
5111 P should be a pointer to struct bpstat, but is defined as a void *
5112 in order for this function to be usable with catch_errors. */
5113
5114 static int
5115 watchpoint_check (void *p)
5116 {
5117 bpstat bs = (bpstat) p;
5118 struct watchpoint *b;
5119 struct frame_info *fr;
5120 int within_current_scope;
5121
5122 /* BS is built from an existing struct breakpoint. */
5123 gdb_assert (bs->breakpoint_at != NULL);
5124 b = (struct watchpoint *) bs->breakpoint_at;
5125
5126 /* If this is a local watchpoint, we only want to check if the
5127 watchpoint frame is in scope if the current thread is the thread
5128 that was used to create the watchpoint. */
5129 if (!watchpoint_in_thread_scope (b))
5130 return WP_IGNORE;
5131
5132 if (b->exp_valid_block == NULL)
5133 within_current_scope = 1;
5134 else
5135 {
5136 struct frame_info *frame = get_current_frame ();
5137 struct gdbarch *frame_arch = get_frame_arch (frame);
5138 CORE_ADDR frame_pc = get_frame_pc (frame);
5139
5140 /* stack_frame_destroyed_p() returns a non-zero value if we're
5141 still in the function but the stack frame has already been
5142 invalidated. Since we can't rely on the values of local
5143 variables after the stack has been destroyed, we are treating
5144 the watchpoint in that state as `not changed' without further
5145 checking. Don't mark watchpoints as changed if the current
5146 frame is in an epilogue - even if they are in some other
5147 frame, our view of the stack is likely to be wrong and
5148 frame_find_by_id could error out. */
5149 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
5150 return WP_IGNORE;
5151
5152 fr = frame_find_by_id (b->watchpoint_frame);
5153 within_current_scope = (fr != NULL);
5154
5155 /* If we've gotten confused in the unwinder, we might have
5156 returned a frame that can't describe this variable. */
5157 if (within_current_scope)
5158 {
5159 struct symbol *function;
5160
5161 function = get_frame_function (fr);
5162 if (function == NULL
5163 || !contained_in (b->exp_valid_block,
5164 SYMBOL_BLOCK_VALUE (function)))
5165 within_current_scope = 0;
5166 }
5167
5168 if (within_current_scope)
5169 /* If we end up stopping, the current frame will get selected
5170 in normal_stop. So this call to select_frame won't affect
5171 the user. */
5172 select_frame (fr);
5173 }
5174
5175 if (within_current_scope)
5176 {
5177 /* We use value_{,free_to_}mark because it could be a *long*
5178 time before we return to the command level and call
5179 free_all_values. We can't call free_all_values because we
5180 might be in the middle of evaluating a function call. */
5181
5182 int pc = 0;
5183 struct value *mark;
5184 struct value *new_val;
5185
5186 if (is_masked_watchpoint (&b->base))
5187 /* Since we don't know the exact trigger address (from
5188 stopped_data_address), just tell the user we've triggered
5189 a mask watchpoint. */
5190 return WP_VALUE_CHANGED;
5191
5192 mark = value_mark ();
5193 fetch_subexp_value (b->exp.get (), &pc, &new_val, NULL, NULL, 0);
5194
5195 if (b->val_bitsize != 0)
5196 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5197
5198 /* We use value_equal_contents instead of value_equal because
5199 the latter coerces an array to a pointer, thus comparing just
5200 the address of the array instead of its contents. This is
5201 not what we want. */
5202 if ((b->val != NULL) != (new_val != NULL)
5203 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
5204 {
5205 if (new_val != NULL)
5206 {
5207 release_value (new_val);
5208 value_free_to_mark (mark);
5209 }
5210 bs->old_val = b->val;
5211 b->val = new_val;
5212 b->val_valid = 1;
5213 return WP_VALUE_CHANGED;
5214 }
5215 else
5216 {
5217 /* Nothing changed. */
5218 value_free_to_mark (mark);
5219 return WP_VALUE_NOT_CHANGED;
5220 }
5221 }
5222 else
5223 {
5224 /* This seems like the only logical thing to do because
5225 if we temporarily ignored the watchpoint, then when
5226 we reenter the block in which it is valid it contains
5227 garbage (in the case of a function, it may have two
5228 garbage values, one before and one after the prologue).
5229 So we can't even detect the first assignment to it and
5230 watch after that (since the garbage may or may not equal
5231 the first value assigned). */
5232 /* We print all the stop information in
5233 breakpoint_ops->print_it, but in this case, by the time we
5234 call breakpoint_ops->print_it this bp will be deleted
5235 already. So we have no choice but print the information
5236 here. */
5237
5238 SWITCH_THRU_ALL_UIS ()
5239 {
5240 struct ui_out *uiout = current_uiout;
5241
5242 if (uiout->is_mi_like_p ())
5243 uiout->field_string
5244 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
5245 uiout->text ("\nWatchpoint ");
5246 uiout->field_int ("wpnum", b->base.number);
5247 uiout->text (" deleted because the program has left the block in\n"
5248 "which its expression is valid.\n");
5249 }
5250
5251 /* Make sure the watchpoint's commands aren't executed. */
5252 decref_counted_command_line (&b->base.commands);
5253 watchpoint_del_at_next_stop (b);
5254
5255 return WP_DELETED;
5256 }
5257 }
5258
5259 /* Return true if it looks like target has stopped due to hitting
5260 breakpoint location BL. This function does not check if we should
5261 stop, only if BL explains the stop. */
5262
5263 static int
5264 bpstat_check_location (const struct bp_location *bl,
5265 struct address_space *aspace, CORE_ADDR bp_addr,
5266 const struct target_waitstatus *ws)
5267 {
5268 struct breakpoint *b = bl->owner;
5269
5270 /* BL is from an existing breakpoint. */
5271 gdb_assert (b != NULL);
5272
5273 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
5274 }
5275
5276 /* Determine if the watched values have actually changed, and we
5277 should stop. If not, set BS->stop to 0. */
5278
5279 static void
5280 bpstat_check_watchpoint (bpstat bs)
5281 {
5282 const struct bp_location *bl;
5283 struct watchpoint *b;
5284
5285 /* BS is built for existing struct breakpoint. */
5286 bl = bs->bp_location_at;
5287 gdb_assert (bl != NULL);
5288 b = (struct watchpoint *) bs->breakpoint_at;
5289 gdb_assert (b != NULL);
5290
5291 {
5292 int must_check_value = 0;
5293
5294 if (b->base.type == bp_watchpoint)
5295 /* For a software watchpoint, we must always check the
5296 watched value. */
5297 must_check_value = 1;
5298 else if (b->watchpoint_triggered == watch_triggered_yes)
5299 /* We have a hardware watchpoint (read, write, or access)
5300 and the target earlier reported an address watched by
5301 this watchpoint. */
5302 must_check_value = 1;
5303 else if (b->watchpoint_triggered == watch_triggered_unknown
5304 && b->base.type == bp_hardware_watchpoint)
5305 /* We were stopped by a hardware watchpoint, but the target could
5306 not report the data address. We must check the watchpoint's
5307 value. Access and read watchpoints are out of luck; without
5308 a data address, we can't figure it out. */
5309 must_check_value = 1;
5310
5311 if (must_check_value)
5312 {
5313 char *message
5314 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
5315 b->base.number);
5316 struct cleanup *cleanups = make_cleanup (xfree, message);
5317 int e = catch_errors (watchpoint_check, bs, message,
5318 RETURN_MASK_ALL);
5319 do_cleanups (cleanups);
5320 switch (e)
5321 {
5322 case WP_DELETED:
5323 /* We've already printed what needs to be printed. */
5324 bs->print_it = print_it_done;
5325 /* Stop. */
5326 break;
5327 case WP_IGNORE:
5328 bs->print_it = print_it_noop;
5329 bs->stop = 0;
5330 break;
5331 case WP_VALUE_CHANGED:
5332 if (b->base.type == bp_read_watchpoint)
5333 {
5334 /* There are two cases to consider here:
5335
5336 1. We're watching the triggered memory for reads.
5337 In that case, trust the target, and always report
5338 the watchpoint hit to the user. Even though
5339 reads don't cause value changes, the value may
5340 have changed since the last time it was read, and
5341 since we're not trapping writes, we will not see
5342 those, and as such we should ignore our notion of
5343 old value.
5344
5345 2. We're watching the triggered memory for both
5346 reads and writes. There are two ways this may
5347 happen:
5348
5349 2.1. This is a target that can't break on data
5350 reads only, but can break on accesses (reads or
5351 writes), such as e.g., x86. We detect this case
5352 at the time we try to insert read watchpoints.
5353
5354 2.2. Otherwise, the target supports read
5355 watchpoints, but, the user set an access or write
5356 watchpoint watching the same memory as this read
5357 watchpoint.
5358
5359 If we're watching memory writes as well as reads,
5360 ignore watchpoint hits when we find that the
5361 value hasn't changed, as reads don't cause
5362 changes. This still gives false positives when
5363 the program writes the same value to memory as
5364 what there was already in memory (we will confuse
5365 it for a read), but it's much better than
5366 nothing. */
5367
5368 int other_write_watchpoint = 0;
5369
5370 if (bl->watchpoint_type == hw_read)
5371 {
5372 struct breakpoint *other_b;
5373
5374 ALL_BREAKPOINTS (other_b)
5375 if (other_b->type == bp_hardware_watchpoint
5376 || other_b->type == bp_access_watchpoint)
5377 {
5378 struct watchpoint *other_w =
5379 (struct watchpoint *) other_b;
5380
5381 if (other_w->watchpoint_triggered
5382 == watch_triggered_yes)
5383 {
5384 other_write_watchpoint = 1;
5385 break;
5386 }
5387 }
5388 }
5389
5390 if (other_write_watchpoint
5391 || bl->watchpoint_type == hw_access)
5392 {
5393 /* We're watching the same memory for writes,
5394 and the value changed since the last time we
5395 updated it, so this trap must be for a write.
5396 Ignore it. */
5397 bs->print_it = print_it_noop;
5398 bs->stop = 0;
5399 }
5400 }
5401 break;
5402 case WP_VALUE_NOT_CHANGED:
5403 if (b->base.type == bp_hardware_watchpoint
5404 || b->base.type == bp_watchpoint)
5405 {
5406 /* Don't stop: write watchpoints shouldn't fire if
5407 the value hasn't changed. */
5408 bs->print_it = print_it_noop;
5409 bs->stop = 0;
5410 }
5411 /* Stop. */
5412 break;
5413 default:
5414 /* Can't happen. */
5415 case 0:
5416 /* Error from catch_errors. */
5417 {
5418 SWITCH_THRU_ALL_UIS ()
5419 {
5420 printf_filtered (_("Watchpoint %d deleted.\n"),
5421 b->base.number);
5422 }
5423 watchpoint_del_at_next_stop (b);
5424 /* We've already printed what needs to be printed. */
5425 bs->print_it = print_it_done;
5426 }
5427 break;
5428 }
5429 }
5430 else /* must_check_value == 0 */
5431 {
5432 /* This is a case where some watchpoint(s) triggered, but
5433 not at the address of this watchpoint, or else no
5434 watchpoint triggered after all. So don't print
5435 anything for this watchpoint. */
5436 bs->print_it = print_it_noop;
5437 bs->stop = 0;
5438 }
5439 }
5440 }
5441
5442 /* For breakpoints that are currently marked as telling gdb to stop,
5443 check conditions (condition proper, frame, thread and ignore count)
5444 of breakpoint referred to by BS. If we should not stop for this
5445 breakpoint, set BS->stop to 0. */
5446
5447 static void
5448 bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5449 {
5450 const struct bp_location *bl;
5451 struct breakpoint *b;
5452 int value_is_zero = 0;
5453 struct expression *cond;
5454
5455 gdb_assert (bs->stop);
5456
5457 /* BS is built for existing struct breakpoint. */
5458 bl = bs->bp_location_at;
5459 gdb_assert (bl != NULL);
5460 b = bs->breakpoint_at;
5461 gdb_assert (b != NULL);
5462
5463 /* Even if the target evaluated the condition on its end and notified GDB, we
5464 need to do so again since GDB does not know if we stopped due to a
5465 breakpoint or a single step breakpoint. */
5466
5467 if (frame_id_p (b->frame_id)
5468 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
5469 {
5470 bs->stop = 0;
5471 return;
5472 }
5473
5474 /* If this is a thread/task-specific breakpoint, don't waste cpu
5475 evaluating the condition if this isn't the specified
5476 thread/task. */
5477 if ((b->thread != -1 && b->thread != ptid_to_global_thread_id (ptid))
5478 || (b->task != 0 && b->task != ada_get_task_number (ptid)))
5479
5480 {
5481 bs->stop = 0;
5482 return;
5483 }
5484
5485 /* Evaluate extension language breakpoints that have a "stop" method
5486 implemented. */
5487 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
5488
5489 if (is_watchpoint (b))
5490 {
5491 struct watchpoint *w = (struct watchpoint *) b;
5492
5493 cond = w->cond_exp.get ();
5494 }
5495 else
5496 cond = bl->cond.get ();
5497
5498 if (cond && b->disposition != disp_del_at_next_stop)
5499 {
5500 int within_current_scope = 1;
5501 struct watchpoint * w;
5502
5503 /* We use value_mark and value_free_to_mark because it could
5504 be a long time before we return to the command level and
5505 call free_all_values. We can't call free_all_values
5506 because we might be in the middle of evaluating a
5507 function call. */
5508 struct value *mark = value_mark ();
5509
5510 if (is_watchpoint (b))
5511 w = (struct watchpoint *) b;
5512 else
5513 w = NULL;
5514
5515 /* Need to select the frame, with all that implies so that
5516 the conditions will have the right context. Because we
5517 use the frame, we will not see an inlined function's
5518 variables when we arrive at a breakpoint at the start
5519 of the inlined function; the current frame will be the
5520 call site. */
5521 if (w == NULL || w->cond_exp_valid_block == NULL)
5522 select_frame (get_current_frame ());
5523 else
5524 {
5525 struct frame_info *frame;
5526
5527 /* For local watchpoint expressions, which particular
5528 instance of a local is being watched matters, so we
5529 keep track of the frame to evaluate the expression
5530 in. To evaluate the condition however, it doesn't
5531 really matter which instantiation of the function
5532 where the condition makes sense triggers the
5533 watchpoint. This allows an expression like "watch
5534 global if q > 10" set in `func', catch writes to
5535 global on all threads that call `func', or catch
5536 writes on all recursive calls of `func' by a single
5537 thread. We simply always evaluate the condition in
5538 the innermost frame that's executing where it makes
5539 sense to evaluate the condition. It seems
5540 intuitive. */
5541 frame = block_innermost_frame (w->cond_exp_valid_block);
5542 if (frame != NULL)
5543 select_frame (frame);
5544 else
5545 within_current_scope = 0;
5546 }
5547 if (within_current_scope)
5548 value_is_zero
5549 = catch_errors (breakpoint_cond_eval, cond,
5550 "Error in testing breakpoint condition:\n",
5551 RETURN_MASK_ALL);
5552 else
5553 {
5554 warning (_("Watchpoint condition cannot be tested "
5555 "in the current scope"));
5556 /* If we failed to set the right context for this
5557 watchpoint, unconditionally report it. */
5558 value_is_zero = 0;
5559 }
5560 /* FIXME-someday, should give breakpoint #. */
5561 value_free_to_mark (mark);
5562 }
5563
5564 if (cond && value_is_zero)
5565 {
5566 bs->stop = 0;
5567 }
5568 else if (b->ignore_count > 0)
5569 {
5570 b->ignore_count--;
5571 bs->stop = 0;
5572 /* Increase the hit count even though we don't stop. */
5573 ++(b->hit_count);
5574 observer_notify_breakpoint_modified (b);
5575 }
5576 }
5577
5578 /* Returns true if we need to track moribund locations of LOC's type
5579 on the current target. */
5580
5581 static int
5582 need_moribund_for_location_type (struct bp_location *loc)
5583 {
5584 return ((loc->loc_type == bp_loc_software_breakpoint
5585 && !target_supports_stopped_by_sw_breakpoint ())
5586 || (loc->loc_type == bp_loc_hardware_breakpoint
5587 && !target_supports_stopped_by_hw_breakpoint ()));
5588 }
5589
5590
5591 /* Get a bpstat associated with having just stopped at address
5592 BP_ADDR in thread PTID.
5593
5594 Determine whether we stopped at a breakpoint, etc, or whether we
5595 don't understand this stop. Result is a chain of bpstat's such
5596 that:
5597
5598 if we don't understand the stop, the result is a null pointer.
5599
5600 if we understand why we stopped, the result is not null.
5601
5602 Each element of the chain refers to a particular breakpoint or
5603 watchpoint at which we have stopped. (We may have stopped for
5604 several reasons concurrently.)
5605
5606 Each element of the chain has valid next, breakpoint_at,
5607 commands, FIXME??? fields. */
5608
5609 bpstat
5610 bpstat_stop_status (struct address_space *aspace,
5611 CORE_ADDR bp_addr, ptid_t ptid,
5612 const struct target_waitstatus *ws)
5613 {
5614 struct breakpoint *b = NULL;
5615 struct bp_location *bl;
5616 struct bp_location *loc;
5617 /* First item of allocated bpstat's. */
5618 bpstat bs_head = NULL, *bs_link = &bs_head;
5619 /* Pointer to the last thing in the chain currently. */
5620 bpstat bs;
5621 int ix;
5622 int need_remove_insert;
5623 int removed_any;
5624
5625 /* First, build the bpstat chain with locations that explain a
5626 target stop, while being careful to not set the target running,
5627 as that may invalidate locations (in particular watchpoint
5628 locations are recreated). Resuming will happen here with
5629 breakpoint conditions or watchpoint expressions that include
5630 inferior function calls. */
5631
5632 ALL_BREAKPOINTS (b)
5633 {
5634 if (!breakpoint_enabled (b))
5635 continue;
5636
5637 for (bl = b->loc; bl != NULL; bl = bl->next)
5638 {
5639 /* For hardware watchpoints, we look only at the first
5640 location. The watchpoint_check function will work on the
5641 entire expression, not the individual locations. For
5642 read watchpoints, the watchpoints_triggered function has
5643 checked all locations already. */
5644 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5645 break;
5646
5647 if (!bl->enabled || bl->shlib_disabled)
5648 continue;
5649
5650 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
5651 continue;
5652
5653 /* Come here if it's a watchpoint, or if the break address
5654 matches. */
5655
5656 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
5657 explain stop. */
5658
5659 /* Assume we stop. Should we find a watchpoint that is not
5660 actually triggered, or if the condition of the breakpoint
5661 evaluates as false, we'll reset 'stop' to 0. */
5662 bs->stop = 1;
5663 bs->print = 1;
5664
5665 /* If this is a scope breakpoint, mark the associated
5666 watchpoint as triggered so that we will handle the
5667 out-of-scope event. We'll get to the watchpoint next
5668 iteration. */
5669 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
5670 {
5671 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5672
5673 w->watchpoint_triggered = watch_triggered_yes;
5674 }
5675 }
5676 }
5677
5678 /* Check if a moribund breakpoint explains the stop. */
5679 if (!target_supports_stopped_by_sw_breakpoint ()
5680 || !target_supports_stopped_by_hw_breakpoint ())
5681 {
5682 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
5683 {
5684 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5685 && need_moribund_for_location_type (loc))
5686 {
5687 bs = bpstat_alloc (loc, &bs_link);
5688 /* For hits of moribund locations, we should just proceed. */
5689 bs->stop = 0;
5690 bs->print = 0;
5691 bs->print_it = print_it_noop;
5692 }
5693 }
5694 }
5695
5696 /* A bit of special processing for shlib breakpoints. We need to
5697 process solib loading here, so that the lists of loaded and
5698 unloaded libraries are correct before we handle "catch load" and
5699 "catch unload". */
5700 for (bs = bs_head; bs != NULL; bs = bs->next)
5701 {
5702 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
5703 {
5704 handle_solib_event ();
5705 break;
5706 }
5707 }
5708
5709 /* Now go through the locations that caused the target to stop, and
5710 check whether we're interested in reporting this stop to higher
5711 layers, or whether we should resume the target transparently. */
5712
5713 removed_any = 0;
5714
5715 for (bs = bs_head; bs != NULL; bs = bs->next)
5716 {
5717 if (!bs->stop)
5718 continue;
5719
5720 b = bs->breakpoint_at;
5721 b->ops->check_status (bs);
5722 if (bs->stop)
5723 {
5724 bpstat_check_breakpoint_conditions (bs, ptid);
5725
5726 if (bs->stop)
5727 {
5728 ++(b->hit_count);
5729 observer_notify_breakpoint_modified (b);
5730
5731 /* We will stop here. */
5732 if (b->disposition == disp_disable)
5733 {
5734 --(b->enable_count);
5735 if (b->enable_count <= 0)
5736 b->enable_state = bp_disabled;
5737 removed_any = 1;
5738 }
5739 if (b->silent)
5740 bs->print = 0;
5741 bs->commands = b->commands;
5742 incref_counted_command_line (bs->commands);
5743 if (command_line_is_silent (bs->commands
5744 ? bs->commands->commands : NULL))
5745 bs->print = 0;
5746
5747 b->ops->after_condition_true (bs);
5748 }
5749
5750 }
5751
5752 /* Print nothing for this entry if we don't stop or don't
5753 print. */
5754 if (!bs->stop || !bs->print)
5755 bs->print_it = print_it_noop;
5756 }
5757
5758 /* If we aren't stopping, the value of some hardware watchpoint may
5759 not have changed, but the intermediate memory locations we are
5760 watching may have. Don't bother if we're stopping; this will get
5761 done later. */
5762 need_remove_insert = 0;
5763 if (! bpstat_causes_stop (bs_head))
5764 for (bs = bs_head; bs != NULL; bs = bs->next)
5765 if (!bs->stop
5766 && bs->breakpoint_at
5767 && is_hardware_watchpoint (bs->breakpoint_at))
5768 {
5769 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5770
5771 update_watchpoint (w, 0 /* don't reparse. */);
5772 need_remove_insert = 1;
5773 }
5774
5775 if (need_remove_insert)
5776 update_global_location_list (UGLL_MAY_INSERT);
5777 else if (removed_any)
5778 update_global_location_list (UGLL_DONT_INSERT);
5779
5780 return bs_head;
5781 }
5782
5783 static void
5784 handle_jit_event (void)
5785 {
5786 struct frame_info *frame;
5787 struct gdbarch *gdbarch;
5788
5789 if (debug_infrun)
5790 fprintf_unfiltered (gdb_stdlog, "handling bp_jit_event\n");
5791
5792 /* Switch terminal for any messages produced by
5793 breakpoint_re_set. */
5794 target_terminal_ours_for_output ();
5795
5796 frame = get_current_frame ();
5797 gdbarch = get_frame_arch (frame);
5798
5799 jit_event_handler (gdbarch);
5800
5801 target_terminal_inferior ();
5802 }
5803
5804 /* Prepare WHAT final decision for infrun. */
5805
5806 /* Decide what infrun needs to do with this bpstat. */
5807
5808 struct bpstat_what
5809 bpstat_what (bpstat bs_head)
5810 {
5811 struct bpstat_what retval;
5812 bpstat bs;
5813
5814 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
5815 retval.call_dummy = STOP_NONE;
5816 retval.is_longjmp = 0;
5817
5818 for (bs = bs_head; bs != NULL; bs = bs->next)
5819 {
5820 /* Extract this BS's action. After processing each BS, we check
5821 if its action overrides all we've seem so far. */
5822 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5823 enum bptype bptype;
5824
5825 if (bs->breakpoint_at == NULL)
5826 {
5827 /* I suspect this can happen if it was a momentary
5828 breakpoint which has since been deleted. */
5829 bptype = bp_none;
5830 }
5831 else
5832 bptype = bs->breakpoint_at->type;
5833
5834 switch (bptype)
5835 {
5836 case bp_none:
5837 break;
5838 case bp_breakpoint:
5839 case bp_hardware_breakpoint:
5840 case bp_single_step:
5841 case bp_until:
5842 case bp_finish:
5843 case bp_shlib_event:
5844 if (bs->stop)
5845 {
5846 if (bs->print)
5847 this_action = BPSTAT_WHAT_STOP_NOISY;
5848 else
5849 this_action = BPSTAT_WHAT_STOP_SILENT;
5850 }
5851 else
5852 this_action = BPSTAT_WHAT_SINGLE;
5853 break;
5854 case bp_watchpoint:
5855 case bp_hardware_watchpoint:
5856 case bp_read_watchpoint:
5857 case bp_access_watchpoint:
5858 if (bs->stop)
5859 {
5860 if (bs->print)
5861 this_action = BPSTAT_WHAT_STOP_NOISY;
5862 else
5863 this_action = BPSTAT_WHAT_STOP_SILENT;
5864 }
5865 else
5866 {
5867 /* There was a watchpoint, but we're not stopping.
5868 This requires no further action. */
5869 }
5870 break;
5871 case bp_longjmp:
5872 case bp_longjmp_call_dummy:
5873 case bp_exception:
5874 if (bs->stop)
5875 {
5876 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5877 retval.is_longjmp = bptype != bp_exception;
5878 }
5879 else
5880 this_action = BPSTAT_WHAT_SINGLE;
5881 break;
5882 case bp_longjmp_resume:
5883 case bp_exception_resume:
5884 if (bs->stop)
5885 {
5886 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5887 retval.is_longjmp = bptype == bp_longjmp_resume;
5888 }
5889 else
5890 this_action = BPSTAT_WHAT_SINGLE;
5891 break;
5892 case bp_step_resume:
5893 if (bs->stop)
5894 this_action = BPSTAT_WHAT_STEP_RESUME;
5895 else
5896 {
5897 /* It is for the wrong frame. */
5898 this_action = BPSTAT_WHAT_SINGLE;
5899 }
5900 break;
5901 case bp_hp_step_resume:
5902 if (bs->stop)
5903 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5904 else
5905 {
5906 /* It is for the wrong frame. */
5907 this_action = BPSTAT_WHAT_SINGLE;
5908 }
5909 break;
5910 case bp_watchpoint_scope:
5911 case bp_thread_event:
5912 case bp_overlay_event:
5913 case bp_longjmp_master:
5914 case bp_std_terminate_master:
5915 case bp_exception_master:
5916 this_action = BPSTAT_WHAT_SINGLE;
5917 break;
5918 case bp_catchpoint:
5919 if (bs->stop)
5920 {
5921 if (bs->print)
5922 this_action = BPSTAT_WHAT_STOP_NOISY;
5923 else
5924 this_action = BPSTAT_WHAT_STOP_SILENT;
5925 }
5926 else
5927 {
5928 /* There was a catchpoint, but we're not stopping.
5929 This requires no further action. */
5930 }
5931 break;
5932 case bp_jit_event:
5933 this_action = BPSTAT_WHAT_SINGLE;
5934 break;
5935 case bp_call_dummy:
5936 /* Make sure the action is stop (silent or noisy),
5937 so infrun.c pops the dummy frame. */
5938 retval.call_dummy = STOP_STACK_DUMMY;
5939 this_action = BPSTAT_WHAT_STOP_SILENT;
5940 break;
5941 case bp_std_terminate:
5942 /* Make sure the action is stop (silent or noisy),
5943 so infrun.c pops the dummy frame. */
5944 retval.call_dummy = STOP_STD_TERMINATE;
5945 this_action = BPSTAT_WHAT_STOP_SILENT;
5946 break;
5947 case bp_tracepoint:
5948 case bp_fast_tracepoint:
5949 case bp_static_tracepoint:
5950 /* Tracepoint hits should not be reported back to GDB, and
5951 if one got through somehow, it should have been filtered
5952 out already. */
5953 internal_error (__FILE__, __LINE__,
5954 _("bpstat_what: tracepoint encountered"));
5955 break;
5956 case bp_gnu_ifunc_resolver:
5957 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5958 this_action = BPSTAT_WHAT_SINGLE;
5959 break;
5960 case bp_gnu_ifunc_resolver_return:
5961 /* The breakpoint will be removed, execution will restart from the
5962 PC of the former breakpoint. */
5963 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5964 break;
5965
5966 case bp_dprintf:
5967 if (bs->stop)
5968 this_action = BPSTAT_WHAT_STOP_SILENT;
5969 else
5970 this_action = BPSTAT_WHAT_SINGLE;
5971 break;
5972
5973 default:
5974 internal_error (__FILE__, __LINE__,
5975 _("bpstat_what: unhandled bptype %d"), (int) bptype);
5976 }
5977
5978 retval.main_action = std::max (retval.main_action, this_action);
5979 }
5980
5981 return retval;
5982 }
5983
5984 void
5985 bpstat_run_callbacks (bpstat bs_head)
5986 {
5987 bpstat bs;
5988
5989 for (bs = bs_head; bs != NULL; bs = bs->next)
5990 {
5991 struct breakpoint *b = bs->breakpoint_at;
5992
5993 if (b == NULL)
5994 continue;
5995 switch (b->type)
5996 {
5997 case bp_jit_event:
5998 handle_jit_event ();
5999 break;
6000 case bp_gnu_ifunc_resolver:
6001 gnu_ifunc_resolver_stop (b);
6002 break;
6003 case bp_gnu_ifunc_resolver_return:
6004 gnu_ifunc_resolver_return_stop (b);
6005 break;
6006 }
6007 }
6008 }
6009
6010 /* Nonzero if we should step constantly (e.g. watchpoints on machines
6011 without hardware support). This isn't related to a specific bpstat,
6012 just to things like whether watchpoints are set. */
6013
6014 int
6015 bpstat_should_step (void)
6016 {
6017 struct breakpoint *b;
6018
6019 ALL_BREAKPOINTS (b)
6020 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
6021 return 1;
6022 return 0;
6023 }
6024
6025 int
6026 bpstat_causes_stop (bpstat bs)
6027 {
6028 for (; bs != NULL; bs = bs->next)
6029 if (bs->stop)
6030 return 1;
6031
6032 return 0;
6033 }
6034
6035 \f
6036
6037 /* Compute a string of spaces suitable to indent the next line
6038 so it starts at the position corresponding to the table column
6039 named COL_NAME in the currently active table of UIOUT. */
6040
6041 static char *
6042 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
6043 {
6044 static char wrap_indent[80];
6045 int i, total_width, width, align;
6046 const char *text;
6047
6048 total_width = 0;
6049 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
6050 {
6051 if (strcmp (text, col_name) == 0)
6052 {
6053 gdb_assert (total_width < sizeof wrap_indent);
6054 memset (wrap_indent, ' ', total_width);
6055 wrap_indent[total_width] = 0;
6056
6057 return wrap_indent;
6058 }
6059
6060 total_width += width + 1;
6061 }
6062
6063 return NULL;
6064 }
6065
6066 /* Determine if the locations of this breakpoint will have their conditions
6067 evaluated by the target, host or a mix of both. Returns the following:
6068
6069 "host": Host evals condition.
6070 "host or target": Host or Target evals condition.
6071 "target": Target evals condition.
6072 */
6073
6074 static const char *
6075 bp_condition_evaluator (struct breakpoint *b)
6076 {
6077 struct bp_location *bl;
6078 char host_evals = 0;
6079 char target_evals = 0;
6080
6081 if (!b)
6082 return NULL;
6083
6084 if (!is_breakpoint (b))
6085 return NULL;
6086
6087 if (gdb_evaluates_breakpoint_condition_p ()
6088 || !target_supports_evaluation_of_breakpoint_conditions ())
6089 return condition_evaluation_host;
6090
6091 for (bl = b->loc; bl; bl = bl->next)
6092 {
6093 if (bl->cond_bytecode)
6094 target_evals++;
6095 else
6096 host_evals++;
6097 }
6098
6099 if (host_evals && target_evals)
6100 return condition_evaluation_both;
6101 else if (target_evals)
6102 return condition_evaluation_target;
6103 else
6104 return condition_evaluation_host;
6105 }
6106
6107 /* Determine the breakpoint location's condition evaluator. This is
6108 similar to bp_condition_evaluator, but for locations. */
6109
6110 static const char *
6111 bp_location_condition_evaluator (struct bp_location *bl)
6112 {
6113 if (bl && !is_breakpoint (bl->owner))
6114 return NULL;
6115
6116 if (gdb_evaluates_breakpoint_condition_p ()
6117 || !target_supports_evaluation_of_breakpoint_conditions ())
6118 return condition_evaluation_host;
6119
6120 if (bl && bl->cond_bytecode)
6121 return condition_evaluation_target;
6122 else
6123 return condition_evaluation_host;
6124 }
6125
6126 /* Print the LOC location out of the list of B->LOC locations. */
6127
6128 static void
6129 print_breakpoint_location (struct breakpoint *b,
6130 struct bp_location *loc)
6131 {
6132 struct ui_out *uiout = current_uiout;
6133 struct cleanup *old_chain = save_current_program_space ();
6134
6135 if (loc != NULL && loc->shlib_disabled)
6136 loc = NULL;
6137
6138 if (loc != NULL)
6139 set_current_program_space (loc->pspace);
6140
6141 if (b->display_canonical)
6142 uiout->field_string ("what", event_location_to_string (b->location.get ()));
6143 else if (loc && loc->symtab)
6144 {
6145 struct symbol *sym
6146 = find_pc_sect_function (loc->address, loc->section);
6147 if (sym)
6148 {
6149 uiout->text ("in ");
6150 uiout->field_string ("func", SYMBOL_PRINT_NAME (sym));
6151 uiout->text (" ");
6152 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
6153 uiout->text ("at ");
6154 }
6155 uiout->field_string ("file",
6156 symtab_to_filename_for_display (loc->symtab));
6157 uiout->text (":");
6158
6159 if (uiout->is_mi_like_p ())
6160 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
6161
6162 uiout->field_int ("line", loc->line_number);
6163 }
6164 else if (loc)
6165 {
6166 string_file stb;
6167
6168 print_address_symbolic (loc->gdbarch, loc->address, &stb,
6169 demangle, "");
6170 uiout->field_stream ("at", stb);
6171 }
6172 else
6173 {
6174 uiout->field_string ("pending",
6175 event_location_to_string (b->location.get ()));
6176 /* If extra_string is available, it could be holding a condition
6177 or dprintf arguments. In either case, make sure it is printed,
6178 too, but only for non-MI streams. */
6179 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
6180 {
6181 if (b->type == bp_dprintf)
6182 uiout->text (",");
6183 else
6184 uiout->text (" ");
6185 uiout->text (b->extra_string);
6186 }
6187 }
6188
6189 if (loc && is_breakpoint (b)
6190 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6191 && bp_condition_evaluator (b) == condition_evaluation_both)
6192 {
6193 uiout->text (" (");
6194 uiout->field_string ("evaluated-by",
6195 bp_location_condition_evaluator (loc));
6196 uiout->text (")");
6197 }
6198
6199 do_cleanups (old_chain);
6200 }
6201
6202 static const char *
6203 bptype_string (enum bptype type)
6204 {
6205 struct ep_type_description
6206 {
6207 enum bptype type;
6208 const char *description;
6209 };
6210 static struct ep_type_description bptypes[] =
6211 {
6212 {bp_none, "?deleted?"},
6213 {bp_breakpoint, "breakpoint"},
6214 {bp_hardware_breakpoint, "hw breakpoint"},
6215 {bp_single_step, "sw single-step"},
6216 {bp_until, "until"},
6217 {bp_finish, "finish"},
6218 {bp_watchpoint, "watchpoint"},
6219 {bp_hardware_watchpoint, "hw watchpoint"},
6220 {bp_read_watchpoint, "read watchpoint"},
6221 {bp_access_watchpoint, "acc watchpoint"},
6222 {bp_longjmp, "longjmp"},
6223 {bp_longjmp_resume, "longjmp resume"},
6224 {bp_longjmp_call_dummy, "longjmp for call dummy"},
6225 {bp_exception, "exception"},
6226 {bp_exception_resume, "exception resume"},
6227 {bp_step_resume, "step resume"},
6228 {bp_hp_step_resume, "high-priority step resume"},
6229 {bp_watchpoint_scope, "watchpoint scope"},
6230 {bp_call_dummy, "call dummy"},
6231 {bp_std_terminate, "std::terminate"},
6232 {bp_shlib_event, "shlib events"},
6233 {bp_thread_event, "thread events"},
6234 {bp_overlay_event, "overlay events"},
6235 {bp_longjmp_master, "longjmp master"},
6236 {bp_std_terminate_master, "std::terminate master"},
6237 {bp_exception_master, "exception master"},
6238 {bp_catchpoint, "catchpoint"},
6239 {bp_tracepoint, "tracepoint"},
6240 {bp_fast_tracepoint, "fast tracepoint"},
6241 {bp_static_tracepoint, "static tracepoint"},
6242 {bp_dprintf, "dprintf"},
6243 {bp_jit_event, "jit events"},
6244 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6245 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
6246 };
6247
6248 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6249 || ((int) type != bptypes[(int) type].type))
6250 internal_error (__FILE__, __LINE__,
6251 _("bptypes table does not describe type #%d."),
6252 (int) type);
6253
6254 return bptypes[(int) type].description;
6255 }
6256
6257 /* For MI, output a field named 'thread-groups' with a list as the value.
6258 For CLI, prefix the list with the string 'inf'. */
6259
6260 static void
6261 output_thread_groups (struct ui_out *uiout,
6262 const char *field_name,
6263 VEC(int) *inf_num,
6264 int mi_only)
6265 {
6266 int is_mi = uiout->is_mi_like_p ();
6267 int inf;
6268 int i;
6269
6270 /* For backward compatibility, don't display inferiors in CLI unless
6271 there are several. Always display them for MI. */
6272 if (!is_mi && mi_only)
6273 return;
6274
6275 ui_out_emit_list list_emitter (uiout, field_name);
6276
6277 for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
6278 {
6279 if (is_mi)
6280 {
6281 char mi_group[10];
6282
6283 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
6284 uiout->field_string (NULL, mi_group);
6285 }
6286 else
6287 {
6288 if (i == 0)
6289 uiout->text (" inf ");
6290 else
6291 uiout->text (", ");
6292
6293 uiout->text (plongest (inf));
6294 }
6295 }
6296 }
6297
6298 /* Print B to gdb_stdout. */
6299
6300 static void
6301 print_one_breakpoint_location (struct breakpoint *b,
6302 struct bp_location *loc,
6303 int loc_number,
6304 struct bp_location **last_loc,
6305 int allflag)
6306 {
6307 struct command_line *l;
6308 static char bpenables[] = "nynny";
6309
6310 struct ui_out *uiout = current_uiout;
6311 int header_of_multiple = 0;
6312 int part_of_multiple = (loc != NULL);
6313 struct value_print_options opts;
6314
6315 get_user_print_options (&opts);
6316
6317 gdb_assert (!loc || loc_number != 0);
6318 /* See comment in print_one_breakpoint concerning treatment of
6319 breakpoints with single disabled location. */
6320 if (loc == NULL
6321 && (b->loc != NULL
6322 && (b->loc->next != NULL || !b->loc->enabled)))
6323 header_of_multiple = 1;
6324 if (loc == NULL)
6325 loc = b->loc;
6326
6327 annotate_record ();
6328
6329 /* 1 */
6330 annotate_field (0);
6331 if (part_of_multiple)
6332 {
6333 char *formatted;
6334 formatted = xstrprintf ("%d.%d", b->number, loc_number);
6335 uiout->field_string ("number", formatted);
6336 xfree (formatted);
6337 }
6338 else
6339 {
6340 uiout->field_int ("number", b->number);
6341 }
6342
6343 /* 2 */
6344 annotate_field (1);
6345 if (part_of_multiple)
6346 uiout->field_skip ("type");
6347 else
6348 uiout->field_string ("type", bptype_string (b->type));
6349
6350 /* 3 */
6351 annotate_field (2);
6352 if (part_of_multiple)
6353 uiout->field_skip ("disp");
6354 else
6355 uiout->field_string ("disp", bpdisp_text (b->disposition));
6356
6357
6358 /* 4 */
6359 annotate_field (3);
6360 if (part_of_multiple)
6361 uiout->field_string ("enabled", loc->enabled ? "y" : "n");
6362 else
6363 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
6364 uiout->spaces (2);
6365
6366
6367 /* 5 and 6 */
6368 if (b->ops != NULL && b->ops->print_one != NULL)
6369 {
6370 /* Although the print_one can possibly print all locations,
6371 calling it here is not likely to get any nice result. So,
6372 make sure there's just one location. */
6373 gdb_assert (b->loc == NULL || b->loc->next == NULL);
6374 b->ops->print_one (b, last_loc);
6375 }
6376 else
6377 switch (b->type)
6378 {
6379 case bp_none:
6380 internal_error (__FILE__, __LINE__,
6381 _("print_one_breakpoint: bp_none encountered\n"));
6382 break;
6383
6384 case bp_watchpoint:
6385 case bp_hardware_watchpoint:
6386 case bp_read_watchpoint:
6387 case bp_access_watchpoint:
6388 {
6389 struct watchpoint *w = (struct watchpoint *) b;
6390
6391 /* Field 4, the address, is omitted (which makes the columns
6392 not line up too nicely with the headers, but the effect
6393 is relatively readable). */
6394 if (opts.addressprint)
6395 uiout->field_skip ("addr");
6396 annotate_field (5);
6397 uiout->field_string ("what", w->exp_string);
6398 }
6399 break;
6400
6401 case bp_breakpoint:
6402 case bp_hardware_breakpoint:
6403 case bp_single_step:
6404 case bp_until:
6405 case bp_finish:
6406 case bp_longjmp:
6407 case bp_longjmp_resume:
6408 case bp_longjmp_call_dummy:
6409 case bp_exception:
6410 case bp_exception_resume:
6411 case bp_step_resume:
6412 case bp_hp_step_resume:
6413 case bp_watchpoint_scope:
6414 case bp_call_dummy:
6415 case bp_std_terminate:
6416 case bp_shlib_event:
6417 case bp_thread_event:
6418 case bp_overlay_event:
6419 case bp_longjmp_master:
6420 case bp_std_terminate_master:
6421 case bp_exception_master:
6422 case bp_tracepoint:
6423 case bp_fast_tracepoint:
6424 case bp_static_tracepoint:
6425 case bp_dprintf:
6426 case bp_jit_event:
6427 case bp_gnu_ifunc_resolver:
6428 case bp_gnu_ifunc_resolver_return:
6429 if (opts.addressprint)
6430 {
6431 annotate_field (4);
6432 if (header_of_multiple)
6433 uiout->field_string ("addr", "<MULTIPLE>");
6434 else if (b->loc == NULL || loc->shlib_disabled)
6435 uiout->field_string ("addr", "<PENDING>");
6436 else
6437 uiout->field_core_addr ("addr",
6438 loc->gdbarch, loc->address);
6439 }
6440 annotate_field (5);
6441 if (!header_of_multiple)
6442 print_breakpoint_location (b, loc);
6443 if (b->loc)
6444 *last_loc = b->loc;
6445 break;
6446 }
6447
6448
6449 if (loc != NULL && !header_of_multiple)
6450 {
6451 struct inferior *inf;
6452 VEC(int) *inf_num = NULL;
6453 int mi_only = 1;
6454
6455 ALL_INFERIORS (inf)
6456 {
6457 if (inf->pspace == loc->pspace)
6458 VEC_safe_push (int, inf_num, inf->num);
6459 }
6460
6461 /* For backward compatibility, don't display inferiors in CLI unless
6462 there are several. Always display for MI. */
6463 if (allflag
6464 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6465 && (number_of_program_spaces () > 1
6466 || number_of_inferiors () > 1)
6467 /* LOC is for existing B, it cannot be in
6468 moribund_locations and thus having NULL OWNER. */
6469 && loc->owner->type != bp_catchpoint))
6470 mi_only = 0;
6471 output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6472 VEC_free (int, inf_num);
6473 }
6474
6475 if (!part_of_multiple)
6476 {
6477 if (b->thread != -1)
6478 {
6479 /* FIXME: This seems to be redundant and lost here; see the
6480 "stop only in" line a little further down. */
6481 uiout->text (" thread ");
6482 uiout->field_int ("thread", b->thread);
6483 }
6484 else if (b->task != 0)
6485 {
6486 uiout->text (" task ");
6487 uiout->field_int ("task", b->task);
6488 }
6489 }
6490
6491 uiout->text ("\n");
6492
6493 if (!part_of_multiple)
6494 b->ops->print_one_detail (b, uiout);
6495
6496 if (part_of_multiple && frame_id_p (b->frame_id))
6497 {
6498 annotate_field (6);
6499 uiout->text ("\tstop only in stack frame at ");
6500 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
6501 the frame ID. */
6502 uiout->field_core_addr ("frame",
6503 b->gdbarch, b->frame_id.stack_addr);
6504 uiout->text ("\n");
6505 }
6506
6507 if (!part_of_multiple && b->cond_string)
6508 {
6509 annotate_field (7);
6510 if (is_tracepoint (b))
6511 uiout->text ("\ttrace only if ");
6512 else
6513 uiout->text ("\tstop only if ");
6514 uiout->field_string ("cond", b->cond_string);
6515
6516 /* Print whether the target is doing the breakpoint's condition
6517 evaluation. If GDB is doing the evaluation, don't print anything. */
6518 if (is_breakpoint (b)
6519 && breakpoint_condition_evaluation_mode ()
6520 == condition_evaluation_target)
6521 {
6522 uiout->text (" (");
6523 uiout->field_string ("evaluated-by",
6524 bp_condition_evaluator (b));
6525 uiout->text (" evals)");
6526 }
6527 uiout->text ("\n");
6528 }
6529
6530 if (!part_of_multiple && b->thread != -1)
6531 {
6532 /* FIXME should make an annotation for this. */
6533 uiout->text ("\tstop only in thread ");
6534 if (uiout->is_mi_like_p ())
6535 uiout->field_int ("thread", b->thread);
6536 else
6537 {
6538 struct thread_info *thr = find_thread_global_id (b->thread);
6539
6540 uiout->field_string ("thread", print_thread_id (thr));
6541 }
6542 uiout->text ("\n");
6543 }
6544
6545 if (!part_of_multiple)
6546 {
6547 if (b->hit_count)
6548 {
6549 /* FIXME should make an annotation for this. */
6550 if (is_catchpoint (b))
6551 uiout->text ("\tcatchpoint");
6552 else if (is_tracepoint (b))
6553 uiout->text ("\ttracepoint");
6554 else
6555 uiout->text ("\tbreakpoint");
6556 uiout->text (" already hit ");
6557 uiout->field_int ("times", b->hit_count);
6558 if (b->hit_count == 1)
6559 uiout->text (" time\n");
6560 else
6561 uiout->text (" times\n");
6562 }
6563 else
6564 {
6565 /* Output the count also if it is zero, but only if this is mi. */
6566 if (uiout->is_mi_like_p ())
6567 uiout->field_int ("times", b->hit_count);
6568 }
6569 }
6570
6571 if (!part_of_multiple && b->ignore_count)
6572 {
6573 annotate_field (8);
6574 uiout->text ("\tignore next ");
6575 uiout->field_int ("ignore", b->ignore_count);
6576 uiout->text (" hits\n");
6577 }
6578
6579 /* Note that an enable count of 1 corresponds to "enable once"
6580 behavior, which is reported by the combination of enablement and
6581 disposition, so we don't need to mention it here. */
6582 if (!part_of_multiple && b->enable_count > 1)
6583 {
6584 annotate_field (8);
6585 uiout->text ("\tdisable after ");
6586 /* Tweak the wording to clarify that ignore and enable counts
6587 are distinct, and have additive effect. */
6588 if (b->ignore_count)
6589 uiout->text ("additional ");
6590 else
6591 uiout->text ("next ");
6592 uiout->field_int ("enable", b->enable_count);
6593 uiout->text (" hits\n");
6594 }
6595
6596 if (!part_of_multiple && is_tracepoint (b))
6597 {
6598 struct tracepoint *tp = (struct tracepoint *) b;
6599
6600 if (tp->traceframe_usage)
6601 {
6602 uiout->text ("\ttrace buffer usage ");
6603 uiout->field_int ("traceframe-usage", tp->traceframe_usage);
6604 uiout->text (" bytes\n");
6605 }
6606 }
6607
6608 l = b->commands ? b->commands->commands : NULL;
6609 if (!part_of_multiple && l)
6610 {
6611 annotate_field (9);
6612 ui_out_emit_tuple tuple_emitter (uiout, "script");
6613 print_command_lines (uiout, l, 4);
6614 }
6615
6616 if (is_tracepoint (b))
6617 {
6618 struct tracepoint *t = (struct tracepoint *) b;
6619
6620 if (!part_of_multiple && t->pass_count)
6621 {
6622 annotate_field (10);
6623 uiout->text ("\tpass count ");
6624 uiout->field_int ("pass", t->pass_count);
6625 uiout->text (" \n");
6626 }
6627
6628 /* Don't display it when tracepoint or tracepoint location is
6629 pending. */
6630 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6631 {
6632 annotate_field (11);
6633
6634 if (uiout->is_mi_like_p ())
6635 uiout->field_string ("installed",
6636 loc->inserted ? "y" : "n");
6637 else
6638 {
6639 if (loc->inserted)
6640 uiout->text ("\t");
6641 else
6642 uiout->text ("\tnot ");
6643 uiout->text ("installed on target\n");
6644 }
6645 }
6646 }
6647
6648 if (uiout->is_mi_like_p () && !part_of_multiple)
6649 {
6650 if (is_watchpoint (b))
6651 {
6652 struct watchpoint *w = (struct watchpoint *) b;
6653
6654 uiout->field_string ("original-location", w->exp_string);
6655 }
6656 else if (b->location != NULL
6657 && event_location_to_string (b->location.get ()) != NULL)
6658 uiout->field_string ("original-location",
6659 event_location_to_string (b->location.get ()));
6660 }
6661 }
6662
6663 static void
6664 print_one_breakpoint (struct breakpoint *b,
6665 struct bp_location **last_loc,
6666 int allflag)
6667 {
6668 struct ui_out *uiout = current_uiout;
6669
6670 {
6671 ui_out_emit_tuple tuple_emitter (uiout, "bkpt");
6672
6673 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
6674 }
6675
6676 /* If this breakpoint has custom print function,
6677 it's already printed. Otherwise, print individual
6678 locations, if any. */
6679 if (b->ops == NULL || b->ops->print_one == NULL)
6680 {
6681 /* If breakpoint has a single location that is disabled, we
6682 print it as if it had several locations, since otherwise it's
6683 hard to represent "breakpoint enabled, location disabled"
6684 situation.
6685
6686 Note that while hardware watchpoints have several locations
6687 internally, that's not a property exposed to user. */
6688 if (b->loc
6689 && !is_hardware_watchpoint (b)
6690 && (b->loc->next || !b->loc->enabled))
6691 {
6692 struct bp_location *loc;
6693 int n = 1;
6694
6695 for (loc = b->loc; loc; loc = loc->next, ++n)
6696 {
6697 ui_out_emit_tuple tuple_emitter (uiout, NULL);
6698 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
6699 }
6700 }
6701 }
6702 }
6703
6704 static int
6705 breakpoint_address_bits (struct breakpoint *b)
6706 {
6707 int print_address_bits = 0;
6708 struct bp_location *loc;
6709
6710 /* Software watchpoints that aren't watching memory don't have an
6711 address to print. */
6712 if (is_no_memory_software_watchpoint (b))
6713 return 0;
6714
6715 for (loc = b->loc; loc; loc = loc->next)
6716 {
6717 int addr_bit;
6718
6719 addr_bit = gdbarch_addr_bit (loc->gdbarch);
6720 if (addr_bit > print_address_bits)
6721 print_address_bits = addr_bit;
6722 }
6723
6724 return print_address_bits;
6725 }
6726
6727 struct captured_breakpoint_query_args
6728 {
6729 int bnum;
6730 };
6731
6732 static int
6733 do_captured_breakpoint_query (struct ui_out *uiout, void *data)
6734 {
6735 struct captured_breakpoint_query_args *args
6736 = (struct captured_breakpoint_query_args *) data;
6737 struct breakpoint *b;
6738 struct bp_location *dummy_loc = NULL;
6739
6740 ALL_BREAKPOINTS (b)
6741 {
6742 if (args->bnum == b->number)
6743 {
6744 print_one_breakpoint (b, &dummy_loc, 0);
6745 return GDB_RC_OK;
6746 }
6747 }
6748 return GDB_RC_NONE;
6749 }
6750
6751 enum gdb_rc
6752 gdb_breakpoint_query (struct ui_out *uiout, int bnum,
6753 char **error_message)
6754 {
6755 struct captured_breakpoint_query_args args;
6756
6757 args.bnum = bnum;
6758 /* For the moment we don't trust print_one_breakpoint() to not throw
6759 an error. */
6760 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
6761 error_message, RETURN_MASK_ALL) < 0)
6762 return GDB_RC_FAIL;
6763 else
6764 return GDB_RC_OK;
6765 }
6766
6767 /* Return true if this breakpoint was set by the user, false if it is
6768 internal or momentary. */
6769
6770 int
6771 user_breakpoint_p (struct breakpoint *b)
6772 {
6773 return b->number > 0;
6774 }
6775
6776 /* See breakpoint.h. */
6777
6778 int
6779 pending_breakpoint_p (struct breakpoint *b)
6780 {
6781 return b->loc == NULL;
6782 }
6783
6784 /* Print information on user settable breakpoint (watchpoint, etc)
6785 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6786 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6787 FILTER is non-NULL, call it on each breakpoint and only include the
6788 ones for which it returns non-zero. Return the total number of
6789 breakpoints listed. */
6790
6791 static int
6792 breakpoint_1 (char *args, int allflag,
6793 int (*filter) (const struct breakpoint *))
6794 {
6795 struct breakpoint *b;
6796 struct bp_location *last_loc = NULL;
6797 int nr_printable_breakpoints;
6798 struct cleanup *bkpttbl_chain;
6799 struct value_print_options opts;
6800 int print_address_bits = 0;
6801 int print_type_col_width = 14;
6802 struct ui_out *uiout = current_uiout;
6803
6804 get_user_print_options (&opts);
6805
6806 /* Compute the number of rows in the table, as well as the size
6807 required for address fields. */
6808 nr_printable_breakpoints = 0;
6809 ALL_BREAKPOINTS (b)
6810 {
6811 /* If we have a filter, only list the breakpoints it accepts. */
6812 if (filter && !filter (b))
6813 continue;
6814
6815 /* If we have an "args" string, it is a list of breakpoints to
6816 accept. Skip the others. */
6817 if (args != NULL && *args != '\0')
6818 {
6819 if (allflag && parse_and_eval_long (args) != b->number)
6820 continue;
6821 if (!allflag && !number_is_in_list (args, b->number))
6822 continue;
6823 }
6824
6825 if (allflag || user_breakpoint_p (b))
6826 {
6827 int addr_bit, type_len;
6828
6829 addr_bit = breakpoint_address_bits (b);
6830 if (addr_bit > print_address_bits)
6831 print_address_bits = addr_bit;
6832
6833 type_len = strlen (bptype_string (b->type));
6834 if (type_len > print_type_col_width)
6835 print_type_col_width = type_len;
6836
6837 nr_printable_breakpoints++;
6838 }
6839 }
6840
6841 if (opts.addressprint)
6842 bkpttbl_chain
6843 = make_cleanup_ui_out_table_begin_end (uiout, 6,
6844 nr_printable_breakpoints,
6845 "BreakpointTable");
6846 else
6847 bkpttbl_chain
6848 = make_cleanup_ui_out_table_begin_end (uiout, 5,
6849 nr_printable_breakpoints,
6850 "BreakpointTable");
6851
6852 if (nr_printable_breakpoints > 0)
6853 annotate_breakpoints_headers ();
6854 if (nr_printable_breakpoints > 0)
6855 annotate_field (0);
6856 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6857 if (nr_printable_breakpoints > 0)
6858 annotate_field (1);
6859 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6860 if (nr_printable_breakpoints > 0)
6861 annotate_field (2);
6862 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6863 if (nr_printable_breakpoints > 0)
6864 annotate_field (3);
6865 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6866 if (opts.addressprint)
6867 {
6868 if (nr_printable_breakpoints > 0)
6869 annotate_field (4);
6870 if (print_address_bits <= 32)
6871 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6872 else
6873 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6874 }
6875 if (nr_printable_breakpoints > 0)
6876 annotate_field (5);
6877 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6878 uiout->table_body ();
6879 if (nr_printable_breakpoints > 0)
6880 annotate_breakpoints_table ();
6881
6882 ALL_BREAKPOINTS (b)
6883 {
6884 QUIT;
6885 /* If we have a filter, only list the breakpoints it accepts. */
6886 if (filter && !filter (b))
6887 continue;
6888
6889 /* If we have an "args" string, it is a list of breakpoints to
6890 accept. Skip the others. */
6891
6892 if (args != NULL && *args != '\0')
6893 {
6894 if (allflag) /* maintenance info breakpoint */
6895 {
6896 if (parse_and_eval_long (args) != b->number)
6897 continue;
6898 }
6899 else /* all others */
6900 {
6901 if (!number_is_in_list (args, b->number))
6902 continue;
6903 }
6904 }
6905 /* We only print out user settable breakpoints unless the
6906 allflag is set. */
6907 if (allflag || user_breakpoint_p (b))
6908 print_one_breakpoint (b, &last_loc, allflag);
6909 }
6910
6911 do_cleanups (bkpttbl_chain);
6912
6913 if (nr_printable_breakpoints == 0)
6914 {
6915 /* If there's a filter, let the caller decide how to report
6916 empty list. */
6917 if (!filter)
6918 {
6919 if (args == NULL || *args == '\0')
6920 uiout->message ("No breakpoints or watchpoints.\n");
6921 else
6922 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
6923 args);
6924 }
6925 }
6926 else
6927 {
6928 if (last_loc && !server_command)
6929 set_next_address (last_loc->gdbarch, last_loc->address);
6930 }
6931
6932 /* FIXME? Should this be moved up so that it is only called when
6933 there have been breakpoints? */
6934 annotate_breakpoints_table_end ();
6935
6936 return nr_printable_breakpoints;
6937 }
6938
6939 /* Display the value of default-collect in a way that is generally
6940 compatible with the breakpoint list. */
6941
6942 static void
6943 default_collect_info (void)
6944 {
6945 struct ui_out *uiout = current_uiout;
6946
6947 /* If it has no value (which is frequently the case), say nothing; a
6948 message like "No default-collect." gets in user's face when it's
6949 not wanted. */
6950 if (!*default_collect)
6951 return;
6952
6953 /* The following phrase lines up nicely with per-tracepoint collect
6954 actions. */
6955 uiout->text ("default collect ");
6956 uiout->field_string ("default-collect", default_collect);
6957 uiout->text (" \n");
6958 }
6959
6960 static void
6961 breakpoints_info (char *args, int from_tty)
6962 {
6963 breakpoint_1 (args, 0, NULL);
6964
6965 default_collect_info ();
6966 }
6967
6968 static void
6969 watchpoints_info (char *args, int from_tty)
6970 {
6971 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
6972 struct ui_out *uiout = current_uiout;
6973
6974 if (num_printed == 0)
6975 {
6976 if (args == NULL || *args == '\0')
6977 uiout->message ("No watchpoints.\n");
6978 else
6979 uiout->message ("No watchpoint matching '%s'.\n", args);
6980 }
6981 }
6982
6983 static void
6984 maintenance_info_breakpoints (char *args, int from_tty)
6985 {
6986 breakpoint_1 (args, 1, NULL);
6987
6988 default_collect_info ();
6989 }
6990
6991 static int
6992 breakpoint_has_pc (struct breakpoint *b,
6993 struct program_space *pspace,
6994 CORE_ADDR pc, struct obj_section *section)
6995 {
6996 struct bp_location *bl = b->loc;
6997
6998 for (; bl; bl = bl->next)
6999 {
7000 if (bl->pspace == pspace
7001 && bl->address == pc
7002 && (!overlay_debugging || bl->section == section))
7003 return 1;
7004 }
7005 return 0;
7006 }
7007
7008 /* Print a message describing any user-breakpoints set at PC. This
7009 concerns with logical breakpoints, so we match program spaces, not
7010 address spaces. */
7011
7012 static void
7013 describe_other_breakpoints (struct gdbarch *gdbarch,
7014 struct program_space *pspace, CORE_ADDR pc,
7015 struct obj_section *section, int thread)
7016 {
7017 int others = 0;
7018 struct breakpoint *b;
7019
7020 ALL_BREAKPOINTS (b)
7021 others += (user_breakpoint_p (b)
7022 && breakpoint_has_pc (b, pspace, pc, section));
7023 if (others > 0)
7024 {
7025 if (others == 1)
7026 printf_filtered (_("Note: breakpoint "));
7027 else /* if (others == ???) */
7028 printf_filtered (_("Note: breakpoints "));
7029 ALL_BREAKPOINTS (b)
7030 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
7031 {
7032 others--;
7033 printf_filtered ("%d", b->number);
7034 if (b->thread == -1 && thread != -1)
7035 printf_filtered (" (all threads)");
7036 else if (b->thread != -1)
7037 printf_filtered (" (thread %d)", b->thread);
7038 printf_filtered ("%s%s ",
7039 ((b->enable_state == bp_disabled
7040 || b->enable_state == bp_call_disabled)
7041 ? " (disabled)"
7042 : ""),
7043 (others > 1) ? ","
7044 : ((others == 1) ? " and" : ""));
7045 }
7046 printf_filtered (_("also set at pc "));
7047 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
7048 printf_filtered (".\n");
7049 }
7050 }
7051 \f
7052
7053 /* Return true iff it is meaningful to use the address member of
7054 BPT locations. For some breakpoint types, the locations' address members
7055 are irrelevant and it makes no sense to attempt to compare them to other
7056 addresses (or use them for any other purpose either).
7057
7058 More specifically, each of the following breakpoint types will
7059 always have a zero valued location address and we don't want to mark
7060 breakpoints of any of these types to be a duplicate of an actual
7061 breakpoint location at address zero:
7062
7063 bp_watchpoint
7064 bp_catchpoint
7065
7066 */
7067
7068 static int
7069 breakpoint_address_is_meaningful (struct breakpoint *bpt)
7070 {
7071 enum bptype type = bpt->type;
7072
7073 return (type != bp_watchpoint && type != bp_catchpoint);
7074 }
7075
7076 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
7077 true if LOC1 and LOC2 represent the same watchpoint location. */
7078
7079 static int
7080 watchpoint_locations_match (struct bp_location *loc1,
7081 struct bp_location *loc2)
7082 {
7083 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
7084 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
7085
7086 /* Both of them must exist. */
7087 gdb_assert (w1 != NULL);
7088 gdb_assert (w2 != NULL);
7089
7090 /* If the target can evaluate the condition expression in hardware,
7091 then we we need to insert both watchpoints even if they are at
7092 the same place. Otherwise the watchpoint will only trigger when
7093 the condition of whichever watchpoint was inserted evaluates to
7094 true, not giving a chance for GDB to check the condition of the
7095 other watchpoint. */
7096 if ((w1->cond_exp
7097 && target_can_accel_watchpoint_condition (loc1->address,
7098 loc1->length,
7099 loc1->watchpoint_type,
7100 w1->cond_exp.get ()))
7101 || (w2->cond_exp
7102 && target_can_accel_watchpoint_condition (loc2->address,
7103 loc2->length,
7104 loc2->watchpoint_type,
7105 w2->cond_exp.get ())))
7106 return 0;
7107
7108 /* Note that this checks the owner's type, not the location's. In
7109 case the target does not support read watchpoints, but does
7110 support access watchpoints, we'll have bp_read_watchpoint
7111 watchpoints with hw_access locations. Those should be considered
7112 duplicates of hw_read locations. The hw_read locations will
7113 become hw_access locations later. */
7114 return (loc1->owner->type == loc2->owner->type
7115 && loc1->pspace->aspace == loc2->pspace->aspace
7116 && loc1->address == loc2->address
7117 && loc1->length == loc2->length);
7118 }
7119
7120 /* See breakpoint.h. */
7121
7122 int
7123 breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
7124 struct address_space *aspace2, CORE_ADDR addr2)
7125 {
7126 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
7127 || aspace1 == aspace2)
7128 && addr1 == addr2);
7129 }
7130
7131 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
7132 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
7133 matches ASPACE2. On targets that have global breakpoints, the address
7134 space doesn't really matter. */
7135
7136 static int
7137 breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
7138 int len1, struct address_space *aspace2,
7139 CORE_ADDR addr2)
7140 {
7141 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
7142 || aspace1 == aspace2)
7143 && addr2 >= addr1 && addr2 < addr1 + len1);
7144 }
7145
7146 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
7147 a ranged breakpoint. In most targets, a match happens only if ASPACE
7148 matches the breakpoint's address space. On targets that have global
7149 breakpoints, the address space doesn't really matter. */
7150
7151 static int
7152 breakpoint_location_address_match (struct bp_location *bl,
7153 struct address_space *aspace,
7154 CORE_ADDR addr)
7155 {
7156 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
7157 aspace, addr)
7158 || (bl->length
7159 && breakpoint_address_match_range (bl->pspace->aspace,
7160 bl->address, bl->length,
7161 aspace, addr)));
7162 }
7163
7164 /* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
7165 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
7166 match happens only if ASPACE matches the breakpoint's address
7167 space. On targets that have global breakpoints, the address space
7168 doesn't really matter. */
7169
7170 static int
7171 breakpoint_location_address_range_overlap (struct bp_location *bl,
7172 struct address_space *aspace,
7173 CORE_ADDR addr, int len)
7174 {
7175 if (gdbarch_has_global_breakpoints (target_gdbarch ())
7176 || bl->pspace->aspace == aspace)
7177 {
7178 int bl_len = bl->length != 0 ? bl->length : 1;
7179
7180 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
7181 return 1;
7182 }
7183 return 0;
7184 }
7185
7186 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
7187 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
7188 true, otherwise returns false. */
7189
7190 static int
7191 tracepoint_locations_match (struct bp_location *loc1,
7192 struct bp_location *loc2)
7193 {
7194 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
7195 /* Since tracepoint locations are never duplicated with others', tracepoint
7196 locations at the same address of different tracepoints are regarded as
7197 different locations. */
7198 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7199 else
7200 return 0;
7201 }
7202
7203 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
7204 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
7205 represent the same location. */
7206
7207 static int
7208 breakpoint_locations_match (struct bp_location *loc1,
7209 struct bp_location *loc2)
7210 {
7211 int hw_point1, hw_point2;
7212
7213 /* Both of them must not be in moribund_locations. */
7214 gdb_assert (loc1->owner != NULL);
7215 gdb_assert (loc2->owner != NULL);
7216
7217 hw_point1 = is_hardware_watchpoint (loc1->owner);
7218 hw_point2 = is_hardware_watchpoint (loc2->owner);
7219
7220 if (hw_point1 != hw_point2)
7221 return 0;
7222 else if (hw_point1)
7223 return watchpoint_locations_match (loc1, loc2);
7224 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7225 return tracepoint_locations_match (loc1, loc2);
7226 else
7227 /* We compare bp_location.length in order to cover ranged breakpoints. */
7228 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7229 loc2->pspace->aspace, loc2->address)
7230 && loc1->length == loc2->length);
7231 }
7232
7233 static void
7234 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7235 int bnum, int have_bnum)
7236 {
7237 /* The longest string possibly returned by hex_string_custom
7238 is 50 chars. These must be at least that big for safety. */
7239 char astr1[64];
7240 char astr2[64];
7241
7242 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7243 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
7244 if (have_bnum)
7245 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
7246 bnum, astr1, astr2);
7247 else
7248 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
7249 }
7250
7251 /* Adjust a breakpoint's address to account for architectural
7252 constraints on breakpoint placement. Return the adjusted address.
7253 Note: Very few targets require this kind of adjustment. For most
7254 targets, this function is simply the identity function. */
7255
7256 static CORE_ADDR
7257 adjust_breakpoint_address (struct gdbarch *gdbarch,
7258 CORE_ADDR bpaddr, enum bptype bptype)
7259 {
7260 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
7261 {
7262 /* Very few targets need any kind of breakpoint adjustment. */
7263 return bpaddr;
7264 }
7265 else if (bptype == bp_watchpoint
7266 || bptype == bp_hardware_watchpoint
7267 || bptype == bp_read_watchpoint
7268 || bptype == bp_access_watchpoint
7269 || bptype == bp_catchpoint)
7270 {
7271 /* Watchpoints and the various bp_catch_* eventpoints should not
7272 have their addresses modified. */
7273 return bpaddr;
7274 }
7275 else if (bptype == bp_single_step)
7276 {
7277 /* Single-step breakpoints should not have their addresses
7278 modified. If there's any architectural constrain that
7279 applies to this address, then it should have already been
7280 taken into account when the breakpoint was created in the
7281 first place. If we didn't do this, stepping through e.g.,
7282 Thumb-2 IT blocks would break. */
7283 return bpaddr;
7284 }
7285 else
7286 {
7287 CORE_ADDR adjusted_bpaddr;
7288
7289 /* Some targets have architectural constraints on the placement
7290 of breakpoint instructions. Obtain the adjusted address. */
7291 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
7292
7293 /* An adjusted breakpoint address can significantly alter
7294 a user's expectations. Print a warning if an adjustment
7295 is required. */
7296 if (adjusted_bpaddr != bpaddr)
7297 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7298
7299 return adjusted_bpaddr;
7300 }
7301 }
7302
7303 void
7304 init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
7305 struct breakpoint *owner)
7306 {
7307 memset (loc, 0, sizeof (*loc));
7308
7309 gdb_assert (ops != NULL);
7310
7311 loc->ops = ops;
7312 loc->owner = owner;
7313 loc->cond_bytecode = NULL;
7314 loc->shlib_disabled = 0;
7315 loc->enabled = 1;
7316
7317 switch (owner->type)
7318 {
7319 case bp_breakpoint:
7320 case bp_single_step:
7321 case bp_until:
7322 case bp_finish:
7323 case bp_longjmp:
7324 case bp_longjmp_resume:
7325 case bp_longjmp_call_dummy:
7326 case bp_exception:
7327 case bp_exception_resume:
7328 case bp_step_resume:
7329 case bp_hp_step_resume:
7330 case bp_watchpoint_scope:
7331 case bp_call_dummy:
7332 case bp_std_terminate:
7333 case bp_shlib_event:
7334 case bp_thread_event:
7335 case bp_overlay_event:
7336 case bp_jit_event:
7337 case bp_longjmp_master:
7338 case bp_std_terminate_master:
7339 case bp_exception_master:
7340 case bp_gnu_ifunc_resolver:
7341 case bp_gnu_ifunc_resolver_return:
7342 case bp_dprintf:
7343 loc->loc_type = bp_loc_software_breakpoint;
7344 mark_breakpoint_location_modified (loc);
7345 break;
7346 case bp_hardware_breakpoint:
7347 loc->loc_type = bp_loc_hardware_breakpoint;
7348 mark_breakpoint_location_modified (loc);
7349 break;
7350 case bp_hardware_watchpoint:
7351 case bp_read_watchpoint:
7352 case bp_access_watchpoint:
7353 loc->loc_type = bp_loc_hardware_watchpoint;
7354 break;
7355 case bp_watchpoint:
7356 case bp_catchpoint:
7357 case bp_tracepoint:
7358 case bp_fast_tracepoint:
7359 case bp_static_tracepoint:
7360 loc->loc_type = bp_loc_other;
7361 break;
7362 default:
7363 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
7364 }
7365
7366 loc->refc = 1;
7367 }
7368
7369 /* Allocate a struct bp_location. */
7370
7371 static struct bp_location *
7372 allocate_bp_location (struct breakpoint *bpt)
7373 {
7374 return bpt->ops->allocate_location (bpt);
7375 }
7376
7377 static void
7378 free_bp_location (struct bp_location *loc)
7379 {
7380 loc->ops->dtor (loc);
7381 delete loc;
7382 }
7383
7384 /* Increment reference count. */
7385
7386 static void
7387 incref_bp_location (struct bp_location *bl)
7388 {
7389 ++bl->refc;
7390 }
7391
7392 /* Decrement reference count. If the reference count reaches 0,
7393 destroy the bp_location. Sets *BLP to NULL. */
7394
7395 static void
7396 decref_bp_location (struct bp_location **blp)
7397 {
7398 gdb_assert ((*blp)->refc > 0);
7399
7400 if (--(*blp)->refc == 0)
7401 free_bp_location (*blp);
7402 *blp = NULL;
7403 }
7404
7405 /* Add breakpoint B at the end of the global breakpoint chain. */
7406
7407 static void
7408 add_to_breakpoint_chain (struct breakpoint *b)
7409 {
7410 struct breakpoint *b1;
7411
7412 /* Add this breakpoint to the end of the chain so that a list of
7413 breakpoints will come out in order of increasing numbers. */
7414
7415 b1 = breakpoint_chain;
7416 if (b1 == 0)
7417 breakpoint_chain = b;
7418 else
7419 {
7420 while (b1->next)
7421 b1 = b1->next;
7422 b1->next = b;
7423 }
7424 }
7425
7426 /* Initializes breakpoint B with type BPTYPE and no locations yet. */
7427
7428 static void
7429 init_raw_breakpoint_without_location (struct breakpoint *b,
7430 struct gdbarch *gdbarch,
7431 enum bptype bptype,
7432 const struct breakpoint_ops *ops)
7433 {
7434 memset (b, 0, sizeof (*b));
7435
7436 gdb_assert (ops != NULL);
7437
7438 b->ops = ops;
7439 b->type = bptype;
7440 b->gdbarch = gdbarch;
7441 b->language = current_language->la_language;
7442 b->input_radix = input_radix;
7443 b->thread = -1;
7444 b->enable_state = bp_enabled;
7445 b->next = 0;
7446 b->silent = 0;
7447 b->ignore_count = 0;
7448 b->commands = NULL;
7449 b->frame_id = null_frame_id;
7450 b->condition_not_parsed = 0;
7451 b->py_bp_object = NULL;
7452 b->related_breakpoint = b;
7453 b->location = NULL;
7454 }
7455
7456 /* Helper to set_raw_breakpoint below. Creates a breakpoint
7457 that has type BPTYPE and has no locations as yet. */
7458
7459 static struct breakpoint *
7460 set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
7461 enum bptype bptype,
7462 const struct breakpoint_ops *ops)
7463 {
7464 struct breakpoint *b = new breakpoint ();
7465
7466 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7467 add_to_breakpoint_chain (b);
7468 return b;
7469 }
7470
7471 /* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7472 resolutions should be made as the user specified the location explicitly
7473 enough. */
7474
7475 static void
7476 set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
7477 {
7478 gdb_assert (loc->owner != NULL);
7479
7480 if (loc->owner->type == bp_breakpoint
7481 || loc->owner->type == bp_hardware_breakpoint
7482 || is_tracepoint (loc->owner))
7483 {
7484 int is_gnu_ifunc;
7485 const char *function_name;
7486 CORE_ADDR func_addr;
7487
7488 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
7489 &func_addr, NULL, &is_gnu_ifunc);
7490
7491 if (is_gnu_ifunc && !explicit_loc)
7492 {
7493 struct breakpoint *b = loc->owner;
7494
7495 gdb_assert (loc->pspace == current_program_space);
7496 if (gnu_ifunc_resolve_name (function_name,
7497 &loc->requested_address))
7498 {
7499 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7500 loc->address = adjust_breakpoint_address (loc->gdbarch,
7501 loc->requested_address,
7502 b->type);
7503 }
7504 else if (b->type == bp_breakpoint && b->loc == loc
7505 && loc->next == NULL && b->related_breakpoint == b)
7506 {
7507 /* Create only the whole new breakpoint of this type but do not
7508 mess more complicated breakpoints with multiple locations. */
7509 b->type = bp_gnu_ifunc_resolver;
7510 /* Remember the resolver's address for use by the return
7511 breakpoint. */
7512 loc->related_address = func_addr;
7513 }
7514 }
7515
7516 if (function_name)
7517 loc->function_name = xstrdup (function_name);
7518 }
7519 }
7520
7521 /* Attempt to determine architecture of location identified by SAL. */
7522 struct gdbarch *
7523 get_sal_arch (struct symtab_and_line sal)
7524 {
7525 if (sal.section)
7526 return get_objfile_arch (sal.section->objfile);
7527 if (sal.symtab)
7528 return get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
7529
7530 return NULL;
7531 }
7532
7533 /* Low level routine for partially initializing a breakpoint of type
7534 BPTYPE. The newly created breakpoint's address, section, source
7535 file name, and line number are provided by SAL.
7536
7537 It is expected that the caller will complete the initialization of
7538 the newly created breakpoint struct as well as output any status
7539 information regarding the creation of a new breakpoint. */
7540
7541 static void
7542 init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
7543 struct symtab_and_line sal, enum bptype bptype,
7544 const struct breakpoint_ops *ops)
7545 {
7546 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7547
7548 add_location_to_breakpoint (b, &sal);
7549
7550 if (bptype != bp_catchpoint)
7551 gdb_assert (sal.pspace != NULL);
7552
7553 /* Store the program space that was used to set the breakpoint,
7554 except for ordinary breakpoints, which are independent of the
7555 program space. */
7556 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7557 b->pspace = sal.pspace;
7558 }
7559
7560 /* set_raw_breakpoint is a low level routine for allocating and
7561 partially initializing a breakpoint of type BPTYPE. The newly
7562 created breakpoint's address, section, source file name, and line
7563 number are provided by SAL. The newly created and partially
7564 initialized breakpoint is added to the breakpoint chain and
7565 is also returned as the value of this function.
7566
7567 It is expected that the caller will complete the initialization of
7568 the newly created breakpoint struct as well as output any status
7569 information regarding the creation of a new breakpoint. In
7570 particular, set_raw_breakpoint does NOT set the breakpoint
7571 number! Care should be taken to not allow an error to occur
7572 prior to completing the initialization of the breakpoint. If this
7573 should happen, a bogus breakpoint will be left on the chain. */
7574
7575 struct breakpoint *
7576 set_raw_breakpoint (struct gdbarch *gdbarch,
7577 struct symtab_and_line sal, enum bptype bptype,
7578 const struct breakpoint_ops *ops)
7579 {
7580 struct breakpoint *b = new breakpoint ();
7581
7582 init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
7583 add_to_breakpoint_chain (b);
7584 return b;
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 struct cleanup *cleanup;
9090 int retval;
9091
9092 gdb_assert (loc != NULL);
9093
9094 /* If we have a catchpoint or a watchpoint, just return 0. We should not
9095 attempt to read from the addresses the locations of these breakpoint types
9096 point to. program_breakpoint_here_p, below, will attempt to read
9097 memory. */
9098 if (!breakpoint_address_is_meaningful (loc->owner))
9099 return 0;
9100
9101 cleanup = save_current_space_and_thread ();
9102 switch_to_program_space_and_thread (loc->pspace);
9103
9104 retval = program_breakpoint_here_p (loc->gdbarch, loc->address);
9105
9106 do_cleanups (cleanup);
9107
9108 return retval;
9109 }
9110
9111 /* Build a command list for the dprintf corresponding to the current
9112 settings of the dprintf style options. */
9113
9114 static void
9115 update_dprintf_command_list (struct breakpoint *b)
9116 {
9117 char *dprintf_args = b->extra_string;
9118 char *printf_line = NULL;
9119
9120 if (!dprintf_args)
9121 return;
9122
9123 dprintf_args = skip_spaces (dprintf_args);
9124
9125 /* Allow a comma, as it may have terminated a location, but don't
9126 insist on it. */
9127 if (*dprintf_args == ',')
9128 ++dprintf_args;
9129 dprintf_args = skip_spaces (dprintf_args);
9130
9131 if (*dprintf_args != '"')
9132 error (_("Bad format string, missing '\"'."));
9133
9134 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
9135 printf_line = xstrprintf ("printf %s", dprintf_args);
9136 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
9137 {
9138 if (!dprintf_function)
9139 error (_("No function supplied for dprintf call"));
9140
9141 if (dprintf_channel && strlen (dprintf_channel) > 0)
9142 printf_line = xstrprintf ("call (void) %s (%s,%s)",
9143 dprintf_function,
9144 dprintf_channel,
9145 dprintf_args);
9146 else
9147 printf_line = xstrprintf ("call (void) %s (%s)",
9148 dprintf_function,
9149 dprintf_args);
9150 }
9151 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
9152 {
9153 if (target_can_run_breakpoint_commands ())
9154 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
9155 else
9156 {
9157 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
9158 printf_line = xstrprintf ("printf %s", dprintf_args);
9159 }
9160 }
9161 else
9162 internal_error (__FILE__, __LINE__,
9163 _("Invalid dprintf style."));
9164
9165 gdb_assert (printf_line != NULL);
9166 /* Manufacture a printf sequence. */
9167 {
9168 struct command_line *printf_cmd_line = XNEW (struct command_line);
9169
9170 printf_cmd_line->control_type = simple_control;
9171 printf_cmd_line->body_count = 0;
9172 printf_cmd_line->body_list = NULL;
9173 printf_cmd_line->next = NULL;
9174 printf_cmd_line->line = printf_line;
9175
9176 breakpoint_set_commands (b, command_line_up (printf_cmd_line));
9177 }
9178 }
9179
9180 /* Update all dprintf commands, making their command lists reflect
9181 current style settings. */
9182
9183 static void
9184 update_dprintf_commands (char *args, int from_tty,
9185 struct cmd_list_element *c)
9186 {
9187 struct breakpoint *b;
9188
9189 ALL_BREAKPOINTS (b)
9190 {
9191 if (b->type == bp_dprintf)
9192 update_dprintf_command_list (b);
9193 }
9194 }
9195
9196 /* Create a breakpoint with SAL as location. Use LOCATION
9197 as a description of the location, and COND_STRING
9198 as condition expression. If LOCATION is NULL then create an
9199 "address location" from the address in the SAL. */
9200
9201 static void
9202 init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
9203 struct symtabs_and_lines sals,
9204 event_location_up &&location,
9205 char *filter, char *cond_string,
9206 char *extra_string,
9207 enum bptype type, enum bpdisp disposition,
9208 int thread, int task, int ignore_count,
9209 const struct breakpoint_ops *ops, int from_tty,
9210 int enabled, int internal, unsigned flags,
9211 int display_canonical)
9212 {
9213 int i;
9214
9215 if (type == bp_hardware_breakpoint)
9216 {
9217 int target_resources_ok;
9218
9219 i = hw_breakpoint_used_count ();
9220 target_resources_ok =
9221 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9222 i + 1, 0);
9223 if (target_resources_ok == 0)
9224 error (_("No hardware breakpoint support in the target."));
9225 else if (target_resources_ok < 0)
9226 error (_("Hardware breakpoints used exceeds limit."));
9227 }
9228
9229 gdb_assert (sals.nelts > 0);
9230
9231 for (i = 0; i < sals.nelts; ++i)
9232 {
9233 struct symtab_and_line sal = sals.sals[i];
9234 struct bp_location *loc;
9235
9236 if (from_tty)
9237 {
9238 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9239 if (!loc_gdbarch)
9240 loc_gdbarch = gdbarch;
9241
9242 describe_other_breakpoints (loc_gdbarch,
9243 sal.pspace, sal.pc, sal.section, thread);
9244 }
9245
9246 if (i == 0)
9247 {
9248 init_raw_breakpoint (b, gdbarch, sal, type, ops);
9249 b->thread = thread;
9250 b->task = task;
9251
9252 b->cond_string = cond_string;
9253 b->extra_string = extra_string;
9254 b->ignore_count = ignore_count;
9255 b->enable_state = enabled ? bp_enabled : bp_disabled;
9256 b->disposition = disposition;
9257
9258 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9259 b->loc->inserted = 1;
9260
9261 if (type == bp_static_tracepoint)
9262 {
9263 struct tracepoint *t = (struct tracepoint *) b;
9264 struct static_tracepoint_marker marker;
9265
9266 if (strace_marker_p (b))
9267 {
9268 /* We already know the marker exists, otherwise, we
9269 wouldn't see a sal for it. */
9270 const char *p
9271 = &event_location_to_string (b->location.get ())[3];
9272 const char *endp;
9273 char *marker_str;
9274
9275 p = skip_spaces_const (p);
9276
9277 endp = skip_to_space_const (p);
9278
9279 marker_str = savestring (p, endp - p);
9280 t->static_trace_marker_id = marker_str;
9281
9282 printf_filtered (_("Probed static tracepoint "
9283 "marker \"%s\"\n"),
9284 t->static_trace_marker_id);
9285 }
9286 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
9287 {
9288 t->static_trace_marker_id = xstrdup (marker.str_id);
9289 release_static_tracepoint_marker (&marker);
9290
9291 printf_filtered (_("Probed static tracepoint "
9292 "marker \"%s\"\n"),
9293 t->static_trace_marker_id);
9294 }
9295 else
9296 warning (_("Couldn't determine the static "
9297 "tracepoint marker to probe"));
9298 }
9299
9300 loc = b->loc;
9301 }
9302 else
9303 {
9304 loc = add_location_to_breakpoint (b, &sal);
9305 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9306 loc->inserted = 1;
9307 }
9308
9309 if (b->cond_string)
9310 {
9311 const char *arg = b->cond_string;
9312
9313 loc->cond = parse_exp_1 (&arg, loc->address,
9314 block_for_pc (loc->address), 0);
9315 if (*arg)
9316 error (_("Garbage '%s' follows condition"), arg);
9317 }
9318
9319 /* Dynamic printf requires and uses additional arguments on the
9320 command line, otherwise it's an error. */
9321 if (type == bp_dprintf)
9322 {
9323 if (b->extra_string)
9324 update_dprintf_command_list (b);
9325 else
9326 error (_("Format string required"));
9327 }
9328 else if (b->extra_string)
9329 error (_("Garbage '%s' at end of command"), b->extra_string);
9330 }
9331
9332 b->display_canonical = display_canonical;
9333 if (location != NULL)
9334 b->location = std::move (location);
9335 else
9336 b->location = new_address_location (b->loc->address, NULL, 0);
9337 b->filter = filter;
9338 }
9339
9340 static void
9341 create_breakpoint_sal (struct gdbarch *gdbarch,
9342 struct symtabs_and_lines sals,
9343 event_location_up &&location,
9344 char *filter, char *cond_string,
9345 char *extra_string,
9346 enum bptype type, enum bpdisp disposition,
9347 int thread, int task, int ignore_count,
9348 const struct breakpoint_ops *ops, int from_tty,
9349 int enabled, int internal, unsigned flags,
9350 int display_canonical)
9351 {
9352 struct breakpoint *b;
9353 struct cleanup *old_chain;
9354
9355 if (is_tracepoint_type (type))
9356 {
9357 struct tracepoint *t;
9358
9359 t = new tracepoint ();
9360 b = &t->base;
9361 }
9362 else
9363 b = new breakpoint ();
9364
9365 old_chain = make_cleanup (xfree, b);
9366
9367 init_breakpoint_sal (b, gdbarch,
9368 sals, std::move (location),
9369 filter, cond_string, extra_string,
9370 type, disposition,
9371 thread, task, ignore_count,
9372 ops, from_tty,
9373 enabled, internal, flags,
9374 display_canonical);
9375 discard_cleanups (old_chain);
9376
9377 install_breakpoint (internal, b, 0);
9378 }
9379
9380 /* Add SALS.nelts breakpoints to the breakpoint table. For each
9381 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9382 value. COND_STRING, if not NULL, specified the condition to be
9383 used for all breakpoints. Essentially the only case where
9384 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9385 function. In that case, it's still not possible to specify
9386 separate conditions for different overloaded functions, so
9387 we take just a single condition string.
9388
9389 NOTE: If the function succeeds, the caller is expected to cleanup
9390 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
9391 array contents). If the function fails (error() is called), the
9392 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
9393 COND and SALS arrays and each of those arrays contents. */
9394
9395 static void
9396 create_breakpoints_sal (struct gdbarch *gdbarch,
9397 struct linespec_result *canonical,
9398 char *cond_string, char *extra_string,
9399 enum bptype type, enum bpdisp disposition,
9400 int thread, int task, int ignore_count,
9401 const struct breakpoint_ops *ops, int from_tty,
9402 int enabled, int internal, unsigned flags)
9403 {
9404 int i;
9405 struct linespec_sals *lsal;
9406
9407 if (canonical->pre_expanded)
9408 gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
9409
9410 for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
9411 {
9412 /* Note that 'location' can be NULL in the case of a plain
9413 'break', without arguments. */
9414 event_location_up location
9415 = (canonical->location != NULL
9416 ? copy_event_location (canonical->location.get ()) : NULL);
9417 char *filter_string = lsal->canonical ? xstrdup (lsal->canonical) : NULL;
9418
9419 make_cleanup (xfree, filter_string);
9420 create_breakpoint_sal (gdbarch, lsal->sals,
9421 std::move (location),
9422 filter_string,
9423 cond_string, extra_string,
9424 type, disposition,
9425 thread, task, ignore_count, ops,
9426 from_tty, enabled, internal, flags,
9427 canonical->special_display);
9428 }
9429 }
9430
9431 /* Parse LOCATION which is assumed to be a SAL specification possibly
9432 followed by conditionals. On return, SALS contains an array of SAL
9433 addresses found. LOCATION points to the end of the SAL (for
9434 linespec locations).
9435
9436 The array and the line spec strings are allocated on the heap, it is
9437 the caller's responsibility to free them. */
9438
9439 static void
9440 parse_breakpoint_sals (const struct event_location *location,
9441 struct linespec_result *canonical)
9442 {
9443 struct symtab_and_line cursal;
9444
9445 if (event_location_type (location) == LINESPEC_LOCATION)
9446 {
9447 const char *address = get_linespec_location (location);
9448
9449 if (address == NULL)
9450 {
9451 /* The last displayed codepoint, if it's valid, is our default
9452 breakpoint address. */
9453 if (last_displayed_sal_is_valid ())
9454 {
9455 struct linespec_sals lsal;
9456 struct symtab_and_line sal;
9457 CORE_ADDR pc;
9458
9459 init_sal (&sal); /* Initialize to zeroes. */
9460 lsal.sals.sals = XNEW (struct symtab_and_line);
9461
9462 /* Set sal's pspace, pc, symtab, and line to the values
9463 corresponding to the last call to print_frame_info.
9464 Be sure to reinitialize LINE with NOTCURRENT == 0
9465 as the breakpoint line number is inappropriate otherwise.
9466 find_pc_line would adjust PC, re-set it back. */
9467 get_last_displayed_sal (&sal);
9468 pc = sal.pc;
9469 sal = find_pc_line (pc, 0);
9470
9471 /* "break" without arguments is equivalent to "break *PC"
9472 where PC is the last displayed codepoint's address. So
9473 make sure to set sal.explicit_pc to prevent GDB from
9474 trying to expand the list of sals to include all other
9475 instances with the same symtab and line. */
9476 sal.pc = pc;
9477 sal.explicit_pc = 1;
9478
9479 lsal.sals.sals[0] = sal;
9480 lsal.sals.nelts = 1;
9481 lsal.canonical = NULL;
9482
9483 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
9484 return;
9485 }
9486 else
9487 error (_("No default breakpoint address now."));
9488 }
9489 }
9490
9491 /* Force almost all breakpoints to be in terms of the
9492 current_source_symtab (which is decode_line_1's default).
9493 This should produce the results we want almost all of the
9494 time while leaving default_breakpoint_* alone.
9495
9496 ObjC: However, don't match an Objective-C method name which
9497 may have a '+' or '-' succeeded by a '['. */
9498 cursal = get_current_source_symtab_and_line ();
9499 if (last_displayed_sal_is_valid ())
9500 {
9501 const char *address = NULL;
9502
9503 if (event_location_type (location) == LINESPEC_LOCATION)
9504 address = get_linespec_location (location);
9505
9506 if (!cursal.symtab
9507 || (address != NULL
9508 && strchr ("+-", address[0]) != NULL
9509 && address[1] != '['))
9510 {
9511 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9512 get_last_displayed_symtab (),
9513 get_last_displayed_line (),
9514 canonical, NULL, NULL);
9515 return;
9516 }
9517 }
9518
9519 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9520 cursal.symtab, cursal.line, canonical, NULL, NULL);
9521 }
9522
9523
9524 /* Convert each SAL into a real PC. Verify that the PC can be
9525 inserted as a breakpoint. If it can't throw an error. */
9526
9527 static void
9528 breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
9529 {
9530 int i;
9531
9532 for (i = 0; i < sals->nelts; i++)
9533 resolve_sal_pc (&sals->sals[i]);
9534 }
9535
9536 /* Fast tracepoints may have restrictions on valid locations. For
9537 instance, a fast tracepoint using a jump instead of a trap will
9538 likely have to overwrite more bytes than a trap would, and so can
9539 only be placed where the instruction is longer than the jump, or a
9540 multi-instruction sequence does not have a jump into the middle of
9541 it, etc. */
9542
9543 static void
9544 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9545 struct symtabs_and_lines *sals)
9546 {
9547 int i, rslt;
9548 struct symtab_and_line *sal;
9549 char *msg;
9550 struct cleanup *old_chain;
9551
9552 for (i = 0; i < sals->nelts; i++)
9553 {
9554 struct gdbarch *sarch;
9555
9556 sal = &sals->sals[i];
9557
9558 sarch = get_sal_arch (*sal);
9559 /* We fall back to GDBARCH if there is no architecture
9560 associated with SAL. */
9561 if (sarch == NULL)
9562 sarch = gdbarch;
9563 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc, &msg);
9564 old_chain = make_cleanup (xfree, msg);
9565
9566 if (!rslt)
9567 error (_("May not have a fast tracepoint at %s%s"),
9568 paddress (sarch, sal->pc), (msg ? msg : ""));
9569
9570 do_cleanups (old_chain);
9571 }
9572 }
9573
9574 /* Given TOK, a string specification of condition and thread, as
9575 accepted by the 'break' command, extract the condition
9576 string and thread number and set *COND_STRING and *THREAD.
9577 PC identifies the context at which the condition should be parsed.
9578 If no condition is found, *COND_STRING is set to NULL.
9579 If no thread is found, *THREAD is set to -1. */
9580
9581 static void
9582 find_condition_and_thread (const char *tok, CORE_ADDR pc,
9583 char **cond_string, int *thread, int *task,
9584 char **rest)
9585 {
9586 *cond_string = NULL;
9587 *thread = -1;
9588 *task = 0;
9589 *rest = NULL;
9590
9591 while (tok && *tok)
9592 {
9593 const char *end_tok;
9594 int toklen;
9595 const char *cond_start = NULL;
9596 const char *cond_end = NULL;
9597
9598 tok = skip_spaces_const (tok);
9599
9600 if ((*tok == '"' || *tok == ',') && rest)
9601 {
9602 *rest = savestring (tok, strlen (tok));
9603 return;
9604 }
9605
9606 end_tok = skip_to_space_const (tok);
9607
9608 toklen = end_tok - tok;
9609
9610 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9611 {
9612 tok = cond_start = end_tok + 1;
9613 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
9614 cond_end = tok;
9615 *cond_string = savestring (cond_start, cond_end - cond_start);
9616 }
9617 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9618 {
9619 const char *tmptok;
9620 struct thread_info *thr;
9621
9622 tok = end_tok + 1;
9623 thr = parse_thread_id (tok, &tmptok);
9624 if (tok == tmptok)
9625 error (_("Junk after thread keyword."));
9626 *thread = thr->global_num;
9627 tok = tmptok;
9628 }
9629 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9630 {
9631 char *tmptok;
9632
9633 tok = end_tok + 1;
9634 *task = strtol (tok, &tmptok, 0);
9635 if (tok == tmptok)
9636 error (_("Junk after task keyword."));
9637 if (!valid_task_id (*task))
9638 error (_("Unknown task %d."), *task);
9639 tok = tmptok;
9640 }
9641 else if (rest)
9642 {
9643 *rest = savestring (tok, strlen (tok));
9644 return;
9645 }
9646 else
9647 error (_("Junk at end of arguments."));
9648 }
9649 }
9650
9651 /* Decode a static tracepoint marker spec. */
9652
9653 static struct symtabs_and_lines
9654 decode_static_tracepoint_spec (const char **arg_p)
9655 {
9656 VEC(static_tracepoint_marker_p) *markers = NULL;
9657 struct symtabs_and_lines sals;
9658 struct cleanup *old_chain;
9659 const char *p = &(*arg_p)[3];
9660 const char *endp;
9661 char *marker_str;
9662 int i;
9663
9664 p = skip_spaces_const (p);
9665
9666 endp = skip_to_space_const (p);
9667
9668 marker_str = savestring (p, endp - p);
9669 old_chain = make_cleanup (xfree, marker_str);
9670
9671 markers = target_static_tracepoint_markers_by_strid (marker_str);
9672 if (VEC_empty(static_tracepoint_marker_p, markers))
9673 error (_("No known static tracepoint marker named %s"), marker_str);
9674
9675 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
9676 sals.sals = XNEWVEC (struct symtab_and_line, sals.nelts);
9677
9678 for (i = 0; i < sals.nelts; i++)
9679 {
9680 struct static_tracepoint_marker *marker;
9681
9682 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9683
9684 init_sal (&sals.sals[i]);
9685
9686 sals.sals[i] = find_pc_line (marker->address, 0);
9687 sals.sals[i].pc = marker->address;
9688
9689 release_static_tracepoint_marker (marker);
9690 }
9691
9692 do_cleanups (old_chain);
9693
9694 *arg_p = endp;
9695 return sals;
9696 }
9697
9698 /* See breakpoint.h. */
9699
9700 int
9701 create_breakpoint (struct gdbarch *gdbarch,
9702 const struct event_location *location, char *cond_string,
9703 int thread, char *extra_string,
9704 int parse_extra,
9705 int tempflag, enum bptype type_wanted,
9706 int ignore_count,
9707 enum auto_boolean pending_break_support,
9708 const struct breakpoint_ops *ops,
9709 int from_tty, int enabled, int internal,
9710 unsigned flags)
9711 {
9712 struct linespec_result canonical;
9713 struct cleanup *bkpt_chain = NULL;
9714 int pending = 0;
9715 int task = 0;
9716 int prev_bkpt_count = breakpoint_count;
9717
9718 gdb_assert (ops != NULL);
9719
9720 /* If extra_string isn't useful, set it to NULL. */
9721 if (extra_string != NULL && *extra_string == '\0')
9722 extra_string = NULL;
9723
9724 TRY
9725 {
9726 ops->create_sals_from_location (location, &canonical, type_wanted);
9727 }
9728 CATCH (e, RETURN_MASK_ERROR)
9729 {
9730 /* If caller is interested in rc value from parse, set
9731 value. */
9732 if (e.error == NOT_FOUND_ERROR)
9733 {
9734 /* If pending breakpoint support is turned off, throw
9735 error. */
9736
9737 if (pending_break_support == AUTO_BOOLEAN_FALSE)
9738 throw_exception (e);
9739
9740 exception_print (gdb_stderr, e);
9741
9742 /* If pending breakpoint support is auto query and the user
9743 selects no, then simply return the error code. */
9744 if (pending_break_support == AUTO_BOOLEAN_AUTO
9745 && !nquery (_("Make %s pending on future shared library load? "),
9746 bptype_string (type_wanted)))
9747 return 0;
9748
9749 /* At this point, either the user was queried about setting
9750 a pending breakpoint and selected yes, or pending
9751 breakpoint behavior is on and thus a pending breakpoint
9752 is defaulted on behalf of the user. */
9753 pending = 1;
9754 }
9755 else
9756 throw_exception (e);
9757 }
9758 END_CATCH
9759
9760 if (!pending && VEC_empty (linespec_sals, canonical.sals))
9761 return 0;
9762
9763 /* ----------------------------- SNIP -----------------------------
9764 Anything added to the cleanup chain beyond this point is assumed
9765 to be part of a breakpoint. If the breakpoint create succeeds
9766 then the memory is not reclaimed. */
9767 bkpt_chain = make_cleanup (null_cleanup, 0);
9768
9769 /* Resolve all line numbers to PC's and verify that the addresses
9770 are ok for the target. */
9771 if (!pending)
9772 {
9773 int ix;
9774 struct linespec_sals *iter;
9775
9776 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9777 breakpoint_sals_to_pc (&iter->sals);
9778 }
9779
9780 /* Fast tracepoints may have additional restrictions on location. */
9781 if (!pending && type_wanted == bp_fast_tracepoint)
9782 {
9783 int ix;
9784 struct linespec_sals *iter;
9785
9786 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9787 check_fast_tracepoint_sals (gdbarch, &iter->sals);
9788 }
9789
9790 /* Verify that condition can be parsed, before setting any
9791 breakpoints. Allocate a separate condition expression for each
9792 breakpoint. */
9793 if (!pending)
9794 {
9795 if (parse_extra)
9796 {
9797 char *rest;
9798 struct linespec_sals *lsal;
9799
9800 lsal = VEC_index (linespec_sals, canonical.sals, 0);
9801
9802 /* Here we only parse 'arg' to separate condition
9803 from thread number, so parsing in context of first
9804 sal is OK. When setting the breakpoint we'll
9805 re-parse it in context of each sal. */
9806
9807 find_condition_and_thread (extra_string, lsal->sals.sals[0].pc,
9808 &cond_string, &thread, &task, &rest);
9809 if (cond_string)
9810 make_cleanup (xfree, cond_string);
9811 if (rest)
9812 make_cleanup (xfree, rest);
9813 if (rest)
9814 extra_string = rest;
9815 else
9816 extra_string = NULL;
9817 }
9818 else
9819 {
9820 if (type_wanted != bp_dprintf
9821 && extra_string != NULL && *extra_string != '\0')
9822 error (_("Garbage '%s' at end of location"), extra_string);
9823
9824 /* Create a private copy of condition string. */
9825 if (cond_string)
9826 {
9827 cond_string = xstrdup (cond_string);
9828 make_cleanup (xfree, cond_string);
9829 }
9830 /* Create a private copy of any extra string. */
9831 if (extra_string)
9832 {
9833 extra_string = xstrdup (extra_string);
9834 make_cleanup (xfree, extra_string);
9835 }
9836 }
9837
9838 ops->create_breakpoints_sal (gdbarch, &canonical,
9839 cond_string, extra_string, type_wanted,
9840 tempflag ? disp_del : disp_donttouch,
9841 thread, task, ignore_count, ops,
9842 from_tty, enabled, internal, flags);
9843 }
9844 else
9845 {
9846 struct breakpoint *b;
9847
9848 if (is_tracepoint_type (type_wanted))
9849 {
9850 struct tracepoint *t;
9851
9852 t = new tracepoint ();
9853 b = &t->base;
9854 }
9855 else
9856 b = new breakpoint ();
9857
9858 init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops);
9859 b->location = copy_event_location (location);
9860
9861 if (parse_extra)
9862 b->cond_string = NULL;
9863 else
9864 {
9865 /* Create a private copy of condition string. */
9866 if (cond_string)
9867 {
9868 cond_string = xstrdup (cond_string);
9869 make_cleanup (xfree, cond_string);
9870 }
9871 b->cond_string = cond_string;
9872 b->thread = thread;
9873 }
9874
9875 /* Create a private copy of any extra string. */
9876 if (extra_string != NULL)
9877 {
9878 extra_string = xstrdup (extra_string);
9879 make_cleanup (xfree, extra_string);
9880 }
9881 b->extra_string = extra_string;
9882 b->ignore_count = ignore_count;
9883 b->disposition = tempflag ? disp_del : disp_donttouch;
9884 b->condition_not_parsed = 1;
9885 b->enable_state = enabled ? bp_enabled : bp_disabled;
9886 if ((type_wanted != bp_breakpoint
9887 && type_wanted != bp_hardware_breakpoint) || thread != -1)
9888 b->pspace = current_program_space;
9889
9890 install_breakpoint (internal, b, 0);
9891 }
9892
9893 if (VEC_length (linespec_sals, canonical.sals) > 1)
9894 {
9895 warning (_("Multiple breakpoints were set.\nUse the "
9896 "\"delete\" command to delete unwanted breakpoints."));
9897 prev_breakpoint_count = prev_bkpt_count;
9898 }
9899
9900 /* That's it. Discard the cleanups for data inserted into the
9901 breakpoint. */
9902 discard_cleanups (bkpt_chain);
9903
9904 /* error call may happen here - have BKPT_CHAIN already discarded. */
9905 update_global_location_list (UGLL_MAY_INSERT);
9906
9907 return 1;
9908 }
9909
9910 /* Set a breakpoint.
9911 ARG is a string describing breakpoint address,
9912 condition, and thread.
9913 FLAG specifies if a breakpoint is hardware on,
9914 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9915 and BP_TEMPFLAG. */
9916
9917 static void
9918 break_command_1 (char *arg, int flag, int from_tty)
9919 {
9920 int tempflag = flag & BP_TEMPFLAG;
9921 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9922 ? bp_hardware_breakpoint
9923 : bp_breakpoint);
9924 struct breakpoint_ops *ops;
9925
9926 event_location_up location = string_to_event_location (&arg, current_language);
9927
9928 /* Matching breakpoints on probes. */
9929 if (location != NULL
9930 && event_location_type (location.get ()) == PROBE_LOCATION)
9931 ops = &bkpt_probe_breakpoint_ops;
9932 else
9933 ops = &bkpt_breakpoint_ops;
9934
9935 create_breakpoint (get_current_arch (),
9936 location.get (),
9937 NULL, 0, arg, 1 /* parse arg */,
9938 tempflag, type_wanted,
9939 0 /* Ignore count */,
9940 pending_break_support,
9941 ops,
9942 from_tty,
9943 1 /* enabled */,
9944 0 /* internal */,
9945 0);
9946 }
9947
9948 /* Helper function for break_command_1 and disassemble_command. */
9949
9950 void
9951 resolve_sal_pc (struct symtab_and_line *sal)
9952 {
9953 CORE_ADDR pc;
9954
9955 if (sal->pc == 0 && sal->symtab != NULL)
9956 {
9957 if (!find_line_pc (sal->symtab, sal->line, &pc))
9958 error (_("No line %d in file \"%s\"."),
9959 sal->line, symtab_to_filename_for_display (sal->symtab));
9960 sal->pc = pc;
9961
9962 /* If this SAL corresponds to a breakpoint inserted using a line
9963 number, then skip the function prologue if necessary. */
9964 if (sal->explicit_line)
9965 skip_prologue_sal (sal);
9966 }
9967
9968 if (sal->section == 0 && sal->symtab != NULL)
9969 {
9970 const struct blockvector *bv;
9971 const struct block *b;
9972 struct symbol *sym;
9973
9974 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9975 SYMTAB_COMPUNIT (sal->symtab));
9976 if (bv != NULL)
9977 {
9978 sym = block_linkage_function (b);
9979 if (sym != NULL)
9980 {
9981 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
9982 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
9983 sym);
9984 }
9985 else
9986 {
9987 /* It really is worthwhile to have the section, so we'll
9988 just have to look harder. This case can be executed
9989 if we have line numbers but no functions (as can
9990 happen in assembly source). */
9991
9992 struct bound_minimal_symbol msym;
9993 struct cleanup *old_chain = save_current_space_and_thread ();
9994
9995 switch_to_program_space_and_thread (sal->pspace);
9996
9997 msym = lookup_minimal_symbol_by_pc (sal->pc);
9998 if (msym.minsym)
9999 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
10000
10001 do_cleanups (old_chain);
10002 }
10003 }
10004 }
10005 }
10006
10007 void
10008 break_command (char *arg, int from_tty)
10009 {
10010 break_command_1 (arg, 0, from_tty);
10011 }
10012
10013 void
10014 tbreak_command (char *arg, int from_tty)
10015 {
10016 break_command_1 (arg, BP_TEMPFLAG, from_tty);
10017 }
10018
10019 static void
10020 hbreak_command (char *arg, int from_tty)
10021 {
10022 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
10023 }
10024
10025 static void
10026 thbreak_command (char *arg, int from_tty)
10027 {
10028 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
10029 }
10030
10031 static void
10032 stop_command (char *arg, int from_tty)
10033 {
10034 printf_filtered (_("Specify the type of breakpoint to set.\n\
10035 Usage: stop in <function | address>\n\
10036 stop at <line>\n"));
10037 }
10038
10039 static void
10040 stopin_command (char *arg, int from_tty)
10041 {
10042 int badInput = 0;
10043
10044 if (arg == (char *) NULL)
10045 badInput = 1;
10046 else if (*arg != '*')
10047 {
10048 char *argptr = arg;
10049 int hasColon = 0;
10050
10051 /* Look for a ':'. If this is a line number specification, then
10052 say it is bad, otherwise, it should be an address or
10053 function/method name. */
10054 while (*argptr && !hasColon)
10055 {
10056 hasColon = (*argptr == ':');
10057 argptr++;
10058 }
10059
10060 if (hasColon)
10061 badInput = (*argptr != ':'); /* Not a class::method */
10062 else
10063 badInput = isdigit (*arg); /* a simple line number */
10064 }
10065
10066 if (badInput)
10067 printf_filtered (_("Usage: stop in <function | address>\n"));
10068 else
10069 break_command_1 (arg, 0, from_tty);
10070 }
10071
10072 static void
10073 stopat_command (char *arg, int from_tty)
10074 {
10075 int badInput = 0;
10076
10077 if (arg == (char *) NULL || *arg == '*') /* no line number */
10078 badInput = 1;
10079 else
10080 {
10081 char *argptr = arg;
10082 int hasColon = 0;
10083
10084 /* Look for a ':'. If there is a '::' then get out, otherwise
10085 it is probably a line number. */
10086 while (*argptr && !hasColon)
10087 {
10088 hasColon = (*argptr == ':');
10089 argptr++;
10090 }
10091
10092 if (hasColon)
10093 badInput = (*argptr == ':'); /* we have class::method */
10094 else
10095 badInput = !isdigit (*arg); /* not a line number */
10096 }
10097
10098 if (badInput)
10099 printf_filtered (_("Usage: stop at <line>\n"));
10100 else
10101 break_command_1 (arg, 0, from_tty);
10102 }
10103
10104 /* The dynamic printf command is mostly like a regular breakpoint, but
10105 with a prewired command list consisting of a single output command,
10106 built from extra arguments supplied on the dprintf command
10107 line. */
10108
10109 static void
10110 dprintf_command (char *arg, int from_tty)
10111 {
10112 event_location_up location = string_to_event_location (&arg, current_language);
10113
10114 /* If non-NULL, ARG should have been advanced past the location;
10115 the next character must be ','. */
10116 if (arg != NULL)
10117 {
10118 if (arg[0] != ',' || arg[1] == '\0')
10119 error (_("Format string required"));
10120 else
10121 {
10122 /* Skip the comma. */
10123 ++arg;
10124 }
10125 }
10126
10127 create_breakpoint (get_current_arch (),
10128 location.get (),
10129 NULL, 0, arg, 1 /* parse arg */,
10130 0, bp_dprintf,
10131 0 /* Ignore count */,
10132 pending_break_support,
10133 &dprintf_breakpoint_ops,
10134 from_tty,
10135 1 /* enabled */,
10136 0 /* internal */,
10137 0);
10138 }
10139
10140 static void
10141 agent_printf_command (char *arg, int from_tty)
10142 {
10143 error (_("May only run agent-printf on the target"));
10144 }
10145
10146 /* Implement the "breakpoint_hit" breakpoint_ops method for
10147 ranged breakpoints. */
10148
10149 static int
10150 breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
10151 struct address_space *aspace,
10152 CORE_ADDR bp_addr,
10153 const struct target_waitstatus *ws)
10154 {
10155 if (ws->kind != TARGET_WAITKIND_STOPPED
10156 || ws->value.sig != GDB_SIGNAL_TRAP)
10157 return 0;
10158
10159 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
10160 bl->length, aspace, bp_addr);
10161 }
10162
10163 /* Implement the "resources_needed" breakpoint_ops method for
10164 ranged breakpoints. */
10165
10166 static int
10167 resources_needed_ranged_breakpoint (const struct bp_location *bl)
10168 {
10169 return target_ranged_break_num_registers ();
10170 }
10171
10172 /* Implement the "print_it" breakpoint_ops method for
10173 ranged breakpoints. */
10174
10175 static enum print_stop_action
10176 print_it_ranged_breakpoint (bpstat bs)
10177 {
10178 struct breakpoint *b = bs->breakpoint_at;
10179 struct bp_location *bl = b->loc;
10180 struct ui_out *uiout = current_uiout;
10181
10182 gdb_assert (b->type == bp_hardware_breakpoint);
10183
10184 /* Ranged breakpoints have only one location. */
10185 gdb_assert (bl && bl->next == NULL);
10186
10187 annotate_breakpoint (b->number);
10188
10189 maybe_print_thread_hit_breakpoint (uiout);
10190
10191 if (b->disposition == disp_del)
10192 uiout->text ("Temporary ranged breakpoint ");
10193 else
10194 uiout->text ("Ranged breakpoint ");
10195 if (uiout->is_mi_like_p ())
10196 {
10197 uiout->field_string ("reason",
10198 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
10199 uiout->field_string ("disp", bpdisp_text (b->disposition));
10200 }
10201 uiout->field_int ("bkptno", b->number);
10202 uiout->text (", ");
10203
10204 return PRINT_SRC_AND_LOC;
10205 }
10206
10207 /* Implement the "print_one" breakpoint_ops method for
10208 ranged breakpoints. */
10209
10210 static void
10211 print_one_ranged_breakpoint (struct breakpoint *b,
10212 struct bp_location **last_loc)
10213 {
10214 struct bp_location *bl = b->loc;
10215 struct value_print_options opts;
10216 struct ui_out *uiout = current_uiout;
10217
10218 /* Ranged breakpoints have only one location. */
10219 gdb_assert (bl && bl->next == NULL);
10220
10221 get_user_print_options (&opts);
10222
10223 if (opts.addressprint)
10224 /* We don't print the address range here, it will be printed later
10225 by print_one_detail_ranged_breakpoint. */
10226 uiout->field_skip ("addr");
10227 annotate_field (5);
10228 print_breakpoint_location (b, bl);
10229 *last_loc = bl;
10230 }
10231
10232 /* Implement the "print_one_detail" breakpoint_ops method for
10233 ranged breakpoints. */
10234
10235 static void
10236 print_one_detail_ranged_breakpoint (const struct breakpoint *b,
10237 struct ui_out *uiout)
10238 {
10239 CORE_ADDR address_start, address_end;
10240 struct bp_location *bl = b->loc;
10241 string_file stb;
10242
10243 gdb_assert (bl);
10244
10245 address_start = bl->address;
10246 address_end = address_start + bl->length - 1;
10247
10248 uiout->text ("\taddress range: ");
10249 stb.printf ("[%s, %s]",
10250 print_core_address (bl->gdbarch, address_start),
10251 print_core_address (bl->gdbarch, address_end));
10252 uiout->field_stream ("addr", stb);
10253 uiout->text ("\n");
10254 }
10255
10256 /* Implement the "print_mention" breakpoint_ops method for
10257 ranged breakpoints. */
10258
10259 static void
10260 print_mention_ranged_breakpoint (struct breakpoint *b)
10261 {
10262 struct bp_location *bl = b->loc;
10263 struct ui_out *uiout = current_uiout;
10264
10265 gdb_assert (bl);
10266 gdb_assert (b->type == bp_hardware_breakpoint);
10267
10268 if (uiout->is_mi_like_p ())
10269 return;
10270
10271 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
10272 b->number, paddress (bl->gdbarch, bl->address),
10273 paddress (bl->gdbarch, bl->address + bl->length - 1));
10274 }
10275
10276 /* Implement the "print_recreate" breakpoint_ops method for
10277 ranged breakpoints. */
10278
10279 static void
10280 print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
10281 {
10282 fprintf_unfiltered (fp, "break-range %s, %s",
10283 event_location_to_string (b->location.get ()),
10284 event_location_to_string (b->location_range_end.get ()));
10285 print_recreate_thread (b, fp);
10286 }
10287
10288 /* The breakpoint_ops structure to be used in ranged breakpoints. */
10289
10290 static struct breakpoint_ops ranged_breakpoint_ops;
10291
10292 /* Find the address where the end of the breakpoint range should be
10293 placed, given the SAL of the end of the range. This is so that if
10294 the user provides a line number, the end of the range is set to the
10295 last instruction of the given line. */
10296
10297 static CORE_ADDR
10298 find_breakpoint_range_end (struct symtab_and_line sal)
10299 {
10300 CORE_ADDR end;
10301
10302 /* If the user provided a PC value, use it. Otherwise,
10303 find the address of the end of the given location. */
10304 if (sal.explicit_pc)
10305 end = sal.pc;
10306 else
10307 {
10308 int ret;
10309 CORE_ADDR start;
10310
10311 ret = find_line_pc_range (sal, &start, &end);
10312 if (!ret)
10313 error (_("Could not find location of the end of the range."));
10314
10315 /* find_line_pc_range returns the start of the next line. */
10316 end--;
10317 }
10318
10319 return end;
10320 }
10321
10322 /* Implement the "break-range" CLI command. */
10323
10324 static void
10325 break_range_command (char *arg, int from_tty)
10326 {
10327 char *arg_start, *addr_string_start;
10328 struct linespec_result canonical_start, canonical_end;
10329 int bp_count, can_use_bp, length;
10330 CORE_ADDR end;
10331 struct breakpoint *b;
10332 struct symtab_and_line sal_start, sal_end;
10333 struct cleanup *cleanup_bkpt;
10334 struct linespec_sals *lsal_start, *lsal_end;
10335
10336 /* We don't support software ranged breakpoints. */
10337 if (target_ranged_break_num_registers () < 0)
10338 error (_("This target does not support hardware ranged breakpoints."));
10339
10340 bp_count = hw_breakpoint_used_count ();
10341 bp_count += target_ranged_break_num_registers ();
10342 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10343 bp_count, 0);
10344 if (can_use_bp < 0)
10345 error (_("Hardware breakpoints used exceeds limit."));
10346
10347 arg = skip_spaces (arg);
10348 if (arg == NULL || arg[0] == '\0')
10349 error(_("No address range specified."));
10350
10351 arg_start = arg;
10352 event_location_up start_location = string_to_event_location (&arg,
10353 current_language);
10354 parse_breakpoint_sals (start_location.get (), &canonical_start);
10355
10356 if (arg[0] != ',')
10357 error (_("Too few arguments."));
10358 else if (VEC_empty (linespec_sals, canonical_start.sals))
10359 error (_("Could not find location of the beginning of the range."));
10360
10361 lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
10362
10363 if (VEC_length (linespec_sals, canonical_start.sals) > 1
10364 || lsal_start->sals.nelts != 1)
10365 error (_("Cannot create a ranged breakpoint with multiple locations."));
10366
10367 sal_start = lsal_start->sals.sals[0];
10368 addr_string_start = savestring (arg_start, arg - arg_start);
10369 cleanup_bkpt = make_cleanup (xfree, addr_string_start);
10370
10371 arg++; /* Skip the comma. */
10372 arg = skip_spaces (arg);
10373
10374 /* Parse the end location. */
10375
10376 arg_start = arg;
10377
10378 /* We call decode_line_full directly here instead of using
10379 parse_breakpoint_sals because we need to specify the start location's
10380 symtab and line as the default symtab and line for the end of the
10381 range. This makes it possible to have ranges like "foo.c:27, +14",
10382 where +14 means 14 lines from the start location. */
10383 event_location_up end_location = string_to_event_location (&arg,
10384 current_language);
10385 decode_line_full (end_location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
10386 sal_start.symtab, sal_start.line,
10387 &canonical_end, NULL, NULL);
10388
10389 if (VEC_empty (linespec_sals, canonical_end.sals))
10390 error (_("Could not find location of the end of the range."));
10391
10392 lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
10393 if (VEC_length (linespec_sals, canonical_end.sals) > 1
10394 || lsal_end->sals.nelts != 1)
10395 error (_("Cannot create a ranged breakpoint with multiple locations."));
10396
10397 sal_end = lsal_end->sals.sals[0];
10398
10399 end = find_breakpoint_range_end (sal_end);
10400 if (sal_start.pc > end)
10401 error (_("Invalid address range, end precedes start."));
10402
10403 length = end - sal_start.pc + 1;
10404 if (length < 0)
10405 /* Length overflowed. */
10406 error (_("Address range too large."));
10407 else if (length == 1)
10408 {
10409 /* This range is simple enough to be handled by
10410 the `hbreak' command. */
10411 hbreak_command (addr_string_start, 1);
10412
10413 do_cleanups (cleanup_bkpt);
10414
10415 return;
10416 }
10417
10418 /* Now set up the breakpoint. */
10419 b = set_raw_breakpoint (get_current_arch (), sal_start,
10420 bp_hardware_breakpoint, &ranged_breakpoint_ops);
10421 set_breakpoint_count (breakpoint_count + 1);
10422 b->number = breakpoint_count;
10423 b->disposition = disp_donttouch;
10424 b->location = std::move (start_location);
10425 b->location_range_end = std::move (end_location);
10426 b->loc->length = length;
10427
10428 do_cleanups (cleanup_bkpt);
10429
10430 mention (b);
10431 observer_notify_breakpoint_created (b);
10432 update_global_location_list (UGLL_MAY_INSERT);
10433 }
10434
10435 /* Return non-zero if EXP is verified as constant. Returned zero
10436 means EXP is variable. Also the constant detection may fail for
10437 some constant expressions and in such case still falsely return
10438 zero. */
10439
10440 static int
10441 watchpoint_exp_is_const (const struct expression *exp)
10442 {
10443 int i = exp->nelts;
10444
10445 while (i > 0)
10446 {
10447 int oplenp, argsp;
10448
10449 /* We are only interested in the descriptor of each element. */
10450 operator_length (exp, i, &oplenp, &argsp);
10451 i -= oplenp;
10452
10453 switch (exp->elts[i].opcode)
10454 {
10455 case BINOP_ADD:
10456 case BINOP_SUB:
10457 case BINOP_MUL:
10458 case BINOP_DIV:
10459 case BINOP_REM:
10460 case BINOP_MOD:
10461 case BINOP_LSH:
10462 case BINOP_RSH:
10463 case BINOP_LOGICAL_AND:
10464 case BINOP_LOGICAL_OR:
10465 case BINOP_BITWISE_AND:
10466 case BINOP_BITWISE_IOR:
10467 case BINOP_BITWISE_XOR:
10468 case BINOP_EQUAL:
10469 case BINOP_NOTEQUAL:
10470 case BINOP_LESS:
10471 case BINOP_GTR:
10472 case BINOP_LEQ:
10473 case BINOP_GEQ:
10474 case BINOP_REPEAT:
10475 case BINOP_COMMA:
10476 case BINOP_EXP:
10477 case BINOP_MIN:
10478 case BINOP_MAX:
10479 case BINOP_INTDIV:
10480 case BINOP_CONCAT:
10481 case TERNOP_COND:
10482 case TERNOP_SLICE:
10483
10484 case OP_LONG:
10485 case OP_DOUBLE:
10486 case OP_DECFLOAT:
10487 case OP_LAST:
10488 case OP_COMPLEX:
10489 case OP_STRING:
10490 case OP_ARRAY:
10491 case OP_TYPE:
10492 case OP_TYPEOF:
10493 case OP_DECLTYPE:
10494 case OP_TYPEID:
10495 case OP_NAME:
10496 case OP_OBJC_NSSTRING:
10497
10498 case UNOP_NEG:
10499 case UNOP_LOGICAL_NOT:
10500 case UNOP_COMPLEMENT:
10501 case UNOP_ADDR:
10502 case UNOP_HIGH:
10503 case UNOP_CAST:
10504
10505 case UNOP_CAST_TYPE:
10506 case UNOP_REINTERPRET_CAST:
10507 case UNOP_DYNAMIC_CAST:
10508 /* Unary, binary and ternary operators: We have to check
10509 their operands. If they are constant, then so is the
10510 result of that operation. For instance, if A and B are
10511 determined to be constants, then so is "A + B".
10512
10513 UNOP_IND is one exception to the rule above, because the
10514 value of *ADDR is not necessarily a constant, even when
10515 ADDR is. */
10516 break;
10517
10518 case OP_VAR_VALUE:
10519 /* Check whether the associated symbol is a constant.
10520
10521 We use SYMBOL_CLASS rather than TYPE_CONST because it's
10522 possible that a buggy compiler could mark a variable as
10523 constant even when it is not, and TYPE_CONST would return
10524 true in this case, while SYMBOL_CLASS wouldn't.
10525
10526 We also have to check for function symbols because they
10527 are always constant. */
10528 {
10529 struct symbol *s = exp->elts[i + 2].symbol;
10530
10531 if (SYMBOL_CLASS (s) != LOC_BLOCK
10532 && SYMBOL_CLASS (s) != LOC_CONST
10533 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10534 return 0;
10535 break;
10536 }
10537
10538 /* The default action is to return 0 because we are using
10539 the optimistic approach here: If we don't know something,
10540 then it is not a constant. */
10541 default:
10542 return 0;
10543 }
10544 }
10545
10546 return 1;
10547 }
10548
10549 /* Implement the "dtor" breakpoint_ops method for watchpoints. */
10550
10551 static void
10552 dtor_watchpoint (struct breakpoint *self)
10553 {
10554 struct watchpoint *w = (struct watchpoint *) self;
10555
10556 xfree (w->exp_string);
10557 xfree (w->exp_string_reparse);
10558 value_free (w->val);
10559
10560 base_breakpoint_ops.dtor (self);
10561 }
10562
10563 /* Implement the "re_set" breakpoint_ops method for watchpoints. */
10564
10565 static void
10566 re_set_watchpoint (struct breakpoint *b)
10567 {
10568 struct watchpoint *w = (struct watchpoint *) b;
10569
10570 /* Watchpoint can be either on expression using entirely global
10571 variables, or it can be on local variables.
10572
10573 Watchpoints of the first kind are never auto-deleted, and even
10574 persist across program restarts. Since they can use variables
10575 from shared libraries, we need to reparse expression as libraries
10576 are loaded and unloaded.
10577
10578 Watchpoints on local variables can also change meaning as result
10579 of solib event. For example, if a watchpoint uses both a local
10580 and a global variables in expression, it's a local watchpoint,
10581 but unloading of a shared library will make the expression
10582 invalid. This is not a very common use case, but we still
10583 re-evaluate expression, to avoid surprises to the user.
10584
10585 Note that for local watchpoints, we re-evaluate it only if
10586 watchpoints frame id is still valid. If it's not, it means the
10587 watchpoint is out of scope and will be deleted soon. In fact,
10588 I'm not sure we'll ever be called in this case.
10589
10590 If a local watchpoint's frame id is still valid, then
10591 w->exp_valid_block is likewise valid, and we can safely use it.
10592
10593 Don't do anything about disabled watchpoints, since they will be
10594 reevaluated again when enabled. */
10595 update_watchpoint (w, 1 /* reparse */);
10596 }
10597
10598 /* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10599
10600 static int
10601 insert_watchpoint (struct bp_location *bl)
10602 {
10603 struct watchpoint *w = (struct watchpoint *) bl->owner;
10604 int length = w->exact ? 1 : bl->length;
10605
10606 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
10607 w->cond_exp.get ());
10608 }
10609
10610 /* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10611
10612 static int
10613 remove_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10614 {
10615 struct watchpoint *w = (struct watchpoint *) bl->owner;
10616 int length = w->exact ? 1 : bl->length;
10617
10618 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
10619 w->cond_exp.get ());
10620 }
10621
10622 static int
10623 breakpoint_hit_watchpoint (const struct bp_location *bl,
10624 struct address_space *aspace, CORE_ADDR bp_addr,
10625 const struct target_waitstatus *ws)
10626 {
10627 struct breakpoint *b = bl->owner;
10628 struct watchpoint *w = (struct watchpoint *) b;
10629
10630 /* Continuable hardware watchpoints are treated as non-existent if the
10631 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10632 some data address). Otherwise gdb won't stop on a break instruction
10633 in the code (not from a breakpoint) when a hardware watchpoint has
10634 been defined. Also skip watchpoints which we know did not trigger
10635 (did not match the data address). */
10636 if (is_hardware_watchpoint (b)
10637 && w->watchpoint_triggered == watch_triggered_no)
10638 return 0;
10639
10640 return 1;
10641 }
10642
10643 static void
10644 check_status_watchpoint (bpstat bs)
10645 {
10646 gdb_assert (is_watchpoint (bs->breakpoint_at));
10647
10648 bpstat_check_watchpoint (bs);
10649 }
10650
10651 /* Implement the "resources_needed" breakpoint_ops method for
10652 hardware watchpoints. */
10653
10654 static int
10655 resources_needed_watchpoint (const struct bp_location *bl)
10656 {
10657 struct watchpoint *w = (struct watchpoint *) bl->owner;
10658 int length = w->exact? 1 : bl->length;
10659
10660 return target_region_ok_for_hw_watchpoint (bl->address, length);
10661 }
10662
10663 /* Implement the "works_in_software_mode" breakpoint_ops method for
10664 hardware watchpoints. */
10665
10666 static int
10667 works_in_software_mode_watchpoint (const struct breakpoint *b)
10668 {
10669 /* Read and access watchpoints only work with hardware support. */
10670 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
10671 }
10672
10673 static enum print_stop_action
10674 print_it_watchpoint (bpstat bs)
10675 {
10676 struct cleanup *old_chain;
10677 struct breakpoint *b;
10678 enum print_stop_action result;
10679 struct watchpoint *w;
10680 struct ui_out *uiout = current_uiout;
10681
10682 gdb_assert (bs->bp_location_at != NULL);
10683
10684 b = bs->breakpoint_at;
10685 w = (struct watchpoint *) b;
10686
10687 old_chain = make_cleanup (null_cleanup, NULL);
10688
10689 annotate_watchpoint (b->number);
10690 maybe_print_thread_hit_breakpoint (uiout);
10691
10692 string_file stb;
10693
10694 switch (b->type)
10695 {
10696 case bp_watchpoint:
10697 case bp_hardware_watchpoint:
10698 if (uiout->is_mi_like_p ())
10699 uiout->field_string
10700 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10701 mention (b);
10702 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10703 uiout->text ("\nOld value = ");
10704 watchpoint_value_print (bs->old_val, &stb);
10705 uiout->field_stream ("old", stb);
10706 uiout->text ("\nNew value = ");
10707 watchpoint_value_print (w->val, &stb);
10708 uiout->field_stream ("new", stb);
10709 uiout->text ("\n");
10710 /* More than one watchpoint may have been triggered. */
10711 result = PRINT_UNKNOWN;
10712 break;
10713
10714 case bp_read_watchpoint:
10715 if (uiout->is_mi_like_p ())
10716 uiout->field_string
10717 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10718 mention (b);
10719 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10720 uiout->text ("\nValue = ");
10721 watchpoint_value_print (w->val, &stb);
10722 uiout->field_stream ("value", stb);
10723 uiout->text ("\n");
10724 result = PRINT_UNKNOWN;
10725 break;
10726
10727 case bp_access_watchpoint:
10728 if (bs->old_val != NULL)
10729 {
10730 if (uiout->is_mi_like_p ())
10731 uiout->field_string
10732 ("reason",
10733 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10734 mention (b);
10735 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10736 uiout->text ("\nOld value = ");
10737 watchpoint_value_print (bs->old_val, &stb);
10738 uiout->field_stream ("old", stb);
10739 uiout->text ("\nNew value = ");
10740 }
10741 else
10742 {
10743 mention (b);
10744 if (uiout->is_mi_like_p ())
10745 uiout->field_string
10746 ("reason",
10747 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10748 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10749 uiout->text ("\nValue = ");
10750 }
10751 watchpoint_value_print (w->val, &stb);
10752 uiout->field_stream ("new", stb);
10753 uiout->text ("\n");
10754 result = PRINT_UNKNOWN;
10755 break;
10756 default:
10757 result = PRINT_UNKNOWN;
10758 }
10759
10760 do_cleanups (old_chain);
10761 return result;
10762 }
10763
10764 /* Implement the "print_mention" breakpoint_ops method for hardware
10765 watchpoints. */
10766
10767 static void
10768 print_mention_watchpoint (struct breakpoint *b)
10769 {
10770 struct watchpoint *w = (struct watchpoint *) b;
10771 struct ui_out *uiout = current_uiout;
10772 const char *tuple_name;
10773
10774 switch (b->type)
10775 {
10776 case bp_watchpoint:
10777 uiout->text ("Watchpoint ");
10778 tuple_name = "wpt";
10779 break;
10780 case bp_hardware_watchpoint:
10781 uiout->text ("Hardware watchpoint ");
10782 tuple_name = "wpt";
10783 break;
10784 case bp_read_watchpoint:
10785 uiout->text ("Hardware read watchpoint ");
10786 tuple_name = "hw-rwpt";
10787 break;
10788 case bp_access_watchpoint:
10789 uiout->text ("Hardware access (read/write) watchpoint ");
10790 tuple_name = "hw-awpt";
10791 break;
10792 default:
10793 internal_error (__FILE__, __LINE__,
10794 _("Invalid hardware watchpoint type."));
10795 }
10796
10797 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10798 uiout->field_int ("number", b->number);
10799 uiout->text (": ");
10800 uiout->field_string ("exp", w->exp_string);
10801 }
10802
10803 /* Implement the "print_recreate" breakpoint_ops method for
10804 watchpoints. */
10805
10806 static void
10807 print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10808 {
10809 struct watchpoint *w = (struct watchpoint *) b;
10810
10811 switch (b->type)
10812 {
10813 case bp_watchpoint:
10814 case bp_hardware_watchpoint:
10815 fprintf_unfiltered (fp, "watch");
10816 break;
10817 case bp_read_watchpoint:
10818 fprintf_unfiltered (fp, "rwatch");
10819 break;
10820 case bp_access_watchpoint:
10821 fprintf_unfiltered (fp, "awatch");
10822 break;
10823 default:
10824 internal_error (__FILE__, __LINE__,
10825 _("Invalid watchpoint type."));
10826 }
10827
10828 fprintf_unfiltered (fp, " %s", w->exp_string);
10829 print_recreate_thread (b, fp);
10830 }
10831
10832 /* Implement the "explains_signal" breakpoint_ops method for
10833 watchpoints. */
10834
10835 static int
10836 explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10837 {
10838 /* A software watchpoint cannot cause a signal other than
10839 GDB_SIGNAL_TRAP. */
10840 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
10841 return 0;
10842
10843 return 1;
10844 }
10845
10846 /* The breakpoint_ops structure to be used in hardware watchpoints. */
10847
10848 static struct breakpoint_ops watchpoint_breakpoint_ops;
10849
10850 /* Implement the "insert" breakpoint_ops method for
10851 masked hardware watchpoints. */
10852
10853 static int
10854 insert_masked_watchpoint (struct bp_location *bl)
10855 {
10856 struct watchpoint *w = (struct watchpoint *) bl->owner;
10857
10858 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
10859 bl->watchpoint_type);
10860 }
10861
10862 /* Implement the "remove" breakpoint_ops method for
10863 masked hardware watchpoints. */
10864
10865 static int
10866 remove_masked_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10867 {
10868 struct watchpoint *w = (struct watchpoint *) bl->owner;
10869
10870 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
10871 bl->watchpoint_type);
10872 }
10873
10874 /* Implement the "resources_needed" breakpoint_ops method for
10875 masked hardware watchpoints. */
10876
10877 static int
10878 resources_needed_masked_watchpoint (const struct bp_location *bl)
10879 {
10880 struct watchpoint *w = (struct watchpoint *) bl->owner;
10881
10882 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
10883 }
10884
10885 /* Implement the "works_in_software_mode" breakpoint_ops method for
10886 masked hardware watchpoints. */
10887
10888 static int
10889 works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10890 {
10891 return 0;
10892 }
10893
10894 /* Implement the "print_it" breakpoint_ops method for
10895 masked hardware watchpoints. */
10896
10897 static enum print_stop_action
10898 print_it_masked_watchpoint (bpstat bs)
10899 {
10900 struct breakpoint *b = bs->breakpoint_at;
10901 struct ui_out *uiout = current_uiout;
10902
10903 /* Masked watchpoints have only one location. */
10904 gdb_assert (b->loc && b->loc->next == NULL);
10905
10906 annotate_watchpoint (b->number);
10907 maybe_print_thread_hit_breakpoint (uiout);
10908
10909 switch (b->type)
10910 {
10911 case bp_hardware_watchpoint:
10912 if (uiout->is_mi_like_p ())
10913 uiout->field_string
10914 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10915 break;
10916
10917 case bp_read_watchpoint:
10918 if (uiout->is_mi_like_p ())
10919 uiout->field_string
10920 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10921 break;
10922
10923 case bp_access_watchpoint:
10924 if (uiout->is_mi_like_p ())
10925 uiout->field_string
10926 ("reason",
10927 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10928 break;
10929 default:
10930 internal_error (__FILE__, __LINE__,
10931 _("Invalid hardware watchpoint type."));
10932 }
10933
10934 mention (b);
10935 uiout->text (_("\n\
10936 Check the underlying instruction at PC for the memory\n\
10937 address and value which triggered this watchpoint.\n"));
10938 uiout->text ("\n");
10939
10940 /* More than one watchpoint may have been triggered. */
10941 return PRINT_UNKNOWN;
10942 }
10943
10944 /* Implement the "print_one_detail" breakpoint_ops method for
10945 masked hardware watchpoints. */
10946
10947 static void
10948 print_one_detail_masked_watchpoint (const struct breakpoint *b,
10949 struct ui_out *uiout)
10950 {
10951 struct watchpoint *w = (struct watchpoint *) b;
10952
10953 /* Masked watchpoints have only one location. */
10954 gdb_assert (b->loc && b->loc->next == NULL);
10955
10956 uiout->text ("\tmask ");
10957 uiout->field_core_addr ("mask", b->loc->gdbarch, w->hw_wp_mask);
10958 uiout->text ("\n");
10959 }
10960
10961 /* Implement the "print_mention" breakpoint_ops method for
10962 masked hardware watchpoints. */
10963
10964 static void
10965 print_mention_masked_watchpoint (struct breakpoint *b)
10966 {
10967 struct watchpoint *w = (struct watchpoint *) b;
10968 struct ui_out *uiout = current_uiout;
10969 const char *tuple_name;
10970
10971 switch (b->type)
10972 {
10973 case bp_hardware_watchpoint:
10974 uiout->text ("Masked hardware watchpoint ");
10975 tuple_name = "wpt";
10976 break;
10977 case bp_read_watchpoint:
10978 uiout->text ("Masked hardware read watchpoint ");
10979 tuple_name = "hw-rwpt";
10980 break;
10981 case bp_access_watchpoint:
10982 uiout->text ("Masked hardware access (read/write) watchpoint ");
10983 tuple_name = "hw-awpt";
10984 break;
10985 default:
10986 internal_error (__FILE__, __LINE__,
10987 _("Invalid hardware watchpoint type."));
10988 }
10989
10990 ui_out_emit_tuple tuple_emitter (uiout, tuple_name);
10991 uiout->field_int ("number", b->number);
10992 uiout->text (": ");
10993 uiout->field_string ("exp", w->exp_string);
10994 }
10995
10996 /* Implement the "print_recreate" breakpoint_ops method for
10997 masked hardware watchpoints. */
10998
10999 static void
11000 print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
11001 {
11002 struct watchpoint *w = (struct watchpoint *) b;
11003 char tmp[40];
11004
11005 switch (b->type)
11006 {
11007 case bp_hardware_watchpoint:
11008 fprintf_unfiltered (fp, "watch");
11009 break;
11010 case bp_read_watchpoint:
11011 fprintf_unfiltered (fp, "rwatch");
11012 break;
11013 case bp_access_watchpoint:
11014 fprintf_unfiltered (fp, "awatch");
11015 break;
11016 default:
11017 internal_error (__FILE__, __LINE__,
11018 _("Invalid hardware watchpoint type."));
11019 }
11020
11021 sprintf_vma (tmp, w->hw_wp_mask);
11022 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
11023 print_recreate_thread (b, fp);
11024 }
11025
11026 /* The breakpoint_ops structure to be used in masked hardware watchpoints. */
11027
11028 static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
11029
11030 /* Tell whether the given watchpoint is a masked hardware watchpoint. */
11031
11032 static int
11033 is_masked_watchpoint (const struct breakpoint *b)
11034 {
11035 return b->ops == &masked_watchpoint_breakpoint_ops;
11036 }
11037
11038 /* accessflag: hw_write: watch write,
11039 hw_read: watch read,
11040 hw_access: watch access (read or write) */
11041 static void
11042 watch_command_1 (const char *arg, int accessflag, int from_tty,
11043 int just_location, int internal)
11044 {
11045 struct breakpoint *b, *scope_breakpoint = NULL;
11046 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
11047 struct value *val, *mark, *result;
11048 int saved_bitpos = 0, saved_bitsize = 0;
11049 const char *exp_start = NULL;
11050 const char *exp_end = NULL;
11051 const char *tok, *end_tok;
11052 int toklen = -1;
11053 const char *cond_start = NULL;
11054 const char *cond_end = NULL;
11055 enum bptype bp_type;
11056 int thread = -1;
11057 int pc = 0;
11058 /* Flag to indicate whether we are going to use masks for
11059 the hardware watchpoint. */
11060 int use_mask = 0;
11061 CORE_ADDR mask = 0;
11062 struct watchpoint *w;
11063 char *expression;
11064 struct cleanup *back_to;
11065
11066 /* Make sure that we actually have parameters to parse. */
11067 if (arg != NULL && arg[0] != '\0')
11068 {
11069 const char *value_start;
11070
11071 exp_end = arg + strlen (arg);
11072
11073 /* Look for "parameter value" pairs at the end
11074 of the arguments string. */
11075 for (tok = exp_end - 1; tok > arg; tok--)
11076 {
11077 /* Skip whitespace at the end of the argument list. */
11078 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11079 tok--;
11080
11081 /* Find the beginning of the last token.
11082 This is the value of the parameter. */
11083 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11084 tok--;
11085 value_start = tok + 1;
11086
11087 /* Skip whitespace. */
11088 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11089 tok--;
11090
11091 end_tok = tok;
11092
11093 /* Find the beginning of the second to last token.
11094 This is the parameter itself. */
11095 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11096 tok--;
11097 tok++;
11098 toklen = end_tok - tok + 1;
11099
11100 if (toklen == 6 && startswith (tok, "thread"))
11101 {
11102 struct thread_info *thr;
11103 /* At this point we've found a "thread" token, which means
11104 the user is trying to set a watchpoint that triggers
11105 only in a specific thread. */
11106 const char *endp;
11107
11108 if (thread != -1)
11109 error(_("You can specify only one thread."));
11110
11111 /* Extract the thread ID from the next token. */
11112 thr = parse_thread_id (value_start, &endp);
11113
11114 /* Check if the user provided a valid thread ID. */
11115 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
11116 invalid_thread_id_error (value_start);
11117
11118 thread = thr->global_num;
11119 }
11120 else if (toklen == 4 && startswith (tok, "mask"))
11121 {
11122 /* We've found a "mask" token, which means the user wants to
11123 create a hardware watchpoint that is going to have the mask
11124 facility. */
11125 struct value *mask_value, *mark;
11126
11127 if (use_mask)
11128 error(_("You can specify only one mask."));
11129
11130 use_mask = just_location = 1;
11131
11132 mark = value_mark ();
11133 mask_value = parse_to_comma_and_eval (&value_start);
11134 mask = value_as_address (mask_value);
11135 value_free_to_mark (mark);
11136 }
11137 else
11138 /* We didn't recognize what we found. We should stop here. */
11139 break;
11140
11141 /* Truncate the string and get rid of the "parameter value" pair before
11142 the arguments string is parsed by the parse_exp_1 function. */
11143 exp_end = tok;
11144 }
11145 }
11146 else
11147 exp_end = arg;
11148
11149 /* Parse the rest of the arguments. From here on out, everything
11150 is in terms of a newly allocated string instead of the original
11151 ARG. */
11152 innermost_block = NULL;
11153 expression = savestring (arg, exp_end - arg);
11154 back_to = make_cleanup (xfree, expression);
11155 exp_start = arg = expression;
11156 expression_up exp = parse_exp_1 (&arg, 0, 0, 0);
11157 exp_end = arg;
11158 /* Remove trailing whitespace from the expression before saving it.
11159 This makes the eventual display of the expression string a bit
11160 prettier. */
11161 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
11162 --exp_end;
11163
11164 /* Checking if the expression is not constant. */
11165 if (watchpoint_exp_is_const (exp.get ()))
11166 {
11167 int len;
11168
11169 len = exp_end - exp_start;
11170 while (len > 0 && isspace (exp_start[len - 1]))
11171 len--;
11172 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
11173 }
11174
11175 exp_valid_block = innermost_block;
11176 mark = value_mark ();
11177 fetch_subexp_value (exp.get (), &pc, &val, &result, NULL, just_location);
11178
11179 if (val != NULL && just_location)
11180 {
11181 saved_bitpos = value_bitpos (val);
11182 saved_bitsize = value_bitsize (val);
11183 }
11184
11185 if (just_location)
11186 {
11187 int ret;
11188
11189 exp_valid_block = NULL;
11190 val = value_addr (result);
11191 release_value (val);
11192 value_free_to_mark (mark);
11193
11194 if (use_mask)
11195 {
11196 ret = target_masked_watch_num_registers (value_as_address (val),
11197 mask);
11198 if (ret == -1)
11199 error (_("This target does not support masked watchpoints."));
11200 else if (ret == -2)
11201 error (_("Invalid mask or memory region."));
11202 }
11203 }
11204 else if (val != NULL)
11205 release_value (val);
11206
11207 tok = skip_spaces_const (arg);
11208 end_tok = skip_to_space_const (tok);
11209
11210 toklen = end_tok - tok;
11211 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
11212 {
11213 innermost_block = NULL;
11214 tok = cond_start = end_tok + 1;
11215 parse_exp_1 (&tok, 0, 0, 0);
11216
11217 /* The watchpoint expression may not be local, but the condition
11218 may still be. E.g.: `watch global if local > 0'. */
11219 cond_exp_valid_block = innermost_block;
11220
11221 cond_end = tok;
11222 }
11223 if (*tok)
11224 error (_("Junk at end of command."));
11225
11226 frame_info *wp_frame = block_innermost_frame (exp_valid_block);
11227
11228 /* Save this because create_internal_breakpoint below invalidates
11229 'wp_frame'. */
11230 frame_id watchpoint_frame = get_frame_id (wp_frame);
11231
11232 /* If the expression is "local", then set up a "watchpoint scope"
11233 breakpoint at the point where we've left the scope of the watchpoint
11234 expression. Create the scope breakpoint before the watchpoint, so
11235 that we will encounter it first in bpstat_stop_status. */
11236 if (exp_valid_block != NULL && wp_frame != NULL)
11237 {
11238 frame_id caller_frame_id = frame_unwind_caller_id (wp_frame);
11239
11240 if (frame_id_p (caller_frame_id))
11241 {
11242 gdbarch *caller_arch = frame_unwind_caller_arch (wp_frame);
11243 CORE_ADDR caller_pc = frame_unwind_caller_pc (wp_frame);
11244
11245 scope_breakpoint
11246 = create_internal_breakpoint (caller_arch, caller_pc,
11247 bp_watchpoint_scope,
11248 &momentary_breakpoint_ops);
11249
11250 /* create_internal_breakpoint could invalidate WP_FRAME. */
11251 wp_frame = NULL;
11252
11253 scope_breakpoint->enable_state = bp_enabled;
11254
11255 /* Automatically delete the breakpoint when it hits. */
11256 scope_breakpoint->disposition = disp_del;
11257
11258 /* Only break in the proper frame (help with recursion). */
11259 scope_breakpoint->frame_id = caller_frame_id;
11260
11261 /* Set the address at which we will stop. */
11262 scope_breakpoint->loc->gdbarch = caller_arch;
11263 scope_breakpoint->loc->requested_address = caller_pc;
11264 scope_breakpoint->loc->address
11265 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
11266 scope_breakpoint->loc->requested_address,
11267 scope_breakpoint->type);
11268 }
11269 }
11270
11271 /* Now set up the breakpoint. We create all watchpoints as hardware
11272 watchpoints here even if hardware watchpoints are turned off, a call
11273 to update_watchpoint later in this function will cause the type to
11274 drop back to bp_watchpoint (software watchpoint) if required. */
11275
11276 if (accessflag == hw_read)
11277 bp_type = bp_read_watchpoint;
11278 else if (accessflag == hw_access)
11279 bp_type = bp_access_watchpoint;
11280 else
11281 bp_type = bp_hardware_watchpoint;
11282
11283 w = new watchpoint ();
11284 b = &w->base;
11285 if (use_mask)
11286 init_raw_breakpoint_without_location (b, NULL, bp_type,
11287 &masked_watchpoint_breakpoint_ops);
11288 else
11289 init_raw_breakpoint_without_location (b, NULL, bp_type,
11290 &watchpoint_breakpoint_ops);
11291 b->thread = thread;
11292 b->disposition = disp_donttouch;
11293 b->pspace = current_program_space;
11294 w->exp = std::move (exp);
11295 w->exp_valid_block = exp_valid_block;
11296 w->cond_exp_valid_block = cond_exp_valid_block;
11297 if (just_location)
11298 {
11299 struct type *t = value_type (val);
11300 CORE_ADDR addr = value_as_address (val);
11301
11302 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
11303
11304 std::string name = type_to_string (t);
11305
11306 w->exp_string_reparse = xstrprintf ("* (%s *) %s", name.c_str (),
11307 core_addr_to_string (addr));
11308
11309 w->exp_string = xstrprintf ("-location %.*s",
11310 (int) (exp_end - exp_start), exp_start);
11311
11312 /* The above expression is in C. */
11313 b->language = language_c;
11314 }
11315 else
11316 w->exp_string = savestring (exp_start, exp_end - exp_start);
11317
11318 if (use_mask)
11319 {
11320 w->hw_wp_mask = mask;
11321 }
11322 else
11323 {
11324 w->val = val;
11325 w->val_bitpos = saved_bitpos;
11326 w->val_bitsize = saved_bitsize;
11327 w->val_valid = 1;
11328 }
11329
11330 if (cond_start)
11331 b->cond_string = savestring (cond_start, cond_end - cond_start);
11332 else
11333 b->cond_string = 0;
11334
11335 if (frame_id_p (watchpoint_frame))
11336 {
11337 w->watchpoint_frame = watchpoint_frame;
11338 w->watchpoint_thread = inferior_ptid;
11339 }
11340 else
11341 {
11342 w->watchpoint_frame = null_frame_id;
11343 w->watchpoint_thread = null_ptid;
11344 }
11345
11346 if (scope_breakpoint != NULL)
11347 {
11348 /* The scope breakpoint is related to the watchpoint. We will
11349 need to act on them together. */
11350 b->related_breakpoint = scope_breakpoint;
11351 scope_breakpoint->related_breakpoint = b;
11352 }
11353
11354 if (!just_location)
11355 value_free_to_mark (mark);
11356
11357 TRY
11358 {
11359 /* Finally update the new watchpoint. This creates the locations
11360 that should be inserted. */
11361 update_watchpoint (w, 1);
11362 }
11363 CATCH (e, RETURN_MASK_ALL)
11364 {
11365 delete_breakpoint (b);
11366 throw_exception (e);
11367 }
11368 END_CATCH
11369
11370 install_breakpoint (internal, b, 1);
11371 do_cleanups (back_to);
11372 }
11373
11374 /* Return count of debug registers needed to watch the given expression.
11375 If the watchpoint cannot be handled in hardware return zero. */
11376
11377 static int
11378 can_use_hardware_watchpoint (struct value *v)
11379 {
11380 int found_memory_cnt = 0;
11381 struct value *head = v;
11382
11383 /* Did the user specifically forbid us to use hardware watchpoints? */
11384 if (!can_use_hw_watchpoints)
11385 return 0;
11386
11387 /* Make sure that the value of the expression depends only upon
11388 memory contents, and values computed from them within GDB. If we
11389 find any register references or function calls, we can't use a
11390 hardware watchpoint.
11391
11392 The idea here is that evaluating an expression generates a series
11393 of values, one holding the value of every subexpression. (The
11394 expression a*b+c has five subexpressions: a, b, a*b, c, and
11395 a*b+c.) GDB's values hold almost enough information to establish
11396 the criteria given above --- they identify memory lvalues,
11397 register lvalues, computed values, etcetera. So we can evaluate
11398 the expression, and then scan the chain of values that leaves
11399 behind to decide whether we can detect any possible change to the
11400 expression's final value using only hardware watchpoints.
11401
11402 However, I don't think that the values returned by inferior
11403 function calls are special in any way. So this function may not
11404 notice that an expression involving an inferior function call
11405 can't be watched with hardware watchpoints. FIXME. */
11406 for (; v; v = value_next (v))
11407 {
11408 if (VALUE_LVAL (v) == lval_memory)
11409 {
11410 if (v != head && value_lazy (v))
11411 /* A lazy memory lvalue in the chain is one that GDB never
11412 needed to fetch; we either just used its address (e.g.,
11413 `a' in `a.b') or we never needed it at all (e.g., `a'
11414 in `a,b'). This doesn't apply to HEAD; if that is
11415 lazy then it was not readable, but watch it anyway. */
11416 ;
11417 else
11418 {
11419 /* Ahh, memory we actually used! Check if we can cover
11420 it with hardware watchpoints. */
11421 struct type *vtype = check_typedef (value_type (v));
11422
11423 /* We only watch structs and arrays if user asked for it
11424 explicitly, never if they just happen to appear in a
11425 middle of some value chain. */
11426 if (v == head
11427 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11428 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11429 {
11430 CORE_ADDR vaddr = value_address (v);
11431 int len;
11432 int num_regs;
11433
11434 len = (target_exact_watchpoints
11435 && is_scalar_type_recursive (vtype))?
11436 1 : TYPE_LENGTH (value_type (v));
11437
11438 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11439 if (!num_regs)
11440 return 0;
11441 else
11442 found_memory_cnt += num_regs;
11443 }
11444 }
11445 }
11446 else if (VALUE_LVAL (v) != not_lval
11447 && deprecated_value_modifiable (v) == 0)
11448 return 0; /* These are values from the history (e.g., $1). */
11449 else if (VALUE_LVAL (v) == lval_register)
11450 return 0; /* Cannot watch a register with a HW watchpoint. */
11451 }
11452
11453 /* The expression itself looks suitable for using a hardware
11454 watchpoint, but give the target machine a chance to reject it. */
11455 return found_memory_cnt;
11456 }
11457
11458 void
11459 watch_command_wrapper (char *arg, int from_tty, int internal)
11460 {
11461 watch_command_1 (arg, hw_write, from_tty, 0, internal);
11462 }
11463
11464 /* A helper function that looks for the "-location" argument and then
11465 calls watch_command_1. */
11466
11467 static void
11468 watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11469 {
11470 int just_location = 0;
11471
11472 if (arg
11473 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11474 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11475 {
11476 arg = skip_spaces (arg);
11477 just_location = 1;
11478 }
11479
11480 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
11481 }
11482
11483 static void
11484 watch_command (char *arg, int from_tty)
11485 {
11486 watch_maybe_just_location (arg, hw_write, from_tty);
11487 }
11488
11489 void
11490 rwatch_command_wrapper (char *arg, int from_tty, int internal)
11491 {
11492 watch_command_1 (arg, hw_read, from_tty, 0, internal);
11493 }
11494
11495 static void
11496 rwatch_command (char *arg, int from_tty)
11497 {
11498 watch_maybe_just_location (arg, hw_read, from_tty);
11499 }
11500
11501 void
11502 awatch_command_wrapper (char *arg, int from_tty, int internal)
11503 {
11504 watch_command_1 (arg, hw_access, from_tty, 0, internal);
11505 }
11506
11507 static void
11508 awatch_command (char *arg, int from_tty)
11509 {
11510 watch_maybe_just_location (arg, hw_access, from_tty);
11511 }
11512 \f
11513
11514 /* Data for the FSM that manages the until(location)/advance commands
11515 in infcmd.c. Here because it uses the mechanisms of
11516 breakpoints. */
11517
11518 struct until_break_fsm
11519 {
11520 /* The base class. */
11521 struct thread_fsm thread_fsm;
11522
11523 /* The thread that as current when the command was executed. */
11524 int thread;
11525
11526 /* The breakpoint set at the destination location. */
11527 struct breakpoint *location_breakpoint;
11528
11529 /* Breakpoint set at the return address in the caller frame. May be
11530 NULL. */
11531 struct breakpoint *caller_breakpoint;
11532 };
11533
11534 static void until_break_fsm_clean_up (struct thread_fsm *self,
11535 struct thread_info *thread);
11536 static int until_break_fsm_should_stop (struct thread_fsm *self,
11537 struct thread_info *thread);
11538 static enum async_reply_reason
11539 until_break_fsm_async_reply_reason (struct thread_fsm *self);
11540
11541 /* until_break_fsm's vtable. */
11542
11543 static struct thread_fsm_ops until_break_fsm_ops =
11544 {
11545 NULL, /* dtor */
11546 until_break_fsm_clean_up,
11547 until_break_fsm_should_stop,
11548 NULL, /* return_value */
11549 until_break_fsm_async_reply_reason,
11550 };
11551
11552 /* Allocate a new until_break_command_fsm. */
11553
11554 static struct until_break_fsm *
11555 new_until_break_fsm (struct interp *cmd_interp, int thread,
11556 struct breakpoint *location_breakpoint,
11557 struct breakpoint *caller_breakpoint)
11558 {
11559 struct until_break_fsm *sm;
11560
11561 sm = XCNEW (struct until_break_fsm);
11562 thread_fsm_ctor (&sm->thread_fsm, &until_break_fsm_ops, cmd_interp);
11563
11564 sm->thread = thread;
11565 sm->location_breakpoint = location_breakpoint;
11566 sm->caller_breakpoint = caller_breakpoint;
11567
11568 return sm;
11569 }
11570
11571 /* Implementation of the 'should_stop' FSM method for the
11572 until(location)/advance commands. */
11573
11574 static int
11575 until_break_fsm_should_stop (struct thread_fsm *self,
11576 struct thread_info *tp)
11577 {
11578 struct until_break_fsm *sm = (struct until_break_fsm *) self;
11579
11580 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
11581 sm->location_breakpoint) != NULL
11582 || (sm->caller_breakpoint != NULL
11583 && bpstat_find_breakpoint (tp->control.stop_bpstat,
11584 sm->caller_breakpoint) != NULL))
11585 thread_fsm_set_finished (self);
11586
11587 return 1;
11588 }
11589
11590 /* Implementation of the 'clean_up' FSM method for the
11591 until(location)/advance commands. */
11592
11593 static void
11594 until_break_fsm_clean_up (struct thread_fsm *self,
11595 struct thread_info *thread)
11596 {
11597 struct until_break_fsm *sm = (struct until_break_fsm *) self;
11598
11599 /* Clean up our temporary breakpoints. */
11600 if (sm->location_breakpoint != NULL)
11601 {
11602 delete_breakpoint (sm->location_breakpoint);
11603 sm->location_breakpoint = NULL;
11604 }
11605 if (sm->caller_breakpoint != NULL)
11606 {
11607 delete_breakpoint (sm->caller_breakpoint);
11608 sm->caller_breakpoint = NULL;
11609 }
11610 delete_longjmp_breakpoint (sm->thread);
11611 }
11612
11613 /* Implementation of the 'async_reply_reason' FSM method for the
11614 until(location)/advance commands. */
11615
11616 static enum async_reply_reason
11617 until_break_fsm_async_reply_reason (struct thread_fsm *self)
11618 {
11619 return EXEC_ASYNC_LOCATION_REACHED;
11620 }
11621
11622 void
11623 until_break_command (char *arg, int from_tty, int anywhere)
11624 {
11625 struct symtabs_and_lines sals;
11626 struct symtab_and_line sal;
11627 struct frame_info *frame;
11628 struct gdbarch *frame_gdbarch;
11629 struct frame_id stack_frame_id;
11630 struct frame_id caller_frame_id;
11631 struct breakpoint *location_breakpoint;
11632 struct breakpoint *caller_breakpoint = NULL;
11633 struct cleanup *old_chain;
11634 int thread;
11635 struct thread_info *tp;
11636 struct until_break_fsm *sm;
11637
11638 clear_proceed_status (0);
11639
11640 /* Set a breakpoint where the user wants it and at return from
11641 this function. */
11642
11643 event_location_up location = string_to_event_location (&arg, current_language);
11644
11645 if (last_displayed_sal_is_valid ())
11646 sals = decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
11647 get_last_displayed_symtab (),
11648 get_last_displayed_line ());
11649 else
11650 sals = decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
11651 NULL, (struct symtab *) NULL, 0);
11652
11653 if (sals.nelts != 1)
11654 error (_("Couldn't get information on specified line."));
11655
11656 sal = sals.sals[0];
11657 xfree (sals.sals); /* malloc'd, so freed. */
11658
11659 if (*arg)
11660 error (_("Junk at end of arguments."));
11661
11662 resolve_sal_pc (&sal);
11663
11664 tp = inferior_thread ();
11665 thread = tp->global_num;
11666
11667 old_chain = make_cleanup (null_cleanup, NULL);
11668
11669 /* Note linespec handling above invalidates the frame chain.
11670 Installing a breakpoint also invalidates the frame chain (as it
11671 may need to switch threads), so do any frame handling before
11672 that. */
11673
11674 frame = get_selected_frame (NULL);
11675 frame_gdbarch = get_frame_arch (frame);
11676 stack_frame_id = get_stack_frame_id (frame);
11677 caller_frame_id = frame_unwind_caller_id (frame);
11678
11679 /* Keep within the current frame, or in frames called by the current
11680 one. */
11681
11682 if (frame_id_p (caller_frame_id))
11683 {
11684 struct symtab_and_line sal2;
11685 struct gdbarch *caller_gdbarch;
11686
11687 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11688 sal2.pc = frame_unwind_caller_pc (frame);
11689 caller_gdbarch = frame_unwind_caller_arch (frame);
11690 caller_breakpoint = set_momentary_breakpoint (caller_gdbarch,
11691 sal2,
11692 caller_frame_id,
11693 bp_until);
11694 make_cleanup_delete_breakpoint (caller_breakpoint);
11695
11696 set_longjmp_breakpoint (tp, caller_frame_id);
11697 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
11698 }
11699
11700 /* set_momentary_breakpoint could invalidate FRAME. */
11701 frame = NULL;
11702
11703 if (anywhere)
11704 /* If the user told us to continue until a specified location,
11705 we don't specify a frame at which we need to stop. */
11706 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11707 null_frame_id, bp_until);
11708 else
11709 /* Otherwise, specify the selected frame, because we want to stop
11710 only at the very same frame. */
11711 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11712 stack_frame_id, bp_until);
11713 make_cleanup_delete_breakpoint (location_breakpoint);
11714
11715 sm = new_until_break_fsm (command_interp (), tp->global_num,
11716 location_breakpoint, caller_breakpoint);
11717 tp->thread_fsm = &sm->thread_fsm;
11718
11719 discard_cleanups (old_chain);
11720
11721 proceed (-1, GDB_SIGNAL_DEFAULT);
11722 }
11723
11724 /* This function attempts to parse an optional "if <cond>" clause
11725 from the arg string. If one is not found, it returns NULL.
11726
11727 Else, it returns a pointer to the condition string. (It does not
11728 attempt to evaluate the string against a particular block.) And,
11729 it updates arg to point to the first character following the parsed
11730 if clause in the arg string. */
11731
11732 const char *
11733 ep_parse_optional_if_clause (const char **arg)
11734 {
11735 const char *cond_string;
11736
11737 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
11738 return NULL;
11739
11740 /* Skip the "if" keyword. */
11741 (*arg) += 2;
11742
11743 /* Skip any extra leading whitespace, and record the start of the
11744 condition string. */
11745 *arg = skip_spaces_const (*arg);
11746 cond_string = *arg;
11747
11748 /* Assume that the condition occupies the remainder of the arg
11749 string. */
11750 (*arg) += strlen (cond_string);
11751
11752 return cond_string;
11753 }
11754
11755 /* Commands to deal with catching events, such as signals, exceptions,
11756 process start/exit, etc. */
11757
11758 typedef enum
11759 {
11760 catch_fork_temporary, catch_vfork_temporary,
11761 catch_fork_permanent, catch_vfork_permanent
11762 }
11763 catch_fork_kind;
11764
11765 static void
11766 catch_fork_command_1 (char *arg_entry, int from_tty,
11767 struct cmd_list_element *command)
11768 {
11769 const char *arg = arg_entry;
11770 struct gdbarch *gdbarch = get_current_arch ();
11771 const char *cond_string = NULL;
11772 catch_fork_kind fork_kind;
11773 int tempflag;
11774
11775 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11776 tempflag = (fork_kind == catch_fork_temporary
11777 || fork_kind == catch_vfork_temporary);
11778
11779 if (!arg)
11780 arg = "";
11781 arg = skip_spaces_const (arg);
11782
11783 /* The allowed syntax is:
11784 catch [v]fork
11785 catch [v]fork if <cond>
11786
11787 First, check if there's an if clause. */
11788 cond_string = ep_parse_optional_if_clause (&arg);
11789
11790 if ((*arg != '\0') && !isspace (*arg))
11791 error (_("Junk at end of arguments."));
11792
11793 /* If this target supports it, create a fork or vfork catchpoint
11794 and enable reporting of such events. */
11795 switch (fork_kind)
11796 {
11797 case catch_fork_temporary:
11798 case catch_fork_permanent:
11799 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11800 &catch_fork_breakpoint_ops);
11801 break;
11802 case catch_vfork_temporary:
11803 case catch_vfork_permanent:
11804 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11805 &catch_vfork_breakpoint_ops);
11806 break;
11807 default:
11808 error (_("unsupported or unknown fork kind; cannot catch it"));
11809 break;
11810 }
11811 }
11812
11813 static void
11814 catch_exec_command_1 (char *arg_entry, int from_tty,
11815 struct cmd_list_element *command)
11816 {
11817 const char *arg = arg_entry;
11818 struct exec_catchpoint *c;
11819 struct gdbarch *gdbarch = get_current_arch ();
11820 int tempflag;
11821 const char *cond_string = NULL;
11822
11823 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11824
11825 if (!arg)
11826 arg = "";
11827 arg = skip_spaces_const (arg);
11828
11829 /* The allowed syntax is:
11830 catch exec
11831 catch exec if <cond>
11832
11833 First, check if there's an if clause. */
11834 cond_string = ep_parse_optional_if_clause (&arg);
11835
11836 if ((*arg != '\0') && !isspace (*arg))
11837 error (_("Junk at end of arguments."));
11838
11839 c = new exec_catchpoint ();
11840 init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
11841 &catch_exec_breakpoint_ops);
11842 c->exec_pathname = NULL;
11843
11844 install_breakpoint (0, &c->base, 1);
11845 }
11846
11847 void
11848 init_ada_exception_breakpoint (struct breakpoint *b,
11849 struct gdbarch *gdbarch,
11850 struct symtab_and_line sal,
11851 char *addr_string,
11852 const struct breakpoint_ops *ops,
11853 int tempflag,
11854 int enabled,
11855 int from_tty)
11856 {
11857 if (from_tty)
11858 {
11859 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11860 if (!loc_gdbarch)
11861 loc_gdbarch = gdbarch;
11862
11863 describe_other_breakpoints (loc_gdbarch,
11864 sal.pspace, sal.pc, sal.section, -1);
11865 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11866 version for exception catchpoints, because two catchpoints
11867 used for different exception names will use the same address.
11868 In this case, a "breakpoint ... also set at..." warning is
11869 unproductive. Besides, the warning phrasing is also a bit
11870 inappropriate, we should use the word catchpoint, and tell
11871 the user what type of catchpoint it is. The above is good
11872 enough for now, though. */
11873 }
11874
11875 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
11876
11877 b->enable_state = enabled ? bp_enabled : bp_disabled;
11878 b->disposition = tempflag ? disp_del : disp_donttouch;
11879 b->location = string_to_event_location (&addr_string,
11880 language_def (language_ada));
11881 b->language = language_ada;
11882 }
11883
11884 static void
11885 catch_command (char *arg, int from_tty)
11886 {
11887 error (_("Catch requires an event name."));
11888 }
11889 \f
11890
11891 static void
11892 tcatch_command (char *arg, int from_tty)
11893 {
11894 error (_("Catch requires an event name."));
11895 }
11896
11897 /* A qsort comparison function that sorts breakpoints in order. */
11898
11899 static int
11900 compare_breakpoints (const void *a, const void *b)
11901 {
11902 const breakpoint_p *ba = (const breakpoint_p *) a;
11903 uintptr_t ua = (uintptr_t) *ba;
11904 const breakpoint_p *bb = (const breakpoint_p *) b;
11905 uintptr_t ub = (uintptr_t) *bb;
11906
11907 if ((*ba)->number < (*bb)->number)
11908 return -1;
11909 else if ((*ba)->number > (*bb)->number)
11910 return 1;
11911
11912 /* Now sort by address, in case we see, e..g, two breakpoints with
11913 the number 0. */
11914 if (ua < ub)
11915 return -1;
11916 return ua > ub ? 1 : 0;
11917 }
11918
11919 /* Delete breakpoints by address or line. */
11920
11921 static void
11922 clear_command (char *arg, int from_tty)
11923 {
11924 struct breakpoint *b, *prev;
11925 VEC(breakpoint_p) *found = 0;
11926 int ix;
11927 int default_match;
11928 struct symtabs_and_lines sals;
11929 struct symtab_and_line sal;
11930 int i;
11931 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
11932
11933 if (arg)
11934 {
11935 sals = decode_line_with_current_source (arg,
11936 (DECODE_LINE_FUNFIRSTLINE
11937 | DECODE_LINE_LIST_MODE));
11938 make_cleanup (xfree, sals.sals);
11939 default_match = 0;
11940 }
11941 else
11942 {
11943 sals.sals = XNEW (struct symtab_and_line);
11944 make_cleanup (xfree, sals.sals);
11945 init_sal (&sal); /* Initialize to zeroes. */
11946
11947 /* Set sal's line, symtab, pc, and pspace to the values
11948 corresponding to the last call to print_frame_info. If the
11949 codepoint is not valid, this will set all the fields to 0. */
11950 get_last_displayed_sal (&sal);
11951 if (sal.symtab == 0)
11952 error (_("No source file specified."));
11953
11954 sals.sals[0] = sal;
11955 sals.nelts = 1;
11956
11957 default_match = 1;
11958 }
11959
11960 /* We don't call resolve_sal_pc here. That's not as bad as it
11961 seems, because all existing breakpoints typically have both
11962 file/line and pc set. So, if clear is given file/line, we can
11963 match this to existing breakpoint without obtaining pc at all.
11964
11965 We only support clearing given the address explicitly
11966 present in breakpoint table. Say, we've set breakpoint
11967 at file:line. There were several PC values for that file:line,
11968 due to optimization, all in one block.
11969
11970 We've picked one PC value. If "clear" is issued with another
11971 PC corresponding to the same file:line, the breakpoint won't
11972 be cleared. We probably can still clear the breakpoint, but
11973 since the other PC value is never presented to user, user
11974 can only find it by guessing, and it does not seem important
11975 to support that. */
11976
11977 /* For each line spec given, delete bps which correspond to it. Do
11978 it in two passes, solely to preserve the current behavior that
11979 from_tty is forced true if we delete more than one
11980 breakpoint. */
11981
11982 found = NULL;
11983 make_cleanup (VEC_cleanup (breakpoint_p), &found);
11984 for (i = 0; i < sals.nelts; i++)
11985 {
11986 const char *sal_fullname;
11987
11988 /* If exact pc given, clear bpts at that pc.
11989 If line given (pc == 0), clear all bpts on specified line.
11990 If defaulting, clear all bpts on default line
11991 or at default pc.
11992
11993 defaulting sal.pc != 0 tests to do
11994
11995 0 1 pc
11996 1 1 pc _and_ line
11997 0 0 line
11998 1 0 <can't happen> */
11999
12000 sal = sals.sals[i];
12001 sal_fullname = (sal.symtab == NULL
12002 ? NULL : symtab_to_fullname (sal.symtab));
12003
12004 /* Find all matching breakpoints and add them to 'found'. */
12005 ALL_BREAKPOINTS (b)
12006 {
12007 int match = 0;
12008 /* Are we going to delete b? */
12009 if (b->type != bp_none && !is_watchpoint (b))
12010 {
12011 struct bp_location *loc = b->loc;
12012 for (; loc; loc = loc->next)
12013 {
12014 /* If the user specified file:line, don't allow a PC
12015 match. This matches historical gdb behavior. */
12016 int pc_match = (!sal.explicit_line
12017 && sal.pc
12018 && (loc->pspace == sal.pspace)
12019 && (loc->address == sal.pc)
12020 && (!section_is_overlay (loc->section)
12021 || loc->section == sal.section));
12022 int line_match = 0;
12023
12024 if ((default_match || sal.explicit_line)
12025 && loc->symtab != NULL
12026 && sal_fullname != NULL
12027 && sal.pspace == loc->pspace
12028 && loc->line_number == sal.line
12029 && filename_cmp (symtab_to_fullname (loc->symtab),
12030 sal_fullname) == 0)
12031 line_match = 1;
12032
12033 if (pc_match || line_match)
12034 {
12035 match = 1;
12036 break;
12037 }
12038 }
12039 }
12040
12041 if (match)
12042 VEC_safe_push(breakpoint_p, found, b);
12043 }
12044 }
12045
12046 /* Now go thru the 'found' chain and delete them. */
12047 if (VEC_empty(breakpoint_p, found))
12048 {
12049 if (arg)
12050 error (_("No breakpoint at %s."), arg);
12051 else
12052 error (_("No breakpoint at this line."));
12053 }
12054
12055 /* Remove duplicates from the vec. */
12056 qsort (VEC_address (breakpoint_p, found),
12057 VEC_length (breakpoint_p, found),
12058 sizeof (breakpoint_p),
12059 compare_breakpoints);
12060 prev = VEC_index (breakpoint_p, found, 0);
12061 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
12062 {
12063 if (b == prev)
12064 {
12065 VEC_ordered_remove (breakpoint_p, found, ix);
12066 --ix;
12067 }
12068 }
12069
12070 if (VEC_length(breakpoint_p, found) > 1)
12071 from_tty = 1; /* Always report if deleted more than one. */
12072 if (from_tty)
12073 {
12074 if (VEC_length(breakpoint_p, found) == 1)
12075 printf_unfiltered (_("Deleted breakpoint "));
12076 else
12077 printf_unfiltered (_("Deleted breakpoints "));
12078 }
12079
12080 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
12081 {
12082 if (from_tty)
12083 printf_unfiltered ("%d ", b->number);
12084 delete_breakpoint (b);
12085 }
12086 if (from_tty)
12087 putchar_unfiltered ('\n');
12088
12089 do_cleanups (cleanups);
12090 }
12091 \f
12092 /* Delete breakpoint in BS if they are `delete' breakpoints and
12093 all breakpoints that are marked for deletion, whether hit or not.
12094 This is called after any breakpoint is hit, or after errors. */
12095
12096 void
12097 breakpoint_auto_delete (bpstat bs)
12098 {
12099 struct breakpoint *b, *b_tmp;
12100
12101 for (; bs; bs = bs->next)
12102 if (bs->breakpoint_at
12103 && bs->breakpoint_at->disposition == disp_del
12104 && bs->stop)
12105 delete_breakpoint (bs->breakpoint_at);
12106
12107 ALL_BREAKPOINTS_SAFE (b, b_tmp)
12108 {
12109 if (b->disposition == disp_del_at_next_stop)
12110 delete_breakpoint (b);
12111 }
12112 }
12113
12114 /* A comparison function for bp_location AP and BP being interfaced to
12115 qsort. Sort elements primarily by their ADDRESS (no matter what
12116 does breakpoint_address_is_meaningful say for its OWNER),
12117 secondarily by ordering first permanent elements and
12118 terciarily just ensuring the array is sorted stable way despite
12119 qsort being an unstable algorithm. */
12120
12121 static int
12122 bp_locations_compare (const void *ap, const void *bp)
12123 {
12124 const struct bp_location *a = *(const struct bp_location **) ap;
12125 const struct bp_location *b = *(const struct bp_location **) bp;
12126
12127 if (a->address != b->address)
12128 return (a->address > b->address) - (a->address < b->address);
12129
12130 /* Sort locations at the same address by their pspace number, keeping
12131 locations of the same inferior (in a multi-inferior environment)
12132 grouped. */
12133
12134 if (a->pspace->num != b->pspace->num)
12135 return ((a->pspace->num > b->pspace->num)
12136 - (a->pspace->num < b->pspace->num));
12137
12138 /* Sort permanent breakpoints first. */
12139 if (a->permanent != b->permanent)
12140 return (a->permanent < b->permanent) - (a->permanent > b->permanent);
12141
12142 /* Make the internal GDB representation stable across GDB runs
12143 where A and B memory inside GDB can differ. Breakpoint locations of
12144 the same type at the same address can be sorted in arbitrary order. */
12145
12146 if (a->owner->number != b->owner->number)
12147 return ((a->owner->number > b->owner->number)
12148 - (a->owner->number < b->owner->number));
12149
12150 return (a > b) - (a < b);
12151 }
12152
12153 /* Set bp_locations_placed_address_before_address_max and
12154 bp_locations_shadow_len_after_address_max according to the current
12155 content of the bp_locations array. */
12156
12157 static void
12158 bp_locations_target_extensions_update (void)
12159 {
12160 struct bp_location *bl, **blp_tmp;
12161
12162 bp_locations_placed_address_before_address_max = 0;
12163 bp_locations_shadow_len_after_address_max = 0;
12164
12165 ALL_BP_LOCATIONS (bl, blp_tmp)
12166 {
12167 CORE_ADDR start, end, addr;
12168
12169 if (!bp_location_has_shadow (bl))
12170 continue;
12171
12172 start = bl->target_info.placed_address;
12173 end = start + bl->target_info.shadow_len;
12174
12175 gdb_assert (bl->address >= start);
12176 addr = bl->address - start;
12177 if (addr > bp_locations_placed_address_before_address_max)
12178 bp_locations_placed_address_before_address_max = addr;
12179
12180 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
12181
12182 gdb_assert (bl->address < end);
12183 addr = end - bl->address;
12184 if (addr > bp_locations_shadow_len_after_address_max)
12185 bp_locations_shadow_len_after_address_max = addr;
12186 }
12187 }
12188
12189 /* Download tracepoint locations if they haven't been. */
12190
12191 static void
12192 download_tracepoint_locations (void)
12193 {
12194 struct breakpoint *b;
12195 struct cleanup *old_chain;
12196 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
12197
12198 old_chain = save_current_space_and_thread ();
12199
12200 ALL_TRACEPOINTS (b)
12201 {
12202 struct bp_location *bl;
12203 struct tracepoint *t;
12204 int bp_location_downloaded = 0;
12205
12206 if ((b->type == bp_fast_tracepoint
12207 ? !may_insert_fast_tracepoints
12208 : !may_insert_tracepoints))
12209 continue;
12210
12211 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
12212 {
12213 if (target_can_download_tracepoint ())
12214 can_download_tracepoint = TRIBOOL_TRUE;
12215 else
12216 can_download_tracepoint = TRIBOOL_FALSE;
12217 }
12218
12219 if (can_download_tracepoint == TRIBOOL_FALSE)
12220 break;
12221
12222 for (bl = b->loc; bl; bl = bl->next)
12223 {
12224 /* In tracepoint, locations are _never_ duplicated, so
12225 should_be_inserted is equivalent to
12226 unduplicated_should_be_inserted. */
12227 if (!should_be_inserted (bl) || bl->inserted)
12228 continue;
12229
12230 switch_to_program_space_and_thread (bl->pspace);
12231
12232 target_download_tracepoint (bl);
12233
12234 bl->inserted = 1;
12235 bp_location_downloaded = 1;
12236 }
12237 t = (struct tracepoint *) b;
12238 t->number_on_target = b->number;
12239 if (bp_location_downloaded)
12240 observer_notify_breakpoint_modified (b);
12241 }
12242
12243 do_cleanups (old_chain);
12244 }
12245
12246 /* Swap the insertion/duplication state between two locations. */
12247
12248 static void
12249 swap_insertion (struct bp_location *left, struct bp_location *right)
12250 {
12251 const int left_inserted = left->inserted;
12252 const int left_duplicate = left->duplicate;
12253 const int left_needs_update = left->needs_update;
12254 const struct bp_target_info left_target_info = left->target_info;
12255
12256 /* Locations of tracepoints can never be duplicated. */
12257 if (is_tracepoint (left->owner))
12258 gdb_assert (!left->duplicate);
12259 if (is_tracepoint (right->owner))
12260 gdb_assert (!right->duplicate);
12261
12262 left->inserted = right->inserted;
12263 left->duplicate = right->duplicate;
12264 left->needs_update = right->needs_update;
12265 left->target_info = right->target_info;
12266 right->inserted = left_inserted;
12267 right->duplicate = left_duplicate;
12268 right->needs_update = left_needs_update;
12269 right->target_info = left_target_info;
12270 }
12271
12272 /* Force the re-insertion of the locations at ADDRESS. This is called
12273 once a new/deleted/modified duplicate location is found and we are evaluating
12274 conditions on the target's side. Such conditions need to be updated on
12275 the target. */
12276
12277 static void
12278 force_breakpoint_reinsertion (struct bp_location *bl)
12279 {
12280 struct bp_location **locp = NULL, **loc2p;
12281 struct bp_location *loc;
12282 CORE_ADDR address = 0;
12283 int pspace_num;
12284
12285 address = bl->address;
12286 pspace_num = bl->pspace->num;
12287
12288 /* This is only meaningful if the target is
12289 evaluating conditions and if the user has
12290 opted for condition evaluation on the target's
12291 side. */
12292 if (gdb_evaluates_breakpoint_condition_p ()
12293 || !target_supports_evaluation_of_breakpoint_conditions ())
12294 return;
12295
12296 /* Flag all breakpoint locations with this address and
12297 the same program space as the location
12298 as "its condition has changed". We need to
12299 update the conditions on the target's side. */
12300 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
12301 {
12302 loc = *loc2p;
12303
12304 if (!is_breakpoint (loc->owner)
12305 || pspace_num != loc->pspace->num)
12306 continue;
12307
12308 /* Flag the location appropriately. We use a different state to
12309 let everyone know that we already updated the set of locations
12310 with addr bl->address and program space bl->pspace. This is so
12311 we don't have to keep calling these functions just to mark locations
12312 that have already been marked. */
12313 loc->condition_changed = condition_updated;
12314
12315 /* Free the agent expression bytecode as well. We will compute
12316 it later on. */
12317 loc->cond_bytecode.reset ();
12318 }
12319 }
12320 /* Called whether new breakpoints are created, or existing breakpoints
12321 deleted, to update the global location list and recompute which
12322 locations are duplicate of which.
12323
12324 The INSERT_MODE flag determines whether locations may not, may, or
12325 shall be inserted now. See 'enum ugll_insert_mode' for more
12326 info. */
12327
12328 static void
12329 update_global_location_list (enum ugll_insert_mode insert_mode)
12330 {
12331 struct breakpoint *b;
12332 struct bp_location **locp, *loc;
12333 struct cleanup *cleanups;
12334 /* Last breakpoint location address that was marked for update. */
12335 CORE_ADDR last_addr = 0;
12336 /* Last breakpoint location program space that was marked for update. */
12337 int last_pspace_num = -1;
12338
12339 /* Used in the duplicates detection below. When iterating over all
12340 bp_locations, points to the first bp_location of a given address.
12341 Breakpoints and watchpoints of different types are never
12342 duplicates of each other. Keep one pointer for each type of
12343 breakpoint/watchpoint, so we only need to loop over all locations
12344 once. */
12345 struct bp_location *bp_loc_first; /* breakpoint */
12346 struct bp_location *wp_loc_first; /* hardware watchpoint */
12347 struct bp_location *awp_loc_first; /* access watchpoint */
12348 struct bp_location *rwp_loc_first; /* read watchpoint */
12349
12350 /* Saved former bp_locations array which we compare against the newly
12351 built bp_locations from the current state of ALL_BREAKPOINTS. */
12352 struct bp_location **old_locations, **old_locp;
12353 unsigned old_locations_count;
12354
12355 old_locations = bp_locations;
12356 old_locations_count = bp_locations_count;
12357 bp_locations = NULL;
12358 bp_locations_count = 0;
12359 cleanups = make_cleanup (xfree, old_locations);
12360
12361 ALL_BREAKPOINTS (b)
12362 for (loc = b->loc; loc; loc = loc->next)
12363 bp_locations_count++;
12364
12365 bp_locations = XNEWVEC (struct bp_location *, bp_locations_count);
12366 locp = bp_locations;
12367 ALL_BREAKPOINTS (b)
12368 for (loc = b->loc; loc; loc = loc->next)
12369 *locp++ = loc;
12370 qsort (bp_locations, bp_locations_count, sizeof (*bp_locations),
12371 bp_locations_compare);
12372
12373 bp_locations_target_extensions_update ();
12374
12375 /* Identify bp_location instances that are no longer present in the
12376 new list, and therefore should be freed. Note that it's not
12377 necessary that those locations should be removed from inferior --
12378 if there's another location at the same address (previously
12379 marked as duplicate), we don't need to remove/insert the
12380 location.
12381
12382 LOCP is kept in sync with OLD_LOCP, each pointing to the current
12383 and former bp_location array state respectively. */
12384
12385 locp = bp_locations;
12386 for (old_locp = old_locations;
12387 old_locp < old_locations + old_locations_count;
12388 old_locp++)
12389 {
12390 struct bp_location *old_loc = *old_locp;
12391 struct bp_location **loc2p;
12392
12393 /* Tells if 'old_loc' is found among the new locations. If
12394 not, we have to free it. */
12395 int found_object = 0;
12396 /* Tells if the location should remain inserted in the target. */
12397 int keep_in_target = 0;
12398 int removed = 0;
12399
12400 /* Skip LOCP entries which will definitely never be needed.
12401 Stop either at or being the one matching OLD_LOC. */
12402 while (locp < bp_locations + bp_locations_count
12403 && (*locp)->address < old_loc->address)
12404 locp++;
12405
12406 for (loc2p = locp;
12407 (loc2p < bp_locations + bp_locations_count
12408 && (*loc2p)->address == old_loc->address);
12409 loc2p++)
12410 {
12411 /* Check if this is a new/duplicated location or a duplicated
12412 location that had its condition modified. If so, we want to send
12413 its condition to the target if evaluation of conditions is taking
12414 place there. */
12415 if ((*loc2p)->condition_changed == condition_modified
12416 && (last_addr != old_loc->address
12417 || last_pspace_num != old_loc->pspace->num))
12418 {
12419 force_breakpoint_reinsertion (*loc2p);
12420 last_pspace_num = old_loc->pspace->num;
12421 }
12422
12423 if (*loc2p == old_loc)
12424 found_object = 1;
12425 }
12426
12427 /* We have already handled this address, update it so that we don't
12428 have to go through updates again. */
12429 last_addr = old_loc->address;
12430
12431 /* Target-side condition evaluation: Handle deleted locations. */
12432 if (!found_object)
12433 force_breakpoint_reinsertion (old_loc);
12434
12435 /* If this location is no longer present, and inserted, look if
12436 there's maybe a new location at the same address. If so,
12437 mark that one inserted, and don't remove this one. This is
12438 needed so that we don't have a time window where a breakpoint
12439 at certain location is not inserted. */
12440
12441 if (old_loc->inserted)
12442 {
12443 /* If the location is inserted now, we might have to remove
12444 it. */
12445
12446 if (found_object && should_be_inserted (old_loc))
12447 {
12448 /* The location is still present in the location list,
12449 and still should be inserted. Don't do anything. */
12450 keep_in_target = 1;
12451 }
12452 else
12453 {
12454 /* This location still exists, but it won't be kept in the
12455 target since it may have been disabled. We proceed to
12456 remove its target-side condition. */
12457
12458 /* The location is either no longer present, or got
12459 disabled. See if there's another location at the
12460 same address, in which case we don't need to remove
12461 this one from the target. */
12462
12463 /* OLD_LOC comes from existing struct breakpoint. */
12464 if (breakpoint_address_is_meaningful (old_loc->owner))
12465 {
12466 for (loc2p = locp;
12467 (loc2p < bp_locations + bp_locations_count
12468 && (*loc2p)->address == old_loc->address);
12469 loc2p++)
12470 {
12471 struct bp_location *loc2 = *loc2p;
12472
12473 if (breakpoint_locations_match (loc2, old_loc))
12474 {
12475 /* Read watchpoint locations are switched to
12476 access watchpoints, if the former are not
12477 supported, but the latter are. */
12478 if (is_hardware_watchpoint (old_loc->owner))
12479 {
12480 gdb_assert (is_hardware_watchpoint (loc2->owner));
12481 loc2->watchpoint_type = old_loc->watchpoint_type;
12482 }
12483
12484 /* loc2 is a duplicated location. We need to check
12485 if it should be inserted in case it will be
12486 unduplicated. */
12487 if (loc2 != old_loc
12488 && unduplicated_should_be_inserted (loc2))
12489 {
12490 swap_insertion (old_loc, loc2);
12491 keep_in_target = 1;
12492 break;
12493 }
12494 }
12495 }
12496 }
12497 }
12498
12499 if (!keep_in_target)
12500 {
12501 if (remove_breakpoint (old_loc))
12502 {
12503 /* This is just about all we can do. We could keep
12504 this location on the global list, and try to
12505 remove it next time, but there's no particular
12506 reason why we will succeed next time.
12507
12508 Note that at this point, old_loc->owner is still
12509 valid, as delete_breakpoint frees the breakpoint
12510 only after calling us. */
12511 printf_filtered (_("warning: Error removing "
12512 "breakpoint %d\n"),
12513 old_loc->owner->number);
12514 }
12515 removed = 1;
12516 }
12517 }
12518
12519 if (!found_object)
12520 {
12521 if (removed && target_is_non_stop_p ()
12522 && need_moribund_for_location_type (old_loc))
12523 {
12524 /* This location was removed from the target. In
12525 non-stop mode, a race condition is possible where
12526 we've removed a breakpoint, but stop events for that
12527 breakpoint are already queued and will arrive later.
12528 We apply an heuristic to be able to distinguish such
12529 SIGTRAPs from other random SIGTRAPs: we keep this
12530 breakpoint location for a bit, and will retire it
12531 after we see some number of events. The theory here
12532 is that reporting of events should, "on the average",
12533 be fair, so after a while we'll see events from all
12534 threads that have anything of interest, and no longer
12535 need to keep this breakpoint location around. We
12536 don't hold locations forever so to reduce chances of
12537 mistaking a non-breakpoint SIGTRAP for a breakpoint
12538 SIGTRAP.
12539
12540 The heuristic failing can be disastrous on
12541 decr_pc_after_break targets.
12542
12543 On decr_pc_after_break targets, like e.g., x86-linux,
12544 if we fail to recognize a late breakpoint SIGTRAP,
12545 because events_till_retirement has reached 0 too
12546 soon, we'll fail to do the PC adjustment, and report
12547 a random SIGTRAP to the user. When the user resumes
12548 the inferior, it will most likely immediately crash
12549 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
12550 corrupted, because of being resumed e.g., in the
12551 middle of a multi-byte instruction, or skipped a
12552 one-byte instruction. This was actually seen happen
12553 on native x86-linux, and should be less rare on
12554 targets that do not support new thread events, like
12555 remote, due to the heuristic depending on
12556 thread_count.
12557
12558 Mistaking a random SIGTRAP for a breakpoint trap
12559 causes similar symptoms (PC adjustment applied when
12560 it shouldn't), but then again, playing with SIGTRAPs
12561 behind the debugger's back is asking for trouble.
12562
12563 Since hardware watchpoint traps are always
12564 distinguishable from other traps, so we don't need to
12565 apply keep hardware watchpoint moribund locations
12566 around. We simply always ignore hardware watchpoint
12567 traps we can no longer explain. */
12568
12569 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12570 old_loc->owner = NULL;
12571
12572 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
12573 }
12574 else
12575 {
12576 old_loc->owner = NULL;
12577 decref_bp_location (&old_loc);
12578 }
12579 }
12580 }
12581
12582 /* Rescan breakpoints at the same address and section, marking the
12583 first one as "first" and any others as "duplicates". This is so
12584 that the bpt instruction is only inserted once. If we have a
12585 permanent breakpoint at the same place as BPT, make that one the
12586 official one, and the rest as duplicates. Permanent breakpoints
12587 are sorted first for the same address.
12588
12589 Do the same for hardware watchpoints, but also considering the
12590 watchpoint's type (regular/access/read) and length. */
12591
12592 bp_loc_first = NULL;
12593 wp_loc_first = NULL;
12594 awp_loc_first = NULL;
12595 rwp_loc_first = NULL;
12596 ALL_BP_LOCATIONS (loc, locp)
12597 {
12598 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12599 non-NULL. */
12600 struct bp_location **loc_first_p;
12601 b = loc->owner;
12602
12603 if (!unduplicated_should_be_inserted (loc)
12604 || !breakpoint_address_is_meaningful (b)
12605 /* Don't detect duplicate for tracepoint locations because they are
12606 never duplicated. See the comments in field `duplicate' of
12607 `struct bp_location'. */
12608 || is_tracepoint (b))
12609 {
12610 /* Clear the condition modification flag. */
12611 loc->condition_changed = condition_unchanged;
12612 continue;
12613 }
12614
12615 if (b->type == bp_hardware_watchpoint)
12616 loc_first_p = &wp_loc_first;
12617 else if (b->type == bp_read_watchpoint)
12618 loc_first_p = &rwp_loc_first;
12619 else if (b->type == bp_access_watchpoint)
12620 loc_first_p = &awp_loc_first;
12621 else
12622 loc_first_p = &bp_loc_first;
12623
12624 if (*loc_first_p == NULL
12625 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12626 || !breakpoint_locations_match (loc, *loc_first_p))
12627 {
12628 *loc_first_p = loc;
12629 loc->duplicate = 0;
12630
12631 if (is_breakpoint (loc->owner) && loc->condition_changed)
12632 {
12633 loc->needs_update = 1;
12634 /* Clear the condition modification flag. */
12635 loc->condition_changed = condition_unchanged;
12636 }
12637 continue;
12638 }
12639
12640
12641 /* This and the above ensure the invariant that the first location
12642 is not duplicated, and is the inserted one.
12643 All following are marked as duplicated, and are not inserted. */
12644 if (loc->inserted)
12645 swap_insertion (loc, *loc_first_p);
12646 loc->duplicate = 1;
12647
12648 /* Clear the condition modification flag. */
12649 loc->condition_changed = condition_unchanged;
12650 }
12651
12652 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
12653 {
12654 if (insert_mode != UGLL_DONT_INSERT)
12655 insert_breakpoint_locations ();
12656 else
12657 {
12658 /* Even though the caller told us to not insert new
12659 locations, we may still need to update conditions on the
12660 target's side of breakpoints that were already inserted
12661 if the target is evaluating breakpoint conditions. We
12662 only update conditions for locations that are marked
12663 "needs_update". */
12664 update_inserted_breakpoint_locations ();
12665 }
12666 }
12667
12668 if (insert_mode != UGLL_DONT_INSERT)
12669 download_tracepoint_locations ();
12670
12671 do_cleanups (cleanups);
12672 }
12673
12674 void
12675 breakpoint_retire_moribund (void)
12676 {
12677 struct bp_location *loc;
12678 int ix;
12679
12680 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12681 if (--(loc->events_till_retirement) == 0)
12682 {
12683 decref_bp_location (&loc);
12684 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12685 --ix;
12686 }
12687 }
12688
12689 static void
12690 update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
12691 {
12692
12693 TRY
12694 {
12695 update_global_location_list (insert_mode);
12696 }
12697 CATCH (e, RETURN_MASK_ERROR)
12698 {
12699 }
12700 END_CATCH
12701 }
12702
12703 /* Clear BKP from a BPS. */
12704
12705 static void
12706 bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12707 {
12708 bpstat bs;
12709
12710 for (bs = bps; bs; bs = bs->next)
12711 if (bs->breakpoint_at == bpt)
12712 {
12713 bs->breakpoint_at = NULL;
12714 bs->old_val = NULL;
12715 /* bs->commands will be freed later. */
12716 }
12717 }
12718
12719 /* Callback for iterate_over_threads. */
12720 static int
12721 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12722 {
12723 struct breakpoint *bpt = (struct breakpoint *) data;
12724
12725 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12726 return 0;
12727 }
12728
12729 /* Helper for breakpoint and tracepoint breakpoint_ops->mention
12730 callbacks. */
12731
12732 static void
12733 say_where (struct breakpoint *b)
12734 {
12735 struct value_print_options opts;
12736
12737 get_user_print_options (&opts);
12738
12739 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12740 single string. */
12741 if (b->loc == NULL)
12742 {
12743 /* For pending locations, the output differs slightly based
12744 on b->extra_string. If this is non-NULL, it contains either
12745 a condition or dprintf arguments. */
12746 if (b->extra_string == NULL)
12747 {
12748 printf_filtered (_(" (%s) pending."),
12749 event_location_to_string (b->location.get ()));
12750 }
12751 else if (b->type == bp_dprintf)
12752 {
12753 printf_filtered (_(" (%s,%s) pending."),
12754 event_location_to_string (b->location.get ()),
12755 b->extra_string);
12756 }
12757 else
12758 {
12759 printf_filtered (_(" (%s %s) pending."),
12760 event_location_to_string (b->location.get ()),
12761 b->extra_string);
12762 }
12763 }
12764 else
12765 {
12766 if (opts.addressprint || b->loc->symtab == NULL)
12767 {
12768 printf_filtered (" at ");
12769 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12770 gdb_stdout);
12771 }
12772 if (b->loc->symtab != NULL)
12773 {
12774 /* If there is a single location, we can print the location
12775 more nicely. */
12776 if (b->loc->next == NULL)
12777 printf_filtered (": file %s, line %d.",
12778 symtab_to_filename_for_display (b->loc->symtab),
12779 b->loc->line_number);
12780 else
12781 /* This is not ideal, but each location may have a
12782 different file name, and this at least reflects the
12783 real situation somewhat. */
12784 printf_filtered (": %s.",
12785 event_location_to_string (b->location.get ()));
12786 }
12787
12788 if (b->loc->next)
12789 {
12790 struct bp_location *loc = b->loc;
12791 int n = 0;
12792 for (; loc; loc = loc->next)
12793 ++n;
12794 printf_filtered (" (%d locations)", n);
12795 }
12796 }
12797 }
12798
12799 /* Default bp_location_ops methods. */
12800
12801 static void
12802 bp_location_dtor (struct bp_location *self)
12803 {
12804 xfree (self->function_name);
12805 }
12806
12807 static const struct bp_location_ops bp_location_ops =
12808 {
12809 bp_location_dtor
12810 };
12811
12812 /* Default breakpoint_ops methods all breakpoint_ops ultimately
12813 inherit from. */
12814
12815 static void
12816 base_breakpoint_dtor (struct breakpoint *self)
12817 {
12818 decref_counted_command_line (&self->commands);
12819 xfree (self->cond_string);
12820 xfree (self->extra_string);
12821 xfree (self->filter);
12822 }
12823
12824 static struct bp_location *
12825 base_breakpoint_allocate_location (struct breakpoint *self)
12826 {
12827 struct bp_location *loc;
12828
12829 loc = new struct bp_location ();
12830 init_bp_location (loc, &bp_location_ops, self);
12831 return loc;
12832 }
12833
12834 static void
12835 base_breakpoint_re_set (struct breakpoint *b)
12836 {
12837 /* Nothing to re-set. */
12838 }
12839
12840 #define internal_error_pure_virtual_called() \
12841 gdb_assert_not_reached ("pure virtual function called")
12842
12843 static int
12844 base_breakpoint_insert_location (struct bp_location *bl)
12845 {
12846 internal_error_pure_virtual_called ();
12847 }
12848
12849 static int
12850 base_breakpoint_remove_location (struct bp_location *bl,
12851 enum remove_bp_reason reason)
12852 {
12853 internal_error_pure_virtual_called ();
12854 }
12855
12856 static int
12857 base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12858 struct address_space *aspace,
12859 CORE_ADDR bp_addr,
12860 const struct target_waitstatus *ws)
12861 {
12862 internal_error_pure_virtual_called ();
12863 }
12864
12865 static void
12866 base_breakpoint_check_status (bpstat bs)
12867 {
12868 /* Always stop. */
12869 }
12870
12871 /* A "works_in_software_mode" breakpoint_ops method that just internal
12872 errors. */
12873
12874 static int
12875 base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12876 {
12877 internal_error_pure_virtual_called ();
12878 }
12879
12880 /* A "resources_needed" breakpoint_ops method that just internal
12881 errors. */
12882
12883 static int
12884 base_breakpoint_resources_needed (const struct bp_location *bl)
12885 {
12886 internal_error_pure_virtual_called ();
12887 }
12888
12889 static enum print_stop_action
12890 base_breakpoint_print_it (bpstat bs)
12891 {
12892 internal_error_pure_virtual_called ();
12893 }
12894
12895 static void
12896 base_breakpoint_print_one_detail (const struct breakpoint *self,
12897 struct ui_out *uiout)
12898 {
12899 /* nothing */
12900 }
12901
12902 static void
12903 base_breakpoint_print_mention (struct breakpoint *b)
12904 {
12905 internal_error_pure_virtual_called ();
12906 }
12907
12908 static void
12909 base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12910 {
12911 internal_error_pure_virtual_called ();
12912 }
12913
12914 static void
12915 base_breakpoint_create_sals_from_location
12916 (const struct event_location *location,
12917 struct linespec_result *canonical,
12918 enum bptype type_wanted)
12919 {
12920 internal_error_pure_virtual_called ();
12921 }
12922
12923 static void
12924 base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12925 struct linespec_result *c,
12926 char *cond_string,
12927 char *extra_string,
12928 enum bptype type_wanted,
12929 enum bpdisp disposition,
12930 int thread,
12931 int task, int ignore_count,
12932 const struct breakpoint_ops *o,
12933 int from_tty, int enabled,
12934 int internal, unsigned flags)
12935 {
12936 internal_error_pure_virtual_called ();
12937 }
12938
12939 static void
12940 base_breakpoint_decode_location (struct breakpoint *b,
12941 const struct event_location *location,
12942 struct program_space *search_pspace,
12943 struct symtabs_and_lines *sals)
12944 {
12945 internal_error_pure_virtual_called ();
12946 }
12947
12948 /* The default 'explains_signal' method. */
12949
12950 static int
12951 base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
12952 {
12953 return 1;
12954 }
12955
12956 /* The default "after_condition_true" method. */
12957
12958 static void
12959 base_breakpoint_after_condition_true (struct bpstats *bs)
12960 {
12961 /* Nothing to do. */
12962 }
12963
12964 struct breakpoint_ops base_breakpoint_ops =
12965 {
12966 base_breakpoint_dtor,
12967 base_breakpoint_allocate_location,
12968 base_breakpoint_re_set,
12969 base_breakpoint_insert_location,
12970 base_breakpoint_remove_location,
12971 base_breakpoint_breakpoint_hit,
12972 base_breakpoint_check_status,
12973 base_breakpoint_resources_needed,
12974 base_breakpoint_works_in_software_mode,
12975 base_breakpoint_print_it,
12976 NULL,
12977 base_breakpoint_print_one_detail,
12978 base_breakpoint_print_mention,
12979 base_breakpoint_print_recreate,
12980 base_breakpoint_create_sals_from_location,
12981 base_breakpoint_create_breakpoints_sal,
12982 base_breakpoint_decode_location,
12983 base_breakpoint_explains_signal,
12984 base_breakpoint_after_condition_true,
12985 };
12986
12987 /* Default breakpoint_ops methods. */
12988
12989 static void
12990 bkpt_re_set (struct breakpoint *b)
12991 {
12992 /* FIXME: is this still reachable? */
12993 if (breakpoint_event_location_empty_p (b))
12994 {
12995 /* Anything without a location can't be re-set. */
12996 delete_breakpoint (b);
12997 return;
12998 }
12999
13000 breakpoint_re_set_default (b);
13001 }
13002
13003 static int
13004 bkpt_insert_location (struct bp_location *bl)
13005 {
13006 CORE_ADDR addr = bl->target_info.reqstd_address;
13007
13008 bl->target_info.kind = breakpoint_kind (bl, &addr);
13009 bl->target_info.placed_address = addr;
13010
13011 if (bl->loc_type == bp_loc_hardware_breakpoint)
13012 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
13013 else
13014 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
13015 }
13016
13017 static int
13018 bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
13019 {
13020 if (bl->loc_type == bp_loc_hardware_breakpoint)
13021 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
13022 else
13023 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
13024 }
13025
13026 static int
13027 bkpt_breakpoint_hit (const struct bp_location *bl,
13028 struct address_space *aspace, CORE_ADDR bp_addr,
13029 const struct target_waitstatus *ws)
13030 {
13031 if (ws->kind != TARGET_WAITKIND_STOPPED
13032 || ws->value.sig != GDB_SIGNAL_TRAP)
13033 return 0;
13034
13035 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
13036 aspace, bp_addr))
13037 return 0;
13038
13039 if (overlay_debugging /* unmapped overlay section */
13040 && section_is_overlay (bl->section)
13041 && !section_is_mapped (bl->section))
13042 return 0;
13043
13044 return 1;
13045 }
13046
13047 static int
13048 dprintf_breakpoint_hit (const struct bp_location *bl,
13049 struct address_space *aspace, CORE_ADDR bp_addr,
13050 const struct target_waitstatus *ws)
13051 {
13052 if (dprintf_style == dprintf_style_agent
13053 && target_can_run_breakpoint_commands ())
13054 {
13055 /* An agent-style dprintf never causes a stop. If we see a trap
13056 for this address it must be for a breakpoint that happens to
13057 be set at the same address. */
13058 return 0;
13059 }
13060
13061 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
13062 }
13063
13064 static int
13065 bkpt_resources_needed (const struct bp_location *bl)
13066 {
13067 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
13068
13069 return 1;
13070 }
13071
13072 static enum print_stop_action
13073 bkpt_print_it (bpstat bs)
13074 {
13075 struct breakpoint *b;
13076 const struct bp_location *bl;
13077 int bp_temp;
13078 struct ui_out *uiout = current_uiout;
13079
13080 gdb_assert (bs->bp_location_at != NULL);
13081
13082 bl = bs->bp_location_at;
13083 b = bs->breakpoint_at;
13084
13085 bp_temp = b->disposition == disp_del;
13086 if (bl->address != bl->requested_address)
13087 breakpoint_adjustment_warning (bl->requested_address,
13088 bl->address,
13089 b->number, 1);
13090 annotate_breakpoint (b->number);
13091 maybe_print_thread_hit_breakpoint (uiout);
13092
13093 if (bp_temp)
13094 uiout->text ("Temporary breakpoint ");
13095 else
13096 uiout->text ("Breakpoint ");
13097 if (uiout->is_mi_like_p ())
13098 {
13099 uiout->field_string ("reason",
13100 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
13101 uiout->field_string ("disp", bpdisp_text (b->disposition));
13102 }
13103 uiout->field_int ("bkptno", b->number);
13104 uiout->text (", ");
13105
13106 return PRINT_SRC_AND_LOC;
13107 }
13108
13109 static void
13110 bkpt_print_mention (struct breakpoint *b)
13111 {
13112 if (current_uiout->is_mi_like_p ())
13113 return;
13114
13115 switch (b->type)
13116 {
13117 case bp_breakpoint:
13118 case bp_gnu_ifunc_resolver:
13119 if (b->disposition == disp_del)
13120 printf_filtered (_("Temporary breakpoint"));
13121 else
13122 printf_filtered (_("Breakpoint"));
13123 printf_filtered (_(" %d"), b->number);
13124 if (b->type == bp_gnu_ifunc_resolver)
13125 printf_filtered (_(" at gnu-indirect-function resolver"));
13126 break;
13127 case bp_hardware_breakpoint:
13128 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
13129 break;
13130 case bp_dprintf:
13131 printf_filtered (_("Dprintf %d"), b->number);
13132 break;
13133 }
13134
13135 say_where (b);
13136 }
13137
13138 static void
13139 bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13140 {
13141 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
13142 fprintf_unfiltered (fp, "tbreak");
13143 else if (tp->type == bp_breakpoint)
13144 fprintf_unfiltered (fp, "break");
13145 else if (tp->type == bp_hardware_breakpoint
13146 && tp->disposition == disp_del)
13147 fprintf_unfiltered (fp, "thbreak");
13148 else if (tp->type == bp_hardware_breakpoint)
13149 fprintf_unfiltered (fp, "hbreak");
13150 else
13151 internal_error (__FILE__, __LINE__,
13152 _("unhandled breakpoint type %d"), (int) tp->type);
13153
13154 fprintf_unfiltered (fp, " %s",
13155 event_location_to_string (tp->location.get ()));
13156
13157 /* Print out extra_string if this breakpoint is pending. It might
13158 contain, for example, conditions that were set by the user. */
13159 if (tp->loc == NULL && tp->extra_string != NULL)
13160 fprintf_unfiltered (fp, " %s", tp->extra_string);
13161
13162 print_recreate_thread (tp, fp);
13163 }
13164
13165 static void
13166 bkpt_create_sals_from_location (const struct event_location *location,
13167 struct linespec_result *canonical,
13168 enum bptype type_wanted)
13169 {
13170 create_sals_from_location_default (location, canonical, type_wanted);
13171 }
13172
13173 static void
13174 bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
13175 struct linespec_result *canonical,
13176 char *cond_string,
13177 char *extra_string,
13178 enum bptype type_wanted,
13179 enum bpdisp disposition,
13180 int thread,
13181 int task, int ignore_count,
13182 const struct breakpoint_ops *ops,
13183 int from_tty, int enabled,
13184 int internal, unsigned flags)
13185 {
13186 create_breakpoints_sal_default (gdbarch, canonical,
13187 cond_string, extra_string,
13188 type_wanted,
13189 disposition, thread, task,
13190 ignore_count, ops, from_tty,
13191 enabled, internal, flags);
13192 }
13193
13194 static void
13195 bkpt_decode_location (struct breakpoint *b,
13196 const struct event_location *location,
13197 struct program_space *search_pspace,
13198 struct symtabs_and_lines *sals)
13199 {
13200 decode_location_default (b, location, search_pspace, sals);
13201 }
13202
13203 /* Virtual table for internal breakpoints. */
13204
13205 static void
13206 internal_bkpt_re_set (struct breakpoint *b)
13207 {
13208 switch (b->type)
13209 {
13210 /* Delete overlay event and longjmp master breakpoints; they
13211 will be reset later by breakpoint_re_set. */
13212 case bp_overlay_event:
13213 case bp_longjmp_master:
13214 case bp_std_terminate_master:
13215 case bp_exception_master:
13216 delete_breakpoint (b);
13217 break;
13218
13219 /* This breakpoint is special, it's set up when the inferior
13220 starts and we really don't want to touch it. */
13221 case bp_shlib_event:
13222
13223 /* Like bp_shlib_event, this breakpoint type is special. Once
13224 it is set up, we do not want to touch it. */
13225 case bp_thread_event:
13226 break;
13227 }
13228 }
13229
13230 static void
13231 internal_bkpt_check_status (bpstat bs)
13232 {
13233 if (bs->breakpoint_at->type == bp_shlib_event)
13234 {
13235 /* If requested, stop when the dynamic linker notifies GDB of
13236 events. This allows the user to get control and place
13237 breakpoints in initializer routines for dynamically loaded
13238 objects (among other things). */
13239 bs->stop = stop_on_solib_events;
13240 bs->print = stop_on_solib_events;
13241 }
13242 else
13243 bs->stop = 0;
13244 }
13245
13246 static enum print_stop_action
13247 internal_bkpt_print_it (bpstat bs)
13248 {
13249 struct breakpoint *b;
13250
13251 b = bs->breakpoint_at;
13252
13253 switch (b->type)
13254 {
13255 case bp_shlib_event:
13256 /* Did we stop because the user set the stop_on_solib_events
13257 variable? (If so, we report this as a generic, "Stopped due
13258 to shlib event" message.) */
13259 print_solib_event (0);
13260 break;
13261
13262 case bp_thread_event:
13263 /* Not sure how we will get here.
13264 GDB should not stop for these breakpoints. */
13265 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
13266 break;
13267
13268 case bp_overlay_event:
13269 /* By analogy with the thread event, GDB should not stop for these. */
13270 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
13271 break;
13272
13273 case bp_longjmp_master:
13274 /* These should never be enabled. */
13275 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
13276 break;
13277
13278 case bp_std_terminate_master:
13279 /* These should never be enabled. */
13280 printf_filtered (_("std::terminate Master Breakpoint: "
13281 "gdb should not stop!\n"));
13282 break;
13283
13284 case bp_exception_master:
13285 /* These should never be enabled. */
13286 printf_filtered (_("Exception Master Breakpoint: "
13287 "gdb should not stop!\n"));
13288 break;
13289 }
13290
13291 return PRINT_NOTHING;
13292 }
13293
13294 static void
13295 internal_bkpt_print_mention (struct breakpoint *b)
13296 {
13297 /* Nothing to mention. These breakpoints are internal. */
13298 }
13299
13300 /* Virtual table for momentary breakpoints */
13301
13302 static void
13303 momentary_bkpt_re_set (struct breakpoint *b)
13304 {
13305 /* Keep temporary breakpoints, which can be encountered when we step
13306 over a dlopen call and solib_add is resetting the breakpoints.
13307 Otherwise these should have been blown away via the cleanup chain
13308 or by breakpoint_init_inferior when we rerun the executable. */
13309 }
13310
13311 static void
13312 momentary_bkpt_check_status (bpstat bs)
13313 {
13314 /* Nothing. The point of these breakpoints is causing a stop. */
13315 }
13316
13317 static enum print_stop_action
13318 momentary_bkpt_print_it (bpstat bs)
13319 {
13320 return PRINT_UNKNOWN;
13321 }
13322
13323 static void
13324 momentary_bkpt_print_mention (struct breakpoint *b)
13325 {
13326 /* Nothing to mention. These breakpoints are internal. */
13327 }
13328
13329 /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
13330
13331 It gets cleared already on the removal of the first one of such placed
13332 breakpoints. This is OK as they get all removed altogether. */
13333
13334 static void
13335 longjmp_bkpt_dtor (struct breakpoint *self)
13336 {
13337 struct thread_info *tp = find_thread_global_id (self->thread);
13338
13339 if (tp)
13340 tp->initiating_frame = null_frame_id;
13341
13342 momentary_breakpoint_ops.dtor (self);
13343 }
13344
13345 /* Specific methods for probe breakpoints. */
13346
13347 static int
13348 bkpt_probe_insert_location (struct bp_location *bl)
13349 {
13350 int v = bkpt_insert_location (bl);
13351
13352 if (v == 0)
13353 {
13354 /* The insertion was successful, now let's set the probe's semaphore
13355 if needed. */
13356 if (bl->probe.probe->pops->set_semaphore != NULL)
13357 bl->probe.probe->pops->set_semaphore (bl->probe.probe,
13358 bl->probe.objfile,
13359 bl->gdbarch);
13360 }
13361
13362 return v;
13363 }
13364
13365 static int
13366 bkpt_probe_remove_location (struct bp_location *bl,
13367 enum remove_bp_reason reason)
13368 {
13369 /* Let's clear the semaphore before removing the location. */
13370 if (bl->probe.probe->pops->clear_semaphore != NULL)
13371 bl->probe.probe->pops->clear_semaphore (bl->probe.probe,
13372 bl->probe.objfile,
13373 bl->gdbarch);
13374
13375 return bkpt_remove_location (bl, reason);
13376 }
13377
13378 static void
13379 bkpt_probe_create_sals_from_location (const struct event_location *location,
13380 struct linespec_result *canonical,
13381 enum bptype type_wanted)
13382 {
13383 struct linespec_sals lsal;
13384
13385 lsal.sals = parse_probes (location, NULL, canonical);
13386 lsal.canonical
13387 = xstrdup (event_location_to_string (canonical->location.get ()));
13388 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13389 }
13390
13391 static void
13392 bkpt_probe_decode_location (struct breakpoint *b,
13393 const struct event_location *location,
13394 struct program_space *search_pspace,
13395 struct symtabs_and_lines *sals)
13396 {
13397 *sals = parse_probes (location, search_pspace, NULL);
13398 if (!sals->sals)
13399 error (_("probe not found"));
13400 }
13401
13402 /* The breakpoint_ops structure to be used in tracepoints. */
13403
13404 static void
13405 tracepoint_re_set (struct breakpoint *b)
13406 {
13407 breakpoint_re_set_default (b);
13408 }
13409
13410 static int
13411 tracepoint_breakpoint_hit (const struct bp_location *bl,
13412 struct address_space *aspace, CORE_ADDR bp_addr,
13413 const struct target_waitstatus *ws)
13414 {
13415 /* By definition, the inferior does not report stops at
13416 tracepoints. */
13417 return 0;
13418 }
13419
13420 static void
13421 tracepoint_print_one_detail (const struct breakpoint *self,
13422 struct ui_out *uiout)
13423 {
13424 struct tracepoint *tp = (struct tracepoint *) self;
13425 if (tp->static_trace_marker_id)
13426 {
13427 gdb_assert (self->type == bp_static_tracepoint);
13428
13429 uiout->text ("\tmarker id is ");
13430 uiout->field_string ("static-tracepoint-marker-string-id",
13431 tp->static_trace_marker_id);
13432 uiout->text ("\n");
13433 }
13434 }
13435
13436 static void
13437 tracepoint_print_mention (struct breakpoint *b)
13438 {
13439 if (current_uiout->is_mi_like_p ())
13440 return;
13441
13442 switch (b->type)
13443 {
13444 case bp_tracepoint:
13445 printf_filtered (_("Tracepoint"));
13446 printf_filtered (_(" %d"), b->number);
13447 break;
13448 case bp_fast_tracepoint:
13449 printf_filtered (_("Fast tracepoint"));
13450 printf_filtered (_(" %d"), b->number);
13451 break;
13452 case bp_static_tracepoint:
13453 printf_filtered (_("Static tracepoint"));
13454 printf_filtered (_(" %d"), b->number);
13455 break;
13456 default:
13457 internal_error (__FILE__, __LINE__,
13458 _("unhandled tracepoint type %d"), (int) b->type);
13459 }
13460
13461 say_where (b);
13462 }
13463
13464 static void
13465 tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
13466 {
13467 struct tracepoint *tp = (struct tracepoint *) self;
13468
13469 if (self->type == bp_fast_tracepoint)
13470 fprintf_unfiltered (fp, "ftrace");
13471 else if (self->type == bp_static_tracepoint)
13472 fprintf_unfiltered (fp, "strace");
13473 else if (self->type == bp_tracepoint)
13474 fprintf_unfiltered (fp, "trace");
13475 else
13476 internal_error (__FILE__, __LINE__,
13477 _("unhandled tracepoint type %d"), (int) self->type);
13478
13479 fprintf_unfiltered (fp, " %s",
13480 event_location_to_string (self->location.get ()));
13481 print_recreate_thread (self, fp);
13482
13483 if (tp->pass_count)
13484 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
13485 }
13486
13487 static void
13488 tracepoint_create_sals_from_location (const struct event_location *location,
13489 struct linespec_result *canonical,
13490 enum bptype type_wanted)
13491 {
13492 create_sals_from_location_default (location, canonical, type_wanted);
13493 }
13494
13495 static void
13496 tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13497 struct linespec_result *canonical,
13498 char *cond_string,
13499 char *extra_string,
13500 enum bptype type_wanted,
13501 enum bpdisp disposition,
13502 int thread,
13503 int task, int ignore_count,
13504 const struct breakpoint_ops *ops,
13505 int from_tty, int enabled,
13506 int internal, unsigned flags)
13507 {
13508 create_breakpoints_sal_default (gdbarch, canonical,
13509 cond_string, extra_string,
13510 type_wanted,
13511 disposition, thread, task,
13512 ignore_count, ops, from_tty,
13513 enabled, internal, flags);
13514 }
13515
13516 static void
13517 tracepoint_decode_location (struct breakpoint *b,
13518 const struct event_location *location,
13519 struct program_space *search_pspace,
13520 struct symtabs_and_lines *sals)
13521 {
13522 decode_location_default (b, location, search_pspace, sals);
13523 }
13524
13525 struct breakpoint_ops tracepoint_breakpoint_ops;
13526
13527 /* The breakpoint_ops structure to be use on tracepoints placed in a
13528 static probe. */
13529
13530 static void
13531 tracepoint_probe_create_sals_from_location
13532 (const struct event_location *location,
13533 struct linespec_result *canonical,
13534 enum bptype type_wanted)
13535 {
13536 /* We use the same method for breakpoint on probes. */
13537 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
13538 }
13539
13540 static void
13541 tracepoint_probe_decode_location (struct breakpoint *b,
13542 const struct event_location *location,
13543 struct program_space *search_pspace,
13544 struct symtabs_and_lines *sals)
13545 {
13546 /* We use the same method for breakpoint on probes. */
13547 bkpt_probe_decode_location (b, location, search_pspace, sals);
13548 }
13549
13550 static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13551
13552 /* Dprintf breakpoint_ops methods. */
13553
13554 static void
13555 dprintf_re_set (struct breakpoint *b)
13556 {
13557 breakpoint_re_set_default (b);
13558
13559 /* extra_string should never be non-NULL for dprintf. */
13560 gdb_assert (b->extra_string != NULL);
13561
13562 /* 1 - connect to target 1, that can run breakpoint commands.
13563 2 - create a dprintf, which resolves fine.
13564 3 - disconnect from target 1
13565 4 - connect to target 2, that can NOT run breakpoint commands.
13566
13567 After steps #3/#4, you'll want the dprintf command list to
13568 be updated, because target 1 and 2 may well return different
13569 answers for target_can_run_breakpoint_commands().
13570 Given absence of finer grained resetting, we get to do
13571 it all the time. */
13572 if (b->extra_string != NULL)
13573 update_dprintf_command_list (b);
13574 }
13575
13576 /* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13577
13578 static void
13579 dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13580 {
13581 fprintf_unfiltered (fp, "dprintf %s,%s",
13582 event_location_to_string (tp->location.get ()),
13583 tp->extra_string);
13584 print_recreate_thread (tp, fp);
13585 }
13586
13587 /* Implement the "after_condition_true" breakpoint_ops method for
13588 dprintf.
13589
13590 dprintf's are implemented with regular commands in their command
13591 list, but we run the commands here instead of before presenting the
13592 stop to the user, as dprintf's don't actually cause a stop. This
13593 also makes it so that the commands of multiple dprintfs at the same
13594 address are all handled. */
13595
13596 static void
13597 dprintf_after_condition_true (struct bpstats *bs)
13598 {
13599 struct cleanup *old_chain;
13600 struct bpstats tmp_bs = { NULL };
13601 struct bpstats *tmp_bs_p = &tmp_bs;
13602
13603 /* dprintf's never cause a stop. This wasn't set in the
13604 check_status hook instead because that would make the dprintf's
13605 condition not be evaluated. */
13606 bs->stop = 0;
13607
13608 /* Run the command list here. Take ownership of it instead of
13609 copying. We never want these commands to run later in
13610 bpstat_do_actions, if a breakpoint that causes a stop happens to
13611 be set at same address as this dprintf, or even if running the
13612 commands here throws. */
13613 tmp_bs.commands = bs->commands;
13614 bs->commands = NULL;
13615 old_chain = make_cleanup_decref_counted_command_line (&tmp_bs.commands);
13616
13617 bpstat_do_actions_1 (&tmp_bs_p);
13618
13619 /* 'tmp_bs.commands' will usually be NULL by now, but
13620 bpstat_do_actions_1 may return early without processing the whole
13621 list. */
13622 do_cleanups (old_chain);
13623 }
13624
13625 /* The breakpoint_ops structure to be used on static tracepoints with
13626 markers (`-m'). */
13627
13628 static void
13629 strace_marker_create_sals_from_location (const struct event_location *location,
13630 struct linespec_result *canonical,
13631 enum bptype type_wanted)
13632 {
13633 struct linespec_sals lsal;
13634 const char *arg_start, *arg;
13635 char *str;
13636 struct cleanup *cleanup;
13637
13638 arg = arg_start = get_linespec_location (location);
13639 lsal.sals = decode_static_tracepoint_spec (&arg);
13640
13641 str = savestring (arg_start, arg - arg_start);
13642 cleanup = make_cleanup (xfree, str);
13643 canonical->location = new_linespec_location (&str);
13644 do_cleanups (cleanup);
13645
13646 lsal.canonical
13647 = xstrdup (event_location_to_string (canonical->location.get ()));
13648 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13649 }
13650
13651 static void
13652 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13653 struct linespec_result *canonical,
13654 char *cond_string,
13655 char *extra_string,
13656 enum bptype type_wanted,
13657 enum bpdisp disposition,
13658 int thread,
13659 int task, int ignore_count,
13660 const struct breakpoint_ops *ops,
13661 int from_tty, int enabled,
13662 int internal, unsigned flags)
13663 {
13664 int i;
13665 struct linespec_sals *lsal = VEC_index (linespec_sals,
13666 canonical->sals, 0);
13667
13668 /* If the user is creating a static tracepoint by marker id
13669 (strace -m MARKER_ID), then store the sals index, so that
13670 breakpoint_re_set can try to match up which of the newly
13671 found markers corresponds to this one, and, don't try to
13672 expand multiple locations for each sal, given than SALS
13673 already should contain all sals for MARKER_ID. */
13674
13675 for (i = 0; i < lsal->sals.nelts; ++i)
13676 {
13677 struct symtabs_and_lines expanded;
13678 struct tracepoint *tp;
13679 event_location_up location;
13680
13681 expanded.nelts = 1;
13682 expanded.sals = &lsal->sals.sals[i];
13683
13684 location = copy_event_location (canonical->location.get ());
13685
13686 tp = new tracepoint ();
13687 init_breakpoint_sal (&tp->base, gdbarch, expanded,
13688 std::move (location), NULL,
13689 cond_string, extra_string,
13690 type_wanted, disposition,
13691 thread, task, ignore_count, ops,
13692 from_tty, enabled, internal, flags,
13693 canonical->special_display);
13694 /* Given that its possible to have multiple markers with
13695 the same string id, if the user is creating a static
13696 tracepoint by marker id ("strace -m MARKER_ID"), then
13697 store the sals index, so that breakpoint_re_set can
13698 try to match up which of the newly found markers
13699 corresponds to this one */
13700 tp->static_trace_marker_id_idx = i;
13701
13702 install_breakpoint (internal, &tp->base, 0);
13703 }
13704 }
13705
13706 static void
13707 strace_marker_decode_location (struct breakpoint *b,
13708 const struct event_location *location,
13709 struct program_space *search_pspace,
13710 struct symtabs_and_lines *sals)
13711 {
13712 struct tracepoint *tp = (struct tracepoint *) b;
13713 const char *s = get_linespec_location (location);
13714
13715 *sals = decode_static_tracepoint_spec (&s);
13716 if (sals->nelts > tp->static_trace_marker_id_idx)
13717 {
13718 sals->sals[0] = sals->sals[tp->static_trace_marker_id_idx];
13719 sals->nelts = 1;
13720 }
13721 else
13722 error (_("marker %s not found"), tp->static_trace_marker_id);
13723 }
13724
13725 static struct breakpoint_ops strace_marker_breakpoint_ops;
13726
13727 static int
13728 strace_marker_p (struct breakpoint *b)
13729 {
13730 return b->ops == &strace_marker_breakpoint_ops;
13731 }
13732
13733 /* Delete a breakpoint and clean up all traces of it in the data
13734 structures. */
13735
13736 void
13737 delete_breakpoint (struct breakpoint *bpt)
13738 {
13739 struct breakpoint *b;
13740
13741 gdb_assert (bpt != NULL);
13742
13743 /* Has this bp already been deleted? This can happen because
13744 multiple lists can hold pointers to bp's. bpstat lists are
13745 especial culprits.
13746
13747 One example of this happening is a watchpoint's scope bp. When
13748 the scope bp triggers, we notice that the watchpoint is out of
13749 scope, and delete it. We also delete its scope bp. But the
13750 scope bp is marked "auto-deleting", and is already on a bpstat.
13751 That bpstat is then checked for auto-deleting bp's, which are
13752 deleted.
13753
13754 A real solution to this problem might involve reference counts in
13755 bp's, and/or giving them pointers back to their referencing
13756 bpstat's, and teaching delete_breakpoint to only free a bp's
13757 storage when no more references were extent. A cheaper bandaid
13758 was chosen. */
13759 if (bpt->type == bp_none)
13760 return;
13761
13762 /* At least avoid this stale reference until the reference counting
13763 of breakpoints gets resolved. */
13764 if (bpt->related_breakpoint != bpt)
13765 {
13766 struct breakpoint *related;
13767 struct watchpoint *w;
13768
13769 if (bpt->type == bp_watchpoint_scope)
13770 w = (struct watchpoint *) bpt->related_breakpoint;
13771 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
13772 w = (struct watchpoint *) bpt;
13773 else
13774 w = NULL;
13775 if (w != NULL)
13776 watchpoint_del_at_next_stop (w);
13777
13778 /* Unlink bpt from the bpt->related_breakpoint ring. */
13779 for (related = bpt; related->related_breakpoint != bpt;
13780 related = related->related_breakpoint);
13781 related->related_breakpoint = bpt->related_breakpoint;
13782 bpt->related_breakpoint = bpt;
13783 }
13784
13785 /* watch_command_1 creates a watchpoint but only sets its number if
13786 update_watchpoint succeeds in creating its bp_locations. If there's
13787 a problem in that process, we'll be asked to delete the half-created
13788 watchpoint. In that case, don't announce the deletion. */
13789 if (bpt->number)
13790 observer_notify_breakpoint_deleted (bpt);
13791
13792 if (breakpoint_chain == bpt)
13793 breakpoint_chain = bpt->next;
13794
13795 ALL_BREAKPOINTS (b)
13796 if (b->next == bpt)
13797 {
13798 b->next = bpt->next;
13799 break;
13800 }
13801
13802 /* Be sure no bpstat's are pointing at the breakpoint after it's
13803 been freed. */
13804 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
13805 in all threads for now. Note that we cannot just remove bpstats
13806 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13807 commands are associated with the bpstat; if we remove it here,
13808 then the later call to bpstat_do_actions (&stop_bpstat); in
13809 event-top.c won't do anything, and temporary breakpoints with
13810 commands won't work. */
13811
13812 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13813
13814 /* Now that breakpoint is removed from breakpoint list, update the
13815 global location list. This will remove locations that used to
13816 belong to this breakpoint. Do this before freeing the breakpoint
13817 itself, since remove_breakpoint looks at location's owner. It
13818 might be better design to have location completely
13819 self-contained, but it's not the case now. */
13820 update_global_location_list (UGLL_DONT_INSERT);
13821
13822 bpt->ops->dtor (bpt);
13823 /* On the chance that someone will soon try again to delete this
13824 same bp, we mark it as deleted before freeing its storage. */
13825 bpt->type = bp_none;
13826 delete bpt;
13827 }
13828
13829 static void
13830 do_delete_breakpoint_cleanup (void *b)
13831 {
13832 delete_breakpoint ((struct breakpoint *) b);
13833 }
13834
13835 struct cleanup *
13836 make_cleanup_delete_breakpoint (struct breakpoint *b)
13837 {
13838 return make_cleanup (do_delete_breakpoint_cleanup, b);
13839 }
13840
13841 /* Iterator function to call a user-provided callback function once
13842 for each of B and its related breakpoints. */
13843
13844 static void
13845 iterate_over_related_breakpoints (struct breakpoint *b,
13846 void (*function) (struct breakpoint *,
13847 void *),
13848 void *data)
13849 {
13850 struct breakpoint *related;
13851
13852 related = b;
13853 do
13854 {
13855 struct breakpoint *next;
13856
13857 /* FUNCTION may delete RELATED. */
13858 next = related->related_breakpoint;
13859
13860 if (next == related)
13861 {
13862 /* RELATED is the last ring entry. */
13863 function (related, data);
13864
13865 /* FUNCTION may have deleted it, so we'd never reach back to
13866 B. There's nothing left to do anyway, so just break
13867 out. */
13868 break;
13869 }
13870 else
13871 function (related, data);
13872
13873 related = next;
13874 }
13875 while (related != b);
13876 }
13877
13878 static void
13879 do_delete_breakpoint (struct breakpoint *b, void *ignore)
13880 {
13881 delete_breakpoint (b);
13882 }
13883
13884 /* A callback for map_breakpoint_numbers that calls
13885 delete_breakpoint. */
13886
13887 static void
13888 do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
13889 {
13890 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
13891 }
13892
13893 void
13894 delete_command (char *arg, int from_tty)
13895 {
13896 struct breakpoint *b, *b_tmp;
13897
13898 dont_repeat ();
13899
13900 if (arg == 0)
13901 {
13902 int breaks_to_delete = 0;
13903
13904 /* Delete all breakpoints if no argument. Do not delete
13905 internal breakpoints, these have to be deleted with an
13906 explicit breakpoint number argument. */
13907 ALL_BREAKPOINTS (b)
13908 if (user_breakpoint_p (b))
13909 {
13910 breaks_to_delete = 1;
13911 break;
13912 }
13913
13914 /* Ask user only if there are some breakpoints to delete. */
13915 if (!from_tty
13916 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
13917 {
13918 ALL_BREAKPOINTS_SAFE (b, b_tmp)
13919 if (user_breakpoint_p (b))
13920 delete_breakpoint (b);
13921 }
13922 }
13923 else
13924 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
13925 }
13926
13927 /* Return true if all locations of B bound to PSPACE are pending. If
13928 PSPACE is NULL, all locations of all program spaces are
13929 considered. */
13930
13931 static int
13932 all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
13933 {
13934 struct bp_location *loc;
13935
13936 for (loc = b->loc; loc != NULL; loc = loc->next)
13937 if ((pspace == NULL
13938 || loc->pspace == pspace)
13939 && !loc->shlib_disabled
13940 && !loc->pspace->executing_startup)
13941 return 0;
13942 return 1;
13943 }
13944
13945 /* Subroutine of update_breakpoint_locations to simplify it.
13946 Return non-zero if multiple fns in list LOC have the same name.
13947 Null names are ignored. */
13948
13949 static int
13950 ambiguous_names_p (struct bp_location *loc)
13951 {
13952 struct bp_location *l;
13953 htab_t htab = htab_create_alloc (13, htab_hash_string,
13954 (int (*) (const void *,
13955 const void *)) streq,
13956 NULL, xcalloc, xfree);
13957
13958 for (l = loc; l != NULL; l = l->next)
13959 {
13960 const char **slot;
13961 const char *name = l->function_name;
13962
13963 /* Allow for some names to be NULL, ignore them. */
13964 if (name == NULL)
13965 continue;
13966
13967 slot = (const char **) htab_find_slot (htab, (const void *) name,
13968 INSERT);
13969 /* NOTE: We can assume slot != NULL here because xcalloc never
13970 returns NULL. */
13971 if (*slot != NULL)
13972 {
13973 htab_delete (htab);
13974 return 1;
13975 }
13976 *slot = name;
13977 }
13978
13979 htab_delete (htab);
13980 return 0;
13981 }
13982
13983 /* When symbols change, it probably means the sources changed as well,
13984 and it might mean the static tracepoint markers are no longer at
13985 the same address or line numbers they used to be at last we
13986 checked. Losing your static tracepoints whenever you rebuild is
13987 undesirable. This function tries to resync/rematch gdb static
13988 tracepoints with the markers on the target, for static tracepoints
13989 that have not been set by marker id. Static tracepoint that have
13990 been set by marker id are reset by marker id in breakpoint_re_set.
13991 The heuristic is:
13992
13993 1) For a tracepoint set at a specific address, look for a marker at
13994 the old PC. If one is found there, assume to be the same marker.
13995 If the name / string id of the marker found is different from the
13996 previous known name, assume that means the user renamed the marker
13997 in the sources, and output a warning.
13998
13999 2) For a tracepoint set at a given line number, look for a marker
14000 at the new address of the old line number. If one is found there,
14001 assume to be the same marker. If the name / string id of the
14002 marker found is different from the previous known name, assume that
14003 means the user renamed the marker in the sources, and output a
14004 warning.
14005
14006 3) If a marker is no longer found at the same address or line, it
14007 may mean the marker no longer exists. But it may also just mean
14008 the code changed a bit. Maybe the user added a few lines of code
14009 that made the marker move up or down (in line number terms). Ask
14010 the target for info about the marker with the string id as we knew
14011 it. If found, update line number and address in the matching
14012 static tracepoint. This will get confused if there's more than one
14013 marker with the same ID (possible in UST, although unadvised
14014 precisely because it confuses tools). */
14015
14016 static struct symtab_and_line
14017 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
14018 {
14019 struct tracepoint *tp = (struct tracepoint *) b;
14020 struct static_tracepoint_marker marker;
14021 CORE_ADDR pc;
14022
14023 pc = sal.pc;
14024 if (sal.line)
14025 find_line_pc (sal.symtab, sal.line, &pc);
14026
14027 if (target_static_tracepoint_marker_at (pc, &marker))
14028 {
14029 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
14030 warning (_("static tracepoint %d changed probed marker from %s to %s"),
14031 b->number,
14032 tp->static_trace_marker_id, marker.str_id);
14033
14034 xfree (tp->static_trace_marker_id);
14035 tp->static_trace_marker_id = xstrdup (marker.str_id);
14036 release_static_tracepoint_marker (&marker);
14037
14038 return sal;
14039 }
14040
14041 /* Old marker wasn't found on target at lineno. Try looking it up
14042 by string ID. */
14043 if (!sal.explicit_pc
14044 && sal.line != 0
14045 && sal.symtab != NULL
14046 && tp->static_trace_marker_id != NULL)
14047 {
14048 VEC(static_tracepoint_marker_p) *markers;
14049
14050 markers
14051 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
14052
14053 if (!VEC_empty(static_tracepoint_marker_p, markers))
14054 {
14055 struct symtab_and_line sal2;
14056 struct symbol *sym;
14057 struct static_tracepoint_marker *tpmarker;
14058 struct ui_out *uiout = current_uiout;
14059 struct explicit_location explicit_loc;
14060
14061 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
14062
14063 xfree (tp->static_trace_marker_id);
14064 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
14065
14066 warning (_("marker for static tracepoint %d (%s) not "
14067 "found at previous line number"),
14068 b->number, tp->static_trace_marker_id);
14069
14070 init_sal (&sal2);
14071
14072 sal2.pc = tpmarker->address;
14073
14074 sal2 = find_pc_line (tpmarker->address, 0);
14075 sym = find_pc_sect_function (tpmarker->address, NULL);
14076 uiout->text ("Now in ");
14077 if (sym)
14078 {
14079 uiout->field_string ("func", SYMBOL_PRINT_NAME (sym));
14080 uiout->text (" at ");
14081 }
14082 uiout->field_string ("file",
14083 symtab_to_filename_for_display (sal2.symtab));
14084 uiout->text (":");
14085
14086 if (uiout->is_mi_like_p ())
14087 {
14088 const char *fullname = symtab_to_fullname (sal2.symtab);
14089
14090 uiout->field_string ("fullname", fullname);
14091 }
14092
14093 uiout->field_int ("line", sal2.line);
14094 uiout->text ("\n");
14095
14096 b->loc->line_number = sal2.line;
14097 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
14098
14099 b->location.reset (NULL);
14100 initialize_explicit_location (&explicit_loc);
14101 explicit_loc.source_filename
14102 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
14103 explicit_loc.line_offset.offset = b->loc->line_number;
14104 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
14105 b->location = new_explicit_location (&explicit_loc);
14106
14107 /* Might be nice to check if function changed, and warn if
14108 so. */
14109
14110 release_static_tracepoint_marker (tpmarker);
14111 }
14112 }
14113 return sal;
14114 }
14115
14116 /* Returns 1 iff locations A and B are sufficiently same that
14117 we don't need to report breakpoint as changed. */
14118
14119 static int
14120 locations_are_equal (struct bp_location *a, struct bp_location *b)
14121 {
14122 while (a && b)
14123 {
14124 if (a->address != b->address)
14125 return 0;
14126
14127 if (a->shlib_disabled != b->shlib_disabled)
14128 return 0;
14129
14130 if (a->enabled != b->enabled)
14131 return 0;
14132
14133 a = a->next;
14134 b = b->next;
14135 }
14136
14137 if ((a == NULL) != (b == NULL))
14138 return 0;
14139
14140 return 1;
14141 }
14142
14143 /* Split all locations of B that are bound to PSPACE out of B's
14144 location list to a separate list and return that list's head. If
14145 PSPACE is NULL, hoist out all locations of B. */
14146
14147 static struct bp_location *
14148 hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
14149 {
14150 struct bp_location head;
14151 struct bp_location *i = b->loc;
14152 struct bp_location **i_link = &b->loc;
14153 struct bp_location *hoisted = &head;
14154
14155 if (pspace == NULL)
14156 {
14157 i = b->loc;
14158 b->loc = NULL;
14159 return i;
14160 }
14161
14162 head.next = NULL;
14163
14164 while (i != NULL)
14165 {
14166 if (i->pspace == pspace)
14167 {
14168 *i_link = i->next;
14169 i->next = NULL;
14170 hoisted->next = i;
14171 hoisted = i;
14172 }
14173 else
14174 i_link = &i->next;
14175 i = *i_link;
14176 }
14177
14178 return head.next;
14179 }
14180
14181 /* Create new breakpoint locations for B (a hardware or software
14182 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
14183 zero, then B is a ranged breakpoint. Only recreates locations for
14184 FILTER_PSPACE. Locations of other program spaces are left
14185 untouched. */
14186
14187 void
14188 update_breakpoint_locations (struct breakpoint *b,
14189 struct program_space *filter_pspace,
14190 struct symtabs_and_lines sals,
14191 struct symtabs_and_lines sals_end)
14192 {
14193 int i;
14194 struct bp_location *existing_locations;
14195
14196 if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
14197 {
14198 /* Ranged breakpoints have only one start location and one end
14199 location. */
14200 b->enable_state = bp_disabled;
14201 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
14202 "multiple locations found\n"),
14203 b->number);
14204 return;
14205 }
14206
14207 /* If there's no new locations, and all existing locations are
14208 pending, don't do anything. This optimizes the common case where
14209 all locations are in the same shared library, that was unloaded.
14210 We'd like to retain the location, so that when the library is
14211 loaded again, we don't loose the enabled/disabled status of the
14212 individual locations. */
14213 if (all_locations_are_pending (b, filter_pspace) && sals.nelts == 0)
14214 return;
14215
14216 existing_locations = hoist_existing_locations (b, filter_pspace);
14217
14218 for (i = 0; i < sals.nelts; ++i)
14219 {
14220 struct bp_location *new_loc;
14221
14222 switch_to_program_space_and_thread (sals.sals[i].pspace);
14223
14224 new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
14225
14226 /* Reparse conditions, they might contain references to the
14227 old symtab. */
14228 if (b->cond_string != NULL)
14229 {
14230 const char *s;
14231
14232 s = b->cond_string;
14233 TRY
14234 {
14235 new_loc->cond = parse_exp_1 (&s, sals.sals[i].pc,
14236 block_for_pc (sals.sals[i].pc),
14237 0);
14238 }
14239 CATCH (e, RETURN_MASK_ERROR)
14240 {
14241 warning (_("failed to reevaluate condition "
14242 "for breakpoint %d: %s"),
14243 b->number, e.message);
14244 new_loc->enabled = 0;
14245 }
14246 END_CATCH
14247 }
14248
14249 if (sals_end.nelts)
14250 {
14251 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
14252
14253 new_loc->length = end - sals.sals[0].pc + 1;
14254 }
14255 }
14256
14257 /* If possible, carry over 'disable' status from existing
14258 breakpoints. */
14259 {
14260 struct bp_location *e = existing_locations;
14261 /* If there are multiple breakpoints with the same function name,
14262 e.g. for inline functions, comparing function names won't work.
14263 Instead compare pc addresses; this is just a heuristic as things
14264 may have moved, but in practice it gives the correct answer
14265 often enough until a better solution is found. */
14266 int have_ambiguous_names = ambiguous_names_p (b->loc);
14267
14268 for (; e; e = e->next)
14269 {
14270 if (!e->enabled && e->function_name)
14271 {
14272 struct bp_location *l = b->loc;
14273 if (have_ambiguous_names)
14274 {
14275 for (; l; l = l->next)
14276 if (breakpoint_locations_match (e, l))
14277 {
14278 l->enabled = 0;
14279 break;
14280 }
14281 }
14282 else
14283 {
14284 for (; l; l = l->next)
14285 if (l->function_name
14286 && strcmp (e->function_name, l->function_name) == 0)
14287 {
14288 l->enabled = 0;
14289 break;
14290 }
14291 }
14292 }
14293 }
14294 }
14295
14296 if (!locations_are_equal (existing_locations, b->loc))
14297 observer_notify_breakpoint_modified (b);
14298 }
14299
14300 /* Find the SaL locations corresponding to the given LOCATION.
14301 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
14302
14303 static struct symtabs_and_lines
14304 location_to_sals (struct breakpoint *b, struct event_location *location,
14305 struct program_space *search_pspace, int *found)
14306 {
14307 struct symtabs_and_lines sals = {0};
14308 struct gdb_exception exception = exception_none;
14309
14310 gdb_assert (b->ops != NULL);
14311
14312 TRY
14313 {
14314 b->ops->decode_location (b, location, search_pspace, &sals);
14315 }
14316 CATCH (e, RETURN_MASK_ERROR)
14317 {
14318 int not_found_and_ok = 0;
14319
14320 exception = e;
14321
14322 /* For pending breakpoints, it's expected that parsing will
14323 fail until the right shared library is loaded. User has
14324 already told to create pending breakpoints and don't need
14325 extra messages. If breakpoint is in bp_shlib_disabled
14326 state, then user already saw the message about that
14327 breakpoint being disabled, and don't want to see more
14328 errors. */
14329 if (e.error == NOT_FOUND_ERROR
14330 && (b->condition_not_parsed
14331 || (b->loc != NULL
14332 && search_pspace != NULL
14333 && b->loc->pspace != search_pspace)
14334 || (b->loc && b->loc->shlib_disabled)
14335 || (b->loc && b->loc->pspace->executing_startup)
14336 || b->enable_state == bp_disabled))
14337 not_found_and_ok = 1;
14338
14339 if (!not_found_and_ok)
14340 {
14341 /* We surely don't want to warn about the same breakpoint
14342 10 times. One solution, implemented here, is disable
14343 the breakpoint on error. Another solution would be to
14344 have separate 'warning emitted' flag. Since this
14345 happens only when a binary has changed, I don't know
14346 which approach is better. */
14347 b->enable_state = bp_disabled;
14348 throw_exception (e);
14349 }
14350 }
14351 END_CATCH
14352
14353 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
14354 {
14355 int i;
14356
14357 for (i = 0; i < sals.nelts; ++i)
14358 resolve_sal_pc (&sals.sals[i]);
14359 if (b->condition_not_parsed && b->extra_string != NULL)
14360 {
14361 char *cond_string, *extra_string;
14362 int thread, task;
14363
14364 find_condition_and_thread (b->extra_string, sals.sals[0].pc,
14365 &cond_string, &thread, &task,
14366 &extra_string);
14367 gdb_assert (b->cond_string == NULL);
14368 if (cond_string)
14369 b->cond_string = cond_string;
14370 b->thread = thread;
14371 b->task = task;
14372 if (extra_string)
14373 {
14374 xfree (b->extra_string);
14375 b->extra_string = extra_string;
14376 }
14377 b->condition_not_parsed = 0;
14378 }
14379
14380 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
14381 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
14382
14383 *found = 1;
14384 }
14385 else
14386 *found = 0;
14387
14388 return sals;
14389 }
14390
14391 /* The default re_set method, for typical hardware or software
14392 breakpoints. Reevaluate the breakpoint and recreate its
14393 locations. */
14394
14395 static void
14396 breakpoint_re_set_default (struct breakpoint *b)
14397 {
14398 int found;
14399 struct symtabs_and_lines sals, sals_end;
14400 struct symtabs_and_lines expanded = {0};
14401 struct symtabs_and_lines expanded_end = {0};
14402 struct program_space *filter_pspace = current_program_space;
14403
14404 sals = location_to_sals (b, b->location.get (), filter_pspace, &found);
14405 if (found)
14406 {
14407 make_cleanup (xfree, sals.sals);
14408 expanded = sals;
14409 }
14410
14411 if (b->location_range_end != NULL)
14412 {
14413 sals_end = location_to_sals (b, b->location_range_end.get (),
14414 filter_pspace, &found);
14415 if (found)
14416 {
14417 make_cleanup (xfree, sals_end.sals);
14418 expanded_end = sals_end;
14419 }
14420 }
14421
14422 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
14423 }
14424
14425 /* Default method for creating SALs from an address string. It basically
14426 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
14427
14428 static void
14429 create_sals_from_location_default (const struct event_location *location,
14430 struct linespec_result *canonical,
14431 enum bptype type_wanted)
14432 {
14433 parse_breakpoint_sals (location, canonical);
14434 }
14435
14436 /* Call create_breakpoints_sal for the given arguments. This is the default
14437 function for the `create_breakpoints_sal' method of
14438 breakpoint_ops. */
14439
14440 static void
14441 create_breakpoints_sal_default (struct gdbarch *gdbarch,
14442 struct linespec_result *canonical,
14443 char *cond_string,
14444 char *extra_string,
14445 enum bptype type_wanted,
14446 enum bpdisp disposition,
14447 int thread,
14448 int task, int ignore_count,
14449 const struct breakpoint_ops *ops,
14450 int from_tty, int enabled,
14451 int internal, unsigned flags)
14452 {
14453 create_breakpoints_sal (gdbarch, canonical, cond_string,
14454 extra_string,
14455 type_wanted, disposition,
14456 thread, task, ignore_count, ops, from_tty,
14457 enabled, internal, flags);
14458 }
14459
14460 /* Decode the line represented by S by calling decode_line_full. This is the
14461 default function for the `decode_location' method of breakpoint_ops. */
14462
14463 static void
14464 decode_location_default (struct breakpoint *b,
14465 const struct event_location *location,
14466 struct program_space *search_pspace,
14467 struct symtabs_and_lines *sals)
14468 {
14469 struct linespec_result canonical;
14470
14471 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
14472 (struct symtab *) NULL, 0,
14473 &canonical, multiple_symbols_all,
14474 b->filter);
14475
14476 /* We should get 0 or 1 resulting SALs. */
14477 gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
14478
14479 if (VEC_length (linespec_sals, canonical.sals) > 0)
14480 {
14481 struct linespec_sals *lsal;
14482
14483 lsal = VEC_index (linespec_sals, canonical.sals, 0);
14484 *sals = lsal->sals;
14485 /* Arrange it so the destructor does not free the
14486 contents. */
14487 lsal->sals.sals = NULL;
14488 }
14489 }
14490
14491 /* Prepare the global context for a re-set of breakpoint B. */
14492
14493 static struct cleanup *
14494 prepare_re_set_context (struct breakpoint *b)
14495 {
14496 input_radix = b->input_radix;
14497 set_language (b->language);
14498
14499 return make_cleanup (null_cleanup, NULL);
14500 }
14501
14502 /* Reset a breakpoint given it's struct breakpoint * BINT.
14503 The value we return ends up being the return value from catch_errors.
14504 Unused in this case. */
14505
14506 static int
14507 breakpoint_re_set_one (void *bint)
14508 {
14509 /* Get past catch_errs. */
14510 struct breakpoint *b = (struct breakpoint *) bint;
14511 struct cleanup *cleanups;
14512
14513 cleanups = prepare_re_set_context (b);
14514 b->ops->re_set (b);
14515 do_cleanups (cleanups);
14516 return 0;
14517 }
14518
14519 /* Re-set breakpoint locations for the current program space.
14520 Locations bound to other program spaces are left untouched. */
14521
14522 void
14523 breakpoint_re_set (void)
14524 {
14525 struct breakpoint *b, *b_tmp;
14526 enum language save_language;
14527 int save_input_radix;
14528 struct cleanup *old_chain;
14529
14530 save_language = current_language->la_language;
14531 save_input_radix = input_radix;
14532 old_chain = save_current_space_and_thread ();
14533
14534 /* Note: we must not try to insert locations until after all
14535 breakpoints have been re-set. Otherwise, e.g., when re-setting
14536 breakpoint 1, we'd insert the locations of breakpoint 2, which
14537 hadn't been re-set yet, and thus may have stale locations. */
14538
14539 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14540 {
14541 /* Format possible error msg. */
14542 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
14543 b->number);
14544 struct cleanup *cleanups = make_cleanup (xfree, message);
14545 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
14546 do_cleanups (cleanups);
14547 }
14548 set_language (save_language);
14549 input_radix = save_input_radix;
14550
14551 jit_breakpoint_re_set ();
14552
14553 do_cleanups (old_chain);
14554
14555 create_overlay_event_breakpoint ();
14556 create_longjmp_master_breakpoint ();
14557 create_std_terminate_master_breakpoint ();
14558 create_exception_master_breakpoint ();
14559
14560 /* Now we can insert. */
14561 update_global_location_list (UGLL_MAY_INSERT);
14562 }
14563 \f
14564 /* Reset the thread number of this breakpoint:
14565
14566 - If the breakpoint is for all threads, leave it as-is.
14567 - Else, reset it to the current thread for inferior_ptid. */
14568 void
14569 breakpoint_re_set_thread (struct breakpoint *b)
14570 {
14571 if (b->thread != -1)
14572 {
14573 if (in_thread_list (inferior_ptid))
14574 b->thread = ptid_to_global_thread_id (inferior_ptid);
14575
14576 /* We're being called after following a fork. The new fork is
14577 selected as current, and unless this was a vfork will have a
14578 different program space from the original thread. Reset that
14579 as well. */
14580 b->loc->pspace = current_program_space;
14581 }
14582 }
14583
14584 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14585 If from_tty is nonzero, it prints a message to that effect,
14586 which ends with a period (no newline). */
14587
14588 void
14589 set_ignore_count (int bptnum, int count, int from_tty)
14590 {
14591 struct breakpoint *b;
14592
14593 if (count < 0)
14594 count = 0;
14595
14596 ALL_BREAKPOINTS (b)
14597 if (b->number == bptnum)
14598 {
14599 if (is_tracepoint (b))
14600 {
14601 if (from_tty && count != 0)
14602 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14603 bptnum);
14604 return;
14605 }
14606
14607 b->ignore_count = count;
14608 if (from_tty)
14609 {
14610 if (count == 0)
14611 printf_filtered (_("Will stop next time "
14612 "breakpoint %d is reached."),
14613 bptnum);
14614 else if (count == 1)
14615 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
14616 bptnum);
14617 else
14618 printf_filtered (_("Will ignore next %d "
14619 "crossings of breakpoint %d."),
14620 count, bptnum);
14621 }
14622 observer_notify_breakpoint_modified (b);
14623 return;
14624 }
14625
14626 error (_("No breakpoint number %d."), bptnum);
14627 }
14628
14629 /* Command to set ignore-count of breakpoint N to COUNT. */
14630
14631 static void
14632 ignore_command (char *args, int from_tty)
14633 {
14634 char *p = args;
14635 int num;
14636
14637 if (p == 0)
14638 error_no_arg (_("a breakpoint number"));
14639
14640 num = get_number (&p);
14641 if (num == 0)
14642 error (_("bad breakpoint number: '%s'"), args);
14643 if (*p == 0)
14644 error (_("Second argument (specified ignore-count) is missing."));
14645
14646 set_ignore_count (num,
14647 longest_to_int (value_as_long (parse_and_eval (p))),
14648 from_tty);
14649 if (from_tty)
14650 printf_filtered ("\n");
14651 }
14652 \f
14653 /* Call FUNCTION on each of the breakpoints
14654 whose numbers are given in ARGS. */
14655
14656 static void
14657 map_breakpoint_numbers (const char *args,
14658 void (*function) (struct breakpoint *,
14659 void *),
14660 void *data)
14661 {
14662 int num;
14663 struct breakpoint *b, *tmp;
14664
14665 if (args == 0 || *args == '\0')
14666 error_no_arg (_("one or more breakpoint numbers"));
14667
14668 number_or_range_parser parser (args);
14669
14670 while (!parser.finished ())
14671 {
14672 const char *p = parser.cur_tok ();
14673 bool match = false;
14674
14675 num = parser.get_number ();
14676 if (num == 0)
14677 {
14678 warning (_("bad breakpoint number at or near '%s'"), p);
14679 }
14680 else
14681 {
14682 ALL_BREAKPOINTS_SAFE (b, tmp)
14683 if (b->number == num)
14684 {
14685 match = true;
14686 function (b, data);
14687 break;
14688 }
14689 if (!match)
14690 printf_unfiltered (_("No breakpoint number %d.\n"), num);
14691 }
14692 }
14693 }
14694
14695 static struct bp_location *
14696 find_location_by_number (char *number)
14697 {
14698 char *dot = strchr (number, '.');
14699 char *p1;
14700 int bp_num;
14701 int loc_num;
14702 struct breakpoint *b;
14703 struct bp_location *loc;
14704
14705 *dot = '\0';
14706
14707 p1 = number;
14708 bp_num = get_number (&p1);
14709 if (bp_num == 0)
14710 error (_("Bad breakpoint number '%s'"), number);
14711
14712 ALL_BREAKPOINTS (b)
14713 if (b->number == bp_num)
14714 {
14715 break;
14716 }
14717
14718 if (!b || b->number != bp_num)
14719 error (_("Bad breakpoint number '%s'"), number);
14720
14721 p1 = dot+1;
14722 loc_num = get_number (&p1);
14723 if (loc_num == 0)
14724 error (_("Bad breakpoint location number '%s'"), number);
14725
14726 --loc_num;
14727 loc = b->loc;
14728 for (;loc_num && loc; --loc_num, loc = loc->next)
14729 ;
14730 if (!loc)
14731 error (_("Bad breakpoint location number '%s'"), dot+1);
14732
14733 return loc;
14734 }
14735
14736
14737 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14738 If from_tty is nonzero, it prints a message to that effect,
14739 which ends with a period (no newline). */
14740
14741 void
14742 disable_breakpoint (struct breakpoint *bpt)
14743 {
14744 /* Never disable a watchpoint scope breakpoint; we want to
14745 hit them when we leave scope so we can delete both the
14746 watchpoint and its scope breakpoint at that time. */
14747 if (bpt->type == bp_watchpoint_scope)
14748 return;
14749
14750 bpt->enable_state = bp_disabled;
14751
14752 /* Mark breakpoint locations modified. */
14753 mark_breakpoint_modified (bpt);
14754
14755 if (target_supports_enable_disable_tracepoint ()
14756 && current_trace_status ()->running && is_tracepoint (bpt))
14757 {
14758 struct bp_location *location;
14759
14760 for (location = bpt->loc; location; location = location->next)
14761 target_disable_tracepoint (location);
14762 }
14763
14764 update_global_location_list (UGLL_DONT_INSERT);
14765
14766 observer_notify_breakpoint_modified (bpt);
14767 }
14768
14769 /* A callback for iterate_over_related_breakpoints. */
14770
14771 static void
14772 do_disable_breakpoint (struct breakpoint *b, void *ignore)
14773 {
14774 disable_breakpoint (b);
14775 }
14776
14777 /* A callback for map_breakpoint_numbers that calls
14778 disable_breakpoint. */
14779
14780 static void
14781 do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
14782 {
14783 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
14784 }
14785
14786 static void
14787 disable_command (char *args, int from_tty)
14788 {
14789 if (args == 0)
14790 {
14791 struct breakpoint *bpt;
14792
14793 ALL_BREAKPOINTS (bpt)
14794 if (user_breakpoint_p (bpt))
14795 disable_breakpoint (bpt);
14796 }
14797 else
14798 {
14799 char *num = extract_arg (&args);
14800
14801 while (num)
14802 {
14803 if (strchr (num, '.'))
14804 {
14805 struct bp_location *loc = find_location_by_number (num);
14806
14807 if (loc)
14808 {
14809 if (loc->enabled)
14810 {
14811 loc->enabled = 0;
14812 mark_breakpoint_location_modified (loc);
14813 }
14814 if (target_supports_enable_disable_tracepoint ()
14815 && current_trace_status ()->running && loc->owner
14816 && is_tracepoint (loc->owner))
14817 target_disable_tracepoint (loc);
14818 }
14819 update_global_location_list (UGLL_DONT_INSERT);
14820 }
14821 else
14822 map_breakpoint_numbers (num, do_map_disable_breakpoint, NULL);
14823 num = extract_arg (&args);
14824 }
14825 }
14826 }
14827
14828 static void
14829 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14830 int count)
14831 {
14832 int target_resources_ok;
14833
14834 if (bpt->type == bp_hardware_breakpoint)
14835 {
14836 int i;
14837 i = hw_breakpoint_used_count ();
14838 target_resources_ok =
14839 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
14840 i + 1, 0);
14841 if (target_resources_ok == 0)
14842 error (_("No hardware breakpoint support in the target."));
14843 else if (target_resources_ok < 0)
14844 error (_("Hardware breakpoints used exceeds limit."));
14845 }
14846
14847 if (is_watchpoint (bpt))
14848 {
14849 /* Initialize it just to avoid a GCC false warning. */
14850 enum enable_state orig_enable_state = bp_disabled;
14851
14852 TRY
14853 {
14854 struct watchpoint *w = (struct watchpoint *) bpt;
14855
14856 orig_enable_state = bpt->enable_state;
14857 bpt->enable_state = bp_enabled;
14858 update_watchpoint (w, 1 /* reparse */);
14859 }
14860 CATCH (e, RETURN_MASK_ALL)
14861 {
14862 bpt->enable_state = orig_enable_state;
14863 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14864 bpt->number);
14865 return;
14866 }
14867 END_CATCH
14868 }
14869
14870 bpt->enable_state = bp_enabled;
14871
14872 /* Mark breakpoint locations modified. */
14873 mark_breakpoint_modified (bpt);
14874
14875 if (target_supports_enable_disable_tracepoint ()
14876 && current_trace_status ()->running && is_tracepoint (bpt))
14877 {
14878 struct bp_location *location;
14879
14880 for (location = bpt->loc; location; location = location->next)
14881 target_enable_tracepoint (location);
14882 }
14883
14884 bpt->disposition = disposition;
14885 bpt->enable_count = count;
14886 update_global_location_list (UGLL_MAY_INSERT);
14887
14888 observer_notify_breakpoint_modified (bpt);
14889 }
14890
14891
14892 void
14893 enable_breakpoint (struct breakpoint *bpt)
14894 {
14895 enable_breakpoint_disp (bpt, bpt->disposition, 0);
14896 }
14897
14898 static void
14899 do_enable_breakpoint (struct breakpoint *bpt, void *arg)
14900 {
14901 enable_breakpoint (bpt);
14902 }
14903
14904 /* A callback for map_breakpoint_numbers that calls
14905 enable_breakpoint. */
14906
14907 static void
14908 do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
14909 {
14910 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
14911 }
14912
14913 /* The enable command enables the specified breakpoints (or all defined
14914 breakpoints) so they once again become (or continue to be) effective
14915 in stopping the inferior. */
14916
14917 static void
14918 enable_command (char *args, int from_tty)
14919 {
14920 if (args == 0)
14921 {
14922 struct breakpoint *bpt;
14923
14924 ALL_BREAKPOINTS (bpt)
14925 if (user_breakpoint_p (bpt))
14926 enable_breakpoint (bpt);
14927 }
14928 else
14929 {
14930 char *num = extract_arg (&args);
14931
14932 while (num)
14933 {
14934 if (strchr (num, '.'))
14935 {
14936 struct bp_location *loc = find_location_by_number (num);
14937
14938 if (loc)
14939 {
14940 if (!loc->enabled)
14941 {
14942 loc->enabled = 1;
14943 mark_breakpoint_location_modified (loc);
14944 }
14945 if (target_supports_enable_disable_tracepoint ()
14946 && current_trace_status ()->running && loc->owner
14947 && is_tracepoint (loc->owner))
14948 target_enable_tracepoint (loc);
14949 }
14950 update_global_location_list (UGLL_MAY_INSERT);
14951 }
14952 else
14953 map_breakpoint_numbers (num, do_map_enable_breakpoint, NULL);
14954 num = extract_arg (&args);
14955 }
14956 }
14957 }
14958
14959 /* This struct packages up disposition data for application to multiple
14960 breakpoints. */
14961
14962 struct disp_data
14963 {
14964 enum bpdisp disp;
14965 int count;
14966 };
14967
14968 static void
14969 do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
14970 {
14971 struct disp_data disp_data = *(struct disp_data *) arg;
14972
14973 enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
14974 }
14975
14976 static void
14977 do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
14978 {
14979 struct disp_data disp = { disp_disable, 1 };
14980
14981 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14982 }
14983
14984 static void
14985 enable_once_command (char *args, int from_tty)
14986 {
14987 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
14988 }
14989
14990 static void
14991 do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
14992 {
14993 struct disp_data disp = { disp_disable, *(int *) countptr };
14994
14995 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14996 }
14997
14998 static void
14999 enable_count_command (char *args, int from_tty)
15000 {
15001 int count;
15002
15003 if (args == NULL)
15004 error_no_arg (_("hit count"));
15005
15006 count = get_number (&args);
15007
15008 map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
15009 }
15010
15011 static void
15012 do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
15013 {
15014 struct disp_data disp = { disp_del, 1 };
15015
15016 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
15017 }
15018
15019 static void
15020 enable_delete_command (char *args, int from_tty)
15021 {
15022 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
15023 }
15024 \f
15025 static void
15026 set_breakpoint_cmd (char *args, int from_tty)
15027 {
15028 }
15029
15030 static void
15031 show_breakpoint_cmd (char *args, int from_tty)
15032 {
15033 }
15034
15035 /* Invalidate last known value of any hardware watchpoint if
15036 the memory which that value represents has been written to by
15037 GDB itself. */
15038
15039 static void
15040 invalidate_bp_value_on_memory_change (struct inferior *inferior,
15041 CORE_ADDR addr, ssize_t len,
15042 const bfd_byte *data)
15043 {
15044 struct breakpoint *bp;
15045
15046 ALL_BREAKPOINTS (bp)
15047 if (bp->enable_state == bp_enabled
15048 && bp->type == bp_hardware_watchpoint)
15049 {
15050 struct watchpoint *wp = (struct watchpoint *) bp;
15051
15052 if (wp->val_valid && wp->val)
15053 {
15054 struct bp_location *loc;
15055
15056 for (loc = bp->loc; loc != NULL; loc = loc->next)
15057 if (loc->loc_type == bp_loc_hardware_watchpoint
15058 && loc->address + loc->length > addr
15059 && addr + len > loc->address)
15060 {
15061 value_free (wp->val);
15062 wp->val = NULL;
15063 wp->val_valid = 0;
15064 }
15065 }
15066 }
15067 }
15068
15069 /* Create and insert a breakpoint for software single step. */
15070
15071 void
15072 insert_single_step_breakpoint (struct gdbarch *gdbarch,
15073 struct address_space *aspace,
15074 CORE_ADDR next_pc)
15075 {
15076 struct thread_info *tp = inferior_thread ();
15077 struct symtab_and_line sal;
15078 CORE_ADDR pc = next_pc;
15079
15080 if (tp->control.single_step_breakpoints == NULL)
15081 {
15082 tp->control.single_step_breakpoints
15083 = new_single_step_breakpoint (tp->global_num, gdbarch);
15084 }
15085
15086 sal = find_pc_line (pc, 0);
15087 sal.pc = pc;
15088 sal.section = find_pc_overlay (pc);
15089 sal.explicit_pc = 1;
15090 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
15091
15092 update_global_location_list (UGLL_INSERT);
15093 }
15094
15095 /* Insert single step breakpoints according to the current state. */
15096
15097 int
15098 insert_single_step_breakpoints (struct gdbarch *gdbarch)
15099 {
15100 struct regcache *regcache = get_current_regcache ();
15101 VEC (CORE_ADDR) * next_pcs;
15102
15103 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
15104
15105 if (next_pcs != NULL)
15106 {
15107 int i;
15108 CORE_ADDR pc;
15109 struct frame_info *frame = get_current_frame ();
15110 struct address_space *aspace = get_frame_address_space (frame);
15111
15112 for (i = 0; VEC_iterate (CORE_ADDR, next_pcs, i, pc); i++)
15113 insert_single_step_breakpoint (gdbarch, aspace, pc);
15114
15115 VEC_free (CORE_ADDR, next_pcs);
15116
15117 return 1;
15118 }
15119 else
15120 return 0;
15121 }
15122
15123 /* See breakpoint.h. */
15124
15125 int
15126 breakpoint_has_location_inserted_here (struct breakpoint *bp,
15127 struct address_space *aspace,
15128 CORE_ADDR pc)
15129 {
15130 struct bp_location *loc;
15131
15132 for (loc = bp->loc; loc != NULL; loc = loc->next)
15133 if (loc->inserted
15134 && breakpoint_location_address_match (loc, aspace, pc))
15135 return 1;
15136
15137 return 0;
15138 }
15139
15140 /* Check whether a software single-step breakpoint is inserted at
15141 PC. */
15142
15143 int
15144 single_step_breakpoint_inserted_here_p (struct address_space *aspace,
15145 CORE_ADDR pc)
15146 {
15147 struct breakpoint *bpt;
15148
15149 ALL_BREAKPOINTS (bpt)
15150 {
15151 if (bpt->type == bp_single_step
15152 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
15153 return 1;
15154 }
15155 return 0;
15156 }
15157
15158 /* Tracepoint-specific operations. */
15159
15160 /* Set tracepoint count to NUM. */
15161 static void
15162 set_tracepoint_count (int num)
15163 {
15164 tracepoint_count = num;
15165 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
15166 }
15167
15168 static void
15169 trace_command (char *arg, int from_tty)
15170 {
15171 struct breakpoint_ops *ops;
15172
15173 event_location_up location = string_to_event_location (&arg,
15174 current_language);
15175 if (location != NULL
15176 && event_location_type (location.get ()) == PROBE_LOCATION)
15177 ops = &tracepoint_probe_breakpoint_ops;
15178 else
15179 ops = &tracepoint_breakpoint_ops;
15180
15181 create_breakpoint (get_current_arch (),
15182 location.get (),
15183 NULL, 0, arg, 1 /* parse arg */,
15184 0 /* tempflag */,
15185 bp_tracepoint /* type_wanted */,
15186 0 /* Ignore count */,
15187 pending_break_support,
15188 ops,
15189 from_tty,
15190 1 /* enabled */,
15191 0 /* internal */, 0);
15192 }
15193
15194 static void
15195 ftrace_command (char *arg, int from_tty)
15196 {
15197 event_location_up location = string_to_event_location (&arg,
15198 current_language);
15199 create_breakpoint (get_current_arch (),
15200 location.get (),
15201 NULL, 0, arg, 1 /* parse arg */,
15202 0 /* tempflag */,
15203 bp_fast_tracepoint /* type_wanted */,
15204 0 /* Ignore count */,
15205 pending_break_support,
15206 &tracepoint_breakpoint_ops,
15207 from_tty,
15208 1 /* enabled */,
15209 0 /* internal */, 0);
15210 }
15211
15212 /* strace command implementation. Creates a static tracepoint. */
15213
15214 static void
15215 strace_command (char *arg, int from_tty)
15216 {
15217 struct breakpoint_ops *ops;
15218 event_location_up location;
15219 struct cleanup *back_to;
15220
15221 /* Decide if we are dealing with a static tracepoint marker (`-m'),
15222 or with a normal static tracepoint. */
15223 if (arg && startswith (arg, "-m") && isspace (arg[2]))
15224 {
15225 ops = &strace_marker_breakpoint_ops;
15226 location = new_linespec_location (&arg);
15227 }
15228 else
15229 {
15230 ops = &tracepoint_breakpoint_ops;
15231 location = string_to_event_location (&arg, current_language);
15232 }
15233
15234 create_breakpoint (get_current_arch (),
15235 location.get (),
15236 NULL, 0, arg, 1 /* parse arg */,
15237 0 /* tempflag */,
15238 bp_static_tracepoint /* type_wanted */,
15239 0 /* Ignore count */,
15240 pending_break_support,
15241 ops,
15242 from_tty,
15243 1 /* enabled */,
15244 0 /* internal */, 0);
15245 }
15246
15247 /* Set up a fake reader function that gets command lines from a linked
15248 list that was acquired during tracepoint uploading. */
15249
15250 static struct uploaded_tp *this_utp;
15251 static int next_cmd;
15252
15253 static char *
15254 read_uploaded_action (void)
15255 {
15256 char *rslt;
15257
15258 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
15259
15260 next_cmd++;
15261
15262 return rslt;
15263 }
15264
15265 /* Given information about a tracepoint as recorded on a target (which
15266 can be either a live system or a trace file), attempt to create an
15267 equivalent GDB tracepoint. This is not a reliable process, since
15268 the target does not necessarily have all the information used when
15269 the tracepoint was originally defined. */
15270
15271 struct tracepoint *
15272 create_tracepoint_from_upload (struct uploaded_tp *utp)
15273 {
15274 char *addr_str, small_buf[100];
15275 struct tracepoint *tp;
15276
15277 if (utp->at_string)
15278 addr_str = utp->at_string;
15279 else
15280 {
15281 /* In the absence of a source location, fall back to raw
15282 address. Since there is no way to confirm that the address
15283 means the same thing as when the trace was started, warn the
15284 user. */
15285 warning (_("Uploaded tracepoint %d has no "
15286 "source location, using raw address"),
15287 utp->number);
15288 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
15289 addr_str = small_buf;
15290 }
15291
15292 /* There's not much we can do with a sequence of bytecodes. */
15293 if (utp->cond && !utp->cond_string)
15294 warning (_("Uploaded tracepoint %d condition "
15295 "has no source form, ignoring it"),
15296 utp->number);
15297
15298 event_location_up location = string_to_event_location (&addr_str,
15299 current_language);
15300 if (!create_breakpoint (get_current_arch (),
15301 location.get (),
15302 utp->cond_string, -1, addr_str,
15303 0 /* parse cond/thread */,
15304 0 /* tempflag */,
15305 utp->type /* type_wanted */,
15306 0 /* Ignore count */,
15307 pending_break_support,
15308 &tracepoint_breakpoint_ops,
15309 0 /* from_tty */,
15310 utp->enabled /* enabled */,
15311 0 /* internal */,
15312 CREATE_BREAKPOINT_FLAGS_INSERTED))
15313 return NULL;
15314
15315 /* Get the tracepoint we just created. */
15316 tp = get_tracepoint (tracepoint_count);
15317 gdb_assert (tp != NULL);
15318
15319 if (utp->pass > 0)
15320 {
15321 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
15322 tp->base.number);
15323
15324 trace_pass_command (small_buf, 0);
15325 }
15326
15327 /* If we have uploaded versions of the original commands, set up a
15328 special-purpose "reader" function and call the usual command line
15329 reader, then pass the result to the breakpoint command-setting
15330 function. */
15331 if (!VEC_empty (char_ptr, utp->cmd_strings))
15332 {
15333 command_line_up cmd_list;
15334
15335 this_utp = utp;
15336 next_cmd = 0;
15337
15338 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
15339
15340 breakpoint_set_commands (&tp->base, std::move (cmd_list));
15341 }
15342 else if (!VEC_empty (char_ptr, utp->actions)
15343 || !VEC_empty (char_ptr, utp->step_actions))
15344 warning (_("Uploaded tracepoint %d actions "
15345 "have no source form, ignoring them"),
15346 utp->number);
15347
15348 /* Copy any status information that might be available. */
15349 tp->base.hit_count = utp->hit_count;
15350 tp->traceframe_usage = utp->traceframe_usage;
15351
15352 return tp;
15353 }
15354
15355 /* Print information on tracepoint number TPNUM_EXP, or all if
15356 omitted. */
15357
15358 static void
15359 tracepoints_info (char *args, int from_tty)
15360 {
15361 struct ui_out *uiout = current_uiout;
15362 int num_printed;
15363
15364 num_printed = breakpoint_1 (args, 0, is_tracepoint);
15365
15366 if (num_printed == 0)
15367 {
15368 if (args == NULL || *args == '\0')
15369 uiout->message ("No tracepoints.\n");
15370 else
15371 uiout->message ("No tracepoint matching '%s'.\n", args);
15372 }
15373
15374 default_collect_info ();
15375 }
15376
15377 /* The 'enable trace' command enables tracepoints.
15378 Not supported by all targets. */
15379 static void
15380 enable_trace_command (char *args, int from_tty)
15381 {
15382 enable_command (args, from_tty);
15383 }
15384
15385 /* The 'disable trace' command disables tracepoints.
15386 Not supported by all targets. */
15387 static void
15388 disable_trace_command (char *args, int from_tty)
15389 {
15390 disable_command (args, from_tty);
15391 }
15392
15393 /* Remove a tracepoint (or all if no argument). */
15394 static void
15395 delete_trace_command (char *arg, int from_tty)
15396 {
15397 struct breakpoint *b, *b_tmp;
15398
15399 dont_repeat ();
15400
15401 if (arg == 0)
15402 {
15403 int breaks_to_delete = 0;
15404
15405 /* Delete all breakpoints if no argument.
15406 Do not delete internal or call-dummy breakpoints, these
15407 have to be deleted with an explicit breakpoint number
15408 argument. */
15409 ALL_TRACEPOINTS (b)
15410 if (is_tracepoint (b) && user_breakpoint_p (b))
15411 {
15412 breaks_to_delete = 1;
15413 break;
15414 }
15415
15416 /* Ask user only if there are some breakpoints to delete. */
15417 if (!from_tty
15418 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
15419 {
15420 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15421 if (is_tracepoint (b) && user_breakpoint_p (b))
15422 delete_breakpoint (b);
15423 }
15424 }
15425 else
15426 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
15427 }
15428
15429 /* Helper function for trace_pass_command. */
15430
15431 static void
15432 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
15433 {
15434 tp->pass_count = count;
15435 observer_notify_breakpoint_modified (&tp->base);
15436 if (from_tty)
15437 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
15438 tp->base.number, count);
15439 }
15440
15441 /* Set passcount for tracepoint.
15442
15443 First command argument is passcount, second is tracepoint number.
15444 If tracepoint number omitted, apply to most recently defined.
15445 Also accepts special argument "all". */
15446
15447 static void
15448 trace_pass_command (char *args, int from_tty)
15449 {
15450 struct tracepoint *t1;
15451 unsigned int count;
15452
15453 if (args == 0 || *args == 0)
15454 error (_("passcount command requires an "
15455 "argument (count + optional TP num)"));
15456
15457 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
15458
15459 args = skip_spaces (args);
15460 if (*args && strncasecmp (args, "all", 3) == 0)
15461 {
15462 struct breakpoint *b;
15463
15464 args += 3; /* Skip special argument "all". */
15465 if (*args)
15466 error (_("Junk at end of arguments."));
15467
15468 ALL_TRACEPOINTS (b)
15469 {
15470 t1 = (struct tracepoint *) b;
15471 trace_pass_set_count (t1, count, from_tty);
15472 }
15473 }
15474 else if (*args == '\0')
15475 {
15476 t1 = get_tracepoint_by_number (&args, NULL);
15477 if (t1)
15478 trace_pass_set_count (t1, count, from_tty);
15479 }
15480 else
15481 {
15482 number_or_range_parser parser (args);
15483 while (!parser.finished ())
15484 {
15485 t1 = get_tracepoint_by_number (&args, &parser);
15486 if (t1)
15487 trace_pass_set_count (t1, count, from_tty);
15488 }
15489 }
15490 }
15491
15492 struct tracepoint *
15493 get_tracepoint (int num)
15494 {
15495 struct breakpoint *t;
15496
15497 ALL_TRACEPOINTS (t)
15498 if (t->number == num)
15499 return (struct tracepoint *) t;
15500
15501 return NULL;
15502 }
15503
15504 /* Find the tracepoint with the given target-side number (which may be
15505 different from the tracepoint number after disconnecting and
15506 reconnecting). */
15507
15508 struct tracepoint *
15509 get_tracepoint_by_number_on_target (int num)
15510 {
15511 struct breakpoint *b;
15512
15513 ALL_TRACEPOINTS (b)
15514 {
15515 struct tracepoint *t = (struct tracepoint *) b;
15516
15517 if (t->number_on_target == num)
15518 return t;
15519 }
15520
15521 return NULL;
15522 }
15523
15524 /* Utility: parse a tracepoint number and look it up in the list.
15525 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
15526 If the argument is missing, the most recent tracepoint
15527 (tracepoint_count) is returned. */
15528
15529 struct tracepoint *
15530 get_tracepoint_by_number (char **arg,
15531 number_or_range_parser *parser)
15532 {
15533 struct breakpoint *t;
15534 int tpnum;
15535 char *instring = arg == NULL ? NULL : *arg;
15536
15537 if (parser != NULL)
15538 {
15539 gdb_assert (!parser->finished ());
15540 tpnum = parser->get_number ();
15541 }
15542 else if (arg == NULL || *arg == NULL || ! **arg)
15543 tpnum = tracepoint_count;
15544 else
15545 tpnum = get_number (arg);
15546
15547 if (tpnum <= 0)
15548 {
15549 if (instring && *instring)
15550 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15551 instring);
15552 else
15553 printf_filtered (_("No previous tracepoint\n"));
15554 return NULL;
15555 }
15556
15557 ALL_TRACEPOINTS (t)
15558 if (t->number == tpnum)
15559 {
15560 return (struct tracepoint *) t;
15561 }
15562
15563 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15564 return NULL;
15565 }
15566
15567 void
15568 print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15569 {
15570 if (b->thread != -1)
15571 fprintf_unfiltered (fp, " thread %d", b->thread);
15572
15573 if (b->task != 0)
15574 fprintf_unfiltered (fp, " task %d", b->task);
15575
15576 fprintf_unfiltered (fp, "\n");
15577 }
15578
15579 /* Save information on user settable breakpoints (watchpoints, etc) to
15580 a new script file named FILENAME. If FILTER is non-NULL, call it
15581 on each breakpoint and only include the ones for which it returns
15582 non-zero. */
15583
15584 static void
15585 save_breakpoints (char *filename, int from_tty,
15586 int (*filter) (const struct breakpoint *))
15587 {
15588 struct breakpoint *tp;
15589 int any = 0;
15590 struct cleanup *cleanup;
15591 int extra_trace_bits = 0;
15592
15593 if (filename == 0 || *filename == 0)
15594 error (_("Argument required (file name in which to save)"));
15595
15596 /* See if we have anything to save. */
15597 ALL_BREAKPOINTS (tp)
15598 {
15599 /* Skip internal and momentary breakpoints. */
15600 if (!user_breakpoint_p (tp))
15601 continue;
15602
15603 /* If we have a filter, only save the breakpoints it accepts. */
15604 if (filter && !filter (tp))
15605 continue;
15606
15607 any = 1;
15608
15609 if (is_tracepoint (tp))
15610 {
15611 extra_trace_bits = 1;
15612
15613 /* We can stop searching. */
15614 break;
15615 }
15616 }
15617
15618 if (!any)
15619 {
15620 warning (_("Nothing to save."));
15621 return;
15622 }
15623
15624 filename = tilde_expand (filename);
15625 cleanup = make_cleanup (xfree, filename);
15626
15627 stdio_file fp;
15628
15629 if (!fp.open (filename, "w"))
15630 error (_("Unable to open file '%s' for saving (%s)"),
15631 filename, safe_strerror (errno));
15632
15633 if (extra_trace_bits)
15634 save_trace_state_variables (&fp);
15635
15636 ALL_BREAKPOINTS (tp)
15637 {
15638 /* Skip internal and momentary breakpoints. */
15639 if (!user_breakpoint_p (tp))
15640 continue;
15641
15642 /* If we have a filter, only save the breakpoints it accepts. */
15643 if (filter && !filter (tp))
15644 continue;
15645
15646 tp->ops->print_recreate (tp, &fp);
15647
15648 /* Note, we can't rely on tp->number for anything, as we can't
15649 assume the recreated breakpoint numbers will match. Use $bpnum
15650 instead. */
15651
15652 if (tp->cond_string)
15653 fp.printf (" condition $bpnum %s\n", tp->cond_string);
15654
15655 if (tp->ignore_count)
15656 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
15657
15658 if (tp->type != bp_dprintf && tp->commands)
15659 {
15660 fp.puts (" commands\n");
15661
15662 current_uiout->redirect (&fp);
15663 TRY
15664 {
15665 print_command_lines (current_uiout, tp->commands->commands, 2);
15666 }
15667 CATCH (ex, RETURN_MASK_ALL)
15668 {
15669 current_uiout->redirect (NULL);
15670 throw_exception (ex);
15671 }
15672 END_CATCH
15673
15674 current_uiout->redirect (NULL);
15675 fp.puts (" end\n");
15676 }
15677
15678 if (tp->enable_state == bp_disabled)
15679 fp.puts ("disable $bpnum\n");
15680
15681 /* If this is a multi-location breakpoint, check if the locations
15682 should be individually disabled. Watchpoint locations are
15683 special, and not user visible. */
15684 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15685 {
15686 struct bp_location *loc;
15687 int n = 1;
15688
15689 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15690 if (!loc->enabled)
15691 fp.printf ("disable $bpnum.%d\n", n);
15692 }
15693 }
15694
15695 if (extra_trace_bits && *default_collect)
15696 fp.printf ("set default-collect %s\n", default_collect);
15697
15698 if (from_tty)
15699 printf_filtered (_("Saved to file '%s'.\n"), filename);
15700 do_cleanups (cleanup);
15701 }
15702
15703 /* The `save breakpoints' command. */
15704
15705 static void
15706 save_breakpoints_command (char *args, int from_tty)
15707 {
15708 save_breakpoints (args, from_tty, NULL);
15709 }
15710
15711 /* The `save tracepoints' command. */
15712
15713 static void
15714 save_tracepoints_command (char *args, int from_tty)
15715 {
15716 save_breakpoints (args, from_tty, is_tracepoint);
15717 }
15718
15719 /* Create a vector of all tracepoints. */
15720
15721 VEC(breakpoint_p) *
15722 all_tracepoints (void)
15723 {
15724 VEC(breakpoint_p) *tp_vec = 0;
15725 struct breakpoint *tp;
15726
15727 ALL_TRACEPOINTS (tp)
15728 {
15729 VEC_safe_push (breakpoint_p, tp_vec, tp);
15730 }
15731
15732 return tp_vec;
15733 }
15734
15735 \f
15736 /* This help string is used to consolidate all the help string for specifying
15737 locations used by several commands. */
15738
15739 #define LOCATION_HELP_STRING \
15740 "Linespecs are colon-separated lists of location parameters, such as\n\
15741 source filename, function name, label name, and line number.\n\
15742 Example: To specify the start of a label named \"the_top\" in the\n\
15743 function \"fact\" in the file \"factorial.c\", use\n\
15744 \"factorial.c:fact:the_top\".\n\
15745 \n\
15746 Address locations begin with \"*\" and specify an exact address in the\n\
15747 program. Example: To specify the fourth byte past the start function\n\
15748 \"main\", use \"*main + 4\".\n\
15749 \n\
15750 Explicit locations are similar to linespecs but use an option/argument\n\
15751 syntax to specify location parameters.\n\
15752 Example: To specify the start of the label named \"the_top\" in the\n\
15753 function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
15754 -function fact -label the_top\".\n"
15755
15756 /* This help string is used for the break, hbreak, tbreak and thbreak
15757 commands. It is defined as a macro to prevent duplication.
15758 COMMAND should be a string constant containing the name of the
15759 command. */
15760
15761 #define BREAK_ARGS_HELP(command) \
15762 command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15763 PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15764 probe point. Accepted values are `-probe' (for a generic, automatically\n\
15765 guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15766 `-probe-dtrace' (for a DTrace probe).\n\
15767 LOCATION may be a linespec, address, or explicit location as described\n\
15768 below.\n\
15769 \n\
15770 With no LOCATION, uses current execution address of the selected\n\
15771 stack frame. This is useful for breaking on return to a stack frame.\n\
15772 \n\
15773 THREADNUM is the number from \"info threads\".\n\
15774 CONDITION is a boolean expression.\n\
15775 \n" LOCATION_HELP_STRING "\n\
15776 Multiple breakpoints at one place are permitted, and useful if their\n\
15777 conditions are different.\n\
15778 \n\
15779 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15780
15781 /* List of subcommands for "catch". */
15782 static struct cmd_list_element *catch_cmdlist;
15783
15784 /* List of subcommands for "tcatch". */
15785 static struct cmd_list_element *tcatch_cmdlist;
15786
15787 void
15788 add_catch_command (const char *name, const char *docstring,
15789 cmd_sfunc_ftype *sfunc,
15790 completer_ftype *completer,
15791 void *user_data_catch,
15792 void *user_data_tcatch)
15793 {
15794 struct cmd_list_element *command;
15795
15796 command = add_cmd (name, class_breakpoint, NULL, docstring,
15797 &catch_cmdlist);
15798 set_cmd_sfunc (command, sfunc);
15799 set_cmd_context (command, user_data_catch);
15800 set_cmd_completer (command, completer);
15801
15802 command = add_cmd (name, class_breakpoint, NULL, docstring,
15803 &tcatch_cmdlist);
15804 set_cmd_sfunc (command, sfunc);
15805 set_cmd_context (command, user_data_tcatch);
15806 set_cmd_completer (command, completer);
15807 }
15808
15809 static void
15810 save_command (char *arg, int from_tty)
15811 {
15812 printf_unfiltered (_("\"save\" must be followed by "
15813 "the name of a save subcommand.\n"));
15814 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
15815 }
15816
15817 struct breakpoint *
15818 iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15819 void *data)
15820 {
15821 struct breakpoint *b, *b_tmp;
15822
15823 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15824 {
15825 if ((*callback) (b, data))
15826 return b;
15827 }
15828
15829 return NULL;
15830 }
15831
15832 /* Zero if any of the breakpoint's locations could be a location where
15833 functions have been inlined, nonzero otherwise. */
15834
15835 static int
15836 is_non_inline_function (struct breakpoint *b)
15837 {
15838 /* The shared library event breakpoint is set on the address of a
15839 non-inline function. */
15840 if (b->type == bp_shlib_event)
15841 return 1;
15842
15843 return 0;
15844 }
15845
15846 /* Nonzero if the specified PC cannot be a location where functions
15847 have been inlined. */
15848
15849 int
15850 pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
15851 const struct target_waitstatus *ws)
15852 {
15853 struct breakpoint *b;
15854 struct bp_location *bl;
15855
15856 ALL_BREAKPOINTS (b)
15857 {
15858 if (!is_non_inline_function (b))
15859 continue;
15860
15861 for (bl = b->loc; bl != NULL; bl = bl->next)
15862 {
15863 if (!bl->shlib_disabled
15864 && bpstat_check_location (bl, aspace, pc, ws))
15865 return 1;
15866 }
15867 }
15868
15869 return 0;
15870 }
15871
15872 /* Remove any references to OBJFILE which is going to be freed. */
15873
15874 void
15875 breakpoint_free_objfile (struct objfile *objfile)
15876 {
15877 struct bp_location **locp, *loc;
15878
15879 ALL_BP_LOCATIONS (loc, locp)
15880 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
15881 loc->symtab = NULL;
15882 }
15883
15884 void
15885 initialize_breakpoint_ops (void)
15886 {
15887 static int initialized = 0;
15888
15889 struct breakpoint_ops *ops;
15890
15891 if (initialized)
15892 return;
15893 initialized = 1;
15894
15895 /* The breakpoint_ops structure to be inherit by all kinds of
15896 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15897 internal and momentary breakpoints, etc.). */
15898 ops = &bkpt_base_breakpoint_ops;
15899 *ops = base_breakpoint_ops;
15900 ops->re_set = bkpt_re_set;
15901 ops->insert_location = bkpt_insert_location;
15902 ops->remove_location = bkpt_remove_location;
15903 ops->breakpoint_hit = bkpt_breakpoint_hit;
15904 ops->create_sals_from_location = bkpt_create_sals_from_location;
15905 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
15906 ops->decode_location = bkpt_decode_location;
15907
15908 /* The breakpoint_ops structure to be used in regular breakpoints. */
15909 ops = &bkpt_breakpoint_ops;
15910 *ops = bkpt_base_breakpoint_ops;
15911 ops->re_set = bkpt_re_set;
15912 ops->resources_needed = bkpt_resources_needed;
15913 ops->print_it = bkpt_print_it;
15914 ops->print_mention = bkpt_print_mention;
15915 ops->print_recreate = bkpt_print_recreate;
15916
15917 /* Ranged breakpoints. */
15918 ops = &ranged_breakpoint_ops;
15919 *ops = bkpt_breakpoint_ops;
15920 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15921 ops->resources_needed = resources_needed_ranged_breakpoint;
15922 ops->print_it = print_it_ranged_breakpoint;
15923 ops->print_one = print_one_ranged_breakpoint;
15924 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15925 ops->print_mention = print_mention_ranged_breakpoint;
15926 ops->print_recreate = print_recreate_ranged_breakpoint;
15927
15928 /* Internal breakpoints. */
15929 ops = &internal_breakpoint_ops;
15930 *ops = bkpt_base_breakpoint_ops;
15931 ops->re_set = internal_bkpt_re_set;
15932 ops->check_status = internal_bkpt_check_status;
15933 ops->print_it = internal_bkpt_print_it;
15934 ops->print_mention = internal_bkpt_print_mention;
15935
15936 /* Momentary breakpoints. */
15937 ops = &momentary_breakpoint_ops;
15938 *ops = bkpt_base_breakpoint_ops;
15939 ops->re_set = momentary_bkpt_re_set;
15940 ops->check_status = momentary_bkpt_check_status;
15941 ops->print_it = momentary_bkpt_print_it;
15942 ops->print_mention = momentary_bkpt_print_mention;
15943
15944 /* Momentary breakpoints for bp_longjmp and bp_exception. */
15945 ops = &longjmp_breakpoint_ops;
15946 *ops = momentary_breakpoint_ops;
15947 ops->dtor = longjmp_bkpt_dtor;
15948
15949 /* Probe breakpoints. */
15950 ops = &bkpt_probe_breakpoint_ops;
15951 *ops = bkpt_breakpoint_ops;
15952 ops->insert_location = bkpt_probe_insert_location;
15953 ops->remove_location = bkpt_probe_remove_location;
15954 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
15955 ops->decode_location = bkpt_probe_decode_location;
15956
15957 /* Watchpoints. */
15958 ops = &watchpoint_breakpoint_ops;
15959 *ops = base_breakpoint_ops;
15960 ops->dtor = dtor_watchpoint;
15961 ops->re_set = re_set_watchpoint;
15962 ops->insert_location = insert_watchpoint;
15963 ops->remove_location = remove_watchpoint;
15964 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15965 ops->check_status = check_status_watchpoint;
15966 ops->resources_needed = resources_needed_watchpoint;
15967 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15968 ops->print_it = print_it_watchpoint;
15969 ops->print_mention = print_mention_watchpoint;
15970 ops->print_recreate = print_recreate_watchpoint;
15971 ops->explains_signal = explains_signal_watchpoint;
15972
15973 /* Masked watchpoints. */
15974 ops = &masked_watchpoint_breakpoint_ops;
15975 *ops = watchpoint_breakpoint_ops;
15976 ops->insert_location = insert_masked_watchpoint;
15977 ops->remove_location = remove_masked_watchpoint;
15978 ops->resources_needed = resources_needed_masked_watchpoint;
15979 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15980 ops->print_it = print_it_masked_watchpoint;
15981 ops->print_one_detail = print_one_detail_masked_watchpoint;
15982 ops->print_mention = print_mention_masked_watchpoint;
15983 ops->print_recreate = print_recreate_masked_watchpoint;
15984
15985 /* Tracepoints. */
15986 ops = &tracepoint_breakpoint_ops;
15987 *ops = base_breakpoint_ops;
15988 ops->re_set = tracepoint_re_set;
15989 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15990 ops->print_one_detail = tracepoint_print_one_detail;
15991 ops->print_mention = tracepoint_print_mention;
15992 ops->print_recreate = tracepoint_print_recreate;
15993 ops->create_sals_from_location = tracepoint_create_sals_from_location;
15994 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
15995 ops->decode_location = tracepoint_decode_location;
15996
15997 /* Probe tracepoints. */
15998 ops = &tracepoint_probe_breakpoint_ops;
15999 *ops = tracepoint_breakpoint_ops;
16000 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
16001 ops->decode_location = tracepoint_probe_decode_location;
16002
16003 /* Static tracepoints with marker (`-m'). */
16004 ops = &strace_marker_breakpoint_ops;
16005 *ops = tracepoint_breakpoint_ops;
16006 ops->create_sals_from_location = strace_marker_create_sals_from_location;
16007 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
16008 ops->decode_location = strace_marker_decode_location;
16009
16010 /* Fork catchpoints. */
16011 ops = &catch_fork_breakpoint_ops;
16012 *ops = base_breakpoint_ops;
16013 ops->insert_location = insert_catch_fork;
16014 ops->remove_location = remove_catch_fork;
16015 ops->breakpoint_hit = breakpoint_hit_catch_fork;
16016 ops->print_it = print_it_catch_fork;
16017 ops->print_one = print_one_catch_fork;
16018 ops->print_mention = print_mention_catch_fork;
16019 ops->print_recreate = print_recreate_catch_fork;
16020
16021 /* Vfork catchpoints. */
16022 ops = &catch_vfork_breakpoint_ops;
16023 *ops = base_breakpoint_ops;
16024 ops->insert_location = insert_catch_vfork;
16025 ops->remove_location = remove_catch_vfork;
16026 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
16027 ops->print_it = print_it_catch_vfork;
16028 ops->print_one = print_one_catch_vfork;
16029 ops->print_mention = print_mention_catch_vfork;
16030 ops->print_recreate = print_recreate_catch_vfork;
16031
16032 /* Exec catchpoints. */
16033 ops = &catch_exec_breakpoint_ops;
16034 *ops = base_breakpoint_ops;
16035 ops->dtor = dtor_catch_exec;
16036 ops->insert_location = insert_catch_exec;
16037 ops->remove_location = remove_catch_exec;
16038 ops->breakpoint_hit = breakpoint_hit_catch_exec;
16039 ops->print_it = print_it_catch_exec;
16040 ops->print_one = print_one_catch_exec;
16041 ops->print_mention = print_mention_catch_exec;
16042 ops->print_recreate = print_recreate_catch_exec;
16043
16044 /* Solib-related catchpoints. */
16045 ops = &catch_solib_breakpoint_ops;
16046 *ops = base_breakpoint_ops;
16047 ops->dtor = dtor_catch_solib;
16048 ops->insert_location = insert_catch_solib;
16049 ops->remove_location = remove_catch_solib;
16050 ops->breakpoint_hit = breakpoint_hit_catch_solib;
16051 ops->check_status = check_status_catch_solib;
16052 ops->print_it = print_it_catch_solib;
16053 ops->print_one = print_one_catch_solib;
16054 ops->print_mention = print_mention_catch_solib;
16055 ops->print_recreate = print_recreate_catch_solib;
16056
16057 ops = &dprintf_breakpoint_ops;
16058 *ops = bkpt_base_breakpoint_ops;
16059 ops->re_set = dprintf_re_set;
16060 ops->resources_needed = bkpt_resources_needed;
16061 ops->print_it = bkpt_print_it;
16062 ops->print_mention = bkpt_print_mention;
16063 ops->print_recreate = dprintf_print_recreate;
16064 ops->after_condition_true = dprintf_after_condition_true;
16065 ops->breakpoint_hit = dprintf_breakpoint_hit;
16066 }
16067
16068 /* Chain containing all defined "enable breakpoint" subcommands. */
16069
16070 static struct cmd_list_element *enablebreaklist = NULL;
16071
16072 void
16073 _initialize_breakpoint (void)
16074 {
16075 struct cmd_list_element *c;
16076
16077 initialize_breakpoint_ops ();
16078
16079 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
16080 observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
16081 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
16082
16083 breakpoint_objfile_key
16084 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
16085
16086 breakpoint_chain = 0;
16087 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
16088 before a breakpoint is set. */
16089 breakpoint_count = 0;
16090
16091 tracepoint_count = 0;
16092
16093 add_com ("ignore", class_breakpoint, ignore_command, _("\
16094 Set ignore-count of breakpoint number N to COUNT.\n\
16095 Usage is `ignore N COUNT'."));
16096
16097 add_com ("commands", class_breakpoint, commands_command, _("\
16098 Set commands to be executed when the given breakpoints are hit.\n\
16099 Give a space-separated breakpoint list as argument after \"commands\".\n\
16100 A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
16101 (e.g. `5-7').\n\
16102 With no argument, the targeted breakpoint is the last one set.\n\
16103 The commands themselves follow starting on the next line.\n\
16104 Type a line containing \"end\" to indicate the end of them.\n\
16105 Give \"silent\" as the first line to make the breakpoint silent;\n\
16106 then no output is printed when it is hit, except what the commands print."));
16107
16108 c = add_com ("condition", class_breakpoint, condition_command, _("\
16109 Specify breakpoint number N to break only if COND is true.\n\
16110 Usage is `condition N COND', where N is an integer and COND is an\n\
16111 expression to be evaluated whenever breakpoint N is reached."));
16112 set_cmd_completer (c, condition_completer);
16113
16114 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
16115 Set a temporary breakpoint.\n\
16116 Like \"break\" except the breakpoint is only temporary,\n\
16117 so it will be deleted when hit. Equivalent to \"break\" followed\n\
16118 by using \"enable delete\" on the breakpoint number.\n\
16119 \n"
16120 BREAK_ARGS_HELP ("tbreak")));
16121 set_cmd_completer (c, location_completer);
16122
16123 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
16124 Set a hardware assisted breakpoint.\n\
16125 Like \"break\" except the breakpoint requires hardware support,\n\
16126 some target hardware may not have this support.\n\
16127 \n"
16128 BREAK_ARGS_HELP ("hbreak")));
16129 set_cmd_completer (c, location_completer);
16130
16131 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
16132 Set a temporary hardware assisted breakpoint.\n\
16133 Like \"hbreak\" except the breakpoint is only temporary,\n\
16134 so it will be deleted when hit.\n\
16135 \n"
16136 BREAK_ARGS_HELP ("thbreak")));
16137 set_cmd_completer (c, location_completer);
16138
16139 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
16140 Enable some breakpoints.\n\
16141 Give breakpoint numbers (separated by spaces) as arguments.\n\
16142 With no subcommand, breakpoints are enabled until you command otherwise.\n\
16143 This is used to cancel the effect of the \"disable\" command.\n\
16144 With a subcommand you can enable temporarily."),
16145 &enablelist, "enable ", 1, &cmdlist);
16146
16147 add_com_alias ("en", "enable", class_breakpoint, 1);
16148
16149 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
16150 Enable some breakpoints.\n\
16151 Give breakpoint numbers (separated by spaces) as arguments.\n\
16152 This is used to cancel the effect of the \"disable\" command.\n\
16153 May be abbreviated to simply \"enable\".\n"),
16154 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
16155
16156 add_cmd ("once", no_class, enable_once_command, _("\
16157 Enable breakpoints for one hit. Give breakpoint numbers.\n\
16158 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
16159 &enablebreaklist);
16160
16161 add_cmd ("delete", no_class, enable_delete_command, _("\
16162 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16163 If a breakpoint is hit while enabled in this fashion, it is deleted."),
16164 &enablebreaklist);
16165
16166 add_cmd ("count", no_class, enable_count_command, _("\
16167 Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16168 If a breakpoint is hit while enabled in this fashion,\n\
16169 the count is decremented; when it reaches zero, the breakpoint is disabled."),
16170 &enablebreaklist);
16171
16172 add_cmd ("delete", no_class, enable_delete_command, _("\
16173 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16174 If a breakpoint is hit while enabled in this fashion, it is deleted."),
16175 &enablelist);
16176
16177 add_cmd ("once", no_class, enable_once_command, _("\
16178 Enable breakpoints for one hit. Give breakpoint numbers.\n\
16179 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
16180 &enablelist);
16181
16182 add_cmd ("count", no_class, enable_count_command, _("\
16183 Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16184 If a breakpoint is hit while enabled in this fashion,\n\
16185 the count is decremented; when it reaches zero, the breakpoint is disabled."),
16186 &enablelist);
16187
16188 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
16189 Disable some breakpoints.\n\
16190 Arguments are breakpoint numbers with spaces in between.\n\
16191 To disable all breakpoints, give no argument.\n\
16192 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
16193 &disablelist, "disable ", 1, &cmdlist);
16194 add_com_alias ("dis", "disable", class_breakpoint, 1);
16195 add_com_alias ("disa", "disable", class_breakpoint, 1);
16196
16197 add_cmd ("breakpoints", class_alias, disable_command, _("\
16198 Disable some breakpoints.\n\
16199 Arguments are breakpoint numbers with spaces in between.\n\
16200 To disable all breakpoints, give no argument.\n\
16201 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
16202 This command may be abbreviated \"disable\"."),
16203 &disablelist);
16204
16205 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
16206 Delete some breakpoints or auto-display expressions.\n\
16207 Arguments are breakpoint numbers with spaces in between.\n\
16208 To delete all breakpoints, give no argument.\n\
16209 \n\
16210 Also a prefix command for deletion of other GDB objects.\n\
16211 The \"unset\" command is also an alias for \"delete\"."),
16212 &deletelist, "delete ", 1, &cmdlist);
16213 add_com_alias ("d", "delete", class_breakpoint, 1);
16214 add_com_alias ("del", "delete", class_breakpoint, 1);
16215
16216 add_cmd ("breakpoints", class_alias, delete_command, _("\
16217 Delete some breakpoints or auto-display expressions.\n\
16218 Arguments are breakpoint numbers with spaces in between.\n\
16219 To delete all breakpoints, give no argument.\n\
16220 This command may be abbreviated \"delete\"."),
16221 &deletelist);
16222
16223 add_com ("clear", class_breakpoint, clear_command, _("\
16224 Clear breakpoint at specified location.\n\
16225 Argument may be a linespec, explicit, or address location as described below.\n\
16226 \n\
16227 With no argument, clears all breakpoints in the line that the selected frame\n\
16228 is executing in.\n"
16229 "\n" LOCATION_HELP_STRING "\n\
16230 See also the \"delete\" command which clears breakpoints by number."));
16231 add_com_alias ("cl", "clear", class_breakpoint, 1);
16232
16233 c = add_com ("break", class_breakpoint, break_command, _("\
16234 Set breakpoint at specified location.\n"
16235 BREAK_ARGS_HELP ("break")));
16236 set_cmd_completer (c, location_completer);
16237
16238 add_com_alias ("b", "break", class_run, 1);
16239 add_com_alias ("br", "break", class_run, 1);
16240 add_com_alias ("bre", "break", class_run, 1);
16241 add_com_alias ("brea", "break", class_run, 1);
16242
16243 if (dbx_commands)
16244 {
16245 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
16246 Break in function/address or break at a line in the current file."),
16247 &stoplist, "stop ", 1, &cmdlist);
16248 add_cmd ("in", class_breakpoint, stopin_command,
16249 _("Break in function or address."), &stoplist);
16250 add_cmd ("at", class_breakpoint, stopat_command,
16251 _("Break at a line in the current file."), &stoplist);
16252 add_com ("status", class_info, breakpoints_info, _("\
16253 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
16254 The \"Type\" column indicates one of:\n\
16255 \tbreakpoint - normal breakpoint\n\
16256 \twatchpoint - watchpoint\n\
16257 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16258 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16259 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16260 address and file/line number respectively.\n\
16261 \n\
16262 Convenience variable \"$_\" and default examine address for \"x\"\n\
16263 are set to the address of the last breakpoint listed unless the command\n\
16264 is prefixed with \"server \".\n\n\
16265 Convenience variable \"$bpnum\" contains the number of the last\n\
16266 breakpoint set."));
16267 }
16268
16269 add_info ("breakpoints", breakpoints_info, _("\
16270 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
16271 The \"Type\" column indicates one of:\n\
16272 \tbreakpoint - normal breakpoint\n\
16273 \twatchpoint - watchpoint\n\
16274 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16275 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16276 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16277 address and file/line number respectively.\n\
16278 \n\
16279 Convenience variable \"$_\" and default examine address for \"x\"\n\
16280 are set to the address of the last breakpoint listed unless the command\n\
16281 is prefixed with \"server \".\n\n\
16282 Convenience variable \"$bpnum\" contains the number of the last\n\
16283 breakpoint set."));
16284
16285 add_info_alias ("b", "breakpoints", 1);
16286
16287 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
16288 Status of all breakpoints, or breakpoint number NUMBER.\n\
16289 The \"Type\" column indicates one of:\n\
16290 \tbreakpoint - normal breakpoint\n\
16291 \twatchpoint - watchpoint\n\
16292 \tlongjmp - internal breakpoint used to step through longjmp()\n\
16293 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
16294 \tuntil - internal breakpoint used by the \"until\" command\n\
16295 \tfinish - internal breakpoint used by the \"finish\" command\n\
16296 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16297 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16298 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16299 address and file/line number respectively.\n\
16300 \n\
16301 Convenience variable \"$_\" and default examine address for \"x\"\n\
16302 are set to the address of the last breakpoint listed unless the command\n\
16303 is prefixed with \"server \".\n\n\
16304 Convenience variable \"$bpnum\" contains the number of the last\n\
16305 breakpoint set."),
16306 &maintenanceinfolist);
16307
16308 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
16309 Set catchpoints to catch events."),
16310 &catch_cmdlist, "catch ",
16311 0/*allow-unknown*/, &cmdlist);
16312
16313 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
16314 Set temporary catchpoints to catch events."),
16315 &tcatch_cmdlist, "tcatch ",
16316 0/*allow-unknown*/, &cmdlist);
16317
16318 add_catch_command ("fork", _("Catch calls to fork."),
16319 catch_fork_command_1,
16320 NULL,
16321 (void *) (uintptr_t) catch_fork_permanent,
16322 (void *) (uintptr_t) catch_fork_temporary);
16323 add_catch_command ("vfork", _("Catch calls to vfork."),
16324 catch_fork_command_1,
16325 NULL,
16326 (void *) (uintptr_t) catch_vfork_permanent,
16327 (void *) (uintptr_t) catch_vfork_temporary);
16328 add_catch_command ("exec", _("Catch calls to exec."),
16329 catch_exec_command_1,
16330 NULL,
16331 CATCH_PERMANENT,
16332 CATCH_TEMPORARY);
16333 add_catch_command ("load", _("Catch loads of shared libraries.\n\
16334 Usage: catch load [REGEX]\n\
16335 If REGEX is given, only stop for libraries matching the regular expression."),
16336 catch_load_command_1,
16337 NULL,
16338 CATCH_PERMANENT,
16339 CATCH_TEMPORARY);
16340 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
16341 Usage: catch unload [REGEX]\n\
16342 If REGEX is given, only stop for libraries matching the regular expression."),
16343 catch_unload_command_1,
16344 NULL,
16345 CATCH_PERMANENT,
16346 CATCH_TEMPORARY);
16347
16348 c = add_com ("watch", class_breakpoint, watch_command, _("\
16349 Set a watchpoint for an expression.\n\
16350 Usage: watch [-l|-location] EXPRESSION\n\
16351 A watchpoint stops execution of your program whenever the value of\n\
16352 an expression changes.\n\
16353 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16354 the memory to which it refers."));
16355 set_cmd_completer (c, expression_completer);
16356
16357 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
16358 Set a read watchpoint for an expression.\n\
16359 Usage: rwatch [-l|-location] EXPRESSION\n\
16360 A watchpoint stops execution of your program whenever the value of\n\
16361 an expression is read.\n\
16362 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16363 the memory to which it refers."));
16364 set_cmd_completer (c, expression_completer);
16365
16366 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
16367 Set a watchpoint for an expression.\n\
16368 Usage: awatch [-l|-location] EXPRESSION\n\
16369 A watchpoint stops execution of your program whenever the value of\n\
16370 an expression is either read or written.\n\
16371 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16372 the memory to which it refers."));
16373 set_cmd_completer (c, expression_completer);
16374
16375 add_info ("watchpoints", watchpoints_info, _("\
16376 Status of specified watchpoints (all watchpoints if no argument)."));
16377
16378 /* XXX: cagney/2005-02-23: This should be a boolean, and should
16379 respond to changes - contrary to the description. */
16380 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
16381 &can_use_hw_watchpoints, _("\
16382 Set debugger's willingness to use watchpoint hardware."), _("\
16383 Show debugger's willingness to use watchpoint hardware."), _("\
16384 If zero, gdb will not use hardware for new watchpoints, even if\n\
16385 such is available. (However, any hardware watchpoints that were\n\
16386 created before setting this to nonzero, will continue to use watchpoint\n\
16387 hardware.)"),
16388 NULL,
16389 show_can_use_hw_watchpoints,
16390 &setlist, &showlist);
16391
16392 can_use_hw_watchpoints = 1;
16393
16394 /* Tracepoint manipulation commands. */
16395
16396 c = add_com ("trace", class_breakpoint, trace_command, _("\
16397 Set a tracepoint at specified location.\n\
16398 \n"
16399 BREAK_ARGS_HELP ("trace") "\n\
16400 Do \"help tracepoints\" for info on other tracepoint commands."));
16401 set_cmd_completer (c, location_completer);
16402
16403 add_com_alias ("tp", "trace", class_alias, 0);
16404 add_com_alias ("tr", "trace", class_alias, 1);
16405 add_com_alias ("tra", "trace", class_alias, 1);
16406 add_com_alias ("trac", "trace", class_alias, 1);
16407
16408 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
16409 Set a fast tracepoint at specified location.\n\
16410 \n"
16411 BREAK_ARGS_HELP ("ftrace") "\n\
16412 Do \"help tracepoints\" for info on other tracepoint commands."));
16413 set_cmd_completer (c, location_completer);
16414
16415 c = add_com ("strace", class_breakpoint, strace_command, _("\
16416 Set a static tracepoint at location or marker.\n\
16417 \n\
16418 strace [LOCATION] [if CONDITION]\n\
16419 LOCATION may be a linespec, explicit, or address location (described below) \n\
16420 or -m MARKER_ID.\n\n\
16421 If a marker id is specified, probe the marker with that name. With\n\
16422 no LOCATION, uses current execution address of the selected stack frame.\n\
16423 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16424 This collects arbitrary user data passed in the probe point call to the\n\
16425 tracing library. You can inspect it when analyzing the trace buffer,\n\
16426 by printing the $_sdata variable like any other convenience variable.\n\
16427 \n\
16428 CONDITION is a boolean expression.\n\
16429 \n" LOCATION_HELP_STRING "\n\
16430 Multiple tracepoints at one place are permitted, and useful if their\n\
16431 conditions are different.\n\
16432 \n\
16433 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16434 Do \"help tracepoints\" for info on other tracepoint commands."));
16435 set_cmd_completer (c, location_completer);
16436
16437 add_info ("tracepoints", tracepoints_info, _("\
16438 Status of specified tracepoints (all tracepoints if no argument).\n\
16439 Convenience variable \"$tpnum\" contains the number of the\n\
16440 last tracepoint set."));
16441
16442 add_info_alias ("tp", "tracepoints", 1);
16443
16444 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16445 Delete specified tracepoints.\n\
16446 Arguments are tracepoint numbers, separated by spaces.\n\
16447 No argument means delete all tracepoints."),
16448 &deletelist);
16449 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
16450
16451 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16452 Disable specified tracepoints.\n\
16453 Arguments are tracepoint numbers, separated by spaces.\n\
16454 No argument means disable all tracepoints."),
16455 &disablelist);
16456 deprecate_cmd (c, "disable");
16457
16458 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16459 Enable specified tracepoints.\n\
16460 Arguments are tracepoint numbers, separated by spaces.\n\
16461 No argument means enable all tracepoints."),
16462 &enablelist);
16463 deprecate_cmd (c, "enable");
16464
16465 add_com ("passcount", class_trace, trace_pass_command, _("\
16466 Set the passcount for a tracepoint.\n\
16467 The trace will end when the tracepoint has been passed 'count' times.\n\
16468 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16469 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16470
16471 add_prefix_cmd ("save", class_breakpoint, save_command,
16472 _("Save breakpoint definitions as a script."),
16473 &save_cmdlist, "save ",
16474 0/*allow-unknown*/, &cmdlist);
16475
16476 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16477 Save current breakpoint definitions as a script.\n\
16478 This includes all types of breakpoints (breakpoints, watchpoints,\n\
16479 catchpoints, tracepoints). Use the 'source' command in another debug\n\
16480 session to restore them."),
16481 &save_cmdlist);
16482 set_cmd_completer (c, filename_completer);
16483
16484 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
16485 Save current tracepoint definitions as a script.\n\
16486 Use the 'source' command in another debug session to restore them."),
16487 &save_cmdlist);
16488 set_cmd_completer (c, filename_completer);
16489
16490 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16491 deprecate_cmd (c, "save tracepoints");
16492
16493 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
16494 Breakpoint specific settings\n\
16495 Configure various breakpoint-specific variables such as\n\
16496 pending breakpoint behavior"),
16497 &breakpoint_set_cmdlist, "set breakpoint ",
16498 0/*allow-unknown*/, &setlist);
16499 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
16500 Breakpoint specific settings\n\
16501 Configure various breakpoint-specific variables such as\n\
16502 pending breakpoint behavior"),
16503 &breakpoint_show_cmdlist, "show breakpoint ",
16504 0/*allow-unknown*/, &showlist);
16505
16506 add_setshow_auto_boolean_cmd ("pending", no_class,
16507 &pending_break_support, _("\
16508 Set debugger's behavior regarding pending breakpoints."), _("\
16509 Show debugger's behavior regarding pending breakpoints."), _("\
16510 If on, an unrecognized breakpoint location will cause gdb to create a\n\
16511 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16512 an error. If auto, an unrecognized breakpoint location results in a\n\
16513 user-query to see if a pending breakpoint should be created."),
16514 NULL,
16515 show_pending_break_support,
16516 &breakpoint_set_cmdlist,
16517 &breakpoint_show_cmdlist);
16518
16519 pending_break_support = AUTO_BOOLEAN_AUTO;
16520
16521 add_setshow_boolean_cmd ("auto-hw", no_class,
16522 &automatic_hardware_breakpoints, _("\
16523 Set automatic usage of hardware breakpoints."), _("\
16524 Show automatic usage of hardware breakpoints."), _("\
16525 If set, the debugger will automatically use hardware breakpoints for\n\
16526 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16527 a warning will be emitted for such breakpoints."),
16528 NULL,
16529 show_automatic_hardware_breakpoints,
16530 &breakpoint_set_cmdlist,
16531 &breakpoint_show_cmdlist);
16532
16533 add_setshow_boolean_cmd ("always-inserted", class_support,
16534 &always_inserted_mode, _("\
16535 Set mode for inserting breakpoints."), _("\
16536 Show mode for inserting breakpoints."), _("\
16537 When this mode is on, breakpoints are inserted immediately as soon as\n\
16538 they're created, kept inserted even when execution stops, and removed\n\
16539 only when the user deletes them. When this mode is off (the default),\n\
16540 breakpoints are inserted only when execution continues, and removed\n\
16541 when execution stops."),
16542 NULL,
16543 &show_always_inserted_mode,
16544 &breakpoint_set_cmdlist,
16545 &breakpoint_show_cmdlist);
16546
16547 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16548 condition_evaluation_enums,
16549 &condition_evaluation_mode_1, _("\
16550 Set mode of breakpoint condition evaluation."), _("\
16551 Show mode of breakpoint condition evaluation."), _("\
16552 When this is set to \"host\", breakpoint conditions will be\n\
16553 evaluated on the host's side by GDB. When it is set to \"target\",\n\
16554 breakpoint conditions will be downloaded to the target (if the target\n\
16555 supports such feature) and conditions will be evaluated on the target's side.\n\
16556 If this is set to \"auto\" (default), this will be automatically set to\n\
16557 \"target\" if it supports condition evaluation, otherwise it will\n\
16558 be set to \"gdb\""),
16559 &set_condition_evaluation_mode,
16560 &show_condition_evaluation_mode,
16561 &breakpoint_set_cmdlist,
16562 &breakpoint_show_cmdlist);
16563
16564 add_com ("break-range", class_breakpoint, break_range_command, _("\
16565 Set a breakpoint for an address range.\n\
16566 break-range START-LOCATION, END-LOCATION\n\
16567 where START-LOCATION and END-LOCATION can be one of the following:\n\
16568 LINENUM, for that line in the current file,\n\
16569 FILE:LINENUM, for that line in that file,\n\
16570 +OFFSET, for that number of lines after the current line\n\
16571 or the start of the range\n\
16572 FUNCTION, for the first line in that function,\n\
16573 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16574 *ADDRESS, for the instruction at that address.\n\
16575 \n\
16576 The breakpoint will stop execution of the inferior whenever it executes\n\
16577 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16578 range (including START-LOCATION and END-LOCATION)."));
16579
16580 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
16581 Set a dynamic printf at specified location.\n\
16582 dprintf location,format string,arg1,arg2,...\n\
16583 location may be a linespec, explicit, or address location.\n"
16584 "\n" LOCATION_HELP_STRING));
16585 set_cmd_completer (c, location_completer);
16586
16587 add_setshow_enum_cmd ("dprintf-style", class_support,
16588 dprintf_style_enums, &dprintf_style, _("\
16589 Set the style of usage for dynamic printf."), _("\
16590 Show the style of usage for dynamic printf."), _("\
16591 This setting chooses how GDB will do a dynamic printf.\n\
16592 If the value is \"gdb\", then the printing is done by GDB to its own\n\
16593 console, as with the \"printf\" command.\n\
16594 If the value is \"call\", the print is done by calling a function in your\n\
16595 program; by default printf(), but you can choose a different function or\n\
16596 output stream by setting dprintf-function and dprintf-channel."),
16597 update_dprintf_commands, NULL,
16598 &setlist, &showlist);
16599
16600 dprintf_function = xstrdup ("printf");
16601 add_setshow_string_cmd ("dprintf-function", class_support,
16602 &dprintf_function, _("\
16603 Set the function to use for dynamic printf"), _("\
16604 Show the function to use for dynamic printf"), NULL,
16605 update_dprintf_commands, NULL,
16606 &setlist, &showlist);
16607
16608 dprintf_channel = xstrdup ("");
16609 add_setshow_string_cmd ("dprintf-channel", class_support,
16610 &dprintf_channel, _("\
16611 Set the channel to use for dynamic printf"), _("\
16612 Show the channel to use for dynamic printf"), NULL,
16613 update_dprintf_commands, NULL,
16614 &setlist, &showlist);
16615
16616 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16617 &disconnected_dprintf, _("\
16618 Set whether dprintf continues after GDB disconnects."), _("\
16619 Show whether dprintf continues after GDB disconnects."), _("\
16620 Use this to let dprintf commands continue to hit and produce output\n\
16621 even if GDB disconnects or detaches from the target."),
16622 NULL,
16623 NULL,
16624 &setlist, &showlist);
16625
16626 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16627 agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16628 (target agent only) This is useful for formatted output in user-defined commands."));
16629
16630 automatic_hardware_breakpoints = 1;
16631
16632 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
16633 observer_attach_thread_exit (remove_threaded_breakpoints);
16634 }
This page took 0.3922 seconds and 4 git commands to generate.