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