Obvious function doc and formatting
[deliverable/binutils-gdb.git] / gdb / breakpoint.c
1 /* Everything about breakpoints, for GDB.
2
3 Copyright (C) 1986-2016 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 "location.h"
71 #include "thread-fsm.h"
72 #include "tid-parse.h"
73
74 /* readline include files */
75 #include "readline/readline.h"
76 #include "readline/history.h"
77
78 /* readline defines this. */
79 #undef savestring
80
81 #include "mi/mi-common.h"
82 #include "extension.h"
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 (char *, void (*) (struct breakpoint *,
105 void *),
106 void *);
107
108 static void ignore_command (char *, int);
109
110 static int breakpoint_re_set_one (void *);
111
112 static void breakpoint_re_set_default (struct breakpoint *);
113
114 static void
115 create_sals_from_location_default (const struct event_location *location,
116 struct linespec_result *canonical,
117 enum bptype type_wanted);
118
119 static void create_breakpoints_sal_default (struct gdbarch *,
120 struct linespec_result *,
121 char *, char *, enum bptype,
122 enum bpdisp, int, int,
123 int,
124 const struct breakpoint_ops *,
125 int, int, int, unsigned);
126
127 static void decode_location_default (struct breakpoint *b,
128 const struct event_location *location,
129 struct program_space *search_pspace,
130 struct symtabs_and_lines *sals);
131
132 static void clear_command (char *, int);
133
134 static void catch_command (char *, int);
135
136 static int can_use_hardware_watchpoint (struct value *);
137
138 static void break_command_1 (char *, int, int);
139
140 static void mention (struct breakpoint *);
141
142 static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
143 enum bptype,
144 const struct breakpoint_ops *);
145 static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
146 const struct symtab_and_line *);
147
148 /* This function is used in gdbtk sources and thus can not be made
149 static. */
150 struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
151 struct symtab_and_line,
152 enum bptype,
153 const struct breakpoint_ops *);
154
155 static struct breakpoint *
156 momentary_breakpoint_from_master (struct breakpoint *orig,
157 enum bptype type,
158 const struct breakpoint_ops *ops,
159 int loc_enabled);
160
161 static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
162
163 static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
164 CORE_ADDR bpaddr,
165 enum bptype bptype);
166
167 static void describe_other_breakpoints (struct gdbarch *,
168 struct program_space *, CORE_ADDR,
169 struct obj_section *, int);
170
171 static int watchpoint_locations_match (struct bp_location *loc1,
172 struct bp_location *loc2);
173
174 static int breakpoint_location_address_match (struct bp_location *bl,
175 struct address_space *aspace,
176 CORE_ADDR addr);
177
178 static int breakpoint_location_address_range_overlap (struct bp_location *,
179 struct address_space *,
180 CORE_ADDR, int);
181
182 static void breakpoints_info (char *, int);
183
184 static void watchpoints_info (char *, int);
185
186 static int breakpoint_1 (char *, int,
187 int (*) (const struct breakpoint *));
188
189 static int breakpoint_cond_eval (void *);
190
191 static void cleanup_executing_breakpoints (void *);
192
193 static void commands_command (char *, int);
194
195 static void condition_command (char *, int);
196
197 typedef enum
198 {
199 mark_inserted,
200 mark_uninserted
201 }
202 insertion_state_t;
203
204 static int remove_breakpoint (struct bp_location *, insertion_state_t);
205 static int remove_breakpoint_1 (struct bp_location *, insertion_state_t);
206
207 static enum print_stop_action print_bp_stop_message (bpstat bs);
208
209 static int watchpoint_check (void *);
210
211 static void maintenance_info_breakpoints (char *, int);
212
213 static int hw_breakpoint_used_count (void);
214
215 static int hw_watchpoint_use_count (struct breakpoint *);
216
217 static int hw_watchpoint_used_count_others (struct breakpoint *except,
218 enum bptype type,
219 int *other_type_used);
220
221 static void hbreak_command (char *, int);
222
223 static void thbreak_command (char *, int);
224
225 static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
226 int count);
227
228 static void stop_command (char *arg, int from_tty);
229
230 static void stopin_command (char *arg, int from_tty);
231
232 static void stopat_command (char *arg, int from_tty);
233
234 static void tcatch_command (char *arg, int from_tty);
235
236 static void free_bp_location (struct bp_location *loc);
237 static void incref_bp_location (struct bp_location *loc);
238 static void decref_bp_location (struct bp_location **loc);
239
240 static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
241
242 /* update_global_location_list's modes of operation wrt to whether to
243 insert locations now. */
244 enum ugll_insert_mode
245 {
246 /* Don't insert any breakpoint locations into the inferior, only
247 remove already-inserted locations that no longer should be
248 inserted. Functions that delete a breakpoint or breakpoints
249 should specify this mode, so that deleting a breakpoint doesn't
250 have the side effect of inserting the locations of other
251 breakpoints that are marked not-inserted, but should_be_inserted
252 returns true on them.
253
254 This behavior is useful is situations close to tear-down -- e.g.,
255 after an exec, while the target still has execution, but
256 breakpoint shadows of the previous executable image should *NOT*
257 be restored to the new image; or before detaching, where the
258 target still has execution and wants to delete breakpoints from
259 GDB's lists, and all breakpoints had already been removed from
260 the inferior. */
261 UGLL_DONT_INSERT,
262
263 /* May insert breakpoints iff breakpoints_should_be_inserted_now
264 claims breakpoints should be inserted now. */
265 UGLL_MAY_INSERT,
266
267 /* Insert locations now, irrespective of
268 breakpoints_should_be_inserted_now. E.g., say all threads are
269 stopped right now, and the user did "continue". We need to
270 insert breakpoints _before_ resuming the target, but
271 UGLL_MAY_INSERT wouldn't insert them, because
272 breakpoints_should_be_inserted_now returns false at that point,
273 as no thread is running yet. */
274 UGLL_INSERT
275 };
276
277 static void update_global_location_list (enum ugll_insert_mode);
278
279 static void update_global_location_list_nothrow (enum ugll_insert_mode);
280
281 static int is_hardware_watchpoint (const struct breakpoint *bpt);
282
283 static void insert_breakpoint_locations (void);
284
285 static void tracepoints_info (char *, int);
286
287 static void delete_trace_command (char *, int);
288
289 static void enable_trace_command (char *, int);
290
291 static void disable_trace_command (char *, int);
292
293 static void trace_pass_command (char *, int);
294
295 static void set_tracepoint_count (int num);
296
297 static int is_masked_watchpoint (const struct breakpoint *b);
298
299 static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
300
301 /* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
302 otherwise. */
303
304 static int strace_marker_p (struct breakpoint *b);
305
306 /* The breakpoint_ops structure to be inherited by all breakpoint_ops
307 that are implemented on top of software or hardware breakpoints
308 (user breakpoints, internal and momentary breakpoints, etc.). */
309 static struct breakpoint_ops bkpt_base_breakpoint_ops;
310
311 /* Internal breakpoints class type. */
312 static struct breakpoint_ops internal_breakpoint_ops;
313
314 /* Momentary breakpoints class type. */
315 static struct breakpoint_ops momentary_breakpoint_ops;
316
317 /* Momentary breakpoints for bp_longjmp and bp_exception class type. */
318 static struct breakpoint_ops longjmp_breakpoint_ops;
319
320 /* The breakpoint_ops structure to be used in regular user created
321 breakpoints. */
322 struct breakpoint_ops bkpt_breakpoint_ops;
323
324 /* Breakpoints set on probes. */
325 static struct breakpoint_ops bkpt_probe_breakpoint_ops;
326
327 /* Dynamic printf class type. */
328 struct breakpoint_ops dprintf_breakpoint_ops;
329
330 /* The style in which to perform a dynamic printf. This is a user
331 option because different output options have different tradeoffs;
332 if GDB does the printing, there is better error handling if there
333 is a problem with any of the arguments, but using an inferior
334 function lets you have special-purpose printers and sending of
335 output to the same place as compiled-in print functions. */
336
337 static const char dprintf_style_gdb[] = "gdb";
338 static const char dprintf_style_call[] = "call";
339 static const char dprintf_style_agent[] = "agent";
340 static const char *const dprintf_style_enums[] = {
341 dprintf_style_gdb,
342 dprintf_style_call,
343 dprintf_style_agent,
344 NULL
345 };
346 static const char *dprintf_style = dprintf_style_gdb;
347
348 /* The function to use for dynamic printf if the preferred style is to
349 call into the inferior. The value is simply a string that is
350 copied into the command, so it can be anything that GDB can
351 evaluate to a callable address, not necessarily a function name. */
352
353 static char *dprintf_function = "";
354
355 /* The channel to use for dynamic printf if the preferred style is to
356 call into the inferior; if a nonempty string, it will be passed to
357 the call as the first argument, with the format string as the
358 second. As with the dprintf function, this can be anything that
359 GDB knows how to evaluate, so in addition to common choices like
360 "stderr", this could be an app-specific expression like
361 "mystreams[curlogger]". */
362
363 static char *dprintf_channel = "";
364
365 /* True if dprintf commands should continue to operate even if GDB
366 has disconnected. */
367 static int disconnected_dprintf = 1;
368
369 /* A reference-counted struct command_line. This lets multiple
370 breakpoints share a single command list. */
371 struct counted_command_line
372 {
373 /* The reference count. */
374 int refc;
375
376 /* The command list. */
377 struct command_line *commands;
378 };
379
380 struct command_line *
381 breakpoint_commands (struct breakpoint *b)
382 {
383 return b->commands ? b->commands->commands : NULL;
384 }
385
386 /* Flag indicating that a command has proceeded the inferior past the
387 current breakpoint. */
388
389 static int breakpoint_proceeded;
390
391 const char *
392 bpdisp_text (enum bpdisp disp)
393 {
394 /* NOTE: the following values are a part of MI protocol and
395 represent values of 'disp' field returned when inferior stops at
396 a breakpoint. */
397 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
398
399 return bpdisps[(int) disp];
400 }
401
402 /* Prototypes for exported functions. */
403 /* If FALSE, gdb will not use hardware support for watchpoints, even
404 if such is available. */
405 static int can_use_hw_watchpoints;
406
407 static void
408 show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
409 struct cmd_list_element *c,
410 const char *value)
411 {
412 fprintf_filtered (file,
413 _("Debugger's willingness to use "
414 "watchpoint hardware is %s.\n"),
415 value);
416 }
417
418 /* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
419 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
420 for unrecognized breakpoint locations.
421 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
422 static enum auto_boolean pending_break_support;
423 static void
424 show_pending_break_support (struct ui_file *file, int from_tty,
425 struct cmd_list_element *c,
426 const char *value)
427 {
428 fprintf_filtered (file,
429 _("Debugger's behavior regarding "
430 "pending breakpoints is %s.\n"),
431 value);
432 }
433
434 /* If 1, gdb will automatically use hardware breakpoints for breakpoints
435 set with "break" but falling in read-only memory.
436 If 0, gdb will warn about such breakpoints, but won't automatically
437 use hardware breakpoints. */
438 static int automatic_hardware_breakpoints;
439 static void
440 show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
441 struct cmd_list_element *c,
442 const char *value)
443 {
444 fprintf_filtered (file,
445 _("Automatic usage of hardware breakpoints is %s.\n"),
446 value);
447 }
448
449 /* If on, GDB keeps breakpoints inserted even if the inferior is
450 stopped, and immediately inserts any new breakpoints as soon as
451 they're created. If off (default), GDB keeps breakpoints off of
452 the target as long as possible. That is, it delays inserting
453 breakpoints until the next resume, and removes them again when the
454 target fully stops. This is a bit safer in case GDB crashes while
455 processing user input. */
456 static int always_inserted_mode = 0;
457
458 static void
459 show_always_inserted_mode (struct ui_file *file, int from_tty,
460 struct cmd_list_element *c, const char *value)
461 {
462 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
463 value);
464 }
465
466 /* See breakpoint.h. */
467
468 int
469 breakpoints_should_be_inserted_now (void)
470 {
471 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
472 {
473 /* If breakpoints are global, they should be inserted even if no
474 thread under gdb's control is running, or even if there are
475 no threads under GDB's control yet. */
476 return 1;
477 }
478 else if (target_has_execution)
479 {
480 struct thread_info *tp;
481
482 if (always_inserted_mode)
483 {
484 /* The user wants breakpoints inserted even if all threads
485 are stopped. */
486 return 1;
487 }
488
489 if (threads_are_executing ())
490 return 1;
491
492 /* Don't remove breakpoints yet if, even though all threads are
493 stopped, we still have events to process. */
494 ALL_NON_EXITED_THREADS (tp)
495 if (tp->resumed
496 && tp->suspend.waitstatus_pending_p)
497 return 1;
498 }
499 return 0;
500 }
501
502 static const char condition_evaluation_both[] = "host or target";
503
504 /* Modes for breakpoint condition evaluation. */
505 static const char condition_evaluation_auto[] = "auto";
506 static const char condition_evaluation_host[] = "host";
507 static const char condition_evaluation_target[] = "target";
508 static const char *const condition_evaluation_enums[] = {
509 condition_evaluation_auto,
510 condition_evaluation_host,
511 condition_evaluation_target,
512 NULL
513 };
514
515 /* Global that holds the current mode for breakpoint condition evaluation. */
516 static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
517
518 /* Global that we use to display information to the user (gets its value from
519 condition_evaluation_mode_1. */
520 static const char *condition_evaluation_mode = condition_evaluation_auto;
521
522 /* Translate a condition evaluation mode MODE into either "host"
523 or "target". This is used mostly to translate from "auto" to the
524 real setting that is being used. It returns the translated
525 evaluation mode. */
526
527 static const char *
528 translate_condition_evaluation_mode (const char *mode)
529 {
530 if (mode == condition_evaluation_auto)
531 {
532 if (target_supports_evaluation_of_breakpoint_conditions ())
533 return condition_evaluation_target;
534 else
535 return condition_evaluation_host;
536 }
537 else
538 return mode;
539 }
540
541 /* Discovers what condition_evaluation_auto translates to. */
542
543 static const char *
544 breakpoint_condition_evaluation_mode (void)
545 {
546 return translate_condition_evaluation_mode (condition_evaluation_mode);
547 }
548
549 /* Return true if GDB should evaluate breakpoint conditions or false
550 otherwise. */
551
552 static int
553 gdb_evaluates_breakpoint_condition_p (void)
554 {
555 const char *mode = breakpoint_condition_evaluation_mode ();
556
557 return (mode == condition_evaluation_host);
558 }
559
560 void _initialize_breakpoint (void);
561
562 /* Are we executing breakpoint commands? */
563 static int executing_breakpoint_commands;
564
565 /* Are overlay event breakpoints enabled? */
566 static int overlay_events_enabled;
567
568 /* See description in breakpoint.h. */
569 int target_exact_watchpoints = 0;
570
571 /* Walk the following statement or block through all breakpoints.
572 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
573 current breakpoint. */
574
575 #define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
576
577 #define ALL_BREAKPOINTS_SAFE(B,TMP) \
578 for (B = breakpoint_chain; \
579 B ? (TMP=B->next, 1): 0; \
580 B = TMP)
581
582 /* Similar iterator for the low-level breakpoints. SAFE variant is
583 not provided so update_global_location_list must not be called
584 while executing the block of ALL_BP_LOCATIONS. */
585
586 #define ALL_BP_LOCATIONS(B,BP_TMP) \
587 for (BP_TMP = bp_location; \
588 BP_TMP < bp_location + bp_location_count && (B = *BP_TMP); \
589 BP_TMP++)
590
591 /* Iterates through locations with address ADDRESS for the currently selected
592 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
593 to where the loop should start from.
594 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
595 appropriate location to start with. */
596
597 #define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
598 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
599 BP_LOCP_TMP = BP_LOCP_START; \
600 BP_LOCP_START \
601 && (BP_LOCP_TMP < bp_location + bp_location_count \
602 && (*BP_LOCP_TMP)->address == ADDRESS); \
603 BP_LOCP_TMP++)
604
605 /* Iterator for tracepoints only. */
606
607 #define ALL_TRACEPOINTS(B) \
608 for (B = breakpoint_chain; B; B = B->next) \
609 if (is_tracepoint (B))
610
611 /* Chains of all breakpoints defined. */
612
613 struct breakpoint *breakpoint_chain;
614
615 /* Array is sorted by bp_location_compare - primarily by the ADDRESS. */
616
617 static struct bp_location **bp_location;
618
619 /* Number of elements of BP_LOCATION. */
620
621 static unsigned bp_location_count;
622
623 /* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
624 ADDRESS for the current elements of BP_LOCATION which get a valid
625 result from bp_location_has_shadow. You can use it for roughly
626 limiting the subrange of BP_LOCATION to scan for shadow bytes for
627 an address you need to read. */
628
629 static CORE_ADDR bp_location_placed_address_before_address_max;
630
631 /* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
632 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
633 BP_LOCATION which get a valid result from bp_location_has_shadow.
634 You can use it for roughly limiting the subrange of BP_LOCATION to
635 scan for shadow bytes for an address you need to read. */
636
637 static CORE_ADDR bp_location_shadow_len_after_address_max;
638
639 /* The locations that no longer correspond to any breakpoint, unlinked
640 from bp_location array, but for which a hit may still be reported
641 by a target. */
642 VEC(bp_location_p) *moribund_locations = NULL;
643
644 /* Number of last breakpoint made. */
645
646 static int breakpoint_count;
647
648 /* The value of `breakpoint_count' before the last command that
649 created breakpoints. If the last (break-like) command created more
650 than one breakpoint, then the difference between BREAKPOINT_COUNT
651 and PREV_BREAKPOINT_COUNT is more than one. */
652 static int prev_breakpoint_count;
653
654 /* Number of last tracepoint made. */
655
656 static int tracepoint_count;
657
658 static struct cmd_list_element *breakpoint_set_cmdlist;
659 static struct cmd_list_element *breakpoint_show_cmdlist;
660 struct cmd_list_element *save_cmdlist;
661
662 /* See declaration at breakpoint.h. */
663
664 struct breakpoint *
665 breakpoint_find_if (int (*func) (struct breakpoint *b, void *d),
666 void *user_data)
667 {
668 struct breakpoint *b = NULL;
669
670 ALL_BREAKPOINTS (b)
671 {
672 if (func (b, user_data) != 0)
673 break;
674 }
675
676 return b;
677 }
678
679 /* Return whether a breakpoint is an active enabled breakpoint. */
680 static int
681 breakpoint_enabled (struct breakpoint *b)
682 {
683 return (b->enable_state == bp_enabled);
684 }
685
686 /* Set breakpoint count to NUM. */
687
688 static void
689 set_breakpoint_count (int num)
690 {
691 prev_breakpoint_count = breakpoint_count;
692 breakpoint_count = num;
693 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
694 }
695
696 /* Used by `start_rbreak_breakpoints' below, to record the current
697 breakpoint count before "rbreak" creates any breakpoint. */
698 static int rbreak_start_breakpoint_count;
699
700 /* Called at the start an "rbreak" command to record the first
701 breakpoint made. */
702
703 void
704 start_rbreak_breakpoints (void)
705 {
706 rbreak_start_breakpoint_count = breakpoint_count;
707 }
708
709 /* Called at the end of an "rbreak" command to record the last
710 breakpoint made. */
711
712 void
713 end_rbreak_breakpoints (void)
714 {
715 prev_breakpoint_count = rbreak_start_breakpoint_count;
716 }
717
718 /* Used in run_command to zero the hit count when a new run starts. */
719
720 void
721 clear_breakpoint_hit_counts (void)
722 {
723 struct breakpoint *b;
724
725 ALL_BREAKPOINTS (b)
726 b->hit_count = 0;
727 }
728
729 /* Allocate a new counted_command_line with reference count of 1.
730 The new structure owns COMMANDS. */
731
732 static struct counted_command_line *
733 alloc_counted_command_line (struct command_line *commands)
734 {
735 struct counted_command_line *result = XNEW (struct counted_command_line);
736
737 result->refc = 1;
738 result->commands = commands;
739
740 return result;
741 }
742
743 /* Increment reference count. This does nothing if CMD is NULL. */
744
745 static void
746 incref_counted_command_line (struct counted_command_line *cmd)
747 {
748 if (cmd)
749 ++cmd->refc;
750 }
751
752 /* Decrement reference count. If the reference count reaches 0,
753 destroy the counted_command_line. Sets *CMDP to NULL. This does
754 nothing if *CMDP is NULL. */
755
756 static void
757 decref_counted_command_line (struct counted_command_line **cmdp)
758 {
759 if (*cmdp)
760 {
761 if (--(*cmdp)->refc == 0)
762 {
763 free_command_lines (&(*cmdp)->commands);
764 xfree (*cmdp);
765 }
766 *cmdp = NULL;
767 }
768 }
769
770 /* A cleanup function that calls decref_counted_command_line. */
771
772 static void
773 do_cleanup_counted_command_line (void *arg)
774 {
775 decref_counted_command_line ((struct counted_command_line **) arg);
776 }
777
778 /* Create a cleanup that calls decref_counted_command_line on the
779 argument. */
780
781 static struct cleanup *
782 make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
783 {
784 return make_cleanup (do_cleanup_counted_command_line, cmdp);
785 }
786
787 \f
788 /* Return the breakpoint with the specified number, or NULL
789 if the number does not refer to an existing breakpoint. */
790
791 struct breakpoint *
792 get_breakpoint (int num)
793 {
794 struct breakpoint *b;
795
796 ALL_BREAKPOINTS (b)
797 if (b->number == num)
798 return b;
799
800 return NULL;
801 }
802
803 \f
804
805 /* Mark locations as "conditions have changed" in case the target supports
806 evaluating conditions on its side. */
807
808 static void
809 mark_breakpoint_modified (struct breakpoint *b)
810 {
811 struct bp_location *loc;
812
813 /* This is only meaningful if the target is
814 evaluating conditions and if the user has
815 opted for condition evaluation on the target's
816 side. */
817 if (gdb_evaluates_breakpoint_condition_p ()
818 || !target_supports_evaluation_of_breakpoint_conditions ())
819 return;
820
821 if (!is_breakpoint (b))
822 return;
823
824 for (loc = b->loc; loc; loc = loc->next)
825 loc->condition_changed = condition_modified;
826 }
827
828 /* Mark location as "conditions have changed" in case the target supports
829 evaluating conditions on its side. */
830
831 static void
832 mark_breakpoint_location_modified (struct bp_location *loc)
833 {
834 /* This is only meaningful if the target is
835 evaluating conditions and if the user has
836 opted for condition evaluation on the target's
837 side. */
838 if (gdb_evaluates_breakpoint_condition_p ()
839 || !target_supports_evaluation_of_breakpoint_conditions ())
840
841 return;
842
843 if (!is_breakpoint (loc->owner))
844 return;
845
846 loc->condition_changed = condition_modified;
847 }
848
849 /* Sets the condition-evaluation mode using the static global
850 condition_evaluation_mode. */
851
852 static void
853 set_condition_evaluation_mode (char *args, int from_tty,
854 struct cmd_list_element *c)
855 {
856 const char *old_mode, *new_mode;
857
858 if ((condition_evaluation_mode_1 == condition_evaluation_target)
859 && !target_supports_evaluation_of_breakpoint_conditions ())
860 {
861 condition_evaluation_mode_1 = condition_evaluation_mode;
862 warning (_("Target does not support breakpoint condition evaluation.\n"
863 "Using host evaluation mode instead."));
864 return;
865 }
866
867 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
868 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
869
870 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
871 settings was "auto". */
872 condition_evaluation_mode = condition_evaluation_mode_1;
873
874 /* Only update the mode if the user picked a different one. */
875 if (new_mode != old_mode)
876 {
877 struct bp_location *loc, **loc_tmp;
878 /* If the user switched to a different evaluation mode, we
879 need to synch the changes with the target as follows:
880
881 "host" -> "target": Send all (valid) conditions to the target.
882 "target" -> "host": Remove all the conditions from the target.
883 */
884
885 if (new_mode == condition_evaluation_target)
886 {
887 /* Mark everything modified and synch conditions with the
888 target. */
889 ALL_BP_LOCATIONS (loc, loc_tmp)
890 mark_breakpoint_location_modified (loc);
891 }
892 else
893 {
894 /* Manually mark non-duplicate locations to synch conditions
895 with the target. We do this to remove all the conditions the
896 target knows about. */
897 ALL_BP_LOCATIONS (loc, loc_tmp)
898 if (is_breakpoint (loc->owner) && loc->inserted)
899 loc->needs_update = 1;
900 }
901
902 /* Do the update. */
903 update_global_location_list (UGLL_MAY_INSERT);
904 }
905
906 return;
907 }
908
909 /* Shows the current mode of breakpoint condition evaluation. Explicitly shows
910 what "auto" is translating to. */
911
912 static void
913 show_condition_evaluation_mode (struct ui_file *file, int from_tty,
914 struct cmd_list_element *c, const char *value)
915 {
916 if (condition_evaluation_mode == condition_evaluation_auto)
917 fprintf_filtered (file,
918 _("Breakpoint condition evaluation "
919 "mode is %s (currently %s).\n"),
920 value,
921 breakpoint_condition_evaluation_mode ());
922 else
923 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
924 value);
925 }
926
927 /* A comparison function for bp_location AP and BP that is used by
928 bsearch. This comparison function only cares about addresses, unlike
929 the more general bp_location_compare function. */
930
931 static int
932 bp_location_compare_addrs (const void *ap, const void *bp)
933 {
934 const struct bp_location *a = *(const struct bp_location **) ap;
935 const struct bp_location *b = *(const struct bp_location **) bp;
936
937 if (a->address == b->address)
938 return 0;
939 else
940 return ((a->address > b->address) - (a->address < b->address));
941 }
942
943 /* Helper function to skip all bp_locations with addresses
944 less than ADDRESS. It returns the first bp_location that
945 is greater than or equal to ADDRESS. If none is found, just
946 return NULL. */
947
948 static struct bp_location **
949 get_first_locp_gte_addr (CORE_ADDR address)
950 {
951 struct bp_location dummy_loc;
952 struct bp_location *dummy_locp = &dummy_loc;
953 struct bp_location **locp_found = NULL;
954
955 /* Initialize the dummy location's address field. */
956 memset (&dummy_loc, 0, sizeof (struct bp_location));
957 dummy_loc.address = address;
958
959 /* Find a close match to the first location at ADDRESS. */
960 locp_found = ((struct bp_location **)
961 bsearch (&dummy_locp, bp_location, bp_location_count,
962 sizeof (struct bp_location **),
963 bp_location_compare_addrs));
964
965 /* Nothing was found, nothing left to do. */
966 if (locp_found == NULL)
967 return NULL;
968
969 /* We may have found a location that is at ADDRESS but is not the first in the
970 location's list. Go backwards (if possible) and locate the first one. */
971 while ((locp_found - 1) >= bp_location
972 && (*(locp_found - 1))->address == address)
973 locp_found--;
974
975 return locp_found;
976 }
977
978 void
979 set_breakpoint_condition (struct breakpoint *b, const char *exp,
980 int from_tty)
981 {
982 xfree (b->cond_string);
983 b->cond_string = NULL;
984
985 if (is_watchpoint (b))
986 {
987 struct watchpoint *w = (struct watchpoint *) b;
988
989 xfree (w->cond_exp);
990 w->cond_exp = NULL;
991 }
992 else
993 {
994 struct bp_location *loc;
995
996 for (loc = b->loc; loc; loc = loc->next)
997 {
998 xfree (loc->cond);
999 loc->cond = NULL;
1000
1001 /* No need to free the condition agent expression
1002 bytecode (if we have one). We will handle this
1003 when we go through update_global_location_list. */
1004 }
1005 }
1006
1007 if (*exp == 0)
1008 {
1009 if (from_tty)
1010 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
1011 }
1012 else
1013 {
1014 const char *arg = exp;
1015
1016 /* I don't know if it matters whether this is the string the user
1017 typed in or the decompiled expression. */
1018 b->cond_string = xstrdup (arg);
1019 b->condition_not_parsed = 0;
1020
1021 if (is_watchpoint (b))
1022 {
1023 struct watchpoint *w = (struct watchpoint *) b;
1024
1025 innermost_block = NULL;
1026 arg = exp;
1027 w->cond_exp = parse_exp_1 (&arg, 0, 0, 0);
1028 if (*arg)
1029 error (_("Junk at end of expression"));
1030 w->cond_exp_valid_block = innermost_block;
1031 }
1032 else
1033 {
1034 struct bp_location *loc;
1035
1036 for (loc = b->loc; loc; loc = loc->next)
1037 {
1038 arg = exp;
1039 loc->cond =
1040 parse_exp_1 (&arg, loc->address,
1041 block_for_pc (loc->address), 0);
1042 if (*arg)
1043 error (_("Junk at end of expression"));
1044 }
1045 }
1046 }
1047 mark_breakpoint_modified (b);
1048
1049 observer_notify_breakpoint_modified (b);
1050 }
1051
1052 /* Completion for the "condition" command. */
1053
1054 static VEC (char_ptr) *
1055 condition_completer (struct cmd_list_element *cmd,
1056 const char *text, const char *word)
1057 {
1058 const char *space;
1059
1060 text = skip_spaces_const (text);
1061 space = skip_to_space_const (text);
1062 if (*space == '\0')
1063 {
1064 int len;
1065 struct breakpoint *b;
1066 VEC (char_ptr) *result = NULL;
1067
1068 if (text[0] == '$')
1069 {
1070 /* We don't support completion of history indices. */
1071 if (isdigit (text[1]))
1072 return NULL;
1073 return complete_internalvar (&text[1]);
1074 }
1075
1076 /* We're completing the breakpoint number. */
1077 len = strlen (text);
1078
1079 ALL_BREAKPOINTS (b)
1080 {
1081 char number[50];
1082
1083 xsnprintf (number, sizeof (number), "%d", b->number);
1084
1085 if (strncmp (number, text, len) == 0)
1086 VEC_safe_push (char_ptr, result, xstrdup (number));
1087 }
1088
1089 return result;
1090 }
1091
1092 /* We're completing the expression part. */
1093 text = skip_spaces_const (space);
1094 return expression_completer (cmd, text, word);
1095 }
1096
1097 /* condition N EXP -- set break condition of breakpoint N to EXP. */
1098
1099 static void
1100 condition_command (char *arg, int from_tty)
1101 {
1102 struct breakpoint *b;
1103 char *p;
1104 int bnum;
1105
1106 if (arg == 0)
1107 error_no_arg (_("breakpoint number"));
1108
1109 p = arg;
1110 bnum = get_number (&p);
1111 if (bnum == 0)
1112 error (_("Bad breakpoint argument: '%s'"), arg);
1113
1114 ALL_BREAKPOINTS (b)
1115 if (b->number == bnum)
1116 {
1117 /* Check if this breakpoint has a "stop" method implemented in an
1118 extension language. This method and conditions entered into GDB
1119 from the CLI are mutually exclusive. */
1120 const struct extension_language_defn *extlang
1121 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1122
1123 if (extlang != NULL)
1124 {
1125 error (_("Only one stop condition allowed. There is currently"
1126 " a %s stop condition defined for this breakpoint."),
1127 ext_lang_capitalized_name (extlang));
1128 }
1129 set_breakpoint_condition (b, p, from_tty);
1130
1131 if (is_breakpoint (b))
1132 update_global_location_list (UGLL_MAY_INSERT);
1133
1134 return;
1135 }
1136
1137 error (_("No breakpoint number %d."), bnum);
1138 }
1139
1140 /* Check that COMMAND do not contain commands that are suitable
1141 only for tracepoints and not suitable for ordinary breakpoints.
1142 Throw if any such commands is found. */
1143
1144 static void
1145 check_no_tracepoint_commands (struct command_line *commands)
1146 {
1147 struct command_line *c;
1148
1149 for (c = commands; c; c = c->next)
1150 {
1151 int i;
1152
1153 if (c->control_type == while_stepping_control)
1154 error (_("The 'while-stepping' command can "
1155 "only be used for tracepoints"));
1156
1157 for (i = 0; i < c->body_count; ++i)
1158 check_no_tracepoint_commands ((c->body_list)[i]);
1159
1160 /* Not that command parsing removes leading whitespace and comment
1161 lines and also empty lines. So, we only need to check for
1162 command directly. */
1163 if (strstr (c->line, "collect ") == c->line)
1164 error (_("The 'collect' command can only be used for tracepoints"));
1165
1166 if (strstr (c->line, "teval ") == c->line)
1167 error (_("The 'teval' command can only be used for tracepoints"));
1168 }
1169 }
1170
1171 /* Encapsulate tests for different types of tracepoints. */
1172
1173 static int
1174 is_tracepoint_type (enum bptype type)
1175 {
1176 return (type == bp_tracepoint
1177 || type == bp_fast_tracepoint
1178 || type == bp_static_tracepoint);
1179 }
1180
1181 int
1182 is_tracepoint (const struct breakpoint *b)
1183 {
1184 return is_tracepoint_type (b->type);
1185 }
1186
1187 /* A helper function that validates that COMMANDS are valid for a
1188 breakpoint. This function will throw an exception if a problem is
1189 found. */
1190
1191 static void
1192 validate_commands_for_breakpoint (struct breakpoint *b,
1193 struct command_line *commands)
1194 {
1195 if (is_tracepoint (b))
1196 {
1197 struct tracepoint *t = (struct tracepoint *) b;
1198 struct command_line *c;
1199 struct command_line *while_stepping = 0;
1200
1201 /* Reset the while-stepping step count. The previous commands
1202 might have included a while-stepping action, while the new
1203 ones might not. */
1204 t->step_count = 0;
1205
1206 /* We need to verify that each top-level element of commands is
1207 valid for tracepoints, that there's at most one
1208 while-stepping element, and that the while-stepping's body
1209 has valid tracing commands excluding nested while-stepping.
1210 We also need to validate the tracepoint action line in the
1211 context of the tracepoint --- validate_actionline actually
1212 has side effects, like setting the tracepoint's
1213 while-stepping STEP_COUNT, in addition to checking if the
1214 collect/teval actions parse and make sense in the
1215 tracepoint's context. */
1216 for (c = commands; c; c = c->next)
1217 {
1218 if (c->control_type == while_stepping_control)
1219 {
1220 if (b->type == bp_fast_tracepoint)
1221 error (_("The 'while-stepping' command "
1222 "cannot be used for fast tracepoint"));
1223 else if (b->type == bp_static_tracepoint)
1224 error (_("The 'while-stepping' command "
1225 "cannot be used for static tracepoint"));
1226
1227 if (while_stepping)
1228 error (_("The 'while-stepping' command "
1229 "can be used only once"));
1230 else
1231 while_stepping = c;
1232 }
1233
1234 validate_actionline (c->line, b);
1235 }
1236 if (while_stepping)
1237 {
1238 struct command_line *c2;
1239
1240 gdb_assert (while_stepping->body_count == 1);
1241 c2 = while_stepping->body_list[0];
1242 for (; c2; c2 = c2->next)
1243 {
1244 if (c2->control_type == while_stepping_control)
1245 error (_("The 'while-stepping' command cannot be nested"));
1246 }
1247 }
1248 }
1249 else
1250 {
1251 check_no_tracepoint_commands (commands);
1252 }
1253 }
1254
1255 /* Return a vector of all the static tracepoints set at ADDR. The
1256 caller is responsible for releasing the vector. */
1257
1258 VEC(breakpoint_p) *
1259 static_tracepoints_here (CORE_ADDR addr)
1260 {
1261 struct breakpoint *b;
1262 VEC(breakpoint_p) *found = 0;
1263 struct bp_location *loc;
1264
1265 ALL_BREAKPOINTS (b)
1266 if (b->type == bp_static_tracepoint)
1267 {
1268 for (loc = b->loc; loc; loc = loc->next)
1269 if (loc->address == addr)
1270 VEC_safe_push(breakpoint_p, found, b);
1271 }
1272
1273 return found;
1274 }
1275
1276 /* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
1277 validate that only allowed commands are included. */
1278
1279 void
1280 breakpoint_set_commands (struct breakpoint *b,
1281 struct command_line *commands)
1282 {
1283 validate_commands_for_breakpoint (b, commands);
1284
1285 decref_counted_command_line (&b->commands);
1286 b->commands = alloc_counted_command_line (commands);
1287 observer_notify_breakpoint_modified (b);
1288 }
1289
1290 /* Set the internal `silent' flag on the breakpoint. Note that this
1291 is not the same as the "silent" that may appear in the breakpoint's
1292 commands. */
1293
1294 void
1295 breakpoint_set_silent (struct breakpoint *b, int silent)
1296 {
1297 int old_silent = b->silent;
1298
1299 b->silent = silent;
1300 if (old_silent != silent)
1301 observer_notify_breakpoint_modified (b);
1302 }
1303
1304 /* Set the thread for this breakpoint. If THREAD is -1, make the
1305 breakpoint work for any thread. */
1306
1307 void
1308 breakpoint_set_thread (struct breakpoint *b, int thread)
1309 {
1310 int old_thread = b->thread;
1311
1312 b->thread = thread;
1313 if (old_thread != thread)
1314 observer_notify_breakpoint_modified (b);
1315 }
1316
1317 /* Set the task for this breakpoint. If TASK is 0, make the
1318 breakpoint work for any task. */
1319
1320 void
1321 breakpoint_set_task (struct breakpoint *b, int task)
1322 {
1323 int old_task = b->task;
1324
1325 b->task = task;
1326 if (old_task != task)
1327 observer_notify_breakpoint_modified (b);
1328 }
1329
1330 void
1331 check_tracepoint_command (char *line, void *closure)
1332 {
1333 struct breakpoint *b = (struct breakpoint *) closure;
1334
1335 validate_actionline (line, b);
1336 }
1337
1338 /* A structure used to pass information through
1339 map_breakpoint_numbers. */
1340
1341 struct commands_info
1342 {
1343 /* True if the command was typed at a tty. */
1344 int from_tty;
1345
1346 /* The breakpoint range spec. */
1347 char *arg;
1348
1349 /* Non-NULL if the body of the commands are being read from this
1350 already-parsed command. */
1351 struct command_line *control;
1352
1353 /* The command lines read from the user, or NULL if they have not
1354 yet been read. */
1355 struct counted_command_line *cmd;
1356 };
1357
1358 /* A callback for map_breakpoint_numbers that sets the commands for
1359 commands_command. */
1360
1361 static void
1362 do_map_commands_command (struct breakpoint *b, void *data)
1363 {
1364 struct commands_info *info = (struct commands_info *) data;
1365
1366 if (info->cmd == NULL)
1367 {
1368 struct command_line *l;
1369
1370 if (info->control != NULL)
1371 l = copy_command_lines (info->control->body_list[0]);
1372 else
1373 {
1374 struct cleanup *old_chain;
1375 char *str;
1376
1377 str = xstrprintf (_("Type commands for breakpoint(s) "
1378 "%s, one per line."),
1379 info->arg);
1380
1381 old_chain = make_cleanup (xfree, str);
1382
1383 l = read_command_lines (str,
1384 info->from_tty, 1,
1385 (is_tracepoint (b)
1386 ? check_tracepoint_command : 0),
1387 b);
1388
1389 do_cleanups (old_chain);
1390 }
1391
1392 info->cmd = alloc_counted_command_line (l);
1393 }
1394
1395 /* If a breakpoint was on the list more than once, we don't need to
1396 do anything. */
1397 if (b->commands != info->cmd)
1398 {
1399 validate_commands_for_breakpoint (b, info->cmd->commands);
1400 incref_counted_command_line (info->cmd);
1401 decref_counted_command_line (&b->commands);
1402 b->commands = info->cmd;
1403 observer_notify_breakpoint_modified (b);
1404 }
1405 }
1406
1407 static void
1408 commands_command_1 (char *arg, int from_tty,
1409 struct command_line *control)
1410 {
1411 struct cleanup *cleanups;
1412 struct commands_info info;
1413
1414 info.from_tty = from_tty;
1415 info.control = control;
1416 info.cmd = NULL;
1417 /* If we read command lines from the user, then `info' will hold an
1418 extra reference to the commands that we must clean up. */
1419 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
1420
1421 if (arg == NULL || !*arg)
1422 {
1423 if (breakpoint_count - prev_breakpoint_count > 1)
1424 arg = xstrprintf ("%d-%d", prev_breakpoint_count + 1,
1425 breakpoint_count);
1426 else if (breakpoint_count > 0)
1427 arg = xstrprintf ("%d", breakpoint_count);
1428 else
1429 {
1430 /* So that we don't try to free the incoming non-NULL
1431 argument in the cleanup below. Mapping breakpoint
1432 numbers will fail in this case. */
1433 arg = NULL;
1434 }
1435 }
1436 else
1437 /* The command loop has some static state, so we need to preserve
1438 our argument. */
1439 arg = xstrdup (arg);
1440
1441 if (arg != NULL)
1442 make_cleanup (xfree, arg);
1443
1444 info.arg = arg;
1445
1446 map_breakpoint_numbers (arg, do_map_commands_command, &info);
1447
1448 if (info.cmd == NULL)
1449 error (_("No breakpoints specified."));
1450
1451 do_cleanups (cleanups);
1452 }
1453
1454 static void
1455 commands_command (char *arg, int from_tty)
1456 {
1457 commands_command_1 (arg, from_tty, NULL);
1458 }
1459
1460 /* Like commands_command, but instead of reading the commands from
1461 input stream, takes them from an already parsed command structure.
1462
1463 This is used by cli-script.c to DTRT with breakpoint commands
1464 that are part of if and while bodies. */
1465 enum command_control_type
1466 commands_from_control_command (char *arg, struct command_line *cmd)
1467 {
1468 commands_command_1 (arg, 0, cmd);
1469 return simple_control;
1470 }
1471
1472 /* Return non-zero if BL->TARGET_INFO contains valid information. */
1473
1474 static int
1475 bp_location_has_shadow (struct bp_location *bl)
1476 {
1477 if (bl->loc_type != bp_loc_software_breakpoint)
1478 return 0;
1479 if (!bl->inserted)
1480 return 0;
1481 if (bl->target_info.shadow_len == 0)
1482 /* BL isn't valid, or doesn't shadow memory. */
1483 return 0;
1484 return 1;
1485 }
1486
1487 /* Update BUF, which is LEN bytes read from the target address
1488 MEMADDR, by replacing a memory breakpoint with its shadowed
1489 contents.
1490
1491 If READBUF is not NULL, this buffer must not overlap with the of
1492 the breakpoint location's shadow_contents buffer. Otherwise, a
1493 failed assertion internal error will be raised. */
1494
1495 static void
1496 one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1497 const gdb_byte *writebuf_org,
1498 ULONGEST memaddr, LONGEST len,
1499 struct bp_target_info *target_info,
1500 struct gdbarch *gdbarch)
1501 {
1502 /* Now do full processing of the found relevant range of elements. */
1503 CORE_ADDR bp_addr = 0;
1504 int bp_size = 0;
1505 int bptoffset = 0;
1506
1507 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1508 current_program_space->aspace, 0))
1509 {
1510 /* The breakpoint is inserted in a different address space. */
1511 return;
1512 }
1513
1514 /* Addresses and length of the part of the breakpoint that
1515 we need to copy. */
1516 bp_addr = target_info->placed_address;
1517 bp_size = target_info->shadow_len;
1518
1519 if (bp_addr + bp_size <= memaddr)
1520 {
1521 /* The breakpoint is entirely before the chunk of memory we are
1522 reading. */
1523 return;
1524 }
1525
1526 if (bp_addr >= memaddr + len)
1527 {
1528 /* The breakpoint is entirely after the chunk of memory we are
1529 reading. */
1530 return;
1531 }
1532
1533 /* Offset within shadow_contents. */
1534 if (bp_addr < memaddr)
1535 {
1536 /* Only copy the second part of the breakpoint. */
1537 bp_size -= memaddr - bp_addr;
1538 bptoffset = memaddr - bp_addr;
1539 bp_addr = memaddr;
1540 }
1541
1542 if (bp_addr + bp_size > memaddr + len)
1543 {
1544 /* Only copy the first part of the breakpoint. */
1545 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1546 }
1547
1548 if (readbuf != NULL)
1549 {
1550 /* Verify that the readbuf buffer does not overlap with the
1551 shadow_contents buffer. */
1552 gdb_assert (target_info->shadow_contents >= readbuf + len
1553 || readbuf >= (target_info->shadow_contents
1554 + target_info->shadow_len));
1555
1556 /* Update the read buffer with this inserted breakpoint's
1557 shadow. */
1558 memcpy (readbuf + bp_addr - memaddr,
1559 target_info->shadow_contents + bptoffset, bp_size);
1560 }
1561 else
1562 {
1563 const unsigned char *bp;
1564 CORE_ADDR addr = target_info->reqstd_address;
1565 int placed_size;
1566
1567 /* Update the shadow with what we want to write to memory. */
1568 memcpy (target_info->shadow_contents + bptoffset,
1569 writebuf_org + bp_addr - memaddr, bp_size);
1570
1571 /* Determine appropriate breakpoint contents and size for this
1572 address. */
1573 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
1574
1575 /* Update the final write buffer with this inserted
1576 breakpoint's INSN. */
1577 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1578 }
1579 }
1580
1581 /* Update BUF, which is LEN bytes read from the target address MEMADDR,
1582 by replacing any memory breakpoints with their shadowed contents.
1583
1584 If READBUF is not NULL, this buffer must not overlap with any of
1585 the breakpoint location's shadow_contents buffers. Otherwise,
1586 a failed assertion internal error will be raised.
1587
1588 The range of shadowed area by each bp_location is:
1589 bl->address - bp_location_placed_address_before_address_max
1590 up to bl->address + bp_location_shadow_len_after_address_max
1591 The range we were requested to resolve shadows for is:
1592 memaddr ... memaddr + len
1593 Thus the safe cutoff boundaries for performance optimization are
1594 memaddr + len <= (bl->address
1595 - bp_location_placed_address_before_address_max)
1596 and:
1597 bl->address + bp_location_shadow_len_after_address_max <= memaddr */
1598
1599 void
1600 breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1601 const gdb_byte *writebuf_org,
1602 ULONGEST memaddr, LONGEST len)
1603 {
1604 /* Left boundary, right boundary and median element of our binary
1605 search. */
1606 unsigned bc_l, bc_r, bc;
1607 size_t i;
1608
1609 /* Find BC_L which is a leftmost element which may affect BUF
1610 content. It is safe to report lower value but a failure to
1611 report higher one. */
1612
1613 bc_l = 0;
1614 bc_r = bp_location_count;
1615 while (bc_l + 1 < bc_r)
1616 {
1617 struct bp_location *bl;
1618
1619 bc = (bc_l + bc_r) / 2;
1620 bl = bp_location[bc];
1621
1622 /* Check first BL->ADDRESS will not overflow due to the added
1623 constant. Then advance the left boundary only if we are sure
1624 the BC element can in no way affect the BUF content (MEMADDR
1625 to MEMADDR + LEN range).
1626
1627 Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1628 offset so that we cannot miss a breakpoint with its shadow
1629 range tail still reaching MEMADDR. */
1630
1631 if ((bl->address + bp_location_shadow_len_after_address_max
1632 >= bl->address)
1633 && (bl->address + bp_location_shadow_len_after_address_max
1634 <= memaddr))
1635 bc_l = bc;
1636 else
1637 bc_r = bc;
1638 }
1639
1640 /* Due to the binary search above, we need to make sure we pick the
1641 first location that's at BC_L's address. E.g., if there are
1642 multiple locations at the same address, BC_L may end up pointing
1643 at a duplicate location, and miss the "master"/"inserted"
1644 location. Say, given locations L1, L2 and L3 at addresses A and
1645 B:
1646
1647 L1@A, L2@A, L3@B, ...
1648
1649 BC_L could end up pointing at location L2, while the "master"
1650 location could be L1. Since the `loc->inserted' flag is only set
1651 on "master" locations, we'd forget to restore the shadow of L1
1652 and L2. */
1653 while (bc_l > 0
1654 && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1655 bc_l--;
1656
1657 /* Now do full processing of the found relevant range of elements. */
1658
1659 for (bc = bc_l; bc < bp_location_count; bc++)
1660 {
1661 struct bp_location *bl = bp_location[bc];
1662 CORE_ADDR bp_addr = 0;
1663 int bp_size = 0;
1664 int bptoffset = 0;
1665
1666 /* bp_location array has BL->OWNER always non-NULL. */
1667 if (bl->owner->type == bp_none)
1668 warning (_("reading through apparently deleted breakpoint #%d?"),
1669 bl->owner->number);
1670
1671 /* Performance optimization: any further element can no longer affect BUF
1672 content. */
1673
1674 if (bl->address >= bp_location_placed_address_before_address_max
1675 && memaddr + len <= (bl->address
1676 - bp_location_placed_address_before_address_max))
1677 break;
1678
1679 if (!bp_location_has_shadow (bl))
1680 continue;
1681
1682 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1683 memaddr, len, &bl->target_info, bl->gdbarch);
1684 }
1685 }
1686
1687 \f
1688
1689 /* Return true if BPT is either a software breakpoint or a hardware
1690 breakpoint. */
1691
1692 int
1693 is_breakpoint (const struct breakpoint *bpt)
1694 {
1695 return (bpt->type == bp_breakpoint
1696 || bpt->type == bp_hardware_breakpoint
1697 || bpt->type == bp_dprintf);
1698 }
1699
1700 /* Return true if BPT is of any hardware watchpoint kind. */
1701
1702 static int
1703 is_hardware_watchpoint (const struct breakpoint *bpt)
1704 {
1705 return (bpt->type == bp_hardware_watchpoint
1706 || bpt->type == bp_read_watchpoint
1707 || bpt->type == bp_access_watchpoint);
1708 }
1709
1710 /* Return true if BPT is of any watchpoint kind, hardware or
1711 software. */
1712
1713 int
1714 is_watchpoint (const struct breakpoint *bpt)
1715 {
1716 return (is_hardware_watchpoint (bpt)
1717 || bpt->type == bp_watchpoint);
1718 }
1719
1720 /* Returns true if the current thread and its running state are safe
1721 to evaluate or update watchpoint B. Watchpoints on local
1722 expressions need to be evaluated in the context of the thread that
1723 was current when the watchpoint was created, and, that thread needs
1724 to be stopped to be able to select the correct frame context.
1725 Watchpoints on global expressions can be evaluated on any thread,
1726 and in any state. It is presently left to the target allowing
1727 memory accesses when threads are running. */
1728
1729 static int
1730 watchpoint_in_thread_scope (struct watchpoint *b)
1731 {
1732 return (b->base.pspace == current_program_space
1733 && (ptid_equal (b->watchpoint_thread, null_ptid)
1734 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1735 && !is_executing (inferior_ptid))));
1736 }
1737
1738 /* Set watchpoint B to disp_del_at_next_stop, even including its possible
1739 associated bp_watchpoint_scope breakpoint. */
1740
1741 static void
1742 watchpoint_del_at_next_stop (struct watchpoint *w)
1743 {
1744 struct breakpoint *b = &w->base;
1745
1746 if (b->related_breakpoint != b)
1747 {
1748 gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1749 gdb_assert (b->related_breakpoint->related_breakpoint == b);
1750 b->related_breakpoint->disposition = disp_del_at_next_stop;
1751 b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1752 b->related_breakpoint = b;
1753 }
1754 b->disposition = disp_del_at_next_stop;
1755 }
1756
1757 /* Extract a bitfield value from value VAL using the bit parameters contained in
1758 watchpoint W. */
1759
1760 static struct value *
1761 extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1762 {
1763 struct value *bit_val;
1764
1765 if (val == NULL)
1766 return NULL;
1767
1768 bit_val = allocate_value (value_type (val));
1769
1770 unpack_value_bitfield (bit_val,
1771 w->val_bitpos,
1772 w->val_bitsize,
1773 value_contents_for_printing (val),
1774 value_offset (val),
1775 val);
1776
1777 return bit_val;
1778 }
1779
1780 /* Allocate a dummy location and add it to B, which must be a software
1781 watchpoint. This is required because even if a software watchpoint
1782 is not watching any memory, bpstat_stop_status requires a location
1783 to be able to report stops. */
1784
1785 static void
1786 software_watchpoint_add_no_memory_location (struct breakpoint *b,
1787 struct program_space *pspace)
1788 {
1789 gdb_assert (b->type == bp_watchpoint && b->loc == NULL);
1790
1791 b->loc = allocate_bp_location (b);
1792 b->loc->pspace = pspace;
1793 b->loc->address = -1;
1794 b->loc->length = -1;
1795 }
1796
1797 /* Returns true if B is a software watchpoint that is not watching any
1798 memory (e.g., "watch $pc"). */
1799
1800 static int
1801 is_no_memory_software_watchpoint (struct breakpoint *b)
1802 {
1803 return (b->type == bp_watchpoint
1804 && b->loc != NULL
1805 && b->loc->next == NULL
1806 && b->loc->address == -1
1807 && b->loc->length == -1);
1808 }
1809
1810 /* Assuming that B is a watchpoint:
1811 - Reparse watchpoint expression, if REPARSE is non-zero
1812 - Evaluate expression and store the result in B->val
1813 - Evaluate the condition if there is one, and store the result
1814 in b->loc->cond.
1815 - Update the list of values that must be watched in B->loc.
1816
1817 If the watchpoint disposition is disp_del_at_next_stop, then do
1818 nothing. If this is local watchpoint that is out of scope, delete
1819 it.
1820
1821 Even with `set breakpoint always-inserted on' the watchpoints are
1822 removed + inserted on each stop here. Normal breakpoints must
1823 never be removed because they might be missed by a running thread
1824 when debugging in non-stop mode. On the other hand, hardware
1825 watchpoints (is_hardware_watchpoint; processed here) are specific
1826 to each LWP since they are stored in each LWP's hardware debug
1827 registers. Therefore, such LWP must be stopped first in order to
1828 be able to modify its hardware watchpoints.
1829
1830 Hardware watchpoints must be reset exactly once after being
1831 presented to the user. It cannot be done sooner, because it would
1832 reset the data used to present the watchpoint hit to the user. And
1833 it must not be done later because it could display the same single
1834 watchpoint hit during multiple GDB stops. Note that the latter is
1835 relevant only to the hardware watchpoint types bp_read_watchpoint
1836 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1837 not user-visible - its hit is suppressed if the memory content has
1838 not changed.
1839
1840 The following constraints influence the location where we can reset
1841 hardware watchpoints:
1842
1843 * target_stopped_by_watchpoint and target_stopped_data_address are
1844 called several times when GDB stops.
1845
1846 [linux]
1847 * Multiple hardware watchpoints can be hit at the same time,
1848 causing GDB to stop. GDB only presents one hardware watchpoint
1849 hit at a time as the reason for stopping, and all the other hits
1850 are presented later, one after the other, each time the user
1851 requests the execution to be resumed. Execution is not resumed
1852 for the threads still having pending hit event stored in
1853 LWP_INFO->STATUS. While the watchpoint is already removed from
1854 the inferior on the first stop the thread hit event is kept being
1855 reported from its cached value by linux_nat_stopped_data_address
1856 until the real thread resume happens after the watchpoint gets
1857 presented and thus its LWP_INFO->STATUS gets reset.
1858
1859 Therefore the hardware watchpoint hit can get safely reset on the
1860 watchpoint removal from inferior. */
1861
1862 static void
1863 update_watchpoint (struct watchpoint *b, int reparse)
1864 {
1865 int within_current_scope;
1866 struct frame_id saved_frame_id;
1867 int frame_saved;
1868
1869 /* If this is a local watchpoint, we only want to check if the
1870 watchpoint frame is in scope if the current thread is the thread
1871 that was used to create the watchpoint. */
1872 if (!watchpoint_in_thread_scope (b))
1873 return;
1874
1875 if (b->base.disposition == disp_del_at_next_stop)
1876 return;
1877
1878 frame_saved = 0;
1879
1880 /* Determine if the watchpoint is within scope. */
1881 if (b->exp_valid_block == NULL)
1882 within_current_scope = 1;
1883 else
1884 {
1885 struct frame_info *fi = get_current_frame ();
1886 struct gdbarch *frame_arch = get_frame_arch (fi);
1887 CORE_ADDR frame_pc = get_frame_pc (fi);
1888
1889 /* If we're at a point where the stack has been destroyed
1890 (e.g. in a function epilogue), unwinding may not work
1891 properly. Do not attempt to recreate locations at this
1892 point. See similar comments in watchpoint_check. */
1893 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
1894 return;
1895
1896 /* Save the current frame's ID so we can restore it after
1897 evaluating the watchpoint expression on its own frame. */
1898 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1899 took a frame parameter, so that we didn't have to change the
1900 selected frame. */
1901 frame_saved = 1;
1902 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1903
1904 fi = frame_find_by_id (b->watchpoint_frame);
1905 within_current_scope = (fi != NULL);
1906 if (within_current_scope)
1907 select_frame (fi);
1908 }
1909
1910 /* We don't free locations. They are stored in the bp_location array
1911 and update_global_location_list will eventually delete them and
1912 remove breakpoints if needed. */
1913 b->base.loc = NULL;
1914
1915 if (within_current_scope && reparse)
1916 {
1917 const char *s;
1918
1919 if (b->exp)
1920 {
1921 xfree (b->exp);
1922 b->exp = NULL;
1923 }
1924 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1925 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
1926 /* If the meaning of expression itself changed, the old value is
1927 no longer relevant. We don't want to report a watchpoint hit
1928 to the user when the old value and the new value may actually
1929 be completely different objects. */
1930 value_free (b->val);
1931 b->val = NULL;
1932 b->val_valid = 0;
1933
1934 /* Note that unlike with breakpoints, the watchpoint's condition
1935 expression is stored in the breakpoint object, not in the
1936 locations (re)created below. */
1937 if (b->base.cond_string != NULL)
1938 {
1939 if (b->cond_exp != NULL)
1940 {
1941 xfree (b->cond_exp);
1942 b->cond_exp = NULL;
1943 }
1944
1945 s = b->base.cond_string;
1946 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
1947 }
1948 }
1949
1950 /* If we failed to parse the expression, for example because
1951 it refers to a global variable in a not-yet-loaded shared library,
1952 don't try to insert watchpoint. We don't automatically delete
1953 such watchpoint, though, since failure to parse expression
1954 is different from out-of-scope watchpoint. */
1955 if (!target_has_execution)
1956 {
1957 /* Without execution, memory can't change. No use to try and
1958 set watchpoint locations. The watchpoint will be reset when
1959 the target gains execution, through breakpoint_re_set. */
1960 if (!can_use_hw_watchpoints)
1961 {
1962 if (b->base.ops->works_in_software_mode (&b->base))
1963 b->base.type = bp_watchpoint;
1964 else
1965 error (_("Can't set read/access watchpoint when "
1966 "hardware watchpoints are disabled."));
1967 }
1968 }
1969 else if (within_current_scope && b->exp)
1970 {
1971 int pc = 0;
1972 struct value *val_chain, *v, *result, *next;
1973 struct program_space *frame_pspace;
1974
1975 fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain, 0);
1976
1977 /* Avoid setting b->val if it's already set. The meaning of
1978 b->val is 'the last value' user saw, and we should update
1979 it only if we reported that last value to user. As it
1980 happens, the code that reports it updates b->val directly.
1981 We don't keep track of the memory value for masked
1982 watchpoints. */
1983 if (!b->val_valid && !is_masked_watchpoint (&b->base))
1984 {
1985 if (b->val_bitsize != 0)
1986 {
1987 v = extract_bitfield_from_watchpoint_value (b, v);
1988 if (v != NULL)
1989 release_value (v);
1990 }
1991 b->val = v;
1992 b->val_valid = 1;
1993 }
1994
1995 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1996
1997 /* Look at each value on the value chain. */
1998 for (v = val_chain; v; v = value_next (v))
1999 {
2000 /* If it's a memory location, and GDB actually needed
2001 its contents to evaluate the expression, then we
2002 must watch it. If the first value returned is
2003 still lazy, that means an error occurred reading it;
2004 watch it anyway in case it becomes readable. */
2005 if (VALUE_LVAL (v) == lval_memory
2006 && (v == val_chain || ! value_lazy (v)))
2007 {
2008 struct type *vtype = check_typedef (value_type (v));
2009
2010 /* We only watch structs and arrays if user asked
2011 for it explicitly, never if they just happen to
2012 appear in the middle of some value chain. */
2013 if (v == result
2014 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
2015 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
2016 {
2017 CORE_ADDR addr;
2018 enum target_hw_bp_type type;
2019 struct bp_location *loc, **tmp;
2020 int bitpos = 0, bitsize = 0;
2021
2022 if (value_bitsize (v) != 0)
2023 {
2024 /* Extract the bit parameters out from the bitfield
2025 sub-expression. */
2026 bitpos = value_bitpos (v);
2027 bitsize = value_bitsize (v);
2028 }
2029 else if (v == result && b->val_bitsize != 0)
2030 {
2031 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
2032 lvalue whose bit parameters are saved in the fields
2033 VAL_BITPOS and VAL_BITSIZE. */
2034 bitpos = b->val_bitpos;
2035 bitsize = b->val_bitsize;
2036 }
2037
2038 addr = value_address (v);
2039 if (bitsize != 0)
2040 {
2041 /* Skip the bytes that don't contain the bitfield. */
2042 addr += bitpos / 8;
2043 }
2044
2045 type = hw_write;
2046 if (b->base.type == bp_read_watchpoint)
2047 type = hw_read;
2048 else if (b->base.type == bp_access_watchpoint)
2049 type = hw_access;
2050
2051 loc = allocate_bp_location (&b->base);
2052 for (tmp = &(b->base.loc); *tmp != NULL; tmp = &((*tmp)->next))
2053 ;
2054 *tmp = loc;
2055 loc->gdbarch = get_type_arch (value_type (v));
2056
2057 loc->pspace = frame_pspace;
2058 loc->address = addr;
2059
2060 if (bitsize != 0)
2061 {
2062 /* Just cover the bytes that make up the bitfield. */
2063 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2064 }
2065 else
2066 loc->length = TYPE_LENGTH (value_type (v));
2067
2068 loc->watchpoint_type = type;
2069 }
2070 }
2071 }
2072
2073 /* Change the type of breakpoint between hardware assisted or
2074 an ordinary watchpoint depending on the hardware support
2075 and free hardware slots. REPARSE is set when the inferior
2076 is started. */
2077 if (reparse)
2078 {
2079 int reg_cnt;
2080 enum bp_loc_type loc_type;
2081 struct bp_location *bl;
2082
2083 reg_cnt = can_use_hardware_watchpoint (val_chain);
2084
2085 if (reg_cnt)
2086 {
2087 int i, target_resources_ok, other_type_used;
2088 enum bptype type;
2089
2090 /* Use an exact watchpoint when there's only one memory region to be
2091 watched, and only one debug register is needed to watch it. */
2092 b->exact = target_exact_watchpoints && reg_cnt == 1;
2093
2094 /* We need to determine how many resources are already
2095 used for all other hardware watchpoints plus this one
2096 to see if we still have enough resources to also fit
2097 this watchpoint in as well. */
2098
2099 /* If this is a software watchpoint, we try to turn it
2100 to a hardware one -- count resources as if B was of
2101 hardware watchpoint type. */
2102 type = b->base.type;
2103 if (type == bp_watchpoint)
2104 type = bp_hardware_watchpoint;
2105
2106 /* This watchpoint may or may not have been placed on
2107 the list yet at this point (it won't be in the list
2108 if we're trying to create it for the first time,
2109 through watch_command), so always account for it
2110 manually. */
2111
2112 /* Count resources used by all watchpoints except B. */
2113 i = hw_watchpoint_used_count_others (&b->base, type, &other_type_used);
2114
2115 /* Add in the resources needed for B. */
2116 i += hw_watchpoint_use_count (&b->base);
2117
2118 target_resources_ok
2119 = target_can_use_hardware_watchpoint (type, i, other_type_used);
2120 if (target_resources_ok <= 0)
2121 {
2122 int sw_mode = b->base.ops->works_in_software_mode (&b->base);
2123
2124 if (target_resources_ok == 0 && !sw_mode)
2125 error (_("Target does not support this type of "
2126 "hardware watchpoint."));
2127 else if (target_resources_ok < 0 && !sw_mode)
2128 error (_("There are not enough available hardware "
2129 "resources for this watchpoint."));
2130
2131 /* Downgrade to software watchpoint. */
2132 b->base.type = bp_watchpoint;
2133 }
2134 else
2135 {
2136 /* If this was a software watchpoint, we've just
2137 found we have enough resources to turn it to a
2138 hardware watchpoint. Otherwise, this is a
2139 nop. */
2140 b->base.type = type;
2141 }
2142 }
2143 else if (!b->base.ops->works_in_software_mode (&b->base))
2144 {
2145 if (!can_use_hw_watchpoints)
2146 error (_("Can't set read/access watchpoint when "
2147 "hardware watchpoints are disabled."));
2148 else
2149 error (_("Expression cannot be implemented with "
2150 "read/access watchpoint."));
2151 }
2152 else
2153 b->base.type = bp_watchpoint;
2154
2155 loc_type = (b->base.type == bp_watchpoint? bp_loc_other
2156 : bp_loc_hardware_watchpoint);
2157 for (bl = b->base.loc; bl; bl = bl->next)
2158 bl->loc_type = loc_type;
2159 }
2160
2161 for (v = val_chain; v; v = next)
2162 {
2163 next = value_next (v);
2164 if (v != b->val)
2165 value_free (v);
2166 }
2167
2168 /* If a software watchpoint is not watching any memory, then the
2169 above left it without any location set up. But,
2170 bpstat_stop_status requires a location to be able to report
2171 stops, so make sure there's at least a dummy one. */
2172 if (b->base.type == bp_watchpoint && b->base.loc == NULL)
2173 software_watchpoint_add_no_memory_location (&b->base, frame_pspace);
2174 }
2175 else if (!within_current_scope)
2176 {
2177 printf_filtered (_("\
2178 Watchpoint %d deleted because the program has left the block\n\
2179 in which its expression is valid.\n"),
2180 b->base.number);
2181 watchpoint_del_at_next_stop (b);
2182 }
2183
2184 /* Restore the selected frame. */
2185 if (frame_saved)
2186 select_frame (frame_find_by_id (saved_frame_id));
2187 }
2188
2189
2190 /* Returns 1 iff breakpoint location should be
2191 inserted in the inferior. We don't differentiate the type of BL's owner
2192 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2193 breakpoint_ops is not defined, because in insert_bp_location,
2194 tracepoint's insert_location will not be called. */
2195 static int
2196 should_be_inserted (struct bp_location *bl)
2197 {
2198 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
2199 return 0;
2200
2201 if (bl->owner->disposition == disp_del_at_next_stop)
2202 return 0;
2203
2204 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
2205 return 0;
2206
2207 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2208 return 0;
2209
2210 /* This is set for example, when we're attached to the parent of a
2211 vfork, and have detached from the child. The child is running
2212 free, and we expect it to do an exec or exit, at which point the
2213 OS makes the parent schedulable again (and the target reports
2214 that the vfork is done). Until the child is done with the shared
2215 memory region, do not insert breakpoints in the parent, otherwise
2216 the child could still trip on the parent's breakpoints. Since
2217 the parent is blocked anyway, it won't miss any breakpoint. */
2218 if (bl->pspace->breakpoints_not_allowed)
2219 return 0;
2220
2221 /* Don't insert a breakpoint if we're trying to step past its
2222 location. */
2223 if ((bl->loc_type == bp_loc_software_breakpoint
2224 || bl->loc_type == bp_loc_hardware_breakpoint)
2225 && stepping_past_instruction_at (bl->pspace->aspace,
2226 bl->address))
2227 {
2228 if (debug_infrun)
2229 {
2230 fprintf_unfiltered (gdb_stdlog,
2231 "infrun: skipping breakpoint: "
2232 "stepping past insn at: %s\n",
2233 paddress (bl->gdbarch, bl->address));
2234 }
2235 return 0;
2236 }
2237
2238 /* Don't insert watchpoints if we're trying to step past the
2239 instruction that triggered one. */
2240 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2241 && stepping_past_nonsteppable_watchpoint ())
2242 {
2243 if (debug_infrun)
2244 {
2245 fprintf_unfiltered (gdb_stdlog,
2246 "infrun: stepping past non-steppable watchpoint. "
2247 "skipping watchpoint at %s:%d\n",
2248 paddress (bl->gdbarch, bl->address),
2249 bl->length);
2250 }
2251 return 0;
2252 }
2253
2254 return 1;
2255 }
2256
2257 /* Same as should_be_inserted but does the check assuming
2258 that the location is not duplicated. */
2259
2260 static int
2261 unduplicated_should_be_inserted (struct bp_location *bl)
2262 {
2263 int result;
2264 const int save_duplicate = bl->duplicate;
2265
2266 bl->duplicate = 0;
2267 result = should_be_inserted (bl);
2268 bl->duplicate = save_duplicate;
2269 return result;
2270 }
2271
2272 /* Parses a conditional described by an expression COND into an
2273 agent expression bytecode suitable for evaluation
2274 by the bytecode interpreter. Return NULL if there was
2275 any error during parsing. */
2276
2277 static struct agent_expr *
2278 parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2279 {
2280 struct agent_expr *aexpr = NULL;
2281
2282 if (!cond)
2283 return NULL;
2284
2285 /* We don't want to stop processing, so catch any errors
2286 that may show up. */
2287 TRY
2288 {
2289 aexpr = gen_eval_for_expr (scope, cond);
2290 }
2291
2292 CATCH (ex, RETURN_MASK_ERROR)
2293 {
2294 /* If we got here, it means the condition could not be parsed to a valid
2295 bytecode expression and thus can't be evaluated on the target's side.
2296 It's no use iterating through the conditions. */
2297 return NULL;
2298 }
2299 END_CATCH
2300
2301 /* We have a valid agent expression. */
2302 return aexpr;
2303 }
2304
2305 /* Based on location BL, create a list of breakpoint conditions to be
2306 passed on to the target. If we have duplicated locations with different
2307 conditions, we will add such conditions to the list. The idea is that the
2308 target will evaluate the list of conditions and will only notify GDB when
2309 one of them is true. */
2310
2311 static void
2312 build_target_condition_list (struct bp_location *bl)
2313 {
2314 struct bp_location **locp = NULL, **loc2p;
2315 int null_condition_or_parse_error = 0;
2316 int modified = bl->needs_update;
2317 struct bp_location *loc;
2318
2319 /* Release conditions left over from a previous insert. */
2320 VEC_free (agent_expr_p, bl->target_info.conditions);
2321
2322 /* This is only meaningful if the target is
2323 evaluating conditions and if the user has
2324 opted for condition evaluation on the target's
2325 side. */
2326 if (gdb_evaluates_breakpoint_condition_p ()
2327 || !target_supports_evaluation_of_breakpoint_conditions ())
2328 return;
2329
2330 /* Do a first pass to check for locations with no assigned
2331 conditions or conditions that fail to parse to a valid agent expression
2332 bytecode. If any of these happen, then it's no use to send conditions
2333 to the target since this location will always trigger and generate a
2334 response back to GDB. */
2335 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2336 {
2337 loc = (*loc2p);
2338 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2339 {
2340 if (modified)
2341 {
2342 struct agent_expr *aexpr;
2343
2344 /* Re-parse the conditions since something changed. In that
2345 case we already freed the condition bytecodes (see
2346 force_breakpoint_reinsertion). We just
2347 need to parse the condition to bytecodes again. */
2348 aexpr = parse_cond_to_aexpr (bl->address, loc->cond);
2349 loc->cond_bytecode = aexpr;
2350 }
2351
2352 /* If we have a NULL bytecode expression, it means something
2353 went wrong or we have a null condition expression. */
2354 if (!loc->cond_bytecode)
2355 {
2356 null_condition_or_parse_error = 1;
2357 break;
2358 }
2359 }
2360 }
2361
2362 /* If any of these happened, it means we will have to evaluate the conditions
2363 for the location's address on gdb's side. It is no use keeping bytecodes
2364 for all the other duplicate locations, thus we free all of them here.
2365
2366 This is so we have a finer control over which locations' conditions are
2367 being evaluated by GDB or the remote stub. */
2368 if (null_condition_or_parse_error)
2369 {
2370 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2371 {
2372 loc = (*loc2p);
2373 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2374 {
2375 /* Only go as far as the first NULL bytecode is
2376 located. */
2377 if (!loc->cond_bytecode)
2378 return;
2379
2380 free_agent_expr (loc->cond_bytecode);
2381 loc->cond_bytecode = NULL;
2382 }
2383 }
2384 }
2385
2386 /* No NULL conditions or failed bytecode generation. Build a condition list
2387 for this location's address. */
2388 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2389 {
2390 loc = (*loc2p);
2391 if (loc->cond
2392 && is_breakpoint (loc->owner)
2393 && loc->pspace->num == bl->pspace->num
2394 && loc->owner->enable_state == bp_enabled
2395 && loc->enabled)
2396 /* Add the condition to the vector. This will be used later to send the
2397 conditions to the target. */
2398 VEC_safe_push (agent_expr_p, bl->target_info.conditions,
2399 loc->cond_bytecode);
2400 }
2401
2402 return;
2403 }
2404
2405 /* Parses a command described by string CMD into an agent expression
2406 bytecode suitable for evaluation by the bytecode interpreter.
2407 Return NULL if there was any error during parsing. */
2408
2409 static struct agent_expr *
2410 parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2411 {
2412 struct cleanup *old_cleanups = 0;
2413 struct expression *expr, **argvec;
2414 struct agent_expr *aexpr = NULL;
2415 const char *cmdrest;
2416 const char *format_start, *format_end;
2417 struct format_piece *fpieces;
2418 int nargs;
2419 struct gdbarch *gdbarch = get_current_arch ();
2420
2421 if (!cmd)
2422 return NULL;
2423
2424 cmdrest = cmd;
2425
2426 if (*cmdrest == ',')
2427 ++cmdrest;
2428 cmdrest = skip_spaces_const (cmdrest);
2429
2430 if (*cmdrest++ != '"')
2431 error (_("No format string following the location"));
2432
2433 format_start = cmdrest;
2434
2435 fpieces = parse_format_string (&cmdrest);
2436
2437 old_cleanups = make_cleanup (free_format_pieces_cleanup, &fpieces);
2438
2439 format_end = cmdrest;
2440
2441 if (*cmdrest++ != '"')
2442 error (_("Bad format string, non-terminated '\"'."));
2443
2444 cmdrest = skip_spaces_const (cmdrest);
2445
2446 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2447 error (_("Invalid argument syntax"));
2448
2449 if (*cmdrest == ',')
2450 cmdrest++;
2451 cmdrest = skip_spaces_const (cmdrest);
2452
2453 /* For each argument, make an expression. */
2454
2455 argvec = (struct expression **) alloca (strlen (cmd)
2456 * sizeof (struct expression *));
2457
2458 nargs = 0;
2459 while (*cmdrest != '\0')
2460 {
2461 const char *cmd1;
2462
2463 cmd1 = cmdrest;
2464 expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2465 argvec[nargs++] = expr;
2466 cmdrest = cmd1;
2467 if (*cmdrest == ',')
2468 ++cmdrest;
2469 }
2470
2471 /* We don't want to stop processing, so catch any errors
2472 that may show up. */
2473 TRY
2474 {
2475 aexpr = gen_printf (scope, gdbarch, 0, 0,
2476 format_start, format_end - format_start,
2477 fpieces, nargs, argvec);
2478 }
2479 CATCH (ex, RETURN_MASK_ERROR)
2480 {
2481 /* If we got here, it means the command could not be parsed to a valid
2482 bytecode expression and thus can't be evaluated on the target's side.
2483 It's no use iterating through the other commands. */
2484 aexpr = NULL;
2485 }
2486 END_CATCH
2487
2488 do_cleanups (old_cleanups);
2489
2490 /* We have a valid agent expression, return it. */
2491 return aexpr;
2492 }
2493
2494 /* Based on location BL, create a list of breakpoint commands to be
2495 passed on to the target. If we have duplicated locations with
2496 different commands, we will add any such to the list. */
2497
2498 static void
2499 build_target_command_list (struct bp_location *bl)
2500 {
2501 struct bp_location **locp = NULL, **loc2p;
2502 int null_command_or_parse_error = 0;
2503 int modified = bl->needs_update;
2504 struct bp_location *loc;
2505
2506 /* Release commands left over from a previous insert. */
2507 VEC_free (agent_expr_p, bl->target_info.tcommands);
2508
2509 if (!target_can_run_breakpoint_commands ())
2510 return;
2511
2512 /* For now, limit to agent-style dprintf breakpoints. */
2513 if (dprintf_style != dprintf_style_agent)
2514 return;
2515
2516 /* For now, if we have any duplicate location that isn't a dprintf,
2517 don't install the target-side commands, as that would make the
2518 breakpoint not be reported to the core, and we'd lose
2519 control. */
2520 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2521 {
2522 loc = (*loc2p);
2523 if (is_breakpoint (loc->owner)
2524 && loc->pspace->num == bl->pspace->num
2525 && loc->owner->type != bp_dprintf)
2526 return;
2527 }
2528
2529 /* Do a first pass to check for locations with no assigned
2530 conditions or conditions that fail to parse to a valid agent expression
2531 bytecode. If any of these happen, then it's no use to send conditions
2532 to the target since this location will always trigger and generate a
2533 response back to GDB. */
2534 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2535 {
2536 loc = (*loc2p);
2537 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2538 {
2539 if (modified)
2540 {
2541 struct agent_expr *aexpr;
2542
2543 /* Re-parse the commands since something changed. In that
2544 case we already freed the command bytecodes (see
2545 force_breakpoint_reinsertion). We just
2546 need to parse the command to bytecodes again. */
2547 aexpr = parse_cmd_to_aexpr (bl->address,
2548 loc->owner->extra_string);
2549 loc->cmd_bytecode = aexpr;
2550 }
2551
2552 /* If we have a NULL bytecode expression, it means something
2553 went wrong or we have a null command expression. */
2554 if (!loc->cmd_bytecode)
2555 {
2556 null_command_or_parse_error = 1;
2557 break;
2558 }
2559 }
2560 }
2561
2562 /* If anything failed, then we're not doing target-side commands,
2563 and so clean up. */
2564 if (null_command_or_parse_error)
2565 {
2566 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2567 {
2568 loc = (*loc2p);
2569 if (is_breakpoint (loc->owner)
2570 && loc->pspace->num == bl->pspace->num)
2571 {
2572 /* Only go as far as the first NULL bytecode is
2573 located. */
2574 if (loc->cmd_bytecode == NULL)
2575 return;
2576
2577 free_agent_expr (loc->cmd_bytecode);
2578 loc->cmd_bytecode = NULL;
2579 }
2580 }
2581 }
2582
2583 /* No NULL commands or failed bytecode generation. Build a command list
2584 for this location's address. */
2585 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2586 {
2587 loc = (*loc2p);
2588 if (loc->owner->extra_string
2589 && is_breakpoint (loc->owner)
2590 && loc->pspace->num == bl->pspace->num
2591 && loc->owner->enable_state == bp_enabled
2592 && loc->enabled)
2593 /* Add the command to the vector. This will be used later
2594 to send the commands to the target. */
2595 VEC_safe_push (agent_expr_p, bl->target_info.tcommands,
2596 loc->cmd_bytecode);
2597 }
2598
2599 bl->target_info.persist = 0;
2600 /* Maybe flag this location as persistent. */
2601 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2602 bl->target_info.persist = 1;
2603 }
2604
2605 /* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2606 location. Any error messages are printed to TMP_ERROR_STREAM; and
2607 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
2608 Returns 0 for success, 1 if the bp_location type is not supported or
2609 -1 for failure.
2610
2611 NOTE drow/2003-09-09: This routine could be broken down to an
2612 object-style method for each breakpoint or catchpoint type. */
2613 static int
2614 insert_bp_location (struct bp_location *bl,
2615 struct ui_file *tmp_error_stream,
2616 int *disabled_breaks,
2617 int *hw_breakpoint_error,
2618 int *hw_bp_error_explained_already)
2619 {
2620 enum errors bp_err = GDB_NO_ERROR;
2621 const char *bp_err_message = NULL;
2622
2623 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2624 return 0;
2625
2626 /* Note we don't initialize bl->target_info, as that wipes out
2627 the breakpoint location's shadow_contents if the breakpoint
2628 is still inserted at that location. This in turn breaks
2629 target_read_memory which depends on these buffers when
2630 a memory read is requested at the breakpoint location:
2631 Once the target_info has been wiped, we fail to see that
2632 we have a breakpoint inserted at that address and thus
2633 read the breakpoint instead of returning the data saved in
2634 the breakpoint location's shadow contents. */
2635 bl->target_info.reqstd_address = bl->address;
2636 bl->target_info.placed_address_space = bl->pspace->aspace;
2637 bl->target_info.length = bl->length;
2638
2639 /* When working with target-side conditions, we must pass all the conditions
2640 for the same breakpoint address down to the target since GDB will not
2641 insert those locations. With a list of breakpoint conditions, the target
2642 can decide when to stop and notify GDB. */
2643
2644 if (is_breakpoint (bl->owner))
2645 {
2646 build_target_condition_list (bl);
2647 build_target_command_list (bl);
2648 /* Reset the modification marker. */
2649 bl->needs_update = 0;
2650 }
2651
2652 if (bl->loc_type == bp_loc_software_breakpoint
2653 || bl->loc_type == bp_loc_hardware_breakpoint)
2654 {
2655 if (bl->owner->type != bp_hardware_breakpoint)
2656 {
2657 /* If the explicitly specified breakpoint type
2658 is not hardware breakpoint, check the memory map to see
2659 if the breakpoint address is in read only memory or not.
2660
2661 Two important cases are:
2662 - location type is not hardware breakpoint, memory
2663 is readonly. We change the type of the location to
2664 hardware breakpoint.
2665 - location type is hardware breakpoint, memory is
2666 read-write. This means we've previously made the
2667 location hardware one, but then the memory map changed,
2668 so we undo.
2669
2670 When breakpoints are removed, remove_breakpoints will use
2671 location types we've just set here, the only possible
2672 problem is that memory map has changed during running
2673 program, but it's not going to work anyway with current
2674 gdb. */
2675 struct mem_region *mr
2676 = lookup_mem_region (bl->target_info.reqstd_address);
2677
2678 if (mr)
2679 {
2680 if (automatic_hardware_breakpoints)
2681 {
2682 enum bp_loc_type new_type;
2683
2684 if (mr->attrib.mode != MEM_RW)
2685 new_type = bp_loc_hardware_breakpoint;
2686 else
2687 new_type = bp_loc_software_breakpoint;
2688
2689 if (new_type != bl->loc_type)
2690 {
2691 static int said = 0;
2692
2693 bl->loc_type = new_type;
2694 if (!said)
2695 {
2696 fprintf_filtered (gdb_stdout,
2697 _("Note: automatically using "
2698 "hardware breakpoints for "
2699 "read-only addresses.\n"));
2700 said = 1;
2701 }
2702 }
2703 }
2704 else if (bl->loc_type == bp_loc_software_breakpoint
2705 && mr->attrib.mode != MEM_RW)
2706 {
2707 fprintf_unfiltered (tmp_error_stream,
2708 _("Cannot insert breakpoint %d.\n"
2709 "Cannot set software breakpoint "
2710 "at read-only address %s\n"),
2711 bl->owner->number,
2712 paddress (bl->gdbarch, bl->address));
2713 return 1;
2714 }
2715 }
2716 }
2717
2718 /* First check to see if we have to handle an overlay. */
2719 if (overlay_debugging == ovly_off
2720 || bl->section == NULL
2721 || !(section_is_overlay (bl->section)))
2722 {
2723 /* No overlay handling: just set the breakpoint. */
2724 TRY
2725 {
2726 int val;
2727
2728 val = bl->owner->ops->insert_location (bl);
2729 if (val)
2730 bp_err = GENERIC_ERROR;
2731 }
2732 CATCH (e, RETURN_MASK_ALL)
2733 {
2734 bp_err = e.error;
2735 bp_err_message = e.message;
2736 }
2737 END_CATCH
2738 }
2739 else
2740 {
2741 /* This breakpoint is in an overlay section.
2742 Shall we set a breakpoint at the LMA? */
2743 if (!overlay_events_enabled)
2744 {
2745 /* Yes -- overlay event support is not active,
2746 so we must try to set a breakpoint at the LMA.
2747 This will not work for a hardware breakpoint. */
2748 if (bl->loc_type == bp_loc_hardware_breakpoint)
2749 warning (_("hardware breakpoint %d not supported in overlay!"),
2750 bl->owner->number);
2751 else
2752 {
2753 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2754 bl->section);
2755 /* Set a software (trap) breakpoint at the LMA. */
2756 bl->overlay_target_info = bl->target_info;
2757 bl->overlay_target_info.reqstd_address = addr;
2758
2759 /* No overlay handling: just set the breakpoint. */
2760 TRY
2761 {
2762 int val;
2763
2764 val = target_insert_breakpoint (bl->gdbarch,
2765 &bl->overlay_target_info);
2766 if (val)
2767 bp_err = GENERIC_ERROR;
2768 }
2769 CATCH (e, RETURN_MASK_ALL)
2770 {
2771 bp_err = e.error;
2772 bp_err_message = e.message;
2773 }
2774 END_CATCH
2775
2776 if (bp_err != GDB_NO_ERROR)
2777 fprintf_unfiltered (tmp_error_stream,
2778 "Overlay breakpoint %d "
2779 "failed: in ROM?\n",
2780 bl->owner->number);
2781 }
2782 }
2783 /* Shall we set a breakpoint at the VMA? */
2784 if (section_is_mapped (bl->section))
2785 {
2786 /* Yes. This overlay section is mapped into memory. */
2787 TRY
2788 {
2789 int val;
2790
2791 val = bl->owner->ops->insert_location (bl);
2792 if (val)
2793 bp_err = GENERIC_ERROR;
2794 }
2795 CATCH (e, RETURN_MASK_ALL)
2796 {
2797 bp_err = e.error;
2798 bp_err_message = e.message;
2799 }
2800 END_CATCH
2801 }
2802 else
2803 {
2804 /* No. This breakpoint will not be inserted.
2805 No error, but do not mark the bp as 'inserted'. */
2806 return 0;
2807 }
2808 }
2809
2810 if (bp_err != GDB_NO_ERROR)
2811 {
2812 /* Can't set the breakpoint. */
2813
2814 /* In some cases, we might not be able to insert a
2815 breakpoint in a shared library that has already been
2816 removed, but we have not yet processed the shlib unload
2817 event. Unfortunately, some targets that implement
2818 breakpoint insertion themselves can't tell why the
2819 breakpoint insertion failed (e.g., the remote target
2820 doesn't define error codes), so we must treat generic
2821 errors as memory errors. */
2822 if ((bp_err == GENERIC_ERROR || bp_err == MEMORY_ERROR)
2823 && bl->loc_type == bp_loc_software_breakpoint
2824 && (solib_name_from_address (bl->pspace, bl->address)
2825 || shared_objfile_contains_address_p (bl->pspace,
2826 bl->address)))
2827 {
2828 /* See also: disable_breakpoints_in_shlibs. */
2829 bl->shlib_disabled = 1;
2830 observer_notify_breakpoint_modified (bl->owner);
2831 if (!*disabled_breaks)
2832 {
2833 fprintf_unfiltered (tmp_error_stream,
2834 "Cannot insert breakpoint %d.\n",
2835 bl->owner->number);
2836 fprintf_unfiltered (tmp_error_stream,
2837 "Temporarily disabling shared "
2838 "library breakpoints:\n");
2839 }
2840 *disabled_breaks = 1;
2841 fprintf_unfiltered (tmp_error_stream,
2842 "breakpoint #%d\n", bl->owner->number);
2843 return 0;
2844 }
2845 else
2846 {
2847 if (bl->loc_type == bp_loc_hardware_breakpoint)
2848 {
2849 *hw_breakpoint_error = 1;
2850 *hw_bp_error_explained_already = bp_err_message != NULL;
2851 fprintf_unfiltered (tmp_error_stream,
2852 "Cannot insert hardware breakpoint %d%s",
2853 bl->owner->number, bp_err_message ? ":" : ".\n");
2854 if (bp_err_message != NULL)
2855 fprintf_unfiltered (tmp_error_stream, "%s.\n", bp_err_message);
2856 }
2857 else
2858 {
2859 if (bp_err_message == NULL)
2860 {
2861 char *message
2862 = memory_error_message (TARGET_XFER_E_IO,
2863 bl->gdbarch, bl->address);
2864 struct cleanup *old_chain = make_cleanup (xfree, message);
2865
2866 fprintf_unfiltered (tmp_error_stream,
2867 "Cannot insert breakpoint %d.\n"
2868 "%s\n",
2869 bl->owner->number, message);
2870 do_cleanups (old_chain);
2871 }
2872 else
2873 {
2874 fprintf_unfiltered (tmp_error_stream,
2875 "Cannot insert breakpoint %d: %s\n",
2876 bl->owner->number,
2877 bp_err_message);
2878 }
2879 }
2880 return 1;
2881
2882 }
2883 }
2884 else
2885 bl->inserted = 1;
2886
2887 return 0;
2888 }
2889
2890 else if (bl->loc_type == bp_loc_hardware_watchpoint
2891 /* NOTE drow/2003-09-08: This state only exists for removing
2892 watchpoints. It's not clear that it's necessary... */
2893 && bl->owner->disposition != disp_del_at_next_stop)
2894 {
2895 int val;
2896
2897 gdb_assert (bl->owner->ops != NULL
2898 && bl->owner->ops->insert_location != NULL);
2899
2900 val = bl->owner->ops->insert_location (bl);
2901
2902 /* If trying to set a read-watchpoint, and it turns out it's not
2903 supported, try emulating one with an access watchpoint. */
2904 if (val == 1 && bl->watchpoint_type == hw_read)
2905 {
2906 struct bp_location *loc, **loc_temp;
2907
2908 /* But don't try to insert it, if there's already another
2909 hw_access location that would be considered a duplicate
2910 of this one. */
2911 ALL_BP_LOCATIONS (loc, loc_temp)
2912 if (loc != bl
2913 && loc->watchpoint_type == hw_access
2914 && watchpoint_locations_match (bl, loc))
2915 {
2916 bl->duplicate = 1;
2917 bl->inserted = 1;
2918 bl->target_info = loc->target_info;
2919 bl->watchpoint_type = hw_access;
2920 val = 0;
2921 break;
2922 }
2923
2924 if (val == 1)
2925 {
2926 bl->watchpoint_type = hw_access;
2927 val = bl->owner->ops->insert_location (bl);
2928
2929 if (val)
2930 /* Back to the original value. */
2931 bl->watchpoint_type = hw_read;
2932 }
2933 }
2934
2935 bl->inserted = (val == 0);
2936 }
2937
2938 else if (bl->owner->type == bp_catchpoint)
2939 {
2940 int val;
2941
2942 gdb_assert (bl->owner->ops != NULL
2943 && bl->owner->ops->insert_location != NULL);
2944
2945 val = bl->owner->ops->insert_location (bl);
2946 if (val)
2947 {
2948 bl->owner->enable_state = bp_disabled;
2949
2950 if (val == 1)
2951 warning (_("\
2952 Error inserting catchpoint %d: Your system does not support this type\n\
2953 of catchpoint."), bl->owner->number);
2954 else
2955 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2956 }
2957
2958 bl->inserted = (val == 0);
2959
2960 /* We've already printed an error message if there was a problem
2961 inserting this catchpoint, and we've disabled the catchpoint,
2962 so just return success. */
2963 return 0;
2964 }
2965
2966 return 0;
2967 }
2968
2969 /* This function is called when program space PSPACE is about to be
2970 deleted. It takes care of updating breakpoints to not reference
2971 PSPACE anymore. */
2972
2973 void
2974 breakpoint_program_space_exit (struct program_space *pspace)
2975 {
2976 struct breakpoint *b, *b_temp;
2977 struct bp_location *loc, **loc_temp;
2978
2979 /* Remove any breakpoint that was set through this program space. */
2980 ALL_BREAKPOINTS_SAFE (b, b_temp)
2981 {
2982 if (b->pspace == pspace)
2983 delete_breakpoint (b);
2984 }
2985
2986 /* Breakpoints set through other program spaces could have locations
2987 bound to PSPACE as well. Remove those. */
2988 ALL_BP_LOCATIONS (loc, loc_temp)
2989 {
2990 struct bp_location *tmp;
2991
2992 if (loc->pspace == pspace)
2993 {
2994 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
2995 if (loc->owner->loc == loc)
2996 loc->owner->loc = loc->next;
2997 else
2998 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2999 if (tmp->next == loc)
3000 {
3001 tmp->next = loc->next;
3002 break;
3003 }
3004 }
3005 }
3006
3007 /* Now update the global location list to permanently delete the
3008 removed locations above. */
3009 update_global_location_list (UGLL_DONT_INSERT);
3010 }
3011
3012 /* Make sure all breakpoints are inserted in inferior.
3013 Throws exception on any error.
3014 A breakpoint that is already inserted won't be inserted
3015 again, so calling this function twice is safe. */
3016 void
3017 insert_breakpoints (void)
3018 {
3019 struct breakpoint *bpt;
3020
3021 ALL_BREAKPOINTS (bpt)
3022 if (is_hardware_watchpoint (bpt))
3023 {
3024 struct watchpoint *w = (struct watchpoint *) bpt;
3025
3026 update_watchpoint (w, 0 /* don't reparse. */);
3027 }
3028
3029 /* Updating watchpoints creates new locations, so update the global
3030 location list. Explicitly tell ugll to insert locations and
3031 ignore breakpoints_always_inserted_mode. */
3032 update_global_location_list (UGLL_INSERT);
3033 }
3034
3035 /* Invoke CALLBACK for each of bp_location. */
3036
3037 void
3038 iterate_over_bp_locations (walk_bp_location_callback callback)
3039 {
3040 struct bp_location *loc, **loc_tmp;
3041
3042 ALL_BP_LOCATIONS (loc, loc_tmp)
3043 {
3044 callback (loc, NULL);
3045 }
3046 }
3047
3048 /* This is used when we need to synch breakpoint conditions between GDB and the
3049 target. It is the case with deleting and disabling of breakpoints when using
3050 always-inserted mode. */
3051
3052 static void
3053 update_inserted_breakpoint_locations (void)
3054 {
3055 struct bp_location *bl, **blp_tmp;
3056 int error_flag = 0;
3057 int val = 0;
3058 int disabled_breaks = 0;
3059 int hw_breakpoint_error = 0;
3060 int hw_bp_details_reported = 0;
3061
3062 struct ui_file *tmp_error_stream = mem_fileopen ();
3063 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
3064
3065 /* Explicitly mark the warning -- this will only be printed if
3066 there was an error. */
3067 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
3068
3069 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 struct ui_file *tmp_error_stream = mem_fileopen ();
3123 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
3124
3125 /* Explicitly mark the warning -- this will only be printed if
3126 there was an error. */
3127 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
3128
3129 save_current_space_and_thread ();
3130
3131 ALL_BP_LOCATIONS (bl, blp_tmp)
3132 {
3133 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
3134 continue;
3135
3136 /* There is no point inserting thread-specific breakpoints if
3137 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3138 has BL->OWNER always non-NULL. */
3139 if (bl->owner->thread != -1
3140 && !valid_global_thread_id (bl->owner->thread))
3141 continue;
3142
3143 switch_to_program_space_and_thread (bl->pspace);
3144
3145 /* For targets that support global breakpoints, there's no need
3146 to select an inferior to insert breakpoint to. In fact, even
3147 if we aren't attached to any process yet, we should still
3148 insert breakpoints. */
3149 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3150 && ptid_equal (inferior_ptid, null_ptid))
3151 continue;
3152
3153 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
3154 &hw_breakpoint_error, &hw_bp_error_explained_already);
3155 if (val)
3156 error_flag = val;
3157 }
3158
3159 /* If we failed to insert all locations of a watchpoint, remove
3160 them, as half-inserted watchpoint is of limited use. */
3161 ALL_BREAKPOINTS (bpt)
3162 {
3163 int some_failed = 0;
3164 struct bp_location *loc;
3165
3166 if (!is_hardware_watchpoint (bpt))
3167 continue;
3168
3169 if (!breakpoint_enabled (bpt))
3170 continue;
3171
3172 if (bpt->disposition == disp_del_at_next_stop)
3173 continue;
3174
3175 for (loc = bpt->loc; loc; loc = loc->next)
3176 if (!loc->inserted && should_be_inserted (loc))
3177 {
3178 some_failed = 1;
3179 break;
3180 }
3181 if (some_failed)
3182 {
3183 for (loc = bpt->loc; loc; loc = loc->next)
3184 if (loc->inserted)
3185 remove_breakpoint (loc, mark_uninserted);
3186
3187 hw_breakpoint_error = 1;
3188 fprintf_unfiltered (tmp_error_stream,
3189 "Could not insert hardware watchpoint %d.\n",
3190 bpt->number);
3191 error_flag = -1;
3192 }
3193 }
3194
3195 if (error_flag)
3196 {
3197 /* If a hardware breakpoint or watchpoint was inserted, add a
3198 message about possibly exhausted resources. */
3199 if (hw_breakpoint_error && !hw_bp_error_explained_already)
3200 {
3201 fprintf_unfiltered (tmp_error_stream,
3202 "Could not insert hardware breakpoints:\n\
3203 You may have requested too many hardware breakpoints/watchpoints.\n");
3204 }
3205 target_terminal_ours_for_output ();
3206 error_stream (tmp_error_stream);
3207 }
3208
3209 do_cleanups (cleanups);
3210 }
3211
3212 /* Used when the program stops.
3213 Returns zero if successful, or non-zero if there was a problem
3214 removing a breakpoint location. */
3215
3216 int
3217 remove_breakpoints (void)
3218 {
3219 struct bp_location *bl, **blp_tmp;
3220 int val = 0;
3221
3222 ALL_BP_LOCATIONS (bl, blp_tmp)
3223 {
3224 if (bl->inserted && !is_tracepoint (bl->owner))
3225 val |= remove_breakpoint (bl, mark_uninserted);
3226 }
3227 return val;
3228 }
3229
3230 /* When a thread exits, remove breakpoints that are related to
3231 that thread. */
3232
3233 static void
3234 remove_threaded_breakpoints (struct thread_info *tp, int silent)
3235 {
3236 struct breakpoint *b, *b_tmp;
3237
3238 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3239 {
3240 if (b->thread == tp->global_num && user_breakpoint_p (b))
3241 {
3242 b->disposition = disp_del_at_next_stop;
3243
3244 printf_filtered (_("\
3245 Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3246 b->number, print_thread_id (tp));
3247
3248 /* Hide it from the user. */
3249 b->number = 0;
3250 }
3251 }
3252 }
3253
3254 /* Remove breakpoints of process PID. */
3255
3256 int
3257 remove_breakpoints_pid (int pid)
3258 {
3259 struct bp_location *bl, **blp_tmp;
3260 int val;
3261 struct inferior *inf = find_inferior_pid (pid);
3262
3263 ALL_BP_LOCATIONS (bl, blp_tmp)
3264 {
3265 if (bl->pspace != inf->pspace)
3266 continue;
3267
3268 if (bl->inserted && !bl->target_info.persist)
3269 {
3270 val = remove_breakpoint (bl, mark_uninserted);
3271 if (val != 0)
3272 return val;
3273 }
3274 }
3275 return 0;
3276 }
3277
3278 int
3279 reattach_breakpoints (int pid)
3280 {
3281 struct cleanup *old_chain;
3282 struct bp_location *bl, **blp_tmp;
3283 int val;
3284 struct ui_file *tmp_error_stream;
3285 int dummy1 = 0, dummy2 = 0, dummy3 = 0;
3286 struct inferior *inf;
3287 struct thread_info *tp;
3288
3289 tp = any_live_thread_of_process (pid);
3290 if (tp == NULL)
3291 return 1;
3292
3293 inf = find_inferior_pid (pid);
3294 old_chain = save_inferior_ptid ();
3295
3296 inferior_ptid = tp->ptid;
3297
3298 tmp_error_stream = mem_fileopen ();
3299 make_cleanup_ui_file_delete (tmp_error_stream);
3300
3301 ALL_BP_LOCATIONS (bl, blp_tmp)
3302 {
3303 if (bl->pspace != inf->pspace)
3304 continue;
3305
3306 if (bl->inserted)
3307 {
3308 bl->inserted = 0;
3309 val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2, &dummy3);
3310 if (val != 0)
3311 {
3312 do_cleanups (old_chain);
3313 return val;
3314 }
3315 }
3316 }
3317 do_cleanups (old_chain);
3318 return 0;
3319 }
3320
3321 static int internal_breakpoint_number = -1;
3322
3323 /* Set the breakpoint number of B, depending on the value of INTERNAL.
3324 If INTERNAL is non-zero, the breakpoint number will be populated
3325 from internal_breakpoint_number and that variable decremented.
3326 Otherwise the breakpoint number will be populated from
3327 breakpoint_count and that value incremented. Internal breakpoints
3328 do not set the internal var bpnum. */
3329 static void
3330 set_breakpoint_number (int internal, struct breakpoint *b)
3331 {
3332 if (internal)
3333 b->number = internal_breakpoint_number--;
3334 else
3335 {
3336 set_breakpoint_count (breakpoint_count + 1);
3337 b->number = breakpoint_count;
3338 }
3339 }
3340
3341 static struct breakpoint *
3342 create_internal_breakpoint (struct gdbarch *gdbarch,
3343 CORE_ADDR address, enum bptype type,
3344 const struct breakpoint_ops *ops)
3345 {
3346 struct symtab_and_line sal;
3347 struct breakpoint *b;
3348
3349 init_sal (&sal); /* Initialize to zeroes. */
3350
3351 sal.pc = address;
3352 sal.section = find_pc_overlay (sal.pc);
3353 sal.pspace = current_program_space;
3354
3355 b = set_raw_breakpoint (gdbarch, sal, type, ops);
3356 b->number = internal_breakpoint_number--;
3357 b->disposition = disp_donttouch;
3358
3359 return b;
3360 }
3361
3362 static const char *const longjmp_names[] =
3363 {
3364 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3365 };
3366 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3367
3368 /* Per-objfile data private to breakpoint.c. */
3369 struct breakpoint_objfile_data
3370 {
3371 /* Minimal symbol for "_ovly_debug_event" (if any). */
3372 struct bound_minimal_symbol overlay_msym;
3373
3374 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3375 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
3376
3377 /* True if we have looked for longjmp probes. */
3378 int longjmp_searched;
3379
3380 /* SystemTap probe points for longjmp (if any). */
3381 VEC (probe_p) *longjmp_probes;
3382
3383 /* Minimal symbol for "std::terminate()" (if any). */
3384 struct bound_minimal_symbol terminate_msym;
3385
3386 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3387 struct bound_minimal_symbol exception_msym;
3388
3389 /* True if we have looked for exception probes. */
3390 int exception_searched;
3391
3392 /* SystemTap probe points for unwinding (if any). */
3393 VEC (probe_p) *exception_probes;
3394 };
3395
3396 static const struct objfile_data *breakpoint_objfile_key;
3397
3398 /* Minimal symbol not found sentinel. */
3399 static struct minimal_symbol msym_not_found;
3400
3401 /* Returns TRUE if MSYM point to the "not found" sentinel. */
3402
3403 static int
3404 msym_not_found_p (const struct minimal_symbol *msym)
3405 {
3406 return msym == &msym_not_found;
3407 }
3408
3409 /* Return per-objfile data needed by breakpoint.c.
3410 Allocate the data if necessary. */
3411
3412 static struct breakpoint_objfile_data *
3413 get_breakpoint_objfile_data (struct objfile *objfile)
3414 {
3415 struct breakpoint_objfile_data *bp_objfile_data;
3416
3417 bp_objfile_data = ((struct breakpoint_objfile_data *)
3418 objfile_data (objfile, breakpoint_objfile_key));
3419 if (bp_objfile_data == NULL)
3420 {
3421 bp_objfile_data =
3422 XOBNEW (&objfile->objfile_obstack, struct breakpoint_objfile_data);
3423
3424 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
3425 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3426 }
3427 return bp_objfile_data;
3428 }
3429
3430 static void
3431 free_breakpoint_probes (struct objfile *obj, void *data)
3432 {
3433 struct breakpoint_objfile_data *bp_objfile_data
3434 = (struct breakpoint_objfile_data *) data;
3435
3436 VEC_free (probe_p, bp_objfile_data->longjmp_probes);
3437 VEC_free (probe_p, bp_objfile_data->exception_probes);
3438 }
3439
3440 static void
3441 create_overlay_event_breakpoint (void)
3442 {
3443 struct objfile *objfile;
3444 const char *const func_name = "_ovly_debug_event";
3445
3446 ALL_OBJFILES (objfile)
3447 {
3448 struct breakpoint *b;
3449 struct breakpoint_objfile_data *bp_objfile_data;
3450 CORE_ADDR addr;
3451 struct explicit_location explicit_loc;
3452
3453 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3454
3455 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
3456 continue;
3457
3458 if (bp_objfile_data->overlay_msym.minsym == NULL)
3459 {
3460 struct bound_minimal_symbol m;
3461
3462 m = lookup_minimal_symbol_text (func_name, objfile);
3463 if (m.minsym == NULL)
3464 {
3465 /* Avoid future lookups in this objfile. */
3466 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
3467 continue;
3468 }
3469 bp_objfile_data->overlay_msym = m;
3470 }
3471
3472 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
3473 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3474 bp_overlay_event,
3475 &internal_breakpoint_ops);
3476 initialize_explicit_location (&explicit_loc);
3477 explicit_loc.function_name = ASTRDUP (func_name);
3478 b->location = new_explicit_location (&explicit_loc);
3479
3480 if (overlay_debugging == ovly_auto)
3481 {
3482 b->enable_state = bp_enabled;
3483 overlay_events_enabled = 1;
3484 }
3485 else
3486 {
3487 b->enable_state = bp_disabled;
3488 overlay_events_enabled = 0;
3489 }
3490 }
3491 }
3492
3493 static void
3494 create_longjmp_master_breakpoint (void)
3495 {
3496 struct program_space *pspace;
3497 struct cleanup *old_chain;
3498
3499 old_chain = save_current_program_space ();
3500
3501 ALL_PSPACES (pspace)
3502 {
3503 struct objfile *objfile;
3504
3505 set_current_program_space (pspace);
3506
3507 ALL_OBJFILES (objfile)
3508 {
3509 int i;
3510 struct gdbarch *gdbarch;
3511 struct breakpoint_objfile_data *bp_objfile_data;
3512
3513 gdbarch = get_objfile_arch (objfile);
3514
3515 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3516
3517 if (!bp_objfile_data->longjmp_searched)
3518 {
3519 VEC (probe_p) *ret;
3520
3521 ret = find_probes_in_objfile (objfile, "libc", "longjmp");
3522 if (ret != NULL)
3523 {
3524 /* We are only interested in checking one element. */
3525 struct probe *p = VEC_index (probe_p, ret, 0);
3526
3527 if (!can_evaluate_probe_arguments (p))
3528 {
3529 /* We cannot use the probe interface here, because it does
3530 not know how to evaluate arguments. */
3531 VEC_free (probe_p, ret);
3532 ret = NULL;
3533 }
3534 }
3535 bp_objfile_data->longjmp_probes = ret;
3536 bp_objfile_data->longjmp_searched = 1;
3537 }
3538
3539 if (bp_objfile_data->longjmp_probes != NULL)
3540 {
3541 int i;
3542 struct probe *probe;
3543 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3544
3545 for (i = 0;
3546 VEC_iterate (probe_p,
3547 bp_objfile_data->longjmp_probes,
3548 i, probe);
3549 ++i)
3550 {
3551 struct breakpoint *b;
3552
3553 b = create_internal_breakpoint (gdbarch,
3554 get_probe_address (probe,
3555 objfile),
3556 bp_longjmp_master,
3557 &internal_breakpoint_ops);
3558 b->location
3559 = new_probe_location ("-probe-stap libc:longjmp");
3560 b->enable_state = bp_disabled;
3561 }
3562
3563 continue;
3564 }
3565
3566 if (!gdbarch_get_longjmp_target_p (gdbarch))
3567 continue;
3568
3569 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
3570 {
3571 struct breakpoint *b;
3572 const char *func_name;
3573 CORE_ADDR addr;
3574 struct explicit_location explicit_loc;
3575
3576 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3577 continue;
3578
3579 func_name = longjmp_names[i];
3580 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3581 {
3582 struct bound_minimal_symbol m;
3583
3584 m = lookup_minimal_symbol_text (func_name, objfile);
3585 if (m.minsym == NULL)
3586 {
3587 /* Prevent future lookups in this objfile. */
3588 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
3589 continue;
3590 }
3591 bp_objfile_data->longjmp_msym[i] = m;
3592 }
3593
3594 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
3595 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3596 &internal_breakpoint_ops);
3597 initialize_explicit_location (&explicit_loc);
3598 explicit_loc.function_name = ASTRDUP (func_name);
3599 b->location = new_explicit_location (&explicit_loc);
3600 b->enable_state = bp_disabled;
3601 }
3602 }
3603 }
3604
3605 do_cleanups (old_chain);
3606 }
3607
3608 /* Create a master std::terminate breakpoint. */
3609 static void
3610 create_std_terminate_master_breakpoint (void)
3611 {
3612 struct program_space *pspace;
3613 struct cleanup *old_chain;
3614 const char *const func_name = "std::terminate()";
3615
3616 old_chain = save_current_program_space ();
3617
3618 ALL_PSPACES (pspace)
3619 {
3620 struct objfile *objfile;
3621 CORE_ADDR addr;
3622
3623 set_current_program_space (pspace);
3624
3625 ALL_OBJFILES (objfile)
3626 {
3627 struct breakpoint *b;
3628 struct breakpoint_objfile_data *bp_objfile_data;
3629 struct explicit_location explicit_loc;
3630
3631 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3632
3633 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3634 continue;
3635
3636 if (bp_objfile_data->terminate_msym.minsym == NULL)
3637 {
3638 struct bound_minimal_symbol m;
3639
3640 m = lookup_minimal_symbol (func_name, NULL, objfile);
3641 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3642 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
3643 {
3644 /* Prevent future lookups in this objfile. */
3645 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3646 continue;
3647 }
3648 bp_objfile_data->terminate_msym = m;
3649 }
3650
3651 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3652 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3653 bp_std_terminate_master,
3654 &internal_breakpoint_ops);
3655 initialize_explicit_location (&explicit_loc);
3656 explicit_loc.function_name = ASTRDUP (func_name);
3657 b->location = new_explicit_location (&explicit_loc);
3658 b->enable_state = bp_disabled;
3659 }
3660 }
3661
3662 do_cleanups (old_chain);
3663 }
3664
3665 /* Install a master breakpoint on the unwinder's debug hook. */
3666
3667 static void
3668 create_exception_master_breakpoint (void)
3669 {
3670 struct objfile *objfile;
3671 const char *const func_name = "_Unwind_DebugHook";
3672
3673 ALL_OBJFILES (objfile)
3674 {
3675 struct breakpoint *b;
3676 struct gdbarch *gdbarch;
3677 struct breakpoint_objfile_data *bp_objfile_data;
3678 CORE_ADDR addr;
3679 struct explicit_location explicit_loc;
3680
3681 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3682
3683 /* We prefer the SystemTap probe point if it exists. */
3684 if (!bp_objfile_data->exception_searched)
3685 {
3686 VEC (probe_p) *ret;
3687
3688 ret = find_probes_in_objfile (objfile, "libgcc", "unwind");
3689
3690 if (ret != NULL)
3691 {
3692 /* We are only interested in checking one element. */
3693 struct probe *p = VEC_index (probe_p, ret, 0);
3694
3695 if (!can_evaluate_probe_arguments (p))
3696 {
3697 /* We cannot use the probe interface here, because it does
3698 not know how to evaluate arguments. */
3699 VEC_free (probe_p, ret);
3700 ret = NULL;
3701 }
3702 }
3703 bp_objfile_data->exception_probes = ret;
3704 bp_objfile_data->exception_searched = 1;
3705 }
3706
3707 if (bp_objfile_data->exception_probes != NULL)
3708 {
3709 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3710 int i;
3711 struct probe *probe;
3712
3713 for (i = 0;
3714 VEC_iterate (probe_p,
3715 bp_objfile_data->exception_probes,
3716 i, probe);
3717 ++i)
3718 {
3719 struct breakpoint *b;
3720
3721 b = create_internal_breakpoint (gdbarch,
3722 get_probe_address (probe,
3723 objfile),
3724 bp_exception_master,
3725 &internal_breakpoint_ops);
3726 b->location
3727 = new_probe_location ("-probe-stap libgcc:unwind");
3728 b->enable_state = bp_disabled;
3729 }
3730
3731 continue;
3732 }
3733
3734 /* Otherwise, try the hook function. */
3735
3736 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3737 continue;
3738
3739 gdbarch = get_objfile_arch (objfile);
3740
3741 if (bp_objfile_data->exception_msym.minsym == NULL)
3742 {
3743 struct bound_minimal_symbol debug_hook;
3744
3745 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3746 if (debug_hook.minsym == NULL)
3747 {
3748 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3749 continue;
3750 }
3751
3752 bp_objfile_data->exception_msym = debug_hook;
3753 }
3754
3755 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
3756 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3757 &current_target);
3758 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3759 &internal_breakpoint_ops);
3760 initialize_explicit_location (&explicit_loc);
3761 explicit_loc.function_name = ASTRDUP (func_name);
3762 b->location = new_explicit_location (&explicit_loc);
3763 b->enable_state = bp_disabled;
3764 }
3765 }
3766
3767 /* Does B have a location spec? */
3768
3769 static int
3770 breakpoint_event_location_empty_p (const struct breakpoint *b)
3771 {
3772 return b->location != NULL && event_location_empty_p (b->location);
3773 }
3774
3775 void
3776 update_breakpoints_after_exec (void)
3777 {
3778 struct breakpoint *b, *b_tmp;
3779 struct bp_location *bploc, **bplocp_tmp;
3780
3781 /* We're about to delete breakpoints from GDB's lists. If the
3782 INSERTED flag is true, GDB will try to lift the breakpoints by
3783 writing the breakpoints' "shadow contents" back into memory. The
3784 "shadow contents" are NOT valid after an exec, so GDB should not
3785 do that. Instead, the target is responsible from marking
3786 breakpoints out as soon as it detects an exec. We don't do that
3787 here instead, because there may be other attempts to delete
3788 breakpoints after detecting an exec and before reaching here. */
3789 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
3790 if (bploc->pspace == current_program_space)
3791 gdb_assert (!bploc->inserted);
3792
3793 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3794 {
3795 if (b->pspace != current_program_space)
3796 continue;
3797
3798 /* Solib breakpoints must be explicitly reset after an exec(). */
3799 if (b->type == bp_shlib_event)
3800 {
3801 delete_breakpoint (b);
3802 continue;
3803 }
3804
3805 /* JIT breakpoints must be explicitly reset after an exec(). */
3806 if (b->type == bp_jit_event)
3807 {
3808 delete_breakpoint (b);
3809 continue;
3810 }
3811
3812 /* Thread event breakpoints must be set anew after an exec(),
3813 as must overlay event and longjmp master breakpoints. */
3814 if (b->type == bp_thread_event || b->type == bp_overlay_event
3815 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3816 || b->type == bp_exception_master)
3817 {
3818 delete_breakpoint (b);
3819 continue;
3820 }
3821
3822 /* Step-resume breakpoints are meaningless after an exec(). */
3823 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3824 {
3825 delete_breakpoint (b);
3826 continue;
3827 }
3828
3829 /* Just like single-step breakpoints. */
3830 if (b->type == bp_single_step)
3831 {
3832 delete_breakpoint (b);
3833 continue;
3834 }
3835
3836 /* Longjmp and longjmp-resume breakpoints are also meaningless
3837 after an exec. */
3838 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3839 || b->type == bp_longjmp_call_dummy
3840 || b->type == bp_exception || b->type == bp_exception_resume)
3841 {
3842 delete_breakpoint (b);
3843 continue;
3844 }
3845
3846 if (b->type == bp_catchpoint)
3847 {
3848 /* For now, none of the bp_catchpoint breakpoints need to
3849 do anything at this point. In the future, if some of
3850 the catchpoints need to something, we will need to add
3851 a new method, and call this method from here. */
3852 continue;
3853 }
3854
3855 /* bp_finish is a special case. The only way we ought to be able
3856 to see one of these when an exec() has happened, is if the user
3857 caught a vfork, and then said "finish". Ordinarily a finish just
3858 carries them to the call-site of the current callee, by setting
3859 a temporary bp there and resuming. But in this case, the finish
3860 will carry them entirely through the vfork & exec.
3861
3862 We don't want to allow a bp_finish to remain inserted now. But
3863 we can't safely delete it, 'cause finish_command has a handle to
3864 the bp on a bpstat, and will later want to delete it. There's a
3865 chance (and I've seen it happen) that if we delete the bp_finish
3866 here, that its storage will get reused by the time finish_command
3867 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3868 We really must allow finish_command to delete a bp_finish.
3869
3870 In the absence of a general solution for the "how do we know
3871 it's safe to delete something others may have handles to?"
3872 problem, what we'll do here is just uninsert the bp_finish, and
3873 let finish_command delete it.
3874
3875 (We know the bp_finish is "doomed" in the sense that it's
3876 momentary, and will be deleted as soon as finish_command sees
3877 the inferior stopped. So it doesn't matter that the bp's
3878 address is probably bogus in the new a.out, unlike e.g., the
3879 solib breakpoints.) */
3880
3881 if (b->type == bp_finish)
3882 {
3883 continue;
3884 }
3885
3886 /* Without a symbolic address, we have little hope of the
3887 pre-exec() address meaning the same thing in the post-exec()
3888 a.out. */
3889 if (breakpoint_event_location_empty_p (b))
3890 {
3891 delete_breakpoint (b);
3892 continue;
3893 }
3894 }
3895 }
3896
3897 int
3898 detach_breakpoints (ptid_t ptid)
3899 {
3900 struct bp_location *bl, **blp_tmp;
3901 int val = 0;
3902 struct cleanup *old_chain = save_inferior_ptid ();
3903 struct inferior *inf = current_inferior ();
3904
3905 if (ptid_get_pid (ptid) == ptid_get_pid (inferior_ptid))
3906 error (_("Cannot detach breakpoints of inferior_ptid"));
3907
3908 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
3909 inferior_ptid = ptid;
3910 ALL_BP_LOCATIONS (bl, blp_tmp)
3911 {
3912 if (bl->pspace != inf->pspace)
3913 continue;
3914
3915 /* This function must physically remove breakpoints locations
3916 from the specified ptid, without modifying the breakpoint
3917 package's state. Locations of type bp_loc_other are only
3918 maintained at GDB side. So, there is no need to remove
3919 these bp_loc_other locations. Moreover, removing these
3920 would modify the breakpoint package's state. */
3921 if (bl->loc_type == bp_loc_other)
3922 continue;
3923
3924 if (bl->inserted)
3925 val |= remove_breakpoint_1 (bl, mark_inserted);
3926 }
3927
3928 do_cleanups (old_chain);
3929 return val;
3930 }
3931
3932 /* Remove the breakpoint location BL from the current address space.
3933 Note that this is used to detach breakpoints from a child fork.
3934 When we get here, the child isn't in the inferior list, and neither
3935 do we have objects to represent its address space --- we should
3936 *not* look at bl->pspace->aspace here. */
3937
3938 static int
3939 remove_breakpoint_1 (struct bp_location *bl, insertion_state_t is)
3940 {
3941 int val;
3942
3943 /* BL is never in moribund_locations by our callers. */
3944 gdb_assert (bl->owner != NULL);
3945
3946 /* The type of none suggests that owner is actually deleted.
3947 This should not ever happen. */
3948 gdb_assert (bl->owner->type != bp_none);
3949
3950 if (bl->loc_type == bp_loc_software_breakpoint
3951 || bl->loc_type == bp_loc_hardware_breakpoint)
3952 {
3953 /* "Normal" instruction breakpoint: either the standard
3954 trap-instruction bp (bp_breakpoint), or a
3955 bp_hardware_breakpoint. */
3956
3957 /* First check to see if we have to handle an overlay. */
3958 if (overlay_debugging == ovly_off
3959 || bl->section == NULL
3960 || !(section_is_overlay (bl->section)))
3961 {
3962 /* No overlay handling: just remove the breakpoint. */
3963
3964 /* If we're trying to uninsert a memory breakpoint that we
3965 know is set in a dynamic object that is marked
3966 shlib_disabled, then either the dynamic object was
3967 removed with "remove-symbol-file" or with
3968 "nosharedlibrary". In the former case, we don't know
3969 whether another dynamic object might have loaded over the
3970 breakpoint's address -- the user might well let us know
3971 about it next with add-symbol-file (the whole point of
3972 add-symbol-file is letting the user manually maintain a
3973 list of dynamically loaded objects). If we have the
3974 breakpoint's shadow memory, that is, this is a software
3975 breakpoint managed by GDB, check whether the breakpoint
3976 is still inserted in memory, to avoid overwriting wrong
3977 code with stale saved shadow contents. Note that HW
3978 breakpoints don't have shadow memory, as they're
3979 implemented using a mechanism that is not dependent on
3980 being able to modify the target's memory, and as such
3981 they should always be removed. */
3982 if (bl->shlib_disabled
3983 && bl->target_info.shadow_len != 0
3984 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3985 val = 0;
3986 else
3987 val = bl->owner->ops->remove_location (bl);
3988 }
3989 else
3990 {
3991 /* This breakpoint is in an overlay section.
3992 Did we set a breakpoint at the LMA? */
3993 if (!overlay_events_enabled)
3994 {
3995 /* Yes -- overlay event support is not active, so we
3996 should have set a breakpoint at the LMA. Remove it.
3997 */
3998 /* Ignore any failures: if the LMA is in ROM, we will
3999 have already warned when we failed to insert it. */
4000 if (bl->loc_type == bp_loc_hardware_breakpoint)
4001 target_remove_hw_breakpoint (bl->gdbarch,
4002 &bl->overlay_target_info);
4003 else
4004 target_remove_breakpoint (bl->gdbarch,
4005 &bl->overlay_target_info);
4006 }
4007 /* Did we set a breakpoint at the VMA?
4008 If so, we will have marked the breakpoint 'inserted'. */
4009 if (bl->inserted)
4010 {
4011 /* Yes -- remove it. Previously we did not bother to
4012 remove the breakpoint if the section had been
4013 unmapped, but let's not rely on that being safe. We
4014 don't know what the overlay manager might do. */
4015
4016 /* However, we should remove *software* breakpoints only
4017 if the section is still mapped, or else we overwrite
4018 wrong code with the saved shadow contents. */
4019 if (bl->loc_type == bp_loc_hardware_breakpoint
4020 || section_is_mapped (bl->section))
4021 val = bl->owner->ops->remove_location (bl);
4022 else
4023 val = 0;
4024 }
4025 else
4026 {
4027 /* No -- not inserted, so no need to remove. No error. */
4028 val = 0;
4029 }
4030 }
4031
4032 /* In some cases, we might not be able to remove a breakpoint in
4033 a shared library that has already been removed, but we have
4034 not yet processed the shlib unload event. Similarly for an
4035 unloaded add-symbol-file object - the user might not yet have
4036 had the chance to remove-symbol-file it. shlib_disabled will
4037 be set if the library/object has already been removed, but
4038 the breakpoint hasn't been uninserted yet, e.g., after
4039 "nosharedlibrary" or "remove-symbol-file" with breakpoints
4040 always-inserted mode. */
4041 if (val
4042 && (bl->loc_type == bp_loc_software_breakpoint
4043 && (bl->shlib_disabled
4044 || solib_name_from_address (bl->pspace, bl->address)
4045 || shared_objfile_contains_address_p (bl->pspace,
4046 bl->address))))
4047 val = 0;
4048
4049 if (val)
4050 return val;
4051 bl->inserted = (is == mark_inserted);
4052 }
4053 else if (bl->loc_type == bp_loc_hardware_watchpoint)
4054 {
4055 gdb_assert (bl->owner->ops != NULL
4056 && bl->owner->ops->remove_location != NULL);
4057
4058 bl->inserted = (is == mark_inserted);
4059 bl->owner->ops->remove_location (bl);
4060
4061 /* Failure to remove any of the hardware watchpoints comes here. */
4062 if ((is == mark_uninserted) && (bl->inserted))
4063 warning (_("Could not remove hardware watchpoint %d."),
4064 bl->owner->number);
4065 }
4066 else if (bl->owner->type == bp_catchpoint
4067 && breakpoint_enabled (bl->owner)
4068 && !bl->duplicate)
4069 {
4070 gdb_assert (bl->owner->ops != NULL
4071 && bl->owner->ops->remove_location != NULL);
4072
4073 val = bl->owner->ops->remove_location (bl);
4074 if (val)
4075 return val;
4076
4077 bl->inserted = (is == mark_inserted);
4078 }
4079
4080 return 0;
4081 }
4082
4083 static int
4084 remove_breakpoint (struct bp_location *bl, insertion_state_t is)
4085 {
4086 int ret;
4087 struct cleanup *old_chain;
4088
4089 /* BL is never in moribund_locations by our callers. */
4090 gdb_assert (bl->owner != NULL);
4091
4092 /* The type of none suggests that owner is actually deleted.
4093 This should not ever happen. */
4094 gdb_assert (bl->owner->type != bp_none);
4095
4096 old_chain = save_current_space_and_thread ();
4097
4098 switch_to_program_space_and_thread (bl->pspace);
4099
4100 ret = remove_breakpoint_1 (bl, is);
4101
4102 do_cleanups (old_chain);
4103 return ret;
4104 }
4105
4106 /* Clear the "inserted" flag in all breakpoints. */
4107
4108 void
4109 mark_breakpoints_out (void)
4110 {
4111 struct bp_location *bl, **blp_tmp;
4112
4113 ALL_BP_LOCATIONS (bl, blp_tmp)
4114 if (bl->pspace == current_program_space)
4115 bl->inserted = 0;
4116 }
4117
4118 /* Clear the "inserted" flag in all breakpoints and delete any
4119 breakpoints which should go away between runs of the program.
4120
4121 Plus other such housekeeping that has to be done for breakpoints
4122 between runs.
4123
4124 Note: this function gets called at the end of a run (by
4125 generic_mourn_inferior) and when a run begins (by
4126 init_wait_for_inferior). */
4127
4128
4129
4130 void
4131 breakpoint_init_inferior (enum inf_context context)
4132 {
4133 struct breakpoint *b, *b_tmp;
4134 struct bp_location *bl, **blp_tmp;
4135 int ix;
4136 struct program_space *pspace = current_program_space;
4137
4138 /* If breakpoint locations are shared across processes, then there's
4139 nothing to do. */
4140 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
4141 return;
4142
4143 mark_breakpoints_out ();
4144
4145 ALL_BREAKPOINTS_SAFE (b, b_tmp)
4146 {
4147 if (b->loc && b->loc->pspace != pspace)
4148 continue;
4149
4150 switch (b->type)
4151 {
4152 case bp_call_dummy:
4153 case bp_longjmp_call_dummy:
4154
4155 /* If the call dummy breakpoint is at the entry point it will
4156 cause problems when the inferior is rerun, so we better get
4157 rid of it. */
4158
4159 case bp_watchpoint_scope:
4160
4161 /* Also get rid of scope breakpoints. */
4162
4163 case bp_shlib_event:
4164
4165 /* Also remove solib event breakpoints. Their addresses may
4166 have changed since the last time we ran the program.
4167 Actually we may now be debugging against different target;
4168 and so the solib backend that installed this breakpoint may
4169 not be used in by the target. E.g.,
4170
4171 (gdb) file prog-linux
4172 (gdb) run # native linux target
4173 ...
4174 (gdb) kill
4175 (gdb) file prog-win.exe
4176 (gdb) tar rem :9999 # remote Windows gdbserver.
4177 */
4178
4179 case bp_step_resume:
4180
4181 /* Also remove step-resume breakpoints. */
4182
4183 case bp_single_step:
4184
4185 /* Also remove single-step breakpoints. */
4186
4187 delete_breakpoint (b);
4188 break;
4189
4190 case bp_watchpoint:
4191 case bp_hardware_watchpoint:
4192 case bp_read_watchpoint:
4193 case bp_access_watchpoint:
4194 {
4195 struct watchpoint *w = (struct watchpoint *) b;
4196
4197 /* Likewise for watchpoints on local expressions. */
4198 if (w->exp_valid_block != NULL)
4199 delete_breakpoint (b);
4200 else if (context == inf_starting)
4201 {
4202 /* Reset val field to force reread of starting value in
4203 insert_breakpoints. */
4204 if (w->val)
4205 value_free (w->val);
4206 w->val = NULL;
4207 w->val_valid = 0;
4208 }
4209 }
4210 break;
4211 default:
4212 break;
4213 }
4214 }
4215
4216 /* Get rid of the moribund locations. */
4217 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
4218 decref_bp_location (&bl);
4219 VEC_free (bp_location_p, moribund_locations);
4220 }
4221
4222 /* These functions concern about actual breakpoints inserted in the
4223 target --- to e.g. check if we need to do decr_pc adjustment or if
4224 we need to hop over the bkpt --- so we check for address space
4225 match, not program space. */
4226
4227 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4228 exists at PC. It returns ordinary_breakpoint_here if it's an
4229 ordinary breakpoint, or permanent_breakpoint_here if it's a
4230 permanent breakpoint.
4231 - When continuing from a location with an ordinary breakpoint, we
4232 actually single step once before calling insert_breakpoints.
4233 - When continuing from a location with a permanent breakpoint, we
4234 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4235 the target, to advance the PC past the breakpoint. */
4236
4237 enum breakpoint_here
4238 breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
4239 {
4240 struct bp_location *bl, **blp_tmp;
4241 int any_breakpoint_here = 0;
4242
4243 ALL_BP_LOCATIONS (bl, blp_tmp)
4244 {
4245 if (bl->loc_type != bp_loc_software_breakpoint
4246 && bl->loc_type != bp_loc_hardware_breakpoint)
4247 continue;
4248
4249 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
4250 if ((breakpoint_enabled (bl->owner)
4251 || bl->permanent)
4252 && breakpoint_location_address_match (bl, aspace, pc))
4253 {
4254 if (overlay_debugging
4255 && section_is_overlay (bl->section)
4256 && !section_is_mapped (bl->section))
4257 continue; /* unmapped overlay -- can't be a match */
4258 else if (bl->permanent)
4259 return permanent_breakpoint_here;
4260 else
4261 any_breakpoint_here = 1;
4262 }
4263 }
4264
4265 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
4266 }
4267
4268 /* See breakpoint.h. */
4269
4270 int
4271 breakpoint_in_range_p (struct address_space *aspace,
4272 CORE_ADDR addr, ULONGEST len)
4273 {
4274 struct bp_location *bl, **blp_tmp;
4275
4276 ALL_BP_LOCATIONS (bl, blp_tmp)
4277 {
4278 if (bl->loc_type != bp_loc_software_breakpoint
4279 && bl->loc_type != bp_loc_hardware_breakpoint)
4280 continue;
4281
4282 if ((breakpoint_enabled (bl->owner)
4283 || bl->permanent)
4284 && breakpoint_location_address_range_overlap (bl, aspace,
4285 addr, len))
4286 {
4287 if (overlay_debugging
4288 && section_is_overlay (bl->section)
4289 && !section_is_mapped (bl->section))
4290 {
4291 /* Unmapped overlay -- can't be a match. */
4292 continue;
4293 }
4294
4295 return 1;
4296 }
4297 }
4298
4299 return 0;
4300 }
4301
4302 /* Return true if there's a moribund breakpoint at PC. */
4303
4304 int
4305 moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
4306 {
4307 struct bp_location *loc;
4308 int ix;
4309
4310 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
4311 if (breakpoint_location_address_match (loc, aspace, pc))
4312 return 1;
4313
4314 return 0;
4315 }
4316
4317 /* Returns non-zero iff BL is inserted at PC, in address space
4318 ASPACE. */
4319
4320 static int
4321 bp_location_inserted_here_p (struct bp_location *bl,
4322 struct address_space *aspace, CORE_ADDR pc)
4323 {
4324 if (bl->inserted
4325 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4326 aspace, pc))
4327 {
4328 if (overlay_debugging
4329 && section_is_overlay (bl->section)
4330 && !section_is_mapped (bl->section))
4331 return 0; /* unmapped overlay -- can't be a match */
4332 else
4333 return 1;
4334 }
4335 return 0;
4336 }
4337
4338 /* Returns non-zero iff there's a breakpoint inserted at PC. */
4339
4340 int
4341 breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
4342 {
4343 struct bp_location **blp, **blp_tmp = NULL;
4344 struct bp_location *bl;
4345
4346 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4347 {
4348 struct bp_location *bl = *blp;
4349
4350 if (bl->loc_type != bp_loc_software_breakpoint
4351 && bl->loc_type != bp_loc_hardware_breakpoint)
4352 continue;
4353
4354 if (bp_location_inserted_here_p (bl, aspace, pc))
4355 return 1;
4356 }
4357 return 0;
4358 }
4359
4360 /* This function returns non-zero iff there is a software breakpoint
4361 inserted at PC. */
4362
4363 int
4364 software_breakpoint_inserted_here_p (struct address_space *aspace,
4365 CORE_ADDR pc)
4366 {
4367 struct bp_location **blp, **blp_tmp = NULL;
4368 struct bp_location *bl;
4369
4370 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4371 {
4372 struct bp_location *bl = *blp;
4373
4374 if (bl->loc_type != bp_loc_software_breakpoint)
4375 continue;
4376
4377 if (bp_location_inserted_here_p (bl, aspace, pc))
4378 return 1;
4379 }
4380
4381 return 0;
4382 }
4383
4384 /* See breakpoint.h. */
4385
4386 int
4387 hardware_breakpoint_inserted_here_p (struct address_space *aspace,
4388 CORE_ADDR pc)
4389 {
4390 struct bp_location **blp, **blp_tmp = NULL;
4391 struct bp_location *bl;
4392
4393 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4394 {
4395 struct bp_location *bl = *blp;
4396
4397 if (bl->loc_type != bp_loc_hardware_breakpoint)
4398 continue;
4399
4400 if (bp_location_inserted_here_p (bl, aspace, pc))
4401 return 1;
4402 }
4403
4404 return 0;
4405 }
4406
4407 int
4408 hardware_watchpoint_inserted_in_range (struct address_space *aspace,
4409 CORE_ADDR addr, ULONGEST len)
4410 {
4411 struct breakpoint *bpt;
4412
4413 ALL_BREAKPOINTS (bpt)
4414 {
4415 struct bp_location *loc;
4416
4417 if (bpt->type != bp_hardware_watchpoint
4418 && bpt->type != bp_access_watchpoint)
4419 continue;
4420
4421 if (!breakpoint_enabled (bpt))
4422 continue;
4423
4424 for (loc = bpt->loc; loc; loc = loc->next)
4425 if (loc->pspace->aspace == aspace && loc->inserted)
4426 {
4427 CORE_ADDR l, h;
4428
4429 /* Check for intersection. */
4430 l = max (loc->address, addr);
4431 h = min (loc->address + loc->length, addr + len);
4432 if (l < h)
4433 return 1;
4434 }
4435 }
4436 return 0;
4437 }
4438 \f
4439
4440 /* bpstat stuff. External routines' interfaces are documented
4441 in breakpoint.h. */
4442
4443 int
4444 is_catchpoint (struct breakpoint *ep)
4445 {
4446 return (ep->type == bp_catchpoint);
4447 }
4448
4449 /* Frees any storage that is part of a bpstat. Does not walk the
4450 'next' chain. */
4451
4452 static void
4453 bpstat_free (bpstat bs)
4454 {
4455 if (bs->old_val != NULL)
4456 value_free (bs->old_val);
4457 decref_counted_command_line (&bs->commands);
4458 decref_bp_location (&bs->bp_location_at);
4459 xfree (bs);
4460 }
4461
4462 /* Clear a bpstat so that it says we are not at any breakpoint.
4463 Also free any storage that is part of a bpstat. */
4464
4465 void
4466 bpstat_clear (bpstat *bsp)
4467 {
4468 bpstat p;
4469 bpstat q;
4470
4471 if (bsp == 0)
4472 return;
4473 p = *bsp;
4474 while (p != NULL)
4475 {
4476 q = p->next;
4477 bpstat_free (p);
4478 p = q;
4479 }
4480 *bsp = NULL;
4481 }
4482
4483 /* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4484 is part of the bpstat is copied as well. */
4485
4486 bpstat
4487 bpstat_copy (bpstat bs)
4488 {
4489 bpstat p = NULL;
4490 bpstat tmp;
4491 bpstat retval = NULL;
4492
4493 if (bs == NULL)
4494 return bs;
4495
4496 for (; bs != NULL; bs = bs->next)
4497 {
4498 tmp = (bpstat) xmalloc (sizeof (*tmp));
4499 memcpy (tmp, bs, sizeof (*tmp));
4500 incref_counted_command_line (tmp->commands);
4501 incref_bp_location (tmp->bp_location_at);
4502 if (bs->old_val != NULL)
4503 {
4504 tmp->old_val = value_copy (bs->old_val);
4505 release_value (tmp->old_val);
4506 }
4507
4508 if (p == NULL)
4509 /* This is the first thing in the chain. */
4510 retval = tmp;
4511 else
4512 p->next = tmp;
4513 p = tmp;
4514 }
4515 p->next = NULL;
4516 return retval;
4517 }
4518
4519 /* Find the bpstat associated with this breakpoint. */
4520
4521 bpstat
4522 bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
4523 {
4524 if (bsp == NULL)
4525 return NULL;
4526
4527 for (; bsp != NULL; bsp = bsp->next)
4528 {
4529 if (bsp->breakpoint_at == breakpoint)
4530 return bsp;
4531 }
4532 return NULL;
4533 }
4534
4535 /* See breakpoint.h. */
4536
4537 int
4538 bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
4539 {
4540 for (; bsp != NULL; bsp = bsp->next)
4541 {
4542 if (bsp->breakpoint_at == NULL)
4543 {
4544 /* A moribund location can never explain a signal other than
4545 GDB_SIGNAL_TRAP. */
4546 if (sig == GDB_SIGNAL_TRAP)
4547 return 1;
4548 }
4549 else
4550 {
4551 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4552 sig))
4553 return 1;
4554 }
4555 }
4556
4557 return 0;
4558 }
4559
4560 /* Put in *NUM the breakpoint number of the first breakpoint we are
4561 stopped at. *BSP upon return is a bpstat which points to the
4562 remaining breakpoints stopped at (but which is not guaranteed to be
4563 good for anything but further calls to bpstat_num).
4564
4565 Return 0 if passed a bpstat which does not indicate any breakpoints.
4566 Return -1 if stopped at a breakpoint that has been deleted since
4567 we set it.
4568 Return 1 otherwise. */
4569
4570 int
4571 bpstat_num (bpstat *bsp, int *num)
4572 {
4573 struct breakpoint *b;
4574
4575 if ((*bsp) == NULL)
4576 return 0; /* No more breakpoint values */
4577
4578 /* We assume we'll never have several bpstats that correspond to a
4579 single breakpoint -- otherwise, this function might return the
4580 same number more than once and this will look ugly. */
4581 b = (*bsp)->breakpoint_at;
4582 *bsp = (*bsp)->next;
4583 if (b == NULL)
4584 return -1; /* breakpoint that's been deleted since */
4585
4586 *num = b->number; /* We have its number */
4587 return 1;
4588 }
4589
4590 /* See breakpoint.h. */
4591
4592 void
4593 bpstat_clear_actions (void)
4594 {
4595 struct thread_info *tp;
4596 bpstat bs;
4597
4598 if (ptid_equal (inferior_ptid, null_ptid))
4599 return;
4600
4601 tp = find_thread_ptid (inferior_ptid);
4602 if (tp == NULL)
4603 return;
4604
4605 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
4606 {
4607 decref_counted_command_line (&bs->commands);
4608
4609 if (bs->old_val != NULL)
4610 {
4611 value_free (bs->old_val);
4612 bs->old_val = NULL;
4613 }
4614 }
4615 }
4616
4617 /* Called when a command is about to proceed the inferior. */
4618
4619 static void
4620 breakpoint_about_to_proceed (void)
4621 {
4622 if (!ptid_equal (inferior_ptid, null_ptid))
4623 {
4624 struct thread_info *tp = inferior_thread ();
4625
4626 /* Allow inferior function calls in breakpoint commands to not
4627 interrupt the command list. When the call finishes
4628 successfully, the inferior will be standing at the same
4629 breakpoint as if nothing happened. */
4630 if (tp->control.in_infcall)
4631 return;
4632 }
4633
4634 breakpoint_proceeded = 1;
4635 }
4636
4637 /* Stub for cleaning up our state if we error-out of a breakpoint
4638 command. */
4639 static void
4640 cleanup_executing_breakpoints (void *ignore)
4641 {
4642 executing_breakpoint_commands = 0;
4643 }
4644
4645 /* Return non-zero iff CMD as the first line of a command sequence is `silent'
4646 or its equivalent. */
4647
4648 static int
4649 command_line_is_silent (struct command_line *cmd)
4650 {
4651 return cmd && (strcmp ("silent", cmd->line) == 0);
4652 }
4653
4654 /* Execute all the commands associated with all the breakpoints at
4655 this location. Any of these commands could cause the process to
4656 proceed beyond this point, etc. We look out for such changes by
4657 checking the global "breakpoint_proceeded" after each command.
4658
4659 Returns true if a breakpoint command resumed the inferior. In that
4660 case, it is the caller's responsibility to recall it again with the
4661 bpstat of the current thread. */
4662
4663 static int
4664 bpstat_do_actions_1 (bpstat *bsp)
4665 {
4666 bpstat bs;
4667 struct cleanup *old_chain;
4668 int again = 0;
4669
4670 /* Avoid endless recursion if a `source' command is contained
4671 in bs->commands. */
4672 if (executing_breakpoint_commands)
4673 return 0;
4674
4675 executing_breakpoint_commands = 1;
4676 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
4677
4678 prevent_dont_repeat ();
4679
4680 /* This pointer will iterate over the list of bpstat's. */
4681 bs = *bsp;
4682
4683 breakpoint_proceeded = 0;
4684 for (; bs != NULL; bs = bs->next)
4685 {
4686 struct counted_command_line *ccmd;
4687 struct command_line *cmd;
4688 struct cleanup *this_cmd_tree_chain;
4689
4690 /* Take ownership of the BSP's command tree, if it has one.
4691
4692 The command tree could legitimately contain commands like
4693 'step' and 'next', which call clear_proceed_status, which
4694 frees stop_bpstat's command tree. To make sure this doesn't
4695 free the tree we're executing out from under us, we need to
4696 take ownership of the tree ourselves. Since a given bpstat's
4697 commands are only executed once, we don't need to copy it; we
4698 can clear the pointer in the bpstat, and make sure we free
4699 the tree when we're done. */
4700 ccmd = bs->commands;
4701 bs->commands = NULL;
4702 this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
4703 cmd = ccmd ? ccmd->commands : NULL;
4704 if (command_line_is_silent (cmd))
4705 {
4706 /* The action has been already done by bpstat_stop_status. */
4707 cmd = cmd->next;
4708 }
4709
4710 while (cmd != NULL)
4711 {
4712 execute_control_command (cmd);
4713
4714 if (breakpoint_proceeded)
4715 break;
4716 else
4717 cmd = cmd->next;
4718 }
4719
4720 /* We can free this command tree now. */
4721 do_cleanups (this_cmd_tree_chain);
4722
4723 if (breakpoint_proceeded)
4724 {
4725 if (interpreter_async)
4726 /* If we are in async mode, then the target might be still
4727 running, not stopped at any breakpoint, so nothing for
4728 us to do here -- just return to the event loop. */
4729 ;
4730 else
4731 /* In sync mode, when execute_control_command returns
4732 we're already standing on the next breakpoint.
4733 Breakpoint commands for that stop were not run, since
4734 execute_command does not run breakpoint commands --
4735 only command_line_handler does, but that one is not
4736 involved in execution of breakpoint commands. So, we
4737 can now execute breakpoint commands. It should be
4738 noted that making execute_command do bpstat actions is
4739 not an option -- in this case we'll have recursive
4740 invocation of bpstat for each breakpoint with a
4741 command, and can easily blow up GDB stack. Instead, we
4742 return true, which will trigger the caller to recall us
4743 with the new stop_bpstat. */
4744 again = 1;
4745 break;
4746 }
4747 }
4748 do_cleanups (old_chain);
4749 return again;
4750 }
4751
4752 void
4753 bpstat_do_actions (void)
4754 {
4755 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4756
4757 /* Do any commands attached to breakpoint we are stopped at. */
4758 while (!ptid_equal (inferior_ptid, null_ptid)
4759 && target_has_execution
4760 && !is_exited (inferior_ptid)
4761 && !is_executing (inferior_ptid))
4762 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4763 and only return when it is stopped at the next breakpoint, we
4764 keep doing breakpoint actions until it returns false to
4765 indicate the inferior was not resumed. */
4766 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
4767 break;
4768
4769 discard_cleanups (cleanup_if_error);
4770 }
4771
4772 /* Print out the (old or new) value associated with a watchpoint. */
4773
4774 static void
4775 watchpoint_value_print (struct value *val, struct ui_file *stream)
4776 {
4777 if (val == NULL)
4778 fprintf_unfiltered (stream, _("<unreadable>"));
4779 else
4780 {
4781 struct value_print_options opts;
4782 get_user_print_options (&opts);
4783 value_print (val, stream, &opts);
4784 }
4785 }
4786
4787 /* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4788 debugging multiple threads. */
4789
4790 void
4791 maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4792 {
4793 if (ui_out_is_mi_like_p (uiout))
4794 return;
4795
4796 ui_out_text (uiout, "\n");
4797
4798 if (show_thread_that_caused_stop ())
4799 {
4800 const char *name;
4801 struct thread_info *thr = inferior_thread ();
4802
4803 ui_out_text (uiout, "Thread ");
4804 ui_out_field_fmt (uiout, "thread-id", "%s", print_thread_id (thr));
4805
4806 name = thr->name != NULL ? thr->name : target_thread_name (thr);
4807 if (name != NULL)
4808 {
4809 ui_out_text (uiout, " \"");
4810 ui_out_field_fmt (uiout, "name", "%s", name);
4811 ui_out_text (uiout, "\"");
4812 }
4813
4814 ui_out_text (uiout, " hit ");
4815 }
4816 }
4817
4818 /* Generic routine for printing messages indicating why we
4819 stopped. The behavior of this function depends on the value
4820 'print_it' in the bpstat structure. Under some circumstances we
4821 may decide not to print anything here and delegate the task to
4822 normal_stop(). */
4823
4824 static enum print_stop_action
4825 print_bp_stop_message (bpstat bs)
4826 {
4827 switch (bs->print_it)
4828 {
4829 case print_it_noop:
4830 /* Nothing should be printed for this bpstat entry. */
4831 return PRINT_UNKNOWN;
4832 break;
4833
4834 case print_it_done:
4835 /* We still want to print the frame, but we already printed the
4836 relevant messages. */
4837 return PRINT_SRC_AND_LOC;
4838 break;
4839
4840 case print_it_normal:
4841 {
4842 struct breakpoint *b = bs->breakpoint_at;
4843
4844 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4845 which has since been deleted. */
4846 if (b == NULL)
4847 return PRINT_UNKNOWN;
4848
4849 /* Normal case. Call the breakpoint's print_it method. */
4850 return b->ops->print_it (bs);
4851 }
4852 break;
4853
4854 default:
4855 internal_error (__FILE__, __LINE__,
4856 _("print_bp_stop_message: unrecognized enum value"));
4857 break;
4858 }
4859 }
4860
4861 /* A helper function that prints a shared library stopped event. */
4862
4863 static void
4864 print_solib_event (int is_catchpoint)
4865 {
4866 int any_deleted
4867 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4868 int any_added
4869 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4870
4871 if (!is_catchpoint)
4872 {
4873 if (any_added || any_deleted)
4874 ui_out_text (current_uiout,
4875 _("Stopped due to shared library event:\n"));
4876 else
4877 ui_out_text (current_uiout,
4878 _("Stopped due to shared library event (no "
4879 "libraries added or removed)\n"));
4880 }
4881
4882 if (ui_out_is_mi_like_p (current_uiout))
4883 ui_out_field_string (current_uiout, "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 ui_out_text (current_uiout, _(" 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 ui_out_text (current_uiout, " ");
4902 ui_out_field_string (current_uiout, "library", name);
4903 ui_out_text (current_uiout, "\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 ui_out_text (current_uiout, _(" 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 ui_out_text (current_uiout, " ");
4925 ui_out_field_string (current_uiout, "library", iter->so_name);
4926 ui_out_text (current_uiout, "\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, &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 struct ui_out *uiout = current_uiout;
5233
5234 /* This seems like the only logical thing to do because
5235 if we temporarily ignored the watchpoint, then when
5236 we reenter the block in which it is valid it contains
5237 garbage (in the case of a function, it may have two
5238 garbage values, one before and one after the prologue).
5239 So we can't even detect the first assignment to it and
5240 watch after that (since the garbage may or may not equal
5241 the first value assigned). */
5242 /* We print all the stop information in
5243 breakpoint_ops->print_it, but in this case, by the time we
5244 call breakpoint_ops->print_it this bp will be deleted
5245 already. So we have no choice but print the information
5246 here. */
5247 if (ui_out_is_mi_like_p (uiout))
5248 ui_out_field_string
5249 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
5250 ui_out_text (uiout, "\nWatchpoint ");
5251 ui_out_field_int (uiout, "wpnum", b->base.number);
5252 ui_out_text (uiout,
5253 " deleted because the program has left the block in\n\
5254 which its expression is valid.\n");
5255
5256 /* Make sure the watchpoint's commands aren't executed. */
5257 decref_counted_command_line (&b->base.commands);
5258 watchpoint_del_at_next_stop (b);
5259
5260 return WP_DELETED;
5261 }
5262 }
5263
5264 /* Return true if it looks like target has stopped due to hitting
5265 breakpoint location BL. This function does not check if we should
5266 stop, only if BL explains the stop. */
5267
5268 static int
5269 bpstat_check_location (const struct bp_location *bl,
5270 struct address_space *aspace, CORE_ADDR bp_addr,
5271 const struct target_waitstatus *ws)
5272 {
5273 struct breakpoint *b = bl->owner;
5274
5275 /* BL is from an existing breakpoint. */
5276 gdb_assert (b != NULL);
5277
5278 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
5279 }
5280
5281 /* Determine if the watched values have actually changed, and we
5282 should stop. If not, set BS->stop to 0. */
5283
5284 static void
5285 bpstat_check_watchpoint (bpstat bs)
5286 {
5287 const struct bp_location *bl;
5288 struct watchpoint *b;
5289
5290 /* BS is built for existing struct breakpoint. */
5291 bl = bs->bp_location_at;
5292 gdb_assert (bl != NULL);
5293 b = (struct watchpoint *) bs->breakpoint_at;
5294 gdb_assert (b != NULL);
5295
5296 {
5297 int must_check_value = 0;
5298
5299 if (b->base.type == bp_watchpoint)
5300 /* For a software watchpoint, we must always check the
5301 watched value. */
5302 must_check_value = 1;
5303 else if (b->watchpoint_triggered == watch_triggered_yes)
5304 /* We have a hardware watchpoint (read, write, or access)
5305 and the target earlier reported an address watched by
5306 this watchpoint. */
5307 must_check_value = 1;
5308 else if (b->watchpoint_triggered == watch_triggered_unknown
5309 && b->base.type == bp_hardware_watchpoint)
5310 /* We were stopped by a hardware watchpoint, but the target could
5311 not report the data address. We must check the watchpoint's
5312 value. Access and read watchpoints are out of luck; without
5313 a data address, we can't figure it out. */
5314 must_check_value = 1;
5315
5316 if (must_check_value)
5317 {
5318 char *message
5319 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
5320 b->base.number);
5321 struct cleanup *cleanups = make_cleanup (xfree, message);
5322 int e = catch_errors (watchpoint_check, bs, message,
5323 RETURN_MASK_ALL);
5324 do_cleanups (cleanups);
5325 switch (e)
5326 {
5327 case WP_DELETED:
5328 /* We've already printed what needs to be printed. */
5329 bs->print_it = print_it_done;
5330 /* Stop. */
5331 break;
5332 case WP_IGNORE:
5333 bs->print_it = print_it_noop;
5334 bs->stop = 0;
5335 break;
5336 case WP_VALUE_CHANGED:
5337 if (b->base.type == bp_read_watchpoint)
5338 {
5339 /* There are two cases to consider here:
5340
5341 1. We're watching the triggered memory for reads.
5342 In that case, trust the target, and always report
5343 the watchpoint hit to the user. Even though
5344 reads don't cause value changes, the value may
5345 have changed since the last time it was read, and
5346 since we're not trapping writes, we will not see
5347 those, and as such we should ignore our notion of
5348 old value.
5349
5350 2. We're watching the triggered memory for both
5351 reads and writes. There are two ways this may
5352 happen:
5353
5354 2.1. This is a target that can't break on data
5355 reads only, but can break on accesses (reads or
5356 writes), such as e.g., x86. We detect this case
5357 at the time we try to insert read watchpoints.
5358
5359 2.2. Otherwise, the target supports read
5360 watchpoints, but, the user set an access or write
5361 watchpoint watching the same memory as this read
5362 watchpoint.
5363
5364 If we're watching memory writes as well as reads,
5365 ignore watchpoint hits when we find that the
5366 value hasn't changed, as reads don't cause
5367 changes. This still gives false positives when
5368 the program writes the same value to memory as
5369 what there was already in memory (we will confuse
5370 it for a read), but it's much better than
5371 nothing. */
5372
5373 int other_write_watchpoint = 0;
5374
5375 if (bl->watchpoint_type == hw_read)
5376 {
5377 struct breakpoint *other_b;
5378
5379 ALL_BREAKPOINTS (other_b)
5380 if (other_b->type == bp_hardware_watchpoint
5381 || other_b->type == bp_access_watchpoint)
5382 {
5383 struct watchpoint *other_w =
5384 (struct watchpoint *) other_b;
5385
5386 if (other_w->watchpoint_triggered
5387 == watch_triggered_yes)
5388 {
5389 other_write_watchpoint = 1;
5390 break;
5391 }
5392 }
5393 }
5394
5395 if (other_write_watchpoint
5396 || bl->watchpoint_type == hw_access)
5397 {
5398 /* We're watching the same memory for writes,
5399 and the value changed since the last time we
5400 updated it, so this trap must be for a write.
5401 Ignore it. */
5402 bs->print_it = print_it_noop;
5403 bs->stop = 0;
5404 }
5405 }
5406 break;
5407 case WP_VALUE_NOT_CHANGED:
5408 if (b->base.type == bp_hardware_watchpoint
5409 || b->base.type == bp_watchpoint)
5410 {
5411 /* Don't stop: write watchpoints shouldn't fire if
5412 the value hasn't changed. */
5413 bs->print_it = print_it_noop;
5414 bs->stop = 0;
5415 }
5416 /* Stop. */
5417 break;
5418 default:
5419 /* Can't happen. */
5420 case 0:
5421 /* Error from catch_errors. */
5422 printf_filtered (_("Watchpoint %d deleted.\n"), b->base.number);
5423 watchpoint_del_at_next_stop (b);
5424 /* We've already printed what needs to be printed. */
5425 bs->print_it = print_it_done;
5426 break;
5427 }
5428 }
5429 else /* must_check_value == 0 */
5430 {
5431 /* This is a case where some watchpoint(s) triggered, but
5432 not at the address of this watchpoint, or else no
5433 watchpoint triggered after all. So don't print
5434 anything for this watchpoint. */
5435 bs->print_it = print_it_noop;
5436 bs->stop = 0;
5437 }
5438 }
5439 }
5440
5441 /* For breakpoints that are currently marked as telling gdb to stop,
5442 check conditions (condition proper, frame, thread and ignore count)
5443 of breakpoint referred to by BS. If we should not stop for this
5444 breakpoint, set BS->stop to 0. */
5445
5446 static void
5447 bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5448 {
5449 const struct bp_location *bl;
5450 struct breakpoint *b;
5451 int value_is_zero = 0;
5452 struct expression *cond;
5453
5454 gdb_assert (bs->stop);
5455
5456 /* BS is built for existing struct breakpoint. */
5457 bl = bs->bp_location_at;
5458 gdb_assert (bl != NULL);
5459 b = bs->breakpoint_at;
5460 gdb_assert (b != NULL);
5461
5462 /* Even if the target evaluated the condition on its end and notified GDB, we
5463 need to do so again since GDB does not know if we stopped due to a
5464 breakpoint or a single step breakpoint. */
5465
5466 if (frame_id_p (b->frame_id)
5467 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
5468 {
5469 bs->stop = 0;
5470 return;
5471 }
5472
5473 /* If this is a thread/task-specific breakpoint, don't waste cpu
5474 evaluating the condition if this isn't the specified
5475 thread/task. */
5476 if ((b->thread != -1 && b->thread != ptid_to_global_thread_id (ptid))
5477 || (b->task != 0 && b->task != ada_get_task_number (ptid)))
5478
5479 {
5480 bs->stop = 0;
5481 return;
5482 }
5483
5484 /* Evaluate extension language breakpoints that have a "stop" method
5485 implemented. */
5486 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
5487
5488 if (is_watchpoint (b))
5489 {
5490 struct watchpoint *w = (struct watchpoint *) b;
5491
5492 cond = w->cond_exp;
5493 }
5494 else
5495 cond = bl->cond;
5496
5497 if (cond && b->disposition != disp_del_at_next_stop)
5498 {
5499 int within_current_scope = 1;
5500 struct watchpoint * w;
5501
5502 /* We use value_mark and value_free_to_mark because it could
5503 be a long time before we return to the command level and
5504 call free_all_values. We can't call free_all_values
5505 because we might be in the middle of evaluating a
5506 function call. */
5507 struct value *mark = value_mark ();
5508
5509 if (is_watchpoint (b))
5510 w = (struct watchpoint *) b;
5511 else
5512 w = NULL;
5513
5514 /* Need to select the frame, with all that implies so that
5515 the conditions will have the right context. Because we
5516 use the frame, we will not see an inlined function's
5517 variables when we arrive at a breakpoint at the start
5518 of the inlined function; the current frame will be the
5519 call site. */
5520 if (w == NULL || w->cond_exp_valid_block == NULL)
5521 select_frame (get_current_frame ());
5522 else
5523 {
5524 struct frame_info *frame;
5525
5526 /* For local watchpoint expressions, which particular
5527 instance of a local is being watched matters, so we
5528 keep track of the frame to evaluate the expression
5529 in. To evaluate the condition however, it doesn't
5530 really matter which instantiation of the function
5531 where the condition makes sense triggers the
5532 watchpoint. This allows an expression like "watch
5533 global if q > 10" set in `func', catch writes to
5534 global on all threads that call `func', or catch
5535 writes on all recursive calls of `func' by a single
5536 thread. We simply always evaluate the condition in
5537 the innermost frame that's executing where it makes
5538 sense to evaluate the condition. It seems
5539 intuitive. */
5540 frame = block_innermost_frame (w->cond_exp_valid_block);
5541 if (frame != NULL)
5542 select_frame (frame);
5543 else
5544 within_current_scope = 0;
5545 }
5546 if (within_current_scope)
5547 value_is_zero
5548 = catch_errors (breakpoint_cond_eval, cond,
5549 "Error in testing breakpoint condition:\n",
5550 RETURN_MASK_ALL);
5551 else
5552 {
5553 warning (_("Watchpoint condition cannot be tested "
5554 "in the current scope"));
5555 /* If we failed to set the right context for this
5556 watchpoint, unconditionally report it. */
5557 value_is_zero = 0;
5558 }
5559 /* FIXME-someday, should give breakpoint #. */
5560 value_free_to_mark (mark);
5561 }
5562
5563 if (cond && value_is_zero)
5564 {
5565 bs->stop = 0;
5566 }
5567 else if (b->ignore_count > 0)
5568 {
5569 b->ignore_count--;
5570 bs->stop = 0;
5571 /* Increase the hit count even though we don't stop. */
5572 ++(b->hit_count);
5573 observer_notify_breakpoint_modified (b);
5574 }
5575 }
5576
5577 /* Returns true if we need to track moribund locations of LOC's type
5578 on the current target. */
5579
5580 static int
5581 need_moribund_for_location_type (struct bp_location *loc)
5582 {
5583 return ((loc->loc_type == bp_loc_software_breakpoint
5584 && !target_supports_stopped_by_sw_breakpoint ())
5585 || (loc->loc_type == bp_loc_hardware_breakpoint
5586 && !target_supports_stopped_by_hw_breakpoint ()));
5587 }
5588
5589
5590 /* Get a bpstat associated with having just stopped at address
5591 BP_ADDR in thread PTID.
5592
5593 Determine whether we stopped at a breakpoint, etc, or whether we
5594 don't understand this stop. Result is a chain of bpstat's such
5595 that:
5596
5597 if we don't understand the stop, the result is a null pointer.
5598
5599 if we understand why we stopped, the result is not null.
5600
5601 Each element of the chain refers to a particular breakpoint or
5602 watchpoint at which we have stopped. (We may have stopped for
5603 several reasons concurrently.)
5604
5605 Each element of the chain has valid next, breakpoint_at,
5606 commands, FIXME??? fields. */
5607
5608 bpstat
5609 bpstat_stop_status (struct address_space *aspace,
5610 CORE_ADDR bp_addr, ptid_t ptid,
5611 const struct target_waitstatus *ws)
5612 {
5613 struct breakpoint *b = NULL;
5614 struct bp_location *bl;
5615 struct bp_location *loc;
5616 /* First item of allocated bpstat's. */
5617 bpstat bs_head = NULL, *bs_link = &bs_head;
5618 /* Pointer to the last thing in the chain currently. */
5619 bpstat bs;
5620 int ix;
5621 int need_remove_insert;
5622 int removed_any;
5623
5624 /* First, build the bpstat chain with locations that explain a
5625 target stop, while being careful to not set the target running,
5626 as that may invalidate locations (in particular watchpoint
5627 locations are recreated). Resuming will happen here with
5628 breakpoint conditions or watchpoint expressions that include
5629 inferior function calls. */
5630
5631 ALL_BREAKPOINTS (b)
5632 {
5633 if (!breakpoint_enabled (b))
5634 continue;
5635
5636 for (bl = b->loc; bl != NULL; bl = bl->next)
5637 {
5638 /* For hardware watchpoints, we look only at the first
5639 location. The watchpoint_check function will work on the
5640 entire expression, not the individual locations. For
5641 read watchpoints, the watchpoints_triggered function has
5642 checked all locations already. */
5643 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5644 break;
5645
5646 if (!bl->enabled || bl->shlib_disabled)
5647 continue;
5648
5649 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
5650 continue;
5651
5652 /* Come here if it's a watchpoint, or if the break address
5653 matches. */
5654
5655 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
5656 explain stop. */
5657
5658 /* Assume we stop. Should we find a watchpoint that is not
5659 actually triggered, or if the condition of the breakpoint
5660 evaluates as false, we'll reset 'stop' to 0. */
5661 bs->stop = 1;
5662 bs->print = 1;
5663
5664 /* If this is a scope breakpoint, mark the associated
5665 watchpoint as triggered so that we will handle the
5666 out-of-scope event. We'll get to the watchpoint next
5667 iteration. */
5668 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
5669 {
5670 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5671
5672 w->watchpoint_triggered = watch_triggered_yes;
5673 }
5674 }
5675 }
5676
5677 /* Check if a moribund breakpoint explains the stop. */
5678 if (!target_supports_stopped_by_sw_breakpoint ()
5679 || !target_supports_stopped_by_hw_breakpoint ())
5680 {
5681 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
5682 {
5683 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5684 && need_moribund_for_location_type (loc))
5685 {
5686 bs = bpstat_alloc (loc, &bs_link);
5687 /* For hits of moribund locations, we should just proceed. */
5688 bs->stop = 0;
5689 bs->print = 0;
5690 bs->print_it = print_it_noop;
5691 }
5692 }
5693 }
5694
5695 /* A bit of special processing for shlib breakpoints. We need to
5696 process solib loading here, so that the lists of loaded and
5697 unloaded libraries are correct before we handle "catch load" and
5698 "catch unload". */
5699 for (bs = bs_head; bs != NULL; bs = bs->next)
5700 {
5701 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
5702 {
5703 handle_solib_event ();
5704 break;
5705 }
5706 }
5707
5708 /* Now go through the locations that caused the target to stop, and
5709 check whether we're interested in reporting this stop to higher
5710 layers, or whether we should resume the target transparently. */
5711
5712 removed_any = 0;
5713
5714 for (bs = bs_head; bs != NULL; bs = bs->next)
5715 {
5716 if (!bs->stop)
5717 continue;
5718
5719 b = bs->breakpoint_at;
5720 b->ops->check_status (bs);
5721 if (bs->stop)
5722 {
5723 bpstat_check_breakpoint_conditions (bs, ptid);
5724
5725 if (bs->stop)
5726 {
5727 ++(b->hit_count);
5728 observer_notify_breakpoint_modified (b);
5729
5730 /* We will stop here. */
5731 if (b->disposition == disp_disable)
5732 {
5733 --(b->enable_count);
5734 if (b->enable_count <= 0)
5735 b->enable_state = bp_disabled;
5736 removed_any = 1;
5737 }
5738 if (b->silent)
5739 bs->print = 0;
5740 bs->commands = b->commands;
5741 incref_counted_command_line (bs->commands);
5742 if (command_line_is_silent (bs->commands
5743 ? bs->commands->commands : NULL))
5744 bs->print = 0;
5745
5746 b->ops->after_condition_true (bs);
5747 }
5748
5749 }
5750
5751 /* Print nothing for this entry if we don't stop or don't
5752 print. */
5753 if (!bs->stop || !bs->print)
5754 bs->print_it = print_it_noop;
5755 }
5756
5757 /* If we aren't stopping, the value of some hardware watchpoint may
5758 not have changed, but the intermediate memory locations we are
5759 watching may have. Don't bother if we're stopping; this will get
5760 done later. */
5761 need_remove_insert = 0;
5762 if (! bpstat_causes_stop (bs_head))
5763 for (bs = bs_head; bs != NULL; bs = bs->next)
5764 if (!bs->stop
5765 && bs->breakpoint_at
5766 && is_hardware_watchpoint (bs->breakpoint_at))
5767 {
5768 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5769
5770 update_watchpoint (w, 0 /* don't reparse. */);
5771 need_remove_insert = 1;
5772 }
5773
5774 if (need_remove_insert)
5775 update_global_location_list (UGLL_MAY_INSERT);
5776 else if (removed_any)
5777 update_global_location_list (UGLL_DONT_INSERT);
5778
5779 return bs_head;
5780 }
5781
5782 static void
5783 handle_jit_event (void)
5784 {
5785 struct frame_info *frame;
5786 struct gdbarch *gdbarch;
5787
5788 if (debug_infrun)
5789 fprintf_unfiltered (gdb_stdlog, "handling bp_jit_event\n");
5790
5791 /* Switch terminal for any messages produced by
5792 breakpoint_re_set. */
5793 target_terminal_ours_for_output ();
5794
5795 frame = get_current_frame ();
5796 gdbarch = get_frame_arch (frame);
5797
5798 jit_event_handler (gdbarch);
5799
5800 target_terminal_inferior ();
5801 }
5802
5803 /* Prepare WHAT final decision for infrun. */
5804
5805 /* Decide what infrun needs to do with this bpstat. */
5806
5807 struct bpstat_what
5808 bpstat_what (bpstat bs_head)
5809 {
5810 struct bpstat_what retval;
5811 int jit_event = 0;
5812 bpstat bs;
5813
5814 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
5815 retval.call_dummy = STOP_NONE;
5816 retval.is_longjmp = 0;
5817
5818 for (bs = bs_head; bs != NULL; bs = bs->next)
5819 {
5820 /* Extract this BS's action. After processing each BS, we check
5821 if its action overrides all we've seem so far. */
5822 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5823 enum bptype bptype;
5824
5825 if (bs->breakpoint_at == NULL)
5826 {
5827 /* I suspect this can happen if it was a momentary
5828 breakpoint which has since been deleted. */
5829 bptype = bp_none;
5830 }
5831 else
5832 bptype = bs->breakpoint_at->type;
5833
5834 switch (bptype)
5835 {
5836 case bp_none:
5837 break;
5838 case bp_breakpoint:
5839 case bp_hardware_breakpoint:
5840 case bp_single_step:
5841 case bp_until:
5842 case bp_finish:
5843 case bp_shlib_event:
5844 if (bs->stop)
5845 {
5846 if (bs->print)
5847 this_action = BPSTAT_WHAT_STOP_NOISY;
5848 else
5849 this_action = BPSTAT_WHAT_STOP_SILENT;
5850 }
5851 else
5852 this_action = BPSTAT_WHAT_SINGLE;
5853 break;
5854 case bp_watchpoint:
5855 case bp_hardware_watchpoint:
5856 case bp_read_watchpoint:
5857 case bp_access_watchpoint:
5858 if (bs->stop)
5859 {
5860 if (bs->print)
5861 this_action = BPSTAT_WHAT_STOP_NOISY;
5862 else
5863 this_action = BPSTAT_WHAT_STOP_SILENT;
5864 }
5865 else
5866 {
5867 /* There was a watchpoint, but we're not stopping.
5868 This requires no further action. */
5869 }
5870 break;
5871 case bp_longjmp:
5872 case bp_longjmp_call_dummy:
5873 case bp_exception:
5874 if (bs->stop)
5875 {
5876 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5877 retval.is_longjmp = bptype != bp_exception;
5878 }
5879 else
5880 this_action = BPSTAT_WHAT_SINGLE;
5881 break;
5882 case bp_longjmp_resume:
5883 case bp_exception_resume:
5884 if (bs->stop)
5885 {
5886 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5887 retval.is_longjmp = bptype == bp_longjmp_resume;
5888 }
5889 else
5890 this_action = BPSTAT_WHAT_SINGLE;
5891 break;
5892 case bp_step_resume:
5893 if (bs->stop)
5894 this_action = BPSTAT_WHAT_STEP_RESUME;
5895 else
5896 {
5897 /* It is for the wrong frame. */
5898 this_action = BPSTAT_WHAT_SINGLE;
5899 }
5900 break;
5901 case bp_hp_step_resume:
5902 if (bs->stop)
5903 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5904 else
5905 {
5906 /* It is for the wrong frame. */
5907 this_action = BPSTAT_WHAT_SINGLE;
5908 }
5909 break;
5910 case bp_watchpoint_scope:
5911 case bp_thread_event:
5912 case bp_overlay_event:
5913 case bp_longjmp_master:
5914 case bp_std_terminate_master:
5915 case bp_exception_master:
5916 this_action = BPSTAT_WHAT_SINGLE;
5917 break;
5918 case bp_catchpoint:
5919 if (bs->stop)
5920 {
5921 if (bs->print)
5922 this_action = BPSTAT_WHAT_STOP_NOISY;
5923 else
5924 this_action = BPSTAT_WHAT_STOP_SILENT;
5925 }
5926 else
5927 {
5928 /* There was a catchpoint, but we're not stopping.
5929 This requires no further action. */
5930 }
5931 break;
5932 case bp_jit_event:
5933 jit_event = 1;
5934 this_action = BPSTAT_WHAT_SINGLE;
5935 break;
5936 case bp_call_dummy:
5937 /* Make sure the action is stop (silent or noisy),
5938 so infrun.c pops the dummy frame. */
5939 retval.call_dummy = STOP_STACK_DUMMY;
5940 this_action = BPSTAT_WHAT_STOP_SILENT;
5941 break;
5942 case bp_std_terminate:
5943 /* Make sure the action is stop (silent or noisy),
5944 so infrun.c pops the dummy frame. */
5945 retval.call_dummy = STOP_STD_TERMINATE;
5946 this_action = BPSTAT_WHAT_STOP_SILENT;
5947 break;
5948 case bp_tracepoint:
5949 case bp_fast_tracepoint:
5950 case bp_static_tracepoint:
5951 /* Tracepoint hits should not be reported back to GDB, and
5952 if one got through somehow, it should have been filtered
5953 out already. */
5954 internal_error (__FILE__, __LINE__,
5955 _("bpstat_what: tracepoint encountered"));
5956 break;
5957 case bp_gnu_ifunc_resolver:
5958 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5959 this_action = BPSTAT_WHAT_SINGLE;
5960 break;
5961 case bp_gnu_ifunc_resolver_return:
5962 /* The breakpoint will be removed, execution will restart from the
5963 PC of the former breakpoint. */
5964 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5965 break;
5966
5967 case bp_dprintf:
5968 if (bs->stop)
5969 this_action = BPSTAT_WHAT_STOP_SILENT;
5970 else
5971 this_action = BPSTAT_WHAT_SINGLE;
5972 break;
5973
5974 default:
5975 internal_error (__FILE__, __LINE__,
5976 _("bpstat_what: unhandled bptype %d"), (int) bptype);
5977 }
5978
5979 retval.main_action = max (retval.main_action, this_action);
5980 }
5981
5982 return retval;
5983 }
5984
5985 void
5986 bpstat_run_callbacks (bpstat bs_head)
5987 {
5988 bpstat bs;
5989
5990 for (bs = bs_head; bs != NULL; bs = bs->next)
5991 {
5992 struct breakpoint *b = bs->breakpoint_at;
5993
5994 if (b == NULL)
5995 continue;
5996 switch (b->type)
5997 {
5998 case bp_jit_event:
5999 handle_jit_event ();
6000 break;
6001 case bp_gnu_ifunc_resolver:
6002 gnu_ifunc_resolver_stop (b);
6003 break;
6004 case bp_gnu_ifunc_resolver_return:
6005 gnu_ifunc_resolver_return_stop (b);
6006 break;
6007 }
6008 }
6009 }
6010
6011 /* Nonzero if we should step constantly (e.g. watchpoints on machines
6012 without hardware support). This isn't related to a specific bpstat,
6013 just to things like whether watchpoints are set. */
6014
6015 int
6016 bpstat_should_step (void)
6017 {
6018 struct breakpoint *b;
6019
6020 ALL_BREAKPOINTS (b)
6021 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
6022 return 1;
6023 return 0;
6024 }
6025
6026 int
6027 bpstat_causes_stop (bpstat bs)
6028 {
6029 for (; bs != NULL; bs = bs->next)
6030 if (bs->stop)
6031 return 1;
6032
6033 return 0;
6034 }
6035
6036 \f
6037
6038 /* Compute a string of spaces suitable to indent the next line
6039 so it starts at the position corresponding to the table column
6040 named COL_NAME in the currently active table of UIOUT. */
6041
6042 static char *
6043 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
6044 {
6045 static char wrap_indent[80];
6046 int i, total_width, width, align;
6047 char *text;
6048
6049 total_width = 0;
6050 for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
6051 {
6052 if (strcmp (text, col_name) == 0)
6053 {
6054 gdb_assert (total_width < sizeof wrap_indent);
6055 memset (wrap_indent, ' ', total_width);
6056 wrap_indent[total_width] = 0;
6057
6058 return wrap_indent;
6059 }
6060
6061 total_width += width + 1;
6062 }
6063
6064 return NULL;
6065 }
6066
6067 /* Determine if the locations of this breakpoint will have their conditions
6068 evaluated by the target, host or a mix of both. Returns the following:
6069
6070 "host": Host evals condition.
6071 "host or target": Host or Target evals condition.
6072 "target": Target evals condition.
6073 */
6074
6075 static const char *
6076 bp_condition_evaluator (struct breakpoint *b)
6077 {
6078 struct bp_location *bl;
6079 char host_evals = 0;
6080 char target_evals = 0;
6081
6082 if (!b)
6083 return NULL;
6084
6085 if (!is_breakpoint (b))
6086 return NULL;
6087
6088 if (gdb_evaluates_breakpoint_condition_p ()
6089 || !target_supports_evaluation_of_breakpoint_conditions ())
6090 return condition_evaluation_host;
6091
6092 for (bl = b->loc; bl; bl = bl->next)
6093 {
6094 if (bl->cond_bytecode)
6095 target_evals++;
6096 else
6097 host_evals++;
6098 }
6099
6100 if (host_evals && target_evals)
6101 return condition_evaluation_both;
6102 else if (target_evals)
6103 return condition_evaluation_target;
6104 else
6105 return condition_evaluation_host;
6106 }
6107
6108 /* Determine the breakpoint location's condition evaluator. This is
6109 similar to bp_condition_evaluator, but for locations. */
6110
6111 static const char *
6112 bp_location_condition_evaluator (struct bp_location *bl)
6113 {
6114 if (bl && !is_breakpoint (bl->owner))
6115 return NULL;
6116
6117 if (gdb_evaluates_breakpoint_condition_p ()
6118 || !target_supports_evaluation_of_breakpoint_conditions ())
6119 return condition_evaluation_host;
6120
6121 if (bl && bl->cond_bytecode)
6122 return condition_evaluation_target;
6123 else
6124 return condition_evaluation_host;
6125 }
6126
6127 /* Print the LOC location out of the list of B->LOC locations. */
6128
6129 static void
6130 print_breakpoint_location (struct breakpoint *b,
6131 struct bp_location *loc)
6132 {
6133 struct ui_out *uiout = current_uiout;
6134 struct cleanup *old_chain = save_current_program_space ();
6135
6136 if (loc != NULL && loc->shlib_disabled)
6137 loc = NULL;
6138
6139 if (loc != NULL)
6140 set_current_program_space (loc->pspace);
6141
6142 if (b->display_canonical)
6143 ui_out_field_string (uiout, "what",
6144 event_location_to_string (b->location));
6145 else if (loc && loc->symtab)
6146 {
6147 struct symbol *sym
6148 = find_pc_sect_function (loc->address, loc->section);
6149 if (sym)
6150 {
6151 ui_out_text (uiout, "in ");
6152 ui_out_field_string (uiout, "func",
6153 SYMBOL_PRINT_NAME (sym));
6154 ui_out_text (uiout, " ");
6155 ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
6156 ui_out_text (uiout, "at ");
6157 }
6158 ui_out_field_string (uiout, "file",
6159 symtab_to_filename_for_display (loc->symtab));
6160 ui_out_text (uiout, ":");
6161
6162 if (ui_out_is_mi_like_p (uiout))
6163 ui_out_field_string (uiout, "fullname",
6164 symtab_to_fullname (loc->symtab));
6165
6166 ui_out_field_int (uiout, "line", loc->line_number);
6167 }
6168 else if (loc)
6169 {
6170 struct ui_file *stb = mem_fileopen ();
6171 struct cleanup *stb_chain = make_cleanup_ui_file_delete (stb);
6172
6173 print_address_symbolic (loc->gdbarch, loc->address, stb,
6174 demangle, "");
6175 ui_out_field_stream (uiout, "at", stb);
6176
6177 do_cleanups (stb_chain);
6178 }
6179 else
6180 {
6181 ui_out_field_string (uiout, "pending",
6182 event_location_to_string (b->location));
6183 /* If extra_string is available, it could be holding a condition
6184 or dprintf arguments. In either case, make sure it is printed,
6185 too, but only for non-MI streams. */
6186 if (!ui_out_is_mi_like_p (uiout) && b->extra_string != NULL)
6187 {
6188 if (b->type == bp_dprintf)
6189 ui_out_text (uiout, ",");
6190 else
6191 ui_out_text (uiout, " ");
6192 ui_out_text (uiout, b->extra_string);
6193 }
6194 }
6195
6196 if (loc && is_breakpoint (b)
6197 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6198 && bp_condition_evaluator (b) == condition_evaluation_both)
6199 {
6200 ui_out_text (uiout, " (");
6201 ui_out_field_string (uiout, "evaluated-by",
6202 bp_location_condition_evaluator (loc));
6203 ui_out_text (uiout, ")");
6204 }
6205
6206 do_cleanups (old_chain);
6207 }
6208
6209 static const char *
6210 bptype_string (enum bptype type)
6211 {
6212 struct ep_type_description
6213 {
6214 enum bptype type;
6215 char *description;
6216 };
6217 static struct ep_type_description bptypes[] =
6218 {
6219 {bp_none, "?deleted?"},
6220 {bp_breakpoint, "breakpoint"},
6221 {bp_hardware_breakpoint, "hw breakpoint"},
6222 {bp_single_step, "sw single-step"},
6223 {bp_until, "until"},
6224 {bp_finish, "finish"},
6225 {bp_watchpoint, "watchpoint"},
6226 {bp_hardware_watchpoint, "hw watchpoint"},
6227 {bp_read_watchpoint, "read watchpoint"},
6228 {bp_access_watchpoint, "acc watchpoint"},
6229 {bp_longjmp, "longjmp"},
6230 {bp_longjmp_resume, "longjmp resume"},
6231 {bp_longjmp_call_dummy, "longjmp for call dummy"},
6232 {bp_exception, "exception"},
6233 {bp_exception_resume, "exception resume"},
6234 {bp_step_resume, "step resume"},
6235 {bp_hp_step_resume, "high-priority step resume"},
6236 {bp_watchpoint_scope, "watchpoint scope"},
6237 {bp_call_dummy, "call dummy"},
6238 {bp_std_terminate, "std::terminate"},
6239 {bp_shlib_event, "shlib events"},
6240 {bp_thread_event, "thread events"},
6241 {bp_overlay_event, "overlay events"},
6242 {bp_longjmp_master, "longjmp master"},
6243 {bp_std_terminate_master, "std::terminate master"},
6244 {bp_exception_master, "exception master"},
6245 {bp_catchpoint, "catchpoint"},
6246 {bp_tracepoint, "tracepoint"},
6247 {bp_fast_tracepoint, "fast tracepoint"},
6248 {bp_static_tracepoint, "static tracepoint"},
6249 {bp_dprintf, "dprintf"},
6250 {bp_jit_event, "jit events"},
6251 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6252 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
6253 };
6254
6255 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6256 || ((int) type != bptypes[(int) type].type))
6257 internal_error (__FILE__, __LINE__,
6258 _("bptypes table does not describe type #%d."),
6259 (int) type);
6260
6261 return bptypes[(int) type].description;
6262 }
6263
6264 /* For MI, output a field named 'thread-groups' with a list as the value.
6265 For CLI, prefix the list with the string 'inf'. */
6266
6267 static void
6268 output_thread_groups (struct ui_out *uiout,
6269 const char *field_name,
6270 VEC(int) *inf_num,
6271 int mi_only)
6272 {
6273 struct cleanup *back_to;
6274 int is_mi = ui_out_is_mi_like_p (uiout);
6275 int inf;
6276 int i;
6277
6278 /* For backward compatibility, don't display inferiors in CLI unless
6279 there are several. Always display them for MI. */
6280 if (!is_mi && mi_only)
6281 return;
6282
6283 back_to = make_cleanup_ui_out_list_begin_end (uiout, field_name);
6284
6285 for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
6286 {
6287 if (is_mi)
6288 {
6289 char mi_group[10];
6290
6291 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
6292 ui_out_field_string (uiout, NULL, mi_group);
6293 }
6294 else
6295 {
6296 if (i == 0)
6297 ui_out_text (uiout, " inf ");
6298 else
6299 ui_out_text (uiout, ", ");
6300
6301 ui_out_text (uiout, plongest (inf));
6302 }
6303 }
6304
6305 do_cleanups (back_to);
6306 }
6307
6308 /* Print B to gdb_stdout. */
6309
6310 static void
6311 print_one_breakpoint_location (struct breakpoint *b,
6312 struct bp_location *loc,
6313 int loc_number,
6314 struct bp_location **last_loc,
6315 int allflag)
6316 {
6317 struct command_line *l;
6318 static char bpenables[] = "nynny";
6319
6320 struct ui_out *uiout = current_uiout;
6321 int header_of_multiple = 0;
6322 int part_of_multiple = (loc != NULL);
6323 struct value_print_options opts;
6324
6325 get_user_print_options (&opts);
6326
6327 gdb_assert (!loc || loc_number != 0);
6328 /* See comment in print_one_breakpoint concerning treatment of
6329 breakpoints with single disabled location. */
6330 if (loc == NULL
6331 && (b->loc != NULL
6332 && (b->loc->next != NULL || !b->loc->enabled)))
6333 header_of_multiple = 1;
6334 if (loc == NULL)
6335 loc = b->loc;
6336
6337 annotate_record ();
6338
6339 /* 1 */
6340 annotate_field (0);
6341 if (part_of_multiple)
6342 {
6343 char *formatted;
6344 formatted = xstrprintf ("%d.%d", b->number, loc_number);
6345 ui_out_field_string (uiout, "number", formatted);
6346 xfree (formatted);
6347 }
6348 else
6349 {
6350 ui_out_field_int (uiout, "number", b->number);
6351 }
6352
6353 /* 2 */
6354 annotate_field (1);
6355 if (part_of_multiple)
6356 ui_out_field_skip (uiout, "type");
6357 else
6358 ui_out_field_string (uiout, "type", bptype_string (b->type));
6359
6360 /* 3 */
6361 annotate_field (2);
6362 if (part_of_multiple)
6363 ui_out_field_skip (uiout, "disp");
6364 else
6365 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
6366
6367
6368 /* 4 */
6369 annotate_field (3);
6370 if (part_of_multiple)
6371 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
6372 else
6373 ui_out_field_fmt (uiout, "enabled", "%c",
6374 bpenables[(int) b->enable_state]);
6375 ui_out_spaces (uiout, 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 ui_out_field_skip (uiout, "addr");
6407 annotate_field (5);
6408 ui_out_field_string (uiout, "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 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
6445 else if (b->loc == NULL || loc->shlib_disabled)
6446 ui_out_field_string (uiout, "addr", "<PENDING>");
6447 else
6448 ui_out_field_core_addr (uiout, "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 ui_out_text (uiout, " thread ");
6493 ui_out_field_int (uiout, "thread", b->thread);
6494 }
6495 else if (b->task != 0)
6496 {
6497 ui_out_text (uiout, " task ");
6498 ui_out_field_int (uiout, "task", b->task);
6499 }
6500 }
6501
6502 ui_out_text (uiout, "\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 ui_out_text (uiout, "\tstop only in stack frame at ");
6511 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
6512 the frame ID. */
6513 ui_out_field_core_addr (uiout, "frame",
6514 b->gdbarch, b->frame_id.stack_addr);
6515 ui_out_text (uiout, "\n");
6516 }
6517
6518 if (!part_of_multiple && b->cond_string)
6519 {
6520 annotate_field (7);
6521 if (is_tracepoint (b))
6522 ui_out_text (uiout, "\ttrace only if ");
6523 else
6524 ui_out_text (uiout, "\tstop only if ");
6525 ui_out_field_string (uiout, "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 ui_out_text (uiout, " (");
6534 ui_out_field_string (uiout, "evaluated-by",
6535 bp_condition_evaluator (b));
6536 ui_out_text (uiout, " evals)");
6537 }
6538 ui_out_text (uiout, "\n");
6539 }
6540
6541 if (!part_of_multiple && b->thread != -1)
6542 {
6543 /* FIXME should make an annotation for this. */
6544 ui_out_text (uiout, "\tstop only in thread ");
6545 if (ui_out_is_mi_like_p (uiout))
6546 ui_out_field_int (uiout, "thread", b->thread);
6547 else
6548 {
6549 struct thread_info *thr = find_thread_global_id (b->thread);
6550
6551 ui_out_field_string (uiout, "thread", print_thread_id (thr));
6552 }
6553 ui_out_text (uiout, "\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 ui_out_text (uiout, "\tcatchpoint");
6563 else if (is_tracepoint (b))
6564 ui_out_text (uiout, "\ttracepoint");
6565 else
6566 ui_out_text (uiout, "\tbreakpoint");
6567 ui_out_text (uiout, " already hit ");
6568 ui_out_field_int (uiout, "times", b->hit_count);
6569 if (b->hit_count == 1)
6570 ui_out_text (uiout, " time\n");
6571 else
6572 ui_out_text (uiout, " times\n");
6573 }
6574 else
6575 {
6576 /* Output the count also if it is zero, but only if this is mi. */
6577 if (ui_out_is_mi_like_p (uiout))
6578 ui_out_field_int (uiout, "times", b->hit_count);
6579 }
6580 }
6581
6582 if (!part_of_multiple && b->ignore_count)
6583 {
6584 annotate_field (8);
6585 ui_out_text (uiout, "\tignore next ");
6586 ui_out_field_int (uiout, "ignore", b->ignore_count);
6587 ui_out_text (uiout, " 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 ui_out_text (uiout, "\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 ui_out_text (uiout, "additional ");
6601 else
6602 ui_out_text (uiout, "next ");
6603 ui_out_field_int (uiout, "enable", b->enable_count);
6604 ui_out_text (uiout, " 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 ui_out_text (uiout, "\ttrace buffer usage ");
6614 ui_out_field_int (uiout, "traceframe-usage", tp->traceframe_usage);
6615 ui_out_text (uiout, " 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 ui_out_text (uiout, "\tpass count ");
6638 ui_out_field_int (uiout, "pass", t->pass_count);
6639 ui_out_text (uiout, " \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 (ui_out_is_mi_like_p (uiout))
6649 ui_out_field_string (uiout, "installed",
6650 loc->inserted ? "y" : "n");
6651 else
6652 {
6653 if (loc->inserted)
6654 ui_out_text (uiout, "\t");
6655 else
6656 ui_out_text (uiout, "\tnot ");
6657 ui_out_text (uiout, "installed on target\n");
6658 }
6659 }
6660 }
6661
6662 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
6663 {
6664 if (is_watchpoint (b))
6665 {
6666 struct watchpoint *w = (struct watchpoint *) b;
6667
6668 ui_out_field_string (uiout, "original-location", w->exp_string);
6669 }
6670 else if (b->location != NULL
6671 && event_location_to_string (b->location) != NULL)
6672 ui_out_field_string (uiout, "original-location",
6673 event_location_to_string (b->location));
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 /* Print information on user settable breakpoint (watchpoint, etc)
6793 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6794 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6795 FILTER is non-NULL, call it on each breakpoint and only include the
6796 ones for which it returns non-zero. Return the total number of
6797 breakpoints listed. */
6798
6799 static int
6800 breakpoint_1 (char *args, int allflag,
6801 int (*filter) (const struct breakpoint *))
6802 {
6803 struct breakpoint *b;
6804 struct bp_location *last_loc = NULL;
6805 int nr_printable_breakpoints;
6806 struct cleanup *bkpttbl_chain;
6807 struct value_print_options opts;
6808 int print_address_bits = 0;
6809 int print_type_col_width = 14;
6810 struct ui_out *uiout = current_uiout;
6811
6812 get_user_print_options (&opts);
6813
6814 /* Compute the number of rows in the table, as well as the size
6815 required for address fields. */
6816 nr_printable_breakpoints = 0;
6817 ALL_BREAKPOINTS (b)
6818 {
6819 /* If we have a filter, only list the breakpoints it accepts. */
6820 if (filter && !filter (b))
6821 continue;
6822
6823 /* If we have an "args" string, it is a list of breakpoints to
6824 accept. Skip the others. */
6825 if (args != NULL && *args != '\0')
6826 {
6827 if (allflag && parse_and_eval_long (args) != b->number)
6828 continue;
6829 if (!allflag && !number_is_in_list (args, b->number))
6830 continue;
6831 }
6832
6833 if (allflag || user_breakpoint_p (b))
6834 {
6835 int addr_bit, type_len;
6836
6837 addr_bit = breakpoint_address_bits (b);
6838 if (addr_bit > print_address_bits)
6839 print_address_bits = addr_bit;
6840
6841 type_len = strlen (bptype_string (b->type));
6842 if (type_len > print_type_col_width)
6843 print_type_col_width = type_len;
6844
6845 nr_printable_breakpoints++;
6846 }
6847 }
6848
6849 if (opts.addressprint)
6850 bkpttbl_chain
6851 = make_cleanup_ui_out_table_begin_end (uiout, 6,
6852 nr_printable_breakpoints,
6853 "BreakpointTable");
6854 else
6855 bkpttbl_chain
6856 = make_cleanup_ui_out_table_begin_end (uiout, 5,
6857 nr_printable_breakpoints,
6858 "BreakpointTable");
6859
6860 if (nr_printable_breakpoints > 0)
6861 annotate_breakpoints_headers ();
6862 if (nr_printable_breakpoints > 0)
6863 annotate_field (0);
6864 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
6865 if (nr_printable_breakpoints > 0)
6866 annotate_field (1);
6867 ui_out_table_header (uiout, print_type_col_width, ui_left,
6868 "type", "Type"); /* 2 */
6869 if (nr_printable_breakpoints > 0)
6870 annotate_field (2);
6871 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
6872 if (nr_printable_breakpoints > 0)
6873 annotate_field (3);
6874 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
6875 if (opts.addressprint)
6876 {
6877 if (nr_printable_breakpoints > 0)
6878 annotate_field (4);
6879 if (print_address_bits <= 32)
6880 ui_out_table_header (uiout, 10, ui_left,
6881 "addr", "Address"); /* 5 */
6882 else
6883 ui_out_table_header (uiout, 18, ui_left,
6884 "addr", "Address"); /* 5 */
6885 }
6886 if (nr_printable_breakpoints > 0)
6887 annotate_field (5);
6888 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
6889 ui_out_table_body (uiout);
6890 if (nr_printable_breakpoints > 0)
6891 annotate_breakpoints_table ();
6892
6893 ALL_BREAKPOINTS (b)
6894 {
6895 QUIT;
6896 /* If we have a filter, only list the breakpoints it accepts. */
6897 if (filter && !filter (b))
6898 continue;
6899
6900 /* If we have an "args" string, it is a list of breakpoints to
6901 accept. Skip the others. */
6902
6903 if (args != NULL && *args != '\0')
6904 {
6905 if (allflag) /* maintenance info breakpoint */
6906 {
6907 if (parse_and_eval_long (args) != b->number)
6908 continue;
6909 }
6910 else /* all others */
6911 {
6912 if (!number_is_in_list (args, b->number))
6913 continue;
6914 }
6915 }
6916 /* We only print out user settable breakpoints unless the
6917 allflag is set. */
6918 if (allflag || user_breakpoint_p (b))
6919 print_one_breakpoint (b, &last_loc, allflag);
6920 }
6921
6922 do_cleanups (bkpttbl_chain);
6923
6924 if (nr_printable_breakpoints == 0)
6925 {
6926 /* If there's a filter, let the caller decide how to report
6927 empty list. */
6928 if (!filter)
6929 {
6930 if (args == NULL || *args == '\0')
6931 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
6932 else
6933 ui_out_message (uiout, 0,
6934 "No breakpoint or watchpoint matching '%s'.\n",
6935 args);
6936 }
6937 }
6938 else
6939 {
6940 if (last_loc && !server_command)
6941 set_next_address (last_loc->gdbarch, last_loc->address);
6942 }
6943
6944 /* FIXME? Should this be moved up so that it is only called when
6945 there have been breakpoints? */
6946 annotate_breakpoints_table_end ();
6947
6948 return nr_printable_breakpoints;
6949 }
6950
6951 /* Display the value of default-collect in a way that is generally
6952 compatible with the breakpoint list. */
6953
6954 static void
6955 default_collect_info (void)
6956 {
6957 struct ui_out *uiout = current_uiout;
6958
6959 /* If it has no value (which is frequently the case), say nothing; a
6960 message like "No default-collect." gets in user's face when it's
6961 not wanted. */
6962 if (!*default_collect)
6963 return;
6964
6965 /* The following phrase lines up nicely with per-tracepoint collect
6966 actions. */
6967 ui_out_text (uiout, "default collect ");
6968 ui_out_field_string (uiout, "default-collect", default_collect);
6969 ui_out_text (uiout, " \n");
6970 }
6971
6972 static void
6973 breakpoints_info (char *args, int from_tty)
6974 {
6975 breakpoint_1 (args, 0, NULL);
6976
6977 default_collect_info ();
6978 }
6979
6980 static void
6981 watchpoints_info (char *args, int from_tty)
6982 {
6983 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
6984 struct ui_out *uiout = current_uiout;
6985
6986 if (num_printed == 0)
6987 {
6988 if (args == NULL || *args == '\0')
6989 ui_out_message (uiout, 0, "No watchpoints.\n");
6990 else
6991 ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
6992 }
6993 }
6994
6995 static void
6996 maintenance_info_breakpoints (char *args, int from_tty)
6997 {
6998 breakpoint_1 (args, 1, NULL);
6999
7000 default_collect_info ();
7001 }
7002
7003 static int
7004 breakpoint_has_pc (struct breakpoint *b,
7005 struct program_space *pspace,
7006 CORE_ADDR pc, struct obj_section *section)
7007 {
7008 struct bp_location *bl = b->loc;
7009
7010 for (; bl; bl = bl->next)
7011 {
7012 if (bl->pspace == pspace
7013 && bl->address == pc
7014 && (!overlay_debugging || bl->section == section))
7015 return 1;
7016 }
7017 return 0;
7018 }
7019
7020 /* Print a message describing any user-breakpoints set at PC. This
7021 concerns with logical breakpoints, so we match program spaces, not
7022 address spaces. */
7023
7024 static void
7025 describe_other_breakpoints (struct gdbarch *gdbarch,
7026 struct program_space *pspace, CORE_ADDR pc,
7027 struct obj_section *section, int thread)
7028 {
7029 int others = 0;
7030 struct breakpoint *b;
7031
7032 ALL_BREAKPOINTS (b)
7033 others += (user_breakpoint_p (b)
7034 && breakpoint_has_pc (b, pspace, pc, section));
7035 if (others > 0)
7036 {
7037 if (others == 1)
7038 printf_filtered (_("Note: breakpoint "));
7039 else /* if (others == ???) */
7040 printf_filtered (_("Note: breakpoints "));
7041 ALL_BREAKPOINTS (b)
7042 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
7043 {
7044 others--;
7045 printf_filtered ("%d", b->number);
7046 if (b->thread == -1 && thread != -1)
7047 printf_filtered (" (all threads)");
7048 else if (b->thread != -1)
7049 printf_filtered (" (thread %d)", b->thread);
7050 printf_filtered ("%s%s ",
7051 ((b->enable_state == bp_disabled
7052 || b->enable_state == bp_call_disabled)
7053 ? " (disabled)"
7054 : ""),
7055 (others > 1) ? ","
7056 : ((others == 1) ? " and" : ""));
7057 }
7058 printf_filtered (_("also set at pc "));
7059 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
7060 printf_filtered (".\n");
7061 }
7062 }
7063 \f
7064
7065 /* Return true iff it is meaningful to use the address member of
7066 BPT locations. For some breakpoint types, the locations' address members
7067 are irrelevant and it makes no sense to attempt to compare them to other
7068 addresses (or use them for any other purpose either).
7069
7070 More specifically, each of the following breakpoint types will
7071 always have a zero valued location address and we don't want to mark
7072 breakpoints of any of these types to be a duplicate of an actual
7073 breakpoint location at address zero:
7074
7075 bp_watchpoint
7076 bp_catchpoint
7077
7078 */
7079
7080 static int
7081 breakpoint_address_is_meaningful (struct breakpoint *bpt)
7082 {
7083 enum bptype type = bpt->type;
7084
7085 return (type != bp_watchpoint && type != bp_catchpoint);
7086 }
7087
7088 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
7089 true if LOC1 and LOC2 represent the same watchpoint location. */
7090
7091 static int
7092 watchpoint_locations_match (struct bp_location *loc1,
7093 struct bp_location *loc2)
7094 {
7095 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
7096 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
7097
7098 /* Both of them must exist. */
7099 gdb_assert (w1 != NULL);
7100 gdb_assert (w2 != NULL);
7101
7102 /* If the target can evaluate the condition expression in hardware,
7103 then we we need to insert both watchpoints even if they are at
7104 the same place. Otherwise the watchpoint will only trigger when
7105 the condition of whichever watchpoint was inserted evaluates to
7106 true, not giving a chance for GDB to check the condition of the
7107 other watchpoint. */
7108 if ((w1->cond_exp
7109 && target_can_accel_watchpoint_condition (loc1->address,
7110 loc1->length,
7111 loc1->watchpoint_type,
7112 w1->cond_exp))
7113 || (w2->cond_exp
7114 && target_can_accel_watchpoint_condition (loc2->address,
7115 loc2->length,
7116 loc2->watchpoint_type,
7117 w2->cond_exp)))
7118 return 0;
7119
7120 /* Note that this checks the owner's type, not the location's. In
7121 case the target does not support read watchpoints, but does
7122 support access watchpoints, we'll have bp_read_watchpoint
7123 watchpoints with hw_access locations. Those should be considered
7124 duplicates of hw_read locations. The hw_read locations will
7125 become hw_access locations later. */
7126 return (loc1->owner->type == loc2->owner->type
7127 && loc1->pspace->aspace == loc2->pspace->aspace
7128 && loc1->address == loc2->address
7129 && loc1->length == loc2->length);
7130 }
7131
7132 /* See breakpoint.h. */
7133
7134 int
7135 breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
7136 struct address_space *aspace2, CORE_ADDR addr2)
7137 {
7138 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
7139 || aspace1 == aspace2)
7140 && addr1 == addr2);
7141 }
7142
7143 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
7144 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
7145 matches ASPACE2. On targets that have global breakpoints, the address
7146 space doesn't really matter. */
7147
7148 static int
7149 breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
7150 int len1, struct address_space *aspace2,
7151 CORE_ADDR addr2)
7152 {
7153 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
7154 || aspace1 == aspace2)
7155 && addr2 >= addr1 && addr2 < addr1 + len1);
7156 }
7157
7158 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
7159 a ranged breakpoint. In most targets, a match happens only if ASPACE
7160 matches the breakpoint's address space. On targets that have global
7161 breakpoints, the address space doesn't really matter. */
7162
7163 static int
7164 breakpoint_location_address_match (struct bp_location *bl,
7165 struct address_space *aspace,
7166 CORE_ADDR addr)
7167 {
7168 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
7169 aspace, addr)
7170 || (bl->length
7171 && breakpoint_address_match_range (bl->pspace->aspace,
7172 bl->address, bl->length,
7173 aspace, addr)));
7174 }
7175
7176 /* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
7177 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
7178 match happens only if ASPACE matches the breakpoint's address
7179 space. On targets that have global breakpoints, the address space
7180 doesn't really matter. */
7181
7182 static int
7183 breakpoint_location_address_range_overlap (struct bp_location *bl,
7184 struct address_space *aspace,
7185 CORE_ADDR addr, int len)
7186 {
7187 if (gdbarch_has_global_breakpoints (target_gdbarch ())
7188 || bl->pspace->aspace == aspace)
7189 {
7190 int bl_len = bl->length != 0 ? bl->length : 1;
7191
7192 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
7193 return 1;
7194 }
7195 return 0;
7196 }
7197
7198 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
7199 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
7200 true, otherwise returns false. */
7201
7202 static int
7203 tracepoint_locations_match (struct bp_location *loc1,
7204 struct bp_location *loc2)
7205 {
7206 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
7207 /* Since tracepoint locations are never duplicated with others', tracepoint
7208 locations at the same address of different tracepoints are regarded as
7209 different locations. */
7210 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7211 else
7212 return 0;
7213 }
7214
7215 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
7216 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
7217 represent the same location. */
7218
7219 static int
7220 breakpoint_locations_match (struct bp_location *loc1,
7221 struct bp_location *loc2)
7222 {
7223 int hw_point1, hw_point2;
7224
7225 /* Both of them must not be in moribund_locations. */
7226 gdb_assert (loc1->owner != NULL);
7227 gdb_assert (loc2->owner != NULL);
7228
7229 hw_point1 = is_hardware_watchpoint (loc1->owner);
7230 hw_point2 = is_hardware_watchpoint (loc2->owner);
7231
7232 if (hw_point1 != hw_point2)
7233 return 0;
7234 else if (hw_point1)
7235 return watchpoint_locations_match (loc1, loc2);
7236 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7237 return tracepoint_locations_match (loc1, loc2);
7238 else
7239 /* We compare bp_location.length in order to cover ranged breakpoints. */
7240 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7241 loc2->pspace->aspace, loc2->address)
7242 && loc1->length == loc2->length);
7243 }
7244
7245 static void
7246 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7247 int bnum, int have_bnum)
7248 {
7249 /* The longest string possibly returned by hex_string_custom
7250 is 50 chars. These must be at least that big for safety. */
7251 char astr1[64];
7252 char astr2[64];
7253
7254 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7255 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
7256 if (have_bnum)
7257 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
7258 bnum, astr1, astr2);
7259 else
7260 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
7261 }
7262
7263 /* Adjust a breakpoint's address to account for architectural
7264 constraints on breakpoint placement. Return the adjusted address.
7265 Note: Very few targets require this kind of adjustment. For most
7266 targets, this function is simply the identity function. */
7267
7268 static CORE_ADDR
7269 adjust_breakpoint_address (struct gdbarch *gdbarch,
7270 CORE_ADDR bpaddr, enum bptype bptype)
7271 {
7272 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
7273 {
7274 /* Very few targets need any kind of breakpoint adjustment. */
7275 return bpaddr;
7276 }
7277 else if (bptype == bp_watchpoint
7278 || bptype == bp_hardware_watchpoint
7279 || bptype == bp_read_watchpoint
7280 || bptype == bp_access_watchpoint
7281 || bptype == bp_catchpoint)
7282 {
7283 /* Watchpoints and the various bp_catch_* eventpoints should not
7284 have their addresses modified. */
7285 return bpaddr;
7286 }
7287 else if (bptype == bp_single_step)
7288 {
7289 /* Single-step breakpoints should not have their addresses
7290 modified. If there's any architectural constrain that
7291 applies to this address, then it should have already been
7292 taken into account when the breakpoint was created in the
7293 first place. If we didn't do this, stepping through e.g.,
7294 Thumb-2 IT blocks would break. */
7295 return bpaddr;
7296 }
7297 else
7298 {
7299 CORE_ADDR adjusted_bpaddr;
7300
7301 /* Some targets have architectural constraints on the placement
7302 of breakpoint instructions. Obtain the adjusted address. */
7303 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
7304
7305 /* An adjusted breakpoint address can significantly alter
7306 a user's expectations. Print a warning if an adjustment
7307 is required. */
7308 if (adjusted_bpaddr != bpaddr)
7309 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7310
7311 return adjusted_bpaddr;
7312 }
7313 }
7314
7315 void
7316 init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
7317 struct breakpoint *owner)
7318 {
7319 memset (loc, 0, sizeof (*loc));
7320
7321 gdb_assert (ops != NULL);
7322
7323 loc->ops = ops;
7324 loc->owner = owner;
7325 loc->cond = NULL;
7326 loc->cond_bytecode = NULL;
7327 loc->shlib_disabled = 0;
7328 loc->enabled = 1;
7329
7330 switch (owner->type)
7331 {
7332 case bp_breakpoint:
7333 case bp_single_step:
7334 case bp_until:
7335 case bp_finish:
7336 case bp_longjmp:
7337 case bp_longjmp_resume:
7338 case bp_longjmp_call_dummy:
7339 case bp_exception:
7340 case bp_exception_resume:
7341 case bp_step_resume:
7342 case bp_hp_step_resume:
7343 case bp_watchpoint_scope:
7344 case bp_call_dummy:
7345 case bp_std_terminate:
7346 case bp_shlib_event:
7347 case bp_thread_event:
7348 case bp_overlay_event:
7349 case bp_jit_event:
7350 case bp_longjmp_master:
7351 case bp_std_terminate_master:
7352 case bp_exception_master:
7353 case bp_gnu_ifunc_resolver:
7354 case bp_gnu_ifunc_resolver_return:
7355 case bp_dprintf:
7356 loc->loc_type = bp_loc_software_breakpoint;
7357 mark_breakpoint_location_modified (loc);
7358 break;
7359 case bp_hardware_breakpoint:
7360 loc->loc_type = bp_loc_hardware_breakpoint;
7361 mark_breakpoint_location_modified (loc);
7362 break;
7363 case bp_hardware_watchpoint:
7364 case bp_read_watchpoint:
7365 case bp_access_watchpoint:
7366 loc->loc_type = bp_loc_hardware_watchpoint;
7367 break;
7368 case bp_watchpoint:
7369 case bp_catchpoint:
7370 case bp_tracepoint:
7371 case bp_fast_tracepoint:
7372 case bp_static_tracepoint:
7373 loc->loc_type = bp_loc_other;
7374 break;
7375 default:
7376 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
7377 }
7378
7379 loc->refc = 1;
7380 }
7381
7382 /* Allocate a struct bp_location. */
7383
7384 static struct bp_location *
7385 allocate_bp_location (struct breakpoint *bpt)
7386 {
7387 return bpt->ops->allocate_location (bpt);
7388 }
7389
7390 static void
7391 free_bp_location (struct bp_location *loc)
7392 {
7393 loc->ops->dtor (loc);
7394 xfree (loc);
7395 }
7396
7397 /* Increment reference count. */
7398
7399 static void
7400 incref_bp_location (struct bp_location *bl)
7401 {
7402 ++bl->refc;
7403 }
7404
7405 /* Decrement reference count. If the reference count reaches 0,
7406 destroy the bp_location. Sets *BLP to NULL. */
7407
7408 static void
7409 decref_bp_location (struct bp_location **blp)
7410 {
7411 gdb_assert ((*blp)->refc > 0);
7412
7413 if (--(*blp)->refc == 0)
7414 free_bp_location (*blp);
7415 *blp = NULL;
7416 }
7417
7418 /* Add breakpoint B at the end of the global breakpoint chain. */
7419
7420 static void
7421 add_to_breakpoint_chain (struct breakpoint *b)
7422 {
7423 struct breakpoint *b1;
7424
7425 /* Add this breakpoint to the end of the chain so that a list of
7426 breakpoints will come out in order of increasing numbers. */
7427
7428 b1 = breakpoint_chain;
7429 if (b1 == 0)
7430 breakpoint_chain = b;
7431 else
7432 {
7433 while (b1->next)
7434 b1 = b1->next;
7435 b1->next = b;
7436 }
7437 }
7438
7439 /* Initializes breakpoint B with type BPTYPE and no locations yet. */
7440
7441 static void
7442 init_raw_breakpoint_without_location (struct breakpoint *b,
7443 struct gdbarch *gdbarch,
7444 enum bptype bptype,
7445 const struct breakpoint_ops *ops)
7446 {
7447 memset (b, 0, sizeof (*b));
7448
7449 gdb_assert (ops != NULL);
7450
7451 b->ops = ops;
7452 b->type = bptype;
7453 b->gdbarch = gdbarch;
7454 b->language = current_language->la_language;
7455 b->input_radix = input_radix;
7456 b->thread = -1;
7457 b->enable_state = bp_enabled;
7458 b->next = 0;
7459 b->silent = 0;
7460 b->ignore_count = 0;
7461 b->commands = NULL;
7462 b->frame_id = null_frame_id;
7463 b->condition_not_parsed = 0;
7464 b->py_bp_object = NULL;
7465 b->related_breakpoint = b;
7466 b->location = NULL;
7467 }
7468
7469 /* Helper to set_raw_breakpoint below. Creates a breakpoint
7470 that has type BPTYPE and has no locations as yet. */
7471
7472 static struct breakpoint *
7473 set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
7474 enum bptype bptype,
7475 const struct breakpoint_ops *ops)
7476 {
7477 struct breakpoint *b = XNEW (struct breakpoint);
7478
7479 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7480 add_to_breakpoint_chain (b);
7481 return b;
7482 }
7483
7484 /* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7485 resolutions should be made as the user specified the location explicitly
7486 enough. */
7487
7488 static void
7489 set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
7490 {
7491 gdb_assert (loc->owner != NULL);
7492
7493 if (loc->owner->type == bp_breakpoint
7494 || loc->owner->type == bp_hardware_breakpoint
7495 || is_tracepoint (loc->owner))
7496 {
7497 int is_gnu_ifunc;
7498 const char *function_name;
7499 CORE_ADDR func_addr;
7500
7501 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
7502 &func_addr, NULL, &is_gnu_ifunc);
7503
7504 if (is_gnu_ifunc && !explicit_loc)
7505 {
7506 struct breakpoint *b = loc->owner;
7507
7508 gdb_assert (loc->pspace == current_program_space);
7509 if (gnu_ifunc_resolve_name (function_name,
7510 &loc->requested_address))
7511 {
7512 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7513 loc->address = adjust_breakpoint_address (loc->gdbarch,
7514 loc->requested_address,
7515 b->type);
7516 }
7517 else if (b->type == bp_breakpoint && b->loc == loc
7518 && loc->next == NULL && b->related_breakpoint == b)
7519 {
7520 /* Create only the whole new breakpoint of this type but do not
7521 mess more complicated breakpoints with multiple locations. */
7522 b->type = bp_gnu_ifunc_resolver;
7523 /* Remember the resolver's address for use by the return
7524 breakpoint. */
7525 loc->related_address = func_addr;
7526 }
7527 }
7528
7529 if (function_name)
7530 loc->function_name = xstrdup (function_name);
7531 }
7532 }
7533
7534 /* Attempt to determine architecture of location identified by SAL. */
7535 struct gdbarch *
7536 get_sal_arch (struct symtab_and_line sal)
7537 {
7538 if (sal.section)
7539 return get_objfile_arch (sal.section->objfile);
7540 if (sal.symtab)
7541 return get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
7542
7543 return NULL;
7544 }
7545
7546 /* Low level routine for partially initializing a breakpoint of type
7547 BPTYPE. The newly created breakpoint's address, section, source
7548 file name, and line number are provided by SAL.
7549
7550 It is expected that the caller will complete the initialization of
7551 the newly created breakpoint struct as well as output any status
7552 information regarding the creation of a new breakpoint. */
7553
7554 static void
7555 init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
7556 struct symtab_and_line sal, enum bptype bptype,
7557 const struct breakpoint_ops *ops)
7558 {
7559 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7560
7561 add_location_to_breakpoint (b, &sal);
7562
7563 if (bptype != bp_catchpoint)
7564 gdb_assert (sal.pspace != NULL);
7565
7566 /* Store the program space that was used to set the breakpoint,
7567 except for ordinary breakpoints, which are independent of the
7568 program space. */
7569 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7570 b->pspace = sal.pspace;
7571 }
7572
7573 /* set_raw_breakpoint is a low level routine for allocating and
7574 partially initializing a breakpoint of type BPTYPE. The newly
7575 created breakpoint's address, section, source file name, and line
7576 number are provided by SAL. The newly created and partially
7577 initialized breakpoint is added to the breakpoint chain and
7578 is also returned as the value of this function.
7579
7580 It is expected that the caller will complete the initialization of
7581 the newly created breakpoint struct as well as output any status
7582 information regarding the creation of a new breakpoint. In
7583 particular, set_raw_breakpoint does NOT set the breakpoint
7584 number! Care should be taken to not allow an error to occur
7585 prior to completing the initialization of the breakpoint. If this
7586 should happen, a bogus breakpoint will be left on the chain. */
7587
7588 struct breakpoint *
7589 set_raw_breakpoint (struct gdbarch *gdbarch,
7590 struct symtab_and_line sal, enum bptype bptype,
7591 const struct breakpoint_ops *ops)
7592 {
7593 struct breakpoint *b = XNEW (struct breakpoint);
7594
7595 init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
7596 add_to_breakpoint_chain (b);
7597 return b;
7598 }
7599
7600 /* Call this routine when stepping and nexting to enable a breakpoint
7601 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7602 initiated the operation. */
7603
7604 void
7605 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
7606 {
7607 struct breakpoint *b, *b_tmp;
7608 int thread = tp->global_num;
7609
7610 /* To avoid having to rescan all objfile symbols at every step,
7611 we maintain a list of continually-inserted but always disabled
7612 longjmp "master" breakpoints. Here, we simply create momentary
7613 clones of those and enable them for the requested thread. */
7614 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7615 if (b->pspace == current_program_space
7616 && (b->type == bp_longjmp_master
7617 || b->type == bp_exception_master))
7618 {
7619 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7620 struct breakpoint *clone;
7621
7622 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7623 after their removal. */
7624 clone = momentary_breakpoint_from_master (b, type,
7625 &longjmp_breakpoint_ops, 1);
7626 clone->thread = thread;
7627 }
7628
7629 tp->initiating_frame = frame;
7630 }
7631
7632 /* Delete all longjmp breakpoints from THREAD. */
7633 void
7634 delete_longjmp_breakpoint (int thread)
7635 {
7636 struct breakpoint *b, *b_tmp;
7637
7638 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7639 if (b->type == bp_longjmp || b->type == bp_exception)
7640 {
7641 if (b->thread == thread)
7642 delete_breakpoint (b);
7643 }
7644 }
7645
7646 void
7647 delete_longjmp_breakpoint_at_next_stop (int thread)
7648 {
7649 struct breakpoint *b, *b_tmp;
7650
7651 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7652 if (b->type == bp_longjmp || b->type == bp_exception)
7653 {
7654 if (b->thread == thread)
7655 b->disposition = disp_del_at_next_stop;
7656 }
7657 }
7658
7659 /* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7660 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7661 pointer to any of them. Return NULL if this system cannot place longjmp
7662 breakpoints. */
7663
7664 struct breakpoint *
7665 set_longjmp_breakpoint_for_call_dummy (void)
7666 {
7667 struct breakpoint *b, *retval = NULL;
7668
7669 ALL_BREAKPOINTS (b)
7670 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7671 {
7672 struct breakpoint *new_b;
7673
7674 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7675 &momentary_breakpoint_ops,
7676 1);
7677 new_b->thread = ptid_to_global_thread_id (inferior_ptid);
7678
7679 /* Link NEW_B into the chain of RETVAL breakpoints. */
7680
7681 gdb_assert (new_b->related_breakpoint == new_b);
7682 if (retval == NULL)
7683 retval = new_b;
7684 new_b->related_breakpoint = retval;
7685 while (retval->related_breakpoint != new_b->related_breakpoint)
7686 retval = retval->related_breakpoint;
7687 retval->related_breakpoint = new_b;
7688 }
7689
7690 return retval;
7691 }
7692
7693 /* Verify all existing dummy frames and their associated breakpoints for
7694 TP. Remove those which can no longer be found in the current frame
7695 stack.
7696
7697 You should call this function only at places where it is safe to currently
7698 unwind the whole stack. Failed stack unwind would discard live dummy
7699 frames. */
7700
7701 void
7702 check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
7703 {
7704 struct breakpoint *b, *b_tmp;
7705
7706 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7707 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
7708 {
7709 struct breakpoint *dummy_b = b->related_breakpoint;
7710
7711 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7712 dummy_b = dummy_b->related_breakpoint;
7713 if (dummy_b->type != bp_call_dummy
7714 || frame_find_by_id (dummy_b->frame_id) != NULL)
7715 continue;
7716
7717 dummy_frame_discard (dummy_b->frame_id, tp->ptid);
7718
7719 while (b->related_breakpoint != b)
7720 {
7721 if (b_tmp == b->related_breakpoint)
7722 b_tmp = b->related_breakpoint->next;
7723 delete_breakpoint (b->related_breakpoint);
7724 }
7725 delete_breakpoint (b);
7726 }
7727 }
7728
7729 void
7730 enable_overlay_breakpoints (void)
7731 {
7732 struct breakpoint *b;
7733
7734 ALL_BREAKPOINTS (b)
7735 if (b->type == bp_overlay_event)
7736 {
7737 b->enable_state = bp_enabled;
7738 update_global_location_list (UGLL_MAY_INSERT);
7739 overlay_events_enabled = 1;
7740 }
7741 }
7742
7743 void
7744 disable_overlay_breakpoints (void)
7745 {
7746 struct breakpoint *b;
7747
7748 ALL_BREAKPOINTS (b)
7749 if (b->type == bp_overlay_event)
7750 {
7751 b->enable_state = bp_disabled;
7752 update_global_location_list (UGLL_DONT_INSERT);
7753 overlay_events_enabled = 0;
7754 }
7755 }
7756
7757 /* Set an active std::terminate breakpoint for each std::terminate
7758 master breakpoint. */
7759 void
7760 set_std_terminate_breakpoint (void)
7761 {
7762 struct breakpoint *b, *b_tmp;
7763
7764 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7765 if (b->pspace == current_program_space
7766 && b->type == bp_std_terminate_master)
7767 {
7768 momentary_breakpoint_from_master (b, bp_std_terminate,
7769 &momentary_breakpoint_ops, 1);
7770 }
7771 }
7772
7773 /* Delete all the std::terminate breakpoints. */
7774 void
7775 delete_std_terminate_breakpoint (void)
7776 {
7777 struct breakpoint *b, *b_tmp;
7778
7779 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7780 if (b->type == bp_std_terminate)
7781 delete_breakpoint (b);
7782 }
7783
7784 struct breakpoint *
7785 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7786 {
7787 struct breakpoint *b;
7788
7789 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7790 &internal_breakpoint_ops);
7791
7792 b->enable_state = bp_enabled;
7793 /* location has to be used or breakpoint_re_set will delete me. */
7794 b->location = new_address_location (b->loc->address, NULL, 0);
7795
7796 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7797
7798 return b;
7799 }
7800
7801 struct lang_and_radix
7802 {
7803 enum language lang;
7804 int radix;
7805 };
7806
7807 /* Create a breakpoint for JIT code registration and unregistration. */
7808
7809 struct breakpoint *
7810 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7811 {
7812 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7813 &internal_breakpoint_ops);
7814 }
7815
7816 /* Remove JIT code registration and unregistration breakpoint(s). */
7817
7818 void
7819 remove_jit_event_breakpoints (void)
7820 {
7821 struct breakpoint *b, *b_tmp;
7822
7823 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7824 if (b->type == bp_jit_event
7825 && b->loc->pspace == current_program_space)
7826 delete_breakpoint (b);
7827 }
7828
7829 void
7830 remove_solib_event_breakpoints (void)
7831 {
7832 struct breakpoint *b, *b_tmp;
7833
7834 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7835 if (b->type == bp_shlib_event
7836 && b->loc->pspace == current_program_space)
7837 delete_breakpoint (b);
7838 }
7839
7840 /* See breakpoint.h. */
7841
7842 void
7843 remove_solib_event_breakpoints_at_next_stop (void)
7844 {
7845 struct breakpoint *b, *b_tmp;
7846
7847 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7848 if (b->type == bp_shlib_event
7849 && b->loc->pspace == current_program_space)
7850 b->disposition = disp_del_at_next_stop;
7851 }
7852
7853 /* Helper for create_solib_event_breakpoint /
7854 create_and_insert_solib_event_breakpoint. Allows specifying which
7855 INSERT_MODE to pass through to update_global_location_list. */
7856
7857 static struct breakpoint *
7858 create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7859 enum ugll_insert_mode insert_mode)
7860 {
7861 struct breakpoint *b;
7862
7863 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7864 &internal_breakpoint_ops);
7865 update_global_location_list_nothrow (insert_mode);
7866 return b;
7867 }
7868
7869 struct breakpoint *
7870 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7871 {
7872 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7873 }
7874
7875 /* See breakpoint.h. */
7876
7877 struct breakpoint *
7878 create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7879 {
7880 struct breakpoint *b;
7881
7882 /* Explicitly tell update_global_location_list to insert
7883 locations. */
7884 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
7885 if (!b->loc->inserted)
7886 {
7887 delete_breakpoint (b);
7888 return NULL;
7889 }
7890 return b;
7891 }
7892
7893 /* Disable any breakpoints that are on code in shared libraries. Only
7894 apply to enabled breakpoints, disabled ones can just stay disabled. */
7895
7896 void
7897 disable_breakpoints_in_shlibs (void)
7898 {
7899 struct bp_location *loc, **locp_tmp;
7900
7901 ALL_BP_LOCATIONS (loc, locp_tmp)
7902 {
7903 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7904 struct breakpoint *b = loc->owner;
7905
7906 /* We apply the check to all breakpoints, including disabled for
7907 those with loc->duplicate set. This is so that when breakpoint
7908 becomes enabled, or the duplicate is removed, gdb will try to
7909 insert all breakpoints. If we don't set shlib_disabled here,
7910 we'll try to insert those breakpoints and fail. */
7911 if (((b->type == bp_breakpoint)
7912 || (b->type == bp_jit_event)
7913 || (b->type == bp_hardware_breakpoint)
7914 || (is_tracepoint (b)))
7915 && loc->pspace == current_program_space
7916 && !loc->shlib_disabled
7917 && solib_name_from_address (loc->pspace, loc->address)
7918 )
7919 {
7920 loc->shlib_disabled = 1;
7921 }
7922 }
7923 }
7924
7925 /* Disable any breakpoints and tracepoints that are in SOLIB upon
7926 notification of unloaded_shlib. Only apply to enabled breakpoints,
7927 disabled ones can just stay disabled. */
7928
7929 static void
7930 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7931 {
7932 struct bp_location *loc, **locp_tmp;
7933 int disabled_shlib_breaks = 0;
7934
7935 /* SunOS a.out shared libraries are always mapped, so do not
7936 disable breakpoints; they will only be reported as unloaded
7937 through clear_solib when GDB discards its shared library
7938 list. See clear_solib for more information. */
7939 if (exec_bfd != NULL
7940 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
7941 return;
7942
7943 ALL_BP_LOCATIONS (loc, locp_tmp)
7944 {
7945 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7946 struct breakpoint *b = loc->owner;
7947
7948 if (solib->pspace == loc->pspace
7949 && !loc->shlib_disabled
7950 && (((b->type == bp_breakpoint
7951 || b->type == bp_jit_event
7952 || b->type == bp_hardware_breakpoint)
7953 && (loc->loc_type == bp_loc_hardware_breakpoint
7954 || loc->loc_type == bp_loc_software_breakpoint))
7955 || is_tracepoint (b))
7956 && solib_contains_address_p (solib, loc->address))
7957 {
7958 loc->shlib_disabled = 1;
7959 /* At this point, we cannot rely on remove_breakpoint
7960 succeeding so we must mark the breakpoint as not inserted
7961 to prevent future errors occurring in remove_breakpoints. */
7962 loc->inserted = 0;
7963
7964 /* This may cause duplicate notifications for the same breakpoint. */
7965 observer_notify_breakpoint_modified (b);
7966
7967 if (!disabled_shlib_breaks)
7968 {
7969 target_terminal_ours_for_output ();
7970 warning (_("Temporarily disabling breakpoints "
7971 "for unloaded shared library \"%s\""),
7972 solib->so_name);
7973 }
7974 disabled_shlib_breaks = 1;
7975 }
7976 }
7977 }
7978
7979 /* Disable any breakpoints and tracepoints in OBJFILE upon
7980 notification of free_objfile. Only apply to enabled breakpoints,
7981 disabled ones can just stay disabled. */
7982
7983 static void
7984 disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7985 {
7986 struct breakpoint *b;
7987
7988 if (objfile == NULL)
7989 return;
7990
7991 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7992 managed by the user with add-symbol-file/remove-symbol-file.
7993 Similarly to how breakpoints in shared libraries are handled in
7994 response to "nosharedlibrary", mark breakpoints in such modules
7995 shlib_disabled so they end up uninserted on the next global
7996 location list update. Shared libraries not loaded by the user
7997 aren't handled here -- they're already handled in
7998 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7999 solib_unloaded observer. We skip objfiles that are not
8000 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
8001 main objfile). */
8002 if ((objfile->flags & OBJF_SHARED) == 0
8003 || (objfile->flags & OBJF_USERLOADED) == 0)
8004 return;
8005
8006 ALL_BREAKPOINTS (b)
8007 {
8008 struct bp_location *loc;
8009 int bp_modified = 0;
8010
8011 if (!is_breakpoint (b) && !is_tracepoint (b))
8012 continue;
8013
8014 for (loc = b->loc; loc != NULL; loc = loc->next)
8015 {
8016 CORE_ADDR loc_addr = loc->address;
8017
8018 if (loc->loc_type != bp_loc_hardware_breakpoint
8019 && loc->loc_type != bp_loc_software_breakpoint)
8020 continue;
8021
8022 if (loc->shlib_disabled != 0)
8023 continue;
8024
8025 if (objfile->pspace != loc->pspace)
8026 continue;
8027
8028 if (loc->loc_type != bp_loc_hardware_breakpoint
8029 && loc->loc_type != bp_loc_software_breakpoint)
8030 continue;
8031
8032 if (is_addr_in_objfile (loc_addr, objfile))
8033 {
8034 loc->shlib_disabled = 1;
8035 /* At this point, we don't know whether the object was
8036 unmapped from the inferior or not, so leave the
8037 inserted flag alone. We'll handle failure to
8038 uninsert quietly, in case the object was indeed
8039 unmapped. */
8040
8041 mark_breakpoint_location_modified (loc);
8042
8043 bp_modified = 1;
8044 }
8045 }
8046
8047 if (bp_modified)
8048 observer_notify_breakpoint_modified (b);
8049 }
8050 }
8051
8052 /* FORK & VFORK catchpoints. */
8053
8054 /* An instance of this type is used to represent a fork or vfork
8055 catchpoint. It includes a "struct breakpoint" as a kind of base
8056 class; users downcast to "struct breakpoint *" when needed. A
8057 breakpoint is really of this type iff its ops pointer points to
8058 CATCH_FORK_BREAKPOINT_OPS. */
8059
8060 struct fork_catchpoint
8061 {
8062 /* The base class. */
8063 struct breakpoint base;
8064
8065 /* Process id of a child process whose forking triggered this
8066 catchpoint. This field is only valid immediately after this
8067 catchpoint has triggered. */
8068 ptid_t forked_inferior_pid;
8069 };
8070
8071 /* Implement the "insert" breakpoint_ops method for fork
8072 catchpoints. */
8073
8074 static int
8075 insert_catch_fork (struct bp_location *bl)
8076 {
8077 return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
8078 }
8079
8080 /* Implement the "remove" breakpoint_ops method for fork
8081 catchpoints. */
8082
8083 static int
8084 remove_catch_fork (struct bp_location *bl)
8085 {
8086 return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
8087 }
8088
8089 /* Implement the "breakpoint_hit" breakpoint_ops method for fork
8090 catchpoints. */
8091
8092 static int
8093 breakpoint_hit_catch_fork (const struct bp_location *bl,
8094 struct address_space *aspace, CORE_ADDR bp_addr,
8095 const struct target_waitstatus *ws)
8096 {
8097 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8098
8099 if (ws->kind != TARGET_WAITKIND_FORKED)
8100 return 0;
8101
8102 c->forked_inferior_pid = ws->value.related_pid;
8103 return 1;
8104 }
8105
8106 /* Implement the "print_it" breakpoint_ops method for fork
8107 catchpoints. */
8108
8109 static enum print_stop_action
8110 print_it_catch_fork (bpstat bs)
8111 {
8112 struct ui_out *uiout = current_uiout;
8113 struct breakpoint *b = bs->breakpoint_at;
8114 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
8115
8116 annotate_catchpoint (b->number);
8117 maybe_print_thread_hit_breakpoint (uiout);
8118 if (b->disposition == disp_del)
8119 ui_out_text (uiout, "Temporary catchpoint ");
8120 else
8121 ui_out_text (uiout, "Catchpoint ");
8122 if (ui_out_is_mi_like_p (uiout))
8123 {
8124 ui_out_field_string (uiout, "reason",
8125 async_reason_lookup (EXEC_ASYNC_FORK));
8126 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8127 }
8128 ui_out_field_int (uiout, "bkptno", b->number);
8129 ui_out_text (uiout, " (forked process ");
8130 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
8131 ui_out_text (uiout, "), ");
8132 return PRINT_SRC_AND_LOC;
8133 }
8134
8135 /* Implement the "print_one" breakpoint_ops method for fork
8136 catchpoints. */
8137
8138 static void
8139 print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
8140 {
8141 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8142 struct value_print_options opts;
8143 struct ui_out *uiout = current_uiout;
8144
8145 get_user_print_options (&opts);
8146
8147 /* Field 4, the address, is omitted (which makes the columns not
8148 line up too nicely with the headers, but the effect is relatively
8149 readable). */
8150 if (opts.addressprint)
8151 ui_out_field_skip (uiout, "addr");
8152 annotate_field (5);
8153 ui_out_text (uiout, "fork");
8154 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
8155 {
8156 ui_out_text (uiout, ", process ");
8157 ui_out_field_int (uiout, "what",
8158 ptid_get_pid (c->forked_inferior_pid));
8159 ui_out_spaces (uiout, 1);
8160 }
8161
8162 if (ui_out_is_mi_like_p (uiout))
8163 ui_out_field_string (uiout, "catch-type", "fork");
8164 }
8165
8166 /* Implement the "print_mention" breakpoint_ops method for fork
8167 catchpoints. */
8168
8169 static void
8170 print_mention_catch_fork (struct breakpoint *b)
8171 {
8172 printf_filtered (_("Catchpoint %d (fork)"), b->number);
8173 }
8174
8175 /* Implement the "print_recreate" breakpoint_ops method for fork
8176 catchpoints. */
8177
8178 static void
8179 print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
8180 {
8181 fprintf_unfiltered (fp, "catch fork");
8182 print_recreate_thread (b, fp);
8183 }
8184
8185 /* The breakpoint_ops structure to be used in fork catchpoints. */
8186
8187 static struct breakpoint_ops catch_fork_breakpoint_ops;
8188
8189 /* Implement the "insert" breakpoint_ops method for vfork
8190 catchpoints. */
8191
8192 static int
8193 insert_catch_vfork (struct bp_location *bl)
8194 {
8195 return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
8196 }
8197
8198 /* Implement the "remove" breakpoint_ops method for vfork
8199 catchpoints. */
8200
8201 static int
8202 remove_catch_vfork (struct bp_location *bl)
8203 {
8204 return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
8205 }
8206
8207 /* Implement the "breakpoint_hit" breakpoint_ops method for vfork
8208 catchpoints. */
8209
8210 static int
8211 breakpoint_hit_catch_vfork (const struct bp_location *bl,
8212 struct address_space *aspace, CORE_ADDR bp_addr,
8213 const struct target_waitstatus *ws)
8214 {
8215 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8216
8217 if (ws->kind != TARGET_WAITKIND_VFORKED)
8218 return 0;
8219
8220 c->forked_inferior_pid = ws->value.related_pid;
8221 return 1;
8222 }
8223
8224 /* Implement the "print_it" breakpoint_ops method for vfork
8225 catchpoints. */
8226
8227 static enum print_stop_action
8228 print_it_catch_vfork (bpstat bs)
8229 {
8230 struct ui_out *uiout = current_uiout;
8231 struct breakpoint *b = bs->breakpoint_at;
8232 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8233
8234 annotate_catchpoint (b->number);
8235 maybe_print_thread_hit_breakpoint (uiout);
8236 if (b->disposition == disp_del)
8237 ui_out_text (uiout, "Temporary catchpoint ");
8238 else
8239 ui_out_text (uiout, "Catchpoint ");
8240 if (ui_out_is_mi_like_p (uiout))
8241 {
8242 ui_out_field_string (uiout, "reason",
8243 async_reason_lookup (EXEC_ASYNC_VFORK));
8244 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8245 }
8246 ui_out_field_int (uiout, "bkptno", b->number);
8247 ui_out_text (uiout, " (vforked process ");
8248 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
8249 ui_out_text (uiout, "), ");
8250 return PRINT_SRC_AND_LOC;
8251 }
8252
8253 /* Implement the "print_one" breakpoint_ops method for vfork
8254 catchpoints. */
8255
8256 static void
8257 print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
8258 {
8259 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8260 struct value_print_options opts;
8261 struct ui_out *uiout = current_uiout;
8262
8263 get_user_print_options (&opts);
8264 /* Field 4, the address, is omitted (which makes the columns not
8265 line up too nicely with the headers, but the effect is relatively
8266 readable). */
8267 if (opts.addressprint)
8268 ui_out_field_skip (uiout, "addr");
8269 annotate_field (5);
8270 ui_out_text (uiout, "vfork");
8271 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
8272 {
8273 ui_out_text (uiout, ", process ");
8274 ui_out_field_int (uiout, "what",
8275 ptid_get_pid (c->forked_inferior_pid));
8276 ui_out_spaces (uiout, 1);
8277 }
8278
8279 if (ui_out_is_mi_like_p (uiout))
8280 ui_out_field_string (uiout, "catch-type", "vfork");
8281 }
8282
8283 /* Implement the "print_mention" breakpoint_ops method for vfork
8284 catchpoints. */
8285
8286 static void
8287 print_mention_catch_vfork (struct breakpoint *b)
8288 {
8289 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
8290 }
8291
8292 /* Implement the "print_recreate" breakpoint_ops method for vfork
8293 catchpoints. */
8294
8295 static void
8296 print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8297 {
8298 fprintf_unfiltered (fp, "catch vfork");
8299 print_recreate_thread (b, fp);
8300 }
8301
8302 /* The breakpoint_ops structure to be used in vfork catchpoints. */
8303
8304 static struct breakpoint_ops catch_vfork_breakpoint_ops;
8305
8306 /* An instance of this type is used to represent an solib catchpoint.
8307 It includes a "struct breakpoint" as a kind of base class; users
8308 downcast to "struct breakpoint *" when needed. A breakpoint is
8309 really of this type iff its ops pointer points to
8310 CATCH_SOLIB_BREAKPOINT_OPS. */
8311
8312 struct solib_catchpoint
8313 {
8314 /* The base class. */
8315 struct breakpoint base;
8316
8317 /* True for "catch load", false for "catch unload". */
8318 unsigned char is_load;
8319
8320 /* Regular expression to match, if any. COMPILED is only valid when
8321 REGEX is non-NULL. */
8322 char *regex;
8323 regex_t compiled;
8324 };
8325
8326 static void
8327 dtor_catch_solib (struct breakpoint *b)
8328 {
8329 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8330
8331 if (self->regex)
8332 regfree (&self->compiled);
8333 xfree (self->regex);
8334
8335 base_breakpoint_ops.dtor (b);
8336 }
8337
8338 static int
8339 insert_catch_solib (struct bp_location *ignore)
8340 {
8341 return 0;
8342 }
8343
8344 static int
8345 remove_catch_solib (struct bp_location *ignore)
8346 {
8347 return 0;
8348 }
8349
8350 static int
8351 breakpoint_hit_catch_solib (const struct bp_location *bl,
8352 struct address_space *aspace,
8353 CORE_ADDR bp_addr,
8354 const struct target_waitstatus *ws)
8355 {
8356 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8357 struct breakpoint *other;
8358
8359 if (ws->kind == TARGET_WAITKIND_LOADED)
8360 return 1;
8361
8362 ALL_BREAKPOINTS (other)
8363 {
8364 struct bp_location *other_bl;
8365
8366 if (other == bl->owner)
8367 continue;
8368
8369 if (other->type != bp_shlib_event)
8370 continue;
8371
8372 if (self->base.pspace != NULL && other->pspace != self->base.pspace)
8373 continue;
8374
8375 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8376 {
8377 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8378 return 1;
8379 }
8380 }
8381
8382 return 0;
8383 }
8384
8385 static void
8386 check_status_catch_solib (struct bpstats *bs)
8387 {
8388 struct solib_catchpoint *self
8389 = (struct solib_catchpoint *) bs->breakpoint_at;
8390 int ix;
8391
8392 if (self->is_load)
8393 {
8394 struct so_list *iter;
8395
8396 for (ix = 0;
8397 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
8398 ix, iter);
8399 ++ix)
8400 {
8401 if (!self->regex
8402 || regexec (&self->compiled, iter->so_name, 0, NULL, 0) == 0)
8403 return;
8404 }
8405 }
8406 else
8407 {
8408 char *iter;
8409
8410 for (ix = 0;
8411 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
8412 ix, iter);
8413 ++ix)
8414 {
8415 if (!self->regex
8416 || regexec (&self->compiled, iter, 0, NULL, 0) == 0)
8417 return;
8418 }
8419 }
8420
8421 bs->stop = 0;
8422 bs->print_it = print_it_noop;
8423 }
8424
8425 static enum print_stop_action
8426 print_it_catch_solib (bpstat bs)
8427 {
8428 struct breakpoint *b = bs->breakpoint_at;
8429 struct ui_out *uiout = current_uiout;
8430
8431 annotate_catchpoint (b->number);
8432 maybe_print_thread_hit_breakpoint (uiout);
8433 if (b->disposition == disp_del)
8434 ui_out_text (uiout, "Temporary catchpoint ");
8435 else
8436 ui_out_text (uiout, "Catchpoint ");
8437 ui_out_field_int (uiout, "bkptno", b->number);
8438 ui_out_text (uiout, "\n");
8439 if (ui_out_is_mi_like_p (uiout))
8440 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8441 print_solib_event (1);
8442 return PRINT_SRC_AND_LOC;
8443 }
8444
8445 static void
8446 print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8447 {
8448 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8449 struct value_print_options opts;
8450 struct ui_out *uiout = current_uiout;
8451 char *msg;
8452
8453 get_user_print_options (&opts);
8454 /* Field 4, the address, is omitted (which makes the columns not
8455 line up too nicely with the headers, but the effect is relatively
8456 readable). */
8457 if (opts.addressprint)
8458 {
8459 annotate_field (4);
8460 ui_out_field_skip (uiout, "addr");
8461 }
8462
8463 annotate_field (5);
8464 if (self->is_load)
8465 {
8466 if (self->regex)
8467 msg = xstrprintf (_("load of library matching %s"), self->regex);
8468 else
8469 msg = xstrdup (_("load of library"));
8470 }
8471 else
8472 {
8473 if (self->regex)
8474 msg = xstrprintf (_("unload of library matching %s"), self->regex);
8475 else
8476 msg = xstrdup (_("unload of library"));
8477 }
8478 ui_out_field_string (uiout, "what", msg);
8479 xfree (msg);
8480
8481 if (ui_out_is_mi_like_p (uiout))
8482 ui_out_field_string (uiout, "catch-type",
8483 self->is_load ? "load" : "unload");
8484 }
8485
8486 static void
8487 print_mention_catch_solib (struct breakpoint *b)
8488 {
8489 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8490
8491 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8492 self->is_load ? "load" : "unload");
8493 }
8494
8495 static void
8496 print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8497 {
8498 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8499
8500 fprintf_unfiltered (fp, "%s %s",
8501 b->disposition == disp_del ? "tcatch" : "catch",
8502 self->is_load ? "load" : "unload");
8503 if (self->regex)
8504 fprintf_unfiltered (fp, " %s", self->regex);
8505 fprintf_unfiltered (fp, "\n");
8506 }
8507
8508 static struct breakpoint_ops catch_solib_breakpoint_ops;
8509
8510 /* Shared helper function (MI and CLI) for creating and installing
8511 a shared object event catchpoint. If IS_LOAD is non-zero then
8512 the events to be caught are load events, otherwise they are
8513 unload events. If IS_TEMP is non-zero the catchpoint is a
8514 temporary one. If ENABLED is non-zero the catchpoint is
8515 created in an enabled state. */
8516
8517 void
8518 add_solib_catchpoint (char *arg, int is_load, int is_temp, int enabled)
8519 {
8520 struct solib_catchpoint *c;
8521 struct gdbarch *gdbarch = get_current_arch ();
8522 struct cleanup *cleanup;
8523
8524 if (!arg)
8525 arg = "";
8526 arg = skip_spaces (arg);
8527
8528 c = XCNEW (struct solib_catchpoint);
8529 cleanup = make_cleanup (xfree, c);
8530
8531 if (*arg != '\0')
8532 {
8533 int errcode;
8534
8535 errcode = regcomp (&c->compiled, arg, REG_NOSUB);
8536 if (errcode != 0)
8537 {
8538 char *err = get_regcomp_error (errcode, &c->compiled);
8539
8540 make_cleanup (xfree, err);
8541 error (_("Invalid regexp (%s): %s"), err, arg);
8542 }
8543 c->regex = xstrdup (arg);
8544 }
8545
8546 c->is_load = is_load;
8547 init_catchpoint (&c->base, gdbarch, is_temp, NULL,
8548 &catch_solib_breakpoint_ops);
8549
8550 c->base.enable_state = enabled ? bp_enabled : bp_disabled;
8551
8552 discard_cleanups (cleanup);
8553 install_breakpoint (0, &c->base, 1);
8554 }
8555
8556 /* A helper function that does all the work for "catch load" and
8557 "catch unload". */
8558
8559 static void
8560 catch_load_or_unload (char *arg, int from_tty, int is_load,
8561 struct cmd_list_element *command)
8562 {
8563 int tempflag;
8564 const int enabled = 1;
8565
8566 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8567
8568 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8569 }
8570
8571 static void
8572 catch_load_command_1 (char *arg, int from_tty,
8573 struct cmd_list_element *command)
8574 {
8575 catch_load_or_unload (arg, from_tty, 1, command);
8576 }
8577
8578 static void
8579 catch_unload_command_1 (char *arg, int from_tty,
8580 struct cmd_list_element *command)
8581 {
8582 catch_load_or_unload (arg, from_tty, 0, command);
8583 }
8584
8585 /* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8586 is non-zero, then make the breakpoint temporary. If COND_STRING is
8587 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8588 the breakpoint_ops structure associated to the catchpoint. */
8589
8590 void
8591 init_catchpoint (struct breakpoint *b,
8592 struct gdbarch *gdbarch, int tempflag,
8593 char *cond_string,
8594 const struct breakpoint_ops *ops)
8595 {
8596 struct symtab_and_line sal;
8597
8598 init_sal (&sal);
8599 sal.pspace = current_program_space;
8600
8601 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
8602
8603 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
8604 b->disposition = tempflag ? disp_del : disp_donttouch;
8605 }
8606
8607 void
8608 install_breakpoint (int internal, struct breakpoint *b, int update_gll)
8609 {
8610 add_to_breakpoint_chain (b);
8611 set_breakpoint_number (internal, b);
8612 if (is_tracepoint (b))
8613 set_tracepoint_count (breakpoint_count);
8614 if (!internal)
8615 mention (b);
8616 observer_notify_breakpoint_created (b);
8617
8618 if (update_gll)
8619 update_global_location_list (UGLL_MAY_INSERT);
8620 }
8621
8622 static void
8623 create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8624 int tempflag, char *cond_string,
8625 const struct breakpoint_ops *ops)
8626 {
8627 struct fork_catchpoint *c = XNEW (struct fork_catchpoint);
8628
8629 init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
8630
8631 c->forked_inferior_pid = null_ptid;
8632
8633 install_breakpoint (0, &c->base, 1);
8634 }
8635
8636 /* Exec catchpoints. */
8637
8638 /* An instance of this type is used to represent an exec catchpoint.
8639 It includes a "struct breakpoint" as a kind of base class; users
8640 downcast to "struct breakpoint *" when needed. A breakpoint is
8641 really of this type iff its ops pointer points to
8642 CATCH_EXEC_BREAKPOINT_OPS. */
8643
8644 struct exec_catchpoint
8645 {
8646 /* The base class. */
8647 struct breakpoint base;
8648
8649 /* Filename of a program whose exec triggered this catchpoint.
8650 This field is only valid immediately after this catchpoint has
8651 triggered. */
8652 char *exec_pathname;
8653 };
8654
8655 /* Implement the "dtor" breakpoint_ops method for exec
8656 catchpoints. */
8657
8658 static void
8659 dtor_catch_exec (struct breakpoint *b)
8660 {
8661 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8662
8663 xfree (c->exec_pathname);
8664
8665 base_breakpoint_ops.dtor (b);
8666 }
8667
8668 static int
8669 insert_catch_exec (struct bp_location *bl)
8670 {
8671 return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
8672 }
8673
8674 static int
8675 remove_catch_exec (struct bp_location *bl)
8676 {
8677 return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
8678 }
8679
8680 static int
8681 breakpoint_hit_catch_exec (const struct bp_location *bl,
8682 struct address_space *aspace, CORE_ADDR bp_addr,
8683 const struct target_waitstatus *ws)
8684 {
8685 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8686
8687 if (ws->kind != TARGET_WAITKIND_EXECD)
8688 return 0;
8689
8690 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8691 return 1;
8692 }
8693
8694 static enum print_stop_action
8695 print_it_catch_exec (bpstat bs)
8696 {
8697 struct ui_out *uiout = current_uiout;
8698 struct breakpoint *b = bs->breakpoint_at;
8699 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8700
8701 annotate_catchpoint (b->number);
8702 maybe_print_thread_hit_breakpoint (uiout);
8703 if (b->disposition == disp_del)
8704 ui_out_text (uiout, "Temporary catchpoint ");
8705 else
8706 ui_out_text (uiout, "Catchpoint ");
8707 if (ui_out_is_mi_like_p (uiout))
8708 {
8709 ui_out_field_string (uiout, "reason",
8710 async_reason_lookup (EXEC_ASYNC_EXEC));
8711 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8712 }
8713 ui_out_field_int (uiout, "bkptno", b->number);
8714 ui_out_text (uiout, " (exec'd ");
8715 ui_out_field_string (uiout, "new-exec", c->exec_pathname);
8716 ui_out_text (uiout, "), ");
8717
8718 return PRINT_SRC_AND_LOC;
8719 }
8720
8721 static void
8722 print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
8723 {
8724 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8725 struct value_print_options opts;
8726 struct ui_out *uiout = current_uiout;
8727
8728 get_user_print_options (&opts);
8729
8730 /* Field 4, the address, is omitted (which makes the columns
8731 not line up too nicely with the headers, but the effect
8732 is relatively readable). */
8733 if (opts.addressprint)
8734 ui_out_field_skip (uiout, "addr");
8735 annotate_field (5);
8736 ui_out_text (uiout, "exec");
8737 if (c->exec_pathname != NULL)
8738 {
8739 ui_out_text (uiout, ", program \"");
8740 ui_out_field_string (uiout, "what", c->exec_pathname);
8741 ui_out_text (uiout, "\" ");
8742 }
8743
8744 if (ui_out_is_mi_like_p (uiout))
8745 ui_out_field_string (uiout, "catch-type", "exec");
8746 }
8747
8748 static void
8749 print_mention_catch_exec (struct breakpoint *b)
8750 {
8751 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8752 }
8753
8754 /* Implement the "print_recreate" breakpoint_ops method for exec
8755 catchpoints. */
8756
8757 static void
8758 print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8759 {
8760 fprintf_unfiltered (fp, "catch exec");
8761 print_recreate_thread (b, fp);
8762 }
8763
8764 static struct breakpoint_ops catch_exec_breakpoint_ops;
8765
8766 static int
8767 hw_breakpoint_used_count (void)
8768 {
8769 int i = 0;
8770 struct breakpoint *b;
8771 struct bp_location *bl;
8772
8773 ALL_BREAKPOINTS (b)
8774 {
8775 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
8776 for (bl = b->loc; bl; bl = bl->next)
8777 {
8778 /* Special types of hardware breakpoints may use more than
8779 one register. */
8780 i += b->ops->resources_needed (bl);
8781 }
8782 }
8783
8784 return i;
8785 }
8786
8787 /* Returns the resources B would use if it were a hardware
8788 watchpoint. */
8789
8790 static int
8791 hw_watchpoint_use_count (struct breakpoint *b)
8792 {
8793 int i = 0;
8794 struct bp_location *bl;
8795
8796 if (!breakpoint_enabled (b))
8797 return 0;
8798
8799 for (bl = b->loc; bl; bl = bl->next)
8800 {
8801 /* Special types of hardware watchpoints may use more than
8802 one register. */
8803 i += b->ops->resources_needed (bl);
8804 }
8805
8806 return i;
8807 }
8808
8809 /* Returns the sum the used resources of all hardware watchpoints of
8810 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8811 the sum of the used resources of all hardware watchpoints of other
8812 types _not_ TYPE. */
8813
8814 static int
8815 hw_watchpoint_used_count_others (struct breakpoint *except,
8816 enum bptype type, int *other_type_used)
8817 {
8818 int i = 0;
8819 struct breakpoint *b;
8820
8821 *other_type_used = 0;
8822 ALL_BREAKPOINTS (b)
8823 {
8824 if (b == except)
8825 continue;
8826 if (!breakpoint_enabled (b))
8827 continue;
8828
8829 if (b->type == type)
8830 i += hw_watchpoint_use_count (b);
8831 else if (is_hardware_watchpoint (b))
8832 *other_type_used = 1;
8833 }
8834
8835 return i;
8836 }
8837
8838 void
8839 disable_watchpoints_before_interactive_call_start (void)
8840 {
8841 struct breakpoint *b;
8842
8843 ALL_BREAKPOINTS (b)
8844 {
8845 if (is_watchpoint (b) && breakpoint_enabled (b))
8846 {
8847 b->enable_state = bp_call_disabled;
8848 update_global_location_list (UGLL_DONT_INSERT);
8849 }
8850 }
8851 }
8852
8853 void
8854 enable_watchpoints_after_interactive_call_stop (void)
8855 {
8856 struct breakpoint *b;
8857
8858 ALL_BREAKPOINTS (b)
8859 {
8860 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
8861 {
8862 b->enable_state = bp_enabled;
8863 update_global_location_list (UGLL_MAY_INSERT);
8864 }
8865 }
8866 }
8867
8868 void
8869 disable_breakpoints_before_startup (void)
8870 {
8871 current_program_space->executing_startup = 1;
8872 update_global_location_list (UGLL_DONT_INSERT);
8873 }
8874
8875 void
8876 enable_breakpoints_after_startup (void)
8877 {
8878 current_program_space->executing_startup = 0;
8879 breakpoint_re_set ();
8880 }
8881
8882 /* Create a new single-step breakpoint for thread THREAD, with no
8883 locations. */
8884
8885 static struct breakpoint *
8886 new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8887 {
8888 struct breakpoint *b = XNEW (struct breakpoint);
8889
8890 init_raw_breakpoint_without_location (b, gdbarch, bp_single_step,
8891 &momentary_breakpoint_ops);
8892
8893 b->disposition = disp_donttouch;
8894 b->frame_id = null_frame_id;
8895
8896 b->thread = thread;
8897 gdb_assert (b->thread != 0);
8898
8899 add_to_breakpoint_chain (b);
8900
8901 return b;
8902 }
8903
8904 /* Set a momentary breakpoint of type TYPE at address specified by
8905 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8906 frame. */
8907
8908 struct breakpoint *
8909 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8910 struct frame_id frame_id, enum bptype type)
8911 {
8912 struct breakpoint *b;
8913
8914 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8915 tail-called one. */
8916 gdb_assert (!frame_id_artificial_p (frame_id));
8917
8918 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
8919 b->enable_state = bp_enabled;
8920 b->disposition = disp_donttouch;
8921 b->frame_id = frame_id;
8922
8923 /* If we're debugging a multi-threaded program, then we want
8924 momentary breakpoints to be active in only a single thread of
8925 control. */
8926 if (in_thread_list (inferior_ptid))
8927 b->thread = ptid_to_global_thread_id (inferior_ptid);
8928
8929 update_global_location_list_nothrow (UGLL_MAY_INSERT);
8930
8931 return b;
8932 }
8933
8934 /* Make a momentary breakpoint based on the master breakpoint ORIG.
8935 The new breakpoint will have type TYPE, use OPS as its
8936 breakpoint_ops, and will set enabled to LOC_ENABLED. */
8937
8938 static struct breakpoint *
8939 momentary_breakpoint_from_master (struct breakpoint *orig,
8940 enum bptype type,
8941 const struct breakpoint_ops *ops,
8942 int loc_enabled)
8943 {
8944 struct breakpoint *copy;
8945
8946 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
8947 copy->loc = allocate_bp_location (copy);
8948 set_breakpoint_location_function (copy->loc, 1);
8949
8950 copy->loc->gdbarch = orig->loc->gdbarch;
8951 copy->loc->requested_address = orig->loc->requested_address;
8952 copy->loc->address = orig->loc->address;
8953 copy->loc->section = orig->loc->section;
8954 copy->loc->pspace = orig->loc->pspace;
8955 copy->loc->probe = orig->loc->probe;
8956 copy->loc->line_number = orig->loc->line_number;
8957 copy->loc->symtab = orig->loc->symtab;
8958 copy->loc->enabled = loc_enabled;
8959 copy->frame_id = orig->frame_id;
8960 copy->thread = orig->thread;
8961 copy->pspace = orig->pspace;
8962
8963 copy->enable_state = bp_enabled;
8964 copy->disposition = disp_donttouch;
8965 copy->number = internal_breakpoint_number--;
8966
8967 update_global_location_list_nothrow (UGLL_DONT_INSERT);
8968 return copy;
8969 }
8970
8971 /* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8972 ORIG is NULL. */
8973
8974 struct breakpoint *
8975 clone_momentary_breakpoint (struct breakpoint *orig)
8976 {
8977 /* If there's nothing to clone, then return nothing. */
8978 if (orig == NULL)
8979 return NULL;
8980
8981 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
8982 }
8983
8984 struct breakpoint *
8985 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8986 enum bptype type)
8987 {
8988 struct symtab_and_line sal;
8989
8990 sal = find_pc_line (pc, 0);
8991 sal.pc = pc;
8992 sal.section = find_pc_overlay (pc);
8993 sal.explicit_pc = 1;
8994
8995 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
8996 }
8997 \f
8998
8999 /* Tell the user we have just set a breakpoint B. */
9000
9001 static void
9002 mention (struct breakpoint *b)
9003 {
9004 b->ops->print_mention (b);
9005 if (ui_out_is_mi_like_p (current_uiout))
9006 return;
9007 printf_filtered ("\n");
9008 }
9009 \f
9010
9011 static int bp_loc_is_permanent (struct bp_location *loc);
9012
9013 static struct bp_location *
9014 add_location_to_breakpoint (struct breakpoint *b,
9015 const struct symtab_and_line *sal)
9016 {
9017 struct bp_location *loc, **tmp;
9018 CORE_ADDR adjusted_address;
9019 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
9020
9021 if (loc_gdbarch == NULL)
9022 loc_gdbarch = b->gdbarch;
9023
9024 /* Adjust the breakpoint's address prior to allocating a location.
9025 Once we call allocate_bp_location(), that mostly uninitialized
9026 location will be placed on the location chain. Adjustment of the
9027 breakpoint may cause target_read_memory() to be called and we do
9028 not want its scan of the location chain to find a breakpoint and
9029 location that's only been partially initialized. */
9030 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
9031 sal->pc, b->type);
9032
9033 /* Sort the locations by their ADDRESS. */
9034 loc = allocate_bp_location (b);
9035 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
9036 tmp = &((*tmp)->next))
9037 ;
9038 loc->next = *tmp;
9039 *tmp = loc;
9040
9041 loc->requested_address = sal->pc;
9042 loc->address = adjusted_address;
9043 loc->pspace = sal->pspace;
9044 loc->probe.probe = sal->probe;
9045 loc->probe.objfile = sal->objfile;
9046 gdb_assert (loc->pspace != NULL);
9047 loc->section = sal->section;
9048 loc->gdbarch = loc_gdbarch;
9049 loc->line_number = sal->line;
9050 loc->symtab = sal->symtab;
9051
9052 set_breakpoint_location_function (loc,
9053 sal->explicit_pc || sal->explicit_line);
9054
9055 /* While by definition, permanent breakpoints are already present in the
9056 code, we don't mark the location as inserted. Normally one would expect
9057 that GDB could rely on that breakpoint instruction to stop the program,
9058 thus removing the need to insert its own breakpoint, except that executing
9059 the breakpoint instruction can kill the target instead of reporting a
9060 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
9061 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
9062 with "Trap 0x02 while interrupts disabled, Error state". Letting the
9063 breakpoint be inserted normally results in QEMU knowing about the GDB
9064 breakpoint, and thus trap before the breakpoint instruction is executed.
9065 (If GDB later needs to continue execution past the permanent breakpoint,
9066 it manually increments the PC, thus avoiding executing the breakpoint
9067 instruction.) */
9068 if (bp_loc_is_permanent (loc))
9069 loc->permanent = 1;
9070
9071 return loc;
9072 }
9073 \f
9074
9075 /* See breakpoint.h. */
9076
9077 int
9078 program_breakpoint_here_p (struct gdbarch *gdbarch, CORE_ADDR address)
9079 {
9080 int len;
9081 CORE_ADDR addr;
9082 const gdb_byte *bpoint;
9083 gdb_byte *target_mem;
9084 struct cleanup *cleanup;
9085 int retval = 0;
9086
9087 addr = address;
9088 bpoint = gdbarch_breakpoint_from_pc (gdbarch, &addr, &len);
9089
9090 /* Software breakpoints unsupported? */
9091 if (bpoint == NULL)
9092 return 0;
9093
9094 target_mem = (gdb_byte *) alloca (len);
9095
9096 /* Enable the automatic memory restoration from breakpoints while
9097 we read the memory. Otherwise we could say about our temporary
9098 breakpoints they are permanent. */
9099 cleanup = make_show_memory_breakpoints_cleanup (0);
9100
9101 if (target_read_memory (address, target_mem, len) == 0
9102 && memcmp (target_mem, bpoint, len) == 0)
9103 retval = 1;
9104
9105 do_cleanups (cleanup);
9106
9107 return retval;
9108 }
9109
9110 /* Return 1 if LOC is pointing to a permanent breakpoint,
9111 return 0 otherwise. */
9112
9113 static int
9114 bp_loc_is_permanent (struct bp_location *loc)
9115 {
9116 struct cleanup *cleanup;
9117 int retval;
9118
9119 gdb_assert (loc != NULL);
9120
9121 /* If we have a catchpoint or a watchpoint, just return 0. We should not
9122 attempt to read from the addresses the locations of these breakpoint types
9123 point to. program_breakpoint_here_p, below, will attempt to read
9124 memory. */
9125 if (!breakpoint_address_is_meaningful (loc->owner))
9126 return 0;
9127
9128 cleanup = save_current_space_and_thread ();
9129 switch_to_program_space_and_thread (loc->pspace);
9130
9131 retval = program_breakpoint_here_p (loc->gdbarch, loc->address);
9132
9133 do_cleanups (cleanup);
9134
9135 return retval;
9136 }
9137
9138 /* Build a command list for the dprintf corresponding to the current
9139 settings of the dprintf style options. */
9140
9141 static void
9142 update_dprintf_command_list (struct breakpoint *b)
9143 {
9144 char *dprintf_args = b->extra_string;
9145 char *printf_line = NULL;
9146
9147 if (!dprintf_args)
9148 return;
9149
9150 dprintf_args = skip_spaces (dprintf_args);
9151
9152 /* Allow a comma, as it may have terminated a location, but don't
9153 insist on it. */
9154 if (*dprintf_args == ',')
9155 ++dprintf_args;
9156 dprintf_args = skip_spaces (dprintf_args);
9157
9158 if (*dprintf_args != '"')
9159 error (_("Bad format string, missing '\"'."));
9160
9161 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
9162 printf_line = xstrprintf ("printf %s", dprintf_args);
9163 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
9164 {
9165 if (!dprintf_function)
9166 error (_("No function supplied for dprintf call"));
9167
9168 if (dprintf_channel && strlen (dprintf_channel) > 0)
9169 printf_line = xstrprintf ("call (void) %s (%s,%s)",
9170 dprintf_function,
9171 dprintf_channel,
9172 dprintf_args);
9173 else
9174 printf_line = xstrprintf ("call (void) %s (%s)",
9175 dprintf_function,
9176 dprintf_args);
9177 }
9178 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
9179 {
9180 if (target_can_run_breakpoint_commands ())
9181 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
9182 else
9183 {
9184 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
9185 printf_line = xstrprintf ("printf %s", dprintf_args);
9186 }
9187 }
9188 else
9189 internal_error (__FILE__, __LINE__,
9190 _("Invalid dprintf style."));
9191
9192 gdb_assert (printf_line != NULL);
9193 /* Manufacture a printf sequence. */
9194 {
9195 struct command_line *printf_cmd_line = XNEW (struct command_line);
9196
9197 printf_cmd_line->control_type = simple_control;
9198 printf_cmd_line->body_count = 0;
9199 printf_cmd_line->body_list = NULL;
9200 printf_cmd_line->next = NULL;
9201 printf_cmd_line->line = printf_line;
9202
9203 breakpoint_set_commands (b, printf_cmd_line);
9204 }
9205 }
9206
9207 /* Update all dprintf commands, making their command lists reflect
9208 current style settings. */
9209
9210 static void
9211 update_dprintf_commands (char *args, int from_tty,
9212 struct cmd_list_element *c)
9213 {
9214 struct breakpoint *b;
9215
9216 ALL_BREAKPOINTS (b)
9217 {
9218 if (b->type == bp_dprintf)
9219 update_dprintf_command_list (b);
9220 }
9221 }
9222
9223 /* Create a breakpoint with SAL as location. Use LOCATION
9224 as a description of the location, and COND_STRING
9225 as condition expression. If LOCATION is NULL then create an
9226 "address location" from the address in the SAL. */
9227
9228 static void
9229 init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
9230 struct symtabs_and_lines sals,
9231 struct event_location *location,
9232 char *filter, char *cond_string,
9233 char *extra_string,
9234 enum bptype type, enum bpdisp disposition,
9235 int thread, int task, int ignore_count,
9236 const struct breakpoint_ops *ops, int from_tty,
9237 int enabled, int internal, unsigned flags,
9238 int display_canonical)
9239 {
9240 int i;
9241
9242 if (type == bp_hardware_breakpoint)
9243 {
9244 int target_resources_ok;
9245
9246 i = hw_breakpoint_used_count ();
9247 target_resources_ok =
9248 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9249 i + 1, 0);
9250 if (target_resources_ok == 0)
9251 error (_("No hardware breakpoint support in the target."));
9252 else if (target_resources_ok < 0)
9253 error (_("Hardware breakpoints used exceeds limit."));
9254 }
9255
9256 gdb_assert (sals.nelts > 0);
9257
9258 for (i = 0; i < sals.nelts; ++i)
9259 {
9260 struct symtab_and_line sal = sals.sals[i];
9261 struct bp_location *loc;
9262
9263 if (from_tty)
9264 {
9265 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9266 if (!loc_gdbarch)
9267 loc_gdbarch = gdbarch;
9268
9269 describe_other_breakpoints (loc_gdbarch,
9270 sal.pspace, sal.pc, sal.section, thread);
9271 }
9272
9273 if (i == 0)
9274 {
9275 init_raw_breakpoint (b, gdbarch, sal, type, ops);
9276 b->thread = thread;
9277 b->task = task;
9278
9279 b->cond_string = cond_string;
9280 b->extra_string = extra_string;
9281 b->ignore_count = ignore_count;
9282 b->enable_state = enabled ? bp_enabled : bp_disabled;
9283 b->disposition = disposition;
9284
9285 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9286 b->loc->inserted = 1;
9287
9288 if (type == bp_static_tracepoint)
9289 {
9290 struct tracepoint *t = (struct tracepoint *) b;
9291 struct static_tracepoint_marker marker;
9292
9293 if (strace_marker_p (b))
9294 {
9295 /* We already know the marker exists, otherwise, we
9296 wouldn't see a sal for it. */
9297 const char *p = &event_location_to_string (b->location)[3];
9298 const char *endp;
9299 char *marker_str;
9300
9301 p = skip_spaces_const (p);
9302
9303 endp = skip_to_space_const (p);
9304
9305 marker_str = savestring (p, endp - p);
9306 t->static_trace_marker_id = marker_str;
9307
9308 printf_filtered (_("Probed static tracepoint "
9309 "marker \"%s\"\n"),
9310 t->static_trace_marker_id);
9311 }
9312 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
9313 {
9314 t->static_trace_marker_id = xstrdup (marker.str_id);
9315 release_static_tracepoint_marker (&marker);
9316
9317 printf_filtered (_("Probed static tracepoint "
9318 "marker \"%s\"\n"),
9319 t->static_trace_marker_id);
9320 }
9321 else
9322 warning (_("Couldn't determine the static "
9323 "tracepoint marker to probe"));
9324 }
9325
9326 loc = b->loc;
9327 }
9328 else
9329 {
9330 loc = add_location_to_breakpoint (b, &sal);
9331 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9332 loc->inserted = 1;
9333 }
9334
9335 if (b->cond_string)
9336 {
9337 const char *arg = b->cond_string;
9338
9339 loc->cond = parse_exp_1 (&arg, loc->address,
9340 block_for_pc (loc->address), 0);
9341 if (*arg)
9342 error (_("Garbage '%s' follows condition"), arg);
9343 }
9344
9345 /* Dynamic printf requires and uses additional arguments on the
9346 command line, otherwise it's an error. */
9347 if (type == bp_dprintf)
9348 {
9349 if (b->extra_string)
9350 update_dprintf_command_list (b);
9351 else
9352 error (_("Format string required"));
9353 }
9354 else if (b->extra_string)
9355 error (_("Garbage '%s' at end of command"), b->extra_string);
9356 }
9357
9358 b->display_canonical = display_canonical;
9359 if (location != NULL)
9360 b->location = location;
9361 else
9362 {
9363 const char *addr_string = NULL;
9364 int addr_string_len = 0;
9365
9366 if (location != NULL)
9367 addr_string = event_location_to_string (location);
9368 if (addr_string != NULL)
9369 addr_string_len = strlen (addr_string);
9370
9371 b->location = new_address_location (b->loc->address,
9372 addr_string, addr_string_len);
9373 }
9374 b->filter = filter;
9375 }
9376
9377 static void
9378 create_breakpoint_sal (struct gdbarch *gdbarch,
9379 struct symtabs_and_lines sals,
9380 struct event_location *location,
9381 char *filter, char *cond_string,
9382 char *extra_string,
9383 enum bptype type, enum bpdisp disposition,
9384 int thread, int task, int ignore_count,
9385 const struct breakpoint_ops *ops, int from_tty,
9386 int enabled, int internal, unsigned flags,
9387 int display_canonical)
9388 {
9389 struct breakpoint *b;
9390 struct cleanup *old_chain;
9391
9392 if (is_tracepoint_type (type))
9393 {
9394 struct tracepoint *t;
9395
9396 t = XCNEW (struct tracepoint);
9397 b = &t->base;
9398 }
9399 else
9400 b = XNEW (struct breakpoint);
9401
9402 old_chain = make_cleanup (xfree, b);
9403
9404 init_breakpoint_sal (b, gdbarch,
9405 sals, location,
9406 filter, cond_string, extra_string,
9407 type, disposition,
9408 thread, task, ignore_count,
9409 ops, from_tty,
9410 enabled, internal, flags,
9411 display_canonical);
9412 discard_cleanups (old_chain);
9413
9414 install_breakpoint (internal, b, 0);
9415 }
9416
9417 /* Add SALS.nelts breakpoints to the breakpoint table. For each
9418 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9419 value. COND_STRING, if not NULL, specified the condition to be
9420 used for all breakpoints. Essentially the only case where
9421 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9422 function. In that case, it's still not possible to specify
9423 separate conditions for different overloaded functions, so
9424 we take just a single condition string.
9425
9426 NOTE: If the function succeeds, the caller is expected to cleanup
9427 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
9428 array contents). If the function fails (error() is called), the
9429 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
9430 COND and SALS arrays and each of those arrays contents. */
9431
9432 static void
9433 create_breakpoints_sal (struct gdbarch *gdbarch,
9434 struct linespec_result *canonical,
9435 char *cond_string, char *extra_string,
9436 enum bptype type, enum bpdisp disposition,
9437 int thread, int task, int ignore_count,
9438 const struct breakpoint_ops *ops, int from_tty,
9439 int enabled, int internal, unsigned flags)
9440 {
9441 int i;
9442 struct linespec_sals *lsal;
9443
9444 if (canonical->pre_expanded)
9445 gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
9446
9447 for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
9448 {
9449 /* Note that 'location' can be NULL in the case of a plain
9450 'break', without arguments. */
9451 struct event_location *location
9452 = (canonical->location != NULL
9453 ? copy_event_location (canonical->location) : NULL);
9454 char *filter_string = lsal->canonical ? xstrdup (lsal->canonical) : NULL;
9455 struct cleanup *inner = make_cleanup_delete_event_location (location);
9456
9457 make_cleanup (xfree, filter_string);
9458 create_breakpoint_sal (gdbarch, lsal->sals,
9459 location,
9460 filter_string,
9461 cond_string, extra_string,
9462 type, disposition,
9463 thread, task, ignore_count, ops,
9464 from_tty, enabled, internal, flags,
9465 canonical->special_display);
9466 discard_cleanups (inner);
9467 }
9468 }
9469
9470 /* Parse LOCATION which is assumed to be a SAL specification possibly
9471 followed by conditionals. On return, SALS contains an array of SAL
9472 addresses found. LOCATION points to the end of the SAL (for
9473 linespec locations).
9474
9475 The array and the line spec strings are allocated on the heap, it is
9476 the caller's responsibility to free them. */
9477
9478 static void
9479 parse_breakpoint_sals (const struct event_location *location,
9480 struct linespec_result *canonical)
9481 {
9482 struct symtab_and_line cursal;
9483
9484 if (event_location_type (location) == LINESPEC_LOCATION)
9485 {
9486 const char *address = get_linespec_location (location);
9487
9488 if (address == NULL)
9489 {
9490 /* The last displayed codepoint, if it's valid, is our default
9491 breakpoint address. */
9492 if (last_displayed_sal_is_valid ())
9493 {
9494 struct linespec_sals lsal;
9495 struct symtab_and_line sal;
9496 CORE_ADDR pc;
9497
9498 init_sal (&sal); /* Initialize to zeroes. */
9499 lsal.sals.sals = XNEW (struct symtab_and_line);
9500
9501 /* Set sal's pspace, pc, symtab, and line to the values
9502 corresponding to the last call to print_frame_info.
9503 Be sure to reinitialize LINE with NOTCURRENT == 0
9504 as the breakpoint line number is inappropriate otherwise.
9505 find_pc_line would adjust PC, re-set it back. */
9506 get_last_displayed_sal (&sal);
9507 pc = sal.pc;
9508 sal = find_pc_line (pc, 0);
9509
9510 /* "break" without arguments is equivalent to "break *PC"
9511 where PC is the last displayed codepoint's address. So
9512 make sure to set sal.explicit_pc to prevent GDB from
9513 trying to expand the list of sals to include all other
9514 instances with the same symtab and line. */
9515 sal.pc = pc;
9516 sal.explicit_pc = 1;
9517
9518 lsal.sals.sals[0] = sal;
9519 lsal.sals.nelts = 1;
9520 lsal.canonical = NULL;
9521
9522 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
9523 return;
9524 }
9525 else
9526 error (_("No default breakpoint address now."));
9527 }
9528 }
9529
9530 /* Force almost all breakpoints to be in terms of the
9531 current_source_symtab (which is decode_line_1's default).
9532 This should produce the results we want almost all of the
9533 time while leaving default_breakpoint_* alone.
9534
9535 ObjC: However, don't match an Objective-C method name which
9536 may have a '+' or '-' succeeded by a '['. */
9537 cursal = get_current_source_symtab_and_line ();
9538 if (last_displayed_sal_is_valid ())
9539 {
9540 const char *address = NULL;
9541
9542 if (event_location_type (location) == LINESPEC_LOCATION)
9543 address = get_linespec_location (location);
9544
9545 if (!cursal.symtab
9546 || (address != NULL
9547 && strchr ("+-", address[0]) != NULL
9548 && address[1] != '['))
9549 {
9550 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9551 get_last_displayed_symtab (),
9552 get_last_displayed_line (),
9553 canonical, NULL, NULL);
9554 return;
9555 }
9556 }
9557
9558 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9559 cursal.symtab, cursal.line, canonical, NULL, NULL);
9560 }
9561
9562
9563 /* Convert each SAL into a real PC. Verify that the PC can be
9564 inserted as a breakpoint. If it can't throw an error. */
9565
9566 static void
9567 breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
9568 {
9569 int i;
9570
9571 for (i = 0; i < sals->nelts; i++)
9572 resolve_sal_pc (&sals->sals[i]);
9573 }
9574
9575 /* Fast tracepoints may have restrictions on valid locations. For
9576 instance, a fast tracepoint using a jump instead of a trap will
9577 likely have to overwrite more bytes than a trap would, and so can
9578 only be placed where the instruction is longer than the jump, or a
9579 multi-instruction sequence does not have a jump into the middle of
9580 it, etc. */
9581
9582 static void
9583 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9584 struct symtabs_and_lines *sals)
9585 {
9586 int i, rslt;
9587 struct symtab_and_line *sal;
9588 char *msg;
9589 struct cleanup *old_chain;
9590
9591 for (i = 0; i < sals->nelts; i++)
9592 {
9593 struct gdbarch *sarch;
9594
9595 sal = &sals->sals[i];
9596
9597 sarch = get_sal_arch (*sal);
9598 /* We fall back to GDBARCH if there is no architecture
9599 associated with SAL. */
9600 if (sarch == NULL)
9601 sarch = gdbarch;
9602 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc, &msg);
9603 old_chain = make_cleanup (xfree, msg);
9604
9605 if (!rslt)
9606 error (_("May not have a fast tracepoint at 0x%s%s"),
9607 paddress (sarch, sal->pc), (msg ? msg : ""));
9608
9609 do_cleanups (old_chain);
9610 }
9611 }
9612
9613 /* Given TOK, a string specification of condition and thread, as
9614 accepted by the 'break' command, extract the condition
9615 string and thread number and set *COND_STRING and *THREAD.
9616 PC identifies the context at which the condition should be parsed.
9617 If no condition is found, *COND_STRING is set to NULL.
9618 If no thread is found, *THREAD is set to -1. */
9619
9620 static void
9621 find_condition_and_thread (const char *tok, CORE_ADDR pc,
9622 char **cond_string, int *thread, int *task,
9623 char **rest)
9624 {
9625 *cond_string = NULL;
9626 *thread = -1;
9627 *task = 0;
9628 *rest = NULL;
9629
9630 while (tok && *tok)
9631 {
9632 const char *end_tok;
9633 int toklen;
9634 const char *cond_start = NULL;
9635 const char *cond_end = NULL;
9636
9637 tok = skip_spaces_const (tok);
9638
9639 if ((*tok == '"' || *tok == ',') && rest)
9640 {
9641 *rest = savestring (tok, strlen (tok));
9642 return;
9643 }
9644
9645 end_tok = skip_to_space_const (tok);
9646
9647 toklen = end_tok - tok;
9648
9649 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9650 {
9651 struct expression *expr;
9652
9653 tok = cond_start = end_tok + 1;
9654 expr = parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
9655 xfree (expr);
9656 cond_end = tok;
9657 *cond_string = savestring (cond_start, cond_end - cond_start);
9658 }
9659 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9660 {
9661 const char *tmptok;
9662 struct thread_info *thr;
9663
9664 tok = end_tok + 1;
9665 thr = parse_thread_id (tok, &tmptok);
9666 if (tok == tmptok)
9667 error (_("Junk after thread keyword."));
9668 *thread = thr->global_num;
9669 tok = tmptok;
9670 }
9671 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9672 {
9673 char *tmptok;
9674
9675 tok = end_tok + 1;
9676 *task = strtol (tok, &tmptok, 0);
9677 if (tok == tmptok)
9678 error (_("Junk after task keyword."));
9679 if (!valid_task_id (*task))
9680 error (_("Unknown task %d."), *task);
9681 tok = tmptok;
9682 }
9683 else if (rest)
9684 {
9685 *rest = savestring (tok, strlen (tok));
9686 return;
9687 }
9688 else
9689 error (_("Junk at end of arguments."));
9690 }
9691 }
9692
9693 /* Decode a static tracepoint marker spec. */
9694
9695 static struct symtabs_and_lines
9696 decode_static_tracepoint_spec (const char **arg_p)
9697 {
9698 VEC(static_tracepoint_marker_p) *markers = NULL;
9699 struct symtabs_and_lines sals;
9700 struct cleanup *old_chain;
9701 const char *p = &(*arg_p)[3];
9702 const char *endp;
9703 char *marker_str;
9704 int i;
9705
9706 p = skip_spaces_const (p);
9707
9708 endp = skip_to_space_const (p);
9709
9710 marker_str = savestring (p, endp - p);
9711 old_chain = make_cleanup (xfree, marker_str);
9712
9713 markers = target_static_tracepoint_markers_by_strid (marker_str);
9714 if (VEC_empty(static_tracepoint_marker_p, markers))
9715 error (_("No known static tracepoint marker named %s"), marker_str);
9716
9717 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
9718 sals.sals = XNEWVEC (struct symtab_and_line, sals.nelts);
9719
9720 for (i = 0; i < sals.nelts; i++)
9721 {
9722 struct static_tracepoint_marker *marker;
9723
9724 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9725
9726 init_sal (&sals.sals[i]);
9727
9728 sals.sals[i] = find_pc_line (marker->address, 0);
9729 sals.sals[i].pc = marker->address;
9730
9731 release_static_tracepoint_marker (marker);
9732 }
9733
9734 do_cleanups (old_chain);
9735
9736 *arg_p = endp;
9737 return sals;
9738 }
9739
9740 /* See breakpoint.h. */
9741
9742 int
9743 create_breakpoint (struct gdbarch *gdbarch,
9744 const struct event_location *location, char *cond_string,
9745 int thread, char *extra_string,
9746 int parse_extra,
9747 int tempflag, enum bptype type_wanted,
9748 int ignore_count,
9749 enum auto_boolean pending_break_support,
9750 const struct breakpoint_ops *ops,
9751 int from_tty, int enabled, int internal,
9752 unsigned flags)
9753 {
9754 struct linespec_result canonical;
9755 struct cleanup *old_chain;
9756 struct cleanup *bkpt_chain = NULL;
9757 int pending = 0;
9758 int task = 0;
9759 int prev_bkpt_count = breakpoint_count;
9760
9761 gdb_assert (ops != NULL);
9762
9763 /* If extra_string isn't useful, set it to NULL. */
9764 if (extra_string != NULL && *extra_string == '\0')
9765 extra_string = NULL;
9766
9767 init_linespec_result (&canonical);
9768
9769 TRY
9770 {
9771 ops->create_sals_from_location (location, &canonical, type_wanted);
9772 }
9773 CATCH (e, RETURN_MASK_ERROR)
9774 {
9775 /* If caller is interested in rc value from parse, set
9776 value. */
9777 if (e.error == NOT_FOUND_ERROR)
9778 {
9779 /* If pending breakpoint support is turned off, throw
9780 error. */
9781
9782 if (pending_break_support == AUTO_BOOLEAN_FALSE)
9783 throw_exception (e);
9784
9785 exception_print (gdb_stderr, e);
9786
9787 /* If pending breakpoint support is auto query and the user
9788 selects no, then simply return the error code. */
9789 if (pending_break_support == AUTO_BOOLEAN_AUTO
9790 && !nquery (_("Make %s pending on future shared library load? "),
9791 bptype_string (type_wanted)))
9792 return 0;
9793
9794 /* At this point, either the user was queried about setting
9795 a pending breakpoint and selected yes, or pending
9796 breakpoint behavior is on and thus a pending breakpoint
9797 is defaulted on behalf of the user. */
9798 pending = 1;
9799 }
9800 else
9801 throw_exception (e);
9802 }
9803 END_CATCH
9804
9805 if (!pending && VEC_empty (linespec_sals, canonical.sals))
9806 return 0;
9807
9808 /* Create a chain of things that always need to be cleaned up. */
9809 old_chain = make_cleanup_destroy_linespec_result (&canonical);
9810
9811 /* ----------------------------- SNIP -----------------------------
9812 Anything added to the cleanup chain beyond this point is assumed
9813 to be part of a breakpoint. If the breakpoint create succeeds
9814 then the memory is not reclaimed. */
9815 bkpt_chain = make_cleanup (null_cleanup, 0);
9816
9817 /* Resolve all line numbers to PC's and verify that the addresses
9818 are ok for the target. */
9819 if (!pending)
9820 {
9821 int ix;
9822 struct linespec_sals *iter;
9823
9824 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9825 breakpoint_sals_to_pc (&iter->sals);
9826 }
9827
9828 /* Fast tracepoints may have additional restrictions on location. */
9829 if (!pending && type_wanted == bp_fast_tracepoint)
9830 {
9831 int ix;
9832 struct linespec_sals *iter;
9833
9834 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9835 check_fast_tracepoint_sals (gdbarch, &iter->sals);
9836 }
9837
9838 /* Verify that condition can be parsed, before setting any
9839 breakpoints. Allocate a separate condition expression for each
9840 breakpoint. */
9841 if (!pending)
9842 {
9843 if (parse_extra)
9844 {
9845 char *rest;
9846 struct linespec_sals *lsal;
9847
9848 lsal = VEC_index (linespec_sals, canonical.sals, 0);
9849
9850 /* Here we only parse 'arg' to separate condition
9851 from thread number, so parsing in context of first
9852 sal is OK. When setting the breakpoint we'll
9853 re-parse it in context of each sal. */
9854
9855 find_condition_and_thread (extra_string, lsal->sals.sals[0].pc,
9856 &cond_string, &thread, &task, &rest);
9857 if (cond_string)
9858 make_cleanup (xfree, cond_string);
9859 if (rest)
9860 make_cleanup (xfree, rest);
9861 if (rest)
9862 extra_string = rest;
9863 else
9864 extra_string = NULL;
9865 }
9866 else
9867 {
9868 if (type_wanted != bp_dprintf
9869 && extra_string != NULL && *extra_string != '\0')
9870 error (_("Garbage '%s' at end of location"), extra_string);
9871
9872 /* Create a private copy of condition string. */
9873 if (cond_string)
9874 {
9875 cond_string = xstrdup (cond_string);
9876 make_cleanup (xfree, cond_string);
9877 }
9878 /* Create a private copy of any extra string. */
9879 if (extra_string)
9880 {
9881 extra_string = xstrdup (extra_string);
9882 make_cleanup (xfree, extra_string);
9883 }
9884 }
9885
9886 ops->create_breakpoints_sal (gdbarch, &canonical,
9887 cond_string, extra_string, type_wanted,
9888 tempflag ? disp_del : disp_donttouch,
9889 thread, task, ignore_count, ops,
9890 from_tty, enabled, internal, flags);
9891 }
9892 else
9893 {
9894 struct breakpoint *b;
9895
9896 if (is_tracepoint_type (type_wanted))
9897 {
9898 struct tracepoint *t;
9899
9900 t = XCNEW (struct tracepoint);
9901 b = &t->base;
9902 }
9903 else
9904 b = XNEW (struct breakpoint);
9905
9906 init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops);
9907 b->location = copy_event_location (location);
9908
9909 if (parse_extra)
9910 b->cond_string = NULL;
9911 else
9912 {
9913 /* Create a private copy of condition string. */
9914 if (cond_string)
9915 {
9916 cond_string = xstrdup (cond_string);
9917 make_cleanup (xfree, cond_string);
9918 }
9919 b->cond_string = cond_string;
9920 b->thread = thread;
9921 }
9922
9923 /* Create a private copy of any extra string. */
9924 if (extra_string != NULL)
9925 {
9926 extra_string = xstrdup (extra_string);
9927 make_cleanup (xfree, extra_string);
9928 }
9929 b->extra_string = extra_string;
9930 b->ignore_count = ignore_count;
9931 b->disposition = tempflag ? disp_del : disp_donttouch;
9932 b->condition_not_parsed = 1;
9933 b->enable_state = enabled ? bp_enabled : bp_disabled;
9934 if ((type_wanted != bp_breakpoint
9935 && type_wanted != bp_hardware_breakpoint) || thread != -1)
9936 b->pspace = current_program_space;
9937
9938 install_breakpoint (internal, b, 0);
9939 }
9940
9941 if (VEC_length (linespec_sals, canonical.sals) > 1)
9942 {
9943 warning (_("Multiple breakpoints were set.\nUse the "
9944 "\"delete\" command to delete unwanted breakpoints."));
9945 prev_breakpoint_count = prev_bkpt_count;
9946 }
9947
9948 /* That's it. Discard the cleanups for data inserted into the
9949 breakpoint. */
9950 discard_cleanups (bkpt_chain);
9951 /* But cleanup everything else. */
9952 do_cleanups (old_chain);
9953
9954 /* error call may happen here - have BKPT_CHAIN already discarded. */
9955 update_global_location_list (UGLL_MAY_INSERT);
9956
9957 return 1;
9958 }
9959
9960 /* Set a breakpoint.
9961 ARG is a string describing breakpoint address,
9962 condition, and thread.
9963 FLAG specifies if a breakpoint is hardware on,
9964 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9965 and BP_TEMPFLAG. */
9966
9967 static void
9968 break_command_1 (char *arg, int flag, int from_tty)
9969 {
9970 int tempflag = flag & BP_TEMPFLAG;
9971 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9972 ? bp_hardware_breakpoint
9973 : bp_breakpoint);
9974 struct breakpoint_ops *ops;
9975 struct event_location *location;
9976 struct cleanup *cleanup;
9977
9978 location = string_to_event_location (&arg, current_language);
9979 cleanup = make_cleanup_delete_event_location (location);
9980
9981 /* Matching breakpoints on probes. */
9982 if (location != NULL
9983 && event_location_type (location) == PROBE_LOCATION)
9984 ops = &bkpt_probe_breakpoint_ops;
9985 else
9986 ops = &bkpt_breakpoint_ops;
9987
9988 create_breakpoint (get_current_arch (),
9989 location,
9990 NULL, 0, arg, 1 /* parse arg */,
9991 tempflag, type_wanted,
9992 0 /* Ignore count */,
9993 pending_break_support,
9994 ops,
9995 from_tty,
9996 1 /* enabled */,
9997 0 /* internal */,
9998 0);
9999 do_cleanups (cleanup);
10000 }
10001
10002 /* Helper function for break_command_1 and disassemble_command. */
10003
10004 void
10005 resolve_sal_pc (struct symtab_and_line *sal)
10006 {
10007 CORE_ADDR pc;
10008
10009 if (sal->pc == 0 && sal->symtab != NULL)
10010 {
10011 if (!find_line_pc (sal->symtab, sal->line, &pc))
10012 error (_("No line %d in file \"%s\"."),
10013 sal->line, symtab_to_filename_for_display (sal->symtab));
10014 sal->pc = pc;
10015
10016 /* If this SAL corresponds to a breakpoint inserted using a line
10017 number, then skip the function prologue if necessary. */
10018 if (sal->explicit_line)
10019 skip_prologue_sal (sal);
10020 }
10021
10022 if (sal->section == 0 && sal->symtab != NULL)
10023 {
10024 const struct blockvector *bv;
10025 const struct block *b;
10026 struct symbol *sym;
10027
10028 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
10029 SYMTAB_COMPUNIT (sal->symtab));
10030 if (bv != NULL)
10031 {
10032 sym = block_linkage_function (b);
10033 if (sym != NULL)
10034 {
10035 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
10036 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
10037 sym);
10038 }
10039 else
10040 {
10041 /* It really is worthwhile to have the section, so we'll
10042 just have to look harder. This case can be executed
10043 if we have line numbers but no functions (as can
10044 happen in assembly source). */
10045
10046 struct bound_minimal_symbol msym;
10047 struct cleanup *old_chain = save_current_space_and_thread ();
10048
10049 switch_to_program_space_and_thread (sal->pspace);
10050
10051 msym = lookup_minimal_symbol_by_pc (sal->pc);
10052 if (msym.minsym)
10053 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
10054
10055 do_cleanups (old_chain);
10056 }
10057 }
10058 }
10059 }
10060
10061 void
10062 break_command (char *arg, int from_tty)
10063 {
10064 break_command_1 (arg, 0, from_tty);
10065 }
10066
10067 void
10068 tbreak_command (char *arg, int from_tty)
10069 {
10070 break_command_1 (arg, BP_TEMPFLAG, from_tty);
10071 }
10072
10073 static void
10074 hbreak_command (char *arg, int from_tty)
10075 {
10076 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
10077 }
10078
10079 static void
10080 thbreak_command (char *arg, int from_tty)
10081 {
10082 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
10083 }
10084
10085 static void
10086 stop_command (char *arg, int from_tty)
10087 {
10088 printf_filtered (_("Specify the type of breakpoint to set.\n\
10089 Usage: stop in <function | address>\n\
10090 stop at <line>\n"));
10091 }
10092
10093 static void
10094 stopin_command (char *arg, int from_tty)
10095 {
10096 int badInput = 0;
10097
10098 if (arg == (char *) NULL)
10099 badInput = 1;
10100 else if (*arg != '*')
10101 {
10102 char *argptr = arg;
10103 int hasColon = 0;
10104
10105 /* Look for a ':'. If this is a line number specification, then
10106 say it is bad, otherwise, it should be an address or
10107 function/method name. */
10108 while (*argptr && !hasColon)
10109 {
10110 hasColon = (*argptr == ':');
10111 argptr++;
10112 }
10113
10114 if (hasColon)
10115 badInput = (*argptr != ':'); /* Not a class::method */
10116 else
10117 badInput = isdigit (*arg); /* a simple line number */
10118 }
10119
10120 if (badInput)
10121 printf_filtered (_("Usage: stop in <function | address>\n"));
10122 else
10123 break_command_1 (arg, 0, from_tty);
10124 }
10125
10126 static void
10127 stopat_command (char *arg, int from_tty)
10128 {
10129 int badInput = 0;
10130
10131 if (arg == (char *) NULL || *arg == '*') /* no line number */
10132 badInput = 1;
10133 else
10134 {
10135 char *argptr = arg;
10136 int hasColon = 0;
10137
10138 /* Look for a ':'. If there is a '::' then get out, otherwise
10139 it is probably a line number. */
10140 while (*argptr && !hasColon)
10141 {
10142 hasColon = (*argptr == ':');
10143 argptr++;
10144 }
10145
10146 if (hasColon)
10147 badInput = (*argptr == ':'); /* we have class::method */
10148 else
10149 badInput = !isdigit (*arg); /* not a line number */
10150 }
10151
10152 if (badInput)
10153 printf_filtered (_("Usage: stop at <line>\n"));
10154 else
10155 break_command_1 (arg, 0, from_tty);
10156 }
10157
10158 /* The dynamic printf command is mostly like a regular breakpoint, but
10159 with a prewired command list consisting of a single output command,
10160 built from extra arguments supplied on the dprintf command
10161 line. */
10162
10163 static void
10164 dprintf_command (char *arg, int from_tty)
10165 {
10166 struct event_location *location;
10167 struct cleanup *cleanup;
10168
10169 location = string_to_event_location (&arg, current_language);
10170 cleanup = make_cleanup_delete_event_location (location);
10171
10172 /* If non-NULL, ARG should have been advanced past the location;
10173 the next character must be ','. */
10174 if (arg != NULL)
10175 {
10176 if (arg[0] != ',' || arg[1] == '\0')
10177 error (_("Format string required"));
10178 else
10179 {
10180 /* Skip the comma. */
10181 ++arg;
10182 }
10183 }
10184
10185 create_breakpoint (get_current_arch (),
10186 location,
10187 NULL, 0, arg, 1 /* parse arg */,
10188 0, bp_dprintf,
10189 0 /* Ignore count */,
10190 pending_break_support,
10191 &dprintf_breakpoint_ops,
10192 from_tty,
10193 1 /* enabled */,
10194 0 /* internal */,
10195 0);
10196 do_cleanups (cleanup);
10197 }
10198
10199 static void
10200 agent_printf_command (char *arg, int from_tty)
10201 {
10202 error (_("May only run agent-printf on the target"));
10203 }
10204
10205 /* Implement the "breakpoint_hit" breakpoint_ops method for
10206 ranged breakpoints. */
10207
10208 static int
10209 breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
10210 struct address_space *aspace,
10211 CORE_ADDR bp_addr,
10212 const struct target_waitstatus *ws)
10213 {
10214 if (ws->kind != TARGET_WAITKIND_STOPPED
10215 || ws->value.sig != GDB_SIGNAL_TRAP)
10216 return 0;
10217
10218 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
10219 bl->length, aspace, bp_addr);
10220 }
10221
10222 /* Implement the "resources_needed" breakpoint_ops method for
10223 ranged breakpoints. */
10224
10225 static int
10226 resources_needed_ranged_breakpoint (const struct bp_location *bl)
10227 {
10228 return target_ranged_break_num_registers ();
10229 }
10230
10231 /* Implement the "print_it" breakpoint_ops method for
10232 ranged breakpoints. */
10233
10234 static enum print_stop_action
10235 print_it_ranged_breakpoint (bpstat bs)
10236 {
10237 struct breakpoint *b = bs->breakpoint_at;
10238 struct bp_location *bl = b->loc;
10239 struct ui_out *uiout = current_uiout;
10240
10241 gdb_assert (b->type == bp_hardware_breakpoint);
10242
10243 /* Ranged breakpoints have only one location. */
10244 gdb_assert (bl && bl->next == NULL);
10245
10246 annotate_breakpoint (b->number);
10247
10248 maybe_print_thread_hit_breakpoint (uiout);
10249
10250 if (b->disposition == disp_del)
10251 ui_out_text (uiout, "Temporary ranged breakpoint ");
10252 else
10253 ui_out_text (uiout, "Ranged breakpoint ");
10254 if (ui_out_is_mi_like_p (uiout))
10255 {
10256 ui_out_field_string (uiout, "reason",
10257 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
10258 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
10259 }
10260 ui_out_field_int (uiout, "bkptno", b->number);
10261 ui_out_text (uiout, ", ");
10262
10263 return PRINT_SRC_AND_LOC;
10264 }
10265
10266 /* Implement the "print_one" breakpoint_ops method for
10267 ranged breakpoints. */
10268
10269 static void
10270 print_one_ranged_breakpoint (struct breakpoint *b,
10271 struct bp_location **last_loc)
10272 {
10273 struct bp_location *bl = b->loc;
10274 struct value_print_options opts;
10275 struct ui_out *uiout = current_uiout;
10276
10277 /* Ranged breakpoints have only one location. */
10278 gdb_assert (bl && bl->next == NULL);
10279
10280 get_user_print_options (&opts);
10281
10282 if (opts.addressprint)
10283 /* We don't print the address range here, it will be printed later
10284 by print_one_detail_ranged_breakpoint. */
10285 ui_out_field_skip (uiout, "addr");
10286 annotate_field (5);
10287 print_breakpoint_location (b, bl);
10288 *last_loc = bl;
10289 }
10290
10291 /* Implement the "print_one_detail" breakpoint_ops method for
10292 ranged breakpoints. */
10293
10294 static void
10295 print_one_detail_ranged_breakpoint (const struct breakpoint *b,
10296 struct ui_out *uiout)
10297 {
10298 CORE_ADDR address_start, address_end;
10299 struct bp_location *bl = b->loc;
10300 struct ui_file *stb = mem_fileopen ();
10301 struct cleanup *cleanup = make_cleanup_ui_file_delete (stb);
10302
10303 gdb_assert (bl);
10304
10305 address_start = bl->address;
10306 address_end = address_start + bl->length - 1;
10307
10308 ui_out_text (uiout, "\taddress range: ");
10309 fprintf_unfiltered (stb, "[%s, %s]",
10310 print_core_address (bl->gdbarch, address_start),
10311 print_core_address (bl->gdbarch, address_end));
10312 ui_out_field_stream (uiout, "addr", stb);
10313 ui_out_text (uiout, "\n");
10314
10315 do_cleanups (cleanup);
10316 }
10317
10318 /* Implement the "print_mention" breakpoint_ops method for
10319 ranged breakpoints. */
10320
10321 static void
10322 print_mention_ranged_breakpoint (struct breakpoint *b)
10323 {
10324 struct bp_location *bl = b->loc;
10325 struct ui_out *uiout = current_uiout;
10326
10327 gdb_assert (bl);
10328 gdb_assert (b->type == bp_hardware_breakpoint);
10329
10330 if (ui_out_is_mi_like_p (uiout))
10331 return;
10332
10333 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
10334 b->number, paddress (bl->gdbarch, bl->address),
10335 paddress (bl->gdbarch, bl->address + bl->length - 1));
10336 }
10337
10338 /* Implement the "print_recreate" breakpoint_ops method for
10339 ranged breakpoints. */
10340
10341 static void
10342 print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
10343 {
10344 fprintf_unfiltered (fp, "break-range %s, %s",
10345 event_location_to_string (b->location),
10346 event_location_to_string (b->location_range_end));
10347 print_recreate_thread (b, fp);
10348 }
10349
10350 /* The breakpoint_ops structure to be used in ranged breakpoints. */
10351
10352 static struct breakpoint_ops ranged_breakpoint_ops;
10353
10354 /* Find the address where the end of the breakpoint range should be
10355 placed, given the SAL of the end of the range. This is so that if
10356 the user provides a line number, the end of the range is set to the
10357 last instruction of the given line. */
10358
10359 static CORE_ADDR
10360 find_breakpoint_range_end (struct symtab_and_line sal)
10361 {
10362 CORE_ADDR end;
10363
10364 /* If the user provided a PC value, use it. Otherwise,
10365 find the address of the end of the given location. */
10366 if (sal.explicit_pc)
10367 end = sal.pc;
10368 else
10369 {
10370 int ret;
10371 CORE_ADDR start;
10372
10373 ret = find_line_pc_range (sal, &start, &end);
10374 if (!ret)
10375 error (_("Could not find location of the end of the range."));
10376
10377 /* find_line_pc_range returns the start of the next line. */
10378 end--;
10379 }
10380
10381 return end;
10382 }
10383
10384 /* Implement the "break-range" CLI command. */
10385
10386 static void
10387 break_range_command (char *arg, int from_tty)
10388 {
10389 char *arg_start, *addr_string_start, *addr_string_end;
10390 struct linespec_result canonical_start, canonical_end;
10391 int bp_count, can_use_bp, length;
10392 CORE_ADDR end;
10393 struct breakpoint *b;
10394 struct symtab_and_line sal_start, sal_end;
10395 struct cleanup *cleanup_bkpt;
10396 struct linespec_sals *lsal_start, *lsal_end;
10397 struct event_location *start_location, *end_location;
10398
10399 /* We don't support software ranged breakpoints. */
10400 if (target_ranged_break_num_registers () < 0)
10401 error (_("This target does not support hardware ranged breakpoints."));
10402
10403 bp_count = hw_breakpoint_used_count ();
10404 bp_count += target_ranged_break_num_registers ();
10405 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10406 bp_count, 0);
10407 if (can_use_bp < 0)
10408 error (_("Hardware breakpoints used exceeds limit."));
10409
10410 arg = skip_spaces (arg);
10411 if (arg == NULL || arg[0] == '\0')
10412 error(_("No address range specified."));
10413
10414 init_linespec_result (&canonical_start);
10415
10416 arg_start = arg;
10417 start_location = string_to_event_location (&arg, current_language);
10418 cleanup_bkpt = make_cleanup_delete_event_location (start_location);
10419 parse_breakpoint_sals (start_location, &canonical_start);
10420 make_cleanup_destroy_linespec_result (&canonical_start);
10421
10422 if (arg[0] != ',')
10423 error (_("Too few arguments."));
10424 else if (VEC_empty (linespec_sals, canonical_start.sals))
10425 error (_("Could not find location of the beginning of the range."));
10426
10427 lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
10428
10429 if (VEC_length (linespec_sals, canonical_start.sals) > 1
10430 || lsal_start->sals.nelts != 1)
10431 error (_("Cannot create a ranged breakpoint with multiple locations."));
10432
10433 sal_start = lsal_start->sals.sals[0];
10434 addr_string_start = savestring (arg_start, arg - arg_start);
10435 make_cleanup (xfree, addr_string_start);
10436
10437 arg++; /* Skip the comma. */
10438 arg = skip_spaces (arg);
10439
10440 /* Parse the end location. */
10441
10442 init_linespec_result (&canonical_end);
10443 arg_start = arg;
10444
10445 /* We call decode_line_full directly here instead of using
10446 parse_breakpoint_sals because we need to specify the start location's
10447 symtab and line as the default symtab and line for the end of the
10448 range. This makes it possible to have ranges like "foo.c:27, +14",
10449 where +14 means 14 lines from the start location. */
10450 end_location = string_to_event_location (&arg, current_language);
10451 make_cleanup_delete_event_location (end_location);
10452 decode_line_full (end_location, DECODE_LINE_FUNFIRSTLINE, NULL,
10453 sal_start.symtab, sal_start.line,
10454 &canonical_end, NULL, NULL);
10455
10456 make_cleanup_destroy_linespec_result (&canonical_end);
10457
10458 if (VEC_empty (linespec_sals, canonical_end.sals))
10459 error (_("Could not find location of the end of the range."));
10460
10461 lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
10462 if (VEC_length (linespec_sals, canonical_end.sals) > 1
10463 || lsal_end->sals.nelts != 1)
10464 error (_("Cannot create a ranged breakpoint with multiple locations."));
10465
10466 sal_end = lsal_end->sals.sals[0];
10467
10468 end = find_breakpoint_range_end (sal_end);
10469 if (sal_start.pc > end)
10470 error (_("Invalid address range, end precedes start."));
10471
10472 length = end - sal_start.pc + 1;
10473 if (length < 0)
10474 /* Length overflowed. */
10475 error (_("Address range too large."));
10476 else if (length == 1)
10477 {
10478 /* This range is simple enough to be handled by
10479 the `hbreak' command. */
10480 hbreak_command (addr_string_start, 1);
10481
10482 do_cleanups (cleanup_bkpt);
10483
10484 return;
10485 }
10486
10487 /* Now set up the breakpoint. */
10488 b = set_raw_breakpoint (get_current_arch (), sal_start,
10489 bp_hardware_breakpoint, &ranged_breakpoint_ops);
10490 set_breakpoint_count (breakpoint_count + 1);
10491 b->number = breakpoint_count;
10492 b->disposition = disp_donttouch;
10493 b->location = copy_event_location (start_location);
10494 b->location_range_end = copy_event_location (end_location);
10495 b->loc->length = length;
10496
10497 do_cleanups (cleanup_bkpt);
10498
10499 mention (b);
10500 observer_notify_breakpoint_created (b);
10501 update_global_location_list (UGLL_MAY_INSERT);
10502 }
10503
10504 /* Return non-zero if EXP is verified as constant. Returned zero
10505 means EXP is variable. Also the constant detection may fail for
10506 some constant expressions and in such case still falsely return
10507 zero. */
10508
10509 static int
10510 watchpoint_exp_is_const (const struct expression *exp)
10511 {
10512 int i = exp->nelts;
10513
10514 while (i > 0)
10515 {
10516 int oplenp, argsp;
10517
10518 /* We are only interested in the descriptor of each element. */
10519 operator_length (exp, i, &oplenp, &argsp);
10520 i -= oplenp;
10521
10522 switch (exp->elts[i].opcode)
10523 {
10524 case BINOP_ADD:
10525 case BINOP_SUB:
10526 case BINOP_MUL:
10527 case BINOP_DIV:
10528 case BINOP_REM:
10529 case BINOP_MOD:
10530 case BINOP_LSH:
10531 case BINOP_RSH:
10532 case BINOP_LOGICAL_AND:
10533 case BINOP_LOGICAL_OR:
10534 case BINOP_BITWISE_AND:
10535 case BINOP_BITWISE_IOR:
10536 case BINOP_BITWISE_XOR:
10537 case BINOP_EQUAL:
10538 case BINOP_NOTEQUAL:
10539 case BINOP_LESS:
10540 case BINOP_GTR:
10541 case BINOP_LEQ:
10542 case BINOP_GEQ:
10543 case BINOP_REPEAT:
10544 case BINOP_COMMA:
10545 case BINOP_EXP:
10546 case BINOP_MIN:
10547 case BINOP_MAX:
10548 case BINOP_INTDIV:
10549 case BINOP_CONCAT:
10550 case TERNOP_COND:
10551 case TERNOP_SLICE:
10552
10553 case OP_LONG:
10554 case OP_DOUBLE:
10555 case OP_DECFLOAT:
10556 case OP_LAST:
10557 case OP_COMPLEX:
10558 case OP_STRING:
10559 case OP_ARRAY:
10560 case OP_TYPE:
10561 case OP_TYPEOF:
10562 case OP_DECLTYPE:
10563 case OP_TYPEID:
10564 case OP_NAME:
10565 case OP_OBJC_NSSTRING:
10566
10567 case UNOP_NEG:
10568 case UNOP_LOGICAL_NOT:
10569 case UNOP_COMPLEMENT:
10570 case UNOP_ADDR:
10571 case UNOP_HIGH:
10572 case UNOP_CAST:
10573
10574 case UNOP_CAST_TYPE:
10575 case UNOP_REINTERPRET_CAST:
10576 case UNOP_DYNAMIC_CAST:
10577 /* Unary, binary and ternary operators: We have to check
10578 their operands. If they are constant, then so is the
10579 result of that operation. For instance, if A and B are
10580 determined to be constants, then so is "A + B".
10581
10582 UNOP_IND is one exception to the rule above, because the
10583 value of *ADDR is not necessarily a constant, even when
10584 ADDR is. */
10585 break;
10586
10587 case OP_VAR_VALUE:
10588 /* Check whether the associated symbol is a constant.
10589
10590 We use SYMBOL_CLASS rather than TYPE_CONST because it's
10591 possible that a buggy compiler could mark a variable as
10592 constant even when it is not, and TYPE_CONST would return
10593 true in this case, while SYMBOL_CLASS wouldn't.
10594
10595 We also have to check for function symbols because they
10596 are always constant. */
10597 {
10598 struct symbol *s = exp->elts[i + 2].symbol;
10599
10600 if (SYMBOL_CLASS (s) != LOC_BLOCK
10601 && SYMBOL_CLASS (s) != LOC_CONST
10602 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10603 return 0;
10604 break;
10605 }
10606
10607 /* The default action is to return 0 because we are using
10608 the optimistic approach here: If we don't know something,
10609 then it is not a constant. */
10610 default:
10611 return 0;
10612 }
10613 }
10614
10615 return 1;
10616 }
10617
10618 /* Implement the "dtor" breakpoint_ops method for watchpoints. */
10619
10620 static void
10621 dtor_watchpoint (struct breakpoint *self)
10622 {
10623 struct watchpoint *w = (struct watchpoint *) self;
10624
10625 xfree (w->cond_exp);
10626 xfree (w->exp);
10627 xfree (w->exp_string);
10628 xfree (w->exp_string_reparse);
10629 value_free (w->val);
10630
10631 base_breakpoint_ops.dtor (self);
10632 }
10633
10634 /* Implement the "re_set" breakpoint_ops method for watchpoints. */
10635
10636 static void
10637 re_set_watchpoint (struct breakpoint *b)
10638 {
10639 struct watchpoint *w = (struct watchpoint *) b;
10640
10641 /* Watchpoint can be either on expression using entirely global
10642 variables, or it can be on local variables.
10643
10644 Watchpoints of the first kind are never auto-deleted, and even
10645 persist across program restarts. Since they can use variables
10646 from shared libraries, we need to reparse expression as libraries
10647 are loaded and unloaded.
10648
10649 Watchpoints on local variables can also change meaning as result
10650 of solib event. For example, if a watchpoint uses both a local
10651 and a global variables in expression, it's a local watchpoint,
10652 but unloading of a shared library will make the expression
10653 invalid. This is not a very common use case, but we still
10654 re-evaluate expression, to avoid surprises to the user.
10655
10656 Note that for local watchpoints, we re-evaluate it only if
10657 watchpoints frame id is still valid. If it's not, it means the
10658 watchpoint is out of scope and will be deleted soon. In fact,
10659 I'm not sure we'll ever be called in this case.
10660
10661 If a local watchpoint's frame id is still valid, then
10662 w->exp_valid_block is likewise valid, and we can safely use it.
10663
10664 Don't do anything about disabled watchpoints, since they will be
10665 reevaluated again when enabled. */
10666 update_watchpoint (w, 1 /* reparse */);
10667 }
10668
10669 /* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10670
10671 static int
10672 insert_watchpoint (struct bp_location *bl)
10673 {
10674 struct watchpoint *w = (struct watchpoint *) bl->owner;
10675 int length = w->exact ? 1 : bl->length;
10676
10677 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
10678 w->cond_exp);
10679 }
10680
10681 /* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10682
10683 static int
10684 remove_watchpoint (struct bp_location *bl)
10685 {
10686 struct watchpoint *w = (struct watchpoint *) bl->owner;
10687 int length = w->exact ? 1 : bl->length;
10688
10689 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
10690 w->cond_exp);
10691 }
10692
10693 static int
10694 breakpoint_hit_watchpoint (const struct bp_location *bl,
10695 struct address_space *aspace, CORE_ADDR bp_addr,
10696 const struct target_waitstatus *ws)
10697 {
10698 struct breakpoint *b = bl->owner;
10699 struct watchpoint *w = (struct watchpoint *) b;
10700
10701 /* Continuable hardware watchpoints are treated as non-existent if the
10702 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10703 some data address). Otherwise gdb won't stop on a break instruction
10704 in the code (not from a breakpoint) when a hardware watchpoint has
10705 been defined. Also skip watchpoints which we know did not trigger
10706 (did not match the data address). */
10707 if (is_hardware_watchpoint (b)
10708 && w->watchpoint_triggered == watch_triggered_no)
10709 return 0;
10710
10711 return 1;
10712 }
10713
10714 static void
10715 check_status_watchpoint (bpstat bs)
10716 {
10717 gdb_assert (is_watchpoint (bs->breakpoint_at));
10718
10719 bpstat_check_watchpoint (bs);
10720 }
10721
10722 /* Implement the "resources_needed" breakpoint_ops method for
10723 hardware watchpoints. */
10724
10725 static int
10726 resources_needed_watchpoint (const struct bp_location *bl)
10727 {
10728 struct watchpoint *w = (struct watchpoint *) bl->owner;
10729 int length = w->exact? 1 : bl->length;
10730
10731 return target_region_ok_for_hw_watchpoint (bl->address, length);
10732 }
10733
10734 /* Implement the "works_in_software_mode" breakpoint_ops method for
10735 hardware watchpoints. */
10736
10737 static int
10738 works_in_software_mode_watchpoint (const struct breakpoint *b)
10739 {
10740 /* Read and access watchpoints only work with hardware support. */
10741 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
10742 }
10743
10744 static enum print_stop_action
10745 print_it_watchpoint (bpstat bs)
10746 {
10747 struct cleanup *old_chain;
10748 struct breakpoint *b;
10749 struct ui_file *stb;
10750 enum print_stop_action result;
10751 struct watchpoint *w;
10752 struct ui_out *uiout = current_uiout;
10753
10754 gdb_assert (bs->bp_location_at != NULL);
10755
10756 b = bs->breakpoint_at;
10757 w = (struct watchpoint *) b;
10758
10759 stb = mem_fileopen ();
10760 old_chain = make_cleanup_ui_file_delete (stb);
10761
10762 annotate_watchpoint (b->number);
10763 maybe_print_thread_hit_breakpoint (uiout);
10764
10765 switch (b->type)
10766 {
10767 case bp_watchpoint:
10768 case bp_hardware_watchpoint:
10769 if (ui_out_is_mi_like_p (uiout))
10770 ui_out_field_string
10771 (uiout, "reason",
10772 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10773 mention (b);
10774 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10775 ui_out_text (uiout, "\nOld value = ");
10776 watchpoint_value_print (bs->old_val, stb);
10777 ui_out_field_stream (uiout, "old", stb);
10778 ui_out_text (uiout, "\nNew value = ");
10779 watchpoint_value_print (w->val, stb);
10780 ui_out_field_stream (uiout, "new", stb);
10781 ui_out_text (uiout, "\n");
10782 /* More than one watchpoint may have been triggered. */
10783 result = PRINT_UNKNOWN;
10784 break;
10785
10786 case bp_read_watchpoint:
10787 if (ui_out_is_mi_like_p (uiout))
10788 ui_out_field_string
10789 (uiout, "reason",
10790 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10791 mention (b);
10792 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10793 ui_out_text (uiout, "\nValue = ");
10794 watchpoint_value_print (w->val, stb);
10795 ui_out_field_stream (uiout, "value", stb);
10796 ui_out_text (uiout, "\n");
10797 result = PRINT_UNKNOWN;
10798 break;
10799
10800 case bp_access_watchpoint:
10801 if (bs->old_val != NULL)
10802 {
10803 if (ui_out_is_mi_like_p (uiout))
10804 ui_out_field_string
10805 (uiout, "reason",
10806 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10807 mention (b);
10808 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10809 ui_out_text (uiout, "\nOld value = ");
10810 watchpoint_value_print (bs->old_val, stb);
10811 ui_out_field_stream (uiout, "old", stb);
10812 ui_out_text (uiout, "\nNew value = ");
10813 }
10814 else
10815 {
10816 mention (b);
10817 if (ui_out_is_mi_like_p (uiout))
10818 ui_out_field_string
10819 (uiout, "reason",
10820 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10821 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10822 ui_out_text (uiout, "\nValue = ");
10823 }
10824 watchpoint_value_print (w->val, stb);
10825 ui_out_field_stream (uiout, "new", stb);
10826 ui_out_text (uiout, "\n");
10827 result = PRINT_UNKNOWN;
10828 break;
10829 default:
10830 result = PRINT_UNKNOWN;
10831 }
10832
10833 do_cleanups (old_chain);
10834 return result;
10835 }
10836
10837 /* Implement the "print_mention" breakpoint_ops method for hardware
10838 watchpoints. */
10839
10840 static void
10841 print_mention_watchpoint (struct breakpoint *b)
10842 {
10843 struct cleanup *ui_out_chain;
10844 struct watchpoint *w = (struct watchpoint *) b;
10845 struct ui_out *uiout = current_uiout;
10846
10847 switch (b->type)
10848 {
10849 case bp_watchpoint:
10850 ui_out_text (uiout, "Watchpoint ");
10851 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10852 break;
10853 case bp_hardware_watchpoint:
10854 ui_out_text (uiout, "Hardware watchpoint ");
10855 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10856 break;
10857 case bp_read_watchpoint:
10858 ui_out_text (uiout, "Hardware read watchpoint ");
10859 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
10860 break;
10861 case bp_access_watchpoint:
10862 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
10863 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
10864 break;
10865 default:
10866 internal_error (__FILE__, __LINE__,
10867 _("Invalid hardware watchpoint type."));
10868 }
10869
10870 ui_out_field_int (uiout, "number", b->number);
10871 ui_out_text (uiout, ": ");
10872 ui_out_field_string (uiout, "exp", w->exp_string);
10873 do_cleanups (ui_out_chain);
10874 }
10875
10876 /* Implement the "print_recreate" breakpoint_ops method for
10877 watchpoints. */
10878
10879 static void
10880 print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10881 {
10882 struct watchpoint *w = (struct watchpoint *) b;
10883
10884 switch (b->type)
10885 {
10886 case bp_watchpoint:
10887 case bp_hardware_watchpoint:
10888 fprintf_unfiltered (fp, "watch");
10889 break;
10890 case bp_read_watchpoint:
10891 fprintf_unfiltered (fp, "rwatch");
10892 break;
10893 case bp_access_watchpoint:
10894 fprintf_unfiltered (fp, "awatch");
10895 break;
10896 default:
10897 internal_error (__FILE__, __LINE__,
10898 _("Invalid watchpoint type."));
10899 }
10900
10901 fprintf_unfiltered (fp, " %s", w->exp_string);
10902 print_recreate_thread (b, fp);
10903 }
10904
10905 /* Implement the "explains_signal" breakpoint_ops method for
10906 watchpoints. */
10907
10908 static int
10909 explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10910 {
10911 /* A software watchpoint cannot cause a signal other than
10912 GDB_SIGNAL_TRAP. */
10913 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
10914 return 0;
10915
10916 return 1;
10917 }
10918
10919 /* The breakpoint_ops structure to be used in hardware watchpoints. */
10920
10921 static struct breakpoint_ops watchpoint_breakpoint_ops;
10922
10923 /* Implement the "insert" breakpoint_ops method for
10924 masked hardware watchpoints. */
10925
10926 static int
10927 insert_masked_watchpoint (struct bp_location *bl)
10928 {
10929 struct watchpoint *w = (struct watchpoint *) bl->owner;
10930
10931 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
10932 bl->watchpoint_type);
10933 }
10934
10935 /* Implement the "remove" breakpoint_ops method for
10936 masked hardware watchpoints. */
10937
10938 static int
10939 remove_masked_watchpoint (struct bp_location *bl)
10940 {
10941 struct watchpoint *w = (struct watchpoint *) bl->owner;
10942
10943 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
10944 bl->watchpoint_type);
10945 }
10946
10947 /* Implement the "resources_needed" breakpoint_ops method for
10948 masked hardware watchpoints. */
10949
10950 static int
10951 resources_needed_masked_watchpoint (const struct bp_location *bl)
10952 {
10953 struct watchpoint *w = (struct watchpoint *) bl->owner;
10954
10955 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
10956 }
10957
10958 /* Implement the "works_in_software_mode" breakpoint_ops method for
10959 masked hardware watchpoints. */
10960
10961 static int
10962 works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10963 {
10964 return 0;
10965 }
10966
10967 /* Implement the "print_it" breakpoint_ops method for
10968 masked hardware watchpoints. */
10969
10970 static enum print_stop_action
10971 print_it_masked_watchpoint (bpstat bs)
10972 {
10973 struct breakpoint *b = bs->breakpoint_at;
10974 struct ui_out *uiout = current_uiout;
10975
10976 /* Masked watchpoints have only one location. */
10977 gdb_assert (b->loc && b->loc->next == NULL);
10978
10979 annotate_watchpoint (b->number);
10980 maybe_print_thread_hit_breakpoint (uiout);
10981
10982 switch (b->type)
10983 {
10984 case bp_hardware_watchpoint:
10985 if (ui_out_is_mi_like_p (uiout))
10986 ui_out_field_string
10987 (uiout, "reason",
10988 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10989 break;
10990
10991 case bp_read_watchpoint:
10992 if (ui_out_is_mi_like_p (uiout))
10993 ui_out_field_string
10994 (uiout, "reason",
10995 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10996 break;
10997
10998 case bp_access_watchpoint:
10999 if (ui_out_is_mi_like_p (uiout))
11000 ui_out_field_string
11001 (uiout, "reason",
11002 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
11003 break;
11004 default:
11005 internal_error (__FILE__, __LINE__,
11006 _("Invalid hardware watchpoint type."));
11007 }
11008
11009 mention (b);
11010 ui_out_text (uiout, _("\n\
11011 Check the underlying instruction at PC for the memory\n\
11012 address and value which triggered this watchpoint.\n"));
11013 ui_out_text (uiout, "\n");
11014
11015 /* More than one watchpoint may have been triggered. */
11016 return PRINT_UNKNOWN;
11017 }
11018
11019 /* Implement the "print_one_detail" breakpoint_ops method for
11020 masked hardware watchpoints. */
11021
11022 static void
11023 print_one_detail_masked_watchpoint (const struct breakpoint *b,
11024 struct ui_out *uiout)
11025 {
11026 struct watchpoint *w = (struct watchpoint *) b;
11027
11028 /* Masked watchpoints have only one location. */
11029 gdb_assert (b->loc && b->loc->next == NULL);
11030
11031 ui_out_text (uiout, "\tmask ");
11032 ui_out_field_core_addr (uiout, "mask", b->loc->gdbarch, w->hw_wp_mask);
11033 ui_out_text (uiout, "\n");
11034 }
11035
11036 /* Implement the "print_mention" breakpoint_ops method for
11037 masked hardware watchpoints. */
11038
11039 static void
11040 print_mention_masked_watchpoint (struct breakpoint *b)
11041 {
11042 struct watchpoint *w = (struct watchpoint *) b;
11043 struct ui_out *uiout = current_uiout;
11044 struct cleanup *ui_out_chain;
11045
11046 switch (b->type)
11047 {
11048 case bp_hardware_watchpoint:
11049 ui_out_text (uiout, "Masked hardware watchpoint ");
11050 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
11051 break;
11052 case bp_read_watchpoint:
11053 ui_out_text (uiout, "Masked hardware read watchpoint ");
11054 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
11055 break;
11056 case bp_access_watchpoint:
11057 ui_out_text (uiout, "Masked hardware access (read/write) watchpoint ");
11058 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
11059 break;
11060 default:
11061 internal_error (__FILE__, __LINE__,
11062 _("Invalid hardware watchpoint type."));
11063 }
11064
11065 ui_out_field_int (uiout, "number", b->number);
11066 ui_out_text (uiout, ": ");
11067 ui_out_field_string (uiout, "exp", w->exp_string);
11068 do_cleanups (ui_out_chain);
11069 }
11070
11071 /* Implement the "print_recreate" breakpoint_ops method for
11072 masked hardware watchpoints. */
11073
11074 static void
11075 print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
11076 {
11077 struct watchpoint *w = (struct watchpoint *) b;
11078 char tmp[40];
11079
11080 switch (b->type)
11081 {
11082 case bp_hardware_watchpoint:
11083 fprintf_unfiltered (fp, "watch");
11084 break;
11085 case bp_read_watchpoint:
11086 fprintf_unfiltered (fp, "rwatch");
11087 break;
11088 case bp_access_watchpoint:
11089 fprintf_unfiltered (fp, "awatch");
11090 break;
11091 default:
11092 internal_error (__FILE__, __LINE__,
11093 _("Invalid hardware watchpoint type."));
11094 }
11095
11096 sprintf_vma (tmp, w->hw_wp_mask);
11097 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
11098 print_recreate_thread (b, fp);
11099 }
11100
11101 /* The breakpoint_ops structure to be used in masked hardware watchpoints. */
11102
11103 static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
11104
11105 /* Tell whether the given watchpoint is a masked hardware watchpoint. */
11106
11107 static int
11108 is_masked_watchpoint (const struct breakpoint *b)
11109 {
11110 return b->ops == &masked_watchpoint_breakpoint_ops;
11111 }
11112
11113 /* accessflag: hw_write: watch write,
11114 hw_read: watch read,
11115 hw_access: watch access (read or write) */
11116 static void
11117 watch_command_1 (const char *arg, int accessflag, int from_tty,
11118 int just_location, int internal)
11119 {
11120 struct breakpoint *b, *scope_breakpoint = NULL;
11121 struct expression *exp;
11122 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
11123 struct value *val, *mark, *result;
11124 int saved_bitpos = 0, saved_bitsize = 0;
11125 struct frame_info *frame;
11126 const char *exp_start = NULL;
11127 const char *exp_end = NULL;
11128 const char *tok, *end_tok;
11129 int toklen = -1;
11130 const char *cond_start = NULL;
11131 const char *cond_end = NULL;
11132 enum bptype bp_type;
11133 int thread = -1;
11134 int pc = 0;
11135 /* Flag to indicate whether we are going to use masks for
11136 the hardware watchpoint. */
11137 int use_mask = 0;
11138 CORE_ADDR mask = 0;
11139 struct watchpoint *w;
11140 char *expression;
11141 struct cleanup *back_to;
11142
11143 /* Make sure that we actually have parameters to parse. */
11144 if (arg != NULL && arg[0] != '\0')
11145 {
11146 const char *value_start;
11147
11148 exp_end = arg + strlen (arg);
11149
11150 /* Look for "parameter value" pairs at the end
11151 of the arguments string. */
11152 for (tok = exp_end - 1; tok > arg; tok--)
11153 {
11154 /* Skip whitespace at the end of the argument list. */
11155 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11156 tok--;
11157
11158 /* Find the beginning of the last token.
11159 This is the value of the parameter. */
11160 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11161 tok--;
11162 value_start = tok + 1;
11163
11164 /* Skip whitespace. */
11165 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11166 tok--;
11167
11168 end_tok = tok;
11169
11170 /* Find the beginning of the second to last token.
11171 This is the parameter itself. */
11172 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11173 tok--;
11174 tok++;
11175 toklen = end_tok - tok + 1;
11176
11177 if (toklen == 6 && startswith (tok, "thread"))
11178 {
11179 struct thread_info *thr;
11180 /* At this point we've found a "thread" token, which means
11181 the user is trying to set a watchpoint that triggers
11182 only in a specific thread. */
11183 const char *endp;
11184
11185 if (thread != -1)
11186 error(_("You can specify only one thread."));
11187
11188 /* Extract the thread ID from the next token. */
11189 thr = parse_thread_id (value_start, &endp);
11190
11191 /* Check if the user provided a valid thread ID. */
11192 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
11193 invalid_thread_id_error (value_start);
11194
11195 thread = thr->global_num;
11196 }
11197 else if (toklen == 4 && startswith (tok, "mask"))
11198 {
11199 /* We've found a "mask" token, which means the user wants to
11200 create a hardware watchpoint that is going to have the mask
11201 facility. */
11202 struct value *mask_value, *mark;
11203
11204 if (use_mask)
11205 error(_("You can specify only one mask."));
11206
11207 use_mask = just_location = 1;
11208
11209 mark = value_mark ();
11210 mask_value = parse_to_comma_and_eval (&value_start);
11211 mask = value_as_address (mask_value);
11212 value_free_to_mark (mark);
11213 }
11214 else
11215 /* We didn't recognize what we found. We should stop here. */
11216 break;
11217
11218 /* Truncate the string and get rid of the "parameter value" pair before
11219 the arguments string is parsed by the parse_exp_1 function. */
11220 exp_end = tok;
11221 }
11222 }
11223 else
11224 exp_end = arg;
11225
11226 /* Parse the rest of the arguments. From here on out, everything
11227 is in terms of a newly allocated string instead of the original
11228 ARG. */
11229 innermost_block = NULL;
11230 expression = savestring (arg, exp_end - arg);
11231 back_to = make_cleanup (xfree, expression);
11232 exp_start = arg = expression;
11233 exp = parse_exp_1 (&arg, 0, 0, 0);
11234 exp_end = arg;
11235 /* Remove trailing whitespace from the expression before saving it.
11236 This makes the eventual display of the expression string a bit
11237 prettier. */
11238 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
11239 --exp_end;
11240
11241 /* Checking if the expression is not constant. */
11242 if (watchpoint_exp_is_const (exp))
11243 {
11244 int len;
11245
11246 len = exp_end - exp_start;
11247 while (len > 0 && isspace (exp_start[len - 1]))
11248 len--;
11249 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
11250 }
11251
11252 exp_valid_block = innermost_block;
11253 mark = value_mark ();
11254 fetch_subexp_value (exp, &pc, &val, &result, NULL, just_location);
11255
11256 if (val != NULL && just_location)
11257 {
11258 saved_bitpos = value_bitpos (val);
11259 saved_bitsize = value_bitsize (val);
11260 }
11261
11262 if (just_location)
11263 {
11264 int ret;
11265
11266 exp_valid_block = NULL;
11267 val = value_addr (result);
11268 release_value (val);
11269 value_free_to_mark (mark);
11270
11271 if (use_mask)
11272 {
11273 ret = target_masked_watch_num_registers (value_as_address (val),
11274 mask);
11275 if (ret == -1)
11276 error (_("This target does not support masked watchpoints."));
11277 else if (ret == -2)
11278 error (_("Invalid mask or memory region."));
11279 }
11280 }
11281 else if (val != NULL)
11282 release_value (val);
11283
11284 tok = skip_spaces_const (arg);
11285 end_tok = skip_to_space_const (tok);
11286
11287 toklen = end_tok - tok;
11288 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
11289 {
11290 struct expression *cond;
11291
11292 innermost_block = NULL;
11293 tok = cond_start = end_tok + 1;
11294 cond = parse_exp_1 (&tok, 0, 0, 0);
11295
11296 /* The watchpoint expression may not be local, but the condition
11297 may still be. E.g.: `watch global if local > 0'. */
11298 cond_exp_valid_block = innermost_block;
11299
11300 xfree (cond);
11301 cond_end = tok;
11302 }
11303 if (*tok)
11304 error (_("Junk at end of command."));
11305
11306 frame = block_innermost_frame (exp_valid_block);
11307
11308 /* If the expression is "local", then set up a "watchpoint scope"
11309 breakpoint at the point where we've left the scope of the watchpoint
11310 expression. Create the scope breakpoint before the watchpoint, so
11311 that we will encounter it first in bpstat_stop_status. */
11312 if (exp_valid_block && frame)
11313 {
11314 if (frame_id_p (frame_unwind_caller_id (frame)))
11315 {
11316 scope_breakpoint
11317 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
11318 frame_unwind_caller_pc (frame),
11319 bp_watchpoint_scope,
11320 &momentary_breakpoint_ops);
11321
11322 scope_breakpoint->enable_state = bp_enabled;
11323
11324 /* Automatically delete the breakpoint when it hits. */
11325 scope_breakpoint->disposition = disp_del;
11326
11327 /* Only break in the proper frame (help with recursion). */
11328 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
11329
11330 /* Set the address at which we will stop. */
11331 scope_breakpoint->loc->gdbarch
11332 = frame_unwind_caller_arch (frame);
11333 scope_breakpoint->loc->requested_address
11334 = frame_unwind_caller_pc (frame);
11335 scope_breakpoint->loc->address
11336 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
11337 scope_breakpoint->loc->requested_address,
11338 scope_breakpoint->type);
11339 }
11340 }
11341
11342 /* Now set up the breakpoint. We create all watchpoints as hardware
11343 watchpoints here even if hardware watchpoints are turned off, a call
11344 to update_watchpoint later in this function will cause the type to
11345 drop back to bp_watchpoint (software watchpoint) if required. */
11346
11347 if (accessflag == hw_read)
11348 bp_type = bp_read_watchpoint;
11349 else if (accessflag == hw_access)
11350 bp_type = bp_access_watchpoint;
11351 else
11352 bp_type = bp_hardware_watchpoint;
11353
11354 w = XCNEW (struct watchpoint);
11355 b = &w->base;
11356 if (use_mask)
11357 init_raw_breakpoint_without_location (b, NULL, bp_type,
11358 &masked_watchpoint_breakpoint_ops);
11359 else
11360 init_raw_breakpoint_without_location (b, NULL, bp_type,
11361 &watchpoint_breakpoint_ops);
11362 b->thread = thread;
11363 b->disposition = disp_donttouch;
11364 b->pspace = current_program_space;
11365 w->exp = exp;
11366 w->exp_valid_block = exp_valid_block;
11367 w->cond_exp_valid_block = cond_exp_valid_block;
11368 if (just_location)
11369 {
11370 struct type *t = value_type (val);
11371 CORE_ADDR addr = value_as_address (val);
11372 char *name;
11373
11374 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
11375 name = type_to_string (t);
11376
11377 w->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
11378 core_addr_to_string (addr));
11379 xfree (name);
11380
11381 w->exp_string = xstrprintf ("-location %.*s",
11382 (int) (exp_end - exp_start), exp_start);
11383
11384 /* The above expression is in C. */
11385 b->language = language_c;
11386 }
11387 else
11388 w->exp_string = savestring (exp_start, exp_end - exp_start);
11389
11390 if (use_mask)
11391 {
11392 w->hw_wp_mask = mask;
11393 }
11394 else
11395 {
11396 w->val = val;
11397 w->val_bitpos = saved_bitpos;
11398 w->val_bitsize = saved_bitsize;
11399 w->val_valid = 1;
11400 }
11401
11402 if (cond_start)
11403 b->cond_string = savestring (cond_start, cond_end - cond_start);
11404 else
11405 b->cond_string = 0;
11406
11407 if (frame)
11408 {
11409 w->watchpoint_frame = get_frame_id (frame);
11410 w->watchpoint_thread = inferior_ptid;
11411 }
11412 else
11413 {
11414 w->watchpoint_frame = null_frame_id;
11415 w->watchpoint_thread = null_ptid;
11416 }
11417
11418 if (scope_breakpoint != NULL)
11419 {
11420 /* The scope breakpoint is related to the watchpoint. We will
11421 need to act on them together. */
11422 b->related_breakpoint = scope_breakpoint;
11423 scope_breakpoint->related_breakpoint = b;
11424 }
11425
11426 if (!just_location)
11427 value_free_to_mark (mark);
11428
11429 TRY
11430 {
11431 /* Finally update the new watchpoint. This creates the locations
11432 that should be inserted. */
11433 update_watchpoint (w, 1);
11434 }
11435 CATCH (e, RETURN_MASK_ALL)
11436 {
11437 delete_breakpoint (b);
11438 throw_exception (e);
11439 }
11440 END_CATCH
11441
11442 install_breakpoint (internal, b, 1);
11443 do_cleanups (back_to);
11444 }
11445
11446 /* Return count of debug registers needed to watch the given expression.
11447 If the watchpoint cannot be handled in hardware return zero. */
11448
11449 static int
11450 can_use_hardware_watchpoint (struct value *v)
11451 {
11452 int found_memory_cnt = 0;
11453 struct value *head = v;
11454
11455 /* Did the user specifically forbid us to use hardware watchpoints? */
11456 if (!can_use_hw_watchpoints)
11457 return 0;
11458
11459 /* Make sure that the value of the expression depends only upon
11460 memory contents, and values computed from them within GDB. If we
11461 find any register references or function calls, we can't use a
11462 hardware watchpoint.
11463
11464 The idea here is that evaluating an expression generates a series
11465 of values, one holding the value of every subexpression. (The
11466 expression a*b+c has five subexpressions: a, b, a*b, c, and
11467 a*b+c.) GDB's values hold almost enough information to establish
11468 the criteria given above --- they identify memory lvalues,
11469 register lvalues, computed values, etcetera. So we can evaluate
11470 the expression, and then scan the chain of values that leaves
11471 behind to decide whether we can detect any possible change to the
11472 expression's final value using only hardware watchpoints.
11473
11474 However, I don't think that the values returned by inferior
11475 function calls are special in any way. So this function may not
11476 notice that an expression involving an inferior function call
11477 can't be watched with hardware watchpoints. FIXME. */
11478 for (; v; v = value_next (v))
11479 {
11480 if (VALUE_LVAL (v) == lval_memory)
11481 {
11482 if (v != head && value_lazy (v))
11483 /* A lazy memory lvalue in the chain is one that GDB never
11484 needed to fetch; we either just used its address (e.g.,
11485 `a' in `a.b') or we never needed it at all (e.g., `a'
11486 in `a,b'). This doesn't apply to HEAD; if that is
11487 lazy then it was not readable, but watch it anyway. */
11488 ;
11489 else
11490 {
11491 /* Ahh, memory we actually used! Check if we can cover
11492 it with hardware watchpoints. */
11493 struct type *vtype = check_typedef (value_type (v));
11494
11495 /* We only watch structs and arrays if user asked for it
11496 explicitly, never if they just happen to appear in a
11497 middle of some value chain. */
11498 if (v == head
11499 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11500 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11501 {
11502 CORE_ADDR vaddr = value_address (v);
11503 int len;
11504 int num_regs;
11505
11506 len = (target_exact_watchpoints
11507 && is_scalar_type_recursive (vtype))?
11508 1 : TYPE_LENGTH (value_type (v));
11509
11510 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11511 if (!num_regs)
11512 return 0;
11513 else
11514 found_memory_cnt += num_regs;
11515 }
11516 }
11517 }
11518 else if (VALUE_LVAL (v) != not_lval
11519 && deprecated_value_modifiable (v) == 0)
11520 return 0; /* These are values from the history (e.g., $1). */
11521 else if (VALUE_LVAL (v) == lval_register)
11522 return 0; /* Cannot watch a register with a HW watchpoint. */
11523 }
11524
11525 /* The expression itself looks suitable for using a hardware
11526 watchpoint, but give the target machine a chance to reject it. */
11527 return found_memory_cnt;
11528 }
11529
11530 void
11531 watch_command_wrapper (char *arg, int from_tty, int internal)
11532 {
11533 watch_command_1 (arg, hw_write, from_tty, 0, internal);
11534 }
11535
11536 /* A helper function that looks for the "-location" argument and then
11537 calls watch_command_1. */
11538
11539 static void
11540 watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11541 {
11542 int just_location = 0;
11543
11544 if (arg
11545 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11546 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11547 {
11548 arg = skip_spaces (arg);
11549 just_location = 1;
11550 }
11551
11552 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
11553 }
11554
11555 static void
11556 watch_command (char *arg, int from_tty)
11557 {
11558 watch_maybe_just_location (arg, hw_write, from_tty);
11559 }
11560
11561 void
11562 rwatch_command_wrapper (char *arg, int from_tty, int internal)
11563 {
11564 watch_command_1 (arg, hw_read, from_tty, 0, internal);
11565 }
11566
11567 static void
11568 rwatch_command (char *arg, int from_tty)
11569 {
11570 watch_maybe_just_location (arg, hw_read, from_tty);
11571 }
11572
11573 void
11574 awatch_command_wrapper (char *arg, int from_tty, int internal)
11575 {
11576 watch_command_1 (arg, hw_access, from_tty, 0, internal);
11577 }
11578
11579 static void
11580 awatch_command (char *arg, int from_tty)
11581 {
11582 watch_maybe_just_location (arg, hw_access, from_tty);
11583 }
11584 \f
11585
11586 /* Data for the FSM that manages the until(location)/advance commands
11587 in infcmd.c. Here because it uses the mechanisms of
11588 breakpoints. */
11589
11590 struct until_break_fsm
11591 {
11592 /* The base class. */
11593 struct thread_fsm thread_fsm;
11594
11595 /* The thread that as current when the command was executed. */
11596 int thread;
11597
11598 /* The breakpoint set at the destination location. */
11599 struct breakpoint *location_breakpoint;
11600
11601 /* Breakpoint set at the return address in the caller frame. May be
11602 NULL. */
11603 struct breakpoint *caller_breakpoint;
11604 };
11605
11606 static void until_break_fsm_clean_up (struct thread_fsm *self);
11607 static int until_break_fsm_should_stop (struct thread_fsm *self);
11608 static enum async_reply_reason
11609 until_break_fsm_async_reply_reason (struct thread_fsm *self);
11610
11611 /* until_break_fsm's vtable. */
11612
11613 static struct thread_fsm_ops until_break_fsm_ops =
11614 {
11615 NULL, /* dtor */
11616 until_break_fsm_clean_up,
11617 until_break_fsm_should_stop,
11618 NULL, /* return_value */
11619 until_break_fsm_async_reply_reason,
11620 };
11621
11622 /* Allocate a new until_break_command_fsm. */
11623
11624 static struct until_break_fsm *
11625 new_until_break_fsm (int thread,
11626 struct breakpoint *location_breakpoint,
11627 struct breakpoint *caller_breakpoint)
11628 {
11629 struct until_break_fsm *sm;
11630
11631 sm = XCNEW (struct until_break_fsm);
11632 thread_fsm_ctor (&sm->thread_fsm, &until_break_fsm_ops);
11633
11634 sm->thread = thread;
11635 sm->location_breakpoint = location_breakpoint;
11636 sm->caller_breakpoint = caller_breakpoint;
11637
11638 return sm;
11639 }
11640
11641 /* Implementation of the 'should_stop' FSM method for the
11642 until(location)/advance commands. */
11643
11644 static int
11645 until_break_fsm_should_stop (struct thread_fsm *self)
11646 {
11647 struct until_break_fsm *sm = (struct until_break_fsm *) self;
11648 struct thread_info *tp = inferior_thread ();
11649
11650 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
11651 sm->location_breakpoint) != NULL
11652 || (sm->caller_breakpoint != NULL
11653 && bpstat_find_breakpoint (tp->control.stop_bpstat,
11654 sm->caller_breakpoint) != NULL))
11655 thread_fsm_set_finished (self);
11656
11657 return 1;
11658 }
11659
11660 /* Implementation of the 'clean_up' FSM method for the
11661 until(location)/advance commands. */
11662
11663 static void
11664 until_break_fsm_clean_up (struct thread_fsm *self)
11665 {
11666 struct until_break_fsm *sm = (struct until_break_fsm *) self;
11667
11668 /* Clean up our temporary breakpoints. */
11669 if (sm->location_breakpoint != NULL)
11670 {
11671 delete_breakpoint (sm->location_breakpoint);
11672 sm->location_breakpoint = NULL;
11673 }
11674 if (sm->caller_breakpoint != NULL)
11675 {
11676 delete_breakpoint (sm->caller_breakpoint);
11677 sm->caller_breakpoint = NULL;
11678 }
11679 delete_longjmp_breakpoint (sm->thread);
11680 }
11681
11682 /* Implementation of the 'async_reply_reason' FSM method for the
11683 until(location)/advance commands. */
11684
11685 static enum async_reply_reason
11686 until_break_fsm_async_reply_reason (struct thread_fsm *self)
11687 {
11688 return EXEC_ASYNC_LOCATION_REACHED;
11689 }
11690
11691 void
11692 until_break_command (char *arg, int from_tty, int anywhere)
11693 {
11694 struct symtabs_and_lines sals;
11695 struct symtab_and_line sal;
11696 struct frame_info *frame;
11697 struct gdbarch *frame_gdbarch;
11698 struct frame_id stack_frame_id;
11699 struct frame_id caller_frame_id;
11700 struct breakpoint *location_breakpoint;
11701 struct breakpoint *caller_breakpoint = NULL;
11702 struct cleanup *old_chain, *cleanup;
11703 int thread;
11704 struct thread_info *tp;
11705 struct event_location *location;
11706 struct until_break_fsm *sm;
11707
11708 clear_proceed_status (0);
11709
11710 /* Set a breakpoint where the user wants it and at return from
11711 this function. */
11712
11713 location = string_to_event_location (&arg, current_language);
11714 cleanup = make_cleanup_delete_event_location (location);
11715
11716 if (last_displayed_sal_is_valid ())
11717 sals = decode_line_1 (location, DECODE_LINE_FUNFIRSTLINE, NULL,
11718 get_last_displayed_symtab (),
11719 get_last_displayed_line ());
11720 else
11721 sals = decode_line_1 (location, DECODE_LINE_FUNFIRSTLINE,
11722 NULL, (struct symtab *) NULL, 0);
11723
11724 if (sals.nelts != 1)
11725 error (_("Couldn't get information on specified line."));
11726
11727 sal = sals.sals[0];
11728 xfree (sals.sals); /* malloc'd, so freed. */
11729
11730 if (*arg)
11731 error (_("Junk at end of arguments."));
11732
11733 resolve_sal_pc (&sal);
11734
11735 tp = inferior_thread ();
11736 thread = tp->global_num;
11737
11738 old_chain = make_cleanup (null_cleanup, NULL);
11739
11740 /* Note linespec handling above invalidates the frame chain.
11741 Installing a breakpoint also invalidates the frame chain (as it
11742 may need to switch threads), so do any frame handling before
11743 that. */
11744
11745 frame = get_selected_frame (NULL);
11746 frame_gdbarch = get_frame_arch (frame);
11747 stack_frame_id = get_stack_frame_id (frame);
11748 caller_frame_id = frame_unwind_caller_id (frame);
11749
11750 /* Keep within the current frame, or in frames called by the current
11751 one. */
11752
11753 if (frame_id_p (caller_frame_id))
11754 {
11755 struct symtab_and_line sal2;
11756 struct gdbarch *caller_gdbarch;
11757
11758 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11759 sal2.pc = frame_unwind_caller_pc (frame);
11760 caller_gdbarch = frame_unwind_caller_arch (frame);
11761 caller_breakpoint = set_momentary_breakpoint (caller_gdbarch,
11762 sal2,
11763 caller_frame_id,
11764 bp_until);
11765 make_cleanup_delete_breakpoint (caller_breakpoint);
11766
11767 set_longjmp_breakpoint (tp, caller_frame_id);
11768 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
11769 }
11770
11771 /* set_momentary_breakpoint could invalidate FRAME. */
11772 frame = NULL;
11773
11774 if (anywhere)
11775 /* If the user told us to continue until a specified location,
11776 we don't specify a frame at which we need to stop. */
11777 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11778 null_frame_id, bp_until);
11779 else
11780 /* Otherwise, specify the selected frame, because we want to stop
11781 only at the very same frame. */
11782 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11783 stack_frame_id, bp_until);
11784 make_cleanup_delete_breakpoint (location_breakpoint);
11785
11786 sm = new_until_break_fsm (tp->global_num,
11787 location_breakpoint, caller_breakpoint);
11788 tp->thread_fsm = &sm->thread_fsm;
11789
11790 discard_cleanups (old_chain);
11791
11792 proceed (-1, GDB_SIGNAL_DEFAULT);
11793
11794 do_cleanups (cleanup);
11795 }
11796
11797 /* This function attempts to parse an optional "if <cond>" clause
11798 from the arg string. If one is not found, it returns NULL.
11799
11800 Else, it returns a pointer to the condition string. (It does not
11801 attempt to evaluate the string against a particular block.) And,
11802 it updates arg to point to the first character following the parsed
11803 if clause in the arg string. */
11804
11805 char *
11806 ep_parse_optional_if_clause (char **arg)
11807 {
11808 char *cond_string;
11809
11810 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
11811 return NULL;
11812
11813 /* Skip the "if" keyword. */
11814 (*arg) += 2;
11815
11816 /* Skip any extra leading whitespace, and record the start of the
11817 condition string. */
11818 *arg = skip_spaces (*arg);
11819 cond_string = *arg;
11820
11821 /* Assume that the condition occupies the remainder of the arg
11822 string. */
11823 (*arg) += strlen (cond_string);
11824
11825 return cond_string;
11826 }
11827
11828 /* Commands to deal with catching events, such as signals, exceptions,
11829 process start/exit, etc. */
11830
11831 typedef enum
11832 {
11833 catch_fork_temporary, catch_vfork_temporary,
11834 catch_fork_permanent, catch_vfork_permanent
11835 }
11836 catch_fork_kind;
11837
11838 static void
11839 catch_fork_command_1 (char *arg, int from_tty,
11840 struct cmd_list_element *command)
11841 {
11842 struct gdbarch *gdbarch = get_current_arch ();
11843 char *cond_string = NULL;
11844 catch_fork_kind fork_kind;
11845 int tempflag;
11846
11847 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11848 tempflag = (fork_kind == catch_fork_temporary
11849 || fork_kind == catch_vfork_temporary);
11850
11851 if (!arg)
11852 arg = "";
11853 arg = skip_spaces (arg);
11854
11855 /* The allowed syntax is:
11856 catch [v]fork
11857 catch [v]fork if <cond>
11858
11859 First, check if there's an if clause. */
11860 cond_string = ep_parse_optional_if_clause (&arg);
11861
11862 if ((*arg != '\0') && !isspace (*arg))
11863 error (_("Junk at end of arguments."));
11864
11865 /* If this target supports it, create a fork or vfork catchpoint
11866 and enable reporting of such events. */
11867 switch (fork_kind)
11868 {
11869 case catch_fork_temporary:
11870 case catch_fork_permanent:
11871 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11872 &catch_fork_breakpoint_ops);
11873 break;
11874 case catch_vfork_temporary:
11875 case catch_vfork_permanent:
11876 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11877 &catch_vfork_breakpoint_ops);
11878 break;
11879 default:
11880 error (_("unsupported or unknown fork kind; cannot catch it"));
11881 break;
11882 }
11883 }
11884
11885 static void
11886 catch_exec_command_1 (char *arg, int from_tty,
11887 struct cmd_list_element *command)
11888 {
11889 struct exec_catchpoint *c;
11890 struct gdbarch *gdbarch = get_current_arch ();
11891 int tempflag;
11892 char *cond_string = NULL;
11893
11894 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11895
11896 if (!arg)
11897 arg = "";
11898 arg = skip_spaces (arg);
11899
11900 /* The allowed syntax is:
11901 catch exec
11902 catch exec if <cond>
11903
11904 First, check if there's an if clause. */
11905 cond_string = ep_parse_optional_if_clause (&arg);
11906
11907 if ((*arg != '\0') && !isspace (*arg))
11908 error (_("Junk at end of arguments."));
11909
11910 c = XNEW (struct exec_catchpoint);
11911 init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
11912 &catch_exec_breakpoint_ops);
11913 c->exec_pathname = NULL;
11914
11915 install_breakpoint (0, &c->base, 1);
11916 }
11917
11918 void
11919 init_ada_exception_breakpoint (struct breakpoint *b,
11920 struct gdbarch *gdbarch,
11921 struct symtab_and_line sal,
11922 char *addr_string,
11923 const struct breakpoint_ops *ops,
11924 int tempflag,
11925 int enabled,
11926 int from_tty)
11927 {
11928 if (from_tty)
11929 {
11930 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11931 if (!loc_gdbarch)
11932 loc_gdbarch = gdbarch;
11933
11934 describe_other_breakpoints (loc_gdbarch,
11935 sal.pspace, sal.pc, sal.section, -1);
11936 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11937 version for exception catchpoints, because two catchpoints
11938 used for different exception names will use the same address.
11939 In this case, a "breakpoint ... also set at..." warning is
11940 unproductive. Besides, the warning phrasing is also a bit
11941 inappropriate, we should use the word catchpoint, and tell
11942 the user what type of catchpoint it is. The above is good
11943 enough for now, though. */
11944 }
11945
11946 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
11947
11948 b->enable_state = enabled ? bp_enabled : bp_disabled;
11949 b->disposition = tempflag ? disp_del : disp_donttouch;
11950 b->location = string_to_event_location (&addr_string,
11951 language_def (language_ada));
11952 b->language = language_ada;
11953 }
11954
11955 static void
11956 catch_command (char *arg, int from_tty)
11957 {
11958 error (_("Catch requires an event name."));
11959 }
11960 \f
11961
11962 static void
11963 tcatch_command (char *arg, int from_tty)
11964 {
11965 error (_("Catch requires an event name."));
11966 }
11967
11968 /* A qsort comparison function that sorts breakpoints in order. */
11969
11970 static int
11971 compare_breakpoints (const void *a, const void *b)
11972 {
11973 const breakpoint_p *ba = (const breakpoint_p *) a;
11974 uintptr_t ua = (uintptr_t) *ba;
11975 const breakpoint_p *bb = (const breakpoint_p *) b;
11976 uintptr_t ub = (uintptr_t) *bb;
11977
11978 if ((*ba)->number < (*bb)->number)
11979 return -1;
11980 else if ((*ba)->number > (*bb)->number)
11981 return 1;
11982
11983 /* Now sort by address, in case we see, e..g, two breakpoints with
11984 the number 0. */
11985 if (ua < ub)
11986 return -1;
11987 return ua > ub ? 1 : 0;
11988 }
11989
11990 /* Delete breakpoints by address or line. */
11991
11992 static void
11993 clear_command (char *arg, int from_tty)
11994 {
11995 struct breakpoint *b, *prev;
11996 VEC(breakpoint_p) *found = 0;
11997 int ix;
11998 int default_match;
11999 struct symtabs_and_lines sals;
12000 struct symtab_and_line sal;
12001 int i;
12002 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
12003
12004 if (arg)
12005 {
12006 sals = decode_line_with_current_source (arg,
12007 (DECODE_LINE_FUNFIRSTLINE
12008 | DECODE_LINE_LIST_MODE));
12009 make_cleanup (xfree, sals.sals);
12010 default_match = 0;
12011 }
12012 else
12013 {
12014 sals.sals = XNEW (struct symtab_and_line);
12015 make_cleanup (xfree, sals.sals);
12016 init_sal (&sal); /* Initialize to zeroes. */
12017
12018 /* Set sal's line, symtab, pc, and pspace to the values
12019 corresponding to the last call to print_frame_info. If the
12020 codepoint is not valid, this will set all the fields to 0. */
12021 get_last_displayed_sal (&sal);
12022 if (sal.symtab == 0)
12023 error (_("No source file specified."));
12024
12025 sals.sals[0] = sal;
12026 sals.nelts = 1;
12027
12028 default_match = 1;
12029 }
12030
12031 /* We don't call resolve_sal_pc here. That's not as bad as it
12032 seems, because all existing breakpoints typically have both
12033 file/line and pc set. So, if clear is given file/line, we can
12034 match this to existing breakpoint without obtaining pc at all.
12035
12036 We only support clearing given the address explicitly
12037 present in breakpoint table. Say, we've set breakpoint
12038 at file:line. There were several PC values for that file:line,
12039 due to optimization, all in one block.
12040
12041 We've picked one PC value. If "clear" is issued with another
12042 PC corresponding to the same file:line, the breakpoint won't
12043 be cleared. We probably can still clear the breakpoint, but
12044 since the other PC value is never presented to user, user
12045 can only find it by guessing, and it does not seem important
12046 to support that. */
12047
12048 /* For each line spec given, delete bps which correspond to it. Do
12049 it in two passes, solely to preserve the current behavior that
12050 from_tty is forced true if we delete more than one
12051 breakpoint. */
12052
12053 found = NULL;
12054 make_cleanup (VEC_cleanup (breakpoint_p), &found);
12055 for (i = 0; i < sals.nelts; i++)
12056 {
12057 const char *sal_fullname;
12058
12059 /* If exact pc given, clear bpts at that pc.
12060 If line given (pc == 0), clear all bpts on specified line.
12061 If defaulting, clear all bpts on default line
12062 or at default pc.
12063
12064 defaulting sal.pc != 0 tests to do
12065
12066 0 1 pc
12067 1 1 pc _and_ line
12068 0 0 line
12069 1 0 <can't happen> */
12070
12071 sal = sals.sals[i];
12072 sal_fullname = (sal.symtab == NULL
12073 ? NULL : symtab_to_fullname (sal.symtab));
12074
12075 /* Find all matching breakpoints and add them to 'found'. */
12076 ALL_BREAKPOINTS (b)
12077 {
12078 int match = 0;
12079 /* Are we going to delete b? */
12080 if (b->type != bp_none && !is_watchpoint (b))
12081 {
12082 struct bp_location *loc = b->loc;
12083 for (; loc; loc = loc->next)
12084 {
12085 /* If the user specified file:line, don't allow a PC
12086 match. This matches historical gdb behavior. */
12087 int pc_match = (!sal.explicit_line
12088 && sal.pc
12089 && (loc->pspace == sal.pspace)
12090 && (loc->address == sal.pc)
12091 && (!section_is_overlay (loc->section)
12092 || loc->section == sal.section));
12093 int line_match = 0;
12094
12095 if ((default_match || sal.explicit_line)
12096 && loc->symtab != NULL
12097 && sal_fullname != NULL
12098 && sal.pspace == loc->pspace
12099 && loc->line_number == sal.line
12100 && filename_cmp (symtab_to_fullname (loc->symtab),
12101 sal_fullname) == 0)
12102 line_match = 1;
12103
12104 if (pc_match || line_match)
12105 {
12106 match = 1;
12107 break;
12108 }
12109 }
12110 }
12111
12112 if (match)
12113 VEC_safe_push(breakpoint_p, found, b);
12114 }
12115 }
12116
12117 /* Now go thru the 'found' chain and delete them. */
12118 if (VEC_empty(breakpoint_p, found))
12119 {
12120 if (arg)
12121 error (_("No breakpoint at %s."), arg);
12122 else
12123 error (_("No breakpoint at this line."));
12124 }
12125
12126 /* Remove duplicates from the vec. */
12127 qsort (VEC_address (breakpoint_p, found),
12128 VEC_length (breakpoint_p, found),
12129 sizeof (breakpoint_p),
12130 compare_breakpoints);
12131 prev = VEC_index (breakpoint_p, found, 0);
12132 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
12133 {
12134 if (b == prev)
12135 {
12136 VEC_ordered_remove (breakpoint_p, found, ix);
12137 --ix;
12138 }
12139 }
12140
12141 if (VEC_length(breakpoint_p, found) > 1)
12142 from_tty = 1; /* Always report if deleted more than one. */
12143 if (from_tty)
12144 {
12145 if (VEC_length(breakpoint_p, found) == 1)
12146 printf_unfiltered (_("Deleted breakpoint "));
12147 else
12148 printf_unfiltered (_("Deleted breakpoints "));
12149 }
12150
12151 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
12152 {
12153 if (from_tty)
12154 printf_unfiltered ("%d ", b->number);
12155 delete_breakpoint (b);
12156 }
12157 if (from_tty)
12158 putchar_unfiltered ('\n');
12159
12160 do_cleanups (cleanups);
12161 }
12162 \f
12163 /* Delete breakpoint in BS if they are `delete' breakpoints and
12164 all breakpoints that are marked for deletion, whether hit or not.
12165 This is called after any breakpoint is hit, or after errors. */
12166
12167 void
12168 breakpoint_auto_delete (bpstat bs)
12169 {
12170 struct breakpoint *b, *b_tmp;
12171
12172 for (; bs; bs = bs->next)
12173 if (bs->breakpoint_at
12174 && bs->breakpoint_at->disposition == disp_del
12175 && bs->stop)
12176 delete_breakpoint (bs->breakpoint_at);
12177
12178 ALL_BREAKPOINTS_SAFE (b, b_tmp)
12179 {
12180 if (b->disposition == disp_del_at_next_stop)
12181 delete_breakpoint (b);
12182 }
12183 }
12184
12185 /* A comparison function for bp_location AP and BP being interfaced to
12186 qsort. Sort elements primarily by their ADDRESS (no matter what
12187 does breakpoint_address_is_meaningful say for its OWNER),
12188 secondarily by ordering first permanent elements and
12189 terciarily just ensuring the array is sorted stable way despite
12190 qsort being an unstable algorithm. */
12191
12192 static int
12193 bp_location_compare (const void *ap, const void *bp)
12194 {
12195 const struct bp_location *a = *(const struct bp_location **) ap;
12196 const struct bp_location *b = *(const struct bp_location **) bp;
12197
12198 if (a->address != b->address)
12199 return (a->address > b->address) - (a->address < b->address);
12200
12201 /* Sort locations at the same address by their pspace number, keeping
12202 locations of the same inferior (in a multi-inferior environment)
12203 grouped. */
12204
12205 if (a->pspace->num != b->pspace->num)
12206 return ((a->pspace->num > b->pspace->num)
12207 - (a->pspace->num < b->pspace->num));
12208
12209 /* Sort permanent breakpoints first. */
12210 if (a->permanent != b->permanent)
12211 return (a->permanent < b->permanent) - (a->permanent > b->permanent);
12212
12213 /* Make the internal GDB representation stable across GDB runs
12214 where A and B memory inside GDB can differ. Breakpoint locations of
12215 the same type at the same address can be sorted in arbitrary order. */
12216
12217 if (a->owner->number != b->owner->number)
12218 return ((a->owner->number > b->owner->number)
12219 - (a->owner->number < b->owner->number));
12220
12221 return (a > b) - (a < b);
12222 }
12223
12224 /* Set bp_location_placed_address_before_address_max and
12225 bp_location_shadow_len_after_address_max according to the current
12226 content of the bp_location array. */
12227
12228 static void
12229 bp_location_target_extensions_update (void)
12230 {
12231 struct bp_location *bl, **blp_tmp;
12232
12233 bp_location_placed_address_before_address_max = 0;
12234 bp_location_shadow_len_after_address_max = 0;
12235
12236 ALL_BP_LOCATIONS (bl, blp_tmp)
12237 {
12238 CORE_ADDR start, end, addr;
12239
12240 if (!bp_location_has_shadow (bl))
12241 continue;
12242
12243 start = bl->target_info.placed_address;
12244 end = start + bl->target_info.shadow_len;
12245
12246 gdb_assert (bl->address >= start);
12247 addr = bl->address - start;
12248 if (addr > bp_location_placed_address_before_address_max)
12249 bp_location_placed_address_before_address_max = addr;
12250
12251 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
12252
12253 gdb_assert (bl->address < end);
12254 addr = end - bl->address;
12255 if (addr > bp_location_shadow_len_after_address_max)
12256 bp_location_shadow_len_after_address_max = addr;
12257 }
12258 }
12259
12260 /* Download tracepoint locations if they haven't been. */
12261
12262 static void
12263 download_tracepoint_locations (void)
12264 {
12265 struct breakpoint *b;
12266 struct cleanup *old_chain;
12267 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
12268
12269 old_chain = save_current_space_and_thread ();
12270
12271 ALL_TRACEPOINTS (b)
12272 {
12273 struct bp_location *bl;
12274 struct tracepoint *t;
12275 int bp_location_downloaded = 0;
12276
12277 if ((b->type == bp_fast_tracepoint
12278 ? !may_insert_fast_tracepoints
12279 : !may_insert_tracepoints))
12280 continue;
12281
12282 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
12283 {
12284 if (target_can_download_tracepoint ())
12285 can_download_tracepoint = TRIBOOL_TRUE;
12286 else
12287 can_download_tracepoint = TRIBOOL_FALSE;
12288 }
12289
12290 if (can_download_tracepoint == TRIBOOL_FALSE)
12291 break;
12292
12293 for (bl = b->loc; bl; bl = bl->next)
12294 {
12295 /* In tracepoint, locations are _never_ duplicated, so
12296 should_be_inserted is equivalent to
12297 unduplicated_should_be_inserted. */
12298 if (!should_be_inserted (bl) || bl->inserted)
12299 continue;
12300
12301 switch_to_program_space_and_thread (bl->pspace);
12302
12303 target_download_tracepoint (bl);
12304
12305 bl->inserted = 1;
12306 bp_location_downloaded = 1;
12307 }
12308 t = (struct tracepoint *) b;
12309 t->number_on_target = b->number;
12310 if (bp_location_downloaded)
12311 observer_notify_breakpoint_modified (b);
12312 }
12313
12314 do_cleanups (old_chain);
12315 }
12316
12317 /* Swap the insertion/duplication state between two locations. */
12318
12319 static void
12320 swap_insertion (struct bp_location *left, struct bp_location *right)
12321 {
12322 const int left_inserted = left->inserted;
12323 const int left_duplicate = left->duplicate;
12324 const int left_needs_update = left->needs_update;
12325 const struct bp_target_info left_target_info = left->target_info;
12326
12327 /* Locations of tracepoints can never be duplicated. */
12328 if (is_tracepoint (left->owner))
12329 gdb_assert (!left->duplicate);
12330 if (is_tracepoint (right->owner))
12331 gdb_assert (!right->duplicate);
12332
12333 left->inserted = right->inserted;
12334 left->duplicate = right->duplicate;
12335 left->needs_update = right->needs_update;
12336 left->target_info = right->target_info;
12337 right->inserted = left_inserted;
12338 right->duplicate = left_duplicate;
12339 right->needs_update = left_needs_update;
12340 right->target_info = left_target_info;
12341 }
12342
12343 /* Force the re-insertion of the locations at ADDRESS. This is called
12344 once a new/deleted/modified duplicate location is found and we are evaluating
12345 conditions on the target's side. Such conditions need to be updated on
12346 the target. */
12347
12348 static void
12349 force_breakpoint_reinsertion (struct bp_location *bl)
12350 {
12351 struct bp_location **locp = NULL, **loc2p;
12352 struct bp_location *loc;
12353 CORE_ADDR address = 0;
12354 int pspace_num;
12355
12356 address = bl->address;
12357 pspace_num = bl->pspace->num;
12358
12359 /* This is only meaningful if the target is
12360 evaluating conditions and if the user has
12361 opted for condition evaluation on the target's
12362 side. */
12363 if (gdb_evaluates_breakpoint_condition_p ()
12364 || !target_supports_evaluation_of_breakpoint_conditions ())
12365 return;
12366
12367 /* Flag all breakpoint locations with this address and
12368 the same program space as the location
12369 as "its condition has changed". We need to
12370 update the conditions on the target's side. */
12371 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
12372 {
12373 loc = *loc2p;
12374
12375 if (!is_breakpoint (loc->owner)
12376 || pspace_num != loc->pspace->num)
12377 continue;
12378
12379 /* Flag the location appropriately. We use a different state to
12380 let everyone know that we already updated the set of locations
12381 with addr bl->address and program space bl->pspace. This is so
12382 we don't have to keep calling these functions just to mark locations
12383 that have already been marked. */
12384 loc->condition_changed = condition_updated;
12385
12386 /* Free the agent expression bytecode as well. We will compute
12387 it later on. */
12388 if (loc->cond_bytecode)
12389 {
12390 free_agent_expr (loc->cond_bytecode);
12391 loc->cond_bytecode = NULL;
12392 }
12393 }
12394 }
12395 /* Called whether new breakpoints are created, or existing breakpoints
12396 deleted, to update the global location list and recompute which
12397 locations are duplicate of which.
12398
12399 The INSERT_MODE flag determines whether locations may not, may, or
12400 shall be inserted now. See 'enum ugll_insert_mode' for more
12401 info. */
12402
12403 static void
12404 update_global_location_list (enum ugll_insert_mode insert_mode)
12405 {
12406 struct breakpoint *b;
12407 struct bp_location **locp, *loc;
12408 struct cleanup *cleanups;
12409 /* Last breakpoint location address that was marked for update. */
12410 CORE_ADDR last_addr = 0;
12411 /* Last breakpoint location program space that was marked for update. */
12412 int last_pspace_num = -1;
12413
12414 /* Used in the duplicates detection below. When iterating over all
12415 bp_locations, points to the first bp_location of a given address.
12416 Breakpoints and watchpoints of different types are never
12417 duplicates of each other. Keep one pointer for each type of
12418 breakpoint/watchpoint, so we only need to loop over all locations
12419 once. */
12420 struct bp_location *bp_loc_first; /* breakpoint */
12421 struct bp_location *wp_loc_first; /* hardware watchpoint */
12422 struct bp_location *awp_loc_first; /* access watchpoint */
12423 struct bp_location *rwp_loc_first; /* read watchpoint */
12424
12425 /* Saved former bp_location array which we compare against the newly
12426 built bp_location from the current state of ALL_BREAKPOINTS. */
12427 struct bp_location **old_location, **old_locp;
12428 unsigned old_location_count;
12429
12430 old_location = bp_location;
12431 old_location_count = bp_location_count;
12432 bp_location = NULL;
12433 bp_location_count = 0;
12434 cleanups = make_cleanup (xfree, old_location);
12435
12436 ALL_BREAKPOINTS (b)
12437 for (loc = b->loc; loc; loc = loc->next)
12438 bp_location_count++;
12439
12440 bp_location = XNEWVEC (struct bp_location *, bp_location_count);
12441 locp = bp_location;
12442 ALL_BREAKPOINTS (b)
12443 for (loc = b->loc; loc; loc = loc->next)
12444 *locp++ = loc;
12445 qsort (bp_location, bp_location_count, sizeof (*bp_location),
12446 bp_location_compare);
12447
12448 bp_location_target_extensions_update ();
12449
12450 /* Identify bp_location instances that are no longer present in the
12451 new list, and therefore should be freed. Note that it's not
12452 necessary that those locations should be removed from inferior --
12453 if there's another location at the same address (previously
12454 marked as duplicate), we don't need to remove/insert the
12455 location.
12456
12457 LOCP is kept in sync with OLD_LOCP, each pointing to the current
12458 and former bp_location array state respectively. */
12459
12460 locp = bp_location;
12461 for (old_locp = old_location; old_locp < old_location + old_location_count;
12462 old_locp++)
12463 {
12464 struct bp_location *old_loc = *old_locp;
12465 struct bp_location **loc2p;
12466
12467 /* Tells if 'old_loc' is found among the new locations. If
12468 not, we have to free it. */
12469 int found_object = 0;
12470 /* Tells if the location should remain inserted in the target. */
12471 int keep_in_target = 0;
12472 int removed = 0;
12473
12474 /* Skip LOCP entries which will definitely never be needed.
12475 Stop either at or being the one matching OLD_LOC. */
12476 while (locp < bp_location + bp_location_count
12477 && (*locp)->address < old_loc->address)
12478 locp++;
12479
12480 for (loc2p = locp;
12481 (loc2p < bp_location + bp_location_count
12482 && (*loc2p)->address == old_loc->address);
12483 loc2p++)
12484 {
12485 /* Check if this is a new/duplicated location or a duplicated
12486 location that had its condition modified. If so, we want to send
12487 its condition to the target if evaluation of conditions is taking
12488 place there. */
12489 if ((*loc2p)->condition_changed == condition_modified
12490 && (last_addr != old_loc->address
12491 || last_pspace_num != old_loc->pspace->num))
12492 {
12493 force_breakpoint_reinsertion (*loc2p);
12494 last_pspace_num = old_loc->pspace->num;
12495 }
12496
12497 if (*loc2p == old_loc)
12498 found_object = 1;
12499 }
12500
12501 /* We have already handled this address, update it so that we don't
12502 have to go through updates again. */
12503 last_addr = old_loc->address;
12504
12505 /* Target-side condition evaluation: Handle deleted locations. */
12506 if (!found_object)
12507 force_breakpoint_reinsertion (old_loc);
12508
12509 /* If this location is no longer present, and inserted, look if
12510 there's maybe a new location at the same address. If so,
12511 mark that one inserted, and don't remove this one. This is
12512 needed so that we don't have a time window where a breakpoint
12513 at certain location is not inserted. */
12514
12515 if (old_loc->inserted)
12516 {
12517 /* If the location is inserted now, we might have to remove
12518 it. */
12519
12520 if (found_object && should_be_inserted (old_loc))
12521 {
12522 /* The location is still present in the location list,
12523 and still should be inserted. Don't do anything. */
12524 keep_in_target = 1;
12525 }
12526 else
12527 {
12528 /* This location still exists, but it won't be kept in the
12529 target since it may have been disabled. We proceed to
12530 remove its target-side condition. */
12531
12532 /* The location is either no longer present, or got
12533 disabled. See if there's another location at the
12534 same address, in which case we don't need to remove
12535 this one from the target. */
12536
12537 /* OLD_LOC comes from existing struct breakpoint. */
12538 if (breakpoint_address_is_meaningful (old_loc->owner))
12539 {
12540 for (loc2p = locp;
12541 (loc2p < bp_location + bp_location_count
12542 && (*loc2p)->address == old_loc->address);
12543 loc2p++)
12544 {
12545 struct bp_location *loc2 = *loc2p;
12546
12547 if (breakpoint_locations_match (loc2, old_loc))
12548 {
12549 /* Read watchpoint locations are switched to
12550 access watchpoints, if the former are not
12551 supported, but the latter are. */
12552 if (is_hardware_watchpoint (old_loc->owner))
12553 {
12554 gdb_assert (is_hardware_watchpoint (loc2->owner));
12555 loc2->watchpoint_type = old_loc->watchpoint_type;
12556 }
12557
12558 /* loc2 is a duplicated location. We need to check
12559 if it should be inserted in case it will be
12560 unduplicated. */
12561 if (loc2 != old_loc
12562 && unduplicated_should_be_inserted (loc2))
12563 {
12564 swap_insertion (old_loc, loc2);
12565 keep_in_target = 1;
12566 break;
12567 }
12568 }
12569 }
12570 }
12571 }
12572
12573 if (!keep_in_target)
12574 {
12575 if (remove_breakpoint (old_loc, mark_uninserted))
12576 {
12577 /* This is just about all we can do. We could keep
12578 this location on the global list, and try to
12579 remove it next time, but there's no particular
12580 reason why we will succeed next time.
12581
12582 Note that at this point, old_loc->owner is still
12583 valid, as delete_breakpoint frees the breakpoint
12584 only after calling us. */
12585 printf_filtered (_("warning: Error removing "
12586 "breakpoint %d\n"),
12587 old_loc->owner->number);
12588 }
12589 removed = 1;
12590 }
12591 }
12592
12593 if (!found_object)
12594 {
12595 if (removed && target_is_non_stop_p ()
12596 && need_moribund_for_location_type (old_loc))
12597 {
12598 /* This location was removed from the target. In
12599 non-stop mode, a race condition is possible where
12600 we've removed a breakpoint, but stop events for that
12601 breakpoint are already queued and will arrive later.
12602 We apply an heuristic to be able to distinguish such
12603 SIGTRAPs from other random SIGTRAPs: we keep this
12604 breakpoint location for a bit, and will retire it
12605 after we see some number of events. The theory here
12606 is that reporting of events should, "on the average",
12607 be fair, so after a while we'll see events from all
12608 threads that have anything of interest, and no longer
12609 need to keep this breakpoint location around. We
12610 don't hold locations forever so to reduce chances of
12611 mistaking a non-breakpoint SIGTRAP for a breakpoint
12612 SIGTRAP.
12613
12614 The heuristic failing can be disastrous on
12615 decr_pc_after_break targets.
12616
12617 On decr_pc_after_break targets, like e.g., x86-linux,
12618 if we fail to recognize a late breakpoint SIGTRAP,
12619 because events_till_retirement has reached 0 too
12620 soon, we'll fail to do the PC adjustment, and report
12621 a random SIGTRAP to the user. When the user resumes
12622 the inferior, it will most likely immediately crash
12623 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
12624 corrupted, because of being resumed e.g., in the
12625 middle of a multi-byte instruction, or skipped a
12626 one-byte instruction. This was actually seen happen
12627 on native x86-linux, and should be less rare on
12628 targets that do not support new thread events, like
12629 remote, due to the heuristic depending on
12630 thread_count.
12631
12632 Mistaking a random SIGTRAP for a breakpoint trap
12633 causes similar symptoms (PC adjustment applied when
12634 it shouldn't), but then again, playing with SIGTRAPs
12635 behind the debugger's back is asking for trouble.
12636
12637 Since hardware watchpoint traps are always
12638 distinguishable from other traps, so we don't need to
12639 apply keep hardware watchpoint moribund locations
12640 around. We simply always ignore hardware watchpoint
12641 traps we can no longer explain. */
12642
12643 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12644 old_loc->owner = NULL;
12645
12646 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
12647 }
12648 else
12649 {
12650 old_loc->owner = NULL;
12651 decref_bp_location (&old_loc);
12652 }
12653 }
12654 }
12655
12656 /* Rescan breakpoints at the same address and section, marking the
12657 first one as "first" and any others as "duplicates". This is so
12658 that the bpt instruction is only inserted once. If we have a
12659 permanent breakpoint at the same place as BPT, make that one the
12660 official one, and the rest as duplicates. Permanent breakpoints
12661 are sorted first for the same address.
12662
12663 Do the same for hardware watchpoints, but also considering the
12664 watchpoint's type (regular/access/read) and length. */
12665
12666 bp_loc_first = NULL;
12667 wp_loc_first = NULL;
12668 awp_loc_first = NULL;
12669 rwp_loc_first = NULL;
12670 ALL_BP_LOCATIONS (loc, locp)
12671 {
12672 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12673 non-NULL. */
12674 struct bp_location **loc_first_p;
12675 b = loc->owner;
12676
12677 if (!unduplicated_should_be_inserted (loc)
12678 || !breakpoint_address_is_meaningful (b)
12679 /* Don't detect duplicate for tracepoint locations because they are
12680 never duplicated. See the comments in field `duplicate' of
12681 `struct bp_location'. */
12682 || is_tracepoint (b))
12683 {
12684 /* Clear the condition modification flag. */
12685 loc->condition_changed = condition_unchanged;
12686 continue;
12687 }
12688
12689 if (b->type == bp_hardware_watchpoint)
12690 loc_first_p = &wp_loc_first;
12691 else if (b->type == bp_read_watchpoint)
12692 loc_first_p = &rwp_loc_first;
12693 else if (b->type == bp_access_watchpoint)
12694 loc_first_p = &awp_loc_first;
12695 else
12696 loc_first_p = &bp_loc_first;
12697
12698 if (*loc_first_p == NULL
12699 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12700 || !breakpoint_locations_match (loc, *loc_first_p))
12701 {
12702 *loc_first_p = loc;
12703 loc->duplicate = 0;
12704
12705 if (is_breakpoint (loc->owner) && loc->condition_changed)
12706 {
12707 loc->needs_update = 1;
12708 /* Clear the condition modification flag. */
12709 loc->condition_changed = condition_unchanged;
12710 }
12711 continue;
12712 }
12713
12714
12715 /* This and the above ensure the invariant that the first location
12716 is not duplicated, and is the inserted one.
12717 All following are marked as duplicated, and are not inserted. */
12718 if (loc->inserted)
12719 swap_insertion (loc, *loc_first_p);
12720 loc->duplicate = 1;
12721
12722 /* Clear the condition modification flag. */
12723 loc->condition_changed = condition_unchanged;
12724 }
12725
12726 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
12727 {
12728 if (insert_mode != UGLL_DONT_INSERT)
12729 insert_breakpoint_locations ();
12730 else
12731 {
12732 /* Even though the caller told us to not insert new
12733 locations, we may still need to update conditions on the
12734 target's side of breakpoints that were already inserted
12735 if the target is evaluating breakpoint conditions. We
12736 only update conditions for locations that are marked
12737 "needs_update". */
12738 update_inserted_breakpoint_locations ();
12739 }
12740 }
12741
12742 if (insert_mode != UGLL_DONT_INSERT)
12743 download_tracepoint_locations ();
12744
12745 do_cleanups (cleanups);
12746 }
12747
12748 void
12749 breakpoint_retire_moribund (void)
12750 {
12751 struct bp_location *loc;
12752 int ix;
12753
12754 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12755 if (--(loc->events_till_retirement) == 0)
12756 {
12757 decref_bp_location (&loc);
12758 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12759 --ix;
12760 }
12761 }
12762
12763 static void
12764 update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
12765 {
12766
12767 TRY
12768 {
12769 update_global_location_list (insert_mode);
12770 }
12771 CATCH (e, RETURN_MASK_ERROR)
12772 {
12773 }
12774 END_CATCH
12775 }
12776
12777 /* Clear BKP from a BPS. */
12778
12779 static void
12780 bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12781 {
12782 bpstat bs;
12783
12784 for (bs = bps; bs; bs = bs->next)
12785 if (bs->breakpoint_at == bpt)
12786 {
12787 bs->breakpoint_at = NULL;
12788 bs->old_val = NULL;
12789 /* bs->commands will be freed later. */
12790 }
12791 }
12792
12793 /* Callback for iterate_over_threads. */
12794 static int
12795 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12796 {
12797 struct breakpoint *bpt = (struct breakpoint *) data;
12798
12799 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12800 return 0;
12801 }
12802
12803 /* Helper for breakpoint and tracepoint breakpoint_ops->mention
12804 callbacks. */
12805
12806 static void
12807 say_where (struct breakpoint *b)
12808 {
12809 struct value_print_options opts;
12810
12811 get_user_print_options (&opts);
12812
12813 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12814 single string. */
12815 if (b->loc == NULL)
12816 {
12817 /* For pending locations, the output differs slightly based
12818 on b->extra_string. If this is non-NULL, it contains either
12819 a condition or dprintf arguments. */
12820 if (b->extra_string == NULL)
12821 {
12822 printf_filtered (_(" (%s) pending."),
12823 event_location_to_string (b->location));
12824 }
12825 else if (b->type == bp_dprintf)
12826 {
12827 printf_filtered (_(" (%s,%s) pending."),
12828 event_location_to_string (b->location),
12829 b->extra_string);
12830 }
12831 else
12832 {
12833 printf_filtered (_(" (%s %s) pending."),
12834 event_location_to_string (b->location),
12835 b->extra_string);
12836 }
12837 }
12838 else
12839 {
12840 if (opts.addressprint || b->loc->symtab == NULL)
12841 {
12842 printf_filtered (" at ");
12843 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12844 gdb_stdout);
12845 }
12846 if (b->loc->symtab != NULL)
12847 {
12848 /* If there is a single location, we can print the location
12849 more nicely. */
12850 if (b->loc->next == NULL)
12851 printf_filtered (": file %s, line %d.",
12852 symtab_to_filename_for_display (b->loc->symtab),
12853 b->loc->line_number);
12854 else
12855 /* This is not ideal, but each location may have a
12856 different file name, and this at least reflects the
12857 real situation somewhat. */
12858 printf_filtered (": %s.",
12859 event_location_to_string (b->location));
12860 }
12861
12862 if (b->loc->next)
12863 {
12864 struct bp_location *loc = b->loc;
12865 int n = 0;
12866 for (; loc; loc = loc->next)
12867 ++n;
12868 printf_filtered (" (%d locations)", n);
12869 }
12870 }
12871 }
12872
12873 /* Default bp_location_ops methods. */
12874
12875 static void
12876 bp_location_dtor (struct bp_location *self)
12877 {
12878 xfree (self->cond);
12879 if (self->cond_bytecode)
12880 free_agent_expr (self->cond_bytecode);
12881 xfree (self->function_name);
12882
12883 VEC_free (agent_expr_p, self->target_info.conditions);
12884 VEC_free (agent_expr_p, self->target_info.tcommands);
12885 }
12886
12887 static const struct bp_location_ops bp_location_ops =
12888 {
12889 bp_location_dtor
12890 };
12891
12892 /* Default breakpoint_ops methods all breakpoint_ops ultimately
12893 inherit from. */
12894
12895 static void
12896 base_breakpoint_dtor (struct breakpoint *self)
12897 {
12898 decref_counted_command_line (&self->commands);
12899 xfree (self->cond_string);
12900 xfree (self->extra_string);
12901 xfree (self->filter);
12902 delete_event_location (self->location);
12903 delete_event_location (self->location_range_end);
12904 }
12905
12906 static struct bp_location *
12907 base_breakpoint_allocate_location (struct breakpoint *self)
12908 {
12909 struct bp_location *loc;
12910
12911 loc = XNEW (struct bp_location);
12912 init_bp_location (loc, &bp_location_ops, self);
12913 return loc;
12914 }
12915
12916 static void
12917 base_breakpoint_re_set (struct breakpoint *b)
12918 {
12919 /* Nothing to re-set. */
12920 }
12921
12922 #define internal_error_pure_virtual_called() \
12923 gdb_assert_not_reached ("pure virtual function called")
12924
12925 static int
12926 base_breakpoint_insert_location (struct bp_location *bl)
12927 {
12928 internal_error_pure_virtual_called ();
12929 }
12930
12931 static int
12932 base_breakpoint_remove_location (struct bp_location *bl)
12933 {
12934 internal_error_pure_virtual_called ();
12935 }
12936
12937 static int
12938 base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12939 struct address_space *aspace,
12940 CORE_ADDR bp_addr,
12941 const struct target_waitstatus *ws)
12942 {
12943 internal_error_pure_virtual_called ();
12944 }
12945
12946 static void
12947 base_breakpoint_check_status (bpstat bs)
12948 {
12949 /* Always stop. */
12950 }
12951
12952 /* A "works_in_software_mode" breakpoint_ops method that just internal
12953 errors. */
12954
12955 static int
12956 base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12957 {
12958 internal_error_pure_virtual_called ();
12959 }
12960
12961 /* A "resources_needed" breakpoint_ops method that just internal
12962 errors. */
12963
12964 static int
12965 base_breakpoint_resources_needed (const struct bp_location *bl)
12966 {
12967 internal_error_pure_virtual_called ();
12968 }
12969
12970 static enum print_stop_action
12971 base_breakpoint_print_it (bpstat bs)
12972 {
12973 internal_error_pure_virtual_called ();
12974 }
12975
12976 static void
12977 base_breakpoint_print_one_detail (const struct breakpoint *self,
12978 struct ui_out *uiout)
12979 {
12980 /* nothing */
12981 }
12982
12983 static void
12984 base_breakpoint_print_mention (struct breakpoint *b)
12985 {
12986 internal_error_pure_virtual_called ();
12987 }
12988
12989 static void
12990 base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12991 {
12992 internal_error_pure_virtual_called ();
12993 }
12994
12995 static void
12996 base_breakpoint_create_sals_from_location
12997 (const struct event_location *location,
12998 struct linespec_result *canonical,
12999 enum bptype type_wanted)
13000 {
13001 internal_error_pure_virtual_called ();
13002 }
13003
13004 static void
13005 base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13006 struct linespec_result *c,
13007 char *cond_string,
13008 char *extra_string,
13009 enum bptype type_wanted,
13010 enum bpdisp disposition,
13011 int thread,
13012 int task, int ignore_count,
13013 const struct breakpoint_ops *o,
13014 int from_tty, int enabled,
13015 int internal, unsigned flags)
13016 {
13017 internal_error_pure_virtual_called ();
13018 }
13019
13020 static void
13021 base_breakpoint_decode_location (struct breakpoint *b,
13022 const struct event_location *location,
13023 struct program_space *search_pspace,
13024 struct symtabs_and_lines *sals)
13025 {
13026 internal_error_pure_virtual_called ();
13027 }
13028
13029 /* The default 'explains_signal' method. */
13030
13031 static int
13032 base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
13033 {
13034 return 1;
13035 }
13036
13037 /* The default "after_condition_true" method. */
13038
13039 static void
13040 base_breakpoint_after_condition_true (struct bpstats *bs)
13041 {
13042 /* Nothing to do. */
13043 }
13044
13045 struct breakpoint_ops base_breakpoint_ops =
13046 {
13047 base_breakpoint_dtor,
13048 base_breakpoint_allocate_location,
13049 base_breakpoint_re_set,
13050 base_breakpoint_insert_location,
13051 base_breakpoint_remove_location,
13052 base_breakpoint_breakpoint_hit,
13053 base_breakpoint_check_status,
13054 base_breakpoint_resources_needed,
13055 base_breakpoint_works_in_software_mode,
13056 base_breakpoint_print_it,
13057 NULL,
13058 base_breakpoint_print_one_detail,
13059 base_breakpoint_print_mention,
13060 base_breakpoint_print_recreate,
13061 base_breakpoint_create_sals_from_location,
13062 base_breakpoint_create_breakpoints_sal,
13063 base_breakpoint_decode_location,
13064 base_breakpoint_explains_signal,
13065 base_breakpoint_after_condition_true,
13066 };
13067
13068 /* Default breakpoint_ops methods. */
13069
13070 static void
13071 bkpt_re_set (struct breakpoint *b)
13072 {
13073 /* FIXME: is this still reachable? */
13074 if (breakpoint_event_location_empty_p (b))
13075 {
13076 /* Anything without a location can't be re-set. */
13077 delete_breakpoint (b);
13078 return;
13079 }
13080
13081 breakpoint_re_set_default (b);
13082 }
13083
13084 static int
13085 bkpt_insert_location (struct bp_location *bl)
13086 {
13087 if (bl->loc_type == bp_loc_hardware_breakpoint)
13088 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
13089 else
13090 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
13091 }
13092
13093 static int
13094 bkpt_remove_location (struct bp_location *bl)
13095 {
13096 if (bl->loc_type == bp_loc_hardware_breakpoint)
13097 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
13098 else
13099 return target_remove_breakpoint (bl->gdbarch, &bl->target_info);
13100 }
13101
13102 static int
13103 bkpt_breakpoint_hit (const struct bp_location *bl,
13104 struct address_space *aspace, CORE_ADDR bp_addr,
13105 const struct target_waitstatus *ws)
13106 {
13107 if (ws->kind != TARGET_WAITKIND_STOPPED
13108 || ws->value.sig != GDB_SIGNAL_TRAP)
13109 return 0;
13110
13111 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
13112 aspace, bp_addr))
13113 return 0;
13114
13115 if (overlay_debugging /* unmapped overlay section */
13116 && section_is_overlay (bl->section)
13117 && !section_is_mapped (bl->section))
13118 return 0;
13119
13120 return 1;
13121 }
13122
13123 static int
13124 dprintf_breakpoint_hit (const struct bp_location *bl,
13125 struct address_space *aspace, CORE_ADDR bp_addr,
13126 const struct target_waitstatus *ws)
13127 {
13128 if (dprintf_style == dprintf_style_agent
13129 && target_can_run_breakpoint_commands ())
13130 {
13131 /* An agent-style dprintf never causes a stop. If we see a trap
13132 for this address it must be for a breakpoint that happens to
13133 be set at the same address. */
13134 return 0;
13135 }
13136
13137 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
13138 }
13139
13140 static int
13141 bkpt_resources_needed (const struct bp_location *bl)
13142 {
13143 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
13144
13145 return 1;
13146 }
13147
13148 static enum print_stop_action
13149 bkpt_print_it (bpstat bs)
13150 {
13151 struct breakpoint *b;
13152 const struct bp_location *bl;
13153 int bp_temp;
13154 struct ui_out *uiout = current_uiout;
13155
13156 gdb_assert (bs->bp_location_at != NULL);
13157
13158 bl = bs->bp_location_at;
13159 b = bs->breakpoint_at;
13160
13161 bp_temp = b->disposition == disp_del;
13162 if (bl->address != bl->requested_address)
13163 breakpoint_adjustment_warning (bl->requested_address,
13164 bl->address,
13165 b->number, 1);
13166 annotate_breakpoint (b->number);
13167 maybe_print_thread_hit_breakpoint (uiout);
13168
13169 if (bp_temp)
13170 ui_out_text (uiout, "Temporary breakpoint ");
13171 else
13172 ui_out_text (uiout, "Breakpoint ");
13173 if (ui_out_is_mi_like_p (uiout))
13174 {
13175 ui_out_field_string (uiout, "reason",
13176 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
13177 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
13178 }
13179 ui_out_field_int (uiout, "bkptno", b->number);
13180 ui_out_text (uiout, ", ");
13181
13182 return PRINT_SRC_AND_LOC;
13183 }
13184
13185 static void
13186 bkpt_print_mention (struct breakpoint *b)
13187 {
13188 if (ui_out_is_mi_like_p (current_uiout))
13189 return;
13190
13191 switch (b->type)
13192 {
13193 case bp_breakpoint:
13194 case bp_gnu_ifunc_resolver:
13195 if (b->disposition == disp_del)
13196 printf_filtered (_("Temporary breakpoint"));
13197 else
13198 printf_filtered (_("Breakpoint"));
13199 printf_filtered (_(" %d"), b->number);
13200 if (b->type == bp_gnu_ifunc_resolver)
13201 printf_filtered (_(" at gnu-indirect-function resolver"));
13202 break;
13203 case bp_hardware_breakpoint:
13204 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
13205 break;
13206 case bp_dprintf:
13207 printf_filtered (_("Dprintf %d"), b->number);
13208 break;
13209 }
13210
13211 say_where (b);
13212 }
13213
13214 static void
13215 bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13216 {
13217 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
13218 fprintf_unfiltered (fp, "tbreak");
13219 else if (tp->type == bp_breakpoint)
13220 fprintf_unfiltered (fp, "break");
13221 else if (tp->type == bp_hardware_breakpoint
13222 && tp->disposition == disp_del)
13223 fprintf_unfiltered (fp, "thbreak");
13224 else if (tp->type == bp_hardware_breakpoint)
13225 fprintf_unfiltered (fp, "hbreak");
13226 else
13227 internal_error (__FILE__, __LINE__,
13228 _("unhandled breakpoint type %d"), (int) tp->type);
13229
13230 fprintf_unfiltered (fp, " %s",
13231 event_location_to_string (tp->location));
13232
13233 /* Print out extra_string if this breakpoint is pending. It might
13234 contain, for example, conditions that were set by the user. */
13235 if (tp->loc == NULL && tp->extra_string != NULL)
13236 fprintf_unfiltered (fp, " %s", tp->extra_string);
13237
13238 print_recreate_thread (tp, fp);
13239 }
13240
13241 static void
13242 bkpt_create_sals_from_location (const struct event_location *location,
13243 struct linespec_result *canonical,
13244 enum bptype type_wanted)
13245 {
13246 create_sals_from_location_default (location, canonical, type_wanted);
13247 }
13248
13249 static void
13250 bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
13251 struct linespec_result *canonical,
13252 char *cond_string,
13253 char *extra_string,
13254 enum bptype type_wanted,
13255 enum bpdisp disposition,
13256 int thread,
13257 int task, int ignore_count,
13258 const struct breakpoint_ops *ops,
13259 int from_tty, int enabled,
13260 int internal, unsigned flags)
13261 {
13262 create_breakpoints_sal_default (gdbarch, canonical,
13263 cond_string, extra_string,
13264 type_wanted,
13265 disposition, thread, task,
13266 ignore_count, ops, from_tty,
13267 enabled, internal, flags);
13268 }
13269
13270 static void
13271 bkpt_decode_location (struct breakpoint *b,
13272 const struct event_location *location,
13273 struct program_space *search_pspace,
13274 struct symtabs_and_lines *sals)
13275 {
13276 decode_location_default (b, location, search_pspace, sals);
13277 }
13278
13279 /* Virtual table for internal breakpoints. */
13280
13281 static void
13282 internal_bkpt_re_set (struct breakpoint *b)
13283 {
13284 switch (b->type)
13285 {
13286 /* Delete overlay event and longjmp master breakpoints; they
13287 will be reset later by breakpoint_re_set. */
13288 case bp_overlay_event:
13289 case bp_longjmp_master:
13290 case bp_std_terminate_master:
13291 case bp_exception_master:
13292 delete_breakpoint (b);
13293 break;
13294
13295 /* This breakpoint is special, it's set up when the inferior
13296 starts and we really don't want to touch it. */
13297 case bp_shlib_event:
13298
13299 /* Like bp_shlib_event, this breakpoint type is special. Once
13300 it is set up, we do not want to touch it. */
13301 case bp_thread_event:
13302 break;
13303 }
13304 }
13305
13306 static void
13307 internal_bkpt_check_status (bpstat bs)
13308 {
13309 if (bs->breakpoint_at->type == bp_shlib_event)
13310 {
13311 /* If requested, stop when the dynamic linker notifies GDB of
13312 events. This allows the user to get control and place
13313 breakpoints in initializer routines for dynamically loaded
13314 objects (among other things). */
13315 bs->stop = stop_on_solib_events;
13316 bs->print = stop_on_solib_events;
13317 }
13318 else
13319 bs->stop = 0;
13320 }
13321
13322 static enum print_stop_action
13323 internal_bkpt_print_it (bpstat bs)
13324 {
13325 struct breakpoint *b;
13326
13327 b = bs->breakpoint_at;
13328
13329 switch (b->type)
13330 {
13331 case bp_shlib_event:
13332 /* Did we stop because the user set the stop_on_solib_events
13333 variable? (If so, we report this as a generic, "Stopped due
13334 to shlib event" message.) */
13335 print_solib_event (0);
13336 break;
13337
13338 case bp_thread_event:
13339 /* Not sure how we will get here.
13340 GDB should not stop for these breakpoints. */
13341 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
13342 break;
13343
13344 case bp_overlay_event:
13345 /* By analogy with the thread event, GDB should not stop for these. */
13346 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
13347 break;
13348
13349 case bp_longjmp_master:
13350 /* These should never be enabled. */
13351 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
13352 break;
13353
13354 case bp_std_terminate_master:
13355 /* These should never be enabled. */
13356 printf_filtered (_("std::terminate Master Breakpoint: "
13357 "gdb should not stop!\n"));
13358 break;
13359
13360 case bp_exception_master:
13361 /* These should never be enabled. */
13362 printf_filtered (_("Exception Master Breakpoint: "
13363 "gdb should not stop!\n"));
13364 break;
13365 }
13366
13367 return PRINT_NOTHING;
13368 }
13369
13370 static void
13371 internal_bkpt_print_mention (struct breakpoint *b)
13372 {
13373 /* Nothing to mention. These breakpoints are internal. */
13374 }
13375
13376 /* Virtual table for momentary breakpoints */
13377
13378 static void
13379 momentary_bkpt_re_set (struct breakpoint *b)
13380 {
13381 /* Keep temporary breakpoints, which can be encountered when we step
13382 over a dlopen call and solib_add is resetting the breakpoints.
13383 Otherwise these should have been blown away via the cleanup chain
13384 or by breakpoint_init_inferior when we rerun the executable. */
13385 }
13386
13387 static void
13388 momentary_bkpt_check_status (bpstat bs)
13389 {
13390 /* Nothing. The point of these breakpoints is causing a stop. */
13391 }
13392
13393 static enum print_stop_action
13394 momentary_bkpt_print_it (bpstat bs)
13395 {
13396 return PRINT_UNKNOWN;
13397 }
13398
13399 static void
13400 momentary_bkpt_print_mention (struct breakpoint *b)
13401 {
13402 /* Nothing to mention. These breakpoints are internal. */
13403 }
13404
13405 /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
13406
13407 It gets cleared already on the removal of the first one of such placed
13408 breakpoints. This is OK as they get all removed altogether. */
13409
13410 static void
13411 longjmp_bkpt_dtor (struct breakpoint *self)
13412 {
13413 struct thread_info *tp = find_thread_global_id (self->thread);
13414
13415 if (tp)
13416 tp->initiating_frame = null_frame_id;
13417
13418 momentary_breakpoint_ops.dtor (self);
13419 }
13420
13421 /* Specific methods for probe breakpoints. */
13422
13423 static int
13424 bkpt_probe_insert_location (struct bp_location *bl)
13425 {
13426 int v = bkpt_insert_location (bl);
13427
13428 if (v == 0)
13429 {
13430 /* The insertion was successful, now let's set the probe's semaphore
13431 if needed. */
13432 if (bl->probe.probe->pops->set_semaphore != NULL)
13433 bl->probe.probe->pops->set_semaphore (bl->probe.probe,
13434 bl->probe.objfile,
13435 bl->gdbarch);
13436 }
13437
13438 return v;
13439 }
13440
13441 static int
13442 bkpt_probe_remove_location (struct bp_location *bl)
13443 {
13444 /* Let's clear the semaphore before removing the location. */
13445 if (bl->probe.probe->pops->clear_semaphore != NULL)
13446 bl->probe.probe->pops->clear_semaphore (bl->probe.probe,
13447 bl->probe.objfile,
13448 bl->gdbarch);
13449
13450 return bkpt_remove_location (bl);
13451 }
13452
13453 static void
13454 bkpt_probe_create_sals_from_location (const struct event_location *location,
13455 struct linespec_result *canonical,
13456 enum bptype type_wanted)
13457 {
13458 struct linespec_sals lsal;
13459
13460 lsal.sals = parse_probes (location, NULL, canonical);
13461 lsal.canonical = xstrdup (event_location_to_string (canonical->location));
13462 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13463 }
13464
13465 static void
13466 bkpt_probe_decode_location (struct breakpoint *b,
13467 const struct event_location *location,
13468 struct program_space *search_pspace,
13469 struct symtabs_and_lines *sals)
13470 {
13471 *sals = parse_probes (location, search_pspace, NULL);
13472 if (!sals->sals)
13473 error (_("probe not found"));
13474 }
13475
13476 /* The breakpoint_ops structure to be used in tracepoints. */
13477
13478 static void
13479 tracepoint_re_set (struct breakpoint *b)
13480 {
13481 breakpoint_re_set_default (b);
13482 }
13483
13484 static int
13485 tracepoint_breakpoint_hit (const struct bp_location *bl,
13486 struct address_space *aspace, CORE_ADDR bp_addr,
13487 const struct target_waitstatus *ws)
13488 {
13489 /* By definition, the inferior does not report stops at
13490 tracepoints. */
13491 return 0;
13492 }
13493
13494 static void
13495 tracepoint_print_one_detail (const struct breakpoint *self,
13496 struct ui_out *uiout)
13497 {
13498 struct tracepoint *tp = (struct tracepoint *) self;
13499 if (tp->static_trace_marker_id)
13500 {
13501 gdb_assert (self->type == bp_static_tracepoint);
13502
13503 ui_out_text (uiout, "\tmarker id is ");
13504 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
13505 tp->static_trace_marker_id);
13506 ui_out_text (uiout, "\n");
13507 }
13508 }
13509
13510 static void
13511 tracepoint_print_mention (struct breakpoint *b)
13512 {
13513 if (ui_out_is_mi_like_p (current_uiout))
13514 return;
13515
13516 switch (b->type)
13517 {
13518 case bp_tracepoint:
13519 printf_filtered (_("Tracepoint"));
13520 printf_filtered (_(" %d"), b->number);
13521 break;
13522 case bp_fast_tracepoint:
13523 printf_filtered (_("Fast tracepoint"));
13524 printf_filtered (_(" %d"), b->number);
13525 break;
13526 case bp_static_tracepoint:
13527 printf_filtered (_("Static tracepoint"));
13528 printf_filtered (_(" %d"), b->number);
13529 break;
13530 default:
13531 internal_error (__FILE__, __LINE__,
13532 _("unhandled tracepoint type %d"), (int) b->type);
13533 }
13534
13535 say_where (b);
13536 }
13537
13538 static void
13539 tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
13540 {
13541 struct tracepoint *tp = (struct tracepoint *) self;
13542
13543 if (self->type == bp_fast_tracepoint)
13544 fprintf_unfiltered (fp, "ftrace");
13545 else if (self->type == bp_static_tracepoint)
13546 fprintf_unfiltered (fp, "strace");
13547 else if (self->type == bp_tracepoint)
13548 fprintf_unfiltered (fp, "trace");
13549 else
13550 internal_error (__FILE__, __LINE__,
13551 _("unhandled tracepoint type %d"), (int) self->type);
13552
13553 fprintf_unfiltered (fp, " %s",
13554 event_location_to_string (self->location));
13555 print_recreate_thread (self, fp);
13556
13557 if (tp->pass_count)
13558 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
13559 }
13560
13561 static void
13562 tracepoint_create_sals_from_location (const struct event_location *location,
13563 struct linespec_result *canonical,
13564 enum bptype type_wanted)
13565 {
13566 create_sals_from_location_default (location, canonical, type_wanted);
13567 }
13568
13569 static void
13570 tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13571 struct linespec_result *canonical,
13572 char *cond_string,
13573 char *extra_string,
13574 enum bptype type_wanted,
13575 enum bpdisp disposition,
13576 int thread,
13577 int task, int ignore_count,
13578 const struct breakpoint_ops *ops,
13579 int from_tty, int enabled,
13580 int internal, unsigned flags)
13581 {
13582 create_breakpoints_sal_default (gdbarch, canonical,
13583 cond_string, extra_string,
13584 type_wanted,
13585 disposition, thread, task,
13586 ignore_count, ops, from_tty,
13587 enabled, internal, flags);
13588 }
13589
13590 static void
13591 tracepoint_decode_location (struct breakpoint *b,
13592 const struct event_location *location,
13593 struct program_space *search_pspace,
13594 struct symtabs_and_lines *sals)
13595 {
13596 decode_location_default (b, location, search_pspace, sals);
13597 }
13598
13599 struct breakpoint_ops tracepoint_breakpoint_ops;
13600
13601 /* The breakpoint_ops structure to be use on tracepoints placed in a
13602 static probe. */
13603
13604 static void
13605 tracepoint_probe_create_sals_from_location
13606 (const struct event_location *location,
13607 struct linespec_result *canonical,
13608 enum bptype type_wanted)
13609 {
13610 /* We use the same method for breakpoint on probes. */
13611 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
13612 }
13613
13614 static void
13615 tracepoint_probe_decode_location (struct breakpoint *b,
13616 const struct event_location *location,
13617 struct program_space *search_pspace,
13618 struct symtabs_and_lines *sals)
13619 {
13620 /* We use the same method for breakpoint on probes. */
13621 bkpt_probe_decode_location (b, location, search_pspace, sals);
13622 }
13623
13624 static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13625
13626 /* Dprintf breakpoint_ops methods. */
13627
13628 static void
13629 dprintf_re_set (struct breakpoint *b)
13630 {
13631 breakpoint_re_set_default (b);
13632
13633 /* extra_string should never be non-NULL for dprintf. */
13634 gdb_assert (b->extra_string != NULL);
13635
13636 /* 1 - connect to target 1, that can run breakpoint commands.
13637 2 - create a dprintf, which resolves fine.
13638 3 - disconnect from target 1
13639 4 - connect to target 2, that can NOT run breakpoint commands.
13640
13641 After steps #3/#4, you'll want the dprintf command list to
13642 be updated, because target 1 and 2 may well return different
13643 answers for target_can_run_breakpoint_commands().
13644 Given absence of finer grained resetting, we get to do
13645 it all the time. */
13646 if (b->extra_string != NULL)
13647 update_dprintf_command_list (b);
13648 }
13649
13650 /* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13651
13652 static void
13653 dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13654 {
13655 fprintf_unfiltered (fp, "dprintf %s,%s",
13656 event_location_to_string (tp->location),
13657 tp->extra_string);
13658 print_recreate_thread (tp, fp);
13659 }
13660
13661 /* Implement the "after_condition_true" breakpoint_ops method for
13662 dprintf.
13663
13664 dprintf's are implemented with regular commands in their command
13665 list, but we run the commands here instead of before presenting the
13666 stop to the user, as dprintf's don't actually cause a stop. This
13667 also makes it so that the commands of multiple dprintfs at the same
13668 address are all handled. */
13669
13670 static void
13671 dprintf_after_condition_true (struct bpstats *bs)
13672 {
13673 struct cleanup *old_chain;
13674 struct bpstats tmp_bs = { NULL };
13675 struct bpstats *tmp_bs_p = &tmp_bs;
13676
13677 /* dprintf's never cause a stop. This wasn't set in the
13678 check_status hook instead because that would make the dprintf's
13679 condition not be evaluated. */
13680 bs->stop = 0;
13681
13682 /* Run the command list here. Take ownership of it instead of
13683 copying. We never want these commands to run later in
13684 bpstat_do_actions, if a breakpoint that causes a stop happens to
13685 be set at same address as this dprintf, or even if running the
13686 commands here throws. */
13687 tmp_bs.commands = bs->commands;
13688 bs->commands = NULL;
13689 old_chain = make_cleanup_decref_counted_command_line (&tmp_bs.commands);
13690
13691 bpstat_do_actions_1 (&tmp_bs_p);
13692
13693 /* 'tmp_bs.commands' will usually be NULL by now, but
13694 bpstat_do_actions_1 may return early without processing the whole
13695 list. */
13696 do_cleanups (old_chain);
13697 }
13698
13699 /* The breakpoint_ops structure to be used on static tracepoints with
13700 markers (`-m'). */
13701
13702 static void
13703 strace_marker_create_sals_from_location (const struct event_location *location,
13704 struct linespec_result *canonical,
13705 enum bptype type_wanted)
13706 {
13707 struct linespec_sals lsal;
13708 const char *arg_start, *arg;
13709 char *str;
13710 struct cleanup *cleanup;
13711
13712 arg = arg_start = get_linespec_location (location);
13713 lsal.sals = decode_static_tracepoint_spec (&arg);
13714
13715 str = savestring (arg_start, arg - arg_start);
13716 cleanup = make_cleanup (xfree, str);
13717 canonical->location = new_linespec_location (&str);
13718 do_cleanups (cleanup);
13719
13720 lsal.canonical = xstrdup (event_location_to_string (canonical->location));
13721 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13722 }
13723
13724 static void
13725 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13726 struct linespec_result *canonical,
13727 char *cond_string,
13728 char *extra_string,
13729 enum bptype type_wanted,
13730 enum bpdisp disposition,
13731 int thread,
13732 int task, int ignore_count,
13733 const struct breakpoint_ops *ops,
13734 int from_tty, int enabled,
13735 int internal, unsigned flags)
13736 {
13737 int i;
13738 struct linespec_sals *lsal = VEC_index (linespec_sals,
13739 canonical->sals, 0);
13740
13741 /* If the user is creating a static tracepoint by marker id
13742 (strace -m MARKER_ID), then store the sals index, so that
13743 breakpoint_re_set can try to match up which of the newly
13744 found markers corresponds to this one, and, don't try to
13745 expand multiple locations for each sal, given than SALS
13746 already should contain all sals for MARKER_ID. */
13747
13748 for (i = 0; i < lsal->sals.nelts; ++i)
13749 {
13750 struct symtabs_and_lines expanded;
13751 struct tracepoint *tp;
13752 struct cleanup *old_chain;
13753 struct event_location *location;
13754
13755 expanded.nelts = 1;
13756 expanded.sals = &lsal->sals.sals[i];
13757
13758 location = copy_event_location (canonical->location);
13759 old_chain = make_cleanup_delete_event_location (location);
13760
13761 tp = XCNEW (struct tracepoint);
13762 init_breakpoint_sal (&tp->base, gdbarch, expanded,
13763 location, NULL,
13764 cond_string, extra_string,
13765 type_wanted, disposition,
13766 thread, task, ignore_count, ops,
13767 from_tty, enabled, internal, flags,
13768 canonical->special_display);
13769 /* Given that its possible to have multiple markers with
13770 the same string id, if the user is creating a static
13771 tracepoint by marker id ("strace -m MARKER_ID"), then
13772 store the sals index, so that breakpoint_re_set can
13773 try to match up which of the newly found markers
13774 corresponds to this one */
13775 tp->static_trace_marker_id_idx = i;
13776
13777 install_breakpoint (internal, &tp->base, 0);
13778
13779 discard_cleanups (old_chain);
13780 }
13781 }
13782
13783 static void
13784 strace_marker_decode_location (struct breakpoint *b,
13785 const struct event_location *location,
13786 struct program_space *search_pspace,
13787 struct symtabs_and_lines *sals)
13788 {
13789 struct tracepoint *tp = (struct tracepoint *) b;
13790 const char *s = get_linespec_location (location);
13791
13792 *sals = decode_static_tracepoint_spec (&s);
13793 if (sals->nelts > tp->static_trace_marker_id_idx)
13794 {
13795 sals->sals[0] = sals->sals[tp->static_trace_marker_id_idx];
13796 sals->nelts = 1;
13797 }
13798 else
13799 error (_("marker %s not found"), tp->static_trace_marker_id);
13800 }
13801
13802 static struct breakpoint_ops strace_marker_breakpoint_ops;
13803
13804 static int
13805 strace_marker_p (struct breakpoint *b)
13806 {
13807 return b->ops == &strace_marker_breakpoint_ops;
13808 }
13809
13810 /* Delete a breakpoint and clean up all traces of it in the data
13811 structures. */
13812
13813 void
13814 delete_breakpoint (struct breakpoint *bpt)
13815 {
13816 struct breakpoint *b;
13817
13818 gdb_assert (bpt != NULL);
13819
13820 /* Has this bp already been deleted? This can happen because
13821 multiple lists can hold pointers to bp's. bpstat lists are
13822 especial culprits.
13823
13824 One example of this happening is a watchpoint's scope bp. When
13825 the scope bp triggers, we notice that the watchpoint is out of
13826 scope, and delete it. We also delete its scope bp. But the
13827 scope bp is marked "auto-deleting", and is already on a bpstat.
13828 That bpstat is then checked for auto-deleting bp's, which are
13829 deleted.
13830
13831 A real solution to this problem might involve reference counts in
13832 bp's, and/or giving them pointers back to their referencing
13833 bpstat's, and teaching delete_breakpoint to only free a bp's
13834 storage when no more references were extent. A cheaper bandaid
13835 was chosen. */
13836 if (bpt->type == bp_none)
13837 return;
13838
13839 /* At least avoid this stale reference until the reference counting
13840 of breakpoints gets resolved. */
13841 if (bpt->related_breakpoint != bpt)
13842 {
13843 struct breakpoint *related;
13844 struct watchpoint *w;
13845
13846 if (bpt->type == bp_watchpoint_scope)
13847 w = (struct watchpoint *) bpt->related_breakpoint;
13848 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
13849 w = (struct watchpoint *) bpt;
13850 else
13851 w = NULL;
13852 if (w != NULL)
13853 watchpoint_del_at_next_stop (w);
13854
13855 /* Unlink bpt from the bpt->related_breakpoint ring. */
13856 for (related = bpt; related->related_breakpoint != bpt;
13857 related = related->related_breakpoint);
13858 related->related_breakpoint = bpt->related_breakpoint;
13859 bpt->related_breakpoint = bpt;
13860 }
13861
13862 /* watch_command_1 creates a watchpoint but only sets its number if
13863 update_watchpoint succeeds in creating its bp_locations. If there's
13864 a problem in that process, we'll be asked to delete the half-created
13865 watchpoint. In that case, don't announce the deletion. */
13866 if (bpt->number)
13867 observer_notify_breakpoint_deleted (bpt);
13868
13869 if (breakpoint_chain == bpt)
13870 breakpoint_chain = bpt->next;
13871
13872 ALL_BREAKPOINTS (b)
13873 if (b->next == bpt)
13874 {
13875 b->next = bpt->next;
13876 break;
13877 }
13878
13879 /* Be sure no bpstat's are pointing at the breakpoint after it's
13880 been freed. */
13881 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
13882 in all threads for now. Note that we cannot just remove bpstats
13883 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13884 commands are associated with the bpstat; if we remove it here,
13885 then the later call to bpstat_do_actions (&stop_bpstat); in
13886 event-top.c won't do anything, and temporary breakpoints with
13887 commands won't work. */
13888
13889 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13890
13891 /* Now that breakpoint is removed from breakpoint list, update the
13892 global location list. This will remove locations that used to
13893 belong to this breakpoint. Do this before freeing the breakpoint
13894 itself, since remove_breakpoint looks at location's owner. It
13895 might be better design to have location completely
13896 self-contained, but it's not the case now. */
13897 update_global_location_list (UGLL_DONT_INSERT);
13898
13899 bpt->ops->dtor (bpt);
13900 /* On the chance that someone will soon try again to delete this
13901 same bp, we mark it as deleted before freeing its storage. */
13902 bpt->type = bp_none;
13903 xfree (bpt);
13904 }
13905
13906 static void
13907 do_delete_breakpoint_cleanup (void *b)
13908 {
13909 delete_breakpoint ((struct breakpoint *) b);
13910 }
13911
13912 struct cleanup *
13913 make_cleanup_delete_breakpoint (struct breakpoint *b)
13914 {
13915 return make_cleanup (do_delete_breakpoint_cleanup, b);
13916 }
13917
13918 /* Iterator function to call a user-provided callback function once
13919 for each of B and its related breakpoints. */
13920
13921 static void
13922 iterate_over_related_breakpoints (struct breakpoint *b,
13923 void (*function) (struct breakpoint *,
13924 void *),
13925 void *data)
13926 {
13927 struct breakpoint *related;
13928
13929 related = b;
13930 do
13931 {
13932 struct breakpoint *next;
13933
13934 /* FUNCTION may delete RELATED. */
13935 next = related->related_breakpoint;
13936
13937 if (next == related)
13938 {
13939 /* RELATED is the last ring entry. */
13940 function (related, data);
13941
13942 /* FUNCTION may have deleted it, so we'd never reach back to
13943 B. There's nothing left to do anyway, so just break
13944 out. */
13945 break;
13946 }
13947 else
13948 function (related, data);
13949
13950 related = next;
13951 }
13952 while (related != b);
13953 }
13954
13955 static void
13956 do_delete_breakpoint (struct breakpoint *b, void *ignore)
13957 {
13958 delete_breakpoint (b);
13959 }
13960
13961 /* A callback for map_breakpoint_numbers that calls
13962 delete_breakpoint. */
13963
13964 static void
13965 do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
13966 {
13967 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
13968 }
13969
13970 void
13971 delete_command (char *arg, int from_tty)
13972 {
13973 struct breakpoint *b, *b_tmp;
13974
13975 dont_repeat ();
13976
13977 if (arg == 0)
13978 {
13979 int breaks_to_delete = 0;
13980
13981 /* Delete all breakpoints if no argument. Do not delete
13982 internal breakpoints, these have to be deleted with an
13983 explicit breakpoint number argument. */
13984 ALL_BREAKPOINTS (b)
13985 if (user_breakpoint_p (b))
13986 {
13987 breaks_to_delete = 1;
13988 break;
13989 }
13990
13991 /* Ask user only if there are some breakpoints to delete. */
13992 if (!from_tty
13993 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
13994 {
13995 ALL_BREAKPOINTS_SAFE (b, b_tmp)
13996 if (user_breakpoint_p (b))
13997 delete_breakpoint (b);
13998 }
13999 }
14000 else
14001 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
14002 }
14003
14004 /* Return true if all locations of B bound to PSPACE are pending. If
14005 PSPACE is NULL, all locations of all program spaces are
14006 considered. */
14007
14008 static int
14009 all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
14010 {
14011 struct bp_location *loc;
14012
14013 for (loc = b->loc; loc != NULL; loc = loc->next)
14014 if ((pspace == NULL
14015 || loc->pspace == pspace)
14016 && !loc->shlib_disabled
14017 && !loc->pspace->executing_startup)
14018 return 0;
14019 return 1;
14020 }
14021
14022 /* Subroutine of update_breakpoint_locations to simplify it.
14023 Return non-zero if multiple fns in list LOC have the same name.
14024 Null names are ignored. */
14025
14026 static int
14027 ambiguous_names_p (struct bp_location *loc)
14028 {
14029 struct bp_location *l;
14030 htab_t htab = htab_create_alloc (13, htab_hash_string,
14031 (int (*) (const void *,
14032 const void *)) streq,
14033 NULL, xcalloc, xfree);
14034
14035 for (l = loc; l != NULL; l = l->next)
14036 {
14037 const char **slot;
14038 const char *name = l->function_name;
14039
14040 /* Allow for some names to be NULL, ignore them. */
14041 if (name == NULL)
14042 continue;
14043
14044 slot = (const char **) htab_find_slot (htab, (const void *) name,
14045 INSERT);
14046 /* NOTE: We can assume slot != NULL here because xcalloc never
14047 returns NULL. */
14048 if (*slot != NULL)
14049 {
14050 htab_delete (htab);
14051 return 1;
14052 }
14053 *slot = name;
14054 }
14055
14056 htab_delete (htab);
14057 return 0;
14058 }
14059
14060 /* When symbols change, it probably means the sources changed as well,
14061 and it might mean the static tracepoint markers are no longer at
14062 the same address or line numbers they used to be at last we
14063 checked. Losing your static tracepoints whenever you rebuild is
14064 undesirable. This function tries to resync/rematch gdb static
14065 tracepoints with the markers on the target, for static tracepoints
14066 that have not been set by marker id. Static tracepoint that have
14067 been set by marker id are reset by marker id in breakpoint_re_set.
14068 The heuristic is:
14069
14070 1) For a tracepoint set at a specific address, look for a marker at
14071 the old PC. If one is found there, assume to be the same marker.
14072 If the name / string id of the marker found is different from the
14073 previous known name, assume that means the user renamed the marker
14074 in the sources, and output a warning.
14075
14076 2) For a tracepoint set at a given line number, look for a marker
14077 at the new address of the old line number. If one is found there,
14078 assume to be the same marker. If the name / string id of the
14079 marker found is different from the previous known name, assume that
14080 means the user renamed the marker in the sources, and output a
14081 warning.
14082
14083 3) If a marker is no longer found at the same address or line, it
14084 may mean the marker no longer exists. But it may also just mean
14085 the code changed a bit. Maybe the user added a few lines of code
14086 that made the marker move up or down (in line number terms). Ask
14087 the target for info about the marker with the string id as we knew
14088 it. If found, update line number and address in the matching
14089 static tracepoint. This will get confused if there's more than one
14090 marker with the same ID (possible in UST, although unadvised
14091 precisely because it confuses tools). */
14092
14093 static struct symtab_and_line
14094 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
14095 {
14096 struct tracepoint *tp = (struct tracepoint *) b;
14097 struct static_tracepoint_marker marker;
14098 CORE_ADDR pc;
14099
14100 pc = sal.pc;
14101 if (sal.line)
14102 find_line_pc (sal.symtab, sal.line, &pc);
14103
14104 if (target_static_tracepoint_marker_at (pc, &marker))
14105 {
14106 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
14107 warning (_("static tracepoint %d changed probed marker from %s to %s"),
14108 b->number,
14109 tp->static_trace_marker_id, marker.str_id);
14110
14111 xfree (tp->static_trace_marker_id);
14112 tp->static_trace_marker_id = xstrdup (marker.str_id);
14113 release_static_tracepoint_marker (&marker);
14114
14115 return sal;
14116 }
14117
14118 /* Old marker wasn't found on target at lineno. Try looking it up
14119 by string ID. */
14120 if (!sal.explicit_pc
14121 && sal.line != 0
14122 && sal.symtab != NULL
14123 && tp->static_trace_marker_id != NULL)
14124 {
14125 VEC(static_tracepoint_marker_p) *markers;
14126
14127 markers
14128 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
14129
14130 if (!VEC_empty(static_tracepoint_marker_p, markers))
14131 {
14132 struct symtab_and_line sal2;
14133 struct symbol *sym;
14134 struct static_tracepoint_marker *tpmarker;
14135 struct ui_out *uiout = current_uiout;
14136 struct explicit_location explicit_loc;
14137
14138 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
14139
14140 xfree (tp->static_trace_marker_id);
14141 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
14142
14143 warning (_("marker for static tracepoint %d (%s) not "
14144 "found at previous line number"),
14145 b->number, tp->static_trace_marker_id);
14146
14147 init_sal (&sal2);
14148
14149 sal2.pc = tpmarker->address;
14150
14151 sal2 = find_pc_line (tpmarker->address, 0);
14152 sym = find_pc_sect_function (tpmarker->address, NULL);
14153 ui_out_text (uiout, "Now in ");
14154 if (sym)
14155 {
14156 ui_out_field_string (uiout, "func",
14157 SYMBOL_PRINT_NAME (sym));
14158 ui_out_text (uiout, " at ");
14159 }
14160 ui_out_field_string (uiout, "file",
14161 symtab_to_filename_for_display (sal2.symtab));
14162 ui_out_text (uiout, ":");
14163
14164 if (ui_out_is_mi_like_p (uiout))
14165 {
14166 const char *fullname = symtab_to_fullname (sal2.symtab);
14167
14168 ui_out_field_string (uiout, "fullname", fullname);
14169 }
14170
14171 ui_out_field_int (uiout, "line", sal2.line);
14172 ui_out_text (uiout, "\n");
14173
14174 b->loc->line_number = sal2.line;
14175 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
14176
14177 delete_event_location (b->location);
14178 initialize_explicit_location (&explicit_loc);
14179 explicit_loc.source_filename
14180 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
14181 explicit_loc.line_offset.offset = b->loc->line_number;
14182 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
14183 b->location = new_explicit_location (&explicit_loc);
14184
14185 /* Might be nice to check if function changed, and warn if
14186 so. */
14187
14188 release_static_tracepoint_marker (tpmarker);
14189 }
14190 }
14191 return sal;
14192 }
14193
14194 /* Returns 1 iff locations A and B are sufficiently same that
14195 we don't need to report breakpoint as changed. */
14196
14197 static int
14198 locations_are_equal (struct bp_location *a, struct bp_location *b)
14199 {
14200 while (a && b)
14201 {
14202 if (a->address != b->address)
14203 return 0;
14204
14205 if (a->shlib_disabled != b->shlib_disabled)
14206 return 0;
14207
14208 if (a->enabled != b->enabled)
14209 return 0;
14210
14211 a = a->next;
14212 b = b->next;
14213 }
14214
14215 if ((a == NULL) != (b == NULL))
14216 return 0;
14217
14218 return 1;
14219 }
14220
14221 /* Split all locations of B that are bound to PSPACE out of B's
14222 location list to a separate list and return that list's head. If
14223 PSPACE is NULL, hoist out all locations of B. */
14224
14225 static struct bp_location *
14226 hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
14227 {
14228 struct bp_location head;
14229 struct bp_location *i = b->loc;
14230 struct bp_location **i_link = &b->loc;
14231 struct bp_location *hoisted = &head;
14232
14233 if (pspace == NULL)
14234 {
14235 i = b->loc;
14236 b->loc = NULL;
14237 return i;
14238 }
14239
14240 head.next = NULL;
14241
14242 while (i != NULL)
14243 {
14244 if (i->pspace == pspace)
14245 {
14246 *i_link = i->next;
14247 i->next = NULL;
14248 hoisted->next = i;
14249 hoisted = i;
14250 }
14251 else
14252 i_link = &i->next;
14253 i = *i_link;
14254 }
14255
14256 return head.next;
14257 }
14258
14259 /* Create new breakpoint locations for B (a hardware or software
14260 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
14261 zero, then B is a ranged breakpoint. Only recreates locations for
14262 FILTER_PSPACE. Locations of other program spaces are left
14263 untouched. */
14264
14265 void
14266 update_breakpoint_locations (struct breakpoint *b,
14267 struct program_space *filter_pspace,
14268 struct symtabs_and_lines sals,
14269 struct symtabs_and_lines sals_end)
14270 {
14271 int i;
14272 struct bp_location *existing_locations;
14273
14274 if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
14275 {
14276 /* Ranged breakpoints have only one start location and one end
14277 location. */
14278 b->enable_state = bp_disabled;
14279 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
14280 "multiple locations found\n"),
14281 b->number);
14282 return;
14283 }
14284
14285 /* If there's no new locations, and all existing locations are
14286 pending, don't do anything. This optimizes the common case where
14287 all locations are in the same shared library, that was unloaded.
14288 We'd like to retain the location, so that when the library is
14289 loaded again, we don't loose the enabled/disabled status of the
14290 individual locations. */
14291 if (all_locations_are_pending (b, filter_pspace) && sals.nelts == 0)
14292 return;
14293
14294 existing_locations = hoist_existing_locations (b, filter_pspace);
14295
14296 for (i = 0; i < sals.nelts; ++i)
14297 {
14298 struct bp_location *new_loc;
14299
14300 switch_to_program_space_and_thread (sals.sals[i].pspace);
14301
14302 new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
14303
14304 /* Reparse conditions, they might contain references to the
14305 old symtab. */
14306 if (b->cond_string != NULL)
14307 {
14308 const char *s;
14309
14310 s = b->cond_string;
14311 TRY
14312 {
14313 new_loc->cond = parse_exp_1 (&s, sals.sals[i].pc,
14314 block_for_pc (sals.sals[i].pc),
14315 0);
14316 }
14317 CATCH (e, RETURN_MASK_ERROR)
14318 {
14319 warning (_("failed to reevaluate condition "
14320 "for breakpoint %d: %s"),
14321 b->number, e.message);
14322 new_loc->enabled = 0;
14323 }
14324 END_CATCH
14325 }
14326
14327 if (sals_end.nelts)
14328 {
14329 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
14330
14331 new_loc->length = end - sals.sals[0].pc + 1;
14332 }
14333 }
14334
14335 /* If possible, carry over 'disable' status from existing
14336 breakpoints. */
14337 {
14338 struct bp_location *e = existing_locations;
14339 /* If there are multiple breakpoints with the same function name,
14340 e.g. for inline functions, comparing function names won't work.
14341 Instead compare pc addresses; this is just a heuristic as things
14342 may have moved, but in practice it gives the correct answer
14343 often enough until a better solution is found. */
14344 int have_ambiguous_names = ambiguous_names_p (b->loc);
14345
14346 for (; e; e = e->next)
14347 {
14348 if (!e->enabled && e->function_name)
14349 {
14350 struct bp_location *l = b->loc;
14351 if (have_ambiguous_names)
14352 {
14353 for (; l; l = l->next)
14354 if (breakpoint_locations_match (e, l))
14355 {
14356 l->enabled = 0;
14357 break;
14358 }
14359 }
14360 else
14361 {
14362 for (; l; l = l->next)
14363 if (l->function_name
14364 && strcmp (e->function_name, l->function_name) == 0)
14365 {
14366 l->enabled = 0;
14367 break;
14368 }
14369 }
14370 }
14371 }
14372 }
14373
14374 if (!locations_are_equal (existing_locations, b->loc))
14375 observer_notify_breakpoint_modified (b);
14376 }
14377
14378 /* Find the SaL locations corresponding to the given LOCATION.
14379 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
14380
14381 static struct symtabs_and_lines
14382 location_to_sals (struct breakpoint *b, struct event_location *location,
14383 struct program_space *search_pspace, int *found)
14384 {
14385 struct symtabs_and_lines sals = {0};
14386 struct gdb_exception exception = exception_none;
14387
14388 gdb_assert (b->ops != NULL);
14389
14390 TRY
14391 {
14392 b->ops->decode_location (b, location, search_pspace, &sals);
14393 }
14394 CATCH (e, RETURN_MASK_ERROR)
14395 {
14396 int not_found_and_ok = 0;
14397
14398 exception = e;
14399
14400 /* For pending breakpoints, it's expected that parsing will
14401 fail until the right shared library is loaded. User has
14402 already told to create pending breakpoints and don't need
14403 extra messages. If breakpoint is in bp_shlib_disabled
14404 state, then user already saw the message about that
14405 breakpoint being disabled, and don't want to see more
14406 errors. */
14407 if (e.error == NOT_FOUND_ERROR
14408 && (b->condition_not_parsed
14409 || (b->loc != NULL
14410 && search_pspace != NULL
14411 && b->loc->pspace != search_pspace)
14412 || (b->loc && b->loc->shlib_disabled)
14413 || (b->loc && b->loc->pspace->executing_startup)
14414 || b->enable_state == bp_disabled))
14415 not_found_and_ok = 1;
14416
14417 if (!not_found_and_ok)
14418 {
14419 /* We surely don't want to warn about the same breakpoint
14420 10 times. One solution, implemented here, is disable
14421 the breakpoint on error. Another solution would be to
14422 have separate 'warning emitted' flag. Since this
14423 happens only when a binary has changed, I don't know
14424 which approach is better. */
14425 b->enable_state = bp_disabled;
14426 throw_exception (e);
14427 }
14428 }
14429 END_CATCH
14430
14431 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
14432 {
14433 int i;
14434
14435 for (i = 0; i < sals.nelts; ++i)
14436 resolve_sal_pc (&sals.sals[i]);
14437 if (b->condition_not_parsed && b->extra_string != NULL)
14438 {
14439 char *cond_string, *extra_string;
14440 int thread, task;
14441
14442 find_condition_and_thread (b->extra_string, sals.sals[0].pc,
14443 &cond_string, &thread, &task,
14444 &extra_string);
14445 gdb_assert (b->cond_string == NULL);
14446 if (cond_string)
14447 b->cond_string = cond_string;
14448 b->thread = thread;
14449 b->task = task;
14450 if (extra_string)
14451 {
14452 xfree (b->extra_string);
14453 b->extra_string = extra_string;
14454 }
14455 b->condition_not_parsed = 0;
14456 }
14457
14458 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
14459 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
14460
14461 *found = 1;
14462 }
14463 else
14464 *found = 0;
14465
14466 return sals;
14467 }
14468
14469 /* The default re_set method, for typical hardware or software
14470 breakpoints. Reevaluate the breakpoint and recreate its
14471 locations. */
14472
14473 static void
14474 breakpoint_re_set_default (struct breakpoint *b)
14475 {
14476 int found;
14477 struct symtabs_and_lines sals, sals_end;
14478 struct symtabs_and_lines expanded = {0};
14479 struct symtabs_and_lines expanded_end = {0};
14480 struct program_space *filter_pspace = current_program_space;
14481
14482 sals = location_to_sals (b, b->location, filter_pspace, &found);
14483 if (found)
14484 {
14485 make_cleanup (xfree, sals.sals);
14486 expanded = sals;
14487 }
14488
14489 if (b->location_range_end != NULL)
14490 {
14491 sals_end = location_to_sals (b, b->location_range_end,
14492 filter_pspace, &found);
14493 if (found)
14494 {
14495 make_cleanup (xfree, sals_end.sals);
14496 expanded_end = sals_end;
14497 }
14498 }
14499
14500 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
14501 }
14502
14503 /* Default method for creating SALs from an address string. It basically
14504 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
14505
14506 static void
14507 create_sals_from_location_default (const struct event_location *location,
14508 struct linespec_result *canonical,
14509 enum bptype type_wanted)
14510 {
14511 parse_breakpoint_sals (location, canonical);
14512 }
14513
14514 /* Call create_breakpoints_sal for the given arguments. This is the default
14515 function for the `create_breakpoints_sal' method of
14516 breakpoint_ops. */
14517
14518 static void
14519 create_breakpoints_sal_default (struct gdbarch *gdbarch,
14520 struct linespec_result *canonical,
14521 char *cond_string,
14522 char *extra_string,
14523 enum bptype type_wanted,
14524 enum bpdisp disposition,
14525 int thread,
14526 int task, int ignore_count,
14527 const struct breakpoint_ops *ops,
14528 int from_tty, int enabled,
14529 int internal, unsigned flags)
14530 {
14531 create_breakpoints_sal (gdbarch, canonical, cond_string,
14532 extra_string,
14533 type_wanted, disposition,
14534 thread, task, ignore_count, ops, from_tty,
14535 enabled, internal, flags);
14536 }
14537
14538 /* Decode the line represented by S by calling decode_line_full. This is the
14539 default function for the `decode_location' method of breakpoint_ops. */
14540
14541 static void
14542 decode_location_default (struct breakpoint *b,
14543 const struct event_location *location,
14544 struct program_space *search_pspace,
14545 struct symtabs_and_lines *sals)
14546 {
14547 struct linespec_result canonical;
14548
14549 init_linespec_result (&canonical);
14550 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
14551 (struct symtab *) NULL, 0,
14552 &canonical, multiple_symbols_all,
14553 b->filter);
14554
14555 /* We should get 0 or 1 resulting SALs. */
14556 gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
14557
14558 if (VEC_length (linespec_sals, canonical.sals) > 0)
14559 {
14560 struct linespec_sals *lsal;
14561
14562 lsal = VEC_index (linespec_sals, canonical.sals, 0);
14563 *sals = lsal->sals;
14564 /* Arrange it so the destructor does not free the
14565 contents. */
14566 lsal->sals.sals = NULL;
14567 }
14568
14569 destroy_linespec_result (&canonical);
14570 }
14571
14572 /* Prepare the global context for a re-set of breakpoint B. */
14573
14574 static struct cleanup *
14575 prepare_re_set_context (struct breakpoint *b)
14576 {
14577 input_radix = b->input_radix;
14578 set_language (b->language);
14579
14580 return make_cleanup (null_cleanup, NULL);
14581 }
14582
14583 /* Reset a breakpoint given it's struct breakpoint * BINT.
14584 The value we return ends up being the return value from catch_errors.
14585 Unused in this case. */
14586
14587 static int
14588 breakpoint_re_set_one (void *bint)
14589 {
14590 /* Get past catch_errs. */
14591 struct breakpoint *b = (struct breakpoint *) bint;
14592 struct cleanup *cleanups;
14593
14594 cleanups = prepare_re_set_context (b);
14595 b->ops->re_set (b);
14596 do_cleanups (cleanups);
14597 return 0;
14598 }
14599
14600 /* Re-set breakpoint locations for the current program space.
14601 Locations bound to other program spaces are left untouched. */
14602
14603 void
14604 breakpoint_re_set (void)
14605 {
14606 struct breakpoint *b, *b_tmp;
14607 enum language save_language;
14608 int save_input_radix;
14609 struct cleanup *old_chain;
14610
14611 save_language = current_language->la_language;
14612 save_input_radix = input_radix;
14613 old_chain = save_current_space_and_thread ();
14614
14615 /* Note: we must not try to insert locations until after all
14616 breakpoints have been re-set. Otherwise, e.g., when re-setting
14617 breakpoint 1, we'd insert the locations of breakpoint 2, which
14618 hadn't been re-set yet, and thus may have stale locations. */
14619
14620 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14621 {
14622 /* Format possible error msg. */
14623 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
14624 b->number);
14625 struct cleanup *cleanups = make_cleanup (xfree, message);
14626 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
14627 do_cleanups (cleanups);
14628 }
14629 set_language (save_language);
14630 input_radix = save_input_radix;
14631
14632 jit_breakpoint_re_set ();
14633
14634 do_cleanups (old_chain);
14635
14636 create_overlay_event_breakpoint ();
14637 create_longjmp_master_breakpoint ();
14638 create_std_terminate_master_breakpoint ();
14639 create_exception_master_breakpoint ();
14640
14641 /* Now we can insert. */
14642 update_global_location_list (UGLL_MAY_INSERT);
14643 }
14644 \f
14645 /* Reset the thread number of this breakpoint:
14646
14647 - If the breakpoint is for all threads, leave it as-is.
14648 - Else, reset it to the current thread for inferior_ptid. */
14649 void
14650 breakpoint_re_set_thread (struct breakpoint *b)
14651 {
14652 if (b->thread != -1)
14653 {
14654 if (in_thread_list (inferior_ptid))
14655 b->thread = ptid_to_global_thread_id (inferior_ptid);
14656
14657 /* We're being called after following a fork. The new fork is
14658 selected as current, and unless this was a vfork will have a
14659 different program space from the original thread. Reset that
14660 as well. */
14661 b->loc->pspace = current_program_space;
14662 }
14663 }
14664
14665 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14666 If from_tty is nonzero, it prints a message to that effect,
14667 which ends with a period (no newline). */
14668
14669 void
14670 set_ignore_count (int bptnum, int count, int from_tty)
14671 {
14672 struct breakpoint *b;
14673
14674 if (count < 0)
14675 count = 0;
14676
14677 ALL_BREAKPOINTS (b)
14678 if (b->number == bptnum)
14679 {
14680 if (is_tracepoint (b))
14681 {
14682 if (from_tty && count != 0)
14683 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14684 bptnum);
14685 return;
14686 }
14687
14688 b->ignore_count = count;
14689 if (from_tty)
14690 {
14691 if (count == 0)
14692 printf_filtered (_("Will stop next time "
14693 "breakpoint %d is reached."),
14694 bptnum);
14695 else if (count == 1)
14696 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
14697 bptnum);
14698 else
14699 printf_filtered (_("Will ignore next %d "
14700 "crossings of breakpoint %d."),
14701 count, bptnum);
14702 }
14703 observer_notify_breakpoint_modified (b);
14704 return;
14705 }
14706
14707 error (_("No breakpoint number %d."), bptnum);
14708 }
14709
14710 /* Command to set ignore-count of breakpoint N to COUNT. */
14711
14712 static void
14713 ignore_command (char *args, int from_tty)
14714 {
14715 char *p = args;
14716 int num;
14717
14718 if (p == 0)
14719 error_no_arg (_("a breakpoint number"));
14720
14721 num = get_number (&p);
14722 if (num == 0)
14723 error (_("bad breakpoint number: '%s'"), args);
14724 if (*p == 0)
14725 error (_("Second argument (specified ignore-count) is missing."));
14726
14727 set_ignore_count (num,
14728 longest_to_int (value_as_long (parse_and_eval (p))),
14729 from_tty);
14730 if (from_tty)
14731 printf_filtered ("\n");
14732 }
14733 \f
14734 /* Call FUNCTION on each of the breakpoints
14735 whose numbers are given in ARGS. */
14736
14737 static void
14738 map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
14739 void *),
14740 void *data)
14741 {
14742 int num;
14743 struct breakpoint *b, *tmp;
14744 int match;
14745 struct get_number_or_range_state state;
14746
14747 if (args == 0 || *args == '\0')
14748 error_no_arg (_("one or more breakpoint numbers"));
14749
14750 init_number_or_range (&state, args);
14751
14752 while (!state.finished)
14753 {
14754 const char *p = state.string;
14755
14756 match = 0;
14757
14758 num = get_number_or_range (&state);
14759 if (num == 0)
14760 {
14761 warning (_("bad breakpoint number at or near '%s'"), p);
14762 }
14763 else
14764 {
14765 ALL_BREAKPOINTS_SAFE (b, tmp)
14766 if (b->number == num)
14767 {
14768 match = 1;
14769 function (b, data);
14770 break;
14771 }
14772 if (match == 0)
14773 printf_unfiltered (_("No breakpoint number %d.\n"), num);
14774 }
14775 }
14776 }
14777
14778 static struct bp_location *
14779 find_location_by_number (char *number)
14780 {
14781 char *dot = strchr (number, '.');
14782 char *p1;
14783 int bp_num;
14784 int loc_num;
14785 struct breakpoint *b;
14786 struct bp_location *loc;
14787
14788 *dot = '\0';
14789
14790 p1 = number;
14791 bp_num = get_number (&p1);
14792 if (bp_num == 0)
14793 error (_("Bad breakpoint number '%s'"), number);
14794
14795 ALL_BREAKPOINTS (b)
14796 if (b->number == bp_num)
14797 {
14798 break;
14799 }
14800
14801 if (!b || b->number != bp_num)
14802 error (_("Bad breakpoint number '%s'"), number);
14803
14804 p1 = dot+1;
14805 loc_num = get_number (&p1);
14806 if (loc_num == 0)
14807 error (_("Bad breakpoint location number '%s'"), number);
14808
14809 --loc_num;
14810 loc = b->loc;
14811 for (;loc_num && loc; --loc_num, loc = loc->next)
14812 ;
14813 if (!loc)
14814 error (_("Bad breakpoint location number '%s'"), dot+1);
14815
14816 return loc;
14817 }
14818
14819
14820 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14821 If from_tty is nonzero, it prints a message to that effect,
14822 which ends with a period (no newline). */
14823
14824 void
14825 disable_breakpoint (struct breakpoint *bpt)
14826 {
14827 /* Never disable a watchpoint scope breakpoint; we want to
14828 hit them when we leave scope so we can delete both the
14829 watchpoint and its scope breakpoint at that time. */
14830 if (bpt->type == bp_watchpoint_scope)
14831 return;
14832
14833 bpt->enable_state = bp_disabled;
14834
14835 /* Mark breakpoint locations modified. */
14836 mark_breakpoint_modified (bpt);
14837
14838 if (target_supports_enable_disable_tracepoint ()
14839 && current_trace_status ()->running && is_tracepoint (bpt))
14840 {
14841 struct bp_location *location;
14842
14843 for (location = bpt->loc; location; location = location->next)
14844 target_disable_tracepoint (location);
14845 }
14846
14847 update_global_location_list (UGLL_DONT_INSERT);
14848
14849 observer_notify_breakpoint_modified (bpt);
14850 }
14851
14852 /* A callback for iterate_over_related_breakpoints. */
14853
14854 static void
14855 do_disable_breakpoint (struct breakpoint *b, void *ignore)
14856 {
14857 disable_breakpoint (b);
14858 }
14859
14860 /* A callback for map_breakpoint_numbers that calls
14861 disable_breakpoint. */
14862
14863 static void
14864 do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
14865 {
14866 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
14867 }
14868
14869 static void
14870 disable_command (char *args, int from_tty)
14871 {
14872 if (args == 0)
14873 {
14874 struct breakpoint *bpt;
14875
14876 ALL_BREAKPOINTS (bpt)
14877 if (user_breakpoint_p (bpt))
14878 disable_breakpoint (bpt);
14879 }
14880 else
14881 {
14882 char *num = extract_arg (&args);
14883
14884 while (num)
14885 {
14886 if (strchr (num, '.'))
14887 {
14888 struct bp_location *loc = find_location_by_number (num);
14889
14890 if (loc)
14891 {
14892 if (loc->enabled)
14893 {
14894 loc->enabled = 0;
14895 mark_breakpoint_location_modified (loc);
14896 }
14897 if (target_supports_enable_disable_tracepoint ()
14898 && current_trace_status ()->running && loc->owner
14899 && is_tracepoint (loc->owner))
14900 target_disable_tracepoint (loc);
14901 }
14902 update_global_location_list (UGLL_DONT_INSERT);
14903 }
14904 else
14905 map_breakpoint_numbers (num, do_map_disable_breakpoint, NULL);
14906 num = extract_arg (&args);
14907 }
14908 }
14909 }
14910
14911 static void
14912 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14913 int count)
14914 {
14915 int target_resources_ok;
14916
14917 if (bpt->type == bp_hardware_breakpoint)
14918 {
14919 int i;
14920 i = hw_breakpoint_used_count ();
14921 target_resources_ok =
14922 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
14923 i + 1, 0);
14924 if (target_resources_ok == 0)
14925 error (_("No hardware breakpoint support in the target."));
14926 else if (target_resources_ok < 0)
14927 error (_("Hardware breakpoints used exceeds limit."));
14928 }
14929
14930 if (is_watchpoint (bpt))
14931 {
14932 /* Initialize it just to avoid a GCC false warning. */
14933 enum enable_state orig_enable_state = bp_disabled;
14934
14935 TRY
14936 {
14937 struct watchpoint *w = (struct watchpoint *) bpt;
14938
14939 orig_enable_state = bpt->enable_state;
14940 bpt->enable_state = bp_enabled;
14941 update_watchpoint (w, 1 /* reparse */);
14942 }
14943 CATCH (e, RETURN_MASK_ALL)
14944 {
14945 bpt->enable_state = orig_enable_state;
14946 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14947 bpt->number);
14948 return;
14949 }
14950 END_CATCH
14951 }
14952
14953 bpt->enable_state = bp_enabled;
14954
14955 /* Mark breakpoint locations modified. */
14956 mark_breakpoint_modified (bpt);
14957
14958 if (target_supports_enable_disable_tracepoint ()
14959 && current_trace_status ()->running && is_tracepoint (bpt))
14960 {
14961 struct bp_location *location;
14962
14963 for (location = bpt->loc; location; location = location->next)
14964 target_enable_tracepoint (location);
14965 }
14966
14967 bpt->disposition = disposition;
14968 bpt->enable_count = count;
14969 update_global_location_list (UGLL_MAY_INSERT);
14970
14971 observer_notify_breakpoint_modified (bpt);
14972 }
14973
14974
14975 void
14976 enable_breakpoint (struct breakpoint *bpt)
14977 {
14978 enable_breakpoint_disp (bpt, bpt->disposition, 0);
14979 }
14980
14981 static void
14982 do_enable_breakpoint (struct breakpoint *bpt, void *arg)
14983 {
14984 enable_breakpoint (bpt);
14985 }
14986
14987 /* A callback for map_breakpoint_numbers that calls
14988 enable_breakpoint. */
14989
14990 static void
14991 do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
14992 {
14993 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
14994 }
14995
14996 /* The enable command enables the specified breakpoints (or all defined
14997 breakpoints) so they once again become (or continue to be) effective
14998 in stopping the inferior. */
14999
15000 static void
15001 enable_command (char *args, int from_tty)
15002 {
15003 if (args == 0)
15004 {
15005 struct breakpoint *bpt;
15006
15007 ALL_BREAKPOINTS (bpt)
15008 if (user_breakpoint_p (bpt))
15009 enable_breakpoint (bpt);
15010 }
15011 else
15012 {
15013 char *num = extract_arg (&args);
15014
15015 while (num)
15016 {
15017 if (strchr (num, '.'))
15018 {
15019 struct bp_location *loc = find_location_by_number (num);
15020
15021 if (loc)
15022 {
15023 if (!loc->enabled)
15024 {
15025 loc->enabled = 1;
15026 mark_breakpoint_location_modified (loc);
15027 }
15028 if (target_supports_enable_disable_tracepoint ()
15029 && current_trace_status ()->running && loc->owner
15030 && is_tracepoint (loc->owner))
15031 target_enable_tracepoint (loc);
15032 }
15033 update_global_location_list (UGLL_MAY_INSERT);
15034 }
15035 else
15036 map_breakpoint_numbers (num, do_map_enable_breakpoint, NULL);
15037 num = extract_arg (&args);
15038 }
15039 }
15040 }
15041
15042 /* This struct packages up disposition data for application to multiple
15043 breakpoints. */
15044
15045 struct disp_data
15046 {
15047 enum bpdisp disp;
15048 int count;
15049 };
15050
15051 static void
15052 do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
15053 {
15054 struct disp_data disp_data = *(struct disp_data *) arg;
15055
15056 enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
15057 }
15058
15059 static void
15060 do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
15061 {
15062 struct disp_data disp = { disp_disable, 1 };
15063
15064 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
15065 }
15066
15067 static void
15068 enable_once_command (char *args, int from_tty)
15069 {
15070 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
15071 }
15072
15073 static void
15074 do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
15075 {
15076 struct disp_data disp = { disp_disable, *(int *) countptr };
15077
15078 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
15079 }
15080
15081 static void
15082 enable_count_command (char *args, int from_tty)
15083 {
15084 int count;
15085
15086 if (args == NULL)
15087 error_no_arg (_("hit count"));
15088
15089 count = get_number (&args);
15090
15091 map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
15092 }
15093
15094 static void
15095 do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
15096 {
15097 struct disp_data disp = { disp_del, 1 };
15098
15099 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
15100 }
15101
15102 static void
15103 enable_delete_command (char *args, int from_tty)
15104 {
15105 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
15106 }
15107 \f
15108 static void
15109 set_breakpoint_cmd (char *args, int from_tty)
15110 {
15111 }
15112
15113 static void
15114 show_breakpoint_cmd (char *args, int from_tty)
15115 {
15116 }
15117
15118 /* Invalidate last known value of any hardware watchpoint if
15119 the memory which that value represents has been written to by
15120 GDB itself. */
15121
15122 static void
15123 invalidate_bp_value_on_memory_change (struct inferior *inferior,
15124 CORE_ADDR addr, ssize_t len,
15125 const bfd_byte *data)
15126 {
15127 struct breakpoint *bp;
15128
15129 ALL_BREAKPOINTS (bp)
15130 if (bp->enable_state == bp_enabled
15131 && bp->type == bp_hardware_watchpoint)
15132 {
15133 struct watchpoint *wp = (struct watchpoint *) bp;
15134
15135 if (wp->val_valid && wp->val)
15136 {
15137 struct bp_location *loc;
15138
15139 for (loc = bp->loc; loc != NULL; loc = loc->next)
15140 if (loc->loc_type == bp_loc_hardware_watchpoint
15141 && loc->address + loc->length > addr
15142 && addr + len > loc->address)
15143 {
15144 value_free (wp->val);
15145 wp->val = NULL;
15146 wp->val_valid = 0;
15147 }
15148 }
15149 }
15150 }
15151
15152 /* Create and insert a breakpoint for software single step. */
15153
15154 void
15155 insert_single_step_breakpoint (struct gdbarch *gdbarch,
15156 struct address_space *aspace,
15157 CORE_ADDR next_pc)
15158 {
15159 struct thread_info *tp = inferior_thread ();
15160 struct symtab_and_line sal;
15161 CORE_ADDR pc = next_pc;
15162
15163 if (tp->control.single_step_breakpoints == NULL)
15164 {
15165 tp->control.single_step_breakpoints
15166 = new_single_step_breakpoint (tp->global_num, gdbarch);
15167 }
15168
15169 sal = find_pc_line (pc, 0);
15170 sal.pc = pc;
15171 sal.section = find_pc_overlay (pc);
15172 sal.explicit_pc = 1;
15173 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
15174
15175 update_global_location_list (UGLL_INSERT);
15176 }
15177
15178 /* See breakpoint.h. */
15179
15180 int
15181 breakpoint_has_location_inserted_here (struct breakpoint *bp,
15182 struct address_space *aspace,
15183 CORE_ADDR pc)
15184 {
15185 struct bp_location *loc;
15186
15187 for (loc = bp->loc; loc != NULL; loc = loc->next)
15188 if (loc->inserted
15189 && breakpoint_location_address_match (loc, aspace, pc))
15190 return 1;
15191
15192 return 0;
15193 }
15194
15195 /* Check whether a software single-step breakpoint is inserted at
15196 PC. */
15197
15198 int
15199 single_step_breakpoint_inserted_here_p (struct address_space *aspace,
15200 CORE_ADDR pc)
15201 {
15202 struct breakpoint *bpt;
15203
15204 ALL_BREAKPOINTS (bpt)
15205 {
15206 if (bpt->type == bp_single_step
15207 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
15208 return 1;
15209 }
15210 return 0;
15211 }
15212
15213 /* Tracepoint-specific operations. */
15214
15215 /* Set tracepoint count to NUM. */
15216 static void
15217 set_tracepoint_count (int num)
15218 {
15219 tracepoint_count = num;
15220 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
15221 }
15222
15223 static void
15224 trace_command (char *arg, int from_tty)
15225 {
15226 struct breakpoint_ops *ops;
15227 struct event_location *location;
15228 struct cleanup *back_to;
15229
15230 location = string_to_event_location (&arg, current_language);
15231 back_to = make_cleanup_delete_event_location (location);
15232 if (location != NULL
15233 && event_location_type (location) == PROBE_LOCATION)
15234 ops = &tracepoint_probe_breakpoint_ops;
15235 else
15236 ops = &tracepoint_breakpoint_ops;
15237
15238 create_breakpoint (get_current_arch (),
15239 location,
15240 NULL, 0, arg, 1 /* parse arg */,
15241 0 /* tempflag */,
15242 bp_tracepoint /* type_wanted */,
15243 0 /* Ignore count */,
15244 pending_break_support,
15245 ops,
15246 from_tty,
15247 1 /* enabled */,
15248 0 /* internal */, 0);
15249 do_cleanups (back_to);
15250 }
15251
15252 static void
15253 ftrace_command (char *arg, int from_tty)
15254 {
15255 struct event_location *location;
15256 struct cleanup *back_to;
15257
15258 location = string_to_event_location (&arg, current_language);
15259 back_to = make_cleanup_delete_event_location (location);
15260 create_breakpoint (get_current_arch (),
15261 location,
15262 NULL, 0, arg, 1 /* parse arg */,
15263 0 /* tempflag */,
15264 bp_fast_tracepoint /* type_wanted */,
15265 0 /* Ignore count */,
15266 pending_break_support,
15267 &tracepoint_breakpoint_ops,
15268 from_tty,
15269 1 /* enabled */,
15270 0 /* internal */, 0);
15271 do_cleanups (back_to);
15272 }
15273
15274 /* strace command implementation. Creates a static tracepoint. */
15275
15276 static void
15277 strace_command (char *arg, int from_tty)
15278 {
15279 struct breakpoint_ops *ops;
15280 struct event_location *location;
15281 struct cleanup *back_to;
15282
15283 /* Decide if we are dealing with a static tracepoint marker (`-m'),
15284 or with a normal static tracepoint. */
15285 if (arg && startswith (arg, "-m") && isspace (arg[2]))
15286 {
15287 ops = &strace_marker_breakpoint_ops;
15288 location = new_linespec_location (&arg);
15289 }
15290 else
15291 {
15292 ops = &tracepoint_breakpoint_ops;
15293 location = string_to_event_location (&arg, current_language);
15294 }
15295
15296 back_to = make_cleanup_delete_event_location (location);
15297 create_breakpoint (get_current_arch (),
15298 location,
15299 NULL, 0, arg, 1 /* parse arg */,
15300 0 /* tempflag */,
15301 bp_static_tracepoint /* type_wanted */,
15302 0 /* Ignore count */,
15303 pending_break_support,
15304 ops,
15305 from_tty,
15306 1 /* enabled */,
15307 0 /* internal */, 0);
15308 do_cleanups (back_to);
15309 }
15310
15311 /* Set up a fake reader function that gets command lines from a linked
15312 list that was acquired during tracepoint uploading. */
15313
15314 static struct uploaded_tp *this_utp;
15315 static int next_cmd;
15316
15317 static char *
15318 read_uploaded_action (void)
15319 {
15320 char *rslt;
15321
15322 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
15323
15324 next_cmd++;
15325
15326 return rslt;
15327 }
15328
15329 /* Given information about a tracepoint as recorded on a target (which
15330 can be either a live system or a trace file), attempt to create an
15331 equivalent GDB tracepoint. This is not a reliable process, since
15332 the target does not necessarily have all the information used when
15333 the tracepoint was originally defined. */
15334
15335 struct tracepoint *
15336 create_tracepoint_from_upload (struct uploaded_tp *utp)
15337 {
15338 char *addr_str, small_buf[100];
15339 struct tracepoint *tp;
15340 struct event_location *location;
15341 struct cleanup *cleanup;
15342
15343 if (utp->at_string)
15344 addr_str = utp->at_string;
15345 else
15346 {
15347 /* In the absence of a source location, fall back to raw
15348 address. Since there is no way to confirm that the address
15349 means the same thing as when the trace was started, warn the
15350 user. */
15351 warning (_("Uploaded tracepoint %d has no "
15352 "source location, using raw address"),
15353 utp->number);
15354 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
15355 addr_str = small_buf;
15356 }
15357
15358 /* There's not much we can do with a sequence of bytecodes. */
15359 if (utp->cond && !utp->cond_string)
15360 warning (_("Uploaded tracepoint %d condition "
15361 "has no source form, ignoring it"),
15362 utp->number);
15363
15364 location = string_to_event_location (&addr_str, current_language);
15365 cleanup = make_cleanup_delete_event_location (location);
15366 if (!create_breakpoint (get_current_arch (),
15367 location,
15368 utp->cond_string, -1, addr_str,
15369 0 /* parse cond/thread */,
15370 0 /* tempflag */,
15371 utp->type /* type_wanted */,
15372 0 /* Ignore count */,
15373 pending_break_support,
15374 &tracepoint_breakpoint_ops,
15375 0 /* from_tty */,
15376 utp->enabled /* enabled */,
15377 0 /* internal */,
15378 CREATE_BREAKPOINT_FLAGS_INSERTED))
15379 {
15380 do_cleanups (cleanup);
15381 return NULL;
15382 }
15383
15384 do_cleanups (cleanup);
15385
15386 /* Get the tracepoint we just created. */
15387 tp = get_tracepoint (tracepoint_count);
15388 gdb_assert (tp != NULL);
15389
15390 if (utp->pass > 0)
15391 {
15392 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
15393 tp->base.number);
15394
15395 trace_pass_command (small_buf, 0);
15396 }
15397
15398 /* If we have uploaded versions of the original commands, set up a
15399 special-purpose "reader" function and call the usual command line
15400 reader, then pass the result to the breakpoint command-setting
15401 function. */
15402 if (!VEC_empty (char_ptr, utp->cmd_strings))
15403 {
15404 struct command_line *cmd_list;
15405
15406 this_utp = utp;
15407 next_cmd = 0;
15408
15409 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
15410
15411 breakpoint_set_commands (&tp->base, cmd_list);
15412 }
15413 else if (!VEC_empty (char_ptr, utp->actions)
15414 || !VEC_empty (char_ptr, utp->step_actions))
15415 warning (_("Uploaded tracepoint %d actions "
15416 "have no source form, ignoring them"),
15417 utp->number);
15418
15419 /* Copy any status information that might be available. */
15420 tp->base.hit_count = utp->hit_count;
15421 tp->traceframe_usage = utp->traceframe_usage;
15422
15423 return tp;
15424 }
15425
15426 /* Print information on tracepoint number TPNUM_EXP, or all if
15427 omitted. */
15428
15429 static void
15430 tracepoints_info (char *args, int from_tty)
15431 {
15432 struct ui_out *uiout = current_uiout;
15433 int num_printed;
15434
15435 num_printed = breakpoint_1 (args, 0, is_tracepoint);
15436
15437 if (num_printed == 0)
15438 {
15439 if (args == NULL || *args == '\0')
15440 ui_out_message (uiout, 0, "No tracepoints.\n");
15441 else
15442 ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
15443 }
15444
15445 default_collect_info ();
15446 }
15447
15448 /* The 'enable trace' command enables tracepoints.
15449 Not supported by all targets. */
15450 static void
15451 enable_trace_command (char *args, int from_tty)
15452 {
15453 enable_command (args, from_tty);
15454 }
15455
15456 /* The 'disable trace' command disables tracepoints.
15457 Not supported by all targets. */
15458 static void
15459 disable_trace_command (char *args, int from_tty)
15460 {
15461 disable_command (args, from_tty);
15462 }
15463
15464 /* Remove a tracepoint (or all if no argument). */
15465 static void
15466 delete_trace_command (char *arg, int from_tty)
15467 {
15468 struct breakpoint *b, *b_tmp;
15469
15470 dont_repeat ();
15471
15472 if (arg == 0)
15473 {
15474 int breaks_to_delete = 0;
15475
15476 /* Delete all breakpoints if no argument.
15477 Do not delete internal or call-dummy breakpoints, these
15478 have to be deleted with an explicit breakpoint number
15479 argument. */
15480 ALL_TRACEPOINTS (b)
15481 if (is_tracepoint (b) && user_breakpoint_p (b))
15482 {
15483 breaks_to_delete = 1;
15484 break;
15485 }
15486
15487 /* Ask user only if there are some breakpoints to delete. */
15488 if (!from_tty
15489 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
15490 {
15491 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15492 if (is_tracepoint (b) && user_breakpoint_p (b))
15493 delete_breakpoint (b);
15494 }
15495 }
15496 else
15497 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
15498 }
15499
15500 /* Helper function for trace_pass_command. */
15501
15502 static void
15503 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
15504 {
15505 tp->pass_count = count;
15506 observer_notify_breakpoint_modified (&tp->base);
15507 if (from_tty)
15508 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
15509 tp->base.number, count);
15510 }
15511
15512 /* Set passcount for tracepoint.
15513
15514 First command argument is passcount, second is tracepoint number.
15515 If tracepoint number omitted, apply to most recently defined.
15516 Also accepts special argument "all". */
15517
15518 static void
15519 trace_pass_command (char *args, int from_tty)
15520 {
15521 struct tracepoint *t1;
15522 unsigned int count;
15523
15524 if (args == 0 || *args == 0)
15525 error (_("passcount command requires an "
15526 "argument (count + optional TP num)"));
15527
15528 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
15529
15530 args = skip_spaces (args);
15531 if (*args && strncasecmp (args, "all", 3) == 0)
15532 {
15533 struct breakpoint *b;
15534
15535 args += 3; /* Skip special argument "all". */
15536 if (*args)
15537 error (_("Junk at end of arguments."));
15538
15539 ALL_TRACEPOINTS (b)
15540 {
15541 t1 = (struct tracepoint *) b;
15542 trace_pass_set_count (t1, count, from_tty);
15543 }
15544 }
15545 else if (*args == '\0')
15546 {
15547 t1 = get_tracepoint_by_number (&args, NULL);
15548 if (t1)
15549 trace_pass_set_count (t1, count, from_tty);
15550 }
15551 else
15552 {
15553 struct get_number_or_range_state state;
15554
15555 init_number_or_range (&state, args);
15556 while (!state.finished)
15557 {
15558 t1 = get_tracepoint_by_number (&args, &state);
15559 if (t1)
15560 trace_pass_set_count (t1, count, from_tty);
15561 }
15562 }
15563 }
15564
15565 struct tracepoint *
15566 get_tracepoint (int num)
15567 {
15568 struct breakpoint *t;
15569
15570 ALL_TRACEPOINTS (t)
15571 if (t->number == num)
15572 return (struct tracepoint *) t;
15573
15574 return NULL;
15575 }
15576
15577 /* Find the tracepoint with the given target-side number (which may be
15578 different from the tracepoint number after disconnecting and
15579 reconnecting). */
15580
15581 struct tracepoint *
15582 get_tracepoint_by_number_on_target (int num)
15583 {
15584 struct breakpoint *b;
15585
15586 ALL_TRACEPOINTS (b)
15587 {
15588 struct tracepoint *t = (struct tracepoint *) b;
15589
15590 if (t->number_on_target == num)
15591 return t;
15592 }
15593
15594 return NULL;
15595 }
15596
15597 /* Utility: parse a tracepoint number and look it up in the list.
15598 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
15599 If the argument is missing, the most recent tracepoint
15600 (tracepoint_count) is returned. */
15601
15602 struct tracepoint *
15603 get_tracepoint_by_number (char **arg,
15604 struct get_number_or_range_state *state)
15605 {
15606 struct breakpoint *t;
15607 int tpnum;
15608 char *instring = arg == NULL ? NULL : *arg;
15609
15610 if (state)
15611 {
15612 gdb_assert (!state->finished);
15613 tpnum = get_number_or_range (state);
15614 }
15615 else if (arg == NULL || *arg == NULL || ! **arg)
15616 tpnum = tracepoint_count;
15617 else
15618 tpnum = get_number (arg);
15619
15620 if (tpnum <= 0)
15621 {
15622 if (instring && *instring)
15623 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15624 instring);
15625 else
15626 printf_filtered (_("No previous tracepoint\n"));
15627 return NULL;
15628 }
15629
15630 ALL_TRACEPOINTS (t)
15631 if (t->number == tpnum)
15632 {
15633 return (struct tracepoint *) t;
15634 }
15635
15636 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15637 return NULL;
15638 }
15639
15640 void
15641 print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15642 {
15643 if (b->thread != -1)
15644 fprintf_unfiltered (fp, " thread %d", b->thread);
15645
15646 if (b->task != 0)
15647 fprintf_unfiltered (fp, " task %d", b->task);
15648
15649 fprintf_unfiltered (fp, "\n");
15650 }
15651
15652 /* Save information on user settable breakpoints (watchpoints, etc) to
15653 a new script file named FILENAME. If FILTER is non-NULL, call it
15654 on each breakpoint and only include the ones for which it returns
15655 non-zero. */
15656
15657 static void
15658 save_breakpoints (char *filename, int from_tty,
15659 int (*filter) (const struct breakpoint *))
15660 {
15661 struct breakpoint *tp;
15662 int any = 0;
15663 struct cleanup *cleanup;
15664 struct ui_file *fp;
15665 int extra_trace_bits = 0;
15666
15667 if (filename == 0 || *filename == 0)
15668 error (_("Argument required (file name in which to save)"));
15669
15670 /* See if we have anything to save. */
15671 ALL_BREAKPOINTS (tp)
15672 {
15673 /* Skip internal and momentary breakpoints. */
15674 if (!user_breakpoint_p (tp))
15675 continue;
15676
15677 /* If we have a filter, only save the breakpoints it accepts. */
15678 if (filter && !filter (tp))
15679 continue;
15680
15681 any = 1;
15682
15683 if (is_tracepoint (tp))
15684 {
15685 extra_trace_bits = 1;
15686
15687 /* We can stop searching. */
15688 break;
15689 }
15690 }
15691
15692 if (!any)
15693 {
15694 warning (_("Nothing to save."));
15695 return;
15696 }
15697
15698 filename = tilde_expand (filename);
15699 cleanup = make_cleanup (xfree, filename);
15700 fp = gdb_fopen (filename, "w");
15701 if (!fp)
15702 error (_("Unable to open file '%s' for saving (%s)"),
15703 filename, safe_strerror (errno));
15704 make_cleanup_ui_file_delete (fp);
15705
15706 if (extra_trace_bits)
15707 save_trace_state_variables (fp);
15708
15709 ALL_BREAKPOINTS (tp)
15710 {
15711 /* Skip internal and momentary breakpoints. */
15712 if (!user_breakpoint_p (tp))
15713 continue;
15714
15715 /* If we have a filter, only save the breakpoints it accepts. */
15716 if (filter && !filter (tp))
15717 continue;
15718
15719 tp->ops->print_recreate (tp, fp);
15720
15721 /* Note, we can't rely on tp->number for anything, as we can't
15722 assume the recreated breakpoint numbers will match. Use $bpnum
15723 instead. */
15724
15725 if (tp->cond_string)
15726 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
15727
15728 if (tp->ignore_count)
15729 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
15730
15731 if (tp->type != bp_dprintf && tp->commands)
15732 {
15733 struct gdb_exception exception;
15734
15735 fprintf_unfiltered (fp, " commands\n");
15736
15737 ui_out_redirect (current_uiout, fp);
15738 TRY
15739 {
15740 print_command_lines (current_uiout, tp->commands->commands, 2);
15741 }
15742 CATCH (ex, RETURN_MASK_ALL)
15743 {
15744 ui_out_redirect (current_uiout, NULL);
15745 throw_exception (ex);
15746 }
15747 END_CATCH
15748
15749 ui_out_redirect (current_uiout, NULL);
15750 fprintf_unfiltered (fp, " end\n");
15751 }
15752
15753 if (tp->enable_state == bp_disabled)
15754 fprintf_unfiltered (fp, "disable $bpnum\n");
15755
15756 /* If this is a multi-location breakpoint, check if the locations
15757 should be individually disabled. Watchpoint locations are
15758 special, and not user visible. */
15759 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15760 {
15761 struct bp_location *loc;
15762 int n = 1;
15763
15764 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15765 if (!loc->enabled)
15766 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
15767 }
15768 }
15769
15770 if (extra_trace_bits && *default_collect)
15771 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
15772
15773 if (from_tty)
15774 printf_filtered (_("Saved to file '%s'.\n"), filename);
15775 do_cleanups (cleanup);
15776 }
15777
15778 /* The `save breakpoints' command. */
15779
15780 static void
15781 save_breakpoints_command (char *args, int from_tty)
15782 {
15783 save_breakpoints (args, from_tty, NULL);
15784 }
15785
15786 /* The `save tracepoints' command. */
15787
15788 static void
15789 save_tracepoints_command (char *args, int from_tty)
15790 {
15791 save_breakpoints (args, from_tty, is_tracepoint);
15792 }
15793
15794 /* Create a vector of all tracepoints. */
15795
15796 VEC(breakpoint_p) *
15797 all_tracepoints (void)
15798 {
15799 VEC(breakpoint_p) *tp_vec = 0;
15800 struct breakpoint *tp;
15801
15802 ALL_TRACEPOINTS (tp)
15803 {
15804 VEC_safe_push (breakpoint_p, tp_vec, tp);
15805 }
15806
15807 return tp_vec;
15808 }
15809
15810 \f
15811 /* This help string is used to consolidate all the help string for specifying
15812 locations used by several commands. */
15813
15814 #define LOCATION_HELP_STRING \
15815 "Linespecs are colon-separated lists of location parameters, such as\n\
15816 source filename, function name, label name, and line number.\n\
15817 Example: To specify the start of a label named \"the_top\" in the\n\
15818 function \"fact\" in the file \"factorial.c\", use\n\
15819 \"factorial.c:fact:the_top\".\n\
15820 \n\
15821 Address locations begin with \"*\" and specify an exact address in the\n\
15822 program. Example: To specify the fourth byte past the start function\n\
15823 \"main\", use \"*main + 4\".\n\
15824 \n\
15825 Explicit locations are similar to linespecs but use an option/argument\n\
15826 syntax to specify location parameters.\n\
15827 Example: To specify the start of the label named \"the_top\" in the\n\
15828 function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
15829 -function fact -label the_top\".\n"
15830
15831 /* This help string is used for the break, hbreak, tbreak and thbreak
15832 commands. It is defined as a macro to prevent duplication.
15833 COMMAND should be a string constant containing the name of the
15834 command. */
15835
15836 #define BREAK_ARGS_HELP(command) \
15837 command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15838 PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15839 probe point. Accepted values are `-probe' (for a generic, automatically\n\
15840 guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15841 `-probe-dtrace' (for a DTrace probe).\n\
15842 LOCATION may be a linespec, address, or explicit location as described\n\
15843 below.\n\
15844 \n\
15845 With no LOCATION, uses current execution address of the selected\n\
15846 stack frame. This is useful for breaking on return to a stack frame.\n\
15847 \n\
15848 THREADNUM is the number from \"info threads\".\n\
15849 CONDITION is a boolean expression.\n\
15850 \n" LOCATION_HELP_STRING "\n\
15851 Multiple breakpoints at one place are permitted, and useful if their\n\
15852 conditions are different.\n\
15853 \n\
15854 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15855
15856 /* List of subcommands for "catch". */
15857 static struct cmd_list_element *catch_cmdlist;
15858
15859 /* List of subcommands for "tcatch". */
15860 static struct cmd_list_element *tcatch_cmdlist;
15861
15862 void
15863 add_catch_command (char *name, char *docstring,
15864 cmd_sfunc_ftype *sfunc,
15865 completer_ftype *completer,
15866 void *user_data_catch,
15867 void *user_data_tcatch)
15868 {
15869 struct cmd_list_element *command;
15870
15871 command = add_cmd (name, class_breakpoint, NULL, docstring,
15872 &catch_cmdlist);
15873 set_cmd_sfunc (command, sfunc);
15874 set_cmd_context (command, user_data_catch);
15875 set_cmd_completer (command, completer);
15876
15877 command = add_cmd (name, class_breakpoint, NULL, docstring,
15878 &tcatch_cmdlist);
15879 set_cmd_sfunc (command, sfunc);
15880 set_cmd_context (command, user_data_tcatch);
15881 set_cmd_completer (command, completer);
15882 }
15883
15884 static void
15885 save_command (char *arg, int from_tty)
15886 {
15887 printf_unfiltered (_("\"save\" must be followed by "
15888 "the name of a save subcommand.\n"));
15889 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
15890 }
15891
15892 struct breakpoint *
15893 iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15894 void *data)
15895 {
15896 struct breakpoint *b, *b_tmp;
15897
15898 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15899 {
15900 if ((*callback) (b, data))
15901 return b;
15902 }
15903
15904 return NULL;
15905 }
15906
15907 /* Zero if any of the breakpoint's locations could be a location where
15908 functions have been inlined, nonzero otherwise. */
15909
15910 static int
15911 is_non_inline_function (struct breakpoint *b)
15912 {
15913 /* The shared library event breakpoint is set on the address of a
15914 non-inline function. */
15915 if (b->type == bp_shlib_event)
15916 return 1;
15917
15918 return 0;
15919 }
15920
15921 /* Nonzero if the specified PC cannot be a location where functions
15922 have been inlined. */
15923
15924 int
15925 pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
15926 const struct target_waitstatus *ws)
15927 {
15928 struct breakpoint *b;
15929 struct bp_location *bl;
15930
15931 ALL_BREAKPOINTS (b)
15932 {
15933 if (!is_non_inline_function (b))
15934 continue;
15935
15936 for (bl = b->loc; bl != NULL; bl = bl->next)
15937 {
15938 if (!bl->shlib_disabled
15939 && bpstat_check_location (bl, aspace, pc, ws))
15940 return 1;
15941 }
15942 }
15943
15944 return 0;
15945 }
15946
15947 /* Remove any references to OBJFILE which is going to be freed. */
15948
15949 void
15950 breakpoint_free_objfile (struct objfile *objfile)
15951 {
15952 struct bp_location **locp, *loc;
15953
15954 ALL_BP_LOCATIONS (loc, locp)
15955 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
15956 loc->symtab = NULL;
15957 }
15958
15959 void
15960 initialize_breakpoint_ops (void)
15961 {
15962 static int initialized = 0;
15963
15964 struct breakpoint_ops *ops;
15965
15966 if (initialized)
15967 return;
15968 initialized = 1;
15969
15970 /* The breakpoint_ops structure to be inherit by all kinds of
15971 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15972 internal and momentary breakpoints, etc.). */
15973 ops = &bkpt_base_breakpoint_ops;
15974 *ops = base_breakpoint_ops;
15975 ops->re_set = bkpt_re_set;
15976 ops->insert_location = bkpt_insert_location;
15977 ops->remove_location = bkpt_remove_location;
15978 ops->breakpoint_hit = bkpt_breakpoint_hit;
15979 ops->create_sals_from_location = bkpt_create_sals_from_location;
15980 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
15981 ops->decode_location = bkpt_decode_location;
15982
15983 /* The breakpoint_ops structure to be used in regular breakpoints. */
15984 ops = &bkpt_breakpoint_ops;
15985 *ops = bkpt_base_breakpoint_ops;
15986 ops->re_set = bkpt_re_set;
15987 ops->resources_needed = bkpt_resources_needed;
15988 ops->print_it = bkpt_print_it;
15989 ops->print_mention = bkpt_print_mention;
15990 ops->print_recreate = bkpt_print_recreate;
15991
15992 /* Ranged breakpoints. */
15993 ops = &ranged_breakpoint_ops;
15994 *ops = bkpt_breakpoint_ops;
15995 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15996 ops->resources_needed = resources_needed_ranged_breakpoint;
15997 ops->print_it = print_it_ranged_breakpoint;
15998 ops->print_one = print_one_ranged_breakpoint;
15999 ops->print_one_detail = print_one_detail_ranged_breakpoint;
16000 ops->print_mention = print_mention_ranged_breakpoint;
16001 ops->print_recreate = print_recreate_ranged_breakpoint;
16002
16003 /* Internal breakpoints. */
16004 ops = &internal_breakpoint_ops;
16005 *ops = bkpt_base_breakpoint_ops;
16006 ops->re_set = internal_bkpt_re_set;
16007 ops->check_status = internal_bkpt_check_status;
16008 ops->print_it = internal_bkpt_print_it;
16009 ops->print_mention = internal_bkpt_print_mention;
16010
16011 /* Momentary breakpoints. */
16012 ops = &momentary_breakpoint_ops;
16013 *ops = bkpt_base_breakpoint_ops;
16014 ops->re_set = momentary_bkpt_re_set;
16015 ops->check_status = momentary_bkpt_check_status;
16016 ops->print_it = momentary_bkpt_print_it;
16017 ops->print_mention = momentary_bkpt_print_mention;
16018
16019 /* Momentary breakpoints for bp_longjmp and bp_exception. */
16020 ops = &longjmp_breakpoint_ops;
16021 *ops = momentary_breakpoint_ops;
16022 ops->dtor = longjmp_bkpt_dtor;
16023
16024 /* Probe breakpoints. */
16025 ops = &bkpt_probe_breakpoint_ops;
16026 *ops = bkpt_breakpoint_ops;
16027 ops->insert_location = bkpt_probe_insert_location;
16028 ops->remove_location = bkpt_probe_remove_location;
16029 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
16030 ops->decode_location = bkpt_probe_decode_location;
16031
16032 /* Watchpoints. */
16033 ops = &watchpoint_breakpoint_ops;
16034 *ops = base_breakpoint_ops;
16035 ops->dtor = dtor_watchpoint;
16036 ops->re_set = re_set_watchpoint;
16037 ops->insert_location = insert_watchpoint;
16038 ops->remove_location = remove_watchpoint;
16039 ops->breakpoint_hit = breakpoint_hit_watchpoint;
16040 ops->check_status = check_status_watchpoint;
16041 ops->resources_needed = resources_needed_watchpoint;
16042 ops->works_in_software_mode = works_in_software_mode_watchpoint;
16043 ops->print_it = print_it_watchpoint;
16044 ops->print_mention = print_mention_watchpoint;
16045 ops->print_recreate = print_recreate_watchpoint;
16046 ops->explains_signal = explains_signal_watchpoint;
16047
16048 /* Masked watchpoints. */
16049 ops = &masked_watchpoint_breakpoint_ops;
16050 *ops = watchpoint_breakpoint_ops;
16051 ops->insert_location = insert_masked_watchpoint;
16052 ops->remove_location = remove_masked_watchpoint;
16053 ops->resources_needed = resources_needed_masked_watchpoint;
16054 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
16055 ops->print_it = print_it_masked_watchpoint;
16056 ops->print_one_detail = print_one_detail_masked_watchpoint;
16057 ops->print_mention = print_mention_masked_watchpoint;
16058 ops->print_recreate = print_recreate_masked_watchpoint;
16059
16060 /* Tracepoints. */
16061 ops = &tracepoint_breakpoint_ops;
16062 *ops = base_breakpoint_ops;
16063 ops->re_set = tracepoint_re_set;
16064 ops->breakpoint_hit = tracepoint_breakpoint_hit;
16065 ops->print_one_detail = tracepoint_print_one_detail;
16066 ops->print_mention = tracepoint_print_mention;
16067 ops->print_recreate = tracepoint_print_recreate;
16068 ops->create_sals_from_location = tracepoint_create_sals_from_location;
16069 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
16070 ops->decode_location = tracepoint_decode_location;
16071
16072 /* Probe tracepoints. */
16073 ops = &tracepoint_probe_breakpoint_ops;
16074 *ops = tracepoint_breakpoint_ops;
16075 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
16076 ops->decode_location = tracepoint_probe_decode_location;
16077
16078 /* Static tracepoints with marker (`-m'). */
16079 ops = &strace_marker_breakpoint_ops;
16080 *ops = tracepoint_breakpoint_ops;
16081 ops->create_sals_from_location = strace_marker_create_sals_from_location;
16082 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
16083 ops->decode_location = strace_marker_decode_location;
16084
16085 /* Fork catchpoints. */
16086 ops = &catch_fork_breakpoint_ops;
16087 *ops = base_breakpoint_ops;
16088 ops->insert_location = insert_catch_fork;
16089 ops->remove_location = remove_catch_fork;
16090 ops->breakpoint_hit = breakpoint_hit_catch_fork;
16091 ops->print_it = print_it_catch_fork;
16092 ops->print_one = print_one_catch_fork;
16093 ops->print_mention = print_mention_catch_fork;
16094 ops->print_recreate = print_recreate_catch_fork;
16095
16096 /* Vfork catchpoints. */
16097 ops = &catch_vfork_breakpoint_ops;
16098 *ops = base_breakpoint_ops;
16099 ops->insert_location = insert_catch_vfork;
16100 ops->remove_location = remove_catch_vfork;
16101 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
16102 ops->print_it = print_it_catch_vfork;
16103 ops->print_one = print_one_catch_vfork;
16104 ops->print_mention = print_mention_catch_vfork;
16105 ops->print_recreate = print_recreate_catch_vfork;
16106
16107 /* Exec catchpoints. */
16108 ops = &catch_exec_breakpoint_ops;
16109 *ops = base_breakpoint_ops;
16110 ops->dtor = dtor_catch_exec;
16111 ops->insert_location = insert_catch_exec;
16112 ops->remove_location = remove_catch_exec;
16113 ops->breakpoint_hit = breakpoint_hit_catch_exec;
16114 ops->print_it = print_it_catch_exec;
16115 ops->print_one = print_one_catch_exec;
16116 ops->print_mention = print_mention_catch_exec;
16117 ops->print_recreate = print_recreate_catch_exec;
16118
16119 /* Solib-related catchpoints. */
16120 ops = &catch_solib_breakpoint_ops;
16121 *ops = base_breakpoint_ops;
16122 ops->dtor = dtor_catch_solib;
16123 ops->insert_location = insert_catch_solib;
16124 ops->remove_location = remove_catch_solib;
16125 ops->breakpoint_hit = breakpoint_hit_catch_solib;
16126 ops->check_status = check_status_catch_solib;
16127 ops->print_it = print_it_catch_solib;
16128 ops->print_one = print_one_catch_solib;
16129 ops->print_mention = print_mention_catch_solib;
16130 ops->print_recreate = print_recreate_catch_solib;
16131
16132 ops = &dprintf_breakpoint_ops;
16133 *ops = bkpt_base_breakpoint_ops;
16134 ops->re_set = dprintf_re_set;
16135 ops->resources_needed = bkpt_resources_needed;
16136 ops->print_it = bkpt_print_it;
16137 ops->print_mention = bkpt_print_mention;
16138 ops->print_recreate = dprintf_print_recreate;
16139 ops->after_condition_true = dprintf_after_condition_true;
16140 ops->breakpoint_hit = dprintf_breakpoint_hit;
16141 }
16142
16143 /* Chain containing all defined "enable breakpoint" subcommands. */
16144
16145 static struct cmd_list_element *enablebreaklist = NULL;
16146
16147 void
16148 _initialize_breakpoint (void)
16149 {
16150 struct cmd_list_element *c;
16151
16152 initialize_breakpoint_ops ();
16153
16154 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
16155 observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
16156 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
16157
16158 breakpoint_objfile_key
16159 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
16160
16161 breakpoint_chain = 0;
16162 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
16163 before a breakpoint is set. */
16164 breakpoint_count = 0;
16165
16166 tracepoint_count = 0;
16167
16168 add_com ("ignore", class_breakpoint, ignore_command, _("\
16169 Set ignore-count of breakpoint number N to COUNT.\n\
16170 Usage is `ignore N COUNT'."));
16171
16172 add_com ("commands", class_breakpoint, commands_command, _("\
16173 Set commands to be executed when a breakpoint is hit.\n\
16174 Give breakpoint number as argument after \"commands\".\n\
16175 With no argument, the targeted breakpoint is the last one set.\n\
16176 The commands themselves follow starting on the next line.\n\
16177 Type a line containing \"end\" to indicate the end of them.\n\
16178 Give \"silent\" as the first line to make the breakpoint silent;\n\
16179 then no output is printed when it is hit, except what the commands print."));
16180
16181 c = add_com ("condition", class_breakpoint, condition_command, _("\
16182 Specify breakpoint number N to break only if COND is true.\n\
16183 Usage is `condition N COND', where N is an integer and COND is an\n\
16184 expression to be evaluated whenever breakpoint N is reached."));
16185 set_cmd_completer (c, condition_completer);
16186
16187 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
16188 Set a temporary breakpoint.\n\
16189 Like \"break\" except the breakpoint is only temporary,\n\
16190 so it will be deleted when hit. Equivalent to \"break\" followed\n\
16191 by using \"enable delete\" on the breakpoint number.\n\
16192 \n"
16193 BREAK_ARGS_HELP ("tbreak")));
16194 set_cmd_completer (c, location_completer);
16195
16196 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
16197 Set a hardware assisted breakpoint.\n\
16198 Like \"break\" except the breakpoint requires hardware support,\n\
16199 some target hardware may not have this support.\n\
16200 \n"
16201 BREAK_ARGS_HELP ("hbreak")));
16202 set_cmd_completer (c, location_completer);
16203
16204 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
16205 Set a temporary hardware assisted breakpoint.\n\
16206 Like \"hbreak\" except the breakpoint is only temporary,\n\
16207 so it will be deleted when hit.\n\
16208 \n"
16209 BREAK_ARGS_HELP ("thbreak")));
16210 set_cmd_completer (c, location_completer);
16211
16212 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
16213 Enable some breakpoints.\n\
16214 Give breakpoint numbers (separated by spaces) as arguments.\n\
16215 With no subcommand, breakpoints are enabled until you command otherwise.\n\
16216 This is used to cancel the effect of the \"disable\" command.\n\
16217 With a subcommand you can enable temporarily."),
16218 &enablelist, "enable ", 1, &cmdlist);
16219
16220 add_com_alias ("en", "enable", class_breakpoint, 1);
16221
16222 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
16223 Enable some breakpoints.\n\
16224 Give breakpoint numbers (separated by spaces) as arguments.\n\
16225 This is used to cancel the effect of the \"disable\" command.\n\
16226 May be abbreviated to simply \"enable\".\n"),
16227 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
16228
16229 add_cmd ("once", no_class, enable_once_command, _("\
16230 Enable breakpoints for one hit. Give breakpoint numbers.\n\
16231 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
16232 &enablebreaklist);
16233
16234 add_cmd ("delete", no_class, enable_delete_command, _("\
16235 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16236 If a breakpoint is hit while enabled in this fashion, it is deleted."),
16237 &enablebreaklist);
16238
16239 add_cmd ("count", no_class, enable_count_command, _("\
16240 Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16241 If a breakpoint is hit while enabled in this fashion,\n\
16242 the count is decremented; when it reaches zero, the breakpoint is disabled."),
16243 &enablebreaklist);
16244
16245 add_cmd ("delete", no_class, enable_delete_command, _("\
16246 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16247 If a breakpoint is hit while enabled in this fashion, it is deleted."),
16248 &enablelist);
16249
16250 add_cmd ("once", no_class, enable_once_command, _("\
16251 Enable breakpoints for one hit. Give breakpoint numbers.\n\
16252 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
16253 &enablelist);
16254
16255 add_cmd ("count", no_class, enable_count_command, _("\
16256 Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16257 If a breakpoint is hit while enabled in this fashion,\n\
16258 the count is decremented; when it reaches zero, the breakpoint is disabled."),
16259 &enablelist);
16260
16261 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
16262 Disable some breakpoints.\n\
16263 Arguments are breakpoint numbers with spaces in between.\n\
16264 To disable all breakpoints, give no argument.\n\
16265 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
16266 &disablelist, "disable ", 1, &cmdlist);
16267 add_com_alias ("dis", "disable", class_breakpoint, 1);
16268 add_com_alias ("disa", "disable", class_breakpoint, 1);
16269
16270 add_cmd ("breakpoints", class_alias, disable_command, _("\
16271 Disable some breakpoints.\n\
16272 Arguments are breakpoint numbers with spaces in between.\n\
16273 To disable all breakpoints, give no argument.\n\
16274 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
16275 This command may be abbreviated \"disable\"."),
16276 &disablelist);
16277
16278 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
16279 Delete some breakpoints or auto-display expressions.\n\
16280 Arguments are breakpoint numbers with spaces in between.\n\
16281 To delete all breakpoints, give no argument.\n\
16282 \n\
16283 Also a prefix command for deletion of other GDB objects.\n\
16284 The \"unset\" command is also an alias for \"delete\"."),
16285 &deletelist, "delete ", 1, &cmdlist);
16286 add_com_alias ("d", "delete", class_breakpoint, 1);
16287 add_com_alias ("del", "delete", class_breakpoint, 1);
16288
16289 add_cmd ("breakpoints", class_alias, delete_command, _("\
16290 Delete some breakpoints or auto-display expressions.\n\
16291 Arguments are breakpoint numbers with spaces in between.\n\
16292 To delete all breakpoints, give no argument.\n\
16293 This command may be abbreviated \"delete\"."),
16294 &deletelist);
16295
16296 add_com ("clear", class_breakpoint, clear_command, _("\
16297 Clear breakpoint at specified location.\n\
16298 Argument may be a linespec, explicit, or address location as described below.\n\
16299 \n\
16300 With no argument, clears all breakpoints in the line that the selected frame\n\
16301 is executing in.\n"
16302 "\n" LOCATION_HELP_STRING "\n\
16303 See also the \"delete\" command which clears breakpoints by number."));
16304 add_com_alias ("cl", "clear", class_breakpoint, 1);
16305
16306 c = add_com ("break", class_breakpoint, break_command, _("\
16307 Set breakpoint at specified location.\n"
16308 BREAK_ARGS_HELP ("break")));
16309 set_cmd_completer (c, location_completer);
16310
16311 add_com_alias ("b", "break", class_run, 1);
16312 add_com_alias ("br", "break", class_run, 1);
16313 add_com_alias ("bre", "break", class_run, 1);
16314 add_com_alias ("brea", "break", class_run, 1);
16315
16316 if (dbx_commands)
16317 {
16318 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
16319 Break in function/address or break at a line in the current file."),
16320 &stoplist, "stop ", 1, &cmdlist);
16321 add_cmd ("in", class_breakpoint, stopin_command,
16322 _("Break in function or address."), &stoplist);
16323 add_cmd ("at", class_breakpoint, stopat_command,
16324 _("Break at a line in the current file."), &stoplist);
16325 add_com ("status", class_info, breakpoints_info, _("\
16326 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
16327 The \"Type\" column indicates one of:\n\
16328 \tbreakpoint - normal breakpoint\n\
16329 \twatchpoint - watchpoint\n\
16330 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16331 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16332 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16333 address and file/line number respectively.\n\
16334 \n\
16335 Convenience variable \"$_\" and default examine address for \"x\"\n\
16336 are set to the address of the last breakpoint listed unless the command\n\
16337 is prefixed with \"server \".\n\n\
16338 Convenience variable \"$bpnum\" contains the number of the last\n\
16339 breakpoint set."));
16340 }
16341
16342 add_info ("breakpoints", breakpoints_info, _("\
16343 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
16344 The \"Type\" column indicates one of:\n\
16345 \tbreakpoint - normal breakpoint\n\
16346 \twatchpoint - watchpoint\n\
16347 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16348 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16349 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16350 address and file/line number respectively.\n\
16351 \n\
16352 Convenience variable \"$_\" and default examine address for \"x\"\n\
16353 are set to the address of the last breakpoint listed unless the command\n\
16354 is prefixed with \"server \".\n\n\
16355 Convenience variable \"$bpnum\" contains the number of the last\n\
16356 breakpoint set."));
16357
16358 add_info_alias ("b", "breakpoints", 1);
16359
16360 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
16361 Status of all breakpoints, or breakpoint number NUMBER.\n\
16362 The \"Type\" column indicates one of:\n\
16363 \tbreakpoint - normal breakpoint\n\
16364 \twatchpoint - watchpoint\n\
16365 \tlongjmp - internal breakpoint used to step through longjmp()\n\
16366 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
16367 \tuntil - internal breakpoint used by the \"until\" command\n\
16368 \tfinish - internal breakpoint used by the \"finish\" command\n\
16369 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16370 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16371 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16372 address and file/line number respectively.\n\
16373 \n\
16374 Convenience variable \"$_\" and default examine address for \"x\"\n\
16375 are set to the address of the last breakpoint listed unless the command\n\
16376 is prefixed with \"server \".\n\n\
16377 Convenience variable \"$bpnum\" contains the number of the last\n\
16378 breakpoint set."),
16379 &maintenanceinfolist);
16380
16381 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
16382 Set catchpoints to catch events."),
16383 &catch_cmdlist, "catch ",
16384 0/*allow-unknown*/, &cmdlist);
16385
16386 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
16387 Set temporary catchpoints to catch events."),
16388 &tcatch_cmdlist, "tcatch ",
16389 0/*allow-unknown*/, &cmdlist);
16390
16391 add_catch_command ("fork", _("Catch calls to fork."),
16392 catch_fork_command_1,
16393 NULL,
16394 (void *) (uintptr_t) catch_fork_permanent,
16395 (void *) (uintptr_t) catch_fork_temporary);
16396 add_catch_command ("vfork", _("Catch calls to vfork."),
16397 catch_fork_command_1,
16398 NULL,
16399 (void *) (uintptr_t) catch_vfork_permanent,
16400 (void *) (uintptr_t) catch_vfork_temporary);
16401 add_catch_command ("exec", _("Catch calls to exec."),
16402 catch_exec_command_1,
16403 NULL,
16404 CATCH_PERMANENT,
16405 CATCH_TEMPORARY);
16406 add_catch_command ("load", _("Catch loads of shared libraries.\n\
16407 Usage: catch load [REGEX]\n\
16408 If REGEX is given, only stop for libraries matching the regular expression."),
16409 catch_load_command_1,
16410 NULL,
16411 CATCH_PERMANENT,
16412 CATCH_TEMPORARY);
16413 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
16414 Usage: catch unload [REGEX]\n\
16415 If REGEX is given, only stop for libraries matching the regular expression."),
16416 catch_unload_command_1,
16417 NULL,
16418 CATCH_PERMANENT,
16419 CATCH_TEMPORARY);
16420
16421 c = add_com ("watch", class_breakpoint, watch_command, _("\
16422 Set a watchpoint for an expression.\n\
16423 Usage: watch [-l|-location] EXPRESSION\n\
16424 A watchpoint stops execution of your program whenever the value of\n\
16425 an expression changes.\n\
16426 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16427 the memory to which it refers."));
16428 set_cmd_completer (c, expression_completer);
16429
16430 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
16431 Set a read watchpoint for an expression.\n\
16432 Usage: rwatch [-l|-location] EXPRESSION\n\
16433 A watchpoint stops execution of your program whenever the value of\n\
16434 an expression is read.\n\
16435 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16436 the memory to which it refers."));
16437 set_cmd_completer (c, expression_completer);
16438
16439 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
16440 Set a watchpoint for an expression.\n\
16441 Usage: awatch [-l|-location] EXPRESSION\n\
16442 A watchpoint stops execution of your program whenever the value of\n\
16443 an expression is either read or written.\n\
16444 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16445 the memory to which it refers."));
16446 set_cmd_completer (c, expression_completer);
16447
16448 add_info ("watchpoints", watchpoints_info, _("\
16449 Status of specified watchpoints (all watchpoints if no argument)."));
16450
16451 /* XXX: cagney/2005-02-23: This should be a boolean, and should
16452 respond to changes - contrary to the description. */
16453 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
16454 &can_use_hw_watchpoints, _("\
16455 Set debugger's willingness to use watchpoint hardware."), _("\
16456 Show debugger's willingness to use watchpoint hardware."), _("\
16457 If zero, gdb will not use hardware for new watchpoints, even if\n\
16458 such is available. (However, any hardware watchpoints that were\n\
16459 created before setting this to nonzero, will continue to use watchpoint\n\
16460 hardware.)"),
16461 NULL,
16462 show_can_use_hw_watchpoints,
16463 &setlist, &showlist);
16464
16465 can_use_hw_watchpoints = 1;
16466
16467 /* Tracepoint manipulation commands. */
16468
16469 c = add_com ("trace", class_breakpoint, trace_command, _("\
16470 Set a tracepoint at specified location.\n\
16471 \n"
16472 BREAK_ARGS_HELP ("trace") "\n\
16473 Do \"help tracepoints\" for info on other tracepoint commands."));
16474 set_cmd_completer (c, location_completer);
16475
16476 add_com_alias ("tp", "trace", class_alias, 0);
16477 add_com_alias ("tr", "trace", class_alias, 1);
16478 add_com_alias ("tra", "trace", class_alias, 1);
16479 add_com_alias ("trac", "trace", class_alias, 1);
16480
16481 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
16482 Set a fast tracepoint at specified location.\n\
16483 \n"
16484 BREAK_ARGS_HELP ("ftrace") "\n\
16485 Do \"help tracepoints\" for info on other tracepoint commands."));
16486 set_cmd_completer (c, location_completer);
16487
16488 c = add_com ("strace", class_breakpoint, strace_command, _("\
16489 Set a static tracepoint at location or marker.\n\
16490 \n\
16491 strace [LOCATION] [if CONDITION]\n\
16492 LOCATION may be a linespec, explicit, or address location (described below) \n\
16493 or -m MARKER_ID.\n\n\
16494 If a marker id is specified, probe the marker with that name. With\n\
16495 no LOCATION, uses current execution address of the selected stack frame.\n\
16496 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16497 This collects arbitrary user data passed in the probe point call to the\n\
16498 tracing library. You can inspect it when analyzing the trace buffer,\n\
16499 by printing the $_sdata variable like any other convenience variable.\n\
16500 \n\
16501 CONDITION is a boolean expression.\n\
16502 \n" LOCATION_HELP_STRING "\n\
16503 Multiple tracepoints at one place are permitted, and useful if their\n\
16504 conditions are different.\n\
16505 \n\
16506 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16507 Do \"help tracepoints\" for info on other tracepoint commands."));
16508 set_cmd_completer (c, location_completer);
16509
16510 add_info ("tracepoints", tracepoints_info, _("\
16511 Status of specified tracepoints (all tracepoints if no argument).\n\
16512 Convenience variable \"$tpnum\" contains the number of the\n\
16513 last tracepoint set."));
16514
16515 add_info_alias ("tp", "tracepoints", 1);
16516
16517 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16518 Delete specified tracepoints.\n\
16519 Arguments are tracepoint numbers, separated by spaces.\n\
16520 No argument means delete all tracepoints."),
16521 &deletelist);
16522 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
16523
16524 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16525 Disable specified tracepoints.\n\
16526 Arguments are tracepoint numbers, separated by spaces.\n\
16527 No argument means disable all tracepoints."),
16528 &disablelist);
16529 deprecate_cmd (c, "disable");
16530
16531 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16532 Enable specified tracepoints.\n\
16533 Arguments are tracepoint numbers, separated by spaces.\n\
16534 No argument means enable all tracepoints."),
16535 &enablelist);
16536 deprecate_cmd (c, "enable");
16537
16538 add_com ("passcount", class_trace, trace_pass_command, _("\
16539 Set the passcount for a tracepoint.\n\
16540 The trace will end when the tracepoint has been passed 'count' times.\n\
16541 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16542 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16543
16544 add_prefix_cmd ("save", class_breakpoint, save_command,
16545 _("Save breakpoint definitions as a script."),
16546 &save_cmdlist, "save ",
16547 0/*allow-unknown*/, &cmdlist);
16548
16549 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16550 Save current breakpoint definitions as a script.\n\
16551 This includes all types of breakpoints (breakpoints, watchpoints,\n\
16552 catchpoints, tracepoints). Use the 'source' command in another debug\n\
16553 session to restore them."),
16554 &save_cmdlist);
16555 set_cmd_completer (c, filename_completer);
16556
16557 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
16558 Save current tracepoint definitions as a script.\n\
16559 Use the 'source' command in another debug session to restore them."),
16560 &save_cmdlist);
16561 set_cmd_completer (c, filename_completer);
16562
16563 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16564 deprecate_cmd (c, "save tracepoints");
16565
16566 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
16567 Breakpoint specific settings\n\
16568 Configure various breakpoint-specific variables such as\n\
16569 pending breakpoint behavior"),
16570 &breakpoint_set_cmdlist, "set breakpoint ",
16571 0/*allow-unknown*/, &setlist);
16572 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
16573 Breakpoint specific settings\n\
16574 Configure various breakpoint-specific variables such as\n\
16575 pending breakpoint behavior"),
16576 &breakpoint_show_cmdlist, "show breakpoint ",
16577 0/*allow-unknown*/, &showlist);
16578
16579 add_setshow_auto_boolean_cmd ("pending", no_class,
16580 &pending_break_support, _("\
16581 Set debugger's behavior regarding pending breakpoints."), _("\
16582 Show debugger's behavior regarding pending breakpoints."), _("\
16583 If on, an unrecognized breakpoint location will cause gdb to create a\n\
16584 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16585 an error. If auto, an unrecognized breakpoint location results in a\n\
16586 user-query to see if a pending breakpoint should be created."),
16587 NULL,
16588 show_pending_break_support,
16589 &breakpoint_set_cmdlist,
16590 &breakpoint_show_cmdlist);
16591
16592 pending_break_support = AUTO_BOOLEAN_AUTO;
16593
16594 add_setshow_boolean_cmd ("auto-hw", no_class,
16595 &automatic_hardware_breakpoints, _("\
16596 Set automatic usage of hardware breakpoints."), _("\
16597 Show automatic usage of hardware breakpoints."), _("\
16598 If set, the debugger will automatically use hardware breakpoints for\n\
16599 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16600 a warning will be emitted for such breakpoints."),
16601 NULL,
16602 show_automatic_hardware_breakpoints,
16603 &breakpoint_set_cmdlist,
16604 &breakpoint_show_cmdlist);
16605
16606 add_setshow_boolean_cmd ("always-inserted", class_support,
16607 &always_inserted_mode, _("\
16608 Set mode for inserting breakpoints."), _("\
16609 Show mode for inserting breakpoints."), _("\
16610 When this mode is on, breakpoints are inserted immediately as soon as\n\
16611 they're created, kept inserted even when execution stops, and removed\n\
16612 only when the user deletes them. When this mode is off (the default),\n\
16613 breakpoints are inserted only when execution continues, and removed\n\
16614 when execution stops."),
16615 NULL,
16616 &show_always_inserted_mode,
16617 &breakpoint_set_cmdlist,
16618 &breakpoint_show_cmdlist);
16619
16620 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16621 condition_evaluation_enums,
16622 &condition_evaluation_mode_1, _("\
16623 Set mode of breakpoint condition evaluation."), _("\
16624 Show mode of breakpoint condition evaluation."), _("\
16625 When this is set to \"host\", breakpoint conditions will be\n\
16626 evaluated on the host's side by GDB. When it is set to \"target\",\n\
16627 breakpoint conditions will be downloaded to the target (if the target\n\
16628 supports such feature) and conditions will be evaluated on the target's side.\n\
16629 If this is set to \"auto\" (default), this will be automatically set to\n\
16630 \"target\" if it supports condition evaluation, otherwise it will\n\
16631 be set to \"gdb\""),
16632 &set_condition_evaluation_mode,
16633 &show_condition_evaluation_mode,
16634 &breakpoint_set_cmdlist,
16635 &breakpoint_show_cmdlist);
16636
16637 add_com ("break-range", class_breakpoint, break_range_command, _("\
16638 Set a breakpoint for an address range.\n\
16639 break-range START-LOCATION, END-LOCATION\n\
16640 where START-LOCATION and END-LOCATION can be one of the following:\n\
16641 LINENUM, for that line in the current file,\n\
16642 FILE:LINENUM, for that line in that file,\n\
16643 +OFFSET, for that number of lines after the current line\n\
16644 or the start of the range\n\
16645 FUNCTION, for the first line in that function,\n\
16646 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16647 *ADDRESS, for the instruction at that address.\n\
16648 \n\
16649 The breakpoint will stop execution of the inferior whenever it executes\n\
16650 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16651 range (including START-LOCATION and END-LOCATION)."));
16652
16653 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
16654 Set a dynamic printf at specified location.\n\
16655 dprintf location,format string,arg1,arg2,...\n\
16656 location may be a linespec, explicit, or address location.\n"
16657 "\n" LOCATION_HELP_STRING));
16658 set_cmd_completer (c, location_completer);
16659
16660 add_setshow_enum_cmd ("dprintf-style", class_support,
16661 dprintf_style_enums, &dprintf_style, _("\
16662 Set the style of usage for dynamic printf."), _("\
16663 Show the style of usage for dynamic printf."), _("\
16664 This setting chooses how GDB will do a dynamic printf.\n\
16665 If the value is \"gdb\", then the printing is done by GDB to its own\n\
16666 console, as with the \"printf\" command.\n\
16667 If the value is \"call\", the print is done by calling a function in your\n\
16668 program; by default printf(), but you can choose a different function or\n\
16669 output stream by setting dprintf-function and dprintf-channel."),
16670 update_dprintf_commands, NULL,
16671 &setlist, &showlist);
16672
16673 dprintf_function = xstrdup ("printf");
16674 add_setshow_string_cmd ("dprintf-function", class_support,
16675 &dprintf_function, _("\
16676 Set the function to use for dynamic printf"), _("\
16677 Show the function to use for dynamic printf"), NULL,
16678 update_dprintf_commands, NULL,
16679 &setlist, &showlist);
16680
16681 dprintf_channel = xstrdup ("");
16682 add_setshow_string_cmd ("dprintf-channel", class_support,
16683 &dprintf_channel, _("\
16684 Set the channel to use for dynamic printf"), _("\
16685 Show the channel to use for dynamic printf"), NULL,
16686 update_dprintf_commands, NULL,
16687 &setlist, &showlist);
16688
16689 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16690 &disconnected_dprintf, _("\
16691 Set whether dprintf continues after GDB disconnects."), _("\
16692 Show whether dprintf continues after GDB disconnects."), _("\
16693 Use this to let dprintf commands continue to hit and produce output\n\
16694 even if GDB disconnects or detaches from the target."),
16695 NULL,
16696 NULL,
16697 &setlist, &showlist);
16698
16699 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16700 agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16701 (target agent only) This is useful for formatted output in user-defined commands."));
16702
16703 automatic_hardware_breakpoints = 1;
16704
16705 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
16706 observer_attach_thread_exit (remove_threaded_breakpoints);
16707 }
This page took 0.438599 seconds and 4 git commands to generate.