Remove make_cleanup_restore_current_ui
[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 #include <algorithm>
84
85 /* Enums for exception-handling support. */
86 enum exception_event_kind
87 {
88 EX_EVENT_THROW,
89 EX_EVENT_RETHROW,
90 EX_EVENT_CATCH
91 };
92
93 /* Prototypes for local functions. */
94
95 static void enable_delete_command (char *, int);
96
97 static void enable_once_command (char *, int);
98
99 static void enable_count_command (char *, int);
100
101 static void disable_command (char *, int);
102
103 static void enable_command (char *, int);
104
105 static void map_breakpoint_numbers (char *, void (*) (struct breakpoint *,
106 void *),
107 void *);
108
109 static void ignore_command (char *, int);
110
111 static int breakpoint_re_set_one (void *);
112
113 static void breakpoint_re_set_default (struct breakpoint *);
114
115 static void
116 create_sals_from_location_default (const struct event_location *location,
117 struct linespec_result *canonical,
118 enum bptype type_wanted);
119
120 static void create_breakpoints_sal_default (struct gdbarch *,
121 struct linespec_result *,
122 char *, char *, enum bptype,
123 enum bpdisp, int, int,
124 int,
125 const struct breakpoint_ops *,
126 int, int, int, unsigned);
127
128 static void decode_location_default (struct breakpoint *b,
129 const struct event_location *location,
130 struct program_space *search_pspace,
131 struct symtabs_and_lines *sals);
132
133 static void clear_command (char *, int);
134
135 static void catch_command (char *, int);
136
137 static int can_use_hardware_watchpoint (struct value *);
138
139 static void break_command_1 (char *, int, int);
140
141 static void mention (struct breakpoint *);
142
143 static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
144 enum bptype,
145 const struct breakpoint_ops *);
146 static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
147 const struct symtab_and_line *);
148
149 /* This function is used in gdbtk sources and thus can not be made
150 static. */
151 struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
152 struct symtab_and_line,
153 enum bptype,
154 const struct breakpoint_ops *);
155
156 static struct breakpoint *
157 momentary_breakpoint_from_master (struct breakpoint *orig,
158 enum bptype type,
159 const struct breakpoint_ops *ops,
160 int loc_enabled);
161
162 static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
163
164 static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
165 CORE_ADDR bpaddr,
166 enum bptype bptype);
167
168 static void describe_other_breakpoints (struct gdbarch *,
169 struct program_space *, CORE_ADDR,
170 struct obj_section *, int);
171
172 static int watchpoint_locations_match (struct bp_location *loc1,
173 struct bp_location *loc2);
174
175 static int breakpoint_location_address_match (struct bp_location *bl,
176 struct address_space *aspace,
177 CORE_ADDR addr);
178
179 static int breakpoint_location_address_range_overlap (struct bp_location *,
180 struct address_space *,
181 CORE_ADDR, int);
182
183 static void breakpoints_info (char *, int);
184
185 static void watchpoints_info (char *, int);
186
187 static int breakpoint_1 (char *, int,
188 int (*) (const struct breakpoint *));
189
190 static int breakpoint_cond_eval (void *);
191
192 static void cleanup_executing_breakpoints (void *);
193
194 static void commands_command (char *, int);
195
196 static void condition_command (char *, int);
197
198 static int remove_breakpoint (struct bp_location *);
199 static int remove_breakpoint_1 (struct bp_location *, enum remove_bp_reason);
200
201 static enum print_stop_action print_bp_stop_message (bpstat bs);
202
203 static int watchpoint_check (void *);
204
205 static void maintenance_info_breakpoints (char *, int);
206
207 static int hw_breakpoint_used_count (void);
208
209 static int hw_watchpoint_use_count (struct breakpoint *);
210
211 static int hw_watchpoint_used_count_others (struct breakpoint *except,
212 enum bptype type,
213 int *other_type_used);
214
215 static void hbreak_command (char *, int);
216
217 static void thbreak_command (char *, int);
218
219 static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
220 int count);
221
222 static void stop_command (char *arg, int from_tty);
223
224 static void stopin_command (char *arg, int from_tty);
225
226 static void stopat_command (char *arg, int from_tty);
227
228 static void tcatch_command (char *arg, int from_tty);
229
230 static void free_bp_location (struct bp_location *loc);
231 static void incref_bp_location (struct bp_location *loc);
232 static void decref_bp_location (struct bp_location **loc);
233
234 static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
235
236 /* update_global_location_list's modes of operation wrt to whether to
237 insert locations now. */
238 enum ugll_insert_mode
239 {
240 /* Don't insert any breakpoint locations into the inferior, only
241 remove already-inserted locations that no longer should be
242 inserted. Functions that delete a breakpoint or breakpoints
243 should specify this mode, so that deleting a breakpoint doesn't
244 have the side effect of inserting the locations of other
245 breakpoints that are marked not-inserted, but should_be_inserted
246 returns true on them.
247
248 This behavior is useful is situations close to tear-down -- e.g.,
249 after an exec, while the target still has execution, but
250 breakpoint shadows of the previous executable image should *NOT*
251 be restored to the new image; or before detaching, where the
252 target still has execution and wants to delete breakpoints from
253 GDB's lists, and all breakpoints had already been removed from
254 the inferior. */
255 UGLL_DONT_INSERT,
256
257 /* May insert breakpoints iff breakpoints_should_be_inserted_now
258 claims breakpoints should be inserted now. */
259 UGLL_MAY_INSERT,
260
261 /* Insert locations now, irrespective of
262 breakpoints_should_be_inserted_now. E.g., say all threads are
263 stopped right now, and the user did "continue". We need to
264 insert breakpoints _before_ resuming the target, but
265 UGLL_MAY_INSERT wouldn't insert them, because
266 breakpoints_should_be_inserted_now returns false at that point,
267 as no thread is running yet. */
268 UGLL_INSERT
269 };
270
271 static void update_global_location_list (enum ugll_insert_mode);
272
273 static void update_global_location_list_nothrow (enum ugll_insert_mode);
274
275 static int is_hardware_watchpoint (const struct breakpoint *bpt);
276
277 static void insert_breakpoint_locations (void);
278
279 static void tracepoints_info (char *, int);
280
281 static void delete_trace_command (char *, int);
282
283 static void enable_trace_command (char *, int);
284
285 static void disable_trace_command (char *, int);
286
287 static void trace_pass_command (char *, int);
288
289 static void set_tracepoint_count (int num);
290
291 static int is_masked_watchpoint (const struct breakpoint *b);
292
293 static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
294
295 /* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
296 otherwise. */
297
298 static int strace_marker_p (struct breakpoint *b);
299
300 /* The breakpoint_ops structure to be inherited by all breakpoint_ops
301 that are implemented on top of software or hardware breakpoints
302 (user breakpoints, internal and momentary breakpoints, etc.). */
303 static struct breakpoint_ops bkpt_base_breakpoint_ops;
304
305 /* Internal breakpoints class type. */
306 static struct breakpoint_ops internal_breakpoint_ops;
307
308 /* Momentary breakpoints class type. */
309 static struct breakpoint_ops momentary_breakpoint_ops;
310
311 /* Momentary breakpoints for bp_longjmp and bp_exception class type. */
312 static struct breakpoint_ops longjmp_breakpoint_ops;
313
314 /* The breakpoint_ops structure to be used in regular user created
315 breakpoints. */
316 struct breakpoint_ops bkpt_breakpoint_ops;
317
318 /* Breakpoints set on probes. */
319 static struct breakpoint_ops bkpt_probe_breakpoint_ops;
320
321 /* Dynamic printf class type. */
322 struct breakpoint_ops dprintf_breakpoint_ops;
323
324 /* The style in which to perform a dynamic printf. This is a user
325 option because different output options have different tradeoffs;
326 if GDB does the printing, there is better error handling if there
327 is a problem with any of the arguments, but using an inferior
328 function lets you have special-purpose printers and sending of
329 output to the same place as compiled-in print functions. */
330
331 static const char dprintf_style_gdb[] = "gdb";
332 static const char dprintf_style_call[] = "call";
333 static const char dprintf_style_agent[] = "agent";
334 static const char *const dprintf_style_enums[] = {
335 dprintf_style_gdb,
336 dprintf_style_call,
337 dprintf_style_agent,
338 NULL
339 };
340 static const char *dprintf_style = dprintf_style_gdb;
341
342 /* The function to use for dynamic printf if the preferred style is to
343 call into the inferior. The value is simply a string that is
344 copied into the command, so it can be anything that GDB can
345 evaluate to a callable address, not necessarily a function name. */
346
347 static char *dprintf_function = "";
348
349 /* The channel to use for dynamic printf if the preferred style is to
350 call into the inferior; if a nonempty string, it will be passed to
351 the call as the first argument, with the format string as the
352 second. As with the dprintf function, this can be anything that
353 GDB knows how to evaluate, so in addition to common choices like
354 "stderr", this could be an app-specific expression like
355 "mystreams[curlogger]". */
356
357 static char *dprintf_channel = "";
358
359 /* True if dprintf commands should continue to operate even if GDB
360 has disconnected. */
361 static int disconnected_dprintf = 1;
362
363 /* A reference-counted struct command_line. This lets multiple
364 breakpoints share a single command list. */
365 struct counted_command_line
366 {
367 /* The reference count. */
368 int refc;
369
370 /* The command list. */
371 struct command_line *commands;
372 };
373
374 struct command_line *
375 breakpoint_commands (struct breakpoint *b)
376 {
377 return b->commands ? b->commands->commands : NULL;
378 }
379
380 /* Flag indicating that a command has proceeded the inferior past the
381 current breakpoint. */
382
383 static int breakpoint_proceeded;
384
385 const char *
386 bpdisp_text (enum bpdisp disp)
387 {
388 /* NOTE: the following values are a part of MI protocol and
389 represent values of 'disp' field returned when inferior stops at
390 a breakpoint. */
391 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
392
393 return bpdisps[(int) disp];
394 }
395
396 /* Prototypes for exported functions. */
397 /* If FALSE, gdb will not use hardware support for watchpoints, even
398 if such is available. */
399 static int can_use_hw_watchpoints;
400
401 static void
402 show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
403 struct cmd_list_element *c,
404 const char *value)
405 {
406 fprintf_filtered (file,
407 _("Debugger's willingness to use "
408 "watchpoint hardware is %s.\n"),
409 value);
410 }
411
412 /* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
413 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
414 for unrecognized breakpoint locations.
415 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
416 static enum auto_boolean pending_break_support;
417 static void
418 show_pending_break_support (struct ui_file *file, int from_tty,
419 struct cmd_list_element *c,
420 const char *value)
421 {
422 fprintf_filtered (file,
423 _("Debugger's behavior regarding "
424 "pending breakpoints is %s.\n"),
425 value);
426 }
427
428 /* If 1, gdb will automatically use hardware breakpoints for breakpoints
429 set with "break" but falling in read-only memory.
430 If 0, gdb will warn about such breakpoints, but won't automatically
431 use hardware breakpoints. */
432 static int automatic_hardware_breakpoints;
433 static void
434 show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
435 struct cmd_list_element *c,
436 const char *value)
437 {
438 fprintf_filtered (file,
439 _("Automatic usage of hardware breakpoints is %s.\n"),
440 value);
441 }
442
443 /* If on, GDB keeps breakpoints inserted even if the inferior is
444 stopped, and immediately inserts any new breakpoints as soon as
445 they're created. If off (default), GDB keeps breakpoints off of
446 the target as long as possible. That is, it delays inserting
447 breakpoints until the next resume, and removes them again when the
448 target fully stops. This is a bit safer in case GDB crashes while
449 processing user input. */
450 static int always_inserted_mode = 0;
451
452 static void
453 show_always_inserted_mode (struct ui_file *file, int from_tty,
454 struct cmd_list_element *c, const char *value)
455 {
456 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
457 value);
458 }
459
460 /* See breakpoint.h. */
461
462 int
463 breakpoints_should_be_inserted_now (void)
464 {
465 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
466 {
467 /* If breakpoints are global, they should be inserted even if no
468 thread under gdb's control is running, or even if there are
469 no threads under GDB's control yet. */
470 return 1;
471 }
472 else if (target_has_execution)
473 {
474 struct thread_info *tp;
475
476 if (always_inserted_mode)
477 {
478 /* The user wants breakpoints inserted even if all threads
479 are stopped. */
480 return 1;
481 }
482
483 if (threads_are_executing ())
484 return 1;
485
486 /* Don't remove breakpoints yet if, even though all threads are
487 stopped, we still have events to process. */
488 ALL_NON_EXITED_THREADS (tp)
489 if (tp->resumed
490 && tp->suspend.waitstatus_pending_p)
491 return 1;
492 }
493 return 0;
494 }
495
496 static const char condition_evaluation_both[] = "host or target";
497
498 /* Modes for breakpoint condition evaluation. */
499 static const char condition_evaluation_auto[] = "auto";
500 static const char condition_evaluation_host[] = "host";
501 static const char condition_evaluation_target[] = "target";
502 static const char *const condition_evaluation_enums[] = {
503 condition_evaluation_auto,
504 condition_evaluation_host,
505 condition_evaluation_target,
506 NULL
507 };
508
509 /* Global that holds the current mode for breakpoint condition evaluation. */
510 static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
511
512 /* Global that we use to display information to the user (gets its value from
513 condition_evaluation_mode_1. */
514 static const char *condition_evaluation_mode = condition_evaluation_auto;
515
516 /* Translate a condition evaluation mode MODE into either "host"
517 or "target". This is used mostly to translate from "auto" to the
518 real setting that is being used. It returns the translated
519 evaluation mode. */
520
521 static const char *
522 translate_condition_evaluation_mode (const char *mode)
523 {
524 if (mode == condition_evaluation_auto)
525 {
526 if (target_supports_evaluation_of_breakpoint_conditions ())
527 return condition_evaluation_target;
528 else
529 return condition_evaluation_host;
530 }
531 else
532 return mode;
533 }
534
535 /* Discovers what condition_evaluation_auto translates to. */
536
537 static const char *
538 breakpoint_condition_evaluation_mode (void)
539 {
540 return translate_condition_evaluation_mode (condition_evaluation_mode);
541 }
542
543 /* Return true if GDB should evaluate breakpoint conditions or false
544 otherwise. */
545
546 static int
547 gdb_evaluates_breakpoint_condition_p (void)
548 {
549 const char *mode = breakpoint_condition_evaluation_mode ();
550
551 return (mode == condition_evaluation_host);
552 }
553
554 void _initialize_breakpoint (void);
555
556 /* Are we executing breakpoint commands? */
557 static int executing_breakpoint_commands;
558
559 /* Are overlay event breakpoints enabled? */
560 static int overlay_events_enabled;
561
562 /* See description in breakpoint.h. */
563 int target_exact_watchpoints = 0;
564
565 /* Walk the following statement or block through all breakpoints.
566 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
567 current breakpoint. */
568
569 #define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
570
571 #define ALL_BREAKPOINTS_SAFE(B,TMP) \
572 for (B = breakpoint_chain; \
573 B ? (TMP=B->next, 1): 0; \
574 B = TMP)
575
576 /* Similar iterator for the low-level breakpoints. SAFE variant is
577 not provided so update_global_location_list must not be called
578 while executing the block of ALL_BP_LOCATIONS. */
579
580 #define ALL_BP_LOCATIONS(B,BP_TMP) \
581 for (BP_TMP = bp_location; \
582 BP_TMP < bp_location + bp_location_count && (B = *BP_TMP); \
583 BP_TMP++)
584
585 /* Iterates through locations with address ADDRESS for the currently selected
586 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
587 to where the loop should start from.
588 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
589 appropriate location to start with. */
590
591 #define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
592 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
593 BP_LOCP_TMP = BP_LOCP_START; \
594 BP_LOCP_START \
595 && (BP_LOCP_TMP < bp_location + bp_location_count \
596 && (*BP_LOCP_TMP)->address == ADDRESS); \
597 BP_LOCP_TMP++)
598
599 /* Iterator for tracepoints only. */
600
601 #define ALL_TRACEPOINTS(B) \
602 for (B = breakpoint_chain; B; B = B->next) \
603 if (is_tracepoint (B))
604
605 /* Chains of all breakpoints defined. */
606
607 struct breakpoint *breakpoint_chain;
608
609 /* Array is sorted by bp_location_compare - primarily by the ADDRESS. */
610
611 static struct bp_location **bp_location;
612
613 /* Number of elements of BP_LOCATION. */
614
615 static unsigned bp_location_count;
616
617 /* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
618 ADDRESS for the current elements of BP_LOCATION which get a valid
619 result from bp_location_has_shadow. You can use it for roughly
620 limiting the subrange of BP_LOCATION to scan for shadow bytes for
621 an address you need to read. */
622
623 static CORE_ADDR bp_location_placed_address_before_address_max;
624
625 /* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
626 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
627 BP_LOCATION which get a valid result from bp_location_has_shadow.
628 You can use it for roughly limiting the subrange of BP_LOCATION to
629 scan for shadow bytes for an address you need to read. */
630
631 static CORE_ADDR bp_location_shadow_len_after_address_max;
632
633 /* The locations that no longer correspond to any breakpoint, unlinked
634 from bp_location array, but for which a hit may still be reported
635 by a target. */
636 VEC(bp_location_p) *moribund_locations = NULL;
637
638 /* Number of last breakpoint made. */
639
640 static int breakpoint_count;
641
642 /* The value of `breakpoint_count' before the last command that
643 created breakpoints. If the last (break-like) command created more
644 than one breakpoint, then the difference between BREAKPOINT_COUNT
645 and PREV_BREAKPOINT_COUNT is more than one. */
646 static int prev_breakpoint_count;
647
648 /* Number of last tracepoint made. */
649
650 static int tracepoint_count;
651
652 static struct cmd_list_element *breakpoint_set_cmdlist;
653 static struct cmd_list_element *breakpoint_show_cmdlist;
654 struct cmd_list_element *save_cmdlist;
655
656 /* See declaration at breakpoint.h. */
657
658 struct breakpoint *
659 breakpoint_find_if (int (*func) (struct breakpoint *b, void *d),
660 void *user_data)
661 {
662 struct breakpoint *b = NULL;
663
664 ALL_BREAKPOINTS (b)
665 {
666 if (func (b, user_data) != 0)
667 break;
668 }
669
670 return b;
671 }
672
673 /* Return whether a breakpoint is an active enabled breakpoint. */
674 static int
675 breakpoint_enabled (struct breakpoint *b)
676 {
677 return (b->enable_state == bp_enabled);
678 }
679
680 /* Set breakpoint count to NUM. */
681
682 static void
683 set_breakpoint_count (int num)
684 {
685 prev_breakpoint_count = breakpoint_count;
686 breakpoint_count = num;
687 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
688 }
689
690 /* Used by `start_rbreak_breakpoints' below, to record the current
691 breakpoint count before "rbreak" creates any breakpoint. */
692 static int rbreak_start_breakpoint_count;
693
694 /* Called at the start an "rbreak" command to record the first
695 breakpoint made. */
696
697 void
698 start_rbreak_breakpoints (void)
699 {
700 rbreak_start_breakpoint_count = breakpoint_count;
701 }
702
703 /* Called at the end of an "rbreak" command to record the last
704 breakpoint made. */
705
706 void
707 end_rbreak_breakpoints (void)
708 {
709 prev_breakpoint_count = rbreak_start_breakpoint_count;
710 }
711
712 /* Used in run_command to zero the hit count when a new run starts. */
713
714 void
715 clear_breakpoint_hit_counts (void)
716 {
717 struct breakpoint *b;
718
719 ALL_BREAKPOINTS (b)
720 b->hit_count = 0;
721 }
722
723 /* Allocate a new counted_command_line with reference count of 1.
724 The new structure owns COMMANDS. */
725
726 static struct counted_command_line *
727 alloc_counted_command_line (struct command_line *commands)
728 {
729 struct counted_command_line *result = XNEW (struct counted_command_line);
730
731 result->refc = 1;
732 result->commands = commands;
733
734 return result;
735 }
736
737 /* Increment reference count. This does nothing if CMD is NULL. */
738
739 static void
740 incref_counted_command_line (struct counted_command_line *cmd)
741 {
742 if (cmd)
743 ++cmd->refc;
744 }
745
746 /* Decrement reference count. If the reference count reaches 0,
747 destroy the counted_command_line. Sets *CMDP to NULL. This does
748 nothing if *CMDP is NULL. */
749
750 static void
751 decref_counted_command_line (struct counted_command_line **cmdp)
752 {
753 if (*cmdp)
754 {
755 if (--(*cmdp)->refc == 0)
756 {
757 free_command_lines (&(*cmdp)->commands);
758 xfree (*cmdp);
759 }
760 *cmdp = NULL;
761 }
762 }
763
764 /* A cleanup function that calls decref_counted_command_line. */
765
766 static void
767 do_cleanup_counted_command_line (void *arg)
768 {
769 decref_counted_command_line ((struct counted_command_line **) arg);
770 }
771
772 /* Create a cleanup that calls decref_counted_command_line on the
773 argument. */
774
775 static struct cleanup *
776 make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
777 {
778 return make_cleanup (do_cleanup_counted_command_line, cmdp);
779 }
780
781 \f
782 /* Return the breakpoint with the specified number, or NULL
783 if the number does not refer to an existing breakpoint. */
784
785 struct breakpoint *
786 get_breakpoint (int num)
787 {
788 struct breakpoint *b;
789
790 ALL_BREAKPOINTS (b)
791 if (b->number == num)
792 return b;
793
794 return NULL;
795 }
796
797 \f
798
799 /* Mark locations as "conditions have changed" in case the target supports
800 evaluating conditions on its side. */
801
802 static void
803 mark_breakpoint_modified (struct breakpoint *b)
804 {
805 struct bp_location *loc;
806
807 /* This is only meaningful if the target is
808 evaluating conditions and if the user has
809 opted for condition evaluation on the target's
810 side. */
811 if (gdb_evaluates_breakpoint_condition_p ()
812 || !target_supports_evaluation_of_breakpoint_conditions ())
813 return;
814
815 if (!is_breakpoint (b))
816 return;
817
818 for (loc = b->loc; loc; loc = loc->next)
819 loc->condition_changed = condition_modified;
820 }
821
822 /* Mark location as "conditions have changed" in case the target supports
823 evaluating conditions on its side. */
824
825 static void
826 mark_breakpoint_location_modified (struct bp_location *loc)
827 {
828 /* This is only meaningful if the target is
829 evaluating conditions and if the user has
830 opted for condition evaluation on the target's
831 side. */
832 if (gdb_evaluates_breakpoint_condition_p ()
833 || !target_supports_evaluation_of_breakpoint_conditions ())
834
835 return;
836
837 if (!is_breakpoint (loc->owner))
838 return;
839
840 loc->condition_changed = condition_modified;
841 }
842
843 /* Sets the condition-evaluation mode using the static global
844 condition_evaluation_mode. */
845
846 static void
847 set_condition_evaluation_mode (char *args, int from_tty,
848 struct cmd_list_element *c)
849 {
850 const char *old_mode, *new_mode;
851
852 if ((condition_evaluation_mode_1 == condition_evaluation_target)
853 && !target_supports_evaluation_of_breakpoint_conditions ())
854 {
855 condition_evaluation_mode_1 = condition_evaluation_mode;
856 warning (_("Target does not support breakpoint condition evaluation.\n"
857 "Using host evaluation mode instead."));
858 return;
859 }
860
861 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
862 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
863
864 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
865 settings was "auto". */
866 condition_evaluation_mode = condition_evaluation_mode_1;
867
868 /* Only update the mode if the user picked a different one. */
869 if (new_mode != old_mode)
870 {
871 struct bp_location *loc, **loc_tmp;
872 /* If the user switched to a different evaluation mode, we
873 need to synch the changes with the target as follows:
874
875 "host" -> "target": Send all (valid) conditions to the target.
876 "target" -> "host": Remove all the conditions from the target.
877 */
878
879 if (new_mode == condition_evaluation_target)
880 {
881 /* Mark everything modified and synch conditions with the
882 target. */
883 ALL_BP_LOCATIONS (loc, loc_tmp)
884 mark_breakpoint_location_modified (loc);
885 }
886 else
887 {
888 /* Manually mark non-duplicate locations to synch conditions
889 with the target. We do this to remove all the conditions the
890 target knows about. */
891 ALL_BP_LOCATIONS (loc, loc_tmp)
892 if (is_breakpoint (loc->owner) && loc->inserted)
893 loc->needs_update = 1;
894 }
895
896 /* Do the update. */
897 update_global_location_list (UGLL_MAY_INSERT);
898 }
899
900 return;
901 }
902
903 /* Shows the current mode of breakpoint condition evaluation. Explicitly shows
904 what "auto" is translating to. */
905
906 static void
907 show_condition_evaluation_mode (struct ui_file *file, int from_tty,
908 struct cmd_list_element *c, const char *value)
909 {
910 if (condition_evaluation_mode == condition_evaluation_auto)
911 fprintf_filtered (file,
912 _("Breakpoint condition evaluation "
913 "mode is %s (currently %s).\n"),
914 value,
915 breakpoint_condition_evaluation_mode ());
916 else
917 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
918 value);
919 }
920
921 /* A comparison function for bp_location AP and BP that is used by
922 bsearch. This comparison function only cares about addresses, unlike
923 the more general bp_location_compare function. */
924
925 static int
926 bp_location_compare_addrs (const void *ap, const void *bp)
927 {
928 const struct bp_location *a = *(const struct bp_location **) ap;
929 const struct bp_location *b = *(const struct bp_location **) bp;
930
931 if (a->address == b->address)
932 return 0;
933 else
934 return ((a->address > b->address) - (a->address < b->address));
935 }
936
937 /* Helper function to skip all bp_locations with addresses
938 less than ADDRESS. It returns the first bp_location that
939 is greater than or equal to ADDRESS. If none is found, just
940 return NULL. */
941
942 static struct bp_location **
943 get_first_locp_gte_addr (CORE_ADDR address)
944 {
945 struct bp_location dummy_loc;
946 struct bp_location *dummy_locp = &dummy_loc;
947 struct bp_location **locp_found = NULL;
948
949 /* Initialize the dummy location's address field. */
950 memset (&dummy_loc, 0, sizeof (struct bp_location));
951 dummy_loc.address = address;
952
953 /* Find a close match to the first location at ADDRESS. */
954 locp_found = ((struct bp_location **)
955 bsearch (&dummy_locp, bp_location, bp_location_count,
956 sizeof (struct bp_location **),
957 bp_location_compare_addrs));
958
959 /* Nothing was found, nothing left to do. */
960 if (locp_found == NULL)
961 return NULL;
962
963 /* We may have found a location that is at ADDRESS but is not the first in the
964 location's list. Go backwards (if possible) and locate the first one. */
965 while ((locp_found - 1) >= bp_location
966 && (*(locp_found - 1))->address == address)
967 locp_found--;
968
969 return locp_found;
970 }
971
972 void
973 set_breakpoint_condition (struct breakpoint *b, const char *exp,
974 int from_tty)
975 {
976 xfree (b->cond_string);
977 b->cond_string = NULL;
978
979 if (is_watchpoint (b))
980 {
981 struct watchpoint *w = (struct watchpoint *) b;
982
983 xfree (w->cond_exp);
984 w->cond_exp = NULL;
985 }
986 else
987 {
988 struct bp_location *loc;
989
990 for (loc = b->loc; loc; loc = loc->next)
991 {
992 xfree (loc->cond);
993 loc->cond = NULL;
994
995 /* No need to free the condition agent expression
996 bytecode (if we have one). We will handle this
997 when we go through update_global_location_list. */
998 }
999 }
1000
1001 if (*exp == 0)
1002 {
1003 if (from_tty)
1004 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
1005 }
1006 else
1007 {
1008 const char *arg = exp;
1009
1010 /* I don't know if it matters whether this is the string the user
1011 typed in or the decompiled expression. */
1012 b->cond_string = xstrdup (arg);
1013 b->condition_not_parsed = 0;
1014
1015 if (is_watchpoint (b))
1016 {
1017 struct watchpoint *w = (struct watchpoint *) b;
1018
1019 innermost_block = NULL;
1020 arg = exp;
1021 w->cond_exp = parse_exp_1 (&arg, 0, 0, 0);
1022 if (*arg)
1023 error (_("Junk at end of expression"));
1024 w->cond_exp_valid_block = innermost_block;
1025 }
1026 else
1027 {
1028 struct bp_location *loc;
1029
1030 for (loc = b->loc; loc; loc = loc->next)
1031 {
1032 arg = exp;
1033 loc->cond =
1034 parse_exp_1 (&arg, loc->address,
1035 block_for_pc (loc->address), 0);
1036 if (*arg)
1037 error (_("Junk at end of expression"));
1038 }
1039 }
1040 }
1041 mark_breakpoint_modified (b);
1042
1043 observer_notify_breakpoint_modified (b);
1044 }
1045
1046 /* Completion for the "condition" command. */
1047
1048 static VEC (char_ptr) *
1049 condition_completer (struct cmd_list_element *cmd,
1050 const char *text, const char *word)
1051 {
1052 const char *space;
1053
1054 text = skip_spaces_const (text);
1055 space = skip_to_space_const (text);
1056 if (*space == '\0')
1057 {
1058 int len;
1059 struct breakpoint *b;
1060 VEC (char_ptr) *result = NULL;
1061
1062 if (text[0] == '$')
1063 {
1064 /* We don't support completion of history indices. */
1065 if (isdigit (text[1]))
1066 return NULL;
1067 return complete_internalvar (&text[1]);
1068 }
1069
1070 /* We're completing the breakpoint number. */
1071 len = strlen (text);
1072
1073 ALL_BREAKPOINTS (b)
1074 {
1075 char number[50];
1076
1077 xsnprintf (number, sizeof (number), "%d", b->number);
1078
1079 if (strncmp (number, text, len) == 0)
1080 VEC_safe_push (char_ptr, result, xstrdup (number));
1081 }
1082
1083 return result;
1084 }
1085
1086 /* We're completing the expression part. */
1087 text = skip_spaces_const (space);
1088 return expression_completer (cmd, text, word);
1089 }
1090
1091 /* condition N EXP -- set break condition of breakpoint N to EXP. */
1092
1093 static void
1094 condition_command (char *arg, int from_tty)
1095 {
1096 struct breakpoint *b;
1097 char *p;
1098 int bnum;
1099
1100 if (arg == 0)
1101 error_no_arg (_("breakpoint number"));
1102
1103 p = arg;
1104 bnum = get_number (&p);
1105 if (bnum == 0)
1106 error (_("Bad breakpoint argument: '%s'"), arg);
1107
1108 ALL_BREAKPOINTS (b)
1109 if (b->number == bnum)
1110 {
1111 /* Check if this breakpoint has a "stop" method implemented in an
1112 extension language. This method and conditions entered into GDB
1113 from the CLI are mutually exclusive. */
1114 const struct extension_language_defn *extlang
1115 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1116
1117 if (extlang != NULL)
1118 {
1119 error (_("Only one stop condition allowed. There is currently"
1120 " a %s stop condition defined for this breakpoint."),
1121 ext_lang_capitalized_name (extlang));
1122 }
1123 set_breakpoint_condition (b, p, from_tty);
1124
1125 if (is_breakpoint (b))
1126 update_global_location_list (UGLL_MAY_INSERT);
1127
1128 return;
1129 }
1130
1131 error (_("No breakpoint number %d."), bnum);
1132 }
1133
1134 /* Check that COMMAND do not contain commands that are suitable
1135 only for tracepoints and not suitable for ordinary breakpoints.
1136 Throw if any such commands is found. */
1137
1138 static void
1139 check_no_tracepoint_commands (struct command_line *commands)
1140 {
1141 struct command_line *c;
1142
1143 for (c = commands; c; c = c->next)
1144 {
1145 int i;
1146
1147 if (c->control_type == while_stepping_control)
1148 error (_("The 'while-stepping' command can "
1149 "only be used for tracepoints"));
1150
1151 for (i = 0; i < c->body_count; ++i)
1152 check_no_tracepoint_commands ((c->body_list)[i]);
1153
1154 /* Not that command parsing removes leading whitespace and comment
1155 lines and also empty lines. So, we only need to check for
1156 command directly. */
1157 if (strstr (c->line, "collect ") == c->line)
1158 error (_("The 'collect' command can only be used for tracepoints"));
1159
1160 if (strstr (c->line, "teval ") == c->line)
1161 error (_("The 'teval' command can only be used for tracepoints"));
1162 }
1163 }
1164
1165 /* Encapsulate tests for different types of tracepoints. */
1166
1167 static int
1168 is_tracepoint_type (enum bptype type)
1169 {
1170 return (type == bp_tracepoint
1171 || type == bp_fast_tracepoint
1172 || type == bp_static_tracepoint);
1173 }
1174
1175 int
1176 is_tracepoint (const struct breakpoint *b)
1177 {
1178 return is_tracepoint_type (b->type);
1179 }
1180
1181 /* A helper function that validates that COMMANDS are valid for a
1182 breakpoint. This function will throw an exception if a problem is
1183 found. */
1184
1185 static void
1186 validate_commands_for_breakpoint (struct breakpoint *b,
1187 struct command_line *commands)
1188 {
1189 if (is_tracepoint (b))
1190 {
1191 struct tracepoint *t = (struct tracepoint *) b;
1192 struct command_line *c;
1193 struct command_line *while_stepping = 0;
1194
1195 /* Reset the while-stepping step count. The previous commands
1196 might have included a while-stepping action, while the new
1197 ones might not. */
1198 t->step_count = 0;
1199
1200 /* We need to verify that each top-level element of commands is
1201 valid for tracepoints, that there's at most one
1202 while-stepping element, and that the while-stepping's body
1203 has valid tracing commands excluding nested while-stepping.
1204 We also need to validate the tracepoint action line in the
1205 context of the tracepoint --- validate_actionline actually
1206 has side effects, like setting the tracepoint's
1207 while-stepping STEP_COUNT, in addition to checking if the
1208 collect/teval actions parse and make sense in the
1209 tracepoint's context. */
1210 for (c = commands; c; c = c->next)
1211 {
1212 if (c->control_type == while_stepping_control)
1213 {
1214 if (b->type == bp_fast_tracepoint)
1215 error (_("The 'while-stepping' command "
1216 "cannot be used for fast tracepoint"));
1217 else if (b->type == bp_static_tracepoint)
1218 error (_("The 'while-stepping' command "
1219 "cannot be used for static tracepoint"));
1220
1221 if (while_stepping)
1222 error (_("The 'while-stepping' command "
1223 "can be used only once"));
1224 else
1225 while_stepping = c;
1226 }
1227
1228 validate_actionline (c->line, b);
1229 }
1230 if (while_stepping)
1231 {
1232 struct command_line *c2;
1233
1234 gdb_assert (while_stepping->body_count == 1);
1235 c2 = while_stepping->body_list[0];
1236 for (; c2; c2 = c2->next)
1237 {
1238 if (c2->control_type == while_stepping_control)
1239 error (_("The 'while-stepping' command cannot be nested"));
1240 }
1241 }
1242 }
1243 else
1244 {
1245 check_no_tracepoint_commands (commands);
1246 }
1247 }
1248
1249 /* Return a vector of all the static tracepoints set at ADDR. The
1250 caller is responsible for releasing the vector. */
1251
1252 VEC(breakpoint_p) *
1253 static_tracepoints_here (CORE_ADDR addr)
1254 {
1255 struct breakpoint *b;
1256 VEC(breakpoint_p) *found = 0;
1257 struct bp_location *loc;
1258
1259 ALL_BREAKPOINTS (b)
1260 if (b->type == bp_static_tracepoint)
1261 {
1262 for (loc = b->loc; loc; loc = loc->next)
1263 if (loc->address == addr)
1264 VEC_safe_push(breakpoint_p, found, b);
1265 }
1266
1267 return found;
1268 }
1269
1270 /* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
1271 validate that only allowed commands are included. */
1272
1273 void
1274 breakpoint_set_commands (struct breakpoint *b,
1275 struct command_line *commands)
1276 {
1277 validate_commands_for_breakpoint (b, commands);
1278
1279 decref_counted_command_line (&b->commands);
1280 b->commands = alloc_counted_command_line (commands);
1281 observer_notify_breakpoint_modified (b);
1282 }
1283
1284 /* Set the internal `silent' flag on the breakpoint. Note that this
1285 is not the same as the "silent" that may appear in the breakpoint's
1286 commands. */
1287
1288 void
1289 breakpoint_set_silent (struct breakpoint *b, int silent)
1290 {
1291 int old_silent = b->silent;
1292
1293 b->silent = silent;
1294 if (old_silent != silent)
1295 observer_notify_breakpoint_modified (b);
1296 }
1297
1298 /* Set the thread for this breakpoint. If THREAD is -1, make the
1299 breakpoint work for any thread. */
1300
1301 void
1302 breakpoint_set_thread (struct breakpoint *b, int thread)
1303 {
1304 int old_thread = b->thread;
1305
1306 b->thread = thread;
1307 if (old_thread != thread)
1308 observer_notify_breakpoint_modified (b);
1309 }
1310
1311 /* Set the task for this breakpoint. If TASK is 0, make the
1312 breakpoint work for any task. */
1313
1314 void
1315 breakpoint_set_task (struct breakpoint *b, int task)
1316 {
1317 int old_task = b->task;
1318
1319 b->task = task;
1320 if (old_task != task)
1321 observer_notify_breakpoint_modified (b);
1322 }
1323
1324 void
1325 check_tracepoint_command (char *line, void *closure)
1326 {
1327 struct breakpoint *b = (struct breakpoint *) closure;
1328
1329 validate_actionline (line, b);
1330 }
1331
1332 /* A structure used to pass information through
1333 map_breakpoint_numbers. */
1334
1335 struct commands_info
1336 {
1337 /* True if the command was typed at a tty. */
1338 int from_tty;
1339
1340 /* The breakpoint range spec. */
1341 char *arg;
1342
1343 /* Non-NULL if the body of the commands are being read from this
1344 already-parsed command. */
1345 struct command_line *control;
1346
1347 /* The command lines read from the user, or NULL if they have not
1348 yet been read. */
1349 struct counted_command_line *cmd;
1350 };
1351
1352 /* A callback for map_breakpoint_numbers that sets the commands for
1353 commands_command. */
1354
1355 static void
1356 do_map_commands_command (struct breakpoint *b, void *data)
1357 {
1358 struct commands_info *info = (struct commands_info *) data;
1359
1360 if (info->cmd == NULL)
1361 {
1362 struct command_line *l;
1363
1364 if (info->control != NULL)
1365 l = copy_command_lines (info->control->body_list[0]);
1366 else
1367 {
1368 struct cleanup *old_chain;
1369 char *str;
1370
1371 str = xstrprintf (_("Type commands for breakpoint(s) "
1372 "%s, one per line."),
1373 info->arg);
1374
1375 old_chain = make_cleanup (xfree, str);
1376
1377 l = read_command_lines (str,
1378 info->from_tty, 1,
1379 (is_tracepoint (b)
1380 ? check_tracepoint_command : 0),
1381 b);
1382
1383 do_cleanups (old_chain);
1384 }
1385
1386 info->cmd = alloc_counted_command_line (l);
1387 }
1388
1389 /* If a breakpoint was on the list more than once, we don't need to
1390 do anything. */
1391 if (b->commands != info->cmd)
1392 {
1393 validate_commands_for_breakpoint (b, info->cmd->commands);
1394 incref_counted_command_line (info->cmd);
1395 decref_counted_command_line (&b->commands);
1396 b->commands = info->cmd;
1397 observer_notify_breakpoint_modified (b);
1398 }
1399 }
1400
1401 static void
1402 commands_command_1 (char *arg, int from_tty,
1403 struct command_line *control)
1404 {
1405 struct cleanup *cleanups;
1406 struct commands_info info;
1407
1408 info.from_tty = from_tty;
1409 info.control = control;
1410 info.cmd = NULL;
1411 /* If we read command lines from the user, then `info' will hold an
1412 extra reference to the commands that we must clean up. */
1413 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
1414
1415 if (arg == NULL || !*arg)
1416 {
1417 if (breakpoint_count - prev_breakpoint_count > 1)
1418 arg = xstrprintf ("%d-%d", prev_breakpoint_count + 1,
1419 breakpoint_count);
1420 else if (breakpoint_count > 0)
1421 arg = xstrprintf ("%d", breakpoint_count);
1422 else
1423 {
1424 /* So that we don't try to free the incoming non-NULL
1425 argument in the cleanup below. Mapping breakpoint
1426 numbers will fail in this case. */
1427 arg = NULL;
1428 }
1429 }
1430 else
1431 /* The command loop has some static state, so we need to preserve
1432 our argument. */
1433 arg = xstrdup (arg);
1434
1435 if (arg != NULL)
1436 make_cleanup (xfree, arg);
1437
1438 info.arg = arg;
1439
1440 map_breakpoint_numbers (arg, do_map_commands_command, &info);
1441
1442 if (info.cmd == NULL)
1443 error (_("No breakpoints specified."));
1444
1445 do_cleanups (cleanups);
1446 }
1447
1448 static void
1449 commands_command (char *arg, int from_tty)
1450 {
1451 commands_command_1 (arg, from_tty, NULL);
1452 }
1453
1454 /* Like commands_command, but instead of reading the commands from
1455 input stream, takes them from an already parsed command structure.
1456
1457 This is used by cli-script.c to DTRT with breakpoint commands
1458 that are part of if and while bodies. */
1459 enum command_control_type
1460 commands_from_control_command (char *arg, struct command_line *cmd)
1461 {
1462 commands_command_1 (arg, 0, cmd);
1463 return simple_control;
1464 }
1465
1466 /* Return non-zero if BL->TARGET_INFO contains valid information. */
1467
1468 static int
1469 bp_location_has_shadow (struct bp_location *bl)
1470 {
1471 if (bl->loc_type != bp_loc_software_breakpoint)
1472 return 0;
1473 if (!bl->inserted)
1474 return 0;
1475 if (bl->target_info.shadow_len == 0)
1476 /* BL isn't valid, or doesn't shadow memory. */
1477 return 0;
1478 return 1;
1479 }
1480
1481 /* Update BUF, which is LEN bytes read from the target address
1482 MEMADDR, by replacing a memory breakpoint with its shadowed
1483 contents.
1484
1485 If READBUF is not NULL, this buffer must not overlap with the of
1486 the breakpoint location's shadow_contents buffer. Otherwise, a
1487 failed assertion internal error will be raised. */
1488
1489 static void
1490 one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1491 const gdb_byte *writebuf_org,
1492 ULONGEST memaddr, LONGEST len,
1493 struct bp_target_info *target_info,
1494 struct gdbarch *gdbarch)
1495 {
1496 /* Now do full processing of the found relevant range of elements. */
1497 CORE_ADDR bp_addr = 0;
1498 int bp_size = 0;
1499 int bptoffset = 0;
1500
1501 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1502 current_program_space->aspace, 0))
1503 {
1504 /* The breakpoint is inserted in a different address space. */
1505 return;
1506 }
1507
1508 /* Addresses and length of the part of the breakpoint that
1509 we need to copy. */
1510 bp_addr = target_info->placed_address;
1511 bp_size = target_info->shadow_len;
1512
1513 if (bp_addr + bp_size <= memaddr)
1514 {
1515 /* The breakpoint is entirely before the chunk of memory we are
1516 reading. */
1517 return;
1518 }
1519
1520 if (bp_addr >= memaddr + len)
1521 {
1522 /* The breakpoint is entirely after the chunk of memory we are
1523 reading. */
1524 return;
1525 }
1526
1527 /* Offset within shadow_contents. */
1528 if (bp_addr < memaddr)
1529 {
1530 /* Only copy the second part of the breakpoint. */
1531 bp_size -= memaddr - bp_addr;
1532 bptoffset = memaddr - bp_addr;
1533 bp_addr = memaddr;
1534 }
1535
1536 if (bp_addr + bp_size > memaddr + len)
1537 {
1538 /* Only copy the first part of the breakpoint. */
1539 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1540 }
1541
1542 if (readbuf != NULL)
1543 {
1544 /* Verify that the readbuf buffer does not overlap with the
1545 shadow_contents buffer. */
1546 gdb_assert (target_info->shadow_contents >= readbuf + len
1547 || readbuf >= (target_info->shadow_contents
1548 + target_info->shadow_len));
1549
1550 /* Update the read buffer with this inserted breakpoint's
1551 shadow. */
1552 memcpy (readbuf + bp_addr - memaddr,
1553 target_info->shadow_contents + bptoffset, bp_size);
1554 }
1555 else
1556 {
1557 const unsigned char *bp;
1558 CORE_ADDR addr = target_info->reqstd_address;
1559 int placed_size;
1560
1561 /* Update the shadow with what we want to write to memory. */
1562 memcpy (target_info->shadow_contents + bptoffset,
1563 writebuf_org + bp_addr - memaddr, bp_size);
1564
1565 /* Determine appropriate breakpoint contents and size for this
1566 address. */
1567 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
1568
1569 /* Update the final write buffer with this inserted
1570 breakpoint's INSN. */
1571 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1572 }
1573 }
1574
1575 /* Update BUF, which is LEN bytes read from the target address MEMADDR,
1576 by replacing any memory breakpoints with their shadowed contents.
1577
1578 If READBUF is not NULL, this buffer must not overlap with any of
1579 the breakpoint location's shadow_contents buffers. Otherwise,
1580 a failed assertion internal error will be raised.
1581
1582 The range of shadowed area by each bp_location is:
1583 bl->address - bp_location_placed_address_before_address_max
1584 up to bl->address + bp_location_shadow_len_after_address_max
1585 The range we were requested to resolve shadows for is:
1586 memaddr ... memaddr + len
1587 Thus the safe cutoff boundaries for performance optimization are
1588 memaddr + len <= (bl->address
1589 - bp_location_placed_address_before_address_max)
1590 and:
1591 bl->address + bp_location_shadow_len_after_address_max <= memaddr */
1592
1593 void
1594 breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1595 const gdb_byte *writebuf_org,
1596 ULONGEST memaddr, LONGEST len)
1597 {
1598 /* Left boundary, right boundary and median element of our binary
1599 search. */
1600 unsigned bc_l, bc_r, bc;
1601
1602 /* Find BC_L which is a leftmost element which may affect BUF
1603 content. It is safe to report lower value but a failure to
1604 report higher one. */
1605
1606 bc_l = 0;
1607 bc_r = bp_location_count;
1608 while (bc_l + 1 < bc_r)
1609 {
1610 struct bp_location *bl;
1611
1612 bc = (bc_l + bc_r) / 2;
1613 bl = bp_location[bc];
1614
1615 /* Check first BL->ADDRESS will not overflow due to the added
1616 constant. Then advance the left boundary only if we are sure
1617 the BC element can in no way affect the BUF content (MEMADDR
1618 to MEMADDR + LEN range).
1619
1620 Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1621 offset so that we cannot miss a breakpoint with its shadow
1622 range tail still reaching MEMADDR. */
1623
1624 if ((bl->address + bp_location_shadow_len_after_address_max
1625 >= bl->address)
1626 && (bl->address + bp_location_shadow_len_after_address_max
1627 <= memaddr))
1628 bc_l = bc;
1629 else
1630 bc_r = bc;
1631 }
1632
1633 /* Due to the binary search above, we need to make sure we pick the
1634 first location that's at BC_L's address. E.g., if there are
1635 multiple locations at the same address, BC_L may end up pointing
1636 at a duplicate location, and miss the "master"/"inserted"
1637 location. Say, given locations L1, L2 and L3 at addresses A and
1638 B:
1639
1640 L1@A, L2@A, L3@B, ...
1641
1642 BC_L could end up pointing at location L2, while the "master"
1643 location could be L1. Since the `loc->inserted' flag is only set
1644 on "master" locations, we'd forget to restore the shadow of L1
1645 and L2. */
1646 while (bc_l > 0
1647 && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1648 bc_l--;
1649
1650 /* Now do full processing of the found relevant range of elements. */
1651
1652 for (bc = bc_l; bc < bp_location_count; bc++)
1653 {
1654 struct bp_location *bl = bp_location[bc];
1655
1656 /* bp_location array has BL->OWNER always non-NULL. */
1657 if (bl->owner->type == bp_none)
1658 warning (_("reading through apparently deleted breakpoint #%d?"),
1659 bl->owner->number);
1660
1661 /* Performance optimization: any further element can no longer affect BUF
1662 content. */
1663
1664 if (bl->address >= bp_location_placed_address_before_address_max
1665 && memaddr + len <= (bl->address
1666 - bp_location_placed_address_before_address_max))
1667 break;
1668
1669 if (!bp_location_has_shadow (bl))
1670 continue;
1671
1672 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1673 memaddr, len, &bl->target_info, bl->gdbarch);
1674 }
1675 }
1676
1677 \f
1678
1679 /* Return true if BPT is either a software breakpoint or a hardware
1680 breakpoint. */
1681
1682 int
1683 is_breakpoint (const struct breakpoint *bpt)
1684 {
1685 return (bpt->type == bp_breakpoint
1686 || bpt->type == bp_hardware_breakpoint
1687 || bpt->type == bp_dprintf);
1688 }
1689
1690 /* Return true if BPT is of any hardware watchpoint kind. */
1691
1692 static int
1693 is_hardware_watchpoint (const struct breakpoint *bpt)
1694 {
1695 return (bpt->type == bp_hardware_watchpoint
1696 || bpt->type == bp_read_watchpoint
1697 || bpt->type == bp_access_watchpoint);
1698 }
1699
1700 /* Return true if BPT is of any watchpoint kind, hardware or
1701 software. */
1702
1703 int
1704 is_watchpoint (const struct breakpoint *bpt)
1705 {
1706 return (is_hardware_watchpoint (bpt)
1707 || bpt->type == bp_watchpoint);
1708 }
1709
1710 /* Returns true if the current thread and its running state are safe
1711 to evaluate or update watchpoint B. Watchpoints on local
1712 expressions need to be evaluated in the context of the thread that
1713 was current when the watchpoint was created, and, that thread needs
1714 to be stopped to be able to select the correct frame context.
1715 Watchpoints on global expressions can be evaluated on any thread,
1716 and in any state. It is presently left to the target allowing
1717 memory accesses when threads are running. */
1718
1719 static int
1720 watchpoint_in_thread_scope (struct watchpoint *b)
1721 {
1722 return (b->base.pspace == current_program_space
1723 && (ptid_equal (b->watchpoint_thread, null_ptid)
1724 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1725 && !is_executing (inferior_ptid))));
1726 }
1727
1728 /* Set watchpoint B to disp_del_at_next_stop, even including its possible
1729 associated bp_watchpoint_scope breakpoint. */
1730
1731 static void
1732 watchpoint_del_at_next_stop (struct watchpoint *w)
1733 {
1734 struct breakpoint *b = &w->base;
1735
1736 if (b->related_breakpoint != b)
1737 {
1738 gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1739 gdb_assert (b->related_breakpoint->related_breakpoint == b);
1740 b->related_breakpoint->disposition = disp_del_at_next_stop;
1741 b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1742 b->related_breakpoint = b;
1743 }
1744 b->disposition = disp_del_at_next_stop;
1745 }
1746
1747 /* Extract a bitfield value from value VAL using the bit parameters contained in
1748 watchpoint W. */
1749
1750 static struct value *
1751 extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1752 {
1753 struct value *bit_val;
1754
1755 if (val == NULL)
1756 return NULL;
1757
1758 bit_val = allocate_value (value_type (val));
1759
1760 unpack_value_bitfield (bit_val,
1761 w->val_bitpos,
1762 w->val_bitsize,
1763 value_contents_for_printing (val),
1764 value_offset (val),
1765 val);
1766
1767 return bit_val;
1768 }
1769
1770 /* Allocate a dummy location and add it to B, which must be a software
1771 watchpoint. This is required because even if a software watchpoint
1772 is not watching any memory, bpstat_stop_status requires a location
1773 to be able to report stops. */
1774
1775 static void
1776 software_watchpoint_add_no_memory_location (struct breakpoint *b,
1777 struct program_space *pspace)
1778 {
1779 gdb_assert (b->type == bp_watchpoint && b->loc == NULL);
1780
1781 b->loc = allocate_bp_location (b);
1782 b->loc->pspace = pspace;
1783 b->loc->address = -1;
1784 b->loc->length = -1;
1785 }
1786
1787 /* Returns true if B is a software watchpoint that is not watching any
1788 memory (e.g., "watch $pc"). */
1789
1790 static int
1791 is_no_memory_software_watchpoint (struct breakpoint *b)
1792 {
1793 return (b->type == bp_watchpoint
1794 && b->loc != NULL
1795 && b->loc->next == NULL
1796 && b->loc->address == -1
1797 && b->loc->length == -1);
1798 }
1799
1800 /* Assuming that B is a watchpoint:
1801 - Reparse watchpoint expression, if REPARSE is non-zero
1802 - Evaluate expression and store the result in B->val
1803 - Evaluate the condition if there is one, and store the result
1804 in b->loc->cond.
1805 - Update the list of values that must be watched in B->loc.
1806
1807 If the watchpoint disposition is disp_del_at_next_stop, then do
1808 nothing. If this is local watchpoint that is out of scope, delete
1809 it.
1810
1811 Even with `set breakpoint always-inserted on' the watchpoints are
1812 removed + inserted on each stop here. Normal breakpoints must
1813 never be removed because they might be missed by a running thread
1814 when debugging in non-stop mode. On the other hand, hardware
1815 watchpoints (is_hardware_watchpoint; processed here) are specific
1816 to each LWP since they are stored in each LWP's hardware debug
1817 registers. Therefore, such LWP must be stopped first in order to
1818 be able to modify its hardware watchpoints.
1819
1820 Hardware watchpoints must be reset exactly once after being
1821 presented to the user. It cannot be done sooner, because it would
1822 reset the data used to present the watchpoint hit to the user. And
1823 it must not be done later because it could display the same single
1824 watchpoint hit during multiple GDB stops. Note that the latter is
1825 relevant only to the hardware watchpoint types bp_read_watchpoint
1826 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1827 not user-visible - its hit is suppressed if the memory content has
1828 not changed.
1829
1830 The following constraints influence the location where we can reset
1831 hardware watchpoints:
1832
1833 * target_stopped_by_watchpoint and target_stopped_data_address are
1834 called several times when GDB stops.
1835
1836 [linux]
1837 * Multiple hardware watchpoints can be hit at the same time,
1838 causing GDB to stop. GDB only presents one hardware watchpoint
1839 hit at a time as the reason for stopping, and all the other hits
1840 are presented later, one after the other, each time the user
1841 requests the execution to be resumed. Execution is not resumed
1842 for the threads still having pending hit event stored in
1843 LWP_INFO->STATUS. While the watchpoint is already removed from
1844 the inferior on the first stop the thread hit event is kept being
1845 reported from its cached value by linux_nat_stopped_data_address
1846 until the real thread resume happens after the watchpoint gets
1847 presented and thus its LWP_INFO->STATUS gets reset.
1848
1849 Therefore the hardware watchpoint hit can get safely reset on the
1850 watchpoint removal from inferior. */
1851
1852 static void
1853 update_watchpoint (struct watchpoint *b, int reparse)
1854 {
1855 int within_current_scope;
1856 struct frame_id saved_frame_id;
1857 int frame_saved;
1858
1859 /* If this is a local watchpoint, we only want to check if the
1860 watchpoint frame is in scope if the current thread is the thread
1861 that was used to create the watchpoint. */
1862 if (!watchpoint_in_thread_scope (b))
1863 return;
1864
1865 if (b->base.disposition == disp_del_at_next_stop)
1866 return;
1867
1868 frame_saved = 0;
1869
1870 /* Determine if the watchpoint is within scope. */
1871 if (b->exp_valid_block == NULL)
1872 within_current_scope = 1;
1873 else
1874 {
1875 struct frame_info *fi = get_current_frame ();
1876 struct gdbarch *frame_arch = get_frame_arch (fi);
1877 CORE_ADDR frame_pc = get_frame_pc (fi);
1878
1879 /* If we're at a point where the stack has been destroyed
1880 (e.g. in a function epilogue), unwinding may not work
1881 properly. Do not attempt to recreate locations at this
1882 point. See similar comments in watchpoint_check. */
1883 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
1884 return;
1885
1886 /* Save the current frame's ID so we can restore it after
1887 evaluating the watchpoint expression on its own frame. */
1888 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1889 took a frame parameter, so that we didn't have to change the
1890 selected frame. */
1891 frame_saved = 1;
1892 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1893
1894 fi = frame_find_by_id (b->watchpoint_frame);
1895 within_current_scope = (fi != NULL);
1896 if (within_current_scope)
1897 select_frame (fi);
1898 }
1899
1900 /* We don't free locations. They are stored in the bp_location array
1901 and update_global_location_list will eventually delete them and
1902 remove breakpoints if needed. */
1903 b->base.loc = NULL;
1904
1905 if (within_current_scope && reparse)
1906 {
1907 const char *s;
1908
1909 if (b->exp)
1910 {
1911 xfree (b->exp);
1912 b->exp = NULL;
1913 }
1914 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1915 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
1916 /* If the meaning of expression itself changed, the old value is
1917 no longer relevant. We don't want to report a watchpoint hit
1918 to the user when the old value and the new value may actually
1919 be completely different objects. */
1920 value_free (b->val);
1921 b->val = NULL;
1922 b->val_valid = 0;
1923
1924 /* Note that unlike with breakpoints, the watchpoint's condition
1925 expression is stored in the breakpoint object, not in the
1926 locations (re)created below. */
1927 if (b->base.cond_string != NULL)
1928 {
1929 if (b->cond_exp != NULL)
1930 {
1931 xfree (b->cond_exp);
1932 b->cond_exp = NULL;
1933 }
1934
1935 s = b->base.cond_string;
1936 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
1937 }
1938 }
1939
1940 /* If we failed to parse the expression, for example because
1941 it refers to a global variable in a not-yet-loaded shared library,
1942 don't try to insert watchpoint. We don't automatically delete
1943 such watchpoint, though, since failure to parse expression
1944 is different from out-of-scope watchpoint. */
1945 if (!target_has_execution)
1946 {
1947 /* Without execution, memory can't change. No use to try and
1948 set watchpoint locations. The watchpoint will be reset when
1949 the target gains execution, through breakpoint_re_set. */
1950 if (!can_use_hw_watchpoints)
1951 {
1952 if (b->base.ops->works_in_software_mode (&b->base))
1953 b->base.type = bp_watchpoint;
1954 else
1955 error (_("Can't set read/access watchpoint when "
1956 "hardware watchpoints are disabled."));
1957 }
1958 }
1959 else if (within_current_scope && b->exp)
1960 {
1961 int pc = 0;
1962 struct value *val_chain, *v, *result, *next;
1963 struct program_space *frame_pspace;
1964
1965 fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain, 0);
1966
1967 /* Avoid setting b->val if it's already set. The meaning of
1968 b->val is 'the last value' user saw, and we should update
1969 it only if we reported that last value to user. As it
1970 happens, the code that reports it updates b->val directly.
1971 We don't keep track of the memory value for masked
1972 watchpoints. */
1973 if (!b->val_valid && !is_masked_watchpoint (&b->base))
1974 {
1975 if (b->val_bitsize != 0)
1976 {
1977 v = extract_bitfield_from_watchpoint_value (b, v);
1978 if (v != NULL)
1979 release_value (v);
1980 }
1981 b->val = v;
1982 b->val_valid = 1;
1983 }
1984
1985 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1986
1987 /* Look at each value on the value chain. */
1988 for (v = val_chain; v; v = value_next (v))
1989 {
1990 /* If it's a memory location, and GDB actually needed
1991 its contents to evaluate the expression, then we
1992 must watch it. If the first value returned is
1993 still lazy, that means an error occurred reading it;
1994 watch it anyway in case it becomes readable. */
1995 if (VALUE_LVAL (v) == lval_memory
1996 && (v == val_chain || ! value_lazy (v)))
1997 {
1998 struct type *vtype = check_typedef (value_type (v));
1999
2000 /* We only watch structs and arrays if user asked
2001 for it explicitly, never if they just happen to
2002 appear in the middle of some value chain. */
2003 if (v == result
2004 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
2005 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
2006 {
2007 CORE_ADDR addr;
2008 enum target_hw_bp_type type;
2009 struct bp_location *loc, **tmp;
2010 int bitpos = 0, bitsize = 0;
2011
2012 if (value_bitsize (v) != 0)
2013 {
2014 /* Extract the bit parameters out from the bitfield
2015 sub-expression. */
2016 bitpos = value_bitpos (v);
2017 bitsize = value_bitsize (v);
2018 }
2019 else if (v == result && b->val_bitsize != 0)
2020 {
2021 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
2022 lvalue whose bit parameters are saved in the fields
2023 VAL_BITPOS and VAL_BITSIZE. */
2024 bitpos = b->val_bitpos;
2025 bitsize = b->val_bitsize;
2026 }
2027
2028 addr = value_address (v);
2029 if (bitsize != 0)
2030 {
2031 /* Skip the bytes that don't contain the bitfield. */
2032 addr += bitpos / 8;
2033 }
2034
2035 type = hw_write;
2036 if (b->base.type == bp_read_watchpoint)
2037 type = hw_read;
2038 else if (b->base.type == bp_access_watchpoint)
2039 type = hw_access;
2040
2041 loc = allocate_bp_location (&b->base);
2042 for (tmp = &(b->base.loc); *tmp != NULL; tmp = &((*tmp)->next))
2043 ;
2044 *tmp = loc;
2045 loc->gdbarch = get_type_arch (value_type (v));
2046
2047 loc->pspace = frame_pspace;
2048 loc->address = addr;
2049
2050 if (bitsize != 0)
2051 {
2052 /* Just cover the bytes that make up the bitfield. */
2053 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2054 }
2055 else
2056 loc->length = TYPE_LENGTH (value_type (v));
2057
2058 loc->watchpoint_type = type;
2059 }
2060 }
2061 }
2062
2063 /* Change the type of breakpoint between hardware assisted or
2064 an ordinary watchpoint depending on the hardware support
2065 and free hardware slots. REPARSE is set when the inferior
2066 is started. */
2067 if (reparse)
2068 {
2069 int reg_cnt;
2070 enum bp_loc_type loc_type;
2071 struct bp_location *bl;
2072
2073 reg_cnt = can_use_hardware_watchpoint (val_chain);
2074
2075 if (reg_cnt)
2076 {
2077 int i, target_resources_ok, other_type_used;
2078 enum bptype type;
2079
2080 /* Use an exact watchpoint when there's only one memory region to be
2081 watched, and only one debug register is needed to watch it. */
2082 b->exact = target_exact_watchpoints && reg_cnt == 1;
2083
2084 /* We need to determine how many resources are already
2085 used for all other hardware watchpoints plus this one
2086 to see if we still have enough resources to also fit
2087 this watchpoint in as well. */
2088
2089 /* If this is a software watchpoint, we try to turn it
2090 to a hardware one -- count resources as if B was of
2091 hardware watchpoint type. */
2092 type = b->base.type;
2093 if (type == bp_watchpoint)
2094 type = bp_hardware_watchpoint;
2095
2096 /* This watchpoint may or may not have been placed on
2097 the list yet at this point (it won't be in the list
2098 if we're trying to create it for the first time,
2099 through watch_command), so always account for it
2100 manually. */
2101
2102 /* Count resources used by all watchpoints except B. */
2103 i = hw_watchpoint_used_count_others (&b->base, type, &other_type_used);
2104
2105 /* Add in the resources needed for B. */
2106 i += hw_watchpoint_use_count (&b->base);
2107
2108 target_resources_ok
2109 = target_can_use_hardware_watchpoint (type, i, other_type_used);
2110 if (target_resources_ok <= 0)
2111 {
2112 int sw_mode = b->base.ops->works_in_software_mode (&b->base);
2113
2114 if (target_resources_ok == 0 && !sw_mode)
2115 error (_("Target does not support this type of "
2116 "hardware watchpoint."));
2117 else if (target_resources_ok < 0 && !sw_mode)
2118 error (_("There are not enough available hardware "
2119 "resources for this watchpoint."));
2120
2121 /* Downgrade to software watchpoint. */
2122 b->base.type = bp_watchpoint;
2123 }
2124 else
2125 {
2126 /* If this was a software watchpoint, we've just
2127 found we have enough resources to turn it to a
2128 hardware watchpoint. Otherwise, this is a
2129 nop. */
2130 b->base.type = type;
2131 }
2132 }
2133 else if (!b->base.ops->works_in_software_mode (&b->base))
2134 {
2135 if (!can_use_hw_watchpoints)
2136 error (_("Can't set read/access watchpoint when "
2137 "hardware watchpoints are disabled."));
2138 else
2139 error (_("Expression cannot be implemented with "
2140 "read/access watchpoint."));
2141 }
2142 else
2143 b->base.type = bp_watchpoint;
2144
2145 loc_type = (b->base.type == bp_watchpoint? bp_loc_other
2146 : bp_loc_hardware_watchpoint);
2147 for (bl = b->base.loc; bl; bl = bl->next)
2148 bl->loc_type = loc_type;
2149 }
2150
2151 for (v = val_chain; v; v = next)
2152 {
2153 next = value_next (v);
2154 if (v != b->val)
2155 value_free (v);
2156 }
2157
2158 /* If a software watchpoint is not watching any memory, then the
2159 above left it without any location set up. But,
2160 bpstat_stop_status requires a location to be able to report
2161 stops, so make sure there's at least a dummy one. */
2162 if (b->base.type == bp_watchpoint && b->base.loc == NULL)
2163 software_watchpoint_add_no_memory_location (&b->base, frame_pspace);
2164 }
2165 else if (!within_current_scope)
2166 {
2167 printf_filtered (_("\
2168 Watchpoint %d deleted because the program has left the block\n\
2169 in which its expression is valid.\n"),
2170 b->base.number);
2171 watchpoint_del_at_next_stop (b);
2172 }
2173
2174 /* Restore the selected frame. */
2175 if (frame_saved)
2176 select_frame (frame_find_by_id (saved_frame_id));
2177 }
2178
2179
2180 /* Returns 1 iff breakpoint location should be
2181 inserted in the inferior. We don't differentiate the type of BL's owner
2182 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2183 breakpoint_ops is not defined, because in insert_bp_location,
2184 tracepoint's insert_location will not be called. */
2185 static int
2186 should_be_inserted (struct bp_location *bl)
2187 {
2188 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
2189 return 0;
2190
2191 if (bl->owner->disposition == disp_del_at_next_stop)
2192 return 0;
2193
2194 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
2195 return 0;
2196
2197 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2198 return 0;
2199
2200 /* This is set for example, when we're attached to the parent of a
2201 vfork, and have detached from the child. The child is running
2202 free, and we expect it to do an exec or exit, at which point the
2203 OS makes the parent schedulable again (and the target reports
2204 that the vfork is done). Until the child is done with the shared
2205 memory region, do not insert breakpoints in the parent, otherwise
2206 the child could still trip on the parent's breakpoints. Since
2207 the parent is blocked anyway, it won't miss any breakpoint. */
2208 if (bl->pspace->breakpoints_not_allowed)
2209 return 0;
2210
2211 /* Don't insert a breakpoint if we're trying to step past its
2212 location, except if the breakpoint is a single-step breakpoint,
2213 and the breakpoint's thread is the thread which is stepping past
2214 a breakpoint. */
2215 if ((bl->loc_type == bp_loc_software_breakpoint
2216 || bl->loc_type == bp_loc_hardware_breakpoint)
2217 && stepping_past_instruction_at (bl->pspace->aspace,
2218 bl->address)
2219 /* The single-step breakpoint may be inserted at the location
2220 we're trying to step if the instruction branches to itself.
2221 However, the instruction won't be executed at all and it may
2222 break the semantics of the instruction, for example, the
2223 instruction is a conditional branch or updates some flags.
2224 We can't fix it unless GDB is able to emulate the instruction
2225 or switch to displaced stepping. */
2226 && !(bl->owner->type == bp_single_step
2227 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
2228 {
2229 if (debug_infrun)
2230 {
2231 fprintf_unfiltered (gdb_stdlog,
2232 "infrun: skipping breakpoint: "
2233 "stepping past insn at: %s\n",
2234 paddress (bl->gdbarch, bl->address));
2235 }
2236 return 0;
2237 }
2238
2239 /* Don't insert watchpoints if we're trying to step past the
2240 instruction that triggered one. */
2241 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2242 && stepping_past_nonsteppable_watchpoint ())
2243 {
2244 if (debug_infrun)
2245 {
2246 fprintf_unfiltered (gdb_stdlog,
2247 "infrun: stepping past non-steppable watchpoint. "
2248 "skipping watchpoint at %s:%d\n",
2249 paddress (bl->gdbarch, bl->address),
2250 bl->length);
2251 }
2252 return 0;
2253 }
2254
2255 return 1;
2256 }
2257
2258 /* Same as should_be_inserted but does the check assuming
2259 that the location is not duplicated. */
2260
2261 static int
2262 unduplicated_should_be_inserted (struct bp_location *bl)
2263 {
2264 int result;
2265 const int save_duplicate = bl->duplicate;
2266
2267 bl->duplicate = 0;
2268 result = should_be_inserted (bl);
2269 bl->duplicate = save_duplicate;
2270 return result;
2271 }
2272
2273 /* Parses a conditional described by an expression COND into an
2274 agent expression bytecode suitable for evaluation
2275 by the bytecode interpreter. Return NULL if there was
2276 any error during parsing. */
2277
2278 static struct agent_expr *
2279 parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2280 {
2281 struct agent_expr *aexpr = NULL;
2282
2283 if (!cond)
2284 return NULL;
2285
2286 /* We don't want to stop processing, so catch any errors
2287 that may show up. */
2288 TRY
2289 {
2290 aexpr = gen_eval_for_expr (scope, cond);
2291 }
2292
2293 CATCH (ex, RETURN_MASK_ERROR)
2294 {
2295 /* If we got here, it means the condition could not be parsed to a valid
2296 bytecode expression and thus can't be evaluated on the target's side.
2297 It's no use iterating through the conditions. */
2298 return NULL;
2299 }
2300 END_CATCH
2301
2302 /* We have a valid agent expression. */
2303 return aexpr;
2304 }
2305
2306 /* Based on location BL, create a list of breakpoint conditions to be
2307 passed on to the target. If we have duplicated locations with different
2308 conditions, we will add such conditions to the list. The idea is that the
2309 target will evaluate the list of conditions and will only notify GDB when
2310 one of them is true. */
2311
2312 static void
2313 build_target_condition_list (struct bp_location *bl)
2314 {
2315 struct bp_location **locp = NULL, **loc2p;
2316 int null_condition_or_parse_error = 0;
2317 int modified = bl->needs_update;
2318 struct bp_location *loc;
2319
2320 /* Release conditions left over from a previous insert. */
2321 VEC_free (agent_expr_p, bl->target_info.conditions);
2322
2323 /* This is only meaningful if the target is
2324 evaluating conditions and if the user has
2325 opted for condition evaluation on the target's
2326 side. */
2327 if (gdb_evaluates_breakpoint_condition_p ()
2328 || !target_supports_evaluation_of_breakpoint_conditions ())
2329 return;
2330
2331 /* Do a first pass to check for locations with no assigned
2332 conditions or conditions that fail to parse to a valid agent expression
2333 bytecode. If any of these happen, then it's no use to send conditions
2334 to the target since this location will always trigger and generate a
2335 response back to GDB. */
2336 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2337 {
2338 loc = (*loc2p);
2339 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2340 {
2341 if (modified)
2342 {
2343 struct agent_expr *aexpr;
2344
2345 /* Re-parse the conditions since something changed. In that
2346 case we already freed the condition bytecodes (see
2347 force_breakpoint_reinsertion). We just
2348 need to parse the condition to bytecodes again. */
2349 aexpr = parse_cond_to_aexpr (bl->address, loc->cond);
2350 loc->cond_bytecode = aexpr;
2351 }
2352
2353 /* If we have a NULL bytecode expression, it means something
2354 went wrong or we have a null condition expression. */
2355 if (!loc->cond_bytecode)
2356 {
2357 null_condition_or_parse_error = 1;
2358 break;
2359 }
2360 }
2361 }
2362
2363 /* If any of these happened, it means we will have to evaluate the conditions
2364 for the location's address on gdb's side. It is no use keeping bytecodes
2365 for all the other duplicate locations, thus we free all of them here.
2366
2367 This is so we have a finer control over which locations' conditions are
2368 being evaluated by GDB or the remote stub. */
2369 if (null_condition_or_parse_error)
2370 {
2371 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2372 {
2373 loc = (*loc2p);
2374 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2375 {
2376 /* Only go as far as the first NULL bytecode is
2377 located. */
2378 if (!loc->cond_bytecode)
2379 return;
2380
2381 free_agent_expr (loc->cond_bytecode);
2382 loc->cond_bytecode = NULL;
2383 }
2384 }
2385 }
2386
2387 /* No NULL conditions or failed bytecode generation. Build a condition list
2388 for this location's address. */
2389 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2390 {
2391 loc = (*loc2p);
2392 if (loc->cond
2393 && is_breakpoint (loc->owner)
2394 && loc->pspace->num == bl->pspace->num
2395 && loc->owner->enable_state == bp_enabled
2396 && loc->enabled)
2397 /* Add the condition to the vector. This will be used later to send the
2398 conditions to the target. */
2399 VEC_safe_push (agent_expr_p, bl->target_info.conditions,
2400 loc->cond_bytecode);
2401 }
2402
2403 return;
2404 }
2405
2406 /* Parses a command described by string CMD into an agent expression
2407 bytecode suitable for evaluation by the bytecode interpreter.
2408 Return NULL if there was any error during parsing. */
2409
2410 static struct agent_expr *
2411 parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2412 {
2413 struct cleanup *old_cleanups = 0;
2414 struct expression *expr, **argvec;
2415 struct agent_expr *aexpr = NULL;
2416 const char *cmdrest;
2417 const char *format_start, *format_end;
2418 struct format_piece *fpieces;
2419 int nargs;
2420 struct gdbarch *gdbarch = get_current_arch ();
2421
2422 if (!cmd)
2423 return NULL;
2424
2425 cmdrest = cmd;
2426
2427 if (*cmdrest == ',')
2428 ++cmdrest;
2429 cmdrest = skip_spaces_const (cmdrest);
2430
2431 if (*cmdrest++ != '"')
2432 error (_("No format string following the location"));
2433
2434 format_start = cmdrest;
2435
2436 fpieces = parse_format_string (&cmdrest);
2437
2438 old_cleanups = make_cleanup (free_format_pieces_cleanup, &fpieces);
2439
2440 format_end = cmdrest;
2441
2442 if (*cmdrest++ != '"')
2443 error (_("Bad format string, non-terminated '\"'."));
2444
2445 cmdrest = skip_spaces_const (cmdrest);
2446
2447 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2448 error (_("Invalid argument syntax"));
2449
2450 if (*cmdrest == ',')
2451 cmdrest++;
2452 cmdrest = skip_spaces_const (cmdrest);
2453
2454 /* For each argument, make an expression. */
2455
2456 argvec = (struct expression **) alloca (strlen (cmd)
2457 * sizeof (struct expression *));
2458
2459 nargs = 0;
2460 while (*cmdrest != '\0')
2461 {
2462 const char *cmd1;
2463
2464 cmd1 = cmdrest;
2465 expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2466 argvec[nargs++] = expr;
2467 cmdrest = cmd1;
2468 if (*cmdrest == ',')
2469 ++cmdrest;
2470 }
2471
2472 /* We don't want to stop processing, so catch any errors
2473 that may show up. */
2474 TRY
2475 {
2476 aexpr = gen_printf (scope, gdbarch, 0, 0,
2477 format_start, format_end - format_start,
2478 fpieces, nargs, argvec);
2479 }
2480 CATCH (ex, RETURN_MASK_ERROR)
2481 {
2482 /* If we got here, it means the command could not be parsed to a valid
2483 bytecode expression and thus can't be evaluated on the target's side.
2484 It's no use iterating through the other commands. */
2485 aexpr = NULL;
2486 }
2487 END_CATCH
2488
2489 do_cleanups (old_cleanups);
2490
2491 /* We have a valid agent expression, return it. */
2492 return aexpr;
2493 }
2494
2495 /* Based on location BL, create a list of breakpoint commands to be
2496 passed on to the target. If we have duplicated locations with
2497 different commands, we will add any such to the list. */
2498
2499 static void
2500 build_target_command_list (struct bp_location *bl)
2501 {
2502 struct bp_location **locp = NULL, **loc2p;
2503 int null_command_or_parse_error = 0;
2504 int modified = bl->needs_update;
2505 struct bp_location *loc;
2506
2507 /* Release commands left over from a previous insert. */
2508 VEC_free (agent_expr_p, bl->target_info.tcommands);
2509
2510 if (!target_can_run_breakpoint_commands ())
2511 return;
2512
2513 /* For now, limit to agent-style dprintf breakpoints. */
2514 if (dprintf_style != dprintf_style_agent)
2515 return;
2516
2517 /* For now, if we have any duplicate location that isn't a dprintf,
2518 don't install the target-side commands, as that would make the
2519 breakpoint not be reported to the core, and we'd lose
2520 control. */
2521 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2522 {
2523 loc = (*loc2p);
2524 if (is_breakpoint (loc->owner)
2525 && loc->pspace->num == bl->pspace->num
2526 && loc->owner->type != bp_dprintf)
2527 return;
2528 }
2529
2530 /* Do a first pass to check for locations with no assigned
2531 conditions or conditions that fail to parse to a valid agent expression
2532 bytecode. If any of these happen, then it's no use to send conditions
2533 to the target since this location will always trigger and generate a
2534 response back to GDB. */
2535 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2536 {
2537 loc = (*loc2p);
2538 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2539 {
2540 if (modified)
2541 {
2542 struct agent_expr *aexpr;
2543
2544 /* Re-parse the commands since something changed. In that
2545 case we already freed the command bytecodes (see
2546 force_breakpoint_reinsertion). We just
2547 need to parse the command to bytecodes again. */
2548 aexpr = parse_cmd_to_aexpr (bl->address,
2549 loc->owner->extra_string);
2550 loc->cmd_bytecode = aexpr;
2551 }
2552
2553 /* If we have a NULL bytecode expression, it means something
2554 went wrong or we have a null command expression. */
2555 if (!loc->cmd_bytecode)
2556 {
2557 null_command_or_parse_error = 1;
2558 break;
2559 }
2560 }
2561 }
2562
2563 /* If anything failed, then we're not doing target-side commands,
2564 and so clean up. */
2565 if (null_command_or_parse_error)
2566 {
2567 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2568 {
2569 loc = (*loc2p);
2570 if (is_breakpoint (loc->owner)
2571 && loc->pspace->num == bl->pspace->num)
2572 {
2573 /* Only go as far as the first NULL bytecode is
2574 located. */
2575 if (loc->cmd_bytecode == NULL)
2576 return;
2577
2578 free_agent_expr (loc->cmd_bytecode);
2579 loc->cmd_bytecode = NULL;
2580 }
2581 }
2582 }
2583
2584 /* No NULL commands or failed bytecode generation. Build a command list
2585 for this location's address. */
2586 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2587 {
2588 loc = (*loc2p);
2589 if (loc->owner->extra_string
2590 && is_breakpoint (loc->owner)
2591 && loc->pspace->num == bl->pspace->num
2592 && loc->owner->enable_state == bp_enabled
2593 && loc->enabled)
2594 /* Add the command to the vector. This will be used later
2595 to send the commands to the target. */
2596 VEC_safe_push (agent_expr_p, bl->target_info.tcommands,
2597 loc->cmd_bytecode);
2598 }
2599
2600 bl->target_info.persist = 0;
2601 /* Maybe flag this location as persistent. */
2602 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2603 bl->target_info.persist = 1;
2604 }
2605
2606 /* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2607 location. Any error messages are printed to TMP_ERROR_STREAM; and
2608 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
2609 Returns 0 for success, 1 if the bp_location type is not supported or
2610 -1 for failure.
2611
2612 NOTE drow/2003-09-09: This routine could be broken down to an
2613 object-style method for each breakpoint or catchpoint type. */
2614 static int
2615 insert_bp_location (struct bp_location *bl,
2616 struct ui_file *tmp_error_stream,
2617 int *disabled_breaks,
2618 int *hw_breakpoint_error,
2619 int *hw_bp_error_explained_already)
2620 {
2621 enum errors bp_err = GDB_NO_ERROR;
2622 const char *bp_err_message = NULL;
2623
2624 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2625 return 0;
2626
2627 /* Note we don't initialize bl->target_info, as that wipes out
2628 the breakpoint location's shadow_contents if the breakpoint
2629 is still inserted at that location. This in turn breaks
2630 target_read_memory which depends on these buffers when
2631 a memory read is requested at the breakpoint location:
2632 Once the target_info has been wiped, we fail to see that
2633 we have a breakpoint inserted at that address and thus
2634 read the breakpoint instead of returning the data saved in
2635 the breakpoint location's shadow contents. */
2636 bl->target_info.reqstd_address = bl->address;
2637 bl->target_info.placed_address_space = bl->pspace->aspace;
2638 bl->target_info.length = bl->length;
2639
2640 /* When working with target-side conditions, we must pass all the conditions
2641 for the same breakpoint address down to the target since GDB will not
2642 insert those locations. With a list of breakpoint conditions, the target
2643 can decide when to stop and notify GDB. */
2644
2645 if (is_breakpoint (bl->owner))
2646 {
2647 build_target_condition_list (bl);
2648 build_target_command_list (bl);
2649 /* Reset the modification marker. */
2650 bl->needs_update = 0;
2651 }
2652
2653 if (bl->loc_type == bp_loc_software_breakpoint
2654 || bl->loc_type == bp_loc_hardware_breakpoint)
2655 {
2656 if (bl->owner->type != bp_hardware_breakpoint)
2657 {
2658 /* If the explicitly specified breakpoint type
2659 is not hardware breakpoint, check the memory map to see
2660 if the breakpoint address is in read only memory or not.
2661
2662 Two important cases are:
2663 - location type is not hardware breakpoint, memory
2664 is readonly. We change the type of the location to
2665 hardware breakpoint.
2666 - location type is hardware breakpoint, memory is
2667 read-write. This means we've previously made the
2668 location hardware one, but then the memory map changed,
2669 so we undo.
2670
2671 When breakpoints are removed, remove_breakpoints will use
2672 location types we've just set here, the only possible
2673 problem is that memory map has changed during running
2674 program, but it's not going to work anyway with current
2675 gdb. */
2676 struct mem_region *mr
2677 = lookup_mem_region (bl->target_info.reqstd_address);
2678
2679 if (mr)
2680 {
2681 if (automatic_hardware_breakpoints)
2682 {
2683 enum bp_loc_type new_type;
2684
2685 if (mr->attrib.mode != MEM_RW)
2686 new_type = bp_loc_hardware_breakpoint;
2687 else
2688 new_type = bp_loc_software_breakpoint;
2689
2690 if (new_type != bl->loc_type)
2691 {
2692 static int said = 0;
2693
2694 bl->loc_type = new_type;
2695 if (!said)
2696 {
2697 fprintf_filtered (gdb_stdout,
2698 _("Note: automatically using "
2699 "hardware breakpoints for "
2700 "read-only addresses.\n"));
2701 said = 1;
2702 }
2703 }
2704 }
2705 else if (bl->loc_type == bp_loc_software_breakpoint
2706 && mr->attrib.mode != MEM_RW)
2707 {
2708 fprintf_unfiltered (tmp_error_stream,
2709 _("Cannot insert breakpoint %d.\n"
2710 "Cannot set software breakpoint "
2711 "at read-only address %s\n"),
2712 bl->owner->number,
2713 paddress (bl->gdbarch, bl->address));
2714 return 1;
2715 }
2716 }
2717 }
2718
2719 /* First check to see if we have to handle an overlay. */
2720 if (overlay_debugging == ovly_off
2721 || bl->section == NULL
2722 || !(section_is_overlay (bl->section)))
2723 {
2724 /* No overlay handling: just set the breakpoint. */
2725 TRY
2726 {
2727 int val;
2728
2729 val = bl->owner->ops->insert_location (bl);
2730 if (val)
2731 bp_err = GENERIC_ERROR;
2732 }
2733 CATCH (e, RETURN_MASK_ALL)
2734 {
2735 bp_err = e.error;
2736 bp_err_message = e.message;
2737 }
2738 END_CATCH
2739 }
2740 else
2741 {
2742 /* This breakpoint is in an overlay section.
2743 Shall we set a breakpoint at the LMA? */
2744 if (!overlay_events_enabled)
2745 {
2746 /* Yes -- overlay event support is not active,
2747 so we must try to set a breakpoint at the LMA.
2748 This will not work for a hardware breakpoint. */
2749 if (bl->loc_type == bp_loc_hardware_breakpoint)
2750 warning (_("hardware breakpoint %d not supported in overlay!"),
2751 bl->owner->number);
2752 else
2753 {
2754 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2755 bl->section);
2756 /* Set a software (trap) breakpoint at the LMA. */
2757 bl->overlay_target_info = bl->target_info;
2758 bl->overlay_target_info.reqstd_address = addr;
2759
2760 /* No overlay handling: just set the breakpoint. */
2761 TRY
2762 {
2763 int val;
2764
2765 val = target_insert_breakpoint (bl->gdbarch,
2766 &bl->overlay_target_info);
2767 if (val)
2768 bp_err = GENERIC_ERROR;
2769 }
2770 CATCH (e, RETURN_MASK_ALL)
2771 {
2772 bp_err = e.error;
2773 bp_err_message = e.message;
2774 }
2775 END_CATCH
2776
2777 if (bp_err != GDB_NO_ERROR)
2778 fprintf_unfiltered (tmp_error_stream,
2779 "Overlay breakpoint %d "
2780 "failed: in ROM?\n",
2781 bl->owner->number);
2782 }
2783 }
2784 /* Shall we set a breakpoint at the VMA? */
2785 if (section_is_mapped (bl->section))
2786 {
2787 /* Yes. This overlay section is mapped into memory. */
2788 TRY
2789 {
2790 int val;
2791
2792 val = bl->owner->ops->insert_location (bl);
2793 if (val)
2794 bp_err = GENERIC_ERROR;
2795 }
2796 CATCH (e, RETURN_MASK_ALL)
2797 {
2798 bp_err = e.error;
2799 bp_err_message = e.message;
2800 }
2801 END_CATCH
2802 }
2803 else
2804 {
2805 /* No. This breakpoint will not be inserted.
2806 No error, but do not mark the bp as 'inserted'. */
2807 return 0;
2808 }
2809 }
2810
2811 if (bp_err != GDB_NO_ERROR)
2812 {
2813 /* Can't set the breakpoint. */
2814
2815 /* In some cases, we might not be able to insert a
2816 breakpoint in a shared library that has already been
2817 removed, but we have not yet processed the shlib unload
2818 event. Unfortunately, some targets that implement
2819 breakpoint insertion themselves can't tell why the
2820 breakpoint insertion failed (e.g., the remote target
2821 doesn't define error codes), so we must treat generic
2822 errors as memory errors. */
2823 if ((bp_err == GENERIC_ERROR || bp_err == MEMORY_ERROR)
2824 && bl->loc_type == bp_loc_software_breakpoint
2825 && (solib_name_from_address (bl->pspace, bl->address)
2826 || shared_objfile_contains_address_p (bl->pspace,
2827 bl->address)))
2828 {
2829 /* See also: disable_breakpoints_in_shlibs. */
2830 bl->shlib_disabled = 1;
2831 observer_notify_breakpoint_modified (bl->owner);
2832 if (!*disabled_breaks)
2833 {
2834 fprintf_unfiltered (tmp_error_stream,
2835 "Cannot insert breakpoint %d.\n",
2836 bl->owner->number);
2837 fprintf_unfiltered (tmp_error_stream,
2838 "Temporarily disabling shared "
2839 "library breakpoints:\n");
2840 }
2841 *disabled_breaks = 1;
2842 fprintf_unfiltered (tmp_error_stream,
2843 "breakpoint #%d\n", bl->owner->number);
2844 return 0;
2845 }
2846 else
2847 {
2848 if (bl->loc_type == bp_loc_hardware_breakpoint)
2849 {
2850 *hw_breakpoint_error = 1;
2851 *hw_bp_error_explained_already = bp_err_message != NULL;
2852 fprintf_unfiltered (tmp_error_stream,
2853 "Cannot insert hardware breakpoint %d%s",
2854 bl->owner->number, bp_err_message ? ":" : ".\n");
2855 if (bp_err_message != NULL)
2856 fprintf_unfiltered (tmp_error_stream, "%s.\n", bp_err_message);
2857 }
2858 else
2859 {
2860 if (bp_err_message == NULL)
2861 {
2862 char *message
2863 = memory_error_message (TARGET_XFER_E_IO,
2864 bl->gdbarch, bl->address);
2865 struct cleanup *old_chain = make_cleanup (xfree, message);
2866
2867 fprintf_unfiltered (tmp_error_stream,
2868 "Cannot insert breakpoint %d.\n"
2869 "%s\n",
2870 bl->owner->number, message);
2871 do_cleanups (old_chain);
2872 }
2873 else
2874 {
2875 fprintf_unfiltered (tmp_error_stream,
2876 "Cannot insert breakpoint %d: %s\n",
2877 bl->owner->number,
2878 bp_err_message);
2879 }
2880 }
2881 return 1;
2882
2883 }
2884 }
2885 else
2886 bl->inserted = 1;
2887
2888 return 0;
2889 }
2890
2891 else if (bl->loc_type == bp_loc_hardware_watchpoint
2892 /* NOTE drow/2003-09-08: This state only exists for removing
2893 watchpoints. It's not clear that it's necessary... */
2894 && bl->owner->disposition != disp_del_at_next_stop)
2895 {
2896 int val;
2897
2898 gdb_assert (bl->owner->ops != NULL
2899 && bl->owner->ops->insert_location != NULL);
2900
2901 val = bl->owner->ops->insert_location (bl);
2902
2903 /* If trying to set a read-watchpoint, and it turns out it's not
2904 supported, try emulating one with an access watchpoint. */
2905 if (val == 1 && bl->watchpoint_type == hw_read)
2906 {
2907 struct bp_location *loc, **loc_temp;
2908
2909 /* But don't try to insert it, if there's already another
2910 hw_access location that would be considered a duplicate
2911 of this one. */
2912 ALL_BP_LOCATIONS (loc, loc_temp)
2913 if (loc != bl
2914 && loc->watchpoint_type == hw_access
2915 && watchpoint_locations_match (bl, loc))
2916 {
2917 bl->duplicate = 1;
2918 bl->inserted = 1;
2919 bl->target_info = loc->target_info;
2920 bl->watchpoint_type = hw_access;
2921 val = 0;
2922 break;
2923 }
2924
2925 if (val == 1)
2926 {
2927 bl->watchpoint_type = hw_access;
2928 val = bl->owner->ops->insert_location (bl);
2929
2930 if (val)
2931 /* Back to the original value. */
2932 bl->watchpoint_type = hw_read;
2933 }
2934 }
2935
2936 bl->inserted = (val == 0);
2937 }
2938
2939 else if (bl->owner->type == bp_catchpoint)
2940 {
2941 int val;
2942
2943 gdb_assert (bl->owner->ops != NULL
2944 && bl->owner->ops->insert_location != NULL);
2945
2946 val = bl->owner->ops->insert_location (bl);
2947 if (val)
2948 {
2949 bl->owner->enable_state = bp_disabled;
2950
2951 if (val == 1)
2952 warning (_("\
2953 Error inserting catchpoint %d: Your system does not support this type\n\
2954 of catchpoint."), bl->owner->number);
2955 else
2956 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2957 }
2958
2959 bl->inserted = (val == 0);
2960
2961 /* We've already printed an error message if there was a problem
2962 inserting this catchpoint, and we've disabled the catchpoint,
2963 so just return success. */
2964 return 0;
2965 }
2966
2967 return 0;
2968 }
2969
2970 /* This function is called when program space PSPACE is about to be
2971 deleted. It takes care of updating breakpoints to not reference
2972 PSPACE anymore. */
2973
2974 void
2975 breakpoint_program_space_exit (struct program_space *pspace)
2976 {
2977 struct breakpoint *b, *b_temp;
2978 struct bp_location *loc, **loc_temp;
2979
2980 /* Remove any breakpoint that was set through this program space. */
2981 ALL_BREAKPOINTS_SAFE (b, b_temp)
2982 {
2983 if (b->pspace == pspace)
2984 delete_breakpoint (b);
2985 }
2986
2987 /* Breakpoints set through other program spaces could have locations
2988 bound to PSPACE as well. Remove those. */
2989 ALL_BP_LOCATIONS (loc, loc_temp)
2990 {
2991 struct bp_location *tmp;
2992
2993 if (loc->pspace == pspace)
2994 {
2995 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
2996 if (loc->owner->loc == loc)
2997 loc->owner->loc = loc->next;
2998 else
2999 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
3000 if (tmp->next == loc)
3001 {
3002 tmp->next = loc->next;
3003 break;
3004 }
3005 }
3006 }
3007
3008 /* Now update the global location list to permanently delete the
3009 removed locations above. */
3010 update_global_location_list (UGLL_DONT_INSERT);
3011 }
3012
3013 /* Make sure all breakpoints are inserted in inferior.
3014 Throws exception on any error.
3015 A breakpoint that is already inserted won't be inserted
3016 again, so calling this function twice is safe. */
3017 void
3018 insert_breakpoints (void)
3019 {
3020 struct breakpoint *bpt;
3021
3022 ALL_BREAKPOINTS (bpt)
3023 if (is_hardware_watchpoint (bpt))
3024 {
3025 struct watchpoint *w = (struct watchpoint *) bpt;
3026
3027 update_watchpoint (w, 0 /* don't reparse. */);
3028 }
3029
3030 /* Updating watchpoints creates new locations, so update the global
3031 location list. Explicitly tell ugll to insert locations and
3032 ignore breakpoints_always_inserted_mode. */
3033 update_global_location_list (UGLL_INSERT);
3034 }
3035
3036 /* Invoke CALLBACK for each of bp_location. */
3037
3038 void
3039 iterate_over_bp_locations (walk_bp_location_callback callback)
3040 {
3041 struct bp_location *loc, **loc_tmp;
3042
3043 ALL_BP_LOCATIONS (loc, loc_tmp)
3044 {
3045 callback (loc, NULL);
3046 }
3047 }
3048
3049 /* This is used when we need to synch breakpoint conditions between GDB and the
3050 target. It is the case with deleting and disabling of breakpoints when using
3051 always-inserted mode. */
3052
3053 static void
3054 update_inserted_breakpoint_locations (void)
3055 {
3056 struct bp_location *bl, **blp_tmp;
3057 int error_flag = 0;
3058 int val = 0;
3059 int disabled_breaks = 0;
3060 int hw_breakpoint_error = 0;
3061 int hw_bp_details_reported = 0;
3062
3063 struct ui_file *tmp_error_stream = mem_fileopen ();
3064 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
3065
3066 /* Explicitly mark the warning -- this will only be printed if
3067 there was an error. */
3068 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
3069
3070 save_current_space_and_thread ();
3071
3072 ALL_BP_LOCATIONS (bl, blp_tmp)
3073 {
3074 /* We only want to update software breakpoints and hardware
3075 breakpoints. */
3076 if (!is_breakpoint (bl->owner))
3077 continue;
3078
3079 /* We only want to update locations that are already inserted
3080 and need updating. This is to avoid unwanted insertion during
3081 deletion of breakpoints. */
3082 if (!bl->inserted || (bl->inserted && !bl->needs_update))
3083 continue;
3084
3085 switch_to_program_space_and_thread (bl->pspace);
3086
3087 /* For targets that support global breakpoints, there's no need
3088 to select an inferior to insert breakpoint to. In fact, even
3089 if we aren't attached to any process yet, we should still
3090 insert breakpoints. */
3091 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3092 && ptid_equal (inferior_ptid, null_ptid))
3093 continue;
3094
3095 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
3096 &hw_breakpoint_error, &hw_bp_details_reported);
3097 if (val)
3098 error_flag = val;
3099 }
3100
3101 if (error_flag)
3102 {
3103 target_terminal_ours_for_output ();
3104 error_stream (tmp_error_stream);
3105 }
3106
3107 do_cleanups (cleanups);
3108 }
3109
3110 /* Used when starting or continuing the program. */
3111
3112 static void
3113 insert_breakpoint_locations (void)
3114 {
3115 struct breakpoint *bpt;
3116 struct bp_location *bl, **blp_tmp;
3117 int error_flag = 0;
3118 int val = 0;
3119 int disabled_breaks = 0;
3120 int hw_breakpoint_error = 0;
3121 int hw_bp_error_explained_already = 0;
3122
3123 struct ui_file *tmp_error_stream = mem_fileopen ();
3124 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
3125
3126 /* Explicitly mark the warning -- this will only be printed if
3127 there was an error. */
3128 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
3129
3130 save_current_space_and_thread ();
3131
3132 ALL_BP_LOCATIONS (bl, blp_tmp)
3133 {
3134 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
3135 continue;
3136
3137 /* There is no point inserting thread-specific breakpoints if
3138 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3139 has BL->OWNER always non-NULL. */
3140 if (bl->owner->thread != -1
3141 && !valid_global_thread_id (bl->owner->thread))
3142 continue;
3143
3144 switch_to_program_space_and_thread (bl->pspace);
3145
3146 /* For targets that support global breakpoints, there's no need
3147 to select an inferior to insert breakpoint to. In fact, even
3148 if we aren't attached to any process yet, we should still
3149 insert breakpoints. */
3150 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3151 && ptid_equal (inferior_ptid, null_ptid))
3152 continue;
3153
3154 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
3155 &hw_breakpoint_error, &hw_bp_error_explained_already);
3156 if (val)
3157 error_flag = val;
3158 }
3159
3160 /* If we failed to insert all locations of a watchpoint, remove
3161 them, as half-inserted watchpoint is of limited use. */
3162 ALL_BREAKPOINTS (bpt)
3163 {
3164 int some_failed = 0;
3165 struct bp_location *loc;
3166
3167 if (!is_hardware_watchpoint (bpt))
3168 continue;
3169
3170 if (!breakpoint_enabled (bpt))
3171 continue;
3172
3173 if (bpt->disposition == disp_del_at_next_stop)
3174 continue;
3175
3176 for (loc = bpt->loc; loc; loc = loc->next)
3177 if (!loc->inserted && should_be_inserted (loc))
3178 {
3179 some_failed = 1;
3180 break;
3181 }
3182 if (some_failed)
3183 {
3184 for (loc = bpt->loc; loc; loc = loc->next)
3185 if (loc->inserted)
3186 remove_breakpoint (loc);
3187
3188 hw_breakpoint_error = 1;
3189 fprintf_unfiltered (tmp_error_stream,
3190 "Could not insert hardware watchpoint %d.\n",
3191 bpt->number);
3192 error_flag = -1;
3193 }
3194 }
3195
3196 if (error_flag)
3197 {
3198 /* If a hardware breakpoint or watchpoint was inserted, add a
3199 message about possibly exhausted resources. */
3200 if (hw_breakpoint_error && !hw_bp_error_explained_already)
3201 {
3202 fprintf_unfiltered (tmp_error_stream,
3203 "Could not insert hardware breakpoints:\n\
3204 You may have requested too many hardware breakpoints/watchpoints.\n");
3205 }
3206 target_terminal_ours_for_output ();
3207 error_stream (tmp_error_stream);
3208 }
3209
3210 do_cleanups (cleanups);
3211 }
3212
3213 /* Used when the program stops.
3214 Returns zero if successful, or non-zero if there was a problem
3215 removing a breakpoint location. */
3216
3217 int
3218 remove_breakpoints (void)
3219 {
3220 struct bp_location *bl, **blp_tmp;
3221 int val = 0;
3222
3223 ALL_BP_LOCATIONS (bl, blp_tmp)
3224 {
3225 if (bl->inserted && !is_tracepoint (bl->owner))
3226 val |= remove_breakpoint (bl);
3227 }
3228 return val;
3229 }
3230
3231 /* When a thread exits, remove breakpoints that are related to
3232 that thread. */
3233
3234 static void
3235 remove_threaded_breakpoints (struct thread_info *tp, int silent)
3236 {
3237 struct breakpoint *b, *b_tmp;
3238
3239 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3240 {
3241 if (b->thread == tp->global_num && user_breakpoint_p (b))
3242 {
3243 b->disposition = disp_del_at_next_stop;
3244
3245 printf_filtered (_("\
3246 Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3247 b->number, print_thread_id (tp));
3248
3249 /* Hide it from the user. */
3250 b->number = 0;
3251 }
3252 }
3253 }
3254
3255 /* Remove breakpoints of process PID. */
3256
3257 int
3258 remove_breakpoints_pid (int pid)
3259 {
3260 struct bp_location *bl, **blp_tmp;
3261 int val;
3262 struct inferior *inf = find_inferior_pid (pid);
3263
3264 ALL_BP_LOCATIONS (bl, blp_tmp)
3265 {
3266 if (bl->pspace != inf->pspace)
3267 continue;
3268
3269 if (bl->inserted && !bl->target_info.persist)
3270 {
3271 val = remove_breakpoint (bl);
3272 if (val != 0)
3273 return val;
3274 }
3275 }
3276 return 0;
3277 }
3278
3279 int
3280 reattach_breakpoints (int pid)
3281 {
3282 struct cleanup *old_chain;
3283 struct bp_location *bl, **blp_tmp;
3284 int val;
3285 struct ui_file *tmp_error_stream;
3286 int dummy1 = 0, dummy2 = 0, dummy3 = 0;
3287 struct inferior *inf;
3288 struct thread_info *tp;
3289
3290 tp = any_live_thread_of_process (pid);
3291 if (tp == NULL)
3292 return 1;
3293
3294 inf = find_inferior_pid (pid);
3295 old_chain = save_inferior_ptid ();
3296
3297 inferior_ptid = tp->ptid;
3298
3299 tmp_error_stream = mem_fileopen ();
3300 make_cleanup_ui_file_delete (tmp_error_stream);
3301
3302 ALL_BP_LOCATIONS (bl, blp_tmp)
3303 {
3304 if (bl->pspace != inf->pspace)
3305 continue;
3306
3307 if (bl->inserted)
3308 {
3309 bl->inserted = 0;
3310 val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2, &dummy3);
3311 if (val != 0)
3312 {
3313 do_cleanups (old_chain);
3314 return val;
3315 }
3316 }
3317 }
3318 do_cleanups (old_chain);
3319 return 0;
3320 }
3321
3322 static int internal_breakpoint_number = -1;
3323
3324 /* Set the breakpoint number of B, depending on the value of INTERNAL.
3325 If INTERNAL is non-zero, the breakpoint number will be populated
3326 from internal_breakpoint_number and that variable decremented.
3327 Otherwise the breakpoint number will be populated from
3328 breakpoint_count and that value incremented. Internal breakpoints
3329 do not set the internal var bpnum. */
3330 static void
3331 set_breakpoint_number (int internal, struct breakpoint *b)
3332 {
3333 if (internal)
3334 b->number = internal_breakpoint_number--;
3335 else
3336 {
3337 set_breakpoint_count (breakpoint_count + 1);
3338 b->number = breakpoint_count;
3339 }
3340 }
3341
3342 static struct breakpoint *
3343 create_internal_breakpoint (struct gdbarch *gdbarch,
3344 CORE_ADDR address, enum bptype type,
3345 const struct breakpoint_ops *ops)
3346 {
3347 struct symtab_and_line sal;
3348 struct breakpoint *b;
3349
3350 init_sal (&sal); /* Initialize to zeroes. */
3351
3352 sal.pc = address;
3353 sal.section = find_pc_overlay (sal.pc);
3354 sal.pspace = current_program_space;
3355
3356 b = set_raw_breakpoint (gdbarch, sal, type, ops);
3357 b->number = internal_breakpoint_number--;
3358 b->disposition = disp_donttouch;
3359
3360 return b;
3361 }
3362
3363 static const char *const longjmp_names[] =
3364 {
3365 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3366 };
3367 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3368
3369 /* Per-objfile data private to breakpoint.c. */
3370 struct breakpoint_objfile_data
3371 {
3372 /* Minimal symbol for "_ovly_debug_event" (if any). */
3373 struct bound_minimal_symbol overlay_msym;
3374
3375 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3376 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
3377
3378 /* True if we have looked for longjmp probes. */
3379 int longjmp_searched;
3380
3381 /* SystemTap probe points for longjmp (if any). */
3382 VEC (probe_p) *longjmp_probes;
3383
3384 /* Minimal symbol for "std::terminate()" (if any). */
3385 struct bound_minimal_symbol terminate_msym;
3386
3387 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3388 struct bound_minimal_symbol exception_msym;
3389
3390 /* True if we have looked for exception probes. */
3391 int exception_searched;
3392
3393 /* SystemTap probe points for unwinding (if any). */
3394 VEC (probe_p) *exception_probes;
3395 };
3396
3397 static const struct objfile_data *breakpoint_objfile_key;
3398
3399 /* Minimal symbol not found sentinel. */
3400 static struct minimal_symbol msym_not_found;
3401
3402 /* Returns TRUE if MSYM point to the "not found" sentinel. */
3403
3404 static int
3405 msym_not_found_p (const struct minimal_symbol *msym)
3406 {
3407 return msym == &msym_not_found;
3408 }
3409
3410 /* Return per-objfile data needed by breakpoint.c.
3411 Allocate the data if necessary. */
3412
3413 static struct breakpoint_objfile_data *
3414 get_breakpoint_objfile_data (struct objfile *objfile)
3415 {
3416 struct breakpoint_objfile_data *bp_objfile_data;
3417
3418 bp_objfile_data = ((struct breakpoint_objfile_data *)
3419 objfile_data (objfile, breakpoint_objfile_key));
3420 if (bp_objfile_data == NULL)
3421 {
3422 bp_objfile_data =
3423 XOBNEW (&objfile->objfile_obstack, struct breakpoint_objfile_data);
3424
3425 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
3426 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3427 }
3428 return bp_objfile_data;
3429 }
3430
3431 static void
3432 free_breakpoint_probes (struct objfile *obj, void *data)
3433 {
3434 struct breakpoint_objfile_data *bp_objfile_data
3435 = (struct breakpoint_objfile_data *) data;
3436
3437 VEC_free (probe_p, bp_objfile_data->longjmp_probes);
3438 VEC_free (probe_p, bp_objfile_data->exception_probes);
3439 }
3440
3441 static void
3442 create_overlay_event_breakpoint (void)
3443 {
3444 struct objfile *objfile;
3445 const char *const func_name = "_ovly_debug_event";
3446
3447 ALL_OBJFILES (objfile)
3448 {
3449 struct breakpoint *b;
3450 struct breakpoint_objfile_data *bp_objfile_data;
3451 CORE_ADDR addr;
3452 struct explicit_location explicit_loc;
3453
3454 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3455
3456 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
3457 continue;
3458
3459 if (bp_objfile_data->overlay_msym.minsym == NULL)
3460 {
3461 struct bound_minimal_symbol m;
3462
3463 m = lookup_minimal_symbol_text (func_name, objfile);
3464 if (m.minsym == NULL)
3465 {
3466 /* Avoid future lookups in this objfile. */
3467 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
3468 continue;
3469 }
3470 bp_objfile_data->overlay_msym = m;
3471 }
3472
3473 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
3474 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3475 bp_overlay_event,
3476 &internal_breakpoint_ops);
3477 initialize_explicit_location (&explicit_loc);
3478 explicit_loc.function_name = ASTRDUP (func_name);
3479 b->location = new_explicit_location (&explicit_loc);
3480
3481 if (overlay_debugging == ovly_auto)
3482 {
3483 b->enable_state = bp_enabled;
3484 overlay_events_enabled = 1;
3485 }
3486 else
3487 {
3488 b->enable_state = bp_disabled;
3489 overlay_events_enabled = 0;
3490 }
3491 }
3492 }
3493
3494 static void
3495 create_longjmp_master_breakpoint (void)
3496 {
3497 struct program_space *pspace;
3498 struct cleanup *old_chain;
3499
3500 old_chain = save_current_program_space ();
3501
3502 ALL_PSPACES (pspace)
3503 {
3504 struct objfile *objfile;
3505
3506 set_current_program_space (pspace);
3507
3508 ALL_OBJFILES (objfile)
3509 {
3510 int i;
3511 struct gdbarch *gdbarch;
3512 struct breakpoint_objfile_data *bp_objfile_data;
3513
3514 gdbarch = get_objfile_arch (objfile);
3515
3516 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3517
3518 if (!bp_objfile_data->longjmp_searched)
3519 {
3520 VEC (probe_p) *ret;
3521
3522 ret = find_probes_in_objfile (objfile, "libc", "longjmp");
3523 if (ret != NULL)
3524 {
3525 /* We are only interested in checking one element. */
3526 struct probe *p = VEC_index (probe_p, ret, 0);
3527
3528 if (!can_evaluate_probe_arguments (p))
3529 {
3530 /* We cannot use the probe interface here, because it does
3531 not know how to evaluate arguments. */
3532 VEC_free (probe_p, ret);
3533 ret = NULL;
3534 }
3535 }
3536 bp_objfile_data->longjmp_probes = ret;
3537 bp_objfile_data->longjmp_searched = 1;
3538 }
3539
3540 if (bp_objfile_data->longjmp_probes != NULL)
3541 {
3542 int i;
3543 struct probe *probe;
3544 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3545
3546 for (i = 0;
3547 VEC_iterate (probe_p,
3548 bp_objfile_data->longjmp_probes,
3549 i, probe);
3550 ++i)
3551 {
3552 struct breakpoint *b;
3553
3554 b = create_internal_breakpoint (gdbarch,
3555 get_probe_address (probe,
3556 objfile),
3557 bp_longjmp_master,
3558 &internal_breakpoint_ops);
3559 b->location
3560 = new_probe_location ("-probe-stap libc:longjmp");
3561 b->enable_state = bp_disabled;
3562 }
3563
3564 continue;
3565 }
3566
3567 if (!gdbarch_get_longjmp_target_p (gdbarch))
3568 continue;
3569
3570 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
3571 {
3572 struct breakpoint *b;
3573 const char *func_name;
3574 CORE_ADDR addr;
3575 struct explicit_location explicit_loc;
3576
3577 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3578 continue;
3579
3580 func_name = longjmp_names[i];
3581 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3582 {
3583 struct bound_minimal_symbol m;
3584
3585 m = lookup_minimal_symbol_text (func_name, objfile);
3586 if (m.minsym == NULL)
3587 {
3588 /* Prevent future lookups in this objfile. */
3589 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
3590 continue;
3591 }
3592 bp_objfile_data->longjmp_msym[i] = m;
3593 }
3594
3595 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
3596 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3597 &internal_breakpoint_ops);
3598 initialize_explicit_location (&explicit_loc);
3599 explicit_loc.function_name = ASTRDUP (func_name);
3600 b->location = new_explicit_location (&explicit_loc);
3601 b->enable_state = bp_disabled;
3602 }
3603 }
3604 }
3605
3606 do_cleanups (old_chain);
3607 }
3608
3609 /* Create a master std::terminate breakpoint. */
3610 static void
3611 create_std_terminate_master_breakpoint (void)
3612 {
3613 struct program_space *pspace;
3614 struct cleanup *old_chain;
3615 const char *const func_name = "std::terminate()";
3616
3617 old_chain = save_current_program_space ();
3618
3619 ALL_PSPACES (pspace)
3620 {
3621 struct objfile *objfile;
3622 CORE_ADDR addr;
3623
3624 set_current_program_space (pspace);
3625
3626 ALL_OBJFILES (objfile)
3627 {
3628 struct breakpoint *b;
3629 struct breakpoint_objfile_data *bp_objfile_data;
3630 struct explicit_location explicit_loc;
3631
3632 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3633
3634 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3635 continue;
3636
3637 if (bp_objfile_data->terminate_msym.minsym == NULL)
3638 {
3639 struct bound_minimal_symbol m;
3640
3641 m = lookup_minimal_symbol (func_name, NULL, objfile);
3642 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3643 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
3644 {
3645 /* Prevent future lookups in this objfile. */
3646 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3647 continue;
3648 }
3649 bp_objfile_data->terminate_msym = m;
3650 }
3651
3652 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3653 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3654 bp_std_terminate_master,
3655 &internal_breakpoint_ops);
3656 initialize_explicit_location (&explicit_loc);
3657 explicit_loc.function_name = ASTRDUP (func_name);
3658 b->location = new_explicit_location (&explicit_loc);
3659 b->enable_state = bp_disabled;
3660 }
3661 }
3662
3663 do_cleanups (old_chain);
3664 }
3665
3666 /* Install a master breakpoint on the unwinder's debug hook. */
3667
3668 static void
3669 create_exception_master_breakpoint (void)
3670 {
3671 struct objfile *objfile;
3672 const char *const func_name = "_Unwind_DebugHook";
3673
3674 ALL_OBJFILES (objfile)
3675 {
3676 struct breakpoint *b;
3677 struct gdbarch *gdbarch;
3678 struct breakpoint_objfile_data *bp_objfile_data;
3679 CORE_ADDR addr;
3680 struct explicit_location explicit_loc;
3681
3682 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3683
3684 /* We prefer the SystemTap probe point if it exists. */
3685 if (!bp_objfile_data->exception_searched)
3686 {
3687 VEC (probe_p) *ret;
3688
3689 ret = find_probes_in_objfile (objfile, "libgcc", "unwind");
3690
3691 if (ret != NULL)
3692 {
3693 /* We are only interested in checking one element. */
3694 struct probe *p = VEC_index (probe_p, ret, 0);
3695
3696 if (!can_evaluate_probe_arguments (p))
3697 {
3698 /* We cannot use the probe interface here, because it does
3699 not know how to evaluate arguments. */
3700 VEC_free (probe_p, ret);
3701 ret = NULL;
3702 }
3703 }
3704 bp_objfile_data->exception_probes = ret;
3705 bp_objfile_data->exception_searched = 1;
3706 }
3707
3708 if (bp_objfile_data->exception_probes != NULL)
3709 {
3710 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3711 int i;
3712 struct probe *probe;
3713
3714 for (i = 0;
3715 VEC_iterate (probe_p,
3716 bp_objfile_data->exception_probes,
3717 i, probe);
3718 ++i)
3719 {
3720 struct breakpoint *b;
3721
3722 b = create_internal_breakpoint (gdbarch,
3723 get_probe_address (probe,
3724 objfile),
3725 bp_exception_master,
3726 &internal_breakpoint_ops);
3727 b->location
3728 = new_probe_location ("-probe-stap libgcc:unwind");
3729 b->enable_state = bp_disabled;
3730 }
3731
3732 continue;
3733 }
3734
3735 /* Otherwise, try the hook function. */
3736
3737 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3738 continue;
3739
3740 gdbarch = get_objfile_arch (objfile);
3741
3742 if (bp_objfile_data->exception_msym.minsym == NULL)
3743 {
3744 struct bound_minimal_symbol debug_hook;
3745
3746 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3747 if (debug_hook.minsym == NULL)
3748 {
3749 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3750 continue;
3751 }
3752
3753 bp_objfile_data->exception_msym = debug_hook;
3754 }
3755
3756 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
3757 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3758 &current_target);
3759 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3760 &internal_breakpoint_ops);
3761 initialize_explicit_location (&explicit_loc);
3762 explicit_loc.function_name = ASTRDUP (func_name);
3763 b->location = new_explicit_location (&explicit_loc);
3764 b->enable_state = bp_disabled;
3765 }
3766 }
3767
3768 /* Does B have a location spec? */
3769
3770 static int
3771 breakpoint_event_location_empty_p (const struct breakpoint *b)
3772 {
3773 return b->location != NULL && event_location_empty_p (b->location);
3774 }
3775
3776 void
3777 update_breakpoints_after_exec (void)
3778 {
3779 struct breakpoint *b, *b_tmp;
3780 struct bp_location *bploc, **bplocp_tmp;
3781
3782 /* We're about to delete breakpoints from GDB's lists. If the
3783 INSERTED flag is true, GDB will try to lift the breakpoints by
3784 writing the breakpoints' "shadow contents" back into memory. The
3785 "shadow contents" are NOT valid after an exec, so GDB should not
3786 do that. Instead, the target is responsible from marking
3787 breakpoints out as soon as it detects an exec. We don't do that
3788 here instead, because there may be other attempts to delete
3789 breakpoints after detecting an exec and before reaching here. */
3790 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
3791 if (bploc->pspace == current_program_space)
3792 gdb_assert (!bploc->inserted);
3793
3794 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3795 {
3796 if (b->pspace != current_program_space)
3797 continue;
3798
3799 /* Solib breakpoints must be explicitly reset after an exec(). */
3800 if (b->type == bp_shlib_event)
3801 {
3802 delete_breakpoint (b);
3803 continue;
3804 }
3805
3806 /* JIT breakpoints must be explicitly reset after an exec(). */
3807 if (b->type == bp_jit_event)
3808 {
3809 delete_breakpoint (b);
3810 continue;
3811 }
3812
3813 /* Thread event breakpoints must be set anew after an exec(),
3814 as must overlay event and longjmp master breakpoints. */
3815 if (b->type == bp_thread_event || b->type == bp_overlay_event
3816 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3817 || b->type == bp_exception_master)
3818 {
3819 delete_breakpoint (b);
3820 continue;
3821 }
3822
3823 /* Step-resume breakpoints are meaningless after an exec(). */
3824 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3825 {
3826 delete_breakpoint (b);
3827 continue;
3828 }
3829
3830 /* Just like single-step breakpoints. */
3831 if (b->type == bp_single_step)
3832 {
3833 delete_breakpoint (b);
3834 continue;
3835 }
3836
3837 /* Longjmp and longjmp-resume breakpoints are also meaningless
3838 after an exec. */
3839 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3840 || b->type == bp_longjmp_call_dummy
3841 || b->type == bp_exception || b->type == bp_exception_resume)
3842 {
3843 delete_breakpoint (b);
3844 continue;
3845 }
3846
3847 if (b->type == bp_catchpoint)
3848 {
3849 /* For now, none of the bp_catchpoint breakpoints need to
3850 do anything at this point. In the future, if some of
3851 the catchpoints need to something, we will need to add
3852 a new method, and call this method from here. */
3853 continue;
3854 }
3855
3856 /* bp_finish is a special case. The only way we ought to be able
3857 to see one of these when an exec() has happened, is if the user
3858 caught a vfork, and then said "finish". Ordinarily a finish just
3859 carries them to the call-site of the current callee, by setting
3860 a temporary bp there and resuming. But in this case, the finish
3861 will carry them entirely through the vfork & exec.
3862
3863 We don't want to allow a bp_finish to remain inserted now. But
3864 we can't safely delete it, 'cause finish_command has a handle to
3865 the bp on a bpstat, and will later want to delete it. There's a
3866 chance (and I've seen it happen) that if we delete the bp_finish
3867 here, that its storage will get reused by the time finish_command
3868 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3869 We really must allow finish_command to delete a bp_finish.
3870
3871 In the absence of a general solution for the "how do we know
3872 it's safe to delete something others may have handles to?"
3873 problem, what we'll do here is just uninsert the bp_finish, and
3874 let finish_command delete it.
3875
3876 (We know the bp_finish is "doomed" in the sense that it's
3877 momentary, and will be deleted as soon as finish_command sees
3878 the inferior stopped. So it doesn't matter that the bp's
3879 address is probably bogus in the new a.out, unlike e.g., the
3880 solib breakpoints.) */
3881
3882 if (b->type == bp_finish)
3883 {
3884 continue;
3885 }
3886
3887 /* Without a symbolic address, we have little hope of the
3888 pre-exec() address meaning the same thing in the post-exec()
3889 a.out. */
3890 if (breakpoint_event_location_empty_p (b))
3891 {
3892 delete_breakpoint (b);
3893 continue;
3894 }
3895 }
3896 }
3897
3898 int
3899 detach_breakpoints (ptid_t ptid)
3900 {
3901 struct bp_location *bl, **blp_tmp;
3902 int val = 0;
3903 struct cleanup *old_chain = save_inferior_ptid ();
3904 struct inferior *inf = current_inferior ();
3905
3906 if (ptid_get_pid (ptid) == ptid_get_pid (inferior_ptid))
3907 error (_("Cannot detach breakpoints of inferior_ptid"));
3908
3909 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
3910 inferior_ptid = ptid;
3911 ALL_BP_LOCATIONS (bl, blp_tmp)
3912 {
3913 if (bl->pspace != inf->pspace)
3914 continue;
3915
3916 /* This function must physically remove breakpoints locations
3917 from the specified ptid, without modifying the breakpoint
3918 package's state. Locations of type bp_loc_other are only
3919 maintained at GDB side. So, there is no need to remove
3920 these bp_loc_other locations. Moreover, removing these
3921 would modify the breakpoint package's state. */
3922 if (bl->loc_type == bp_loc_other)
3923 continue;
3924
3925 if (bl->inserted)
3926 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
3927 }
3928
3929 do_cleanups (old_chain);
3930 return val;
3931 }
3932
3933 /* Remove the breakpoint location BL from the current address space.
3934 Note that this is used to detach breakpoints from a child fork.
3935 When we get here, the child isn't in the inferior list, and neither
3936 do we have objects to represent its address space --- we should
3937 *not* look at bl->pspace->aspace here. */
3938
3939 static int
3940 remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
3941 {
3942 int val;
3943
3944 /* BL is never in moribund_locations by our callers. */
3945 gdb_assert (bl->owner != NULL);
3946
3947 /* The type of none suggests that owner is actually deleted.
3948 This should not ever happen. */
3949 gdb_assert (bl->owner->type != bp_none);
3950
3951 if (bl->loc_type == bp_loc_software_breakpoint
3952 || bl->loc_type == bp_loc_hardware_breakpoint)
3953 {
3954 /* "Normal" instruction breakpoint: either the standard
3955 trap-instruction bp (bp_breakpoint), or a
3956 bp_hardware_breakpoint. */
3957
3958 /* First check to see if we have to handle an overlay. */
3959 if (overlay_debugging == ovly_off
3960 || bl->section == NULL
3961 || !(section_is_overlay (bl->section)))
3962 {
3963 /* No overlay handling: just remove the breakpoint. */
3964
3965 /* If we're trying to uninsert a memory breakpoint that we
3966 know is set in a dynamic object that is marked
3967 shlib_disabled, then either the dynamic object was
3968 removed with "remove-symbol-file" or with
3969 "nosharedlibrary". In the former case, we don't know
3970 whether another dynamic object might have loaded over the
3971 breakpoint's address -- the user might well let us know
3972 about it next with add-symbol-file (the whole point of
3973 add-symbol-file is letting the user manually maintain a
3974 list of dynamically loaded objects). If we have the
3975 breakpoint's shadow memory, that is, this is a software
3976 breakpoint managed by GDB, check whether the breakpoint
3977 is still inserted in memory, to avoid overwriting wrong
3978 code with stale saved shadow contents. Note that HW
3979 breakpoints don't have shadow memory, as they're
3980 implemented using a mechanism that is not dependent on
3981 being able to modify the target's memory, and as such
3982 they should always be removed. */
3983 if (bl->shlib_disabled
3984 && bl->target_info.shadow_len != 0
3985 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3986 val = 0;
3987 else
3988 val = bl->owner->ops->remove_location (bl, reason);
3989 }
3990 else
3991 {
3992 /* This breakpoint is in an overlay section.
3993 Did we set a breakpoint at the LMA? */
3994 if (!overlay_events_enabled)
3995 {
3996 /* Yes -- overlay event support is not active, so we
3997 should have set a breakpoint at the LMA. Remove it.
3998 */
3999 /* Ignore any failures: if the LMA is in ROM, we will
4000 have already warned when we failed to insert it. */
4001 if (bl->loc_type == bp_loc_hardware_breakpoint)
4002 target_remove_hw_breakpoint (bl->gdbarch,
4003 &bl->overlay_target_info);
4004 else
4005 target_remove_breakpoint (bl->gdbarch,
4006 &bl->overlay_target_info,
4007 reason);
4008 }
4009 /* Did we set a breakpoint at the VMA?
4010 If so, we will have marked the breakpoint 'inserted'. */
4011 if (bl->inserted)
4012 {
4013 /* Yes -- remove it. Previously we did not bother to
4014 remove the breakpoint if the section had been
4015 unmapped, but let's not rely on that being safe. We
4016 don't know what the overlay manager might do. */
4017
4018 /* However, we should remove *software* breakpoints only
4019 if the section is still mapped, or else we overwrite
4020 wrong code with the saved shadow contents. */
4021 if (bl->loc_type == bp_loc_hardware_breakpoint
4022 || section_is_mapped (bl->section))
4023 val = bl->owner->ops->remove_location (bl, reason);
4024 else
4025 val = 0;
4026 }
4027 else
4028 {
4029 /* No -- not inserted, so no need to remove. No error. */
4030 val = 0;
4031 }
4032 }
4033
4034 /* In some cases, we might not be able to remove a breakpoint in
4035 a shared library that has already been removed, but we have
4036 not yet processed the shlib unload event. Similarly for an
4037 unloaded add-symbol-file object - the user might not yet have
4038 had the chance to remove-symbol-file it. shlib_disabled will
4039 be set if the library/object has already been removed, but
4040 the breakpoint hasn't been uninserted yet, e.g., after
4041 "nosharedlibrary" or "remove-symbol-file" with breakpoints
4042 always-inserted mode. */
4043 if (val
4044 && (bl->loc_type == bp_loc_software_breakpoint
4045 && (bl->shlib_disabled
4046 || solib_name_from_address (bl->pspace, bl->address)
4047 || shared_objfile_contains_address_p (bl->pspace,
4048 bl->address))))
4049 val = 0;
4050
4051 if (val)
4052 return val;
4053 bl->inserted = (reason == DETACH_BREAKPOINT);
4054 }
4055 else if (bl->loc_type == bp_loc_hardware_watchpoint)
4056 {
4057 gdb_assert (bl->owner->ops != NULL
4058 && bl->owner->ops->remove_location != NULL);
4059
4060 bl->inserted = (reason == DETACH_BREAKPOINT);
4061 bl->owner->ops->remove_location (bl, reason);
4062
4063 /* Failure to remove any of the hardware watchpoints comes here. */
4064 if (reason == REMOVE_BREAKPOINT && bl->inserted)
4065 warning (_("Could not remove hardware watchpoint %d."),
4066 bl->owner->number);
4067 }
4068 else if (bl->owner->type == bp_catchpoint
4069 && breakpoint_enabled (bl->owner)
4070 && !bl->duplicate)
4071 {
4072 gdb_assert (bl->owner->ops != NULL
4073 && bl->owner->ops->remove_location != NULL);
4074
4075 val = bl->owner->ops->remove_location (bl, reason);
4076 if (val)
4077 return val;
4078
4079 bl->inserted = (reason == DETACH_BREAKPOINT);
4080 }
4081
4082 return 0;
4083 }
4084
4085 static int
4086 remove_breakpoint (struct bp_location *bl)
4087 {
4088 int ret;
4089 struct cleanup *old_chain;
4090
4091 /* BL is never in moribund_locations by our callers. */
4092 gdb_assert (bl->owner != NULL);
4093
4094 /* The type of none suggests that owner is actually deleted.
4095 This should not ever happen. */
4096 gdb_assert (bl->owner->type != bp_none);
4097
4098 old_chain = save_current_space_and_thread ();
4099
4100 switch_to_program_space_and_thread (bl->pspace);
4101
4102 ret = remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
4103
4104 do_cleanups (old_chain);
4105 return ret;
4106 }
4107
4108 /* Clear the "inserted" flag in all breakpoints. */
4109
4110 void
4111 mark_breakpoints_out (void)
4112 {
4113 struct bp_location *bl, **blp_tmp;
4114
4115 ALL_BP_LOCATIONS (bl, blp_tmp)
4116 if (bl->pspace == current_program_space)
4117 bl->inserted = 0;
4118 }
4119
4120 /* Clear the "inserted" flag in all breakpoints and delete any
4121 breakpoints which should go away between runs of the program.
4122
4123 Plus other such housekeeping that has to be done for breakpoints
4124 between runs.
4125
4126 Note: this function gets called at the end of a run (by
4127 generic_mourn_inferior) and when a run begins (by
4128 init_wait_for_inferior). */
4129
4130
4131
4132 void
4133 breakpoint_init_inferior (enum inf_context context)
4134 {
4135 struct breakpoint *b, *b_tmp;
4136 struct bp_location *bl;
4137 int ix;
4138 struct program_space *pspace = current_program_space;
4139
4140 /* If breakpoint locations are shared across processes, then there's
4141 nothing to do. */
4142 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
4143 return;
4144
4145 mark_breakpoints_out ();
4146
4147 ALL_BREAKPOINTS_SAFE (b, b_tmp)
4148 {
4149 if (b->loc && b->loc->pspace != pspace)
4150 continue;
4151
4152 switch (b->type)
4153 {
4154 case bp_call_dummy:
4155 case bp_longjmp_call_dummy:
4156
4157 /* If the call dummy breakpoint is at the entry point it will
4158 cause problems when the inferior is rerun, so we better get
4159 rid of it. */
4160
4161 case bp_watchpoint_scope:
4162
4163 /* Also get rid of scope breakpoints. */
4164
4165 case bp_shlib_event:
4166
4167 /* Also remove solib event breakpoints. Their addresses may
4168 have changed since the last time we ran the program.
4169 Actually we may now be debugging against different target;
4170 and so the solib backend that installed this breakpoint may
4171 not be used in by the target. E.g.,
4172
4173 (gdb) file prog-linux
4174 (gdb) run # native linux target
4175 ...
4176 (gdb) kill
4177 (gdb) file prog-win.exe
4178 (gdb) tar rem :9999 # remote Windows gdbserver.
4179 */
4180
4181 case bp_step_resume:
4182
4183 /* Also remove step-resume breakpoints. */
4184
4185 case bp_single_step:
4186
4187 /* Also remove single-step breakpoints. */
4188
4189 delete_breakpoint (b);
4190 break;
4191
4192 case bp_watchpoint:
4193 case bp_hardware_watchpoint:
4194 case bp_read_watchpoint:
4195 case bp_access_watchpoint:
4196 {
4197 struct watchpoint *w = (struct watchpoint *) b;
4198
4199 /* Likewise for watchpoints on local expressions. */
4200 if (w->exp_valid_block != NULL)
4201 delete_breakpoint (b);
4202 else
4203 {
4204 /* Get rid of existing locations, which are no longer
4205 valid. New ones will be created in
4206 update_watchpoint, when the inferior is restarted.
4207 The next update_global_location_list call will
4208 garbage collect them. */
4209 b->loc = NULL;
4210
4211 if (context == inf_starting)
4212 {
4213 /* Reset val field to force reread of starting value in
4214 insert_breakpoints. */
4215 if (w->val)
4216 value_free (w->val);
4217 w->val = NULL;
4218 w->val_valid = 0;
4219 }
4220 }
4221 }
4222 break;
4223 default:
4224 break;
4225 }
4226 }
4227
4228 /* Get rid of the moribund locations. */
4229 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
4230 decref_bp_location (&bl);
4231 VEC_free (bp_location_p, moribund_locations);
4232 }
4233
4234 /* These functions concern about actual breakpoints inserted in the
4235 target --- to e.g. check if we need to do decr_pc adjustment or if
4236 we need to hop over the bkpt --- so we check for address space
4237 match, not program space. */
4238
4239 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4240 exists at PC. It returns ordinary_breakpoint_here if it's an
4241 ordinary breakpoint, or permanent_breakpoint_here if it's a
4242 permanent breakpoint.
4243 - When continuing from a location with an ordinary breakpoint, we
4244 actually single step once before calling insert_breakpoints.
4245 - When continuing from a location with a permanent breakpoint, we
4246 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4247 the target, to advance the PC past the breakpoint. */
4248
4249 enum breakpoint_here
4250 breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
4251 {
4252 struct bp_location *bl, **blp_tmp;
4253 int any_breakpoint_here = 0;
4254
4255 ALL_BP_LOCATIONS (bl, blp_tmp)
4256 {
4257 if (bl->loc_type != bp_loc_software_breakpoint
4258 && bl->loc_type != bp_loc_hardware_breakpoint)
4259 continue;
4260
4261 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
4262 if ((breakpoint_enabled (bl->owner)
4263 || bl->permanent)
4264 && breakpoint_location_address_match (bl, aspace, pc))
4265 {
4266 if (overlay_debugging
4267 && section_is_overlay (bl->section)
4268 && !section_is_mapped (bl->section))
4269 continue; /* unmapped overlay -- can't be a match */
4270 else if (bl->permanent)
4271 return permanent_breakpoint_here;
4272 else
4273 any_breakpoint_here = 1;
4274 }
4275 }
4276
4277 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
4278 }
4279
4280 /* See breakpoint.h. */
4281
4282 int
4283 breakpoint_in_range_p (struct address_space *aspace,
4284 CORE_ADDR addr, ULONGEST len)
4285 {
4286 struct bp_location *bl, **blp_tmp;
4287
4288 ALL_BP_LOCATIONS (bl, blp_tmp)
4289 {
4290 if (bl->loc_type != bp_loc_software_breakpoint
4291 && bl->loc_type != bp_loc_hardware_breakpoint)
4292 continue;
4293
4294 if ((breakpoint_enabled (bl->owner)
4295 || bl->permanent)
4296 && breakpoint_location_address_range_overlap (bl, aspace,
4297 addr, len))
4298 {
4299 if (overlay_debugging
4300 && section_is_overlay (bl->section)
4301 && !section_is_mapped (bl->section))
4302 {
4303 /* Unmapped overlay -- can't be a match. */
4304 continue;
4305 }
4306
4307 return 1;
4308 }
4309 }
4310
4311 return 0;
4312 }
4313
4314 /* Return true if there's a moribund breakpoint at PC. */
4315
4316 int
4317 moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
4318 {
4319 struct bp_location *loc;
4320 int ix;
4321
4322 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
4323 if (breakpoint_location_address_match (loc, aspace, pc))
4324 return 1;
4325
4326 return 0;
4327 }
4328
4329 /* Returns non-zero iff BL is inserted at PC, in address space
4330 ASPACE. */
4331
4332 static int
4333 bp_location_inserted_here_p (struct bp_location *bl,
4334 struct address_space *aspace, CORE_ADDR pc)
4335 {
4336 if (bl->inserted
4337 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4338 aspace, pc))
4339 {
4340 if (overlay_debugging
4341 && section_is_overlay (bl->section)
4342 && !section_is_mapped (bl->section))
4343 return 0; /* unmapped overlay -- can't be a match */
4344 else
4345 return 1;
4346 }
4347 return 0;
4348 }
4349
4350 /* Returns non-zero iff there's a breakpoint inserted at PC. */
4351
4352 int
4353 breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
4354 {
4355 struct bp_location **blp, **blp_tmp = NULL;
4356
4357 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4358 {
4359 struct bp_location *bl = *blp;
4360
4361 if (bl->loc_type != bp_loc_software_breakpoint
4362 && bl->loc_type != bp_loc_hardware_breakpoint)
4363 continue;
4364
4365 if (bp_location_inserted_here_p (bl, aspace, pc))
4366 return 1;
4367 }
4368 return 0;
4369 }
4370
4371 /* This function returns non-zero iff there is a software breakpoint
4372 inserted at PC. */
4373
4374 int
4375 software_breakpoint_inserted_here_p (struct address_space *aspace,
4376 CORE_ADDR pc)
4377 {
4378 struct bp_location **blp, **blp_tmp = NULL;
4379
4380 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4381 {
4382 struct bp_location *bl = *blp;
4383
4384 if (bl->loc_type != bp_loc_software_breakpoint)
4385 continue;
4386
4387 if (bp_location_inserted_here_p (bl, aspace, pc))
4388 return 1;
4389 }
4390
4391 return 0;
4392 }
4393
4394 /* See breakpoint.h. */
4395
4396 int
4397 hardware_breakpoint_inserted_here_p (struct address_space *aspace,
4398 CORE_ADDR pc)
4399 {
4400 struct bp_location **blp, **blp_tmp = NULL;
4401
4402 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4403 {
4404 struct bp_location *bl = *blp;
4405
4406 if (bl->loc_type != bp_loc_hardware_breakpoint)
4407 continue;
4408
4409 if (bp_location_inserted_here_p (bl, aspace, pc))
4410 return 1;
4411 }
4412
4413 return 0;
4414 }
4415
4416 int
4417 hardware_watchpoint_inserted_in_range (struct address_space *aspace,
4418 CORE_ADDR addr, ULONGEST len)
4419 {
4420 struct breakpoint *bpt;
4421
4422 ALL_BREAKPOINTS (bpt)
4423 {
4424 struct bp_location *loc;
4425
4426 if (bpt->type != bp_hardware_watchpoint
4427 && bpt->type != bp_access_watchpoint)
4428 continue;
4429
4430 if (!breakpoint_enabled (bpt))
4431 continue;
4432
4433 for (loc = bpt->loc; loc; loc = loc->next)
4434 if (loc->pspace->aspace == aspace && loc->inserted)
4435 {
4436 CORE_ADDR l, h;
4437
4438 /* Check for intersection. */
4439 l = std::max<CORE_ADDR> (loc->address, addr);
4440 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
4441 if (l < h)
4442 return 1;
4443 }
4444 }
4445 return 0;
4446 }
4447 \f
4448
4449 /* bpstat stuff. External routines' interfaces are documented
4450 in breakpoint.h. */
4451
4452 int
4453 is_catchpoint (struct breakpoint *ep)
4454 {
4455 return (ep->type == bp_catchpoint);
4456 }
4457
4458 /* Frees any storage that is part of a bpstat. Does not walk the
4459 'next' chain. */
4460
4461 static void
4462 bpstat_free (bpstat bs)
4463 {
4464 if (bs->old_val != NULL)
4465 value_free (bs->old_val);
4466 decref_counted_command_line (&bs->commands);
4467 decref_bp_location (&bs->bp_location_at);
4468 xfree (bs);
4469 }
4470
4471 /* Clear a bpstat so that it says we are not at any breakpoint.
4472 Also free any storage that is part of a bpstat. */
4473
4474 void
4475 bpstat_clear (bpstat *bsp)
4476 {
4477 bpstat p;
4478 bpstat q;
4479
4480 if (bsp == 0)
4481 return;
4482 p = *bsp;
4483 while (p != NULL)
4484 {
4485 q = p->next;
4486 bpstat_free (p);
4487 p = q;
4488 }
4489 *bsp = NULL;
4490 }
4491
4492 /* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4493 is part of the bpstat is copied as well. */
4494
4495 bpstat
4496 bpstat_copy (bpstat bs)
4497 {
4498 bpstat p = NULL;
4499 bpstat tmp;
4500 bpstat retval = NULL;
4501
4502 if (bs == NULL)
4503 return bs;
4504
4505 for (; bs != NULL; bs = bs->next)
4506 {
4507 tmp = (bpstat) xmalloc (sizeof (*tmp));
4508 memcpy (tmp, bs, sizeof (*tmp));
4509 incref_counted_command_line (tmp->commands);
4510 incref_bp_location (tmp->bp_location_at);
4511 if (bs->old_val != NULL)
4512 {
4513 tmp->old_val = value_copy (bs->old_val);
4514 release_value (tmp->old_val);
4515 }
4516
4517 if (p == NULL)
4518 /* This is the first thing in the chain. */
4519 retval = tmp;
4520 else
4521 p->next = tmp;
4522 p = tmp;
4523 }
4524 p->next = NULL;
4525 return retval;
4526 }
4527
4528 /* Find the bpstat associated with this breakpoint. */
4529
4530 bpstat
4531 bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
4532 {
4533 if (bsp == NULL)
4534 return NULL;
4535
4536 for (; bsp != NULL; bsp = bsp->next)
4537 {
4538 if (bsp->breakpoint_at == breakpoint)
4539 return bsp;
4540 }
4541 return NULL;
4542 }
4543
4544 /* See breakpoint.h. */
4545
4546 int
4547 bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
4548 {
4549 for (; bsp != NULL; bsp = bsp->next)
4550 {
4551 if (bsp->breakpoint_at == NULL)
4552 {
4553 /* A moribund location can never explain a signal other than
4554 GDB_SIGNAL_TRAP. */
4555 if (sig == GDB_SIGNAL_TRAP)
4556 return 1;
4557 }
4558 else
4559 {
4560 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4561 sig))
4562 return 1;
4563 }
4564 }
4565
4566 return 0;
4567 }
4568
4569 /* Put in *NUM the breakpoint number of the first breakpoint we are
4570 stopped at. *BSP upon return is a bpstat which points to the
4571 remaining breakpoints stopped at (but which is not guaranteed to be
4572 good for anything but further calls to bpstat_num).
4573
4574 Return 0 if passed a bpstat which does not indicate any breakpoints.
4575 Return -1 if stopped at a breakpoint that has been deleted since
4576 we set it.
4577 Return 1 otherwise. */
4578
4579 int
4580 bpstat_num (bpstat *bsp, int *num)
4581 {
4582 struct breakpoint *b;
4583
4584 if ((*bsp) == NULL)
4585 return 0; /* No more breakpoint values */
4586
4587 /* We assume we'll never have several bpstats that correspond to a
4588 single breakpoint -- otherwise, this function might return the
4589 same number more than once and this will look ugly. */
4590 b = (*bsp)->breakpoint_at;
4591 *bsp = (*bsp)->next;
4592 if (b == NULL)
4593 return -1; /* breakpoint that's been deleted since */
4594
4595 *num = b->number; /* We have its number */
4596 return 1;
4597 }
4598
4599 /* See breakpoint.h. */
4600
4601 void
4602 bpstat_clear_actions (void)
4603 {
4604 struct thread_info *tp;
4605 bpstat bs;
4606
4607 if (ptid_equal (inferior_ptid, null_ptid))
4608 return;
4609
4610 tp = find_thread_ptid (inferior_ptid);
4611 if (tp == NULL)
4612 return;
4613
4614 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
4615 {
4616 decref_counted_command_line (&bs->commands);
4617
4618 if (bs->old_val != NULL)
4619 {
4620 value_free (bs->old_val);
4621 bs->old_val = NULL;
4622 }
4623 }
4624 }
4625
4626 /* Called when a command is about to proceed the inferior. */
4627
4628 static void
4629 breakpoint_about_to_proceed (void)
4630 {
4631 if (!ptid_equal (inferior_ptid, null_ptid))
4632 {
4633 struct thread_info *tp = inferior_thread ();
4634
4635 /* Allow inferior function calls in breakpoint commands to not
4636 interrupt the command list. When the call finishes
4637 successfully, the inferior will be standing at the same
4638 breakpoint as if nothing happened. */
4639 if (tp->control.in_infcall)
4640 return;
4641 }
4642
4643 breakpoint_proceeded = 1;
4644 }
4645
4646 /* Stub for cleaning up our state if we error-out of a breakpoint
4647 command. */
4648 static void
4649 cleanup_executing_breakpoints (void *ignore)
4650 {
4651 executing_breakpoint_commands = 0;
4652 }
4653
4654 /* Return non-zero iff CMD as the first line of a command sequence is `silent'
4655 or its equivalent. */
4656
4657 static int
4658 command_line_is_silent (struct command_line *cmd)
4659 {
4660 return cmd && (strcmp ("silent", cmd->line) == 0);
4661 }
4662
4663 /* Execute all the commands associated with all the breakpoints at
4664 this location. Any of these commands could cause the process to
4665 proceed beyond this point, etc. We look out for such changes by
4666 checking the global "breakpoint_proceeded" after each command.
4667
4668 Returns true if a breakpoint command resumed the inferior. In that
4669 case, it is the caller's responsibility to recall it again with the
4670 bpstat of the current thread. */
4671
4672 static int
4673 bpstat_do_actions_1 (bpstat *bsp)
4674 {
4675 bpstat bs;
4676 struct cleanup *old_chain;
4677 int again = 0;
4678
4679 /* Avoid endless recursion if a `source' command is contained
4680 in bs->commands. */
4681 if (executing_breakpoint_commands)
4682 return 0;
4683
4684 executing_breakpoint_commands = 1;
4685 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
4686
4687 prevent_dont_repeat ();
4688
4689 /* This pointer will iterate over the list of bpstat's. */
4690 bs = *bsp;
4691
4692 breakpoint_proceeded = 0;
4693 for (; bs != NULL; bs = bs->next)
4694 {
4695 struct counted_command_line *ccmd;
4696 struct command_line *cmd;
4697 struct cleanup *this_cmd_tree_chain;
4698
4699 /* Take ownership of the BSP's command tree, if it has one.
4700
4701 The command tree could legitimately contain commands like
4702 'step' and 'next', which call clear_proceed_status, which
4703 frees stop_bpstat's command tree. To make sure this doesn't
4704 free the tree we're executing out from under us, we need to
4705 take ownership of the tree ourselves. Since a given bpstat's
4706 commands are only executed once, we don't need to copy it; we
4707 can clear the pointer in the bpstat, and make sure we free
4708 the tree when we're done. */
4709 ccmd = bs->commands;
4710 bs->commands = NULL;
4711 this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
4712 cmd = ccmd ? ccmd->commands : NULL;
4713 if (command_line_is_silent (cmd))
4714 {
4715 /* The action has been already done by bpstat_stop_status. */
4716 cmd = cmd->next;
4717 }
4718
4719 while (cmd != NULL)
4720 {
4721 execute_control_command (cmd);
4722
4723 if (breakpoint_proceeded)
4724 break;
4725 else
4726 cmd = cmd->next;
4727 }
4728
4729 /* We can free this command tree now. */
4730 do_cleanups (this_cmd_tree_chain);
4731
4732 if (breakpoint_proceeded)
4733 {
4734 if (current_ui->async)
4735 /* If we are in async mode, then the target might be still
4736 running, not stopped at any breakpoint, so nothing for
4737 us to do here -- just return to the event loop. */
4738 ;
4739 else
4740 /* In sync mode, when execute_control_command returns
4741 we're already standing on the next breakpoint.
4742 Breakpoint commands for that stop were not run, since
4743 execute_command does not run breakpoint commands --
4744 only command_line_handler does, but that one is not
4745 involved in execution of breakpoint commands. So, we
4746 can now execute breakpoint commands. It should be
4747 noted that making execute_command do bpstat actions is
4748 not an option -- in this case we'll have recursive
4749 invocation of bpstat for each breakpoint with a
4750 command, and can easily blow up GDB stack. Instead, we
4751 return true, which will trigger the caller to recall us
4752 with the new stop_bpstat. */
4753 again = 1;
4754 break;
4755 }
4756 }
4757 do_cleanups (old_chain);
4758 return again;
4759 }
4760
4761 void
4762 bpstat_do_actions (void)
4763 {
4764 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4765
4766 /* Do any commands attached to breakpoint we are stopped at. */
4767 while (!ptid_equal (inferior_ptid, null_ptid)
4768 && target_has_execution
4769 && !is_exited (inferior_ptid)
4770 && !is_executing (inferior_ptid))
4771 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4772 and only return when it is stopped at the next breakpoint, we
4773 keep doing breakpoint actions until it returns false to
4774 indicate the inferior was not resumed. */
4775 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
4776 break;
4777
4778 discard_cleanups (cleanup_if_error);
4779 }
4780
4781 /* Print out the (old or new) value associated with a watchpoint. */
4782
4783 static void
4784 watchpoint_value_print (struct value *val, struct ui_file *stream)
4785 {
4786 if (val == NULL)
4787 fprintf_unfiltered (stream, _("<unreadable>"));
4788 else
4789 {
4790 struct value_print_options opts;
4791 get_user_print_options (&opts);
4792 value_print (val, stream, &opts);
4793 }
4794 }
4795
4796 /* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4797 debugging multiple threads. */
4798
4799 void
4800 maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4801 {
4802 if (ui_out_is_mi_like_p (uiout))
4803 return;
4804
4805 ui_out_text (uiout, "\n");
4806
4807 if (show_thread_that_caused_stop ())
4808 {
4809 const char *name;
4810 struct thread_info *thr = inferior_thread ();
4811
4812 ui_out_text (uiout, "Thread ");
4813 ui_out_field_fmt (uiout, "thread-id", "%s", print_thread_id (thr));
4814
4815 name = thr->name != NULL ? thr->name : target_thread_name (thr);
4816 if (name != NULL)
4817 {
4818 ui_out_text (uiout, " \"");
4819 ui_out_field_fmt (uiout, "name", "%s", name);
4820 ui_out_text (uiout, "\"");
4821 }
4822
4823 ui_out_text (uiout, " hit ");
4824 }
4825 }
4826
4827 /* Generic routine for printing messages indicating why we
4828 stopped. The behavior of this function depends on the value
4829 'print_it' in the bpstat structure. Under some circumstances we
4830 may decide not to print anything here and delegate the task to
4831 normal_stop(). */
4832
4833 static enum print_stop_action
4834 print_bp_stop_message (bpstat bs)
4835 {
4836 switch (bs->print_it)
4837 {
4838 case print_it_noop:
4839 /* Nothing should be printed for this bpstat entry. */
4840 return PRINT_UNKNOWN;
4841 break;
4842
4843 case print_it_done:
4844 /* We still want to print the frame, but we already printed the
4845 relevant messages. */
4846 return PRINT_SRC_AND_LOC;
4847 break;
4848
4849 case print_it_normal:
4850 {
4851 struct breakpoint *b = bs->breakpoint_at;
4852
4853 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4854 which has since been deleted. */
4855 if (b == NULL)
4856 return PRINT_UNKNOWN;
4857
4858 /* Normal case. Call the breakpoint's print_it method. */
4859 return b->ops->print_it (bs);
4860 }
4861 break;
4862
4863 default:
4864 internal_error (__FILE__, __LINE__,
4865 _("print_bp_stop_message: unrecognized enum value"));
4866 break;
4867 }
4868 }
4869
4870 /* A helper function that prints a shared library stopped event. */
4871
4872 static void
4873 print_solib_event (int is_catchpoint)
4874 {
4875 int any_deleted
4876 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4877 int any_added
4878 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4879
4880 if (!is_catchpoint)
4881 {
4882 if (any_added || any_deleted)
4883 ui_out_text (current_uiout,
4884 _("Stopped due to shared library event:\n"));
4885 else
4886 ui_out_text (current_uiout,
4887 _("Stopped due to shared library event (no "
4888 "libraries added or removed)\n"));
4889 }
4890
4891 if (ui_out_is_mi_like_p (current_uiout))
4892 ui_out_field_string (current_uiout, "reason",
4893 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4894
4895 if (any_deleted)
4896 {
4897 struct cleanup *cleanup;
4898 char *name;
4899 int ix;
4900
4901 ui_out_text (current_uiout, _(" Inferior unloaded "));
4902 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4903 "removed");
4904 for (ix = 0;
4905 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4906 ix, name);
4907 ++ix)
4908 {
4909 if (ix > 0)
4910 ui_out_text (current_uiout, " ");
4911 ui_out_field_string (current_uiout, "library", name);
4912 ui_out_text (current_uiout, "\n");
4913 }
4914
4915 do_cleanups (cleanup);
4916 }
4917
4918 if (any_added)
4919 {
4920 struct so_list *iter;
4921 int ix;
4922 struct cleanup *cleanup;
4923
4924 ui_out_text (current_uiout, _(" Inferior loaded "));
4925 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4926 "added");
4927 for (ix = 0;
4928 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4929 ix, iter);
4930 ++ix)
4931 {
4932 if (ix > 0)
4933 ui_out_text (current_uiout, " ");
4934 ui_out_field_string (current_uiout, "library", iter->so_name);
4935 ui_out_text (current_uiout, "\n");
4936 }
4937
4938 do_cleanups (cleanup);
4939 }
4940 }
4941
4942 /* Print a message indicating what happened. This is called from
4943 normal_stop(). The input to this routine is the head of the bpstat
4944 list - a list of the eventpoints that caused this stop. KIND is
4945 the target_waitkind for the stopping event. This
4946 routine calls the generic print routine for printing a message
4947 about reasons for stopping. This will print (for example) the
4948 "Breakpoint n," part of the output. The return value of this
4949 routine is one of:
4950
4951 PRINT_UNKNOWN: Means we printed nothing.
4952 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4953 code to print the location. An example is
4954 "Breakpoint 1, " which should be followed by
4955 the location.
4956 PRINT_SRC_ONLY: Means we printed something, but there is no need
4957 to also print the location part of the message.
4958 An example is the catch/throw messages, which
4959 don't require a location appended to the end.
4960 PRINT_NOTHING: We have done some printing and we don't need any
4961 further info to be printed. */
4962
4963 enum print_stop_action
4964 bpstat_print (bpstat bs, int kind)
4965 {
4966 enum print_stop_action val;
4967
4968 /* Maybe another breakpoint in the chain caused us to stop.
4969 (Currently all watchpoints go on the bpstat whether hit or not.
4970 That probably could (should) be changed, provided care is taken
4971 with respect to bpstat_explains_signal). */
4972 for (; bs; bs = bs->next)
4973 {
4974 val = print_bp_stop_message (bs);
4975 if (val == PRINT_SRC_ONLY
4976 || val == PRINT_SRC_AND_LOC
4977 || val == PRINT_NOTHING)
4978 return val;
4979 }
4980
4981 /* If we had hit a shared library event breakpoint,
4982 print_bp_stop_message would print out this message. If we hit an
4983 OS-level shared library event, do the same thing. */
4984 if (kind == TARGET_WAITKIND_LOADED)
4985 {
4986 print_solib_event (0);
4987 return PRINT_NOTHING;
4988 }
4989
4990 /* We reached the end of the chain, or we got a null BS to start
4991 with and nothing was printed. */
4992 return PRINT_UNKNOWN;
4993 }
4994
4995 /* Evaluate the expression EXP and return 1 if value is zero.
4996 This returns the inverse of the condition because it is called
4997 from catch_errors which returns 0 if an exception happened, and if an
4998 exception happens we want execution to stop.
4999 The argument is a "struct expression *" that has been cast to a
5000 "void *" to make it pass through catch_errors. */
5001
5002 static int
5003 breakpoint_cond_eval (void *exp)
5004 {
5005 struct value *mark = value_mark ();
5006 int i = !value_true (evaluate_expression ((struct expression *) exp));
5007
5008 value_free_to_mark (mark);
5009 return i;
5010 }
5011
5012 /* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
5013
5014 static bpstat
5015 bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
5016 {
5017 bpstat bs;
5018
5019 bs = (bpstat) xmalloc (sizeof (*bs));
5020 bs->next = NULL;
5021 **bs_link_pointer = bs;
5022 *bs_link_pointer = &bs->next;
5023 bs->breakpoint_at = bl->owner;
5024 bs->bp_location_at = bl;
5025 incref_bp_location (bl);
5026 /* If the condition is false, etc., don't do the commands. */
5027 bs->commands = NULL;
5028 bs->old_val = NULL;
5029 bs->print_it = print_it_normal;
5030 return bs;
5031 }
5032 \f
5033 /* The target has stopped with waitstatus WS. Check if any hardware
5034 watchpoints have triggered, according to the target. */
5035
5036 int
5037 watchpoints_triggered (struct target_waitstatus *ws)
5038 {
5039 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
5040 CORE_ADDR addr;
5041 struct breakpoint *b;
5042
5043 if (!stopped_by_watchpoint)
5044 {
5045 /* We were not stopped by a watchpoint. Mark all watchpoints
5046 as not triggered. */
5047 ALL_BREAKPOINTS (b)
5048 if (is_hardware_watchpoint (b))
5049 {
5050 struct watchpoint *w = (struct watchpoint *) b;
5051
5052 w->watchpoint_triggered = watch_triggered_no;
5053 }
5054
5055 return 0;
5056 }
5057
5058 if (!target_stopped_data_address (&current_target, &addr))
5059 {
5060 /* We were stopped by a watchpoint, but we don't know where.
5061 Mark all watchpoints as unknown. */
5062 ALL_BREAKPOINTS (b)
5063 if (is_hardware_watchpoint (b))
5064 {
5065 struct watchpoint *w = (struct watchpoint *) b;
5066
5067 w->watchpoint_triggered = watch_triggered_unknown;
5068 }
5069
5070 return 1;
5071 }
5072
5073 /* The target could report the data address. Mark watchpoints
5074 affected by this data address as triggered, and all others as not
5075 triggered. */
5076
5077 ALL_BREAKPOINTS (b)
5078 if (is_hardware_watchpoint (b))
5079 {
5080 struct watchpoint *w = (struct watchpoint *) b;
5081 struct bp_location *loc;
5082
5083 w->watchpoint_triggered = watch_triggered_no;
5084 for (loc = b->loc; loc; loc = loc->next)
5085 {
5086 if (is_masked_watchpoint (b))
5087 {
5088 CORE_ADDR newaddr = addr & w->hw_wp_mask;
5089 CORE_ADDR start = loc->address & w->hw_wp_mask;
5090
5091 if (newaddr == start)
5092 {
5093 w->watchpoint_triggered = watch_triggered_yes;
5094 break;
5095 }
5096 }
5097 /* Exact match not required. Within range is sufficient. */
5098 else if (target_watchpoint_addr_within_range (&current_target,
5099 addr, loc->address,
5100 loc->length))
5101 {
5102 w->watchpoint_triggered = watch_triggered_yes;
5103 break;
5104 }
5105 }
5106 }
5107
5108 return 1;
5109 }
5110
5111 /* Possible return values for watchpoint_check (this can't be an enum
5112 because of check_errors). */
5113 /* The watchpoint has been deleted. */
5114 #define WP_DELETED 1
5115 /* The value has changed. */
5116 #define WP_VALUE_CHANGED 2
5117 /* The value has not changed. */
5118 #define WP_VALUE_NOT_CHANGED 3
5119 /* Ignore this watchpoint, no matter if the value changed or not. */
5120 #define WP_IGNORE 4
5121
5122 #define BP_TEMPFLAG 1
5123 #define BP_HARDWAREFLAG 2
5124
5125 /* Evaluate watchpoint condition expression and check if its value
5126 changed.
5127
5128 P should be a pointer to struct bpstat, but is defined as a void *
5129 in order for this function to be usable with catch_errors. */
5130
5131 static int
5132 watchpoint_check (void *p)
5133 {
5134 bpstat bs = (bpstat) p;
5135 struct watchpoint *b;
5136 struct frame_info *fr;
5137 int within_current_scope;
5138
5139 /* BS is built from an existing struct breakpoint. */
5140 gdb_assert (bs->breakpoint_at != NULL);
5141 b = (struct watchpoint *) bs->breakpoint_at;
5142
5143 /* If this is a local watchpoint, we only want to check if the
5144 watchpoint frame is in scope if the current thread is the thread
5145 that was used to create the watchpoint. */
5146 if (!watchpoint_in_thread_scope (b))
5147 return WP_IGNORE;
5148
5149 if (b->exp_valid_block == NULL)
5150 within_current_scope = 1;
5151 else
5152 {
5153 struct frame_info *frame = get_current_frame ();
5154 struct gdbarch *frame_arch = get_frame_arch (frame);
5155 CORE_ADDR frame_pc = get_frame_pc (frame);
5156
5157 /* stack_frame_destroyed_p() returns a non-zero value if we're
5158 still in the function but the stack frame has already been
5159 invalidated. Since we can't rely on the values of local
5160 variables after the stack has been destroyed, we are treating
5161 the watchpoint in that state as `not changed' without further
5162 checking. Don't mark watchpoints as changed if the current
5163 frame is in an epilogue - even if they are in some other
5164 frame, our view of the stack is likely to be wrong and
5165 frame_find_by_id could error out. */
5166 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
5167 return WP_IGNORE;
5168
5169 fr = frame_find_by_id (b->watchpoint_frame);
5170 within_current_scope = (fr != NULL);
5171
5172 /* If we've gotten confused in the unwinder, we might have
5173 returned a frame that can't describe this variable. */
5174 if (within_current_scope)
5175 {
5176 struct symbol *function;
5177
5178 function = get_frame_function (fr);
5179 if (function == NULL
5180 || !contained_in (b->exp_valid_block,
5181 SYMBOL_BLOCK_VALUE (function)))
5182 within_current_scope = 0;
5183 }
5184
5185 if (within_current_scope)
5186 /* If we end up stopping, the current frame will get selected
5187 in normal_stop. So this call to select_frame won't affect
5188 the user. */
5189 select_frame (fr);
5190 }
5191
5192 if (within_current_scope)
5193 {
5194 /* We use value_{,free_to_}mark because it could be a *long*
5195 time before we return to the command level and call
5196 free_all_values. We can't call free_all_values because we
5197 might be in the middle of evaluating a function call. */
5198
5199 int pc = 0;
5200 struct value *mark;
5201 struct value *new_val;
5202
5203 if (is_masked_watchpoint (&b->base))
5204 /* Since we don't know the exact trigger address (from
5205 stopped_data_address), just tell the user we've triggered
5206 a mask watchpoint. */
5207 return WP_VALUE_CHANGED;
5208
5209 mark = value_mark ();
5210 fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL, 0);
5211
5212 if (b->val_bitsize != 0)
5213 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5214
5215 /* We use value_equal_contents instead of value_equal because
5216 the latter coerces an array to a pointer, thus comparing just
5217 the address of the array instead of its contents. This is
5218 not what we want. */
5219 if ((b->val != NULL) != (new_val != NULL)
5220 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
5221 {
5222 if (new_val != NULL)
5223 {
5224 release_value (new_val);
5225 value_free_to_mark (mark);
5226 }
5227 bs->old_val = b->val;
5228 b->val = new_val;
5229 b->val_valid = 1;
5230 return WP_VALUE_CHANGED;
5231 }
5232 else
5233 {
5234 /* Nothing changed. */
5235 value_free_to_mark (mark);
5236 return WP_VALUE_NOT_CHANGED;
5237 }
5238 }
5239 else
5240 {
5241 /* This seems like the only logical thing to do because
5242 if we temporarily ignored the watchpoint, then when
5243 we reenter the block in which it is valid it contains
5244 garbage (in the case of a function, it may have two
5245 garbage values, one before and one after the prologue).
5246 So we can't even detect the first assignment to it and
5247 watch after that (since the garbage may or may not equal
5248 the first value assigned). */
5249 /* We print all the stop information in
5250 breakpoint_ops->print_it, but in this case, by the time we
5251 call breakpoint_ops->print_it this bp will be deleted
5252 already. So we have no choice but print the information
5253 here. */
5254
5255 SWITCH_THRU_ALL_UIS ()
5256 {
5257 struct ui_out *uiout = current_uiout;
5258
5259 if (ui_out_is_mi_like_p (uiout))
5260 ui_out_field_string
5261 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
5262 ui_out_text (uiout, "\nWatchpoint ");
5263 ui_out_field_int (uiout, "wpnum", b->base.number);
5264 ui_out_text (uiout,
5265 " deleted because the program has left the block in\n"
5266 "which its expression is valid.\n");
5267 }
5268
5269 /* Make sure the watchpoint's commands aren't executed. */
5270 decref_counted_command_line (&b->base.commands);
5271 watchpoint_del_at_next_stop (b);
5272
5273 return WP_DELETED;
5274 }
5275 }
5276
5277 /* Return true if it looks like target has stopped due to hitting
5278 breakpoint location BL. This function does not check if we should
5279 stop, only if BL explains the stop. */
5280
5281 static int
5282 bpstat_check_location (const struct bp_location *bl,
5283 struct address_space *aspace, CORE_ADDR bp_addr,
5284 const struct target_waitstatus *ws)
5285 {
5286 struct breakpoint *b = bl->owner;
5287
5288 /* BL is from an existing breakpoint. */
5289 gdb_assert (b != NULL);
5290
5291 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
5292 }
5293
5294 /* Determine if the watched values have actually changed, and we
5295 should stop. If not, set BS->stop to 0. */
5296
5297 static void
5298 bpstat_check_watchpoint (bpstat bs)
5299 {
5300 const struct bp_location *bl;
5301 struct watchpoint *b;
5302
5303 /* BS is built for existing struct breakpoint. */
5304 bl = bs->bp_location_at;
5305 gdb_assert (bl != NULL);
5306 b = (struct watchpoint *) bs->breakpoint_at;
5307 gdb_assert (b != NULL);
5308
5309 {
5310 int must_check_value = 0;
5311
5312 if (b->base.type == bp_watchpoint)
5313 /* For a software watchpoint, we must always check the
5314 watched value. */
5315 must_check_value = 1;
5316 else if (b->watchpoint_triggered == watch_triggered_yes)
5317 /* We have a hardware watchpoint (read, write, or access)
5318 and the target earlier reported an address watched by
5319 this watchpoint. */
5320 must_check_value = 1;
5321 else if (b->watchpoint_triggered == watch_triggered_unknown
5322 && b->base.type == bp_hardware_watchpoint)
5323 /* We were stopped by a hardware watchpoint, but the target could
5324 not report the data address. We must check the watchpoint's
5325 value. Access and read watchpoints are out of luck; without
5326 a data address, we can't figure it out. */
5327 must_check_value = 1;
5328
5329 if (must_check_value)
5330 {
5331 char *message
5332 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
5333 b->base.number);
5334 struct cleanup *cleanups = make_cleanup (xfree, message);
5335 int e = catch_errors (watchpoint_check, bs, message,
5336 RETURN_MASK_ALL);
5337 do_cleanups (cleanups);
5338 switch (e)
5339 {
5340 case WP_DELETED:
5341 /* We've already printed what needs to be printed. */
5342 bs->print_it = print_it_done;
5343 /* Stop. */
5344 break;
5345 case WP_IGNORE:
5346 bs->print_it = print_it_noop;
5347 bs->stop = 0;
5348 break;
5349 case WP_VALUE_CHANGED:
5350 if (b->base.type == bp_read_watchpoint)
5351 {
5352 /* There are two cases to consider here:
5353
5354 1. We're watching the triggered memory for reads.
5355 In that case, trust the target, and always report
5356 the watchpoint hit to the user. Even though
5357 reads don't cause value changes, the value may
5358 have changed since the last time it was read, and
5359 since we're not trapping writes, we will not see
5360 those, and as such we should ignore our notion of
5361 old value.
5362
5363 2. We're watching the triggered memory for both
5364 reads and writes. There are two ways this may
5365 happen:
5366
5367 2.1. This is a target that can't break on data
5368 reads only, but can break on accesses (reads or
5369 writes), such as e.g., x86. We detect this case
5370 at the time we try to insert read watchpoints.
5371
5372 2.2. Otherwise, the target supports read
5373 watchpoints, but, the user set an access or write
5374 watchpoint watching the same memory as this read
5375 watchpoint.
5376
5377 If we're watching memory writes as well as reads,
5378 ignore watchpoint hits when we find that the
5379 value hasn't changed, as reads don't cause
5380 changes. This still gives false positives when
5381 the program writes the same value to memory as
5382 what there was already in memory (we will confuse
5383 it for a read), but it's much better than
5384 nothing. */
5385
5386 int other_write_watchpoint = 0;
5387
5388 if (bl->watchpoint_type == hw_read)
5389 {
5390 struct breakpoint *other_b;
5391
5392 ALL_BREAKPOINTS (other_b)
5393 if (other_b->type == bp_hardware_watchpoint
5394 || other_b->type == bp_access_watchpoint)
5395 {
5396 struct watchpoint *other_w =
5397 (struct watchpoint *) other_b;
5398
5399 if (other_w->watchpoint_triggered
5400 == watch_triggered_yes)
5401 {
5402 other_write_watchpoint = 1;
5403 break;
5404 }
5405 }
5406 }
5407
5408 if (other_write_watchpoint
5409 || bl->watchpoint_type == hw_access)
5410 {
5411 /* We're watching the same memory for writes,
5412 and the value changed since the last time we
5413 updated it, so this trap must be for a write.
5414 Ignore it. */
5415 bs->print_it = print_it_noop;
5416 bs->stop = 0;
5417 }
5418 }
5419 break;
5420 case WP_VALUE_NOT_CHANGED:
5421 if (b->base.type == bp_hardware_watchpoint
5422 || b->base.type == bp_watchpoint)
5423 {
5424 /* Don't stop: write watchpoints shouldn't fire if
5425 the value hasn't changed. */
5426 bs->print_it = print_it_noop;
5427 bs->stop = 0;
5428 }
5429 /* Stop. */
5430 break;
5431 default:
5432 /* Can't happen. */
5433 case 0:
5434 /* Error from catch_errors. */
5435 {
5436 SWITCH_THRU_ALL_UIS ()
5437 {
5438 printf_filtered (_("Watchpoint %d deleted.\n"),
5439 b->base.number);
5440 }
5441 watchpoint_del_at_next_stop (b);
5442 /* We've already printed what needs to be printed. */
5443 bs->print_it = print_it_done;
5444 }
5445 break;
5446 }
5447 }
5448 else /* must_check_value == 0 */
5449 {
5450 /* This is a case where some watchpoint(s) triggered, but
5451 not at the address of this watchpoint, or else no
5452 watchpoint triggered after all. So don't print
5453 anything for this watchpoint. */
5454 bs->print_it = print_it_noop;
5455 bs->stop = 0;
5456 }
5457 }
5458 }
5459
5460 /* For breakpoints that are currently marked as telling gdb to stop,
5461 check conditions (condition proper, frame, thread and ignore count)
5462 of breakpoint referred to by BS. If we should not stop for this
5463 breakpoint, set BS->stop to 0. */
5464
5465 static void
5466 bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5467 {
5468 const struct bp_location *bl;
5469 struct breakpoint *b;
5470 int value_is_zero = 0;
5471 struct expression *cond;
5472
5473 gdb_assert (bs->stop);
5474
5475 /* BS is built for existing struct breakpoint. */
5476 bl = bs->bp_location_at;
5477 gdb_assert (bl != NULL);
5478 b = bs->breakpoint_at;
5479 gdb_assert (b != NULL);
5480
5481 /* Even if the target evaluated the condition on its end and notified GDB, we
5482 need to do so again since GDB does not know if we stopped due to a
5483 breakpoint or a single step breakpoint. */
5484
5485 if (frame_id_p (b->frame_id)
5486 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
5487 {
5488 bs->stop = 0;
5489 return;
5490 }
5491
5492 /* If this is a thread/task-specific breakpoint, don't waste cpu
5493 evaluating the condition if this isn't the specified
5494 thread/task. */
5495 if ((b->thread != -1 && b->thread != ptid_to_global_thread_id (ptid))
5496 || (b->task != 0 && b->task != ada_get_task_number (ptid)))
5497
5498 {
5499 bs->stop = 0;
5500 return;
5501 }
5502
5503 /* Evaluate extension language breakpoints that have a "stop" method
5504 implemented. */
5505 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
5506
5507 if (is_watchpoint (b))
5508 {
5509 struct watchpoint *w = (struct watchpoint *) b;
5510
5511 cond = w->cond_exp;
5512 }
5513 else
5514 cond = bl->cond;
5515
5516 if (cond && b->disposition != disp_del_at_next_stop)
5517 {
5518 int within_current_scope = 1;
5519 struct watchpoint * w;
5520
5521 /* We use value_mark and value_free_to_mark because it could
5522 be a long time before we return to the command level and
5523 call free_all_values. We can't call free_all_values
5524 because we might be in the middle of evaluating a
5525 function call. */
5526 struct value *mark = value_mark ();
5527
5528 if (is_watchpoint (b))
5529 w = (struct watchpoint *) b;
5530 else
5531 w = NULL;
5532
5533 /* Need to select the frame, with all that implies so that
5534 the conditions will have the right context. Because we
5535 use the frame, we will not see an inlined function's
5536 variables when we arrive at a breakpoint at the start
5537 of the inlined function; the current frame will be the
5538 call site. */
5539 if (w == NULL || w->cond_exp_valid_block == NULL)
5540 select_frame (get_current_frame ());
5541 else
5542 {
5543 struct frame_info *frame;
5544
5545 /* For local watchpoint expressions, which particular
5546 instance of a local is being watched matters, so we
5547 keep track of the frame to evaluate the expression
5548 in. To evaluate the condition however, it doesn't
5549 really matter which instantiation of the function
5550 where the condition makes sense triggers the
5551 watchpoint. This allows an expression like "watch
5552 global if q > 10" set in `func', catch writes to
5553 global on all threads that call `func', or catch
5554 writes on all recursive calls of `func' by a single
5555 thread. We simply always evaluate the condition in
5556 the innermost frame that's executing where it makes
5557 sense to evaluate the condition. It seems
5558 intuitive. */
5559 frame = block_innermost_frame (w->cond_exp_valid_block);
5560 if (frame != NULL)
5561 select_frame (frame);
5562 else
5563 within_current_scope = 0;
5564 }
5565 if (within_current_scope)
5566 value_is_zero
5567 = catch_errors (breakpoint_cond_eval, cond,
5568 "Error in testing breakpoint condition:\n",
5569 RETURN_MASK_ALL);
5570 else
5571 {
5572 warning (_("Watchpoint condition cannot be tested "
5573 "in the current scope"));
5574 /* If we failed to set the right context for this
5575 watchpoint, unconditionally report it. */
5576 value_is_zero = 0;
5577 }
5578 /* FIXME-someday, should give breakpoint #. */
5579 value_free_to_mark (mark);
5580 }
5581
5582 if (cond && value_is_zero)
5583 {
5584 bs->stop = 0;
5585 }
5586 else if (b->ignore_count > 0)
5587 {
5588 b->ignore_count--;
5589 bs->stop = 0;
5590 /* Increase the hit count even though we don't stop. */
5591 ++(b->hit_count);
5592 observer_notify_breakpoint_modified (b);
5593 }
5594 }
5595
5596 /* Returns true if we need to track moribund locations of LOC's type
5597 on the current target. */
5598
5599 static int
5600 need_moribund_for_location_type (struct bp_location *loc)
5601 {
5602 return ((loc->loc_type == bp_loc_software_breakpoint
5603 && !target_supports_stopped_by_sw_breakpoint ())
5604 || (loc->loc_type == bp_loc_hardware_breakpoint
5605 && !target_supports_stopped_by_hw_breakpoint ()));
5606 }
5607
5608
5609 /* Get a bpstat associated with having just stopped at address
5610 BP_ADDR in thread PTID.
5611
5612 Determine whether we stopped at a breakpoint, etc, or whether we
5613 don't understand this stop. Result is a chain of bpstat's such
5614 that:
5615
5616 if we don't understand the stop, the result is a null pointer.
5617
5618 if we understand why we stopped, the result is not null.
5619
5620 Each element of the chain refers to a particular breakpoint or
5621 watchpoint at which we have stopped. (We may have stopped for
5622 several reasons concurrently.)
5623
5624 Each element of the chain has valid next, breakpoint_at,
5625 commands, FIXME??? fields. */
5626
5627 bpstat
5628 bpstat_stop_status (struct address_space *aspace,
5629 CORE_ADDR bp_addr, ptid_t ptid,
5630 const struct target_waitstatus *ws)
5631 {
5632 struct breakpoint *b = NULL;
5633 struct bp_location *bl;
5634 struct bp_location *loc;
5635 /* First item of allocated bpstat's. */
5636 bpstat bs_head = NULL, *bs_link = &bs_head;
5637 /* Pointer to the last thing in the chain currently. */
5638 bpstat bs;
5639 int ix;
5640 int need_remove_insert;
5641 int removed_any;
5642
5643 /* First, build the bpstat chain with locations that explain a
5644 target stop, while being careful to not set the target running,
5645 as that may invalidate locations (in particular watchpoint
5646 locations are recreated). Resuming will happen here with
5647 breakpoint conditions or watchpoint expressions that include
5648 inferior function calls. */
5649
5650 ALL_BREAKPOINTS (b)
5651 {
5652 if (!breakpoint_enabled (b))
5653 continue;
5654
5655 for (bl = b->loc; bl != NULL; bl = bl->next)
5656 {
5657 /* For hardware watchpoints, we look only at the first
5658 location. The watchpoint_check function will work on the
5659 entire expression, not the individual locations. For
5660 read watchpoints, the watchpoints_triggered function has
5661 checked all locations already. */
5662 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5663 break;
5664
5665 if (!bl->enabled || bl->shlib_disabled)
5666 continue;
5667
5668 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
5669 continue;
5670
5671 /* Come here if it's a watchpoint, or if the break address
5672 matches. */
5673
5674 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
5675 explain stop. */
5676
5677 /* Assume we stop. Should we find a watchpoint that is not
5678 actually triggered, or if the condition of the breakpoint
5679 evaluates as false, we'll reset 'stop' to 0. */
5680 bs->stop = 1;
5681 bs->print = 1;
5682
5683 /* If this is a scope breakpoint, mark the associated
5684 watchpoint as triggered so that we will handle the
5685 out-of-scope event. We'll get to the watchpoint next
5686 iteration. */
5687 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
5688 {
5689 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5690
5691 w->watchpoint_triggered = watch_triggered_yes;
5692 }
5693 }
5694 }
5695
5696 /* Check if a moribund breakpoint explains the stop. */
5697 if (!target_supports_stopped_by_sw_breakpoint ()
5698 || !target_supports_stopped_by_hw_breakpoint ())
5699 {
5700 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
5701 {
5702 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5703 && need_moribund_for_location_type (loc))
5704 {
5705 bs = bpstat_alloc (loc, &bs_link);
5706 /* For hits of moribund locations, we should just proceed. */
5707 bs->stop = 0;
5708 bs->print = 0;
5709 bs->print_it = print_it_noop;
5710 }
5711 }
5712 }
5713
5714 /* A bit of special processing for shlib breakpoints. We need to
5715 process solib loading here, so that the lists of loaded and
5716 unloaded libraries are correct before we handle "catch load" and
5717 "catch unload". */
5718 for (bs = bs_head; bs != NULL; bs = bs->next)
5719 {
5720 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
5721 {
5722 handle_solib_event ();
5723 break;
5724 }
5725 }
5726
5727 /* Now go through the locations that caused the target to stop, and
5728 check whether we're interested in reporting this stop to higher
5729 layers, or whether we should resume the target transparently. */
5730
5731 removed_any = 0;
5732
5733 for (bs = bs_head; bs != NULL; bs = bs->next)
5734 {
5735 if (!bs->stop)
5736 continue;
5737
5738 b = bs->breakpoint_at;
5739 b->ops->check_status (bs);
5740 if (bs->stop)
5741 {
5742 bpstat_check_breakpoint_conditions (bs, ptid);
5743
5744 if (bs->stop)
5745 {
5746 ++(b->hit_count);
5747 observer_notify_breakpoint_modified (b);
5748
5749 /* We will stop here. */
5750 if (b->disposition == disp_disable)
5751 {
5752 --(b->enable_count);
5753 if (b->enable_count <= 0)
5754 b->enable_state = bp_disabled;
5755 removed_any = 1;
5756 }
5757 if (b->silent)
5758 bs->print = 0;
5759 bs->commands = b->commands;
5760 incref_counted_command_line (bs->commands);
5761 if (command_line_is_silent (bs->commands
5762 ? bs->commands->commands : NULL))
5763 bs->print = 0;
5764
5765 b->ops->after_condition_true (bs);
5766 }
5767
5768 }
5769
5770 /* Print nothing for this entry if we don't stop or don't
5771 print. */
5772 if (!bs->stop || !bs->print)
5773 bs->print_it = print_it_noop;
5774 }
5775
5776 /* If we aren't stopping, the value of some hardware watchpoint may
5777 not have changed, but the intermediate memory locations we are
5778 watching may have. Don't bother if we're stopping; this will get
5779 done later. */
5780 need_remove_insert = 0;
5781 if (! bpstat_causes_stop (bs_head))
5782 for (bs = bs_head; bs != NULL; bs = bs->next)
5783 if (!bs->stop
5784 && bs->breakpoint_at
5785 && is_hardware_watchpoint (bs->breakpoint_at))
5786 {
5787 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5788
5789 update_watchpoint (w, 0 /* don't reparse. */);
5790 need_remove_insert = 1;
5791 }
5792
5793 if (need_remove_insert)
5794 update_global_location_list (UGLL_MAY_INSERT);
5795 else if (removed_any)
5796 update_global_location_list (UGLL_DONT_INSERT);
5797
5798 return bs_head;
5799 }
5800
5801 static void
5802 handle_jit_event (void)
5803 {
5804 struct frame_info *frame;
5805 struct gdbarch *gdbarch;
5806
5807 if (debug_infrun)
5808 fprintf_unfiltered (gdb_stdlog, "handling bp_jit_event\n");
5809
5810 /* Switch terminal for any messages produced by
5811 breakpoint_re_set. */
5812 target_terminal_ours_for_output ();
5813
5814 frame = get_current_frame ();
5815 gdbarch = get_frame_arch (frame);
5816
5817 jit_event_handler (gdbarch);
5818
5819 target_terminal_inferior ();
5820 }
5821
5822 /* Prepare WHAT final decision for infrun. */
5823
5824 /* Decide what infrun needs to do with this bpstat. */
5825
5826 struct bpstat_what
5827 bpstat_what (bpstat bs_head)
5828 {
5829 struct bpstat_what retval;
5830 bpstat bs;
5831
5832 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
5833 retval.call_dummy = STOP_NONE;
5834 retval.is_longjmp = 0;
5835
5836 for (bs = bs_head; bs != NULL; bs = bs->next)
5837 {
5838 /* Extract this BS's action. After processing each BS, we check
5839 if its action overrides all we've seem so far. */
5840 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5841 enum bptype bptype;
5842
5843 if (bs->breakpoint_at == NULL)
5844 {
5845 /* I suspect this can happen if it was a momentary
5846 breakpoint which has since been deleted. */
5847 bptype = bp_none;
5848 }
5849 else
5850 bptype = bs->breakpoint_at->type;
5851
5852 switch (bptype)
5853 {
5854 case bp_none:
5855 break;
5856 case bp_breakpoint:
5857 case bp_hardware_breakpoint:
5858 case bp_single_step:
5859 case bp_until:
5860 case bp_finish:
5861 case bp_shlib_event:
5862 if (bs->stop)
5863 {
5864 if (bs->print)
5865 this_action = BPSTAT_WHAT_STOP_NOISY;
5866 else
5867 this_action = BPSTAT_WHAT_STOP_SILENT;
5868 }
5869 else
5870 this_action = BPSTAT_WHAT_SINGLE;
5871 break;
5872 case bp_watchpoint:
5873 case bp_hardware_watchpoint:
5874 case bp_read_watchpoint:
5875 case bp_access_watchpoint:
5876 if (bs->stop)
5877 {
5878 if (bs->print)
5879 this_action = BPSTAT_WHAT_STOP_NOISY;
5880 else
5881 this_action = BPSTAT_WHAT_STOP_SILENT;
5882 }
5883 else
5884 {
5885 /* There was a watchpoint, but we're not stopping.
5886 This requires no further action. */
5887 }
5888 break;
5889 case bp_longjmp:
5890 case bp_longjmp_call_dummy:
5891 case bp_exception:
5892 if (bs->stop)
5893 {
5894 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5895 retval.is_longjmp = bptype != bp_exception;
5896 }
5897 else
5898 this_action = BPSTAT_WHAT_SINGLE;
5899 break;
5900 case bp_longjmp_resume:
5901 case bp_exception_resume:
5902 if (bs->stop)
5903 {
5904 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5905 retval.is_longjmp = bptype == bp_longjmp_resume;
5906 }
5907 else
5908 this_action = BPSTAT_WHAT_SINGLE;
5909 break;
5910 case bp_step_resume:
5911 if (bs->stop)
5912 this_action = BPSTAT_WHAT_STEP_RESUME;
5913 else
5914 {
5915 /* It is for the wrong frame. */
5916 this_action = BPSTAT_WHAT_SINGLE;
5917 }
5918 break;
5919 case bp_hp_step_resume:
5920 if (bs->stop)
5921 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5922 else
5923 {
5924 /* It is for the wrong frame. */
5925 this_action = BPSTAT_WHAT_SINGLE;
5926 }
5927 break;
5928 case bp_watchpoint_scope:
5929 case bp_thread_event:
5930 case bp_overlay_event:
5931 case bp_longjmp_master:
5932 case bp_std_terminate_master:
5933 case bp_exception_master:
5934 this_action = BPSTAT_WHAT_SINGLE;
5935 break;
5936 case bp_catchpoint:
5937 if (bs->stop)
5938 {
5939 if (bs->print)
5940 this_action = BPSTAT_WHAT_STOP_NOISY;
5941 else
5942 this_action = BPSTAT_WHAT_STOP_SILENT;
5943 }
5944 else
5945 {
5946 /* There was a catchpoint, but we're not stopping.
5947 This requires no further action. */
5948 }
5949 break;
5950 case bp_jit_event:
5951 this_action = BPSTAT_WHAT_SINGLE;
5952 break;
5953 case bp_call_dummy:
5954 /* Make sure the action is stop (silent or noisy),
5955 so infrun.c pops the dummy frame. */
5956 retval.call_dummy = STOP_STACK_DUMMY;
5957 this_action = BPSTAT_WHAT_STOP_SILENT;
5958 break;
5959 case bp_std_terminate:
5960 /* Make sure the action is stop (silent or noisy),
5961 so infrun.c pops the dummy frame. */
5962 retval.call_dummy = STOP_STD_TERMINATE;
5963 this_action = BPSTAT_WHAT_STOP_SILENT;
5964 break;
5965 case bp_tracepoint:
5966 case bp_fast_tracepoint:
5967 case bp_static_tracepoint:
5968 /* Tracepoint hits should not be reported back to GDB, and
5969 if one got through somehow, it should have been filtered
5970 out already. */
5971 internal_error (__FILE__, __LINE__,
5972 _("bpstat_what: tracepoint encountered"));
5973 break;
5974 case bp_gnu_ifunc_resolver:
5975 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5976 this_action = BPSTAT_WHAT_SINGLE;
5977 break;
5978 case bp_gnu_ifunc_resolver_return:
5979 /* The breakpoint will be removed, execution will restart from the
5980 PC of the former breakpoint. */
5981 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5982 break;
5983
5984 case bp_dprintf:
5985 if (bs->stop)
5986 this_action = BPSTAT_WHAT_STOP_SILENT;
5987 else
5988 this_action = BPSTAT_WHAT_SINGLE;
5989 break;
5990
5991 default:
5992 internal_error (__FILE__, __LINE__,
5993 _("bpstat_what: unhandled bptype %d"), (int) bptype);
5994 }
5995
5996 retval.main_action = std::max (retval.main_action, this_action);
5997 }
5998
5999 return retval;
6000 }
6001
6002 void
6003 bpstat_run_callbacks (bpstat bs_head)
6004 {
6005 bpstat bs;
6006
6007 for (bs = bs_head; bs != NULL; bs = bs->next)
6008 {
6009 struct breakpoint *b = bs->breakpoint_at;
6010
6011 if (b == NULL)
6012 continue;
6013 switch (b->type)
6014 {
6015 case bp_jit_event:
6016 handle_jit_event ();
6017 break;
6018 case bp_gnu_ifunc_resolver:
6019 gnu_ifunc_resolver_stop (b);
6020 break;
6021 case bp_gnu_ifunc_resolver_return:
6022 gnu_ifunc_resolver_return_stop (b);
6023 break;
6024 }
6025 }
6026 }
6027
6028 /* Nonzero if we should step constantly (e.g. watchpoints on machines
6029 without hardware support). This isn't related to a specific bpstat,
6030 just to things like whether watchpoints are set. */
6031
6032 int
6033 bpstat_should_step (void)
6034 {
6035 struct breakpoint *b;
6036
6037 ALL_BREAKPOINTS (b)
6038 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
6039 return 1;
6040 return 0;
6041 }
6042
6043 int
6044 bpstat_causes_stop (bpstat bs)
6045 {
6046 for (; bs != NULL; bs = bs->next)
6047 if (bs->stop)
6048 return 1;
6049
6050 return 0;
6051 }
6052
6053 \f
6054
6055 /* Compute a string of spaces suitable to indent the next line
6056 so it starts at the position corresponding to the table column
6057 named COL_NAME in the currently active table of UIOUT. */
6058
6059 static char *
6060 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
6061 {
6062 static char wrap_indent[80];
6063 int i, total_width, width, align;
6064 char *text;
6065
6066 total_width = 0;
6067 for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
6068 {
6069 if (strcmp (text, col_name) == 0)
6070 {
6071 gdb_assert (total_width < sizeof wrap_indent);
6072 memset (wrap_indent, ' ', total_width);
6073 wrap_indent[total_width] = 0;
6074
6075 return wrap_indent;
6076 }
6077
6078 total_width += width + 1;
6079 }
6080
6081 return NULL;
6082 }
6083
6084 /* Determine if the locations of this breakpoint will have their conditions
6085 evaluated by the target, host or a mix of both. Returns the following:
6086
6087 "host": Host evals condition.
6088 "host or target": Host or Target evals condition.
6089 "target": Target evals condition.
6090 */
6091
6092 static const char *
6093 bp_condition_evaluator (struct breakpoint *b)
6094 {
6095 struct bp_location *bl;
6096 char host_evals = 0;
6097 char target_evals = 0;
6098
6099 if (!b)
6100 return NULL;
6101
6102 if (!is_breakpoint (b))
6103 return NULL;
6104
6105 if (gdb_evaluates_breakpoint_condition_p ()
6106 || !target_supports_evaluation_of_breakpoint_conditions ())
6107 return condition_evaluation_host;
6108
6109 for (bl = b->loc; bl; bl = bl->next)
6110 {
6111 if (bl->cond_bytecode)
6112 target_evals++;
6113 else
6114 host_evals++;
6115 }
6116
6117 if (host_evals && target_evals)
6118 return condition_evaluation_both;
6119 else if (target_evals)
6120 return condition_evaluation_target;
6121 else
6122 return condition_evaluation_host;
6123 }
6124
6125 /* Determine the breakpoint location's condition evaluator. This is
6126 similar to bp_condition_evaluator, but for locations. */
6127
6128 static const char *
6129 bp_location_condition_evaluator (struct bp_location *bl)
6130 {
6131 if (bl && !is_breakpoint (bl->owner))
6132 return NULL;
6133
6134 if (gdb_evaluates_breakpoint_condition_p ()
6135 || !target_supports_evaluation_of_breakpoint_conditions ())
6136 return condition_evaluation_host;
6137
6138 if (bl && bl->cond_bytecode)
6139 return condition_evaluation_target;
6140 else
6141 return condition_evaluation_host;
6142 }
6143
6144 /* Print the LOC location out of the list of B->LOC locations. */
6145
6146 static void
6147 print_breakpoint_location (struct breakpoint *b,
6148 struct bp_location *loc)
6149 {
6150 struct ui_out *uiout = current_uiout;
6151 struct cleanup *old_chain = save_current_program_space ();
6152
6153 if (loc != NULL && loc->shlib_disabled)
6154 loc = NULL;
6155
6156 if (loc != NULL)
6157 set_current_program_space (loc->pspace);
6158
6159 if (b->display_canonical)
6160 ui_out_field_string (uiout, "what",
6161 event_location_to_string (b->location));
6162 else if (loc && loc->symtab)
6163 {
6164 struct symbol *sym
6165 = find_pc_sect_function (loc->address, loc->section);
6166 if (sym)
6167 {
6168 ui_out_text (uiout, "in ");
6169 ui_out_field_string (uiout, "func",
6170 SYMBOL_PRINT_NAME (sym));
6171 ui_out_text (uiout, " ");
6172 ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
6173 ui_out_text (uiout, "at ");
6174 }
6175 ui_out_field_string (uiout, "file",
6176 symtab_to_filename_for_display (loc->symtab));
6177 ui_out_text (uiout, ":");
6178
6179 if (ui_out_is_mi_like_p (uiout))
6180 ui_out_field_string (uiout, "fullname",
6181 symtab_to_fullname (loc->symtab));
6182
6183 ui_out_field_int (uiout, "line", loc->line_number);
6184 }
6185 else if (loc)
6186 {
6187 struct ui_file *stb = mem_fileopen ();
6188 struct cleanup *stb_chain = make_cleanup_ui_file_delete (stb);
6189
6190 print_address_symbolic (loc->gdbarch, loc->address, stb,
6191 demangle, "");
6192 ui_out_field_stream (uiout, "at", stb);
6193
6194 do_cleanups (stb_chain);
6195 }
6196 else
6197 {
6198 ui_out_field_string (uiout, "pending",
6199 event_location_to_string (b->location));
6200 /* If extra_string is available, it could be holding a condition
6201 or dprintf arguments. In either case, make sure it is printed,
6202 too, but only for non-MI streams. */
6203 if (!ui_out_is_mi_like_p (uiout) && b->extra_string != NULL)
6204 {
6205 if (b->type == bp_dprintf)
6206 ui_out_text (uiout, ",");
6207 else
6208 ui_out_text (uiout, " ");
6209 ui_out_text (uiout, b->extra_string);
6210 }
6211 }
6212
6213 if (loc && is_breakpoint (b)
6214 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6215 && bp_condition_evaluator (b) == condition_evaluation_both)
6216 {
6217 ui_out_text (uiout, " (");
6218 ui_out_field_string (uiout, "evaluated-by",
6219 bp_location_condition_evaluator (loc));
6220 ui_out_text (uiout, ")");
6221 }
6222
6223 do_cleanups (old_chain);
6224 }
6225
6226 static const char *
6227 bptype_string (enum bptype type)
6228 {
6229 struct ep_type_description
6230 {
6231 enum bptype type;
6232 char *description;
6233 };
6234 static struct ep_type_description bptypes[] =
6235 {
6236 {bp_none, "?deleted?"},
6237 {bp_breakpoint, "breakpoint"},
6238 {bp_hardware_breakpoint, "hw breakpoint"},
6239 {bp_single_step, "sw single-step"},
6240 {bp_until, "until"},
6241 {bp_finish, "finish"},
6242 {bp_watchpoint, "watchpoint"},
6243 {bp_hardware_watchpoint, "hw watchpoint"},
6244 {bp_read_watchpoint, "read watchpoint"},
6245 {bp_access_watchpoint, "acc watchpoint"},
6246 {bp_longjmp, "longjmp"},
6247 {bp_longjmp_resume, "longjmp resume"},
6248 {bp_longjmp_call_dummy, "longjmp for call dummy"},
6249 {bp_exception, "exception"},
6250 {bp_exception_resume, "exception resume"},
6251 {bp_step_resume, "step resume"},
6252 {bp_hp_step_resume, "high-priority step resume"},
6253 {bp_watchpoint_scope, "watchpoint scope"},
6254 {bp_call_dummy, "call dummy"},
6255 {bp_std_terminate, "std::terminate"},
6256 {bp_shlib_event, "shlib events"},
6257 {bp_thread_event, "thread events"},
6258 {bp_overlay_event, "overlay events"},
6259 {bp_longjmp_master, "longjmp master"},
6260 {bp_std_terminate_master, "std::terminate master"},
6261 {bp_exception_master, "exception master"},
6262 {bp_catchpoint, "catchpoint"},
6263 {bp_tracepoint, "tracepoint"},
6264 {bp_fast_tracepoint, "fast tracepoint"},
6265 {bp_static_tracepoint, "static tracepoint"},
6266 {bp_dprintf, "dprintf"},
6267 {bp_jit_event, "jit events"},
6268 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6269 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
6270 };
6271
6272 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6273 || ((int) type != bptypes[(int) type].type))
6274 internal_error (__FILE__, __LINE__,
6275 _("bptypes table does not describe type #%d."),
6276 (int) type);
6277
6278 return bptypes[(int) type].description;
6279 }
6280
6281 /* For MI, output a field named 'thread-groups' with a list as the value.
6282 For CLI, prefix the list with the string 'inf'. */
6283
6284 static void
6285 output_thread_groups (struct ui_out *uiout,
6286 const char *field_name,
6287 VEC(int) *inf_num,
6288 int mi_only)
6289 {
6290 struct cleanup *back_to;
6291 int is_mi = ui_out_is_mi_like_p (uiout);
6292 int inf;
6293 int i;
6294
6295 /* For backward compatibility, don't display inferiors in CLI unless
6296 there are several. Always display them for MI. */
6297 if (!is_mi && mi_only)
6298 return;
6299
6300 back_to = make_cleanup_ui_out_list_begin_end (uiout, field_name);
6301
6302 for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
6303 {
6304 if (is_mi)
6305 {
6306 char mi_group[10];
6307
6308 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
6309 ui_out_field_string (uiout, NULL, mi_group);
6310 }
6311 else
6312 {
6313 if (i == 0)
6314 ui_out_text (uiout, " inf ");
6315 else
6316 ui_out_text (uiout, ", ");
6317
6318 ui_out_text (uiout, plongest (inf));
6319 }
6320 }
6321
6322 do_cleanups (back_to);
6323 }
6324
6325 /* Print B to gdb_stdout. */
6326
6327 static void
6328 print_one_breakpoint_location (struct breakpoint *b,
6329 struct bp_location *loc,
6330 int loc_number,
6331 struct bp_location **last_loc,
6332 int allflag)
6333 {
6334 struct command_line *l;
6335 static char bpenables[] = "nynny";
6336
6337 struct ui_out *uiout = current_uiout;
6338 int header_of_multiple = 0;
6339 int part_of_multiple = (loc != NULL);
6340 struct value_print_options opts;
6341
6342 get_user_print_options (&opts);
6343
6344 gdb_assert (!loc || loc_number != 0);
6345 /* See comment in print_one_breakpoint concerning treatment of
6346 breakpoints with single disabled location. */
6347 if (loc == NULL
6348 && (b->loc != NULL
6349 && (b->loc->next != NULL || !b->loc->enabled)))
6350 header_of_multiple = 1;
6351 if (loc == NULL)
6352 loc = b->loc;
6353
6354 annotate_record ();
6355
6356 /* 1 */
6357 annotate_field (0);
6358 if (part_of_multiple)
6359 {
6360 char *formatted;
6361 formatted = xstrprintf ("%d.%d", b->number, loc_number);
6362 ui_out_field_string (uiout, "number", formatted);
6363 xfree (formatted);
6364 }
6365 else
6366 {
6367 ui_out_field_int (uiout, "number", b->number);
6368 }
6369
6370 /* 2 */
6371 annotate_field (1);
6372 if (part_of_multiple)
6373 ui_out_field_skip (uiout, "type");
6374 else
6375 ui_out_field_string (uiout, "type", bptype_string (b->type));
6376
6377 /* 3 */
6378 annotate_field (2);
6379 if (part_of_multiple)
6380 ui_out_field_skip (uiout, "disp");
6381 else
6382 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
6383
6384
6385 /* 4 */
6386 annotate_field (3);
6387 if (part_of_multiple)
6388 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
6389 else
6390 ui_out_field_fmt (uiout, "enabled", "%c",
6391 bpenables[(int) b->enable_state]);
6392 ui_out_spaces (uiout, 2);
6393
6394
6395 /* 5 and 6 */
6396 if (b->ops != NULL && b->ops->print_one != NULL)
6397 {
6398 /* Although the print_one can possibly print all locations,
6399 calling it here is not likely to get any nice result. So,
6400 make sure there's just one location. */
6401 gdb_assert (b->loc == NULL || b->loc->next == NULL);
6402 b->ops->print_one (b, last_loc);
6403 }
6404 else
6405 switch (b->type)
6406 {
6407 case bp_none:
6408 internal_error (__FILE__, __LINE__,
6409 _("print_one_breakpoint: bp_none encountered\n"));
6410 break;
6411
6412 case bp_watchpoint:
6413 case bp_hardware_watchpoint:
6414 case bp_read_watchpoint:
6415 case bp_access_watchpoint:
6416 {
6417 struct watchpoint *w = (struct watchpoint *) b;
6418
6419 /* Field 4, the address, is omitted (which makes the columns
6420 not line up too nicely with the headers, but the effect
6421 is relatively readable). */
6422 if (opts.addressprint)
6423 ui_out_field_skip (uiout, "addr");
6424 annotate_field (5);
6425 ui_out_field_string (uiout, "what", w->exp_string);
6426 }
6427 break;
6428
6429 case bp_breakpoint:
6430 case bp_hardware_breakpoint:
6431 case bp_single_step:
6432 case bp_until:
6433 case bp_finish:
6434 case bp_longjmp:
6435 case bp_longjmp_resume:
6436 case bp_longjmp_call_dummy:
6437 case bp_exception:
6438 case bp_exception_resume:
6439 case bp_step_resume:
6440 case bp_hp_step_resume:
6441 case bp_watchpoint_scope:
6442 case bp_call_dummy:
6443 case bp_std_terminate:
6444 case bp_shlib_event:
6445 case bp_thread_event:
6446 case bp_overlay_event:
6447 case bp_longjmp_master:
6448 case bp_std_terminate_master:
6449 case bp_exception_master:
6450 case bp_tracepoint:
6451 case bp_fast_tracepoint:
6452 case bp_static_tracepoint:
6453 case bp_dprintf:
6454 case bp_jit_event:
6455 case bp_gnu_ifunc_resolver:
6456 case bp_gnu_ifunc_resolver_return:
6457 if (opts.addressprint)
6458 {
6459 annotate_field (4);
6460 if (header_of_multiple)
6461 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
6462 else if (b->loc == NULL || loc->shlib_disabled)
6463 ui_out_field_string (uiout, "addr", "<PENDING>");
6464 else
6465 ui_out_field_core_addr (uiout, "addr",
6466 loc->gdbarch, loc->address);
6467 }
6468 annotate_field (5);
6469 if (!header_of_multiple)
6470 print_breakpoint_location (b, loc);
6471 if (b->loc)
6472 *last_loc = b->loc;
6473 break;
6474 }
6475
6476
6477 if (loc != NULL && !header_of_multiple)
6478 {
6479 struct inferior *inf;
6480 VEC(int) *inf_num = NULL;
6481 int mi_only = 1;
6482
6483 ALL_INFERIORS (inf)
6484 {
6485 if (inf->pspace == loc->pspace)
6486 VEC_safe_push (int, inf_num, inf->num);
6487 }
6488
6489 /* For backward compatibility, don't display inferiors in CLI unless
6490 there are several. Always display for MI. */
6491 if (allflag
6492 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6493 && (number_of_program_spaces () > 1
6494 || number_of_inferiors () > 1)
6495 /* LOC is for existing B, it cannot be in
6496 moribund_locations and thus having NULL OWNER. */
6497 && loc->owner->type != bp_catchpoint))
6498 mi_only = 0;
6499 output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6500 VEC_free (int, inf_num);
6501 }
6502
6503 if (!part_of_multiple)
6504 {
6505 if (b->thread != -1)
6506 {
6507 /* FIXME: This seems to be redundant and lost here; see the
6508 "stop only in" line a little further down. */
6509 ui_out_text (uiout, " thread ");
6510 ui_out_field_int (uiout, "thread", b->thread);
6511 }
6512 else if (b->task != 0)
6513 {
6514 ui_out_text (uiout, " task ");
6515 ui_out_field_int (uiout, "task", b->task);
6516 }
6517 }
6518
6519 ui_out_text (uiout, "\n");
6520
6521 if (!part_of_multiple)
6522 b->ops->print_one_detail (b, uiout);
6523
6524 if (part_of_multiple && frame_id_p (b->frame_id))
6525 {
6526 annotate_field (6);
6527 ui_out_text (uiout, "\tstop only in stack frame at ");
6528 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
6529 the frame ID. */
6530 ui_out_field_core_addr (uiout, "frame",
6531 b->gdbarch, b->frame_id.stack_addr);
6532 ui_out_text (uiout, "\n");
6533 }
6534
6535 if (!part_of_multiple && b->cond_string)
6536 {
6537 annotate_field (7);
6538 if (is_tracepoint (b))
6539 ui_out_text (uiout, "\ttrace only if ");
6540 else
6541 ui_out_text (uiout, "\tstop only if ");
6542 ui_out_field_string (uiout, "cond", b->cond_string);
6543
6544 /* Print whether the target is doing the breakpoint's condition
6545 evaluation. If GDB is doing the evaluation, don't print anything. */
6546 if (is_breakpoint (b)
6547 && breakpoint_condition_evaluation_mode ()
6548 == condition_evaluation_target)
6549 {
6550 ui_out_text (uiout, " (");
6551 ui_out_field_string (uiout, "evaluated-by",
6552 bp_condition_evaluator (b));
6553 ui_out_text (uiout, " evals)");
6554 }
6555 ui_out_text (uiout, "\n");
6556 }
6557
6558 if (!part_of_multiple && b->thread != -1)
6559 {
6560 /* FIXME should make an annotation for this. */
6561 ui_out_text (uiout, "\tstop only in thread ");
6562 if (ui_out_is_mi_like_p (uiout))
6563 ui_out_field_int (uiout, "thread", b->thread);
6564 else
6565 {
6566 struct thread_info *thr = find_thread_global_id (b->thread);
6567
6568 ui_out_field_string (uiout, "thread", print_thread_id (thr));
6569 }
6570 ui_out_text (uiout, "\n");
6571 }
6572
6573 if (!part_of_multiple)
6574 {
6575 if (b->hit_count)
6576 {
6577 /* FIXME should make an annotation for this. */
6578 if (is_catchpoint (b))
6579 ui_out_text (uiout, "\tcatchpoint");
6580 else if (is_tracepoint (b))
6581 ui_out_text (uiout, "\ttracepoint");
6582 else
6583 ui_out_text (uiout, "\tbreakpoint");
6584 ui_out_text (uiout, " already hit ");
6585 ui_out_field_int (uiout, "times", b->hit_count);
6586 if (b->hit_count == 1)
6587 ui_out_text (uiout, " time\n");
6588 else
6589 ui_out_text (uiout, " times\n");
6590 }
6591 else
6592 {
6593 /* Output the count also if it is zero, but only if this is mi. */
6594 if (ui_out_is_mi_like_p (uiout))
6595 ui_out_field_int (uiout, "times", b->hit_count);
6596 }
6597 }
6598
6599 if (!part_of_multiple && b->ignore_count)
6600 {
6601 annotate_field (8);
6602 ui_out_text (uiout, "\tignore next ");
6603 ui_out_field_int (uiout, "ignore", b->ignore_count);
6604 ui_out_text (uiout, " hits\n");
6605 }
6606
6607 /* Note that an enable count of 1 corresponds to "enable once"
6608 behavior, which is reported by the combination of enablement and
6609 disposition, so we don't need to mention it here. */
6610 if (!part_of_multiple && b->enable_count > 1)
6611 {
6612 annotate_field (8);
6613 ui_out_text (uiout, "\tdisable after ");
6614 /* Tweak the wording to clarify that ignore and enable counts
6615 are distinct, and have additive effect. */
6616 if (b->ignore_count)
6617 ui_out_text (uiout, "additional ");
6618 else
6619 ui_out_text (uiout, "next ");
6620 ui_out_field_int (uiout, "enable", b->enable_count);
6621 ui_out_text (uiout, " hits\n");
6622 }
6623
6624 if (!part_of_multiple && is_tracepoint (b))
6625 {
6626 struct tracepoint *tp = (struct tracepoint *) b;
6627
6628 if (tp->traceframe_usage)
6629 {
6630 ui_out_text (uiout, "\ttrace buffer usage ");
6631 ui_out_field_int (uiout, "traceframe-usage", tp->traceframe_usage);
6632 ui_out_text (uiout, " bytes\n");
6633 }
6634 }
6635
6636 l = b->commands ? b->commands->commands : NULL;
6637 if (!part_of_multiple && l)
6638 {
6639 struct cleanup *script_chain;
6640
6641 annotate_field (9);
6642 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
6643 print_command_lines (uiout, l, 4);
6644 do_cleanups (script_chain);
6645 }
6646
6647 if (is_tracepoint (b))
6648 {
6649 struct tracepoint *t = (struct tracepoint *) b;
6650
6651 if (!part_of_multiple && t->pass_count)
6652 {
6653 annotate_field (10);
6654 ui_out_text (uiout, "\tpass count ");
6655 ui_out_field_int (uiout, "pass", t->pass_count);
6656 ui_out_text (uiout, " \n");
6657 }
6658
6659 /* Don't display it when tracepoint or tracepoint location is
6660 pending. */
6661 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6662 {
6663 annotate_field (11);
6664
6665 if (ui_out_is_mi_like_p (uiout))
6666 ui_out_field_string (uiout, "installed",
6667 loc->inserted ? "y" : "n");
6668 else
6669 {
6670 if (loc->inserted)
6671 ui_out_text (uiout, "\t");
6672 else
6673 ui_out_text (uiout, "\tnot ");
6674 ui_out_text (uiout, "installed on target\n");
6675 }
6676 }
6677 }
6678
6679 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
6680 {
6681 if (is_watchpoint (b))
6682 {
6683 struct watchpoint *w = (struct watchpoint *) b;
6684
6685 ui_out_field_string (uiout, "original-location", w->exp_string);
6686 }
6687 else if (b->location != NULL
6688 && event_location_to_string (b->location) != NULL)
6689 ui_out_field_string (uiout, "original-location",
6690 event_location_to_string (b->location));
6691 }
6692 }
6693
6694 static void
6695 print_one_breakpoint (struct breakpoint *b,
6696 struct bp_location **last_loc,
6697 int allflag)
6698 {
6699 struct cleanup *bkpt_chain;
6700 struct ui_out *uiout = current_uiout;
6701
6702 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
6703
6704 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
6705 do_cleanups (bkpt_chain);
6706
6707 /* If this breakpoint has custom print function,
6708 it's already printed. Otherwise, print individual
6709 locations, if any. */
6710 if (b->ops == NULL || b->ops->print_one == NULL)
6711 {
6712 /* If breakpoint has a single location that is disabled, we
6713 print it as if it had several locations, since otherwise it's
6714 hard to represent "breakpoint enabled, location disabled"
6715 situation.
6716
6717 Note that while hardware watchpoints have several locations
6718 internally, that's not a property exposed to user. */
6719 if (b->loc
6720 && !is_hardware_watchpoint (b)
6721 && (b->loc->next || !b->loc->enabled))
6722 {
6723 struct bp_location *loc;
6724 int n = 1;
6725
6726 for (loc = b->loc; loc; loc = loc->next, ++n)
6727 {
6728 struct cleanup *inner2 =
6729 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
6730 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
6731 do_cleanups (inner2);
6732 }
6733 }
6734 }
6735 }
6736
6737 static int
6738 breakpoint_address_bits (struct breakpoint *b)
6739 {
6740 int print_address_bits = 0;
6741 struct bp_location *loc;
6742
6743 /* Software watchpoints that aren't watching memory don't have an
6744 address to print. */
6745 if (is_no_memory_software_watchpoint (b))
6746 return 0;
6747
6748 for (loc = b->loc; loc; loc = loc->next)
6749 {
6750 int addr_bit;
6751
6752 addr_bit = gdbarch_addr_bit (loc->gdbarch);
6753 if (addr_bit > print_address_bits)
6754 print_address_bits = addr_bit;
6755 }
6756
6757 return print_address_bits;
6758 }
6759
6760 struct captured_breakpoint_query_args
6761 {
6762 int bnum;
6763 };
6764
6765 static int
6766 do_captured_breakpoint_query (struct ui_out *uiout, void *data)
6767 {
6768 struct captured_breakpoint_query_args *args
6769 = (struct captured_breakpoint_query_args *) data;
6770 struct breakpoint *b;
6771 struct bp_location *dummy_loc = NULL;
6772
6773 ALL_BREAKPOINTS (b)
6774 {
6775 if (args->bnum == b->number)
6776 {
6777 print_one_breakpoint (b, &dummy_loc, 0);
6778 return GDB_RC_OK;
6779 }
6780 }
6781 return GDB_RC_NONE;
6782 }
6783
6784 enum gdb_rc
6785 gdb_breakpoint_query (struct ui_out *uiout, int bnum,
6786 char **error_message)
6787 {
6788 struct captured_breakpoint_query_args args;
6789
6790 args.bnum = bnum;
6791 /* For the moment we don't trust print_one_breakpoint() to not throw
6792 an error. */
6793 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
6794 error_message, RETURN_MASK_ALL) < 0)
6795 return GDB_RC_FAIL;
6796 else
6797 return GDB_RC_OK;
6798 }
6799
6800 /* Return true if this breakpoint was set by the user, false if it is
6801 internal or momentary. */
6802
6803 int
6804 user_breakpoint_p (struct breakpoint *b)
6805 {
6806 return b->number > 0;
6807 }
6808
6809 /* See breakpoint.h. */
6810
6811 int
6812 pending_breakpoint_p (struct breakpoint *b)
6813 {
6814 return b->loc == NULL;
6815 }
6816
6817 /* Print information on user settable breakpoint (watchpoint, etc)
6818 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6819 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6820 FILTER is non-NULL, call it on each breakpoint and only include the
6821 ones for which it returns non-zero. Return the total number of
6822 breakpoints listed. */
6823
6824 static int
6825 breakpoint_1 (char *args, int allflag,
6826 int (*filter) (const struct breakpoint *))
6827 {
6828 struct breakpoint *b;
6829 struct bp_location *last_loc = NULL;
6830 int nr_printable_breakpoints;
6831 struct cleanup *bkpttbl_chain;
6832 struct value_print_options opts;
6833 int print_address_bits = 0;
6834 int print_type_col_width = 14;
6835 struct ui_out *uiout = current_uiout;
6836
6837 get_user_print_options (&opts);
6838
6839 /* Compute the number of rows in the table, as well as the size
6840 required for address fields. */
6841 nr_printable_breakpoints = 0;
6842 ALL_BREAKPOINTS (b)
6843 {
6844 /* If we have a filter, only list the breakpoints it accepts. */
6845 if (filter && !filter (b))
6846 continue;
6847
6848 /* If we have an "args" string, it is a list of breakpoints to
6849 accept. Skip the others. */
6850 if (args != NULL && *args != '\0')
6851 {
6852 if (allflag && parse_and_eval_long (args) != b->number)
6853 continue;
6854 if (!allflag && !number_is_in_list (args, b->number))
6855 continue;
6856 }
6857
6858 if (allflag || user_breakpoint_p (b))
6859 {
6860 int addr_bit, type_len;
6861
6862 addr_bit = breakpoint_address_bits (b);
6863 if (addr_bit > print_address_bits)
6864 print_address_bits = addr_bit;
6865
6866 type_len = strlen (bptype_string (b->type));
6867 if (type_len > print_type_col_width)
6868 print_type_col_width = type_len;
6869
6870 nr_printable_breakpoints++;
6871 }
6872 }
6873
6874 if (opts.addressprint)
6875 bkpttbl_chain
6876 = make_cleanup_ui_out_table_begin_end (uiout, 6,
6877 nr_printable_breakpoints,
6878 "BreakpointTable");
6879 else
6880 bkpttbl_chain
6881 = make_cleanup_ui_out_table_begin_end (uiout, 5,
6882 nr_printable_breakpoints,
6883 "BreakpointTable");
6884
6885 if (nr_printable_breakpoints > 0)
6886 annotate_breakpoints_headers ();
6887 if (nr_printable_breakpoints > 0)
6888 annotate_field (0);
6889 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
6890 if (nr_printable_breakpoints > 0)
6891 annotate_field (1);
6892 ui_out_table_header (uiout, print_type_col_width, ui_left,
6893 "type", "Type"); /* 2 */
6894 if (nr_printable_breakpoints > 0)
6895 annotate_field (2);
6896 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
6897 if (nr_printable_breakpoints > 0)
6898 annotate_field (3);
6899 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
6900 if (opts.addressprint)
6901 {
6902 if (nr_printable_breakpoints > 0)
6903 annotate_field (4);
6904 if (print_address_bits <= 32)
6905 ui_out_table_header (uiout, 10, ui_left,
6906 "addr", "Address"); /* 5 */
6907 else
6908 ui_out_table_header (uiout, 18, ui_left,
6909 "addr", "Address"); /* 5 */
6910 }
6911 if (nr_printable_breakpoints > 0)
6912 annotate_field (5);
6913 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
6914 ui_out_table_body (uiout);
6915 if (nr_printable_breakpoints > 0)
6916 annotate_breakpoints_table ();
6917
6918 ALL_BREAKPOINTS (b)
6919 {
6920 QUIT;
6921 /* If we have a filter, only list the breakpoints it accepts. */
6922 if (filter && !filter (b))
6923 continue;
6924
6925 /* If we have an "args" string, it is a list of breakpoints to
6926 accept. Skip the others. */
6927
6928 if (args != NULL && *args != '\0')
6929 {
6930 if (allflag) /* maintenance info breakpoint */
6931 {
6932 if (parse_and_eval_long (args) != b->number)
6933 continue;
6934 }
6935 else /* all others */
6936 {
6937 if (!number_is_in_list (args, b->number))
6938 continue;
6939 }
6940 }
6941 /* We only print out user settable breakpoints unless the
6942 allflag is set. */
6943 if (allflag || user_breakpoint_p (b))
6944 print_one_breakpoint (b, &last_loc, allflag);
6945 }
6946
6947 do_cleanups (bkpttbl_chain);
6948
6949 if (nr_printable_breakpoints == 0)
6950 {
6951 /* If there's a filter, let the caller decide how to report
6952 empty list. */
6953 if (!filter)
6954 {
6955 if (args == NULL || *args == '\0')
6956 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
6957 else
6958 ui_out_message (uiout, 0,
6959 "No breakpoint or watchpoint matching '%s'.\n",
6960 args);
6961 }
6962 }
6963 else
6964 {
6965 if (last_loc && !server_command)
6966 set_next_address (last_loc->gdbarch, last_loc->address);
6967 }
6968
6969 /* FIXME? Should this be moved up so that it is only called when
6970 there have been breakpoints? */
6971 annotate_breakpoints_table_end ();
6972
6973 return nr_printable_breakpoints;
6974 }
6975
6976 /* Display the value of default-collect in a way that is generally
6977 compatible with the breakpoint list. */
6978
6979 static void
6980 default_collect_info (void)
6981 {
6982 struct ui_out *uiout = current_uiout;
6983
6984 /* If it has no value (which is frequently the case), say nothing; a
6985 message like "No default-collect." gets in user's face when it's
6986 not wanted. */
6987 if (!*default_collect)
6988 return;
6989
6990 /* The following phrase lines up nicely with per-tracepoint collect
6991 actions. */
6992 ui_out_text (uiout, "default collect ");
6993 ui_out_field_string (uiout, "default-collect", default_collect);
6994 ui_out_text (uiout, " \n");
6995 }
6996
6997 static void
6998 breakpoints_info (char *args, int from_tty)
6999 {
7000 breakpoint_1 (args, 0, NULL);
7001
7002 default_collect_info ();
7003 }
7004
7005 static void
7006 watchpoints_info (char *args, int from_tty)
7007 {
7008 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
7009 struct ui_out *uiout = current_uiout;
7010
7011 if (num_printed == 0)
7012 {
7013 if (args == NULL || *args == '\0')
7014 ui_out_message (uiout, 0, "No watchpoints.\n");
7015 else
7016 ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
7017 }
7018 }
7019
7020 static void
7021 maintenance_info_breakpoints (char *args, int from_tty)
7022 {
7023 breakpoint_1 (args, 1, NULL);
7024
7025 default_collect_info ();
7026 }
7027
7028 static int
7029 breakpoint_has_pc (struct breakpoint *b,
7030 struct program_space *pspace,
7031 CORE_ADDR pc, struct obj_section *section)
7032 {
7033 struct bp_location *bl = b->loc;
7034
7035 for (; bl; bl = bl->next)
7036 {
7037 if (bl->pspace == pspace
7038 && bl->address == pc
7039 && (!overlay_debugging || bl->section == section))
7040 return 1;
7041 }
7042 return 0;
7043 }
7044
7045 /* Print a message describing any user-breakpoints set at PC. This
7046 concerns with logical breakpoints, so we match program spaces, not
7047 address spaces. */
7048
7049 static void
7050 describe_other_breakpoints (struct gdbarch *gdbarch,
7051 struct program_space *pspace, CORE_ADDR pc,
7052 struct obj_section *section, int thread)
7053 {
7054 int others = 0;
7055 struct breakpoint *b;
7056
7057 ALL_BREAKPOINTS (b)
7058 others += (user_breakpoint_p (b)
7059 && breakpoint_has_pc (b, pspace, pc, section));
7060 if (others > 0)
7061 {
7062 if (others == 1)
7063 printf_filtered (_("Note: breakpoint "));
7064 else /* if (others == ???) */
7065 printf_filtered (_("Note: breakpoints "));
7066 ALL_BREAKPOINTS (b)
7067 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
7068 {
7069 others--;
7070 printf_filtered ("%d", b->number);
7071 if (b->thread == -1 && thread != -1)
7072 printf_filtered (" (all threads)");
7073 else if (b->thread != -1)
7074 printf_filtered (" (thread %d)", b->thread);
7075 printf_filtered ("%s%s ",
7076 ((b->enable_state == bp_disabled
7077 || b->enable_state == bp_call_disabled)
7078 ? " (disabled)"
7079 : ""),
7080 (others > 1) ? ","
7081 : ((others == 1) ? " and" : ""));
7082 }
7083 printf_filtered (_("also set at pc "));
7084 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
7085 printf_filtered (".\n");
7086 }
7087 }
7088 \f
7089
7090 /* Return true iff it is meaningful to use the address member of
7091 BPT locations. For some breakpoint types, the locations' address members
7092 are irrelevant and it makes no sense to attempt to compare them to other
7093 addresses (or use them for any other purpose either).
7094
7095 More specifically, each of the following breakpoint types will
7096 always have a zero valued location address and we don't want to mark
7097 breakpoints of any of these types to be a duplicate of an actual
7098 breakpoint location at address zero:
7099
7100 bp_watchpoint
7101 bp_catchpoint
7102
7103 */
7104
7105 static int
7106 breakpoint_address_is_meaningful (struct breakpoint *bpt)
7107 {
7108 enum bptype type = bpt->type;
7109
7110 return (type != bp_watchpoint && type != bp_catchpoint);
7111 }
7112
7113 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
7114 true if LOC1 and LOC2 represent the same watchpoint location. */
7115
7116 static int
7117 watchpoint_locations_match (struct bp_location *loc1,
7118 struct bp_location *loc2)
7119 {
7120 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
7121 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
7122
7123 /* Both of them must exist. */
7124 gdb_assert (w1 != NULL);
7125 gdb_assert (w2 != NULL);
7126
7127 /* If the target can evaluate the condition expression in hardware,
7128 then we we need to insert both watchpoints even if they are at
7129 the same place. Otherwise the watchpoint will only trigger when
7130 the condition of whichever watchpoint was inserted evaluates to
7131 true, not giving a chance for GDB to check the condition of the
7132 other watchpoint. */
7133 if ((w1->cond_exp
7134 && target_can_accel_watchpoint_condition (loc1->address,
7135 loc1->length,
7136 loc1->watchpoint_type,
7137 w1->cond_exp))
7138 || (w2->cond_exp
7139 && target_can_accel_watchpoint_condition (loc2->address,
7140 loc2->length,
7141 loc2->watchpoint_type,
7142 w2->cond_exp)))
7143 return 0;
7144
7145 /* Note that this checks the owner's type, not the location's. In
7146 case the target does not support read watchpoints, but does
7147 support access watchpoints, we'll have bp_read_watchpoint
7148 watchpoints with hw_access locations. Those should be considered
7149 duplicates of hw_read locations. The hw_read locations will
7150 become hw_access locations later. */
7151 return (loc1->owner->type == loc2->owner->type
7152 && loc1->pspace->aspace == loc2->pspace->aspace
7153 && loc1->address == loc2->address
7154 && loc1->length == loc2->length);
7155 }
7156
7157 /* See breakpoint.h. */
7158
7159 int
7160 breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
7161 struct address_space *aspace2, CORE_ADDR addr2)
7162 {
7163 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
7164 || aspace1 == aspace2)
7165 && addr1 == addr2);
7166 }
7167
7168 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
7169 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
7170 matches ASPACE2. On targets that have global breakpoints, the address
7171 space doesn't really matter. */
7172
7173 static int
7174 breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
7175 int len1, struct address_space *aspace2,
7176 CORE_ADDR addr2)
7177 {
7178 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
7179 || aspace1 == aspace2)
7180 && addr2 >= addr1 && addr2 < addr1 + len1);
7181 }
7182
7183 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
7184 a ranged breakpoint. In most targets, a match happens only if ASPACE
7185 matches the breakpoint's address space. On targets that have global
7186 breakpoints, the address space doesn't really matter. */
7187
7188 static int
7189 breakpoint_location_address_match (struct bp_location *bl,
7190 struct address_space *aspace,
7191 CORE_ADDR addr)
7192 {
7193 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
7194 aspace, addr)
7195 || (bl->length
7196 && breakpoint_address_match_range (bl->pspace->aspace,
7197 bl->address, bl->length,
7198 aspace, addr)));
7199 }
7200
7201 /* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
7202 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
7203 match happens only if ASPACE matches the breakpoint's address
7204 space. On targets that have global breakpoints, the address space
7205 doesn't really matter. */
7206
7207 static int
7208 breakpoint_location_address_range_overlap (struct bp_location *bl,
7209 struct address_space *aspace,
7210 CORE_ADDR addr, int len)
7211 {
7212 if (gdbarch_has_global_breakpoints (target_gdbarch ())
7213 || bl->pspace->aspace == aspace)
7214 {
7215 int bl_len = bl->length != 0 ? bl->length : 1;
7216
7217 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
7218 return 1;
7219 }
7220 return 0;
7221 }
7222
7223 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
7224 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
7225 true, otherwise returns false. */
7226
7227 static int
7228 tracepoint_locations_match (struct bp_location *loc1,
7229 struct bp_location *loc2)
7230 {
7231 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
7232 /* Since tracepoint locations are never duplicated with others', tracepoint
7233 locations at the same address of different tracepoints are regarded as
7234 different locations. */
7235 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7236 else
7237 return 0;
7238 }
7239
7240 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
7241 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
7242 represent the same location. */
7243
7244 static int
7245 breakpoint_locations_match (struct bp_location *loc1,
7246 struct bp_location *loc2)
7247 {
7248 int hw_point1, hw_point2;
7249
7250 /* Both of them must not be in moribund_locations. */
7251 gdb_assert (loc1->owner != NULL);
7252 gdb_assert (loc2->owner != NULL);
7253
7254 hw_point1 = is_hardware_watchpoint (loc1->owner);
7255 hw_point2 = is_hardware_watchpoint (loc2->owner);
7256
7257 if (hw_point1 != hw_point2)
7258 return 0;
7259 else if (hw_point1)
7260 return watchpoint_locations_match (loc1, loc2);
7261 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7262 return tracepoint_locations_match (loc1, loc2);
7263 else
7264 /* We compare bp_location.length in order to cover ranged breakpoints. */
7265 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7266 loc2->pspace->aspace, loc2->address)
7267 && loc1->length == loc2->length);
7268 }
7269
7270 static void
7271 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7272 int bnum, int have_bnum)
7273 {
7274 /* The longest string possibly returned by hex_string_custom
7275 is 50 chars. These must be at least that big for safety. */
7276 char astr1[64];
7277 char astr2[64];
7278
7279 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7280 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
7281 if (have_bnum)
7282 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
7283 bnum, astr1, astr2);
7284 else
7285 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
7286 }
7287
7288 /* Adjust a breakpoint's address to account for architectural
7289 constraints on breakpoint placement. Return the adjusted address.
7290 Note: Very few targets require this kind of adjustment. For most
7291 targets, this function is simply the identity function. */
7292
7293 static CORE_ADDR
7294 adjust_breakpoint_address (struct gdbarch *gdbarch,
7295 CORE_ADDR bpaddr, enum bptype bptype)
7296 {
7297 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
7298 {
7299 /* Very few targets need any kind of breakpoint adjustment. */
7300 return bpaddr;
7301 }
7302 else if (bptype == bp_watchpoint
7303 || bptype == bp_hardware_watchpoint
7304 || bptype == bp_read_watchpoint
7305 || bptype == bp_access_watchpoint
7306 || bptype == bp_catchpoint)
7307 {
7308 /* Watchpoints and the various bp_catch_* eventpoints should not
7309 have their addresses modified. */
7310 return bpaddr;
7311 }
7312 else if (bptype == bp_single_step)
7313 {
7314 /* Single-step breakpoints should not have their addresses
7315 modified. If there's any architectural constrain that
7316 applies to this address, then it should have already been
7317 taken into account when the breakpoint was created in the
7318 first place. If we didn't do this, stepping through e.g.,
7319 Thumb-2 IT blocks would break. */
7320 return bpaddr;
7321 }
7322 else
7323 {
7324 CORE_ADDR adjusted_bpaddr;
7325
7326 /* Some targets have architectural constraints on the placement
7327 of breakpoint instructions. Obtain the adjusted address. */
7328 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
7329
7330 /* An adjusted breakpoint address can significantly alter
7331 a user's expectations. Print a warning if an adjustment
7332 is required. */
7333 if (adjusted_bpaddr != bpaddr)
7334 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7335
7336 return adjusted_bpaddr;
7337 }
7338 }
7339
7340 void
7341 init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
7342 struct breakpoint *owner)
7343 {
7344 memset (loc, 0, sizeof (*loc));
7345
7346 gdb_assert (ops != NULL);
7347
7348 loc->ops = ops;
7349 loc->owner = owner;
7350 loc->cond = NULL;
7351 loc->cond_bytecode = NULL;
7352 loc->shlib_disabled = 0;
7353 loc->enabled = 1;
7354
7355 switch (owner->type)
7356 {
7357 case bp_breakpoint:
7358 case bp_single_step:
7359 case bp_until:
7360 case bp_finish:
7361 case bp_longjmp:
7362 case bp_longjmp_resume:
7363 case bp_longjmp_call_dummy:
7364 case bp_exception:
7365 case bp_exception_resume:
7366 case bp_step_resume:
7367 case bp_hp_step_resume:
7368 case bp_watchpoint_scope:
7369 case bp_call_dummy:
7370 case bp_std_terminate:
7371 case bp_shlib_event:
7372 case bp_thread_event:
7373 case bp_overlay_event:
7374 case bp_jit_event:
7375 case bp_longjmp_master:
7376 case bp_std_terminate_master:
7377 case bp_exception_master:
7378 case bp_gnu_ifunc_resolver:
7379 case bp_gnu_ifunc_resolver_return:
7380 case bp_dprintf:
7381 loc->loc_type = bp_loc_software_breakpoint;
7382 mark_breakpoint_location_modified (loc);
7383 break;
7384 case bp_hardware_breakpoint:
7385 loc->loc_type = bp_loc_hardware_breakpoint;
7386 mark_breakpoint_location_modified (loc);
7387 break;
7388 case bp_hardware_watchpoint:
7389 case bp_read_watchpoint:
7390 case bp_access_watchpoint:
7391 loc->loc_type = bp_loc_hardware_watchpoint;
7392 break;
7393 case bp_watchpoint:
7394 case bp_catchpoint:
7395 case bp_tracepoint:
7396 case bp_fast_tracepoint:
7397 case bp_static_tracepoint:
7398 loc->loc_type = bp_loc_other;
7399 break;
7400 default:
7401 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
7402 }
7403
7404 loc->refc = 1;
7405 }
7406
7407 /* Allocate a struct bp_location. */
7408
7409 static struct bp_location *
7410 allocate_bp_location (struct breakpoint *bpt)
7411 {
7412 return bpt->ops->allocate_location (bpt);
7413 }
7414
7415 static void
7416 free_bp_location (struct bp_location *loc)
7417 {
7418 loc->ops->dtor (loc);
7419 xfree (loc);
7420 }
7421
7422 /* Increment reference count. */
7423
7424 static void
7425 incref_bp_location (struct bp_location *bl)
7426 {
7427 ++bl->refc;
7428 }
7429
7430 /* Decrement reference count. If the reference count reaches 0,
7431 destroy the bp_location. Sets *BLP to NULL. */
7432
7433 static void
7434 decref_bp_location (struct bp_location **blp)
7435 {
7436 gdb_assert ((*blp)->refc > 0);
7437
7438 if (--(*blp)->refc == 0)
7439 free_bp_location (*blp);
7440 *blp = NULL;
7441 }
7442
7443 /* Add breakpoint B at the end of the global breakpoint chain. */
7444
7445 static void
7446 add_to_breakpoint_chain (struct breakpoint *b)
7447 {
7448 struct breakpoint *b1;
7449
7450 /* Add this breakpoint to the end of the chain so that a list of
7451 breakpoints will come out in order of increasing numbers. */
7452
7453 b1 = breakpoint_chain;
7454 if (b1 == 0)
7455 breakpoint_chain = b;
7456 else
7457 {
7458 while (b1->next)
7459 b1 = b1->next;
7460 b1->next = b;
7461 }
7462 }
7463
7464 /* Initializes breakpoint B with type BPTYPE and no locations yet. */
7465
7466 static void
7467 init_raw_breakpoint_without_location (struct breakpoint *b,
7468 struct gdbarch *gdbarch,
7469 enum bptype bptype,
7470 const struct breakpoint_ops *ops)
7471 {
7472 memset (b, 0, sizeof (*b));
7473
7474 gdb_assert (ops != NULL);
7475
7476 b->ops = ops;
7477 b->type = bptype;
7478 b->gdbarch = gdbarch;
7479 b->language = current_language->la_language;
7480 b->input_radix = input_radix;
7481 b->thread = -1;
7482 b->enable_state = bp_enabled;
7483 b->next = 0;
7484 b->silent = 0;
7485 b->ignore_count = 0;
7486 b->commands = NULL;
7487 b->frame_id = null_frame_id;
7488 b->condition_not_parsed = 0;
7489 b->py_bp_object = NULL;
7490 b->related_breakpoint = b;
7491 b->location = NULL;
7492 }
7493
7494 /* Helper to set_raw_breakpoint below. Creates a breakpoint
7495 that has type BPTYPE and has no locations as yet. */
7496
7497 static struct breakpoint *
7498 set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
7499 enum bptype bptype,
7500 const struct breakpoint_ops *ops)
7501 {
7502 struct breakpoint *b = XNEW (struct breakpoint);
7503
7504 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7505 add_to_breakpoint_chain (b);
7506 return b;
7507 }
7508
7509 /* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7510 resolutions should be made as the user specified the location explicitly
7511 enough. */
7512
7513 static void
7514 set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
7515 {
7516 gdb_assert (loc->owner != NULL);
7517
7518 if (loc->owner->type == bp_breakpoint
7519 || loc->owner->type == bp_hardware_breakpoint
7520 || is_tracepoint (loc->owner))
7521 {
7522 int is_gnu_ifunc;
7523 const char *function_name;
7524 CORE_ADDR func_addr;
7525
7526 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
7527 &func_addr, NULL, &is_gnu_ifunc);
7528
7529 if (is_gnu_ifunc && !explicit_loc)
7530 {
7531 struct breakpoint *b = loc->owner;
7532
7533 gdb_assert (loc->pspace == current_program_space);
7534 if (gnu_ifunc_resolve_name (function_name,
7535 &loc->requested_address))
7536 {
7537 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7538 loc->address = adjust_breakpoint_address (loc->gdbarch,
7539 loc->requested_address,
7540 b->type);
7541 }
7542 else if (b->type == bp_breakpoint && b->loc == loc
7543 && loc->next == NULL && b->related_breakpoint == b)
7544 {
7545 /* Create only the whole new breakpoint of this type but do not
7546 mess more complicated breakpoints with multiple locations. */
7547 b->type = bp_gnu_ifunc_resolver;
7548 /* Remember the resolver's address for use by the return
7549 breakpoint. */
7550 loc->related_address = func_addr;
7551 }
7552 }
7553
7554 if (function_name)
7555 loc->function_name = xstrdup (function_name);
7556 }
7557 }
7558
7559 /* Attempt to determine architecture of location identified by SAL. */
7560 struct gdbarch *
7561 get_sal_arch (struct symtab_and_line sal)
7562 {
7563 if (sal.section)
7564 return get_objfile_arch (sal.section->objfile);
7565 if (sal.symtab)
7566 return get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
7567
7568 return NULL;
7569 }
7570
7571 /* Low level routine for partially initializing a breakpoint of type
7572 BPTYPE. The newly created breakpoint's address, section, source
7573 file name, and line number are provided by SAL.
7574
7575 It is expected that the caller will complete the initialization of
7576 the newly created breakpoint struct as well as output any status
7577 information regarding the creation of a new breakpoint. */
7578
7579 static void
7580 init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
7581 struct symtab_and_line sal, enum bptype bptype,
7582 const struct breakpoint_ops *ops)
7583 {
7584 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7585
7586 add_location_to_breakpoint (b, &sal);
7587
7588 if (bptype != bp_catchpoint)
7589 gdb_assert (sal.pspace != NULL);
7590
7591 /* Store the program space that was used to set the breakpoint,
7592 except for ordinary breakpoints, which are independent of the
7593 program space. */
7594 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7595 b->pspace = sal.pspace;
7596 }
7597
7598 /* set_raw_breakpoint is a low level routine for allocating and
7599 partially initializing a breakpoint of type BPTYPE. The newly
7600 created breakpoint's address, section, source file name, and line
7601 number are provided by SAL. The newly created and partially
7602 initialized breakpoint is added to the breakpoint chain and
7603 is also returned as the value of this function.
7604
7605 It is expected that the caller will complete the initialization of
7606 the newly created breakpoint struct as well as output any status
7607 information regarding the creation of a new breakpoint. In
7608 particular, set_raw_breakpoint does NOT set the breakpoint
7609 number! Care should be taken to not allow an error to occur
7610 prior to completing the initialization of the breakpoint. If this
7611 should happen, a bogus breakpoint will be left on the chain. */
7612
7613 struct breakpoint *
7614 set_raw_breakpoint (struct gdbarch *gdbarch,
7615 struct symtab_and_line sal, enum bptype bptype,
7616 const struct breakpoint_ops *ops)
7617 {
7618 struct breakpoint *b = XNEW (struct breakpoint);
7619
7620 init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
7621 add_to_breakpoint_chain (b);
7622 return b;
7623 }
7624
7625 /* Call this routine when stepping and nexting to enable a breakpoint
7626 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7627 initiated the operation. */
7628
7629 void
7630 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
7631 {
7632 struct breakpoint *b, *b_tmp;
7633 int thread = tp->global_num;
7634
7635 /* To avoid having to rescan all objfile symbols at every step,
7636 we maintain a list of continually-inserted but always disabled
7637 longjmp "master" breakpoints. Here, we simply create momentary
7638 clones of those and enable them for the requested thread. */
7639 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7640 if (b->pspace == current_program_space
7641 && (b->type == bp_longjmp_master
7642 || b->type == bp_exception_master))
7643 {
7644 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7645 struct breakpoint *clone;
7646
7647 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7648 after their removal. */
7649 clone = momentary_breakpoint_from_master (b, type,
7650 &longjmp_breakpoint_ops, 1);
7651 clone->thread = thread;
7652 }
7653
7654 tp->initiating_frame = frame;
7655 }
7656
7657 /* Delete all longjmp breakpoints from THREAD. */
7658 void
7659 delete_longjmp_breakpoint (int thread)
7660 {
7661 struct breakpoint *b, *b_tmp;
7662
7663 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7664 if (b->type == bp_longjmp || b->type == bp_exception)
7665 {
7666 if (b->thread == thread)
7667 delete_breakpoint (b);
7668 }
7669 }
7670
7671 void
7672 delete_longjmp_breakpoint_at_next_stop (int thread)
7673 {
7674 struct breakpoint *b, *b_tmp;
7675
7676 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7677 if (b->type == bp_longjmp || b->type == bp_exception)
7678 {
7679 if (b->thread == thread)
7680 b->disposition = disp_del_at_next_stop;
7681 }
7682 }
7683
7684 /* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7685 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7686 pointer to any of them. Return NULL if this system cannot place longjmp
7687 breakpoints. */
7688
7689 struct breakpoint *
7690 set_longjmp_breakpoint_for_call_dummy (void)
7691 {
7692 struct breakpoint *b, *retval = NULL;
7693
7694 ALL_BREAKPOINTS (b)
7695 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7696 {
7697 struct breakpoint *new_b;
7698
7699 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7700 &momentary_breakpoint_ops,
7701 1);
7702 new_b->thread = ptid_to_global_thread_id (inferior_ptid);
7703
7704 /* Link NEW_B into the chain of RETVAL breakpoints. */
7705
7706 gdb_assert (new_b->related_breakpoint == new_b);
7707 if (retval == NULL)
7708 retval = new_b;
7709 new_b->related_breakpoint = retval;
7710 while (retval->related_breakpoint != new_b->related_breakpoint)
7711 retval = retval->related_breakpoint;
7712 retval->related_breakpoint = new_b;
7713 }
7714
7715 return retval;
7716 }
7717
7718 /* Verify all existing dummy frames and their associated breakpoints for
7719 TP. Remove those which can no longer be found in the current frame
7720 stack.
7721
7722 You should call this function only at places where it is safe to currently
7723 unwind the whole stack. Failed stack unwind would discard live dummy
7724 frames. */
7725
7726 void
7727 check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
7728 {
7729 struct breakpoint *b, *b_tmp;
7730
7731 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7732 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
7733 {
7734 struct breakpoint *dummy_b = b->related_breakpoint;
7735
7736 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7737 dummy_b = dummy_b->related_breakpoint;
7738 if (dummy_b->type != bp_call_dummy
7739 || frame_find_by_id (dummy_b->frame_id) != NULL)
7740 continue;
7741
7742 dummy_frame_discard (dummy_b->frame_id, tp->ptid);
7743
7744 while (b->related_breakpoint != b)
7745 {
7746 if (b_tmp == b->related_breakpoint)
7747 b_tmp = b->related_breakpoint->next;
7748 delete_breakpoint (b->related_breakpoint);
7749 }
7750 delete_breakpoint (b);
7751 }
7752 }
7753
7754 void
7755 enable_overlay_breakpoints (void)
7756 {
7757 struct breakpoint *b;
7758
7759 ALL_BREAKPOINTS (b)
7760 if (b->type == bp_overlay_event)
7761 {
7762 b->enable_state = bp_enabled;
7763 update_global_location_list (UGLL_MAY_INSERT);
7764 overlay_events_enabled = 1;
7765 }
7766 }
7767
7768 void
7769 disable_overlay_breakpoints (void)
7770 {
7771 struct breakpoint *b;
7772
7773 ALL_BREAKPOINTS (b)
7774 if (b->type == bp_overlay_event)
7775 {
7776 b->enable_state = bp_disabled;
7777 update_global_location_list (UGLL_DONT_INSERT);
7778 overlay_events_enabled = 0;
7779 }
7780 }
7781
7782 /* Set an active std::terminate breakpoint for each std::terminate
7783 master breakpoint. */
7784 void
7785 set_std_terminate_breakpoint (void)
7786 {
7787 struct breakpoint *b, *b_tmp;
7788
7789 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7790 if (b->pspace == current_program_space
7791 && b->type == bp_std_terminate_master)
7792 {
7793 momentary_breakpoint_from_master (b, bp_std_terminate,
7794 &momentary_breakpoint_ops, 1);
7795 }
7796 }
7797
7798 /* Delete all the std::terminate breakpoints. */
7799 void
7800 delete_std_terminate_breakpoint (void)
7801 {
7802 struct breakpoint *b, *b_tmp;
7803
7804 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7805 if (b->type == bp_std_terminate)
7806 delete_breakpoint (b);
7807 }
7808
7809 struct breakpoint *
7810 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7811 {
7812 struct breakpoint *b;
7813
7814 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7815 &internal_breakpoint_ops);
7816
7817 b->enable_state = bp_enabled;
7818 /* location has to be used or breakpoint_re_set will delete me. */
7819 b->location = new_address_location (b->loc->address, NULL, 0);
7820
7821 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7822
7823 return b;
7824 }
7825
7826 struct lang_and_radix
7827 {
7828 enum language lang;
7829 int radix;
7830 };
7831
7832 /* Create a breakpoint for JIT code registration and unregistration. */
7833
7834 struct breakpoint *
7835 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7836 {
7837 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7838 &internal_breakpoint_ops);
7839 }
7840
7841 /* Remove JIT code registration and unregistration breakpoint(s). */
7842
7843 void
7844 remove_jit_event_breakpoints (void)
7845 {
7846 struct breakpoint *b, *b_tmp;
7847
7848 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7849 if (b->type == bp_jit_event
7850 && b->loc->pspace == current_program_space)
7851 delete_breakpoint (b);
7852 }
7853
7854 void
7855 remove_solib_event_breakpoints (void)
7856 {
7857 struct breakpoint *b, *b_tmp;
7858
7859 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7860 if (b->type == bp_shlib_event
7861 && b->loc->pspace == current_program_space)
7862 delete_breakpoint (b);
7863 }
7864
7865 /* See breakpoint.h. */
7866
7867 void
7868 remove_solib_event_breakpoints_at_next_stop (void)
7869 {
7870 struct breakpoint *b, *b_tmp;
7871
7872 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7873 if (b->type == bp_shlib_event
7874 && b->loc->pspace == current_program_space)
7875 b->disposition = disp_del_at_next_stop;
7876 }
7877
7878 /* Helper for create_solib_event_breakpoint /
7879 create_and_insert_solib_event_breakpoint. Allows specifying which
7880 INSERT_MODE to pass through to update_global_location_list. */
7881
7882 static struct breakpoint *
7883 create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7884 enum ugll_insert_mode insert_mode)
7885 {
7886 struct breakpoint *b;
7887
7888 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7889 &internal_breakpoint_ops);
7890 update_global_location_list_nothrow (insert_mode);
7891 return b;
7892 }
7893
7894 struct breakpoint *
7895 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7896 {
7897 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7898 }
7899
7900 /* See breakpoint.h. */
7901
7902 struct breakpoint *
7903 create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7904 {
7905 struct breakpoint *b;
7906
7907 /* Explicitly tell update_global_location_list to insert
7908 locations. */
7909 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
7910 if (!b->loc->inserted)
7911 {
7912 delete_breakpoint (b);
7913 return NULL;
7914 }
7915 return b;
7916 }
7917
7918 /* Disable any breakpoints that are on code in shared libraries. Only
7919 apply to enabled breakpoints, disabled ones can just stay disabled. */
7920
7921 void
7922 disable_breakpoints_in_shlibs (void)
7923 {
7924 struct bp_location *loc, **locp_tmp;
7925
7926 ALL_BP_LOCATIONS (loc, locp_tmp)
7927 {
7928 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7929 struct breakpoint *b = loc->owner;
7930
7931 /* We apply the check to all breakpoints, including disabled for
7932 those with loc->duplicate set. This is so that when breakpoint
7933 becomes enabled, or the duplicate is removed, gdb will try to
7934 insert all breakpoints. If we don't set shlib_disabled here,
7935 we'll try to insert those breakpoints and fail. */
7936 if (((b->type == bp_breakpoint)
7937 || (b->type == bp_jit_event)
7938 || (b->type == bp_hardware_breakpoint)
7939 || (is_tracepoint (b)))
7940 && loc->pspace == current_program_space
7941 && !loc->shlib_disabled
7942 && solib_name_from_address (loc->pspace, loc->address)
7943 )
7944 {
7945 loc->shlib_disabled = 1;
7946 }
7947 }
7948 }
7949
7950 /* Disable any breakpoints and tracepoints that are in SOLIB upon
7951 notification of unloaded_shlib. Only apply to enabled breakpoints,
7952 disabled ones can just stay disabled. */
7953
7954 static void
7955 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7956 {
7957 struct bp_location *loc, **locp_tmp;
7958 int disabled_shlib_breaks = 0;
7959
7960 /* SunOS a.out shared libraries are always mapped, so do not
7961 disable breakpoints; they will only be reported as unloaded
7962 through clear_solib when GDB discards its shared library
7963 list. See clear_solib for more information. */
7964 if (exec_bfd != NULL
7965 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
7966 return;
7967
7968 ALL_BP_LOCATIONS (loc, locp_tmp)
7969 {
7970 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7971 struct breakpoint *b = loc->owner;
7972
7973 if (solib->pspace == loc->pspace
7974 && !loc->shlib_disabled
7975 && (((b->type == bp_breakpoint
7976 || b->type == bp_jit_event
7977 || b->type == bp_hardware_breakpoint)
7978 && (loc->loc_type == bp_loc_hardware_breakpoint
7979 || loc->loc_type == bp_loc_software_breakpoint))
7980 || is_tracepoint (b))
7981 && solib_contains_address_p (solib, loc->address))
7982 {
7983 loc->shlib_disabled = 1;
7984 /* At this point, we cannot rely on remove_breakpoint
7985 succeeding so we must mark the breakpoint as not inserted
7986 to prevent future errors occurring in remove_breakpoints. */
7987 loc->inserted = 0;
7988
7989 /* This may cause duplicate notifications for the same breakpoint. */
7990 observer_notify_breakpoint_modified (b);
7991
7992 if (!disabled_shlib_breaks)
7993 {
7994 target_terminal_ours_for_output ();
7995 warning (_("Temporarily disabling breakpoints "
7996 "for unloaded shared library \"%s\""),
7997 solib->so_name);
7998 }
7999 disabled_shlib_breaks = 1;
8000 }
8001 }
8002 }
8003
8004 /* Disable any breakpoints and tracepoints in OBJFILE upon
8005 notification of free_objfile. Only apply to enabled breakpoints,
8006 disabled ones can just stay disabled. */
8007
8008 static void
8009 disable_breakpoints_in_freed_objfile (struct objfile *objfile)
8010 {
8011 struct breakpoint *b;
8012
8013 if (objfile == NULL)
8014 return;
8015
8016 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
8017 managed by the user with add-symbol-file/remove-symbol-file.
8018 Similarly to how breakpoints in shared libraries are handled in
8019 response to "nosharedlibrary", mark breakpoints in such modules
8020 shlib_disabled so they end up uninserted on the next global
8021 location list update. Shared libraries not loaded by the user
8022 aren't handled here -- they're already handled in
8023 disable_breakpoints_in_unloaded_shlib, called by solib.c's
8024 solib_unloaded observer. We skip objfiles that are not
8025 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
8026 main objfile). */
8027 if ((objfile->flags & OBJF_SHARED) == 0
8028 || (objfile->flags & OBJF_USERLOADED) == 0)
8029 return;
8030
8031 ALL_BREAKPOINTS (b)
8032 {
8033 struct bp_location *loc;
8034 int bp_modified = 0;
8035
8036 if (!is_breakpoint (b) && !is_tracepoint (b))
8037 continue;
8038
8039 for (loc = b->loc; loc != NULL; loc = loc->next)
8040 {
8041 CORE_ADDR loc_addr = loc->address;
8042
8043 if (loc->loc_type != bp_loc_hardware_breakpoint
8044 && loc->loc_type != bp_loc_software_breakpoint)
8045 continue;
8046
8047 if (loc->shlib_disabled != 0)
8048 continue;
8049
8050 if (objfile->pspace != loc->pspace)
8051 continue;
8052
8053 if (loc->loc_type != bp_loc_hardware_breakpoint
8054 && loc->loc_type != bp_loc_software_breakpoint)
8055 continue;
8056
8057 if (is_addr_in_objfile (loc_addr, objfile))
8058 {
8059 loc->shlib_disabled = 1;
8060 /* At this point, we don't know whether the object was
8061 unmapped from the inferior or not, so leave the
8062 inserted flag alone. We'll handle failure to
8063 uninsert quietly, in case the object was indeed
8064 unmapped. */
8065
8066 mark_breakpoint_location_modified (loc);
8067
8068 bp_modified = 1;
8069 }
8070 }
8071
8072 if (bp_modified)
8073 observer_notify_breakpoint_modified (b);
8074 }
8075 }
8076
8077 /* FORK & VFORK catchpoints. */
8078
8079 /* An instance of this type is used to represent a fork or vfork
8080 catchpoint. It includes a "struct breakpoint" as a kind of base
8081 class; users downcast to "struct breakpoint *" when needed. A
8082 breakpoint is really of this type iff its ops pointer points to
8083 CATCH_FORK_BREAKPOINT_OPS. */
8084
8085 struct fork_catchpoint
8086 {
8087 /* The base class. */
8088 struct breakpoint base;
8089
8090 /* Process id of a child process whose forking triggered this
8091 catchpoint. This field is only valid immediately after this
8092 catchpoint has triggered. */
8093 ptid_t forked_inferior_pid;
8094 };
8095
8096 /* Implement the "insert" breakpoint_ops method for fork
8097 catchpoints. */
8098
8099 static int
8100 insert_catch_fork (struct bp_location *bl)
8101 {
8102 return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
8103 }
8104
8105 /* Implement the "remove" breakpoint_ops method for fork
8106 catchpoints. */
8107
8108 static int
8109 remove_catch_fork (struct bp_location *bl, enum remove_bp_reason reason)
8110 {
8111 return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
8112 }
8113
8114 /* Implement the "breakpoint_hit" breakpoint_ops method for fork
8115 catchpoints. */
8116
8117 static int
8118 breakpoint_hit_catch_fork (const struct bp_location *bl,
8119 struct address_space *aspace, CORE_ADDR bp_addr,
8120 const struct target_waitstatus *ws)
8121 {
8122 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8123
8124 if (ws->kind != TARGET_WAITKIND_FORKED)
8125 return 0;
8126
8127 c->forked_inferior_pid = ws->value.related_pid;
8128 return 1;
8129 }
8130
8131 /* Implement the "print_it" breakpoint_ops method for fork
8132 catchpoints. */
8133
8134 static enum print_stop_action
8135 print_it_catch_fork (bpstat bs)
8136 {
8137 struct ui_out *uiout = current_uiout;
8138 struct breakpoint *b = bs->breakpoint_at;
8139 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
8140
8141 annotate_catchpoint (b->number);
8142 maybe_print_thread_hit_breakpoint (uiout);
8143 if (b->disposition == disp_del)
8144 ui_out_text (uiout, "Temporary catchpoint ");
8145 else
8146 ui_out_text (uiout, "Catchpoint ");
8147 if (ui_out_is_mi_like_p (uiout))
8148 {
8149 ui_out_field_string (uiout, "reason",
8150 async_reason_lookup (EXEC_ASYNC_FORK));
8151 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8152 }
8153 ui_out_field_int (uiout, "bkptno", b->number);
8154 ui_out_text (uiout, " (forked process ");
8155 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
8156 ui_out_text (uiout, "), ");
8157 return PRINT_SRC_AND_LOC;
8158 }
8159
8160 /* Implement the "print_one" breakpoint_ops method for fork
8161 catchpoints. */
8162
8163 static void
8164 print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
8165 {
8166 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8167 struct value_print_options opts;
8168 struct ui_out *uiout = current_uiout;
8169
8170 get_user_print_options (&opts);
8171
8172 /* Field 4, the address, is omitted (which makes the columns not
8173 line up too nicely with the headers, but the effect is relatively
8174 readable). */
8175 if (opts.addressprint)
8176 ui_out_field_skip (uiout, "addr");
8177 annotate_field (5);
8178 ui_out_text (uiout, "fork");
8179 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
8180 {
8181 ui_out_text (uiout, ", process ");
8182 ui_out_field_int (uiout, "what",
8183 ptid_get_pid (c->forked_inferior_pid));
8184 ui_out_spaces (uiout, 1);
8185 }
8186
8187 if (ui_out_is_mi_like_p (uiout))
8188 ui_out_field_string (uiout, "catch-type", "fork");
8189 }
8190
8191 /* Implement the "print_mention" breakpoint_ops method for fork
8192 catchpoints. */
8193
8194 static void
8195 print_mention_catch_fork (struct breakpoint *b)
8196 {
8197 printf_filtered (_("Catchpoint %d (fork)"), b->number);
8198 }
8199
8200 /* Implement the "print_recreate" breakpoint_ops method for fork
8201 catchpoints. */
8202
8203 static void
8204 print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
8205 {
8206 fprintf_unfiltered (fp, "catch fork");
8207 print_recreate_thread (b, fp);
8208 }
8209
8210 /* The breakpoint_ops structure to be used in fork catchpoints. */
8211
8212 static struct breakpoint_ops catch_fork_breakpoint_ops;
8213
8214 /* Implement the "insert" breakpoint_ops method for vfork
8215 catchpoints. */
8216
8217 static int
8218 insert_catch_vfork (struct bp_location *bl)
8219 {
8220 return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
8221 }
8222
8223 /* Implement the "remove" breakpoint_ops method for vfork
8224 catchpoints. */
8225
8226 static int
8227 remove_catch_vfork (struct bp_location *bl, enum remove_bp_reason reason)
8228 {
8229 return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
8230 }
8231
8232 /* Implement the "breakpoint_hit" breakpoint_ops method for vfork
8233 catchpoints. */
8234
8235 static int
8236 breakpoint_hit_catch_vfork (const struct bp_location *bl,
8237 struct address_space *aspace, CORE_ADDR bp_addr,
8238 const struct target_waitstatus *ws)
8239 {
8240 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8241
8242 if (ws->kind != TARGET_WAITKIND_VFORKED)
8243 return 0;
8244
8245 c->forked_inferior_pid = ws->value.related_pid;
8246 return 1;
8247 }
8248
8249 /* Implement the "print_it" breakpoint_ops method for vfork
8250 catchpoints. */
8251
8252 static enum print_stop_action
8253 print_it_catch_vfork (bpstat bs)
8254 {
8255 struct ui_out *uiout = current_uiout;
8256 struct breakpoint *b = bs->breakpoint_at;
8257 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8258
8259 annotate_catchpoint (b->number);
8260 maybe_print_thread_hit_breakpoint (uiout);
8261 if (b->disposition == disp_del)
8262 ui_out_text (uiout, "Temporary catchpoint ");
8263 else
8264 ui_out_text (uiout, "Catchpoint ");
8265 if (ui_out_is_mi_like_p (uiout))
8266 {
8267 ui_out_field_string (uiout, "reason",
8268 async_reason_lookup (EXEC_ASYNC_VFORK));
8269 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8270 }
8271 ui_out_field_int (uiout, "bkptno", b->number);
8272 ui_out_text (uiout, " (vforked process ");
8273 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
8274 ui_out_text (uiout, "), ");
8275 return PRINT_SRC_AND_LOC;
8276 }
8277
8278 /* Implement the "print_one" breakpoint_ops method for vfork
8279 catchpoints. */
8280
8281 static void
8282 print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
8283 {
8284 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8285 struct value_print_options opts;
8286 struct ui_out *uiout = current_uiout;
8287
8288 get_user_print_options (&opts);
8289 /* Field 4, the address, is omitted (which makes the columns not
8290 line up too nicely with the headers, but the effect is relatively
8291 readable). */
8292 if (opts.addressprint)
8293 ui_out_field_skip (uiout, "addr");
8294 annotate_field (5);
8295 ui_out_text (uiout, "vfork");
8296 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
8297 {
8298 ui_out_text (uiout, ", process ");
8299 ui_out_field_int (uiout, "what",
8300 ptid_get_pid (c->forked_inferior_pid));
8301 ui_out_spaces (uiout, 1);
8302 }
8303
8304 if (ui_out_is_mi_like_p (uiout))
8305 ui_out_field_string (uiout, "catch-type", "vfork");
8306 }
8307
8308 /* Implement the "print_mention" breakpoint_ops method for vfork
8309 catchpoints. */
8310
8311 static void
8312 print_mention_catch_vfork (struct breakpoint *b)
8313 {
8314 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
8315 }
8316
8317 /* Implement the "print_recreate" breakpoint_ops method for vfork
8318 catchpoints. */
8319
8320 static void
8321 print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8322 {
8323 fprintf_unfiltered (fp, "catch vfork");
8324 print_recreate_thread (b, fp);
8325 }
8326
8327 /* The breakpoint_ops structure to be used in vfork catchpoints. */
8328
8329 static struct breakpoint_ops catch_vfork_breakpoint_ops;
8330
8331 /* An instance of this type is used to represent an solib catchpoint.
8332 It includes a "struct breakpoint" as a kind of base class; users
8333 downcast to "struct breakpoint *" when needed. A breakpoint is
8334 really of this type iff its ops pointer points to
8335 CATCH_SOLIB_BREAKPOINT_OPS. */
8336
8337 struct solib_catchpoint
8338 {
8339 /* The base class. */
8340 struct breakpoint base;
8341
8342 /* True for "catch load", false for "catch unload". */
8343 unsigned char is_load;
8344
8345 /* Regular expression to match, if any. COMPILED is only valid when
8346 REGEX is non-NULL. */
8347 char *regex;
8348 regex_t compiled;
8349 };
8350
8351 static void
8352 dtor_catch_solib (struct breakpoint *b)
8353 {
8354 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8355
8356 if (self->regex)
8357 regfree (&self->compiled);
8358 xfree (self->regex);
8359
8360 base_breakpoint_ops.dtor (b);
8361 }
8362
8363 static int
8364 insert_catch_solib (struct bp_location *ignore)
8365 {
8366 return 0;
8367 }
8368
8369 static int
8370 remove_catch_solib (struct bp_location *ignore, enum remove_bp_reason reason)
8371 {
8372 return 0;
8373 }
8374
8375 static int
8376 breakpoint_hit_catch_solib (const struct bp_location *bl,
8377 struct address_space *aspace,
8378 CORE_ADDR bp_addr,
8379 const struct target_waitstatus *ws)
8380 {
8381 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8382 struct breakpoint *other;
8383
8384 if (ws->kind == TARGET_WAITKIND_LOADED)
8385 return 1;
8386
8387 ALL_BREAKPOINTS (other)
8388 {
8389 struct bp_location *other_bl;
8390
8391 if (other == bl->owner)
8392 continue;
8393
8394 if (other->type != bp_shlib_event)
8395 continue;
8396
8397 if (self->base.pspace != NULL && other->pspace != self->base.pspace)
8398 continue;
8399
8400 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8401 {
8402 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8403 return 1;
8404 }
8405 }
8406
8407 return 0;
8408 }
8409
8410 static void
8411 check_status_catch_solib (struct bpstats *bs)
8412 {
8413 struct solib_catchpoint *self
8414 = (struct solib_catchpoint *) bs->breakpoint_at;
8415 int ix;
8416
8417 if (self->is_load)
8418 {
8419 struct so_list *iter;
8420
8421 for (ix = 0;
8422 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
8423 ix, iter);
8424 ++ix)
8425 {
8426 if (!self->regex
8427 || regexec (&self->compiled, iter->so_name, 0, NULL, 0) == 0)
8428 return;
8429 }
8430 }
8431 else
8432 {
8433 char *iter;
8434
8435 for (ix = 0;
8436 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
8437 ix, iter);
8438 ++ix)
8439 {
8440 if (!self->regex
8441 || regexec (&self->compiled, iter, 0, NULL, 0) == 0)
8442 return;
8443 }
8444 }
8445
8446 bs->stop = 0;
8447 bs->print_it = print_it_noop;
8448 }
8449
8450 static enum print_stop_action
8451 print_it_catch_solib (bpstat bs)
8452 {
8453 struct breakpoint *b = bs->breakpoint_at;
8454 struct ui_out *uiout = current_uiout;
8455
8456 annotate_catchpoint (b->number);
8457 maybe_print_thread_hit_breakpoint (uiout);
8458 if (b->disposition == disp_del)
8459 ui_out_text (uiout, "Temporary catchpoint ");
8460 else
8461 ui_out_text (uiout, "Catchpoint ");
8462 ui_out_field_int (uiout, "bkptno", b->number);
8463 ui_out_text (uiout, "\n");
8464 if (ui_out_is_mi_like_p (uiout))
8465 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8466 print_solib_event (1);
8467 return PRINT_SRC_AND_LOC;
8468 }
8469
8470 static void
8471 print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8472 {
8473 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8474 struct value_print_options opts;
8475 struct ui_out *uiout = current_uiout;
8476 char *msg;
8477
8478 get_user_print_options (&opts);
8479 /* Field 4, the address, is omitted (which makes the columns not
8480 line up too nicely with the headers, but the effect is relatively
8481 readable). */
8482 if (opts.addressprint)
8483 {
8484 annotate_field (4);
8485 ui_out_field_skip (uiout, "addr");
8486 }
8487
8488 annotate_field (5);
8489 if (self->is_load)
8490 {
8491 if (self->regex)
8492 msg = xstrprintf (_("load of library matching %s"), self->regex);
8493 else
8494 msg = xstrdup (_("load of library"));
8495 }
8496 else
8497 {
8498 if (self->regex)
8499 msg = xstrprintf (_("unload of library matching %s"), self->regex);
8500 else
8501 msg = xstrdup (_("unload of library"));
8502 }
8503 ui_out_field_string (uiout, "what", msg);
8504 xfree (msg);
8505
8506 if (ui_out_is_mi_like_p (uiout))
8507 ui_out_field_string (uiout, "catch-type",
8508 self->is_load ? "load" : "unload");
8509 }
8510
8511 static void
8512 print_mention_catch_solib (struct breakpoint *b)
8513 {
8514 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8515
8516 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8517 self->is_load ? "load" : "unload");
8518 }
8519
8520 static void
8521 print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8522 {
8523 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8524
8525 fprintf_unfiltered (fp, "%s %s",
8526 b->disposition == disp_del ? "tcatch" : "catch",
8527 self->is_load ? "load" : "unload");
8528 if (self->regex)
8529 fprintf_unfiltered (fp, " %s", self->regex);
8530 fprintf_unfiltered (fp, "\n");
8531 }
8532
8533 static struct breakpoint_ops catch_solib_breakpoint_ops;
8534
8535 /* Shared helper function (MI and CLI) for creating and installing
8536 a shared object event catchpoint. If IS_LOAD is non-zero then
8537 the events to be caught are load events, otherwise they are
8538 unload events. If IS_TEMP is non-zero the catchpoint is a
8539 temporary one. If ENABLED is non-zero the catchpoint is
8540 created in an enabled state. */
8541
8542 void
8543 add_solib_catchpoint (char *arg, int is_load, int is_temp, int enabled)
8544 {
8545 struct solib_catchpoint *c;
8546 struct gdbarch *gdbarch = get_current_arch ();
8547 struct cleanup *cleanup;
8548
8549 if (!arg)
8550 arg = "";
8551 arg = skip_spaces (arg);
8552
8553 c = XCNEW (struct solib_catchpoint);
8554 cleanup = make_cleanup (xfree, c);
8555
8556 if (*arg != '\0')
8557 {
8558 int errcode;
8559
8560 errcode = regcomp (&c->compiled, arg, REG_NOSUB);
8561 if (errcode != 0)
8562 {
8563 char *err = get_regcomp_error (errcode, &c->compiled);
8564
8565 make_cleanup (xfree, err);
8566 error (_("Invalid regexp (%s): %s"), err, arg);
8567 }
8568 c->regex = xstrdup (arg);
8569 }
8570
8571 c->is_load = is_load;
8572 init_catchpoint (&c->base, gdbarch, is_temp, NULL,
8573 &catch_solib_breakpoint_ops);
8574
8575 c->base.enable_state = enabled ? bp_enabled : bp_disabled;
8576
8577 discard_cleanups (cleanup);
8578 install_breakpoint (0, &c->base, 1);
8579 }
8580
8581 /* A helper function that does all the work for "catch load" and
8582 "catch unload". */
8583
8584 static void
8585 catch_load_or_unload (char *arg, int from_tty, int is_load,
8586 struct cmd_list_element *command)
8587 {
8588 int tempflag;
8589 const int enabled = 1;
8590
8591 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8592
8593 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8594 }
8595
8596 static void
8597 catch_load_command_1 (char *arg, int from_tty,
8598 struct cmd_list_element *command)
8599 {
8600 catch_load_or_unload (arg, from_tty, 1, command);
8601 }
8602
8603 static void
8604 catch_unload_command_1 (char *arg, int from_tty,
8605 struct cmd_list_element *command)
8606 {
8607 catch_load_or_unload (arg, from_tty, 0, command);
8608 }
8609
8610 /* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8611 is non-zero, then make the breakpoint temporary. If COND_STRING is
8612 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8613 the breakpoint_ops structure associated to the catchpoint. */
8614
8615 void
8616 init_catchpoint (struct breakpoint *b,
8617 struct gdbarch *gdbarch, int tempflag,
8618 char *cond_string,
8619 const struct breakpoint_ops *ops)
8620 {
8621 struct symtab_and_line sal;
8622
8623 init_sal (&sal);
8624 sal.pspace = current_program_space;
8625
8626 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
8627
8628 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
8629 b->disposition = tempflag ? disp_del : disp_donttouch;
8630 }
8631
8632 void
8633 install_breakpoint (int internal, struct breakpoint *b, int update_gll)
8634 {
8635 add_to_breakpoint_chain (b);
8636 set_breakpoint_number (internal, b);
8637 if (is_tracepoint (b))
8638 set_tracepoint_count (breakpoint_count);
8639 if (!internal)
8640 mention (b);
8641 observer_notify_breakpoint_created (b);
8642
8643 if (update_gll)
8644 update_global_location_list (UGLL_MAY_INSERT);
8645 }
8646
8647 static void
8648 create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8649 int tempflag, char *cond_string,
8650 const struct breakpoint_ops *ops)
8651 {
8652 struct fork_catchpoint *c = XNEW (struct fork_catchpoint);
8653
8654 init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
8655
8656 c->forked_inferior_pid = null_ptid;
8657
8658 install_breakpoint (0, &c->base, 1);
8659 }
8660
8661 /* Exec catchpoints. */
8662
8663 /* An instance of this type is used to represent an exec catchpoint.
8664 It includes a "struct breakpoint" as a kind of base class; users
8665 downcast to "struct breakpoint *" when needed. A breakpoint is
8666 really of this type iff its ops pointer points to
8667 CATCH_EXEC_BREAKPOINT_OPS. */
8668
8669 struct exec_catchpoint
8670 {
8671 /* The base class. */
8672 struct breakpoint base;
8673
8674 /* Filename of a program whose exec triggered this catchpoint.
8675 This field is only valid immediately after this catchpoint has
8676 triggered. */
8677 char *exec_pathname;
8678 };
8679
8680 /* Implement the "dtor" breakpoint_ops method for exec
8681 catchpoints. */
8682
8683 static void
8684 dtor_catch_exec (struct breakpoint *b)
8685 {
8686 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8687
8688 xfree (c->exec_pathname);
8689
8690 base_breakpoint_ops.dtor (b);
8691 }
8692
8693 static int
8694 insert_catch_exec (struct bp_location *bl)
8695 {
8696 return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
8697 }
8698
8699 static int
8700 remove_catch_exec (struct bp_location *bl, enum remove_bp_reason reason)
8701 {
8702 return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
8703 }
8704
8705 static int
8706 breakpoint_hit_catch_exec (const struct bp_location *bl,
8707 struct address_space *aspace, CORE_ADDR bp_addr,
8708 const struct target_waitstatus *ws)
8709 {
8710 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8711
8712 if (ws->kind != TARGET_WAITKIND_EXECD)
8713 return 0;
8714
8715 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8716 return 1;
8717 }
8718
8719 static enum print_stop_action
8720 print_it_catch_exec (bpstat bs)
8721 {
8722 struct ui_out *uiout = current_uiout;
8723 struct breakpoint *b = bs->breakpoint_at;
8724 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8725
8726 annotate_catchpoint (b->number);
8727 maybe_print_thread_hit_breakpoint (uiout);
8728 if (b->disposition == disp_del)
8729 ui_out_text (uiout, "Temporary catchpoint ");
8730 else
8731 ui_out_text (uiout, "Catchpoint ");
8732 if (ui_out_is_mi_like_p (uiout))
8733 {
8734 ui_out_field_string (uiout, "reason",
8735 async_reason_lookup (EXEC_ASYNC_EXEC));
8736 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8737 }
8738 ui_out_field_int (uiout, "bkptno", b->number);
8739 ui_out_text (uiout, " (exec'd ");
8740 ui_out_field_string (uiout, "new-exec", c->exec_pathname);
8741 ui_out_text (uiout, "), ");
8742
8743 return PRINT_SRC_AND_LOC;
8744 }
8745
8746 static void
8747 print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
8748 {
8749 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8750 struct value_print_options opts;
8751 struct ui_out *uiout = current_uiout;
8752
8753 get_user_print_options (&opts);
8754
8755 /* Field 4, the address, is omitted (which makes the columns
8756 not line up too nicely with the headers, but the effect
8757 is relatively readable). */
8758 if (opts.addressprint)
8759 ui_out_field_skip (uiout, "addr");
8760 annotate_field (5);
8761 ui_out_text (uiout, "exec");
8762 if (c->exec_pathname != NULL)
8763 {
8764 ui_out_text (uiout, ", program \"");
8765 ui_out_field_string (uiout, "what", c->exec_pathname);
8766 ui_out_text (uiout, "\" ");
8767 }
8768
8769 if (ui_out_is_mi_like_p (uiout))
8770 ui_out_field_string (uiout, "catch-type", "exec");
8771 }
8772
8773 static void
8774 print_mention_catch_exec (struct breakpoint *b)
8775 {
8776 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8777 }
8778
8779 /* Implement the "print_recreate" breakpoint_ops method for exec
8780 catchpoints. */
8781
8782 static void
8783 print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8784 {
8785 fprintf_unfiltered (fp, "catch exec");
8786 print_recreate_thread (b, fp);
8787 }
8788
8789 static struct breakpoint_ops catch_exec_breakpoint_ops;
8790
8791 static int
8792 hw_breakpoint_used_count (void)
8793 {
8794 int i = 0;
8795 struct breakpoint *b;
8796 struct bp_location *bl;
8797
8798 ALL_BREAKPOINTS (b)
8799 {
8800 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
8801 for (bl = b->loc; bl; bl = bl->next)
8802 {
8803 /* Special types of hardware breakpoints may use more than
8804 one register. */
8805 i += b->ops->resources_needed (bl);
8806 }
8807 }
8808
8809 return i;
8810 }
8811
8812 /* Returns the resources B would use if it were a hardware
8813 watchpoint. */
8814
8815 static int
8816 hw_watchpoint_use_count (struct breakpoint *b)
8817 {
8818 int i = 0;
8819 struct bp_location *bl;
8820
8821 if (!breakpoint_enabled (b))
8822 return 0;
8823
8824 for (bl = b->loc; bl; bl = bl->next)
8825 {
8826 /* Special types of hardware watchpoints may use more than
8827 one register. */
8828 i += b->ops->resources_needed (bl);
8829 }
8830
8831 return i;
8832 }
8833
8834 /* Returns the sum the used resources of all hardware watchpoints of
8835 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8836 the sum of the used resources of all hardware watchpoints of other
8837 types _not_ TYPE. */
8838
8839 static int
8840 hw_watchpoint_used_count_others (struct breakpoint *except,
8841 enum bptype type, int *other_type_used)
8842 {
8843 int i = 0;
8844 struct breakpoint *b;
8845
8846 *other_type_used = 0;
8847 ALL_BREAKPOINTS (b)
8848 {
8849 if (b == except)
8850 continue;
8851 if (!breakpoint_enabled (b))
8852 continue;
8853
8854 if (b->type == type)
8855 i += hw_watchpoint_use_count (b);
8856 else if (is_hardware_watchpoint (b))
8857 *other_type_used = 1;
8858 }
8859
8860 return i;
8861 }
8862
8863 void
8864 disable_watchpoints_before_interactive_call_start (void)
8865 {
8866 struct breakpoint *b;
8867
8868 ALL_BREAKPOINTS (b)
8869 {
8870 if (is_watchpoint (b) && breakpoint_enabled (b))
8871 {
8872 b->enable_state = bp_call_disabled;
8873 update_global_location_list (UGLL_DONT_INSERT);
8874 }
8875 }
8876 }
8877
8878 void
8879 enable_watchpoints_after_interactive_call_stop (void)
8880 {
8881 struct breakpoint *b;
8882
8883 ALL_BREAKPOINTS (b)
8884 {
8885 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
8886 {
8887 b->enable_state = bp_enabled;
8888 update_global_location_list (UGLL_MAY_INSERT);
8889 }
8890 }
8891 }
8892
8893 void
8894 disable_breakpoints_before_startup (void)
8895 {
8896 current_program_space->executing_startup = 1;
8897 update_global_location_list (UGLL_DONT_INSERT);
8898 }
8899
8900 void
8901 enable_breakpoints_after_startup (void)
8902 {
8903 current_program_space->executing_startup = 0;
8904 breakpoint_re_set ();
8905 }
8906
8907 /* Create a new single-step breakpoint for thread THREAD, with no
8908 locations. */
8909
8910 static struct breakpoint *
8911 new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8912 {
8913 struct breakpoint *b = XNEW (struct breakpoint);
8914
8915 init_raw_breakpoint_without_location (b, gdbarch, bp_single_step,
8916 &momentary_breakpoint_ops);
8917
8918 b->disposition = disp_donttouch;
8919 b->frame_id = null_frame_id;
8920
8921 b->thread = thread;
8922 gdb_assert (b->thread != 0);
8923
8924 add_to_breakpoint_chain (b);
8925
8926 return b;
8927 }
8928
8929 /* Set a momentary breakpoint of type TYPE at address specified by
8930 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8931 frame. */
8932
8933 struct breakpoint *
8934 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8935 struct frame_id frame_id, enum bptype type)
8936 {
8937 struct breakpoint *b;
8938
8939 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8940 tail-called one. */
8941 gdb_assert (!frame_id_artificial_p (frame_id));
8942
8943 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
8944 b->enable_state = bp_enabled;
8945 b->disposition = disp_donttouch;
8946 b->frame_id = frame_id;
8947
8948 /* If we're debugging a multi-threaded program, then we want
8949 momentary breakpoints to be active in only a single thread of
8950 control. */
8951 if (in_thread_list (inferior_ptid))
8952 b->thread = ptid_to_global_thread_id (inferior_ptid);
8953
8954 update_global_location_list_nothrow (UGLL_MAY_INSERT);
8955
8956 return b;
8957 }
8958
8959 /* Make a momentary breakpoint based on the master breakpoint ORIG.
8960 The new breakpoint will have type TYPE, use OPS as its
8961 breakpoint_ops, and will set enabled to LOC_ENABLED. */
8962
8963 static struct breakpoint *
8964 momentary_breakpoint_from_master (struct breakpoint *orig,
8965 enum bptype type,
8966 const struct breakpoint_ops *ops,
8967 int loc_enabled)
8968 {
8969 struct breakpoint *copy;
8970
8971 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
8972 copy->loc = allocate_bp_location (copy);
8973 set_breakpoint_location_function (copy->loc, 1);
8974
8975 copy->loc->gdbarch = orig->loc->gdbarch;
8976 copy->loc->requested_address = orig->loc->requested_address;
8977 copy->loc->address = orig->loc->address;
8978 copy->loc->section = orig->loc->section;
8979 copy->loc->pspace = orig->loc->pspace;
8980 copy->loc->probe = orig->loc->probe;
8981 copy->loc->line_number = orig->loc->line_number;
8982 copy->loc->symtab = orig->loc->symtab;
8983 copy->loc->enabled = loc_enabled;
8984 copy->frame_id = orig->frame_id;
8985 copy->thread = orig->thread;
8986 copy->pspace = orig->pspace;
8987
8988 copy->enable_state = bp_enabled;
8989 copy->disposition = disp_donttouch;
8990 copy->number = internal_breakpoint_number--;
8991
8992 update_global_location_list_nothrow (UGLL_DONT_INSERT);
8993 return copy;
8994 }
8995
8996 /* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8997 ORIG is NULL. */
8998
8999 struct breakpoint *
9000 clone_momentary_breakpoint (struct breakpoint *orig)
9001 {
9002 /* If there's nothing to clone, then return nothing. */
9003 if (orig == NULL)
9004 return NULL;
9005
9006 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
9007 }
9008
9009 struct breakpoint *
9010 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
9011 enum bptype type)
9012 {
9013 struct symtab_and_line sal;
9014
9015 sal = find_pc_line (pc, 0);
9016 sal.pc = pc;
9017 sal.section = find_pc_overlay (pc);
9018 sal.explicit_pc = 1;
9019
9020 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
9021 }
9022 \f
9023
9024 /* Tell the user we have just set a breakpoint B. */
9025
9026 static void
9027 mention (struct breakpoint *b)
9028 {
9029 b->ops->print_mention (b);
9030 if (ui_out_is_mi_like_p (current_uiout))
9031 return;
9032 printf_filtered ("\n");
9033 }
9034 \f
9035
9036 static int bp_loc_is_permanent (struct bp_location *loc);
9037
9038 static struct bp_location *
9039 add_location_to_breakpoint (struct breakpoint *b,
9040 const struct symtab_and_line *sal)
9041 {
9042 struct bp_location *loc, **tmp;
9043 CORE_ADDR adjusted_address;
9044 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
9045
9046 if (loc_gdbarch == NULL)
9047 loc_gdbarch = b->gdbarch;
9048
9049 /* Adjust the breakpoint's address prior to allocating a location.
9050 Once we call allocate_bp_location(), that mostly uninitialized
9051 location will be placed on the location chain. Adjustment of the
9052 breakpoint may cause target_read_memory() to be called and we do
9053 not want its scan of the location chain to find a breakpoint and
9054 location that's only been partially initialized. */
9055 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
9056 sal->pc, b->type);
9057
9058 /* Sort the locations by their ADDRESS. */
9059 loc = allocate_bp_location (b);
9060 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
9061 tmp = &((*tmp)->next))
9062 ;
9063 loc->next = *tmp;
9064 *tmp = loc;
9065
9066 loc->requested_address = sal->pc;
9067 loc->address = adjusted_address;
9068 loc->pspace = sal->pspace;
9069 loc->probe.probe = sal->probe;
9070 loc->probe.objfile = sal->objfile;
9071 gdb_assert (loc->pspace != NULL);
9072 loc->section = sal->section;
9073 loc->gdbarch = loc_gdbarch;
9074 loc->line_number = sal->line;
9075 loc->symtab = sal->symtab;
9076
9077 set_breakpoint_location_function (loc,
9078 sal->explicit_pc || sal->explicit_line);
9079
9080 /* While by definition, permanent breakpoints are already present in the
9081 code, we don't mark the location as inserted. Normally one would expect
9082 that GDB could rely on that breakpoint instruction to stop the program,
9083 thus removing the need to insert its own breakpoint, except that executing
9084 the breakpoint instruction can kill the target instead of reporting a
9085 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
9086 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
9087 with "Trap 0x02 while interrupts disabled, Error state". Letting the
9088 breakpoint be inserted normally results in QEMU knowing about the GDB
9089 breakpoint, and thus trap before the breakpoint instruction is executed.
9090 (If GDB later needs to continue execution past the permanent breakpoint,
9091 it manually increments the PC, thus avoiding executing the breakpoint
9092 instruction.) */
9093 if (bp_loc_is_permanent (loc))
9094 loc->permanent = 1;
9095
9096 return loc;
9097 }
9098 \f
9099
9100 /* See breakpoint.h. */
9101
9102 int
9103 program_breakpoint_here_p (struct gdbarch *gdbarch, CORE_ADDR address)
9104 {
9105 int len;
9106 CORE_ADDR addr;
9107 const gdb_byte *bpoint;
9108 gdb_byte *target_mem;
9109 struct cleanup *cleanup;
9110 int retval = 0;
9111
9112 addr = address;
9113 bpoint = gdbarch_breakpoint_from_pc (gdbarch, &addr, &len);
9114
9115 /* Software breakpoints unsupported? */
9116 if (bpoint == NULL)
9117 return 0;
9118
9119 target_mem = (gdb_byte *) alloca (len);
9120
9121 /* Enable the automatic memory restoration from breakpoints while
9122 we read the memory. Otherwise we could say about our temporary
9123 breakpoints they are permanent. */
9124 cleanup = make_show_memory_breakpoints_cleanup (0);
9125
9126 if (target_read_memory (address, target_mem, len) == 0
9127 && memcmp (target_mem, bpoint, len) == 0)
9128 retval = 1;
9129
9130 do_cleanups (cleanup);
9131
9132 return retval;
9133 }
9134
9135 /* Return 1 if LOC is pointing to a permanent breakpoint,
9136 return 0 otherwise. */
9137
9138 static int
9139 bp_loc_is_permanent (struct bp_location *loc)
9140 {
9141 struct cleanup *cleanup;
9142 int retval;
9143
9144 gdb_assert (loc != NULL);
9145
9146 /* If we have a catchpoint or a watchpoint, just return 0. We should not
9147 attempt to read from the addresses the locations of these breakpoint types
9148 point to. program_breakpoint_here_p, below, will attempt to read
9149 memory. */
9150 if (!breakpoint_address_is_meaningful (loc->owner))
9151 return 0;
9152
9153 cleanup = save_current_space_and_thread ();
9154 switch_to_program_space_and_thread (loc->pspace);
9155
9156 retval = program_breakpoint_here_p (loc->gdbarch, loc->address);
9157
9158 do_cleanups (cleanup);
9159
9160 return retval;
9161 }
9162
9163 /* Build a command list for the dprintf corresponding to the current
9164 settings of the dprintf style options. */
9165
9166 static void
9167 update_dprintf_command_list (struct breakpoint *b)
9168 {
9169 char *dprintf_args = b->extra_string;
9170 char *printf_line = NULL;
9171
9172 if (!dprintf_args)
9173 return;
9174
9175 dprintf_args = skip_spaces (dprintf_args);
9176
9177 /* Allow a comma, as it may have terminated a location, but don't
9178 insist on it. */
9179 if (*dprintf_args == ',')
9180 ++dprintf_args;
9181 dprintf_args = skip_spaces (dprintf_args);
9182
9183 if (*dprintf_args != '"')
9184 error (_("Bad format string, missing '\"'."));
9185
9186 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
9187 printf_line = xstrprintf ("printf %s", dprintf_args);
9188 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
9189 {
9190 if (!dprintf_function)
9191 error (_("No function supplied for dprintf call"));
9192
9193 if (dprintf_channel && strlen (dprintf_channel) > 0)
9194 printf_line = xstrprintf ("call (void) %s (%s,%s)",
9195 dprintf_function,
9196 dprintf_channel,
9197 dprintf_args);
9198 else
9199 printf_line = xstrprintf ("call (void) %s (%s)",
9200 dprintf_function,
9201 dprintf_args);
9202 }
9203 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
9204 {
9205 if (target_can_run_breakpoint_commands ())
9206 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
9207 else
9208 {
9209 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
9210 printf_line = xstrprintf ("printf %s", dprintf_args);
9211 }
9212 }
9213 else
9214 internal_error (__FILE__, __LINE__,
9215 _("Invalid dprintf style."));
9216
9217 gdb_assert (printf_line != NULL);
9218 /* Manufacture a printf sequence. */
9219 {
9220 struct command_line *printf_cmd_line = XNEW (struct command_line);
9221
9222 printf_cmd_line->control_type = simple_control;
9223 printf_cmd_line->body_count = 0;
9224 printf_cmd_line->body_list = NULL;
9225 printf_cmd_line->next = NULL;
9226 printf_cmd_line->line = printf_line;
9227
9228 breakpoint_set_commands (b, printf_cmd_line);
9229 }
9230 }
9231
9232 /* Update all dprintf commands, making their command lists reflect
9233 current style settings. */
9234
9235 static void
9236 update_dprintf_commands (char *args, int from_tty,
9237 struct cmd_list_element *c)
9238 {
9239 struct breakpoint *b;
9240
9241 ALL_BREAKPOINTS (b)
9242 {
9243 if (b->type == bp_dprintf)
9244 update_dprintf_command_list (b);
9245 }
9246 }
9247
9248 /* Create a breakpoint with SAL as location. Use LOCATION
9249 as a description of the location, and COND_STRING
9250 as condition expression. If LOCATION is NULL then create an
9251 "address location" from the address in the SAL. */
9252
9253 static void
9254 init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
9255 struct symtabs_and_lines sals,
9256 struct event_location *location,
9257 char *filter, char *cond_string,
9258 char *extra_string,
9259 enum bptype type, enum bpdisp disposition,
9260 int thread, int task, int ignore_count,
9261 const struct breakpoint_ops *ops, int from_tty,
9262 int enabled, int internal, unsigned flags,
9263 int display_canonical)
9264 {
9265 int i;
9266
9267 if (type == bp_hardware_breakpoint)
9268 {
9269 int target_resources_ok;
9270
9271 i = hw_breakpoint_used_count ();
9272 target_resources_ok =
9273 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9274 i + 1, 0);
9275 if (target_resources_ok == 0)
9276 error (_("No hardware breakpoint support in the target."));
9277 else if (target_resources_ok < 0)
9278 error (_("Hardware breakpoints used exceeds limit."));
9279 }
9280
9281 gdb_assert (sals.nelts > 0);
9282
9283 for (i = 0; i < sals.nelts; ++i)
9284 {
9285 struct symtab_and_line sal = sals.sals[i];
9286 struct bp_location *loc;
9287
9288 if (from_tty)
9289 {
9290 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9291 if (!loc_gdbarch)
9292 loc_gdbarch = gdbarch;
9293
9294 describe_other_breakpoints (loc_gdbarch,
9295 sal.pspace, sal.pc, sal.section, thread);
9296 }
9297
9298 if (i == 0)
9299 {
9300 init_raw_breakpoint (b, gdbarch, sal, type, ops);
9301 b->thread = thread;
9302 b->task = task;
9303
9304 b->cond_string = cond_string;
9305 b->extra_string = extra_string;
9306 b->ignore_count = ignore_count;
9307 b->enable_state = enabled ? bp_enabled : bp_disabled;
9308 b->disposition = disposition;
9309
9310 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9311 b->loc->inserted = 1;
9312
9313 if (type == bp_static_tracepoint)
9314 {
9315 struct tracepoint *t = (struct tracepoint *) b;
9316 struct static_tracepoint_marker marker;
9317
9318 if (strace_marker_p (b))
9319 {
9320 /* We already know the marker exists, otherwise, we
9321 wouldn't see a sal for it. */
9322 const char *p = &event_location_to_string (b->location)[3];
9323 const char *endp;
9324 char *marker_str;
9325
9326 p = skip_spaces_const (p);
9327
9328 endp = skip_to_space_const (p);
9329
9330 marker_str = savestring (p, endp - p);
9331 t->static_trace_marker_id = marker_str;
9332
9333 printf_filtered (_("Probed static tracepoint "
9334 "marker \"%s\"\n"),
9335 t->static_trace_marker_id);
9336 }
9337 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
9338 {
9339 t->static_trace_marker_id = xstrdup (marker.str_id);
9340 release_static_tracepoint_marker (&marker);
9341
9342 printf_filtered (_("Probed static tracepoint "
9343 "marker \"%s\"\n"),
9344 t->static_trace_marker_id);
9345 }
9346 else
9347 warning (_("Couldn't determine the static "
9348 "tracepoint marker to probe"));
9349 }
9350
9351 loc = b->loc;
9352 }
9353 else
9354 {
9355 loc = add_location_to_breakpoint (b, &sal);
9356 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9357 loc->inserted = 1;
9358 }
9359
9360 if (b->cond_string)
9361 {
9362 const char *arg = b->cond_string;
9363
9364 loc->cond = parse_exp_1 (&arg, loc->address,
9365 block_for_pc (loc->address), 0);
9366 if (*arg)
9367 error (_("Garbage '%s' follows condition"), arg);
9368 }
9369
9370 /* Dynamic printf requires and uses additional arguments on the
9371 command line, otherwise it's an error. */
9372 if (type == bp_dprintf)
9373 {
9374 if (b->extra_string)
9375 update_dprintf_command_list (b);
9376 else
9377 error (_("Format string required"));
9378 }
9379 else if (b->extra_string)
9380 error (_("Garbage '%s' at end of command"), b->extra_string);
9381 }
9382
9383 b->display_canonical = display_canonical;
9384 if (location != NULL)
9385 b->location = location;
9386 else
9387 {
9388 const char *addr_string = NULL;
9389 int addr_string_len = 0;
9390
9391 if (location != NULL)
9392 addr_string = event_location_to_string (location);
9393 if (addr_string != NULL)
9394 addr_string_len = strlen (addr_string);
9395
9396 b->location = new_address_location (b->loc->address,
9397 addr_string, addr_string_len);
9398 }
9399 b->filter = filter;
9400 }
9401
9402 static void
9403 create_breakpoint_sal (struct gdbarch *gdbarch,
9404 struct symtabs_and_lines sals,
9405 struct event_location *location,
9406 char *filter, char *cond_string,
9407 char *extra_string,
9408 enum bptype type, enum bpdisp disposition,
9409 int thread, int task, int ignore_count,
9410 const struct breakpoint_ops *ops, int from_tty,
9411 int enabled, int internal, unsigned flags,
9412 int display_canonical)
9413 {
9414 struct breakpoint *b;
9415 struct cleanup *old_chain;
9416
9417 if (is_tracepoint_type (type))
9418 {
9419 struct tracepoint *t;
9420
9421 t = XCNEW (struct tracepoint);
9422 b = &t->base;
9423 }
9424 else
9425 b = XNEW (struct breakpoint);
9426
9427 old_chain = make_cleanup (xfree, b);
9428
9429 init_breakpoint_sal (b, gdbarch,
9430 sals, location,
9431 filter, cond_string, extra_string,
9432 type, disposition,
9433 thread, task, ignore_count,
9434 ops, from_tty,
9435 enabled, internal, flags,
9436 display_canonical);
9437 discard_cleanups (old_chain);
9438
9439 install_breakpoint (internal, b, 0);
9440 }
9441
9442 /* Add SALS.nelts breakpoints to the breakpoint table. For each
9443 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9444 value. COND_STRING, if not NULL, specified the condition to be
9445 used for all breakpoints. Essentially the only case where
9446 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9447 function. In that case, it's still not possible to specify
9448 separate conditions for different overloaded functions, so
9449 we take just a single condition string.
9450
9451 NOTE: If the function succeeds, the caller is expected to cleanup
9452 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
9453 array contents). If the function fails (error() is called), the
9454 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
9455 COND and SALS arrays and each of those arrays contents. */
9456
9457 static void
9458 create_breakpoints_sal (struct gdbarch *gdbarch,
9459 struct linespec_result *canonical,
9460 char *cond_string, char *extra_string,
9461 enum bptype type, enum bpdisp disposition,
9462 int thread, int task, int ignore_count,
9463 const struct breakpoint_ops *ops, int from_tty,
9464 int enabled, int internal, unsigned flags)
9465 {
9466 int i;
9467 struct linespec_sals *lsal;
9468
9469 if (canonical->pre_expanded)
9470 gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
9471
9472 for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
9473 {
9474 /* Note that 'location' can be NULL in the case of a plain
9475 'break', without arguments. */
9476 struct event_location *location
9477 = (canonical->location != NULL
9478 ? copy_event_location (canonical->location) : NULL);
9479 char *filter_string = lsal->canonical ? xstrdup (lsal->canonical) : NULL;
9480 struct cleanup *inner = make_cleanup_delete_event_location (location);
9481
9482 make_cleanup (xfree, filter_string);
9483 create_breakpoint_sal (gdbarch, lsal->sals,
9484 location,
9485 filter_string,
9486 cond_string, extra_string,
9487 type, disposition,
9488 thread, task, ignore_count, ops,
9489 from_tty, enabled, internal, flags,
9490 canonical->special_display);
9491 discard_cleanups (inner);
9492 }
9493 }
9494
9495 /* Parse LOCATION which is assumed to be a SAL specification possibly
9496 followed by conditionals. On return, SALS contains an array of SAL
9497 addresses found. LOCATION points to the end of the SAL (for
9498 linespec locations).
9499
9500 The array and the line spec strings are allocated on the heap, it is
9501 the caller's responsibility to free them. */
9502
9503 static void
9504 parse_breakpoint_sals (const struct event_location *location,
9505 struct linespec_result *canonical)
9506 {
9507 struct symtab_and_line cursal;
9508
9509 if (event_location_type (location) == LINESPEC_LOCATION)
9510 {
9511 const char *address = get_linespec_location (location);
9512
9513 if (address == NULL)
9514 {
9515 /* The last displayed codepoint, if it's valid, is our default
9516 breakpoint address. */
9517 if (last_displayed_sal_is_valid ())
9518 {
9519 struct linespec_sals lsal;
9520 struct symtab_and_line sal;
9521 CORE_ADDR pc;
9522
9523 init_sal (&sal); /* Initialize to zeroes. */
9524 lsal.sals.sals = XNEW (struct symtab_and_line);
9525
9526 /* Set sal's pspace, pc, symtab, and line to the values
9527 corresponding to the last call to print_frame_info.
9528 Be sure to reinitialize LINE with NOTCURRENT == 0
9529 as the breakpoint line number is inappropriate otherwise.
9530 find_pc_line would adjust PC, re-set it back. */
9531 get_last_displayed_sal (&sal);
9532 pc = sal.pc;
9533 sal = find_pc_line (pc, 0);
9534
9535 /* "break" without arguments is equivalent to "break *PC"
9536 where PC is the last displayed codepoint's address. So
9537 make sure to set sal.explicit_pc to prevent GDB from
9538 trying to expand the list of sals to include all other
9539 instances with the same symtab and line. */
9540 sal.pc = pc;
9541 sal.explicit_pc = 1;
9542
9543 lsal.sals.sals[0] = sal;
9544 lsal.sals.nelts = 1;
9545 lsal.canonical = NULL;
9546
9547 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
9548 return;
9549 }
9550 else
9551 error (_("No default breakpoint address now."));
9552 }
9553 }
9554
9555 /* Force almost all breakpoints to be in terms of the
9556 current_source_symtab (which is decode_line_1's default).
9557 This should produce the results we want almost all of the
9558 time while leaving default_breakpoint_* alone.
9559
9560 ObjC: However, don't match an Objective-C method name which
9561 may have a '+' or '-' succeeded by a '['. */
9562 cursal = get_current_source_symtab_and_line ();
9563 if (last_displayed_sal_is_valid ())
9564 {
9565 const char *address = NULL;
9566
9567 if (event_location_type (location) == LINESPEC_LOCATION)
9568 address = get_linespec_location (location);
9569
9570 if (!cursal.symtab
9571 || (address != NULL
9572 && strchr ("+-", address[0]) != NULL
9573 && address[1] != '['))
9574 {
9575 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9576 get_last_displayed_symtab (),
9577 get_last_displayed_line (),
9578 canonical, NULL, NULL);
9579 return;
9580 }
9581 }
9582
9583 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9584 cursal.symtab, cursal.line, canonical, NULL, NULL);
9585 }
9586
9587
9588 /* Convert each SAL into a real PC. Verify that the PC can be
9589 inserted as a breakpoint. If it can't throw an error. */
9590
9591 static void
9592 breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
9593 {
9594 int i;
9595
9596 for (i = 0; i < sals->nelts; i++)
9597 resolve_sal_pc (&sals->sals[i]);
9598 }
9599
9600 /* Fast tracepoints may have restrictions on valid locations. For
9601 instance, a fast tracepoint using a jump instead of a trap will
9602 likely have to overwrite more bytes than a trap would, and so can
9603 only be placed where the instruction is longer than the jump, or a
9604 multi-instruction sequence does not have a jump into the middle of
9605 it, etc. */
9606
9607 static void
9608 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9609 struct symtabs_and_lines *sals)
9610 {
9611 int i, rslt;
9612 struct symtab_and_line *sal;
9613 char *msg;
9614 struct cleanup *old_chain;
9615
9616 for (i = 0; i < sals->nelts; i++)
9617 {
9618 struct gdbarch *sarch;
9619
9620 sal = &sals->sals[i];
9621
9622 sarch = get_sal_arch (*sal);
9623 /* We fall back to GDBARCH if there is no architecture
9624 associated with SAL. */
9625 if (sarch == NULL)
9626 sarch = gdbarch;
9627 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc, &msg);
9628 old_chain = make_cleanup (xfree, msg);
9629
9630 if (!rslt)
9631 error (_("May not have a fast tracepoint at 0x%s%s"),
9632 paddress (sarch, sal->pc), (msg ? msg : ""));
9633
9634 do_cleanups (old_chain);
9635 }
9636 }
9637
9638 /* Given TOK, a string specification of condition and thread, as
9639 accepted by the 'break' command, extract the condition
9640 string and thread number and set *COND_STRING and *THREAD.
9641 PC identifies the context at which the condition should be parsed.
9642 If no condition is found, *COND_STRING is set to NULL.
9643 If no thread is found, *THREAD is set to -1. */
9644
9645 static void
9646 find_condition_and_thread (const char *tok, CORE_ADDR pc,
9647 char **cond_string, int *thread, int *task,
9648 char **rest)
9649 {
9650 *cond_string = NULL;
9651 *thread = -1;
9652 *task = 0;
9653 *rest = NULL;
9654
9655 while (tok && *tok)
9656 {
9657 const char *end_tok;
9658 int toklen;
9659 const char *cond_start = NULL;
9660 const char *cond_end = NULL;
9661
9662 tok = skip_spaces_const (tok);
9663
9664 if ((*tok == '"' || *tok == ',') && rest)
9665 {
9666 *rest = savestring (tok, strlen (tok));
9667 return;
9668 }
9669
9670 end_tok = skip_to_space_const (tok);
9671
9672 toklen = end_tok - tok;
9673
9674 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9675 {
9676 struct expression *expr;
9677
9678 tok = cond_start = end_tok + 1;
9679 expr = parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
9680 xfree (expr);
9681 cond_end = tok;
9682 *cond_string = savestring (cond_start, cond_end - cond_start);
9683 }
9684 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9685 {
9686 const char *tmptok;
9687 struct thread_info *thr;
9688
9689 tok = end_tok + 1;
9690 thr = parse_thread_id (tok, &tmptok);
9691 if (tok == tmptok)
9692 error (_("Junk after thread keyword."));
9693 *thread = thr->global_num;
9694 tok = tmptok;
9695 }
9696 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9697 {
9698 char *tmptok;
9699
9700 tok = end_tok + 1;
9701 *task = strtol (tok, &tmptok, 0);
9702 if (tok == tmptok)
9703 error (_("Junk after task keyword."));
9704 if (!valid_task_id (*task))
9705 error (_("Unknown task %d."), *task);
9706 tok = tmptok;
9707 }
9708 else if (rest)
9709 {
9710 *rest = savestring (tok, strlen (tok));
9711 return;
9712 }
9713 else
9714 error (_("Junk at end of arguments."));
9715 }
9716 }
9717
9718 /* Decode a static tracepoint marker spec. */
9719
9720 static struct symtabs_and_lines
9721 decode_static_tracepoint_spec (const char **arg_p)
9722 {
9723 VEC(static_tracepoint_marker_p) *markers = NULL;
9724 struct symtabs_and_lines sals;
9725 struct cleanup *old_chain;
9726 const char *p = &(*arg_p)[3];
9727 const char *endp;
9728 char *marker_str;
9729 int i;
9730
9731 p = skip_spaces_const (p);
9732
9733 endp = skip_to_space_const (p);
9734
9735 marker_str = savestring (p, endp - p);
9736 old_chain = make_cleanup (xfree, marker_str);
9737
9738 markers = target_static_tracepoint_markers_by_strid (marker_str);
9739 if (VEC_empty(static_tracepoint_marker_p, markers))
9740 error (_("No known static tracepoint marker named %s"), marker_str);
9741
9742 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
9743 sals.sals = XNEWVEC (struct symtab_and_line, sals.nelts);
9744
9745 for (i = 0; i < sals.nelts; i++)
9746 {
9747 struct static_tracepoint_marker *marker;
9748
9749 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9750
9751 init_sal (&sals.sals[i]);
9752
9753 sals.sals[i] = find_pc_line (marker->address, 0);
9754 sals.sals[i].pc = marker->address;
9755
9756 release_static_tracepoint_marker (marker);
9757 }
9758
9759 do_cleanups (old_chain);
9760
9761 *arg_p = endp;
9762 return sals;
9763 }
9764
9765 /* See breakpoint.h. */
9766
9767 int
9768 create_breakpoint (struct gdbarch *gdbarch,
9769 const struct event_location *location, char *cond_string,
9770 int thread, char *extra_string,
9771 int parse_extra,
9772 int tempflag, enum bptype type_wanted,
9773 int ignore_count,
9774 enum auto_boolean pending_break_support,
9775 const struct breakpoint_ops *ops,
9776 int from_tty, int enabled, int internal,
9777 unsigned flags)
9778 {
9779 struct linespec_result canonical;
9780 struct cleanup *old_chain;
9781 struct cleanup *bkpt_chain = NULL;
9782 int pending = 0;
9783 int task = 0;
9784 int prev_bkpt_count = breakpoint_count;
9785
9786 gdb_assert (ops != NULL);
9787
9788 /* If extra_string isn't useful, set it to NULL. */
9789 if (extra_string != NULL && *extra_string == '\0')
9790 extra_string = NULL;
9791
9792 init_linespec_result (&canonical);
9793
9794 TRY
9795 {
9796 ops->create_sals_from_location (location, &canonical, type_wanted);
9797 }
9798 CATCH (e, RETURN_MASK_ERROR)
9799 {
9800 /* If caller is interested in rc value from parse, set
9801 value. */
9802 if (e.error == NOT_FOUND_ERROR)
9803 {
9804 /* If pending breakpoint support is turned off, throw
9805 error. */
9806
9807 if (pending_break_support == AUTO_BOOLEAN_FALSE)
9808 throw_exception (e);
9809
9810 exception_print (gdb_stderr, e);
9811
9812 /* If pending breakpoint support is auto query and the user
9813 selects no, then simply return the error code. */
9814 if (pending_break_support == AUTO_BOOLEAN_AUTO
9815 && !nquery (_("Make %s pending on future shared library load? "),
9816 bptype_string (type_wanted)))
9817 return 0;
9818
9819 /* At this point, either the user was queried about setting
9820 a pending breakpoint and selected yes, or pending
9821 breakpoint behavior is on and thus a pending breakpoint
9822 is defaulted on behalf of the user. */
9823 pending = 1;
9824 }
9825 else
9826 throw_exception (e);
9827 }
9828 END_CATCH
9829
9830 if (!pending && VEC_empty (linespec_sals, canonical.sals))
9831 return 0;
9832
9833 /* Create a chain of things that always need to be cleaned up. */
9834 old_chain = make_cleanup_destroy_linespec_result (&canonical);
9835
9836 /* ----------------------------- SNIP -----------------------------
9837 Anything added to the cleanup chain beyond this point is assumed
9838 to be part of a breakpoint. If the breakpoint create succeeds
9839 then the memory is not reclaimed. */
9840 bkpt_chain = make_cleanup (null_cleanup, 0);
9841
9842 /* Resolve all line numbers to PC's and verify that the addresses
9843 are ok for the target. */
9844 if (!pending)
9845 {
9846 int ix;
9847 struct linespec_sals *iter;
9848
9849 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9850 breakpoint_sals_to_pc (&iter->sals);
9851 }
9852
9853 /* Fast tracepoints may have additional restrictions on location. */
9854 if (!pending && type_wanted == bp_fast_tracepoint)
9855 {
9856 int ix;
9857 struct linespec_sals *iter;
9858
9859 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9860 check_fast_tracepoint_sals (gdbarch, &iter->sals);
9861 }
9862
9863 /* Verify that condition can be parsed, before setting any
9864 breakpoints. Allocate a separate condition expression for each
9865 breakpoint. */
9866 if (!pending)
9867 {
9868 if (parse_extra)
9869 {
9870 char *rest;
9871 struct linespec_sals *lsal;
9872
9873 lsal = VEC_index (linespec_sals, canonical.sals, 0);
9874
9875 /* Here we only parse 'arg' to separate condition
9876 from thread number, so parsing in context of first
9877 sal is OK. When setting the breakpoint we'll
9878 re-parse it in context of each sal. */
9879
9880 find_condition_and_thread (extra_string, lsal->sals.sals[0].pc,
9881 &cond_string, &thread, &task, &rest);
9882 if (cond_string)
9883 make_cleanup (xfree, cond_string);
9884 if (rest)
9885 make_cleanup (xfree, rest);
9886 if (rest)
9887 extra_string = rest;
9888 else
9889 extra_string = NULL;
9890 }
9891 else
9892 {
9893 if (type_wanted != bp_dprintf
9894 && extra_string != NULL && *extra_string != '\0')
9895 error (_("Garbage '%s' at end of location"), extra_string);
9896
9897 /* Create a private copy of condition string. */
9898 if (cond_string)
9899 {
9900 cond_string = xstrdup (cond_string);
9901 make_cleanup (xfree, cond_string);
9902 }
9903 /* Create a private copy of any extra string. */
9904 if (extra_string)
9905 {
9906 extra_string = xstrdup (extra_string);
9907 make_cleanup (xfree, extra_string);
9908 }
9909 }
9910
9911 ops->create_breakpoints_sal (gdbarch, &canonical,
9912 cond_string, extra_string, type_wanted,
9913 tempflag ? disp_del : disp_donttouch,
9914 thread, task, ignore_count, ops,
9915 from_tty, enabled, internal, flags);
9916 }
9917 else
9918 {
9919 struct breakpoint *b;
9920
9921 if (is_tracepoint_type (type_wanted))
9922 {
9923 struct tracepoint *t;
9924
9925 t = XCNEW (struct tracepoint);
9926 b = &t->base;
9927 }
9928 else
9929 b = XNEW (struct breakpoint);
9930
9931 init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops);
9932 b->location = copy_event_location (location);
9933
9934 if (parse_extra)
9935 b->cond_string = NULL;
9936 else
9937 {
9938 /* Create a private copy of condition string. */
9939 if (cond_string)
9940 {
9941 cond_string = xstrdup (cond_string);
9942 make_cleanup (xfree, cond_string);
9943 }
9944 b->cond_string = cond_string;
9945 b->thread = thread;
9946 }
9947
9948 /* Create a private copy of any extra string. */
9949 if (extra_string != NULL)
9950 {
9951 extra_string = xstrdup (extra_string);
9952 make_cleanup (xfree, extra_string);
9953 }
9954 b->extra_string = extra_string;
9955 b->ignore_count = ignore_count;
9956 b->disposition = tempflag ? disp_del : disp_donttouch;
9957 b->condition_not_parsed = 1;
9958 b->enable_state = enabled ? bp_enabled : bp_disabled;
9959 if ((type_wanted != bp_breakpoint
9960 && type_wanted != bp_hardware_breakpoint) || thread != -1)
9961 b->pspace = current_program_space;
9962
9963 install_breakpoint (internal, b, 0);
9964 }
9965
9966 if (VEC_length (linespec_sals, canonical.sals) > 1)
9967 {
9968 warning (_("Multiple breakpoints were set.\nUse the "
9969 "\"delete\" command to delete unwanted breakpoints."));
9970 prev_breakpoint_count = prev_bkpt_count;
9971 }
9972
9973 /* That's it. Discard the cleanups for data inserted into the
9974 breakpoint. */
9975 discard_cleanups (bkpt_chain);
9976 /* But cleanup everything else. */
9977 do_cleanups (old_chain);
9978
9979 /* error call may happen here - have BKPT_CHAIN already discarded. */
9980 update_global_location_list (UGLL_MAY_INSERT);
9981
9982 return 1;
9983 }
9984
9985 /* Set a breakpoint.
9986 ARG is a string describing breakpoint address,
9987 condition, and thread.
9988 FLAG specifies if a breakpoint is hardware on,
9989 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9990 and BP_TEMPFLAG. */
9991
9992 static void
9993 break_command_1 (char *arg, int flag, int from_tty)
9994 {
9995 int tempflag = flag & BP_TEMPFLAG;
9996 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9997 ? bp_hardware_breakpoint
9998 : bp_breakpoint);
9999 struct breakpoint_ops *ops;
10000 struct event_location *location;
10001 struct cleanup *cleanup;
10002
10003 location = string_to_event_location (&arg, current_language);
10004 cleanup = make_cleanup_delete_event_location (location);
10005
10006 /* Matching breakpoints on probes. */
10007 if (location != NULL
10008 && event_location_type (location) == PROBE_LOCATION)
10009 ops = &bkpt_probe_breakpoint_ops;
10010 else
10011 ops = &bkpt_breakpoint_ops;
10012
10013 create_breakpoint (get_current_arch (),
10014 location,
10015 NULL, 0, arg, 1 /* parse arg */,
10016 tempflag, type_wanted,
10017 0 /* Ignore count */,
10018 pending_break_support,
10019 ops,
10020 from_tty,
10021 1 /* enabled */,
10022 0 /* internal */,
10023 0);
10024 do_cleanups (cleanup);
10025 }
10026
10027 /* Helper function for break_command_1 and disassemble_command. */
10028
10029 void
10030 resolve_sal_pc (struct symtab_and_line *sal)
10031 {
10032 CORE_ADDR pc;
10033
10034 if (sal->pc == 0 && sal->symtab != NULL)
10035 {
10036 if (!find_line_pc (sal->symtab, sal->line, &pc))
10037 error (_("No line %d in file \"%s\"."),
10038 sal->line, symtab_to_filename_for_display (sal->symtab));
10039 sal->pc = pc;
10040
10041 /* If this SAL corresponds to a breakpoint inserted using a line
10042 number, then skip the function prologue if necessary. */
10043 if (sal->explicit_line)
10044 skip_prologue_sal (sal);
10045 }
10046
10047 if (sal->section == 0 && sal->symtab != NULL)
10048 {
10049 const struct blockvector *bv;
10050 const struct block *b;
10051 struct symbol *sym;
10052
10053 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
10054 SYMTAB_COMPUNIT (sal->symtab));
10055 if (bv != NULL)
10056 {
10057 sym = block_linkage_function (b);
10058 if (sym != NULL)
10059 {
10060 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
10061 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
10062 sym);
10063 }
10064 else
10065 {
10066 /* It really is worthwhile to have the section, so we'll
10067 just have to look harder. This case can be executed
10068 if we have line numbers but no functions (as can
10069 happen in assembly source). */
10070
10071 struct bound_minimal_symbol msym;
10072 struct cleanup *old_chain = save_current_space_and_thread ();
10073
10074 switch_to_program_space_and_thread (sal->pspace);
10075
10076 msym = lookup_minimal_symbol_by_pc (sal->pc);
10077 if (msym.minsym)
10078 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
10079
10080 do_cleanups (old_chain);
10081 }
10082 }
10083 }
10084 }
10085
10086 void
10087 break_command (char *arg, int from_tty)
10088 {
10089 break_command_1 (arg, 0, from_tty);
10090 }
10091
10092 void
10093 tbreak_command (char *arg, int from_tty)
10094 {
10095 break_command_1 (arg, BP_TEMPFLAG, from_tty);
10096 }
10097
10098 static void
10099 hbreak_command (char *arg, int from_tty)
10100 {
10101 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
10102 }
10103
10104 static void
10105 thbreak_command (char *arg, int from_tty)
10106 {
10107 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
10108 }
10109
10110 static void
10111 stop_command (char *arg, int from_tty)
10112 {
10113 printf_filtered (_("Specify the type of breakpoint to set.\n\
10114 Usage: stop in <function | address>\n\
10115 stop at <line>\n"));
10116 }
10117
10118 static void
10119 stopin_command (char *arg, int from_tty)
10120 {
10121 int badInput = 0;
10122
10123 if (arg == (char *) NULL)
10124 badInput = 1;
10125 else if (*arg != '*')
10126 {
10127 char *argptr = arg;
10128 int hasColon = 0;
10129
10130 /* Look for a ':'. If this is a line number specification, then
10131 say it is bad, otherwise, it should be an address or
10132 function/method name. */
10133 while (*argptr && !hasColon)
10134 {
10135 hasColon = (*argptr == ':');
10136 argptr++;
10137 }
10138
10139 if (hasColon)
10140 badInput = (*argptr != ':'); /* Not a class::method */
10141 else
10142 badInput = isdigit (*arg); /* a simple line number */
10143 }
10144
10145 if (badInput)
10146 printf_filtered (_("Usage: stop in <function | address>\n"));
10147 else
10148 break_command_1 (arg, 0, from_tty);
10149 }
10150
10151 static void
10152 stopat_command (char *arg, int from_tty)
10153 {
10154 int badInput = 0;
10155
10156 if (arg == (char *) NULL || *arg == '*') /* no line number */
10157 badInput = 1;
10158 else
10159 {
10160 char *argptr = arg;
10161 int hasColon = 0;
10162
10163 /* Look for a ':'. If there is a '::' then get out, otherwise
10164 it is probably a line number. */
10165 while (*argptr && !hasColon)
10166 {
10167 hasColon = (*argptr == ':');
10168 argptr++;
10169 }
10170
10171 if (hasColon)
10172 badInput = (*argptr == ':'); /* we have class::method */
10173 else
10174 badInput = !isdigit (*arg); /* not a line number */
10175 }
10176
10177 if (badInput)
10178 printf_filtered (_("Usage: stop at <line>\n"));
10179 else
10180 break_command_1 (arg, 0, from_tty);
10181 }
10182
10183 /* The dynamic printf command is mostly like a regular breakpoint, but
10184 with a prewired command list consisting of a single output command,
10185 built from extra arguments supplied on the dprintf command
10186 line. */
10187
10188 static void
10189 dprintf_command (char *arg, int from_tty)
10190 {
10191 struct event_location *location;
10192 struct cleanup *cleanup;
10193
10194 location = string_to_event_location (&arg, current_language);
10195 cleanup = make_cleanup_delete_event_location (location);
10196
10197 /* If non-NULL, ARG should have been advanced past the location;
10198 the next character must be ','. */
10199 if (arg != NULL)
10200 {
10201 if (arg[0] != ',' || arg[1] == '\0')
10202 error (_("Format string required"));
10203 else
10204 {
10205 /* Skip the comma. */
10206 ++arg;
10207 }
10208 }
10209
10210 create_breakpoint (get_current_arch (),
10211 location,
10212 NULL, 0, arg, 1 /* parse arg */,
10213 0, bp_dprintf,
10214 0 /* Ignore count */,
10215 pending_break_support,
10216 &dprintf_breakpoint_ops,
10217 from_tty,
10218 1 /* enabled */,
10219 0 /* internal */,
10220 0);
10221 do_cleanups (cleanup);
10222 }
10223
10224 static void
10225 agent_printf_command (char *arg, int from_tty)
10226 {
10227 error (_("May only run agent-printf on the target"));
10228 }
10229
10230 /* Implement the "breakpoint_hit" breakpoint_ops method for
10231 ranged breakpoints. */
10232
10233 static int
10234 breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
10235 struct address_space *aspace,
10236 CORE_ADDR bp_addr,
10237 const struct target_waitstatus *ws)
10238 {
10239 if (ws->kind != TARGET_WAITKIND_STOPPED
10240 || ws->value.sig != GDB_SIGNAL_TRAP)
10241 return 0;
10242
10243 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
10244 bl->length, aspace, bp_addr);
10245 }
10246
10247 /* Implement the "resources_needed" breakpoint_ops method for
10248 ranged breakpoints. */
10249
10250 static int
10251 resources_needed_ranged_breakpoint (const struct bp_location *bl)
10252 {
10253 return target_ranged_break_num_registers ();
10254 }
10255
10256 /* Implement the "print_it" breakpoint_ops method for
10257 ranged breakpoints. */
10258
10259 static enum print_stop_action
10260 print_it_ranged_breakpoint (bpstat bs)
10261 {
10262 struct breakpoint *b = bs->breakpoint_at;
10263 struct bp_location *bl = b->loc;
10264 struct ui_out *uiout = current_uiout;
10265
10266 gdb_assert (b->type == bp_hardware_breakpoint);
10267
10268 /* Ranged breakpoints have only one location. */
10269 gdb_assert (bl && bl->next == NULL);
10270
10271 annotate_breakpoint (b->number);
10272
10273 maybe_print_thread_hit_breakpoint (uiout);
10274
10275 if (b->disposition == disp_del)
10276 ui_out_text (uiout, "Temporary ranged breakpoint ");
10277 else
10278 ui_out_text (uiout, "Ranged breakpoint ");
10279 if (ui_out_is_mi_like_p (uiout))
10280 {
10281 ui_out_field_string (uiout, "reason",
10282 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
10283 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
10284 }
10285 ui_out_field_int (uiout, "bkptno", b->number);
10286 ui_out_text (uiout, ", ");
10287
10288 return PRINT_SRC_AND_LOC;
10289 }
10290
10291 /* Implement the "print_one" breakpoint_ops method for
10292 ranged breakpoints. */
10293
10294 static void
10295 print_one_ranged_breakpoint (struct breakpoint *b,
10296 struct bp_location **last_loc)
10297 {
10298 struct bp_location *bl = b->loc;
10299 struct value_print_options opts;
10300 struct ui_out *uiout = current_uiout;
10301
10302 /* Ranged breakpoints have only one location. */
10303 gdb_assert (bl && bl->next == NULL);
10304
10305 get_user_print_options (&opts);
10306
10307 if (opts.addressprint)
10308 /* We don't print the address range here, it will be printed later
10309 by print_one_detail_ranged_breakpoint. */
10310 ui_out_field_skip (uiout, "addr");
10311 annotate_field (5);
10312 print_breakpoint_location (b, bl);
10313 *last_loc = bl;
10314 }
10315
10316 /* Implement the "print_one_detail" breakpoint_ops method for
10317 ranged breakpoints. */
10318
10319 static void
10320 print_one_detail_ranged_breakpoint (const struct breakpoint *b,
10321 struct ui_out *uiout)
10322 {
10323 CORE_ADDR address_start, address_end;
10324 struct bp_location *bl = b->loc;
10325 struct ui_file *stb = mem_fileopen ();
10326 struct cleanup *cleanup = make_cleanup_ui_file_delete (stb);
10327
10328 gdb_assert (bl);
10329
10330 address_start = bl->address;
10331 address_end = address_start + bl->length - 1;
10332
10333 ui_out_text (uiout, "\taddress range: ");
10334 fprintf_unfiltered (stb, "[%s, %s]",
10335 print_core_address (bl->gdbarch, address_start),
10336 print_core_address (bl->gdbarch, address_end));
10337 ui_out_field_stream (uiout, "addr", stb);
10338 ui_out_text (uiout, "\n");
10339
10340 do_cleanups (cleanup);
10341 }
10342
10343 /* Implement the "print_mention" breakpoint_ops method for
10344 ranged breakpoints. */
10345
10346 static void
10347 print_mention_ranged_breakpoint (struct breakpoint *b)
10348 {
10349 struct bp_location *bl = b->loc;
10350 struct ui_out *uiout = current_uiout;
10351
10352 gdb_assert (bl);
10353 gdb_assert (b->type == bp_hardware_breakpoint);
10354
10355 if (ui_out_is_mi_like_p (uiout))
10356 return;
10357
10358 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
10359 b->number, paddress (bl->gdbarch, bl->address),
10360 paddress (bl->gdbarch, bl->address + bl->length - 1));
10361 }
10362
10363 /* Implement the "print_recreate" breakpoint_ops method for
10364 ranged breakpoints. */
10365
10366 static void
10367 print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
10368 {
10369 fprintf_unfiltered (fp, "break-range %s, %s",
10370 event_location_to_string (b->location),
10371 event_location_to_string (b->location_range_end));
10372 print_recreate_thread (b, fp);
10373 }
10374
10375 /* The breakpoint_ops structure to be used in ranged breakpoints. */
10376
10377 static struct breakpoint_ops ranged_breakpoint_ops;
10378
10379 /* Find the address where the end of the breakpoint range should be
10380 placed, given the SAL of the end of the range. This is so that if
10381 the user provides a line number, the end of the range is set to the
10382 last instruction of the given line. */
10383
10384 static CORE_ADDR
10385 find_breakpoint_range_end (struct symtab_and_line sal)
10386 {
10387 CORE_ADDR end;
10388
10389 /* If the user provided a PC value, use it. Otherwise,
10390 find the address of the end of the given location. */
10391 if (sal.explicit_pc)
10392 end = sal.pc;
10393 else
10394 {
10395 int ret;
10396 CORE_ADDR start;
10397
10398 ret = find_line_pc_range (sal, &start, &end);
10399 if (!ret)
10400 error (_("Could not find location of the end of the range."));
10401
10402 /* find_line_pc_range returns the start of the next line. */
10403 end--;
10404 }
10405
10406 return end;
10407 }
10408
10409 /* Implement the "break-range" CLI command. */
10410
10411 static void
10412 break_range_command (char *arg, int from_tty)
10413 {
10414 char *arg_start, *addr_string_start;
10415 struct linespec_result canonical_start, canonical_end;
10416 int bp_count, can_use_bp, length;
10417 CORE_ADDR end;
10418 struct breakpoint *b;
10419 struct symtab_and_line sal_start, sal_end;
10420 struct cleanup *cleanup_bkpt;
10421 struct linespec_sals *lsal_start, *lsal_end;
10422 struct event_location *start_location, *end_location;
10423
10424 /* We don't support software ranged breakpoints. */
10425 if (target_ranged_break_num_registers () < 0)
10426 error (_("This target does not support hardware ranged breakpoints."));
10427
10428 bp_count = hw_breakpoint_used_count ();
10429 bp_count += target_ranged_break_num_registers ();
10430 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10431 bp_count, 0);
10432 if (can_use_bp < 0)
10433 error (_("Hardware breakpoints used exceeds limit."));
10434
10435 arg = skip_spaces (arg);
10436 if (arg == NULL || arg[0] == '\0')
10437 error(_("No address range specified."));
10438
10439 init_linespec_result (&canonical_start);
10440
10441 arg_start = arg;
10442 start_location = string_to_event_location (&arg, current_language);
10443 cleanup_bkpt = make_cleanup_delete_event_location (start_location);
10444 parse_breakpoint_sals (start_location, &canonical_start);
10445 make_cleanup_destroy_linespec_result (&canonical_start);
10446
10447 if (arg[0] != ',')
10448 error (_("Too few arguments."));
10449 else if (VEC_empty (linespec_sals, canonical_start.sals))
10450 error (_("Could not find location of the beginning of the range."));
10451
10452 lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
10453
10454 if (VEC_length (linespec_sals, canonical_start.sals) > 1
10455 || lsal_start->sals.nelts != 1)
10456 error (_("Cannot create a ranged breakpoint with multiple locations."));
10457
10458 sal_start = lsal_start->sals.sals[0];
10459 addr_string_start = savestring (arg_start, arg - arg_start);
10460 make_cleanup (xfree, addr_string_start);
10461
10462 arg++; /* Skip the comma. */
10463 arg = skip_spaces (arg);
10464
10465 /* Parse the end location. */
10466
10467 init_linespec_result (&canonical_end);
10468 arg_start = arg;
10469
10470 /* We call decode_line_full directly here instead of using
10471 parse_breakpoint_sals because we need to specify the start location's
10472 symtab and line as the default symtab and line for the end of the
10473 range. This makes it possible to have ranges like "foo.c:27, +14",
10474 where +14 means 14 lines from the start location. */
10475 end_location = string_to_event_location (&arg, current_language);
10476 make_cleanup_delete_event_location (end_location);
10477 decode_line_full (end_location, DECODE_LINE_FUNFIRSTLINE, NULL,
10478 sal_start.symtab, sal_start.line,
10479 &canonical_end, NULL, NULL);
10480
10481 make_cleanup_destroy_linespec_result (&canonical_end);
10482
10483 if (VEC_empty (linespec_sals, canonical_end.sals))
10484 error (_("Could not find location of the end of the range."));
10485
10486 lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
10487 if (VEC_length (linespec_sals, canonical_end.sals) > 1
10488 || lsal_end->sals.nelts != 1)
10489 error (_("Cannot create a ranged breakpoint with multiple locations."));
10490
10491 sal_end = lsal_end->sals.sals[0];
10492
10493 end = find_breakpoint_range_end (sal_end);
10494 if (sal_start.pc > end)
10495 error (_("Invalid address range, end precedes start."));
10496
10497 length = end - sal_start.pc + 1;
10498 if (length < 0)
10499 /* Length overflowed. */
10500 error (_("Address range too large."));
10501 else if (length == 1)
10502 {
10503 /* This range is simple enough to be handled by
10504 the `hbreak' command. */
10505 hbreak_command (addr_string_start, 1);
10506
10507 do_cleanups (cleanup_bkpt);
10508
10509 return;
10510 }
10511
10512 /* Now set up the breakpoint. */
10513 b = set_raw_breakpoint (get_current_arch (), sal_start,
10514 bp_hardware_breakpoint, &ranged_breakpoint_ops);
10515 set_breakpoint_count (breakpoint_count + 1);
10516 b->number = breakpoint_count;
10517 b->disposition = disp_donttouch;
10518 b->location = copy_event_location (start_location);
10519 b->location_range_end = copy_event_location (end_location);
10520 b->loc->length = length;
10521
10522 do_cleanups (cleanup_bkpt);
10523
10524 mention (b);
10525 observer_notify_breakpoint_created (b);
10526 update_global_location_list (UGLL_MAY_INSERT);
10527 }
10528
10529 /* Return non-zero if EXP is verified as constant. Returned zero
10530 means EXP is variable. Also the constant detection may fail for
10531 some constant expressions and in such case still falsely return
10532 zero. */
10533
10534 static int
10535 watchpoint_exp_is_const (const struct expression *exp)
10536 {
10537 int i = exp->nelts;
10538
10539 while (i > 0)
10540 {
10541 int oplenp, argsp;
10542
10543 /* We are only interested in the descriptor of each element. */
10544 operator_length (exp, i, &oplenp, &argsp);
10545 i -= oplenp;
10546
10547 switch (exp->elts[i].opcode)
10548 {
10549 case BINOP_ADD:
10550 case BINOP_SUB:
10551 case BINOP_MUL:
10552 case BINOP_DIV:
10553 case BINOP_REM:
10554 case BINOP_MOD:
10555 case BINOP_LSH:
10556 case BINOP_RSH:
10557 case BINOP_LOGICAL_AND:
10558 case BINOP_LOGICAL_OR:
10559 case BINOP_BITWISE_AND:
10560 case BINOP_BITWISE_IOR:
10561 case BINOP_BITWISE_XOR:
10562 case BINOP_EQUAL:
10563 case BINOP_NOTEQUAL:
10564 case BINOP_LESS:
10565 case BINOP_GTR:
10566 case BINOP_LEQ:
10567 case BINOP_GEQ:
10568 case BINOP_REPEAT:
10569 case BINOP_COMMA:
10570 case BINOP_EXP:
10571 case BINOP_MIN:
10572 case BINOP_MAX:
10573 case BINOP_INTDIV:
10574 case BINOP_CONCAT:
10575 case TERNOP_COND:
10576 case TERNOP_SLICE:
10577
10578 case OP_LONG:
10579 case OP_DOUBLE:
10580 case OP_DECFLOAT:
10581 case OP_LAST:
10582 case OP_COMPLEX:
10583 case OP_STRING:
10584 case OP_ARRAY:
10585 case OP_TYPE:
10586 case OP_TYPEOF:
10587 case OP_DECLTYPE:
10588 case OP_TYPEID:
10589 case OP_NAME:
10590 case OP_OBJC_NSSTRING:
10591
10592 case UNOP_NEG:
10593 case UNOP_LOGICAL_NOT:
10594 case UNOP_COMPLEMENT:
10595 case UNOP_ADDR:
10596 case UNOP_HIGH:
10597 case UNOP_CAST:
10598
10599 case UNOP_CAST_TYPE:
10600 case UNOP_REINTERPRET_CAST:
10601 case UNOP_DYNAMIC_CAST:
10602 /* Unary, binary and ternary operators: We have to check
10603 their operands. If they are constant, then so is the
10604 result of that operation. For instance, if A and B are
10605 determined to be constants, then so is "A + B".
10606
10607 UNOP_IND is one exception to the rule above, because the
10608 value of *ADDR is not necessarily a constant, even when
10609 ADDR is. */
10610 break;
10611
10612 case OP_VAR_VALUE:
10613 /* Check whether the associated symbol is a constant.
10614
10615 We use SYMBOL_CLASS rather than TYPE_CONST because it's
10616 possible that a buggy compiler could mark a variable as
10617 constant even when it is not, and TYPE_CONST would return
10618 true in this case, while SYMBOL_CLASS wouldn't.
10619
10620 We also have to check for function symbols because they
10621 are always constant. */
10622 {
10623 struct symbol *s = exp->elts[i + 2].symbol;
10624
10625 if (SYMBOL_CLASS (s) != LOC_BLOCK
10626 && SYMBOL_CLASS (s) != LOC_CONST
10627 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10628 return 0;
10629 break;
10630 }
10631
10632 /* The default action is to return 0 because we are using
10633 the optimistic approach here: If we don't know something,
10634 then it is not a constant. */
10635 default:
10636 return 0;
10637 }
10638 }
10639
10640 return 1;
10641 }
10642
10643 /* Implement the "dtor" breakpoint_ops method for watchpoints. */
10644
10645 static void
10646 dtor_watchpoint (struct breakpoint *self)
10647 {
10648 struct watchpoint *w = (struct watchpoint *) self;
10649
10650 xfree (w->cond_exp);
10651 xfree (w->exp);
10652 xfree (w->exp_string);
10653 xfree (w->exp_string_reparse);
10654 value_free (w->val);
10655
10656 base_breakpoint_ops.dtor (self);
10657 }
10658
10659 /* Implement the "re_set" breakpoint_ops method for watchpoints. */
10660
10661 static void
10662 re_set_watchpoint (struct breakpoint *b)
10663 {
10664 struct watchpoint *w = (struct watchpoint *) b;
10665
10666 /* Watchpoint can be either on expression using entirely global
10667 variables, or it can be on local variables.
10668
10669 Watchpoints of the first kind are never auto-deleted, and even
10670 persist across program restarts. Since they can use variables
10671 from shared libraries, we need to reparse expression as libraries
10672 are loaded and unloaded.
10673
10674 Watchpoints on local variables can also change meaning as result
10675 of solib event. For example, if a watchpoint uses both a local
10676 and a global variables in expression, it's a local watchpoint,
10677 but unloading of a shared library will make the expression
10678 invalid. This is not a very common use case, but we still
10679 re-evaluate expression, to avoid surprises to the user.
10680
10681 Note that for local watchpoints, we re-evaluate it only if
10682 watchpoints frame id is still valid. If it's not, it means the
10683 watchpoint is out of scope and will be deleted soon. In fact,
10684 I'm not sure we'll ever be called in this case.
10685
10686 If a local watchpoint's frame id is still valid, then
10687 w->exp_valid_block is likewise valid, and we can safely use it.
10688
10689 Don't do anything about disabled watchpoints, since they will be
10690 reevaluated again when enabled. */
10691 update_watchpoint (w, 1 /* reparse */);
10692 }
10693
10694 /* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10695
10696 static int
10697 insert_watchpoint (struct bp_location *bl)
10698 {
10699 struct watchpoint *w = (struct watchpoint *) bl->owner;
10700 int length = w->exact ? 1 : bl->length;
10701
10702 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
10703 w->cond_exp);
10704 }
10705
10706 /* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10707
10708 static int
10709 remove_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10710 {
10711 struct watchpoint *w = (struct watchpoint *) bl->owner;
10712 int length = w->exact ? 1 : bl->length;
10713
10714 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
10715 w->cond_exp);
10716 }
10717
10718 static int
10719 breakpoint_hit_watchpoint (const struct bp_location *bl,
10720 struct address_space *aspace, CORE_ADDR bp_addr,
10721 const struct target_waitstatus *ws)
10722 {
10723 struct breakpoint *b = bl->owner;
10724 struct watchpoint *w = (struct watchpoint *) b;
10725
10726 /* Continuable hardware watchpoints are treated as non-existent if the
10727 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10728 some data address). Otherwise gdb won't stop on a break instruction
10729 in the code (not from a breakpoint) when a hardware watchpoint has
10730 been defined. Also skip watchpoints which we know did not trigger
10731 (did not match the data address). */
10732 if (is_hardware_watchpoint (b)
10733 && w->watchpoint_triggered == watch_triggered_no)
10734 return 0;
10735
10736 return 1;
10737 }
10738
10739 static void
10740 check_status_watchpoint (bpstat bs)
10741 {
10742 gdb_assert (is_watchpoint (bs->breakpoint_at));
10743
10744 bpstat_check_watchpoint (bs);
10745 }
10746
10747 /* Implement the "resources_needed" breakpoint_ops method for
10748 hardware watchpoints. */
10749
10750 static int
10751 resources_needed_watchpoint (const struct bp_location *bl)
10752 {
10753 struct watchpoint *w = (struct watchpoint *) bl->owner;
10754 int length = w->exact? 1 : bl->length;
10755
10756 return target_region_ok_for_hw_watchpoint (bl->address, length);
10757 }
10758
10759 /* Implement the "works_in_software_mode" breakpoint_ops method for
10760 hardware watchpoints. */
10761
10762 static int
10763 works_in_software_mode_watchpoint (const struct breakpoint *b)
10764 {
10765 /* Read and access watchpoints only work with hardware support. */
10766 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
10767 }
10768
10769 static enum print_stop_action
10770 print_it_watchpoint (bpstat bs)
10771 {
10772 struct cleanup *old_chain;
10773 struct breakpoint *b;
10774 struct ui_file *stb;
10775 enum print_stop_action result;
10776 struct watchpoint *w;
10777 struct ui_out *uiout = current_uiout;
10778
10779 gdb_assert (bs->bp_location_at != NULL);
10780
10781 b = bs->breakpoint_at;
10782 w = (struct watchpoint *) b;
10783
10784 stb = mem_fileopen ();
10785 old_chain = make_cleanup_ui_file_delete (stb);
10786
10787 annotate_watchpoint (b->number);
10788 maybe_print_thread_hit_breakpoint (uiout);
10789
10790 switch (b->type)
10791 {
10792 case bp_watchpoint:
10793 case bp_hardware_watchpoint:
10794 if (ui_out_is_mi_like_p (uiout))
10795 ui_out_field_string
10796 (uiout, "reason",
10797 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10798 mention (b);
10799 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10800 ui_out_text (uiout, "\nOld value = ");
10801 watchpoint_value_print (bs->old_val, stb);
10802 ui_out_field_stream (uiout, "old", stb);
10803 ui_out_text (uiout, "\nNew value = ");
10804 watchpoint_value_print (w->val, stb);
10805 ui_out_field_stream (uiout, "new", stb);
10806 ui_out_text (uiout, "\n");
10807 /* More than one watchpoint may have been triggered. */
10808 result = PRINT_UNKNOWN;
10809 break;
10810
10811 case bp_read_watchpoint:
10812 if (ui_out_is_mi_like_p (uiout))
10813 ui_out_field_string
10814 (uiout, "reason",
10815 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10816 mention (b);
10817 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10818 ui_out_text (uiout, "\nValue = ");
10819 watchpoint_value_print (w->val, stb);
10820 ui_out_field_stream (uiout, "value", stb);
10821 ui_out_text (uiout, "\n");
10822 result = PRINT_UNKNOWN;
10823 break;
10824
10825 case bp_access_watchpoint:
10826 if (bs->old_val != NULL)
10827 {
10828 if (ui_out_is_mi_like_p (uiout))
10829 ui_out_field_string
10830 (uiout, "reason",
10831 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10832 mention (b);
10833 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10834 ui_out_text (uiout, "\nOld value = ");
10835 watchpoint_value_print (bs->old_val, stb);
10836 ui_out_field_stream (uiout, "old", stb);
10837 ui_out_text (uiout, "\nNew value = ");
10838 }
10839 else
10840 {
10841 mention (b);
10842 if (ui_out_is_mi_like_p (uiout))
10843 ui_out_field_string
10844 (uiout, "reason",
10845 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10846 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10847 ui_out_text (uiout, "\nValue = ");
10848 }
10849 watchpoint_value_print (w->val, stb);
10850 ui_out_field_stream (uiout, "new", stb);
10851 ui_out_text (uiout, "\n");
10852 result = PRINT_UNKNOWN;
10853 break;
10854 default:
10855 result = PRINT_UNKNOWN;
10856 }
10857
10858 do_cleanups (old_chain);
10859 return result;
10860 }
10861
10862 /* Implement the "print_mention" breakpoint_ops method for hardware
10863 watchpoints. */
10864
10865 static void
10866 print_mention_watchpoint (struct breakpoint *b)
10867 {
10868 struct cleanup *ui_out_chain;
10869 struct watchpoint *w = (struct watchpoint *) b;
10870 struct ui_out *uiout = current_uiout;
10871
10872 switch (b->type)
10873 {
10874 case bp_watchpoint:
10875 ui_out_text (uiout, "Watchpoint ");
10876 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10877 break;
10878 case bp_hardware_watchpoint:
10879 ui_out_text (uiout, "Hardware watchpoint ");
10880 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10881 break;
10882 case bp_read_watchpoint:
10883 ui_out_text (uiout, "Hardware read watchpoint ");
10884 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
10885 break;
10886 case bp_access_watchpoint:
10887 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
10888 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
10889 break;
10890 default:
10891 internal_error (__FILE__, __LINE__,
10892 _("Invalid hardware watchpoint type."));
10893 }
10894
10895 ui_out_field_int (uiout, "number", b->number);
10896 ui_out_text (uiout, ": ");
10897 ui_out_field_string (uiout, "exp", w->exp_string);
10898 do_cleanups (ui_out_chain);
10899 }
10900
10901 /* Implement the "print_recreate" breakpoint_ops method for
10902 watchpoints. */
10903
10904 static void
10905 print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10906 {
10907 struct watchpoint *w = (struct watchpoint *) b;
10908
10909 switch (b->type)
10910 {
10911 case bp_watchpoint:
10912 case bp_hardware_watchpoint:
10913 fprintf_unfiltered (fp, "watch");
10914 break;
10915 case bp_read_watchpoint:
10916 fprintf_unfiltered (fp, "rwatch");
10917 break;
10918 case bp_access_watchpoint:
10919 fprintf_unfiltered (fp, "awatch");
10920 break;
10921 default:
10922 internal_error (__FILE__, __LINE__,
10923 _("Invalid watchpoint type."));
10924 }
10925
10926 fprintf_unfiltered (fp, " %s", w->exp_string);
10927 print_recreate_thread (b, fp);
10928 }
10929
10930 /* Implement the "explains_signal" breakpoint_ops method for
10931 watchpoints. */
10932
10933 static int
10934 explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10935 {
10936 /* A software watchpoint cannot cause a signal other than
10937 GDB_SIGNAL_TRAP. */
10938 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
10939 return 0;
10940
10941 return 1;
10942 }
10943
10944 /* The breakpoint_ops structure to be used in hardware watchpoints. */
10945
10946 static struct breakpoint_ops watchpoint_breakpoint_ops;
10947
10948 /* Implement the "insert" breakpoint_ops method for
10949 masked hardware watchpoints. */
10950
10951 static int
10952 insert_masked_watchpoint (struct bp_location *bl)
10953 {
10954 struct watchpoint *w = (struct watchpoint *) bl->owner;
10955
10956 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
10957 bl->watchpoint_type);
10958 }
10959
10960 /* Implement the "remove" breakpoint_ops method for
10961 masked hardware watchpoints. */
10962
10963 static int
10964 remove_masked_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10965 {
10966 struct watchpoint *w = (struct watchpoint *) bl->owner;
10967
10968 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
10969 bl->watchpoint_type);
10970 }
10971
10972 /* Implement the "resources_needed" breakpoint_ops method for
10973 masked hardware watchpoints. */
10974
10975 static int
10976 resources_needed_masked_watchpoint (const struct bp_location *bl)
10977 {
10978 struct watchpoint *w = (struct watchpoint *) bl->owner;
10979
10980 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
10981 }
10982
10983 /* Implement the "works_in_software_mode" breakpoint_ops method for
10984 masked hardware watchpoints. */
10985
10986 static int
10987 works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10988 {
10989 return 0;
10990 }
10991
10992 /* Implement the "print_it" breakpoint_ops method for
10993 masked hardware watchpoints. */
10994
10995 static enum print_stop_action
10996 print_it_masked_watchpoint (bpstat bs)
10997 {
10998 struct breakpoint *b = bs->breakpoint_at;
10999 struct ui_out *uiout = current_uiout;
11000
11001 /* Masked watchpoints have only one location. */
11002 gdb_assert (b->loc && b->loc->next == NULL);
11003
11004 annotate_watchpoint (b->number);
11005 maybe_print_thread_hit_breakpoint (uiout);
11006
11007 switch (b->type)
11008 {
11009 case bp_hardware_watchpoint:
11010 if (ui_out_is_mi_like_p (uiout))
11011 ui_out_field_string
11012 (uiout, "reason",
11013 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
11014 break;
11015
11016 case bp_read_watchpoint:
11017 if (ui_out_is_mi_like_p (uiout))
11018 ui_out_field_string
11019 (uiout, "reason",
11020 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
11021 break;
11022
11023 case bp_access_watchpoint:
11024 if (ui_out_is_mi_like_p (uiout))
11025 ui_out_field_string
11026 (uiout, "reason",
11027 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
11028 break;
11029 default:
11030 internal_error (__FILE__, __LINE__,
11031 _("Invalid hardware watchpoint type."));
11032 }
11033
11034 mention (b);
11035 ui_out_text (uiout, _("\n\
11036 Check the underlying instruction at PC for the memory\n\
11037 address and value which triggered this watchpoint.\n"));
11038 ui_out_text (uiout, "\n");
11039
11040 /* More than one watchpoint may have been triggered. */
11041 return PRINT_UNKNOWN;
11042 }
11043
11044 /* Implement the "print_one_detail" breakpoint_ops method for
11045 masked hardware watchpoints. */
11046
11047 static void
11048 print_one_detail_masked_watchpoint (const struct breakpoint *b,
11049 struct ui_out *uiout)
11050 {
11051 struct watchpoint *w = (struct watchpoint *) b;
11052
11053 /* Masked watchpoints have only one location. */
11054 gdb_assert (b->loc && b->loc->next == NULL);
11055
11056 ui_out_text (uiout, "\tmask ");
11057 ui_out_field_core_addr (uiout, "mask", b->loc->gdbarch, w->hw_wp_mask);
11058 ui_out_text (uiout, "\n");
11059 }
11060
11061 /* Implement the "print_mention" breakpoint_ops method for
11062 masked hardware watchpoints. */
11063
11064 static void
11065 print_mention_masked_watchpoint (struct breakpoint *b)
11066 {
11067 struct watchpoint *w = (struct watchpoint *) b;
11068 struct ui_out *uiout = current_uiout;
11069 struct cleanup *ui_out_chain;
11070
11071 switch (b->type)
11072 {
11073 case bp_hardware_watchpoint:
11074 ui_out_text (uiout, "Masked hardware watchpoint ");
11075 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
11076 break;
11077 case bp_read_watchpoint:
11078 ui_out_text (uiout, "Masked hardware read watchpoint ");
11079 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
11080 break;
11081 case bp_access_watchpoint:
11082 ui_out_text (uiout, "Masked hardware access (read/write) watchpoint ");
11083 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
11084 break;
11085 default:
11086 internal_error (__FILE__, __LINE__,
11087 _("Invalid hardware watchpoint type."));
11088 }
11089
11090 ui_out_field_int (uiout, "number", b->number);
11091 ui_out_text (uiout, ": ");
11092 ui_out_field_string (uiout, "exp", w->exp_string);
11093 do_cleanups (ui_out_chain);
11094 }
11095
11096 /* Implement the "print_recreate" breakpoint_ops method for
11097 masked hardware watchpoints. */
11098
11099 static void
11100 print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
11101 {
11102 struct watchpoint *w = (struct watchpoint *) b;
11103 char tmp[40];
11104
11105 switch (b->type)
11106 {
11107 case bp_hardware_watchpoint:
11108 fprintf_unfiltered (fp, "watch");
11109 break;
11110 case bp_read_watchpoint:
11111 fprintf_unfiltered (fp, "rwatch");
11112 break;
11113 case bp_access_watchpoint:
11114 fprintf_unfiltered (fp, "awatch");
11115 break;
11116 default:
11117 internal_error (__FILE__, __LINE__,
11118 _("Invalid hardware watchpoint type."));
11119 }
11120
11121 sprintf_vma (tmp, w->hw_wp_mask);
11122 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
11123 print_recreate_thread (b, fp);
11124 }
11125
11126 /* The breakpoint_ops structure to be used in masked hardware watchpoints. */
11127
11128 static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
11129
11130 /* Tell whether the given watchpoint is a masked hardware watchpoint. */
11131
11132 static int
11133 is_masked_watchpoint (const struct breakpoint *b)
11134 {
11135 return b->ops == &masked_watchpoint_breakpoint_ops;
11136 }
11137
11138 /* accessflag: hw_write: watch write,
11139 hw_read: watch read,
11140 hw_access: watch access (read or write) */
11141 static void
11142 watch_command_1 (const char *arg, int accessflag, int from_tty,
11143 int just_location, int internal)
11144 {
11145 struct breakpoint *b, *scope_breakpoint = NULL;
11146 struct expression *exp;
11147 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
11148 struct value *val, *mark, *result;
11149 int saved_bitpos = 0, saved_bitsize = 0;
11150 struct frame_info *frame;
11151 const char *exp_start = NULL;
11152 const char *exp_end = NULL;
11153 const char *tok, *end_tok;
11154 int toklen = -1;
11155 const char *cond_start = NULL;
11156 const char *cond_end = NULL;
11157 enum bptype bp_type;
11158 int thread = -1;
11159 int pc = 0;
11160 /* Flag to indicate whether we are going to use masks for
11161 the hardware watchpoint. */
11162 int use_mask = 0;
11163 CORE_ADDR mask = 0;
11164 struct watchpoint *w;
11165 char *expression;
11166 struct cleanup *back_to;
11167
11168 /* Make sure that we actually have parameters to parse. */
11169 if (arg != NULL && arg[0] != '\0')
11170 {
11171 const char *value_start;
11172
11173 exp_end = arg + strlen (arg);
11174
11175 /* Look for "parameter value" pairs at the end
11176 of the arguments string. */
11177 for (tok = exp_end - 1; tok > arg; tok--)
11178 {
11179 /* Skip whitespace at the end of the argument list. */
11180 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11181 tok--;
11182
11183 /* Find the beginning of the last token.
11184 This is the value of the parameter. */
11185 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11186 tok--;
11187 value_start = tok + 1;
11188
11189 /* Skip whitespace. */
11190 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11191 tok--;
11192
11193 end_tok = tok;
11194
11195 /* Find the beginning of the second to last token.
11196 This is the parameter itself. */
11197 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11198 tok--;
11199 tok++;
11200 toklen = end_tok - tok + 1;
11201
11202 if (toklen == 6 && startswith (tok, "thread"))
11203 {
11204 struct thread_info *thr;
11205 /* At this point we've found a "thread" token, which means
11206 the user is trying to set a watchpoint that triggers
11207 only in a specific thread. */
11208 const char *endp;
11209
11210 if (thread != -1)
11211 error(_("You can specify only one thread."));
11212
11213 /* Extract the thread ID from the next token. */
11214 thr = parse_thread_id (value_start, &endp);
11215
11216 /* Check if the user provided a valid thread ID. */
11217 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
11218 invalid_thread_id_error (value_start);
11219
11220 thread = thr->global_num;
11221 }
11222 else if (toklen == 4 && startswith (tok, "mask"))
11223 {
11224 /* We've found a "mask" token, which means the user wants to
11225 create a hardware watchpoint that is going to have the mask
11226 facility. */
11227 struct value *mask_value, *mark;
11228
11229 if (use_mask)
11230 error(_("You can specify only one mask."));
11231
11232 use_mask = just_location = 1;
11233
11234 mark = value_mark ();
11235 mask_value = parse_to_comma_and_eval (&value_start);
11236 mask = value_as_address (mask_value);
11237 value_free_to_mark (mark);
11238 }
11239 else
11240 /* We didn't recognize what we found. We should stop here. */
11241 break;
11242
11243 /* Truncate the string and get rid of the "parameter value" pair before
11244 the arguments string is parsed by the parse_exp_1 function. */
11245 exp_end = tok;
11246 }
11247 }
11248 else
11249 exp_end = arg;
11250
11251 /* Parse the rest of the arguments. From here on out, everything
11252 is in terms of a newly allocated string instead of the original
11253 ARG. */
11254 innermost_block = NULL;
11255 expression = savestring (arg, exp_end - arg);
11256 back_to = make_cleanup (xfree, expression);
11257 exp_start = arg = expression;
11258 exp = parse_exp_1 (&arg, 0, 0, 0);
11259 exp_end = arg;
11260 /* Remove trailing whitespace from the expression before saving it.
11261 This makes the eventual display of the expression string a bit
11262 prettier. */
11263 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
11264 --exp_end;
11265
11266 /* Checking if the expression is not constant. */
11267 if (watchpoint_exp_is_const (exp))
11268 {
11269 int len;
11270
11271 len = exp_end - exp_start;
11272 while (len > 0 && isspace (exp_start[len - 1]))
11273 len--;
11274 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
11275 }
11276
11277 exp_valid_block = innermost_block;
11278 mark = value_mark ();
11279 fetch_subexp_value (exp, &pc, &val, &result, NULL, just_location);
11280
11281 if (val != NULL && just_location)
11282 {
11283 saved_bitpos = value_bitpos (val);
11284 saved_bitsize = value_bitsize (val);
11285 }
11286
11287 if (just_location)
11288 {
11289 int ret;
11290
11291 exp_valid_block = NULL;
11292 val = value_addr (result);
11293 release_value (val);
11294 value_free_to_mark (mark);
11295
11296 if (use_mask)
11297 {
11298 ret = target_masked_watch_num_registers (value_as_address (val),
11299 mask);
11300 if (ret == -1)
11301 error (_("This target does not support masked watchpoints."));
11302 else if (ret == -2)
11303 error (_("Invalid mask or memory region."));
11304 }
11305 }
11306 else if (val != NULL)
11307 release_value (val);
11308
11309 tok = skip_spaces_const (arg);
11310 end_tok = skip_to_space_const (tok);
11311
11312 toklen = end_tok - tok;
11313 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
11314 {
11315 struct expression *cond;
11316
11317 innermost_block = NULL;
11318 tok = cond_start = end_tok + 1;
11319 cond = parse_exp_1 (&tok, 0, 0, 0);
11320
11321 /* The watchpoint expression may not be local, but the condition
11322 may still be. E.g.: `watch global if local > 0'. */
11323 cond_exp_valid_block = innermost_block;
11324
11325 xfree (cond);
11326 cond_end = tok;
11327 }
11328 if (*tok)
11329 error (_("Junk at end of command."));
11330
11331 frame = block_innermost_frame (exp_valid_block);
11332
11333 /* If the expression is "local", then set up a "watchpoint scope"
11334 breakpoint at the point where we've left the scope of the watchpoint
11335 expression. Create the scope breakpoint before the watchpoint, so
11336 that we will encounter it first in bpstat_stop_status. */
11337 if (exp_valid_block && frame)
11338 {
11339 if (frame_id_p (frame_unwind_caller_id (frame)))
11340 {
11341 scope_breakpoint
11342 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
11343 frame_unwind_caller_pc (frame),
11344 bp_watchpoint_scope,
11345 &momentary_breakpoint_ops);
11346
11347 scope_breakpoint->enable_state = bp_enabled;
11348
11349 /* Automatically delete the breakpoint when it hits. */
11350 scope_breakpoint->disposition = disp_del;
11351
11352 /* Only break in the proper frame (help with recursion). */
11353 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
11354
11355 /* Set the address at which we will stop. */
11356 scope_breakpoint->loc->gdbarch
11357 = frame_unwind_caller_arch (frame);
11358 scope_breakpoint->loc->requested_address
11359 = frame_unwind_caller_pc (frame);
11360 scope_breakpoint->loc->address
11361 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
11362 scope_breakpoint->loc->requested_address,
11363 scope_breakpoint->type);
11364 }
11365 }
11366
11367 /* Now set up the breakpoint. We create all watchpoints as hardware
11368 watchpoints here even if hardware watchpoints are turned off, a call
11369 to update_watchpoint later in this function will cause the type to
11370 drop back to bp_watchpoint (software watchpoint) if required. */
11371
11372 if (accessflag == hw_read)
11373 bp_type = bp_read_watchpoint;
11374 else if (accessflag == hw_access)
11375 bp_type = bp_access_watchpoint;
11376 else
11377 bp_type = bp_hardware_watchpoint;
11378
11379 w = XCNEW (struct watchpoint);
11380 b = &w->base;
11381 if (use_mask)
11382 init_raw_breakpoint_without_location (b, NULL, bp_type,
11383 &masked_watchpoint_breakpoint_ops);
11384 else
11385 init_raw_breakpoint_without_location (b, NULL, bp_type,
11386 &watchpoint_breakpoint_ops);
11387 b->thread = thread;
11388 b->disposition = disp_donttouch;
11389 b->pspace = current_program_space;
11390 w->exp = exp;
11391 w->exp_valid_block = exp_valid_block;
11392 w->cond_exp_valid_block = cond_exp_valid_block;
11393 if (just_location)
11394 {
11395 struct type *t = value_type (val);
11396 CORE_ADDR addr = value_as_address (val);
11397 char *name;
11398
11399 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
11400 name = type_to_string (t);
11401
11402 w->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
11403 core_addr_to_string (addr));
11404 xfree (name);
11405
11406 w->exp_string = xstrprintf ("-location %.*s",
11407 (int) (exp_end - exp_start), exp_start);
11408
11409 /* The above expression is in C. */
11410 b->language = language_c;
11411 }
11412 else
11413 w->exp_string = savestring (exp_start, exp_end - exp_start);
11414
11415 if (use_mask)
11416 {
11417 w->hw_wp_mask = mask;
11418 }
11419 else
11420 {
11421 w->val = val;
11422 w->val_bitpos = saved_bitpos;
11423 w->val_bitsize = saved_bitsize;
11424 w->val_valid = 1;
11425 }
11426
11427 if (cond_start)
11428 b->cond_string = savestring (cond_start, cond_end - cond_start);
11429 else
11430 b->cond_string = 0;
11431
11432 if (frame)
11433 {
11434 w->watchpoint_frame = get_frame_id (frame);
11435 w->watchpoint_thread = inferior_ptid;
11436 }
11437 else
11438 {
11439 w->watchpoint_frame = null_frame_id;
11440 w->watchpoint_thread = null_ptid;
11441 }
11442
11443 if (scope_breakpoint != NULL)
11444 {
11445 /* The scope breakpoint is related to the watchpoint. We will
11446 need to act on them together. */
11447 b->related_breakpoint = scope_breakpoint;
11448 scope_breakpoint->related_breakpoint = b;
11449 }
11450
11451 if (!just_location)
11452 value_free_to_mark (mark);
11453
11454 TRY
11455 {
11456 /* Finally update the new watchpoint. This creates the locations
11457 that should be inserted. */
11458 update_watchpoint (w, 1);
11459 }
11460 CATCH (e, RETURN_MASK_ALL)
11461 {
11462 delete_breakpoint (b);
11463 throw_exception (e);
11464 }
11465 END_CATCH
11466
11467 install_breakpoint (internal, b, 1);
11468 do_cleanups (back_to);
11469 }
11470
11471 /* Return count of debug registers needed to watch the given expression.
11472 If the watchpoint cannot be handled in hardware return zero. */
11473
11474 static int
11475 can_use_hardware_watchpoint (struct value *v)
11476 {
11477 int found_memory_cnt = 0;
11478 struct value *head = v;
11479
11480 /* Did the user specifically forbid us to use hardware watchpoints? */
11481 if (!can_use_hw_watchpoints)
11482 return 0;
11483
11484 /* Make sure that the value of the expression depends only upon
11485 memory contents, and values computed from them within GDB. If we
11486 find any register references or function calls, we can't use a
11487 hardware watchpoint.
11488
11489 The idea here is that evaluating an expression generates a series
11490 of values, one holding the value of every subexpression. (The
11491 expression a*b+c has five subexpressions: a, b, a*b, c, and
11492 a*b+c.) GDB's values hold almost enough information to establish
11493 the criteria given above --- they identify memory lvalues,
11494 register lvalues, computed values, etcetera. So we can evaluate
11495 the expression, and then scan the chain of values that leaves
11496 behind to decide whether we can detect any possible change to the
11497 expression's final value using only hardware watchpoints.
11498
11499 However, I don't think that the values returned by inferior
11500 function calls are special in any way. So this function may not
11501 notice that an expression involving an inferior function call
11502 can't be watched with hardware watchpoints. FIXME. */
11503 for (; v; v = value_next (v))
11504 {
11505 if (VALUE_LVAL (v) == lval_memory)
11506 {
11507 if (v != head && value_lazy (v))
11508 /* A lazy memory lvalue in the chain is one that GDB never
11509 needed to fetch; we either just used its address (e.g.,
11510 `a' in `a.b') or we never needed it at all (e.g., `a'
11511 in `a,b'). This doesn't apply to HEAD; if that is
11512 lazy then it was not readable, but watch it anyway. */
11513 ;
11514 else
11515 {
11516 /* Ahh, memory we actually used! Check if we can cover
11517 it with hardware watchpoints. */
11518 struct type *vtype = check_typedef (value_type (v));
11519
11520 /* We only watch structs and arrays if user asked for it
11521 explicitly, never if they just happen to appear in a
11522 middle of some value chain. */
11523 if (v == head
11524 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11525 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11526 {
11527 CORE_ADDR vaddr = value_address (v);
11528 int len;
11529 int num_regs;
11530
11531 len = (target_exact_watchpoints
11532 && is_scalar_type_recursive (vtype))?
11533 1 : TYPE_LENGTH (value_type (v));
11534
11535 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11536 if (!num_regs)
11537 return 0;
11538 else
11539 found_memory_cnt += num_regs;
11540 }
11541 }
11542 }
11543 else if (VALUE_LVAL (v) != not_lval
11544 && deprecated_value_modifiable (v) == 0)
11545 return 0; /* These are values from the history (e.g., $1). */
11546 else if (VALUE_LVAL (v) == lval_register)
11547 return 0; /* Cannot watch a register with a HW watchpoint. */
11548 }
11549
11550 /* The expression itself looks suitable for using a hardware
11551 watchpoint, but give the target machine a chance to reject it. */
11552 return found_memory_cnt;
11553 }
11554
11555 void
11556 watch_command_wrapper (char *arg, int from_tty, int internal)
11557 {
11558 watch_command_1 (arg, hw_write, from_tty, 0, internal);
11559 }
11560
11561 /* A helper function that looks for the "-location" argument and then
11562 calls watch_command_1. */
11563
11564 static void
11565 watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11566 {
11567 int just_location = 0;
11568
11569 if (arg
11570 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11571 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11572 {
11573 arg = skip_spaces (arg);
11574 just_location = 1;
11575 }
11576
11577 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
11578 }
11579
11580 static void
11581 watch_command (char *arg, int from_tty)
11582 {
11583 watch_maybe_just_location (arg, hw_write, from_tty);
11584 }
11585
11586 void
11587 rwatch_command_wrapper (char *arg, int from_tty, int internal)
11588 {
11589 watch_command_1 (arg, hw_read, from_tty, 0, internal);
11590 }
11591
11592 static void
11593 rwatch_command (char *arg, int from_tty)
11594 {
11595 watch_maybe_just_location (arg, hw_read, from_tty);
11596 }
11597
11598 void
11599 awatch_command_wrapper (char *arg, int from_tty, int internal)
11600 {
11601 watch_command_1 (arg, hw_access, from_tty, 0, internal);
11602 }
11603
11604 static void
11605 awatch_command (char *arg, int from_tty)
11606 {
11607 watch_maybe_just_location (arg, hw_access, from_tty);
11608 }
11609 \f
11610
11611 /* Data for the FSM that manages the until(location)/advance commands
11612 in infcmd.c. Here because it uses the mechanisms of
11613 breakpoints. */
11614
11615 struct until_break_fsm
11616 {
11617 /* The base class. */
11618 struct thread_fsm thread_fsm;
11619
11620 /* The thread that as current when the command was executed. */
11621 int thread;
11622
11623 /* The breakpoint set at the destination location. */
11624 struct breakpoint *location_breakpoint;
11625
11626 /* Breakpoint set at the return address in the caller frame. May be
11627 NULL. */
11628 struct breakpoint *caller_breakpoint;
11629 };
11630
11631 static void until_break_fsm_clean_up (struct thread_fsm *self,
11632 struct thread_info *thread);
11633 static int until_break_fsm_should_stop (struct thread_fsm *self,
11634 struct thread_info *thread);
11635 static enum async_reply_reason
11636 until_break_fsm_async_reply_reason (struct thread_fsm *self);
11637
11638 /* until_break_fsm's vtable. */
11639
11640 static struct thread_fsm_ops until_break_fsm_ops =
11641 {
11642 NULL, /* dtor */
11643 until_break_fsm_clean_up,
11644 until_break_fsm_should_stop,
11645 NULL, /* return_value */
11646 until_break_fsm_async_reply_reason,
11647 };
11648
11649 /* Allocate a new until_break_command_fsm. */
11650
11651 static struct until_break_fsm *
11652 new_until_break_fsm (struct interp *cmd_interp, int thread,
11653 struct breakpoint *location_breakpoint,
11654 struct breakpoint *caller_breakpoint)
11655 {
11656 struct until_break_fsm *sm;
11657
11658 sm = XCNEW (struct until_break_fsm);
11659 thread_fsm_ctor (&sm->thread_fsm, &until_break_fsm_ops, cmd_interp);
11660
11661 sm->thread = thread;
11662 sm->location_breakpoint = location_breakpoint;
11663 sm->caller_breakpoint = caller_breakpoint;
11664
11665 return sm;
11666 }
11667
11668 /* Implementation of the 'should_stop' FSM method for the
11669 until(location)/advance commands. */
11670
11671 static int
11672 until_break_fsm_should_stop (struct thread_fsm *self,
11673 struct thread_info *tp)
11674 {
11675 struct until_break_fsm *sm = (struct until_break_fsm *) self;
11676
11677 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
11678 sm->location_breakpoint) != NULL
11679 || (sm->caller_breakpoint != NULL
11680 && bpstat_find_breakpoint (tp->control.stop_bpstat,
11681 sm->caller_breakpoint) != NULL))
11682 thread_fsm_set_finished (self);
11683
11684 return 1;
11685 }
11686
11687 /* Implementation of the 'clean_up' FSM method for the
11688 until(location)/advance commands. */
11689
11690 static void
11691 until_break_fsm_clean_up (struct thread_fsm *self,
11692 struct thread_info *thread)
11693 {
11694 struct until_break_fsm *sm = (struct until_break_fsm *) self;
11695
11696 /* Clean up our temporary breakpoints. */
11697 if (sm->location_breakpoint != NULL)
11698 {
11699 delete_breakpoint (sm->location_breakpoint);
11700 sm->location_breakpoint = NULL;
11701 }
11702 if (sm->caller_breakpoint != NULL)
11703 {
11704 delete_breakpoint (sm->caller_breakpoint);
11705 sm->caller_breakpoint = NULL;
11706 }
11707 delete_longjmp_breakpoint (sm->thread);
11708 }
11709
11710 /* Implementation of the 'async_reply_reason' FSM method for the
11711 until(location)/advance commands. */
11712
11713 static enum async_reply_reason
11714 until_break_fsm_async_reply_reason (struct thread_fsm *self)
11715 {
11716 return EXEC_ASYNC_LOCATION_REACHED;
11717 }
11718
11719 void
11720 until_break_command (char *arg, int from_tty, int anywhere)
11721 {
11722 struct symtabs_and_lines sals;
11723 struct symtab_and_line sal;
11724 struct frame_info *frame;
11725 struct gdbarch *frame_gdbarch;
11726 struct frame_id stack_frame_id;
11727 struct frame_id caller_frame_id;
11728 struct breakpoint *location_breakpoint;
11729 struct breakpoint *caller_breakpoint = NULL;
11730 struct cleanup *old_chain, *cleanup;
11731 int thread;
11732 struct thread_info *tp;
11733 struct event_location *location;
11734 struct until_break_fsm *sm;
11735
11736 clear_proceed_status (0);
11737
11738 /* Set a breakpoint where the user wants it and at return from
11739 this function. */
11740
11741 location = string_to_event_location (&arg, current_language);
11742 cleanup = make_cleanup_delete_event_location (location);
11743
11744 if (last_displayed_sal_is_valid ())
11745 sals = decode_line_1 (location, DECODE_LINE_FUNFIRSTLINE, NULL,
11746 get_last_displayed_symtab (),
11747 get_last_displayed_line ());
11748 else
11749 sals = decode_line_1 (location, DECODE_LINE_FUNFIRSTLINE,
11750 NULL, (struct symtab *) NULL, 0);
11751
11752 if (sals.nelts != 1)
11753 error (_("Couldn't get information on specified line."));
11754
11755 sal = sals.sals[0];
11756 xfree (sals.sals); /* malloc'd, so freed. */
11757
11758 if (*arg)
11759 error (_("Junk at end of arguments."));
11760
11761 resolve_sal_pc (&sal);
11762
11763 tp = inferior_thread ();
11764 thread = tp->global_num;
11765
11766 old_chain = make_cleanup (null_cleanup, NULL);
11767
11768 /* Note linespec handling above invalidates the frame chain.
11769 Installing a breakpoint also invalidates the frame chain (as it
11770 may need to switch threads), so do any frame handling before
11771 that. */
11772
11773 frame = get_selected_frame (NULL);
11774 frame_gdbarch = get_frame_arch (frame);
11775 stack_frame_id = get_stack_frame_id (frame);
11776 caller_frame_id = frame_unwind_caller_id (frame);
11777
11778 /* Keep within the current frame, or in frames called by the current
11779 one. */
11780
11781 if (frame_id_p (caller_frame_id))
11782 {
11783 struct symtab_and_line sal2;
11784 struct gdbarch *caller_gdbarch;
11785
11786 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11787 sal2.pc = frame_unwind_caller_pc (frame);
11788 caller_gdbarch = frame_unwind_caller_arch (frame);
11789 caller_breakpoint = set_momentary_breakpoint (caller_gdbarch,
11790 sal2,
11791 caller_frame_id,
11792 bp_until);
11793 make_cleanup_delete_breakpoint (caller_breakpoint);
11794
11795 set_longjmp_breakpoint (tp, caller_frame_id);
11796 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
11797 }
11798
11799 /* set_momentary_breakpoint could invalidate FRAME. */
11800 frame = NULL;
11801
11802 if (anywhere)
11803 /* If the user told us to continue until a specified location,
11804 we don't specify a frame at which we need to stop. */
11805 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11806 null_frame_id, bp_until);
11807 else
11808 /* Otherwise, specify the selected frame, because we want to stop
11809 only at the very same frame. */
11810 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11811 stack_frame_id, bp_until);
11812 make_cleanup_delete_breakpoint (location_breakpoint);
11813
11814 sm = new_until_break_fsm (command_interp (), tp->global_num,
11815 location_breakpoint, caller_breakpoint);
11816 tp->thread_fsm = &sm->thread_fsm;
11817
11818 discard_cleanups (old_chain);
11819
11820 proceed (-1, GDB_SIGNAL_DEFAULT);
11821
11822 do_cleanups (cleanup);
11823 }
11824
11825 /* This function attempts to parse an optional "if <cond>" clause
11826 from the arg string. If one is not found, it returns NULL.
11827
11828 Else, it returns a pointer to the condition string. (It does not
11829 attempt to evaluate the string against a particular block.) And,
11830 it updates arg to point to the first character following the parsed
11831 if clause in the arg string. */
11832
11833 char *
11834 ep_parse_optional_if_clause (char **arg)
11835 {
11836 char *cond_string;
11837
11838 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
11839 return NULL;
11840
11841 /* Skip the "if" keyword. */
11842 (*arg) += 2;
11843
11844 /* Skip any extra leading whitespace, and record the start of the
11845 condition string. */
11846 *arg = skip_spaces (*arg);
11847 cond_string = *arg;
11848
11849 /* Assume that the condition occupies the remainder of the arg
11850 string. */
11851 (*arg) += strlen (cond_string);
11852
11853 return cond_string;
11854 }
11855
11856 /* Commands to deal with catching events, such as signals, exceptions,
11857 process start/exit, etc. */
11858
11859 typedef enum
11860 {
11861 catch_fork_temporary, catch_vfork_temporary,
11862 catch_fork_permanent, catch_vfork_permanent
11863 }
11864 catch_fork_kind;
11865
11866 static void
11867 catch_fork_command_1 (char *arg, int from_tty,
11868 struct cmd_list_element *command)
11869 {
11870 struct gdbarch *gdbarch = get_current_arch ();
11871 char *cond_string = NULL;
11872 catch_fork_kind fork_kind;
11873 int tempflag;
11874
11875 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11876 tempflag = (fork_kind == catch_fork_temporary
11877 || fork_kind == catch_vfork_temporary);
11878
11879 if (!arg)
11880 arg = "";
11881 arg = skip_spaces (arg);
11882
11883 /* The allowed syntax is:
11884 catch [v]fork
11885 catch [v]fork if <cond>
11886
11887 First, check if there's an if clause. */
11888 cond_string = ep_parse_optional_if_clause (&arg);
11889
11890 if ((*arg != '\0') && !isspace (*arg))
11891 error (_("Junk at end of arguments."));
11892
11893 /* If this target supports it, create a fork or vfork catchpoint
11894 and enable reporting of such events. */
11895 switch (fork_kind)
11896 {
11897 case catch_fork_temporary:
11898 case catch_fork_permanent:
11899 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11900 &catch_fork_breakpoint_ops);
11901 break;
11902 case catch_vfork_temporary:
11903 case catch_vfork_permanent:
11904 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11905 &catch_vfork_breakpoint_ops);
11906 break;
11907 default:
11908 error (_("unsupported or unknown fork kind; cannot catch it"));
11909 break;
11910 }
11911 }
11912
11913 static void
11914 catch_exec_command_1 (char *arg, int from_tty,
11915 struct cmd_list_element *command)
11916 {
11917 struct exec_catchpoint *c;
11918 struct gdbarch *gdbarch = get_current_arch ();
11919 int tempflag;
11920 char *cond_string = NULL;
11921
11922 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11923
11924 if (!arg)
11925 arg = "";
11926 arg = skip_spaces (arg);
11927
11928 /* The allowed syntax is:
11929 catch exec
11930 catch exec if <cond>
11931
11932 First, check if there's an if clause. */
11933 cond_string = ep_parse_optional_if_clause (&arg);
11934
11935 if ((*arg != '\0') && !isspace (*arg))
11936 error (_("Junk at end of arguments."));
11937
11938 c = XNEW (struct exec_catchpoint);
11939 init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
11940 &catch_exec_breakpoint_ops);
11941 c->exec_pathname = NULL;
11942
11943 install_breakpoint (0, &c->base, 1);
11944 }
11945
11946 void
11947 init_ada_exception_breakpoint (struct breakpoint *b,
11948 struct gdbarch *gdbarch,
11949 struct symtab_and_line sal,
11950 char *addr_string,
11951 const struct breakpoint_ops *ops,
11952 int tempflag,
11953 int enabled,
11954 int from_tty)
11955 {
11956 if (from_tty)
11957 {
11958 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11959 if (!loc_gdbarch)
11960 loc_gdbarch = gdbarch;
11961
11962 describe_other_breakpoints (loc_gdbarch,
11963 sal.pspace, sal.pc, sal.section, -1);
11964 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11965 version for exception catchpoints, because two catchpoints
11966 used for different exception names will use the same address.
11967 In this case, a "breakpoint ... also set at..." warning is
11968 unproductive. Besides, the warning phrasing is also a bit
11969 inappropriate, we should use the word catchpoint, and tell
11970 the user what type of catchpoint it is. The above is good
11971 enough for now, though. */
11972 }
11973
11974 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
11975
11976 b->enable_state = enabled ? bp_enabled : bp_disabled;
11977 b->disposition = tempflag ? disp_del : disp_donttouch;
11978 b->location = string_to_event_location (&addr_string,
11979 language_def (language_ada));
11980 b->language = language_ada;
11981 }
11982
11983 static void
11984 catch_command (char *arg, int from_tty)
11985 {
11986 error (_("Catch requires an event name."));
11987 }
11988 \f
11989
11990 static void
11991 tcatch_command (char *arg, int from_tty)
11992 {
11993 error (_("Catch requires an event name."));
11994 }
11995
11996 /* A qsort comparison function that sorts breakpoints in order. */
11997
11998 static int
11999 compare_breakpoints (const void *a, const void *b)
12000 {
12001 const breakpoint_p *ba = (const breakpoint_p *) a;
12002 uintptr_t ua = (uintptr_t) *ba;
12003 const breakpoint_p *bb = (const breakpoint_p *) b;
12004 uintptr_t ub = (uintptr_t) *bb;
12005
12006 if ((*ba)->number < (*bb)->number)
12007 return -1;
12008 else if ((*ba)->number > (*bb)->number)
12009 return 1;
12010
12011 /* Now sort by address, in case we see, e..g, two breakpoints with
12012 the number 0. */
12013 if (ua < ub)
12014 return -1;
12015 return ua > ub ? 1 : 0;
12016 }
12017
12018 /* Delete breakpoints by address or line. */
12019
12020 static void
12021 clear_command (char *arg, int from_tty)
12022 {
12023 struct breakpoint *b, *prev;
12024 VEC(breakpoint_p) *found = 0;
12025 int ix;
12026 int default_match;
12027 struct symtabs_and_lines sals;
12028 struct symtab_and_line sal;
12029 int i;
12030 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
12031
12032 if (arg)
12033 {
12034 sals = decode_line_with_current_source (arg,
12035 (DECODE_LINE_FUNFIRSTLINE
12036 | DECODE_LINE_LIST_MODE));
12037 make_cleanup (xfree, sals.sals);
12038 default_match = 0;
12039 }
12040 else
12041 {
12042 sals.sals = XNEW (struct symtab_and_line);
12043 make_cleanup (xfree, sals.sals);
12044 init_sal (&sal); /* Initialize to zeroes. */
12045
12046 /* Set sal's line, symtab, pc, and pspace to the values
12047 corresponding to the last call to print_frame_info. If the
12048 codepoint is not valid, this will set all the fields to 0. */
12049 get_last_displayed_sal (&sal);
12050 if (sal.symtab == 0)
12051 error (_("No source file specified."));
12052
12053 sals.sals[0] = sal;
12054 sals.nelts = 1;
12055
12056 default_match = 1;
12057 }
12058
12059 /* We don't call resolve_sal_pc here. That's not as bad as it
12060 seems, because all existing breakpoints typically have both
12061 file/line and pc set. So, if clear is given file/line, we can
12062 match this to existing breakpoint without obtaining pc at all.
12063
12064 We only support clearing given the address explicitly
12065 present in breakpoint table. Say, we've set breakpoint
12066 at file:line. There were several PC values for that file:line,
12067 due to optimization, all in one block.
12068
12069 We've picked one PC value. If "clear" is issued with another
12070 PC corresponding to the same file:line, the breakpoint won't
12071 be cleared. We probably can still clear the breakpoint, but
12072 since the other PC value is never presented to user, user
12073 can only find it by guessing, and it does not seem important
12074 to support that. */
12075
12076 /* For each line spec given, delete bps which correspond to it. Do
12077 it in two passes, solely to preserve the current behavior that
12078 from_tty is forced true if we delete more than one
12079 breakpoint. */
12080
12081 found = NULL;
12082 make_cleanup (VEC_cleanup (breakpoint_p), &found);
12083 for (i = 0; i < sals.nelts; i++)
12084 {
12085 const char *sal_fullname;
12086
12087 /* If exact pc given, clear bpts at that pc.
12088 If line given (pc == 0), clear all bpts on specified line.
12089 If defaulting, clear all bpts on default line
12090 or at default pc.
12091
12092 defaulting sal.pc != 0 tests to do
12093
12094 0 1 pc
12095 1 1 pc _and_ line
12096 0 0 line
12097 1 0 <can't happen> */
12098
12099 sal = sals.sals[i];
12100 sal_fullname = (sal.symtab == NULL
12101 ? NULL : symtab_to_fullname (sal.symtab));
12102
12103 /* Find all matching breakpoints and add them to 'found'. */
12104 ALL_BREAKPOINTS (b)
12105 {
12106 int match = 0;
12107 /* Are we going to delete b? */
12108 if (b->type != bp_none && !is_watchpoint (b))
12109 {
12110 struct bp_location *loc = b->loc;
12111 for (; loc; loc = loc->next)
12112 {
12113 /* If the user specified file:line, don't allow a PC
12114 match. This matches historical gdb behavior. */
12115 int pc_match = (!sal.explicit_line
12116 && sal.pc
12117 && (loc->pspace == sal.pspace)
12118 && (loc->address == sal.pc)
12119 && (!section_is_overlay (loc->section)
12120 || loc->section == sal.section));
12121 int line_match = 0;
12122
12123 if ((default_match || sal.explicit_line)
12124 && loc->symtab != NULL
12125 && sal_fullname != NULL
12126 && sal.pspace == loc->pspace
12127 && loc->line_number == sal.line
12128 && filename_cmp (symtab_to_fullname (loc->symtab),
12129 sal_fullname) == 0)
12130 line_match = 1;
12131
12132 if (pc_match || line_match)
12133 {
12134 match = 1;
12135 break;
12136 }
12137 }
12138 }
12139
12140 if (match)
12141 VEC_safe_push(breakpoint_p, found, b);
12142 }
12143 }
12144
12145 /* Now go thru the 'found' chain and delete them. */
12146 if (VEC_empty(breakpoint_p, found))
12147 {
12148 if (arg)
12149 error (_("No breakpoint at %s."), arg);
12150 else
12151 error (_("No breakpoint at this line."));
12152 }
12153
12154 /* Remove duplicates from the vec. */
12155 qsort (VEC_address (breakpoint_p, found),
12156 VEC_length (breakpoint_p, found),
12157 sizeof (breakpoint_p),
12158 compare_breakpoints);
12159 prev = VEC_index (breakpoint_p, found, 0);
12160 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
12161 {
12162 if (b == prev)
12163 {
12164 VEC_ordered_remove (breakpoint_p, found, ix);
12165 --ix;
12166 }
12167 }
12168
12169 if (VEC_length(breakpoint_p, found) > 1)
12170 from_tty = 1; /* Always report if deleted more than one. */
12171 if (from_tty)
12172 {
12173 if (VEC_length(breakpoint_p, found) == 1)
12174 printf_unfiltered (_("Deleted breakpoint "));
12175 else
12176 printf_unfiltered (_("Deleted breakpoints "));
12177 }
12178
12179 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
12180 {
12181 if (from_tty)
12182 printf_unfiltered ("%d ", b->number);
12183 delete_breakpoint (b);
12184 }
12185 if (from_tty)
12186 putchar_unfiltered ('\n');
12187
12188 do_cleanups (cleanups);
12189 }
12190 \f
12191 /* Delete breakpoint in BS if they are `delete' breakpoints and
12192 all breakpoints that are marked for deletion, whether hit or not.
12193 This is called after any breakpoint is hit, or after errors. */
12194
12195 void
12196 breakpoint_auto_delete (bpstat bs)
12197 {
12198 struct breakpoint *b, *b_tmp;
12199
12200 for (; bs; bs = bs->next)
12201 if (bs->breakpoint_at
12202 && bs->breakpoint_at->disposition == disp_del
12203 && bs->stop)
12204 delete_breakpoint (bs->breakpoint_at);
12205
12206 ALL_BREAKPOINTS_SAFE (b, b_tmp)
12207 {
12208 if (b->disposition == disp_del_at_next_stop)
12209 delete_breakpoint (b);
12210 }
12211 }
12212
12213 /* A comparison function for bp_location AP and BP being interfaced to
12214 qsort. Sort elements primarily by their ADDRESS (no matter what
12215 does breakpoint_address_is_meaningful say for its OWNER),
12216 secondarily by ordering first permanent elements and
12217 terciarily just ensuring the array is sorted stable way despite
12218 qsort being an unstable algorithm. */
12219
12220 static int
12221 bp_location_compare (const void *ap, const void *bp)
12222 {
12223 const struct bp_location *a = *(const struct bp_location **) ap;
12224 const struct bp_location *b = *(const struct bp_location **) bp;
12225
12226 if (a->address != b->address)
12227 return (a->address > b->address) - (a->address < b->address);
12228
12229 /* Sort locations at the same address by their pspace number, keeping
12230 locations of the same inferior (in a multi-inferior environment)
12231 grouped. */
12232
12233 if (a->pspace->num != b->pspace->num)
12234 return ((a->pspace->num > b->pspace->num)
12235 - (a->pspace->num < b->pspace->num));
12236
12237 /* Sort permanent breakpoints first. */
12238 if (a->permanent != b->permanent)
12239 return (a->permanent < b->permanent) - (a->permanent > b->permanent);
12240
12241 /* Make the internal GDB representation stable across GDB runs
12242 where A and B memory inside GDB can differ. Breakpoint locations of
12243 the same type at the same address can be sorted in arbitrary order. */
12244
12245 if (a->owner->number != b->owner->number)
12246 return ((a->owner->number > b->owner->number)
12247 - (a->owner->number < b->owner->number));
12248
12249 return (a > b) - (a < b);
12250 }
12251
12252 /* Set bp_location_placed_address_before_address_max and
12253 bp_location_shadow_len_after_address_max according to the current
12254 content of the bp_location array. */
12255
12256 static void
12257 bp_location_target_extensions_update (void)
12258 {
12259 struct bp_location *bl, **blp_tmp;
12260
12261 bp_location_placed_address_before_address_max = 0;
12262 bp_location_shadow_len_after_address_max = 0;
12263
12264 ALL_BP_LOCATIONS (bl, blp_tmp)
12265 {
12266 CORE_ADDR start, end, addr;
12267
12268 if (!bp_location_has_shadow (bl))
12269 continue;
12270
12271 start = bl->target_info.placed_address;
12272 end = start + bl->target_info.shadow_len;
12273
12274 gdb_assert (bl->address >= start);
12275 addr = bl->address - start;
12276 if (addr > bp_location_placed_address_before_address_max)
12277 bp_location_placed_address_before_address_max = addr;
12278
12279 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
12280
12281 gdb_assert (bl->address < end);
12282 addr = end - bl->address;
12283 if (addr > bp_location_shadow_len_after_address_max)
12284 bp_location_shadow_len_after_address_max = addr;
12285 }
12286 }
12287
12288 /* Download tracepoint locations if they haven't been. */
12289
12290 static void
12291 download_tracepoint_locations (void)
12292 {
12293 struct breakpoint *b;
12294 struct cleanup *old_chain;
12295 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
12296
12297 old_chain = save_current_space_and_thread ();
12298
12299 ALL_TRACEPOINTS (b)
12300 {
12301 struct bp_location *bl;
12302 struct tracepoint *t;
12303 int bp_location_downloaded = 0;
12304
12305 if ((b->type == bp_fast_tracepoint
12306 ? !may_insert_fast_tracepoints
12307 : !may_insert_tracepoints))
12308 continue;
12309
12310 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
12311 {
12312 if (target_can_download_tracepoint ())
12313 can_download_tracepoint = TRIBOOL_TRUE;
12314 else
12315 can_download_tracepoint = TRIBOOL_FALSE;
12316 }
12317
12318 if (can_download_tracepoint == TRIBOOL_FALSE)
12319 break;
12320
12321 for (bl = b->loc; bl; bl = bl->next)
12322 {
12323 /* In tracepoint, locations are _never_ duplicated, so
12324 should_be_inserted is equivalent to
12325 unduplicated_should_be_inserted. */
12326 if (!should_be_inserted (bl) || bl->inserted)
12327 continue;
12328
12329 switch_to_program_space_and_thread (bl->pspace);
12330
12331 target_download_tracepoint (bl);
12332
12333 bl->inserted = 1;
12334 bp_location_downloaded = 1;
12335 }
12336 t = (struct tracepoint *) b;
12337 t->number_on_target = b->number;
12338 if (bp_location_downloaded)
12339 observer_notify_breakpoint_modified (b);
12340 }
12341
12342 do_cleanups (old_chain);
12343 }
12344
12345 /* Swap the insertion/duplication state between two locations. */
12346
12347 static void
12348 swap_insertion (struct bp_location *left, struct bp_location *right)
12349 {
12350 const int left_inserted = left->inserted;
12351 const int left_duplicate = left->duplicate;
12352 const int left_needs_update = left->needs_update;
12353 const struct bp_target_info left_target_info = left->target_info;
12354
12355 /* Locations of tracepoints can never be duplicated. */
12356 if (is_tracepoint (left->owner))
12357 gdb_assert (!left->duplicate);
12358 if (is_tracepoint (right->owner))
12359 gdb_assert (!right->duplicate);
12360
12361 left->inserted = right->inserted;
12362 left->duplicate = right->duplicate;
12363 left->needs_update = right->needs_update;
12364 left->target_info = right->target_info;
12365 right->inserted = left_inserted;
12366 right->duplicate = left_duplicate;
12367 right->needs_update = left_needs_update;
12368 right->target_info = left_target_info;
12369 }
12370
12371 /* Force the re-insertion of the locations at ADDRESS. This is called
12372 once a new/deleted/modified duplicate location is found and we are evaluating
12373 conditions on the target's side. Such conditions need to be updated on
12374 the target. */
12375
12376 static void
12377 force_breakpoint_reinsertion (struct bp_location *bl)
12378 {
12379 struct bp_location **locp = NULL, **loc2p;
12380 struct bp_location *loc;
12381 CORE_ADDR address = 0;
12382 int pspace_num;
12383
12384 address = bl->address;
12385 pspace_num = bl->pspace->num;
12386
12387 /* This is only meaningful if the target is
12388 evaluating conditions and if the user has
12389 opted for condition evaluation on the target's
12390 side. */
12391 if (gdb_evaluates_breakpoint_condition_p ()
12392 || !target_supports_evaluation_of_breakpoint_conditions ())
12393 return;
12394
12395 /* Flag all breakpoint locations with this address and
12396 the same program space as the location
12397 as "its condition has changed". We need to
12398 update the conditions on the target's side. */
12399 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
12400 {
12401 loc = *loc2p;
12402
12403 if (!is_breakpoint (loc->owner)
12404 || pspace_num != loc->pspace->num)
12405 continue;
12406
12407 /* Flag the location appropriately. We use a different state to
12408 let everyone know that we already updated the set of locations
12409 with addr bl->address and program space bl->pspace. This is so
12410 we don't have to keep calling these functions just to mark locations
12411 that have already been marked. */
12412 loc->condition_changed = condition_updated;
12413
12414 /* Free the agent expression bytecode as well. We will compute
12415 it later on. */
12416 if (loc->cond_bytecode)
12417 {
12418 free_agent_expr (loc->cond_bytecode);
12419 loc->cond_bytecode = NULL;
12420 }
12421 }
12422 }
12423 /* Called whether new breakpoints are created, or existing breakpoints
12424 deleted, to update the global location list and recompute which
12425 locations are duplicate of which.
12426
12427 The INSERT_MODE flag determines whether locations may not, may, or
12428 shall be inserted now. See 'enum ugll_insert_mode' for more
12429 info. */
12430
12431 static void
12432 update_global_location_list (enum ugll_insert_mode insert_mode)
12433 {
12434 struct breakpoint *b;
12435 struct bp_location **locp, *loc;
12436 struct cleanup *cleanups;
12437 /* Last breakpoint location address that was marked for update. */
12438 CORE_ADDR last_addr = 0;
12439 /* Last breakpoint location program space that was marked for update. */
12440 int last_pspace_num = -1;
12441
12442 /* Used in the duplicates detection below. When iterating over all
12443 bp_locations, points to the first bp_location of a given address.
12444 Breakpoints and watchpoints of different types are never
12445 duplicates of each other. Keep one pointer for each type of
12446 breakpoint/watchpoint, so we only need to loop over all locations
12447 once. */
12448 struct bp_location *bp_loc_first; /* breakpoint */
12449 struct bp_location *wp_loc_first; /* hardware watchpoint */
12450 struct bp_location *awp_loc_first; /* access watchpoint */
12451 struct bp_location *rwp_loc_first; /* read watchpoint */
12452
12453 /* Saved former bp_location array which we compare against the newly
12454 built bp_location from the current state of ALL_BREAKPOINTS. */
12455 struct bp_location **old_location, **old_locp;
12456 unsigned old_location_count;
12457
12458 old_location = bp_location;
12459 old_location_count = bp_location_count;
12460 bp_location = NULL;
12461 bp_location_count = 0;
12462 cleanups = make_cleanup (xfree, old_location);
12463
12464 ALL_BREAKPOINTS (b)
12465 for (loc = b->loc; loc; loc = loc->next)
12466 bp_location_count++;
12467
12468 bp_location = XNEWVEC (struct bp_location *, bp_location_count);
12469 locp = bp_location;
12470 ALL_BREAKPOINTS (b)
12471 for (loc = b->loc; loc; loc = loc->next)
12472 *locp++ = loc;
12473 qsort (bp_location, bp_location_count, sizeof (*bp_location),
12474 bp_location_compare);
12475
12476 bp_location_target_extensions_update ();
12477
12478 /* Identify bp_location instances that are no longer present in the
12479 new list, and therefore should be freed. Note that it's not
12480 necessary that those locations should be removed from inferior --
12481 if there's another location at the same address (previously
12482 marked as duplicate), we don't need to remove/insert the
12483 location.
12484
12485 LOCP is kept in sync with OLD_LOCP, each pointing to the current
12486 and former bp_location array state respectively. */
12487
12488 locp = bp_location;
12489 for (old_locp = old_location; old_locp < old_location + old_location_count;
12490 old_locp++)
12491 {
12492 struct bp_location *old_loc = *old_locp;
12493 struct bp_location **loc2p;
12494
12495 /* Tells if 'old_loc' is found among the new locations. If
12496 not, we have to free it. */
12497 int found_object = 0;
12498 /* Tells if the location should remain inserted in the target. */
12499 int keep_in_target = 0;
12500 int removed = 0;
12501
12502 /* Skip LOCP entries which will definitely never be needed.
12503 Stop either at or being the one matching OLD_LOC. */
12504 while (locp < bp_location + bp_location_count
12505 && (*locp)->address < old_loc->address)
12506 locp++;
12507
12508 for (loc2p = locp;
12509 (loc2p < bp_location + bp_location_count
12510 && (*loc2p)->address == old_loc->address);
12511 loc2p++)
12512 {
12513 /* Check if this is a new/duplicated location or a duplicated
12514 location that had its condition modified. If so, we want to send
12515 its condition to the target if evaluation of conditions is taking
12516 place there. */
12517 if ((*loc2p)->condition_changed == condition_modified
12518 && (last_addr != old_loc->address
12519 || last_pspace_num != old_loc->pspace->num))
12520 {
12521 force_breakpoint_reinsertion (*loc2p);
12522 last_pspace_num = old_loc->pspace->num;
12523 }
12524
12525 if (*loc2p == old_loc)
12526 found_object = 1;
12527 }
12528
12529 /* We have already handled this address, update it so that we don't
12530 have to go through updates again. */
12531 last_addr = old_loc->address;
12532
12533 /* Target-side condition evaluation: Handle deleted locations. */
12534 if (!found_object)
12535 force_breakpoint_reinsertion (old_loc);
12536
12537 /* If this location is no longer present, and inserted, look if
12538 there's maybe a new location at the same address. If so,
12539 mark that one inserted, and don't remove this one. This is
12540 needed so that we don't have a time window where a breakpoint
12541 at certain location is not inserted. */
12542
12543 if (old_loc->inserted)
12544 {
12545 /* If the location is inserted now, we might have to remove
12546 it. */
12547
12548 if (found_object && should_be_inserted (old_loc))
12549 {
12550 /* The location is still present in the location list,
12551 and still should be inserted. Don't do anything. */
12552 keep_in_target = 1;
12553 }
12554 else
12555 {
12556 /* This location still exists, but it won't be kept in the
12557 target since it may have been disabled. We proceed to
12558 remove its target-side condition. */
12559
12560 /* The location is either no longer present, or got
12561 disabled. See if there's another location at the
12562 same address, in which case we don't need to remove
12563 this one from the target. */
12564
12565 /* OLD_LOC comes from existing struct breakpoint. */
12566 if (breakpoint_address_is_meaningful (old_loc->owner))
12567 {
12568 for (loc2p = locp;
12569 (loc2p < bp_location + bp_location_count
12570 && (*loc2p)->address == old_loc->address);
12571 loc2p++)
12572 {
12573 struct bp_location *loc2 = *loc2p;
12574
12575 if (breakpoint_locations_match (loc2, old_loc))
12576 {
12577 /* Read watchpoint locations are switched to
12578 access watchpoints, if the former are not
12579 supported, but the latter are. */
12580 if (is_hardware_watchpoint (old_loc->owner))
12581 {
12582 gdb_assert (is_hardware_watchpoint (loc2->owner));
12583 loc2->watchpoint_type = old_loc->watchpoint_type;
12584 }
12585
12586 /* loc2 is a duplicated location. We need to check
12587 if it should be inserted in case it will be
12588 unduplicated. */
12589 if (loc2 != old_loc
12590 && unduplicated_should_be_inserted (loc2))
12591 {
12592 swap_insertion (old_loc, loc2);
12593 keep_in_target = 1;
12594 break;
12595 }
12596 }
12597 }
12598 }
12599 }
12600
12601 if (!keep_in_target)
12602 {
12603 if (remove_breakpoint (old_loc))
12604 {
12605 /* This is just about all we can do. We could keep
12606 this location on the global list, and try to
12607 remove it next time, but there's no particular
12608 reason why we will succeed next time.
12609
12610 Note that at this point, old_loc->owner is still
12611 valid, as delete_breakpoint frees the breakpoint
12612 only after calling us. */
12613 printf_filtered (_("warning: Error removing "
12614 "breakpoint %d\n"),
12615 old_loc->owner->number);
12616 }
12617 removed = 1;
12618 }
12619 }
12620
12621 if (!found_object)
12622 {
12623 if (removed && target_is_non_stop_p ()
12624 && need_moribund_for_location_type (old_loc))
12625 {
12626 /* This location was removed from the target. In
12627 non-stop mode, a race condition is possible where
12628 we've removed a breakpoint, but stop events for that
12629 breakpoint are already queued and will arrive later.
12630 We apply an heuristic to be able to distinguish such
12631 SIGTRAPs from other random SIGTRAPs: we keep this
12632 breakpoint location for a bit, and will retire it
12633 after we see some number of events. The theory here
12634 is that reporting of events should, "on the average",
12635 be fair, so after a while we'll see events from all
12636 threads that have anything of interest, and no longer
12637 need to keep this breakpoint location around. We
12638 don't hold locations forever so to reduce chances of
12639 mistaking a non-breakpoint SIGTRAP for a breakpoint
12640 SIGTRAP.
12641
12642 The heuristic failing can be disastrous on
12643 decr_pc_after_break targets.
12644
12645 On decr_pc_after_break targets, like e.g., x86-linux,
12646 if we fail to recognize a late breakpoint SIGTRAP,
12647 because events_till_retirement has reached 0 too
12648 soon, we'll fail to do the PC adjustment, and report
12649 a random SIGTRAP to the user. When the user resumes
12650 the inferior, it will most likely immediately crash
12651 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
12652 corrupted, because of being resumed e.g., in the
12653 middle of a multi-byte instruction, or skipped a
12654 one-byte instruction. This was actually seen happen
12655 on native x86-linux, and should be less rare on
12656 targets that do not support new thread events, like
12657 remote, due to the heuristic depending on
12658 thread_count.
12659
12660 Mistaking a random SIGTRAP for a breakpoint trap
12661 causes similar symptoms (PC adjustment applied when
12662 it shouldn't), but then again, playing with SIGTRAPs
12663 behind the debugger's back is asking for trouble.
12664
12665 Since hardware watchpoint traps are always
12666 distinguishable from other traps, so we don't need to
12667 apply keep hardware watchpoint moribund locations
12668 around. We simply always ignore hardware watchpoint
12669 traps we can no longer explain. */
12670
12671 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12672 old_loc->owner = NULL;
12673
12674 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
12675 }
12676 else
12677 {
12678 old_loc->owner = NULL;
12679 decref_bp_location (&old_loc);
12680 }
12681 }
12682 }
12683
12684 /* Rescan breakpoints at the same address and section, marking the
12685 first one as "first" and any others as "duplicates". This is so
12686 that the bpt instruction is only inserted once. If we have a
12687 permanent breakpoint at the same place as BPT, make that one the
12688 official one, and the rest as duplicates. Permanent breakpoints
12689 are sorted first for the same address.
12690
12691 Do the same for hardware watchpoints, but also considering the
12692 watchpoint's type (regular/access/read) and length. */
12693
12694 bp_loc_first = NULL;
12695 wp_loc_first = NULL;
12696 awp_loc_first = NULL;
12697 rwp_loc_first = NULL;
12698 ALL_BP_LOCATIONS (loc, locp)
12699 {
12700 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12701 non-NULL. */
12702 struct bp_location **loc_first_p;
12703 b = loc->owner;
12704
12705 if (!unduplicated_should_be_inserted (loc)
12706 || !breakpoint_address_is_meaningful (b)
12707 /* Don't detect duplicate for tracepoint locations because they are
12708 never duplicated. See the comments in field `duplicate' of
12709 `struct bp_location'. */
12710 || is_tracepoint (b))
12711 {
12712 /* Clear the condition modification flag. */
12713 loc->condition_changed = condition_unchanged;
12714 continue;
12715 }
12716
12717 if (b->type == bp_hardware_watchpoint)
12718 loc_first_p = &wp_loc_first;
12719 else if (b->type == bp_read_watchpoint)
12720 loc_first_p = &rwp_loc_first;
12721 else if (b->type == bp_access_watchpoint)
12722 loc_first_p = &awp_loc_first;
12723 else
12724 loc_first_p = &bp_loc_first;
12725
12726 if (*loc_first_p == NULL
12727 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12728 || !breakpoint_locations_match (loc, *loc_first_p))
12729 {
12730 *loc_first_p = loc;
12731 loc->duplicate = 0;
12732
12733 if (is_breakpoint (loc->owner) && loc->condition_changed)
12734 {
12735 loc->needs_update = 1;
12736 /* Clear the condition modification flag. */
12737 loc->condition_changed = condition_unchanged;
12738 }
12739 continue;
12740 }
12741
12742
12743 /* This and the above ensure the invariant that the first location
12744 is not duplicated, and is the inserted one.
12745 All following are marked as duplicated, and are not inserted. */
12746 if (loc->inserted)
12747 swap_insertion (loc, *loc_first_p);
12748 loc->duplicate = 1;
12749
12750 /* Clear the condition modification flag. */
12751 loc->condition_changed = condition_unchanged;
12752 }
12753
12754 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
12755 {
12756 if (insert_mode != UGLL_DONT_INSERT)
12757 insert_breakpoint_locations ();
12758 else
12759 {
12760 /* Even though the caller told us to not insert new
12761 locations, we may still need to update conditions on the
12762 target's side of breakpoints that were already inserted
12763 if the target is evaluating breakpoint conditions. We
12764 only update conditions for locations that are marked
12765 "needs_update". */
12766 update_inserted_breakpoint_locations ();
12767 }
12768 }
12769
12770 if (insert_mode != UGLL_DONT_INSERT)
12771 download_tracepoint_locations ();
12772
12773 do_cleanups (cleanups);
12774 }
12775
12776 void
12777 breakpoint_retire_moribund (void)
12778 {
12779 struct bp_location *loc;
12780 int ix;
12781
12782 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12783 if (--(loc->events_till_retirement) == 0)
12784 {
12785 decref_bp_location (&loc);
12786 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12787 --ix;
12788 }
12789 }
12790
12791 static void
12792 update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
12793 {
12794
12795 TRY
12796 {
12797 update_global_location_list (insert_mode);
12798 }
12799 CATCH (e, RETURN_MASK_ERROR)
12800 {
12801 }
12802 END_CATCH
12803 }
12804
12805 /* Clear BKP from a BPS. */
12806
12807 static void
12808 bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12809 {
12810 bpstat bs;
12811
12812 for (bs = bps; bs; bs = bs->next)
12813 if (bs->breakpoint_at == bpt)
12814 {
12815 bs->breakpoint_at = NULL;
12816 bs->old_val = NULL;
12817 /* bs->commands will be freed later. */
12818 }
12819 }
12820
12821 /* Callback for iterate_over_threads. */
12822 static int
12823 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12824 {
12825 struct breakpoint *bpt = (struct breakpoint *) data;
12826
12827 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12828 return 0;
12829 }
12830
12831 /* Helper for breakpoint and tracepoint breakpoint_ops->mention
12832 callbacks. */
12833
12834 static void
12835 say_where (struct breakpoint *b)
12836 {
12837 struct value_print_options opts;
12838
12839 get_user_print_options (&opts);
12840
12841 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12842 single string. */
12843 if (b->loc == NULL)
12844 {
12845 /* For pending locations, the output differs slightly based
12846 on b->extra_string. If this is non-NULL, it contains either
12847 a condition or dprintf arguments. */
12848 if (b->extra_string == NULL)
12849 {
12850 printf_filtered (_(" (%s) pending."),
12851 event_location_to_string (b->location));
12852 }
12853 else if (b->type == bp_dprintf)
12854 {
12855 printf_filtered (_(" (%s,%s) pending."),
12856 event_location_to_string (b->location),
12857 b->extra_string);
12858 }
12859 else
12860 {
12861 printf_filtered (_(" (%s %s) pending."),
12862 event_location_to_string (b->location),
12863 b->extra_string);
12864 }
12865 }
12866 else
12867 {
12868 if (opts.addressprint || b->loc->symtab == NULL)
12869 {
12870 printf_filtered (" at ");
12871 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12872 gdb_stdout);
12873 }
12874 if (b->loc->symtab != NULL)
12875 {
12876 /* If there is a single location, we can print the location
12877 more nicely. */
12878 if (b->loc->next == NULL)
12879 printf_filtered (": file %s, line %d.",
12880 symtab_to_filename_for_display (b->loc->symtab),
12881 b->loc->line_number);
12882 else
12883 /* This is not ideal, but each location may have a
12884 different file name, and this at least reflects the
12885 real situation somewhat. */
12886 printf_filtered (": %s.",
12887 event_location_to_string (b->location));
12888 }
12889
12890 if (b->loc->next)
12891 {
12892 struct bp_location *loc = b->loc;
12893 int n = 0;
12894 for (; loc; loc = loc->next)
12895 ++n;
12896 printf_filtered (" (%d locations)", n);
12897 }
12898 }
12899 }
12900
12901 /* Default bp_location_ops methods. */
12902
12903 static void
12904 bp_location_dtor (struct bp_location *self)
12905 {
12906 xfree (self->cond);
12907 if (self->cond_bytecode)
12908 free_agent_expr (self->cond_bytecode);
12909 xfree (self->function_name);
12910
12911 VEC_free (agent_expr_p, self->target_info.conditions);
12912 VEC_free (agent_expr_p, self->target_info.tcommands);
12913 }
12914
12915 static const struct bp_location_ops bp_location_ops =
12916 {
12917 bp_location_dtor
12918 };
12919
12920 /* Default breakpoint_ops methods all breakpoint_ops ultimately
12921 inherit from. */
12922
12923 static void
12924 base_breakpoint_dtor (struct breakpoint *self)
12925 {
12926 decref_counted_command_line (&self->commands);
12927 xfree (self->cond_string);
12928 xfree (self->extra_string);
12929 xfree (self->filter);
12930 delete_event_location (self->location);
12931 delete_event_location (self->location_range_end);
12932 }
12933
12934 static struct bp_location *
12935 base_breakpoint_allocate_location (struct breakpoint *self)
12936 {
12937 struct bp_location *loc;
12938
12939 loc = XNEW (struct bp_location);
12940 init_bp_location (loc, &bp_location_ops, self);
12941 return loc;
12942 }
12943
12944 static void
12945 base_breakpoint_re_set (struct breakpoint *b)
12946 {
12947 /* Nothing to re-set. */
12948 }
12949
12950 #define internal_error_pure_virtual_called() \
12951 gdb_assert_not_reached ("pure virtual function called")
12952
12953 static int
12954 base_breakpoint_insert_location (struct bp_location *bl)
12955 {
12956 internal_error_pure_virtual_called ();
12957 }
12958
12959 static int
12960 base_breakpoint_remove_location (struct bp_location *bl,
12961 enum remove_bp_reason reason)
12962 {
12963 internal_error_pure_virtual_called ();
12964 }
12965
12966 static int
12967 base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12968 struct address_space *aspace,
12969 CORE_ADDR bp_addr,
12970 const struct target_waitstatus *ws)
12971 {
12972 internal_error_pure_virtual_called ();
12973 }
12974
12975 static void
12976 base_breakpoint_check_status (bpstat bs)
12977 {
12978 /* Always stop. */
12979 }
12980
12981 /* A "works_in_software_mode" breakpoint_ops method that just internal
12982 errors. */
12983
12984 static int
12985 base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12986 {
12987 internal_error_pure_virtual_called ();
12988 }
12989
12990 /* A "resources_needed" breakpoint_ops method that just internal
12991 errors. */
12992
12993 static int
12994 base_breakpoint_resources_needed (const struct bp_location *bl)
12995 {
12996 internal_error_pure_virtual_called ();
12997 }
12998
12999 static enum print_stop_action
13000 base_breakpoint_print_it (bpstat bs)
13001 {
13002 internal_error_pure_virtual_called ();
13003 }
13004
13005 static void
13006 base_breakpoint_print_one_detail (const struct breakpoint *self,
13007 struct ui_out *uiout)
13008 {
13009 /* nothing */
13010 }
13011
13012 static void
13013 base_breakpoint_print_mention (struct breakpoint *b)
13014 {
13015 internal_error_pure_virtual_called ();
13016 }
13017
13018 static void
13019 base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
13020 {
13021 internal_error_pure_virtual_called ();
13022 }
13023
13024 static void
13025 base_breakpoint_create_sals_from_location
13026 (const struct event_location *location,
13027 struct linespec_result *canonical,
13028 enum bptype type_wanted)
13029 {
13030 internal_error_pure_virtual_called ();
13031 }
13032
13033 static void
13034 base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13035 struct linespec_result *c,
13036 char *cond_string,
13037 char *extra_string,
13038 enum bptype type_wanted,
13039 enum bpdisp disposition,
13040 int thread,
13041 int task, int ignore_count,
13042 const struct breakpoint_ops *o,
13043 int from_tty, int enabled,
13044 int internal, unsigned flags)
13045 {
13046 internal_error_pure_virtual_called ();
13047 }
13048
13049 static void
13050 base_breakpoint_decode_location (struct breakpoint *b,
13051 const struct event_location *location,
13052 struct program_space *search_pspace,
13053 struct symtabs_and_lines *sals)
13054 {
13055 internal_error_pure_virtual_called ();
13056 }
13057
13058 /* The default 'explains_signal' method. */
13059
13060 static int
13061 base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
13062 {
13063 return 1;
13064 }
13065
13066 /* The default "after_condition_true" method. */
13067
13068 static void
13069 base_breakpoint_after_condition_true (struct bpstats *bs)
13070 {
13071 /* Nothing to do. */
13072 }
13073
13074 struct breakpoint_ops base_breakpoint_ops =
13075 {
13076 base_breakpoint_dtor,
13077 base_breakpoint_allocate_location,
13078 base_breakpoint_re_set,
13079 base_breakpoint_insert_location,
13080 base_breakpoint_remove_location,
13081 base_breakpoint_breakpoint_hit,
13082 base_breakpoint_check_status,
13083 base_breakpoint_resources_needed,
13084 base_breakpoint_works_in_software_mode,
13085 base_breakpoint_print_it,
13086 NULL,
13087 base_breakpoint_print_one_detail,
13088 base_breakpoint_print_mention,
13089 base_breakpoint_print_recreate,
13090 base_breakpoint_create_sals_from_location,
13091 base_breakpoint_create_breakpoints_sal,
13092 base_breakpoint_decode_location,
13093 base_breakpoint_explains_signal,
13094 base_breakpoint_after_condition_true,
13095 };
13096
13097 /* Default breakpoint_ops methods. */
13098
13099 static void
13100 bkpt_re_set (struct breakpoint *b)
13101 {
13102 /* FIXME: is this still reachable? */
13103 if (breakpoint_event_location_empty_p (b))
13104 {
13105 /* Anything without a location can't be re-set. */
13106 delete_breakpoint (b);
13107 return;
13108 }
13109
13110 breakpoint_re_set_default (b);
13111 }
13112
13113 static int
13114 bkpt_insert_location (struct bp_location *bl)
13115 {
13116 if (bl->loc_type == bp_loc_hardware_breakpoint)
13117 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
13118 else
13119 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
13120 }
13121
13122 static int
13123 bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
13124 {
13125 if (bl->loc_type == bp_loc_hardware_breakpoint)
13126 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
13127 else
13128 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
13129 }
13130
13131 static int
13132 bkpt_breakpoint_hit (const struct bp_location *bl,
13133 struct address_space *aspace, CORE_ADDR bp_addr,
13134 const struct target_waitstatus *ws)
13135 {
13136 if (ws->kind != TARGET_WAITKIND_STOPPED
13137 || ws->value.sig != GDB_SIGNAL_TRAP)
13138 return 0;
13139
13140 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
13141 aspace, bp_addr))
13142 return 0;
13143
13144 if (overlay_debugging /* unmapped overlay section */
13145 && section_is_overlay (bl->section)
13146 && !section_is_mapped (bl->section))
13147 return 0;
13148
13149 return 1;
13150 }
13151
13152 static int
13153 dprintf_breakpoint_hit (const struct bp_location *bl,
13154 struct address_space *aspace, CORE_ADDR bp_addr,
13155 const struct target_waitstatus *ws)
13156 {
13157 if (dprintf_style == dprintf_style_agent
13158 && target_can_run_breakpoint_commands ())
13159 {
13160 /* An agent-style dprintf never causes a stop. If we see a trap
13161 for this address it must be for a breakpoint that happens to
13162 be set at the same address. */
13163 return 0;
13164 }
13165
13166 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
13167 }
13168
13169 static int
13170 bkpt_resources_needed (const struct bp_location *bl)
13171 {
13172 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
13173
13174 return 1;
13175 }
13176
13177 static enum print_stop_action
13178 bkpt_print_it (bpstat bs)
13179 {
13180 struct breakpoint *b;
13181 const struct bp_location *bl;
13182 int bp_temp;
13183 struct ui_out *uiout = current_uiout;
13184
13185 gdb_assert (bs->bp_location_at != NULL);
13186
13187 bl = bs->bp_location_at;
13188 b = bs->breakpoint_at;
13189
13190 bp_temp = b->disposition == disp_del;
13191 if (bl->address != bl->requested_address)
13192 breakpoint_adjustment_warning (bl->requested_address,
13193 bl->address,
13194 b->number, 1);
13195 annotate_breakpoint (b->number);
13196 maybe_print_thread_hit_breakpoint (uiout);
13197
13198 if (bp_temp)
13199 ui_out_text (uiout, "Temporary breakpoint ");
13200 else
13201 ui_out_text (uiout, "Breakpoint ");
13202 if (ui_out_is_mi_like_p (uiout))
13203 {
13204 ui_out_field_string (uiout, "reason",
13205 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
13206 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
13207 }
13208 ui_out_field_int (uiout, "bkptno", b->number);
13209 ui_out_text (uiout, ", ");
13210
13211 return PRINT_SRC_AND_LOC;
13212 }
13213
13214 static void
13215 bkpt_print_mention (struct breakpoint *b)
13216 {
13217 if (ui_out_is_mi_like_p (current_uiout))
13218 return;
13219
13220 switch (b->type)
13221 {
13222 case bp_breakpoint:
13223 case bp_gnu_ifunc_resolver:
13224 if (b->disposition == disp_del)
13225 printf_filtered (_("Temporary breakpoint"));
13226 else
13227 printf_filtered (_("Breakpoint"));
13228 printf_filtered (_(" %d"), b->number);
13229 if (b->type == bp_gnu_ifunc_resolver)
13230 printf_filtered (_(" at gnu-indirect-function resolver"));
13231 break;
13232 case bp_hardware_breakpoint:
13233 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
13234 break;
13235 case bp_dprintf:
13236 printf_filtered (_("Dprintf %d"), b->number);
13237 break;
13238 }
13239
13240 say_where (b);
13241 }
13242
13243 static void
13244 bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13245 {
13246 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
13247 fprintf_unfiltered (fp, "tbreak");
13248 else if (tp->type == bp_breakpoint)
13249 fprintf_unfiltered (fp, "break");
13250 else if (tp->type == bp_hardware_breakpoint
13251 && tp->disposition == disp_del)
13252 fprintf_unfiltered (fp, "thbreak");
13253 else if (tp->type == bp_hardware_breakpoint)
13254 fprintf_unfiltered (fp, "hbreak");
13255 else
13256 internal_error (__FILE__, __LINE__,
13257 _("unhandled breakpoint type %d"), (int) tp->type);
13258
13259 fprintf_unfiltered (fp, " %s",
13260 event_location_to_string (tp->location));
13261
13262 /* Print out extra_string if this breakpoint is pending. It might
13263 contain, for example, conditions that were set by the user. */
13264 if (tp->loc == NULL && tp->extra_string != NULL)
13265 fprintf_unfiltered (fp, " %s", tp->extra_string);
13266
13267 print_recreate_thread (tp, fp);
13268 }
13269
13270 static void
13271 bkpt_create_sals_from_location (const struct event_location *location,
13272 struct linespec_result *canonical,
13273 enum bptype type_wanted)
13274 {
13275 create_sals_from_location_default (location, canonical, type_wanted);
13276 }
13277
13278 static void
13279 bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
13280 struct linespec_result *canonical,
13281 char *cond_string,
13282 char *extra_string,
13283 enum bptype type_wanted,
13284 enum bpdisp disposition,
13285 int thread,
13286 int task, int ignore_count,
13287 const struct breakpoint_ops *ops,
13288 int from_tty, int enabled,
13289 int internal, unsigned flags)
13290 {
13291 create_breakpoints_sal_default (gdbarch, canonical,
13292 cond_string, extra_string,
13293 type_wanted,
13294 disposition, thread, task,
13295 ignore_count, ops, from_tty,
13296 enabled, internal, flags);
13297 }
13298
13299 static void
13300 bkpt_decode_location (struct breakpoint *b,
13301 const struct event_location *location,
13302 struct program_space *search_pspace,
13303 struct symtabs_and_lines *sals)
13304 {
13305 decode_location_default (b, location, search_pspace, sals);
13306 }
13307
13308 /* Virtual table for internal breakpoints. */
13309
13310 static void
13311 internal_bkpt_re_set (struct breakpoint *b)
13312 {
13313 switch (b->type)
13314 {
13315 /* Delete overlay event and longjmp master breakpoints; they
13316 will be reset later by breakpoint_re_set. */
13317 case bp_overlay_event:
13318 case bp_longjmp_master:
13319 case bp_std_terminate_master:
13320 case bp_exception_master:
13321 delete_breakpoint (b);
13322 break;
13323
13324 /* This breakpoint is special, it's set up when the inferior
13325 starts and we really don't want to touch it. */
13326 case bp_shlib_event:
13327
13328 /* Like bp_shlib_event, this breakpoint type is special. Once
13329 it is set up, we do not want to touch it. */
13330 case bp_thread_event:
13331 break;
13332 }
13333 }
13334
13335 static void
13336 internal_bkpt_check_status (bpstat bs)
13337 {
13338 if (bs->breakpoint_at->type == bp_shlib_event)
13339 {
13340 /* If requested, stop when the dynamic linker notifies GDB of
13341 events. This allows the user to get control and place
13342 breakpoints in initializer routines for dynamically loaded
13343 objects (among other things). */
13344 bs->stop = stop_on_solib_events;
13345 bs->print = stop_on_solib_events;
13346 }
13347 else
13348 bs->stop = 0;
13349 }
13350
13351 static enum print_stop_action
13352 internal_bkpt_print_it (bpstat bs)
13353 {
13354 struct breakpoint *b;
13355
13356 b = bs->breakpoint_at;
13357
13358 switch (b->type)
13359 {
13360 case bp_shlib_event:
13361 /* Did we stop because the user set the stop_on_solib_events
13362 variable? (If so, we report this as a generic, "Stopped due
13363 to shlib event" message.) */
13364 print_solib_event (0);
13365 break;
13366
13367 case bp_thread_event:
13368 /* Not sure how we will get here.
13369 GDB should not stop for these breakpoints. */
13370 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
13371 break;
13372
13373 case bp_overlay_event:
13374 /* By analogy with the thread event, GDB should not stop for these. */
13375 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
13376 break;
13377
13378 case bp_longjmp_master:
13379 /* These should never be enabled. */
13380 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
13381 break;
13382
13383 case bp_std_terminate_master:
13384 /* These should never be enabled. */
13385 printf_filtered (_("std::terminate Master Breakpoint: "
13386 "gdb should not stop!\n"));
13387 break;
13388
13389 case bp_exception_master:
13390 /* These should never be enabled. */
13391 printf_filtered (_("Exception Master Breakpoint: "
13392 "gdb should not stop!\n"));
13393 break;
13394 }
13395
13396 return PRINT_NOTHING;
13397 }
13398
13399 static void
13400 internal_bkpt_print_mention (struct breakpoint *b)
13401 {
13402 /* Nothing to mention. These breakpoints are internal. */
13403 }
13404
13405 /* Virtual table for momentary breakpoints */
13406
13407 static void
13408 momentary_bkpt_re_set (struct breakpoint *b)
13409 {
13410 /* Keep temporary breakpoints, which can be encountered when we step
13411 over a dlopen call and solib_add is resetting the breakpoints.
13412 Otherwise these should have been blown away via the cleanup chain
13413 or by breakpoint_init_inferior when we rerun the executable. */
13414 }
13415
13416 static void
13417 momentary_bkpt_check_status (bpstat bs)
13418 {
13419 /* Nothing. The point of these breakpoints is causing a stop. */
13420 }
13421
13422 static enum print_stop_action
13423 momentary_bkpt_print_it (bpstat bs)
13424 {
13425 return PRINT_UNKNOWN;
13426 }
13427
13428 static void
13429 momentary_bkpt_print_mention (struct breakpoint *b)
13430 {
13431 /* Nothing to mention. These breakpoints are internal. */
13432 }
13433
13434 /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
13435
13436 It gets cleared already on the removal of the first one of such placed
13437 breakpoints. This is OK as they get all removed altogether. */
13438
13439 static void
13440 longjmp_bkpt_dtor (struct breakpoint *self)
13441 {
13442 struct thread_info *tp = find_thread_global_id (self->thread);
13443
13444 if (tp)
13445 tp->initiating_frame = null_frame_id;
13446
13447 momentary_breakpoint_ops.dtor (self);
13448 }
13449
13450 /* Specific methods for probe breakpoints. */
13451
13452 static int
13453 bkpt_probe_insert_location (struct bp_location *bl)
13454 {
13455 int v = bkpt_insert_location (bl);
13456
13457 if (v == 0)
13458 {
13459 /* The insertion was successful, now let's set the probe's semaphore
13460 if needed. */
13461 if (bl->probe.probe->pops->set_semaphore != NULL)
13462 bl->probe.probe->pops->set_semaphore (bl->probe.probe,
13463 bl->probe.objfile,
13464 bl->gdbarch);
13465 }
13466
13467 return v;
13468 }
13469
13470 static int
13471 bkpt_probe_remove_location (struct bp_location *bl,
13472 enum remove_bp_reason reason)
13473 {
13474 /* Let's clear the semaphore before removing the location. */
13475 if (bl->probe.probe->pops->clear_semaphore != NULL)
13476 bl->probe.probe->pops->clear_semaphore (bl->probe.probe,
13477 bl->probe.objfile,
13478 bl->gdbarch);
13479
13480 return bkpt_remove_location (bl, reason);
13481 }
13482
13483 static void
13484 bkpt_probe_create_sals_from_location (const struct event_location *location,
13485 struct linespec_result *canonical,
13486 enum bptype type_wanted)
13487 {
13488 struct linespec_sals lsal;
13489
13490 lsal.sals = parse_probes (location, NULL, canonical);
13491 lsal.canonical = xstrdup (event_location_to_string (canonical->location));
13492 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13493 }
13494
13495 static void
13496 bkpt_probe_decode_location (struct breakpoint *b,
13497 const struct event_location *location,
13498 struct program_space *search_pspace,
13499 struct symtabs_and_lines *sals)
13500 {
13501 *sals = parse_probes (location, search_pspace, NULL);
13502 if (!sals->sals)
13503 error (_("probe not found"));
13504 }
13505
13506 /* The breakpoint_ops structure to be used in tracepoints. */
13507
13508 static void
13509 tracepoint_re_set (struct breakpoint *b)
13510 {
13511 breakpoint_re_set_default (b);
13512 }
13513
13514 static int
13515 tracepoint_breakpoint_hit (const struct bp_location *bl,
13516 struct address_space *aspace, CORE_ADDR bp_addr,
13517 const struct target_waitstatus *ws)
13518 {
13519 /* By definition, the inferior does not report stops at
13520 tracepoints. */
13521 return 0;
13522 }
13523
13524 static void
13525 tracepoint_print_one_detail (const struct breakpoint *self,
13526 struct ui_out *uiout)
13527 {
13528 struct tracepoint *tp = (struct tracepoint *) self;
13529 if (tp->static_trace_marker_id)
13530 {
13531 gdb_assert (self->type == bp_static_tracepoint);
13532
13533 ui_out_text (uiout, "\tmarker id is ");
13534 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
13535 tp->static_trace_marker_id);
13536 ui_out_text (uiout, "\n");
13537 }
13538 }
13539
13540 static void
13541 tracepoint_print_mention (struct breakpoint *b)
13542 {
13543 if (ui_out_is_mi_like_p (current_uiout))
13544 return;
13545
13546 switch (b->type)
13547 {
13548 case bp_tracepoint:
13549 printf_filtered (_("Tracepoint"));
13550 printf_filtered (_(" %d"), b->number);
13551 break;
13552 case bp_fast_tracepoint:
13553 printf_filtered (_("Fast tracepoint"));
13554 printf_filtered (_(" %d"), b->number);
13555 break;
13556 case bp_static_tracepoint:
13557 printf_filtered (_("Static tracepoint"));
13558 printf_filtered (_(" %d"), b->number);
13559 break;
13560 default:
13561 internal_error (__FILE__, __LINE__,
13562 _("unhandled tracepoint type %d"), (int) b->type);
13563 }
13564
13565 say_where (b);
13566 }
13567
13568 static void
13569 tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
13570 {
13571 struct tracepoint *tp = (struct tracepoint *) self;
13572
13573 if (self->type == bp_fast_tracepoint)
13574 fprintf_unfiltered (fp, "ftrace");
13575 else if (self->type == bp_static_tracepoint)
13576 fprintf_unfiltered (fp, "strace");
13577 else if (self->type == bp_tracepoint)
13578 fprintf_unfiltered (fp, "trace");
13579 else
13580 internal_error (__FILE__, __LINE__,
13581 _("unhandled tracepoint type %d"), (int) self->type);
13582
13583 fprintf_unfiltered (fp, " %s",
13584 event_location_to_string (self->location));
13585 print_recreate_thread (self, fp);
13586
13587 if (tp->pass_count)
13588 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
13589 }
13590
13591 static void
13592 tracepoint_create_sals_from_location (const struct event_location *location,
13593 struct linespec_result *canonical,
13594 enum bptype type_wanted)
13595 {
13596 create_sals_from_location_default (location, canonical, type_wanted);
13597 }
13598
13599 static void
13600 tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13601 struct linespec_result *canonical,
13602 char *cond_string,
13603 char *extra_string,
13604 enum bptype type_wanted,
13605 enum bpdisp disposition,
13606 int thread,
13607 int task, int ignore_count,
13608 const struct breakpoint_ops *ops,
13609 int from_tty, int enabled,
13610 int internal, unsigned flags)
13611 {
13612 create_breakpoints_sal_default (gdbarch, canonical,
13613 cond_string, extra_string,
13614 type_wanted,
13615 disposition, thread, task,
13616 ignore_count, ops, from_tty,
13617 enabled, internal, flags);
13618 }
13619
13620 static void
13621 tracepoint_decode_location (struct breakpoint *b,
13622 const struct event_location *location,
13623 struct program_space *search_pspace,
13624 struct symtabs_and_lines *sals)
13625 {
13626 decode_location_default (b, location, search_pspace, sals);
13627 }
13628
13629 struct breakpoint_ops tracepoint_breakpoint_ops;
13630
13631 /* The breakpoint_ops structure to be use on tracepoints placed in a
13632 static probe. */
13633
13634 static void
13635 tracepoint_probe_create_sals_from_location
13636 (const struct event_location *location,
13637 struct linespec_result *canonical,
13638 enum bptype type_wanted)
13639 {
13640 /* We use the same method for breakpoint on probes. */
13641 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
13642 }
13643
13644 static void
13645 tracepoint_probe_decode_location (struct breakpoint *b,
13646 const struct event_location *location,
13647 struct program_space *search_pspace,
13648 struct symtabs_and_lines *sals)
13649 {
13650 /* We use the same method for breakpoint on probes. */
13651 bkpt_probe_decode_location (b, location, search_pspace, sals);
13652 }
13653
13654 static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13655
13656 /* Dprintf breakpoint_ops methods. */
13657
13658 static void
13659 dprintf_re_set (struct breakpoint *b)
13660 {
13661 breakpoint_re_set_default (b);
13662
13663 /* extra_string should never be non-NULL for dprintf. */
13664 gdb_assert (b->extra_string != NULL);
13665
13666 /* 1 - connect to target 1, that can run breakpoint commands.
13667 2 - create a dprintf, which resolves fine.
13668 3 - disconnect from target 1
13669 4 - connect to target 2, that can NOT run breakpoint commands.
13670
13671 After steps #3/#4, you'll want the dprintf command list to
13672 be updated, because target 1 and 2 may well return different
13673 answers for target_can_run_breakpoint_commands().
13674 Given absence of finer grained resetting, we get to do
13675 it all the time. */
13676 if (b->extra_string != NULL)
13677 update_dprintf_command_list (b);
13678 }
13679
13680 /* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13681
13682 static void
13683 dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13684 {
13685 fprintf_unfiltered (fp, "dprintf %s,%s",
13686 event_location_to_string (tp->location),
13687 tp->extra_string);
13688 print_recreate_thread (tp, fp);
13689 }
13690
13691 /* Implement the "after_condition_true" breakpoint_ops method for
13692 dprintf.
13693
13694 dprintf's are implemented with regular commands in their command
13695 list, but we run the commands here instead of before presenting the
13696 stop to the user, as dprintf's don't actually cause a stop. This
13697 also makes it so that the commands of multiple dprintfs at the same
13698 address are all handled. */
13699
13700 static void
13701 dprintf_after_condition_true (struct bpstats *bs)
13702 {
13703 struct cleanup *old_chain;
13704 struct bpstats tmp_bs = { NULL };
13705 struct bpstats *tmp_bs_p = &tmp_bs;
13706
13707 /* dprintf's never cause a stop. This wasn't set in the
13708 check_status hook instead because that would make the dprintf's
13709 condition not be evaluated. */
13710 bs->stop = 0;
13711
13712 /* Run the command list here. Take ownership of it instead of
13713 copying. We never want these commands to run later in
13714 bpstat_do_actions, if a breakpoint that causes a stop happens to
13715 be set at same address as this dprintf, or even if running the
13716 commands here throws. */
13717 tmp_bs.commands = bs->commands;
13718 bs->commands = NULL;
13719 old_chain = make_cleanup_decref_counted_command_line (&tmp_bs.commands);
13720
13721 bpstat_do_actions_1 (&tmp_bs_p);
13722
13723 /* 'tmp_bs.commands' will usually be NULL by now, but
13724 bpstat_do_actions_1 may return early without processing the whole
13725 list. */
13726 do_cleanups (old_chain);
13727 }
13728
13729 /* The breakpoint_ops structure to be used on static tracepoints with
13730 markers (`-m'). */
13731
13732 static void
13733 strace_marker_create_sals_from_location (const struct event_location *location,
13734 struct linespec_result *canonical,
13735 enum bptype type_wanted)
13736 {
13737 struct linespec_sals lsal;
13738 const char *arg_start, *arg;
13739 char *str;
13740 struct cleanup *cleanup;
13741
13742 arg = arg_start = get_linespec_location (location);
13743 lsal.sals = decode_static_tracepoint_spec (&arg);
13744
13745 str = savestring (arg_start, arg - arg_start);
13746 cleanup = make_cleanup (xfree, str);
13747 canonical->location = new_linespec_location (&str);
13748 do_cleanups (cleanup);
13749
13750 lsal.canonical = xstrdup (event_location_to_string (canonical->location));
13751 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13752 }
13753
13754 static void
13755 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13756 struct linespec_result *canonical,
13757 char *cond_string,
13758 char *extra_string,
13759 enum bptype type_wanted,
13760 enum bpdisp disposition,
13761 int thread,
13762 int task, int ignore_count,
13763 const struct breakpoint_ops *ops,
13764 int from_tty, int enabled,
13765 int internal, unsigned flags)
13766 {
13767 int i;
13768 struct linespec_sals *lsal = VEC_index (linespec_sals,
13769 canonical->sals, 0);
13770
13771 /* If the user is creating a static tracepoint by marker id
13772 (strace -m MARKER_ID), then store the sals index, so that
13773 breakpoint_re_set can try to match up which of the newly
13774 found markers corresponds to this one, and, don't try to
13775 expand multiple locations for each sal, given than SALS
13776 already should contain all sals for MARKER_ID. */
13777
13778 for (i = 0; i < lsal->sals.nelts; ++i)
13779 {
13780 struct symtabs_and_lines expanded;
13781 struct tracepoint *tp;
13782 struct cleanup *old_chain;
13783 struct event_location *location;
13784
13785 expanded.nelts = 1;
13786 expanded.sals = &lsal->sals.sals[i];
13787
13788 location = copy_event_location (canonical->location);
13789 old_chain = make_cleanup_delete_event_location (location);
13790
13791 tp = XCNEW (struct tracepoint);
13792 init_breakpoint_sal (&tp->base, gdbarch, expanded,
13793 location, NULL,
13794 cond_string, extra_string,
13795 type_wanted, disposition,
13796 thread, task, ignore_count, ops,
13797 from_tty, enabled, internal, flags,
13798 canonical->special_display);
13799 /* Given that its possible to have multiple markers with
13800 the same string id, if the user is creating a static
13801 tracepoint by marker id ("strace -m MARKER_ID"), then
13802 store the sals index, so that breakpoint_re_set can
13803 try to match up which of the newly found markers
13804 corresponds to this one */
13805 tp->static_trace_marker_id_idx = i;
13806
13807 install_breakpoint (internal, &tp->base, 0);
13808
13809 discard_cleanups (old_chain);
13810 }
13811 }
13812
13813 static void
13814 strace_marker_decode_location (struct breakpoint *b,
13815 const struct event_location *location,
13816 struct program_space *search_pspace,
13817 struct symtabs_and_lines *sals)
13818 {
13819 struct tracepoint *tp = (struct tracepoint *) b;
13820 const char *s = get_linespec_location (location);
13821
13822 *sals = decode_static_tracepoint_spec (&s);
13823 if (sals->nelts > tp->static_trace_marker_id_idx)
13824 {
13825 sals->sals[0] = sals->sals[tp->static_trace_marker_id_idx];
13826 sals->nelts = 1;
13827 }
13828 else
13829 error (_("marker %s not found"), tp->static_trace_marker_id);
13830 }
13831
13832 static struct breakpoint_ops strace_marker_breakpoint_ops;
13833
13834 static int
13835 strace_marker_p (struct breakpoint *b)
13836 {
13837 return b->ops == &strace_marker_breakpoint_ops;
13838 }
13839
13840 /* Delete a breakpoint and clean up all traces of it in the data
13841 structures. */
13842
13843 void
13844 delete_breakpoint (struct breakpoint *bpt)
13845 {
13846 struct breakpoint *b;
13847
13848 gdb_assert (bpt != NULL);
13849
13850 /* Has this bp already been deleted? This can happen because
13851 multiple lists can hold pointers to bp's. bpstat lists are
13852 especial culprits.
13853
13854 One example of this happening is a watchpoint's scope bp. When
13855 the scope bp triggers, we notice that the watchpoint is out of
13856 scope, and delete it. We also delete its scope bp. But the
13857 scope bp is marked "auto-deleting", and is already on a bpstat.
13858 That bpstat is then checked for auto-deleting bp's, which are
13859 deleted.
13860
13861 A real solution to this problem might involve reference counts in
13862 bp's, and/or giving them pointers back to their referencing
13863 bpstat's, and teaching delete_breakpoint to only free a bp's
13864 storage when no more references were extent. A cheaper bandaid
13865 was chosen. */
13866 if (bpt->type == bp_none)
13867 return;
13868
13869 /* At least avoid this stale reference until the reference counting
13870 of breakpoints gets resolved. */
13871 if (bpt->related_breakpoint != bpt)
13872 {
13873 struct breakpoint *related;
13874 struct watchpoint *w;
13875
13876 if (bpt->type == bp_watchpoint_scope)
13877 w = (struct watchpoint *) bpt->related_breakpoint;
13878 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
13879 w = (struct watchpoint *) bpt;
13880 else
13881 w = NULL;
13882 if (w != NULL)
13883 watchpoint_del_at_next_stop (w);
13884
13885 /* Unlink bpt from the bpt->related_breakpoint ring. */
13886 for (related = bpt; related->related_breakpoint != bpt;
13887 related = related->related_breakpoint);
13888 related->related_breakpoint = bpt->related_breakpoint;
13889 bpt->related_breakpoint = bpt;
13890 }
13891
13892 /* watch_command_1 creates a watchpoint but only sets its number if
13893 update_watchpoint succeeds in creating its bp_locations. If there's
13894 a problem in that process, we'll be asked to delete the half-created
13895 watchpoint. In that case, don't announce the deletion. */
13896 if (bpt->number)
13897 observer_notify_breakpoint_deleted (bpt);
13898
13899 if (breakpoint_chain == bpt)
13900 breakpoint_chain = bpt->next;
13901
13902 ALL_BREAKPOINTS (b)
13903 if (b->next == bpt)
13904 {
13905 b->next = bpt->next;
13906 break;
13907 }
13908
13909 /* Be sure no bpstat's are pointing at the breakpoint after it's
13910 been freed. */
13911 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
13912 in all threads for now. Note that we cannot just remove bpstats
13913 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13914 commands are associated with the bpstat; if we remove it here,
13915 then the later call to bpstat_do_actions (&stop_bpstat); in
13916 event-top.c won't do anything, and temporary breakpoints with
13917 commands won't work. */
13918
13919 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13920
13921 /* Now that breakpoint is removed from breakpoint list, update the
13922 global location list. This will remove locations that used to
13923 belong to this breakpoint. Do this before freeing the breakpoint
13924 itself, since remove_breakpoint looks at location's owner. It
13925 might be better design to have location completely
13926 self-contained, but it's not the case now. */
13927 update_global_location_list (UGLL_DONT_INSERT);
13928
13929 bpt->ops->dtor (bpt);
13930 /* On the chance that someone will soon try again to delete this
13931 same bp, we mark it as deleted before freeing its storage. */
13932 bpt->type = bp_none;
13933 xfree (bpt);
13934 }
13935
13936 static void
13937 do_delete_breakpoint_cleanup (void *b)
13938 {
13939 delete_breakpoint ((struct breakpoint *) b);
13940 }
13941
13942 struct cleanup *
13943 make_cleanup_delete_breakpoint (struct breakpoint *b)
13944 {
13945 return make_cleanup (do_delete_breakpoint_cleanup, b);
13946 }
13947
13948 /* Iterator function to call a user-provided callback function once
13949 for each of B and its related breakpoints. */
13950
13951 static void
13952 iterate_over_related_breakpoints (struct breakpoint *b,
13953 void (*function) (struct breakpoint *,
13954 void *),
13955 void *data)
13956 {
13957 struct breakpoint *related;
13958
13959 related = b;
13960 do
13961 {
13962 struct breakpoint *next;
13963
13964 /* FUNCTION may delete RELATED. */
13965 next = related->related_breakpoint;
13966
13967 if (next == related)
13968 {
13969 /* RELATED is the last ring entry. */
13970 function (related, data);
13971
13972 /* FUNCTION may have deleted it, so we'd never reach back to
13973 B. There's nothing left to do anyway, so just break
13974 out. */
13975 break;
13976 }
13977 else
13978 function (related, data);
13979
13980 related = next;
13981 }
13982 while (related != b);
13983 }
13984
13985 static void
13986 do_delete_breakpoint (struct breakpoint *b, void *ignore)
13987 {
13988 delete_breakpoint (b);
13989 }
13990
13991 /* A callback for map_breakpoint_numbers that calls
13992 delete_breakpoint. */
13993
13994 static void
13995 do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
13996 {
13997 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
13998 }
13999
14000 void
14001 delete_command (char *arg, int from_tty)
14002 {
14003 struct breakpoint *b, *b_tmp;
14004
14005 dont_repeat ();
14006
14007 if (arg == 0)
14008 {
14009 int breaks_to_delete = 0;
14010
14011 /* Delete all breakpoints if no argument. Do not delete
14012 internal breakpoints, these have to be deleted with an
14013 explicit breakpoint number argument. */
14014 ALL_BREAKPOINTS (b)
14015 if (user_breakpoint_p (b))
14016 {
14017 breaks_to_delete = 1;
14018 break;
14019 }
14020
14021 /* Ask user only if there are some breakpoints to delete. */
14022 if (!from_tty
14023 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
14024 {
14025 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14026 if (user_breakpoint_p (b))
14027 delete_breakpoint (b);
14028 }
14029 }
14030 else
14031 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
14032 }
14033
14034 /* Return true if all locations of B bound to PSPACE are pending. If
14035 PSPACE is NULL, all locations of all program spaces are
14036 considered. */
14037
14038 static int
14039 all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
14040 {
14041 struct bp_location *loc;
14042
14043 for (loc = b->loc; loc != NULL; loc = loc->next)
14044 if ((pspace == NULL
14045 || loc->pspace == pspace)
14046 && !loc->shlib_disabled
14047 && !loc->pspace->executing_startup)
14048 return 0;
14049 return 1;
14050 }
14051
14052 /* Subroutine of update_breakpoint_locations to simplify it.
14053 Return non-zero if multiple fns in list LOC have the same name.
14054 Null names are ignored. */
14055
14056 static int
14057 ambiguous_names_p (struct bp_location *loc)
14058 {
14059 struct bp_location *l;
14060 htab_t htab = htab_create_alloc (13, htab_hash_string,
14061 (int (*) (const void *,
14062 const void *)) streq,
14063 NULL, xcalloc, xfree);
14064
14065 for (l = loc; l != NULL; l = l->next)
14066 {
14067 const char **slot;
14068 const char *name = l->function_name;
14069
14070 /* Allow for some names to be NULL, ignore them. */
14071 if (name == NULL)
14072 continue;
14073
14074 slot = (const char **) htab_find_slot (htab, (const void *) name,
14075 INSERT);
14076 /* NOTE: We can assume slot != NULL here because xcalloc never
14077 returns NULL. */
14078 if (*slot != NULL)
14079 {
14080 htab_delete (htab);
14081 return 1;
14082 }
14083 *slot = name;
14084 }
14085
14086 htab_delete (htab);
14087 return 0;
14088 }
14089
14090 /* When symbols change, it probably means the sources changed as well,
14091 and it might mean the static tracepoint markers are no longer at
14092 the same address or line numbers they used to be at last we
14093 checked. Losing your static tracepoints whenever you rebuild is
14094 undesirable. This function tries to resync/rematch gdb static
14095 tracepoints with the markers on the target, for static tracepoints
14096 that have not been set by marker id. Static tracepoint that have
14097 been set by marker id are reset by marker id in breakpoint_re_set.
14098 The heuristic is:
14099
14100 1) For a tracepoint set at a specific address, look for a marker at
14101 the old PC. If one is found there, assume to be the same marker.
14102 If the name / string id of the marker found is different from the
14103 previous known name, assume that means the user renamed the marker
14104 in the sources, and output a warning.
14105
14106 2) For a tracepoint set at a given line number, look for a marker
14107 at the new address of the old line number. If one is found there,
14108 assume to be the same marker. If the name / string id of the
14109 marker found is different from the previous known name, assume that
14110 means the user renamed the marker in the sources, and output a
14111 warning.
14112
14113 3) If a marker is no longer found at the same address or line, it
14114 may mean the marker no longer exists. But it may also just mean
14115 the code changed a bit. Maybe the user added a few lines of code
14116 that made the marker move up or down (in line number terms). Ask
14117 the target for info about the marker with the string id as we knew
14118 it. If found, update line number and address in the matching
14119 static tracepoint. This will get confused if there's more than one
14120 marker with the same ID (possible in UST, although unadvised
14121 precisely because it confuses tools). */
14122
14123 static struct symtab_and_line
14124 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
14125 {
14126 struct tracepoint *tp = (struct tracepoint *) b;
14127 struct static_tracepoint_marker marker;
14128 CORE_ADDR pc;
14129
14130 pc = sal.pc;
14131 if (sal.line)
14132 find_line_pc (sal.symtab, sal.line, &pc);
14133
14134 if (target_static_tracepoint_marker_at (pc, &marker))
14135 {
14136 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
14137 warning (_("static tracepoint %d changed probed marker from %s to %s"),
14138 b->number,
14139 tp->static_trace_marker_id, marker.str_id);
14140
14141 xfree (tp->static_trace_marker_id);
14142 tp->static_trace_marker_id = xstrdup (marker.str_id);
14143 release_static_tracepoint_marker (&marker);
14144
14145 return sal;
14146 }
14147
14148 /* Old marker wasn't found on target at lineno. Try looking it up
14149 by string ID. */
14150 if (!sal.explicit_pc
14151 && sal.line != 0
14152 && sal.symtab != NULL
14153 && tp->static_trace_marker_id != NULL)
14154 {
14155 VEC(static_tracepoint_marker_p) *markers;
14156
14157 markers
14158 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
14159
14160 if (!VEC_empty(static_tracepoint_marker_p, markers))
14161 {
14162 struct symtab_and_line sal2;
14163 struct symbol *sym;
14164 struct static_tracepoint_marker *tpmarker;
14165 struct ui_out *uiout = current_uiout;
14166 struct explicit_location explicit_loc;
14167
14168 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
14169
14170 xfree (tp->static_trace_marker_id);
14171 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
14172
14173 warning (_("marker for static tracepoint %d (%s) not "
14174 "found at previous line number"),
14175 b->number, tp->static_trace_marker_id);
14176
14177 init_sal (&sal2);
14178
14179 sal2.pc = tpmarker->address;
14180
14181 sal2 = find_pc_line (tpmarker->address, 0);
14182 sym = find_pc_sect_function (tpmarker->address, NULL);
14183 ui_out_text (uiout, "Now in ");
14184 if (sym)
14185 {
14186 ui_out_field_string (uiout, "func",
14187 SYMBOL_PRINT_NAME (sym));
14188 ui_out_text (uiout, " at ");
14189 }
14190 ui_out_field_string (uiout, "file",
14191 symtab_to_filename_for_display (sal2.symtab));
14192 ui_out_text (uiout, ":");
14193
14194 if (ui_out_is_mi_like_p (uiout))
14195 {
14196 const char *fullname = symtab_to_fullname (sal2.symtab);
14197
14198 ui_out_field_string (uiout, "fullname", fullname);
14199 }
14200
14201 ui_out_field_int (uiout, "line", sal2.line);
14202 ui_out_text (uiout, "\n");
14203
14204 b->loc->line_number = sal2.line;
14205 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
14206
14207 delete_event_location (b->location);
14208 initialize_explicit_location (&explicit_loc);
14209 explicit_loc.source_filename
14210 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
14211 explicit_loc.line_offset.offset = b->loc->line_number;
14212 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
14213 b->location = new_explicit_location (&explicit_loc);
14214
14215 /* Might be nice to check if function changed, and warn if
14216 so. */
14217
14218 release_static_tracepoint_marker (tpmarker);
14219 }
14220 }
14221 return sal;
14222 }
14223
14224 /* Returns 1 iff locations A and B are sufficiently same that
14225 we don't need to report breakpoint as changed. */
14226
14227 static int
14228 locations_are_equal (struct bp_location *a, struct bp_location *b)
14229 {
14230 while (a && b)
14231 {
14232 if (a->address != b->address)
14233 return 0;
14234
14235 if (a->shlib_disabled != b->shlib_disabled)
14236 return 0;
14237
14238 if (a->enabled != b->enabled)
14239 return 0;
14240
14241 a = a->next;
14242 b = b->next;
14243 }
14244
14245 if ((a == NULL) != (b == NULL))
14246 return 0;
14247
14248 return 1;
14249 }
14250
14251 /* Split all locations of B that are bound to PSPACE out of B's
14252 location list to a separate list and return that list's head. If
14253 PSPACE is NULL, hoist out all locations of B. */
14254
14255 static struct bp_location *
14256 hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
14257 {
14258 struct bp_location head;
14259 struct bp_location *i = b->loc;
14260 struct bp_location **i_link = &b->loc;
14261 struct bp_location *hoisted = &head;
14262
14263 if (pspace == NULL)
14264 {
14265 i = b->loc;
14266 b->loc = NULL;
14267 return i;
14268 }
14269
14270 head.next = NULL;
14271
14272 while (i != NULL)
14273 {
14274 if (i->pspace == pspace)
14275 {
14276 *i_link = i->next;
14277 i->next = NULL;
14278 hoisted->next = i;
14279 hoisted = i;
14280 }
14281 else
14282 i_link = &i->next;
14283 i = *i_link;
14284 }
14285
14286 return head.next;
14287 }
14288
14289 /* Create new breakpoint locations for B (a hardware or software
14290 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
14291 zero, then B is a ranged breakpoint. Only recreates locations for
14292 FILTER_PSPACE. Locations of other program spaces are left
14293 untouched. */
14294
14295 void
14296 update_breakpoint_locations (struct breakpoint *b,
14297 struct program_space *filter_pspace,
14298 struct symtabs_and_lines sals,
14299 struct symtabs_and_lines sals_end)
14300 {
14301 int i;
14302 struct bp_location *existing_locations;
14303
14304 if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
14305 {
14306 /* Ranged breakpoints have only one start location and one end
14307 location. */
14308 b->enable_state = bp_disabled;
14309 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
14310 "multiple locations found\n"),
14311 b->number);
14312 return;
14313 }
14314
14315 /* If there's no new locations, and all existing locations are
14316 pending, don't do anything. This optimizes the common case where
14317 all locations are in the same shared library, that was unloaded.
14318 We'd like to retain the location, so that when the library is
14319 loaded again, we don't loose the enabled/disabled status of the
14320 individual locations. */
14321 if (all_locations_are_pending (b, filter_pspace) && sals.nelts == 0)
14322 return;
14323
14324 existing_locations = hoist_existing_locations (b, filter_pspace);
14325
14326 for (i = 0; i < sals.nelts; ++i)
14327 {
14328 struct bp_location *new_loc;
14329
14330 switch_to_program_space_and_thread (sals.sals[i].pspace);
14331
14332 new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
14333
14334 /* Reparse conditions, they might contain references to the
14335 old symtab. */
14336 if (b->cond_string != NULL)
14337 {
14338 const char *s;
14339
14340 s = b->cond_string;
14341 TRY
14342 {
14343 new_loc->cond = parse_exp_1 (&s, sals.sals[i].pc,
14344 block_for_pc (sals.sals[i].pc),
14345 0);
14346 }
14347 CATCH (e, RETURN_MASK_ERROR)
14348 {
14349 warning (_("failed to reevaluate condition "
14350 "for breakpoint %d: %s"),
14351 b->number, e.message);
14352 new_loc->enabled = 0;
14353 }
14354 END_CATCH
14355 }
14356
14357 if (sals_end.nelts)
14358 {
14359 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
14360
14361 new_loc->length = end - sals.sals[0].pc + 1;
14362 }
14363 }
14364
14365 /* If possible, carry over 'disable' status from existing
14366 breakpoints. */
14367 {
14368 struct bp_location *e = existing_locations;
14369 /* If there are multiple breakpoints with the same function name,
14370 e.g. for inline functions, comparing function names won't work.
14371 Instead compare pc addresses; this is just a heuristic as things
14372 may have moved, but in practice it gives the correct answer
14373 often enough until a better solution is found. */
14374 int have_ambiguous_names = ambiguous_names_p (b->loc);
14375
14376 for (; e; e = e->next)
14377 {
14378 if (!e->enabled && e->function_name)
14379 {
14380 struct bp_location *l = b->loc;
14381 if (have_ambiguous_names)
14382 {
14383 for (; l; l = l->next)
14384 if (breakpoint_locations_match (e, l))
14385 {
14386 l->enabled = 0;
14387 break;
14388 }
14389 }
14390 else
14391 {
14392 for (; l; l = l->next)
14393 if (l->function_name
14394 && strcmp (e->function_name, l->function_name) == 0)
14395 {
14396 l->enabled = 0;
14397 break;
14398 }
14399 }
14400 }
14401 }
14402 }
14403
14404 if (!locations_are_equal (existing_locations, b->loc))
14405 observer_notify_breakpoint_modified (b);
14406 }
14407
14408 /* Find the SaL locations corresponding to the given LOCATION.
14409 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
14410
14411 static struct symtabs_and_lines
14412 location_to_sals (struct breakpoint *b, struct event_location *location,
14413 struct program_space *search_pspace, int *found)
14414 {
14415 struct symtabs_and_lines sals = {0};
14416 struct gdb_exception exception = exception_none;
14417
14418 gdb_assert (b->ops != NULL);
14419
14420 TRY
14421 {
14422 b->ops->decode_location (b, location, search_pspace, &sals);
14423 }
14424 CATCH (e, RETURN_MASK_ERROR)
14425 {
14426 int not_found_and_ok = 0;
14427
14428 exception = e;
14429
14430 /* For pending breakpoints, it's expected that parsing will
14431 fail until the right shared library is loaded. User has
14432 already told to create pending breakpoints and don't need
14433 extra messages. If breakpoint is in bp_shlib_disabled
14434 state, then user already saw the message about that
14435 breakpoint being disabled, and don't want to see more
14436 errors. */
14437 if (e.error == NOT_FOUND_ERROR
14438 && (b->condition_not_parsed
14439 || (b->loc != NULL
14440 && search_pspace != NULL
14441 && b->loc->pspace != search_pspace)
14442 || (b->loc && b->loc->shlib_disabled)
14443 || (b->loc && b->loc->pspace->executing_startup)
14444 || b->enable_state == bp_disabled))
14445 not_found_and_ok = 1;
14446
14447 if (!not_found_and_ok)
14448 {
14449 /* We surely don't want to warn about the same breakpoint
14450 10 times. One solution, implemented here, is disable
14451 the breakpoint on error. Another solution would be to
14452 have separate 'warning emitted' flag. Since this
14453 happens only when a binary has changed, I don't know
14454 which approach is better. */
14455 b->enable_state = bp_disabled;
14456 throw_exception (e);
14457 }
14458 }
14459 END_CATCH
14460
14461 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
14462 {
14463 int i;
14464
14465 for (i = 0; i < sals.nelts; ++i)
14466 resolve_sal_pc (&sals.sals[i]);
14467 if (b->condition_not_parsed && b->extra_string != NULL)
14468 {
14469 char *cond_string, *extra_string;
14470 int thread, task;
14471
14472 find_condition_and_thread (b->extra_string, sals.sals[0].pc,
14473 &cond_string, &thread, &task,
14474 &extra_string);
14475 gdb_assert (b->cond_string == NULL);
14476 if (cond_string)
14477 b->cond_string = cond_string;
14478 b->thread = thread;
14479 b->task = task;
14480 if (extra_string)
14481 {
14482 xfree (b->extra_string);
14483 b->extra_string = extra_string;
14484 }
14485 b->condition_not_parsed = 0;
14486 }
14487
14488 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
14489 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
14490
14491 *found = 1;
14492 }
14493 else
14494 *found = 0;
14495
14496 return sals;
14497 }
14498
14499 /* The default re_set method, for typical hardware or software
14500 breakpoints. Reevaluate the breakpoint and recreate its
14501 locations. */
14502
14503 static void
14504 breakpoint_re_set_default (struct breakpoint *b)
14505 {
14506 int found;
14507 struct symtabs_and_lines sals, sals_end;
14508 struct symtabs_and_lines expanded = {0};
14509 struct symtabs_and_lines expanded_end = {0};
14510 struct program_space *filter_pspace = current_program_space;
14511
14512 sals = location_to_sals (b, b->location, filter_pspace, &found);
14513 if (found)
14514 {
14515 make_cleanup (xfree, sals.sals);
14516 expanded = sals;
14517 }
14518
14519 if (b->location_range_end != NULL)
14520 {
14521 sals_end = location_to_sals (b, b->location_range_end,
14522 filter_pspace, &found);
14523 if (found)
14524 {
14525 make_cleanup (xfree, sals_end.sals);
14526 expanded_end = sals_end;
14527 }
14528 }
14529
14530 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
14531 }
14532
14533 /* Default method for creating SALs from an address string. It basically
14534 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
14535
14536 static void
14537 create_sals_from_location_default (const struct event_location *location,
14538 struct linespec_result *canonical,
14539 enum bptype type_wanted)
14540 {
14541 parse_breakpoint_sals (location, canonical);
14542 }
14543
14544 /* Call create_breakpoints_sal for the given arguments. This is the default
14545 function for the `create_breakpoints_sal' method of
14546 breakpoint_ops. */
14547
14548 static void
14549 create_breakpoints_sal_default (struct gdbarch *gdbarch,
14550 struct linespec_result *canonical,
14551 char *cond_string,
14552 char *extra_string,
14553 enum bptype type_wanted,
14554 enum bpdisp disposition,
14555 int thread,
14556 int task, int ignore_count,
14557 const struct breakpoint_ops *ops,
14558 int from_tty, int enabled,
14559 int internal, unsigned flags)
14560 {
14561 create_breakpoints_sal (gdbarch, canonical, cond_string,
14562 extra_string,
14563 type_wanted, disposition,
14564 thread, task, ignore_count, ops, from_tty,
14565 enabled, internal, flags);
14566 }
14567
14568 /* Decode the line represented by S by calling decode_line_full. This is the
14569 default function for the `decode_location' method of breakpoint_ops. */
14570
14571 static void
14572 decode_location_default (struct breakpoint *b,
14573 const struct event_location *location,
14574 struct program_space *search_pspace,
14575 struct symtabs_and_lines *sals)
14576 {
14577 struct linespec_result canonical;
14578
14579 init_linespec_result (&canonical);
14580 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
14581 (struct symtab *) NULL, 0,
14582 &canonical, multiple_symbols_all,
14583 b->filter);
14584
14585 /* We should get 0 or 1 resulting SALs. */
14586 gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
14587
14588 if (VEC_length (linespec_sals, canonical.sals) > 0)
14589 {
14590 struct linespec_sals *lsal;
14591
14592 lsal = VEC_index (linespec_sals, canonical.sals, 0);
14593 *sals = lsal->sals;
14594 /* Arrange it so the destructor does not free the
14595 contents. */
14596 lsal->sals.sals = NULL;
14597 }
14598
14599 destroy_linespec_result (&canonical);
14600 }
14601
14602 /* Prepare the global context for a re-set of breakpoint B. */
14603
14604 static struct cleanup *
14605 prepare_re_set_context (struct breakpoint *b)
14606 {
14607 input_radix = b->input_radix;
14608 set_language (b->language);
14609
14610 return make_cleanup (null_cleanup, NULL);
14611 }
14612
14613 /* Reset a breakpoint given it's struct breakpoint * BINT.
14614 The value we return ends up being the return value from catch_errors.
14615 Unused in this case. */
14616
14617 static int
14618 breakpoint_re_set_one (void *bint)
14619 {
14620 /* Get past catch_errs. */
14621 struct breakpoint *b = (struct breakpoint *) bint;
14622 struct cleanup *cleanups;
14623
14624 cleanups = prepare_re_set_context (b);
14625 b->ops->re_set (b);
14626 do_cleanups (cleanups);
14627 return 0;
14628 }
14629
14630 /* Re-set breakpoint locations for the current program space.
14631 Locations bound to other program spaces are left untouched. */
14632
14633 void
14634 breakpoint_re_set (void)
14635 {
14636 struct breakpoint *b, *b_tmp;
14637 enum language save_language;
14638 int save_input_radix;
14639 struct cleanup *old_chain;
14640
14641 save_language = current_language->la_language;
14642 save_input_radix = input_radix;
14643 old_chain = save_current_space_and_thread ();
14644
14645 /* Note: we must not try to insert locations until after all
14646 breakpoints have been re-set. Otherwise, e.g., when re-setting
14647 breakpoint 1, we'd insert the locations of breakpoint 2, which
14648 hadn't been re-set yet, and thus may have stale locations. */
14649
14650 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14651 {
14652 /* Format possible error msg. */
14653 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
14654 b->number);
14655 struct cleanup *cleanups = make_cleanup (xfree, message);
14656 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
14657 do_cleanups (cleanups);
14658 }
14659 set_language (save_language);
14660 input_radix = save_input_radix;
14661
14662 jit_breakpoint_re_set ();
14663
14664 do_cleanups (old_chain);
14665
14666 create_overlay_event_breakpoint ();
14667 create_longjmp_master_breakpoint ();
14668 create_std_terminate_master_breakpoint ();
14669 create_exception_master_breakpoint ();
14670
14671 /* Now we can insert. */
14672 update_global_location_list (UGLL_MAY_INSERT);
14673 }
14674 \f
14675 /* Reset the thread number of this breakpoint:
14676
14677 - If the breakpoint is for all threads, leave it as-is.
14678 - Else, reset it to the current thread for inferior_ptid. */
14679 void
14680 breakpoint_re_set_thread (struct breakpoint *b)
14681 {
14682 if (b->thread != -1)
14683 {
14684 if (in_thread_list (inferior_ptid))
14685 b->thread = ptid_to_global_thread_id (inferior_ptid);
14686
14687 /* We're being called after following a fork. The new fork is
14688 selected as current, and unless this was a vfork will have a
14689 different program space from the original thread. Reset that
14690 as well. */
14691 b->loc->pspace = current_program_space;
14692 }
14693 }
14694
14695 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14696 If from_tty is nonzero, it prints a message to that effect,
14697 which ends with a period (no newline). */
14698
14699 void
14700 set_ignore_count (int bptnum, int count, int from_tty)
14701 {
14702 struct breakpoint *b;
14703
14704 if (count < 0)
14705 count = 0;
14706
14707 ALL_BREAKPOINTS (b)
14708 if (b->number == bptnum)
14709 {
14710 if (is_tracepoint (b))
14711 {
14712 if (from_tty && count != 0)
14713 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14714 bptnum);
14715 return;
14716 }
14717
14718 b->ignore_count = count;
14719 if (from_tty)
14720 {
14721 if (count == 0)
14722 printf_filtered (_("Will stop next time "
14723 "breakpoint %d is reached."),
14724 bptnum);
14725 else if (count == 1)
14726 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
14727 bptnum);
14728 else
14729 printf_filtered (_("Will ignore next %d "
14730 "crossings of breakpoint %d."),
14731 count, bptnum);
14732 }
14733 observer_notify_breakpoint_modified (b);
14734 return;
14735 }
14736
14737 error (_("No breakpoint number %d."), bptnum);
14738 }
14739
14740 /* Command to set ignore-count of breakpoint N to COUNT. */
14741
14742 static void
14743 ignore_command (char *args, int from_tty)
14744 {
14745 char *p = args;
14746 int num;
14747
14748 if (p == 0)
14749 error_no_arg (_("a breakpoint number"));
14750
14751 num = get_number (&p);
14752 if (num == 0)
14753 error (_("bad breakpoint number: '%s'"), args);
14754 if (*p == 0)
14755 error (_("Second argument (specified ignore-count) is missing."));
14756
14757 set_ignore_count (num,
14758 longest_to_int (value_as_long (parse_and_eval (p))),
14759 from_tty);
14760 if (from_tty)
14761 printf_filtered ("\n");
14762 }
14763 \f
14764 /* Call FUNCTION on each of the breakpoints
14765 whose numbers are given in ARGS. */
14766
14767 static void
14768 map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
14769 void *),
14770 void *data)
14771 {
14772 int num;
14773 struct breakpoint *b, *tmp;
14774
14775 if (args == 0 || *args == '\0')
14776 error_no_arg (_("one or more breakpoint numbers"));
14777
14778 number_or_range_parser parser (args);
14779
14780 while (!parser.finished ())
14781 {
14782 const char *p = parser.cur_tok ();
14783 bool match = false;
14784
14785 num = parser.get_number ();
14786 if (num == 0)
14787 {
14788 warning (_("bad breakpoint number at or near '%s'"), p);
14789 }
14790 else
14791 {
14792 ALL_BREAKPOINTS_SAFE (b, tmp)
14793 if (b->number == num)
14794 {
14795 match = true;
14796 function (b, data);
14797 break;
14798 }
14799 if (!match)
14800 printf_unfiltered (_("No breakpoint number %d.\n"), num);
14801 }
14802 }
14803 }
14804
14805 static struct bp_location *
14806 find_location_by_number (char *number)
14807 {
14808 char *dot = strchr (number, '.');
14809 char *p1;
14810 int bp_num;
14811 int loc_num;
14812 struct breakpoint *b;
14813 struct bp_location *loc;
14814
14815 *dot = '\0';
14816
14817 p1 = number;
14818 bp_num = get_number (&p1);
14819 if (bp_num == 0)
14820 error (_("Bad breakpoint number '%s'"), number);
14821
14822 ALL_BREAKPOINTS (b)
14823 if (b->number == bp_num)
14824 {
14825 break;
14826 }
14827
14828 if (!b || b->number != bp_num)
14829 error (_("Bad breakpoint number '%s'"), number);
14830
14831 p1 = dot+1;
14832 loc_num = get_number (&p1);
14833 if (loc_num == 0)
14834 error (_("Bad breakpoint location number '%s'"), number);
14835
14836 --loc_num;
14837 loc = b->loc;
14838 for (;loc_num && loc; --loc_num, loc = loc->next)
14839 ;
14840 if (!loc)
14841 error (_("Bad breakpoint location number '%s'"), dot+1);
14842
14843 return loc;
14844 }
14845
14846
14847 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14848 If from_tty is nonzero, it prints a message to that effect,
14849 which ends with a period (no newline). */
14850
14851 void
14852 disable_breakpoint (struct breakpoint *bpt)
14853 {
14854 /* Never disable a watchpoint scope breakpoint; we want to
14855 hit them when we leave scope so we can delete both the
14856 watchpoint and its scope breakpoint at that time. */
14857 if (bpt->type == bp_watchpoint_scope)
14858 return;
14859
14860 bpt->enable_state = bp_disabled;
14861
14862 /* Mark breakpoint locations modified. */
14863 mark_breakpoint_modified (bpt);
14864
14865 if (target_supports_enable_disable_tracepoint ()
14866 && current_trace_status ()->running && is_tracepoint (bpt))
14867 {
14868 struct bp_location *location;
14869
14870 for (location = bpt->loc; location; location = location->next)
14871 target_disable_tracepoint (location);
14872 }
14873
14874 update_global_location_list (UGLL_DONT_INSERT);
14875
14876 observer_notify_breakpoint_modified (bpt);
14877 }
14878
14879 /* A callback for iterate_over_related_breakpoints. */
14880
14881 static void
14882 do_disable_breakpoint (struct breakpoint *b, void *ignore)
14883 {
14884 disable_breakpoint (b);
14885 }
14886
14887 /* A callback for map_breakpoint_numbers that calls
14888 disable_breakpoint. */
14889
14890 static void
14891 do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
14892 {
14893 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
14894 }
14895
14896 static void
14897 disable_command (char *args, int from_tty)
14898 {
14899 if (args == 0)
14900 {
14901 struct breakpoint *bpt;
14902
14903 ALL_BREAKPOINTS (bpt)
14904 if (user_breakpoint_p (bpt))
14905 disable_breakpoint (bpt);
14906 }
14907 else
14908 {
14909 char *num = extract_arg (&args);
14910
14911 while (num)
14912 {
14913 if (strchr (num, '.'))
14914 {
14915 struct bp_location *loc = find_location_by_number (num);
14916
14917 if (loc)
14918 {
14919 if (loc->enabled)
14920 {
14921 loc->enabled = 0;
14922 mark_breakpoint_location_modified (loc);
14923 }
14924 if (target_supports_enable_disable_tracepoint ()
14925 && current_trace_status ()->running && loc->owner
14926 && is_tracepoint (loc->owner))
14927 target_disable_tracepoint (loc);
14928 }
14929 update_global_location_list (UGLL_DONT_INSERT);
14930 }
14931 else
14932 map_breakpoint_numbers (num, do_map_disable_breakpoint, NULL);
14933 num = extract_arg (&args);
14934 }
14935 }
14936 }
14937
14938 static void
14939 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14940 int count)
14941 {
14942 int target_resources_ok;
14943
14944 if (bpt->type == bp_hardware_breakpoint)
14945 {
14946 int i;
14947 i = hw_breakpoint_used_count ();
14948 target_resources_ok =
14949 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
14950 i + 1, 0);
14951 if (target_resources_ok == 0)
14952 error (_("No hardware breakpoint support in the target."));
14953 else if (target_resources_ok < 0)
14954 error (_("Hardware breakpoints used exceeds limit."));
14955 }
14956
14957 if (is_watchpoint (bpt))
14958 {
14959 /* Initialize it just to avoid a GCC false warning. */
14960 enum enable_state orig_enable_state = bp_disabled;
14961
14962 TRY
14963 {
14964 struct watchpoint *w = (struct watchpoint *) bpt;
14965
14966 orig_enable_state = bpt->enable_state;
14967 bpt->enable_state = bp_enabled;
14968 update_watchpoint (w, 1 /* reparse */);
14969 }
14970 CATCH (e, RETURN_MASK_ALL)
14971 {
14972 bpt->enable_state = orig_enable_state;
14973 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14974 bpt->number);
14975 return;
14976 }
14977 END_CATCH
14978 }
14979
14980 bpt->enable_state = bp_enabled;
14981
14982 /* Mark breakpoint locations modified. */
14983 mark_breakpoint_modified (bpt);
14984
14985 if (target_supports_enable_disable_tracepoint ()
14986 && current_trace_status ()->running && is_tracepoint (bpt))
14987 {
14988 struct bp_location *location;
14989
14990 for (location = bpt->loc; location; location = location->next)
14991 target_enable_tracepoint (location);
14992 }
14993
14994 bpt->disposition = disposition;
14995 bpt->enable_count = count;
14996 update_global_location_list (UGLL_MAY_INSERT);
14997
14998 observer_notify_breakpoint_modified (bpt);
14999 }
15000
15001
15002 void
15003 enable_breakpoint (struct breakpoint *bpt)
15004 {
15005 enable_breakpoint_disp (bpt, bpt->disposition, 0);
15006 }
15007
15008 static void
15009 do_enable_breakpoint (struct breakpoint *bpt, void *arg)
15010 {
15011 enable_breakpoint (bpt);
15012 }
15013
15014 /* A callback for map_breakpoint_numbers that calls
15015 enable_breakpoint. */
15016
15017 static void
15018 do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
15019 {
15020 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
15021 }
15022
15023 /* The enable command enables the specified breakpoints (or all defined
15024 breakpoints) so they once again become (or continue to be) effective
15025 in stopping the inferior. */
15026
15027 static void
15028 enable_command (char *args, int from_tty)
15029 {
15030 if (args == 0)
15031 {
15032 struct breakpoint *bpt;
15033
15034 ALL_BREAKPOINTS (bpt)
15035 if (user_breakpoint_p (bpt))
15036 enable_breakpoint (bpt);
15037 }
15038 else
15039 {
15040 char *num = extract_arg (&args);
15041
15042 while (num)
15043 {
15044 if (strchr (num, '.'))
15045 {
15046 struct bp_location *loc = find_location_by_number (num);
15047
15048 if (loc)
15049 {
15050 if (!loc->enabled)
15051 {
15052 loc->enabled = 1;
15053 mark_breakpoint_location_modified (loc);
15054 }
15055 if (target_supports_enable_disable_tracepoint ()
15056 && current_trace_status ()->running && loc->owner
15057 && is_tracepoint (loc->owner))
15058 target_enable_tracepoint (loc);
15059 }
15060 update_global_location_list (UGLL_MAY_INSERT);
15061 }
15062 else
15063 map_breakpoint_numbers (num, do_map_enable_breakpoint, NULL);
15064 num = extract_arg (&args);
15065 }
15066 }
15067 }
15068
15069 /* This struct packages up disposition data for application to multiple
15070 breakpoints. */
15071
15072 struct disp_data
15073 {
15074 enum bpdisp disp;
15075 int count;
15076 };
15077
15078 static void
15079 do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
15080 {
15081 struct disp_data disp_data = *(struct disp_data *) arg;
15082
15083 enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
15084 }
15085
15086 static void
15087 do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
15088 {
15089 struct disp_data disp = { disp_disable, 1 };
15090
15091 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
15092 }
15093
15094 static void
15095 enable_once_command (char *args, int from_tty)
15096 {
15097 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
15098 }
15099
15100 static void
15101 do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
15102 {
15103 struct disp_data disp = { disp_disable, *(int *) countptr };
15104
15105 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
15106 }
15107
15108 static void
15109 enable_count_command (char *args, int from_tty)
15110 {
15111 int count;
15112
15113 if (args == NULL)
15114 error_no_arg (_("hit count"));
15115
15116 count = get_number (&args);
15117
15118 map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
15119 }
15120
15121 static void
15122 do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
15123 {
15124 struct disp_data disp = { disp_del, 1 };
15125
15126 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
15127 }
15128
15129 static void
15130 enable_delete_command (char *args, int from_tty)
15131 {
15132 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
15133 }
15134 \f
15135 static void
15136 set_breakpoint_cmd (char *args, int from_tty)
15137 {
15138 }
15139
15140 static void
15141 show_breakpoint_cmd (char *args, int from_tty)
15142 {
15143 }
15144
15145 /* Invalidate last known value of any hardware watchpoint if
15146 the memory which that value represents has been written to by
15147 GDB itself. */
15148
15149 static void
15150 invalidate_bp_value_on_memory_change (struct inferior *inferior,
15151 CORE_ADDR addr, ssize_t len,
15152 const bfd_byte *data)
15153 {
15154 struct breakpoint *bp;
15155
15156 ALL_BREAKPOINTS (bp)
15157 if (bp->enable_state == bp_enabled
15158 && bp->type == bp_hardware_watchpoint)
15159 {
15160 struct watchpoint *wp = (struct watchpoint *) bp;
15161
15162 if (wp->val_valid && wp->val)
15163 {
15164 struct bp_location *loc;
15165
15166 for (loc = bp->loc; loc != NULL; loc = loc->next)
15167 if (loc->loc_type == bp_loc_hardware_watchpoint
15168 && loc->address + loc->length > addr
15169 && addr + len > loc->address)
15170 {
15171 value_free (wp->val);
15172 wp->val = NULL;
15173 wp->val_valid = 0;
15174 }
15175 }
15176 }
15177 }
15178
15179 /* Create and insert a breakpoint for software single step. */
15180
15181 void
15182 insert_single_step_breakpoint (struct gdbarch *gdbarch,
15183 struct address_space *aspace,
15184 CORE_ADDR next_pc)
15185 {
15186 struct thread_info *tp = inferior_thread ();
15187 struct symtab_and_line sal;
15188 CORE_ADDR pc = next_pc;
15189
15190 if (tp->control.single_step_breakpoints == NULL)
15191 {
15192 tp->control.single_step_breakpoints
15193 = new_single_step_breakpoint (tp->global_num, gdbarch);
15194 }
15195
15196 sal = find_pc_line (pc, 0);
15197 sal.pc = pc;
15198 sal.section = find_pc_overlay (pc);
15199 sal.explicit_pc = 1;
15200 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
15201
15202 update_global_location_list (UGLL_INSERT);
15203 }
15204
15205 /* See breakpoint.h. */
15206
15207 int
15208 breakpoint_has_location_inserted_here (struct breakpoint *bp,
15209 struct address_space *aspace,
15210 CORE_ADDR pc)
15211 {
15212 struct bp_location *loc;
15213
15214 for (loc = bp->loc; loc != NULL; loc = loc->next)
15215 if (loc->inserted
15216 && breakpoint_location_address_match (loc, aspace, pc))
15217 return 1;
15218
15219 return 0;
15220 }
15221
15222 /* Check whether a software single-step breakpoint is inserted at
15223 PC. */
15224
15225 int
15226 single_step_breakpoint_inserted_here_p (struct address_space *aspace,
15227 CORE_ADDR pc)
15228 {
15229 struct breakpoint *bpt;
15230
15231 ALL_BREAKPOINTS (bpt)
15232 {
15233 if (bpt->type == bp_single_step
15234 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
15235 return 1;
15236 }
15237 return 0;
15238 }
15239
15240 /* Tracepoint-specific operations. */
15241
15242 /* Set tracepoint count to NUM. */
15243 static void
15244 set_tracepoint_count (int num)
15245 {
15246 tracepoint_count = num;
15247 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
15248 }
15249
15250 static void
15251 trace_command (char *arg, int from_tty)
15252 {
15253 struct breakpoint_ops *ops;
15254 struct event_location *location;
15255 struct cleanup *back_to;
15256
15257 location = string_to_event_location (&arg, current_language);
15258 back_to = make_cleanup_delete_event_location (location);
15259 if (location != NULL
15260 && event_location_type (location) == PROBE_LOCATION)
15261 ops = &tracepoint_probe_breakpoint_ops;
15262 else
15263 ops = &tracepoint_breakpoint_ops;
15264
15265 create_breakpoint (get_current_arch (),
15266 location,
15267 NULL, 0, arg, 1 /* parse arg */,
15268 0 /* tempflag */,
15269 bp_tracepoint /* type_wanted */,
15270 0 /* Ignore count */,
15271 pending_break_support,
15272 ops,
15273 from_tty,
15274 1 /* enabled */,
15275 0 /* internal */, 0);
15276 do_cleanups (back_to);
15277 }
15278
15279 static void
15280 ftrace_command (char *arg, int from_tty)
15281 {
15282 struct event_location *location;
15283 struct cleanup *back_to;
15284
15285 location = string_to_event_location (&arg, current_language);
15286 back_to = make_cleanup_delete_event_location (location);
15287 create_breakpoint (get_current_arch (),
15288 location,
15289 NULL, 0, arg, 1 /* parse arg */,
15290 0 /* tempflag */,
15291 bp_fast_tracepoint /* type_wanted */,
15292 0 /* Ignore count */,
15293 pending_break_support,
15294 &tracepoint_breakpoint_ops,
15295 from_tty,
15296 1 /* enabled */,
15297 0 /* internal */, 0);
15298 do_cleanups (back_to);
15299 }
15300
15301 /* strace command implementation. Creates a static tracepoint. */
15302
15303 static void
15304 strace_command (char *arg, int from_tty)
15305 {
15306 struct breakpoint_ops *ops;
15307 struct event_location *location;
15308 struct cleanup *back_to;
15309
15310 /* Decide if we are dealing with a static tracepoint marker (`-m'),
15311 or with a normal static tracepoint. */
15312 if (arg && startswith (arg, "-m") && isspace (arg[2]))
15313 {
15314 ops = &strace_marker_breakpoint_ops;
15315 location = new_linespec_location (&arg);
15316 }
15317 else
15318 {
15319 ops = &tracepoint_breakpoint_ops;
15320 location = string_to_event_location (&arg, current_language);
15321 }
15322
15323 back_to = make_cleanup_delete_event_location (location);
15324 create_breakpoint (get_current_arch (),
15325 location,
15326 NULL, 0, arg, 1 /* parse arg */,
15327 0 /* tempflag */,
15328 bp_static_tracepoint /* type_wanted */,
15329 0 /* Ignore count */,
15330 pending_break_support,
15331 ops,
15332 from_tty,
15333 1 /* enabled */,
15334 0 /* internal */, 0);
15335 do_cleanups (back_to);
15336 }
15337
15338 /* Set up a fake reader function that gets command lines from a linked
15339 list that was acquired during tracepoint uploading. */
15340
15341 static struct uploaded_tp *this_utp;
15342 static int next_cmd;
15343
15344 static char *
15345 read_uploaded_action (void)
15346 {
15347 char *rslt;
15348
15349 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
15350
15351 next_cmd++;
15352
15353 return rslt;
15354 }
15355
15356 /* Given information about a tracepoint as recorded on a target (which
15357 can be either a live system or a trace file), attempt to create an
15358 equivalent GDB tracepoint. This is not a reliable process, since
15359 the target does not necessarily have all the information used when
15360 the tracepoint was originally defined. */
15361
15362 struct tracepoint *
15363 create_tracepoint_from_upload (struct uploaded_tp *utp)
15364 {
15365 char *addr_str, small_buf[100];
15366 struct tracepoint *tp;
15367 struct event_location *location;
15368 struct cleanup *cleanup;
15369
15370 if (utp->at_string)
15371 addr_str = utp->at_string;
15372 else
15373 {
15374 /* In the absence of a source location, fall back to raw
15375 address. Since there is no way to confirm that the address
15376 means the same thing as when the trace was started, warn the
15377 user. */
15378 warning (_("Uploaded tracepoint %d has no "
15379 "source location, using raw address"),
15380 utp->number);
15381 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
15382 addr_str = small_buf;
15383 }
15384
15385 /* There's not much we can do with a sequence of bytecodes. */
15386 if (utp->cond && !utp->cond_string)
15387 warning (_("Uploaded tracepoint %d condition "
15388 "has no source form, ignoring it"),
15389 utp->number);
15390
15391 location = string_to_event_location (&addr_str, current_language);
15392 cleanup = make_cleanup_delete_event_location (location);
15393 if (!create_breakpoint (get_current_arch (),
15394 location,
15395 utp->cond_string, -1, addr_str,
15396 0 /* parse cond/thread */,
15397 0 /* tempflag */,
15398 utp->type /* type_wanted */,
15399 0 /* Ignore count */,
15400 pending_break_support,
15401 &tracepoint_breakpoint_ops,
15402 0 /* from_tty */,
15403 utp->enabled /* enabled */,
15404 0 /* internal */,
15405 CREATE_BREAKPOINT_FLAGS_INSERTED))
15406 {
15407 do_cleanups (cleanup);
15408 return NULL;
15409 }
15410
15411 do_cleanups (cleanup);
15412
15413 /* Get the tracepoint we just created. */
15414 tp = get_tracepoint (tracepoint_count);
15415 gdb_assert (tp != NULL);
15416
15417 if (utp->pass > 0)
15418 {
15419 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
15420 tp->base.number);
15421
15422 trace_pass_command (small_buf, 0);
15423 }
15424
15425 /* If we have uploaded versions of the original commands, set up a
15426 special-purpose "reader" function and call the usual command line
15427 reader, then pass the result to the breakpoint command-setting
15428 function. */
15429 if (!VEC_empty (char_ptr, utp->cmd_strings))
15430 {
15431 struct command_line *cmd_list;
15432
15433 this_utp = utp;
15434 next_cmd = 0;
15435
15436 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
15437
15438 breakpoint_set_commands (&tp->base, cmd_list);
15439 }
15440 else if (!VEC_empty (char_ptr, utp->actions)
15441 || !VEC_empty (char_ptr, utp->step_actions))
15442 warning (_("Uploaded tracepoint %d actions "
15443 "have no source form, ignoring them"),
15444 utp->number);
15445
15446 /* Copy any status information that might be available. */
15447 tp->base.hit_count = utp->hit_count;
15448 tp->traceframe_usage = utp->traceframe_usage;
15449
15450 return tp;
15451 }
15452
15453 /* Print information on tracepoint number TPNUM_EXP, or all if
15454 omitted. */
15455
15456 static void
15457 tracepoints_info (char *args, int from_tty)
15458 {
15459 struct ui_out *uiout = current_uiout;
15460 int num_printed;
15461
15462 num_printed = breakpoint_1 (args, 0, is_tracepoint);
15463
15464 if (num_printed == 0)
15465 {
15466 if (args == NULL || *args == '\0')
15467 ui_out_message (uiout, 0, "No tracepoints.\n");
15468 else
15469 ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
15470 }
15471
15472 default_collect_info ();
15473 }
15474
15475 /* The 'enable trace' command enables tracepoints.
15476 Not supported by all targets. */
15477 static void
15478 enable_trace_command (char *args, int from_tty)
15479 {
15480 enable_command (args, from_tty);
15481 }
15482
15483 /* The 'disable trace' command disables tracepoints.
15484 Not supported by all targets. */
15485 static void
15486 disable_trace_command (char *args, int from_tty)
15487 {
15488 disable_command (args, from_tty);
15489 }
15490
15491 /* Remove a tracepoint (or all if no argument). */
15492 static void
15493 delete_trace_command (char *arg, int from_tty)
15494 {
15495 struct breakpoint *b, *b_tmp;
15496
15497 dont_repeat ();
15498
15499 if (arg == 0)
15500 {
15501 int breaks_to_delete = 0;
15502
15503 /* Delete all breakpoints if no argument.
15504 Do not delete internal or call-dummy breakpoints, these
15505 have to be deleted with an explicit breakpoint number
15506 argument. */
15507 ALL_TRACEPOINTS (b)
15508 if (is_tracepoint (b) && user_breakpoint_p (b))
15509 {
15510 breaks_to_delete = 1;
15511 break;
15512 }
15513
15514 /* Ask user only if there are some breakpoints to delete. */
15515 if (!from_tty
15516 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
15517 {
15518 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15519 if (is_tracepoint (b) && user_breakpoint_p (b))
15520 delete_breakpoint (b);
15521 }
15522 }
15523 else
15524 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
15525 }
15526
15527 /* Helper function for trace_pass_command. */
15528
15529 static void
15530 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
15531 {
15532 tp->pass_count = count;
15533 observer_notify_breakpoint_modified (&tp->base);
15534 if (from_tty)
15535 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
15536 tp->base.number, count);
15537 }
15538
15539 /* Set passcount for tracepoint.
15540
15541 First command argument is passcount, second is tracepoint number.
15542 If tracepoint number omitted, apply to most recently defined.
15543 Also accepts special argument "all". */
15544
15545 static void
15546 trace_pass_command (char *args, int from_tty)
15547 {
15548 struct tracepoint *t1;
15549 unsigned int count;
15550
15551 if (args == 0 || *args == 0)
15552 error (_("passcount command requires an "
15553 "argument (count + optional TP num)"));
15554
15555 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
15556
15557 args = skip_spaces (args);
15558 if (*args && strncasecmp (args, "all", 3) == 0)
15559 {
15560 struct breakpoint *b;
15561
15562 args += 3; /* Skip special argument "all". */
15563 if (*args)
15564 error (_("Junk at end of arguments."));
15565
15566 ALL_TRACEPOINTS (b)
15567 {
15568 t1 = (struct tracepoint *) b;
15569 trace_pass_set_count (t1, count, from_tty);
15570 }
15571 }
15572 else if (*args == '\0')
15573 {
15574 t1 = get_tracepoint_by_number (&args, NULL);
15575 if (t1)
15576 trace_pass_set_count (t1, count, from_tty);
15577 }
15578 else
15579 {
15580 number_or_range_parser parser (args);
15581 while (!parser.finished ())
15582 {
15583 t1 = get_tracepoint_by_number (&args, &parser);
15584 if (t1)
15585 trace_pass_set_count (t1, count, from_tty);
15586 }
15587 }
15588 }
15589
15590 struct tracepoint *
15591 get_tracepoint (int num)
15592 {
15593 struct breakpoint *t;
15594
15595 ALL_TRACEPOINTS (t)
15596 if (t->number == num)
15597 return (struct tracepoint *) t;
15598
15599 return NULL;
15600 }
15601
15602 /* Find the tracepoint with the given target-side number (which may be
15603 different from the tracepoint number after disconnecting and
15604 reconnecting). */
15605
15606 struct tracepoint *
15607 get_tracepoint_by_number_on_target (int num)
15608 {
15609 struct breakpoint *b;
15610
15611 ALL_TRACEPOINTS (b)
15612 {
15613 struct tracepoint *t = (struct tracepoint *) b;
15614
15615 if (t->number_on_target == num)
15616 return t;
15617 }
15618
15619 return NULL;
15620 }
15621
15622 /* Utility: parse a tracepoint number and look it up in the list.
15623 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
15624 If the argument is missing, the most recent tracepoint
15625 (tracepoint_count) is returned. */
15626
15627 struct tracepoint *
15628 get_tracepoint_by_number (char **arg,
15629 number_or_range_parser *parser)
15630 {
15631 struct breakpoint *t;
15632 int tpnum;
15633 char *instring = arg == NULL ? NULL : *arg;
15634
15635 if (parser != NULL)
15636 {
15637 gdb_assert (!parser->finished ());
15638 tpnum = parser->get_number ();
15639 }
15640 else if (arg == NULL || *arg == NULL || ! **arg)
15641 tpnum = tracepoint_count;
15642 else
15643 tpnum = get_number (arg);
15644
15645 if (tpnum <= 0)
15646 {
15647 if (instring && *instring)
15648 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15649 instring);
15650 else
15651 printf_filtered (_("No previous tracepoint\n"));
15652 return NULL;
15653 }
15654
15655 ALL_TRACEPOINTS (t)
15656 if (t->number == tpnum)
15657 {
15658 return (struct tracepoint *) t;
15659 }
15660
15661 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15662 return NULL;
15663 }
15664
15665 void
15666 print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15667 {
15668 if (b->thread != -1)
15669 fprintf_unfiltered (fp, " thread %d", b->thread);
15670
15671 if (b->task != 0)
15672 fprintf_unfiltered (fp, " task %d", b->task);
15673
15674 fprintf_unfiltered (fp, "\n");
15675 }
15676
15677 /* Save information on user settable breakpoints (watchpoints, etc) to
15678 a new script file named FILENAME. If FILTER is non-NULL, call it
15679 on each breakpoint and only include the ones for which it returns
15680 non-zero. */
15681
15682 static void
15683 save_breakpoints (char *filename, int from_tty,
15684 int (*filter) (const struct breakpoint *))
15685 {
15686 struct breakpoint *tp;
15687 int any = 0;
15688 struct cleanup *cleanup;
15689 struct ui_file *fp;
15690 int extra_trace_bits = 0;
15691
15692 if (filename == 0 || *filename == 0)
15693 error (_("Argument required (file name in which to save)"));
15694
15695 /* See if we have anything to save. */
15696 ALL_BREAKPOINTS (tp)
15697 {
15698 /* Skip internal and momentary breakpoints. */
15699 if (!user_breakpoint_p (tp))
15700 continue;
15701
15702 /* If we have a filter, only save the breakpoints it accepts. */
15703 if (filter && !filter (tp))
15704 continue;
15705
15706 any = 1;
15707
15708 if (is_tracepoint (tp))
15709 {
15710 extra_trace_bits = 1;
15711
15712 /* We can stop searching. */
15713 break;
15714 }
15715 }
15716
15717 if (!any)
15718 {
15719 warning (_("Nothing to save."));
15720 return;
15721 }
15722
15723 filename = tilde_expand (filename);
15724 cleanup = make_cleanup (xfree, filename);
15725 fp = gdb_fopen (filename, "w");
15726 if (!fp)
15727 error (_("Unable to open file '%s' for saving (%s)"),
15728 filename, safe_strerror (errno));
15729 make_cleanup_ui_file_delete (fp);
15730
15731 if (extra_trace_bits)
15732 save_trace_state_variables (fp);
15733
15734 ALL_BREAKPOINTS (tp)
15735 {
15736 /* Skip internal and momentary breakpoints. */
15737 if (!user_breakpoint_p (tp))
15738 continue;
15739
15740 /* If we have a filter, only save the breakpoints it accepts. */
15741 if (filter && !filter (tp))
15742 continue;
15743
15744 tp->ops->print_recreate (tp, fp);
15745
15746 /* Note, we can't rely on tp->number for anything, as we can't
15747 assume the recreated breakpoint numbers will match. Use $bpnum
15748 instead. */
15749
15750 if (tp->cond_string)
15751 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
15752
15753 if (tp->ignore_count)
15754 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
15755
15756 if (tp->type != bp_dprintf && tp->commands)
15757 {
15758 fprintf_unfiltered (fp, " commands\n");
15759
15760 ui_out_redirect (current_uiout, fp);
15761 TRY
15762 {
15763 print_command_lines (current_uiout, tp->commands->commands, 2);
15764 }
15765 CATCH (ex, RETURN_MASK_ALL)
15766 {
15767 ui_out_redirect (current_uiout, NULL);
15768 throw_exception (ex);
15769 }
15770 END_CATCH
15771
15772 ui_out_redirect (current_uiout, NULL);
15773 fprintf_unfiltered (fp, " end\n");
15774 }
15775
15776 if (tp->enable_state == bp_disabled)
15777 fprintf_unfiltered (fp, "disable $bpnum\n");
15778
15779 /* If this is a multi-location breakpoint, check if the locations
15780 should be individually disabled. Watchpoint locations are
15781 special, and not user visible. */
15782 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15783 {
15784 struct bp_location *loc;
15785 int n = 1;
15786
15787 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15788 if (!loc->enabled)
15789 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
15790 }
15791 }
15792
15793 if (extra_trace_bits && *default_collect)
15794 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
15795
15796 if (from_tty)
15797 printf_filtered (_("Saved to file '%s'.\n"), filename);
15798 do_cleanups (cleanup);
15799 }
15800
15801 /* The `save breakpoints' command. */
15802
15803 static void
15804 save_breakpoints_command (char *args, int from_tty)
15805 {
15806 save_breakpoints (args, from_tty, NULL);
15807 }
15808
15809 /* The `save tracepoints' command. */
15810
15811 static void
15812 save_tracepoints_command (char *args, int from_tty)
15813 {
15814 save_breakpoints (args, from_tty, is_tracepoint);
15815 }
15816
15817 /* Create a vector of all tracepoints. */
15818
15819 VEC(breakpoint_p) *
15820 all_tracepoints (void)
15821 {
15822 VEC(breakpoint_p) *tp_vec = 0;
15823 struct breakpoint *tp;
15824
15825 ALL_TRACEPOINTS (tp)
15826 {
15827 VEC_safe_push (breakpoint_p, tp_vec, tp);
15828 }
15829
15830 return tp_vec;
15831 }
15832
15833 \f
15834 /* This help string is used to consolidate all the help string for specifying
15835 locations used by several commands. */
15836
15837 #define LOCATION_HELP_STRING \
15838 "Linespecs are colon-separated lists of location parameters, such as\n\
15839 source filename, function name, label name, and line number.\n\
15840 Example: To specify the start of a label named \"the_top\" in the\n\
15841 function \"fact\" in the file \"factorial.c\", use\n\
15842 \"factorial.c:fact:the_top\".\n\
15843 \n\
15844 Address locations begin with \"*\" and specify an exact address in the\n\
15845 program. Example: To specify the fourth byte past the start function\n\
15846 \"main\", use \"*main + 4\".\n\
15847 \n\
15848 Explicit locations are similar to linespecs but use an option/argument\n\
15849 syntax to specify location parameters.\n\
15850 Example: To specify the start of the label named \"the_top\" in the\n\
15851 function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
15852 -function fact -label the_top\".\n"
15853
15854 /* This help string is used for the break, hbreak, tbreak and thbreak
15855 commands. It is defined as a macro to prevent duplication.
15856 COMMAND should be a string constant containing the name of the
15857 command. */
15858
15859 #define BREAK_ARGS_HELP(command) \
15860 command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15861 PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15862 probe point. Accepted values are `-probe' (for a generic, automatically\n\
15863 guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15864 `-probe-dtrace' (for a DTrace probe).\n\
15865 LOCATION may be a linespec, address, or explicit location as described\n\
15866 below.\n\
15867 \n\
15868 With no LOCATION, uses current execution address of the selected\n\
15869 stack frame. This is useful for breaking on return to a stack frame.\n\
15870 \n\
15871 THREADNUM is the number from \"info threads\".\n\
15872 CONDITION is a boolean expression.\n\
15873 \n" LOCATION_HELP_STRING "\n\
15874 Multiple breakpoints at one place are permitted, and useful if their\n\
15875 conditions are different.\n\
15876 \n\
15877 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15878
15879 /* List of subcommands for "catch". */
15880 static struct cmd_list_element *catch_cmdlist;
15881
15882 /* List of subcommands for "tcatch". */
15883 static struct cmd_list_element *tcatch_cmdlist;
15884
15885 void
15886 add_catch_command (char *name, char *docstring,
15887 cmd_sfunc_ftype *sfunc,
15888 completer_ftype *completer,
15889 void *user_data_catch,
15890 void *user_data_tcatch)
15891 {
15892 struct cmd_list_element *command;
15893
15894 command = add_cmd (name, class_breakpoint, NULL, docstring,
15895 &catch_cmdlist);
15896 set_cmd_sfunc (command, sfunc);
15897 set_cmd_context (command, user_data_catch);
15898 set_cmd_completer (command, completer);
15899
15900 command = add_cmd (name, class_breakpoint, NULL, docstring,
15901 &tcatch_cmdlist);
15902 set_cmd_sfunc (command, sfunc);
15903 set_cmd_context (command, user_data_tcatch);
15904 set_cmd_completer (command, completer);
15905 }
15906
15907 static void
15908 save_command (char *arg, int from_tty)
15909 {
15910 printf_unfiltered (_("\"save\" must be followed by "
15911 "the name of a save subcommand.\n"));
15912 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
15913 }
15914
15915 struct breakpoint *
15916 iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15917 void *data)
15918 {
15919 struct breakpoint *b, *b_tmp;
15920
15921 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15922 {
15923 if ((*callback) (b, data))
15924 return b;
15925 }
15926
15927 return NULL;
15928 }
15929
15930 /* Zero if any of the breakpoint's locations could be a location where
15931 functions have been inlined, nonzero otherwise. */
15932
15933 static int
15934 is_non_inline_function (struct breakpoint *b)
15935 {
15936 /* The shared library event breakpoint is set on the address of a
15937 non-inline function. */
15938 if (b->type == bp_shlib_event)
15939 return 1;
15940
15941 return 0;
15942 }
15943
15944 /* Nonzero if the specified PC cannot be a location where functions
15945 have been inlined. */
15946
15947 int
15948 pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
15949 const struct target_waitstatus *ws)
15950 {
15951 struct breakpoint *b;
15952 struct bp_location *bl;
15953
15954 ALL_BREAKPOINTS (b)
15955 {
15956 if (!is_non_inline_function (b))
15957 continue;
15958
15959 for (bl = b->loc; bl != NULL; bl = bl->next)
15960 {
15961 if (!bl->shlib_disabled
15962 && bpstat_check_location (bl, aspace, pc, ws))
15963 return 1;
15964 }
15965 }
15966
15967 return 0;
15968 }
15969
15970 /* Remove any references to OBJFILE which is going to be freed. */
15971
15972 void
15973 breakpoint_free_objfile (struct objfile *objfile)
15974 {
15975 struct bp_location **locp, *loc;
15976
15977 ALL_BP_LOCATIONS (loc, locp)
15978 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
15979 loc->symtab = NULL;
15980 }
15981
15982 void
15983 initialize_breakpoint_ops (void)
15984 {
15985 static int initialized = 0;
15986
15987 struct breakpoint_ops *ops;
15988
15989 if (initialized)
15990 return;
15991 initialized = 1;
15992
15993 /* The breakpoint_ops structure to be inherit by all kinds of
15994 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15995 internal and momentary breakpoints, etc.). */
15996 ops = &bkpt_base_breakpoint_ops;
15997 *ops = base_breakpoint_ops;
15998 ops->re_set = bkpt_re_set;
15999 ops->insert_location = bkpt_insert_location;
16000 ops->remove_location = bkpt_remove_location;
16001 ops->breakpoint_hit = bkpt_breakpoint_hit;
16002 ops->create_sals_from_location = bkpt_create_sals_from_location;
16003 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
16004 ops->decode_location = bkpt_decode_location;
16005
16006 /* The breakpoint_ops structure to be used in regular breakpoints. */
16007 ops = &bkpt_breakpoint_ops;
16008 *ops = bkpt_base_breakpoint_ops;
16009 ops->re_set = bkpt_re_set;
16010 ops->resources_needed = bkpt_resources_needed;
16011 ops->print_it = bkpt_print_it;
16012 ops->print_mention = bkpt_print_mention;
16013 ops->print_recreate = bkpt_print_recreate;
16014
16015 /* Ranged breakpoints. */
16016 ops = &ranged_breakpoint_ops;
16017 *ops = bkpt_breakpoint_ops;
16018 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
16019 ops->resources_needed = resources_needed_ranged_breakpoint;
16020 ops->print_it = print_it_ranged_breakpoint;
16021 ops->print_one = print_one_ranged_breakpoint;
16022 ops->print_one_detail = print_one_detail_ranged_breakpoint;
16023 ops->print_mention = print_mention_ranged_breakpoint;
16024 ops->print_recreate = print_recreate_ranged_breakpoint;
16025
16026 /* Internal breakpoints. */
16027 ops = &internal_breakpoint_ops;
16028 *ops = bkpt_base_breakpoint_ops;
16029 ops->re_set = internal_bkpt_re_set;
16030 ops->check_status = internal_bkpt_check_status;
16031 ops->print_it = internal_bkpt_print_it;
16032 ops->print_mention = internal_bkpt_print_mention;
16033
16034 /* Momentary breakpoints. */
16035 ops = &momentary_breakpoint_ops;
16036 *ops = bkpt_base_breakpoint_ops;
16037 ops->re_set = momentary_bkpt_re_set;
16038 ops->check_status = momentary_bkpt_check_status;
16039 ops->print_it = momentary_bkpt_print_it;
16040 ops->print_mention = momentary_bkpt_print_mention;
16041
16042 /* Momentary breakpoints for bp_longjmp and bp_exception. */
16043 ops = &longjmp_breakpoint_ops;
16044 *ops = momentary_breakpoint_ops;
16045 ops->dtor = longjmp_bkpt_dtor;
16046
16047 /* Probe breakpoints. */
16048 ops = &bkpt_probe_breakpoint_ops;
16049 *ops = bkpt_breakpoint_ops;
16050 ops->insert_location = bkpt_probe_insert_location;
16051 ops->remove_location = bkpt_probe_remove_location;
16052 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
16053 ops->decode_location = bkpt_probe_decode_location;
16054
16055 /* Watchpoints. */
16056 ops = &watchpoint_breakpoint_ops;
16057 *ops = base_breakpoint_ops;
16058 ops->dtor = dtor_watchpoint;
16059 ops->re_set = re_set_watchpoint;
16060 ops->insert_location = insert_watchpoint;
16061 ops->remove_location = remove_watchpoint;
16062 ops->breakpoint_hit = breakpoint_hit_watchpoint;
16063 ops->check_status = check_status_watchpoint;
16064 ops->resources_needed = resources_needed_watchpoint;
16065 ops->works_in_software_mode = works_in_software_mode_watchpoint;
16066 ops->print_it = print_it_watchpoint;
16067 ops->print_mention = print_mention_watchpoint;
16068 ops->print_recreate = print_recreate_watchpoint;
16069 ops->explains_signal = explains_signal_watchpoint;
16070
16071 /* Masked watchpoints. */
16072 ops = &masked_watchpoint_breakpoint_ops;
16073 *ops = watchpoint_breakpoint_ops;
16074 ops->insert_location = insert_masked_watchpoint;
16075 ops->remove_location = remove_masked_watchpoint;
16076 ops->resources_needed = resources_needed_masked_watchpoint;
16077 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
16078 ops->print_it = print_it_masked_watchpoint;
16079 ops->print_one_detail = print_one_detail_masked_watchpoint;
16080 ops->print_mention = print_mention_masked_watchpoint;
16081 ops->print_recreate = print_recreate_masked_watchpoint;
16082
16083 /* Tracepoints. */
16084 ops = &tracepoint_breakpoint_ops;
16085 *ops = base_breakpoint_ops;
16086 ops->re_set = tracepoint_re_set;
16087 ops->breakpoint_hit = tracepoint_breakpoint_hit;
16088 ops->print_one_detail = tracepoint_print_one_detail;
16089 ops->print_mention = tracepoint_print_mention;
16090 ops->print_recreate = tracepoint_print_recreate;
16091 ops->create_sals_from_location = tracepoint_create_sals_from_location;
16092 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
16093 ops->decode_location = tracepoint_decode_location;
16094
16095 /* Probe tracepoints. */
16096 ops = &tracepoint_probe_breakpoint_ops;
16097 *ops = tracepoint_breakpoint_ops;
16098 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
16099 ops->decode_location = tracepoint_probe_decode_location;
16100
16101 /* Static tracepoints with marker (`-m'). */
16102 ops = &strace_marker_breakpoint_ops;
16103 *ops = tracepoint_breakpoint_ops;
16104 ops->create_sals_from_location = strace_marker_create_sals_from_location;
16105 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
16106 ops->decode_location = strace_marker_decode_location;
16107
16108 /* Fork catchpoints. */
16109 ops = &catch_fork_breakpoint_ops;
16110 *ops = base_breakpoint_ops;
16111 ops->insert_location = insert_catch_fork;
16112 ops->remove_location = remove_catch_fork;
16113 ops->breakpoint_hit = breakpoint_hit_catch_fork;
16114 ops->print_it = print_it_catch_fork;
16115 ops->print_one = print_one_catch_fork;
16116 ops->print_mention = print_mention_catch_fork;
16117 ops->print_recreate = print_recreate_catch_fork;
16118
16119 /* Vfork catchpoints. */
16120 ops = &catch_vfork_breakpoint_ops;
16121 *ops = base_breakpoint_ops;
16122 ops->insert_location = insert_catch_vfork;
16123 ops->remove_location = remove_catch_vfork;
16124 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
16125 ops->print_it = print_it_catch_vfork;
16126 ops->print_one = print_one_catch_vfork;
16127 ops->print_mention = print_mention_catch_vfork;
16128 ops->print_recreate = print_recreate_catch_vfork;
16129
16130 /* Exec catchpoints. */
16131 ops = &catch_exec_breakpoint_ops;
16132 *ops = base_breakpoint_ops;
16133 ops->dtor = dtor_catch_exec;
16134 ops->insert_location = insert_catch_exec;
16135 ops->remove_location = remove_catch_exec;
16136 ops->breakpoint_hit = breakpoint_hit_catch_exec;
16137 ops->print_it = print_it_catch_exec;
16138 ops->print_one = print_one_catch_exec;
16139 ops->print_mention = print_mention_catch_exec;
16140 ops->print_recreate = print_recreate_catch_exec;
16141
16142 /* Solib-related catchpoints. */
16143 ops = &catch_solib_breakpoint_ops;
16144 *ops = base_breakpoint_ops;
16145 ops->dtor = dtor_catch_solib;
16146 ops->insert_location = insert_catch_solib;
16147 ops->remove_location = remove_catch_solib;
16148 ops->breakpoint_hit = breakpoint_hit_catch_solib;
16149 ops->check_status = check_status_catch_solib;
16150 ops->print_it = print_it_catch_solib;
16151 ops->print_one = print_one_catch_solib;
16152 ops->print_mention = print_mention_catch_solib;
16153 ops->print_recreate = print_recreate_catch_solib;
16154
16155 ops = &dprintf_breakpoint_ops;
16156 *ops = bkpt_base_breakpoint_ops;
16157 ops->re_set = dprintf_re_set;
16158 ops->resources_needed = bkpt_resources_needed;
16159 ops->print_it = bkpt_print_it;
16160 ops->print_mention = bkpt_print_mention;
16161 ops->print_recreate = dprintf_print_recreate;
16162 ops->after_condition_true = dprintf_after_condition_true;
16163 ops->breakpoint_hit = dprintf_breakpoint_hit;
16164 }
16165
16166 /* Chain containing all defined "enable breakpoint" subcommands. */
16167
16168 static struct cmd_list_element *enablebreaklist = NULL;
16169
16170 void
16171 _initialize_breakpoint (void)
16172 {
16173 struct cmd_list_element *c;
16174
16175 initialize_breakpoint_ops ();
16176
16177 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
16178 observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
16179 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
16180
16181 breakpoint_objfile_key
16182 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
16183
16184 breakpoint_chain = 0;
16185 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
16186 before a breakpoint is set. */
16187 breakpoint_count = 0;
16188
16189 tracepoint_count = 0;
16190
16191 add_com ("ignore", class_breakpoint, ignore_command, _("\
16192 Set ignore-count of breakpoint number N to COUNT.\n\
16193 Usage is `ignore N COUNT'."));
16194
16195 add_com ("commands", class_breakpoint, commands_command, _("\
16196 Set commands to be executed when a breakpoint is hit.\n\
16197 Give breakpoint number as argument after \"commands\".\n\
16198 With no argument, the targeted breakpoint is the last one set.\n\
16199 The commands themselves follow starting on the next line.\n\
16200 Type a line containing \"end\" to indicate the end of them.\n\
16201 Give \"silent\" as the first line to make the breakpoint silent;\n\
16202 then no output is printed when it is hit, except what the commands print."));
16203
16204 c = add_com ("condition", class_breakpoint, condition_command, _("\
16205 Specify breakpoint number N to break only if COND is true.\n\
16206 Usage is `condition N COND', where N is an integer and COND is an\n\
16207 expression to be evaluated whenever breakpoint N is reached."));
16208 set_cmd_completer (c, condition_completer);
16209
16210 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
16211 Set a temporary breakpoint.\n\
16212 Like \"break\" except the breakpoint is only temporary,\n\
16213 so it will be deleted when hit. Equivalent to \"break\" followed\n\
16214 by using \"enable delete\" on the breakpoint number.\n\
16215 \n"
16216 BREAK_ARGS_HELP ("tbreak")));
16217 set_cmd_completer (c, location_completer);
16218
16219 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
16220 Set a hardware assisted breakpoint.\n\
16221 Like \"break\" except the breakpoint requires hardware support,\n\
16222 some target hardware may not have this support.\n\
16223 \n"
16224 BREAK_ARGS_HELP ("hbreak")));
16225 set_cmd_completer (c, location_completer);
16226
16227 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
16228 Set a temporary hardware assisted breakpoint.\n\
16229 Like \"hbreak\" except the breakpoint is only temporary,\n\
16230 so it will be deleted when hit.\n\
16231 \n"
16232 BREAK_ARGS_HELP ("thbreak")));
16233 set_cmd_completer (c, location_completer);
16234
16235 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
16236 Enable some breakpoints.\n\
16237 Give breakpoint numbers (separated by spaces) as arguments.\n\
16238 With no subcommand, breakpoints are enabled until you command otherwise.\n\
16239 This is used to cancel the effect of the \"disable\" command.\n\
16240 With a subcommand you can enable temporarily."),
16241 &enablelist, "enable ", 1, &cmdlist);
16242
16243 add_com_alias ("en", "enable", class_breakpoint, 1);
16244
16245 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
16246 Enable some breakpoints.\n\
16247 Give breakpoint numbers (separated by spaces) as arguments.\n\
16248 This is used to cancel the effect of the \"disable\" command.\n\
16249 May be abbreviated to simply \"enable\".\n"),
16250 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
16251
16252 add_cmd ("once", no_class, enable_once_command, _("\
16253 Enable breakpoints for one hit. Give breakpoint numbers.\n\
16254 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
16255 &enablebreaklist);
16256
16257 add_cmd ("delete", no_class, enable_delete_command, _("\
16258 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16259 If a breakpoint is hit while enabled in this fashion, it is deleted."),
16260 &enablebreaklist);
16261
16262 add_cmd ("count", no_class, enable_count_command, _("\
16263 Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16264 If a breakpoint is hit while enabled in this fashion,\n\
16265 the count is decremented; when it reaches zero, the breakpoint is disabled."),
16266 &enablebreaklist);
16267
16268 add_cmd ("delete", no_class, enable_delete_command, _("\
16269 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16270 If a breakpoint is hit while enabled in this fashion, it is deleted."),
16271 &enablelist);
16272
16273 add_cmd ("once", no_class, enable_once_command, _("\
16274 Enable breakpoints for one hit. Give breakpoint numbers.\n\
16275 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
16276 &enablelist);
16277
16278 add_cmd ("count", no_class, enable_count_command, _("\
16279 Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16280 If a breakpoint is hit while enabled in this fashion,\n\
16281 the count is decremented; when it reaches zero, the breakpoint is disabled."),
16282 &enablelist);
16283
16284 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
16285 Disable some breakpoints.\n\
16286 Arguments are breakpoint numbers with spaces in between.\n\
16287 To disable all breakpoints, give no argument.\n\
16288 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
16289 &disablelist, "disable ", 1, &cmdlist);
16290 add_com_alias ("dis", "disable", class_breakpoint, 1);
16291 add_com_alias ("disa", "disable", class_breakpoint, 1);
16292
16293 add_cmd ("breakpoints", class_alias, disable_command, _("\
16294 Disable some breakpoints.\n\
16295 Arguments are breakpoint numbers with spaces in between.\n\
16296 To disable all breakpoints, give no argument.\n\
16297 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
16298 This command may be abbreviated \"disable\"."),
16299 &disablelist);
16300
16301 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
16302 Delete some breakpoints or auto-display expressions.\n\
16303 Arguments are breakpoint numbers with spaces in between.\n\
16304 To delete all breakpoints, give no argument.\n\
16305 \n\
16306 Also a prefix command for deletion of other GDB objects.\n\
16307 The \"unset\" command is also an alias for \"delete\"."),
16308 &deletelist, "delete ", 1, &cmdlist);
16309 add_com_alias ("d", "delete", class_breakpoint, 1);
16310 add_com_alias ("del", "delete", class_breakpoint, 1);
16311
16312 add_cmd ("breakpoints", class_alias, delete_command, _("\
16313 Delete some breakpoints or auto-display expressions.\n\
16314 Arguments are breakpoint numbers with spaces in between.\n\
16315 To delete all breakpoints, give no argument.\n\
16316 This command may be abbreviated \"delete\"."),
16317 &deletelist);
16318
16319 add_com ("clear", class_breakpoint, clear_command, _("\
16320 Clear breakpoint at specified location.\n\
16321 Argument may be a linespec, explicit, or address location as described below.\n\
16322 \n\
16323 With no argument, clears all breakpoints in the line that the selected frame\n\
16324 is executing in.\n"
16325 "\n" LOCATION_HELP_STRING "\n\
16326 See also the \"delete\" command which clears breakpoints by number."));
16327 add_com_alias ("cl", "clear", class_breakpoint, 1);
16328
16329 c = add_com ("break", class_breakpoint, break_command, _("\
16330 Set breakpoint at specified location.\n"
16331 BREAK_ARGS_HELP ("break")));
16332 set_cmd_completer (c, location_completer);
16333
16334 add_com_alias ("b", "break", class_run, 1);
16335 add_com_alias ("br", "break", class_run, 1);
16336 add_com_alias ("bre", "break", class_run, 1);
16337 add_com_alias ("brea", "break", class_run, 1);
16338
16339 if (dbx_commands)
16340 {
16341 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
16342 Break in function/address or break at a line in the current file."),
16343 &stoplist, "stop ", 1, &cmdlist);
16344 add_cmd ("in", class_breakpoint, stopin_command,
16345 _("Break in function or address."), &stoplist);
16346 add_cmd ("at", class_breakpoint, stopat_command,
16347 _("Break at a line in the current file."), &stoplist);
16348 add_com ("status", class_info, breakpoints_info, _("\
16349 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
16350 The \"Type\" column indicates one of:\n\
16351 \tbreakpoint - normal breakpoint\n\
16352 \twatchpoint - watchpoint\n\
16353 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16354 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16355 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16356 address and file/line number respectively.\n\
16357 \n\
16358 Convenience variable \"$_\" and default examine address for \"x\"\n\
16359 are set to the address of the last breakpoint listed unless the command\n\
16360 is prefixed with \"server \".\n\n\
16361 Convenience variable \"$bpnum\" contains the number of the last\n\
16362 breakpoint set."));
16363 }
16364
16365 add_info ("breakpoints", breakpoints_info, _("\
16366 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
16367 The \"Type\" column indicates one of:\n\
16368 \tbreakpoint - normal breakpoint\n\
16369 \twatchpoint - watchpoint\n\
16370 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16371 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16372 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16373 address and file/line number respectively.\n\
16374 \n\
16375 Convenience variable \"$_\" and default examine address for \"x\"\n\
16376 are set to the address of the last breakpoint listed unless the command\n\
16377 is prefixed with \"server \".\n\n\
16378 Convenience variable \"$bpnum\" contains the number of the last\n\
16379 breakpoint set."));
16380
16381 add_info_alias ("b", "breakpoints", 1);
16382
16383 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
16384 Status of all breakpoints, or breakpoint number NUMBER.\n\
16385 The \"Type\" column indicates one of:\n\
16386 \tbreakpoint - normal breakpoint\n\
16387 \twatchpoint - watchpoint\n\
16388 \tlongjmp - internal breakpoint used to step through longjmp()\n\
16389 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
16390 \tuntil - internal breakpoint used by the \"until\" command\n\
16391 \tfinish - internal breakpoint used by the \"finish\" command\n\
16392 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16393 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16394 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16395 address and file/line number respectively.\n\
16396 \n\
16397 Convenience variable \"$_\" and default examine address for \"x\"\n\
16398 are set to the address of the last breakpoint listed unless the command\n\
16399 is prefixed with \"server \".\n\n\
16400 Convenience variable \"$bpnum\" contains the number of the last\n\
16401 breakpoint set."),
16402 &maintenanceinfolist);
16403
16404 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
16405 Set catchpoints to catch events."),
16406 &catch_cmdlist, "catch ",
16407 0/*allow-unknown*/, &cmdlist);
16408
16409 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
16410 Set temporary catchpoints to catch events."),
16411 &tcatch_cmdlist, "tcatch ",
16412 0/*allow-unknown*/, &cmdlist);
16413
16414 add_catch_command ("fork", _("Catch calls to fork."),
16415 catch_fork_command_1,
16416 NULL,
16417 (void *) (uintptr_t) catch_fork_permanent,
16418 (void *) (uintptr_t) catch_fork_temporary);
16419 add_catch_command ("vfork", _("Catch calls to vfork."),
16420 catch_fork_command_1,
16421 NULL,
16422 (void *) (uintptr_t) catch_vfork_permanent,
16423 (void *) (uintptr_t) catch_vfork_temporary);
16424 add_catch_command ("exec", _("Catch calls to exec."),
16425 catch_exec_command_1,
16426 NULL,
16427 CATCH_PERMANENT,
16428 CATCH_TEMPORARY);
16429 add_catch_command ("load", _("Catch loads of shared libraries.\n\
16430 Usage: catch load [REGEX]\n\
16431 If REGEX is given, only stop for libraries matching the regular expression."),
16432 catch_load_command_1,
16433 NULL,
16434 CATCH_PERMANENT,
16435 CATCH_TEMPORARY);
16436 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
16437 Usage: catch unload [REGEX]\n\
16438 If REGEX is given, only stop for libraries matching the regular expression."),
16439 catch_unload_command_1,
16440 NULL,
16441 CATCH_PERMANENT,
16442 CATCH_TEMPORARY);
16443
16444 c = add_com ("watch", class_breakpoint, watch_command, _("\
16445 Set a watchpoint for an expression.\n\
16446 Usage: watch [-l|-location] EXPRESSION\n\
16447 A watchpoint stops execution of your program whenever the value of\n\
16448 an expression changes.\n\
16449 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16450 the memory to which it refers."));
16451 set_cmd_completer (c, expression_completer);
16452
16453 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
16454 Set a read watchpoint for an expression.\n\
16455 Usage: rwatch [-l|-location] EXPRESSION\n\
16456 A watchpoint stops execution of your program whenever the value of\n\
16457 an expression is read.\n\
16458 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16459 the memory to which it refers."));
16460 set_cmd_completer (c, expression_completer);
16461
16462 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
16463 Set a watchpoint for an expression.\n\
16464 Usage: awatch [-l|-location] EXPRESSION\n\
16465 A watchpoint stops execution of your program whenever the value of\n\
16466 an expression is either read or written.\n\
16467 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16468 the memory to which it refers."));
16469 set_cmd_completer (c, expression_completer);
16470
16471 add_info ("watchpoints", watchpoints_info, _("\
16472 Status of specified watchpoints (all watchpoints if no argument)."));
16473
16474 /* XXX: cagney/2005-02-23: This should be a boolean, and should
16475 respond to changes - contrary to the description. */
16476 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
16477 &can_use_hw_watchpoints, _("\
16478 Set debugger's willingness to use watchpoint hardware."), _("\
16479 Show debugger's willingness to use watchpoint hardware."), _("\
16480 If zero, gdb will not use hardware for new watchpoints, even if\n\
16481 such is available. (However, any hardware watchpoints that were\n\
16482 created before setting this to nonzero, will continue to use watchpoint\n\
16483 hardware.)"),
16484 NULL,
16485 show_can_use_hw_watchpoints,
16486 &setlist, &showlist);
16487
16488 can_use_hw_watchpoints = 1;
16489
16490 /* Tracepoint manipulation commands. */
16491
16492 c = add_com ("trace", class_breakpoint, trace_command, _("\
16493 Set a tracepoint at specified location.\n\
16494 \n"
16495 BREAK_ARGS_HELP ("trace") "\n\
16496 Do \"help tracepoints\" for info on other tracepoint commands."));
16497 set_cmd_completer (c, location_completer);
16498
16499 add_com_alias ("tp", "trace", class_alias, 0);
16500 add_com_alias ("tr", "trace", class_alias, 1);
16501 add_com_alias ("tra", "trace", class_alias, 1);
16502 add_com_alias ("trac", "trace", class_alias, 1);
16503
16504 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
16505 Set a fast tracepoint at specified location.\n\
16506 \n"
16507 BREAK_ARGS_HELP ("ftrace") "\n\
16508 Do \"help tracepoints\" for info on other tracepoint commands."));
16509 set_cmd_completer (c, location_completer);
16510
16511 c = add_com ("strace", class_breakpoint, strace_command, _("\
16512 Set a static tracepoint at location or marker.\n\
16513 \n\
16514 strace [LOCATION] [if CONDITION]\n\
16515 LOCATION may be a linespec, explicit, or address location (described below) \n\
16516 or -m MARKER_ID.\n\n\
16517 If a marker id is specified, probe the marker with that name. With\n\
16518 no LOCATION, uses current execution address of the selected stack frame.\n\
16519 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16520 This collects arbitrary user data passed in the probe point call to the\n\
16521 tracing library. You can inspect it when analyzing the trace buffer,\n\
16522 by printing the $_sdata variable like any other convenience variable.\n\
16523 \n\
16524 CONDITION is a boolean expression.\n\
16525 \n" LOCATION_HELP_STRING "\n\
16526 Multiple tracepoints at one place are permitted, and useful if their\n\
16527 conditions are different.\n\
16528 \n\
16529 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16530 Do \"help tracepoints\" for info on other tracepoint commands."));
16531 set_cmd_completer (c, location_completer);
16532
16533 add_info ("tracepoints", tracepoints_info, _("\
16534 Status of specified tracepoints (all tracepoints if no argument).\n\
16535 Convenience variable \"$tpnum\" contains the number of the\n\
16536 last tracepoint set."));
16537
16538 add_info_alias ("tp", "tracepoints", 1);
16539
16540 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16541 Delete specified tracepoints.\n\
16542 Arguments are tracepoint numbers, separated by spaces.\n\
16543 No argument means delete all tracepoints."),
16544 &deletelist);
16545 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
16546
16547 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16548 Disable specified tracepoints.\n\
16549 Arguments are tracepoint numbers, separated by spaces.\n\
16550 No argument means disable all tracepoints."),
16551 &disablelist);
16552 deprecate_cmd (c, "disable");
16553
16554 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16555 Enable specified tracepoints.\n\
16556 Arguments are tracepoint numbers, separated by spaces.\n\
16557 No argument means enable all tracepoints."),
16558 &enablelist);
16559 deprecate_cmd (c, "enable");
16560
16561 add_com ("passcount", class_trace, trace_pass_command, _("\
16562 Set the passcount for a tracepoint.\n\
16563 The trace will end when the tracepoint has been passed 'count' times.\n\
16564 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16565 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16566
16567 add_prefix_cmd ("save", class_breakpoint, save_command,
16568 _("Save breakpoint definitions as a script."),
16569 &save_cmdlist, "save ",
16570 0/*allow-unknown*/, &cmdlist);
16571
16572 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16573 Save current breakpoint definitions as a script.\n\
16574 This includes all types of breakpoints (breakpoints, watchpoints,\n\
16575 catchpoints, tracepoints). Use the 'source' command in another debug\n\
16576 session to restore them."),
16577 &save_cmdlist);
16578 set_cmd_completer (c, filename_completer);
16579
16580 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
16581 Save current tracepoint definitions as a script.\n\
16582 Use the 'source' command in another debug session to restore them."),
16583 &save_cmdlist);
16584 set_cmd_completer (c, filename_completer);
16585
16586 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16587 deprecate_cmd (c, "save tracepoints");
16588
16589 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
16590 Breakpoint specific settings\n\
16591 Configure various breakpoint-specific variables such as\n\
16592 pending breakpoint behavior"),
16593 &breakpoint_set_cmdlist, "set breakpoint ",
16594 0/*allow-unknown*/, &setlist);
16595 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
16596 Breakpoint specific settings\n\
16597 Configure various breakpoint-specific variables such as\n\
16598 pending breakpoint behavior"),
16599 &breakpoint_show_cmdlist, "show breakpoint ",
16600 0/*allow-unknown*/, &showlist);
16601
16602 add_setshow_auto_boolean_cmd ("pending", no_class,
16603 &pending_break_support, _("\
16604 Set debugger's behavior regarding pending breakpoints."), _("\
16605 Show debugger's behavior regarding pending breakpoints."), _("\
16606 If on, an unrecognized breakpoint location will cause gdb to create a\n\
16607 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16608 an error. If auto, an unrecognized breakpoint location results in a\n\
16609 user-query to see if a pending breakpoint should be created."),
16610 NULL,
16611 show_pending_break_support,
16612 &breakpoint_set_cmdlist,
16613 &breakpoint_show_cmdlist);
16614
16615 pending_break_support = AUTO_BOOLEAN_AUTO;
16616
16617 add_setshow_boolean_cmd ("auto-hw", no_class,
16618 &automatic_hardware_breakpoints, _("\
16619 Set automatic usage of hardware breakpoints."), _("\
16620 Show automatic usage of hardware breakpoints."), _("\
16621 If set, the debugger will automatically use hardware breakpoints for\n\
16622 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16623 a warning will be emitted for such breakpoints."),
16624 NULL,
16625 show_automatic_hardware_breakpoints,
16626 &breakpoint_set_cmdlist,
16627 &breakpoint_show_cmdlist);
16628
16629 add_setshow_boolean_cmd ("always-inserted", class_support,
16630 &always_inserted_mode, _("\
16631 Set mode for inserting breakpoints."), _("\
16632 Show mode for inserting breakpoints."), _("\
16633 When this mode is on, breakpoints are inserted immediately as soon as\n\
16634 they're created, kept inserted even when execution stops, and removed\n\
16635 only when the user deletes them. When this mode is off (the default),\n\
16636 breakpoints are inserted only when execution continues, and removed\n\
16637 when execution stops."),
16638 NULL,
16639 &show_always_inserted_mode,
16640 &breakpoint_set_cmdlist,
16641 &breakpoint_show_cmdlist);
16642
16643 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16644 condition_evaluation_enums,
16645 &condition_evaluation_mode_1, _("\
16646 Set mode of breakpoint condition evaluation."), _("\
16647 Show mode of breakpoint condition evaluation."), _("\
16648 When this is set to \"host\", breakpoint conditions will be\n\
16649 evaluated on the host's side by GDB. When it is set to \"target\",\n\
16650 breakpoint conditions will be downloaded to the target (if the target\n\
16651 supports such feature) and conditions will be evaluated on the target's side.\n\
16652 If this is set to \"auto\" (default), this will be automatically set to\n\
16653 \"target\" if it supports condition evaluation, otherwise it will\n\
16654 be set to \"gdb\""),
16655 &set_condition_evaluation_mode,
16656 &show_condition_evaluation_mode,
16657 &breakpoint_set_cmdlist,
16658 &breakpoint_show_cmdlist);
16659
16660 add_com ("break-range", class_breakpoint, break_range_command, _("\
16661 Set a breakpoint for an address range.\n\
16662 break-range START-LOCATION, END-LOCATION\n\
16663 where START-LOCATION and END-LOCATION can be one of the following:\n\
16664 LINENUM, for that line in the current file,\n\
16665 FILE:LINENUM, for that line in that file,\n\
16666 +OFFSET, for that number of lines after the current line\n\
16667 or the start of the range\n\
16668 FUNCTION, for the first line in that function,\n\
16669 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16670 *ADDRESS, for the instruction at that address.\n\
16671 \n\
16672 The breakpoint will stop execution of the inferior whenever it executes\n\
16673 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16674 range (including START-LOCATION and END-LOCATION)."));
16675
16676 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
16677 Set a dynamic printf at specified location.\n\
16678 dprintf location,format string,arg1,arg2,...\n\
16679 location may be a linespec, explicit, or address location.\n"
16680 "\n" LOCATION_HELP_STRING));
16681 set_cmd_completer (c, location_completer);
16682
16683 add_setshow_enum_cmd ("dprintf-style", class_support,
16684 dprintf_style_enums, &dprintf_style, _("\
16685 Set the style of usage for dynamic printf."), _("\
16686 Show the style of usage for dynamic printf."), _("\
16687 This setting chooses how GDB will do a dynamic printf.\n\
16688 If the value is \"gdb\", then the printing is done by GDB to its own\n\
16689 console, as with the \"printf\" command.\n\
16690 If the value is \"call\", the print is done by calling a function in your\n\
16691 program; by default printf(), but you can choose a different function or\n\
16692 output stream by setting dprintf-function and dprintf-channel."),
16693 update_dprintf_commands, NULL,
16694 &setlist, &showlist);
16695
16696 dprintf_function = xstrdup ("printf");
16697 add_setshow_string_cmd ("dprintf-function", class_support,
16698 &dprintf_function, _("\
16699 Set the function to use for dynamic printf"), _("\
16700 Show the function to use for dynamic printf"), NULL,
16701 update_dprintf_commands, NULL,
16702 &setlist, &showlist);
16703
16704 dprintf_channel = xstrdup ("");
16705 add_setshow_string_cmd ("dprintf-channel", class_support,
16706 &dprintf_channel, _("\
16707 Set the channel to use for dynamic printf"), _("\
16708 Show the channel to use for dynamic printf"), NULL,
16709 update_dprintf_commands, NULL,
16710 &setlist, &showlist);
16711
16712 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16713 &disconnected_dprintf, _("\
16714 Set whether dprintf continues after GDB disconnects."), _("\
16715 Show whether dprintf continues after GDB disconnects."), _("\
16716 Use this to let dprintf commands continue to hit and produce output\n\
16717 even if GDB disconnects or detaches from the target."),
16718 NULL,
16719 NULL,
16720 &setlist, &showlist);
16721
16722 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16723 agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16724 (target agent only) This is useful for formatted output in user-defined commands."));
16725
16726 automatic_hardware_breakpoints = 1;
16727
16728 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
16729 observer_attach_thread_exit (remove_threaded_breakpoints);
16730 }
This page took 0.401603 seconds and 4 git commands to generate.