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