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