Fix potential buffer overflows with sprintf and very large integer values.
[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
1608 /* Find BC_L which is a leftmost element which may affect BUF
1609 content. It is safe to report lower value but a failure to
1610 report higher one. */
1611
1612 bc_l = 0;
1613 bc_r = bp_location_count;
1614 while (bc_l + 1 < bc_r)
1615 {
1616 struct bp_location *bl;
1617
1618 bc = (bc_l + bc_r) / 2;
1619 bl = bp_location[bc];
1620
1621 /* Check first BL->ADDRESS will not overflow due to the added
1622 constant. Then advance the left boundary only if we are sure
1623 the BC element can in no way affect the BUF content (MEMADDR
1624 to MEMADDR + LEN range).
1625
1626 Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1627 offset so that we cannot miss a breakpoint with its shadow
1628 range tail still reaching MEMADDR. */
1629
1630 if ((bl->address + bp_location_shadow_len_after_address_max
1631 >= bl->address)
1632 && (bl->address + bp_location_shadow_len_after_address_max
1633 <= memaddr))
1634 bc_l = bc;
1635 else
1636 bc_r = bc;
1637 }
1638
1639 /* Due to the binary search above, we need to make sure we pick the
1640 first location that's at BC_L's address. E.g., if there are
1641 multiple locations at the same address, BC_L may end up pointing
1642 at a duplicate location, and miss the "master"/"inserted"
1643 location. Say, given locations L1, L2 and L3 at addresses A and
1644 B:
1645
1646 L1@A, L2@A, L3@B, ...
1647
1648 BC_L could end up pointing at location L2, while the "master"
1649 location could be L1. Since the `loc->inserted' flag is only set
1650 on "master" locations, we'd forget to restore the shadow of L1
1651 and L2. */
1652 while (bc_l > 0
1653 && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1654 bc_l--;
1655
1656 /* Now do full processing of the found relevant range of elements. */
1657
1658 for (bc = bc_l; bc < bp_location_count; bc++)
1659 {
1660 struct bp_location *bl = bp_location[bc];
1661
1662 /* bp_location array has BL->OWNER always non-NULL. */
1663 if (bl->owner->type == bp_none)
1664 warning (_("reading through apparently deleted breakpoint #%d?"),
1665 bl->owner->number);
1666
1667 /* Performance optimization: any further element can no longer affect BUF
1668 content. */
1669
1670 if (bl->address >= bp_location_placed_address_before_address_max
1671 && memaddr + len <= (bl->address
1672 - bp_location_placed_address_before_address_max))
1673 break;
1674
1675 if (!bp_location_has_shadow (bl))
1676 continue;
1677
1678 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1679 memaddr, len, &bl->target_info, bl->gdbarch);
1680 }
1681 }
1682
1683 \f
1684
1685 /* Return true if BPT is either a software breakpoint or a hardware
1686 breakpoint. */
1687
1688 int
1689 is_breakpoint (const struct breakpoint *bpt)
1690 {
1691 return (bpt->type == bp_breakpoint
1692 || bpt->type == bp_hardware_breakpoint
1693 || bpt->type == bp_dprintf);
1694 }
1695
1696 /* Return true if BPT is of any hardware watchpoint kind. */
1697
1698 static int
1699 is_hardware_watchpoint (const struct breakpoint *bpt)
1700 {
1701 return (bpt->type == bp_hardware_watchpoint
1702 || bpt->type == bp_read_watchpoint
1703 || bpt->type == bp_access_watchpoint);
1704 }
1705
1706 /* Return true if BPT is of any watchpoint kind, hardware or
1707 software. */
1708
1709 int
1710 is_watchpoint (const struct breakpoint *bpt)
1711 {
1712 return (is_hardware_watchpoint (bpt)
1713 || bpt->type == bp_watchpoint);
1714 }
1715
1716 /* Returns true if the current thread and its running state are safe
1717 to evaluate or update watchpoint B. Watchpoints on local
1718 expressions need to be evaluated in the context of the thread that
1719 was current when the watchpoint was created, and, that thread needs
1720 to be stopped to be able to select the correct frame context.
1721 Watchpoints on global expressions can be evaluated on any thread,
1722 and in any state. It is presently left to the target allowing
1723 memory accesses when threads are running. */
1724
1725 static int
1726 watchpoint_in_thread_scope (struct watchpoint *b)
1727 {
1728 return (b->base.pspace == current_program_space
1729 && (ptid_equal (b->watchpoint_thread, null_ptid)
1730 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1731 && !is_executing (inferior_ptid))));
1732 }
1733
1734 /* Set watchpoint B to disp_del_at_next_stop, even including its possible
1735 associated bp_watchpoint_scope breakpoint. */
1736
1737 static void
1738 watchpoint_del_at_next_stop (struct watchpoint *w)
1739 {
1740 struct breakpoint *b = &w->base;
1741
1742 if (b->related_breakpoint != b)
1743 {
1744 gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1745 gdb_assert (b->related_breakpoint->related_breakpoint == b);
1746 b->related_breakpoint->disposition = disp_del_at_next_stop;
1747 b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1748 b->related_breakpoint = b;
1749 }
1750 b->disposition = disp_del_at_next_stop;
1751 }
1752
1753 /* Extract a bitfield value from value VAL using the bit parameters contained in
1754 watchpoint W. */
1755
1756 static struct value *
1757 extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1758 {
1759 struct value *bit_val;
1760
1761 if (val == NULL)
1762 return NULL;
1763
1764 bit_val = allocate_value (value_type (val));
1765
1766 unpack_value_bitfield (bit_val,
1767 w->val_bitpos,
1768 w->val_bitsize,
1769 value_contents_for_printing (val),
1770 value_offset (val),
1771 val);
1772
1773 return bit_val;
1774 }
1775
1776 /* Allocate a dummy location and add it to B, which must be a software
1777 watchpoint. This is required because even if a software watchpoint
1778 is not watching any memory, bpstat_stop_status requires a location
1779 to be able to report stops. */
1780
1781 static void
1782 software_watchpoint_add_no_memory_location (struct breakpoint *b,
1783 struct program_space *pspace)
1784 {
1785 gdb_assert (b->type == bp_watchpoint && b->loc == NULL);
1786
1787 b->loc = allocate_bp_location (b);
1788 b->loc->pspace = pspace;
1789 b->loc->address = -1;
1790 b->loc->length = -1;
1791 }
1792
1793 /* Returns true if B is a software watchpoint that is not watching any
1794 memory (e.g., "watch $pc"). */
1795
1796 static int
1797 is_no_memory_software_watchpoint (struct breakpoint *b)
1798 {
1799 return (b->type == bp_watchpoint
1800 && b->loc != NULL
1801 && b->loc->next == NULL
1802 && b->loc->address == -1
1803 && b->loc->length == -1);
1804 }
1805
1806 /* Assuming that B is a watchpoint:
1807 - Reparse watchpoint expression, if REPARSE is non-zero
1808 - Evaluate expression and store the result in B->val
1809 - Evaluate the condition if there is one, and store the result
1810 in b->loc->cond.
1811 - Update the list of values that must be watched in B->loc.
1812
1813 If the watchpoint disposition is disp_del_at_next_stop, then do
1814 nothing. If this is local watchpoint that is out of scope, delete
1815 it.
1816
1817 Even with `set breakpoint always-inserted on' the watchpoints are
1818 removed + inserted on each stop here. Normal breakpoints must
1819 never be removed because they might be missed by a running thread
1820 when debugging in non-stop mode. On the other hand, hardware
1821 watchpoints (is_hardware_watchpoint; processed here) are specific
1822 to each LWP since they are stored in each LWP's hardware debug
1823 registers. Therefore, such LWP must be stopped first in order to
1824 be able to modify its hardware watchpoints.
1825
1826 Hardware watchpoints must be reset exactly once after being
1827 presented to the user. It cannot be done sooner, because it would
1828 reset the data used to present the watchpoint hit to the user. And
1829 it must not be done later because it could display the same single
1830 watchpoint hit during multiple GDB stops. Note that the latter is
1831 relevant only to the hardware watchpoint types bp_read_watchpoint
1832 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1833 not user-visible - its hit is suppressed if the memory content has
1834 not changed.
1835
1836 The following constraints influence the location where we can reset
1837 hardware watchpoints:
1838
1839 * target_stopped_by_watchpoint and target_stopped_data_address are
1840 called several times when GDB stops.
1841
1842 [linux]
1843 * Multiple hardware watchpoints can be hit at the same time,
1844 causing GDB to stop. GDB only presents one hardware watchpoint
1845 hit at a time as the reason for stopping, and all the other hits
1846 are presented later, one after the other, each time the user
1847 requests the execution to be resumed. Execution is not resumed
1848 for the threads still having pending hit event stored in
1849 LWP_INFO->STATUS. While the watchpoint is already removed from
1850 the inferior on the first stop the thread hit event is kept being
1851 reported from its cached value by linux_nat_stopped_data_address
1852 until the real thread resume happens after the watchpoint gets
1853 presented and thus its LWP_INFO->STATUS gets reset.
1854
1855 Therefore the hardware watchpoint hit can get safely reset on the
1856 watchpoint removal from inferior. */
1857
1858 static void
1859 update_watchpoint (struct watchpoint *b, int reparse)
1860 {
1861 int within_current_scope;
1862 struct frame_id saved_frame_id;
1863 int frame_saved;
1864
1865 /* If this is a local watchpoint, we only want to check if the
1866 watchpoint frame is in scope if the current thread is the thread
1867 that was used to create the watchpoint. */
1868 if (!watchpoint_in_thread_scope (b))
1869 return;
1870
1871 if (b->base.disposition == disp_del_at_next_stop)
1872 return;
1873
1874 frame_saved = 0;
1875
1876 /* Determine if the watchpoint is within scope. */
1877 if (b->exp_valid_block == NULL)
1878 within_current_scope = 1;
1879 else
1880 {
1881 struct frame_info *fi = get_current_frame ();
1882 struct gdbarch *frame_arch = get_frame_arch (fi);
1883 CORE_ADDR frame_pc = get_frame_pc (fi);
1884
1885 /* If we're at a point where the stack has been destroyed
1886 (e.g. in a function epilogue), unwinding may not work
1887 properly. Do not attempt to recreate locations at this
1888 point. See similar comments in watchpoint_check. */
1889 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
1890 return;
1891
1892 /* Save the current frame's ID so we can restore it after
1893 evaluating the watchpoint expression on its own frame. */
1894 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1895 took a frame parameter, so that we didn't have to change the
1896 selected frame. */
1897 frame_saved = 1;
1898 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1899
1900 fi = frame_find_by_id (b->watchpoint_frame);
1901 within_current_scope = (fi != NULL);
1902 if (within_current_scope)
1903 select_frame (fi);
1904 }
1905
1906 /* We don't free locations. They are stored in the bp_location array
1907 and update_global_location_list will eventually delete them and
1908 remove breakpoints if needed. */
1909 b->base.loc = NULL;
1910
1911 if (within_current_scope && reparse)
1912 {
1913 const char *s;
1914
1915 if (b->exp)
1916 {
1917 xfree (b->exp);
1918 b->exp = NULL;
1919 }
1920 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1921 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
1922 /* If the meaning of expression itself changed, the old value is
1923 no longer relevant. We don't want to report a watchpoint hit
1924 to the user when the old value and the new value may actually
1925 be completely different objects. */
1926 value_free (b->val);
1927 b->val = NULL;
1928 b->val_valid = 0;
1929
1930 /* Note that unlike with breakpoints, the watchpoint's condition
1931 expression is stored in the breakpoint object, not in the
1932 locations (re)created below. */
1933 if (b->base.cond_string != NULL)
1934 {
1935 if (b->cond_exp != NULL)
1936 {
1937 xfree (b->cond_exp);
1938 b->cond_exp = NULL;
1939 }
1940
1941 s = b->base.cond_string;
1942 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
1943 }
1944 }
1945
1946 /* If we failed to parse the expression, for example because
1947 it refers to a global variable in a not-yet-loaded shared library,
1948 don't try to insert watchpoint. We don't automatically delete
1949 such watchpoint, though, since failure to parse expression
1950 is different from out-of-scope watchpoint. */
1951 if (!target_has_execution)
1952 {
1953 /* Without execution, memory can't change. No use to try and
1954 set watchpoint locations. The watchpoint will be reset when
1955 the target gains execution, through breakpoint_re_set. */
1956 if (!can_use_hw_watchpoints)
1957 {
1958 if (b->base.ops->works_in_software_mode (&b->base))
1959 b->base.type = bp_watchpoint;
1960 else
1961 error (_("Can't set read/access watchpoint when "
1962 "hardware watchpoints are disabled."));
1963 }
1964 }
1965 else if (within_current_scope && b->exp)
1966 {
1967 int pc = 0;
1968 struct value *val_chain, *v, *result, *next;
1969 struct program_space *frame_pspace;
1970
1971 fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain, 0);
1972
1973 /* Avoid setting b->val if it's already set. The meaning of
1974 b->val is 'the last value' user saw, and we should update
1975 it only if we reported that last value to user. As it
1976 happens, the code that reports it updates b->val directly.
1977 We don't keep track of the memory value for masked
1978 watchpoints. */
1979 if (!b->val_valid && !is_masked_watchpoint (&b->base))
1980 {
1981 if (b->val_bitsize != 0)
1982 {
1983 v = extract_bitfield_from_watchpoint_value (b, v);
1984 if (v != NULL)
1985 release_value (v);
1986 }
1987 b->val = v;
1988 b->val_valid = 1;
1989 }
1990
1991 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1992
1993 /* Look at each value on the value chain. */
1994 for (v = val_chain; v; v = value_next (v))
1995 {
1996 /* If it's a memory location, and GDB actually needed
1997 its contents to evaluate the expression, then we
1998 must watch it. If the first value returned is
1999 still lazy, that means an error occurred reading it;
2000 watch it anyway in case it becomes readable. */
2001 if (VALUE_LVAL (v) == lval_memory
2002 && (v == val_chain || ! value_lazy (v)))
2003 {
2004 struct type *vtype = check_typedef (value_type (v));
2005
2006 /* We only watch structs and arrays if user asked
2007 for it explicitly, never if they just happen to
2008 appear in the middle of some value chain. */
2009 if (v == result
2010 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
2011 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
2012 {
2013 CORE_ADDR addr;
2014 enum target_hw_bp_type type;
2015 struct bp_location *loc, **tmp;
2016 int bitpos = 0, bitsize = 0;
2017
2018 if (value_bitsize (v) != 0)
2019 {
2020 /* Extract the bit parameters out from the bitfield
2021 sub-expression. */
2022 bitpos = value_bitpos (v);
2023 bitsize = value_bitsize (v);
2024 }
2025 else if (v == result && b->val_bitsize != 0)
2026 {
2027 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
2028 lvalue whose bit parameters are saved in the fields
2029 VAL_BITPOS and VAL_BITSIZE. */
2030 bitpos = b->val_bitpos;
2031 bitsize = b->val_bitsize;
2032 }
2033
2034 addr = value_address (v);
2035 if (bitsize != 0)
2036 {
2037 /* Skip the bytes that don't contain the bitfield. */
2038 addr += bitpos / 8;
2039 }
2040
2041 type = hw_write;
2042 if (b->base.type == bp_read_watchpoint)
2043 type = hw_read;
2044 else if (b->base.type == bp_access_watchpoint)
2045 type = hw_access;
2046
2047 loc = allocate_bp_location (&b->base);
2048 for (tmp = &(b->base.loc); *tmp != NULL; tmp = &((*tmp)->next))
2049 ;
2050 *tmp = loc;
2051 loc->gdbarch = get_type_arch (value_type (v));
2052
2053 loc->pspace = frame_pspace;
2054 loc->address = addr;
2055
2056 if (bitsize != 0)
2057 {
2058 /* Just cover the bytes that make up the bitfield. */
2059 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2060 }
2061 else
2062 loc->length = TYPE_LENGTH (value_type (v));
2063
2064 loc->watchpoint_type = type;
2065 }
2066 }
2067 }
2068
2069 /* Change the type of breakpoint between hardware assisted or
2070 an ordinary watchpoint depending on the hardware support
2071 and free hardware slots. REPARSE is set when the inferior
2072 is started. */
2073 if (reparse)
2074 {
2075 int reg_cnt;
2076 enum bp_loc_type loc_type;
2077 struct bp_location *bl;
2078
2079 reg_cnt = can_use_hardware_watchpoint (val_chain);
2080
2081 if (reg_cnt)
2082 {
2083 int i, target_resources_ok, other_type_used;
2084 enum bptype type;
2085
2086 /* Use an exact watchpoint when there's only one memory region to be
2087 watched, and only one debug register is needed to watch it. */
2088 b->exact = target_exact_watchpoints && reg_cnt == 1;
2089
2090 /* We need to determine how many resources are already
2091 used for all other hardware watchpoints plus this one
2092 to see if we still have enough resources to also fit
2093 this watchpoint in as well. */
2094
2095 /* If this is a software watchpoint, we try to turn it
2096 to a hardware one -- count resources as if B was of
2097 hardware watchpoint type. */
2098 type = b->base.type;
2099 if (type == bp_watchpoint)
2100 type = bp_hardware_watchpoint;
2101
2102 /* This watchpoint may or may not have been placed on
2103 the list yet at this point (it won't be in the list
2104 if we're trying to create it for the first time,
2105 through watch_command), so always account for it
2106 manually. */
2107
2108 /* Count resources used by all watchpoints except B. */
2109 i = hw_watchpoint_used_count_others (&b->base, type, &other_type_used);
2110
2111 /* Add in the resources needed for B. */
2112 i += hw_watchpoint_use_count (&b->base);
2113
2114 target_resources_ok
2115 = target_can_use_hardware_watchpoint (type, i, other_type_used);
2116 if (target_resources_ok <= 0)
2117 {
2118 int sw_mode = b->base.ops->works_in_software_mode (&b->base);
2119
2120 if (target_resources_ok == 0 && !sw_mode)
2121 error (_("Target does not support this type of "
2122 "hardware watchpoint."));
2123 else if (target_resources_ok < 0 && !sw_mode)
2124 error (_("There are not enough available hardware "
2125 "resources for this watchpoint."));
2126
2127 /* Downgrade to software watchpoint. */
2128 b->base.type = bp_watchpoint;
2129 }
2130 else
2131 {
2132 /* If this was a software watchpoint, we've just
2133 found we have enough resources to turn it to a
2134 hardware watchpoint. Otherwise, this is a
2135 nop. */
2136 b->base.type = type;
2137 }
2138 }
2139 else if (!b->base.ops->works_in_software_mode (&b->base))
2140 {
2141 if (!can_use_hw_watchpoints)
2142 error (_("Can't set read/access watchpoint when "
2143 "hardware watchpoints are disabled."));
2144 else
2145 error (_("Expression cannot be implemented with "
2146 "read/access watchpoint."));
2147 }
2148 else
2149 b->base.type = bp_watchpoint;
2150
2151 loc_type = (b->base.type == bp_watchpoint? bp_loc_other
2152 : bp_loc_hardware_watchpoint);
2153 for (bl = b->base.loc; bl; bl = bl->next)
2154 bl->loc_type = loc_type;
2155 }
2156
2157 for (v = val_chain; v; v = next)
2158 {
2159 next = value_next (v);
2160 if (v != b->val)
2161 value_free (v);
2162 }
2163
2164 /* If a software watchpoint is not watching any memory, then the
2165 above left it without any location set up. But,
2166 bpstat_stop_status requires a location to be able to report
2167 stops, so make sure there's at least a dummy one. */
2168 if (b->base.type == bp_watchpoint && b->base.loc == NULL)
2169 software_watchpoint_add_no_memory_location (&b->base, frame_pspace);
2170 }
2171 else if (!within_current_scope)
2172 {
2173 printf_filtered (_("\
2174 Watchpoint %d deleted because the program has left the block\n\
2175 in which its expression is valid.\n"),
2176 b->base.number);
2177 watchpoint_del_at_next_stop (b);
2178 }
2179
2180 /* Restore the selected frame. */
2181 if (frame_saved)
2182 select_frame (frame_find_by_id (saved_frame_id));
2183 }
2184
2185
2186 /* Returns 1 iff breakpoint location should be
2187 inserted in the inferior. We don't differentiate the type of BL's owner
2188 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2189 breakpoint_ops is not defined, because in insert_bp_location,
2190 tracepoint's insert_location will not be called. */
2191 static int
2192 should_be_inserted (struct bp_location *bl)
2193 {
2194 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
2195 return 0;
2196
2197 if (bl->owner->disposition == disp_del_at_next_stop)
2198 return 0;
2199
2200 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
2201 return 0;
2202
2203 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2204 return 0;
2205
2206 /* This is set for example, when we're attached to the parent of a
2207 vfork, and have detached from the child. The child is running
2208 free, and we expect it to do an exec or exit, at which point the
2209 OS makes the parent schedulable again (and the target reports
2210 that the vfork is done). Until the child is done with the shared
2211 memory region, do not insert breakpoints in the parent, otherwise
2212 the child could still trip on the parent's breakpoints. Since
2213 the parent is blocked anyway, it won't miss any breakpoint. */
2214 if (bl->pspace->breakpoints_not_allowed)
2215 return 0;
2216
2217 /* Don't insert a breakpoint if we're trying to step past its
2218 location, except if the breakpoint is a single-step breakpoint,
2219 and the breakpoint's thread is the thread which is stepping past
2220 a breakpoint. */
2221 if ((bl->loc_type == bp_loc_software_breakpoint
2222 || bl->loc_type == bp_loc_hardware_breakpoint)
2223 && stepping_past_instruction_at (bl->pspace->aspace,
2224 bl->address)
2225 /* The single-step breakpoint may be inserted at the location
2226 we're trying to step if the instruction branches to itself.
2227 However, the instruction won't be executed at all and it may
2228 break the semantics of the instruction, for example, the
2229 instruction is a conditional branch or updates some flags.
2230 We can't fix it unless GDB is able to emulate the instruction
2231 or switch to displaced stepping. */
2232 && !(bl->owner->type == bp_single_step
2233 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
2234 {
2235 if (debug_infrun)
2236 {
2237 fprintf_unfiltered (gdb_stdlog,
2238 "infrun: skipping breakpoint: "
2239 "stepping past insn at: %s\n",
2240 paddress (bl->gdbarch, bl->address));
2241 }
2242 return 0;
2243 }
2244
2245 /* Don't insert watchpoints if we're trying to step past the
2246 instruction that triggered one. */
2247 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2248 && stepping_past_nonsteppable_watchpoint ())
2249 {
2250 if (debug_infrun)
2251 {
2252 fprintf_unfiltered (gdb_stdlog,
2253 "infrun: stepping past non-steppable watchpoint. "
2254 "skipping watchpoint at %s:%d\n",
2255 paddress (bl->gdbarch, bl->address),
2256 bl->length);
2257 }
2258 return 0;
2259 }
2260
2261 return 1;
2262 }
2263
2264 /* Same as should_be_inserted but does the check assuming
2265 that the location is not duplicated. */
2266
2267 static int
2268 unduplicated_should_be_inserted (struct bp_location *bl)
2269 {
2270 int result;
2271 const int save_duplicate = bl->duplicate;
2272
2273 bl->duplicate = 0;
2274 result = should_be_inserted (bl);
2275 bl->duplicate = save_duplicate;
2276 return result;
2277 }
2278
2279 /* Parses a conditional described by an expression COND into an
2280 agent expression bytecode suitable for evaluation
2281 by the bytecode interpreter. Return NULL if there was
2282 any error during parsing. */
2283
2284 static struct agent_expr *
2285 parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2286 {
2287 struct agent_expr *aexpr = NULL;
2288
2289 if (!cond)
2290 return NULL;
2291
2292 /* We don't want to stop processing, so catch any errors
2293 that may show up. */
2294 TRY
2295 {
2296 aexpr = gen_eval_for_expr (scope, cond);
2297 }
2298
2299 CATCH (ex, RETURN_MASK_ERROR)
2300 {
2301 /* If we got here, it means the condition could not be parsed to a valid
2302 bytecode expression and thus can't be evaluated on the target's side.
2303 It's no use iterating through the conditions. */
2304 return NULL;
2305 }
2306 END_CATCH
2307
2308 /* We have a valid agent expression. */
2309 return aexpr;
2310 }
2311
2312 /* Based on location BL, create a list of breakpoint conditions to be
2313 passed on to the target. If we have duplicated locations with different
2314 conditions, we will add such conditions to the list. The idea is that the
2315 target will evaluate the list of conditions and will only notify GDB when
2316 one of them is true. */
2317
2318 static void
2319 build_target_condition_list (struct bp_location *bl)
2320 {
2321 struct bp_location **locp = NULL, **loc2p;
2322 int null_condition_or_parse_error = 0;
2323 int modified = bl->needs_update;
2324 struct bp_location *loc;
2325
2326 /* Release conditions left over from a previous insert. */
2327 VEC_free (agent_expr_p, bl->target_info.conditions);
2328
2329 /* This is only meaningful if the target is
2330 evaluating conditions and if the user has
2331 opted for condition evaluation on the target's
2332 side. */
2333 if (gdb_evaluates_breakpoint_condition_p ()
2334 || !target_supports_evaluation_of_breakpoint_conditions ())
2335 return;
2336
2337 /* Do a first pass to check for locations with no assigned
2338 conditions or conditions that fail to parse to a valid agent expression
2339 bytecode. If any of these happen, then it's no use to send conditions
2340 to the target since this location will always trigger and generate a
2341 response back to GDB. */
2342 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2343 {
2344 loc = (*loc2p);
2345 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2346 {
2347 if (modified)
2348 {
2349 struct agent_expr *aexpr;
2350
2351 /* Re-parse the conditions since something changed. In that
2352 case we already freed the condition bytecodes (see
2353 force_breakpoint_reinsertion). We just
2354 need to parse the condition to bytecodes again. */
2355 aexpr = parse_cond_to_aexpr (bl->address, loc->cond);
2356 loc->cond_bytecode = aexpr;
2357 }
2358
2359 /* If we have a NULL bytecode expression, it means something
2360 went wrong or we have a null condition expression. */
2361 if (!loc->cond_bytecode)
2362 {
2363 null_condition_or_parse_error = 1;
2364 break;
2365 }
2366 }
2367 }
2368
2369 /* If any of these happened, it means we will have to evaluate the conditions
2370 for the location's address on gdb's side. It is no use keeping bytecodes
2371 for all the other duplicate locations, thus we free all of them here.
2372
2373 This is so we have a finer control over which locations' conditions are
2374 being evaluated by GDB or the remote stub. */
2375 if (null_condition_or_parse_error)
2376 {
2377 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2378 {
2379 loc = (*loc2p);
2380 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2381 {
2382 /* Only go as far as the first NULL bytecode is
2383 located. */
2384 if (!loc->cond_bytecode)
2385 return;
2386
2387 free_agent_expr (loc->cond_bytecode);
2388 loc->cond_bytecode = NULL;
2389 }
2390 }
2391 }
2392
2393 /* No NULL conditions or failed bytecode generation. Build a condition list
2394 for this location's address. */
2395 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2396 {
2397 loc = (*loc2p);
2398 if (loc->cond
2399 && is_breakpoint (loc->owner)
2400 && loc->pspace->num == bl->pspace->num
2401 && loc->owner->enable_state == bp_enabled
2402 && loc->enabled)
2403 /* Add the condition to the vector. This will be used later to send the
2404 conditions to the target. */
2405 VEC_safe_push (agent_expr_p, bl->target_info.conditions,
2406 loc->cond_bytecode);
2407 }
2408
2409 return;
2410 }
2411
2412 /* Parses a command described by string CMD into an agent expression
2413 bytecode suitable for evaluation by the bytecode interpreter.
2414 Return NULL if there was any error during parsing. */
2415
2416 static struct agent_expr *
2417 parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2418 {
2419 struct cleanup *old_cleanups = 0;
2420 struct expression *expr, **argvec;
2421 struct agent_expr *aexpr = NULL;
2422 const char *cmdrest;
2423 const char *format_start, *format_end;
2424 struct format_piece *fpieces;
2425 int nargs;
2426 struct gdbarch *gdbarch = get_current_arch ();
2427
2428 if (!cmd)
2429 return NULL;
2430
2431 cmdrest = cmd;
2432
2433 if (*cmdrest == ',')
2434 ++cmdrest;
2435 cmdrest = skip_spaces_const (cmdrest);
2436
2437 if (*cmdrest++ != '"')
2438 error (_("No format string following the location"));
2439
2440 format_start = cmdrest;
2441
2442 fpieces = parse_format_string (&cmdrest);
2443
2444 old_cleanups = make_cleanup (free_format_pieces_cleanup, &fpieces);
2445
2446 format_end = cmdrest;
2447
2448 if (*cmdrest++ != '"')
2449 error (_("Bad format string, non-terminated '\"'."));
2450
2451 cmdrest = skip_spaces_const (cmdrest);
2452
2453 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2454 error (_("Invalid argument syntax"));
2455
2456 if (*cmdrest == ',')
2457 cmdrest++;
2458 cmdrest = skip_spaces_const (cmdrest);
2459
2460 /* For each argument, make an expression. */
2461
2462 argvec = (struct expression **) alloca (strlen (cmd)
2463 * sizeof (struct expression *));
2464
2465 nargs = 0;
2466 while (*cmdrest != '\0')
2467 {
2468 const char *cmd1;
2469
2470 cmd1 = cmdrest;
2471 expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2472 argvec[nargs++] = expr;
2473 cmdrest = cmd1;
2474 if (*cmdrest == ',')
2475 ++cmdrest;
2476 }
2477
2478 /* We don't want to stop processing, so catch any errors
2479 that may show up. */
2480 TRY
2481 {
2482 aexpr = gen_printf (scope, gdbarch, 0, 0,
2483 format_start, format_end - format_start,
2484 fpieces, nargs, argvec);
2485 }
2486 CATCH (ex, RETURN_MASK_ERROR)
2487 {
2488 /* If we got here, it means the command could not be parsed to a valid
2489 bytecode expression and thus can't be evaluated on the target's side.
2490 It's no use iterating through the other commands. */
2491 aexpr = NULL;
2492 }
2493 END_CATCH
2494
2495 do_cleanups (old_cleanups);
2496
2497 /* We have a valid agent expression, return it. */
2498 return aexpr;
2499 }
2500
2501 /* Based on location BL, create a list of breakpoint commands to be
2502 passed on to the target. If we have duplicated locations with
2503 different commands, we will add any such to the list. */
2504
2505 static void
2506 build_target_command_list (struct bp_location *bl)
2507 {
2508 struct bp_location **locp = NULL, **loc2p;
2509 int null_command_or_parse_error = 0;
2510 int modified = bl->needs_update;
2511 struct bp_location *loc;
2512
2513 /* Release commands left over from a previous insert. */
2514 VEC_free (agent_expr_p, bl->target_info.tcommands);
2515
2516 if (!target_can_run_breakpoint_commands ())
2517 return;
2518
2519 /* For now, limit to agent-style dprintf breakpoints. */
2520 if (dprintf_style != dprintf_style_agent)
2521 return;
2522
2523 /* For now, if we have any duplicate location that isn't a dprintf,
2524 don't install the target-side commands, as that would make the
2525 breakpoint not be reported to the core, and we'd lose
2526 control. */
2527 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2528 {
2529 loc = (*loc2p);
2530 if (is_breakpoint (loc->owner)
2531 && loc->pspace->num == bl->pspace->num
2532 && loc->owner->type != bp_dprintf)
2533 return;
2534 }
2535
2536 /* Do a first pass to check for locations with no assigned
2537 conditions or conditions that fail to parse to a valid agent expression
2538 bytecode. If any of these happen, then it's no use to send conditions
2539 to the target since this location will always trigger and generate a
2540 response back to GDB. */
2541 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2542 {
2543 loc = (*loc2p);
2544 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2545 {
2546 if (modified)
2547 {
2548 struct agent_expr *aexpr;
2549
2550 /* Re-parse the commands since something changed. In that
2551 case we already freed the command bytecodes (see
2552 force_breakpoint_reinsertion). We just
2553 need to parse the command to bytecodes again. */
2554 aexpr = parse_cmd_to_aexpr (bl->address,
2555 loc->owner->extra_string);
2556 loc->cmd_bytecode = aexpr;
2557 }
2558
2559 /* If we have a NULL bytecode expression, it means something
2560 went wrong or we have a null command expression. */
2561 if (!loc->cmd_bytecode)
2562 {
2563 null_command_or_parse_error = 1;
2564 break;
2565 }
2566 }
2567 }
2568
2569 /* If anything failed, then we're not doing target-side commands,
2570 and so clean up. */
2571 if (null_command_or_parse_error)
2572 {
2573 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2574 {
2575 loc = (*loc2p);
2576 if (is_breakpoint (loc->owner)
2577 && loc->pspace->num == bl->pspace->num)
2578 {
2579 /* Only go as far as the first NULL bytecode is
2580 located. */
2581 if (loc->cmd_bytecode == NULL)
2582 return;
2583
2584 free_agent_expr (loc->cmd_bytecode);
2585 loc->cmd_bytecode = NULL;
2586 }
2587 }
2588 }
2589
2590 /* No NULL commands or failed bytecode generation. Build a command list
2591 for this location's address. */
2592 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2593 {
2594 loc = (*loc2p);
2595 if (loc->owner->extra_string
2596 && is_breakpoint (loc->owner)
2597 && loc->pspace->num == bl->pspace->num
2598 && loc->owner->enable_state == bp_enabled
2599 && loc->enabled)
2600 /* Add the command to the vector. This will be used later
2601 to send the commands to the target. */
2602 VEC_safe_push (agent_expr_p, bl->target_info.tcommands,
2603 loc->cmd_bytecode);
2604 }
2605
2606 bl->target_info.persist = 0;
2607 /* Maybe flag this location as persistent. */
2608 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2609 bl->target_info.persist = 1;
2610 }
2611
2612 /* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2613 location. Any error messages are printed to TMP_ERROR_STREAM; and
2614 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
2615 Returns 0 for success, 1 if the bp_location type is not supported or
2616 -1 for failure.
2617
2618 NOTE drow/2003-09-09: This routine could be broken down to an
2619 object-style method for each breakpoint or catchpoint type. */
2620 static int
2621 insert_bp_location (struct bp_location *bl,
2622 struct ui_file *tmp_error_stream,
2623 int *disabled_breaks,
2624 int *hw_breakpoint_error,
2625 int *hw_bp_error_explained_already)
2626 {
2627 enum errors bp_err = GDB_NO_ERROR;
2628 const char *bp_err_message = NULL;
2629
2630 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2631 return 0;
2632
2633 /* Note we don't initialize bl->target_info, as that wipes out
2634 the breakpoint location's shadow_contents if the breakpoint
2635 is still inserted at that location. This in turn breaks
2636 target_read_memory which depends on these buffers when
2637 a memory read is requested at the breakpoint location:
2638 Once the target_info has been wiped, we fail to see that
2639 we have a breakpoint inserted at that address and thus
2640 read the breakpoint instead of returning the data saved in
2641 the breakpoint location's shadow contents. */
2642 bl->target_info.reqstd_address = bl->address;
2643 bl->target_info.placed_address_space = bl->pspace->aspace;
2644 bl->target_info.length = bl->length;
2645
2646 /* When working with target-side conditions, we must pass all the conditions
2647 for the same breakpoint address down to the target since GDB will not
2648 insert those locations. With a list of breakpoint conditions, the target
2649 can decide when to stop and notify GDB. */
2650
2651 if (is_breakpoint (bl->owner))
2652 {
2653 build_target_condition_list (bl);
2654 build_target_command_list (bl);
2655 /* Reset the modification marker. */
2656 bl->needs_update = 0;
2657 }
2658
2659 if (bl->loc_type == bp_loc_software_breakpoint
2660 || bl->loc_type == bp_loc_hardware_breakpoint)
2661 {
2662 if (bl->owner->type != bp_hardware_breakpoint)
2663 {
2664 /* If the explicitly specified breakpoint type
2665 is not hardware breakpoint, check the memory map to see
2666 if the breakpoint address is in read only memory or not.
2667
2668 Two important cases are:
2669 - location type is not hardware breakpoint, memory
2670 is readonly. We change the type of the location to
2671 hardware breakpoint.
2672 - location type is hardware breakpoint, memory is
2673 read-write. This means we've previously made the
2674 location hardware one, but then the memory map changed,
2675 so we undo.
2676
2677 When breakpoints are removed, remove_breakpoints will use
2678 location types we've just set here, the only possible
2679 problem is that memory map has changed during running
2680 program, but it's not going to work anyway with current
2681 gdb. */
2682 struct mem_region *mr
2683 = lookup_mem_region (bl->target_info.reqstd_address);
2684
2685 if (mr)
2686 {
2687 if (automatic_hardware_breakpoints)
2688 {
2689 enum bp_loc_type new_type;
2690
2691 if (mr->attrib.mode != MEM_RW)
2692 new_type = bp_loc_hardware_breakpoint;
2693 else
2694 new_type = bp_loc_software_breakpoint;
2695
2696 if (new_type != bl->loc_type)
2697 {
2698 static int said = 0;
2699
2700 bl->loc_type = new_type;
2701 if (!said)
2702 {
2703 fprintf_filtered (gdb_stdout,
2704 _("Note: automatically using "
2705 "hardware breakpoints for "
2706 "read-only addresses.\n"));
2707 said = 1;
2708 }
2709 }
2710 }
2711 else if (bl->loc_type == bp_loc_software_breakpoint
2712 && mr->attrib.mode != MEM_RW)
2713 {
2714 fprintf_unfiltered (tmp_error_stream,
2715 _("Cannot insert breakpoint %d.\n"
2716 "Cannot set software breakpoint "
2717 "at read-only address %s\n"),
2718 bl->owner->number,
2719 paddress (bl->gdbarch, bl->address));
2720 return 1;
2721 }
2722 }
2723 }
2724
2725 /* First check to see if we have to handle an overlay. */
2726 if (overlay_debugging == ovly_off
2727 || bl->section == NULL
2728 || !(section_is_overlay (bl->section)))
2729 {
2730 /* No overlay handling: just set the breakpoint. */
2731 TRY
2732 {
2733 int val;
2734
2735 val = bl->owner->ops->insert_location (bl);
2736 if (val)
2737 bp_err = GENERIC_ERROR;
2738 }
2739 CATCH (e, RETURN_MASK_ALL)
2740 {
2741 bp_err = e.error;
2742 bp_err_message = e.message;
2743 }
2744 END_CATCH
2745 }
2746 else
2747 {
2748 /* This breakpoint is in an overlay section.
2749 Shall we set a breakpoint at the LMA? */
2750 if (!overlay_events_enabled)
2751 {
2752 /* Yes -- overlay event support is not active,
2753 so we must try to set a breakpoint at the LMA.
2754 This will not work for a hardware breakpoint. */
2755 if (bl->loc_type == bp_loc_hardware_breakpoint)
2756 warning (_("hardware breakpoint %d not supported in overlay!"),
2757 bl->owner->number);
2758 else
2759 {
2760 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2761 bl->section);
2762 /* Set a software (trap) breakpoint at the LMA. */
2763 bl->overlay_target_info = bl->target_info;
2764 bl->overlay_target_info.reqstd_address = addr;
2765
2766 /* No overlay handling: just set the breakpoint. */
2767 TRY
2768 {
2769 int val;
2770
2771 val = target_insert_breakpoint (bl->gdbarch,
2772 &bl->overlay_target_info);
2773 if (val)
2774 bp_err = GENERIC_ERROR;
2775 }
2776 CATCH (e, RETURN_MASK_ALL)
2777 {
2778 bp_err = e.error;
2779 bp_err_message = e.message;
2780 }
2781 END_CATCH
2782
2783 if (bp_err != GDB_NO_ERROR)
2784 fprintf_unfiltered (tmp_error_stream,
2785 "Overlay breakpoint %d "
2786 "failed: in ROM?\n",
2787 bl->owner->number);
2788 }
2789 }
2790 /* Shall we set a breakpoint at the VMA? */
2791 if (section_is_mapped (bl->section))
2792 {
2793 /* Yes. This overlay section is mapped into memory. */
2794 TRY
2795 {
2796 int val;
2797
2798 val = bl->owner->ops->insert_location (bl);
2799 if (val)
2800 bp_err = GENERIC_ERROR;
2801 }
2802 CATCH (e, RETURN_MASK_ALL)
2803 {
2804 bp_err = e.error;
2805 bp_err_message = e.message;
2806 }
2807 END_CATCH
2808 }
2809 else
2810 {
2811 /* No. This breakpoint will not be inserted.
2812 No error, but do not mark the bp as 'inserted'. */
2813 return 0;
2814 }
2815 }
2816
2817 if (bp_err != GDB_NO_ERROR)
2818 {
2819 /* Can't set the breakpoint. */
2820
2821 /* In some cases, we might not be able to insert a
2822 breakpoint in a shared library that has already been
2823 removed, but we have not yet processed the shlib unload
2824 event. Unfortunately, some targets that implement
2825 breakpoint insertion themselves can't tell why the
2826 breakpoint insertion failed (e.g., the remote target
2827 doesn't define error codes), so we must treat generic
2828 errors as memory errors. */
2829 if ((bp_err == GENERIC_ERROR || bp_err == MEMORY_ERROR)
2830 && bl->loc_type == bp_loc_software_breakpoint
2831 && (solib_name_from_address (bl->pspace, bl->address)
2832 || shared_objfile_contains_address_p (bl->pspace,
2833 bl->address)))
2834 {
2835 /* See also: disable_breakpoints_in_shlibs. */
2836 bl->shlib_disabled = 1;
2837 observer_notify_breakpoint_modified (bl->owner);
2838 if (!*disabled_breaks)
2839 {
2840 fprintf_unfiltered (tmp_error_stream,
2841 "Cannot insert breakpoint %d.\n",
2842 bl->owner->number);
2843 fprintf_unfiltered (tmp_error_stream,
2844 "Temporarily disabling shared "
2845 "library breakpoints:\n");
2846 }
2847 *disabled_breaks = 1;
2848 fprintf_unfiltered (tmp_error_stream,
2849 "breakpoint #%d\n", bl->owner->number);
2850 return 0;
2851 }
2852 else
2853 {
2854 if (bl->loc_type == bp_loc_hardware_breakpoint)
2855 {
2856 *hw_breakpoint_error = 1;
2857 *hw_bp_error_explained_already = bp_err_message != NULL;
2858 fprintf_unfiltered (tmp_error_stream,
2859 "Cannot insert hardware breakpoint %d%s",
2860 bl->owner->number, bp_err_message ? ":" : ".\n");
2861 if (bp_err_message != NULL)
2862 fprintf_unfiltered (tmp_error_stream, "%s.\n", bp_err_message);
2863 }
2864 else
2865 {
2866 if (bp_err_message == NULL)
2867 {
2868 char *message
2869 = memory_error_message (TARGET_XFER_E_IO,
2870 bl->gdbarch, bl->address);
2871 struct cleanup *old_chain = make_cleanup (xfree, message);
2872
2873 fprintf_unfiltered (tmp_error_stream,
2874 "Cannot insert breakpoint %d.\n"
2875 "%s\n",
2876 bl->owner->number, message);
2877 do_cleanups (old_chain);
2878 }
2879 else
2880 {
2881 fprintf_unfiltered (tmp_error_stream,
2882 "Cannot insert breakpoint %d: %s\n",
2883 bl->owner->number,
2884 bp_err_message);
2885 }
2886 }
2887 return 1;
2888
2889 }
2890 }
2891 else
2892 bl->inserted = 1;
2893
2894 return 0;
2895 }
2896
2897 else if (bl->loc_type == bp_loc_hardware_watchpoint
2898 /* NOTE drow/2003-09-08: This state only exists for removing
2899 watchpoints. It's not clear that it's necessary... */
2900 && bl->owner->disposition != disp_del_at_next_stop)
2901 {
2902 int val;
2903
2904 gdb_assert (bl->owner->ops != NULL
2905 && bl->owner->ops->insert_location != NULL);
2906
2907 val = bl->owner->ops->insert_location (bl);
2908
2909 /* If trying to set a read-watchpoint, and it turns out it's not
2910 supported, try emulating one with an access watchpoint. */
2911 if (val == 1 && bl->watchpoint_type == hw_read)
2912 {
2913 struct bp_location *loc, **loc_temp;
2914
2915 /* But don't try to insert it, if there's already another
2916 hw_access location that would be considered a duplicate
2917 of this one. */
2918 ALL_BP_LOCATIONS (loc, loc_temp)
2919 if (loc != bl
2920 && loc->watchpoint_type == hw_access
2921 && watchpoint_locations_match (bl, loc))
2922 {
2923 bl->duplicate = 1;
2924 bl->inserted = 1;
2925 bl->target_info = loc->target_info;
2926 bl->watchpoint_type = hw_access;
2927 val = 0;
2928 break;
2929 }
2930
2931 if (val == 1)
2932 {
2933 bl->watchpoint_type = hw_access;
2934 val = bl->owner->ops->insert_location (bl);
2935
2936 if (val)
2937 /* Back to the original value. */
2938 bl->watchpoint_type = hw_read;
2939 }
2940 }
2941
2942 bl->inserted = (val == 0);
2943 }
2944
2945 else if (bl->owner->type == bp_catchpoint)
2946 {
2947 int val;
2948
2949 gdb_assert (bl->owner->ops != NULL
2950 && bl->owner->ops->insert_location != NULL);
2951
2952 val = bl->owner->ops->insert_location (bl);
2953 if (val)
2954 {
2955 bl->owner->enable_state = bp_disabled;
2956
2957 if (val == 1)
2958 warning (_("\
2959 Error inserting catchpoint %d: Your system does not support this type\n\
2960 of catchpoint."), bl->owner->number);
2961 else
2962 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2963 }
2964
2965 bl->inserted = (val == 0);
2966
2967 /* We've already printed an error message if there was a problem
2968 inserting this catchpoint, and we've disabled the catchpoint,
2969 so just return success. */
2970 return 0;
2971 }
2972
2973 return 0;
2974 }
2975
2976 /* This function is called when program space PSPACE is about to be
2977 deleted. It takes care of updating breakpoints to not reference
2978 PSPACE anymore. */
2979
2980 void
2981 breakpoint_program_space_exit (struct program_space *pspace)
2982 {
2983 struct breakpoint *b, *b_temp;
2984 struct bp_location *loc, **loc_temp;
2985
2986 /* Remove any breakpoint that was set through this program space. */
2987 ALL_BREAKPOINTS_SAFE (b, b_temp)
2988 {
2989 if (b->pspace == pspace)
2990 delete_breakpoint (b);
2991 }
2992
2993 /* Breakpoints set through other program spaces could have locations
2994 bound to PSPACE as well. Remove those. */
2995 ALL_BP_LOCATIONS (loc, loc_temp)
2996 {
2997 struct bp_location *tmp;
2998
2999 if (loc->pspace == pspace)
3000 {
3001 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
3002 if (loc->owner->loc == loc)
3003 loc->owner->loc = loc->next;
3004 else
3005 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
3006 if (tmp->next == loc)
3007 {
3008 tmp->next = loc->next;
3009 break;
3010 }
3011 }
3012 }
3013
3014 /* Now update the global location list to permanently delete the
3015 removed locations above. */
3016 update_global_location_list (UGLL_DONT_INSERT);
3017 }
3018
3019 /* Make sure all breakpoints are inserted in inferior.
3020 Throws exception on any error.
3021 A breakpoint that is already inserted won't be inserted
3022 again, so calling this function twice is safe. */
3023 void
3024 insert_breakpoints (void)
3025 {
3026 struct breakpoint *bpt;
3027
3028 ALL_BREAKPOINTS (bpt)
3029 if (is_hardware_watchpoint (bpt))
3030 {
3031 struct watchpoint *w = (struct watchpoint *) bpt;
3032
3033 update_watchpoint (w, 0 /* don't reparse. */);
3034 }
3035
3036 /* Updating watchpoints creates new locations, so update the global
3037 location list. Explicitly tell ugll to insert locations and
3038 ignore breakpoints_always_inserted_mode. */
3039 update_global_location_list (UGLL_INSERT);
3040 }
3041
3042 /* Invoke CALLBACK for each of bp_location. */
3043
3044 void
3045 iterate_over_bp_locations (walk_bp_location_callback callback)
3046 {
3047 struct bp_location *loc, **loc_tmp;
3048
3049 ALL_BP_LOCATIONS (loc, loc_tmp)
3050 {
3051 callback (loc, NULL);
3052 }
3053 }
3054
3055 /* This is used when we need to synch breakpoint conditions between GDB and the
3056 target. It is the case with deleting and disabling of breakpoints when using
3057 always-inserted mode. */
3058
3059 static void
3060 update_inserted_breakpoint_locations (void)
3061 {
3062 struct bp_location *bl, **blp_tmp;
3063 int error_flag = 0;
3064 int val = 0;
3065 int disabled_breaks = 0;
3066 int hw_breakpoint_error = 0;
3067 int hw_bp_details_reported = 0;
3068
3069 struct ui_file *tmp_error_stream = mem_fileopen ();
3070 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
3071
3072 /* Explicitly mark the warning -- this will only be printed if
3073 there was an error. */
3074 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
3075
3076 save_current_space_and_thread ();
3077
3078 ALL_BP_LOCATIONS (bl, blp_tmp)
3079 {
3080 /* We only want to update software breakpoints and hardware
3081 breakpoints. */
3082 if (!is_breakpoint (bl->owner))
3083 continue;
3084
3085 /* We only want to update locations that are already inserted
3086 and need updating. This is to avoid unwanted insertion during
3087 deletion of breakpoints. */
3088 if (!bl->inserted || (bl->inserted && !bl->needs_update))
3089 continue;
3090
3091 switch_to_program_space_and_thread (bl->pspace);
3092
3093 /* For targets that support global breakpoints, there's no need
3094 to select an inferior to insert breakpoint to. In fact, even
3095 if we aren't attached to any process yet, we should still
3096 insert breakpoints. */
3097 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3098 && ptid_equal (inferior_ptid, null_ptid))
3099 continue;
3100
3101 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
3102 &hw_breakpoint_error, &hw_bp_details_reported);
3103 if (val)
3104 error_flag = val;
3105 }
3106
3107 if (error_flag)
3108 {
3109 target_terminal_ours_for_output ();
3110 error_stream (tmp_error_stream);
3111 }
3112
3113 do_cleanups (cleanups);
3114 }
3115
3116 /* Used when starting or continuing the program. */
3117
3118 static void
3119 insert_breakpoint_locations (void)
3120 {
3121 struct breakpoint *bpt;
3122 struct bp_location *bl, **blp_tmp;
3123 int error_flag = 0;
3124 int val = 0;
3125 int disabled_breaks = 0;
3126 int hw_breakpoint_error = 0;
3127 int hw_bp_error_explained_already = 0;
3128
3129 struct ui_file *tmp_error_stream = mem_fileopen ();
3130 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
3131
3132 /* Explicitly mark the warning -- this will only be printed if
3133 there was an error. */
3134 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
3135
3136 save_current_space_and_thread ();
3137
3138 ALL_BP_LOCATIONS (bl, blp_tmp)
3139 {
3140 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
3141 continue;
3142
3143 /* There is no point inserting thread-specific breakpoints if
3144 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3145 has BL->OWNER always non-NULL. */
3146 if (bl->owner->thread != -1
3147 && !valid_global_thread_id (bl->owner->thread))
3148 continue;
3149
3150 switch_to_program_space_and_thread (bl->pspace);
3151
3152 /* For targets that support global breakpoints, there's no need
3153 to select an inferior to insert breakpoint to. In fact, even
3154 if we aren't attached to any process yet, we should still
3155 insert breakpoints. */
3156 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3157 && ptid_equal (inferior_ptid, null_ptid))
3158 continue;
3159
3160 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
3161 &hw_breakpoint_error, &hw_bp_error_explained_already);
3162 if (val)
3163 error_flag = val;
3164 }
3165
3166 /* If we failed to insert all locations of a watchpoint, remove
3167 them, as half-inserted watchpoint is of limited use. */
3168 ALL_BREAKPOINTS (bpt)
3169 {
3170 int some_failed = 0;
3171 struct bp_location *loc;
3172
3173 if (!is_hardware_watchpoint (bpt))
3174 continue;
3175
3176 if (!breakpoint_enabled (bpt))
3177 continue;
3178
3179 if (bpt->disposition == disp_del_at_next_stop)
3180 continue;
3181
3182 for (loc = bpt->loc; loc; loc = loc->next)
3183 if (!loc->inserted && should_be_inserted (loc))
3184 {
3185 some_failed = 1;
3186 break;
3187 }
3188 if (some_failed)
3189 {
3190 for (loc = bpt->loc; loc; loc = loc->next)
3191 if (loc->inserted)
3192 remove_breakpoint (loc, mark_uninserted);
3193
3194 hw_breakpoint_error = 1;
3195 fprintf_unfiltered (tmp_error_stream,
3196 "Could not insert hardware watchpoint %d.\n",
3197 bpt->number);
3198 error_flag = -1;
3199 }
3200 }
3201
3202 if (error_flag)
3203 {
3204 /* If a hardware breakpoint or watchpoint was inserted, add a
3205 message about possibly exhausted resources. */
3206 if (hw_breakpoint_error && !hw_bp_error_explained_already)
3207 {
3208 fprintf_unfiltered (tmp_error_stream,
3209 "Could not insert hardware breakpoints:\n\
3210 You may have requested too many hardware breakpoints/watchpoints.\n");
3211 }
3212 target_terminal_ours_for_output ();
3213 error_stream (tmp_error_stream);
3214 }
3215
3216 do_cleanups (cleanups);
3217 }
3218
3219 /* Used when the program stops.
3220 Returns zero if successful, or non-zero if there was a problem
3221 removing a breakpoint location. */
3222
3223 int
3224 remove_breakpoints (void)
3225 {
3226 struct bp_location *bl, **blp_tmp;
3227 int val = 0;
3228
3229 ALL_BP_LOCATIONS (bl, blp_tmp)
3230 {
3231 if (bl->inserted && !is_tracepoint (bl->owner))
3232 val |= remove_breakpoint (bl, mark_uninserted);
3233 }
3234 return val;
3235 }
3236
3237 /* When a thread exits, remove breakpoints that are related to
3238 that thread. */
3239
3240 static void
3241 remove_threaded_breakpoints (struct thread_info *tp, int silent)
3242 {
3243 struct breakpoint *b, *b_tmp;
3244
3245 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3246 {
3247 if (b->thread == tp->global_num && user_breakpoint_p (b))
3248 {
3249 b->disposition = disp_del_at_next_stop;
3250
3251 printf_filtered (_("\
3252 Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3253 b->number, print_thread_id (tp));
3254
3255 /* Hide it from the user. */
3256 b->number = 0;
3257 }
3258 }
3259 }
3260
3261 /* Remove breakpoints of process PID. */
3262
3263 int
3264 remove_breakpoints_pid (int pid)
3265 {
3266 struct bp_location *bl, **blp_tmp;
3267 int val;
3268 struct inferior *inf = find_inferior_pid (pid);
3269
3270 ALL_BP_LOCATIONS (bl, blp_tmp)
3271 {
3272 if (bl->pspace != inf->pspace)
3273 continue;
3274
3275 if (bl->inserted && !bl->target_info.persist)
3276 {
3277 val = remove_breakpoint (bl, mark_uninserted);
3278 if (val != 0)
3279 return val;
3280 }
3281 }
3282 return 0;
3283 }
3284
3285 int
3286 reattach_breakpoints (int pid)
3287 {
3288 struct cleanup *old_chain;
3289 struct bp_location *bl, **blp_tmp;
3290 int val;
3291 struct ui_file *tmp_error_stream;
3292 int dummy1 = 0, dummy2 = 0, dummy3 = 0;
3293 struct inferior *inf;
3294 struct thread_info *tp;
3295
3296 tp = any_live_thread_of_process (pid);
3297 if (tp == NULL)
3298 return 1;
3299
3300 inf = find_inferior_pid (pid);
3301 old_chain = save_inferior_ptid ();
3302
3303 inferior_ptid = tp->ptid;
3304
3305 tmp_error_stream = mem_fileopen ();
3306 make_cleanup_ui_file_delete (tmp_error_stream);
3307
3308 ALL_BP_LOCATIONS (bl, blp_tmp)
3309 {
3310 if (bl->pspace != inf->pspace)
3311 continue;
3312
3313 if (bl->inserted)
3314 {
3315 bl->inserted = 0;
3316 val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2, &dummy3);
3317 if (val != 0)
3318 {
3319 do_cleanups (old_chain);
3320 return val;
3321 }
3322 }
3323 }
3324 do_cleanups (old_chain);
3325 return 0;
3326 }
3327
3328 static int internal_breakpoint_number = -1;
3329
3330 /* Set the breakpoint number of B, depending on the value of INTERNAL.
3331 If INTERNAL is non-zero, the breakpoint number will be populated
3332 from internal_breakpoint_number and that variable decremented.
3333 Otherwise the breakpoint number will be populated from
3334 breakpoint_count and that value incremented. Internal breakpoints
3335 do not set the internal var bpnum. */
3336 static void
3337 set_breakpoint_number (int internal, struct breakpoint *b)
3338 {
3339 if (internal)
3340 b->number = internal_breakpoint_number--;
3341 else
3342 {
3343 set_breakpoint_count (breakpoint_count + 1);
3344 b->number = breakpoint_count;
3345 }
3346 }
3347
3348 static struct breakpoint *
3349 create_internal_breakpoint (struct gdbarch *gdbarch,
3350 CORE_ADDR address, enum bptype type,
3351 const struct breakpoint_ops *ops)
3352 {
3353 struct symtab_and_line sal;
3354 struct breakpoint *b;
3355
3356 init_sal (&sal); /* Initialize to zeroes. */
3357
3358 sal.pc = address;
3359 sal.section = find_pc_overlay (sal.pc);
3360 sal.pspace = current_program_space;
3361
3362 b = set_raw_breakpoint (gdbarch, sal, type, ops);
3363 b->number = internal_breakpoint_number--;
3364 b->disposition = disp_donttouch;
3365
3366 return b;
3367 }
3368
3369 static const char *const longjmp_names[] =
3370 {
3371 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3372 };
3373 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3374
3375 /* Per-objfile data private to breakpoint.c. */
3376 struct breakpoint_objfile_data
3377 {
3378 /* Minimal symbol for "_ovly_debug_event" (if any). */
3379 struct bound_minimal_symbol overlay_msym;
3380
3381 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3382 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
3383
3384 /* True if we have looked for longjmp probes. */
3385 int longjmp_searched;
3386
3387 /* SystemTap probe points for longjmp (if any). */
3388 VEC (probe_p) *longjmp_probes;
3389
3390 /* Minimal symbol for "std::terminate()" (if any). */
3391 struct bound_minimal_symbol terminate_msym;
3392
3393 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3394 struct bound_minimal_symbol exception_msym;
3395
3396 /* True if we have looked for exception probes. */
3397 int exception_searched;
3398
3399 /* SystemTap probe points for unwinding (if any). */
3400 VEC (probe_p) *exception_probes;
3401 };
3402
3403 static const struct objfile_data *breakpoint_objfile_key;
3404
3405 /* Minimal symbol not found sentinel. */
3406 static struct minimal_symbol msym_not_found;
3407
3408 /* Returns TRUE if MSYM point to the "not found" sentinel. */
3409
3410 static int
3411 msym_not_found_p (const struct minimal_symbol *msym)
3412 {
3413 return msym == &msym_not_found;
3414 }
3415
3416 /* Return per-objfile data needed by breakpoint.c.
3417 Allocate the data if necessary. */
3418
3419 static struct breakpoint_objfile_data *
3420 get_breakpoint_objfile_data (struct objfile *objfile)
3421 {
3422 struct breakpoint_objfile_data *bp_objfile_data;
3423
3424 bp_objfile_data = ((struct breakpoint_objfile_data *)
3425 objfile_data (objfile, breakpoint_objfile_key));
3426 if (bp_objfile_data == NULL)
3427 {
3428 bp_objfile_data =
3429 XOBNEW (&objfile->objfile_obstack, struct breakpoint_objfile_data);
3430
3431 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
3432 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3433 }
3434 return bp_objfile_data;
3435 }
3436
3437 static void
3438 free_breakpoint_probes (struct objfile *obj, void *data)
3439 {
3440 struct breakpoint_objfile_data *bp_objfile_data
3441 = (struct breakpoint_objfile_data *) data;
3442
3443 VEC_free (probe_p, bp_objfile_data->longjmp_probes);
3444 VEC_free (probe_p, bp_objfile_data->exception_probes);
3445 }
3446
3447 static void
3448 create_overlay_event_breakpoint (void)
3449 {
3450 struct objfile *objfile;
3451 const char *const func_name = "_ovly_debug_event";
3452
3453 ALL_OBJFILES (objfile)
3454 {
3455 struct breakpoint *b;
3456 struct breakpoint_objfile_data *bp_objfile_data;
3457 CORE_ADDR addr;
3458 struct explicit_location explicit_loc;
3459
3460 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3461
3462 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
3463 continue;
3464
3465 if (bp_objfile_data->overlay_msym.minsym == NULL)
3466 {
3467 struct bound_minimal_symbol m;
3468
3469 m = lookup_minimal_symbol_text (func_name, objfile);
3470 if (m.minsym == NULL)
3471 {
3472 /* Avoid future lookups in this objfile. */
3473 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
3474 continue;
3475 }
3476 bp_objfile_data->overlay_msym = m;
3477 }
3478
3479 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
3480 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3481 bp_overlay_event,
3482 &internal_breakpoint_ops);
3483 initialize_explicit_location (&explicit_loc);
3484 explicit_loc.function_name = ASTRDUP (func_name);
3485 b->location = new_explicit_location (&explicit_loc);
3486
3487 if (overlay_debugging == ovly_auto)
3488 {
3489 b->enable_state = bp_enabled;
3490 overlay_events_enabled = 1;
3491 }
3492 else
3493 {
3494 b->enable_state = bp_disabled;
3495 overlay_events_enabled = 0;
3496 }
3497 }
3498 }
3499
3500 static void
3501 create_longjmp_master_breakpoint (void)
3502 {
3503 struct program_space *pspace;
3504 struct cleanup *old_chain;
3505
3506 old_chain = save_current_program_space ();
3507
3508 ALL_PSPACES (pspace)
3509 {
3510 struct objfile *objfile;
3511
3512 set_current_program_space (pspace);
3513
3514 ALL_OBJFILES (objfile)
3515 {
3516 int i;
3517 struct gdbarch *gdbarch;
3518 struct breakpoint_objfile_data *bp_objfile_data;
3519
3520 gdbarch = get_objfile_arch (objfile);
3521
3522 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3523
3524 if (!bp_objfile_data->longjmp_searched)
3525 {
3526 VEC (probe_p) *ret;
3527
3528 ret = find_probes_in_objfile (objfile, "libc", "longjmp");
3529 if (ret != NULL)
3530 {
3531 /* We are only interested in checking one element. */
3532 struct probe *p = VEC_index (probe_p, ret, 0);
3533
3534 if (!can_evaluate_probe_arguments (p))
3535 {
3536 /* We cannot use the probe interface here, because it does
3537 not know how to evaluate arguments. */
3538 VEC_free (probe_p, ret);
3539 ret = NULL;
3540 }
3541 }
3542 bp_objfile_data->longjmp_probes = ret;
3543 bp_objfile_data->longjmp_searched = 1;
3544 }
3545
3546 if (bp_objfile_data->longjmp_probes != NULL)
3547 {
3548 int i;
3549 struct probe *probe;
3550 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3551
3552 for (i = 0;
3553 VEC_iterate (probe_p,
3554 bp_objfile_data->longjmp_probes,
3555 i, probe);
3556 ++i)
3557 {
3558 struct breakpoint *b;
3559
3560 b = create_internal_breakpoint (gdbarch,
3561 get_probe_address (probe,
3562 objfile),
3563 bp_longjmp_master,
3564 &internal_breakpoint_ops);
3565 b->location
3566 = new_probe_location ("-probe-stap libc:longjmp");
3567 b->enable_state = bp_disabled;
3568 }
3569
3570 continue;
3571 }
3572
3573 if (!gdbarch_get_longjmp_target_p (gdbarch))
3574 continue;
3575
3576 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
3577 {
3578 struct breakpoint *b;
3579 const char *func_name;
3580 CORE_ADDR addr;
3581 struct explicit_location explicit_loc;
3582
3583 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3584 continue;
3585
3586 func_name = longjmp_names[i];
3587 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3588 {
3589 struct bound_minimal_symbol m;
3590
3591 m = lookup_minimal_symbol_text (func_name, objfile);
3592 if (m.minsym == NULL)
3593 {
3594 /* Prevent future lookups in this objfile. */
3595 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
3596 continue;
3597 }
3598 bp_objfile_data->longjmp_msym[i] = m;
3599 }
3600
3601 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
3602 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3603 &internal_breakpoint_ops);
3604 initialize_explicit_location (&explicit_loc);
3605 explicit_loc.function_name = ASTRDUP (func_name);
3606 b->location = new_explicit_location (&explicit_loc);
3607 b->enable_state = bp_disabled;
3608 }
3609 }
3610 }
3611
3612 do_cleanups (old_chain);
3613 }
3614
3615 /* Create a master std::terminate breakpoint. */
3616 static void
3617 create_std_terminate_master_breakpoint (void)
3618 {
3619 struct program_space *pspace;
3620 struct cleanup *old_chain;
3621 const char *const func_name = "std::terminate()";
3622
3623 old_chain = save_current_program_space ();
3624
3625 ALL_PSPACES (pspace)
3626 {
3627 struct objfile *objfile;
3628 CORE_ADDR addr;
3629
3630 set_current_program_space (pspace);
3631
3632 ALL_OBJFILES (objfile)
3633 {
3634 struct breakpoint *b;
3635 struct breakpoint_objfile_data *bp_objfile_data;
3636 struct explicit_location explicit_loc;
3637
3638 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3639
3640 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3641 continue;
3642
3643 if (bp_objfile_data->terminate_msym.minsym == NULL)
3644 {
3645 struct bound_minimal_symbol m;
3646
3647 m = lookup_minimal_symbol (func_name, NULL, objfile);
3648 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3649 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
3650 {
3651 /* Prevent future lookups in this objfile. */
3652 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3653 continue;
3654 }
3655 bp_objfile_data->terminate_msym = m;
3656 }
3657
3658 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3659 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3660 bp_std_terminate_master,
3661 &internal_breakpoint_ops);
3662 initialize_explicit_location (&explicit_loc);
3663 explicit_loc.function_name = ASTRDUP (func_name);
3664 b->location = new_explicit_location (&explicit_loc);
3665 b->enable_state = bp_disabled;
3666 }
3667 }
3668
3669 do_cleanups (old_chain);
3670 }
3671
3672 /* Install a master breakpoint on the unwinder's debug hook. */
3673
3674 static void
3675 create_exception_master_breakpoint (void)
3676 {
3677 struct objfile *objfile;
3678 const char *const func_name = "_Unwind_DebugHook";
3679
3680 ALL_OBJFILES (objfile)
3681 {
3682 struct breakpoint *b;
3683 struct gdbarch *gdbarch;
3684 struct breakpoint_objfile_data *bp_objfile_data;
3685 CORE_ADDR addr;
3686 struct explicit_location explicit_loc;
3687
3688 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3689
3690 /* We prefer the SystemTap probe point if it exists. */
3691 if (!bp_objfile_data->exception_searched)
3692 {
3693 VEC (probe_p) *ret;
3694
3695 ret = find_probes_in_objfile (objfile, "libgcc", "unwind");
3696
3697 if (ret != NULL)
3698 {
3699 /* We are only interested in checking one element. */
3700 struct probe *p = VEC_index (probe_p, ret, 0);
3701
3702 if (!can_evaluate_probe_arguments (p))
3703 {
3704 /* We cannot use the probe interface here, because it does
3705 not know how to evaluate arguments. */
3706 VEC_free (probe_p, ret);
3707 ret = NULL;
3708 }
3709 }
3710 bp_objfile_data->exception_probes = ret;
3711 bp_objfile_data->exception_searched = 1;
3712 }
3713
3714 if (bp_objfile_data->exception_probes != NULL)
3715 {
3716 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3717 int i;
3718 struct probe *probe;
3719
3720 for (i = 0;
3721 VEC_iterate (probe_p,
3722 bp_objfile_data->exception_probes,
3723 i, probe);
3724 ++i)
3725 {
3726 struct breakpoint *b;
3727
3728 b = create_internal_breakpoint (gdbarch,
3729 get_probe_address (probe,
3730 objfile),
3731 bp_exception_master,
3732 &internal_breakpoint_ops);
3733 b->location
3734 = new_probe_location ("-probe-stap libgcc:unwind");
3735 b->enable_state = bp_disabled;
3736 }
3737
3738 continue;
3739 }
3740
3741 /* Otherwise, try the hook function. */
3742
3743 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3744 continue;
3745
3746 gdbarch = get_objfile_arch (objfile);
3747
3748 if (bp_objfile_data->exception_msym.minsym == NULL)
3749 {
3750 struct bound_minimal_symbol debug_hook;
3751
3752 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3753 if (debug_hook.minsym == NULL)
3754 {
3755 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3756 continue;
3757 }
3758
3759 bp_objfile_data->exception_msym = debug_hook;
3760 }
3761
3762 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
3763 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3764 &current_target);
3765 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3766 &internal_breakpoint_ops);
3767 initialize_explicit_location (&explicit_loc);
3768 explicit_loc.function_name = ASTRDUP (func_name);
3769 b->location = new_explicit_location (&explicit_loc);
3770 b->enable_state = bp_disabled;
3771 }
3772 }
3773
3774 /* Does B have a location spec? */
3775
3776 static int
3777 breakpoint_event_location_empty_p (const struct breakpoint *b)
3778 {
3779 return b->location != NULL && event_location_empty_p (b->location);
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 (breakpoint_event_location_empty_p (b))
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;
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
4208 {
4209 /* Get rid of existing locations, which are no longer
4210 valid. New ones will be created in
4211 update_watchpoint, when the inferior is restarted.
4212 The next update_global_location_list call will
4213 garbage collect them. */
4214 b->loc = NULL;
4215
4216 if (context == inf_starting)
4217 {
4218 /* Reset val field to force reread of starting value in
4219 insert_breakpoints. */
4220 if (w->val)
4221 value_free (w->val);
4222 w->val = NULL;
4223 w->val_valid = 0;
4224 }
4225 }
4226 }
4227 break;
4228 default:
4229 break;
4230 }
4231 }
4232
4233 /* Get rid of the moribund locations. */
4234 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
4235 decref_bp_location (&bl);
4236 VEC_free (bp_location_p, moribund_locations);
4237 }
4238
4239 /* These functions concern about actual breakpoints inserted in the
4240 target --- to e.g. check if we need to do decr_pc adjustment or if
4241 we need to hop over the bkpt --- so we check for address space
4242 match, not program space. */
4243
4244 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4245 exists at PC. It returns ordinary_breakpoint_here if it's an
4246 ordinary breakpoint, or permanent_breakpoint_here if it's a
4247 permanent breakpoint.
4248 - When continuing from a location with an ordinary breakpoint, we
4249 actually single step once before calling insert_breakpoints.
4250 - When continuing from a location with a permanent breakpoint, we
4251 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4252 the target, to advance the PC past the breakpoint. */
4253
4254 enum breakpoint_here
4255 breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
4256 {
4257 struct bp_location *bl, **blp_tmp;
4258 int any_breakpoint_here = 0;
4259
4260 ALL_BP_LOCATIONS (bl, blp_tmp)
4261 {
4262 if (bl->loc_type != bp_loc_software_breakpoint
4263 && bl->loc_type != bp_loc_hardware_breakpoint)
4264 continue;
4265
4266 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
4267 if ((breakpoint_enabled (bl->owner)
4268 || bl->permanent)
4269 && breakpoint_location_address_match (bl, aspace, pc))
4270 {
4271 if (overlay_debugging
4272 && section_is_overlay (bl->section)
4273 && !section_is_mapped (bl->section))
4274 continue; /* unmapped overlay -- can't be a match */
4275 else if (bl->permanent)
4276 return permanent_breakpoint_here;
4277 else
4278 any_breakpoint_here = 1;
4279 }
4280 }
4281
4282 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
4283 }
4284
4285 /* See breakpoint.h. */
4286
4287 int
4288 breakpoint_in_range_p (struct address_space *aspace,
4289 CORE_ADDR addr, ULONGEST len)
4290 {
4291 struct bp_location *bl, **blp_tmp;
4292
4293 ALL_BP_LOCATIONS (bl, blp_tmp)
4294 {
4295 if (bl->loc_type != bp_loc_software_breakpoint
4296 && bl->loc_type != bp_loc_hardware_breakpoint)
4297 continue;
4298
4299 if ((breakpoint_enabled (bl->owner)
4300 || bl->permanent)
4301 && breakpoint_location_address_range_overlap (bl, aspace,
4302 addr, len))
4303 {
4304 if (overlay_debugging
4305 && section_is_overlay (bl->section)
4306 && !section_is_mapped (bl->section))
4307 {
4308 /* Unmapped overlay -- can't be a match. */
4309 continue;
4310 }
4311
4312 return 1;
4313 }
4314 }
4315
4316 return 0;
4317 }
4318
4319 /* Return true if there's a moribund breakpoint at PC. */
4320
4321 int
4322 moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
4323 {
4324 struct bp_location *loc;
4325 int ix;
4326
4327 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
4328 if (breakpoint_location_address_match (loc, aspace, pc))
4329 return 1;
4330
4331 return 0;
4332 }
4333
4334 /* Returns non-zero iff BL is inserted at PC, in address space
4335 ASPACE. */
4336
4337 static int
4338 bp_location_inserted_here_p (struct bp_location *bl,
4339 struct address_space *aspace, CORE_ADDR pc)
4340 {
4341 if (bl->inserted
4342 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4343 aspace, pc))
4344 {
4345 if (overlay_debugging
4346 && section_is_overlay (bl->section)
4347 && !section_is_mapped (bl->section))
4348 return 0; /* unmapped overlay -- can't be a match */
4349 else
4350 return 1;
4351 }
4352 return 0;
4353 }
4354
4355 /* Returns non-zero iff there's a breakpoint inserted at PC. */
4356
4357 int
4358 breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
4359 {
4360 struct bp_location **blp, **blp_tmp = NULL;
4361
4362 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4363 {
4364 struct bp_location *bl = *blp;
4365
4366 if (bl->loc_type != bp_loc_software_breakpoint
4367 && bl->loc_type != bp_loc_hardware_breakpoint)
4368 continue;
4369
4370 if (bp_location_inserted_here_p (bl, aspace, pc))
4371 return 1;
4372 }
4373 return 0;
4374 }
4375
4376 /* This function returns non-zero iff there is a software breakpoint
4377 inserted at PC. */
4378
4379 int
4380 software_breakpoint_inserted_here_p (struct address_space *aspace,
4381 CORE_ADDR pc)
4382 {
4383 struct bp_location **blp, **blp_tmp = NULL;
4384
4385 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4386 {
4387 struct bp_location *bl = *blp;
4388
4389 if (bl->loc_type != bp_loc_software_breakpoint)
4390 continue;
4391
4392 if (bp_location_inserted_here_p (bl, aspace, pc))
4393 return 1;
4394 }
4395
4396 return 0;
4397 }
4398
4399 /* See breakpoint.h. */
4400
4401 int
4402 hardware_breakpoint_inserted_here_p (struct address_space *aspace,
4403 CORE_ADDR pc)
4404 {
4405 struct bp_location **blp, **blp_tmp = NULL;
4406
4407 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4408 {
4409 struct bp_location *bl = *blp;
4410
4411 if (bl->loc_type != bp_loc_hardware_breakpoint)
4412 continue;
4413
4414 if (bp_location_inserted_here_p (bl, aspace, pc))
4415 return 1;
4416 }
4417
4418 return 0;
4419 }
4420
4421 int
4422 hardware_watchpoint_inserted_in_range (struct address_space *aspace,
4423 CORE_ADDR addr, ULONGEST len)
4424 {
4425 struct breakpoint *bpt;
4426
4427 ALL_BREAKPOINTS (bpt)
4428 {
4429 struct bp_location *loc;
4430
4431 if (bpt->type != bp_hardware_watchpoint
4432 && bpt->type != bp_access_watchpoint)
4433 continue;
4434
4435 if (!breakpoint_enabled (bpt))
4436 continue;
4437
4438 for (loc = bpt->loc; loc; loc = loc->next)
4439 if (loc->pspace->aspace == aspace && loc->inserted)
4440 {
4441 CORE_ADDR l, h;
4442
4443 /* Check for intersection. */
4444 l = max (loc->address, addr);
4445 h = min (loc->address + loc->length, addr + len);
4446 if (l < h)
4447 return 1;
4448 }
4449 }
4450 return 0;
4451 }
4452 \f
4453
4454 /* bpstat stuff. External routines' interfaces are documented
4455 in breakpoint.h. */
4456
4457 int
4458 is_catchpoint (struct breakpoint *ep)
4459 {
4460 return (ep->type == bp_catchpoint);
4461 }
4462
4463 /* Frees any storage that is part of a bpstat. Does not walk the
4464 'next' chain. */
4465
4466 static void
4467 bpstat_free (bpstat bs)
4468 {
4469 if (bs->old_val != NULL)
4470 value_free (bs->old_val);
4471 decref_counted_command_line (&bs->commands);
4472 decref_bp_location (&bs->bp_location_at);
4473 xfree (bs);
4474 }
4475
4476 /* Clear a bpstat so that it says we are not at any breakpoint.
4477 Also free any storage that is part of a bpstat. */
4478
4479 void
4480 bpstat_clear (bpstat *bsp)
4481 {
4482 bpstat p;
4483 bpstat q;
4484
4485 if (bsp == 0)
4486 return;
4487 p = *bsp;
4488 while (p != NULL)
4489 {
4490 q = p->next;
4491 bpstat_free (p);
4492 p = q;
4493 }
4494 *bsp = NULL;
4495 }
4496
4497 /* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4498 is part of the bpstat is copied as well. */
4499
4500 bpstat
4501 bpstat_copy (bpstat bs)
4502 {
4503 bpstat p = NULL;
4504 bpstat tmp;
4505 bpstat retval = NULL;
4506
4507 if (bs == NULL)
4508 return bs;
4509
4510 for (; bs != NULL; bs = bs->next)
4511 {
4512 tmp = (bpstat) xmalloc (sizeof (*tmp));
4513 memcpy (tmp, bs, sizeof (*tmp));
4514 incref_counted_command_line (tmp->commands);
4515 incref_bp_location (tmp->bp_location_at);
4516 if (bs->old_val != NULL)
4517 {
4518 tmp->old_val = value_copy (bs->old_val);
4519 release_value (tmp->old_val);
4520 }
4521
4522 if (p == NULL)
4523 /* This is the first thing in the chain. */
4524 retval = tmp;
4525 else
4526 p->next = tmp;
4527 p = tmp;
4528 }
4529 p->next = NULL;
4530 return retval;
4531 }
4532
4533 /* Find the bpstat associated with this breakpoint. */
4534
4535 bpstat
4536 bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
4537 {
4538 if (bsp == NULL)
4539 return NULL;
4540
4541 for (; bsp != NULL; bsp = bsp->next)
4542 {
4543 if (bsp->breakpoint_at == breakpoint)
4544 return bsp;
4545 }
4546 return NULL;
4547 }
4548
4549 /* See breakpoint.h. */
4550
4551 int
4552 bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
4553 {
4554 for (; bsp != NULL; bsp = bsp->next)
4555 {
4556 if (bsp->breakpoint_at == NULL)
4557 {
4558 /* A moribund location can never explain a signal other than
4559 GDB_SIGNAL_TRAP. */
4560 if (sig == GDB_SIGNAL_TRAP)
4561 return 1;
4562 }
4563 else
4564 {
4565 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4566 sig))
4567 return 1;
4568 }
4569 }
4570
4571 return 0;
4572 }
4573
4574 /* Put in *NUM the breakpoint number of the first breakpoint we are
4575 stopped at. *BSP upon return is a bpstat which points to the
4576 remaining breakpoints stopped at (but which is not guaranteed to be
4577 good for anything but further calls to bpstat_num).
4578
4579 Return 0 if passed a bpstat which does not indicate any breakpoints.
4580 Return -1 if stopped at a breakpoint that has been deleted since
4581 we set it.
4582 Return 1 otherwise. */
4583
4584 int
4585 bpstat_num (bpstat *bsp, int *num)
4586 {
4587 struct breakpoint *b;
4588
4589 if ((*bsp) == NULL)
4590 return 0; /* No more breakpoint values */
4591
4592 /* We assume we'll never have several bpstats that correspond to a
4593 single breakpoint -- otherwise, this function might return the
4594 same number more than once and this will look ugly. */
4595 b = (*bsp)->breakpoint_at;
4596 *bsp = (*bsp)->next;
4597 if (b == NULL)
4598 return -1; /* breakpoint that's been deleted since */
4599
4600 *num = b->number; /* We have its number */
4601 return 1;
4602 }
4603
4604 /* See breakpoint.h. */
4605
4606 void
4607 bpstat_clear_actions (void)
4608 {
4609 struct thread_info *tp;
4610 bpstat bs;
4611
4612 if (ptid_equal (inferior_ptid, null_ptid))
4613 return;
4614
4615 tp = find_thread_ptid (inferior_ptid);
4616 if (tp == NULL)
4617 return;
4618
4619 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
4620 {
4621 decref_counted_command_line (&bs->commands);
4622
4623 if (bs->old_val != NULL)
4624 {
4625 value_free (bs->old_val);
4626 bs->old_val = NULL;
4627 }
4628 }
4629 }
4630
4631 /* Called when a command is about to proceed the inferior. */
4632
4633 static void
4634 breakpoint_about_to_proceed (void)
4635 {
4636 if (!ptid_equal (inferior_ptid, null_ptid))
4637 {
4638 struct thread_info *tp = inferior_thread ();
4639
4640 /* Allow inferior function calls in breakpoint commands to not
4641 interrupt the command list. When the call finishes
4642 successfully, the inferior will be standing at the same
4643 breakpoint as if nothing happened. */
4644 if (tp->control.in_infcall)
4645 return;
4646 }
4647
4648 breakpoint_proceeded = 1;
4649 }
4650
4651 /* Stub for cleaning up our state if we error-out of a breakpoint
4652 command. */
4653 static void
4654 cleanup_executing_breakpoints (void *ignore)
4655 {
4656 executing_breakpoint_commands = 0;
4657 }
4658
4659 /* Return non-zero iff CMD as the first line of a command sequence is `silent'
4660 or its equivalent. */
4661
4662 static int
4663 command_line_is_silent (struct command_line *cmd)
4664 {
4665 return cmd && (strcmp ("silent", cmd->line) == 0);
4666 }
4667
4668 /* Execute all the commands associated with all the breakpoints at
4669 this location. Any of these commands could cause the process to
4670 proceed beyond this point, etc. We look out for such changes by
4671 checking the global "breakpoint_proceeded" after each command.
4672
4673 Returns true if a breakpoint command resumed the inferior. In that
4674 case, it is the caller's responsibility to recall it again with the
4675 bpstat of the current thread. */
4676
4677 static int
4678 bpstat_do_actions_1 (bpstat *bsp)
4679 {
4680 bpstat bs;
4681 struct cleanup *old_chain;
4682 int again = 0;
4683
4684 /* Avoid endless recursion if a `source' command is contained
4685 in bs->commands. */
4686 if (executing_breakpoint_commands)
4687 return 0;
4688
4689 executing_breakpoint_commands = 1;
4690 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
4691
4692 prevent_dont_repeat ();
4693
4694 /* This pointer will iterate over the list of bpstat's. */
4695 bs = *bsp;
4696
4697 breakpoint_proceeded = 0;
4698 for (; bs != NULL; bs = bs->next)
4699 {
4700 struct counted_command_line *ccmd;
4701 struct command_line *cmd;
4702 struct cleanup *this_cmd_tree_chain;
4703
4704 /* Take ownership of the BSP's command tree, if it has one.
4705
4706 The command tree could legitimately contain commands like
4707 'step' and 'next', which call clear_proceed_status, which
4708 frees stop_bpstat's command tree. To make sure this doesn't
4709 free the tree we're executing out from under us, we need to
4710 take ownership of the tree ourselves. Since a given bpstat's
4711 commands are only executed once, we don't need to copy it; we
4712 can clear the pointer in the bpstat, and make sure we free
4713 the tree when we're done. */
4714 ccmd = bs->commands;
4715 bs->commands = NULL;
4716 this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
4717 cmd = ccmd ? ccmd->commands : NULL;
4718 if (command_line_is_silent (cmd))
4719 {
4720 /* The action has been already done by bpstat_stop_status. */
4721 cmd = cmd->next;
4722 }
4723
4724 while (cmd != NULL)
4725 {
4726 execute_control_command (cmd);
4727
4728 if (breakpoint_proceeded)
4729 break;
4730 else
4731 cmd = cmd->next;
4732 }
4733
4734 /* We can free this command tree now. */
4735 do_cleanups (this_cmd_tree_chain);
4736
4737 if (breakpoint_proceeded)
4738 {
4739 if (current_ui->async)
4740 /* If we are in async mode, then the target might be still
4741 running, not stopped at any breakpoint, so nothing for
4742 us to do here -- just return to the event loop. */
4743 ;
4744 else
4745 /* In sync mode, when execute_control_command returns
4746 we're already standing on the next breakpoint.
4747 Breakpoint commands for that stop were not run, since
4748 execute_command does not run breakpoint commands --
4749 only command_line_handler does, but that one is not
4750 involved in execution of breakpoint commands. So, we
4751 can now execute breakpoint commands. It should be
4752 noted that making execute_command do bpstat actions is
4753 not an option -- in this case we'll have recursive
4754 invocation of bpstat for each breakpoint with a
4755 command, and can easily blow up GDB stack. Instead, we
4756 return true, which will trigger the caller to recall us
4757 with the new stop_bpstat. */
4758 again = 1;
4759 break;
4760 }
4761 }
4762 do_cleanups (old_chain);
4763 return again;
4764 }
4765
4766 void
4767 bpstat_do_actions (void)
4768 {
4769 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4770
4771 /* Do any commands attached to breakpoint we are stopped at. */
4772 while (!ptid_equal (inferior_ptid, null_ptid)
4773 && target_has_execution
4774 && !is_exited (inferior_ptid)
4775 && !is_executing (inferior_ptid))
4776 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4777 and only return when it is stopped at the next breakpoint, we
4778 keep doing breakpoint actions until it returns false to
4779 indicate the inferior was not resumed. */
4780 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
4781 break;
4782
4783 discard_cleanups (cleanup_if_error);
4784 }
4785
4786 /* Print out the (old or new) value associated with a watchpoint. */
4787
4788 static void
4789 watchpoint_value_print (struct value *val, struct ui_file *stream)
4790 {
4791 if (val == NULL)
4792 fprintf_unfiltered (stream, _("<unreadable>"));
4793 else
4794 {
4795 struct value_print_options opts;
4796 get_user_print_options (&opts);
4797 value_print (val, stream, &opts);
4798 }
4799 }
4800
4801 /* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4802 debugging multiple threads. */
4803
4804 void
4805 maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4806 {
4807 if (ui_out_is_mi_like_p (uiout))
4808 return;
4809
4810 ui_out_text (uiout, "\n");
4811
4812 if (show_thread_that_caused_stop ())
4813 {
4814 const char *name;
4815 struct thread_info *thr = inferior_thread ();
4816
4817 ui_out_text (uiout, "Thread ");
4818 ui_out_field_fmt (uiout, "thread-id", "%s", print_thread_id (thr));
4819
4820 name = thr->name != NULL ? thr->name : target_thread_name (thr);
4821 if (name != NULL)
4822 {
4823 ui_out_text (uiout, " \"");
4824 ui_out_field_fmt (uiout, "name", "%s", name);
4825 ui_out_text (uiout, "\"");
4826 }
4827
4828 ui_out_text (uiout, " hit ");
4829 }
4830 }
4831
4832 /* Generic routine for printing messages indicating why we
4833 stopped. The behavior of this function depends on the value
4834 'print_it' in the bpstat structure. Under some circumstances we
4835 may decide not to print anything here and delegate the task to
4836 normal_stop(). */
4837
4838 static enum print_stop_action
4839 print_bp_stop_message (bpstat bs)
4840 {
4841 switch (bs->print_it)
4842 {
4843 case print_it_noop:
4844 /* Nothing should be printed for this bpstat entry. */
4845 return PRINT_UNKNOWN;
4846 break;
4847
4848 case print_it_done:
4849 /* We still want to print the frame, but we already printed the
4850 relevant messages. */
4851 return PRINT_SRC_AND_LOC;
4852 break;
4853
4854 case print_it_normal:
4855 {
4856 struct breakpoint *b = bs->breakpoint_at;
4857
4858 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4859 which has since been deleted. */
4860 if (b == NULL)
4861 return PRINT_UNKNOWN;
4862
4863 /* Normal case. Call the breakpoint's print_it method. */
4864 return b->ops->print_it (bs);
4865 }
4866 break;
4867
4868 default:
4869 internal_error (__FILE__, __LINE__,
4870 _("print_bp_stop_message: unrecognized enum value"));
4871 break;
4872 }
4873 }
4874
4875 /* A helper function that prints a shared library stopped event. */
4876
4877 static void
4878 print_solib_event (int is_catchpoint)
4879 {
4880 int any_deleted
4881 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4882 int any_added
4883 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4884
4885 if (!is_catchpoint)
4886 {
4887 if (any_added || any_deleted)
4888 ui_out_text (current_uiout,
4889 _("Stopped due to shared library event:\n"));
4890 else
4891 ui_out_text (current_uiout,
4892 _("Stopped due to shared library event (no "
4893 "libraries added or removed)\n"));
4894 }
4895
4896 if (ui_out_is_mi_like_p (current_uiout))
4897 ui_out_field_string (current_uiout, "reason",
4898 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4899
4900 if (any_deleted)
4901 {
4902 struct cleanup *cleanup;
4903 char *name;
4904 int ix;
4905
4906 ui_out_text (current_uiout, _(" Inferior unloaded "));
4907 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4908 "removed");
4909 for (ix = 0;
4910 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4911 ix, name);
4912 ++ix)
4913 {
4914 if (ix > 0)
4915 ui_out_text (current_uiout, " ");
4916 ui_out_field_string (current_uiout, "library", name);
4917 ui_out_text (current_uiout, "\n");
4918 }
4919
4920 do_cleanups (cleanup);
4921 }
4922
4923 if (any_added)
4924 {
4925 struct so_list *iter;
4926 int ix;
4927 struct cleanup *cleanup;
4928
4929 ui_out_text (current_uiout, _(" Inferior loaded "));
4930 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4931 "added");
4932 for (ix = 0;
4933 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4934 ix, iter);
4935 ++ix)
4936 {
4937 if (ix > 0)
4938 ui_out_text (current_uiout, " ");
4939 ui_out_field_string (current_uiout, "library", iter->so_name);
4940 ui_out_text (current_uiout, "\n");
4941 }
4942
4943 do_cleanups (cleanup);
4944 }
4945 }
4946
4947 /* Print a message indicating what happened. This is called from
4948 normal_stop(). The input to this routine is the head of the bpstat
4949 list - a list of the eventpoints that caused this stop. KIND is
4950 the target_waitkind for the stopping event. This
4951 routine calls the generic print routine for printing a message
4952 about reasons for stopping. This will print (for example) the
4953 "Breakpoint n," part of the output. The return value of this
4954 routine is one of:
4955
4956 PRINT_UNKNOWN: Means we printed nothing.
4957 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4958 code to print the location. An example is
4959 "Breakpoint 1, " which should be followed by
4960 the location.
4961 PRINT_SRC_ONLY: Means we printed something, but there is no need
4962 to also print the location part of the message.
4963 An example is the catch/throw messages, which
4964 don't require a location appended to the end.
4965 PRINT_NOTHING: We have done some printing and we don't need any
4966 further info to be printed. */
4967
4968 enum print_stop_action
4969 bpstat_print (bpstat bs, int kind)
4970 {
4971 enum print_stop_action val;
4972
4973 /* Maybe another breakpoint in the chain caused us to stop.
4974 (Currently all watchpoints go on the bpstat whether hit or not.
4975 That probably could (should) be changed, provided care is taken
4976 with respect to bpstat_explains_signal). */
4977 for (; bs; bs = bs->next)
4978 {
4979 val = print_bp_stop_message (bs);
4980 if (val == PRINT_SRC_ONLY
4981 || val == PRINT_SRC_AND_LOC
4982 || val == PRINT_NOTHING)
4983 return val;
4984 }
4985
4986 /* If we had hit a shared library event breakpoint,
4987 print_bp_stop_message would print out this message. If we hit an
4988 OS-level shared library event, do the same thing. */
4989 if (kind == TARGET_WAITKIND_LOADED)
4990 {
4991 print_solib_event (0);
4992 return PRINT_NOTHING;
4993 }
4994
4995 /* We reached the end of the chain, or we got a null BS to start
4996 with and nothing was printed. */
4997 return PRINT_UNKNOWN;
4998 }
4999
5000 /* Evaluate the expression EXP and return 1 if value is zero.
5001 This returns the inverse of the condition because it is called
5002 from catch_errors which returns 0 if an exception happened, and if an
5003 exception happens we want execution to stop.
5004 The argument is a "struct expression *" that has been cast to a
5005 "void *" to make it pass through catch_errors. */
5006
5007 static int
5008 breakpoint_cond_eval (void *exp)
5009 {
5010 struct value *mark = value_mark ();
5011 int i = !value_true (evaluate_expression ((struct expression *) exp));
5012
5013 value_free_to_mark (mark);
5014 return i;
5015 }
5016
5017 /* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
5018
5019 static bpstat
5020 bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
5021 {
5022 bpstat bs;
5023
5024 bs = (bpstat) xmalloc (sizeof (*bs));
5025 bs->next = NULL;
5026 **bs_link_pointer = bs;
5027 *bs_link_pointer = &bs->next;
5028 bs->breakpoint_at = bl->owner;
5029 bs->bp_location_at = bl;
5030 incref_bp_location (bl);
5031 /* If the condition is false, etc., don't do the commands. */
5032 bs->commands = NULL;
5033 bs->old_val = NULL;
5034 bs->print_it = print_it_normal;
5035 return bs;
5036 }
5037 \f
5038 /* The target has stopped with waitstatus WS. Check if any hardware
5039 watchpoints have triggered, according to the target. */
5040
5041 int
5042 watchpoints_triggered (struct target_waitstatus *ws)
5043 {
5044 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
5045 CORE_ADDR addr;
5046 struct breakpoint *b;
5047
5048 if (!stopped_by_watchpoint)
5049 {
5050 /* We were not stopped by a watchpoint. Mark all watchpoints
5051 as not triggered. */
5052 ALL_BREAKPOINTS (b)
5053 if (is_hardware_watchpoint (b))
5054 {
5055 struct watchpoint *w = (struct watchpoint *) b;
5056
5057 w->watchpoint_triggered = watch_triggered_no;
5058 }
5059
5060 return 0;
5061 }
5062
5063 if (!target_stopped_data_address (&current_target, &addr))
5064 {
5065 /* We were stopped by a watchpoint, but we don't know where.
5066 Mark all watchpoints as unknown. */
5067 ALL_BREAKPOINTS (b)
5068 if (is_hardware_watchpoint (b))
5069 {
5070 struct watchpoint *w = (struct watchpoint *) b;
5071
5072 w->watchpoint_triggered = watch_triggered_unknown;
5073 }
5074
5075 return 1;
5076 }
5077
5078 /* The target could report the data address. Mark watchpoints
5079 affected by this data address as triggered, and all others as not
5080 triggered. */
5081
5082 ALL_BREAKPOINTS (b)
5083 if (is_hardware_watchpoint (b))
5084 {
5085 struct watchpoint *w = (struct watchpoint *) b;
5086 struct bp_location *loc;
5087
5088 w->watchpoint_triggered = watch_triggered_no;
5089 for (loc = b->loc; loc; loc = loc->next)
5090 {
5091 if (is_masked_watchpoint (b))
5092 {
5093 CORE_ADDR newaddr = addr & w->hw_wp_mask;
5094 CORE_ADDR start = loc->address & w->hw_wp_mask;
5095
5096 if (newaddr == start)
5097 {
5098 w->watchpoint_triggered = watch_triggered_yes;
5099 break;
5100 }
5101 }
5102 /* Exact match not required. Within range is sufficient. */
5103 else if (target_watchpoint_addr_within_range (&current_target,
5104 addr, loc->address,
5105 loc->length))
5106 {
5107 w->watchpoint_triggered = watch_triggered_yes;
5108 break;
5109 }
5110 }
5111 }
5112
5113 return 1;
5114 }
5115
5116 /* Possible return values for watchpoint_check (this can't be an enum
5117 because of check_errors). */
5118 /* The watchpoint has been deleted. */
5119 #define WP_DELETED 1
5120 /* The value has changed. */
5121 #define WP_VALUE_CHANGED 2
5122 /* The value has not changed. */
5123 #define WP_VALUE_NOT_CHANGED 3
5124 /* Ignore this watchpoint, no matter if the value changed or not. */
5125 #define WP_IGNORE 4
5126
5127 #define BP_TEMPFLAG 1
5128 #define BP_HARDWAREFLAG 2
5129
5130 /* Evaluate watchpoint condition expression and check if its value
5131 changed.
5132
5133 P should be a pointer to struct bpstat, but is defined as a void *
5134 in order for this function to be usable with catch_errors. */
5135
5136 static int
5137 watchpoint_check (void *p)
5138 {
5139 bpstat bs = (bpstat) p;
5140 struct watchpoint *b;
5141 struct frame_info *fr;
5142 int within_current_scope;
5143
5144 /* BS is built from an existing struct breakpoint. */
5145 gdb_assert (bs->breakpoint_at != NULL);
5146 b = (struct watchpoint *) bs->breakpoint_at;
5147
5148 /* If this is a local watchpoint, we only want to check if the
5149 watchpoint frame is in scope if the current thread is the thread
5150 that was used to create the watchpoint. */
5151 if (!watchpoint_in_thread_scope (b))
5152 return WP_IGNORE;
5153
5154 if (b->exp_valid_block == NULL)
5155 within_current_scope = 1;
5156 else
5157 {
5158 struct frame_info *frame = get_current_frame ();
5159 struct gdbarch *frame_arch = get_frame_arch (frame);
5160 CORE_ADDR frame_pc = get_frame_pc (frame);
5161
5162 /* stack_frame_destroyed_p() returns a non-zero value if we're
5163 still in the function but the stack frame has already been
5164 invalidated. Since we can't rely on the values of local
5165 variables after the stack has been destroyed, we are treating
5166 the watchpoint in that state as `not changed' without further
5167 checking. Don't mark watchpoints as changed if the current
5168 frame is in an epilogue - even if they are in some other
5169 frame, our view of the stack is likely to be wrong and
5170 frame_find_by_id could error out. */
5171 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
5172 return WP_IGNORE;
5173
5174 fr = frame_find_by_id (b->watchpoint_frame);
5175 within_current_scope = (fr != NULL);
5176
5177 /* If we've gotten confused in the unwinder, we might have
5178 returned a frame that can't describe this variable. */
5179 if (within_current_scope)
5180 {
5181 struct symbol *function;
5182
5183 function = get_frame_function (fr);
5184 if (function == NULL
5185 || !contained_in (b->exp_valid_block,
5186 SYMBOL_BLOCK_VALUE (function)))
5187 within_current_scope = 0;
5188 }
5189
5190 if (within_current_scope)
5191 /* If we end up stopping, the current frame will get selected
5192 in normal_stop. So this call to select_frame won't affect
5193 the user. */
5194 select_frame (fr);
5195 }
5196
5197 if (within_current_scope)
5198 {
5199 /* We use value_{,free_to_}mark because it could be a *long*
5200 time before we return to the command level and call
5201 free_all_values. We can't call free_all_values because we
5202 might be in the middle of evaluating a function call. */
5203
5204 int pc = 0;
5205 struct value *mark;
5206 struct value *new_val;
5207
5208 if (is_masked_watchpoint (&b->base))
5209 /* Since we don't know the exact trigger address (from
5210 stopped_data_address), just tell the user we've triggered
5211 a mask watchpoint. */
5212 return WP_VALUE_CHANGED;
5213
5214 mark = value_mark ();
5215 fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL, 0);
5216
5217 if (b->val_bitsize != 0)
5218 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5219
5220 /* We use value_equal_contents instead of value_equal because
5221 the latter coerces an array to a pointer, thus comparing just
5222 the address of the array instead of its contents. This is
5223 not what we want. */
5224 if ((b->val != NULL) != (new_val != NULL)
5225 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
5226 {
5227 if (new_val != NULL)
5228 {
5229 release_value (new_val);
5230 value_free_to_mark (mark);
5231 }
5232 bs->old_val = b->val;
5233 b->val = new_val;
5234 b->val_valid = 1;
5235 return WP_VALUE_CHANGED;
5236 }
5237 else
5238 {
5239 /* Nothing changed. */
5240 value_free_to_mark (mark);
5241 return WP_VALUE_NOT_CHANGED;
5242 }
5243 }
5244 else
5245 {
5246 struct switch_thru_all_uis state;
5247
5248 /* This seems like the only logical thing to do because
5249 if we temporarily ignored the watchpoint, then when
5250 we reenter the block in which it is valid it contains
5251 garbage (in the case of a function, it may have two
5252 garbage values, one before and one after the prologue).
5253 So we can't even detect the first assignment to it and
5254 watch after that (since the garbage may or may not equal
5255 the first value assigned). */
5256 /* We print all the stop information in
5257 breakpoint_ops->print_it, but in this case, by the time we
5258 call breakpoint_ops->print_it this bp will be deleted
5259 already. So we have no choice but print the information
5260 here. */
5261
5262 SWITCH_THRU_ALL_UIS (state)
5263 {
5264 struct ui_out *uiout = current_uiout;
5265
5266 if (ui_out_is_mi_like_p (uiout))
5267 ui_out_field_string
5268 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
5269 ui_out_text (uiout, "\nWatchpoint ");
5270 ui_out_field_int (uiout, "wpnum", b->base.number);
5271 ui_out_text (uiout,
5272 " deleted because the program has left the block in\n"
5273 "which its expression is valid.\n");
5274 }
5275
5276 /* Make sure the watchpoint's commands aren't executed. */
5277 decref_counted_command_line (&b->base.commands);
5278 watchpoint_del_at_next_stop (b);
5279
5280 return WP_DELETED;
5281 }
5282 }
5283
5284 /* Return true if it looks like target has stopped due to hitting
5285 breakpoint location BL. This function does not check if we should
5286 stop, only if BL explains the stop. */
5287
5288 static int
5289 bpstat_check_location (const struct bp_location *bl,
5290 struct address_space *aspace, CORE_ADDR bp_addr,
5291 const struct target_waitstatus *ws)
5292 {
5293 struct breakpoint *b = bl->owner;
5294
5295 /* BL is from an existing breakpoint. */
5296 gdb_assert (b != NULL);
5297
5298 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
5299 }
5300
5301 /* Determine if the watched values have actually changed, and we
5302 should stop. If not, set BS->stop to 0. */
5303
5304 static void
5305 bpstat_check_watchpoint (bpstat bs)
5306 {
5307 const struct bp_location *bl;
5308 struct watchpoint *b;
5309
5310 /* BS is built for existing struct breakpoint. */
5311 bl = bs->bp_location_at;
5312 gdb_assert (bl != NULL);
5313 b = (struct watchpoint *) bs->breakpoint_at;
5314 gdb_assert (b != NULL);
5315
5316 {
5317 int must_check_value = 0;
5318
5319 if (b->base.type == bp_watchpoint)
5320 /* For a software watchpoint, we must always check the
5321 watched value. */
5322 must_check_value = 1;
5323 else if (b->watchpoint_triggered == watch_triggered_yes)
5324 /* We have a hardware watchpoint (read, write, or access)
5325 and the target earlier reported an address watched by
5326 this watchpoint. */
5327 must_check_value = 1;
5328 else if (b->watchpoint_triggered == watch_triggered_unknown
5329 && b->base.type == bp_hardware_watchpoint)
5330 /* We were stopped by a hardware watchpoint, but the target could
5331 not report the data address. We must check the watchpoint's
5332 value. Access and read watchpoints are out of luck; without
5333 a data address, we can't figure it out. */
5334 must_check_value = 1;
5335
5336 if (must_check_value)
5337 {
5338 char *message
5339 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
5340 b->base.number);
5341 struct cleanup *cleanups = make_cleanup (xfree, message);
5342 int e = catch_errors (watchpoint_check, bs, message,
5343 RETURN_MASK_ALL);
5344 do_cleanups (cleanups);
5345 switch (e)
5346 {
5347 case WP_DELETED:
5348 /* We've already printed what needs to be printed. */
5349 bs->print_it = print_it_done;
5350 /* Stop. */
5351 break;
5352 case WP_IGNORE:
5353 bs->print_it = print_it_noop;
5354 bs->stop = 0;
5355 break;
5356 case WP_VALUE_CHANGED:
5357 if (b->base.type == bp_read_watchpoint)
5358 {
5359 /* There are two cases to consider here:
5360
5361 1. We're watching the triggered memory for reads.
5362 In that case, trust the target, and always report
5363 the watchpoint hit to the user. Even though
5364 reads don't cause value changes, the value may
5365 have changed since the last time it was read, and
5366 since we're not trapping writes, we will not see
5367 those, and as such we should ignore our notion of
5368 old value.
5369
5370 2. We're watching the triggered memory for both
5371 reads and writes. There are two ways this may
5372 happen:
5373
5374 2.1. This is a target that can't break on data
5375 reads only, but can break on accesses (reads or
5376 writes), such as e.g., x86. We detect this case
5377 at the time we try to insert read watchpoints.
5378
5379 2.2. Otherwise, the target supports read
5380 watchpoints, but, the user set an access or write
5381 watchpoint watching the same memory as this read
5382 watchpoint.
5383
5384 If we're watching memory writes as well as reads,
5385 ignore watchpoint hits when we find that the
5386 value hasn't changed, as reads don't cause
5387 changes. This still gives false positives when
5388 the program writes the same value to memory as
5389 what there was already in memory (we will confuse
5390 it for a read), but it's much better than
5391 nothing. */
5392
5393 int other_write_watchpoint = 0;
5394
5395 if (bl->watchpoint_type == hw_read)
5396 {
5397 struct breakpoint *other_b;
5398
5399 ALL_BREAKPOINTS (other_b)
5400 if (other_b->type == bp_hardware_watchpoint
5401 || other_b->type == bp_access_watchpoint)
5402 {
5403 struct watchpoint *other_w =
5404 (struct watchpoint *) other_b;
5405
5406 if (other_w->watchpoint_triggered
5407 == watch_triggered_yes)
5408 {
5409 other_write_watchpoint = 1;
5410 break;
5411 }
5412 }
5413 }
5414
5415 if (other_write_watchpoint
5416 || bl->watchpoint_type == hw_access)
5417 {
5418 /* We're watching the same memory for writes,
5419 and the value changed since the last time we
5420 updated it, so this trap must be for a write.
5421 Ignore it. */
5422 bs->print_it = print_it_noop;
5423 bs->stop = 0;
5424 }
5425 }
5426 break;
5427 case WP_VALUE_NOT_CHANGED:
5428 if (b->base.type == bp_hardware_watchpoint
5429 || b->base.type == bp_watchpoint)
5430 {
5431 /* Don't stop: write watchpoints shouldn't fire if
5432 the value hasn't changed. */
5433 bs->print_it = print_it_noop;
5434 bs->stop = 0;
5435 }
5436 /* Stop. */
5437 break;
5438 default:
5439 /* Can't happen. */
5440 case 0:
5441 /* Error from catch_errors. */
5442 {
5443 struct switch_thru_all_uis state;
5444
5445 SWITCH_THRU_ALL_UIS (state)
5446 {
5447 printf_filtered (_("Watchpoint %d deleted.\n"),
5448 b->base.number);
5449 }
5450 watchpoint_del_at_next_stop (b);
5451 /* We've already printed what needs to be printed. */
5452 bs->print_it = print_it_done;
5453 }
5454 break;
5455 }
5456 }
5457 else /* must_check_value == 0 */
5458 {
5459 /* This is a case where some watchpoint(s) triggered, but
5460 not at the address of this watchpoint, or else no
5461 watchpoint triggered after all. So don't print
5462 anything for this watchpoint. */
5463 bs->print_it = print_it_noop;
5464 bs->stop = 0;
5465 }
5466 }
5467 }
5468
5469 /* For breakpoints that are currently marked as telling gdb to stop,
5470 check conditions (condition proper, frame, thread and ignore count)
5471 of breakpoint referred to by BS. If we should not stop for this
5472 breakpoint, set BS->stop to 0. */
5473
5474 static void
5475 bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5476 {
5477 const struct bp_location *bl;
5478 struct breakpoint *b;
5479 int value_is_zero = 0;
5480 struct expression *cond;
5481
5482 gdb_assert (bs->stop);
5483
5484 /* BS is built for existing struct breakpoint. */
5485 bl = bs->bp_location_at;
5486 gdb_assert (bl != NULL);
5487 b = bs->breakpoint_at;
5488 gdb_assert (b != NULL);
5489
5490 /* Even if the target evaluated the condition on its end and notified GDB, we
5491 need to do so again since GDB does not know if we stopped due to a
5492 breakpoint or a single step breakpoint. */
5493
5494 if (frame_id_p (b->frame_id)
5495 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
5496 {
5497 bs->stop = 0;
5498 return;
5499 }
5500
5501 /* If this is a thread/task-specific breakpoint, don't waste cpu
5502 evaluating the condition if this isn't the specified
5503 thread/task. */
5504 if ((b->thread != -1 && b->thread != ptid_to_global_thread_id (ptid))
5505 || (b->task != 0 && b->task != ada_get_task_number (ptid)))
5506
5507 {
5508 bs->stop = 0;
5509 return;
5510 }
5511
5512 /* Evaluate extension language breakpoints that have a "stop" method
5513 implemented. */
5514 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
5515
5516 if (is_watchpoint (b))
5517 {
5518 struct watchpoint *w = (struct watchpoint *) b;
5519
5520 cond = w->cond_exp;
5521 }
5522 else
5523 cond = bl->cond;
5524
5525 if (cond && b->disposition != disp_del_at_next_stop)
5526 {
5527 int within_current_scope = 1;
5528 struct watchpoint * w;
5529
5530 /* We use value_mark and value_free_to_mark because it could
5531 be a long time before we return to the command level and
5532 call free_all_values. We can't call free_all_values
5533 because we might be in the middle of evaluating a
5534 function call. */
5535 struct value *mark = value_mark ();
5536
5537 if (is_watchpoint (b))
5538 w = (struct watchpoint *) b;
5539 else
5540 w = NULL;
5541
5542 /* Need to select the frame, with all that implies so that
5543 the conditions will have the right context. Because we
5544 use the frame, we will not see an inlined function's
5545 variables when we arrive at a breakpoint at the start
5546 of the inlined function; the current frame will be the
5547 call site. */
5548 if (w == NULL || w->cond_exp_valid_block == NULL)
5549 select_frame (get_current_frame ());
5550 else
5551 {
5552 struct frame_info *frame;
5553
5554 /* For local watchpoint expressions, which particular
5555 instance of a local is being watched matters, so we
5556 keep track of the frame to evaluate the expression
5557 in. To evaluate the condition however, it doesn't
5558 really matter which instantiation of the function
5559 where the condition makes sense triggers the
5560 watchpoint. This allows an expression like "watch
5561 global if q > 10" set in `func', catch writes to
5562 global on all threads that call `func', or catch
5563 writes on all recursive calls of `func' by a single
5564 thread. We simply always evaluate the condition in
5565 the innermost frame that's executing where it makes
5566 sense to evaluate the condition. It seems
5567 intuitive. */
5568 frame = block_innermost_frame (w->cond_exp_valid_block);
5569 if (frame != NULL)
5570 select_frame (frame);
5571 else
5572 within_current_scope = 0;
5573 }
5574 if (within_current_scope)
5575 value_is_zero
5576 = catch_errors (breakpoint_cond_eval, cond,
5577 "Error in testing breakpoint condition:\n",
5578 RETURN_MASK_ALL);
5579 else
5580 {
5581 warning (_("Watchpoint condition cannot be tested "
5582 "in the current scope"));
5583 /* If we failed to set the right context for this
5584 watchpoint, unconditionally report it. */
5585 value_is_zero = 0;
5586 }
5587 /* FIXME-someday, should give breakpoint #. */
5588 value_free_to_mark (mark);
5589 }
5590
5591 if (cond && value_is_zero)
5592 {
5593 bs->stop = 0;
5594 }
5595 else if (b->ignore_count > 0)
5596 {
5597 b->ignore_count--;
5598 bs->stop = 0;
5599 /* Increase the hit count even though we don't stop. */
5600 ++(b->hit_count);
5601 observer_notify_breakpoint_modified (b);
5602 }
5603 }
5604
5605 /* Returns true if we need to track moribund locations of LOC's type
5606 on the current target. */
5607
5608 static int
5609 need_moribund_for_location_type (struct bp_location *loc)
5610 {
5611 return ((loc->loc_type == bp_loc_software_breakpoint
5612 && !target_supports_stopped_by_sw_breakpoint ())
5613 || (loc->loc_type == bp_loc_hardware_breakpoint
5614 && !target_supports_stopped_by_hw_breakpoint ()));
5615 }
5616
5617
5618 /* Get a bpstat associated with having just stopped at address
5619 BP_ADDR in thread PTID.
5620
5621 Determine whether we stopped at a breakpoint, etc, or whether we
5622 don't understand this stop. Result is a chain of bpstat's such
5623 that:
5624
5625 if we don't understand the stop, the result is a null pointer.
5626
5627 if we understand why we stopped, the result is not null.
5628
5629 Each element of the chain refers to a particular breakpoint or
5630 watchpoint at which we have stopped. (We may have stopped for
5631 several reasons concurrently.)
5632
5633 Each element of the chain has valid next, breakpoint_at,
5634 commands, FIXME??? fields. */
5635
5636 bpstat
5637 bpstat_stop_status (struct address_space *aspace,
5638 CORE_ADDR bp_addr, ptid_t ptid,
5639 const struct target_waitstatus *ws)
5640 {
5641 struct breakpoint *b = NULL;
5642 struct bp_location *bl;
5643 struct bp_location *loc;
5644 /* First item of allocated bpstat's. */
5645 bpstat bs_head = NULL, *bs_link = &bs_head;
5646 /* Pointer to the last thing in the chain currently. */
5647 bpstat bs;
5648 int ix;
5649 int need_remove_insert;
5650 int removed_any;
5651
5652 /* First, build the bpstat chain with locations that explain a
5653 target stop, while being careful to not set the target running,
5654 as that may invalidate locations (in particular watchpoint
5655 locations are recreated). Resuming will happen here with
5656 breakpoint conditions or watchpoint expressions that include
5657 inferior function calls. */
5658
5659 ALL_BREAKPOINTS (b)
5660 {
5661 if (!breakpoint_enabled (b))
5662 continue;
5663
5664 for (bl = b->loc; bl != NULL; bl = bl->next)
5665 {
5666 /* For hardware watchpoints, we look only at the first
5667 location. The watchpoint_check function will work on the
5668 entire expression, not the individual locations. For
5669 read watchpoints, the watchpoints_triggered function has
5670 checked all locations already. */
5671 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5672 break;
5673
5674 if (!bl->enabled || bl->shlib_disabled)
5675 continue;
5676
5677 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
5678 continue;
5679
5680 /* Come here if it's a watchpoint, or if the break address
5681 matches. */
5682
5683 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
5684 explain stop. */
5685
5686 /* Assume we stop. Should we find a watchpoint that is not
5687 actually triggered, or if the condition of the breakpoint
5688 evaluates as false, we'll reset 'stop' to 0. */
5689 bs->stop = 1;
5690 bs->print = 1;
5691
5692 /* If this is a scope breakpoint, mark the associated
5693 watchpoint as triggered so that we will handle the
5694 out-of-scope event. We'll get to the watchpoint next
5695 iteration. */
5696 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
5697 {
5698 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5699
5700 w->watchpoint_triggered = watch_triggered_yes;
5701 }
5702 }
5703 }
5704
5705 /* Check if a moribund breakpoint explains the stop. */
5706 if (!target_supports_stopped_by_sw_breakpoint ()
5707 || !target_supports_stopped_by_hw_breakpoint ())
5708 {
5709 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
5710 {
5711 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5712 && need_moribund_for_location_type (loc))
5713 {
5714 bs = bpstat_alloc (loc, &bs_link);
5715 /* For hits of moribund locations, we should just proceed. */
5716 bs->stop = 0;
5717 bs->print = 0;
5718 bs->print_it = print_it_noop;
5719 }
5720 }
5721 }
5722
5723 /* A bit of special processing for shlib breakpoints. We need to
5724 process solib loading here, so that the lists of loaded and
5725 unloaded libraries are correct before we handle "catch load" and
5726 "catch unload". */
5727 for (bs = bs_head; bs != NULL; bs = bs->next)
5728 {
5729 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
5730 {
5731 handle_solib_event ();
5732 break;
5733 }
5734 }
5735
5736 /* Now go through the locations that caused the target to stop, and
5737 check whether we're interested in reporting this stop to higher
5738 layers, or whether we should resume the target transparently. */
5739
5740 removed_any = 0;
5741
5742 for (bs = bs_head; bs != NULL; bs = bs->next)
5743 {
5744 if (!bs->stop)
5745 continue;
5746
5747 b = bs->breakpoint_at;
5748 b->ops->check_status (bs);
5749 if (bs->stop)
5750 {
5751 bpstat_check_breakpoint_conditions (bs, ptid);
5752
5753 if (bs->stop)
5754 {
5755 ++(b->hit_count);
5756 observer_notify_breakpoint_modified (b);
5757
5758 /* We will stop here. */
5759 if (b->disposition == disp_disable)
5760 {
5761 --(b->enable_count);
5762 if (b->enable_count <= 0)
5763 b->enable_state = bp_disabled;
5764 removed_any = 1;
5765 }
5766 if (b->silent)
5767 bs->print = 0;
5768 bs->commands = b->commands;
5769 incref_counted_command_line (bs->commands);
5770 if (command_line_is_silent (bs->commands
5771 ? bs->commands->commands : NULL))
5772 bs->print = 0;
5773
5774 b->ops->after_condition_true (bs);
5775 }
5776
5777 }
5778
5779 /* Print nothing for this entry if we don't stop or don't
5780 print. */
5781 if (!bs->stop || !bs->print)
5782 bs->print_it = print_it_noop;
5783 }
5784
5785 /* If we aren't stopping, the value of some hardware watchpoint may
5786 not have changed, but the intermediate memory locations we are
5787 watching may have. Don't bother if we're stopping; this will get
5788 done later. */
5789 need_remove_insert = 0;
5790 if (! bpstat_causes_stop (bs_head))
5791 for (bs = bs_head; bs != NULL; bs = bs->next)
5792 if (!bs->stop
5793 && bs->breakpoint_at
5794 && is_hardware_watchpoint (bs->breakpoint_at))
5795 {
5796 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5797
5798 update_watchpoint (w, 0 /* don't reparse. */);
5799 need_remove_insert = 1;
5800 }
5801
5802 if (need_remove_insert)
5803 update_global_location_list (UGLL_MAY_INSERT);
5804 else if (removed_any)
5805 update_global_location_list (UGLL_DONT_INSERT);
5806
5807 return bs_head;
5808 }
5809
5810 static void
5811 handle_jit_event (void)
5812 {
5813 struct frame_info *frame;
5814 struct gdbarch *gdbarch;
5815
5816 if (debug_infrun)
5817 fprintf_unfiltered (gdb_stdlog, "handling bp_jit_event\n");
5818
5819 /* Switch terminal for any messages produced by
5820 breakpoint_re_set. */
5821 target_terminal_ours_for_output ();
5822
5823 frame = get_current_frame ();
5824 gdbarch = get_frame_arch (frame);
5825
5826 jit_event_handler (gdbarch);
5827
5828 target_terminal_inferior ();
5829 }
5830
5831 /* Prepare WHAT final decision for infrun. */
5832
5833 /* Decide what infrun needs to do with this bpstat. */
5834
5835 struct bpstat_what
5836 bpstat_what (bpstat bs_head)
5837 {
5838 struct bpstat_what retval;
5839 bpstat bs;
5840
5841 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
5842 retval.call_dummy = STOP_NONE;
5843 retval.is_longjmp = 0;
5844
5845 for (bs = bs_head; bs != NULL; bs = bs->next)
5846 {
5847 /* Extract this BS's action. After processing each BS, we check
5848 if its action overrides all we've seem so far. */
5849 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5850 enum bptype bptype;
5851
5852 if (bs->breakpoint_at == NULL)
5853 {
5854 /* I suspect this can happen if it was a momentary
5855 breakpoint which has since been deleted. */
5856 bptype = bp_none;
5857 }
5858 else
5859 bptype = bs->breakpoint_at->type;
5860
5861 switch (bptype)
5862 {
5863 case bp_none:
5864 break;
5865 case bp_breakpoint:
5866 case bp_hardware_breakpoint:
5867 case bp_single_step:
5868 case bp_until:
5869 case bp_finish:
5870 case bp_shlib_event:
5871 if (bs->stop)
5872 {
5873 if (bs->print)
5874 this_action = BPSTAT_WHAT_STOP_NOISY;
5875 else
5876 this_action = BPSTAT_WHAT_STOP_SILENT;
5877 }
5878 else
5879 this_action = BPSTAT_WHAT_SINGLE;
5880 break;
5881 case bp_watchpoint:
5882 case bp_hardware_watchpoint:
5883 case bp_read_watchpoint:
5884 case bp_access_watchpoint:
5885 if (bs->stop)
5886 {
5887 if (bs->print)
5888 this_action = BPSTAT_WHAT_STOP_NOISY;
5889 else
5890 this_action = BPSTAT_WHAT_STOP_SILENT;
5891 }
5892 else
5893 {
5894 /* There was a watchpoint, but we're not stopping.
5895 This requires no further action. */
5896 }
5897 break;
5898 case bp_longjmp:
5899 case bp_longjmp_call_dummy:
5900 case bp_exception:
5901 if (bs->stop)
5902 {
5903 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5904 retval.is_longjmp = bptype != bp_exception;
5905 }
5906 else
5907 this_action = BPSTAT_WHAT_SINGLE;
5908 break;
5909 case bp_longjmp_resume:
5910 case bp_exception_resume:
5911 if (bs->stop)
5912 {
5913 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5914 retval.is_longjmp = bptype == bp_longjmp_resume;
5915 }
5916 else
5917 this_action = BPSTAT_WHAT_SINGLE;
5918 break;
5919 case bp_step_resume:
5920 if (bs->stop)
5921 this_action = BPSTAT_WHAT_STEP_RESUME;
5922 else
5923 {
5924 /* It is for the wrong frame. */
5925 this_action = BPSTAT_WHAT_SINGLE;
5926 }
5927 break;
5928 case bp_hp_step_resume:
5929 if (bs->stop)
5930 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5931 else
5932 {
5933 /* It is for the wrong frame. */
5934 this_action = BPSTAT_WHAT_SINGLE;
5935 }
5936 break;
5937 case bp_watchpoint_scope:
5938 case bp_thread_event:
5939 case bp_overlay_event:
5940 case bp_longjmp_master:
5941 case bp_std_terminate_master:
5942 case bp_exception_master:
5943 this_action = BPSTAT_WHAT_SINGLE;
5944 break;
5945 case bp_catchpoint:
5946 if (bs->stop)
5947 {
5948 if (bs->print)
5949 this_action = BPSTAT_WHAT_STOP_NOISY;
5950 else
5951 this_action = BPSTAT_WHAT_STOP_SILENT;
5952 }
5953 else
5954 {
5955 /* There was a catchpoint, but we're not stopping.
5956 This requires no further action. */
5957 }
5958 break;
5959 case bp_jit_event:
5960 this_action = BPSTAT_WHAT_SINGLE;
5961 break;
5962 case bp_call_dummy:
5963 /* Make sure the action is stop (silent or noisy),
5964 so infrun.c pops the dummy frame. */
5965 retval.call_dummy = STOP_STACK_DUMMY;
5966 this_action = BPSTAT_WHAT_STOP_SILENT;
5967 break;
5968 case bp_std_terminate:
5969 /* Make sure the action is stop (silent or noisy),
5970 so infrun.c pops the dummy frame. */
5971 retval.call_dummy = STOP_STD_TERMINATE;
5972 this_action = BPSTAT_WHAT_STOP_SILENT;
5973 break;
5974 case bp_tracepoint:
5975 case bp_fast_tracepoint:
5976 case bp_static_tracepoint:
5977 /* Tracepoint hits should not be reported back to GDB, and
5978 if one got through somehow, it should have been filtered
5979 out already. */
5980 internal_error (__FILE__, __LINE__,
5981 _("bpstat_what: tracepoint encountered"));
5982 break;
5983 case bp_gnu_ifunc_resolver:
5984 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5985 this_action = BPSTAT_WHAT_SINGLE;
5986 break;
5987 case bp_gnu_ifunc_resolver_return:
5988 /* The breakpoint will be removed, execution will restart from the
5989 PC of the former breakpoint. */
5990 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5991 break;
5992
5993 case bp_dprintf:
5994 if (bs->stop)
5995 this_action = BPSTAT_WHAT_STOP_SILENT;
5996 else
5997 this_action = BPSTAT_WHAT_SINGLE;
5998 break;
5999
6000 default:
6001 internal_error (__FILE__, __LINE__,
6002 _("bpstat_what: unhandled bptype %d"), (int) bptype);
6003 }
6004
6005 retval.main_action = max (retval.main_action, this_action);
6006 }
6007
6008 return retval;
6009 }
6010
6011 void
6012 bpstat_run_callbacks (bpstat bs_head)
6013 {
6014 bpstat bs;
6015
6016 for (bs = bs_head; bs != NULL; bs = bs->next)
6017 {
6018 struct breakpoint *b = bs->breakpoint_at;
6019
6020 if (b == NULL)
6021 continue;
6022 switch (b->type)
6023 {
6024 case bp_jit_event:
6025 handle_jit_event ();
6026 break;
6027 case bp_gnu_ifunc_resolver:
6028 gnu_ifunc_resolver_stop (b);
6029 break;
6030 case bp_gnu_ifunc_resolver_return:
6031 gnu_ifunc_resolver_return_stop (b);
6032 break;
6033 }
6034 }
6035 }
6036
6037 /* Nonzero if we should step constantly (e.g. watchpoints on machines
6038 without hardware support). This isn't related to a specific bpstat,
6039 just to things like whether watchpoints are set. */
6040
6041 int
6042 bpstat_should_step (void)
6043 {
6044 struct breakpoint *b;
6045
6046 ALL_BREAKPOINTS (b)
6047 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
6048 return 1;
6049 return 0;
6050 }
6051
6052 int
6053 bpstat_causes_stop (bpstat bs)
6054 {
6055 for (; bs != NULL; bs = bs->next)
6056 if (bs->stop)
6057 return 1;
6058
6059 return 0;
6060 }
6061
6062 \f
6063
6064 /* Compute a string of spaces suitable to indent the next line
6065 so it starts at the position corresponding to the table column
6066 named COL_NAME in the currently active table of UIOUT. */
6067
6068 static char *
6069 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
6070 {
6071 static char wrap_indent[80];
6072 int i, total_width, width, align;
6073 char *text;
6074
6075 total_width = 0;
6076 for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
6077 {
6078 if (strcmp (text, col_name) == 0)
6079 {
6080 gdb_assert (total_width < sizeof wrap_indent);
6081 memset (wrap_indent, ' ', total_width);
6082 wrap_indent[total_width] = 0;
6083
6084 return wrap_indent;
6085 }
6086
6087 total_width += width + 1;
6088 }
6089
6090 return NULL;
6091 }
6092
6093 /* Determine if the locations of this breakpoint will have their conditions
6094 evaluated by the target, host or a mix of both. Returns the following:
6095
6096 "host": Host evals condition.
6097 "host or target": Host or Target evals condition.
6098 "target": Target evals condition.
6099 */
6100
6101 static const char *
6102 bp_condition_evaluator (struct breakpoint *b)
6103 {
6104 struct bp_location *bl;
6105 char host_evals = 0;
6106 char target_evals = 0;
6107
6108 if (!b)
6109 return NULL;
6110
6111 if (!is_breakpoint (b))
6112 return NULL;
6113
6114 if (gdb_evaluates_breakpoint_condition_p ()
6115 || !target_supports_evaluation_of_breakpoint_conditions ())
6116 return condition_evaluation_host;
6117
6118 for (bl = b->loc; bl; bl = bl->next)
6119 {
6120 if (bl->cond_bytecode)
6121 target_evals++;
6122 else
6123 host_evals++;
6124 }
6125
6126 if (host_evals && target_evals)
6127 return condition_evaluation_both;
6128 else if (target_evals)
6129 return condition_evaluation_target;
6130 else
6131 return condition_evaluation_host;
6132 }
6133
6134 /* Determine the breakpoint location's condition evaluator. This is
6135 similar to bp_condition_evaluator, but for locations. */
6136
6137 static const char *
6138 bp_location_condition_evaluator (struct bp_location *bl)
6139 {
6140 if (bl && !is_breakpoint (bl->owner))
6141 return NULL;
6142
6143 if (gdb_evaluates_breakpoint_condition_p ()
6144 || !target_supports_evaluation_of_breakpoint_conditions ())
6145 return condition_evaluation_host;
6146
6147 if (bl && bl->cond_bytecode)
6148 return condition_evaluation_target;
6149 else
6150 return condition_evaluation_host;
6151 }
6152
6153 /* Print the LOC location out of the list of B->LOC locations. */
6154
6155 static void
6156 print_breakpoint_location (struct breakpoint *b,
6157 struct bp_location *loc)
6158 {
6159 struct ui_out *uiout = current_uiout;
6160 struct cleanup *old_chain = save_current_program_space ();
6161
6162 if (loc != NULL && loc->shlib_disabled)
6163 loc = NULL;
6164
6165 if (loc != NULL)
6166 set_current_program_space (loc->pspace);
6167
6168 if (b->display_canonical)
6169 ui_out_field_string (uiout, "what",
6170 event_location_to_string (b->location));
6171 else if (loc && loc->symtab)
6172 {
6173 struct symbol *sym
6174 = find_pc_sect_function (loc->address, loc->section);
6175 if (sym)
6176 {
6177 ui_out_text (uiout, "in ");
6178 ui_out_field_string (uiout, "func",
6179 SYMBOL_PRINT_NAME (sym));
6180 ui_out_text (uiout, " ");
6181 ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
6182 ui_out_text (uiout, "at ");
6183 }
6184 ui_out_field_string (uiout, "file",
6185 symtab_to_filename_for_display (loc->symtab));
6186 ui_out_text (uiout, ":");
6187
6188 if (ui_out_is_mi_like_p (uiout))
6189 ui_out_field_string (uiout, "fullname",
6190 symtab_to_fullname (loc->symtab));
6191
6192 ui_out_field_int (uiout, "line", loc->line_number);
6193 }
6194 else if (loc)
6195 {
6196 struct ui_file *stb = mem_fileopen ();
6197 struct cleanup *stb_chain = make_cleanup_ui_file_delete (stb);
6198
6199 print_address_symbolic (loc->gdbarch, loc->address, stb,
6200 demangle, "");
6201 ui_out_field_stream (uiout, "at", stb);
6202
6203 do_cleanups (stb_chain);
6204 }
6205 else
6206 {
6207 ui_out_field_string (uiout, "pending",
6208 event_location_to_string (b->location));
6209 /* If extra_string is available, it could be holding a condition
6210 or dprintf arguments. In either case, make sure it is printed,
6211 too, but only for non-MI streams. */
6212 if (!ui_out_is_mi_like_p (uiout) && b->extra_string != NULL)
6213 {
6214 if (b->type == bp_dprintf)
6215 ui_out_text (uiout, ",");
6216 else
6217 ui_out_text (uiout, " ");
6218 ui_out_text (uiout, b->extra_string);
6219 }
6220 }
6221
6222 if (loc && is_breakpoint (b)
6223 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6224 && bp_condition_evaluator (b) == condition_evaluation_both)
6225 {
6226 ui_out_text (uiout, " (");
6227 ui_out_field_string (uiout, "evaluated-by",
6228 bp_location_condition_evaluator (loc));
6229 ui_out_text (uiout, ")");
6230 }
6231
6232 do_cleanups (old_chain);
6233 }
6234
6235 static const char *
6236 bptype_string (enum bptype type)
6237 {
6238 struct ep_type_description
6239 {
6240 enum bptype type;
6241 char *description;
6242 };
6243 static struct ep_type_description bptypes[] =
6244 {
6245 {bp_none, "?deleted?"},
6246 {bp_breakpoint, "breakpoint"},
6247 {bp_hardware_breakpoint, "hw breakpoint"},
6248 {bp_single_step, "sw single-step"},
6249 {bp_until, "until"},
6250 {bp_finish, "finish"},
6251 {bp_watchpoint, "watchpoint"},
6252 {bp_hardware_watchpoint, "hw watchpoint"},
6253 {bp_read_watchpoint, "read watchpoint"},
6254 {bp_access_watchpoint, "acc watchpoint"},
6255 {bp_longjmp, "longjmp"},
6256 {bp_longjmp_resume, "longjmp resume"},
6257 {bp_longjmp_call_dummy, "longjmp for call dummy"},
6258 {bp_exception, "exception"},
6259 {bp_exception_resume, "exception resume"},
6260 {bp_step_resume, "step resume"},
6261 {bp_hp_step_resume, "high-priority step resume"},
6262 {bp_watchpoint_scope, "watchpoint scope"},
6263 {bp_call_dummy, "call dummy"},
6264 {bp_std_terminate, "std::terminate"},
6265 {bp_shlib_event, "shlib events"},
6266 {bp_thread_event, "thread events"},
6267 {bp_overlay_event, "overlay events"},
6268 {bp_longjmp_master, "longjmp master"},
6269 {bp_std_terminate_master, "std::terminate master"},
6270 {bp_exception_master, "exception master"},
6271 {bp_catchpoint, "catchpoint"},
6272 {bp_tracepoint, "tracepoint"},
6273 {bp_fast_tracepoint, "fast tracepoint"},
6274 {bp_static_tracepoint, "static tracepoint"},
6275 {bp_dprintf, "dprintf"},
6276 {bp_jit_event, "jit events"},
6277 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6278 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
6279 };
6280
6281 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6282 || ((int) type != bptypes[(int) type].type))
6283 internal_error (__FILE__, __LINE__,
6284 _("bptypes table does not describe type #%d."),
6285 (int) type);
6286
6287 return bptypes[(int) type].description;
6288 }
6289
6290 /* For MI, output a field named 'thread-groups' with a list as the value.
6291 For CLI, prefix the list with the string 'inf'. */
6292
6293 static void
6294 output_thread_groups (struct ui_out *uiout,
6295 const char *field_name,
6296 VEC(int) *inf_num,
6297 int mi_only)
6298 {
6299 struct cleanup *back_to;
6300 int is_mi = ui_out_is_mi_like_p (uiout);
6301 int inf;
6302 int i;
6303
6304 /* For backward compatibility, don't display inferiors in CLI unless
6305 there are several. Always display them for MI. */
6306 if (!is_mi && mi_only)
6307 return;
6308
6309 back_to = make_cleanup_ui_out_list_begin_end (uiout, field_name);
6310
6311 for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
6312 {
6313 if (is_mi)
6314 {
6315 char mi_group[10];
6316
6317 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
6318 ui_out_field_string (uiout, NULL, mi_group);
6319 }
6320 else
6321 {
6322 if (i == 0)
6323 ui_out_text (uiout, " inf ");
6324 else
6325 ui_out_text (uiout, ", ");
6326
6327 ui_out_text (uiout, plongest (inf));
6328 }
6329 }
6330
6331 do_cleanups (back_to);
6332 }
6333
6334 /* Print B to gdb_stdout. */
6335
6336 static void
6337 print_one_breakpoint_location (struct breakpoint *b,
6338 struct bp_location *loc,
6339 int loc_number,
6340 struct bp_location **last_loc,
6341 int allflag)
6342 {
6343 struct command_line *l;
6344 static char bpenables[] = "nynny";
6345
6346 struct ui_out *uiout = current_uiout;
6347 int header_of_multiple = 0;
6348 int part_of_multiple = (loc != NULL);
6349 struct value_print_options opts;
6350
6351 get_user_print_options (&opts);
6352
6353 gdb_assert (!loc || loc_number != 0);
6354 /* See comment in print_one_breakpoint concerning treatment of
6355 breakpoints with single disabled location. */
6356 if (loc == NULL
6357 && (b->loc != NULL
6358 && (b->loc->next != NULL || !b->loc->enabled)))
6359 header_of_multiple = 1;
6360 if (loc == NULL)
6361 loc = b->loc;
6362
6363 annotate_record ();
6364
6365 /* 1 */
6366 annotate_field (0);
6367 if (part_of_multiple)
6368 {
6369 char *formatted;
6370 formatted = xstrprintf ("%d.%d", b->number, loc_number);
6371 ui_out_field_string (uiout, "number", formatted);
6372 xfree (formatted);
6373 }
6374 else
6375 {
6376 ui_out_field_int (uiout, "number", b->number);
6377 }
6378
6379 /* 2 */
6380 annotate_field (1);
6381 if (part_of_multiple)
6382 ui_out_field_skip (uiout, "type");
6383 else
6384 ui_out_field_string (uiout, "type", bptype_string (b->type));
6385
6386 /* 3 */
6387 annotate_field (2);
6388 if (part_of_multiple)
6389 ui_out_field_skip (uiout, "disp");
6390 else
6391 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
6392
6393
6394 /* 4 */
6395 annotate_field (3);
6396 if (part_of_multiple)
6397 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
6398 else
6399 ui_out_field_fmt (uiout, "enabled", "%c",
6400 bpenables[(int) b->enable_state]);
6401 ui_out_spaces (uiout, 2);
6402
6403
6404 /* 5 and 6 */
6405 if (b->ops != NULL && b->ops->print_one != NULL)
6406 {
6407 /* Although the print_one can possibly print all locations,
6408 calling it here is not likely to get any nice result. So,
6409 make sure there's just one location. */
6410 gdb_assert (b->loc == NULL || b->loc->next == NULL);
6411 b->ops->print_one (b, last_loc);
6412 }
6413 else
6414 switch (b->type)
6415 {
6416 case bp_none:
6417 internal_error (__FILE__, __LINE__,
6418 _("print_one_breakpoint: bp_none encountered\n"));
6419 break;
6420
6421 case bp_watchpoint:
6422 case bp_hardware_watchpoint:
6423 case bp_read_watchpoint:
6424 case bp_access_watchpoint:
6425 {
6426 struct watchpoint *w = (struct watchpoint *) b;
6427
6428 /* Field 4, the address, is omitted (which makes the columns
6429 not line up too nicely with the headers, but the effect
6430 is relatively readable). */
6431 if (opts.addressprint)
6432 ui_out_field_skip (uiout, "addr");
6433 annotate_field (5);
6434 ui_out_field_string (uiout, "what", w->exp_string);
6435 }
6436 break;
6437
6438 case bp_breakpoint:
6439 case bp_hardware_breakpoint:
6440 case bp_single_step:
6441 case bp_until:
6442 case bp_finish:
6443 case bp_longjmp:
6444 case bp_longjmp_resume:
6445 case bp_longjmp_call_dummy:
6446 case bp_exception:
6447 case bp_exception_resume:
6448 case bp_step_resume:
6449 case bp_hp_step_resume:
6450 case bp_watchpoint_scope:
6451 case bp_call_dummy:
6452 case bp_std_terminate:
6453 case bp_shlib_event:
6454 case bp_thread_event:
6455 case bp_overlay_event:
6456 case bp_longjmp_master:
6457 case bp_std_terminate_master:
6458 case bp_exception_master:
6459 case bp_tracepoint:
6460 case bp_fast_tracepoint:
6461 case bp_static_tracepoint:
6462 case bp_dprintf:
6463 case bp_jit_event:
6464 case bp_gnu_ifunc_resolver:
6465 case bp_gnu_ifunc_resolver_return:
6466 if (opts.addressprint)
6467 {
6468 annotate_field (4);
6469 if (header_of_multiple)
6470 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
6471 else if (b->loc == NULL || loc->shlib_disabled)
6472 ui_out_field_string (uiout, "addr", "<PENDING>");
6473 else
6474 ui_out_field_core_addr (uiout, "addr",
6475 loc->gdbarch, loc->address);
6476 }
6477 annotate_field (5);
6478 if (!header_of_multiple)
6479 print_breakpoint_location (b, loc);
6480 if (b->loc)
6481 *last_loc = b->loc;
6482 break;
6483 }
6484
6485
6486 if (loc != NULL && !header_of_multiple)
6487 {
6488 struct inferior *inf;
6489 VEC(int) *inf_num = NULL;
6490 int mi_only = 1;
6491
6492 ALL_INFERIORS (inf)
6493 {
6494 if (inf->pspace == loc->pspace)
6495 VEC_safe_push (int, inf_num, inf->num);
6496 }
6497
6498 /* For backward compatibility, don't display inferiors in CLI unless
6499 there are several. Always display for MI. */
6500 if (allflag
6501 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6502 && (number_of_program_spaces () > 1
6503 || number_of_inferiors () > 1)
6504 /* LOC is for existing B, it cannot be in
6505 moribund_locations and thus having NULL OWNER. */
6506 && loc->owner->type != bp_catchpoint))
6507 mi_only = 0;
6508 output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6509 VEC_free (int, inf_num);
6510 }
6511
6512 if (!part_of_multiple)
6513 {
6514 if (b->thread != -1)
6515 {
6516 /* FIXME: This seems to be redundant and lost here; see the
6517 "stop only in" line a little further down. */
6518 ui_out_text (uiout, " thread ");
6519 ui_out_field_int (uiout, "thread", b->thread);
6520 }
6521 else if (b->task != 0)
6522 {
6523 ui_out_text (uiout, " task ");
6524 ui_out_field_int (uiout, "task", b->task);
6525 }
6526 }
6527
6528 ui_out_text (uiout, "\n");
6529
6530 if (!part_of_multiple)
6531 b->ops->print_one_detail (b, uiout);
6532
6533 if (part_of_multiple && frame_id_p (b->frame_id))
6534 {
6535 annotate_field (6);
6536 ui_out_text (uiout, "\tstop only in stack frame at ");
6537 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
6538 the frame ID. */
6539 ui_out_field_core_addr (uiout, "frame",
6540 b->gdbarch, b->frame_id.stack_addr);
6541 ui_out_text (uiout, "\n");
6542 }
6543
6544 if (!part_of_multiple && b->cond_string)
6545 {
6546 annotate_field (7);
6547 if (is_tracepoint (b))
6548 ui_out_text (uiout, "\ttrace only if ");
6549 else
6550 ui_out_text (uiout, "\tstop only if ");
6551 ui_out_field_string (uiout, "cond", b->cond_string);
6552
6553 /* Print whether the target is doing the breakpoint's condition
6554 evaluation. If GDB is doing the evaluation, don't print anything. */
6555 if (is_breakpoint (b)
6556 && breakpoint_condition_evaluation_mode ()
6557 == condition_evaluation_target)
6558 {
6559 ui_out_text (uiout, " (");
6560 ui_out_field_string (uiout, "evaluated-by",
6561 bp_condition_evaluator (b));
6562 ui_out_text (uiout, " evals)");
6563 }
6564 ui_out_text (uiout, "\n");
6565 }
6566
6567 if (!part_of_multiple && b->thread != -1)
6568 {
6569 /* FIXME should make an annotation for this. */
6570 ui_out_text (uiout, "\tstop only in thread ");
6571 if (ui_out_is_mi_like_p (uiout))
6572 ui_out_field_int (uiout, "thread", b->thread);
6573 else
6574 {
6575 struct thread_info *thr = find_thread_global_id (b->thread);
6576
6577 ui_out_field_string (uiout, "thread", print_thread_id (thr));
6578 }
6579 ui_out_text (uiout, "\n");
6580 }
6581
6582 if (!part_of_multiple)
6583 {
6584 if (b->hit_count)
6585 {
6586 /* FIXME should make an annotation for this. */
6587 if (is_catchpoint (b))
6588 ui_out_text (uiout, "\tcatchpoint");
6589 else if (is_tracepoint (b))
6590 ui_out_text (uiout, "\ttracepoint");
6591 else
6592 ui_out_text (uiout, "\tbreakpoint");
6593 ui_out_text (uiout, " already hit ");
6594 ui_out_field_int (uiout, "times", b->hit_count);
6595 if (b->hit_count == 1)
6596 ui_out_text (uiout, " time\n");
6597 else
6598 ui_out_text (uiout, " times\n");
6599 }
6600 else
6601 {
6602 /* Output the count also if it is zero, but only if this is mi. */
6603 if (ui_out_is_mi_like_p (uiout))
6604 ui_out_field_int (uiout, "times", b->hit_count);
6605 }
6606 }
6607
6608 if (!part_of_multiple && b->ignore_count)
6609 {
6610 annotate_field (8);
6611 ui_out_text (uiout, "\tignore next ");
6612 ui_out_field_int (uiout, "ignore", b->ignore_count);
6613 ui_out_text (uiout, " hits\n");
6614 }
6615
6616 /* Note that an enable count of 1 corresponds to "enable once"
6617 behavior, which is reported by the combination of enablement and
6618 disposition, so we don't need to mention it here. */
6619 if (!part_of_multiple && b->enable_count > 1)
6620 {
6621 annotate_field (8);
6622 ui_out_text (uiout, "\tdisable after ");
6623 /* Tweak the wording to clarify that ignore and enable counts
6624 are distinct, and have additive effect. */
6625 if (b->ignore_count)
6626 ui_out_text (uiout, "additional ");
6627 else
6628 ui_out_text (uiout, "next ");
6629 ui_out_field_int (uiout, "enable", b->enable_count);
6630 ui_out_text (uiout, " hits\n");
6631 }
6632
6633 if (!part_of_multiple && is_tracepoint (b))
6634 {
6635 struct tracepoint *tp = (struct tracepoint *) b;
6636
6637 if (tp->traceframe_usage)
6638 {
6639 ui_out_text (uiout, "\ttrace buffer usage ");
6640 ui_out_field_int (uiout, "traceframe-usage", tp->traceframe_usage);
6641 ui_out_text (uiout, " bytes\n");
6642 }
6643 }
6644
6645 l = b->commands ? b->commands->commands : NULL;
6646 if (!part_of_multiple && l)
6647 {
6648 struct cleanup *script_chain;
6649
6650 annotate_field (9);
6651 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
6652 print_command_lines (uiout, l, 4);
6653 do_cleanups (script_chain);
6654 }
6655
6656 if (is_tracepoint (b))
6657 {
6658 struct tracepoint *t = (struct tracepoint *) b;
6659
6660 if (!part_of_multiple && t->pass_count)
6661 {
6662 annotate_field (10);
6663 ui_out_text (uiout, "\tpass count ");
6664 ui_out_field_int (uiout, "pass", t->pass_count);
6665 ui_out_text (uiout, " \n");
6666 }
6667
6668 /* Don't display it when tracepoint or tracepoint location is
6669 pending. */
6670 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6671 {
6672 annotate_field (11);
6673
6674 if (ui_out_is_mi_like_p (uiout))
6675 ui_out_field_string (uiout, "installed",
6676 loc->inserted ? "y" : "n");
6677 else
6678 {
6679 if (loc->inserted)
6680 ui_out_text (uiout, "\t");
6681 else
6682 ui_out_text (uiout, "\tnot ");
6683 ui_out_text (uiout, "installed on target\n");
6684 }
6685 }
6686 }
6687
6688 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
6689 {
6690 if (is_watchpoint (b))
6691 {
6692 struct watchpoint *w = (struct watchpoint *) b;
6693
6694 ui_out_field_string (uiout, "original-location", w->exp_string);
6695 }
6696 else if (b->location != NULL
6697 && event_location_to_string (b->location) != NULL)
6698 ui_out_field_string (uiout, "original-location",
6699 event_location_to_string (b->location));
6700 }
6701 }
6702
6703 static void
6704 print_one_breakpoint (struct breakpoint *b,
6705 struct bp_location **last_loc,
6706 int allflag)
6707 {
6708 struct cleanup *bkpt_chain;
6709 struct ui_out *uiout = current_uiout;
6710
6711 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
6712
6713 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
6714 do_cleanups (bkpt_chain);
6715
6716 /* If this breakpoint has custom print function,
6717 it's already printed. Otherwise, print individual
6718 locations, if any. */
6719 if (b->ops == NULL || b->ops->print_one == NULL)
6720 {
6721 /* If breakpoint has a single location that is disabled, we
6722 print it as if it had several locations, since otherwise it's
6723 hard to represent "breakpoint enabled, location disabled"
6724 situation.
6725
6726 Note that while hardware watchpoints have several locations
6727 internally, that's not a property exposed to user. */
6728 if (b->loc
6729 && !is_hardware_watchpoint (b)
6730 && (b->loc->next || !b->loc->enabled))
6731 {
6732 struct bp_location *loc;
6733 int n = 1;
6734
6735 for (loc = b->loc; loc; loc = loc->next, ++n)
6736 {
6737 struct cleanup *inner2 =
6738 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
6739 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
6740 do_cleanups (inner2);
6741 }
6742 }
6743 }
6744 }
6745
6746 static int
6747 breakpoint_address_bits (struct breakpoint *b)
6748 {
6749 int print_address_bits = 0;
6750 struct bp_location *loc;
6751
6752 /* Software watchpoints that aren't watching memory don't have an
6753 address to print. */
6754 if (is_no_memory_software_watchpoint (b))
6755 return 0;
6756
6757 for (loc = b->loc; loc; loc = loc->next)
6758 {
6759 int addr_bit;
6760
6761 addr_bit = gdbarch_addr_bit (loc->gdbarch);
6762 if (addr_bit > print_address_bits)
6763 print_address_bits = addr_bit;
6764 }
6765
6766 return print_address_bits;
6767 }
6768
6769 struct captured_breakpoint_query_args
6770 {
6771 int bnum;
6772 };
6773
6774 static int
6775 do_captured_breakpoint_query (struct ui_out *uiout, void *data)
6776 {
6777 struct captured_breakpoint_query_args *args
6778 = (struct captured_breakpoint_query_args *) data;
6779 struct breakpoint *b;
6780 struct bp_location *dummy_loc = NULL;
6781
6782 ALL_BREAKPOINTS (b)
6783 {
6784 if (args->bnum == b->number)
6785 {
6786 print_one_breakpoint (b, &dummy_loc, 0);
6787 return GDB_RC_OK;
6788 }
6789 }
6790 return GDB_RC_NONE;
6791 }
6792
6793 enum gdb_rc
6794 gdb_breakpoint_query (struct ui_out *uiout, int bnum,
6795 char **error_message)
6796 {
6797 struct captured_breakpoint_query_args args;
6798
6799 args.bnum = bnum;
6800 /* For the moment we don't trust print_one_breakpoint() to not throw
6801 an error. */
6802 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
6803 error_message, RETURN_MASK_ALL) < 0)
6804 return GDB_RC_FAIL;
6805 else
6806 return GDB_RC_OK;
6807 }
6808
6809 /* Return true if this breakpoint was set by the user, false if it is
6810 internal or momentary. */
6811
6812 int
6813 user_breakpoint_p (struct breakpoint *b)
6814 {
6815 return b->number > 0;
6816 }
6817
6818 /* Print information on user settable breakpoint (watchpoint, etc)
6819 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6820 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6821 FILTER is non-NULL, call it on each breakpoint and only include the
6822 ones for which it returns non-zero. Return the total number of
6823 breakpoints listed. */
6824
6825 static int
6826 breakpoint_1 (char *args, int allflag,
6827 int (*filter) (const struct breakpoint *))
6828 {
6829 struct breakpoint *b;
6830 struct bp_location *last_loc = NULL;
6831 int nr_printable_breakpoints;
6832 struct cleanup *bkpttbl_chain;
6833 struct value_print_options opts;
6834 int print_address_bits = 0;
6835 int print_type_col_width = 14;
6836 struct ui_out *uiout = current_uiout;
6837
6838 get_user_print_options (&opts);
6839
6840 /* Compute the number of rows in the table, as well as the size
6841 required for address fields. */
6842 nr_printable_breakpoints = 0;
6843 ALL_BREAKPOINTS (b)
6844 {
6845 /* If we have a filter, only list the breakpoints it accepts. */
6846 if (filter && !filter (b))
6847 continue;
6848
6849 /* If we have an "args" string, it is a list of breakpoints to
6850 accept. Skip the others. */
6851 if (args != NULL && *args != '\0')
6852 {
6853 if (allflag && parse_and_eval_long (args) != b->number)
6854 continue;
6855 if (!allflag && !number_is_in_list (args, b->number))
6856 continue;
6857 }
6858
6859 if (allflag || user_breakpoint_p (b))
6860 {
6861 int addr_bit, type_len;
6862
6863 addr_bit = breakpoint_address_bits (b);
6864 if (addr_bit > print_address_bits)
6865 print_address_bits = addr_bit;
6866
6867 type_len = strlen (bptype_string (b->type));
6868 if (type_len > print_type_col_width)
6869 print_type_col_width = type_len;
6870
6871 nr_printable_breakpoints++;
6872 }
6873 }
6874
6875 if (opts.addressprint)
6876 bkpttbl_chain
6877 = make_cleanup_ui_out_table_begin_end (uiout, 6,
6878 nr_printable_breakpoints,
6879 "BreakpointTable");
6880 else
6881 bkpttbl_chain
6882 = make_cleanup_ui_out_table_begin_end (uiout, 5,
6883 nr_printable_breakpoints,
6884 "BreakpointTable");
6885
6886 if (nr_printable_breakpoints > 0)
6887 annotate_breakpoints_headers ();
6888 if (nr_printable_breakpoints > 0)
6889 annotate_field (0);
6890 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
6891 if (nr_printable_breakpoints > 0)
6892 annotate_field (1);
6893 ui_out_table_header (uiout, print_type_col_width, ui_left,
6894 "type", "Type"); /* 2 */
6895 if (nr_printable_breakpoints > 0)
6896 annotate_field (2);
6897 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
6898 if (nr_printable_breakpoints > 0)
6899 annotate_field (3);
6900 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
6901 if (opts.addressprint)
6902 {
6903 if (nr_printable_breakpoints > 0)
6904 annotate_field (4);
6905 if (print_address_bits <= 32)
6906 ui_out_table_header (uiout, 10, ui_left,
6907 "addr", "Address"); /* 5 */
6908 else
6909 ui_out_table_header (uiout, 18, ui_left,
6910 "addr", "Address"); /* 5 */
6911 }
6912 if (nr_printable_breakpoints > 0)
6913 annotate_field (5);
6914 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
6915 ui_out_table_body (uiout);
6916 if (nr_printable_breakpoints > 0)
6917 annotate_breakpoints_table ();
6918
6919 ALL_BREAKPOINTS (b)
6920 {
6921 QUIT;
6922 /* If we have a filter, only list the breakpoints it accepts. */
6923 if (filter && !filter (b))
6924 continue;
6925
6926 /* If we have an "args" string, it is a list of breakpoints to
6927 accept. Skip the others. */
6928
6929 if (args != NULL && *args != '\0')
6930 {
6931 if (allflag) /* maintenance info breakpoint */
6932 {
6933 if (parse_and_eval_long (args) != b->number)
6934 continue;
6935 }
6936 else /* all others */
6937 {
6938 if (!number_is_in_list (args, b->number))
6939 continue;
6940 }
6941 }
6942 /* We only print out user settable breakpoints unless the
6943 allflag is set. */
6944 if (allflag || user_breakpoint_p (b))
6945 print_one_breakpoint (b, &last_loc, allflag);
6946 }
6947
6948 do_cleanups (bkpttbl_chain);
6949
6950 if (nr_printable_breakpoints == 0)
6951 {
6952 /* If there's a filter, let the caller decide how to report
6953 empty list. */
6954 if (!filter)
6955 {
6956 if (args == NULL || *args == '\0')
6957 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
6958 else
6959 ui_out_message (uiout, 0,
6960 "No breakpoint or watchpoint matching '%s'.\n",
6961 args);
6962 }
6963 }
6964 else
6965 {
6966 if (last_loc && !server_command)
6967 set_next_address (last_loc->gdbarch, last_loc->address);
6968 }
6969
6970 /* FIXME? Should this be moved up so that it is only called when
6971 there have been breakpoints? */
6972 annotate_breakpoints_table_end ();
6973
6974 return nr_printable_breakpoints;
6975 }
6976
6977 /* Display the value of default-collect in a way that is generally
6978 compatible with the breakpoint list. */
6979
6980 static void
6981 default_collect_info (void)
6982 {
6983 struct ui_out *uiout = current_uiout;
6984
6985 /* If it has no value (which is frequently the case), say nothing; a
6986 message like "No default-collect." gets in user's face when it's
6987 not wanted. */
6988 if (!*default_collect)
6989 return;
6990
6991 /* The following phrase lines up nicely with per-tracepoint collect
6992 actions. */
6993 ui_out_text (uiout, "default collect ");
6994 ui_out_field_string (uiout, "default-collect", default_collect);
6995 ui_out_text (uiout, " \n");
6996 }
6997
6998 static void
6999 breakpoints_info (char *args, int from_tty)
7000 {
7001 breakpoint_1 (args, 0, NULL);
7002
7003 default_collect_info ();
7004 }
7005
7006 static void
7007 watchpoints_info (char *args, int from_tty)
7008 {
7009 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
7010 struct ui_out *uiout = current_uiout;
7011
7012 if (num_printed == 0)
7013 {
7014 if (args == NULL || *args == '\0')
7015 ui_out_message (uiout, 0, "No watchpoints.\n");
7016 else
7017 ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
7018 }
7019 }
7020
7021 static void
7022 maintenance_info_breakpoints (char *args, int from_tty)
7023 {
7024 breakpoint_1 (args, 1, NULL);
7025
7026 default_collect_info ();
7027 }
7028
7029 static int
7030 breakpoint_has_pc (struct breakpoint *b,
7031 struct program_space *pspace,
7032 CORE_ADDR pc, struct obj_section *section)
7033 {
7034 struct bp_location *bl = b->loc;
7035
7036 for (; bl; bl = bl->next)
7037 {
7038 if (bl->pspace == pspace
7039 && bl->address == pc
7040 && (!overlay_debugging || bl->section == section))
7041 return 1;
7042 }
7043 return 0;
7044 }
7045
7046 /* Print a message describing any user-breakpoints set at PC. This
7047 concerns with logical breakpoints, so we match program spaces, not
7048 address spaces. */
7049
7050 static void
7051 describe_other_breakpoints (struct gdbarch *gdbarch,
7052 struct program_space *pspace, CORE_ADDR pc,
7053 struct obj_section *section, int thread)
7054 {
7055 int others = 0;
7056 struct breakpoint *b;
7057
7058 ALL_BREAKPOINTS (b)
7059 others += (user_breakpoint_p (b)
7060 && breakpoint_has_pc (b, pspace, pc, section));
7061 if (others > 0)
7062 {
7063 if (others == 1)
7064 printf_filtered (_("Note: breakpoint "));
7065 else /* if (others == ???) */
7066 printf_filtered (_("Note: breakpoints "));
7067 ALL_BREAKPOINTS (b)
7068 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
7069 {
7070 others--;
7071 printf_filtered ("%d", b->number);
7072 if (b->thread == -1 && thread != -1)
7073 printf_filtered (" (all threads)");
7074 else if (b->thread != -1)
7075 printf_filtered (" (thread %d)", b->thread);
7076 printf_filtered ("%s%s ",
7077 ((b->enable_state == bp_disabled
7078 || b->enable_state == bp_call_disabled)
7079 ? " (disabled)"
7080 : ""),
7081 (others > 1) ? ","
7082 : ((others == 1) ? " and" : ""));
7083 }
7084 printf_filtered (_("also set at pc "));
7085 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
7086 printf_filtered (".\n");
7087 }
7088 }
7089 \f
7090
7091 /* Return true iff it is meaningful to use the address member of
7092 BPT locations. For some breakpoint types, the locations' address members
7093 are irrelevant and it makes no sense to attempt to compare them to other
7094 addresses (or use them for any other purpose either).
7095
7096 More specifically, each of the following breakpoint types will
7097 always have a zero valued location address and we don't want to mark
7098 breakpoints of any of these types to be a duplicate of an actual
7099 breakpoint location at address zero:
7100
7101 bp_watchpoint
7102 bp_catchpoint
7103
7104 */
7105
7106 static int
7107 breakpoint_address_is_meaningful (struct breakpoint *bpt)
7108 {
7109 enum bptype type = bpt->type;
7110
7111 return (type != bp_watchpoint && type != bp_catchpoint);
7112 }
7113
7114 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
7115 true if LOC1 and LOC2 represent the same watchpoint location. */
7116
7117 static int
7118 watchpoint_locations_match (struct bp_location *loc1,
7119 struct bp_location *loc2)
7120 {
7121 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
7122 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
7123
7124 /* Both of them must exist. */
7125 gdb_assert (w1 != NULL);
7126 gdb_assert (w2 != NULL);
7127
7128 /* If the target can evaluate the condition expression in hardware,
7129 then we we need to insert both watchpoints even if they are at
7130 the same place. Otherwise the watchpoint will only trigger when
7131 the condition of whichever watchpoint was inserted evaluates to
7132 true, not giving a chance for GDB to check the condition of the
7133 other watchpoint. */
7134 if ((w1->cond_exp
7135 && target_can_accel_watchpoint_condition (loc1->address,
7136 loc1->length,
7137 loc1->watchpoint_type,
7138 w1->cond_exp))
7139 || (w2->cond_exp
7140 && target_can_accel_watchpoint_condition (loc2->address,
7141 loc2->length,
7142 loc2->watchpoint_type,
7143 w2->cond_exp)))
7144 return 0;
7145
7146 /* Note that this checks the owner's type, not the location's. In
7147 case the target does not support read watchpoints, but does
7148 support access watchpoints, we'll have bp_read_watchpoint
7149 watchpoints with hw_access locations. Those should be considered
7150 duplicates of hw_read locations. The hw_read locations will
7151 become hw_access locations later. */
7152 return (loc1->owner->type == loc2->owner->type
7153 && loc1->pspace->aspace == loc2->pspace->aspace
7154 && loc1->address == loc2->address
7155 && loc1->length == loc2->length);
7156 }
7157
7158 /* See breakpoint.h. */
7159
7160 int
7161 breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
7162 struct address_space *aspace2, CORE_ADDR addr2)
7163 {
7164 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
7165 || aspace1 == aspace2)
7166 && addr1 == addr2);
7167 }
7168
7169 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
7170 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
7171 matches ASPACE2. On targets that have global breakpoints, the address
7172 space doesn't really matter. */
7173
7174 static int
7175 breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
7176 int len1, struct address_space *aspace2,
7177 CORE_ADDR addr2)
7178 {
7179 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
7180 || aspace1 == aspace2)
7181 && addr2 >= addr1 && addr2 < addr1 + len1);
7182 }
7183
7184 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
7185 a ranged breakpoint. In most targets, a match happens only if ASPACE
7186 matches the breakpoint's address space. On targets that have global
7187 breakpoints, the address space doesn't really matter. */
7188
7189 static int
7190 breakpoint_location_address_match (struct bp_location *bl,
7191 struct address_space *aspace,
7192 CORE_ADDR addr)
7193 {
7194 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
7195 aspace, addr)
7196 || (bl->length
7197 && breakpoint_address_match_range (bl->pspace->aspace,
7198 bl->address, bl->length,
7199 aspace, addr)));
7200 }
7201
7202 /* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
7203 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
7204 match happens only if ASPACE matches the breakpoint's address
7205 space. On targets that have global breakpoints, the address space
7206 doesn't really matter. */
7207
7208 static int
7209 breakpoint_location_address_range_overlap (struct bp_location *bl,
7210 struct address_space *aspace,
7211 CORE_ADDR addr, int len)
7212 {
7213 if (gdbarch_has_global_breakpoints (target_gdbarch ())
7214 || bl->pspace->aspace == aspace)
7215 {
7216 int bl_len = bl->length != 0 ? bl->length : 1;
7217
7218 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
7219 return 1;
7220 }
7221 return 0;
7222 }
7223
7224 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
7225 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
7226 true, otherwise returns false. */
7227
7228 static int
7229 tracepoint_locations_match (struct bp_location *loc1,
7230 struct bp_location *loc2)
7231 {
7232 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
7233 /* Since tracepoint locations are never duplicated with others', tracepoint
7234 locations at the same address of different tracepoints are regarded as
7235 different locations. */
7236 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7237 else
7238 return 0;
7239 }
7240
7241 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
7242 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
7243 represent the same location. */
7244
7245 static int
7246 breakpoint_locations_match (struct bp_location *loc1,
7247 struct bp_location *loc2)
7248 {
7249 int hw_point1, hw_point2;
7250
7251 /* Both of them must not be in moribund_locations. */
7252 gdb_assert (loc1->owner != NULL);
7253 gdb_assert (loc2->owner != NULL);
7254
7255 hw_point1 = is_hardware_watchpoint (loc1->owner);
7256 hw_point2 = is_hardware_watchpoint (loc2->owner);
7257
7258 if (hw_point1 != hw_point2)
7259 return 0;
7260 else if (hw_point1)
7261 return watchpoint_locations_match (loc1, loc2);
7262 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7263 return tracepoint_locations_match (loc1, loc2);
7264 else
7265 /* We compare bp_location.length in order to cover ranged breakpoints. */
7266 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7267 loc2->pspace->aspace, loc2->address)
7268 && loc1->length == loc2->length);
7269 }
7270
7271 static void
7272 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7273 int bnum, int have_bnum)
7274 {
7275 /* The longest string possibly returned by hex_string_custom
7276 is 50 chars. These must be at least that big for safety. */
7277 char astr1[64];
7278 char astr2[64];
7279
7280 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7281 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
7282 if (have_bnum)
7283 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
7284 bnum, astr1, astr2);
7285 else
7286 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
7287 }
7288
7289 /* Adjust a breakpoint's address to account for architectural
7290 constraints on breakpoint placement. Return the adjusted address.
7291 Note: Very few targets require this kind of adjustment. For most
7292 targets, this function is simply the identity function. */
7293
7294 static CORE_ADDR
7295 adjust_breakpoint_address (struct gdbarch *gdbarch,
7296 CORE_ADDR bpaddr, enum bptype bptype)
7297 {
7298 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
7299 {
7300 /* Very few targets need any kind of breakpoint adjustment. */
7301 return bpaddr;
7302 }
7303 else if (bptype == bp_watchpoint
7304 || bptype == bp_hardware_watchpoint
7305 || bptype == bp_read_watchpoint
7306 || bptype == bp_access_watchpoint
7307 || bptype == bp_catchpoint)
7308 {
7309 /* Watchpoints and the various bp_catch_* eventpoints should not
7310 have their addresses modified. */
7311 return bpaddr;
7312 }
7313 else if (bptype == bp_single_step)
7314 {
7315 /* Single-step breakpoints should not have their addresses
7316 modified. If there's any architectural constrain that
7317 applies to this address, then it should have already been
7318 taken into account when the breakpoint was created in the
7319 first place. If we didn't do this, stepping through e.g.,
7320 Thumb-2 IT blocks would break. */
7321 return bpaddr;
7322 }
7323 else
7324 {
7325 CORE_ADDR adjusted_bpaddr;
7326
7327 /* Some targets have architectural constraints on the placement
7328 of breakpoint instructions. Obtain the adjusted address. */
7329 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
7330
7331 /* An adjusted breakpoint address can significantly alter
7332 a user's expectations. Print a warning if an adjustment
7333 is required. */
7334 if (adjusted_bpaddr != bpaddr)
7335 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7336
7337 return adjusted_bpaddr;
7338 }
7339 }
7340
7341 void
7342 init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
7343 struct breakpoint *owner)
7344 {
7345 memset (loc, 0, sizeof (*loc));
7346
7347 gdb_assert (ops != NULL);
7348
7349 loc->ops = ops;
7350 loc->owner = owner;
7351 loc->cond = NULL;
7352 loc->cond_bytecode = NULL;
7353 loc->shlib_disabled = 0;
7354 loc->enabled = 1;
7355
7356 switch (owner->type)
7357 {
7358 case bp_breakpoint:
7359 case bp_single_step:
7360 case bp_until:
7361 case bp_finish:
7362 case bp_longjmp:
7363 case bp_longjmp_resume:
7364 case bp_longjmp_call_dummy:
7365 case bp_exception:
7366 case bp_exception_resume:
7367 case bp_step_resume:
7368 case bp_hp_step_resume:
7369 case bp_watchpoint_scope:
7370 case bp_call_dummy:
7371 case bp_std_terminate:
7372 case bp_shlib_event:
7373 case bp_thread_event:
7374 case bp_overlay_event:
7375 case bp_jit_event:
7376 case bp_longjmp_master:
7377 case bp_std_terminate_master:
7378 case bp_exception_master:
7379 case bp_gnu_ifunc_resolver:
7380 case bp_gnu_ifunc_resolver_return:
7381 case bp_dprintf:
7382 loc->loc_type = bp_loc_software_breakpoint;
7383 mark_breakpoint_location_modified (loc);
7384 break;
7385 case bp_hardware_breakpoint:
7386 loc->loc_type = bp_loc_hardware_breakpoint;
7387 mark_breakpoint_location_modified (loc);
7388 break;
7389 case bp_hardware_watchpoint:
7390 case bp_read_watchpoint:
7391 case bp_access_watchpoint:
7392 loc->loc_type = bp_loc_hardware_watchpoint;
7393 break;
7394 case bp_watchpoint:
7395 case bp_catchpoint:
7396 case bp_tracepoint:
7397 case bp_fast_tracepoint:
7398 case bp_static_tracepoint:
7399 loc->loc_type = bp_loc_other;
7400 break;
7401 default:
7402 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
7403 }
7404
7405 loc->refc = 1;
7406 }
7407
7408 /* Allocate a struct bp_location. */
7409
7410 static struct bp_location *
7411 allocate_bp_location (struct breakpoint *bpt)
7412 {
7413 return bpt->ops->allocate_location (bpt);
7414 }
7415
7416 static void
7417 free_bp_location (struct bp_location *loc)
7418 {
7419 loc->ops->dtor (loc);
7420 xfree (loc);
7421 }
7422
7423 /* Increment reference count. */
7424
7425 static void
7426 incref_bp_location (struct bp_location *bl)
7427 {
7428 ++bl->refc;
7429 }
7430
7431 /* Decrement reference count. If the reference count reaches 0,
7432 destroy the bp_location. Sets *BLP to NULL. */
7433
7434 static void
7435 decref_bp_location (struct bp_location **blp)
7436 {
7437 gdb_assert ((*blp)->refc > 0);
7438
7439 if (--(*blp)->refc == 0)
7440 free_bp_location (*blp);
7441 *blp = NULL;
7442 }
7443
7444 /* Add breakpoint B at the end of the global breakpoint chain. */
7445
7446 static void
7447 add_to_breakpoint_chain (struct breakpoint *b)
7448 {
7449 struct breakpoint *b1;
7450
7451 /* Add this breakpoint to the end of the chain so that a list of
7452 breakpoints will come out in order of increasing numbers. */
7453
7454 b1 = breakpoint_chain;
7455 if (b1 == 0)
7456 breakpoint_chain = b;
7457 else
7458 {
7459 while (b1->next)
7460 b1 = b1->next;
7461 b1->next = b;
7462 }
7463 }
7464
7465 /* Initializes breakpoint B with type BPTYPE and no locations yet. */
7466
7467 static void
7468 init_raw_breakpoint_without_location (struct breakpoint *b,
7469 struct gdbarch *gdbarch,
7470 enum bptype bptype,
7471 const struct breakpoint_ops *ops)
7472 {
7473 memset (b, 0, sizeof (*b));
7474
7475 gdb_assert (ops != NULL);
7476
7477 b->ops = ops;
7478 b->type = bptype;
7479 b->gdbarch = gdbarch;
7480 b->language = current_language->la_language;
7481 b->input_radix = input_radix;
7482 b->thread = -1;
7483 b->enable_state = bp_enabled;
7484 b->next = 0;
7485 b->silent = 0;
7486 b->ignore_count = 0;
7487 b->commands = NULL;
7488 b->frame_id = null_frame_id;
7489 b->condition_not_parsed = 0;
7490 b->py_bp_object = NULL;
7491 b->related_breakpoint = b;
7492 b->location = NULL;
7493 }
7494
7495 /* Helper to set_raw_breakpoint below. Creates a breakpoint
7496 that has type BPTYPE and has no locations as yet. */
7497
7498 static struct breakpoint *
7499 set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
7500 enum bptype bptype,
7501 const struct breakpoint_ops *ops)
7502 {
7503 struct breakpoint *b = XNEW (struct breakpoint);
7504
7505 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7506 add_to_breakpoint_chain (b);
7507 return b;
7508 }
7509
7510 /* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7511 resolutions should be made as the user specified the location explicitly
7512 enough. */
7513
7514 static void
7515 set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
7516 {
7517 gdb_assert (loc->owner != NULL);
7518
7519 if (loc->owner->type == bp_breakpoint
7520 || loc->owner->type == bp_hardware_breakpoint
7521 || is_tracepoint (loc->owner))
7522 {
7523 int is_gnu_ifunc;
7524 const char *function_name;
7525 CORE_ADDR func_addr;
7526
7527 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
7528 &func_addr, NULL, &is_gnu_ifunc);
7529
7530 if (is_gnu_ifunc && !explicit_loc)
7531 {
7532 struct breakpoint *b = loc->owner;
7533
7534 gdb_assert (loc->pspace == current_program_space);
7535 if (gnu_ifunc_resolve_name (function_name,
7536 &loc->requested_address))
7537 {
7538 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7539 loc->address = adjust_breakpoint_address (loc->gdbarch,
7540 loc->requested_address,
7541 b->type);
7542 }
7543 else if (b->type == bp_breakpoint && b->loc == loc
7544 && loc->next == NULL && b->related_breakpoint == b)
7545 {
7546 /* Create only the whole new breakpoint of this type but do not
7547 mess more complicated breakpoints with multiple locations. */
7548 b->type = bp_gnu_ifunc_resolver;
7549 /* Remember the resolver's address for use by the return
7550 breakpoint. */
7551 loc->related_address = func_addr;
7552 }
7553 }
7554
7555 if (function_name)
7556 loc->function_name = xstrdup (function_name);
7557 }
7558 }
7559
7560 /* Attempt to determine architecture of location identified by SAL. */
7561 struct gdbarch *
7562 get_sal_arch (struct symtab_and_line sal)
7563 {
7564 if (sal.section)
7565 return get_objfile_arch (sal.section->objfile);
7566 if (sal.symtab)
7567 return get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
7568
7569 return NULL;
7570 }
7571
7572 /* Low level routine for partially initializing a breakpoint of type
7573 BPTYPE. The newly created breakpoint's address, section, source
7574 file name, and line number are provided by SAL.
7575
7576 It is expected that the caller will complete the initialization of
7577 the newly created breakpoint struct as well as output any status
7578 information regarding the creation of a new breakpoint. */
7579
7580 static void
7581 init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
7582 struct symtab_and_line sal, enum bptype bptype,
7583 const struct breakpoint_ops *ops)
7584 {
7585 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7586
7587 add_location_to_breakpoint (b, &sal);
7588
7589 if (bptype != bp_catchpoint)
7590 gdb_assert (sal.pspace != NULL);
7591
7592 /* Store the program space that was used to set the breakpoint,
7593 except for ordinary breakpoints, which are independent of the
7594 program space. */
7595 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7596 b->pspace = sal.pspace;
7597 }
7598
7599 /* set_raw_breakpoint is a low level routine for allocating and
7600 partially initializing a breakpoint of type BPTYPE. The newly
7601 created breakpoint's address, section, source file name, and line
7602 number are provided by SAL. The newly created and partially
7603 initialized breakpoint is added to the breakpoint chain and
7604 is also returned as the value of this function.
7605
7606 It is expected that the caller will complete the initialization of
7607 the newly created breakpoint struct as well as output any status
7608 information regarding the creation of a new breakpoint. In
7609 particular, set_raw_breakpoint does NOT set the breakpoint
7610 number! Care should be taken to not allow an error to occur
7611 prior to completing the initialization of the breakpoint. If this
7612 should happen, a bogus breakpoint will be left on the chain. */
7613
7614 struct breakpoint *
7615 set_raw_breakpoint (struct gdbarch *gdbarch,
7616 struct symtab_and_line sal, enum bptype bptype,
7617 const struct breakpoint_ops *ops)
7618 {
7619 struct breakpoint *b = XNEW (struct breakpoint);
7620
7621 init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
7622 add_to_breakpoint_chain (b);
7623 return b;
7624 }
7625
7626 /* Call this routine when stepping and nexting to enable a breakpoint
7627 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7628 initiated the operation. */
7629
7630 void
7631 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
7632 {
7633 struct breakpoint *b, *b_tmp;
7634 int thread = tp->global_num;
7635
7636 /* To avoid having to rescan all objfile symbols at every step,
7637 we maintain a list of continually-inserted but always disabled
7638 longjmp "master" breakpoints. Here, we simply create momentary
7639 clones of those and enable them for the requested thread. */
7640 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7641 if (b->pspace == current_program_space
7642 && (b->type == bp_longjmp_master
7643 || b->type == bp_exception_master))
7644 {
7645 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7646 struct breakpoint *clone;
7647
7648 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7649 after their removal. */
7650 clone = momentary_breakpoint_from_master (b, type,
7651 &longjmp_breakpoint_ops, 1);
7652 clone->thread = thread;
7653 }
7654
7655 tp->initiating_frame = frame;
7656 }
7657
7658 /* Delete all longjmp breakpoints from THREAD. */
7659 void
7660 delete_longjmp_breakpoint (int thread)
7661 {
7662 struct breakpoint *b, *b_tmp;
7663
7664 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7665 if (b->type == bp_longjmp || b->type == bp_exception)
7666 {
7667 if (b->thread == thread)
7668 delete_breakpoint (b);
7669 }
7670 }
7671
7672 void
7673 delete_longjmp_breakpoint_at_next_stop (int thread)
7674 {
7675 struct breakpoint *b, *b_tmp;
7676
7677 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7678 if (b->type == bp_longjmp || b->type == bp_exception)
7679 {
7680 if (b->thread == thread)
7681 b->disposition = disp_del_at_next_stop;
7682 }
7683 }
7684
7685 /* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7686 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7687 pointer to any of them. Return NULL if this system cannot place longjmp
7688 breakpoints. */
7689
7690 struct breakpoint *
7691 set_longjmp_breakpoint_for_call_dummy (void)
7692 {
7693 struct breakpoint *b, *retval = NULL;
7694
7695 ALL_BREAKPOINTS (b)
7696 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7697 {
7698 struct breakpoint *new_b;
7699
7700 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7701 &momentary_breakpoint_ops,
7702 1);
7703 new_b->thread = ptid_to_global_thread_id (inferior_ptid);
7704
7705 /* Link NEW_B into the chain of RETVAL breakpoints. */
7706
7707 gdb_assert (new_b->related_breakpoint == new_b);
7708 if (retval == NULL)
7709 retval = new_b;
7710 new_b->related_breakpoint = retval;
7711 while (retval->related_breakpoint != new_b->related_breakpoint)
7712 retval = retval->related_breakpoint;
7713 retval->related_breakpoint = new_b;
7714 }
7715
7716 return retval;
7717 }
7718
7719 /* Verify all existing dummy frames and their associated breakpoints for
7720 TP. Remove those which can no longer be found in the current frame
7721 stack.
7722
7723 You should call this function only at places where it is safe to currently
7724 unwind the whole stack. Failed stack unwind would discard live dummy
7725 frames. */
7726
7727 void
7728 check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
7729 {
7730 struct breakpoint *b, *b_tmp;
7731
7732 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7733 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
7734 {
7735 struct breakpoint *dummy_b = b->related_breakpoint;
7736
7737 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7738 dummy_b = dummy_b->related_breakpoint;
7739 if (dummy_b->type != bp_call_dummy
7740 || frame_find_by_id (dummy_b->frame_id) != NULL)
7741 continue;
7742
7743 dummy_frame_discard (dummy_b->frame_id, tp->ptid);
7744
7745 while (b->related_breakpoint != b)
7746 {
7747 if (b_tmp == b->related_breakpoint)
7748 b_tmp = b->related_breakpoint->next;
7749 delete_breakpoint (b->related_breakpoint);
7750 }
7751 delete_breakpoint (b);
7752 }
7753 }
7754
7755 void
7756 enable_overlay_breakpoints (void)
7757 {
7758 struct breakpoint *b;
7759
7760 ALL_BREAKPOINTS (b)
7761 if (b->type == bp_overlay_event)
7762 {
7763 b->enable_state = bp_enabled;
7764 update_global_location_list (UGLL_MAY_INSERT);
7765 overlay_events_enabled = 1;
7766 }
7767 }
7768
7769 void
7770 disable_overlay_breakpoints (void)
7771 {
7772 struct breakpoint *b;
7773
7774 ALL_BREAKPOINTS (b)
7775 if (b->type == bp_overlay_event)
7776 {
7777 b->enable_state = bp_disabled;
7778 update_global_location_list (UGLL_DONT_INSERT);
7779 overlay_events_enabled = 0;
7780 }
7781 }
7782
7783 /* Set an active std::terminate breakpoint for each std::terminate
7784 master breakpoint. */
7785 void
7786 set_std_terminate_breakpoint (void)
7787 {
7788 struct breakpoint *b, *b_tmp;
7789
7790 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7791 if (b->pspace == current_program_space
7792 && b->type == bp_std_terminate_master)
7793 {
7794 momentary_breakpoint_from_master (b, bp_std_terminate,
7795 &momentary_breakpoint_ops, 1);
7796 }
7797 }
7798
7799 /* Delete all the std::terminate breakpoints. */
7800 void
7801 delete_std_terminate_breakpoint (void)
7802 {
7803 struct breakpoint *b, *b_tmp;
7804
7805 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7806 if (b->type == bp_std_terminate)
7807 delete_breakpoint (b);
7808 }
7809
7810 struct breakpoint *
7811 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7812 {
7813 struct breakpoint *b;
7814
7815 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7816 &internal_breakpoint_ops);
7817
7818 b->enable_state = bp_enabled;
7819 /* location has to be used or breakpoint_re_set will delete me. */
7820 b->location = new_address_location (b->loc->address, NULL, 0);
7821
7822 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7823
7824 return b;
7825 }
7826
7827 struct lang_and_radix
7828 {
7829 enum language lang;
7830 int radix;
7831 };
7832
7833 /* Create a breakpoint for JIT code registration and unregistration. */
7834
7835 struct breakpoint *
7836 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7837 {
7838 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7839 &internal_breakpoint_ops);
7840 }
7841
7842 /* Remove JIT code registration and unregistration breakpoint(s). */
7843
7844 void
7845 remove_jit_event_breakpoints (void)
7846 {
7847 struct breakpoint *b, *b_tmp;
7848
7849 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7850 if (b->type == bp_jit_event
7851 && b->loc->pspace == current_program_space)
7852 delete_breakpoint (b);
7853 }
7854
7855 void
7856 remove_solib_event_breakpoints (void)
7857 {
7858 struct breakpoint *b, *b_tmp;
7859
7860 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7861 if (b->type == bp_shlib_event
7862 && b->loc->pspace == current_program_space)
7863 delete_breakpoint (b);
7864 }
7865
7866 /* See breakpoint.h. */
7867
7868 void
7869 remove_solib_event_breakpoints_at_next_stop (void)
7870 {
7871 struct breakpoint *b, *b_tmp;
7872
7873 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7874 if (b->type == bp_shlib_event
7875 && b->loc->pspace == current_program_space)
7876 b->disposition = disp_del_at_next_stop;
7877 }
7878
7879 /* Helper for create_solib_event_breakpoint /
7880 create_and_insert_solib_event_breakpoint. Allows specifying which
7881 INSERT_MODE to pass through to update_global_location_list. */
7882
7883 static struct breakpoint *
7884 create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7885 enum ugll_insert_mode insert_mode)
7886 {
7887 struct breakpoint *b;
7888
7889 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7890 &internal_breakpoint_ops);
7891 update_global_location_list_nothrow (insert_mode);
7892 return b;
7893 }
7894
7895 struct breakpoint *
7896 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7897 {
7898 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7899 }
7900
7901 /* See breakpoint.h. */
7902
7903 struct breakpoint *
7904 create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7905 {
7906 struct breakpoint *b;
7907
7908 /* Explicitly tell update_global_location_list to insert
7909 locations. */
7910 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
7911 if (!b->loc->inserted)
7912 {
7913 delete_breakpoint (b);
7914 return NULL;
7915 }
7916 return b;
7917 }
7918
7919 /* Disable any breakpoints that are on code in shared libraries. Only
7920 apply to enabled breakpoints, disabled ones can just stay disabled. */
7921
7922 void
7923 disable_breakpoints_in_shlibs (void)
7924 {
7925 struct bp_location *loc, **locp_tmp;
7926
7927 ALL_BP_LOCATIONS (loc, locp_tmp)
7928 {
7929 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7930 struct breakpoint *b = loc->owner;
7931
7932 /* We apply the check to all breakpoints, including disabled for
7933 those with loc->duplicate set. This is so that when breakpoint
7934 becomes enabled, or the duplicate is removed, gdb will try to
7935 insert all breakpoints. If we don't set shlib_disabled here,
7936 we'll try to insert those breakpoints and fail. */
7937 if (((b->type == bp_breakpoint)
7938 || (b->type == bp_jit_event)
7939 || (b->type == bp_hardware_breakpoint)
7940 || (is_tracepoint (b)))
7941 && loc->pspace == current_program_space
7942 && !loc->shlib_disabled
7943 && solib_name_from_address (loc->pspace, loc->address)
7944 )
7945 {
7946 loc->shlib_disabled = 1;
7947 }
7948 }
7949 }
7950
7951 /* Disable any breakpoints and tracepoints that are in SOLIB upon
7952 notification of unloaded_shlib. Only apply to enabled breakpoints,
7953 disabled ones can just stay disabled. */
7954
7955 static void
7956 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7957 {
7958 struct bp_location *loc, **locp_tmp;
7959 int disabled_shlib_breaks = 0;
7960
7961 /* SunOS a.out shared libraries are always mapped, so do not
7962 disable breakpoints; they will only be reported as unloaded
7963 through clear_solib when GDB discards its shared library
7964 list. See clear_solib for more information. */
7965 if (exec_bfd != NULL
7966 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
7967 return;
7968
7969 ALL_BP_LOCATIONS (loc, locp_tmp)
7970 {
7971 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7972 struct breakpoint *b = loc->owner;
7973
7974 if (solib->pspace == loc->pspace
7975 && !loc->shlib_disabled
7976 && (((b->type == bp_breakpoint
7977 || b->type == bp_jit_event
7978 || b->type == bp_hardware_breakpoint)
7979 && (loc->loc_type == bp_loc_hardware_breakpoint
7980 || loc->loc_type == bp_loc_software_breakpoint))
7981 || is_tracepoint (b))
7982 && solib_contains_address_p (solib, loc->address))
7983 {
7984 loc->shlib_disabled = 1;
7985 /* At this point, we cannot rely on remove_breakpoint
7986 succeeding so we must mark the breakpoint as not inserted
7987 to prevent future errors occurring in remove_breakpoints. */
7988 loc->inserted = 0;
7989
7990 /* This may cause duplicate notifications for the same breakpoint. */
7991 observer_notify_breakpoint_modified (b);
7992
7993 if (!disabled_shlib_breaks)
7994 {
7995 target_terminal_ours_for_output ();
7996 warning (_("Temporarily disabling breakpoints "
7997 "for unloaded shared library \"%s\""),
7998 solib->so_name);
7999 }
8000 disabled_shlib_breaks = 1;
8001 }
8002 }
8003 }
8004
8005 /* Disable any breakpoints and tracepoints in OBJFILE upon
8006 notification of free_objfile. Only apply to enabled breakpoints,
8007 disabled ones can just stay disabled. */
8008
8009 static void
8010 disable_breakpoints_in_freed_objfile (struct objfile *objfile)
8011 {
8012 struct breakpoint *b;
8013
8014 if (objfile == NULL)
8015 return;
8016
8017 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
8018 managed by the user with add-symbol-file/remove-symbol-file.
8019 Similarly to how breakpoints in shared libraries are handled in
8020 response to "nosharedlibrary", mark breakpoints in such modules
8021 shlib_disabled so they end up uninserted on the next global
8022 location list update. Shared libraries not loaded by the user
8023 aren't handled here -- they're already handled in
8024 disable_breakpoints_in_unloaded_shlib, called by solib.c's
8025 solib_unloaded observer. We skip objfiles that are not
8026 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
8027 main objfile). */
8028 if ((objfile->flags & OBJF_SHARED) == 0
8029 || (objfile->flags & OBJF_USERLOADED) == 0)
8030 return;
8031
8032 ALL_BREAKPOINTS (b)
8033 {
8034 struct bp_location *loc;
8035 int bp_modified = 0;
8036
8037 if (!is_breakpoint (b) && !is_tracepoint (b))
8038 continue;
8039
8040 for (loc = b->loc; loc != NULL; loc = loc->next)
8041 {
8042 CORE_ADDR loc_addr = loc->address;
8043
8044 if (loc->loc_type != bp_loc_hardware_breakpoint
8045 && loc->loc_type != bp_loc_software_breakpoint)
8046 continue;
8047
8048 if (loc->shlib_disabled != 0)
8049 continue;
8050
8051 if (objfile->pspace != loc->pspace)
8052 continue;
8053
8054 if (loc->loc_type != bp_loc_hardware_breakpoint
8055 && loc->loc_type != bp_loc_software_breakpoint)
8056 continue;
8057
8058 if (is_addr_in_objfile (loc_addr, objfile))
8059 {
8060 loc->shlib_disabled = 1;
8061 /* At this point, we don't know whether the object was
8062 unmapped from the inferior or not, so leave the
8063 inserted flag alone. We'll handle failure to
8064 uninsert quietly, in case the object was indeed
8065 unmapped. */
8066
8067 mark_breakpoint_location_modified (loc);
8068
8069 bp_modified = 1;
8070 }
8071 }
8072
8073 if (bp_modified)
8074 observer_notify_breakpoint_modified (b);
8075 }
8076 }
8077
8078 /* FORK & VFORK catchpoints. */
8079
8080 /* An instance of this type is used to represent a fork or vfork
8081 catchpoint. It includes a "struct breakpoint" as a kind of base
8082 class; users downcast to "struct breakpoint *" when needed. A
8083 breakpoint is really of this type iff its ops pointer points to
8084 CATCH_FORK_BREAKPOINT_OPS. */
8085
8086 struct fork_catchpoint
8087 {
8088 /* The base class. */
8089 struct breakpoint base;
8090
8091 /* Process id of a child process whose forking triggered this
8092 catchpoint. This field is only valid immediately after this
8093 catchpoint has triggered. */
8094 ptid_t forked_inferior_pid;
8095 };
8096
8097 /* Implement the "insert" breakpoint_ops method for fork
8098 catchpoints. */
8099
8100 static int
8101 insert_catch_fork (struct bp_location *bl)
8102 {
8103 return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
8104 }
8105
8106 /* Implement the "remove" breakpoint_ops method for fork
8107 catchpoints. */
8108
8109 static int
8110 remove_catch_fork (struct bp_location *bl)
8111 {
8112 return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
8113 }
8114
8115 /* Implement the "breakpoint_hit" breakpoint_ops method for fork
8116 catchpoints. */
8117
8118 static int
8119 breakpoint_hit_catch_fork (const struct bp_location *bl,
8120 struct address_space *aspace, CORE_ADDR bp_addr,
8121 const struct target_waitstatus *ws)
8122 {
8123 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8124
8125 if (ws->kind != TARGET_WAITKIND_FORKED)
8126 return 0;
8127
8128 c->forked_inferior_pid = ws->value.related_pid;
8129 return 1;
8130 }
8131
8132 /* Implement the "print_it" breakpoint_ops method for fork
8133 catchpoints. */
8134
8135 static enum print_stop_action
8136 print_it_catch_fork (bpstat bs)
8137 {
8138 struct ui_out *uiout = current_uiout;
8139 struct breakpoint *b = bs->breakpoint_at;
8140 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
8141
8142 annotate_catchpoint (b->number);
8143 maybe_print_thread_hit_breakpoint (uiout);
8144 if (b->disposition == disp_del)
8145 ui_out_text (uiout, "Temporary catchpoint ");
8146 else
8147 ui_out_text (uiout, "Catchpoint ");
8148 if (ui_out_is_mi_like_p (uiout))
8149 {
8150 ui_out_field_string (uiout, "reason",
8151 async_reason_lookup (EXEC_ASYNC_FORK));
8152 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8153 }
8154 ui_out_field_int (uiout, "bkptno", b->number);
8155 ui_out_text (uiout, " (forked process ");
8156 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
8157 ui_out_text (uiout, "), ");
8158 return PRINT_SRC_AND_LOC;
8159 }
8160
8161 /* Implement the "print_one" breakpoint_ops method for fork
8162 catchpoints. */
8163
8164 static void
8165 print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
8166 {
8167 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8168 struct value_print_options opts;
8169 struct ui_out *uiout = current_uiout;
8170
8171 get_user_print_options (&opts);
8172
8173 /* Field 4, the address, is omitted (which makes the columns not
8174 line up too nicely with the headers, but the effect is relatively
8175 readable). */
8176 if (opts.addressprint)
8177 ui_out_field_skip (uiout, "addr");
8178 annotate_field (5);
8179 ui_out_text (uiout, "fork");
8180 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
8181 {
8182 ui_out_text (uiout, ", process ");
8183 ui_out_field_int (uiout, "what",
8184 ptid_get_pid (c->forked_inferior_pid));
8185 ui_out_spaces (uiout, 1);
8186 }
8187
8188 if (ui_out_is_mi_like_p (uiout))
8189 ui_out_field_string (uiout, "catch-type", "fork");
8190 }
8191
8192 /* Implement the "print_mention" breakpoint_ops method for fork
8193 catchpoints. */
8194
8195 static void
8196 print_mention_catch_fork (struct breakpoint *b)
8197 {
8198 printf_filtered (_("Catchpoint %d (fork)"), b->number);
8199 }
8200
8201 /* Implement the "print_recreate" breakpoint_ops method for fork
8202 catchpoints. */
8203
8204 static void
8205 print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
8206 {
8207 fprintf_unfiltered (fp, "catch fork");
8208 print_recreate_thread (b, fp);
8209 }
8210
8211 /* The breakpoint_ops structure to be used in fork catchpoints. */
8212
8213 static struct breakpoint_ops catch_fork_breakpoint_ops;
8214
8215 /* Implement the "insert" breakpoint_ops method for vfork
8216 catchpoints. */
8217
8218 static int
8219 insert_catch_vfork (struct bp_location *bl)
8220 {
8221 return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
8222 }
8223
8224 /* Implement the "remove" breakpoint_ops method for vfork
8225 catchpoints. */
8226
8227 static int
8228 remove_catch_vfork (struct bp_location *bl)
8229 {
8230 return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
8231 }
8232
8233 /* Implement the "breakpoint_hit" breakpoint_ops method for vfork
8234 catchpoints. */
8235
8236 static int
8237 breakpoint_hit_catch_vfork (const struct bp_location *bl,
8238 struct address_space *aspace, CORE_ADDR bp_addr,
8239 const struct target_waitstatus *ws)
8240 {
8241 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8242
8243 if (ws->kind != TARGET_WAITKIND_VFORKED)
8244 return 0;
8245
8246 c->forked_inferior_pid = ws->value.related_pid;
8247 return 1;
8248 }
8249
8250 /* Implement the "print_it" breakpoint_ops method for vfork
8251 catchpoints. */
8252
8253 static enum print_stop_action
8254 print_it_catch_vfork (bpstat bs)
8255 {
8256 struct ui_out *uiout = current_uiout;
8257 struct breakpoint *b = bs->breakpoint_at;
8258 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8259
8260 annotate_catchpoint (b->number);
8261 maybe_print_thread_hit_breakpoint (uiout);
8262 if (b->disposition == disp_del)
8263 ui_out_text (uiout, "Temporary catchpoint ");
8264 else
8265 ui_out_text (uiout, "Catchpoint ");
8266 if (ui_out_is_mi_like_p (uiout))
8267 {
8268 ui_out_field_string (uiout, "reason",
8269 async_reason_lookup (EXEC_ASYNC_VFORK));
8270 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8271 }
8272 ui_out_field_int (uiout, "bkptno", b->number);
8273 ui_out_text (uiout, " (vforked process ");
8274 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
8275 ui_out_text (uiout, "), ");
8276 return PRINT_SRC_AND_LOC;
8277 }
8278
8279 /* Implement the "print_one" breakpoint_ops method for vfork
8280 catchpoints. */
8281
8282 static void
8283 print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
8284 {
8285 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8286 struct value_print_options opts;
8287 struct ui_out *uiout = current_uiout;
8288
8289 get_user_print_options (&opts);
8290 /* Field 4, the address, is omitted (which makes the columns not
8291 line up too nicely with the headers, but the effect is relatively
8292 readable). */
8293 if (opts.addressprint)
8294 ui_out_field_skip (uiout, "addr");
8295 annotate_field (5);
8296 ui_out_text (uiout, "vfork");
8297 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
8298 {
8299 ui_out_text (uiout, ", process ");
8300 ui_out_field_int (uiout, "what",
8301 ptid_get_pid (c->forked_inferior_pid));
8302 ui_out_spaces (uiout, 1);
8303 }
8304
8305 if (ui_out_is_mi_like_p (uiout))
8306 ui_out_field_string (uiout, "catch-type", "vfork");
8307 }
8308
8309 /* Implement the "print_mention" breakpoint_ops method for vfork
8310 catchpoints. */
8311
8312 static void
8313 print_mention_catch_vfork (struct breakpoint *b)
8314 {
8315 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
8316 }
8317
8318 /* Implement the "print_recreate" breakpoint_ops method for vfork
8319 catchpoints. */
8320
8321 static void
8322 print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8323 {
8324 fprintf_unfiltered (fp, "catch vfork");
8325 print_recreate_thread (b, fp);
8326 }
8327
8328 /* The breakpoint_ops structure to be used in vfork catchpoints. */
8329
8330 static struct breakpoint_ops catch_vfork_breakpoint_ops;
8331
8332 /* An instance of this type is used to represent an solib catchpoint.
8333 It includes a "struct breakpoint" as a kind of base class; users
8334 downcast to "struct breakpoint *" when needed. A breakpoint is
8335 really of this type iff its ops pointer points to
8336 CATCH_SOLIB_BREAKPOINT_OPS. */
8337
8338 struct solib_catchpoint
8339 {
8340 /* The base class. */
8341 struct breakpoint base;
8342
8343 /* True for "catch load", false for "catch unload". */
8344 unsigned char is_load;
8345
8346 /* Regular expression to match, if any. COMPILED is only valid when
8347 REGEX is non-NULL. */
8348 char *regex;
8349 regex_t compiled;
8350 };
8351
8352 static void
8353 dtor_catch_solib (struct breakpoint *b)
8354 {
8355 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8356
8357 if (self->regex)
8358 regfree (&self->compiled);
8359 xfree (self->regex);
8360
8361 base_breakpoint_ops.dtor (b);
8362 }
8363
8364 static int
8365 insert_catch_solib (struct bp_location *ignore)
8366 {
8367 return 0;
8368 }
8369
8370 static int
8371 remove_catch_solib (struct bp_location *ignore)
8372 {
8373 return 0;
8374 }
8375
8376 static int
8377 breakpoint_hit_catch_solib (const struct bp_location *bl,
8378 struct address_space *aspace,
8379 CORE_ADDR bp_addr,
8380 const struct target_waitstatus *ws)
8381 {
8382 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8383 struct breakpoint *other;
8384
8385 if (ws->kind == TARGET_WAITKIND_LOADED)
8386 return 1;
8387
8388 ALL_BREAKPOINTS (other)
8389 {
8390 struct bp_location *other_bl;
8391
8392 if (other == bl->owner)
8393 continue;
8394
8395 if (other->type != bp_shlib_event)
8396 continue;
8397
8398 if (self->base.pspace != NULL && other->pspace != self->base.pspace)
8399 continue;
8400
8401 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8402 {
8403 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8404 return 1;
8405 }
8406 }
8407
8408 return 0;
8409 }
8410
8411 static void
8412 check_status_catch_solib (struct bpstats *bs)
8413 {
8414 struct solib_catchpoint *self
8415 = (struct solib_catchpoint *) bs->breakpoint_at;
8416 int ix;
8417
8418 if (self->is_load)
8419 {
8420 struct so_list *iter;
8421
8422 for (ix = 0;
8423 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
8424 ix, iter);
8425 ++ix)
8426 {
8427 if (!self->regex
8428 || regexec (&self->compiled, iter->so_name, 0, NULL, 0) == 0)
8429 return;
8430 }
8431 }
8432 else
8433 {
8434 char *iter;
8435
8436 for (ix = 0;
8437 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
8438 ix, iter);
8439 ++ix)
8440 {
8441 if (!self->regex
8442 || regexec (&self->compiled, iter, 0, NULL, 0) == 0)
8443 return;
8444 }
8445 }
8446
8447 bs->stop = 0;
8448 bs->print_it = print_it_noop;
8449 }
8450
8451 static enum print_stop_action
8452 print_it_catch_solib (bpstat bs)
8453 {
8454 struct breakpoint *b = bs->breakpoint_at;
8455 struct ui_out *uiout = current_uiout;
8456
8457 annotate_catchpoint (b->number);
8458 maybe_print_thread_hit_breakpoint (uiout);
8459 if (b->disposition == disp_del)
8460 ui_out_text (uiout, "Temporary catchpoint ");
8461 else
8462 ui_out_text (uiout, "Catchpoint ");
8463 ui_out_field_int (uiout, "bkptno", b->number);
8464 ui_out_text (uiout, "\n");
8465 if (ui_out_is_mi_like_p (uiout))
8466 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8467 print_solib_event (1);
8468 return PRINT_SRC_AND_LOC;
8469 }
8470
8471 static void
8472 print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8473 {
8474 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8475 struct value_print_options opts;
8476 struct ui_out *uiout = current_uiout;
8477 char *msg;
8478
8479 get_user_print_options (&opts);
8480 /* Field 4, the address, is omitted (which makes the columns not
8481 line up too nicely with the headers, but the effect is relatively
8482 readable). */
8483 if (opts.addressprint)
8484 {
8485 annotate_field (4);
8486 ui_out_field_skip (uiout, "addr");
8487 }
8488
8489 annotate_field (5);
8490 if (self->is_load)
8491 {
8492 if (self->regex)
8493 msg = xstrprintf (_("load of library matching %s"), self->regex);
8494 else
8495 msg = xstrdup (_("load of library"));
8496 }
8497 else
8498 {
8499 if (self->regex)
8500 msg = xstrprintf (_("unload of library matching %s"), self->regex);
8501 else
8502 msg = xstrdup (_("unload of library"));
8503 }
8504 ui_out_field_string (uiout, "what", msg);
8505 xfree (msg);
8506
8507 if (ui_out_is_mi_like_p (uiout))
8508 ui_out_field_string (uiout, "catch-type",
8509 self->is_load ? "load" : "unload");
8510 }
8511
8512 static void
8513 print_mention_catch_solib (struct breakpoint *b)
8514 {
8515 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8516
8517 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8518 self->is_load ? "load" : "unload");
8519 }
8520
8521 static void
8522 print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8523 {
8524 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8525
8526 fprintf_unfiltered (fp, "%s %s",
8527 b->disposition == disp_del ? "tcatch" : "catch",
8528 self->is_load ? "load" : "unload");
8529 if (self->regex)
8530 fprintf_unfiltered (fp, " %s", self->regex);
8531 fprintf_unfiltered (fp, "\n");
8532 }
8533
8534 static struct breakpoint_ops catch_solib_breakpoint_ops;
8535
8536 /* Shared helper function (MI and CLI) for creating and installing
8537 a shared object event catchpoint. If IS_LOAD is non-zero then
8538 the events to be caught are load events, otherwise they are
8539 unload events. If IS_TEMP is non-zero the catchpoint is a
8540 temporary one. If ENABLED is non-zero the catchpoint is
8541 created in an enabled state. */
8542
8543 void
8544 add_solib_catchpoint (char *arg, int is_load, int is_temp, int enabled)
8545 {
8546 struct solib_catchpoint *c;
8547 struct gdbarch *gdbarch = get_current_arch ();
8548 struct cleanup *cleanup;
8549
8550 if (!arg)
8551 arg = "";
8552 arg = skip_spaces (arg);
8553
8554 c = XCNEW (struct solib_catchpoint);
8555 cleanup = make_cleanup (xfree, c);
8556
8557 if (*arg != '\0')
8558 {
8559 int errcode;
8560
8561 errcode = regcomp (&c->compiled, arg, REG_NOSUB);
8562 if (errcode != 0)
8563 {
8564 char *err = get_regcomp_error (errcode, &c->compiled);
8565
8566 make_cleanup (xfree, err);
8567 error (_("Invalid regexp (%s): %s"), err, arg);
8568 }
8569 c->regex = xstrdup (arg);
8570 }
8571
8572 c->is_load = is_load;
8573 init_catchpoint (&c->base, gdbarch, is_temp, NULL,
8574 &catch_solib_breakpoint_ops);
8575
8576 c->base.enable_state = enabled ? bp_enabled : bp_disabled;
8577
8578 discard_cleanups (cleanup);
8579 install_breakpoint (0, &c->base, 1);
8580 }
8581
8582 /* A helper function that does all the work for "catch load" and
8583 "catch unload". */
8584
8585 static void
8586 catch_load_or_unload (char *arg, int from_tty, int is_load,
8587 struct cmd_list_element *command)
8588 {
8589 int tempflag;
8590 const int enabled = 1;
8591
8592 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8593
8594 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8595 }
8596
8597 static void
8598 catch_load_command_1 (char *arg, int from_tty,
8599 struct cmd_list_element *command)
8600 {
8601 catch_load_or_unload (arg, from_tty, 1, command);
8602 }
8603
8604 static void
8605 catch_unload_command_1 (char *arg, int from_tty,
8606 struct cmd_list_element *command)
8607 {
8608 catch_load_or_unload (arg, from_tty, 0, command);
8609 }
8610
8611 /* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8612 is non-zero, then make the breakpoint temporary. If COND_STRING is
8613 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8614 the breakpoint_ops structure associated to the catchpoint. */
8615
8616 void
8617 init_catchpoint (struct breakpoint *b,
8618 struct gdbarch *gdbarch, int tempflag,
8619 char *cond_string,
8620 const struct breakpoint_ops *ops)
8621 {
8622 struct symtab_and_line sal;
8623
8624 init_sal (&sal);
8625 sal.pspace = current_program_space;
8626
8627 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
8628
8629 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
8630 b->disposition = tempflag ? disp_del : disp_donttouch;
8631 }
8632
8633 void
8634 install_breakpoint (int internal, struct breakpoint *b, int update_gll)
8635 {
8636 add_to_breakpoint_chain (b);
8637 set_breakpoint_number (internal, b);
8638 if (is_tracepoint (b))
8639 set_tracepoint_count (breakpoint_count);
8640 if (!internal)
8641 mention (b);
8642 observer_notify_breakpoint_created (b);
8643
8644 if (update_gll)
8645 update_global_location_list (UGLL_MAY_INSERT);
8646 }
8647
8648 static void
8649 create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8650 int tempflag, char *cond_string,
8651 const struct breakpoint_ops *ops)
8652 {
8653 struct fork_catchpoint *c = XNEW (struct fork_catchpoint);
8654
8655 init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
8656
8657 c->forked_inferior_pid = null_ptid;
8658
8659 install_breakpoint (0, &c->base, 1);
8660 }
8661
8662 /* Exec catchpoints. */
8663
8664 /* An instance of this type is used to represent an exec catchpoint.
8665 It includes a "struct breakpoint" as a kind of base class; users
8666 downcast to "struct breakpoint *" when needed. A breakpoint is
8667 really of this type iff its ops pointer points to
8668 CATCH_EXEC_BREAKPOINT_OPS. */
8669
8670 struct exec_catchpoint
8671 {
8672 /* The base class. */
8673 struct breakpoint base;
8674
8675 /* Filename of a program whose exec triggered this catchpoint.
8676 This field is only valid immediately after this catchpoint has
8677 triggered. */
8678 char *exec_pathname;
8679 };
8680
8681 /* Implement the "dtor" breakpoint_ops method for exec
8682 catchpoints. */
8683
8684 static void
8685 dtor_catch_exec (struct breakpoint *b)
8686 {
8687 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8688
8689 xfree (c->exec_pathname);
8690
8691 base_breakpoint_ops.dtor (b);
8692 }
8693
8694 static int
8695 insert_catch_exec (struct bp_location *bl)
8696 {
8697 return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
8698 }
8699
8700 static int
8701 remove_catch_exec (struct bp_location *bl)
8702 {
8703 return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
8704 }
8705
8706 static int
8707 breakpoint_hit_catch_exec (const struct bp_location *bl,
8708 struct address_space *aspace, CORE_ADDR bp_addr,
8709 const struct target_waitstatus *ws)
8710 {
8711 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8712
8713 if (ws->kind != TARGET_WAITKIND_EXECD)
8714 return 0;
8715
8716 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8717 return 1;
8718 }
8719
8720 static enum print_stop_action
8721 print_it_catch_exec (bpstat bs)
8722 {
8723 struct ui_out *uiout = current_uiout;
8724 struct breakpoint *b = bs->breakpoint_at;
8725 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8726
8727 annotate_catchpoint (b->number);
8728 maybe_print_thread_hit_breakpoint (uiout);
8729 if (b->disposition == disp_del)
8730 ui_out_text (uiout, "Temporary catchpoint ");
8731 else
8732 ui_out_text (uiout, "Catchpoint ");
8733 if (ui_out_is_mi_like_p (uiout))
8734 {
8735 ui_out_field_string (uiout, "reason",
8736 async_reason_lookup (EXEC_ASYNC_EXEC));
8737 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8738 }
8739 ui_out_field_int (uiout, "bkptno", b->number);
8740 ui_out_text (uiout, " (exec'd ");
8741 ui_out_field_string (uiout, "new-exec", c->exec_pathname);
8742 ui_out_text (uiout, "), ");
8743
8744 return PRINT_SRC_AND_LOC;
8745 }
8746
8747 static void
8748 print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
8749 {
8750 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8751 struct value_print_options opts;
8752 struct ui_out *uiout = current_uiout;
8753
8754 get_user_print_options (&opts);
8755
8756 /* Field 4, the address, is omitted (which makes the columns
8757 not line up too nicely with the headers, but the effect
8758 is relatively readable). */
8759 if (opts.addressprint)
8760 ui_out_field_skip (uiout, "addr");
8761 annotate_field (5);
8762 ui_out_text (uiout, "exec");
8763 if (c->exec_pathname != NULL)
8764 {
8765 ui_out_text (uiout, ", program \"");
8766 ui_out_field_string (uiout, "what", c->exec_pathname);
8767 ui_out_text (uiout, "\" ");
8768 }
8769
8770 if (ui_out_is_mi_like_p (uiout))
8771 ui_out_field_string (uiout, "catch-type", "exec");
8772 }
8773
8774 static void
8775 print_mention_catch_exec (struct breakpoint *b)
8776 {
8777 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8778 }
8779
8780 /* Implement the "print_recreate" breakpoint_ops method for exec
8781 catchpoints. */
8782
8783 static void
8784 print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8785 {
8786 fprintf_unfiltered (fp, "catch exec");
8787 print_recreate_thread (b, fp);
8788 }
8789
8790 static struct breakpoint_ops catch_exec_breakpoint_ops;
8791
8792 static int
8793 hw_breakpoint_used_count (void)
8794 {
8795 int i = 0;
8796 struct breakpoint *b;
8797 struct bp_location *bl;
8798
8799 ALL_BREAKPOINTS (b)
8800 {
8801 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
8802 for (bl = b->loc; bl; bl = bl->next)
8803 {
8804 /* Special types of hardware breakpoints may use more than
8805 one register. */
8806 i += b->ops->resources_needed (bl);
8807 }
8808 }
8809
8810 return i;
8811 }
8812
8813 /* Returns the resources B would use if it were a hardware
8814 watchpoint. */
8815
8816 static int
8817 hw_watchpoint_use_count (struct breakpoint *b)
8818 {
8819 int i = 0;
8820 struct bp_location *bl;
8821
8822 if (!breakpoint_enabled (b))
8823 return 0;
8824
8825 for (bl = b->loc; bl; bl = bl->next)
8826 {
8827 /* Special types of hardware watchpoints may use more than
8828 one register. */
8829 i += b->ops->resources_needed (bl);
8830 }
8831
8832 return i;
8833 }
8834
8835 /* Returns the sum the used resources of all hardware watchpoints of
8836 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8837 the sum of the used resources of all hardware watchpoints of other
8838 types _not_ TYPE. */
8839
8840 static int
8841 hw_watchpoint_used_count_others (struct breakpoint *except,
8842 enum bptype type, int *other_type_used)
8843 {
8844 int i = 0;
8845 struct breakpoint *b;
8846
8847 *other_type_used = 0;
8848 ALL_BREAKPOINTS (b)
8849 {
8850 if (b == except)
8851 continue;
8852 if (!breakpoint_enabled (b))
8853 continue;
8854
8855 if (b->type == type)
8856 i += hw_watchpoint_use_count (b);
8857 else if (is_hardware_watchpoint (b))
8858 *other_type_used = 1;
8859 }
8860
8861 return i;
8862 }
8863
8864 void
8865 disable_watchpoints_before_interactive_call_start (void)
8866 {
8867 struct breakpoint *b;
8868
8869 ALL_BREAKPOINTS (b)
8870 {
8871 if (is_watchpoint (b) && breakpoint_enabled (b))
8872 {
8873 b->enable_state = bp_call_disabled;
8874 update_global_location_list (UGLL_DONT_INSERT);
8875 }
8876 }
8877 }
8878
8879 void
8880 enable_watchpoints_after_interactive_call_stop (void)
8881 {
8882 struct breakpoint *b;
8883
8884 ALL_BREAKPOINTS (b)
8885 {
8886 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
8887 {
8888 b->enable_state = bp_enabled;
8889 update_global_location_list (UGLL_MAY_INSERT);
8890 }
8891 }
8892 }
8893
8894 void
8895 disable_breakpoints_before_startup (void)
8896 {
8897 current_program_space->executing_startup = 1;
8898 update_global_location_list (UGLL_DONT_INSERT);
8899 }
8900
8901 void
8902 enable_breakpoints_after_startup (void)
8903 {
8904 current_program_space->executing_startup = 0;
8905 breakpoint_re_set ();
8906 }
8907
8908 /* Create a new single-step breakpoint for thread THREAD, with no
8909 locations. */
8910
8911 static struct breakpoint *
8912 new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8913 {
8914 struct breakpoint *b = XNEW (struct breakpoint);
8915
8916 init_raw_breakpoint_without_location (b, gdbarch, bp_single_step,
8917 &momentary_breakpoint_ops);
8918
8919 b->disposition = disp_donttouch;
8920 b->frame_id = null_frame_id;
8921
8922 b->thread = thread;
8923 gdb_assert (b->thread != 0);
8924
8925 add_to_breakpoint_chain (b);
8926
8927 return b;
8928 }
8929
8930 /* Set a momentary breakpoint of type TYPE at address specified by
8931 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8932 frame. */
8933
8934 struct breakpoint *
8935 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8936 struct frame_id frame_id, enum bptype type)
8937 {
8938 struct breakpoint *b;
8939
8940 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8941 tail-called one. */
8942 gdb_assert (!frame_id_artificial_p (frame_id));
8943
8944 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
8945 b->enable_state = bp_enabled;
8946 b->disposition = disp_donttouch;
8947 b->frame_id = frame_id;
8948
8949 /* If we're debugging a multi-threaded program, then we want
8950 momentary breakpoints to be active in only a single thread of
8951 control. */
8952 if (in_thread_list (inferior_ptid))
8953 b->thread = ptid_to_global_thread_id (inferior_ptid);
8954
8955 update_global_location_list_nothrow (UGLL_MAY_INSERT);
8956
8957 return b;
8958 }
8959
8960 /* Make a momentary breakpoint based on the master breakpoint ORIG.
8961 The new breakpoint will have type TYPE, use OPS as its
8962 breakpoint_ops, and will set enabled to LOC_ENABLED. */
8963
8964 static struct breakpoint *
8965 momentary_breakpoint_from_master (struct breakpoint *orig,
8966 enum bptype type,
8967 const struct breakpoint_ops *ops,
8968 int loc_enabled)
8969 {
8970 struct breakpoint *copy;
8971
8972 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
8973 copy->loc = allocate_bp_location (copy);
8974 set_breakpoint_location_function (copy->loc, 1);
8975
8976 copy->loc->gdbarch = orig->loc->gdbarch;
8977 copy->loc->requested_address = orig->loc->requested_address;
8978 copy->loc->address = orig->loc->address;
8979 copy->loc->section = orig->loc->section;
8980 copy->loc->pspace = orig->loc->pspace;
8981 copy->loc->probe = orig->loc->probe;
8982 copy->loc->line_number = orig->loc->line_number;
8983 copy->loc->symtab = orig->loc->symtab;
8984 copy->loc->enabled = loc_enabled;
8985 copy->frame_id = orig->frame_id;
8986 copy->thread = orig->thread;
8987 copy->pspace = orig->pspace;
8988
8989 copy->enable_state = bp_enabled;
8990 copy->disposition = disp_donttouch;
8991 copy->number = internal_breakpoint_number--;
8992
8993 update_global_location_list_nothrow (UGLL_DONT_INSERT);
8994 return copy;
8995 }
8996
8997 /* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8998 ORIG is NULL. */
8999
9000 struct breakpoint *
9001 clone_momentary_breakpoint (struct breakpoint *orig)
9002 {
9003 /* If there's nothing to clone, then return nothing. */
9004 if (orig == NULL)
9005 return NULL;
9006
9007 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
9008 }
9009
9010 struct breakpoint *
9011 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
9012 enum bptype type)
9013 {
9014 struct symtab_and_line sal;
9015
9016 sal = find_pc_line (pc, 0);
9017 sal.pc = pc;
9018 sal.section = find_pc_overlay (pc);
9019 sal.explicit_pc = 1;
9020
9021 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
9022 }
9023 \f
9024
9025 /* Tell the user we have just set a breakpoint B. */
9026
9027 static void
9028 mention (struct breakpoint *b)
9029 {
9030 b->ops->print_mention (b);
9031 if (ui_out_is_mi_like_p (current_uiout))
9032 return;
9033 printf_filtered ("\n");
9034 }
9035 \f
9036
9037 static int bp_loc_is_permanent (struct bp_location *loc);
9038
9039 static struct bp_location *
9040 add_location_to_breakpoint (struct breakpoint *b,
9041 const struct symtab_and_line *sal)
9042 {
9043 struct bp_location *loc, **tmp;
9044 CORE_ADDR adjusted_address;
9045 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
9046
9047 if (loc_gdbarch == NULL)
9048 loc_gdbarch = b->gdbarch;
9049
9050 /* Adjust the breakpoint's address prior to allocating a location.
9051 Once we call allocate_bp_location(), that mostly uninitialized
9052 location will be placed on the location chain. Adjustment of the
9053 breakpoint may cause target_read_memory() to be called and we do
9054 not want its scan of the location chain to find a breakpoint and
9055 location that's only been partially initialized. */
9056 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
9057 sal->pc, b->type);
9058
9059 /* Sort the locations by their ADDRESS. */
9060 loc = allocate_bp_location (b);
9061 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
9062 tmp = &((*tmp)->next))
9063 ;
9064 loc->next = *tmp;
9065 *tmp = loc;
9066
9067 loc->requested_address = sal->pc;
9068 loc->address = adjusted_address;
9069 loc->pspace = sal->pspace;
9070 loc->probe.probe = sal->probe;
9071 loc->probe.objfile = sal->objfile;
9072 gdb_assert (loc->pspace != NULL);
9073 loc->section = sal->section;
9074 loc->gdbarch = loc_gdbarch;
9075 loc->line_number = sal->line;
9076 loc->symtab = sal->symtab;
9077
9078 set_breakpoint_location_function (loc,
9079 sal->explicit_pc || sal->explicit_line);
9080
9081 /* While by definition, permanent breakpoints are already present in the
9082 code, we don't mark the location as inserted. Normally one would expect
9083 that GDB could rely on that breakpoint instruction to stop the program,
9084 thus removing the need to insert its own breakpoint, except that executing
9085 the breakpoint instruction can kill the target instead of reporting a
9086 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
9087 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
9088 with "Trap 0x02 while interrupts disabled, Error state". Letting the
9089 breakpoint be inserted normally results in QEMU knowing about the GDB
9090 breakpoint, and thus trap before the breakpoint instruction is executed.
9091 (If GDB later needs to continue execution past the permanent breakpoint,
9092 it manually increments the PC, thus avoiding executing the breakpoint
9093 instruction.) */
9094 if (bp_loc_is_permanent (loc))
9095 loc->permanent = 1;
9096
9097 return loc;
9098 }
9099 \f
9100
9101 /* See breakpoint.h. */
9102
9103 int
9104 program_breakpoint_here_p (struct gdbarch *gdbarch, CORE_ADDR address)
9105 {
9106 int len;
9107 CORE_ADDR addr;
9108 const gdb_byte *bpoint;
9109 gdb_byte *target_mem;
9110 struct cleanup *cleanup;
9111 int retval = 0;
9112
9113 addr = address;
9114 bpoint = gdbarch_breakpoint_from_pc (gdbarch, &addr, &len);
9115
9116 /* Software breakpoints unsupported? */
9117 if (bpoint == NULL)
9118 return 0;
9119
9120 target_mem = (gdb_byte *) alloca (len);
9121
9122 /* Enable the automatic memory restoration from breakpoints while
9123 we read the memory. Otherwise we could say about our temporary
9124 breakpoints they are permanent. */
9125 cleanup = make_show_memory_breakpoints_cleanup (0);
9126
9127 if (target_read_memory (address, target_mem, len) == 0
9128 && memcmp (target_mem, bpoint, len) == 0)
9129 retval = 1;
9130
9131 do_cleanups (cleanup);
9132
9133 return retval;
9134 }
9135
9136 /* Return 1 if LOC is pointing to a permanent breakpoint,
9137 return 0 otherwise. */
9138
9139 static int
9140 bp_loc_is_permanent (struct bp_location *loc)
9141 {
9142 struct cleanup *cleanup;
9143 int retval;
9144
9145 gdb_assert (loc != NULL);
9146
9147 /* If we have a catchpoint or a watchpoint, just return 0. We should not
9148 attempt to read from the addresses the locations of these breakpoint types
9149 point to. program_breakpoint_here_p, below, will attempt to read
9150 memory. */
9151 if (!breakpoint_address_is_meaningful (loc->owner))
9152 return 0;
9153
9154 cleanup = save_current_space_and_thread ();
9155 switch_to_program_space_and_thread (loc->pspace);
9156
9157 retval = program_breakpoint_here_p (loc->gdbarch, loc->address);
9158
9159 do_cleanups (cleanup);
9160
9161 return retval;
9162 }
9163
9164 /* Build a command list for the dprintf corresponding to the current
9165 settings of the dprintf style options. */
9166
9167 static void
9168 update_dprintf_command_list (struct breakpoint *b)
9169 {
9170 char *dprintf_args = b->extra_string;
9171 char *printf_line = NULL;
9172
9173 if (!dprintf_args)
9174 return;
9175
9176 dprintf_args = skip_spaces (dprintf_args);
9177
9178 /* Allow a comma, as it may have terminated a location, but don't
9179 insist on it. */
9180 if (*dprintf_args == ',')
9181 ++dprintf_args;
9182 dprintf_args = skip_spaces (dprintf_args);
9183
9184 if (*dprintf_args != '"')
9185 error (_("Bad format string, missing '\"'."));
9186
9187 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
9188 printf_line = xstrprintf ("printf %s", dprintf_args);
9189 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
9190 {
9191 if (!dprintf_function)
9192 error (_("No function supplied for dprintf call"));
9193
9194 if (dprintf_channel && strlen (dprintf_channel) > 0)
9195 printf_line = xstrprintf ("call (void) %s (%s,%s)",
9196 dprintf_function,
9197 dprintf_channel,
9198 dprintf_args);
9199 else
9200 printf_line = xstrprintf ("call (void) %s (%s)",
9201 dprintf_function,
9202 dprintf_args);
9203 }
9204 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
9205 {
9206 if (target_can_run_breakpoint_commands ())
9207 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
9208 else
9209 {
9210 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
9211 printf_line = xstrprintf ("printf %s", dprintf_args);
9212 }
9213 }
9214 else
9215 internal_error (__FILE__, __LINE__,
9216 _("Invalid dprintf style."));
9217
9218 gdb_assert (printf_line != NULL);
9219 /* Manufacture a printf sequence. */
9220 {
9221 struct command_line *printf_cmd_line = XNEW (struct command_line);
9222
9223 printf_cmd_line->control_type = simple_control;
9224 printf_cmd_line->body_count = 0;
9225 printf_cmd_line->body_list = NULL;
9226 printf_cmd_line->next = NULL;
9227 printf_cmd_line->line = printf_line;
9228
9229 breakpoint_set_commands (b, printf_cmd_line);
9230 }
9231 }
9232
9233 /* Update all dprintf commands, making their command lists reflect
9234 current style settings. */
9235
9236 static void
9237 update_dprintf_commands (char *args, int from_tty,
9238 struct cmd_list_element *c)
9239 {
9240 struct breakpoint *b;
9241
9242 ALL_BREAKPOINTS (b)
9243 {
9244 if (b->type == bp_dprintf)
9245 update_dprintf_command_list (b);
9246 }
9247 }
9248
9249 /* Create a breakpoint with SAL as location. Use LOCATION
9250 as a description of the location, and COND_STRING
9251 as condition expression. If LOCATION is NULL then create an
9252 "address location" from the address in the SAL. */
9253
9254 static void
9255 init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
9256 struct symtabs_and_lines sals,
9257 struct event_location *location,
9258 char *filter, char *cond_string,
9259 char *extra_string,
9260 enum bptype type, enum bpdisp disposition,
9261 int thread, int task, int ignore_count,
9262 const struct breakpoint_ops *ops, int from_tty,
9263 int enabled, int internal, unsigned flags,
9264 int display_canonical)
9265 {
9266 int i;
9267
9268 if (type == bp_hardware_breakpoint)
9269 {
9270 int target_resources_ok;
9271
9272 i = hw_breakpoint_used_count ();
9273 target_resources_ok =
9274 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9275 i + 1, 0);
9276 if (target_resources_ok == 0)
9277 error (_("No hardware breakpoint support in the target."));
9278 else if (target_resources_ok < 0)
9279 error (_("Hardware breakpoints used exceeds limit."));
9280 }
9281
9282 gdb_assert (sals.nelts > 0);
9283
9284 for (i = 0; i < sals.nelts; ++i)
9285 {
9286 struct symtab_and_line sal = sals.sals[i];
9287 struct bp_location *loc;
9288
9289 if (from_tty)
9290 {
9291 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9292 if (!loc_gdbarch)
9293 loc_gdbarch = gdbarch;
9294
9295 describe_other_breakpoints (loc_gdbarch,
9296 sal.pspace, sal.pc, sal.section, thread);
9297 }
9298
9299 if (i == 0)
9300 {
9301 init_raw_breakpoint (b, gdbarch, sal, type, ops);
9302 b->thread = thread;
9303 b->task = task;
9304
9305 b->cond_string = cond_string;
9306 b->extra_string = extra_string;
9307 b->ignore_count = ignore_count;
9308 b->enable_state = enabled ? bp_enabled : bp_disabled;
9309 b->disposition = disposition;
9310
9311 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9312 b->loc->inserted = 1;
9313
9314 if (type == bp_static_tracepoint)
9315 {
9316 struct tracepoint *t = (struct tracepoint *) b;
9317 struct static_tracepoint_marker marker;
9318
9319 if (strace_marker_p (b))
9320 {
9321 /* We already know the marker exists, otherwise, we
9322 wouldn't see a sal for it. */
9323 const char *p = &event_location_to_string (b->location)[3];
9324 const char *endp;
9325 char *marker_str;
9326
9327 p = skip_spaces_const (p);
9328
9329 endp = skip_to_space_const (p);
9330
9331 marker_str = savestring (p, endp - p);
9332 t->static_trace_marker_id = marker_str;
9333
9334 printf_filtered (_("Probed static tracepoint "
9335 "marker \"%s\"\n"),
9336 t->static_trace_marker_id);
9337 }
9338 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
9339 {
9340 t->static_trace_marker_id = xstrdup (marker.str_id);
9341 release_static_tracepoint_marker (&marker);
9342
9343 printf_filtered (_("Probed static tracepoint "
9344 "marker \"%s\"\n"),
9345 t->static_trace_marker_id);
9346 }
9347 else
9348 warning (_("Couldn't determine the static "
9349 "tracepoint marker to probe"));
9350 }
9351
9352 loc = b->loc;
9353 }
9354 else
9355 {
9356 loc = add_location_to_breakpoint (b, &sal);
9357 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9358 loc->inserted = 1;
9359 }
9360
9361 if (b->cond_string)
9362 {
9363 const char *arg = b->cond_string;
9364
9365 loc->cond = parse_exp_1 (&arg, loc->address,
9366 block_for_pc (loc->address), 0);
9367 if (*arg)
9368 error (_("Garbage '%s' follows condition"), arg);
9369 }
9370
9371 /* Dynamic printf requires and uses additional arguments on the
9372 command line, otherwise it's an error. */
9373 if (type == bp_dprintf)
9374 {
9375 if (b->extra_string)
9376 update_dprintf_command_list (b);
9377 else
9378 error (_("Format string required"));
9379 }
9380 else if (b->extra_string)
9381 error (_("Garbage '%s' at end of command"), b->extra_string);
9382 }
9383
9384 b->display_canonical = display_canonical;
9385 if (location != NULL)
9386 b->location = location;
9387 else
9388 {
9389 const char *addr_string = NULL;
9390 int addr_string_len = 0;
9391
9392 if (location != NULL)
9393 addr_string = event_location_to_string (location);
9394 if (addr_string != NULL)
9395 addr_string_len = strlen (addr_string);
9396
9397 b->location = new_address_location (b->loc->address,
9398 addr_string, addr_string_len);
9399 }
9400 b->filter = filter;
9401 }
9402
9403 static void
9404 create_breakpoint_sal (struct gdbarch *gdbarch,
9405 struct symtabs_and_lines sals,
9406 struct event_location *location,
9407 char *filter, char *cond_string,
9408 char *extra_string,
9409 enum bptype type, enum bpdisp disposition,
9410 int thread, int task, int ignore_count,
9411 const struct breakpoint_ops *ops, int from_tty,
9412 int enabled, int internal, unsigned flags,
9413 int display_canonical)
9414 {
9415 struct breakpoint *b;
9416 struct cleanup *old_chain;
9417
9418 if (is_tracepoint_type (type))
9419 {
9420 struct tracepoint *t;
9421
9422 t = XCNEW (struct tracepoint);
9423 b = &t->base;
9424 }
9425 else
9426 b = XNEW (struct breakpoint);
9427
9428 old_chain = make_cleanup (xfree, b);
9429
9430 init_breakpoint_sal (b, gdbarch,
9431 sals, location,
9432 filter, cond_string, extra_string,
9433 type, disposition,
9434 thread, task, ignore_count,
9435 ops, from_tty,
9436 enabled, internal, flags,
9437 display_canonical);
9438 discard_cleanups (old_chain);
9439
9440 install_breakpoint (internal, b, 0);
9441 }
9442
9443 /* Add SALS.nelts breakpoints to the breakpoint table. For each
9444 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9445 value. COND_STRING, if not NULL, specified the condition to be
9446 used for all breakpoints. Essentially the only case where
9447 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9448 function. In that case, it's still not possible to specify
9449 separate conditions for different overloaded functions, so
9450 we take just a single condition string.
9451
9452 NOTE: If the function succeeds, the caller is expected to cleanup
9453 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
9454 array contents). If the function fails (error() is called), the
9455 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
9456 COND and SALS arrays and each of those arrays contents. */
9457
9458 static void
9459 create_breakpoints_sal (struct gdbarch *gdbarch,
9460 struct linespec_result *canonical,
9461 char *cond_string, char *extra_string,
9462 enum bptype type, enum bpdisp disposition,
9463 int thread, int task, int ignore_count,
9464 const struct breakpoint_ops *ops, int from_tty,
9465 int enabled, int internal, unsigned flags)
9466 {
9467 int i;
9468 struct linespec_sals *lsal;
9469
9470 if (canonical->pre_expanded)
9471 gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
9472
9473 for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
9474 {
9475 /* Note that 'location' can be NULL in the case of a plain
9476 'break', without arguments. */
9477 struct event_location *location
9478 = (canonical->location != NULL
9479 ? copy_event_location (canonical->location) : NULL);
9480 char *filter_string = lsal->canonical ? xstrdup (lsal->canonical) : NULL;
9481 struct cleanup *inner = make_cleanup_delete_event_location (location);
9482
9483 make_cleanup (xfree, filter_string);
9484 create_breakpoint_sal (gdbarch, lsal->sals,
9485 location,
9486 filter_string,
9487 cond_string, extra_string,
9488 type, disposition,
9489 thread, task, ignore_count, ops,
9490 from_tty, enabled, internal, flags,
9491 canonical->special_display);
9492 discard_cleanups (inner);
9493 }
9494 }
9495
9496 /* Parse LOCATION which is assumed to be a SAL specification possibly
9497 followed by conditionals. On return, SALS contains an array of SAL
9498 addresses found. LOCATION points to the end of the SAL (for
9499 linespec locations).
9500
9501 The array and the line spec strings are allocated on the heap, it is
9502 the caller's responsibility to free them. */
9503
9504 static void
9505 parse_breakpoint_sals (const struct event_location *location,
9506 struct linespec_result *canonical)
9507 {
9508 struct symtab_and_line cursal;
9509
9510 if (event_location_type (location) == LINESPEC_LOCATION)
9511 {
9512 const char *address = get_linespec_location (location);
9513
9514 if (address == NULL)
9515 {
9516 /* The last displayed codepoint, if it's valid, is our default
9517 breakpoint address. */
9518 if (last_displayed_sal_is_valid ())
9519 {
9520 struct linespec_sals lsal;
9521 struct symtab_and_line sal;
9522 CORE_ADDR pc;
9523
9524 init_sal (&sal); /* Initialize to zeroes. */
9525 lsal.sals.sals = XNEW (struct symtab_and_line);
9526
9527 /* Set sal's pspace, pc, symtab, and line to the values
9528 corresponding to the last call to print_frame_info.
9529 Be sure to reinitialize LINE with NOTCURRENT == 0
9530 as the breakpoint line number is inappropriate otherwise.
9531 find_pc_line would adjust PC, re-set it back. */
9532 get_last_displayed_sal (&sal);
9533 pc = sal.pc;
9534 sal = find_pc_line (pc, 0);
9535
9536 /* "break" without arguments is equivalent to "break *PC"
9537 where PC is the last displayed codepoint's address. So
9538 make sure to set sal.explicit_pc to prevent GDB from
9539 trying to expand the list of sals to include all other
9540 instances with the same symtab and line. */
9541 sal.pc = pc;
9542 sal.explicit_pc = 1;
9543
9544 lsal.sals.sals[0] = sal;
9545 lsal.sals.nelts = 1;
9546 lsal.canonical = NULL;
9547
9548 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
9549 return;
9550 }
9551 else
9552 error (_("No default breakpoint address now."));
9553 }
9554 }
9555
9556 /* Force almost all breakpoints to be in terms of the
9557 current_source_symtab (which is decode_line_1's default).
9558 This should produce the results we want almost all of the
9559 time while leaving default_breakpoint_* alone.
9560
9561 ObjC: However, don't match an Objective-C method name which
9562 may have a '+' or '-' succeeded by a '['. */
9563 cursal = get_current_source_symtab_and_line ();
9564 if (last_displayed_sal_is_valid ())
9565 {
9566 const char *address = NULL;
9567
9568 if (event_location_type (location) == LINESPEC_LOCATION)
9569 address = get_linespec_location (location);
9570
9571 if (!cursal.symtab
9572 || (address != NULL
9573 && strchr ("+-", address[0]) != NULL
9574 && address[1] != '['))
9575 {
9576 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9577 get_last_displayed_symtab (),
9578 get_last_displayed_line (),
9579 canonical, NULL, NULL);
9580 return;
9581 }
9582 }
9583
9584 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9585 cursal.symtab, cursal.line, canonical, NULL, NULL);
9586 }
9587
9588
9589 /* Convert each SAL into a real PC. Verify that the PC can be
9590 inserted as a breakpoint. If it can't throw an error. */
9591
9592 static void
9593 breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
9594 {
9595 int i;
9596
9597 for (i = 0; i < sals->nelts; i++)
9598 resolve_sal_pc (&sals->sals[i]);
9599 }
9600
9601 /* Fast tracepoints may have restrictions on valid locations. For
9602 instance, a fast tracepoint using a jump instead of a trap will
9603 likely have to overwrite more bytes than a trap would, and so can
9604 only be placed where the instruction is longer than the jump, or a
9605 multi-instruction sequence does not have a jump into the middle of
9606 it, etc. */
9607
9608 static void
9609 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9610 struct symtabs_and_lines *sals)
9611 {
9612 int i, rslt;
9613 struct symtab_and_line *sal;
9614 char *msg;
9615 struct cleanup *old_chain;
9616
9617 for (i = 0; i < sals->nelts; i++)
9618 {
9619 struct gdbarch *sarch;
9620
9621 sal = &sals->sals[i];
9622
9623 sarch = get_sal_arch (*sal);
9624 /* We fall back to GDBARCH if there is no architecture
9625 associated with SAL. */
9626 if (sarch == NULL)
9627 sarch = gdbarch;
9628 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc, &msg);
9629 old_chain = make_cleanup (xfree, msg);
9630
9631 if (!rslt)
9632 error (_("May not have a fast tracepoint at 0x%s%s"),
9633 paddress (sarch, sal->pc), (msg ? msg : ""));
9634
9635 do_cleanups (old_chain);
9636 }
9637 }
9638
9639 /* Given TOK, a string specification of condition and thread, as
9640 accepted by the 'break' command, extract the condition
9641 string and thread number and set *COND_STRING and *THREAD.
9642 PC identifies the context at which the condition should be parsed.
9643 If no condition is found, *COND_STRING is set to NULL.
9644 If no thread is found, *THREAD is set to -1. */
9645
9646 static void
9647 find_condition_and_thread (const char *tok, CORE_ADDR pc,
9648 char **cond_string, int *thread, int *task,
9649 char **rest)
9650 {
9651 *cond_string = NULL;
9652 *thread = -1;
9653 *task = 0;
9654 *rest = NULL;
9655
9656 while (tok && *tok)
9657 {
9658 const char *end_tok;
9659 int toklen;
9660 const char *cond_start = NULL;
9661 const char *cond_end = NULL;
9662
9663 tok = skip_spaces_const (tok);
9664
9665 if ((*tok == '"' || *tok == ',') && rest)
9666 {
9667 *rest = savestring (tok, strlen (tok));
9668 return;
9669 }
9670
9671 end_tok = skip_to_space_const (tok);
9672
9673 toklen = end_tok - tok;
9674
9675 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9676 {
9677 struct expression *expr;
9678
9679 tok = cond_start = end_tok + 1;
9680 expr = parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
9681 xfree (expr);
9682 cond_end = tok;
9683 *cond_string = savestring (cond_start, cond_end - cond_start);
9684 }
9685 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9686 {
9687 const char *tmptok;
9688 struct thread_info *thr;
9689
9690 tok = end_tok + 1;
9691 thr = parse_thread_id (tok, &tmptok);
9692 if (tok == tmptok)
9693 error (_("Junk after thread keyword."));
9694 *thread = thr->global_num;
9695 tok = tmptok;
9696 }
9697 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9698 {
9699 char *tmptok;
9700
9701 tok = end_tok + 1;
9702 *task = strtol (tok, &tmptok, 0);
9703 if (tok == tmptok)
9704 error (_("Junk after task keyword."));
9705 if (!valid_task_id (*task))
9706 error (_("Unknown task %d."), *task);
9707 tok = tmptok;
9708 }
9709 else if (rest)
9710 {
9711 *rest = savestring (tok, strlen (tok));
9712 return;
9713 }
9714 else
9715 error (_("Junk at end of arguments."));
9716 }
9717 }
9718
9719 /* Decode a static tracepoint marker spec. */
9720
9721 static struct symtabs_and_lines
9722 decode_static_tracepoint_spec (const char **arg_p)
9723 {
9724 VEC(static_tracepoint_marker_p) *markers = NULL;
9725 struct symtabs_and_lines sals;
9726 struct cleanup *old_chain;
9727 const char *p = &(*arg_p)[3];
9728 const char *endp;
9729 char *marker_str;
9730 int i;
9731
9732 p = skip_spaces_const (p);
9733
9734 endp = skip_to_space_const (p);
9735
9736 marker_str = savestring (p, endp - p);
9737 old_chain = make_cleanup (xfree, marker_str);
9738
9739 markers = target_static_tracepoint_markers_by_strid (marker_str);
9740 if (VEC_empty(static_tracepoint_marker_p, markers))
9741 error (_("No known static tracepoint marker named %s"), marker_str);
9742
9743 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
9744 sals.sals = XNEWVEC (struct symtab_and_line, sals.nelts);
9745
9746 for (i = 0; i < sals.nelts; i++)
9747 {
9748 struct static_tracepoint_marker *marker;
9749
9750 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9751
9752 init_sal (&sals.sals[i]);
9753
9754 sals.sals[i] = find_pc_line (marker->address, 0);
9755 sals.sals[i].pc = marker->address;
9756
9757 release_static_tracepoint_marker (marker);
9758 }
9759
9760 do_cleanups (old_chain);
9761
9762 *arg_p = endp;
9763 return sals;
9764 }
9765
9766 /* See breakpoint.h. */
9767
9768 int
9769 create_breakpoint (struct gdbarch *gdbarch,
9770 const struct event_location *location, char *cond_string,
9771 int thread, char *extra_string,
9772 int parse_extra,
9773 int tempflag, enum bptype type_wanted,
9774 int ignore_count,
9775 enum auto_boolean pending_break_support,
9776 const struct breakpoint_ops *ops,
9777 int from_tty, int enabled, int internal,
9778 unsigned flags)
9779 {
9780 struct linespec_result canonical;
9781 struct cleanup *old_chain;
9782 struct cleanup *bkpt_chain = NULL;
9783 int pending = 0;
9784 int task = 0;
9785 int prev_bkpt_count = breakpoint_count;
9786
9787 gdb_assert (ops != NULL);
9788
9789 /* If extra_string isn't useful, set it to NULL. */
9790 if (extra_string != NULL && *extra_string == '\0')
9791 extra_string = NULL;
9792
9793 init_linespec_result (&canonical);
9794
9795 TRY
9796 {
9797 ops->create_sals_from_location (location, &canonical, type_wanted);
9798 }
9799 CATCH (e, RETURN_MASK_ERROR)
9800 {
9801 /* If caller is interested in rc value from parse, set
9802 value. */
9803 if (e.error == NOT_FOUND_ERROR)
9804 {
9805 /* If pending breakpoint support is turned off, throw
9806 error. */
9807
9808 if (pending_break_support == AUTO_BOOLEAN_FALSE)
9809 throw_exception (e);
9810
9811 exception_print (gdb_stderr, e);
9812
9813 /* If pending breakpoint support is auto query and the user
9814 selects no, then simply return the error code. */
9815 if (pending_break_support == AUTO_BOOLEAN_AUTO
9816 && !nquery (_("Make %s pending on future shared library load? "),
9817 bptype_string (type_wanted)))
9818 return 0;
9819
9820 /* At this point, either the user was queried about setting
9821 a pending breakpoint and selected yes, or pending
9822 breakpoint behavior is on and thus a pending breakpoint
9823 is defaulted on behalf of the user. */
9824 pending = 1;
9825 }
9826 else
9827 throw_exception (e);
9828 }
9829 END_CATCH
9830
9831 if (!pending && VEC_empty (linespec_sals, canonical.sals))
9832 return 0;
9833
9834 /* Create a chain of things that always need to be cleaned up. */
9835 old_chain = make_cleanup_destroy_linespec_result (&canonical);
9836
9837 /* ----------------------------- SNIP -----------------------------
9838 Anything added to the cleanup chain beyond this point is assumed
9839 to be part of a breakpoint. If the breakpoint create succeeds
9840 then the memory is not reclaimed. */
9841 bkpt_chain = make_cleanup (null_cleanup, 0);
9842
9843 /* Resolve all line numbers to PC's and verify that the addresses
9844 are ok for the target. */
9845 if (!pending)
9846 {
9847 int ix;
9848 struct linespec_sals *iter;
9849
9850 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9851 breakpoint_sals_to_pc (&iter->sals);
9852 }
9853
9854 /* Fast tracepoints may have additional restrictions on location. */
9855 if (!pending && type_wanted == bp_fast_tracepoint)
9856 {
9857 int ix;
9858 struct linespec_sals *iter;
9859
9860 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9861 check_fast_tracepoint_sals (gdbarch, &iter->sals);
9862 }
9863
9864 /* Verify that condition can be parsed, before setting any
9865 breakpoints. Allocate a separate condition expression for each
9866 breakpoint. */
9867 if (!pending)
9868 {
9869 if (parse_extra)
9870 {
9871 char *rest;
9872 struct linespec_sals *lsal;
9873
9874 lsal = VEC_index (linespec_sals, canonical.sals, 0);
9875
9876 /* Here we only parse 'arg' to separate condition
9877 from thread number, so parsing in context of first
9878 sal is OK. When setting the breakpoint we'll
9879 re-parse it in context of each sal. */
9880
9881 find_condition_and_thread (extra_string, lsal->sals.sals[0].pc,
9882 &cond_string, &thread, &task, &rest);
9883 if (cond_string)
9884 make_cleanup (xfree, cond_string);
9885 if (rest)
9886 make_cleanup (xfree, rest);
9887 if (rest)
9888 extra_string = rest;
9889 else
9890 extra_string = NULL;
9891 }
9892 else
9893 {
9894 if (type_wanted != bp_dprintf
9895 && extra_string != NULL && *extra_string != '\0')
9896 error (_("Garbage '%s' at end of location"), extra_string);
9897
9898 /* Create a private copy of condition string. */
9899 if (cond_string)
9900 {
9901 cond_string = xstrdup (cond_string);
9902 make_cleanup (xfree, cond_string);
9903 }
9904 /* Create a private copy of any extra string. */
9905 if (extra_string)
9906 {
9907 extra_string = xstrdup (extra_string);
9908 make_cleanup (xfree, extra_string);
9909 }
9910 }
9911
9912 ops->create_breakpoints_sal (gdbarch, &canonical,
9913 cond_string, extra_string, type_wanted,
9914 tempflag ? disp_del : disp_donttouch,
9915 thread, task, ignore_count, ops,
9916 from_tty, enabled, internal, flags);
9917 }
9918 else
9919 {
9920 struct breakpoint *b;
9921
9922 if (is_tracepoint_type (type_wanted))
9923 {
9924 struct tracepoint *t;
9925
9926 t = XCNEW (struct tracepoint);
9927 b = &t->base;
9928 }
9929 else
9930 b = XNEW (struct breakpoint);
9931
9932 init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops);
9933 b->location = copy_event_location (location);
9934
9935 if (parse_extra)
9936 b->cond_string = NULL;
9937 else
9938 {
9939 /* Create a private copy of condition string. */
9940 if (cond_string)
9941 {
9942 cond_string = xstrdup (cond_string);
9943 make_cleanup (xfree, cond_string);
9944 }
9945 b->cond_string = cond_string;
9946 b->thread = thread;
9947 }
9948
9949 /* Create a private copy of any extra string. */
9950 if (extra_string != NULL)
9951 {
9952 extra_string = xstrdup (extra_string);
9953 make_cleanup (xfree, extra_string);
9954 }
9955 b->extra_string = extra_string;
9956 b->ignore_count = ignore_count;
9957 b->disposition = tempflag ? disp_del : disp_donttouch;
9958 b->condition_not_parsed = 1;
9959 b->enable_state = enabled ? bp_enabled : bp_disabled;
9960 if ((type_wanted != bp_breakpoint
9961 && type_wanted != bp_hardware_breakpoint) || thread != -1)
9962 b->pspace = current_program_space;
9963
9964 install_breakpoint (internal, b, 0);
9965 }
9966
9967 if (VEC_length (linespec_sals, canonical.sals) > 1)
9968 {
9969 warning (_("Multiple breakpoints were set.\nUse the "
9970 "\"delete\" command to delete unwanted breakpoints."));
9971 prev_breakpoint_count = prev_bkpt_count;
9972 }
9973
9974 /* That's it. Discard the cleanups for data inserted into the
9975 breakpoint. */
9976 discard_cleanups (bkpt_chain);
9977 /* But cleanup everything else. */
9978 do_cleanups (old_chain);
9979
9980 /* error call may happen here - have BKPT_CHAIN already discarded. */
9981 update_global_location_list (UGLL_MAY_INSERT);
9982
9983 return 1;
9984 }
9985
9986 /* Set a breakpoint.
9987 ARG is a string describing breakpoint address,
9988 condition, and thread.
9989 FLAG specifies if a breakpoint is hardware on,
9990 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9991 and BP_TEMPFLAG. */
9992
9993 static void
9994 break_command_1 (char *arg, int flag, int from_tty)
9995 {
9996 int tempflag = flag & BP_TEMPFLAG;
9997 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9998 ? bp_hardware_breakpoint
9999 : bp_breakpoint);
10000 struct breakpoint_ops *ops;
10001 struct event_location *location;
10002 struct cleanup *cleanup;
10003
10004 location = string_to_event_location (&arg, current_language);
10005 cleanup = make_cleanup_delete_event_location (location);
10006
10007 /* Matching breakpoints on probes. */
10008 if (location != NULL
10009 && event_location_type (location) == PROBE_LOCATION)
10010 ops = &bkpt_probe_breakpoint_ops;
10011 else
10012 ops = &bkpt_breakpoint_ops;
10013
10014 create_breakpoint (get_current_arch (),
10015 location,
10016 NULL, 0, arg, 1 /* parse arg */,
10017 tempflag, type_wanted,
10018 0 /* Ignore count */,
10019 pending_break_support,
10020 ops,
10021 from_tty,
10022 1 /* enabled */,
10023 0 /* internal */,
10024 0);
10025 do_cleanups (cleanup);
10026 }
10027
10028 /* Helper function for break_command_1 and disassemble_command. */
10029
10030 void
10031 resolve_sal_pc (struct symtab_and_line *sal)
10032 {
10033 CORE_ADDR pc;
10034
10035 if (sal->pc == 0 && sal->symtab != NULL)
10036 {
10037 if (!find_line_pc (sal->symtab, sal->line, &pc))
10038 error (_("No line %d in file \"%s\"."),
10039 sal->line, symtab_to_filename_for_display (sal->symtab));
10040 sal->pc = pc;
10041
10042 /* If this SAL corresponds to a breakpoint inserted using a line
10043 number, then skip the function prologue if necessary. */
10044 if (sal->explicit_line)
10045 skip_prologue_sal (sal);
10046 }
10047
10048 if (sal->section == 0 && sal->symtab != NULL)
10049 {
10050 const struct blockvector *bv;
10051 const struct block *b;
10052 struct symbol *sym;
10053
10054 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
10055 SYMTAB_COMPUNIT (sal->symtab));
10056 if (bv != NULL)
10057 {
10058 sym = block_linkage_function (b);
10059 if (sym != NULL)
10060 {
10061 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
10062 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
10063 sym);
10064 }
10065 else
10066 {
10067 /* It really is worthwhile to have the section, so we'll
10068 just have to look harder. This case can be executed
10069 if we have line numbers but no functions (as can
10070 happen in assembly source). */
10071
10072 struct bound_minimal_symbol msym;
10073 struct cleanup *old_chain = save_current_space_and_thread ();
10074
10075 switch_to_program_space_and_thread (sal->pspace);
10076
10077 msym = lookup_minimal_symbol_by_pc (sal->pc);
10078 if (msym.minsym)
10079 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
10080
10081 do_cleanups (old_chain);
10082 }
10083 }
10084 }
10085 }
10086
10087 void
10088 break_command (char *arg, int from_tty)
10089 {
10090 break_command_1 (arg, 0, from_tty);
10091 }
10092
10093 void
10094 tbreak_command (char *arg, int from_tty)
10095 {
10096 break_command_1 (arg, BP_TEMPFLAG, from_tty);
10097 }
10098
10099 static void
10100 hbreak_command (char *arg, int from_tty)
10101 {
10102 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
10103 }
10104
10105 static void
10106 thbreak_command (char *arg, int from_tty)
10107 {
10108 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
10109 }
10110
10111 static void
10112 stop_command (char *arg, int from_tty)
10113 {
10114 printf_filtered (_("Specify the type of breakpoint to set.\n\
10115 Usage: stop in <function | address>\n\
10116 stop at <line>\n"));
10117 }
10118
10119 static void
10120 stopin_command (char *arg, int from_tty)
10121 {
10122 int badInput = 0;
10123
10124 if (arg == (char *) NULL)
10125 badInput = 1;
10126 else if (*arg != '*')
10127 {
10128 char *argptr = arg;
10129 int hasColon = 0;
10130
10131 /* Look for a ':'. If this is a line number specification, then
10132 say it is bad, otherwise, it should be an address or
10133 function/method name. */
10134 while (*argptr && !hasColon)
10135 {
10136 hasColon = (*argptr == ':');
10137 argptr++;
10138 }
10139
10140 if (hasColon)
10141 badInput = (*argptr != ':'); /* Not a class::method */
10142 else
10143 badInput = isdigit (*arg); /* a simple line number */
10144 }
10145
10146 if (badInput)
10147 printf_filtered (_("Usage: stop in <function | address>\n"));
10148 else
10149 break_command_1 (arg, 0, from_tty);
10150 }
10151
10152 static void
10153 stopat_command (char *arg, int from_tty)
10154 {
10155 int badInput = 0;
10156
10157 if (arg == (char *) NULL || *arg == '*') /* no line number */
10158 badInput = 1;
10159 else
10160 {
10161 char *argptr = arg;
10162 int hasColon = 0;
10163
10164 /* Look for a ':'. If there is a '::' then get out, otherwise
10165 it is probably a line number. */
10166 while (*argptr && !hasColon)
10167 {
10168 hasColon = (*argptr == ':');
10169 argptr++;
10170 }
10171
10172 if (hasColon)
10173 badInput = (*argptr == ':'); /* we have class::method */
10174 else
10175 badInput = !isdigit (*arg); /* not a line number */
10176 }
10177
10178 if (badInput)
10179 printf_filtered (_("Usage: stop at <line>\n"));
10180 else
10181 break_command_1 (arg, 0, from_tty);
10182 }
10183
10184 /* The dynamic printf command is mostly like a regular breakpoint, but
10185 with a prewired command list consisting of a single output command,
10186 built from extra arguments supplied on the dprintf command
10187 line. */
10188
10189 static void
10190 dprintf_command (char *arg, int from_tty)
10191 {
10192 struct event_location *location;
10193 struct cleanup *cleanup;
10194
10195 location = string_to_event_location (&arg, current_language);
10196 cleanup = make_cleanup_delete_event_location (location);
10197
10198 /* If non-NULL, ARG should have been advanced past the location;
10199 the next character must be ','. */
10200 if (arg != NULL)
10201 {
10202 if (arg[0] != ',' || arg[1] == '\0')
10203 error (_("Format string required"));
10204 else
10205 {
10206 /* Skip the comma. */
10207 ++arg;
10208 }
10209 }
10210
10211 create_breakpoint (get_current_arch (),
10212 location,
10213 NULL, 0, arg, 1 /* parse arg */,
10214 0, bp_dprintf,
10215 0 /* Ignore count */,
10216 pending_break_support,
10217 &dprintf_breakpoint_ops,
10218 from_tty,
10219 1 /* enabled */,
10220 0 /* internal */,
10221 0);
10222 do_cleanups (cleanup);
10223 }
10224
10225 static void
10226 agent_printf_command (char *arg, int from_tty)
10227 {
10228 error (_("May only run agent-printf on the target"));
10229 }
10230
10231 /* Implement the "breakpoint_hit" breakpoint_ops method for
10232 ranged breakpoints. */
10233
10234 static int
10235 breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
10236 struct address_space *aspace,
10237 CORE_ADDR bp_addr,
10238 const struct target_waitstatus *ws)
10239 {
10240 if (ws->kind != TARGET_WAITKIND_STOPPED
10241 || ws->value.sig != GDB_SIGNAL_TRAP)
10242 return 0;
10243
10244 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
10245 bl->length, aspace, bp_addr);
10246 }
10247
10248 /* Implement the "resources_needed" breakpoint_ops method for
10249 ranged breakpoints. */
10250
10251 static int
10252 resources_needed_ranged_breakpoint (const struct bp_location *bl)
10253 {
10254 return target_ranged_break_num_registers ();
10255 }
10256
10257 /* Implement the "print_it" breakpoint_ops method for
10258 ranged breakpoints. */
10259
10260 static enum print_stop_action
10261 print_it_ranged_breakpoint (bpstat bs)
10262 {
10263 struct breakpoint *b = bs->breakpoint_at;
10264 struct bp_location *bl = b->loc;
10265 struct ui_out *uiout = current_uiout;
10266
10267 gdb_assert (b->type == bp_hardware_breakpoint);
10268
10269 /* Ranged breakpoints have only one location. */
10270 gdb_assert (bl && bl->next == NULL);
10271
10272 annotate_breakpoint (b->number);
10273
10274 maybe_print_thread_hit_breakpoint (uiout);
10275
10276 if (b->disposition == disp_del)
10277 ui_out_text (uiout, "Temporary ranged breakpoint ");
10278 else
10279 ui_out_text (uiout, "Ranged breakpoint ");
10280 if (ui_out_is_mi_like_p (uiout))
10281 {
10282 ui_out_field_string (uiout, "reason",
10283 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
10284 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
10285 }
10286 ui_out_field_int (uiout, "bkptno", b->number);
10287 ui_out_text (uiout, ", ");
10288
10289 return PRINT_SRC_AND_LOC;
10290 }
10291
10292 /* Implement the "print_one" breakpoint_ops method for
10293 ranged breakpoints. */
10294
10295 static void
10296 print_one_ranged_breakpoint (struct breakpoint *b,
10297 struct bp_location **last_loc)
10298 {
10299 struct bp_location *bl = b->loc;
10300 struct value_print_options opts;
10301 struct ui_out *uiout = current_uiout;
10302
10303 /* Ranged breakpoints have only one location. */
10304 gdb_assert (bl && bl->next == NULL);
10305
10306 get_user_print_options (&opts);
10307
10308 if (opts.addressprint)
10309 /* We don't print the address range here, it will be printed later
10310 by print_one_detail_ranged_breakpoint. */
10311 ui_out_field_skip (uiout, "addr");
10312 annotate_field (5);
10313 print_breakpoint_location (b, bl);
10314 *last_loc = bl;
10315 }
10316
10317 /* Implement the "print_one_detail" breakpoint_ops method for
10318 ranged breakpoints. */
10319
10320 static void
10321 print_one_detail_ranged_breakpoint (const struct breakpoint *b,
10322 struct ui_out *uiout)
10323 {
10324 CORE_ADDR address_start, address_end;
10325 struct bp_location *bl = b->loc;
10326 struct ui_file *stb = mem_fileopen ();
10327 struct cleanup *cleanup = make_cleanup_ui_file_delete (stb);
10328
10329 gdb_assert (bl);
10330
10331 address_start = bl->address;
10332 address_end = address_start + bl->length - 1;
10333
10334 ui_out_text (uiout, "\taddress range: ");
10335 fprintf_unfiltered (stb, "[%s, %s]",
10336 print_core_address (bl->gdbarch, address_start),
10337 print_core_address (bl->gdbarch, address_end));
10338 ui_out_field_stream (uiout, "addr", stb);
10339 ui_out_text (uiout, "\n");
10340
10341 do_cleanups (cleanup);
10342 }
10343
10344 /* Implement the "print_mention" breakpoint_ops method for
10345 ranged breakpoints. */
10346
10347 static void
10348 print_mention_ranged_breakpoint (struct breakpoint *b)
10349 {
10350 struct bp_location *bl = b->loc;
10351 struct ui_out *uiout = current_uiout;
10352
10353 gdb_assert (bl);
10354 gdb_assert (b->type == bp_hardware_breakpoint);
10355
10356 if (ui_out_is_mi_like_p (uiout))
10357 return;
10358
10359 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
10360 b->number, paddress (bl->gdbarch, bl->address),
10361 paddress (bl->gdbarch, bl->address + bl->length - 1));
10362 }
10363
10364 /* Implement the "print_recreate" breakpoint_ops method for
10365 ranged breakpoints. */
10366
10367 static void
10368 print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
10369 {
10370 fprintf_unfiltered (fp, "break-range %s, %s",
10371 event_location_to_string (b->location),
10372 event_location_to_string (b->location_range_end));
10373 print_recreate_thread (b, fp);
10374 }
10375
10376 /* The breakpoint_ops structure to be used in ranged breakpoints. */
10377
10378 static struct breakpoint_ops ranged_breakpoint_ops;
10379
10380 /* Find the address where the end of the breakpoint range should be
10381 placed, given the SAL of the end of the range. This is so that if
10382 the user provides a line number, the end of the range is set to the
10383 last instruction of the given line. */
10384
10385 static CORE_ADDR
10386 find_breakpoint_range_end (struct symtab_and_line sal)
10387 {
10388 CORE_ADDR end;
10389
10390 /* If the user provided a PC value, use it. Otherwise,
10391 find the address of the end of the given location. */
10392 if (sal.explicit_pc)
10393 end = sal.pc;
10394 else
10395 {
10396 int ret;
10397 CORE_ADDR start;
10398
10399 ret = find_line_pc_range (sal, &start, &end);
10400 if (!ret)
10401 error (_("Could not find location of the end of the range."));
10402
10403 /* find_line_pc_range returns the start of the next line. */
10404 end--;
10405 }
10406
10407 return end;
10408 }
10409
10410 /* Implement the "break-range" CLI command. */
10411
10412 static void
10413 break_range_command (char *arg, int from_tty)
10414 {
10415 char *arg_start, *addr_string_start;
10416 struct linespec_result canonical_start, canonical_end;
10417 int bp_count, can_use_bp, length;
10418 CORE_ADDR end;
10419 struct breakpoint *b;
10420 struct symtab_and_line sal_start, sal_end;
10421 struct cleanup *cleanup_bkpt;
10422 struct linespec_sals *lsal_start, *lsal_end;
10423 struct event_location *start_location, *end_location;
10424
10425 /* We don't support software ranged breakpoints. */
10426 if (target_ranged_break_num_registers () < 0)
10427 error (_("This target does not support hardware ranged breakpoints."));
10428
10429 bp_count = hw_breakpoint_used_count ();
10430 bp_count += target_ranged_break_num_registers ();
10431 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10432 bp_count, 0);
10433 if (can_use_bp < 0)
10434 error (_("Hardware breakpoints used exceeds limit."));
10435
10436 arg = skip_spaces (arg);
10437 if (arg == NULL || arg[0] == '\0')
10438 error(_("No address range specified."));
10439
10440 init_linespec_result (&canonical_start);
10441
10442 arg_start = arg;
10443 start_location = string_to_event_location (&arg, current_language);
10444 cleanup_bkpt = make_cleanup_delete_event_location (start_location);
10445 parse_breakpoint_sals (start_location, &canonical_start);
10446 make_cleanup_destroy_linespec_result (&canonical_start);
10447
10448 if (arg[0] != ',')
10449 error (_("Too few arguments."));
10450 else if (VEC_empty (linespec_sals, canonical_start.sals))
10451 error (_("Could not find location of the beginning of the range."));
10452
10453 lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
10454
10455 if (VEC_length (linespec_sals, canonical_start.sals) > 1
10456 || lsal_start->sals.nelts != 1)
10457 error (_("Cannot create a ranged breakpoint with multiple locations."));
10458
10459 sal_start = lsal_start->sals.sals[0];
10460 addr_string_start = savestring (arg_start, arg - arg_start);
10461 make_cleanup (xfree, addr_string_start);
10462
10463 arg++; /* Skip the comma. */
10464 arg = skip_spaces (arg);
10465
10466 /* Parse the end location. */
10467
10468 init_linespec_result (&canonical_end);
10469 arg_start = arg;
10470
10471 /* We call decode_line_full directly here instead of using
10472 parse_breakpoint_sals because we need to specify the start location's
10473 symtab and line as the default symtab and line for the end of the
10474 range. This makes it possible to have ranges like "foo.c:27, +14",
10475 where +14 means 14 lines from the start location. */
10476 end_location = string_to_event_location (&arg, current_language);
10477 make_cleanup_delete_event_location (end_location);
10478 decode_line_full (end_location, DECODE_LINE_FUNFIRSTLINE, NULL,
10479 sal_start.symtab, sal_start.line,
10480 &canonical_end, NULL, NULL);
10481
10482 make_cleanup_destroy_linespec_result (&canonical_end);
10483
10484 if (VEC_empty (linespec_sals, canonical_end.sals))
10485 error (_("Could not find location of the end of the range."));
10486
10487 lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
10488 if (VEC_length (linespec_sals, canonical_end.sals) > 1
10489 || lsal_end->sals.nelts != 1)
10490 error (_("Cannot create a ranged breakpoint with multiple locations."));
10491
10492 sal_end = lsal_end->sals.sals[0];
10493
10494 end = find_breakpoint_range_end (sal_end);
10495 if (sal_start.pc > end)
10496 error (_("Invalid address range, end precedes start."));
10497
10498 length = end - sal_start.pc + 1;
10499 if (length < 0)
10500 /* Length overflowed. */
10501 error (_("Address range too large."));
10502 else if (length == 1)
10503 {
10504 /* This range is simple enough to be handled by
10505 the `hbreak' command. */
10506 hbreak_command (addr_string_start, 1);
10507
10508 do_cleanups (cleanup_bkpt);
10509
10510 return;
10511 }
10512
10513 /* Now set up the breakpoint. */
10514 b = set_raw_breakpoint (get_current_arch (), sal_start,
10515 bp_hardware_breakpoint, &ranged_breakpoint_ops);
10516 set_breakpoint_count (breakpoint_count + 1);
10517 b->number = breakpoint_count;
10518 b->disposition = disp_donttouch;
10519 b->location = copy_event_location (start_location);
10520 b->location_range_end = copy_event_location (end_location);
10521 b->loc->length = length;
10522
10523 do_cleanups (cleanup_bkpt);
10524
10525 mention (b);
10526 observer_notify_breakpoint_created (b);
10527 update_global_location_list (UGLL_MAY_INSERT);
10528 }
10529
10530 /* Return non-zero if EXP is verified as constant. Returned zero
10531 means EXP is variable. Also the constant detection may fail for
10532 some constant expressions and in such case still falsely return
10533 zero. */
10534
10535 static int
10536 watchpoint_exp_is_const (const struct expression *exp)
10537 {
10538 int i = exp->nelts;
10539
10540 while (i > 0)
10541 {
10542 int oplenp, argsp;
10543
10544 /* We are only interested in the descriptor of each element. */
10545 operator_length (exp, i, &oplenp, &argsp);
10546 i -= oplenp;
10547
10548 switch (exp->elts[i].opcode)
10549 {
10550 case BINOP_ADD:
10551 case BINOP_SUB:
10552 case BINOP_MUL:
10553 case BINOP_DIV:
10554 case BINOP_REM:
10555 case BINOP_MOD:
10556 case BINOP_LSH:
10557 case BINOP_RSH:
10558 case BINOP_LOGICAL_AND:
10559 case BINOP_LOGICAL_OR:
10560 case BINOP_BITWISE_AND:
10561 case BINOP_BITWISE_IOR:
10562 case BINOP_BITWISE_XOR:
10563 case BINOP_EQUAL:
10564 case BINOP_NOTEQUAL:
10565 case BINOP_LESS:
10566 case BINOP_GTR:
10567 case BINOP_LEQ:
10568 case BINOP_GEQ:
10569 case BINOP_REPEAT:
10570 case BINOP_COMMA:
10571 case BINOP_EXP:
10572 case BINOP_MIN:
10573 case BINOP_MAX:
10574 case BINOP_INTDIV:
10575 case BINOP_CONCAT:
10576 case TERNOP_COND:
10577 case TERNOP_SLICE:
10578
10579 case OP_LONG:
10580 case OP_DOUBLE:
10581 case OP_DECFLOAT:
10582 case OP_LAST:
10583 case OP_COMPLEX:
10584 case OP_STRING:
10585 case OP_ARRAY:
10586 case OP_TYPE:
10587 case OP_TYPEOF:
10588 case OP_DECLTYPE:
10589 case OP_TYPEID:
10590 case OP_NAME:
10591 case OP_OBJC_NSSTRING:
10592
10593 case UNOP_NEG:
10594 case UNOP_LOGICAL_NOT:
10595 case UNOP_COMPLEMENT:
10596 case UNOP_ADDR:
10597 case UNOP_HIGH:
10598 case UNOP_CAST:
10599
10600 case UNOP_CAST_TYPE:
10601 case UNOP_REINTERPRET_CAST:
10602 case UNOP_DYNAMIC_CAST:
10603 /* Unary, binary and ternary operators: We have to check
10604 their operands. If they are constant, then so is the
10605 result of that operation. For instance, if A and B are
10606 determined to be constants, then so is "A + B".
10607
10608 UNOP_IND is one exception to the rule above, because the
10609 value of *ADDR is not necessarily a constant, even when
10610 ADDR is. */
10611 break;
10612
10613 case OP_VAR_VALUE:
10614 /* Check whether the associated symbol is a constant.
10615
10616 We use SYMBOL_CLASS rather than TYPE_CONST because it's
10617 possible that a buggy compiler could mark a variable as
10618 constant even when it is not, and TYPE_CONST would return
10619 true in this case, while SYMBOL_CLASS wouldn't.
10620
10621 We also have to check for function symbols because they
10622 are always constant. */
10623 {
10624 struct symbol *s = exp->elts[i + 2].symbol;
10625
10626 if (SYMBOL_CLASS (s) != LOC_BLOCK
10627 && SYMBOL_CLASS (s) != LOC_CONST
10628 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10629 return 0;
10630 break;
10631 }
10632
10633 /* The default action is to return 0 because we are using
10634 the optimistic approach here: If we don't know something,
10635 then it is not a constant. */
10636 default:
10637 return 0;
10638 }
10639 }
10640
10641 return 1;
10642 }
10643
10644 /* Implement the "dtor" breakpoint_ops method for watchpoints. */
10645
10646 static void
10647 dtor_watchpoint (struct breakpoint *self)
10648 {
10649 struct watchpoint *w = (struct watchpoint *) self;
10650
10651 xfree (w->cond_exp);
10652 xfree (w->exp);
10653 xfree (w->exp_string);
10654 xfree (w->exp_string_reparse);
10655 value_free (w->val);
10656
10657 base_breakpoint_ops.dtor (self);
10658 }
10659
10660 /* Implement the "re_set" breakpoint_ops method for watchpoints. */
10661
10662 static void
10663 re_set_watchpoint (struct breakpoint *b)
10664 {
10665 struct watchpoint *w = (struct watchpoint *) b;
10666
10667 /* Watchpoint can be either on expression using entirely global
10668 variables, or it can be on local variables.
10669
10670 Watchpoints of the first kind are never auto-deleted, and even
10671 persist across program restarts. Since they can use variables
10672 from shared libraries, we need to reparse expression as libraries
10673 are loaded and unloaded.
10674
10675 Watchpoints on local variables can also change meaning as result
10676 of solib event. For example, if a watchpoint uses both a local
10677 and a global variables in expression, it's a local watchpoint,
10678 but unloading of a shared library will make the expression
10679 invalid. This is not a very common use case, but we still
10680 re-evaluate expression, to avoid surprises to the user.
10681
10682 Note that for local watchpoints, we re-evaluate it only if
10683 watchpoints frame id is still valid. If it's not, it means the
10684 watchpoint is out of scope and will be deleted soon. In fact,
10685 I'm not sure we'll ever be called in this case.
10686
10687 If a local watchpoint's frame id is still valid, then
10688 w->exp_valid_block is likewise valid, and we can safely use it.
10689
10690 Don't do anything about disabled watchpoints, since they will be
10691 reevaluated again when enabled. */
10692 update_watchpoint (w, 1 /* reparse */);
10693 }
10694
10695 /* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10696
10697 static int
10698 insert_watchpoint (struct bp_location *bl)
10699 {
10700 struct watchpoint *w = (struct watchpoint *) bl->owner;
10701 int length = w->exact ? 1 : bl->length;
10702
10703 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
10704 w->cond_exp);
10705 }
10706
10707 /* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10708
10709 static int
10710 remove_watchpoint (struct bp_location *bl)
10711 {
10712 struct watchpoint *w = (struct watchpoint *) bl->owner;
10713 int length = w->exact ? 1 : bl->length;
10714
10715 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
10716 w->cond_exp);
10717 }
10718
10719 static int
10720 breakpoint_hit_watchpoint (const struct bp_location *bl,
10721 struct address_space *aspace, CORE_ADDR bp_addr,
10722 const struct target_waitstatus *ws)
10723 {
10724 struct breakpoint *b = bl->owner;
10725 struct watchpoint *w = (struct watchpoint *) b;
10726
10727 /* Continuable hardware watchpoints are treated as non-existent if the
10728 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10729 some data address). Otherwise gdb won't stop on a break instruction
10730 in the code (not from a breakpoint) when a hardware watchpoint has
10731 been defined. Also skip watchpoints which we know did not trigger
10732 (did not match the data address). */
10733 if (is_hardware_watchpoint (b)
10734 && w->watchpoint_triggered == watch_triggered_no)
10735 return 0;
10736
10737 return 1;
10738 }
10739
10740 static void
10741 check_status_watchpoint (bpstat bs)
10742 {
10743 gdb_assert (is_watchpoint (bs->breakpoint_at));
10744
10745 bpstat_check_watchpoint (bs);
10746 }
10747
10748 /* Implement the "resources_needed" breakpoint_ops method for
10749 hardware watchpoints. */
10750
10751 static int
10752 resources_needed_watchpoint (const struct bp_location *bl)
10753 {
10754 struct watchpoint *w = (struct watchpoint *) bl->owner;
10755 int length = w->exact? 1 : bl->length;
10756
10757 return target_region_ok_for_hw_watchpoint (bl->address, length);
10758 }
10759
10760 /* Implement the "works_in_software_mode" breakpoint_ops method for
10761 hardware watchpoints. */
10762
10763 static int
10764 works_in_software_mode_watchpoint (const struct breakpoint *b)
10765 {
10766 /* Read and access watchpoints only work with hardware support. */
10767 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
10768 }
10769
10770 static enum print_stop_action
10771 print_it_watchpoint (bpstat bs)
10772 {
10773 struct cleanup *old_chain;
10774 struct breakpoint *b;
10775 struct ui_file *stb;
10776 enum print_stop_action result;
10777 struct watchpoint *w;
10778 struct ui_out *uiout = current_uiout;
10779
10780 gdb_assert (bs->bp_location_at != NULL);
10781
10782 b = bs->breakpoint_at;
10783 w = (struct watchpoint *) b;
10784
10785 stb = mem_fileopen ();
10786 old_chain = make_cleanup_ui_file_delete (stb);
10787
10788 annotate_watchpoint (b->number);
10789 maybe_print_thread_hit_breakpoint (uiout);
10790
10791 switch (b->type)
10792 {
10793 case bp_watchpoint:
10794 case bp_hardware_watchpoint:
10795 if (ui_out_is_mi_like_p (uiout))
10796 ui_out_field_string
10797 (uiout, "reason",
10798 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10799 mention (b);
10800 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10801 ui_out_text (uiout, "\nOld value = ");
10802 watchpoint_value_print (bs->old_val, stb);
10803 ui_out_field_stream (uiout, "old", stb);
10804 ui_out_text (uiout, "\nNew value = ");
10805 watchpoint_value_print (w->val, stb);
10806 ui_out_field_stream (uiout, "new", stb);
10807 ui_out_text (uiout, "\n");
10808 /* More than one watchpoint may have been triggered. */
10809 result = PRINT_UNKNOWN;
10810 break;
10811
10812 case bp_read_watchpoint:
10813 if (ui_out_is_mi_like_p (uiout))
10814 ui_out_field_string
10815 (uiout, "reason",
10816 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10817 mention (b);
10818 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10819 ui_out_text (uiout, "\nValue = ");
10820 watchpoint_value_print (w->val, stb);
10821 ui_out_field_stream (uiout, "value", stb);
10822 ui_out_text (uiout, "\n");
10823 result = PRINT_UNKNOWN;
10824 break;
10825
10826 case bp_access_watchpoint:
10827 if (bs->old_val != NULL)
10828 {
10829 if (ui_out_is_mi_like_p (uiout))
10830 ui_out_field_string
10831 (uiout, "reason",
10832 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10833 mention (b);
10834 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10835 ui_out_text (uiout, "\nOld value = ");
10836 watchpoint_value_print (bs->old_val, stb);
10837 ui_out_field_stream (uiout, "old", stb);
10838 ui_out_text (uiout, "\nNew value = ");
10839 }
10840 else
10841 {
10842 mention (b);
10843 if (ui_out_is_mi_like_p (uiout))
10844 ui_out_field_string
10845 (uiout, "reason",
10846 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10847 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10848 ui_out_text (uiout, "\nValue = ");
10849 }
10850 watchpoint_value_print (w->val, stb);
10851 ui_out_field_stream (uiout, "new", stb);
10852 ui_out_text (uiout, "\n");
10853 result = PRINT_UNKNOWN;
10854 break;
10855 default:
10856 result = PRINT_UNKNOWN;
10857 }
10858
10859 do_cleanups (old_chain);
10860 return result;
10861 }
10862
10863 /* Implement the "print_mention" breakpoint_ops method for hardware
10864 watchpoints. */
10865
10866 static void
10867 print_mention_watchpoint (struct breakpoint *b)
10868 {
10869 struct cleanup *ui_out_chain;
10870 struct watchpoint *w = (struct watchpoint *) b;
10871 struct ui_out *uiout = current_uiout;
10872
10873 switch (b->type)
10874 {
10875 case bp_watchpoint:
10876 ui_out_text (uiout, "Watchpoint ");
10877 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10878 break;
10879 case bp_hardware_watchpoint:
10880 ui_out_text (uiout, "Hardware watchpoint ");
10881 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10882 break;
10883 case bp_read_watchpoint:
10884 ui_out_text (uiout, "Hardware read watchpoint ");
10885 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
10886 break;
10887 case bp_access_watchpoint:
10888 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
10889 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
10890 break;
10891 default:
10892 internal_error (__FILE__, __LINE__,
10893 _("Invalid hardware watchpoint type."));
10894 }
10895
10896 ui_out_field_int (uiout, "number", b->number);
10897 ui_out_text (uiout, ": ");
10898 ui_out_field_string (uiout, "exp", w->exp_string);
10899 do_cleanups (ui_out_chain);
10900 }
10901
10902 /* Implement the "print_recreate" breakpoint_ops method for
10903 watchpoints. */
10904
10905 static void
10906 print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10907 {
10908 struct watchpoint *w = (struct watchpoint *) b;
10909
10910 switch (b->type)
10911 {
10912 case bp_watchpoint:
10913 case bp_hardware_watchpoint:
10914 fprintf_unfiltered (fp, "watch");
10915 break;
10916 case bp_read_watchpoint:
10917 fprintf_unfiltered (fp, "rwatch");
10918 break;
10919 case bp_access_watchpoint:
10920 fprintf_unfiltered (fp, "awatch");
10921 break;
10922 default:
10923 internal_error (__FILE__, __LINE__,
10924 _("Invalid watchpoint type."));
10925 }
10926
10927 fprintf_unfiltered (fp, " %s", w->exp_string);
10928 print_recreate_thread (b, fp);
10929 }
10930
10931 /* Implement the "explains_signal" breakpoint_ops method for
10932 watchpoints. */
10933
10934 static int
10935 explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10936 {
10937 /* A software watchpoint cannot cause a signal other than
10938 GDB_SIGNAL_TRAP. */
10939 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
10940 return 0;
10941
10942 return 1;
10943 }
10944
10945 /* The breakpoint_ops structure to be used in hardware watchpoints. */
10946
10947 static struct breakpoint_ops watchpoint_breakpoint_ops;
10948
10949 /* Implement the "insert" breakpoint_ops method for
10950 masked hardware watchpoints. */
10951
10952 static int
10953 insert_masked_watchpoint (struct bp_location *bl)
10954 {
10955 struct watchpoint *w = (struct watchpoint *) bl->owner;
10956
10957 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
10958 bl->watchpoint_type);
10959 }
10960
10961 /* Implement the "remove" breakpoint_ops method for
10962 masked hardware watchpoints. */
10963
10964 static int
10965 remove_masked_watchpoint (struct bp_location *bl)
10966 {
10967 struct watchpoint *w = (struct watchpoint *) bl->owner;
10968
10969 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
10970 bl->watchpoint_type);
10971 }
10972
10973 /* Implement the "resources_needed" breakpoint_ops method for
10974 masked hardware watchpoints. */
10975
10976 static int
10977 resources_needed_masked_watchpoint (const struct bp_location *bl)
10978 {
10979 struct watchpoint *w = (struct watchpoint *) bl->owner;
10980
10981 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
10982 }
10983
10984 /* Implement the "works_in_software_mode" breakpoint_ops method for
10985 masked hardware watchpoints. */
10986
10987 static int
10988 works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10989 {
10990 return 0;
10991 }
10992
10993 /* Implement the "print_it" breakpoint_ops method for
10994 masked hardware watchpoints. */
10995
10996 static enum print_stop_action
10997 print_it_masked_watchpoint (bpstat bs)
10998 {
10999 struct breakpoint *b = bs->breakpoint_at;
11000 struct ui_out *uiout = current_uiout;
11001
11002 /* Masked watchpoints have only one location. */
11003 gdb_assert (b->loc && b->loc->next == NULL);
11004
11005 annotate_watchpoint (b->number);
11006 maybe_print_thread_hit_breakpoint (uiout);
11007
11008 switch (b->type)
11009 {
11010 case bp_hardware_watchpoint:
11011 if (ui_out_is_mi_like_p (uiout))
11012 ui_out_field_string
11013 (uiout, "reason",
11014 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
11015 break;
11016
11017 case bp_read_watchpoint:
11018 if (ui_out_is_mi_like_p (uiout))
11019 ui_out_field_string
11020 (uiout, "reason",
11021 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
11022 break;
11023
11024 case bp_access_watchpoint:
11025 if (ui_out_is_mi_like_p (uiout))
11026 ui_out_field_string
11027 (uiout, "reason",
11028 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
11029 break;
11030 default:
11031 internal_error (__FILE__, __LINE__,
11032 _("Invalid hardware watchpoint type."));
11033 }
11034
11035 mention (b);
11036 ui_out_text (uiout, _("\n\
11037 Check the underlying instruction at PC for the memory\n\
11038 address and value which triggered this watchpoint.\n"));
11039 ui_out_text (uiout, "\n");
11040
11041 /* More than one watchpoint may have been triggered. */
11042 return PRINT_UNKNOWN;
11043 }
11044
11045 /* Implement the "print_one_detail" breakpoint_ops method for
11046 masked hardware watchpoints. */
11047
11048 static void
11049 print_one_detail_masked_watchpoint (const struct breakpoint *b,
11050 struct ui_out *uiout)
11051 {
11052 struct watchpoint *w = (struct watchpoint *) b;
11053
11054 /* Masked watchpoints have only one location. */
11055 gdb_assert (b->loc && b->loc->next == NULL);
11056
11057 ui_out_text (uiout, "\tmask ");
11058 ui_out_field_core_addr (uiout, "mask", b->loc->gdbarch, w->hw_wp_mask);
11059 ui_out_text (uiout, "\n");
11060 }
11061
11062 /* Implement the "print_mention" breakpoint_ops method for
11063 masked hardware watchpoints. */
11064
11065 static void
11066 print_mention_masked_watchpoint (struct breakpoint *b)
11067 {
11068 struct watchpoint *w = (struct watchpoint *) b;
11069 struct ui_out *uiout = current_uiout;
11070 struct cleanup *ui_out_chain;
11071
11072 switch (b->type)
11073 {
11074 case bp_hardware_watchpoint:
11075 ui_out_text (uiout, "Masked hardware watchpoint ");
11076 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
11077 break;
11078 case bp_read_watchpoint:
11079 ui_out_text (uiout, "Masked hardware read watchpoint ");
11080 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
11081 break;
11082 case bp_access_watchpoint:
11083 ui_out_text (uiout, "Masked hardware access (read/write) watchpoint ");
11084 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
11085 break;
11086 default:
11087 internal_error (__FILE__, __LINE__,
11088 _("Invalid hardware watchpoint type."));
11089 }
11090
11091 ui_out_field_int (uiout, "number", b->number);
11092 ui_out_text (uiout, ": ");
11093 ui_out_field_string (uiout, "exp", w->exp_string);
11094 do_cleanups (ui_out_chain);
11095 }
11096
11097 /* Implement the "print_recreate" breakpoint_ops method for
11098 masked hardware watchpoints. */
11099
11100 static void
11101 print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
11102 {
11103 struct watchpoint *w = (struct watchpoint *) b;
11104 char tmp[40];
11105
11106 switch (b->type)
11107 {
11108 case bp_hardware_watchpoint:
11109 fprintf_unfiltered (fp, "watch");
11110 break;
11111 case bp_read_watchpoint:
11112 fprintf_unfiltered (fp, "rwatch");
11113 break;
11114 case bp_access_watchpoint:
11115 fprintf_unfiltered (fp, "awatch");
11116 break;
11117 default:
11118 internal_error (__FILE__, __LINE__,
11119 _("Invalid hardware watchpoint type."));
11120 }
11121
11122 sprintf_vma (tmp, w->hw_wp_mask);
11123 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
11124 print_recreate_thread (b, fp);
11125 }
11126
11127 /* The breakpoint_ops structure to be used in masked hardware watchpoints. */
11128
11129 static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
11130
11131 /* Tell whether the given watchpoint is a masked hardware watchpoint. */
11132
11133 static int
11134 is_masked_watchpoint (const struct breakpoint *b)
11135 {
11136 return b->ops == &masked_watchpoint_breakpoint_ops;
11137 }
11138
11139 /* accessflag: hw_write: watch write,
11140 hw_read: watch read,
11141 hw_access: watch access (read or write) */
11142 static void
11143 watch_command_1 (const char *arg, int accessflag, int from_tty,
11144 int just_location, int internal)
11145 {
11146 struct breakpoint *b, *scope_breakpoint = NULL;
11147 struct expression *exp;
11148 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
11149 struct value *val, *mark, *result;
11150 int saved_bitpos = 0, saved_bitsize = 0;
11151 struct frame_info *frame;
11152 const char *exp_start = NULL;
11153 const char *exp_end = NULL;
11154 const char *tok, *end_tok;
11155 int toklen = -1;
11156 const char *cond_start = NULL;
11157 const char *cond_end = NULL;
11158 enum bptype bp_type;
11159 int thread = -1;
11160 int pc = 0;
11161 /* Flag to indicate whether we are going to use masks for
11162 the hardware watchpoint. */
11163 int use_mask = 0;
11164 CORE_ADDR mask = 0;
11165 struct watchpoint *w;
11166 char *expression;
11167 struct cleanup *back_to;
11168
11169 /* Make sure that we actually have parameters to parse. */
11170 if (arg != NULL && arg[0] != '\0')
11171 {
11172 const char *value_start;
11173
11174 exp_end = arg + strlen (arg);
11175
11176 /* Look for "parameter value" pairs at the end
11177 of the arguments string. */
11178 for (tok = exp_end - 1; tok > arg; tok--)
11179 {
11180 /* Skip whitespace at the end of the argument list. */
11181 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11182 tok--;
11183
11184 /* Find the beginning of the last token.
11185 This is the value of the parameter. */
11186 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11187 tok--;
11188 value_start = tok + 1;
11189
11190 /* Skip whitespace. */
11191 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11192 tok--;
11193
11194 end_tok = tok;
11195
11196 /* Find the beginning of the second to last token.
11197 This is the parameter itself. */
11198 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11199 tok--;
11200 tok++;
11201 toklen = end_tok - tok + 1;
11202
11203 if (toklen == 6 && startswith (tok, "thread"))
11204 {
11205 struct thread_info *thr;
11206 /* At this point we've found a "thread" token, which means
11207 the user is trying to set a watchpoint that triggers
11208 only in a specific thread. */
11209 const char *endp;
11210
11211 if (thread != -1)
11212 error(_("You can specify only one thread."));
11213
11214 /* Extract the thread ID from the next token. */
11215 thr = parse_thread_id (value_start, &endp);
11216
11217 /* Check if the user provided a valid thread ID. */
11218 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
11219 invalid_thread_id_error (value_start);
11220
11221 thread = thr->global_num;
11222 }
11223 else if (toklen == 4 && startswith (tok, "mask"))
11224 {
11225 /* We've found a "mask" token, which means the user wants to
11226 create a hardware watchpoint that is going to have the mask
11227 facility. */
11228 struct value *mask_value, *mark;
11229
11230 if (use_mask)
11231 error(_("You can specify only one mask."));
11232
11233 use_mask = just_location = 1;
11234
11235 mark = value_mark ();
11236 mask_value = parse_to_comma_and_eval (&value_start);
11237 mask = value_as_address (mask_value);
11238 value_free_to_mark (mark);
11239 }
11240 else
11241 /* We didn't recognize what we found. We should stop here. */
11242 break;
11243
11244 /* Truncate the string and get rid of the "parameter value" pair before
11245 the arguments string is parsed by the parse_exp_1 function. */
11246 exp_end = tok;
11247 }
11248 }
11249 else
11250 exp_end = arg;
11251
11252 /* Parse the rest of the arguments. From here on out, everything
11253 is in terms of a newly allocated string instead of the original
11254 ARG. */
11255 innermost_block = NULL;
11256 expression = savestring (arg, exp_end - arg);
11257 back_to = make_cleanup (xfree, expression);
11258 exp_start = arg = expression;
11259 exp = parse_exp_1 (&arg, 0, 0, 0);
11260 exp_end = arg;
11261 /* Remove trailing whitespace from the expression before saving it.
11262 This makes the eventual display of the expression string a bit
11263 prettier. */
11264 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
11265 --exp_end;
11266
11267 /* Checking if the expression is not constant. */
11268 if (watchpoint_exp_is_const (exp))
11269 {
11270 int len;
11271
11272 len = exp_end - exp_start;
11273 while (len > 0 && isspace (exp_start[len - 1]))
11274 len--;
11275 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
11276 }
11277
11278 exp_valid_block = innermost_block;
11279 mark = value_mark ();
11280 fetch_subexp_value (exp, &pc, &val, &result, NULL, just_location);
11281
11282 if (val != NULL && just_location)
11283 {
11284 saved_bitpos = value_bitpos (val);
11285 saved_bitsize = value_bitsize (val);
11286 }
11287
11288 if (just_location)
11289 {
11290 int ret;
11291
11292 exp_valid_block = NULL;
11293 val = value_addr (result);
11294 release_value (val);
11295 value_free_to_mark (mark);
11296
11297 if (use_mask)
11298 {
11299 ret = target_masked_watch_num_registers (value_as_address (val),
11300 mask);
11301 if (ret == -1)
11302 error (_("This target does not support masked watchpoints."));
11303 else if (ret == -2)
11304 error (_("Invalid mask or memory region."));
11305 }
11306 }
11307 else if (val != NULL)
11308 release_value (val);
11309
11310 tok = skip_spaces_const (arg);
11311 end_tok = skip_to_space_const (tok);
11312
11313 toklen = end_tok - tok;
11314 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
11315 {
11316 struct expression *cond;
11317
11318 innermost_block = NULL;
11319 tok = cond_start = end_tok + 1;
11320 cond = parse_exp_1 (&tok, 0, 0, 0);
11321
11322 /* The watchpoint expression may not be local, but the condition
11323 may still be. E.g.: `watch global if local > 0'. */
11324 cond_exp_valid_block = innermost_block;
11325
11326 xfree (cond);
11327 cond_end = tok;
11328 }
11329 if (*tok)
11330 error (_("Junk at end of command."));
11331
11332 frame = block_innermost_frame (exp_valid_block);
11333
11334 /* If the expression is "local", then set up a "watchpoint scope"
11335 breakpoint at the point where we've left the scope of the watchpoint
11336 expression. Create the scope breakpoint before the watchpoint, so
11337 that we will encounter it first in bpstat_stop_status. */
11338 if (exp_valid_block && frame)
11339 {
11340 if (frame_id_p (frame_unwind_caller_id (frame)))
11341 {
11342 scope_breakpoint
11343 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
11344 frame_unwind_caller_pc (frame),
11345 bp_watchpoint_scope,
11346 &momentary_breakpoint_ops);
11347
11348 scope_breakpoint->enable_state = bp_enabled;
11349
11350 /* Automatically delete the breakpoint when it hits. */
11351 scope_breakpoint->disposition = disp_del;
11352
11353 /* Only break in the proper frame (help with recursion). */
11354 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
11355
11356 /* Set the address at which we will stop. */
11357 scope_breakpoint->loc->gdbarch
11358 = frame_unwind_caller_arch (frame);
11359 scope_breakpoint->loc->requested_address
11360 = frame_unwind_caller_pc (frame);
11361 scope_breakpoint->loc->address
11362 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
11363 scope_breakpoint->loc->requested_address,
11364 scope_breakpoint->type);
11365 }
11366 }
11367
11368 /* Now set up the breakpoint. We create all watchpoints as hardware
11369 watchpoints here even if hardware watchpoints are turned off, a call
11370 to update_watchpoint later in this function will cause the type to
11371 drop back to bp_watchpoint (software watchpoint) if required. */
11372
11373 if (accessflag == hw_read)
11374 bp_type = bp_read_watchpoint;
11375 else if (accessflag == hw_access)
11376 bp_type = bp_access_watchpoint;
11377 else
11378 bp_type = bp_hardware_watchpoint;
11379
11380 w = XCNEW (struct watchpoint);
11381 b = &w->base;
11382 if (use_mask)
11383 init_raw_breakpoint_without_location (b, NULL, bp_type,
11384 &masked_watchpoint_breakpoint_ops);
11385 else
11386 init_raw_breakpoint_without_location (b, NULL, bp_type,
11387 &watchpoint_breakpoint_ops);
11388 b->thread = thread;
11389 b->disposition = disp_donttouch;
11390 b->pspace = current_program_space;
11391 w->exp = exp;
11392 w->exp_valid_block = exp_valid_block;
11393 w->cond_exp_valid_block = cond_exp_valid_block;
11394 if (just_location)
11395 {
11396 struct type *t = value_type (val);
11397 CORE_ADDR addr = value_as_address (val);
11398 char *name;
11399
11400 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
11401 name = type_to_string (t);
11402
11403 w->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
11404 core_addr_to_string (addr));
11405 xfree (name);
11406
11407 w->exp_string = xstrprintf ("-location %.*s",
11408 (int) (exp_end - exp_start), exp_start);
11409
11410 /* The above expression is in C. */
11411 b->language = language_c;
11412 }
11413 else
11414 w->exp_string = savestring (exp_start, exp_end - exp_start);
11415
11416 if (use_mask)
11417 {
11418 w->hw_wp_mask = mask;
11419 }
11420 else
11421 {
11422 w->val = val;
11423 w->val_bitpos = saved_bitpos;
11424 w->val_bitsize = saved_bitsize;
11425 w->val_valid = 1;
11426 }
11427
11428 if (cond_start)
11429 b->cond_string = savestring (cond_start, cond_end - cond_start);
11430 else
11431 b->cond_string = 0;
11432
11433 if (frame)
11434 {
11435 w->watchpoint_frame = get_frame_id (frame);
11436 w->watchpoint_thread = inferior_ptid;
11437 }
11438 else
11439 {
11440 w->watchpoint_frame = null_frame_id;
11441 w->watchpoint_thread = null_ptid;
11442 }
11443
11444 if (scope_breakpoint != NULL)
11445 {
11446 /* The scope breakpoint is related to the watchpoint. We will
11447 need to act on them together. */
11448 b->related_breakpoint = scope_breakpoint;
11449 scope_breakpoint->related_breakpoint = b;
11450 }
11451
11452 if (!just_location)
11453 value_free_to_mark (mark);
11454
11455 TRY
11456 {
11457 /* Finally update the new watchpoint. This creates the locations
11458 that should be inserted. */
11459 update_watchpoint (w, 1);
11460 }
11461 CATCH (e, RETURN_MASK_ALL)
11462 {
11463 delete_breakpoint (b);
11464 throw_exception (e);
11465 }
11466 END_CATCH
11467
11468 install_breakpoint (internal, b, 1);
11469 do_cleanups (back_to);
11470 }
11471
11472 /* Return count of debug registers needed to watch the given expression.
11473 If the watchpoint cannot be handled in hardware return zero. */
11474
11475 static int
11476 can_use_hardware_watchpoint (struct value *v)
11477 {
11478 int found_memory_cnt = 0;
11479 struct value *head = v;
11480
11481 /* Did the user specifically forbid us to use hardware watchpoints? */
11482 if (!can_use_hw_watchpoints)
11483 return 0;
11484
11485 /* Make sure that the value of the expression depends only upon
11486 memory contents, and values computed from them within GDB. If we
11487 find any register references or function calls, we can't use a
11488 hardware watchpoint.
11489
11490 The idea here is that evaluating an expression generates a series
11491 of values, one holding the value of every subexpression. (The
11492 expression a*b+c has five subexpressions: a, b, a*b, c, and
11493 a*b+c.) GDB's values hold almost enough information to establish
11494 the criteria given above --- they identify memory lvalues,
11495 register lvalues, computed values, etcetera. So we can evaluate
11496 the expression, and then scan the chain of values that leaves
11497 behind to decide whether we can detect any possible change to the
11498 expression's final value using only hardware watchpoints.
11499
11500 However, I don't think that the values returned by inferior
11501 function calls are special in any way. So this function may not
11502 notice that an expression involving an inferior function call
11503 can't be watched with hardware watchpoints. FIXME. */
11504 for (; v; v = value_next (v))
11505 {
11506 if (VALUE_LVAL (v) == lval_memory)
11507 {
11508 if (v != head && value_lazy (v))
11509 /* A lazy memory lvalue in the chain is one that GDB never
11510 needed to fetch; we either just used its address (e.g.,
11511 `a' in `a.b') or we never needed it at all (e.g., `a'
11512 in `a,b'). This doesn't apply to HEAD; if that is
11513 lazy then it was not readable, but watch it anyway. */
11514 ;
11515 else
11516 {
11517 /* Ahh, memory we actually used! Check if we can cover
11518 it with hardware watchpoints. */
11519 struct type *vtype = check_typedef (value_type (v));
11520
11521 /* We only watch structs and arrays if user asked for it
11522 explicitly, never if they just happen to appear in a
11523 middle of some value chain. */
11524 if (v == head
11525 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11526 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11527 {
11528 CORE_ADDR vaddr = value_address (v);
11529 int len;
11530 int num_regs;
11531
11532 len = (target_exact_watchpoints
11533 && is_scalar_type_recursive (vtype))?
11534 1 : TYPE_LENGTH (value_type (v));
11535
11536 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11537 if (!num_regs)
11538 return 0;
11539 else
11540 found_memory_cnt += num_regs;
11541 }
11542 }
11543 }
11544 else if (VALUE_LVAL (v) != not_lval
11545 && deprecated_value_modifiable (v) == 0)
11546 return 0; /* These are values from the history (e.g., $1). */
11547 else if (VALUE_LVAL (v) == lval_register)
11548 return 0; /* Cannot watch a register with a HW watchpoint. */
11549 }
11550
11551 /* The expression itself looks suitable for using a hardware
11552 watchpoint, but give the target machine a chance to reject it. */
11553 return found_memory_cnt;
11554 }
11555
11556 void
11557 watch_command_wrapper (char *arg, int from_tty, int internal)
11558 {
11559 watch_command_1 (arg, hw_write, from_tty, 0, internal);
11560 }
11561
11562 /* A helper function that looks for the "-location" argument and then
11563 calls watch_command_1. */
11564
11565 static void
11566 watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11567 {
11568 int just_location = 0;
11569
11570 if (arg
11571 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11572 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11573 {
11574 arg = skip_spaces (arg);
11575 just_location = 1;
11576 }
11577
11578 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
11579 }
11580
11581 static void
11582 watch_command (char *arg, int from_tty)
11583 {
11584 watch_maybe_just_location (arg, hw_write, from_tty);
11585 }
11586
11587 void
11588 rwatch_command_wrapper (char *arg, int from_tty, int internal)
11589 {
11590 watch_command_1 (arg, hw_read, from_tty, 0, internal);
11591 }
11592
11593 static void
11594 rwatch_command (char *arg, int from_tty)
11595 {
11596 watch_maybe_just_location (arg, hw_read, from_tty);
11597 }
11598
11599 void
11600 awatch_command_wrapper (char *arg, int from_tty, int internal)
11601 {
11602 watch_command_1 (arg, hw_access, from_tty, 0, internal);
11603 }
11604
11605 static void
11606 awatch_command (char *arg, int from_tty)
11607 {
11608 watch_maybe_just_location (arg, hw_access, from_tty);
11609 }
11610 \f
11611
11612 /* Data for the FSM that manages the until(location)/advance commands
11613 in infcmd.c. Here because it uses the mechanisms of
11614 breakpoints. */
11615
11616 struct until_break_fsm
11617 {
11618 /* The base class. */
11619 struct thread_fsm thread_fsm;
11620
11621 /* The thread that as current when the command was executed. */
11622 int thread;
11623
11624 /* The breakpoint set at the destination location. */
11625 struct breakpoint *location_breakpoint;
11626
11627 /* Breakpoint set at the return address in the caller frame. May be
11628 NULL. */
11629 struct breakpoint *caller_breakpoint;
11630 };
11631
11632 static void until_break_fsm_clean_up (struct thread_fsm *self,
11633 struct thread_info *thread);
11634 static int until_break_fsm_should_stop (struct thread_fsm *self,
11635 struct thread_info *thread);
11636 static enum async_reply_reason
11637 until_break_fsm_async_reply_reason (struct thread_fsm *self);
11638
11639 /* until_break_fsm's vtable. */
11640
11641 static struct thread_fsm_ops until_break_fsm_ops =
11642 {
11643 NULL, /* dtor */
11644 until_break_fsm_clean_up,
11645 until_break_fsm_should_stop,
11646 NULL, /* return_value */
11647 until_break_fsm_async_reply_reason,
11648 };
11649
11650 /* Allocate a new until_break_command_fsm. */
11651
11652 static struct until_break_fsm *
11653 new_until_break_fsm (struct interp *cmd_interp, int thread,
11654 struct breakpoint *location_breakpoint,
11655 struct breakpoint *caller_breakpoint)
11656 {
11657 struct until_break_fsm *sm;
11658
11659 sm = XCNEW (struct until_break_fsm);
11660 thread_fsm_ctor (&sm->thread_fsm, &until_break_fsm_ops, cmd_interp);
11661
11662 sm->thread = thread;
11663 sm->location_breakpoint = location_breakpoint;
11664 sm->caller_breakpoint = caller_breakpoint;
11665
11666 return sm;
11667 }
11668
11669 /* Implementation of the 'should_stop' FSM method for the
11670 until(location)/advance commands. */
11671
11672 static int
11673 until_break_fsm_should_stop (struct thread_fsm *self,
11674 struct thread_info *tp)
11675 {
11676 struct until_break_fsm *sm = (struct until_break_fsm *) self;
11677
11678 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
11679 sm->location_breakpoint) != NULL
11680 || (sm->caller_breakpoint != NULL
11681 && bpstat_find_breakpoint (tp->control.stop_bpstat,
11682 sm->caller_breakpoint) != NULL))
11683 thread_fsm_set_finished (self);
11684
11685 return 1;
11686 }
11687
11688 /* Implementation of the 'clean_up' FSM method for the
11689 until(location)/advance commands. */
11690
11691 static void
11692 until_break_fsm_clean_up (struct thread_fsm *self,
11693 struct thread_info *thread)
11694 {
11695 struct until_break_fsm *sm = (struct until_break_fsm *) self;
11696
11697 /* Clean up our temporary breakpoints. */
11698 if (sm->location_breakpoint != NULL)
11699 {
11700 delete_breakpoint (sm->location_breakpoint);
11701 sm->location_breakpoint = NULL;
11702 }
11703 if (sm->caller_breakpoint != NULL)
11704 {
11705 delete_breakpoint (sm->caller_breakpoint);
11706 sm->caller_breakpoint = NULL;
11707 }
11708 delete_longjmp_breakpoint (sm->thread);
11709 }
11710
11711 /* Implementation of the 'async_reply_reason' FSM method for the
11712 until(location)/advance commands. */
11713
11714 static enum async_reply_reason
11715 until_break_fsm_async_reply_reason (struct thread_fsm *self)
11716 {
11717 return EXEC_ASYNC_LOCATION_REACHED;
11718 }
11719
11720 void
11721 until_break_command (char *arg, int from_tty, int anywhere)
11722 {
11723 struct symtabs_and_lines sals;
11724 struct symtab_and_line sal;
11725 struct frame_info *frame;
11726 struct gdbarch *frame_gdbarch;
11727 struct frame_id stack_frame_id;
11728 struct frame_id caller_frame_id;
11729 struct breakpoint *location_breakpoint;
11730 struct breakpoint *caller_breakpoint = NULL;
11731 struct cleanup *old_chain, *cleanup;
11732 int thread;
11733 struct thread_info *tp;
11734 struct event_location *location;
11735 struct until_break_fsm *sm;
11736
11737 clear_proceed_status (0);
11738
11739 /* Set a breakpoint where the user wants it and at return from
11740 this function. */
11741
11742 location = string_to_event_location (&arg, current_language);
11743 cleanup = make_cleanup_delete_event_location (location);
11744
11745 if (last_displayed_sal_is_valid ())
11746 sals = decode_line_1 (location, DECODE_LINE_FUNFIRSTLINE, NULL,
11747 get_last_displayed_symtab (),
11748 get_last_displayed_line ());
11749 else
11750 sals = decode_line_1 (location, DECODE_LINE_FUNFIRSTLINE,
11751 NULL, (struct symtab *) NULL, 0);
11752
11753 if (sals.nelts != 1)
11754 error (_("Couldn't get information on specified line."));
11755
11756 sal = sals.sals[0];
11757 xfree (sals.sals); /* malloc'd, so freed. */
11758
11759 if (*arg)
11760 error (_("Junk at end of arguments."));
11761
11762 resolve_sal_pc (&sal);
11763
11764 tp = inferior_thread ();
11765 thread = tp->global_num;
11766
11767 old_chain = make_cleanup (null_cleanup, NULL);
11768
11769 /* Note linespec handling above invalidates the frame chain.
11770 Installing a breakpoint also invalidates the frame chain (as it
11771 may need to switch threads), so do any frame handling before
11772 that. */
11773
11774 frame = get_selected_frame (NULL);
11775 frame_gdbarch = get_frame_arch (frame);
11776 stack_frame_id = get_stack_frame_id (frame);
11777 caller_frame_id = frame_unwind_caller_id (frame);
11778
11779 /* Keep within the current frame, or in frames called by the current
11780 one. */
11781
11782 if (frame_id_p (caller_frame_id))
11783 {
11784 struct symtab_and_line sal2;
11785 struct gdbarch *caller_gdbarch;
11786
11787 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11788 sal2.pc = frame_unwind_caller_pc (frame);
11789 caller_gdbarch = frame_unwind_caller_arch (frame);
11790 caller_breakpoint = set_momentary_breakpoint (caller_gdbarch,
11791 sal2,
11792 caller_frame_id,
11793 bp_until);
11794 make_cleanup_delete_breakpoint (caller_breakpoint);
11795
11796 set_longjmp_breakpoint (tp, caller_frame_id);
11797 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
11798 }
11799
11800 /* set_momentary_breakpoint could invalidate FRAME. */
11801 frame = NULL;
11802
11803 if (anywhere)
11804 /* If the user told us to continue until a specified location,
11805 we don't specify a frame at which we need to stop. */
11806 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11807 null_frame_id, bp_until);
11808 else
11809 /* Otherwise, specify the selected frame, because we want to stop
11810 only at the very same frame. */
11811 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11812 stack_frame_id, bp_until);
11813 make_cleanup_delete_breakpoint (location_breakpoint);
11814
11815 sm = new_until_break_fsm (command_interp (), tp->global_num,
11816 location_breakpoint, caller_breakpoint);
11817 tp->thread_fsm = &sm->thread_fsm;
11818
11819 discard_cleanups (old_chain);
11820
11821 proceed (-1, GDB_SIGNAL_DEFAULT);
11822
11823 do_cleanups (cleanup);
11824 }
11825
11826 /* This function attempts to parse an optional "if <cond>" clause
11827 from the arg string. If one is not found, it returns NULL.
11828
11829 Else, it returns a pointer to the condition string. (It does not
11830 attempt to evaluate the string against a particular block.) And,
11831 it updates arg to point to the first character following the parsed
11832 if clause in the arg string. */
11833
11834 char *
11835 ep_parse_optional_if_clause (char **arg)
11836 {
11837 char *cond_string;
11838
11839 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
11840 return NULL;
11841
11842 /* Skip the "if" keyword. */
11843 (*arg) += 2;
11844
11845 /* Skip any extra leading whitespace, and record the start of the
11846 condition string. */
11847 *arg = skip_spaces (*arg);
11848 cond_string = *arg;
11849
11850 /* Assume that the condition occupies the remainder of the arg
11851 string. */
11852 (*arg) += strlen (cond_string);
11853
11854 return cond_string;
11855 }
11856
11857 /* Commands to deal with catching events, such as signals, exceptions,
11858 process start/exit, etc. */
11859
11860 typedef enum
11861 {
11862 catch_fork_temporary, catch_vfork_temporary,
11863 catch_fork_permanent, catch_vfork_permanent
11864 }
11865 catch_fork_kind;
11866
11867 static void
11868 catch_fork_command_1 (char *arg, int from_tty,
11869 struct cmd_list_element *command)
11870 {
11871 struct gdbarch *gdbarch = get_current_arch ();
11872 char *cond_string = NULL;
11873 catch_fork_kind fork_kind;
11874 int tempflag;
11875
11876 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11877 tempflag = (fork_kind == catch_fork_temporary
11878 || fork_kind == catch_vfork_temporary);
11879
11880 if (!arg)
11881 arg = "";
11882 arg = skip_spaces (arg);
11883
11884 /* The allowed syntax is:
11885 catch [v]fork
11886 catch [v]fork if <cond>
11887
11888 First, check if there's an if clause. */
11889 cond_string = ep_parse_optional_if_clause (&arg);
11890
11891 if ((*arg != '\0') && !isspace (*arg))
11892 error (_("Junk at end of arguments."));
11893
11894 /* If this target supports it, create a fork or vfork catchpoint
11895 and enable reporting of such events. */
11896 switch (fork_kind)
11897 {
11898 case catch_fork_temporary:
11899 case catch_fork_permanent:
11900 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11901 &catch_fork_breakpoint_ops);
11902 break;
11903 case catch_vfork_temporary:
11904 case catch_vfork_permanent:
11905 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11906 &catch_vfork_breakpoint_ops);
11907 break;
11908 default:
11909 error (_("unsupported or unknown fork kind; cannot catch it"));
11910 break;
11911 }
11912 }
11913
11914 static void
11915 catch_exec_command_1 (char *arg, int from_tty,
11916 struct cmd_list_element *command)
11917 {
11918 struct exec_catchpoint *c;
11919 struct gdbarch *gdbarch = get_current_arch ();
11920 int tempflag;
11921 char *cond_string = NULL;
11922
11923 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11924
11925 if (!arg)
11926 arg = "";
11927 arg = skip_spaces (arg);
11928
11929 /* The allowed syntax is:
11930 catch exec
11931 catch exec if <cond>
11932
11933 First, check if there's an if clause. */
11934 cond_string = ep_parse_optional_if_clause (&arg);
11935
11936 if ((*arg != '\0') && !isspace (*arg))
11937 error (_("Junk at end of arguments."));
11938
11939 c = XNEW (struct exec_catchpoint);
11940 init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
11941 &catch_exec_breakpoint_ops);
11942 c->exec_pathname = NULL;
11943
11944 install_breakpoint (0, &c->base, 1);
11945 }
11946
11947 void
11948 init_ada_exception_breakpoint (struct breakpoint *b,
11949 struct gdbarch *gdbarch,
11950 struct symtab_and_line sal,
11951 char *addr_string,
11952 const struct breakpoint_ops *ops,
11953 int tempflag,
11954 int enabled,
11955 int from_tty)
11956 {
11957 if (from_tty)
11958 {
11959 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11960 if (!loc_gdbarch)
11961 loc_gdbarch = gdbarch;
11962
11963 describe_other_breakpoints (loc_gdbarch,
11964 sal.pspace, sal.pc, sal.section, -1);
11965 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11966 version for exception catchpoints, because two catchpoints
11967 used for different exception names will use the same address.
11968 In this case, a "breakpoint ... also set at..." warning is
11969 unproductive. Besides, the warning phrasing is also a bit
11970 inappropriate, we should use the word catchpoint, and tell
11971 the user what type of catchpoint it is. The above is good
11972 enough for now, though. */
11973 }
11974
11975 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
11976
11977 b->enable_state = enabled ? bp_enabled : bp_disabled;
11978 b->disposition = tempflag ? disp_del : disp_donttouch;
11979 b->location = string_to_event_location (&addr_string,
11980 language_def (language_ada));
11981 b->language = language_ada;
11982 }
11983
11984 static void
11985 catch_command (char *arg, int from_tty)
11986 {
11987 error (_("Catch requires an event name."));
11988 }
11989 \f
11990
11991 static void
11992 tcatch_command (char *arg, int from_tty)
11993 {
11994 error (_("Catch requires an event name."));
11995 }
11996
11997 /* A qsort comparison function that sorts breakpoints in order. */
11998
11999 static int
12000 compare_breakpoints (const void *a, const void *b)
12001 {
12002 const breakpoint_p *ba = (const breakpoint_p *) a;
12003 uintptr_t ua = (uintptr_t) *ba;
12004 const breakpoint_p *bb = (const breakpoint_p *) b;
12005 uintptr_t ub = (uintptr_t) *bb;
12006
12007 if ((*ba)->number < (*bb)->number)
12008 return -1;
12009 else if ((*ba)->number > (*bb)->number)
12010 return 1;
12011
12012 /* Now sort by address, in case we see, e..g, two breakpoints with
12013 the number 0. */
12014 if (ua < ub)
12015 return -1;
12016 return ua > ub ? 1 : 0;
12017 }
12018
12019 /* Delete breakpoints by address or line. */
12020
12021 static void
12022 clear_command (char *arg, int from_tty)
12023 {
12024 struct breakpoint *b, *prev;
12025 VEC(breakpoint_p) *found = 0;
12026 int ix;
12027 int default_match;
12028 struct symtabs_and_lines sals;
12029 struct symtab_and_line sal;
12030 int i;
12031 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
12032
12033 if (arg)
12034 {
12035 sals = decode_line_with_current_source (arg,
12036 (DECODE_LINE_FUNFIRSTLINE
12037 | DECODE_LINE_LIST_MODE));
12038 make_cleanup (xfree, sals.sals);
12039 default_match = 0;
12040 }
12041 else
12042 {
12043 sals.sals = XNEW (struct symtab_and_line);
12044 make_cleanup (xfree, sals.sals);
12045 init_sal (&sal); /* Initialize to zeroes. */
12046
12047 /* Set sal's line, symtab, pc, and pspace to the values
12048 corresponding to the last call to print_frame_info. If the
12049 codepoint is not valid, this will set all the fields to 0. */
12050 get_last_displayed_sal (&sal);
12051 if (sal.symtab == 0)
12052 error (_("No source file specified."));
12053
12054 sals.sals[0] = sal;
12055 sals.nelts = 1;
12056
12057 default_match = 1;
12058 }
12059
12060 /* We don't call resolve_sal_pc here. That's not as bad as it
12061 seems, because all existing breakpoints typically have both
12062 file/line and pc set. So, if clear is given file/line, we can
12063 match this to existing breakpoint without obtaining pc at all.
12064
12065 We only support clearing given the address explicitly
12066 present in breakpoint table. Say, we've set breakpoint
12067 at file:line. There were several PC values for that file:line,
12068 due to optimization, all in one block.
12069
12070 We've picked one PC value. If "clear" is issued with another
12071 PC corresponding to the same file:line, the breakpoint won't
12072 be cleared. We probably can still clear the breakpoint, but
12073 since the other PC value is never presented to user, user
12074 can only find it by guessing, and it does not seem important
12075 to support that. */
12076
12077 /* For each line spec given, delete bps which correspond to it. Do
12078 it in two passes, solely to preserve the current behavior that
12079 from_tty is forced true if we delete more than one
12080 breakpoint. */
12081
12082 found = NULL;
12083 make_cleanup (VEC_cleanup (breakpoint_p), &found);
12084 for (i = 0; i < sals.nelts; i++)
12085 {
12086 const char *sal_fullname;
12087
12088 /* If exact pc given, clear bpts at that pc.
12089 If line given (pc == 0), clear all bpts on specified line.
12090 If defaulting, clear all bpts on default line
12091 or at default pc.
12092
12093 defaulting sal.pc != 0 tests to do
12094
12095 0 1 pc
12096 1 1 pc _and_ line
12097 0 0 line
12098 1 0 <can't happen> */
12099
12100 sal = sals.sals[i];
12101 sal_fullname = (sal.symtab == NULL
12102 ? NULL : symtab_to_fullname (sal.symtab));
12103
12104 /* Find all matching breakpoints and add them to 'found'. */
12105 ALL_BREAKPOINTS (b)
12106 {
12107 int match = 0;
12108 /* Are we going to delete b? */
12109 if (b->type != bp_none && !is_watchpoint (b))
12110 {
12111 struct bp_location *loc = b->loc;
12112 for (; loc; loc = loc->next)
12113 {
12114 /* If the user specified file:line, don't allow a PC
12115 match. This matches historical gdb behavior. */
12116 int pc_match = (!sal.explicit_line
12117 && sal.pc
12118 && (loc->pspace == sal.pspace)
12119 && (loc->address == sal.pc)
12120 && (!section_is_overlay (loc->section)
12121 || loc->section == sal.section));
12122 int line_match = 0;
12123
12124 if ((default_match || sal.explicit_line)
12125 && loc->symtab != NULL
12126 && sal_fullname != NULL
12127 && sal.pspace == loc->pspace
12128 && loc->line_number == sal.line
12129 && filename_cmp (symtab_to_fullname (loc->symtab),
12130 sal_fullname) == 0)
12131 line_match = 1;
12132
12133 if (pc_match || line_match)
12134 {
12135 match = 1;
12136 break;
12137 }
12138 }
12139 }
12140
12141 if (match)
12142 VEC_safe_push(breakpoint_p, found, b);
12143 }
12144 }
12145
12146 /* Now go thru the 'found' chain and delete them. */
12147 if (VEC_empty(breakpoint_p, found))
12148 {
12149 if (arg)
12150 error (_("No breakpoint at %s."), arg);
12151 else
12152 error (_("No breakpoint at this line."));
12153 }
12154
12155 /* Remove duplicates from the vec. */
12156 qsort (VEC_address (breakpoint_p, found),
12157 VEC_length (breakpoint_p, found),
12158 sizeof (breakpoint_p),
12159 compare_breakpoints);
12160 prev = VEC_index (breakpoint_p, found, 0);
12161 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
12162 {
12163 if (b == prev)
12164 {
12165 VEC_ordered_remove (breakpoint_p, found, ix);
12166 --ix;
12167 }
12168 }
12169
12170 if (VEC_length(breakpoint_p, found) > 1)
12171 from_tty = 1; /* Always report if deleted more than one. */
12172 if (from_tty)
12173 {
12174 if (VEC_length(breakpoint_p, found) == 1)
12175 printf_unfiltered (_("Deleted breakpoint "));
12176 else
12177 printf_unfiltered (_("Deleted breakpoints "));
12178 }
12179
12180 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
12181 {
12182 if (from_tty)
12183 printf_unfiltered ("%d ", b->number);
12184 delete_breakpoint (b);
12185 }
12186 if (from_tty)
12187 putchar_unfiltered ('\n');
12188
12189 do_cleanups (cleanups);
12190 }
12191 \f
12192 /* Delete breakpoint in BS if they are `delete' breakpoints and
12193 all breakpoints that are marked for deletion, whether hit or not.
12194 This is called after any breakpoint is hit, or after errors. */
12195
12196 void
12197 breakpoint_auto_delete (bpstat bs)
12198 {
12199 struct breakpoint *b, *b_tmp;
12200
12201 for (; bs; bs = bs->next)
12202 if (bs->breakpoint_at
12203 && bs->breakpoint_at->disposition == disp_del
12204 && bs->stop)
12205 delete_breakpoint (bs->breakpoint_at);
12206
12207 ALL_BREAKPOINTS_SAFE (b, b_tmp)
12208 {
12209 if (b->disposition == disp_del_at_next_stop)
12210 delete_breakpoint (b);
12211 }
12212 }
12213
12214 /* A comparison function for bp_location AP and BP being interfaced to
12215 qsort. Sort elements primarily by their ADDRESS (no matter what
12216 does breakpoint_address_is_meaningful say for its OWNER),
12217 secondarily by ordering first permanent elements and
12218 terciarily just ensuring the array is sorted stable way despite
12219 qsort being an unstable algorithm. */
12220
12221 static int
12222 bp_location_compare (const void *ap, const void *bp)
12223 {
12224 const struct bp_location *a = *(const struct bp_location **) ap;
12225 const struct bp_location *b = *(const struct bp_location **) bp;
12226
12227 if (a->address != b->address)
12228 return (a->address > b->address) - (a->address < b->address);
12229
12230 /* Sort locations at the same address by their pspace number, keeping
12231 locations of the same inferior (in a multi-inferior environment)
12232 grouped. */
12233
12234 if (a->pspace->num != b->pspace->num)
12235 return ((a->pspace->num > b->pspace->num)
12236 - (a->pspace->num < b->pspace->num));
12237
12238 /* Sort permanent breakpoints first. */
12239 if (a->permanent != b->permanent)
12240 return (a->permanent < b->permanent) - (a->permanent > b->permanent);
12241
12242 /* Make the internal GDB representation stable across GDB runs
12243 where A and B memory inside GDB can differ. Breakpoint locations of
12244 the same type at the same address can be sorted in arbitrary order. */
12245
12246 if (a->owner->number != b->owner->number)
12247 return ((a->owner->number > b->owner->number)
12248 - (a->owner->number < b->owner->number));
12249
12250 return (a > b) - (a < b);
12251 }
12252
12253 /* Set bp_location_placed_address_before_address_max and
12254 bp_location_shadow_len_after_address_max according to the current
12255 content of the bp_location array. */
12256
12257 static void
12258 bp_location_target_extensions_update (void)
12259 {
12260 struct bp_location *bl, **blp_tmp;
12261
12262 bp_location_placed_address_before_address_max = 0;
12263 bp_location_shadow_len_after_address_max = 0;
12264
12265 ALL_BP_LOCATIONS (bl, blp_tmp)
12266 {
12267 CORE_ADDR start, end, addr;
12268
12269 if (!bp_location_has_shadow (bl))
12270 continue;
12271
12272 start = bl->target_info.placed_address;
12273 end = start + bl->target_info.shadow_len;
12274
12275 gdb_assert (bl->address >= start);
12276 addr = bl->address - start;
12277 if (addr > bp_location_placed_address_before_address_max)
12278 bp_location_placed_address_before_address_max = addr;
12279
12280 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
12281
12282 gdb_assert (bl->address < end);
12283 addr = end - bl->address;
12284 if (addr > bp_location_shadow_len_after_address_max)
12285 bp_location_shadow_len_after_address_max = addr;
12286 }
12287 }
12288
12289 /* Download tracepoint locations if they haven't been. */
12290
12291 static void
12292 download_tracepoint_locations (void)
12293 {
12294 struct breakpoint *b;
12295 struct cleanup *old_chain;
12296 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
12297
12298 old_chain = save_current_space_and_thread ();
12299
12300 ALL_TRACEPOINTS (b)
12301 {
12302 struct bp_location *bl;
12303 struct tracepoint *t;
12304 int bp_location_downloaded = 0;
12305
12306 if ((b->type == bp_fast_tracepoint
12307 ? !may_insert_fast_tracepoints
12308 : !may_insert_tracepoints))
12309 continue;
12310
12311 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
12312 {
12313 if (target_can_download_tracepoint ())
12314 can_download_tracepoint = TRIBOOL_TRUE;
12315 else
12316 can_download_tracepoint = TRIBOOL_FALSE;
12317 }
12318
12319 if (can_download_tracepoint == TRIBOOL_FALSE)
12320 break;
12321
12322 for (bl = b->loc; bl; bl = bl->next)
12323 {
12324 /* In tracepoint, locations are _never_ duplicated, so
12325 should_be_inserted is equivalent to
12326 unduplicated_should_be_inserted. */
12327 if (!should_be_inserted (bl) || bl->inserted)
12328 continue;
12329
12330 switch_to_program_space_and_thread (bl->pspace);
12331
12332 target_download_tracepoint (bl);
12333
12334 bl->inserted = 1;
12335 bp_location_downloaded = 1;
12336 }
12337 t = (struct tracepoint *) b;
12338 t->number_on_target = b->number;
12339 if (bp_location_downloaded)
12340 observer_notify_breakpoint_modified (b);
12341 }
12342
12343 do_cleanups (old_chain);
12344 }
12345
12346 /* Swap the insertion/duplication state between two locations. */
12347
12348 static void
12349 swap_insertion (struct bp_location *left, struct bp_location *right)
12350 {
12351 const int left_inserted = left->inserted;
12352 const int left_duplicate = left->duplicate;
12353 const int left_needs_update = left->needs_update;
12354 const struct bp_target_info left_target_info = left->target_info;
12355
12356 /* Locations of tracepoints can never be duplicated. */
12357 if (is_tracepoint (left->owner))
12358 gdb_assert (!left->duplicate);
12359 if (is_tracepoint (right->owner))
12360 gdb_assert (!right->duplicate);
12361
12362 left->inserted = right->inserted;
12363 left->duplicate = right->duplicate;
12364 left->needs_update = right->needs_update;
12365 left->target_info = right->target_info;
12366 right->inserted = left_inserted;
12367 right->duplicate = left_duplicate;
12368 right->needs_update = left_needs_update;
12369 right->target_info = left_target_info;
12370 }
12371
12372 /* Force the re-insertion of the locations at ADDRESS. This is called
12373 once a new/deleted/modified duplicate location is found and we are evaluating
12374 conditions on the target's side. Such conditions need to be updated on
12375 the target. */
12376
12377 static void
12378 force_breakpoint_reinsertion (struct bp_location *bl)
12379 {
12380 struct bp_location **locp = NULL, **loc2p;
12381 struct bp_location *loc;
12382 CORE_ADDR address = 0;
12383 int pspace_num;
12384
12385 address = bl->address;
12386 pspace_num = bl->pspace->num;
12387
12388 /* This is only meaningful if the target is
12389 evaluating conditions and if the user has
12390 opted for condition evaluation on the target's
12391 side. */
12392 if (gdb_evaluates_breakpoint_condition_p ()
12393 || !target_supports_evaluation_of_breakpoint_conditions ())
12394 return;
12395
12396 /* Flag all breakpoint locations with this address and
12397 the same program space as the location
12398 as "its condition has changed". We need to
12399 update the conditions on the target's side. */
12400 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
12401 {
12402 loc = *loc2p;
12403
12404 if (!is_breakpoint (loc->owner)
12405 || pspace_num != loc->pspace->num)
12406 continue;
12407
12408 /* Flag the location appropriately. We use a different state to
12409 let everyone know that we already updated the set of locations
12410 with addr bl->address and program space bl->pspace. This is so
12411 we don't have to keep calling these functions just to mark locations
12412 that have already been marked. */
12413 loc->condition_changed = condition_updated;
12414
12415 /* Free the agent expression bytecode as well. We will compute
12416 it later on. */
12417 if (loc->cond_bytecode)
12418 {
12419 free_agent_expr (loc->cond_bytecode);
12420 loc->cond_bytecode = NULL;
12421 }
12422 }
12423 }
12424 /* Called whether new breakpoints are created, or existing breakpoints
12425 deleted, to update the global location list and recompute which
12426 locations are duplicate of which.
12427
12428 The INSERT_MODE flag determines whether locations may not, may, or
12429 shall be inserted now. See 'enum ugll_insert_mode' for more
12430 info. */
12431
12432 static void
12433 update_global_location_list (enum ugll_insert_mode insert_mode)
12434 {
12435 struct breakpoint *b;
12436 struct bp_location **locp, *loc;
12437 struct cleanup *cleanups;
12438 /* Last breakpoint location address that was marked for update. */
12439 CORE_ADDR last_addr = 0;
12440 /* Last breakpoint location program space that was marked for update. */
12441 int last_pspace_num = -1;
12442
12443 /* Used in the duplicates detection below. When iterating over all
12444 bp_locations, points to the first bp_location of a given address.
12445 Breakpoints and watchpoints of different types are never
12446 duplicates of each other. Keep one pointer for each type of
12447 breakpoint/watchpoint, so we only need to loop over all locations
12448 once. */
12449 struct bp_location *bp_loc_first; /* breakpoint */
12450 struct bp_location *wp_loc_first; /* hardware watchpoint */
12451 struct bp_location *awp_loc_first; /* access watchpoint */
12452 struct bp_location *rwp_loc_first; /* read watchpoint */
12453
12454 /* Saved former bp_location array which we compare against the newly
12455 built bp_location from the current state of ALL_BREAKPOINTS. */
12456 struct bp_location **old_location, **old_locp;
12457 unsigned old_location_count;
12458
12459 old_location = bp_location;
12460 old_location_count = bp_location_count;
12461 bp_location = NULL;
12462 bp_location_count = 0;
12463 cleanups = make_cleanup (xfree, old_location);
12464
12465 ALL_BREAKPOINTS (b)
12466 for (loc = b->loc; loc; loc = loc->next)
12467 bp_location_count++;
12468
12469 bp_location = XNEWVEC (struct bp_location *, bp_location_count);
12470 locp = bp_location;
12471 ALL_BREAKPOINTS (b)
12472 for (loc = b->loc; loc; loc = loc->next)
12473 *locp++ = loc;
12474 qsort (bp_location, bp_location_count, sizeof (*bp_location),
12475 bp_location_compare);
12476
12477 bp_location_target_extensions_update ();
12478
12479 /* Identify bp_location instances that are no longer present in the
12480 new list, and therefore should be freed. Note that it's not
12481 necessary that those locations should be removed from inferior --
12482 if there's another location at the same address (previously
12483 marked as duplicate), we don't need to remove/insert the
12484 location.
12485
12486 LOCP is kept in sync with OLD_LOCP, each pointing to the current
12487 and former bp_location array state respectively. */
12488
12489 locp = bp_location;
12490 for (old_locp = old_location; old_locp < old_location + old_location_count;
12491 old_locp++)
12492 {
12493 struct bp_location *old_loc = *old_locp;
12494 struct bp_location **loc2p;
12495
12496 /* Tells if 'old_loc' is found among the new locations. If
12497 not, we have to free it. */
12498 int found_object = 0;
12499 /* Tells if the location should remain inserted in the target. */
12500 int keep_in_target = 0;
12501 int removed = 0;
12502
12503 /* Skip LOCP entries which will definitely never be needed.
12504 Stop either at or being the one matching OLD_LOC. */
12505 while (locp < bp_location + bp_location_count
12506 && (*locp)->address < old_loc->address)
12507 locp++;
12508
12509 for (loc2p = locp;
12510 (loc2p < bp_location + bp_location_count
12511 && (*loc2p)->address == old_loc->address);
12512 loc2p++)
12513 {
12514 /* Check if this is a new/duplicated location or a duplicated
12515 location that had its condition modified. If so, we want to send
12516 its condition to the target if evaluation of conditions is taking
12517 place there. */
12518 if ((*loc2p)->condition_changed == condition_modified
12519 && (last_addr != old_loc->address
12520 || last_pspace_num != old_loc->pspace->num))
12521 {
12522 force_breakpoint_reinsertion (*loc2p);
12523 last_pspace_num = old_loc->pspace->num;
12524 }
12525
12526 if (*loc2p == old_loc)
12527 found_object = 1;
12528 }
12529
12530 /* We have already handled this address, update it so that we don't
12531 have to go through updates again. */
12532 last_addr = old_loc->address;
12533
12534 /* Target-side condition evaluation: Handle deleted locations. */
12535 if (!found_object)
12536 force_breakpoint_reinsertion (old_loc);
12537
12538 /* If this location is no longer present, and inserted, look if
12539 there's maybe a new location at the same address. If so,
12540 mark that one inserted, and don't remove this one. This is
12541 needed so that we don't have a time window where a breakpoint
12542 at certain location is not inserted. */
12543
12544 if (old_loc->inserted)
12545 {
12546 /* If the location is inserted now, we might have to remove
12547 it. */
12548
12549 if (found_object && should_be_inserted (old_loc))
12550 {
12551 /* The location is still present in the location list,
12552 and still should be inserted. Don't do anything. */
12553 keep_in_target = 1;
12554 }
12555 else
12556 {
12557 /* This location still exists, but it won't be kept in the
12558 target since it may have been disabled. We proceed to
12559 remove its target-side condition. */
12560
12561 /* The location is either no longer present, or got
12562 disabled. See if there's another location at the
12563 same address, in which case we don't need to remove
12564 this one from the target. */
12565
12566 /* OLD_LOC comes from existing struct breakpoint. */
12567 if (breakpoint_address_is_meaningful (old_loc->owner))
12568 {
12569 for (loc2p = locp;
12570 (loc2p < bp_location + bp_location_count
12571 && (*loc2p)->address == old_loc->address);
12572 loc2p++)
12573 {
12574 struct bp_location *loc2 = *loc2p;
12575
12576 if (breakpoint_locations_match (loc2, old_loc))
12577 {
12578 /* Read watchpoint locations are switched to
12579 access watchpoints, if the former are not
12580 supported, but the latter are. */
12581 if (is_hardware_watchpoint (old_loc->owner))
12582 {
12583 gdb_assert (is_hardware_watchpoint (loc2->owner));
12584 loc2->watchpoint_type = old_loc->watchpoint_type;
12585 }
12586
12587 /* loc2 is a duplicated location. We need to check
12588 if it should be inserted in case it will be
12589 unduplicated. */
12590 if (loc2 != old_loc
12591 && unduplicated_should_be_inserted (loc2))
12592 {
12593 swap_insertion (old_loc, loc2);
12594 keep_in_target = 1;
12595 break;
12596 }
12597 }
12598 }
12599 }
12600 }
12601
12602 if (!keep_in_target)
12603 {
12604 if (remove_breakpoint (old_loc, mark_uninserted))
12605 {
12606 /* This is just about all we can do. We could keep
12607 this location on the global list, and try to
12608 remove it next time, but there's no particular
12609 reason why we will succeed next time.
12610
12611 Note that at this point, old_loc->owner is still
12612 valid, as delete_breakpoint frees the breakpoint
12613 only after calling us. */
12614 printf_filtered (_("warning: Error removing "
12615 "breakpoint %d\n"),
12616 old_loc->owner->number);
12617 }
12618 removed = 1;
12619 }
12620 }
12621
12622 if (!found_object)
12623 {
12624 if (removed && target_is_non_stop_p ()
12625 && need_moribund_for_location_type (old_loc))
12626 {
12627 /* This location was removed from the target. In
12628 non-stop mode, a race condition is possible where
12629 we've removed a breakpoint, but stop events for that
12630 breakpoint are already queued and will arrive later.
12631 We apply an heuristic to be able to distinguish such
12632 SIGTRAPs from other random SIGTRAPs: we keep this
12633 breakpoint location for a bit, and will retire it
12634 after we see some number of events. The theory here
12635 is that reporting of events should, "on the average",
12636 be fair, so after a while we'll see events from all
12637 threads that have anything of interest, and no longer
12638 need to keep this breakpoint location around. We
12639 don't hold locations forever so to reduce chances of
12640 mistaking a non-breakpoint SIGTRAP for a breakpoint
12641 SIGTRAP.
12642
12643 The heuristic failing can be disastrous on
12644 decr_pc_after_break targets.
12645
12646 On decr_pc_after_break targets, like e.g., x86-linux,
12647 if we fail to recognize a late breakpoint SIGTRAP,
12648 because events_till_retirement has reached 0 too
12649 soon, we'll fail to do the PC adjustment, and report
12650 a random SIGTRAP to the user. When the user resumes
12651 the inferior, it will most likely immediately crash
12652 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
12653 corrupted, because of being resumed e.g., in the
12654 middle of a multi-byte instruction, or skipped a
12655 one-byte instruction. This was actually seen happen
12656 on native x86-linux, and should be less rare on
12657 targets that do not support new thread events, like
12658 remote, due to the heuristic depending on
12659 thread_count.
12660
12661 Mistaking a random SIGTRAP for a breakpoint trap
12662 causes similar symptoms (PC adjustment applied when
12663 it shouldn't), but then again, playing with SIGTRAPs
12664 behind the debugger's back is asking for trouble.
12665
12666 Since hardware watchpoint traps are always
12667 distinguishable from other traps, so we don't need to
12668 apply keep hardware watchpoint moribund locations
12669 around. We simply always ignore hardware watchpoint
12670 traps we can no longer explain. */
12671
12672 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12673 old_loc->owner = NULL;
12674
12675 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
12676 }
12677 else
12678 {
12679 old_loc->owner = NULL;
12680 decref_bp_location (&old_loc);
12681 }
12682 }
12683 }
12684
12685 /* Rescan breakpoints at the same address and section, marking the
12686 first one as "first" and any others as "duplicates". This is so
12687 that the bpt instruction is only inserted once. If we have a
12688 permanent breakpoint at the same place as BPT, make that one the
12689 official one, and the rest as duplicates. Permanent breakpoints
12690 are sorted first for the same address.
12691
12692 Do the same for hardware watchpoints, but also considering the
12693 watchpoint's type (regular/access/read) and length. */
12694
12695 bp_loc_first = NULL;
12696 wp_loc_first = NULL;
12697 awp_loc_first = NULL;
12698 rwp_loc_first = NULL;
12699 ALL_BP_LOCATIONS (loc, locp)
12700 {
12701 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12702 non-NULL. */
12703 struct bp_location **loc_first_p;
12704 b = loc->owner;
12705
12706 if (!unduplicated_should_be_inserted (loc)
12707 || !breakpoint_address_is_meaningful (b)
12708 /* Don't detect duplicate for tracepoint locations because they are
12709 never duplicated. See the comments in field `duplicate' of
12710 `struct bp_location'. */
12711 || is_tracepoint (b))
12712 {
12713 /* Clear the condition modification flag. */
12714 loc->condition_changed = condition_unchanged;
12715 continue;
12716 }
12717
12718 if (b->type == bp_hardware_watchpoint)
12719 loc_first_p = &wp_loc_first;
12720 else if (b->type == bp_read_watchpoint)
12721 loc_first_p = &rwp_loc_first;
12722 else if (b->type == bp_access_watchpoint)
12723 loc_first_p = &awp_loc_first;
12724 else
12725 loc_first_p = &bp_loc_first;
12726
12727 if (*loc_first_p == NULL
12728 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12729 || !breakpoint_locations_match (loc, *loc_first_p))
12730 {
12731 *loc_first_p = loc;
12732 loc->duplicate = 0;
12733
12734 if (is_breakpoint (loc->owner) && loc->condition_changed)
12735 {
12736 loc->needs_update = 1;
12737 /* Clear the condition modification flag. */
12738 loc->condition_changed = condition_unchanged;
12739 }
12740 continue;
12741 }
12742
12743
12744 /* This and the above ensure the invariant that the first location
12745 is not duplicated, and is the inserted one.
12746 All following are marked as duplicated, and are not inserted. */
12747 if (loc->inserted)
12748 swap_insertion (loc, *loc_first_p);
12749 loc->duplicate = 1;
12750
12751 /* Clear the condition modification flag. */
12752 loc->condition_changed = condition_unchanged;
12753 }
12754
12755 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
12756 {
12757 if (insert_mode != UGLL_DONT_INSERT)
12758 insert_breakpoint_locations ();
12759 else
12760 {
12761 /* Even though the caller told us to not insert new
12762 locations, we may still need to update conditions on the
12763 target's side of breakpoints that were already inserted
12764 if the target is evaluating breakpoint conditions. We
12765 only update conditions for locations that are marked
12766 "needs_update". */
12767 update_inserted_breakpoint_locations ();
12768 }
12769 }
12770
12771 if (insert_mode != UGLL_DONT_INSERT)
12772 download_tracepoint_locations ();
12773
12774 do_cleanups (cleanups);
12775 }
12776
12777 void
12778 breakpoint_retire_moribund (void)
12779 {
12780 struct bp_location *loc;
12781 int ix;
12782
12783 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12784 if (--(loc->events_till_retirement) == 0)
12785 {
12786 decref_bp_location (&loc);
12787 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12788 --ix;
12789 }
12790 }
12791
12792 static void
12793 update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
12794 {
12795
12796 TRY
12797 {
12798 update_global_location_list (insert_mode);
12799 }
12800 CATCH (e, RETURN_MASK_ERROR)
12801 {
12802 }
12803 END_CATCH
12804 }
12805
12806 /* Clear BKP from a BPS. */
12807
12808 static void
12809 bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12810 {
12811 bpstat bs;
12812
12813 for (bs = bps; bs; bs = bs->next)
12814 if (bs->breakpoint_at == bpt)
12815 {
12816 bs->breakpoint_at = NULL;
12817 bs->old_val = NULL;
12818 /* bs->commands will be freed later. */
12819 }
12820 }
12821
12822 /* Callback for iterate_over_threads. */
12823 static int
12824 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12825 {
12826 struct breakpoint *bpt = (struct breakpoint *) data;
12827
12828 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12829 return 0;
12830 }
12831
12832 /* Helper for breakpoint and tracepoint breakpoint_ops->mention
12833 callbacks. */
12834
12835 static void
12836 say_where (struct breakpoint *b)
12837 {
12838 struct value_print_options opts;
12839
12840 get_user_print_options (&opts);
12841
12842 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12843 single string. */
12844 if (b->loc == NULL)
12845 {
12846 /* For pending locations, the output differs slightly based
12847 on b->extra_string. If this is non-NULL, it contains either
12848 a condition or dprintf arguments. */
12849 if (b->extra_string == NULL)
12850 {
12851 printf_filtered (_(" (%s) pending."),
12852 event_location_to_string (b->location));
12853 }
12854 else if (b->type == bp_dprintf)
12855 {
12856 printf_filtered (_(" (%s,%s) pending."),
12857 event_location_to_string (b->location),
12858 b->extra_string);
12859 }
12860 else
12861 {
12862 printf_filtered (_(" (%s %s) pending."),
12863 event_location_to_string (b->location),
12864 b->extra_string);
12865 }
12866 }
12867 else
12868 {
12869 if (opts.addressprint || b->loc->symtab == NULL)
12870 {
12871 printf_filtered (" at ");
12872 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12873 gdb_stdout);
12874 }
12875 if (b->loc->symtab != NULL)
12876 {
12877 /* If there is a single location, we can print the location
12878 more nicely. */
12879 if (b->loc->next == NULL)
12880 printf_filtered (": file %s, line %d.",
12881 symtab_to_filename_for_display (b->loc->symtab),
12882 b->loc->line_number);
12883 else
12884 /* This is not ideal, but each location may have a
12885 different file name, and this at least reflects the
12886 real situation somewhat. */
12887 printf_filtered (": %s.",
12888 event_location_to_string (b->location));
12889 }
12890
12891 if (b->loc->next)
12892 {
12893 struct bp_location *loc = b->loc;
12894 int n = 0;
12895 for (; loc; loc = loc->next)
12896 ++n;
12897 printf_filtered (" (%d locations)", n);
12898 }
12899 }
12900 }
12901
12902 /* Default bp_location_ops methods. */
12903
12904 static void
12905 bp_location_dtor (struct bp_location *self)
12906 {
12907 xfree (self->cond);
12908 if (self->cond_bytecode)
12909 free_agent_expr (self->cond_bytecode);
12910 xfree (self->function_name);
12911
12912 VEC_free (agent_expr_p, self->target_info.conditions);
12913 VEC_free (agent_expr_p, self->target_info.tcommands);
12914 }
12915
12916 static const struct bp_location_ops bp_location_ops =
12917 {
12918 bp_location_dtor
12919 };
12920
12921 /* Default breakpoint_ops methods all breakpoint_ops ultimately
12922 inherit from. */
12923
12924 static void
12925 base_breakpoint_dtor (struct breakpoint *self)
12926 {
12927 decref_counted_command_line (&self->commands);
12928 xfree (self->cond_string);
12929 xfree (self->extra_string);
12930 xfree (self->filter);
12931 delete_event_location (self->location);
12932 delete_event_location (self->location_range_end);
12933 }
12934
12935 static struct bp_location *
12936 base_breakpoint_allocate_location (struct breakpoint *self)
12937 {
12938 struct bp_location *loc;
12939
12940 loc = XNEW (struct bp_location);
12941 init_bp_location (loc, &bp_location_ops, self);
12942 return loc;
12943 }
12944
12945 static void
12946 base_breakpoint_re_set (struct breakpoint *b)
12947 {
12948 /* Nothing to re-set. */
12949 }
12950
12951 #define internal_error_pure_virtual_called() \
12952 gdb_assert_not_reached ("pure virtual function called")
12953
12954 static int
12955 base_breakpoint_insert_location (struct bp_location *bl)
12956 {
12957 internal_error_pure_virtual_called ();
12958 }
12959
12960 static int
12961 base_breakpoint_remove_location (struct bp_location *bl)
12962 {
12963 internal_error_pure_virtual_called ();
12964 }
12965
12966 static int
12967 base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12968 struct address_space *aspace,
12969 CORE_ADDR bp_addr,
12970 const struct target_waitstatus *ws)
12971 {
12972 internal_error_pure_virtual_called ();
12973 }
12974
12975 static void
12976 base_breakpoint_check_status (bpstat bs)
12977 {
12978 /* Always stop. */
12979 }
12980
12981 /* A "works_in_software_mode" breakpoint_ops method that just internal
12982 errors. */
12983
12984 static int
12985 base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12986 {
12987 internal_error_pure_virtual_called ();
12988 }
12989
12990 /* A "resources_needed" breakpoint_ops method that just internal
12991 errors. */
12992
12993 static int
12994 base_breakpoint_resources_needed (const struct bp_location *bl)
12995 {
12996 internal_error_pure_virtual_called ();
12997 }
12998
12999 static enum print_stop_action
13000 base_breakpoint_print_it (bpstat bs)
13001 {
13002 internal_error_pure_virtual_called ();
13003 }
13004
13005 static void
13006 base_breakpoint_print_one_detail (const struct breakpoint *self,
13007 struct ui_out *uiout)
13008 {
13009 /* nothing */
13010 }
13011
13012 static void
13013 base_breakpoint_print_mention (struct breakpoint *b)
13014 {
13015 internal_error_pure_virtual_called ();
13016 }
13017
13018 static void
13019 base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
13020 {
13021 internal_error_pure_virtual_called ();
13022 }
13023
13024 static void
13025 base_breakpoint_create_sals_from_location
13026 (const struct event_location *location,
13027 struct linespec_result *canonical,
13028 enum bptype type_wanted)
13029 {
13030 internal_error_pure_virtual_called ();
13031 }
13032
13033 static void
13034 base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13035 struct linespec_result *c,
13036 char *cond_string,
13037 char *extra_string,
13038 enum bptype type_wanted,
13039 enum bpdisp disposition,
13040 int thread,
13041 int task, int ignore_count,
13042 const struct breakpoint_ops *o,
13043 int from_tty, int enabled,
13044 int internal, unsigned flags)
13045 {
13046 internal_error_pure_virtual_called ();
13047 }
13048
13049 static void
13050 base_breakpoint_decode_location (struct breakpoint *b,
13051 const struct event_location *location,
13052 struct program_space *search_pspace,
13053 struct symtabs_and_lines *sals)
13054 {
13055 internal_error_pure_virtual_called ();
13056 }
13057
13058 /* The default 'explains_signal' method. */
13059
13060 static int
13061 base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
13062 {
13063 return 1;
13064 }
13065
13066 /* The default "after_condition_true" method. */
13067
13068 static void
13069 base_breakpoint_after_condition_true (struct bpstats *bs)
13070 {
13071 /* Nothing to do. */
13072 }
13073
13074 struct breakpoint_ops base_breakpoint_ops =
13075 {
13076 base_breakpoint_dtor,
13077 base_breakpoint_allocate_location,
13078 base_breakpoint_re_set,
13079 base_breakpoint_insert_location,
13080 base_breakpoint_remove_location,
13081 base_breakpoint_breakpoint_hit,
13082 base_breakpoint_check_status,
13083 base_breakpoint_resources_needed,
13084 base_breakpoint_works_in_software_mode,
13085 base_breakpoint_print_it,
13086 NULL,
13087 base_breakpoint_print_one_detail,
13088 base_breakpoint_print_mention,
13089 base_breakpoint_print_recreate,
13090 base_breakpoint_create_sals_from_location,
13091 base_breakpoint_create_breakpoints_sal,
13092 base_breakpoint_decode_location,
13093 base_breakpoint_explains_signal,
13094 base_breakpoint_after_condition_true,
13095 };
13096
13097 /* Default breakpoint_ops methods. */
13098
13099 static void
13100 bkpt_re_set (struct breakpoint *b)
13101 {
13102 /* FIXME: is this still reachable? */
13103 if (breakpoint_event_location_empty_p (b))
13104 {
13105 /* Anything without a location can't be re-set. */
13106 delete_breakpoint (b);
13107 return;
13108 }
13109
13110 breakpoint_re_set_default (b);
13111 }
13112
13113 static int
13114 bkpt_insert_location (struct bp_location *bl)
13115 {
13116 if (bl->loc_type == bp_loc_hardware_breakpoint)
13117 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
13118 else
13119 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
13120 }
13121
13122 static int
13123 bkpt_remove_location (struct bp_location *bl)
13124 {
13125 if (bl->loc_type == bp_loc_hardware_breakpoint)
13126 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
13127 else
13128 return target_remove_breakpoint (bl->gdbarch, &bl->target_info);
13129 }
13130
13131 static int
13132 bkpt_breakpoint_hit (const struct bp_location *bl,
13133 struct address_space *aspace, CORE_ADDR bp_addr,
13134 const struct target_waitstatus *ws)
13135 {
13136 if (ws->kind != TARGET_WAITKIND_STOPPED
13137 || ws->value.sig != GDB_SIGNAL_TRAP)
13138 return 0;
13139
13140 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
13141 aspace, bp_addr))
13142 return 0;
13143
13144 if (overlay_debugging /* unmapped overlay section */
13145 && section_is_overlay (bl->section)
13146 && !section_is_mapped (bl->section))
13147 return 0;
13148
13149 return 1;
13150 }
13151
13152 static int
13153 dprintf_breakpoint_hit (const struct bp_location *bl,
13154 struct address_space *aspace, CORE_ADDR bp_addr,
13155 const struct target_waitstatus *ws)
13156 {
13157 if (dprintf_style == dprintf_style_agent
13158 && target_can_run_breakpoint_commands ())
13159 {
13160 /* An agent-style dprintf never causes a stop. If we see a trap
13161 for this address it must be for a breakpoint that happens to
13162 be set at the same address. */
13163 return 0;
13164 }
13165
13166 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
13167 }
13168
13169 static int
13170 bkpt_resources_needed (const struct bp_location *bl)
13171 {
13172 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
13173
13174 return 1;
13175 }
13176
13177 static enum print_stop_action
13178 bkpt_print_it (bpstat bs)
13179 {
13180 struct breakpoint *b;
13181 const struct bp_location *bl;
13182 int bp_temp;
13183 struct ui_out *uiout = current_uiout;
13184
13185 gdb_assert (bs->bp_location_at != NULL);
13186
13187 bl = bs->bp_location_at;
13188 b = bs->breakpoint_at;
13189
13190 bp_temp = b->disposition == disp_del;
13191 if (bl->address != bl->requested_address)
13192 breakpoint_adjustment_warning (bl->requested_address,
13193 bl->address,
13194 b->number, 1);
13195 annotate_breakpoint (b->number);
13196 maybe_print_thread_hit_breakpoint (uiout);
13197
13198 if (bp_temp)
13199 ui_out_text (uiout, "Temporary breakpoint ");
13200 else
13201 ui_out_text (uiout, "Breakpoint ");
13202 if (ui_out_is_mi_like_p (uiout))
13203 {
13204 ui_out_field_string (uiout, "reason",
13205 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
13206 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
13207 }
13208 ui_out_field_int (uiout, "bkptno", b->number);
13209 ui_out_text (uiout, ", ");
13210
13211 return PRINT_SRC_AND_LOC;
13212 }
13213
13214 static void
13215 bkpt_print_mention (struct breakpoint *b)
13216 {
13217 if (ui_out_is_mi_like_p (current_uiout))
13218 return;
13219
13220 switch (b->type)
13221 {
13222 case bp_breakpoint:
13223 case bp_gnu_ifunc_resolver:
13224 if (b->disposition == disp_del)
13225 printf_filtered (_("Temporary breakpoint"));
13226 else
13227 printf_filtered (_("Breakpoint"));
13228 printf_filtered (_(" %d"), b->number);
13229 if (b->type == bp_gnu_ifunc_resolver)
13230 printf_filtered (_(" at gnu-indirect-function resolver"));
13231 break;
13232 case bp_hardware_breakpoint:
13233 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
13234 break;
13235 case bp_dprintf:
13236 printf_filtered (_("Dprintf %d"), b->number);
13237 break;
13238 }
13239
13240 say_where (b);
13241 }
13242
13243 static void
13244 bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13245 {
13246 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
13247 fprintf_unfiltered (fp, "tbreak");
13248 else if (tp->type == bp_breakpoint)
13249 fprintf_unfiltered (fp, "break");
13250 else if (tp->type == bp_hardware_breakpoint
13251 && tp->disposition == disp_del)
13252 fprintf_unfiltered (fp, "thbreak");
13253 else if (tp->type == bp_hardware_breakpoint)
13254 fprintf_unfiltered (fp, "hbreak");
13255 else
13256 internal_error (__FILE__, __LINE__,
13257 _("unhandled breakpoint type %d"), (int) tp->type);
13258
13259 fprintf_unfiltered (fp, " %s",
13260 event_location_to_string (tp->location));
13261
13262 /* Print out extra_string if this breakpoint is pending. It might
13263 contain, for example, conditions that were set by the user. */
13264 if (tp->loc == NULL && tp->extra_string != NULL)
13265 fprintf_unfiltered (fp, " %s", tp->extra_string);
13266
13267 print_recreate_thread (tp, fp);
13268 }
13269
13270 static void
13271 bkpt_create_sals_from_location (const struct event_location *location,
13272 struct linespec_result *canonical,
13273 enum bptype type_wanted)
13274 {
13275 create_sals_from_location_default (location, canonical, type_wanted);
13276 }
13277
13278 static void
13279 bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
13280 struct linespec_result *canonical,
13281 char *cond_string,
13282 char *extra_string,
13283 enum bptype type_wanted,
13284 enum bpdisp disposition,
13285 int thread,
13286 int task, int ignore_count,
13287 const struct breakpoint_ops *ops,
13288 int from_tty, int enabled,
13289 int internal, unsigned flags)
13290 {
13291 create_breakpoints_sal_default (gdbarch, canonical,
13292 cond_string, extra_string,
13293 type_wanted,
13294 disposition, thread, task,
13295 ignore_count, ops, from_tty,
13296 enabled, internal, flags);
13297 }
13298
13299 static void
13300 bkpt_decode_location (struct breakpoint *b,
13301 const struct event_location *location,
13302 struct program_space *search_pspace,
13303 struct symtabs_and_lines *sals)
13304 {
13305 decode_location_default (b, location, search_pspace, sals);
13306 }
13307
13308 /* Virtual table for internal breakpoints. */
13309
13310 static void
13311 internal_bkpt_re_set (struct breakpoint *b)
13312 {
13313 switch (b->type)
13314 {
13315 /* Delete overlay event and longjmp master breakpoints; they
13316 will be reset later by breakpoint_re_set. */
13317 case bp_overlay_event:
13318 case bp_longjmp_master:
13319 case bp_std_terminate_master:
13320 case bp_exception_master:
13321 delete_breakpoint (b);
13322 break;
13323
13324 /* This breakpoint is special, it's set up when the inferior
13325 starts and we really don't want to touch it. */
13326 case bp_shlib_event:
13327
13328 /* Like bp_shlib_event, this breakpoint type is special. Once
13329 it is set up, we do not want to touch it. */
13330 case bp_thread_event:
13331 break;
13332 }
13333 }
13334
13335 static void
13336 internal_bkpt_check_status (bpstat bs)
13337 {
13338 if (bs->breakpoint_at->type == bp_shlib_event)
13339 {
13340 /* If requested, stop when the dynamic linker notifies GDB of
13341 events. This allows the user to get control and place
13342 breakpoints in initializer routines for dynamically loaded
13343 objects (among other things). */
13344 bs->stop = stop_on_solib_events;
13345 bs->print = stop_on_solib_events;
13346 }
13347 else
13348 bs->stop = 0;
13349 }
13350
13351 static enum print_stop_action
13352 internal_bkpt_print_it (bpstat bs)
13353 {
13354 struct breakpoint *b;
13355
13356 b = bs->breakpoint_at;
13357
13358 switch (b->type)
13359 {
13360 case bp_shlib_event:
13361 /* Did we stop because the user set the stop_on_solib_events
13362 variable? (If so, we report this as a generic, "Stopped due
13363 to shlib event" message.) */
13364 print_solib_event (0);
13365 break;
13366
13367 case bp_thread_event:
13368 /* Not sure how we will get here.
13369 GDB should not stop for these breakpoints. */
13370 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
13371 break;
13372
13373 case bp_overlay_event:
13374 /* By analogy with the thread event, GDB should not stop for these. */
13375 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
13376 break;
13377
13378 case bp_longjmp_master:
13379 /* These should never be enabled. */
13380 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
13381 break;
13382
13383 case bp_std_terminate_master:
13384 /* These should never be enabled. */
13385 printf_filtered (_("std::terminate Master Breakpoint: "
13386 "gdb should not stop!\n"));
13387 break;
13388
13389 case bp_exception_master:
13390 /* These should never be enabled. */
13391 printf_filtered (_("Exception Master Breakpoint: "
13392 "gdb should not stop!\n"));
13393 break;
13394 }
13395
13396 return PRINT_NOTHING;
13397 }
13398
13399 static void
13400 internal_bkpt_print_mention (struct breakpoint *b)
13401 {
13402 /* Nothing to mention. These breakpoints are internal. */
13403 }
13404
13405 /* Virtual table for momentary breakpoints */
13406
13407 static void
13408 momentary_bkpt_re_set (struct breakpoint *b)
13409 {
13410 /* Keep temporary breakpoints, which can be encountered when we step
13411 over a dlopen call and solib_add is resetting the breakpoints.
13412 Otherwise these should have been blown away via the cleanup chain
13413 or by breakpoint_init_inferior when we rerun the executable. */
13414 }
13415
13416 static void
13417 momentary_bkpt_check_status (bpstat bs)
13418 {
13419 /* Nothing. The point of these breakpoints is causing a stop. */
13420 }
13421
13422 static enum print_stop_action
13423 momentary_bkpt_print_it (bpstat bs)
13424 {
13425 return PRINT_UNKNOWN;
13426 }
13427
13428 static void
13429 momentary_bkpt_print_mention (struct breakpoint *b)
13430 {
13431 /* Nothing to mention. These breakpoints are internal. */
13432 }
13433
13434 /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
13435
13436 It gets cleared already on the removal of the first one of such placed
13437 breakpoints. This is OK as they get all removed altogether. */
13438
13439 static void
13440 longjmp_bkpt_dtor (struct breakpoint *self)
13441 {
13442 struct thread_info *tp = find_thread_global_id (self->thread);
13443
13444 if (tp)
13445 tp->initiating_frame = null_frame_id;
13446
13447 momentary_breakpoint_ops.dtor (self);
13448 }
13449
13450 /* Specific methods for probe breakpoints. */
13451
13452 static int
13453 bkpt_probe_insert_location (struct bp_location *bl)
13454 {
13455 int v = bkpt_insert_location (bl);
13456
13457 if (v == 0)
13458 {
13459 /* The insertion was successful, now let's set the probe's semaphore
13460 if needed. */
13461 if (bl->probe.probe->pops->set_semaphore != NULL)
13462 bl->probe.probe->pops->set_semaphore (bl->probe.probe,
13463 bl->probe.objfile,
13464 bl->gdbarch);
13465 }
13466
13467 return v;
13468 }
13469
13470 static int
13471 bkpt_probe_remove_location (struct bp_location *bl)
13472 {
13473 /* Let's clear the semaphore before removing the location. */
13474 if (bl->probe.probe->pops->clear_semaphore != NULL)
13475 bl->probe.probe->pops->clear_semaphore (bl->probe.probe,
13476 bl->probe.objfile,
13477 bl->gdbarch);
13478
13479 return bkpt_remove_location (bl);
13480 }
13481
13482 static void
13483 bkpt_probe_create_sals_from_location (const struct event_location *location,
13484 struct linespec_result *canonical,
13485 enum bptype type_wanted)
13486 {
13487 struct linespec_sals lsal;
13488
13489 lsal.sals = parse_probes (location, NULL, canonical);
13490 lsal.canonical = xstrdup (event_location_to_string (canonical->location));
13491 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13492 }
13493
13494 static void
13495 bkpt_probe_decode_location (struct breakpoint *b,
13496 const struct event_location *location,
13497 struct program_space *search_pspace,
13498 struct symtabs_and_lines *sals)
13499 {
13500 *sals = parse_probes (location, search_pspace, NULL);
13501 if (!sals->sals)
13502 error (_("probe not found"));
13503 }
13504
13505 /* The breakpoint_ops structure to be used in tracepoints. */
13506
13507 static void
13508 tracepoint_re_set (struct breakpoint *b)
13509 {
13510 breakpoint_re_set_default (b);
13511 }
13512
13513 static int
13514 tracepoint_breakpoint_hit (const struct bp_location *bl,
13515 struct address_space *aspace, CORE_ADDR bp_addr,
13516 const struct target_waitstatus *ws)
13517 {
13518 /* By definition, the inferior does not report stops at
13519 tracepoints. */
13520 return 0;
13521 }
13522
13523 static void
13524 tracepoint_print_one_detail (const struct breakpoint *self,
13525 struct ui_out *uiout)
13526 {
13527 struct tracepoint *tp = (struct tracepoint *) self;
13528 if (tp->static_trace_marker_id)
13529 {
13530 gdb_assert (self->type == bp_static_tracepoint);
13531
13532 ui_out_text (uiout, "\tmarker id is ");
13533 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
13534 tp->static_trace_marker_id);
13535 ui_out_text (uiout, "\n");
13536 }
13537 }
13538
13539 static void
13540 tracepoint_print_mention (struct breakpoint *b)
13541 {
13542 if (ui_out_is_mi_like_p (current_uiout))
13543 return;
13544
13545 switch (b->type)
13546 {
13547 case bp_tracepoint:
13548 printf_filtered (_("Tracepoint"));
13549 printf_filtered (_(" %d"), b->number);
13550 break;
13551 case bp_fast_tracepoint:
13552 printf_filtered (_("Fast tracepoint"));
13553 printf_filtered (_(" %d"), b->number);
13554 break;
13555 case bp_static_tracepoint:
13556 printf_filtered (_("Static tracepoint"));
13557 printf_filtered (_(" %d"), b->number);
13558 break;
13559 default:
13560 internal_error (__FILE__, __LINE__,
13561 _("unhandled tracepoint type %d"), (int) b->type);
13562 }
13563
13564 say_where (b);
13565 }
13566
13567 static void
13568 tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
13569 {
13570 struct tracepoint *tp = (struct tracepoint *) self;
13571
13572 if (self->type == bp_fast_tracepoint)
13573 fprintf_unfiltered (fp, "ftrace");
13574 else if (self->type == bp_static_tracepoint)
13575 fprintf_unfiltered (fp, "strace");
13576 else if (self->type == bp_tracepoint)
13577 fprintf_unfiltered (fp, "trace");
13578 else
13579 internal_error (__FILE__, __LINE__,
13580 _("unhandled tracepoint type %d"), (int) self->type);
13581
13582 fprintf_unfiltered (fp, " %s",
13583 event_location_to_string (self->location));
13584 print_recreate_thread (self, fp);
13585
13586 if (tp->pass_count)
13587 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
13588 }
13589
13590 static void
13591 tracepoint_create_sals_from_location (const struct event_location *location,
13592 struct linespec_result *canonical,
13593 enum bptype type_wanted)
13594 {
13595 create_sals_from_location_default (location, canonical, type_wanted);
13596 }
13597
13598 static void
13599 tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13600 struct linespec_result *canonical,
13601 char *cond_string,
13602 char *extra_string,
13603 enum bptype type_wanted,
13604 enum bpdisp disposition,
13605 int thread,
13606 int task, int ignore_count,
13607 const struct breakpoint_ops *ops,
13608 int from_tty, int enabled,
13609 int internal, unsigned flags)
13610 {
13611 create_breakpoints_sal_default (gdbarch, canonical,
13612 cond_string, extra_string,
13613 type_wanted,
13614 disposition, thread, task,
13615 ignore_count, ops, from_tty,
13616 enabled, internal, flags);
13617 }
13618
13619 static void
13620 tracepoint_decode_location (struct breakpoint *b,
13621 const struct event_location *location,
13622 struct program_space *search_pspace,
13623 struct symtabs_and_lines *sals)
13624 {
13625 decode_location_default (b, location, search_pspace, sals);
13626 }
13627
13628 struct breakpoint_ops tracepoint_breakpoint_ops;
13629
13630 /* The breakpoint_ops structure to be use on tracepoints placed in a
13631 static probe. */
13632
13633 static void
13634 tracepoint_probe_create_sals_from_location
13635 (const struct event_location *location,
13636 struct linespec_result *canonical,
13637 enum bptype type_wanted)
13638 {
13639 /* We use the same method for breakpoint on probes. */
13640 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
13641 }
13642
13643 static void
13644 tracepoint_probe_decode_location (struct breakpoint *b,
13645 const struct event_location *location,
13646 struct program_space *search_pspace,
13647 struct symtabs_and_lines *sals)
13648 {
13649 /* We use the same method for breakpoint on probes. */
13650 bkpt_probe_decode_location (b, location, search_pspace, sals);
13651 }
13652
13653 static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13654
13655 /* Dprintf breakpoint_ops methods. */
13656
13657 static void
13658 dprintf_re_set (struct breakpoint *b)
13659 {
13660 breakpoint_re_set_default (b);
13661
13662 /* extra_string should never be non-NULL for dprintf. */
13663 gdb_assert (b->extra_string != NULL);
13664
13665 /* 1 - connect to target 1, that can run breakpoint commands.
13666 2 - create a dprintf, which resolves fine.
13667 3 - disconnect from target 1
13668 4 - connect to target 2, that can NOT run breakpoint commands.
13669
13670 After steps #3/#4, you'll want the dprintf command list to
13671 be updated, because target 1 and 2 may well return different
13672 answers for target_can_run_breakpoint_commands().
13673 Given absence of finer grained resetting, we get to do
13674 it all the time. */
13675 if (b->extra_string != NULL)
13676 update_dprintf_command_list (b);
13677 }
13678
13679 /* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13680
13681 static void
13682 dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13683 {
13684 fprintf_unfiltered (fp, "dprintf %s,%s",
13685 event_location_to_string (tp->location),
13686 tp->extra_string);
13687 print_recreate_thread (tp, fp);
13688 }
13689
13690 /* Implement the "after_condition_true" breakpoint_ops method for
13691 dprintf.
13692
13693 dprintf's are implemented with regular commands in their command
13694 list, but we run the commands here instead of before presenting the
13695 stop to the user, as dprintf's don't actually cause a stop. This
13696 also makes it so that the commands of multiple dprintfs at the same
13697 address are all handled. */
13698
13699 static void
13700 dprintf_after_condition_true (struct bpstats *bs)
13701 {
13702 struct cleanup *old_chain;
13703 struct bpstats tmp_bs = { NULL };
13704 struct bpstats *tmp_bs_p = &tmp_bs;
13705
13706 /* dprintf's never cause a stop. This wasn't set in the
13707 check_status hook instead because that would make the dprintf's
13708 condition not be evaluated. */
13709 bs->stop = 0;
13710
13711 /* Run the command list here. Take ownership of it instead of
13712 copying. We never want these commands to run later in
13713 bpstat_do_actions, if a breakpoint that causes a stop happens to
13714 be set at same address as this dprintf, or even if running the
13715 commands here throws. */
13716 tmp_bs.commands = bs->commands;
13717 bs->commands = NULL;
13718 old_chain = make_cleanup_decref_counted_command_line (&tmp_bs.commands);
13719
13720 bpstat_do_actions_1 (&tmp_bs_p);
13721
13722 /* 'tmp_bs.commands' will usually be NULL by now, but
13723 bpstat_do_actions_1 may return early without processing the whole
13724 list. */
13725 do_cleanups (old_chain);
13726 }
13727
13728 /* The breakpoint_ops structure to be used on static tracepoints with
13729 markers (`-m'). */
13730
13731 static void
13732 strace_marker_create_sals_from_location (const struct event_location *location,
13733 struct linespec_result *canonical,
13734 enum bptype type_wanted)
13735 {
13736 struct linespec_sals lsal;
13737 const char *arg_start, *arg;
13738 char *str;
13739 struct cleanup *cleanup;
13740
13741 arg = arg_start = get_linespec_location (location);
13742 lsal.sals = decode_static_tracepoint_spec (&arg);
13743
13744 str = savestring (arg_start, arg - arg_start);
13745 cleanup = make_cleanup (xfree, str);
13746 canonical->location = new_linespec_location (&str);
13747 do_cleanups (cleanup);
13748
13749 lsal.canonical = xstrdup (event_location_to_string (canonical->location));
13750 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13751 }
13752
13753 static void
13754 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13755 struct linespec_result *canonical,
13756 char *cond_string,
13757 char *extra_string,
13758 enum bptype type_wanted,
13759 enum bpdisp disposition,
13760 int thread,
13761 int task, int ignore_count,
13762 const struct breakpoint_ops *ops,
13763 int from_tty, int enabled,
13764 int internal, unsigned flags)
13765 {
13766 int i;
13767 struct linespec_sals *lsal = VEC_index (linespec_sals,
13768 canonical->sals, 0);
13769
13770 /* If the user is creating a static tracepoint by marker id
13771 (strace -m MARKER_ID), then store the sals index, so that
13772 breakpoint_re_set can try to match up which of the newly
13773 found markers corresponds to this one, and, don't try to
13774 expand multiple locations for each sal, given than SALS
13775 already should contain all sals for MARKER_ID. */
13776
13777 for (i = 0; i < lsal->sals.nelts; ++i)
13778 {
13779 struct symtabs_and_lines expanded;
13780 struct tracepoint *tp;
13781 struct cleanup *old_chain;
13782 struct event_location *location;
13783
13784 expanded.nelts = 1;
13785 expanded.sals = &lsal->sals.sals[i];
13786
13787 location = copy_event_location (canonical->location);
13788 old_chain = make_cleanup_delete_event_location (location);
13789
13790 tp = XCNEW (struct tracepoint);
13791 init_breakpoint_sal (&tp->base, gdbarch, expanded,
13792 location, NULL,
13793 cond_string, extra_string,
13794 type_wanted, disposition,
13795 thread, task, ignore_count, ops,
13796 from_tty, enabled, internal, flags,
13797 canonical->special_display);
13798 /* Given that its possible to have multiple markers with
13799 the same string id, if the user is creating a static
13800 tracepoint by marker id ("strace -m MARKER_ID"), then
13801 store the sals index, so that breakpoint_re_set can
13802 try to match up which of the newly found markers
13803 corresponds to this one */
13804 tp->static_trace_marker_id_idx = i;
13805
13806 install_breakpoint (internal, &tp->base, 0);
13807
13808 discard_cleanups (old_chain);
13809 }
13810 }
13811
13812 static void
13813 strace_marker_decode_location (struct breakpoint *b,
13814 const struct event_location *location,
13815 struct program_space *search_pspace,
13816 struct symtabs_and_lines *sals)
13817 {
13818 struct tracepoint *tp = (struct tracepoint *) b;
13819 const char *s = get_linespec_location (location);
13820
13821 *sals = decode_static_tracepoint_spec (&s);
13822 if (sals->nelts > tp->static_trace_marker_id_idx)
13823 {
13824 sals->sals[0] = sals->sals[tp->static_trace_marker_id_idx];
13825 sals->nelts = 1;
13826 }
13827 else
13828 error (_("marker %s not found"), tp->static_trace_marker_id);
13829 }
13830
13831 static struct breakpoint_ops strace_marker_breakpoint_ops;
13832
13833 static int
13834 strace_marker_p (struct breakpoint *b)
13835 {
13836 return b->ops == &strace_marker_breakpoint_ops;
13837 }
13838
13839 /* Delete a breakpoint and clean up all traces of it in the data
13840 structures. */
13841
13842 void
13843 delete_breakpoint (struct breakpoint *bpt)
13844 {
13845 struct breakpoint *b;
13846
13847 gdb_assert (bpt != NULL);
13848
13849 /* Has this bp already been deleted? This can happen because
13850 multiple lists can hold pointers to bp's. bpstat lists are
13851 especial culprits.
13852
13853 One example of this happening is a watchpoint's scope bp. When
13854 the scope bp triggers, we notice that the watchpoint is out of
13855 scope, and delete it. We also delete its scope bp. But the
13856 scope bp is marked "auto-deleting", and is already on a bpstat.
13857 That bpstat is then checked for auto-deleting bp's, which are
13858 deleted.
13859
13860 A real solution to this problem might involve reference counts in
13861 bp's, and/or giving them pointers back to their referencing
13862 bpstat's, and teaching delete_breakpoint to only free a bp's
13863 storage when no more references were extent. A cheaper bandaid
13864 was chosen. */
13865 if (bpt->type == bp_none)
13866 return;
13867
13868 /* At least avoid this stale reference until the reference counting
13869 of breakpoints gets resolved. */
13870 if (bpt->related_breakpoint != bpt)
13871 {
13872 struct breakpoint *related;
13873 struct watchpoint *w;
13874
13875 if (bpt->type == bp_watchpoint_scope)
13876 w = (struct watchpoint *) bpt->related_breakpoint;
13877 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
13878 w = (struct watchpoint *) bpt;
13879 else
13880 w = NULL;
13881 if (w != NULL)
13882 watchpoint_del_at_next_stop (w);
13883
13884 /* Unlink bpt from the bpt->related_breakpoint ring. */
13885 for (related = bpt; related->related_breakpoint != bpt;
13886 related = related->related_breakpoint);
13887 related->related_breakpoint = bpt->related_breakpoint;
13888 bpt->related_breakpoint = bpt;
13889 }
13890
13891 /* watch_command_1 creates a watchpoint but only sets its number if
13892 update_watchpoint succeeds in creating its bp_locations. If there's
13893 a problem in that process, we'll be asked to delete the half-created
13894 watchpoint. In that case, don't announce the deletion. */
13895 if (bpt->number)
13896 observer_notify_breakpoint_deleted (bpt);
13897
13898 if (breakpoint_chain == bpt)
13899 breakpoint_chain = bpt->next;
13900
13901 ALL_BREAKPOINTS (b)
13902 if (b->next == bpt)
13903 {
13904 b->next = bpt->next;
13905 break;
13906 }
13907
13908 /* Be sure no bpstat's are pointing at the breakpoint after it's
13909 been freed. */
13910 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
13911 in all threads for now. Note that we cannot just remove bpstats
13912 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13913 commands are associated with the bpstat; if we remove it here,
13914 then the later call to bpstat_do_actions (&stop_bpstat); in
13915 event-top.c won't do anything, and temporary breakpoints with
13916 commands won't work. */
13917
13918 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13919
13920 /* Now that breakpoint is removed from breakpoint list, update the
13921 global location list. This will remove locations that used to
13922 belong to this breakpoint. Do this before freeing the breakpoint
13923 itself, since remove_breakpoint looks at location's owner. It
13924 might be better design to have location completely
13925 self-contained, but it's not the case now. */
13926 update_global_location_list (UGLL_DONT_INSERT);
13927
13928 bpt->ops->dtor (bpt);
13929 /* On the chance that someone will soon try again to delete this
13930 same bp, we mark it as deleted before freeing its storage. */
13931 bpt->type = bp_none;
13932 xfree (bpt);
13933 }
13934
13935 static void
13936 do_delete_breakpoint_cleanup (void *b)
13937 {
13938 delete_breakpoint ((struct breakpoint *) b);
13939 }
13940
13941 struct cleanup *
13942 make_cleanup_delete_breakpoint (struct breakpoint *b)
13943 {
13944 return make_cleanup (do_delete_breakpoint_cleanup, b);
13945 }
13946
13947 /* Iterator function to call a user-provided callback function once
13948 for each of B and its related breakpoints. */
13949
13950 static void
13951 iterate_over_related_breakpoints (struct breakpoint *b,
13952 void (*function) (struct breakpoint *,
13953 void *),
13954 void *data)
13955 {
13956 struct breakpoint *related;
13957
13958 related = b;
13959 do
13960 {
13961 struct breakpoint *next;
13962
13963 /* FUNCTION may delete RELATED. */
13964 next = related->related_breakpoint;
13965
13966 if (next == related)
13967 {
13968 /* RELATED is the last ring entry. */
13969 function (related, data);
13970
13971 /* FUNCTION may have deleted it, so we'd never reach back to
13972 B. There's nothing left to do anyway, so just break
13973 out. */
13974 break;
13975 }
13976 else
13977 function (related, data);
13978
13979 related = next;
13980 }
13981 while (related != b);
13982 }
13983
13984 static void
13985 do_delete_breakpoint (struct breakpoint *b, void *ignore)
13986 {
13987 delete_breakpoint (b);
13988 }
13989
13990 /* A callback for map_breakpoint_numbers that calls
13991 delete_breakpoint. */
13992
13993 static void
13994 do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
13995 {
13996 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
13997 }
13998
13999 void
14000 delete_command (char *arg, int from_tty)
14001 {
14002 struct breakpoint *b, *b_tmp;
14003
14004 dont_repeat ();
14005
14006 if (arg == 0)
14007 {
14008 int breaks_to_delete = 0;
14009
14010 /* Delete all breakpoints if no argument. Do not delete
14011 internal breakpoints, these have to be deleted with an
14012 explicit breakpoint number argument. */
14013 ALL_BREAKPOINTS (b)
14014 if (user_breakpoint_p (b))
14015 {
14016 breaks_to_delete = 1;
14017 break;
14018 }
14019
14020 /* Ask user only if there are some breakpoints to delete. */
14021 if (!from_tty
14022 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
14023 {
14024 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14025 if (user_breakpoint_p (b))
14026 delete_breakpoint (b);
14027 }
14028 }
14029 else
14030 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
14031 }
14032
14033 /* Return true if all locations of B bound to PSPACE are pending. If
14034 PSPACE is NULL, all locations of all program spaces are
14035 considered. */
14036
14037 static int
14038 all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
14039 {
14040 struct bp_location *loc;
14041
14042 for (loc = b->loc; loc != NULL; loc = loc->next)
14043 if ((pspace == NULL
14044 || loc->pspace == pspace)
14045 && !loc->shlib_disabled
14046 && !loc->pspace->executing_startup)
14047 return 0;
14048 return 1;
14049 }
14050
14051 /* Subroutine of update_breakpoint_locations to simplify it.
14052 Return non-zero if multiple fns in list LOC have the same name.
14053 Null names are ignored. */
14054
14055 static int
14056 ambiguous_names_p (struct bp_location *loc)
14057 {
14058 struct bp_location *l;
14059 htab_t htab = htab_create_alloc (13, htab_hash_string,
14060 (int (*) (const void *,
14061 const void *)) streq,
14062 NULL, xcalloc, xfree);
14063
14064 for (l = loc; l != NULL; l = l->next)
14065 {
14066 const char **slot;
14067 const char *name = l->function_name;
14068
14069 /* Allow for some names to be NULL, ignore them. */
14070 if (name == NULL)
14071 continue;
14072
14073 slot = (const char **) htab_find_slot (htab, (const void *) name,
14074 INSERT);
14075 /* NOTE: We can assume slot != NULL here because xcalloc never
14076 returns NULL. */
14077 if (*slot != NULL)
14078 {
14079 htab_delete (htab);
14080 return 1;
14081 }
14082 *slot = name;
14083 }
14084
14085 htab_delete (htab);
14086 return 0;
14087 }
14088
14089 /* When symbols change, it probably means the sources changed as well,
14090 and it might mean the static tracepoint markers are no longer at
14091 the same address or line numbers they used to be at last we
14092 checked. Losing your static tracepoints whenever you rebuild is
14093 undesirable. This function tries to resync/rematch gdb static
14094 tracepoints with the markers on the target, for static tracepoints
14095 that have not been set by marker id. Static tracepoint that have
14096 been set by marker id are reset by marker id in breakpoint_re_set.
14097 The heuristic is:
14098
14099 1) For a tracepoint set at a specific address, look for a marker at
14100 the old PC. If one is found there, assume to be the same marker.
14101 If the name / string id of the marker found is different from the
14102 previous known name, assume that means the user renamed the marker
14103 in the sources, and output a warning.
14104
14105 2) For a tracepoint set at a given line number, look for a marker
14106 at the new address of the old line number. If one is found there,
14107 assume to be the same marker. If the name / string id of the
14108 marker found is different from the previous known name, assume that
14109 means the user renamed the marker in the sources, and output a
14110 warning.
14111
14112 3) If a marker is no longer found at the same address or line, it
14113 may mean the marker no longer exists. But it may also just mean
14114 the code changed a bit. Maybe the user added a few lines of code
14115 that made the marker move up or down (in line number terms). Ask
14116 the target for info about the marker with the string id as we knew
14117 it. If found, update line number and address in the matching
14118 static tracepoint. This will get confused if there's more than one
14119 marker with the same ID (possible in UST, although unadvised
14120 precisely because it confuses tools). */
14121
14122 static struct symtab_and_line
14123 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
14124 {
14125 struct tracepoint *tp = (struct tracepoint *) b;
14126 struct static_tracepoint_marker marker;
14127 CORE_ADDR pc;
14128
14129 pc = sal.pc;
14130 if (sal.line)
14131 find_line_pc (sal.symtab, sal.line, &pc);
14132
14133 if (target_static_tracepoint_marker_at (pc, &marker))
14134 {
14135 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
14136 warning (_("static tracepoint %d changed probed marker from %s to %s"),
14137 b->number,
14138 tp->static_trace_marker_id, marker.str_id);
14139
14140 xfree (tp->static_trace_marker_id);
14141 tp->static_trace_marker_id = xstrdup (marker.str_id);
14142 release_static_tracepoint_marker (&marker);
14143
14144 return sal;
14145 }
14146
14147 /* Old marker wasn't found on target at lineno. Try looking it up
14148 by string ID. */
14149 if (!sal.explicit_pc
14150 && sal.line != 0
14151 && sal.symtab != NULL
14152 && tp->static_trace_marker_id != NULL)
14153 {
14154 VEC(static_tracepoint_marker_p) *markers;
14155
14156 markers
14157 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
14158
14159 if (!VEC_empty(static_tracepoint_marker_p, markers))
14160 {
14161 struct symtab_and_line sal2;
14162 struct symbol *sym;
14163 struct static_tracepoint_marker *tpmarker;
14164 struct ui_out *uiout = current_uiout;
14165 struct explicit_location explicit_loc;
14166
14167 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
14168
14169 xfree (tp->static_trace_marker_id);
14170 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
14171
14172 warning (_("marker for static tracepoint %d (%s) not "
14173 "found at previous line number"),
14174 b->number, tp->static_trace_marker_id);
14175
14176 init_sal (&sal2);
14177
14178 sal2.pc = tpmarker->address;
14179
14180 sal2 = find_pc_line (tpmarker->address, 0);
14181 sym = find_pc_sect_function (tpmarker->address, NULL);
14182 ui_out_text (uiout, "Now in ");
14183 if (sym)
14184 {
14185 ui_out_field_string (uiout, "func",
14186 SYMBOL_PRINT_NAME (sym));
14187 ui_out_text (uiout, " at ");
14188 }
14189 ui_out_field_string (uiout, "file",
14190 symtab_to_filename_for_display (sal2.symtab));
14191 ui_out_text (uiout, ":");
14192
14193 if (ui_out_is_mi_like_p (uiout))
14194 {
14195 const char *fullname = symtab_to_fullname (sal2.symtab);
14196
14197 ui_out_field_string (uiout, "fullname", fullname);
14198 }
14199
14200 ui_out_field_int (uiout, "line", sal2.line);
14201 ui_out_text (uiout, "\n");
14202
14203 b->loc->line_number = sal2.line;
14204 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
14205
14206 delete_event_location (b->location);
14207 initialize_explicit_location (&explicit_loc);
14208 explicit_loc.source_filename
14209 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
14210 explicit_loc.line_offset.offset = b->loc->line_number;
14211 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
14212 b->location = new_explicit_location (&explicit_loc);
14213
14214 /* Might be nice to check if function changed, and warn if
14215 so. */
14216
14217 release_static_tracepoint_marker (tpmarker);
14218 }
14219 }
14220 return sal;
14221 }
14222
14223 /* Returns 1 iff locations A and B are sufficiently same that
14224 we don't need to report breakpoint as changed. */
14225
14226 static int
14227 locations_are_equal (struct bp_location *a, struct bp_location *b)
14228 {
14229 while (a && b)
14230 {
14231 if (a->address != b->address)
14232 return 0;
14233
14234 if (a->shlib_disabled != b->shlib_disabled)
14235 return 0;
14236
14237 if (a->enabled != b->enabled)
14238 return 0;
14239
14240 a = a->next;
14241 b = b->next;
14242 }
14243
14244 if ((a == NULL) != (b == NULL))
14245 return 0;
14246
14247 return 1;
14248 }
14249
14250 /* Split all locations of B that are bound to PSPACE out of B's
14251 location list to a separate list and return that list's head. If
14252 PSPACE is NULL, hoist out all locations of B. */
14253
14254 static struct bp_location *
14255 hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
14256 {
14257 struct bp_location head;
14258 struct bp_location *i = b->loc;
14259 struct bp_location **i_link = &b->loc;
14260 struct bp_location *hoisted = &head;
14261
14262 if (pspace == NULL)
14263 {
14264 i = b->loc;
14265 b->loc = NULL;
14266 return i;
14267 }
14268
14269 head.next = NULL;
14270
14271 while (i != NULL)
14272 {
14273 if (i->pspace == pspace)
14274 {
14275 *i_link = i->next;
14276 i->next = NULL;
14277 hoisted->next = i;
14278 hoisted = i;
14279 }
14280 else
14281 i_link = &i->next;
14282 i = *i_link;
14283 }
14284
14285 return head.next;
14286 }
14287
14288 /* Create new breakpoint locations for B (a hardware or software
14289 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
14290 zero, then B is a ranged breakpoint. Only recreates locations for
14291 FILTER_PSPACE. Locations of other program spaces are left
14292 untouched. */
14293
14294 void
14295 update_breakpoint_locations (struct breakpoint *b,
14296 struct program_space *filter_pspace,
14297 struct symtabs_and_lines sals,
14298 struct symtabs_and_lines sals_end)
14299 {
14300 int i;
14301 struct bp_location *existing_locations;
14302
14303 if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
14304 {
14305 /* Ranged breakpoints have only one start location and one end
14306 location. */
14307 b->enable_state = bp_disabled;
14308 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
14309 "multiple locations found\n"),
14310 b->number);
14311 return;
14312 }
14313
14314 /* If there's no new locations, and all existing locations are
14315 pending, don't do anything. This optimizes the common case where
14316 all locations are in the same shared library, that was unloaded.
14317 We'd like to retain the location, so that when the library is
14318 loaded again, we don't loose the enabled/disabled status of the
14319 individual locations. */
14320 if (all_locations_are_pending (b, filter_pspace) && sals.nelts == 0)
14321 return;
14322
14323 existing_locations = hoist_existing_locations (b, filter_pspace);
14324
14325 for (i = 0; i < sals.nelts; ++i)
14326 {
14327 struct bp_location *new_loc;
14328
14329 switch_to_program_space_and_thread (sals.sals[i].pspace);
14330
14331 new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
14332
14333 /* Reparse conditions, they might contain references to the
14334 old symtab. */
14335 if (b->cond_string != NULL)
14336 {
14337 const char *s;
14338
14339 s = b->cond_string;
14340 TRY
14341 {
14342 new_loc->cond = parse_exp_1 (&s, sals.sals[i].pc,
14343 block_for_pc (sals.sals[i].pc),
14344 0);
14345 }
14346 CATCH (e, RETURN_MASK_ERROR)
14347 {
14348 warning (_("failed to reevaluate condition "
14349 "for breakpoint %d: %s"),
14350 b->number, e.message);
14351 new_loc->enabled = 0;
14352 }
14353 END_CATCH
14354 }
14355
14356 if (sals_end.nelts)
14357 {
14358 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
14359
14360 new_loc->length = end - sals.sals[0].pc + 1;
14361 }
14362 }
14363
14364 /* If possible, carry over 'disable' status from existing
14365 breakpoints. */
14366 {
14367 struct bp_location *e = existing_locations;
14368 /* If there are multiple breakpoints with the same function name,
14369 e.g. for inline functions, comparing function names won't work.
14370 Instead compare pc addresses; this is just a heuristic as things
14371 may have moved, but in practice it gives the correct answer
14372 often enough until a better solution is found. */
14373 int have_ambiguous_names = ambiguous_names_p (b->loc);
14374
14375 for (; e; e = e->next)
14376 {
14377 if (!e->enabled && e->function_name)
14378 {
14379 struct bp_location *l = b->loc;
14380 if (have_ambiguous_names)
14381 {
14382 for (; l; l = l->next)
14383 if (breakpoint_locations_match (e, l))
14384 {
14385 l->enabled = 0;
14386 break;
14387 }
14388 }
14389 else
14390 {
14391 for (; l; l = l->next)
14392 if (l->function_name
14393 && strcmp (e->function_name, l->function_name) == 0)
14394 {
14395 l->enabled = 0;
14396 break;
14397 }
14398 }
14399 }
14400 }
14401 }
14402
14403 if (!locations_are_equal (existing_locations, b->loc))
14404 observer_notify_breakpoint_modified (b);
14405 }
14406
14407 /* Find the SaL locations corresponding to the given LOCATION.
14408 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
14409
14410 static struct symtabs_and_lines
14411 location_to_sals (struct breakpoint *b, struct event_location *location,
14412 struct program_space *search_pspace, int *found)
14413 {
14414 struct symtabs_and_lines sals = {0};
14415 struct gdb_exception exception = exception_none;
14416
14417 gdb_assert (b->ops != NULL);
14418
14419 TRY
14420 {
14421 b->ops->decode_location (b, location, search_pspace, &sals);
14422 }
14423 CATCH (e, RETURN_MASK_ERROR)
14424 {
14425 int not_found_and_ok = 0;
14426
14427 exception = e;
14428
14429 /* For pending breakpoints, it's expected that parsing will
14430 fail until the right shared library is loaded. User has
14431 already told to create pending breakpoints and don't need
14432 extra messages. If breakpoint is in bp_shlib_disabled
14433 state, then user already saw the message about that
14434 breakpoint being disabled, and don't want to see more
14435 errors. */
14436 if (e.error == NOT_FOUND_ERROR
14437 && (b->condition_not_parsed
14438 || (b->loc != NULL
14439 && search_pspace != NULL
14440 && b->loc->pspace != search_pspace)
14441 || (b->loc && b->loc->shlib_disabled)
14442 || (b->loc && b->loc->pspace->executing_startup)
14443 || b->enable_state == bp_disabled))
14444 not_found_and_ok = 1;
14445
14446 if (!not_found_and_ok)
14447 {
14448 /* We surely don't want to warn about the same breakpoint
14449 10 times. One solution, implemented here, is disable
14450 the breakpoint on error. Another solution would be to
14451 have separate 'warning emitted' flag. Since this
14452 happens only when a binary has changed, I don't know
14453 which approach is better. */
14454 b->enable_state = bp_disabled;
14455 throw_exception (e);
14456 }
14457 }
14458 END_CATCH
14459
14460 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
14461 {
14462 int i;
14463
14464 for (i = 0; i < sals.nelts; ++i)
14465 resolve_sal_pc (&sals.sals[i]);
14466 if (b->condition_not_parsed && b->extra_string != NULL)
14467 {
14468 char *cond_string, *extra_string;
14469 int thread, task;
14470
14471 find_condition_and_thread (b->extra_string, sals.sals[0].pc,
14472 &cond_string, &thread, &task,
14473 &extra_string);
14474 gdb_assert (b->cond_string == NULL);
14475 if (cond_string)
14476 b->cond_string = cond_string;
14477 b->thread = thread;
14478 b->task = task;
14479 if (extra_string)
14480 {
14481 xfree (b->extra_string);
14482 b->extra_string = extra_string;
14483 }
14484 b->condition_not_parsed = 0;
14485 }
14486
14487 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
14488 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
14489
14490 *found = 1;
14491 }
14492 else
14493 *found = 0;
14494
14495 return sals;
14496 }
14497
14498 /* The default re_set method, for typical hardware or software
14499 breakpoints. Reevaluate the breakpoint and recreate its
14500 locations. */
14501
14502 static void
14503 breakpoint_re_set_default (struct breakpoint *b)
14504 {
14505 int found;
14506 struct symtabs_and_lines sals, sals_end;
14507 struct symtabs_and_lines expanded = {0};
14508 struct symtabs_and_lines expanded_end = {0};
14509 struct program_space *filter_pspace = current_program_space;
14510
14511 sals = location_to_sals (b, b->location, filter_pspace, &found);
14512 if (found)
14513 {
14514 make_cleanup (xfree, sals.sals);
14515 expanded = sals;
14516 }
14517
14518 if (b->location_range_end != NULL)
14519 {
14520 sals_end = location_to_sals (b, b->location_range_end,
14521 filter_pspace, &found);
14522 if (found)
14523 {
14524 make_cleanup (xfree, sals_end.sals);
14525 expanded_end = sals_end;
14526 }
14527 }
14528
14529 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
14530 }
14531
14532 /* Default method for creating SALs from an address string. It basically
14533 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
14534
14535 static void
14536 create_sals_from_location_default (const struct event_location *location,
14537 struct linespec_result *canonical,
14538 enum bptype type_wanted)
14539 {
14540 parse_breakpoint_sals (location, canonical);
14541 }
14542
14543 /* Call create_breakpoints_sal for the given arguments. This is the default
14544 function for the `create_breakpoints_sal' method of
14545 breakpoint_ops. */
14546
14547 static void
14548 create_breakpoints_sal_default (struct gdbarch *gdbarch,
14549 struct linespec_result *canonical,
14550 char *cond_string,
14551 char *extra_string,
14552 enum bptype type_wanted,
14553 enum bpdisp disposition,
14554 int thread,
14555 int task, int ignore_count,
14556 const struct breakpoint_ops *ops,
14557 int from_tty, int enabled,
14558 int internal, unsigned flags)
14559 {
14560 create_breakpoints_sal (gdbarch, canonical, cond_string,
14561 extra_string,
14562 type_wanted, disposition,
14563 thread, task, ignore_count, ops, from_tty,
14564 enabled, internal, flags);
14565 }
14566
14567 /* Decode the line represented by S by calling decode_line_full. This is the
14568 default function for the `decode_location' method of breakpoint_ops. */
14569
14570 static void
14571 decode_location_default (struct breakpoint *b,
14572 const struct event_location *location,
14573 struct program_space *search_pspace,
14574 struct symtabs_and_lines *sals)
14575 {
14576 struct linespec_result canonical;
14577
14578 init_linespec_result (&canonical);
14579 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
14580 (struct symtab *) NULL, 0,
14581 &canonical, multiple_symbols_all,
14582 b->filter);
14583
14584 /* We should get 0 or 1 resulting SALs. */
14585 gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
14586
14587 if (VEC_length (linespec_sals, canonical.sals) > 0)
14588 {
14589 struct linespec_sals *lsal;
14590
14591 lsal = VEC_index (linespec_sals, canonical.sals, 0);
14592 *sals = lsal->sals;
14593 /* Arrange it so the destructor does not free the
14594 contents. */
14595 lsal->sals.sals = NULL;
14596 }
14597
14598 destroy_linespec_result (&canonical);
14599 }
14600
14601 /* Prepare the global context for a re-set of breakpoint B. */
14602
14603 static struct cleanup *
14604 prepare_re_set_context (struct breakpoint *b)
14605 {
14606 input_radix = b->input_radix;
14607 set_language (b->language);
14608
14609 return make_cleanup (null_cleanup, NULL);
14610 }
14611
14612 /* Reset a breakpoint given it's struct breakpoint * BINT.
14613 The value we return ends up being the return value from catch_errors.
14614 Unused in this case. */
14615
14616 static int
14617 breakpoint_re_set_one (void *bint)
14618 {
14619 /* Get past catch_errs. */
14620 struct breakpoint *b = (struct breakpoint *) bint;
14621 struct cleanup *cleanups;
14622
14623 cleanups = prepare_re_set_context (b);
14624 b->ops->re_set (b);
14625 do_cleanups (cleanups);
14626 return 0;
14627 }
14628
14629 /* Re-set breakpoint locations for the current program space.
14630 Locations bound to other program spaces are left untouched. */
14631
14632 void
14633 breakpoint_re_set (void)
14634 {
14635 struct breakpoint *b, *b_tmp;
14636 enum language save_language;
14637 int save_input_radix;
14638 struct cleanup *old_chain;
14639
14640 save_language = current_language->la_language;
14641 save_input_radix = input_radix;
14642 old_chain = save_current_space_and_thread ();
14643
14644 /* Note: we must not try to insert locations until after all
14645 breakpoints have been re-set. Otherwise, e.g., when re-setting
14646 breakpoint 1, we'd insert the locations of breakpoint 2, which
14647 hadn't been re-set yet, and thus may have stale locations. */
14648
14649 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14650 {
14651 /* Format possible error msg. */
14652 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
14653 b->number);
14654 struct cleanup *cleanups = make_cleanup (xfree, message);
14655 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
14656 do_cleanups (cleanups);
14657 }
14658 set_language (save_language);
14659 input_radix = save_input_radix;
14660
14661 jit_breakpoint_re_set ();
14662
14663 do_cleanups (old_chain);
14664
14665 create_overlay_event_breakpoint ();
14666 create_longjmp_master_breakpoint ();
14667 create_std_terminate_master_breakpoint ();
14668 create_exception_master_breakpoint ();
14669
14670 /* Now we can insert. */
14671 update_global_location_list (UGLL_MAY_INSERT);
14672 }
14673 \f
14674 /* Reset the thread number of this breakpoint:
14675
14676 - If the breakpoint is for all threads, leave it as-is.
14677 - Else, reset it to the current thread for inferior_ptid. */
14678 void
14679 breakpoint_re_set_thread (struct breakpoint *b)
14680 {
14681 if (b->thread != -1)
14682 {
14683 if (in_thread_list (inferior_ptid))
14684 b->thread = ptid_to_global_thread_id (inferior_ptid);
14685
14686 /* We're being called after following a fork. The new fork is
14687 selected as current, and unless this was a vfork will have a
14688 different program space from the original thread. Reset that
14689 as well. */
14690 b->loc->pspace = current_program_space;
14691 }
14692 }
14693
14694 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14695 If from_tty is nonzero, it prints a message to that effect,
14696 which ends with a period (no newline). */
14697
14698 void
14699 set_ignore_count (int bptnum, int count, int from_tty)
14700 {
14701 struct breakpoint *b;
14702
14703 if (count < 0)
14704 count = 0;
14705
14706 ALL_BREAKPOINTS (b)
14707 if (b->number == bptnum)
14708 {
14709 if (is_tracepoint (b))
14710 {
14711 if (from_tty && count != 0)
14712 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14713 bptnum);
14714 return;
14715 }
14716
14717 b->ignore_count = count;
14718 if (from_tty)
14719 {
14720 if (count == 0)
14721 printf_filtered (_("Will stop next time "
14722 "breakpoint %d is reached."),
14723 bptnum);
14724 else if (count == 1)
14725 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
14726 bptnum);
14727 else
14728 printf_filtered (_("Will ignore next %d "
14729 "crossings of breakpoint %d."),
14730 count, bptnum);
14731 }
14732 observer_notify_breakpoint_modified (b);
14733 return;
14734 }
14735
14736 error (_("No breakpoint number %d."), bptnum);
14737 }
14738
14739 /* Command to set ignore-count of breakpoint N to COUNT. */
14740
14741 static void
14742 ignore_command (char *args, int from_tty)
14743 {
14744 char *p = args;
14745 int num;
14746
14747 if (p == 0)
14748 error_no_arg (_("a breakpoint number"));
14749
14750 num = get_number (&p);
14751 if (num == 0)
14752 error (_("bad breakpoint number: '%s'"), args);
14753 if (*p == 0)
14754 error (_("Second argument (specified ignore-count) is missing."));
14755
14756 set_ignore_count (num,
14757 longest_to_int (value_as_long (parse_and_eval (p))),
14758 from_tty);
14759 if (from_tty)
14760 printf_filtered ("\n");
14761 }
14762 \f
14763 /* Call FUNCTION on each of the breakpoints
14764 whose numbers are given in ARGS. */
14765
14766 static void
14767 map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
14768 void *),
14769 void *data)
14770 {
14771 int num;
14772 struct breakpoint *b, *tmp;
14773 int match;
14774 struct get_number_or_range_state state;
14775
14776 if (args == 0 || *args == '\0')
14777 error_no_arg (_("one or more breakpoint numbers"));
14778
14779 init_number_or_range (&state, args);
14780
14781 while (!state.finished)
14782 {
14783 const char *p = state.string;
14784
14785 match = 0;
14786
14787 num = get_number_or_range (&state);
14788 if (num == 0)
14789 {
14790 warning (_("bad breakpoint number at or near '%s'"), p);
14791 }
14792 else
14793 {
14794 ALL_BREAKPOINTS_SAFE (b, tmp)
14795 if (b->number == num)
14796 {
14797 match = 1;
14798 function (b, data);
14799 break;
14800 }
14801 if (match == 0)
14802 printf_unfiltered (_("No breakpoint number %d.\n"), num);
14803 }
14804 }
14805 }
14806
14807 static struct bp_location *
14808 find_location_by_number (char *number)
14809 {
14810 char *dot = strchr (number, '.');
14811 char *p1;
14812 int bp_num;
14813 int loc_num;
14814 struct breakpoint *b;
14815 struct bp_location *loc;
14816
14817 *dot = '\0';
14818
14819 p1 = number;
14820 bp_num = get_number (&p1);
14821 if (bp_num == 0)
14822 error (_("Bad breakpoint number '%s'"), number);
14823
14824 ALL_BREAKPOINTS (b)
14825 if (b->number == bp_num)
14826 {
14827 break;
14828 }
14829
14830 if (!b || b->number != bp_num)
14831 error (_("Bad breakpoint number '%s'"), number);
14832
14833 p1 = dot+1;
14834 loc_num = get_number (&p1);
14835 if (loc_num == 0)
14836 error (_("Bad breakpoint location number '%s'"), number);
14837
14838 --loc_num;
14839 loc = b->loc;
14840 for (;loc_num && loc; --loc_num, loc = loc->next)
14841 ;
14842 if (!loc)
14843 error (_("Bad breakpoint location number '%s'"), dot+1);
14844
14845 return loc;
14846 }
14847
14848
14849 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14850 If from_tty is nonzero, it prints a message to that effect,
14851 which ends with a period (no newline). */
14852
14853 void
14854 disable_breakpoint (struct breakpoint *bpt)
14855 {
14856 /* Never disable a watchpoint scope breakpoint; we want to
14857 hit them when we leave scope so we can delete both the
14858 watchpoint and its scope breakpoint at that time. */
14859 if (bpt->type == bp_watchpoint_scope)
14860 return;
14861
14862 bpt->enable_state = bp_disabled;
14863
14864 /* Mark breakpoint locations modified. */
14865 mark_breakpoint_modified (bpt);
14866
14867 if (target_supports_enable_disable_tracepoint ()
14868 && current_trace_status ()->running && is_tracepoint (bpt))
14869 {
14870 struct bp_location *location;
14871
14872 for (location = bpt->loc; location; location = location->next)
14873 target_disable_tracepoint (location);
14874 }
14875
14876 update_global_location_list (UGLL_DONT_INSERT);
14877
14878 observer_notify_breakpoint_modified (bpt);
14879 }
14880
14881 /* A callback for iterate_over_related_breakpoints. */
14882
14883 static void
14884 do_disable_breakpoint (struct breakpoint *b, void *ignore)
14885 {
14886 disable_breakpoint (b);
14887 }
14888
14889 /* A callback for map_breakpoint_numbers that calls
14890 disable_breakpoint. */
14891
14892 static void
14893 do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
14894 {
14895 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
14896 }
14897
14898 static void
14899 disable_command (char *args, int from_tty)
14900 {
14901 if (args == 0)
14902 {
14903 struct breakpoint *bpt;
14904
14905 ALL_BREAKPOINTS (bpt)
14906 if (user_breakpoint_p (bpt))
14907 disable_breakpoint (bpt);
14908 }
14909 else
14910 {
14911 char *num = extract_arg (&args);
14912
14913 while (num)
14914 {
14915 if (strchr (num, '.'))
14916 {
14917 struct bp_location *loc = find_location_by_number (num);
14918
14919 if (loc)
14920 {
14921 if (loc->enabled)
14922 {
14923 loc->enabled = 0;
14924 mark_breakpoint_location_modified (loc);
14925 }
14926 if (target_supports_enable_disable_tracepoint ()
14927 && current_trace_status ()->running && loc->owner
14928 && is_tracepoint (loc->owner))
14929 target_disable_tracepoint (loc);
14930 }
14931 update_global_location_list (UGLL_DONT_INSERT);
14932 }
14933 else
14934 map_breakpoint_numbers (num, do_map_disable_breakpoint, NULL);
14935 num = extract_arg (&args);
14936 }
14937 }
14938 }
14939
14940 static void
14941 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14942 int count)
14943 {
14944 int target_resources_ok;
14945
14946 if (bpt->type == bp_hardware_breakpoint)
14947 {
14948 int i;
14949 i = hw_breakpoint_used_count ();
14950 target_resources_ok =
14951 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
14952 i + 1, 0);
14953 if (target_resources_ok == 0)
14954 error (_("No hardware breakpoint support in the target."));
14955 else if (target_resources_ok < 0)
14956 error (_("Hardware breakpoints used exceeds limit."));
14957 }
14958
14959 if (is_watchpoint (bpt))
14960 {
14961 /* Initialize it just to avoid a GCC false warning. */
14962 enum enable_state orig_enable_state = bp_disabled;
14963
14964 TRY
14965 {
14966 struct watchpoint *w = (struct watchpoint *) bpt;
14967
14968 orig_enable_state = bpt->enable_state;
14969 bpt->enable_state = bp_enabled;
14970 update_watchpoint (w, 1 /* reparse */);
14971 }
14972 CATCH (e, RETURN_MASK_ALL)
14973 {
14974 bpt->enable_state = orig_enable_state;
14975 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14976 bpt->number);
14977 return;
14978 }
14979 END_CATCH
14980 }
14981
14982 bpt->enable_state = bp_enabled;
14983
14984 /* Mark breakpoint locations modified. */
14985 mark_breakpoint_modified (bpt);
14986
14987 if (target_supports_enable_disable_tracepoint ()
14988 && current_trace_status ()->running && is_tracepoint (bpt))
14989 {
14990 struct bp_location *location;
14991
14992 for (location = bpt->loc; location; location = location->next)
14993 target_enable_tracepoint (location);
14994 }
14995
14996 bpt->disposition = disposition;
14997 bpt->enable_count = count;
14998 update_global_location_list (UGLL_MAY_INSERT);
14999
15000 observer_notify_breakpoint_modified (bpt);
15001 }
15002
15003
15004 void
15005 enable_breakpoint (struct breakpoint *bpt)
15006 {
15007 enable_breakpoint_disp (bpt, bpt->disposition, 0);
15008 }
15009
15010 static void
15011 do_enable_breakpoint (struct breakpoint *bpt, void *arg)
15012 {
15013 enable_breakpoint (bpt);
15014 }
15015
15016 /* A callback for map_breakpoint_numbers that calls
15017 enable_breakpoint. */
15018
15019 static void
15020 do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
15021 {
15022 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
15023 }
15024
15025 /* The enable command enables the specified breakpoints (or all defined
15026 breakpoints) so they once again become (or continue to be) effective
15027 in stopping the inferior. */
15028
15029 static void
15030 enable_command (char *args, int from_tty)
15031 {
15032 if (args == 0)
15033 {
15034 struct breakpoint *bpt;
15035
15036 ALL_BREAKPOINTS (bpt)
15037 if (user_breakpoint_p (bpt))
15038 enable_breakpoint (bpt);
15039 }
15040 else
15041 {
15042 char *num = extract_arg (&args);
15043
15044 while (num)
15045 {
15046 if (strchr (num, '.'))
15047 {
15048 struct bp_location *loc = find_location_by_number (num);
15049
15050 if (loc)
15051 {
15052 if (!loc->enabled)
15053 {
15054 loc->enabled = 1;
15055 mark_breakpoint_location_modified (loc);
15056 }
15057 if (target_supports_enable_disable_tracepoint ()
15058 && current_trace_status ()->running && loc->owner
15059 && is_tracepoint (loc->owner))
15060 target_enable_tracepoint (loc);
15061 }
15062 update_global_location_list (UGLL_MAY_INSERT);
15063 }
15064 else
15065 map_breakpoint_numbers (num, do_map_enable_breakpoint, NULL);
15066 num = extract_arg (&args);
15067 }
15068 }
15069 }
15070
15071 /* This struct packages up disposition data for application to multiple
15072 breakpoints. */
15073
15074 struct disp_data
15075 {
15076 enum bpdisp disp;
15077 int count;
15078 };
15079
15080 static void
15081 do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
15082 {
15083 struct disp_data disp_data = *(struct disp_data *) arg;
15084
15085 enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
15086 }
15087
15088 static void
15089 do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
15090 {
15091 struct disp_data disp = { disp_disable, 1 };
15092
15093 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
15094 }
15095
15096 static void
15097 enable_once_command (char *args, int from_tty)
15098 {
15099 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
15100 }
15101
15102 static void
15103 do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
15104 {
15105 struct disp_data disp = { disp_disable, *(int *) countptr };
15106
15107 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
15108 }
15109
15110 static void
15111 enable_count_command (char *args, int from_tty)
15112 {
15113 int count;
15114
15115 if (args == NULL)
15116 error_no_arg (_("hit count"));
15117
15118 count = get_number (&args);
15119
15120 map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
15121 }
15122
15123 static void
15124 do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
15125 {
15126 struct disp_data disp = { disp_del, 1 };
15127
15128 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
15129 }
15130
15131 static void
15132 enable_delete_command (char *args, int from_tty)
15133 {
15134 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
15135 }
15136 \f
15137 static void
15138 set_breakpoint_cmd (char *args, int from_tty)
15139 {
15140 }
15141
15142 static void
15143 show_breakpoint_cmd (char *args, int from_tty)
15144 {
15145 }
15146
15147 /* Invalidate last known value of any hardware watchpoint if
15148 the memory which that value represents has been written to by
15149 GDB itself. */
15150
15151 static void
15152 invalidate_bp_value_on_memory_change (struct inferior *inferior,
15153 CORE_ADDR addr, ssize_t len,
15154 const bfd_byte *data)
15155 {
15156 struct breakpoint *bp;
15157
15158 ALL_BREAKPOINTS (bp)
15159 if (bp->enable_state == bp_enabled
15160 && bp->type == bp_hardware_watchpoint)
15161 {
15162 struct watchpoint *wp = (struct watchpoint *) bp;
15163
15164 if (wp->val_valid && wp->val)
15165 {
15166 struct bp_location *loc;
15167
15168 for (loc = bp->loc; loc != NULL; loc = loc->next)
15169 if (loc->loc_type == bp_loc_hardware_watchpoint
15170 && loc->address + loc->length > addr
15171 && addr + len > loc->address)
15172 {
15173 value_free (wp->val);
15174 wp->val = NULL;
15175 wp->val_valid = 0;
15176 }
15177 }
15178 }
15179 }
15180
15181 /* Create and insert a breakpoint for software single step. */
15182
15183 void
15184 insert_single_step_breakpoint (struct gdbarch *gdbarch,
15185 struct address_space *aspace,
15186 CORE_ADDR next_pc)
15187 {
15188 struct thread_info *tp = inferior_thread ();
15189 struct symtab_and_line sal;
15190 CORE_ADDR pc = next_pc;
15191
15192 if (tp->control.single_step_breakpoints == NULL)
15193 {
15194 tp->control.single_step_breakpoints
15195 = new_single_step_breakpoint (tp->global_num, gdbarch);
15196 }
15197
15198 sal = find_pc_line (pc, 0);
15199 sal.pc = pc;
15200 sal.section = find_pc_overlay (pc);
15201 sal.explicit_pc = 1;
15202 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
15203
15204 update_global_location_list (UGLL_INSERT);
15205 }
15206
15207 /* See breakpoint.h. */
15208
15209 int
15210 breakpoint_has_location_inserted_here (struct breakpoint *bp,
15211 struct address_space *aspace,
15212 CORE_ADDR pc)
15213 {
15214 struct bp_location *loc;
15215
15216 for (loc = bp->loc; loc != NULL; loc = loc->next)
15217 if (loc->inserted
15218 && breakpoint_location_address_match (loc, aspace, pc))
15219 return 1;
15220
15221 return 0;
15222 }
15223
15224 /* Check whether a software single-step breakpoint is inserted at
15225 PC. */
15226
15227 int
15228 single_step_breakpoint_inserted_here_p (struct address_space *aspace,
15229 CORE_ADDR pc)
15230 {
15231 struct breakpoint *bpt;
15232
15233 ALL_BREAKPOINTS (bpt)
15234 {
15235 if (bpt->type == bp_single_step
15236 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
15237 return 1;
15238 }
15239 return 0;
15240 }
15241
15242 /* Tracepoint-specific operations. */
15243
15244 /* Set tracepoint count to NUM. */
15245 static void
15246 set_tracepoint_count (int num)
15247 {
15248 tracepoint_count = num;
15249 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
15250 }
15251
15252 static void
15253 trace_command (char *arg, int from_tty)
15254 {
15255 struct breakpoint_ops *ops;
15256 struct event_location *location;
15257 struct cleanup *back_to;
15258
15259 location = string_to_event_location (&arg, current_language);
15260 back_to = make_cleanup_delete_event_location (location);
15261 if (location != NULL
15262 && event_location_type (location) == PROBE_LOCATION)
15263 ops = &tracepoint_probe_breakpoint_ops;
15264 else
15265 ops = &tracepoint_breakpoint_ops;
15266
15267 create_breakpoint (get_current_arch (),
15268 location,
15269 NULL, 0, arg, 1 /* parse arg */,
15270 0 /* tempflag */,
15271 bp_tracepoint /* type_wanted */,
15272 0 /* Ignore count */,
15273 pending_break_support,
15274 ops,
15275 from_tty,
15276 1 /* enabled */,
15277 0 /* internal */, 0);
15278 do_cleanups (back_to);
15279 }
15280
15281 static void
15282 ftrace_command (char *arg, int from_tty)
15283 {
15284 struct event_location *location;
15285 struct cleanup *back_to;
15286
15287 location = string_to_event_location (&arg, current_language);
15288 back_to = make_cleanup_delete_event_location (location);
15289 create_breakpoint (get_current_arch (),
15290 location,
15291 NULL, 0, arg, 1 /* parse arg */,
15292 0 /* tempflag */,
15293 bp_fast_tracepoint /* type_wanted */,
15294 0 /* Ignore count */,
15295 pending_break_support,
15296 &tracepoint_breakpoint_ops,
15297 from_tty,
15298 1 /* enabled */,
15299 0 /* internal */, 0);
15300 do_cleanups (back_to);
15301 }
15302
15303 /* strace command implementation. Creates a static tracepoint. */
15304
15305 static void
15306 strace_command (char *arg, int from_tty)
15307 {
15308 struct breakpoint_ops *ops;
15309 struct event_location *location;
15310 struct cleanup *back_to;
15311
15312 /* Decide if we are dealing with a static tracepoint marker (`-m'),
15313 or with a normal static tracepoint. */
15314 if (arg && startswith (arg, "-m") && isspace (arg[2]))
15315 {
15316 ops = &strace_marker_breakpoint_ops;
15317 location = new_linespec_location (&arg);
15318 }
15319 else
15320 {
15321 ops = &tracepoint_breakpoint_ops;
15322 location = string_to_event_location (&arg, current_language);
15323 }
15324
15325 back_to = make_cleanup_delete_event_location (location);
15326 create_breakpoint (get_current_arch (),
15327 location,
15328 NULL, 0, arg, 1 /* parse arg */,
15329 0 /* tempflag */,
15330 bp_static_tracepoint /* type_wanted */,
15331 0 /* Ignore count */,
15332 pending_break_support,
15333 ops,
15334 from_tty,
15335 1 /* enabled */,
15336 0 /* internal */, 0);
15337 do_cleanups (back_to);
15338 }
15339
15340 /* Set up a fake reader function that gets command lines from a linked
15341 list that was acquired during tracepoint uploading. */
15342
15343 static struct uploaded_tp *this_utp;
15344 static int next_cmd;
15345
15346 static char *
15347 read_uploaded_action (void)
15348 {
15349 char *rslt;
15350
15351 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
15352
15353 next_cmd++;
15354
15355 return rslt;
15356 }
15357
15358 /* Given information about a tracepoint as recorded on a target (which
15359 can be either a live system or a trace file), attempt to create an
15360 equivalent GDB tracepoint. This is not a reliable process, since
15361 the target does not necessarily have all the information used when
15362 the tracepoint was originally defined. */
15363
15364 struct tracepoint *
15365 create_tracepoint_from_upload (struct uploaded_tp *utp)
15366 {
15367 char *addr_str, small_buf[100];
15368 struct tracepoint *tp;
15369 struct event_location *location;
15370 struct cleanup *cleanup;
15371
15372 if (utp->at_string)
15373 addr_str = utp->at_string;
15374 else
15375 {
15376 /* In the absence of a source location, fall back to raw
15377 address. Since there is no way to confirm that the address
15378 means the same thing as when the trace was started, warn the
15379 user. */
15380 warning (_("Uploaded tracepoint %d has no "
15381 "source location, using raw address"),
15382 utp->number);
15383 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
15384 addr_str = small_buf;
15385 }
15386
15387 /* There's not much we can do with a sequence of bytecodes. */
15388 if (utp->cond && !utp->cond_string)
15389 warning (_("Uploaded tracepoint %d condition "
15390 "has no source form, ignoring it"),
15391 utp->number);
15392
15393 location = string_to_event_location (&addr_str, current_language);
15394 cleanup = make_cleanup_delete_event_location (location);
15395 if (!create_breakpoint (get_current_arch (),
15396 location,
15397 utp->cond_string, -1, addr_str,
15398 0 /* parse cond/thread */,
15399 0 /* tempflag */,
15400 utp->type /* type_wanted */,
15401 0 /* Ignore count */,
15402 pending_break_support,
15403 &tracepoint_breakpoint_ops,
15404 0 /* from_tty */,
15405 utp->enabled /* enabled */,
15406 0 /* internal */,
15407 CREATE_BREAKPOINT_FLAGS_INSERTED))
15408 {
15409 do_cleanups (cleanup);
15410 return NULL;
15411 }
15412
15413 do_cleanups (cleanup);
15414
15415 /* Get the tracepoint we just created. */
15416 tp = get_tracepoint (tracepoint_count);
15417 gdb_assert (tp != NULL);
15418
15419 if (utp->pass > 0)
15420 {
15421 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
15422 tp->base.number);
15423
15424 trace_pass_command (small_buf, 0);
15425 }
15426
15427 /* If we have uploaded versions of the original commands, set up a
15428 special-purpose "reader" function and call the usual command line
15429 reader, then pass the result to the breakpoint command-setting
15430 function. */
15431 if (!VEC_empty (char_ptr, utp->cmd_strings))
15432 {
15433 struct command_line *cmd_list;
15434
15435 this_utp = utp;
15436 next_cmd = 0;
15437
15438 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
15439
15440 breakpoint_set_commands (&tp->base, cmd_list);
15441 }
15442 else if (!VEC_empty (char_ptr, utp->actions)
15443 || !VEC_empty (char_ptr, utp->step_actions))
15444 warning (_("Uploaded tracepoint %d actions "
15445 "have no source form, ignoring them"),
15446 utp->number);
15447
15448 /* Copy any status information that might be available. */
15449 tp->base.hit_count = utp->hit_count;
15450 tp->traceframe_usage = utp->traceframe_usage;
15451
15452 return tp;
15453 }
15454
15455 /* Print information on tracepoint number TPNUM_EXP, or all if
15456 omitted. */
15457
15458 static void
15459 tracepoints_info (char *args, int from_tty)
15460 {
15461 struct ui_out *uiout = current_uiout;
15462 int num_printed;
15463
15464 num_printed = breakpoint_1 (args, 0, is_tracepoint);
15465
15466 if (num_printed == 0)
15467 {
15468 if (args == NULL || *args == '\0')
15469 ui_out_message (uiout, 0, "No tracepoints.\n");
15470 else
15471 ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
15472 }
15473
15474 default_collect_info ();
15475 }
15476
15477 /* The 'enable trace' command enables tracepoints.
15478 Not supported by all targets. */
15479 static void
15480 enable_trace_command (char *args, int from_tty)
15481 {
15482 enable_command (args, from_tty);
15483 }
15484
15485 /* The 'disable trace' command disables tracepoints.
15486 Not supported by all targets. */
15487 static void
15488 disable_trace_command (char *args, int from_tty)
15489 {
15490 disable_command (args, from_tty);
15491 }
15492
15493 /* Remove a tracepoint (or all if no argument). */
15494 static void
15495 delete_trace_command (char *arg, int from_tty)
15496 {
15497 struct breakpoint *b, *b_tmp;
15498
15499 dont_repeat ();
15500
15501 if (arg == 0)
15502 {
15503 int breaks_to_delete = 0;
15504
15505 /* Delete all breakpoints if no argument.
15506 Do not delete internal or call-dummy breakpoints, these
15507 have to be deleted with an explicit breakpoint number
15508 argument. */
15509 ALL_TRACEPOINTS (b)
15510 if (is_tracepoint (b) && user_breakpoint_p (b))
15511 {
15512 breaks_to_delete = 1;
15513 break;
15514 }
15515
15516 /* Ask user only if there are some breakpoints to delete. */
15517 if (!from_tty
15518 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
15519 {
15520 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15521 if (is_tracepoint (b) && user_breakpoint_p (b))
15522 delete_breakpoint (b);
15523 }
15524 }
15525 else
15526 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
15527 }
15528
15529 /* Helper function for trace_pass_command. */
15530
15531 static void
15532 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
15533 {
15534 tp->pass_count = count;
15535 observer_notify_breakpoint_modified (&tp->base);
15536 if (from_tty)
15537 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
15538 tp->base.number, count);
15539 }
15540
15541 /* Set passcount for tracepoint.
15542
15543 First command argument is passcount, second is tracepoint number.
15544 If tracepoint number omitted, apply to most recently defined.
15545 Also accepts special argument "all". */
15546
15547 static void
15548 trace_pass_command (char *args, int from_tty)
15549 {
15550 struct tracepoint *t1;
15551 unsigned int count;
15552
15553 if (args == 0 || *args == 0)
15554 error (_("passcount command requires an "
15555 "argument (count + optional TP num)"));
15556
15557 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
15558
15559 args = skip_spaces (args);
15560 if (*args && strncasecmp (args, "all", 3) == 0)
15561 {
15562 struct breakpoint *b;
15563
15564 args += 3; /* Skip special argument "all". */
15565 if (*args)
15566 error (_("Junk at end of arguments."));
15567
15568 ALL_TRACEPOINTS (b)
15569 {
15570 t1 = (struct tracepoint *) b;
15571 trace_pass_set_count (t1, count, from_tty);
15572 }
15573 }
15574 else if (*args == '\0')
15575 {
15576 t1 = get_tracepoint_by_number (&args, NULL);
15577 if (t1)
15578 trace_pass_set_count (t1, count, from_tty);
15579 }
15580 else
15581 {
15582 struct get_number_or_range_state state;
15583
15584 init_number_or_range (&state, args);
15585 while (!state.finished)
15586 {
15587 t1 = get_tracepoint_by_number (&args, &state);
15588 if (t1)
15589 trace_pass_set_count (t1, count, from_tty);
15590 }
15591 }
15592 }
15593
15594 struct tracepoint *
15595 get_tracepoint (int num)
15596 {
15597 struct breakpoint *t;
15598
15599 ALL_TRACEPOINTS (t)
15600 if (t->number == num)
15601 return (struct tracepoint *) t;
15602
15603 return NULL;
15604 }
15605
15606 /* Find the tracepoint with the given target-side number (which may be
15607 different from the tracepoint number after disconnecting and
15608 reconnecting). */
15609
15610 struct tracepoint *
15611 get_tracepoint_by_number_on_target (int num)
15612 {
15613 struct breakpoint *b;
15614
15615 ALL_TRACEPOINTS (b)
15616 {
15617 struct tracepoint *t = (struct tracepoint *) b;
15618
15619 if (t->number_on_target == num)
15620 return t;
15621 }
15622
15623 return NULL;
15624 }
15625
15626 /* Utility: parse a tracepoint number and look it up in the list.
15627 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
15628 If the argument is missing, the most recent tracepoint
15629 (tracepoint_count) is returned. */
15630
15631 struct tracepoint *
15632 get_tracepoint_by_number (char **arg,
15633 struct get_number_or_range_state *state)
15634 {
15635 struct breakpoint *t;
15636 int tpnum;
15637 char *instring = arg == NULL ? NULL : *arg;
15638
15639 if (state)
15640 {
15641 gdb_assert (!state->finished);
15642 tpnum = get_number_or_range (state);
15643 }
15644 else if (arg == NULL || *arg == NULL || ! **arg)
15645 tpnum = tracepoint_count;
15646 else
15647 tpnum = get_number (arg);
15648
15649 if (tpnum <= 0)
15650 {
15651 if (instring && *instring)
15652 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15653 instring);
15654 else
15655 printf_filtered (_("No previous tracepoint\n"));
15656 return NULL;
15657 }
15658
15659 ALL_TRACEPOINTS (t)
15660 if (t->number == tpnum)
15661 {
15662 return (struct tracepoint *) t;
15663 }
15664
15665 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15666 return NULL;
15667 }
15668
15669 void
15670 print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15671 {
15672 if (b->thread != -1)
15673 fprintf_unfiltered (fp, " thread %d", b->thread);
15674
15675 if (b->task != 0)
15676 fprintf_unfiltered (fp, " task %d", b->task);
15677
15678 fprintf_unfiltered (fp, "\n");
15679 }
15680
15681 /* Save information on user settable breakpoints (watchpoints, etc) to
15682 a new script file named FILENAME. If FILTER is non-NULL, call it
15683 on each breakpoint and only include the ones for which it returns
15684 non-zero. */
15685
15686 static void
15687 save_breakpoints (char *filename, int from_tty,
15688 int (*filter) (const struct breakpoint *))
15689 {
15690 struct breakpoint *tp;
15691 int any = 0;
15692 struct cleanup *cleanup;
15693 struct ui_file *fp;
15694 int extra_trace_bits = 0;
15695
15696 if (filename == 0 || *filename == 0)
15697 error (_("Argument required (file name in which to save)"));
15698
15699 /* See if we have anything to save. */
15700 ALL_BREAKPOINTS (tp)
15701 {
15702 /* Skip internal and momentary breakpoints. */
15703 if (!user_breakpoint_p (tp))
15704 continue;
15705
15706 /* If we have a filter, only save the breakpoints it accepts. */
15707 if (filter && !filter (tp))
15708 continue;
15709
15710 any = 1;
15711
15712 if (is_tracepoint (tp))
15713 {
15714 extra_trace_bits = 1;
15715
15716 /* We can stop searching. */
15717 break;
15718 }
15719 }
15720
15721 if (!any)
15722 {
15723 warning (_("Nothing to save."));
15724 return;
15725 }
15726
15727 filename = tilde_expand (filename);
15728 cleanup = make_cleanup (xfree, filename);
15729 fp = gdb_fopen (filename, "w");
15730 if (!fp)
15731 error (_("Unable to open file '%s' for saving (%s)"),
15732 filename, safe_strerror (errno));
15733 make_cleanup_ui_file_delete (fp);
15734
15735 if (extra_trace_bits)
15736 save_trace_state_variables (fp);
15737
15738 ALL_BREAKPOINTS (tp)
15739 {
15740 /* Skip internal and momentary breakpoints. */
15741 if (!user_breakpoint_p (tp))
15742 continue;
15743
15744 /* If we have a filter, only save the breakpoints it accepts. */
15745 if (filter && !filter (tp))
15746 continue;
15747
15748 tp->ops->print_recreate (tp, fp);
15749
15750 /* Note, we can't rely on tp->number for anything, as we can't
15751 assume the recreated breakpoint numbers will match. Use $bpnum
15752 instead. */
15753
15754 if (tp->cond_string)
15755 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
15756
15757 if (tp->ignore_count)
15758 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
15759
15760 if (tp->type != bp_dprintf && tp->commands)
15761 {
15762 fprintf_unfiltered (fp, " commands\n");
15763
15764 ui_out_redirect (current_uiout, fp);
15765 TRY
15766 {
15767 print_command_lines (current_uiout, tp->commands->commands, 2);
15768 }
15769 CATCH (ex, RETURN_MASK_ALL)
15770 {
15771 ui_out_redirect (current_uiout, NULL);
15772 throw_exception (ex);
15773 }
15774 END_CATCH
15775
15776 ui_out_redirect (current_uiout, NULL);
15777 fprintf_unfiltered (fp, " end\n");
15778 }
15779
15780 if (tp->enable_state == bp_disabled)
15781 fprintf_unfiltered (fp, "disable $bpnum\n");
15782
15783 /* If this is a multi-location breakpoint, check if the locations
15784 should be individually disabled. Watchpoint locations are
15785 special, and not user visible. */
15786 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15787 {
15788 struct bp_location *loc;
15789 int n = 1;
15790
15791 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15792 if (!loc->enabled)
15793 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
15794 }
15795 }
15796
15797 if (extra_trace_bits && *default_collect)
15798 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
15799
15800 if (from_tty)
15801 printf_filtered (_("Saved to file '%s'.\n"), filename);
15802 do_cleanups (cleanup);
15803 }
15804
15805 /* The `save breakpoints' command. */
15806
15807 static void
15808 save_breakpoints_command (char *args, int from_tty)
15809 {
15810 save_breakpoints (args, from_tty, NULL);
15811 }
15812
15813 /* The `save tracepoints' command. */
15814
15815 static void
15816 save_tracepoints_command (char *args, int from_tty)
15817 {
15818 save_breakpoints (args, from_tty, is_tracepoint);
15819 }
15820
15821 /* Create a vector of all tracepoints. */
15822
15823 VEC(breakpoint_p) *
15824 all_tracepoints (void)
15825 {
15826 VEC(breakpoint_p) *tp_vec = 0;
15827 struct breakpoint *tp;
15828
15829 ALL_TRACEPOINTS (tp)
15830 {
15831 VEC_safe_push (breakpoint_p, tp_vec, tp);
15832 }
15833
15834 return tp_vec;
15835 }
15836
15837 \f
15838 /* This help string is used to consolidate all the help string for specifying
15839 locations used by several commands. */
15840
15841 #define LOCATION_HELP_STRING \
15842 "Linespecs are colon-separated lists of location parameters, such as\n\
15843 source filename, function name, label name, and line number.\n\
15844 Example: To specify the start of a label named \"the_top\" in the\n\
15845 function \"fact\" in the file \"factorial.c\", use\n\
15846 \"factorial.c:fact:the_top\".\n\
15847 \n\
15848 Address locations begin with \"*\" and specify an exact address in the\n\
15849 program. Example: To specify the fourth byte past the start function\n\
15850 \"main\", use \"*main + 4\".\n\
15851 \n\
15852 Explicit locations are similar to linespecs but use an option/argument\n\
15853 syntax to specify location parameters.\n\
15854 Example: To specify the start of the label named \"the_top\" in the\n\
15855 function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
15856 -function fact -label the_top\".\n"
15857
15858 /* This help string is used for the break, hbreak, tbreak and thbreak
15859 commands. It is defined as a macro to prevent duplication.
15860 COMMAND should be a string constant containing the name of the
15861 command. */
15862
15863 #define BREAK_ARGS_HELP(command) \
15864 command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15865 PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15866 probe point. Accepted values are `-probe' (for a generic, automatically\n\
15867 guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15868 `-probe-dtrace' (for a DTrace probe).\n\
15869 LOCATION may be a linespec, address, or explicit location as described\n\
15870 below.\n\
15871 \n\
15872 With no LOCATION, uses current execution address of the selected\n\
15873 stack frame. This is useful for breaking on return to a stack frame.\n\
15874 \n\
15875 THREADNUM is the number from \"info threads\".\n\
15876 CONDITION is a boolean expression.\n\
15877 \n" LOCATION_HELP_STRING "\n\
15878 Multiple breakpoints at one place are permitted, and useful if their\n\
15879 conditions are different.\n\
15880 \n\
15881 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15882
15883 /* List of subcommands for "catch". */
15884 static struct cmd_list_element *catch_cmdlist;
15885
15886 /* List of subcommands for "tcatch". */
15887 static struct cmd_list_element *tcatch_cmdlist;
15888
15889 void
15890 add_catch_command (char *name, char *docstring,
15891 cmd_sfunc_ftype *sfunc,
15892 completer_ftype *completer,
15893 void *user_data_catch,
15894 void *user_data_tcatch)
15895 {
15896 struct cmd_list_element *command;
15897
15898 command = add_cmd (name, class_breakpoint, NULL, docstring,
15899 &catch_cmdlist);
15900 set_cmd_sfunc (command, sfunc);
15901 set_cmd_context (command, user_data_catch);
15902 set_cmd_completer (command, completer);
15903
15904 command = add_cmd (name, class_breakpoint, NULL, docstring,
15905 &tcatch_cmdlist);
15906 set_cmd_sfunc (command, sfunc);
15907 set_cmd_context (command, user_data_tcatch);
15908 set_cmd_completer (command, completer);
15909 }
15910
15911 static void
15912 save_command (char *arg, int from_tty)
15913 {
15914 printf_unfiltered (_("\"save\" must be followed by "
15915 "the name of a save subcommand.\n"));
15916 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
15917 }
15918
15919 struct breakpoint *
15920 iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15921 void *data)
15922 {
15923 struct breakpoint *b, *b_tmp;
15924
15925 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15926 {
15927 if ((*callback) (b, data))
15928 return b;
15929 }
15930
15931 return NULL;
15932 }
15933
15934 /* Zero if any of the breakpoint's locations could be a location where
15935 functions have been inlined, nonzero otherwise. */
15936
15937 static int
15938 is_non_inline_function (struct breakpoint *b)
15939 {
15940 /* The shared library event breakpoint is set on the address of a
15941 non-inline function. */
15942 if (b->type == bp_shlib_event)
15943 return 1;
15944
15945 return 0;
15946 }
15947
15948 /* Nonzero if the specified PC cannot be a location where functions
15949 have been inlined. */
15950
15951 int
15952 pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
15953 const struct target_waitstatus *ws)
15954 {
15955 struct breakpoint *b;
15956 struct bp_location *bl;
15957
15958 ALL_BREAKPOINTS (b)
15959 {
15960 if (!is_non_inline_function (b))
15961 continue;
15962
15963 for (bl = b->loc; bl != NULL; bl = bl->next)
15964 {
15965 if (!bl->shlib_disabled
15966 && bpstat_check_location (bl, aspace, pc, ws))
15967 return 1;
15968 }
15969 }
15970
15971 return 0;
15972 }
15973
15974 /* Remove any references to OBJFILE which is going to be freed. */
15975
15976 void
15977 breakpoint_free_objfile (struct objfile *objfile)
15978 {
15979 struct bp_location **locp, *loc;
15980
15981 ALL_BP_LOCATIONS (loc, locp)
15982 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
15983 loc->symtab = NULL;
15984 }
15985
15986 void
15987 initialize_breakpoint_ops (void)
15988 {
15989 static int initialized = 0;
15990
15991 struct breakpoint_ops *ops;
15992
15993 if (initialized)
15994 return;
15995 initialized = 1;
15996
15997 /* The breakpoint_ops structure to be inherit by all kinds of
15998 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15999 internal and momentary breakpoints, etc.). */
16000 ops = &bkpt_base_breakpoint_ops;
16001 *ops = base_breakpoint_ops;
16002 ops->re_set = bkpt_re_set;
16003 ops->insert_location = bkpt_insert_location;
16004 ops->remove_location = bkpt_remove_location;
16005 ops->breakpoint_hit = bkpt_breakpoint_hit;
16006 ops->create_sals_from_location = bkpt_create_sals_from_location;
16007 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
16008 ops->decode_location = bkpt_decode_location;
16009
16010 /* The breakpoint_ops structure to be used in regular breakpoints. */
16011 ops = &bkpt_breakpoint_ops;
16012 *ops = bkpt_base_breakpoint_ops;
16013 ops->re_set = bkpt_re_set;
16014 ops->resources_needed = bkpt_resources_needed;
16015 ops->print_it = bkpt_print_it;
16016 ops->print_mention = bkpt_print_mention;
16017 ops->print_recreate = bkpt_print_recreate;
16018
16019 /* Ranged breakpoints. */
16020 ops = &ranged_breakpoint_ops;
16021 *ops = bkpt_breakpoint_ops;
16022 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
16023 ops->resources_needed = resources_needed_ranged_breakpoint;
16024 ops->print_it = print_it_ranged_breakpoint;
16025 ops->print_one = print_one_ranged_breakpoint;
16026 ops->print_one_detail = print_one_detail_ranged_breakpoint;
16027 ops->print_mention = print_mention_ranged_breakpoint;
16028 ops->print_recreate = print_recreate_ranged_breakpoint;
16029
16030 /* Internal breakpoints. */
16031 ops = &internal_breakpoint_ops;
16032 *ops = bkpt_base_breakpoint_ops;
16033 ops->re_set = internal_bkpt_re_set;
16034 ops->check_status = internal_bkpt_check_status;
16035 ops->print_it = internal_bkpt_print_it;
16036 ops->print_mention = internal_bkpt_print_mention;
16037
16038 /* Momentary breakpoints. */
16039 ops = &momentary_breakpoint_ops;
16040 *ops = bkpt_base_breakpoint_ops;
16041 ops->re_set = momentary_bkpt_re_set;
16042 ops->check_status = momentary_bkpt_check_status;
16043 ops->print_it = momentary_bkpt_print_it;
16044 ops->print_mention = momentary_bkpt_print_mention;
16045
16046 /* Momentary breakpoints for bp_longjmp and bp_exception. */
16047 ops = &longjmp_breakpoint_ops;
16048 *ops = momentary_breakpoint_ops;
16049 ops->dtor = longjmp_bkpt_dtor;
16050
16051 /* Probe breakpoints. */
16052 ops = &bkpt_probe_breakpoint_ops;
16053 *ops = bkpt_breakpoint_ops;
16054 ops->insert_location = bkpt_probe_insert_location;
16055 ops->remove_location = bkpt_probe_remove_location;
16056 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
16057 ops->decode_location = bkpt_probe_decode_location;
16058
16059 /* Watchpoints. */
16060 ops = &watchpoint_breakpoint_ops;
16061 *ops = base_breakpoint_ops;
16062 ops->dtor = dtor_watchpoint;
16063 ops->re_set = re_set_watchpoint;
16064 ops->insert_location = insert_watchpoint;
16065 ops->remove_location = remove_watchpoint;
16066 ops->breakpoint_hit = breakpoint_hit_watchpoint;
16067 ops->check_status = check_status_watchpoint;
16068 ops->resources_needed = resources_needed_watchpoint;
16069 ops->works_in_software_mode = works_in_software_mode_watchpoint;
16070 ops->print_it = print_it_watchpoint;
16071 ops->print_mention = print_mention_watchpoint;
16072 ops->print_recreate = print_recreate_watchpoint;
16073 ops->explains_signal = explains_signal_watchpoint;
16074
16075 /* Masked watchpoints. */
16076 ops = &masked_watchpoint_breakpoint_ops;
16077 *ops = watchpoint_breakpoint_ops;
16078 ops->insert_location = insert_masked_watchpoint;
16079 ops->remove_location = remove_masked_watchpoint;
16080 ops->resources_needed = resources_needed_masked_watchpoint;
16081 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
16082 ops->print_it = print_it_masked_watchpoint;
16083 ops->print_one_detail = print_one_detail_masked_watchpoint;
16084 ops->print_mention = print_mention_masked_watchpoint;
16085 ops->print_recreate = print_recreate_masked_watchpoint;
16086
16087 /* Tracepoints. */
16088 ops = &tracepoint_breakpoint_ops;
16089 *ops = base_breakpoint_ops;
16090 ops->re_set = tracepoint_re_set;
16091 ops->breakpoint_hit = tracepoint_breakpoint_hit;
16092 ops->print_one_detail = tracepoint_print_one_detail;
16093 ops->print_mention = tracepoint_print_mention;
16094 ops->print_recreate = tracepoint_print_recreate;
16095 ops->create_sals_from_location = tracepoint_create_sals_from_location;
16096 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
16097 ops->decode_location = tracepoint_decode_location;
16098
16099 /* Probe tracepoints. */
16100 ops = &tracepoint_probe_breakpoint_ops;
16101 *ops = tracepoint_breakpoint_ops;
16102 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
16103 ops->decode_location = tracepoint_probe_decode_location;
16104
16105 /* Static tracepoints with marker (`-m'). */
16106 ops = &strace_marker_breakpoint_ops;
16107 *ops = tracepoint_breakpoint_ops;
16108 ops->create_sals_from_location = strace_marker_create_sals_from_location;
16109 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
16110 ops->decode_location = strace_marker_decode_location;
16111
16112 /* Fork catchpoints. */
16113 ops = &catch_fork_breakpoint_ops;
16114 *ops = base_breakpoint_ops;
16115 ops->insert_location = insert_catch_fork;
16116 ops->remove_location = remove_catch_fork;
16117 ops->breakpoint_hit = breakpoint_hit_catch_fork;
16118 ops->print_it = print_it_catch_fork;
16119 ops->print_one = print_one_catch_fork;
16120 ops->print_mention = print_mention_catch_fork;
16121 ops->print_recreate = print_recreate_catch_fork;
16122
16123 /* Vfork catchpoints. */
16124 ops = &catch_vfork_breakpoint_ops;
16125 *ops = base_breakpoint_ops;
16126 ops->insert_location = insert_catch_vfork;
16127 ops->remove_location = remove_catch_vfork;
16128 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
16129 ops->print_it = print_it_catch_vfork;
16130 ops->print_one = print_one_catch_vfork;
16131 ops->print_mention = print_mention_catch_vfork;
16132 ops->print_recreate = print_recreate_catch_vfork;
16133
16134 /* Exec catchpoints. */
16135 ops = &catch_exec_breakpoint_ops;
16136 *ops = base_breakpoint_ops;
16137 ops->dtor = dtor_catch_exec;
16138 ops->insert_location = insert_catch_exec;
16139 ops->remove_location = remove_catch_exec;
16140 ops->breakpoint_hit = breakpoint_hit_catch_exec;
16141 ops->print_it = print_it_catch_exec;
16142 ops->print_one = print_one_catch_exec;
16143 ops->print_mention = print_mention_catch_exec;
16144 ops->print_recreate = print_recreate_catch_exec;
16145
16146 /* Solib-related catchpoints. */
16147 ops = &catch_solib_breakpoint_ops;
16148 *ops = base_breakpoint_ops;
16149 ops->dtor = dtor_catch_solib;
16150 ops->insert_location = insert_catch_solib;
16151 ops->remove_location = remove_catch_solib;
16152 ops->breakpoint_hit = breakpoint_hit_catch_solib;
16153 ops->check_status = check_status_catch_solib;
16154 ops->print_it = print_it_catch_solib;
16155 ops->print_one = print_one_catch_solib;
16156 ops->print_mention = print_mention_catch_solib;
16157 ops->print_recreate = print_recreate_catch_solib;
16158
16159 ops = &dprintf_breakpoint_ops;
16160 *ops = bkpt_base_breakpoint_ops;
16161 ops->re_set = dprintf_re_set;
16162 ops->resources_needed = bkpt_resources_needed;
16163 ops->print_it = bkpt_print_it;
16164 ops->print_mention = bkpt_print_mention;
16165 ops->print_recreate = dprintf_print_recreate;
16166 ops->after_condition_true = dprintf_after_condition_true;
16167 ops->breakpoint_hit = dprintf_breakpoint_hit;
16168 }
16169
16170 /* Chain containing all defined "enable breakpoint" subcommands. */
16171
16172 static struct cmd_list_element *enablebreaklist = NULL;
16173
16174 void
16175 _initialize_breakpoint (void)
16176 {
16177 struct cmd_list_element *c;
16178
16179 initialize_breakpoint_ops ();
16180
16181 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
16182 observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
16183 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
16184
16185 breakpoint_objfile_key
16186 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
16187
16188 breakpoint_chain = 0;
16189 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
16190 before a breakpoint is set. */
16191 breakpoint_count = 0;
16192
16193 tracepoint_count = 0;
16194
16195 add_com ("ignore", class_breakpoint, ignore_command, _("\
16196 Set ignore-count of breakpoint number N to COUNT.\n\
16197 Usage is `ignore N COUNT'."));
16198
16199 add_com ("commands", class_breakpoint, commands_command, _("\
16200 Set commands to be executed when a breakpoint is hit.\n\
16201 Give breakpoint number as argument after \"commands\".\n\
16202 With no argument, the targeted breakpoint is the last one set.\n\
16203 The commands themselves follow starting on the next line.\n\
16204 Type a line containing \"end\" to indicate the end of them.\n\
16205 Give \"silent\" as the first line to make the breakpoint silent;\n\
16206 then no output is printed when it is hit, except what the commands print."));
16207
16208 c = add_com ("condition", class_breakpoint, condition_command, _("\
16209 Specify breakpoint number N to break only if COND is true.\n\
16210 Usage is `condition N COND', where N is an integer and COND is an\n\
16211 expression to be evaluated whenever breakpoint N is reached."));
16212 set_cmd_completer (c, condition_completer);
16213
16214 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
16215 Set a temporary breakpoint.\n\
16216 Like \"break\" except the breakpoint is only temporary,\n\
16217 so it will be deleted when hit. Equivalent to \"break\" followed\n\
16218 by using \"enable delete\" on the breakpoint number.\n\
16219 \n"
16220 BREAK_ARGS_HELP ("tbreak")));
16221 set_cmd_completer (c, location_completer);
16222
16223 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
16224 Set a hardware assisted breakpoint.\n\
16225 Like \"break\" except the breakpoint requires hardware support,\n\
16226 some target hardware may not have this support.\n\
16227 \n"
16228 BREAK_ARGS_HELP ("hbreak")));
16229 set_cmd_completer (c, location_completer);
16230
16231 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
16232 Set a temporary hardware assisted breakpoint.\n\
16233 Like \"hbreak\" except the breakpoint is only temporary,\n\
16234 so it will be deleted when hit.\n\
16235 \n"
16236 BREAK_ARGS_HELP ("thbreak")));
16237 set_cmd_completer (c, location_completer);
16238
16239 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
16240 Enable some breakpoints.\n\
16241 Give breakpoint numbers (separated by spaces) as arguments.\n\
16242 With no subcommand, breakpoints are enabled until you command otherwise.\n\
16243 This is used to cancel the effect of the \"disable\" command.\n\
16244 With a subcommand you can enable temporarily."),
16245 &enablelist, "enable ", 1, &cmdlist);
16246
16247 add_com_alias ("en", "enable", class_breakpoint, 1);
16248
16249 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
16250 Enable some breakpoints.\n\
16251 Give breakpoint numbers (separated by spaces) as arguments.\n\
16252 This is used to cancel the effect of the \"disable\" command.\n\
16253 May be abbreviated to simply \"enable\".\n"),
16254 &enablebreaklist, "enable breakpoints ", 1, &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 &enablebreaklist);
16260
16261 add_cmd ("delete", no_class, enable_delete_command, _("\
16262 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16263 If a breakpoint is hit while enabled in this fashion, it is deleted."),
16264 &enablebreaklist);
16265
16266 add_cmd ("count", no_class, enable_count_command, _("\
16267 Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16268 If a breakpoint is hit while enabled in this fashion,\n\
16269 the count is decremented; when it reaches zero, the breakpoint is disabled."),
16270 &enablebreaklist);
16271
16272 add_cmd ("delete", no_class, enable_delete_command, _("\
16273 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16274 If a breakpoint is hit while enabled in this fashion, it is deleted."),
16275 &enablelist);
16276
16277 add_cmd ("once", no_class, enable_once_command, _("\
16278 Enable breakpoints for one hit. Give breakpoint numbers.\n\
16279 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
16280 &enablelist);
16281
16282 add_cmd ("count", no_class, enable_count_command, _("\
16283 Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16284 If a breakpoint is hit while enabled in this fashion,\n\
16285 the count is decremented; when it reaches zero, the breakpoint is disabled."),
16286 &enablelist);
16287
16288 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
16289 Disable some breakpoints.\n\
16290 Arguments are breakpoint numbers with spaces in between.\n\
16291 To disable all breakpoints, give no argument.\n\
16292 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
16293 &disablelist, "disable ", 1, &cmdlist);
16294 add_com_alias ("dis", "disable", class_breakpoint, 1);
16295 add_com_alias ("disa", "disable", class_breakpoint, 1);
16296
16297 add_cmd ("breakpoints", class_alias, disable_command, _("\
16298 Disable some breakpoints.\n\
16299 Arguments are breakpoint numbers with spaces in between.\n\
16300 To disable all breakpoints, give no argument.\n\
16301 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
16302 This command may be abbreviated \"disable\"."),
16303 &disablelist);
16304
16305 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
16306 Delete some breakpoints or auto-display expressions.\n\
16307 Arguments are breakpoint numbers with spaces in between.\n\
16308 To delete all breakpoints, give no argument.\n\
16309 \n\
16310 Also a prefix command for deletion of other GDB objects.\n\
16311 The \"unset\" command is also an alias for \"delete\"."),
16312 &deletelist, "delete ", 1, &cmdlist);
16313 add_com_alias ("d", "delete", class_breakpoint, 1);
16314 add_com_alias ("del", "delete", class_breakpoint, 1);
16315
16316 add_cmd ("breakpoints", class_alias, delete_command, _("\
16317 Delete some breakpoints or auto-display expressions.\n\
16318 Arguments are breakpoint numbers with spaces in between.\n\
16319 To delete all breakpoints, give no argument.\n\
16320 This command may be abbreviated \"delete\"."),
16321 &deletelist);
16322
16323 add_com ("clear", class_breakpoint, clear_command, _("\
16324 Clear breakpoint at specified location.\n\
16325 Argument may be a linespec, explicit, or address location as described below.\n\
16326 \n\
16327 With no argument, clears all breakpoints in the line that the selected frame\n\
16328 is executing in.\n"
16329 "\n" LOCATION_HELP_STRING "\n\
16330 See also the \"delete\" command which clears breakpoints by number."));
16331 add_com_alias ("cl", "clear", class_breakpoint, 1);
16332
16333 c = add_com ("break", class_breakpoint, break_command, _("\
16334 Set breakpoint at specified location.\n"
16335 BREAK_ARGS_HELP ("break")));
16336 set_cmd_completer (c, location_completer);
16337
16338 add_com_alias ("b", "break", class_run, 1);
16339 add_com_alias ("br", "break", class_run, 1);
16340 add_com_alias ("bre", "break", class_run, 1);
16341 add_com_alias ("brea", "break", class_run, 1);
16342
16343 if (dbx_commands)
16344 {
16345 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
16346 Break in function/address or break at a line in the current file."),
16347 &stoplist, "stop ", 1, &cmdlist);
16348 add_cmd ("in", class_breakpoint, stopin_command,
16349 _("Break in function or address."), &stoplist);
16350 add_cmd ("at", class_breakpoint, stopat_command,
16351 _("Break at a line in the current file."), &stoplist);
16352 add_com ("status", class_info, breakpoints_info, _("\
16353 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
16354 The \"Type\" column indicates one of:\n\
16355 \tbreakpoint - normal breakpoint\n\
16356 \twatchpoint - watchpoint\n\
16357 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16358 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16359 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16360 address and file/line number respectively.\n\
16361 \n\
16362 Convenience variable \"$_\" and default examine address for \"x\"\n\
16363 are set to the address of the last breakpoint listed unless the command\n\
16364 is prefixed with \"server \".\n\n\
16365 Convenience variable \"$bpnum\" contains the number of the last\n\
16366 breakpoint set."));
16367 }
16368
16369 add_info ("breakpoints", breakpoints_info, _("\
16370 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
16371 The \"Type\" column indicates one of:\n\
16372 \tbreakpoint - normal breakpoint\n\
16373 \twatchpoint - watchpoint\n\
16374 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16375 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16376 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16377 address and file/line number respectively.\n\
16378 \n\
16379 Convenience variable \"$_\" and default examine address for \"x\"\n\
16380 are set to the address of the last breakpoint listed unless the command\n\
16381 is prefixed with \"server \".\n\n\
16382 Convenience variable \"$bpnum\" contains the number of the last\n\
16383 breakpoint set."));
16384
16385 add_info_alias ("b", "breakpoints", 1);
16386
16387 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
16388 Status of all breakpoints, or breakpoint number NUMBER.\n\
16389 The \"Type\" column indicates one of:\n\
16390 \tbreakpoint - normal breakpoint\n\
16391 \twatchpoint - watchpoint\n\
16392 \tlongjmp - internal breakpoint used to step through longjmp()\n\
16393 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
16394 \tuntil - internal breakpoint used by the \"until\" command\n\
16395 \tfinish - internal breakpoint used by the \"finish\" command\n\
16396 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16397 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16398 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16399 address and file/line number respectively.\n\
16400 \n\
16401 Convenience variable \"$_\" and default examine address for \"x\"\n\
16402 are set to the address of the last breakpoint listed unless the command\n\
16403 is prefixed with \"server \".\n\n\
16404 Convenience variable \"$bpnum\" contains the number of the last\n\
16405 breakpoint set."),
16406 &maintenanceinfolist);
16407
16408 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
16409 Set catchpoints to catch events."),
16410 &catch_cmdlist, "catch ",
16411 0/*allow-unknown*/, &cmdlist);
16412
16413 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
16414 Set temporary catchpoints to catch events."),
16415 &tcatch_cmdlist, "tcatch ",
16416 0/*allow-unknown*/, &cmdlist);
16417
16418 add_catch_command ("fork", _("Catch calls to fork."),
16419 catch_fork_command_1,
16420 NULL,
16421 (void *) (uintptr_t) catch_fork_permanent,
16422 (void *) (uintptr_t) catch_fork_temporary);
16423 add_catch_command ("vfork", _("Catch calls to vfork."),
16424 catch_fork_command_1,
16425 NULL,
16426 (void *) (uintptr_t) catch_vfork_permanent,
16427 (void *) (uintptr_t) catch_vfork_temporary);
16428 add_catch_command ("exec", _("Catch calls to exec."),
16429 catch_exec_command_1,
16430 NULL,
16431 CATCH_PERMANENT,
16432 CATCH_TEMPORARY);
16433 add_catch_command ("load", _("Catch loads of shared libraries.\n\
16434 Usage: catch load [REGEX]\n\
16435 If REGEX is given, only stop for libraries matching the regular expression."),
16436 catch_load_command_1,
16437 NULL,
16438 CATCH_PERMANENT,
16439 CATCH_TEMPORARY);
16440 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
16441 Usage: catch unload [REGEX]\n\
16442 If REGEX is given, only stop for libraries matching the regular expression."),
16443 catch_unload_command_1,
16444 NULL,
16445 CATCH_PERMANENT,
16446 CATCH_TEMPORARY);
16447
16448 c = add_com ("watch", class_breakpoint, watch_command, _("\
16449 Set a watchpoint for an expression.\n\
16450 Usage: watch [-l|-location] EXPRESSION\n\
16451 A watchpoint stops execution of your program whenever the value of\n\
16452 an expression changes.\n\
16453 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16454 the memory to which it refers."));
16455 set_cmd_completer (c, expression_completer);
16456
16457 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
16458 Set a read watchpoint for an expression.\n\
16459 Usage: rwatch [-l|-location] EXPRESSION\n\
16460 A watchpoint stops execution of your program whenever the value of\n\
16461 an expression is read.\n\
16462 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16463 the memory to which it refers."));
16464 set_cmd_completer (c, expression_completer);
16465
16466 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
16467 Set a watchpoint for an expression.\n\
16468 Usage: awatch [-l|-location] EXPRESSION\n\
16469 A watchpoint stops execution of your program whenever the value of\n\
16470 an expression is either read or written.\n\
16471 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16472 the memory to which it refers."));
16473 set_cmd_completer (c, expression_completer);
16474
16475 add_info ("watchpoints", watchpoints_info, _("\
16476 Status of specified watchpoints (all watchpoints if no argument)."));
16477
16478 /* XXX: cagney/2005-02-23: This should be a boolean, and should
16479 respond to changes - contrary to the description. */
16480 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
16481 &can_use_hw_watchpoints, _("\
16482 Set debugger's willingness to use watchpoint hardware."), _("\
16483 Show debugger's willingness to use watchpoint hardware."), _("\
16484 If zero, gdb will not use hardware for new watchpoints, even if\n\
16485 such is available. (However, any hardware watchpoints that were\n\
16486 created before setting this to nonzero, will continue to use watchpoint\n\
16487 hardware.)"),
16488 NULL,
16489 show_can_use_hw_watchpoints,
16490 &setlist, &showlist);
16491
16492 can_use_hw_watchpoints = 1;
16493
16494 /* Tracepoint manipulation commands. */
16495
16496 c = add_com ("trace", class_breakpoint, trace_command, _("\
16497 Set a tracepoint at specified location.\n\
16498 \n"
16499 BREAK_ARGS_HELP ("trace") "\n\
16500 Do \"help tracepoints\" for info on other tracepoint commands."));
16501 set_cmd_completer (c, location_completer);
16502
16503 add_com_alias ("tp", "trace", class_alias, 0);
16504 add_com_alias ("tr", "trace", class_alias, 1);
16505 add_com_alias ("tra", "trace", class_alias, 1);
16506 add_com_alias ("trac", "trace", class_alias, 1);
16507
16508 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
16509 Set a fast tracepoint at specified location.\n\
16510 \n"
16511 BREAK_ARGS_HELP ("ftrace") "\n\
16512 Do \"help tracepoints\" for info on other tracepoint commands."));
16513 set_cmd_completer (c, location_completer);
16514
16515 c = add_com ("strace", class_breakpoint, strace_command, _("\
16516 Set a static tracepoint at location or marker.\n\
16517 \n\
16518 strace [LOCATION] [if CONDITION]\n\
16519 LOCATION may be a linespec, explicit, or address location (described below) \n\
16520 or -m MARKER_ID.\n\n\
16521 If a marker id is specified, probe the marker with that name. With\n\
16522 no LOCATION, uses current execution address of the selected stack frame.\n\
16523 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16524 This collects arbitrary user data passed in the probe point call to the\n\
16525 tracing library. You can inspect it when analyzing the trace buffer,\n\
16526 by printing the $_sdata variable like any other convenience variable.\n\
16527 \n\
16528 CONDITION is a boolean expression.\n\
16529 \n" LOCATION_HELP_STRING "\n\
16530 Multiple tracepoints at one place are permitted, and useful if their\n\
16531 conditions are different.\n\
16532 \n\
16533 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16534 Do \"help tracepoints\" for info on other tracepoint commands."));
16535 set_cmd_completer (c, location_completer);
16536
16537 add_info ("tracepoints", tracepoints_info, _("\
16538 Status of specified tracepoints (all tracepoints if no argument).\n\
16539 Convenience variable \"$tpnum\" contains the number of the\n\
16540 last tracepoint set."));
16541
16542 add_info_alias ("tp", "tracepoints", 1);
16543
16544 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16545 Delete specified tracepoints.\n\
16546 Arguments are tracepoint numbers, separated by spaces.\n\
16547 No argument means delete all tracepoints."),
16548 &deletelist);
16549 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
16550
16551 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16552 Disable specified tracepoints.\n\
16553 Arguments are tracepoint numbers, separated by spaces.\n\
16554 No argument means disable all tracepoints."),
16555 &disablelist);
16556 deprecate_cmd (c, "disable");
16557
16558 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16559 Enable specified tracepoints.\n\
16560 Arguments are tracepoint numbers, separated by spaces.\n\
16561 No argument means enable all tracepoints."),
16562 &enablelist);
16563 deprecate_cmd (c, "enable");
16564
16565 add_com ("passcount", class_trace, trace_pass_command, _("\
16566 Set the passcount for a tracepoint.\n\
16567 The trace will end when the tracepoint has been passed 'count' times.\n\
16568 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16569 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16570
16571 add_prefix_cmd ("save", class_breakpoint, save_command,
16572 _("Save breakpoint definitions as a script."),
16573 &save_cmdlist, "save ",
16574 0/*allow-unknown*/, &cmdlist);
16575
16576 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16577 Save current breakpoint definitions as a script.\n\
16578 This includes all types of breakpoints (breakpoints, watchpoints,\n\
16579 catchpoints, tracepoints). Use the 'source' command in another debug\n\
16580 session to restore them."),
16581 &save_cmdlist);
16582 set_cmd_completer (c, filename_completer);
16583
16584 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
16585 Save current tracepoint definitions as a script.\n\
16586 Use the 'source' command in another debug session to restore them."),
16587 &save_cmdlist);
16588 set_cmd_completer (c, filename_completer);
16589
16590 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16591 deprecate_cmd (c, "save tracepoints");
16592
16593 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
16594 Breakpoint specific settings\n\
16595 Configure various breakpoint-specific variables such as\n\
16596 pending breakpoint behavior"),
16597 &breakpoint_set_cmdlist, "set breakpoint ",
16598 0/*allow-unknown*/, &setlist);
16599 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
16600 Breakpoint specific settings\n\
16601 Configure various breakpoint-specific variables such as\n\
16602 pending breakpoint behavior"),
16603 &breakpoint_show_cmdlist, "show breakpoint ",
16604 0/*allow-unknown*/, &showlist);
16605
16606 add_setshow_auto_boolean_cmd ("pending", no_class,
16607 &pending_break_support, _("\
16608 Set debugger's behavior regarding pending breakpoints."), _("\
16609 Show debugger's behavior regarding pending breakpoints."), _("\
16610 If on, an unrecognized breakpoint location will cause gdb to create a\n\
16611 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16612 an error. If auto, an unrecognized breakpoint location results in a\n\
16613 user-query to see if a pending breakpoint should be created."),
16614 NULL,
16615 show_pending_break_support,
16616 &breakpoint_set_cmdlist,
16617 &breakpoint_show_cmdlist);
16618
16619 pending_break_support = AUTO_BOOLEAN_AUTO;
16620
16621 add_setshow_boolean_cmd ("auto-hw", no_class,
16622 &automatic_hardware_breakpoints, _("\
16623 Set automatic usage of hardware breakpoints."), _("\
16624 Show automatic usage of hardware breakpoints."), _("\
16625 If set, the debugger will automatically use hardware breakpoints for\n\
16626 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16627 a warning will be emitted for such breakpoints."),
16628 NULL,
16629 show_automatic_hardware_breakpoints,
16630 &breakpoint_set_cmdlist,
16631 &breakpoint_show_cmdlist);
16632
16633 add_setshow_boolean_cmd ("always-inserted", class_support,
16634 &always_inserted_mode, _("\
16635 Set mode for inserting breakpoints."), _("\
16636 Show mode for inserting breakpoints."), _("\
16637 When this mode is on, breakpoints are inserted immediately as soon as\n\
16638 they're created, kept inserted even when execution stops, and removed\n\
16639 only when the user deletes them. When this mode is off (the default),\n\
16640 breakpoints are inserted only when execution continues, and removed\n\
16641 when execution stops."),
16642 NULL,
16643 &show_always_inserted_mode,
16644 &breakpoint_set_cmdlist,
16645 &breakpoint_show_cmdlist);
16646
16647 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16648 condition_evaluation_enums,
16649 &condition_evaluation_mode_1, _("\
16650 Set mode of breakpoint condition evaluation."), _("\
16651 Show mode of breakpoint condition evaluation."), _("\
16652 When this is set to \"host\", breakpoint conditions will be\n\
16653 evaluated on the host's side by GDB. When it is set to \"target\",\n\
16654 breakpoint conditions will be downloaded to the target (if the target\n\
16655 supports such feature) and conditions will be evaluated on the target's side.\n\
16656 If this is set to \"auto\" (default), this will be automatically set to\n\
16657 \"target\" if it supports condition evaluation, otherwise it will\n\
16658 be set to \"gdb\""),
16659 &set_condition_evaluation_mode,
16660 &show_condition_evaluation_mode,
16661 &breakpoint_set_cmdlist,
16662 &breakpoint_show_cmdlist);
16663
16664 add_com ("break-range", class_breakpoint, break_range_command, _("\
16665 Set a breakpoint for an address range.\n\
16666 break-range START-LOCATION, END-LOCATION\n\
16667 where START-LOCATION and END-LOCATION can be one of the following:\n\
16668 LINENUM, for that line in the current file,\n\
16669 FILE:LINENUM, for that line in that file,\n\
16670 +OFFSET, for that number of lines after the current line\n\
16671 or the start of the range\n\
16672 FUNCTION, for the first line in that function,\n\
16673 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16674 *ADDRESS, for the instruction at that address.\n\
16675 \n\
16676 The breakpoint will stop execution of the inferior whenever it executes\n\
16677 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16678 range (including START-LOCATION and END-LOCATION)."));
16679
16680 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
16681 Set a dynamic printf at specified location.\n\
16682 dprintf location,format string,arg1,arg2,...\n\
16683 location may be a linespec, explicit, or address location.\n"
16684 "\n" LOCATION_HELP_STRING));
16685 set_cmd_completer (c, location_completer);
16686
16687 add_setshow_enum_cmd ("dprintf-style", class_support,
16688 dprintf_style_enums, &dprintf_style, _("\
16689 Set the style of usage for dynamic printf."), _("\
16690 Show the style of usage for dynamic printf."), _("\
16691 This setting chooses how GDB will do a dynamic printf.\n\
16692 If the value is \"gdb\", then the printing is done by GDB to its own\n\
16693 console, as with the \"printf\" command.\n\
16694 If the value is \"call\", the print is done by calling a function in your\n\
16695 program; by default printf(), but you can choose a different function or\n\
16696 output stream by setting dprintf-function and dprintf-channel."),
16697 update_dprintf_commands, NULL,
16698 &setlist, &showlist);
16699
16700 dprintf_function = xstrdup ("printf");
16701 add_setshow_string_cmd ("dprintf-function", class_support,
16702 &dprintf_function, _("\
16703 Set the function to use for dynamic printf"), _("\
16704 Show the function to use for dynamic printf"), NULL,
16705 update_dprintf_commands, NULL,
16706 &setlist, &showlist);
16707
16708 dprintf_channel = xstrdup ("");
16709 add_setshow_string_cmd ("dprintf-channel", class_support,
16710 &dprintf_channel, _("\
16711 Set the channel to use for dynamic printf"), _("\
16712 Show the channel to use for dynamic printf"), NULL,
16713 update_dprintf_commands, NULL,
16714 &setlist, &showlist);
16715
16716 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16717 &disconnected_dprintf, _("\
16718 Set whether dprintf continues after GDB disconnects."), _("\
16719 Show whether dprintf continues after GDB disconnects."), _("\
16720 Use this to let dprintf commands continue to hit and produce output\n\
16721 even if GDB disconnects or detaches from the target."),
16722 NULL,
16723 NULL,
16724 &setlist, &showlist);
16725
16726 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16727 agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16728 (target agent only) This is useful for formatted output in user-defined commands."));
16729
16730 automatic_hardware_breakpoints = 1;
16731
16732 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
16733 observer_attach_thread_exit (remove_threaded_breakpoints);
16734 }
This page took 0.392553 seconds and 4 git commands to generate.