Change breakpoint event locations to event_location_up
[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_location; \
582 BP_TMP < bp_location + bp_location_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_location + bp_location_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_location_compare - primarily by the ADDRESS. */
610
611 static struct bp_location **bp_location;
612
613 /* Number of elements of BP_LOCATION. */
614
615 static unsigned bp_location_count;
616
617 /* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
618 ADDRESS for the current elements of BP_LOCATION which get a valid
619 result from bp_location_has_shadow. You can use it for roughly
620 limiting the subrange of BP_LOCATION to scan for shadow bytes for
621 an address you need to read. */
622
623 static CORE_ADDR bp_location_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_LOCATION which get a valid result from bp_location_has_shadow.
628 You can use it for roughly limiting the subrange of BP_LOCATION to
629 scan for shadow bytes for an address you need to read. */
630
631 static CORE_ADDR bp_location_shadow_len_after_address_max;
632
633 /* The locations that no longer correspond to any breakpoint, unlinked
634 from bp_location array, but for which a hit may still be reported
635 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_location_compare function. */
924
925 static int
926 bp_location_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_location, bp_location_count,
956 sizeof (struct bp_location **),
957 bp_location_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_location
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_location_placed_address_before_address_max
1572 up to bl->address + bp_location_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_location_placed_address_before_address_max)
1578 and:
1579 bl->address + bp_location_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_location_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_location[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_LOCATION_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_location_shadow_len_after_address_max
1613 >= bl->address)
1614 && (bl->address + bp_location_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_location[bc_l]->address == bp_location[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_location_count; bc++)
1641 {
1642 struct bp_location *bl = bp_location[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_location_placed_address_before_address_max
1653 && memaddr + len <= (bl->address
1654 - bp_location_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 struct cleanup *cleanup;
4889 char *name;
4890 int ix;
4891
4892 current_uiout->text (_(" Inferior unloaded "));
4893 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4894 "removed");
4895 for (ix = 0;
4896 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4897 ix, name);
4898 ++ix)
4899 {
4900 if (ix > 0)
4901 current_uiout->text (" ");
4902 current_uiout->field_string ("library", name);
4903 current_uiout->text ("\n");
4904 }
4905
4906 do_cleanups (cleanup);
4907 }
4908
4909 if (any_added)
4910 {
4911 struct so_list *iter;
4912 int ix;
4913 struct cleanup *cleanup;
4914
4915 current_uiout->text (_(" Inferior loaded "));
4916 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4917 "added");
4918 for (ix = 0;
4919 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4920 ix, iter);
4921 ++ix)
4922 {
4923 if (ix > 0)
4924 current_uiout->text (" ");
4925 current_uiout->field_string ("library", iter->so_name);
4926 current_uiout->text ("\n");
4927 }
4928
4929 do_cleanups (cleanup);
4930 }
4931 }
4932
4933 /* Print a message indicating what happened. This is called from
4934 normal_stop(). The input to this routine is the head of the bpstat
4935 list - a list of the eventpoints that caused this stop. KIND is
4936 the target_waitkind for the stopping event. This
4937 routine calls the generic print routine for printing a message
4938 about reasons for stopping. This will print (for example) the
4939 "Breakpoint n," part of the output. The return value of this
4940 routine is one of:
4941
4942 PRINT_UNKNOWN: Means we printed nothing.
4943 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4944 code to print the location. An example is
4945 "Breakpoint 1, " which should be followed by
4946 the location.
4947 PRINT_SRC_ONLY: Means we printed something, but there is no need
4948 to also print the location part of the message.
4949 An example is the catch/throw messages, which
4950 don't require a location appended to the end.
4951 PRINT_NOTHING: We have done some printing and we don't need any
4952 further info to be printed. */
4953
4954 enum print_stop_action
4955 bpstat_print (bpstat bs, int kind)
4956 {
4957 enum print_stop_action val;
4958
4959 /* Maybe another breakpoint in the chain caused us to stop.
4960 (Currently all watchpoints go on the bpstat whether hit or not.
4961 That probably could (should) be changed, provided care is taken
4962 with respect to bpstat_explains_signal). */
4963 for (; bs; bs = bs->next)
4964 {
4965 val = print_bp_stop_message (bs);
4966 if (val == PRINT_SRC_ONLY
4967 || val == PRINT_SRC_AND_LOC
4968 || val == PRINT_NOTHING)
4969 return val;
4970 }
4971
4972 /* If we had hit a shared library event breakpoint,
4973 print_bp_stop_message would print out this message. If we hit an
4974 OS-level shared library event, do the same thing. */
4975 if (kind == TARGET_WAITKIND_LOADED)
4976 {
4977 print_solib_event (0);
4978 return PRINT_NOTHING;
4979 }
4980
4981 /* We reached the end of the chain, or we got a null BS to start
4982 with and nothing was printed. */
4983 return PRINT_UNKNOWN;
4984 }
4985
4986 /* Evaluate the expression EXP and return 1 if value is zero.
4987 This returns the inverse of the condition because it is called
4988 from catch_errors which returns 0 if an exception happened, and if an
4989 exception happens we want execution to stop.
4990 The argument is a "struct expression *" that has been cast to a
4991 "void *" to make it pass through catch_errors. */
4992
4993 static int
4994 breakpoint_cond_eval (void *exp)
4995 {
4996 struct value *mark = value_mark ();
4997 int i = !value_true (evaluate_expression ((struct expression *) exp));
4998
4999 value_free_to_mark (mark);
5000 return i;
5001 }
5002
5003 /* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
5004
5005 static bpstat
5006 bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
5007 {
5008 bpstat bs;
5009
5010 bs = (bpstat) xmalloc (sizeof (*bs));
5011 bs->next = NULL;
5012 **bs_link_pointer = bs;
5013 *bs_link_pointer = &bs->next;
5014 bs->breakpoint_at = bl->owner;
5015 bs->bp_location_at = bl;
5016 incref_bp_location (bl);
5017 /* If the condition is false, etc., don't do the commands. */
5018 bs->commands = NULL;
5019 bs->old_val = NULL;
5020 bs->print_it = print_it_normal;
5021 return bs;
5022 }
5023 \f
5024 /* The target has stopped with waitstatus WS. Check if any hardware
5025 watchpoints have triggered, according to the target. */
5026
5027 int
5028 watchpoints_triggered (struct target_waitstatus *ws)
5029 {
5030 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
5031 CORE_ADDR addr;
5032 struct breakpoint *b;
5033
5034 if (!stopped_by_watchpoint)
5035 {
5036 /* We were not stopped by a watchpoint. Mark all watchpoints
5037 as not triggered. */
5038 ALL_BREAKPOINTS (b)
5039 if (is_hardware_watchpoint (b))
5040 {
5041 struct watchpoint *w = (struct watchpoint *) b;
5042
5043 w->watchpoint_triggered = watch_triggered_no;
5044 }
5045
5046 return 0;
5047 }
5048
5049 if (!target_stopped_data_address (&current_target, &addr))
5050 {
5051 /* We were stopped by a watchpoint, but we don't know where.
5052 Mark all watchpoints as unknown. */
5053 ALL_BREAKPOINTS (b)
5054 if (is_hardware_watchpoint (b))
5055 {
5056 struct watchpoint *w = (struct watchpoint *) b;
5057
5058 w->watchpoint_triggered = watch_triggered_unknown;
5059 }
5060
5061 return 1;
5062 }
5063
5064 /* The target could report the data address. Mark watchpoints
5065 affected by this data address as triggered, and all others as not
5066 triggered. */
5067
5068 ALL_BREAKPOINTS (b)
5069 if (is_hardware_watchpoint (b))
5070 {
5071 struct watchpoint *w = (struct watchpoint *) b;
5072 struct bp_location *loc;
5073
5074 w->watchpoint_triggered = watch_triggered_no;
5075 for (loc = b->loc; loc; loc = loc->next)
5076 {
5077 if (is_masked_watchpoint (b))
5078 {
5079 CORE_ADDR newaddr = addr & w->hw_wp_mask;
5080 CORE_ADDR start = loc->address & w->hw_wp_mask;
5081
5082 if (newaddr == start)
5083 {
5084 w->watchpoint_triggered = watch_triggered_yes;
5085 break;
5086 }
5087 }
5088 /* Exact match not required. Within range is sufficient. */
5089 else if (target_watchpoint_addr_within_range (&current_target,
5090 addr, loc->address,
5091 loc->length))
5092 {
5093 w->watchpoint_triggered = watch_triggered_yes;
5094 break;
5095 }
5096 }
5097 }
5098
5099 return 1;
5100 }
5101
5102 /* Possible return values for watchpoint_check (this can't be an enum
5103 because of check_errors). */
5104 /* The watchpoint has been deleted. */
5105 #define WP_DELETED 1
5106 /* The value has changed. */
5107 #define WP_VALUE_CHANGED 2
5108 /* The value has not changed. */
5109 #define WP_VALUE_NOT_CHANGED 3
5110 /* Ignore this watchpoint, no matter if the value changed or not. */
5111 #define WP_IGNORE 4
5112
5113 #define BP_TEMPFLAG 1
5114 #define BP_HARDWAREFLAG 2
5115
5116 /* Evaluate watchpoint condition expression and check if its value
5117 changed.
5118
5119 P should be a pointer to struct bpstat, but is defined as a void *
5120 in order for this function to be usable with catch_errors. */
5121
5122 static int
5123 watchpoint_check (void *p)
5124 {
5125 bpstat bs = (bpstat) p;
5126 struct watchpoint *b;
5127 struct frame_info *fr;
5128 int within_current_scope;
5129
5130 /* BS is built from an existing struct breakpoint. */
5131 gdb_assert (bs->breakpoint_at != NULL);
5132 b = (struct watchpoint *) bs->breakpoint_at;
5133
5134 /* If this is a local watchpoint, we only want to check if the
5135 watchpoint frame is in scope if the current thread is the thread
5136 that was used to create the watchpoint. */
5137 if (!watchpoint_in_thread_scope (b))
5138 return WP_IGNORE;
5139
5140 if (b->exp_valid_block == NULL)
5141 within_current_scope = 1;
5142 else
5143 {
5144 struct frame_info *frame = get_current_frame ();
5145 struct gdbarch *frame_arch = get_frame_arch (frame);
5146 CORE_ADDR frame_pc = get_frame_pc (frame);
5147
5148 /* stack_frame_destroyed_p() returns a non-zero value if we're
5149 still in the function but the stack frame has already been
5150 invalidated. Since we can't rely on the values of local
5151 variables after the stack has been destroyed, we are treating
5152 the watchpoint in that state as `not changed' without further
5153 checking. Don't mark watchpoints as changed if the current
5154 frame is in an epilogue - even if they are in some other
5155 frame, our view of the stack is likely to be wrong and
5156 frame_find_by_id could error out. */
5157 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
5158 return WP_IGNORE;
5159
5160 fr = frame_find_by_id (b->watchpoint_frame);
5161 within_current_scope = (fr != NULL);
5162
5163 /* If we've gotten confused in the unwinder, we might have
5164 returned a frame that can't describe this variable. */
5165 if (within_current_scope)
5166 {
5167 struct symbol *function;
5168
5169 function = get_frame_function (fr);
5170 if (function == NULL
5171 || !contained_in (b->exp_valid_block,
5172 SYMBOL_BLOCK_VALUE (function)))
5173 within_current_scope = 0;
5174 }
5175
5176 if (within_current_scope)
5177 /* If we end up stopping, the current frame will get selected
5178 in normal_stop. So this call to select_frame won't affect
5179 the user. */
5180 select_frame (fr);
5181 }
5182
5183 if (within_current_scope)
5184 {
5185 /* We use value_{,free_to_}mark because it could be a *long*
5186 time before we return to the command level and call
5187 free_all_values. We can't call free_all_values because we
5188 might be in the middle of evaluating a function call. */
5189
5190 int pc = 0;
5191 struct value *mark;
5192 struct value *new_val;
5193
5194 if (is_masked_watchpoint (&b->base))
5195 /* Since we don't know the exact trigger address (from
5196 stopped_data_address), just tell the user we've triggered
5197 a mask watchpoint. */
5198 return WP_VALUE_CHANGED;
5199
5200 mark = value_mark ();
5201 fetch_subexp_value (b->exp.get (), &pc, &new_val, NULL, NULL, 0);
5202
5203 if (b->val_bitsize != 0)
5204 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5205
5206 /* We use value_equal_contents instead of value_equal because
5207 the latter coerces an array to a pointer, thus comparing just
5208 the address of the array instead of its contents. This is
5209 not what we want. */
5210 if ((b->val != NULL) != (new_val != NULL)
5211 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
5212 {
5213 if (new_val != NULL)
5214 {
5215 release_value (new_val);
5216 value_free_to_mark (mark);
5217 }
5218 bs->old_val = b->val;
5219 b->val = new_val;
5220 b->val_valid = 1;
5221 return WP_VALUE_CHANGED;
5222 }
5223 else
5224 {
5225 /* Nothing changed. */
5226 value_free_to_mark (mark);
5227 return WP_VALUE_NOT_CHANGED;
5228 }
5229 }
5230 else
5231 {
5232 /* This seems like the only logical thing to do because
5233 if we temporarily ignored the watchpoint, then when
5234 we reenter the block in which it is valid it contains
5235 garbage (in the case of a function, it may have two
5236 garbage values, one before and one after the prologue).
5237 So we can't even detect the first assignment to it and
5238 watch after that (since the garbage may or may not equal
5239 the first value assigned). */
5240 /* We print all the stop information in
5241 breakpoint_ops->print_it, but in this case, by the time we
5242 call breakpoint_ops->print_it this bp will be deleted
5243 already. So we have no choice but print the information
5244 here. */
5245
5246 SWITCH_THRU_ALL_UIS ()
5247 {
5248 struct ui_out *uiout = current_uiout;
5249
5250 if (uiout->is_mi_like_p ())
5251 uiout->field_string
5252 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
5253 uiout->text ("\nWatchpoint ");
5254 uiout->field_int ("wpnum", b->base.number);
5255 uiout->text (" deleted because the program has left the block in\n"
5256 "which its expression is valid.\n");
5257 }
5258
5259 /* Make sure the watchpoint's commands aren't executed. */
5260 decref_counted_command_line (&b->base.commands);
5261 watchpoint_del_at_next_stop (b);
5262
5263 return WP_DELETED;
5264 }
5265 }
5266
5267 /* Return true if it looks like target has stopped due to hitting
5268 breakpoint location BL. This function does not check if we should
5269 stop, only if BL explains the stop. */
5270
5271 static int
5272 bpstat_check_location (const struct bp_location *bl,
5273 struct address_space *aspace, CORE_ADDR bp_addr,
5274 const struct target_waitstatus *ws)
5275 {
5276 struct breakpoint *b = bl->owner;
5277
5278 /* BL is from an existing breakpoint. */
5279 gdb_assert (b != NULL);
5280
5281 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
5282 }
5283
5284 /* Determine if the watched values have actually changed, and we
5285 should stop. If not, set BS->stop to 0. */
5286
5287 static void
5288 bpstat_check_watchpoint (bpstat bs)
5289 {
5290 const struct bp_location *bl;
5291 struct watchpoint *b;
5292
5293 /* BS is built for existing struct breakpoint. */
5294 bl = bs->bp_location_at;
5295 gdb_assert (bl != NULL);
5296 b = (struct watchpoint *) bs->breakpoint_at;
5297 gdb_assert (b != NULL);
5298
5299 {
5300 int must_check_value = 0;
5301
5302 if (b->base.type == bp_watchpoint)
5303 /* For a software watchpoint, we must always check the
5304 watched value. */
5305 must_check_value = 1;
5306 else if (b->watchpoint_triggered == watch_triggered_yes)
5307 /* We have a hardware watchpoint (read, write, or access)
5308 and the target earlier reported an address watched by
5309 this watchpoint. */
5310 must_check_value = 1;
5311 else if (b->watchpoint_triggered == watch_triggered_unknown
5312 && b->base.type == bp_hardware_watchpoint)
5313 /* We were stopped by a hardware watchpoint, but the target could
5314 not report the data address. We must check the watchpoint's
5315 value. Access and read watchpoints are out of luck; without
5316 a data address, we can't figure it out. */
5317 must_check_value = 1;
5318
5319 if (must_check_value)
5320 {
5321 char *message
5322 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
5323 b->base.number);
5324 struct cleanup *cleanups = make_cleanup (xfree, message);
5325 int e = catch_errors (watchpoint_check, bs, message,
5326 RETURN_MASK_ALL);
5327 do_cleanups (cleanups);
5328 switch (e)
5329 {
5330 case WP_DELETED:
5331 /* We've already printed what needs to be printed. */
5332 bs->print_it = print_it_done;
5333 /* Stop. */
5334 break;
5335 case WP_IGNORE:
5336 bs->print_it = print_it_noop;
5337 bs->stop = 0;
5338 break;
5339 case WP_VALUE_CHANGED:
5340 if (b->base.type == bp_read_watchpoint)
5341 {
5342 /* There are two cases to consider here:
5343
5344 1. We're watching the triggered memory for reads.
5345 In that case, trust the target, and always report
5346 the watchpoint hit to the user. Even though
5347 reads don't cause value changes, the value may
5348 have changed since the last time it was read, and
5349 since we're not trapping writes, we will not see
5350 those, and as such we should ignore our notion of
5351 old value.
5352
5353 2. We're watching the triggered memory for both
5354 reads and writes. There are two ways this may
5355 happen:
5356
5357 2.1. This is a target that can't break on data
5358 reads only, but can break on accesses (reads or
5359 writes), such as e.g., x86. We detect this case
5360 at the time we try to insert read watchpoints.
5361
5362 2.2. Otherwise, the target supports read
5363 watchpoints, but, the user set an access or write
5364 watchpoint watching the same memory as this read
5365 watchpoint.
5366
5367 If we're watching memory writes as well as reads,
5368 ignore watchpoint hits when we find that the
5369 value hasn't changed, as reads don't cause
5370 changes. This still gives false positives when
5371 the program writes the same value to memory as
5372 what there was already in memory (we will confuse
5373 it for a read), but it's much better than
5374 nothing. */
5375
5376 int other_write_watchpoint = 0;
5377
5378 if (bl->watchpoint_type == hw_read)
5379 {
5380 struct breakpoint *other_b;
5381
5382 ALL_BREAKPOINTS (other_b)
5383 if (other_b->type == bp_hardware_watchpoint
5384 || other_b->type == bp_access_watchpoint)
5385 {
5386 struct watchpoint *other_w =
5387 (struct watchpoint *) other_b;
5388
5389 if (other_w->watchpoint_triggered
5390 == watch_triggered_yes)
5391 {
5392 other_write_watchpoint = 1;
5393 break;
5394 }
5395 }
5396 }
5397
5398 if (other_write_watchpoint
5399 || bl->watchpoint_type == hw_access)
5400 {
5401 /* We're watching the same memory for writes,
5402 and the value changed since the last time we
5403 updated it, so this trap must be for a write.
5404 Ignore it. */
5405 bs->print_it = print_it_noop;
5406 bs->stop = 0;
5407 }
5408 }
5409 break;
5410 case WP_VALUE_NOT_CHANGED:
5411 if (b->base.type == bp_hardware_watchpoint
5412 || b->base.type == bp_watchpoint)
5413 {
5414 /* Don't stop: write watchpoints shouldn't fire if
5415 the value hasn't changed. */
5416 bs->print_it = print_it_noop;
5417 bs->stop = 0;
5418 }
5419 /* Stop. */
5420 break;
5421 default:
5422 /* Can't happen. */
5423 case 0:
5424 /* Error from catch_errors. */
5425 {
5426 SWITCH_THRU_ALL_UIS ()
5427 {
5428 printf_filtered (_("Watchpoint %d deleted.\n"),
5429 b->base.number);
5430 }
5431 watchpoint_del_at_next_stop (b);
5432 /* We've already printed what needs to be printed. */
5433 bs->print_it = print_it_done;
5434 }
5435 break;
5436 }
5437 }
5438 else /* must_check_value == 0 */
5439 {
5440 /* This is a case where some watchpoint(s) triggered, but
5441 not at the address of this watchpoint, or else no
5442 watchpoint triggered after all. So don't print
5443 anything for this watchpoint. */
5444 bs->print_it = print_it_noop;
5445 bs->stop = 0;
5446 }
5447 }
5448 }
5449
5450 /* For breakpoints that are currently marked as telling gdb to stop,
5451 check conditions (condition proper, frame, thread and ignore count)
5452 of breakpoint referred to by BS. If we should not stop for this
5453 breakpoint, set BS->stop to 0. */
5454
5455 static void
5456 bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5457 {
5458 const struct bp_location *bl;
5459 struct breakpoint *b;
5460 int value_is_zero = 0;
5461 struct expression *cond;
5462
5463 gdb_assert (bs->stop);
5464
5465 /* BS is built for existing struct breakpoint. */
5466 bl = bs->bp_location_at;
5467 gdb_assert (bl != NULL);
5468 b = bs->breakpoint_at;
5469 gdb_assert (b != NULL);
5470
5471 /* Even if the target evaluated the condition on its end and notified GDB, we
5472 need to do so again since GDB does not know if we stopped due to a
5473 breakpoint or a single step breakpoint. */
5474
5475 if (frame_id_p (b->frame_id)
5476 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
5477 {
5478 bs->stop = 0;
5479 return;
5480 }
5481
5482 /* If this is a thread/task-specific breakpoint, don't waste cpu
5483 evaluating the condition if this isn't the specified
5484 thread/task. */
5485 if ((b->thread != -1 && b->thread != ptid_to_global_thread_id (ptid))
5486 || (b->task != 0 && b->task != ada_get_task_number (ptid)))
5487
5488 {
5489 bs->stop = 0;
5490 return;
5491 }
5492
5493 /* Evaluate extension language breakpoints that have a "stop" method
5494 implemented. */
5495 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
5496
5497 if (is_watchpoint (b))
5498 {
5499 struct watchpoint *w = (struct watchpoint *) b;
5500
5501 cond = w->cond_exp.get ();
5502 }
5503 else
5504 cond = bl->cond.get ();
5505
5506 if (cond && b->disposition != disp_del_at_next_stop)
5507 {
5508 int within_current_scope = 1;
5509 struct watchpoint * w;
5510
5511 /* We use value_mark and value_free_to_mark because it could
5512 be a long time before we return to the command level and
5513 call free_all_values. We can't call free_all_values
5514 because we might be in the middle of evaluating a
5515 function call. */
5516 struct value *mark = value_mark ();
5517
5518 if (is_watchpoint (b))
5519 w = (struct watchpoint *) b;
5520 else
5521 w = NULL;
5522
5523 /* Need to select the frame, with all that implies so that
5524 the conditions will have the right context. Because we
5525 use the frame, we will not see an inlined function's
5526 variables when we arrive at a breakpoint at the start
5527 of the inlined function; the current frame will be the
5528 call site. */
5529 if (w == NULL || w->cond_exp_valid_block == NULL)
5530 select_frame (get_current_frame ());
5531 else
5532 {
5533 struct frame_info *frame;
5534
5535 /* For local watchpoint expressions, which particular
5536 instance of a local is being watched matters, so we
5537 keep track of the frame to evaluate the expression
5538 in. To evaluate the condition however, it doesn't
5539 really matter which instantiation of the function
5540 where the condition makes sense triggers the
5541 watchpoint. This allows an expression like "watch
5542 global if q > 10" set in `func', catch writes to
5543 global on all threads that call `func', or catch
5544 writes on all recursive calls of `func' by a single
5545 thread. We simply always evaluate the condition in
5546 the innermost frame that's executing where it makes
5547 sense to evaluate the condition. It seems
5548 intuitive. */
5549 frame = block_innermost_frame (w->cond_exp_valid_block);
5550 if (frame != NULL)
5551 select_frame (frame);
5552 else
5553 within_current_scope = 0;
5554 }
5555 if (within_current_scope)
5556 value_is_zero
5557 = catch_errors (breakpoint_cond_eval, cond,
5558 "Error in testing breakpoint condition:\n",
5559 RETURN_MASK_ALL);
5560 else
5561 {
5562 warning (_("Watchpoint condition cannot be tested "
5563 "in the current scope"));
5564 /* If we failed to set the right context for this
5565 watchpoint, unconditionally report it. */
5566 value_is_zero = 0;
5567 }
5568 /* FIXME-someday, should give breakpoint #. */
5569 value_free_to_mark (mark);
5570 }
5571
5572 if (cond && value_is_zero)
5573 {
5574 bs->stop = 0;
5575 }
5576 else if (b->ignore_count > 0)
5577 {
5578 b->ignore_count--;
5579 bs->stop = 0;
5580 /* Increase the hit count even though we don't stop. */
5581 ++(b->hit_count);
5582 observer_notify_breakpoint_modified (b);
5583 }
5584 }
5585
5586 /* Returns true if we need to track moribund locations of LOC's type
5587 on the current target. */
5588
5589 static int
5590 need_moribund_for_location_type (struct bp_location *loc)
5591 {
5592 return ((loc->loc_type == bp_loc_software_breakpoint
5593 && !target_supports_stopped_by_sw_breakpoint ())
5594 || (loc->loc_type == bp_loc_hardware_breakpoint
5595 && !target_supports_stopped_by_hw_breakpoint ()));
5596 }
5597
5598
5599 /* Get a bpstat associated with having just stopped at address
5600 BP_ADDR in thread PTID.
5601
5602 Determine whether we stopped at a breakpoint, etc, or whether we
5603 don't understand this stop. Result is a chain of bpstat's such
5604 that:
5605
5606 if we don't understand the stop, the result is a null pointer.
5607
5608 if we understand why we stopped, the result is not null.
5609
5610 Each element of the chain refers to a particular breakpoint or
5611 watchpoint at which we have stopped. (We may have stopped for
5612 several reasons concurrently.)
5613
5614 Each element of the chain has valid next, breakpoint_at,
5615 commands, FIXME??? fields. */
5616
5617 bpstat
5618 bpstat_stop_status (struct address_space *aspace,
5619 CORE_ADDR bp_addr, ptid_t ptid,
5620 const struct target_waitstatus *ws)
5621 {
5622 struct breakpoint *b = NULL;
5623 struct bp_location *bl;
5624 struct bp_location *loc;
5625 /* First item of allocated bpstat's. */
5626 bpstat bs_head = NULL, *bs_link = &bs_head;
5627 /* Pointer to the last thing in the chain currently. */
5628 bpstat bs;
5629 int ix;
5630 int need_remove_insert;
5631 int removed_any;
5632
5633 /* First, build the bpstat chain with locations that explain a
5634 target stop, while being careful to not set the target running,
5635 as that may invalidate locations (in particular watchpoint
5636 locations are recreated). Resuming will happen here with
5637 breakpoint conditions or watchpoint expressions that include
5638 inferior function calls. */
5639
5640 ALL_BREAKPOINTS (b)
5641 {
5642 if (!breakpoint_enabled (b))
5643 continue;
5644
5645 for (bl = b->loc; bl != NULL; bl = bl->next)
5646 {
5647 /* For hardware watchpoints, we look only at the first
5648 location. The watchpoint_check function will work on the
5649 entire expression, not the individual locations. For
5650 read watchpoints, the watchpoints_triggered function has
5651 checked all locations already. */
5652 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5653 break;
5654
5655 if (!bl->enabled || bl->shlib_disabled)
5656 continue;
5657
5658 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
5659 continue;
5660
5661 /* Come here if it's a watchpoint, or if the break address
5662 matches. */
5663
5664 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
5665 explain stop. */
5666
5667 /* Assume we stop. Should we find a watchpoint that is not
5668 actually triggered, or if the condition of the breakpoint
5669 evaluates as false, we'll reset 'stop' to 0. */
5670 bs->stop = 1;
5671 bs->print = 1;
5672
5673 /* If this is a scope breakpoint, mark the associated
5674 watchpoint as triggered so that we will handle the
5675 out-of-scope event. We'll get to the watchpoint next
5676 iteration. */
5677 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
5678 {
5679 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5680
5681 w->watchpoint_triggered = watch_triggered_yes;
5682 }
5683 }
5684 }
5685
5686 /* Check if a moribund breakpoint explains the stop. */
5687 if (!target_supports_stopped_by_sw_breakpoint ()
5688 || !target_supports_stopped_by_hw_breakpoint ())
5689 {
5690 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
5691 {
5692 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5693 && need_moribund_for_location_type (loc))
5694 {
5695 bs = bpstat_alloc (loc, &bs_link);
5696 /* For hits of moribund locations, we should just proceed. */
5697 bs->stop = 0;
5698 bs->print = 0;
5699 bs->print_it = print_it_noop;
5700 }
5701 }
5702 }
5703
5704 /* A bit of special processing for shlib breakpoints. We need to
5705 process solib loading here, so that the lists of loaded and
5706 unloaded libraries are correct before we handle "catch load" and
5707 "catch unload". */
5708 for (bs = bs_head; bs != NULL; bs = bs->next)
5709 {
5710 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
5711 {
5712 handle_solib_event ();
5713 break;
5714 }
5715 }
5716
5717 /* Now go through the locations that caused the target to stop, and
5718 check whether we're interested in reporting this stop to higher
5719 layers, or whether we should resume the target transparently. */
5720
5721 removed_any = 0;
5722
5723 for (bs = bs_head; bs != NULL; bs = bs->next)
5724 {
5725 if (!bs->stop)
5726 continue;
5727
5728 b = bs->breakpoint_at;
5729 b->ops->check_status (bs);
5730 if (bs->stop)
5731 {
5732 bpstat_check_breakpoint_conditions (bs, ptid);
5733
5734 if (bs->stop)
5735 {
5736 ++(b->hit_count);
5737 observer_notify_breakpoint_modified (b);
5738
5739 /* We will stop here. */
5740 if (b->disposition == disp_disable)
5741 {
5742 --(b->enable_count);
5743 if (b->enable_count <= 0)
5744 b->enable_state = bp_disabled;
5745 removed_any = 1;
5746 }
5747 if (b->silent)
5748 bs->print = 0;
5749 bs->commands = b->commands;
5750 incref_counted_command_line (bs->commands);
5751 if (command_line_is_silent (bs->commands
5752 ? bs->commands->commands : NULL))
5753 bs->print = 0;
5754
5755 b->ops->after_condition_true (bs);
5756 }
5757
5758 }
5759
5760 /* Print nothing for this entry if we don't stop or don't
5761 print. */
5762 if (!bs->stop || !bs->print)
5763 bs->print_it = print_it_noop;
5764 }
5765
5766 /* If we aren't stopping, the value of some hardware watchpoint may
5767 not have changed, but the intermediate memory locations we are
5768 watching may have. Don't bother if we're stopping; this will get
5769 done later. */
5770 need_remove_insert = 0;
5771 if (! bpstat_causes_stop (bs_head))
5772 for (bs = bs_head; bs != NULL; bs = bs->next)
5773 if (!bs->stop
5774 && bs->breakpoint_at
5775 && is_hardware_watchpoint (bs->breakpoint_at))
5776 {
5777 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5778
5779 update_watchpoint (w, 0 /* don't reparse. */);
5780 need_remove_insert = 1;
5781 }
5782
5783 if (need_remove_insert)
5784 update_global_location_list (UGLL_MAY_INSERT);
5785 else if (removed_any)
5786 update_global_location_list (UGLL_DONT_INSERT);
5787
5788 return bs_head;
5789 }
5790
5791 static void
5792 handle_jit_event (void)
5793 {
5794 struct frame_info *frame;
5795 struct gdbarch *gdbarch;
5796
5797 if (debug_infrun)
5798 fprintf_unfiltered (gdb_stdlog, "handling bp_jit_event\n");
5799
5800 /* Switch terminal for any messages produced by
5801 breakpoint_re_set. */
5802 target_terminal_ours_for_output ();
5803
5804 frame = get_current_frame ();
5805 gdbarch = get_frame_arch (frame);
5806
5807 jit_event_handler (gdbarch);
5808
5809 target_terminal_inferior ();
5810 }
5811
5812 /* Prepare WHAT final decision for infrun. */
5813
5814 /* Decide what infrun needs to do with this bpstat. */
5815
5816 struct bpstat_what
5817 bpstat_what (bpstat bs_head)
5818 {
5819 struct bpstat_what retval;
5820 bpstat bs;
5821
5822 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
5823 retval.call_dummy = STOP_NONE;
5824 retval.is_longjmp = 0;
5825
5826 for (bs = bs_head; bs != NULL; bs = bs->next)
5827 {
5828 /* Extract this BS's action. After processing each BS, we check
5829 if its action overrides all we've seem so far. */
5830 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5831 enum bptype bptype;
5832
5833 if (bs->breakpoint_at == NULL)
5834 {
5835 /* I suspect this can happen if it was a momentary
5836 breakpoint which has since been deleted. */
5837 bptype = bp_none;
5838 }
5839 else
5840 bptype = bs->breakpoint_at->type;
5841
5842 switch (bptype)
5843 {
5844 case bp_none:
5845 break;
5846 case bp_breakpoint:
5847 case bp_hardware_breakpoint:
5848 case bp_single_step:
5849 case bp_until:
5850 case bp_finish:
5851 case bp_shlib_event:
5852 if (bs->stop)
5853 {
5854 if (bs->print)
5855 this_action = BPSTAT_WHAT_STOP_NOISY;
5856 else
5857 this_action = BPSTAT_WHAT_STOP_SILENT;
5858 }
5859 else
5860 this_action = BPSTAT_WHAT_SINGLE;
5861 break;
5862 case bp_watchpoint:
5863 case bp_hardware_watchpoint:
5864 case bp_read_watchpoint:
5865 case bp_access_watchpoint:
5866 if (bs->stop)
5867 {
5868 if (bs->print)
5869 this_action = BPSTAT_WHAT_STOP_NOISY;
5870 else
5871 this_action = BPSTAT_WHAT_STOP_SILENT;
5872 }
5873 else
5874 {
5875 /* There was a watchpoint, but we're not stopping.
5876 This requires no further action. */
5877 }
5878 break;
5879 case bp_longjmp:
5880 case bp_longjmp_call_dummy:
5881 case bp_exception:
5882 if (bs->stop)
5883 {
5884 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5885 retval.is_longjmp = bptype != bp_exception;
5886 }
5887 else
5888 this_action = BPSTAT_WHAT_SINGLE;
5889 break;
5890 case bp_longjmp_resume:
5891 case bp_exception_resume:
5892 if (bs->stop)
5893 {
5894 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5895 retval.is_longjmp = bptype == bp_longjmp_resume;
5896 }
5897 else
5898 this_action = BPSTAT_WHAT_SINGLE;
5899 break;
5900 case bp_step_resume:
5901 if (bs->stop)
5902 this_action = BPSTAT_WHAT_STEP_RESUME;
5903 else
5904 {
5905 /* It is for the wrong frame. */
5906 this_action = BPSTAT_WHAT_SINGLE;
5907 }
5908 break;
5909 case bp_hp_step_resume:
5910 if (bs->stop)
5911 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5912 else
5913 {
5914 /* It is for the wrong frame. */
5915 this_action = BPSTAT_WHAT_SINGLE;
5916 }
5917 break;
5918 case bp_watchpoint_scope:
5919 case bp_thread_event:
5920 case bp_overlay_event:
5921 case bp_longjmp_master:
5922 case bp_std_terminate_master:
5923 case bp_exception_master:
5924 this_action = BPSTAT_WHAT_SINGLE;
5925 break;
5926 case bp_catchpoint:
5927 if (bs->stop)
5928 {
5929 if (bs->print)
5930 this_action = BPSTAT_WHAT_STOP_NOISY;
5931 else
5932 this_action = BPSTAT_WHAT_STOP_SILENT;
5933 }
5934 else
5935 {
5936 /* There was a catchpoint, but we're not stopping.
5937 This requires no further action. */
5938 }
5939 break;
5940 case bp_jit_event:
5941 this_action = BPSTAT_WHAT_SINGLE;
5942 break;
5943 case bp_call_dummy:
5944 /* Make sure the action is stop (silent or noisy),
5945 so infrun.c pops the dummy frame. */
5946 retval.call_dummy = STOP_STACK_DUMMY;
5947 this_action = BPSTAT_WHAT_STOP_SILENT;
5948 break;
5949 case bp_std_terminate:
5950 /* Make sure the action is stop (silent or noisy),
5951 so infrun.c pops the dummy frame. */
5952 retval.call_dummy = STOP_STD_TERMINATE;
5953 this_action = BPSTAT_WHAT_STOP_SILENT;
5954 break;
5955 case bp_tracepoint:
5956 case bp_fast_tracepoint:
5957 case bp_static_tracepoint:
5958 /* Tracepoint hits should not be reported back to GDB, and
5959 if one got through somehow, it should have been filtered
5960 out already. */
5961 internal_error (__FILE__, __LINE__,
5962 _("bpstat_what: tracepoint encountered"));
5963 break;
5964 case bp_gnu_ifunc_resolver:
5965 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5966 this_action = BPSTAT_WHAT_SINGLE;
5967 break;
5968 case bp_gnu_ifunc_resolver_return:
5969 /* The breakpoint will be removed, execution will restart from the
5970 PC of the former breakpoint. */
5971 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5972 break;
5973
5974 case bp_dprintf:
5975 if (bs->stop)
5976 this_action = BPSTAT_WHAT_STOP_SILENT;
5977 else
5978 this_action = BPSTAT_WHAT_SINGLE;
5979 break;
5980
5981 default:
5982 internal_error (__FILE__, __LINE__,
5983 _("bpstat_what: unhandled bptype %d"), (int) bptype);
5984 }
5985
5986 retval.main_action = std::max (retval.main_action, this_action);
5987 }
5988
5989 return retval;
5990 }
5991
5992 void
5993 bpstat_run_callbacks (bpstat bs_head)
5994 {
5995 bpstat bs;
5996
5997 for (bs = bs_head; bs != NULL; bs = bs->next)
5998 {
5999 struct breakpoint *b = bs->breakpoint_at;
6000
6001 if (b == NULL)
6002 continue;
6003 switch (b->type)
6004 {
6005 case bp_jit_event:
6006 handle_jit_event ();
6007 break;
6008 case bp_gnu_ifunc_resolver:
6009 gnu_ifunc_resolver_stop (b);
6010 break;
6011 case bp_gnu_ifunc_resolver_return:
6012 gnu_ifunc_resolver_return_stop (b);
6013 break;
6014 }
6015 }
6016 }
6017
6018 /* Nonzero if we should step constantly (e.g. watchpoints on machines
6019 without hardware support). This isn't related to a specific bpstat,
6020 just to things like whether watchpoints are set. */
6021
6022 int
6023 bpstat_should_step (void)
6024 {
6025 struct breakpoint *b;
6026
6027 ALL_BREAKPOINTS (b)
6028 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
6029 return 1;
6030 return 0;
6031 }
6032
6033 int
6034 bpstat_causes_stop (bpstat bs)
6035 {
6036 for (; bs != NULL; bs = bs->next)
6037 if (bs->stop)
6038 return 1;
6039
6040 return 0;
6041 }
6042
6043 \f
6044
6045 /* Compute a string of spaces suitable to indent the next line
6046 so it starts at the position corresponding to the table column
6047 named COL_NAME in the currently active table of UIOUT. */
6048
6049 static char *
6050 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
6051 {
6052 static char wrap_indent[80];
6053 int i, total_width, width, align;
6054 const char *text;
6055
6056 total_width = 0;
6057 for (i = 1; uiout->query_table_field (i, &width, &align, &text); i++)
6058 {
6059 if (strcmp (text, col_name) == 0)
6060 {
6061 gdb_assert (total_width < sizeof wrap_indent);
6062 memset (wrap_indent, ' ', total_width);
6063 wrap_indent[total_width] = 0;
6064
6065 return wrap_indent;
6066 }
6067
6068 total_width += width + 1;
6069 }
6070
6071 return NULL;
6072 }
6073
6074 /* Determine if the locations of this breakpoint will have their conditions
6075 evaluated by the target, host or a mix of both. Returns the following:
6076
6077 "host": Host evals condition.
6078 "host or target": Host or Target evals condition.
6079 "target": Target evals condition.
6080 */
6081
6082 static const char *
6083 bp_condition_evaluator (struct breakpoint *b)
6084 {
6085 struct bp_location *bl;
6086 char host_evals = 0;
6087 char target_evals = 0;
6088
6089 if (!b)
6090 return NULL;
6091
6092 if (!is_breakpoint (b))
6093 return NULL;
6094
6095 if (gdb_evaluates_breakpoint_condition_p ()
6096 || !target_supports_evaluation_of_breakpoint_conditions ())
6097 return condition_evaluation_host;
6098
6099 for (bl = b->loc; bl; bl = bl->next)
6100 {
6101 if (bl->cond_bytecode)
6102 target_evals++;
6103 else
6104 host_evals++;
6105 }
6106
6107 if (host_evals && target_evals)
6108 return condition_evaluation_both;
6109 else if (target_evals)
6110 return condition_evaluation_target;
6111 else
6112 return condition_evaluation_host;
6113 }
6114
6115 /* Determine the breakpoint location's condition evaluator. This is
6116 similar to bp_condition_evaluator, but for locations. */
6117
6118 static const char *
6119 bp_location_condition_evaluator (struct bp_location *bl)
6120 {
6121 if (bl && !is_breakpoint (bl->owner))
6122 return NULL;
6123
6124 if (gdb_evaluates_breakpoint_condition_p ()
6125 || !target_supports_evaluation_of_breakpoint_conditions ())
6126 return condition_evaluation_host;
6127
6128 if (bl && bl->cond_bytecode)
6129 return condition_evaluation_target;
6130 else
6131 return condition_evaluation_host;
6132 }
6133
6134 /* Print the LOC location out of the list of B->LOC locations. */
6135
6136 static void
6137 print_breakpoint_location (struct breakpoint *b,
6138 struct bp_location *loc)
6139 {
6140 struct ui_out *uiout = current_uiout;
6141 struct cleanup *old_chain = save_current_program_space ();
6142
6143 if (loc != NULL && loc->shlib_disabled)
6144 loc = NULL;
6145
6146 if (loc != NULL)
6147 set_current_program_space (loc->pspace);
6148
6149 if (b->display_canonical)
6150 uiout->field_string ("what", event_location_to_string (b->location.get ()));
6151 else if (loc && loc->symtab)
6152 {
6153 struct symbol *sym
6154 = find_pc_sect_function (loc->address, loc->section);
6155 if (sym)
6156 {
6157 uiout->text ("in ");
6158 uiout->field_string ("func", SYMBOL_PRINT_NAME (sym));
6159 uiout->text (" ");
6160 uiout->wrap_hint (wrap_indent_at_field (uiout, "what"));
6161 uiout->text ("at ");
6162 }
6163 uiout->field_string ("file",
6164 symtab_to_filename_for_display (loc->symtab));
6165 uiout->text (":");
6166
6167 if (uiout->is_mi_like_p ())
6168 uiout->field_string ("fullname", symtab_to_fullname (loc->symtab));
6169
6170 uiout->field_int ("line", loc->line_number);
6171 }
6172 else if (loc)
6173 {
6174 string_file stb;
6175
6176 print_address_symbolic (loc->gdbarch, loc->address, &stb,
6177 demangle, "");
6178 uiout->field_stream ("at", stb);
6179 }
6180 else
6181 {
6182 uiout->field_string ("pending",
6183 event_location_to_string (b->location.get ()));
6184 /* If extra_string is available, it could be holding a condition
6185 or dprintf arguments. In either case, make sure it is printed,
6186 too, but only for non-MI streams. */
6187 if (!uiout->is_mi_like_p () && b->extra_string != NULL)
6188 {
6189 if (b->type == bp_dprintf)
6190 uiout->text (",");
6191 else
6192 uiout->text (" ");
6193 uiout->text (b->extra_string);
6194 }
6195 }
6196
6197 if (loc && is_breakpoint (b)
6198 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6199 && bp_condition_evaluator (b) == condition_evaluation_both)
6200 {
6201 uiout->text (" (");
6202 uiout->field_string ("evaluated-by",
6203 bp_location_condition_evaluator (loc));
6204 uiout->text (")");
6205 }
6206
6207 do_cleanups (old_chain);
6208 }
6209
6210 static const char *
6211 bptype_string (enum bptype type)
6212 {
6213 struct ep_type_description
6214 {
6215 enum bptype type;
6216 const char *description;
6217 };
6218 static struct ep_type_description bptypes[] =
6219 {
6220 {bp_none, "?deleted?"},
6221 {bp_breakpoint, "breakpoint"},
6222 {bp_hardware_breakpoint, "hw breakpoint"},
6223 {bp_single_step, "sw single-step"},
6224 {bp_until, "until"},
6225 {bp_finish, "finish"},
6226 {bp_watchpoint, "watchpoint"},
6227 {bp_hardware_watchpoint, "hw watchpoint"},
6228 {bp_read_watchpoint, "read watchpoint"},
6229 {bp_access_watchpoint, "acc watchpoint"},
6230 {bp_longjmp, "longjmp"},
6231 {bp_longjmp_resume, "longjmp resume"},
6232 {bp_longjmp_call_dummy, "longjmp for call dummy"},
6233 {bp_exception, "exception"},
6234 {bp_exception_resume, "exception resume"},
6235 {bp_step_resume, "step resume"},
6236 {bp_hp_step_resume, "high-priority step resume"},
6237 {bp_watchpoint_scope, "watchpoint scope"},
6238 {bp_call_dummy, "call dummy"},
6239 {bp_std_terminate, "std::terminate"},
6240 {bp_shlib_event, "shlib events"},
6241 {bp_thread_event, "thread events"},
6242 {bp_overlay_event, "overlay events"},
6243 {bp_longjmp_master, "longjmp master"},
6244 {bp_std_terminate_master, "std::terminate master"},
6245 {bp_exception_master, "exception master"},
6246 {bp_catchpoint, "catchpoint"},
6247 {bp_tracepoint, "tracepoint"},
6248 {bp_fast_tracepoint, "fast tracepoint"},
6249 {bp_static_tracepoint, "static tracepoint"},
6250 {bp_dprintf, "dprintf"},
6251 {bp_jit_event, "jit events"},
6252 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6253 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
6254 };
6255
6256 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6257 || ((int) type != bptypes[(int) type].type))
6258 internal_error (__FILE__, __LINE__,
6259 _("bptypes table does not describe type #%d."),
6260 (int) type);
6261
6262 return bptypes[(int) type].description;
6263 }
6264
6265 /* For MI, output a field named 'thread-groups' with a list as the value.
6266 For CLI, prefix the list with the string 'inf'. */
6267
6268 static void
6269 output_thread_groups (struct ui_out *uiout,
6270 const char *field_name,
6271 VEC(int) *inf_num,
6272 int mi_only)
6273 {
6274 struct cleanup *back_to;
6275 int is_mi = uiout->is_mi_like_p ();
6276 int inf;
6277 int i;
6278
6279 /* For backward compatibility, don't display inferiors in CLI unless
6280 there are several. Always display them for MI. */
6281 if (!is_mi && mi_only)
6282 return;
6283
6284 back_to = make_cleanup_ui_out_list_begin_end (uiout, field_name);
6285
6286 for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
6287 {
6288 if (is_mi)
6289 {
6290 char mi_group[10];
6291
6292 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
6293 uiout->field_string (NULL, mi_group);
6294 }
6295 else
6296 {
6297 if (i == 0)
6298 uiout->text (" inf ");
6299 else
6300 uiout->text (", ");
6301
6302 uiout->text (plongest (inf));
6303 }
6304 }
6305
6306 do_cleanups (back_to);
6307 }
6308
6309 /* Print B to gdb_stdout. */
6310
6311 static void
6312 print_one_breakpoint_location (struct breakpoint *b,
6313 struct bp_location *loc,
6314 int loc_number,
6315 struct bp_location **last_loc,
6316 int allflag)
6317 {
6318 struct command_line *l;
6319 static char bpenables[] = "nynny";
6320
6321 struct ui_out *uiout = current_uiout;
6322 int header_of_multiple = 0;
6323 int part_of_multiple = (loc != NULL);
6324 struct value_print_options opts;
6325
6326 get_user_print_options (&opts);
6327
6328 gdb_assert (!loc || loc_number != 0);
6329 /* See comment in print_one_breakpoint concerning treatment of
6330 breakpoints with single disabled location. */
6331 if (loc == NULL
6332 && (b->loc != NULL
6333 && (b->loc->next != NULL || !b->loc->enabled)))
6334 header_of_multiple = 1;
6335 if (loc == NULL)
6336 loc = b->loc;
6337
6338 annotate_record ();
6339
6340 /* 1 */
6341 annotate_field (0);
6342 if (part_of_multiple)
6343 {
6344 char *formatted;
6345 formatted = xstrprintf ("%d.%d", b->number, loc_number);
6346 uiout->field_string ("number", formatted);
6347 xfree (formatted);
6348 }
6349 else
6350 {
6351 uiout->field_int ("number", b->number);
6352 }
6353
6354 /* 2 */
6355 annotate_field (1);
6356 if (part_of_multiple)
6357 uiout->field_skip ("type");
6358 else
6359 uiout->field_string ("type", bptype_string (b->type));
6360
6361 /* 3 */
6362 annotate_field (2);
6363 if (part_of_multiple)
6364 uiout->field_skip ("disp");
6365 else
6366 uiout->field_string ("disp", bpdisp_text (b->disposition));
6367
6368
6369 /* 4 */
6370 annotate_field (3);
6371 if (part_of_multiple)
6372 uiout->field_string ("enabled", loc->enabled ? "y" : "n");
6373 else
6374 uiout->field_fmt ("enabled", "%c", bpenables[(int) b->enable_state]);
6375 uiout->spaces (2);
6376
6377
6378 /* 5 and 6 */
6379 if (b->ops != NULL && b->ops->print_one != NULL)
6380 {
6381 /* Although the print_one can possibly print all locations,
6382 calling it here is not likely to get any nice result. So,
6383 make sure there's just one location. */
6384 gdb_assert (b->loc == NULL || b->loc->next == NULL);
6385 b->ops->print_one (b, last_loc);
6386 }
6387 else
6388 switch (b->type)
6389 {
6390 case bp_none:
6391 internal_error (__FILE__, __LINE__,
6392 _("print_one_breakpoint: bp_none encountered\n"));
6393 break;
6394
6395 case bp_watchpoint:
6396 case bp_hardware_watchpoint:
6397 case bp_read_watchpoint:
6398 case bp_access_watchpoint:
6399 {
6400 struct watchpoint *w = (struct watchpoint *) b;
6401
6402 /* Field 4, the address, is omitted (which makes the columns
6403 not line up too nicely with the headers, but the effect
6404 is relatively readable). */
6405 if (opts.addressprint)
6406 uiout->field_skip ("addr");
6407 annotate_field (5);
6408 uiout->field_string ("what", w->exp_string);
6409 }
6410 break;
6411
6412 case bp_breakpoint:
6413 case bp_hardware_breakpoint:
6414 case bp_single_step:
6415 case bp_until:
6416 case bp_finish:
6417 case bp_longjmp:
6418 case bp_longjmp_resume:
6419 case bp_longjmp_call_dummy:
6420 case bp_exception:
6421 case bp_exception_resume:
6422 case bp_step_resume:
6423 case bp_hp_step_resume:
6424 case bp_watchpoint_scope:
6425 case bp_call_dummy:
6426 case bp_std_terminate:
6427 case bp_shlib_event:
6428 case bp_thread_event:
6429 case bp_overlay_event:
6430 case bp_longjmp_master:
6431 case bp_std_terminate_master:
6432 case bp_exception_master:
6433 case bp_tracepoint:
6434 case bp_fast_tracepoint:
6435 case bp_static_tracepoint:
6436 case bp_dprintf:
6437 case bp_jit_event:
6438 case bp_gnu_ifunc_resolver:
6439 case bp_gnu_ifunc_resolver_return:
6440 if (opts.addressprint)
6441 {
6442 annotate_field (4);
6443 if (header_of_multiple)
6444 uiout->field_string ("addr", "<MULTIPLE>");
6445 else if (b->loc == NULL || loc->shlib_disabled)
6446 uiout->field_string ("addr", "<PENDING>");
6447 else
6448 uiout->field_core_addr ("addr",
6449 loc->gdbarch, loc->address);
6450 }
6451 annotate_field (5);
6452 if (!header_of_multiple)
6453 print_breakpoint_location (b, loc);
6454 if (b->loc)
6455 *last_loc = b->loc;
6456 break;
6457 }
6458
6459
6460 if (loc != NULL && !header_of_multiple)
6461 {
6462 struct inferior *inf;
6463 VEC(int) *inf_num = NULL;
6464 int mi_only = 1;
6465
6466 ALL_INFERIORS (inf)
6467 {
6468 if (inf->pspace == loc->pspace)
6469 VEC_safe_push (int, inf_num, inf->num);
6470 }
6471
6472 /* For backward compatibility, don't display inferiors in CLI unless
6473 there are several. Always display for MI. */
6474 if (allflag
6475 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6476 && (number_of_program_spaces () > 1
6477 || number_of_inferiors () > 1)
6478 /* LOC is for existing B, it cannot be in
6479 moribund_locations and thus having NULL OWNER. */
6480 && loc->owner->type != bp_catchpoint))
6481 mi_only = 0;
6482 output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6483 VEC_free (int, inf_num);
6484 }
6485
6486 if (!part_of_multiple)
6487 {
6488 if (b->thread != -1)
6489 {
6490 /* FIXME: This seems to be redundant and lost here; see the
6491 "stop only in" line a little further down. */
6492 uiout->text (" thread ");
6493 uiout->field_int ("thread", b->thread);
6494 }
6495 else if (b->task != 0)
6496 {
6497 uiout->text (" task ");
6498 uiout->field_int ("task", b->task);
6499 }
6500 }
6501
6502 uiout->text ("\n");
6503
6504 if (!part_of_multiple)
6505 b->ops->print_one_detail (b, uiout);
6506
6507 if (part_of_multiple && frame_id_p (b->frame_id))
6508 {
6509 annotate_field (6);
6510 uiout->text ("\tstop only in stack frame at ");
6511 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
6512 the frame ID. */
6513 uiout->field_core_addr ("frame",
6514 b->gdbarch, b->frame_id.stack_addr);
6515 uiout->text ("\n");
6516 }
6517
6518 if (!part_of_multiple && b->cond_string)
6519 {
6520 annotate_field (7);
6521 if (is_tracepoint (b))
6522 uiout->text ("\ttrace only if ");
6523 else
6524 uiout->text ("\tstop only if ");
6525 uiout->field_string ("cond", b->cond_string);
6526
6527 /* Print whether the target is doing the breakpoint's condition
6528 evaluation. If GDB is doing the evaluation, don't print anything. */
6529 if (is_breakpoint (b)
6530 && breakpoint_condition_evaluation_mode ()
6531 == condition_evaluation_target)
6532 {
6533 uiout->text (" (");
6534 uiout->field_string ("evaluated-by",
6535 bp_condition_evaluator (b));
6536 uiout->text (" evals)");
6537 }
6538 uiout->text ("\n");
6539 }
6540
6541 if (!part_of_multiple && b->thread != -1)
6542 {
6543 /* FIXME should make an annotation for this. */
6544 uiout->text ("\tstop only in thread ");
6545 if (uiout->is_mi_like_p ())
6546 uiout->field_int ("thread", b->thread);
6547 else
6548 {
6549 struct thread_info *thr = find_thread_global_id (b->thread);
6550
6551 uiout->field_string ("thread", print_thread_id (thr));
6552 }
6553 uiout->text ("\n");
6554 }
6555
6556 if (!part_of_multiple)
6557 {
6558 if (b->hit_count)
6559 {
6560 /* FIXME should make an annotation for this. */
6561 if (is_catchpoint (b))
6562 uiout->text ("\tcatchpoint");
6563 else if (is_tracepoint (b))
6564 uiout->text ("\ttracepoint");
6565 else
6566 uiout->text ("\tbreakpoint");
6567 uiout->text (" already hit ");
6568 uiout->field_int ("times", b->hit_count);
6569 if (b->hit_count == 1)
6570 uiout->text (" time\n");
6571 else
6572 uiout->text (" times\n");
6573 }
6574 else
6575 {
6576 /* Output the count also if it is zero, but only if this is mi. */
6577 if (uiout->is_mi_like_p ())
6578 uiout->field_int ("times", b->hit_count);
6579 }
6580 }
6581
6582 if (!part_of_multiple && b->ignore_count)
6583 {
6584 annotate_field (8);
6585 uiout->text ("\tignore next ");
6586 uiout->field_int ("ignore", b->ignore_count);
6587 uiout->text (" hits\n");
6588 }
6589
6590 /* Note that an enable count of 1 corresponds to "enable once"
6591 behavior, which is reported by the combination of enablement and
6592 disposition, so we don't need to mention it here. */
6593 if (!part_of_multiple && b->enable_count > 1)
6594 {
6595 annotate_field (8);
6596 uiout->text ("\tdisable after ");
6597 /* Tweak the wording to clarify that ignore and enable counts
6598 are distinct, and have additive effect. */
6599 if (b->ignore_count)
6600 uiout->text ("additional ");
6601 else
6602 uiout->text ("next ");
6603 uiout->field_int ("enable", b->enable_count);
6604 uiout->text (" hits\n");
6605 }
6606
6607 if (!part_of_multiple && is_tracepoint (b))
6608 {
6609 struct tracepoint *tp = (struct tracepoint *) b;
6610
6611 if (tp->traceframe_usage)
6612 {
6613 uiout->text ("\ttrace buffer usage ");
6614 uiout->field_int ("traceframe-usage", tp->traceframe_usage);
6615 uiout->text (" bytes\n");
6616 }
6617 }
6618
6619 l = b->commands ? b->commands->commands : NULL;
6620 if (!part_of_multiple && l)
6621 {
6622 struct cleanup *script_chain;
6623
6624 annotate_field (9);
6625 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
6626 print_command_lines (uiout, l, 4);
6627 do_cleanups (script_chain);
6628 }
6629
6630 if (is_tracepoint (b))
6631 {
6632 struct tracepoint *t = (struct tracepoint *) b;
6633
6634 if (!part_of_multiple && t->pass_count)
6635 {
6636 annotate_field (10);
6637 uiout->text ("\tpass count ");
6638 uiout->field_int ("pass", t->pass_count);
6639 uiout->text (" \n");
6640 }
6641
6642 /* Don't display it when tracepoint or tracepoint location is
6643 pending. */
6644 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6645 {
6646 annotate_field (11);
6647
6648 if (uiout->is_mi_like_p ())
6649 uiout->field_string ("installed",
6650 loc->inserted ? "y" : "n");
6651 else
6652 {
6653 if (loc->inserted)
6654 uiout->text ("\t");
6655 else
6656 uiout->text ("\tnot ");
6657 uiout->text ("installed on target\n");
6658 }
6659 }
6660 }
6661
6662 if (uiout->is_mi_like_p () && !part_of_multiple)
6663 {
6664 if (is_watchpoint (b))
6665 {
6666 struct watchpoint *w = (struct watchpoint *) b;
6667
6668 uiout->field_string ("original-location", w->exp_string);
6669 }
6670 else if (b->location != NULL
6671 && event_location_to_string (b->location.get ()) != NULL)
6672 uiout->field_string ("original-location",
6673 event_location_to_string (b->location.get ()));
6674 }
6675 }
6676
6677 static void
6678 print_one_breakpoint (struct breakpoint *b,
6679 struct bp_location **last_loc,
6680 int allflag)
6681 {
6682 struct cleanup *bkpt_chain;
6683 struct ui_out *uiout = current_uiout;
6684
6685 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
6686
6687 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
6688 do_cleanups (bkpt_chain);
6689
6690 /* If this breakpoint has custom print function,
6691 it's already printed. Otherwise, print individual
6692 locations, if any. */
6693 if (b->ops == NULL || b->ops->print_one == NULL)
6694 {
6695 /* If breakpoint has a single location that is disabled, we
6696 print it as if it had several locations, since otherwise it's
6697 hard to represent "breakpoint enabled, location disabled"
6698 situation.
6699
6700 Note that while hardware watchpoints have several locations
6701 internally, that's not a property exposed to user. */
6702 if (b->loc
6703 && !is_hardware_watchpoint (b)
6704 && (b->loc->next || !b->loc->enabled))
6705 {
6706 struct bp_location *loc;
6707 int n = 1;
6708
6709 for (loc = b->loc; loc; loc = loc->next, ++n)
6710 {
6711 struct cleanup *inner2 =
6712 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
6713 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
6714 do_cleanups (inner2);
6715 }
6716 }
6717 }
6718 }
6719
6720 static int
6721 breakpoint_address_bits (struct breakpoint *b)
6722 {
6723 int print_address_bits = 0;
6724 struct bp_location *loc;
6725
6726 /* Software watchpoints that aren't watching memory don't have an
6727 address to print. */
6728 if (is_no_memory_software_watchpoint (b))
6729 return 0;
6730
6731 for (loc = b->loc; loc; loc = loc->next)
6732 {
6733 int addr_bit;
6734
6735 addr_bit = gdbarch_addr_bit (loc->gdbarch);
6736 if (addr_bit > print_address_bits)
6737 print_address_bits = addr_bit;
6738 }
6739
6740 return print_address_bits;
6741 }
6742
6743 struct captured_breakpoint_query_args
6744 {
6745 int bnum;
6746 };
6747
6748 static int
6749 do_captured_breakpoint_query (struct ui_out *uiout, void *data)
6750 {
6751 struct captured_breakpoint_query_args *args
6752 = (struct captured_breakpoint_query_args *) data;
6753 struct breakpoint *b;
6754 struct bp_location *dummy_loc = NULL;
6755
6756 ALL_BREAKPOINTS (b)
6757 {
6758 if (args->bnum == b->number)
6759 {
6760 print_one_breakpoint (b, &dummy_loc, 0);
6761 return GDB_RC_OK;
6762 }
6763 }
6764 return GDB_RC_NONE;
6765 }
6766
6767 enum gdb_rc
6768 gdb_breakpoint_query (struct ui_out *uiout, int bnum,
6769 char **error_message)
6770 {
6771 struct captured_breakpoint_query_args args;
6772
6773 args.bnum = bnum;
6774 /* For the moment we don't trust print_one_breakpoint() to not throw
6775 an error. */
6776 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
6777 error_message, RETURN_MASK_ALL) < 0)
6778 return GDB_RC_FAIL;
6779 else
6780 return GDB_RC_OK;
6781 }
6782
6783 /* Return true if this breakpoint was set by the user, false if it is
6784 internal or momentary. */
6785
6786 int
6787 user_breakpoint_p (struct breakpoint *b)
6788 {
6789 return b->number > 0;
6790 }
6791
6792 /* See breakpoint.h. */
6793
6794 int
6795 pending_breakpoint_p (struct breakpoint *b)
6796 {
6797 return b->loc == NULL;
6798 }
6799
6800 /* Print information on user settable breakpoint (watchpoint, etc)
6801 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6802 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6803 FILTER is non-NULL, call it on each breakpoint and only include the
6804 ones for which it returns non-zero. Return the total number of
6805 breakpoints listed. */
6806
6807 static int
6808 breakpoint_1 (char *args, int allflag,
6809 int (*filter) (const struct breakpoint *))
6810 {
6811 struct breakpoint *b;
6812 struct bp_location *last_loc = NULL;
6813 int nr_printable_breakpoints;
6814 struct cleanup *bkpttbl_chain;
6815 struct value_print_options opts;
6816 int print_address_bits = 0;
6817 int print_type_col_width = 14;
6818 struct ui_out *uiout = current_uiout;
6819
6820 get_user_print_options (&opts);
6821
6822 /* Compute the number of rows in the table, as well as the size
6823 required for address fields. */
6824 nr_printable_breakpoints = 0;
6825 ALL_BREAKPOINTS (b)
6826 {
6827 /* If we have a filter, only list the breakpoints it accepts. */
6828 if (filter && !filter (b))
6829 continue;
6830
6831 /* If we have an "args" string, it is a list of breakpoints to
6832 accept. Skip the others. */
6833 if (args != NULL && *args != '\0')
6834 {
6835 if (allflag && parse_and_eval_long (args) != b->number)
6836 continue;
6837 if (!allflag && !number_is_in_list (args, b->number))
6838 continue;
6839 }
6840
6841 if (allflag || user_breakpoint_p (b))
6842 {
6843 int addr_bit, type_len;
6844
6845 addr_bit = breakpoint_address_bits (b);
6846 if (addr_bit > print_address_bits)
6847 print_address_bits = addr_bit;
6848
6849 type_len = strlen (bptype_string (b->type));
6850 if (type_len > print_type_col_width)
6851 print_type_col_width = type_len;
6852
6853 nr_printable_breakpoints++;
6854 }
6855 }
6856
6857 if (opts.addressprint)
6858 bkpttbl_chain
6859 = make_cleanup_ui_out_table_begin_end (uiout, 6,
6860 nr_printable_breakpoints,
6861 "BreakpointTable");
6862 else
6863 bkpttbl_chain
6864 = make_cleanup_ui_out_table_begin_end (uiout, 5,
6865 nr_printable_breakpoints,
6866 "BreakpointTable");
6867
6868 if (nr_printable_breakpoints > 0)
6869 annotate_breakpoints_headers ();
6870 if (nr_printable_breakpoints > 0)
6871 annotate_field (0);
6872 uiout->table_header (7, ui_left, "number", "Num"); /* 1 */
6873 if (nr_printable_breakpoints > 0)
6874 annotate_field (1);
6875 uiout->table_header (print_type_col_width, ui_left, "type", "Type"); /* 2 */
6876 if (nr_printable_breakpoints > 0)
6877 annotate_field (2);
6878 uiout->table_header (4, ui_left, "disp", "Disp"); /* 3 */
6879 if (nr_printable_breakpoints > 0)
6880 annotate_field (3);
6881 uiout->table_header (3, ui_left, "enabled", "Enb"); /* 4 */
6882 if (opts.addressprint)
6883 {
6884 if (nr_printable_breakpoints > 0)
6885 annotate_field (4);
6886 if (print_address_bits <= 32)
6887 uiout->table_header (10, ui_left, "addr", "Address"); /* 5 */
6888 else
6889 uiout->table_header (18, ui_left, "addr", "Address"); /* 5 */
6890 }
6891 if (nr_printable_breakpoints > 0)
6892 annotate_field (5);
6893 uiout->table_header (40, ui_noalign, "what", "What"); /* 6 */
6894 uiout->table_body ();
6895 if (nr_printable_breakpoints > 0)
6896 annotate_breakpoints_table ();
6897
6898 ALL_BREAKPOINTS (b)
6899 {
6900 QUIT;
6901 /* If we have a filter, only list the breakpoints it accepts. */
6902 if (filter && !filter (b))
6903 continue;
6904
6905 /* If we have an "args" string, it is a list of breakpoints to
6906 accept. Skip the others. */
6907
6908 if (args != NULL && *args != '\0')
6909 {
6910 if (allflag) /* maintenance info breakpoint */
6911 {
6912 if (parse_and_eval_long (args) != b->number)
6913 continue;
6914 }
6915 else /* all others */
6916 {
6917 if (!number_is_in_list (args, b->number))
6918 continue;
6919 }
6920 }
6921 /* We only print out user settable breakpoints unless the
6922 allflag is set. */
6923 if (allflag || user_breakpoint_p (b))
6924 print_one_breakpoint (b, &last_loc, allflag);
6925 }
6926
6927 do_cleanups (bkpttbl_chain);
6928
6929 if (nr_printable_breakpoints == 0)
6930 {
6931 /* If there's a filter, let the caller decide how to report
6932 empty list. */
6933 if (!filter)
6934 {
6935 if (args == NULL || *args == '\0')
6936 uiout->message ("No breakpoints or watchpoints.\n");
6937 else
6938 uiout->message ("No breakpoint or watchpoint matching '%s'.\n",
6939 args);
6940 }
6941 }
6942 else
6943 {
6944 if (last_loc && !server_command)
6945 set_next_address (last_loc->gdbarch, last_loc->address);
6946 }
6947
6948 /* FIXME? Should this be moved up so that it is only called when
6949 there have been breakpoints? */
6950 annotate_breakpoints_table_end ();
6951
6952 return nr_printable_breakpoints;
6953 }
6954
6955 /* Display the value of default-collect in a way that is generally
6956 compatible with the breakpoint list. */
6957
6958 static void
6959 default_collect_info (void)
6960 {
6961 struct ui_out *uiout = current_uiout;
6962
6963 /* If it has no value (which is frequently the case), say nothing; a
6964 message like "No default-collect." gets in user's face when it's
6965 not wanted. */
6966 if (!*default_collect)
6967 return;
6968
6969 /* The following phrase lines up nicely with per-tracepoint collect
6970 actions. */
6971 uiout->text ("default collect ");
6972 uiout->field_string ("default-collect", default_collect);
6973 uiout->text (" \n");
6974 }
6975
6976 static void
6977 breakpoints_info (char *args, int from_tty)
6978 {
6979 breakpoint_1 (args, 0, NULL);
6980
6981 default_collect_info ();
6982 }
6983
6984 static void
6985 watchpoints_info (char *args, int from_tty)
6986 {
6987 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
6988 struct ui_out *uiout = current_uiout;
6989
6990 if (num_printed == 0)
6991 {
6992 if (args == NULL || *args == '\0')
6993 uiout->message ("No watchpoints.\n");
6994 else
6995 uiout->message ("No watchpoint matching '%s'.\n", args);
6996 }
6997 }
6998
6999 static void
7000 maintenance_info_breakpoints (char *args, int from_tty)
7001 {
7002 breakpoint_1 (args, 1, NULL);
7003
7004 default_collect_info ();
7005 }
7006
7007 static int
7008 breakpoint_has_pc (struct breakpoint *b,
7009 struct program_space *pspace,
7010 CORE_ADDR pc, struct obj_section *section)
7011 {
7012 struct bp_location *bl = b->loc;
7013
7014 for (; bl; bl = bl->next)
7015 {
7016 if (bl->pspace == pspace
7017 && bl->address == pc
7018 && (!overlay_debugging || bl->section == section))
7019 return 1;
7020 }
7021 return 0;
7022 }
7023
7024 /* Print a message describing any user-breakpoints set at PC. This
7025 concerns with logical breakpoints, so we match program spaces, not
7026 address spaces. */
7027
7028 static void
7029 describe_other_breakpoints (struct gdbarch *gdbarch,
7030 struct program_space *pspace, CORE_ADDR pc,
7031 struct obj_section *section, int thread)
7032 {
7033 int others = 0;
7034 struct breakpoint *b;
7035
7036 ALL_BREAKPOINTS (b)
7037 others += (user_breakpoint_p (b)
7038 && breakpoint_has_pc (b, pspace, pc, section));
7039 if (others > 0)
7040 {
7041 if (others == 1)
7042 printf_filtered (_("Note: breakpoint "));
7043 else /* if (others == ???) */
7044 printf_filtered (_("Note: breakpoints "));
7045 ALL_BREAKPOINTS (b)
7046 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
7047 {
7048 others--;
7049 printf_filtered ("%d", b->number);
7050 if (b->thread == -1 && thread != -1)
7051 printf_filtered (" (all threads)");
7052 else if (b->thread != -1)
7053 printf_filtered (" (thread %d)", b->thread);
7054 printf_filtered ("%s%s ",
7055 ((b->enable_state == bp_disabled
7056 || b->enable_state == bp_call_disabled)
7057 ? " (disabled)"
7058 : ""),
7059 (others > 1) ? ","
7060 : ((others == 1) ? " and" : ""));
7061 }
7062 printf_filtered (_("also set at pc "));
7063 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
7064 printf_filtered (".\n");
7065 }
7066 }
7067 \f
7068
7069 /* Return true iff it is meaningful to use the address member of
7070 BPT locations. For some breakpoint types, the locations' address members
7071 are irrelevant and it makes no sense to attempt to compare them to other
7072 addresses (or use them for any other purpose either).
7073
7074 More specifically, each of the following breakpoint types will
7075 always have a zero valued location address and we don't want to mark
7076 breakpoints of any of these types to be a duplicate of an actual
7077 breakpoint location at address zero:
7078
7079 bp_watchpoint
7080 bp_catchpoint
7081
7082 */
7083
7084 static int
7085 breakpoint_address_is_meaningful (struct breakpoint *bpt)
7086 {
7087 enum bptype type = bpt->type;
7088
7089 return (type != bp_watchpoint && type != bp_catchpoint);
7090 }
7091
7092 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
7093 true if LOC1 and LOC2 represent the same watchpoint location. */
7094
7095 static int
7096 watchpoint_locations_match (struct bp_location *loc1,
7097 struct bp_location *loc2)
7098 {
7099 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
7100 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
7101
7102 /* Both of them must exist. */
7103 gdb_assert (w1 != NULL);
7104 gdb_assert (w2 != NULL);
7105
7106 /* If the target can evaluate the condition expression in hardware,
7107 then we we need to insert both watchpoints even if they are at
7108 the same place. Otherwise the watchpoint will only trigger when
7109 the condition of whichever watchpoint was inserted evaluates to
7110 true, not giving a chance for GDB to check the condition of the
7111 other watchpoint. */
7112 if ((w1->cond_exp
7113 && target_can_accel_watchpoint_condition (loc1->address,
7114 loc1->length,
7115 loc1->watchpoint_type,
7116 w1->cond_exp.get ()))
7117 || (w2->cond_exp
7118 && target_can_accel_watchpoint_condition (loc2->address,
7119 loc2->length,
7120 loc2->watchpoint_type,
7121 w2->cond_exp.get ())))
7122 return 0;
7123
7124 /* Note that this checks the owner's type, not the location's. In
7125 case the target does not support read watchpoints, but does
7126 support access watchpoints, we'll have bp_read_watchpoint
7127 watchpoints with hw_access locations. Those should be considered
7128 duplicates of hw_read locations. The hw_read locations will
7129 become hw_access locations later. */
7130 return (loc1->owner->type == loc2->owner->type
7131 && loc1->pspace->aspace == loc2->pspace->aspace
7132 && loc1->address == loc2->address
7133 && loc1->length == loc2->length);
7134 }
7135
7136 /* See breakpoint.h. */
7137
7138 int
7139 breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
7140 struct address_space *aspace2, CORE_ADDR addr2)
7141 {
7142 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
7143 || aspace1 == aspace2)
7144 && addr1 == addr2);
7145 }
7146
7147 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
7148 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
7149 matches ASPACE2. On targets that have global breakpoints, the address
7150 space doesn't really matter. */
7151
7152 static int
7153 breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
7154 int len1, struct address_space *aspace2,
7155 CORE_ADDR addr2)
7156 {
7157 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
7158 || aspace1 == aspace2)
7159 && addr2 >= addr1 && addr2 < addr1 + len1);
7160 }
7161
7162 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
7163 a ranged breakpoint. In most targets, a match happens only if ASPACE
7164 matches the breakpoint's address space. On targets that have global
7165 breakpoints, the address space doesn't really matter. */
7166
7167 static int
7168 breakpoint_location_address_match (struct bp_location *bl,
7169 struct address_space *aspace,
7170 CORE_ADDR addr)
7171 {
7172 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
7173 aspace, addr)
7174 || (bl->length
7175 && breakpoint_address_match_range (bl->pspace->aspace,
7176 bl->address, bl->length,
7177 aspace, addr)));
7178 }
7179
7180 /* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
7181 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
7182 match happens only if ASPACE matches the breakpoint's address
7183 space. On targets that have global breakpoints, the address space
7184 doesn't really matter. */
7185
7186 static int
7187 breakpoint_location_address_range_overlap (struct bp_location *bl,
7188 struct address_space *aspace,
7189 CORE_ADDR addr, int len)
7190 {
7191 if (gdbarch_has_global_breakpoints (target_gdbarch ())
7192 || bl->pspace->aspace == aspace)
7193 {
7194 int bl_len = bl->length != 0 ? bl->length : 1;
7195
7196 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
7197 return 1;
7198 }
7199 return 0;
7200 }
7201
7202 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
7203 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
7204 true, otherwise returns false. */
7205
7206 static int
7207 tracepoint_locations_match (struct bp_location *loc1,
7208 struct bp_location *loc2)
7209 {
7210 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
7211 /* Since tracepoint locations are never duplicated with others', tracepoint
7212 locations at the same address of different tracepoints are regarded as
7213 different locations. */
7214 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7215 else
7216 return 0;
7217 }
7218
7219 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
7220 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
7221 represent the same location. */
7222
7223 static int
7224 breakpoint_locations_match (struct bp_location *loc1,
7225 struct bp_location *loc2)
7226 {
7227 int hw_point1, hw_point2;
7228
7229 /* Both of them must not be in moribund_locations. */
7230 gdb_assert (loc1->owner != NULL);
7231 gdb_assert (loc2->owner != NULL);
7232
7233 hw_point1 = is_hardware_watchpoint (loc1->owner);
7234 hw_point2 = is_hardware_watchpoint (loc2->owner);
7235
7236 if (hw_point1 != hw_point2)
7237 return 0;
7238 else if (hw_point1)
7239 return watchpoint_locations_match (loc1, loc2);
7240 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7241 return tracepoint_locations_match (loc1, loc2);
7242 else
7243 /* We compare bp_location.length in order to cover ranged breakpoints. */
7244 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7245 loc2->pspace->aspace, loc2->address)
7246 && loc1->length == loc2->length);
7247 }
7248
7249 static void
7250 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7251 int bnum, int have_bnum)
7252 {
7253 /* The longest string possibly returned by hex_string_custom
7254 is 50 chars. These must be at least that big for safety. */
7255 char astr1[64];
7256 char astr2[64];
7257
7258 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7259 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
7260 if (have_bnum)
7261 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
7262 bnum, astr1, astr2);
7263 else
7264 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
7265 }
7266
7267 /* Adjust a breakpoint's address to account for architectural
7268 constraints on breakpoint placement. Return the adjusted address.
7269 Note: Very few targets require this kind of adjustment. For most
7270 targets, this function is simply the identity function. */
7271
7272 static CORE_ADDR
7273 adjust_breakpoint_address (struct gdbarch *gdbarch,
7274 CORE_ADDR bpaddr, enum bptype bptype)
7275 {
7276 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
7277 {
7278 /* Very few targets need any kind of breakpoint adjustment. */
7279 return bpaddr;
7280 }
7281 else if (bptype == bp_watchpoint
7282 || bptype == bp_hardware_watchpoint
7283 || bptype == bp_read_watchpoint
7284 || bptype == bp_access_watchpoint
7285 || bptype == bp_catchpoint)
7286 {
7287 /* Watchpoints and the various bp_catch_* eventpoints should not
7288 have their addresses modified. */
7289 return bpaddr;
7290 }
7291 else if (bptype == bp_single_step)
7292 {
7293 /* Single-step breakpoints should not have their addresses
7294 modified. If there's any architectural constrain that
7295 applies to this address, then it should have already been
7296 taken into account when the breakpoint was created in the
7297 first place. If we didn't do this, stepping through e.g.,
7298 Thumb-2 IT blocks would break. */
7299 return bpaddr;
7300 }
7301 else
7302 {
7303 CORE_ADDR adjusted_bpaddr;
7304
7305 /* Some targets have architectural constraints on the placement
7306 of breakpoint instructions. Obtain the adjusted address. */
7307 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
7308
7309 /* An adjusted breakpoint address can significantly alter
7310 a user's expectations. Print a warning if an adjustment
7311 is required. */
7312 if (adjusted_bpaddr != bpaddr)
7313 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7314
7315 return adjusted_bpaddr;
7316 }
7317 }
7318
7319 void
7320 init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
7321 struct breakpoint *owner)
7322 {
7323 memset (loc, 0, sizeof (*loc));
7324
7325 gdb_assert (ops != NULL);
7326
7327 loc->ops = ops;
7328 loc->owner = owner;
7329 loc->cond_bytecode = NULL;
7330 loc->shlib_disabled = 0;
7331 loc->enabled = 1;
7332
7333 switch (owner->type)
7334 {
7335 case bp_breakpoint:
7336 case bp_single_step:
7337 case bp_until:
7338 case bp_finish:
7339 case bp_longjmp:
7340 case bp_longjmp_resume:
7341 case bp_longjmp_call_dummy:
7342 case bp_exception:
7343 case bp_exception_resume:
7344 case bp_step_resume:
7345 case bp_hp_step_resume:
7346 case bp_watchpoint_scope:
7347 case bp_call_dummy:
7348 case bp_std_terminate:
7349 case bp_shlib_event:
7350 case bp_thread_event:
7351 case bp_overlay_event:
7352 case bp_jit_event:
7353 case bp_longjmp_master:
7354 case bp_std_terminate_master:
7355 case bp_exception_master:
7356 case bp_gnu_ifunc_resolver:
7357 case bp_gnu_ifunc_resolver_return:
7358 case bp_dprintf:
7359 loc->loc_type = bp_loc_software_breakpoint;
7360 mark_breakpoint_location_modified (loc);
7361 break;
7362 case bp_hardware_breakpoint:
7363 loc->loc_type = bp_loc_hardware_breakpoint;
7364 mark_breakpoint_location_modified (loc);
7365 break;
7366 case bp_hardware_watchpoint:
7367 case bp_read_watchpoint:
7368 case bp_access_watchpoint:
7369 loc->loc_type = bp_loc_hardware_watchpoint;
7370 break;
7371 case bp_watchpoint:
7372 case bp_catchpoint:
7373 case bp_tracepoint:
7374 case bp_fast_tracepoint:
7375 case bp_static_tracepoint:
7376 loc->loc_type = bp_loc_other;
7377 break;
7378 default:
7379 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
7380 }
7381
7382 loc->refc = 1;
7383 }
7384
7385 /* Allocate a struct bp_location. */
7386
7387 static struct bp_location *
7388 allocate_bp_location (struct breakpoint *bpt)
7389 {
7390 return bpt->ops->allocate_location (bpt);
7391 }
7392
7393 static void
7394 free_bp_location (struct bp_location *loc)
7395 {
7396 loc->ops->dtor (loc);
7397 delete loc;
7398 }
7399
7400 /* Increment reference count. */
7401
7402 static void
7403 incref_bp_location (struct bp_location *bl)
7404 {
7405 ++bl->refc;
7406 }
7407
7408 /* Decrement reference count. If the reference count reaches 0,
7409 destroy the bp_location. Sets *BLP to NULL. */
7410
7411 static void
7412 decref_bp_location (struct bp_location **blp)
7413 {
7414 gdb_assert ((*blp)->refc > 0);
7415
7416 if (--(*blp)->refc == 0)
7417 free_bp_location (*blp);
7418 *blp = NULL;
7419 }
7420
7421 /* Add breakpoint B at the end of the global breakpoint chain. */
7422
7423 static void
7424 add_to_breakpoint_chain (struct breakpoint *b)
7425 {
7426 struct breakpoint *b1;
7427
7428 /* Add this breakpoint to the end of the chain so that a list of
7429 breakpoints will come out in order of increasing numbers. */
7430
7431 b1 = breakpoint_chain;
7432 if (b1 == 0)
7433 breakpoint_chain = b;
7434 else
7435 {
7436 while (b1->next)
7437 b1 = b1->next;
7438 b1->next = b;
7439 }
7440 }
7441
7442 /* Initializes breakpoint B with type BPTYPE and no locations yet. */
7443
7444 static void
7445 init_raw_breakpoint_without_location (struct breakpoint *b,
7446 struct gdbarch *gdbarch,
7447 enum bptype bptype,
7448 const struct breakpoint_ops *ops)
7449 {
7450 memset (b, 0, sizeof (*b));
7451
7452 gdb_assert (ops != NULL);
7453
7454 b->ops = ops;
7455 b->type = bptype;
7456 b->gdbarch = gdbarch;
7457 b->language = current_language->la_language;
7458 b->input_radix = input_radix;
7459 b->thread = -1;
7460 b->enable_state = bp_enabled;
7461 b->next = 0;
7462 b->silent = 0;
7463 b->ignore_count = 0;
7464 b->commands = NULL;
7465 b->frame_id = null_frame_id;
7466 b->condition_not_parsed = 0;
7467 b->py_bp_object = NULL;
7468 b->related_breakpoint = b;
7469 b->location = NULL;
7470 }
7471
7472 /* Helper to set_raw_breakpoint below. Creates a breakpoint
7473 that has type BPTYPE and has no locations as yet. */
7474
7475 static struct breakpoint *
7476 set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
7477 enum bptype bptype,
7478 const struct breakpoint_ops *ops)
7479 {
7480 struct breakpoint *b = new breakpoint ();
7481
7482 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7483 add_to_breakpoint_chain (b);
7484 return b;
7485 }
7486
7487 /* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7488 resolutions should be made as the user specified the location explicitly
7489 enough. */
7490
7491 static void
7492 set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
7493 {
7494 gdb_assert (loc->owner != NULL);
7495
7496 if (loc->owner->type == bp_breakpoint
7497 || loc->owner->type == bp_hardware_breakpoint
7498 || is_tracepoint (loc->owner))
7499 {
7500 int is_gnu_ifunc;
7501 const char *function_name;
7502 CORE_ADDR func_addr;
7503
7504 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
7505 &func_addr, NULL, &is_gnu_ifunc);
7506
7507 if (is_gnu_ifunc && !explicit_loc)
7508 {
7509 struct breakpoint *b = loc->owner;
7510
7511 gdb_assert (loc->pspace == current_program_space);
7512 if (gnu_ifunc_resolve_name (function_name,
7513 &loc->requested_address))
7514 {
7515 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7516 loc->address = adjust_breakpoint_address (loc->gdbarch,
7517 loc->requested_address,
7518 b->type);
7519 }
7520 else if (b->type == bp_breakpoint && b->loc == loc
7521 && loc->next == NULL && b->related_breakpoint == b)
7522 {
7523 /* Create only the whole new breakpoint of this type but do not
7524 mess more complicated breakpoints with multiple locations. */
7525 b->type = bp_gnu_ifunc_resolver;
7526 /* Remember the resolver's address for use by the return
7527 breakpoint. */
7528 loc->related_address = func_addr;
7529 }
7530 }
7531
7532 if (function_name)
7533 loc->function_name = xstrdup (function_name);
7534 }
7535 }
7536
7537 /* Attempt to determine architecture of location identified by SAL. */
7538 struct gdbarch *
7539 get_sal_arch (struct symtab_and_line sal)
7540 {
7541 if (sal.section)
7542 return get_objfile_arch (sal.section->objfile);
7543 if (sal.symtab)
7544 return get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
7545
7546 return NULL;
7547 }
7548
7549 /* Low level routine for partially initializing a breakpoint of type
7550 BPTYPE. The newly created breakpoint's address, section, source
7551 file name, and line number are provided by SAL.
7552
7553 It is expected that the caller will complete the initialization of
7554 the newly created breakpoint struct as well as output any status
7555 information regarding the creation of a new breakpoint. */
7556
7557 static void
7558 init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
7559 struct symtab_and_line sal, enum bptype bptype,
7560 const struct breakpoint_ops *ops)
7561 {
7562 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7563
7564 add_location_to_breakpoint (b, &sal);
7565
7566 if (bptype != bp_catchpoint)
7567 gdb_assert (sal.pspace != NULL);
7568
7569 /* Store the program space that was used to set the breakpoint,
7570 except for ordinary breakpoints, which are independent of the
7571 program space. */
7572 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7573 b->pspace = sal.pspace;
7574 }
7575
7576 /* set_raw_breakpoint is a low level routine for allocating and
7577 partially initializing a breakpoint of type BPTYPE. The newly
7578 created breakpoint's address, section, source file name, and line
7579 number are provided by SAL. The newly created and partially
7580 initialized breakpoint is added to the breakpoint chain and
7581 is also returned as the value of this function.
7582
7583 It is expected that the caller will complete the initialization of
7584 the newly created breakpoint struct as well as output any status
7585 information regarding the creation of a new breakpoint. In
7586 particular, set_raw_breakpoint does NOT set the breakpoint
7587 number! Care should be taken to not allow an error to occur
7588 prior to completing the initialization of the breakpoint. If this
7589 should happen, a bogus breakpoint will be left on the chain. */
7590
7591 struct breakpoint *
7592 set_raw_breakpoint (struct gdbarch *gdbarch,
7593 struct symtab_and_line sal, enum bptype bptype,
7594 const struct breakpoint_ops *ops)
7595 {
7596 struct breakpoint *b = new breakpoint ();
7597
7598 init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
7599 add_to_breakpoint_chain (b);
7600 return b;
7601 }
7602
7603 /* Call this routine when stepping and nexting to enable a breakpoint
7604 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7605 initiated the operation. */
7606
7607 void
7608 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
7609 {
7610 struct breakpoint *b, *b_tmp;
7611 int thread = tp->global_num;
7612
7613 /* To avoid having to rescan all objfile symbols at every step,
7614 we maintain a list of continually-inserted but always disabled
7615 longjmp "master" breakpoints. Here, we simply create momentary
7616 clones of those and enable them for the requested thread. */
7617 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7618 if (b->pspace == current_program_space
7619 && (b->type == bp_longjmp_master
7620 || b->type == bp_exception_master))
7621 {
7622 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7623 struct breakpoint *clone;
7624
7625 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7626 after their removal. */
7627 clone = momentary_breakpoint_from_master (b, type,
7628 &longjmp_breakpoint_ops, 1);
7629 clone->thread = thread;
7630 }
7631
7632 tp->initiating_frame = frame;
7633 }
7634
7635 /* Delete all longjmp breakpoints from THREAD. */
7636 void
7637 delete_longjmp_breakpoint (int thread)
7638 {
7639 struct breakpoint *b, *b_tmp;
7640
7641 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7642 if (b->type == bp_longjmp || b->type == bp_exception)
7643 {
7644 if (b->thread == thread)
7645 delete_breakpoint (b);
7646 }
7647 }
7648
7649 void
7650 delete_longjmp_breakpoint_at_next_stop (int thread)
7651 {
7652 struct breakpoint *b, *b_tmp;
7653
7654 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7655 if (b->type == bp_longjmp || b->type == bp_exception)
7656 {
7657 if (b->thread == thread)
7658 b->disposition = disp_del_at_next_stop;
7659 }
7660 }
7661
7662 /* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7663 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7664 pointer to any of them. Return NULL if this system cannot place longjmp
7665 breakpoints. */
7666
7667 struct breakpoint *
7668 set_longjmp_breakpoint_for_call_dummy (void)
7669 {
7670 struct breakpoint *b, *retval = NULL;
7671
7672 ALL_BREAKPOINTS (b)
7673 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7674 {
7675 struct breakpoint *new_b;
7676
7677 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7678 &momentary_breakpoint_ops,
7679 1);
7680 new_b->thread = ptid_to_global_thread_id (inferior_ptid);
7681
7682 /* Link NEW_B into the chain of RETVAL breakpoints. */
7683
7684 gdb_assert (new_b->related_breakpoint == new_b);
7685 if (retval == NULL)
7686 retval = new_b;
7687 new_b->related_breakpoint = retval;
7688 while (retval->related_breakpoint != new_b->related_breakpoint)
7689 retval = retval->related_breakpoint;
7690 retval->related_breakpoint = new_b;
7691 }
7692
7693 return retval;
7694 }
7695
7696 /* Verify all existing dummy frames and their associated breakpoints for
7697 TP. Remove those which can no longer be found in the current frame
7698 stack.
7699
7700 You should call this function only at places where it is safe to currently
7701 unwind the whole stack. Failed stack unwind would discard live dummy
7702 frames. */
7703
7704 void
7705 check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
7706 {
7707 struct breakpoint *b, *b_tmp;
7708
7709 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7710 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
7711 {
7712 struct breakpoint *dummy_b = b->related_breakpoint;
7713
7714 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7715 dummy_b = dummy_b->related_breakpoint;
7716 if (dummy_b->type != bp_call_dummy
7717 || frame_find_by_id (dummy_b->frame_id) != NULL)
7718 continue;
7719
7720 dummy_frame_discard (dummy_b->frame_id, tp->ptid);
7721
7722 while (b->related_breakpoint != b)
7723 {
7724 if (b_tmp == b->related_breakpoint)
7725 b_tmp = b->related_breakpoint->next;
7726 delete_breakpoint (b->related_breakpoint);
7727 }
7728 delete_breakpoint (b);
7729 }
7730 }
7731
7732 void
7733 enable_overlay_breakpoints (void)
7734 {
7735 struct breakpoint *b;
7736
7737 ALL_BREAKPOINTS (b)
7738 if (b->type == bp_overlay_event)
7739 {
7740 b->enable_state = bp_enabled;
7741 update_global_location_list (UGLL_MAY_INSERT);
7742 overlay_events_enabled = 1;
7743 }
7744 }
7745
7746 void
7747 disable_overlay_breakpoints (void)
7748 {
7749 struct breakpoint *b;
7750
7751 ALL_BREAKPOINTS (b)
7752 if (b->type == bp_overlay_event)
7753 {
7754 b->enable_state = bp_disabled;
7755 update_global_location_list (UGLL_DONT_INSERT);
7756 overlay_events_enabled = 0;
7757 }
7758 }
7759
7760 /* Set an active std::terminate breakpoint for each std::terminate
7761 master breakpoint. */
7762 void
7763 set_std_terminate_breakpoint (void)
7764 {
7765 struct breakpoint *b, *b_tmp;
7766
7767 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7768 if (b->pspace == current_program_space
7769 && b->type == bp_std_terminate_master)
7770 {
7771 momentary_breakpoint_from_master (b, bp_std_terminate,
7772 &momentary_breakpoint_ops, 1);
7773 }
7774 }
7775
7776 /* Delete all the std::terminate breakpoints. */
7777 void
7778 delete_std_terminate_breakpoint (void)
7779 {
7780 struct breakpoint *b, *b_tmp;
7781
7782 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7783 if (b->type == bp_std_terminate)
7784 delete_breakpoint (b);
7785 }
7786
7787 struct breakpoint *
7788 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7789 {
7790 struct breakpoint *b;
7791
7792 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7793 &internal_breakpoint_ops);
7794
7795 b->enable_state = bp_enabled;
7796 /* location has to be used or breakpoint_re_set will delete me. */
7797 b->location = new_address_location (b->loc->address, NULL, 0);
7798
7799 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7800
7801 return b;
7802 }
7803
7804 struct lang_and_radix
7805 {
7806 enum language lang;
7807 int radix;
7808 };
7809
7810 /* Create a breakpoint for JIT code registration and unregistration. */
7811
7812 struct breakpoint *
7813 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7814 {
7815 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7816 &internal_breakpoint_ops);
7817 }
7818
7819 /* Remove JIT code registration and unregistration breakpoint(s). */
7820
7821 void
7822 remove_jit_event_breakpoints (void)
7823 {
7824 struct breakpoint *b, *b_tmp;
7825
7826 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7827 if (b->type == bp_jit_event
7828 && b->loc->pspace == current_program_space)
7829 delete_breakpoint (b);
7830 }
7831
7832 void
7833 remove_solib_event_breakpoints (void)
7834 {
7835 struct breakpoint *b, *b_tmp;
7836
7837 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7838 if (b->type == bp_shlib_event
7839 && b->loc->pspace == current_program_space)
7840 delete_breakpoint (b);
7841 }
7842
7843 /* See breakpoint.h. */
7844
7845 void
7846 remove_solib_event_breakpoints_at_next_stop (void)
7847 {
7848 struct breakpoint *b, *b_tmp;
7849
7850 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7851 if (b->type == bp_shlib_event
7852 && b->loc->pspace == current_program_space)
7853 b->disposition = disp_del_at_next_stop;
7854 }
7855
7856 /* Helper for create_solib_event_breakpoint /
7857 create_and_insert_solib_event_breakpoint. Allows specifying which
7858 INSERT_MODE to pass through to update_global_location_list. */
7859
7860 static struct breakpoint *
7861 create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7862 enum ugll_insert_mode insert_mode)
7863 {
7864 struct breakpoint *b;
7865
7866 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7867 &internal_breakpoint_ops);
7868 update_global_location_list_nothrow (insert_mode);
7869 return b;
7870 }
7871
7872 struct breakpoint *
7873 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7874 {
7875 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7876 }
7877
7878 /* See breakpoint.h. */
7879
7880 struct breakpoint *
7881 create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7882 {
7883 struct breakpoint *b;
7884
7885 /* Explicitly tell update_global_location_list to insert
7886 locations. */
7887 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
7888 if (!b->loc->inserted)
7889 {
7890 delete_breakpoint (b);
7891 return NULL;
7892 }
7893 return b;
7894 }
7895
7896 /* Disable any breakpoints that are on code in shared libraries. Only
7897 apply to enabled breakpoints, disabled ones can just stay disabled. */
7898
7899 void
7900 disable_breakpoints_in_shlibs (void)
7901 {
7902 struct bp_location *loc, **locp_tmp;
7903
7904 ALL_BP_LOCATIONS (loc, locp_tmp)
7905 {
7906 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7907 struct breakpoint *b = loc->owner;
7908
7909 /* We apply the check to all breakpoints, including disabled for
7910 those with loc->duplicate set. This is so that when breakpoint
7911 becomes enabled, or the duplicate is removed, gdb will try to
7912 insert all breakpoints. If we don't set shlib_disabled here,
7913 we'll try to insert those breakpoints and fail. */
7914 if (((b->type == bp_breakpoint)
7915 || (b->type == bp_jit_event)
7916 || (b->type == bp_hardware_breakpoint)
7917 || (is_tracepoint (b)))
7918 && loc->pspace == current_program_space
7919 && !loc->shlib_disabled
7920 && solib_name_from_address (loc->pspace, loc->address)
7921 )
7922 {
7923 loc->shlib_disabled = 1;
7924 }
7925 }
7926 }
7927
7928 /* Disable any breakpoints and tracepoints that are in SOLIB upon
7929 notification of unloaded_shlib. Only apply to enabled breakpoints,
7930 disabled ones can just stay disabled. */
7931
7932 static void
7933 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7934 {
7935 struct bp_location *loc, **locp_tmp;
7936 int disabled_shlib_breaks = 0;
7937
7938 ALL_BP_LOCATIONS (loc, locp_tmp)
7939 {
7940 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7941 struct breakpoint *b = loc->owner;
7942
7943 if (solib->pspace == loc->pspace
7944 && !loc->shlib_disabled
7945 && (((b->type == bp_breakpoint
7946 || b->type == bp_jit_event
7947 || b->type == bp_hardware_breakpoint)
7948 && (loc->loc_type == bp_loc_hardware_breakpoint
7949 || loc->loc_type == bp_loc_software_breakpoint))
7950 || is_tracepoint (b))
7951 && solib_contains_address_p (solib, loc->address))
7952 {
7953 loc->shlib_disabled = 1;
7954 /* At this point, we cannot rely on remove_breakpoint
7955 succeeding so we must mark the breakpoint as not inserted
7956 to prevent future errors occurring in remove_breakpoints. */
7957 loc->inserted = 0;
7958
7959 /* This may cause duplicate notifications for the same breakpoint. */
7960 observer_notify_breakpoint_modified (b);
7961
7962 if (!disabled_shlib_breaks)
7963 {
7964 target_terminal_ours_for_output ();
7965 warning (_("Temporarily disabling breakpoints "
7966 "for unloaded shared library \"%s\""),
7967 solib->so_name);
7968 }
7969 disabled_shlib_breaks = 1;
7970 }
7971 }
7972 }
7973
7974 /* Disable any breakpoints and tracepoints in OBJFILE upon
7975 notification of free_objfile. Only apply to enabled breakpoints,
7976 disabled ones can just stay disabled. */
7977
7978 static void
7979 disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7980 {
7981 struct breakpoint *b;
7982
7983 if (objfile == NULL)
7984 return;
7985
7986 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7987 managed by the user with add-symbol-file/remove-symbol-file.
7988 Similarly to how breakpoints in shared libraries are handled in
7989 response to "nosharedlibrary", mark breakpoints in such modules
7990 shlib_disabled so they end up uninserted on the next global
7991 location list update. Shared libraries not loaded by the user
7992 aren't handled here -- they're already handled in
7993 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7994 solib_unloaded observer. We skip objfiles that are not
7995 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7996 main objfile). */
7997 if ((objfile->flags & OBJF_SHARED) == 0
7998 || (objfile->flags & OBJF_USERLOADED) == 0)
7999 return;
8000
8001 ALL_BREAKPOINTS (b)
8002 {
8003 struct bp_location *loc;
8004 int bp_modified = 0;
8005
8006 if (!is_breakpoint (b) && !is_tracepoint (b))
8007 continue;
8008
8009 for (loc = b->loc; loc != NULL; loc = loc->next)
8010 {
8011 CORE_ADDR loc_addr = loc->address;
8012
8013 if (loc->loc_type != bp_loc_hardware_breakpoint
8014 && loc->loc_type != bp_loc_software_breakpoint)
8015 continue;
8016
8017 if (loc->shlib_disabled != 0)
8018 continue;
8019
8020 if (objfile->pspace != loc->pspace)
8021 continue;
8022
8023 if (loc->loc_type != bp_loc_hardware_breakpoint
8024 && loc->loc_type != bp_loc_software_breakpoint)
8025 continue;
8026
8027 if (is_addr_in_objfile (loc_addr, objfile))
8028 {
8029 loc->shlib_disabled = 1;
8030 /* At this point, we don't know whether the object was
8031 unmapped from the inferior or not, so leave the
8032 inserted flag alone. We'll handle failure to
8033 uninsert quietly, in case the object was indeed
8034 unmapped. */
8035
8036 mark_breakpoint_location_modified (loc);
8037
8038 bp_modified = 1;
8039 }
8040 }
8041
8042 if (bp_modified)
8043 observer_notify_breakpoint_modified (b);
8044 }
8045 }
8046
8047 /* FORK & VFORK catchpoints. */
8048
8049 /* An instance of this type is used to represent a fork or vfork
8050 catchpoint. It includes a "struct breakpoint" as a kind of base
8051 class; users downcast to "struct breakpoint *" when needed. A
8052 breakpoint is really of this type iff its ops pointer points to
8053 CATCH_FORK_BREAKPOINT_OPS. */
8054
8055 struct fork_catchpoint
8056 {
8057 /* The base class. */
8058 struct breakpoint base;
8059
8060 /* Process id of a child process whose forking triggered this
8061 catchpoint. This field is only valid immediately after this
8062 catchpoint has triggered. */
8063 ptid_t forked_inferior_pid;
8064 };
8065
8066 /* Implement the "insert" breakpoint_ops method for fork
8067 catchpoints. */
8068
8069 static int
8070 insert_catch_fork (struct bp_location *bl)
8071 {
8072 return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
8073 }
8074
8075 /* Implement the "remove" breakpoint_ops method for fork
8076 catchpoints. */
8077
8078 static int
8079 remove_catch_fork (struct bp_location *bl, enum remove_bp_reason reason)
8080 {
8081 return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
8082 }
8083
8084 /* Implement the "breakpoint_hit" breakpoint_ops method for fork
8085 catchpoints. */
8086
8087 static int
8088 breakpoint_hit_catch_fork (const struct bp_location *bl,
8089 struct address_space *aspace, CORE_ADDR bp_addr,
8090 const struct target_waitstatus *ws)
8091 {
8092 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8093
8094 if (ws->kind != TARGET_WAITKIND_FORKED)
8095 return 0;
8096
8097 c->forked_inferior_pid = ws->value.related_pid;
8098 return 1;
8099 }
8100
8101 /* Implement the "print_it" breakpoint_ops method for fork
8102 catchpoints. */
8103
8104 static enum print_stop_action
8105 print_it_catch_fork (bpstat bs)
8106 {
8107 struct ui_out *uiout = current_uiout;
8108 struct breakpoint *b = bs->breakpoint_at;
8109 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
8110
8111 annotate_catchpoint (b->number);
8112 maybe_print_thread_hit_breakpoint (uiout);
8113 if (b->disposition == disp_del)
8114 uiout->text ("Temporary catchpoint ");
8115 else
8116 uiout->text ("Catchpoint ");
8117 if (uiout->is_mi_like_p ())
8118 {
8119 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_FORK));
8120 uiout->field_string ("disp", bpdisp_text (b->disposition));
8121 }
8122 uiout->field_int ("bkptno", b->number);
8123 uiout->text (" (forked process ");
8124 uiout->field_int ("newpid", ptid_get_pid (c->forked_inferior_pid));
8125 uiout->text ("), ");
8126 return PRINT_SRC_AND_LOC;
8127 }
8128
8129 /* Implement the "print_one" breakpoint_ops method for fork
8130 catchpoints. */
8131
8132 static void
8133 print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
8134 {
8135 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8136 struct value_print_options opts;
8137 struct ui_out *uiout = current_uiout;
8138
8139 get_user_print_options (&opts);
8140
8141 /* Field 4, the address, is omitted (which makes the columns not
8142 line up too nicely with the headers, but the effect is relatively
8143 readable). */
8144 if (opts.addressprint)
8145 uiout->field_skip ("addr");
8146 annotate_field (5);
8147 uiout->text ("fork");
8148 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
8149 {
8150 uiout->text (", process ");
8151 uiout->field_int ("what", ptid_get_pid (c->forked_inferior_pid));
8152 uiout->spaces (1);
8153 }
8154
8155 if (uiout->is_mi_like_p ())
8156 uiout->field_string ("catch-type", "fork");
8157 }
8158
8159 /* Implement the "print_mention" breakpoint_ops method for fork
8160 catchpoints. */
8161
8162 static void
8163 print_mention_catch_fork (struct breakpoint *b)
8164 {
8165 printf_filtered (_("Catchpoint %d (fork)"), b->number);
8166 }
8167
8168 /* Implement the "print_recreate" breakpoint_ops method for fork
8169 catchpoints. */
8170
8171 static void
8172 print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
8173 {
8174 fprintf_unfiltered (fp, "catch fork");
8175 print_recreate_thread (b, fp);
8176 }
8177
8178 /* The breakpoint_ops structure to be used in fork catchpoints. */
8179
8180 static struct breakpoint_ops catch_fork_breakpoint_ops;
8181
8182 /* Implement the "insert" breakpoint_ops method for vfork
8183 catchpoints. */
8184
8185 static int
8186 insert_catch_vfork (struct bp_location *bl)
8187 {
8188 return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
8189 }
8190
8191 /* Implement the "remove" breakpoint_ops method for vfork
8192 catchpoints. */
8193
8194 static int
8195 remove_catch_vfork (struct bp_location *bl, enum remove_bp_reason reason)
8196 {
8197 return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
8198 }
8199
8200 /* Implement the "breakpoint_hit" breakpoint_ops method for vfork
8201 catchpoints. */
8202
8203 static int
8204 breakpoint_hit_catch_vfork (const struct bp_location *bl,
8205 struct address_space *aspace, CORE_ADDR bp_addr,
8206 const struct target_waitstatus *ws)
8207 {
8208 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8209
8210 if (ws->kind != TARGET_WAITKIND_VFORKED)
8211 return 0;
8212
8213 c->forked_inferior_pid = ws->value.related_pid;
8214 return 1;
8215 }
8216
8217 /* Implement the "print_it" breakpoint_ops method for vfork
8218 catchpoints. */
8219
8220 static enum print_stop_action
8221 print_it_catch_vfork (bpstat bs)
8222 {
8223 struct ui_out *uiout = current_uiout;
8224 struct breakpoint *b = bs->breakpoint_at;
8225 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8226
8227 annotate_catchpoint (b->number);
8228 maybe_print_thread_hit_breakpoint (uiout);
8229 if (b->disposition == disp_del)
8230 uiout->text ("Temporary catchpoint ");
8231 else
8232 uiout->text ("Catchpoint ");
8233 if (uiout->is_mi_like_p ())
8234 {
8235 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_VFORK));
8236 uiout->field_string ("disp", bpdisp_text (b->disposition));
8237 }
8238 uiout->field_int ("bkptno", b->number);
8239 uiout->text (" (vforked process ");
8240 uiout->field_int ("newpid", ptid_get_pid (c->forked_inferior_pid));
8241 uiout->text ("), ");
8242 return PRINT_SRC_AND_LOC;
8243 }
8244
8245 /* Implement the "print_one" breakpoint_ops method for vfork
8246 catchpoints. */
8247
8248 static void
8249 print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
8250 {
8251 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8252 struct value_print_options opts;
8253 struct ui_out *uiout = current_uiout;
8254
8255 get_user_print_options (&opts);
8256 /* Field 4, the address, is omitted (which makes the columns not
8257 line up too nicely with the headers, but the effect is relatively
8258 readable). */
8259 if (opts.addressprint)
8260 uiout->field_skip ("addr");
8261 annotate_field (5);
8262 uiout->text ("vfork");
8263 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
8264 {
8265 uiout->text (", process ");
8266 uiout->field_int ("what", ptid_get_pid (c->forked_inferior_pid));
8267 uiout->spaces (1);
8268 }
8269
8270 if (uiout->is_mi_like_p ())
8271 uiout->field_string ("catch-type", "vfork");
8272 }
8273
8274 /* Implement the "print_mention" breakpoint_ops method for vfork
8275 catchpoints. */
8276
8277 static void
8278 print_mention_catch_vfork (struct breakpoint *b)
8279 {
8280 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
8281 }
8282
8283 /* Implement the "print_recreate" breakpoint_ops method for vfork
8284 catchpoints. */
8285
8286 static void
8287 print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8288 {
8289 fprintf_unfiltered (fp, "catch vfork");
8290 print_recreate_thread (b, fp);
8291 }
8292
8293 /* The breakpoint_ops structure to be used in vfork catchpoints. */
8294
8295 static struct breakpoint_ops catch_vfork_breakpoint_ops;
8296
8297 /* An instance of this type is used to represent an solib catchpoint.
8298 It includes a "struct breakpoint" as a kind of base class; users
8299 downcast to "struct breakpoint *" when needed. A breakpoint is
8300 really of this type iff its ops pointer points to
8301 CATCH_SOLIB_BREAKPOINT_OPS. */
8302
8303 struct solib_catchpoint
8304 {
8305 /* The base class. */
8306 struct breakpoint base;
8307
8308 /* True for "catch load", false for "catch unload". */
8309 unsigned char is_load;
8310
8311 /* Regular expression to match, if any. COMPILED is only valid when
8312 REGEX is non-NULL. */
8313 char *regex;
8314 regex_t compiled;
8315 };
8316
8317 static void
8318 dtor_catch_solib (struct breakpoint *b)
8319 {
8320 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8321
8322 if (self->regex)
8323 regfree (&self->compiled);
8324 xfree (self->regex);
8325
8326 base_breakpoint_ops.dtor (b);
8327 }
8328
8329 static int
8330 insert_catch_solib (struct bp_location *ignore)
8331 {
8332 return 0;
8333 }
8334
8335 static int
8336 remove_catch_solib (struct bp_location *ignore, enum remove_bp_reason reason)
8337 {
8338 return 0;
8339 }
8340
8341 static int
8342 breakpoint_hit_catch_solib (const struct bp_location *bl,
8343 struct address_space *aspace,
8344 CORE_ADDR bp_addr,
8345 const struct target_waitstatus *ws)
8346 {
8347 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8348 struct breakpoint *other;
8349
8350 if (ws->kind == TARGET_WAITKIND_LOADED)
8351 return 1;
8352
8353 ALL_BREAKPOINTS (other)
8354 {
8355 struct bp_location *other_bl;
8356
8357 if (other == bl->owner)
8358 continue;
8359
8360 if (other->type != bp_shlib_event)
8361 continue;
8362
8363 if (self->base.pspace != NULL && other->pspace != self->base.pspace)
8364 continue;
8365
8366 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8367 {
8368 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8369 return 1;
8370 }
8371 }
8372
8373 return 0;
8374 }
8375
8376 static void
8377 check_status_catch_solib (struct bpstats *bs)
8378 {
8379 struct solib_catchpoint *self
8380 = (struct solib_catchpoint *) bs->breakpoint_at;
8381 int ix;
8382
8383 if (self->is_load)
8384 {
8385 struct so_list *iter;
8386
8387 for (ix = 0;
8388 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
8389 ix, iter);
8390 ++ix)
8391 {
8392 if (!self->regex
8393 || regexec (&self->compiled, iter->so_name, 0, NULL, 0) == 0)
8394 return;
8395 }
8396 }
8397 else
8398 {
8399 char *iter;
8400
8401 for (ix = 0;
8402 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
8403 ix, iter);
8404 ++ix)
8405 {
8406 if (!self->regex
8407 || regexec (&self->compiled, iter, 0, NULL, 0) == 0)
8408 return;
8409 }
8410 }
8411
8412 bs->stop = 0;
8413 bs->print_it = print_it_noop;
8414 }
8415
8416 static enum print_stop_action
8417 print_it_catch_solib (bpstat bs)
8418 {
8419 struct breakpoint *b = bs->breakpoint_at;
8420 struct ui_out *uiout = current_uiout;
8421
8422 annotate_catchpoint (b->number);
8423 maybe_print_thread_hit_breakpoint (uiout);
8424 if (b->disposition == disp_del)
8425 uiout->text ("Temporary catchpoint ");
8426 else
8427 uiout->text ("Catchpoint ");
8428 uiout->field_int ("bkptno", b->number);
8429 uiout->text ("\n");
8430 if (uiout->is_mi_like_p ())
8431 uiout->field_string ("disp", bpdisp_text (b->disposition));
8432 print_solib_event (1);
8433 return PRINT_SRC_AND_LOC;
8434 }
8435
8436 static void
8437 print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8438 {
8439 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8440 struct value_print_options opts;
8441 struct ui_out *uiout = current_uiout;
8442 char *msg;
8443
8444 get_user_print_options (&opts);
8445 /* Field 4, the address, is omitted (which makes the columns not
8446 line up too nicely with the headers, but the effect is relatively
8447 readable). */
8448 if (opts.addressprint)
8449 {
8450 annotate_field (4);
8451 uiout->field_skip ("addr");
8452 }
8453
8454 annotate_field (5);
8455 if (self->is_load)
8456 {
8457 if (self->regex)
8458 msg = xstrprintf (_("load of library matching %s"), self->regex);
8459 else
8460 msg = xstrdup (_("load of library"));
8461 }
8462 else
8463 {
8464 if (self->regex)
8465 msg = xstrprintf (_("unload of library matching %s"), self->regex);
8466 else
8467 msg = xstrdup (_("unload of library"));
8468 }
8469 uiout->field_string ("what", msg);
8470 xfree (msg);
8471
8472 if (uiout->is_mi_like_p ())
8473 uiout->field_string ("catch-type", self->is_load ? "load" : "unload");
8474 }
8475
8476 static void
8477 print_mention_catch_solib (struct breakpoint *b)
8478 {
8479 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8480
8481 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8482 self->is_load ? "load" : "unload");
8483 }
8484
8485 static void
8486 print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8487 {
8488 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8489
8490 fprintf_unfiltered (fp, "%s %s",
8491 b->disposition == disp_del ? "tcatch" : "catch",
8492 self->is_load ? "load" : "unload");
8493 if (self->regex)
8494 fprintf_unfiltered (fp, " %s", self->regex);
8495 fprintf_unfiltered (fp, "\n");
8496 }
8497
8498 static struct breakpoint_ops catch_solib_breakpoint_ops;
8499
8500 /* Shared helper function (MI and CLI) for creating and installing
8501 a shared object event catchpoint. If IS_LOAD is non-zero then
8502 the events to be caught are load events, otherwise they are
8503 unload events. If IS_TEMP is non-zero the catchpoint is a
8504 temporary one. If ENABLED is non-zero the catchpoint is
8505 created in an enabled state. */
8506
8507 void
8508 add_solib_catchpoint (const char *arg, int is_load, int is_temp, int enabled)
8509 {
8510 struct solib_catchpoint *c;
8511 struct gdbarch *gdbarch = get_current_arch ();
8512 struct cleanup *cleanup;
8513
8514 if (!arg)
8515 arg = "";
8516 arg = skip_spaces_const (arg);
8517
8518 c = new solib_catchpoint ();
8519 cleanup = make_cleanup (xfree, c);
8520
8521 if (*arg != '\0')
8522 {
8523 int errcode;
8524
8525 errcode = regcomp (&c->compiled, arg, REG_NOSUB);
8526 if (errcode != 0)
8527 {
8528 char *err = get_regcomp_error (errcode, &c->compiled);
8529
8530 make_cleanup (xfree, err);
8531 error (_("Invalid regexp (%s): %s"), err, arg);
8532 }
8533 c->regex = xstrdup (arg);
8534 }
8535
8536 c->is_load = is_load;
8537 init_catchpoint (&c->base, gdbarch, is_temp, NULL,
8538 &catch_solib_breakpoint_ops);
8539
8540 c->base.enable_state = enabled ? bp_enabled : bp_disabled;
8541
8542 discard_cleanups (cleanup);
8543 install_breakpoint (0, &c->base, 1);
8544 }
8545
8546 /* A helper function that does all the work for "catch load" and
8547 "catch unload". */
8548
8549 static void
8550 catch_load_or_unload (char *arg, int from_tty, int is_load,
8551 struct cmd_list_element *command)
8552 {
8553 int tempflag;
8554 const int enabled = 1;
8555
8556 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8557
8558 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8559 }
8560
8561 static void
8562 catch_load_command_1 (char *arg, int from_tty,
8563 struct cmd_list_element *command)
8564 {
8565 catch_load_or_unload (arg, from_tty, 1, command);
8566 }
8567
8568 static void
8569 catch_unload_command_1 (char *arg, int from_tty,
8570 struct cmd_list_element *command)
8571 {
8572 catch_load_or_unload (arg, from_tty, 0, command);
8573 }
8574
8575 /* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8576 is non-zero, then make the breakpoint temporary. If COND_STRING is
8577 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8578 the breakpoint_ops structure associated to the catchpoint. */
8579
8580 void
8581 init_catchpoint (struct breakpoint *b,
8582 struct gdbarch *gdbarch, int tempflag,
8583 const char *cond_string,
8584 const struct breakpoint_ops *ops)
8585 {
8586 struct symtab_and_line sal;
8587
8588 init_sal (&sal);
8589 sal.pspace = current_program_space;
8590
8591 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
8592
8593 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
8594 b->disposition = tempflag ? disp_del : disp_donttouch;
8595 }
8596
8597 void
8598 install_breakpoint (int internal, struct breakpoint *b, int update_gll)
8599 {
8600 add_to_breakpoint_chain (b);
8601 set_breakpoint_number (internal, b);
8602 if (is_tracepoint (b))
8603 set_tracepoint_count (breakpoint_count);
8604 if (!internal)
8605 mention (b);
8606 observer_notify_breakpoint_created (b);
8607
8608 if (update_gll)
8609 update_global_location_list (UGLL_MAY_INSERT);
8610 }
8611
8612 static void
8613 create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8614 int tempflag, const char *cond_string,
8615 const struct breakpoint_ops *ops)
8616 {
8617 struct fork_catchpoint *c = new fork_catchpoint ();
8618
8619 init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
8620
8621 c->forked_inferior_pid = null_ptid;
8622
8623 install_breakpoint (0, &c->base, 1);
8624 }
8625
8626 /* Exec catchpoints. */
8627
8628 /* An instance of this type is used to represent an exec catchpoint.
8629 It includes a "struct breakpoint" as a kind of base class; users
8630 downcast to "struct breakpoint *" when needed. A breakpoint is
8631 really of this type iff its ops pointer points to
8632 CATCH_EXEC_BREAKPOINT_OPS. */
8633
8634 struct exec_catchpoint
8635 {
8636 /* The base class. */
8637 struct breakpoint base;
8638
8639 /* Filename of a program whose exec triggered this catchpoint.
8640 This field is only valid immediately after this catchpoint has
8641 triggered. */
8642 char *exec_pathname;
8643 };
8644
8645 /* Implement the "dtor" breakpoint_ops method for exec
8646 catchpoints. */
8647
8648 static void
8649 dtor_catch_exec (struct breakpoint *b)
8650 {
8651 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8652
8653 xfree (c->exec_pathname);
8654
8655 base_breakpoint_ops.dtor (b);
8656 }
8657
8658 static int
8659 insert_catch_exec (struct bp_location *bl)
8660 {
8661 return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
8662 }
8663
8664 static int
8665 remove_catch_exec (struct bp_location *bl, enum remove_bp_reason reason)
8666 {
8667 return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
8668 }
8669
8670 static int
8671 breakpoint_hit_catch_exec (const struct bp_location *bl,
8672 struct address_space *aspace, CORE_ADDR bp_addr,
8673 const struct target_waitstatus *ws)
8674 {
8675 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8676
8677 if (ws->kind != TARGET_WAITKIND_EXECD)
8678 return 0;
8679
8680 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8681 return 1;
8682 }
8683
8684 static enum print_stop_action
8685 print_it_catch_exec (bpstat bs)
8686 {
8687 struct ui_out *uiout = current_uiout;
8688 struct breakpoint *b = bs->breakpoint_at;
8689 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8690
8691 annotate_catchpoint (b->number);
8692 maybe_print_thread_hit_breakpoint (uiout);
8693 if (b->disposition == disp_del)
8694 uiout->text ("Temporary catchpoint ");
8695 else
8696 uiout->text ("Catchpoint ");
8697 if (uiout->is_mi_like_p ())
8698 {
8699 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_EXEC));
8700 uiout->field_string ("disp", bpdisp_text (b->disposition));
8701 }
8702 uiout->field_int ("bkptno", b->number);
8703 uiout->text (" (exec'd ");
8704 uiout->field_string ("new-exec", c->exec_pathname);
8705 uiout->text ("), ");
8706
8707 return PRINT_SRC_AND_LOC;
8708 }
8709
8710 static void
8711 print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
8712 {
8713 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8714 struct value_print_options opts;
8715 struct ui_out *uiout = current_uiout;
8716
8717 get_user_print_options (&opts);
8718
8719 /* Field 4, the address, is omitted (which makes the columns
8720 not line up too nicely with the headers, but the effect
8721 is relatively readable). */
8722 if (opts.addressprint)
8723 uiout->field_skip ("addr");
8724 annotate_field (5);
8725 uiout->text ("exec");
8726 if (c->exec_pathname != NULL)
8727 {
8728 uiout->text (", program \"");
8729 uiout->field_string ("what", c->exec_pathname);
8730 uiout->text ("\" ");
8731 }
8732
8733 if (uiout->is_mi_like_p ())
8734 uiout->field_string ("catch-type", "exec");
8735 }
8736
8737 static void
8738 print_mention_catch_exec (struct breakpoint *b)
8739 {
8740 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8741 }
8742
8743 /* Implement the "print_recreate" breakpoint_ops method for exec
8744 catchpoints. */
8745
8746 static void
8747 print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8748 {
8749 fprintf_unfiltered (fp, "catch exec");
8750 print_recreate_thread (b, fp);
8751 }
8752
8753 static struct breakpoint_ops catch_exec_breakpoint_ops;
8754
8755 static int
8756 hw_breakpoint_used_count (void)
8757 {
8758 int i = 0;
8759 struct breakpoint *b;
8760 struct bp_location *bl;
8761
8762 ALL_BREAKPOINTS (b)
8763 {
8764 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
8765 for (bl = b->loc; bl; bl = bl->next)
8766 {
8767 /* Special types of hardware breakpoints may use more than
8768 one register. */
8769 i += b->ops->resources_needed (bl);
8770 }
8771 }
8772
8773 return i;
8774 }
8775
8776 /* Returns the resources B would use if it were a hardware
8777 watchpoint. */
8778
8779 static int
8780 hw_watchpoint_use_count (struct breakpoint *b)
8781 {
8782 int i = 0;
8783 struct bp_location *bl;
8784
8785 if (!breakpoint_enabled (b))
8786 return 0;
8787
8788 for (bl = b->loc; bl; bl = bl->next)
8789 {
8790 /* Special types of hardware watchpoints may use more than
8791 one register. */
8792 i += b->ops->resources_needed (bl);
8793 }
8794
8795 return i;
8796 }
8797
8798 /* Returns the sum the used resources of all hardware watchpoints of
8799 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8800 the sum of the used resources of all hardware watchpoints of other
8801 types _not_ TYPE. */
8802
8803 static int
8804 hw_watchpoint_used_count_others (struct breakpoint *except,
8805 enum bptype type, int *other_type_used)
8806 {
8807 int i = 0;
8808 struct breakpoint *b;
8809
8810 *other_type_used = 0;
8811 ALL_BREAKPOINTS (b)
8812 {
8813 if (b == except)
8814 continue;
8815 if (!breakpoint_enabled (b))
8816 continue;
8817
8818 if (b->type == type)
8819 i += hw_watchpoint_use_count (b);
8820 else if (is_hardware_watchpoint (b))
8821 *other_type_used = 1;
8822 }
8823
8824 return i;
8825 }
8826
8827 void
8828 disable_watchpoints_before_interactive_call_start (void)
8829 {
8830 struct breakpoint *b;
8831
8832 ALL_BREAKPOINTS (b)
8833 {
8834 if (is_watchpoint (b) && breakpoint_enabled (b))
8835 {
8836 b->enable_state = bp_call_disabled;
8837 update_global_location_list (UGLL_DONT_INSERT);
8838 }
8839 }
8840 }
8841
8842 void
8843 enable_watchpoints_after_interactive_call_stop (void)
8844 {
8845 struct breakpoint *b;
8846
8847 ALL_BREAKPOINTS (b)
8848 {
8849 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
8850 {
8851 b->enable_state = bp_enabled;
8852 update_global_location_list (UGLL_MAY_INSERT);
8853 }
8854 }
8855 }
8856
8857 void
8858 disable_breakpoints_before_startup (void)
8859 {
8860 current_program_space->executing_startup = 1;
8861 update_global_location_list (UGLL_DONT_INSERT);
8862 }
8863
8864 void
8865 enable_breakpoints_after_startup (void)
8866 {
8867 current_program_space->executing_startup = 0;
8868 breakpoint_re_set ();
8869 }
8870
8871 /* Create a new single-step breakpoint for thread THREAD, with no
8872 locations. */
8873
8874 static struct breakpoint *
8875 new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8876 {
8877 struct breakpoint *b = new breakpoint ();
8878
8879 init_raw_breakpoint_without_location (b, gdbarch, bp_single_step,
8880 &momentary_breakpoint_ops);
8881
8882 b->disposition = disp_donttouch;
8883 b->frame_id = null_frame_id;
8884
8885 b->thread = thread;
8886 gdb_assert (b->thread != 0);
8887
8888 add_to_breakpoint_chain (b);
8889
8890 return b;
8891 }
8892
8893 /* Set a momentary breakpoint of type TYPE at address specified by
8894 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8895 frame. */
8896
8897 struct breakpoint *
8898 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8899 struct frame_id frame_id, enum bptype type)
8900 {
8901 struct breakpoint *b;
8902
8903 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8904 tail-called one. */
8905 gdb_assert (!frame_id_artificial_p (frame_id));
8906
8907 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
8908 b->enable_state = bp_enabled;
8909 b->disposition = disp_donttouch;
8910 b->frame_id = frame_id;
8911
8912 /* If we're debugging a multi-threaded program, then we want
8913 momentary breakpoints to be active in only a single thread of
8914 control. */
8915 if (in_thread_list (inferior_ptid))
8916 b->thread = ptid_to_global_thread_id (inferior_ptid);
8917
8918 update_global_location_list_nothrow (UGLL_MAY_INSERT);
8919
8920 return b;
8921 }
8922
8923 /* Make a momentary breakpoint based on the master breakpoint ORIG.
8924 The new breakpoint will have type TYPE, use OPS as its
8925 breakpoint_ops, and will set enabled to LOC_ENABLED. */
8926
8927 static struct breakpoint *
8928 momentary_breakpoint_from_master (struct breakpoint *orig,
8929 enum bptype type,
8930 const struct breakpoint_ops *ops,
8931 int loc_enabled)
8932 {
8933 struct breakpoint *copy;
8934
8935 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
8936 copy->loc = allocate_bp_location (copy);
8937 set_breakpoint_location_function (copy->loc, 1);
8938
8939 copy->loc->gdbarch = orig->loc->gdbarch;
8940 copy->loc->requested_address = orig->loc->requested_address;
8941 copy->loc->address = orig->loc->address;
8942 copy->loc->section = orig->loc->section;
8943 copy->loc->pspace = orig->loc->pspace;
8944 copy->loc->probe = orig->loc->probe;
8945 copy->loc->line_number = orig->loc->line_number;
8946 copy->loc->symtab = orig->loc->symtab;
8947 copy->loc->enabled = loc_enabled;
8948 copy->frame_id = orig->frame_id;
8949 copy->thread = orig->thread;
8950 copy->pspace = orig->pspace;
8951
8952 copy->enable_state = bp_enabled;
8953 copy->disposition = disp_donttouch;
8954 copy->number = internal_breakpoint_number--;
8955
8956 update_global_location_list_nothrow (UGLL_DONT_INSERT);
8957 return copy;
8958 }
8959
8960 /* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8961 ORIG is NULL. */
8962
8963 struct breakpoint *
8964 clone_momentary_breakpoint (struct breakpoint *orig)
8965 {
8966 /* If there's nothing to clone, then return nothing. */
8967 if (orig == NULL)
8968 return NULL;
8969
8970 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
8971 }
8972
8973 struct breakpoint *
8974 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8975 enum bptype type)
8976 {
8977 struct symtab_and_line sal;
8978
8979 sal = find_pc_line (pc, 0);
8980 sal.pc = pc;
8981 sal.section = find_pc_overlay (pc);
8982 sal.explicit_pc = 1;
8983
8984 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
8985 }
8986 \f
8987
8988 /* Tell the user we have just set a breakpoint B. */
8989
8990 static void
8991 mention (struct breakpoint *b)
8992 {
8993 b->ops->print_mention (b);
8994 if (current_uiout->is_mi_like_p ())
8995 return;
8996 printf_filtered ("\n");
8997 }
8998 \f
8999
9000 static int bp_loc_is_permanent (struct bp_location *loc);
9001
9002 static struct bp_location *
9003 add_location_to_breakpoint (struct breakpoint *b,
9004 const struct symtab_and_line *sal)
9005 {
9006 struct bp_location *loc, **tmp;
9007 CORE_ADDR adjusted_address;
9008 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
9009
9010 if (loc_gdbarch == NULL)
9011 loc_gdbarch = b->gdbarch;
9012
9013 /* Adjust the breakpoint's address prior to allocating a location.
9014 Once we call allocate_bp_location(), that mostly uninitialized
9015 location will be placed on the location chain. Adjustment of the
9016 breakpoint may cause target_read_memory() to be called and we do
9017 not want its scan of the location chain to find a breakpoint and
9018 location that's only been partially initialized. */
9019 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
9020 sal->pc, b->type);
9021
9022 /* Sort the locations by their ADDRESS. */
9023 loc = allocate_bp_location (b);
9024 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
9025 tmp = &((*tmp)->next))
9026 ;
9027 loc->next = *tmp;
9028 *tmp = loc;
9029
9030 loc->requested_address = sal->pc;
9031 loc->address = adjusted_address;
9032 loc->pspace = sal->pspace;
9033 loc->probe.probe = sal->probe;
9034 loc->probe.objfile = sal->objfile;
9035 gdb_assert (loc->pspace != NULL);
9036 loc->section = sal->section;
9037 loc->gdbarch = loc_gdbarch;
9038 loc->line_number = sal->line;
9039 loc->symtab = sal->symtab;
9040
9041 set_breakpoint_location_function (loc,
9042 sal->explicit_pc || sal->explicit_line);
9043
9044 /* While by definition, permanent breakpoints are already present in the
9045 code, we don't mark the location as inserted. Normally one would expect
9046 that GDB could rely on that breakpoint instruction to stop the program,
9047 thus removing the need to insert its own breakpoint, except that executing
9048 the breakpoint instruction can kill the target instead of reporting a
9049 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
9050 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
9051 with "Trap 0x02 while interrupts disabled, Error state". Letting the
9052 breakpoint be inserted normally results in QEMU knowing about the GDB
9053 breakpoint, and thus trap before the breakpoint instruction is executed.
9054 (If GDB later needs to continue execution past the permanent breakpoint,
9055 it manually increments the PC, thus avoiding executing the breakpoint
9056 instruction.) */
9057 if (bp_loc_is_permanent (loc))
9058 loc->permanent = 1;
9059
9060 return loc;
9061 }
9062 \f
9063
9064 /* See breakpoint.h. */
9065
9066 int
9067 program_breakpoint_here_p (struct gdbarch *gdbarch, CORE_ADDR address)
9068 {
9069 int len;
9070 CORE_ADDR addr;
9071 const gdb_byte *bpoint;
9072 gdb_byte *target_mem;
9073 struct cleanup *cleanup;
9074 int retval = 0;
9075
9076 addr = address;
9077 bpoint = gdbarch_breakpoint_from_pc (gdbarch, &addr, &len);
9078
9079 /* Software breakpoints unsupported? */
9080 if (bpoint == NULL)
9081 return 0;
9082
9083 target_mem = (gdb_byte *) alloca (len);
9084
9085 /* Enable the automatic memory restoration from breakpoints while
9086 we read the memory. Otherwise we could say about our temporary
9087 breakpoints they are permanent. */
9088 cleanup = make_show_memory_breakpoints_cleanup (0);
9089
9090 if (target_read_memory (address, target_mem, len) == 0
9091 && memcmp (target_mem, bpoint, len) == 0)
9092 retval = 1;
9093
9094 do_cleanups (cleanup);
9095
9096 return retval;
9097 }
9098
9099 /* Return 1 if LOC is pointing to a permanent breakpoint,
9100 return 0 otherwise. */
9101
9102 static int
9103 bp_loc_is_permanent (struct bp_location *loc)
9104 {
9105 struct cleanup *cleanup;
9106 int retval;
9107
9108 gdb_assert (loc != NULL);
9109
9110 /* If we have a catchpoint or a watchpoint, just return 0. We should not
9111 attempt to read from the addresses the locations of these breakpoint types
9112 point to. program_breakpoint_here_p, below, will attempt to read
9113 memory. */
9114 if (!breakpoint_address_is_meaningful (loc->owner))
9115 return 0;
9116
9117 cleanup = save_current_space_and_thread ();
9118 switch_to_program_space_and_thread (loc->pspace);
9119
9120 retval = program_breakpoint_here_p (loc->gdbarch, loc->address);
9121
9122 do_cleanups (cleanup);
9123
9124 return retval;
9125 }
9126
9127 /* Build a command list for the dprintf corresponding to the current
9128 settings of the dprintf style options. */
9129
9130 static void
9131 update_dprintf_command_list (struct breakpoint *b)
9132 {
9133 char *dprintf_args = b->extra_string;
9134 char *printf_line = NULL;
9135
9136 if (!dprintf_args)
9137 return;
9138
9139 dprintf_args = skip_spaces (dprintf_args);
9140
9141 /* Allow a comma, as it may have terminated a location, but don't
9142 insist on it. */
9143 if (*dprintf_args == ',')
9144 ++dprintf_args;
9145 dprintf_args = skip_spaces (dprintf_args);
9146
9147 if (*dprintf_args != '"')
9148 error (_("Bad format string, missing '\"'."));
9149
9150 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
9151 printf_line = xstrprintf ("printf %s", dprintf_args);
9152 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
9153 {
9154 if (!dprintf_function)
9155 error (_("No function supplied for dprintf call"));
9156
9157 if (dprintf_channel && strlen (dprintf_channel) > 0)
9158 printf_line = xstrprintf ("call (void) %s (%s,%s)",
9159 dprintf_function,
9160 dprintf_channel,
9161 dprintf_args);
9162 else
9163 printf_line = xstrprintf ("call (void) %s (%s)",
9164 dprintf_function,
9165 dprintf_args);
9166 }
9167 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
9168 {
9169 if (target_can_run_breakpoint_commands ())
9170 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
9171 else
9172 {
9173 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
9174 printf_line = xstrprintf ("printf %s", dprintf_args);
9175 }
9176 }
9177 else
9178 internal_error (__FILE__, __LINE__,
9179 _("Invalid dprintf style."));
9180
9181 gdb_assert (printf_line != NULL);
9182 /* Manufacture a printf sequence. */
9183 {
9184 struct command_line *printf_cmd_line = XNEW (struct command_line);
9185
9186 printf_cmd_line->control_type = simple_control;
9187 printf_cmd_line->body_count = 0;
9188 printf_cmd_line->body_list = NULL;
9189 printf_cmd_line->next = NULL;
9190 printf_cmd_line->line = printf_line;
9191
9192 breakpoint_set_commands (b, command_line_up (printf_cmd_line));
9193 }
9194 }
9195
9196 /* Update all dprintf commands, making their command lists reflect
9197 current style settings. */
9198
9199 static void
9200 update_dprintf_commands (char *args, int from_tty,
9201 struct cmd_list_element *c)
9202 {
9203 struct breakpoint *b;
9204
9205 ALL_BREAKPOINTS (b)
9206 {
9207 if (b->type == bp_dprintf)
9208 update_dprintf_command_list (b);
9209 }
9210 }
9211
9212 /* Create a breakpoint with SAL as location. Use LOCATION
9213 as a description of the location, and COND_STRING
9214 as condition expression. If LOCATION is NULL then create an
9215 "address location" from the address in the SAL. */
9216
9217 static void
9218 init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
9219 struct symtabs_and_lines sals,
9220 event_location_up &&location,
9221 char *filter, char *cond_string,
9222 char *extra_string,
9223 enum bptype type, enum bpdisp disposition,
9224 int thread, int task, int ignore_count,
9225 const struct breakpoint_ops *ops, int from_tty,
9226 int enabled, int internal, unsigned flags,
9227 int display_canonical)
9228 {
9229 int i;
9230
9231 if (type == bp_hardware_breakpoint)
9232 {
9233 int target_resources_ok;
9234
9235 i = hw_breakpoint_used_count ();
9236 target_resources_ok =
9237 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9238 i + 1, 0);
9239 if (target_resources_ok == 0)
9240 error (_("No hardware breakpoint support in the target."));
9241 else if (target_resources_ok < 0)
9242 error (_("Hardware breakpoints used exceeds limit."));
9243 }
9244
9245 gdb_assert (sals.nelts > 0);
9246
9247 for (i = 0; i < sals.nelts; ++i)
9248 {
9249 struct symtab_and_line sal = sals.sals[i];
9250 struct bp_location *loc;
9251
9252 if (from_tty)
9253 {
9254 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9255 if (!loc_gdbarch)
9256 loc_gdbarch = gdbarch;
9257
9258 describe_other_breakpoints (loc_gdbarch,
9259 sal.pspace, sal.pc, sal.section, thread);
9260 }
9261
9262 if (i == 0)
9263 {
9264 init_raw_breakpoint (b, gdbarch, sal, type, ops);
9265 b->thread = thread;
9266 b->task = task;
9267
9268 b->cond_string = cond_string;
9269 b->extra_string = extra_string;
9270 b->ignore_count = ignore_count;
9271 b->enable_state = enabled ? bp_enabled : bp_disabled;
9272 b->disposition = disposition;
9273
9274 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9275 b->loc->inserted = 1;
9276
9277 if (type == bp_static_tracepoint)
9278 {
9279 struct tracepoint *t = (struct tracepoint *) b;
9280 struct static_tracepoint_marker marker;
9281
9282 if (strace_marker_p (b))
9283 {
9284 /* We already know the marker exists, otherwise, we
9285 wouldn't see a sal for it. */
9286 const char *p
9287 = &event_location_to_string (b->location.get ())[3];
9288 const char *endp;
9289 char *marker_str;
9290
9291 p = skip_spaces_const (p);
9292
9293 endp = skip_to_space_const (p);
9294
9295 marker_str = savestring (p, endp - p);
9296 t->static_trace_marker_id = marker_str;
9297
9298 printf_filtered (_("Probed static tracepoint "
9299 "marker \"%s\"\n"),
9300 t->static_trace_marker_id);
9301 }
9302 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
9303 {
9304 t->static_trace_marker_id = xstrdup (marker.str_id);
9305 release_static_tracepoint_marker (&marker);
9306
9307 printf_filtered (_("Probed static tracepoint "
9308 "marker \"%s\"\n"),
9309 t->static_trace_marker_id);
9310 }
9311 else
9312 warning (_("Couldn't determine the static "
9313 "tracepoint marker to probe"));
9314 }
9315
9316 loc = b->loc;
9317 }
9318 else
9319 {
9320 loc = add_location_to_breakpoint (b, &sal);
9321 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9322 loc->inserted = 1;
9323 }
9324
9325 if (b->cond_string)
9326 {
9327 const char *arg = b->cond_string;
9328
9329 loc->cond = parse_exp_1 (&arg, loc->address,
9330 block_for_pc (loc->address), 0);
9331 if (*arg)
9332 error (_("Garbage '%s' follows condition"), arg);
9333 }
9334
9335 /* Dynamic printf requires and uses additional arguments on the
9336 command line, otherwise it's an error. */
9337 if (type == bp_dprintf)
9338 {
9339 if (b->extra_string)
9340 update_dprintf_command_list (b);
9341 else
9342 error (_("Format string required"));
9343 }
9344 else if (b->extra_string)
9345 error (_("Garbage '%s' at end of command"), b->extra_string);
9346 }
9347
9348 b->display_canonical = display_canonical;
9349 if (location != NULL)
9350 b->location = std::move (location);
9351 else
9352 b->location = new_address_location (b->loc->address, NULL, 0);
9353 b->filter = filter;
9354 }
9355
9356 static void
9357 create_breakpoint_sal (struct gdbarch *gdbarch,
9358 struct symtabs_and_lines sals,
9359 event_location_up &&location,
9360 char *filter, char *cond_string,
9361 char *extra_string,
9362 enum bptype type, enum bpdisp disposition,
9363 int thread, int task, int ignore_count,
9364 const struct breakpoint_ops *ops, int from_tty,
9365 int enabled, int internal, unsigned flags,
9366 int display_canonical)
9367 {
9368 struct breakpoint *b;
9369 struct cleanup *old_chain;
9370
9371 if (is_tracepoint_type (type))
9372 {
9373 struct tracepoint *t;
9374
9375 t = new tracepoint ();
9376 b = &t->base;
9377 }
9378 else
9379 b = new breakpoint ();
9380
9381 old_chain = make_cleanup (xfree, b);
9382
9383 init_breakpoint_sal (b, gdbarch,
9384 sals, std::move (location),
9385 filter, cond_string, extra_string,
9386 type, disposition,
9387 thread, task, ignore_count,
9388 ops, from_tty,
9389 enabled, internal, flags,
9390 display_canonical);
9391 discard_cleanups (old_chain);
9392
9393 install_breakpoint (internal, b, 0);
9394 }
9395
9396 /* Add SALS.nelts breakpoints to the breakpoint table. For each
9397 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9398 value. COND_STRING, if not NULL, specified the condition to be
9399 used for all breakpoints. Essentially the only case where
9400 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9401 function. In that case, it's still not possible to specify
9402 separate conditions for different overloaded functions, so
9403 we take just a single condition string.
9404
9405 NOTE: If the function succeeds, the caller is expected to cleanup
9406 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
9407 array contents). If the function fails (error() is called), the
9408 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
9409 COND and SALS arrays and each of those arrays contents. */
9410
9411 static void
9412 create_breakpoints_sal (struct gdbarch *gdbarch,
9413 struct linespec_result *canonical,
9414 char *cond_string, char *extra_string,
9415 enum bptype type, enum bpdisp disposition,
9416 int thread, int task, int ignore_count,
9417 const struct breakpoint_ops *ops, int from_tty,
9418 int enabled, int internal, unsigned flags)
9419 {
9420 int i;
9421 struct linespec_sals *lsal;
9422
9423 if (canonical->pre_expanded)
9424 gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
9425
9426 for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
9427 {
9428 /* Note that 'location' can be NULL in the case of a plain
9429 'break', without arguments. */
9430 event_location_up location
9431 = (canonical->location != NULL
9432 ? copy_event_location (canonical->location) : NULL);
9433 char *filter_string = lsal->canonical ? xstrdup (lsal->canonical) : NULL;
9434
9435 make_cleanup (xfree, filter_string);
9436 create_breakpoint_sal (gdbarch, lsal->sals,
9437 std::move (location),
9438 filter_string,
9439 cond_string, extra_string,
9440 type, disposition,
9441 thread, task, ignore_count, ops,
9442 from_tty, enabled, internal, flags,
9443 canonical->special_display);
9444 }
9445 }
9446
9447 /* Parse LOCATION which is assumed to be a SAL specification possibly
9448 followed by conditionals. On return, SALS contains an array of SAL
9449 addresses found. LOCATION points to the end of the SAL (for
9450 linespec locations).
9451
9452 The array and the line spec strings are allocated on the heap, it is
9453 the caller's responsibility to free them. */
9454
9455 static void
9456 parse_breakpoint_sals (const struct event_location *location,
9457 struct linespec_result *canonical)
9458 {
9459 struct symtab_and_line cursal;
9460
9461 if (event_location_type (location) == LINESPEC_LOCATION)
9462 {
9463 const char *address = get_linespec_location (location);
9464
9465 if (address == NULL)
9466 {
9467 /* The last displayed codepoint, if it's valid, is our default
9468 breakpoint address. */
9469 if (last_displayed_sal_is_valid ())
9470 {
9471 struct linespec_sals lsal;
9472 struct symtab_and_line sal;
9473 CORE_ADDR pc;
9474
9475 init_sal (&sal); /* Initialize to zeroes. */
9476 lsal.sals.sals = XNEW (struct symtab_and_line);
9477
9478 /* Set sal's pspace, pc, symtab, and line to the values
9479 corresponding to the last call to print_frame_info.
9480 Be sure to reinitialize LINE with NOTCURRENT == 0
9481 as the breakpoint line number is inappropriate otherwise.
9482 find_pc_line would adjust PC, re-set it back. */
9483 get_last_displayed_sal (&sal);
9484 pc = sal.pc;
9485 sal = find_pc_line (pc, 0);
9486
9487 /* "break" without arguments is equivalent to "break *PC"
9488 where PC is the last displayed codepoint's address. So
9489 make sure to set sal.explicit_pc to prevent GDB from
9490 trying to expand the list of sals to include all other
9491 instances with the same symtab and line. */
9492 sal.pc = pc;
9493 sal.explicit_pc = 1;
9494
9495 lsal.sals.sals[0] = sal;
9496 lsal.sals.nelts = 1;
9497 lsal.canonical = NULL;
9498
9499 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
9500 return;
9501 }
9502 else
9503 error (_("No default breakpoint address now."));
9504 }
9505 }
9506
9507 /* Force almost all breakpoints to be in terms of the
9508 current_source_symtab (which is decode_line_1's default).
9509 This should produce the results we want almost all of the
9510 time while leaving default_breakpoint_* alone.
9511
9512 ObjC: However, don't match an Objective-C method name which
9513 may have a '+' or '-' succeeded by a '['. */
9514 cursal = get_current_source_symtab_and_line ();
9515 if (last_displayed_sal_is_valid ())
9516 {
9517 const char *address = NULL;
9518
9519 if (event_location_type (location) == LINESPEC_LOCATION)
9520 address = get_linespec_location (location);
9521
9522 if (!cursal.symtab
9523 || (address != NULL
9524 && strchr ("+-", address[0]) != NULL
9525 && address[1] != '['))
9526 {
9527 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9528 get_last_displayed_symtab (),
9529 get_last_displayed_line (),
9530 canonical, NULL, NULL);
9531 return;
9532 }
9533 }
9534
9535 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9536 cursal.symtab, cursal.line, canonical, NULL, NULL);
9537 }
9538
9539
9540 /* Convert each SAL into a real PC. Verify that the PC can be
9541 inserted as a breakpoint. If it can't throw an error. */
9542
9543 static void
9544 breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
9545 {
9546 int i;
9547
9548 for (i = 0; i < sals->nelts; i++)
9549 resolve_sal_pc (&sals->sals[i]);
9550 }
9551
9552 /* Fast tracepoints may have restrictions on valid locations. For
9553 instance, a fast tracepoint using a jump instead of a trap will
9554 likely have to overwrite more bytes than a trap would, and so can
9555 only be placed where the instruction is longer than the jump, or a
9556 multi-instruction sequence does not have a jump into the middle of
9557 it, etc. */
9558
9559 static void
9560 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9561 struct symtabs_and_lines *sals)
9562 {
9563 int i, rslt;
9564 struct symtab_and_line *sal;
9565 char *msg;
9566 struct cleanup *old_chain;
9567
9568 for (i = 0; i < sals->nelts; i++)
9569 {
9570 struct gdbarch *sarch;
9571
9572 sal = &sals->sals[i];
9573
9574 sarch = get_sal_arch (*sal);
9575 /* We fall back to GDBARCH if there is no architecture
9576 associated with SAL. */
9577 if (sarch == NULL)
9578 sarch = gdbarch;
9579 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc, &msg);
9580 old_chain = make_cleanup (xfree, msg);
9581
9582 if (!rslt)
9583 error (_("May not have a fast tracepoint at %s%s"),
9584 paddress (sarch, sal->pc), (msg ? msg : ""));
9585
9586 do_cleanups (old_chain);
9587 }
9588 }
9589
9590 /* Given TOK, a string specification of condition and thread, as
9591 accepted by the 'break' command, extract the condition
9592 string and thread number and set *COND_STRING and *THREAD.
9593 PC identifies the context at which the condition should be parsed.
9594 If no condition is found, *COND_STRING is set to NULL.
9595 If no thread is found, *THREAD is set to -1. */
9596
9597 static void
9598 find_condition_and_thread (const char *tok, CORE_ADDR pc,
9599 char **cond_string, int *thread, int *task,
9600 char **rest)
9601 {
9602 *cond_string = NULL;
9603 *thread = -1;
9604 *task = 0;
9605 *rest = NULL;
9606
9607 while (tok && *tok)
9608 {
9609 const char *end_tok;
9610 int toklen;
9611 const char *cond_start = NULL;
9612 const char *cond_end = NULL;
9613
9614 tok = skip_spaces_const (tok);
9615
9616 if ((*tok == '"' || *tok == ',') && rest)
9617 {
9618 *rest = savestring (tok, strlen (tok));
9619 return;
9620 }
9621
9622 end_tok = skip_to_space_const (tok);
9623
9624 toklen = end_tok - tok;
9625
9626 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9627 {
9628 tok = cond_start = end_tok + 1;
9629 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
9630 cond_end = tok;
9631 *cond_string = savestring (cond_start, cond_end - cond_start);
9632 }
9633 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9634 {
9635 const char *tmptok;
9636 struct thread_info *thr;
9637
9638 tok = end_tok + 1;
9639 thr = parse_thread_id (tok, &tmptok);
9640 if (tok == tmptok)
9641 error (_("Junk after thread keyword."));
9642 *thread = thr->global_num;
9643 tok = tmptok;
9644 }
9645 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9646 {
9647 char *tmptok;
9648
9649 tok = end_tok + 1;
9650 *task = strtol (tok, &tmptok, 0);
9651 if (tok == tmptok)
9652 error (_("Junk after task keyword."));
9653 if (!valid_task_id (*task))
9654 error (_("Unknown task %d."), *task);
9655 tok = tmptok;
9656 }
9657 else if (rest)
9658 {
9659 *rest = savestring (tok, strlen (tok));
9660 return;
9661 }
9662 else
9663 error (_("Junk at end of arguments."));
9664 }
9665 }
9666
9667 /* Decode a static tracepoint marker spec. */
9668
9669 static struct symtabs_and_lines
9670 decode_static_tracepoint_spec (const char **arg_p)
9671 {
9672 VEC(static_tracepoint_marker_p) *markers = NULL;
9673 struct symtabs_and_lines sals;
9674 struct cleanup *old_chain;
9675 const char *p = &(*arg_p)[3];
9676 const char *endp;
9677 char *marker_str;
9678 int i;
9679
9680 p = skip_spaces_const (p);
9681
9682 endp = skip_to_space_const (p);
9683
9684 marker_str = savestring (p, endp - p);
9685 old_chain = make_cleanup (xfree, marker_str);
9686
9687 markers = target_static_tracepoint_markers_by_strid (marker_str);
9688 if (VEC_empty(static_tracepoint_marker_p, markers))
9689 error (_("No known static tracepoint marker named %s"), marker_str);
9690
9691 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
9692 sals.sals = XNEWVEC (struct symtab_and_line, sals.nelts);
9693
9694 for (i = 0; i < sals.nelts; i++)
9695 {
9696 struct static_tracepoint_marker *marker;
9697
9698 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9699
9700 init_sal (&sals.sals[i]);
9701
9702 sals.sals[i] = find_pc_line (marker->address, 0);
9703 sals.sals[i].pc = marker->address;
9704
9705 release_static_tracepoint_marker (marker);
9706 }
9707
9708 do_cleanups (old_chain);
9709
9710 *arg_p = endp;
9711 return sals;
9712 }
9713
9714 /* See breakpoint.h. */
9715
9716 int
9717 create_breakpoint (struct gdbarch *gdbarch,
9718 const struct event_location *location, char *cond_string,
9719 int thread, char *extra_string,
9720 int parse_extra,
9721 int tempflag, enum bptype type_wanted,
9722 int ignore_count,
9723 enum auto_boolean pending_break_support,
9724 const struct breakpoint_ops *ops,
9725 int from_tty, int enabled, int internal,
9726 unsigned flags)
9727 {
9728 struct linespec_result canonical;
9729 struct cleanup *old_chain;
9730 struct cleanup *bkpt_chain = NULL;
9731 int pending = 0;
9732 int task = 0;
9733 int prev_bkpt_count = breakpoint_count;
9734
9735 gdb_assert (ops != NULL);
9736
9737 /* If extra_string isn't useful, set it to NULL. */
9738 if (extra_string != NULL && *extra_string == '\0')
9739 extra_string = NULL;
9740
9741 init_linespec_result (&canonical);
9742
9743 TRY
9744 {
9745 ops->create_sals_from_location (location, &canonical, type_wanted);
9746 }
9747 CATCH (e, RETURN_MASK_ERROR)
9748 {
9749 /* If caller is interested in rc value from parse, set
9750 value. */
9751 if (e.error == NOT_FOUND_ERROR)
9752 {
9753 /* If pending breakpoint support is turned off, throw
9754 error. */
9755
9756 if (pending_break_support == AUTO_BOOLEAN_FALSE)
9757 throw_exception (e);
9758
9759 exception_print (gdb_stderr, e);
9760
9761 /* If pending breakpoint support is auto query and the user
9762 selects no, then simply return the error code. */
9763 if (pending_break_support == AUTO_BOOLEAN_AUTO
9764 && !nquery (_("Make %s pending on future shared library load? "),
9765 bptype_string (type_wanted)))
9766 return 0;
9767
9768 /* At this point, either the user was queried about setting
9769 a pending breakpoint and selected yes, or pending
9770 breakpoint behavior is on and thus a pending breakpoint
9771 is defaulted on behalf of the user. */
9772 pending = 1;
9773 }
9774 else
9775 throw_exception (e);
9776 }
9777 END_CATCH
9778
9779 if (!pending && VEC_empty (linespec_sals, canonical.sals))
9780 return 0;
9781
9782 /* Create a chain of things that always need to be cleaned up. */
9783 old_chain = make_cleanup_destroy_linespec_result (&canonical);
9784
9785 /* ----------------------------- SNIP -----------------------------
9786 Anything added to the cleanup chain beyond this point is assumed
9787 to be part of a breakpoint. If the breakpoint create succeeds
9788 then the memory is not reclaimed. */
9789 bkpt_chain = make_cleanup (null_cleanup, 0);
9790
9791 /* Resolve all line numbers to PC's and verify that the addresses
9792 are ok for the target. */
9793 if (!pending)
9794 {
9795 int ix;
9796 struct linespec_sals *iter;
9797
9798 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9799 breakpoint_sals_to_pc (&iter->sals);
9800 }
9801
9802 /* Fast tracepoints may have additional restrictions on location. */
9803 if (!pending && type_wanted == bp_fast_tracepoint)
9804 {
9805 int ix;
9806 struct linespec_sals *iter;
9807
9808 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9809 check_fast_tracepoint_sals (gdbarch, &iter->sals);
9810 }
9811
9812 /* Verify that condition can be parsed, before setting any
9813 breakpoints. Allocate a separate condition expression for each
9814 breakpoint. */
9815 if (!pending)
9816 {
9817 if (parse_extra)
9818 {
9819 char *rest;
9820 struct linespec_sals *lsal;
9821
9822 lsal = VEC_index (linespec_sals, canonical.sals, 0);
9823
9824 /* Here we only parse 'arg' to separate condition
9825 from thread number, so parsing in context of first
9826 sal is OK. When setting the breakpoint we'll
9827 re-parse it in context of each sal. */
9828
9829 find_condition_and_thread (extra_string, lsal->sals.sals[0].pc,
9830 &cond_string, &thread, &task, &rest);
9831 if (cond_string)
9832 make_cleanup (xfree, cond_string);
9833 if (rest)
9834 make_cleanup (xfree, rest);
9835 if (rest)
9836 extra_string = rest;
9837 else
9838 extra_string = NULL;
9839 }
9840 else
9841 {
9842 if (type_wanted != bp_dprintf
9843 && extra_string != NULL && *extra_string != '\0')
9844 error (_("Garbage '%s' at end of location"), extra_string);
9845
9846 /* Create a private copy of condition string. */
9847 if (cond_string)
9848 {
9849 cond_string = xstrdup (cond_string);
9850 make_cleanup (xfree, cond_string);
9851 }
9852 /* Create a private copy of any extra string. */
9853 if (extra_string)
9854 {
9855 extra_string = xstrdup (extra_string);
9856 make_cleanup (xfree, extra_string);
9857 }
9858 }
9859
9860 ops->create_breakpoints_sal (gdbarch, &canonical,
9861 cond_string, extra_string, type_wanted,
9862 tempflag ? disp_del : disp_donttouch,
9863 thread, task, ignore_count, ops,
9864 from_tty, enabled, internal, flags);
9865 }
9866 else
9867 {
9868 struct breakpoint *b;
9869
9870 if (is_tracepoint_type (type_wanted))
9871 {
9872 struct tracepoint *t;
9873
9874 t = new tracepoint ();
9875 b = &t->base;
9876 }
9877 else
9878 b = new breakpoint ();
9879
9880 init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops);
9881 b->location = copy_event_location (location);
9882
9883 if (parse_extra)
9884 b->cond_string = NULL;
9885 else
9886 {
9887 /* Create a private copy of condition string. */
9888 if (cond_string)
9889 {
9890 cond_string = xstrdup (cond_string);
9891 make_cleanup (xfree, cond_string);
9892 }
9893 b->cond_string = cond_string;
9894 b->thread = thread;
9895 }
9896
9897 /* Create a private copy of any extra string. */
9898 if (extra_string != NULL)
9899 {
9900 extra_string = xstrdup (extra_string);
9901 make_cleanup (xfree, extra_string);
9902 }
9903 b->extra_string = extra_string;
9904 b->ignore_count = ignore_count;
9905 b->disposition = tempflag ? disp_del : disp_donttouch;
9906 b->condition_not_parsed = 1;
9907 b->enable_state = enabled ? bp_enabled : bp_disabled;
9908 if ((type_wanted != bp_breakpoint
9909 && type_wanted != bp_hardware_breakpoint) || thread != -1)
9910 b->pspace = current_program_space;
9911
9912 install_breakpoint (internal, b, 0);
9913 }
9914
9915 if (VEC_length (linespec_sals, canonical.sals) > 1)
9916 {
9917 warning (_("Multiple breakpoints were set.\nUse the "
9918 "\"delete\" command to delete unwanted breakpoints."));
9919 prev_breakpoint_count = prev_bkpt_count;
9920 }
9921
9922 /* That's it. Discard the cleanups for data inserted into the
9923 breakpoint. */
9924 discard_cleanups (bkpt_chain);
9925 /* But cleanup everything else. */
9926 do_cleanups (old_chain);
9927
9928 /* error call may happen here - have BKPT_CHAIN already discarded. */
9929 update_global_location_list (UGLL_MAY_INSERT);
9930
9931 return 1;
9932 }
9933
9934 /* Set a breakpoint.
9935 ARG is a string describing breakpoint address,
9936 condition, and thread.
9937 FLAG specifies if a breakpoint is hardware on,
9938 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9939 and BP_TEMPFLAG. */
9940
9941 static void
9942 break_command_1 (char *arg, int flag, int from_tty)
9943 {
9944 int tempflag = flag & BP_TEMPFLAG;
9945 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9946 ? bp_hardware_breakpoint
9947 : bp_breakpoint);
9948 struct breakpoint_ops *ops;
9949
9950 event_location_up location = string_to_event_location (&arg, current_language);
9951
9952 /* Matching breakpoints on probes. */
9953 if (location != NULL
9954 && event_location_type (location.get ()) == PROBE_LOCATION)
9955 ops = &bkpt_probe_breakpoint_ops;
9956 else
9957 ops = &bkpt_breakpoint_ops;
9958
9959 create_breakpoint (get_current_arch (),
9960 location.get (),
9961 NULL, 0, arg, 1 /* parse arg */,
9962 tempflag, type_wanted,
9963 0 /* Ignore count */,
9964 pending_break_support,
9965 ops,
9966 from_tty,
9967 1 /* enabled */,
9968 0 /* internal */,
9969 0);
9970 }
9971
9972 /* Helper function for break_command_1 and disassemble_command. */
9973
9974 void
9975 resolve_sal_pc (struct symtab_and_line *sal)
9976 {
9977 CORE_ADDR pc;
9978
9979 if (sal->pc == 0 && sal->symtab != NULL)
9980 {
9981 if (!find_line_pc (sal->symtab, sal->line, &pc))
9982 error (_("No line %d in file \"%s\"."),
9983 sal->line, symtab_to_filename_for_display (sal->symtab));
9984 sal->pc = pc;
9985
9986 /* If this SAL corresponds to a breakpoint inserted using a line
9987 number, then skip the function prologue if necessary. */
9988 if (sal->explicit_line)
9989 skip_prologue_sal (sal);
9990 }
9991
9992 if (sal->section == 0 && sal->symtab != NULL)
9993 {
9994 const struct blockvector *bv;
9995 const struct block *b;
9996 struct symbol *sym;
9997
9998 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9999 SYMTAB_COMPUNIT (sal->symtab));
10000 if (bv != NULL)
10001 {
10002 sym = block_linkage_function (b);
10003 if (sym != NULL)
10004 {
10005 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
10006 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
10007 sym);
10008 }
10009 else
10010 {
10011 /* It really is worthwhile to have the section, so we'll
10012 just have to look harder. This case can be executed
10013 if we have line numbers but no functions (as can
10014 happen in assembly source). */
10015
10016 struct bound_minimal_symbol msym;
10017 struct cleanup *old_chain = save_current_space_and_thread ();
10018
10019 switch_to_program_space_and_thread (sal->pspace);
10020
10021 msym = lookup_minimal_symbol_by_pc (sal->pc);
10022 if (msym.minsym)
10023 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
10024
10025 do_cleanups (old_chain);
10026 }
10027 }
10028 }
10029 }
10030
10031 void
10032 break_command (char *arg, int from_tty)
10033 {
10034 break_command_1 (arg, 0, from_tty);
10035 }
10036
10037 void
10038 tbreak_command (char *arg, int from_tty)
10039 {
10040 break_command_1 (arg, BP_TEMPFLAG, from_tty);
10041 }
10042
10043 static void
10044 hbreak_command (char *arg, int from_tty)
10045 {
10046 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
10047 }
10048
10049 static void
10050 thbreak_command (char *arg, int from_tty)
10051 {
10052 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
10053 }
10054
10055 static void
10056 stop_command (char *arg, int from_tty)
10057 {
10058 printf_filtered (_("Specify the type of breakpoint to set.\n\
10059 Usage: stop in <function | address>\n\
10060 stop at <line>\n"));
10061 }
10062
10063 static void
10064 stopin_command (char *arg, int from_tty)
10065 {
10066 int badInput = 0;
10067
10068 if (arg == (char *) NULL)
10069 badInput = 1;
10070 else if (*arg != '*')
10071 {
10072 char *argptr = arg;
10073 int hasColon = 0;
10074
10075 /* Look for a ':'. If this is a line number specification, then
10076 say it is bad, otherwise, it should be an address or
10077 function/method name. */
10078 while (*argptr && !hasColon)
10079 {
10080 hasColon = (*argptr == ':');
10081 argptr++;
10082 }
10083
10084 if (hasColon)
10085 badInput = (*argptr != ':'); /* Not a class::method */
10086 else
10087 badInput = isdigit (*arg); /* a simple line number */
10088 }
10089
10090 if (badInput)
10091 printf_filtered (_("Usage: stop in <function | address>\n"));
10092 else
10093 break_command_1 (arg, 0, from_tty);
10094 }
10095
10096 static void
10097 stopat_command (char *arg, int from_tty)
10098 {
10099 int badInput = 0;
10100
10101 if (arg == (char *) NULL || *arg == '*') /* no line number */
10102 badInput = 1;
10103 else
10104 {
10105 char *argptr = arg;
10106 int hasColon = 0;
10107
10108 /* Look for a ':'. If there is a '::' then get out, otherwise
10109 it is probably a line number. */
10110 while (*argptr && !hasColon)
10111 {
10112 hasColon = (*argptr == ':');
10113 argptr++;
10114 }
10115
10116 if (hasColon)
10117 badInput = (*argptr == ':'); /* we have class::method */
10118 else
10119 badInput = !isdigit (*arg); /* not a line number */
10120 }
10121
10122 if (badInput)
10123 printf_filtered (_("Usage: stop at <line>\n"));
10124 else
10125 break_command_1 (arg, 0, from_tty);
10126 }
10127
10128 /* The dynamic printf command is mostly like a regular breakpoint, but
10129 with a prewired command list consisting of a single output command,
10130 built from extra arguments supplied on the dprintf command
10131 line. */
10132
10133 static void
10134 dprintf_command (char *arg, int from_tty)
10135 {
10136 event_location_up location = string_to_event_location (&arg, current_language);
10137
10138 /* If non-NULL, ARG should have been advanced past the location;
10139 the next character must be ','. */
10140 if (arg != NULL)
10141 {
10142 if (arg[0] != ',' || arg[1] == '\0')
10143 error (_("Format string required"));
10144 else
10145 {
10146 /* Skip the comma. */
10147 ++arg;
10148 }
10149 }
10150
10151 create_breakpoint (get_current_arch (),
10152 location.get (),
10153 NULL, 0, arg, 1 /* parse arg */,
10154 0, bp_dprintf,
10155 0 /* Ignore count */,
10156 pending_break_support,
10157 &dprintf_breakpoint_ops,
10158 from_tty,
10159 1 /* enabled */,
10160 0 /* internal */,
10161 0);
10162 }
10163
10164 static void
10165 agent_printf_command (char *arg, int from_tty)
10166 {
10167 error (_("May only run agent-printf on the target"));
10168 }
10169
10170 /* Implement the "breakpoint_hit" breakpoint_ops method for
10171 ranged breakpoints. */
10172
10173 static int
10174 breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
10175 struct address_space *aspace,
10176 CORE_ADDR bp_addr,
10177 const struct target_waitstatus *ws)
10178 {
10179 if (ws->kind != TARGET_WAITKIND_STOPPED
10180 || ws->value.sig != GDB_SIGNAL_TRAP)
10181 return 0;
10182
10183 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
10184 bl->length, aspace, bp_addr);
10185 }
10186
10187 /* Implement the "resources_needed" breakpoint_ops method for
10188 ranged breakpoints. */
10189
10190 static int
10191 resources_needed_ranged_breakpoint (const struct bp_location *bl)
10192 {
10193 return target_ranged_break_num_registers ();
10194 }
10195
10196 /* Implement the "print_it" breakpoint_ops method for
10197 ranged breakpoints. */
10198
10199 static enum print_stop_action
10200 print_it_ranged_breakpoint (bpstat bs)
10201 {
10202 struct breakpoint *b = bs->breakpoint_at;
10203 struct bp_location *bl = b->loc;
10204 struct ui_out *uiout = current_uiout;
10205
10206 gdb_assert (b->type == bp_hardware_breakpoint);
10207
10208 /* Ranged breakpoints have only one location. */
10209 gdb_assert (bl && bl->next == NULL);
10210
10211 annotate_breakpoint (b->number);
10212
10213 maybe_print_thread_hit_breakpoint (uiout);
10214
10215 if (b->disposition == disp_del)
10216 uiout->text ("Temporary ranged breakpoint ");
10217 else
10218 uiout->text ("Ranged breakpoint ");
10219 if (uiout->is_mi_like_p ())
10220 {
10221 uiout->field_string ("reason",
10222 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
10223 uiout->field_string ("disp", bpdisp_text (b->disposition));
10224 }
10225 uiout->field_int ("bkptno", b->number);
10226 uiout->text (", ");
10227
10228 return PRINT_SRC_AND_LOC;
10229 }
10230
10231 /* Implement the "print_one" breakpoint_ops method for
10232 ranged breakpoints. */
10233
10234 static void
10235 print_one_ranged_breakpoint (struct breakpoint *b,
10236 struct bp_location **last_loc)
10237 {
10238 struct bp_location *bl = b->loc;
10239 struct value_print_options opts;
10240 struct ui_out *uiout = current_uiout;
10241
10242 /* Ranged breakpoints have only one location. */
10243 gdb_assert (bl && bl->next == NULL);
10244
10245 get_user_print_options (&opts);
10246
10247 if (opts.addressprint)
10248 /* We don't print the address range here, it will be printed later
10249 by print_one_detail_ranged_breakpoint. */
10250 uiout->field_skip ("addr");
10251 annotate_field (5);
10252 print_breakpoint_location (b, bl);
10253 *last_loc = bl;
10254 }
10255
10256 /* Implement the "print_one_detail" breakpoint_ops method for
10257 ranged breakpoints. */
10258
10259 static void
10260 print_one_detail_ranged_breakpoint (const struct breakpoint *b,
10261 struct ui_out *uiout)
10262 {
10263 CORE_ADDR address_start, address_end;
10264 struct bp_location *bl = b->loc;
10265 string_file stb;
10266
10267 gdb_assert (bl);
10268
10269 address_start = bl->address;
10270 address_end = address_start + bl->length - 1;
10271
10272 uiout->text ("\taddress range: ");
10273 stb.printf ("[%s, %s]",
10274 print_core_address (bl->gdbarch, address_start),
10275 print_core_address (bl->gdbarch, address_end));
10276 uiout->field_stream ("addr", stb);
10277 uiout->text ("\n");
10278 }
10279
10280 /* Implement the "print_mention" breakpoint_ops method for
10281 ranged breakpoints. */
10282
10283 static void
10284 print_mention_ranged_breakpoint (struct breakpoint *b)
10285 {
10286 struct bp_location *bl = b->loc;
10287 struct ui_out *uiout = current_uiout;
10288
10289 gdb_assert (bl);
10290 gdb_assert (b->type == bp_hardware_breakpoint);
10291
10292 if (uiout->is_mi_like_p ())
10293 return;
10294
10295 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
10296 b->number, paddress (bl->gdbarch, bl->address),
10297 paddress (bl->gdbarch, bl->address + bl->length - 1));
10298 }
10299
10300 /* Implement the "print_recreate" breakpoint_ops method for
10301 ranged breakpoints. */
10302
10303 static void
10304 print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
10305 {
10306 fprintf_unfiltered (fp, "break-range %s, %s",
10307 event_location_to_string (b->location.get ()),
10308 event_location_to_string (b->location_range_end.get ()));
10309 print_recreate_thread (b, fp);
10310 }
10311
10312 /* The breakpoint_ops structure to be used in ranged breakpoints. */
10313
10314 static struct breakpoint_ops ranged_breakpoint_ops;
10315
10316 /* Find the address where the end of the breakpoint range should be
10317 placed, given the SAL of the end of the range. This is so that if
10318 the user provides a line number, the end of the range is set to the
10319 last instruction of the given line. */
10320
10321 static CORE_ADDR
10322 find_breakpoint_range_end (struct symtab_and_line sal)
10323 {
10324 CORE_ADDR end;
10325
10326 /* If the user provided a PC value, use it. Otherwise,
10327 find the address of the end of the given location. */
10328 if (sal.explicit_pc)
10329 end = sal.pc;
10330 else
10331 {
10332 int ret;
10333 CORE_ADDR start;
10334
10335 ret = find_line_pc_range (sal, &start, &end);
10336 if (!ret)
10337 error (_("Could not find location of the end of the range."));
10338
10339 /* find_line_pc_range returns the start of the next line. */
10340 end--;
10341 }
10342
10343 return end;
10344 }
10345
10346 /* Implement the "break-range" CLI command. */
10347
10348 static void
10349 break_range_command (char *arg, int from_tty)
10350 {
10351 char *arg_start, *addr_string_start;
10352 struct linespec_result canonical_start, canonical_end;
10353 int bp_count, can_use_bp, length;
10354 CORE_ADDR end;
10355 struct breakpoint *b;
10356 struct symtab_and_line sal_start, sal_end;
10357 struct cleanup *cleanup_bkpt;
10358 struct linespec_sals *lsal_start, *lsal_end;
10359
10360 /* We don't support software ranged breakpoints. */
10361 if (target_ranged_break_num_registers () < 0)
10362 error (_("This target does not support hardware ranged breakpoints."));
10363
10364 bp_count = hw_breakpoint_used_count ();
10365 bp_count += target_ranged_break_num_registers ();
10366 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10367 bp_count, 0);
10368 if (can_use_bp < 0)
10369 error (_("Hardware breakpoints used exceeds limit."));
10370
10371 arg = skip_spaces (arg);
10372 if (arg == NULL || arg[0] == '\0')
10373 error(_("No address range specified."));
10374
10375 init_linespec_result (&canonical_start);
10376
10377 arg_start = arg;
10378 event_location_up start_location = string_to_event_location (&arg,
10379 current_language);
10380 parse_breakpoint_sals (start_location.get (), &canonical_start);
10381 cleanup_bkpt = make_cleanup_destroy_linespec_result (&canonical_start);
10382
10383 if (arg[0] != ',')
10384 error (_("Too few arguments."));
10385 else if (VEC_empty (linespec_sals, canonical_start.sals))
10386 error (_("Could not find location of the beginning of the range."));
10387
10388 lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
10389
10390 if (VEC_length (linespec_sals, canonical_start.sals) > 1
10391 || lsal_start->sals.nelts != 1)
10392 error (_("Cannot create a ranged breakpoint with multiple locations."));
10393
10394 sal_start = lsal_start->sals.sals[0];
10395 addr_string_start = savestring (arg_start, arg - arg_start);
10396 make_cleanup (xfree, addr_string_start);
10397
10398 arg++; /* Skip the comma. */
10399 arg = skip_spaces (arg);
10400
10401 /* Parse the end location. */
10402
10403 init_linespec_result (&canonical_end);
10404 arg_start = arg;
10405
10406 /* We call decode_line_full directly here instead of using
10407 parse_breakpoint_sals because we need to specify the start location's
10408 symtab and line as the default symtab and line for the end of the
10409 range. This makes it possible to have ranges like "foo.c:27, +14",
10410 where +14 means 14 lines from the start location. */
10411 event_location_up end_location = string_to_event_location (&arg,
10412 current_language);
10413 decode_line_full (end_location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
10414 sal_start.symtab, sal_start.line,
10415 &canonical_end, NULL, NULL);
10416
10417 make_cleanup_destroy_linespec_result (&canonical_end);
10418
10419 if (VEC_empty (linespec_sals, canonical_end.sals))
10420 error (_("Could not find location of the end of the range."));
10421
10422 lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
10423 if (VEC_length (linespec_sals, canonical_end.sals) > 1
10424 || lsal_end->sals.nelts != 1)
10425 error (_("Cannot create a ranged breakpoint with multiple locations."));
10426
10427 sal_end = lsal_end->sals.sals[0];
10428
10429 end = find_breakpoint_range_end (sal_end);
10430 if (sal_start.pc > end)
10431 error (_("Invalid address range, end precedes start."));
10432
10433 length = end - sal_start.pc + 1;
10434 if (length < 0)
10435 /* Length overflowed. */
10436 error (_("Address range too large."));
10437 else if (length == 1)
10438 {
10439 /* This range is simple enough to be handled by
10440 the `hbreak' command. */
10441 hbreak_command (addr_string_start, 1);
10442
10443 do_cleanups (cleanup_bkpt);
10444
10445 return;
10446 }
10447
10448 /* Now set up the breakpoint. */
10449 b = set_raw_breakpoint (get_current_arch (), sal_start,
10450 bp_hardware_breakpoint, &ranged_breakpoint_ops);
10451 set_breakpoint_count (breakpoint_count + 1);
10452 b->number = breakpoint_count;
10453 b->disposition = disp_donttouch;
10454 b->location = std::move (start_location);
10455 b->location_range_end = std::move (end_location);
10456 b->loc->length = length;
10457
10458 do_cleanups (cleanup_bkpt);
10459
10460 mention (b);
10461 observer_notify_breakpoint_created (b);
10462 update_global_location_list (UGLL_MAY_INSERT);
10463 }
10464
10465 /* Return non-zero if EXP is verified as constant. Returned zero
10466 means EXP is variable. Also the constant detection may fail for
10467 some constant expressions and in such case still falsely return
10468 zero. */
10469
10470 static int
10471 watchpoint_exp_is_const (const struct expression *exp)
10472 {
10473 int i = exp->nelts;
10474
10475 while (i > 0)
10476 {
10477 int oplenp, argsp;
10478
10479 /* We are only interested in the descriptor of each element. */
10480 operator_length (exp, i, &oplenp, &argsp);
10481 i -= oplenp;
10482
10483 switch (exp->elts[i].opcode)
10484 {
10485 case BINOP_ADD:
10486 case BINOP_SUB:
10487 case BINOP_MUL:
10488 case BINOP_DIV:
10489 case BINOP_REM:
10490 case BINOP_MOD:
10491 case BINOP_LSH:
10492 case BINOP_RSH:
10493 case BINOP_LOGICAL_AND:
10494 case BINOP_LOGICAL_OR:
10495 case BINOP_BITWISE_AND:
10496 case BINOP_BITWISE_IOR:
10497 case BINOP_BITWISE_XOR:
10498 case BINOP_EQUAL:
10499 case BINOP_NOTEQUAL:
10500 case BINOP_LESS:
10501 case BINOP_GTR:
10502 case BINOP_LEQ:
10503 case BINOP_GEQ:
10504 case BINOP_REPEAT:
10505 case BINOP_COMMA:
10506 case BINOP_EXP:
10507 case BINOP_MIN:
10508 case BINOP_MAX:
10509 case BINOP_INTDIV:
10510 case BINOP_CONCAT:
10511 case TERNOP_COND:
10512 case TERNOP_SLICE:
10513
10514 case OP_LONG:
10515 case OP_DOUBLE:
10516 case OP_DECFLOAT:
10517 case OP_LAST:
10518 case OP_COMPLEX:
10519 case OP_STRING:
10520 case OP_ARRAY:
10521 case OP_TYPE:
10522 case OP_TYPEOF:
10523 case OP_DECLTYPE:
10524 case OP_TYPEID:
10525 case OP_NAME:
10526 case OP_OBJC_NSSTRING:
10527
10528 case UNOP_NEG:
10529 case UNOP_LOGICAL_NOT:
10530 case UNOP_COMPLEMENT:
10531 case UNOP_ADDR:
10532 case UNOP_HIGH:
10533 case UNOP_CAST:
10534
10535 case UNOP_CAST_TYPE:
10536 case UNOP_REINTERPRET_CAST:
10537 case UNOP_DYNAMIC_CAST:
10538 /* Unary, binary and ternary operators: We have to check
10539 their operands. If they are constant, then so is the
10540 result of that operation. For instance, if A and B are
10541 determined to be constants, then so is "A + B".
10542
10543 UNOP_IND is one exception to the rule above, because the
10544 value of *ADDR is not necessarily a constant, even when
10545 ADDR is. */
10546 break;
10547
10548 case OP_VAR_VALUE:
10549 /* Check whether the associated symbol is a constant.
10550
10551 We use SYMBOL_CLASS rather than TYPE_CONST because it's
10552 possible that a buggy compiler could mark a variable as
10553 constant even when it is not, and TYPE_CONST would return
10554 true in this case, while SYMBOL_CLASS wouldn't.
10555
10556 We also have to check for function symbols because they
10557 are always constant. */
10558 {
10559 struct symbol *s = exp->elts[i + 2].symbol;
10560
10561 if (SYMBOL_CLASS (s) != LOC_BLOCK
10562 && SYMBOL_CLASS (s) != LOC_CONST
10563 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10564 return 0;
10565 break;
10566 }
10567
10568 /* The default action is to return 0 because we are using
10569 the optimistic approach here: If we don't know something,
10570 then it is not a constant. */
10571 default:
10572 return 0;
10573 }
10574 }
10575
10576 return 1;
10577 }
10578
10579 /* Implement the "dtor" breakpoint_ops method for watchpoints. */
10580
10581 static void
10582 dtor_watchpoint (struct breakpoint *self)
10583 {
10584 struct watchpoint *w = (struct watchpoint *) self;
10585
10586 xfree (w->exp_string);
10587 xfree (w->exp_string_reparse);
10588 value_free (w->val);
10589
10590 base_breakpoint_ops.dtor (self);
10591 }
10592
10593 /* Implement the "re_set" breakpoint_ops method for watchpoints. */
10594
10595 static void
10596 re_set_watchpoint (struct breakpoint *b)
10597 {
10598 struct watchpoint *w = (struct watchpoint *) b;
10599
10600 /* Watchpoint can be either on expression using entirely global
10601 variables, or it can be on local variables.
10602
10603 Watchpoints of the first kind are never auto-deleted, and even
10604 persist across program restarts. Since they can use variables
10605 from shared libraries, we need to reparse expression as libraries
10606 are loaded and unloaded.
10607
10608 Watchpoints on local variables can also change meaning as result
10609 of solib event. For example, if a watchpoint uses both a local
10610 and a global variables in expression, it's a local watchpoint,
10611 but unloading of a shared library will make the expression
10612 invalid. This is not a very common use case, but we still
10613 re-evaluate expression, to avoid surprises to the user.
10614
10615 Note that for local watchpoints, we re-evaluate it only if
10616 watchpoints frame id is still valid. If it's not, it means the
10617 watchpoint is out of scope and will be deleted soon. In fact,
10618 I'm not sure we'll ever be called in this case.
10619
10620 If a local watchpoint's frame id is still valid, then
10621 w->exp_valid_block is likewise valid, and we can safely use it.
10622
10623 Don't do anything about disabled watchpoints, since they will be
10624 reevaluated again when enabled. */
10625 update_watchpoint (w, 1 /* reparse */);
10626 }
10627
10628 /* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10629
10630 static int
10631 insert_watchpoint (struct bp_location *bl)
10632 {
10633 struct watchpoint *w = (struct watchpoint *) bl->owner;
10634 int length = w->exact ? 1 : bl->length;
10635
10636 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
10637 w->cond_exp.get ());
10638 }
10639
10640 /* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10641
10642 static int
10643 remove_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10644 {
10645 struct watchpoint *w = (struct watchpoint *) bl->owner;
10646 int length = w->exact ? 1 : bl->length;
10647
10648 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
10649 w->cond_exp.get ());
10650 }
10651
10652 static int
10653 breakpoint_hit_watchpoint (const struct bp_location *bl,
10654 struct address_space *aspace, CORE_ADDR bp_addr,
10655 const struct target_waitstatus *ws)
10656 {
10657 struct breakpoint *b = bl->owner;
10658 struct watchpoint *w = (struct watchpoint *) b;
10659
10660 /* Continuable hardware watchpoints are treated as non-existent if the
10661 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10662 some data address). Otherwise gdb won't stop on a break instruction
10663 in the code (not from a breakpoint) when a hardware watchpoint has
10664 been defined. Also skip watchpoints which we know did not trigger
10665 (did not match the data address). */
10666 if (is_hardware_watchpoint (b)
10667 && w->watchpoint_triggered == watch_triggered_no)
10668 return 0;
10669
10670 return 1;
10671 }
10672
10673 static void
10674 check_status_watchpoint (bpstat bs)
10675 {
10676 gdb_assert (is_watchpoint (bs->breakpoint_at));
10677
10678 bpstat_check_watchpoint (bs);
10679 }
10680
10681 /* Implement the "resources_needed" breakpoint_ops method for
10682 hardware watchpoints. */
10683
10684 static int
10685 resources_needed_watchpoint (const struct bp_location *bl)
10686 {
10687 struct watchpoint *w = (struct watchpoint *) bl->owner;
10688 int length = w->exact? 1 : bl->length;
10689
10690 return target_region_ok_for_hw_watchpoint (bl->address, length);
10691 }
10692
10693 /* Implement the "works_in_software_mode" breakpoint_ops method for
10694 hardware watchpoints. */
10695
10696 static int
10697 works_in_software_mode_watchpoint (const struct breakpoint *b)
10698 {
10699 /* Read and access watchpoints only work with hardware support. */
10700 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
10701 }
10702
10703 static enum print_stop_action
10704 print_it_watchpoint (bpstat bs)
10705 {
10706 struct cleanup *old_chain;
10707 struct breakpoint *b;
10708 enum print_stop_action result;
10709 struct watchpoint *w;
10710 struct ui_out *uiout = current_uiout;
10711
10712 gdb_assert (bs->bp_location_at != NULL);
10713
10714 b = bs->breakpoint_at;
10715 w = (struct watchpoint *) b;
10716
10717 old_chain = make_cleanup (null_cleanup, NULL);
10718
10719 annotate_watchpoint (b->number);
10720 maybe_print_thread_hit_breakpoint (uiout);
10721
10722 string_file stb;
10723
10724 switch (b->type)
10725 {
10726 case bp_watchpoint:
10727 case bp_hardware_watchpoint:
10728 if (uiout->is_mi_like_p ())
10729 uiout->field_string
10730 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10731 mention (b);
10732 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10733 uiout->text ("\nOld value = ");
10734 watchpoint_value_print (bs->old_val, &stb);
10735 uiout->field_stream ("old", stb);
10736 uiout->text ("\nNew value = ");
10737 watchpoint_value_print (w->val, &stb);
10738 uiout->field_stream ("new", stb);
10739 uiout->text ("\n");
10740 /* More than one watchpoint may have been triggered. */
10741 result = PRINT_UNKNOWN;
10742 break;
10743
10744 case bp_read_watchpoint:
10745 if (uiout->is_mi_like_p ())
10746 uiout->field_string
10747 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10748 mention (b);
10749 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10750 uiout->text ("\nValue = ");
10751 watchpoint_value_print (w->val, &stb);
10752 uiout->field_stream ("value", stb);
10753 uiout->text ("\n");
10754 result = PRINT_UNKNOWN;
10755 break;
10756
10757 case bp_access_watchpoint:
10758 if (bs->old_val != NULL)
10759 {
10760 if (uiout->is_mi_like_p ())
10761 uiout->field_string
10762 ("reason",
10763 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10764 mention (b);
10765 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10766 uiout->text ("\nOld value = ");
10767 watchpoint_value_print (bs->old_val, &stb);
10768 uiout->field_stream ("old", stb);
10769 uiout->text ("\nNew value = ");
10770 }
10771 else
10772 {
10773 mention (b);
10774 if (uiout->is_mi_like_p ())
10775 uiout->field_string
10776 ("reason",
10777 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10778 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10779 uiout->text ("\nValue = ");
10780 }
10781 watchpoint_value_print (w->val, &stb);
10782 uiout->field_stream ("new", stb);
10783 uiout->text ("\n");
10784 result = PRINT_UNKNOWN;
10785 break;
10786 default:
10787 result = PRINT_UNKNOWN;
10788 }
10789
10790 do_cleanups (old_chain);
10791 return result;
10792 }
10793
10794 /* Implement the "print_mention" breakpoint_ops method for hardware
10795 watchpoints. */
10796
10797 static void
10798 print_mention_watchpoint (struct breakpoint *b)
10799 {
10800 struct cleanup *ui_out_chain;
10801 struct watchpoint *w = (struct watchpoint *) b;
10802 struct ui_out *uiout = current_uiout;
10803
10804 switch (b->type)
10805 {
10806 case bp_watchpoint:
10807 uiout->text ("Watchpoint ");
10808 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10809 break;
10810 case bp_hardware_watchpoint:
10811 uiout->text ("Hardware watchpoint ");
10812 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10813 break;
10814 case bp_read_watchpoint:
10815 uiout->text ("Hardware read watchpoint ");
10816 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
10817 break;
10818 case bp_access_watchpoint:
10819 uiout->text ("Hardware access (read/write) watchpoint ");
10820 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
10821 break;
10822 default:
10823 internal_error (__FILE__, __LINE__,
10824 _("Invalid hardware watchpoint type."));
10825 }
10826
10827 uiout->field_int ("number", b->number);
10828 uiout->text (": ");
10829 uiout->field_string ("exp", w->exp_string);
10830 do_cleanups (ui_out_chain);
10831 }
10832
10833 /* Implement the "print_recreate" breakpoint_ops method for
10834 watchpoints. */
10835
10836 static void
10837 print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10838 {
10839 struct watchpoint *w = (struct watchpoint *) b;
10840
10841 switch (b->type)
10842 {
10843 case bp_watchpoint:
10844 case bp_hardware_watchpoint:
10845 fprintf_unfiltered (fp, "watch");
10846 break;
10847 case bp_read_watchpoint:
10848 fprintf_unfiltered (fp, "rwatch");
10849 break;
10850 case bp_access_watchpoint:
10851 fprintf_unfiltered (fp, "awatch");
10852 break;
10853 default:
10854 internal_error (__FILE__, __LINE__,
10855 _("Invalid watchpoint type."));
10856 }
10857
10858 fprintf_unfiltered (fp, " %s", w->exp_string);
10859 print_recreate_thread (b, fp);
10860 }
10861
10862 /* Implement the "explains_signal" breakpoint_ops method for
10863 watchpoints. */
10864
10865 static int
10866 explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10867 {
10868 /* A software watchpoint cannot cause a signal other than
10869 GDB_SIGNAL_TRAP. */
10870 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
10871 return 0;
10872
10873 return 1;
10874 }
10875
10876 /* The breakpoint_ops structure to be used in hardware watchpoints. */
10877
10878 static struct breakpoint_ops watchpoint_breakpoint_ops;
10879
10880 /* Implement the "insert" breakpoint_ops method for
10881 masked hardware watchpoints. */
10882
10883 static int
10884 insert_masked_watchpoint (struct bp_location *bl)
10885 {
10886 struct watchpoint *w = (struct watchpoint *) bl->owner;
10887
10888 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
10889 bl->watchpoint_type);
10890 }
10891
10892 /* Implement the "remove" breakpoint_ops method for
10893 masked hardware watchpoints. */
10894
10895 static int
10896 remove_masked_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10897 {
10898 struct watchpoint *w = (struct watchpoint *) bl->owner;
10899
10900 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
10901 bl->watchpoint_type);
10902 }
10903
10904 /* Implement the "resources_needed" breakpoint_ops method for
10905 masked hardware watchpoints. */
10906
10907 static int
10908 resources_needed_masked_watchpoint (const struct bp_location *bl)
10909 {
10910 struct watchpoint *w = (struct watchpoint *) bl->owner;
10911
10912 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
10913 }
10914
10915 /* Implement the "works_in_software_mode" breakpoint_ops method for
10916 masked hardware watchpoints. */
10917
10918 static int
10919 works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10920 {
10921 return 0;
10922 }
10923
10924 /* Implement the "print_it" breakpoint_ops method for
10925 masked hardware watchpoints. */
10926
10927 static enum print_stop_action
10928 print_it_masked_watchpoint (bpstat bs)
10929 {
10930 struct breakpoint *b = bs->breakpoint_at;
10931 struct ui_out *uiout = current_uiout;
10932
10933 /* Masked watchpoints have only one location. */
10934 gdb_assert (b->loc && b->loc->next == NULL);
10935
10936 annotate_watchpoint (b->number);
10937 maybe_print_thread_hit_breakpoint (uiout);
10938
10939 switch (b->type)
10940 {
10941 case bp_hardware_watchpoint:
10942 if (uiout->is_mi_like_p ())
10943 uiout->field_string
10944 ("reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10945 break;
10946
10947 case bp_read_watchpoint:
10948 if (uiout->is_mi_like_p ())
10949 uiout->field_string
10950 ("reason", async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10951 break;
10952
10953 case bp_access_watchpoint:
10954 if (uiout->is_mi_like_p ())
10955 uiout->field_string
10956 ("reason",
10957 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10958 break;
10959 default:
10960 internal_error (__FILE__, __LINE__,
10961 _("Invalid hardware watchpoint type."));
10962 }
10963
10964 mention (b);
10965 uiout->text (_("\n\
10966 Check the underlying instruction at PC for the memory\n\
10967 address and value which triggered this watchpoint.\n"));
10968 uiout->text ("\n");
10969
10970 /* More than one watchpoint may have been triggered. */
10971 return PRINT_UNKNOWN;
10972 }
10973
10974 /* Implement the "print_one_detail" breakpoint_ops method for
10975 masked hardware watchpoints. */
10976
10977 static void
10978 print_one_detail_masked_watchpoint (const struct breakpoint *b,
10979 struct ui_out *uiout)
10980 {
10981 struct watchpoint *w = (struct watchpoint *) b;
10982
10983 /* Masked watchpoints have only one location. */
10984 gdb_assert (b->loc && b->loc->next == NULL);
10985
10986 uiout->text ("\tmask ");
10987 uiout->field_core_addr ("mask", b->loc->gdbarch, w->hw_wp_mask);
10988 uiout->text ("\n");
10989 }
10990
10991 /* Implement the "print_mention" breakpoint_ops method for
10992 masked hardware watchpoints. */
10993
10994 static void
10995 print_mention_masked_watchpoint (struct breakpoint *b)
10996 {
10997 struct watchpoint *w = (struct watchpoint *) b;
10998 struct ui_out *uiout = current_uiout;
10999 struct cleanup *ui_out_chain;
11000
11001 switch (b->type)
11002 {
11003 case bp_hardware_watchpoint:
11004 uiout->text ("Masked hardware watchpoint ");
11005 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
11006 break;
11007 case bp_read_watchpoint:
11008 uiout->text ("Masked hardware read watchpoint ");
11009 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
11010 break;
11011 case bp_access_watchpoint:
11012 uiout->text ("Masked hardware access (read/write) watchpoint ");
11013 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
11014 break;
11015 default:
11016 internal_error (__FILE__, __LINE__,
11017 _("Invalid hardware watchpoint type."));
11018 }
11019
11020 uiout->field_int ("number", b->number);
11021 uiout->text (": ");
11022 uiout->field_string ("exp", w->exp_string);
11023 do_cleanups (ui_out_chain);
11024 }
11025
11026 /* Implement the "print_recreate" breakpoint_ops method for
11027 masked hardware watchpoints. */
11028
11029 static void
11030 print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
11031 {
11032 struct watchpoint *w = (struct watchpoint *) b;
11033 char tmp[40];
11034
11035 switch (b->type)
11036 {
11037 case bp_hardware_watchpoint:
11038 fprintf_unfiltered (fp, "watch");
11039 break;
11040 case bp_read_watchpoint:
11041 fprintf_unfiltered (fp, "rwatch");
11042 break;
11043 case bp_access_watchpoint:
11044 fprintf_unfiltered (fp, "awatch");
11045 break;
11046 default:
11047 internal_error (__FILE__, __LINE__,
11048 _("Invalid hardware watchpoint type."));
11049 }
11050
11051 sprintf_vma (tmp, w->hw_wp_mask);
11052 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
11053 print_recreate_thread (b, fp);
11054 }
11055
11056 /* The breakpoint_ops structure to be used in masked hardware watchpoints. */
11057
11058 static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
11059
11060 /* Tell whether the given watchpoint is a masked hardware watchpoint. */
11061
11062 static int
11063 is_masked_watchpoint (const struct breakpoint *b)
11064 {
11065 return b->ops == &masked_watchpoint_breakpoint_ops;
11066 }
11067
11068 /* accessflag: hw_write: watch write,
11069 hw_read: watch read,
11070 hw_access: watch access (read or write) */
11071 static void
11072 watch_command_1 (const char *arg, int accessflag, int from_tty,
11073 int just_location, int internal)
11074 {
11075 struct breakpoint *b, *scope_breakpoint = NULL;
11076 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
11077 struct value *val, *mark, *result;
11078 int saved_bitpos = 0, saved_bitsize = 0;
11079 struct frame_info *frame;
11080 const char *exp_start = NULL;
11081 const char *exp_end = NULL;
11082 const char *tok, *end_tok;
11083 int toklen = -1;
11084 const char *cond_start = NULL;
11085 const char *cond_end = NULL;
11086 enum bptype bp_type;
11087 int thread = -1;
11088 int pc = 0;
11089 /* Flag to indicate whether we are going to use masks for
11090 the hardware watchpoint. */
11091 int use_mask = 0;
11092 CORE_ADDR mask = 0;
11093 struct watchpoint *w;
11094 char *expression;
11095 struct cleanup *back_to;
11096
11097 /* Make sure that we actually have parameters to parse. */
11098 if (arg != NULL && arg[0] != '\0')
11099 {
11100 const char *value_start;
11101
11102 exp_end = arg + strlen (arg);
11103
11104 /* Look for "parameter value" pairs at the end
11105 of the arguments string. */
11106 for (tok = exp_end - 1; tok > arg; tok--)
11107 {
11108 /* Skip whitespace at the end of the argument list. */
11109 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11110 tok--;
11111
11112 /* Find the beginning of the last token.
11113 This is the value of the parameter. */
11114 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11115 tok--;
11116 value_start = tok + 1;
11117
11118 /* Skip whitespace. */
11119 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11120 tok--;
11121
11122 end_tok = tok;
11123
11124 /* Find the beginning of the second to last token.
11125 This is the parameter itself. */
11126 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11127 tok--;
11128 tok++;
11129 toklen = end_tok - tok + 1;
11130
11131 if (toklen == 6 && startswith (tok, "thread"))
11132 {
11133 struct thread_info *thr;
11134 /* At this point we've found a "thread" token, which means
11135 the user is trying to set a watchpoint that triggers
11136 only in a specific thread. */
11137 const char *endp;
11138
11139 if (thread != -1)
11140 error(_("You can specify only one thread."));
11141
11142 /* Extract the thread ID from the next token. */
11143 thr = parse_thread_id (value_start, &endp);
11144
11145 /* Check if the user provided a valid thread ID. */
11146 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
11147 invalid_thread_id_error (value_start);
11148
11149 thread = thr->global_num;
11150 }
11151 else if (toklen == 4 && startswith (tok, "mask"))
11152 {
11153 /* We've found a "mask" token, which means the user wants to
11154 create a hardware watchpoint that is going to have the mask
11155 facility. */
11156 struct value *mask_value, *mark;
11157
11158 if (use_mask)
11159 error(_("You can specify only one mask."));
11160
11161 use_mask = just_location = 1;
11162
11163 mark = value_mark ();
11164 mask_value = parse_to_comma_and_eval (&value_start);
11165 mask = value_as_address (mask_value);
11166 value_free_to_mark (mark);
11167 }
11168 else
11169 /* We didn't recognize what we found. We should stop here. */
11170 break;
11171
11172 /* Truncate the string and get rid of the "parameter value" pair before
11173 the arguments string is parsed by the parse_exp_1 function. */
11174 exp_end = tok;
11175 }
11176 }
11177 else
11178 exp_end = arg;
11179
11180 /* Parse the rest of the arguments. From here on out, everything
11181 is in terms of a newly allocated string instead of the original
11182 ARG. */
11183 innermost_block = NULL;
11184 expression = savestring (arg, exp_end - arg);
11185 back_to = make_cleanup (xfree, expression);
11186 exp_start = arg = expression;
11187 expression_up exp = parse_exp_1 (&arg, 0, 0, 0);
11188 exp_end = arg;
11189 /* Remove trailing whitespace from the expression before saving it.
11190 This makes the eventual display of the expression string a bit
11191 prettier. */
11192 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
11193 --exp_end;
11194
11195 /* Checking if the expression is not constant. */
11196 if (watchpoint_exp_is_const (exp.get ()))
11197 {
11198 int len;
11199
11200 len = exp_end - exp_start;
11201 while (len > 0 && isspace (exp_start[len - 1]))
11202 len--;
11203 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
11204 }
11205
11206 exp_valid_block = innermost_block;
11207 mark = value_mark ();
11208 fetch_subexp_value (exp.get (), &pc, &val, &result, NULL, just_location);
11209
11210 if (val != NULL && just_location)
11211 {
11212 saved_bitpos = value_bitpos (val);
11213 saved_bitsize = value_bitsize (val);
11214 }
11215
11216 if (just_location)
11217 {
11218 int ret;
11219
11220 exp_valid_block = NULL;
11221 val = value_addr (result);
11222 release_value (val);
11223 value_free_to_mark (mark);
11224
11225 if (use_mask)
11226 {
11227 ret = target_masked_watch_num_registers (value_as_address (val),
11228 mask);
11229 if (ret == -1)
11230 error (_("This target does not support masked watchpoints."));
11231 else if (ret == -2)
11232 error (_("Invalid mask or memory region."));
11233 }
11234 }
11235 else if (val != NULL)
11236 release_value (val);
11237
11238 tok = skip_spaces_const (arg);
11239 end_tok = skip_to_space_const (tok);
11240
11241 toklen = end_tok - tok;
11242 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
11243 {
11244 innermost_block = NULL;
11245 tok = cond_start = end_tok + 1;
11246 parse_exp_1 (&tok, 0, 0, 0);
11247
11248 /* The watchpoint expression may not be local, but the condition
11249 may still be. E.g.: `watch global if local > 0'. */
11250 cond_exp_valid_block = innermost_block;
11251
11252 cond_end = tok;
11253 }
11254 if (*tok)
11255 error (_("Junk at end of command."));
11256
11257 frame = block_innermost_frame (exp_valid_block);
11258
11259 /* If the expression is "local", then set up a "watchpoint scope"
11260 breakpoint at the point where we've left the scope of the watchpoint
11261 expression. Create the scope breakpoint before the watchpoint, so
11262 that we will encounter it first in bpstat_stop_status. */
11263 if (exp_valid_block && frame)
11264 {
11265 if (frame_id_p (frame_unwind_caller_id (frame)))
11266 {
11267 scope_breakpoint
11268 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
11269 frame_unwind_caller_pc (frame),
11270 bp_watchpoint_scope,
11271 &momentary_breakpoint_ops);
11272
11273 scope_breakpoint->enable_state = bp_enabled;
11274
11275 /* Automatically delete the breakpoint when it hits. */
11276 scope_breakpoint->disposition = disp_del;
11277
11278 /* Only break in the proper frame (help with recursion). */
11279 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
11280
11281 /* Set the address at which we will stop. */
11282 scope_breakpoint->loc->gdbarch
11283 = frame_unwind_caller_arch (frame);
11284 scope_breakpoint->loc->requested_address
11285 = frame_unwind_caller_pc (frame);
11286 scope_breakpoint->loc->address
11287 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
11288 scope_breakpoint->loc->requested_address,
11289 scope_breakpoint->type);
11290 }
11291 }
11292
11293 /* Now set up the breakpoint. We create all watchpoints as hardware
11294 watchpoints here even if hardware watchpoints are turned off, a call
11295 to update_watchpoint later in this function will cause the type to
11296 drop back to bp_watchpoint (software watchpoint) if required. */
11297
11298 if (accessflag == hw_read)
11299 bp_type = bp_read_watchpoint;
11300 else if (accessflag == hw_access)
11301 bp_type = bp_access_watchpoint;
11302 else
11303 bp_type = bp_hardware_watchpoint;
11304
11305 w = new watchpoint ();
11306 b = &w->base;
11307 if (use_mask)
11308 init_raw_breakpoint_without_location (b, NULL, bp_type,
11309 &masked_watchpoint_breakpoint_ops);
11310 else
11311 init_raw_breakpoint_without_location (b, NULL, bp_type,
11312 &watchpoint_breakpoint_ops);
11313 b->thread = thread;
11314 b->disposition = disp_donttouch;
11315 b->pspace = current_program_space;
11316 w->exp = std::move (exp);
11317 w->exp_valid_block = exp_valid_block;
11318 w->cond_exp_valid_block = cond_exp_valid_block;
11319 if (just_location)
11320 {
11321 struct type *t = value_type (val);
11322 CORE_ADDR addr = value_as_address (val);
11323
11324 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
11325
11326 std::string name = type_to_string (t);
11327
11328 w->exp_string_reparse = xstrprintf ("* (%s *) %s", name.c_str (),
11329 core_addr_to_string (addr));
11330
11331 w->exp_string = xstrprintf ("-location %.*s",
11332 (int) (exp_end - exp_start), exp_start);
11333
11334 /* The above expression is in C. */
11335 b->language = language_c;
11336 }
11337 else
11338 w->exp_string = savestring (exp_start, exp_end - exp_start);
11339
11340 if (use_mask)
11341 {
11342 w->hw_wp_mask = mask;
11343 }
11344 else
11345 {
11346 w->val = val;
11347 w->val_bitpos = saved_bitpos;
11348 w->val_bitsize = saved_bitsize;
11349 w->val_valid = 1;
11350 }
11351
11352 if (cond_start)
11353 b->cond_string = savestring (cond_start, cond_end - cond_start);
11354 else
11355 b->cond_string = 0;
11356
11357 if (frame)
11358 {
11359 w->watchpoint_frame = get_frame_id (frame);
11360 w->watchpoint_thread = inferior_ptid;
11361 }
11362 else
11363 {
11364 w->watchpoint_frame = null_frame_id;
11365 w->watchpoint_thread = null_ptid;
11366 }
11367
11368 if (scope_breakpoint != NULL)
11369 {
11370 /* The scope breakpoint is related to the watchpoint. We will
11371 need to act on them together. */
11372 b->related_breakpoint = scope_breakpoint;
11373 scope_breakpoint->related_breakpoint = b;
11374 }
11375
11376 if (!just_location)
11377 value_free_to_mark (mark);
11378
11379 TRY
11380 {
11381 /* Finally update the new watchpoint. This creates the locations
11382 that should be inserted. */
11383 update_watchpoint (w, 1);
11384 }
11385 CATCH (e, RETURN_MASK_ALL)
11386 {
11387 delete_breakpoint (b);
11388 throw_exception (e);
11389 }
11390 END_CATCH
11391
11392 install_breakpoint (internal, b, 1);
11393 do_cleanups (back_to);
11394 }
11395
11396 /* Return count of debug registers needed to watch the given expression.
11397 If the watchpoint cannot be handled in hardware return zero. */
11398
11399 static int
11400 can_use_hardware_watchpoint (struct value *v)
11401 {
11402 int found_memory_cnt = 0;
11403 struct value *head = v;
11404
11405 /* Did the user specifically forbid us to use hardware watchpoints? */
11406 if (!can_use_hw_watchpoints)
11407 return 0;
11408
11409 /* Make sure that the value of the expression depends only upon
11410 memory contents, and values computed from them within GDB. If we
11411 find any register references or function calls, we can't use a
11412 hardware watchpoint.
11413
11414 The idea here is that evaluating an expression generates a series
11415 of values, one holding the value of every subexpression. (The
11416 expression a*b+c has five subexpressions: a, b, a*b, c, and
11417 a*b+c.) GDB's values hold almost enough information to establish
11418 the criteria given above --- they identify memory lvalues,
11419 register lvalues, computed values, etcetera. So we can evaluate
11420 the expression, and then scan the chain of values that leaves
11421 behind to decide whether we can detect any possible change to the
11422 expression's final value using only hardware watchpoints.
11423
11424 However, I don't think that the values returned by inferior
11425 function calls are special in any way. So this function may not
11426 notice that an expression involving an inferior function call
11427 can't be watched with hardware watchpoints. FIXME. */
11428 for (; v; v = value_next (v))
11429 {
11430 if (VALUE_LVAL (v) == lval_memory)
11431 {
11432 if (v != head && value_lazy (v))
11433 /* A lazy memory lvalue in the chain is one that GDB never
11434 needed to fetch; we either just used its address (e.g.,
11435 `a' in `a.b') or we never needed it at all (e.g., `a'
11436 in `a,b'). This doesn't apply to HEAD; if that is
11437 lazy then it was not readable, but watch it anyway. */
11438 ;
11439 else
11440 {
11441 /* Ahh, memory we actually used! Check if we can cover
11442 it with hardware watchpoints. */
11443 struct type *vtype = check_typedef (value_type (v));
11444
11445 /* We only watch structs and arrays if user asked for it
11446 explicitly, never if they just happen to appear in a
11447 middle of some value chain. */
11448 if (v == head
11449 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11450 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11451 {
11452 CORE_ADDR vaddr = value_address (v);
11453 int len;
11454 int num_regs;
11455
11456 len = (target_exact_watchpoints
11457 && is_scalar_type_recursive (vtype))?
11458 1 : TYPE_LENGTH (value_type (v));
11459
11460 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11461 if (!num_regs)
11462 return 0;
11463 else
11464 found_memory_cnt += num_regs;
11465 }
11466 }
11467 }
11468 else if (VALUE_LVAL (v) != not_lval
11469 && deprecated_value_modifiable (v) == 0)
11470 return 0; /* These are values from the history (e.g., $1). */
11471 else if (VALUE_LVAL (v) == lval_register)
11472 return 0; /* Cannot watch a register with a HW watchpoint. */
11473 }
11474
11475 /* The expression itself looks suitable for using a hardware
11476 watchpoint, but give the target machine a chance to reject it. */
11477 return found_memory_cnt;
11478 }
11479
11480 void
11481 watch_command_wrapper (char *arg, int from_tty, int internal)
11482 {
11483 watch_command_1 (arg, hw_write, from_tty, 0, internal);
11484 }
11485
11486 /* A helper function that looks for the "-location" argument and then
11487 calls watch_command_1. */
11488
11489 static void
11490 watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11491 {
11492 int just_location = 0;
11493
11494 if (arg
11495 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11496 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11497 {
11498 arg = skip_spaces (arg);
11499 just_location = 1;
11500 }
11501
11502 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
11503 }
11504
11505 static void
11506 watch_command (char *arg, int from_tty)
11507 {
11508 watch_maybe_just_location (arg, hw_write, from_tty);
11509 }
11510
11511 void
11512 rwatch_command_wrapper (char *arg, int from_tty, int internal)
11513 {
11514 watch_command_1 (arg, hw_read, from_tty, 0, internal);
11515 }
11516
11517 static void
11518 rwatch_command (char *arg, int from_tty)
11519 {
11520 watch_maybe_just_location (arg, hw_read, from_tty);
11521 }
11522
11523 void
11524 awatch_command_wrapper (char *arg, int from_tty, int internal)
11525 {
11526 watch_command_1 (arg, hw_access, from_tty, 0, internal);
11527 }
11528
11529 static void
11530 awatch_command (char *arg, int from_tty)
11531 {
11532 watch_maybe_just_location (arg, hw_access, from_tty);
11533 }
11534 \f
11535
11536 /* Data for the FSM that manages the until(location)/advance commands
11537 in infcmd.c. Here because it uses the mechanisms of
11538 breakpoints. */
11539
11540 struct until_break_fsm
11541 {
11542 /* The base class. */
11543 struct thread_fsm thread_fsm;
11544
11545 /* The thread that as current when the command was executed. */
11546 int thread;
11547
11548 /* The breakpoint set at the destination location. */
11549 struct breakpoint *location_breakpoint;
11550
11551 /* Breakpoint set at the return address in the caller frame. May be
11552 NULL. */
11553 struct breakpoint *caller_breakpoint;
11554 };
11555
11556 static void until_break_fsm_clean_up (struct thread_fsm *self,
11557 struct thread_info *thread);
11558 static int until_break_fsm_should_stop (struct thread_fsm *self,
11559 struct thread_info *thread);
11560 static enum async_reply_reason
11561 until_break_fsm_async_reply_reason (struct thread_fsm *self);
11562
11563 /* until_break_fsm's vtable. */
11564
11565 static struct thread_fsm_ops until_break_fsm_ops =
11566 {
11567 NULL, /* dtor */
11568 until_break_fsm_clean_up,
11569 until_break_fsm_should_stop,
11570 NULL, /* return_value */
11571 until_break_fsm_async_reply_reason,
11572 };
11573
11574 /* Allocate a new until_break_command_fsm. */
11575
11576 static struct until_break_fsm *
11577 new_until_break_fsm (struct interp *cmd_interp, int thread,
11578 struct breakpoint *location_breakpoint,
11579 struct breakpoint *caller_breakpoint)
11580 {
11581 struct until_break_fsm *sm;
11582
11583 sm = XCNEW (struct until_break_fsm);
11584 thread_fsm_ctor (&sm->thread_fsm, &until_break_fsm_ops, cmd_interp);
11585
11586 sm->thread = thread;
11587 sm->location_breakpoint = location_breakpoint;
11588 sm->caller_breakpoint = caller_breakpoint;
11589
11590 return sm;
11591 }
11592
11593 /* Implementation of the 'should_stop' FSM method for the
11594 until(location)/advance commands. */
11595
11596 static int
11597 until_break_fsm_should_stop (struct thread_fsm *self,
11598 struct thread_info *tp)
11599 {
11600 struct until_break_fsm *sm = (struct until_break_fsm *) self;
11601
11602 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
11603 sm->location_breakpoint) != NULL
11604 || (sm->caller_breakpoint != NULL
11605 && bpstat_find_breakpoint (tp->control.stop_bpstat,
11606 sm->caller_breakpoint) != NULL))
11607 thread_fsm_set_finished (self);
11608
11609 return 1;
11610 }
11611
11612 /* Implementation of the 'clean_up' FSM method for the
11613 until(location)/advance commands. */
11614
11615 static void
11616 until_break_fsm_clean_up (struct thread_fsm *self,
11617 struct thread_info *thread)
11618 {
11619 struct until_break_fsm *sm = (struct until_break_fsm *) self;
11620
11621 /* Clean up our temporary breakpoints. */
11622 if (sm->location_breakpoint != NULL)
11623 {
11624 delete_breakpoint (sm->location_breakpoint);
11625 sm->location_breakpoint = NULL;
11626 }
11627 if (sm->caller_breakpoint != NULL)
11628 {
11629 delete_breakpoint (sm->caller_breakpoint);
11630 sm->caller_breakpoint = NULL;
11631 }
11632 delete_longjmp_breakpoint (sm->thread);
11633 }
11634
11635 /* Implementation of the 'async_reply_reason' FSM method for the
11636 until(location)/advance commands. */
11637
11638 static enum async_reply_reason
11639 until_break_fsm_async_reply_reason (struct thread_fsm *self)
11640 {
11641 return EXEC_ASYNC_LOCATION_REACHED;
11642 }
11643
11644 void
11645 until_break_command (char *arg, int from_tty, int anywhere)
11646 {
11647 struct symtabs_and_lines sals;
11648 struct symtab_and_line sal;
11649 struct frame_info *frame;
11650 struct gdbarch *frame_gdbarch;
11651 struct frame_id stack_frame_id;
11652 struct frame_id caller_frame_id;
11653 struct breakpoint *location_breakpoint;
11654 struct breakpoint *caller_breakpoint = NULL;
11655 struct cleanup *old_chain;
11656 int thread;
11657 struct thread_info *tp;
11658 struct until_break_fsm *sm;
11659
11660 clear_proceed_status (0);
11661
11662 /* Set a breakpoint where the user wants it and at return from
11663 this function. */
11664
11665 event_location_up location = string_to_event_location (&arg, current_language);
11666
11667 if (last_displayed_sal_is_valid ())
11668 sals = decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE, NULL,
11669 get_last_displayed_symtab (),
11670 get_last_displayed_line ());
11671 else
11672 sals = decode_line_1 (location.get (), DECODE_LINE_FUNFIRSTLINE,
11673 NULL, (struct symtab *) NULL, 0);
11674
11675 if (sals.nelts != 1)
11676 error (_("Couldn't get information on specified line."));
11677
11678 sal = sals.sals[0];
11679 xfree (sals.sals); /* malloc'd, so freed. */
11680
11681 if (*arg)
11682 error (_("Junk at end of arguments."));
11683
11684 resolve_sal_pc (&sal);
11685
11686 tp = inferior_thread ();
11687 thread = tp->global_num;
11688
11689 old_chain = make_cleanup (null_cleanup, NULL);
11690
11691 /* Note linespec handling above invalidates the frame chain.
11692 Installing a breakpoint also invalidates the frame chain (as it
11693 may need to switch threads), so do any frame handling before
11694 that. */
11695
11696 frame = get_selected_frame (NULL);
11697 frame_gdbarch = get_frame_arch (frame);
11698 stack_frame_id = get_stack_frame_id (frame);
11699 caller_frame_id = frame_unwind_caller_id (frame);
11700
11701 /* Keep within the current frame, or in frames called by the current
11702 one. */
11703
11704 if (frame_id_p (caller_frame_id))
11705 {
11706 struct symtab_and_line sal2;
11707 struct gdbarch *caller_gdbarch;
11708
11709 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11710 sal2.pc = frame_unwind_caller_pc (frame);
11711 caller_gdbarch = frame_unwind_caller_arch (frame);
11712 caller_breakpoint = set_momentary_breakpoint (caller_gdbarch,
11713 sal2,
11714 caller_frame_id,
11715 bp_until);
11716 make_cleanup_delete_breakpoint (caller_breakpoint);
11717
11718 set_longjmp_breakpoint (tp, caller_frame_id);
11719 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
11720 }
11721
11722 /* set_momentary_breakpoint could invalidate FRAME. */
11723 frame = NULL;
11724
11725 if (anywhere)
11726 /* If the user told us to continue until a specified location,
11727 we don't specify a frame at which we need to stop. */
11728 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11729 null_frame_id, bp_until);
11730 else
11731 /* Otherwise, specify the selected frame, because we want to stop
11732 only at the very same frame. */
11733 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11734 stack_frame_id, bp_until);
11735 make_cleanup_delete_breakpoint (location_breakpoint);
11736
11737 sm = new_until_break_fsm (command_interp (), tp->global_num,
11738 location_breakpoint, caller_breakpoint);
11739 tp->thread_fsm = &sm->thread_fsm;
11740
11741 discard_cleanups (old_chain);
11742
11743 proceed (-1, GDB_SIGNAL_DEFAULT);
11744 }
11745
11746 /* This function attempts to parse an optional "if <cond>" clause
11747 from the arg string. If one is not found, it returns NULL.
11748
11749 Else, it returns a pointer to the condition string. (It does not
11750 attempt to evaluate the string against a particular block.) And,
11751 it updates arg to point to the first character following the parsed
11752 if clause in the arg string. */
11753
11754 const char *
11755 ep_parse_optional_if_clause (const char **arg)
11756 {
11757 const char *cond_string;
11758
11759 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
11760 return NULL;
11761
11762 /* Skip the "if" keyword. */
11763 (*arg) += 2;
11764
11765 /* Skip any extra leading whitespace, and record the start of the
11766 condition string. */
11767 *arg = skip_spaces_const (*arg);
11768 cond_string = *arg;
11769
11770 /* Assume that the condition occupies the remainder of the arg
11771 string. */
11772 (*arg) += strlen (cond_string);
11773
11774 return cond_string;
11775 }
11776
11777 /* Commands to deal with catching events, such as signals, exceptions,
11778 process start/exit, etc. */
11779
11780 typedef enum
11781 {
11782 catch_fork_temporary, catch_vfork_temporary,
11783 catch_fork_permanent, catch_vfork_permanent
11784 }
11785 catch_fork_kind;
11786
11787 static void
11788 catch_fork_command_1 (char *arg_entry, int from_tty,
11789 struct cmd_list_element *command)
11790 {
11791 const char *arg = arg_entry;
11792 struct gdbarch *gdbarch = get_current_arch ();
11793 const char *cond_string = NULL;
11794 catch_fork_kind fork_kind;
11795 int tempflag;
11796
11797 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11798 tempflag = (fork_kind == catch_fork_temporary
11799 || fork_kind == catch_vfork_temporary);
11800
11801 if (!arg)
11802 arg = "";
11803 arg = skip_spaces_const (arg);
11804
11805 /* The allowed syntax is:
11806 catch [v]fork
11807 catch [v]fork if <cond>
11808
11809 First, check if there's an if clause. */
11810 cond_string = ep_parse_optional_if_clause (&arg);
11811
11812 if ((*arg != '\0') && !isspace (*arg))
11813 error (_("Junk at end of arguments."));
11814
11815 /* If this target supports it, create a fork or vfork catchpoint
11816 and enable reporting of such events. */
11817 switch (fork_kind)
11818 {
11819 case catch_fork_temporary:
11820 case catch_fork_permanent:
11821 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11822 &catch_fork_breakpoint_ops);
11823 break;
11824 case catch_vfork_temporary:
11825 case catch_vfork_permanent:
11826 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11827 &catch_vfork_breakpoint_ops);
11828 break;
11829 default:
11830 error (_("unsupported or unknown fork kind; cannot catch it"));
11831 break;
11832 }
11833 }
11834
11835 static void
11836 catch_exec_command_1 (char *arg_entry, int from_tty,
11837 struct cmd_list_element *command)
11838 {
11839 const char *arg = arg_entry;
11840 struct exec_catchpoint *c;
11841 struct gdbarch *gdbarch = get_current_arch ();
11842 int tempflag;
11843 const char *cond_string = NULL;
11844
11845 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11846
11847 if (!arg)
11848 arg = "";
11849 arg = skip_spaces_const (arg);
11850
11851 /* The allowed syntax is:
11852 catch exec
11853 catch exec if <cond>
11854
11855 First, check if there's an if clause. */
11856 cond_string = ep_parse_optional_if_clause (&arg);
11857
11858 if ((*arg != '\0') && !isspace (*arg))
11859 error (_("Junk at end of arguments."));
11860
11861 c = new exec_catchpoint ();
11862 init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
11863 &catch_exec_breakpoint_ops);
11864 c->exec_pathname = NULL;
11865
11866 install_breakpoint (0, &c->base, 1);
11867 }
11868
11869 void
11870 init_ada_exception_breakpoint (struct breakpoint *b,
11871 struct gdbarch *gdbarch,
11872 struct symtab_and_line sal,
11873 char *addr_string,
11874 const struct breakpoint_ops *ops,
11875 int tempflag,
11876 int enabled,
11877 int from_tty)
11878 {
11879 if (from_tty)
11880 {
11881 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11882 if (!loc_gdbarch)
11883 loc_gdbarch = gdbarch;
11884
11885 describe_other_breakpoints (loc_gdbarch,
11886 sal.pspace, sal.pc, sal.section, -1);
11887 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11888 version for exception catchpoints, because two catchpoints
11889 used for different exception names will use the same address.
11890 In this case, a "breakpoint ... also set at..." warning is
11891 unproductive. Besides, the warning phrasing is also a bit
11892 inappropriate, we should use the word catchpoint, and tell
11893 the user what type of catchpoint it is. The above is good
11894 enough for now, though. */
11895 }
11896
11897 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
11898
11899 b->enable_state = enabled ? bp_enabled : bp_disabled;
11900 b->disposition = tempflag ? disp_del : disp_donttouch;
11901 b->location = string_to_event_location (&addr_string,
11902 language_def (language_ada));
11903 b->language = language_ada;
11904 }
11905
11906 static void
11907 catch_command (char *arg, int from_tty)
11908 {
11909 error (_("Catch requires an event name."));
11910 }
11911 \f
11912
11913 static void
11914 tcatch_command (char *arg, int from_tty)
11915 {
11916 error (_("Catch requires an event name."));
11917 }
11918
11919 /* A qsort comparison function that sorts breakpoints in order. */
11920
11921 static int
11922 compare_breakpoints (const void *a, const void *b)
11923 {
11924 const breakpoint_p *ba = (const breakpoint_p *) a;
11925 uintptr_t ua = (uintptr_t) *ba;
11926 const breakpoint_p *bb = (const breakpoint_p *) b;
11927 uintptr_t ub = (uintptr_t) *bb;
11928
11929 if ((*ba)->number < (*bb)->number)
11930 return -1;
11931 else if ((*ba)->number > (*bb)->number)
11932 return 1;
11933
11934 /* Now sort by address, in case we see, e..g, two breakpoints with
11935 the number 0. */
11936 if (ua < ub)
11937 return -1;
11938 return ua > ub ? 1 : 0;
11939 }
11940
11941 /* Delete breakpoints by address or line. */
11942
11943 static void
11944 clear_command (char *arg, int from_tty)
11945 {
11946 struct breakpoint *b, *prev;
11947 VEC(breakpoint_p) *found = 0;
11948 int ix;
11949 int default_match;
11950 struct symtabs_and_lines sals;
11951 struct symtab_and_line sal;
11952 int i;
11953 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
11954
11955 if (arg)
11956 {
11957 sals = decode_line_with_current_source (arg,
11958 (DECODE_LINE_FUNFIRSTLINE
11959 | DECODE_LINE_LIST_MODE));
11960 make_cleanup (xfree, sals.sals);
11961 default_match = 0;
11962 }
11963 else
11964 {
11965 sals.sals = XNEW (struct symtab_and_line);
11966 make_cleanup (xfree, sals.sals);
11967 init_sal (&sal); /* Initialize to zeroes. */
11968
11969 /* Set sal's line, symtab, pc, and pspace to the values
11970 corresponding to the last call to print_frame_info. If the
11971 codepoint is not valid, this will set all the fields to 0. */
11972 get_last_displayed_sal (&sal);
11973 if (sal.symtab == 0)
11974 error (_("No source file specified."));
11975
11976 sals.sals[0] = sal;
11977 sals.nelts = 1;
11978
11979 default_match = 1;
11980 }
11981
11982 /* We don't call resolve_sal_pc here. That's not as bad as it
11983 seems, because all existing breakpoints typically have both
11984 file/line and pc set. So, if clear is given file/line, we can
11985 match this to existing breakpoint without obtaining pc at all.
11986
11987 We only support clearing given the address explicitly
11988 present in breakpoint table. Say, we've set breakpoint
11989 at file:line. There were several PC values for that file:line,
11990 due to optimization, all in one block.
11991
11992 We've picked one PC value. If "clear" is issued with another
11993 PC corresponding to the same file:line, the breakpoint won't
11994 be cleared. We probably can still clear the breakpoint, but
11995 since the other PC value is never presented to user, user
11996 can only find it by guessing, and it does not seem important
11997 to support that. */
11998
11999 /* For each line spec given, delete bps which correspond to it. Do
12000 it in two passes, solely to preserve the current behavior that
12001 from_tty is forced true if we delete more than one
12002 breakpoint. */
12003
12004 found = NULL;
12005 make_cleanup (VEC_cleanup (breakpoint_p), &found);
12006 for (i = 0; i < sals.nelts; i++)
12007 {
12008 const char *sal_fullname;
12009
12010 /* If exact pc given, clear bpts at that pc.
12011 If line given (pc == 0), clear all bpts on specified line.
12012 If defaulting, clear all bpts on default line
12013 or at default pc.
12014
12015 defaulting sal.pc != 0 tests to do
12016
12017 0 1 pc
12018 1 1 pc _and_ line
12019 0 0 line
12020 1 0 <can't happen> */
12021
12022 sal = sals.sals[i];
12023 sal_fullname = (sal.symtab == NULL
12024 ? NULL : symtab_to_fullname (sal.symtab));
12025
12026 /* Find all matching breakpoints and add them to 'found'. */
12027 ALL_BREAKPOINTS (b)
12028 {
12029 int match = 0;
12030 /* Are we going to delete b? */
12031 if (b->type != bp_none && !is_watchpoint (b))
12032 {
12033 struct bp_location *loc = b->loc;
12034 for (; loc; loc = loc->next)
12035 {
12036 /* If the user specified file:line, don't allow a PC
12037 match. This matches historical gdb behavior. */
12038 int pc_match = (!sal.explicit_line
12039 && sal.pc
12040 && (loc->pspace == sal.pspace)
12041 && (loc->address == sal.pc)
12042 && (!section_is_overlay (loc->section)
12043 || loc->section == sal.section));
12044 int line_match = 0;
12045
12046 if ((default_match || sal.explicit_line)
12047 && loc->symtab != NULL
12048 && sal_fullname != NULL
12049 && sal.pspace == loc->pspace
12050 && loc->line_number == sal.line
12051 && filename_cmp (symtab_to_fullname (loc->symtab),
12052 sal_fullname) == 0)
12053 line_match = 1;
12054
12055 if (pc_match || line_match)
12056 {
12057 match = 1;
12058 break;
12059 }
12060 }
12061 }
12062
12063 if (match)
12064 VEC_safe_push(breakpoint_p, found, b);
12065 }
12066 }
12067
12068 /* Now go thru the 'found' chain and delete them. */
12069 if (VEC_empty(breakpoint_p, found))
12070 {
12071 if (arg)
12072 error (_("No breakpoint at %s."), arg);
12073 else
12074 error (_("No breakpoint at this line."));
12075 }
12076
12077 /* Remove duplicates from the vec. */
12078 qsort (VEC_address (breakpoint_p, found),
12079 VEC_length (breakpoint_p, found),
12080 sizeof (breakpoint_p),
12081 compare_breakpoints);
12082 prev = VEC_index (breakpoint_p, found, 0);
12083 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
12084 {
12085 if (b == prev)
12086 {
12087 VEC_ordered_remove (breakpoint_p, found, ix);
12088 --ix;
12089 }
12090 }
12091
12092 if (VEC_length(breakpoint_p, found) > 1)
12093 from_tty = 1; /* Always report if deleted more than one. */
12094 if (from_tty)
12095 {
12096 if (VEC_length(breakpoint_p, found) == 1)
12097 printf_unfiltered (_("Deleted breakpoint "));
12098 else
12099 printf_unfiltered (_("Deleted breakpoints "));
12100 }
12101
12102 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
12103 {
12104 if (from_tty)
12105 printf_unfiltered ("%d ", b->number);
12106 delete_breakpoint (b);
12107 }
12108 if (from_tty)
12109 putchar_unfiltered ('\n');
12110
12111 do_cleanups (cleanups);
12112 }
12113 \f
12114 /* Delete breakpoint in BS if they are `delete' breakpoints and
12115 all breakpoints that are marked for deletion, whether hit or not.
12116 This is called after any breakpoint is hit, or after errors. */
12117
12118 void
12119 breakpoint_auto_delete (bpstat bs)
12120 {
12121 struct breakpoint *b, *b_tmp;
12122
12123 for (; bs; bs = bs->next)
12124 if (bs->breakpoint_at
12125 && bs->breakpoint_at->disposition == disp_del
12126 && bs->stop)
12127 delete_breakpoint (bs->breakpoint_at);
12128
12129 ALL_BREAKPOINTS_SAFE (b, b_tmp)
12130 {
12131 if (b->disposition == disp_del_at_next_stop)
12132 delete_breakpoint (b);
12133 }
12134 }
12135
12136 /* A comparison function for bp_location AP and BP being interfaced to
12137 qsort. Sort elements primarily by their ADDRESS (no matter what
12138 does breakpoint_address_is_meaningful say for its OWNER),
12139 secondarily by ordering first permanent elements and
12140 terciarily just ensuring the array is sorted stable way despite
12141 qsort being an unstable algorithm. */
12142
12143 static int
12144 bp_location_compare (const void *ap, const void *bp)
12145 {
12146 const struct bp_location *a = *(const struct bp_location **) ap;
12147 const struct bp_location *b = *(const struct bp_location **) bp;
12148
12149 if (a->address != b->address)
12150 return (a->address > b->address) - (a->address < b->address);
12151
12152 /* Sort locations at the same address by their pspace number, keeping
12153 locations of the same inferior (in a multi-inferior environment)
12154 grouped. */
12155
12156 if (a->pspace->num != b->pspace->num)
12157 return ((a->pspace->num > b->pspace->num)
12158 - (a->pspace->num < b->pspace->num));
12159
12160 /* Sort permanent breakpoints first. */
12161 if (a->permanent != b->permanent)
12162 return (a->permanent < b->permanent) - (a->permanent > b->permanent);
12163
12164 /* Make the internal GDB representation stable across GDB runs
12165 where A and B memory inside GDB can differ. Breakpoint locations of
12166 the same type at the same address can be sorted in arbitrary order. */
12167
12168 if (a->owner->number != b->owner->number)
12169 return ((a->owner->number > b->owner->number)
12170 - (a->owner->number < b->owner->number));
12171
12172 return (a > b) - (a < b);
12173 }
12174
12175 /* Set bp_location_placed_address_before_address_max and
12176 bp_location_shadow_len_after_address_max according to the current
12177 content of the bp_location array. */
12178
12179 static void
12180 bp_location_target_extensions_update (void)
12181 {
12182 struct bp_location *bl, **blp_tmp;
12183
12184 bp_location_placed_address_before_address_max = 0;
12185 bp_location_shadow_len_after_address_max = 0;
12186
12187 ALL_BP_LOCATIONS (bl, blp_tmp)
12188 {
12189 CORE_ADDR start, end, addr;
12190
12191 if (!bp_location_has_shadow (bl))
12192 continue;
12193
12194 start = bl->target_info.placed_address;
12195 end = start + bl->target_info.shadow_len;
12196
12197 gdb_assert (bl->address >= start);
12198 addr = bl->address - start;
12199 if (addr > bp_location_placed_address_before_address_max)
12200 bp_location_placed_address_before_address_max = addr;
12201
12202 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
12203
12204 gdb_assert (bl->address < end);
12205 addr = end - bl->address;
12206 if (addr > bp_location_shadow_len_after_address_max)
12207 bp_location_shadow_len_after_address_max = addr;
12208 }
12209 }
12210
12211 /* Download tracepoint locations if they haven't been. */
12212
12213 static void
12214 download_tracepoint_locations (void)
12215 {
12216 struct breakpoint *b;
12217 struct cleanup *old_chain;
12218 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
12219
12220 old_chain = save_current_space_and_thread ();
12221
12222 ALL_TRACEPOINTS (b)
12223 {
12224 struct bp_location *bl;
12225 struct tracepoint *t;
12226 int bp_location_downloaded = 0;
12227
12228 if ((b->type == bp_fast_tracepoint
12229 ? !may_insert_fast_tracepoints
12230 : !may_insert_tracepoints))
12231 continue;
12232
12233 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
12234 {
12235 if (target_can_download_tracepoint ())
12236 can_download_tracepoint = TRIBOOL_TRUE;
12237 else
12238 can_download_tracepoint = TRIBOOL_FALSE;
12239 }
12240
12241 if (can_download_tracepoint == TRIBOOL_FALSE)
12242 break;
12243
12244 for (bl = b->loc; bl; bl = bl->next)
12245 {
12246 /* In tracepoint, locations are _never_ duplicated, so
12247 should_be_inserted is equivalent to
12248 unduplicated_should_be_inserted. */
12249 if (!should_be_inserted (bl) || bl->inserted)
12250 continue;
12251
12252 switch_to_program_space_and_thread (bl->pspace);
12253
12254 target_download_tracepoint (bl);
12255
12256 bl->inserted = 1;
12257 bp_location_downloaded = 1;
12258 }
12259 t = (struct tracepoint *) b;
12260 t->number_on_target = b->number;
12261 if (bp_location_downloaded)
12262 observer_notify_breakpoint_modified (b);
12263 }
12264
12265 do_cleanups (old_chain);
12266 }
12267
12268 /* Swap the insertion/duplication state between two locations. */
12269
12270 static void
12271 swap_insertion (struct bp_location *left, struct bp_location *right)
12272 {
12273 const int left_inserted = left->inserted;
12274 const int left_duplicate = left->duplicate;
12275 const int left_needs_update = left->needs_update;
12276 const struct bp_target_info left_target_info = left->target_info;
12277
12278 /* Locations of tracepoints can never be duplicated. */
12279 if (is_tracepoint (left->owner))
12280 gdb_assert (!left->duplicate);
12281 if (is_tracepoint (right->owner))
12282 gdb_assert (!right->duplicate);
12283
12284 left->inserted = right->inserted;
12285 left->duplicate = right->duplicate;
12286 left->needs_update = right->needs_update;
12287 left->target_info = right->target_info;
12288 right->inserted = left_inserted;
12289 right->duplicate = left_duplicate;
12290 right->needs_update = left_needs_update;
12291 right->target_info = left_target_info;
12292 }
12293
12294 /* Force the re-insertion of the locations at ADDRESS. This is called
12295 once a new/deleted/modified duplicate location is found and we are evaluating
12296 conditions on the target's side. Such conditions need to be updated on
12297 the target. */
12298
12299 static void
12300 force_breakpoint_reinsertion (struct bp_location *bl)
12301 {
12302 struct bp_location **locp = NULL, **loc2p;
12303 struct bp_location *loc;
12304 CORE_ADDR address = 0;
12305 int pspace_num;
12306
12307 address = bl->address;
12308 pspace_num = bl->pspace->num;
12309
12310 /* This is only meaningful if the target is
12311 evaluating conditions and if the user has
12312 opted for condition evaluation on the target's
12313 side. */
12314 if (gdb_evaluates_breakpoint_condition_p ()
12315 || !target_supports_evaluation_of_breakpoint_conditions ())
12316 return;
12317
12318 /* Flag all breakpoint locations with this address and
12319 the same program space as the location
12320 as "its condition has changed". We need to
12321 update the conditions on the target's side. */
12322 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
12323 {
12324 loc = *loc2p;
12325
12326 if (!is_breakpoint (loc->owner)
12327 || pspace_num != loc->pspace->num)
12328 continue;
12329
12330 /* Flag the location appropriately. We use a different state to
12331 let everyone know that we already updated the set of locations
12332 with addr bl->address and program space bl->pspace. This is so
12333 we don't have to keep calling these functions just to mark locations
12334 that have already been marked. */
12335 loc->condition_changed = condition_updated;
12336
12337 /* Free the agent expression bytecode as well. We will compute
12338 it later on. */
12339 loc->cond_bytecode.reset ();
12340 }
12341 }
12342 /* Called whether new breakpoints are created, or existing breakpoints
12343 deleted, to update the global location list and recompute which
12344 locations are duplicate of which.
12345
12346 The INSERT_MODE flag determines whether locations may not, may, or
12347 shall be inserted now. See 'enum ugll_insert_mode' for more
12348 info. */
12349
12350 static void
12351 update_global_location_list (enum ugll_insert_mode insert_mode)
12352 {
12353 struct breakpoint *b;
12354 struct bp_location **locp, *loc;
12355 struct cleanup *cleanups;
12356 /* Last breakpoint location address that was marked for update. */
12357 CORE_ADDR last_addr = 0;
12358 /* Last breakpoint location program space that was marked for update. */
12359 int last_pspace_num = -1;
12360
12361 /* Used in the duplicates detection below. When iterating over all
12362 bp_locations, points to the first bp_location of a given address.
12363 Breakpoints and watchpoints of different types are never
12364 duplicates of each other. Keep one pointer for each type of
12365 breakpoint/watchpoint, so we only need to loop over all locations
12366 once. */
12367 struct bp_location *bp_loc_first; /* breakpoint */
12368 struct bp_location *wp_loc_first; /* hardware watchpoint */
12369 struct bp_location *awp_loc_first; /* access watchpoint */
12370 struct bp_location *rwp_loc_first; /* read watchpoint */
12371
12372 /* Saved former bp_location array which we compare against the newly
12373 built bp_location from the current state of ALL_BREAKPOINTS. */
12374 struct bp_location **old_location, **old_locp;
12375 unsigned old_location_count;
12376
12377 old_location = bp_location;
12378 old_location_count = bp_location_count;
12379 bp_location = NULL;
12380 bp_location_count = 0;
12381 cleanups = make_cleanup (xfree, old_location);
12382
12383 ALL_BREAKPOINTS (b)
12384 for (loc = b->loc; loc; loc = loc->next)
12385 bp_location_count++;
12386
12387 bp_location = XNEWVEC (struct bp_location *, bp_location_count);
12388 locp = bp_location;
12389 ALL_BREAKPOINTS (b)
12390 for (loc = b->loc; loc; loc = loc->next)
12391 *locp++ = loc;
12392 qsort (bp_location, bp_location_count, sizeof (*bp_location),
12393 bp_location_compare);
12394
12395 bp_location_target_extensions_update ();
12396
12397 /* Identify bp_location instances that are no longer present in the
12398 new list, and therefore should be freed. Note that it's not
12399 necessary that those locations should be removed from inferior --
12400 if there's another location at the same address (previously
12401 marked as duplicate), we don't need to remove/insert the
12402 location.
12403
12404 LOCP is kept in sync with OLD_LOCP, each pointing to the current
12405 and former bp_location array state respectively. */
12406
12407 locp = bp_location;
12408 for (old_locp = old_location; old_locp < old_location + old_location_count;
12409 old_locp++)
12410 {
12411 struct bp_location *old_loc = *old_locp;
12412 struct bp_location **loc2p;
12413
12414 /* Tells if 'old_loc' is found among the new locations. If
12415 not, we have to free it. */
12416 int found_object = 0;
12417 /* Tells if the location should remain inserted in the target. */
12418 int keep_in_target = 0;
12419 int removed = 0;
12420
12421 /* Skip LOCP entries which will definitely never be needed.
12422 Stop either at or being the one matching OLD_LOC. */
12423 while (locp < bp_location + bp_location_count
12424 && (*locp)->address < old_loc->address)
12425 locp++;
12426
12427 for (loc2p = locp;
12428 (loc2p < bp_location + bp_location_count
12429 && (*loc2p)->address == old_loc->address);
12430 loc2p++)
12431 {
12432 /* Check if this is a new/duplicated location or a duplicated
12433 location that had its condition modified. If so, we want to send
12434 its condition to the target if evaluation of conditions is taking
12435 place there. */
12436 if ((*loc2p)->condition_changed == condition_modified
12437 && (last_addr != old_loc->address
12438 || last_pspace_num != old_loc->pspace->num))
12439 {
12440 force_breakpoint_reinsertion (*loc2p);
12441 last_pspace_num = old_loc->pspace->num;
12442 }
12443
12444 if (*loc2p == old_loc)
12445 found_object = 1;
12446 }
12447
12448 /* We have already handled this address, update it so that we don't
12449 have to go through updates again. */
12450 last_addr = old_loc->address;
12451
12452 /* Target-side condition evaluation: Handle deleted locations. */
12453 if (!found_object)
12454 force_breakpoint_reinsertion (old_loc);
12455
12456 /* If this location is no longer present, and inserted, look if
12457 there's maybe a new location at the same address. If so,
12458 mark that one inserted, and don't remove this one. This is
12459 needed so that we don't have a time window where a breakpoint
12460 at certain location is not inserted. */
12461
12462 if (old_loc->inserted)
12463 {
12464 /* If the location is inserted now, we might have to remove
12465 it. */
12466
12467 if (found_object && should_be_inserted (old_loc))
12468 {
12469 /* The location is still present in the location list,
12470 and still should be inserted. Don't do anything. */
12471 keep_in_target = 1;
12472 }
12473 else
12474 {
12475 /* This location still exists, but it won't be kept in the
12476 target since it may have been disabled. We proceed to
12477 remove its target-side condition. */
12478
12479 /* The location is either no longer present, or got
12480 disabled. See if there's another location at the
12481 same address, in which case we don't need to remove
12482 this one from the target. */
12483
12484 /* OLD_LOC comes from existing struct breakpoint. */
12485 if (breakpoint_address_is_meaningful (old_loc->owner))
12486 {
12487 for (loc2p = locp;
12488 (loc2p < bp_location + bp_location_count
12489 && (*loc2p)->address == old_loc->address);
12490 loc2p++)
12491 {
12492 struct bp_location *loc2 = *loc2p;
12493
12494 if (breakpoint_locations_match (loc2, old_loc))
12495 {
12496 /* Read watchpoint locations are switched to
12497 access watchpoints, if the former are not
12498 supported, but the latter are. */
12499 if (is_hardware_watchpoint (old_loc->owner))
12500 {
12501 gdb_assert (is_hardware_watchpoint (loc2->owner));
12502 loc2->watchpoint_type = old_loc->watchpoint_type;
12503 }
12504
12505 /* loc2 is a duplicated location. We need to check
12506 if it should be inserted in case it will be
12507 unduplicated. */
12508 if (loc2 != old_loc
12509 && unduplicated_should_be_inserted (loc2))
12510 {
12511 swap_insertion (old_loc, loc2);
12512 keep_in_target = 1;
12513 break;
12514 }
12515 }
12516 }
12517 }
12518 }
12519
12520 if (!keep_in_target)
12521 {
12522 if (remove_breakpoint (old_loc))
12523 {
12524 /* This is just about all we can do. We could keep
12525 this location on the global list, and try to
12526 remove it next time, but there's no particular
12527 reason why we will succeed next time.
12528
12529 Note that at this point, old_loc->owner is still
12530 valid, as delete_breakpoint frees the breakpoint
12531 only after calling us. */
12532 printf_filtered (_("warning: Error removing "
12533 "breakpoint %d\n"),
12534 old_loc->owner->number);
12535 }
12536 removed = 1;
12537 }
12538 }
12539
12540 if (!found_object)
12541 {
12542 if (removed && target_is_non_stop_p ()
12543 && need_moribund_for_location_type (old_loc))
12544 {
12545 /* This location was removed from the target. In
12546 non-stop mode, a race condition is possible where
12547 we've removed a breakpoint, but stop events for that
12548 breakpoint are already queued and will arrive later.
12549 We apply an heuristic to be able to distinguish such
12550 SIGTRAPs from other random SIGTRAPs: we keep this
12551 breakpoint location for a bit, and will retire it
12552 after we see some number of events. The theory here
12553 is that reporting of events should, "on the average",
12554 be fair, so after a while we'll see events from all
12555 threads that have anything of interest, and no longer
12556 need to keep this breakpoint location around. We
12557 don't hold locations forever so to reduce chances of
12558 mistaking a non-breakpoint SIGTRAP for a breakpoint
12559 SIGTRAP.
12560
12561 The heuristic failing can be disastrous on
12562 decr_pc_after_break targets.
12563
12564 On decr_pc_after_break targets, like e.g., x86-linux,
12565 if we fail to recognize a late breakpoint SIGTRAP,
12566 because events_till_retirement has reached 0 too
12567 soon, we'll fail to do the PC adjustment, and report
12568 a random SIGTRAP to the user. When the user resumes
12569 the inferior, it will most likely immediately crash
12570 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
12571 corrupted, because of being resumed e.g., in the
12572 middle of a multi-byte instruction, or skipped a
12573 one-byte instruction. This was actually seen happen
12574 on native x86-linux, and should be less rare on
12575 targets that do not support new thread events, like
12576 remote, due to the heuristic depending on
12577 thread_count.
12578
12579 Mistaking a random SIGTRAP for a breakpoint trap
12580 causes similar symptoms (PC adjustment applied when
12581 it shouldn't), but then again, playing with SIGTRAPs
12582 behind the debugger's back is asking for trouble.
12583
12584 Since hardware watchpoint traps are always
12585 distinguishable from other traps, so we don't need to
12586 apply keep hardware watchpoint moribund locations
12587 around. We simply always ignore hardware watchpoint
12588 traps we can no longer explain. */
12589
12590 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12591 old_loc->owner = NULL;
12592
12593 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
12594 }
12595 else
12596 {
12597 old_loc->owner = NULL;
12598 decref_bp_location (&old_loc);
12599 }
12600 }
12601 }
12602
12603 /* Rescan breakpoints at the same address and section, marking the
12604 first one as "first" and any others as "duplicates". This is so
12605 that the bpt instruction is only inserted once. If we have a
12606 permanent breakpoint at the same place as BPT, make that one the
12607 official one, and the rest as duplicates. Permanent breakpoints
12608 are sorted first for the same address.
12609
12610 Do the same for hardware watchpoints, but also considering the
12611 watchpoint's type (regular/access/read) and length. */
12612
12613 bp_loc_first = NULL;
12614 wp_loc_first = NULL;
12615 awp_loc_first = NULL;
12616 rwp_loc_first = NULL;
12617 ALL_BP_LOCATIONS (loc, locp)
12618 {
12619 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12620 non-NULL. */
12621 struct bp_location **loc_first_p;
12622 b = loc->owner;
12623
12624 if (!unduplicated_should_be_inserted (loc)
12625 || !breakpoint_address_is_meaningful (b)
12626 /* Don't detect duplicate for tracepoint locations because they are
12627 never duplicated. See the comments in field `duplicate' of
12628 `struct bp_location'. */
12629 || is_tracepoint (b))
12630 {
12631 /* Clear the condition modification flag. */
12632 loc->condition_changed = condition_unchanged;
12633 continue;
12634 }
12635
12636 if (b->type == bp_hardware_watchpoint)
12637 loc_first_p = &wp_loc_first;
12638 else if (b->type == bp_read_watchpoint)
12639 loc_first_p = &rwp_loc_first;
12640 else if (b->type == bp_access_watchpoint)
12641 loc_first_p = &awp_loc_first;
12642 else
12643 loc_first_p = &bp_loc_first;
12644
12645 if (*loc_first_p == NULL
12646 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12647 || !breakpoint_locations_match (loc, *loc_first_p))
12648 {
12649 *loc_first_p = loc;
12650 loc->duplicate = 0;
12651
12652 if (is_breakpoint (loc->owner) && loc->condition_changed)
12653 {
12654 loc->needs_update = 1;
12655 /* Clear the condition modification flag. */
12656 loc->condition_changed = condition_unchanged;
12657 }
12658 continue;
12659 }
12660
12661
12662 /* This and the above ensure the invariant that the first location
12663 is not duplicated, and is the inserted one.
12664 All following are marked as duplicated, and are not inserted. */
12665 if (loc->inserted)
12666 swap_insertion (loc, *loc_first_p);
12667 loc->duplicate = 1;
12668
12669 /* Clear the condition modification flag. */
12670 loc->condition_changed = condition_unchanged;
12671 }
12672
12673 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
12674 {
12675 if (insert_mode != UGLL_DONT_INSERT)
12676 insert_breakpoint_locations ();
12677 else
12678 {
12679 /* Even though the caller told us to not insert new
12680 locations, we may still need to update conditions on the
12681 target's side of breakpoints that were already inserted
12682 if the target is evaluating breakpoint conditions. We
12683 only update conditions for locations that are marked
12684 "needs_update". */
12685 update_inserted_breakpoint_locations ();
12686 }
12687 }
12688
12689 if (insert_mode != UGLL_DONT_INSERT)
12690 download_tracepoint_locations ();
12691
12692 do_cleanups (cleanups);
12693 }
12694
12695 void
12696 breakpoint_retire_moribund (void)
12697 {
12698 struct bp_location *loc;
12699 int ix;
12700
12701 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12702 if (--(loc->events_till_retirement) == 0)
12703 {
12704 decref_bp_location (&loc);
12705 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12706 --ix;
12707 }
12708 }
12709
12710 static void
12711 update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
12712 {
12713
12714 TRY
12715 {
12716 update_global_location_list (insert_mode);
12717 }
12718 CATCH (e, RETURN_MASK_ERROR)
12719 {
12720 }
12721 END_CATCH
12722 }
12723
12724 /* Clear BKP from a BPS. */
12725
12726 static void
12727 bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12728 {
12729 bpstat bs;
12730
12731 for (bs = bps; bs; bs = bs->next)
12732 if (bs->breakpoint_at == bpt)
12733 {
12734 bs->breakpoint_at = NULL;
12735 bs->old_val = NULL;
12736 /* bs->commands will be freed later. */
12737 }
12738 }
12739
12740 /* Callback for iterate_over_threads. */
12741 static int
12742 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12743 {
12744 struct breakpoint *bpt = (struct breakpoint *) data;
12745
12746 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12747 return 0;
12748 }
12749
12750 /* Helper for breakpoint and tracepoint breakpoint_ops->mention
12751 callbacks. */
12752
12753 static void
12754 say_where (struct breakpoint *b)
12755 {
12756 struct value_print_options opts;
12757
12758 get_user_print_options (&opts);
12759
12760 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12761 single string. */
12762 if (b->loc == NULL)
12763 {
12764 /* For pending locations, the output differs slightly based
12765 on b->extra_string. If this is non-NULL, it contains either
12766 a condition or dprintf arguments. */
12767 if (b->extra_string == NULL)
12768 {
12769 printf_filtered (_(" (%s) pending."),
12770 event_location_to_string (b->location.get ()));
12771 }
12772 else if (b->type == bp_dprintf)
12773 {
12774 printf_filtered (_(" (%s,%s) pending."),
12775 event_location_to_string (b->location.get ()),
12776 b->extra_string);
12777 }
12778 else
12779 {
12780 printf_filtered (_(" (%s %s) pending."),
12781 event_location_to_string (b->location.get ()),
12782 b->extra_string);
12783 }
12784 }
12785 else
12786 {
12787 if (opts.addressprint || b->loc->symtab == NULL)
12788 {
12789 printf_filtered (" at ");
12790 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12791 gdb_stdout);
12792 }
12793 if (b->loc->symtab != NULL)
12794 {
12795 /* If there is a single location, we can print the location
12796 more nicely. */
12797 if (b->loc->next == NULL)
12798 printf_filtered (": file %s, line %d.",
12799 symtab_to_filename_for_display (b->loc->symtab),
12800 b->loc->line_number);
12801 else
12802 /* This is not ideal, but each location may have a
12803 different file name, and this at least reflects the
12804 real situation somewhat. */
12805 printf_filtered (": %s.",
12806 event_location_to_string (b->location.get ()));
12807 }
12808
12809 if (b->loc->next)
12810 {
12811 struct bp_location *loc = b->loc;
12812 int n = 0;
12813 for (; loc; loc = loc->next)
12814 ++n;
12815 printf_filtered (" (%d locations)", n);
12816 }
12817 }
12818 }
12819
12820 /* Default bp_location_ops methods. */
12821
12822 static void
12823 bp_location_dtor (struct bp_location *self)
12824 {
12825 xfree (self->function_name);
12826 }
12827
12828 static const struct bp_location_ops bp_location_ops =
12829 {
12830 bp_location_dtor
12831 };
12832
12833 /* Default breakpoint_ops methods all breakpoint_ops ultimately
12834 inherit from. */
12835
12836 static void
12837 base_breakpoint_dtor (struct breakpoint *self)
12838 {
12839 decref_counted_command_line (&self->commands);
12840 xfree (self->cond_string);
12841 xfree (self->extra_string);
12842 xfree (self->filter);
12843 }
12844
12845 static struct bp_location *
12846 base_breakpoint_allocate_location (struct breakpoint *self)
12847 {
12848 struct bp_location *loc;
12849
12850 loc = new struct bp_location ();
12851 init_bp_location (loc, &bp_location_ops, self);
12852 return loc;
12853 }
12854
12855 static void
12856 base_breakpoint_re_set (struct breakpoint *b)
12857 {
12858 /* Nothing to re-set. */
12859 }
12860
12861 #define internal_error_pure_virtual_called() \
12862 gdb_assert_not_reached ("pure virtual function called")
12863
12864 static int
12865 base_breakpoint_insert_location (struct bp_location *bl)
12866 {
12867 internal_error_pure_virtual_called ();
12868 }
12869
12870 static int
12871 base_breakpoint_remove_location (struct bp_location *bl,
12872 enum remove_bp_reason reason)
12873 {
12874 internal_error_pure_virtual_called ();
12875 }
12876
12877 static int
12878 base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12879 struct address_space *aspace,
12880 CORE_ADDR bp_addr,
12881 const struct target_waitstatus *ws)
12882 {
12883 internal_error_pure_virtual_called ();
12884 }
12885
12886 static void
12887 base_breakpoint_check_status (bpstat bs)
12888 {
12889 /* Always stop. */
12890 }
12891
12892 /* A "works_in_software_mode" breakpoint_ops method that just internal
12893 errors. */
12894
12895 static int
12896 base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12897 {
12898 internal_error_pure_virtual_called ();
12899 }
12900
12901 /* A "resources_needed" breakpoint_ops method that just internal
12902 errors. */
12903
12904 static int
12905 base_breakpoint_resources_needed (const struct bp_location *bl)
12906 {
12907 internal_error_pure_virtual_called ();
12908 }
12909
12910 static enum print_stop_action
12911 base_breakpoint_print_it (bpstat bs)
12912 {
12913 internal_error_pure_virtual_called ();
12914 }
12915
12916 static void
12917 base_breakpoint_print_one_detail (const struct breakpoint *self,
12918 struct ui_out *uiout)
12919 {
12920 /* nothing */
12921 }
12922
12923 static void
12924 base_breakpoint_print_mention (struct breakpoint *b)
12925 {
12926 internal_error_pure_virtual_called ();
12927 }
12928
12929 static void
12930 base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12931 {
12932 internal_error_pure_virtual_called ();
12933 }
12934
12935 static void
12936 base_breakpoint_create_sals_from_location
12937 (const struct event_location *location,
12938 struct linespec_result *canonical,
12939 enum bptype type_wanted)
12940 {
12941 internal_error_pure_virtual_called ();
12942 }
12943
12944 static void
12945 base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12946 struct linespec_result *c,
12947 char *cond_string,
12948 char *extra_string,
12949 enum bptype type_wanted,
12950 enum bpdisp disposition,
12951 int thread,
12952 int task, int ignore_count,
12953 const struct breakpoint_ops *o,
12954 int from_tty, int enabled,
12955 int internal, unsigned flags)
12956 {
12957 internal_error_pure_virtual_called ();
12958 }
12959
12960 static void
12961 base_breakpoint_decode_location (struct breakpoint *b,
12962 const struct event_location *location,
12963 struct program_space *search_pspace,
12964 struct symtabs_and_lines *sals)
12965 {
12966 internal_error_pure_virtual_called ();
12967 }
12968
12969 /* The default 'explains_signal' method. */
12970
12971 static int
12972 base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
12973 {
12974 return 1;
12975 }
12976
12977 /* The default "after_condition_true" method. */
12978
12979 static void
12980 base_breakpoint_after_condition_true (struct bpstats *bs)
12981 {
12982 /* Nothing to do. */
12983 }
12984
12985 struct breakpoint_ops base_breakpoint_ops =
12986 {
12987 base_breakpoint_dtor,
12988 base_breakpoint_allocate_location,
12989 base_breakpoint_re_set,
12990 base_breakpoint_insert_location,
12991 base_breakpoint_remove_location,
12992 base_breakpoint_breakpoint_hit,
12993 base_breakpoint_check_status,
12994 base_breakpoint_resources_needed,
12995 base_breakpoint_works_in_software_mode,
12996 base_breakpoint_print_it,
12997 NULL,
12998 base_breakpoint_print_one_detail,
12999 base_breakpoint_print_mention,
13000 base_breakpoint_print_recreate,
13001 base_breakpoint_create_sals_from_location,
13002 base_breakpoint_create_breakpoints_sal,
13003 base_breakpoint_decode_location,
13004 base_breakpoint_explains_signal,
13005 base_breakpoint_after_condition_true,
13006 };
13007
13008 /* Default breakpoint_ops methods. */
13009
13010 static void
13011 bkpt_re_set (struct breakpoint *b)
13012 {
13013 /* FIXME: is this still reachable? */
13014 if (breakpoint_event_location_empty_p (b))
13015 {
13016 /* Anything without a location can't be re-set. */
13017 delete_breakpoint (b);
13018 return;
13019 }
13020
13021 breakpoint_re_set_default (b);
13022 }
13023
13024 static int
13025 bkpt_insert_location (struct bp_location *bl)
13026 {
13027 CORE_ADDR addr = bl->target_info.reqstd_address;
13028
13029 bl->target_info.kind = breakpoint_kind (bl, &addr);
13030 bl->target_info.placed_address = addr;
13031
13032 if (bl->loc_type == bp_loc_hardware_breakpoint)
13033 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
13034 else
13035 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
13036 }
13037
13038 static int
13039 bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
13040 {
13041 if (bl->loc_type == bp_loc_hardware_breakpoint)
13042 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
13043 else
13044 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
13045 }
13046
13047 static int
13048 bkpt_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 (ws->kind != TARGET_WAITKIND_STOPPED
13053 || ws->value.sig != GDB_SIGNAL_TRAP)
13054 return 0;
13055
13056 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
13057 aspace, bp_addr))
13058 return 0;
13059
13060 if (overlay_debugging /* unmapped overlay section */
13061 && section_is_overlay (bl->section)
13062 && !section_is_mapped (bl->section))
13063 return 0;
13064
13065 return 1;
13066 }
13067
13068 static int
13069 dprintf_breakpoint_hit (const struct bp_location *bl,
13070 struct address_space *aspace, CORE_ADDR bp_addr,
13071 const struct target_waitstatus *ws)
13072 {
13073 if (dprintf_style == dprintf_style_agent
13074 && target_can_run_breakpoint_commands ())
13075 {
13076 /* An agent-style dprintf never causes a stop. If we see a trap
13077 for this address it must be for a breakpoint that happens to
13078 be set at the same address. */
13079 return 0;
13080 }
13081
13082 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
13083 }
13084
13085 static int
13086 bkpt_resources_needed (const struct bp_location *bl)
13087 {
13088 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
13089
13090 return 1;
13091 }
13092
13093 static enum print_stop_action
13094 bkpt_print_it (bpstat bs)
13095 {
13096 struct breakpoint *b;
13097 const struct bp_location *bl;
13098 int bp_temp;
13099 struct ui_out *uiout = current_uiout;
13100
13101 gdb_assert (bs->bp_location_at != NULL);
13102
13103 bl = bs->bp_location_at;
13104 b = bs->breakpoint_at;
13105
13106 bp_temp = b->disposition == disp_del;
13107 if (bl->address != bl->requested_address)
13108 breakpoint_adjustment_warning (bl->requested_address,
13109 bl->address,
13110 b->number, 1);
13111 annotate_breakpoint (b->number);
13112 maybe_print_thread_hit_breakpoint (uiout);
13113
13114 if (bp_temp)
13115 uiout->text ("Temporary breakpoint ");
13116 else
13117 uiout->text ("Breakpoint ");
13118 if (uiout->is_mi_like_p ())
13119 {
13120 uiout->field_string ("reason",
13121 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
13122 uiout->field_string ("disp", bpdisp_text (b->disposition));
13123 }
13124 uiout->field_int ("bkptno", b->number);
13125 uiout->text (", ");
13126
13127 return PRINT_SRC_AND_LOC;
13128 }
13129
13130 static void
13131 bkpt_print_mention (struct breakpoint *b)
13132 {
13133 if (current_uiout->is_mi_like_p ())
13134 return;
13135
13136 switch (b->type)
13137 {
13138 case bp_breakpoint:
13139 case bp_gnu_ifunc_resolver:
13140 if (b->disposition == disp_del)
13141 printf_filtered (_("Temporary breakpoint"));
13142 else
13143 printf_filtered (_("Breakpoint"));
13144 printf_filtered (_(" %d"), b->number);
13145 if (b->type == bp_gnu_ifunc_resolver)
13146 printf_filtered (_(" at gnu-indirect-function resolver"));
13147 break;
13148 case bp_hardware_breakpoint:
13149 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
13150 break;
13151 case bp_dprintf:
13152 printf_filtered (_("Dprintf %d"), b->number);
13153 break;
13154 }
13155
13156 say_where (b);
13157 }
13158
13159 static void
13160 bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13161 {
13162 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
13163 fprintf_unfiltered (fp, "tbreak");
13164 else if (tp->type == bp_breakpoint)
13165 fprintf_unfiltered (fp, "break");
13166 else if (tp->type == bp_hardware_breakpoint
13167 && tp->disposition == disp_del)
13168 fprintf_unfiltered (fp, "thbreak");
13169 else if (tp->type == bp_hardware_breakpoint)
13170 fprintf_unfiltered (fp, "hbreak");
13171 else
13172 internal_error (__FILE__, __LINE__,
13173 _("unhandled breakpoint type %d"), (int) tp->type);
13174
13175 fprintf_unfiltered (fp, " %s",
13176 event_location_to_string (tp->location.get ()));
13177
13178 /* Print out extra_string if this breakpoint is pending. It might
13179 contain, for example, conditions that were set by the user. */
13180 if (tp->loc == NULL && tp->extra_string != NULL)
13181 fprintf_unfiltered (fp, " %s", tp->extra_string);
13182
13183 print_recreate_thread (tp, fp);
13184 }
13185
13186 static void
13187 bkpt_create_sals_from_location (const struct event_location *location,
13188 struct linespec_result *canonical,
13189 enum bptype type_wanted)
13190 {
13191 create_sals_from_location_default (location, canonical, type_wanted);
13192 }
13193
13194 static void
13195 bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
13196 struct linespec_result *canonical,
13197 char *cond_string,
13198 char *extra_string,
13199 enum bptype type_wanted,
13200 enum bpdisp disposition,
13201 int thread,
13202 int task, int ignore_count,
13203 const struct breakpoint_ops *ops,
13204 int from_tty, int enabled,
13205 int internal, unsigned flags)
13206 {
13207 create_breakpoints_sal_default (gdbarch, canonical,
13208 cond_string, extra_string,
13209 type_wanted,
13210 disposition, thread, task,
13211 ignore_count, ops, from_tty,
13212 enabled, internal, flags);
13213 }
13214
13215 static void
13216 bkpt_decode_location (struct breakpoint *b,
13217 const struct event_location *location,
13218 struct program_space *search_pspace,
13219 struct symtabs_and_lines *sals)
13220 {
13221 decode_location_default (b, location, search_pspace, sals);
13222 }
13223
13224 /* Virtual table for internal breakpoints. */
13225
13226 static void
13227 internal_bkpt_re_set (struct breakpoint *b)
13228 {
13229 switch (b->type)
13230 {
13231 /* Delete overlay event and longjmp master breakpoints; they
13232 will be reset later by breakpoint_re_set. */
13233 case bp_overlay_event:
13234 case bp_longjmp_master:
13235 case bp_std_terminate_master:
13236 case bp_exception_master:
13237 delete_breakpoint (b);
13238 break;
13239
13240 /* This breakpoint is special, it's set up when the inferior
13241 starts and we really don't want to touch it. */
13242 case bp_shlib_event:
13243
13244 /* Like bp_shlib_event, this breakpoint type is special. Once
13245 it is set up, we do not want to touch it. */
13246 case bp_thread_event:
13247 break;
13248 }
13249 }
13250
13251 static void
13252 internal_bkpt_check_status (bpstat bs)
13253 {
13254 if (bs->breakpoint_at->type == bp_shlib_event)
13255 {
13256 /* If requested, stop when the dynamic linker notifies GDB of
13257 events. This allows the user to get control and place
13258 breakpoints in initializer routines for dynamically loaded
13259 objects (among other things). */
13260 bs->stop = stop_on_solib_events;
13261 bs->print = stop_on_solib_events;
13262 }
13263 else
13264 bs->stop = 0;
13265 }
13266
13267 static enum print_stop_action
13268 internal_bkpt_print_it (bpstat bs)
13269 {
13270 struct breakpoint *b;
13271
13272 b = bs->breakpoint_at;
13273
13274 switch (b->type)
13275 {
13276 case bp_shlib_event:
13277 /* Did we stop because the user set the stop_on_solib_events
13278 variable? (If so, we report this as a generic, "Stopped due
13279 to shlib event" message.) */
13280 print_solib_event (0);
13281 break;
13282
13283 case bp_thread_event:
13284 /* Not sure how we will get here.
13285 GDB should not stop for these breakpoints. */
13286 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
13287 break;
13288
13289 case bp_overlay_event:
13290 /* By analogy with the thread event, GDB should not stop for these. */
13291 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
13292 break;
13293
13294 case bp_longjmp_master:
13295 /* These should never be enabled. */
13296 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
13297 break;
13298
13299 case bp_std_terminate_master:
13300 /* These should never be enabled. */
13301 printf_filtered (_("std::terminate Master Breakpoint: "
13302 "gdb should not stop!\n"));
13303 break;
13304
13305 case bp_exception_master:
13306 /* These should never be enabled. */
13307 printf_filtered (_("Exception Master Breakpoint: "
13308 "gdb should not stop!\n"));
13309 break;
13310 }
13311
13312 return PRINT_NOTHING;
13313 }
13314
13315 static void
13316 internal_bkpt_print_mention (struct breakpoint *b)
13317 {
13318 /* Nothing to mention. These breakpoints are internal. */
13319 }
13320
13321 /* Virtual table for momentary breakpoints */
13322
13323 static void
13324 momentary_bkpt_re_set (struct breakpoint *b)
13325 {
13326 /* Keep temporary breakpoints, which can be encountered when we step
13327 over a dlopen call and solib_add is resetting the breakpoints.
13328 Otherwise these should have been blown away via the cleanup chain
13329 or by breakpoint_init_inferior when we rerun the executable. */
13330 }
13331
13332 static void
13333 momentary_bkpt_check_status (bpstat bs)
13334 {
13335 /* Nothing. The point of these breakpoints is causing a stop. */
13336 }
13337
13338 static enum print_stop_action
13339 momentary_bkpt_print_it (bpstat bs)
13340 {
13341 return PRINT_UNKNOWN;
13342 }
13343
13344 static void
13345 momentary_bkpt_print_mention (struct breakpoint *b)
13346 {
13347 /* Nothing to mention. These breakpoints are internal. */
13348 }
13349
13350 /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
13351
13352 It gets cleared already on the removal of the first one of such placed
13353 breakpoints. This is OK as they get all removed altogether. */
13354
13355 static void
13356 longjmp_bkpt_dtor (struct breakpoint *self)
13357 {
13358 struct thread_info *tp = find_thread_global_id (self->thread);
13359
13360 if (tp)
13361 tp->initiating_frame = null_frame_id;
13362
13363 momentary_breakpoint_ops.dtor (self);
13364 }
13365
13366 /* Specific methods for probe breakpoints. */
13367
13368 static int
13369 bkpt_probe_insert_location (struct bp_location *bl)
13370 {
13371 int v = bkpt_insert_location (bl);
13372
13373 if (v == 0)
13374 {
13375 /* The insertion was successful, now let's set the probe's semaphore
13376 if needed. */
13377 if (bl->probe.probe->pops->set_semaphore != NULL)
13378 bl->probe.probe->pops->set_semaphore (bl->probe.probe,
13379 bl->probe.objfile,
13380 bl->gdbarch);
13381 }
13382
13383 return v;
13384 }
13385
13386 static int
13387 bkpt_probe_remove_location (struct bp_location *bl,
13388 enum remove_bp_reason reason)
13389 {
13390 /* Let's clear the semaphore before removing the location. */
13391 if (bl->probe.probe->pops->clear_semaphore != NULL)
13392 bl->probe.probe->pops->clear_semaphore (bl->probe.probe,
13393 bl->probe.objfile,
13394 bl->gdbarch);
13395
13396 return bkpt_remove_location (bl, reason);
13397 }
13398
13399 static void
13400 bkpt_probe_create_sals_from_location (const struct event_location *location,
13401 struct linespec_result *canonical,
13402 enum bptype type_wanted)
13403 {
13404 struct linespec_sals lsal;
13405
13406 lsal.sals = parse_probes (location, NULL, canonical);
13407 lsal.canonical = xstrdup (event_location_to_string (canonical->location));
13408 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13409 }
13410
13411 static void
13412 bkpt_probe_decode_location (struct breakpoint *b,
13413 const struct event_location *location,
13414 struct program_space *search_pspace,
13415 struct symtabs_and_lines *sals)
13416 {
13417 *sals = parse_probes (location, search_pspace, NULL);
13418 if (!sals->sals)
13419 error (_("probe not found"));
13420 }
13421
13422 /* The breakpoint_ops structure to be used in tracepoints. */
13423
13424 static void
13425 tracepoint_re_set (struct breakpoint *b)
13426 {
13427 breakpoint_re_set_default (b);
13428 }
13429
13430 static int
13431 tracepoint_breakpoint_hit (const struct bp_location *bl,
13432 struct address_space *aspace, CORE_ADDR bp_addr,
13433 const struct target_waitstatus *ws)
13434 {
13435 /* By definition, the inferior does not report stops at
13436 tracepoints. */
13437 return 0;
13438 }
13439
13440 static void
13441 tracepoint_print_one_detail (const struct breakpoint *self,
13442 struct ui_out *uiout)
13443 {
13444 struct tracepoint *tp = (struct tracepoint *) self;
13445 if (tp->static_trace_marker_id)
13446 {
13447 gdb_assert (self->type == bp_static_tracepoint);
13448
13449 uiout->text ("\tmarker id is ");
13450 uiout->field_string ("static-tracepoint-marker-string-id",
13451 tp->static_trace_marker_id);
13452 uiout->text ("\n");
13453 }
13454 }
13455
13456 static void
13457 tracepoint_print_mention (struct breakpoint *b)
13458 {
13459 if (current_uiout->is_mi_like_p ())
13460 return;
13461
13462 switch (b->type)
13463 {
13464 case bp_tracepoint:
13465 printf_filtered (_("Tracepoint"));
13466 printf_filtered (_(" %d"), b->number);
13467 break;
13468 case bp_fast_tracepoint:
13469 printf_filtered (_("Fast tracepoint"));
13470 printf_filtered (_(" %d"), b->number);
13471 break;
13472 case bp_static_tracepoint:
13473 printf_filtered (_("Static tracepoint"));
13474 printf_filtered (_(" %d"), b->number);
13475 break;
13476 default:
13477 internal_error (__FILE__, __LINE__,
13478 _("unhandled tracepoint type %d"), (int) b->type);
13479 }
13480
13481 say_where (b);
13482 }
13483
13484 static void
13485 tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
13486 {
13487 struct tracepoint *tp = (struct tracepoint *) self;
13488
13489 if (self->type == bp_fast_tracepoint)
13490 fprintf_unfiltered (fp, "ftrace");
13491 else if (self->type == bp_static_tracepoint)
13492 fprintf_unfiltered (fp, "strace");
13493 else if (self->type == bp_tracepoint)
13494 fprintf_unfiltered (fp, "trace");
13495 else
13496 internal_error (__FILE__, __LINE__,
13497 _("unhandled tracepoint type %d"), (int) self->type);
13498
13499 fprintf_unfiltered (fp, " %s",
13500 event_location_to_string (self->location.get ()));
13501 print_recreate_thread (self, fp);
13502
13503 if (tp->pass_count)
13504 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
13505 }
13506
13507 static void
13508 tracepoint_create_sals_from_location (const struct event_location *location,
13509 struct linespec_result *canonical,
13510 enum bptype type_wanted)
13511 {
13512 create_sals_from_location_default (location, canonical, type_wanted);
13513 }
13514
13515 static void
13516 tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13517 struct linespec_result *canonical,
13518 char *cond_string,
13519 char *extra_string,
13520 enum bptype type_wanted,
13521 enum bpdisp disposition,
13522 int thread,
13523 int task, int ignore_count,
13524 const struct breakpoint_ops *ops,
13525 int from_tty, int enabled,
13526 int internal, unsigned flags)
13527 {
13528 create_breakpoints_sal_default (gdbarch, canonical,
13529 cond_string, extra_string,
13530 type_wanted,
13531 disposition, thread, task,
13532 ignore_count, ops, from_tty,
13533 enabled, internal, flags);
13534 }
13535
13536 static void
13537 tracepoint_decode_location (struct breakpoint *b,
13538 const struct event_location *location,
13539 struct program_space *search_pspace,
13540 struct symtabs_and_lines *sals)
13541 {
13542 decode_location_default (b, location, search_pspace, sals);
13543 }
13544
13545 struct breakpoint_ops tracepoint_breakpoint_ops;
13546
13547 /* The breakpoint_ops structure to be use on tracepoints placed in a
13548 static probe. */
13549
13550 static void
13551 tracepoint_probe_create_sals_from_location
13552 (const struct event_location *location,
13553 struct linespec_result *canonical,
13554 enum bptype type_wanted)
13555 {
13556 /* We use the same method for breakpoint on probes. */
13557 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
13558 }
13559
13560 static void
13561 tracepoint_probe_decode_location (struct breakpoint *b,
13562 const struct event_location *location,
13563 struct program_space *search_pspace,
13564 struct symtabs_and_lines *sals)
13565 {
13566 /* We use the same method for breakpoint on probes. */
13567 bkpt_probe_decode_location (b, location, search_pspace, sals);
13568 }
13569
13570 static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13571
13572 /* Dprintf breakpoint_ops methods. */
13573
13574 static void
13575 dprintf_re_set (struct breakpoint *b)
13576 {
13577 breakpoint_re_set_default (b);
13578
13579 /* extra_string should never be non-NULL for dprintf. */
13580 gdb_assert (b->extra_string != NULL);
13581
13582 /* 1 - connect to target 1, that can run breakpoint commands.
13583 2 - create a dprintf, which resolves fine.
13584 3 - disconnect from target 1
13585 4 - connect to target 2, that can NOT run breakpoint commands.
13586
13587 After steps #3/#4, you'll want the dprintf command list to
13588 be updated, because target 1 and 2 may well return different
13589 answers for target_can_run_breakpoint_commands().
13590 Given absence of finer grained resetting, we get to do
13591 it all the time. */
13592 if (b->extra_string != NULL)
13593 update_dprintf_command_list (b);
13594 }
13595
13596 /* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13597
13598 static void
13599 dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13600 {
13601 fprintf_unfiltered (fp, "dprintf %s,%s",
13602 event_location_to_string (tp->location.get ()),
13603 tp->extra_string);
13604 print_recreate_thread (tp, fp);
13605 }
13606
13607 /* Implement the "after_condition_true" breakpoint_ops method for
13608 dprintf.
13609
13610 dprintf's are implemented with regular commands in their command
13611 list, but we run the commands here instead of before presenting the
13612 stop to the user, as dprintf's don't actually cause a stop. This
13613 also makes it so that the commands of multiple dprintfs at the same
13614 address are all handled. */
13615
13616 static void
13617 dprintf_after_condition_true (struct bpstats *bs)
13618 {
13619 struct cleanup *old_chain;
13620 struct bpstats tmp_bs = { NULL };
13621 struct bpstats *tmp_bs_p = &tmp_bs;
13622
13623 /* dprintf's never cause a stop. This wasn't set in the
13624 check_status hook instead because that would make the dprintf's
13625 condition not be evaluated. */
13626 bs->stop = 0;
13627
13628 /* Run the command list here. Take ownership of it instead of
13629 copying. We never want these commands to run later in
13630 bpstat_do_actions, if a breakpoint that causes a stop happens to
13631 be set at same address as this dprintf, or even if running the
13632 commands here throws. */
13633 tmp_bs.commands = bs->commands;
13634 bs->commands = NULL;
13635 old_chain = make_cleanup_decref_counted_command_line (&tmp_bs.commands);
13636
13637 bpstat_do_actions_1 (&tmp_bs_p);
13638
13639 /* 'tmp_bs.commands' will usually be NULL by now, but
13640 bpstat_do_actions_1 may return early without processing the whole
13641 list. */
13642 do_cleanups (old_chain);
13643 }
13644
13645 /* The breakpoint_ops structure to be used on static tracepoints with
13646 markers (`-m'). */
13647
13648 static void
13649 strace_marker_create_sals_from_location (const struct event_location *location,
13650 struct linespec_result *canonical,
13651 enum bptype type_wanted)
13652 {
13653 struct linespec_sals lsal;
13654 const char *arg_start, *arg;
13655 char *str;
13656 struct cleanup *cleanup;
13657
13658 arg = arg_start = get_linespec_location (location);
13659 lsal.sals = decode_static_tracepoint_spec (&arg);
13660
13661 str = savestring (arg_start, arg - arg_start);
13662 cleanup = make_cleanup (xfree, str);
13663 canonical->location = new_linespec_location (&str).release ();
13664 do_cleanups (cleanup);
13665
13666 lsal.canonical = xstrdup (event_location_to_string (canonical->location));
13667 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13668 }
13669
13670 static void
13671 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13672 struct linespec_result *canonical,
13673 char *cond_string,
13674 char *extra_string,
13675 enum bptype type_wanted,
13676 enum bpdisp disposition,
13677 int thread,
13678 int task, int ignore_count,
13679 const struct breakpoint_ops *ops,
13680 int from_tty, int enabled,
13681 int internal, unsigned flags)
13682 {
13683 int i;
13684 struct linespec_sals *lsal = VEC_index (linespec_sals,
13685 canonical->sals, 0);
13686
13687 /* If the user is creating a static tracepoint by marker id
13688 (strace -m MARKER_ID), then store the sals index, so that
13689 breakpoint_re_set can try to match up which of the newly
13690 found markers corresponds to this one, and, don't try to
13691 expand multiple locations for each sal, given than SALS
13692 already should contain all sals for MARKER_ID. */
13693
13694 for (i = 0; i < lsal->sals.nelts; ++i)
13695 {
13696 struct symtabs_and_lines expanded;
13697 struct tracepoint *tp;
13698 event_location_up location;
13699
13700 expanded.nelts = 1;
13701 expanded.sals = &lsal->sals.sals[i];
13702
13703 location = copy_event_location (canonical->location);
13704
13705 tp = new tracepoint ();
13706 init_breakpoint_sal (&tp->base, gdbarch, expanded,
13707 std::move (location), NULL,
13708 cond_string, extra_string,
13709 type_wanted, disposition,
13710 thread, task, ignore_count, ops,
13711 from_tty, enabled, internal, flags,
13712 canonical->special_display);
13713 /* Given that its possible to have multiple markers with
13714 the same string id, if the user is creating a static
13715 tracepoint by marker id ("strace -m MARKER_ID"), then
13716 store the sals index, so that breakpoint_re_set can
13717 try to match up which of the newly found markers
13718 corresponds to this one */
13719 tp->static_trace_marker_id_idx = i;
13720
13721 install_breakpoint (internal, &tp->base, 0);
13722 }
13723 }
13724
13725 static void
13726 strace_marker_decode_location (struct breakpoint *b,
13727 const struct event_location *location,
13728 struct program_space *search_pspace,
13729 struct symtabs_and_lines *sals)
13730 {
13731 struct tracepoint *tp = (struct tracepoint *) b;
13732 const char *s = get_linespec_location (location);
13733
13734 *sals = decode_static_tracepoint_spec (&s);
13735 if (sals->nelts > tp->static_trace_marker_id_idx)
13736 {
13737 sals->sals[0] = sals->sals[tp->static_trace_marker_id_idx];
13738 sals->nelts = 1;
13739 }
13740 else
13741 error (_("marker %s not found"), tp->static_trace_marker_id);
13742 }
13743
13744 static struct breakpoint_ops strace_marker_breakpoint_ops;
13745
13746 static int
13747 strace_marker_p (struct breakpoint *b)
13748 {
13749 return b->ops == &strace_marker_breakpoint_ops;
13750 }
13751
13752 /* Delete a breakpoint and clean up all traces of it in the data
13753 structures. */
13754
13755 void
13756 delete_breakpoint (struct breakpoint *bpt)
13757 {
13758 struct breakpoint *b;
13759
13760 gdb_assert (bpt != NULL);
13761
13762 /* Has this bp already been deleted? This can happen because
13763 multiple lists can hold pointers to bp's. bpstat lists are
13764 especial culprits.
13765
13766 One example of this happening is a watchpoint's scope bp. When
13767 the scope bp triggers, we notice that the watchpoint is out of
13768 scope, and delete it. We also delete its scope bp. But the
13769 scope bp is marked "auto-deleting", and is already on a bpstat.
13770 That bpstat is then checked for auto-deleting bp's, which are
13771 deleted.
13772
13773 A real solution to this problem might involve reference counts in
13774 bp's, and/or giving them pointers back to their referencing
13775 bpstat's, and teaching delete_breakpoint to only free a bp's
13776 storage when no more references were extent. A cheaper bandaid
13777 was chosen. */
13778 if (bpt->type == bp_none)
13779 return;
13780
13781 /* At least avoid this stale reference until the reference counting
13782 of breakpoints gets resolved. */
13783 if (bpt->related_breakpoint != bpt)
13784 {
13785 struct breakpoint *related;
13786 struct watchpoint *w;
13787
13788 if (bpt->type == bp_watchpoint_scope)
13789 w = (struct watchpoint *) bpt->related_breakpoint;
13790 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
13791 w = (struct watchpoint *) bpt;
13792 else
13793 w = NULL;
13794 if (w != NULL)
13795 watchpoint_del_at_next_stop (w);
13796
13797 /* Unlink bpt from the bpt->related_breakpoint ring. */
13798 for (related = bpt; related->related_breakpoint != bpt;
13799 related = related->related_breakpoint);
13800 related->related_breakpoint = bpt->related_breakpoint;
13801 bpt->related_breakpoint = bpt;
13802 }
13803
13804 /* watch_command_1 creates a watchpoint but only sets its number if
13805 update_watchpoint succeeds in creating its bp_locations. If there's
13806 a problem in that process, we'll be asked to delete the half-created
13807 watchpoint. In that case, don't announce the deletion. */
13808 if (bpt->number)
13809 observer_notify_breakpoint_deleted (bpt);
13810
13811 if (breakpoint_chain == bpt)
13812 breakpoint_chain = bpt->next;
13813
13814 ALL_BREAKPOINTS (b)
13815 if (b->next == bpt)
13816 {
13817 b->next = bpt->next;
13818 break;
13819 }
13820
13821 /* Be sure no bpstat's are pointing at the breakpoint after it's
13822 been freed. */
13823 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
13824 in all threads for now. Note that we cannot just remove bpstats
13825 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13826 commands are associated with the bpstat; if we remove it here,
13827 then the later call to bpstat_do_actions (&stop_bpstat); in
13828 event-top.c won't do anything, and temporary breakpoints with
13829 commands won't work. */
13830
13831 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13832
13833 /* Now that breakpoint is removed from breakpoint list, update the
13834 global location list. This will remove locations that used to
13835 belong to this breakpoint. Do this before freeing the breakpoint
13836 itself, since remove_breakpoint looks at location's owner. It
13837 might be better design to have location completely
13838 self-contained, but it's not the case now. */
13839 update_global_location_list (UGLL_DONT_INSERT);
13840
13841 bpt->ops->dtor (bpt);
13842 /* On the chance that someone will soon try again to delete this
13843 same bp, we mark it as deleted before freeing its storage. */
13844 bpt->type = bp_none;
13845 delete bpt;
13846 }
13847
13848 static void
13849 do_delete_breakpoint_cleanup (void *b)
13850 {
13851 delete_breakpoint ((struct breakpoint *) b);
13852 }
13853
13854 struct cleanup *
13855 make_cleanup_delete_breakpoint (struct breakpoint *b)
13856 {
13857 return make_cleanup (do_delete_breakpoint_cleanup, b);
13858 }
13859
13860 /* Iterator function to call a user-provided callback function once
13861 for each of B and its related breakpoints. */
13862
13863 static void
13864 iterate_over_related_breakpoints (struct breakpoint *b,
13865 void (*function) (struct breakpoint *,
13866 void *),
13867 void *data)
13868 {
13869 struct breakpoint *related;
13870
13871 related = b;
13872 do
13873 {
13874 struct breakpoint *next;
13875
13876 /* FUNCTION may delete RELATED. */
13877 next = related->related_breakpoint;
13878
13879 if (next == related)
13880 {
13881 /* RELATED is the last ring entry. */
13882 function (related, data);
13883
13884 /* FUNCTION may have deleted it, so we'd never reach back to
13885 B. There's nothing left to do anyway, so just break
13886 out. */
13887 break;
13888 }
13889 else
13890 function (related, data);
13891
13892 related = next;
13893 }
13894 while (related != b);
13895 }
13896
13897 static void
13898 do_delete_breakpoint (struct breakpoint *b, void *ignore)
13899 {
13900 delete_breakpoint (b);
13901 }
13902
13903 /* A callback for map_breakpoint_numbers that calls
13904 delete_breakpoint. */
13905
13906 static void
13907 do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
13908 {
13909 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
13910 }
13911
13912 void
13913 delete_command (char *arg, int from_tty)
13914 {
13915 struct breakpoint *b, *b_tmp;
13916
13917 dont_repeat ();
13918
13919 if (arg == 0)
13920 {
13921 int breaks_to_delete = 0;
13922
13923 /* Delete all breakpoints if no argument. Do not delete
13924 internal breakpoints, these have to be deleted with an
13925 explicit breakpoint number argument. */
13926 ALL_BREAKPOINTS (b)
13927 if (user_breakpoint_p (b))
13928 {
13929 breaks_to_delete = 1;
13930 break;
13931 }
13932
13933 /* Ask user only if there are some breakpoints to delete. */
13934 if (!from_tty
13935 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
13936 {
13937 ALL_BREAKPOINTS_SAFE (b, b_tmp)
13938 if (user_breakpoint_p (b))
13939 delete_breakpoint (b);
13940 }
13941 }
13942 else
13943 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
13944 }
13945
13946 /* Return true if all locations of B bound to PSPACE are pending. If
13947 PSPACE is NULL, all locations of all program spaces are
13948 considered. */
13949
13950 static int
13951 all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
13952 {
13953 struct bp_location *loc;
13954
13955 for (loc = b->loc; loc != NULL; loc = loc->next)
13956 if ((pspace == NULL
13957 || loc->pspace == pspace)
13958 && !loc->shlib_disabled
13959 && !loc->pspace->executing_startup)
13960 return 0;
13961 return 1;
13962 }
13963
13964 /* Subroutine of update_breakpoint_locations to simplify it.
13965 Return non-zero if multiple fns in list LOC have the same name.
13966 Null names are ignored. */
13967
13968 static int
13969 ambiguous_names_p (struct bp_location *loc)
13970 {
13971 struct bp_location *l;
13972 htab_t htab = htab_create_alloc (13, htab_hash_string,
13973 (int (*) (const void *,
13974 const void *)) streq,
13975 NULL, xcalloc, xfree);
13976
13977 for (l = loc; l != NULL; l = l->next)
13978 {
13979 const char **slot;
13980 const char *name = l->function_name;
13981
13982 /* Allow for some names to be NULL, ignore them. */
13983 if (name == NULL)
13984 continue;
13985
13986 slot = (const char **) htab_find_slot (htab, (const void *) name,
13987 INSERT);
13988 /* NOTE: We can assume slot != NULL here because xcalloc never
13989 returns NULL. */
13990 if (*slot != NULL)
13991 {
13992 htab_delete (htab);
13993 return 1;
13994 }
13995 *slot = name;
13996 }
13997
13998 htab_delete (htab);
13999 return 0;
14000 }
14001
14002 /* When symbols change, it probably means the sources changed as well,
14003 and it might mean the static tracepoint markers are no longer at
14004 the same address or line numbers they used to be at last we
14005 checked. Losing your static tracepoints whenever you rebuild is
14006 undesirable. This function tries to resync/rematch gdb static
14007 tracepoints with the markers on the target, for static tracepoints
14008 that have not been set by marker id. Static tracepoint that have
14009 been set by marker id are reset by marker id in breakpoint_re_set.
14010 The heuristic is:
14011
14012 1) For a tracepoint set at a specific address, look for a marker at
14013 the old PC. If one is found there, assume to be the same marker.
14014 If the name / string id of the marker found is different from the
14015 previous known name, assume that means the user renamed the marker
14016 in the sources, and output a warning.
14017
14018 2) For a tracepoint set at a given line number, look for a marker
14019 at the new address of the old line number. If one is found there,
14020 assume to be the same marker. If the name / string id of the
14021 marker found is different from the previous known name, assume that
14022 means the user renamed the marker in the sources, and output a
14023 warning.
14024
14025 3) If a marker is no longer found at the same address or line, it
14026 may mean the marker no longer exists. But it may also just mean
14027 the code changed a bit. Maybe the user added a few lines of code
14028 that made the marker move up or down (in line number terms). Ask
14029 the target for info about the marker with the string id as we knew
14030 it. If found, update line number and address in the matching
14031 static tracepoint. This will get confused if there's more than one
14032 marker with the same ID (possible in UST, although unadvised
14033 precisely because it confuses tools). */
14034
14035 static struct symtab_and_line
14036 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
14037 {
14038 struct tracepoint *tp = (struct tracepoint *) b;
14039 struct static_tracepoint_marker marker;
14040 CORE_ADDR pc;
14041
14042 pc = sal.pc;
14043 if (sal.line)
14044 find_line_pc (sal.symtab, sal.line, &pc);
14045
14046 if (target_static_tracepoint_marker_at (pc, &marker))
14047 {
14048 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
14049 warning (_("static tracepoint %d changed probed marker from %s to %s"),
14050 b->number,
14051 tp->static_trace_marker_id, marker.str_id);
14052
14053 xfree (tp->static_trace_marker_id);
14054 tp->static_trace_marker_id = xstrdup (marker.str_id);
14055 release_static_tracepoint_marker (&marker);
14056
14057 return sal;
14058 }
14059
14060 /* Old marker wasn't found on target at lineno. Try looking it up
14061 by string ID. */
14062 if (!sal.explicit_pc
14063 && sal.line != 0
14064 && sal.symtab != NULL
14065 && tp->static_trace_marker_id != NULL)
14066 {
14067 VEC(static_tracepoint_marker_p) *markers;
14068
14069 markers
14070 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
14071
14072 if (!VEC_empty(static_tracepoint_marker_p, markers))
14073 {
14074 struct symtab_and_line sal2;
14075 struct symbol *sym;
14076 struct static_tracepoint_marker *tpmarker;
14077 struct ui_out *uiout = current_uiout;
14078 struct explicit_location explicit_loc;
14079
14080 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
14081
14082 xfree (tp->static_trace_marker_id);
14083 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
14084
14085 warning (_("marker for static tracepoint %d (%s) not "
14086 "found at previous line number"),
14087 b->number, tp->static_trace_marker_id);
14088
14089 init_sal (&sal2);
14090
14091 sal2.pc = tpmarker->address;
14092
14093 sal2 = find_pc_line (tpmarker->address, 0);
14094 sym = find_pc_sect_function (tpmarker->address, NULL);
14095 uiout->text ("Now in ");
14096 if (sym)
14097 {
14098 uiout->field_string ("func", SYMBOL_PRINT_NAME (sym));
14099 uiout->text (" at ");
14100 }
14101 uiout->field_string ("file",
14102 symtab_to_filename_for_display (sal2.symtab));
14103 uiout->text (":");
14104
14105 if (uiout->is_mi_like_p ())
14106 {
14107 const char *fullname = symtab_to_fullname (sal2.symtab);
14108
14109 uiout->field_string ("fullname", fullname);
14110 }
14111
14112 uiout->field_int ("line", sal2.line);
14113 uiout->text ("\n");
14114
14115 b->loc->line_number = sal2.line;
14116 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
14117
14118 b->location.reset (NULL);
14119 initialize_explicit_location (&explicit_loc);
14120 explicit_loc.source_filename
14121 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
14122 explicit_loc.line_offset.offset = b->loc->line_number;
14123 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
14124 b->location = new_explicit_location (&explicit_loc);
14125
14126 /* Might be nice to check if function changed, and warn if
14127 so. */
14128
14129 release_static_tracepoint_marker (tpmarker);
14130 }
14131 }
14132 return sal;
14133 }
14134
14135 /* Returns 1 iff locations A and B are sufficiently same that
14136 we don't need to report breakpoint as changed. */
14137
14138 static int
14139 locations_are_equal (struct bp_location *a, struct bp_location *b)
14140 {
14141 while (a && b)
14142 {
14143 if (a->address != b->address)
14144 return 0;
14145
14146 if (a->shlib_disabled != b->shlib_disabled)
14147 return 0;
14148
14149 if (a->enabled != b->enabled)
14150 return 0;
14151
14152 a = a->next;
14153 b = b->next;
14154 }
14155
14156 if ((a == NULL) != (b == NULL))
14157 return 0;
14158
14159 return 1;
14160 }
14161
14162 /* Split all locations of B that are bound to PSPACE out of B's
14163 location list to a separate list and return that list's head. If
14164 PSPACE is NULL, hoist out all locations of B. */
14165
14166 static struct bp_location *
14167 hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
14168 {
14169 struct bp_location head;
14170 struct bp_location *i = b->loc;
14171 struct bp_location **i_link = &b->loc;
14172 struct bp_location *hoisted = &head;
14173
14174 if (pspace == NULL)
14175 {
14176 i = b->loc;
14177 b->loc = NULL;
14178 return i;
14179 }
14180
14181 head.next = NULL;
14182
14183 while (i != NULL)
14184 {
14185 if (i->pspace == pspace)
14186 {
14187 *i_link = i->next;
14188 i->next = NULL;
14189 hoisted->next = i;
14190 hoisted = i;
14191 }
14192 else
14193 i_link = &i->next;
14194 i = *i_link;
14195 }
14196
14197 return head.next;
14198 }
14199
14200 /* Create new breakpoint locations for B (a hardware or software
14201 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
14202 zero, then B is a ranged breakpoint. Only recreates locations for
14203 FILTER_PSPACE. Locations of other program spaces are left
14204 untouched. */
14205
14206 void
14207 update_breakpoint_locations (struct breakpoint *b,
14208 struct program_space *filter_pspace,
14209 struct symtabs_and_lines sals,
14210 struct symtabs_and_lines sals_end)
14211 {
14212 int i;
14213 struct bp_location *existing_locations;
14214
14215 if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
14216 {
14217 /* Ranged breakpoints have only one start location and one end
14218 location. */
14219 b->enable_state = bp_disabled;
14220 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
14221 "multiple locations found\n"),
14222 b->number);
14223 return;
14224 }
14225
14226 /* If there's no new locations, and all existing locations are
14227 pending, don't do anything. This optimizes the common case where
14228 all locations are in the same shared library, that was unloaded.
14229 We'd like to retain the location, so that when the library is
14230 loaded again, we don't loose the enabled/disabled status of the
14231 individual locations. */
14232 if (all_locations_are_pending (b, filter_pspace) && sals.nelts == 0)
14233 return;
14234
14235 existing_locations = hoist_existing_locations (b, filter_pspace);
14236
14237 for (i = 0; i < sals.nelts; ++i)
14238 {
14239 struct bp_location *new_loc;
14240
14241 switch_to_program_space_and_thread (sals.sals[i].pspace);
14242
14243 new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
14244
14245 /* Reparse conditions, they might contain references to the
14246 old symtab. */
14247 if (b->cond_string != NULL)
14248 {
14249 const char *s;
14250
14251 s = b->cond_string;
14252 TRY
14253 {
14254 new_loc->cond = parse_exp_1 (&s, sals.sals[i].pc,
14255 block_for_pc (sals.sals[i].pc),
14256 0);
14257 }
14258 CATCH (e, RETURN_MASK_ERROR)
14259 {
14260 warning (_("failed to reevaluate condition "
14261 "for breakpoint %d: %s"),
14262 b->number, e.message);
14263 new_loc->enabled = 0;
14264 }
14265 END_CATCH
14266 }
14267
14268 if (sals_end.nelts)
14269 {
14270 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
14271
14272 new_loc->length = end - sals.sals[0].pc + 1;
14273 }
14274 }
14275
14276 /* If possible, carry over 'disable' status from existing
14277 breakpoints. */
14278 {
14279 struct bp_location *e = existing_locations;
14280 /* If there are multiple breakpoints with the same function name,
14281 e.g. for inline functions, comparing function names won't work.
14282 Instead compare pc addresses; this is just a heuristic as things
14283 may have moved, but in practice it gives the correct answer
14284 often enough until a better solution is found. */
14285 int have_ambiguous_names = ambiguous_names_p (b->loc);
14286
14287 for (; e; e = e->next)
14288 {
14289 if (!e->enabled && e->function_name)
14290 {
14291 struct bp_location *l = b->loc;
14292 if (have_ambiguous_names)
14293 {
14294 for (; l; l = l->next)
14295 if (breakpoint_locations_match (e, l))
14296 {
14297 l->enabled = 0;
14298 break;
14299 }
14300 }
14301 else
14302 {
14303 for (; l; l = l->next)
14304 if (l->function_name
14305 && strcmp (e->function_name, l->function_name) == 0)
14306 {
14307 l->enabled = 0;
14308 break;
14309 }
14310 }
14311 }
14312 }
14313 }
14314
14315 if (!locations_are_equal (existing_locations, b->loc))
14316 observer_notify_breakpoint_modified (b);
14317 }
14318
14319 /* Find the SaL locations corresponding to the given LOCATION.
14320 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
14321
14322 static struct symtabs_and_lines
14323 location_to_sals (struct breakpoint *b, struct event_location *location,
14324 struct program_space *search_pspace, int *found)
14325 {
14326 struct symtabs_and_lines sals = {0};
14327 struct gdb_exception exception = exception_none;
14328
14329 gdb_assert (b->ops != NULL);
14330
14331 TRY
14332 {
14333 b->ops->decode_location (b, location, search_pspace, &sals);
14334 }
14335 CATCH (e, RETURN_MASK_ERROR)
14336 {
14337 int not_found_and_ok = 0;
14338
14339 exception = e;
14340
14341 /* For pending breakpoints, it's expected that parsing will
14342 fail until the right shared library is loaded. User has
14343 already told to create pending breakpoints and don't need
14344 extra messages. If breakpoint is in bp_shlib_disabled
14345 state, then user already saw the message about that
14346 breakpoint being disabled, and don't want to see more
14347 errors. */
14348 if (e.error == NOT_FOUND_ERROR
14349 && (b->condition_not_parsed
14350 || (b->loc != NULL
14351 && search_pspace != NULL
14352 && b->loc->pspace != search_pspace)
14353 || (b->loc && b->loc->shlib_disabled)
14354 || (b->loc && b->loc->pspace->executing_startup)
14355 || b->enable_state == bp_disabled))
14356 not_found_and_ok = 1;
14357
14358 if (!not_found_and_ok)
14359 {
14360 /* We surely don't want to warn about the same breakpoint
14361 10 times. One solution, implemented here, is disable
14362 the breakpoint on error. Another solution would be to
14363 have separate 'warning emitted' flag. Since this
14364 happens only when a binary has changed, I don't know
14365 which approach is better. */
14366 b->enable_state = bp_disabled;
14367 throw_exception (e);
14368 }
14369 }
14370 END_CATCH
14371
14372 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
14373 {
14374 int i;
14375
14376 for (i = 0; i < sals.nelts; ++i)
14377 resolve_sal_pc (&sals.sals[i]);
14378 if (b->condition_not_parsed && b->extra_string != NULL)
14379 {
14380 char *cond_string, *extra_string;
14381 int thread, task;
14382
14383 find_condition_and_thread (b->extra_string, sals.sals[0].pc,
14384 &cond_string, &thread, &task,
14385 &extra_string);
14386 gdb_assert (b->cond_string == NULL);
14387 if (cond_string)
14388 b->cond_string = cond_string;
14389 b->thread = thread;
14390 b->task = task;
14391 if (extra_string)
14392 {
14393 xfree (b->extra_string);
14394 b->extra_string = extra_string;
14395 }
14396 b->condition_not_parsed = 0;
14397 }
14398
14399 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
14400 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
14401
14402 *found = 1;
14403 }
14404 else
14405 *found = 0;
14406
14407 return sals;
14408 }
14409
14410 /* The default re_set method, for typical hardware or software
14411 breakpoints. Reevaluate the breakpoint and recreate its
14412 locations. */
14413
14414 static void
14415 breakpoint_re_set_default (struct breakpoint *b)
14416 {
14417 int found;
14418 struct symtabs_and_lines sals, sals_end;
14419 struct symtabs_and_lines expanded = {0};
14420 struct symtabs_and_lines expanded_end = {0};
14421 struct program_space *filter_pspace = current_program_space;
14422
14423 sals = location_to_sals (b, b->location.get (), filter_pspace, &found);
14424 if (found)
14425 {
14426 make_cleanup (xfree, sals.sals);
14427 expanded = sals;
14428 }
14429
14430 if (b->location_range_end != NULL)
14431 {
14432 sals_end = location_to_sals (b, b->location_range_end.get (),
14433 filter_pspace, &found);
14434 if (found)
14435 {
14436 make_cleanup (xfree, sals_end.sals);
14437 expanded_end = sals_end;
14438 }
14439 }
14440
14441 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
14442 }
14443
14444 /* Default method for creating SALs from an address string. It basically
14445 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
14446
14447 static void
14448 create_sals_from_location_default (const struct event_location *location,
14449 struct linespec_result *canonical,
14450 enum bptype type_wanted)
14451 {
14452 parse_breakpoint_sals (location, canonical);
14453 }
14454
14455 /* Call create_breakpoints_sal for the given arguments. This is the default
14456 function for the `create_breakpoints_sal' method of
14457 breakpoint_ops. */
14458
14459 static void
14460 create_breakpoints_sal_default (struct gdbarch *gdbarch,
14461 struct linespec_result *canonical,
14462 char *cond_string,
14463 char *extra_string,
14464 enum bptype type_wanted,
14465 enum bpdisp disposition,
14466 int thread,
14467 int task, int ignore_count,
14468 const struct breakpoint_ops *ops,
14469 int from_tty, int enabled,
14470 int internal, unsigned flags)
14471 {
14472 create_breakpoints_sal (gdbarch, canonical, cond_string,
14473 extra_string,
14474 type_wanted, disposition,
14475 thread, task, ignore_count, ops, from_tty,
14476 enabled, internal, flags);
14477 }
14478
14479 /* Decode the line represented by S by calling decode_line_full. This is the
14480 default function for the `decode_location' method of breakpoint_ops. */
14481
14482 static void
14483 decode_location_default (struct breakpoint *b,
14484 const struct event_location *location,
14485 struct program_space *search_pspace,
14486 struct symtabs_and_lines *sals)
14487 {
14488 struct linespec_result canonical;
14489
14490 init_linespec_result (&canonical);
14491 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
14492 (struct symtab *) NULL, 0,
14493 &canonical, multiple_symbols_all,
14494 b->filter);
14495
14496 /* We should get 0 or 1 resulting SALs. */
14497 gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
14498
14499 if (VEC_length (linespec_sals, canonical.sals) > 0)
14500 {
14501 struct linespec_sals *lsal;
14502
14503 lsal = VEC_index (linespec_sals, canonical.sals, 0);
14504 *sals = lsal->sals;
14505 /* Arrange it so the destructor does not free the
14506 contents. */
14507 lsal->sals.sals = NULL;
14508 }
14509
14510 destroy_linespec_result (&canonical);
14511 }
14512
14513 /* Prepare the global context for a re-set of breakpoint B. */
14514
14515 static struct cleanup *
14516 prepare_re_set_context (struct breakpoint *b)
14517 {
14518 input_radix = b->input_radix;
14519 set_language (b->language);
14520
14521 return make_cleanup (null_cleanup, NULL);
14522 }
14523
14524 /* Reset a breakpoint given it's struct breakpoint * BINT.
14525 The value we return ends up being the return value from catch_errors.
14526 Unused in this case. */
14527
14528 static int
14529 breakpoint_re_set_one (void *bint)
14530 {
14531 /* Get past catch_errs. */
14532 struct breakpoint *b = (struct breakpoint *) bint;
14533 struct cleanup *cleanups;
14534
14535 cleanups = prepare_re_set_context (b);
14536 b->ops->re_set (b);
14537 do_cleanups (cleanups);
14538 return 0;
14539 }
14540
14541 /* Re-set breakpoint locations for the current program space.
14542 Locations bound to other program spaces are left untouched. */
14543
14544 void
14545 breakpoint_re_set (void)
14546 {
14547 struct breakpoint *b, *b_tmp;
14548 enum language save_language;
14549 int save_input_radix;
14550 struct cleanup *old_chain;
14551
14552 save_language = current_language->la_language;
14553 save_input_radix = input_radix;
14554 old_chain = save_current_space_and_thread ();
14555
14556 /* Note: we must not try to insert locations until after all
14557 breakpoints have been re-set. Otherwise, e.g., when re-setting
14558 breakpoint 1, we'd insert the locations of breakpoint 2, which
14559 hadn't been re-set yet, and thus may have stale locations. */
14560
14561 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14562 {
14563 /* Format possible error msg. */
14564 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
14565 b->number);
14566 struct cleanup *cleanups = make_cleanup (xfree, message);
14567 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
14568 do_cleanups (cleanups);
14569 }
14570 set_language (save_language);
14571 input_radix = save_input_radix;
14572
14573 jit_breakpoint_re_set ();
14574
14575 do_cleanups (old_chain);
14576
14577 create_overlay_event_breakpoint ();
14578 create_longjmp_master_breakpoint ();
14579 create_std_terminate_master_breakpoint ();
14580 create_exception_master_breakpoint ();
14581
14582 /* Now we can insert. */
14583 update_global_location_list (UGLL_MAY_INSERT);
14584 }
14585 \f
14586 /* Reset the thread number of this breakpoint:
14587
14588 - If the breakpoint is for all threads, leave it as-is.
14589 - Else, reset it to the current thread for inferior_ptid. */
14590 void
14591 breakpoint_re_set_thread (struct breakpoint *b)
14592 {
14593 if (b->thread != -1)
14594 {
14595 if (in_thread_list (inferior_ptid))
14596 b->thread = ptid_to_global_thread_id (inferior_ptid);
14597
14598 /* We're being called after following a fork. The new fork is
14599 selected as current, and unless this was a vfork will have a
14600 different program space from the original thread. Reset that
14601 as well. */
14602 b->loc->pspace = current_program_space;
14603 }
14604 }
14605
14606 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14607 If from_tty is nonzero, it prints a message to that effect,
14608 which ends with a period (no newline). */
14609
14610 void
14611 set_ignore_count (int bptnum, int count, int from_tty)
14612 {
14613 struct breakpoint *b;
14614
14615 if (count < 0)
14616 count = 0;
14617
14618 ALL_BREAKPOINTS (b)
14619 if (b->number == bptnum)
14620 {
14621 if (is_tracepoint (b))
14622 {
14623 if (from_tty && count != 0)
14624 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14625 bptnum);
14626 return;
14627 }
14628
14629 b->ignore_count = count;
14630 if (from_tty)
14631 {
14632 if (count == 0)
14633 printf_filtered (_("Will stop next time "
14634 "breakpoint %d is reached."),
14635 bptnum);
14636 else if (count == 1)
14637 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
14638 bptnum);
14639 else
14640 printf_filtered (_("Will ignore next %d "
14641 "crossings of breakpoint %d."),
14642 count, bptnum);
14643 }
14644 observer_notify_breakpoint_modified (b);
14645 return;
14646 }
14647
14648 error (_("No breakpoint number %d."), bptnum);
14649 }
14650
14651 /* Command to set ignore-count of breakpoint N to COUNT. */
14652
14653 static void
14654 ignore_command (char *args, int from_tty)
14655 {
14656 char *p = args;
14657 int num;
14658
14659 if (p == 0)
14660 error_no_arg (_("a breakpoint number"));
14661
14662 num = get_number (&p);
14663 if (num == 0)
14664 error (_("bad breakpoint number: '%s'"), args);
14665 if (*p == 0)
14666 error (_("Second argument (specified ignore-count) is missing."));
14667
14668 set_ignore_count (num,
14669 longest_to_int (value_as_long (parse_and_eval (p))),
14670 from_tty);
14671 if (from_tty)
14672 printf_filtered ("\n");
14673 }
14674 \f
14675 /* Call FUNCTION on each of the breakpoints
14676 whose numbers are given in ARGS. */
14677
14678 static void
14679 map_breakpoint_numbers (const char *args,
14680 void (*function) (struct breakpoint *,
14681 void *),
14682 void *data)
14683 {
14684 int num;
14685 struct breakpoint *b, *tmp;
14686
14687 if (args == 0 || *args == '\0')
14688 error_no_arg (_("one or more breakpoint numbers"));
14689
14690 number_or_range_parser parser (args);
14691
14692 while (!parser.finished ())
14693 {
14694 const char *p = parser.cur_tok ();
14695 bool match = false;
14696
14697 num = parser.get_number ();
14698 if (num == 0)
14699 {
14700 warning (_("bad breakpoint number at or near '%s'"), p);
14701 }
14702 else
14703 {
14704 ALL_BREAKPOINTS_SAFE (b, tmp)
14705 if (b->number == num)
14706 {
14707 match = true;
14708 function (b, data);
14709 break;
14710 }
14711 if (!match)
14712 printf_unfiltered (_("No breakpoint number %d.\n"), num);
14713 }
14714 }
14715 }
14716
14717 static struct bp_location *
14718 find_location_by_number (char *number)
14719 {
14720 char *dot = strchr (number, '.');
14721 char *p1;
14722 int bp_num;
14723 int loc_num;
14724 struct breakpoint *b;
14725 struct bp_location *loc;
14726
14727 *dot = '\0';
14728
14729 p1 = number;
14730 bp_num = get_number (&p1);
14731 if (bp_num == 0)
14732 error (_("Bad breakpoint number '%s'"), number);
14733
14734 ALL_BREAKPOINTS (b)
14735 if (b->number == bp_num)
14736 {
14737 break;
14738 }
14739
14740 if (!b || b->number != bp_num)
14741 error (_("Bad breakpoint number '%s'"), number);
14742
14743 p1 = dot+1;
14744 loc_num = get_number (&p1);
14745 if (loc_num == 0)
14746 error (_("Bad breakpoint location number '%s'"), number);
14747
14748 --loc_num;
14749 loc = b->loc;
14750 for (;loc_num && loc; --loc_num, loc = loc->next)
14751 ;
14752 if (!loc)
14753 error (_("Bad breakpoint location number '%s'"), dot+1);
14754
14755 return loc;
14756 }
14757
14758
14759 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14760 If from_tty is nonzero, it prints a message to that effect,
14761 which ends with a period (no newline). */
14762
14763 void
14764 disable_breakpoint (struct breakpoint *bpt)
14765 {
14766 /* Never disable a watchpoint scope breakpoint; we want to
14767 hit them when we leave scope so we can delete both the
14768 watchpoint and its scope breakpoint at that time. */
14769 if (bpt->type == bp_watchpoint_scope)
14770 return;
14771
14772 bpt->enable_state = bp_disabled;
14773
14774 /* Mark breakpoint locations modified. */
14775 mark_breakpoint_modified (bpt);
14776
14777 if (target_supports_enable_disable_tracepoint ()
14778 && current_trace_status ()->running && is_tracepoint (bpt))
14779 {
14780 struct bp_location *location;
14781
14782 for (location = bpt->loc; location; location = location->next)
14783 target_disable_tracepoint (location);
14784 }
14785
14786 update_global_location_list (UGLL_DONT_INSERT);
14787
14788 observer_notify_breakpoint_modified (bpt);
14789 }
14790
14791 /* A callback for iterate_over_related_breakpoints. */
14792
14793 static void
14794 do_disable_breakpoint (struct breakpoint *b, void *ignore)
14795 {
14796 disable_breakpoint (b);
14797 }
14798
14799 /* A callback for map_breakpoint_numbers that calls
14800 disable_breakpoint. */
14801
14802 static void
14803 do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
14804 {
14805 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
14806 }
14807
14808 static void
14809 disable_command (char *args, int from_tty)
14810 {
14811 if (args == 0)
14812 {
14813 struct breakpoint *bpt;
14814
14815 ALL_BREAKPOINTS (bpt)
14816 if (user_breakpoint_p (bpt))
14817 disable_breakpoint (bpt);
14818 }
14819 else
14820 {
14821 char *num = extract_arg (&args);
14822
14823 while (num)
14824 {
14825 if (strchr (num, '.'))
14826 {
14827 struct bp_location *loc = find_location_by_number (num);
14828
14829 if (loc)
14830 {
14831 if (loc->enabled)
14832 {
14833 loc->enabled = 0;
14834 mark_breakpoint_location_modified (loc);
14835 }
14836 if (target_supports_enable_disable_tracepoint ()
14837 && current_trace_status ()->running && loc->owner
14838 && is_tracepoint (loc->owner))
14839 target_disable_tracepoint (loc);
14840 }
14841 update_global_location_list (UGLL_DONT_INSERT);
14842 }
14843 else
14844 map_breakpoint_numbers (num, do_map_disable_breakpoint, NULL);
14845 num = extract_arg (&args);
14846 }
14847 }
14848 }
14849
14850 static void
14851 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14852 int count)
14853 {
14854 int target_resources_ok;
14855
14856 if (bpt->type == bp_hardware_breakpoint)
14857 {
14858 int i;
14859 i = hw_breakpoint_used_count ();
14860 target_resources_ok =
14861 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
14862 i + 1, 0);
14863 if (target_resources_ok == 0)
14864 error (_("No hardware breakpoint support in the target."));
14865 else if (target_resources_ok < 0)
14866 error (_("Hardware breakpoints used exceeds limit."));
14867 }
14868
14869 if (is_watchpoint (bpt))
14870 {
14871 /* Initialize it just to avoid a GCC false warning. */
14872 enum enable_state orig_enable_state = bp_disabled;
14873
14874 TRY
14875 {
14876 struct watchpoint *w = (struct watchpoint *) bpt;
14877
14878 orig_enable_state = bpt->enable_state;
14879 bpt->enable_state = bp_enabled;
14880 update_watchpoint (w, 1 /* reparse */);
14881 }
14882 CATCH (e, RETURN_MASK_ALL)
14883 {
14884 bpt->enable_state = orig_enable_state;
14885 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14886 bpt->number);
14887 return;
14888 }
14889 END_CATCH
14890 }
14891
14892 bpt->enable_state = bp_enabled;
14893
14894 /* Mark breakpoint locations modified. */
14895 mark_breakpoint_modified (bpt);
14896
14897 if (target_supports_enable_disable_tracepoint ()
14898 && current_trace_status ()->running && is_tracepoint (bpt))
14899 {
14900 struct bp_location *location;
14901
14902 for (location = bpt->loc; location; location = location->next)
14903 target_enable_tracepoint (location);
14904 }
14905
14906 bpt->disposition = disposition;
14907 bpt->enable_count = count;
14908 update_global_location_list (UGLL_MAY_INSERT);
14909
14910 observer_notify_breakpoint_modified (bpt);
14911 }
14912
14913
14914 void
14915 enable_breakpoint (struct breakpoint *bpt)
14916 {
14917 enable_breakpoint_disp (bpt, bpt->disposition, 0);
14918 }
14919
14920 static void
14921 do_enable_breakpoint (struct breakpoint *bpt, void *arg)
14922 {
14923 enable_breakpoint (bpt);
14924 }
14925
14926 /* A callback for map_breakpoint_numbers that calls
14927 enable_breakpoint. */
14928
14929 static void
14930 do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
14931 {
14932 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
14933 }
14934
14935 /* The enable command enables the specified breakpoints (or all defined
14936 breakpoints) so they once again become (or continue to be) effective
14937 in stopping the inferior. */
14938
14939 static void
14940 enable_command (char *args, int from_tty)
14941 {
14942 if (args == 0)
14943 {
14944 struct breakpoint *bpt;
14945
14946 ALL_BREAKPOINTS (bpt)
14947 if (user_breakpoint_p (bpt))
14948 enable_breakpoint (bpt);
14949 }
14950 else
14951 {
14952 char *num = extract_arg (&args);
14953
14954 while (num)
14955 {
14956 if (strchr (num, '.'))
14957 {
14958 struct bp_location *loc = find_location_by_number (num);
14959
14960 if (loc)
14961 {
14962 if (!loc->enabled)
14963 {
14964 loc->enabled = 1;
14965 mark_breakpoint_location_modified (loc);
14966 }
14967 if (target_supports_enable_disable_tracepoint ()
14968 && current_trace_status ()->running && loc->owner
14969 && is_tracepoint (loc->owner))
14970 target_enable_tracepoint (loc);
14971 }
14972 update_global_location_list (UGLL_MAY_INSERT);
14973 }
14974 else
14975 map_breakpoint_numbers (num, do_map_enable_breakpoint, NULL);
14976 num = extract_arg (&args);
14977 }
14978 }
14979 }
14980
14981 /* This struct packages up disposition data for application to multiple
14982 breakpoints. */
14983
14984 struct disp_data
14985 {
14986 enum bpdisp disp;
14987 int count;
14988 };
14989
14990 static void
14991 do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
14992 {
14993 struct disp_data disp_data = *(struct disp_data *) arg;
14994
14995 enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
14996 }
14997
14998 static void
14999 do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
15000 {
15001 struct disp_data disp = { disp_disable, 1 };
15002
15003 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
15004 }
15005
15006 static void
15007 enable_once_command (char *args, int from_tty)
15008 {
15009 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
15010 }
15011
15012 static void
15013 do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
15014 {
15015 struct disp_data disp = { disp_disable, *(int *) countptr };
15016
15017 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
15018 }
15019
15020 static void
15021 enable_count_command (char *args, int from_tty)
15022 {
15023 int count;
15024
15025 if (args == NULL)
15026 error_no_arg (_("hit count"));
15027
15028 count = get_number (&args);
15029
15030 map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
15031 }
15032
15033 static void
15034 do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
15035 {
15036 struct disp_data disp = { disp_del, 1 };
15037
15038 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
15039 }
15040
15041 static void
15042 enable_delete_command (char *args, int from_tty)
15043 {
15044 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
15045 }
15046 \f
15047 static void
15048 set_breakpoint_cmd (char *args, int from_tty)
15049 {
15050 }
15051
15052 static void
15053 show_breakpoint_cmd (char *args, int from_tty)
15054 {
15055 }
15056
15057 /* Invalidate last known value of any hardware watchpoint if
15058 the memory which that value represents has been written to by
15059 GDB itself. */
15060
15061 static void
15062 invalidate_bp_value_on_memory_change (struct inferior *inferior,
15063 CORE_ADDR addr, ssize_t len,
15064 const bfd_byte *data)
15065 {
15066 struct breakpoint *bp;
15067
15068 ALL_BREAKPOINTS (bp)
15069 if (bp->enable_state == bp_enabled
15070 && bp->type == bp_hardware_watchpoint)
15071 {
15072 struct watchpoint *wp = (struct watchpoint *) bp;
15073
15074 if (wp->val_valid && wp->val)
15075 {
15076 struct bp_location *loc;
15077
15078 for (loc = bp->loc; loc != NULL; loc = loc->next)
15079 if (loc->loc_type == bp_loc_hardware_watchpoint
15080 && loc->address + loc->length > addr
15081 && addr + len > loc->address)
15082 {
15083 value_free (wp->val);
15084 wp->val = NULL;
15085 wp->val_valid = 0;
15086 }
15087 }
15088 }
15089 }
15090
15091 /* Create and insert a breakpoint for software single step. */
15092
15093 void
15094 insert_single_step_breakpoint (struct gdbarch *gdbarch,
15095 struct address_space *aspace,
15096 CORE_ADDR next_pc)
15097 {
15098 struct thread_info *tp = inferior_thread ();
15099 struct symtab_and_line sal;
15100 CORE_ADDR pc = next_pc;
15101
15102 if (tp->control.single_step_breakpoints == NULL)
15103 {
15104 tp->control.single_step_breakpoints
15105 = new_single_step_breakpoint (tp->global_num, gdbarch);
15106 }
15107
15108 sal = find_pc_line (pc, 0);
15109 sal.pc = pc;
15110 sal.section = find_pc_overlay (pc);
15111 sal.explicit_pc = 1;
15112 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
15113
15114 update_global_location_list (UGLL_INSERT);
15115 }
15116
15117 /* Insert single step breakpoints according to the current state. */
15118
15119 int
15120 insert_single_step_breakpoints (struct gdbarch *gdbarch)
15121 {
15122 struct regcache *regcache = get_current_regcache ();
15123 VEC (CORE_ADDR) * next_pcs;
15124
15125 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
15126
15127 if (next_pcs != NULL)
15128 {
15129 int i;
15130 CORE_ADDR pc;
15131 struct frame_info *frame = get_current_frame ();
15132 struct address_space *aspace = get_frame_address_space (frame);
15133
15134 for (i = 0; VEC_iterate (CORE_ADDR, next_pcs, i, pc); i++)
15135 insert_single_step_breakpoint (gdbarch, aspace, pc);
15136
15137 VEC_free (CORE_ADDR, next_pcs);
15138
15139 return 1;
15140 }
15141 else
15142 return 0;
15143 }
15144
15145 /* See breakpoint.h. */
15146
15147 int
15148 breakpoint_has_location_inserted_here (struct breakpoint *bp,
15149 struct address_space *aspace,
15150 CORE_ADDR pc)
15151 {
15152 struct bp_location *loc;
15153
15154 for (loc = bp->loc; loc != NULL; loc = loc->next)
15155 if (loc->inserted
15156 && breakpoint_location_address_match (loc, aspace, pc))
15157 return 1;
15158
15159 return 0;
15160 }
15161
15162 /* Check whether a software single-step breakpoint is inserted at
15163 PC. */
15164
15165 int
15166 single_step_breakpoint_inserted_here_p (struct address_space *aspace,
15167 CORE_ADDR pc)
15168 {
15169 struct breakpoint *bpt;
15170
15171 ALL_BREAKPOINTS (bpt)
15172 {
15173 if (bpt->type == bp_single_step
15174 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
15175 return 1;
15176 }
15177 return 0;
15178 }
15179
15180 /* Tracepoint-specific operations. */
15181
15182 /* Set tracepoint count to NUM. */
15183 static void
15184 set_tracepoint_count (int num)
15185 {
15186 tracepoint_count = num;
15187 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
15188 }
15189
15190 static void
15191 trace_command (char *arg, int from_tty)
15192 {
15193 struct breakpoint_ops *ops;
15194
15195 event_location_up location = string_to_event_location (&arg,
15196 current_language);
15197 if (location != NULL
15198 && event_location_type (location.get ()) == PROBE_LOCATION)
15199 ops = &tracepoint_probe_breakpoint_ops;
15200 else
15201 ops = &tracepoint_breakpoint_ops;
15202
15203 create_breakpoint (get_current_arch (),
15204 location.get (),
15205 NULL, 0, arg, 1 /* parse arg */,
15206 0 /* tempflag */,
15207 bp_tracepoint /* type_wanted */,
15208 0 /* Ignore count */,
15209 pending_break_support,
15210 ops,
15211 from_tty,
15212 1 /* enabled */,
15213 0 /* internal */, 0);
15214 }
15215
15216 static void
15217 ftrace_command (char *arg, int from_tty)
15218 {
15219 event_location_up location = string_to_event_location (&arg,
15220 current_language);
15221 create_breakpoint (get_current_arch (),
15222 location.get (),
15223 NULL, 0, arg, 1 /* parse arg */,
15224 0 /* tempflag */,
15225 bp_fast_tracepoint /* type_wanted */,
15226 0 /* Ignore count */,
15227 pending_break_support,
15228 &tracepoint_breakpoint_ops,
15229 from_tty,
15230 1 /* enabled */,
15231 0 /* internal */, 0);
15232 }
15233
15234 /* strace command implementation. Creates a static tracepoint. */
15235
15236 static void
15237 strace_command (char *arg, int from_tty)
15238 {
15239 struct breakpoint_ops *ops;
15240 event_location_up location;
15241 struct cleanup *back_to;
15242
15243 /* Decide if we are dealing with a static tracepoint marker (`-m'),
15244 or with a normal static tracepoint. */
15245 if (arg && startswith (arg, "-m") && isspace (arg[2]))
15246 {
15247 ops = &strace_marker_breakpoint_ops;
15248 location = new_linespec_location (&arg);
15249 }
15250 else
15251 {
15252 ops = &tracepoint_breakpoint_ops;
15253 location = string_to_event_location (&arg, current_language);
15254 }
15255
15256 create_breakpoint (get_current_arch (),
15257 location.get (),
15258 NULL, 0, arg, 1 /* parse arg */,
15259 0 /* tempflag */,
15260 bp_static_tracepoint /* type_wanted */,
15261 0 /* Ignore count */,
15262 pending_break_support,
15263 ops,
15264 from_tty,
15265 1 /* enabled */,
15266 0 /* internal */, 0);
15267 }
15268
15269 /* Set up a fake reader function that gets command lines from a linked
15270 list that was acquired during tracepoint uploading. */
15271
15272 static struct uploaded_tp *this_utp;
15273 static int next_cmd;
15274
15275 static char *
15276 read_uploaded_action (void)
15277 {
15278 char *rslt;
15279
15280 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
15281
15282 next_cmd++;
15283
15284 return rslt;
15285 }
15286
15287 /* Given information about a tracepoint as recorded on a target (which
15288 can be either a live system or a trace file), attempt to create an
15289 equivalent GDB tracepoint. This is not a reliable process, since
15290 the target does not necessarily have all the information used when
15291 the tracepoint was originally defined. */
15292
15293 struct tracepoint *
15294 create_tracepoint_from_upload (struct uploaded_tp *utp)
15295 {
15296 char *addr_str, small_buf[100];
15297 struct tracepoint *tp;
15298
15299 if (utp->at_string)
15300 addr_str = utp->at_string;
15301 else
15302 {
15303 /* In the absence of a source location, fall back to raw
15304 address. Since there is no way to confirm that the address
15305 means the same thing as when the trace was started, warn the
15306 user. */
15307 warning (_("Uploaded tracepoint %d has no "
15308 "source location, using raw address"),
15309 utp->number);
15310 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
15311 addr_str = small_buf;
15312 }
15313
15314 /* There's not much we can do with a sequence of bytecodes. */
15315 if (utp->cond && !utp->cond_string)
15316 warning (_("Uploaded tracepoint %d condition "
15317 "has no source form, ignoring it"),
15318 utp->number);
15319
15320 event_location_up location = string_to_event_location (&addr_str,
15321 current_language);
15322 if (!create_breakpoint (get_current_arch (),
15323 location.get (),
15324 utp->cond_string, -1, addr_str,
15325 0 /* parse cond/thread */,
15326 0 /* tempflag */,
15327 utp->type /* type_wanted */,
15328 0 /* Ignore count */,
15329 pending_break_support,
15330 &tracepoint_breakpoint_ops,
15331 0 /* from_tty */,
15332 utp->enabled /* enabled */,
15333 0 /* internal */,
15334 CREATE_BREAKPOINT_FLAGS_INSERTED))
15335 return NULL;
15336
15337 /* Get the tracepoint we just created. */
15338 tp = get_tracepoint (tracepoint_count);
15339 gdb_assert (tp != NULL);
15340
15341 if (utp->pass > 0)
15342 {
15343 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
15344 tp->base.number);
15345
15346 trace_pass_command (small_buf, 0);
15347 }
15348
15349 /* If we have uploaded versions of the original commands, set up a
15350 special-purpose "reader" function and call the usual command line
15351 reader, then pass the result to the breakpoint command-setting
15352 function. */
15353 if (!VEC_empty (char_ptr, utp->cmd_strings))
15354 {
15355 command_line_up cmd_list;
15356
15357 this_utp = utp;
15358 next_cmd = 0;
15359
15360 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
15361
15362 breakpoint_set_commands (&tp->base, std::move (cmd_list));
15363 }
15364 else if (!VEC_empty (char_ptr, utp->actions)
15365 || !VEC_empty (char_ptr, utp->step_actions))
15366 warning (_("Uploaded tracepoint %d actions "
15367 "have no source form, ignoring them"),
15368 utp->number);
15369
15370 /* Copy any status information that might be available. */
15371 tp->base.hit_count = utp->hit_count;
15372 tp->traceframe_usage = utp->traceframe_usage;
15373
15374 return tp;
15375 }
15376
15377 /* Print information on tracepoint number TPNUM_EXP, or all if
15378 omitted. */
15379
15380 static void
15381 tracepoints_info (char *args, int from_tty)
15382 {
15383 struct ui_out *uiout = current_uiout;
15384 int num_printed;
15385
15386 num_printed = breakpoint_1 (args, 0, is_tracepoint);
15387
15388 if (num_printed == 0)
15389 {
15390 if (args == NULL || *args == '\0')
15391 uiout->message ("No tracepoints.\n");
15392 else
15393 uiout->message ("No tracepoint matching '%s'.\n", args);
15394 }
15395
15396 default_collect_info ();
15397 }
15398
15399 /* The 'enable trace' command enables tracepoints.
15400 Not supported by all targets. */
15401 static void
15402 enable_trace_command (char *args, int from_tty)
15403 {
15404 enable_command (args, from_tty);
15405 }
15406
15407 /* The 'disable trace' command disables tracepoints.
15408 Not supported by all targets. */
15409 static void
15410 disable_trace_command (char *args, int from_tty)
15411 {
15412 disable_command (args, from_tty);
15413 }
15414
15415 /* Remove a tracepoint (or all if no argument). */
15416 static void
15417 delete_trace_command (char *arg, int from_tty)
15418 {
15419 struct breakpoint *b, *b_tmp;
15420
15421 dont_repeat ();
15422
15423 if (arg == 0)
15424 {
15425 int breaks_to_delete = 0;
15426
15427 /* Delete all breakpoints if no argument.
15428 Do not delete internal or call-dummy breakpoints, these
15429 have to be deleted with an explicit breakpoint number
15430 argument. */
15431 ALL_TRACEPOINTS (b)
15432 if (is_tracepoint (b) && user_breakpoint_p (b))
15433 {
15434 breaks_to_delete = 1;
15435 break;
15436 }
15437
15438 /* Ask user only if there are some breakpoints to delete. */
15439 if (!from_tty
15440 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
15441 {
15442 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15443 if (is_tracepoint (b) && user_breakpoint_p (b))
15444 delete_breakpoint (b);
15445 }
15446 }
15447 else
15448 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
15449 }
15450
15451 /* Helper function for trace_pass_command. */
15452
15453 static void
15454 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
15455 {
15456 tp->pass_count = count;
15457 observer_notify_breakpoint_modified (&tp->base);
15458 if (from_tty)
15459 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
15460 tp->base.number, count);
15461 }
15462
15463 /* Set passcount for tracepoint.
15464
15465 First command argument is passcount, second is tracepoint number.
15466 If tracepoint number omitted, apply to most recently defined.
15467 Also accepts special argument "all". */
15468
15469 static void
15470 trace_pass_command (char *args, int from_tty)
15471 {
15472 struct tracepoint *t1;
15473 unsigned int count;
15474
15475 if (args == 0 || *args == 0)
15476 error (_("passcount command requires an "
15477 "argument (count + optional TP num)"));
15478
15479 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
15480
15481 args = skip_spaces (args);
15482 if (*args && strncasecmp (args, "all", 3) == 0)
15483 {
15484 struct breakpoint *b;
15485
15486 args += 3; /* Skip special argument "all". */
15487 if (*args)
15488 error (_("Junk at end of arguments."));
15489
15490 ALL_TRACEPOINTS (b)
15491 {
15492 t1 = (struct tracepoint *) b;
15493 trace_pass_set_count (t1, count, from_tty);
15494 }
15495 }
15496 else if (*args == '\0')
15497 {
15498 t1 = get_tracepoint_by_number (&args, NULL);
15499 if (t1)
15500 trace_pass_set_count (t1, count, from_tty);
15501 }
15502 else
15503 {
15504 number_or_range_parser parser (args);
15505 while (!parser.finished ())
15506 {
15507 t1 = get_tracepoint_by_number (&args, &parser);
15508 if (t1)
15509 trace_pass_set_count (t1, count, from_tty);
15510 }
15511 }
15512 }
15513
15514 struct tracepoint *
15515 get_tracepoint (int num)
15516 {
15517 struct breakpoint *t;
15518
15519 ALL_TRACEPOINTS (t)
15520 if (t->number == num)
15521 return (struct tracepoint *) t;
15522
15523 return NULL;
15524 }
15525
15526 /* Find the tracepoint with the given target-side number (which may be
15527 different from the tracepoint number after disconnecting and
15528 reconnecting). */
15529
15530 struct tracepoint *
15531 get_tracepoint_by_number_on_target (int num)
15532 {
15533 struct breakpoint *b;
15534
15535 ALL_TRACEPOINTS (b)
15536 {
15537 struct tracepoint *t = (struct tracepoint *) b;
15538
15539 if (t->number_on_target == num)
15540 return t;
15541 }
15542
15543 return NULL;
15544 }
15545
15546 /* Utility: parse a tracepoint number and look it up in the list.
15547 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
15548 If the argument is missing, the most recent tracepoint
15549 (tracepoint_count) is returned. */
15550
15551 struct tracepoint *
15552 get_tracepoint_by_number (char **arg,
15553 number_or_range_parser *parser)
15554 {
15555 struct breakpoint *t;
15556 int tpnum;
15557 char *instring = arg == NULL ? NULL : *arg;
15558
15559 if (parser != NULL)
15560 {
15561 gdb_assert (!parser->finished ());
15562 tpnum = parser->get_number ();
15563 }
15564 else if (arg == NULL || *arg == NULL || ! **arg)
15565 tpnum = tracepoint_count;
15566 else
15567 tpnum = get_number (arg);
15568
15569 if (tpnum <= 0)
15570 {
15571 if (instring && *instring)
15572 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15573 instring);
15574 else
15575 printf_filtered (_("No previous tracepoint\n"));
15576 return NULL;
15577 }
15578
15579 ALL_TRACEPOINTS (t)
15580 if (t->number == tpnum)
15581 {
15582 return (struct tracepoint *) t;
15583 }
15584
15585 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15586 return NULL;
15587 }
15588
15589 void
15590 print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15591 {
15592 if (b->thread != -1)
15593 fprintf_unfiltered (fp, " thread %d", b->thread);
15594
15595 if (b->task != 0)
15596 fprintf_unfiltered (fp, " task %d", b->task);
15597
15598 fprintf_unfiltered (fp, "\n");
15599 }
15600
15601 /* Save information on user settable breakpoints (watchpoints, etc) to
15602 a new script file named FILENAME. If FILTER is non-NULL, call it
15603 on each breakpoint and only include the ones for which it returns
15604 non-zero. */
15605
15606 static void
15607 save_breakpoints (char *filename, int from_tty,
15608 int (*filter) (const struct breakpoint *))
15609 {
15610 struct breakpoint *tp;
15611 int any = 0;
15612 struct cleanup *cleanup;
15613 int extra_trace_bits = 0;
15614
15615 if (filename == 0 || *filename == 0)
15616 error (_("Argument required (file name in which to save)"));
15617
15618 /* See if we have anything to save. */
15619 ALL_BREAKPOINTS (tp)
15620 {
15621 /* Skip internal and momentary breakpoints. */
15622 if (!user_breakpoint_p (tp))
15623 continue;
15624
15625 /* If we have a filter, only save the breakpoints it accepts. */
15626 if (filter && !filter (tp))
15627 continue;
15628
15629 any = 1;
15630
15631 if (is_tracepoint (tp))
15632 {
15633 extra_trace_bits = 1;
15634
15635 /* We can stop searching. */
15636 break;
15637 }
15638 }
15639
15640 if (!any)
15641 {
15642 warning (_("Nothing to save."));
15643 return;
15644 }
15645
15646 filename = tilde_expand (filename);
15647 cleanup = make_cleanup (xfree, filename);
15648
15649 stdio_file fp;
15650
15651 if (!fp.open (filename, "w"))
15652 error (_("Unable to open file '%s' for saving (%s)"),
15653 filename, safe_strerror (errno));
15654
15655 if (extra_trace_bits)
15656 save_trace_state_variables (&fp);
15657
15658 ALL_BREAKPOINTS (tp)
15659 {
15660 /* Skip internal and momentary breakpoints. */
15661 if (!user_breakpoint_p (tp))
15662 continue;
15663
15664 /* If we have a filter, only save the breakpoints it accepts. */
15665 if (filter && !filter (tp))
15666 continue;
15667
15668 tp->ops->print_recreate (tp, &fp);
15669
15670 /* Note, we can't rely on tp->number for anything, as we can't
15671 assume the recreated breakpoint numbers will match. Use $bpnum
15672 instead. */
15673
15674 if (tp->cond_string)
15675 fp.printf (" condition $bpnum %s\n", tp->cond_string);
15676
15677 if (tp->ignore_count)
15678 fp.printf (" ignore $bpnum %d\n", tp->ignore_count);
15679
15680 if (tp->type != bp_dprintf && tp->commands)
15681 {
15682 fp.puts (" commands\n");
15683
15684 current_uiout->redirect (&fp);
15685 TRY
15686 {
15687 print_command_lines (current_uiout, tp->commands->commands, 2);
15688 }
15689 CATCH (ex, RETURN_MASK_ALL)
15690 {
15691 current_uiout->redirect (NULL);
15692 throw_exception (ex);
15693 }
15694 END_CATCH
15695
15696 current_uiout->redirect (NULL);
15697 fp.puts (" end\n");
15698 }
15699
15700 if (tp->enable_state == bp_disabled)
15701 fp.puts ("disable $bpnum\n");
15702
15703 /* If this is a multi-location breakpoint, check if the locations
15704 should be individually disabled. Watchpoint locations are
15705 special, and not user visible. */
15706 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15707 {
15708 struct bp_location *loc;
15709 int n = 1;
15710
15711 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15712 if (!loc->enabled)
15713 fp.printf ("disable $bpnum.%d\n", n);
15714 }
15715 }
15716
15717 if (extra_trace_bits && *default_collect)
15718 fp.printf ("set default-collect %s\n", default_collect);
15719
15720 if (from_tty)
15721 printf_filtered (_("Saved to file '%s'.\n"), filename);
15722 do_cleanups (cleanup);
15723 }
15724
15725 /* The `save breakpoints' command. */
15726
15727 static void
15728 save_breakpoints_command (char *args, int from_tty)
15729 {
15730 save_breakpoints (args, from_tty, NULL);
15731 }
15732
15733 /* The `save tracepoints' command. */
15734
15735 static void
15736 save_tracepoints_command (char *args, int from_tty)
15737 {
15738 save_breakpoints (args, from_tty, is_tracepoint);
15739 }
15740
15741 /* Create a vector of all tracepoints. */
15742
15743 VEC(breakpoint_p) *
15744 all_tracepoints (void)
15745 {
15746 VEC(breakpoint_p) *tp_vec = 0;
15747 struct breakpoint *tp;
15748
15749 ALL_TRACEPOINTS (tp)
15750 {
15751 VEC_safe_push (breakpoint_p, tp_vec, tp);
15752 }
15753
15754 return tp_vec;
15755 }
15756
15757 \f
15758 /* This help string is used to consolidate all the help string for specifying
15759 locations used by several commands. */
15760
15761 #define LOCATION_HELP_STRING \
15762 "Linespecs are colon-separated lists of location parameters, such as\n\
15763 source filename, function name, label name, and line number.\n\
15764 Example: To specify the start of a label named \"the_top\" in the\n\
15765 function \"fact\" in the file \"factorial.c\", use\n\
15766 \"factorial.c:fact:the_top\".\n\
15767 \n\
15768 Address locations begin with \"*\" and specify an exact address in the\n\
15769 program. Example: To specify the fourth byte past the start function\n\
15770 \"main\", use \"*main + 4\".\n\
15771 \n\
15772 Explicit locations are similar to linespecs but use an option/argument\n\
15773 syntax to specify location parameters.\n\
15774 Example: To specify the start of the label named \"the_top\" in the\n\
15775 function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
15776 -function fact -label the_top\".\n"
15777
15778 /* This help string is used for the break, hbreak, tbreak and thbreak
15779 commands. It is defined as a macro to prevent duplication.
15780 COMMAND should be a string constant containing the name of the
15781 command. */
15782
15783 #define BREAK_ARGS_HELP(command) \
15784 command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15785 PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15786 probe point. Accepted values are `-probe' (for a generic, automatically\n\
15787 guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15788 `-probe-dtrace' (for a DTrace probe).\n\
15789 LOCATION may be a linespec, address, or explicit location as described\n\
15790 below.\n\
15791 \n\
15792 With no LOCATION, uses current execution address of the selected\n\
15793 stack frame. This is useful for breaking on return to a stack frame.\n\
15794 \n\
15795 THREADNUM is the number from \"info threads\".\n\
15796 CONDITION is a boolean expression.\n\
15797 \n" LOCATION_HELP_STRING "\n\
15798 Multiple breakpoints at one place are permitted, and useful if their\n\
15799 conditions are different.\n\
15800 \n\
15801 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15802
15803 /* List of subcommands for "catch". */
15804 static struct cmd_list_element *catch_cmdlist;
15805
15806 /* List of subcommands for "tcatch". */
15807 static struct cmd_list_element *tcatch_cmdlist;
15808
15809 void
15810 add_catch_command (const char *name, const char *docstring,
15811 cmd_sfunc_ftype *sfunc,
15812 completer_ftype *completer,
15813 void *user_data_catch,
15814 void *user_data_tcatch)
15815 {
15816 struct cmd_list_element *command;
15817
15818 command = add_cmd (name, class_breakpoint, NULL, docstring,
15819 &catch_cmdlist);
15820 set_cmd_sfunc (command, sfunc);
15821 set_cmd_context (command, user_data_catch);
15822 set_cmd_completer (command, completer);
15823
15824 command = add_cmd (name, class_breakpoint, NULL, docstring,
15825 &tcatch_cmdlist);
15826 set_cmd_sfunc (command, sfunc);
15827 set_cmd_context (command, user_data_tcatch);
15828 set_cmd_completer (command, completer);
15829 }
15830
15831 static void
15832 save_command (char *arg, int from_tty)
15833 {
15834 printf_unfiltered (_("\"save\" must be followed by "
15835 "the name of a save subcommand.\n"));
15836 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
15837 }
15838
15839 struct breakpoint *
15840 iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15841 void *data)
15842 {
15843 struct breakpoint *b, *b_tmp;
15844
15845 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15846 {
15847 if ((*callback) (b, data))
15848 return b;
15849 }
15850
15851 return NULL;
15852 }
15853
15854 /* Zero if any of the breakpoint's locations could be a location where
15855 functions have been inlined, nonzero otherwise. */
15856
15857 static int
15858 is_non_inline_function (struct breakpoint *b)
15859 {
15860 /* The shared library event breakpoint is set on the address of a
15861 non-inline function. */
15862 if (b->type == bp_shlib_event)
15863 return 1;
15864
15865 return 0;
15866 }
15867
15868 /* Nonzero if the specified PC cannot be a location where functions
15869 have been inlined. */
15870
15871 int
15872 pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
15873 const struct target_waitstatus *ws)
15874 {
15875 struct breakpoint *b;
15876 struct bp_location *bl;
15877
15878 ALL_BREAKPOINTS (b)
15879 {
15880 if (!is_non_inline_function (b))
15881 continue;
15882
15883 for (bl = b->loc; bl != NULL; bl = bl->next)
15884 {
15885 if (!bl->shlib_disabled
15886 && bpstat_check_location (bl, aspace, pc, ws))
15887 return 1;
15888 }
15889 }
15890
15891 return 0;
15892 }
15893
15894 /* Remove any references to OBJFILE which is going to be freed. */
15895
15896 void
15897 breakpoint_free_objfile (struct objfile *objfile)
15898 {
15899 struct bp_location **locp, *loc;
15900
15901 ALL_BP_LOCATIONS (loc, locp)
15902 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
15903 loc->symtab = NULL;
15904 }
15905
15906 void
15907 initialize_breakpoint_ops (void)
15908 {
15909 static int initialized = 0;
15910
15911 struct breakpoint_ops *ops;
15912
15913 if (initialized)
15914 return;
15915 initialized = 1;
15916
15917 /* The breakpoint_ops structure to be inherit by all kinds of
15918 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15919 internal and momentary breakpoints, etc.). */
15920 ops = &bkpt_base_breakpoint_ops;
15921 *ops = base_breakpoint_ops;
15922 ops->re_set = bkpt_re_set;
15923 ops->insert_location = bkpt_insert_location;
15924 ops->remove_location = bkpt_remove_location;
15925 ops->breakpoint_hit = bkpt_breakpoint_hit;
15926 ops->create_sals_from_location = bkpt_create_sals_from_location;
15927 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
15928 ops->decode_location = bkpt_decode_location;
15929
15930 /* The breakpoint_ops structure to be used in regular breakpoints. */
15931 ops = &bkpt_breakpoint_ops;
15932 *ops = bkpt_base_breakpoint_ops;
15933 ops->re_set = bkpt_re_set;
15934 ops->resources_needed = bkpt_resources_needed;
15935 ops->print_it = bkpt_print_it;
15936 ops->print_mention = bkpt_print_mention;
15937 ops->print_recreate = bkpt_print_recreate;
15938
15939 /* Ranged breakpoints. */
15940 ops = &ranged_breakpoint_ops;
15941 *ops = bkpt_breakpoint_ops;
15942 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15943 ops->resources_needed = resources_needed_ranged_breakpoint;
15944 ops->print_it = print_it_ranged_breakpoint;
15945 ops->print_one = print_one_ranged_breakpoint;
15946 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15947 ops->print_mention = print_mention_ranged_breakpoint;
15948 ops->print_recreate = print_recreate_ranged_breakpoint;
15949
15950 /* Internal breakpoints. */
15951 ops = &internal_breakpoint_ops;
15952 *ops = bkpt_base_breakpoint_ops;
15953 ops->re_set = internal_bkpt_re_set;
15954 ops->check_status = internal_bkpt_check_status;
15955 ops->print_it = internal_bkpt_print_it;
15956 ops->print_mention = internal_bkpt_print_mention;
15957
15958 /* Momentary breakpoints. */
15959 ops = &momentary_breakpoint_ops;
15960 *ops = bkpt_base_breakpoint_ops;
15961 ops->re_set = momentary_bkpt_re_set;
15962 ops->check_status = momentary_bkpt_check_status;
15963 ops->print_it = momentary_bkpt_print_it;
15964 ops->print_mention = momentary_bkpt_print_mention;
15965
15966 /* Momentary breakpoints for bp_longjmp and bp_exception. */
15967 ops = &longjmp_breakpoint_ops;
15968 *ops = momentary_breakpoint_ops;
15969 ops->dtor = longjmp_bkpt_dtor;
15970
15971 /* Probe breakpoints. */
15972 ops = &bkpt_probe_breakpoint_ops;
15973 *ops = bkpt_breakpoint_ops;
15974 ops->insert_location = bkpt_probe_insert_location;
15975 ops->remove_location = bkpt_probe_remove_location;
15976 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
15977 ops->decode_location = bkpt_probe_decode_location;
15978
15979 /* Watchpoints. */
15980 ops = &watchpoint_breakpoint_ops;
15981 *ops = base_breakpoint_ops;
15982 ops->dtor = dtor_watchpoint;
15983 ops->re_set = re_set_watchpoint;
15984 ops->insert_location = insert_watchpoint;
15985 ops->remove_location = remove_watchpoint;
15986 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15987 ops->check_status = check_status_watchpoint;
15988 ops->resources_needed = resources_needed_watchpoint;
15989 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15990 ops->print_it = print_it_watchpoint;
15991 ops->print_mention = print_mention_watchpoint;
15992 ops->print_recreate = print_recreate_watchpoint;
15993 ops->explains_signal = explains_signal_watchpoint;
15994
15995 /* Masked watchpoints. */
15996 ops = &masked_watchpoint_breakpoint_ops;
15997 *ops = watchpoint_breakpoint_ops;
15998 ops->insert_location = insert_masked_watchpoint;
15999 ops->remove_location = remove_masked_watchpoint;
16000 ops->resources_needed = resources_needed_masked_watchpoint;
16001 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
16002 ops->print_it = print_it_masked_watchpoint;
16003 ops->print_one_detail = print_one_detail_masked_watchpoint;
16004 ops->print_mention = print_mention_masked_watchpoint;
16005 ops->print_recreate = print_recreate_masked_watchpoint;
16006
16007 /* Tracepoints. */
16008 ops = &tracepoint_breakpoint_ops;
16009 *ops = base_breakpoint_ops;
16010 ops->re_set = tracepoint_re_set;
16011 ops->breakpoint_hit = tracepoint_breakpoint_hit;
16012 ops->print_one_detail = tracepoint_print_one_detail;
16013 ops->print_mention = tracepoint_print_mention;
16014 ops->print_recreate = tracepoint_print_recreate;
16015 ops->create_sals_from_location = tracepoint_create_sals_from_location;
16016 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
16017 ops->decode_location = tracepoint_decode_location;
16018
16019 /* Probe tracepoints. */
16020 ops = &tracepoint_probe_breakpoint_ops;
16021 *ops = tracepoint_breakpoint_ops;
16022 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
16023 ops->decode_location = tracepoint_probe_decode_location;
16024
16025 /* Static tracepoints with marker (`-m'). */
16026 ops = &strace_marker_breakpoint_ops;
16027 *ops = tracepoint_breakpoint_ops;
16028 ops->create_sals_from_location = strace_marker_create_sals_from_location;
16029 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
16030 ops->decode_location = strace_marker_decode_location;
16031
16032 /* Fork catchpoints. */
16033 ops = &catch_fork_breakpoint_ops;
16034 *ops = base_breakpoint_ops;
16035 ops->insert_location = insert_catch_fork;
16036 ops->remove_location = remove_catch_fork;
16037 ops->breakpoint_hit = breakpoint_hit_catch_fork;
16038 ops->print_it = print_it_catch_fork;
16039 ops->print_one = print_one_catch_fork;
16040 ops->print_mention = print_mention_catch_fork;
16041 ops->print_recreate = print_recreate_catch_fork;
16042
16043 /* Vfork catchpoints. */
16044 ops = &catch_vfork_breakpoint_ops;
16045 *ops = base_breakpoint_ops;
16046 ops->insert_location = insert_catch_vfork;
16047 ops->remove_location = remove_catch_vfork;
16048 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
16049 ops->print_it = print_it_catch_vfork;
16050 ops->print_one = print_one_catch_vfork;
16051 ops->print_mention = print_mention_catch_vfork;
16052 ops->print_recreate = print_recreate_catch_vfork;
16053
16054 /* Exec catchpoints. */
16055 ops = &catch_exec_breakpoint_ops;
16056 *ops = base_breakpoint_ops;
16057 ops->dtor = dtor_catch_exec;
16058 ops->insert_location = insert_catch_exec;
16059 ops->remove_location = remove_catch_exec;
16060 ops->breakpoint_hit = breakpoint_hit_catch_exec;
16061 ops->print_it = print_it_catch_exec;
16062 ops->print_one = print_one_catch_exec;
16063 ops->print_mention = print_mention_catch_exec;
16064 ops->print_recreate = print_recreate_catch_exec;
16065
16066 /* Solib-related catchpoints. */
16067 ops = &catch_solib_breakpoint_ops;
16068 *ops = base_breakpoint_ops;
16069 ops->dtor = dtor_catch_solib;
16070 ops->insert_location = insert_catch_solib;
16071 ops->remove_location = remove_catch_solib;
16072 ops->breakpoint_hit = breakpoint_hit_catch_solib;
16073 ops->check_status = check_status_catch_solib;
16074 ops->print_it = print_it_catch_solib;
16075 ops->print_one = print_one_catch_solib;
16076 ops->print_mention = print_mention_catch_solib;
16077 ops->print_recreate = print_recreate_catch_solib;
16078
16079 ops = &dprintf_breakpoint_ops;
16080 *ops = bkpt_base_breakpoint_ops;
16081 ops->re_set = dprintf_re_set;
16082 ops->resources_needed = bkpt_resources_needed;
16083 ops->print_it = bkpt_print_it;
16084 ops->print_mention = bkpt_print_mention;
16085 ops->print_recreate = dprintf_print_recreate;
16086 ops->after_condition_true = dprintf_after_condition_true;
16087 ops->breakpoint_hit = dprintf_breakpoint_hit;
16088 }
16089
16090 /* Chain containing all defined "enable breakpoint" subcommands. */
16091
16092 static struct cmd_list_element *enablebreaklist = NULL;
16093
16094 void
16095 _initialize_breakpoint (void)
16096 {
16097 struct cmd_list_element *c;
16098
16099 initialize_breakpoint_ops ();
16100
16101 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
16102 observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
16103 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
16104
16105 breakpoint_objfile_key
16106 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
16107
16108 breakpoint_chain = 0;
16109 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
16110 before a breakpoint is set. */
16111 breakpoint_count = 0;
16112
16113 tracepoint_count = 0;
16114
16115 add_com ("ignore", class_breakpoint, ignore_command, _("\
16116 Set ignore-count of breakpoint number N to COUNT.\n\
16117 Usage is `ignore N COUNT'."));
16118
16119 add_com ("commands", class_breakpoint, commands_command, _("\
16120 Set commands to be executed when the given breakpoints are hit.\n\
16121 Give a space-separated breakpoint list as argument after \"commands\".\n\
16122 A list element can be a breakpoint number (e.g. `5') or a range of numbers\n\
16123 (e.g. `5-7').\n\
16124 With no argument, the targeted breakpoint is the last one set.\n\
16125 The commands themselves follow starting on the next line.\n\
16126 Type a line containing \"end\" to indicate the end of them.\n\
16127 Give \"silent\" as the first line to make the breakpoint silent;\n\
16128 then no output is printed when it is hit, except what the commands print."));
16129
16130 c = add_com ("condition", class_breakpoint, condition_command, _("\
16131 Specify breakpoint number N to break only if COND is true.\n\
16132 Usage is `condition N COND', where N is an integer and COND is an\n\
16133 expression to be evaluated whenever breakpoint N is reached."));
16134 set_cmd_completer (c, condition_completer);
16135
16136 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
16137 Set a temporary breakpoint.\n\
16138 Like \"break\" except the breakpoint is only temporary,\n\
16139 so it will be deleted when hit. Equivalent to \"break\" followed\n\
16140 by using \"enable delete\" on the breakpoint number.\n\
16141 \n"
16142 BREAK_ARGS_HELP ("tbreak")));
16143 set_cmd_completer (c, location_completer);
16144
16145 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
16146 Set a hardware assisted breakpoint.\n\
16147 Like \"break\" except the breakpoint requires hardware support,\n\
16148 some target hardware may not have this support.\n\
16149 \n"
16150 BREAK_ARGS_HELP ("hbreak")));
16151 set_cmd_completer (c, location_completer);
16152
16153 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
16154 Set a temporary hardware assisted breakpoint.\n\
16155 Like \"hbreak\" except the breakpoint is only temporary,\n\
16156 so it will be deleted when hit.\n\
16157 \n"
16158 BREAK_ARGS_HELP ("thbreak")));
16159 set_cmd_completer (c, location_completer);
16160
16161 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
16162 Enable some breakpoints.\n\
16163 Give breakpoint numbers (separated by spaces) as arguments.\n\
16164 With no subcommand, breakpoints are enabled until you command otherwise.\n\
16165 This is used to cancel the effect of the \"disable\" command.\n\
16166 With a subcommand you can enable temporarily."),
16167 &enablelist, "enable ", 1, &cmdlist);
16168
16169 add_com_alias ("en", "enable", class_breakpoint, 1);
16170
16171 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
16172 Enable some breakpoints.\n\
16173 Give breakpoint numbers (separated by spaces) as arguments.\n\
16174 This is used to cancel the effect of the \"disable\" command.\n\
16175 May be abbreviated to simply \"enable\".\n"),
16176 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
16177
16178 add_cmd ("once", no_class, enable_once_command, _("\
16179 Enable breakpoints for one hit. Give breakpoint numbers.\n\
16180 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
16181 &enablebreaklist);
16182
16183 add_cmd ("delete", no_class, enable_delete_command, _("\
16184 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16185 If a breakpoint is hit while enabled in this fashion, it is deleted."),
16186 &enablebreaklist);
16187
16188 add_cmd ("count", no_class, enable_count_command, _("\
16189 Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16190 If a breakpoint is hit while enabled in this fashion,\n\
16191 the count is decremented; when it reaches zero, the breakpoint is disabled."),
16192 &enablebreaklist);
16193
16194 add_cmd ("delete", no_class, enable_delete_command, _("\
16195 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16196 If a breakpoint is hit while enabled in this fashion, it is deleted."),
16197 &enablelist);
16198
16199 add_cmd ("once", no_class, enable_once_command, _("\
16200 Enable breakpoints for one hit. Give breakpoint numbers.\n\
16201 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
16202 &enablelist);
16203
16204 add_cmd ("count", no_class, enable_count_command, _("\
16205 Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16206 If a breakpoint is hit while enabled in this fashion,\n\
16207 the count is decremented; when it reaches zero, the breakpoint is disabled."),
16208 &enablelist);
16209
16210 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
16211 Disable some breakpoints.\n\
16212 Arguments are breakpoint numbers with spaces in between.\n\
16213 To disable all breakpoints, give no argument.\n\
16214 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
16215 &disablelist, "disable ", 1, &cmdlist);
16216 add_com_alias ("dis", "disable", class_breakpoint, 1);
16217 add_com_alias ("disa", "disable", class_breakpoint, 1);
16218
16219 add_cmd ("breakpoints", class_alias, disable_command, _("\
16220 Disable some breakpoints.\n\
16221 Arguments are breakpoint numbers with spaces in between.\n\
16222 To disable all breakpoints, give no argument.\n\
16223 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
16224 This command may be abbreviated \"disable\"."),
16225 &disablelist);
16226
16227 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
16228 Delete some breakpoints or auto-display expressions.\n\
16229 Arguments are breakpoint numbers with spaces in between.\n\
16230 To delete all breakpoints, give no argument.\n\
16231 \n\
16232 Also a prefix command for deletion of other GDB objects.\n\
16233 The \"unset\" command is also an alias for \"delete\"."),
16234 &deletelist, "delete ", 1, &cmdlist);
16235 add_com_alias ("d", "delete", class_breakpoint, 1);
16236 add_com_alias ("del", "delete", class_breakpoint, 1);
16237
16238 add_cmd ("breakpoints", class_alias, delete_command, _("\
16239 Delete some breakpoints or auto-display expressions.\n\
16240 Arguments are breakpoint numbers with spaces in between.\n\
16241 To delete all breakpoints, give no argument.\n\
16242 This command may be abbreviated \"delete\"."),
16243 &deletelist);
16244
16245 add_com ("clear", class_breakpoint, clear_command, _("\
16246 Clear breakpoint at specified location.\n\
16247 Argument may be a linespec, explicit, or address location as described below.\n\
16248 \n\
16249 With no argument, clears all breakpoints in the line that the selected frame\n\
16250 is executing in.\n"
16251 "\n" LOCATION_HELP_STRING "\n\
16252 See also the \"delete\" command which clears breakpoints by number."));
16253 add_com_alias ("cl", "clear", class_breakpoint, 1);
16254
16255 c = add_com ("break", class_breakpoint, break_command, _("\
16256 Set breakpoint at specified location.\n"
16257 BREAK_ARGS_HELP ("break")));
16258 set_cmd_completer (c, location_completer);
16259
16260 add_com_alias ("b", "break", class_run, 1);
16261 add_com_alias ("br", "break", class_run, 1);
16262 add_com_alias ("bre", "break", class_run, 1);
16263 add_com_alias ("brea", "break", class_run, 1);
16264
16265 if (dbx_commands)
16266 {
16267 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
16268 Break in function/address or break at a line in the current file."),
16269 &stoplist, "stop ", 1, &cmdlist);
16270 add_cmd ("in", class_breakpoint, stopin_command,
16271 _("Break in function or address."), &stoplist);
16272 add_cmd ("at", class_breakpoint, stopat_command,
16273 _("Break at a line in the current file."), &stoplist);
16274 add_com ("status", class_info, breakpoints_info, _("\
16275 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
16276 The \"Type\" column indicates one of:\n\
16277 \tbreakpoint - normal breakpoint\n\
16278 \twatchpoint - watchpoint\n\
16279 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16280 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16281 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16282 address and file/line number respectively.\n\
16283 \n\
16284 Convenience variable \"$_\" and default examine address for \"x\"\n\
16285 are set to the address of the last breakpoint listed unless the command\n\
16286 is prefixed with \"server \".\n\n\
16287 Convenience variable \"$bpnum\" contains the number of the last\n\
16288 breakpoint set."));
16289 }
16290
16291 add_info ("breakpoints", breakpoints_info, _("\
16292 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
16293 The \"Type\" column indicates one of:\n\
16294 \tbreakpoint - normal breakpoint\n\
16295 \twatchpoint - watchpoint\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
16307 add_info_alias ("b", "breakpoints", 1);
16308
16309 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
16310 Status of all breakpoints, or breakpoint number NUMBER.\n\
16311 The \"Type\" column indicates one of:\n\
16312 \tbreakpoint - normal breakpoint\n\
16313 \twatchpoint - watchpoint\n\
16314 \tlongjmp - internal breakpoint used to step through longjmp()\n\
16315 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
16316 \tuntil - internal breakpoint used by the \"until\" command\n\
16317 \tfinish - internal breakpoint used by the \"finish\" command\n\
16318 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16319 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16320 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16321 address and file/line number respectively.\n\
16322 \n\
16323 Convenience variable \"$_\" and default examine address for \"x\"\n\
16324 are set to the address of the last breakpoint listed unless the command\n\
16325 is prefixed with \"server \".\n\n\
16326 Convenience variable \"$bpnum\" contains the number of the last\n\
16327 breakpoint set."),
16328 &maintenanceinfolist);
16329
16330 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
16331 Set catchpoints to catch events."),
16332 &catch_cmdlist, "catch ",
16333 0/*allow-unknown*/, &cmdlist);
16334
16335 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
16336 Set temporary catchpoints to catch events."),
16337 &tcatch_cmdlist, "tcatch ",
16338 0/*allow-unknown*/, &cmdlist);
16339
16340 add_catch_command ("fork", _("Catch calls to fork."),
16341 catch_fork_command_1,
16342 NULL,
16343 (void *) (uintptr_t) catch_fork_permanent,
16344 (void *) (uintptr_t) catch_fork_temporary);
16345 add_catch_command ("vfork", _("Catch calls to vfork."),
16346 catch_fork_command_1,
16347 NULL,
16348 (void *) (uintptr_t) catch_vfork_permanent,
16349 (void *) (uintptr_t) catch_vfork_temporary);
16350 add_catch_command ("exec", _("Catch calls to exec."),
16351 catch_exec_command_1,
16352 NULL,
16353 CATCH_PERMANENT,
16354 CATCH_TEMPORARY);
16355 add_catch_command ("load", _("Catch loads of shared libraries.\n\
16356 Usage: catch load [REGEX]\n\
16357 If REGEX is given, only stop for libraries matching the regular expression."),
16358 catch_load_command_1,
16359 NULL,
16360 CATCH_PERMANENT,
16361 CATCH_TEMPORARY);
16362 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
16363 Usage: catch unload [REGEX]\n\
16364 If REGEX is given, only stop for libraries matching the regular expression."),
16365 catch_unload_command_1,
16366 NULL,
16367 CATCH_PERMANENT,
16368 CATCH_TEMPORARY);
16369
16370 c = add_com ("watch", class_breakpoint, watch_command, _("\
16371 Set a watchpoint for an expression.\n\
16372 Usage: watch [-l|-location] EXPRESSION\n\
16373 A watchpoint stops execution of your program whenever the value of\n\
16374 an expression changes.\n\
16375 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16376 the memory to which it refers."));
16377 set_cmd_completer (c, expression_completer);
16378
16379 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
16380 Set a read watchpoint for an expression.\n\
16381 Usage: rwatch [-l|-location] EXPRESSION\n\
16382 A watchpoint stops execution of your program whenever the value of\n\
16383 an expression is read.\n\
16384 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16385 the memory to which it refers."));
16386 set_cmd_completer (c, expression_completer);
16387
16388 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
16389 Set a watchpoint for an expression.\n\
16390 Usage: awatch [-l|-location] EXPRESSION\n\
16391 A watchpoint stops execution of your program whenever the value of\n\
16392 an expression is either read or written.\n\
16393 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16394 the memory to which it refers."));
16395 set_cmd_completer (c, expression_completer);
16396
16397 add_info ("watchpoints", watchpoints_info, _("\
16398 Status of specified watchpoints (all watchpoints if no argument)."));
16399
16400 /* XXX: cagney/2005-02-23: This should be a boolean, and should
16401 respond to changes - contrary to the description. */
16402 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
16403 &can_use_hw_watchpoints, _("\
16404 Set debugger's willingness to use watchpoint hardware."), _("\
16405 Show debugger's willingness to use watchpoint hardware."), _("\
16406 If zero, gdb will not use hardware for new watchpoints, even if\n\
16407 such is available. (However, any hardware watchpoints that were\n\
16408 created before setting this to nonzero, will continue to use watchpoint\n\
16409 hardware.)"),
16410 NULL,
16411 show_can_use_hw_watchpoints,
16412 &setlist, &showlist);
16413
16414 can_use_hw_watchpoints = 1;
16415
16416 /* Tracepoint manipulation commands. */
16417
16418 c = add_com ("trace", class_breakpoint, trace_command, _("\
16419 Set a tracepoint at specified location.\n\
16420 \n"
16421 BREAK_ARGS_HELP ("trace") "\n\
16422 Do \"help tracepoints\" for info on other tracepoint commands."));
16423 set_cmd_completer (c, location_completer);
16424
16425 add_com_alias ("tp", "trace", class_alias, 0);
16426 add_com_alias ("tr", "trace", class_alias, 1);
16427 add_com_alias ("tra", "trace", class_alias, 1);
16428 add_com_alias ("trac", "trace", class_alias, 1);
16429
16430 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
16431 Set a fast tracepoint at specified location.\n\
16432 \n"
16433 BREAK_ARGS_HELP ("ftrace") "\n\
16434 Do \"help tracepoints\" for info on other tracepoint commands."));
16435 set_cmd_completer (c, location_completer);
16436
16437 c = add_com ("strace", class_breakpoint, strace_command, _("\
16438 Set a static tracepoint at location or marker.\n\
16439 \n\
16440 strace [LOCATION] [if CONDITION]\n\
16441 LOCATION may be a linespec, explicit, or address location (described below) \n\
16442 or -m MARKER_ID.\n\n\
16443 If a marker id is specified, probe the marker with that name. With\n\
16444 no LOCATION, uses current execution address of the selected stack frame.\n\
16445 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16446 This collects arbitrary user data passed in the probe point call to the\n\
16447 tracing library. You can inspect it when analyzing the trace buffer,\n\
16448 by printing the $_sdata variable like any other convenience variable.\n\
16449 \n\
16450 CONDITION is a boolean expression.\n\
16451 \n" LOCATION_HELP_STRING "\n\
16452 Multiple tracepoints at one place are permitted, and useful if their\n\
16453 conditions are different.\n\
16454 \n\
16455 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16456 Do \"help tracepoints\" for info on other tracepoint commands."));
16457 set_cmd_completer (c, location_completer);
16458
16459 add_info ("tracepoints", tracepoints_info, _("\
16460 Status of specified tracepoints (all tracepoints if no argument).\n\
16461 Convenience variable \"$tpnum\" contains the number of the\n\
16462 last tracepoint set."));
16463
16464 add_info_alias ("tp", "tracepoints", 1);
16465
16466 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16467 Delete specified tracepoints.\n\
16468 Arguments are tracepoint numbers, separated by spaces.\n\
16469 No argument means delete all tracepoints."),
16470 &deletelist);
16471 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
16472
16473 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16474 Disable specified tracepoints.\n\
16475 Arguments are tracepoint numbers, separated by spaces.\n\
16476 No argument means disable all tracepoints."),
16477 &disablelist);
16478 deprecate_cmd (c, "disable");
16479
16480 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16481 Enable specified tracepoints.\n\
16482 Arguments are tracepoint numbers, separated by spaces.\n\
16483 No argument means enable all tracepoints."),
16484 &enablelist);
16485 deprecate_cmd (c, "enable");
16486
16487 add_com ("passcount", class_trace, trace_pass_command, _("\
16488 Set the passcount for a tracepoint.\n\
16489 The trace will end when the tracepoint has been passed 'count' times.\n\
16490 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16491 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16492
16493 add_prefix_cmd ("save", class_breakpoint, save_command,
16494 _("Save breakpoint definitions as a script."),
16495 &save_cmdlist, "save ",
16496 0/*allow-unknown*/, &cmdlist);
16497
16498 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16499 Save current breakpoint definitions as a script.\n\
16500 This includes all types of breakpoints (breakpoints, watchpoints,\n\
16501 catchpoints, tracepoints). Use the 'source' command in another debug\n\
16502 session to restore them."),
16503 &save_cmdlist);
16504 set_cmd_completer (c, filename_completer);
16505
16506 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
16507 Save current tracepoint definitions as a script.\n\
16508 Use the 'source' command in another debug session to restore them."),
16509 &save_cmdlist);
16510 set_cmd_completer (c, filename_completer);
16511
16512 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16513 deprecate_cmd (c, "save tracepoints");
16514
16515 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
16516 Breakpoint specific settings\n\
16517 Configure various breakpoint-specific variables such as\n\
16518 pending breakpoint behavior"),
16519 &breakpoint_set_cmdlist, "set breakpoint ",
16520 0/*allow-unknown*/, &setlist);
16521 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
16522 Breakpoint specific settings\n\
16523 Configure various breakpoint-specific variables such as\n\
16524 pending breakpoint behavior"),
16525 &breakpoint_show_cmdlist, "show breakpoint ",
16526 0/*allow-unknown*/, &showlist);
16527
16528 add_setshow_auto_boolean_cmd ("pending", no_class,
16529 &pending_break_support, _("\
16530 Set debugger's behavior regarding pending breakpoints."), _("\
16531 Show debugger's behavior regarding pending breakpoints."), _("\
16532 If on, an unrecognized breakpoint location will cause gdb to create a\n\
16533 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16534 an error. If auto, an unrecognized breakpoint location results in a\n\
16535 user-query to see if a pending breakpoint should be created."),
16536 NULL,
16537 show_pending_break_support,
16538 &breakpoint_set_cmdlist,
16539 &breakpoint_show_cmdlist);
16540
16541 pending_break_support = AUTO_BOOLEAN_AUTO;
16542
16543 add_setshow_boolean_cmd ("auto-hw", no_class,
16544 &automatic_hardware_breakpoints, _("\
16545 Set automatic usage of hardware breakpoints."), _("\
16546 Show automatic usage of hardware breakpoints."), _("\
16547 If set, the debugger will automatically use hardware breakpoints for\n\
16548 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16549 a warning will be emitted for such breakpoints."),
16550 NULL,
16551 show_automatic_hardware_breakpoints,
16552 &breakpoint_set_cmdlist,
16553 &breakpoint_show_cmdlist);
16554
16555 add_setshow_boolean_cmd ("always-inserted", class_support,
16556 &always_inserted_mode, _("\
16557 Set mode for inserting breakpoints."), _("\
16558 Show mode for inserting breakpoints."), _("\
16559 When this mode is on, breakpoints are inserted immediately as soon as\n\
16560 they're created, kept inserted even when execution stops, and removed\n\
16561 only when the user deletes them. When this mode is off (the default),\n\
16562 breakpoints are inserted only when execution continues, and removed\n\
16563 when execution stops."),
16564 NULL,
16565 &show_always_inserted_mode,
16566 &breakpoint_set_cmdlist,
16567 &breakpoint_show_cmdlist);
16568
16569 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16570 condition_evaluation_enums,
16571 &condition_evaluation_mode_1, _("\
16572 Set mode of breakpoint condition evaluation."), _("\
16573 Show mode of breakpoint condition evaluation."), _("\
16574 When this is set to \"host\", breakpoint conditions will be\n\
16575 evaluated on the host's side by GDB. When it is set to \"target\",\n\
16576 breakpoint conditions will be downloaded to the target (if the target\n\
16577 supports such feature) and conditions will be evaluated on the target's side.\n\
16578 If this is set to \"auto\" (default), this will be automatically set to\n\
16579 \"target\" if it supports condition evaluation, otherwise it will\n\
16580 be set to \"gdb\""),
16581 &set_condition_evaluation_mode,
16582 &show_condition_evaluation_mode,
16583 &breakpoint_set_cmdlist,
16584 &breakpoint_show_cmdlist);
16585
16586 add_com ("break-range", class_breakpoint, break_range_command, _("\
16587 Set a breakpoint for an address range.\n\
16588 break-range START-LOCATION, END-LOCATION\n\
16589 where START-LOCATION and END-LOCATION can be one of the following:\n\
16590 LINENUM, for that line in the current file,\n\
16591 FILE:LINENUM, for that line in that file,\n\
16592 +OFFSET, for that number of lines after the current line\n\
16593 or the start of the range\n\
16594 FUNCTION, for the first line in that function,\n\
16595 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16596 *ADDRESS, for the instruction at that address.\n\
16597 \n\
16598 The breakpoint will stop execution of the inferior whenever it executes\n\
16599 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16600 range (including START-LOCATION and END-LOCATION)."));
16601
16602 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
16603 Set a dynamic printf at specified location.\n\
16604 dprintf location,format string,arg1,arg2,...\n\
16605 location may be a linespec, explicit, or address location.\n"
16606 "\n" LOCATION_HELP_STRING));
16607 set_cmd_completer (c, location_completer);
16608
16609 add_setshow_enum_cmd ("dprintf-style", class_support,
16610 dprintf_style_enums, &dprintf_style, _("\
16611 Set the style of usage for dynamic printf."), _("\
16612 Show the style of usage for dynamic printf."), _("\
16613 This setting chooses how GDB will do a dynamic printf.\n\
16614 If the value is \"gdb\", then the printing is done by GDB to its own\n\
16615 console, as with the \"printf\" command.\n\
16616 If the value is \"call\", the print is done by calling a function in your\n\
16617 program; by default printf(), but you can choose a different function or\n\
16618 output stream by setting dprintf-function and dprintf-channel."),
16619 update_dprintf_commands, NULL,
16620 &setlist, &showlist);
16621
16622 dprintf_function = xstrdup ("printf");
16623 add_setshow_string_cmd ("dprintf-function", class_support,
16624 &dprintf_function, _("\
16625 Set the function to use for dynamic printf"), _("\
16626 Show the function to use for dynamic printf"), NULL,
16627 update_dprintf_commands, NULL,
16628 &setlist, &showlist);
16629
16630 dprintf_channel = xstrdup ("");
16631 add_setshow_string_cmd ("dprintf-channel", class_support,
16632 &dprintf_channel, _("\
16633 Set the channel to use for dynamic printf"), _("\
16634 Show the channel to use for dynamic printf"), NULL,
16635 update_dprintf_commands, NULL,
16636 &setlist, &showlist);
16637
16638 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16639 &disconnected_dprintf, _("\
16640 Set whether dprintf continues after GDB disconnects."), _("\
16641 Show whether dprintf continues after GDB disconnects."), _("\
16642 Use this to let dprintf commands continue to hit and produce output\n\
16643 even if GDB disconnects or detaches from the target."),
16644 NULL,
16645 NULL,
16646 &setlist, &showlist);
16647
16648 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16649 agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16650 (target agent only) This is useful for formatted output in user-defined commands."));
16651
16652 automatic_hardware_breakpoints = 1;
16653
16654 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
16655 observer_attach_thread_exit (remove_threaded_breakpoints);
16656 }
This page took 0.403029 seconds and 4 git commands to generate.