PR c++/12824:
[deliverable/binutils-gdb.git] / gdb / breakpoint.c
1 /* Everything about breakpoints, for GDB.
2
3 Copyright (C) 1986-2013 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 "gdbthread.h"
36 #include "target.h"
37 #include "language.h"
38 #include "gdb_string.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 "gdb_assert.h"
51 #include "block.h"
52 #include "solib.h"
53 #include "solist.h"
54 #include "observer.h"
55 #include "exceptions.h"
56 #include "memattr.h"
57 #include "ada-lang.h"
58 #include "top.h"
59 #include "valprint.h"
60 #include "jit.h"
61 #include "xml-syscall.h"
62 #include "parser-defs.h"
63 #include "gdb_regex.h"
64 #include "probe.h"
65 #include "cli/cli-utils.h"
66 #include "continuations.h"
67 #include "stack.h"
68 #include "skip.h"
69 #include "gdb_regex.h"
70 #include "ax-gdb.h"
71 #include "dummy-frame.h"
72
73 #include "format.h"
74
75 /* readline include files */
76 #include "readline/readline.h"
77 #include "readline/history.h"
78
79 /* readline defines this. */
80 #undef savestring
81
82 #include "mi/mi-common.h"
83 #include "python/python.h"
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 create_sals_from_address_default (char **,
116 struct linespec_result *,
117 enum bptype, char *,
118 char **);
119
120 static void create_breakpoints_sal_default (struct gdbarch *,
121 struct linespec_result *,
122 struct linespec_sals *,
123 char *, char *, enum bptype,
124 enum bpdisp, int, int,
125 int,
126 const struct breakpoint_ops *,
127 int, int, int, unsigned);
128
129 static void decode_linespec_default (struct breakpoint *, char **,
130 struct symtabs_and_lines *);
131
132 static void clear_command (char *, int);
133
134 static void catch_command (char *, int);
135
136 static int can_use_hardware_watchpoint (struct value *);
137
138 static void break_command_1 (char *, int, int);
139
140 static void mention (struct breakpoint *);
141
142 static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
143 enum bptype,
144 const struct breakpoint_ops *);
145 static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
146 const struct symtab_and_line *);
147
148 /* This function is used in gdbtk sources and thus can not be made
149 static. */
150 struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
151 struct symtab_and_line,
152 enum bptype,
153 const struct breakpoint_ops *);
154
155 static struct breakpoint *
156 momentary_breakpoint_from_master (struct breakpoint *orig,
157 enum bptype type,
158 const struct breakpoint_ops *ops);
159
160 static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
161
162 static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
163 CORE_ADDR bpaddr,
164 enum bptype bptype);
165
166 static void describe_other_breakpoints (struct gdbarch *,
167 struct program_space *, CORE_ADDR,
168 struct obj_section *, int);
169
170 static int breakpoint_address_match (struct address_space *aspace1,
171 CORE_ADDR addr1,
172 struct address_space *aspace2,
173 CORE_ADDR addr2);
174
175 static int watchpoint_locations_match (struct bp_location *loc1,
176 struct bp_location *loc2);
177
178 static int breakpoint_location_address_match (struct bp_location *bl,
179 struct address_space *aspace,
180 CORE_ADDR addr);
181
182 static void breakpoints_info (char *, int);
183
184 static void watchpoints_info (char *, int);
185
186 static int breakpoint_1 (char *, int,
187 int (*) (const struct breakpoint *));
188
189 static int breakpoint_cond_eval (void *);
190
191 static void cleanup_executing_breakpoints (void *);
192
193 static void commands_command (char *, int);
194
195 static void condition_command (char *, int);
196
197 typedef enum
198 {
199 mark_inserted,
200 mark_uninserted
201 }
202 insertion_state_t;
203
204 static int remove_breakpoint (struct bp_location *, insertion_state_t);
205 static int remove_breakpoint_1 (struct bp_location *, insertion_state_t);
206
207 static enum print_stop_action print_bp_stop_message (bpstat bs);
208
209 static int watchpoint_check (void *);
210
211 static void maintenance_info_breakpoints (char *, int);
212
213 static int hw_breakpoint_used_count (void);
214
215 static int hw_watchpoint_use_count (struct breakpoint *);
216
217 static int hw_watchpoint_used_count_others (struct breakpoint *except,
218 enum bptype type,
219 int *other_type_used);
220
221 static void hbreak_command (char *, int);
222
223 static void thbreak_command (char *, int);
224
225 static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
226 int count);
227
228 static void stop_command (char *arg, int from_tty);
229
230 static void stopin_command (char *arg, int from_tty);
231
232 static void stopat_command (char *arg, int from_tty);
233
234 static char *ep_parse_optional_if_clause (char **arg);
235
236 static void catch_exception_command_1 (enum exception_event_kind ex_event,
237 char *arg, int tempflag, int from_tty);
238
239 static void tcatch_command (char *arg, int from_tty);
240
241 static void detach_single_step_breakpoints (void);
242
243 static int single_step_breakpoint_inserted_here_p (struct address_space *,
244 CORE_ADDR pc);
245
246 static void free_bp_location (struct bp_location *loc);
247 static void incref_bp_location (struct bp_location *loc);
248 static void decref_bp_location (struct bp_location **loc);
249
250 static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
251
252 static void update_global_location_list (int);
253
254 static void update_global_location_list_nothrow (int);
255
256 static int is_hardware_watchpoint (const struct breakpoint *bpt);
257
258 static void insert_breakpoint_locations (void);
259
260 static int syscall_catchpoint_p (struct breakpoint *b);
261
262 static void tracepoints_info (char *, int);
263
264 static void delete_trace_command (char *, int);
265
266 static void enable_trace_command (char *, int);
267
268 static void disable_trace_command (char *, int);
269
270 static void trace_pass_command (char *, int);
271
272 static void set_tracepoint_count (int num);
273
274 static int is_masked_watchpoint (const struct breakpoint *b);
275
276 static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
277
278 /* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
279 otherwise. */
280
281 static int strace_marker_p (struct breakpoint *b);
282
283 /* The abstract base class all breakpoint_ops structures inherit
284 from. */
285 struct breakpoint_ops base_breakpoint_ops;
286
287 /* The breakpoint_ops structure to be inherited by all breakpoint_ops
288 that are implemented on top of software or hardware breakpoints
289 (user breakpoints, internal and momentary breakpoints, etc.). */
290 static struct breakpoint_ops bkpt_base_breakpoint_ops;
291
292 /* Internal breakpoints class type. */
293 static struct breakpoint_ops internal_breakpoint_ops;
294
295 /* Momentary breakpoints class type. */
296 static struct breakpoint_ops momentary_breakpoint_ops;
297
298 /* Momentary breakpoints for bp_longjmp and bp_exception class type. */
299 static struct breakpoint_ops longjmp_breakpoint_ops;
300
301 /* The breakpoint_ops structure to be used in regular user created
302 breakpoints. */
303 struct breakpoint_ops bkpt_breakpoint_ops;
304
305 /* Breakpoints set on probes. */
306 static struct breakpoint_ops bkpt_probe_breakpoint_ops;
307
308 /* Dynamic printf class type. */
309 static struct breakpoint_ops dprintf_breakpoint_ops;
310
311 /* The style in which to perform a dynamic printf. This is a user
312 option because different output options have different tradeoffs;
313 if GDB does the printing, there is better error handling if there
314 is a problem with any of the arguments, but using an inferior
315 function lets you have special-purpose printers and sending of
316 output to the same place as compiled-in print functions. */
317
318 static const char dprintf_style_gdb[] = "gdb";
319 static const char dprintf_style_call[] = "call";
320 static const char dprintf_style_agent[] = "agent";
321 static const char *const dprintf_style_enums[] = {
322 dprintf_style_gdb,
323 dprintf_style_call,
324 dprintf_style_agent,
325 NULL
326 };
327 static const char *dprintf_style = dprintf_style_gdb;
328
329 /* The function to use for dynamic printf if the preferred style is to
330 call into the inferior. The value is simply a string that is
331 copied into the command, so it can be anything that GDB can
332 evaluate to a callable address, not necessarily a function name. */
333
334 static char *dprintf_function = "";
335
336 /* The channel to use for dynamic printf if the preferred style is to
337 call into the inferior; if a nonempty string, it will be passed to
338 the call as the first argument, with the format string as the
339 second. As with the dprintf function, this can be anything that
340 GDB knows how to evaluate, so in addition to common choices like
341 "stderr", this could be an app-specific expression like
342 "mystreams[curlogger]". */
343
344 static char *dprintf_channel = "";
345
346 /* True if dprintf commands should continue to operate even if GDB
347 has disconnected. */
348 static int disconnected_dprintf = 1;
349
350 /* A reference-counted struct command_line. This lets multiple
351 breakpoints share a single command list. */
352 struct counted_command_line
353 {
354 /* The reference count. */
355 int refc;
356
357 /* The command list. */
358 struct command_line *commands;
359 };
360
361 struct command_line *
362 breakpoint_commands (struct breakpoint *b)
363 {
364 return b->commands ? b->commands->commands : NULL;
365 }
366
367 /* Flag indicating that a command has proceeded the inferior past the
368 current breakpoint. */
369
370 static int breakpoint_proceeded;
371
372 const char *
373 bpdisp_text (enum bpdisp disp)
374 {
375 /* NOTE: the following values are a part of MI protocol and
376 represent values of 'disp' field returned when inferior stops at
377 a breakpoint. */
378 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
379
380 return bpdisps[(int) disp];
381 }
382
383 /* Prototypes for exported functions. */
384 /* If FALSE, gdb will not use hardware support for watchpoints, even
385 if such is available. */
386 static int can_use_hw_watchpoints;
387
388 static void
389 show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
390 struct cmd_list_element *c,
391 const char *value)
392 {
393 fprintf_filtered (file,
394 _("Debugger's willingness to use "
395 "watchpoint hardware is %s.\n"),
396 value);
397 }
398
399 /* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
400 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
401 for unrecognized breakpoint locations.
402 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
403 static enum auto_boolean pending_break_support;
404 static void
405 show_pending_break_support (struct ui_file *file, int from_tty,
406 struct cmd_list_element *c,
407 const char *value)
408 {
409 fprintf_filtered (file,
410 _("Debugger's behavior regarding "
411 "pending breakpoints is %s.\n"),
412 value);
413 }
414
415 /* If 1, gdb will automatically use hardware breakpoints for breakpoints
416 set with "break" but falling in read-only memory.
417 If 0, gdb will warn about such breakpoints, but won't automatically
418 use hardware breakpoints. */
419 static int automatic_hardware_breakpoints;
420 static void
421 show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
422 struct cmd_list_element *c,
423 const char *value)
424 {
425 fprintf_filtered (file,
426 _("Automatic usage of hardware breakpoints is %s.\n"),
427 value);
428 }
429
430 /* If on, gdb will keep breakpoints inserted even as inferior is
431 stopped, and immediately insert any new breakpoints. If off, gdb
432 will insert breakpoints into inferior only when resuming it, and
433 will remove breakpoints upon stop. If auto, GDB will behave as ON
434 if in non-stop mode, and as OFF if all-stop mode.*/
435
436 static enum auto_boolean always_inserted_mode = AUTO_BOOLEAN_AUTO;
437
438 static void
439 show_always_inserted_mode (struct ui_file *file, int from_tty,
440 struct cmd_list_element *c, const char *value)
441 {
442 if (always_inserted_mode == AUTO_BOOLEAN_AUTO)
443 fprintf_filtered (file,
444 _("Always inserted breakpoint "
445 "mode is %s (currently %s).\n"),
446 value,
447 breakpoints_always_inserted_mode () ? "on" : "off");
448 else
449 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
450 value);
451 }
452
453 int
454 breakpoints_always_inserted_mode (void)
455 {
456 return (always_inserted_mode == AUTO_BOOLEAN_TRUE
457 || (always_inserted_mode == AUTO_BOOLEAN_AUTO && non_stop));
458 }
459
460 static const char condition_evaluation_both[] = "host or target";
461
462 /* Modes for breakpoint condition evaluation. */
463 static const char condition_evaluation_auto[] = "auto";
464 static const char condition_evaluation_host[] = "host";
465 static const char condition_evaluation_target[] = "target";
466 static const char *const condition_evaluation_enums[] = {
467 condition_evaluation_auto,
468 condition_evaluation_host,
469 condition_evaluation_target,
470 NULL
471 };
472
473 /* Global that holds the current mode for breakpoint condition evaluation. */
474 static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
475
476 /* Global that we use to display information to the user (gets its value from
477 condition_evaluation_mode_1. */
478 static const char *condition_evaluation_mode = condition_evaluation_auto;
479
480 /* Translate a condition evaluation mode MODE into either "host"
481 or "target". This is used mostly to translate from "auto" to the
482 real setting that is being used. It returns the translated
483 evaluation mode. */
484
485 static const char *
486 translate_condition_evaluation_mode (const char *mode)
487 {
488 if (mode == condition_evaluation_auto)
489 {
490 if (target_supports_evaluation_of_breakpoint_conditions ())
491 return condition_evaluation_target;
492 else
493 return condition_evaluation_host;
494 }
495 else
496 return mode;
497 }
498
499 /* Discovers what condition_evaluation_auto translates to. */
500
501 static const char *
502 breakpoint_condition_evaluation_mode (void)
503 {
504 return translate_condition_evaluation_mode (condition_evaluation_mode);
505 }
506
507 /* Return true if GDB should evaluate breakpoint conditions or false
508 otherwise. */
509
510 static int
511 gdb_evaluates_breakpoint_condition_p (void)
512 {
513 const char *mode = breakpoint_condition_evaluation_mode ();
514
515 return (mode == condition_evaluation_host);
516 }
517
518 void _initialize_breakpoint (void);
519
520 /* Are we executing breakpoint commands? */
521 static int executing_breakpoint_commands;
522
523 /* Are overlay event breakpoints enabled? */
524 static int overlay_events_enabled;
525
526 /* See description in breakpoint.h. */
527 int target_exact_watchpoints = 0;
528
529 /* Walk the following statement or block through all breakpoints.
530 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
531 current breakpoint. */
532
533 #define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
534
535 #define ALL_BREAKPOINTS_SAFE(B,TMP) \
536 for (B = breakpoint_chain; \
537 B ? (TMP=B->next, 1): 0; \
538 B = TMP)
539
540 /* Similar iterator for the low-level breakpoints. SAFE variant is
541 not provided so update_global_location_list must not be called
542 while executing the block of ALL_BP_LOCATIONS. */
543
544 #define ALL_BP_LOCATIONS(B,BP_TMP) \
545 for (BP_TMP = bp_location; \
546 BP_TMP < bp_location + bp_location_count && (B = *BP_TMP); \
547 BP_TMP++)
548
549 /* Iterates through locations with address ADDRESS for the currently selected
550 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
551 to where the loop should start from.
552 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
553 appropriate location to start with. */
554
555 #define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
556 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
557 BP_LOCP_TMP = BP_LOCP_START; \
558 BP_LOCP_START \
559 && (BP_LOCP_TMP < bp_location + bp_location_count \
560 && (*BP_LOCP_TMP)->address == ADDRESS); \
561 BP_LOCP_TMP++)
562
563 /* Iterator for tracepoints only. */
564
565 #define ALL_TRACEPOINTS(B) \
566 for (B = breakpoint_chain; B; B = B->next) \
567 if (is_tracepoint (B))
568
569 /* Chains of all breakpoints defined. */
570
571 struct breakpoint *breakpoint_chain;
572
573 /* Array is sorted by bp_location_compare - primarily by the ADDRESS. */
574
575 static struct bp_location **bp_location;
576
577 /* Number of elements of BP_LOCATION. */
578
579 static unsigned bp_location_count;
580
581 /* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
582 ADDRESS for the current elements of BP_LOCATION which get a valid
583 result from bp_location_has_shadow. You can use it for roughly
584 limiting the subrange of BP_LOCATION to scan for shadow bytes for
585 an address you need to read. */
586
587 static CORE_ADDR bp_location_placed_address_before_address_max;
588
589 /* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
590 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
591 BP_LOCATION which get a valid result from bp_location_has_shadow.
592 You can use it for roughly limiting the subrange of BP_LOCATION to
593 scan for shadow bytes for an address you need to read. */
594
595 static CORE_ADDR bp_location_shadow_len_after_address_max;
596
597 /* The locations that no longer correspond to any breakpoint, unlinked
598 from bp_location array, but for which a hit may still be reported
599 by a target. */
600 VEC(bp_location_p) *moribund_locations = NULL;
601
602 /* Number of last breakpoint made. */
603
604 static int breakpoint_count;
605
606 /* The value of `breakpoint_count' before the last command that
607 created breakpoints. If the last (break-like) command created more
608 than one breakpoint, then the difference between BREAKPOINT_COUNT
609 and PREV_BREAKPOINT_COUNT is more than one. */
610 static int prev_breakpoint_count;
611
612 /* Number of last tracepoint made. */
613
614 static int tracepoint_count;
615
616 static struct cmd_list_element *breakpoint_set_cmdlist;
617 static struct cmd_list_element *breakpoint_show_cmdlist;
618 struct cmd_list_element *save_cmdlist;
619
620 /* Return whether a breakpoint is an active enabled breakpoint. */
621 static int
622 breakpoint_enabled (struct breakpoint *b)
623 {
624 return (b->enable_state == bp_enabled);
625 }
626
627 /* Set breakpoint count to NUM. */
628
629 static void
630 set_breakpoint_count (int num)
631 {
632 prev_breakpoint_count = breakpoint_count;
633 breakpoint_count = num;
634 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
635 }
636
637 /* Used by `start_rbreak_breakpoints' below, to record the current
638 breakpoint count before "rbreak" creates any breakpoint. */
639 static int rbreak_start_breakpoint_count;
640
641 /* Called at the start an "rbreak" command to record the first
642 breakpoint made. */
643
644 void
645 start_rbreak_breakpoints (void)
646 {
647 rbreak_start_breakpoint_count = breakpoint_count;
648 }
649
650 /* Called at the end of an "rbreak" command to record the last
651 breakpoint made. */
652
653 void
654 end_rbreak_breakpoints (void)
655 {
656 prev_breakpoint_count = rbreak_start_breakpoint_count;
657 }
658
659 /* Used in run_command to zero the hit count when a new run starts. */
660
661 void
662 clear_breakpoint_hit_counts (void)
663 {
664 struct breakpoint *b;
665
666 ALL_BREAKPOINTS (b)
667 b->hit_count = 0;
668 }
669
670 /* Allocate a new counted_command_line with reference count of 1.
671 The new structure owns COMMANDS. */
672
673 static struct counted_command_line *
674 alloc_counted_command_line (struct command_line *commands)
675 {
676 struct counted_command_line *result
677 = xmalloc (sizeof (struct counted_command_line));
678
679 result->refc = 1;
680 result->commands = commands;
681 return result;
682 }
683
684 /* Increment reference count. This does nothing if CMD is NULL. */
685
686 static void
687 incref_counted_command_line (struct counted_command_line *cmd)
688 {
689 if (cmd)
690 ++cmd->refc;
691 }
692
693 /* Decrement reference count. If the reference count reaches 0,
694 destroy the counted_command_line. Sets *CMDP to NULL. This does
695 nothing if *CMDP is NULL. */
696
697 static void
698 decref_counted_command_line (struct counted_command_line **cmdp)
699 {
700 if (*cmdp)
701 {
702 if (--(*cmdp)->refc == 0)
703 {
704 free_command_lines (&(*cmdp)->commands);
705 xfree (*cmdp);
706 }
707 *cmdp = NULL;
708 }
709 }
710
711 /* A cleanup function that calls decref_counted_command_line. */
712
713 static void
714 do_cleanup_counted_command_line (void *arg)
715 {
716 decref_counted_command_line (arg);
717 }
718
719 /* Create a cleanup that calls decref_counted_command_line on the
720 argument. */
721
722 static struct cleanup *
723 make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
724 {
725 return make_cleanup (do_cleanup_counted_command_line, cmdp);
726 }
727
728 \f
729 /* Return the breakpoint with the specified number, or NULL
730 if the number does not refer to an existing breakpoint. */
731
732 struct breakpoint *
733 get_breakpoint (int num)
734 {
735 struct breakpoint *b;
736
737 ALL_BREAKPOINTS (b)
738 if (b->number == num)
739 return b;
740
741 return NULL;
742 }
743
744 \f
745
746 /* Mark locations as "conditions have changed" in case the target supports
747 evaluating conditions on its side. */
748
749 static void
750 mark_breakpoint_modified (struct breakpoint *b)
751 {
752 struct bp_location *loc;
753
754 /* This is only meaningful if the target is
755 evaluating conditions and if the user has
756 opted for condition evaluation on the target's
757 side. */
758 if (gdb_evaluates_breakpoint_condition_p ()
759 || !target_supports_evaluation_of_breakpoint_conditions ())
760 return;
761
762 if (!is_breakpoint (b))
763 return;
764
765 for (loc = b->loc; loc; loc = loc->next)
766 loc->condition_changed = condition_modified;
767 }
768
769 /* Mark location as "conditions have changed" in case the target supports
770 evaluating conditions on its side. */
771
772 static void
773 mark_breakpoint_location_modified (struct bp_location *loc)
774 {
775 /* This is only meaningful if the target is
776 evaluating conditions and if the user has
777 opted for condition evaluation on the target's
778 side. */
779 if (gdb_evaluates_breakpoint_condition_p ()
780 || !target_supports_evaluation_of_breakpoint_conditions ())
781
782 return;
783
784 if (!is_breakpoint (loc->owner))
785 return;
786
787 loc->condition_changed = condition_modified;
788 }
789
790 /* Sets the condition-evaluation mode using the static global
791 condition_evaluation_mode. */
792
793 static void
794 set_condition_evaluation_mode (char *args, int from_tty,
795 struct cmd_list_element *c)
796 {
797 const char *old_mode, *new_mode;
798
799 if ((condition_evaluation_mode_1 == condition_evaluation_target)
800 && !target_supports_evaluation_of_breakpoint_conditions ())
801 {
802 condition_evaluation_mode_1 = condition_evaluation_mode;
803 warning (_("Target does not support breakpoint condition evaluation.\n"
804 "Using host evaluation mode instead."));
805 return;
806 }
807
808 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
809 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
810
811 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
812 settings was "auto". */
813 condition_evaluation_mode = condition_evaluation_mode_1;
814
815 /* Only update the mode if the user picked a different one. */
816 if (new_mode != old_mode)
817 {
818 struct bp_location *loc, **loc_tmp;
819 /* If the user switched to a different evaluation mode, we
820 need to synch the changes with the target as follows:
821
822 "host" -> "target": Send all (valid) conditions to the target.
823 "target" -> "host": Remove all the conditions from the target.
824 */
825
826 if (new_mode == condition_evaluation_target)
827 {
828 /* Mark everything modified and synch conditions with the
829 target. */
830 ALL_BP_LOCATIONS (loc, loc_tmp)
831 mark_breakpoint_location_modified (loc);
832 }
833 else
834 {
835 /* Manually mark non-duplicate locations to synch conditions
836 with the target. We do this to remove all the conditions the
837 target knows about. */
838 ALL_BP_LOCATIONS (loc, loc_tmp)
839 if (is_breakpoint (loc->owner) && loc->inserted)
840 loc->needs_update = 1;
841 }
842
843 /* Do the update. */
844 update_global_location_list (1);
845 }
846
847 return;
848 }
849
850 /* Shows the current mode of breakpoint condition evaluation. Explicitly shows
851 what "auto" is translating to. */
852
853 static void
854 show_condition_evaluation_mode (struct ui_file *file, int from_tty,
855 struct cmd_list_element *c, const char *value)
856 {
857 if (condition_evaluation_mode == condition_evaluation_auto)
858 fprintf_filtered (file,
859 _("Breakpoint condition evaluation "
860 "mode is %s (currently %s).\n"),
861 value,
862 breakpoint_condition_evaluation_mode ());
863 else
864 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
865 value);
866 }
867
868 /* A comparison function for bp_location AP and BP that is used by
869 bsearch. This comparison function only cares about addresses, unlike
870 the more general bp_location_compare function. */
871
872 static int
873 bp_location_compare_addrs (const void *ap, const void *bp)
874 {
875 struct bp_location *a = *(void **) ap;
876 struct bp_location *b = *(void **) bp;
877
878 if (a->address == b->address)
879 return 0;
880 else
881 return ((a->address > b->address) - (a->address < b->address));
882 }
883
884 /* Helper function to skip all bp_locations with addresses
885 less than ADDRESS. It returns the first bp_location that
886 is greater than or equal to ADDRESS. If none is found, just
887 return NULL. */
888
889 static struct bp_location **
890 get_first_locp_gte_addr (CORE_ADDR address)
891 {
892 struct bp_location dummy_loc;
893 struct bp_location *dummy_locp = &dummy_loc;
894 struct bp_location **locp_found = NULL;
895
896 /* Initialize the dummy location's address field. */
897 memset (&dummy_loc, 0, sizeof (struct bp_location));
898 dummy_loc.address = address;
899
900 /* Find a close match to the first location at ADDRESS. */
901 locp_found = bsearch (&dummy_locp, bp_location, bp_location_count,
902 sizeof (struct bp_location **),
903 bp_location_compare_addrs);
904
905 /* Nothing was found, nothing left to do. */
906 if (locp_found == NULL)
907 return NULL;
908
909 /* We may have found a location that is at ADDRESS but is not the first in the
910 location's list. Go backwards (if possible) and locate the first one. */
911 while ((locp_found - 1) >= bp_location
912 && (*(locp_found - 1))->address == address)
913 locp_found--;
914
915 return locp_found;
916 }
917
918 void
919 set_breakpoint_condition (struct breakpoint *b, char *exp,
920 int from_tty)
921 {
922 xfree (b->cond_string);
923 b->cond_string = NULL;
924
925 if (is_watchpoint (b))
926 {
927 struct watchpoint *w = (struct watchpoint *) b;
928
929 xfree (w->cond_exp);
930 w->cond_exp = NULL;
931 }
932 else
933 {
934 struct bp_location *loc;
935
936 for (loc = b->loc; loc; loc = loc->next)
937 {
938 xfree (loc->cond);
939 loc->cond = NULL;
940
941 /* No need to free the condition agent expression
942 bytecode (if we have one). We will handle this
943 when we go through update_global_location_list. */
944 }
945 }
946
947 if (*exp == 0)
948 {
949 if (from_tty)
950 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
951 }
952 else
953 {
954 const char *arg = exp;
955
956 /* I don't know if it matters whether this is the string the user
957 typed in or the decompiled expression. */
958 b->cond_string = xstrdup (arg);
959 b->condition_not_parsed = 0;
960
961 if (is_watchpoint (b))
962 {
963 struct watchpoint *w = (struct watchpoint *) b;
964
965 innermost_block = NULL;
966 arg = exp;
967 w->cond_exp = parse_exp_1 (&arg, 0, 0, 0);
968 if (*arg)
969 error (_("Junk at end of expression"));
970 w->cond_exp_valid_block = innermost_block;
971 }
972 else
973 {
974 struct bp_location *loc;
975
976 for (loc = b->loc; loc; loc = loc->next)
977 {
978 arg = exp;
979 loc->cond =
980 parse_exp_1 (&arg, loc->address,
981 block_for_pc (loc->address), 0);
982 if (*arg)
983 error (_("Junk at end of expression"));
984 }
985 }
986 }
987 mark_breakpoint_modified (b);
988
989 observer_notify_breakpoint_modified (b);
990 }
991
992 /* Completion for the "condition" command. */
993
994 static VEC (char_ptr) *
995 condition_completer (struct cmd_list_element *cmd,
996 const char *text, const char *word)
997 {
998 const char *space;
999
1000 text = skip_spaces_const (text);
1001 space = skip_to_space_const (text);
1002 if (*space == '\0')
1003 {
1004 int len;
1005 struct breakpoint *b;
1006 VEC (char_ptr) *result = NULL;
1007
1008 if (text[0] == '$')
1009 {
1010 /* We don't support completion of history indices. */
1011 if (isdigit (text[1]))
1012 return NULL;
1013 return complete_internalvar (&text[1]);
1014 }
1015
1016 /* We're completing the breakpoint number. */
1017 len = strlen (text);
1018
1019 ALL_BREAKPOINTS (b)
1020 {
1021 int single = b->loc->next == NULL;
1022 struct bp_location *loc;
1023 int count = 1;
1024
1025 for (loc = b->loc; loc; loc = loc->next)
1026 {
1027 char location[50];
1028
1029 if (single)
1030 xsnprintf (location, sizeof (location), "%d", b->number);
1031 else
1032 xsnprintf (location, sizeof (location), "%d.%d", b->number,
1033 count);
1034
1035 if (strncmp (location, text, len) == 0)
1036 VEC_safe_push (char_ptr, result, xstrdup (location));
1037
1038 ++count;
1039 }
1040 }
1041
1042 return result;
1043 }
1044
1045 /* We're completing the expression part. */
1046 text = skip_spaces_const (space);
1047 return expression_completer (cmd, text, word);
1048 }
1049
1050 /* condition N EXP -- set break condition of breakpoint N to EXP. */
1051
1052 static void
1053 condition_command (char *arg, int from_tty)
1054 {
1055 struct breakpoint *b;
1056 char *p;
1057 int bnum;
1058
1059 if (arg == 0)
1060 error_no_arg (_("breakpoint number"));
1061
1062 p = arg;
1063 bnum = get_number (&p);
1064 if (bnum == 0)
1065 error (_("Bad breakpoint argument: '%s'"), arg);
1066
1067 ALL_BREAKPOINTS (b)
1068 if (b->number == bnum)
1069 {
1070 /* Check if this breakpoint has a Python object assigned to
1071 it, and if it has a definition of the "stop"
1072 method. This method and conditions entered into GDB from
1073 the CLI are mutually exclusive. */
1074 if (b->py_bp_object
1075 && gdbpy_breakpoint_has_py_cond (b->py_bp_object))
1076 error (_("Cannot set a condition where a Python 'stop' "
1077 "method has been defined in the breakpoint."));
1078 set_breakpoint_condition (b, p, from_tty);
1079
1080 if (is_breakpoint (b))
1081 update_global_location_list (1);
1082
1083 return;
1084 }
1085
1086 error (_("No breakpoint number %d."), bnum);
1087 }
1088
1089 /* Check that COMMAND do not contain commands that are suitable
1090 only for tracepoints and not suitable for ordinary breakpoints.
1091 Throw if any such commands is found. */
1092
1093 static void
1094 check_no_tracepoint_commands (struct command_line *commands)
1095 {
1096 struct command_line *c;
1097
1098 for (c = commands; c; c = c->next)
1099 {
1100 int i;
1101
1102 if (c->control_type == while_stepping_control)
1103 error (_("The 'while-stepping' command can "
1104 "only be used for tracepoints"));
1105
1106 for (i = 0; i < c->body_count; ++i)
1107 check_no_tracepoint_commands ((c->body_list)[i]);
1108
1109 /* Not that command parsing removes leading whitespace and comment
1110 lines and also empty lines. So, we only need to check for
1111 command directly. */
1112 if (strstr (c->line, "collect ") == c->line)
1113 error (_("The 'collect' command can only be used for tracepoints"));
1114
1115 if (strstr (c->line, "teval ") == c->line)
1116 error (_("The 'teval' command can only be used for tracepoints"));
1117 }
1118 }
1119
1120 /* Encapsulate tests for different types of tracepoints. */
1121
1122 static int
1123 is_tracepoint_type (enum bptype type)
1124 {
1125 return (type == bp_tracepoint
1126 || type == bp_fast_tracepoint
1127 || type == bp_static_tracepoint);
1128 }
1129
1130 int
1131 is_tracepoint (const struct breakpoint *b)
1132 {
1133 return is_tracepoint_type (b->type);
1134 }
1135
1136 /* A helper function that validates that COMMANDS are valid for a
1137 breakpoint. This function will throw an exception if a problem is
1138 found. */
1139
1140 static void
1141 validate_commands_for_breakpoint (struct breakpoint *b,
1142 struct command_line *commands)
1143 {
1144 if (is_tracepoint (b))
1145 {
1146 struct tracepoint *t = (struct tracepoint *) b;
1147 struct command_line *c;
1148 struct command_line *while_stepping = 0;
1149
1150 /* Reset the while-stepping step count. The previous commands
1151 might have included a while-stepping action, while the new
1152 ones might not. */
1153 t->step_count = 0;
1154
1155 /* We need to verify that each top-level element of commands is
1156 valid for tracepoints, that there's at most one
1157 while-stepping element, and that the while-stepping's body
1158 has valid tracing commands excluding nested while-stepping.
1159 We also need to validate the tracepoint action line in the
1160 context of the tracepoint --- validate_actionline actually
1161 has side effects, like setting the tracepoint's
1162 while-stepping STEP_COUNT, in addition to checking if the
1163 collect/teval actions parse and make sense in the
1164 tracepoint's context. */
1165 for (c = commands; c; c = c->next)
1166 {
1167 if (c->control_type == while_stepping_control)
1168 {
1169 if (b->type == bp_fast_tracepoint)
1170 error (_("The 'while-stepping' command "
1171 "cannot be used for fast tracepoint"));
1172 else if (b->type == bp_static_tracepoint)
1173 error (_("The 'while-stepping' command "
1174 "cannot be used for static tracepoint"));
1175
1176 if (while_stepping)
1177 error (_("The 'while-stepping' command "
1178 "can be used only once"));
1179 else
1180 while_stepping = c;
1181 }
1182
1183 validate_actionline (c->line, b);
1184 }
1185 if (while_stepping)
1186 {
1187 struct command_line *c2;
1188
1189 gdb_assert (while_stepping->body_count == 1);
1190 c2 = while_stepping->body_list[0];
1191 for (; c2; c2 = c2->next)
1192 {
1193 if (c2->control_type == while_stepping_control)
1194 error (_("The 'while-stepping' command cannot be nested"));
1195 }
1196 }
1197 }
1198 else
1199 {
1200 check_no_tracepoint_commands (commands);
1201 }
1202 }
1203
1204 /* Return a vector of all the static tracepoints set at ADDR. The
1205 caller is responsible for releasing the vector. */
1206
1207 VEC(breakpoint_p) *
1208 static_tracepoints_here (CORE_ADDR addr)
1209 {
1210 struct breakpoint *b;
1211 VEC(breakpoint_p) *found = 0;
1212 struct bp_location *loc;
1213
1214 ALL_BREAKPOINTS (b)
1215 if (b->type == bp_static_tracepoint)
1216 {
1217 for (loc = b->loc; loc; loc = loc->next)
1218 if (loc->address == addr)
1219 VEC_safe_push(breakpoint_p, found, b);
1220 }
1221
1222 return found;
1223 }
1224
1225 /* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
1226 validate that only allowed commands are included. */
1227
1228 void
1229 breakpoint_set_commands (struct breakpoint *b,
1230 struct command_line *commands)
1231 {
1232 validate_commands_for_breakpoint (b, commands);
1233
1234 decref_counted_command_line (&b->commands);
1235 b->commands = alloc_counted_command_line (commands);
1236 observer_notify_breakpoint_modified (b);
1237 }
1238
1239 /* Set the internal `silent' flag on the breakpoint. Note that this
1240 is not the same as the "silent" that may appear in the breakpoint's
1241 commands. */
1242
1243 void
1244 breakpoint_set_silent (struct breakpoint *b, int silent)
1245 {
1246 int old_silent = b->silent;
1247
1248 b->silent = silent;
1249 if (old_silent != silent)
1250 observer_notify_breakpoint_modified (b);
1251 }
1252
1253 /* Set the thread for this breakpoint. If THREAD is -1, make the
1254 breakpoint work for any thread. */
1255
1256 void
1257 breakpoint_set_thread (struct breakpoint *b, int thread)
1258 {
1259 int old_thread = b->thread;
1260
1261 b->thread = thread;
1262 if (old_thread != thread)
1263 observer_notify_breakpoint_modified (b);
1264 }
1265
1266 /* Set the task for this breakpoint. If TASK is 0, make the
1267 breakpoint work for any task. */
1268
1269 void
1270 breakpoint_set_task (struct breakpoint *b, int task)
1271 {
1272 int old_task = b->task;
1273
1274 b->task = task;
1275 if (old_task != task)
1276 observer_notify_breakpoint_modified (b);
1277 }
1278
1279 void
1280 check_tracepoint_command (char *line, void *closure)
1281 {
1282 struct breakpoint *b = closure;
1283
1284 validate_actionline (line, b);
1285 }
1286
1287 /* A structure used to pass information through
1288 map_breakpoint_numbers. */
1289
1290 struct commands_info
1291 {
1292 /* True if the command was typed at a tty. */
1293 int from_tty;
1294
1295 /* The breakpoint range spec. */
1296 char *arg;
1297
1298 /* Non-NULL if the body of the commands are being read from this
1299 already-parsed command. */
1300 struct command_line *control;
1301
1302 /* The command lines read from the user, or NULL if they have not
1303 yet been read. */
1304 struct counted_command_line *cmd;
1305 };
1306
1307 /* A callback for map_breakpoint_numbers that sets the commands for
1308 commands_command. */
1309
1310 static void
1311 do_map_commands_command (struct breakpoint *b, void *data)
1312 {
1313 struct commands_info *info = data;
1314
1315 if (info->cmd == NULL)
1316 {
1317 struct command_line *l;
1318
1319 if (info->control != NULL)
1320 l = copy_command_lines (info->control->body_list[0]);
1321 else
1322 {
1323 struct cleanup *old_chain;
1324 char *str;
1325
1326 str = xstrprintf (_("Type commands for breakpoint(s) "
1327 "%s, one per line."),
1328 info->arg);
1329
1330 old_chain = make_cleanup (xfree, str);
1331
1332 l = read_command_lines (str,
1333 info->from_tty, 1,
1334 (is_tracepoint (b)
1335 ? check_tracepoint_command : 0),
1336 b);
1337
1338 do_cleanups (old_chain);
1339 }
1340
1341 info->cmd = alloc_counted_command_line (l);
1342 }
1343
1344 /* If a breakpoint was on the list more than once, we don't need to
1345 do anything. */
1346 if (b->commands != info->cmd)
1347 {
1348 validate_commands_for_breakpoint (b, info->cmd->commands);
1349 incref_counted_command_line (info->cmd);
1350 decref_counted_command_line (&b->commands);
1351 b->commands = info->cmd;
1352 observer_notify_breakpoint_modified (b);
1353 }
1354 }
1355
1356 static void
1357 commands_command_1 (char *arg, int from_tty,
1358 struct command_line *control)
1359 {
1360 struct cleanup *cleanups;
1361 struct commands_info info;
1362
1363 info.from_tty = from_tty;
1364 info.control = control;
1365 info.cmd = NULL;
1366 /* If we read command lines from the user, then `info' will hold an
1367 extra reference to the commands that we must clean up. */
1368 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
1369
1370 if (arg == NULL || !*arg)
1371 {
1372 if (breakpoint_count - prev_breakpoint_count > 1)
1373 arg = xstrprintf ("%d-%d", prev_breakpoint_count + 1,
1374 breakpoint_count);
1375 else if (breakpoint_count > 0)
1376 arg = xstrprintf ("%d", breakpoint_count);
1377 else
1378 {
1379 /* So that we don't try to free the incoming non-NULL
1380 argument in the cleanup below. Mapping breakpoint
1381 numbers will fail in this case. */
1382 arg = NULL;
1383 }
1384 }
1385 else
1386 /* The command loop has some static state, so we need to preserve
1387 our argument. */
1388 arg = xstrdup (arg);
1389
1390 if (arg != NULL)
1391 make_cleanup (xfree, arg);
1392
1393 info.arg = arg;
1394
1395 map_breakpoint_numbers (arg, do_map_commands_command, &info);
1396
1397 if (info.cmd == NULL)
1398 error (_("No breakpoints specified."));
1399
1400 do_cleanups (cleanups);
1401 }
1402
1403 static void
1404 commands_command (char *arg, int from_tty)
1405 {
1406 commands_command_1 (arg, from_tty, NULL);
1407 }
1408
1409 /* Like commands_command, but instead of reading the commands from
1410 input stream, takes them from an already parsed command structure.
1411
1412 This is used by cli-script.c to DTRT with breakpoint commands
1413 that are part of if and while bodies. */
1414 enum command_control_type
1415 commands_from_control_command (char *arg, struct command_line *cmd)
1416 {
1417 commands_command_1 (arg, 0, cmd);
1418 return simple_control;
1419 }
1420
1421 /* Return non-zero if BL->TARGET_INFO contains valid information. */
1422
1423 static int
1424 bp_location_has_shadow (struct bp_location *bl)
1425 {
1426 if (bl->loc_type != bp_loc_software_breakpoint)
1427 return 0;
1428 if (!bl->inserted)
1429 return 0;
1430 if (bl->target_info.shadow_len == 0)
1431 /* BL isn't valid, or doesn't shadow memory. */
1432 return 0;
1433 return 1;
1434 }
1435
1436 /* Update BUF, which is LEN bytes read from the target address MEMADDR,
1437 by replacing any memory breakpoints with their shadowed contents.
1438
1439 If READBUF is not NULL, this buffer must not overlap with any of
1440 the breakpoint location's shadow_contents buffers. Otherwise,
1441 a failed assertion internal error will be raised.
1442
1443 The range of shadowed area by each bp_location is:
1444 bl->address - bp_location_placed_address_before_address_max
1445 up to bl->address + bp_location_shadow_len_after_address_max
1446 The range we were requested to resolve shadows for is:
1447 memaddr ... memaddr + len
1448 Thus the safe cutoff boundaries for performance optimization are
1449 memaddr + len <= (bl->address
1450 - bp_location_placed_address_before_address_max)
1451 and:
1452 bl->address + bp_location_shadow_len_after_address_max <= memaddr */
1453
1454 void
1455 breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1456 const gdb_byte *writebuf_org,
1457 ULONGEST memaddr, LONGEST len)
1458 {
1459 /* Left boundary, right boundary and median element of our binary
1460 search. */
1461 unsigned bc_l, bc_r, bc;
1462
1463 /* Find BC_L which is a leftmost element which may affect BUF
1464 content. It is safe to report lower value but a failure to
1465 report higher one. */
1466
1467 bc_l = 0;
1468 bc_r = bp_location_count;
1469 while (bc_l + 1 < bc_r)
1470 {
1471 struct bp_location *bl;
1472
1473 bc = (bc_l + bc_r) / 2;
1474 bl = bp_location[bc];
1475
1476 /* Check first BL->ADDRESS will not overflow due to the added
1477 constant. Then advance the left boundary only if we are sure
1478 the BC element can in no way affect the BUF content (MEMADDR
1479 to MEMADDR + LEN range).
1480
1481 Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1482 offset so that we cannot miss a breakpoint with its shadow
1483 range tail still reaching MEMADDR. */
1484
1485 if ((bl->address + bp_location_shadow_len_after_address_max
1486 >= bl->address)
1487 && (bl->address + bp_location_shadow_len_after_address_max
1488 <= memaddr))
1489 bc_l = bc;
1490 else
1491 bc_r = bc;
1492 }
1493
1494 /* Due to the binary search above, we need to make sure we pick the
1495 first location that's at BC_L's address. E.g., if there are
1496 multiple locations at the same address, BC_L may end up pointing
1497 at a duplicate location, and miss the "master"/"inserted"
1498 location. Say, given locations L1, L2 and L3 at addresses A and
1499 B:
1500
1501 L1@A, L2@A, L3@B, ...
1502
1503 BC_L could end up pointing at location L2, while the "master"
1504 location could be L1. Since the `loc->inserted' flag is only set
1505 on "master" locations, we'd forget to restore the shadow of L1
1506 and L2. */
1507 while (bc_l > 0
1508 && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1509 bc_l--;
1510
1511 /* Now do full processing of the found relevant range of elements. */
1512
1513 for (bc = bc_l; bc < bp_location_count; bc++)
1514 {
1515 struct bp_location *bl = bp_location[bc];
1516 CORE_ADDR bp_addr = 0;
1517 int bp_size = 0;
1518 int bptoffset = 0;
1519
1520 /* bp_location array has BL->OWNER always non-NULL. */
1521 if (bl->owner->type == bp_none)
1522 warning (_("reading through apparently deleted breakpoint #%d?"),
1523 bl->owner->number);
1524
1525 /* Performance optimization: any further element can no longer affect BUF
1526 content. */
1527
1528 if (bl->address >= bp_location_placed_address_before_address_max
1529 && memaddr + len <= (bl->address
1530 - bp_location_placed_address_before_address_max))
1531 break;
1532
1533 if (!bp_location_has_shadow (bl))
1534 continue;
1535 if (!breakpoint_address_match (bl->target_info.placed_address_space, 0,
1536 current_program_space->aspace, 0))
1537 continue;
1538
1539 /* Addresses and length of the part of the breakpoint that
1540 we need to copy. */
1541 bp_addr = bl->target_info.placed_address;
1542 bp_size = bl->target_info.shadow_len;
1543
1544 if (bp_addr + bp_size <= memaddr)
1545 /* The breakpoint is entirely before the chunk of memory we
1546 are reading. */
1547 continue;
1548
1549 if (bp_addr >= memaddr + len)
1550 /* The breakpoint is entirely after the chunk of memory we are
1551 reading. */
1552 continue;
1553
1554 /* Offset within shadow_contents. */
1555 if (bp_addr < memaddr)
1556 {
1557 /* Only copy the second part of the breakpoint. */
1558 bp_size -= memaddr - bp_addr;
1559 bptoffset = memaddr - bp_addr;
1560 bp_addr = memaddr;
1561 }
1562
1563 if (bp_addr + bp_size > memaddr + len)
1564 {
1565 /* Only copy the first part of the breakpoint. */
1566 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1567 }
1568
1569 if (readbuf != NULL)
1570 {
1571 /* Verify that the readbuf buffer does not overlap with
1572 the shadow_contents buffer. */
1573 gdb_assert (bl->target_info.shadow_contents >= readbuf + len
1574 || readbuf >= (bl->target_info.shadow_contents
1575 + bl->target_info.shadow_len));
1576
1577 /* Update the read buffer with this inserted breakpoint's
1578 shadow. */
1579 memcpy (readbuf + bp_addr - memaddr,
1580 bl->target_info.shadow_contents + bptoffset, bp_size);
1581 }
1582 else
1583 {
1584 struct gdbarch *gdbarch = bl->gdbarch;
1585 const unsigned char *bp;
1586 CORE_ADDR placed_address = bl->target_info.placed_address;
1587 int placed_size = bl->target_info.placed_size;
1588
1589 /* Update the shadow with what we want to write to memory. */
1590 memcpy (bl->target_info.shadow_contents + bptoffset,
1591 writebuf_org + bp_addr - memaddr, bp_size);
1592
1593 /* Determine appropriate breakpoint contents and size for this
1594 address. */
1595 bp = gdbarch_breakpoint_from_pc (gdbarch, &placed_address, &placed_size);
1596
1597 /* Update the final write buffer with this inserted
1598 breakpoint's INSN. */
1599 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1600 }
1601 }
1602 }
1603 \f
1604
1605 /* Return true if BPT is either a software breakpoint or a hardware
1606 breakpoint. */
1607
1608 int
1609 is_breakpoint (const struct breakpoint *bpt)
1610 {
1611 return (bpt->type == bp_breakpoint
1612 || bpt->type == bp_hardware_breakpoint
1613 || bpt->type == bp_dprintf);
1614 }
1615
1616 /* Return true if BPT is of any hardware watchpoint kind. */
1617
1618 static int
1619 is_hardware_watchpoint (const struct breakpoint *bpt)
1620 {
1621 return (bpt->type == bp_hardware_watchpoint
1622 || bpt->type == bp_read_watchpoint
1623 || bpt->type == bp_access_watchpoint);
1624 }
1625
1626 /* Return true if BPT is of any watchpoint kind, hardware or
1627 software. */
1628
1629 int
1630 is_watchpoint (const struct breakpoint *bpt)
1631 {
1632 return (is_hardware_watchpoint (bpt)
1633 || bpt->type == bp_watchpoint);
1634 }
1635
1636 /* Returns true if the current thread and its running state are safe
1637 to evaluate or update watchpoint B. Watchpoints on local
1638 expressions need to be evaluated in the context of the thread that
1639 was current when the watchpoint was created, and, that thread needs
1640 to be stopped to be able to select the correct frame context.
1641 Watchpoints on global expressions can be evaluated on any thread,
1642 and in any state. It is presently left to the target allowing
1643 memory accesses when threads are running. */
1644
1645 static int
1646 watchpoint_in_thread_scope (struct watchpoint *b)
1647 {
1648 return (b->base.pspace == current_program_space
1649 && (ptid_equal (b->watchpoint_thread, null_ptid)
1650 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1651 && !is_executing (inferior_ptid))));
1652 }
1653
1654 /* Set watchpoint B to disp_del_at_next_stop, even including its possible
1655 associated bp_watchpoint_scope breakpoint. */
1656
1657 static void
1658 watchpoint_del_at_next_stop (struct watchpoint *w)
1659 {
1660 struct breakpoint *b = &w->base;
1661
1662 if (b->related_breakpoint != b)
1663 {
1664 gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1665 gdb_assert (b->related_breakpoint->related_breakpoint == b);
1666 b->related_breakpoint->disposition = disp_del_at_next_stop;
1667 b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1668 b->related_breakpoint = b;
1669 }
1670 b->disposition = disp_del_at_next_stop;
1671 }
1672
1673 /* Assuming that B is a watchpoint:
1674 - Reparse watchpoint expression, if REPARSE is non-zero
1675 - Evaluate expression and store the result in B->val
1676 - Evaluate the condition if there is one, and store the result
1677 in b->loc->cond.
1678 - Update the list of values that must be watched in B->loc.
1679
1680 If the watchpoint disposition is disp_del_at_next_stop, then do
1681 nothing. If this is local watchpoint that is out of scope, delete
1682 it.
1683
1684 Even with `set breakpoint always-inserted on' the watchpoints are
1685 removed + inserted on each stop here. Normal breakpoints must
1686 never be removed because they might be missed by a running thread
1687 when debugging in non-stop mode. On the other hand, hardware
1688 watchpoints (is_hardware_watchpoint; processed here) are specific
1689 to each LWP since they are stored in each LWP's hardware debug
1690 registers. Therefore, such LWP must be stopped first in order to
1691 be able to modify its hardware watchpoints.
1692
1693 Hardware watchpoints must be reset exactly once after being
1694 presented to the user. It cannot be done sooner, because it would
1695 reset the data used to present the watchpoint hit to the user. And
1696 it must not be done later because it could display the same single
1697 watchpoint hit during multiple GDB stops. Note that the latter is
1698 relevant only to the hardware watchpoint types bp_read_watchpoint
1699 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1700 not user-visible - its hit is suppressed if the memory content has
1701 not changed.
1702
1703 The following constraints influence the location where we can reset
1704 hardware watchpoints:
1705
1706 * target_stopped_by_watchpoint and target_stopped_data_address are
1707 called several times when GDB stops.
1708
1709 [linux]
1710 * Multiple hardware watchpoints can be hit at the same time,
1711 causing GDB to stop. GDB only presents one hardware watchpoint
1712 hit at a time as the reason for stopping, and all the other hits
1713 are presented later, one after the other, each time the user
1714 requests the execution to be resumed. Execution is not resumed
1715 for the threads still having pending hit event stored in
1716 LWP_INFO->STATUS. While the watchpoint is already removed from
1717 the inferior on the first stop the thread hit event is kept being
1718 reported from its cached value by linux_nat_stopped_data_address
1719 until the real thread resume happens after the watchpoint gets
1720 presented and thus its LWP_INFO->STATUS gets reset.
1721
1722 Therefore the hardware watchpoint hit can get safely reset on the
1723 watchpoint removal from inferior. */
1724
1725 static void
1726 update_watchpoint (struct watchpoint *b, int reparse)
1727 {
1728 int within_current_scope;
1729 struct frame_id saved_frame_id;
1730 int frame_saved;
1731
1732 /* If this is a local watchpoint, we only want to check if the
1733 watchpoint frame is in scope if the current thread is the thread
1734 that was used to create the watchpoint. */
1735 if (!watchpoint_in_thread_scope (b))
1736 return;
1737
1738 if (b->base.disposition == disp_del_at_next_stop)
1739 return;
1740
1741 frame_saved = 0;
1742
1743 /* Determine if the watchpoint is within scope. */
1744 if (b->exp_valid_block == NULL)
1745 within_current_scope = 1;
1746 else
1747 {
1748 struct frame_info *fi = get_current_frame ();
1749 struct gdbarch *frame_arch = get_frame_arch (fi);
1750 CORE_ADDR frame_pc = get_frame_pc (fi);
1751
1752 /* If we're in a function epilogue, unwinding may not work
1753 properly, so do not attempt to recreate locations at this
1754 point. See similar comments in watchpoint_check. */
1755 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
1756 return;
1757
1758 /* Save the current frame's ID so we can restore it after
1759 evaluating the watchpoint expression on its own frame. */
1760 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1761 took a frame parameter, so that we didn't have to change the
1762 selected frame. */
1763 frame_saved = 1;
1764 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1765
1766 fi = frame_find_by_id (b->watchpoint_frame);
1767 within_current_scope = (fi != NULL);
1768 if (within_current_scope)
1769 select_frame (fi);
1770 }
1771
1772 /* We don't free locations. They are stored in the bp_location array
1773 and update_global_location_list will eventually delete them and
1774 remove breakpoints if needed. */
1775 b->base.loc = NULL;
1776
1777 if (within_current_scope && reparse)
1778 {
1779 const char *s;
1780
1781 if (b->exp)
1782 {
1783 xfree (b->exp);
1784 b->exp = NULL;
1785 }
1786 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1787 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
1788 /* If the meaning of expression itself changed, the old value is
1789 no longer relevant. We don't want to report a watchpoint hit
1790 to the user when the old value and the new value may actually
1791 be completely different objects. */
1792 value_free (b->val);
1793 b->val = NULL;
1794 b->val_valid = 0;
1795
1796 /* Note that unlike with breakpoints, the watchpoint's condition
1797 expression is stored in the breakpoint object, not in the
1798 locations (re)created below. */
1799 if (b->base.cond_string != NULL)
1800 {
1801 if (b->cond_exp != NULL)
1802 {
1803 xfree (b->cond_exp);
1804 b->cond_exp = NULL;
1805 }
1806
1807 s = b->base.cond_string;
1808 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
1809 }
1810 }
1811
1812 /* If we failed to parse the expression, for example because
1813 it refers to a global variable in a not-yet-loaded shared library,
1814 don't try to insert watchpoint. We don't automatically delete
1815 such watchpoint, though, since failure to parse expression
1816 is different from out-of-scope watchpoint. */
1817 if ( !target_has_execution)
1818 {
1819 /* Without execution, memory can't change. No use to try and
1820 set watchpoint locations. The watchpoint will be reset when
1821 the target gains execution, through breakpoint_re_set. */
1822 }
1823 else if (within_current_scope && b->exp)
1824 {
1825 int pc = 0;
1826 struct value *val_chain, *v, *result, *next;
1827 struct program_space *frame_pspace;
1828
1829 fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain);
1830
1831 /* Avoid setting b->val if it's already set. The meaning of
1832 b->val is 'the last value' user saw, and we should update
1833 it only if we reported that last value to user. As it
1834 happens, the code that reports it updates b->val directly.
1835 We don't keep track of the memory value for masked
1836 watchpoints. */
1837 if (!b->val_valid && !is_masked_watchpoint (&b->base))
1838 {
1839 b->val = v;
1840 b->val_valid = 1;
1841 }
1842
1843 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1844
1845 /* Look at each value on the value chain. */
1846 for (v = val_chain; v; v = value_next (v))
1847 {
1848 /* If it's a memory location, and GDB actually needed
1849 its contents to evaluate the expression, then we
1850 must watch it. If the first value returned is
1851 still lazy, that means an error occurred reading it;
1852 watch it anyway in case it becomes readable. */
1853 if (VALUE_LVAL (v) == lval_memory
1854 && (v == val_chain || ! value_lazy (v)))
1855 {
1856 struct type *vtype = check_typedef (value_type (v));
1857
1858 /* We only watch structs and arrays if user asked
1859 for it explicitly, never if they just happen to
1860 appear in the middle of some value chain. */
1861 if (v == result
1862 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1863 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1864 {
1865 CORE_ADDR addr;
1866 int type;
1867 struct bp_location *loc, **tmp;
1868
1869 addr = value_address (v);
1870 type = hw_write;
1871 if (b->base.type == bp_read_watchpoint)
1872 type = hw_read;
1873 else if (b->base.type == bp_access_watchpoint)
1874 type = hw_access;
1875
1876 loc = allocate_bp_location (&b->base);
1877 for (tmp = &(b->base.loc); *tmp != NULL; tmp = &((*tmp)->next))
1878 ;
1879 *tmp = loc;
1880 loc->gdbarch = get_type_arch (value_type (v));
1881
1882 loc->pspace = frame_pspace;
1883 loc->address = addr;
1884 loc->length = TYPE_LENGTH (value_type (v));
1885 loc->watchpoint_type = type;
1886 }
1887 }
1888 }
1889
1890 /* Change the type of breakpoint between hardware assisted or
1891 an ordinary watchpoint depending on the hardware support
1892 and free hardware slots. REPARSE is set when the inferior
1893 is started. */
1894 if (reparse)
1895 {
1896 int reg_cnt;
1897 enum bp_loc_type loc_type;
1898 struct bp_location *bl;
1899
1900 reg_cnt = can_use_hardware_watchpoint (val_chain);
1901
1902 if (reg_cnt)
1903 {
1904 int i, target_resources_ok, other_type_used;
1905 enum bptype type;
1906
1907 /* Use an exact watchpoint when there's only one memory region to be
1908 watched, and only one debug register is needed to watch it. */
1909 b->exact = target_exact_watchpoints && reg_cnt == 1;
1910
1911 /* We need to determine how many resources are already
1912 used for all other hardware watchpoints plus this one
1913 to see if we still have enough resources to also fit
1914 this watchpoint in as well. */
1915
1916 /* If this is a software watchpoint, we try to turn it
1917 to a hardware one -- count resources as if B was of
1918 hardware watchpoint type. */
1919 type = b->base.type;
1920 if (type == bp_watchpoint)
1921 type = bp_hardware_watchpoint;
1922
1923 /* This watchpoint may or may not have been placed on
1924 the list yet at this point (it won't be in the list
1925 if we're trying to create it for the first time,
1926 through watch_command), so always account for it
1927 manually. */
1928
1929 /* Count resources used by all watchpoints except B. */
1930 i = hw_watchpoint_used_count_others (&b->base, type, &other_type_used);
1931
1932 /* Add in the resources needed for B. */
1933 i += hw_watchpoint_use_count (&b->base);
1934
1935 target_resources_ok
1936 = target_can_use_hardware_watchpoint (type, i, other_type_used);
1937 if (target_resources_ok <= 0)
1938 {
1939 int sw_mode = b->base.ops->works_in_software_mode (&b->base);
1940
1941 if (target_resources_ok == 0 && !sw_mode)
1942 error (_("Target does not support this type of "
1943 "hardware watchpoint."));
1944 else if (target_resources_ok < 0 && !sw_mode)
1945 error (_("There are not enough available hardware "
1946 "resources for this watchpoint."));
1947
1948 /* Downgrade to software watchpoint. */
1949 b->base.type = bp_watchpoint;
1950 }
1951 else
1952 {
1953 /* If this was a software watchpoint, we've just
1954 found we have enough resources to turn it to a
1955 hardware watchpoint. Otherwise, this is a
1956 nop. */
1957 b->base.type = type;
1958 }
1959 }
1960 else if (!b->base.ops->works_in_software_mode (&b->base))
1961 error (_("Expression cannot be implemented with "
1962 "read/access watchpoint."));
1963 else
1964 b->base.type = bp_watchpoint;
1965
1966 loc_type = (b->base.type == bp_watchpoint? bp_loc_other
1967 : bp_loc_hardware_watchpoint);
1968 for (bl = b->base.loc; bl; bl = bl->next)
1969 bl->loc_type = loc_type;
1970 }
1971
1972 for (v = val_chain; v; v = next)
1973 {
1974 next = value_next (v);
1975 if (v != b->val)
1976 value_free (v);
1977 }
1978
1979 /* If a software watchpoint is not watching any memory, then the
1980 above left it without any location set up. But,
1981 bpstat_stop_status requires a location to be able to report
1982 stops, so make sure there's at least a dummy one. */
1983 if (b->base.type == bp_watchpoint && b->base.loc == NULL)
1984 {
1985 struct breakpoint *base = &b->base;
1986 base->loc = allocate_bp_location (base);
1987 base->loc->pspace = frame_pspace;
1988 base->loc->address = -1;
1989 base->loc->length = -1;
1990 base->loc->watchpoint_type = -1;
1991 }
1992 }
1993 else if (!within_current_scope)
1994 {
1995 printf_filtered (_("\
1996 Watchpoint %d deleted because the program has left the block\n\
1997 in which its expression is valid.\n"),
1998 b->base.number);
1999 watchpoint_del_at_next_stop (b);
2000 }
2001
2002 /* Restore the selected frame. */
2003 if (frame_saved)
2004 select_frame (frame_find_by_id (saved_frame_id));
2005 }
2006
2007
2008 /* Returns 1 iff breakpoint location should be
2009 inserted in the inferior. We don't differentiate the type of BL's owner
2010 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2011 breakpoint_ops is not defined, because in insert_bp_location,
2012 tracepoint's insert_location will not be called. */
2013 static int
2014 should_be_inserted (struct bp_location *bl)
2015 {
2016 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
2017 return 0;
2018
2019 if (bl->owner->disposition == disp_del_at_next_stop)
2020 return 0;
2021
2022 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
2023 return 0;
2024
2025 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2026 return 0;
2027
2028 /* This is set for example, when we're attached to the parent of a
2029 vfork, and have detached from the child. The child is running
2030 free, and we expect it to do an exec or exit, at which point the
2031 OS makes the parent schedulable again (and the target reports
2032 that the vfork is done). Until the child is done with the shared
2033 memory region, do not insert breakpoints in the parent, otherwise
2034 the child could still trip on the parent's breakpoints. Since
2035 the parent is blocked anyway, it won't miss any breakpoint. */
2036 if (bl->pspace->breakpoints_not_allowed)
2037 return 0;
2038
2039 return 1;
2040 }
2041
2042 /* Same as should_be_inserted but does the check assuming
2043 that the location is not duplicated. */
2044
2045 static int
2046 unduplicated_should_be_inserted (struct bp_location *bl)
2047 {
2048 int result;
2049 const int save_duplicate = bl->duplicate;
2050
2051 bl->duplicate = 0;
2052 result = should_be_inserted (bl);
2053 bl->duplicate = save_duplicate;
2054 return result;
2055 }
2056
2057 /* Parses a conditional described by an expression COND into an
2058 agent expression bytecode suitable for evaluation
2059 by the bytecode interpreter. Return NULL if there was
2060 any error during parsing. */
2061
2062 static struct agent_expr *
2063 parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2064 {
2065 struct agent_expr *aexpr = NULL;
2066 volatile struct gdb_exception ex;
2067
2068 if (!cond)
2069 return NULL;
2070
2071 /* We don't want to stop processing, so catch any errors
2072 that may show up. */
2073 TRY_CATCH (ex, RETURN_MASK_ERROR)
2074 {
2075 aexpr = gen_eval_for_expr (scope, cond);
2076 }
2077
2078 if (ex.reason < 0)
2079 {
2080 /* If we got here, it means the condition could not be parsed to a valid
2081 bytecode expression and thus can't be evaluated on the target's side.
2082 It's no use iterating through the conditions. */
2083 return NULL;
2084 }
2085
2086 /* We have a valid agent expression. */
2087 return aexpr;
2088 }
2089
2090 /* Based on location BL, create a list of breakpoint conditions to be
2091 passed on to the target. If we have duplicated locations with different
2092 conditions, we will add such conditions to the list. The idea is that the
2093 target will evaluate the list of conditions and will only notify GDB when
2094 one of them is true. */
2095
2096 static void
2097 build_target_condition_list (struct bp_location *bl)
2098 {
2099 struct bp_location **locp = NULL, **loc2p;
2100 int null_condition_or_parse_error = 0;
2101 int modified = bl->needs_update;
2102 struct bp_location *loc;
2103
2104 /* This is only meaningful if the target is
2105 evaluating conditions and if the user has
2106 opted for condition evaluation on the target's
2107 side. */
2108 if (gdb_evaluates_breakpoint_condition_p ()
2109 || !target_supports_evaluation_of_breakpoint_conditions ())
2110 return;
2111
2112 /* Do a first pass to check for locations with no assigned
2113 conditions or conditions that fail to parse to a valid agent expression
2114 bytecode. If any of these happen, then it's no use to send conditions
2115 to the target since this location will always trigger and generate a
2116 response back to GDB. */
2117 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2118 {
2119 loc = (*loc2p);
2120 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2121 {
2122 if (modified)
2123 {
2124 struct agent_expr *aexpr;
2125
2126 /* Re-parse the conditions since something changed. In that
2127 case we already freed the condition bytecodes (see
2128 force_breakpoint_reinsertion). We just
2129 need to parse the condition to bytecodes again. */
2130 aexpr = parse_cond_to_aexpr (bl->address, loc->cond);
2131 loc->cond_bytecode = aexpr;
2132
2133 /* Check if we managed to parse the conditional expression
2134 correctly. If not, we will not send this condition
2135 to the target. */
2136 if (aexpr)
2137 continue;
2138 }
2139
2140 /* If we have a NULL bytecode expression, it means something
2141 went wrong or we have a null condition expression. */
2142 if (!loc->cond_bytecode)
2143 {
2144 null_condition_or_parse_error = 1;
2145 break;
2146 }
2147 }
2148 }
2149
2150 /* If any of these happened, it means we will have to evaluate the conditions
2151 for the location's address on gdb's side. It is no use keeping bytecodes
2152 for all the other duplicate locations, thus we free all of them here.
2153
2154 This is so we have a finer control over which locations' conditions are
2155 being evaluated by GDB or the remote stub. */
2156 if (null_condition_or_parse_error)
2157 {
2158 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2159 {
2160 loc = (*loc2p);
2161 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2162 {
2163 /* Only go as far as the first NULL bytecode is
2164 located. */
2165 if (!loc->cond_bytecode)
2166 return;
2167
2168 free_agent_expr (loc->cond_bytecode);
2169 loc->cond_bytecode = NULL;
2170 }
2171 }
2172 }
2173
2174 /* No NULL conditions or failed bytecode generation. Build a condition list
2175 for this location's address. */
2176 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2177 {
2178 loc = (*loc2p);
2179 if (loc->cond
2180 && is_breakpoint (loc->owner)
2181 && loc->pspace->num == bl->pspace->num
2182 && loc->owner->enable_state == bp_enabled
2183 && loc->enabled)
2184 /* Add the condition to the vector. This will be used later to send the
2185 conditions to the target. */
2186 VEC_safe_push (agent_expr_p, bl->target_info.conditions,
2187 loc->cond_bytecode);
2188 }
2189
2190 return;
2191 }
2192
2193 /* Parses a command described by string CMD into an agent expression
2194 bytecode suitable for evaluation by the bytecode interpreter.
2195 Return NULL if there was any error during parsing. */
2196
2197 static struct agent_expr *
2198 parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2199 {
2200 struct cleanup *old_cleanups = 0;
2201 struct expression *expr, **argvec;
2202 struct agent_expr *aexpr = NULL;
2203 volatile struct gdb_exception ex;
2204 const char *cmdrest;
2205 const char *format_start, *format_end;
2206 struct format_piece *fpieces;
2207 int nargs;
2208 struct gdbarch *gdbarch = get_current_arch ();
2209
2210 if (!cmd)
2211 return NULL;
2212
2213 cmdrest = cmd;
2214
2215 if (*cmdrest == ',')
2216 ++cmdrest;
2217 cmdrest = skip_spaces_const (cmdrest);
2218
2219 if (*cmdrest++ != '"')
2220 error (_("No format string following the location"));
2221
2222 format_start = cmdrest;
2223
2224 fpieces = parse_format_string (&cmdrest);
2225
2226 old_cleanups = make_cleanup (free_format_pieces_cleanup, &fpieces);
2227
2228 format_end = cmdrest;
2229
2230 if (*cmdrest++ != '"')
2231 error (_("Bad format string, non-terminated '\"'."));
2232
2233 cmdrest = skip_spaces_const (cmdrest);
2234
2235 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2236 error (_("Invalid argument syntax"));
2237
2238 if (*cmdrest == ',')
2239 cmdrest++;
2240 cmdrest = skip_spaces_const (cmdrest);
2241
2242 /* For each argument, make an expression. */
2243
2244 argvec = (struct expression **) alloca (strlen (cmd)
2245 * sizeof (struct expression *));
2246
2247 nargs = 0;
2248 while (*cmdrest != '\0')
2249 {
2250 const char *cmd1;
2251
2252 cmd1 = cmdrest;
2253 expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2254 argvec[nargs++] = expr;
2255 cmdrest = cmd1;
2256 if (*cmdrest == ',')
2257 ++cmdrest;
2258 }
2259
2260 /* We don't want to stop processing, so catch any errors
2261 that may show up. */
2262 TRY_CATCH (ex, RETURN_MASK_ERROR)
2263 {
2264 aexpr = gen_printf (scope, gdbarch, 0, 0,
2265 format_start, format_end - format_start,
2266 fpieces, nargs, argvec);
2267 }
2268
2269 if (ex.reason < 0)
2270 {
2271 /* If we got here, it means the command could not be parsed to a valid
2272 bytecode expression and thus can't be evaluated on the target's side.
2273 It's no use iterating through the other commands. */
2274 return NULL;
2275 }
2276
2277 do_cleanups (old_cleanups);
2278
2279 /* We have a valid agent expression, return it. */
2280 return aexpr;
2281 }
2282
2283 /* Based on location BL, create a list of breakpoint commands to be
2284 passed on to the target. If we have duplicated locations with
2285 different commands, we will add any such to the list. */
2286
2287 static void
2288 build_target_command_list (struct bp_location *bl)
2289 {
2290 struct bp_location **locp = NULL, **loc2p;
2291 int null_command_or_parse_error = 0;
2292 int modified = bl->needs_update;
2293 struct bp_location *loc;
2294
2295 /* For now, limit to agent-style dprintf breakpoints. */
2296 if (bl->owner->type != bp_dprintf
2297 || strcmp (dprintf_style, dprintf_style_agent) != 0)
2298 return;
2299
2300 if (!target_can_run_breakpoint_commands ())
2301 return;
2302
2303 /* Do a first pass to check for locations with no assigned
2304 conditions or conditions that fail to parse to a valid agent expression
2305 bytecode. If any of these happen, then it's no use to send conditions
2306 to the target since this location will always trigger and generate a
2307 response back to GDB. */
2308 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2309 {
2310 loc = (*loc2p);
2311 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2312 {
2313 if (modified)
2314 {
2315 struct agent_expr *aexpr;
2316
2317 /* Re-parse the commands since something changed. In that
2318 case we already freed the command bytecodes (see
2319 force_breakpoint_reinsertion). We just
2320 need to parse the command to bytecodes again. */
2321 aexpr = parse_cmd_to_aexpr (bl->address,
2322 loc->owner->extra_string);
2323 loc->cmd_bytecode = aexpr;
2324
2325 if (!aexpr)
2326 continue;
2327 }
2328
2329 /* If we have a NULL bytecode expression, it means something
2330 went wrong or we have a null command expression. */
2331 if (!loc->cmd_bytecode)
2332 {
2333 null_command_or_parse_error = 1;
2334 break;
2335 }
2336 }
2337 }
2338
2339 /* If anything failed, then we're not doing target-side commands,
2340 and so clean up. */
2341 if (null_command_or_parse_error)
2342 {
2343 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2344 {
2345 loc = (*loc2p);
2346 if (is_breakpoint (loc->owner)
2347 && loc->pspace->num == bl->pspace->num)
2348 {
2349 /* Only go as far as the first NULL bytecode is
2350 located. */
2351 if (!loc->cond_bytecode)
2352 return;
2353
2354 free_agent_expr (loc->cond_bytecode);
2355 loc->cond_bytecode = NULL;
2356 }
2357 }
2358 }
2359
2360 /* No NULL commands or failed bytecode generation. Build a command list
2361 for this location's address. */
2362 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2363 {
2364 loc = (*loc2p);
2365 if (loc->owner->extra_string
2366 && is_breakpoint (loc->owner)
2367 && loc->pspace->num == bl->pspace->num
2368 && loc->owner->enable_state == bp_enabled
2369 && loc->enabled)
2370 /* Add the command to the vector. This will be used later
2371 to send the commands to the target. */
2372 VEC_safe_push (agent_expr_p, bl->target_info.tcommands,
2373 loc->cmd_bytecode);
2374 }
2375
2376 bl->target_info.persist = 0;
2377 /* Maybe flag this location as persistent. */
2378 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2379 bl->target_info.persist = 1;
2380 }
2381
2382 /* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2383 location. Any error messages are printed to TMP_ERROR_STREAM; and
2384 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
2385 Returns 0 for success, 1 if the bp_location type is not supported or
2386 -1 for failure.
2387
2388 NOTE drow/2003-09-09: This routine could be broken down to an
2389 object-style method for each breakpoint or catchpoint type. */
2390 static int
2391 insert_bp_location (struct bp_location *bl,
2392 struct ui_file *tmp_error_stream,
2393 int *disabled_breaks,
2394 int *hw_breakpoint_error,
2395 int *hw_bp_error_explained_already)
2396 {
2397 int val = 0;
2398 char *hw_bp_err_string = NULL;
2399 struct gdb_exception e;
2400
2401 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2402 return 0;
2403
2404 /* Note we don't initialize bl->target_info, as that wipes out
2405 the breakpoint location's shadow_contents if the breakpoint
2406 is still inserted at that location. This in turn breaks
2407 target_read_memory which depends on these buffers when
2408 a memory read is requested at the breakpoint location:
2409 Once the target_info has been wiped, we fail to see that
2410 we have a breakpoint inserted at that address and thus
2411 read the breakpoint instead of returning the data saved in
2412 the breakpoint location's shadow contents. */
2413 bl->target_info.placed_address = bl->address;
2414 bl->target_info.placed_address_space = bl->pspace->aspace;
2415 bl->target_info.length = bl->length;
2416
2417 /* When working with target-side conditions, we must pass all the conditions
2418 for the same breakpoint address down to the target since GDB will not
2419 insert those locations. With a list of breakpoint conditions, the target
2420 can decide when to stop and notify GDB. */
2421
2422 if (is_breakpoint (bl->owner))
2423 {
2424 build_target_condition_list (bl);
2425 build_target_command_list (bl);
2426 /* Reset the modification marker. */
2427 bl->needs_update = 0;
2428 }
2429
2430 if (bl->loc_type == bp_loc_software_breakpoint
2431 || bl->loc_type == bp_loc_hardware_breakpoint)
2432 {
2433 if (bl->owner->type != bp_hardware_breakpoint)
2434 {
2435 /* If the explicitly specified breakpoint type
2436 is not hardware breakpoint, check the memory map to see
2437 if the breakpoint address is in read only memory or not.
2438
2439 Two important cases are:
2440 - location type is not hardware breakpoint, memory
2441 is readonly. We change the type of the location to
2442 hardware breakpoint.
2443 - location type is hardware breakpoint, memory is
2444 read-write. This means we've previously made the
2445 location hardware one, but then the memory map changed,
2446 so we undo.
2447
2448 When breakpoints are removed, remove_breakpoints will use
2449 location types we've just set here, the only possible
2450 problem is that memory map has changed during running
2451 program, but it's not going to work anyway with current
2452 gdb. */
2453 struct mem_region *mr
2454 = lookup_mem_region (bl->target_info.placed_address);
2455
2456 if (mr)
2457 {
2458 if (automatic_hardware_breakpoints)
2459 {
2460 enum bp_loc_type new_type;
2461
2462 if (mr->attrib.mode != MEM_RW)
2463 new_type = bp_loc_hardware_breakpoint;
2464 else
2465 new_type = bp_loc_software_breakpoint;
2466
2467 if (new_type != bl->loc_type)
2468 {
2469 static int said = 0;
2470
2471 bl->loc_type = new_type;
2472 if (!said)
2473 {
2474 fprintf_filtered (gdb_stdout,
2475 _("Note: automatically using "
2476 "hardware breakpoints for "
2477 "read-only addresses.\n"));
2478 said = 1;
2479 }
2480 }
2481 }
2482 else if (bl->loc_type == bp_loc_software_breakpoint
2483 && mr->attrib.mode != MEM_RW)
2484 warning (_("cannot set software breakpoint "
2485 "at readonly address %s"),
2486 paddress (bl->gdbarch, bl->address));
2487 }
2488 }
2489
2490 /* First check to see if we have to handle an overlay. */
2491 if (overlay_debugging == ovly_off
2492 || bl->section == NULL
2493 || !(section_is_overlay (bl->section)))
2494 {
2495 /* No overlay handling: just set the breakpoint. */
2496 TRY_CATCH (e, RETURN_MASK_ALL)
2497 {
2498 val = bl->owner->ops->insert_location (bl);
2499 }
2500 if (e.reason < 0)
2501 {
2502 val = 1;
2503 hw_bp_err_string = (char *) e.message;
2504 }
2505 }
2506 else
2507 {
2508 /* This breakpoint is in an overlay section.
2509 Shall we set a breakpoint at the LMA? */
2510 if (!overlay_events_enabled)
2511 {
2512 /* Yes -- overlay event support is not active,
2513 so we must try to set a breakpoint at the LMA.
2514 This will not work for a hardware breakpoint. */
2515 if (bl->loc_type == bp_loc_hardware_breakpoint)
2516 warning (_("hardware breakpoint %d not supported in overlay!"),
2517 bl->owner->number);
2518 else
2519 {
2520 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2521 bl->section);
2522 /* Set a software (trap) breakpoint at the LMA. */
2523 bl->overlay_target_info = bl->target_info;
2524 bl->overlay_target_info.placed_address = addr;
2525 val = target_insert_breakpoint (bl->gdbarch,
2526 &bl->overlay_target_info);
2527 if (val != 0)
2528 fprintf_unfiltered (tmp_error_stream,
2529 "Overlay breakpoint %d "
2530 "failed: in ROM?\n",
2531 bl->owner->number);
2532 }
2533 }
2534 /* Shall we set a breakpoint at the VMA? */
2535 if (section_is_mapped (bl->section))
2536 {
2537 /* Yes. This overlay section is mapped into memory. */
2538 TRY_CATCH (e, RETURN_MASK_ALL)
2539 {
2540 val = bl->owner->ops->insert_location (bl);
2541 }
2542 if (e.reason < 0)
2543 {
2544 val = 1;
2545 hw_bp_err_string = (char *) e.message;
2546 }
2547 }
2548 else
2549 {
2550 /* No. This breakpoint will not be inserted.
2551 No error, but do not mark the bp as 'inserted'. */
2552 return 0;
2553 }
2554 }
2555
2556 if (val)
2557 {
2558 /* Can't set the breakpoint. */
2559 if (solib_name_from_address (bl->pspace, bl->address))
2560 {
2561 /* See also: disable_breakpoints_in_shlibs. */
2562 val = 0;
2563 bl->shlib_disabled = 1;
2564 observer_notify_breakpoint_modified (bl->owner);
2565 if (!*disabled_breaks)
2566 {
2567 fprintf_unfiltered (tmp_error_stream,
2568 "Cannot insert breakpoint %d.\n",
2569 bl->owner->number);
2570 fprintf_unfiltered (tmp_error_stream,
2571 "Temporarily disabling shared "
2572 "library breakpoints:\n");
2573 }
2574 *disabled_breaks = 1;
2575 fprintf_unfiltered (tmp_error_stream,
2576 "breakpoint #%d\n", bl->owner->number);
2577 }
2578 else
2579 {
2580 if (bl->loc_type == bp_loc_hardware_breakpoint)
2581 {
2582 *hw_breakpoint_error = 1;
2583 *hw_bp_error_explained_already = hw_bp_err_string != NULL;
2584 fprintf_unfiltered (tmp_error_stream,
2585 "Cannot insert hardware breakpoint %d%s",
2586 bl->owner->number, hw_bp_err_string ? ":" : ".\n");
2587 if (hw_bp_err_string)
2588 fprintf_unfiltered (tmp_error_stream, "%s.\n", hw_bp_err_string);
2589 }
2590 else
2591 {
2592 fprintf_unfiltered (tmp_error_stream,
2593 "Cannot insert breakpoint %d.\n",
2594 bl->owner->number);
2595 fprintf_filtered (tmp_error_stream,
2596 "Error accessing memory address ");
2597 fputs_filtered (paddress (bl->gdbarch, bl->address),
2598 tmp_error_stream);
2599 fprintf_filtered (tmp_error_stream, ": %s.\n",
2600 safe_strerror (val));
2601 }
2602
2603 }
2604 }
2605 else
2606 bl->inserted = 1;
2607
2608 return val;
2609 }
2610
2611 else if (bl->loc_type == bp_loc_hardware_watchpoint
2612 /* NOTE drow/2003-09-08: This state only exists for removing
2613 watchpoints. It's not clear that it's necessary... */
2614 && bl->owner->disposition != disp_del_at_next_stop)
2615 {
2616 gdb_assert (bl->owner->ops != NULL
2617 && bl->owner->ops->insert_location != NULL);
2618
2619 val = bl->owner->ops->insert_location (bl);
2620
2621 /* If trying to set a read-watchpoint, and it turns out it's not
2622 supported, try emulating one with an access watchpoint. */
2623 if (val == 1 && bl->watchpoint_type == hw_read)
2624 {
2625 struct bp_location *loc, **loc_temp;
2626
2627 /* But don't try to insert it, if there's already another
2628 hw_access location that would be considered a duplicate
2629 of this one. */
2630 ALL_BP_LOCATIONS (loc, loc_temp)
2631 if (loc != bl
2632 && loc->watchpoint_type == hw_access
2633 && watchpoint_locations_match (bl, loc))
2634 {
2635 bl->duplicate = 1;
2636 bl->inserted = 1;
2637 bl->target_info = loc->target_info;
2638 bl->watchpoint_type = hw_access;
2639 val = 0;
2640 break;
2641 }
2642
2643 if (val == 1)
2644 {
2645 bl->watchpoint_type = hw_access;
2646 val = bl->owner->ops->insert_location (bl);
2647
2648 if (val)
2649 /* Back to the original value. */
2650 bl->watchpoint_type = hw_read;
2651 }
2652 }
2653
2654 bl->inserted = (val == 0);
2655 }
2656
2657 else if (bl->owner->type == bp_catchpoint)
2658 {
2659 gdb_assert (bl->owner->ops != NULL
2660 && bl->owner->ops->insert_location != NULL);
2661
2662 val = bl->owner->ops->insert_location (bl);
2663 if (val)
2664 {
2665 bl->owner->enable_state = bp_disabled;
2666
2667 if (val == 1)
2668 warning (_("\
2669 Error inserting catchpoint %d: Your system does not support this type\n\
2670 of catchpoint."), bl->owner->number);
2671 else
2672 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2673 }
2674
2675 bl->inserted = (val == 0);
2676
2677 /* We've already printed an error message if there was a problem
2678 inserting this catchpoint, and we've disabled the catchpoint,
2679 so just return success. */
2680 return 0;
2681 }
2682
2683 return 0;
2684 }
2685
2686 /* This function is called when program space PSPACE is about to be
2687 deleted. It takes care of updating breakpoints to not reference
2688 PSPACE anymore. */
2689
2690 void
2691 breakpoint_program_space_exit (struct program_space *pspace)
2692 {
2693 struct breakpoint *b, *b_temp;
2694 struct bp_location *loc, **loc_temp;
2695
2696 /* Remove any breakpoint that was set through this program space. */
2697 ALL_BREAKPOINTS_SAFE (b, b_temp)
2698 {
2699 if (b->pspace == pspace)
2700 delete_breakpoint (b);
2701 }
2702
2703 /* Breakpoints set through other program spaces could have locations
2704 bound to PSPACE as well. Remove those. */
2705 ALL_BP_LOCATIONS (loc, loc_temp)
2706 {
2707 struct bp_location *tmp;
2708
2709 if (loc->pspace == pspace)
2710 {
2711 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
2712 if (loc->owner->loc == loc)
2713 loc->owner->loc = loc->next;
2714 else
2715 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2716 if (tmp->next == loc)
2717 {
2718 tmp->next = loc->next;
2719 break;
2720 }
2721 }
2722 }
2723
2724 /* Now update the global location list to permanently delete the
2725 removed locations above. */
2726 update_global_location_list (0);
2727 }
2728
2729 /* Make sure all breakpoints are inserted in inferior.
2730 Throws exception on any error.
2731 A breakpoint that is already inserted won't be inserted
2732 again, so calling this function twice is safe. */
2733 void
2734 insert_breakpoints (void)
2735 {
2736 struct breakpoint *bpt;
2737
2738 ALL_BREAKPOINTS (bpt)
2739 if (is_hardware_watchpoint (bpt))
2740 {
2741 struct watchpoint *w = (struct watchpoint *) bpt;
2742
2743 update_watchpoint (w, 0 /* don't reparse. */);
2744 }
2745
2746 update_global_location_list (1);
2747
2748 /* update_global_location_list does not insert breakpoints when
2749 always_inserted_mode is not enabled. Explicitly insert them
2750 now. */
2751 if (!breakpoints_always_inserted_mode ())
2752 insert_breakpoint_locations ();
2753 }
2754
2755 /* Invoke CALLBACK for each of bp_location. */
2756
2757 void
2758 iterate_over_bp_locations (walk_bp_location_callback callback)
2759 {
2760 struct bp_location *loc, **loc_tmp;
2761
2762 ALL_BP_LOCATIONS (loc, loc_tmp)
2763 {
2764 callback (loc, NULL);
2765 }
2766 }
2767
2768 /* This is used when we need to synch breakpoint conditions between GDB and the
2769 target. It is the case with deleting and disabling of breakpoints when using
2770 always-inserted mode. */
2771
2772 static void
2773 update_inserted_breakpoint_locations (void)
2774 {
2775 struct bp_location *bl, **blp_tmp;
2776 int error_flag = 0;
2777 int val = 0;
2778 int disabled_breaks = 0;
2779 int hw_breakpoint_error = 0;
2780 int hw_bp_details_reported = 0;
2781
2782 struct ui_file *tmp_error_stream = mem_fileopen ();
2783 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
2784
2785 /* Explicitly mark the warning -- this will only be printed if
2786 there was an error. */
2787 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
2788
2789 save_current_space_and_thread ();
2790
2791 ALL_BP_LOCATIONS (bl, blp_tmp)
2792 {
2793 /* We only want to update software breakpoints and hardware
2794 breakpoints. */
2795 if (!is_breakpoint (bl->owner))
2796 continue;
2797
2798 /* We only want to update locations that are already inserted
2799 and need updating. This is to avoid unwanted insertion during
2800 deletion of breakpoints. */
2801 if (!bl->inserted || (bl->inserted && !bl->needs_update))
2802 continue;
2803
2804 switch_to_program_space_and_thread (bl->pspace);
2805
2806 /* For targets that support global breakpoints, there's no need
2807 to select an inferior to insert breakpoint to. In fact, even
2808 if we aren't attached to any process yet, we should still
2809 insert breakpoints. */
2810 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
2811 && ptid_equal (inferior_ptid, null_ptid))
2812 continue;
2813
2814 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
2815 &hw_breakpoint_error, &hw_bp_details_reported);
2816 if (val)
2817 error_flag = val;
2818 }
2819
2820 if (error_flag)
2821 {
2822 target_terminal_ours_for_output ();
2823 error_stream (tmp_error_stream);
2824 }
2825
2826 do_cleanups (cleanups);
2827 }
2828
2829 /* Used when starting or continuing the program. */
2830
2831 static void
2832 insert_breakpoint_locations (void)
2833 {
2834 struct breakpoint *bpt;
2835 struct bp_location *bl, **blp_tmp;
2836 int error_flag = 0;
2837 int val = 0;
2838 int disabled_breaks = 0;
2839 int hw_breakpoint_error = 0;
2840 int hw_bp_error_explained_already = 0;
2841
2842 struct ui_file *tmp_error_stream = mem_fileopen ();
2843 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
2844
2845 /* Explicitly mark the warning -- this will only be printed if
2846 there was an error. */
2847 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
2848
2849 save_current_space_and_thread ();
2850
2851 ALL_BP_LOCATIONS (bl, blp_tmp)
2852 {
2853 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2854 continue;
2855
2856 /* There is no point inserting thread-specific breakpoints if
2857 the thread no longer exists. ALL_BP_LOCATIONS bp_location
2858 has BL->OWNER always non-NULL. */
2859 if (bl->owner->thread != -1
2860 && !valid_thread_id (bl->owner->thread))
2861 continue;
2862
2863 switch_to_program_space_and_thread (bl->pspace);
2864
2865 /* For targets that support global breakpoints, there's no need
2866 to select an inferior to insert breakpoint to. In fact, even
2867 if we aren't attached to any process yet, we should still
2868 insert breakpoints. */
2869 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
2870 && ptid_equal (inferior_ptid, null_ptid))
2871 continue;
2872
2873 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
2874 &hw_breakpoint_error, &hw_bp_error_explained_already);
2875 if (val)
2876 error_flag = val;
2877 }
2878
2879 /* If we failed to insert all locations of a watchpoint, remove
2880 them, as half-inserted watchpoint is of limited use. */
2881 ALL_BREAKPOINTS (bpt)
2882 {
2883 int some_failed = 0;
2884 struct bp_location *loc;
2885
2886 if (!is_hardware_watchpoint (bpt))
2887 continue;
2888
2889 if (!breakpoint_enabled (bpt))
2890 continue;
2891
2892 if (bpt->disposition == disp_del_at_next_stop)
2893 continue;
2894
2895 for (loc = bpt->loc; loc; loc = loc->next)
2896 if (!loc->inserted && should_be_inserted (loc))
2897 {
2898 some_failed = 1;
2899 break;
2900 }
2901 if (some_failed)
2902 {
2903 for (loc = bpt->loc; loc; loc = loc->next)
2904 if (loc->inserted)
2905 remove_breakpoint (loc, mark_uninserted);
2906
2907 hw_breakpoint_error = 1;
2908 fprintf_unfiltered (tmp_error_stream,
2909 "Could not insert hardware watchpoint %d.\n",
2910 bpt->number);
2911 error_flag = -1;
2912 }
2913 }
2914
2915 if (error_flag)
2916 {
2917 /* If a hardware breakpoint or watchpoint was inserted, add a
2918 message about possibly exhausted resources. */
2919 if (hw_breakpoint_error && !hw_bp_error_explained_already)
2920 {
2921 fprintf_unfiltered (tmp_error_stream,
2922 "Could not insert hardware breakpoints:\n\
2923 You may have requested too many hardware breakpoints/watchpoints.\n");
2924 }
2925 target_terminal_ours_for_output ();
2926 error_stream (tmp_error_stream);
2927 }
2928
2929 do_cleanups (cleanups);
2930 }
2931
2932 /* Used when the program stops.
2933 Returns zero if successful, or non-zero if there was a problem
2934 removing a breakpoint location. */
2935
2936 int
2937 remove_breakpoints (void)
2938 {
2939 struct bp_location *bl, **blp_tmp;
2940 int val = 0;
2941
2942 ALL_BP_LOCATIONS (bl, blp_tmp)
2943 {
2944 if (bl->inserted && !is_tracepoint (bl->owner))
2945 val |= remove_breakpoint (bl, mark_uninserted);
2946 }
2947 return val;
2948 }
2949
2950 /* Remove breakpoints of process PID. */
2951
2952 int
2953 remove_breakpoints_pid (int pid)
2954 {
2955 struct bp_location *bl, **blp_tmp;
2956 int val;
2957 struct inferior *inf = find_inferior_pid (pid);
2958
2959 ALL_BP_LOCATIONS (bl, blp_tmp)
2960 {
2961 if (bl->pspace != inf->pspace)
2962 continue;
2963
2964 if (bl->owner->type == bp_dprintf)
2965 continue;
2966
2967 if (bl->inserted)
2968 {
2969 val = remove_breakpoint (bl, mark_uninserted);
2970 if (val != 0)
2971 return val;
2972 }
2973 }
2974 return 0;
2975 }
2976
2977 int
2978 reattach_breakpoints (int pid)
2979 {
2980 struct cleanup *old_chain;
2981 struct bp_location *bl, **blp_tmp;
2982 int val;
2983 struct ui_file *tmp_error_stream;
2984 int dummy1 = 0, dummy2 = 0, dummy3 = 0;
2985 struct inferior *inf;
2986 struct thread_info *tp;
2987
2988 tp = any_live_thread_of_process (pid);
2989 if (tp == NULL)
2990 return 1;
2991
2992 inf = find_inferior_pid (pid);
2993 old_chain = save_inferior_ptid ();
2994
2995 inferior_ptid = tp->ptid;
2996
2997 tmp_error_stream = mem_fileopen ();
2998 make_cleanup_ui_file_delete (tmp_error_stream);
2999
3000 ALL_BP_LOCATIONS (bl, blp_tmp)
3001 {
3002 if (bl->pspace != inf->pspace)
3003 continue;
3004
3005 if (bl->inserted)
3006 {
3007 bl->inserted = 0;
3008 val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2, &dummy3);
3009 if (val != 0)
3010 {
3011 do_cleanups (old_chain);
3012 return val;
3013 }
3014 }
3015 }
3016 do_cleanups (old_chain);
3017 return 0;
3018 }
3019
3020 static int internal_breakpoint_number = -1;
3021
3022 /* Set the breakpoint number of B, depending on the value of INTERNAL.
3023 If INTERNAL is non-zero, the breakpoint number will be populated
3024 from internal_breakpoint_number and that variable decremented.
3025 Otherwise the breakpoint number will be populated from
3026 breakpoint_count and that value incremented. Internal breakpoints
3027 do not set the internal var bpnum. */
3028 static void
3029 set_breakpoint_number (int internal, struct breakpoint *b)
3030 {
3031 if (internal)
3032 b->number = internal_breakpoint_number--;
3033 else
3034 {
3035 set_breakpoint_count (breakpoint_count + 1);
3036 b->number = breakpoint_count;
3037 }
3038 }
3039
3040 static struct breakpoint *
3041 create_internal_breakpoint (struct gdbarch *gdbarch,
3042 CORE_ADDR address, enum bptype type,
3043 const struct breakpoint_ops *ops)
3044 {
3045 struct symtab_and_line sal;
3046 struct breakpoint *b;
3047
3048 init_sal (&sal); /* Initialize to zeroes. */
3049
3050 sal.pc = address;
3051 sal.section = find_pc_overlay (sal.pc);
3052 sal.pspace = current_program_space;
3053
3054 b = set_raw_breakpoint (gdbarch, sal, type, ops);
3055 b->number = internal_breakpoint_number--;
3056 b->disposition = disp_donttouch;
3057
3058 return b;
3059 }
3060
3061 static const char *const longjmp_names[] =
3062 {
3063 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3064 };
3065 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3066
3067 /* Per-objfile data private to breakpoint.c. */
3068 struct breakpoint_objfile_data
3069 {
3070 /* Minimal symbol for "_ovly_debug_event" (if any). */
3071 struct minimal_symbol *overlay_msym;
3072
3073 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3074 struct minimal_symbol *longjmp_msym[NUM_LONGJMP_NAMES];
3075
3076 /* True if we have looked for longjmp probes. */
3077 int longjmp_searched;
3078
3079 /* SystemTap probe points for longjmp (if any). */
3080 VEC (probe_p) *longjmp_probes;
3081
3082 /* Minimal symbol for "std::terminate()" (if any). */
3083 struct minimal_symbol *terminate_msym;
3084
3085 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3086 struct minimal_symbol *exception_msym;
3087
3088 /* True if we have looked for exception probes. */
3089 int exception_searched;
3090
3091 /* SystemTap probe points for unwinding (if any). */
3092 VEC (probe_p) *exception_probes;
3093 };
3094
3095 static const struct objfile_data *breakpoint_objfile_key;
3096
3097 /* Minimal symbol not found sentinel. */
3098 static struct minimal_symbol msym_not_found;
3099
3100 /* Returns TRUE if MSYM point to the "not found" sentinel. */
3101
3102 static int
3103 msym_not_found_p (const struct minimal_symbol *msym)
3104 {
3105 return msym == &msym_not_found;
3106 }
3107
3108 /* Return per-objfile data needed by breakpoint.c.
3109 Allocate the data if necessary. */
3110
3111 static struct breakpoint_objfile_data *
3112 get_breakpoint_objfile_data (struct objfile *objfile)
3113 {
3114 struct breakpoint_objfile_data *bp_objfile_data;
3115
3116 bp_objfile_data = objfile_data (objfile, breakpoint_objfile_key);
3117 if (bp_objfile_data == NULL)
3118 {
3119 bp_objfile_data = obstack_alloc (&objfile->objfile_obstack,
3120 sizeof (*bp_objfile_data));
3121
3122 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
3123 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3124 }
3125 return bp_objfile_data;
3126 }
3127
3128 static void
3129 free_breakpoint_probes (struct objfile *obj, void *data)
3130 {
3131 struct breakpoint_objfile_data *bp_objfile_data = data;
3132
3133 VEC_free (probe_p, bp_objfile_data->longjmp_probes);
3134 VEC_free (probe_p, bp_objfile_data->exception_probes);
3135 }
3136
3137 static void
3138 create_overlay_event_breakpoint (void)
3139 {
3140 struct objfile *objfile;
3141 const char *const func_name = "_ovly_debug_event";
3142
3143 ALL_OBJFILES (objfile)
3144 {
3145 struct breakpoint *b;
3146 struct breakpoint_objfile_data *bp_objfile_data;
3147 CORE_ADDR addr;
3148
3149 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3150
3151 if (msym_not_found_p (bp_objfile_data->overlay_msym))
3152 continue;
3153
3154 if (bp_objfile_data->overlay_msym == NULL)
3155 {
3156 struct minimal_symbol *m;
3157
3158 m = lookup_minimal_symbol_text (func_name, objfile);
3159 if (m == NULL)
3160 {
3161 /* Avoid future lookups in this objfile. */
3162 bp_objfile_data->overlay_msym = &msym_not_found;
3163 continue;
3164 }
3165 bp_objfile_data->overlay_msym = m;
3166 }
3167
3168 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
3169 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3170 bp_overlay_event,
3171 &internal_breakpoint_ops);
3172 b->addr_string = xstrdup (func_name);
3173
3174 if (overlay_debugging == ovly_auto)
3175 {
3176 b->enable_state = bp_enabled;
3177 overlay_events_enabled = 1;
3178 }
3179 else
3180 {
3181 b->enable_state = bp_disabled;
3182 overlay_events_enabled = 0;
3183 }
3184 }
3185 update_global_location_list (1);
3186 }
3187
3188 static void
3189 create_longjmp_master_breakpoint (void)
3190 {
3191 struct program_space *pspace;
3192 struct cleanup *old_chain;
3193
3194 old_chain = save_current_program_space ();
3195
3196 ALL_PSPACES (pspace)
3197 {
3198 struct objfile *objfile;
3199
3200 set_current_program_space (pspace);
3201
3202 ALL_OBJFILES (objfile)
3203 {
3204 int i;
3205 struct gdbarch *gdbarch;
3206 struct breakpoint_objfile_data *bp_objfile_data;
3207
3208 gdbarch = get_objfile_arch (objfile);
3209 if (!gdbarch_get_longjmp_target_p (gdbarch))
3210 continue;
3211
3212 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3213
3214 if (!bp_objfile_data->longjmp_searched)
3215 {
3216 bp_objfile_data->longjmp_probes
3217 = find_probes_in_objfile (objfile, "libc", "longjmp");
3218 bp_objfile_data->longjmp_searched = 1;
3219 }
3220
3221 if (bp_objfile_data->longjmp_probes != NULL)
3222 {
3223 int i;
3224 struct probe *probe;
3225 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3226
3227 for (i = 0;
3228 VEC_iterate (probe_p,
3229 bp_objfile_data->longjmp_probes,
3230 i, probe);
3231 ++i)
3232 {
3233 struct breakpoint *b;
3234
3235 b = create_internal_breakpoint (gdbarch, probe->address,
3236 bp_longjmp_master,
3237 &internal_breakpoint_ops);
3238 b->addr_string = xstrdup ("-probe-stap libc:longjmp");
3239 b->enable_state = bp_disabled;
3240 }
3241
3242 continue;
3243 }
3244
3245 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
3246 {
3247 struct breakpoint *b;
3248 const char *func_name;
3249 CORE_ADDR addr;
3250
3251 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i]))
3252 continue;
3253
3254 func_name = longjmp_names[i];
3255 if (bp_objfile_data->longjmp_msym[i] == NULL)
3256 {
3257 struct minimal_symbol *m;
3258
3259 m = lookup_minimal_symbol_text (func_name, objfile);
3260 if (m == NULL)
3261 {
3262 /* Prevent future lookups in this objfile. */
3263 bp_objfile_data->longjmp_msym[i] = &msym_not_found;
3264 continue;
3265 }
3266 bp_objfile_data->longjmp_msym[i] = m;
3267 }
3268
3269 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
3270 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3271 &internal_breakpoint_ops);
3272 b->addr_string = xstrdup (func_name);
3273 b->enable_state = bp_disabled;
3274 }
3275 }
3276 }
3277 update_global_location_list (1);
3278
3279 do_cleanups (old_chain);
3280 }
3281
3282 /* Create a master std::terminate breakpoint. */
3283 static void
3284 create_std_terminate_master_breakpoint (void)
3285 {
3286 struct program_space *pspace;
3287 struct cleanup *old_chain;
3288 const char *const func_name = "std::terminate()";
3289
3290 old_chain = save_current_program_space ();
3291
3292 ALL_PSPACES (pspace)
3293 {
3294 struct objfile *objfile;
3295 CORE_ADDR addr;
3296
3297 set_current_program_space (pspace);
3298
3299 ALL_OBJFILES (objfile)
3300 {
3301 struct breakpoint *b;
3302 struct breakpoint_objfile_data *bp_objfile_data;
3303
3304 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3305
3306 if (msym_not_found_p (bp_objfile_data->terminate_msym))
3307 continue;
3308
3309 if (bp_objfile_data->terminate_msym == NULL)
3310 {
3311 struct minimal_symbol *m;
3312
3313 m = lookup_minimal_symbol (func_name, NULL, objfile);
3314 if (m == NULL || (MSYMBOL_TYPE (m) != mst_text
3315 && MSYMBOL_TYPE (m) != mst_file_text))
3316 {
3317 /* Prevent future lookups in this objfile. */
3318 bp_objfile_data->terminate_msym = &msym_not_found;
3319 continue;
3320 }
3321 bp_objfile_data->terminate_msym = m;
3322 }
3323
3324 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3325 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3326 bp_std_terminate_master,
3327 &internal_breakpoint_ops);
3328 b->addr_string = xstrdup (func_name);
3329 b->enable_state = bp_disabled;
3330 }
3331 }
3332
3333 update_global_location_list (1);
3334
3335 do_cleanups (old_chain);
3336 }
3337
3338 /* Install a master breakpoint on the unwinder's debug hook. */
3339
3340 static void
3341 create_exception_master_breakpoint (void)
3342 {
3343 struct objfile *objfile;
3344 const char *const func_name = "_Unwind_DebugHook";
3345
3346 ALL_OBJFILES (objfile)
3347 {
3348 struct breakpoint *b;
3349 struct gdbarch *gdbarch;
3350 struct breakpoint_objfile_data *bp_objfile_data;
3351 CORE_ADDR addr;
3352
3353 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3354
3355 /* We prefer the SystemTap probe point if it exists. */
3356 if (!bp_objfile_data->exception_searched)
3357 {
3358 bp_objfile_data->exception_probes
3359 = find_probes_in_objfile (objfile, "libgcc", "unwind");
3360 bp_objfile_data->exception_searched = 1;
3361 }
3362
3363 if (bp_objfile_data->exception_probes != NULL)
3364 {
3365 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3366 int i;
3367 struct probe *probe;
3368
3369 for (i = 0;
3370 VEC_iterate (probe_p,
3371 bp_objfile_data->exception_probes,
3372 i, probe);
3373 ++i)
3374 {
3375 struct breakpoint *b;
3376
3377 b = create_internal_breakpoint (gdbarch, probe->address,
3378 bp_exception_master,
3379 &internal_breakpoint_ops);
3380 b->addr_string = xstrdup ("-probe-stap libgcc:unwind");
3381 b->enable_state = bp_disabled;
3382 }
3383
3384 continue;
3385 }
3386
3387 /* Otherwise, try the hook function. */
3388
3389 if (msym_not_found_p (bp_objfile_data->exception_msym))
3390 continue;
3391
3392 gdbarch = get_objfile_arch (objfile);
3393
3394 if (bp_objfile_data->exception_msym == NULL)
3395 {
3396 struct minimal_symbol *debug_hook;
3397
3398 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3399 if (debug_hook == NULL)
3400 {
3401 bp_objfile_data->exception_msym = &msym_not_found;
3402 continue;
3403 }
3404
3405 bp_objfile_data->exception_msym = debug_hook;
3406 }
3407
3408 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
3409 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3410 &current_target);
3411 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3412 &internal_breakpoint_ops);
3413 b->addr_string = xstrdup (func_name);
3414 b->enable_state = bp_disabled;
3415 }
3416
3417 update_global_location_list (1);
3418 }
3419
3420 void
3421 update_breakpoints_after_exec (void)
3422 {
3423 struct breakpoint *b, *b_tmp;
3424 struct bp_location *bploc, **bplocp_tmp;
3425
3426 /* We're about to delete breakpoints from GDB's lists. If the
3427 INSERTED flag is true, GDB will try to lift the breakpoints by
3428 writing the breakpoints' "shadow contents" back into memory. The
3429 "shadow contents" are NOT valid after an exec, so GDB should not
3430 do that. Instead, the target is responsible from marking
3431 breakpoints out as soon as it detects an exec. We don't do that
3432 here instead, because there may be other attempts to delete
3433 breakpoints after detecting an exec and before reaching here. */
3434 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
3435 if (bploc->pspace == current_program_space)
3436 gdb_assert (!bploc->inserted);
3437
3438 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3439 {
3440 if (b->pspace != current_program_space)
3441 continue;
3442
3443 /* Solib breakpoints must be explicitly reset after an exec(). */
3444 if (b->type == bp_shlib_event)
3445 {
3446 delete_breakpoint (b);
3447 continue;
3448 }
3449
3450 /* JIT breakpoints must be explicitly reset after an exec(). */
3451 if (b->type == bp_jit_event)
3452 {
3453 delete_breakpoint (b);
3454 continue;
3455 }
3456
3457 /* Thread event breakpoints must be set anew after an exec(),
3458 as must overlay event and longjmp master breakpoints. */
3459 if (b->type == bp_thread_event || b->type == bp_overlay_event
3460 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3461 || b->type == bp_exception_master)
3462 {
3463 delete_breakpoint (b);
3464 continue;
3465 }
3466
3467 /* Step-resume breakpoints are meaningless after an exec(). */
3468 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3469 {
3470 delete_breakpoint (b);
3471 continue;
3472 }
3473
3474 /* Longjmp and longjmp-resume breakpoints are also meaningless
3475 after an exec. */
3476 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3477 || b->type == bp_longjmp_call_dummy
3478 || b->type == bp_exception || b->type == bp_exception_resume)
3479 {
3480 delete_breakpoint (b);
3481 continue;
3482 }
3483
3484 if (b->type == bp_catchpoint)
3485 {
3486 /* For now, none of the bp_catchpoint breakpoints need to
3487 do anything at this point. In the future, if some of
3488 the catchpoints need to something, we will need to add
3489 a new method, and call this method from here. */
3490 continue;
3491 }
3492
3493 /* bp_finish is a special case. The only way we ought to be able
3494 to see one of these when an exec() has happened, is if the user
3495 caught a vfork, and then said "finish". Ordinarily a finish just
3496 carries them to the call-site of the current callee, by setting
3497 a temporary bp there and resuming. But in this case, the finish
3498 will carry them entirely through the vfork & exec.
3499
3500 We don't want to allow a bp_finish to remain inserted now. But
3501 we can't safely delete it, 'cause finish_command has a handle to
3502 the bp on a bpstat, and will later want to delete it. There's a
3503 chance (and I've seen it happen) that if we delete the bp_finish
3504 here, that its storage will get reused by the time finish_command
3505 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3506 We really must allow finish_command to delete a bp_finish.
3507
3508 In the absence of a general solution for the "how do we know
3509 it's safe to delete something others may have handles to?"
3510 problem, what we'll do here is just uninsert the bp_finish, and
3511 let finish_command delete it.
3512
3513 (We know the bp_finish is "doomed" in the sense that it's
3514 momentary, and will be deleted as soon as finish_command sees
3515 the inferior stopped. So it doesn't matter that the bp's
3516 address is probably bogus in the new a.out, unlike e.g., the
3517 solib breakpoints.) */
3518
3519 if (b->type == bp_finish)
3520 {
3521 continue;
3522 }
3523
3524 /* Without a symbolic address, we have little hope of the
3525 pre-exec() address meaning the same thing in the post-exec()
3526 a.out. */
3527 if (b->addr_string == NULL)
3528 {
3529 delete_breakpoint (b);
3530 continue;
3531 }
3532 }
3533 /* FIXME what about longjmp breakpoints? Re-create them here? */
3534 create_overlay_event_breakpoint ();
3535 create_longjmp_master_breakpoint ();
3536 create_std_terminate_master_breakpoint ();
3537 create_exception_master_breakpoint ();
3538 }
3539
3540 int
3541 detach_breakpoints (ptid_t ptid)
3542 {
3543 struct bp_location *bl, **blp_tmp;
3544 int val = 0;
3545 struct cleanup *old_chain = save_inferior_ptid ();
3546 struct inferior *inf = current_inferior ();
3547
3548 if (PIDGET (ptid) == PIDGET (inferior_ptid))
3549 error (_("Cannot detach breakpoints of inferior_ptid"));
3550
3551 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
3552 inferior_ptid = ptid;
3553 ALL_BP_LOCATIONS (bl, blp_tmp)
3554 {
3555 if (bl->pspace != inf->pspace)
3556 continue;
3557
3558 if (bl->inserted)
3559 val |= remove_breakpoint_1 (bl, mark_inserted);
3560 }
3561
3562 /* Detach single-step breakpoints as well. */
3563 detach_single_step_breakpoints ();
3564
3565 do_cleanups (old_chain);
3566 return val;
3567 }
3568
3569 /* Remove the breakpoint location BL from the current address space.
3570 Note that this is used to detach breakpoints from a child fork.
3571 When we get here, the child isn't in the inferior list, and neither
3572 do we have objects to represent its address space --- we should
3573 *not* look at bl->pspace->aspace here. */
3574
3575 static int
3576 remove_breakpoint_1 (struct bp_location *bl, insertion_state_t is)
3577 {
3578 int val;
3579
3580 /* BL is never in moribund_locations by our callers. */
3581 gdb_assert (bl->owner != NULL);
3582
3583 if (bl->owner->enable_state == bp_permanent)
3584 /* Permanent breakpoints cannot be inserted or removed. */
3585 return 0;
3586
3587 /* The type of none suggests that owner is actually deleted.
3588 This should not ever happen. */
3589 gdb_assert (bl->owner->type != bp_none);
3590
3591 if (bl->loc_type == bp_loc_software_breakpoint
3592 || bl->loc_type == bp_loc_hardware_breakpoint)
3593 {
3594 /* "Normal" instruction breakpoint: either the standard
3595 trap-instruction bp (bp_breakpoint), or a
3596 bp_hardware_breakpoint. */
3597
3598 /* First check to see if we have to handle an overlay. */
3599 if (overlay_debugging == ovly_off
3600 || bl->section == NULL
3601 || !(section_is_overlay (bl->section)))
3602 {
3603 /* No overlay handling: just remove the breakpoint. */
3604 val = bl->owner->ops->remove_location (bl);
3605 }
3606 else
3607 {
3608 /* This breakpoint is in an overlay section.
3609 Did we set a breakpoint at the LMA? */
3610 if (!overlay_events_enabled)
3611 {
3612 /* Yes -- overlay event support is not active, so we
3613 should have set a breakpoint at the LMA. Remove it.
3614 */
3615 /* Ignore any failures: if the LMA is in ROM, we will
3616 have already warned when we failed to insert it. */
3617 if (bl->loc_type == bp_loc_hardware_breakpoint)
3618 target_remove_hw_breakpoint (bl->gdbarch,
3619 &bl->overlay_target_info);
3620 else
3621 target_remove_breakpoint (bl->gdbarch,
3622 &bl->overlay_target_info);
3623 }
3624 /* Did we set a breakpoint at the VMA?
3625 If so, we will have marked the breakpoint 'inserted'. */
3626 if (bl->inserted)
3627 {
3628 /* Yes -- remove it. Previously we did not bother to
3629 remove the breakpoint if the section had been
3630 unmapped, but let's not rely on that being safe. We
3631 don't know what the overlay manager might do. */
3632
3633 /* However, we should remove *software* breakpoints only
3634 if the section is still mapped, or else we overwrite
3635 wrong code with the saved shadow contents. */
3636 if (bl->loc_type == bp_loc_hardware_breakpoint
3637 || section_is_mapped (bl->section))
3638 val = bl->owner->ops->remove_location (bl);
3639 else
3640 val = 0;
3641 }
3642 else
3643 {
3644 /* No -- not inserted, so no need to remove. No error. */
3645 val = 0;
3646 }
3647 }
3648
3649 /* In some cases, we might not be able to remove a breakpoint
3650 in a shared library that has already been removed, but we
3651 have not yet processed the shlib unload event. */
3652 if (val && solib_name_from_address (bl->pspace, bl->address))
3653 val = 0;
3654
3655 if (val)
3656 return val;
3657 bl->inserted = (is == mark_inserted);
3658 }
3659 else if (bl->loc_type == bp_loc_hardware_watchpoint)
3660 {
3661 gdb_assert (bl->owner->ops != NULL
3662 && bl->owner->ops->remove_location != NULL);
3663
3664 bl->inserted = (is == mark_inserted);
3665 bl->owner->ops->remove_location (bl);
3666
3667 /* Failure to remove any of the hardware watchpoints comes here. */
3668 if ((is == mark_uninserted) && (bl->inserted))
3669 warning (_("Could not remove hardware watchpoint %d."),
3670 bl->owner->number);
3671 }
3672 else if (bl->owner->type == bp_catchpoint
3673 && breakpoint_enabled (bl->owner)
3674 && !bl->duplicate)
3675 {
3676 gdb_assert (bl->owner->ops != NULL
3677 && bl->owner->ops->remove_location != NULL);
3678
3679 val = bl->owner->ops->remove_location (bl);
3680 if (val)
3681 return val;
3682
3683 bl->inserted = (is == mark_inserted);
3684 }
3685
3686 return 0;
3687 }
3688
3689 static int
3690 remove_breakpoint (struct bp_location *bl, insertion_state_t is)
3691 {
3692 int ret;
3693 struct cleanup *old_chain;
3694
3695 /* BL is never in moribund_locations by our callers. */
3696 gdb_assert (bl->owner != NULL);
3697
3698 if (bl->owner->enable_state == bp_permanent)
3699 /* Permanent breakpoints cannot be inserted or removed. */
3700 return 0;
3701
3702 /* The type of none suggests that owner is actually deleted.
3703 This should not ever happen. */
3704 gdb_assert (bl->owner->type != bp_none);
3705
3706 old_chain = save_current_space_and_thread ();
3707
3708 switch_to_program_space_and_thread (bl->pspace);
3709
3710 ret = remove_breakpoint_1 (bl, is);
3711
3712 do_cleanups (old_chain);
3713 return ret;
3714 }
3715
3716 /* Clear the "inserted" flag in all breakpoints. */
3717
3718 void
3719 mark_breakpoints_out (void)
3720 {
3721 struct bp_location *bl, **blp_tmp;
3722
3723 ALL_BP_LOCATIONS (bl, blp_tmp)
3724 if (bl->pspace == current_program_space)
3725 bl->inserted = 0;
3726 }
3727
3728 /* Clear the "inserted" flag in all breakpoints and delete any
3729 breakpoints which should go away between runs of the program.
3730
3731 Plus other such housekeeping that has to be done for breakpoints
3732 between runs.
3733
3734 Note: this function gets called at the end of a run (by
3735 generic_mourn_inferior) and when a run begins (by
3736 init_wait_for_inferior). */
3737
3738
3739
3740 void
3741 breakpoint_init_inferior (enum inf_context context)
3742 {
3743 struct breakpoint *b, *b_tmp;
3744 struct bp_location *bl, **blp_tmp;
3745 int ix;
3746 struct program_space *pspace = current_program_space;
3747
3748 /* If breakpoint locations are shared across processes, then there's
3749 nothing to do. */
3750 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
3751 return;
3752
3753 ALL_BP_LOCATIONS (bl, blp_tmp)
3754 {
3755 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
3756 if (bl->pspace == pspace
3757 && bl->owner->enable_state != bp_permanent)
3758 bl->inserted = 0;
3759 }
3760
3761 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3762 {
3763 if (b->loc && b->loc->pspace != pspace)
3764 continue;
3765
3766 switch (b->type)
3767 {
3768 case bp_call_dummy:
3769 case bp_longjmp_call_dummy:
3770
3771 /* If the call dummy breakpoint is at the entry point it will
3772 cause problems when the inferior is rerun, so we better get
3773 rid of it. */
3774
3775 case bp_watchpoint_scope:
3776
3777 /* Also get rid of scope breakpoints. */
3778
3779 case bp_shlib_event:
3780
3781 /* Also remove solib event breakpoints. Their addresses may
3782 have changed since the last time we ran the program.
3783 Actually we may now be debugging against different target;
3784 and so the solib backend that installed this breakpoint may
3785 not be used in by the target. E.g.,
3786
3787 (gdb) file prog-linux
3788 (gdb) run # native linux target
3789 ...
3790 (gdb) kill
3791 (gdb) file prog-win.exe
3792 (gdb) tar rem :9999 # remote Windows gdbserver.
3793 */
3794
3795 case bp_step_resume:
3796
3797 /* Also remove step-resume breakpoints. */
3798
3799 delete_breakpoint (b);
3800 break;
3801
3802 case bp_watchpoint:
3803 case bp_hardware_watchpoint:
3804 case bp_read_watchpoint:
3805 case bp_access_watchpoint:
3806 {
3807 struct watchpoint *w = (struct watchpoint *) b;
3808
3809 /* Likewise for watchpoints on local expressions. */
3810 if (w->exp_valid_block != NULL)
3811 delete_breakpoint (b);
3812 else if (context == inf_starting)
3813 {
3814 /* Reset val field to force reread of starting value in
3815 insert_breakpoints. */
3816 if (w->val)
3817 value_free (w->val);
3818 w->val = NULL;
3819 w->val_valid = 0;
3820 }
3821 }
3822 break;
3823 default:
3824 break;
3825 }
3826 }
3827
3828 /* Get rid of the moribund locations. */
3829 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
3830 decref_bp_location (&bl);
3831 VEC_free (bp_location_p, moribund_locations);
3832 }
3833
3834 /* These functions concern about actual breakpoints inserted in the
3835 target --- to e.g. check if we need to do decr_pc adjustment or if
3836 we need to hop over the bkpt --- so we check for address space
3837 match, not program space. */
3838
3839 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
3840 exists at PC. It returns ordinary_breakpoint_here if it's an
3841 ordinary breakpoint, or permanent_breakpoint_here if it's a
3842 permanent breakpoint.
3843 - When continuing from a location with an ordinary breakpoint, we
3844 actually single step once before calling insert_breakpoints.
3845 - When continuing from a location with a permanent breakpoint, we
3846 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
3847 the target, to advance the PC past the breakpoint. */
3848
3849 enum breakpoint_here
3850 breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
3851 {
3852 struct bp_location *bl, **blp_tmp;
3853 int any_breakpoint_here = 0;
3854
3855 ALL_BP_LOCATIONS (bl, blp_tmp)
3856 {
3857 if (bl->loc_type != bp_loc_software_breakpoint
3858 && bl->loc_type != bp_loc_hardware_breakpoint)
3859 continue;
3860
3861 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
3862 if ((breakpoint_enabled (bl->owner)
3863 || bl->owner->enable_state == bp_permanent)
3864 && breakpoint_location_address_match (bl, aspace, pc))
3865 {
3866 if (overlay_debugging
3867 && section_is_overlay (bl->section)
3868 && !section_is_mapped (bl->section))
3869 continue; /* unmapped overlay -- can't be a match */
3870 else if (bl->owner->enable_state == bp_permanent)
3871 return permanent_breakpoint_here;
3872 else
3873 any_breakpoint_here = 1;
3874 }
3875 }
3876
3877 return any_breakpoint_here ? ordinary_breakpoint_here : 0;
3878 }
3879
3880 /* Return true if there's a moribund breakpoint at PC. */
3881
3882 int
3883 moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
3884 {
3885 struct bp_location *loc;
3886 int ix;
3887
3888 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
3889 if (breakpoint_location_address_match (loc, aspace, pc))
3890 return 1;
3891
3892 return 0;
3893 }
3894
3895 /* Returns non-zero if there's a breakpoint inserted at PC, which is
3896 inserted using regular breakpoint_chain / bp_location array
3897 mechanism. This does not check for single-step breakpoints, which
3898 are inserted and removed using direct target manipulation. */
3899
3900 int
3901 regular_breakpoint_inserted_here_p (struct address_space *aspace,
3902 CORE_ADDR pc)
3903 {
3904 struct bp_location *bl, **blp_tmp;
3905
3906 ALL_BP_LOCATIONS (bl, blp_tmp)
3907 {
3908 if (bl->loc_type != bp_loc_software_breakpoint
3909 && bl->loc_type != bp_loc_hardware_breakpoint)
3910 continue;
3911
3912 if (bl->inserted
3913 && breakpoint_location_address_match (bl, aspace, pc))
3914 {
3915 if (overlay_debugging
3916 && section_is_overlay (bl->section)
3917 && !section_is_mapped (bl->section))
3918 continue; /* unmapped overlay -- can't be a match */
3919 else
3920 return 1;
3921 }
3922 }
3923 return 0;
3924 }
3925
3926 /* Returns non-zero iff there's either regular breakpoint
3927 or a single step breakpoint inserted at PC. */
3928
3929 int
3930 breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
3931 {
3932 if (regular_breakpoint_inserted_here_p (aspace, pc))
3933 return 1;
3934
3935 if (single_step_breakpoint_inserted_here_p (aspace, pc))
3936 return 1;
3937
3938 return 0;
3939 }
3940
3941 /* This function returns non-zero iff there is a software breakpoint
3942 inserted at PC. */
3943
3944 int
3945 software_breakpoint_inserted_here_p (struct address_space *aspace,
3946 CORE_ADDR pc)
3947 {
3948 struct bp_location *bl, **blp_tmp;
3949
3950 ALL_BP_LOCATIONS (bl, blp_tmp)
3951 {
3952 if (bl->loc_type != bp_loc_software_breakpoint)
3953 continue;
3954
3955 if (bl->inserted
3956 && breakpoint_address_match (bl->pspace->aspace, bl->address,
3957 aspace, pc))
3958 {
3959 if (overlay_debugging
3960 && section_is_overlay (bl->section)
3961 && !section_is_mapped (bl->section))
3962 continue; /* unmapped overlay -- can't be a match */
3963 else
3964 return 1;
3965 }
3966 }
3967
3968 /* Also check for software single-step breakpoints. */
3969 if (single_step_breakpoint_inserted_here_p (aspace, pc))
3970 return 1;
3971
3972 return 0;
3973 }
3974
3975 int
3976 hardware_watchpoint_inserted_in_range (struct address_space *aspace,
3977 CORE_ADDR addr, ULONGEST len)
3978 {
3979 struct breakpoint *bpt;
3980
3981 ALL_BREAKPOINTS (bpt)
3982 {
3983 struct bp_location *loc;
3984
3985 if (bpt->type != bp_hardware_watchpoint
3986 && bpt->type != bp_access_watchpoint)
3987 continue;
3988
3989 if (!breakpoint_enabled (bpt))
3990 continue;
3991
3992 for (loc = bpt->loc; loc; loc = loc->next)
3993 if (loc->pspace->aspace == aspace && loc->inserted)
3994 {
3995 CORE_ADDR l, h;
3996
3997 /* Check for intersection. */
3998 l = max (loc->address, addr);
3999 h = min (loc->address + loc->length, addr + len);
4000 if (l < h)
4001 return 1;
4002 }
4003 }
4004 return 0;
4005 }
4006
4007 /* breakpoint_thread_match (PC, PTID) returns true if the breakpoint at
4008 PC is valid for process/thread PTID. */
4009
4010 int
4011 breakpoint_thread_match (struct address_space *aspace, CORE_ADDR pc,
4012 ptid_t ptid)
4013 {
4014 struct bp_location *bl, **blp_tmp;
4015 /* The thread and task IDs associated to PTID, computed lazily. */
4016 int thread = -1;
4017 int task = 0;
4018
4019 ALL_BP_LOCATIONS (bl, blp_tmp)
4020 {
4021 if (bl->loc_type != bp_loc_software_breakpoint
4022 && bl->loc_type != bp_loc_hardware_breakpoint)
4023 continue;
4024
4025 /* ALL_BP_LOCATIONS bp_location has bl->OWNER always non-NULL. */
4026 if (!breakpoint_enabled (bl->owner)
4027 && bl->owner->enable_state != bp_permanent)
4028 continue;
4029
4030 if (!breakpoint_location_address_match (bl, aspace, pc))
4031 continue;
4032
4033 if (bl->owner->thread != -1)
4034 {
4035 /* This is a thread-specific breakpoint. Check that ptid
4036 matches that thread. If thread hasn't been computed yet,
4037 it is now time to do so. */
4038 if (thread == -1)
4039 thread = pid_to_thread_id (ptid);
4040 if (bl->owner->thread != thread)
4041 continue;
4042 }
4043
4044 if (bl->owner->task != 0)
4045 {
4046 /* This is a task-specific breakpoint. Check that ptid
4047 matches that task. If task hasn't been computed yet,
4048 it is now time to do so. */
4049 if (task == 0)
4050 task = ada_get_task_number (ptid);
4051 if (bl->owner->task != task)
4052 continue;
4053 }
4054
4055 if (overlay_debugging
4056 && section_is_overlay (bl->section)
4057 && !section_is_mapped (bl->section))
4058 continue; /* unmapped overlay -- can't be a match */
4059
4060 return 1;
4061 }
4062
4063 return 0;
4064 }
4065 \f
4066
4067 /* bpstat stuff. External routines' interfaces are documented
4068 in breakpoint.h. */
4069
4070 int
4071 is_catchpoint (struct breakpoint *ep)
4072 {
4073 return (ep->type == bp_catchpoint);
4074 }
4075
4076 /* Frees any storage that is part of a bpstat. Does not walk the
4077 'next' chain. */
4078
4079 static void
4080 bpstat_free (bpstat bs)
4081 {
4082 if (bs->old_val != NULL)
4083 value_free (bs->old_val);
4084 decref_counted_command_line (&bs->commands);
4085 decref_bp_location (&bs->bp_location_at);
4086 xfree (bs);
4087 }
4088
4089 /* Clear a bpstat so that it says we are not at any breakpoint.
4090 Also free any storage that is part of a bpstat. */
4091
4092 void
4093 bpstat_clear (bpstat *bsp)
4094 {
4095 bpstat p;
4096 bpstat q;
4097
4098 if (bsp == 0)
4099 return;
4100 p = *bsp;
4101 while (p != NULL)
4102 {
4103 q = p->next;
4104 bpstat_free (p);
4105 p = q;
4106 }
4107 *bsp = NULL;
4108 }
4109
4110 /* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4111 is part of the bpstat is copied as well. */
4112
4113 bpstat
4114 bpstat_copy (bpstat bs)
4115 {
4116 bpstat p = NULL;
4117 bpstat tmp;
4118 bpstat retval = NULL;
4119
4120 if (bs == NULL)
4121 return bs;
4122
4123 for (; bs != NULL; bs = bs->next)
4124 {
4125 tmp = (bpstat) xmalloc (sizeof (*tmp));
4126 memcpy (tmp, bs, sizeof (*tmp));
4127 incref_counted_command_line (tmp->commands);
4128 incref_bp_location (tmp->bp_location_at);
4129 if (bs->old_val != NULL)
4130 {
4131 tmp->old_val = value_copy (bs->old_val);
4132 release_value (tmp->old_val);
4133 }
4134
4135 if (p == NULL)
4136 /* This is the first thing in the chain. */
4137 retval = tmp;
4138 else
4139 p->next = tmp;
4140 p = tmp;
4141 }
4142 p->next = NULL;
4143 return retval;
4144 }
4145
4146 /* Find the bpstat associated with this breakpoint. */
4147
4148 bpstat
4149 bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
4150 {
4151 if (bsp == NULL)
4152 return NULL;
4153
4154 for (; bsp != NULL; bsp = bsp->next)
4155 {
4156 if (bsp->breakpoint_at == breakpoint)
4157 return bsp;
4158 }
4159 return NULL;
4160 }
4161
4162 /* See breakpoint.h. */
4163
4164 enum bpstat_signal_value
4165 bpstat_explains_signal (bpstat bsp)
4166 {
4167 enum bpstat_signal_value result = BPSTAT_SIGNAL_NO;
4168
4169 for (; bsp != NULL; bsp = bsp->next)
4170 {
4171 /* Ensure that, if we ever entered this loop, then we at least
4172 return BPSTAT_SIGNAL_HIDE. */
4173 enum bpstat_signal_value newval = BPSTAT_SIGNAL_HIDE;
4174
4175 if (bsp->breakpoint_at != NULL)
4176 newval = bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at);
4177
4178 if (newval > result)
4179 result = newval;
4180 }
4181
4182 return result;
4183 }
4184
4185 /* Put in *NUM the breakpoint number of the first breakpoint we are
4186 stopped at. *BSP upon return is a bpstat which points to the
4187 remaining breakpoints stopped at (but which is not guaranteed to be
4188 good for anything but further calls to bpstat_num).
4189
4190 Return 0 if passed a bpstat which does not indicate any breakpoints.
4191 Return -1 if stopped at a breakpoint that has been deleted since
4192 we set it.
4193 Return 1 otherwise. */
4194
4195 int
4196 bpstat_num (bpstat *bsp, int *num)
4197 {
4198 struct breakpoint *b;
4199
4200 if ((*bsp) == NULL)
4201 return 0; /* No more breakpoint values */
4202
4203 /* We assume we'll never have several bpstats that correspond to a
4204 single breakpoint -- otherwise, this function might return the
4205 same number more than once and this will look ugly. */
4206 b = (*bsp)->breakpoint_at;
4207 *bsp = (*bsp)->next;
4208 if (b == NULL)
4209 return -1; /* breakpoint that's been deleted since */
4210
4211 *num = b->number; /* We have its number */
4212 return 1;
4213 }
4214
4215 /* See breakpoint.h. */
4216
4217 void
4218 bpstat_clear_actions (void)
4219 {
4220 struct thread_info *tp;
4221 bpstat bs;
4222
4223 if (ptid_equal (inferior_ptid, null_ptid))
4224 return;
4225
4226 tp = find_thread_ptid (inferior_ptid);
4227 if (tp == NULL)
4228 return;
4229
4230 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
4231 {
4232 decref_counted_command_line (&bs->commands);
4233
4234 if (bs->old_val != NULL)
4235 {
4236 value_free (bs->old_val);
4237 bs->old_val = NULL;
4238 }
4239 }
4240 }
4241
4242 /* Called when a command is about to proceed the inferior. */
4243
4244 static void
4245 breakpoint_about_to_proceed (void)
4246 {
4247 if (!ptid_equal (inferior_ptid, null_ptid))
4248 {
4249 struct thread_info *tp = inferior_thread ();
4250
4251 /* Allow inferior function calls in breakpoint commands to not
4252 interrupt the command list. When the call finishes
4253 successfully, the inferior will be standing at the same
4254 breakpoint as if nothing happened. */
4255 if (tp->control.in_infcall)
4256 return;
4257 }
4258
4259 breakpoint_proceeded = 1;
4260 }
4261
4262 /* Stub for cleaning up our state if we error-out of a breakpoint
4263 command. */
4264 static void
4265 cleanup_executing_breakpoints (void *ignore)
4266 {
4267 executing_breakpoint_commands = 0;
4268 }
4269
4270 /* Return non-zero iff CMD as the first line of a command sequence is `silent'
4271 or its equivalent. */
4272
4273 static int
4274 command_line_is_silent (struct command_line *cmd)
4275 {
4276 return cmd && (strcmp ("silent", cmd->line) == 0
4277 || (xdb_commands && strcmp ("Q", cmd->line) == 0));
4278 }
4279
4280 /* Execute all the commands associated with all the breakpoints at
4281 this location. Any of these commands could cause the process to
4282 proceed beyond this point, etc. We look out for such changes by
4283 checking the global "breakpoint_proceeded" after each command.
4284
4285 Returns true if a breakpoint command resumed the inferior. In that
4286 case, it is the caller's responsibility to recall it again with the
4287 bpstat of the current thread. */
4288
4289 static int
4290 bpstat_do_actions_1 (bpstat *bsp)
4291 {
4292 bpstat bs;
4293 struct cleanup *old_chain;
4294 int again = 0;
4295
4296 /* Avoid endless recursion if a `source' command is contained
4297 in bs->commands. */
4298 if (executing_breakpoint_commands)
4299 return 0;
4300
4301 executing_breakpoint_commands = 1;
4302 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
4303
4304 prevent_dont_repeat ();
4305
4306 /* This pointer will iterate over the list of bpstat's. */
4307 bs = *bsp;
4308
4309 breakpoint_proceeded = 0;
4310 for (; bs != NULL; bs = bs->next)
4311 {
4312 struct counted_command_line *ccmd;
4313 struct command_line *cmd;
4314 struct cleanup *this_cmd_tree_chain;
4315
4316 /* Take ownership of the BSP's command tree, if it has one.
4317
4318 The command tree could legitimately contain commands like
4319 'step' and 'next', which call clear_proceed_status, which
4320 frees stop_bpstat's command tree. To make sure this doesn't
4321 free the tree we're executing out from under us, we need to
4322 take ownership of the tree ourselves. Since a given bpstat's
4323 commands are only executed once, we don't need to copy it; we
4324 can clear the pointer in the bpstat, and make sure we free
4325 the tree when we're done. */
4326 ccmd = bs->commands;
4327 bs->commands = NULL;
4328 this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
4329 cmd = ccmd ? ccmd->commands : NULL;
4330 if (command_line_is_silent (cmd))
4331 {
4332 /* The action has been already done by bpstat_stop_status. */
4333 cmd = cmd->next;
4334 }
4335
4336 while (cmd != NULL)
4337 {
4338 execute_control_command (cmd);
4339
4340 if (breakpoint_proceeded)
4341 break;
4342 else
4343 cmd = cmd->next;
4344 }
4345
4346 /* We can free this command tree now. */
4347 do_cleanups (this_cmd_tree_chain);
4348
4349 if (breakpoint_proceeded)
4350 {
4351 if (target_can_async_p ())
4352 /* If we are in async mode, then the target might be still
4353 running, not stopped at any breakpoint, so nothing for
4354 us to do here -- just return to the event loop. */
4355 ;
4356 else
4357 /* In sync mode, when execute_control_command returns
4358 we're already standing on the next breakpoint.
4359 Breakpoint commands for that stop were not run, since
4360 execute_command does not run breakpoint commands --
4361 only command_line_handler does, but that one is not
4362 involved in execution of breakpoint commands. So, we
4363 can now execute breakpoint commands. It should be
4364 noted that making execute_command do bpstat actions is
4365 not an option -- in this case we'll have recursive
4366 invocation of bpstat for each breakpoint with a
4367 command, and can easily blow up GDB stack. Instead, we
4368 return true, which will trigger the caller to recall us
4369 with the new stop_bpstat. */
4370 again = 1;
4371 break;
4372 }
4373 }
4374 do_cleanups (old_chain);
4375 return again;
4376 }
4377
4378 void
4379 bpstat_do_actions (void)
4380 {
4381 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4382
4383 /* Do any commands attached to breakpoint we are stopped at. */
4384 while (!ptid_equal (inferior_ptid, null_ptid)
4385 && target_has_execution
4386 && !is_exited (inferior_ptid)
4387 && !is_executing (inferior_ptid))
4388 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4389 and only return when it is stopped at the next breakpoint, we
4390 keep doing breakpoint actions until it returns false to
4391 indicate the inferior was not resumed. */
4392 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
4393 break;
4394
4395 discard_cleanups (cleanup_if_error);
4396 }
4397
4398 /* Print out the (old or new) value associated with a watchpoint. */
4399
4400 static void
4401 watchpoint_value_print (struct value *val, struct ui_file *stream)
4402 {
4403 if (val == NULL)
4404 fprintf_unfiltered (stream, _("<unreadable>"));
4405 else
4406 {
4407 struct value_print_options opts;
4408 get_user_print_options (&opts);
4409 value_print (val, stream, &opts);
4410 }
4411 }
4412
4413 /* Generic routine for printing messages indicating why we
4414 stopped. The behavior of this function depends on the value
4415 'print_it' in the bpstat structure. Under some circumstances we
4416 may decide not to print anything here and delegate the task to
4417 normal_stop(). */
4418
4419 static enum print_stop_action
4420 print_bp_stop_message (bpstat bs)
4421 {
4422 switch (bs->print_it)
4423 {
4424 case print_it_noop:
4425 /* Nothing should be printed for this bpstat entry. */
4426 return PRINT_UNKNOWN;
4427 break;
4428
4429 case print_it_done:
4430 /* We still want to print the frame, but we already printed the
4431 relevant messages. */
4432 return PRINT_SRC_AND_LOC;
4433 break;
4434
4435 case print_it_normal:
4436 {
4437 struct breakpoint *b = bs->breakpoint_at;
4438
4439 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4440 which has since been deleted. */
4441 if (b == NULL)
4442 return PRINT_UNKNOWN;
4443
4444 /* Normal case. Call the breakpoint's print_it method. */
4445 return b->ops->print_it (bs);
4446 }
4447 break;
4448
4449 default:
4450 internal_error (__FILE__, __LINE__,
4451 _("print_bp_stop_message: unrecognized enum value"));
4452 break;
4453 }
4454 }
4455
4456 /* A helper function that prints a shared library stopped event. */
4457
4458 static void
4459 print_solib_event (int is_catchpoint)
4460 {
4461 int any_deleted
4462 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4463 int any_added
4464 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4465
4466 if (!is_catchpoint)
4467 {
4468 if (any_added || any_deleted)
4469 ui_out_text (current_uiout,
4470 _("Stopped due to shared library event:\n"));
4471 else
4472 ui_out_text (current_uiout,
4473 _("Stopped due to shared library event (no "
4474 "libraries added or removed)\n"));
4475 }
4476
4477 if (ui_out_is_mi_like_p (current_uiout))
4478 ui_out_field_string (current_uiout, "reason",
4479 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4480
4481 if (any_deleted)
4482 {
4483 struct cleanup *cleanup;
4484 char *name;
4485 int ix;
4486
4487 ui_out_text (current_uiout, _(" Inferior unloaded "));
4488 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4489 "removed");
4490 for (ix = 0;
4491 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4492 ix, name);
4493 ++ix)
4494 {
4495 if (ix > 0)
4496 ui_out_text (current_uiout, " ");
4497 ui_out_field_string (current_uiout, "library", name);
4498 ui_out_text (current_uiout, "\n");
4499 }
4500
4501 do_cleanups (cleanup);
4502 }
4503
4504 if (any_added)
4505 {
4506 struct so_list *iter;
4507 int ix;
4508 struct cleanup *cleanup;
4509
4510 ui_out_text (current_uiout, _(" Inferior loaded "));
4511 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4512 "added");
4513 for (ix = 0;
4514 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4515 ix, iter);
4516 ++ix)
4517 {
4518 if (ix > 0)
4519 ui_out_text (current_uiout, " ");
4520 ui_out_field_string (current_uiout, "library", iter->so_name);
4521 ui_out_text (current_uiout, "\n");
4522 }
4523
4524 do_cleanups (cleanup);
4525 }
4526 }
4527
4528 /* Print a message indicating what happened. This is called from
4529 normal_stop(). The input to this routine is the head of the bpstat
4530 list - a list of the eventpoints that caused this stop. KIND is
4531 the target_waitkind for the stopping event. This
4532 routine calls the generic print routine for printing a message
4533 about reasons for stopping. This will print (for example) the
4534 "Breakpoint n," part of the output. The return value of this
4535 routine is one of:
4536
4537 PRINT_UNKNOWN: Means we printed nothing.
4538 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4539 code to print the location. An example is
4540 "Breakpoint 1, " which should be followed by
4541 the location.
4542 PRINT_SRC_ONLY: Means we printed something, but there is no need
4543 to also print the location part of the message.
4544 An example is the catch/throw messages, which
4545 don't require a location appended to the end.
4546 PRINT_NOTHING: We have done some printing and we don't need any
4547 further info to be printed. */
4548
4549 enum print_stop_action
4550 bpstat_print (bpstat bs, int kind)
4551 {
4552 int val;
4553
4554 /* Maybe another breakpoint in the chain caused us to stop.
4555 (Currently all watchpoints go on the bpstat whether hit or not.
4556 That probably could (should) be changed, provided care is taken
4557 with respect to bpstat_explains_signal). */
4558 for (; bs; bs = bs->next)
4559 {
4560 val = print_bp_stop_message (bs);
4561 if (val == PRINT_SRC_ONLY
4562 || val == PRINT_SRC_AND_LOC
4563 || val == PRINT_NOTHING)
4564 return val;
4565 }
4566
4567 /* If we had hit a shared library event breakpoint,
4568 print_bp_stop_message would print out this message. If we hit an
4569 OS-level shared library event, do the same thing. */
4570 if (kind == TARGET_WAITKIND_LOADED)
4571 {
4572 print_solib_event (0);
4573 return PRINT_NOTHING;
4574 }
4575
4576 /* We reached the end of the chain, or we got a null BS to start
4577 with and nothing was printed. */
4578 return PRINT_UNKNOWN;
4579 }
4580
4581 /* Evaluate the expression EXP and return 1 if value is zero. This is
4582 used inside a catch_errors to evaluate the breakpoint condition.
4583 The argument is a "struct expression *" that has been cast to a
4584 "char *" to make it pass through catch_errors. */
4585
4586 static int
4587 breakpoint_cond_eval (void *exp)
4588 {
4589 struct value *mark = value_mark ();
4590 int i = !value_true (evaluate_expression ((struct expression *) exp));
4591
4592 value_free_to_mark (mark);
4593 return i;
4594 }
4595
4596 /* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
4597
4598 static bpstat
4599 bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
4600 {
4601 bpstat bs;
4602
4603 bs = (bpstat) xmalloc (sizeof (*bs));
4604 bs->next = NULL;
4605 **bs_link_pointer = bs;
4606 *bs_link_pointer = &bs->next;
4607 bs->breakpoint_at = bl->owner;
4608 bs->bp_location_at = bl;
4609 incref_bp_location (bl);
4610 /* If the condition is false, etc., don't do the commands. */
4611 bs->commands = NULL;
4612 bs->old_val = NULL;
4613 bs->print_it = print_it_normal;
4614 return bs;
4615 }
4616 \f
4617 /* The target has stopped with waitstatus WS. Check if any hardware
4618 watchpoints have triggered, according to the target. */
4619
4620 int
4621 watchpoints_triggered (struct target_waitstatus *ws)
4622 {
4623 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
4624 CORE_ADDR addr;
4625 struct breakpoint *b;
4626
4627 if (!stopped_by_watchpoint)
4628 {
4629 /* We were not stopped by a watchpoint. Mark all watchpoints
4630 as not triggered. */
4631 ALL_BREAKPOINTS (b)
4632 if (is_hardware_watchpoint (b))
4633 {
4634 struct watchpoint *w = (struct watchpoint *) b;
4635
4636 w->watchpoint_triggered = watch_triggered_no;
4637 }
4638
4639 return 0;
4640 }
4641
4642 if (!target_stopped_data_address (&current_target, &addr))
4643 {
4644 /* We were stopped by a watchpoint, but we don't know where.
4645 Mark all watchpoints as unknown. */
4646 ALL_BREAKPOINTS (b)
4647 if (is_hardware_watchpoint (b))
4648 {
4649 struct watchpoint *w = (struct watchpoint *) b;
4650
4651 w->watchpoint_triggered = watch_triggered_unknown;
4652 }
4653
4654 return stopped_by_watchpoint;
4655 }
4656
4657 /* The target could report the data address. Mark watchpoints
4658 affected by this data address as triggered, and all others as not
4659 triggered. */
4660
4661 ALL_BREAKPOINTS (b)
4662 if (is_hardware_watchpoint (b))
4663 {
4664 struct watchpoint *w = (struct watchpoint *) b;
4665 struct bp_location *loc;
4666
4667 w->watchpoint_triggered = watch_triggered_no;
4668 for (loc = b->loc; loc; loc = loc->next)
4669 {
4670 if (is_masked_watchpoint (b))
4671 {
4672 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4673 CORE_ADDR start = loc->address & w->hw_wp_mask;
4674
4675 if (newaddr == start)
4676 {
4677 w->watchpoint_triggered = watch_triggered_yes;
4678 break;
4679 }
4680 }
4681 /* Exact match not required. Within range is sufficient. */
4682 else if (target_watchpoint_addr_within_range (&current_target,
4683 addr, loc->address,
4684 loc->length))
4685 {
4686 w->watchpoint_triggered = watch_triggered_yes;
4687 break;
4688 }
4689 }
4690 }
4691
4692 return 1;
4693 }
4694
4695 /* Possible return values for watchpoint_check (this can't be an enum
4696 because of check_errors). */
4697 /* The watchpoint has been deleted. */
4698 #define WP_DELETED 1
4699 /* The value has changed. */
4700 #define WP_VALUE_CHANGED 2
4701 /* The value has not changed. */
4702 #define WP_VALUE_NOT_CHANGED 3
4703 /* Ignore this watchpoint, no matter if the value changed or not. */
4704 #define WP_IGNORE 4
4705
4706 #define BP_TEMPFLAG 1
4707 #define BP_HARDWAREFLAG 2
4708
4709 /* Evaluate watchpoint condition expression and check if its value
4710 changed.
4711
4712 P should be a pointer to struct bpstat, but is defined as a void *
4713 in order for this function to be usable with catch_errors. */
4714
4715 static int
4716 watchpoint_check (void *p)
4717 {
4718 bpstat bs = (bpstat) p;
4719 struct watchpoint *b;
4720 struct frame_info *fr;
4721 int within_current_scope;
4722
4723 /* BS is built from an existing struct breakpoint. */
4724 gdb_assert (bs->breakpoint_at != NULL);
4725 b = (struct watchpoint *) bs->breakpoint_at;
4726
4727 /* If this is a local watchpoint, we only want to check if the
4728 watchpoint frame is in scope if the current thread is the thread
4729 that was used to create the watchpoint. */
4730 if (!watchpoint_in_thread_scope (b))
4731 return WP_IGNORE;
4732
4733 if (b->exp_valid_block == NULL)
4734 within_current_scope = 1;
4735 else
4736 {
4737 struct frame_info *frame = get_current_frame ();
4738 struct gdbarch *frame_arch = get_frame_arch (frame);
4739 CORE_ADDR frame_pc = get_frame_pc (frame);
4740
4741 /* in_function_epilogue_p() returns a non-zero value if we're
4742 still in the function but the stack frame has already been
4743 invalidated. Since we can't rely on the values of local
4744 variables after the stack has been destroyed, we are treating
4745 the watchpoint in that state as `not changed' without further
4746 checking. Don't mark watchpoints as changed if the current
4747 frame is in an epilogue - even if they are in some other
4748 frame, our view of the stack is likely to be wrong and
4749 frame_find_by_id could error out. */
4750 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
4751 return WP_IGNORE;
4752
4753 fr = frame_find_by_id (b->watchpoint_frame);
4754 within_current_scope = (fr != NULL);
4755
4756 /* If we've gotten confused in the unwinder, we might have
4757 returned a frame that can't describe this variable. */
4758 if (within_current_scope)
4759 {
4760 struct symbol *function;
4761
4762 function = get_frame_function (fr);
4763 if (function == NULL
4764 || !contained_in (b->exp_valid_block,
4765 SYMBOL_BLOCK_VALUE (function)))
4766 within_current_scope = 0;
4767 }
4768
4769 if (within_current_scope)
4770 /* If we end up stopping, the current frame will get selected
4771 in normal_stop. So this call to select_frame won't affect
4772 the user. */
4773 select_frame (fr);
4774 }
4775
4776 if (within_current_scope)
4777 {
4778 /* We use value_{,free_to_}mark because it could be a *long*
4779 time before we return to the command level and call
4780 free_all_values. We can't call free_all_values because we
4781 might be in the middle of evaluating a function call. */
4782
4783 int pc = 0;
4784 struct value *mark;
4785 struct value *new_val;
4786
4787 if (is_masked_watchpoint (&b->base))
4788 /* Since we don't know the exact trigger address (from
4789 stopped_data_address), just tell the user we've triggered
4790 a mask watchpoint. */
4791 return WP_VALUE_CHANGED;
4792
4793 mark = value_mark ();
4794 fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL);
4795
4796 /* We use value_equal_contents instead of value_equal because
4797 the latter coerces an array to a pointer, thus comparing just
4798 the address of the array instead of its contents. This is
4799 not what we want. */
4800 if ((b->val != NULL) != (new_val != NULL)
4801 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
4802 {
4803 if (new_val != NULL)
4804 {
4805 release_value (new_val);
4806 value_free_to_mark (mark);
4807 }
4808 bs->old_val = b->val;
4809 b->val = new_val;
4810 b->val_valid = 1;
4811 return WP_VALUE_CHANGED;
4812 }
4813 else
4814 {
4815 /* Nothing changed. */
4816 value_free_to_mark (mark);
4817 return WP_VALUE_NOT_CHANGED;
4818 }
4819 }
4820 else
4821 {
4822 struct ui_out *uiout = current_uiout;
4823
4824 /* This seems like the only logical thing to do because
4825 if we temporarily ignored the watchpoint, then when
4826 we reenter the block in which it is valid it contains
4827 garbage (in the case of a function, it may have two
4828 garbage values, one before and one after the prologue).
4829 So we can't even detect the first assignment to it and
4830 watch after that (since the garbage may or may not equal
4831 the first value assigned). */
4832 /* We print all the stop information in
4833 breakpoint_ops->print_it, but in this case, by the time we
4834 call breakpoint_ops->print_it this bp will be deleted
4835 already. So we have no choice but print the information
4836 here. */
4837 if (ui_out_is_mi_like_p (uiout))
4838 ui_out_field_string
4839 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
4840 ui_out_text (uiout, "\nWatchpoint ");
4841 ui_out_field_int (uiout, "wpnum", b->base.number);
4842 ui_out_text (uiout,
4843 " deleted because the program has left the block in\n\
4844 which its expression is valid.\n");
4845
4846 /* Make sure the watchpoint's commands aren't executed. */
4847 decref_counted_command_line (&b->base.commands);
4848 watchpoint_del_at_next_stop (b);
4849
4850 return WP_DELETED;
4851 }
4852 }
4853
4854 /* Return true if it looks like target has stopped due to hitting
4855 breakpoint location BL. This function does not check if we should
4856 stop, only if BL explains the stop. */
4857
4858 static int
4859 bpstat_check_location (const struct bp_location *bl,
4860 struct address_space *aspace, CORE_ADDR bp_addr,
4861 const struct target_waitstatus *ws)
4862 {
4863 struct breakpoint *b = bl->owner;
4864
4865 /* BL is from an existing breakpoint. */
4866 gdb_assert (b != NULL);
4867
4868 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
4869 }
4870
4871 /* Determine if the watched values have actually changed, and we
4872 should stop. If not, set BS->stop to 0. */
4873
4874 static void
4875 bpstat_check_watchpoint (bpstat bs)
4876 {
4877 const struct bp_location *bl;
4878 struct watchpoint *b;
4879
4880 /* BS is built for existing struct breakpoint. */
4881 bl = bs->bp_location_at;
4882 gdb_assert (bl != NULL);
4883 b = (struct watchpoint *) bs->breakpoint_at;
4884 gdb_assert (b != NULL);
4885
4886 {
4887 int must_check_value = 0;
4888
4889 if (b->base.type == bp_watchpoint)
4890 /* For a software watchpoint, we must always check the
4891 watched value. */
4892 must_check_value = 1;
4893 else if (b->watchpoint_triggered == watch_triggered_yes)
4894 /* We have a hardware watchpoint (read, write, or access)
4895 and the target earlier reported an address watched by
4896 this watchpoint. */
4897 must_check_value = 1;
4898 else if (b->watchpoint_triggered == watch_triggered_unknown
4899 && b->base.type == bp_hardware_watchpoint)
4900 /* We were stopped by a hardware watchpoint, but the target could
4901 not report the data address. We must check the watchpoint's
4902 value. Access and read watchpoints are out of luck; without
4903 a data address, we can't figure it out. */
4904 must_check_value = 1;
4905
4906 if (must_check_value)
4907 {
4908 char *message
4909 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
4910 b->base.number);
4911 struct cleanup *cleanups = make_cleanup (xfree, message);
4912 int e = catch_errors (watchpoint_check, bs, message,
4913 RETURN_MASK_ALL);
4914 do_cleanups (cleanups);
4915 switch (e)
4916 {
4917 case WP_DELETED:
4918 /* We've already printed what needs to be printed. */
4919 bs->print_it = print_it_done;
4920 /* Stop. */
4921 break;
4922 case WP_IGNORE:
4923 bs->print_it = print_it_noop;
4924 bs->stop = 0;
4925 break;
4926 case WP_VALUE_CHANGED:
4927 if (b->base.type == bp_read_watchpoint)
4928 {
4929 /* There are two cases to consider here:
4930
4931 1. We're watching the triggered memory for reads.
4932 In that case, trust the target, and always report
4933 the watchpoint hit to the user. Even though
4934 reads don't cause value changes, the value may
4935 have changed since the last time it was read, and
4936 since we're not trapping writes, we will not see
4937 those, and as such we should ignore our notion of
4938 old value.
4939
4940 2. We're watching the triggered memory for both
4941 reads and writes. There are two ways this may
4942 happen:
4943
4944 2.1. This is a target that can't break on data
4945 reads only, but can break on accesses (reads or
4946 writes), such as e.g., x86. We detect this case
4947 at the time we try to insert read watchpoints.
4948
4949 2.2. Otherwise, the target supports read
4950 watchpoints, but, the user set an access or write
4951 watchpoint watching the same memory as this read
4952 watchpoint.
4953
4954 If we're watching memory writes as well as reads,
4955 ignore watchpoint hits when we find that the
4956 value hasn't changed, as reads don't cause
4957 changes. This still gives false positives when
4958 the program writes the same value to memory as
4959 what there was already in memory (we will confuse
4960 it for a read), but it's much better than
4961 nothing. */
4962
4963 int other_write_watchpoint = 0;
4964
4965 if (bl->watchpoint_type == hw_read)
4966 {
4967 struct breakpoint *other_b;
4968
4969 ALL_BREAKPOINTS (other_b)
4970 if (other_b->type == bp_hardware_watchpoint
4971 || other_b->type == bp_access_watchpoint)
4972 {
4973 struct watchpoint *other_w =
4974 (struct watchpoint *) other_b;
4975
4976 if (other_w->watchpoint_triggered
4977 == watch_triggered_yes)
4978 {
4979 other_write_watchpoint = 1;
4980 break;
4981 }
4982 }
4983 }
4984
4985 if (other_write_watchpoint
4986 || bl->watchpoint_type == hw_access)
4987 {
4988 /* We're watching the same memory for writes,
4989 and the value changed since the last time we
4990 updated it, so this trap must be for a write.
4991 Ignore it. */
4992 bs->print_it = print_it_noop;
4993 bs->stop = 0;
4994 }
4995 }
4996 break;
4997 case WP_VALUE_NOT_CHANGED:
4998 if (b->base.type == bp_hardware_watchpoint
4999 || b->base.type == bp_watchpoint)
5000 {
5001 /* Don't stop: write watchpoints shouldn't fire if
5002 the value hasn't changed. */
5003 bs->print_it = print_it_noop;
5004 bs->stop = 0;
5005 }
5006 /* Stop. */
5007 break;
5008 default:
5009 /* Can't happen. */
5010 case 0:
5011 /* Error from catch_errors. */
5012 printf_filtered (_("Watchpoint %d deleted.\n"), b->base.number);
5013 watchpoint_del_at_next_stop (b);
5014 /* We've already printed what needs to be printed. */
5015 bs->print_it = print_it_done;
5016 break;
5017 }
5018 }
5019 else /* must_check_value == 0 */
5020 {
5021 /* This is a case where some watchpoint(s) triggered, but
5022 not at the address of this watchpoint, or else no
5023 watchpoint triggered after all. So don't print
5024 anything for this watchpoint. */
5025 bs->print_it = print_it_noop;
5026 bs->stop = 0;
5027 }
5028 }
5029 }
5030
5031
5032 /* Check conditions (condition proper, frame, thread and ignore count)
5033 of breakpoint referred to by BS. If we should not stop for this
5034 breakpoint, set BS->stop to 0. */
5035
5036 static void
5037 bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5038 {
5039 int thread_id = pid_to_thread_id (ptid);
5040 const struct bp_location *bl;
5041 struct breakpoint *b;
5042
5043 /* BS is built for existing struct breakpoint. */
5044 bl = bs->bp_location_at;
5045 gdb_assert (bl != NULL);
5046 b = bs->breakpoint_at;
5047 gdb_assert (b != NULL);
5048
5049 /* Even if the target evaluated the condition on its end and notified GDB, we
5050 need to do so again since GDB does not know if we stopped due to a
5051 breakpoint or a single step breakpoint. */
5052
5053 if (frame_id_p (b->frame_id)
5054 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
5055 bs->stop = 0;
5056 else if (bs->stop)
5057 {
5058 int value_is_zero = 0;
5059 struct expression *cond;
5060
5061 /* Evaluate Python breakpoints that have a "stop"
5062 method implemented. */
5063 if (b->py_bp_object)
5064 bs->stop = gdbpy_should_stop (b->py_bp_object);
5065
5066 if (is_watchpoint (b))
5067 {
5068 struct watchpoint *w = (struct watchpoint *) b;
5069
5070 cond = w->cond_exp;
5071 }
5072 else
5073 cond = bl->cond;
5074
5075 if (cond && b->disposition != disp_del_at_next_stop)
5076 {
5077 int within_current_scope = 1;
5078 struct watchpoint * w;
5079
5080 /* We use value_mark and value_free_to_mark because it could
5081 be a long time before we return to the command level and
5082 call free_all_values. We can't call free_all_values
5083 because we might be in the middle of evaluating a
5084 function call. */
5085 struct value *mark = value_mark ();
5086
5087 if (is_watchpoint (b))
5088 w = (struct watchpoint *) b;
5089 else
5090 w = NULL;
5091
5092 /* Need to select the frame, with all that implies so that
5093 the conditions will have the right context. Because we
5094 use the frame, we will not see an inlined function's
5095 variables when we arrive at a breakpoint at the start
5096 of the inlined function; the current frame will be the
5097 call site. */
5098 if (w == NULL || w->cond_exp_valid_block == NULL)
5099 select_frame (get_current_frame ());
5100 else
5101 {
5102 struct frame_info *frame;
5103
5104 /* For local watchpoint expressions, which particular
5105 instance of a local is being watched matters, so we
5106 keep track of the frame to evaluate the expression
5107 in. To evaluate the condition however, it doesn't
5108 really matter which instantiation of the function
5109 where the condition makes sense triggers the
5110 watchpoint. This allows an expression like "watch
5111 global if q > 10" set in `func', catch writes to
5112 global on all threads that call `func', or catch
5113 writes on all recursive calls of `func' by a single
5114 thread. We simply always evaluate the condition in
5115 the innermost frame that's executing where it makes
5116 sense to evaluate the condition. It seems
5117 intuitive. */
5118 frame = block_innermost_frame (w->cond_exp_valid_block);
5119 if (frame != NULL)
5120 select_frame (frame);
5121 else
5122 within_current_scope = 0;
5123 }
5124 if (within_current_scope)
5125 value_is_zero
5126 = catch_errors (breakpoint_cond_eval, cond,
5127 "Error in testing breakpoint condition:\n",
5128 RETURN_MASK_ALL);
5129 else
5130 {
5131 warning (_("Watchpoint condition cannot be tested "
5132 "in the current scope"));
5133 /* If we failed to set the right context for this
5134 watchpoint, unconditionally report it. */
5135 value_is_zero = 0;
5136 }
5137 /* FIXME-someday, should give breakpoint #. */
5138 value_free_to_mark (mark);
5139 }
5140
5141 if (cond && value_is_zero)
5142 {
5143 bs->stop = 0;
5144 }
5145 else if (b->thread != -1 && b->thread != thread_id)
5146 {
5147 bs->stop = 0;
5148 }
5149 else if (b->ignore_count > 0)
5150 {
5151 b->ignore_count--;
5152 bs->stop = 0;
5153 /* Increase the hit count even though we don't stop. */
5154 ++(b->hit_count);
5155 observer_notify_breakpoint_modified (b);
5156 }
5157 }
5158 }
5159
5160
5161 /* Get a bpstat associated with having just stopped at address
5162 BP_ADDR in thread PTID.
5163
5164 Determine whether we stopped at a breakpoint, etc, or whether we
5165 don't understand this stop. Result is a chain of bpstat's such
5166 that:
5167
5168 if we don't understand the stop, the result is a null pointer.
5169
5170 if we understand why we stopped, the result is not null.
5171
5172 Each element of the chain refers to a particular breakpoint or
5173 watchpoint at which we have stopped. (We may have stopped for
5174 several reasons concurrently.)
5175
5176 Each element of the chain has valid next, breakpoint_at,
5177 commands, FIXME??? fields. */
5178
5179 bpstat
5180 bpstat_stop_status (struct address_space *aspace,
5181 CORE_ADDR bp_addr, ptid_t ptid,
5182 const struct target_waitstatus *ws)
5183 {
5184 struct breakpoint *b = NULL;
5185 struct bp_location *bl;
5186 struct bp_location *loc;
5187 /* First item of allocated bpstat's. */
5188 bpstat bs_head = NULL, *bs_link = &bs_head;
5189 /* Pointer to the last thing in the chain currently. */
5190 bpstat bs;
5191 int ix;
5192 int need_remove_insert;
5193 int removed_any;
5194
5195 /* First, build the bpstat chain with locations that explain a
5196 target stop, while being careful to not set the target running,
5197 as that may invalidate locations (in particular watchpoint
5198 locations are recreated). Resuming will happen here with
5199 breakpoint conditions or watchpoint expressions that include
5200 inferior function calls. */
5201
5202 ALL_BREAKPOINTS (b)
5203 {
5204 if (!breakpoint_enabled (b) && b->enable_state != bp_permanent)
5205 continue;
5206
5207 for (bl = b->loc; bl != NULL; bl = bl->next)
5208 {
5209 /* For hardware watchpoints, we look only at the first
5210 location. The watchpoint_check function will work on the
5211 entire expression, not the individual locations. For
5212 read watchpoints, the watchpoints_triggered function has
5213 checked all locations already. */
5214 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5215 break;
5216
5217 if (!bl->enabled || bl->shlib_disabled)
5218 continue;
5219
5220 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
5221 continue;
5222
5223 /* Come here if it's a watchpoint, or if the break address
5224 matches. */
5225
5226 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
5227 explain stop. */
5228
5229 /* Assume we stop. Should we find a watchpoint that is not
5230 actually triggered, or if the condition of the breakpoint
5231 evaluates as false, we'll reset 'stop' to 0. */
5232 bs->stop = 1;
5233 bs->print = 1;
5234
5235 /* If this is a scope breakpoint, mark the associated
5236 watchpoint as triggered so that we will handle the
5237 out-of-scope event. We'll get to the watchpoint next
5238 iteration. */
5239 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
5240 {
5241 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5242
5243 w->watchpoint_triggered = watch_triggered_yes;
5244 }
5245 }
5246 }
5247
5248 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
5249 {
5250 if (breakpoint_location_address_match (loc, aspace, bp_addr))
5251 {
5252 bs = bpstat_alloc (loc, &bs_link);
5253 /* For hits of moribund locations, we should just proceed. */
5254 bs->stop = 0;
5255 bs->print = 0;
5256 bs->print_it = print_it_noop;
5257 }
5258 }
5259
5260 /* A bit of special processing for shlib breakpoints. We need to
5261 process solib loading here, so that the lists of loaded and
5262 unloaded libraries are correct before we handle "catch load" and
5263 "catch unload". */
5264 for (bs = bs_head; bs != NULL; bs = bs->next)
5265 {
5266 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
5267 {
5268 handle_solib_event ();
5269 break;
5270 }
5271 }
5272
5273 /* Now go through the locations that caused the target to stop, and
5274 check whether we're interested in reporting this stop to higher
5275 layers, or whether we should resume the target transparently. */
5276
5277 removed_any = 0;
5278
5279 for (bs = bs_head; bs != NULL; bs = bs->next)
5280 {
5281 if (!bs->stop)
5282 continue;
5283
5284 b = bs->breakpoint_at;
5285 b->ops->check_status (bs);
5286 if (bs->stop)
5287 {
5288 bpstat_check_breakpoint_conditions (bs, ptid);
5289
5290 if (bs->stop)
5291 {
5292 ++(b->hit_count);
5293 observer_notify_breakpoint_modified (b);
5294
5295 /* We will stop here. */
5296 if (b->disposition == disp_disable)
5297 {
5298 --(b->enable_count);
5299 if (b->enable_count <= 0
5300 && b->enable_state != bp_permanent)
5301 b->enable_state = bp_disabled;
5302 removed_any = 1;
5303 }
5304 if (b->silent)
5305 bs->print = 0;
5306 bs->commands = b->commands;
5307 incref_counted_command_line (bs->commands);
5308 if (command_line_is_silent (bs->commands
5309 ? bs->commands->commands : NULL))
5310 bs->print = 0;
5311 }
5312
5313 }
5314
5315 /* Print nothing for this entry if we don't stop or don't
5316 print. */
5317 if (!bs->stop || !bs->print)
5318 bs->print_it = print_it_noop;
5319 }
5320
5321 /* If we aren't stopping, the value of some hardware watchpoint may
5322 not have changed, but the intermediate memory locations we are
5323 watching may have. Don't bother if we're stopping; this will get
5324 done later. */
5325 need_remove_insert = 0;
5326 if (! bpstat_causes_stop (bs_head))
5327 for (bs = bs_head; bs != NULL; bs = bs->next)
5328 if (!bs->stop
5329 && bs->breakpoint_at
5330 && is_hardware_watchpoint (bs->breakpoint_at))
5331 {
5332 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5333
5334 update_watchpoint (w, 0 /* don't reparse. */);
5335 need_remove_insert = 1;
5336 }
5337
5338 if (need_remove_insert)
5339 update_global_location_list (1);
5340 else if (removed_any)
5341 update_global_location_list (0);
5342
5343 return bs_head;
5344 }
5345
5346 static void
5347 handle_jit_event (void)
5348 {
5349 struct frame_info *frame;
5350 struct gdbarch *gdbarch;
5351
5352 /* Switch terminal for any messages produced by
5353 breakpoint_re_set. */
5354 target_terminal_ours_for_output ();
5355
5356 frame = get_current_frame ();
5357 gdbarch = get_frame_arch (frame);
5358
5359 jit_event_handler (gdbarch);
5360
5361 target_terminal_inferior ();
5362 }
5363
5364 /* Handle an solib event by calling solib_add. */
5365
5366 void
5367 handle_solib_event (void)
5368 {
5369 clear_program_space_solib_cache (current_inferior ()->pspace);
5370
5371 /* Check for any newly added shared libraries if we're supposed to
5372 be adding them automatically. Switch terminal for any messages
5373 produced by breakpoint_re_set. */
5374 target_terminal_ours_for_output ();
5375 #ifdef SOLIB_ADD
5376 SOLIB_ADD (NULL, 0, &current_target, auto_solib_add);
5377 #else
5378 solib_add (NULL, 0, &current_target, auto_solib_add);
5379 #endif
5380 target_terminal_inferior ();
5381 }
5382
5383 /* Prepare WHAT final decision for infrun. */
5384
5385 /* Decide what infrun needs to do with this bpstat. */
5386
5387 struct bpstat_what
5388 bpstat_what (bpstat bs_head)
5389 {
5390 struct bpstat_what retval;
5391 int jit_event = 0;
5392 bpstat bs;
5393
5394 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
5395 retval.call_dummy = STOP_NONE;
5396 retval.is_longjmp = 0;
5397
5398 for (bs = bs_head; bs != NULL; bs = bs->next)
5399 {
5400 /* Extract this BS's action. After processing each BS, we check
5401 if its action overrides all we've seem so far. */
5402 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5403 enum bptype bptype;
5404
5405 if (bs->breakpoint_at == NULL)
5406 {
5407 /* I suspect this can happen if it was a momentary
5408 breakpoint which has since been deleted. */
5409 bptype = bp_none;
5410 }
5411 else
5412 bptype = bs->breakpoint_at->type;
5413
5414 switch (bptype)
5415 {
5416 case bp_none:
5417 break;
5418 case bp_breakpoint:
5419 case bp_hardware_breakpoint:
5420 case bp_until:
5421 case bp_finish:
5422 case bp_shlib_event:
5423 if (bs->stop)
5424 {
5425 if (bs->print)
5426 this_action = BPSTAT_WHAT_STOP_NOISY;
5427 else
5428 this_action = BPSTAT_WHAT_STOP_SILENT;
5429 }
5430 else
5431 this_action = BPSTAT_WHAT_SINGLE;
5432 break;
5433 case bp_watchpoint:
5434 case bp_hardware_watchpoint:
5435 case bp_read_watchpoint:
5436 case bp_access_watchpoint:
5437 if (bs->stop)
5438 {
5439 if (bs->print)
5440 this_action = BPSTAT_WHAT_STOP_NOISY;
5441 else
5442 this_action = BPSTAT_WHAT_STOP_SILENT;
5443 }
5444 else
5445 {
5446 /* There was a watchpoint, but we're not stopping.
5447 This requires no further action. */
5448 }
5449 break;
5450 case bp_longjmp:
5451 case bp_longjmp_call_dummy:
5452 case bp_exception:
5453 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5454 retval.is_longjmp = bptype != bp_exception;
5455 break;
5456 case bp_longjmp_resume:
5457 case bp_exception_resume:
5458 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5459 retval.is_longjmp = bptype == bp_longjmp_resume;
5460 break;
5461 case bp_step_resume:
5462 if (bs->stop)
5463 this_action = BPSTAT_WHAT_STEP_RESUME;
5464 else
5465 {
5466 /* It is for the wrong frame. */
5467 this_action = BPSTAT_WHAT_SINGLE;
5468 }
5469 break;
5470 case bp_hp_step_resume:
5471 if (bs->stop)
5472 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5473 else
5474 {
5475 /* It is for the wrong frame. */
5476 this_action = BPSTAT_WHAT_SINGLE;
5477 }
5478 break;
5479 case bp_watchpoint_scope:
5480 case bp_thread_event:
5481 case bp_overlay_event:
5482 case bp_longjmp_master:
5483 case bp_std_terminate_master:
5484 case bp_exception_master:
5485 this_action = BPSTAT_WHAT_SINGLE;
5486 break;
5487 case bp_catchpoint:
5488 if (bs->stop)
5489 {
5490 if (bs->print)
5491 this_action = BPSTAT_WHAT_STOP_NOISY;
5492 else
5493 this_action = BPSTAT_WHAT_STOP_SILENT;
5494 }
5495 else
5496 {
5497 /* There was a catchpoint, but we're not stopping.
5498 This requires no further action. */
5499 }
5500 break;
5501 case bp_jit_event:
5502 jit_event = 1;
5503 this_action = BPSTAT_WHAT_SINGLE;
5504 break;
5505 case bp_call_dummy:
5506 /* Make sure the action is stop (silent or noisy),
5507 so infrun.c pops the dummy frame. */
5508 retval.call_dummy = STOP_STACK_DUMMY;
5509 this_action = BPSTAT_WHAT_STOP_SILENT;
5510 break;
5511 case bp_std_terminate:
5512 /* Make sure the action is stop (silent or noisy),
5513 so infrun.c pops the dummy frame. */
5514 retval.call_dummy = STOP_STD_TERMINATE;
5515 this_action = BPSTAT_WHAT_STOP_SILENT;
5516 break;
5517 case bp_tracepoint:
5518 case bp_fast_tracepoint:
5519 case bp_static_tracepoint:
5520 /* Tracepoint hits should not be reported back to GDB, and
5521 if one got through somehow, it should have been filtered
5522 out already. */
5523 internal_error (__FILE__, __LINE__,
5524 _("bpstat_what: tracepoint encountered"));
5525 break;
5526 case bp_gnu_ifunc_resolver:
5527 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5528 this_action = BPSTAT_WHAT_SINGLE;
5529 break;
5530 case bp_gnu_ifunc_resolver_return:
5531 /* The breakpoint will be removed, execution will restart from the
5532 PC of the former breakpoint. */
5533 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5534 break;
5535
5536 case bp_dprintf:
5537 this_action = BPSTAT_WHAT_STOP_SILENT;
5538 break;
5539
5540 default:
5541 internal_error (__FILE__, __LINE__,
5542 _("bpstat_what: unhandled bptype %d"), (int) bptype);
5543 }
5544
5545 retval.main_action = max (retval.main_action, this_action);
5546 }
5547
5548 /* These operations may affect the bs->breakpoint_at state so they are
5549 delayed after MAIN_ACTION is decided above. */
5550
5551 if (jit_event)
5552 {
5553 if (debug_infrun)
5554 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_jit_event\n");
5555
5556 handle_jit_event ();
5557 }
5558
5559 for (bs = bs_head; bs != NULL; bs = bs->next)
5560 {
5561 struct breakpoint *b = bs->breakpoint_at;
5562
5563 if (b == NULL)
5564 continue;
5565 switch (b->type)
5566 {
5567 case bp_gnu_ifunc_resolver:
5568 gnu_ifunc_resolver_stop (b);
5569 break;
5570 case bp_gnu_ifunc_resolver_return:
5571 gnu_ifunc_resolver_return_stop (b);
5572 break;
5573 }
5574 }
5575
5576 return retval;
5577 }
5578
5579 /* Nonzero if we should step constantly (e.g. watchpoints on machines
5580 without hardware support). This isn't related to a specific bpstat,
5581 just to things like whether watchpoints are set. */
5582
5583 int
5584 bpstat_should_step (void)
5585 {
5586 struct breakpoint *b;
5587
5588 ALL_BREAKPOINTS (b)
5589 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
5590 return 1;
5591 return 0;
5592 }
5593
5594 int
5595 bpstat_causes_stop (bpstat bs)
5596 {
5597 for (; bs != NULL; bs = bs->next)
5598 if (bs->stop)
5599 return 1;
5600
5601 return 0;
5602 }
5603
5604 \f
5605
5606 /* Compute a string of spaces suitable to indent the next line
5607 so it starts at the position corresponding to the table column
5608 named COL_NAME in the currently active table of UIOUT. */
5609
5610 static char *
5611 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5612 {
5613 static char wrap_indent[80];
5614 int i, total_width, width, align;
5615 char *text;
5616
5617 total_width = 0;
5618 for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
5619 {
5620 if (strcmp (text, col_name) == 0)
5621 {
5622 gdb_assert (total_width < sizeof wrap_indent);
5623 memset (wrap_indent, ' ', total_width);
5624 wrap_indent[total_width] = 0;
5625
5626 return wrap_indent;
5627 }
5628
5629 total_width += width + 1;
5630 }
5631
5632 return NULL;
5633 }
5634
5635 /* Determine if the locations of this breakpoint will have their conditions
5636 evaluated by the target, host or a mix of both. Returns the following:
5637
5638 "host": Host evals condition.
5639 "host or target": Host or Target evals condition.
5640 "target": Target evals condition.
5641 */
5642
5643 static const char *
5644 bp_condition_evaluator (struct breakpoint *b)
5645 {
5646 struct bp_location *bl;
5647 char host_evals = 0;
5648 char target_evals = 0;
5649
5650 if (!b)
5651 return NULL;
5652
5653 if (!is_breakpoint (b))
5654 return NULL;
5655
5656 if (gdb_evaluates_breakpoint_condition_p ()
5657 || !target_supports_evaluation_of_breakpoint_conditions ())
5658 return condition_evaluation_host;
5659
5660 for (bl = b->loc; bl; bl = bl->next)
5661 {
5662 if (bl->cond_bytecode)
5663 target_evals++;
5664 else
5665 host_evals++;
5666 }
5667
5668 if (host_evals && target_evals)
5669 return condition_evaluation_both;
5670 else if (target_evals)
5671 return condition_evaluation_target;
5672 else
5673 return condition_evaluation_host;
5674 }
5675
5676 /* Determine the breakpoint location's condition evaluator. This is
5677 similar to bp_condition_evaluator, but for locations. */
5678
5679 static const char *
5680 bp_location_condition_evaluator (struct bp_location *bl)
5681 {
5682 if (bl && !is_breakpoint (bl->owner))
5683 return NULL;
5684
5685 if (gdb_evaluates_breakpoint_condition_p ()
5686 || !target_supports_evaluation_of_breakpoint_conditions ())
5687 return condition_evaluation_host;
5688
5689 if (bl && bl->cond_bytecode)
5690 return condition_evaluation_target;
5691 else
5692 return condition_evaluation_host;
5693 }
5694
5695 /* Print the LOC location out of the list of B->LOC locations. */
5696
5697 static void
5698 print_breakpoint_location (struct breakpoint *b,
5699 struct bp_location *loc)
5700 {
5701 struct ui_out *uiout = current_uiout;
5702 struct cleanup *old_chain = save_current_program_space ();
5703
5704 if (loc != NULL && loc->shlib_disabled)
5705 loc = NULL;
5706
5707 if (loc != NULL)
5708 set_current_program_space (loc->pspace);
5709
5710 if (b->display_canonical)
5711 ui_out_field_string (uiout, "what", b->addr_string);
5712 else if (loc && loc->symtab)
5713 {
5714 struct symbol *sym
5715 = find_pc_sect_function (loc->address, loc->section);
5716 if (sym)
5717 {
5718 ui_out_text (uiout, "in ");
5719 ui_out_field_string (uiout, "func",
5720 SYMBOL_PRINT_NAME (sym));
5721 ui_out_text (uiout, " ");
5722 ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
5723 ui_out_text (uiout, "at ");
5724 }
5725 ui_out_field_string (uiout, "file",
5726 symtab_to_filename_for_display (loc->symtab));
5727 ui_out_text (uiout, ":");
5728
5729 if (ui_out_is_mi_like_p (uiout))
5730 ui_out_field_string (uiout, "fullname",
5731 symtab_to_fullname (loc->symtab));
5732
5733 ui_out_field_int (uiout, "line", loc->line_number);
5734 }
5735 else if (loc)
5736 {
5737 struct ui_file *stb = mem_fileopen ();
5738 struct cleanup *stb_chain = make_cleanup_ui_file_delete (stb);
5739
5740 print_address_symbolic (loc->gdbarch, loc->address, stb,
5741 demangle, "");
5742 ui_out_field_stream (uiout, "at", stb);
5743
5744 do_cleanups (stb_chain);
5745 }
5746 else
5747 ui_out_field_string (uiout, "pending", b->addr_string);
5748
5749 if (loc && is_breakpoint (b)
5750 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
5751 && bp_condition_evaluator (b) == condition_evaluation_both)
5752 {
5753 ui_out_text (uiout, " (");
5754 ui_out_field_string (uiout, "evaluated-by",
5755 bp_location_condition_evaluator (loc));
5756 ui_out_text (uiout, ")");
5757 }
5758
5759 do_cleanups (old_chain);
5760 }
5761
5762 static const char *
5763 bptype_string (enum bptype type)
5764 {
5765 struct ep_type_description
5766 {
5767 enum bptype type;
5768 char *description;
5769 };
5770 static struct ep_type_description bptypes[] =
5771 {
5772 {bp_none, "?deleted?"},
5773 {bp_breakpoint, "breakpoint"},
5774 {bp_hardware_breakpoint, "hw breakpoint"},
5775 {bp_until, "until"},
5776 {bp_finish, "finish"},
5777 {bp_watchpoint, "watchpoint"},
5778 {bp_hardware_watchpoint, "hw watchpoint"},
5779 {bp_read_watchpoint, "read watchpoint"},
5780 {bp_access_watchpoint, "acc watchpoint"},
5781 {bp_longjmp, "longjmp"},
5782 {bp_longjmp_resume, "longjmp resume"},
5783 {bp_longjmp_call_dummy, "longjmp for call dummy"},
5784 {bp_exception, "exception"},
5785 {bp_exception_resume, "exception resume"},
5786 {bp_step_resume, "step resume"},
5787 {bp_hp_step_resume, "high-priority step resume"},
5788 {bp_watchpoint_scope, "watchpoint scope"},
5789 {bp_call_dummy, "call dummy"},
5790 {bp_std_terminate, "std::terminate"},
5791 {bp_shlib_event, "shlib events"},
5792 {bp_thread_event, "thread events"},
5793 {bp_overlay_event, "overlay events"},
5794 {bp_longjmp_master, "longjmp master"},
5795 {bp_std_terminate_master, "std::terminate master"},
5796 {bp_exception_master, "exception master"},
5797 {bp_catchpoint, "catchpoint"},
5798 {bp_tracepoint, "tracepoint"},
5799 {bp_fast_tracepoint, "fast tracepoint"},
5800 {bp_static_tracepoint, "static tracepoint"},
5801 {bp_dprintf, "dprintf"},
5802 {bp_jit_event, "jit events"},
5803 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
5804 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
5805 };
5806
5807 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
5808 || ((int) type != bptypes[(int) type].type))
5809 internal_error (__FILE__, __LINE__,
5810 _("bptypes table does not describe type #%d."),
5811 (int) type);
5812
5813 return bptypes[(int) type].description;
5814 }
5815
5816 DEF_VEC_I(int);
5817
5818 /* For MI, output a field named 'thread-groups' with a list as the value.
5819 For CLI, prefix the list with the string 'inf'. */
5820
5821 static void
5822 output_thread_groups (struct ui_out *uiout,
5823 const char *field_name,
5824 VEC(int) *inf_num,
5825 int mi_only)
5826 {
5827 struct cleanup *back_to = make_cleanup_ui_out_list_begin_end (uiout,
5828 field_name);
5829 int is_mi = ui_out_is_mi_like_p (uiout);
5830 int inf;
5831 int i;
5832
5833 /* For backward compatibility, don't display inferiors in CLI unless
5834 there are several. Always display them for MI. */
5835 if (!is_mi && mi_only)
5836 return;
5837
5838 for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
5839 {
5840 if (is_mi)
5841 {
5842 char mi_group[10];
5843
5844 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
5845 ui_out_field_string (uiout, NULL, mi_group);
5846 }
5847 else
5848 {
5849 if (i == 0)
5850 ui_out_text (uiout, " inf ");
5851 else
5852 ui_out_text (uiout, ", ");
5853
5854 ui_out_text (uiout, plongest (inf));
5855 }
5856 }
5857
5858 do_cleanups (back_to);
5859 }
5860
5861 /* Print B to gdb_stdout. */
5862
5863 static void
5864 print_one_breakpoint_location (struct breakpoint *b,
5865 struct bp_location *loc,
5866 int loc_number,
5867 struct bp_location **last_loc,
5868 int allflag)
5869 {
5870 struct command_line *l;
5871 static char bpenables[] = "nynny";
5872
5873 struct ui_out *uiout = current_uiout;
5874 int header_of_multiple = 0;
5875 int part_of_multiple = (loc != NULL);
5876 struct value_print_options opts;
5877
5878 get_user_print_options (&opts);
5879
5880 gdb_assert (!loc || loc_number != 0);
5881 /* See comment in print_one_breakpoint concerning treatment of
5882 breakpoints with single disabled location. */
5883 if (loc == NULL
5884 && (b->loc != NULL
5885 && (b->loc->next != NULL || !b->loc->enabled)))
5886 header_of_multiple = 1;
5887 if (loc == NULL)
5888 loc = b->loc;
5889
5890 annotate_record ();
5891
5892 /* 1 */
5893 annotate_field (0);
5894 if (part_of_multiple)
5895 {
5896 char *formatted;
5897 formatted = xstrprintf ("%d.%d", b->number, loc_number);
5898 ui_out_field_string (uiout, "number", formatted);
5899 xfree (formatted);
5900 }
5901 else
5902 {
5903 ui_out_field_int (uiout, "number", b->number);
5904 }
5905
5906 /* 2 */
5907 annotate_field (1);
5908 if (part_of_multiple)
5909 ui_out_field_skip (uiout, "type");
5910 else
5911 ui_out_field_string (uiout, "type", bptype_string (b->type));
5912
5913 /* 3 */
5914 annotate_field (2);
5915 if (part_of_multiple)
5916 ui_out_field_skip (uiout, "disp");
5917 else
5918 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
5919
5920
5921 /* 4 */
5922 annotate_field (3);
5923 if (part_of_multiple)
5924 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
5925 else
5926 ui_out_field_fmt (uiout, "enabled", "%c",
5927 bpenables[(int) b->enable_state]);
5928 ui_out_spaces (uiout, 2);
5929
5930
5931 /* 5 and 6 */
5932 if (b->ops != NULL && b->ops->print_one != NULL)
5933 {
5934 /* Although the print_one can possibly print all locations,
5935 calling it here is not likely to get any nice result. So,
5936 make sure there's just one location. */
5937 gdb_assert (b->loc == NULL || b->loc->next == NULL);
5938 b->ops->print_one (b, last_loc);
5939 }
5940 else
5941 switch (b->type)
5942 {
5943 case bp_none:
5944 internal_error (__FILE__, __LINE__,
5945 _("print_one_breakpoint: bp_none encountered\n"));
5946 break;
5947
5948 case bp_watchpoint:
5949 case bp_hardware_watchpoint:
5950 case bp_read_watchpoint:
5951 case bp_access_watchpoint:
5952 {
5953 struct watchpoint *w = (struct watchpoint *) b;
5954
5955 /* Field 4, the address, is omitted (which makes the columns
5956 not line up too nicely with the headers, but the effect
5957 is relatively readable). */
5958 if (opts.addressprint)
5959 ui_out_field_skip (uiout, "addr");
5960 annotate_field (5);
5961 ui_out_field_string (uiout, "what", w->exp_string);
5962 }
5963 break;
5964
5965 case bp_breakpoint:
5966 case bp_hardware_breakpoint:
5967 case bp_until:
5968 case bp_finish:
5969 case bp_longjmp:
5970 case bp_longjmp_resume:
5971 case bp_longjmp_call_dummy:
5972 case bp_exception:
5973 case bp_exception_resume:
5974 case bp_step_resume:
5975 case bp_hp_step_resume:
5976 case bp_watchpoint_scope:
5977 case bp_call_dummy:
5978 case bp_std_terminate:
5979 case bp_shlib_event:
5980 case bp_thread_event:
5981 case bp_overlay_event:
5982 case bp_longjmp_master:
5983 case bp_std_terminate_master:
5984 case bp_exception_master:
5985 case bp_tracepoint:
5986 case bp_fast_tracepoint:
5987 case bp_static_tracepoint:
5988 case bp_dprintf:
5989 case bp_jit_event:
5990 case bp_gnu_ifunc_resolver:
5991 case bp_gnu_ifunc_resolver_return:
5992 if (opts.addressprint)
5993 {
5994 annotate_field (4);
5995 if (header_of_multiple)
5996 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
5997 else if (b->loc == NULL || loc->shlib_disabled)
5998 ui_out_field_string (uiout, "addr", "<PENDING>");
5999 else
6000 ui_out_field_core_addr (uiout, "addr",
6001 loc->gdbarch, loc->address);
6002 }
6003 annotate_field (5);
6004 if (!header_of_multiple)
6005 print_breakpoint_location (b, loc);
6006 if (b->loc)
6007 *last_loc = b->loc;
6008 break;
6009 }
6010
6011
6012 if (loc != NULL && !header_of_multiple)
6013 {
6014 struct inferior *inf;
6015 VEC(int) *inf_num = NULL;
6016 int mi_only = 1;
6017
6018 ALL_INFERIORS (inf)
6019 {
6020 if (inf->pspace == loc->pspace)
6021 VEC_safe_push (int, inf_num, inf->num);
6022 }
6023
6024 /* For backward compatibility, don't display inferiors in CLI unless
6025 there are several. Always display for MI. */
6026 if (allflag
6027 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6028 && (number_of_program_spaces () > 1
6029 || number_of_inferiors () > 1)
6030 /* LOC is for existing B, it cannot be in
6031 moribund_locations and thus having NULL OWNER. */
6032 && loc->owner->type != bp_catchpoint))
6033 mi_only = 0;
6034 output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6035 VEC_free (int, inf_num);
6036 }
6037
6038 if (!part_of_multiple)
6039 {
6040 if (b->thread != -1)
6041 {
6042 /* FIXME: This seems to be redundant and lost here; see the
6043 "stop only in" line a little further down. */
6044 ui_out_text (uiout, " thread ");
6045 ui_out_field_int (uiout, "thread", b->thread);
6046 }
6047 else if (b->task != 0)
6048 {
6049 ui_out_text (uiout, " task ");
6050 ui_out_field_int (uiout, "task", b->task);
6051 }
6052 }
6053
6054 ui_out_text (uiout, "\n");
6055
6056 if (!part_of_multiple)
6057 b->ops->print_one_detail (b, uiout);
6058
6059 if (part_of_multiple && frame_id_p (b->frame_id))
6060 {
6061 annotate_field (6);
6062 ui_out_text (uiout, "\tstop only in stack frame at ");
6063 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
6064 the frame ID. */
6065 ui_out_field_core_addr (uiout, "frame",
6066 b->gdbarch, b->frame_id.stack_addr);
6067 ui_out_text (uiout, "\n");
6068 }
6069
6070 if (!part_of_multiple && b->cond_string)
6071 {
6072 annotate_field (7);
6073 if (is_tracepoint (b))
6074 ui_out_text (uiout, "\ttrace only if ");
6075 else
6076 ui_out_text (uiout, "\tstop only if ");
6077 ui_out_field_string (uiout, "cond", b->cond_string);
6078
6079 /* Print whether the target is doing the breakpoint's condition
6080 evaluation. If GDB is doing the evaluation, don't print anything. */
6081 if (is_breakpoint (b)
6082 && breakpoint_condition_evaluation_mode ()
6083 == condition_evaluation_target)
6084 {
6085 ui_out_text (uiout, " (");
6086 ui_out_field_string (uiout, "evaluated-by",
6087 bp_condition_evaluator (b));
6088 ui_out_text (uiout, " evals)");
6089 }
6090 ui_out_text (uiout, "\n");
6091 }
6092
6093 if (!part_of_multiple && b->thread != -1)
6094 {
6095 /* FIXME should make an annotation for this. */
6096 ui_out_text (uiout, "\tstop only in thread ");
6097 ui_out_field_int (uiout, "thread", b->thread);
6098 ui_out_text (uiout, "\n");
6099 }
6100
6101 if (!part_of_multiple)
6102 {
6103 if (b->hit_count)
6104 {
6105 /* FIXME should make an annotation for this. */
6106 if (is_catchpoint (b))
6107 ui_out_text (uiout, "\tcatchpoint");
6108 else if (is_tracepoint (b))
6109 ui_out_text (uiout, "\ttracepoint");
6110 else
6111 ui_out_text (uiout, "\tbreakpoint");
6112 ui_out_text (uiout, " already hit ");
6113 ui_out_field_int (uiout, "times", b->hit_count);
6114 if (b->hit_count == 1)
6115 ui_out_text (uiout, " time\n");
6116 else
6117 ui_out_text (uiout, " times\n");
6118 }
6119 else
6120 {
6121 /* Output the count also if it is zero, but only if this is mi. */
6122 if (ui_out_is_mi_like_p (uiout))
6123 ui_out_field_int (uiout, "times", b->hit_count);
6124 }
6125 }
6126
6127 if (!part_of_multiple && b->ignore_count)
6128 {
6129 annotate_field (8);
6130 ui_out_text (uiout, "\tignore next ");
6131 ui_out_field_int (uiout, "ignore", b->ignore_count);
6132 ui_out_text (uiout, " hits\n");
6133 }
6134
6135 /* Note that an enable count of 1 corresponds to "enable once"
6136 behavior, which is reported by the combination of enablement and
6137 disposition, so we don't need to mention it here. */
6138 if (!part_of_multiple && b->enable_count > 1)
6139 {
6140 annotate_field (8);
6141 ui_out_text (uiout, "\tdisable after ");
6142 /* Tweak the wording to clarify that ignore and enable counts
6143 are distinct, and have additive effect. */
6144 if (b->ignore_count)
6145 ui_out_text (uiout, "additional ");
6146 else
6147 ui_out_text (uiout, "next ");
6148 ui_out_field_int (uiout, "enable", b->enable_count);
6149 ui_out_text (uiout, " hits\n");
6150 }
6151
6152 if (!part_of_multiple && is_tracepoint (b))
6153 {
6154 struct tracepoint *tp = (struct tracepoint *) b;
6155
6156 if (tp->traceframe_usage)
6157 {
6158 ui_out_text (uiout, "\ttrace buffer usage ");
6159 ui_out_field_int (uiout, "traceframe-usage", tp->traceframe_usage);
6160 ui_out_text (uiout, " bytes\n");
6161 }
6162 }
6163
6164 l = b->commands ? b->commands->commands : NULL;
6165 if (!part_of_multiple && l)
6166 {
6167 struct cleanup *script_chain;
6168
6169 annotate_field (9);
6170 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
6171 print_command_lines (uiout, l, 4);
6172 do_cleanups (script_chain);
6173 }
6174
6175 if (is_tracepoint (b))
6176 {
6177 struct tracepoint *t = (struct tracepoint *) b;
6178
6179 if (!part_of_multiple && t->pass_count)
6180 {
6181 annotate_field (10);
6182 ui_out_text (uiout, "\tpass count ");
6183 ui_out_field_int (uiout, "pass", t->pass_count);
6184 ui_out_text (uiout, " \n");
6185 }
6186
6187 /* Don't display it when tracepoint or tracepoint location is
6188 pending. */
6189 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6190 {
6191 annotate_field (11);
6192
6193 if (ui_out_is_mi_like_p (uiout))
6194 ui_out_field_string (uiout, "installed",
6195 loc->inserted ? "y" : "n");
6196 else
6197 {
6198 if (loc->inserted)
6199 ui_out_text (uiout, "\t");
6200 else
6201 ui_out_text (uiout, "\tnot ");
6202 ui_out_text (uiout, "installed on target\n");
6203 }
6204 }
6205 }
6206
6207 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
6208 {
6209 if (is_watchpoint (b))
6210 {
6211 struct watchpoint *w = (struct watchpoint *) b;
6212
6213 ui_out_field_string (uiout, "original-location", w->exp_string);
6214 }
6215 else if (b->addr_string)
6216 ui_out_field_string (uiout, "original-location", b->addr_string);
6217 }
6218 }
6219
6220 static void
6221 print_one_breakpoint (struct breakpoint *b,
6222 struct bp_location **last_loc,
6223 int allflag)
6224 {
6225 struct cleanup *bkpt_chain;
6226 struct ui_out *uiout = current_uiout;
6227
6228 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
6229
6230 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
6231 do_cleanups (bkpt_chain);
6232
6233 /* If this breakpoint has custom print function,
6234 it's already printed. Otherwise, print individual
6235 locations, if any. */
6236 if (b->ops == NULL || b->ops->print_one == NULL)
6237 {
6238 /* If breakpoint has a single location that is disabled, we
6239 print it as if it had several locations, since otherwise it's
6240 hard to represent "breakpoint enabled, location disabled"
6241 situation.
6242
6243 Note that while hardware watchpoints have several locations
6244 internally, that's not a property exposed to user. */
6245 if (b->loc
6246 && !is_hardware_watchpoint (b)
6247 && (b->loc->next || !b->loc->enabled))
6248 {
6249 struct bp_location *loc;
6250 int n = 1;
6251
6252 for (loc = b->loc; loc; loc = loc->next, ++n)
6253 {
6254 struct cleanup *inner2 =
6255 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
6256 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
6257 do_cleanups (inner2);
6258 }
6259 }
6260 }
6261 }
6262
6263 static int
6264 breakpoint_address_bits (struct breakpoint *b)
6265 {
6266 int print_address_bits = 0;
6267 struct bp_location *loc;
6268
6269 for (loc = b->loc; loc; loc = loc->next)
6270 {
6271 int addr_bit;
6272
6273 /* Software watchpoints that aren't watching memory don't have
6274 an address to print. */
6275 if (b->type == bp_watchpoint && loc->watchpoint_type == -1)
6276 continue;
6277
6278 addr_bit = gdbarch_addr_bit (loc->gdbarch);
6279 if (addr_bit > print_address_bits)
6280 print_address_bits = addr_bit;
6281 }
6282
6283 return print_address_bits;
6284 }
6285
6286 struct captured_breakpoint_query_args
6287 {
6288 int bnum;
6289 };
6290
6291 static int
6292 do_captured_breakpoint_query (struct ui_out *uiout, void *data)
6293 {
6294 struct captured_breakpoint_query_args *args = data;
6295 struct breakpoint *b;
6296 struct bp_location *dummy_loc = NULL;
6297
6298 ALL_BREAKPOINTS (b)
6299 {
6300 if (args->bnum == b->number)
6301 {
6302 print_one_breakpoint (b, &dummy_loc, 0);
6303 return GDB_RC_OK;
6304 }
6305 }
6306 return GDB_RC_NONE;
6307 }
6308
6309 enum gdb_rc
6310 gdb_breakpoint_query (struct ui_out *uiout, int bnum,
6311 char **error_message)
6312 {
6313 struct captured_breakpoint_query_args args;
6314
6315 args.bnum = bnum;
6316 /* For the moment we don't trust print_one_breakpoint() to not throw
6317 an error. */
6318 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
6319 error_message, RETURN_MASK_ALL) < 0)
6320 return GDB_RC_FAIL;
6321 else
6322 return GDB_RC_OK;
6323 }
6324
6325 /* Return true if this breakpoint was set by the user, false if it is
6326 internal or momentary. */
6327
6328 int
6329 user_breakpoint_p (struct breakpoint *b)
6330 {
6331 return b->number > 0;
6332 }
6333
6334 /* Print information on user settable breakpoint (watchpoint, etc)
6335 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6336 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6337 FILTER is non-NULL, call it on each breakpoint and only include the
6338 ones for which it returns non-zero. Return the total number of
6339 breakpoints listed. */
6340
6341 static int
6342 breakpoint_1 (char *args, int allflag,
6343 int (*filter) (const struct breakpoint *))
6344 {
6345 struct breakpoint *b;
6346 struct bp_location *last_loc = NULL;
6347 int nr_printable_breakpoints;
6348 struct cleanup *bkpttbl_chain;
6349 struct value_print_options opts;
6350 int print_address_bits = 0;
6351 int print_type_col_width = 14;
6352 struct ui_out *uiout = current_uiout;
6353
6354 get_user_print_options (&opts);
6355
6356 /* Compute the number of rows in the table, as well as the size
6357 required for address fields. */
6358 nr_printable_breakpoints = 0;
6359 ALL_BREAKPOINTS (b)
6360 {
6361 /* If we have a filter, only list the breakpoints it accepts. */
6362 if (filter && !filter (b))
6363 continue;
6364
6365 /* If we have an "args" string, it is a list of breakpoints to
6366 accept. Skip the others. */
6367 if (args != NULL && *args != '\0')
6368 {
6369 if (allflag && parse_and_eval_long (args) != b->number)
6370 continue;
6371 if (!allflag && !number_is_in_list (args, b->number))
6372 continue;
6373 }
6374
6375 if (allflag || user_breakpoint_p (b))
6376 {
6377 int addr_bit, type_len;
6378
6379 addr_bit = breakpoint_address_bits (b);
6380 if (addr_bit > print_address_bits)
6381 print_address_bits = addr_bit;
6382
6383 type_len = strlen (bptype_string (b->type));
6384 if (type_len > print_type_col_width)
6385 print_type_col_width = type_len;
6386
6387 nr_printable_breakpoints++;
6388 }
6389 }
6390
6391 if (opts.addressprint)
6392 bkpttbl_chain
6393 = make_cleanup_ui_out_table_begin_end (uiout, 6,
6394 nr_printable_breakpoints,
6395 "BreakpointTable");
6396 else
6397 bkpttbl_chain
6398 = make_cleanup_ui_out_table_begin_end (uiout, 5,
6399 nr_printable_breakpoints,
6400 "BreakpointTable");
6401
6402 if (nr_printable_breakpoints > 0)
6403 annotate_breakpoints_headers ();
6404 if (nr_printable_breakpoints > 0)
6405 annotate_field (0);
6406 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
6407 if (nr_printable_breakpoints > 0)
6408 annotate_field (1);
6409 ui_out_table_header (uiout, print_type_col_width, ui_left,
6410 "type", "Type"); /* 2 */
6411 if (nr_printable_breakpoints > 0)
6412 annotate_field (2);
6413 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
6414 if (nr_printable_breakpoints > 0)
6415 annotate_field (3);
6416 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
6417 if (opts.addressprint)
6418 {
6419 if (nr_printable_breakpoints > 0)
6420 annotate_field (4);
6421 if (print_address_bits <= 32)
6422 ui_out_table_header (uiout, 10, ui_left,
6423 "addr", "Address"); /* 5 */
6424 else
6425 ui_out_table_header (uiout, 18, ui_left,
6426 "addr", "Address"); /* 5 */
6427 }
6428 if (nr_printable_breakpoints > 0)
6429 annotate_field (5);
6430 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
6431 ui_out_table_body (uiout);
6432 if (nr_printable_breakpoints > 0)
6433 annotate_breakpoints_table ();
6434
6435 ALL_BREAKPOINTS (b)
6436 {
6437 QUIT;
6438 /* If we have a filter, only list the breakpoints it accepts. */
6439 if (filter && !filter (b))
6440 continue;
6441
6442 /* If we have an "args" string, it is a list of breakpoints to
6443 accept. Skip the others. */
6444
6445 if (args != NULL && *args != '\0')
6446 {
6447 if (allflag) /* maintenance info breakpoint */
6448 {
6449 if (parse_and_eval_long (args) != b->number)
6450 continue;
6451 }
6452 else /* all others */
6453 {
6454 if (!number_is_in_list (args, b->number))
6455 continue;
6456 }
6457 }
6458 /* We only print out user settable breakpoints unless the
6459 allflag is set. */
6460 if (allflag || user_breakpoint_p (b))
6461 print_one_breakpoint (b, &last_loc, allflag);
6462 }
6463
6464 do_cleanups (bkpttbl_chain);
6465
6466 if (nr_printable_breakpoints == 0)
6467 {
6468 /* If there's a filter, let the caller decide how to report
6469 empty list. */
6470 if (!filter)
6471 {
6472 if (args == NULL || *args == '\0')
6473 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
6474 else
6475 ui_out_message (uiout, 0,
6476 "No breakpoint or watchpoint matching '%s'.\n",
6477 args);
6478 }
6479 }
6480 else
6481 {
6482 if (last_loc && !server_command)
6483 set_next_address (last_loc->gdbarch, last_loc->address);
6484 }
6485
6486 /* FIXME? Should this be moved up so that it is only called when
6487 there have been breakpoints? */
6488 annotate_breakpoints_table_end ();
6489
6490 return nr_printable_breakpoints;
6491 }
6492
6493 /* Display the value of default-collect in a way that is generally
6494 compatible with the breakpoint list. */
6495
6496 static void
6497 default_collect_info (void)
6498 {
6499 struct ui_out *uiout = current_uiout;
6500
6501 /* If it has no value (which is frequently the case), say nothing; a
6502 message like "No default-collect." gets in user's face when it's
6503 not wanted. */
6504 if (!*default_collect)
6505 return;
6506
6507 /* The following phrase lines up nicely with per-tracepoint collect
6508 actions. */
6509 ui_out_text (uiout, "default collect ");
6510 ui_out_field_string (uiout, "default-collect", default_collect);
6511 ui_out_text (uiout, " \n");
6512 }
6513
6514 static void
6515 breakpoints_info (char *args, int from_tty)
6516 {
6517 breakpoint_1 (args, 0, NULL);
6518
6519 default_collect_info ();
6520 }
6521
6522 static void
6523 watchpoints_info (char *args, int from_tty)
6524 {
6525 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
6526 struct ui_out *uiout = current_uiout;
6527
6528 if (num_printed == 0)
6529 {
6530 if (args == NULL || *args == '\0')
6531 ui_out_message (uiout, 0, "No watchpoints.\n");
6532 else
6533 ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
6534 }
6535 }
6536
6537 static void
6538 maintenance_info_breakpoints (char *args, int from_tty)
6539 {
6540 breakpoint_1 (args, 1, NULL);
6541
6542 default_collect_info ();
6543 }
6544
6545 static int
6546 breakpoint_has_pc (struct breakpoint *b,
6547 struct program_space *pspace,
6548 CORE_ADDR pc, struct obj_section *section)
6549 {
6550 struct bp_location *bl = b->loc;
6551
6552 for (; bl; bl = bl->next)
6553 {
6554 if (bl->pspace == pspace
6555 && bl->address == pc
6556 && (!overlay_debugging || bl->section == section))
6557 return 1;
6558 }
6559 return 0;
6560 }
6561
6562 /* Print a message describing any user-breakpoints set at PC. This
6563 concerns with logical breakpoints, so we match program spaces, not
6564 address spaces. */
6565
6566 static void
6567 describe_other_breakpoints (struct gdbarch *gdbarch,
6568 struct program_space *pspace, CORE_ADDR pc,
6569 struct obj_section *section, int thread)
6570 {
6571 int others = 0;
6572 struct breakpoint *b;
6573
6574 ALL_BREAKPOINTS (b)
6575 others += (user_breakpoint_p (b)
6576 && breakpoint_has_pc (b, pspace, pc, section));
6577 if (others > 0)
6578 {
6579 if (others == 1)
6580 printf_filtered (_("Note: breakpoint "));
6581 else /* if (others == ???) */
6582 printf_filtered (_("Note: breakpoints "));
6583 ALL_BREAKPOINTS (b)
6584 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
6585 {
6586 others--;
6587 printf_filtered ("%d", b->number);
6588 if (b->thread == -1 && thread != -1)
6589 printf_filtered (" (all threads)");
6590 else if (b->thread != -1)
6591 printf_filtered (" (thread %d)", b->thread);
6592 printf_filtered ("%s%s ",
6593 ((b->enable_state == bp_disabled
6594 || b->enable_state == bp_call_disabled)
6595 ? " (disabled)"
6596 : b->enable_state == bp_permanent
6597 ? " (permanent)"
6598 : ""),
6599 (others > 1) ? ","
6600 : ((others == 1) ? " and" : ""));
6601 }
6602 printf_filtered (_("also set at pc "));
6603 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
6604 printf_filtered (".\n");
6605 }
6606 }
6607 \f
6608
6609 /* Return true iff it is meaningful to use the address member of
6610 BPT. For some breakpoint types, the address member is irrelevant
6611 and it makes no sense to attempt to compare it to other addresses
6612 (or use it for any other purpose either).
6613
6614 More specifically, each of the following breakpoint types will
6615 always have a zero valued address and we don't want to mark
6616 breakpoints of any of these types to be a duplicate of an actual
6617 breakpoint at address zero:
6618
6619 bp_watchpoint
6620 bp_catchpoint
6621
6622 */
6623
6624 static int
6625 breakpoint_address_is_meaningful (struct breakpoint *bpt)
6626 {
6627 enum bptype type = bpt->type;
6628
6629 return (type != bp_watchpoint && type != bp_catchpoint);
6630 }
6631
6632 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6633 true if LOC1 and LOC2 represent the same watchpoint location. */
6634
6635 static int
6636 watchpoint_locations_match (struct bp_location *loc1,
6637 struct bp_location *loc2)
6638 {
6639 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6640 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6641
6642 /* Both of them must exist. */
6643 gdb_assert (w1 != NULL);
6644 gdb_assert (w2 != NULL);
6645
6646 /* If the target can evaluate the condition expression in hardware,
6647 then we we need to insert both watchpoints even if they are at
6648 the same place. Otherwise the watchpoint will only trigger when
6649 the condition of whichever watchpoint was inserted evaluates to
6650 true, not giving a chance for GDB to check the condition of the
6651 other watchpoint. */
6652 if ((w1->cond_exp
6653 && target_can_accel_watchpoint_condition (loc1->address,
6654 loc1->length,
6655 loc1->watchpoint_type,
6656 w1->cond_exp))
6657 || (w2->cond_exp
6658 && target_can_accel_watchpoint_condition (loc2->address,
6659 loc2->length,
6660 loc2->watchpoint_type,
6661 w2->cond_exp)))
6662 return 0;
6663
6664 /* Note that this checks the owner's type, not the location's. In
6665 case the target does not support read watchpoints, but does
6666 support access watchpoints, we'll have bp_read_watchpoint
6667 watchpoints with hw_access locations. Those should be considered
6668 duplicates of hw_read locations. The hw_read locations will
6669 become hw_access locations later. */
6670 return (loc1->owner->type == loc2->owner->type
6671 && loc1->pspace->aspace == loc2->pspace->aspace
6672 && loc1->address == loc2->address
6673 && loc1->length == loc2->length);
6674 }
6675
6676 /* Returns true if {ASPACE1,ADDR1} and {ASPACE2,ADDR2} represent the
6677 same breakpoint location. In most targets, this can only be true
6678 if ASPACE1 matches ASPACE2. On targets that have global
6679 breakpoints, the address space doesn't really matter. */
6680
6681 static int
6682 breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
6683 struct address_space *aspace2, CORE_ADDR addr2)
6684 {
6685 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6686 || aspace1 == aspace2)
6687 && addr1 == addr2);
6688 }
6689
6690 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6691 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6692 matches ASPACE2. On targets that have global breakpoints, the address
6693 space doesn't really matter. */
6694
6695 static int
6696 breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
6697 int len1, struct address_space *aspace2,
6698 CORE_ADDR addr2)
6699 {
6700 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6701 || aspace1 == aspace2)
6702 && addr2 >= addr1 && addr2 < addr1 + len1);
6703 }
6704
6705 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
6706 a ranged breakpoint. In most targets, a match happens only if ASPACE
6707 matches the breakpoint's address space. On targets that have global
6708 breakpoints, the address space doesn't really matter. */
6709
6710 static int
6711 breakpoint_location_address_match (struct bp_location *bl,
6712 struct address_space *aspace,
6713 CORE_ADDR addr)
6714 {
6715 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
6716 aspace, addr)
6717 || (bl->length
6718 && breakpoint_address_match_range (bl->pspace->aspace,
6719 bl->address, bl->length,
6720 aspace, addr)));
6721 }
6722
6723 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
6724 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
6725 true, otherwise returns false. */
6726
6727 static int
6728 tracepoint_locations_match (struct bp_location *loc1,
6729 struct bp_location *loc2)
6730 {
6731 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
6732 /* Since tracepoint locations are never duplicated with others', tracepoint
6733 locations at the same address of different tracepoints are regarded as
6734 different locations. */
6735 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
6736 else
6737 return 0;
6738 }
6739
6740 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
6741 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
6742 represent the same location. */
6743
6744 static int
6745 breakpoint_locations_match (struct bp_location *loc1,
6746 struct bp_location *loc2)
6747 {
6748 int hw_point1, hw_point2;
6749
6750 /* Both of them must not be in moribund_locations. */
6751 gdb_assert (loc1->owner != NULL);
6752 gdb_assert (loc2->owner != NULL);
6753
6754 hw_point1 = is_hardware_watchpoint (loc1->owner);
6755 hw_point2 = is_hardware_watchpoint (loc2->owner);
6756
6757 if (hw_point1 != hw_point2)
6758 return 0;
6759 else if (hw_point1)
6760 return watchpoint_locations_match (loc1, loc2);
6761 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
6762 return tracepoint_locations_match (loc1, loc2);
6763 else
6764 /* We compare bp_location.length in order to cover ranged breakpoints. */
6765 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
6766 loc2->pspace->aspace, loc2->address)
6767 && loc1->length == loc2->length);
6768 }
6769
6770 static void
6771 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
6772 int bnum, int have_bnum)
6773 {
6774 /* The longest string possibly returned by hex_string_custom
6775 is 50 chars. These must be at least that big for safety. */
6776 char astr1[64];
6777 char astr2[64];
6778
6779 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
6780 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
6781 if (have_bnum)
6782 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
6783 bnum, astr1, astr2);
6784 else
6785 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
6786 }
6787
6788 /* Adjust a breakpoint's address to account for architectural
6789 constraints on breakpoint placement. Return the adjusted address.
6790 Note: Very few targets require this kind of adjustment. For most
6791 targets, this function is simply the identity function. */
6792
6793 static CORE_ADDR
6794 adjust_breakpoint_address (struct gdbarch *gdbarch,
6795 CORE_ADDR bpaddr, enum bptype bptype)
6796 {
6797 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
6798 {
6799 /* Very few targets need any kind of breakpoint adjustment. */
6800 return bpaddr;
6801 }
6802 else if (bptype == bp_watchpoint
6803 || bptype == bp_hardware_watchpoint
6804 || bptype == bp_read_watchpoint
6805 || bptype == bp_access_watchpoint
6806 || bptype == bp_catchpoint)
6807 {
6808 /* Watchpoints and the various bp_catch_* eventpoints should not
6809 have their addresses modified. */
6810 return bpaddr;
6811 }
6812 else
6813 {
6814 CORE_ADDR adjusted_bpaddr;
6815
6816 /* Some targets have architectural constraints on the placement
6817 of breakpoint instructions. Obtain the adjusted address. */
6818 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
6819
6820 /* An adjusted breakpoint address can significantly alter
6821 a user's expectations. Print a warning if an adjustment
6822 is required. */
6823 if (adjusted_bpaddr != bpaddr)
6824 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
6825
6826 return adjusted_bpaddr;
6827 }
6828 }
6829
6830 void
6831 init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
6832 struct breakpoint *owner)
6833 {
6834 memset (loc, 0, sizeof (*loc));
6835
6836 gdb_assert (ops != NULL);
6837
6838 loc->ops = ops;
6839 loc->owner = owner;
6840 loc->cond = NULL;
6841 loc->cond_bytecode = NULL;
6842 loc->shlib_disabled = 0;
6843 loc->enabled = 1;
6844
6845 switch (owner->type)
6846 {
6847 case bp_breakpoint:
6848 case bp_until:
6849 case bp_finish:
6850 case bp_longjmp:
6851 case bp_longjmp_resume:
6852 case bp_longjmp_call_dummy:
6853 case bp_exception:
6854 case bp_exception_resume:
6855 case bp_step_resume:
6856 case bp_hp_step_resume:
6857 case bp_watchpoint_scope:
6858 case bp_call_dummy:
6859 case bp_std_terminate:
6860 case bp_shlib_event:
6861 case bp_thread_event:
6862 case bp_overlay_event:
6863 case bp_jit_event:
6864 case bp_longjmp_master:
6865 case bp_std_terminate_master:
6866 case bp_exception_master:
6867 case bp_gnu_ifunc_resolver:
6868 case bp_gnu_ifunc_resolver_return:
6869 case bp_dprintf:
6870 loc->loc_type = bp_loc_software_breakpoint;
6871 mark_breakpoint_location_modified (loc);
6872 break;
6873 case bp_hardware_breakpoint:
6874 loc->loc_type = bp_loc_hardware_breakpoint;
6875 mark_breakpoint_location_modified (loc);
6876 break;
6877 case bp_hardware_watchpoint:
6878 case bp_read_watchpoint:
6879 case bp_access_watchpoint:
6880 loc->loc_type = bp_loc_hardware_watchpoint;
6881 break;
6882 case bp_watchpoint:
6883 case bp_catchpoint:
6884 case bp_tracepoint:
6885 case bp_fast_tracepoint:
6886 case bp_static_tracepoint:
6887 loc->loc_type = bp_loc_other;
6888 break;
6889 default:
6890 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
6891 }
6892
6893 loc->refc = 1;
6894 }
6895
6896 /* Allocate a struct bp_location. */
6897
6898 static struct bp_location *
6899 allocate_bp_location (struct breakpoint *bpt)
6900 {
6901 return bpt->ops->allocate_location (bpt);
6902 }
6903
6904 static void
6905 free_bp_location (struct bp_location *loc)
6906 {
6907 loc->ops->dtor (loc);
6908 xfree (loc);
6909 }
6910
6911 /* Increment reference count. */
6912
6913 static void
6914 incref_bp_location (struct bp_location *bl)
6915 {
6916 ++bl->refc;
6917 }
6918
6919 /* Decrement reference count. If the reference count reaches 0,
6920 destroy the bp_location. Sets *BLP to NULL. */
6921
6922 static void
6923 decref_bp_location (struct bp_location **blp)
6924 {
6925 gdb_assert ((*blp)->refc > 0);
6926
6927 if (--(*blp)->refc == 0)
6928 free_bp_location (*blp);
6929 *blp = NULL;
6930 }
6931
6932 /* Add breakpoint B at the end of the global breakpoint chain. */
6933
6934 static void
6935 add_to_breakpoint_chain (struct breakpoint *b)
6936 {
6937 struct breakpoint *b1;
6938
6939 /* Add this breakpoint to the end of the chain so that a list of
6940 breakpoints will come out in order of increasing numbers. */
6941
6942 b1 = breakpoint_chain;
6943 if (b1 == 0)
6944 breakpoint_chain = b;
6945 else
6946 {
6947 while (b1->next)
6948 b1 = b1->next;
6949 b1->next = b;
6950 }
6951 }
6952
6953 /* Initializes breakpoint B with type BPTYPE and no locations yet. */
6954
6955 static void
6956 init_raw_breakpoint_without_location (struct breakpoint *b,
6957 struct gdbarch *gdbarch,
6958 enum bptype bptype,
6959 const struct breakpoint_ops *ops)
6960 {
6961 memset (b, 0, sizeof (*b));
6962
6963 gdb_assert (ops != NULL);
6964
6965 b->ops = ops;
6966 b->type = bptype;
6967 b->gdbarch = gdbarch;
6968 b->language = current_language->la_language;
6969 b->input_radix = input_radix;
6970 b->thread = -1;
6971 b->enable_state = bp_enabled;
6972 b->next = 0;
6973 b->silent = 0;
6974 b->ignore_count = 0;
6975 b->commands = NULL;
6976 b->frame_id = null_frame_id;
6977 b->condition_not_parsed = 0;
6978 b->py_bp_object = NULL;
6979 b->related_breakpoint = b;
6980 }
6981
6982 /* Helper to set_raw_breakpoint below. Creates a breakpoint
6983 that has type BPTYPE and has no locations as yet. */
6984
6985 static struct breakpoint *
6986 set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
6987 enum bptype bptype,
6988 const struct breakpoint_ops *ops)
6989 {
6990 struct breakpoint *b = XNEW (struct breakpoint);
6991
6992 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
6993 add_to_breakpoint_chain (b);
6994 return b;
6995 }
6996
6997 /* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
6998 resolutions should be made as the user specified the location explicitly
6999 enough. */
7000
7001 static void
7002 set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
7003 {
7004 gdb_assert (loc->owner != NULL);
7005
7006 if (loc->owner->type == bp_breakpoint
7007 || loc->owner->type == bp_hardware_breakpoint
7008 || is_tracepoint (loc->owner))
7009 {
7010 int is_gnu_ifunc;
7011 const char *function_name;
7012 CORE_ADDR func_addr;
7013
7014 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
7015 &func_addr, NULL, &is_gnu_ifunc);
7016
7017 if (is_gnu_ifunc && !explicit_loc)
7018 {
7019 struct breakpoint *b = loc->owner;
7020
7021 gdb_assert (loc->pspace == current_program_space);
7022 if (gnu_ifunc_resolve_name (function_name,
7023 &loc->requested_address))
7024 {
7025 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7026 loc->address = adjust_breakpoint_address (loc->gdbarch,
7027 loc->requested_address,
7028 b->type);
7029 }
7030 else if (b->type == bp_breakpoint && b->loc == loc
7031 && loc->next == NULL && b->related_breakpoint == b)
7032 {
7033 /* Create only the whole new breakpoint of this type but do not
7034 mess more complicated breakpoints with multiple locations. */
7035 b->type = bp_gnu_ifunc_resolver;
7036 /* Remember the resolver's address for use by the return
7037 breakpoint. */
7038 loc->related_address = func_addr;
7039 }
7040 }
7041
7042 if (function_name)
7043 loc->function_name = xstrdup (function_name);
7044 }
7045 }
7046
7047 /* Attempt to determine architecture of location identified by SAL. */
7048 struct gdbarch *
7049 get_sal_arch (struct symtab_and_line sal)
7050 {
7051 if (sal.section)
7052 return get_objfile_arch (sal.section->objfile);
7053 if (sal.symtab)
7054 return get_objfile_arch (sal.symtab->objfile);
7055
7056 return NULL;
7057 }
7058
7059 /* Low level routine for partially initializing a breakpoint of type
7060 BPTYPE. The newly created breakpoint's address, section, source
7061 file name, and line number are provided by SAL.
7062
7063 It is expected that the caller will complete the initialization of
7064 the newly created breakpoint struct as well as output any status
7065 information regarding the creation of a new breakpoint. */
7066
7067 static void
7068 init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
7069 struct symtab_and_line sal, enum bptype bptype,
7070 const struct breakpoint_ops *ops)
7071 {
7072 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7073
7074 add_location_to_breakpoint (b, &sal);
7075
7076 if (bptype != bp_catchpoint)
7077 gdb_assert (sal.pspace != NULL);
7078
7079 /* Store the program space that was used to set the breakpoint,
7080 except for ordinary breakpoints, which are independent of the
7081 program space. */
7082 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7083 b->pspace = sal.pspace;
7084 }
7085
7086 /* set_raw_breakpoint is a low level routine for allocating and
7087 partially initializing a breakpoint of type BPTYPE. The newly
7088 created breakpoint's address, section, source file name, and line
7089 number are provided by SAL. The newly created and partially
7090 initialized breakpoint is added to the breakpoint chain and
7091 is also returned as the value of this function.
7092
7093 It is expected that the caller will complete the initialization of
7094 the newly created breakpoint struct as well as output any status
7095 information regarding the creation of a new breakpoint. In
7096 particular, set_raw_breakpoint does NOT set the breakpoint
7097 number! Care should be taken to not allow an error to occur
7098 prior to completing the initialization of the breakpoint. If this
7099 should happen, a bogus breakpoint will be left on the chain. */
7100
7101 struct breakpoint *
7102 set_raw_breakpoint (struct gdbarch *gdbarch,
7103 struct symtab_and_line sal, enum bptype bptype,
7104 const struct breakpoint_ops *ops)
7105 {
7106 struct breakpoint *b = XNEW (struct breakpoint);
7107
7108 init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
7109 add_to_breakpoint_chain (b);
7110 return b;
7111 }
7112
7113
7114 /* Note that the breakpoint object B describes a permanent breakpoint
7115 instruction, hard-wired into the inferior's code. */
7116 void
7117 make_breakpoint_permanent (struct breakpoint *b)
7118 {
7119 struct bp_location *bl;
7120
7121 b->enable_state = bp_permanent;
7122
7123 /* By definition, permanent breakpoints are already present in the
7124 code. Mark all locations as inserted. For now,
7125 make_breakpoint_permanent is called in just one place, so it's
7126 hard to say if it's reasonable to have permanent breakpoint with
7127 multiple locations or not, but it's easy to implement. */
7128 for (bl = b->loc; bl; bl = bl->next)
7129 bl->inserted = 1;
7130 }
7131
7132 /* Call this routine when stepping and nexting to enable a breakpoint
7133 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7134 initiated the operation. */
7135
7136 void
7137 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
7138 {
7139 struct breakpoint *b, *b_tmp;
7140 int thread = tp->num;
7141
7142 /* To avoid having to rescan all objfile symbols at every step,
7143 we maintain a list of continually-inserted but always disabled
7144 longjmp "master" breakpoints. Here, we simply create momentary
7145 clones of those and enable them for the requested thread. */
7146 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7147 if (b->pspace == current_program_space
7148 && (b->type == bp_longjmp_master
7149 || b->type == bp_exception_master))
7150 {
7151 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7152 struct breakpoint *clone;
7153
7154 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7155 after their removal. */
7156 clone = momentary_breakpoint_from_master (b, type,
7157 &longjmp_breakpoint_ops);
7158 clone->thread = thread;
7159 }
7160
7161 tp->initiating_frame = frame;
7162 }
7163
7164 /* Delete all longjmp breakpoints from THREAD. */
7165 void
7166 delete_longjmp_breakpoint (int thread)
7167 {
7168 struct breakpoint *b, *b_tmp;
7169
7170 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7171 if (b->type == bp_longjmp || b->type == bp_exception)
7172 {
7173 if (b->thread == thread)
7174 delete_breakpoint (b);
7175 }
7176 }
7177
7178 void
7179 delete_longjmp_breakpoint_at_next_stop (int thread)
7180 {
7181 struct breakpoint *b, *b_tmp;
7182
7183 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7184 if (b->type == bp_longjmp || b->type == bp_exception)
7185 {
7186 if (b->thread == thread)
7187 b->disposition = disp_del_at_next_stop;
7188 }
7189 }
7190
7191 /* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7192 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7193 pointer to any of them. Return NULL if this system cannot place longjmp
7194 breakpoints. */
7195
7196 struct breakpoint *
7197 set_longjmp_breakpoint_for_call_dummy (void)
7198 {
7199 struct breakpoint *b, *retval = NULL;
7200
7201 ALL_BREAKPOINTS (b)
7202 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7203 {
7204 struct breakpoint *new_b;
7205
7206 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7207 &momentary_breakpoint_ops);
7208 new_b->thread = pid_to_thread_id (inferior_ptid);
7209
7210 /* Link NEW_B into the chain of RETVAL breakpoints. */
7211
7212 gdb_assert (new_b->related_breakpoint == new_b);
7213 if (retval == NULL)
7214 retval = new_b;
7215 new_b->related_breakpoint = retval;
7216 while (retval->related_breakpoint != new_b->related_breakpoint)
7217 retval = retval->related_breakpoint;
7218 retval->related_breakpoint = new_b;
7219 }
7220
7221 return retval;
7222 }
7223
7224 /* Verify all existing dummy frames and their associated breakpoints for
7225 THREAD. Remove those which can no longer be found in the current frame
7226 stack.
7227
7228 You should call this function only at places where it is safe to currently
7229 unwind the whole stack. Failed stack unwind would discard live dummy
7230 frames. */
7231
7232 void
7233 check_longjmp_breakpoint_for_call_dummy (int thread)
7234 {
7235 struct breakpoint *b, *b_tmp;
7236
7237 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7238 if (b->type == bp_longjmp_call_dummy && b->thread == thread)
7239 {
7240 struct breakpoint *dummy_b = b->related_breakpoint;
7241
7242 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7243 dummy_b = dummy_b->related_breakpoint;
7244 if (dummy_b->type != bp_call_dummy
7245 || frame_find_by_id (dummy_b->frame_id) != NULL)
7246 continue;
7247
7248 dummy_frame_discard (dummy_b->frame_id);
7249
7250 while (b->related_breakpoint != b)
7251 {
7252 if (b_tmp == b->related_breakpoint)
7253 b_tmp = b->related_breakpoint->next;
7254 delete_breakpoint (b->related_breakpoint);
7255 }
7256 delete_breakpoint (b);
7257 }
7258 }
7259
7260 void
7261 enable_overlay_breakpoints (void)
7262 {
7263 struct breakpoint *b;
7264
7265 ALL_BREAKPOINTS (b)
7266 if (b->type == bp_overlay_event)
7267 {
7268 b->enable_state = bp_enabled;
7269 update_global_location_list (1);
7270 overlay_events_enabled = 1;
7271 }
7272 }
7273
7274 void
7275 disable_overlay_breakpoints (void)
7276 {
7277 struct breakpoint *b;
7278
7279 ALL_BREAKPOINTS (b)
7280 if (b->type == bp_overlay_event)
7281 {
7282 b->enable_state = bp_disabled;
7283 update_global_location_list (0);
7284 overlay_events_enabled = 0;
7285 }
7286 }
7287
7288 /* Set an active std::terminate breakpoint for each std::terminate
7289 master breakpoint. */
7290 void
7291 set_std_terminate_breakpoint (void)
7292 {
7293 struct breakpoint *b, *b_tmp;
7294
7295 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7296 if (b->pspace == current_program_space
7297 && b->type == bp_std_terminate_master)
7298 {
7299 momentary_breakpoint_from_master (b, bp_std_terminate,
7300 &momentary_breakpoint_ops);
7301 }
7302 }
7303
7304 /* Delete all the std::terminate breakpoints. */
7305 void
7306 delete_std_terminate_breakpoint (void)
7307 {
7308 struct breakpoint *b, *b_tmp;
7309
7310 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7311 if (b->type == bp_std_terminate)
7312 delete_breakpoint (b);
7313 }
7314
7315 struct breakpoint *
7316 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7317 {
7318 struct breakpoint *b;
7319
7320 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7321 &internal_breakpoint_ops);
7322
7323 b->enable_state = bp_enabled;
7324 /* addr_string has to be used or breakpoint_re_set will delete me. */
7325 b->addr_string
7326 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
7327
7328 update_global_location_list_nothrow (1);
7329
7330 return b;
7331 }
7332
7333 void
7334 remove_thread_event_breakpoints (void)
7335 {
7336 struct breakpoint *b, *b_tmp;
7337
7338 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7339 if (b->type == bp_thread_event
7340 && b->loc->pspace == current_program_space)
7341 delete_breakpoint (b);
7342 }
7343
7344 struct lang_and_radix
7345 {
7346 enum language lang;
7347 int radix;
7348 };
7349
7350 /* Create a breakpoint for JIT code registration and unregistration. */
7351
7352 struct breakpoint *
7353 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7354 {
7355 struct breakpoint *b;
7356
7357 b = create_internal_breakpoint (gdbarch, address, bp_jit_event,
7358 &internal_breakpoint_ops);
7359 update_global_location_list_nothrow (1);
7360 return b;
7361 }
7362
7363 /* Remove JIT code registration and unregistration breakpoint(s). */
7364
7365 void
7366 remove_jit_event_breakpoints (void)
7367 {
7368 struct breakpoint *b, *b_tmp;
7369
7370 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7371 if (b->type == bp_jit_event
7372 && b->loc->pspace == current_program_space)
7373 delete_breakpoint (b);
7374 }
7375
7376 void
7377 remove_solib_event_breakpoints (void)
7378 {
7379 struct breakpoint *b, *b_tmp;
7380
7381 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7382 if (b->type == bp_shlib_event
7383 && b->loc->pspace == current_program_space)
7384 delete_breakpoint (b);
7385 }
7386
7387 struct breakpoint *
7388 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7389 {
7390 struct breakpoint *b;
7391
7392 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7393 &internal_breakpoint_ops);
7394 update_global_location_list_nothrow (1);
7395 return b;
7396 }
7397
7398 /* Disable any breakpoints that are on code in shared libraries. Only
7399 apply to enabled breakpoints, disabled ones can just stay disabled. */
7400
7401 void
7402 disable_breakpoints_in_shlibs (void)
7403 {
7404 struct bp_location *loc, **locp_tmp;
7405
7406 ALL_BP_LOCATIONS (loc, locp_tmp)
7407 {
7408 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7409 struct breakpoint *b = loc->owner;
7410
7411 /* We apply the check to all breakpoints, including disabled for
7412 those with loc->duplicate set. This is so that when breakpoint
7413 becomes enabled, or the duplicate is removed, gdb will try to
7414 insert all breakpoints. If we don't set shlib_disabled here,
7415 we'll try to insert those breakpoints and fail. */
7416 if (((b->type == bp_breakpoint)
7417 || (b->type == bp_jit_event)
7418 || (b->type == bp_hardware_breakpoint)
7419 || (is_tracepoint (b)))
7420 && loc->pspace == current_program_space
7421 && !loc->shlib_disabled
7422 #ifdef PC_SOLIB
7423 && PC_SOLIB (loc->address)
7424 #else
7425 && solib_name_from_address (loc->pspace, loc->address)
7426 #endif
7427 )
7428 {
7429 loc->shlib_disabled = 1;
7430 }
7431 }
7432 }
7433
7434 /* Disable any breakpoints and tracepoints that are in an unloaded shared
7435 library. Only apply to enabled breakpoints, disabled ones can just stay
7436 disabled. */
7437
7438 static void
7439 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7440 {
7441 struct bp_location *loc, **locp_tmp;
7442 int disabled_shlib_breaks = 0;
7443
7444 /* SunOS a.out shared libraries are always mapped, so do not
7445 disable breakpoints; they will only be reported as unloaded
7446 through clear_solib when GDB discards its shared library
7447 list. See clear_solib for more information. */
7448 if (exec_bfd != NULL
7449 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
7450 return;
7451
7452 ALL_BP_LOCATIONS (loc, locp_tmp)
7453 {
7454 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7455 struct breakpoint *b = loc->owner;
7456
7457 if (solib->pspace == loc->pspace
7458 && !loc->shlib_disabled
7459 && (((b->type == bp_breakpoint
7460 || b->type == bp_jit_event
7461 || b->type == bp_hardware_breakpoint)
7462 && (loc->loc_type == bp_loc_hardware_breakpoint
7463 || loc->loc_type == bp_loc_software_breakpoint))
7464 || is_tracepoint (b))
7465 && solib_contains_address_p (solib, loc->address))
7466 {
7467 loc->shlib_disabled = 1;
7468 /* At this point, we cannot rely on remove_breakpoint
7469 succeeding so we must mark the breakpoint as not inserted
7470 to prevent future errors occurring in remove_breakpoints. */
7471 loc->inserted = 0;
7472
7473 /* This may cause duplicate notifications for the same breakpoint. */
7474 observer_notify_breakpoint_modified (b);
7475
7476 if (!disabled_shlib_breaks)
7477 {
7478 target_terminal_ours_for_output ();
7479 warning (_("Temporarily disabling breakpoints "
7480 "for unloaded shared library \"%s\""),
7481 solib->so_name);
7482 }
7483 disabled_shlib_breaks = 1;
7484 }
7485 }
7486 }
7487
7488 /* FORK & VFORK catchpoints. */
7489
7490 /* An instance of this type is used to represent a fork or vfork
7491 catchpoint. It includes a "struct breakpoint" as a kind of base
7492 class; users downcast to "struct breakpoint *" when needed. A
7493 breakpoint is really of this type iff its ops pointer points to
7494 CATCH_FORK_BREAKPOINT_OPS. */
7495
7496 struct fork_catchpoint
7497 {
7498 /* The base class. */
7499 struct breakpoint base;
7500
7501 /* Process id of a child process whose forking triggered this
7502 catchpoint. This field is only valid immediately after this
7503 catchpoint has triggered. */
7504 ptid_t forked_inferior_pid;
7505 };
7506
7507 /* Implement the "insert" breakpoint_ops method for fork
7508 catchpoints. */
7509
7510 static int
7511 insert_catch_fork (struct bp_location *bl)
7512 {
7513 return target_insert_fork_catchpoint (PIDGET (inferior_ptid));
7514 }
7515
7516 /* Implement the "remove" breakpoint_ops method for fork
7517 catchpoints. */
7518
7519 static int
7520 remove_catch_fork (struct bp_location *bl)
7521 {
7522 return target_remove_fork_catchpoint (PIDGET (inferior_ptid));
7523 }
7524
7525 /* Implement the "breakpoint_hit" breakpoint_ops method for fork
7526 catchpoints. */
7527
7528 static int
7529 breakpoint_hit_catch_fork (const struct bp_location *bl,
7530 struct address_space *aspace, CORE_ADDR bp_addr,
7531 const struct target_waitstatus *ws)
7532 {
7533 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7534
7535 if (ws->kind != TARGET_WAITKIND_FORKED)
7536 return 0;
7537
7538 c->forked_inferior_pid = ws->value.related_pid;
7539 return 1;
7540 }
7541
7542 /* Implement the "print_it" breakpoint_ops method for fork
7543 catchpoints. */
7544
7545 static enum print_stop_action
7546 print_it_catch_fork (bpstat bs)
7547 {
7548 struct ui_out *uiout = current_uiout;
7549 struct breakpoint *b = bs->breakpoint_at;
7550 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
7551
7552 annotate_catchpoint (b->number);
7553 if (b->disposition == disp_del)
7554 ui_out_text (uiout, "\nTemporary catchpoint ");
7555 else
7556 ui_out_text (uiout, "\nCatchpoint ");
7557 if (ui_out_is_mi_like_p (uiout))
7558 {
7559 ui_out_field_string (uiout, "reason",
7560 async_reason_lookup (EXEC_ASYNC_FORK));
7561 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7562 }
7563 ui_out_field_int (uiout, "bkptno", b->number);
7564 ui_out_text (uiout, " (forked process ");
7565 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
7566 ui_out_text (uiout, "), ");
7567 return PRINT_SRC_AND_LOC;
7568 }
7569
7570 /* Implement the "print_one" breakpoint_ops method for fork
7571 catchpoints. */
7572
7573 static void
7574 print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
7575 {
7576 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7577 struct value_print_options opts;
7578 struct ui_out *uiout = current_uiout;
7579
7580 get_user_print_options (&opts);
7581
7582 /* Field 4, the address, is omitted (which makes the columns not
7583 line up too nicely with the headers, but the effect is relatively
7584 readable). */
7585 if (opts.addressprint)
7586 ui_out_field_skip (uiout, "addr");
7587 annotate_field (5);
7588 ui_out_text (uiout, "fork");
7589 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
7590 {
7591 ui_out_text (uiout, ", process ");
7592 ui_out_field_int (uiout, "what",
7593 ptid_get_pid (c->forked_inferior_pid));
7594 ui_out_spaces (uiout, 1);
7595 }
7596
7597 if (ui_out_is_mi_like_p (uiout))
7598 ui_out_field_string (uiout, "catch-type", "fork");
7599 }
7600
7601 /* Implement the "print_mention" breakpoint_ops method for fork
7602 catchpoints. */
7603
7604 static void
7605 print_mention_catch_fork (struct breakpoint *b)
7606 {
7607 printf_filtered (_("Catchpoint %d (fork)"), b->number);
7608 }
7609
7610 /* Implement the "print_recreate" breakpoint_ops method for fork
7611 catchpoints. */
7612
7613 static void
7614 print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
7615 {
7616 fprintf_unfiltered (fp, "catch fork");
7617 print_recreate_thread (b, fp);
7618 }
7619
7620 /* The breakpoint_ops structure to be used in fork catchpoints. */
7621
7622 static struct breakpoint_ops catch_fork_breakpoint_ops;
7623
7624 /* Implement the "insert" breakpoint_ops method for vfork
7625 catchpoints. */
7626
7627 static int
7628 insert_catch_vfork (struct bp_location *bl)
7629 {
7630 return target_insert_vfork_catchpoint (PIDGET (inferior_ptid));
7631 }
7632
7633 /* Implement the "remove" breakpoint_ops method for vfork
7634 catchpoints. */
7635
7636 static int
7637 remove_catch_vfork (struct bp_location *bl)
7638 {
7639 return target_remove_vfork_catchpoint (PIDGET (inferior_ptid));
7640 }
7641
7642 /* Implement the "breakpoint_hit" breakpoint_ops method for vfork
7643 catchpoints. */
7644
7645 static int
7646 breakpoint_hit_catch_vfork (const struct bp_location *bl,
7647 struct address_space *aspace, CORE_ADDR bp_addr,
7648 const struct target_waitstatus *ws)
7649 {
7650 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7651
7652 if (ws->kind != TARGET_WAITKIND_VFORKED)
7653 return 0;
7654
7655 c->forked_inferior_pid = ws->value.related_pid;
7656 return 1;
7657 }
7658
7659 /* Implement the "print_it" breakpoint_ops method for vfork
7660 catchpoints. */
7661
7662 static enum print_stop_action
7663 print_it_catch_vfork (bpstat bs)
7664 {
7665 struct ui_out *uiout = current_uiout;
7666 struct breakpoint *b = bs->breakpoint_at;
7667 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7668
7669 annotate_catchpoint (b->number);
7670 if (b->disposition == disp_del)
7671 ui_out_text (uiout, "\nTemporary catchpoint ");
7672 else
7673 ui_out_text (uiout, "\nCatchpoint ");
7674 if (ui_out_is_mi_like_p (uiout))
7675 {
7676 ui_out_field_string (uiout, "reason",
7677 async_reason_lookup (EXEC_ASYNC_VFORK));
7678 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7679 }
7680 ui_out_field_int (uiout, "bkptno", b->number);
7681 ui_out_text (uiout, " (vforked process ");
7682 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
7683 ui_out_text (uiout, "), ");
7684 return PRINT_SRC_AND_LOC;
7685 }
7686
7687 /* Implement the "print_one" breakpoint_ops method for vfork
7688 catchpoints. */
7689
7690 static void
7691 print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
7692 {
7693 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7694 struct value_print_options opts;
7695 struct ui_out *uiout = current_uiout;
7696
7697 get_user_print_options (&opts);
7698 /* Field 4, the address, is omitted (which makes the columns not
7699 line up too nicely with the headers, but the effect is relatively
7700 readable). */
7701 if (opts.addressprint)
7702 ui_out_field_skip (uiout, "addr");
7703 annotate_field (5);
7704 ui_out_text (uiout, "vfork");
7705 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
7706 {
7707 ui_out_text (uiout, ", process ");
7708 ui_out_field_int (uiout, "what",
7709 ptid_get_pid (c->forked_inferior_pid));
7710 ui_out_spaces (uiout, 1);
7711 }
7712
7713 if (ui_out_is_mi_like_p (uiout))
7714 ui_out_field_string (uiout, "catch-type", "vfork");
7715 }
7716
7717 /* Implement the "print_mention" breakpoint_ops method for vfork
7718 catchpoints. */
7719
7720 static void
7721 print_mention_catch_vfork (struct breakpoint *b)
7722 {
7723 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
7724 }
7725
7726 /* Implement the "print_recreate" breakpoint_ops method for vfork
7727 catchpoints. */
7728
7729 static void
7730 print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
7731 {
7732 fprintf_unfiltered (fp, "catch vfork");
7733 print_recreate_thread (b, fp);
7734 }
7735
7736 /* The breakpoint_ops structure to be used in vfork catchpoints. */
7737
7738 static struct breakpoint_ops catch_vfork_breakpoint_ops;
7739
7740 /* An instance of this type is used to represent an solib catchpoint.
7741 It includes a "struct breakpoint" as a kind of base class; users
7742 downcast to "struct breakpoint *" when needed. A breakpoint is
7743 really of this type iff its ops pointer points to
7744 CATCH_SOLIB_BREAKPOINT_OPS. */
7745
7746 struct solib_catchpoint
7747 {
7748 /* The base class. */
7749 struct breakpoint base;
7750
7751 /* True for "catch load", false for "catch unload". */
7752 unsigned char is_load;
7753
7754 /* Regular expression to match, if any. COMPILED is only valid when
7755 REGEX is non-NULL. */
7756 char *regex;
7757 regex_t compiled;
7758 };
7759
7760 static void
7761 dtor_catch_solib (struct breakpoint *b)
7762 {
7763 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
7764
7765 if (self->regex)
7766 regfree (&self->compiled);
7767 xfree (self->regex);
7768
7769 base_breakpoint_ops.dtor (b);
7770 }
7771
7772 static int
7773 insert_catch_solib (struct bp_location *ignore)
7774 {
7775 return 0;
7776 }
7777
7778 static int
7779 remove_catch_solib (struct bp_location *ignore)
7780 {
7781 return 0;
7782 }
7783
7784 static int
7785 breakpoint_hit_catch_solib (const struct bp_location *bl,
7786 struct address_space *aspace,
7787 CORE_ADDR bp_addr,
7788 const struct target_waitstatus *ws)
7789 {
7790 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
7791 struct breakpoint *other;
7792
7793 if (ws->kind == TARGET_WAITKIND_LOADED)
7794 return 1;
7795
7796 ALL_BREAKPOINTS (other)
7797 {
7798 struct bp_location *other_bl;
7799
7800 if (other == bl->owner)
7801 continue;
7802
7803 if (other->type != bp_shlib_event)
7804 continue;
7805
7806 if (self->base.pspace != NULL && other->pspace != self->base.pspace)
7807 continue;
7808
7809 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
7810 {
7811 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
7812 return 1;
7813 }
7814 }
7815
7816 return 0;
7817 }
7818
7819 static void
7820 check_status_catch_solib (struct bpstats *bs)
7821 {
7822 struct solib_catchpoint *self
7823 = (struct solib_catchpoint *) bs->breakpoint_at;
7824 int ix;
7825
7826 if (self->is_load)
7827 {
7828 struct so_list *iter;
7829
7830 for (ix = 0;
7831 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
7832 ix, iter);
7833 ++ix)
7834 {
7835 if (!self->regex
7836 || regexec (&self->compiled, iter->so_name, 0, NULL, 0) == 0)
7837 return;
7838 }
7839 }
7840 else
7841 {
7842 char *iter;
7843
7844 for (ix = 0;
7845 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
7846 ix, iter);
7847 ++ix)
7848 {
7849 if (!self->regex
7850 || regexec (&self->compiled, iter, 0, NULL, 0) == 0)
7851 return;
7852 }
7853 }
7854
7855 bs->stop = 0;
7856 bs->print_it = print_it_noop;
7857 }
7858
7859 static enum print_stop_action
7860 print_it_catch_solib (bpstat bs)
7861 {
7862 struct breakpoint *b = bs->breakpoint_at;
7863 struct ui_out *uiout = current_uiout;
7864
7865 annotate_catchpoint (b->number);
7866 if (b->disposition == disp_del)
7867 ui_out_text (uiout, "\nTemporary catchpoint ");
7868 else
7869 ui_out_text (uiout, "\nCatchpoint ");
7870 ui_out_field_int (uiout, "bkptno", b->number);
7871 ui_out_text (uiout, "\n");
7872 if (ui_out_is_mi_like_p (uiout))
7873 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7874 print_solib_event (1);
7875 return PRINT_SRC_AND_LOC;
7876 }
7877
7878 static void
7879 print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
7880 {
7881 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
7882 struct value_print_options opts;
7883 struct ui_out *uiout = current_uiout;
7884 char *msg;
7885
7886 get_user_print_options (&opts);
7887 /* Field 4, the address, is omitted (which makes the columns not
7888 line up too nicely with the headers, but the effect is relatively
7889 readable). */
7890 if (opts.addressprint)
7891 {
7892 annotate_field (4);
7893 ui_out_field_skip (uiout, "addr");
7894 }
7895
7896 annotate_field (5);
7897 if (self->is_load)
7898 {
7899 if (self->regex)
7900 msg = xstrprintf (_("load of library matching %s"), self->regex);
7901 else
7902 msg = xstrdup (_("load of library"));
7903 }
7904 else
7905 {
7906 if (self->regex)
7907 msg = xstrprintf (_("unload of library matching %s"), self->regex);
7908 else
7909 msg = xstrdup (_("unload of library"));
7910 }
7911 ui_out_field_string (uiout, "what", msg);
7912 xfree (msg);
7913
7914 if (ui_out_is_mi_like_p (uiout))
7915 ui_out_field_string (uiout, "catch-type",
7916 self->is_load ? "load" : "unload");
7917 }
7918
7919 static void
7920 print_mention_catch_solib (struct breakpoint *b)
7921 {
7922 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
7923
7924 printf_filtered (_("Catchpoint %d (%s)"), b->number,
7925 self->is_load ? "load" : "unload");
7926 }
7927
7928 static void
7929 print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
7930 {
7931 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
7932
7933 fprintf_unfiltered (fp, "%s %s",
7934 b->disposition == disp_del ? "tcatch" : "catch",
7935 self->is_load ? "load" : "unload");
7936 if (self->regex)
7937 fprintf_unfiltered (fp, " %s", self->regex);
7938 fprintf_unfiltered (fp, "\n");
7939 }
7940
7941 static struct breakpoint_ops catch_solib_breakpoint_ops;
7942
7943 /* Shared helper function (MI and CLI) for creating and installing
7944 a shared object event catchpoint. If IS_LOAD is non-zero then
7945 the events to be caught are load events, otherwise they are
7946 unload events. If IS_TEMP is non-zero the catchpoint is a
7947 temporary one. If ENABLED is non-zero the catchpoint is
7948 created in an enabled state. */
7949
7950 void
7951 add_solib_catchpoint (char *arg, int is_load, int is_temp, int enabled)
7952 {
7953 struct solib_catchpoint *c;
7954 struct gdbarch *gdbarch = get_current_arch ();
7955 struct cleanup *cleanup;
7956
7957 if (!arg)
7958 arg = "";
7959 arg = skip_spaces (arg);
7960
7961 c = XCNEW (struct solib_catchpoint);
7962 cleanup = make_cleanup (xfree, c);
7963
7964 if (*arg != '\0')
7965 {
7966 int errcode;
7967
7968 errcode = regcomp (&c->compiled, arg, REG_NOSUB);
7969 if (errcode != 0)
7970 {
7971 char *err = get_regcomp_error (errcode, &c->compiled);
7972
7973 make_cleanup (xfree, err);
7974 error (_("Invalid regexp (%s): %s"), err, arg);
7975 }
7976 c->regex = xstrdup (arg);
7977 }
7978
7979 c->is_load = is_load;
7980 init_catchpoint (&c->base, gdbarch, is_temp, NULL,
7981 &catch_solib_breakpoint_ops);
7982
7983 c->base.enable_state = enabled ? bp_enabled : bp_disabled;
7984
7985 discard_cleanups (cleanup);
7986 install_breakpoint (0, &c->base, 1);
7987 }
7988
7989 /* A helper function that does all the work for "catch load" and
7990 "catch unload". */
7991
7992 static void
7993 catch_load_or_unload (char *arg, int from_tty, int is_load,
7994 struct cmd_list_element *command)
7995 {
7996 int tempflag;
7997 const int enabled = 1;
7998
7999 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8000
8001 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8002 }
8003
8004 static void
8005 catch_load_command_1 (char *arg, int from_tty,
8006 struct cmd_list_element *command)
8007 {
8008 catch_load_or_unload (arg, from_tty, 1, command);
8009 }
8010
8011 static void
8012 catch_unload_command_1 (char *arg, int from_tty,
8013 struct cmd_list_element *command)
8014 {
8015 catch_load_or_unload (arg, from_tty, 0, command);
8016 }
8017
8018 /* An instance of this type is used to represent a syscall catchpoint.
8019 It includes a "struct breakpoint" as a kind of base class; users
8020 downcast to "struct breakpoint *" when needed. A breakpoint is
8021 really of this type iff its ops pointer points to
8022 CATCH_SYSCALL_BREAKPOINT_OPS. */
8023
8024 struct syscall_catchpoint
8025 {
8026 /* The base class. */
8027 struct breakpoint base;
8028
8029 /* Syscall numbers used for the 'catch syscall' feature. If no
8030 syscall has been specified for filtering, its value is NULL.
8031 Otherwise, it holds a list of all syscalls to be caught. The
8032 list elements are allocated with xmalloc. */
8033 VEC(int) *syscalls_to_be_caught;
8034 };
8035
8036 /* Implement the "dtor" breakpoint_ops method for syscall
8037 catchpoints. */
8038
8039 static void
8040 dtor_catch_syscall (struct breakpoint *b)
8041 {
8042 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8043
8044 VEC_free (int, c->syscalls_to_be_caught);
8045
8046 base_breakpoint_ops.dtor (b);
8047 }
8048
8049 static const struct inferior_data *catch_syscall_inferior_data = NULL;
8050
8051 struct catch_syscall_inferior_data
8052 {
8053 /* We keep a count of the number of times the user has requested a
8054 particular syscall to be tracked, and pass this information to the
8055 target. This lets capable targets implement filtering directly. */
8056
8057 /* Number of times that "any" syscall is requested. */
8058 int any_syscall_count;
8059
8060 /* Count of each system call. */
8061 VEC(int) *syscalls_counts;
8062
8063 /* This counts all syscall catch requests, so we can readily determine
8064 if any catching is necessary. */
8065 int total_syscalls_count;
8066 };
8067
8068 static struct catch_syscall_inferior_data*
8069 get_catch_syscall_inferior_data (struct inferior *inf)
8070 {
8071 struct catch_syscall_inferior_data *inf_data;
8072
8073 inf_data = inferior_data (inf, catch_syscall_inferior_data);
8074 if (inf_data == NULL)
8075 {
8076 inf_data = XZALLOC (struct catch_syscall_inferior_data);
8077 set_inferior_data (inf, catch_syscall_inferior_data, inf_data);
8078 }
8079
8080 return inf_data;
8081 }
8082
8083 static void
8084 catch_syscall_inferior_data_cleanup (struct inferior *inf, void *arg)
8085 {
8086 xfree (arg);
8087 }
8088
8089
8090 /* Implement the "insert" breakpoint_ops method for syscall
8091 catchpoints. */
8092
8093 static int
8094 insert_catch_syscall (struct bp_location *bl)
8095 {
8096 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
8097 struct inferior *inf = current_inferior ();
8098 struct catch_syscall_inferior_data *inf_data
8099 = get_catch_syscall_inferior_data (inf);
8100
8101 ++inf_data->total_syscalls_count;
8102 if (!c->syscalls_to_be_caught)
8103 ++inf_data->any_syscall_count;
8104 else
8105 {
8106 int i, iter;
8107
8108 for (i = 0;
8109 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
8110 i++)
8111 {
8112 int elem;
8113
8114 if (iter >= VEC_length (int, inf_data->syscalls_counts))
8115 {
8116 int old_size = VEC_length (int, inf_data->syscalls_counts);
8117 uintptr_t vec_addr_offset
8118 = old_size * ((uintptr_t) sizeof (int));
8119 uintptr_t vec_addr;
8120 VEC_safe_grow (int, inf_data->syscalls_counts, iter + 1);
8121 vec_addr = ((uintptr_t) VEC_address (int,
8122 inf_data->syscalls_counts)
8123 + vec_addr_offset);
8124 memset ((void *) vec_addr, 0,
8125 (iter + 1 - old_size) * sizeof (int));
8126 }
8127 elem = VEC_index (int, inf_data->syscalls_counts, iter);
8128 VEC_replace (int, inf_data->syscalls_counts, iter, ++elem);
8129 }
8130 }
8131
8132 return target_set_syscall_catchpoint (PIDGET (inferior_ptid),
8133 inf_data->total_syscalls_count != 0,
8134 inf_data->any_syscall_count,
8135 VEC_length (int,
8136 inf_data->syscalls_counts),
8137 VEC_address (int,
8138 inf_data->syscalls_counts));
8139 }
8140
8141 /* Implement the "remove" breakpoint_ops method for syscall
8142 catchpoints. */
8143
8144 static int
8145 remove_catch_syscall (struct bp_location *bl)
8146 {
8147 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
8148 struct inferior *inf = current_inferior ();
8149 struct catch_syscall_inferior_data *inf_data
8150 = get_catch_syscall_inferior_data (inf);
8151
8152 --inf_data->total_syscalls_count;
8153 if (!c->syscalls_to_be_caught)
8154 --inf_data->any_syscall_count;
8155 else
8156 {
8157 int i, iter;
8158
8159 for (i = 0;
8160 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
8161 i++)
8162 {
8163 int elem;
8164 if (iter >= VEC_length (int, inf_data->syscalls_counts))
8165 /* Shouldn't happen. */
8166 continue;
8167 elem = VEC_index (int, inf_data->syscalls_counts, iter);
8168 VEC_replace (int, inf_data->syscalls_counts, iter, --elem);
8169 }
8170 }
8171
8172 return target_set_syscall_catchpoint (PIDGET (inferior_ptid),
8173 inf_data->total_syscalls_count != 0,
8174 inf_data->any_syscall_count,
8175 VEC_length (int,
8176 inf_data->syscalls_counts),
8177 VEC_address (int,
8178 inf_data->syscalls_counts));
8179 }
8180
8181 /* Implement the "breakpoint_hit" breakpoint_ops method for syscall
8182 catchpoints. */
8183
8184 static int
8185 breakpoint_hit_catch_syscall (const struct bp_location *bl,
8186 struct address_space *aspace, CORE_ADDR bp_addr,
8187 const struct target_waitstatus *ws)
8188 {
8189 /* We must check if we are catching specific syscalls in this
8190 breakpoint. If we are, then we must guarantee that the called
8191 syscall is the same syscall we are catching. */
8192 int syscall_number = 0;
8193 const struct syscall_catchpoint *c
8194 = (const struct syscall_catchpoint *) bl->owner;
8195
8196 if (ws->kind != TARGET_WAITKIND_SYSCALL_ENTRY
8197 && ws->kind != TARGET_WAITKIND_SYSCALL_RETURN)
8198 return 0;
8199
8200 syscall_number = ws->value.syscall_number;
8201
8202 /* Now, checking if the syscall is the same. */
8203 if (c->syscalls_to_be_caught)
8204 {
8205 int i, iter;
8206
8207 for (i = 0;
8208 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
8209 i++)
8210 if (syscall_number == iter)
8211 break;
8212 /* Not the same. */
8213 if (!iter)
8214 return 0;
8215 }
8216
8217 return 1;
8218 }
8219
8220 /* Implement the "print_it" breakpoint_ops method for syscall
8221 catchpoints. */
8222
8223 static enum print_stop_action
8224 print_it_catch_syscall (bpstat bs)
8225 {
8226 struct ui_out *uiout = current_uiout;
8227 struct breakpoint *b = bs->breakpoint_at;
8228 /* These are needed because we want to know in which state a
8229 syscall is. It can be in the TARGET_WAITKIND_SYSCALL_ENTRY
8230 or TARGET_WAITKIND_SYSCALL_RETURN, and depending on it we
8231 must print "called syscall" or "returned from syscall". */
8232 ptid_t ptid;
8233 struct target_waitstatus last;
8234 struct syscall s;
8235
8236 get_last_target_status (&ptid, &last);
8237
8238 get_syscall_by_number (last.value.syscall_number, &s);
8239
8240 annotate_catchpoint (b->number);
8241
8242 if (b->disposition == disp_del)
8243 ui_out_text (uiout, "\nTemporary catchpoint ");
8244 else
8245 ui_out_text (uiout, "\nCatchpoint ");
8246 if (ui_out_is_mi_like_p (uiout))
8247 {
8248 ui_out_field_string (uiout, "reason",
8249 async_reason_lookup (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY
8250 ? EXEC_ASYNC_SYSCALL_ENTRY
8251 : EXEC_ASYNC_SYSCALL_RETURN));
8252 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8253 }
8254 ui_out_field_int (uiout, "bkptno", b->number);
8255
8256 if (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY)
8257 ui_out_text (uiout, " (call to syscall ");
8258 else
8259 ui_out_text (uiout, " (returned from syscall ");
8260
8261 if (s.name == NULL || ui_out_is_mi_like_p (uiout))
8262 ui_out_field_int (uiout, "syscall-number", last.value.syscall_number);
8263 if (s.name != NULL)
8264 ui_out_field_string (uiout, "syscall-name", s.name);
8265
8266 ui_out_text (uiout, "), ");
8267
8268 return PRINT_SRC_AND_LOC;
8269 }
8270
8271 /* Implement the "print_one" breakpoint_ops method for syscall
8272 catchpoints. */
8273
8274 static void
8275 print_one_catch_syscall (struct breakpoint *b,
8276 struct bp_location **last_loc)
8277 {
8278 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8279 struct value_print_options opts;
8280 struct ui_out *uiout = current_uiout;
8281
8282 get_user_print_options (&opts);
8283 /* Field 4, the address, is omitted (which makes the columns not
8284 line up too nicely with the headers, but the effect is relatively
8285 readable). */
8286 if (opts.addressprint)
8287 ui_out_field_skip (uiout, "addr");
8288 annotate_field (5);
8289
8290 if (c->syscalls_to_be_caught
8291 && VEC_length (int, c->syscalls_to_be_caught) > 1)
8292 ui_out_text (uiout, "syscalls \"");
8293 else
8294 ui_out_text (uiout, "syscall \"");
8295
8296 if (c->syscalls_to_be_caught)
8297 {
8298 int i, iter;
8299 char *text = xstrprintf ("%s", "");
8300
8301 for (i = 0;
8302 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
8303 i++)
8304 {
8305 char *x = text;
8306 struct syscall s;
8307 get_syscall_by_number (iter, &s);
8308
8309 if (s.name != NULL)
8310 text = xstrprintf ("%s%s, ", text, s.name);
8311 else
8312 text = xstrprintf ("%s%d, ", text, iter);
8313
8314 /* We have to xfree the last 'text' (now stored at 'x')
8315 because xstrprintf dynamically allocates new space for it
8316 on every call. */
8317 xfree (x);
8318 }
8319 /* Remove the last comma. */
8320 text[strlen (text) - 2] = '\0';
8321 ui_out_field_string (uiout, "what", text);
8322 }
8323 else
8324 ui_out_field_string (uiout, "what", "<any syscall>");
8325 ui_out_text (uiout, "\" ");
8326
8327 if (ui_out_is_mi_like_p (uiout))
8328 ui_out_field_string (uiout, "catch-type", "syscall");
8329 }
8330
8331 /* Implement the "print_mention" breakpoint_ops method for syscall
8332 catchpoints. */
8333
8334 static void
8335 print_mention_catch_syscall (struct breakpoint *b)
8336 {
8337 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8338
8339 if (c->syscalls_to_be_caught)
8340 {
8341 int i, iter;
8342
8343 if (VEC_length (int, c->syscalls_to_be_caught) > 1)
8344 printf_filtered (_("Catchpoint %d (syscalls"), b->number);
8345 else
8346 printf_filtered (_("Catchpoint %d (syscall"), b->number);
8347
8348 for (i = 0;
8349 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
8350 i++)
8351 {
8352 struct syscall s;
8353 get_syscall_by_number (iter, &s);
8354
8355 if (s.name)
8356 printf_filtered (" '%s' [%d]", s.name, s.number);
8357 else
8358 printf_filtered (" %d", s.number);
8359 }
8360 printf_filtered (")");
8361 }
8362 else
8363 printf_filtered (_("Catchpoint %d (any syscall)"),
8364 b->number);
8365 }
8366
8367 /* Implement the "print_recreate" breakpoint_ops method for syscall
8368 catchpoints. */
8369
8370 static void
8371 print_recreate_catch_syscall (struct breakpoint *b, struct ui_file *fp)
8372 {
8373 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8374
8375 fprintf_unfiltered (fp, "catch syscall");
8376
8377 if (c->syscalls_to_be_caught)
8378 {
8379 int i, iter;
8380
8381 for (i = 0;
8382 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
8383 i++)
8384 {
8385 struct syscall s;
8386
8387 get_syscall_by_number (iter, &s);
8388 if (s.name)
8389 fprintf_unfiltered (fp, " %s", s.name);
8390 else
8391 fprintf_unfiltered (fp, " %d", s.number);
8392 }
8393 }
8394 print_recreate_thread (b, fp);
8395 }
8396
8397 /* The breakpoint_ops structure to be used in syscall catchpoints. */
8398
8399 static struct breakpoint_ops catch_syscall_breakpoint_ops;
8400
8401 /* Returns non-zero if 'b' is a syscall catchpoint. */
8402
8403 static int
8404 syscall_catchpoint_p (struct breakpoint *b)
8405 {
8406 return (b->ops == &catch_syscall_breakpoint_ops);
8407 }
8408
8409 /* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8410 is non-zero, then make the breakpoint temporary. If COND_STRING is
8411 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8412 the breakpoint_ops structure associated to the catchpoint. */
8413
8414 void
8415 init_catchpoint (struct breakpoint *b,
8416 struct gdbarch *gdbarch, int tempflag,
8417 char *cond_string,
8418 const struct breakpoint_ops *ops)
8419 {
8420 struct symtab_and_line sal;
8421
8422 init_sal (&sal);
8423 sal.pspace = current_program_space;
8424
8425 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
8426
8427 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
8428 b->disposition = tempflag ? disp_del : disp_donttouch;
8429 }
8430
8431 void
8432 install_breakpoint (int internal, struct breakpoint *b, int update_gll)
8433 {
8434 add_to_breakpoint_chain (b);
8435 set_breakpoint_number (internal, b);
8436 if (is_tracepoint (b))
8437 set_tracepoint_count (breakpoint_count);
8438 if (!internal)
8439 mention (b);
8440 observer_notify_breakpoint_created (b);
8441
8442 if (update_gll)
8443 update_global_location_list (1);
8444 }
8445
8446 static void
8447 create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8448 int tempflag, char *cond_string,
8449 const struct breakpoint_ops *ops)
8450 {
8451 struct fork_catchpoint *c = XNEW (struct fork_catchpoint);
8452
8453 init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
8454
8455 c->forked_inferior_pid = null_ptid;
8456
8457 install_breakpoint (0, &c->base, 1);
8458 }
8459
8460 /* Exec catchpoints. */
8461
8462 /* An instance of this type is used to represent an exec catchpoint.
8463 It includes a "struct breakpoint" as a kind of base class; users
8464 downcast to "struct breakpoint *" when needed. A breakpoint is
8465 really of this type iff its ops pointer points to
8466 CATCH_EXEC_BREAKPOINT_OPS. */
8467
8468 struct exec_catchpoint
8469 {
8470 /* The base class. */
8471 struct breakpoint base;
8472
8473 /* Filename of a program whose exec triggered this catchpoint.
8474 This field is only valid immediately after this catchpoint has
8475 triggered. */
8476 char *exec_pathname;
8477 };
8478
8479 /* Implement the "dtor" breakpoint_ops method for exec
8480 catchpoints. */
8481
8482 static void
8483 dtor_catch_exec (struct breakpoint *b)
8484 {
8485 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8486
8487 xfree (c->exec_pathname);
8488
8489 base_breakpoint_ops.dtor (b);
8490 }
8491
8492 static int
8493 insert_catch_exec (struct bp_location *bl)
8494 {
8495 return target_insert_exec_catchpoint (PIDGET (inferior_ptid));
8496 }
8497
8498 static int
8499 remove_catch_exec (struct bp_location *bl)
8500 {
8501 return target_remove_exec_catchpoint (PIDGET (inferior_ptid));
8502 }
8503
8504 static int
8505 breakpoint_hit_catch_exec (const struct bp_location *bl,
8506 struct address_space *aspace, CORE_ADDR bp_addr,
8507 const struct target_waitstatus *ws)
8508 {
8509 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8510
8511 if (ws->kind != TARGET_WAITKIND_EXECD)
8512 return 0;
8513
8514 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8515 return 1;
8516 }
8517
8518 static enum print_stop_action
8519 print_it_catch_exec (bpstat bs)
8520 {
8521 struct ui_out *uiout = current_uiout;
8522 struct breakpoint *b = bs->breakpoint_at;
8523 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8524
8525 annotate_catchpoint (b->number);
8526 if (b->disposition == disp_del)
8527 ui_out_text (uiout, "\nTemporary catchpoint ");
8528 else
8529 ui_out_text (uiout, "\nCatchpoint ");
8530 if (ui_out_is_mi_like_p (uiout))
8531 {
8532 ui_out_field_string (uiout, "reason",
8533 async_reason_lookup (EXEC_ASYNC_EXEC));
8534 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8535 }
8536 ui_out_field_int (uiout, "bkptno", b->number);
8537 ui_out_text (uiout, " (exec'd ");
8538 ui_out_field_string (uiout, "new-exec", c->exec_pathname);
8539 ui_out_text (uiout, "), ");
8540
8541 return PRINT_SRC_AND_LOC;
8542 }
8543
8544 static void
8545 print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
8546 {
8547 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8548 struct value_print_options opts;
8549 struct ui_out *uiout = current_uiout;
8550
8551 get_user_print_options (&opts);
8552
8553 /* Field 4, the address, is omitted (which makes the columns
8554 not line up too nicely with the headers, but the effect
8555 is relatively readable). */
8556 if (opts.addressprint)
8557 ui_out_field_skip (uiout, "addr");
8558 annotate_field (5);
8559 ui_out_text (uiout, "exec");
8560 if (c->exec_pathname != NULL)
8561 {
8562 ui_out_text (uiout, ", program \"");
8563 ui_out_field_string (uiout, "what", c->exec_pathname);
8564 ui_out_text (uiout, "\" ");
8565 }
8566
8567 if (ui_out_is_mi_like_p (uiout))
8568 ui_out_field_string (uiout, "catch-type", "exec");
8569 }
8570
8571 static void
8572 print_mention_catch_exec (struct breakpoint *b)
8573 {
8574 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8575 }
8576
8577 /* Implement the "print_recreate" breakpoint_ops method for exec
8578 catchpoints. */
8579
8580 static void
8581 print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8582 {
8583 fprintf_unfiltered (fp, "catch exec");
8584 print_recreate_thread (b, fp);
8585 }
8586
8587 static struct breakpoint_ops catch_exec_breakpoint_ops;
8588
8589 static void
8590 create_syscall_event_catchpoint (int tempflag, VEC(int) *filter,
8591 const struct breakpoint_ops *ops)
8592 {
8593 struct syscall_catchpoint *c;
8594 struct gdbarch *gdbarch = get_current_arch ();
8595
8596 c = XNEW (struct syscall_catchpoint);
8597 init_catchpoint (&c->base, gdbarch, tempflag, NULL, ops);
8598 c->syscalls_to_be_caught = filter;
8599
8600 install_breakpoint (0, &c->base, 1);
8601 }
8602
8603 static int
8604 hw_breakpoint_used_count (void)
8605 {
8606 int i = 0;
8607 struct breakpoint *b;
8608 struct bp_location *bl;
8609
8610 ALL_BREAKPOINTS (b)
8611 {
8612 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
8613 for (bl = b->loc; bl; bl = bl->next)
8614 {
8615 /* Special types of hardware breakpoints may use more than
8616 one register. */
8617 i += b->ops->resources_needed (bl);
8618 }
8619 }
8620
8621 return i;
8622 }
8623
8624 /* Returns the resources B would use if it were a hardware
8625 watchpoint. */
8626
8627 static int
8628 hw_watchpoint_use_count (struct breakpoint *b)
8629 {
8630 int i = 0;
8631 struct bp_location *bl;
8632
8633 if (!breakpoint_enabled (b))
8634 return 0;
8635
8636 for (bl = b->loc; bl; bl = bl->next)
8637 {
8638 /* Special types of hardware watchpoints may use more than
8639 one register. */
8640 i += b->ops->resources_needed (bl);
8641 }
8642
8643 return i;
8644 }
8645
8646 /* Returns the sum the used resources of all hardware watchpoints of
8647 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8648 the sum of the used resources of all hardware watchpoints of other
8649 types _not_ TYPE. */
8650
8651 static int
8652 hw_watchpoint_used_count_others (struct breakpoint *except,
8653 enum bptype type, int *other_type_used)
8654 {
8655 int i = 0;
8656 struct breakpoint *b;
8657
8658 *other_type_used = 0;
8659 ALL_BREAKPOINTS (b)
8660 {
8661 if (b == except)
8662 continue;
8663 if (!breakpoint_enabled (b))
8664 continue;
8665
8666 if (b->type == type)
8667 i += hw_watchpoint_use_count (b);
8668 else if (is_hardware_watchpoint (b))
8669 *other_type_used = 1;
8670 }
8671
8672 return i;
8673 }
8674
8675 void
8676 disable_watchpoints_before_interactive_call_start (void)
8677 {
8678 struct breakpoint *b;
8679
8680 ALL_BREAKPOINTS (b)
8681 {
8682 if (is_watchpoint (b) && breakpoint_enabled (b))
8683 {
8684 b->enable_state = bp_call_disabled;
8685 update_global_location_list (0);
8686 }
8687 }
8688 }
8689
8690 void
8691 enable_watchpoints_after_interactive_call_stop (void)
8692 {
8693 struct breakpoint *b;
8694
8695 ALL_BREAKPOINTS (b)
8696 {
8697 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
8698 {
8699 b->enable_state = bp_enabled;
8700 update_global_location_list (1);
8701 }
8702 }
8703 }
8704
8705 void
8706 disable_breakpoints_before_startup (void)
8707 {
8708 current_program_space->executing_startup = 1;
8709 update_global_location_list (0);
8710 }
8711
8712 void
8713 enable_breakpoints_after_startup (void)
8714 {
8715 current_program_space->executing_startup = 0;
8716 breakpoint_re_set ();
8717 }
8718
8719
8720 /* Set a breakpoint that will evaporate an end of command
8721 at address specified by SAL.
8722 Restrict it to frame FRAME if FRAME is nonzero. */
8723
8724 struct breakpoint *
8725 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8726 struct frame_id frame_id, enum bptype type)
8727 {
8728 struct breakpoint *b;
8729
8730 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8731 tail-called one. */
8732 gdb_assert (!frame_id_artificial_p (frame_id));
8733
8734 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
8735 b->enable_state = bp_enabled;
8736 b->disposition = disp_donttouch;
8737 b->frame_id = frame_id;
8738
8739 /* If we're debugging a multi-threaded program, then we want
8740 momentary breakpoints to be active in only a single thread of
8741 control. */
8742 if (in_thread_list (inferior_ptid))
8743 b->thread = pid_to_thread_id (inferior_ptid);
8744
8745 update_global_location_list_nothrow (1);
8746
8747 return b;
8748 }
8749
8750 /* Make a momentary breakpoint based on the master breakpoint ORIG.
8751 The new breakpoint will have type TYPE, and use OPS as it
8752 breakpoint_ops. */
8753
8754 static struct breakpoint *
8755 momentary_breakpoint_from_master (struct breakpoint *orig,
8756 enum bptype type,
8757 const struct breakpoint_ops *ops)
8758 {
8759 struct breakpoint *copy;
8760
8761 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
8762 copy->loc = allocate_bp_location (copy);
8763 set_breakpoint_location_function (copy->loc, 1);
8764
8765 copy->loc->gdbarch = orig->loc->gdbarch;
8766 copy->loc->requested_address = orig->loc->requested_address;
8767 copy->loc->address = orig->loc->address;
8768 copy->loc->section = orig->loc->section;
8769 copy->loc->pspace = orig->loc->pspace;
8770 copy->loc->probe = orig->loc->probe;
8771 copy->loc->line_number = orig->loc->line_number;
8772 copy->loc->symtab = orig->loc->symtab;
8773 copy->frame_id = orig->frame_id;
8774 copy->thread = orig->thread;
8775 copy->pspace = orig->pspace;
8776
8777 copy->enable_state = bp_enabled;
8778 copy->disposition = disp_donttouch;
8779 copy->number = internal_breakpoint_number--;
8780
8781 update_global_location_list_nothrow (0);
8782 return copy;
8783 }
8784
8785 /* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8786 ORIG is NULL. */
8787
8788 struct breakpoint *
8789 clone_momentary_breakpoint (struct breakpoint *orig)
8790 {
8791 /* If there's nothing to clone, then return nothing. */
8792 if (orig == NULL)
8793 return NULL;
8794
8795 return momentary_breakpoint_from_master (orig, orig->type, orig->ops);
8796 }
8797
8798 struct breakpoint *
8799 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8800 enum bptype type)
8801 {
8802 struct symtab_and_line sal;
8803
8804 sal = find_pc_line (pc, 0);
8805 sal.pc = pc;
8806 sal.section = find_pc_overlay (pc);
8807 sal.explicit_pc = 1;
8808
8809 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
8810 }
8811 \f
8812
8813 /* Tell the user we have just set a breakpoint B. */
8814
8815 static void
8816 mention (struct breakpoint *b)
8817 {
8818 b->ops->print_mention (b);
8819 if (ui_out_is_mi_like_p (current_uiout))
8820 return;
8821 printf_filtered ("\n");
8822 }
8823 \f
8824
8825 static struct bp_location *
8826 add_location_to_breakpoint (struct breakpoint *b,
8827 const struct symtab_and_line *sal)
8828 {
8829 struct bp_location *loc, **tmp;
8830 CORE_ADDR adjusted_address;
8831 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8832
8833 if (loc_gdbarch == NULL)
8834 loc_gdbarch = b->gdbarch;
8835
8836 /* Adjust the breakpoint's address prior to allocating a location.
8837 Once we call allocate_bp_location(), that mostly uninitialized
8838 location will be placed on the location chain. Adjustment of the
8839 breakpoint may cause target_read_memory() to be called and we do
8840 not want its scan of the location chain to find a breakpoint and
8841 location that's only been partially initialized. */
8842 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8843 sal->pc, b->type);
8844
8845 /* Sort the locations by their ADDRESS. */
8846 loc = allocate_bp_location (b);
8847 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8848 tmp = &((*tmp)->next))
8849 ;
8850 loc->next = *tmp;
8851 *tmp = loc;
8852
8853 loc->requested_address = sal->pc;
8854 loc->address = adjusted_address;
8855 loc->pspace = sal->pspace;
8856 loc->probe = sal->probe;
8857 gdb_assert (loc->pspace != NULL);
8858 loc->section = sal->section;
8859 loc->gdbarch = loc_gdbarch;
8860 loc->line_number = sal->line;
8861 loc->symtab = sal->symtab;
8862
8863 set_breakpoint_location_function (loc,
8864 sal->explicit_pc || sal->explicit_line);
8865 return loc;
8866 }
8867 \f
8868
8869 /* Return 1 if LOC is pointing to a permanent breakpoint,
8870 return 0 otherwise. */
8871
8872 static int
8873 bp_loc_is_permanent (struct bp_location *loc)
8874 {
8875 int len;
8876 CORE_ADDR addr;
8877 const gdb_byte *bpoint;
8878 gdb_byte *target_mem;
8879 struct cleanup *cleanup;
8880 int retval = 0;
8881
8882 gdb_assert (loc != NULL);
8883
8884 addr = loc->address;
8885 bpoint = gdbarch_breakpoint_from_pc (loc->gdbarch, &addr, &len);
8886
8887 /* Software breakpoints unsupported? */
8888 if (bpoint == NULL)
8889 return 0;
8890
8891 target_mem = alloca (len);
8892
8893 /* Enable the automatic memory restoration from breakpoints while
8894 we read the memory. Otherwise we could say about our temporary
8895 breakpoints they are permanent. */
8896 cleanup = save_current_space_and_thread ();
8897
8898 switch_to_program_space_and_thread (loc->pspace);
8899 make_show_memory_breakpoints_cleanup (0);
8900
8901 if (target_read_memory (loc->address, target_mem, len) == 0
8902 && memcmp (target_mem, bpoint, len) == 0)
8903 retval = 1;
8904
8905 do_cleanups (cleanup);
8906
8907 return retval;
8908 }
8909
8910 /* Build a command list for the dprintf corresponding to the current
8911 settings of the dprintf style options. */
8912
8913 static void
8914 update_dprintf_command_list (struct breakpoint *b)
8915 {
8916 char *dprintf_args = b->extra_string;
8917 char *printf_line = NULL;
8918
8919 if (!dprintf_args)
8920 return;
8921
8922 dprintf_args = skip_spaces (dprintf_args);
8923
8924 /* Allow a comma, as it may have terminated a location, but don't
8925 insist on it. */
8926 if (*dprintf_args == ',')
8927 ++dprintf_args;
8928 dprintf_args = skip_spaces (dprintf_args);
8929
8930 if (*dprintf_args != '"')
8931 error (_("Bad format string, missing '\"'."));
8932
8933 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
8934 printf_line = xstrprintf ("printf %s", dprintf_args);
8935 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
8936 {
8937 if (!dprintf_function)
8938 error (_("No function supplied for dprintf call"));
8939
8940 if (dprintf_channel && strlen (dprintf_channel) > 0)
8941 printf_line = xstrprintf ("call (void) %s (%s,%s)",
8942 dprintf_function,
8943 dprintf_channel,
8944 dprintf_args);
8945 else
8946 printf_line = xstrprintf ("call (void) %s (%s)",
8947 dprintf_function,
8948 dprintf_args);
8949 }
8950 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
8951 {
8952 if (target_can_run_breakpoint_commands ())
8953 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
8954 else
8955 {
8956 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
8957 printf_line = xstrprintf ("printf %s", dprintf_args);
8958 }
8959 }
8960 else
8961 internal_error (__FILE__, __LINE__,
8962 _("Invalid dprintf style."));
8963
8964 gdb_assert (printf_line != NULL);
8965 /* Manufacture a printf/continue sequence. */
8966 {
8967 struct command_line *printf_cmd_line, *cont_cmd_line = NULL;
8968
8969 if (strcmp (dprintf_style, dprintf_style_agent) != 0)
8970 {
8971 cont_cmd_line = xmalloc (sizeof (struct command_line));
8972 cont_cmd_line->control_type = simple_control;
8973 cont_cmd_line->body_count = 0;
8974 cont_cmd_line->body_list = NULL;
8975 cont_cmd_line->next = NULL;
8976 cont_cmd_line->line = xstrdup ("continue");
8977 }
8978
8979 printf_cmd_line = xmalloc (sizeof (struct command_line));
8980 printf_cmd_line->control_type = simple_control;
8981 printf_cmd_line->body_count = 0;
8982 printf_cmd_line->body_list = NULL;
8983 printf_cmd_line->next = cont_cmd_line;
8984 printf_cmd_line->line = printf_line;
8985
8986 breakpoint_set_commands (b, printf_cmd_line);
8987 }
8988 }
8989
8990 /* Update all dprintf commands, making their command lists reflect
8991 current style settings. */
8992
8993 static void
8994 update_dprintf_commands (char *args, int from_tty,
8995 struct cmd_list_element *c)
8996 {
8997 struct breakpoint *b;
8998
8999 ALL_BREAKPOINTS (b)
9000 {
9001 if (b->type == bp_dprintf)
9002 update_dprintf_command_list (b);
9003 }
9004 }
9005
9006 /* Create a breakpoint with SAL as location. Use ADDR_STRING
9007 as textual description of the location, and COND_STRING
9008 as condition expression. */
9009
9010 static void
9011 init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
9012 struct symtabs_and_lines sals, char *addr_string,
9013 char *filter, char *cond_string,
9014 char *extra_string,
9015 enum bptype type, enum bpdisp disposition,
9016 int thread, int task, int ignore_count,
9017 const struct breakpoint_ops *ops, int from_tty,
9018 int enabled, int internal, unsigned flags,
9019 int display_canonical)
9020 {
9021 int i;
9022
9023 if (type == bp_hardware_breakpoint)
9024 {
9025 int target_resources_ok;
9026
9027 i = hw_breakpoint_used_count ();
9028 target_resources_ok =
9029 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9030 i + 1, 0);
9031 if (target_resources_ok == 0)
9032 error (_("No hardware breakpoint support in the target."));
9033 else if (target_resources_ok < 0)
9034 error (_("Hardware breakpoints used exceeds limit."));
9035 }
9036
9037 gdb_assert (sals.nelts > 0);
9038
9039 for (i = 0; i < sals.nelts; ++i)
9040 {
9041 struct symtab_and_line sal = sals.sals[i];
9042 struct bp_location *loc;
9043
9044 if (from_tty)
9045 {
9046 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9047 if (!loc_gdbarch)
9048 loc_gdbarch = gdbarch;
9049
9050 describe_other_breakpoints (loc_gdbarch,
9051 sal.pspace, sal.pc, sal.section, thread);
9052 }
9053
9054 if (i == 0)
9055 {
9056 init_raw_breakpoint (b, gdbarch, sal, type, ops);
9057 b->thread = thread;
9058 b->task = task;
9059
9060 b->cond_string = cond_string;
9061 b->extra_string = extra_string;
9062 b->ignore_count = ignore_count;
9063 b->enable_state = enabled ? bp_enabled : bp_disabled;
9064 b->disposition = disposition;
9065
9066 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9067 b->loc->inserted = 1;
9068
9069 if (type == bp_static_tracepoint)
9070 {
9071 struct tracepoint *t = (struct tracepoint *) b;
9072 struct static_tracepoint_marker marker;
9073
9074 if (strace_marker_p (b))
9075 {
9076 /* We already know the marker exists, otherwise, we
9077 wouldn't see a sal for it. */
9078 char *p = &addr_string[3];
9079 char *endp;
9080 char *marker_str;
9081
9082 p = skip_spaces (p);
9083
9084 endp = skip_to_space (p);
9085
9086 marker_str = savestring (p, endp - p);
9087 t->static_trace_marker_id = marker_str;
9088
9089 printf_filtered (_("Probed static tracepoint "
9090 "marker \"%s\"\n"),
9091 t->static_trace_marker_id);
9092 }
9093 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
9094 {
9095 t->static_trace_marker_id = xstrdup (marker.str_id);
9096 release_static_tracepoint_marker (&marker);
9097
9098 printf_filtered (_("Probed static tracepoint "
9099 "marker \"%s\"\n"),
9100 t->static_trace_marker_id);
9101 }
9102 else
9103 warning (_("Couldn't determine the static "
9104 "tracepoint marker to probe"));
9105 }
9106
9107 loc = b->loc;
9108 }
9109 else
9110 {
9111 loc = add_location_to_breakpoint (b, &sal);
9112 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9113 loc->inserted = 1;
9114 }
9115
9116 if (bp_loc_is_permanent (loc))
9117 make_breakpoint_permanent (b);
9118
9119 if (b->cond_string)
9120 {
9121 const char *arg = b->cond_string;
9122
9123 loc->cond = parse_exp_1 (&arg, loc->address,
9124 block_for_pc (loc->address), 0);
9125 if (*arg)
9126 error (_("Garbage '%s' follows condition"), arg);
9127 }
9128
9129 /* Dynamic printf requires and uses additional arguments on the
9130 command line, otherwise it's an error. */
9131 if (type == bp_dprintf)
9132 {
9133 if (b->extra_string)
9134 update_dprintf_command_list (b);
9135 else
9136 error (_("Format string required"));
9137 }
9138 else if (b->extra_string)
9139 error (_("Garbage '%s' at end of command"), b->extra_string);
9140 }
9141
9142 b->display_canonical = display_canonical;
9143 if (addr_string)
9144 b->addr_string = addr_string;
9145 else
9146 /* addr_string has to be used or breakpoint_re_set will delete
9147 me. */
9148 b->addr_string
9149 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
9150 b->filter = filter;
9151 }
9152
9153 static void
9154 create_breakpoint_sal (struct gdbarch *gdbarch,
9155 struct symtabs_and_lines sals, char *addr_string,
9156 char *filter, char *cond_string,
9157 char *extra_string,
9158 enum bptype type, enum bpdisp disposition,
9159 int thread, int task, int ignore_count,
9160 const struct breakpoint_ops *ops, int from_tty,
9161 int enabled, int internal, unsigned flags,
9162 int display_canonical)
9163 {
9164 struct breakpoint *b;
9165 struct cleanup *old_chain;
9166
9167 if (is_tracepoint_type (type))
9168 {
9169 struct tracepoint *t;
9170
9171 t = XCNEW (struct tracepoint);
9172 b = &t->base;
9173 }
9174 else
9175 b = XNEW (struct breakpoint);
9176
9177 old_chain = make_cleanup (xfree, b);
9178
9179 init_breakpoint_sal (b, gdbarch,
9180 sals, addr_string,
9181 filter, cond_string, extra_string,
9182 type, disposition,
9183 thread, task, ignore_count,
9184 ops, from_tty,
9185 enabled, internal, flags,
9186 display_canonical);
9187 discard_cleanups (old_chain);
9188
9189 install_breakpoint (internal, b, 0);
9190 }
9191
9192 /* Add SALS.nelts breakpoints to the breakpoint table. For each
9193 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9194 value. COND_STRING, if not NULL, specified the condition to be
9195 used for all breakpoints. Essentially the only case where
9196 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9197 function. In that case, it's still not possible to specify
9198 separate conditions for different overloaded functions, so
9199 we take just a single condition string.
9200
9201 NOTE: If the function succeeds, the caller is expected to cleanup
9202 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
9203 array contents). If the function fails (error() is called), the
9204 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
9205 COND and SALS arrays and each of those arrays contents. */
9206
9207 static void
9208 create_breakpoints_sal (struct gdbarch *gdbarch,
9209 struct linespec_result *canonical,
9210 char *cond_string, char *extra_string,
9211 enum bptype type, enum bpdisp disposition,
9212 int thread, int task, int ignore_count,
9213 const struct breakpoint_ops *ops, int from_tty,
9214 int enabled, int internal, unsigned flags)
9215 {
9216 int i;
9217 struct linespec_sals *lsal;
9218
9219 if (canonical->pre_expanded)
9220 gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
9221
9222 for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
9223 {
9224 /* Note that 'addr_string' can be NULL in the case of a plain
9225 'break', without arguments. */
9226 char *addr_string = (canonical->addr_string
9227 ? xstrdup (canonical->addr_string)
9228 : NULL);
9229 char *filter_string = lsal->canonical ? xstrdup (lsal->canonical) : NULL;
9230 struct cleanup *inner = make_cleanup (xfree, addr_string);
9231
9232 make_cleanup (xfree, filter_string);
9233 create_breakpoint_sal (gdbarch, lsal->sals,
9234 addr_string,
9235 filter_string,
9236 cond_string, extra_string,
9237 type, disposition,
9238 thread, task, ignore_count, ops,
9239 from_tty, enabled, internal, flags,
9240 canonical->special_display);
9241 discard_cleanups (inner);
9242 }
9243 }
9244
9245 /* Parse ADDRESS which is assumed to be a SAL specification possibly
9246 followed by conditionals. On return, SALS contains an array of SAL
9247 addresses found. ADDR_STRING contains a vector of (canonical)
9248 address strings. ADDRESS points to the end of the SAL.
9249
9250 The array and the line spec strings are allocated on the heap, it is
9251 the caller's responsibility to free them. */
9252
9253 static void
9254 parse_breakpoint_sals (char **address,
9255 struct linespec_result *canonical)
9256 {
9257 /* If no arg given, or if first arg is 'if ', use the default
9258 breakpoint. */
9259 if ((*address) == NULL
9260 || (strncmp ((*address), "if", 2) == 0 && isspace ((*address)[2])))
9261 {
9262 /* The last displayed codepoint, if it's valid, is our default breakpoint
9263 address. */
9264 if (last_displayed_sal_is_valid ())
9265 {
9266 struct linespec_sals lsal;
9267 struct symtab_and_line sal;
9268 CORE_ADDR pc;
9269
9270 init_sal (&sal); /* Initialize to zeroes. */
9271 lsal.sals.sals = (struct symtab_and_line *)
9272 xmalloc (sizeof (struct symtab_and_line));
9273
9274 /* Set sal's pspace, pc, symtab, and line to the values
9275 corresponding to the last call to print_frame_info.
9276 Be sure to reinitialize LINE with NOTCURRENT == 0
9277 as the breakpoint line number is inappropriate otherwise.
9278 find_pc_line would adjust PC, re-set it back. */
9279 get_last_displayed_sal (&sal);
9280 pc = sal.pc;
9281 sal = find_pc_line (pc, 0);
9282
9283 /* "break" without arguments is equivalent to "break *PC"
9284 where PC is the last displayed codepoint's address. So
9285 make sure to set sal.explicit_pc to prevent GDB from
9286 trying to expand the list of sals to include all other
9287 instances with the same symtab and line. */
9288 sal.pc = pc;
9289 sal.explicit_pc = 1;
9290
9291 lsal.sals.sals[0] = sal;
9292 lsal.sals.nelts = 1;
9293 lsal.canonical = NULL;
9294
9295 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
9296 }
9297 else
9298 error (_("No default breakpoint address now."));
9299 }
9300 else
9301 {
9302 struct symtab_and_line cursal = get_current_source_symtab_and_line ();
9303
9304 /* Force almost all breakpoints to be in terms of the
9305 current_source_symtab (which is decode_line_1's default).
9306 This should produce the results we want almost all of the
9307 time while leaving default_breakpoint_* alone.
9308
9309 ObjC: However, don't match an Objective-C method name which
9310 may have a '+' or '-' succeeded by a '['. */
9311 if (last_displayed_sal_is_valid ()
9312 && (!cursal.symtab
9313 || ((strchr ("+-", (*address)[0]) != NULL)
9314 && ((*address)[1] != '['))))
9315 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
9316 get_last_displayed_symtab (),
9317 get_last_displayed_line (),
9318 canonical, NULL, NULL);
9319 else
9320 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
9321 cursal.symtab, cursal.line, canonical, NULL, NULL);
9322 }
9323 }
9324
9325
9326 /* Convert each SAL into a real PC. Verify that the PC can be
9327 inserted as a breakpoint. If it can't throw an error. */
9328
9329 static void
9330 breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
9331 {
9332 int i;
9333
9334 for (i = 0; i < sals->nelts; i++)
9335 resolve_sal_pc (&sals->sals[i]);
9336 }
9337
9338 /* Fast tracepoints may have restrictions on valid locations. For
9339 instance, a fast tracepoint using a jump instead of a trap will
9340 likely have to overwrite more bytes than a trap would, and so can
9341 only be placed where the instruction is longer than the jump, or a
9342 multi-instruction sequence does not have a jump into the middle of
9343 it, etc. */
9344
9345 static void
9346 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9347 struct symtabs_and_lines *sals)
9348 {
9349 int i, rslt;
9350 struct symtab_and_line *sal;
9351 char *msg;
9352 struct cleanup *old_chain;
9353
9354 for (i = 0; i < sals->nelts; i++)
9355 {
9356 struct gdbarch *sarch;
9357
9358 sal = &sals->sals[i];
9359
9360 sarch = get_sal_arch (*sal);
9361 /* We fall back to GDBARCH if there is no architecture
9362 associated with SAL. */
9363 if (sarch == NULL)
9364 sarch = gdbarch;
9365 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc,
9366 NULL, &msg);
9367 old_chain = make_cleanup (xfree, msg);
9368
9369 if (!rslt)
9370 error (_("May not have a fast tracepoint at 0x%s%s"),
9371 paddress (sarch, sal->pc), (msg ? msg : ""));
9372
9373 do_cleanups (old_chain);
9374 }
9375 }
9376
9377 /* Issue an invalid thread ID error. */
9378
9379 static void ATTRIBUTE_NORETURN
9380 invalid_thread_id_error (int id)
9381 {
9382 error (_("Unknown thread %d."), id);
9383 }
9384
9385 /* Given TOK, a string specification of condition and thread, as
9386 accepted by the 'break' command, extract the condition
9387 string and thread number and set *COND_STRING and *THREAD.
9388 PC identifies the context at which the condition should be parsed.
9389 If no condition is found, *COND_STRING is set to NULL.
9390 If no thread is found, *THREAD is set to -1. */
9391
9392 static void
9393 find_condition_and_thread (const char *tok, CORE_ADDR pc,
9394 char **cond_string, int *thread, int *task,
9395 char **rest)
9396 {
9397 *cond_string = NULL;
9398 *thread = -1;
9399 *task = 0;
9400 *rest = NULL;
9401
9402 while (tok && *tok)
9403 {
9404 const char *end_tok;
9405 int toklen;
9406 const char *cond_start = NULL;
9407 const char *cond_end = NULL;
9408
9409 tok = skip_spaces_const (tok);
9410
9411 if ((*tok == '"' || *tok == ',') && rest)
9412 {
9413 *rest = savestring (tok, strlen (tok));
9414 return;
9415 }
9416
9417 end_tok = skip_to_space_const (tok);
9418
9419 toklen = end_tok - tok;
9420
9421 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9422 {
9423 struct expression *expr;
9424
9425 tok = cond_start = end_tok + 1;
9426 expr = parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
9427 xfree (expr);
9428 cond_end = tok;
9429 *cond_string = savestring (cond_start, cond_end - cond_start);
9430 }
9431 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9432 {
9433 char *tmptok;
9434
9435 tok = end_tok + 1;
9436 *thread = strtol (tok, &tmptok, 0);
9437 if (tok == tmptok)
9438 error (_("Junk after thread keyword."));
9439 if (!valid_thread_id (*thread))
9440 invalid_thread_id_error (*thread);
9441 tok = tmptok;
9442 }
9443 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9444 {
9445 char *tmptok;
9446
9447 tok = end_tok + 1;
9448 *task = strtol (tok, &tmptok, 0);
9449 if (tok == tmptok)
9450 error (_("Junk after task keyword."));
9451 if (!valid_task_id (*task))
9452 error (_("Unknown task %d."), *task);
9453 tok = tmptok;
9454 }
9455 else if (rest)
9456 {
9457 *rest = savestring (tok, strlen (tok));
9458 return;
9459 }
9460 else
9461 error (_("Junk at end of arguments."));
9462 }
9463 }
9464
9465 /* Decode a static tracepoint marker spec. */
9466
9467 static struct symtabs_and_lines
9468 decode_static_tracepoint_spec (char **arg_p)
9469 {
9470 VEC(static_tracepoint_marker_p) *markers = NULL;
9471 struct symtabs_and_lines sals;
9472 struct cleanup *old_chain;
9473 char *p = &(*arg_p)[3];
9474 char *endp;
9475 char *marker_str;
9476 int i;
9477
9478 p = skip_spaces (p);
9479
9480 endp = skip_to_space (p);
9481
9482 marker_str = savestring (p, endp - p);
9483 old_chain = make_cleanup (xfree, marker_str);
9484
9485 markers = target_static_tracepoint_markers_by_strid (marker_str);
9486 if (VEC_empty(static_tracepoint_marker_p, markers))
9487 error (_("No known static tracepoint marker named %s"), marker_str);
9488
9489 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
9490 sals.sals = xmalloc (sizeof *sals.sals * sals.nelts);
9491
9492 for (i = 0; i < sals.nelts; i++)
9493 {
9494 struct static_tracepoint_marker *marker;
9495
9496 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9497
9498 init_sal (&sals.sals[i]);
9499
9500 sals.sals[i] = find_pc_line (marker->address, 0);
9501 sals.sals[i].pc = marker->address;
9502
9503 release_static_tracepoint_marker (marker);
9504 }
9505
9506 do_cleanups (old_chain);
9507
9508 *arg_p = endp;
9509 return sals;
9510 }
9511
9512 /* Set a breakpoint. This function is shared between CLI and MI
9513 functions for setting a breakpoint. This function has two major
9514 modes of operations, selected by the PARSE_ARG parameter. If
9515 non-zero, the function will parse ARG, extracting location,
9516 condition, thread and extra string. Otherwise, ARG is just the
9517 breakpoint's location, with condition, thread, and extra string
9518 specified by the COND_STRING, THREAD and EXTRA_STRING parameters.
9519 If INTERNAL is non-zero, the breakpoint number will be allocated
9520 from the internal breakpoint count. Returns true if any breakpoint
9521 was created; false otherwise. */
9522
9523 int
9524 create_breakpoint (struct gdbarch *gdbarch,
9525 char *arg, char *cond_string,
9526 int thread, char *extra_string,
9527 int parse_arg,
9528 int tempflag, enum bptype type_wanted,
9529 int ignore_count,
9530 enum auto_boolean pending_break_support,
9531 const struct breakpoint_ops *ops,
9532 int from_tty, int enabled, int internal,
9533 unsigned flags)
9534 {
9535 volatile struct gdb_exception e;
9536 char *copy_arg = NULL;
9537 char *addr_start = arg;
9538 struct linespec_result canonical;
9539 struct cleanup *old_chain;
9540 struct cleanup *bkpt_chain = NULL;
9541 int pending = 0;
9542 int task = 0;
9543 int prev_bkpt_count = breakpoint_count;
9544
9545 gdb_assert (ops != NULL);
9546
9547 init_linespec_result (&canonical);
9548
9549 TRY_CATCH (e, RETURN_MASK_ALL)
9550 {
9551 ops->create_sals_from_address (&arg, &canonical, type_wanted,
9552 addr_start, &copy_arg);
9553 }
9554
9555 /* If caller is interested in rc value from parse, set value. */
9556 switch (e.reason)
9557 {
9558 case GDB_NO_ERROR:
9559 if (VEC_empty (linespec_sals, canonical.sals))
9560 return 0;
9561 break;
9562 case RETURN_ERROR:
9563 switch (e.error)
9564 {
9565 case NOT_FOUND_ERROR:
9566
9567 /* If pending breakpoint support is turned off, throw
9568 error. */
9569
9570 if (pending_break_support == AUTO_BOOLEAN_FALSE)
9571 throw_exception (e);
9572
9573 exception_print (gdb_stderr, e);
9574
9575 /* If pending breakpoint support is auto query and the user
9576 selects no, then simply return the error code. */
9577 if (pending_break_support == AUTO_BOOLEAN_AUTO
9578 && !nquery (_("Make %s pending on future shared library load? "),
9579 bptype_string (type_wanted)))
9580 return 0;
9581
9582 /* At this point, either the user was queried about setting
9583 a pending breakpoint and selected yes, or pending
9584 breakpoint behavior is on and thus a pending breakpoint
9585 is defaulted on behalf of the user. */
9586 {
9587 struct linespec_sals lsal;
9588
9589 copy_arg = xstrdup (addr_start);
9590 lsal.canonical = xstrdup (copy_arg);
9591 lsal.sals.nelts = 1;
9592 lsal.sals.sals = XNEW (struct symtab_and_line);
9593 init_sal (&lsal.sals.sals[0]);
9594 pending = 1;
9595 VEC_safe_push (linespec_sals, canonical.sals, &lsal);
9596 }
9597 break;
9598 default:
9599 throw_exception (e);
9600 }
9601 break;
9602 default:
9603 throw_exception (e);
9604 }
9605
9606 /* Create a chain of things that always need to be cleaned up. */
9607 old_chain = make_cleanup_destroy_linespec_result (&canonical);
9608
9609 /* ----------------------------- SNIP -----------------------------
9610 Anything added to the cleanup chain beyond this point is assumed
9611 to be part of a breakpoint. If the breakpoint create succeeds
9612 then the memory is not reclaimed. */
9613 bkpt_chain = make_cleanup (null_cleanup, 0);
9614
9615 /* Resolve all line numbers to PC's and verify that the addresses
9616 are ok for the target. */
9617 if (!pending)
9618 {
9619 int ix;
9620 struct linespec_sals *iter;
9621
9622 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9623 breakpoint_sals_to_pc (&iter->sals);
9624 }
9625
9626 /* Fast tracepoints may have additional restrictions on location. */
9627 if (!pending && type_wanted == bp_fast_tracepoint)
9628 {
9629 int ix;
9630 struct linespec_sals *iter;
9631
9632 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9633 check_fast_tracepoint_sals (gdbarch, &iter->sals);
9634 }
9635
9636 /* Verify that condition can be parsed, before setting any
9637 breakpoints. Allocate a separate condition expression for each
9638 breakpoint. */
9639 if (!pending)
9640 {
9641 struct linespec_sals *lsal;
9642
9643 lsal = VEC_index (linespec_sals, canonical.sals, 0);
9644
9645 if (parse_arg)
9646 {
9647 char *rest;
9648 /* Here we only parse 'arg' to separate condition
9649 from thread number, so parsing in context of first
9650 sal is OK. When setting the breakpoint we'll
9651 re-parse it in context of each sal. */
9652
9653 find_condition_and_thread (arg, lsal->sals.sals[0].pc, &cond_string,
9654 &thread, &task, &rest);
9655 if (cond_string)
9656 make_cleanup (xfree, cond_string);
9657 if (rest)
9658 make_cleanup (xfree, rest);
9659 if (rest)
9660 extra_string = rest;
9661 }
9662 else
9663 {
9664 if (*arg != '\0')
9665 error (_("Garbage '%s' at end of location"), arg);
9666
9667 /* Create a private copy of condition string. */
9668 if (cond_string)
9669 {
9670 cond_string = xstrdup (cond_string);
9671 make_cleanup (xfree, cond_string);
9672 }
9673 /* Create a private copy of any extra string. */
9674 if (extra_string)
9675 {
9676 extra_string = xstrdup (extra_string);
9677 make_cleanup (xfree, extra_string);
9678 }
9679 }
9680
9681 ops->create_breakpoints_sal (gdbarch, &canonical, lsal,
9682 cond_string, extra_string, type_wanted,
9683 tempflag ? disp_del : disp_donttouch,
9684 thread, task, ignore_count, ops,
9685 from_tty, enabled, internal, flags);
9686 }
9687 else
9688 {
9689 struct breakpoint *b;
9690
9691 make_cleanup (xfree, copy_arg);
9692
9693 if (is_tracepoint_type (type_wanted))
9694 {
9695 struct tracepoint *t;
9696
9697 t = XCNEW (struct tracepoint);
9698 b = &t->base;
9699 }
9700 else
9701 b = XNEW (struct breakpoint);
9702
9703 init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops);
9704
9705 b->addr_string = copy_arg;
9706 if (parse_arg)
9707 b->cond_string = NULL;
9708 else
9709 {
9710 /* Create a private copy of condition string. */
9711 if (cond_string)
9712 {
9713 cond_string = xstrdup (cond_string);
9714 make_cleanup (xfree, cond_string);
9715 }
9716 b->cond_string = cond_string;
9717 }
9718 b->extra_string = NULL;
9719 b->ignore_count = ignore_count;
9720 b->disposition = tempflag ? disp_del : disp_donttouch;
9721 b->condition_not_parsed = 1;
9722 b->enable_state = enabled ? bp_enabled : bp_disabled;
9723 if ((type_wanted != bp_breakpoint
9724 && type_wanted != bp_hardware_breakpoint) || thread != -1)
9725 b->pspace = current_program_space;
9726
9727 install_breakpoint (internal, b, 0);
9728 }
9729
9730 if (VEC_length (linespec_sals, canonical.sals) > 1)
9731 {
9732 warning (_("Multiple breakpoints were set.\nUse the "
9733 "\"delete\" command to delete unwanted breakpoints."));
9734 prev_breakpoint_count = prev_bkpt_count;
9735 }
9736
9737 /* That's it. Discard the cleanups for data inserted into the
9738 breakpoint. */
9739 discard_cleanups (bkpt_chain);
9740 /* But cleanup everything else. */
9741 do_cleanups (old_chain);
9742
9743 /* error call may happen here - have BKPT_CHAIN already discarded. */
9744 update_global_location_list (1);
9745
9746 return 1;
9747 }
9748
9749 /* Set a breakpoint.
9750 ARG is a string describing breakpoint address,
9751 condition, and thread.
9752 FLAG specifies if a breakpoint is hardware on,
9753 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9754 and BP_TEMPFLAG. */
9755
9756 static void
9757 break_command_1 (char *arg, int flag, int from_tty)
9758 {
9759 int tempflag = flag & BP_TEMPFLAG;
9760 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9761 ? bp_hardware_breakpoint
9762 : bp_breakpoint);
9763 struct breakpoint_ops *ops;
9764 const char *arg_cp = arg;
9765
9766 /* Matching breakpoints on probes. */
9767 if (arg && probe_linespec_to_ops (&arg_cp) != NULL)
9768 ops = &bkpt_probe_breakpoint_ops;
9769 else
9770 ops = &bkpt_breakpoint_ops;
9771
9772 create_breakpoint (get_current_arch (),
9773 arg,
9774 NULL, 0, NULL, 1 /* parse arg */,
9775 tempflag, type_wanted,
9776 0 /* Ignore count */,
9777 pending_break_support,
9778 ops,
9779 from_tty,
9780 1 /* enabled */,
9781 0 /* internal */,
9782 0);
9783 }
9784
9785 /* Helper function for break_command_1 and disassemble_command. */
9786
9787 void
9788 resolve_sal_pc (struct symtab_and_line *sal)
9789 {
9790 CORE_ADDR pc;
9791
9792 if (sal->pc == 0 && sal->symtab != NULL)
9793 {
9794 if (!find_line_pc (sal->symtab, sal->line, &pc))
9795 error (_("No line %d in file \"%s\"."),
9796 sal->line, symtab_to_filename_for_display (sal->symtab));
9797 sal->pc = pc;
9798
9799 /* If this SAL corresponds to a breakpoint inserted using a line
9800 number, then skip the function prologue if necessary. */
9801 if (sal->explicit_line)
9802 skip_prologue_sal (sal);
9803 }
9804
9805 if (sal->section == 0 && sal->symtab != NULL)
9806 {
9807 struct blockvector *bv;
9808 struct block *b;
9809 struct symbol *sym;
9810
9811 bv = blockvector_for_pc_sect (sal->pc, 0, &b, sal->symtab);
9812 if (bv != NULL)
9813 {
9814 sym = block_linkage_function (b);
9815 if (sym != NULL)
9816 {
9817 fixup_symbol_section (sym, sal->symtab->objfile);
9818 sal->section = SYMBOL_OBJ_SECTION (sal->symtab->objfile, sym);
9819 }
9820 else
9821 {
9822 /* It really is worthwhile to have the section, so we'll
9823 just have to look harder. This case can be executed
9824 if we have line numbers but no functions (as can
9825 happen in assembly source). */
9826
9827 struct bound_minimal_symbol msym;
9828 struct cleanup *old_chain = save_current_space_and_thread ();
9829
9830 switch_to_program_space_and_thread (sal->pspace);
9831
9832 msym = lookup_minimal_symbol_by_pc (sal->pc);
9833 if (msym.minsym)
9834 sal->section = SYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
9835
9836 do_cleanups (old_chain);
9837 }
9838 }
9839 }
9840 }
9841
9842 void
9843 break_command (char *arg, int from_tty)
9844 {
9845 break_command_1 (arg, 0, from_tty);
9846 }
9847
9848 void
9849 tbreak_command (char *arg, int from_tty)
9850 {
9851 break_command_1 (arg, BP_TEMPFLAG, from_tty);
9852 }
9853
9854 static void
9855 hbreak_command (char *arg, int from_tty)
9856 {
9857 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
9858 }
9859
9860 static void
9861 thbreak_command (char *arg, int from_tty)
9862 {
9863 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
9864 }
9865
9866 static void
9867 stop_command (char *arg, int from_tty)
9868 {
9869 printf_filtered (_("Specify the type of breakpoint to set.\n\
9870 Usage: stop in <function | address>\n\
9871 stop at <line>\n"));
9872 }
9873
9874 static void
9875 stopin_command (char *arg, int from_tty)
9876 {
9877 int badInput = 0;
9878
9879 if (arg == (char *) NULL)
9880 badInput = 1;
9881 else if (*arg != '*')
9882 {
9883 char *argptr = arg;
9884 int hasColon = 0;
9885
9886 /* Look for a ':'. If this is a line number specification, then
9887 say it is bad, otherwise, it should be an address or
9888 function/method name. */
9889 while (*argptr && !hasColon)
9890 {
9891 hasColon = (*argptr == ':');
9892 argptr++;
9893 }
9894
9895 if (hasColon)
9896 badInput = (*argptr != ':'); /* Not a class::method */
9897 else
9898 badInput = isdigit (*arg); /* a simple line number */
9899 }
9900
9901 if (badInput)
9902 printf_filtered (_("Usage: stop in <function | address>\n"));
9903 else
9904 break_command_1 (arg, 0, from_tty);
9905 }
9906
9907 static void
9908 stopat_command (char *arg, int from_tty)
9909 {
9910 int badInput = 0;
9911
9912 if (arg == (char *) NULL || *arg == '*') /* no line number */
9913 badInput = 1;
9914 else
9915 {
9916 char *argptr = arg;
9917 int hasColon = 0;
9918
9919 /* Look for a ':'. If there is a '::' then get out, otherwise
9920 it is probably a line number. */
9921 while (*argptr && !hasColon)
9922 {
9923 hasColon = (*argptr == ':');
9924 argptr++;
9925 }
9926
9927 if (hasColon)
9928 badInput = (*argptr == ':'); /* we have class::method */
9929 else
9930 badInput = !isdigit (*arg); /* not a line number */
9931 }
9932
9933 if (badInput)
9934 printf_filtered (_("Usage: stop at <line>\n"));
9935 else
9936 break_command_1 (arg, 0, from_tty);
9937 }
9938
9939 /* The dynamic printf command is mostly like a regular breakpoint, but
9940 with a prewired command list consisting of a single output command,
9941 built from extra arguments supplied on the dprintf command
9942 line. */
9943
9944 static void
9945 dprintf_command (char *arg, int from_tty)
9946 {
9947 create_breakpoint (get_current_arch (),
9948 arg,
9949 NULL, 0, NULL, 1 /* parse arg */,
9950 0, bp_dprintf,
9951 0 /* Ignore count */,
9952 pending_break_support,
9953 &dprintf_breakpoint_ops,
9954 from_tty,
9955 1 /* enabled */,
9956 0 /* internal */,
9957 0);
9958 }
9959
9960 static void
9961 agent_printf_command (char *arg, int from_tty)
9962 {
9963 error (_("May only run agent-printf on the target"));
9964 }
9965
9966 /* Implement the "breakpoint_hit" breakpoint_ops method for
9967 ranged breakpoints. */
9968
9969 static int
9970 breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
9971 struct address_space *aspace,
9972 CORE_ADDR bp_addr,
9973 const struct target_waitstatus *ws)
9974 {
9975 if (ws->kind != TARGET_WAITKIND_STOPPED
9976 || ws->value.sig != GDB_SIGNAL_TRAP)
9977 return 0;
9978
9979 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9980 bl->length, aspace, bp_addr);
9981 }
9982
9983 /* Implement the "resources_needed" breakpoint_ops method for
9984 ranged breakpoints. */
9985
9986 static int
9987 resources_needed_ranged_breakpoint (const struct bp_location *bl)
9988 {
9989 return target_ranged_break_num_registers ();
9990 }
9991
9992 /* Implement the "print_it" breakpoint_ops method for
9993 ranged breakpoints. */
9994
9995 static enum print_stop_action
9996 print_it_ranged_breakpoint (bpstat bs)
9997 {
9998 struct breakpoint *b = bs->breakpoint_at;
9999 struct bp_location *bl = b->loc;
10000 struct ui_out *uiout = current_uiout;
10001
10002 gdb_assert (b->type == bp_hardware_breakpoint);
10003
10004 /* Ranged breakpoints have only one location. */
10005 gdb_assert (bl && bl->next == NULL);
10006
10007 annotate_breakpoint (b->number);
10008 if (b->disposition == disp_del)
10009 ui_out_text (uiout, "\nTemporary ranged breakpoint ");
10010 else
10011 ui_out_text (uiout, "\nRanged breakpoint ");
10012 if (ui_out_is_mi_like_p (uiout))
10013 {
10014 ui_out_field_string (uiout, "reason",
10015 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
10016 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
10017 }
10018 ui_out_field_int (uiout, "bkptno", b->number);
10019 ui_out_text (uiout, ", ");
10020
10021 return PRINT_SRC_AND_LOC;
10022 }
10023
10024 /* Implement the "print_one" breakpoint_ops method for
10025 ranged breakpoints. */
10026
10027 static void
10028 print_one_ranged_breakpoint (struct breakpoint *b,
10029 struct bp_location **last_loc)
10030 {
10031 struct bp_location *bl = b->loc;
10032 struct value_print_options opts;
10033 struct ui_out *uiout = current_uiout;
10034
10035 /* Ranged breakpoints have only one location. */
10036 gdb_assert (bl && bl->next == NULL);
10037
10038 get_user_print_options (&opts);
10039
10040 if (opts.addressprint)
10041 /* We don't print the address range here, it will be printed later
10042 by print_one_detail_ranged_breakpoint. */
10043 ui_out_field_skip (uiout, "addr");
10044 annotate_field (5);
10045 print_breakpoint_location (b, bl);
10046 *last_loc = bl;
10047 }
10048
10049 /* Implement the "print_one_detail" breakpoint_ops method for
10050 ranged breakpoints. */
10051
10052 static void
10053 print_one_detail_ranged_breakpoint (const struct breakpoint *b,
10054 struct ui_out *uiout)
10055 {
10056 CORE_ADDR address_start, address_end;
10057 struct bp_location *bl = b->loc;
10058 struct ui_file *stb = mem_fileopen ();
10059 struct cleanup *cleanup = make_cleanup_ui_file_delete (stb);
10060
10061 gdb_assert (bl);
10062
10063 address_start = bl->address;
10064 address_end = address_start + bl->length - 1;
10065
10066 ui_out_text (uiout, "\taddress range: ");
10067 fprintf_unfiltered (stb, "[%s, %s]",
10068 print_core_address (bl->gdbarch, address_start),
10069 print_core_address (bl->gdbarch, address_end));
10070 ui_out_field_stream (uiout, "addr", stb);
10071 ui_out_text (uiout, "\n");
10072
10073 do_cleanups (cleanup);
10074 }
10075
10076 /* Implement the "print_mention" breakpoint_ops method for
10077 ranged breakpoints. */
10078
10079 static void
10080 print_mention_ranged_breakpoint (struct breakpoint *b)
10081 {
10082 struct bp_location *bl = b->loc;
10083 struct ui_out *uiout = current_uiout;
10084
10085 gdb_assert (bl);
10086 gdb_assert (b->type == bp_hardware_breakpoint);
10087
10088 if (ui_out_is_mi_like_p (uiout))
10089 return;
10090
10091 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
10092 b->number, paddress (bl->gdbarch, bl->address),
10093 paddress (bl->gdbarch, bl->address + bl->length - 1));
10094 }
10095
10096 /* Implement the "print_recreate" breakpoint_ops method for
10097 ranged breakpoints. */
10098
10099 static void
10100 print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
10101 {
10102 fprintf_unfiltered (fp, "break-range %s, %s", b->addr_string,
10103 b->addr_string_range_end);
10104 print_recreate_thread (b, fp);
10105 }
10106
10107 /* The breakpoint_ops structure to be used in ranged breakpoints. */
10108
10109 static struct breakpoint_ops ranged_breakpoint_ops;
10110
10111 /* Find the address where the end of the breakpoint range should be
10112 placed, given the SAL of the end of the range. This is so that if
10113 the user provides a line number, the end of the range is set to the
10114 last instruction of the given line. */
10115
10116 static CORE_ADDR
10117 find_breakpoint_range_end (struct symtab_and_line sal)
10118 {
10119 CORE_ADDR end;
10120
10121 /* If the user provided a PC value, use it. Otherwise,
10122 find the address of the end of the given location. */
10123 if (sal.explicit_pc)
10124 end = sal.pc;
10125 else
10126 {
10127 int ret;
10128 CORE_ADDR start;
10129
10130 ret = find_line_pc_range (sal, &start, &end);
10131 if (!ret)
10132 error (_("Could not find location of the end of the range."));
10133
10134 /* find_line_pc_range returns the start of the next line. */
10135 end--;
10136 }
10137
10138 return end;
10139 }
10140
10141 /* Implement the "break-range" CLI command. */
10142
10143 static void
10144 break_range_command (char *arg, int from_tty)
10145 {
10146 char *arg_start, *addr_string_start, *addr_string_end;
10147 struct linespec_result canonical_start, canonical_end;
10148 int bp_count, can_use_bp, length;
10149 CORE_ADDR end;
10150 struct breakpoint *b;
10151 struct symtab_and_line sal_start, sal_end;
10152 struct cleanup *cleanup_bkpt;
10153 struct linespec_sals *lsal_start, *lsal_end;
10154
10155 /* We don't support software ranged breakpoints. */
10156 if (target_ranged_break_num_registers () < 0)
10157 error (_("This target does not support hardware ranged breakpoints."));
10158
10159 bp_count = hw_breakpoint_used_count ();
10160 bp_count += target_ranged_break_num_registers ();
10161 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10162 bp_count, 0);
10163 if (can_use_bp < 0)
10164 error (_("Hardware breakpoints used exceeds limit."));
10165
10166 arg = skip_spaces (arg);
10167 if (arg == NULL || arg[0] == '\0')
10168 error(_("No address range specified."));
10169
10170 init_linespec_result (&canonical_start);
10171
10172 arg_start = arg;
10173 parse_breakpoint_sals (&arg, &canonical_start);
10174
10175 cleanup_bkpt = make_cleanup_destroy_linespec_result (&canonical_start);
10176
10177 if (arg[0] != ',')
10178 error (_("Too few arguments."));
10179 else if (VEC_empty (linespec_sals, canonical_start.sals))
10180 error (_("Could not find location of the beginning of the range."));
10181
10182 lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
10183
10184 if (VEC_length (linespec_sals, canonical_start.sals) > 1
10185 || lsal_start->sals.nelts != 1)
10186 error (_("Cannot create a ranged breakpoint with multiple locations."));
10187
10188 sal_start = lsal_start->sals.sals[0];
10189 addr_string_start = savestring (arg_start, arg - arg_start);
10190 make_cleanup (xfree, addr_string_start);
10191
10192 arg++; /* Skip the comma. */
10193 arg = skip_spaces (arg);
10194
10195 /* Parse the end location. */
10196
10197 init_linespec_result (&canonical_end);
10198 arg_start = arg;
10199
10200 /* We call decode_line_full directly here instead of using
10201 parse_breakpoint_sals because we need to specify the start location's
10202 symtab and line as the default symtab and line for the end of the
10203 range. This makes it possible to have ranges like "foo.c:27, +14",
10204 where +14 means 14 lines from the start location. */
10205 decode_line_full (&arg, DECODE_LINE_FUNFIRSTLINE,
10206 sal_start.symtab, sal_start.line,
10207 &canonical_end, NULL, NULL);
10208
10209 make_cleanup_destroy_linespec_result (&canonical_end);
10210
10211 if (VEC_empty (linespec_sals, canonical_end.sals))
10212 error (_("Could not find location of the end of the range."));
10213
10214 lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
10215 if (VEC_length (linespec_sals, canonical_end.sals) > 1
10216 || lsal_end->sals.nelts != 1)
10217 error (_("Cannot create a ranged breakpoint with multiple locations."));
10218
10219 sal_end = lsal_end->sals.sals[0];
10220 addr_string_end = savestring (arg_start, arg - arg_start);
10221 make_cleanup (xfree, addr_string_end);
10222
10223 end = find_breakpoint_range_end (sal_end);
10224 if (sal_start.pc > end)
10225 error (_("Invalid address range, end precedes start."));
10226
10227 length = end - sal_start.pc + 1;
10228 if (length < 0)
10229 /* Length overflowed. */
10230 error (_("Address range too large."));
10231 else if (length == 1)
10232 {
10233 /* This range is simple enough to be handled by
10234 the `hbreak' command. */
10235 hbreak_command (addr_string_start, 1);
10236
10237 do_cleanups (cleanup_bkpt);
10238
10239 return;
10240 }
10241
10242 /* Now set up the breakpoint. */
10243 b = set_raw_breakpoint (get_current_arch (), sal_start,
10244 bp_hardware_breakpoint, &ranged_breakpoint_ops);
10245 set_breakpoint_count (breakpoint_count + 1);
10246 b->number = breakpoint_count;
10247 b->disposition = disp_donttouch;
10248 b->addr_string = xstrdup (addr_string_start);
10249 b->addr_string_range_end = xstrdup (addr_string_end);
10250 b->loc->length = length;
10251
10252 do_cleanups (cleanup_bkpt);
10253
10254 mention (b);
10255 observer_notify_breakpoint_created (b);
10256 update_global_location_list (1);
10257 }
10258
10259 /* Return non-zero if EXP is verified as constant. Returned zero
10260 means EXP is variable. Also the constant detection may fail for
10261 some constant expressions and in such case still falsely return
10262 zero. */
10263
10264 static int
10265 watchpoint_exp_is_const (const struct expression *exp)
10266 {
10267 int i = exp->nelts;
10268
10269 while (i > 0)
10270 {
10271 int oplenp, argsp;
10272
10273 /* We are only interested in the descriptor of each element. */
10274 operator_length (exp, i, &oplenp, &argsp);
10275 i -= oplenp;
10276
10277 switch (exp->elts[i].opcode)
10278 {
10279 case BINOP_ADD:
10280 case BINOP_SUB:
10281 case BINOP_MUL:
10282 case BINOP_DIV:
10283 case BINOP_REM:
10284 case BINOP_MOD:
10285 case BINOP_LSH:
10286 case BINOP_RSH:
10287 case BINOP_LOGICAL_AND:
10288 case BINOP_LOGICAL_OR:
10289 case BINOP_BITWISE_AND:
10290 case BINOP_BITWISE_IOR:
10291 case BINOP_BITWISE_XOR:
10292 case BINOP_EQUAL:
10293 case BINOP_NOTEQUAL:
10294 case BINOP_LESS:
10295 case BINOP_GTR:
10296 case BINOP_LEQ:
10297 case BINOP_GEQ:
10298 case BINOP_REPEAT:
10299 case BINOP_COMMA:
10300 case BINOP_EXP:
10301 case BINOP_MIN:
10302 case BINOP_MAX:
10303 case BINOP_INTDIV:
10304 case BINOP_CONCAT:
10305 case BINOP_IN:
10306 case BINOP_RANGE:
10307 case TERNOP_COND:
10308 case TERNOP_SLICE:
10309
10310 case OP_LONG:
10311 case OP_DOUBLE:
10312 case OP_DECFLOAT:
10313 case OP_LAST:
10314 case OP_COMPLEX:
10315 case OP_STRING:
10316 case OP_ARRAY:
10317 case OP_TYPE:
10318 case OP_TYPEOF:
10319 case OP_DECLTYPE:
10320 case OP_NAME:
10321 case OP_OBJC_NSSTRING:
10322
10323 case UNOP_NEG:
10324 case UNOP_LOGICAL_NOT:
10325 case UNOP_COMPLEMENT:
10326 case UNOP_ADDR:
10327 case UNOP_HIGH:
10328 case UNOP_CAST:
10329
10330 case UNOP_CAST_TYPE:
10331 case UNOP_REINTERPRET_CAST:
10332 case UNOP_DYNAMIC_CAST:
10333 /* Unary, binary and ternary operators: We have to check
10334 their operands. If they are constant, then so is the
10335 result of that operation. For instance, if A and B are
10336 determined to be constants, then so is "A + B".
10337
10338 UNOP_IND is one exception to the rule above, because the
10339 value of *ADDR is not necessarily a constant, even when
10340 ADDR is. */
10341 break;
10342
10343 case OP_VAR_VALUE:
10344 /* Check whether the associated symbol is a constant.
10345
10346 We use SYMBOL_CLASS rather than TYPE_CONST because it's
10347 possible that a buggy compiler could mark a variable as
10348 constant even when it is not, and TYPE_CONST would return
10349 true in this case, while SYMBOL_CLASS wouldn't.
10350
10351 We also have to check for function symbols because they
10352 are always constant. */
10353 {
10354 struct symbol *s = exp->elts[i + 2].symbol;
10355
10356 if (SYMBOL_CLASS (s) != LOC_BLOCK
10357 && SYMBOL_CLASS (s) != LOC_CONST
10358 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10359 return 0;
10360 break;
10361 }
10362
10363 /* The default action is to return 0 because we are using
10364 the optimistic approach here: If we don't know something,
10365 then it is not a constant. */
10366 default:
10367 return 0;
10368 }
10369 }
10370
10371 return 1;
10372 }
10373
10374 /* Implement the "dtor" breakpoint_ops method for watchpoints. */
10375
10376 static void
10377 dtor_watchpoint (struct breakpoint *self)
10378 {
10379 struct watchpoint *w = (struct watchpoint *) self;
10380
10381 xfree (w->cond_exp);
10382 xfree (w->exp);
10383 xfree (w->exp_string);
10384 xfree (w->exp_string_reparse);
10385 value_free (w->val);
10386
10387 base_breakpoint_ops.dtor (self);
10388 }
10389
10390 /* Implement the "re_set" breakpoint_ops method for watchpoints. */
10391
10392 static void
10393 re_set_watchpoint (struct breakpoint *b)
10394 {
10395 struct watchpoint *w = (struct watchpoint *) b;
10396
10397 /* Watchpoint can be either on expression using entirely global
10398 variables, or it can be on local variables.
10399
10400 Watchpoints of the first kind are never auto-deleted, and even
10401 persist across program restarts. Since they can use variables
10402 from shared libraries, we need to reparse expression as libraries
10403 are loaded and unloaded.
10404
10405 Watchpoints on local variables can also change meaning as result
10406 of solib event. For example, if a watchpoint uses both a local
10407 and a global variables in expression, it's a local watchpoint,
10408 but unloading of a shared library will make the expression
10409 invalid. This is not a very common use case, but we still
10410 re-evaluate expression, to avoid surprises to the user.
10411
10412 Note that for local watchpoints, we re-evaluate it only if
10413 watchpoints frame id is still valid. If it's not, it means the
10414 watchpoint is out of scope and will be deleted soon. In fact,
10415 I'm not sure we'll ever be called in this case.
10416
10417 If a local watchpoint's frame id is still valid, then
10418 w->exp_valid_block is likewise valid, and we can safely use it.
10419
10420 Don't do anything about disabled watchpoints, since they will be
10421 reevaluated again when enabled. */
10422 update_watchpoint (w, 1 /* reparse */);
10423 }
10424
10425 /* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10426
10427 static int
10428 insert_watchpoint (struct bp_location *bl)
10429 {
10430 struct watchpoint *w = (struct watchpoint *) bl->owner;
10431 int length = w->exact ? 1 : bl->length;
10432
10433 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
10434 w->cond_exp);
10435 }
10436
10437 /* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10438
10439 static int
10440 remove_watchpoint (struct bp_location *bl)
10441 {
10442 struct watchpoint *w = (struct watchpoint *) bl->owner;
10443 int length = w->exact ? 1 : bl->length;
10444
10445 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
10446 w->cond_exp);
10447 }
10448
10449 static int
10450 breakpoint_hit_watchpoint (const struct bp_location *bl,
10451 struct address_space *aspace, CORE_ADDR bp_addr,
10452 const struct target_waitstatus *ws)
10453 {
10454 struct breakpoint *b = bl->owner;
10455 struct watchpoint *w = (struct watchpoint *) b;
10456
10457 /* Continuable hardware watchpoints are treated as non-existent if the
10458 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10459 some data address). Otherwise gdb won't stop on a break instruction
10460 in the code (not from a breakpoint) when a hardware watchpoint has
10461 been defined. Also skip watchpoints which we know did not trigger
10462 (did not match the data address). */
10463 if (is_hardware_watchpoint (b)
10464 && w->watchpoint_triggered == watch_triggered_no)
10465 return 0;
10466
10467 return 1;
10468 }
10469
10470 static void
10471 check_status_watchpoint (bpstat bs)
10472 {
10473 gdb_assert (is_watchpoint (bs->breakpoint_at));
10474
10475 bpstat_check_watchpoint (bs);
10476 }
10477
10478 /* Implement the "resources_needed" breakpoint_ops method for
10479 hardware watchpoints. */
10480
10481 static int
10482 resources_needed_watchpoint (const struct bp_location *bl)
10483 {
10484 struct watchpoint *w = (struct watchpoint *) bl->owner;
10485 int length = w->exact? 1 : bl->length;
10486
10487 return target_region_ok_for_hw_watchpoint (bl->address, length);
10488 }
10489
10490 /* Implement the "works_in_software_mode" breakpoint_ops method for
10491 hardware watchpoints. */
10492
10493 static int
10494 works_in_software_mode_watchpoint (const struct breakpoint *b)
10495 {
10496 /* Read and access watchpoints only work with hardware support. */
10497 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
10498 }
10499
10500 static enum print_stop_action
10501 print_it_watchpoint (bpstat bs)
10502 {
10503 struct cleanup *old_chain;
10504 struct breakpoint *b;
10505 struct ui_file *stb;
10506 enum print_stop_action result;
10507 struct watchpoint *w;
10508 struct ui_out *uiout = current_uiout;
10509
10510 gdb_assert (bs->bp_location_at != NULL);
10511
10512 b = bs->breakpoint_at;
10513 w = (struct watchpoint *) b;
10514
10515 stb = mem_fileopen ();
10516 old_chain = make_cleanup_ui_file_delete (stb);
10517
10518 switch (b->type)
10519 {
10520 case bp_watchpoint:
10521 case bp_hardware_watchpoint:
10522 annotate_watchpoint (b->number);
10523 if (ui_out_is_mi_like_p (uiout))
10524 ui_out_field_string
10525 (uiout, "reason",
10526 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10527 mention (b);
10528 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10529 ui_out_text (uiout, "\nOld value = ");
10530 watchpoint_value_print (bs->old_val, stb);
10531 ui_out_field_stream (uiout, "old", stb);
10532 ui_out_text (uiout, "\nNew value = ");
10533 watchpoint_value_print (w->val, stb);
10534 ui_out_field_stream (uiout, "new", stb);
10535 ui_out_text (uiout, "\n");
10536 /* More than one watchpoint may have been triggered. */
10537 result = PRINT_UNKNOWN;
10538 break;
10539
10540 case bp_read_watchpoint:
10541 if (ui_out_is_mi_like_p (uiout))
10542 ui_out_field_string
10543 (uiout, "reason",
10544 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10545 mention (b);
10546 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10547 ui_out_text (uiout, "\nValue = ");
10548 watchpoint_value_print (w->val, stb);
10549 ui_out_field_stream (uiout, "value", stb);
10550 ui_out_text (uiout, "\n");
10551 result = PRINT_UNKNOWN;
10552 break;
10553
10554 case bp_access_watchpoint:
10555 if (bs->old_val != NULL)
10556 {
10557 annotate_watchpoint (b->number);
10558 if (ui_out_is_mi_like_p (uiout))
10559 ui_out_field_string
10560 (uiout, "reason",
10561 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10562 mention (b);
10563 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10564 ui_out_text (uiout, "\nOld value = ");
10565 watchpoint_value_print (bs->old_val, stb);
10566 ui_out_field_stream (uiout, "old", stb);
10567 ui_out_text (uiout, "\nNew value = ");
10568 }
10569 else
10570 {
10571 mention (b);
10572 if (ui_out_is_mi_like_p (uiout))
10573 ui_out_field_string
10574 (uiout, "reason",
10575 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10576 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10577 ui_out_text (uiout, "\nValue = ");
10578 }
10579 watchpoint_value_print (w->val, stb);
10580 ui_out_field_stream (uiout, "new", stb);
10581 ui_out_text (uiout, "\n");
10582 result = PRINT_UNKNOWN;
10583 break;
10584 default:
10585 result = PRINT_UNKNOWN;
10586 }
10587
10588 do_cleanups (old_chain);
10589 return result;
10590 }
10591
10592 /* Implement the "print_mention" breakpoint_ops method for hardware
10593 watchpoints. */
10594
10595 static void
10596 print_mention_watchpoint (struct breakpoint *b)
10597 {
10598 struct cleanup *ui_out_chain;
10599 struct watchpoint *w = (struct watchpoint *) b;
10600 struct ui_out *uiout = current_uiout;
10601
10602 switch (b->type)
10603 {
10604 case bp_watchpoint:
10605 ui_out_text (uiout, "Watchpoint ");
10606 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10607 break;
10608 case bp_hardware_watchpoint:
10609 ui_out_text (uiout, "Hardware watchpoint ");
10610 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10611 break;
10612 case bp_read_watchpoint:
10613 ui_out_text (uiout, "Hardware read watchpoint ");
10614 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
10615 break;
10616 case bp_access_watchpoint:
10617 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
10618 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
10619 break;
10620 default:
10621 internal_error (__FILE__, __LINE__,
10622 _("Invalid hardware watchpoint type."));
10623 }
10624
10625 ui_out_field_int (uiout, "number", b->number);
10626 ui_out_text (uiout, ": ");
10627 ui_out_field_string (uiout, "exp", w->exp_string);
10628 do_cleanups (ui_out_chain);
10629 }
10630
10631 /* Implement the "print_recreate" breakpoint_ops method for
10632 watchpoints. */
10633
10634 static void
10635 print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10636 {
10637 struct watchpoint *w = (struct watchpoint *) b;
10638
10639 switch (b->type)
10640 {
10641 case bp_watchpoint:
10642 case bp_hardware_watchpoint:
10643 fprintf_unfiltered (fp, "watch");
10644 break;
10645 case bp_read_watchpoint:
10646 fprintf_unfiltered (fp, "rwatch");
10647 break;
10648 case bp_access_watchpoint:
10649 fprintf_unfiltered (fp, "awatch");
10650 break;
10651 default:
10652 internal_error (__FILE__, __LINE__,
10653 _("Invalid watchpoint type."));
10654 }
10655
10656 fprintf_unfiltered (fp, " %s", w->exp_string);
10657 print_recreate_thread (b, fp);
10658 }
10659
10660 /* The breakpoint_ops structure to be used in hardware watchpoints. */
10661
10662 static struct breakpoint_ops watchpoint_breakpoint_ops;
10663
10664 /* Implement the "insert" breakpoint_ops method for
10665 masked hardware watchpoints. */
10666
10667 static int
10668 insert_masked_watchpoint (struct bp_location *bl)
10669 {
10670 struct watchpoint *w = (struct watchpoint *) bl->owner;
10671
10672 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
10673 bl->watchpoint_type);
10674 }
10675
10676 /* Implement the "remove" breakpoint_ops method for
10677 masked hardware watchpoints. */
10678
10679 static int
10680 remove_masked_watchpoint (struct bp_location *bl)
10681 {
10682 struct watchpoint *w = (struct watchpoint *) bl->owner;
10683
10684 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
10685 bl->watchpoint_type);
10686 }
10687
10688 /* Implement the "resources_needed" breakpoint_ops method for
10689 masked hardware watchpoints. */
10690
10691 static int
10692 resources_needed_masked_watchpoint (const struct bp_location *bl)
10693 {
10694 struct watchpoint *w = (struct watchpoint *) bl->owner;
10695
10696 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
10697 }
10698
10699 /* Implement the "works_in_software_mode" breakpoint_ops method for
10700 masked hardware watchpoints. */
10701
10702 static int
10703 works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10704 {
10705 return 0;
10706 }
10707
10708 /* Implement the "print_it" breakpoint_ops method for
10709 masked hardware watchpoints. */
10710
10711 static enum print_stop_action
10712 print_it_masked_watchpoint (bpstat bs)
10713 {
10714 struct breakpoint *b = bs->breakpoint_at;
10715 struct ui_out *uiout = current_uiout;
10716
10717 /* Masked watchpoints have only one location. */
10718 gdb_assert (b->loc && b->loc->next == NULL);
10719
10720 switch (b->type)
10721 {
10722 case bp_hardware_watchpoint:
10723 annotate_watchpoint (b->number);
10724 if (ui_out_is_mi_like_p (uiout))
10725 ui_out_field_string
10726 (uiout, "reason",
10727 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10728 break;
10729
10730 case bp_read_watchpoint:
10731 if (ui_out_is_mi_like_p (uiout))
10732 ui_out_field_string
10733 (uiout, "reason",
10734 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10735 break;
10736
10737 case bp_access_watchpoint:
10738 if (ui_out_is_mi_like_p (uiout))
10739 ui_out_field_string
10740 (uiout, "reason",
10741 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10742 break;
10743 default:
10744 internal_error (__FILE__, __LINE__,
10745 _("Invalid hardware watchpoint type."));
10746 }
10747
10748 mention (b);
10749 ui_out_text (uiout, _("\n\
10750 Check the underlying instruction at PC for the memory\n\
10751 address and value which triggered this watchpoint.\n"));
10752 ui_out_text (uiout, "\n");
10753
10754 /* More than one watchpoint may have been triggered. */
10755 return PRINT_UNKNOWN;
10756 }
10757
10758 /* Implement the "print_one_detail" breakpoint_ops method for
10759 masked hardware watchpoints. */
10760
10761 static void
10762 print_one_detail_masked_watchpoint (const struct breakpoint *b,
10763 struct ui_out *uiout)
10764 {
10765 struct watchpoint *w = (struct watchpoint *) b;
10766
10767 /* Masked watchpoints have only one location. */
10768 gdb_assert (b->loc && b->loc->next == NULL);
10769
10770 ui_out_text (uiout, "\tmask ");
10771 ui_out_field_core_addr (uiout, "mask", b->loc->gdbarch, w->hw_wp_mask);
10772 ui_out_text (uiout, "\n");
10773 }
10774
10775 /* Implement the "print_mention" breakpoint_ops method for
10776 masked hardware watchpoints. */
10777
10778 static void
10779 print_mention_masked_watchpoint (struct breakpoint *b)
10780 {
10781 struct watchpoint *w = (struct watchpoint *) b;
10782 struct ui_out *uiout = current_uiout;
10783 struct cleanup *ui_out_chain;
10784
10785 switch (b->type)
10786 {
10787 case bp_hardware_watchpoint:
10788 ui_out_text (uiout, "Masked hardware watchpoint ");
10789 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10790 break;
10791 case bp_read_watchpoint:
10792 ui_out_text (uiout, "Masked hardware read watchpoint ");
10793 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
10794 break;
10795 case bp_access_watchpoint:
10796 ui_out_text (uiout, "Masked hardware access (read/write) watchpoint ");
10797 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
10798 break;
10799 default:
10800 internal_error (__FILE__, __LINE__,
10801 _("Invalid hardware watchpoint type."));
10802 }
10803
10804 ui_out_field_int (uiout, "number", b->number);
10805 ui_out_text (uiout, ": ");
10806 ui_out_field_string (uiout, "exp", w->exp_string);
10807 do_cleanups (ui_out_chain);
10808 }
10809
10810 /* Implement the "print_recreate" breakpoint_ops method for
10811 masked hardware watchpoints. */
10812
10813 static void
10814 print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10815 {
10816 struct watchpoint *w = (struct watchpoint *) b;
10817 char tmp[40];
10818
10819 switch (b->type)
10820 {
10821 case bp_hardware_watchpoint:
10822 fprintf_unfiltered (fp, "watch");
10823 break;
10824 case bp_read_watchpoint:
10825 fprintf_unfiltered (fp, "rwatch");
10826 break;
10827 case bp_access_watchpoint:
10828 fprintf_unfiltered (fp, "awatch");
10829 break;
10830 default:
10831 internal_error (__FILE__, __LINE__,
10832 _("Invalid hardware watchpoint type."));
10833 }
10834
10835 sprintf_vma (tmp, w->hw_wp_mask);
10836 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
10837 print_recreate_thread (b, fp);
10838 }
10839
10840 /* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10841
10842 static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
10843
10844 /* Tell whether the given watchpoint is a masked hardware watchpoint. */
10845
10846 static int
10847 is_masked_watchpoint (const struct breakpoint *b)
10848 {
10849 return b->ops == &masked_watchpoint_breakpoint_ops;
10850 }
10851
10852 /* accessflag: hw_write: watch write,
10853 hw_read: watch read,
10854 hw_access: watch access (read or write) */
10855 static void
10856 watch_command_1 (const char *arg, int accessflag, int from_tty,
10857 int just_location, int internal)
10858 {
10859 volatile struct gdb_exception e;
10860 struct breakpoint *b, *scope_breakpoint = NULL;
10861 struct expression *exp;
10862 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
10863 struct value *val, *mark, *result;
10864 struct frame_info *frame;
10865 const char *exp_start = NULL;
10866 const char *exp_end = NULL;
10867 const char *tok, *end_tok;
10868 int toklen = -1;
10869 const char *cond_start = NULL;
10870 const char *cond_end = NULL;
10871 enum bptype bp_type;
10872 int thread = -1;
10873 int pc = 0;
10874 /* Flag to indicate whether we are going to use masks for
10875 the hardware watchpoint. */
10876 int use_mask = 0;
10877 CORE_ADDR mask = 0;
10878 struct watchpoint *w;
10879 char *expression;
10880 struct cleanup *back_to;
10881
10882 /* Make sure that we actually have parameters to parse. */
10883 if (arg != NULL && arg[0] != '\0')
10884 {
10885 const char *value_start;
10886
10887 exp_end = arg + strlen (arg);
10888
10889 /* Look for "parameter value" pairs at the end
10890 of the arguments string. */
10891 for (tok = exp_end - 1; tok > arg; tok--)
10892 {
10893 /* Skip whitespace at the end of the argument list. */
10894 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10895 tok--;
10896
10897 /* Find the beginning of the last token.
10898 This is the value of the parameter. */
10899 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10900 tok--;
10901 value_start = tok + 1;
10902
10903 /* Skip whitespace. */
10904 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10905 tok--;
10906
10907 end_tok = tok;
10908
10909 /* Find the beginning of the second to last token.
10910 This is the parameter itself. */
10911 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10912 tok--;
10913 tok++;
10914 toklen = end_tok - tok + 1;
10915
10916 if (toklen == 6 && !strncmp (tok, "thread", 6))
10917 {
10918 /* At this point we've found a "thread" token, which means
10919 the user is trying to set a watchpoint that triggers
10920 only in a specific thread. */
10921 char *endp;
10922
10923 if (thread != -1)
10924 error(_("You can specify only one thread."));
10925
10926 /* Extract the thread ID from the next token. */
10927 thread = strtol (value_start, &endp, 0);
10928
10929 /* Check if the user provided a valid numeric value for the
10930 thread ID. */
10931 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
10932 error (_("Invalid thread ID specification %s."), value_start);
10933
10934 /* Check if the thread actually exists. */
10935 if (!valid_thread_id (thread))
10936 invalid_thread_id_error (thread);
10937 }
10938 else if (toklen == 4 && !strncmp (tok, "mask", 4))
10939 {
10940 /* We've found a "mask" token, which means the user wants to
10941 create a hardware watchpoint that is going to have the mask
10942 facility. */
10943 struct value *mask_value, *mark;
10944
10945 if (use_mask)
10946 error(_("You can specify only one mask."));
10947
10948 use_mask = just_location = 1;
10949
10950 mark = value_mark ();
10951 mask_value = parse_to_comma_and_eval (&value_start);
10952 mask = value_as_address (mask_value);
10953 value_free_to_mark (mark);
10954 }
10955 else
10956 /* We didn't recognize what we found. We should stop here. */
10957 break;
10958
10959 /* Truncate the string and get rid of the "parameter value" pair before
10960 the arguments string is parsed by the parse_exp_1 function. */
10961 exp_end = tok;
10962 }
10963 }
10964 else
10965 exp_end = arg;
10966
10967 /* Parse the rest of the arguments. From here on out, everything
10968 is in terms of a newly allocated string instead of the original
10969 ARG. */
10970 innermost_block = NULL;
10971 expression = savestring (arg, exp_end - arg);
10972 back_to = make_cleanup (xfree, expression);
10973 exp_start = arg = expression;
10974 exp = parse_exp_1 (&arg, 0, 0, 0);
10975 exp_end = arg;
10976 /* Remove trailing whitespace from the expression before saving it.
10977 This makes the eventual display of the expression string a bit
10978 prettier. */
10979 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10980 --exp_end;
10981
10982 /* Checking if the expression is not constant. */
10983 if (watchpoint_exp_is_const (exp))
10984 {
10985 int len;
10986
10987 len = exp_end - exp_start;
10988 while (len > 0 && isspace (exp_start[len - 1]))
10989 len--;
10990 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10991 }
10992
10993 exp_valid_block = innermost_block;
10994 mark = value_mark ();
10995 fetch_subexp_value (exp, &pc, &val, &result, NULL);
10996
10997 if (just_location)
10998 {
10999 int ret;
11000
11001 exp_valid_block = NULL;
11002 val = value_addr (result);
11003 release_value (val);
11004 value_free_to_mark (mark);
11005
11006 if (use_mask)
11007 {
11008 ret = target_masked_watch_num_registers (value_as_address (val),
11009 mask);
11010 if (ret == -1)
11011 error (_("This target does not support masked watchpoints."));
11012 else if (ret == -2)
11013 error (_("Invalid mask or memory region."));
11014 }
11015 }
11016 else if (val != NULL)
11017 release_value (val);
11018
11019 tok = skip_spaces_const (arg);
11020 end_tok = skip_to_space_const (tok);
11021
11022 toklen = end_tok - tok;
11023 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
11024 {
11025 struct expression *cond;
11026
11027 innermost_block = NULL;
11028 tok = cond_start = end_tok + 1;
11029 cond = parse_exp_1 (&tok, 0, 0, 0);
11030
11031 /* The watchpoint expression may not be local, but the condition
11032 may still be. E.g.: `watch global if local > 0'. */
11033 cond_exp_valid_block = innermost_block;
11034
11035 xfree (cond);
11036 cond_end = tok;
11037 }
11038 if (*tok)
11039 error (_("Junk at end of command."));
11040
11041 if (accessflag == hw_read)
11042 bp_type = bp_read_watchpoint;
11043 else if (accessflag == hw_access)
11044 bp_type = bp_access_watchpoint;
11045 else
11046 bp_type = bp_hardware_watchpoint;
11047
11048 frame = block_innermost_frame (exp_valid_block);
11049
11050 /* If the expression is "local", then set up a "watchpoint scope"
11051 breakpoint at the point where we've left the scope of the watchpoint
11052 expression. Create the scope breakpoint before the watchpoint, so
11053 that we will encounter it first in bpstat_stop_status. */
11054 if (exp_valid_block && frame)
11055 {
11056 if (frame_id_p (frame_unwind_caller_id (frame)))
11057 {
11058 scope_breakpoint
11059 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
11060 frame_unwind_caller_pc (frame),
11061 bp_watchpoint_scope,
11062 &momentary_breakpoint_ops);
11063
11064 scope_breakpoint->enable_state = bp_enabled;
11065
11066 /* Automatically delete the breakpoint when it hits. */
11067 scope_breakpoint->disposition = disp_del;
11068
11069 /* Only break in the proper frame (help with recursion). */
11070 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
11071
11072 /* Set the address at which we will stop. */
11073 scope_breakpoint->loc->gdbarch
11074 = frame_unwind_caller_arch (frame);
11075 scope_breakpoint->loc->requested_address
11076 = frame_unwind_caller_pc (frame);
11077 scope_breakpoint->loc->address
11078 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
11079 scope_breakpoint->loc->requested_address,
11080 scope_breakpoint->type);
11081 }
11082 }
11083
11084 /* Now set up the breakpoint. */
11085
11086 w = XCNEW (struct watchpoint);
11087 b = &w->base;
11088 if (use_mask)
11089 init_raw_breakpoint_without_location (b, NULL, bp_type,
11090 &masked_watchpoint_breakpoint_ops);
11091 else
11092 init_raw_breakpoint_without_location (b, NULL, bp_type,
11093 &watchpoint_breakpoint_ops);
11094 b->thread = thread;
11095 b->disposition = disp_donttouch;
11096 b->pspace = current_program_space;
11097 w->exp = exp;
11098 w->exp_valid_block = exp_valid_block;
11099 w->cond_exp_valid_block = cond_exp_valid_block;
11100 if (just_location)
11101 {
11102 struct type *t = value_type (val);
11103 CORE_ADDR addr = value_as_address (val);
11104 char *name;
11105
11106 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
11107 name = type_to_string (t);
11108
11109 w->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
11110 core_addr_to_string (addr));
11111 xfree (name);
11112
11113 w->exp_string = xstrprintf ("-location %.*s",
11114 (int) (exp_end - exp_start), exp_start);
11115
11116 /* The above expression is in C. */
11117 b->language = language_c;
11118 }
11119 else
11120 w->exp_string = savestring (exp_start, exp_end - exp_start);
11121
11122 if (use_mask)
11123 {
11124 w->hw_wp_mask = mask;
11125 }
11126 else
11127 {
11128 w->val = val;
11129 w->val_valid = 1;
11130 }
11131
11132 if (cond_start)
11133 b->cond_string = savestring (cond_start, cond_end - cond_start);
11134 else
11135 b->cond_string = 0;
11136
11137 if (frame)
11138 {
11139 w->watchpoint_frame = get_frame_id (frame);
11140 w->watchpoint_thread = inferior_ptid;
11141 }
11142 else
11143 {
11144 w->watchpoint_frame = null_frame_id;
11145 w->watchpoint_thread = null_ptid;
11146 }
11147
11148 if (scope_breakpoint != NULL)
11149 {
11150 /* The scope breakpoint is related to the watchpoint. We will
11151 need to act on them together. */
11152 b->related_breakpoint = scope_breakpoint;
11153 scope_breakpoint->related_breakpoint = b;
11154 }
11155
11156 if (!just_location)
11157 value_free_to_mark (mark);
11158
11159 TRY_CATCH (e, RETURN_MASK_ALL)
11160 {
11161 /* Finally update the new watchpoint. This creates the locations
11162 that should be inserted. */
11163 update_watchpoint (w, 1);
11164 }
11165 if (e.reason < 0)
11166 {
11167 delete_breakpoint (b);
11168 throw_exception (e);
11169 }
11170
11171 install_breakpoint (internal, b, 1);
11172 do_cleanups (back_to);
11173 }
11174
11175 /* Return count of debug registers needed to watch the given expression.
11176 If the watchpoint cannot be handled in hardware return zero. */
11177
11178 static int
11179 can_use_hardware_watchpoint (struct value *v)
11180 {
11181 int found_memory_cnt = 0;
11182 struct value *head = v;
11183
11184 /* Did the user specifically forbid us to use hardware watchpoints? */
11185 if (!can_use_hw_watchpoints)
11186 return 0;
11187
11188 /* Make sure that the value of the expression depends only upon
11189 memory contents, and values computed from them within GDB. If we
11190 find any register references or function calls, we can't use a
11191 hardware watchpoint.
11192
11193 The idea here is that evaluating an expression generates a series
11194 of values, one holding the value of every subexpression. (The
11195 expression a*b+c has five subexpressions: a, b, a*b, c, and
11196 a*b+c.) GDB's values hold almost enough information to establish
11197 the criteria given above --- they identify memory lvalues,
11198 register lvalues, computed values, etcetera. So we can evaluate
11199 the expression, and then scan the chain of values that leaves
11200 behind to decide whether we can detect any possible change to the
11201 expression's final value using only hardware watchpoints.
11202
11203 However, I don't think that the values returned by inferior
11204 function calls are special in any way. So this function may not
11205 notice that an expression involving an inferior function call
11206 can't be watched with hardware watchpoints. FIXME. */
11207 for (; v; v = value_next (v))
11208 {
11209 if (VALUE_LVAL (v) == lval_memory)
11210 {
11211 if (v != head && value_lazy (v))
11212 /* A lazy memory lvalue in the chain is one that GDB never
11213 needed to fetch; we either just used its address (e.g.,
11214 `a' in `a.b') or we never needed it at all (e.g., `a'
11215 in `a,b'). This doesn't apply to HEAD; if that is
11216 lazy then it was not readable, but watch it anyway. */
11217 ;
11218 else
11219 {
11220 /* Ahh, memory we actually used! Check if we can cover
11221 it with hardware watchpoints. */
11222 struct type *vtype = check_typedef (value_type (v));
11223
11224 /* We only watch structs and arrays if user asked for it
11225 explicitly, never if they just happen to appear in a
11226 middle of some value chain. */
11227 if (v == head
11228 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11229 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11230 {
11231 CORE_ADDR vaddr = value_address (v);
11232 int len;
11233 int num_regs;
11234
11235 len = (target_exact_watchpoints
11236 && is_scalar_type_recursive (vtype))?
11237 1 : TYPE_LENGTH (value_type (v));
11238
11239 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11240 if (!num_regs)
11241 return 0;
11242 else
11243 found_memory_cnt += num_regs;
11244 }
11245 }
11246 }
11247 else if (VALUE_LVAL (v) != not_lval
11248 && deprecated_value_modifiable (v) == 0)
11249 return 0; /* These are values from the history (e.g., $1). */
11250 else if (VALUE_LVAL (v) == lval_register)
11251 return 0; /* Cannot watch a register with a HW watchpoint. */
11252 }
11253
11254 /* The expression itself looks suitable for using a hardware
11255 watchpoint, but give the target machine a chance to reject it. */
11256 return found_memory_cnt;
11257 }
11258
11259 void
11260 watch_command_wrapper (char *arg, int from_tty, int internal)
11261 {
11262 watch_command_1 (arg, hw_write, from_tty, 0, internal);
11263 }
11264
11265 /* A helper function that looks for the "-location" argument and then
11266 calls watch_command_1. */
11267
11268 static void
11269 watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11270 {
11271 int just_location = 0;
11272
11273 if (arg
11274 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11275 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11276 {
11277 arg = skip_spaces (arg);
11278 just_location = 1;
11279 }
11280
11281 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
11282 }
11283
11284 static void
11285 watch_command (char *arg, int from_tty)
11286 {
11287 watch_maybe_just_location (arg, hw_write, from_tty);
11288 }
11289
11290 void
11291 rwatch_command_wrapper (char *arg, int from_tty, int internal)
11292 {
11293 watch_command_1 (arg, hw_read, from_tty, 0, internal);
11294 }
11295
11296 static void
11297 rwatch_command (char *arg, int from_tty)
11298 {
11299 watch_maybe_just_location (arg, hw_read, from_tty);
11300 }
11301
11302 void
11303 awatch_command_wrapper (char *arg, int from_tty, int internal)
11304 {
11305 watch_command_1 (arg, hw_access, from_tty, 0, internal);
11306 }
11307
11308 static void
11309 awatch_command (char *arg, int from_tty)
11310 {
11311 watch_maybe_just_location (arg, hw_access, from_tty);
11312 }
11313 \f
11314
11315 /* Helper routines for the until_command routine in infcmd.c. Here
11316 because it uses the mechanisms of breakpoints. */
11317
11318 struct until_break_command_continuation_args
11319 {
11320 struct breakpoint *breakpoint;
11321 struct breakpoint *breakpoint2;
11322 int thread_num;
11323 };
11324
11325 /* This function is called by fetch_inferior_event via the
11326 cmd_continuation pointer, to complete the until command. It takes
11327 care of cleaning up the temporary breakpoints set up by the until
11328 command. */
11329 static void
11330 until_break_command_continuation (void *arg, int err)
11331 {
11332 struct until_break_command_continuation_args *a = arg;
11333
11334 delete_breakpoint (a->breakpoint);
11335 if (a->breakpoint2)
11336 delete_breakpoint (a->breakpoint2);
11337 delete_longjmp_breakpoint (a->thread_num);
11338 }
11339
11340 void
11341 until_break_command (char *arg, int from_tty, int anywhere)
11342 {
11343 struct symtabs_and_lines sals;
11344 struct symtab_and_line sal;
11345 struct frame_info *frame;
11346 struct gdbarch *frame_gdbarch;
11347 struct frame_id stack_frame_id;
11348 struct frame_id caller_frame_id;
11349 struct breakpoint *breakpoint;
11350 struct breakpoint *breakpoint2 = NULL;
11351 struct cleanup *old_chain;
11352 int thread;
11353 struct thread_info *tp;
11354
11355 clear_proceed_status ();
11356
11357 /* Set a breakpoint where the user wants it and at return from
11358 this function. */
11359
11360 if (last_displayed_sal_is_valid ())
11361 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
11362 get_last_displayed_symtab (),
11363 get_last_displayed_line ());
11364 else
11365 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
11366 (struct symtab *) NULL, 0);
11367
11368 if (sals.nelts != 1)
11369 error (_("Couldn't get information on specified line."));
11370
11371 sal = sals.sals[0];
11372 xfree (sals.sals); /* malloc'd, so freed. */
11373
11374 if (*arg)
11375 error (_("Junk at end of arguments."));
11376
11377 resolve_sal_pc (&sal);
11378
11379 tp = inferior_thread ();
11380 thread = tp->num;
11381
11382 old_chain = make_cleanup (null_cleanup, NULL);
11383
11384 /* Note linespec handling above invalidates the frame chain.
11385 Installing a breakpoint also invalidates the frame chain (as it
11386 may need to switch threads), so do any frame handling before
11387 that. */
11388
11389 frame = get_selected_frame (NULL);
11390 frame_gdbarch = get_frame_arch (frame);
11391 stack_frame_id = get_stack_frame_id (frame);
11392 caller_frame_id = frame_unwind_caller_id (frame);
11393
11394 /* Keep within the current frame, or in frames called by the current
11395 one. */
11396
11397 if (frame_id_p (caller_frame_id))
11398 {
11399 struct symtab_and_line sal2;
11400
11401 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11402 sal2.pc = frame_unwind_caller_pc (frame);
11403 breakpoint2 = set_momentary_breakpoint (frame_unwind_caller_arch (frame),
11404 sal2,
11405 caller_frame_id,
11406 bp_until);
11407 make_cleanup_delete_breakpoint (breakpoint2);
11408
11409 set_longjmp_breakpoint (tp, caller_frame_id);
11410 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
11411 }
11412
11413 /* set_momentary_breakpoint could invalidate FRAME. */
11414 frame = NULL;
11415
11416 if (anywhere)
11417 /* If the user told us to continue until a specified location,
11418 we don't specify a frame at which we need to stop. */
11419 breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11420 null_frame_id, bp_until);
11421 else
11422 /* Otherwise, specify the selected frame, because we want to stop
11423 only at the very same frame. */
11424 breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11425 stack_frame_id, bp_until);
11426 make_cleanup_delete_breakpoint (breakpoint);
11427
11428 proceed (-1, GDB_SIGNAL_DEFAULT, 0);
11429
11430 /* If we are running asynchronously, and proceed call above has
11431 actually managed to start the target, arrange for breakpoints to
11432 be deleted when the target stops. Otherwise, we're already
11433 stopped and delete breakpoints via cleanup chain. */
11434
11435 if (target_can_async_p () && is_running (inferior_ptid))
11436 {
11437 struct until_break_command_continuation_args *args;
11438 args = xmalloc (sizeof (*args));
11439
11440 args->breakpoint = breakpoint;
11441 args->breakpoint2 = breakpoint2;
11442 args->thread_num = thread;
11443
11444 discard_cleanups (old_chain);
11445 add_continuation (inferior_thread (),
11446 until_break_command_continuation, args,
11447 xfree);
11448 }
11449 else
11450 do_cleanups (old_chain);
11451 }
11452
11453 /* This function attempts to parse an optional "if <cond>" clause
11454 from the arg string. If one is not found, it returns NULL.
11455
11456 Else, it returns a pointer to the condition string. (It does not
11457 attempt to evaluate the string against a particular block.) And,
11458 it updates arg to point to the first character following the parsed
11459 if clause in the arg string. */
11460
11461 static char *
11462 ep_parse_optional_if_clause (char **arg)
11463 {
11464 char *cond_string;
11465
11466 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
11467 return NULL;
11468
11469 /* Skip the "if" keyword. */
11470 (*arg) += 2;
11471
11472 /* Skip any extra leading whitespace, and record the start of the
11473 condition string. */
11474 *arg = skip_spaces (*arg);
11475 cond_string = *arg;
11476
11477 /* Assume that the condition occupies the remainder of the arg
11478 string. */
11479 (*arg) += strlen (cond_string);
11480
11481 return cond_string;
11482 }
11483
11484 /* Commands to deal with catching events, such as signals, exceptions,
11485 process start/exit, etc. */
11486
11487 typedef enum
11488 {
11489 catch_fork_temporary, catch_vfork_temporary,
11490 catch_fork_permanent, catch_vfork_permanent
11491 }
11492 catch_fork_kind;
11493
11494 static void
11495 catch_fork_command_1 (char *arg, int from_tty,
11496 struct cmd_list_element *command)
11497 {
11498 struct gdbarch *gdbarch = get_current_arch ();
11499 char *cond_string = NULL;
11500 catch_fork_kind fork_kind;
11501 int tempflag;
11502
11503 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11504 tempflag = (fork_kind == catch_fork_temporary
11505 || fork_kind == catch_vfork_temporary);
11506
11507 if (!arg)
11508 arg = "";
11509 arg = skip_spaces (arg);
11510
11511 /* The allowed syntax is:
11512 catch [v]fork
11513 catch [v]fork if <cond>
11514
11515 First, check if there's an if clause. */
11516 cond_string = ep_parse_optional_if_clause (&arg);
11517
11518 if ((*arg != '\0') && !isspace (*arg))
11519 error (_("Junk at end of arguments."));
11520
11521 /* If this target supports it, create a fork or vfork catchpoint
11522 and enable reporting of such events. */
11523 switch (fork_kind)
11524 {
11525 case catch_fork_temporary:
11526 case catch_fork_permanent:
11527 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11528 &catch_fork_breakpoint_ops);
11529 break;
11530 case catch_vfork_temporary:
11531 case catch_vfork_permanent:
11532 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11533 &catch_vfork_breakpoint_ops);
11534 break;
11535 default:
11536 error (_("unsupported or unknown fork kind; cannot catch it"));
11537 break;
11538 }
11539 }
11540
11541 static void
11542 catch_exec_command_1 (char *arg, int from_tty,
11543 struct cmd_list_element *command)
11544 {
11545 struct exec_catchpoint *c;
11546 struct gdbarch *gdbarch = get_current_arch ();
11547 int tempflag;
11548 char *cond_string = NULL;
11549
11550 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11551
11552 if (!arg)
11553 arg = "";
11554 arg = skip_spaces (arg);
11555
11556 /* The allowed syntax is:
11557 catch exec
11558 catch exec if <cond>
11559
11560 First, check if there's an if clause. */
11561 cond_string = ep_parse_optional_if_clause (&arg);
11562
11563 if ((*arg != '\0') && !isspace (*arg))
11564 error (_("Junk at end of arguments."));
11565
11566 c = XNEW (struct exec_catchpoint);
11567 init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
11568 &catch_exec_breakpoint_ops);
11569 c->exec_pathname = NULL;
11570
11571 install_breakpoint (0, &c->base, 1);
11572 }
11573
11574 /* A helper function that returns a value indicating the kind of the
11575 exception catchpoint B. */
11576
11577 static enum exception_event_kind
11578 classify_exception_breakpoint (struct breakpoint *b)
11579 {
11580 if (strstr (b->addr_string, "catch") != NULL)
11581 return EX_EVENT_CATCH;
11582 else if (strstr (b->addr_string, "rethrow") != NULL)
11583 return EX_EVENT_RETHROW;
11584 else
11585 return EX_EVENT_THROW;
11586 }
11587
11588 static enum print_stop_action
11589 print_it_exception_catchpoint (bpstat bs)
11590 {
11591 struct ui_out *uiout = current_uiout;
11592 struct breakpoint *b = bs->breakpoint_at;
11593 int bp_temp;
11594 enum exception_event_kind kind = classify_exception_breakpoint (b);
11595
11596 annotate_catchpoint (b->number);
11597
11598 if (b->loc->address != b->loc->requested_address)
11599 breakpoint_adjustment_warning (b->loc->requested_address,
11600 b->loc->address,
11601 b->number, 1);
11602 bp_temp = b->disposition == disp_del;
11603 ui_out_text (uiout,
11604 bp_temp ? "Temporary catchpoint "
11605 : "Catchpoint ");
11606 if (!ui_out_is_mi_like_p (uiout))
11607 ui_out_field_int (uiout, "bkptno", b->number);
11608 ui_out_text (uiout,
11609 (kind == EX_EVENT_THROW ? " (exception thrown), "
11610 : (kind == EX_EVENT_CATCH ? " (exception caught), "
11611 : " (exception rethrown), ")));
11612 if (ui_out_is_mi_like_p (uiout))
11613 {
11614 ui_out_field_string (uiout, "reason",
11615 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
11616 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
11617 ui_out_field_int (uiout, "bkptno", b->number);
11618 }
11619 return PRINT_SRC_AND_LOC;
11620 }
11621
11622 static void
11623 print_one_exception_catchpoint (struct breakpoint *b,
11624 struct bp_location **last_loc)
11625 {
11626 struct value_print_options opts;
11627 struct ui_out *uiout = current_uiout;
11628 enum exception_event_kind kind = classify_exception_breakpoint (b);
11629
11630 get_user_print_options (&opts);
11631 if (opts.addressprint)
11632 {
11633 annotate_field (4);
11634 if (b->loc == NULL || b->loc->shlib_disabled)
11635 ui_out_field_string (uiout, "addr", "<PENDING>");
11636 else
11637 ui_out_field_core_addr (uiout, "addr",
11638 b->loc->gdbarch, b->loc->address);
11639 }
11640 annotate_field (5);
11641 if (b->loc)
11642 *last_loc = b->loc;
11643
11644 switch (kind)
11645 {
11646 case EX_EVENT_THROW:
11647 ui_out_field_string (uiout, "what", "exception throw");
11648 if (ui_out_is_mi_like_p (uiout))
11649 ui_out_field_string (uiout, "catch-type", "throw");
11650 break;
11651
11652 case EX_EVENT_RETHROW:
11653 ui_out_field_string (uiout, "what", "exception rethrow");
11654 if (ui_out_is_mi_like_p (uiout))
11655 ui_out_field_string (uiout, "catch-type", "rethrow");
11656 break;
11657
11658 case EX_EVENT_CATCH:
11659 ui_out_field_string (uiout, "what", "exception catch");
11660 if (ui_out_is_mi_like_p (uiout))
11661 ui_out_field_string (uiout, "catch-type", "catch");
11662 break;
11663 }
11664 }
11665
11666 static void
11667 print_mention_exception_catchpoint (struct breakpoint *b)
11668 {
11669 struct ui_out *uiout = current_uiout;
11670 int bp_temp;
11671 enum exception_event_kind kind = classify_exception_breakpoint (b);
11672
11673 bp_temp = b->disposition == disp_del;
11674 ui_out_text (uiout, bp_temp ? _("Temporary catchpoint ")
11675 : _("Catchpoint "));
11676 ui_out_field_int (uiout, "bkptno", b->number);
11677 ui_out_text (uiout, (kind == EX_EVENT_THROW ? _(" (throw)")
11678 : (kind == EX_EVENT_CATCH ? _(" (catch)")
11679 : _(" (rethrow)"))));
11680 }
11681
11682 /* Implement the "print_recreate" breakpoint_ops method for throw and
11683 catch catchpoints. */
11684
11685 static void
11686 print_recreate_exception_catchpoint (struct breakpoint *b,
11687 struct ui_file *fp)
11688 {
11689 int bp_temp;
11690 enum exception_event_kind kind = classify_exception_breakpoint (b);
11691
11692 bp_temp = b->disposition == disp_del;
11693 fprintf_unfiltered (fp, bp_temp ? "tcatch " : "catch ");
11694 switch (kind)
11695 {
11696 case EX_EVENT_THROW:
11697 fprintf_unfiltered (fp, "throw");
11698 break;
11699 case EX_EVENT_CATCH:
11700 fprintf_unfiltered (fp, "catch");
11701 break;
11702 case EX_EVENT_RETHROW:
11703 fprintf_unfiltered (fp, "rethrow");
11704 break;
11705 }
11706 print_recreate_thread (b, fp);
11707 }
11708
11709 static struct breakpoint_ops gnu_v3_exception_catchpoint_ops;
11710
11711 static int
11712 handle_gnu_v3_exceptions (int tempflag, char *cond_string,
11713 enum exception_event_kind ex_event, int from_tty)
11714 {
11715 char *trigger_func_name;
11716
11717 if (ex_event == EX_EVENT_CATCH)
11718 trigger_func_name = "__cxa_begin_catch";
11719 else if (ex_event == EX_EVENT_RETHROW)
11720 trigger_func_name = "__cxa_rethrow";
11721 else
11722 {
11723 gdb_assert (ex_event == EX_EVENT_THROW);
11724 trigger_func_name = "__cxa_throw";
11725 }
11726
11727 create_breakpoint (get_current_arch (),
11728 trigger_func_name, cond_string, -1, NULL,
11729 0 /* condition and thread are valid. */,
11730 tempflag, bp_breakpoint,
11731 0,
11732 AUTO_BOOLEAN_TRUE /* pending */,
11733 &gnu_v3_exception_catchpoint_ops, from_tty,
11734 1 /* enabled */,
11735 0 /* internal */,
11736 0);
11737
11738 return 1;
11739 }
11740
11741 /* Deal with "catch catch" and "catch throw" commands. */
11742
11743 static void
11744 catch_exception_command_1 (enum exception_event_kind ex_event, char *arg,
11745 int tempflag, int from_tty)
11746 {
11747 char *cond_string = NULL;
11748
11749 if (!arg)
11750 arg = "";
11751 arg = skip_spaces (arg);
11752
11753 cond_string = ep_parse_optional_if_clause (&arg);
11754
11755 if ((*arg != '\0') && !isspace (*arg))
11756 error (_("Junk at end of arguments."));
11757
11758 if (ex_event != EX_EVENT_THROW
11759 && ex_event != EX_EVENT_CATCH
11760 && ex_event != EX_EVENT_RETHROW)
11761 error (_("Unsupported or unknown exception event; cannot catch it"));
11762
11763 if (handle_gnu_v3_exceptions (tempflag, cond_string, ex_event, from_tty))
11764 return;
11765
11766 warning (_("Unsupported with this platform/compiler combination."));
11767 }
11768
11769 /* Implementation of "catch catch" command. */
11770
11771 static void
11772 catch_catch_command (char *arg, int from_tty, struct cmd_list_element *command)
11773 {
11774 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11775
11776 catch_exception_command_1 (EX_EVENT_CATCH, arg, tempflag, from_tty);
11777 }
11778
11779 /* Implementation of "catch throw" command. */
11780
11781 static void
11782 catch_throw_command (char *arg, int from_tty, struct cmd_list_element *command)
11783 {
11784 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11785
11786 catch_exception_command_1 (EX_EVENT_THROW, arg, tempflag, from_tty);
11787 }
11788
11789 /* Implementation of "catch rethrow" command. */
11790
11791 static void
11792 catch_rethrow_command (char *arg, int from_tty,
11793 struct cmd_list_element *command)
11794 {
11795 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11796
11797 catch_exception_command_1 (EX_EVENT_RETHROW, arg, tempflag, from_tty);
11798 }
11799
11800 void
11801 init_ada_exception_breakpoint (struct breakpoint *b,
11802 struct gdbarch *gdbarch,
11803 struct symtab_and_line sal,
11804 char *addr_string,
11805 const struct breakpoint_ops *ops,
11806 int tempflag,
11807 int from_tty)
11808 {
11809 if (from_tty)
11810 {
11811 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11812 if (!loc_gdbarch)
11813 loc_gdbarch = gdbarch;
11814
11815 describe_other_breakpoints (loc_gdbarch,
11816 sal.pspace, sal.pc, sal.section, -1);
11817 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11818 version for exception catchpoints, because two catchpoints
11819 used for different exception names will use the same address.
11820 In this case, a "breakpoint ... also set at..." warning is
11821 unproductive. Besides, the warning phrasing is also a bit
11822 inappropriate, we should use the word catchpoint, and tell
11823 the user what type of catchpoint it is. The above is good
11824 enough for now, though. */
11825 }
11826
11827 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
11828
11829 b->enable_state = bp_enabled;
11830 b->disposition = tempflag ? disp_del : disp_donttouch;
11831 b->addr_string = addr_string;
11832 b->language = language_ada;
11833 }
11834
11835 /* Splits the argument using space as delimiter. Returns an xmalloc'd
11836 filter list, or NULL if no filtering is required. */
11837 static VEC(int) *
11838 catch_syscall_split_args (char *arg)
11839 {
11840 VEC(int) *result = NULL;
11841 struct cleanup *cleanup = make_cleanup (VEC_cleanup (int), &result);
11842
11843 while (*arg != '\0')
11844 {
11845 int i, syscall_number;
11846 char *endptr;
11847 char cur_name[128];
11848 struct syscall s;
11849
11850 /* Skip whitespace. */
11851 arg = skip_spaces (arg);
11852
11853 for (i = 0; i < 127 && arg[i] && !isspace (arg[i]); ++i)
11854 cur_name[i] = arg[i];
11855 cur_name[i] = '\0';
11856 arg += i;
11857
11858 /* Check if the user provided a syscall name or a number. */
11859 syscall_number = (int) strtol (cur_name, &endptr, 0);
11860 if (*endptr == '\0')
11861 get_syscall_by_number (syscall_number, &s);
11862 else
11863 {
11864 /* We have a name. Let's check if it's valid and convert it
11865 to a number. */
11866 get_syscall_by_name (cur_name, &s);
11867
11868 if (s.number == UNKNOWN_SYSCALL)
11869 /* Here we have to issue an error instead of a warning,
11870 because GDB cannot do anything useful if there's no
11871 syscall number to be caught. */
11872 error (_("Unknown syscall name '%s'."), cur_name);
11873 }
11874
11875 /* Ok, it's valid. */
11876 VEC_safe_push (int, result, s.number);
11877 }
11878
11879 discard_cleanups (cleanup);
11880 return result;
11881 }
11882
11883 /* Implement the "catch syscall" command. */
11884
11885 static void
11886 catch_syscall_command_1 (char *arg, int from_tty,
11887 struct cmd_list_element *command)
11888 {
11889 int tempflag;
11890 VEC(int) *filter;
11891 struct syscall s;
11892 struct gdbarch *gdbarch = get_current_arch ();
11893
11894 /* Checking if the feature if supported. */
11895 if (gdbarch_get_syscall_number_p (gdbarch) == 0)
11896 error (_("The feature 'catch syscall' is not supported on \
11897 this architecture yet."));
11898
11899 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11900
11901 arg = skip_spaces (arg);
11902
11903 /* We need to do this first "dummy" translation in order
11904 to get the syscall XML file loaded or, most important,
11905 to display a warning to the user if there's no XML file
11906 for his/her architecture. */
11907 get_syscall_by_number (0, &s);
11908
11909 /* The allowed syntax is:
11910 catch syscall
11911 catch syscall <name | number> [<name | number> ... <name | number>]
11912
11913 Let's check if there's a syscall name. */
11914
11915 if (arg != NULL)
11916 filter = catch_syscall_split_args (arg);
11917 else
11918 filter = NULL;
11919
11920 create_syscall_event_catchpoint (tempflag, filter,
11921 &catch_syscall_breakpoint_ops);
11922 }
11923
11924 static void
11925 catch_command (char *arg, int from_tty)
11926 {
11927 error (_("Catch requires an event name."));
11928 }
11929 \f
11930
11931 static void
11932 tcatch_command (char *arg, int from_tty)
11933 {
11934 error (_("Catch requires an event name."));
11935 }
11936
11937 /* A qsort comparison function that sorts breakpoints in order. */
11938
11939 static int
11940 compare_breakpoints (const void *a, const void *b)
11941 {
11942 const breakpoint_p *ba = a;
11943 uintptr_t ua = (uintptr_t) *ba;
11944 const breakpoint_p *bb = b;
11945 uintptr_t ub = (uintptr_t) *bb;
11946
11947 if ((*ba)->number < (*bb)->number)
11948 return -1;
11949 else if ((*ba)->number > (*bb)->number)
11950 return 1;
11951
11952 /* Now sort by address, in case we see, e..g, two breakpoints with
11953 the number 0. */
11954 if (ua < ub)
11955 return -1;
11956 return ua > ub ? 1 : 0;
11957 }
11958
11959 /* Delete breakpoints by address or line. */
11960
11961 static void
11962 clear_command (char *arg, int from_tty)
11963 {
11964 struct breakpoint *b, *prev;
11965 VEC(breakpoint_p) *found = 0;
11966 int ix;
11967 int default_match;
11968 struct symtabs_and_lines sals;
11969 struct symtab_and_line sal;
11970 int i;
11971 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
11972
11973 if (arg)
11974 {
11975 sals = decode_line_with_current_source (arg,
11976 (DECODE_LINE_FUNFIRSTLINE
11977 | DECODE_LINE_LIST_MODE));
11978 make_cleanup (xfree, sals.sals);
11979 default_match = 0;
11980 }
11981 else
11982 {
11983 sals.sals = (struct symtab_and_line *)
11984 xmalloc (sizeof (struct symtab_and_line));
11985 make_cleanup (xfree, sals.sals);
11986 init_sal (&sal); /* Initialize to zeroes. */
11987
11988 /* Set sal's line, symtab, pc, and pspace to the values
11989 corresponding to the last call to print_frame_info. If the
11990 codepoint is not valid, this will set all the fields to 0. */
11991 get_last_displayed_sal (&sal);
11992 if (sal.symtab == 0)
11993 error (_("No source file specified."));
11994
11995 sals.sals[0] = sal;
11996 sals.nelts = 1;
11997
11998 default_match = 1;
11999 }
12000
12001 /* We don't call resolve_sal_pc here. That's not as bad as it
12002 seems, because all existing breakpoints typically have both
12003 file/line and pc set. So, if clear is given file/line, we can
12004 match this to existing breakpoint without obtaining pc at all.
12005
12006 We only support clearing given the address explicitly
12007 present in breakpoint table. Say, we've set breakpoint
12008 at file:line. There were several PC values for that file:line,
12009 due to optimization, all in one block.
12010
12011 We've picked one PC value. If "clear" is issued with another
12012 PC corresponding to the same file:line, the breakpoint won't
12013 be cleared. We probably can still clear the breakpoint, but
12014 since the other PC value is never presented to user, user
12015 can only find it by guessing, and it does not seem important
12016 to support that. */
12017
12018 /* For each line spec given, delete bps which correspond to it. Do
12019 it in two passes, solely to preserve the current behavior that
12020 from_tty is forced true if we delete more than one
12021 breakpoint. */
12022
12023 found = NULL;
12024 make_cleanup (VEC_cleanup (breakpoint_p), &found);
12025 for (i = 0; i < sals.nelts; i++)
12026 {
12027 const char *sal_fullname;
12028
12029 /* If exact pc given, clear bpts at that pc.
12030 If line given (pc == 0), clear all bpts on specified line.
12031 If defaulting, clear all bpts on default line
12032 or at default pc.
12033
12034 defaulting sal.pc != 0 tests to do
12035
12036 0 1 pc
12037 1 1 pc _and_ line
12038 0 0 line
12039 1 0 <can't happen> */
12040
12041 sal = sals.sals[i];
12042 sal_fullname = (sal.symtab == NULL
12043 ? NULL : symtab_to_fullname (sal.symtab));
12044
12045 /* Find all matching breakpoints and add them to 'found'. */
12046 ALL_BREAKPOINTS (b)
12047 {
12048 int match = 0;
12049 /* Are we going to delete b? */
12050 if (b->type != bp_none && !is_watchpoint (b))
12051 {
12052 struct bp_location *loc = b->loc;
12053 for (; loc; loc = loc->next)
12054 {
12055 /* If the user specified file:line, don't allow a PC
12056 match. This matches historical gdb behavior. */
12057 int pc_match = (!sal.explicit_line
12058 && sal.pc
12059 && (loc->pspace == sal.pspace)
12060 && (loc->address == sal.pc)
12061 && (!section_is_overlay (loc->section)
12062 || loc->section == sal.section));
12063 int line_match = 0;
12064
12065 if ((default_match || sal.explicit_line)
12066 && loc->symtab != NULL
12067 && sal_fullname != NULL
12068 && sal.pspace == loc->pspace
12069 && loc->line_number == sal.line
12070 && filename_cmp (symtab_to_fullname (loc->symtab),
12071 sal_fullname) == 0)
12072 line_match = 1;
12073
12074 if (pc_match || line_match)
12075 {
12076 match = 1;
12077 break;
12078 }
12079 }
12080 }
12081
12082 if (match)
12083 VEC_safe_push(breakpoint_p, found, b);
12084 }
12085 }
12086
12087 /* Now go thru the 'found' chain and delete them. */
12088 if (VEC_empty(breakpoint_p, found))
12089 {
12090 if (arg)
12091 error (_("No breakpoint at %s."), arg);
12092 else
12093 error (_("No breakpoint at this line."));
12094 }
12095
12096 /* Remove duplicates from the vec. */
12097 qsort (VEC_address (breakpoint_p, found),
12098 VEC_length (breakpoint_p, found),
12099 sizeof (breakpoint_p),
12100 compare_breakpoints);
12101 prev = VEC_index (breakpoint_p, found, 0);
12102 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
12103 {
12104 if (b == prev)
12105 {
12106 VEC_ordered_remove (breakpoint_p, found, ix);
12107 --ix;
12108 }
12109 }
12110
12111 if (VEC_length(breakpoint_p, found) > 1)
12112 from_tty = 1; /* Always report if deleted more than one. */
12113 if (from_tty)
12114 {
12115 if (VEC_length(breakpoint_p, found) == 1)
12116 printf_unfiltered (_("Deleted breakpoint "));
12117 else
12118 printf_unfiltered (_("Deleted breakpoints "));
12119 }
12120
12121 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
12122 {
12123 if (from_tty)
12124 printf_unfiltered ("%d ", b->number);
12125 delete_breakpoint (b);
12126 }
12127 if (from_tty)
12128 putchar_unfiltered ('\n');
12129
12130 do_cleanups (cleanups);
12131 }
12132 \f
12133 /* Delete breakpoint in BS if they are `delete' breakpoints and
12134 all breakpoints that are marked for deletion, whether hit or not.
12135 This is called after any breakpoint is hit, or after errors. */
12136
12137 void
12138 breakpoint_auto_delete (bpstat bs)
12139 {
12140 struct breakpoint *b, *b_tmp;
12141
12142 for (; bs; bs = bs->next)
12143 if (bs->breakpoint_at
12144 && bs->breakpoint_at->disposition == disp_del
12145 && bs->stop)
12146 delete_breakpoint (bs->breakpoint_at);
12147
12148 ALL_BREAKPOINTS_SAFE (b, b_tmp)
12149 {
12150 if (b->disposition == disp_del_at_next_stop)
12151 delete_breakpoint (b);
12152 }
12153 }
12154
12155 /* A comparison function for bp_location AP and BP being interfaced to
12156 qsort. Sort elements primarily by their ADDRESS (no matter what
12157 does breakpoint_address_is_meaningful say for its OWNER),
12158 secondarily by ordering first bp_permanent OWNERed elements and
12159 terciarily just ensuring the array is sorted stable way despite
12160 qsort being an unstable algorithm. */
12161
12162 static int
12163 bp_location_compare (const void *ap, const void *bp)
12164 {
12165 struct bp_location *a = *(void **) ap;
12166 struct bp_location *b = *(void **) bp;
12167 /* A and B come from existing breakpoints having non-NULL OWNER. */
12168 int a_perm = a->owner->enable_state == bp_permanent;
12169 int b_perm = b->owner->enable_state == bp_permanent;
12170
12171 if (a->address != b->address)
12172 return (a->address > b->address) - (a->address < b->address);
12173
12174 /* Sort locations at the same address by their pspace number, keeping
12175 locations of the same inferior (in a multi-inferior environment)
12176 grouped. */
12177
12178 if (a->pspace->num != b->pspace->num)
12179 return ((a->pspace->num > b->pspace->num)
12180 - (a->pspace->num < b->pspace->num));
12181
12182 /* Sort permanent breakpoints first. */
12183 if (a_perm != b_perm)
12184 return (a_perm < b_perm) - (a_perm > b_perm);
12185
12186 /* Make the internal GDB representation stable across GDB runs
12187 where A and B memory inside GDB can differ. Breakpoint locations of
12188 the same type at the same address can be sorted in arbitrary order. */
12189
12190 if (a->owner->number != b->owner->number)
12191 return ((a->owner->number > b->owner->number)
12192 - (a->owner->number < b->owner->number));
12193
12194 return (a > b) - (a < b);
12195 }
12196
12197 /* Set bp_location_placed_address_before_address_max and
12198 bp_location_shadow_len_after_address_max according to the current
12199 content of the bp_location array. */
12200
12201 static void
12202 bp_location_target_extensions_update (void)
12203 {
12204 struct bp_location *bl, **blp_tmp;
12205
12206 bp_location_placed_address_before_address_max = 0;
12207 bp_location_shadow_len_after_address_max = 0;
12208
12209 ALL_BP_LOCATIONS (bl, blp_tmp)
12210 {
12211 CORE_ADDR start, end, addr;
12212
12213 if (!bp_location_has_shadow (bl))
12214 continue;
12215
12216 start = bl->target_info.placed_address;
12217 end = start + bl->target_info.shadow_len;
12218
12219 gdb_assert (bl->address >= start);
12220 addr = bl->address - start;
12221 if (addr > bp_location_placed_address_before_address_max)
12222 bp_location_placed_address_before_address_max = addr;
12223
12224 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
12225
12226 gdb_assert (bl->address < end);
12227 addr = end - bl->address;
12228 if (addr > bp_location_shadow_len_after_address_max)
12229 bp_location_shadow_len_after_address_max = addr;
12230 }
12231 }
12232
12233 /* Download tracepoint locations if they haven't been. */
12234
12235 static void
12236 download_tracepoint_locations (void)
12237 {
12238 struct breakpoint *b;
12239 struct cleanup *old_chain;
12240
12241 if (!target_can_download_tracepoint ())
12242 return;
12243
12244 old_chain = save_current_space_and_thread ();
12245
12246 ALL_TRACEPOINTS (b)
12247 {
12248 struct bp_location *bl;
12249 struct tracepoint *t;
12250 int bp_location_downloaded = 0;
12251
12252 if ((b->type == bp_fast_tracepoint
12253 ? !may_insert_fast_tracepoints
12254 : !may_insert_tracepoints))
12255 continue;
12256
12257 for (bl = b->loc; bl; bl = bl->next)
12258 {
12259 /* In tracepoint, locations are _never_ duplicated, so
12260 should_be_inserted is equivalent to
12261 unduplicated_should_be_inserted. */
12262 if (!should_be_inserted (bl) || bl->inserted)
12263 continue;
12264
12265 switch_to_program_space_and_thread (bl->pspace);
12266
12267 target_download_tracepoint (bl);
12268
12269 bl->inserted = 1;
12270 bp_location_downloaded = 1;
12271 }
12272 t = (struct tracepoint *) b;
12273 t->number_on_target = b->number;
12274 if (bp_location_downloaded)
12275 observer_notify_breakpoint_modified (b);
12276 }
12277
12278 do_cleanups (old_chain);
12279 }
12280
12281 /* Swap the insertion/duplication state between two locations. */
12282
12283 static void
12284 swap_insertion (struct bp_location *left, struct bp_location *right)
12285 {
12286 const int left_inserted = left->inserted;
12287 const int left_duplicate = left->duplicate;
12288 const int left_needs_update = left->needs_update;
12289 const struct bp_target_info left_target_info = left->target_info;
12290
12291 /* Locations of tracepoints can never be duplicated. */
12292 if (is_tracepoint (left->owner))
12293 gdb_assert (!left->duplicate);
12294 if (is_tracepoint (right->owner))
12295 gdb_assert (!right->duplicate);
12296
12297 left->inserted = right->inserted;
12298 left->duplicate = right->duplicate;
12299 left->needs_update = right->needs_update;
12300 left->target_info = right->target_info;
12301 right->inserted = left_inserted;
12302 right->duplicate = left_duplicate;
12303 right->needs_update = left_needs_update;
12304 right->target_info = left_target_info;
12305 }
12306
12307 /* Force the re-insertion of the locations at ADDRESS. This is called
12308 once a new/deleted/modified duplicate location is found and we are evaluating
12309 conditions on the target's side. Such conditions need to be updated on
12310 the target. */
12311
12312 static void
12313 force_breakpoint_reinsertion (struct bp_location *bl)
12314 {
12315 struct bp_location **locp = NULL, **loc2p;
12316 struct bp_location *loc;
12317 CORE_ADDR address = 0;
12318 int pspace_num;
12319
12320 address = bl->address;
12321 pspace_num = bl->pspace->num;
12322
12323 /* This is only meaningful if the target is
12324 evaluating conditions and if the user has
12325 opted for condition evaluation on the target's
12326 side. */
12327 if (gdb_evaluates_breakpoint_condition_p ()
12328 || !target_supports_evaluation_of_breakpoint_conditions ())
12329 return;
12330
12331 /* Flag all breakpoint locations with this address and
12332 the same program space as the location
12333 as "its condition has changed". We need to
12334 update the conditions on the target's side. */
12335 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
12336 {
12337 loc = *loc2p;
12338
12339 if (!is_breakpoint (loc->owner)
12340 || pspace_num != loc->pspace->num)
12341 continue;
12342
12343 /* Flag the location appropriately. We use a different state to
12344 let everyone know that we already updated the set of locations
12345 with addr bl->address and program space bl->pspace. This is so
12346 we don't have to keep calling these functions just to mark locations
12347 that have already been marked. */
12348 loc->condition_changed = condition_updated;
12349
12350 /* Free the agent expression bytecode as well. We will compute
12351 it later on. */
12352 if (loc->cond_bytecode)
12353 {
12354 free_agent_expr (loc->cond_bytecode);
12355 loc->cond_bytecode = NULL;
12356 }
12357 }
12358 }
12359
12360 /* If SHOULD_INSERT is false, do not insert any breakpoint locations
12361 into the inferior, only remove already-inserted locations that no
12362 longer should be inserted. Functions that delete a breakpoint or
12363 breakpoints should pass false, so that deleting a breakpoint
12364 doesn't have the side effect of inserting the locations of other
12365 breakpoints that are marked not-inserted, but should_be_inserted
12366 returns true on them.
12367
12368 This behaviour is useful is situations close to tear-down -- e.g.,
12369 after an exec, while the target still has execution, but breakpoint
12370 shadows of the previous executable image should *NOT* be restored
12371 to the new image; or before detaching, where the target still has
12372 execution and wants to delete breakpoints from GDB's lists, and all
12373 breakpoints had already been removed from the inferior. */
12374
12375 static void
12376 update_global_location_list (int should_insert)
12377 {
12378 struct breakpoint *b;
12379 struct bp_location **locp, *loc;
12380 struct cleanup *cleanups;
12381 /* Last breakpoint location address that was marked for update. */
12382 CORE_ADDR last_addr = 0;
12383 /* Last breakpoint location program space that was marked for update. */
12384 int last_pspace_num = -1;
12385
12386 /* Used in the duplicates detection below. When iterating over all
12387 bp_locations, points to the first bp_location of a given address.
12388 Breakpoints and watchpoints of different types are never
12389 duplicates of each other. Keep one pointer for each type of
12390 breakpoint/watchpoint, so we only need to loop over all locations
12391 once. */
12392 struct bp_location *bp_loc_first; /* breakpoint */
12393 struct bp_location *wp_loc_first; /* hardware watchpoint */
12394 struct bp_location *awp_loc_first; /* access watchpoint */
12395 struct bp_location *rwp_loc_first; /* read watchpoint */
12396
12397 /* Saved former bp_location array which we compare against the newly
12398 built bp_location from the current state of ALL_BREAKPOINTS. */
12399 struct bp_location **old_location, **old_locp;
12400 unsigned old_location_count;
12401
12402 old_location = bp_location;
12403 old_location_count = bp_location_count;
12404 bp_location = NULL;
12405 bp_location_count = 0;
12406 cleanups = make_cleanup (xfree, old_location);
12407
12408 ALL_BREAKPOINTS (b)
12409 for (loc = b->loc; loc; loc = loc->next)
12410 bp_location_count++;
12411
12412 bp_location = xmalloc (sizeof (*bp_location) * bp_location_count);
12413 locp = bp_location;
12414 ALL_BREAKPOINTS (b)
12415 for (loc = b->loc; loc; loc = loc->next)
12416 *locp++ = loc;
12417 qsort (bp_location, bp_location_count, sizeof (*bp_location),
12418 bp_location_compare);
12419
12420 bp_location_target_extensions_update ();
12421
12422 /* Identify bp_location instances that are no longer present in the
12423 new list, and therefore should be freed. Note that it's not
12424 necessary that those locations should be removed from inferior --
12425 if there's another location at the same address (previously
12426 marked as duplicate), we don't need to remove/insert the
12427 location.
12428
12429 LOCP is kept in sync with OLD_LOCP, each pointing to the current
12430 and former bp_location array state respectively. */
12431
12432 locp = bp_location;
12433 for (old_locp = old_location; old_locp < old_location + old_location_count;
12434 old_locp++)
12435 {
12436 struct bp_location *old_loc = *old_locp;
12437 struct bp_location **loc2p;
12438
12439 /* Tells if 'old_loc' is found among the new locations. If
12440 not, we have to free it. */
12441 int found_object = 0;
12442 /* Tells if the location should remain inserted in the target. */
12443 int keep_in_target = 0;
12444 int removed = 0;
12445
12446 /* Skip LOCP entries which will definitely never be needed.
12447 Stop either at or being the one matching OLD_LOC. */
12448 while (locp < bp_location + bp_location_count
12449 && (*locp)->address < old_loc->address)
12450 locp++;
12451
12452 for (loc2p = locp;
12453 (loc2p < bp_location + bp_location_count
12454 && (*loc2p)->address == old_loc->address);
12455 loc2p++)
12456 {
12457 /* Check if this is a new/duplicated location or a duplicated
12458 location that had its condition modified. If so, we want to send
12459 its condition to the target if evaluation of conditions is taking
12460 place there. */
12461 if ((*loc2p)->condition_changed == condition_modified
12462 && (last_addr != old_loc->address
12463 || last_pspace_num != old_loc->pspace->num))
12464 {
12465 force_breakpoint_reinsertion (*loc2p);
12466 last_pspace_num = old_loc->pspace->num;
12467 }
12468
12469 if (*loc2p == old_loc)
12470 found_object = 1;
12471 }
12472
12473 /* We have already handled this address, update it so that we don't
12474 have to go through updates again. */
12475 last_addr = old_loc->address;
12476
12477 /* Target-side condition evaluation: Handle deleted locations. */
12478 if (!found_object)
12479 force_breakpoint_reinsertion (old_loc);
12480
12481 /* If this location is no longer present, and inserted, look if
12482 there's maybe a new location at the same address. If so,
12483 mark that one inserted, and don't remove this one. This is
12484 needed so that we don't have a time window where a breakpoint
12485 at certain location is not inserted. */
12486
12487 if (old_loc->inserted)
12488 {
12489 /* If the location is inserted now, we might have to remove
12490 it. */
12491
12492 if (found_object && should_be_inserted (old_loc))
12493 {
12494 /* The location is still present in the location list,
12495 and still should be inserted. Don't do anything. */
12496 keep_in_target = 1;
12497 }
12498 else
12499 {
12500 /* This location still exists, but it won't be kept in the
12501 target since it may have been disabled. We proceed to
12502 remove its target-side condition. */
12503
12504 /* The location is either no longer present, or got
12505 disabled. See if there's another location at the
12506 same address, in which case we don't need to remove
12507 this one from the target. */
12508
12509 /* OLD_LOC comes from existing struct breakpoint. */
12510 if (breakpoint_address_is_meaningful (old_loc->owner))
12511 {
12512 for (loc2p = locp;
12513 (loc2p < bp_location + bp_location_count
12514 && (*loc2p)->address == old_loc->address);
12515 loc2p++)
12516 {
12517 struct bp_location *loc2 = *loc2p;
12518
12519 if (breakpoint_locations_match (loc2, old_loc))
12520 {
12521 /* Read watchpoint locations are switched to
12522 access watchpoints, if the former are not
12523 supported, but the latter are. */
12524 if (is_hardware_watchpoint (old_loc->owner))
12525 {
12526 gdb_assert (is_hardware_watchpoint (loc2->owner));
12527 loc2->watchpoint_type = old_loc->watchpoint_type;
12528 }
12529
12530 /* loc2 is a duplicated location. We need to check
12531 if it should be inserted in case it will be
12532 unduplicated. */
12533 if (loc2 != old_loc
12534 && unduplicated_should_be_inserted (loc2))
12535 {
12536 swap_insertion (old_loc, loc2);
12537 keep_in_target = 1;
12538 break;
12539 }
12540 }
12541 }
12542 }
12543 }
12544
12545 if (!keep_in_target)
12546 {
12547 if (remove_breakpoint (old_loc, mark_uninserted))
12548 {
12549 /* This is just about all we can do. We could keep
12550 this location on the global list, and try to
12551 remove it next time, but there's no particular
12552 reason why we will succeed next time.
12553
12554 Note that at this point, old_loc->owner is still
12555 valid, as delete_breakpoint frees the breakpoint
12556 only after calling us. */
12557 printf_filtered (_("warning: Error removing "
12558 "breakpoint %d\n"),
12559 old_loc->owner->number);
12560 }
12561 removed = 1;
12562 }
12563 }
12564
12565 if (!found_object)
12566 {
12567 if (removed && non_stop
12568 && breakpoint_address_is_meaningful (old_loc->owner)
12569 && !is_hardware_watchpoint (old_loc->owner))
12570 {
12571 /* This location was removed from the target. In
12572 non-stop mode, a race condition is possible where
12573 we've removed a breakpoint, but stop events for that
12574 breakpoint are already queued and will arrive later.
12575 We apply an heuristic to be able to distinguish such
12576 SIGTRAPs from other random SIGTRAPs: we keep this
12577 breakpoint location for a bit, and will retire it
12578 after we see some number of events. The theory here
12579 is that reporting of events should, "on the average",
12580 be fair, so after a while we'll see events from all
12581 threads that have anything of interest, and no longer
12582 need to keep this breakpoint location around. We
12583 don't hold locations forever so to reduce chances of
12584 mistaking a non-breakpoint SIGTRAP for a breakpoint
12585 SIGTRAP.
12586
12587 The heuristic failing can be disastrous on
12588 decr_pc_after_break targets.
12589
12590 On decr_pc_after_break targets, like e.g., x86-linux,
12591 if we fail to recognize a late breakpoint SIGTRAP,
12592 because events_till_retirement has reached 0 too
12593 soon, we'll fail to do the PC adjustment, and report
12594 a random SIGTRAP to the user. When the user resumes
12595 the inferior, it will most likely immediately crash
12596 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
12597 corrupted, because of being resumed e.g., in the
12598 middle of a multi-byte instruction, or skipped a
12599 one-byte instruction. This was actually seen happen
12600 on native x86-linux, and should be less rare on
12601 targets that do not support new thread events, like
12602 remote, due to the heuristic depending on
12603 thread_count.
12604
12605 Mistaking a random SIGTRAP for a breakpoint trap
12606 causes similar symptoms (PC adjustment applied when
12607 it shouldn't), but then again, playing with SIGTRAPs
12608 behind the debugger's back is asking for trouble.
12609
12610 Since hardware watchpoint traps are always
12611 distinguishable from other traps, so we don't need to
12612 apply keep hardware watchpoint moribund locations
12613 around. We simply always ignore hardware watchpoint
12614 traps we can no longer explain. */
12615
12616 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12617 old_loc->owner = NULL;
12618
12619 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
12620 }
12621 else
12622 {
12623 old_loc->owner = NULL;
12624 decref_bp_location (&old_loc);
12625 }
12626 }
12627 }
12628
12629 /* Rescan breakpoints at the same address and section, marking the
12630 first one as "first" and any others as "duplicates". This is so
12631 that the bpt instruction is only inserted once. If we have a
12632 permanent breakpoint at the same place as BPT, make that one the
12633 official one, and the rest as duplicates. Permanent breakpoints
12634 are sorted first for the same address.
12635
12636 Do the same for hardware watchpoints, but also considering the
12637 watchpoint's type (regular/access/read) and length. */
12638
12639 bp_loc_first = NULL;
12640 wp_loc_first = NULL;
12641 awp_loc_first = NULL;
12642 rwp_loc_first = NULL;
12643 ALL_BP_LOCATIONS (loc, locp)
12644 {
12645 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12646 non-NULL. */
12647 struct bp_location **loc_first_p;
12648 b = loc->owner;
12649
12650 if (!unduplicated_should_be_inserted (loc)
12651 || !breakpoint_address_is_meaningful (b)
12652 /* Don't detect duplicate for tracepoint locations because they are
12653 never duplicated. See the comments in field `duplicate' of
12654 `struct bp_location'. */
12655 || is_tracepoint (b))
12656 {
12657 /* Clear the condition modification flag. */
12658 loc->condition_changed = condition_unchanged;
12659 continue;
12660 }
12661
12662 /* Permanent breakpoint should always be inserted. */
12663 if (b->enable_state == bp_permanent && ! loc->inserted)
12664 internal_error (__FILE__, __LINE__,
12665 _("allegedly permanent breakpoint is not "
12666 "actually inserted"));
12667
12668 if (b->type == bp_hardware_watchpoint)
12669 loc_first_p = &wp_loc_first;
12670 else if (b->type == bp_read_watchpoint)
12671 loc_first_p = &rwp_loc_first;
12672 else if (b->type == bp_access_watchpoint)
12673 loc_first_p = &awp_loc_first;
12674 else
12675 loc_first_p = &bp_loc_first;
12676
12677 if (*loc_first_p == NULL
12678 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12679 || !breakpoint_locations_match (loc, *loc_first_p))
12680 {
12681 *loc_first_p = loc;
12682 loc->duplicate = 0;
12683
12684 if (is_breakpoint (loc->owner) && loc->condition_changed)
12685 {
12686 loc->needs_update = 1;
12687 /* Clear the condition modification flag. */
12688 loc->condition_changed = condition_unchanged;
12689 }
12690 continue;
12691 }
12692
12693
12694 /* This and the above ensure the invariant that the first location
12695 is not duplicated, and is the inserted one.
12696 All following are marked as duplicated, and are not inserted. */
12697 if (loc->inserted)
12698 swap_insertion (loc, *loc_first_p);
12699 loc->duplicate = 1;
12700
12701 /* Clear the condition modification flag. */
12702 loc->condition_changed = condition_unchanged;
12703
12704 if ((*loc_first_p)->owner->enable_state == bp_permanent && loc->inserted
12705 && b->enable_state != bp_permanent)
12706 internal_error (__FILE__, __LINE__,
12707 _("another breakpoint was inserted on top of "
12708 "a permanent breakpoint"));
12709 }
12710
12711 if (breakpoints_always_inserted_mode ()
12712 && (have_live_inferiors ()
12713 || (gdbarch_has_global_breakpoints (target_gdbarch ()))))
12714 {
12715 if (should_insert)
12716 insert_breakpoint_locations ();
12717 else
12718 {
12719 /* Though should_insert is false, we may need to update conditions
12720 on the target's side if it is evaluating such conditions. We
12721 only update conditions for locations that are marked
12722 "needs_update". */
12723 update_inserted_breakpoint_locations ();
12724 }
12725 }
12726
12727 if (should_insert)
12728 download_tracepoint_locations ();
12729
12730 do_cleanups (cleanups);
12731 }
12732
12733 void
12734 breakpoint_retire_moribund (void)
12735 {
12736 struct bp_location *loc;
12737 int ix;
12738
12739 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12740 if (--(loc->events_till_retirement) == 0)
12741 {
12742 decref_bp_location (&loc);
12743 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12744 --ix;
12745 }
12746 }
12747
12748 static void
12749 update_global_location_list_nothrow (int inserting)
12750 {
12751 volatile struct gdb_exception e;
12752
12753 TRY_CATCH (e, RETURN_MASK_ERROR)
12754 update_global_location_list (inserting);
12755 }
12756
12757 /* Clear BKP from a BPS. */
12758
12759 static void
12760 bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12761 {
12762 bpstat bs;
12763
12764 for (bs = bps; bs; bs = bs->next)
12765 if (bs->breakpoint_at == bpt)
12766 {
12767 bs->breakpoint_at = NULL;
12768 bs->old_val = NULL;
12769 /* bs->commands will be freed later. */
12770 }
12771 }
12772
12773 /* Callback for iterate_over_threads. */
12774 static int
12775 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12776 {
12777 struct breakpoint *bpt = data;
12778
12779 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12780 return 0;
12781 }
12782
12783 /* Helper for breakpoint and tracepoint breakpoint_ops->mention
12784 callbacks. */
12785
12786 static void
12787 say_where (struct breakpoint *b)
12788 {
12789 struct value_print_options opts;
12790
12791 get_user_print_options (&opts);
12792
12793 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12794 single string. */
12795 if (b->loc == NULL)
12796 {
12797 printf_filtered (_(" (%s) pending."), b->addr_string);
12798 }
12799 else
12800 {
12801 if (opts.addressprint || b->loc->symtab == NULL)
12802 {
12803 printf_filtered (" at ");
12804 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12805 gdb_stdout);
12806 }
12807 if (b->loc->symtab != NULL)
12808 {
12809 /* If there is a single location, we can print the location
12810 more nicely. */
12811 if (b->loc->next == NULL)
12812 printf_filtered (": file %s, line %d.",
12813 symtab_to_filename_for_display (b->loc->symtab),
12814 b->loc->line_number);
12815 else
12816 /* This is not ideal, but each location may have a
12817 different file name, and this at least reflects the
12818 real situation somewhat. */
12819 printf_filtered (": %s.", b->addr_string);
12820 }
12821
12822 if (b->loc->next)
12823 {
12824 struct bp_location *loc = b->loc;
12825 int n = 0;
12826 for (; loc; loc = loc->next)
12827 ++n;
12828 printf_filtered (" (%d locations)", n);
12829 }
12830 }
12831 }
12832
12833 /* Default bp_location_ops methods. */
12834
12835 static void
12836 bp_location_dtor (struct bp_location *self)
12837 {
12838 xfree (self->cond);
12839 if (self->cond_bytecode)
12840 free_agent_expr (self->cond_bytecode);
12841 xfree (self->function_name);
12842 }
12843
12844 static const struct bp_location_ops bp_location_ops =
12845 {
12846 bp_location_dtor
12847 };
12848
12849 /* Default breakpoint_ops methods all breakpoint_ops ultimately
12850 inherit from. */
12851
12852 static void
12853 base_breakpoint_dtor (struct breakpoint *self)
12854 {
12855 decref_counted_command_line (&self->commands);
12856 xfree (self->cond_string);
12857 xfree (self->extra_string);
12858 xfree (self->addr_string);
12859 xfree (self->filter);
12860 xfree (self->addr_string_range_end);
12861 }
12862
12863 static struct bp_location *
12864 base_breakpoint_allocate_location (struct breakpoint *self)
12865 {
12866 struct bp_location *loc;
12867
12868 loc = XNEW (struct bp_location);
12869 init_bp_location (loc, &bp_location_ops, self);
12870 return loc;
12871 }
12872
12873 static void
12874 base_breakpoint_re_set (struct breakpoint *b)
12875 {
12876 /* Nothing to re-set. */
12877 }
12878
12879 #define internal_error_pure_virtual_called() \
12880 gdb_assert_not_reached ("pure virtual function called")
12881
12882 static int
12883 base_breakpoint_insert_location (struct bp_location *bl)
12884 {
12885 internal_error_pure_virtual_called ();
12886 }
12887
12888 static int
12889 base_breakpoint_remove_location (struct bp_location *bl)
12890 {
12891 internal_error_pure_virtual_called ();
12892 }
12893
12894 static int
12895 base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12896 struct address_space *aspace,
12897 CORE_ADDR bp_addr,
12898 const struct target_waitstatus *ws)
12899 {
12900 internal_error_pure_virtual_called ();
12901 }
12902
12903 static void
12904 base_breakpoint_check_status (bpstat bs)
12905 {
12906 /* Always stop. */
12907 }
12908
12909 /* A "works_in_software_mode" breakpoint_ops method that just internal
12910 errors. */
12911
12912 static int
12913 base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12914 {
12915 internal_error_pure_virtual_called ();
12916 }
12917
12918 /* A "resources_needed" breakpoint_ops method that just internal
12919 errors. */
12920
12921 static int
12922 base_breakpoint_resources_needed (const struct bp_location *bl)
12923 {
12924 internal_error_pure_virtual_called ();
12925 }
12926
12927 static enum print_stop_action
12928 base_breakpoint_print_it (bpstat bs)
12929 {
12930 internal_error_pure_virtual_called ();
12931 }
12932
12933 static void
12934 base_breakpoint_print_one_detail (const struct breakpoint *self,
12935 struct ui_out *uiout)
12936 {
12937 /* nothing */
12938 }
12939
12940 static void
12941 base_breakpoint_print_mention (struct breakpoint *b)
12942 {
12943 internal_error_pure_virtual_called ();
12944 }
12945
12946 static void
12947 base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12948 {
12949 internal_error_pure_virtual_called ();
12950 }
12951
12952 static void
12953 base_breakpoint_create_sals_from_address (char **arg,
12954 struct linespec_result *canonical,
12955 enum bptype type_wanted,
12956 char *addr_start,
12957 char **copy_arg)
12958 {
12959 internal_error_pure_virtual_called ();
12960 }
12961
12962 static void
12963 base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12964 struct linespec_result *c,
12965 struct linespec_sals *lsal,
12966 char *cond_string,
12967 char *extra_string,
12968 enum bptype type_wanted,
12969 enum bpdisp disposition,
12970 int thread,
12971 int task, int ignore_count,
12972 const struct breakpoint_ops *o,
12973 int from_tty, int enabled,
12974 int internal, unsigned flags)
12975 {
12976 internal_error_pure_virtual_called ();
12977 }
12978
12979 static void
12980 base_breakpoint_decode_linespec (struct breakpoint *b, char **s,
12981 struct symtabs_and_lines *sals)
12982 {
12983 internal_error_pure_virtual_called ();
12984 }
12985
12986 /* The default 'explains_signal' method. */
12987
12988 static enum bpstat_signal_value
12989 base_breakpoint_explains_signal (struct breakpoint *b)
12990 {
12991 return BPSTAT_SIGNAL_HIDE;
12992 }
12993
12994 struct breakpoint_ops base_breakpoint_ops =
12995 {
12996 base_breakpoint_dtor,
12997 base_breakpoint_allocate_location,
12998 base_breakpoint_re_set,
12999 base_breakpoint_insert_location,
13000 base_breakpoint_remove_location,
13001 base_breakpoint_breakpoint_hit,
13002 base_breakpoint_check_status,
13003 base_breakpoint_resources_needed,
13004 base_breakpoint_works_in_software_mode,
13005 base_breakpoint_print_it,
13006 NULL,
13007 base_breakpoint_print_one_detail,
13008 base_breakpoint_print_mention,
13009 base_breakpoint_print_recreate,
13010 base_breakpoint_create_sals_from_address,
13011 base_breakpoint_create_breakpoints_sal,
13012 base_breakpoint_decode_linespec,
13013 base_breakpoint_explains_signal
13014 };
13015
13016 /* Default breakpoint_ops methods. */
13017
13018 static void
13019 bkpt_re_set (struct breakpoint *b)
13020 {
13021 /* FIXME: is this still reachable? */
13022 if (b->addr_string == NULL)
13023 {
13024 /* Anything without a string can't be re-set. */
13025 delete_breakpoint (b);
13026 return;
13027 }
13028
13029 breakpoint_re_set_default (b);
13030 }
13031
13032 static int
13033 bkpt_insert_location (struct bp_location *bl)
13034 {
13035 if (bl->loc_type == bp_loc_hardware_breakpoint)
13036 return target_insert_hw_breakpoint (bl->gdbarch,
13037 &bl->target_info);
13038 else
13039 return target_insert_breakpoint (bl->gdbarch,
13040 &bl->target_info);
13041 }
13042
13043 static int
13044 bkpt_remove_location (struct bp_location *bl)
13045 {
13046 if (bl->loc_type == bp_loc_hardware_breakpoint)
13047 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
13048 else
13049 return target_remove_breakpoint (bl->gdbarch, &bl->target_info);
13050 }
13051
13052 static int
13053 bkpt_breakpoint_hit (const struct bp_location *bl,
13054 struct address_space *aspace, CORE_ADDR bp_addr,
13055 const struct target_waitstatus *ws)
13056 {
13057 if (ws->kind != TARGET_WAITKIND_STOPPED
13058 || ws->value.sig != GDB_SIGNAL_TRAP)
13059 return 0;
13060
13061 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
13062 aspace, bp_addr))
13063 return 0;
13064
13065 if (overlay_debugging /* unmapped overlay section */
13066 && section_is_overlay (bl->section)
13067 && !section_is_mapped (bl->section))
13068 return 0;
13069
13070 return 1;
13071 }
13072
13073 static int
13074 bkpt_resources_needed (const struct bp_location *bl)
13075 {
13076 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
13077
13078 return 1;
13079 }
13080
13081 static enum print_stop_action
13082 bkpt_print_it (bpstat bs)
13083 {
13084 struct breakpoint *b;
13085 const struct bp_location *bl;
13086 int bp_temp;
13087 struct ui_out *uiout = current_uiout;
13088
13089 gdb_assert (bs->bp_location_at != NULL);
13090
13091 bl = bs->bp_location_at;
13092 b = bs->breakpoint_at;
13093
13094 bp_temp = b->disposition == disp_del;
13095 if (bl->address != bl->requested_address)
13096 breakpoint_adjustment_warning (bl->requested_address,
13097 bl->address,
13098 b->number, 1);
13099 annotate_breakpoint (b->number);
13100 if (bp_temp)
13101 ui_out_text (uiout, "\nTemporary breakpoint ");
13102 else
13103 ui_out_text (uiout, "\nBreakpoint ");
13104 if (ui_out_is_mi_like_p (uiout))
13105 {
13106 ui_out_field_string (uiout, "reason",
13107 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
13108 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
13109 }
13110 ui_out_field_int (uiout, "bkptno", b->number);
13111 ui_out_text (uiout, ", ");
13112
13113 return PRINT_SRC_AND_LOC;
13114 }
13115
13116 static void
13117 bkpt_print_mention (struct breakpoint *b)
13118 {
13119 if (ui_out_is_mi_like_p (current_uiout))
13120 return;
13121
13122 switch (b->type)
13123 {
13124 case bp_breakpoint:
13125 case bp_gnu_ifunc_resolver:
13126 if (b->disposition == disp_del)
13127 printf_filtered (_("Temporary breakpoint"));
13128 else
13129 printf_filtered (_("Breakpoint"));
13130 printf_filtered (_(" %d"), b->number);
13131 if (b->type == bp_gnu_ifunc_resolver)
13132 printf_filtered (_(" at gnu-indirect-function resolver"));
13133 break;
13134 case bp_hardware_breakpoint:
13135 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
13136 break;
13137 case bp_dprintf:
13138 printf_filtered (_("Dprintf %d"), b->number);
13139 break;
13140 }
13141
13142 say_where (b);
13143 }
13144
13145 static void
13146 bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13147 {
13148 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
13149 fprintf_unfiltered (fp, "tbreak");
13150 else if (tp->type == bp_breakpoint)
13151 fprintf_unfiltered (fp, "break");
13152 else if (tp->type == bp_hardware_breakpoint
13153 && tp->disposition == disp_del)
13154 fprintf_unfiltered (fp, "thbreak");
13155 else if (tp->type == bp_hardware_breakpoint)
13156 fprintf_unfiltered (fp, "hbreak");
13157 else
13158 internal_error (__FILE__, __LINE__,
13159 _("unhandled breakpoint type %d"), (int) tp->type);
13160
13161 fprintf_unfiltered (fp, " %s", tp->addr_string);
13162 print_recreate_thread (tp, fp);
13163 }
13164
13165 static void
13166 bkpt_create_sals_from_address (char **arg,
13167 struct linespec_result *canonical,
13168 enum bptype type_wanted,
13169 char *addr_start, char **copy_arg)
13170 {
13171 create_sals_from_address_default (arg, canonical, type_wanted,
13172 addr_start, copy_arg);
13173 }
13174
13175 static void
13176 bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
13177 struct linespec_result *canonical,
13178 struct linespec_sals *lsal,
13179 char *cond_string,
13180 char *extra_string,
13181 enum bptype type_wanted,
13182 enum bpdisp disposition,
13183 int thread,
13184 int task, int ignore_count,
13185 const struct breakpoint_ops *ops,
13186 int from_tty, int enabled,
13187 int internal, unsigned flags)
13188 {
13189 create_breakpoints_sal_default (gdbarch, canonical, lsal,
13190 cond_string, extra_string,
13191 type_wanted,
13192 disposition, thread, task,
13193 ignore_count, ops, from_tty,
13194 enabled, internal, flags);
13195 }
13196
13197 static void
13198 bkpt_decode_linespec (struct breakpoint *b, char **s,
13199 struct symtabs_and_lines *sals)
13200 {
13201 decode_linespec_default (b, s, sals);
13202 }
13203
13204 /* Virtual table for internal breakpoints. */
13205
13206 static void
13207 internal_bkpt_re_set (struct breakpoint *b)
13208 {
13209 switch (b->type)
13210 {
13211 /* Delete overlay event and longjmp master breakpoints; they
13212 will be reset later by breakpoint_re_set. */
13213 case bp_overlay_event:
13214 case bp_longjmp_master:
13215 case bp_std_terminate_master:
13216 case bp_exception_master:
13217 delete_breakpoint (b);
13218 break;
13219
13220 /* This breakpoint is special, it's set up when the inferior
13221 starts and we really don't want to touch it. */
13222 case bp_shlib_event:
13223
13224 /* Like bp_shlib_event, this breakpoint type is special. Once
13225 it is set up, we do not want to touch it. */
13226 case bp_thread_event:
13227 break;
13228 }
13229 }
13230
13231 static void
13232 internal_bkpt_check_status (bpstat bs)
13233 {
13234 if (bs->breakpoint_at->type == bp_shlib_event)
13235 {
13236 /* If requested, stop when the dynamic linker notifies GDB of
13237 events. This allows the user to get control and place
13238 breakpoints in initializer routines for dynamically loaded
13239 objects (among other things). */
13240 bs->stop = stop_on_solib_events;
13241 bs->print = stop_on_solib_events;
13242 }
13243 else
13244 bs->stop = 0;
13245 }
13246
13247 static enum print_stop_action
13248 internal_bkpt_print_it (bpstat bs)
13249 {
13250 struct breakpoint *b;
13251
13252 b = bs->breakpoint_at;
13253
13254 switch (b->type)
13255 {
13256 case bp_shlib_event:
13257 /* Did we stop because the user set the stop_on_solib_events
13258 variable? (If so, we report this as a generic, "Stopped due
13259 to shlib event" message.) */
13260 print_solib_event (0);
13261 break;
13262
13263 case bp_thread_event:
13264 /* Not sure how we will get here.
13265 GDB should not stop for these breakpoints. */
13266 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
13267 break;
13268
13269 case bp_overlay_event:
13270 /* By analogy with the thread event, GDB should not stop for these. */
13271 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
13272 break;
13273
13274 case bp_longjmp_master:
13275 /* These should never be enabled. */
13276 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
13277 break;
13278
13279 case bp_std_terminate_master:
13280 /* These should never be enabled. */
13281 printf_filtered (_("std::terminate Master Breakpoint: "
13282 "gdb should not stop!\n"));
13283 break;
13284
13285 case bp_exception_master:
13286 /* These should never be enabled. */
13287 printf_filtered (_("Exception Master Breakpoint: "
13288 "gdb should not stop!\n"));
13289 break;
13290 }
13291
13292 return PRINT_NOTHING;
13293 }
13294
13295 static void
13296 internal_bkpt_print_mention (struct breakpoint *b)
13297 {
13298 /* Nothing to mention. These breakpoints are internal. */
13299 }
13300
13301 /* Virtual table for momentary breakpoints */
13302
13303 static void
13304 momentary_bkpt_re_set (struct breakpoint *b)
13305 {
13306 /* Keep temporary breakpoints, which can be encountered when we step
13307 over a dlopen call and SOLIB_ADD is resetting the breakpoints.
13308 Otherwise these should have been blown away via the cleanup chain
13309 or by breakpoint_init_inferior when we rerun the executable. */
13310 }
13311
13312 static void
13313 momentary_bkpt_check_status (bpstat bs)
13314 {
13315 /* Nothing. The point of these breakpoints is causing a stop. */
13316 }
13317
13318 static enum print_stop_action
13319 momentary_bkpt_print_it (bpstat bs)
13320 {
13321 struct ui_out *uiout = current_uiout;
13322
13323 if (ui_out_is_mi_like_p (uiout))
13324 {
13325 struct breakpoint *b = bs->breakpoint_at;
13326
13327 switch (b->type)
13328 {
13329 case bp_finish:
13330 ui_out_field_string
13331 (uiout, "reason",
13332 async_reason_lookup (EXEC_ASYNC_FUNCTION_FINISHED));
13333 break;
13334
13335 case bp_until:
13336 ui_out_field_string
13337 (uiout, "reason",
13338 async_reason_lookup (EXEC_ASYNC_LOCATION_REACHED));
13339 break;
13340 }
13341 }
13342
13343 return PRINT_UNKNOWN;
13344 }
13345
13346 static void
13347 momentary_bkpt_print_mention (struct breakpoint *b)
13348 {
13349 /* Nothing to mention. These breakpoints are internal. */
13350 }
13351
13352 /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
13353
13354 It gets cleared already on the removal of the first one of such placed
13355 breakpoints. This is OK as they get all removed altogether. */
13356
13357 static void
13358 longjmp_bkpt_dtor (struct breakpoint *self)
13359 {
13360 struct thread_info *tp = find_thread_id (self->thread);
13361
13362 if (tp)
13363 tp->initiating_frame = null_frame_id;
13364
13365 momentary_breakpoint_ops.dtor (self);
13366 }
13367
13368 /* Specific methods for probe breakpoints. */
13369
13370 static int
13371 bkpt_probe_insert_location (struct bp_location *bl)
13372 {
13373 int v = bkpt_insert_location (bl);
13374
13375 if (v == 0)
13376 {
13377 /* The insertion was successful, now let's set the probe's semaphore
13378 if needed. */
13379 bl->probe->pops->set_semaphore (bl->probe, bl->gdbarch);
13380 }
13381
13382 return v;
13383 }
13384
13385 static int
13386 bkpt_probe_remove_location (struct bp_location *bl)
13387 {
13388 /* Let's clear the semaphore before removing the location. */
13389 bl->probe->pops->clear_semaphore (bl->probe, bl->gdbarch);
13390
13391 return bkpt_remove_location (bl);
13392 }
13393
13394 static void
13395 bkpt_probe_create_sals_from_address (char **arg,
13396 struct linespec_result *canonical,
13397 enum bptype type_wanted,
13398 char *addr_start, char **copy_arg)
13399 {
13400 struct linespec_sals lsal;
13401
13402 lsal.sals = parse_probes (arg, canonical);
13403
13404 *copy_arg = xstrdup (canonical->addr_string);
13405 lsal.canonical = xstrdup (*copy_arg);
13406
13407 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13408 }
13409
13410 static void
13411 bkpt_probe_decode_linespec (struct breakpoint *b, char **s,
13412 struct symtabs_and_lines *sals)
13413 {
13414 *sals = parse_probes (s, NULL);
13415 if (!sals->sals)
13416 error (_("probe not found"));
13417 }
13418
13419 /* The breakpoint_ops structure to be used in tracepoints. */
13420
13421 static void
13422 tracepoint_re_set (struct breakpoint *b)
13423 {
13424 breakpoint_re_set_default (b);
13425 }
13426
13427 static int
13428 tracepoint_breakpoint_hit (const struct bp_location *bl,
13429 struct address_space *aspace, CORE_ADDR bp_addr,
13430 const struct target_waitstatus *ws)
13431 {
13432 /* By definition, the inferior does not report stops at
13433 tracepoints. */
13434 return 0;
13435 }
13436
13437 static void
13438 tracepoint_print_one_detail (const struct breakpoint *self,
13439 struct ui_out *uiout)
13440 {
13441 struct tracepoint *tp = (struct tracepoint *) self;
13442 if (tp->static_trace_marker_id)
13443 {
13444 gdb_assert (self->type == bp_static_tracepoint);
13445
13446 ui_out_text (uiout, "\tmarker id is ");
13447 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
13448 tp->static_trace_marker_id);
13449 ui_out_text (uiout, "\n");
13450 }
13451 }
13452
13453 static void
13454 tracepoint_print_mention (struct breakpoint *b)
13455 {
13456 if (ui_out_is_mi_like_p (current_uiout))
13457 return;
13458
13459 switch (b->type)
13460 {
13461 case bp_tracepoint:
13462 printf_filtered (_("Tracepoint"));
13463 printf_filtered (_(" %d"), b->number);
13464 break;
13465 case bp_fast_tracepoint:
13466 printf_filtered (_("Fast tracepoint"));
13467 printf_filtered (_(" %d"), b->number);
13468 break;
13469 case bp_static_tracepoint:
13470 printf_filtered (_("Static tracepoint"));
13471 printf_filtered (_(" %d"), b->number);
13472 break;
13473 default:
13474 internal_error (__FILE__, __LINE__,
13475 _("unhandled tracepoint type %d"), (int) b->type);
13476 }
13477
13478 say_where (b);
13479 }
13480
13481 static void
13482 tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
13483 {
13484 struct tracepoint *tp = (struct tracepoint *) self;
13485
13486 if (self->type == bp_fast_tracepoint)
13487 fprintf_unfiltered (fp, "ftrace");
13488 if (self->type == bp_static_tracepoint)
13489 fprintf_unfiltered (fp, "strace");
13490 else if (self->type == bp_tracepoint)
13491 fprintf_unfiltered (fp, "trace");
13492 else
13493 internal_error (__FILE__, __LINE__,
13494 _("unhandled tracepoint type %d"), (int) self->type);
13495
13496 fprintf_unfiltered (fp, " %s", self->addr_string);
13497 print_recreate_thread (self, fp);
13498
13499 if (tp->pass_count)
13500 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
13501 }
13502
13503 static void
13504 tracepoint_create_sals_from_address (char **arg,
13505 struct linespec_result *canonical,
13506 enum bptype type_wanted,
13507 char *addr_start, char **copy_arg)
13508 {
13509 create_sals_from_address_default (arg, canonical, type_wanted,
13510 addr_start, copy_arg);
13511 }
13512
13513 static void
13514 tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13515 struct linespec_result *canonical,
13516 struct linespec_sals *lsal,
13517 char *cond_string,
13518 char *extra_string,
13519 enum bptype type_wanted,
13520 enum bpdisp disposition,
13521 int thread,
13522 int task, int ignore_count,
13523 const struct breakpoint_ops *ops,
13524 int from_tty, int enabled,
13525 int internal, unsigned flags)
13526 {
13527 create_breakpoints_sal_default (gdbarch, canonical, lsal,
13528 cond_string, extra_string,
13529 type_wanted,
13530 disposition, thread, task,
13531 ignore_count, ops, from_tty,
13532 enabled, internal, flags);
13533 }
13534
13535 static void
13536 tracepoint_decode_linespec (struct breakpoint *b, char **s,
13537 struct symtabs_and_lines *sals)
13538 {
13539 decode_linespec_default (b, s, sals);
13540 }
13541
13542 struct breakpoint_ops tracepoint_breakpoint_ops;
13543
13544 /* The breakpoint_ops structure to be use on tracepoints placed in a
13545 static probe. */
13546
13547 static void
13548 tracepoint_probe_create_sals_from_address (char **arg,
13549 struct linespec_result *canonical,
13550 enum bptype type_wanted,
13551 char *addr_start, char **copy_arg)
13552 {
13553 /* We use the same method for breakpoint on probes. */
13554 bkpt_probe_create_sals_from_address (arg, canonical, type_wanted,
13555 addr_start, copy_arg);
13556 }
13557
13558 static void
13559 tracepoint_probe_decode_linespec (struct breakpoint *b, char **s,
13560 struct symtabs_and_lines *sals)
13561 {
13562 /* We use the same method for breakpoint on probes. */
13563 bkpt_probe_decode_linespec (b, s, sals);
13564 }
13565
13566 static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13567
13568 /* Dprintf breakpoint_ops methods. */
13569
13570 static void
13571 dprintf_re_set (struct breakpoint *b)
13572 {
13573 breakpoint_re_set_default (b);
13574
13575 /* This breakpoint could have been pending, and be resolved now, and
13576 if so, we should now have the extra string. If we don't, the
13577 dprintf was malformed when created, but we couldn't tell because
13578 we can't extract the extra string until the location is
13579 resolved. */
13580 if (b->loc != NULL && b->extra_string == NULL)
13581 error (_("Format string required"));
13582
13583 /* 1 - connect to target 1, that can run breakpoint commands.
13584 2 - create a dprintf, which resolves fine.
13585 3 - disconnect from target 1
13586 4 - connect to target 2, that can NOT run breakpoint commands.
13587
13588 After steps #3/#4, you'll want the dprintf command list to
13589 be updated, because target 1 and 2 may well return different
13590 answers for target_can_run_breakpoint_commands().
13591 Given absence of finer grained resetting, we get to do
13592 it all the time. */
13593 if (b->extra_string != NULL)
13594 update_dprintf_command_list (b);
13595 }
13596
13597 /* The breakpoint_ops structure to be used on static tracepoints with
13598 markers (`-m'). */
13599
13600 static void
13601 strace_marker_create_sals_from_address (char **arg,
13602 struct linespec_result *canonical,
13603 enum bptype type_wanted,
13604 char *addr_start, char **copy_arg)
13605 {
13606 struct linespec_sals lsal;
13607
13608 lsal.sals = decode_static_tracepoint_spec (arg);
13609
13610 *copy_arg = savestring (addr_start, *arg - addr_start);
13611
13612 canonical->addr_string = xstrdup (*copy_arg);
13613 lsal.canonical = xstrdup (*copy_arg);
13614 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13615 }
13616
13617 static void
13618 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13619 struct linespec_result *canonical,
13620 struct linespec_sals *lsal,
13621 char *cond_string,
13622 char *extra_string,
13623 enum bptype type_wanted,
13624 enum bpdisp disposition,
13625 int thread,
13626 int task, int ignore_count,
13627 const struct breakpoint_ops *ops,
13628 int from_tty, int enabled,
13629 int internal, unsigned flags)
13630 {
13631 int i;
13632
13633 /* If the user is creating a static tracepoint by marker id
13634 (strace -m MARKER_ID), then store the sals index, so that
13635 breakpoint_re_set can try to match up which of the newly
13636 found markers corresponds to this one, and, don't try to
13637 expand multiple locations for each sal, given than SALS
13638 already should contain all sals for MARKER_ID. */
13639
13640 for (i = 0; i < lsal->sals.nelts; ++i)
13641 {
13642 struct symtabs_and_lines expanded;
13643 struct tracepoint *tp;
13644 struct cleanup *old_chain;
13645 char *addr_string;
13646
13647 expanded.nelts = 1;
13648 expanded.sals = &lsal->sals.sals[i];
13649
13650 addr_string = xstrdup (canonical->addr_string);
13651 old_chain = make_cleanup (xfree, addr_string);
13652
13653 tp = XCNEW (struct tracepoint);
13654 init_breakpoint_sal (&tp->base, gdbarch, expanded,
13655 addr_string, NULL,
13656 cond_string, extra_string,
13657 type_wanted, disposition,
13658 thread, task, ignore_count, ops,
13659 from_tty, enabled, internal, flags,
13660 canonical->special_display);
13661 /* Given that its possible to have multiple markers with
13662 the same string id, if the user is creating a static
13663 tracepoint by marker id ("strace -m MARKER_ID"), then
13664 store the sals index, so that breakpoint_re_set can
13665 try to match up which of the newly found markers
13666 corresponds to this one */
13667 tp->static_trace_marker_id_idx = i;
13668
13669 install_breakpoint (internal, &tp->base, 0);
13670
13671 discard_cleanups (old_chain);
13672 }
13673 }
13674
13675 static void
13676 strace_marker_decode_linespec (struct breakpoint *b, char **s,
13677 struct symtabs_and_lines *sals)
13678 {
13679 struct tracepoint *tp = (struct tracepoint *) b;
13680
13681 *sals = decode_static_tracepoint_spec (s);
13682 if (sals->nelts > tp->static_trace_marker_id_idx)
13683 {
13684 sals->sals[0] = sals->sals[tp->static_trace_marker_id_idx];
13685 sals->nelts = 1;
13686 }
13687 else
13688 error (_("marker %s not found"), tp->static_trace_marker_id);
13689 }
13690
13691 static struct breakpoint_ops strace_marker_breakpoint_ops;
13692
13693 static int
13694 strace_marker_p (struct breakpoint *b)
13695 {
13696 return b->ops == &strace_marker_breakpoint_ops;
13697 }
13698
13699 /* Delete a breakpoint and clean up all traces of it in the data
13700 structures. */
13701
13702 void
13703 delete_breakpoint (struct breakpoint *bpt)
13704 {
13705 struct breakpoint *b;
13706
13707 gdb_assert (bpt != NULL);
13708
13709 /* Has this bp already been deleted? This can happen because
13710 multiple lists can hold pointers to bp's. bpstat lists are
13711 especial culprits.
13712
13713 One example of this happening is a watchpoint's scope bp. When
13714 the scope bp triggers, we notice that the watchpoint is out of
13715 scope, and delete it. We also delete its scope bp. But the
13716 scope bp is marked "auto-deleting", and is already on a bpstat.
13717 That bpstat is then checked for auto-deleting bp's, which are
13718 deleted.
13719
13720 A real solution to this problem might involve reference counts in
13721 bp's, and/or giving them pointers back to their referencing
13722 bpstat's, and teaching delete_breakpoint to only free a bp's
13723 storage when no more references were extent. A cheaper bandaid
13724 was chosen. */
13725 if (bpt->type == bp_none)
13726 return;
13727
13728 /* At least avoid this stale reference until the reference counting
13729 of breakpoints gets resolved. */
13730 if (bpt->related_breakpoint != bpt)
13731 {
13732 struct breakpoint *related;
13733 struct watchpoint *w;
13734
13735 if (bpt->type == bp_watchpoint_scope)
13736 w = (struct watchpoint *) bpt->related_breakpoint;
13737 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
13738 w = (struct watchpoint *) bpt;
13739 else
13740 w = NULL;
13741 if (w != NULL)
13742 watchpoint_del_at_next_stop (w);
13743
13744 /* Unlink bpt from the bpt->related_breakpoint ring. */
13745 for (related = bpt; related->related_breakpoint != bpt;
13746 related = related->related_breakpoint);
13747 related->related_breakpoint = bpt->related_breakpoint;
13748 bpt->related_breakpoint = bpt;
13749 }
13750
13751 /* watch_command_1 creates a watchpoint but only sets its number if
13752 update_watchpoint succeeds in creating its bp_locations. If there's
13753 a problem in that process, we'll be asked to delete the half-created
13754 watchpoint. In that case, don't announce the deletion. */
13755 if (bpt->number)
13756 observer_notify_breakpoint_deleted (bpt);
13757
13758 if (breakpoint_chain == bpt)
13759 breakpoint_chain = bpt->next;
13760
13761 ALL_BREAKPOINTS (b)
13762 if (b->next == bpt)
13763 {
13764 b->next = bpt->next;
13765 break;
13766 }
13767
13768 /* Be sure no bpstat's are pointing at the breakpoint after it's
13769 been freed. */
13770 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
13771 in all threads for now. Note that we cannot just remove bpstats
13772 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13773 commands are associated with the bpstat; if we remove it here,
13774 then the later call to bpstat_do_actions (&stop_bpstat); in
13775 event-top.c won't do anything, and temporary breakpoints with
13776 commands won't work. */
13777
13778 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13779
13780 /* Now that breakpoint is removed from breakpoint list, update the
13781 global location list. This will remove locations that used to
13782 belong to this breakpoint. Do this before freeing the breakpoint
13783 itself, since remove_breakpoint looks at location's owner. It
13784 might be better design to have location completely
13785 self-contained, but it's not the case now. */
13786 update_global_location_list (0);
13787
13788 bpt->ops->dtor (bpt);
13789 /* On the chance that someone will soon try again to delete this
13790 same bp, we mark it as deleted before freeing its storage. */
13791 bpt->type = bp_none;
13792 xfree (bpt);
13793 }
13794
13795 static void
13796 do_delete_breakpoint_cleanup (void *b)
13797 {
13798 delete_breakpoint (b);
13799 }
13800
13801 struct cleanup *
13802 make_cleanup_delete_breakpoint (struct breakpoint *b)
13803 {
13804 return make_cleanup (do_delete_breakpoint_cleanup, b);
13805 }
13806
13807 /* Iterator function to call a user-provided callback function once
13808 for each of B and its related breakpoints. */
13809
13810 static void
13811 iterate_over_related_breakpoints (struct breakpoint *b,
13812 void (*function) (struct breakpoint *,
13813 void *),
13814 void *data)
13815 {
13816 struct breakpoint *related;
13817
13818 related = b;
13819 do
13820 {
13821 struct breakpoint *next;
13822
13823 /* FUNCTION may delete RELATED. */
13824 next = related->related_breakpoint;
13825
13826 if (next == related)
13827 {
13828 /* RELATED is the last ring entry. */
13829 function (related, data);
13830
13831 /* FUNCTION may have deleted it, so we'd never reach back to
13832 B. There's nothing left to do anyway, so just break
13833 out. */
13834 break;
13835 }
13836 else
13837 function (related, data);
13838
13839 related = next;
13840 }
13841 while (related != b);
13842 }
13843
13844 static void
13845 do_delete_breakpoint (struct breakpoint *b, void *ignore)
13846 {
13847 delete_breakpoint (b);
13848 }
13849
13850 /* A callback for map_breakpoint_numbers that calls
13851 delete_breakpoint. */
13852
13853 static void
13854 do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
13855 {
13856 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
13857 }
13858
13859 void
13860 delete_command (char *arg, int from_tty)
13861 {
13862 struct breakpoint *b, *b_tmp;
13863
13864 dont_repeat ();
13865
13866 if (arg == 0)
13867 {
13868 int breaks_to_delete = 0;
13869
13870 /* Delete all breakpoints if no argument. Do not delete
13871 internal breakpoints, these have to be deleted with an
13872 explicit breakpoint number argument. */
13873 ALL_BREAKPOINTS (b)
13874 if (user_breakpoint_p (b))
13875 {
13876 breaks_to_delete = 1;
13877 break;
13878 }
13879
13880 /* Ask user only if there are some breakpoints to delete. */
13881 if (!from_tty
13882 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
13883 {
13884 ALL_BREAKPOINTS_SAFE (b, b_tmp)
13885 if (user_breakpoint_p (b))
13886 delete_breakpoint (b);
13887 }
13888 }
13889 else
13890 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
13891 }
13892
13893 static int
13894 all_locations_are_pending (struct bp_location *loc)
13895 {
13896 for (; loc; loc = loc->next)
13897 if (!loc->shlib_disabled
13898 && !loc->pspace->executing_startup)
13899 return 0;
13900 return 1;
13901 }
13902
13903 /* Subroutine of update_breakpoint_locations to simplify it.
13904 Return non-zero if multiple fns in list LOC have the same name.
13905 Null names are ignored. */
13906
13907 static int
13908 ambiguous_names_p (struct bp_location *loc)
13909 {
13910 struct bp_location *l;
13911 htab_t htab = htab_create_alloc (13, htab_hash_string,
13912 (int (*) (const void *,
13913 const void *)) streq,
13914 NULL, xcalloc, xfree);
13915
13916 for (l = loc; l != NULL; l = l->next)
13917 {
13918 const char **slot;
13919 const char *name = l->function_name;
13920
13921 /* Allow for some names to be NULL, ignore them. */
13922 if (name == NULL)
13923 continue;
13924
13925 slot = (const char **) htab_find_slot (htab, (const void *) name,
13926 INSERT);
13927 /* NOTE: We can assume slot != NULL here because xcalloc never
13928 returns NULL. */
13929 if (*slot != NULL)
13930 {
13931 htab_delete (htab);
13932 return 1;
13933 }
13934 *slot = name;
13935 }
13936
13937 htab_delete (htab);
13938 return 0;
13939 }
13940
13941 /* When symbols change, it probably means the sources changed as well,
13942 and it might mean the static tracepoint markers are no longer at
13943 the same address or line numbers they used to be at last we
13944 checked. Losing your static tracepoints whenever you rebuild is
13945 undesirable. This function tries to resync/rematch gdb static
13946 tracepoints with the markers on the target, for static tracepoints
13947 that have not been set by marker id. Static tracepoint that have
13948 been set by marker id are reset by marker id in breakpoint_re_set.
13949 The heuristic is:
13950
13951 1) For a tracepoint set at a specific address, look for a marker at
13952 the old PC. If one is found there, assume to be the same marker.
13953 If the name / string id of the marker found is different from the
13954 previous known name, assume that means the user renamed the marker
13955 in the sources, and output a warning.
13956
13957 2) For a tracepoint set at a given line number, look for a marker
13958 at the new address of the old line number. If one is found there,
13959 assume to be the same marker. If the name / string id of the
13960 marker found is different from the previous known name, assume that
13961 means the user renamed the marker in the sources, and output a
13962 warning.
13963
13964 3) If a marker is no longer found at the same address or line, it
13965 may mean the marker no longer exists. But it may also just mean
13966 the code changed a bit. Maybe the user added a few lines of code
13967 that made the marker move up or down (in line number terms). Ask
13968 the target for info about the marker with the string id as we knew
13969 it. If found, update line number and address in the matching
13970 static tracepoint. This will get confused if there's more than one
13971 marker with the same ID (possible in UST, although unadvised
13972 precisely because it confuses tools). */
13973
13974 static struct symtab_and_line
13975 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13976 {
13977 struct tracepoint *tp = (struct tracepoint *) b;
13978 struct static_tracepoint_marker marker;
13979 CORE_ADDR pc;
13980
13981 pc = sal.pc;
13982 if (sal.line)
13983 find_line_pc (sal.symtab, sal.line, &pc);
13984
13985 if (target_static_tracepoint_marker_at (pc, &marker))
13986 {
13987 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
13988 warning (_("static tracepoint %d changed probed marker from %s to %s"),
13989 b->number,
13990 tp->static_trace_marker_id, marker.str_id);
13991
13992 xfree (tp->static_trace_marker_id);
13993 tp->static_trace_marker_id = xstrdup (marker.str_id);
13994 release_static_tracepoint_marker (&marker);
13995
13996 return sal;
13997 }
13998
13999 /* Old marker wasn't found on target at lineno. Try looking it up
14000 by string ID. */
14001 if (!sal.explicit_pc
14002 && sal.line != 0
14003 && sal.symtab != NULL
14004 && tp->static_trace_marker_id != NULL)
14005 {
14006 VEC(static_tracepoint_marker_p) *markers;
14007
14008 markers
14009 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
14010
14011 if (!VEC_empty(static_tracepoint_marker_p, markers))
14012 {
14013 struct symtab_and_line sal2;
14014 struct symbol *sym;
14015 struct static_tracepoint_marker *tpmarker;
14016 struct ui_out *uiout = current_uiout;
14017
14018 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
14019
14020 xfree (tp->static_trace_marker_id);
14021 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
14022
14023 warning (_("marker for static tracepoint %d (%s) not "
14024 "found at previous line number"),
14025 b->number, tp->static_trace_marker_id);
14026
14027 init_sal (&sal2);
14028
14029 sal2.pc = tpmarker->address;
14030
14031 sal2 = find_pc_line (tpmarker->address, 0);
14032 sym = find_pc_sect_function (tpmarker->address, NULL);
14033 ui_out_text (uiout, "Now in ");
14034 if (sym)
14035 {
14036 ui_out_field_string (uiout, "func",
14037 SYMBOL_PRINT_NAME (sym));
14038 ui_out_text (uiout, " at ");
14039 }
14040 ui_out_field_string (uiout, "file",
14041 symtab_to_filename_for_display (sal2.symtab));
14042 ui_out_text (uiout, ":");
14043
14044 if (ui_out_is_mi_like_p (uiout))
14045 {
14046 const char *fullname = symtab_to_fullname (sal2.symtab);
14047
14048 ui_out_field_string (uiout, "fullname", fullname);
14049 }
14050
14051 ui_out_field_int (uiout, "line", sal2.line);
14052 ui_out_text (uiout, "\n");
14053
14054 b->loc->line_number = sal2.line;
14055 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
14056
14057 xfree (b->addr_string);
14058 b->addr_string = xstrprintf ("%s:%d",
14059 symtab_to_filename_for_display (sal2.symtab),
14060 b->loc->line_number);
14061
14062 /* Might be nice to check if function changed, and warn if
14063 so. */
14064
14065 release_static_tracepoint_marker (tpmarker);
14066 }
14067 }
14068 return sal;
14069 }
14070
14071 /* Returns 1 iff locations A and B are sufficiently same that
14072 we don't need to report breakpoint as changed. */
14073
14074 static int
14075 locations_are_equal (struct bp_location *a, struct bp_location *b)
14076 {
14077 while (a && b)
14078 {
14079 if (a->address != b->address)
14080 return 0;
14081
14082 if (a->shlib_disabled != b->shlib_disabled)
14083 return 0;
14084
14085 if (a->enabled != b->enabled)
14086 return 0;
14087
14088 a = a->next;
14089 b = b->next;
14090 }
14091
14092 if ((a == NULL) != (b == NULL))
14093 return 0;
14094
14095 return 1;
14096 }
14097
14098 /* Create new breakpoint locations for B (a hardware or software breakpoint)
14099 based on SALS and SALS_END. If SALS_END.NELTS is not zero, then B is
14100 a ranged breakpoint. */
14101
14102 void
14103 update_breakpoint_locations (struct breakpoint *b,
14104 struct symtabs_and_lines sals,
14105 struct symtabs_and_lines sals_end)
14106 {
14107 int i;
14108 struct bp_location *existing_locations = b->loc;
14109
14110 if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
14111 {
14112 /* Ranged breakpoints have only one start location and one end
14113 location. */
14114 b->enable_state = bp_disabled;
14115 update_global_location_list (1);
14116 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
14117 "multiple locations found\n"),
14118 b->number);
14119 return;
14120 }
14121
14122 /* If there's no new locations, and all existing locations are
14123 pending, don't do anything. This optimizes the common case where
14124 all locations are in the same shared library, that was unloaded.
14125 We'd like to retain the location, so that when the library is
14126 loaded again, we don't loose the enabled/disabled status of the
14127 individual locations. */
14128 if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
14129 return;
14130
14131 b->loc = NULL;
14132
14133 for (i = 0; i < sals.nelts; ++i)
14134 {
14135 struct bp_location *new_loc;
14136
14137 switch_to_program_space_and_thread (sals.sals[i].pspace);
14138
14139 new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
14140
14141 /* Reparse conditions, they might contain references to the
14142 old symtab. */
14143 if (b->cond_string != NULL)
14144 {
14145 const char *s;
14146 volatile struct gdb_exception e;
14147
14148 s = b->cond_string;
14149 TRY_CATCH (e, RETURN_MASK_ERROR)
14150 {
14151 new_loc->cond = parse_exp_1 (&s, sals.sals[i].pc,
14152 block_for_pc (sals.sals[i].pc),
14153 0);
14154 }
14155 if (e.reason < 0)
14156 {
14157 warning (_("failed to reevaluate condition "
14158 "for breakpoint %d: %s"),
14159 b->number, e.message);
14160 new_loc->enabled = 0;
14161 }
14162 }
14163
14164 if (sals_end.nelts)
14165 {
14166 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
14167
14168 new_loc->length = end - sals.sals[0].pc + 1;
14169 }
14170 }
14171
14172 /* Update locations of permanent breakpoints. */
14173 if (b->enable_state == bp_permanent)
14174 make_breakpoint_permanent (b);
14175
14176 /* If possible, carry over 'disable' status from existing
14177 breakpoints. */
14178 {
14179 struct bp_location *e = existing_locations;
14180 /* If there are multiple breakpoints with the same function name,
14181 e.g. for inline functions, comparing function names won't work.
14182 Instead compare pc addresses; this is just a heuristic as things
14183 may have moved, but in practice it gives the correct answer
14184 often enough until a better solution is found. */
14185 int have_ambiguous_names = ambiguous_names_p (b->loc);
14186
14187 for (; e; e = e->next)
14188 {
14189 if (!e->enabled && e->function_name)
14190 {
14191 struct bp_location *l = b->loc;
14192 if (have_ambiguous_names)
14193 {
14194 for (; l; l = l->next)
14195 if (breakpoint_locations_match (e, l))
14196 {
14197 l->enabled = 0;
14198 break;
14199 }
14200 }
14201 else
14202 {
14203 for (; l; l = l->next)
14204 if (l->function_name
14205 && strcmp (e->function_name, l->function_name) == 0)
14206 {
14207 l->enabled = 0;
14208 break;
14209 }
14210 }
14211 }
14212 }
14213 }
14214
14215 if (!locations_are_equal (existing_locations, b->loc))
14216 observer_notify_breakpoint_modified (b);
14217
14218 update_global_location_list (1);
14219 }
14220
14221 /* Find the SaL locations corresponding to the given ADDR_STRING.
14222 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
14223
14224 static struct symtabs_and_lines
14225 addr_string_to_sals (struct breakpoint *b, char *addr_string, int *found)
14226 {
14227 char *s;
14228 struct symtabs_and_lines sals = {0};
14229 volatile struct gdb_exception e;
14230
14231 gdb_assert (b->ops != NULL);
14232 s = addr_string;
14233
14234 TRY_CATCH (e, RETURN_MASK_ERROR)
14235 {
14236 b->ops->decode_linespec (b, &s, &sals);
14237 }
14238 if (e.reason < 0)
14239 {
14240 int not_found_and_ok = 0;
14241 /* For pending breakpoints, it's expected that parsing will
14242 fail until the right shared library is loaded. User has
14243 already told to create pending breakpoints and don't need
14244 extra messages. If breakpoint is in bp_shlib_disabled
14245 state, then user already saw the message about that
14246 breakpoint being disabled, and don't want to see more
14247 errors. */
14248 if (e.error == NOT_FOUND_ERROR
14249 && (b->condition_not_parsed
14250 || (b->loc && b->loc->shlib_disabled)
14251 || (b->loc && b->loc->pspace->executing_startup)
14252 || b->enable_state == bp_disabled))
14253 not_found_and_ok = 1;
14254
14255 if (!not_found_and_ok)
14256 {
14257 /* We surely don't want to warn about the same breakpoint
14258 10 times. One solution, implemented here, is disable
14259 the breakpoint on error. Another solution would be to
14260 have separate 'warning emitted' flag. Since this
14261 happens only when a binary has changed, I don't know
14262 which approach is better. */
14263 b->enable_state = bp_disabled;
14264 throw_exception (e);
14265 }
14266 }
14267
14268 if (e.reason == 0 || e.error != NOT_FOUND_ERROR)
14269 {
14270 int i;
14271
14272 for (i = 0; i < sals.nelts; ++i)
14273 resolve_sal_pc (&sals.sals[i]);
14274 if (b->condition_not_parsed && s && s[0])
14275 {
14276 char *cond_string, *extra_string;
14277 int thread, task;
14278
14279 find_condition_and_thread (s, sals.sals[0].pc,
14280 &cond_string, &thread, &task,
14281 &extra_string);
14282 if (cond_string)
14283 b->cond_string = cond_string;
14284 b->thread = thread;
14285 b->task = task;
14286 if (extra_string)
14287 b->extra_string = extra_string;
14288 b->condition_not_parsed = 0;
14289 }
14290
14291 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
14292 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
14293
14294 *found = 1;
14295 }
14296 else
14297 *found = 0;
14298
14299 return sals;
14300 }
14301
14302 /* The default re_set method, for typical hardware or software
14303 breakpoints. Reevaluate the breakpoint and recreate its
14304 locations. */
14305
14306 static void
14307 breakpoint_re_set_default (struct breakpoint *b)
14308 {
14309 int found;
14310 struct symtabs_and_lines sals, sals_end;
14311 struct symtabs_and_lines expanded = {0};
14312 struct symtabs_and_lines expanded_end = {0};
14313
14314 sals = addr_string_to_sals (b, b->addr_string, &found);
14315 if (found)
14316 {
14317 make_cleanup (xfree, sals.sals);
14318 expanded = sals;
14319 }
14320
14321 if (b->addr_string_range_end)
14322 {
14323 sals_end = addr_string_to_sals (b, b->addr_string_range_end, &found);
14324 if (found)
14325 {
14326 make_cleanup (xfree, sals_end.sals);
14327 expanded_end = sals_end;
14328 }
14329 }
14330
14331 update_breakpoint_locations (b, expanded, expanded_end);
14332 }
14333
14334 /* Default method for creating SALs from an address string. It basically
14335 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
14336
14337 static void
14338 create_sals_from_address_default (char **arg,
14339 struct linespec_result *canonical,
14340 enum bptype type_wanted,
14341 char *addr_start, char **copy_arg)
14342 {
14343 parse_breakpoint_sals (arg, canonical);
14344 }
14345
14346 /* Call create_breakpoints_sal for the given arguments. This is the default
14347 function for the `create_breakpoints_sal' method of
14348 breakpoint_ops. */
14349
14350 static void
14351 create_breakpoints_sal_default (struct gdbarch *gdbarch,
14352 struct linespec_result *canonical,
14353 struct linespec_sals *lsal,
14354 char *cond_string,
14355 char *extra_string,
14356 enum bptype type_wanted,
14357 enum bpdisp disposition,
14358 int thread,
14359 int task, int ignore_count,
14360 const struct breakpoint_ops *ops,
14361 int from_tty, int enabled,
14362 int internal, unsigned flags)
14363 {
14364 create_breakpoints_sal (gdbarch, canonical, cond_string,
14365 extra_string,
14366 type_wanted, disposition,
14367 thread, task, ignore_count, ops, from_tty,
14368 enabled, internal, flags);
14369 }
14370
14371 /* Decode the line represented by S by calling decode_line_full. This is the
14372 default function for the `decode_linespec' method of breakpoint_ops. */
14373
14374 static void
14375 decode_linespec_default (struct breakpoint *b, char **s,
14376 struct symtabs_and_lines *sals)
14377 {
14378 struct linespec_result canonical;
14379
14380 init_linespec_result (&canonical);
14381 decode_line_full (s, DECODE_LINE_FUNFIRSTLINE,
14382 (struct symtab *) NULL, 0,
14383 &canonical, multiple_symbols_all,
14384 b->filter);
14385
14386 /* We should get 0 or 1 resulting SALs. */
14387 gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
14388
14389 if (VEC_length (linespec_sals, canonical.sals) > 0)
14390 {
14391 struct linespec_sals *lsal;
14392
14393 lsal = VEC_index (linespec_sals, canonical.sals, 0);
14394 *sals = lsal->sals;
14395 /* Arrange it so the destructor does not free the
14396 contents. */
14397 lsal->sals.sals = NULL;
14398 }
14399
14400 destroy_linespec_result (&canonical);
14401 }
14402
14403 /* Prepare the global context for a re-set of breakpoint B. */
14404
14405 static struct cleanup *
14406 prepare_re_set_context (struct breakpoint *b)
14407 {
14408 struct cleanup *cleanups;
14409
14410 input_radix = b->input_radix;
14411 cleanups = save_current_space_and_thread ();
14412 if (b->pspace != NULL)
14413 switch_to_program_space_and_thread (b->pspace);
14414 set_language (b->language);
14415
14416 return cleanups;
14417 }
14418
14419 /* Reset a breakpoint given it's struct breakpoint * BINT.
14420 The value we return ends up being the return value from catch_errors.
14421 Unused in this case. */
14422
14423 static int
14424 breakpoint_re_set_one (void *bint)
14425 {
14426 /* Get past catch_errs. */
14427 struct breakpoint *b = (struct breakpoint *) bint;
14428 struct cleanup *cleanups;
14429
14430 cleanups = prepare_re_set_context (b);
14431 b->ops->re_set (b);
14432 do_cleanups (cleanups);
14433 return 0;
14434 }
14435
14436 /* Re-set all breakpoints after symbols have been re-loaded. */
14437 void
14438 breakpoint_re_set (void)
14439 {
14440 struct breakpoint *b, *b_tmp;
14441 enum language save_language;
14442 int save_input_radix;
14443 struct cleanup *old_chain;
14444
14445 save_language = current_language->la_language;
14446 save_input_radix = input_radix;
14447 old_chain = save_current_program_space ();
14448
14449 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14450 {
14451 /* Format possible error msg. */
14452 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
14453 b->number);
14454 struct cleanup *cleanups = make_cleanup (xfree, message);
14455 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
14456 do_cleanups (cleanups);
14457 }
14458 set_language (save_language);
14459 input_radix = save_input_radix;
14460
14461 jit_breakpoint_re_set ();
14462
14463 do_cleanups (old_chain);
14464
14465 create_overlay_event_breakpoint ();
14466 create_longjmp_master_breakpoint ();
14467 create_std_terminate_master_breakpoint ();
14468 create_exception_master_breakpoint ();
14469 }
14470 \f
14471 /* Reset the thread number of this breakpoint:
14472
14473 - If the breakpoint is for all threads, leave it as-is.
14474 - Else, reset it to the current thread for inferior_ptid. */
14475 void
14476 breakpoint_re_set_thread (struct breakpoint *b)
14477 {
14478 if (b->thread != -1)
14479 {
14480 if (in_thread_list (inferior_ptid))
14481 b->thread = pid_to_thread_id (inferior_ptid);
14482
14483 /* We're being called after following a fork. The new fork is
14484 selected as current, and unless this was a vfork will have a
14485 different program space from the original thread. Reset that
14486 as well. */
14487 b->loc->pspace = current_program_space;
14488 }
14489 }
14490
14491 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14492 If from_tty is nonzero, it prints a message to that effect,
14493 which ends with a period (no newline). */
14494
14495 void
14496 set_ignore_count (int bptnum, int count, int from_tty)
14497 {
14498 struct breakpoint *b;
14499
14500 if (count < 0)
14501 count = 0;
14502
14503 ALL_BREAKPOINTS (b)
14504 if (b->number == bptnum)
14505 {
14506 if (is_tracepoint (b))
14507 {
14508 if (from_tty && count != 0)
14509 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14510 bptnum);
14511 return;
14512 }
14513
14514 b->ignore_count = count;
14515 if (from_tty)
14516 {
14517 if (count == 0)
14518 printf_filtered (_("Will stop next time "
14519 "breakpoint %d is reached."),
14520 bptnum);
14521 else if (count == 1)
14522 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
14523 bptnum);
14524 else
14525 printf_filtered (_("Will ignore next %d "
14526 "crossings of breakpoint %d."),
14527 count, bptnum);
14528 }
14529 observer_notify_breakpoint_modified (b);
14530 return;
14531 }
14532
14533 error (_("No breakpoint number %d."), bptnum);
14534 }
14535
14536 /* Command to set ignore-count of breakpoint N to COUNT. */
14537
14538 static void
14539 ignore_command (char *args, int from_tty)
14540 {
14541 char *p = args;
14542 int num;
14543
14544 if (p == 0)
14545 error_no_arg (_("a breakpoint number"));
14546
14547 num = get_number (&p);
14548 if (num == 0)
14549 error (_("bad breakpoint number: '%s'"), args);
14550 if (*p == 0)
14551 error (_("Second argument (specified ignore-count) is missing."));
14552
14553 set_ignore_count (num,
14554 longest_to_int (value_as_long (parse_and_eval (p))),
14555 from_tty);
14556 if (from_tty)
14557 printf_filtered ("\n");
14558 }
14559 \f
14560 /* Call FUNCTION on each of the breakpoints
14561 whose numbers are given in ARGS. */
14562
14563 static void
14564 map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
14565 void *),
14566 void *data)
14567 {
14568 int num;
14569 struct breakpoint *b, *tmp;
14570 int match;
14571 struct get_number_or_range_state state;
14572
14573 if (args == 0)
14574 error_no_arg (_("one or more breakpoint numbers"));
14575
14576 init_number_or_range (&state, args);
14577
14578 while (!state.finished)
14579 {
14580 char *p = state.string;
14581
14582 match = 0;
14583
14584 num = get_number_or_range (&state);
14585 if (num == 0)
14586 {
14587 warning (_("bad breakpoint number at or near '%s'"), p);
14588 }
14589 else
14590 {
14591 ALL_BREAKPOINTS_SAFE (b, tmp)
14592 if (b->number == num)
14593 {
14594 match = 1;
14595 function (b, data);
14596 break;
14597 }
14598 if (match == 0)
14599 printf_unfiltered (_("No breakpoint number %d.\n"), num);
14600 }
14601 }
14602 }
14603
14604 static struct bp_location *
14605 find_location_by_number (char *number)
14606 {
14607 char *dot = strchr (number, '.');
14608 char *p1;
14609 int bp_num;
14610 int loc_num;
14611 struct breakpoint *b;
14612 struct bp_location *loc;
14613
14614 *dot = '\0';
14615
14616 p1 = number;
14617 bp_num = get_number (&p1);
14618 if (bp_num == 0)
14619 error (_("Bad breakpoint number '%s'"), number);
14620
14621 ALL_BREAKPOINTS (b)
14622 if (b->number == bp_num)
14623 {
14624 break;
14625 }
14626
14627 if (!b || b->number != bp_num)
14628 error (_("Bad breakpoint number '%s'"), number);
14629
14630 p1 = dot+1;
14631 loc_num = get_number (&p1);
14632 if (loc_num == 0)
14633 error (_("Bad breakpoint location number '%s'"), number);
14634
14635 --loc_num;
14636 loc = b->loc;
14637 for (;loc_num && loc; --loc_num, loc = loc->next)
14638 ;
14639 if (!loc)
14640 error (_("Bad breakpoint location number '%s'"), dot+1);
14641
14642 return loc;
14643 }
14644
14645
14646 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14647 If from_tty is nonzero, it prints a message to that effect,
14648 which ends with a period (no newline). */
14649
14650 void
14651 disable_breakpoint (struct breakpoint *bpt)
14652 {
14653 /* Never disable a watchpoint scope breakpoint; we want to
14654 hit them when we leave scope so we can delete both the
14655 watchpoint and its scope breakpoint at that time. */
14656 if (bpt->type == bp_watchpoint_scope)
14657 return;
14658
14659 /* You can't disable permanent breakpoints. */
14660 if (bpt->enable_state == bp_permanent)
14661 return;
14662
14663 bpt->enable_state = bp_disabled;
14664
14665 /* Mark breakpoint locations modified. */
14666 mark_breakpoint_modified (bpt);
14667
14668 if (target_supports_enable_disable_tracepoint ()
14669 && current_trace_status ()->running && is_tracepoint (bpt))
14670 {
14671 struct bp_location *location;
14672
14673 for (location = bpt->loc; location; location = location->next)
14674 target_disable_tracepoint (location);
14675 }
14676
14677 update_global_location_list (0);
14678
14679 observer_notify_breakpoint_modified (bpt);
14680 }
14681
14682 /* A callback for iterate_over_related_breakpoints. */
14683
14684 static void
14685 do_disable_breakpoint (struct breakpoint *b, void *ignore)
14686 {
14687 disable_breakpoint (b);
14688 }
14689
14690 /* A callback for map_breakpoint_numbers that calls
14691 disable_breakpoint. */
14692
14693 static void
14694 do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
14695 {
14696 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
14697 }
14698
14699 static void
14700 disable_command (char *args, int from_tty)
14701 {
14702 if (args == 0)
14703 {
14704 struct breakpoint *bpt;
14705
14706 ALL_BREAKPOINTS (bpt)
14707 if (user_breakpoint_p (bpt))
14708 disable_breakpoint (bpt);
14709 }
14710 else if (strchr (args, '.'))
14711 {
14712 struct bp_location *loc = find_location_by_number (args);
14713 if (loc)
14714 {
14715 if (loc->enabled)
14716 {
14717 loc->enabled = 0;
14718 mark_breakpoint_location_modified (loc);
14719 }
14720 if (target_supports_enable_disable_tracepoint ()
14721 && current_trace_status ()->running && loc->owner
14722 && is_tracepoint (loc->owner))
14723 target_disable_tracepoint (loc);
14724 }
14725 update_global_location_list (0);
14726 }
14727 else
14728 map_breakpoint_numbers (args, do_map_disable_breakpoint, NULL);
14729 }
14730
14731 static void
14732 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14733 int count)
14734 {
14735 int target_resources_ok;
14736
14737 if (bpt->type == bp_hardware_breakpoint)
14738 {
14739 int i;
14740 i = hw_breakpoint_used_count ();
14741 target_resources_ok =
14742 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
14743 i + 1, 0);
14744 if (target_resources_ok == 0)
14745 error (_("No hardware breakpoint support in the target."));
14746 else if (target_resources_ok < 0)
14747 error (_("Hardware breakpoints used exceeds limit."));
14748 }
14749
14750 if (is_watchpoint (bpt))
14751 {
14752 /* Initialize it just to avoid a GCC false warning. */
14753 enum enable_state orig_enable_state = 0;
14754 volatile struct gdb_exception e;
14755
14756 TRY_CATCH (e, RETURN_MASK_ALL)
14757 {
14758 struct watchpoint *w = (struct watchpoint *) bpt;
14759
14760 orig_enable_state = bpt->enable_state;
14761 bpt->enable_state = bp_enabled;
14762 update_watchpoint (w, 1 /* reparse */);
14763 }
14764 if (e.reason < 0)
14765 {
14766 bpt->enable_state = orig_enable_state;
14767 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14768 bpt->number);
14769 return;
14770 }
14771 }
14772
14773 if (bpt->enable_state != bp_permanent)
14774 bpt->enable_state = bp_enabled;
14775
14776 bpt->enable_state = bp_enabled;
14777
14778 /* Mark breakpoint locations modified. */
14779 mark_breakpoint_modified (bpt);
14780
14781 if (target_supports_enable_disable_tracepoint ()
14782 && current_trace_status ()->running && is_tracepoint (bpt))
14783 {
14784 struct bp_location *location;
14785
14786 for (location = bpt->loc; location; location = location->next)
14787 target_enable_tracepoint (location);
14788 }
14789
14790 bpt->disposition = disposition;
14791 bpt->enable_count = count;
14792 update_global_location_list (1);
14793
14794 observer_notify_breakpoint_modified (bpt);
14795 }
14796
14797
14798 void
14799 enable_breakpoint (struct breakpoint *bpt)
14800 {
14801 enable_breakpoint_disp (bpt, bpt->disposition, 0);
14802 }
14803
14804 static void
14805 do_enable_breakpoint (struct breakpoint *bpt, void *arg)
14806 {
14807 enable_breakpoint (bpt);
14808 }
14809
14810 /* A callback for map_breakpoint_numbers that calls
14811 enable_breakpoint. */
14812
14813 static void
14814 do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
14815 {
14816 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
14817 }
14818
14819 /* The enable command enables the specified breakpoints (or all defined
14820 breakpoints) so they once again become (or continue to be) effective
14821 in stopping the inferior. */
14822
14823 static void
14824 enable_command (char *args, int from_tty)
14825 {
14826 if (args == 0)
14827 {
14828 struct breakpoint *bpt;
14829
14830 ALL_BREAKPOINTS (bpt)
14831 if (user_breakpoint_p (bpt))
14832 enable_breakpoint (bpt);
14833 }
14834 else if (strchr (args, '.'))
14835 {
14836 struct bp_location *loc = find_location_by_number (args);
14837 if (loc)
14838 {
14839 if (!loc->enabled)
14840 {
14841 loc->enabled = 1;
14842 mark_breakpoint_location_modified (loc);
14843 }
14844 if (target_supports_enable_disable_tracepoint ()
14845 && current_trace_status ()->running && loc->owner
14846 && is_tracepoint (loc->owner))
14847 target_enable_tracepoint (loc);
14848 }
14849 update_global_location_list (1);
14850 }
14851 else
14852 map_breakpoint_numbers (args, do_map_enable_breakpoint, NULL);
14853 }
14854
14855 /* This struct packages up disposition data for application to multiple
14856 breakpoints. */
14857
14858 struct disp_data
14859 {
14860 enum bpdisp disp;
14861 int count;
14862 };
14863
14864 static void
14865 do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
14866 {
14867 struct disp_data disp_data = *(struct disp_data *) arg;
14868
14869 enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
14870 }
14871
14872 static void
14873 do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
14874 {
14875 struct disp_data disp = { disp_disable, 1 };
14876
14877 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14878 }
14879
14880 static void
14881 enable_once_command (char *args, int from_tty)
14882 {
14883 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
14884 }
14885
14886 static void
14887 do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
14888 {
14889 struct disp_data disp = { disp_disable, *(int *) countptr };
14890
14891 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14892 }
14893
14894 static void
14895 enable_count_command (char *args, int from_tty)
14896 {
14897 int count = get_number (&args);
14898
14899 map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
14900 }
14901
14902 static void
14903 do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
14904 {
14905 struct disp_data disp = { disp_del, 1 };
14906
14907 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14908 }
14909
14910 static void
14911 enable_delete_command (char *args, int from_tty)
14912 {
14913 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
14914 }
14915 \f
14916 static void
14917 set_breakpoint_cmd (char *args, int from_tty)
14918 {
14919 }
14920
14921 static void
14922 show_breakpoint_cmd (char *args, int from_tty)
14923 {
14924 }
14925
14926 /* Invalidate last known value of any hardware watchpoint if
14927 the memory which that value represents has been written to by
14928 GDB itself. */
14929
14930 static void
14931 invalidate_bp_value_on_memory_change (struct inferior *inferior,
14932 CORE_ADDR addr, ssize_t len,
14933 const bfd_byte *data)
14934 {
14935 struct breakpoint *bp;
14936
14937 ALL_BREAKPOINTS (bp)
14938 if (bp->enable_state == bp_enabled
14939 && bp->type == bp_hardware_watchpoint)
14940 {
14941 struct watchpoint *wp = (struct watchpoint *) bp;
14942
14943 if (wp->val_valid && wp->val)
14944 {
14945 struct bp_location *loc;
14946
14947 for (loc = bp->loc; loc != NULL; loc = loc->next)
14948 if (loc->loc_type == bp_loc_hardware_watchpoint
14949 && loc->address + loc->length > addr
14950 && addr + len > loc->address)
14951 {
14952 value_free (wp->val);
14953 wp->val = NULL;
14954 wp->val_valid = 0;
14955 }
14956 }
14957 }
14958 }
14959
14960 /* Create and insert a raw software breakpoint at PC. Return an
14961 identifier, which should be used to remove the breakpoint later.
14962 In general, places which call this should be using something on the
14963 breakpoint chain instead; this function should be eliminated
14964 someday. */
14965
14966 void *
14967 deprecated_insert_raw_breakpoint (struct gdbarch *gdbarch,
14968 struct address_space *aspace, CORE_ADDR pc)
14969 {
14970 struct bp_target_info *bp_tgt;
14971
14972 bp_tgt = XZALLOC (struct bp_target_info);
14973
14974 bp_tgt->placed_address_space = aspace;
14975 bp_tgt->placed_address = pc;
14976
14977 if (target_insert_breakpoint (gdbarch, bp_tgt) != 0)
14978 {
14979 /* Could not insert the breakpoint. */
14980 xfree (bp_tgt);
14981 return NULL;
14982 }
14983
14984 return bp_tgt;
14985 }
14986
14987 /* Remove a breakpoint BP inserted by
14988 deprecated_insert_raw_breakpoint. */
14989
14990 int
14991 deprecated_remove_raw_breakpoint (struct gdbarch *gdbarch, void *bp)
14992 {
14993 struct bp_target_info *bp_tgt = bp;
14994 int ret;
14995
14996 ret = target_remove_breakpoint (gdbarch, bp_tgt);
14997 xfree (bp_tgt);
14998
14999 return ret;
15000 }
15001
15002 /* One (or perhaps two) breakpoints used for software single
15003 stepping. */
15004
15005 static void *single_step_breakpoints[2];
15006 static struct gdbarch *single_step_gdbarch[2];
15007
15008 /* Create and insert a breakpoint for software single step. */
15009
15010 void
15011 insert_single_step_breakpoint (struct gdbarch *gdbarch,
15012 struct address_space *aspace,
15013 CORE_ADDR next_pc)
15014 {
15015 void **bpt_p;
15016
15017 if (single_step_breakpoints[0] == NULL)
15018 {
15019 bpt_p = &single_step_breakpoints[0];
15020 single_step_gdbarch[0] = gdbarch;
15021 }
15022 else
15023 {
15024 gdb_assert (single_step_breakpoints[1] == NULL);
15025 bpt_p = &single_step_breakpoints[1];
15026 single_step_gdbarch[1] = gdbarch;
15027 }
15028
15029 /* NOTE drow/2006-04-11: A future improvement to this function would
15030 be to only create the breakpoints once, and actually put them on
15031 the breakpoint chain. That would let us use set_raw_breakpoint.
15032 We could adjust the addresses each time they were needed. Doing
15033 this requires corresponding changes elsewhere where single step
15034 breakpoints are handled, however. So, for now, we use this. */
15035
15036 *bpt_p = deprecated_insert_raw_breakpoint (gdbarch, aspace, next_pc);
15037 if (*bpt_p == NULL)
15038 error (_("Could not insert single-step breakpoint at %s"),
15039 paddress (gdbarch, next_pc));
15040 }
15041
15042 /* Check if the breakpoints used for software single stepping
15043 were inserted or not. */
15044
15045 int
15046 single_step_breakpoints_inserted (void)
15047 {
15048 return (single_step_breakpoints[0] != NULL
15049 || single_step_breakpoints[1] != NULL);
15050 }
15051
15052 /* Remove and delete any breakpoints used for software single step. */
15053
15054 void
15055 remove_single_step_breakpoints (void)
15056 {
15057 gdb_assert (single_step_breakpoints[0] != NULL);
15058
15059 /* See insert_single_step_breakpoint for more about this deprecated
15060 call. */
15061 deprecated_remove_raw_breakpoint (single_step_gdbarch[0],
15062 single_step_breakpoints[0]);
15063 single_step_gdbarch[0] = NULL;
15064 single_step_breakpoints[0] = NULL;
15065
15066 if (single_step_breakpoints[1] != NULL)
15067 {
15068 deprecated_remove_raw_breakpoint (single_step_gdbarch[1],
15069 single_step_breakpoints[1]);
15070 single_step_gdbarch[1] = NULL;
15071 single_step_breakpoints[1] = NULL;
15072 }
15073 }
15074
15075 /* Delete software single step breakpoints without removing them from
15076 the inferior. This is intended to be used if the inferior's address
15077 space where they were inserted is already gone, e.g. after exit or
15078 exec. */
15079
15080 void
15081 cancel_single_step_breakpoints (void)
15082 {
15083 int i;
15084
15085 for (i = 0; i < 2; i++)
15086 if (single_step_breakpoints[i])
15087 {
15088 xfree (single_step_breakpoints[i]);
15089 single_step_breakpoints[i] = NULL;
15090 single_step_gdbarch[i] = NULL;
15091 }
15092 }
15093
15094 /* Detach software single-step breakpoints from INFERIOR_PTID without
15095 removing them. */
15096
15097 static void
15098 detach_single_step_breakpoints (void)
15099 {
15100 int i;
15101
15102 for (i = 0; i < 2; i++)
15103 if (single_step_breakpoints[i])
15104 target_remove_breakpoint (single_step_gdbarch[i],
15105 single_step_breakpoints[i]);
15106 }
15107
15108 /* Check whether a software single-step breakpoint is inserted at
15109 PC. */
15110
15111 static int
15112 single_step_breakpoint_inserted_here_p (struct address_space *aspace,
15113 CORE_ADDR pc)
15114 {
15115 int i;
15116
15117 for (i = 0; i < 2; i++)
15118 {
15119 struct bp_target_info *bp_tgt = single_step_breakpoints[i];
15120 if (bp_tgt
15121 && breakpoint_address_match (bp_tgt->placed_address_space,
15122 bp_tgt->placed_address,
15123 aspace, pc))
15124 return 1;
15125 }
15126
15127 return 0;
15128 }
15129
15130 /* Returns 0 if 'bp' is NOT a syscall catchpoint,
15131 non-zero otherwise. */
15132 static int
15133 is_syscall_catchpoint_enabled (struct breakpoint *bp)
15134 {
15135 if (syscall_catchpoint_p (bp)
15136 && bp->enable_state != bp_disabled
15137 && bp->enable_state != bp_call_disabled)
15138 return 1;
15139 else
15140 return 0;
15141 }
15142
15143 int
15144 catch_syscall_enabled (void)
15145 {
15146 struct catch_syscall_inferior_data *inf_data
15147 = get_catch_syscall_inferior_data (current_inferior ());
15148
15149 return inf_data->total_syscalls_count != 0;
15150 }
15151
15152 int
15153 catching_syscall_number (int syscall_number)
15154 {
15155 struct breakpoint *bp;
15156
15157 ALL_BREAKPOINTS (bp)
15158 if (is_syscall_catchpoint_enabled (bp))
15159 {
15160 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bp;
15161
15162 if (c->syscalls_to_be_caught)
15163 {
15164 int i, iter;
15165 for (i = 0;
15166 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
15167 i++)
15168 if (syscall_number == iter)
15169 return 1;
15170 }
15171 else
15172 return 1;
15173 }
15174
15175 return 0;
15176 }
15177
15178 /* Complete syscall names. Used by "catch syscall". */
15179 static VEC (char_ptr) *
15180 catch_syscall_completer (struct cmd_list_element *cmd,
15181 const char *text, const char *word)
15182 {
15183 const char **list = get_syscall_names ();
15184 VEC (char_ptr) *retlist
15185 = (list == NULL) ? NULL : complete_on_enum (list, word, word);
15186
15187 xfree (list);
15188 return retlist;
15189 }
15190
15191 /* Tracepoint-specific operations. */
15192
15193 /* Set tracepoint count to NUM. */
15194 static void
15195 set_tracepoint_count (int num)
15196 {
15197 tracepoint_count = num;
15198 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
15199 }
15200
15201 static void
15202 trace_command (char *arg, int from_tty)
15203 {
15204 struct breakpoint_ops *ops;
15205 const char *arg_cp = arg;
15206
15207 if (arg && probe_linespec_to_ops (&arg_cp))
15208 ops = &tracepoint_probe_breakpoint_ops;
15209 else
15210 ops = &tracepoint_breakpoint_ops;
15211
15212 create_breakpoint (get_current_arch (),
15213 arg,
15214 NULL, 0, NULL, 1 /* parse arg */,
15215 0 /* tempflag */,
15216 bp_tracepoint /* type_wanted */,
15217 0 /* Ignore count */,
15218 pending_break_support,
15219 ops,
15220 from_tty,
15221 1 /* enabled */,
15222 0 /* internal */, 0);
15223 }
15224
15225 static void
15226 ftrace_command (char *arg, int from_tty)
15227 {
15228 create_breakpoint (get_current_arch (),
15229 arg,
15230 NULL, 0, NULL, 1 /* parse arg */,
15231 0 /* tempflag */,
15232 bp_fast_tracepoint /* type_wanted */,
15233 0 /* Ignore count */,
15234 pending_break_support,
15235 &tracepoint_breakpoint_ops,
15236 from_tty,
15237 1 /* enabled */,
15238 0 /* internal */, 0);
15239 }
15240
15241 /* strace command implementation. Creates a static tracepoint. */
15242
15243 static void
15244 strace_command (char *arg, int from_tty)
15245 {
15246 struct breakpoint_ops *ops;
15247
15248 /* Decide if we are dealing with a static tracepoint marker (`-m'),
15249 or with a normal static tracepoint. */
15250 if (arg && strncmp (arg, "-m", 2) == 0 && isspace (arg[2]))
15251 ops = &strace_marker_breakpoint_ops;
15252 else
15253 ops = &tracepoint_breakpoint_ops;
15254
15255 create_breakpoint (get_current_arch (),
15256 arg,
15257 NULL, 0, NULL, 1 /* parse arg */,
15258 0 /* tempflag */,
15259 bp_static_tracepoint /* type_wanted */,
15260 0 /* Ignore count */,
15261 pending_break_support,
15262 ops,
15263 from_tty,
15264 1 /* enabled */,
15265 0 /* internal */, 0);
15266 }
15267
15268 /* Set up a fake reader function that gets command lines from a linked
15269 list that was acquired during tracepoint uploading. */
15270
15271 static struct uploaded_tp *this_utp;
15272 static int next_cmd;
15273
15274 static char *
15275 read_uploaded_action (void)
15276 {
15277 char *rslt;
15278
15279 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
15280
15281 next_cmd++;
15282
15283 return rslt;
15284 }
15285
15286 /* Given information about a tracepoint as recorded on a target (which
15287 can be either a live system or a trace file), attempt to create an
15288 equivalent GDB tracepoint. This is not a reliable process, since
15289 the target does not necessarily have all the information used when
15290 the tracepoint was originally defined. */
15291
15292 struct tracepoint *
15293 create_tracepoint_from_upload (struct uploaded_tp *utp)
15294 {
15295 char *addr_str, small_buf[100];
15296 struct tracepoint *tp;
15297
15298 if (utp->at_string)
15299 addr_str = utp->at_string;
15300 else
15301 {
15302 /* In the absence of a source location, fall back to raw
15303 address. Since there is no way to confirm that the address
15304 means the same thing as when the trace was started, warn the
15305 user. */
15306 warning (_("Uploaded tracepoint %d has no "
15307 "source location, using raw address"),
15308 utp->number);
15309 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
15310 addr_str = small_buf;
15311 }
15312
15313 /* There's not much we can do with a sequence of bytecodes. */
15314 if (utp->cond && !utp->cond_string)
15315 warning (_("Uploaded tracepoint %d condition "
15316 "has no source form, ignoring it"),
15317 utp->number);
15318
15319 if (!create_breakpoint (get_current_arch (),
15320 addr_str,
15321 utp->cond_string, -1, NULL,
15322 0 /* parse cond/thread */,
15323 0 /* tempflag */,
15324 utp->type /* type_wanted */,
15325 0 /* Ignore count */,
15326 pending_break_support,
15327 &tracepoint_breakpoint_ops,
15328 0 /* from_tty */,
15329 utp->enabled /* enabled */,
15330 0 /* internal */,
15331 CREATE_BREAKPOINT_FLAGS_INSERTED))
15332 return NULL;
15333
15334 /* Get the tracepoint we just created. */
15335 tp = get_tracepoint (tracepoint_count);
15336 gdb_assert (tp != NULL);
15337
15338 if (utp->pass > 0)
15339 {
15340 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
15341 tp->base.number);
15342
15343 trace_pass_command (small_buf, 0);
15344 }
15345
15346 /* If we have uploaded versions of the original commands, set up a
15347 special-purpose "reader" function and call the usual command line
15348 reader, then pass the result to the breakpoint command-setting
15349 function. */
15350 if (!VEC_empty (char_ptr, utp->cmd_strings))
15351 {
15352 struct command_line *cmd_list;
15353
15354 this_utp = utp;
15355 next_cmd = 0;
15356
15357 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
15358
15359 breakpoint_set_commands (&tp->base, cmd_list);
15360 }
15361 else if (!VEC_empty (char_ptr, utp->actions)
15362 || !VEC_empty (char_ptr, utp->step_actions))
15363 warning (_("Uploaded tracepoint %d actions "
15364 "have no source form, ignoring them"),
15365 utp->number);
15366
15367 /* Copy any status information that might be available. */
15368 tp->base.hit_count = utp->hit_count;
15369 tp->traceframe_usage = utp->traceframe_usage;
15370
15371 return tp;
15372 }
15373
15374 /* Print information on tracepoint number TPNUM_EXP, or all if
15375 omitted. */
15376
15377 static void
15378 tracepoints_info (char *args, int from_tty)
15379 {
15380 struct ui_out *uiout = current_uiout;
15381 int num_printed;
15382
15383 num_printed = breakpoint_1 (args, 0, is_tracepoint);
15384
15385 if (num_printed == 0)
15386 {
15387 if (args == NULL || *args == '\0')
15388 ui_out_message (uiout, 0, "No tracepoints.\n");
15389 else
15390 ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
15391 }
15392
15393 default_collect_info ();
15394 }
15395
15396 /* The 'enable trace' command enables tracepoints.
15397 Not supported by all targets. */
15398 static void
15399 enable_trace_command (char *args, int from_tty)
15400 {
15401 enable_command (args, from_tty);
15402 }
15403
15404 /* The 'disable trace' command disables tracepoints.
15405 Not supported by all targets. */
15406 static void
15407 disable_trace_command (char *args, int from_tty)
15408 {
15409 disable_command (args, from_tty);
15410 }
15411
15412 /* Remove a tracepoint (or all if no argument). */
15413 static void
15414 delete_trace_command (char *arg, int from_tty)
15415 {
15416 struct breakpoint *b, *b_tmp;
15417
15418 dont_repeat ();
15419
15420 if (arg == 0)
15421 {
15422 int breaks_to_delete = 0;
15423
15424 /* Delete all breakpoints if no argument.
15425 Do not delete internal or call-dummy breakpoints, these
15426 have to be deleted with an explicit breakpoint number
15427 argument. */
15428 ALL_TRACEPOINTS (b)
15429 if (is_tracepoint (b) && user_breakpoint_p (b))
15430 {
15431 breaks_to_delete = 1;
15432 break;
15433 }
15434
15435 /* Ask user only if there are some breakpoints to delete. */
15436 if (!from_tty
15437 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
15438 {
15439 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15440 if (is_tracepoint (b) && user_breakpoint_p (b))
15441 delete_breakpoint (b);
15442 }
15443 }
15444 else
15445 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
15446 }
15447
15448 /* Helper function for trace_pass_command. */
15449
15450 static void
15451 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
15452 {
15453 tp->pass_count = count;
15454 observer_notify_breakpoint_modified (&tp->base);
15455 if (from_tty)
15456 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
15457 tp->base.number, count);
15458 }
15459
15460 /* Set passcount for tracepoint.
15461
15462 First command argument is passcount, second is tracepoint number.
15463 If tracepoint number omitted, apply to most recently defined.
15464 Also accepts special argument "all". */
15465
15466 static void
15467 trace_pass_command (char *args, int from_tty)
15468 {
15469 struct tracepoint *t1;
15470 unsigned int count;
15471
15472 if (args == 0 || *args == 0)
15473 error (_("passcount command requires an "
15474 "argument (count + optional TP num)"));
15475
15476 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
15477
15478 args = skip_spaces (args);
15479 if (*args && strncasecmp (args, "all", 3) == 0)
15480 {
15481 struct breakpoint *b;
15482
15483 args += 3; /* Skip special argument "all". */
15484 if (*args)
15485 error (_("Junk at end of arguments."));
15486
15487 ALL_TRACEPOINTS (b)
15488 {
15489 t1 = (struct tracepoint *) b;
15490 trace_pass_set_count (t1, count, from_tty);
15491 }
15492 }
15493 else if (*args == '\0')
15494 {
15495 t1 = get_tracepoint_by_number (&args, NULL, 1);
15496 if (t1)
15497 trace_pass_set_count (t1, count, from_tty);
15498 }
15499 else
15500 {
15501 struct get_number_or_range_state state;
15502
15503 init_number_or_range (&state, args);
15504 while (!state.finished)
15505 {
15506 t1 = get_tracepoint_by_number (&args, &state, 1);
15507 if (t1)
15508 trace_pass_set_count (t1, count, from_tty);
15509 }
15510 }
15511 }
15512
15513 struct tracepoint *
15514 get_tracepoint (int num)
15515 {
15516 struct breakpoint *t;
15517
15518 ALL_TRACEPOINTS (t)
15519 if (t->number == num)
15520 return (struct tracepoint *) t;
15521
15522 return NULL;
15523 }
15524
15525 /* Find the tracepoint with the given target-side number (which may be
15526 different from the tracepoint number after disconnecting and
15527 reconnecting). */
15528
15529 struct tracepoint *
15530 get_tracepoint_by_number_on_target (int num)
15531 {
15532 struct breakpoint *b;
15533
15534 ALL_TRACEPOINTS (b)
15535 {
15536 struct tracepoint *t = (struct tracepoint *) b;
15537
15538 if (t->number_on_target == num)
15539 return t;
15540 }
15541
15542 return NULL;
15543 }
15544
15545 /* Utility: parse a tracepoint number and look it up in the list.
15546 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
15547 If OPTIONAL_P is true, then if the argument is missing, the most
15548 recent tracepoint (tracepoint_count) is returned. */
15549 struct tracepoint *
15550 get_tracepoint_by_number (char **arg,
15551 struct get_number_or_range_state *state,
15552 int optional_p)
15553 {
15554 struct breakpoint *t;
15555 int tpnum;
15556 char *instring = arg == NULL ? NULL : *arg;
15557
15558 if (state)
15559 {
15560 gdb_assert (!state->finished);
15561 tpnum = get_number_or_range (state);
15562 }
15563 else if (arg == NULL || *arg == NULL || ! **arg)
15564 {
15565 if (optional_p)
15566 tpnum = tracepoint_count;
15567 else
15568 error_no_arg (_("tracepoint number"));
15569 }
15570 else
15571 tpnum = get_number (arg);
15572
15573 if (tpnum <= 0)
15574 {
15575 if (instring && *instring)
15576 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15577 instring);
15578 else
15579 printf_filtered (_("Tracepoint argument missing "
15580 "and no previous tracepoint\n"));
15581 return NULL;
15582 }
15583
15584 ALL_TRACEPOINTS (t)
15585 if (t->number == tpnum)
15586 {
15587 return (struct tracepoint *) t;
15588 }
15589
15590 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15591 return NULL;
15592 }
15593
15594 void
15595 print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15596 {
15597 if (b->thread != -1)
15598 fprintf_unfiltered (fp, " thread %d", b->thread);
15599
15600 if (b->task != 0)
15601 fprintf_unfiltered (fp, " task %d", b->task);
15602
15603 fprintf_unfiltered (fp, "\n");
15604 }
15605
15606 /* Save information on user settable breakpoints (watchpoints, etc) to
15607 a new script file named FILENAME. If FILTER is non-NULL, call it
15608 on each breakpoint and only include the ones for which it returns
15609 non-zero. */
15610
15611 static void
15612 save_breakpoints (char *filename, int from_tty,
15613 int (*filter) (const struct breakpoint *))
15614 {
15615 struct breakpoint *tp;
15616 int any = 0;
15617 char *pathname;
15618 struct cleanup *cleanup;
15619 struct ui_file *fp;
15620 int extra_trace_bits = 0;
15621
15622 if (filename == 0 || *filename == 0)
15623 error (_("Argument required (file name in which to save)"));
15624
15625 /* See if we have anything to save. */
15626 ALL_BREAKPOINTS (tp)
15627 {
15628 /* Skip internal and momentary breakpoints. */
15629 if (!user_breakpoint_p (tp))
15630 continue;
15631
15632 /* If we have a filter, only save the breakpoints it accepts. */
15633 if (filter && !filter (tp))
15634 continue;
15635
15636 any = 1;
15637
15638 if (is_tracepoint (tp))
15639 {
15640 extra_trace_bits = 1;
15641
15642 /* We can stop searching. */
15643 break;
15644 }
15645 }
15646
15647 if (!any)
15648 {
15649 warning (_("Nothing to save."));
15650 return;
15651 }
15652
15653 pathname = tilde_expand (filename);
15654 cleanup = make_cleanup (xfree, pathname);
15655 fp = gdb_fopen (pathname, "w");
15656 if (!fp)
15657 error (_("Unable to open file '%s' for saving (%s)"),
15658 filename, safe_strerror (errno));
15659 make_cleanup_ui_file_delete (fp);
15660
15661 if (extra_trace_bits)
15662 save_trace_state_variables (fp);
15663
15664 ALL_BREAKPOINTS (tp)
15665 {
15666 /* Skip internal and momentary breakpoints. */
15667 if (!user_breakpoint_p (tp))
15668 continue;
15669
15670 /* If we have a filter, only save the breakpoints it accepts. */
15671 if (filter && !filter (tp))
15672 continue;
15673
15674 tp->ops->print_recreate (tp, fp);
15675
15676 /* Note, we can't rely on tp->number for anything, as we can't
15677 assume the recreated breakpoint numbers will match. Use $bpnum
15678 instead. */
15679
15680 if (tp->cond_string)
15681 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
15682
15683 if (tp->ignore_count)
15684 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
15685
15686 if (tp->commands)
15687 {
15688 volatile struct gdb_exception ex;
15689
15690 fprintf_unfiltered (fp, " commands\n");
15691
15692 ui_out_redirect (current_uiout, fp);
15693 TRY_CATCH (ex, RETURN_MASK_ALL)
15694 {
15695 print_command_lines (current_uiout, tp->commands->commands, 2);
15696 }
15697 ui_out_redirect (current_uiout, NULL);
15698
15699 if (ex.reason < 0)
15700 throw_exception (ex);
15701
15702 fprintf_unfiltered (fp, " end\n");
15703 }
15704
15705 if (tp->enable_state == bp_disabled)
15706 fprintf_unfiltered (fp, "disable\n");
15707
15708 /* If this is a multi-location breakpoint, check if the locations
15709 should be individually disabled. Watchpoint locations are
15710 special, and not user visible. */
15711 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15712 {
15713 struct bp_location *loc;
15714 int n = 1;
15715
15716 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15717 if (!loc->enabled)
15718 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
15719 }
15720 }
15721
15722 if (extra_trace_bits && *default_collect)
15723 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
15724
15725 do_cleanups (cleanup);
15726 if (from_tty)
15727 printf_filtered (_("Saved to file '%s'.\n"), filename);
15728 }
15729
15730 /* The `save breakpoints' command. */
15731
15732 static void
15733 save_breakpoints_command (char *args, int from_tty)
15734 {
15735 save_breakpoints (args, from_tty, NULL);
15736 }
15737
15738 /* The `save tracepoints' command. */
15739
15740 static void
15741 save_tracepoints_command (char *args, int from_tty)
15742 {
15743 save_breakpoints (args, from_tty, is_tracepoint);
15744 }
15745
15746 /* Create a vector of all tracepoints. */
15747
15748 VEC(breakpoint_p) *
15749 all_tracepoints (void)
15750 {
15751 VEC(breakpoint_p) *tp_vec = 0;
15752 struct breakpoint *tp;
15753
15754 ALL_TRACEPOINTS (tp)
15755 {
15756 VEC_safe_push (breakpoint_p, tp_vec, tp);
15757 }
15758
15759 return tp_vec;
15760 }
15761
15762 \f
15763 /* This help string is used for the break, hbreak, tbreak and thbreak
15764 commands. It is defined as a macro to prevent duplication.
15765 COMMAND should be a string constant containing the name of the
15766 command. */
15767 #define BREAK_ARGS_HELP(command) \
15768 command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15769 PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15770 probe point. Accepted values are `-probe' (for a generic, automatically\n\
15771 guessed probe type) or `-probe-stap' (for a SystemTap probe).\n\
15772 LOCATION may be a line number, function name, or \"*\" and an address.\n\
15773 If a line number is specified, break at start of code for that line.\n\
15774 If a function is specified, break at start of code for that function.\n\
15775 If an address is specified, break at that exact address.\n\
15776 With no LOCATION, uses current execution address of the selected\n\
15777 stack frame. This is useful for breaking on return to a stack frame.\n\
15778 \n\
15779 THREADNUM is the number from \"info threads\".\n\
15780 CONDITION is a boolean expression.\n\
15781 \n\
15782 Multiple breakpoints at one place are permitted, and useful if their\n\
15783 conditions are different.\n\
15784 \n\
15785 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15786
15787 /* List of subcommands for "catch". */
15788 static struct cmd_list_element *catch_cmdlist;
15789
15790 /* List of subcommands for "tcatch". */
15791 static struct cmd_list_element *tcatch_cmdlist;
15792
15793 void
15794 add_catch_command (char *name, char *docstring,
15795 void (*sfunc) (char *args, int from_tty,
15796 struct cmd_list_element *command),
15797 completer_ftype *completer,
15798 void *user_data_catch,
15799 void *user_data_tcatch)
15800 {
15801 struct cmd_list_element *command;
15802
15803 command = add_cmd (name, class_breakpoint, NULL, docstring,
15804 &catch_cmdlist);
15805 set_cmd_sfunc (command, sfunc);
15806 set_cmd_context (command, user_data_catch);
15807 set_cmd_completer (command, completer);
15808
15809 command = add_cmd (name, class_breakpoint, NULL, docstring,
15810 &tcatch_cmdlist);
15811 set_cmd_sfunc (command, sfunc);
15812 set_cmd_context (command, user_data_tcatch);
15813 set_cmd_completer (command, completer);
15814 }
15815
15816 static void
15817 clear_syscall_counts (struct inferior *inf)
15818 {
15819 struct catch_syscall_inferior_data *inf_data
15820 = get_catch_syscall_inferior_data (inf);
15821
15822 inf_data->total_syscalls_count = 0;
15823 inf_data->any_syscall_count = 0;
15824 VEC_free (int, inf_data->syscalls_counts);
15825 }
15826
15827 static void
15828 save_command (char *arg, int from_tty)
15829 {
15830 printf_unfiltered (_("\"save\" must be followed by "
15831 "the name of a save subcommand.\n"));
15832 help_list (save_cmdlist, "save ", -1, gdb_stdout);
15833 }
15834
15835 struct breakpoint *
15836 iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15837 void *data)
15838 {
15839 struct breakpoint *b, *b_tmp;
15840
15841 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15842 {
15843 if ((*callback) (b, data))
15844 return b;
15845 }
15846
15847 return NULL;
15848 }
15849
15850 /* Zero if any of the breakpoint's locations could be a location where
15851 functions have been inlined, nonzero otherwise. */
15852
15853 static int
15854 is_non_inline_function (struct breakpoint *b)
15855 {
15856 /* The shared library event breakpoint is set on the address of a
15857 non-inline function. */
15858 if (b->type == bp_shlib_event)
15859 return 1;
15860
15861 return 0;
15862 }
15863
15864 /* Nonzero if the specified PC cannot be a location where functions
15865 have been inlined. */
15866
15867 int
15868 pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
15869 const struct target_waitstatus *ws)
15870 {
15871 struct breakpoint *b;
15872 struct bp_location *bl;
15873
15874 ALL_BREAKPOINTS (b)
15875 {
15876 if (!is_non_inline_function (b))
15877 continue;
15878
15879 for (bl = b->loc; bl != NULL; bl = bl->next)
15880 {
15881 if (!bl->shlib_disabled
15882 && bpstat_check_location (bl, aspace, pc, ws))
15883 return 1;
15884 }
15885 }
15886
15887 return 0;
15888 }
15889
15890 /* Remove any references to OBJFILE which is going to be freed. */
15891
15892 void
15893 breakpoint_free_objfile (struct objfile *objfile)
15894 {
15895 struct bp_location **locp, *loc;
15896
15897 ALL_BP_LOCATIONS (loc, locp)
15898 if (loc->symtab != NULL && loc->symtab->objfile == objfile)
15899 loc->symtab = NULL;
15900 }
15901
15902 void
15903 initialize_breakpoint_ops (void)
15904 {
15905 static int initialized = 0;
15906
15907 struct breakpoint_ops *ops;
15908
15909 if (initialized)
15910 return;
15911 initialized = 1;
15912
15913 /* The breakpoint_ops structure to be inherit by all kinds of
15914 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15915 internal and momentary breakpoints, etc.). */
15916 ops = &bkpt_base_breakpoint_ops;
15917 *ops = base_breakpoint_ops;
15918 ops->re_set = bkpt_re_set;
15919 ops->insert_location = bkpt_insert_location;
15920 ops->remove_location = bkpt_remove_location;
15921 ops->breakpoint_hit = bkpt_breakpoint_hit;
15922 ops->create_sals_from_address = bkpt_create_sals_from_address;
15923 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
15924 ops->decode_linespec = bkpt_decode_linespec;
15925
15926 /* The breakpoint_ops structure to be used in regular breakpoints. */
15927 ops = &bkpt_breakpoint_ops;
15928 *ops = bkpt_base_breakpoint_ops;
15929 ops->re_set = bkpt_re_set;
15930 ops->resources_needed = bkpt_resources_needed;
15931 ops->print_it = bkpt_print_it;
15932 ops->print_mention = bkpt_print_mention;
15933 ops->print_recreate = bkpt_print_recreate;
15934
15935 /* Ranged breakpoints. */
15936 ops = &ranged_breakpoint_ops;
15937 *ops = bkpt_breakpoint_ops;
15938 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15939 ops->resources_needed = resources_needed_ranged_breakpoint;
15940 ops->print_it = print_it_ranged_breakpoint;
15941 ops->print_one = print_one_ranged_breakpoint;
15942 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15943 ops->print_mention = print_mention_ranged_breakpoint;
15944 ops->print_recreate = print_recreate_ranged_breakpoint;
15945
15946 /* Internal breakpoints. */
15947 ops = &internal_breakpoint_ops;
15948 *ops = bkpt_base_breakpoint_ops;
15949 ops->re_set = internal_bkpt_re_set;
15950 ops->check_status = internal_bkpt_check_status;
15951 ops->print_it = internal_bkpt_print_it;
15952 ops->print_mention = internal_bkpt_print_mention;
15953
15954 /* Momentary breakpoints. */
15955 ops = &momentary_breakpoint_ops;
15956 *ops = bkpt_base_breakpoint_ops;
15957 ops->re_set = momentary_bkpt_re_set;
15958 ops->check_status = momentary_bkpt_check_status;
15959 ops->print_it = momentary_bkpt_print_it;
15960 ops->print_mention = momentary_bkpt_print_mention;
15961
15962 /* Momentary breakpoints for bp_longjmp and bp_exception. */
15963 ops = &longjmp_breakpoint_ops;
15964 *ops = momentary_breakpoint_ops;
15965 ops->dtor = longjmp_bkpt_dtor;
15966
15967 /* Probe breakpoints. */
15968 ops = &bkpt_probe_breakpoint_ops;
15969 *ops = bkpt_breakpoint_ops;
15970 ops->insert_location = bkpt_probe_insert_location;
15971 ops->remove_location = bkpt_probe_remove_location;
15972 ops->create_sals_from_address = bkpt_probe_create_sals_from_address;
15973 ops->decode_linespec = bkpt_probe_decode_linespec;
15974
15975 /* GNU v3 exception catchpoints. */
15976 ops = &gnu_v3_exception_catchpoint_ops;
15977 *ops = bkpt_breakpoint_ops;
15978 ops->print_it = print_it_exception_catchpoint;
15979 ops->print_one = print_one_exception_catchpoint;
15980 ops->print_mention = print_mention_exception_catchpoint;
15981 ops->print_recreate = print_recreate_exception_catchpoint;
15982
15983 /* Watchpoints. */
15984 ops = &watchpoint_breakpoint_ops;
15985 *ops = base_breakpoint_ops;
15986 ops->dtor = dtor_watchpoint;
15987 ops->re_set = re_set_watchpoint;
15988 ops->insert_location = insert_watchpoint;
15989 ops->remove_location = remove_watchpoint;
15990 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15991 ops->check_status = check_status_watchpoint;
15992 ops->resources_needed = resources_needed_watchpoint;
15993 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15994 ops->print_it = print_it_watchpoint;
15995 ops->print_mention = print_mention_watchpoint;
15996 ops->print_recreate = print_recreate_watchpoint;
15997
15998 /* Masked watchpoints. */
15999 ops = &masked_watchpoint_breakpoint_ops;
16000 *ops = watchpoint_breakpoint_ops;
16001 ops->insert_location = insert_masked_watchpoint;
16002 ops->remove_location = remove_masked_watchpoint;
16003 ops->resources_needed = resources_needed_masked_watchpoint;
16004 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
16005 ops->print_it = print_it_masked_watchpoint;
16006 ops->print_one_detail = print_one_detail_masked_watchpoint;
16007 ops->print_mention = print_mention_masked_watchpoint;
16008 ops->print_recreate = print_recreate_masked_watchpoint;
16009
16010 /* Tracepoints. */
16011 ops = &tracepoint_breakpoint_ops;
16012 *ops = base_breakpoint_ops;
16013 ops->re_set = tracepoint_re_set;
16014 ops->breakpoint_hit = tracepoint_breakpoint_hit;
16015 ops->print_one_detail = tracepoint_print_one_detail;
16016 ops->print_mention = tracepoint_print_mention;
16017 ops->print_recreate = tracepoint_print_recreate;
16018 ops->create_sals_from_address = tracepoint_create_sals_from_address;
16019 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
16020 ops->decode_linespec = tracepoint_decode_linespec;
16021
16022 /* Probe tracepoints. */
16023 ops = &tracepoint_probe_breakpoint_ops;
16024 *ops = tracepoint_breakpoint_ops;
16025 ops->create_sals_from_address = tracepoint_probe_create_sals_from_address;
16026 ops->decode_linespec = tracepoint_probe_decode_linespec;
16027
16028 /* Static tracepoints with marker (`-m'). */
16029 ops = &strace_marker_breakpoint_ops;
16030 *ops = tracepoint_breakpoint_ops;
16031 ops->create_sals_from_address = strace_marker_create_sals_from_address;
16032 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
16033 ops->decode_linespec = strace_marker_decode_linespec;
16034
16035 /* Fork catchpoints. */
16036 ops = &catch_fork_breakpoint_ops;
16037 *ops = base_breakpoint_ops;
16038 ops->insert_location = insert_catch_fork;
16039 ops->remove_location = remove_catch_fork;
16040 ops->breakpoint_hit = breakpoint_hit_catch_fork;
16041 ops->print_it = print_it_catch_fork;
16042 ops->print_one = print_one_catch_fork;
16043 ops->print_mention = print_mention_catch_fork;
16044 ops->print_recreate = print_recreate_catch_fork;
16045
16046 /* Vfork catchpoints. */
16047 ops = &catch_vfork_breakpoint_ops;
16048 *ops = base_breakpoint_ops;
16049 ops->insert_location = insert_catch_vfork;
16050 ops->remove_location = remove_catch_vfork;
16051 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
16052 ops->print_it = print_it_catch_vfork;
16053 ops->print_one = print_one_catch_vfork;
16054 ops->print_mention = print_mention_catch_vfork;
16055 ops->print_recreate = print_recreate_catch_vfork;
16056
16057 /* Exec catchpoints. */
16058 ops = &catch_exec_breakpoint_ops;
16059 *ops = base_breakpoint_ops;
16060 ops->dtor = dtor_catch_exec;
16061 ops->insert_location = insert_catch_exec;
16062 ops->remove_location = remove_catch_exec;
16063 ops->breakpoint_hit = breakpoint_hit_catch_exec;
16064 ops->print_it = print_it_catch_exec;
16065 ops->print_one = print_one_catch_exec;
16066 ops->print_mention = print_mention_catch_exec;
16067 ops->print_recreate = print_recreate_catch_exec;
16068
16069 /* Syscall catchpoints. */
16070 ops = &catch_syscall_breakpoint_ops;
16071 *ops = base_breakpoint_ops;
16072 ops->dtor = dtor_catch_syscall;
16073 ops->insert_location = insert_catch_syscall;
16074 ops->remove_location = remove_catch_syscall;
16075 ops->breakpoint_hit = breakpoint_hit_catch_syscall;
16076 ops->print_it = print_it_catch_syscall;
16077 ops->print_one = print_one_catch_syscall;
16078 ops->print_mention = print_mention_catch_syscall;
16079 ops->print_recreate = print_recreate_catch_syscall;
16080
16081 /* Solib-related catchpoints. */
16082 ops = &catch_solib_breakpoint_ops;
16083 *ops = base_breakpoint_ops;
16084 ops->dtor = dtor_catch_solib;
16085 ops->insert_location = insert_catch_solib;
16086 ops->remove_location = remove_catch_solib;
16087 ops->breakpoint_hit = breakpoint_hit_catch_solib;
16088 ops->check_status = check_status_catch_solib;
16089 ops->print_it = print_it_catch_solib;
16090 ops->print_one = print_one_catch_solib;
16091 ops->print_mention = print_mention_catch_solib;
16092 ops->print_recreate = print_recreate_catch_solib;
16093
16094 ops = &dprintf_breakpoint_ops;
16095 *ops = bkpt_base_breakpoint_ops;
16096 ops->re_set = dprintf_re_set;
16097 ops->resources_needed = bkpt_resources_needed;
16098 ops->print_it = bkpt_print_it;
16099 ops->print_mention = bkpt_print_mention;
16100 ops->print_recreate = bkpt_print_recreate;
16101 }
16102
16103 /* Chain containing all defined "enable breakpoint" subcommands. */
16104
16105 static struct cmd_list_element *enablebreaklist = NULL;
16106
16107 void
16108 _initialize_breakpoint (void)
16109 {
16110 struct cmd_list_element *c;
16111
16112 initialize_breakpoint_ops ();
16113
16114 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
16115 observer_attach_inferior_exit (clear_syscall_counts);
16116 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
16117
16118 breakpoint_objfile_key
16119 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
16120
16121 catch_syscall_inferior_data
16122 = register_inferior_data_with_cleanup (NULL,
16123 catch_syscall_inferior_data_cleanup);
16124
16125 breakpoint_chain = 0;
16126 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
16127 before a breakpoint is set. */
16128 breakpoint_count = 0;
16129
16130 tracepoint_count = 0;
16131
16132 add_com ("ignore", class_breakpoint, ignore_command, _("\
16133 Set ignore-count of breakpoint number N to COUNT.\n\
16134 Usage is `ignore N COUNT'."));
16135 if (xdb_commands)
16136 add_com_alias ("bc", "ignore", class_breakpoint, 1);
16137
16138 add_com ("commands", class_breakpoint, commands_command, _("\
16139 Set commands to be executed when a breakpoint is hit.\n\
16140 Give breakpoint number as argument after \"commands\".\n\
16141 With no argument, the targeted breakpoint is the last one set.\n\
16142 The commands themselves follow starting on the next line.\n\
16143 Type a line containing \"end\" to indicate the end of them.\n\
16144 Give \"silent\" as the first line to make the breakpoint silent;\n\
16145 then no output is printed when it is hit, except what the commands print."));
16146
16147 c = add_com ("condition", class_breakpoint, condition_command, _("\
16148 Specify breakpoint number N to break only if COND is true.\n\
16149 Usage is `condition N COND', where N is an integer and COND is an\n\
16150 expression to be evaluated whenever breakpoint N is reached."));
16151 set_cmd_completer (c, condition_completer);
16152
16153 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
16154 Set a temporary breakpoint.\n\
16155 Like \"break\" except the breakpoint is only temporary,\n\
16156 so it will be deleted when hit. Equivalent to \"break\" followed\n\
16157 by using \"enable delete\" on the breakpoint number.\n\
16158 \n"
16159 BREAK_ARGS_HELP ("tbreak")));
16160 set_cmd_completer (c, location_completer);
16161
16162 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
16163 Set a hardware assisted breakpoint.\n\
16164 Like \"break\" except the breakpoint requires hardware support,\n\
16165 some target hardware may not have this support.\n\
16166 \n"
16167 BREAK_ARGS_HELP ("hbreak")));
16168 set_cmd_completer (c, location_completer);
16169
16170 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
16171 Set a temporary hardware assisted breakpoint.\n\
16172 Like \"hbreak\" except the breakpoint is only temporary,\n\
16173 so it will be deleted when hit.\n\
16174 \n"
16175 BREAK_ARGS_HELP ("thbreak")));
16176 set_cmd_completer (c, location_completer);
16177
16178 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
16179 Enable some breakpoints.\n\
16180 Give breakpoint numbers (separated by spaces) as arguments.\n\
16181 With no subcommand, breakpoints are enabled until you command otherwise.\n\
16182 This is used to cancel the effect of the \"disable\" command.\n\
16183 With a subcommand you can enable temporarily."),
16184 &enablelist, "enable ", 1, &cmdlist);
16185 if (xdb_commands)
16186 add_com ("ab", class_breakpoint, enable_command, _("\
16187 Enable some breakpoints.\n\
16188 Give breakpoint numbers (separated by spaces) as arguments.\n\
16189 With no subcommand, breakpoints are enabled until you command otherwise.\n\
16190 This is used to cancel the effect of the \"disable\" command.\n\
16191 With a subcommand you can enable temporarily."));
16192
16193 add_com_alias ("en", "enable", class_breakpoint, 1);
16194
16195 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
16196 Enable some breakpoints.\n\
16197 Give breakpoint numbers (separated by spaces) as arguments.\n\
16198 This is used to cancel the effect of the \"disable\" command.\n\
16199 May be abbreviated to simply \"enable\".\n"),
16200 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
16201
16202 add_cmd ("once", no_class, enable_once_command, _("\
16203 Enable breakpoints for one hit. Give breakpoint numbers.\n\
16204 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
16205 &enablebreaklist);
16206
16207 add_cmd ("delete", no_class, enable_delete_command, _("\
16208 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16209 If a breakpoint is hit while enabled in this fashion, it is deleted."),
16210 &enablebreaklist);
16211
16212 add_cmd ("count", no_class, enable_count_command, _("\
16213 Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16214 If a breakpoint is hit while enabled in this fashion,\n\
16215 the count is decremented; when it reaches zero, the breakpoint is disabled."),
16216 &enablebreaklist);
16217
16218 add_cmd ("delete", no_class, enable_delete_command, _("\
16219 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16220 If a breakpoint is hit while enabled in this fashion, it is deleted."),
16221 &enablelist);
16222
16223 add_cmd ("once", no_class, enable_once_command, _("\
16224 Enable breakpoints for one hit. Give breakpoint numbers.\n\
16225 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
16226 &enablelist);
16227
16228 add_cmd ("count", no_class, enable_count_command, _("\
16229 Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16230 If a breakpoint is hit while enabled in this fashion,\n\
16231 the count is decremented; when it reaches zero, the breakpoint is disabled."),
16232 &enablelist);
16233
16234 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
16235 Disable some breakpoints.\n\
16236 Arguments are breakpoint numbers with spaces in between.\n\
16237 To disable all breakpoints, give no argument.\n\
16238 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
16239 &disablelist, "disable ", 1, &cmdlist);
16240 add_com_alias ("dis", "disable", class_breakpoint, 1);
16241 add_com_alias ("disa", "disable", class_breakpoint, 1);
16242 if (xdb_commands)
16243 add_com ("sb", class_breakpoint, disable_command, _("\
16244 Disable some breakpoints.\n\
16245 Arguments are breakpoint numbers with spaces in between.\n\
16246 To disable all breakpoints, give no argument.\n\
16247 A disabled breakpoint is not forgotten, but has no effect until re-enabled."));
16248
16249 add_cmd ("breakpoints", class_alias, disable_command, _("\
16250 Disable some breakpoints.\n\
16251 Arguments are breakpoint numbers with spaces in between.\n\
16252 To disable all breakpoints, give no argument.\n\
16253 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
16254 This command may be abbreviated \"disable\"."),
16255 &disablelist);
16256
16257 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
16258 Delete some breakpoints or auto-display expressions.\n\
16259 Arguments are breakpoint numbers with spaces in between.\n\
16260 To delete all breakpoints, give no argument.\n\
16261 \n\
16262 Also a prefix command for deletion of other GDB objects.\n\
16263 The \"unset\" command is also an alias for \"delete\"."),
16264 &deletelist, "delete ", 1, &cmdlist);
16265 add_com_alias ("d", "delete", class_breakpoint, 1);
16266 add_com_alias ("del", "delete", class_breakpoint, 1);
16267 if (xdb_commands)
16268 add_com ("db", class_breakpoint, delete_command, _("\
16269 Delete some breakpoints.\n\
16270 Arguments are breakpoint numbers with spaces in between.\n\
16271 To delete all breakpoints, give no argument.\n"));
16272
16273 add_cmd ("breakpoints", class_alias, delete_command, _("\
16274 Delete some breakpoints or auto-display expressions.\n\
16275 Arguments are breakpoint numbers with spaces in between.\n\
16276 To delete all breakpoints, give no argument.\n\
16277 This command may be abbreviated \"delete\"."),
16278 &deletelist);
16279
16280 add_com ("clear", class_breakpoint, clear_command, _("\
16281 Clear breakpoint at specified line or function.\n\
16282 Argument may be line number, function name, or \"*\" and an address.\n\
16283 If line number is specified, all breakpoints in that line are cleared.\n\
16284 If function is specified, breakpoints at beginning of function are cleared.\n\
16285 If an address is specified, breakpoints at that address are cleared.\n\
16286 \n\
16287 With no argument, clears all breakpoints in the line that the selected frame\n\
16288 is executing in.\n\
16289 \n\
16290 See also the \"delete\" command which clears breakpoints by number."));
16291 add_com_alias ("cl", "clear", class_breakpoint, 1);
16292
16293 c = add_com ("break", class_breakpoint, break_command, _("\
16294 Set breakpoint at specified line or function.\n"
16295 BREAK_ARGS_HELP ("break")));
16296 set_cmd_completer (c, location_completer);
16297
16298 add_com_alias ("b", "break", class_run, 1);
16299 add_com_alias ("br", "break", class_run, 1);
16300 add_com_alias ("bre", "break", class_run, 1);
16301 add_com_alias ("brea", "break", class_run, 1);
16302
16303 if (xdb_commands)
16304 add_com_alias ("ba", "break", class_breakpoint, 1);
16305
16306 if (dbx_commands)
16307 {
16308 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
16309 Break in function/address or break at a line in the current file."),
16310 &stoplist, "stop ", 1, &cmdlist);
16311 add_cmd ("in", class_breakpoint, stopin_command,
16312 _("Break in function or address."), &stoplist);
16313 add_cmd ("at", class_breakpoint, stopat_command,
16314 _("Break at a line in the current file."), &stoplist);
16315 add_com ("status", class_info, breakpoints_info, _("\
16316 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
16317 The \"Type\" column indicates one of:\n\
16318 \tbreakpoint - normal breakpoint\n\
16319 \twatchpoint - watchpoint\n\
16320 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16321 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16322 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16323 address and file/line number respectively.\n\
16324 \n\
16325 Convenience variable \"$_\" and default examine address for \"x\"\n\
16326 are set to the address of the last breakpoint listed unless the command\n\
16327 is prefixed with \"server \".\n\n\
16328 Convenience variable \"$bpnum\" contains the number of the last\n\
16329 breakpoint set."));
16330 }
16331
16332 add_info ("breakpoints", breakpoints_info, _("\
16333 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
16334 The \"Type\" column indicates one of:\n\
16335 \tbreakpoint - normal breakpoint\n\
16336 \twatchpoint - watchpoint\n\
16337 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16338 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16339 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16340 address and file/line number respectively.\n\
16341 \n\
16342 Convenience variable \"$_\" and default examine address for \"x\"\n\
16343 are set to the address of the last breakpoint listed unless the command\n\
16344 is prefixed with \"server \".\n\n\
16345 Convenience variable \"$bpnum\" contains the number of the last\n\
16346 breakpoint set."));
16347
16348 add_info_alias ("b", "breakpoints", 1);
16349
16350 if (xdb_commands)
16351 add_com ("lb", class_breakpoint, breakpoints_info, _("\
16352 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
16353 The \"Type\" column indicates one of:\n\
16354 \tbreakpoint - normal breakpoint\n\
16355 \twatchpoint - watchpoint\n\
16356 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16357 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16358 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16359 address and file/line number respectively.\n\
16360 \n\
16361 Convenience variable \"$_\" and default examine address for \"x\"\n\
16362 are set to the address of the last breakpoint listed unless the command\n\
16363 is prefixed with \"server \".\n\n\
16364 Convenience variable \"$bpnum\" contains the number of the last\n\
16365 breakpoint set."));
16366
16367 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
16368 Status of all breakpoints, or breakpoint number NUMBER.\n\
16369 The \"Type\" column indicates one of:\n\
16370 \tbreakpoint - normal breakpoint\n\
16371 \twatchpoint - watchpoint\n\
16372 \tlongjmp - internal breakpoint used to step through longjmp()\n\
16373 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
16374 \tuntil - internal breakpoint used by the \"until\" command\n\
16375 \tfinish - internal breakpoint used by the \"finish\" command\n\
16376 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16377 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16378 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16379 address and file/line number respectively.\n\
16380 \n\
16381 Convenience variable \"$_\" and default examine address for \"x\"\n\
16382 are set to the address of the last breakpoint listed unless the command\n\
16383 is prefixed with \"server \".\n\n\
16384 Convenience variable \"$bpnum\" contains the number of the last\n\
16385 breakpoint set."),
16386 &maintenanceinfolist);
16387
16388 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
16389 Set catchpoints to catch events."),
16390 &catch_cmdlist, "catch ",
16391 0/*allow-unknown*/, &cmdlist);
16392
16393 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
16394 Set temporary catchpoints to catch events."),
16395 &tcatch_cmdlist, "tcatch ",
16396 0/*allow-unknown*/, &cmdlist);
16397
16398 /* Add catch and tcatch sub-commands. */
16399 add_catch_command ("catch", _("\
16400 Catch an exception, when caught."),
16401 catch_catch_command,
16402 NULL,
16403 CATCH_PERMANENT,
16404 CATCH_TEMPORARY);
16405 add_catch_command ("throw", _("\
16406 Catch an exception, when thrown."),
16407 catch_throw_command,
16408 NULL,
16409 CATCH_PERMANENT,
16410 CATCH_TEMPORARY);
16411 add_catch_command ("rethrow", _("\
16412 Catch an exception, when rethrown."),
16413 catch_rethrow_command,
16414 NULL,
16415 CATCH_PERMANENT,
16416 CATCH_TEMPORARY);
16417 add_catch_command ("fork", _("Catch calls to fork."),
16418 catch_fork_command_1,
16419 NULL,
16420 (void *) (uintptr_t) catch_fork_permanent,
16421 (void *) (uintptr_t) catch_fork_temporary);
16422 add_catch_command ("vfork", _("Catch calls to vfork."),
16423 catch_fork_command_1,
16424 NULL,
16425 (void *) (uintptr_t) catch_vfork_permanent,
16426 (void *) (uintptr_t) catch_vfork_temporary);
16427 add_catch_command ("exec", _("Catch calls to exec."),
16428 catch_exec_command_1,
16429 NULL,
16430 CATCH_PERMANENT,
16431 CATCH_TEMPORARY);
16432 add_catch_command ("load", _("Catch loads of shared libraries.\n\
16433 Usage: catch load [REGEX]\n\
16434 If REGEX is given, only stop for libraries matching the regular expression."),
16435 catch_load_command_1,
16436 NULL,
16437 CATCH_PERMANENT,
16438 CATCH_TEMPORARY);
16439 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
16440 Usage: catch unload [REGEX]\n\
16441 If REGEX is given, only stop for libraries matching the regular expression."),
16442 catch_unload_command_1,
16443 NULL,
16444 CATCH_PERMANENT,
16445 CATCH_TEMPORARY);
16446 add_catch_command ("syscall", _("\
16447 Catch system calls by their names and/or numbers.\n\
16448 Arguments say which system calls to catch. If no arguments\n\
16449 are given, every system call will be caught.\n\
16450 Arguments, if given, should be one or more system call names\n\
16451 (if your system supports that), or system call numbers."),
16452 catch_syscall_command_1,
16453 catch_syscall_completer,
16454 CATCH_PERMANENT,
16455 CATCH_TEMPORARY);
16456
16457 c = add_com ("watch", class_breakpoint, watch_command, _("\
16458 Set a watchpoint for an expression.\n\
16459 Usage: watch [-l|-location] EXPRESSION\n\
16460 A watchpoint stops execution of your program whenever the value of\n\
16461 an expression changes.\n\
16462 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16463 the memory to which it refers."));
16464 set_cmd_completer (c, expression_completer);
16465
16466 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
16467 Set a read watchpoint for an expression.\n\
16468 Usage: rwatch [-l|-location] EXPRESSION\n\
16469 A watchpoint stops execution of your program whenever the value of\n\
16470 an expression is read.\n\
16471 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16472 the memory to which it refers."));
16473 set_cmd_completer (c, expression_completer);
16474
16475 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
16476 Set a watchpoint for an expression.\n\
16477 Usage: awatch [-l|-location] EXPRESSION\n\
16478 A watchpoint stops execution of your program whenever the value of\n\
16479 an expression is either read or written.\n\
16480 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16481 the memory to which it refers."));
16482 set_cmd_completer (c, expression_completer);
16483
16484 add_info ("watchpoints", watchpoints_info, _("\
16485 Status of specified watchpoints (all watchpoints if no argument)."));
16486
16487 /* XXX: cagney/2005-02-23: This should be a boolean, and should
16488 respond to changes - contrary to the description. */
16489 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
16490 &can_use_hw_watchpoints, _("\
16491 Set debugger's willingness to use watchpoint hardware."), _("\
16492 Show debugger's willingness to use watchpoint hardware."), _("\
16493 If zero, gdb will not use hardware for new watchpoints, even if\n\
16494 such is available. (However, any hardware watchpoints that were\n\
16495 created before setting this to nonzero, will continue to use watchpoint\n\
16496 hardware.)"),
16497 NULL,
16498 show_can_use_hw_watchpoints,
16499 &setlist, &showlist);
16500
16501 can_use_hw_watchpoints = 1;
16502
16503 /* Tracepoint manipulation commands. */
16504
16505 c = add_com ("trace", class_breakpoint, trace_command, _("\
16506 Set a tracepoint at specified line or function.\n\
16507 \n"
16508 BREAK_ARGS_HELP ("trace") "\n\
16509 Do \"help tracepoints\" for info on other tracepoint commands."));
16510 set_cmd_completer (c, location_completer);
16511
16512 add_com_alias ("tp", "trace", class_alias, 0);
16513 add_com_alias ("tr", "trace", class_alias, 1);
16514 add_com_alias ("tra", "trace", class_alias, 1);
16515 add_com_alias ("trac", "trace", class_alias, 1);
16516
16517 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
16518 Set a fast tracepoint at specified line or function.\n\
16519 \n"
16520 BREAK_ARGS_HELP ("ftrace") "\n\
16521 Do \"help tracepoints\" for info on other tracepoint commands."));
16522 set_cmd_completer (c, location_completer);
16523
16524 c = add_com ("strace", class_breakpoint, strace_command, _("\
16525 Set a static tracepoint at specified line, function or marker.\n\
16526 \n\
16527 strace [LOCATION] [if CONDITION]\n\
16528 LOCATION may be a line number, function name, \"*\" and an address,\n\
16529 or -m MARKER_ID.\n\
16530 If a line number is specified, probe the marker at start of code\n\
16531 for that line. If a function is specified, probe the marker at start\n\
16532 of code for that function. If an address is specified, probe the marker\n\
16533 at that exact address. If a marker id is specified, probe the marker\n\
16534 with that name. With no LOCATION, uses current execution address of\n\
16535 the selected stack frame.\n\
16536 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16537 This collects arbitrary user data passed in the probe point call to the\n\
16538 tracing library. You can inspect it when analyzing the trace buffer,\n\
16539 by printing the $_sdata variable like any other convenience variable.\n\
16540 \n\
16541 CONDITION is a boolean expression.\n\
16542 \n\
16543 Multiple tracepoints at one place are permitted, and useful if their\n\
16544 conditions are different.\n\
16545 \n\
16546 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16547 Do \"help tracepoints\" for info on other tracepoint commands."));
16548 set_cmd_completer (c, location_completer);
16549
16550 add_info ("tracepoints", tracepoints_info, _("\
16551 Status of specified tracepoints (all tracepoints if no argument).\n\
16552 Convenience variable \"$tpnum\" contains the number of the\n\
16553 last tracepoint set."));
16554
16555 add_info_alias ("tp", "tracepoints", 1);
16556
16557 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16558 Delete specified tracepoints.\n\
16559 Arguments are tracepoint numbers, separated by spaces.\n\
16560 No argument means delete all tracepoints."),
16561 &deletelist);
16562 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
16563
16564 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16565 Disable specified tracepoints.\n\
16566 Arguments are tracepoint numbers, separated by spaces.\n\
16567 No argument means disable all tracepoints."),
16568 &disablelist);
16569 deprecate_cmd (c, "disable");
16570
16571 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16572 Enable specified tracepoints.\n\
16573 Arguments are tracepoint numbers, separated by spaces.\n\
16574 No argument means enable all tracepoints."),
16575 &enablelist);
16576 deprecate_cmd (c, "enable");
16577
16578 add_com ("passcount", class_trace, trace_pass_command, _("\
16579 Set the passcount for a tracepoint.\n\
16580 The trace will end when the tracepoint has been passed 'count' times.\n\
16581 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16582 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16583
16584 add_prefix_cmd ("save", class_breakpoint, save_command,
16585 _("Save breakpoint definitions as a script."),
16586 &save_cmdlist, "save ",
16587 0/*allow-unknown*/, &cmdlist);
16588
16589 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16590 Save current breakpoint definitions as a script.\n\
16591 This includes all types of breakpoints (breakpoints, watchpoints,\n\
16592 catchpoints, tracepoints). Use the 'source' command in another debug\n\
16593 session to restore them."),
16594 &save_cmdlist);
16595 set_cmd_completer (c, filename_completer);
16596
16597 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
16598 Save current tracepoint definitions as a script.\n\
16599 Use the 'source' command in another debug session to restore them."),
16600 &save_cmdlist);
16601 set_cmd_completer (c, filename_completer);
16602
16603 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16604 deprecate_cmd (c, "save tracepoints");
16605
16606 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
16607 Breakpoint specific settings\n\
16608 Configure various breakpoint-specific variables such as\n\
16609 pending breakpoint behavior"),
16610 &breakpoint_set_cmdlist, "set breakpoint ",
16611 0/*allow-unknown*/, &setlist);
16612 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
16613 Breakpoint specific settings\n\
16614 Configure various breakpoint-specific variables such as\n\
16615 pending breakpoint behavior"),
16616 &breakpoint_show_cmdlist, "show breakpoint ",
16617 0/*allow-unknown*/, &showlist);
16618
16619 add_setshow_auto_boolean_cmd ("pending", no_class,
16620 &pending_break_support, _("\
16621 Set debugger's behavior regarding pending breakpoints."), _("\
16622 Show debugger's behavior regarding pending breakpoints."), _("\
16623 If on, an unrecognized breakpoint location will cause gdb to create a\n\
16624 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16625 an error. If auto, an unrecognized breakpoint location results in a\n\
16626 user-query to see if a pending breakpoint should be created."),
16627 NULL,
16628 show_pending_break_support,
16629 &breakpoint_set_cmdlist,
16630 &breakpoint_show_cmdlist);
16631
16632 pending_break_support = AUTO_BOOLEAN_AUTO;
16633
16634 add_setshow_boolean_cmd ("auto-hw", no_class,
16635 &automatic_hardware_breakpoints, _("\
16636 Set automatic usage of hardware breakpoints."), _("\
16637 Show automatic usage of hardware breakpoints."), _("\
16638 If set, the debugger will automatically use hardware breakpoints for\n\
16639 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16640 a warning will be emitted for such breakpoints."),
16641 NULL,
16642 show_automatic_hardware_breakpoints,
16643 &breakpoint_set_cmdlist,
16644 &breakpoint_show_cmdlist);
16645
16646 add_setshow_auto_boolean_cmd ("always-inserted", class_support,
16647 &always_inserted_mode, _("\
16648 Set mode for inserting breakpoints."), _("\
16649 Show mode for inserting breakpoints."), _("\
16650 When this mode is off, breakpoints are inserted in inferior when it is\n\
16651 resumed, and removed when execution stops. When this mode is on,\n\
16652 breakpoints are inserted immediately and removed only when the user\n\
16653 deletes the breakpoint. When this mode is auto (which is the default),\n\
16654 the behaviour depends on the non-stop setting (see help set non-stop).\n\
16655 In this case, if gdb is controlling the inferior in non-stop mode, gdb\n\
16656 behaves as if always-inserted mode is on; if gdb is controlling the\n\
16657 inferior in all-stop mode, gdb behaves as if always-inserted mode is off."),
16658 NULL,
16659 &show_always_inserted_mode,
16660 &breakpoint_set_cmdlist,
16661 &breakpoint_show_cmdlist);
16662
16663 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16664 condition_evaluation_enums,
16665 &condition_evaluation_mode_1, _("\
16666 Set mode of breakpoint condition evaluation."), _("\
16667 Show mode of breakpoint condition evaluation."), _("\
16668 When this is set to \"host\", breakpoint conditions will be\n\
16669 evaluated on the host's side by GDB. When it is set to \"target\",\n\
16670 breakpoint conditions will be downloaded to the target (if the target\n\
16671 supports such feature) and conditions will be evaluated on the target's side.\n\
16672 If this is set to \"auto\" (default), this will be automatically set to\n\
16673 \"target\" if it supports condition evaluation, otherwise it will\n\
16674 be set to \"gdb\""),
16675 &set_condition_evaluation_mode,
16676 &show_condition_evaluation_mode,
16677 &breakpoint_set_cmdlist,
16678 &breakpoint_show_cmdlist);
16679
16680 add_com ("break-range", class_breakpoint, break_range_command, _("\
16681 Set a breakpoint for an address range.\n\
16682 break-range START-LOCATION, END-LOCATION\n\
16683 where START-LOCATION and END-LOCATION can be one of the following:\n\
16684 LINENUM, for that line in the current file,\n\
16685 FILE:LINENUM, for that line in that file,\n\
16686 +OFFSET, for that number of lines after the current line\n\
16687 or the start of the range\n\
16688 FUNCTION, for the first line in that function,\n\
16689 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16690 *ADDRESS, for the instruction at that address.\n\
16691 \n\
16692 The breakpoint will stop execution of the inferior whenever it executes\n\
16693 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16694 range (including START-LOCATION and END-LOCATION)."));
16695
16696 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
16697 Set a dynamic printf at specified line or function.\n\
16698 dprintf location,format string,arg1,arg2,...\n\
16699 location may be a line number, function name, or \"*\" and an address.\n\
16700 If a line number is specified, break at start of code for that line.\n\
16701 If a function is specified, break at start of code for that function.\n\
16702 "));
16703 set_cmd_completer (c, location_completer);
16704
16705 add_setshow_enum_cmd ("dprintf-style", class_support,
16706 dprintf_style_enums, &dprintf_style, _("\
16707 Set the style of usage for dynamic printf."), _("\
16708 Show the style of usage for dynamic printf."), _("\
16709 This setting chooses how GDB will do a dynamic printf.\n\
16710 If the value is \"gdb\", then the printing is done by GDB to its own\n\
16711 console, as with the \"printf\" command.\n\
16712 If the value is \"call\", the print is done by calling a function in your\n\
16713 program; by default printf(), but you can choose a different function or\n\
16714 output stream by setting dprintf-function and dprintf-channel."),
16715 update_dprintf_commands, NULL,
16716 &setlist, &showlist);
16717
16718 dprintf_function = xstrdup ("printf");
16719 add_setshow_string_cmd ("dprintf-function", class_support,
16720 &dprintf_function, _("\
16721 Set the function to use for dynamic printf"), _("\
16722 Show the function to use for dynamic printf"), NULL,
16723 update_dprintf_commands, NULL,
16724 &setlist, &showlist);
16725
16726 dprintf_channel = xstrdup ("");
16727 add_setshow_string_cmd ("dprintf-channel", class_support,
16728 &dprintf_channel, _("\
16729 Set the channel to use for dynamic printf"), _("\
16730 Show the channel to use for dynamic printf"), NULL,
16731 update_dprintf_commands, NULL,
16732 &setlist, &showlist);
16733
16734 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16735 &disconnected_dprintf, _("\
16736 Set whether dprintf continues after GDB disconnects."), _("\
16737 Show whether dprintf continues after GDB disconnects."), _("\
16738 Use this to let dprintf commands continue to hit and produce output\n\
16739 even if GDB disconnects or detaches from the target."),
16740 NULL,
16741 NULL,
16742 &setlist, &showlist);
16743
16744 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16745 agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16746 (target agent only) This is useful for formatted output in user-defined commands."));
16747
16748 automatic_hardware_breakpoints = 1;
16749
16750 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
16751 }
This page took 0.381177 seconds and 4 git commands to generate.