* breakpoint.c (condition_completer): New function.
[deliverable/binutils-gdb.git] / gdb / breakpoint.c
1 /* Everything about breakpoints, for GDB.
2
3 Copyright (C) 1986-2012 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
72 /* readline include files */
73 #include "readline/readline.h"
74 #include "readline/history.h"
75
76 /* readline defines this. */
77 #undef savestring
78
79 #include "mi/mi-common.h"
80 #include "python/python.h"
81
82 /* Prototypes for local functions. */
83
84 static void enable_delete_command (char *, int);
85
86 static void enable_once_command (char *, int);
87
88 static void enable_count_command (char *, int);
89
90 static void disable_command (char *, int);
91
92 static void enable_command (char *, int);
93
94 static void map_breakpoint_numbers (char *, void (*) (struct breakpoint *,
95 void *),
96 void *);
97
98 static void ignore_command (char *, int);
99
100 static int breakpoint_re_set_one (void *);
101
102 static void breakpoint_re_set_default (struct breakpoint *);
103
104 static void create_sals_from_address_default (char **,
105 struct linespec_result *,
106 enum bptype, char *,
107 char **);
108
109 static void create_breakpoints_sal_default (struct gdbarch *,
110 struct linespec_result *,
111 struct linespec_sals *,
112 char *, char *, enum bptype,
113 enum bpdisp, int, int,
114 int,
115 const struct breakpoint_ops *,
116 int, int, int, unsigned);
117
118 static void decode_linespec_default (struct breakpoint *, char **,
119 struct symtabs_and_lines *);
120
121 static void clear_command (char *, int);
122
123 static void catch_command (char *, int);
124
125 static int can_use_hardware_watchpoint (struct value *);
126
127 static void break_command_1 (char *, int, int);
128
129 static void mention (struct breakpoint *);
130
131 static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
132 enum bptype,
133 const struct breakpoint_ops *);
134 static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
135 const struct symtab_and_line *);
136
137 /* This function is used in gdbtk sources and thus can not be made
138 static. */
139 struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
140 struct symtab_and_line,
141 enum bptype,
142 const struct breakpoint_ops *);
143
144 static struct breakpoint *
145 momentary_breakpoint_from_master (struct breakpoint *orig,
146 enum bptype type,
147 const struct breakpoint_ops *ops);
148
149 static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
150
151 static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
152 CORE_ADDR bpaddr,
153 enum bptype bptype);
154
155 static void describe_other_breakpoints (struct gdbarch *,
156 struct program_space *, CORE_ADDR,
157 struct obj_section *, int);
158
159 static int breakpoint_address_match (struct address_space *aspace1,
160 CORE_ADDR addr1,
161 struct address_space *aspace2,
162 CORE_ADDR addr2);
163
164 static int watchpoint_locations_match (struct bp_location *loc1,
165 struct bp_location *loc2);
166
167 static int breakpoint_location_address_match (struct bp_location *bl,
168 struct address_space *aspace,
169 CORE_ADDR addr);
170
171 static void breakpoints_info (char *, int);
172
173 static void watchpoints_info (char *, int);
174
175 static int breakpoint_1 (char *, int,
176 int (*) (const struct breakpoint *));
177
178 static int breakpoint_cond_eval (void *);
179
180 static void cleanup_executing_breakpoints (void *);
181
182 static void commands_command (char *, int);
183
184 static void condition_command (char *, int);
185
186 typedef enum
187 {
188 mark_inserted,
189 mark_uninserted
190 }
191 insertion_state_t;
192
193 static int remove_breakpoint (struct bp_location *, insertion_state_t);
194 static int remove_breakpoint_1 (struct bp_location *, insertion_state_t);
195
196 static enum print_stop_action print_bp_stop_message (bpstat bs);
197
198 static int watchpoint_check (void *);
199
200 static void maintenance_info_breakpoints (char *, int);
201
202 static int hw_breakpoint_used_count (void);
203
204 static int hw_watchpoint_use_count (struct breakpoint *);
205
206 static int hw_watchpoint_used_count_others (struct breakpoint *except,
207 enum bptype type,
208 int *other_type_used);
209
210 static void hbreak_command (char *, int);
211
212 static void thbreak_command (char *, int);
213
214 static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
215 int count);
216
217 static void stop_command (char *arg, int from_tty);
218
219 static void stopin_command (char *arg, int from_tty);
220
221 static void stopat_command (char *arg, int from_tty);
222
223 static char *ep_parse_optional_if_clause (char **arg);
224
225 static void catch_exception_command_1 (enum exception_event_kind ex_event,
226 char *arg, int tempflag, int from_tty);
227
228 static void tcatch_command (char *arg, int from_tty);
229
230 static void detach_single_step_breakpoints (void);
231
232 static int single_step_breakpoint_inserted_here_p (struct address_space *,
233 CORE_ADDR pc);
234
235 static void free_bp_location (struct bp_location *loc);
236 static void incref_bp_location (struct bp_location *loc);
237 static void decref_bp_location (struct bp_location **loc);
238
239 static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
240
241 static void update_global_location_list (int);
242
243 static void update_global_location_list_nothrow (int);
244
245 static int is_hardware_watchpoint (const struct breakpoint *bpt);
246
247 static void insert_breakpoint_locations (void);
248
249 static int syscall_catchpoint_p (struct breakpoint *b);
250
251 static void tracepoints_info (char *, int);
252
253 static void delete_trace_command (char *, int);
254
255 static void enable_trace_command (char *, int);
256
257 static void disable_trace_command (char *, int);
258
259 static void trace_pass_command (char *, int);
260
261 static int is_masked_watchpoint (const struct breakpoint *b);
262
263 static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
264
265 /* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
266 otherwise. */
267
268 static int strace_marker_p (struct breakpoint *b);
269
270 static void init_catchpoint (struct breakpoint *b,
271 struct gdbarch *gdbarch, int tempflag,
272 char *cond_string,
273 const struct breakpoint_ops *ops);
274
275 /* The abstract base class all breakpoint_ops structures inherit
276 from. */
277 static struct breakpoint_ops base_breakpoint_ops;
278
279 /* The breakpoint_ops structure to be inherited by all breakpoint_ops
280 that are implemented on top of software or hardware breakpoints
281 (user breakpoints, internal and momentary breakpoints, etc.). */
282 static struct breakpoint_ops bkpt_base_breakpoint_ops;
283
284 /* Internal breakpoints class type. */
285 static struct breakpoint_ops internal_breakpoint_ops;
286
287 /* Momentary breakpoints class type. */
288 static struct breakpoint_ops momentary_breakpoint_ops;
289
290 /* The breakpoint_ops structure to be used in regular user created
291 breakpoints. */
292 struct breakpoint_ops bkpt_breakpoint_ops;
293
294 /* Breakpoints set on probes. */
295 static struct breakpoint_ops bkpt_probe_breakpoint_ops;
296
297 /* Dynamic printf class type. */
298 static struct breakpoint_ops dprintf_breakpoint_ops;
299
300 /* A reference-counted struct command_line. This lets multiple
301 breakpoints share a single command list. */
302 struct counted_command_line
303 {
304 /* The reference count. */
305 int refc;
306
307 /* The command list. */
308 struct command_line *commands;
309 };
310
311 struct command_line *
312 breakpoint_commands (struct breakpoint *b)
313 {
314 return b->commands ? b->commands->commands : NULL;
315 }
316
317 /* Flag indicating that a command has proceeded the inferior past the
318 current breakpoint. */
319
320 static int breakpoint_proceeded;
321
322 const char *
323 bpdisp_text (enum bpdisp disp)
324 {
325 /* NOTE: the following values are a part of MI protocol and
326 represent values of 'disp' field returned when inferior stops at
327 a breakpoint. */
328 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
329
330 return bpdisps[(int) disp];
331 }
332
333 /* Prototypes for exported functions. */
334 /* If FALSE, gdb will not use hardware support for watchpoints, even
335 if such is available. */
336 static int can_use_hw_watchpoints;
337
338 static void
339 show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
340 struct cmd_list_element *c,
341 const char *value)
342 {
343 fprintf_filtered (file,
344 _("Debugger's willingness to use "
345 "watchpoint hardware is %s.\n"),
346 value);
347 }
348
349 /* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
350 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
351 for unrecognized breakpoint locations.
352 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
353 static enum auto_boolean pending_break_support;
354 static void
355 show_pending_break_support (struct ui_file *file, int from_tty,
356 struct cmd_list_element *c,
357 const char *value)
358 {
359 fprintf_filtered (file,
360 _("Debugger's behavior regarding "
361 "pending breakpoints is %s.\n"),
362 value);
363 }
364
365 /* If 1, gdb will automatically use hardware breakpoints for breakpoints
366 set with "break" but falling in read-only memory.
367 If 0, gdb will warn about such breakpoints, but won't automatically
368 use hardware breakpoints. */
369 static int automatic_hardware_breakpoints;
370 static void
371 show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
372 struct cmd_list_element *c,
373 const char *value)
374 {
375 fprintf_filtered (file,
376 _("Automatic usage of hardware breakpoints is %s.\n"),
377 value);
378 }
379
380 /* If on, gdb will keep breakpoints inserted even as inferior is
381 stopped, and immediately insert any new breakpoints. If off, gdb
382 will insert breakpoints into inferior only when resuming it, and
383 will remove breakpoints upon stop. If auto, GDB will behave as ON
384 if in non-stop mode, and as OFF if all-stop mode.*/
385
386 static const char always_inserted_auto[] = "auto";
387 static const char always_inserted_on[] = "on";
388 static const char always_inserted_off[] = "off";
389 static const char *const always_inserted_enums[] = {
390 always_inserted_auto,
391 always_inserted_off,
392 always_inserted_on,
393 NULL
394 };
395 static const char *always_inserted_mode = always_inserted_auto;
396 static void
397 show_always_inserted_mode (struct ui_file *file, int from_tty,
398 struct cmd_list_element *c, const char *value)
399 {
400 if (always_inserted_mode == always_inserted_auto)
401 fprintf_filtered (file,
402 _("Always inserted breakpoint "
403 "mode is %s (currently %s).\n"),
404 value,
405 breakpoints_always_inserted_mode () ? "on" : "off");
406 else
407 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
408 value);
409 }
410
411 int
412 breakpoints_always_inserted_mode (void)
413 {
414 return (always_inserted_mode == always_inserted_on
415 || (always_inserted_mode == always_inserted_auto && non_stop));
416 }
417
418 static const char condition_evaluation_both[] = "host or target";
419
420 /* Modes for breakpoint condition evaluation. */
421 static const char condition_evaluation_auto[] = "auto";
422 static const char condition_evaluation_host[] = "host";
423 static const char condition_evaluation_target[] = "target";
424 static const char *const condition_evaluation_enums[] = {
425 condition_evaluation_auto,
426 condition_evaluation_host,
427 condition_evaluation_target,
428 NULL
429 };
430
431 /* Global that holds the current mode for breakpoint condition evaluation. */
432 static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
433
434 /* Global that we use to display information to the user (gets its value from
435 condition_evaluation_mode_1. */
436 static const char *condition_evaluation_mode = condition_evaluation_auto;
437
438 /* Translate a condition evaluation mode MODE into either "host"
439 or "target". This is used mostly to translate from "auto" to the
440 real setting that is being used. It returns the translated
441 evaluation mode. */
442
443 static const char *
444 translate_condition_evaluation_mode (const char *mode)
445 {
446 if (mode == condition_evaluation_auto)
447 {
448 if (target_supports_evaluation_of_breakpoint_conditions ())
449 return condition_evaluation_target;
450 else
451 return condition_evaluation_host;
452 }
453 else
454 return mode;
455 }
456
457 /* Discovers what condition_evaluation_auto translates to. */
458
459 static const char *
460 breakpoint_condition_evaluation_mode (void)
461 {
462 return translate_condition_evaluation_mode (condition_evaluation_mode);
463 }
464
465 /* Return true if GDB should evaluate breakpoint conditions or false
466 otherwise. */
467
468 static int
469 gdb_evaluates_breakpoint_condition_p (void)
470 {
471 const char *mode = breakpoint_condition_evaluation_mode ();
472
473 return (mode == condition_evaluation_host);
474 }
475
476 void _initialize_breakpoint (void);
477
478 /* Are we executing breakpoint commands? */
479 static int executing_breakpoint_commands;
480
481 /* Are overlay event breakpoints enabled? */
482 static int overlay_events_enabled;
483
484 /* See description in breakpoint.h. */
485 int target_exact_watchpoints = 0;
486
487 /* Walk the following statement or block through all breakpoints.
488 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
489 current breakpoint. */
490
491 #define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
492
493 #define ALL_BREAKPOINTS_SAFE(B,TMP) \
494 for (B = breakpoint_chain; \
495 B ? (TMP=B->next, 1): 0; \
496 B = TMP)
497
498 /* Similar iterator for the low-level breakpoints. SAFE variant is
499 not provided so update_global_location_list must not be called
500 while executing the block of ALL_BP_LOCATIONS. */
501
502 #define ALL_BP_LOCATIONS(B,BP_TMP) \
503 for (BP_TMP = bp_location; \
504 BP_TMP < bp_location + bp_location_count && (B = *BP_TMP); \
505 BP_TMP++)
506
507 /* Iterates through locations with address ADDRESS for the currently selected
508 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
509 to where the loop should start from.
510 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
511 appropriate location to start with. */
512
513 #define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
514 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
515 BP_LOCP_TMP = BP_LOCP_START; \
516 BP_LOCP_START \
517 && (BP_LOCP_TMP < bp_location + bp_location_count \
518 && (*BP_LOCP_TMP)->address == ADDRESS); \
519 BP_LOCP_TMP++)
520
521 /* Iterator for tracepoints only. */
522
523 #define ALL_TRACEPOINTS(B) \
524 for (B = breakpoint_chain; B; B = B->next) \
525 if (is_tracepoint (B))
526
527 /* Chains of all breakpoints defined. */
528
529 struct breakpoint *breakpoint_chain;
530
531 /* Array is sorted by bp_location_compare - primarily by the ADDRESS. */
532
533 static struct bp_location **bp_location;
534
535 /* Number of elements of BP_LOCATION. */
536
537 static unsigned bp_location_count;
538
539 /* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
540 ADDRESS for the current elements of BP_LOCATION which get a valid
541 result from bp_location_has_shadow. You can use it for roughly
542 limiting the subrange of BP_LOCATION to scan for shadow bytes for
543 an address you need to read. */
544
545 static CORE_ADDR bp_location_placed_address_before_address_max;
546
547 /* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
548 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
549 BP_LOCATION which get a valid result from bp_location_has_shadow.
550 You can use it for roughly limiting the subrange of BP_LOCATION to
551 scan for shadow bytes for an address you need to read. */
552
553 static CORE_ADDR bp_location_shadow_len_after_address_max;
554
555 /* The locations that no longer correspond to any breakpoint, unlinked
556 from bp_location array, but for which a hit may still be reported
557 by a target. */
558 VEC(bp_location_p) *moribund_locations = NULL;
559
560 /* Number of last breakpoint made. */
561
562 static int breakpoint_count;
563
564 /* The value of `breakpoint_count' before the last command that
565 created breakpoints. If the last (break-like) command created more
566 than one breakpoint, then the difference between BREAKPOINT_COUNT
567 and PREV_BREAKPOINT_COUNT is more than one. */
568 static int prev_breakpoint_count;
569
570 /* Number of last tracepoint made. */
571
572 static int tracepoint_count;
573
574 static struct cmd_list_element *breakpoint_set_cmdlist;
575 static struct cmd_list_element *breakpoint_show_cmdlist;
576 struct cmd_list_element *save_cmdlist;
577
578 /* Return whether a breakpoint is an active enabled breakpoint. */
579 static int
580 breakpoint_enabled (struct breakpoint *b)
581 {
582 return (b->enable_state == bp_enabled);
583 }
584
585 /* Set breakpoint count to NUM. */
586
587 static void
588 set_breakpoint_count (int num)
589 {
590 prev_breakpoint_count = breakpoint_count;
591 breakpoint_count = num;
592 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
593 }
594
595 /* Used by `start_rbreak_breakpoints' below, to record the current
596 breakpoint count before "rbreak" creates any breakpoint. */
597 static int rbreak_start_breakpoint_count;
598
599 /* Called at the start an "rbreak" command to record the first
600 breakpoint made. */
601
602 void
603 start_rbreak_breakpoints (void)
604 {
605 rbreak_start_breakpoint_count = breakpoint_count;
606 }
607
608 /* Called at the end of an "rbreak" command to record the last
609 breakpoint made. */
610
611 void
612 end_rbreak_breakpoints (void)
613 {
614 prev_breakpoint_count = rbreak_start_breakpoint_count;
615 }
616
617 /* Used in run_command to zero the hit count when a new run starts. */
618
619 void
620 clear_breakpoint_hit_counts (void)
621 {
622 struct breakpoint *b;
623
624 ALL_BREAKPOINTS (b)
625 b->hit_count = 0;
626 }
627
628 /* Allocate a new counted_command_line with reference count of 1.
629 The new structure owns COMMANDS. */
630
631 static struct counted_command_line *
632 alloc_counted_command_line (struct command_line *commands)
633 {
634 struct counted_command_line *result
635 = xmalloc (sizeof (struct counted_command_line));
636
637 result->refc = 1;
638 result->commands = commands;
639 return result;
640 }
641
642 /* Increment reference count. This does nothing if CMD is NULL. */
643
644 static void
645 incref_counted_command_line (struct counted_command_line *cmd)
646 {
647 if (cmd)
648 ++cmd->refc;
649 }
650
651 /* Decrement reference count. If the reference count reaches 0,
652 destroy the counted_command_line. Sets *CMDP to NULL. This does
653 nothing if *CMDP is NULL. */
654
655 static void
656 decref_counted_command_line (struct counted_command_line **cmdp)
657 {
658 if (*cmdp)
659 {
660 if (--(*cmdp)->refc == 0)
661 {
662 free_command_lines (&(*cmdp)->commands);
663 xfree (*cmdp);
664 }
665 *cmdp = NULL;
666 }
667 }
668
669 /* A cleanup function that calls decref_counted_command_line. */
670
671 static void
672 do_cleanup_counted_command_line (void *arg)
673 {
674 decref_counted_command_line (arg);
675 }
676
677 /* Create a cleanup that calls decref_counted_command_line on the
678 argument. */
679
680 static struct cleanup *
681 make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
682 {
683 return make_cleanup (do_cleanup_counted_command_line, cmdp);
684 }
685
686 \f
687 /* Return the breakpoint with the specified number, or NULL
688 if the number does not refer to an existing breakpoint. */
689
690 struct breakpoint *
691 get_breakpoint (int num)
692 {
693 struct breakpoint *b;
694
695 ALL_BREAKPOINTS (b)
696 if (b->number == num)
697 return b;
698
699 return NULL;
700 }
701
702 \f
703
704 /* Mark locations as "conditions have changed" in case the target supports
705 evaluating conditions on its side. */
706
707 static void
708 mark_breakpoint_modified (struct breakpoint *b)
709 {
710 struct bp_location *loc;
711
712 /* This is only meaningful if the target is
713 evaluating conditions and if the user has
714 opted for condition evaluation on the target's
715 side. */
716 if (gdb_evaluates_breakpoint_condition_p ()
717 || !target_supports_evaluation_of_breakpoint_conditions ())
718 return;
719
720 if (!is_breakpoint (b))
721 return;
722
723 for (loc = b->loc; loc; loc = loc->next)
724 loc->condition_changed = condition_modified;
725 }
726
727 /* Mark location as "conditions have changed" in case the target supports
728 evaluating conditions on its side. */
729
730 static void
731 mark_breakpoint_location_modified (struct bp_location *loc)
732 {
733 /* This is only meaningful if the target is
734 evaluating conditions and if the user has
735 opted for condition evaluation on the target's
736 side. */
737 if (gdb_evaluates_breakpoint_condition_p ()
738 || !target_supports_evaluation_of_breakpoint_conditions ())
739
740 return;
741
742 if (!is_breakpoint (loc->owner))
743 return;
744
745 loc->condition_changed = condition_modified;
746 }
747
748 /* Sets the condition-evaluation mode using the static global
749 condition_evaluation_mode. */
750
751 static void
752 set_condition_evaluation_mode (char *args, int from_tty,
753 struct cmd_list_element *c)
754 {
755 const char *old_mode, *new_mode;
756
757 if ((condition_evaluation_mode_1 == condition_evaluation_target)
758 && !target_supports_evaluation_of_breakpoint_conditions ())
759 {
760 condition_evaluation_mode_1 = condition_evaluation_mode;
761 warning (_("Target does not support breakpoint condition evaluation.\n"
762 "Using host evaluation mode instead."));
763 return;
764 }
765
766 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
767 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
768
769 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
770 settings was "auto". */
771 condition_evaluation_mode = condition_evaluation_mode_1;
772
773 /* Only update the mode if the user picked a different one. */
774 if (new_mode != old_mode)
775 {
776 struct bp_location *loc, **loc_tmp;
777 /* If the user switched to a different evaluation mode, we
778 need to synch the changes with the target as follows:
779
780 "host" -> "target": Send all (valid) conditions to the target.
781 "target" -> "host": Remove all the conditions from the target.
782 */
783
784 if (new_mode == condition_evaluation_target)
785 {
786 /* Mark everything modified and synch conditions with the
787 target. */
788 ALL_BP_LOCATIONS (loc, loc_tmp)
789 mark_breakpoint_location_modified (loc);
790 }
791 else
792 {
793 /* Manually mark non-duplicate locations to synch conditions
794 with the target. We do this to remove all the conditions the
795 target knows about. */
796 ALL_BP_LOCATIONS (loc, loc_tmp)
797 if (is_breakpoint (loc->owner) && loc->inserted)
798 loc->needs_update = 1;
799 }
800
801 /* Do the update. */
802 update_global_location_list (1);
803 }
804
805 return;
806 }
807
808 /* Shows the current mode of breakpoint condition evaluation. Explicitly shows
809 what "auto" is translating to. */
810
811 static void
812 show_condition_evaluation_mode (struct ui_file *file, int from_tty,
813 struct cmd_list_element *c, const char *value)
814 {
815 if (condition_evaluation_mode == condition_evaluation_auto)
816 fprintf_filtered (file,
817 _("Breakpoint condition evaluation "
818 "mode is %s (currently %s).\n"),
819 value,
820 breakpoint_condition_evaluation_mode ());
821 else
822 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
823 value);
824 }
825
826 /* A comparison function for bp_location AP and BP that is used by
827 bsearch. This comparison function only cares about addresses, unlike
828 the more general bp_location_compare function. */
829
830 static int
831 bp_location_compare_addrs (const void *ap, const void *bp)
832 {
833 struct bp_location *a = *(void **) ap;
834 struct bp_location *b = *(void **) bp;
835
836 if (a->address == b->address)
837 return 0;
838 else
839 return ((a->address > b->address) - (a->address < b->address));
840 }
841
842 /* Helper function to skip all bp_locations with addresses
843 less than ADDRESS. It returns the first bp_location that
844 is greater than or equal to ADDRESS. If none is found, just
845 return NULL. */
846
847 static struct bp_location **
848 get_first_locp_gte_addr (CORE_ADDR address)
849 {
850 struct bp_location dummy_loc;
851 struct bp_location *dummy_locp = &dummy_loc;
852 struct bp_location **locp_found = NULL;
853
854 /* Initialize the dummy location's address field. */
855 memset (&dummy_loc, 0, sizeof (struct bp_location));
856 dummy_loc.address = address;
857
858 /* Find a close match to the first location at ADDRESS. */
859 locp_found = bsearch (&dummy_locp, bp_location, bp_location_count,
860 sizeof (struct bp_location **),
861 bp_location_compare_addrs);
862
863 /* Nothing was found, nothing left to do. */
864 if (locp_found == NULL)
865 return NULL;
866
867 /* We may have found a location that is at ADDRESS but is not the first in the
868 location's list. Go backwards (if possible) and locate the first one. */
869 while ((locp_found - 1) >= bp_location
870 && (*(locp_found - 1))->address == address)
871 locp_found--;
872
873 return locp_found;
874 }
875
876 void
877 set_breakpoint_condition (struct breakpoint *b, char *exp,
878 int from_tty)
879 {
880 xfree (b->cond_string);
881 b->cond_string = NULL;
882
883 if (is_watchpoint (b))
884 {
885 struct watchpoint *w = (struct watchpoint *) b;
886
887 xfree (w->cond_exp);
888 w->cond_exp = NULL;
889 }
890 else
891 {
892 struct bp_location *loc;
893
894 for (loc = b->loc; loc; loc = loc->next)
895 {
896 xfree (loc->cond);
897 loc->cond = NULL;
898
899 /* No need to free the condition agent expression
900 bytecode (if we have one). We will handle this
901 when we go through update_global_location_list. */
902 }
903 }
904
905 if (*exp == 0)
906 {
907 if (from_tty)
908 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
909 }
910 else
911 {
912 char *arg = exp;
913
914 /* I don't know if it matters whether this is the string the user
915 typed in or the decompiled expression. */
916 b->cond_string = xstrdup (arg);
917 b->condition_not_parsed = 0;
918
919 if (is_watchpoint (b))
920 {
921 struct watchpoint *w = (struct watchpoint *) b;
922
923 innermost_block = NULL;
924 arg = exp;
925 w->cond_exp = parse_exp_1 (&arg, 0, 0);
926 if (*arg)
927 error (_("Junk at end of expression"));
928 w->cond_exp_valid_block = innermost_block;
929 }
930 else
931 {
932 struct bp_location *loc;
933
934 for (loc = b->loc; loc; loc = loc->next)
935 {
936 arg = exp;
937 loc->cond =
938 parse_exp_1 (&arg, block_for_pc (loc->address), 0);
939 if (*arg)
940 error (_("Junk at end of expression"));
941 }
942 }
943 }
944 mark_breakpoint_modified (b);
945
946 breakpoints_changed ();
947 observer_notify_breakpoint_modified (b);
948 }
949
950 /* Completion for the "condition" command. */
951
952 static VEC (char_ptr) *
953 condition_completer (struct cmd_list_element *cmd, char *text, char *word)
954 {
955 char *space;
956
957 text = skip_spaces (text);
958 space = skip_to_space (text);
959 if (*space == '\0')
960 {
961 int len;
962 struct breakpoint *b;
963 VEC (char_ptr) *result = NULL;
964
965 if (text[0] == '$')
966 {
967 /* We don't support completion of history indices. */
968 if (isdigit (text[1]))
969 return NULL;
970 return complete_internalvar (&text[1]);
971 }
972
973 /* We're completing the breakpoint number. */
974 len = strlen (text);
975
976 ALL_BREAKPOINTS (b)
977 {
978 int single = b->loc->next == NULL;
979 struct bp_location *loc;
980 int count = 1;
981
982 for (loc = b->loc; loc; loc = loc->next)
983 {
984 char location[50];
985
986 if (single)
987 sprintf (location, "%d", b->number);
988 else
989 sprintf (location, "%d.%d", b->number, count);
990
991 if (strncmp (location, text, len) == 0)
992 VEC_safe_push (char_ptr, result, xstrdup (location));
993
994 ++count;
995 }
996 }
997
998 return result;
999 }
1000
1001 /* We're completing the expression part. */
1002 text = skip_spaces (space);
1003 return expression_completer (cmd, text, word);
1004 }
1005
1006 /* condition N EXP -- set break condition of breakpoint N to EXP. */
1007
1008 static void
1009 condition_command (char *arg, int from_tty)
1010 {
1011 struct breakpoint *b;
1012 char *p;
1013 int bnum;
1014
1015 if (arg == 0)
1016 error_no_arg (_("breakpoint number"));
1017
1018 p = arg;
1019 bnum = get_number (&p);
1020 if (bnum == 0)
1021 error (_("Bad breakpoint argument: '%s'"), arg);
1022
1023 ALL_BREAKPOINTS (b)
1024 if (b->number == bnum)
1025 {
1026 /* Check if this breakpoint has a Python object assigned to
1027 it, and if it has a definition of the "stop"
1028 method. This method and conditions entered into GDB from
1029 the CLI are mutually exclusive. */
1030 if (b->py_bp_object
1031 && gdbpy_breakpoint_has_py_cond (b->py_bp_object))
1032 error (_("Cannot set a condition where a Python 'stop' "
1033 "method has been defined in the breakpoint."));
1034 set_breakpoint_condition (b, p, from_tty);
1035
1036 if (is_breakpoint (b))
1037 update_global_location_list (1);
1038
1039 return;
1040 }
1041
1042 error (_("No breakpoint number %d."), bnum);
1043 }
1044
1045 /* Check that COMMAND do not contain commands that are suitable
1046 only for tracepoints and not suitable for ordinary breakpoints.
1047 Throw if any such commands is found. */
1048
1049 static void
1050 check_no_tracepoint_commands (struct command_line *commands)
1051 {
1052 struct command_line *c;
1053
1054 for (c = commands; c; c = c->next)
1055 {
1056 int i;
1057
1058 if (c->control_type == while_stepping_control)
1059 error (_("The 'while-stepping' command can "
1060 "only be used for tracepoints"));
1061
1062 for (i = 0; i < c->body_count; ++i)
1063 check_no_tracepoint_commands ((c->body_list)[i]);
1064
1065 /* Not that command parsing removes leading whitespace and comment
1066 lines and also empty lines. So, we only need to check for
1067 command directly. */
1068 if (strstr (c->line, "collect ") == c->line)
1069 error (_("The 'collect' command can only be used for tracepoints"));
1070
1071 if (strstr (c->line, "teval ") == c->line)
1072 error (_("The 'teval' command can only be used for tracepoints"));
1073 }
1074 }
1075
1076 /* Encapsulate tests for different types of tracepoints. */
1077
1078 static int
1079 is_tracepoint_type (enum bptype type)
1080 {
1081 return (type == bp_tracepoint
1082 || type == bp_fast_tracepoint
1083 || type == bp_static_tracepoint);
1084 }
1085
1086 int
1087 is_tracepoint (const struct breakpoint *b)
1088 {
1089 return is_tracepoint_type (b->type);
1090 }
1091
1092 /* A helper function that validates that COMMANDS are valid for a
1093 breakpoint. This function will throw an exception if a problem is
1094 found. */
1095
1096 static void
1097 validate_commands_for_breakpoint (struct breakpoint *b,
1098 struct command_line *commands)
1099 {
1100 if (is_tracepoint (b))
1101 {
1102 /* We need to verify that each top-level element of commands is
1103 valid for tracepoints, that there's at most one
1104 while-stepping element, and that while-stepping's body has
1105 valid tracing commands excluding nested while-stepping. */
1106 struct command_line *c;
1107 struct command_line *while_stepping = 0;
1108 for (c = commands; c; c = c->next)
1109 {
1110 if (c->control_type == while_stepping_control)
1111 {
1112 if (b->type == bp_fast_tracepoint)
1113 error (_("The 'while-stepping' command "
1114 "cannot be used for fast tracepoint"));
1115 else if (b->type == bp_static_tracepoint)
1116 error (_("The 'while-stepping' command "
1117 "cannot be used for static tracepoint"));
1118
1119 if (while_stepping)
1120 error (_("The 'while-stepping' command "
1121 "can be used only once"));
1122 else
1123 while_stepping = c;
1124 }
1125 }
1126 if (while_stepping)
1127 {
1128 struct command_line *c2;
1129
1130 gdb_assert (while_stepping->body_count == 1);
1131 c2 = while_stepping->body_list[0];
1132 for (; c2; c2 = c2->next)
1133 {
1134 if (c2->control_type == while_stepping_control)
1135 error (_("The 'while-stepping' command cannot be nested"));
1136 }
1137 }
1138 }
1139 else
1140 {
1141 check_no_tracepoint_commands (commands);
1142 }
1143 }
1144
1145 /* Return a vector of all the static tracepoints set at ADDR. The
1146 caller is responsible for releasing the vector. */
1147
1148 VEC(breakpoint_p) *
1149 static_tracepoints_here (CORE_ADDR addr)
1150 {
1151 struct breakpoint *b;
1152 VEC(breakpoint_p) *found = 0;
1153 struct bp_location *loc;
1154
1155 ALL_BREAKPOINTS (b)
1156 if (b->type == bp_static_tracepoint)
1157 {
1158 for (loc = b->loc; loc; loc = loc->next)
1159 if (loc->address == addr)
1160 VEC_safe_push(breakpoint_p, found, b);
1161 }
1162
1163 return found;
1164 }
1165
1166 /* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
1167 validate that only allowed commands are included. */
1168
1169 void
1170 breakpoint_set_commands (struct breakpoint *b,
1171 struct command_line *commands)
1172 {
1173 validate_commands_for_breakpoint (b, commands);
1174
1175 decref_counted_command_line (&b->commands);
1176 b->commands = alloc_counted_command_line (commands);
1177 breakpoints_changed ();
1178 observer_notify_breakpoint_modified (b);
1179 }
1180
1181 /* Set the internal `silent' flag on the breakpoint. Note that this
1182 is not the same as the "silent" that may appear in the breakpoint's
1183 commands. */
1184
1185 void
1186 breakpoint_set_silent (struct breakpoint *b, int silent)
1187 {
1188 int old_silent = b->silent;
1189
1190 b->silent = silent;
1191 if (old_silent != silent)
1192 observer_notify_breakpoint_modified (b);
1193 }
1194
1195 /* Set the thread for this breakpoint. If THREAD is -1, make the
1196 breakpoint work for any thread. */
1197
1198 void
1199 breakpoint_set_thread (struct breakpoint *b, int thread)
1200 {
1201 int old_thread = b->thread;
1202
1203 b->thread = thread;
1204 if (old_thread != thread)
1205 observer_notify_breakpoint_modified (b);
1206 }
1207
1208 /* Set the task for this breakpoint. If TASK is 0, make the
1209 breakpoint work for any task. */
1210
1211 void
1212 breakpoint_set_task (struct breakpoint *b, int task)
1213 {
1214 int old_task = b->task;
1215
1216 b->task = task;
1217 if (old_task != task)
1218 observer_notify_breakpoint_modified (b);
1219 }
1220
1221 void
1222 check_tracepoint_command (char *line, void *closure)
1223 {
1224 struct breakpoint *b = closure;
1225
1226 validate_actionline (&line, b);
1227 }
1228
1229 /* A structure used to pass information through
1230 map_breakpoint_numbers. */
1231
1232 struct commands_info
1233 {
1234 /* True if the command was typed at a tty. */
1235 int from_tty;
1236
1237 /* The breakpoint range spec. */
1238 char *arg;
1239
1240 /* Non-NULL if the body of the commands are being read from this
1241 already-parsed command. */
1242 struct command_line *control;
1243
1244 /* The command lines read from the user, or NULL if they have not
1245 yet been read. */
1246 struct counted_command_line *cmd;
1247 };
1248
1249 /* A callback for map_breakpoint_numbers that sets the commands for
1250 commands_command. */
1251
1252 static void
1253 do_map_commands_command (struct breakpoint *b, void *data)
1254 {
1255 struct commands_info *info = data;
1256
1257 if (info->cmd == NULL)
1258 {
1259 struct command_line *l;
1260
1261 if (info->control != NULL)
1262 l = copy_command_lines (info->control->body_list[0]);
1263 else
1264 {
1265 struct cleanup *old_chain;
1266 char *str;
1267
1268 str = xstrprintf (_("Type commands for breakpoint(s) "
1269 "%s, one per line."),
1270 info->arg);
1271
1272 old_chain = make_cleanup (xfree, str);
1273
1274 l = read_command_lines (str,
1275 info->from_tty, 1,
1276 (is_tracepoint (b)
1277 ? check_tracepoint_command : 0),
1278 b);
1279
1280 do_cleanups (old_chain);
1281 }
1282
1283 info->cmd = alloc_counted_command_line (l);
1284 }
1285
1286 /* If a breakpoint was on the list more than once, we don't need to
1287 do anything. */
1288 if (b->commands != info->cmd)
1289 {
1290 validate_commands_for_breakpoint (b, info->cmd->commands);
1291 incref_counted_command_line (info->cmd);
1292 decref_counted_command_line (&b->commands);
1293 b->commands = info->cmd;
1294 breakpoints_changed ();
1295 observer_notify_breakpoint_modified (b);
1296 }
1297 }
1298
1299 static void
1300 commands_command_1 (char *arg, int from_tty,
1301 struct command_line *control)
1302 {
1303 struct cleanup *cleanups;
1304 struct commands_info info;
1305
1306 info.from_tty = from_tty;
1307 info.control = control;
1308 info.cmd = NULL;
1309 /* If we read command lines from the user, then `info' will hold an
1310 extra reference to the commands that we must clean up. */
1311 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
1312
1313 if (arg == NULL || !*arg)
1314 {
1315 if (breakpoint_count - prev_breakpoint_count > 1)
1316 arg = xstrprintf ("%d-%d", prev_breakpoint_count + 1,
1317 breakpoint_count);
1318 else if (breakpoint_count > 0)
1319 arg = xstrprintf ("%d", breakpoint_count);
1320 else
1321 {
1322 /* So that we don't try to free the incoming non-NULL
1323 argument in the cleanup below. Mapping breakpoint
1324 numbers will fail in this case. */
1325 arg = NULL;
1326 }
1327 }
1328 else
1329 /* The command loop has some static state, so we need to preserve
1330 our argument. */
1331 arg = xstrdup (arg);
1332
1333 if (arg != NULL)
1334 make_cleanup (xfree, arg);
1335
1336 info.arg = arg;
1337
1338 map_breakpoint_numbers (arg, do_map_commands_command, &info);
1339
1340 if (info.cmd == NULL)
1341 error (_("No breakpoints specified."));
1342
1343 do_cleanups (cleanups);
1344 }
1345
1346 static void
1347 commands_command (char *arg, int from_tty)
1348 {
1349 commands_command_1 (arg, from_tty, NULL);
1350 }
1351
1352 /* Like commands_command, but instead of reading the commands from
1353 input stream, takes them from an already parsed command structure.
1354
1355 This is used by cli-script.c to DTRT with breakpoint commands
1356 that are part of if and while bodies. */
1357 enum command_control_type
1358 commands_from_control_command (char *arg, struct command_line *cmd)
1359 {
1360 commands_command_1 (arg, 0, cmd);
1361 return simple_control;
1362 }
1363
1364 /* Return non-zero if BL->TARGET_INFO contains valid information. */
1365
1366 static int
1367 bp_location_has_shadow (struct bp_location *bl)
1368 {
1369 if (bl->loc_type != bp_loc_software_breakpoint)
1370 return 0;
1371 if (!bl->inserted)
1372 return 0;
1373 if (bl->target_info.shadow_len == 0)
1374 /* BL isn't valid, or doesn't shadow memory. */
1375 return 0;
1376 return 1;
1377 }
1378
1379 /* Update BUF, which is LEN bytes read from the target address MEMADDR,
1380 by replacing any memory breakpoints with their shadowed contents.
1381
1382 If READBUF is not NULL, this buffer must not overlap with any of
1383 the breakpoint location's shadow_contents buffers. Otherwise,
1384 a failed assertion internal error will be raised.
1385
1386 The range of shadowed area by each bp_location is:
1387 bl->address - bp_location_placed_address_before_address_max
1388 up to bl->address + bp_location_shadow_len_after_address_max
1389 The range we were requested to resolve shadows for is:
1390 memaddr ... memaddr + len
1391 Thus the safe cutoff boundaries for performance optimization are
1392 memaddr + len <= (bl->address
1393 - bp_location_placed_address_before_address_max)
1394 and:
1395 bl->address + bp_location_shadow_len_after_address_max <= memaddr */
1396
1397 void
1398 breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1399 const gdb_byte *writebuf_org,
1400 ULONGEST memaddr, LONGEST len)
1401 {
1402 /* Left boundary, right boundary and median element of our binary
1403 search. */
1404 unsigned bc_l, bc_r, bc;
1405
1406 /* Find BC_L which is a leftmost element which may affect BUF
1407 content. It is safe to report lower value but a failure to
1408 report higher one. */
1409
1410 bc_l = 0;
1411 bc_r = bp_location_count;
1412 while (bc_l + 1 < bc_r)
1413 {
1414 struct bp_location *bl;
1415
1416 bc = (bc_l + bc_r) / 2;
1417 bl = bp_location[bc];
1418
1419 /* Check first BL->ADDRESS will not overflow due to the added
1420 constant. Then advance the left boundary only if we are sure
1421 the BC element can in no way affect the BUF content (MEMADDR
1422 to MEMADDR + LEN range).
1423
1424 Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1425 offset so that we cannot miss a breakpoint with its shadow
1426 range tail still reaching MEMADDR. */
1427
1428 if ((bl->address + bp_location_shadow_len_after_address_max
1429 >= bl->address)
1430 && (bl->address + bp_location_shadow_len_after_address_max
1431 <= memaddr))
1432 bc_l = bc;
1433 else
1434 bc_r = bc;
1435 }
1436
1437 /* Due to the binary search above, we need to make sure we pick the
1438 first location that's at BC_L's address. E.g., if there are
1439 multiple locations at the same address, BC_L may end up pointing
1440 at a duplicate location, and miss the "master"/"inserted"
1441 location. Say, given locations L1, L2 and L3 at addresses A and
1442 B:
1443
1444 L1@A, L2@A, L3@B, ...
1445
1446 BC_L could end up pointing at location L2, while the "master"
1447 location could be L1. Since the `loc->inserted' flag is only set
1448 on "master" locations, we'd forget to restore the shadow of L1
1449 and L2. */
1450 while (bc_l > 0
1451 && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1452 bc_l--;
1453
1454 /* Now do full processing of the found relevant range of elements. */
1455
1456 for (bc = bc_l; bc < bp_location_count; bc++)
1457 {
1458 struct bp_location *bl = bp_location[bc];
1459 CORE_ADDR bp_addr = 0;
1460 int bp_size = 0;
1461 int bptoffset = 0;
1462
1463 /* bp_location array has BL->OWNER always non-NULL. */
1464 if (bl->owner->type == bp_none)
1465 warning (_("reading through apparently deleted breakpoint #%d?"),
1466 bl->owner->number);
1467
1468 /* Performance optimization: any further element can no longer affect BUF
1469 content. */
1470
1471 if (bl->address >= bp_location_placed_address_before_address_max
1472 && memaddr + len <= (bl->address
1473 - bp_location_placed_address_before_address_max))
1474 break;
1475
1476 if (!bp_location_has_shadow (bl))
1477 continue;
1478 if (!breakpoint_address_match (bl->target_info.placed_address_space, 0,
1479 current_program_space->aspace, 0))
1480 continue;
1481
1482 /* Addresses and length of the part of the breakpoint that
1483 we need to copy. */
1484 bp_addr = bl->target_info.placed_address;
1485 bp_size = bl->target_info.shadow_len;
1486
1487 if (bp_addr + bp_size <= memaddr)
1488 /* The breakpoint is entirely before the chunk of memory we
1489 are reading. */
1490 continue;
1491
1492 if (bp_addr >= memaddr + len)
1493 /* The breakpoint is entirely after the chunk of memory we are
1494 reading. */
1495 continue;
1496
1497 /* Offset within shadow_contents. */
1498 if (bp_addr < memaddr)
1499 {
1500 /* Only copy the second part of the breakpoint. */
1501 bp_size -= memaddr - bp_addr;
1502 bptoffset = memaddr - bp_addr;
1503 bp_addr = memaddr;
1504 }
1505
1506 if (bp_addr + bp_size > memaddr + len)
1507 {
1508 /* Only copy the first part of the breakpoint. */
1509 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1510 }
1511
1512 if (readbuf != NULL)
1513 {
1514 /* Verify that the readbuf buffer does not overlap with
1515 the shadow_contents buffer. */
1516 gdb_assert (bl->target_info.shadow_contents >= readbuf + len
1517 || readbuf >= (bl->target_info.shadow_contents
1518 + bl->target_info.shadow_len));
1519
1520 /* Update the read buffer with this inserted breakpoint's
1521 shadow. */
1522 memcpy (readbuf + bp_addr - memaddr,
1523 bl->target_info.shadow_contents + bptoffset, bp_size);
1524 }
1525 else
1526 {
1527 struct gdbarch *gdbarch = bl->gdbarch;
1528 const unsigned char *bp;
1529 CORE_ADDR placed_address = bl->target_info.placed_address;
1530 unsigned placed_size = bl->target_info.placed_size;
1531
1532 /* Update the shadow with what we want to write to memory. */
1533 memcpy (bl->target_info.shadow_contents + bptoffset,
1534 writebuf_org + bp_addr - memaddr, bp_size);
1535
1536 /* Determine appropriate breakpoint contents and size for this
1537 address. */
1538 bp = gdbarch_breakpoint_from_pc (gdbarch, &placed_address, &placed_size);
1539
1540 /* Update the final write buffer with this inserted
1541 breakpoint's INSN. */
1542 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1543 }
1544 }
1545 }
1546 \f
1547
1548 /* Return true if BPT is either a software breakpoint or a hardware
1549 breakpoint. */
1550
1551 int
1552 is_breakpoint (const struct breakpoint *bpt)
1553 {
1554 return (bpt->type == bp_breakpoint
1555 || bpt->type == bp_hardware_breakpoint
1556 || bpt->type == bp_dprintf);
1557 }
1558
1559 /* Return true if BPT is of any hardware watchpoint kind. */
1560
1561 static int
1562 is_hardware_watchpoint (const struct breakpoint *bpt)
1563 {
1564 return (bpt->type == bp_hardware_watchpoint
1565 || bpt->type == bp_read_watchpoint
1566 || bpt->type == bp_access_watchpoint);
1567 }
1568
1569 /* Return true if BPT is of any watchpoint kind, hardware or
1570 software. */
1571
1572 int
1573 is_watchpoint (const struct breakpoint *bpt)
1574 {
1575 return (is_hardware_watchpoint (bpt)
1576 || bpt->type == bp_watchpoint);
1577 }
1578
1579 /* Returns true if the current thread and its running state are safe
1580 to evaluate or update watchpoint B. Watchpoints on local
1581 expressions need to be evaluated in the context of the thread that
1582 was current when the watchpoint was created, and, that thread needs
1583 to be stopped to be able to select the correct frame context.
1584 Watchpoints on global expressions can be evaluated on any thread,
1585 and in any state. It is presently left to the target allowing
1586 memory accesses when threads are running. */
1587
1588 static int
1589 watchpoint_in_thread_scope (struct watchpoint *b)
1590 {
1591 return (b->base.pspace == current_program_space
1592 && (ptid_equal (b->watchpoint_thread, null_ptid)
1593 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1594 && !is_executing (inferior_ptid))));
1595 }
1596
1597 /* Set watchpoint B to disp_del_at_next_stop, even including its possible
1598 associated bp_watchpoint_scope breakpoint. */
1599
1600 static void
1601 watchpoint_del_at_next_stop (struct watchpoint *w)
1602 {
1603 struct breakpoint *b = &w->base;
1604
1605 if (b->related_breakpoint != b)
1606 {
1607 gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1608 gdb_assert (b->related_breakpoint->related_breakpoint == b);
1609 b->related_breakpoint->disposition = disp_del_at_next_stop;
1610 b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1611 b->related_breakpoint = b;
1612 }
1613 b->disposition = disp_del_at_next_stop;
1614 }
1615
1616 /* Assuming that B is a watchpoint:
1617 - Reparse watchpoint expression, if REPARSE is non-zero
1618 - Evaluate expression and store the result in B->val
1619 - Evaluate the condition if there is one, and store the result
1620 in b->loc->cond.
1621 - Update the list of values that must be watched in B->loc.
1622
1623 If the watchpoint disposition is disp_del_at_next_stop, then do
1624 nothing. If this is local watchpoint that is out of scope, delete
1625 it.
1626
1627 Even with `set breakpoint always-inserted on' the watchpoints are
1628 removed + inserted on each stop here. Normal breakpoints must
1629 never be removed because they might be missed by a running thread
1630 when debugging in non-stop mode. On the other hand, hardware
1631 watchpoints (is_hardware_watchpoint; processed here) are specific
1632 to each LWP since they are stored in each LWP's hardware debug
1633 registers. Therefore, such LWP must be stopped first in order to
1634 be able to modify its hardware watchpoints.
1635
1636 Hardware watchpoints must be reset exactly once after being
1637 presented to the user. It cannot be done sooner, because it would
1638 reset the data used to present the watchpoint hit to the user. And
1639 it must not be done later because it could display the same single
1640 watchpoint hit during multiple GDB stops. Note that the latter is
1641 relevant only to the hardware watchpoint types bp_read_watchpoint
1642 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1643 not user-visible - its hit is suppressed if the memory content has
1644 not changed.
1645
1646 The following constraints influence the location where we can reset
1647 hardware watchpoints:
1648
1649 * target_stopped_by_watchpoint and target_stopped_data_address are
1650 called several times when GDB stops.
1651
1652 [linux]
1653 * Multiple hardware watchpoints can be hit at the same time,
1654 causing GDB to stop. GDB only presents one hardware watchpoint
1655 hit at a time as the reason for stopping, and all the other hits
1656 are presented later, one after the other, each time the user
1657 requests the execution to be resumed. Execution is not resumed
1658 for the threads still having pending hit event stored in
1659 LWP_INFO->STATUS. While the watchpoint is already removed from
1660 the inferior on the first stop the thread hit event is kept being
1661 reported from its cached value by linux_nat_stopped_data_address
1662 until the real thread resume happens after the watchpoint gets
1663 presented and thus its LWP_INFO->STATUS gets reset.
1664
1665 Therefore the hardware watchpoint hit can get safely reset on the
1666 watchpoint removal from inferior. */
1667
1668 static void
1669 update_watchpoint (struct watchpoint *b, int reparse)
1670 {
1671 int within_current_scope;
1672 struct frame_id saved_frame_id;
1673 int frame_saved;
1674
1675 /* If this is a local watchpoint, we only want to check if the
1676 watchpoint frame is in scope if the current thread is the thread
1677 that was used to create the watchpoint. */
1678 if (!watchpoint_in_thread_scope (b))
1679 return;
1680
1681 if (b->base.disposition == disp_del_at_next_stop)
1682 return;
1683
1684 frame_saved = 0;
1685
1686 /* Determine if the watchpoint is within scope. */
1687 if (b->exp_valid_block == NULL)
1688 within_current_scope = 1;
1689 else
1690 {
1691 struct frame_info *fi = get_current_frame ();
1692 struct gdbarch *frame_arch = get_frame_arch (fi);
1693 CORE_ADDR frame_pc = get_frame_pc (fi);
1694
1695 /* If we're in a function epilogue, unwinding may not work
1696 properly, so do not attempt to recreate locations at this
1697 point. See similar comments in watchpoint_check. */
1698 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
1699 return;
1700
1701 /* Save the current frame's ID so we can restore it after
1702 evaluating the watchpoint expression on its own frame. */
1703 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1704 took a frame parameter, so that we didn't have to change the
1705 selected frame. */
1706 frame_saved = 1;
1707 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1708
1709 fi = frame_find_by_id (b->watchpoint_frame);
1710 within_current_scope = (fi != NULL);
1711 if (within_current_scope)
1712 select_frame (fi);
1713 }
1714
1715 /* We don't free locations. They are stored in the bp_location array
1716 and update_global_location_list will eventually delete them and
1717 remove breakpoints if needed. */
1718 b->base.loc = NULL;
1719
1720 if (within_current_scope && reparse)
1721 {
1722 char *s;
1723
1724 if (b->exp)
1725 {
1726 xfree (b->exp);
1727 b->exp = NULL;
1728 }
1729 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1730 b->exp = parse_exp_1 (&s, b->exp_valid_block, 0);
1731 /* If the meaning of expression itself changed, the old value is
1732 no longer relevant. We don't want to report a watchpoint hit
1733 to the user when the old value and the new value may actually
1734 be completely different objects. */
1735 value_free (b->val);
1736 b->val = NULL;
1737 b->val_valid = 0;
1738
1739 /* Note that unlike with breakpoints, the watchpoint's condition
1740 expression is stored in the breakpoint object, not in the
1741 locations (re)created below. */
1742 if (b->base.cond_string != NULL)
1743 {
1744 if (b->cond_exp != NULL)
1745 {
1746 xfree (b->cond_exp);
1747 b->cond_exp = NULL;
1748 }
1749
1750 s = b->base.cond_string;
1751 b->cond_exp = parse_exp_1 (&s, b->cond_exp_valid_block, 0);
1752 }
1753 }
1754
1755 /* If we failed to parse the expression, for example because
1756 it refers to a global variable in a not-yet-loaded shared library,
1757 don't try to insert watchpoint. We don't automatically delete
1758 such watchpoint, though, since failure to parse expression
1759 is different from out-of-scope watchpoint. */
1760 if ( !target_has_execution)
1761 {
1762 /* Without execution, memory can't change. No use to try and
1763 set watchpoint locations. The watchpoint will be reset when
1764 the target gains execution, through breakpoint_re_set. */
1765 }
1766 else if (within_current_scope && b->exp)
1767 {
1768 int pc = 0;
1769 struct value *val_chain, *v, *result, *next;
1770 struct program_space *frame_pspace;
1771
1772 fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain);
1773
1774 /* Avoid setting b->val if it's already set. The meaning of
1775 b->val is 'the last value' user saw, and we should update
1776 it only if we reported that last value to user. As it
1777 happens, the code that reports it updates b->val directly.
1778 We don't keep track of the memory value for masked
1779 watchpoints. */
1780 if (!b->val_valid && !is_masked_watchpoint (&b->base))
1781 {
1782 b->val = v;
1783 b->val_valid = 1;
1784 }
1785
1786 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1787
1788 /* Look at each value on the value chain. */
1789 for (v = val_chain; v; v = value_next (v))
1790 {
1791 /* If it's a memory location, and GDB actually needed
1792 its contents to evaluate the expression, then we
1793 must watch it. If the first value returned is
1794 still lazy, that means an error occurred reading it;
1795 watch it anyway in case it becomes readable. */
1796 if (VALUE_LVAL (v) == lval_memory
1797 && (v == val_chain || ! value_lazy (v)))
1798 {
1799 struct type *vtype = check_typedef (value_type (v));
1800
1801 /* We only watch structs and arrays if user asked
1802 for it explicitly, never if they just happen to
1803 appear in the middle of some value chain. */
1804 if (v == result
1805 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1806 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1807 {
1808 CORE_ADDR addr;
1809 int len, type;
1810 struct bp_location *loc, **tmp;
1811
1812 addr = value_address (v);
1813 len = TYPE_LENGTH (value_type (v));
1814 type = hw_write;
1815 if (b->base.type == bp_read_watchpoint)
1816 type = hw_read;
1817 else if (b->base.type == bp_access_watchpoint)
1818 type = hw_access;
1819
1820 loc = allocate_bp_location (&b->base);
1821 for (tmp = &(b->base.loc); *tmp != NULL; tmp = &((*tmp)->next))
1822 ;
1823 *tmp = loc;
1824 loc->gdbarch = get_type_arch (value_type (v));
1825
1826 loc->pspace = frame_pspace;
1827 loc->address = addr;
1828 loc->length = len;
1829 loc->watchpoint_type = type;
1830 }
1831 }
1832 }
1833
1834 /* Change the type of breakpoint between hardware assisted or
1835 an ordinary watchpoint depending on the hardware support
1836 and free hardware slots. REPARSE is set when the inferior
1837 is started. */
1838 if (reparse)
1839 {
1840 int reg_cnt;
1841 enum bp_loc_type loc_type;
1842 struct bp_location *bl;
1843
1844 reg_cnt = can_use_hardware_watchpoint (val_chain);
1845
1846 if (reg_cnt)
1847 {
1848 int i, target_resources_ok, other_type_used;
1849 enum bptype type;
1850
1851 /* Use an exact watchpoint when there's only one memory region to be
1852 watched, and only one debug register is needed to watch it. */
1853 b->exact = target_exact_watchpoints && reg_cnt == 1;
1854
1855 /* We need to determine how many resources are already
1856 used for all other hardware watchpoints plus this one
1857 to see if we still have enough resources to also fit
1858 this watchpoint in as well. */
1859
1860 /* If this is a software watchpoint, we try to turn it
1861 to a hardware one -- count resources as if B was of
1862 hardware watchpoint type. */
1863 type = b->base.type;
1864 if (type == bp_watchpoint)
1865 type = bp_hardware_watchpoint;
1866
1867 /* This watchpoint may or may not have been placed on
1868 the list yet at this point (it won't be in the list
1869 if we're trying to create it for the first time,
1870 through watch_command), so always account for it
1871 manually. */
1872
1873 /* Count resources used by all watchpoints except B. */
1874 i = hw_watchpoint_used_count_others (&b->base, type, &other_type_used);
1875
1876 /* Add in the resources needed for B. */
1877 i += hw_watchpoint_use_count (&b->base);
1878
1879 target_resources_ok
1880 = target_can_use_hardware_watchpoint (type, i, other_type_used);
1881 if (target_resources_ok <= 0)
1882 {
1883 int sw_mode = b->base.ops->works_in_software_mode (&b->base);
1884
1885 if (target_resources_ok == 0 && !sw_mode)
1886 error (_("Target does not support this type of "
1887 "hardware watchpoint."));
1888 else if (target_resources_ok < 0 && !sw_mode)
1889 error (_("There are not enough available hardware "
1890 "resources for this watchpoint."));
1891
1892 /* Downgrade to software watchpoint. */
1893 b->base.type = bp_watchpoint;
1894 }
1895 else
1896 {
1897 /* If this was a software watchpoint, we've just
1898 found we have enough resources to turn it to a
1899 hardware watchpoint. Otherwise, this is a
1900 nop. */
1901 b->base.type = type;
1902 }
1903 }
1904 else if (!b->base.ops->works_in_software_mode (&b->base))
1905 error (_("Expression cannot be implemented with "
1906 "read/access watchpoint."));
1907 else
1908 b->base.type = bp_watchpoint;
1909
1910 loc_type = (b->base.type == bp_watchpoint? bp_loc_other
1911 : bp_loc_hardware_watchpoint);
1912 for (bl = b->base.loc; bl; bl = bl->next)
1913 bl->loc_type = loc_type;
1914 }
1915
1916 for (v = val_chain; v; v = next)
1917 {
1918 next = value_next (v);
1919 if (v != b->val)
1920 value_free (v);
1921 }
1922
1923 /* If a software watchpoint is not watching any memory, then the
1924 above left it without any location set up. But,
1925 bpstat_stop_status requires a location to be able to report
1926 stops, so make sure there's at least a dummy one. */
1927 if (b->base.type == bp_watchpoint && b->base.loc == NULL)
1928 {
1929 struct breakpoint *base = &b->base;
1930 base->loc = allocate_bp_location (base);
1931 base->loc->pspace = frame_pspace;
1932 base->loc->address = -1;
1933 base->loc->length = -1;
1934 base->loc->watchpoint_type = -1;
1935 }
1936 }
1937 else if (!within_current_scope)
1938 {
1939 printf_filtered (_("\
1940 Watchpoint %d deleted because the program has left the block\n\
1941 in which its expression is valid.\n"),
1942 b->base.number);
1943 watchpoint_del_at_next_stop (b);
1944 }
1945
1946 /* Restore the selected frame. */
1947 if (frame_saved)
1948 select_frame (frame_find_by_id (saved_frame_id));
1949 }
1950
1951
1952 /* Returns 1 iff breakpoint location should be
1953 inserted in the inferior. We don't differentiate the type of BL's owner
1954 (breakpoint vs. tracepoint), although insert_location in tracepoint's
1955 breakpoint_ops is not defined, because in insert_bp_location,
1956 tracepoint's insert_location will not be called. */
1957 static int
1958 should_be_inserted (struct bp_location *bl)
1959 {
1960 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
1961 return 0;
1962
1963 if (bl->owner->disposition == disp_del_at_next_stop)
1964 return 0;
1965
1966 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
1967 return 0;
1968
1969 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
1970 return 0;
1971
1972 /* This is set for example, when we're attached to the parent of a
1973 vfork, and have detached from the child. The child is running
1974 free, and we expect it to do an exec or exit, at which point the
1975 OS makes the parent schedulable again (and the target reports
1976 that the vfork is done). Until the child is done with the shared
1977 memory region, do not insert breakpoints in the parent, otherwise
1978 the child could still trip on the parent's breakpoints. Since
1979 the parent is blocked anyway, it won't miss any breakpoint. */
1980 if (bl->pspace->breakpoints_not_allowed)
1981 return 0;
1982
1983 return 1;
1984 }
1985
1986 /* Same as should_be_inserted but does the check assuming
1987 that the location is not duplicated. */
1988
1989 static int
1990 unduplicated_should_be_inserted (struct bp_location *bl)
1991 {
1992 int result;
1993 const int save_duplicate = bl->duplicate;
1994
1995 bl->duplicate = 0;
1996 result = should_be_inserted (bl);
1997 bl->duplicate = save_duplicate;
1998 return result;
1999 }
2000
2001 /* Parses a conditional described by an expression COND into an
2002 agent expression bytecode suitable for evaluation
2003 by the bytecode interpreter. Return NULL if there was
2004 any error during parsing. */
2005
2006 static struct agent_expr *
2007 parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2008 {
2009 struct agent_expr *aexpr = NULL;
2010 struct cleanup *old_chain = NULL;
2011 volatile struct gdb_exception ex;
2012
2013 if (!cond)
2014 return NULL;
2015
2016 /* We don't want to stop processing, so catch any errors
2017 that may show up. */
2018 TRY_CATCH (ex, RETURN_MASK_ERROR)
2019 {
2020 aexpr = gen_eval_for_expr (scope, cond);
2021 }
2022
2023 if (ex.reason < 0)
2024 {
2025 /* If we got here, it means the condition could not be parsed to a valid
2026 bytecode expression and thus can't be evaluated on the target's side.
2027 It's no use iterating through the conditions. */
2028 return NULL;
2029 }
2030
2031 /* We have a valid agent expression. */
2032 return aexpr;
2033 }
2034
2035 /* Based on location BL, create a list of breakpoint conditions to be
2036 passed on to the target. If we have duplicated locations with different
2037 conditions, we will add such conditions to the list. The idea is that the
2038 target will evaluate the list of conditions and will only notify GDB when
2039 one of them is true. */
2040
2041 static void
2042 build_target_condition_list (struct bp_location *bl)
2043 {
2044 struct bp_location **locp = NULL, **loc2p;
2045 int null_condition_or_parse_error = 0;
2046 int modified = bl->needs_update;
2047 struct bp_location *loc;
2048
2049 /* This is only meaningful if the target is
2050 evaluating conditions and if the user has
2051 opted for condition evaluation on the target's
2052 side. */
2053 if (gdb_evaluates_breakpoint_condition_p ()
2054 || !target_supports_evaluation_of_breakpoint_conditions ())
2055 return;
2056
2057 /* Do a first pass to check for locations with no assigned
2058 conditions or conditions that fail to parse to a valid agent expression
2059 bytecode. If any of these happen, then it's no use to send conditions
2060 to the target since this location will always trigger and generate a
2061 response back to GDB. */
2062 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2063 {
2064 loc = (*loc2p);
2065 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2066 {
2067 if (modified)
2068 {
2069 struct agent_expr *aexpr;
2070
2071 /* Re-parse the conditions since something changed. In that
2072 case we already freed the condition bytecodes (see
2073 force_breakpoint_reinsertion). We just
2074 need to parse the condition to bytecodes again. */
2075 aexpr = parse_cond_to_aexpr (bl->address, loc->cond);
2076 loc->cond_bytecode = aexpr;
2077
2078 /* Check if we managed to parse the conditional expression
2079 correctly. If not, we will not send this condition
2080 to the target. */
2081 if (aexpr)
2082 continue;
2083 }
2084
2085 /* If we have a NULL bytecode expression, it means something
2086 went wrong or we have a null condition expression. */
2087 if (!loc->cond_bytecode)
2088 {
2089 null_condition_or_parse_error = 1;
2090 break;
2091 }
2092 }
2093 }
2094
2095 /* If any of these happened, it means we will have to evaluate the conditions
2096 for the location's address on gdb's side. It is no use keeping bytecodes
2097 for all the other duplicate locations, thus we free all of them here.
2098
2099 This is so we have a finer control over which locations' conditions are
2100 being evaluated by GDB or the remote stub. */
2101 if (null_condition_or_parse_error)
2102 {
2103 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2104 {
2105 loc = (*loc2p);
2106 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2107 {
2108 /* Only go as far as the first NULL bytecode is
2109 located. */
2110 if (!loc->cond_bytecode)
2111 return;
2112
2113 free_agent_expr (loc->cond_bytecode);
2114 loc->cond_bytecode = NULL;
2115 }
2116 }
2117 }
2118
2119 /* No NULL conditions or failed bytecode generation. Build a condition list
2120 for this location's address. */
2121 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2122 {
2123 loc = (*loc2p);
2124 if (loc->cond
2125 && is_breakpoint (loc->owner)
2126 && loc->pspace->num == bl->pspace->num
2127 && loc->owner->enable_state == bp_enabled
2128 && loc->enabled)
2129 /* Add the condition to the vector. This will be used later to send the
2130 conditions to the target. */
2131 VEC_safe_push (agent_expr_p, bl->target_info.conditions,
2132 loc->cond_bytecode);
2133 }
2134
2135 return;
2136 }
2137
2138 /* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2139 location. Any error messages are printed to TMP_ERROR_STREAM; and
2140 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
2141 Returns 0 for success, 1 if the bp_location type is not supported or
2142 -1 for failure.
2143
2144 NOTE drow/2003-09-09: This routine could be broken down to an
2145 object-style method for each breakpoint or catchpoint type. */
2146 static int
2147 insert_bp_location (struct bp_location *bl,
2148 struct ui_file *tmp_error_stream,
2149 int *disabled_breaks,
2150 int *hw_breakpoint_error)
2151 {
2152 int val = 0;
2153
2154 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2155 return 0;
2156
2157 /* Note we don't initialize bl->target_info, as that wipes out
2158 the breakpoint location's shadow_contents if the breakpoint
2159 is still inserted at that location. This in turn breaks
2160 target_read_memory which depends on these buffers when
2161 a memory read is requested at the breakpoint location:
2162 Once the target_info has been wiped, we fail to see that
2163 we have a breakpoint inserted at that address and thus
2164 read the breakpoint instead of returning the data saved in
2165 the breakpoint location's shadow contents. */
2166 bl->target_info.placed_address = bl->address;
2167 bl->target_info.placed_address_space = bl->pspace->aspace;
2168 bl->target_info.length = bl->length;
2169
2170 /* When working with target-side conditions, we must pass all the conditions
2171 for the same breakpoint address down to the target since GDB will not
2172 insert those locations. With a list of breakpoint conditions, the target
2173 can decide when to stop and notify GDB. */
2174
2175 if (is_breakpoint (bl->owner))
2176 {
2177 build_target_condition_list (bl);
2178 /* Reset the condition modification marker. */
2179 bl->needs_update = 0;
2180 }
2181
2182 if (bl->loc_type == bp_loc_software_breakpoint
2183 || bl->loc_type == bp_loc_hardware_breakpoint)
2184 {
2185 if (bl->owner->type != bp_hardware_breakpoint)
2186 {
2187 /* If the explicitly specified breakpoint type
2188 is not hardware breakpoint, check the memory map to see
2189 if the breakpoint address is in read only memory or not.
2190
2191 Two important cases are:
2192 - location type is not hardware breakpoint, memory
2193 is readonly. We change the type of the location to
2194 hardware breakpoint.
2195 - location type is hardware breakpoint, memory is
2196 read-write. This means we've previously made the
2197 location hardware one, but then the memory map changed,
2198 so we undo.
2199
2200 When breakpoints are removed, remove_breakpoints will use
2201 location types we've just set here, the only possible
2202 problem is that memory map has changed during running
2203 program, but it's not going to work anyway with current
2204 gdb. */
2205 struct mem_region *mr
2206 = lookup_mem_region (bl->target_info.placed_address);
2207
2208 if (mr)
2209 {
2210 if (automatic_hardware_breakpoints)
2211 {
2212 enum bp_loc_type new_type;
2213
2214 if (mr->attrib.mode != MEM_RW)
2215 new_type = bp_loc_hardware_breakpoint;
2216 else
2217 new_type = bp_loc_software_breakpoint;
2218
2219 if (new_type != bl->loc_type)
2220 {
2221 static int said = 0;
2222
2223 bl->loc_type = new_type;
2224 if (!said)
2225 {
2226 fprintf_filtered (gdb_stdout,
2227 _("Note: automatically using "
2228 "hardware breakpoints for "
2229 "read-only addresses.\n"));
2230 said = 1;
2231 }
2232 }
2233 }
2234 else if (bl->loc_type == bp_loc_software_breakpoint
2235 && mr->attrib.mode != MEM_RW)
2236 warning (_("cannot set software breakpoint "
2237 "at readonly address %s"),
2238 paddress (bl->gdbarch, bl->address));
2239 }
2240 }
2241
2242 /* First check to see if we have to handle an overlay. */
2243 if (overlay_debugging == ovly_off
2244 || bl->section == NULL
2245 || !(section_is_overlay (bl->section)))
2246 {
2247 /* No overlay handling: just set the breakpoint. */
2248
2249 val = bl->owner->ops->insert_location (bl);
2250 }
2251 else
2252 {
2253 /* This breakpoint is in an overlay section.
2254 Shall we set a breakpoint at the LMA? */
2255 if (!overlay_events_enabled)
2256 {
2257 /* Yes -- overlay event support is not active,
2258 so we must try to set a breakpoint at the LMA.
2259 This will not work for a hardware breakpoint. */
2260 if (bl->loc_type == bp_loc_hardware_breakpoint)
2261 warning (_("hardware breakpoint %d not supported in overlay!"),
2262 bl->owner->number);
2263 else
2264 {
2265 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2266 bl->section);
2267 /* Set a software (trap) breakpoint at the LMA. */
2268 bl->overlay_target_info = bl->target_info;
2269 bl->overlay_target_info.placed_address = addr;
2270 val = target_insert_breakpoint (bl->gdbarch,
2271 &bl->overlay_target_info);
2272 if (val != 0)
2273 fprintf_unfiltered (tmp_error_stream,
2274 "Overlay breakpoint %d "
2275 "failed: in ROM?\n",
2276 bl->owner->number);
2277 }
2278 }
2279 /* Shall we set a breakpoint at the VMA? */
2280 if (section_is_mapped (bl->section))
2281 {
2282 /* Yes. This overlay section is mapped into memory. */
2283 val = bl->owner->ops->insert_location (bl);
2284 }
2285 else
2286 {
2287 /* No. This breakpoint will not be inserted.
2288 No error, but do not mark the bp as 'inserted'. */
2289 return 0;
2290 }
2291 }
2292
2293 if (val)
2294 {
2295 /* Can't set the breakpoint. */
2296 if (solib_name_from_address (bl->pspace, bl->address))
2297 {
2298 /* See also: disable_breakpoints_in_shlibs. */
2299 val = 0;
2300 bl->shlib_disabled = 1;
2301 observer_notify_breakpoint_modified (bl->owner);
2302 if (!*disabled_breaks)
2303 {
2304 fprintf_unfiltered (tmp_error_stream,
2305 "Cannot insert breakpoint %d.\n",
2306 bl->owner->number);
2307 fprintf_unfiltered (tmp_error_stream,
2308 "Temporarily disabling shared "
2309 "library breakpoints:\n");
2310 }
2311 *disabled_breaks = 1;
2312 fprintf_unfiltered (tmp_error_stream,
2313 "breakpoint #%d\n", bl->owner->number);
2314 }
2315 else
2316 {
2317 if (bl->loc_type == bp_loc_hardware_breakpoint)
2318 {
2319 *hw_breakpoint_error = 1;
2320 fprintf_unfiltered (tmp_error_stream,
2321 "Cannot insert hardware "
2322 "breakpoint %d.\n",
2323 bl->owner->number);
2324 }
2325 else
2326 {
2327 fprintf_unfiltered (tmp_error_stream,
2328 "Cannot insert breakpoint %d.\n",
2329 bl->owner->number);
2330 fprintf_filtered (tmp_error_stream,
2331 "Error accessing memory address ");
2332 fputs_filtered (paddress (bl->gdbarch, bl->address),
2333 tmp_error_stream);
2334 fprintf_filtered (tmp_error_stream, ": %s.\n",
2335 safe_strerror (val));
2336 }
2337
2338 }
2339 }
2340 else
2341 bl->inserted = 1;
2342
2343 return val;
2344 }
2345
2346 else if (bl->loc_type == bp_loc_hardware_watchpoint
2347 /* NOTE drow/2003-09-08: This state only exists for removing
2348 watchpoints. It's not clear that it's necessary... */
2349 && bl->owner->disposition != disp_del_at_next_stop)
2350 {
2351 gdb_assert (bl->owner->ops != NULL
2352 && bl->owner->ops->insert_location != NULL);
2353
2354 val = bl->owner->ops->insert_location (bl);
2355
2356 /* If trying to set a read-watchpoint, and it turns out it's not
2357 supported, try emulating one with an access watchpoint. */
2358 if (val == 1 && bl->watchpoint_type == hw_read)
2359 {
2360 struct bp_location *loc, **loc_temp;
2361
2362 /* But don't try to insert it, if there's already another
2363 hw_access location that would be considered a duplicate
2364 of this one. */
2365 ALL_BP_LOCATIONS (loc, loc_temp)
2366 if (loc != bl
2367 && loc->watchpoint_type == hw_access
2368 && watchpoint_locations_match (bl, loc))
2369 {
2370 bl->duplicate = 1;
2371 bl->inserted = 1;
2372 bl->target_info = loc->target_info;
2373 bl->watchpoint_type = hw_access;
2374 val = 0;
2375 break;
2376 }
2377
2378 if (val == 1)
2379 {
2380 bl->watchpoint_type = hw_access;
2381 val = bl->owner->ops->insert_location (bl);
2382
2383 if (val)
2384 /* Back to the original value. */
2385 bl->watchpoint_type = hw_read;
2386 }
2387 }
2388
2389 bl->inserted = (val == 0);
2390 }
2391
2392 else if (bl->owner->type == bp_catchpoint)
2393 {
2394 gdb_assert (bl->owner->ops != NULL
2395 && bl->owner->ops->insert_location != NULL);
2396
2397 val = bl->owner->ops->insert_location (bl);
2398 if (val)
2399 {
2400 bl->owner->enable_state = bp_disabled;
2401
2402 if (val == 1)
2403 warning (_("\
2404 Error inserting catchpoint %d: Your system does not support this type\n\
2405 of catchpoint."), bl->owner->number);
2406 else
2407 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2408 }
2409
2410 bl->inserted = (val == 0);
2411
2412 /* We've already printed an error message if there was a problem
2413 inserting this catchpoint, and we've disabled the catchpoint,
2414 so just return success. */
2415 return 0;
2416 }
2417
2418 return 0;
2419 }
2420
2421 /* This function is called when program space PSPACE is about to be
2422 deleted. It takes care of updating breakpoints to not reference
2423 PSPACE anymore. */
2424
2425 void
2426 breakpoint_program_space_exit (struct program_space *pspace)
2427 {
2428 struct breakpoint *b, *b_temp;
2429 struct bp_location *loc, **loc_temp;
2430
2431 /* Remove any breakpoint that was set through this program space. */
2432 ALL_BREAKPOINTS_SAFE (b, b_temp)
2433 {
2434 if (b->pspace == pspace)
2435 delete_breakpoint (b);
2436 }
2437
2438 /* Breakpoints set through other program spaces could have locations
2439 bound to PSPACE as well. Remove those. */
2440 ALL_BP_LOCATIONS (loc, loc_temp)
2441 {
2442 struct bp_location *tmp;
2443
2444 if (loc->pspace == pspace)
2445 {
2446 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
2447 if (loc->owner->loc == loc)
2448 loc->owner->loc = loc->next;
2449 else
2450 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2451 if (tmp->next == loc)
2452 {
2453 tmp->next = loc->next;
2454 break;
2455 }
2456 }
2457 }
2458
2459 /* Now update the global location list to permanently delete the
2460 removed locations above. */
2461 update_global_location_list (0);
2462 }
2463
2464 /* Make sure all breakpoints are inserted in inferior.
2465 Throws exception on any error.
2466 A breakpoint that is already inserted won't be inserted
2467 again, so calling this function twice is safe. */
2468 void
2469 insert_breakpoints (void)
2470 {
2471 struct breakpoint *bpt;
2472
2473 ALL_BREAKPOINTS (bpt)
2474 if (is_hardware_watchpoint (bpt))
2475 {
2476 struct watchpoint *w = (struct watchpoint *) bpt;
2477
2478 update_watchpoint (w, 0 /* don't reparse. */);
2479 }
2480
2481 update_global_location_list (1);
2482
2483 /* update_global_location_list does not insert breakpoints when
2484 always_inserted_mode is not enabled. Explicitly insert them
2485 now. */
2486 if (!breakpoints_always_inserted_mode ())
2487 insert_breakpoint_locations ();
2488 }
2489
2490 /* Invoke CALLBACK for each of bp_location. */
2491
2492 void
2493 iterate_over_bp_locations (walk_bp_location_callback callback)
2494 {
2495 struct bp_location *loc, **loc_tmp;
2496
2497 ALL_BP_LOCATIONS (loc, loc_tmp)
2498 {
2499 callback (loc, NULL);
2500 }
2501 }
2502
2503 /* This is used when we need to synch breakpoint conditions between GDB and the
2504 target. It is the case with deleting and disabling of breakpoints when using
2505 always-inserted mode. */
2506
2507 static void
2508 update_inserted_breakpoint_locations (void)
2509 {
2510 struct bp_location *bl, **blp_tmp;
2511 int error_flag = 0;
2512 int val = 0;
2513 int disabled_breaks = 0;
2514 int hw_breakpoint_error = 0;
2515
2516 struct ui_file *tmp_error_stream = mem_fileopen ();
2517 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
2518
2519 /* Explicitly mark the warning -- this will only be printed if
2520 there was an error. */
2521 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
2522
2523 save_current_space_and_thread ();
2524
2525 ALL_BP_LOCATIONS (bl, blp_tmp)
2526 {
2527 /* We only want to update software breakpoints and hardware
2528 breakpoints. */
2529 if (!is_breakpoint (bl->owner))
2530 continue;
2531
2532 /* We only want to update locations that are already inserted
2533 and need updating. This is to avoid unwanted insertion during
2534 deletion of breakpoints. */
2535 if (!bl->inserted || (bl->inserted && !bl->needs_update))
2536 continue;
2537
2538 switch_to_program_space_and_thread (bl->pspace);
2539
2540 /* For targets that support global breakpoints, there's no need
2541 to select an inferior to insert breakpoint to. In fact, even
2542 if we aren't attached to any process yet, we should still
2543 insert breakpoints. */
2544 if (!gdbarch_has_global_breakpoints (target_gdbarch)
2545 && ptid_equal (inferior_ptid, null_ptid))
2546 continue;
2547
2548 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
2549 &hw_breakpoint_error);
2550 if (val)
2551 error_flag = val;
2552 }
2553
2554 if (error_flag)
2555 {
2556 target_terminal_ours_for_output ();
2557 error_stream (tmp_error_stream);
2558 }
2559
2560 do_cleanups (cleanups);
2561 }
2562
2563 /* Used when starting or continuing the program. */
2564
2565 static void
2566 insert_breakpoint_locations (void)
2567 {
2568 struct breakpoint *bpt;
2569 struct bp_location *bl, **blp_tmp;
2570 int error_flag = 0;
2571 int val = 0;
2572 int disabled_breaks = 0;
2573 int hw_breakpoint_error = 0;
2574
2575 struct ui_file *tmp_error_stream = mem_fileopen ();
2576 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
2577
2578 /* Explicitly mark the warning -- this will only be printed if
2579 there was an error. */
2580 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
2581
2582 save_current_space_and_thread ();
2583
2584 ALL_BP_LOCATIONS (bl, blp_tmp)
2585 {
2586 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2587 continue;
2588
2589 /* There is no point inserting thread-specific breakpoints if
2590 the thread no longer exists. ALL_BP_LOCATIONS bp_location
2591 has BL->OWNER always non-NULL. */
2592 if (bl->owner->thread != -1
2593 && !valid_thread_id (bl->owner->thread))
2594 continue;
2595
2596 switch_to_program_space_and_thread (bl->pspace);
2597
2598 /* For targets that support global breakpoints, there's no need
2599 to select an inferior to insert breakpoint to. In fact, even
2600 if we aren't attached to any process yet, we should still
2601 insert breakpoints. */
2602 if (!gdbarch_has_global_breakpoints (target_gdbarch)
2603 && ptid_equal (inferior_ptid, null_ptid))
2604 continue;
2605
2606 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
2607 &hw_breakpoint_error);
2608 if (val)
2609 error_flag = val;
2610 }
2611
2612 /* If we failed to insert all locations of a watchpoint, remove
2613 them, as half-inserted watchpoint is of limited use. */
2614 ALL_BREAKPOINTS (bpt)
2615 {
2616 int some_failed = 0;
2617 struct bp_location *loc;
2618
2619 if (!is_hardware_watchpoint (bpt))
2620 continue;
2621
2622 if (!breakpoint_enabled (bpt))
2623 continue;
2624
2625 if (bpt->disposition == disp_del_at_next_stop)
2626 continue;
2627
2628 for (loc = bpt->loc; loc; loc = loc->next)
2629 if (!loc->inserted && should_be_inserted (loc))
2630 {
2631 some_failed = 1;
2632 break;
2633 }
2634 if (some_failed)
2635 {
2636 for (loc = bpt->loc; loc; loc = loc->next)
2637 if (loc->inserted)
2638 remove_breakpoint (loc, mark_uninserted);
2639
2640 hw_breakpoint_error = 1;
2641 fprintf_unfiltered (tmp_error_stream,
2642 "Could not insert hardware watchpoint %d.\n",
2643 bpt->number);
2644 error_flag = -1;
2645 }
2646 }
2647
2648 if (error_flag)
2649 {
2650 /* If a hardware breakpoint or watchpoint was inserted, add a
2651 message about possibly exhausted resources. */
2652 if (hw_breakpoint_error)
2653 {
2654 fprintf_unfiltered (tmp_error_stream,
2655 "Could not insert hardware breakpoints:\n\
2656 You may have requested too many hardware breakpoints/watchpoints.\n");
2657 }
2658 target_terminal_ours_for_output ();
2659 error_stream (tmp_error_stream);
2660 }
2661
2662 do_cleanups (cleanups);
2663 }
2664
2665 /* Used when the program stops.
2666 Returns zero if successful, or non-zero if there was a problem
2667 removing a breakpoint location. */
2668
2669 int
2670 remove_breakpoints (void)
2671 {
2672 struct bp_location *bl, **blp_tmp;
2673 int val = 0;
2674
2675 ALL_BP_LOCATIONS (bl, blp_tmp)
2676 {
2677 if (bl->inserted && !is_tracepoint (bl->owner))
2678 val |= remove_breakpoint (bl, mark_uninserted);
2679 }
2680 return val;
2681 }
2682
2683 /* Remove breakpoints of process PID. */
2684
2685 int
2686 remove_breakpoints_pid (int pid)
2687 {
2688 struct bp_location *bl, **blp_tmp;
2689 int val;
2690 struct inferior *inf = find_inferior_pid (pid);
2691
2692 ALL_BP_LOCATIONS (bl, blp_tmp)
2693 {
2694 if (bl->pspace != inf->pspace)
2695 continue;
2696
2697 if (bl->inserted)
2698 {
2699 val = remove_breakpoint (bl, mark_uninserted);
2700 if (val != 0)
2701 return val;
2702 }
2703 }
2704 return 0;
2705 }
2706
2707 int
2708 reattach_breakpoints (int pid)
2709 {
2710 struct cleanup *old_chain;
2711 struct bp_location *bl, **blp_tmp;
2712 int val;
2713 struct ui_file *tmp_error_stream;
2714 int dummy1 = 0, dummy2 = 0;
2715 struct inferior *inf;
2716 struct thread_info *tp;
2717
2718 tp = any_live_thread_of_process (pid);
2719 if (tp == NULL)
2720 return 1;
2721
2722 inf = find_inferior_pid (pid);
2723 old_chain = save_inferior_ptid ();
2724
2725 inferior_ptid = tp->ptid;
2726
2727 tmp_error_stream = mem_fileopen ();
2728 make_cleanup_ui_file_delete (tmp_error_stream);
2729
2730 ALL_BP_LOCATIONS (bl, blp_tmp)
2731 {
2732 if (bl->pspace != inf->pspace)
2733 continue;
2734
2735 if (bl->inserted)
2736 {
2737 bl->inserted = 0;
2738 val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2);
2739 if (val != 0)
2740 {
2741 do_cleanups (old_chain);
2742 return val;
2743 }
2744 }
2745 }
2746 do_cleanups (old_chain);
2747 return 0;
2748 }
2749
2750 static int internal_breakpoint_number = -1;
2751
2752 /* Set the breakpoint number of B, depending on the value of INTERNAL.
2753 If INTERNAL is non-zero, the breakpoint number will be populated
2754 from internal_breakpoint_number and that variable decremented.
2755 Otherwise the breakpoint number will be populated from
2756 breakpoint_count and that value incremented. Internal breakpoints
2757 do not set the internal var bpnum. */
2758 static void
2759 set_breakpoint_number (int internal, struct breakpoint *b)
2760 {
2761 if (internal)
2762 b->number = internal_breakpoint_number--;
2763 else
2764 {
2765 set_breakpoint_count (breakpoint_count + 1);
2766 b->number = breakpoint_count;
2767 }
2768 }
2769
2770 static struct breakpoint *
2771 create_internal_breakpoint (struct gdbarch *gdbarch,
2772 CORE_ADDR address, enum bptype type,
2773 const struct breakpoint_ops *ops)
2774 {
2775 struct symtab_and_line sal;
2776 struct breakpoint *b;
2777
2778 init_sal (&sal); /* Initialize to zeroes. */
2779
2780 sal.pc = address;
2781 sal.section = find_pc_overlay (sal.pc);
2782 sal.pspace = current_program_space;
2783
2784 b = set_raw_breakpoint (gdbarch, sal, type, ops);
2785 b->number = internal_breakpoint_number--;
2786 b->disposition = disp_donttouch;
2787
2788 return b;
2789 }
2790
2791 static const char *const longjmp_names[] =
2792 {
2793 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
2794 };
2795 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
2796
2797 /* Per-objfile data private to breakpoint.c. */
2798 struct breakpoint_objfile_data
2799 {
2800 /* Minimal symbol for "_ovly_debug_event" (if any). */
2801 struct minimal_symbol *overlay_msym;
2802
2803 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
2804 struct minimal_symbol *longjmp_msym[NUM_LONGJMP_NAMES];
2805
2806 /* True if we have looked for longjmp probes. */
2807 int longjmp_searched;
2808
2809 /* SystemTap probe points for longjmp (if any). */
2810 VEC (probe_p) *longjmp_probes;
2811
2812 /* Minimal symbol for "std::terminate()" (if any). */
2813 struct minimal_symbol *terminate_msym;
2814
2815 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
2816 struct minimal_symbol *exception_msym;
2817
2818 /* True if we have looked for exception probes. */
2819 int exception_searched;
2820
2821 /* SystemTap probe points for unwinding (if any). */
2822 VEC (probe_p) *exception_probes;
2823 };
2824
2825 static const struct objfile_data *breakpoint_objfile_key;
2826
2827 /* Minimal symbol not found sentinel. */
2828 static struct minimal_symbol msym_not_found;
2829
2830 /* Returns TRUE if MSYM point to the "not found" sentinel. */
2831
2832 static int
2833 msym_not_found_p (const struct minimal_symbol *msym)
2834 {
2835 return msym == &msym_not_found;
2836 }
2837
2838 /* Return per-objfile data needed by breakpoint.c.
2839 Allocate the data if necessary. */
2840
2841 static struct breakpoint_objfile_data *
2842 get_breakpoint_objfile_data (struct objfile *objfile)
2843 {
2844 struct breakpoint_objfile_data *bp_objfile_data;
2845
2846 bp_objfile_data = objfile_data (objfile, breakpoint_objfile_key);
2847 if (bp_objfile_data == NULL)
2848 {
2849 bp_objfile_data = obstack_alloc (&objfile->objfile_obstack,
2850 sizeof (*bp_objfile_data));
2851
2852 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
2853 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
2854 }
2855 return bp_objfile_data;
2856 }
2857
2858 static void
2859 free_breakpoint_probes (struct objfile *obj, void *data)
2860 {
2861 struct breakpoint_objfile_data *bp_objfile_data = data;
2862
2863 VEC_free (probe_p, bp_objfile_data->longjmp_probes);
2864 VEC_free (probe_p, bp_objfile_data->exception_probes);
2865 }
2866
2867 static void
2868 create_overlay_event_breakpoint (void)
2869 {
2870 struct objfile *objfile;
2871 const char *const func_name = "_ovly_debug_event";
2872
2873 ALL_OBJFILES (objfile)
2874 {
2875 struct breakpoint *b;
2876 struct breakpoint_objfile_data *bp_objfile_data;
2877 CORE_ADDR addr;
2878
2879 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2880
2881 if (msym_not_found_p (bp_objfile_data->overlay_msym))
2882 continue;
2883
2884 if (bp_objfile_data->overlay_msym == NULL)
2885 {
2886 struct minimal_symbol *m;
2887
2888 m = lookup_minimal_symbol_text (func_name, objfile);
2889 if (m == NULL)
2890 {
2891 /* Avoid future lookups in this objfile. */
2892 bp_objfile_data->overlay_msym = &msym_not_found;
2893 continue;
2894 }
2895 bp_objfile_data->overlay_msym = m;
2896 }
2897
2898 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
2899 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
2900 bp_overlay_event,
2901 &internal_breakpoint_ops);
2902 b->addr_string = xstrdup (func_name);
2903
2904 if (overlay_debugging == ovly_auto)
2905 {
2906 b->enable_state = bp_enabled;
2907 overlay_events_enabled = 1;
2908 }
2909 else
2910 {
2911 b->enable_state = bp_disabled;
2912 overlay_events_enabled = 0;
2913 }
2914 }
2915 update_global_location_list (1);
2916 }
2917
2918 static void
2919 create_longjmp_master_breakpoint (void)
2920 {
2921 struct program_space *pspace;
2922 struct cleanup *old_chain;
2923
2924 old_chain = save_current_program_space ();
2925
2926 ALL_PSPACES (pspace)
2927 {
2928 struct objfile *objfile;
2929
2930 set_current_program_space (pspace);
2931
2932 ALL_OBJFILES (objfile)
2933 {
2934 int i;
2935 struct gdbarch *gdbarch;
2936 struct breakpoint_objfile_data *bp_objfile_data;
2937
2938 gdbarch = get_objfile_arch (objfile);
2939 if (!gdbarch_get_longjmp_target_p (gdbarch))
2940 continue;
2941
2942 bp_objfile_data = get_breakpoint_objfile_data (objfile);
2943
2944 if (!bp_objfile_data->longjmp_searched)
2945 {
2946 bp_objfile_data->longjmp_probes
2947 = find_probes_in_objfile (objfile, "libc", "longjmp");
2948 bp_objfile_data->longjmp_searched = 1;
2949 }
2950
2951 if (bp_objfile_data->longjmp_probes != NULL)
2952 {
2953 int i;
2954 struct probe *probe;
2955 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2956
2957 for (i = 0;
2958 VEC_iterate (probe_p,
2959 bp_objfile_data->longjmp_probes,
2960 i, probe);
2961 ++i)
2962 {
2963 struct breakpoint *b;
2964
2965 b = create_internal_breakpoint (gdbarch, probe->address,
2966 bp_longjmp_master,
2967 &internal_breakpoint_ops);
2968 b->addr_string = xstrdup ("-probe-stap libc:longjmp");
2969 b->enable_state = bp_disabled;
2970 }
2971
2972 continue;
2973 }
2974
2975 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
2976 {
2977 struct breakpoint *b;
2978 const char *func_name;
2979 CORE_ADDR addr;
2980
2981 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i]))
2982 continue;
2983
2984 func_name = longjmp_names[i];
2985 if (bp_objfile_data->longjmp_msym[i] == NULL)
2986 {
2987 struct minimal_symbol *m;
2988
2989 m = lookup_minimal_symbol_text (func_name, objfile);
2990 if (m == NULL)
2991 {
2992 /* Prevent future lookups in this objfile. */
2993 bp_objfile_data->longjmp_msym[i] = &msym_not_found;
2994 continue;
2995 }
2996 bp_objfile_data->longjmp_msym[i] = m;
2997 }
2998
2999 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
3000 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3001 &internal_breakpoint_ops);
3002 b->addr_string = xstrdup (func_name);
3003 b->enable_state = bp_disabled;
3004 }
3005 }
3006 }
3007 update_global_location_list (1);
3008
3009 do_cleanups (old_chain);
3010 }
3011
3012 /* Create a master std::terminate breakpoint. */
3013 static void
3014 create_std_terminate_master_breakpoint (void)
3015 {
3016 struct program_space *pspace;
3017 struct cleanup *old_chain;
3018 const char *const func_name = "std::terminate()";
3019
3020 old_chain = save_current_program_space ();
3021
3022 ALL_PSPACES (pspace)
3023 {
3024 struct objfile *objfile;
3025 CORE_ADDR addr;
3026
3027 set_current_program_space (pspace);
3028
3029 ALL_OBJFILES (objfile)
3030 {
3031 struct breakpoint *b;
3032 struct breakpoint_objfile_data *bp_objfile_data;
3033
3034 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3035
3036 if (msym_not_found_p (bp_objfile_data->terminate_msym))
3037 continue;
3038
3039 if (bp_objfile_data->terminate_msym == NULL)
3040 {
3041 struct minimal_symbol *m;
3042
3043 m = lookup_minimal_symbol (func_name, NULL, objfile);
3044 if (m == NULL || (MSYMBOL_TYPE (m) != mst_text
3045 && MSYMBOL_TYPE (m) != mst_file_text))
3046 {
3047 /* Prevent future lookups in this objfile. */
3048 bp_objfile_data->terminate_msym = &msym_not_found;
3049 continue;
3050 }
3051 bp_objfile_data->terminate_msym = m;
3052 }
3053
3054 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3055 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3056 bp_std_terminate_master,
3057 &internal_breakpoint_ops);
3058 b->addr_string = xstrdup (func_name);
3059 b->enable_state = bp_disabled;
3060 }
3061 }
3062
3063 update_global_location_list (1);
3064
3065 do_cleanups (old_chain);
3066 }
3067
3068 /* Install a master breakpoint on the unwinder's debug hook. */
3069
3070 static void
3071 create_exception_master_breakpoint (void)
3072 {
3073 struct objfile *objfile;
3074 const char *const func_name = "_Unwind_DebugHook";
3075
3076 ALL_OBJFILES (objfile)
3077 {
3078 struct breakpoint *b;
3079 struct gdbarch *gdbarch;
3080 struct breakpoint_objfile_data *bp_objfile_data;
3081 CORE_ADDR addr;
3082
3083 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3084
3085 /* We prefer the SystemTap probe point if it exists. */
3086 if (!bp_objfile_data->exception_searched)
3087 {
3088 bp_objfile_data->exception_probes
3089 = find_probes_in_objfile (objfile, "libgcc", "unwind");
3090 bp_objfile_data->exception_searched = 1;
3091 }
3092
3093 if (bp_objfile_data->exception_probes != NULL)
3094 {
3095 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3096 int i;
3097 struct probe *probe;
3098
3099 for (i = 0;
3100 VEC_iterate (probe_p,
3101 bp_objfile_data->exception_probes,
3102 i, probe);
3103 ++i)
3104 {
3105 struct breakpoint *b;
3106
3107 b = create_internal_breakpoint (gdbarch, probe->address,
3108 bp_exception_master,
3109 &internal_breakpoint_ops);
3110 b->addr_string = xstrdup ("-probe-stap libgcc:unwind");
3111 b->enable_state = bp_disabled;
3112 }
3113
3114 continue;
3115 }
3116
3117 /* Otherwise, try the hook function. */
3118
3119 if (msym_not_found_p (bp_objfile_data->exception_msym))
3120 continue;
3121
3122 gdbarch = get_objfile_arch (objfile);
3123
3124 if (bp_objfile_data->exception_msym == NULL)
3125 {
3126 struct minimal_symbol *debug_hook;
3127
3128 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3129 if (debug_hook == NULL)
3130 {
3131 bp_objfile_data->exception_msym = &msym_not_found;
3132 continue;
3133 }
3134
3135 bp_objfile_data->exception_msym = debug_hook;
3136 }
3137
3138 addr = SYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
3139 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3140 &current_target);
3141 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3142 &internal_breakpoint_ops);
3143 b->addr_string = xstrdup (func_name);
3144 b->enable_state = bp_disabled;
3145 }
3146
3147 update_global_location_list (1);
3148 }
3149
3150 void
3151 update_breakpoints_after_exec (void)
3152 {
3153 struct breakpoint *b, *b_tmp;
3154 struct bp_location *bploc, **bplocp_tmp;
3155
3156 /* We're about to delete breakpoints from GDB's lists. If the
3157 INSERTED flag is true, GDB will try to lift the breakpoints by
3158 writing the breakpoints' "shadow contents" back into memory. The
3159 "shadow contents" are NOT valid after an exec, so GDB should not
3160 do that. Instead, the target is responsible from marking
3161 breakpoints out as soon as it detects an exec. We don't do that
3162 here instead, because there may be other attempts to delete
3163 breakpoints after detecting an exec and before reaching here. */
3164 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
3165 if (bploc->pspace == current_program_space)
3166 gdb_assert (!bploc->inserted);
3167
3168 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3169 {
3170 if (b->pspace != current_program_space)
3171 continue;
3172
3173 /* Solib breakpoints must be explicitly reset after an exec(). */
3174 if (b->type == bp_shlib_event)
3175 {
3176 delete_breakpoint (b);
3177 continue;
3178 }
3179
3180 /* JIT breakpoints must be explicitly reset after an exec(). */
3181 if (b->type == bp_jit_event)
3182 {
3183 delete_breakpoint (b);
3184 continue;
3185 }
3186
3187 /* Thread event breakpoints must be set anew after an exec(),
3188 as must overlay event and longjmp master breakpoints. */
3189 if (b->type == bp_thread_event || b->type == bp_overlay_event
3190 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3191 || b->type == bp_exception_master)
3192 {
3193 delete_breakpoint (b);
3194 continue;
3195 }
3196
3197 /* Step-resume breakpoints are meaningless after an exec(). */
3198 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3199 {
3200 delete_breakpoint (b);
3201 continue;
3202 }
3203
3204 /* Longjmp and longjmp-resume breakpoints are also meaningless
3205 after an exec. */
3206 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3207 || b->type == bp_exception || b->type == bp_exception_resume)
3208 {
3209 delete_breakpoint (b);
3210 continue;
3211 }
3212
3213 if (b->type == bp_catchpoint)
3214 {
3215 /* For now, none of the bp_catchpoint breakpoints need to
3216 do anything at this point. In the future, if some of
3217 the catchpoints need to something, we will need to add
3218 a new method, and call this method from here. */
3219 continue;
3220 }
3221
3222 /* bp_finish is a special case. The only way we ought to be able
3223 to see one of these when an exec() has happened, is if the user
3224 caught a vfork, and then said "finish". Ordinarily a finish just
3225 carries them to the call-site of the current callee, by setting
3226 a temporary bp there and resuming. But in this case, the finish
3227 will carry them entirely through the vfork & exec.
3228
3229 We don't want to allow a bp_finish to remain inserted now. But
3230 we can't safely delete it, 'cause finish_command has a handle to
3231 the bp on a bpstat, and will later want to delete it. There's a
3232 chance (and I've seen it happen) that if we delete the bp_finish
3233 here, that its storage will get reused by the time finish_command
3234 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3235 We really must allow finish_command to delete a bp_finish.
3236
3237 In the absence of a general solution for the "how do we know
3238 it's safe to delete something others may have handles to?"
3239 problem, what we'll do here is just uninsert the bp_finish, and
3240 let finish_command delete it.
3241
3242 (We know the bp_finish is "doomed" in the sense that it's
3243 momentary, and will be deleted as soon as finish_command sees
3244 the inferior stopped. So it doesn't matter that the bp's
3245 address is probably bogus in the new a.out, unlike e.g., the
3246 solib breakpoints.) */
3247
3248 if (b->type == bp_finish)
3249 {
3250 continue;
3251 }
3252
3253 /* Without a symbolic address, we have little hope of the
3254 pre-exec() address meaning the same thing in the post-exec()
3255 a.out. */
3256 if (b->addr_string == NULL)
3257 {
3258 delete_breakpoint (b);
3259 continue;
3260 }
3261 }
3262 /* FIXME what about longjmp breakpoints? Re-create them here? */
3263 create_overlay_event_breakpoint ();
3264 create_longjmp_master_breakpoint ();
3265 create_std_terminate_master_breakpoint ();
3266 create_exception_master_breakpoint ();
3267 }
3268
3269 int
3270 detach_breakpoints (int pid)
3271 {
3272 struct bp_location *bl, **blp_tmp;
3273 int val = 0;
3274 struct cleanup *old_chain = save_inferior_ptid ();
3275 struct inferior *inf = current_inferior ();
3276
3277 if (pid == PIDGET (inferior_ptid))
3278 error (_("Cannot detach breakpoints of inferior_ptid"));
3279
3280 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
3281 inferior_ptid = pid_to_ptid (pid);
3282 ALL_BP_LOCATIONS (bl, blp_tmp)
3283 {
3284 if (bl->pspace != inf->pspace)
3285 continue;
3286
3287 if (bl->inserted)
3288 val |= remove_breakpoint_1 (bl, mark_inserted);
3289 }
3290
3291 /* Detach single-step breakpoints as well. */
3292 detach_single_step_breakpoints ();
3293
3294 do_cleanups (old_chain);
3295 return val;
3296 }
3297
3298 /* Remove the breakpoint location BL from the current address space.
3299 Note that this is used to detach breakpoints from a child fork.
3300 When we get here, the child isn't in the inferior list, and neither
3301 do we have objects to represent its address space --- we should
3302 *not* look at bl->pspace->aspace here. */
3303
3304 static int
3305 remove_breakpoint_1 (struct bp_location *bl, insertion_state_t is)
3306 {
3307 int val;
3308
3309 /* BL is never in moribund_locations by our callers. */
3310 gdb_assert (bl->owner != NULL);
3311
3312 if (bl->owner->enable_state == bp_permanent)
3313 /* Permanent breakpoints cannot be inserted or removed. */
3314 return 0;
3315
3316 /* The type of none suggests that owner is actually deleted.
3317 This should not ever happen. */
3318 gdb_assert (bl->owner->type != bp_none);
3319
3320 if (bl->loc_type == bp_loc_software_breakpoint
3321 || bl->loc_type == bp_loc_hardware_breakpoint)
3322 {
3323 /* "Normal" instruction breakpoint: either the standard
3324 trap-instruction bp (bp_breakpoint), or a
3325 bp_hardware_breakpoint. */
3326
3327 /* First check to see if we have to handle an overlay. */
3328 if (overlay_debugging == ovly_off
3329 || bl->section == NULL
3330 || !(section_is_overlay (bl->section)))
3331 {
3332 /* No overlay handling: just remove the breakpoint. */
3333 val = bl->owner->ops->remove_location (bl);
3334 }
3335 else
3336 {
3337 /* This breakpoint is in an overlay section.
3338 Did we set a breakpoint at the LMA? */
3339 if (!overlay_events_enabled)
3340 {
3341 /* Yes -- overlay event support is not active, so we
3342 should have set a breakpoint at the LMA. Remove it.
3343 */
3344 /* Ignore any failures: if the LMA is in ROM, we will
3345 have already warned when we failed to insert it. */
3346 if (bl->loc_type == bp_loc_hardware_breakpoint)
3347 target_remove_hw_breakpoint (bl->gdbarch,
3348 &bl->overlay_target_info);
3349 else
3350 target_remove_breakpoint (bl->gdbarch,
3351 &bl->overlay_target_info);
3352 }
3353 /* Did we set a breakpoint at the VMA?
3354 If so, we will have marked the breakpoint 'inserted'. */
3355 if (bl->inserted)
3356 {
3357 /* Yes -- remove it. Previously we did not bother to
3358 remove the breakpoint if the section had been
3359 unmapped, but let's not rely on that being safe. We
3360 don't know what the overlay manager might do. */
3361
3362 /* However, we should remove *software* breakpoints only
3363 if the section is still mapped, or else we overwrite
3364 wrong code with the saved shadow contents. */
3365 if (bl->loc_type == bp_loc_hardware_breakpoint
3366 || section_is_mapped (bl->section))
3367 val = bl->owner->ops->remove_location (bl);
3368 else
3369 val = 0;
3370 }
3371 else
3372 {
3373 /* No -- not inserted, so no need to remove. No error. */
3374 val = 0;
3375 }
3376 }
3377
3378 /* In some cases, we might not be able to remove a breakpoint
3379 in a shared library that has already been removed, but we
3380 have not yet processed the shlib unload event. */
3381 if (val && solib_name_from_address (bl->pspace, bl->address))
3382 val = 0;
3383
3384 if (val)
3385 return val;
3386 bl->inserted = (is == mark_inserted);
3387 }
3388 else if (bl->loc_type == bp_loc_hardware_watchpoint)
3389 {
3390 gdb_assert (bl->owner->ops != NULL
3391 && bl->owner->ops->remove_location != NULL);
3392
3393 bl->inserted = (is == mark_inserted);
3394 bl->owner->ops->remove_location (bl);
3395
3396 /* Failure to remove any of the hardware watchpoints comes here. */
3397 if ((is == mark_uninserted) && (bl->inserted))
3398 warning (_("Could not remove hardware watchpoint %d."),
3399 bl->owner->number);
3400 }
3401 else if (bl->owner->type == bp_catchpoint
3402 && breakpoint_enabled (bl->owner)
3403 && !bl->duplicate)
3404 {
3405 gdb_assert (bl->owner->ops != NULL
3406 && bl->owner->ops->remove_location != NULL);
3407
3408 val = bl->owner->ops->remove_location (bl);
3409 if (val)
3410 return val;
3411
3412 bl->inserted = (is == mark_inserted);
3413 }
3414
3415 return 0;
3416 }
3417
3418 static int
3419 remove_breakpoint (struct bp_location *bl, insertion_state_t is)
3420 {
3421 int ret;
3422 struct cleanup *old_chain;
3423
3424 /* BL is never in moribund_locations by our callers. */
3425 gdb_assert (bl->owner != NULL);
3426
3427 if (bl->owner->enable_state == bp_permanent)
3428 /* Permanent breakpoints cannot be inserted or removed. */
3429 return 0;
3430
3431 /* The type of none suggests that owner is actually deleted.
3432 This should not ever happen. */
3433 gdb_assert (bl->owner->type != bp_none);
3434
3435 old_chain = save_current_space_and_thread ();
3436
3437 switch_to_program_space_and_thread (bl->pspace);
3438
3439 ret = remove_breakpoint_1 (bl, is);
3440
3441 do_cleanups (old_chain);
3442 return ret;
3443 }
3444
3445 /* Clear the "inserted" flag in all breakpoints. */
3446
3447 void
3448 mark_breakpoints_out (void)
3449 {
3450 struct bp_location *bl, **blp_tmp;
3451
3452 ALL_BP_LOCATIONS (bl, blp_tmp)
3453 if (bl->pspace == current_program_space)
3454 bl->inserted = 0;
3455 }
3456
3457 /* Clear the "inserted" flag in all breakpoints and delete any
3458 breakpoints which should go away between runs of the program.
3459
3460 Plus other such housekeeping that has to be done for breakpoints
3461 between runs.
3462
3463 Note: this function gets called at the end of a run (by
3464 generic_mourn_inferior) and when a run begins (by
3465 init_wait_for_inferior). */
3466
3467
3468
3469 void
3470 breakpoint_init_inferior (enum inf_context context)
3471 {
3472 struct breakpoint *b, *b_tmp;
3473 struct bp_location *bl, **blp_tmp;
3474 int ix;
3475 struct program_space *pspace = current_program_space;
3476
3477 /* If breakpoint locations are shared across processes, then there's
3478 nothing to do. */
3479 if (gdbarch_has_global_breakpoints (target_gdbarch))
3480 return;
3481
3482 ALL_BP_LOCATIONS (bl, blp_tmp)
3483 {
3484 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
3485 if (bl->pspace == pspace
3486 && bl->owner->enable_state != bp_permanent)
3487 bl->inserted = 0;
3488 }
3489
3490 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3491 {
3492 if (b->loc && b->loc->pspace != pspace)
3493 continue;
3494
3495 switch (b->type)
3496 {
3497 case bp_call_dummy:
3498
3499 /* If the call dummy breakpoint is at the entry point it will
3500 cause problems when the inferior is rerun, so we better get
3501 rid of it. */
3502
3503 case bp_watchpoint_scope:
3504
3505 /* Also get rid of scope breakpoints. */
3506
3507 case bp_shlib_event:
3508
3509 /* Also remove solib event breakpoints. Their addresses may
3510 have changed since the last time we ran the program.
3511 Actually we may now be debugging against different target;
3512 and so the solib backend that installed this breakpoint may
3513 not be used in by the target. E.g.,
3514
3515 (gdb) file prog-linux
3516 (gdb) run # native linux target
3517 ...
3518 (gdb) kill
3519 (gdb) file prog-win.exe
3520 (gdb) tar rem :9999 # remote Windows gdbserver.
3521 */
3522
3523 case bp_step_resume:
3524
3525 /* Also remove step-resume breakpoints. */
3526
3527 delete_breakpoint (b);
3528 break;
3529
3530 case bp_watchpoint:
3531 case bp_hardware_watchpoint:
3532 case bp_read_watchpoint:
3533 case bp_access_watchpoint:
3534 {
3535 struct watchpoint *w = (struct watchpoint *) b;
3536
3537 /* Likewise for watchpoints on local expressions. */
3538 if (w->exp_valid_block != NULL)
3539 delete_breakpoint (b);
3540 else if (context == inf_starting)
3541 {
3542 /* Reset val field to force reread of starting value in
3543 insert_breakpoints. */
3544 if (w->val)
3545 value_free (w->val);
3546 w->val = NULL;
3547 w->val_valid = 0;
3548 }
3549 }
3550 break;
3551 default:
3552 break;
3553 }
3554 }
3555
3556 /* Get rid of the moribund locations. */
3557 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
3558 decref_bp_location (&bl);
3559 VEC_free (bp_location_p, moribund_locations);
3560 }
3561
3562 /* These functions concern about actual breakpoints inserted in the
3563 target --- to e.g. check if we need to do decr_pc adjustment or if
3564 we need to hop over the bkpt --- so we check for address space
3565 match, not program space. */
3566
3567 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
3568 exists at PC. It returns ordinary_breakpoint_here if it's an
3569 ordinary breakpoint, or permanent_breakpoint_here if it's a
3570 permanent breakpoint.
3571 - When continuing from a location with an ordinary breakpoint, we
3572 actually single step once before calling insert_breakpoints.
3573 - When continuing from a location with a permanent breakpoint, we
3574 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
3575 the target, to advance the PC past the breakpoint. */
3576
3577 enum breakpoint_here
3578 breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
3579 {
3580 struct bp_location *bl, **blp_tmp;
3581 int any_breakpoint_here = 0;
3582
3583 ALL_BP_LOCATIONS (bl, blp_tmp)
3584 {
3585 if (bl->loc_type != bp_loc_software_breakpoint
3586 && bl->loc_type != bp_loc_hardware_breakpoint)
3587 continue;
3588
3589 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
3590 if ((breakpoint_enabled (bl->owner)
3591 || bl->owner->enable_state == bp_permanent)
3592 && breakpoint_location_address_match (bl, aspace, pc))
3593 {
3594 if (overlay_debugging
3595 && section_is_overlay (bl->section)
3596 && !section_is_mapped (bl->section))
3597 continue; /* unmapped overlay -- can't be a match */
3598 else if (bl->owner->enable_state == bp_permanent)
3599 return permanent_breakpoint_here;
3600 else
3601 any_breakpoint_here = 1;
3602 }
3603 }
3604
3605 return any_breakpoint_here ? ordinary_breakpoint_here : 0;
3606 }
3607
3608 /* Return true if there's a moribund breakpoint at PC. */
3609
3610 int
3611 moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
3612 {
3613 struct bp_location *loc;
3614 int ix;
3615
3616 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
3617 if (breakpoint_location_address_match (loc, aspace, pc))
3618 return 1;
3619
3620 return 0;
3621 }
3622
3623 /* Returns non-zero if there's a breakpoint inserted at PC, which is
3624 inserted using regular breakpoint_chain / bp_location array
3625 mechanism. This does not check for single-step breakpoints, which
3626 are inserted and removed using direct target manipulation. */
3627
3628 int
3629 regular_breakpoint_inserted_here_p (struct address_space *aspace,
3630 CORE_ADDR pc)
3631 {
3632 struct bp_location *bl, **blp_tmp;
3633
3634 ALL_BP_LOCATIONS (bl, blp_tmp)
3635 {
3636 if (bl->loc_type != bp_loc_software_breakpoint
3637 && bl->loc_type != bp_loc_hardware_breakpoint)
3638 continue;
3639
3640 if (bl->inserted
3641 && breakpoint_location_address_match (bl, aspace, pc))
3642 {
3643 if (overlay_debugging
3644 && section_is_overlay (bl->section)
3645 && !section_is_mapped (bl->section))
3646 continue; /* unmapped overlay -- can't be a match */
3647 else
3648 return 1;
3649 }
3650 }
3651 return 0;
3652 }
3653
3654 /* Returns non-zero iff there's either regular breakpoint
3655 or a single step breakpoint inserted at PC. */
3656
3657 int
3658 breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
3659 {
3660 if (regular_breakpoint_inserted_here_p (aspace, pc))
3661 return 1;
3662
3663 if (single_step_breakpoint_inserted_here_p (aspace, pc))
3664 return 1;
3665
3666 return 0;
3667 }
3668
3669 /* This function returns non-zero iff there is a software breakpoint
3670 inserted at PC. */
3671
3672 int
3673 software_breakpoint_inserted_here_p (struct address_space *aspace,
3674 CORE_ADDR pc)
3675 {
3676 struct bp_location *bl, **blp_tmp;
3677
3678 ALL_BP_LOCATIONS (bl, blp_tmp)
3679 {
3680 if (bl->loc_type != bp_loc_software_breakpoint)
3681 continue;
3682
3683 if (bl->inserted
3684 && breakpoint_address_match (bl->pspace->aspace, bl->address,
3685 aspace, pc))
3686 {
3687 if (overlay_debugging
3688 && section_is_overlay (bl->section)
3689 && !section_is_mapped (bl->section))
3690 continue; /* unmapped overlay -- can't be a match */
3691 else
3692 return 1;
3693 }
3694 }
3695
3696 /* Also check for software single-step breakpoints. */
3697 if (single_step_breakpoint_inserted_here_p (aspace, pc))
3698 return 1;
3699
3700 return 0;
3701 }
3702
3703 int
3704 hardware_watchpoint_inserted_in_range (struct address_space *aspace,
3705 CORE_ADDR addr, ULONGEST len)
3706 {
3707 struct breakpoint *bpt;
3708
3709 ALL_BREAKPOINTS (bpt)
3710 {
3711 struct bp_location *loc;
3712
3713 if (bpt->type != bp_hardware_watchpoint
3714 && bpt->type != bp_access_watchpoint)
3715 continue;
3716
3717 if (!breakpoint_enabled (bpt))
3718 continue;
3719
3720 for (loc = bpt->loc; loc; loc = loc->next)
3721 if (loc->pspace->aspace == aspace && loc->inserted)
3722 {
3723 CORE_ADDR l, h;
3724
3725 /* Check for intersection. */
3726 l = max (loc->address, addr);
3727 h = min (loc->address + loc->length, addr + len);
3728 if (l < h)
3729 return 1;
3730 }
3731 }
3732 return 0;
3733 }
3734
3735 /* breakpoint_thread_match (PC, PTID) returns true if the breakpoint at
3736 PC is valid for process/thread PTID. */
3737
3738 int
3739 breakpoint_thread_match (struct address_space *aspace, CORE_ADDR pc,
3740 ptid_t ptid)
3741 {
3742 struct bp_location *bl, **blp_tmp;
3743 /* The thread and task IDs associated to PTID, computed lazily. */
3744 int thread = -1;
3745 int task = 0;
3746
3747 ALL_BP_LOCATIONS (bl, blp_tmp)
3748 {
3749 if (bl->loc_type != bp_loc_software_breakpoint
3750 && bl->loc_type != bp_loc_hardware_breakpoint)
3751 continue;
3752
3753 /* ALL_BP_LOCATIONS bp_location has bl->OWNER always non-NULL. */
3754 if (!breakpoint_enabled (bl->owner)
3755 && bl->owner->enable_state != bp_permanent)
3756 continue;
3757
3758 if (!breakpoint_location_address_match (bl, aspace, pc))
3759 continue;
3760
3761 if (bl->owner->thread != -1)
3762 {
3763 /* This is a thread-specific breakpoint. Check that ptid
3764 matches that thread. If thread hasn't been computed yet,
3765 it is now time to do so. */
3766 if (thread == -1)
3767 thread = pid_to_thread_id (ptid);
3768 if (bl->owner->thread != thread)
3769 continue;
3770 }
3771
3772 if (bl->owner->task != 0)
3773 {
3774 /* This is a task-specific breakpoint. Check that ptid
3775 matches that task. If task hasn't been computed yet,
3776 it is now time to do so. */
3777 if (task == 0)
3778 task = ada_get_task_number (ptid);
3779 if (bl->owner->task != task)
3780 continue;
3781 }
3782
3783 if (overlay_debugging
3784 && section_is_overlay (bl->section)
3785 && !section_is_mapped (bl->section))
3786 continue; /* unmapped overlay -- can't be a match */
3787
3788 return 1;
3789 }
3790
3791 return 0;
3792 }
3793 \f
3794
3795 /* bpstat stuff. External routines' interfaces are documented
3796 in breakpoint.h. */
3797
3798 int
3799 is_catchpoint (struct breakpoint *ep)
3800 {
3801 return (ep->type == bp_catchpoint);
3802 }
3803
3804 /* Frees any storage that is part of a bpstat. Does not walk the
3805 'next' chain. */
3806
3807 static void
3808 bpstat_free (bpstat bs)
3809 {
3810 if (bs->old_val != NULL)
3811 value_free (bs->old_val);
3812 decref_counted_command_line (&bs->commands);
3813 decref_bp_location (&bs->bp_location_at);
3814 xfree (bs);
3815 }
3816
3817 /* Clear a bpstat so that it says we are not at any breakpoint.
3818 Also free any storage that is part of a bpstat. */
3819
3820 void
3821 bpstat_clear (bpstat *bsp)
3822 {
3823 bpstat p;
3824 bpstat q;
3825
3826 if (bsp == 0)
3827 return;
3828 p = *bsp;
3829 while (p != NULL)
3830 {
3831 q = p->next;
3832 bpstat_free (p);
3833 p = q;
3834 }
3835 *bsp = NULL;
3836 }
3837
3838 /* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
3839 is part of the bpstat is copied as well. */
3840
3841 bpstat
3842 bpstat_copy (bpstat bs)
3843 {
3844 bpstat p = NULL;
3845 bpstat tmp;
3846 bpstat retval = NULL;
3847
3848 if (bs == NULL)
3849 return bs;
3850
3851 for (; bs != NULL; bs = bs->next)
3852 {
3853 tmp = (bpstat) xmalloc (sizeof (*tmp));
3854 memcpy (tmp, bs, sizeof (*tmp));
3855 incref_counted_command_line (tmp->commands);
3856 incref_bp_location (tmp->bp_location_at);
3857 if (bs->old_val != NULL)
3858 {
3859 tmp->old_val = value_copy (bs->old_val);
3860 release_value (tmp->old_val);
3861 }
3862
3863 if (p == NULL)
3864 /* This is the first thing in the chain. */
3865 retval = tmp;
3866 else
3867 p->next = tmp;
3868 p = tmp;
3869 }
3870 p->next = NULL;
3871 return retval;
3872 }
3873
3874 /* Find the bpstat associated with this breakpoint. */
3875
3876 bpstat
3877 bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
3878 {
3879 if (bsp == NULL)
3880 return NULL;
3881
3882 for (; bsp != NULL; bsp = bsp->next)
3883 {
3884 if (bsp->breakpoint_at == breakpoint)
3885 return bsp;
3886 }
3887 return NULL;
3888 }
3889
3890 /* Put in *NUM the breakpoint number of the first breakpoint we are
3891 stopped at. *BSP upon return is a bpstat which points to the
3892 remaining breakpoints stopped at (but which is not guaranteed to be
3893 good for anything but further calls to bpstat_num).
3894
3895 Return 0 if passed a bpstat which does not indicate any breakpoints.
3896 Return -1 if stopped at a breakpoint that has been deleted since
3897 we set it.
3898 Return 1 otherwise. */
3899
3900 int
3901 bpstat_num (bpstat *bsp, int *num)
3902 {
3903 struct breakpoint *b;
3904
3905 if ((*bsp) == NULL)
3906 return 0; /* No more breakpoint values */
3907
3908 /* We assume we'll never have several bpstats that correspond to a
3909 single breakpoint -- otherwise, this function might return the
3910 same number more than once and this will look ugly. */
3911 b = (*bsp)->breakpoint_at;
3912 *bsp = (*bsp)->next;
3913 if (b == NULL)
3914 return -1; /* breakpoint that's been deleted since */
3915
3916 *num = b->number; /* We have its number */
3917 return 1;
3918 }
3919
3920 /* See breakpoint.h. */
3921
3922 void
3923 bpstat_clear_actions (void)
3924 {
3925 struct thread_info *tp;
3926 bpstat bs;
3927
3928 if (ptid_equal (inferior_ptid, null_ptid))
3929 return;
3930
3931 tp = find_thread_ptid (inferior_ptid);
3932 if (tp == NULL)
3933 return;
3934
3935 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
3936 {
3937 decref_counted_command_line (&bs->commands);
3938
3939 if (bs->old_val != NULL)
3940 {
3941 value_free (bs->old_val);
3942 bs->old_val = NULL;
3943 }
3944 }
3945 }
3946
3947 /* Called when a command is about to proceed the inferior. */
3948
3949 static void
3950 breakpoint_about_to_proceed (void)
3951 {
3952 if (!ptid_equal (inferior_ptid, null_ptid))
3953 {
3954 struct thread_info *tp = inferior_thread ();
3955
3956 /* Allow inferior function calls in breakpoint commands to not
3957 interrupt the command list. When the call finishes
3958 successfully, the inferior will be standing at the same
3959 breakpoint as if nothing happened. */
3960 if (tp->control.in_infcall)
3961 return;
3962 }
3963
3964 breakpoint_proceeded = 1;
3965 }
3966
3967 /* Stub for cleaning up our state if we error-out of a breakpoint
3968 command. */
3969 static void
3970 cleanup_executing_breakpoints (void *ignore)
3971 {
3972 executing_breakpoint_commands = 0;
3973 }
3974
3975 /* Return non-zero iff CMD as the first line of a command sequence is `silent'
3976 or its equivalent. */
3977
3978 static int
3979 command_line_is_silent (struct command_line *cmd)
3980 {
3981 return cmd && (strcmp ("silent", cmd->line) == 0
3982 || (xdb_commands && strcmp ("Q", cmd->line) == 0));
3983 }
3984
3985 /* Execute all the commands associated with all the breakpoints at
3986 this location. Any of these commands could cause the process to
3987 proceed beyond this point, etc. We look out for such changes by
3988 checking the global "breakpoint_proceeded" after each command.
3989
3990 Returns true if a breakpoint command resumed the inferior. In that
3991 case, it is the caller's responsibility to recall it again with the
3992 bpstat of the current thread. */
3993
3994 static int
3995 bpstat_do_actions_1 (bpstat *bsp)
3996 {
3997 bpstat bs;
3998 struct cleanup *old_chain;
3999 int again = 0;
4000
4001 /* Avoid endless recursion if a `source' command is contained
4002 in bs->commands. */
4003 if (executing_breakpoint_commands)
4004 return 0;
4005
4006 executing_breakpoint_commands = 1;
4007 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
4008
4009 prevent_dont_repeat ();
4010
4011 /* This pointer will iterate over the list of bpstat's. */
4012 bs = *bsp;
4013
4014 breakpoint_proceeded = 0;
4015 for (; bs != NULL; bs = bs->next)
4016 {
4017 struct counted_command_line *ccmd;
4018 struct command_line *cmd;
4019 struct cleanup *this_cmd_tree_chain;
4020
4021 /* Take ownership of the BSP's command tree, if it has one.
4022
4023 The command tree could legitimately contain commands like
4024 'step' and 'next', which call clear_proceed_status, which
4025 frees stop_bpstat's command tree. To make sure this doesn't
4026 free the tree we're executing out from under us, we need to
4027 take ownership of the tree ourselves. Since a given bpstat's
4028 commands are only executed once, we don't need to copy it; we
4029 can clear the pointer in the bpstat, and make sure we free
4030 the tree when we're done. */
4031 ccmd = bs->commands;
4032 bs->commands = NULL;
4033 this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
4034 cmd = ccmd ? ccmd->commands : NULL;
4035 if (command_line_is_silent (cmd))
4036 {
4037 /* The action has been already done by bpstat_stop_status. */
4038 cmd = cmd->next;
4039 }
4040
4041 while (cmd != NULL)
4042 {
4043 execute_control_command (cmd);
4044
4045 if (breakpoint_proceeded)
4046 break;
4047 else
4048 cmd = cmd->next;
4049 }
4050
4051 /* We can free this command tree now. */
4052 do_cleanups (this_cmd_tree_chain);
4053
4054 if (breakpoint_proceeded)
4055 {
4056 if (target_can_async_p ())
4057 /* If we are in async mode, then the target might be still
4058 running, not stopped at any breakpoint, so nothing for
4059 us to do here -- just return to the event loop. */
4060 ;
4061 else
4062 /* In sync mode, when execute_control_command returns
4063 we're already standing on the next breakpoint.
4064 Breakpoint commands for that stop were not run, since
4065 execute_command does not run breakpoint commands --
4066 only command_line_handler does, but that one is not
4067 involved in execution of breakpoint commands. So, we
4068 can now execute breakpoint commands. It should be
4069 noted that making execute_command do bpstat actions is
4070 not an option -- in this case we'll have recursive
4071 invocation of bpstat for each breakpoint with a
4072 command, and can easily blow up GDB stack. Instead, we
4073 return true, which will trigger the caller to recall us
4074 with the new stop_bpstat. */
4075 again = 1;
4076 break;
4077 }
4078 }
4079 do_cleanups (old_chain);
4080 return again;
4081 }
4082
4083 void
4084 bpstat_do_actions (void)
4085 {
4086 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4087
4088 /* Do any commands attached to breakpoint we are stopped at. */
4089 while (!ptid_equal (inferior_ptid, null_ptid)
4090 && target_has_execution
4091 && !is_exited (inferior_ptid)
4092 && !is_executing (inferior_ptid))
4093 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4094 and only return when it is stopped at the next breakpoint, we
4095 keep doing breakpoint actions until it returns false to
4096 indicate the inferior was not resumed. */
4097 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
4098 break;
4099
4100 discard_cleanups (cleanup_if_error);
4101 }
4102
4103 /* Print out the (old or new) value associated with a watchpoint. */
4104
4105 static void
4106 watchpoint_value_print (struct value *val, struct ui_file *stream)
4107 {
4108 if (val == NULL)
4109 fprintf_unfiltered (stream, _("<unreadable>"));
4110 else
4111 {
4112 struct value_print_options opts;
4113 get_user_print_options (&opts);
4114 value_print (val, stream, &opts);
4115 }
4116 }
4117
4118 /* Generic routine for printing messages indicating why we
4119 stopped. The behavior of this function depends on the value
4120 'print_it' in the bpstat structure. Under some circumstances we
4121 may decide not to print anything here and delegate the task to
4122 normal_stop(). */
4123
4124 static enum print_stop_action
4125 print_bp_stop_message (bpstat bs)
4126 {
4127 switch (bs->print_it)
4128 {
4129 case print_it_noop:
4130 /* Nothing should be printed for this bpstat entry. */
4131 return PRINT_UNKNOWN;
4132 break;
4133
4134 case print_it_done:
4135 /* We still want to print the frame, but we already printed the
4136 relevant messages. */
4137 return PRINT_SRC_AND_LOC;
4138 break;
4139
4140 case print_it_normal:
4141 {
4142 struct breakpoint *b = bs->breakpoint_at;
4143
4144 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4145 which has since been deleted. */
4146 if (b == NULL)
4147 return PRINT_UNKNOWN;
4148
4149 /* Normal case. Call the breakpoint's print_it method. */
4150 return b->ops->print_it (bs);
4151 }
4152 break;
4153
4154 default:
4155 internal_error (__FILE__, __LINE__,
4156 _("print_bp_stop_message: unrecognized enum value"));
4157 break;
4158 }
4159 }
4160
4161 /* A helper function that prints a shared library stopped event. */
4162
4163 static void
4164 print_solib_event (int is_catchpoint)
4165 {
4166 int any_deleted
4167 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4168 int any_added
4169 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4170
4171 if (!is_catchpoint)
4172 {
4173 if (any_added || any_deleted)
4174 ui_out_text (current_uiout,
4175 _("Stopped due to shared library event:\n"));
4176 else
4177 ui_out_text (current_uiout,
4178 _("Stopped due to shared library event (no "
4179 "libraries added or removed)\n"));
4180 }
4181
4182 if (ui_out_is_mi_like_p (current_uiout))
4183 ui_out_field_string (current_uiout, "reason",
4184 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4185
4186 if (any_deleted)
4187 {
4188 struct cleanup *cleanup;
4189 char *name;
4190 int ix;
4191
4192 ui_out_text (current_uiout, _(" Inferior unloaded "));
4193 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4194 "removed");
4195 for (ix = 0;
4196 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4197 ix, name);
4198 ++ix)
4199 {
4200 if (ix > 0)
4201 ui_out_text (current_uiout, " ");
4202 ui_out_field_string (current_uiout, "library", name);
4203 ui_out_text (current_uiout, "\n");
4204 }
4205
4206 do_cleanups (cleanup);
4207 }
4208
4209 if (any_added)
4210 {
4211 struct so_list *iter;
4212 int ix;
4213 struct cleanup *cleanup;
4214
4215 ui_out_text (current_uiout, _(" Inferior loaded "));
4216 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4217 "added");
4218 for (ix = 0;
4219 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4220 ix, iter);
4221 ++ix)
4222 {
4223 if (ix > 0)
4224 ui_out_text (current_uiout, " ");
4225 ui_out_field_string (current_uiout, "library", iter->so_name);
4226 ui_out_text (current_uiout, "\n");
4227 }
4228
4229 do_cleanups (cleanup);
4230 }
4231 }
4232
4233 /* Print a message indicating what happened. This is called from
4234 normal_stop(). The input to this routine is the head of the bpstat
4235 list - a list of the eventpoints that caused this stop. KIND is
4236 the target_waitkind for the stopping event. This
4237 routine calls the generic print routine for printing a message
4238 about reasons for stopping. This will print (for example) the
4239 "Breakpoint n," part of the output. The return value of this
4240 routine is one of:
4241
4242 PRINT_UNKNOWN: Means we printed nothing.
4243 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4244 code to print the location. An example is
4245 "Breakpoint 1, " which should be followed by
4246 the location.
4247 PRINT_SRC_ONLY: Means we printed something, but there is no need
4248 to also print the location part of the message.
4249 An example is the catch/throw messages, which
4250 don't require a location appended to the end.
4251 PRINT_NOTHING: We have done some printing and we don't need any
4252 further info to be printed. */
4253
4254 enum print_stop_action
4255 bpstat_print (bpstat bs, int kind)
4256 {
4257 int val;
4258
4259 /* Maybe another breakpoint in the chain caused us to stop.
4260 (Currently all watchpoints go on the bpstat whether hit or not.
4261 That probably could (should) be changed, provided care is taken
4262 with respect to bpstat_explains_signal). */
4263 for (; bs; bs = bs->next)
4264 {
4265 val = print_bp_stop_message (bs);
4266 if (val == PRINT_SRC_ONLY
4267 || val == PRINT_SRC_AND_LOC
4268 || val == PRINT_NOTHING)
4269 return val;
4270 }
4271
4272 /* If we had hit a shared library event breakpoint,
4273 print_bp_stop_message would print out this message. If we hit an
4274 OS-level shared library event, do the same thing. */
4275 if (kind == TARGET_WAITKIND_LOADED)
4276 {
4277 print_solib_event (0);
4278 return PRINT_NOTHING;
4279 }
4280
4281 /* We reached the end of the chain, or we got a null BS to start
4282 with and nothing was printed. */
4283 return PRINT_UNKNOWN;
4284 }
4285
4286 /* Evaluate the expression EXP and return 1 if value is zero. This is
4287 used inside a catch_errors to evaluate the breakpoint condition.
4288 The argument is a "struct expression *" that has been cast to a
4289 "char *" to make it pass through catch_errors. */
4290
4291 static int
4292 breakpoint_cond_eval (void *exp)
4293 {
4294 struct value *mark = value_mark ();
4295 int i = !value_true (evaluate_expression ((struct expression *) exp));
4296
4297 value_free_to_mark (mark);
4298 return i;
4299 }
4300
4301 /* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
4302
4303 static bpstat
4304 bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
4305 {
4306 bpstat bs;
4307
4308 bs = (bpstat) xmalloc (sizeof (*bs));
4309 bs->next = NULL;
4310 **bs_link_pointer = bs;
4311 *bs_link_pointer = &bs->next;
4312 bs->breakpoint_at = bl->owner;
4313 bs->bp_location_at = bl;
4314 incref_bp_location (bl);
4315 /* If the condition is false, etc., don't do the commands. */
4316 bs->commands = NULL;
4317 bs->old_val = NULL;
4318 bs->print_it = print_it_normal;
4319 return bs;
4320 }
4321 \f
4322 /* The target has stopped with waitstatus WS. Check if any hardware
4323 watchpoints have triggered, according to the target. */
4324
4325 int
4326 watchpoints_triggered (struct target_waitstatus *ws)
4327 {
4328 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
4329 CORE_ADDR addr;
4330 struct breakpoint *b;
4331
4332 if (!stopped_by_watchpoint)
4333 {
4334 /* We were not stopped by a watchpoint. Mark all watchpoints
4335 as not triggered. */
4336 ALL_BREAKPOINTS (b)
4337 if (is_hardware_watchpoint (b))
4338 {
4339 struct watchpoint *w = (struct watchpoint *) b;
4340
4341 w->watchpoint_triggered = watch_triggered_no;
4342 }
4343
4344 return 0;
4345 }
4346
4347 if (!target_stopped_data_address (&current_target, &addr))
4348 {
4349 /* We were stopped by a watchpoint, but we don't know where.
4350 Mark all watchpoints as unknown. */
4351 ALL_BREAKPOINTS (b)
4352 if (is_hardware_watchpoint (b))
4353 {
4354 struct watchpoint *w = (struct watchpoint *) b;
4355
4356 w->watchpoint_triggered = watch_triggered_unknown;
4357 }
4358
4359 return stopped_by_watchpoint;
4360 }
4361
4362 /* The target could report the data address. Mark watchpoints
4363 affected by this data address as triggered, and all others as not
4364 triggered. */
4365
4366 ALL_BREAKPOINTS (b)
4367 if (is_hardware_watchpoint (b))
4368 {
4369 struct watchpoint *w = (struct watchpoint *) b;
4370 struct bp_location *loc;
4371
4372 w->watchpoint_triggered = watch_triggered_no;
4373 for (loc = b->loc; loc; loc = loc->next)
4374 {
4375 if (is_masked_watchpoint (b))
4376 {
4377 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4378 CORE_ADDR start = loc->address & w->hw_wp_mask;
4379
4380 if (newaddr == start)
4381 {
4382 w->watchpoint_triggered = watch_triggered_yes;
4383 break;
4384 }
4385 }
4386 /* Exact match not required. Within range is sufficient. */
4387 else if (target_watchpoint_addr_within_range (&current_target,
4388 addr, loc->address,
4389 loc->length))
4390 {
4391 w->watchpoint_triggered = watch_triggered_yes;
4392 break;
4393 }
4394 }
4395 }
4396
4397 return 1;
4398 }
4399
4400 /* Possible return values for watchpoint_check (this can't be an enum
4401 because of check_errors). */
4402 /* The watchpoint has been deleted. */
4403 #define WP_DELETED 1
4404 /* The value has changed. */
4405 #define WP_VALUE_CHANGED 2
4406 /* The value has not changed. */
4407 #define WP_VALUE_NOT_CHANGED 3
4408 /* Ignore this watchpoint, no matter if the value changed or not. */
4409 #define WP_IGNORE 4
4410
4411 #define BP_TEMPFLAG 1
4412 #define BP_HARDWAREFLAG 2
4413
4414 /* Evaluate watchpoint condition expression and check if its value
4415 changed.
4416
4417 P should be a pointer to struct bpstat, but is defined as a void *
4418 in order for this function to be usable with catch_errors. */
4419
4420 static int
4421 watchpoint_check (void *p)
4422 {
4423 bpstat bs = (bpstat) p;
4424 struct watchpoint *b;
4425 struct frame_info *fr;
4426 int within_current_scope;
4427
4428 /* BS is built from an existing struct breakpoint. */
4429 gdb_assert (bs->breakpoint_at != NULL);
4430 b = (struct watchpoint *) bs->breakpoint_at;
4431
4432 /* If this is a local watchpoint, we only want to check if the
4433 watchpoint frame is in scope if the current thread is the thread
4434 that was used to create the watchpoint. */
4435 if (!watchpoint_in_thread_scope (b))
4436 return WP_IGNORE;
4437
4438 if (b->exp_valid_block == NULL)
4439 within_current_scope = 1;
4440 else
4441 {
4442 struct frame_info *frame = get_current_frame ();
4443 struct gdbarch *frame_arch = get_frame_arch (frame);
4444 CORE_ADDR frame_pc = get_frame_pc (frame);
4445
4446 /* in_function_epilogue_p() returns a non-zero value if we're
4447 still in the function but the stack frame has already been
4448 invalidated. Since we can't rely on the values of local
4449 variables after the stack has been destroyed, we are treating
4450 the watchpoint in that state as `not changed' without further
4451 checking. Don't mark watchpoints as changed if the current
4452 frame is in an epilogue - even if they are in some other
4453 frame, our view of the stack is likely to be wrong and
4454 frame_find_by_id could error out. */
4455 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
4456 return WP_IGNORE;
4457
4458 fr = frame_find_by_id (b->watchpoint_frame);
4459 within_current_scope = (fr != NULL);
4460
4461 /* If we've gotten confused in the unwinder, we might have
4462 returned a frame that can't describe this variable. */
4463 if (within_current_scope)
4464 {
4465 struct symbol *function;
4466
4467 function = get_frame_function (fr);
4468 if (function == NULL
4469 || !contained_in (b->exp_valid_block,
4470 SYMBOL_BLOCK_VALUE (function)))
4471 within_current_scope = 0;
4472 }
4473
4474 if (within_current_scope)
4475 /* If we end up stopping, the current frame will get selected
4476 in normal_stop. So this call to select_frame won't affect
4477 the user. */
4478 select_frame (fr);
4479 }
4480
4481 if (within_current_scope)
4482 {
4483 /* We use value_{,free_to_}mark because it could be a *long*
4484 time before we return to the command level and call
4485 free_all_values. We can't call free_all_values because we
4486 might be in the middle of evaluating a function call. */
4487
4488 int pc = 0;
4489 struct value *mark;
4490 struct value *new_val;
4491
4492 if (is_masked_watchpoint (&b->base))
4493 /* Since we don't know the exact trigger address (from
4494 stopped_data_address), just tell the user we've triggered
4495 a mask watchpoint. */
4496 return WP_VALUE_CHANGED;
4497
4498 mark = value_mark ();
4499 fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL);
4500
4501 /* We use value_equal_contents instead of value_equal because
4502 the latter coerces an array to a pointer, thus comparing just
4503 the address of the array instead of its contents. This is
4504 not what we want. */
4505 if ((b->val != NULL) != (new_val != NULL)
4506 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
4507 {
4508 if (new_val != NULL)
4509 {
4510 release_value (new_val);
4511 value_free_to_mark (mark);
4512 }
4513 bs->old_val = b->val;
4514 b->val = new_val;
4515 b->val_valid = 1;
4516 return WP_VALUE_CHANGED;
4517 }
4518 else
4519 {
4520 /* Nothing changed. */
4521 value_free_to_mark (mark);
4522 return WP_VALUE_NOT_CHANGED;
4523 }
4524 }
4525 else
4526 {
4527 struct ui_out *uiout = current_uiout;
4528
4529 /* This seems like the only logical thing to do because
4530 if we temporarily ignored the watchpoint, then when
4531 we reenter the block in which it is valid it contains
4532 garbage (in the case of a function, it may have two
4533 garbage values, one before and one after the prologue).
4534 So we can't even detect the first assignment to it and
4535 watch after that (since the garbage may or may not equal
4536 the first value assigned). */
4537 /* We print all the stop information in
4538 breakpoint_ops->print_it, but in this case, by the time we
4539 call breakpoint_ops->print_it this bp will be deleted
4540 already. So we have no choice but print the information
4541 here. */
4542 if (ui_out_is_mi_like_p (uiout))
4543 ui_out_field_string
4544 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
4545 ui_out_text (uiout, "\nWatchpoint ");
4546 ui_out_field_int (uiout, "wpnum", b->base.number);
4547 ui_out_text (uiout,
4548 " deleted because the program has left the block in\n\
4549 which its expression is valid.\n");
4550
4551 /* Make sure the watchpoint's commands aren't executed. */
4552 decref_counted_command_line (&b->base.commands);
4553 watchpoint_del_at_next_stop (b);
4554
4555 return WP_DELETED;
4556 }
4557 }
4558
4559 /* Return true if it looks like target has stopped due to hitting
4560 breakpoint location BL. This function does not check if we should
4561 stop, only if BL explains the stop. */
4562
4563 static int
4564 bpstat_check_location (const struct bp_location *bl,
4565 struct address_space *aspace, CORE_ADDR bp_addr,
4566 const struct target_waitstatus *ws)
4567 {
4568 struct breakpoint *b = bl->owner;
4569
4570 /* BL is from an existing breakpoint. */
4571 gdb_assert (b != NULL);
4572
4573 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
4574 }
4575
4576 /* Determine if the watched values have actually changed, and we
4577 should stop. If not, set BS->stop to 0. */
4578
4579 static void
4580 bpstat_check_watchpoint (bpstat bs)
4581 {
4582 const struct bp_location *bl;
4583 struct watchpoint *b;
4584
4585 /* BS is built for existing struct breakpoint. */
4586 bl = bs->bp_location_at;
4587 gdb_assert (bl != NULL);
4588 b = (struct watchpoint *) bs->breakpoint_at;
4589 gdb_assert (b != NULL);
4590
4591 {
4592 int must_check_value = 0;
4593
4594 if (b->base.type == bp_watchpoint)
4595 /* For a software watchpoint, we must always check the
4596 watched value. */
4597 must_check_value = 1;
4598 else if (b->watchpoint_triggered == watch_triggered_yes)
4599 /* We have a hardware watchpoint (read, write, or access)
4600 and the target earlier reported an address watched by
4601 this watchpoint. */
4602 must_check_value = 1;
4603 else if (b->watchpoint_triggered == watch_triggered_unknown
4604 && b->base.type == bp_hardware_watchpoint)
4605 /* We were stopped by a hardware watchpoint, but the target could
4606 not report the data address. We must check the watchpoint's
4607 value. Access and read watchpoints are out of luck; without
4608 a data address, we can't figure it out. */
4609 must_check_value = 1;
4610
4611 if (must_check_value)
4612 {
4613 char *message
4614 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
4615 b->base.number);
4616 struct cleanup *cleanups = make_cleanup (xfree, message);
4617 int e = catch_errors (watchpoint_check, bs, message,
4618 RETURN_MASK_ALL);
4619 do_cleanups (cleanups);
4620 switch (e)
4621 {
4622 case WP_DELETED:
4623 /* We've already printed what needs to be printed. */
4624 bs->print_it = print_it_done;
4625 /* Stop. */
4626 break;
4627 case WP_IGNORE:
4628 bs->print_it = print_it_noop;
4629 bs->stop = 0;
4630 break;
4631 case WP_VALUE_CHANGED:
4632 if (b->base.type == bp_read_watchpoint)
4633 {
4634 /* There are two cases to consider here:
4635
4636 1. We're watching the triggered memory for reads.
4637 In that case, trust the target, and always report
4638 the watchpoint hit to the user. Even though
4639 reads don't cause value changes, the value may
4640 have changed since the last time it was read, and
4641 since we're not trapping writes, we will not see
4642 those, and as such we should ignore our notion of
4643 old value.
4644
4645 2. We're watching the triggered memory for both
4646 reads and writes. There are two ways this may
4647 happen:
4648
4649 2.1. This is a target that can't break on data
4650 reads only, but can break on accesses (reads or
4651 writes), such as e.g., x86. We detect this case
4652 at the time we try to insert read watchpoints.
4653
4654 2.2. Otherwise, the target supports read
4655 watchpoints, but, the user set an access or write
4656 watchpoint watching the same memory as this read
4657 watchpoint.
4658
4659 If we're watching memory writes as well as reads,
4660 ignore watchpoint hits when we find that the
4661 value hasn't changed, as reads don't cause
4662 changes. This still gives false positives when
4663 the program writes the same value to memory as
4664 what there was already in memory (we will confuse
4665 it for a read), but it's much better than
4666 nothing. */
4667
4668 int other_write_watchpoint = 0;
4669
4670 if (bl->watchpoint_type == hw_read)
4671 {
4672 struct breakpoint *other_b;
4673
4674 ALL_BREAKPOINTS (other_b)
4675 if (other_b->type == bp_hardware_watchpoint
4676 || other_b->type == bp_access_watchpoint)
4677 {
4678 struct watchpoint *other_w =
4679 (struct watchpoint *) other_b;
4680
4681 if (other_w->watchpoint_triggered
4682 == watch_triggered_yes)
4683 {
4684 other_write_watchpoint = 1;
4685 break;
4686 }
4687 }
4688 }
4689
4690 if (other_write_watchpoint
4691 || bl->watchpoint_type == hw_access)
4692 {
4693 /* We're watching the same memory for writes,
4694 and the value changed since the last time we
4695 updated it, so this trap must be for a write.
4696 Ignore it. */
4697 bs->print_it = print_it_noop;
4698 bs->stop = 0;
4699 }
4700 }
4701 break;
4702 case WP_VALUE_NOT_CHANGED:
4703 if (b->base.type == bp_hardware_watchpoint
4704 || b->base.type == bp_watchpoint)
4705 {
4706 /* Don't stop: write watchpoints shouldn't fire if
4707 the value hasn't changed. */
4708 bs->print_it = print_it_noop;
4709 bs->stop = 0;
4710 }
4711 /* Stop. */
4712 break;
4713 default:
4714 /* Can't happen. */
4715 case 0:
4716 /* Error from catch_errors. */
4717 printf_filtered (_("Watchpoint %d deleted.\n"), b->base.number);
4718 watchpoint_del_at_next_stop (b);
4719 /* We've already printed what needs to be printed. */
4720 bs->print_it = print_it_done;
4721 break;
4722 }
4723 }
4724 else /* must_check_value == 0 */
4725 {
4726 /* This is a case where some watchpoint(s) triggered, but
4727 not at the address of this watchpoint, or else no
4728 watchpoint triggered after all. So don't print
4729 anything for this watchpoint. */
4730 bs->print_it = print_it_noop;
4731 bs->stop = 0;
4732 }
4733 }
4734 }
4735
4736
4737 /* Check conditions (condition proper, frame, thread and ignore count)
4738 of breakpoint referred to by BS. If we should not stop for this
4739 breakpoint, set BS->stop to 0. */
4740
4741 static void
4742 bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
4743 {
4744 int thread_id = pid_to_thread_id (ptid);
4745 const struct bp_location *bl;
4746 struct breakpoint *b;
4747
4748 /* BS is built for existing struct breakpoint. */
4749 bl = bs->bp_location_at;
4750 gdb_assert (bl != NULL);
4751 b = bs->breakpoint_at;
4752 gdb_assert (b != NULL);
4753
4754 /* Even if the target evaluated the condition on its end and notified GDB, we
4755 need to do so again since GDB does not know if we stopped due to a
4756 breakpoint or a single step breakpoint. */
4757
4758 if (frame_id_p (b->frame_id)
4759 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
4760 bs->stop = 0;
4761 else if (bs->stop)
4762 {
4763 int value_is_zero = 0;
4764 struct expression *cond;
4765
4766 /* Evaluate Python breakpoints that have a "stop"
4767 method implemented. */
4768 if (b->py_bp_object)
4769 bs->stop = gdbpy_should_stop (b->py_bp_object);
4770
4771 if (is_watchpoint (b))
4772 {
4773 struct watchpoint *w = (struct watchpoint *) b;
4774
4775 cond = w->cond_exp;
4776 }
4777 else
4778 cond = bl->cond;
4779
4780 if (cond && b->disposition != disp_del_at_next_stop)
4781 {
4782 int within_current_scope = 1;
4783 struct watchpoint * w;
4784
4785 /* We use value_mark and value_free_to_mark because it could
4786 be a long time before we return to the command level and
4787 call free_all_values. We can't call free_all_values
4788 because we might be in the middle of evaluating a
4789 function call. */
4790 struct value *mark = value_mark ();
4791
4792 if (is_watchpoint (b))
4793 w = (struct watchpoint *) b;
4794 else
4795 w = NULL;
4796
4797 /* Need to select the frame, with all that implies so that
4798 the conditions will have the right context. Because we
4799 use the frame, we will not see an inlined function's
4800 variables when we arrive at a breakpoint at the start
4801 of the inlined function; the current frame will be the
4802 call site. */
4803 if (w == NULL || w->cond_exp_valid_block == NULL)
4804 select_frame (get_current_frame ());
4805 else
4806 {
4807 struct frame_info *frame;
4808
4809 /* For local watchpoint expressions, which particular
4810 instance of a local is being watched matters, so we
4811 keep track of the frame to evaluate the expression
4812 in. To evaluate the condition however, it doesn't
4813 really matter which instantiation of the function
4814 where the condition makes sense triggers the
4815 watchpoint. This allows an expression like "watch
4816 global if q > 10" set in `func', catch writes to
4817 global on all threads that call `func', or catch
4818 writes on all recursive calls of `func' by a single
4819 thread. We simply always evaluate the condition in
4820 the innermost frame that's executing where it makes
4821 sense to evaluate the condition. It seems
4822 intuitive. */
4823 frame = block_innermost_frame (w->cond_exp_valid_block);
4824 if (frame != NULL)
4825 select_frame (frame);
4826 else
4827 within_current_scope = 0;
4828 }
4829 if (within_current_scope)
4830 value_is_zero
4831 = catch_errors (breakpoint_cond_eval, cond,
4832 "Error in testing breakpoint condition:\n",
4833 RETURN_MASK_ALL);
4834 else
4835 {
4836 warning (_("Watchpoint condition cannot be tested "
4837 "in the current scope"));
4838 /* If we failed to set the right context for this
4839 watchpoint, unconditionally report it. */
4840 value_is_zero = 0;
4841 }
4842 /* FIXME-someday, should give breakpoint #. */
4843 value_free_to_mark (mark);
4844 }
4845
4846 if (cond && value_is_zero)
4847 {
4848 bs->stop = 0;
4849 }
4850 else if (b->thread != -1 && b->thread != thread_id)
4851 {
4852 bs->stop = 0;
4853 }
4854 else if (b->ignore_count > 0)
4855 {
4856 b->ignore_count--;
4857 annotate_ignore_count_change ();
4858 bs->stop = 0;
4859 /* Increase the hit count even though we don't stop. */
4860 ++(b->hit_count);
4861 observer_notify_breakpoint_modified (b);
4862 }
4863 }
4864 }
4865
4866
4867 /* Get a bpstat associated with having just stopped at address
4868 BP_ADDR in thread PTID.
4869
4870 Determine whether we stopped at a breakpoint, etc, or whether we
4871 don't understand this stop. Result is a chain of bpstat's such
4872 that:
4873
4874 if we don't understand the stop, the result is a null pointer.
4875
4876 if we understand why we stopped, the result is not null.
4877
4878 Each element of the chain refers to a particular breakpoint or
4879 watchpoint at which we have stopped. (We may have stopped for
4880 several reasons concurrently.)
4881
4882 Each element of the chain has valid next, breakpoint_at,
4883 commands, FIXME??? fields. */
4884
4885 bpstat
4886 bpstat_stop_status (struct address_space *aspace,
4887 CORE_ADDR bp_addr, ptid_t ptid,
4888 const struct target_waitstatus *ws)
4889 {
4890 struct breakpoint *b = NULL;
4891 struct bp_location *bl;
4892 struct bp_location *loc;
4893 /* First item of allocated bpstat's. */
4894 bpstat bs_head = NULL, *bs_link = &bs_head;
4895 /* Pointer to the last thing in the chain currently. */
4896 bpstat bs;
4897 int ix;
4898 int need_remove_insert;
4899 int removed_any;
4900
4901 /* First, build the bpstat chain with locations that explain a
4902 target stop, while being careful to not set the target running,
4903 as that may invalidate locations (in particular watchpoint
4904 locations are recreated). Resuming will happen here with
4905 breakpoint conditions or watchpoint expressions that include
4906 inferior function calls. */
4907
4908 ALL_BREAKPOINTS (b)
4909 {
4910 if (!breakpoint_enabled (b) && b->enable_state != bp_permanent)
4911 continue;
4912
4913 for (bl = b->loc; bl != NULL; bl = bl->next)
4914 {
4915 /* For hardware watchpoints, we look only at the first
4916 location. The watchpoint_check function will work on the
4917 entire expression, not the individual locations. For
4918 read watchpoints, the watchpoints_triggered function has
4919 checked all locations already. */
4920 if (b->type == bp_hardware_watchpoint && bl != b->loc)
4921 break;
4922
4923 if (bl->shlib_disabled)
4924 continue;
4925
4926 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
4927 continue;
4928
4929 /* Come here if it's a watchpoint, or if the break address
4930 matches. */
4931
4932 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
4933 explain stop. */
4934
4935 /* Assume we stop. Should we find a watchpoint that is not
4936 actually triggered, or if the condition of the breakpoint
4937 evaluates as false, we'll reset 'stop' to 0. */
4938 bs->stop = 1;
4939 bs->print = 1;
4940
4941 /* If this is a scope breakpoint, mark the associated
4942 watchpoint as triggered so that we will handle the
4943 out-of-scope event. We'll get to the watchpoint next
4944 iteration. */
4945 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
4946 {
4947 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
4948
4949 w->watchpoint_triggered = watch_triggered_yes;
4950 }
4951 }
4952 }
4953
4954 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
4955 {
4956 if (breakpoint_location_address_match (loc, aspace, bp_addr))
4957 {
4958 bs = bpstat_alloc (loc, &bs_link);
4959 /* For hits of moribund locations, we should just proceed. */
4960 bs->stop = 0;
4961 bs->print = 0;
4962 bs->print_it = print_it_noop;
4963 }
4964 }
4965
4966 /* A bit of special processing for shlib breakpoints. We need to
4967 process solib loading here, so that the lists of loaded and
4968 unloaded libraries are correct before we handle "catch load" and
4969 "catch unload". */
4970 for (bs = bs_head; bs != NULL; bs = bs->next)
4971 {
4972 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
4973 {
4974 handle_solib_event ();
4975 break;
4976 }
4977 }
4978
4979 /* Now go through the locations that caused the target to stop, and
4980 check whether we're interested in reporting this stop to higher
4981 layers, or whether we should resume the target transparently. */
4982
4983 removed_any = 0;
4984
4985 for (bs = bs_head; bs != NULL; bs = bs->next)
4986 {
4987 if (!bs->stop)
4988 continue;
4989
4990 b = bs->breakpoint_at;
4991 b->ops->check_status (bs);
4992 if (bs->stop)
4993 {
4994 bpstat_check_breakpoint_conditions (bs, ptid);
4995
4996 if (bs->stop)
4997 {
4998 ++(b->hit_count);
4999 observer_notify_breakpoint_modified (b);
5000
5001 /* We will stop here. */
5002 if (b->disposition == disp_disable)
5003 {
5004 --(b->enable_count);
5005 if (b->enable_count <= 0
5006 && b->enable_state != bp_permanent)
5007 b->enable_state = bp_disabled;
5008 removed_any = 1;
5009 }
5010 if (b->silent)
5011 bs->print = 0;
5012 bs->commands = b->commands;
5013 incref_counted_command_line (bs->commands);
5014 if (command_line_is_silent (bs->commands
5015 ? bs->commands->commands : NULL))
5016 bs->print = 0;
5017 }
5018
5019 }
5020
5021 /* Print nothing for this entry if we don't stop or don't
5022 print. */
5023 if (!bs->stop || !bs->print)
5024 bs->print_it = print_it_noop;
5025 }
5026
5027 /* If we aren't stopping, the value of some hardware watchpoint may
5028 not have changed, but the intermediate memory locations we are
5029 watching may have. Don't bother if we're stopping; this will get
5030 done later. */
5031 need_remove_insert = 0;
5032 if (! bpstat_causes_stop (bs_head))
5033 for (bs = bs_head; bs != NULL; bs = bs->next)
5034 if (!bs->stop
5035 && bs->breakpoint_at
5036 && is_hardware_watchpoint (bs->breakpoint_at))
5037 {
5038 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5039
5040 update_watchpoint (w, 0 /* don't reparse. */);
5041 need_remove_insert = 1;
5042 }
5043
5044 if (need_remove_insert)
5045 update_global_location_list (1);
5046 else if (removed_any)
5047 update_global_location_list (0);
5048
5049 return bs_head;
5050 }
5051
5052 static void
5053 handle_jit_event (void)
5054 {
5055 struct frame_info *frame;
5056 struct gdbarch *gdbarch;
5057
5058 /* Switch terminal for any messages produced by
5059 breakpoint_re_set. */
5060 target_terminal_ours_for_output ();
5061
5062 frame = get_current_frame ();
5063 gdbarch = get_frame_arch (frame);
5064
5065 jit_event_handler (gdbarch);
5066
5067 target_terminal_inferior ();
5068 }
5069
5070 /* Handle an solib event by calling solib_add. */
5071
5072 void
5073 handle_solib_event (void)
5074 {
5075 clear_program_space_solib_cache (current_inferior ()->pspace);
5076
5077 /* Check for any newly added shared libraries if we're supposed to
5078 be adding them automatically. Switch terminal for any messages
5079 produced by breakpoint_re_set. */
5080 target_terminal_ours_for_output ();
5081 #ifdef SOLIB_ADD
5082 SOLIB_ADD (NULL, 0, &current_target, auto_solib_add);
5083 #else
5084 solib_add (NULL, 0, &current_target, auto_solib_add);
5085 #endif
5086 target_terminal_inferior ();
5087 }
5088
5089 /* Prepare WHAT final decision for infrun. */
5090
5091 /* Decide what infrun needs to do with this bpstat. */
5092
5093 struct bpstat_what
5094 bpstat_what (bpstat bs_head)
5095 {
5096 struct bpstat_what retval;
5097 int jit_event = 0;
5098 bpstat bs;
5099
5100 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
5101 retval.call_dummy = STOP_NONE;
5102 retval.is_longjmp = 0;
5103
5104 for (bs = bs_head; bs != NULL; bs = bs->next)
5105 {
5106 /* Extract this BS's action. After processing each BS, we check
5107 if its action overrides all we've seem so far. */
5108 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5109 enum bptype bptype;
5110
5111 if (bs->breakpoint_at == NULL)
5112 {
5113 /* I suspect this can happen if it was a momentary
5114 breakpoint which has since been deleted. */
5115 bptype = bp_none;
5116 }
5117 else
5118 bptype = bs->breakpoint_at->type;
5119
5120 switch (bptype)
5121 {
5122 case bp_none:
5123 break;
5124 case bp_breakpoint:
5125 case bp_hardware_breakpoint:
5126 case bp_until:
5127 case bp_finish:
5128 case bp_shlib_event:
5129 if (bs->stop)
5130 {
5131 if (bs->print)
5132 this_action = BPSTAT_WHAT_STOP_NOISY;
5133 else
5134 this_action = BPSTAT_WHAT_STOP_SILENT;
5135 }
5136 else
5137 this_action = BPSTAT_WHAT_SINGLE;
5138 break;
5139 case bp_watchpoint:
5140 case bp_hardware_watchpoint:
5141 case bp_read_watchpoint:
5142 case bp_access_watchpoint:
5143 if (bs->stop)
5144 {
5145 if (bs->print)
5146 this_action = BPSTAT_WHAT_STOP_NOISY;
5147 else
5148 this_action = BPSTAT_WHAT_STOP_SILENT;
5149 }
5150 else
5151 {
5152 /* There was a watchpoint, but we're not stopping.
5153 This requires no further action. */
5154 }
5155 break;
5156 case bp_longjmp:
5157 case bp_exception:
5158 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5159 retval.is_longjmp = bptype == bp_longjmp;
5160 break;
5161 case bp_longjmp_resume:
5162 case bp_exception_resume:
5163 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5164 retval.is_longjmp = bptype == bp_longjmp_resume;
5165 break;
5166 case bp_step_resume:
5167 if (bs->stop)
5168 this_action = BPSTAT_WHAT_STEP_RESUME;
5169 else
5170 {
5171 /* It is for the wrong frame. */
5172 this_action = BPSTAT_WHAT_SINGLE;
5173 }
5174 break;
5175 case bp_hp_step_resume:
5176 if (bs->stop)
5177 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5178 else
5179 {
5180 /* It is for the wrong frame. */
5181 this_action = BPSTAT_WHAT_SINGLE;
5182 }
5183 break;
5184 case bp_watchpoint_scope:
5185 case bp_thread_event:
5186 case bp_overlay_event:
5187 case bp_longjmp_master:
5188 case bp_std_terminate_master:
5189 case bp_exception_master:
5190 this_action = BPSTAT_WHAT_SINGLE;
5191 break;
5192 case bp_catchpoint:
5193 if (bs->stop)
5194 {
5195 if (bs->print)
5196 this_action = BPSTAT_WHAT_STOP_NOISY;
5197 else
5198 this_action = BPSTAT_WHAT_STOP_SILENT;
5199 }
5200 else
5201 {
5202 /* There was a catchpoint, but we're not stopping.
5203 This requires no further action. */
5204 }
5205 break;
5206 case bp_jit_event:
5207 jit_event = 1;
5208 this_action = BPSTAT_WHAT_SINGLE;
5209 break;
5210 case bp_call_dummy:
5211 /* Make sure the action is stop (silent or noisy),
5212 so infrun.c pops the dummy frame. */
5213 retval.call_dummy = STOP_STACK_DUMMY;
5214 this_action = BPSTAT_WHAT_STOP_SILENT;
5215 break;
5216 case bp_std_terminate:
5217 /* Make sure the action is stop (silent or noisy),
5218 so infrun.c pops the dummy frame. */
5219 retval.call_dummy = STOP_STD_TERMINATE;
5220 this_action = BPSTAT_WHAT_STOP_SILENT;
5221 break;
5222 case bp_tracepoint:
5223 case bp_fast_tracepoint:
5224 case bp_static_tracepoint:
5225 /* Tracepoint hits should not be reported back to GDB, and
5226 if one got through somehow, it should have been filtered
5227 out already. */
5228 internal_error (__FILE__, __LINE__,
5229 _("bpstat_what: tracepoint encountered"));
5230 break;
5231 case bp_gnu_ifunc_resolver:
5232 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5233 this_action = BPSTAT_WHAT_SINGLE;
5234 break;
5235 case bp_gnu_ifunc_resolver_return:
5236 /* The breakpoint will be removed, execution will restart from the
5237 PC of the former breakpoint. */
5238 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5239 break;
5240
5241 case bp_dprintf:
5242 this_action = BPSTAT_WHAT_STOP_SILENT;
5243 break;
5244
5245 default:
5246 internal_error (__FILE__, __LINE__,
5247 _("bpstat_what: unhandled bptype %d"), (int) bptype);
5248 }
5249
5250 retval.main_action = max (retval.main_action, this_action);
5251 }
5252
5253 /* These operations may affect the bs->breakpoint_at state so they are
5254 delayed after MAIN_ACTION is decided above. */
5255
5256 if (jit_event)
5257 {
5258 if (debug_infrun)
5259 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_jit_event\n");
5260
5261 handle_jit_event ();
5262 }
5263
5264 for (bs = bs_head; bs != NULL; bs = bs->next)
5265 {
5266 struct breakpoint *b = bs->breakpoint_at;
5267
5268 if (b == NULL)
5269 continue;
5270 switch (b->type)
5271 {
5272 case bp_gnu_ifunc_resolver:
5273 gnu_ifunc_resolver_stop (b);
5274 break;
5275 case bp_gnu_ifunc_resolver_return:
5276 gnu_ifunc_resolver_return_stop (b);
5277 break;
5278 }
5279 }
5280
5281 return retval;
5282 }
5283
5284 /* Nonzero if we should step constantly (e.g. watchpoints on machines
5285 without hardware support). This isn't related to a specific bpstat,
5286 just to things like whether watchpoints are set. */
5287
5288 int
5289 bpstat_should_step (void)
5290 {
5291 struct breakpoint *b;
5292
5293 ALL_BREAKPOINTS (b)
5294 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
5295 return 1;
5296 return 0;
5297 }
5298
5299 int
5300 bpstat_causes_stop (bpstat bs)
5301 {
5302 for (; bs != NULL; bs = bs->next)
5303 if (bs->stop)
5304 return 1;
5305
5306 return 0;
5307 }
5308
5309 \f
5310
5311 /* Compute a string of spaces suitable to indent the next line
5312 so it starts at the position corresponding to the table column
5313 named COL_NAME in the currently active table of UIOUT. */
5314
5315 static char *
5316 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5317 {
5318 static char wrap_indent[80];
5319 int i, total_width, width, align;
5320 char *text;
5321
5322 total_width = 0;
5323 for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
5324 {
5325 if (strcmp (text, col_name) == 0)
5326 {
5327 gdb_assert (total_width < sizeof wrap_indent);
5328 memset (wrap_indent, ' ', total_width);
5329 wrap_indent[total_width] = 0;
5330
5331 return wrap_indent;
5332 }
5333
5334 total_width += width + 1;
5335 }
5336
5337 return NULL;
5338 }
5339
5340 /* Determine if the locations of this breakpoint will have their conditions
5341 evaluated by the target, host or a mix of both. Returns the following:
5342
5343 "host": Host evals condition.
5344 "host or target": Host or Target evals condition.
5345 "target": Target evals condition.
5346 */
5347
5348 static const char *
5349 bp_condition_evaluator (struct breakpoint *b)
5350 {
5351 struct bp_location *bl;
5352 char host_evals = 0;
5353 char target_evals = 0;
5354
5355 if (!b)
5356 return NULL;
5357
5358 if (!is_breakpoint (b))
5359 return NULL;
5360
5361 if (gdb_evaluates_breakpoint_condition_p ()
5362 || !target_supports_evaluation_of_breakpoint_conditions ())
5363 return condition_evaluation_host;
5364
5365 for (bl = b->loc; bl; bl = bl->next)
5366 {
5367 if (bl->cond_bytecode)
5368 target_evals++;
5369 else
5370 host_evals++;
5371 }
5372
5373 if (host_evals && target_evals)
5374 return condition_evaluation_both;
5375 else if (target_evals)
5376 return condition_evaluation_target;
5377 else
5378 return condition_evaluation_host;
5379 }
5380
5381 /* Determine the breakpoint location's condition evaluator. This is
5382 similar to bp_condition_evaluator, but for locations. */
5383
5384 static const char *
5385 bp_location_condition_evaluator (struct bp_location *bl)
5386 {
5387 if (bl && !is_breakpoint (bl->owner))
5388 return NULL;
5389
5390 if (gdb_evaluates_breakpoint_condition_p ()
5391 || !target_supports_evaluation_of_breakpoint_conditions ())
5392 return condition_evaluation_host;
5393
5394 if (bl && bl->cond_bytecode)
5395 return condition_evaluation_target;
5396 else
5397 return condition_evaluation_host;
5398 }
5399
5400 /* Print the LOC location out of the list of B->LOC locations. */
5401
5402 static void
5403 print_breakpoint_location (struct breakpoint *b,
5404 struct bp_location *loc)
5405 {
5406 struct ui_out *uiout = current_uiout;
5407 struct cleanup *old_chain = save_current_program_space ();
5408
5409 if (loc != NULL && loc->shlib_disabled)
5410 loc = NULL;
5411
5412 if (loc != NULL)
5413 set_current_program_space (loc->pspace);
5414
5415 if (b->display_canonical)
5416 ui_out_field_string (uiout, "what", b->addr_string);
5417 else if (loc && loc->source_file)
5418 {
5419 struct symbol *sym
5420 = find_pc_sect_function (loc->address, loc->section);
5421 if (sym)
5422 {
5423 ui_out_text (uiout, "in ");
5424 ui_out_field_string (uiout, "func",
5425 SYMBOL_PRINT_NAME (sym));
5426 ui_out_text (uiout, " ");
5427 ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
5428 ui_out_text (uiout, "at ");
5429 }
5430 ui_out_field_string (uiout, "file", loc->source_file);
5431 ui_out_text (uiout, ":");
5432
5433 if (ui_out_is_mi_like_p (uiout))
5434 {
5435 struct symtab_and_line sal = find_pc_line (loc->address, 0);
5436 char *fullname = symtab_to_fullname (sal.symtab);
5437
5438 if (fullname)
5439 ui_out_field_string (uiout, "fullname", fullname);
5440 }
5441
5442 ui_out_field_int (uiout, "line", loc->line_number);
5443 }
5444 else if (loc)
5445 {
5446 struct ui_file *stb = mem_fileopen ();
5447 struct cleanup *stb_chain = make_cleanup_ui_file_delete (stb);
5448
5449 print_address_symbolic (loc->gdbarch, loc->address, stb,
5450 demangle, "");
5451 ui_out_field_stream (uiout, "at", stb);
5452
5453 do_cleanups (stb_chain);
5454 }
5455 else
5456 ui_out_field_string (uiout, "pending", b->addr_string);
5457
5458 if (loc && is_breakpoint (b)
5459 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
5460 && bp_condition_evaluator (b) == condition_evaluation_both)
5461 {
5462 ui_out_text (uiout, " (");
5463 ui_out_field_string (uiout, "evaluated-by",
5464 bp_location_condition_evaluator (loc));
5465 ui_out_text (uiout, ")");
5466 }
5467
5468 do_cleanups (old_chain);
5469 }
5470
5471 static const char *
5472 bptype_string (enum bptype type)
5473 {
5474 struct ep_type_description
5475 {
5476 enum bptype type;
5477 char *description;
5478 };
5479 static struct ep_type_description bptypes[] =
5480 {
5481 {bp_none, "?deleted?"},
5482 {bp_breakpoint, "breakpoint"},
5483 {bp_hardware_breakpoint, "hw breakpoint"},
5484 {bp_until, "until"},
5485 {bp_finish, "finish"},
5486 {bp_watchpoint, "watchpoint"},
5487 {bp_hardware_watchpoint, "hw watchpoint"},
5488 {bp_read_watchpoint, "read watchpoint"},
5489 {bp_access_watchpoint, "acc watchpoint"},
5490 {bp_longjmp, "longjmp"},
5491 {bp_longjmp_resume, "longjmp resume"},
5492 {bp_exception, "exception"},
5493 {bp_exception_resume, "exception resume"},
5494 {bp_step_resume, "step resume"},
5495 {bp_hp_step_resume, "high-priority step resume"},
5496 {bp_watchpoint_scope, "watchpoint scope"},
5497 {bp_call_dummy, "call dummy"},
5498 {bp_std_terminate, "std::terminate"},
5499 {bp_shlib_event, "shlib events"},
5500 {bp_thread_event, "thread events"},
5501 {bp_overlay_event, "overlay events"},
5502 {bp_longjmp_master, "longjmp master"},
5503 {bp_std_terminate_master, "std::terminate master"},
5504 {bp_exception_master, "exception master"},
5505 {bp_catchpoint, "catchpoint"},
5506 {bp_tracepoint, "tracepoint"},
5507 {bp_fast_tracepoint, "fast tracepoint"},
5508 {bp_static_tracepoint, "static tracepoint"},
5509 {bp_dprintf, "dprintf"},
5510 {bp_jit_event, "jit events"},
5511 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
5512 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
5513 };
5514
5515 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
5516 || ((int) type != bptypes[(int) type].type))
5517 internal_error (__FILE__, __LINE__,
5518 _("bptypes table does not describe type #%d."),
5519 (int) type);
5520
5521 return bptypes[(int) type].description;
5522 }
5523
5524 /* Print B to gdb_stdout. */
5525
5526 static void
5527 print_one_breakpoint_location (struct breakpoint *b,
5528 struct bp_location *loc,
5529 int loc_number,
5530 struct bp_location **last_loc,
5531 int allflag)
5532 {
5533 struct command_line *l;
5534 static char bpenables[] = "nynny";
5535
5536 struct ui_out *uiout = current_uiout;
5537 int header_of_multiple = 0;
5538 int part_of_multiple = (loc != NULL);
5539 struct value_print_options opts;
5540
5541 get_user_print_options (&opts);
5542
5543 gdb_assert (!loc || loc_number != 0);
5544 /* See comment in print_one_breakpoint concerning treatment of
5545 breakpoints with single disabled location. */
5546 if (loc == NULL
5547 && (b->loc != NULL
5548 && (b->loc->next != NULL || !b->loc->enabled)))
5549 header_of_multiple = 1;
5550 if (loc == NULL)
5551 loc = b->loc;
5552
5553 annotate_record ();
5554
5555 /* 1 */
5556 annotate_field (0);
5557 if (part_of_multiple)
5558 {
5559 char *formatted;
5560 formatted = xstrprintf ("%d.%d", b->number, loc_number);
5561 ui_out_field_string (uiout, "number", formatted);
5562 xfree (formatted);
5563 }
5564 else
5565 {
5566 ui_out_field_int (uiout, "number", b->number);
5567 }
5568
5569 /* 2 */
5570 annotate_field (1);
5571 if (part_of_multiple)
5572 ui_out_field_skip (uiout, "type");
5573 else
5574 ui_out_field_string (uiout, "type", bptype_string (b->type));
5575
5576 /* 3 */
5577 annotate_field (2);
5578 if (part_of_multiple)
5579 ui_out_field_skip (uiout, "disp");
5580 else
5581 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
5582
5583
5584 /* 4 */
5585 annotate_field (3);
5586 if (part_of_multiple)
5587 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
5588 else
5589 ui_out_field_fmt (uiout, "enabled", "%c",
5590 bpenables[(int) b->enable_state]);
5591 ui_out_spaces (uiout, 2);
5592
5593
5594 /* 5 and 6 */
5595 if (b->ops != NULL && b->ops->print_one != NULL)
5596 {
5597 /* Although the print_one can possibly print all locations,
5598 calling it here is not likely to get any nice result. So,
5599 make sure there's just one location. */
5600 gdb_assert (b->loc == NULL || b->loc->next == NULL);
5601 b->ops->print_one (b, last_loc);
5602 }
5603 else
5604 switch (b->type)
5605 {
5606 case bp_none:
5607 internal_error (__FILE__, __LINE__,
5608 _("print_one_breakpoint: bp_none encountered\n"));
5609 break;
5610
5611 case bp_watchpoint:
5612 case bp_hardware_watchpoint:
5613 case bp_read_watchpoint:
5614 case bp_access_watchpoint:
5615 {
5616 struct watchpoint *w = (struct watchpoint *) b;
5617
5618 /* Field 4, the address, is omitted (which makes the columns
5619 not line up too nicely with the headers, but the effect
5620 is relatively readable). */
5621 if (opts.addressprint)
5622 ui_out_field_skip (uiout, "addr");
5623 annotate_field (5);
5624 ui_out_field_string (uiout, "what", w->exp_string);
5625 }
5626 break;
5627
5628 case bp_breakpoint:
5629 case bp_hardware_breakpoint:
5630 case bp_until:
5631 case bp_finish:
5632 case bp_longjmp:
5633 case bp_longjmp_resume:
5634 case bp_exception:
5635 case bp_exception_resume:
5636 case bp_step_resume:
5637 case bp_hp_step_resume:
5638 case bp_watchpoint_scope:
5639 case bp_call_dummy:
5640 case bp_std_terminate:
5641 case bp_shlib_event:
5642 case bp_thread_event:
5643 case bp_overlay_event:
5644 case bp_longjmp_master:
5645 case bp_std_terminate_master:
5646 case bp_exception_master:
5647 case bp_tracepoint:
5648 case bp_fast_tracepoint:
5649 case bp_static_tracepoint:
5650 case bp_dprintf:
5651 case bp_jit_event:
5652 case bp_gnu_ifunc_resolver:
5653 case bp_gnu_ifunc_resolver_return:
5654 if (opts.addressprint)
5655 {
5656 annotate_field (4);
5657 if (header_of_multiple)
5658 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
5659 else if (b->loc == NULL || loc->shlib_disabled)
5660 ui_out_field_string (uiout, "addr", "<PENDING>");
5661 else
5662 ui_out_field_core_addr (uiout, "addr",
5663 loc->gdbarch, loc->address);
5664 }
5665 annotate_field (5);
5666 if (!header_of_multiple)
5667 print_breakpoint_location (b, loc);
5668 if (b->loc)
5669 *last_loc = b->loc;
5670 break;
5671 }
5672
5673
5674 /* For backward compatibility, don't display inferiors unless there
5675 are several. */
5676 if (loc != NULL
5677 && !header_of_multiple
5678 && (allflag
5679 || (!gdbarch_has_global_breakpoints (target_gdbarch)
5680 && (number_of_program_spaces () > 1
5681 || number_of_inferiors () > 1)
5682 /* LOC is for existing B, it cannot be in
5683 moribund_locations and thus having NULL OWNER. */
5684 && loc->owner->type != bp_catchpoint)))
5685 {
5686 struct inferior *inf;
5687 int first = 1;
5688
5689 for (inf = inferior_list; inf != NULL; inf = inf->next)
5690 {
5691 if (inf->pspace == loc->pspace)
5692 {
5693 if (first)
5694 {
5695 first = 0;
5696 ui_out_text (uiout, " inf ");
5697 }
5698 else
5699 ui_out_text (uiout, ", ");
5700 ui_out_text (uiout, plongest (inf->num));
5701 }
5702 }
5703 }
5704
5705 if (!part_of_multiple)
5706 {
5707 if (b->thread != -1)
5708 {
5709 /* FIXME: This seems to be redundant and lost here; see the
5710 "stop only in" line a little further down. */
5711 ui_out_text (uiout, " thread ");
5712 ui_out_field_int (uiout, "thread", b->thread);
5713 }
5714 else if (b->task != 0)
5715 {
5716 ui_out_text (uiout, " task ");
5717 ui_out_field_int (uiout, "task", b->task);
5718 }
5719 }
5720
5721 ui_out_text (uiout, "\n");
5722
5723 if (!part_of_multiple)
5724 b->ops->print_one_detail (b, uiout);
5725
5726 if (part_of_multiple && frame_id_p (b->frame_id))
5727 {
5728 annotate_field (6);
5729 ui_out_text (uiout, "\tstop only in stack frame at ");
5730 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
5731 the frame ID. */
5732 ui_out_field_core_addr (uiout, "frame",
5733 b->gdbarch, b->frame_id.stack_addr);
5734 ui_out_text (uiout, "\n");
5735 }
5736
5737 if (!part_of_multiple && b->cond_string)
5738 {
5739 annotate_field (7);
5740 if (is_tracepoint (b))
5741 ui_out_text (uiout, "\ttrace only if ");
5742 else
5743 ui_out_text (uiout, "\tstop only if ");
5744 ui_out_field_string (uiout, "cond", b->cond_string);
5745
5746 /* Print whether the target is doing the breakpoint's condition
5747 evaluation. If GDB is doing the evaluation, don't print anything. */
5748 if (is_breakpoint (b)
5749 && breakpoint_condition_evaluation_mode ()
5750 == condition_evaluation_target)
5751 {
5752 ui_out_text (uiout, " (");
5753 ui_out_field_string (uiout, "evaluated-by",
5754 bp_condition_evaluator (b));
5755 ui_out_text (uiout, " evals)");
5756 }
5757 ui_out_text (uiout, "\n");
5758 }
5759
5760 if (!part_of_multiple && b->thread != -1)
5761 {
5762 /* FIXME should make an annotation for this. */
5763 ui_out_text (uiout, "\tstop only in thread ");
5764 ui_out_field_int (uiout, "thread", b->thread);
5765 ui_out_text (uiout, "\n");
5766 }
5767
5768 if (!part_of_multiple && b->hit_count)
5769 {
5770 /* FIXME should make an annotation for this. */
5771 if (is_catchpoint (b))
5772 ui_out_text (uiout, "\tcatchpoint");
5773 else if (is_tracepoint (b))
5774 ui_out_text (uiout, "\ttracepoint");
5775 else
5776 ui_out_text (uiout, "\tbreakpoint");
5777 ui_out_text (uiout, " already hit ");
5778 ui_out_field_int (uiout, "times", b->hit_count);
5779 if (b->hit_count == 1)
5780 ui_out_text (uiout, " time\n");
5781 else
5782 ui_out_text (uiout, " times\n");
5783 }
5784
5785 /* Output the count also if it is zero, but only if this is mi.
5786 FIXME: Should have a better test for this. */
5787 if (ui_out_is_mi_like_p (uiout))
5788 if (!part_of_multiple && b->hit_count == 0)
5789 ui_out_field_int (uiout, "times", b->hit_count);
5790
5791 if (!part_of_multiple && b->ignore_count)
5792 {
5793 annotate_field (8);
5794 ui_out_text (uiout, "\tignore next ");
5795 ui_out_field_int (uiout, "ignore", b->ignore_count);
5796 ui_out_text (uiout, " hits\n");
5797 }
5798
5799 /* Note that an enable count of 1 corresponds to "enable once"
5800 behavior, which is reported by the combination of enablement and
5801 disposition, so we don't need to mention it here. */
5802 if (!part_of_multiple && b->enable_count > 1)
5803 {
5804 annotate_field (8);
5805 ui_out_text (uiout, "\tdisable after ");
5806 /* Tweak the wording to clarify that ignore and enable counts
5807 are distinct, and have additive effect. */
5808 if (b->ignore_count)
5809 ui_out_text (uiout, "additional ");
5810 else
5811 ui_out_text (uiout, "next ");
5812 ui_out_field_int (uiout, "enable", b->enable_count);
5813 ui_out_text (uiout, " hits\n");
5814 }
5815
5816 if (!part_of_multiple && is_tracepoint (b))
5817 {
5818 struct tracepoint *tp = (struct tracepoint *) b;
5819
5820 if (tp->traceframe_usage)
5821 {
5822 ui_out_text (uiout, "\ttrace buffer usage ");
5823 ui_out_field_int (uiout, "traceframe-usage", tp->traceframe_usage);
5824 ui_out_text (uiout, " bytes\n");
5825 }
5826 }
5827
5828 l = b->commands ? b->commands->commands : NULL;
5829 if (!part_of_multiple && l)
5830 {
5831 struct cleanup *script_chain;
5832
5833 annotate_field (9);
5834 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
5835 print_command_lines (uiout, l, 4);
5836 do_cleanups (script_chain);
5837 }
5838
5839 if (is_tracepoint (b))
5840 {
5841 struct tracepoint *t = (struct tracepoint *) b;
5842
5843 if (!part_of_multiple && t->pass_count)
5844 {
5845 annotate_field (10);
5846 ui_out_text (uiout, "\tpass count ");
5847 ui_out_field_int (uiout, "pass", t->pass_count);
5848 ui_out_text (uiout, " \n");
5849 }
5850 }
5851
5852 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
5853 {
5854 if (is_watchpoint (b))
5855 {
5856 struct watchpoint *w = (struct watchpoint *) b;
5857
5858 ui_out_field_string (uiout, "original-location", w->exp_string);
5859 }
5860 else if (b->addr_string)
5861 ui_out_field_string (uiout, "original-location", b->addr_string);
5862 }
5863 }
5864
5865 static void
5866 print_one_breakpoint (struct breakpoint *b,
5867 struct bp_location **last_loc,
5868 int allflag)
5869 {
5870 struct cleanup *bkpt_chain;
5871 struct ui_out *uiout = current_uiout;
5872
5873 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
5874
5875 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
5876 do_cleanups (bkpt_chain);
5877
5878 /* If this breakpoint has custom print function,
5879 it's already printed. Otherwise, print individual
5880 locations, if any. */
5881 if (b->ops == NULL || b->ops->print_one == NULL)
5882 {
5883 /* If breakpoint has a single location that is disabled, we
5884 print it as if it had several locations, since otherwise it's
5885 hard to represent "breakpoint enabled, location disabled"
5886 situation.
5887
5888 Note that while hardware watchpoints have several locations
5889 internally, that's not a property exposed to user. */
5890 if (b->loc
5891 && !is_hardware_watchpoint (b)
5892 && (b->loc->next || !b->loc->enabled))
5893 {
5894 struct bp_location *loc;
5895 int n = 1;
5896
5897 for (loc = b->loc; loc; loc = loc->next, ++n)
5898 {
5899 struct cleanup *inner2 =
5900 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
5901 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
5902 do_cleanups (inner2);
5903 }
5904 }
5905 }
5906 }
5907
5908 static int
5909 breakpoint_address_bits (struct breakpoint *b)
5910 {
5911 int print_address_bits = 0;
5912 struct bp_location *loc;
5913
5914 for (loc = b->loc; loc; loc = loc->next)
5915 {
5916 int addr_bit;
5917
5918 /* Software watchpoints that aren't watching memory don't have
5919 an address to print. */
5920 if (b->type == bp_watchpoint && loc->watchpoint_type == -1)
5921 continue;
5922
5923 addr_bit = gdbarch_addr_bit (loc->gdbarch);
5924 if (addr_bit > print_address_bits)
5925 print_address_bits = addr_bit;
5926 }
5927
5928 return print_address_bits;
5929 }
5930
5931 struct captured_breakpoint_query_args
5932 {
5933 int bnum;
5934 };
5935
5936 static int
5937 do_captured_breakpoint_query (struct ui_out *uiout, void *data)
5938 {
5939 struct captured_breakpoint_query_args *args = data;
5940 struct breakpoint *b;
5941 struct bp_location *dummy_loc = NULL;
5942
5943 ALL_BREAKPOINTS (b)
5944 {
5945 if (args->bnum == b->number)
5946 {
5947 print_one_breakpoint (b, &dummy_loc, 0);
5948 return GDB_RC_OK;
5949 }
5950 }
5951 return GDB_RC_NONE;
5952 }
5953
5954 enum gdb_rc
5955 gdb_breakpoint_query (struct ui_out *uiout, int bnum,
5956 char **error_message)
5957 {
5958 struct captured_breakpoint_query_args args;
5959
5960 args.bnum = bnum;
5961 /* For the moment we don't trust print_one_breakpoint() to not throw
5962 an error. */
5963 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
5964 error_message, RETURN_MASK_ALL) < 0)
5965 return GDB_RC_FAIL;
5966 else
5967 return GDB_RC_OK;
5968 }
5969
5970 /* Return true if this breakpoint was set by the user, false if it is
5971 internal or momentary. */
5972
5973 int
5974 user_breakpoint_p (struct breakpoint *b)
5975 {
5976 return b->number > 0;
5977 }
5978
5979 /* Print information on user settable breakpoint (watchpoint, etc)
5980 number BNUM. If BNUM is -1 print all user-settable breakpoints.
5981 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
5982 FILTER is non-NULL, call it on each breakpoint and only include the
5983 ones for which it returns non-zero. Return the total number of
5984 breakpoints listed. */
5985
5986 static int
5987 breakpoint_1 (char *args, int allflag,
5988 int (*filter) (const struct breakpoint *))
5989 {
5990 struct breakpoint *b;
5991 struct bp_location *last_loc = NULL;
5992 int nr_printable_breakpoints;
5993 struct cleanup *bkpttbl_chain;
5994 struct value_print_options opts;
5995 int print_address_bits = 0;
5996 int print_type_col_width = 14;
5997 struct ui_out *uiout = current_uiout;
5998
5999 get_user_print_options (&opts);
6000
6001 /* Compute the number of rows in the table, as well as the size
6002 required for address fields. */
6003 nr_printable_breakpoints = 0;
6004 ALL_BREAKPOINTS (b)
6005 {
6006 /* If we have a filter, only list the breakpoints it accepts. */
6007 if (filter && !filter (b))
6008 continue;
6009
6010 /* If we have an "args" string, it is a list of breakpoints to
6011 accept. Skip the others. */
6012 if (args != NULL && *args != '\0')
6013 {
6014 if (allflag && parse_and_eval_long (args) != b->number)
6015 continue;
6016 if (!allflag && !number_is_in_list (args, b->number))
6017 continue;
6018 }
6019
6020 if (allflag || user_breakpoint_p (b))
6021 {
6022 int addr_bit, type_len;
6023
6024 addr_bit = breakpoint_address_bits (b);
6025 if (addr_bit > print_address_bits)
6026 print_address_bits = addr_bit;
6027
6028 type_len = strlen (bptype_string (b->type));
6029 if (type_len > print_type_col_width)
6030 print_type_col_width = type_len;
6031
6032 nr_printable_breakpoints++;
6033 }
6034 }
6035
6036 if (opts.addressprint)
6037 bkpttbl_chain
6038 = make_cleanup_ui_out_table_begin_end (uiout, 6,
6039 nr_printable_breakpoints,
6040 "BreakpointTable");
6041 else
6042 bkpttbl_chain
6043 = make_cleanup_ui_out_table_begin_end (uiout, 5,
6044 nr_printable_breakpoints,
6045 "BreakpointTable");
6046
6047 if (nr_printable_breakpoints > 0)
6048 annotate_breakpoints_headers ();
6049 if (nr_printable_breakpoints > 0)
6050 annotate_field (0);
6051 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
6052 if (nr_printable_breakpoints > 0)
6053 annotate_field (1);
6054 ui_out_table_header (uiout, print_type_col_width, ui_left,
6055 "type", "Type"); /* 2 */
6056 if (nr_printable_breakpoints > 0)
6057 annotate_field (2);
6058 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
6059 if (nr_printable_breakpoints > 0)
6060 annotate_field (3);
6061 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
6062 if (opts.addressprint)
6063 {
6064 if (nr_printable_breakpoints > 0)
6065 annotate_field (4);
6066 if (print_address_bits <= 32)
6067 ui_out_table_header (uiout, 10, ui_left,
6068 "addr", "Address"); /* 5 */
6069 else
6070 ui_out_table_header (uiout, 18, ui_left,
6071 "addr", "Address"); /* 5 */
6072 }
6073 if (nr_printable_breakpoints > 0)
6074 annotate_field (5);
6075 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
6076 ui_out_table_body (uiout);
6077 if (nr_printable_breakpoints > 0)
6078 annotate_breakpoints_table ();
6079
6080 ALL_BREAKPOINTS (b)
6081 {
6082 QUIT;
6083 /* If we have a filter, only list the breakpoints it accepts. */
6084 if (filter && !filter (b))
6085 continue;
6086
6087 /* If we have an "args" string, it is a list of breakpoints to
6088 accept. Skip the others. */
6089
6090 if (args != NULL && *args != '\0')
6091 {
6092 if (allflag) /* maintenance info breakpoint */
6093 {
6094 if (parse_and_eval_long (args) != b->number)
6095 continue;
6096 }
6097 else /* all others */
6098 {
6099 if (!number_is_in_list (args, b->number))
6100 continue;
6101 }
6102 }
6103 /* We only print out user settable breakpoints unless the
6104 allflag is set. */
6105 if (allflag || user_breakpoint_p (b))
6106 print_one_breakpoint (b, &last_loc, allflag);
6107 }
6108
6109 do_cleanups (bkpttbl_chain);
6110
6111 if (nr_printable_breakpoints == 0)
6112 {
6113 /* If there's a filter, let the caller decide how to report
6114 empty list. */
6115 if (!filter)
6116 {
6117 if (args == NULL || *args == '\0')
6118 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
6119 else
6120 ui_out_message (uiout, 0,
6121 "No breakpoint or watchpoint matching '%s'.\n",
6122 args);
6123 }
6124 }
6125 else
6126 {
6127 if (last_loc && !server_command)
6128 set_next_address (last_loc->gdbarch, last_loc->address);
6129 }
6130
6131 /* FIXME? Should this be moved up so that it is only called when
6132 there have been breakpoints? */
6133 annotate_breakpoints_table_end ();
6134
6135 return nr_printable_breakpoints;
6136 }
6137
6138 /* Display the value of default-collect in a way that is generally
6139 compatible with the breakpoint list. */
6140
6141 static void
6142 default_collect_info (void)
6143 {
6144 struct ui_out *uiout = current_uiout;
6145
6146 /* If it has no value (which is frequently the case), say nothing; a
6147 message like "No default-collect." gets in user's face when it's
6148 not wanted. */
6149 if (!*default_collect)
6150 return;
6151
6152 /* The following phrase lines up nicely with per-tracepoint collect
6153 actions. */
6154 ui_out_text (uiout, "default collect ");
6155 ui_out_field_string (uiout, "default-collect", default_collect);
6156 ui_out_text (uiout, " \n");
6157 }
6158
6159 static void
6160 breakpoints_info (char *args, int from_tty)
6161 {
6162 breakpoint_1 (args, 0, NULL);
6163
6164 default_collect_info ();
6165 }
6166
6167 static void
6168 watchpoints_info (char *args, int from_tty)
6169 {
6170 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
6171 struct ui_out *uiout = current_uiout;
6172
6173 if (num_printed == 0)
6174 {
6175 if (args == NULL || *args == '\0')
6176 ui_out_message (uiout, 0, "No watchpoints.\n");
6177 else
6178 ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
6179 }
6180 }
6181
6182 static void
6183 maintenance_info_breakpoints (char *args, int from_tty)
6184 {
6185 breakpoint_1 (args, 1, NULL);
6186
6187 default_collect_info ();
6188 }
6189
6190 static int
6191 breakpoint_has_pc (struct breakpoint *b,
6192 struct program_space *pspace,
6193 CORE_ADDR pc, struct obj_section *section)
6194 {
6195 struct bp_location *bl = b->loc;
6196
6197 for (; bl; bl = bl->next)
6198 {
6199 if (bl->pspace == pspace
6200 && bl->address == pc
6201 && (!overlay_debugging || bl->section == section))
6202 return 1;
6203 }
6204 return 0;
6205 }
6206
6207 /* Print a message describing any user-breakpoints set at PC. This
6208 concerns with logical breakpoints, so we match program spaces, not
6209 address spaces. */
6210
6211 static void
6212 describe_other_breakpoints (struct gdbarch *gdbarch,
6213 struct program_space *pspace, CORE_ADDR pc,
6214 struct obj_section *section, int thread)
6215 {
6216 int others = 0;
6217 struct breakpoint *b;
6218
6219 ALL_BREAKPOINTS (b)
6220 others += (user_breakpoint_p (b)
6221 && breakpoint_has_pc (b, pspace, pc, section));
6222 if (others > 0)
6223 {
6224 if (others == 1)
6225 printf_filtered (_("Note: breakpoint "));
6226 else /* if (others == ???) */
6227 printf_filtered (_("Note: breakpoints "));
6228 ALL_BREAKPOINTS (b)
6229 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
6230 {
6231 others--;
6232 printf_filtered ("%d", b->number);
6233 if (b->thread == -1 && thread != -1)
6234 printf_filtered (" (all threads)");
6235 else if (b->thread != -1)
6236 printf_filtered (" (thread %d)", b->thread);
6237 printf_filtered ("%s%s ",
6238 ((b->enable_state == bp_disabled
6239 || b->enable_state == bp_call_disabled)
6240 ? " (disabled)"
6241 : b->enable_state == bp_permanent
6242 ? " (permanent)"
6243 : ""),
6244 (others > 1) ? ","
6245 : ((others == 1) ? " and" : ""));
6246 }
6247 printf_filtered (_("also set at pc "));
6248 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
6249 printf_filtered (".\n");
6250 }
6251 }
6252 \f
6253
6254 /* Return true iff it is meaningful to use the address member of
6255 BPT. For some breakpoint types, the address member is irrelevant
6256 and it makes no sense to attempt to compare it to other addresses
6257 (or use it for any other purpose either).
6258
6259 More specifically, each of the following breakpoint types will
6260 always have a zero valued address and we don't want to mark
6261 breakpoints of any of these types to be a duplicate of an actual
6262 breakpoint at address zero:
6263
6264 bp_watchpoint
6265 bp_catchpoint
6266
6267 */
6268
6269 static int
6270 breakpoint_address_is_meaningful (struct breakpoint *bpt)
6271 {
6272 enum bptype type = bpt->type;
6273
6274 return (type != bp_watchpoint && type != bp_catchpoint);
6275 }
6276
6277 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6278 true if LOC1 and LOC2 represent the same watchpoint location. */
6279
6280 static int
6281 watchpoint_locations_match (struct bp_location *loc1,
6282 struct bp_location *loc2)
6283 {
6284 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6285 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6286
6287 /* Both of them must exist. */
6288 gdb_assert (w1 != NULL);
6289 gdb_assert (w2 != NULL);
6290
6291 /* If the target can evaluate the condition expression in hardware,
6292 then we we need to insert both watchpoints even if they are at
6293 the same place. Otherwise the watchpoint will only trigger when
6294 the condition of whichever watchpoint was inserted evaluates to
6295 true, not giving a chance for GDB to check the condition of the
6296 other watchpoint. */
6297 if ((w1->cond_exp
6298 && target_can_accel_watchpoint_condition (loc1->address,
6299 loc1->length,
6300 loc1->watchpoint_type,
6301 w1->cond_exp))
6302 || (w2->cond_exp
6303 && target_can_accel_watchpoint_condition (loc2->address,
6304 loc2->length,
6305 loc2->watchpoint_type,
6306 w2->cond_exp)))
6307 return 0;
6308
6309 /* Note that this checks the owner's type, not the location's. In
6310 case the target does not support read watchpoints, but does
6311 support access watchpoints, we'll have bp_read_watchpoint
6312 watchpoints with hw_access locations. Those should be considered
6313 duplicates of hw_read locations. The hw_read locations will
6314 become hw_access locations later. */
6315 return (loc1->owner->type == loc2->owner->type
6316 && loc1->pspace->aspace == loc2->pspace->aspace
6317 && loc1->address == loc2->address
6318 && loc1->length == loc2->length);
6319 }
6320
6321 /* Returns true if {ASPACE1,ADDR1} and {ASPACE2,ADDR2} represent the
6322 same breakpoint location. In most targets, this can only be true
6323 if ASPACE1 matches ASPACE2. On targets that have global
6324 breakpoints, the address space doesn't really matter. */
6325
6326 static int
6327 breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
6328 struct address_space *aspace2, CORE_ADDR addr2)
6329 {
6330 return ((gdbarch_has_global_breakpoints (target_gdbarch)
6331 || aspace1 == aspace2)
6332 && addr1 == addr2);
6333 }
6334
6335 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6336 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6337 matches ASPACE2. On targets that have global breakpoints, the address
6338 space doesn't really matter. */
6339
6340 static int
6341 breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
6342 int len1, struct address_space *aspace2,
6343 CORE_ADDR addr2)
6344 {
6345 return ((gdbarch_has_global_breakpoints (target_gdbarch)
6346 || aspace1 == aspace2)
6347 && addr2 >= addr1 && addr2 < addr1 + len1);
6348 }
6349
6350 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
6351 a ranged breakpoint. In most targets, a match happens only if ASPACE
6352 matches the breakpoint's address space. On targets that have global
6353 breakpoints, the address space doesn't really matter. */
6354
6355 static int
6356 breakpoint_location_address_match (struct bp_location *bl,
6357 struct address_space *aspace,
6358 CORE_ADDR addr)
6359 {
6360 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
6361 aspace, addr)
6362 || (bl->length
6363 && breakpoint_address_match_range (bl->pspace->aspace,
6364 bl->address, bl->length,
6365 aspace, addr)));
6366 }
6367
6368 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
6369 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
6370 true, otherwise returns false. */
6371
6372 static int
6373 tracepoint_locations_match (struct bp_location *loc1,
6374 struct bp_location *loc2)
6375 {
6376 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
6377 /* Since tracepoint locations are never duplicated with others', tracepoint
6378 locations at the same address of different tracepoints are regarded as
6379 different locations. */
6380 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
6381 else
6382 return 0;
6383 }
6384
6385 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
6386 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
6387 represent the same location. */
6388
6389 static int
6390 breakpoint_locations_match (struct bp_location *loc1,
6391 struct bp_location *loc2)
6392 {
6393 int hw_point1, hw_point2;
6394
6395 /* Both of them must not be in moribund_locations. */
6396 gdb_assert (loc1->owner != NULL);
6397 gdb_assert (loc2->owner != NULL);
6398
6399 hw_point1 = is_hardware_watchpoint (loc1->owner);
6400 hw_point2 = is_hardware_watchpoint (loc2->owner);
6401
6402 if (hw_point1 != hw_point2)
6403 return 0;
6404 else if (hw_point1)
6405 return watchpoint_locations_match (loc1, loc2);
6406 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
6407 return tracepoint_locations_match (loc1, loc2);
6408 else
6409 /* We compare bp_location.length in order to cover ranged breakpoints. */
6410 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
6411 loc2->pspace->aspace, loc2->address)
6412 && loc1->length == loc2->length);
6413 }
6414
6415 static void
6416 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
6417 int bnum, int have_bnum)
6418 {
6419 /* The longest string possibly returned by hex_string_custom
6420 is 50 chars. These must be at least that big for safety. */
6421 char astr1[64];
6422 char astr2[64];
6423
6424 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
6425 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
6426 if (have_bnum)
6427 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
6428 bnum, astr1, astr2);
6429 else
6430 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
6431 }
6432
6433 /* Adjust a breakpoint's address to account for architectural
6434 constraints on breakpoint placement. Return the adjusted address.
6435 Note: Very few targets require this kind of adjustment. For most
6436 targets, this function is simply the identity function. */
6437
6438 static CORE_ADDR
6439 adjust_breakpoint_address (struct gdbarch *gdbarch,
6440 CORE_ADDR bpaddr, enum bptype bptype)
6441 {
6442 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
6443 {
6444 /* Very few targets need any kind of breakpoint adjustment. */
6445 return bpaddr;
6446 }
6447 else if (bptype == bp_watchpoint
6448 || bptype == bp_hardware_watchpoint
6449 || bptype == bp_read_watchpoint
6450 || bptype == bp_access_watchpoint
6451 || bptype == bp_catchpoint)
6452 {
6453 /* Watchpoints and the various bp_catch_* eventpoints should not
6454 have their addresses modified. */
6455 return bpaddr;
6456 }
6457 else
6458 {
6459 CORE_ADDR adjusted_bpaddr;
6460
6461 /* Some targets have architectural constraints on the placement
6462 of breakpoint instructions. Obtain the adjusted address. */
6463 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
6464
6465 /* An adjusted breakpoint address can significantly alter
6466 a user's expectations. Print a warning if an adjustment
6467 is required. */
6468 if (adjusted_bpaddr != bpaddr)
6469 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
6470
6471 return adjusted_bpaddr;
6472 }
6473 }
6474
6475 void
6476 init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
6477 struct breakpoint *owner)
6478 {
6479 memset (loc, 0, sizeof (*loc));
6480
6481 gdb_assert (ops != NULL);
6482
6483 loc->ops = ops;
6484 loc->owner = owner;
6485 loc->cond = NULL;
6486 loc->cond_bytecode = NULL;
6487 loc->shlib_disabled = 0;
6488 loc->enabled = 1;
6489
6490 switch (owner->type)
6491 {
6492 case bp_breakpoint:
6493 case bp_until:
6494 case bp_finish:
6495 case bp_longjmp:
6496 case bp_longjmp_resume:
6497 case bp_exception:
6498 case bp_exception_resume:
6499 case bp_step_resume:
6500 case bp_hp_step_resume:
6501 case bp_watchpoint_scope:
6502 case bp_call_dummy:
6503 case bp_std_terminate:
6504 case bp_shlib_event:
6505 case bp_thread_event:
6506 case bp_overlay_event:
6507 case bp_jit_event:
6508 case bp_longjmp_master:
6509 case bp_std_terminate_master:
6510 case bp_exception_master:
6511 case bp_gnu_ifunc_resolver:
6512 case bp_gnu_ifunc_resolver_return:
6513 case bp_dprintf:
6514 loc->loc_type = bp_loc_software_breakpoint;
6515 mark_breakpoint_location_modified (loc);
6516 break;
6517 case bp_hardware_breakpoint:
6518 loc->loc_type = bp_loc_hardware_breakpoint;
6519 mark_breakpoint_location_modified (loc);
6520 break;
6521 case bp_hardware_watchpoint:
6522 case bp_read_watchpoint:
6523 case bp_access_watchpoint:
6524 loc->loc_type = bp_loc_hardware_watchpoint;
6525 break;
6526 case bp_watchpoint:
6527 case bp_catchpoint:
6528 case bp_tracepoint:
6529 case bp_fast_tracepoint:
6530 case bp_static_tracepoint:
6531 loc->loc_type = bp_loc_other;
6532 break;
6533 default:
6534 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
6535 }
6536
6537 loc->refc = 1;
6538 }
6539
6540 /* Allocate a struct bp_location. */
6541
6542 static struct bp_location *
6543 allocate_bp_location (struct breakpoint *bpt)
6544 {
6545 return bpt->ops->allocate_location (bpt);
6546 }
6547
6548 static void
6549 free_bp_location (struct bp_location *loc)
6550 {
6551 loc->ops->dtor (loc);
6552 xfree (loc);
6553 }
6554
6555 /* Increment reference count. */
6556
6557 static void
6558 incref_bp_location (struct bp_location *bl)
6559 {
6560 ++bl->refc;
6561 }
6562
6563 /* Decrement reference count. If the reference count reaches 0,
6564 destroy the bp_location. Sets *BLP to NULL. */
6565
6566 static void
6567 decref_bp_location (struct bp_location **blp)
6568 {
6569 gdb_assert ((*blp)->refc > 0);
6570
6571 if (--(*blp)->refc == 0)
6572 free_bp_location (*blp);
6573 *blp = NULL;
6574 }
6575
6576 /* Add breakpoint B at the end of the global breakpoint chain. */
6577
6578 static void
6579 add_to_breakpoint_chain (struct breakpoint *b)
6580 {
6581 struct breakpoint *b1;
6582
6583 /* Add this breakpoint to the end of the chain so that a list of
6584 breakpoints will come out in order of increasing numbers. */
6585
6586 b1 = breakpoint_chain;
6587 if (b1 == 0)
6588 breakpoint_chain = b;
6589 else
6590 {
6591 while (b1->next)
6592 b1 = b1->next;
6593 b1->next = b;
6594 }
6595 }
6596
6597 /* Initializes breakpoint B with type BPTYPE and no locations yet. */
6598
6599 static void
6600 init_raw_breakpoint_without_location (struct breakpoint *b,
6601 struct gdbarch *gdbarch,
6602 enum bptype bptype,
6603 const struct breakpoint_ops *ops)
6604 {
6605 memset (b, 0, sizeof (*b));
6606
6607 gdb_assert (ops != NULL);
6608
6609 b->ops = ops;
6610 b->type = bptype;
6611 b->gdbarch = gdbarch;
6612 b->language = current_language->la_language;
6613 b->input_radix = input_radix;
6614 b->thread = -1;
6615 b->enable_state = bp_enabled;
6616 b->next = 0;
6617 b->silent = 0;
6618 b->ignore_count = 0;
6619 b->commands = NULL;
6620 b->frame_id = null_frame_id;
6621 b->condition_not_parsed = 0;
6622 b->py_bp_object = NULL;
6623 b->related_breakpoint = b;
6624 }
6625
6626 /* Helper to set_raw_breakpoint below. Creates a breakpoint
6627 that has type BPTYPE and has no locations as yet. */
6628
6629 static struct breakpoint *
6630 set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
6631 enum bptype bptype,
6632 const struct breakpoint_ops *ops)
6633 {
6634 struct breakpoint *b = XNEW (struct breakpoint);
6635
6636 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
6637 add_to_breakpoint_chain (b);
6638 return b;
6639 }
6640
6641 /* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
6642 resolutions should be made as the user specified the location explicitly
6643 enough. */
6644
6645 static void
6646 set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
6647 {
6648 gdb_assert (loc->owner != NULL);
6649
6650 if (loc->owner->type == bp_breakpoint
6651 || loc->owner->type == bp_hardware_breakpoint
6652 || is_tracepoint (loc->owner))
6653 {
6654 int is_gnu_ifunc;
6655 const char *function_name;
6656 CORE_ADDR func_addr;
6657
6658 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
6659 &func_addr, NULL, &is_gnu_ifunc);
6660
6661 if (is_gnu_ifunc && !explicit_loc)
6662 {
6663 struct breakpoint *b = loc->owner;
6664
6665 gdb_assert (loc->pspace == current_program_space);
6666 if (gnu_ifunc_resolve_name (function_name,
6667 &loc->requested_address))
6668 {
6669 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
6670 loc->address = adjust_breakpoint_address (loc->gdbarch,
6671 loc->requested_address,
6672 b->type);
6673 }
6674 else if (b->type == bp_breakpoint && b->loc == loc
6675 && loc->next == NULL && b->related_breakpoint == b)
6676 {
6677 /* Create only the whole new breakpoint of this type but do not
6678 mess more complicated breakpoints with multiple locations. */
6679 b->type = bp_gnu_ifunc_resolver;
6680 /* Remember the resolver's address for use by the return
6681 breakpoint. */
6682 loc->related_address = func_addr;
6683 }
6684 }
6685
6686 if (function_name)
6687 loc->function_name = xstrdup (function_name);
6688 }
6689 }
6690
6691 /* Attempt to determine architecture of location identified by SAL. */
6692 struct gdbarch *
6693 get_sal_arch (struct symtab_and_line sal)
6694 {
6695 if (sal.section)
6696 return get_objfile_arch (sal.section->objfile);
6697 if (sal.symtab)
6698 return get_objfile_arch (sal.symtab->objfile);
6699
6700 return NULL;
6701 }
6702
6703 /* Low level routine for partially initializing a breakpoint of type
6704 BPTYPE. The newly created breakpoint's address, section, source
6705 file name, and line number are provided by SAL.
6706
6707 It is expected that the caller will complete the initialization of
6708 the newly created breakpoint struct as well as output any status
6709 information regarding the creation of a new breakpoint. */
6710
6711 static void
6712 init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
6713 struct symtab_and_line sal, enum bptype bptype,
6714 const struct breakpoint_ops *ops)
6715 {
6716 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
6717
6718 add_location_to_breakpoint (b, &sal);
6719
6720 if (bptype != bp_catchpoint)
6721 gdb_assert (sal.pspace != NULL);
6722
6723 /* Store the program space that was used to set the breakpoint,
6724 except for ordinary breakpoints, which are independent of the
6725 program space. */
6726 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
6727 b->pspace = sal.pspace;
6728
6729 breakpoints_changed ();
6730 }
6731
6732 /* set_raw_breakpoint is a low level routine for allocating and
6733 partially initializing a breakpoint of type BPTYPE. The newly
6734 created breakpoint's address, section, source file name, and line
6735 number are provided by SAL. The newly created and partially
6736 initialized breakpoint is added to the breakpoint chain and
6737 is also returned as the value of this function.
6738
6739 It is expected that the caller will complete the initialization of
6740 the newly created breakpoint struct as well as output any status
6741 information regarding the creation of a new breakpoint. In
6742 particular, set_raw_breakpoint does NOT set the breakpoint
6743 number! Care should be taken to not allow an error to occur
6744 prior to completing the initialization of the breakpoint. If this
6745 should happen, a bogus breakpoint will be left on the chain. */
6746
6747 struct breakpoint *
6748 set_raw_breakpoint (struct gdbarch *gdbarch,
6749 struct symtab_and_line sal, enum bptype bptype,
6750 const struct breakpoint_ops *ops)
6751 {
6752 struct breakpoint *b = XNEW (struct breakpoint);
6753
6754 init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
6755 add_to_breakpoint_chain (b);
6756 return b;
6757 }
6758
6759
6760 /* Note that the breakpoint object B describes a permanent breakpoint
6761 instruction, hard-wired into the inferior's code. */
6762 void
6763 make_breakpoint_permanent (struct breakpoint *b)
6764 {
6765 struct bp_location *bl;
6766
6767 b->enable_state = bp_permanent;
6768
6769 /* By definition, permanent breakpoints are already present in the
6770 code. Mark all locations as inserted. For now,
6771 make_breakpoint_permanent is called in just one place, so it's
6772 hard to say if it's reasonable to have permanent breakpoint with
6773 multiple locations or not, but it's easy to implement. */
6774 for (bl = b->loc; bl; bl = bl->next)
6775 bl->inserted = 1;
6776 }
6777
6778 /* Call this routine when stepping and nexting to enable a breakpoint
6779 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
6780 initiated the operation. */
6781
6782 void
6783 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
6784 {
6785 struct breakpoint *b, *b_tmp;
6786 int thread = tp->num;
6787
6788 /* To avoid having to rescan all objfile symbols at every step,
6789 we maintain a list of continually-inserted but always disabled
6790 longjmp "master" breakpoints. Here, we simply create momentary
6791 clones of those and enable them for the requested thread. */
6792 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6793 if (b->pspace == current_program_space
6794 && (b->type == bp_longjmp_master
6795 || b->type == bp_exception_master))
6796 {
6797 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
6798 struct breakpoint *clone;
6799
6800 clone = momentary_breakpoint_from_master (b, type,
6801 &momentary_breakpoint_ops);
6802 clone->thread = thread;
6803 }
6804
6805 tp->initiating_frame = frame;
6806 }
6807
6808 /* Delete all longjmp breakpoints from THREAD. */
6809 void
6810 delete_longjmp_breakpoint (int thread)
6811 {
6812 struct breakpoint *b, *b_tmp;
6813
6814 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6815 if (b->type == bp_longjmp || b->type == bp_exception)
6816 {
6817 if (b->thread == thread)
6818 delete_breakpoint (b);
6819 }
6820 }
6821
6822 void
6823 delete_longjmp_breakpoint_at_next_stop (int thread)
6824 {
6825 struct breakpoint *b, *b_tmp;
6826
6827 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6828 if (b->type == bp_longjmp || b->type == bp_exception)
6829 {
6830 if (b->thread == thread)
6831 b->disposition = disp_del_at_next_stop;
6832 }
6833 }
6834
6835 void
6836 enable_overlay_breakpoints (void)
6837 {
6838 struct breakpoint *b;
6839
6840 ALL_BREAKPOINTS (b)
6841 if (b->type == bp_overlay_event)
6842 {
6843 b->enable_state = bp_enabled;
6844 update_global_location_list (1);
6845 overlay_events_enabled = 1;
6846 }
6847 }
6848
6849 void
6850 disable_overlay_breakpoints (void)
6851 {
6852 struct breakpoint *b;
6853
6854 ALL_BREAKPOINTS (b)
6855 if (b->type == bp_overlay_event)
6856 {
6857 b->enable_state = bp_disabled;
6858 update_global_location_list (0);
6859 overlay_events_enabled = 0;
6860 }
6861 }
6862
6863 /* Set an active std::terminate breakpoint for each std::terminate
6864 master breakpoint. */
6865 void
6866 set_std_terminate_breakpoint (void)
6867 {
6868 struct breakpoint *b, *b_tmp;
6869
6870 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6871 if (b->pspace == current_program_space
6872 && b->type == bp_std_terminate_master)
6873 {
6874 momentary_breakpoint_from_master (b, bp_std_terminate,
6875 &momentary_breakpoint_ops);
6876 }
6877 }
6878
6879 /* Delete all the std::terminate breakpoints. */
6880 void
6881 delete_std_terminate_breakpoint (void)
6882 {
6883 struct breakpoint *b, *b_tmp;
6884
6885 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6886 if (b->type == bp_std_terminate)
6887 delete_breakpoint (b);
6888 }
6889
6890 struct breakpoint *
6891 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
6892 {
6893 struct breakpoint *b;
6894
6895 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
6896 &internal_breakpoint_ops);
6897
6898 b->enable_state = bp_enabled;
6899 /* addr_string has to be used or breakpoint_re_set will delete me. */
6900 b->addr_string
6901 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
6902
6903 update_global_location_list_nothrow (1);
6904
6905 return b;
6906 }
6907
6908 void
6909 remove_thread_event_breakpoints (void)
6910 {
6911 struct breakpoint *b, *b_tmp;
6912
6913 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6914 if (b->type == bp_thread_event
6915 && b->loc->pspace == current_program_space)
6916 delete_breakpoint (b);
6917 }
6918
6919 struct lang_and_radix
6920 {
6921 enum language lang;
6922 int radix;
6923 };
6924
6925 /* Create a breakpoint for JIT code registration and unregistration. */
6926
6927 struct breakpoint *
6928 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
6929 {
6930 struct breakpoint *b;
6931
6932 b = create_internal_breakpoint (gdbarch, address, bp_jit_event,
6933 &internal_breakpoint_ops);
6934 update_global_location_list_nothrow (1);
6935 return b;
6936 }
6937
6938 /* Remove JIT code registration and unregistration breakpoint(s). */
6939
6940 void
6941 remove_jit_event_breakpoints (void)
6942 {
6943 struct breakpoint *b, *b_tmp;
6944
6945 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6946 if (b->type == bp_jit_event
6947 && b->loc->pspace == current_program_space)
6948 delete_breakpoint (b);
6949 }
6950
6951 void
6952 remove_solib_event_breakpoints (void)
6953 {
6954 struct breakpoint *b, *b_tmp;
6955
6956 ALL_BREAKPOINTS_SAFE (b, b_tmp)
6957 if (b->type == bp_shlib_event
6958 && b->loc->pspace == current_program_space)
6959 delete_breakpoint (b);
6960 }
6961
6962 struct breakpoint *
6963 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
6964 {
6965 struct breakpoint *b;
6966
6967 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
6968 &internal_breakpoint_ops);
6969 update_global_location_list_nothrow (1);
6970 return b;
6971 }
6972
6973 /* Disable any breakpoints that are on code in shared libraries. Only
6974 apply to enabled breakpoints, disabled ones can just stay disabled. */
6975
6976 void
6977 disable_breakpoints_in_shlibs (void)
6978 {
6979 struct bp_location *loc, **locp_tmp;
6980
6981 ALL_BP_LOCATIONS (loc, locp_tmp)
6982 {
6983 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
6984 struct breakpoint *b = loc->owner;
6985
6986 /* We apply the check to all breakpoints, including disabled for
6987 those with loc->duplicate set. This is so that when breakpoint
6988 becomes enabled, or the duplicate is removed, gdb will try to
6989 insert all breakpoints. If we don't set shlib_disabled here,
6990 we'll try to insert those breakpoints and fail. */
6991 if (((b->type == bp_breakpoint)
6992 || (b->type == bp_jit_event)
6993 || (b->type == bp_hardware_breakpoint)
6994 || (is_tracepoint (b)))
6995 && loc->pspace == current_program_space
6996 && !loc->shlib_disabled
6997 #ifdef PC_SOLIB
6998 && PC_SOLIB (loc->address)
6999 #else
7000 && solib_name_from_address (loc->pspace, loc->address)
7001 #endif
7002 )
7003 {
7004 loc->shlib_disabled = 1;
7005 }
7006 }
7007 }
7008
7009 /* Disable any breakpoints and tracepoints that are in an unloaded shared
7010 library. Only apply to enabled breakpoints, disabled ones can just stay
7011 disabled. */
7012
7013 static void
7014 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7015 {
7016 struct bp_location *loc, **locp_tmp;
7017 int disabled_shlib_breaks = 0;
7018
7019 /* SunOS a.out shared libraries are always mapped, so do not
7020 disable breakpoints; they will only be reported as unloaded
7021 through clear_solib when GDB discards its shared library
7022 list. See clear_solib for more information. */
7023 if (exec_bfd != NULL
7024 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
7025 return;
7026
7027 ALL_BP_LOCATIONS (loc, locp_tmp)
7028 {
7029 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7030 struct breakpoint *b = loc->owner;
7031
7032 if (solib->pspace == loc->pspace
7033 && !loc->shlib_disabled
7034 && (((b->type == bp_breakpoint
7035 || b->type == bp_jit_event
7036 || b->type == bp_hardware_breakpoint)
7037 && (loc->loc_type == bp_loc_hardware_breakpoint
7038 || loc->loc_type == bp_loc_software_breakpoint))
7039 || is_tracepoint (b))
7040 && solib_contains_address_p (solib, loc->address))
7041 {
7042 loc->shlib_disabled = 1;
7043 /* At this point, we cannot rely on remove_breakpoint
7044 succeeding so we must mark the breakpoint as not inserted
7045 to prevent future errors occurring in remove_breakpoints. */
7046 loc->inserted = 0;
7047
7048 /* This may cause duplicate notifications for the same breakpoint. */
7049 observer_notify_breakpoint_modified (b);
7050
7051 if (!disabled_shlib_breaks)
7052 {
7053 target_terminal_ours_for_output ();
7054 warning (_("Temporarily disabling breakpoints "
7055 "for unloaded shared library \"%s\""),
7056 solib->so_name);
7057 }
7058 disabled_shlib_breaks = 1;
7059 }
7060 }
7061 }
7062
7063 /* FORK & VFORK catchpoints. */
7064
7065 /* An instance of this type is used to represent a fork or vfork
7066 catchpoint. It includes a "struct breakpoint" as a kind of base
7067 class; users downcast to "struct breakpoint *" when needed. A
7068 breakpoint is really of this type iff its ops pointer points to
7069 CATCH_FORK_BREAKPOINT_OPS. */
7070
7071 struct fork_catchpoint
7072 {
7073 /* The base class. */
7074 struct breakpoint base;
7075
7076 /* Process id of a child process whose forking triggered this
7077 catchpoint. This field is only valid immediately after this
7078 catchpoint has triggered. */
7079 ptid_t forked_inferior_pid;
7080 };
7081
7082 /* Implement the "insert" breakpoint_ops method for fork
7083 catchpoints. */
7084
7085 static int
7086 insert_catch_fork (struct bp_location *bl)
7087 {
7088 return target_insert_fork_catchpoint (PIDGET (inferior_ptid));
7089 }
7090
7091 /* Implement the "remove" breakpoint_ops method for fork
7092 catchpoints. */
7093
7094 static int
7095 remove_catch_fork (struct bp_location *bl)
7096 {
7097 return target_remove_fork_catchpoint (PIDGET (inferior_ptid));
7098 }
7099
7100 /* Implement the "breakpoint_hit" breakpoint_ops method for fork
7101 catchpoints. */
7102
7103 static int
7104 breakpoint_hit_catch_fork (const struct bp_location *bl,
7105 struct address_space *aspace, CORE_ADDR bp_addr,
7106 const struct target_waitstatus *ws)
7107 {
7108 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7109
7110 if (ws->kind != TARGET_WAITKIND_FORKED)
7111 return 0;
7112
7113 c->forked_inferior_pid = ws->value.related_pid;
7114 return 1;
7115 }
7116
7117 /* Implement the "print_it" breakpoint_ops method for fork
7118 catchpoints. */
7119
7120 static enum print_stop_action
7121 print_it_catch_fork (bpstat bs)
7122 {
7123 struct ui_out *uiout = current_uiout;
7124 struct breakpoint *b = bs->breakpoint_at;
7125 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
7126
7127 annotate_catchpoint (b->number);
7128 if (b->disposition == disp_del)
7129 ui_out_text (uiout, "\nTemporary catchpoint ");
7130 else
7131 ui_out_text (uiout, "\nCatchpoint ");
7132 if (ui_out_is_mi_like_p (uiout))
7133 {
7134 ui_out_field_string (uiout, "reason",
7135 async_reason_lookup (EXEC_ASYNC_FORK));
7136 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7137 }
7138 ui_out_field_int (uiout, "bkptno", b->number);
7139 ui_out_text (uiout, " (forked process ");
7140 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
7141 ui_out_text (uiout, "), ");
7142 return PRINT_SRC_AND_LOC;
7143 }
7144
7145 /* Implement the "print_one" breakpoint_ops method for fork
7146 catchpoints. */
7147
7148 static void
7149 print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
7150 {
7151 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7152 struct value_print_options opts;
7153 struct ui_out *uiout = current_uiout;
7154
7155 get_user_print_options (&opts);
7156
7157 /* Field 4, the address, is omitted (which makes the columns not
7158 line up too nicely with the headers, but the effect is relatively
7159 readable). */
7160 if (opts.addressprint)
7161 ui_out_field_skip (uiout, "addr");
7162 annotate_field (5);
7163 ui_out_text (uiout, "fork");
7164 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
7165 {
7166 ui_out_text (uiout, ", process ");
7167 ui_out_field_int (uiout, "what",
7168 ptid_get_pid (c->forked_inferior_pid));
7169 ui_out_spaces (uiout, 1);
7170 }
7171 }
7172
7173 /* Implement the "print_mention" breakpoint_ops method for fork
7174 catchpoints. */
7175
7176 static void
7177 print_mention_catch_fork (struct breakpoint *b)
7178 {
7179 printf_filtered (_("Catchpoint %d (fork)"), b->number);
7180 }
7181
7182 /* Implement the "print_recreate" breakpoint_ops method for fork
7183 catchpoints. */
7184
7185 static void
7186 print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
7187 {
7188 fprintf_unfiltered (fp, "catch fork");
7189 print_recreate_thread (b, fp);
7190 }
7191
7192 /* The breakpoint_ops structure to be used in fork catchpoints. */
7193
7194 static struct breakpoint_ops catch_fork_breakpoint_ops;
7195
7196 /* Implement the "insert" breakpoint_ops method for vfork
7197 catchpoints. */
7198
7199 static int
7200 insert_catch_vfork (struct bp_location *bl)
7201 {
7202 return target_insert_vfork_catchpoint (PIDGET (inferior_ptid));
7203 }
7204
7205 /* Implement the "remove" breakpoint_ops method for vfork
7206 catchpoints. */
7207
7208 static int
7209 remove_catch_vfork (struct bp_location *bl)
7210 {
7211 return target_remove_vfork_catchpoint (PIDGET (inferior_ptid));
7212 }
7213
7214 /* Implement the "breakpoint_hit" breakpoint_ops method for vfork
7215 catchpoints. */
7216
7217 static int
7218 breakpoint_hit_catch_vfork (const struct bp_location *bl,
7219 struct address_space *aspace, CORE_ADDR bp_addr,
7220 const struct target_waitstatus *ws)
7221 {
7222 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7223
7224 if (ws->kind != TARGET_WAITKIND_VFORKED)
7225 return 0;
7226
7227 c->forked_inferior_pid = ws->value.related_pid;
7228 return 1;
7229 }
7230
7231 /* Implement the "print_it" breakpoint_ops method for vfork
7232 catchpoints. */
7233
7234 static enum print_stop_action
7235 print_it_catch_vfork (bpstat bs)
7236 {
7237 struct ui_out *uiout = current_uiout;
7238 struct breakpoint *b = bs->breakpoint_at;
7239 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7240
7241 annotate_catchpoint (b->number);
7242 if (b->disposition == disp_del)
7243 ui_out_text (uiout, "\nTemporary catchpoint ");
7244 else
7245 ui_out_text (uiout, "\nCatchpoint ");
7246 if (ui_out_is_mi_like_p (uiout))
7247 {
7248 ui_out_field_string (uiout, "reason",
7249 async_reason_lookup (EXEC_ASYNC_VFORK));
7250 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7251 }
7252 ui_out_field_int (uiout, "bkptno", b->number);
7253 ui_out_text (uiout, " (vforked process ");
7254 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
7255 ui_out_text (uiout, "), ");
7256 return PRINT_SRC_AND_LOC;
7257 }
7258
7259 /* Implement the "print_one" breakpoint_ops method for vfork
7260 catchpoints. */
7261
7262 static void
7263 print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
7264 {
7265 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7266 struct value_print_options opts;
7267 struct ui_out *uiout = current_uiout;
7268
7269 get_user_print_options (&opts);
7270 /* Field 4, the address, is omitted (which makes the columns not
7271 line up too nicely with the headers, but the effect is relatively
7272 readable). */
7273 if (opts.addressprint)
7274 ui_out_field_skip (uiout, "addr");
7275 annotate_field (5);
7276 ui_out_text (uiout, "vfork");
7277 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
7278 {
7279 ui_out_text (uiout, ", process ");
7280 ui_out_field_int (uiout, "what",
7281 ptid_get_pid (c->forked_inferior_pid));
7282 ui_out_spaces (uiout, 1);
7283 }
7284 }
7285
7286 /* Implement the "print_mention" breakpoint_ops method for vfork
7287 catchpoints. */
7288
7289 static void
7290 print_mention_catch_vfork (struct breakpoint *b)
7291 {
7292 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
7293 }
7294
7295 /* Implement the "print_recreate" breakpoint_ops method for vfork
7296 catchpoints. */
7297
7298 static void
7299 print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
7300 {
7301 fprintf_unfiltered (fp, "catch vfork");
7302 print_recreate_thread (b, fp);
7303 }
7304
7305 /* The breakpoint_ops structure to be used in vfork catchpoints. */
7306
7307 static struct breakpoint_ops catch_vfork_breakpoint_ops;
7308
7309 /* An instance of this type is used to represent an solib catchpoint.
7310 It includes a "struct breakpoint" as a kind of base class; users
7311 downcast to "struct breakpoint *" when needed. A breakpoint is
7312 really of this type iff its ops pointer points to
7313 CATCH_SOLIB_BREAKPOINT_OPS. */
7314
7315 struct solib_catchpoint
7316 {
7317 /* The base class. */
7318 struct breakpoint base;
7319
7320 /* True for "catch load", false for "catch unload". */
7321 unsigned char is_load;
7322
7323 /* Regular expression to match, if any. COMPILED is only valid when
7324 REGEX is non-NULL. */
7325 char *regex;
7326 regex_t compiled;
7327 };
7328
7329 static void
7330 dtor_catch_solib (struct breakpoint *b)
7331 {
7332 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
7333
7334 if (self->regex)
7335 regfree (&self->compiled);
7336 xfree (self->regex);
7337
7338 base_breakpoint_ops.dtor (b);
7339 }
7340
7341 static int
7342 insert_catch_solib (struct bp_location *ignore)
7343 {
7344 return 0;
7345 }
7346
7347 static int
7348 remove_catch_solib (struct bp_location *ignore)
7349 {
7350 return 0;
7351 }
7352
7353 static int
7354 breakpoint_hit_catch_solib (const struct bp_location *bl,
7355 struct address_space *aspace,
7356 CORE_ADDR bp_addr,
7357 const struct target_waitstatus *ws)
7358 {
7359 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
7360 struct breakpoint *other;
7361
7362 if (ws->kind == TARGET_WAITKIND_LOADED)
7363 return 1;
7364
7365 ALL_BREAKPOINTS (other)
7366 {
7367 struct bp_location *other_bl;
7368
7369 if (other == bl->owner)
7370 continue;
7371
7372 if (other->type != bp_shlib_event)
7373 continue;
7374
7375 if (self->base.pspace != NULL && other->pspace != self->base.pspace)
7376 continue;
7377
7378 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
7379 {
7380 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
7381 return 1;
7382 }
7383 }
7384
7385 return 0;
7386 }
7387
7388 static void
7389 check_status_catch_solib (struct bpstats *bs)
7390 {
7391 struct solib_catchpoint *self
7392 = (struct solib_catchpoint *) bs->breakpoint_at;
7393 int ix;
7394
7395 if (self->is_load)
7396 {
7397 struct so_list *iter;
7398
7399 for (ix = 0;
7400 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
7401 ix, iter);
7402 ++ix)
7403 {
7404 if (!self->regex
7405 || regexec (&self->compiled, iter->so_name, 0, NULL, 0) == 0)
7406 return;
7407 }
7408 }
7409 else
7410 {
7411 char *iter;
7412
7413 for (ix = 0;
7414 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
7415 ix, iter);
7416 ++ix)
7417 {
7418 if (!self->regex
7419 || regexec (&self->compiled, iter, 0, NULL, 0) == 0)
7420 return;
7421 }
7422 }
7423
7424 bs->stop = 0;
7425 bs->print_it = print_it_noop;
7426 }
7427
7428 static enum print_stop_action
7429 print_it_catch_solib (bpstat bs)
7430 {
7431 struct breakpoint *b = bs->breakpoint_at;
7432 struct ui_out *uiout = current_uiout;
7433
7434 annotate_catchpoint (b->number);
7435 if (b->disposition == disp_del)
7436 ui_out_text (uiout, "\nTemporary catchpoint ");
7437 else
7438 ui_out_text (uiout, "\nCatchpoint ");
7439 ui_out_field_int (uiout, "bkptno", b->number);
7440 ui_out_text (uiout, "\n");
7441 if (ui_out_is_mi_like_p (uiout))
7442 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7443 print_solib_event (1);
7444 return PRINT_SRC_AND_LOC;
7445 }
7446
7447 static void
7448 print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
7449 {
7450 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
7451 struct value_print_options opts;
7452 struct ui_out *uiout = current_uiout;
7453 char *msg;
7454
7455 get_user_print_options (&opts);
7456 /* Field 4, the address, is omitted (which makes the columns not
7457 line up too nicely with the headers, but the effect is relatively
7458 readable). */
7459 if (opts.addressprint)
7460 {
7461 annotate_field (4);
7462 ui_out_field_skip (uiout, "addr");
7463 }
7464
7465 annotate_field (5);
7466 if (self->is_load)
7467 {
7468 if (self->regex)
7469 msg = xstrprintf (_("load of library matching %s"), self->regex);
7470 else
7471 msg = xstrdup (_("load of library"));
7472 }
7473 else
7474 {
7475 if (self->regex)
7476 msg = xstrprintf (_("unload of library matching %s"), self->regex);
7477 else
7478 msg = xstrdup (_("unload of library"));
7479 }
7480 ui_out_field_string (uiout, "what", msg);
7481 xfree (msg);
7482 }
7483
7484 static void
7485 print_mention_catch_solib (struct breakpoint *b)
7486 {
7487 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
7488
7489 printf_filtered (_("Catchpoint %d (%s)"), b->number,
7490 self->is_load ? "load" : "unload");
7491 }
7492
7493 static void
7494 print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
7495 {
7496 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
7497
7498 fprintf_unfiltered (fp, "%s %s",
7499 b->disposition == disp_del ? "tcatch" : "catch",
7500 self->is_load ? "load" : "unload");
7501 if (self->regex)
7502 fprintf_unfiltered (fp, " %s", self->regex);
7503 fprintf_unfiltered (fp, "\n");
7504 }
7505
7506 static struct breakpoint_ops catch_solib_breakpoint_ops;
7507
7508 /* A helper function that does all the work for "catch load" and
7509 "catch unload". */
7510
7511 static void
7512 catch_load_or_unload (char *arg, int from_tty, int is_load,
7513 struct cmd_list_element *command)
7514 {
7515 struct solib_catchpoint *c;
7516 struct gdbarch *gdbarch = get_current_arch ();
7517 int tempflag;
7518 struct cleanup *cleanup;
7519
7520 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
7521
7522 if (!arg)
7523 arg = "";
7524 arg = skip_spaces (arg);
7525
7526 c = XCNEW (struct solib_catchpoint);
7527 cleanup = make_cleanup (xfree, c);
7528
7529 if (*arg != '\0')
7530 {
7531 int errcode;
7532
7533 errcode = regcomp (&c->compiled, arg, REG_NOSUB);
7534 if (errcode != 0)
7535 {
7536 char *err = get_regcomp_error (errcode, &c->compiled);
7537
7538 make_cleanup (xfree, err);
7539 error (_("Invalid regexp (%s): %s"), err, arg);
7540 }
7541 c->regex = xstrdup (arg);
7542 }
7543
7544 c->is_load = is_load;
7545 init_catchpoint (&c->base, gdbarch, tempflag, NULL,
7546 &catch_solib_breakpoint_ops);
7547
7548 discard_cleanups (cleanup);
7549 install_breakpoint (0, &c->base, 1);
7550 }
7551
7552 static void
7553 catch_load_command_1 (char *arg, int from_tty,
7554 struct cmd_list_element *command)
7555 {
7556 catch_load_or_unload (arg, from_tty, 1, command);
7557 }
7558
7559 static void
7560 catch_unload_command_1 (char *arg, int from_tty,
7561 struct cmd_list_element *command)
7562 {
7563 catch_load_or_unload (arg, from_tty, 0, command);
7564 }
7565
7566 DEF_VEC_I(int);
7567
7568 /* An instance of this type is used to represent a syscall catchpoint.
7569 It includes a "struct breakpoint" as a kind of base class; users
7570 downcast to "struct breakpoint *" when needed. A breakpoint is
7571 really of this type iff its ops pointer points to
7572 CATCH_SYSCALL_BREAKPOINT_OPS. */
7573
7574 struct syscall_catchpoint
7575 {
7576 /* The base class. */
7577 struct breakpoint base;
7578
7579 /* Syscall numbers used for the 'catch syscall' feature. If no
7580 syscall has been specified for filtering, its value is NULL.
7581 Otherwise, it holds a list of all syscalls to be caught. The
7582 list elements are allocated with xmalloc. */
7583 VEC(int) *syscalls_to_be_caught;
7584 };
7585
7586 /* Implement the "dtor" breakpoint_ops method for syscall
7587 catchpoints. */
7588
7589 static void
7590 dtor_catch_syscall (struct breakpoint *b)
7591 {
7592 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
7593
7594 VEC_free (int, c->syscalls_to_be_caught);
7595
7596 base_breakpoint_ops.dtor (b);
7597 }
7598
7599 static const struct inferior_data *catch_syscall_inferior_data = NULL;
7600
7601 struct catch_syscall_inferior_data
7602 {
7603 /* We keep a count of the number of times the user has requested a
7604 particular syscall to be tracked, and pass this information to the
7605 target. This lets capable targets implement filtering directly. */
7606
7607 /* Number of times that "any" syscall is requested. */
7608 int any_syscall_count;
7609
7610 /* Count of each system call. */
7611 VEC(int) *syscalls_counts;
7612
7613 /* This counts all syscall catch requests, so we can readily determine
7614 if any catching is necessary. */
7615 int total_syscalls_count;
7616 };
7617
7618 static struct catch_syscall_inferior_data*
7619 get_catch_syscall_inferior_data (struct inferior *inf)
7620 {
7621 struct catch_syscall_inferior_data *inf_data;
7622
7623 inf_data = inferior_data (inf, catch_syscall_inferior_data);
7624 if (inf_data == NULL)
7625 {
7626 inf_data = XZALLOC (struct catch_syscall_inferior_data);
7627 set_inferior_data (inf, catch_syscall_inferior_data, inf_data);
7628 }
7629
7630 return inf_data;
7631 }
7632
7633 static void
7634 catch_syscall_inferior_data_cleanup (struct inferior *inf, void *arg)
7635 {
7636 xfree (arg);
7637 }
7638
7639
7640 /* Implement the "insert" breakpoint_ops method for syscall
7641 catchpoints. */
7642
7643 static int
7644 insert_catch_syscall (struct bp_location *bl)
7645 {
7646 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
7647 struct inferior *inf = current_inferior ();
7648 struct catch_syscall_inferior_data *inf_data
7649 = get_catch_syscall_inferior_data (inf);
7650
7651 ++inf_data->total_syscalls_count;
7652 if (!c->syscalls_to_be_caught)
7653 ++inf_data->any_syscall_count;
7654 else
7655 {
7656 int i, iter;
7657
7658 for (i = 0;
7659 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
7660 i++)
7661 {
7662 int elem;
7663
7664 if (iter >= VEC_length (int, inf_data->syscalls_counts))
7665 {
7666 int old_size = VEC_length (int, inf_data->syscalls_counts);
7667 uintptr_t vec_addr_offset
7668 = old_size * ((uintptr_t) sizeof (int));
7669 uintptr_t vec_addr;
7670 VEC_safe_grow (int, inf_data->syscalls_counts, iter + 1);
7671 vec_addr = ((uintptr_t) VEC_address (int,
7672 inf_data->syscalls_counts)
7673 + vec_addr_offset);
7674 memset ((void *) vec_addr, 0,
7675 (iter + 1 - old_size) * sizeof (int));
7676 }
7677 elem = VEC_index (int, inf_data->syscalls_counts, iter);
7678 VEC_replace (int, inf_data->syscalls_counts, iter, ++elem);
7679 }
7680 }
7681
7682 return target_set_syscall_catchpoint (PIDGET (inferior_ptid),
7683 inf_data->total_syscalls_count != 0,
7684 inf_data->any_syscall_count,
7685 VEC_length (int,
7686 inf_data->syscalls_counts),
7687 VEC_address (int,
7688 inf_data->syscalls_counts));
7689 }
7690
7691 /* Implement the "remove" breakpoint_ops method for syscall
7692 catchpoints. */
7693
7694 static int
7695 remove_catch_syscall (struct bp_location *bl)
7696 {
7697 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
7698 struct inferior *inf = current_inferior ();
7699 struct catch_syscall_inferior_data *inf_data
7700 = get_catch_syscall_inferior_data (inf);
7701
7702 --inf_data->total_syscalls_count;
7703 if (!c->syscalls_to_be_caught)
7704 --inf_data->any_syscall_count;
7705 else
7706 {
7707 int i, iter;
7708
7709 for (i = 0;
7710 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
7711 i++)
7712 {
7713 int elem;
7714 if (iter >= VEC_length (int, inf_data->syscalls_counts))
7715 /* Shouldn't happen. */
7716 continue;
7717 elem = VEC_index (int, inf_data->syscalls_counts, iter);
7718 VEC_replace (int, inf_data->syscalls_counts, iter, --elem);
7719 }
7720 }
7721
7722 return target_set_syscall_catchpoint (PIDGET (inferior_ptid),
7723 inf_data->total_syscalls_count != 0,
7724 inf_data->any_syscall_count,
7725 VEC_length (int,
7726 inf_data->syscalls_counts),
7727 VEC_address (int,
7728 inf_data->syscalls_counts));
7729 }
7730
7731 /* Implement the "breakpoint_hit" breakpoint_ops method for syscall
7732 catchpoints. */
7733
7734 static int
7735 breakpoint_hit_catch_syscall (const struct bp_location *bl,
7736 struct address_space *aspace, CORE_ADDR bp_addr,
7737 const struct target_waitstatus *ws)
7738 {
7739 /* We must check if we are catching specific syscalls in this
7740 breakpoint. If we are, then we must guarantee that the called
7741 syscall is the same syscall we are catching. */
7742 int syscall_number = 0;
7743 const struct syscall_catchpoint *c
7744 = (const struct syscall_catchpoint *) bl->owner;
7745
7746 if (ws->kind != TARGET_WAITKIND_SYSCALL_ENTRY
7747 && ws->kind != TARGET_WAITKIND_SYSCALL_RETURN)
7748 return 0;
7749
7750 syscall_number = ws->value.syscall_number;
7751
7752 /* Now, checking if the syscall is the same. */
7753 if (c->syscalls_to_be_caught)
7754 {
7755 int i, iter;
7756
7757 for (i = 0;
7758 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
7759 i++)
7760 if (syscall_number == iter)
7761 break;
7762 /* Not the same. */
7763 if (!iter)
7764 return 0;
7765 }
7766
7767 return 1;
7768 }
7769
7770 /* Implement the "print_it" breakpoint_ops method for syscall
7771 catchpoints. */
7772
7773 static enum print_stop_action
7774 print_it_catch_syscall (bpstat bs)
7775 {
7776 struct ui_out *uiout = current_uiout;
7777 struct breakpoint *b = bs->breakpoint_at;
7778 /* These are needed because we want to know in which state a
7779 syscall is. It can be in the TARGET_WAITKIND_SYSCALL_ENTRY
7780 or TARGET_WAITKIND_SYSCALL_RETURN, and depending on it we
7781 must print "called syscall" or "returned from syscall". */
7782 ptid_t ptid;
7783 struct target_waitstatus last;
7784 struct syscall s;
7785
7786 get_last_target_status (&ptid, &last);
7787
7788 get_syscall_by_number (last.value.syscall_number, &s);
7789
7790 annotate_catchpoint (b->number);
7791
7792 if (b->disposition == disp_del)
7793 ui_out_text (uiout, "\nTemporary catchpoint ");
7794 else
7795 ui_out_text (uiout, "\nCatchpoint ");
7796 if (ui_out_is_mi_like_p (uiout))
7797 {
7798 ui_out_field_string (uiout, "reason",
7799 async_reason_lookup (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY
7800 ? EXEC_ASYNC_SYSCALL_ENTRY
7801 : EXEC_ASYNC_SYSCALL_RETURN));
7802 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7803 }
7804 ui_out_field_int (uiout, "bkptno", b->number);
7805
7806 if (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY)
7807 ui_out_text (uiout, " (call to syscall ");
7808 else
7809 ui_out_text (uiout, " (returned from syscall ");
7810
7811 if (s.name == NULL || ui_out_is_mi_like_p (uiout))
7812 ui_out_field_int (uiout, "syscall-number", last.value.syscall_number);
7813 if (s.name != NULL)
7814 ui_out_field_string (uiout, "syscall-name", s.name);
7815
7816 ui_out_text (uiout, "), ");
7817
7818 return PRINT_SRC_AND_LOC;
7819 }
7820
7821 /* Implement the "print_one" breakpoint_ops method for syscall
7822 catchpoints. */
7823
7824 static void
7825 print_one_catch_syscall (struct breakpoint *b,
7826 struct bp_location **last_loc)
7827 {
7828 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
7829 struct value_print_options opts;
7830 struct ui_out *uiout = current_uiout;
7831
7832 get_user_print_options (&opts);
7833 /* Field 4, the address, is omitted (which makes the columns not
7834 line up too nicely with the headers, but the effect is relatively
7835 readable). */
7836 if (opts.addressprint)
7837 ui_out_field_skip (uiout, "addr");
7838 annotate_field (5);
7839
7840 if (c->syscalls_to_be_caught
7841 && VEC_length (int, c->syscalls_to_be_caught) > 1)
7842 ui_out_text (uiout, "syscalls \"");
7843 else
7844 ui_out_text (uiout, "syscall \"");
7845
7846 if (c->syscalls_to_be_caught)
7847 {
7848 int i, iter;
7849 char *text = xstrprintf ("%s", "");
7850
7851 for (i = 0;
7852 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
7853 i++)
7854 {
7855 char *x = text;
7856 struct syscall s;
7857 get_syscall_by_number (iter, &s);
7858
7859 if (s.name != NULL)
7860 text = xstrprintf ("%s%s, ", text, s.name);
7861 else
7862 text = xstrprintf ("%s%d, ", text, iter);
7863
7864 /* We have to xfree the last 'text' (now stored at 'x')
7865 because xstrprintf dynamically allocates new space for it
7866 on every call. */
7867 xfree (x);
7868 }
7869 /* Remove the last comma. */
7870 text[strlen (text) - 2] = '\0';
7871 ui_out_field_string (uiout, "what", text);
7872 }
7873 else
7874 ui_out_field_string (uiout, "what", "<any syscall>");
7875 ui_out_text (uiout, "\" ");
7876 }
7877
7878 /* Implement the "print_mention" breakpoint_ops method for syscall
7879 catchpoints. */
7880
7881 static void
7882 print_mention_catch_syscall (struct breakpoint *b)
7883 {
7884 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
7885
7886 if (c->syscalls_to_be_caught)
7887 {
7888 int i, iter;
7889
7890 if (VEC_length (int, c->syscalls_to_be_caught) > 1)
7891 printf_filtered (_("Catchpoint %d (syscalls"), b->number);
7892 else
7893 printf_filtered (_("Catchpoint %d (syscall"), b->number);
7894
7895 for (i = 0;
7896 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
7897 i++)
7898 {
7899 struct syscall s;
7900 get_syscall_by_number (iter, &s);
7901
7902 if (s.name)
7903 printf_filtered (" '%s' [%d]", s.name, s.number);
7904 else
7905 printf_filtered (" %d", s.number);
7906 }
7907 printf_filtered (")");
7908 }
7909 else
7910 printf_filtered (_("Catchpoint %d (any syscall)"),
7911 b->number);
7912 }
7913
7914 /* Implement the "print_recreate" breakpoint_ops method for syscall
7915 catchpoints. */
7916
7917 static void
7918 print_recreate_catch_syscall (struct breakpoint *b, struct ui_file *fp)
7919 {
7920 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
7921
7922 fprintf_unfiltered (fp, "catch syscall");
7923
7924 if (c->syscalls_to_be_caught)
7925 {
7926 int i, iter;
7927
7928 for (i = 0;
7929 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
7930 i++)
7931 {
7932 struct syscall s;
7933
7934 get_syscall_by_number (iter, &s);
7935 if (s.name)
7936 fprintf_unfiltered (fp, " %s", s.name);
7937 else
7938 fprintf_unfiltered (fp, " %d", s.number);
7939 }
7940 }
7941 print_recreate_thread (b, fp);
7942 }
7943
7944 /* The breakpoint_ops structure to be used in syscall catchpoints. */
7945
7946 static struct breakpoint_ops catch_syscall_breakpoint_ops;
7947
7948 /* Returns non-zero if 'b' is a syscall catchpoint. */
7949
7950 static int
7951 syscall_catchpoint_p (struct breakpoint *b)
7952 {
7953 return (b->ops == &catch_syscall_breakpoint_ops);
7954 }
7955
7956 /* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
7957 is non-zero, then make the breakpoint temporary. If COND_STRING is
7958 not NULL, then store it in the breakpoint. OPS, if not NULL, is
7959 the breakpoint_ops structure associated to the catchpoint. */
7960
7961 static void
7962 init_catchpoint (struct breakpoint *b,
7963 struct gdbarch *gdbarch, int tempflag,
7964 char *cond_string,
7965 const struct breakpoint_ops *ops)
7966 {
7967 struct symtab_and_line sal;
7968
7969 init_sal (&sal);
7970 sal.pspace = current_program_space;
7971
7972 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
7973
7974 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
7975 b->disposition = tempflag ? disp_del : disp_donttouch;
7976 }
7977
7978 void
7979 install_breakpoint (int internal, struct breakpoint *b, int update_gll)
7980 {
7981 add_to_breakpoint_chain (b);
7982 set_breakpoint_number (internal, b);
7983 if (!internal)
7984 mention (b);
7985 observer_notify_breakpoint_created (b);
7986
7987 if (update_gll)
7988 update_global_location_list (1);
7989 }
7990
7991 static void
7992 create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
7993 int tempflag, char *cond_string,
7994 const struct breakpoint_ops *ops)
7995 {
7996 struct fork_catchpoint *c = XNEW (struct fork_catchpoint);
7997
7998 init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
7999
8000 c->forked_inferior_pid = null_ptid;
8001
8002 install_breakpoint (0, &c->base, 1);
8003 }
8004
8005 /* Exec catchpoints. */
8006
8007 /* An instance of this type is used to represent an exec catchpoint.
8008 It includes a "struct breakpoint" as a kind of base class; users
8009 downcast to "struct breakpoint *" when needed. A breakpoint is
8010 really of this type iff its ops pointer points to
8011 CATCH_EXEC_BREAKPOINT_OPS. */
8012
8013 struct exec_catchpoint
8014 {
8015 /* The base class. */
8016 struct breakpoint base;
8017
8018 /* Filename of a program whose exec triggered this catchpoint.
8019 This field is only valid immediately after this catchpoint has
8020 triggered. */
8021 char *exec_pathname;
8022 };
8023
8024 /* Implement the "dtor" breakpoint_ops method for exec
8025 catchpoints. */
8026
8027 static void
8028 dtor_catch_exec (struct breakpoint *b)
8029 {
8030 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8031
8032 xfree (c->exec_pathname);
8033
8034 base_breakpoint_ops.dtor (b);
8035 }
8036
8037 static int
8038 insert_catch_exec (struct bp_location *bl)
8039 {
8040 return target_insert_exec_catchpoint (PIDGET (inferior_ptid));
8041 }
8042
8043 static int
8044 remove_catch_exec (struct bp_location *bl)
8045 {
8046 return target_remove_exec_catchpoint (PIDGET (inferior_ptid));
8047 }
8048
8049 static int
8050 breakpoint_hit_catch_exec (const struct bp_location *bl,
8051 struct address_space *aspace, CORE_ADDR bp_addr,
8052 const struct target_waitstatus *ws)
8053 {
8054 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8055
8056 if (ws->kind != TARGET_WAITKIND_EXECD)
8057 return 0;
8058
8059 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8060 return 1;
8061 }
8062
8063 static enum print_stop_action
8064 print_it_catch_exec (bpstat bs)
8065 {
8066 struct ui_out *uiout = current_uiout;
8067 struct breakpoint *b = bs->breakpoint_at;
8068 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8069
8070 annotate_catchpoint (b->number);
8071 if (b->disposition == disp_del)
8072 ui_out_text (uiout, "\nTemporary catchpoint ");
8073 else
8074 ui_out_text (uiout, "\nCatchpoint ");
8075 if (ui_out_is_mi_like_p (uiout))
8076 {
8077 ui_out_field_string (uiout, "reason",
8078 async_reason_lookup (EXEC_ASYNC_EXEC));
8079 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8080 }
8081 ui_out_field_int (uiout, "bkptno", b->number);
8082 ui_out_text (uiout, " (exec'd ");
8083 ui_out_field_string (uiout, "new-exec", c->exec_pathname);
8084 ui_out_text (uiout, "), ");
8085
8086 return PRINT_SRC_AND_LOC;
8087 }
8088
8089 static void
8090 print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
8091 {
8092 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8093 struct value_print_options opts;
8094 struct ui_out *uiout = current_uiout;
8095
8096 get_user_print_options (&opts);
8097
8098 /* Field 4, the address, is omitted (which makes the columns
8099 not line up too nicely with the headers, but the effect
8100 is relatively readable). */
8101 if (opts.addressprint)
8102 ui_out_field_skip (uiout, "addr");
8103 annotate_field (5);
8104 ui_out_text (uiout, "exec");
8105 if (c->exec_pathname != NULL)
8106 {
8107 ui_out_text (uiout, ", program \"");
8108 ui_out_field_string (uiout, "what", c->exec_pathname);
8109 ui_out_text (uiout, "\" ");
8110 }
8111 }
8112
8113 static void
8114 print_mention_catch_exec (struct breakpoint *b)
8115 {
8116 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8117 }
8118
8119 /* Implement the "print_recreate" breakpoint_ops method for exec
8120 catchpoints. */
8121
8122 static void
8123 print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8124 {
8125 fprintf_unfiltered (fp, "catch exec");
8126 print_recreate_thread (b, fp);
8127 }
8128
8129 static struct breakpoint_ops catch_exec_breakpoint_ops;
8130
8131 static void
8132 create_syscall_event_catchpoint (int tempflag, VEC(int) *filter,
8133 const struct breakpoint_ops *ops)
8134 {
8135 struct syscall_catchpoint *c;
8136 struct gdbarch *gdbarch = get_current_arch ();
8137
8138 c = XNEW (struct syscall_catchpoint);
8139 init_catchpoint (&c->base, gdbarch, tempflag, NULL, ops);
8140 c->syscalls_to_be_caught = filter;
8141
8142 install_breakpoint (0, &c->base, 1);
8143 }
8144
8145 static int
8146 hw_breakpoint_used_count (void)
8147 {
8148 int i = 0;
8149 struct breakpoint *b;
8150 struct bp_location *bl;
8151
8152 ALL_BREAKPOINTS (b)
8153 {
8154 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
8155 for (bl = b->loc; bl; bl = bl->next)
8156 {
8157 /* Special types of hardware breakpoints may use more than
8158 one register. */
8159 i += b->ops->resources_needed (bl);
8160 }
8161 }
8162
8163 return i;
8164 }
8165
8166 /* Returns the resources B would use if it were a hardware
8167 watchpoint. */
8168
8169 static int
8170 hw_watchpoint_use_count (struct breakpoint *b)
8171 {
8172 int i = 0;
8173 struct bp_location *bl;
8174
8175 if (!breakpoint_enabled (b))
8176 return 0;
8177
8178 for (bl = b->loc; bl; bl = bl->next)
8179 {
8180 /* Special types of hardware watchpoints may use more than
8181 one register. */
8182 i += b->ops->resources_needed (bl);
8183 }
8184
8185 return i;
8186 }
8187
8188 /* Returns the sum the used resources of all hardware watchpoints of
8189 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8190 the sum of the used resources of all hardware watchpoints of other
8191 types _not_ TYPE. */
8192
8193 static int
8194 hw_watchpoint_used_count_others (struct breakpoint *except,
8195 enum bptype type, int *other_type_used)
8196 {
8197 int i = 0;
8198 struct breakpoint *b;
8199
8200 *other_type_used = 0;
8201 ALL_BREAKPOINTS (b)
8202 {
8203 if (b == except)
8204 continue;
8205 if (!breakpoint_enabled (b))
8206 continue;
8207
8208 if (b->type == type)
8209 i += hw_watchpoint_use_count (b);
8210 else if (is_hardware_watchpoint (b))
8211 *other_type_used = 1;
8212 }
8213
8214 return i;
8215 }
8216
8217 void
8218 disable_watchpoints_before_interactive_call_start (void)
8219 {
8220 struct breakpoint *b;
8221
8222 ALL_BREAKPOINTS (b)
8223 {
8224 if (is_watchpoint (b) && breakpoint_enabled (b))
8225 {
8226 b->enable_state = bp_call_disabled;
8227 update_global_location_list (0);
8228 }
8229 }
8230 }
8231
8232 void
8233 enable_watchpoints_after_interactive_call_stop (void)
8234 {
8235 struct breakpoint *b;
8236
8237 ALL_BREAKPOINTS (b)
8238 {
8239 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
8240 {
8241 b->enable_state = bp_enabled;
8242 update_global_location_list (1);
8243 }
8244 }
8245 }
8246
8247 void
8248 disable_breakpoints_before_startup (void)
8249 {
8250 current_program_space->executing_startup = 1;
8251 update_global_location_list (0);
8252 }
8253
8254 void
8255 enable_breakpoints_after_startup (void)
8256 {
8257 current_program_space->executing_startup = 0;
8258 breakpoint_re_set ();
8259 }
8260
8261
8262 /* Set a breakpoint that will evaporate an end of command
8263 at address specified by SAL.
8264 Restrict it to frame FRAME if FRAME is nonzero. */
8265
8266 struct breakpoint *
8267 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8268 struct frame_id frame_id, enum bptype type)
8269 {
8270 struct breakpoint *b;
8271
8272 /* If FRAME_ID is valid, it should be a real frame, not an inlined
8273 one. */
8274 gdb_assert (!frame_id_inlined_p (frame_id));
8275
8276 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
8277 b->enable_state = bp_enabled;
8278 b->disposition = disp_donttouch;
8279 b->frame_id = frame_id;
8280
8281 /* If we're debugging a multi-threaded program, then we want
8282 momentary breakpoints to be active in only a single thread of
8283 control. */
8284 if (in_thread_list (inferior_ptid))
8285 b->thread = pid_to_thread_id (inferior_ptid);
8286
8287 update_global_location_list_nothrow (1);
8288
8289 return b;
8290 }
8291
8292 /* Make a momentary breakpoint based on the master breakpoint ORIG.
8293 The new breakpoint will have type TYPE, and use OPS as it
8294 breakpoint_ops. */
8295
8296 static struct breakpoint *
8297 momentary_breakpoint_from_master (struct breakpoint *orig,
8298 enum bptype type,
8299 const struct breakpoint_ops *ops)
8300 {
8301 struct breakpoint *copy;
8302
8303 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
8304 copy->loc = allocate_bp_location (copy);
8305 set_breakpoint_location_function (copy->loc, 1);
8306
8307 copy->loc->gdbarch = orig->loc->gdbarch;
8308 copy->loc->requested_address = orig->loc->requested_address;
8309 copy->loc->address = orig->loc->address;
8310 copy->loc->section = orig->loc->section;
8311 copy->loc->pspace = orig->loc->pspace;
8312 copy->loc->probe = orig->loc->probe;
8313
8314 if (orig->loc->source_file != NULL)
8315 copy->loc->source_file = xstrdup (orig->loc->source_file);
8316
8317 copy->loc->line_number = orig->loc->line_number;
8318 copy->frame_id = orig->frame_id;
8319 copy->thread = orig->thread;
8320 copy->pspace = orig->pspace;
8321
8322 copy->enable_state = bp_enabled;
8323 copy->disposition = disp_donttouch;
8324 copy->number = internal_breakpoint_number--;
8325
8326 update_global_location_list_nothrow (0);
8327 return copy;
8328 }
8329
8330 /* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8331 ORIG is NULL. */
8332
8333 struct breakpoint *
8334 clone_momentary_breakpoint (struct breakpoint *orig)
8335 {
8336 /* If there's nothing to clone, then return nothing. */
8337 if (orig == NULL)
8338 return NULL;
8339
8340 return momentary_breakpoint_from_master (orig, orig->type, orig->ops);
8341 }
8342
8343 struct breakpoint *
8344 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8345 enum bptype type)
8346 {
8347 struct symtab_and_line sal;
8348
8349 sal = find_pc_line (pc, 0);
8350 sal.pc = pc;
8351 sal.section = find_pc_overlay (pc);
8352 sal.explicit_pc = 1;
8353
8354 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
8355 }
8356 \f
8357
8358 /* Tell the user we have just set a breakpoint B. */
8359
8360 static void
8361 mention (struct breakpoint *b)
8362 {
8363 b->ops->print_mention (b);
8364 if (ui_out_is_mi_like_p (current_uiout))
8365 return;
8366 printf_filtered ("\n");
8367 }
8368 \f
8369
8370 static struct bp_location *
8371 add_location_to_breakpoint (struct breakpoint *b,
8372 const struct symtab_and_line *sal)
8373 {
8374 struct bp_location *loc, **tmp;
8375 CORE_ADDR adjusted_address;
8376 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8377
8378 if (loc_gdbarch == NULL)
8379 loc_gdbarch = b->gdbarch;
8380
8381 /* Adjust the breakpoint's address prior to allocating a location.
8382 Once we call allocate_bp_location(), that mostly uninitialized
8383 location will be placed on the location chain. Adjustment of the
8384 breakpoint may cause target_read_memory() to be called and we do
8385 not want its scan of the location chain to find a breakpoint and
8386 location that's only been partially initialized. */
8387 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8388 sal->pc, b->type);
8389
8390 loc = allocate_bp_location (b);
8391 for (tmp = &(b->loc); *tmp != NULL; tmp = &((*tmp)->next))
8392 ;
8393 *tmp = loc;
8394
8395 loc->requested_address = sal->pc;
8396 loc->address = adjusted_address;
8397 loc->pspace = sal->pspace;
8398 loc->probe = sal->probe;
8399 gdb_assert (loc->pspace != NULL);
8400 loc->section = sal->section;
8401 loc->gdbarch = loc_gdbarch;
8402
8403 if (sal->symtab != NULL)
8404 loc->source_file = xstrdup (sal->symtab->filename);
8405 loc->line_number = sal->line;
8406
8407 set_breakpoint_location_function (loc,
8408 sal->explicit_pc || sal->explicit_line);
8409 return loc;
8410 }
8411 \f
8412
8413 /* Return 1 if LOC is pointing to a permanent breakpoint,
8414 return 0 otherwise. */
8415
8416 static int
8417 bp_loc_is_permanent (struct bp_location *loc)
8418 {
8419 int len;
8420 CORE_ADDR addr;
8421 const gdb_byte *bpoint;
8422 gdb_byte *target_mem;
8423 struct cleanup *cleanup;
8424 int retval = 0;
8425
8426 gdb_assert (loc != NULL);
8427
8428 addr = loc->address;
8429 bpoint = gdbarch_breakpoint_from_pc (loc->gdbarch, &addr, &len);
8430
8431 /* Software breakpoints unsupported? */
8432 if (bpoint == NULL)
8433 return 0;
8434
8435 target_mem = alloca (len);
8436
8437 /* Enable the automatic memory restoration from breakpoints while
8438 we read the memory. Otherwise we could say about our temporary
8439 breakpoints they are permanent. */
8440 cleanup = save_current_space_and_thread ();
8441
8442 switch_to_program_space_and_thread (loc->pspace);
8443 make_show_memory_breakpoints_cleanup (0);
8444
8445 if (target_read_memory (loc->address, target_mem, len) == 0
8446 && memcmp (target_mem, bpoint, len) == 0)
8447 retval = 1;
8448
8449 do_cleanups (cleanup);
8450
8451 return retval;
8452 }
8453
8454 /* The style in which to perform a dynamic printf. This is a user
8455 option because different output options have different tradeoffs;
8456 if GDB does the printing, there is better error handling if there
8457 is a problem with any of the arguments, but using an inferior
8458 function lets you have special-purpose printers and sending of
8459 output to the same place as compiled-in print functions. (Future
8460 styles may include the ability to do a target-side printf.) */
8461
8462 static const char dprintf_style_gdb[] = "gdb";
8463 static const char dprintf_style_call[] = "call";
8464 static const char *const dprintf_style_enums[] = {
8465 dprintf_style_gdb,
8466 dprintf_style_call,
8467 NULL
8468 };
8469 static const char *dprintf_style = dprintf_style_gdb;
8470
8471 /* The function to use for dynamic printf if the preferred style is to
8472 call into the inferior. The value is simply a string that is
8473 copied into the command, so it can be anything that GDB can
8474 evaluate to a callable address, not necessarily a function name. */
8475
8476 static char *dprintf_function = "";
8477
8478 /* The channel to use for dynamic printf if the preferred style is to
8479 call into the inferior; if a nonempty string, it will be passed to
8480 the call as the first argument, with the format string as the
8481 second. As with the dprintf function, this can be anything that
8482 GDB knows how to evaluate, so in addition to common choices like
8483 "stderr", this could be an app-specific expression like
8484 "mystreams[curlogger]". */
8485
8486 static char *dprintf_channel = "";
8487
8488 /* Build a command list for the dprintf corresponding to the current
8489 settings of the dprintf style options. */
8490
8491 static void
8492 update_dprintf_command_list (struct breakpoint *b)
8493 {
8494 char *dprintf_args = b->extra_string;
8495 char *printf_line = NULL;
8496
8497 if (!dprintf_args)
8498 return;
8499
8500 dprintf_args = skip_spaces (dprintf_args);
8501
8502 /* Allow a comma, as it may have terminated a location, but don't
8503 insist on it. */
8504 if (*dprintf_args == ',')
8505 ++dprintf_args;
8506 dprintf_args = skip_spaces (dprintf_args);
8507
8508 if (*dprintf_args != '"')
8509 error (_("Bad format string, missing '\"'."));
8510
8511 if (strcmp (dprintf_style, "gdb") == 0)
8512 printf_line = xstrprintf ("printf %s", dprintf_args);
8513 else if (strcmp (dprintf_style, "call") == 0)
8514 {
8515 if (!dprintf_function)
8516 error (_("No function supplied for dprintf call"));
8517
8518 if (dprintf_channel && strlen (dprintf_channel) > 0)
8519 printf_line = xstrprintf ("call (void) %s (%s,%s)",
8520 dprintf_function,
8521 dprintf_channel,
8522 dprintf_args);
8523 else
8524 printf_line = xstrprintf ("call (void) %s (%s)",
8525 dprintf_function,
8526 dprintf_args);
8527 }
8528 else
8529 internal_error (__FILE__, __LINE__,
8530 _("Invalid dprintf style."));
8531
8532 /* Manufacture a printf/continue sequence. */
8533 if (printf_line)
8534 {
8535 struct command_line *printf_cmd_line, *cont_cmd_line = NULL;
8536
8537 cont_cmd_line = xmalloc (sizeof (struct command_line));
8538 cont_cmd_line->control_type = simple_control;
8539 cont_cmd_line->body_count = 0;
8540 cont_cmd_line->body_list = NULL;
8541 cont_cmd_line->next = NULL;
8542 cont_cmd_line->line = xstrdup ("continue");
8543
8544 printf_cmd_line = xmalloc (sizeof (struct command_line));
8545 printf_cmd_line->control_type = simple_control;
8546 printf_cmd_line->body_count = 0;
8547 printf_cmd_line->body_list = NULL;
8548 printf_cmd_line->next = cont_cmd_line;
8549 printf_cmd_line->line = printf_line;
8550
8551 breakpoint_set_commands (b, printf_cmd_line);
8552 }
8553 }
8554
8555 /* Update all dprintf commands, making their command lists reflect
8556 current style settings. */
8557
8558 static void
8559 update_dprintf_commands (char *args, int from_tty,
8560 struct cmd_list_element *c)
8561 {
8562 struct breakpoint *b;
8563
8564 ALL_BREAKPOINTS (b)
8565 {
8566 if (b->type == bp_dprintf)
8567 update_dprintf_command_list (b);
8568 }
8569 }
8570
8571 /* Create a breakpoint with SAL as location. Use ADDR_STRING
8572 as textual description of the location, and COND_STRING
8573 as condition expression. */
8574
8575 static void
8576 init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
8577 struct symtabs_and_lines sals, char *addr_string,
8578 char *filter, char *cond_string,
8579 char *extra_string,
8580 enum bptype type, enum bpdisp disposition,
8581 int thread, int task, int ignore_count,
8582 const struct breakpoint_ops *ops, int from_tty,
8583 int enabled, int internal, unsigned flags,
8584 int display_canonical)
8585 {
8586 int i;
8587
8588 if (type == bp_hardware_breakpoint)
8589 {
8590 int target_resources_ok;
8591
8592 i = hw_breakpoint_used_count ();
8593 target_resources_ok =
8594 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
8595 i + 1, 0);
8596 if (target_resources_ok == 0)
8597 error (_("No hardware breakpoint support in the target."));
8598 else if (target_resources_ok < 0)
8599 error (_("Hardware breakpoints used exceeds limit."));
8600 }
8601
8602 gdb_assert (sals.nelts > 0);
8603
8604 for (i = 0; i < sals.nelts; ++i)
8605 {
8606 struct symtab_and_line sal = sals.sals[i];
8607 struct bp_location *loc;
8608
8609 if (from_tty)
8610 {
8611 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
8612 if (!loc_gdbarch)
8613 loc_gdbarch = gdbarch;
8614
8615 describe_other_breakpoints (loc_gdbarch,
8616 sal.pspace, sal.pc, sal.section, thread);
8617 }
8618
8619 if (i == 0)
8620 {
8621 init_raw_breakpoint (b, gdbarch, sal, type, ops);
8622 b->thread = thread;
8623 b->task = task;
8624
8625 b->cond_string = cond_string;
8626 b->extra_string = extra_string;
8627 b->ignore_count = ignore_count;
8628 b->enable_state = enabled ? bp_enabled : bp_disabled;
8629 b->disposition = disposition;
8630
8631 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8632 b->loc->inserted = 1;
8633
8634 if (type == bp_static_tracepoint)
8635 {
8636 struct tracepoint *t = (struct tracepoint *) b;
8637 struct static_tracepoint_marker marker;
8638
8639 if (strace_marker_p (b))
8640 {
8641 /* We already know the marker exists, otherwise, we
8642 wouldn't see a sal for it. */
8643 char *p = &addr_string[3];
8644 char *endp;
8645 char *marker_str;
8646
8647 p = skip_spaces (p);
8648
8649 endp = skip_to_space (p);
8650
8651 marker_str = savestring (p, endp - p);
8652 t->static_trace_marker_id = marker_str;
8653
8654 printf_filtered (_("Probed static tracepoint "
8655 "marker \"%s\"\n"),
8656 t->static_trace_marker_id);
8657 }
8658 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
8659 {
8660 t->static_trace_marker_id = xstrdup (marker.str_id);
8661 release_static_tracepoint_marker (&marker);
8662
8663 printf_filtered (_("Probed static tracepoint "
8664 "marker \"%s\"\n"),
8665 t->static_trace_marker_id);
8666 }
8667 else
8668 warning (_("Couldn't determine the static "
8669 "tracepoint marker to probe"));
8670 }
8671
8672 loc = b->loc;
8673 }
8674 else
8675 {
8676 loc = add_location_to_breakpoint (b, &sal);
8677 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
8678 loc->inserted = 1;
8679 }
8680
8681 if (bp_loc_is_permanent (loc))
8682 make_breakpoint_permanent (b);
8683
8684 if (b->cond_string)
8685 {
8686 char *arg = b->cond_string;
8687 loc->cond = parse_exp_1 (&arg, block_for_pc (loc->address), 0);
8688 if (*arg)
8689 error (_("Garbage '%s' follows condition"), arg);
8690 }
8691
8692 /* Dynamic printf requires and uses additional arguments on the
8693 command line, otherwise it's an error. */
8694 if (type == bp_dprintf)
8695 {
8696 if (b->extra_string)
8697 update_dprintf_command_list (b);
8698 else
8699 error (_("Format string required"));
8700 }
8701 else if (b->extra_string)
8702 error (_("Garbage '%s' at end of command"), b->extra_string);
8703 }
8704
8705 b->display_canonical = display_canonical;
8706 if (addr_string)
8707 b->addr_string = addr_string;
8708 else
8709 /* addr_string has to be used or breakpoint_re_set will delete
8710 me. */
8711 b->addr_string
8712 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
8713 b->filter = filter;
8714 }
8715
8716 static void
8717 create_breakpoint_sal (struct gdbarch *gdbarch,
8718 struct symtabs_and_lines sals, char *addr_string,
8719 char *filter, char *cond_string,
8720 char *extra_string,
8721 enum bptype type, enum bpdisp disposition,
8722 int thread, int task, int ignore_count,
8723 const struct breakpoint_ops *ops, int from_tty,
8724 int enabled, int internal, unsigned flags,
8725 int display_canonical)
8726 {
8727 struct breakpoint *b;
8728 struct cleanup *old_chain;
8729
8730 if (is_tracepoint_type (type))
8731 {
8732 struct tracepoint *t;
8733
8734 t = XCNEW (struct tracepoint);
8735 b = &t->base;
8736 }
8737 else
8738 b = XNEW (struct breakpoint);
8739
8740 old_chain = make_cleanup (xfree, b);
8741
8742 init_breakpoint_sal (b, gdbarch,
8743 sals, addr_string,
8744 filter, cond_string, extra_string,
8745 type, disposition,
8746 thread, task, ignore_count,
8747 ops, from_tty,
8748 enabled, internal, flags,
8749 display_canonical);
8750 discard_cleanups (old_chain);
8751
8752 install_breakpoint (internal, b, 0);
8753 }
8754
8755 /* Add SALS.nelts breakpoints to the breakpoint table. For each
8756 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
8757 value. COND_STRING, if not NULL, specified the condition to be
8758 used for all breakpoints. Essentially the only case where
8759 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
8760 function. In that case, it's still not possible to specify
8761 separate conditions for different overloaded functions, so
8762 we take just a single condition string.
8763
8764 NOTE: If the function succeeds, the caller is expected to cleanup
8765 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
8766 array contents). If the function fails (error() is called), the
8767 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
8768 COND and SALS arrays and each of those arrays contents. */
8769
8770 static void
8771 create_breakpoints_sal (struct gdbarch *gdbarch,
8772 struct linespec_result *canonical,
8773 char *cond_string, char *extra_string,
8774 enum bptype type, enum bpdisp disposition,
8775 int thread, int task, int ignore_count,
8776 const struct breakpoint_ops *ops, int from_tty,
8777 int enabled, int internal, unsigned flags)
8778 {
8779 int i;
8780 struct linespec_sals *lsal;
8781
8782 if (canonical->pre_expanded)
8783 gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
8784
8785 for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
8786 {
8787 /* Note that 'addr_string' can be NULL in the case of a plain
8788 'break', without arguments. */
8789 char *addr_string = (canonical->addr_string
8790 ? xstrdup (canonical->addr_string)
8791 : NULL);
8792 char *filter_string = lsal->canonical ? xstrdup (lsal->canonical) : NULL;
8793 struct cleanup *inner = make_cleanup (xfree, addr_string);
8794
8795 make_cleanup (xfree, filter_string);
8796 create_breakpoint_sal (gdbarch, lsal->sals,
8797 addr_string,
8798 filter_string,
8799 cond_string, extra_string,
8800 type, disposition,
8801 thread, task, ignore_count, ops,
8802 from_tty, enabled, internal, flags,
8803 canonical->special_display);
8804 discard_cleanups (inner);
8805 }
8806 }
8807
8808 /* Parse ADDRESS which is assumed to be a SAL specification possibly
8809 followed by conditionals. On return, SALS contains an array of SAL
8810 addresses found. ADDR_STRING contains a vector of (canonical)
8811 address strings. ADDRESS points to the end of the SAL.
8812
8813 The array and the line spec strings are allocated on the heap, it is
8814 the caller's responsibility to free them. */
8815
8816 static void
8817 parse_breakpoint_sals (char **address,
8818 struct linespec_result *canonical)
8819 {
8820 char *addr_start = *address;
8821
8822 /* If no arg given, or if first arg is 'if ', use the default
8823 breakpoint. */
8824 if ((*address) == NULL
8825 || (strncmp ((*address), "if", 2) == 0 && isspace ((*address)[2])))
8826 {
8827 /* The last displayed codepoint, if it's valid, is our default breakpoint
8828 address. */
8829 if (last_displayed_sal_is_valid ())
8830 {
8831 struct linespec_sals lsal;
8832 struct symtab_and_line sal;
8833
8834 init_sal (&sal); /* Initialize to zeroes. */
8835 lsal.sals.sals = (struct symtab_and_line *)
8836 xmalloc (sizeof (struct symtab_and_line));
8837
8838 /* Set sal's pspace, pc, symtab, and line to the values
8839 corresponding to the last call to print_frame_info. */
8840 get_last_displayed_sal (&sal);
8841 sal.section = find_pc_overlay (sal.pc);
8842
8843 /* "break" without arguments is equivalent to "break *PC"
8844 where PC is the last displayed codepoint's address. So
8845 make sure to set sal.explicit_pc to prevent GDB from
8846 trying to expand the list of sals to include all other
8847 instances with the same symtab and line. */
8848 sal.explicit_pc = 1;
8849
8850 lsal.sals.sals[0] = sal;
8851 lsal.sals.nelts = 1;
8852 lsal.canonical = NULL;
8853
8854 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
8855 }
8856 else
8857 error (_("No default breakpoint address now."));
8858 }
8859 else
8860 {
8861 struct symtab_and_line cursal = get_current_source_symtab_and_line ();
8862
8863 /* Force almost all breakpoints to be in terms of the
8864 current_source_symtab (which is decode_line_1's default).
8865 This should produce the results we want almost all of the
8866 time while leaving default_breakpoint_* alone.
8867
8868 ObjC: However, don't match an Objective-C method name which
8869 may have a '+' or '-' succeeded by a '['. */
8870 if (last_displayed_sal_is_valid ()
8871 && (!cursal.symtab
8872 || ((strchr ("+-", (*address)[0]) != NULL)
8873 && ((*address)[1] != '['))))
8874 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
8875 get_last_displayed_symtab (),
8876 get_last_displayed_line (),
8877 canonical, NULL, NULL);
8878 else
8879 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
8880 cursal.symtab, cursal.line, canonical, NULL, NULL);
8881 }
8882 }
8883
8884
8885 /* Convert each SAL into a real PC. Verify that the PC can be
8886 inserted as a breakpoint. If it can't throw an error. */
8887
8888 static void
8889 breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
8890 {
8891 int i;
8892
8893 for (i = 0; i < sals->nelts; i++)
8894 resolve_sal_pc (&sals->sals[i]);
8895 }
8896
8897 /* Fast tracepoints may have restrictions on valid locations. For
8898 instance, a fast tracepoint using a jump instead of a trap will
8899 likely have to overwrite more bytes than a trap would, and so can
8900 only be placed where the instruction is longer than the jump, or a
8901 multi-instruction sequence does not have a jump into the middle of
8902 it, etc. */
8903
8904 static void
8905 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
8906 struct symtabs_and_lines *sals)
8907 {
8908 int i, rslt;
8909 struct symtab_and_line *sal;
8910 char *msg;
8911 struct cleanup *old_chain;
8912
8913 for (i = 0; i < sals->nelts; i++)
8914 {
8915 struct gdbarch *sarch;
8916
8917 sal = &sals->sals[i];
8918
8919 sarch = get_sal_arch (*sal);
8920 /* We fall back to GDBARCH if there is no architecture
8921 associated with SAL. */
8922 if (sarch == NULL)
8923 sarch = gdbarch;
8924 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc,
8925 NULL, &msg);
8926 old_chain = make_cleanup (xfree, msg);
8927
8928 if (!rslt)
8929 error (_("May not have a fast tracepoint at 0x%s%s"),
8930 paddress (sarch, sal->pc), (msg ? msg : ""));
8931
8932 do_cleanups (old_chain);
8933 }
8934 }
8935
8936 /* Given TOK, a string specification of condition and thread, as
8937 accepted by the 'break' command, extract the condition
8938 string and thread number and set *COND_STRING and *THREAD.
8939 PC identifies the context at which the condition should be parsed.
8940 If no condition is found, *COND_STRING is set to NULL.
8941 If no thread is found, *THREAD is set to -1. */
8942
8943 static void
8944 find_condition_and_thread (char *tok, CORE_ADDR pc,
8945 char **cond_string, int *thread, int *task,
8946 char **rest)
8947 {
8948 *cond_string = NULL;
8949 *thread = -1;
8950 while (tok && *tok)
8951 {
8952 char *end_tok;
8953 int toklen;
8954 char *cond_start = NULL;
8955 char *cond_end = NULL;
8956
8957 tok = skip_spaces (tok);
8958
8959 if ((*tok == '"' || *tok == ',') && rest)
8960 {
8961 *rest = savestring (tok, strlen (tok));
8962 return;
8963 }
8964
8965 end_tok = skip_to_space (tok);
8966
8967 toklen = end_tok - tok;
8968
8969 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
8970 {
8971 struct expression *expr;
8972
8973 tok = cond_start = end_tok + 1;
8974 expr = parse_exp_1 (&tok, block_for_pc (pc), 0);
8975 xfree (expr);
8976 cond_end = tok;
8977 *cond_string = savestring (cond_start, cond_end - cond_start);
8978 }
8979 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
8980 {
8981 char *tmptok;
8982
8983 tok = end_tok + 1;
8984 tmptok = tok;
8985 *thread = strtol (tok, &tok, 0);
8986 if (tok == tmptok)
8987 error (_("Junk after thread keyword."));
8988 if (!valid_thread_id (*thread))
8989 error (_("Unknown thread %d."), *thread);
8990 }
8991 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
8992 {
8993 char *tmptok;
8994
8995 tok = end_tok + 1;
8996 tmptok = tok;
8997 *task = strtol (tok, &tok, 0);
8998 if (tok == tmptok)
8999 error (_("Junk after task keyword."));
9000 if (!valid_task_id (*task))
9001 error (_("Unknown task %d."), *task);
9002 }
9003 else if (rest)
9004 {
9005 *rest = savestring (tok, strlen (tok));
9006 return;
9007 }
9008 else
9009 error (_("Junk at end of arguments."));
9010 }
9011 }
9012
9013 /* Decode a static tracepoint marker spec. */
9014
9015 static struct symtabs_and_lines
9016 decode_static_tracepoint_spec (char **arg_p)
9017 {
9018 VEC(static_tracepoint_marker_p) *markers = NULL;
9019 struct symtabs_and_lines sals;
9020 struct cleanup *old_chain;
9021 char *p = &(*arg_p)[3];
9022 char *endp;
9023 char *marker_str;
9024 int i;
9025
9026 p = skip_spaces (p);
9027
9028 endp = skip_to_space (p);
9029
9030 marker_str = savestring (p, endp - p);
9031 old_chain = make_cleanup (xfree, marker_str);
9032
9033 markers = target_static_tracepoint_markers_by_strid (marker_str);
9034 if (VEC_empty(static_tracepoint_marker_p, markers))
9035 error (_("No known static tracepoint marker named %s"), marker_str);
9036
9037 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
9038 sals.sals = xmalloc (sizeof *sals.sals * sals.nelts);
9039
9040 for (i = 0; i < sals.nelts; i++)
9041 {
9042 struct static_tracepoint_marker *marker;
9043
9044 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9045
9046 init_sal (&sals.sals[i]);
9047
9048 sals.sals[i] = find_pc_line (marker->address, 0);
9049 sals.sals[i].pc = marker->address;
9050
9051 release_static_tracepoint_marker (marker);
9052 }
9053
9054 do_cleanups (old_chain);
9055
9056 *arg_p = endp;
9057 return sals;
9058 }
9059
9060 /* Set a breakpoint. This function is shared between CLI and MI
9061 functions for setting a breakpoint. This function has two major
9062 modes of operations, selected by the PARSE_CONDITION_AND_THREAD
9063 parameter. If non-zero, the function will parse arg, extracting
9064 breakpoint location, address and thread. Otherwise, ARG is just
9065 the location of breakpoint, with condition and thread specified by
9066 the COND_STRING and THREAD parameters. If INTERNAL is non-zero,
9067 the breakpoint number will be allocated from the internal
9068 breakpoint count. Returns true if any breakpoint was created;
9069 false otherwise. */
9070
9071 int
9072 create_breakpoint (struct gdbarch *gdbarch,
9073 char *arg, char *cond_string,
9074 int thread, char *extra_string,
9075 int parse_condition_and_thread,
9076 int tempflag, enum bptype type_wanted,
9077 int ignore_count,
9078 enum auto_boolean pending_break_support,
9079 const struct breakpoint_ops *ops,
9080 int from_tty, int enabled, int internal,
9081 unsigned flags)
9082 {
9083 volatile struct gdb_exception e;
9084 char *copy_arg = NULL;
9085 char *addr_start = arg;
9086 struct linespec_result canonical;
9087 struct cleanup *old_chain;
9088 struct cleanup *bkpt_chain = NULL;
9089 int pending = 0;
9090 int task = 0;
9091 int prev_bkpt_count = breakpoint_count;
9092
9093 gdb_assert (ops != NULL);
9094
9095 init_linespec_result (&canonical);
9096
9097 TRY_CATCH (e, RETURN_MASK_ALL)
9098 {
9099 ops->create_sals_from_address (&arg, &canonical, type_wanted,
9100 addr_start, &copy_arg);
9101 }
9102
9103 /* If caller is interested in rc value from parse, set value. */
9104 switch (e.reason)
9105 {
9106 case GDB_NO_ERROR:
9107 if (VEC_empty (linespec_sals, canonical.sals))
9108 return 0;
9109 break;
9110 case RETURN_ERROR:
9111 switch (e.error)
9112 {
9113 case NOT_FOUND_ERROR:
9114
9115 /* If pending breakpoint support is turned off, throw
9116 error. */
9117
9118 if (pending_break_support == AUTO_BOOLEAN_FALSE)
9119 throw_exception (e);
9120
9121 exception_print (gdb_stderr, e);
9122
9123 /* If pending breakpoint support is auto query and the user
9124 selects no, then simply return the error code. */
9125 if (pending_break_support == AUTO_BOOLEAN_AUTO
9126 && !nquery (_("Make %s pending on future shared library load? "),
9127 bptype_string (type_wanted)))
9128 return 0;
9129
9130 /* At this point, either the user was queried about setting
9131 a pending breakpoint and selected yes, or pending
9132 breakpoint behavior is on and thus a pending breakpoint
9133 is defaulted on behalf of the user. */
9134 {
9135 struct linespec_sals lsal;
9136
9137 copy_arg = xstrdup (addr_start);
9138 lsal.canonical = xstrdup (copy_arg);
9139 lsal.sals.nelts = 1;
9140 lsal.sals.sals = XNEW (struct symtab_and_line);
9141 init_sal (&lsal.sals.sals[0]);
9142 pending = 1;
9143 VEC_safe_push (linespec_sals, canonical.sals, &lsal);
9144 }
9145 break;
9146 default:
9147 throw_exception (e);
9148 }
9149 break;
9150 default:
9151 throw_exception (e);
9152 }
9153
9154 /* Create a chain of things that always need to be cleaned up. */
9155 old_chain = make_cleanup_destroy_linespec_result (&canonical);
9156
9157 /* ----------------------------- SNIP -----------------------------
9158 Anything added to the cleanup chain beyond this point is assumed
9159 to be part of a breakpoint. If the breakpoint create succeeds
9160 then the memory is not reclaimed. */
9161 bkpt_chain = make_cleanup (null_cleanup, 0);
9162
9163 /* Resolve all line numbers to PC's and verify that the addresses
9164 are ok for the target. */
9165 if (!pending)
9166 {
9167 int ix;
9168 struct linespec_sals *iter;
9169
9170 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9171 breakpoint_sals_to_pc (&iter->sals);
9172 }
9173
9174 /* Fast tracepoints may have additional restrictions on location. */
9175 if (!pending && type_wanted == bp_fast_tracepoint)
9176 {
9177 int ix;
9178 struct linespec_sals *iter;
9179
9180 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9181 check_fast_tracepoint_sals (gdbarch, &iter->sals);
9182 }
9183
9184 /* Verify that condition can be parsed, before setting any
9185 breakpoints. Allocate a separate condition expression for each
9186 breakpoint. */
9187 if (!pending)
9188 {
9189 struct linespec_sals *lsal;
9190
9191 lsal = VEC_index (linespec_sals, canonical.sals, 0);
9192
9193 if (parse_condition_and_thread)
9194 {
9195 char *rest;
9196 /* Here we only parse 'arg' to separate condition
9197 from thread number, so parsing in context of first
9198 sal is OK. When setting the breakpoint we'll
9199 re-parse it in context of each sal. */
9200 cond_string = NULL;
9201 thread = -1;
9202 rest = NULL;
9203 find_condition_and_thread (arg, lsal->sals.sals[0].pc, &cond_string,
9204 &thread, &task, &rest);
9205 if (cond_string)
9206 make_cleanup (xfree, cond_string);
9207 if (rest)
9208 make_cleanup (xfree, rest);
9209 if (rest)
9210 extra_string = rest;
9211 }
9212 else
9213 {
9214 /* Create a private copy of condition string. */
9215 if (cond_string)
9216 {
9217 cond_string = xstrdup (cond_string);
9218 make_cleanup (xfree, cond_string);
9219 }
9220 /* Create a private copy of any extra string. */
9221 if (extra_string)
9222 {
9223 extra_string = xstrdup (extra_string);
9224 make_cleanup (xfree, extra_string);
9225 }
9226 }
9227
9228 ops->create_breakpoints_sal (gdbarch, &canonical, lsal,
9229 cond_string, extra_string, type_wanted,
9230 tempflag ? disp_del : disp_donttouch,
9231 thread, task, ignore_count, ops,
9232 from_tty, enabled, internal, flags);
9233 }
9234 else
9235 {
9236 struct breakpoint *b;
9237
9238 make_cleanup (xfree, copy_arg);
9239
9240 if (is_tracepoint_type (type_wanted))
9241 {
9242 struct tracepoint *t;
9243
9244 t = XCNEW (struct tracepoint);
9245 b = &t->base;
9246 }
9247 else
9248 b = XNEW (struct breakpoint);
9249
9250 init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops);
9251
9252 b->addr_string = copy_arg;
9253 b->cond_string = NULL;
9254 b->extra_string = NULL;
9255 b->ignore_count = ignore_count;
9256 b->disposition = tempflag ? disp_del : disp_donttouch;
9257 b->condition_not_parsed = 1;
9258 b->enable_state = enabled ? bp_enabled : bp_disabled;
9259 if ((type_wanted != bp_breakpoint
9260 && type_wanted != bp_hardware_breakpoint) || thread != -1)
9261 b->pspace = current_program_space;
9262
9263 install_breakpoint (internal, b, 0);
9264 }
9265
9266 if (VEC_length (linespec_sals, canonical.sals) > 1)
9267 {
9268 warning (_("Multiple breakpoints were set.\nUse the "
9269 "\"delete\" command to delete unwanted breakpoints."));
9270 prev_breakpoint_count = prev_bkpt_count;
9271 }
9272
9273 /* That's it. Discard the cleanups for data inserted into the
9274 breakpoint. */
9275 discard_cleanups (bkpt_chain);
9276 /* But cleanup everything else. */
9277 do_cleanups (old_chain);
9278
9279 /* error call may happen here - have BKPT_CHAIN already discarded. */
9280 update_global_location_list (1);
9281
9282 return 1;
9283 }
9284
9285 /* Set a breakpoint.
9286 ARG is a string describing breakpoint address,
9287 condition, and thread.
9288 FLAG specifies if a breakpoint is hardware on,
9289 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9290 and BP_TEMPFLAG. */
9291
9292 static void
9293 break_command_1 (char *arg, int flag, int from_tty)
9294 {
9295 int tempflag = flag & BP_TEMPFLAG;
9296 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9297 ? bp_hardware_breakpoint
9298 : bp_breakpoint);
9299 struct breakpoint_ops *ops;
9300 const char *arg_cp = arg;
9301
9302 /* Matching breakpoints on probes. */
9303 if (arg && probe_linespec_to_ops (&arg_cp) != NULL)
9304 ops = &bkpt_probe_breakpoint_ops;
9305 else
9306 ops = &bkpt_breakpoint_ops;
9307
9308 create_breakpoint (get_current_arch (),
9309 arg,
9310 NULL, 0, NULL, 1 /* parse arg */,
9311 tempflag, type_wanted,
9312 0 /* Ignore count */,
9313 pending_break_support,
9314 ops,
9315 from_tty,
9316 1 /* enabled */,
9317 0 /* internal */,
9318 0);
9319 }
9320
9321 /* Helper function for break_command_1 and disassemble_command. */
9322
9323 void
9324 resolve_sal_pc (struct symtab_and_line *sal)
9325 {
9326 CORE_ADDR pc;
9327
9328 if (sal->pc == 0 && sal->symtab != NULL)
9329 {
9330 if (!find_line_pc (sal->symtab, sal->line, &pc))
9331 error (_("No line %d in file \"%s\"."),
9332 sal->line, sal->symtab->filename);
9333 sal->pc = pc;
9334
9335 /* If this SAL corresponds to a breakpoint inserted using a line
9336 number, then skip the function prologue if necessary. */
9337 if (sal->explicit_line)
9338 skip_prologue_sal (sal);
9339 }
9340
9341 if (sal->section == 0 && sal->symtab != NULL)
9342 {
9343 struct blockvector *bv;
9344 struct block *b;
9345 struct symbol *sym;
9346
9347 bv = blockvector_for_pc_sect (sal->pc, 0, &b, sal->symtab);
9348 if (bv != NULL)
9349 {
9350 sym = block_linkage_function (b);
9351 if (sym != NULL)
9352 {
9353 fixup_symbol_section (sym, sal->symtab->objfile);
9354 sal->section = SYMBOL_OBJ_SECTION (sym);
9355 }
9356 else
9357 {
9358 /* It really is worthwhile to have the section, so we'll
9359 just have to look harder. This case can be executed
9360 if we have line numbers but no functions (as can
9361 happen in assembly source). */
9362
9363 struct minimal_symbol *msym;
9364 struct cleanup *old_chain = save_current_space_and_thread ();
9365
9366 switch_to_program_space_and_thread (sal->pspace);
9367
9368 msym = lookup_minimal_symbol_by_pc (sal->pc);
9369 if (msym)
9370 sal->section = SYMBOL_OBJ_SECTION (msym);
9371
9372 do_cleanups (old_chain);
9373 }
9374 }
9375 }
9376 }
9377
9378 void
9379 break_command (char *arg, int from_tty)
9380 {
9381 break_command_1 (arg, 0, from_tty);
9382 }
9383
9384 void
9385 tbreak_command (char *arg, int from_tty)
9386 {
9387 break_command_1 (arg, BP_TEMPFLAG, from_tty);
9388 }
9389
9390 static void
9391 hbreak_command (char *arg, int from_tty)
9392 {
9393 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
9394 }
9395
9396 static void
9397 thbreak_command (char *arg, int from_tty)
9398 {
9399 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
9400 }
9401
9402 static void
9403 stop_command (char *arg, int from_tty)
9404 {
9405 printf_filtered (_("Specify the type of breakpoint to set.\n\
9406 Usage: stop in <function | address>\n\
9407 stop at <line>\n"));
9408 }
9409
9410 static void
9411 stopin_command (char *arg, int from_tty)
9412 {
9413 int badInput = 0;
9414
9415 if (arg == (char *) NULL)
9416 badInput = 1;
9417 else if (*arg != '*')
9418 {
9419 char *argptr = arg;
9420 int hasColon = 0;
9421
9422 /* Look for a ':'. If this is a line number specification, then
9423 say it is bad, otherwise, it should be an address or
9424 function/method name. */
9425 while (*argptr && !hasColon)
9426 {
9427 hasColon = (*argptr == ':');
9428 argptr++;
9429 }
9430
9431 if (hasColon)
9432 badInput = (*argptr != ':'); /* Not a class::method */
9433 else
9434 badInput = isdigit (*arg); /* a simple line number */
9435 }
9436
9437 if (badInput)
9438 printf_filtered (_("Usage: stop in <function | address>\n"));
9439 else
9440 break_command_1 (arg, 0, from_tty);
9441 }
9442
9443 static void
9444 stopat_command (char *arg, int from_tty)
9445 {
9446 int badInput = 0;
9447
9448 if (arg == (char *) NULL || *arg == '*') /* no line number */
9449 badInput = 1;
9450 else
9451 {
9452 char *argptr = arg;
9453 int hasColon = 0;
9454
9455 /* Look for a ':'. If there is a '::' then get out, otherwise
9456 it is probably a line number. */
9457 while (*argptr && !hasColon)
9458 {
9459 hasColon = (*argptr == ':');
9460 argptr++;
9461 }
9462
9463 if (hasColon)
9464 badInput = (*argptr == ':'); /* we have class::method */
9465 else
9466 badInput = !isdigit (*arg); /* not a line number */
9467 }
9468
9469 if (badInput)
9470 printf_filtered (_("Usage: stop at <line>\n"));
9471 else
9472 break_command_1 (arg, 0, from_tty);
9473 }
9474
9475 void dprintf_command (char *arg, int from_tty);
9476
9477 /* The dynamic printf command is mostly like a regular breakpoint, but
9478 with a prewired command list consisting of a single output command,
9479 built from extra arguments supplied on the dprintf command
9480 line. */
9481
9482 void
9483 dprintf_command (char *arg, int from_tty)
9484 {
9485 create_breakpoint (get_current_arch (),
9486 arg,
9487 NULL, 0, NULL, 1 /* parse arg */,
9488 0, bp_dprintf,
9489 0 /* Ignore count */,
9490 pending_break_support,
9491 &dprintf_breakpoint_ops,
9492 from_tty,
9493 1 /* enabled */,
9494 0 /* internal */,
9495 0);
9496 }
9497
9498 /* Implement the "breakpoint_hit" breakpoint_ops method for
9499 ranged breakpoints. */
9500
9501 static int
9502 breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
9503 struct address_space *aspace,
9504 CORE_ADDR bp_addr,
9505 const struct target_waitstatus *ws)
9506 {
9507 if (ws->kind != TARGET_WAITKIND_STOPPED
9508 || ws->value.sig != GDB_SIGNAL_TRAP)
9509 return 0;
9510
9511 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
9512 bl->length, aspace, bp_addr);
9513 }
9514
9515 /* Implement the "resources_needed" breakpoint_ops method for
9516 ranged breakpoints. */
9517
9518 static int
9519 resources_needed_ranged_breakpoint (const struct bp_location *bl)
9520 {
9521 return target_ranged_break_num_registers ();
9522 }
9523
9524 /* Implement the "print_it" breakpoint_ops method for
9525 ranged breakpoints. */
9526
9527 static enum print_stop_action
9528 print_it_ranged_breakpoint (bpstat bs)
9529 {
9530 struct breakpoint *b = bs->breakpoint_at;
9531 struct bp_location *bl = b->loc;
9532 struct ui_out *uiout = current_uiout;
9533
9534 gdb_assert (b->type == bp_hardware_breakpoint);
9535
9536 /* Ranged breakpoints have only one location. */
9537 gdb_assert (bl && bl->next == NULL);
9538
9539 annotate_breakpoint (b->number);
9540 if (b->disposition == disp_del)
9541 ui_out_text (uiout, "\nTemporary ranged breakpoint ");
9542 else
9543 ui_out_text (uiout, "\nRanged breakpoint ");
9544 if (ui_out_is_mi_like_p (uiout))
9545 {
9546 ui_out_field_string (uiout, "reason",
9547 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
9548 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
9549 }
9550 ui_out_field_int (uiout, "bkptno", b->number);
9551 ui_out_text (uiout, ", ");
9552
9553 return PRINT_SRC_AND_LOC;
9554 }
9555
9556 /* Implement the "print_one" breakpoint_ops method for
9557 ranged breakpoints. */
9558
9559 static void
9560 print_one_ranged_breakpoint (struct breakpoint *b,
9561 struct bp_location **last_loc)
9562 {
9563 struct bp_location *bl = b->loc;
9564 struct value_print_options opts;
9565 struct ui_out *uiout = current_uiout;
9566
9567 /* Ranged breakpoints have only one location. */
9568 gdb_assert (bl && bl->next == NULL);
9569
9570 get_user_print_options (&opts);
9571
9572 if (opts.addressprint)
9573 /* We don't print the address range here, it will be printed later
9574 by print_one_detail_ranged_breakpoint. */
9575 ui_out_field_skip (uiout, "addr");
9576 annotate_field (5);
9577 print_breakpoint_location (b, bl);
9578 *last_loc = bl;
9579 }
9580
9581 /* Implement the "print_one_detail" breakpoint_ops method for
9582 ranged breakpoints. */
9583
9584 static void
9585 print_one_detail_ranged_breakpoint (const struct breakpoint *b,
9586 struct ui_out *uiout)
9587 {
9588 CORE_ADDR address_start, address_end;
9589 struct bp_location *bl = b->loc;
9590 struct ui_file *stb = mem_fileopen ();
9591 struct cleanup *cleanup = make_cleanup_ui_file_delete (stb);
9592
9593 gdb_assert (bl);
9594
9595 address_start = bl->address;
9596 address_end = address_start + bl->length - 1;
9597
9598 ui_out_text (uiout, "\taddress range: ");
9599 fprintf_unfiltered (stb, "[%s, %s]",
9600 print_core_address (bl->gdbarch, address_start),
9601 print_core_address (bl->gdbarch, address_end));
9602 ui_out_field_stream (uiout, "addr", stb);
9603 ui_out_text (uiout, "\n");
9604
9605 do_cleanups (cleanup);
9606 }
9607
9608 /* Implement the "print_mention" breakpoint_ops method for
9609 ranged breakpoints. */
9610
9611 static void
9612 print_mention_ranged_breakpoint (struct breakpoint *b)
9613 {
9614 struct bp_location *bl = b->loc;
9615 struct ui_out *uiout = current_uiout;
9616
9617 gdb_assert (bl);
9618 gdb_assert (b->type == bp_hardware_breakpoint);
9619
9620 if (ui_out_is_mi_like_p (uiout))
9621 return;
9622
9623 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
9624 b->number, paddress (bl->gdbarch, bl->address),
9625 paddress (bl->gdbarch, bl->address + bl->length - 1));
9626 }
9627
9628 /* Implement the "print_recreate" breakpoint_ops method for
9629 ranged breakpoints. */
9630
9631 static void
9632 print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
9633 {
9634 fprintf_unfiltered (fp, "break-range %s, %s", b->addr_string,
9635 b->addr_string_range_end);
9636 print_recreate_thread (b, fp);
9637 }
9638
9639 /* The breakpoint_ops structure to be used in ranged breakpoints. */
9640
9641 static struct breakpoint_ops ranged_breakpoint_ops;
9642
9643 /* Find the address where the end of the breakpoint range should be
9644 placed, given the SAL of the end of the range. This is so that if
9645 the user provides a line number, the end of the range is set to the
9646 last instruction of the given line. */
9647
9648 static CORE_ADDR
9649 find_breakpoint_range_end (struct symtab_and_line sal)
9650 {
9651 CORE_ADDR end;
9652
9653 /* If the user provided a PC value, use it. Otherwise,
9654 find the address of the end of the given location. */
9655 if (sal.explicit_pc)
9656 end = sal.pc;
9657 else
9658 {
9659 int ret;
9660 CORE_ADDR start;
9661
9662 ret = find_line_pc_range (sal, &start, &end);
9663 if (!ret)
9664 error (_("Could not find location of the end of the range."));
9665
9666 /* find_line_pc_range returns the start of the next line. */
9667 end--;
9668 }
9669
9670 return end;
9671 }
9672
9673 /* Implement the "break-range" CLI command. */
9674
9675 static void
9676 break_range_command (char *arg, int from_tty)
9677 {
9678 char *arg_start, *addr_string_start, *addr_string_end;
9679 struct linespec_result canonical_start, canonical_end;
9680 int bp_count, can_use_bp, length;
9681 CORE_ADDR end;
9682 struct breakpoint *b;
9683 struct symtab_and_line sal_start, sal_end;
9684 struct cleanup *cleanup_bkpt;
9685 struct linespec_sals *lsal_start, *lsal_end;
9686
9687 /* We don't support software ranged breakpoints. */
9688 if (target_ranged_break_num_registers () < 0)
9689 error (_("This target does not support hardware ranged breakpoints."));
9690
9691 bp_count = hw_breakpoint_used_count ();
9692 bp_count += target_ranged_break_num_registers ();
9693 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9694 bp_count, 0);
9695 if (can_use_bp < 0)
9696 error (_("Hardware breakpoints used exceeds limit."));
9697
9698 arg = skip_spaces (arg);
9699 if (arg == NULL || arg[0] == '\0')
9700 error(_("No address range specified."));
9701
9702 init_linespec_result (&canonical_start);
9703
9704 arg_start = arg;
9705 parse_breakpoint_sals (&arg, &canonical_start);
9706
9707 cleanup_bkpt = make_cleanup_destroy_linespec_result (&canonical_start);
9708
9709 if (arg[0] != ',')
9710 error (_("Too few arguments."));
9711 else if (VEC_empty (linespec_sals, canonical_start.sals))
9712 error (_("Could not find location of the beginning of the range."));
9713
9714 lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
9715
9716 if (VEC_length (linespec_sals, canonical_start.sals) > 1
9717 || lsal_start->sals.nelts != 1)
9718 error (_("Cannot create a ranged breakpoint with multiple locations."));
9719
9720 sal_start = lsal_start->sals.sals[0];
9721 addr_string_start = savestring (arg_start, arg - arg_start);
9722 make_cleanup (xfree, addr_string_start);
9723
9724 arg++; /* Skip the comma. */
9725 arg = skip_spaces (arg);
9726
9727 /* Parse the end location. */
9728
9729 init_linespec_result (&canonical_end);
9730 arg_start = arg;
9731
9732 /* We call decode_line_full directly here instead of using
9733 parse_breakpoint_sals because we need to specify the start location's
9734 symtab and line as the default symtab and line for the end of the
9735 range. This makes it possible to have ranges like "foo.c:27, +14",
9736 where +14 means 14 lines from the start location. */
9737 decode_line_full (&arg, DECODE_LINE_FUNFIRSTLINE,
9738 sal_start.symtab, sal_start.line,
9739 &canonical_end, NULL, NULL);
9740
9741 make_cleanup_destroy_linespec_result (&canonical_end);
9742
9743 if (VEC_empty (linespec_sals, canonical_end.sals))
9744 error (_("Could not find location of the end of the range."));
9745
9746 lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
9747 if (VEC_length (linespec_sals, canonical_end.sals) > 1
9748 || lsal_end->sals.nelts != 1)
9749 error (_("Cannot create a ranged breakpoint with multiple locations."));
9750
9751 sal_end = lsal_end->sals.sals[0];
9752 addr_string_end = savestring (arg_start, arg - arg_start);
9753 make_cleanup (xfree, addr_string_end);
9754
9755 end = find_breakpoint_range_end (sal_end);
9756 if (sal_start.pc > end)
9757 error (_("Invalid address range, end precedes start."));
9758
9759 length = end - sal_start.pc + 1;
9760 if (length < 0)
9761 /* Length overflowed. */
9762 error (_("Address range too large."));
9763 else if (length == 1)
9764 {
9765 /* This range is simple enough to be handled by
9766 the `hbreak' command. */
9767 hbreak_command (addr_string_start, 1);
9768
9769 do_cleanups (cleanup_bkpt);
9770
9771 return;
9772 }
9773
9774 /* Now set up the breakpoint. */
9775 b = set_raw_breakpoint (get_current_arch (), sal_start,
9776 bp_hardware_breakpoint, &ranged_breakpoint_ops);
9777 set_breakpoint_count (breakpoint_count + 1);
9778 b->number = breakpoint_count;
9779 b->disposition = disp_donttouch;
9780 b->addr_string = xstrdup (addr_string_start);
9781 b->addr_string_range_end = xstrdup (addr_string_end);
9782 b->loc->length = length;
9783
9784 do_cleanups (cleanup_bkpt);
9785
9786 mention (b);
9787 observer_notify_breakpoint_created (b);
9788 update_global_location_list (1);
9789 }
9790
9791 /* Return non-zero if EXP is verified as constant. Returned zero
9792 means EXP is variable. Also the constant detection may fail for
9793 some constant expressions and in such case still falsely return
9794 zero. */
9795
9796 static int
9797 watchpoint_exp_is_const (const struct expression *exp)
9798 {
9799 int i = exp->nelts;
9800
9801 while (i > 0)
9802 {
9803 int oplenp, argsp;
9804
9805 /* We are only interested in the descriptor of each element. */
9806 operator_length (exp, i, &oplenp, &argsp);
9807 i -= oplenp;
9808
9809 switch (exp->elts[i].opcode)
9810 {
9811 case BINOP_ADD:
9812 case BINOP_SUB:
9813 case BINOP_MUL:
9814 case BINOP_DIV:
9815 case BINOP_REM:
9816 case BINOP_MOD:
9817 case BINOP_LSH:
9818 case BINOP_RSH:
9819 case BINOP_LOGICAL_AND:
9820 case BINOP_LOGICAL_OR:
9821 case BINOP_BITWISE_AND:
9822 case BINOP_BITWISE_IOR:
9823 case BINOP_BITWISE_XOR:
9824 case BINOP_EQUAL:
9825 case BINOP_NOTEQUAL:
9826 case BINOP_LESS:
9827 case BINOP_GTR:
9828 case BINOP_LEQ:
9829 case BINOP_GEQ:
9830 case BINOP_REPEAT:
9831 case BINOP_COMMA:
9832 case BINOP_EXP:
9833 case BINOP_MIN:
9834 case BINOP_MAX:
9835 case BINOP_INTDIV:
9836 case BINOP_CONCAT:
9837 case BINOP_IN:
9838 case BINOP_RANGE:
9839 case TERNOP_COND:
9840 case TERNOP_SLICE:
9841 case TERNOP_SLICE_COUNT:
9842
9843 case OP_LONG:
9844 case OP_DOUBLE:
9845 case OP_DECFLOAT:
9846 case OP_LAST:
9847 case OP_COMPLEX:
9848 case OP_STRING:
9849 case OP_BITSTRING:
9850 case OP_ARRAY:
9851 case OP_TYPE:
9852 case OP_NAME:
9853 case OP_OBJC_NSSTRING:
9854
9855 case UNOP_NEG:
9856 case UNOP_LOGICAL_NOT:
9857 case UNOP_COMPLEMENT:
9858 case UNOP_ADDR:
9859 case UNOP_HIGH:
9860 case UNOP_CAST:
9861 /* Unary, binary and ternary operators: We have to check
9862 their operands. If they are constant, then so is the
9863 result of that operation. For instance, if A and B are
9864 determined to be constants, then so is "A + B".
9865
9866 UNOP_IND is one exception to the rule above, because the
9867 value of *ADDR is not necessarily a constant, even when
9868 ADDR is. */
9869 break;
9870
9871 case OP_VAR_VALUE:
9872 /* Check whether the associated symbol is a constant.
9873
9874 We use SYMBOL_CLASS rather than TYPE_CONST because it's
9875 possible that a buggy compiler could mark a variable as
9876 constant even when it is not, and TYPE_CONST would return
9877 true in this case, while SYMBOL_CLASS wouldn't.
9878
9879 We also have to check for function symbols because they
9880 are always constant. */
9881 {
9882 struct symbol *s = exp->elts[i + 2].symbol;
9883
9884 if (SYMBOL_CLASS (s) != LOC_BLOCK
9885 && SYMBOL_CLASS (s) != LOC_CONST
9886 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
9887 return 0;
9888 break;
9889 }
9890
9891 /* The default action is to return 0 because we are using
9892 the optimistic approach here: If we don't know something,
9893 then it is not a constant. */
9894 default:
9895 return 0;
9896 }
9897 }
9898
9899 return 1;
9900 }
9901
9902 /* Implement the "dtor" breakpoint_ops method for watchpoints. */
9903
9904 static void
9905 dtor_watchpoint (struct breakpoint *self)
9906 {
9907 struct watchpoint *w = (struct watchpoint *) self;
9908
9909 xfree (w->cond_exp);
9910 xfree (w->exp);
9911 xfree (w->exp_string);
9912 xfree (w->exp_string_reparse);
9913 value_free (w->val);
9914
9915 base_breakpoint_ops.dtor (self);
9916 }
9917
9918 /* Implement the "re_set" breakpoint_ops method for watchpoints. */
9919
9920 static void
9921 re_set_watchpoint (struct breakpoint *b)
9922 {
9923 struct watchpoint *w = (struct watchpoint *) b;
9924
9925 /* Watchpoint can be either on expression using entirely global
9926 variables, or it can be on local variables.
9927
9928 Watchpoints of the first kind are never auto-deleted, and even
9929 persist across program restarts. Since they can use variables
9930 from shared libraries, we need to reparse expression as libraries
9931 are loaded and unloaded.
9932
9933 Watchpoints on local variables can also change meaning as result
9934 of solib event. For example, if a watchpoint uses both a local
9935 and a global variables in expression, it's a local watchpoint,
9936 but unloading of a shared library will make the expression
9937 invalid. This is not a very common use case, but we still
9938 re-evaluate expression, to avoid surprises to the user.
9939
9940 Note that for local watchpoints, we re-evaluate it only if
9941 watchpoints frame id is still valid. If it's not, it means the
9942 watchpoint is out of scope and will be deleted soon. In fact,
9943 I'm not sure we'll ever be called in this case.
9944
9945 If a local watchpoint's frame id is still valid, then
9946 w->exp_valid_block is likewise valid, and we can safely use it.
9947
9948 Don't do anything about disabled watchpoints, since they will be
9949 reevaluated again when enabled. */
9950 update_watchpoint (w, 1 /* reparse */);
9951 }
9952
9953 /* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
9954
9955 static int
9956 insert_watchpoint (struct bp_location *bl)
9957 {
9958 struct watchpoint *w = (struct watchpoint *) bl->owner;
9959 int length = w->exact ? 1 : bl->length;
9960
9961 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
9962 w->cond_exp);
9963 }
9964
9965 /* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
9966
9967 static int
9968 remove_watchpoint (struct bp_location *bl)
9969 {
9970 struct watchpoint *w = (struct watchpoint *) bl->owner;
9971 int length = w->exact ? 1 : bl->length;
9972
9973 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
9974 w->cond_exp);
9975 }
9976
9977 static int
9978 breakpoint_hit_watchpoint (const struct bp_location *bl,
9979 struct address_space *aspace, CORE_ADDR bp_addr,
9980 const struct target_waitstatus *ws)
9981 {
9982 struct breakpoint *b = bl->owner;
9983 struct watchpoint *w = (struct watchpoint *) b;
9984
9985 /* Continuable hardware watchpoints are treated as non-existent if the
9986 reason we stopped wasn't a hardware watchpoint (we didn't stop on
9987 some data address). Otherwise gdb won't stop on a break instruction
9988 in the code (not from a breakpoint) when a hardware watchpoint has
9989 been defined. Also skip watchpoints which we know did not trigger
9990 (did not match the data address). */
9991 if (is_hardware_watchpoint (b)
9992 && w->watchpoint_triggered == watch_triggered_no)
9993 return 0;
9994
9995 return 1;
9996 }
9997
9998 static void
9999 check_status_watchpoint (bpstat bs)
10000 {
10001 gdb_assert (is_watchpoint (bs->breakpoint_at));
10002
10003 bpstat_check_watchpoint (bs);
10004 }
10005
10006 /* Implement the "resources_needed" breakpoint_ops method for
10007 hardware watchpoints. */
10008
10009 static int
10010 resources_needed_watchpoint (const struct bp_location *bl)
10011 {
10012 struct watchpoint *w = (struct watchpoint *) bl->owner;
10013 int length = w->exact? 1 : bl->length;
10014
10015 return target_region_ok_for_hw_watchpoint (bl->address, length);
10016 }
10017
10018 /* Implement the "works_in_software_mode" breakpoint_ops method for
10019 hardware watchpoints. */
10020
10021 static int
10022 works_in_software_mode_watchpoint (const struct breakpoint *b)
10023 {
10024 /* Read and access watchpoints only work with hardware support. */
10025 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
10026 }
10027
10028 static enum print_stop_action
10029 print_it_watchpoint (bpstat bs)
10030 {
10031 struct cleanup *old_chain;
10032 struct breakpoint *b;
10033 const struct bp_location *bl;
10034 struct ui_file *stb;
10035 enum print_stop_action result;
10036 struct watchpoint *w;
10037 struct ui_out *uiout = current_uiout;
10038
10039 gdb_assert (bs->bp_location_at != NULL);
10040
10041 bl = bs->bp_location_at;
10042 b = bs->breakpoint_at;
10043 w = (struct watchpoint *) b;
10044
10045 stb = mem_fileopen ();
10046 old_chain = make_cleanup_ui_file_delete (stb);
10047
10048 switch (b->type)
10049 {
10050 case bp_watchpoint:
10051 case bp_hardware_watchpoint:
10052 annotate_watchpoint (b->number);
10053 if (ui_out_is_mi_like_p (uiout))
10054 ui_out_field_string
10055 (uiout, "reason",
10056 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10057 mention (b);
10058 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10059 ui_out_text (uiout, "\nOld value = ");
10060 watchpoint_value_print (bs->old_val, stb);
10061 ui_out_field_stream (uiout, "old", stb);
10062 ui_out_text (uiout, "\nNew value = ");
10063 watchpoint_value_print (w->val, stb);
10064 ui_out_field_stream (uiout, "new", stb);
10065 ui_out_text (uiout, "\n");
10066 /* More than one watchpoint may have been triggered. */
10067 result = PRINT_UNKNOWN;
10068 break;
10069
10070 case bp_read_watchpoint:
10071 if (ui_out_is_mi_like_p (uiout))
10072 ui_out_field_string
10073 (uiout, "reason",
10074 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10075 mention (b);
10076 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10077 ui_out_text (uiout, "\nValue = ");
10078 watchpoint_value_print (w->val, stb);
10079 ui_out_field_stream (uiout, "value", stb);
10080 ui_out_text (uiout, "\n");
10081 result = PRINT_UNKNOWN;
10082 break;
10083
10084 case bp_access_watchpoint:
10085 if (bs->old_val != NULL)
10086 {
10087 annotate_watchpoint (b->number);
10088 if (ui_out_is_mi_like_p (uiout))
10089 ui_out_field_string
10090 (uiout, "reason",
10091 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10092 mention (b);
10093 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10094 ui_out_text (uiout, "\nOld value = ");
10095 watchpoint_value_print (bs->old_val, stb);
10096 ui_out_field_stream (uiout, "old", stb);
10097 ui_out_text (uiout, "\nNew value = ");
10098 }
10099 else
10100 {
10101 mention (b);
10102 if (ui_out_is_mi_like_p (uiout))
10103 ui_out_field_string
10104 (uiout, "reason",
10105 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10106 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10107 ui_out_text (uiout, "\nValue = ");
10108 }
10109 watchpoint_value_print (w->val, stb);
10110 ui_out_field_stream (uiout, "new", stb);
10111 ui_out_text (uiout, "\n");
10112 result = PRINT_UNKNOWN;
10113 break;
10114 default:
10115 result = PRINT_UNKNOWN;
10116 }
10117
10118 do_cleanups (old_chain);
10119 return result;
10120 }
10121
10122 /* Implement the "print_mention" breakpoint_ops method for hardware
10123 watchpoints. */
10124
10125 static void
10126 print_mention_watchpoint (struct breakpoint *b)
10127 {
10128 struct cleanup *ui_out_chain;
10129 struct watchpoint *w = (struct watchpoint *) b;
10130 struct ui_out *uiout = current_uiout;
10131
10132 switch (b->type)
10133 {
10134 case bp_watchpoint:
10135 ui_out_text (uiout, "Watchpoint ");
10136 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10137 break;
10138 case bp_hardware_watchpoint:
10139 ui_out_text (uiout, "Hardware watchpoint ");
10140 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10141 break;
10142 case bp_read_watchpoint:
10143 ui_out_text (uiout, "Hardware read watchpoint ");
10144 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
10145 break;
10146 case bp_access_watchpoint:
10147 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
10148 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
10149 break;
10150 default:
10151 internal_error (__FILE__, __LINE__,
10152 _("Invalid hardware watchpoint type."));
10153 }
10154
10155 ui_out_field_int (uiout, "number", b->number);
10156 ui_out_text (uiout, ": ");
10157 ui_out_field_string (uiout, "exp", w->exp_string);
10158 do_cleanups (ui_out_chain);
10159 }
10160
10161 /* Implement the "print_recreate" breakpoint_ops method for
10162 watchpoints. */
10163
10164 static void
10165 print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10166 {
10167 struct watchpoint *w = (struct watchpoint *) b;
10168
10169 switch (b->type)
10170 {
10171 case bp_watchpoint:
10172 case bp_hardware_watchpoint:
10173 fprintf_unfiltered (fp, "watch");
10174 break;
10175 case bp_read_watchpoint:
10176 fprintf_unfiltered (fp, "rwatch");
10177 break;
10178 case bp_access_watchpoint:
10179 fprintf_unfiltered (fp, "awatch");
10180 break;
10181 default:
10182 internal_error (__FILE__, __LINE__,
10183 _("Invalid watchpoint type."));
10184 }
10185
10186 fprintf_unfiltered (fp, " %s", w->exp_string);
10187 print_recreate_thread (b, fp);
10188 }
10189
10190 /* The breakpoint_ops structure to be used in hardware watchpoints. */
10191
10192 static struct breakpoint_ops watchpoint_breakpoint_ops;
10193
10194 /* Implement the "insert" breakpoint_ops method for
10195 masked hardware watchpoints. */
10196
10197 static int
10198 insert_masked_watchpoint (struct bp_location *bl)
10199 {
10200 struct watchpoint *w = (struct watchpoint *) bl->owner;
10201
10202 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
10203 bl->watchpoint_type);
10204 }
10205
10206 /* Implement the "remove" breakpoint_ops method for
10207 masked hardware watchpoints. */
10208
10209 static int
10210 remove_masked_watchpoint (struct bp_location *bl)
10211 {
10212 struct watchpoint *w = (struct watchpoint *) bl->owner;
10213
10214 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
10215 bl->watchpoint_type);
10216 }
10217
10218 /* Implement the "resources_needed" breakpoint_ops method for
10219 masked hardware watchpoints. */
10220
10221 static int
10222 resources_needed_masked_watchpoint (const struct bp_location *bl)
10223 {
10224 struct watchpoint *w = (struct watchpoint *) bl->owner;
10225
10226 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
10227 }
10228
10229 /* Implement the "works_in_software_mode" breakpoint_ops method for
10230 masked hardware watchpoints. */
10231
10232 static int
10233 works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10234 {
10235 return 0;
10236 }
10237
10238 /* Implement the "print_it" breakpoint_ops method for
10239 masked hardware watchpoints. */
10240
10241 static enum print_stop_action
10242 print_it_masked_watchpoint (bpstat bs)
10243 {
10244 struct breakpoint *b = bs->breakpoint_at;
10245 struct ui_out *uiout = current_uiout;
10246
10247 /* Masked watchpoints have only one location. */
10248 gdb_assert (b->loc && b->loc->next == NULL);
10249
10250 switch (b->type)
10251 {
10252 case bp_hardware_watchpoint:
10253 annotate_watchpoint (b->number);
10254 if (ui_out_is_mi_like_p (uiout))
10255 ui_out_field_string
10256 (uiout, "reason",
10257 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10258 break;
10259
10260 case bp_read_watchpoint:
10261 if (ui_out_is_mi_like_p (uiout))
10262 ui_out_field_string
10263 (uiout, "reason",
10264 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10265 break;
10266
10267 case bp_access_watchpoint:
10268 if (ui_out_is_mi_like_p (uiout))
10269 ui_out_field_string
10270 (uiout, "reason",
10271 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10272 break;
10273 default:
10274 internal_error (__FILE__, __LINE__,
10275 _("Invalid hardware watchpoint type."));
10276 }
10277
10278 mention (b);
10279 ui_out_text (uiout, _("\n\
10280 Check the underlying instruction at PC for the memory\n\
10281 address and value which triggered this watchpoint.\n"));
10282 ui_out_text (uiout, "\n");
10283
10284 /* More than one watchpoint may have been triggered. */
10285 return PRINT_UNKNOWN;
10286 }
10287
10288 /* Implement the "print_one_detail" breakpoint_ops method for
10289 masked hardware watchpoints. */
10290
10291 static void
10292 print_one_detail_masked_watchpoint (const struct breakpoint *b,
10293 struct ui_out *uiout)
10294 {
10295 struct watchpoint *w = (struct watchpoint *) b;
10296
10297 /* Masked watchpoints have only one location. */
10298 gdb_assert (b->loc && b->loc->next == NULL);
10299
10300 ui_out_text (uiout, "\tmask ");
10301 ui_out_field_core_addr (uiout, "mask", b->loc->gdbarch, w->hw_wp_mask);
10302 ui_out_text (uiout, "\n");
10303 }
10304
10305 /* Implement the "print_mention" breakpoint_ops method for
10306 masked hardware watchpoints. */
10307
10308 static void
10309 print_mention_masked_watchpoint (struct breakpoint *b)
10310 {
10311 struct watchpoint *w = (struct watchpoint *) b;
10312 struct ui_out *uiout = current_uiout;
10313 struct cleanup *ui_out_chain;
10314
10315 switch (b->type)
10316 {
10317 case bp_hardware_watchpoint:
10318 ui_out_text (uiout, "Masked hardware watchpoint ");
10319 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10320 break;
10321 case bp_read_watchpoint:
10322 ui_out_text (uiout, "Masked hardware read watchpoint ");
10323 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
10324 break;
10325 case bp_access_watchpoint:
10326 ui_out_text (uiout, "Masked hardware access (read/write) watchpoint ");
10327 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
10328 break;
10329 default:
10330 internal_error (__FILE__, __LINE__,
10331 _("Invalid hardware watchpoint type."));
10332 }
10333
10334 ui_out_field_int (uiout, "number", b->number);
10335 ui_out_text (uiout, ": ");
10336 ui_out_field_string (uiout, "exp", w->exp_string);
10337 do_cleanups (ui_out_chain);
10338 }
10339
10340 /* Implement the "print_recreate" breakpoint_ops method for
10341 masked hardware watchpoints. */
10342
10343 static void
10344 print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10345 {
10346 struct watchpoint *w = (struct watchpoint *) b;
10347 char tmp[40];
10348
10349 switch (b->type)
10350 {
10351 case bp_hardware_watchpoint:
10352 fprintf_unfiltered (fp, "watch");
10353 break;
10354 case bp_read_watchpoint:
10355 fprintf_unfiltered (fp, "rwatch");
10356 break;
10357 case bp_access_watchpoint:
10358 fprintf_unfiltered (fp, "awatch");
10359 break;
10360 default:
10361 internal_error (__FILE__, __LINE__,
10362 _("Invalid hardware watchpoint type."));
10363 }
10364
10365 sprintf_vma (tmp, w->hw_wp_mask);
10366 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
10367 print_recreate_thread (b, fp);
10368 }
10369
10370 /* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10371
10372 static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
10373
10374 /* Tell whether the given watchpoint is a masked hardware watchpoint. */
10375
10376 static int
10377 is_masked_watchpoint (const struct breakpoint *b)
10378 {
10379 return b->ops == &masked_watchpoint_breakpoint_ops;
10380 }
10381
10382 /* accessflag: hw_write: watch write,
10383 hw_read: watch read,
10384 hw_access: watch access (read or write) */
10385 static void
10386 watch_command_1 (char *arg, int accessflag, int from_tty,
10387 int just_location, int internal)
10388 {
10389 volatile struct gdb_exception e;
10390 struct breakpoint *b, *scope_breakpoint = NULL;
10391 struct expression *exp;
10392 struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
10393 struct value *val, *mark, *result;
10394 struct frame_info *frame;
10395 char *exp_start = NULL;
10396 char *exp_end = NULL;
10397 char *tok, *end_tok;
10398 int toklen = -1;
10399 char *cond_start = NULL;
10400 char *cond_end = NULL;
10401 enum bptype bp_type;
10402 int thread = -1;
10403 int pc = 0;
10404 /* Flag to indicate whether we are going to use masks for
10405 the hardware watchpoint. */
10406 int use_mask = 0;
10407 CORE_ADDR mask = 0;
10408 struct watchpoint *w;
10409
10410 /* Make sure that we actually have parameters to parse. */
10411 if (arg != NULL && arg[0] != '\0')
10412 {
10413 char *value_start;
10414
10415 /* Look for "parameter value" pairs at the end
10416 of the arguments string. */
10417 for (tok = arg + strlen (arg) - 1; tok > arg; tok--)
10418 {
10419 /* Skip whitespace at the end of the argument list. */
10420 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10421 tok--;
10422
10423 /* Find the beginning of the last token.
10424 This is the value of the parameter. */
10425 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10426 tok--;
10427 value_start = tok + 1;
10428
10429 /* Skip whitespace. */
10430 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10431 tok--;
10432
10433 end_tok = tok;
10434
10435 /* Find the beginning of the second to last token.
10436 This is the parameter itself. */
10437 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10438 tok--;
10439 tok++;
10440 toklen = end_tok - tok + 1;
10441
10442 if (toklen == 6 && !strncmp (tok, "thread", 6))
10443 {
10444 /* At this point we've found a "thread" token, which means
10445 the user is trying to set a watchpoint that triggers
10446 only in a specific thread. */
10447 char *endp;
10448
10449 if (thread != -1)
10450 error(_("You can specify only one thread."));
10451
10452 /* Extract the thread ID from the next token. */
10453 thread = strtol (value_start, &endp, 0);
10454
10455 /* Check if the user provided a valid numeric value for the
10456 thread ID. */
10457 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
10458 error (_("Invalid thread ID specification %s."), value_start);
10459
10460 /* Check if the thread actually exists. */
10461 if (!valid_thread_id (thread))
10462 error (_("Unknown thread %d."), thread);
10463 }
10464 else if (toklen == 4 && !strncmp (tok, "mask", 4))
10465 {
10466 /* We've found a "mask" token, which means the user wants to
10467 create a hardware watchpoint that is going to have the mask
10468 facility. */
10469 struct value *mask_value, *mark;
10470
10471 if (use_mask)
10472 error(_("You can specify only one mask."));
10473
10474 use_mask = just_location = 1;
10475
10476 mark = value_mark ();
10477 mask_value = parse_to_comma_and_eval (&value_start);
10478 mask = value_as_address (mask_value);
10479 value_free_to_mark (mark);
10480 }
10481 else
10482 /* We didn't recognize what we found. We should stop here. */
10483 break;
10484
10485 /* Truncate the string and get rid of the "parameter value" pair before
10486 the arguments string is parsed by the parse_exp_1 function. */
10487 *tok = '\0';
10488 }
10489 }
10490
10491 /* Parse the rest of the arguments. */
10492 innermost_block = NULL;
10493 exp_start = arg;
10494 exp = parse_exp_1 (&arg, 0, 0);
10495 exp_end = arg;
10496 /* Remove trailing whitespace from the expression before saving it.
10497 This makes the eventual display of the expression string a bit
10498 prettier. */
10499 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
10500 --exp_end;
10501
10502 /* Checking if the expression is not constant. */
10503 if (watchpoint_exp_is_const (exp))
10504 {
10505 int len;
10506
10507 len = exp_end - exp_start;
10508 while (len > 0 && isspace (exp_start[len - 1]))
10509 len--;
10510 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
10511 }
10512
10513 exp_valid_block = innermost_block;
10514 mark = value_mark ();
10515 fetch_subexp_value (exp, &pc, &val, &result, NULL);
10516
10517 if (just_location)
10518 {
10519 int ret;
10520
10521 exp_valid_block = NULL;
10522 val = value_addr (result);
10523 release_value (val);
10524 value_free_to_mark (mark);
10525
10526 if (use_mask)
10527 {
10528 ret = target_masked_watch_num_registers (value_as_address (val),
10529 mask);
10530 if (ret == -1)
10531 error (_("This target does not support masked watchpoints."));
10532 else if (ret == -2)
10533 error (_("Invalid mask or memory region."));
10534 }
10535 }
10536 else if (val != NULL)
10537 release_value (val);
10538
10539 tok = skip_spaces (arg);
10540 end_tok = skip_to_space (tok);
10541
10542 toklen = end_tok - tok;
10543 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
10544 {
10545 struct expression *cond;
10546
10547 innermost_block = NULL;
10548 tok = cond_start = end_tok + 1;
10549 cond = parse_exp_1 (&tok, 0, 0);
10550
10551 /* The watchpoint expression may not be local, but the condition
10552 may still be. E.g.: `watch global if local > 0'. */
10553 cond_exp_valid_block = innermost_block;
10554
10555 xfree (cond);
10556 cond_end = tok;
10557 }
10558 if (*tok)
10559 error (_("Junk at end of command."));
10560
10561 if (accessflag == hw_read)
10562 bp_type = bp_read_watchpoint;
10563 else if (accessflag == hw_access)
10564 bp_type = bp_access_watchpoint;
10565 else
10566 bp_type = bp_hardware_watchpoint;
10567
10568 frame = block_innermost_frame (exp_valid_block);
10569
10570 /* If the expression is "local", then set up a "watchpoint scope"
10571 breakpoint at the point where we've left the scope of the watchpoint
10572 expression. Create the scope breakpoint before the watchpoint, so
10573 that we will encounter it first in bpstat_stop_status. */
10574 if (exp_valid_block && frame)
10575 {
10576 if (frame_id_p (frame_unwind_caller_id (frame)))
10577 {
10578 scope_breakpoint
10579 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
10580 frame_unwind_caller_pc (frame),
10581 bp_watchpoint_scope,
10582 &momentary_breakpoint_ops);
10583
10584 scope_breakpoint->enable_state = bp_enabled;
10585
10586 /* Automatically delete the breakpoint when it hits. */
10587 scope_breakpoint->disposition = disp_del;
10588
10589 /* Only break in the proper frame (help with recursion). */
10590 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
10591
10592 /* Set the address at which we will stop. */
10593 scope_breakpoint->loc->gdbarch
10594 = frame_unwind_caller_arch (frame);
10595 scope_breakpoint->loc->requested_address
10596 = frame_unwind_caller_pc (frame);
10597 scope_breakpoint->loc->address
10598 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
10599 scope_breakpoint->loc->requested_address,
10600 scope_breakpoint->type);
10601 }
10602 }
10603
10604 /* Now set up the breakpoint. */
10605
10606 w = XCNEW (struct watchpoint);
10607 b = &w->base;
10608 if (use_mask)
10609 init_raw_breakpoint_without_location (b, NULL, bp_type,
10610 &masked_watchpoint_breakpoint_ops);
10611 else
10612 init_raw_breakpoint_without_location (b, NULL, bp_type,
10613 &watchpoint_breakpoint_ops);
10614 b->thread = thread;
10615 b->disposition = disp_donttouch;
10616 b->pspace = current_program_space;
10617 w->exp = exp;
10618 w->exp_valid_block = exp_valid_block;
10619 w->cond_exp_valid_block = cond_exp_valid_block;
10620 if (just_location)
10621 {
10622 struct type *t = value_type (val);
10623 CORE_ADDR addr = value_as_address (val);
10624 char *name;
10625
10626 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
10627 name = type_to_string (t);
10628
10629 w->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
10630 core_addr_to_string (addr));
10631 xfree (name);
10632
10633 w->exp_string = xstrprintf ("-location %.*s",
10634 (int) (exp_end - exp_start), exp_start);
10635
10636 /* The above expression is in C. */
10637 b->language = language_c;
10638 }
10639 else
10640 w->exp_string = savestring (exp_start, exp_end - exp_start);
10641
10642 if (use_mask)
10643 {
10644 w->hw_wp_mask = mask;
10645 }
10646 else
10647 {
10648 w->val = val;
10649 w->val_valid = 1;
10650 }
10651
10652 if (cond_start)
10653 b->cond_string = savestring (cond_start, cond_end - cond_start);
10654 else
10655 b->cond_string = 0;
10656
10657 if (frame)
10658 {
10659 w->watchpoint_frame = get_frame_id (frame);
10660 w->watchpoint_thread = inferior_ptid;
10661 }
10662 else
10663 {
10664 w->watchpoint_frame = null_frame_id;
10665 w->watchpoint_thread = null_ptid;
10666 }
10667
10668 if (scope_breakpoint != NULL)
10669 {
10670 /* The scope breakpoint is related to the watchpoint. We will
10671 need to act on them together. */
10672 b->related_breakpoint = scope_breakpoint;
10673 scope_breakpoint->related_breakpoint = b;
10674 }
10675
10676 if (!just_location)
10677 value_free_to_mark (mark);
10678
10679 TRY_CATCH (e, RETURN_MASK_ALL)
10680 {
10681 /* Finally update the new watchpoint. This creates the locations
10682 that should be inserted. */
10683 update_watchpoint (w, 1);
10684 }
10685 if (e.reason < 0)
10686 {
10687 delete_breakpoint (b);
10688 throw_exception (e);
10689 }
10690
10691 install_breakpoint (internal, b, 1);
10692 }
10693
10694 /* Return count of debug registers needed to watch the given expression.
10695 If the watchpoint cannot be handled in hardware return zero. */
10696
10697 static int
10698 can_use_hardware_watchpoint (struct value *v)
10699 {
10700 int found_memory_cnt = 0;
10701 struct value *head = v;
10702
10703 /* Did the user specifically forbid us to use hardware watchpoints? */
10704 if (!can_use_hw_watchpoints)
10705 return 0;
10706
10707 /* Make sure that the value of the expression depends only upon
10708 memory contents, and values computed from them within GDB. If we
10709 find any register references or function calls, we can't use a
10710 hardware watchpoint.
10711
10712 The idea here is that evaluating an expression generates a series
10713 of values, one holding the value of every subexpression. (The
10714 expression a*b+c has five subexpressions: a, b, a*b, c, and
10715 a*b+c.) GDB's values hold almost enough information to establish
10716 the criteria given above --- they identify memory lvalues,
10717 register lvalues, computed values, etcetera. So we can evaluate
10718 the expression, and then scan the chain of values that leaves
10719 behind to decide whether we can detect any possible change to the
10720 expression's final value using only hardware watchpoints.
10721
10722 However, I don't think that the values returned by inferior
10723 function calls are special in any way. So this function may not
10724 notice that an expression involving an inferior function call
10725 can't be watched with hardware watchpoints. FIXME. */
10726 for (; v; v = value_next (v))
10727 {
10728 if (VALUE_LVAL (v) == lval_memory)
10729 {
10730 if (v != head && value_lazy (v))
10731 /* A lazy memory lvalue in the chain is one that GDB never
10732 needed to fetch; we either just used its address (e.g.,
10733 `a' in `a.b') or we never needed it at all (e.g., `a'
10734 in `a,b'). This doesn't apply to HEAD; if that is
10735 lazy then it was not readable, but watch it anyway. */
10736 ;
10737 else
10738 {
10739 /* Ahh, memory we actually used! Check if we can cover
10740 it with hardware watchpoints. */
10741 struct type *vtype = check_typedef (value_type (v));
10742
10743 /* We only watch structs and arrays if user asked for it
10744 explicitly, never if they just happen to appear in a
10745 middle of some value chain. */
10746 if (v == head
10747 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
10748 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
10749 {
10750 CORE_ADDR vaddr = value_address (v);
10751 int len;
10752 int num_regs;
10753
10754 len = (target_exact_watchpoints
10755 && is_scalar_type_recursive (vtype))?
10756 1 : TYPE_LENGTH (value_type (v));
10757
10758 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
10759 if (!num_regs)
10760 return 0;
10761 else
10762 found_memory_cnt += num_regs;
10763 }
10764 }
10765 }
10766 else if (VALUE_LVAL (v) != not_lval
10767 && deprecated_value_modifiable (v) == 0)
10768 return 0; /* These are values from the history (e.g., $1). */
10769 else if (VALUE_LVAL (v) == lval_register)
10770 return 0; /* Cannot watch a register with a HW watchpoint. */
10771 }
10772
10773 /* The expression itself looks suitable for using a hardware
10774 watchpoint, but give the target machine a chance to reject it. */
10775 return found_memory_cnt;
10776 }
10777
10778 void
10779 watch_command_wrapper (char *arg, int from_tty, int internal)
10780 {
10781 watch_command_1 (arg, hw_write, from_tty, 0, internal);
10782 }
10783
10784 /* A helper function that looks for an argument at the start of a
10785 string. The argument must also either be at the end of the string,
10786 or be followed by whitespace. Returns 1 if it finds the argument,
10787 0 otherwise. If the argument is found, it updates *STR. */
10788
10789 static int
10790 check_for_argument (char **str, char *arg, int arg_len)
10791 {
10792 if (strncmp (*str, arg, arg_len) == 0
10793 && ((*str)[arg_len] == '\0' || isspace ((*str)[arg_len])))
10794 {
10795 *str += arg_len;
10796 return 1;
10797 }
10798 return 0;
10799 }
10800
10801 /* A helper function that looks for the "-location" argument and then
10802 calls watch_command_1. */
10803
10804 static void
10805 watch_maybe_just_location (char *arg, int accessflag, int from_tty)
10806 {
10807 int just_location = 0;
10808
10809 if (arg
10810 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
10811 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
10812 {
10813 arg = skip_spaces (arg);
10814 just_location = 1;
10815 }
10816
10817 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
10818 }
10819
10820 static void
10821 watch_command (char *arg, int from_tty)
10822 {
10823 watch_maybe_just_location (arg, hw_write, from_tty);
10824 }
10825
10826 void
10827 rwatch_command_wrapper (char *arg, int from_tty, int internal)
10828 {
10829 watch_command_1 (arg, hw_read, from_tty, 0, internal);
10830 }
10831
10832 static void
10833 rwatch_command (char *arg, int from_tty)
10834 {
10835 watch_maybe_just_location (arg, hw_read, from_tty);
10836 }
10837
10838 void
10839 awatch_command_wrapper (char *arg, int from_tty, int internal)
10840 {
10841 watch_command_1 (arg, hw_access, from_tty, 0, internal);
10842 }
10843
10844 static void
10845 awatch_command (char *arg, int from_tty)
10846 {
10847 watch_maybe_just_location (arg, hw_access, from_tty);
10848 }
10849 \f
10850
10851 /* Helper routines for the until_command routine in infcmd.c. Here
10852 because it uses the mechanisms of breakpoints. */
10853
10854 struct until_break_command_continuation_args
10855 {
10856 struct breakpoint *breakpoint;
10857 struct breakpoint *breakpoint2;
10858 int thread_num;
10859 };
10860
10861 /* This function is called by fetch_inferior_event via the
10862 cmd_continuation pointer, to complete the until command. It takes
10863 care of cleaning up the temporary breakpoints set up by the until
10864 command. */
10865 static void
10866 until_break_command_continuation (void *arg, int err)
10867 {
10868 struct until_break_command_continuation_args *a = arg;
10869
10870 delete_breakpoint (a->breakpoint);
10871 if (a->breakpoint2)
10872 delete_breakpoint (a->breakpoint2);
10873 delete_longjmp_breakpoint (a->thread_num);
10874 }
10875
10876 void
10877 until_break_command (char *arg, int from_tty, int anywhere)
10878 {
10879 struct symtabs_and_lines sals;
10880 struct symtab_and_line sal;
10881 struct frame_info *frame;
10882 struct gdbarch *frame_gdbarch;
10883 struct frame_id stack_frame_id;
10884 struct frame_id caller_frame_id;
10885 struct breakpoint *breakpoint;
10886 struct breakpoint *breakpoint2 = NULL;
10887 struct cleanup *old_chain;
10888 int thread;
10889 struct thread_info *tp;
10890
10891 clear_proceed_status ();
10892
10893 /* Set a breakpoint where the user wants it and at return from
10894 this function. */
10895
10896 if (last_displayed_sal_is_valid ())
10897 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
10898 get_last_displayed_symtab (),
10899 get_last_displayed_line ());
10900 else
10901 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
10902 (struct symtab *) NULL, 0);
10903
10904 if (sals.nelts != 1)
10905 error (_("Couldn't get information on specified line."));
10906
10907 sal = sals.sals[0];
10908 xfree (sals.sals); /* malloc'd, so freed. */
10909
10910 if (*arg)
10911 error (_("Junk at end of arguments."));
10912
10913 resolve_sal_pc (&sal);
10914
10915 tp = inferior_thread ();
10916 thread = tp->num;
10917
10918 old_chain = make_cleanup (null_cleanup, NULL);
10919
10920 /* Note linespec handling above invalidates the frame chain.
10921 Installing a breakpoint also invalidates the frame chain (as it
10922 may need to switch threads), so do any frame handling before
10923 that. */
10924
10925 frame = get_selected_frame (NULL);
10926 frame_gdbarch = get_frame_arch (frame);
10927 stack_frame_id = get_stack_frame_id (frame);
10928 caller_frame_id = frame_unwind_caller_id (frame);
10929
10930 /* Keep within the current frame, or in frames called by the current
10931 one. */
10932
10933 if (frame_id_p (caller_frame_id))
10934 {
10935 struct symtab_and_line sal2;
10936
10937 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
10938 sal2.pc = frame_unwind_caller_pc (frame);
10939 breakpoint2 = set_momentary_breakpoint (frame_unwind_caller_arch (frame),
10940 sal2,
10941 caller_frame_id,
10942 bp_until);
10943 make_cleanup_delete_breakpoint (breakpoint2);
10944
10945 set_longjmp_breakpoint (tp, caller_frame_id);
10946 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
10947 }
10948
10949 /* set_momentary_breakpoint could invalidate FRAME. */
10950 frame = NULL;
10951
10952 if (anywhere)
10953 /* If the user told us to continue until a specified location,
10954 we don't specify a frame at which we need to stop. */
10955 breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
10956 null_frame_id, bp_until);
10957 else
10958 /* Otherwise, specify the selected frame, because we want to stop
10959 only at the very same frame. */
10960 breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
10961 stack_frame_id, bp_until);
10962 make_cleanup_delete_breakpoint (breakpoint);
10963
10964 proceed (-1, GDB_SIGNAL_DEFAULT, 0);
10965
10966 /* If we are running asynchronously, and proceed call above has
10967 actually managed to start the target, arrange for breakpoints to
10968 be deleted when the target stops. Otherwise, we're already
10969 stopped and delete breakpoints via cleanup chain. */
10970
10971 if (target_can_async_p () && is_running (inferior_ptid))
10972 {
10973 struct until_break_command_continuation_args *args;
10974 args = xmalloc (sizeof (*args));
10975
10976 args->breakpoint = breakpoint;
10977 args->breakpoint2 = breakpoint2;
10978 args->thread_num = thread;
10979
10980 discard_cleanups (old_chain);
10981 add_continuation (inferior_thread (),
10982 until_break_command_continuation, args,
10983 xfree);
10984 }
10985 else
10986 do_cleanups (old_chain);
10987 }
10988
10989 /* This function attempts to parse an optional "if <cond>" clause
10990 from the arg string. If one is not found, it returns NULL.
10991
10992 Else, it returns a pointer to the condition string. (It does not
10993 attempt to evaluate the string against a particular block.) And,
10994 it updates arg to point to the first character following the parsed
10995 if clause in the arg string. */
10996
10997 static char *
10998 ep_parse_optional_if_clause (char **arg)
10999 {
11000 char *cond_string;
11001
11002 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
11003 return NULL;
11004
11005 /* Skip the "if" keyword. */
11006 (*arg) += 2;
11007
11008 /* Skip any extra leading whitespace, and record the start of the
11009 condition string. */
11010 *arg = skip_spaces (*arg);
11011 cond_string = *arg;
11012
11013 /* Assume that the condition occupies the remainder of the arg
11014 string. */
11015 (*arg) += strlen (cond_string);
11016
11017 return cond_string;
11018 }
11019
11020 /* Commands to deal with catching events, such as signals, exceptions,
11021 process start/exit, etc. */
11022
11023 typedef enum
11024 {
11025 catch_fork_temporary, catch_vfork_temporary,
11026 catch_fork_permanent, catch_vfork_permanent
11027 }
11028 catch_fork_kind;
11029
11030 static void
11031 catch_fork_command_1 (char *arg, int from_tty,
11032 struct cmd_list_element *command)
11033 {
11034 struct gdbarch *gdbarch = get_current_arch ();
11035 char *cond_string = NULL;
11036 catch_fork_kind fork_kind;
11037 int tempflag;
11038
11039 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11040 tempflag = (fork_kind == catch_fork_temporary
11041 || fork_kind == catch_vfork_temporary);
11042
11043 if (!arg)
11044 arg = "";
11045 arg = skip_spaces (arg);
11046
11047 /* The allowed syntax is:
11048 catch [v]fork
11049 catch [v]fork if <cond>
11050
11051 First, check if there's an if clause. */
11052 cond_string = ep_parse_optional_if_clause (&arg);
11053
11054 if ((*arg != '\0') && !isspace (*arg))
11055 error (_("Junk at end of arguments."));
11056
11057 /* If this target supports it, create a fork or vfork catchpoint
11058 and enable reporting of such events. */
11059 switch (fork_kind)
11060 {
11061 case catch_fork_temporary:
11062 case catch_fork_permanent:
11063 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11064 &catch_fork_breakpoint_ops);
11065 break;
11066 case catch_vfork_temporary:
11067 case catch_vfork_permanent:
11068 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11069 &catch_vfork_breakpoint_ops);
11070 break;
11071 default:
11072 error (_("unsupported or unknown fork kind; cannot catch it"));
11073 break;
11074 }
11075 }
11076
11077 static void
11078 catch_exec_command_1 (char *arg, int from_tty,
11079 struct cmd_list_element *command)
11080 {
11081 struct exec_catchpoint *c;
11082 struct gdbarch *gdbarch = get_current_arch ();
11083 int tempflag;
11084 char *cond_string = NULL;
11085
11086 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11087
11088 if (!arg)
11089 arg = "";
11090 arg = skip_spaces (arg);
11091
11092 /* The allowed syntax is:
11093 catch exec
11094 catch exec if <cond>
11095
11096 First, check if there's an if clause. */
11097 cond_string = ep_parse_optional_if_clause (&arg);
11098
11099 if ((*arg != '\0') && !isspace (*arg))
11100 error (_("Junk at end of arguments."));
11101
11102 c = XNEW (struct exec_catchpoint);
11103 init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
11104 &catch_exec_breakpoint_ops);
11105 c->exec_pathname = NULL;
11106
11107 install_breakpoint (0, &c->base, 1);
11108 }
11109
11110 static enum print_stop_action
11111 print_it_exception_catchpoint (bpstat bs)
11112 {
11113 struct ui_out *uiout = current_uiout;
11114 struct breakpoint *b = bs->breakpoint_at;
11115 int bp_temp, bp_throw;
11116
11117 annotate_catchpoint (b->number);
11118
11119 bp_throw = strstr (b->addr_string, "throw") != NULL;
11120 if (b->loc->address != b->loc->requested_address)
11121 breakpoint_adjustment_warning (b->loc->requested_address,
11122 b->loc->address,
11123 b->number, 1);
11124 bp_temp = b->disposition == disp_del;
11125 ui_out_text (uiout,
11126 bp_temp ? "Temporary catchpoint "
11127 : "Catchpoint ");
11128 if (!ui_out_is_mi_like_p (uiout))
11129 ui_out_field_int (uiout, "bkptno", b->number);
11130 ui_out_text (uiout,
11131 bp_throw ? " (exception thrown), "
11132 : " (exception caught), ");
11133 if (ui_out_is_mi_like_p (uiout))
11134 {
11135 ui_out_field_string (uiout, "reason",
11136 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
11137 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
11138 ui_out_field_int (uiout, "bkptno", b->number);
11139 }
11140 return PRINT_SRC_AND_LOC;
11141 }
11142
11143 static void
11144 print_one_exception_catchpoint (struct breakpoint *b,
11145 struct bp_location **last_loc)
11146 {
11147 struct value_print_options opts;
11148 struct ui_out *uiout = current_uiout;
11149
11150 get_user_print_options (&opts);
11151 if (opts.addressprint)
11152 {
11153 annotate_field (4);
11154 if (b->loc == NULL || b->loc->shlib_disabled)
11155 ui_out_field_string (uiout, "addr", "<PENDING>");
11156 else
11157 ui_out_field_core_addr (uiout, "addr",
11158 b->loc->gdbarch, b->loc->address);
11159 }
11160 annotate_field (5);
11161 if (b->loc)
11162 *last_loc = b->loc;
11163 if (strstr (b->addr_string, "throw") != NULL)
11164 ui_out_field_string (uiout, "what", "exception throw");
11165 else
11166 ui_out_field_string (uiout, "what", "exception catch");
11167 }
11168
11169 static void
11170 print_mention_exception_catchpoint (struct breakpoint *b)
11171 {
11172 struct ui_out *uiout = current_uiout;
11173 int bp_temp;
11174 int bp_throw;
11175
11176 bp_temp = b->disposition == disp_del;
11177 bp_throw = strstr (b->addr_string, "throw") != NULL;
11178 ui_out_text (uiout, bp_temp ? _("Temporary catchpoint ")
11179 : _("Catchpoint "));
11180 ui_out_field_int (uiout, "bkptno", b->number);
11181 ui_out_text (uiout, bp_throw ? _(" (throw)")
11182 : _(" (catch)"));
11183 }
11184
11185 /* Implement the "print_recreate" breakpoint_ops method for throw and
11186 catch catchpoints. */
11187
11188 static void
11189 print_recreate_exception_catchpoint (struct breakpoint *b,
11190 struct ui_file *fp)
11191 {
11192 int bp_temp;
11193 int bp_throw;
11194
11195 bp_temp = b->disposition == disp_del;
11196 bp_throw = strstr (b->addr_string, "throw") != NULL;
11197 fprintf_unfiltered (fp, bp_temp ? "tcatch " : "catch ");
11198 fprintf_unfiltered (fp, bp_throw ? "throw" : "catch");
11199 print_recreate_thread (b, fp);
11200 }
11201
11202 static struct breakpoint_ops gnu_v3_exception_catchpoint_ops;
11203
11204 static int
11205 handle_gnu_v3_exceptions (int tempflag, char *cond_string,
11206 enum exception_event_kind ex_event, int from_tty)
11207 {
11208 char *trigger_func_name;
11209
11210 if (ex_event == EX_EVENT_CATCH)
11211 trigger_func_name = "__cxa_begin_catch";
11212 else
11213 trigger_func_name = "__cxa_throw";
11214
11215 create_breakpoint (get_current_arch (),
11216 trigger_func_name, cond_string, -1, NULL,
11217 0 /* condition and thread are valid. */,
11218 tempflag, bp_breakpoint,
11219 0,
11220 AUTO_BOOLEAN_TRUE /* pending */,
11221 &gnu_v3_exception_catchpoint_ops, from_tty,
11222 1 /* enabled */,
11223 0 /* internal */,
11224 0);
11225
11226 return 1;
11227 }
11228
11229 /* Deal with "catch catch" and "catch throw" commands. */
11230
11231 static void
11232 catch_exception_command_1 (enum exception_event_kind ex_event, char *arg,
11233 int tempflag, int from_tty)
11234 {
11235 char *cond_string = NULL;
11236
11237 if (!arg)
11238 arg = "";
11239 arg = skip_spaces (arg);
11240
11241 cond_string = ep_parse_optional_if_clause (&arg);
11242
11243 if ((*arg != '\0') && !isspace (*arg))
11244 error (_("Junk at end of arguments."));
11245
11246 if (ex_event != EX_EVENT_THROW
11247 && ex_event != EX_EVENT_CATCH)
11248 error (_("Unsupported or unknown exception event; cannot catch it"));
11249
11250 if (handle_gnu_v3_exceptions (tempflag, cond_string, ex_event, from_tty))
11251 return;
11252
11253 warning (_("Unsupported with this platform/compiler combination."));
11254 }
11255
11256 /* Implementation of "catch catch" command. */
11257
11258 static void
11259 catch_catch_command (char *arg, int from_tty, struct cmd_list_element *command)
11260 {
11261 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11262
11263 catch_exception_command_1 (EX_EVENT_CATCH, arg, tempflag, from_tty);
11264 }
11265
11266 /* Implementation of "catch throw" command. */
11267
11268 static void
11269 catch_throw_command (char *arg, int from_tty, struct cmd_list_element *command)
11270 {
11271 int tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11272
11273 catch_exception_command_1 (EX_EVENT_THROW, arg, tempflag, from_tty);
11274 }
11275
11276 void
11277 init_ada_exception_breakpoint (struct breakpoint *b,
11278 struct gdbarch *gdbarch,
11279 struct symtab_and_line sal,
11280 char *addr_string,
11281 const struct breakpoint_ops *ops,
11282 int tempflag,
11283 int from_tty)
11284 {
11285 if (from_tty)
11286 {
11287 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11288 if (!loc_gdbarch)
11289 loc_gdbarch = gdbarch;
11290
11291 describe_other_breakpoints (loc_gdbarch,
11292 sal.pspace, sal.pc, sal.section, -1);
11293 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11294 version for exception catchpoints, because two catchpoints
11295 used for different exception names will use the same address.
11296 In this case, a "breakpoint ... also set at..." warning is
11297 unproductive. Besides, the warning phrasing is also a bit
11298 inappropriate, we should use the word catchpoint, and tell
11299 the user what type of catchpoint it is. The above is good
11300 enough for now, though. */
11301 }
11302
11303 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
11304
11305 b->enable_state = bp_enabled;
11306 b->disposition = tempflag ? disp_del : disp_donttouch;
11307 b->addr_string = addr_string;
11308 b->language = language_ada;
11309 }
11310
11311 /* Splits the argument using space as delimiter. Returns an xmalloc'd
11312 filter list, or NULL if no filtering is required. */
11313 static VEC(int) *
11314 catch_syscall_split_args (char *arg)
11315 {
11316 VEC(int) *result = NULL;
11317 struct cleanup *cleanup = make_cleanup (VEC_cleanup (int), &result);
11318
11319 while (*arg != '\0')
11320 {
11321 int i, syscall_number;
11322 char *endptr;
11323 char cur_name[128];
11324 struct syscall s;
11325
11326 /* Skip whitespace. */
11327 while (isspace (*arg))
11328 arg++;
11329
11330 for (i = 0; i < 127 && arg[i] && !isspace (arg[i]); ++i)
11331 cur_name[i] = arg[i];
11332 cur_name[i] = '\0';
11333 arg += i;
11334
11335 /* Check if the user provided a syscall name or a number. */
11336 syscall_number = (int) strtol (cur_name, &endptr, 0);
11337 if (*endptr == '\0')
11338 get_syscall_by_number (syscall_number, &s);
11339 else
11340 {
11341 /* We have a name. Let's check if it's valid and convert it
11342 to a number. */
11343 get_syscall_by_name (cur_name, &s);
11344
11345 if (s.number == UNKNOWN_SYSCALL)
11346 /* Here we have to issue an error instead of a warning,
11347 because GDB cannot do anything useful if there's no
11348 syscall number to be caught. */
11349 error (_("Unknown syscall name '%s'."), cur_name);
11350 }
11351
11352 /* Ok, it's valid. */
11353 VEC_safe_push (int, result, s.number);
11354 }
11355
11356 discard_cleanups (cleanup);
11357 return result;
11358 }
11359
11360 /* Implement the "catch syscall" command. */
11361
11362 static void
11363 catch_syscall_command_1 (char *arg, int from_tty,
11364 struct cmd_list_element *command)
11365 {
11366 int tempflag;
11367 VEC(int) *filter;
11368 struct syscall s;
11369 struct gdbarch *gdbarch = get_current_arch ();
11370
11371 /* Checking if the feature if supported. */
11372 if (gdbarch_get_syscall_number_p (gdbarch) == 0)
11373 error (_("The feature 'catch syscall' is not supported on \
11374 this architecture yet."));
11375
11376 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11377
11378 arg = skip_spaces (arg);
11379
11380 /* We need to do this first "dummy" translation in order
11381 to get the syscall XML file loaded or, most important,
11382 to display a warning to the user if there's no XML file
11383 for his/her architecture. */
11384 get_syscall_by_number (0, &s);
11385
11386 /* The allowed syntax is:
11387 catch syscall
11388 catch syscall <name | number> [<name | number> ... <name | number>]
11389
11390 Let's check if there's a syscall name. */
11391
11392 if (arg != NULL)
11393 filter = catch_syscall_split_args (arg);
11394 else
11395 filter = NULL;
11396
11397 create_syscall_event_catchpoint (tempflag, filter,
11398 &catch_syscall_breakpoint_ops);
11399 }
11400
11401 static void
11402 catch_command (char *arg, int from_tty)
11403 {
11404 error (_("Catch requires an event name."));
11405 }
11406 \f
11407
11408 static void
11409 tcatch_command (char *arg, int from_tty)
11410 {
11411 error (_("Catch requires an event name."));
11412 }
11413
11414 /* A qsort comparison function that sorts breakpoints in order. */
11415
11416 static int
11417 compare_breakpoints (const void *a, const void *b)
11418 {
11419 const breakpoint_p *ba = a;
11420 uintptr_t ua = (uintptr_t) *ba;
11421 const breakpoint_p *bb = b;
11422 uintptr_t ub = (uintptr_t) *bb;
11423
11424 if ((*ba)->number < (*bb)->number)
11425 return -1;
11426 else if ((*ba)->number > (*bb)->number)
11427 return 1;
11428
11429 /* Now sort by address, in case we see, e..g, two breakpoints with
11430 the number 0. */
11431 if (ua < ub)
11432 return -1;
11433 return ub > ub ? 1 : 0;
11434 }
11435
11436 /* Delete breakpoints by address or line. */
11437
11438 static void
11439 clear_command (char *arg, int from_tty)
11440 {
11441 struct breakpoint *b, *prev;
11442 VEC(breakpoint_p) *found = 0;
11443 int ix;
11444 int default_match;
11445 struct symtabs_and_lines sals;
11446 struct symtab_and_line sal;
11447 int i;
11448 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
11449
11450 if (arg)
11451 {
11452 sals = decode_line_spec (arg, (DECODE_LINE_FUNFIRSTLINE
11453 | DECODE_LINE_LIST_MODE));
11454 default_match = 0;
11455 }
11456 else
11457 {
11458 sals.sals = (struct symtab_and_line *)
11459 xmalloc (sizeof (struct symtab_and_line));
11460 make_cleanup (xfree, sals.sals);
11461 init_sal (&sal); /* Initialize to zeroes. */
11462
11463 /* Set sal's line, symtab, pc, and pspace to the values
11464 corresponding to the last call to print_frame_info. If the
11465 codepoint is not valid, this will set all the fields to 0. */
11466 get_last_displayed_sal (&sal);
11467 if (sal.symtab == 0)
11468 error (_("No source file specified."));
11469
11470 sals.sals[0] = sal;
11471 sals.nelts = 1;
11472
11473 default_match = 1;
11474 }
11475
11476 /* We don't call resolve_sal_pc here. That's not as bad as it
11477 seems, because all existing breakpoints typically have both
11478 file/line and pc set. So, if clear is given file/line, we can
11479 match this to existing breakpoint without obtaining pc at all.
11480
11481 We only support clearing given the address explicitly
11482 present in breakpoint table. Say, we've set breakpoint
11483 at file:line. There were several PC values for that file:line,
11484 due to optimization, all in one block.
11485
11486 We've picked one PC value. If "clear" is issued with another
11487 PC corresponding to the same file:line, the breakpoint won't
11488 be cleared. We probably can still clear the breakpoint, but
11489 since the other PC value is never presented to user, user
11490 can only find it by guessing, and it does not seem important
11491 to support that. */
11492
11493 /* For each line spec given, delete bps which correspond to it. Do
11494 it in two passes, solely to preserve the current behavior that
11495 from_tty is forced true if we delete more than one
11496 breakpoint. */
11497
11498 found = NULL;
11499 make_cleanup (VEC_cleanup (breakpoint_p), &found);
11500 for (i = 0; i < sals.nelts; i++)
11501 {
11502 int is_abs, sal_name_len;
11503
11504 /* If exact pc given, clear bpts at that pc.
11505 If line given (pc == 0), clear all bpts on specified line.
11506 If defaulting, clear all bpts on default line
11507 or at default pc.
11508
11509 defaulting sal.pc != 0 tests to do
11510
11511 0 1 pc
11512 1 1 pc _and_ line
11513 0 0 line
11514 1 0 <can't happen> */
11515
11516 sal = sals.sals[i];
11517 is_abs = sal.symtab == NULL ? 1 : IS_ABSOLUTE_PATH (sal.symtab->filename);
11518 sal_name_len = is_abs ? 0 : strlen (sal.symtab->filename);
11519
11520 /* Find all matching breakpoints and add them to 'found'. */
11521 ALL_BREAKPOINTS (b)
11522 {
11523 int match = 0;
11524 /* Are we going to delete b? */
11525 if (b->type != bp_none && !is_watchpoint (b))
11526 {
11527 struct bp_location *loc = b->loc;
11528 for (; loc; loc = loc->next)
11529 {
11530 /* If the user specified file:line, don't allow a PC
11531 match. This matches historical gdb behavior. */
11532 int pc_match = (!sal.explicit_line
11533 && sal.pc
11534 && (loc->pspace == sal.pspace)
11535 && (loc->address == sal.pc)
11536 && (!section_is_overlay (loc->section)
11537 || loc->section == sal.section));
11538 int line_match = 0;
11539
11540 if ((default_match || sal.explicit_line)
11541 && loc->source_file != NULL
11542 && sal.symtab != NULL
11543 && sal.pspace == loc->pspace
11544 && loc->line_number == sal.line)
11545 {
11546 if (filename_cmp (loc->source_file,
11547 sal.symtab->filename) == 0)
11548 line_match = 1;
11549 else if (!IS_ABSOLUTE_PATH (sal.symtab->filename)
11550 && compare_filenames_for_search (loc->source_file,
11551 sal.symtab->filename,
11552 sal_name_len))
11553 line_match = 1;
11554 }
11555
11556 if (pc_match || line_match)
11557 {
11558 match = 1;
11559 break;
11560 }
11561 }
11562 }
11563
11564 if (match)
11565 VEC_safe_push(breakpoint_p, found, b);
11566 }
11567 }
11568
11569 /* Now go thru the 'found' chain and delete them. */
11570 if (VEC_empty(breakpoint_p, found))
11571 {
11572 if (arg)
11573 error (_("No breakpoint at %s."), arg);
11574 else
11575 error (_("No breakpoint at this line."));
11576 }
11577
11578 /* Remove duplicates from the vec. */
11579 qsort (VEC_address (breakpoint_p, found),
11580 VEC_length (breakpoint_p, found),
11581 sizeof (breakpoint_p),
11582 compare_breakpoints);
11583 prev = VEC_index (breakpoint_p, found, 0);
11584 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
11585 {
11586 if (b == prev)
11587 {
11588 VEC_ordered_remove (breakpoint_p, found, ix);
11589 --ix;
11590 }
11591 }
11592
11593 if (VEC_length(breakpoint_p, found) > 1)
11594 from_tty = 1; /* Always report if deleted more than one. */
11595 if (from_tty)
11596 {
11597 if (VEC_length(breakpoint_p, found) == 1)
11598 printf_unfiltered (_("Deleted breakpoint "));
11599 else
11600 printf_unfiltered (_("Deleted breakpoints "));
11601 }
11602 breakpoints_changed ();
11603
11604 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
11605 {
11606 if (from_tty)
11607 printf_unfiltered ("%d ", b->number);
11608 delete_breakpoint (b);
11609 }
11610 if (from_tty)
11611 putchar_unfiltered ('\n');
11612
11613 do_cleanups (cleanups);
11614 }
11615 \f
11616 /* Delete breakpoint in BS if they are `delete' breakpoints and
11617 all breakpoints that are marked for deletion, whether hit or not.
11618 This is called after any breakpoint is hit, or after errors. */
11619
11620 void
11621 breakpoint_auto_delete (bpstat bs)
11622 {
11623 struct breakpoint *b, *b_tmp;
11624
11625 for (; bs; bs = bs->next)
11626 if (bs->breakpoint_at
11627 && bs->breakpoint_at->disposition == disp_del
11628 && bs->stop)
11629 delete_breakpoint (bs->breakpoint_at);
11630
11631 ALL_BREAKPOINTS_SAFE (b, b_tmp)
11632 {
11633 if (b->disposition == disp_del_at_next_stop)
11634 delete_breakpoint (b);
11635 }
11636 }
11637
11638 /* A comparison function for bp_location AP and BP being interfaced to
11639 qsort. Sort elements primarily by their ADDRESS (no matter what
11640 does breakpoint_address_is_meaningful say for its OWNER),
11641 secondarily by ordering first bp_permanent OWNERed elements and
11642 terciarily just ensuring the array is sorted stable way despite
11643 qsort being an unstable algorithm. */
11644
11645 static int
11646 bp_location_compare (const void *ap, const void *bp)
11647 {
11648 struct bp_location *a = *(void **) ap;
11649 struct bp_location *b = *(void **) bp;
11650 /* A and B come from existing breakpoints having non-NULL OWNER. */
11651 int a_perm = a->owner->enable_state == bp_permanent;
11652 int b_perm = b->owner->enable_state == bp_permanent;
11653
11654 if (a->address != b->address)
11655 return (a->address > b->address) - (a->address < b->address);
11656
11657 /* Sort locations at the same address by their pspace number, keeping
11658 locations of the same inferior (in a multi-inferior environment)
11659 grouped. */
11660
11661 if (a->pspace->num != b->pspace->num)
11662 return ((a->pspace->num > b->pspace->num)
11663 - (a->pspace->num < b->pspace->num));
11664
11665 /* Sort permanent breakpoints first. */
11666 if (a_perm != b_perm)
11667 return (a_perm < b_perm) - (a_perm > b_perm);
11668
11669 /* Make the internal GDB representation stable across GDB runs
11670 where A and B memory inside GDB can differ. Breakpoint locations of
11671 the same type at the same address can be sorted in arbitrary order. */
11672
11673 if (a->owner->number != b->owner->number)
11674 return ((a->owner->number > b->owner->number)
11675 - (a->owner->number < b->owner->number));
11676
11677 return (a > b) - (a < b);
11678 }
11679
11680 /* Set bp_location_placed_address_before_address_max and
11681 bp_location_shadow_len_after_address_max according to the current
11682 content of the bp_location array. */
11683
11684 static void
11685 bp_location_target_extensions_update (void)
11686 {
11687 struct bp_location *bl, **blp_tmp;
11688
11689 bp_location_placed_address_before_address_max = 0;
11690 bp_location_shadow_len_after_address_max = 0;
11691
11692 ALL_BP_LOCATIONS (bl, blp_tmp)
11693 {
11694 CORE_ADDR start, end, addr;
11695
11696 if (!bp_location_has_shadow (bl))
11697 continue;
11698
11699 start = bl->target_info.placed_address;
11700 end = start + bl->target_info.shadow_len;
11701
11702 gdb_assert (bl->address >= start);
11703 addr = bl->address - start;
11704 if (addr > bp_location_placed_address_before_address_max)
11705 bp_location_placed_address_before_address_max = addr;
11706
11707 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
11708
11709 gdb_assert (bl->address < end);
11710 addr = end - bl->address;
11711 if (addr > bp_location_shadow_len_after_address_max)
11712 bp_location_shadow_len_after_address_max = addr;
11713 }
11714 }
11715
11716 /* Download tracepoint locations if they haven't been. */
11717
11718 static void
11719 download_tracepoint_locations (void)
11720 {
11721 struct bp_location *bl, **blp_tmp;
11722 struct cleanup *old_chain;
11723
11724 if (!target_can_download_tracepoint ())
11725 return;
11726
11727 old_chain = save_current_space_and_thread ();
11728
11729 ALL_BP_LOCATIONS (bl, blp_tmp)
11730 {
11731 struct tracepoint *t;
11732
11733 if (!is_tracepoint (bl->owner))
11734 continue;
11735
11736 if ((bl->owner->type == bp_fast_tracepoint
11737 ? !may_insert_fast_tracepoints
11738 : !may_insert_tracepoints))
11739 continue;
11740
11741 /* In tracepoint, locations are _never_ duplicated, so
11742 should_be_inserted is equivalent to
11743 unduplicated_should_be_inserted. */
11744 if (!should_be_inserted (bl) || bl->inserted)
11745 continue;
11746
11747 switch_to_program_space_and_thread (bl->pspace);
11748
11749 target_download_tracepoint (bl);
11750
11751 bl->inserted = 1;
11752 t = (struct tracepoint *) bl->owner;
11753 t->number_on_target = bl->owner->number;
11754 }
11755
11756 do_cleanups (old_chain);
11757 }
11758
11759 /* Swap the insertion/duplication state between two locations. */
11760
11761 static void
11762 swap_insertion (struct bp_location *left, struct bp_location *right)
11763 {
11764 const int left_inserted = left->inserted;
11765 const int left_duplicate = left->duplicate;
11766 const int left_needs_update = left->needs_update;
11767 const struct bp_target_info left_target_info = left->target_info;
11768
11769 /* Locations of tracepoints can never be duplicated. */
11770 if (is_tracepoint (left->owner))
11771 gdb_assert (!left->duplicate);
11772 if (is_tracepoint (right->owner))
11773 gdb_assert (!right->duplicate);
11774
11775 left->inserted = right->inserted;
11776 left->duplicate = right->duplicate;
11777 left->needs_update = right->needs_update;
11778 left->target_info = right->target_info;
11779 right->inserted = left_inserted;
11780 right->duplicate = left_duplicate;
11781 right->needs_update = left_needs_update;
11782 right->target_info = left_target_info;
11783 }
11784
11785 /* Force the re-insertion of the locations at ADDRESS. This is called
11786 once a new/deleted/modified duplicate location is found and we are evaluating
11787 conditions on the target's side. Such conditions need to be updated on
11788 the target. */
11789
11790 static void
11791 force_breakpoint_reinsertion (struct bp_location *bl)
11792 {
11793 struct bp_location **locp = NULL, **loc2p;
11794 struct bp_location *loc;
11795 CORE_ADDR address = 0;
11796 int pspace_num;
11797
11798 address = bl->address;
11799 pspace_num = bl->pspace->num;
11800
11801 /* This is only meaningful if the target is
11802 evaluating conditions and if the user has
11803 opted for condition evaluation on the target's
11804 side. */
11805 if (gdb_evaluates_breakpoint_condition_p ()
11806 || !target_supports_evaluation_of_breakpoint_conditions ())
11807 return;
11808
11809 /* Flag all breakpoint locations with this address and
11810 the same program space as the location
11811 as "its condition has changed". We need to
11812 update the conditions on the target's side. */
11813 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
11814 {
11815 loc = *loc2p;
11816
11817 if (!is_breakpoint (loc->owner)
11818 || pspace_num != loc->pspace->num)
11819 continue;
11820
11821 /* Flag the location appropriately. We use a different state to
11822 let everyone know that we already updated the set of locations
11823 with addr bl->address and program space bl->pspace. This is so
11824 we don't have to keep calling these functions just to mark locations
11825 that have already been marked. */
11826 loc->condition_changed = condition_updated;
11827
11828 /* Free the agent expression bytecode as well. We will compute
11829 it later on. */
11830 if (loc->cond_bytecode)
11831 {
11832 free_agent_expr (loc->cond_bytecode);
11833 loc->cond_bytecode = NULL;
11834 }
11835 }
11836 }
11837
11838 /* If SHOULD_INSERT is false, do not insert any breakpoint locations
11839 into the inferior, only remove already-inserted locations that no
11840 longer should be inserted. Functions that delete a breakpoint or
11841 breakpoints should pass false, so that deleting a breakpoint
11842 doesn't have the side effect of inserting the locations of other
11843 breakpoints that are marked not-inserted, but should_be_inserted
11844 returns true on them.
11845
11846 This behaviour is useful is situations close to tear-down -- e.g.,
11847 after an exec, while the target still has execution, but breakpoint
11848 shadows of the previous executable image should *NOT* be restored
11849 to the new image; or before detaching, where the target still has
11850 execution and wants to delete breakpoints from GDB's lists, and all
11851 breakpoints had already been removed from the inferior. */
11852
11853 static void
11854 update_global_location_list (int should_insert)
11855 {
11856 struct breakpoint *b;
11857 struct bp_location **locp, *loc;
11858 struct cleanup *cleanups;
11859 /* Last breakpoint location address that was marked for update. */
11860 CORE_ADDR last_addr = 0;
11861 /* Last breakpoint location program space that was marked for update. */
11862 int last_pspace_num = -1;
11863
11864 /* Used in the duplicates detection below. When iterating over all
11865 bp_locations, points to the first bp_location of a given address.
11866 Breakpoints and watchpoints of different types are never
11867 duplicates of each other. Keep one pointer for each type of
11868 breakpoint/watchpoint, so we only need to loop over all locations
11869 once. */
11870 struct bp_location *bp_loc_first; /* breakpoint */
11871 struct bp_location *wp_loc_first; /* hardware watchpoint */
11872 struct bp_location *awp_loc_first; /* access watchpoint */
11873 struct bp_location *rwp_loc_first; /* read watchpoint */
11874
11875 /* Saved former bp_location array which we compare against the newly
11876 built bp_location from the current state of ALL_BREAKPOINTS. */
11877 struct bp_location **old_location, **old_locp;
11878 unsigned old_location_count;
11879
11880 old_location = bp_location;
11881 old_location_count = bp_location_count;
11882 bp_location = NULL;
11883 bp_location_count = 0;
11884 cleanups = make_cleanup (xfree, old_location);
11885
11886 ALL_BREAKPOINTS (b)
11887 for (loc = b->loc; loc; loc = loc->next)
11888 bp_location_count++;
11889
11890 bp_location = xmalloc (sizeof (*bp_location) * bp_location_count);
11891 locp = bp_location;
11892 ALL_BREAKPOINTS (b)
11893 for (loc = b->loc; loc; loc = loc->next)
11894 *locp++ = loc;
11895 qsort (bp_location, bp_location_count, sizeof (*bp_location),
11896 bp_location_compare);
11897
11898 bp_location_target_extensions_update ();
11899
11900 /* Identify bp_location instances that are no longer present in the
11901 new list, and therefore should be freed. Note that it's not
11902 necessary that those locations should be removed from inferior --
11903 if there's another location at the same address (previously
11904 marked as duplicate), we don't need to remove/insert the
11905 location.
11906
11907 LOCP is kept in sync with OLD_LOCP, each pointing to the current
11908 and former bp_location array state respectively. */
11909
11910 locp = bp_location;
11911 for (old_locp = old_location; old_locp < old_location + old_location_count;
11912 old_locp++)
11913 {
11914 struct bp_location *old_loc = *old_locp;
11915 struct bp_location **loc2p;
11916
11917 /* Tells if 'old_loc' is found among the new locations. If
11918 not, we have to free it. */
11919 int found_object = 0;
11920 /* Tells if the location should remain inserted in the target. */
11921 int keep_in_target = 0;
11922 int removed = 0;
11923
11924 /* Skip LOCP entries which will definitely never be needed.
11925 Stop either at or being the one matching OLD_LOC. */
11926 while (locp < bp_location + bp_location_count
11927 && (*locp)->address < old_loc->address)
11928 locp++;
11929
11930 for (loc2p = locp;
11931 (loc2p < bp_location + bp_location_count
11932 && (*loc2p)->address == old_loc->address);
11933 loc2p++)
11934 {
11935 /* Check if this is a new/duplicated location or a duplicated
11936 location that had its condition modified. If so, we want to send
11937 its condition to the target if evaluation of conditions is taking
11938 place there. */
11939 if ((*loc2p)->condition_changed == condition_modified
11940 && (last_addr != old_loc->address
11941 || last_pspace_num != old_loc->pspace->num))
11942 {
11943 force_breakpoint_reinsertion (*loc2p);
11944 last_pspace_num = old_loc->pspace->num;
11945 }
11946
11947 if (*loc2p == old_loc)
11948 found_object = 1;
11949 }
11950
11951 /* We have already handled this address, update it so that we don't
11952 have to go through updates again. */
11953 last_addr = old_loc->address;
11954
11955 /* Target-side condition evaluation: Handle deleted locations. */
11956 if (!found_object)
11957 force_breakpoint_reinsertion (old_loc);
11958
11959 /* If this location is no longer present, and inserted, look if
11960 there's maybe a new location at the same address. If so,
11961 mark that one inserted, and don't remove this one. This is
11962 needed so that we don't have a time window where a breakpoint
11963 at certain location is not inserted. */
11964
11965 if (old_loc->inserted)
11966 {
11967 /* If the location is inserted now, we might have to remove
11968 it. */
11969
11970 if (found_object && should_be_inserted (old_loc))
11971 {
11972 /* The location is still present in the location list,
11973 and still should be inserted. Don't do anything. */
11974 keep_in_target = 1;
11975 }
11976 else
11977 {
11978 /* This location still exists, but it won't be kept in the
11979 target since it may have been disabled. We proceed to
11980 remove its target-side condition. */
11981
11982 /* The location is either no longer present, or got
11983 disabled. See if there's another location at the
11984 same address, in which case we don't need to remove
11985 this one from the target. */
11986
11987 /* OLD_LOC comes from existing struct breakpoint. */
11988 if (breakpoint_address_is_meaningful (old_loc->owner))
11989 {
11990 for (loc2p = locp;
11991 (loc2p < bp_location + bp_location_count
11992 && (*loc2p)->address == old_loc->address);
11993 loc2p++)
11994 {
11995 struct bp_location *loc2 = *loc2p;
11996
11997 if (breakpoint_locations_match (loc2, old_loc))
11998 {
11999 /* Read watchpoint locations are switched to
12000 access watchpoints, if the former are not
12001 supported, but the latter are. */
12002 if (is_hardware_watchpoint (old_loc->owner))
12003 {
12004 gdb_assert (is_hardware_watchpoint (loc2->owner));
12005 loc2->watchpoint_type = old_loc->watchpoint_type;
12006 }
12007
12008 /* loc2 is a duplicated location. We need to check
12009 if it should be inserted in case it will be
12010 unduplicated. */
12011 if (loc2 != old_loc
12012 && unduplicated_should_be_inserted (loc2))
12013 {
12014 swap_insertion (old_loc, loc2);
12015 keep_in_target = 1;
12016 break;
12017 }
12018 }
12019 }
12020 }
12021 }
12022
12023 if (!keep_in_target)
12024 {
12025 if (remove_breakpoint (old_loc, mark_uninserted))
12026 {
12027 /* This is just about all we can do. We could keep
12028 this location on the global list, and try to
12029 remove it next time, but there's no particular
12030 reason why we will succeed next time.
12031
12032 Note that at this point, old_loc->owner is still
12033 valid, as delete_breakpoint frees the breakpoint
12034 only after calling us. */
12035 printf_filtered (_("warning: Error removing "
12036 "breakpoint %d\n"),
12037 old_loc->owner->number);
12038 }
12039 removed = 1;
12040 }
12041 }
12042
12043 if (!found_object)
12044 {
12045 if (removed && non_stop
12046 && breakpoint_address_is_meaningful (old_loc->owner)
12047 && !is_hardware_watchpoint (old_loc->owner))
12048 {
12049 /* This location was removed from the target. In
12050 non-stop mode, a race condition is possible where
12051 we've removed a breakpoint, but stop events for that
12052 breakpoint are already queued and will arrive later.
12053 We apply an heuristic to be able to distinguish such
12054 SIGTRAPs from other random SIGTRAPs: we keep this
12055 breakpoint location for a bit, and will retire it
12056 after we see some number of events. The theory here
12057 is that reporting of events should, "on the average",
12058 be fair, so after a while we'll see events from all
12059 threads that have anything of interest, and no longer
12060 need to keep this breakpoint location around. We
12061 don't hold locations forever so to reduce chances of
12062 mistaking a non-breakpoint SIGTRAP for a breakpoint
12063 SIGTRAP.
12064
12065 The heuristic failing can be disastrous on
12066 decr_pc_after_break targets.
12067
12068 On decr_pc_after_break targets, like e.g., x86-linux,
12069 if we fail to recognize a late breakpoint SIGTRAP,
12070 because events_till_retirement has reached 0 too
12071 soon, we'll fail to do the PC adjustment, and report
12072 a random SIGTRAP to the user. When the user resumes
12073 the inferior, it will most likely immediately crash
12074 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
12075 corrupted, because of being resumed e.g., in the
12076 middle of a multi-byte instruction, or skipped a
12077 one-byte instruction. This was actually seen happen
12078 on native x86-linux, and should be less rare on
12079 targets that do not support new thread events, like
12080 remote, due to the heuristic depending on
12081 thread_count.
12082
12083 Mistaking a random SIGTRAP for a breakpoint trap
12084 causes similar symptoms (PC adjustment applied when
12085 it shouldn't), but then again, playing with SIGTRAPs
12086 behind the debugger's back is asking for trouble.
12087
12088 Since hardware watchpoint traps are always
12089 distinguishable from other traps, so we don't need to
12090 apply keep hardware watchpoint moribund locations
12091 around. We simply always ignore hardware watchpoint
12092 traps we can no longer explain. */
12093
12094 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12095 old_loc->owner = NULL;
12096
12097 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
12098 }
12099 else
12100 {
12101 old_loc->owner = NULL;
12102 decref_bp_location (&old_loc);
12103 }
12104 }
12105 }
12106
12107 /* Rescan breakpoints at the same address and section, marking the
12108 first one as "first" and any others as "duplicates". This is so
12109 that the bpt instruction is only inserted once. If we have a
12110 permanent breakpoint at the same place as BPT, make that one the
12111 official one, and the rest as duplicates. Permanent breakpoints
12112 are sorted first for the same address.
12113
12114 Do the same for hardware watchpoints, but also considering the
12115 watchpoint's type (regular/access/read) and length. */
12116
12117 bp_loc_first = NULL;
12118 wp_loc_first = NULL;
12119 awp_loc_first = NULL;
12120 rwp_loc_first = NULL;
12121 ALL_BP_LOCATIONS (loc, locp)
12122 {
12123 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12124 non-NULL. */
12125 struct bp_location **loc_first_p;
12126 b = loc->owner;
12127
12128 if (!should_be_inserted (loc)
12129 || !breakpoint_address_is_meaningful (b)
12130 /* Don't detect duplicate for tracepoint locations because they are
12131 never duplicated. See the comments in field `duplicate' of
12132 `struct bp_location'. */
12133 || is_tracepoint (b))
12134 {
12135 /* Clear the condition modification flag. */
12136 loc->condition_changed = condition_unchanged;
12137 continue;
12138 }
12139
12140 /* Permanent breakpoint should always be inserted. */
12141 if (b->enable_state == bp_permanent && ! loc->inserted)
12142 internal_error (__FILE__, __LINE__,
12143 _("allegedly permanent breakpoint is not "
12144 "actually inserted"));
12145
12146 if (b->type == bp_hardware_watchpoint)
12147 loc_first_p = &wp_loc_first;
12148 else if (b->type == bp_read_watchpoint)
12149 loc_first_p = &rwp_loc_first;
12150 else if (b->type == bp_access_watchpoint)
12151 loc_first_p = &awp_loc_first;
12152 else
12153 loc_first_p = &bp_loc_first;
12154
12155 if (*loc_first_p == NULL
12156 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12157 || !breakpoint_locations_match (loc, *loc_first_p))
12158 {
12159 *loc_first_p = loc;
12160 loc->duplicate = 0;
12161
12162 if (is_breakpoint (loc->owner) && loc->condition_changed)
12163 {
12164 loc->needs_update = 1;
12165 /* Clear the condition modification flag. */
12166 loc->condition_changed = condition_unchanged;
12167 }
12168 continue;
12169 }
12170
12171
12172 /* This and the above ensure the invariant that the first location
12173 is not duplicated, and is the inserted one.
12174 All following are marked as duplicated, and are not inserted. */
12175 if (loc->inserted)
12176 swap_insertion (loc, *loc_first_p);
12177 loc->duplicate = 1;
12178
12179 /* Clear the condition modification flag. */
12180 loc->condition_changed = condition_unchanged;
12181
12182 if ((*loc_first_p)->owner->enable_state == bp_permanent && loc->inserted
12183 && b->enable_state != bp_permanent)
12184 internal_error (__FILE__, __LINE__,
12185 _("another breakpoint was inserted on top of "
12186 "a permanent breakpoint"));
12187 }
12188
12189 if (breakpoints_always_inserted_mode ()
12190 && (have_live_inferiors ()
12191 || (gdbarch_has_global_breakpoints (target_gdbarch))))
12192 {
12193 if (should_insert)
12194 insert_breakpoint_locations ();
12195 else
12196 {
12197 /* Though should_insert is false, we may need to update conditions
12198 on the target's side if it is evaluating such conditions. We
12199 only update conditions for locations that are marked
12200 "needs_update". */
12201 update_inserted_breakpoint_locations ();
12202 }
12203 }
12204
12205 if (should_insert)
12206 download_tracepoint_locations ();
12207
12208 do_cleanups (cleanups);
12209 }
12210
12211 void
12212 breakpoint_retire_moribund (void)
12213 {
12214 struct bp_location *loc;
12215 int ix;
12216
12217 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12218 if (--(loc->events_till_retirement) == 0)
12219 {
12220 decref_bp_location (&loc);
12221 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12222 --ix;
12223 }
12224 }
12225
12226 static void
12227 update_global_location_list_nothrow (int inserting)
12228 {
12229 volatile struct gdb_exception e;
12230
12231 TRY_CATCH (e, RETURN_MASK_ERROR)
12232 update_global_location_list (inserting);
12233 }
12234
12235 /* Clear BKP from a BPS. */
12236
12237 static void
12238 bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12239 {
12240 bpstat bs;
12241
12242 for (bs = bps; bs; bs = bs->next)
12243 if (bs->breakpoint_at == bpt)
12244 {
12245 bs->breakpoint_at = NULL;
12246 bs->old_val = NULL;
12247 /* bs->commands will be freed later. */
12248 }
12249 }
12250
12251 /* Callback for iterate_over_threads. */
12252 static int
12253 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12254 {
12255 struct breakpoint *bpt = data;
12256
12257 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12258 return 0;
12259 }
12260
12261 /* Helper for breakpoint and tracepoint breakpoint_ops->mention
12262 callbacks. */
12263
12264 static void
12265 say_where (struct breakpoint *b)
12266 {
12267 struct ui_out *uiout = current_uiout;
12268 struct value_print_options opts;
12269
12270 get_user_print_options (&opts);
12271
12272 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12273 single string. */
12274 if (b->loc == NULL)
12275 {
12276 printf_filtered (_(" (%s) pending."), b->addr_string);
12277 }
12278 else
12279 {
12280 if (opts.addressprint || b->loc->source_file == NULL)
12281 {
12282 printf_filtered (" at ");
12283 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12284 gdb_stdout);
12285 }
12286 if (b->loc->source_file)
12287 {
12288 /* If there is a single location, we can print the location
12289 more nicely. */
12290 if (b->loc->next == NULL)
12291 printf_filtered (": file %s, line %d.",
12292 b->loc->source_file, b->loc->line_number);
12293 else
12294 /* This is not ideal, but each location may have a
12295 different file name, and this at least reflects the
12296 real situation somewhat. */
12297 printf_filtered (": %s.", b->addr_string);
12298 }
12299
12300 if (b->loc->next)
12301 {
12302 struct bp_location *loc = b->loc;
12303 int n = 0;
12304 for (; loc; loc = loc->next)
12305 ++n;
12306 printf_filtered (" (%d locations)", n);
12307 }
12308 }
12309 }
12310
12311 /* Default bp_location_ops methods. */
12312
12313 static void
12314 bp_location_dtor (struct bp_location *self)
12315 {
12316 xfree (self->cond);
12317 if (self->cond_bytecode)
12318 free_agent_expr (self->cond_bytecode);
12319 xfree (self->function_name);
12320 xfree (self->source_file);
12321 }
12322
12323 static const struct bp_location_ops bp_location_ops =
12324 {
12325 bp_location_dtor
12326 };
12327
12328 /* Default breakpoint_ops methods all breakpoint_ops ultimately
12329 inherit from. */
12330
12331 static void
12332 base_breakpoint_dtor (struct breakpoint *self)
12333 {
12334 decref_counted_command_line (&self->commands);
12335 xfree (self->cond_string);
12336 xfree (self->addr_string);
12337 xfree (self->filter);
12338 xfree (self->addr_string_range_end);
12339 }
12340
12341 static struct bp_location *
12342 base_breakpoint_allocate_location (struct breakpoint *self)
12343 {
12344 struct bp_location *loc;
12345
12346 loc = XNEW (struct bp_location);
12347 init_bp_location (loc, &bp_location_ops, self);
12348 return loc;
12349 }
12350
12351 static void
12352 base_breakpoint_re_set (struct breakpoint *b)
12353 {
12354 /* Nothing to re-set. */
12355 }
12356
12357 #define internal_error_pure_virtual_called() \
12358 gdb_assert_not_reached ("pure virtual function called")
12359
12360 static int
12361 base_breakpoint_insert_location (struct bp_location *bl)
12362 {
12363 internal_error_pure_virtual_called ();
12364 }
12365
12366 static int
12367 base_breakpoint_remove_location (struct bp_location *bl)
12368 {
12369 internal_error_pure_virtual_called ();
12370 }
12371
12372 static int
12373 base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12374 struct address_space *aspace,
12375 CORE_ADDR bp_addr,
12376 const struct target_waitstatus *ws)
12377 {
12378 internal_error_pure_virtual_called ();
12379 }
12380
12381 static void
12382 base_breakpoint_check_status (bpstat bs)
12383 {
12384 /* Always stop. */
12385 }
12386
12387 /* A "works_in_software_mode" breakpoint_ops method that just internal
12388 errors. */
12389
12390 static int
12391 base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12392 {
12393 internal_error_pure_virtual_called ();
12394 }
12395
12396 /* A "resources_needed" breakpoint_ops method that just internal
12397 errors. */
12398
12399 static int
12400 base_breakpoint_resources_needed (const struct bp_location *bl)
12401 {
12402 internal_error_pure_virtual_called ();
12403 }
12404
12405 static enum print_stop_action
12406 base_breakpoint_print_it (bpstat bs)
12407 {
12408 internal_error_pure_virtual_called ();
12409 }
12410
12411 static void
12412 base_breakpoint_print_one_detail (const struct breakpoint *self,
12413 struct ui_out *uiout)
12414 {
12415 /* nothing */
12416 }
12417
12418 static void
12419 base_breakpoint_print_mention (struct breakpoint *b)
12420 {
12421 internal_error_pure_virtual_called ();
12422 }
12423
12424 static void
12425 base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12426 {
12427 internal_error_pure_virtual_called ();
12428 }
12429
12430 static void
12431 base_breakpoint_create_sals_from_address (char **arg,
12432 struct linespec_result *canonical,
12433 enum bptype type_wanted,
12434 char *addr_start,
12435 char **copy_arg)
12436 {
12437 internal_error_pure_virtual_called ();
12438 }
12439
12440 static void
12441 base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12442 struct linespec_result *c,
12443 struct linespec_sals *lsal,
12444 char *cond_string,
12445 char *extra_string,
12446 enum bptype type_wanted,
12447 enum bpdisp disposition,
12448 int thread,
12449 int task, int ignore_count,
12450 const struct breakpoint_ops *o,
12451 int from_tty, int enabled,
12452 int internal, unsigned flags)
12453 {
12454 internal_error_pure_virtual_called ();
12455 }
12456
12457 static void
12458 base_breakpoint_decode_linespec (struct breakpoint *b, char **s,
12459 struct symtabs_and_lines *sals)
12460 {
12461 internal_error_pure_virtual_called ();
12462 }
12463
12464 static struct breakpoint_ops base_breakpoint_ops =
12465 {
12466 base_breakpoint_dtor,
12467 base_breakpoint_allocate_location,
12468 base_breakpoint_re_set,
12469 base_breakpoint_insert_location,
12470 base_breakpoint_remove_location,
12471 base_breakpoint_breakpoint_hit,
12472 base_breakpoint_check_status,
12473 base_breakpoint_resources_needed,
12474 base_breakpoint_works_in_software_mode,
12475 base_breakpoint_print_it,
12476 NULL,
12477 base_breakpoint_print_one_detail,
12478 base_breakpoint_print_mention,
12479 base_breakpoint_print_recreate,
12480 base_breakpoint_create_sals_from_address,
12481 base_breakpoint_create_breakpoints_sal,
12482 base_breakpoint_decode_linespec,
12483 };
12484
12485 /* Default breakpoint_ops methods. */
12486
12487 static void
12488 bkpt_re_set (struct breakpoint *b)
12489 {
12490 /* FIXME: is this still reachable? */
12491 if (b->addr_string == NULL)
12492 {
12493 /* Anything without a string can't be re-set. */
12494 delete_breakpoint (b);
12495 return;
12496 }
12497
12498 breakpoint_re_set_default (b);
12499 }
12500
12501 static int
12502 bkpt_insert_location (struct bp_location *bl)
12503 {
12504 if (bl->loc_type == bp_loc_hardware_breakpoint)
12505 return target_insert_hw_breakpoint (bl->gdbarch,
12506 &bl->target_info);
12507 else
12508 return target_insert_breakpoint (bl->gdbarch,
12509 &bl->target_info);
12510 }
12511
12512 static int
12513 bkpt_remove_location (struct bp_location *bl)
12514 {
12515 if (bl->loc_type == bp_loc_hardware_breakpoint)
12516 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12517 else
12518 return target_remove_breakpoint (bl->gdbarch, &bl->target_info);
12519 }
12520
12521 static int
12522 bkpt_breakpoint_hit (const struct bp_location *bl,
12523 struct address_space *aspace, CORE_ADDR bp_addr,
12524 const struct target_waitstatus *ws)
12525 {
12526 struct breakpoint *b = bl->owner;
12527
12528 if (ws->kind != TARGET_WAITKIND_STOPPED
12529 || ws->value.sig != GDB_SIGNAL_TRAP)
12530 return 0;
12531
12532 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12533 aspace, bp_addr))
12534 return 0;
12535
12536 if (overlay_debugging /* unmapped overlay section */
12537 && section_is_overlay (bl->section)
12538 && !section_is_mapped (bl->section))
12539 return 0;
12540
12541 return 1;
12542 }
12543
12544 static int
12545 bkpt_resources_needed (const struct bp_location *bl)
12546 {
12547 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12548
12549 return 1;
12550 }
12551
12552 static enum print_stop_action
12553 bkpt_print_it (bpstat bs)
12554 {
12555 struct breakpoint *b;
12556 const struct bp_location *bl;
12557 int bp_temp;
12558 struct ui_out *uiout = current_uiout;
12559
12560 gdb_assert (bs->bp_location_at != NULL);
12561
12562 bl = bs->bp_location_at;
12563 b = bs->breakpoint_at;
12564
12565 bp_temp = b->disposition == disp_del;
12566 if (bl->address != bl->requested_address)
12567 breakpoint_adjustment_warning (bl->requested_address,
12568 bl->address,
12569 b->number, 1);
12570 annotate_breakpoint (b->number);
12571 if (bp_temp)
12572 ui_out_text (uiout, "\nTemporary breakpoint ");
12573 else
12574 ui_out_text (uiout, "\nBreakpoint ");
12575 if (ui_out_is_mi_like_p (uiout))
12576 {
12577 ui_out_field_string (uiout, "reason",
12578 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
12579 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
12580 }
12581 ui_out_field_int (uiout, "bkptno", b->number);
12582 ui_out_text (uiout, ", ");
12583
12584 return PRINT_SRC_AND_LOC;
12585 }
12586
12587 static void
12588 bkpt_print_mention (struct breakpoint *b)
12589 {
12590 if (ui_out_is_mi_like_p (current_uiout))
12591 return;
12592
12593 switch (b->type)
12594 {
12595 case bp_breakpoint:
12596 case bp_gnu_ifunc_resolver:
12597 if (b->disposition == disp_del)
12598 printf_filtered (_("Temporary breakpoint"));
12599 else
12600 printf_filtered (_("Breakpoint"));
12601 printf_filtered (_(" %d"), b->number);
12602 if (b->type == bp_gnu_ifunc_resolver)
12603 printf_filtered (_(" at gnu-indirect-function resolver"));
12604 break;
12605 case bp_hardware_breakpoint:
12606 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
12607 break;
12608 case bp_dprintf:
12609 printf_filtered (_("Dprintf %d"), b->number);
12610 break;
12611 }
12612
12613 say_where (b);
12614 }
12615
12616 static void
12617 bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12618 {
12619 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
12620 fprintf_unfiltered (fp, "tbreak");
12621 else if (tp->type == bp_breakpoint)
12622 fprintf_unfiltered (fp, "break");
12623 else if (tp->type == bp_hardware_breakpoint
12624 && tp->disposition == disp_del)
12625 fprintf_unfiltered (fp, "thbreak");
12626 else if (tp->type == bp_hardware_breakpoint)
12627 fprintf_unfiltered (fp, "hbreak");
12628 else
12629 internal_error (__FILE__, __LINE__,
12630 _("unhandled breakpoint type %d"), (int) tp->type);
12631
12632 fprintf_unfiltered (fp, " %s", tp->addr_string);
12633 print_recreate_thread (tp, fp);
12634 }
12635
12636 static void
12637 bkpt_create_sals_from_address (char **arg,
12638 struct linespec_result *canonical,
12639 enum bptype type_wanted,
12640 char *addr_start, char **copy_arg)
12641 {
12642 create_sals_from_address_default (arg, canonical, type_wanted,
12643 addr_start, copy_arg);
12644 }
12645
12646 static void
12647 bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
12648 struct linespec_result *canonical,
12649 struct linespec_sals *lsal,
12650 char *cond_string,
12651 char *extra_string,
12652 enum bptype type_wanted,
12653 enum bpdisp disposition,
12654 int thread,
12655 int task, int ignore_count,
12656 const struct breakpoint_ops *ops,
12657 int from_tty, int enabled,
12658 int internal, unsigned flags)
12659 {
12660 create_breakpoints_sal_default (gdbarch, canonical, lsal,
12661 cond_string, extra_string,
12662 type_wanted,
12663 disposition, thread, task,
12664 ignore_count, ops, from_tty,
12665 enabled, internal, flags);
12666 }
12667
12668 static void
12669 bkpt_decode_linespec (struct breakpoint *b, char **s,
12670 struct symtabs_and_lines *sals)
12671 {
12672 decode_linespec_default (b, s, sals);
12673 }
12674
12675 /* Virtual table for internal breakpoints. */
12676
12677 static void
12678 internal_bkpt_re_set (struct breakpoint *b)
12679 {
12680 switch (b->type)
12681 {
12682 /* Delete overlay event and longjmp master breakpoints; they
12683 will be reset later by breakpoint_re_set. */
12684 case bp_overlay_event:
12685 case bp_longjmp_master:
12686 case bp_std_terminate_master:
12687 case bp_exception_master:
12688 delete_breakpoint (b);
12689 break;
12690
12691 /* This breakpoint is special, it's set up when the inferior
12692 starts and we really don't want to touch it. */
12693 case bp_shlib_event:
12694
12695 /* Like bp_shlib_event, this breakpoint type is special. Once
12696 it is set up, we do not want to touch it. */
12697 case bp_thread_event:
12698 break;
12699 }
12700 }
12701
12702 static void
12703 internal_bkpt_check_status (bpstat bs)
12704 {
12705 if (bs->breakpoint_at->type == bp_shlib_event)
12706 {
12707 /* If requested, stop when the dynamic linker notifies GDB of
12708 events. This allows the user to get control and place
12709 breakpoints in initializer routines for dynamically loaded
12710 objects (among other things). */
12711 bs->stop = stop_on_solib_events;
12712 bs->print = stop_on_solib_events;
12713 }
12714 else
12715 bs->stop = 0;
12716 }
12717
12718 static enum print_stop_action
12719 internal_bkpt_print_it (bpstat bs)
12720 {
12721 struct ui_out *uiout = current_uiout;
12722 struct breakpoint *b;
12723
12724 b = bs->breakpoint_at;
12725
12726 switch (b->type)
12727 {
12728 case bp_shlib_event:
12729 /* Did we stop because the user set the stop_on_solib_events
12730 variable? (If so, we report this as a generic, "Stopped due
12731 to shlib event" message.) */
12732 print_solib_event (0);
12733 break;
12734
12735 case bp_thread_event:
12736 /* Not sure how we will get here.
12737 GDB should not stop for these breakpoints. */
12738 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
12739 break;
12740
12741 case bp_overlay_event:
12742 /* By analogy with the thread event, GDB should not stop for these. */
12743 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
12744 break;
12745
12746 case bp_longjmp_master:
12747 /* These should never be enabled. */
12748 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
12749 break;
12750
12751 case bp_std_terminate_master:
12752 /* These should never be enabled. */
12753 printf_filtered (_("std::terminate Master Breakpoint: "
12754 "gdb should not stop!\n"));
12755 break;
12756
12757 case bp_exception_master:
12758 /* These should never be enabled. */
12759 printf_filtered (_("Exception Master Breakpoint: "
12760 "gdb should not stop!\n"));
12761 break;
12762 }
12763
12764 return PRINT_NOTHING;
12765 }
12766
12767 static void
12768 internal_bkpt_print_mention (struct breakpoint *b)
12769 {
12770 /* Nothing to mention. These breakpoints are internal. */
12771 }
12772
12773 /* Virtual table for momentary breakpoints */
12774
12775 static void
12776 momentary_bkpt_re_set (struct breakpoint *b)
12777 {
12778 /* Keep temporary breakpoints, which can be encountered when we step
12779 over a dlopen call and SOLIB_ADD is resetting the breakpoints.
12780 Otherwise these should have been blown away via the cleanup chain
12781 or by breakpoint_init_inferior when we rerun the executable. */
12782 }
12783
12784 static void
12785 momentary_bkpt_check_status (bpstat bs)
12786 {
12787 /* Nothing. The point of these breakpoints is causing a stop. */
12788 }
12789
12790 static enum print_stop_action
12791 momentary_bkpt_print_it (bpstat bs)
12792 {
12793 struct ui_out *uiout = current_uiout;
12794
12795 if (ui_out_is_mi_like_p (uiout))
12796 {
12797 struct breakpoint *b = bs->breakpoint_at;
12798
12799 switch (b->type)
12800 {
12801 case bp_finish:
12802 ui_out_field_string
12803 (uiout, "reason",
12804 async_reason_lookup (EXEC_ASYNC_FUNCTION_FINISHED));
12805 break;
12806
12807 case bp_until:
12808 ui_out_field_string
12809 (uiout, "reason",
12810 async_reason_lookup (EXEC_ASYNC_LOCATION_REACHED));
12811 break;
12812 }
12813 }
12814
12815 return PRINT_UNKNOWN;
12816 }
12817
12818 static void
12819 momentary_bkpt_print_mention (struct breakpoint *b)
12820 {
12821 /* Nothing to mention. These breakpoints are internal. */
12822 }
12823
12824 /* Specific methods for probe breakpoints. */
12825
12826 static int
12827 bkpt_probe_insert_location (struct bp_location *bl)
12828 {
12829 int v = bkpt_insert_location (bl);
12830
12831 if (v == 0)
12832 {
12833 /* The insertion was successful, now let's set the probe's semaphore
12834 if needed. */
12835 bl->probe->pops->set_semaphore (bl->probe, bl->gdbarch);
12836 }
12837
12838 return v;
12839 }
12840
12841 static int
12842 bkpt_probe_remove_location (struct bp_location *bl)
12843 {
12844 /* Let's clear the semaphore before removing the location. */
12845 bl->probe->pops->clear_semaphore (bl->probe, bl->gdbarch);
12846
12847 return bkpt_remove_location (bl);
12848 }
12849
12850 static void
12851 bkpt_probe_create_sals_from_address (char **arg,
12852 struct linespec_result *canonical,
12853 enum bptype type_wanted,
12854 char *addr_start, char **copy_arg)
12855 {
12856 struct linespec_sals lsal;
12857
12858 lsal.sals = parse_probes (arg, canonical);
12859
12860 *copy_arg = xstrdup (canonical->addr_string);
12861 lsal.canonical = xstrdup (*copy_arg);
12862
12863 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
12864 }
12865
12866 static void
12867 bkpt_probe_decode_linespec (struct breakpoint *b, char **s,
12868 struct symtabs_and_lines *sals)
12869 {
12870 *sals = parse_probes (s, NULL);
12871 if (!sals->sals)
12872 error (_("probe not found"));
12873 }
12874
12875 /* The breakpoint_ops structure to be used in tracepoints. */
12876
12877 static void
12878 tracepoint_re_set (struct breakpoint *b)
12879 {
12880 breakpoint_re_set_default (b);
12881 }
12882
12883 static int
12884 tracepoint_breakpoint_hit (const struct bp_location *bl,
12885 struct address_space *aspace, CORE_ADDR bp_addr,
12886 const struct target_waitstatus *ws)
12887 {
12888 /* By definition, the inferior does not report stops at
12889 tracepoints. */
12890 return 0;
12891 }
12892
12893 static void
12894 tracepoint_print_one_detail (const struct breakpoint *self,
12895 struct ui_out *uiout)
12896 {
12897 struct tracepoint *tp = (struct tracepoint *) self;
12898 if (tp->static_trace_marker_id)
12899 {
12900 gdb_assert (self->type == bp_static_tracepoint);
12901
12902 ui_out_text (uiout, "\tmarker id is ");
12903 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
12904 tp->static_trace_marker_id);
12905 ui_out_text (uiout, "\n");
12906 }
12907 }
12908
12909 static void
12910 tracepoint_print_mention (struct breakpoint *b)
12911 {
12912 if (ui_out_is_mi_like_p (current_uiout))
12913 return;
12914
12915 switch (b->type)
12916 {
12917 case bp_tracepoint:
12918 printf_filtered (_("Tracepoint"));
12919 printf_filtered (_(" %d"), b->number);
12920 break;
12921 case bp_fast_tracepoint:
12922 printf_filtered (_("Fast tracepoint"));
12923 printf_filtered (_(" %d"), b->number);
12924 break;
12925 case bp_static_tracepoint:
12926 printf_filtered (_("Static tracepoint"));
12927 printf_filtered (_(" %d"), b->number);
12928 break;
12929 default:
12930 internal_error (__FILE__, __LINE__,
12931 _("unhandled tracepoint type %d"), (int) b->type);
12932 }
12933
12934 say_where (b);
12935 }
12936
12937 static void
12938 tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
12939 {
12940 struct tracepoint *tp = (struct tracepoint *) self;
12941
12942 if (self->type == bp_fast_tracepoint)
12943 fprintf_unfiltered (fp, "ftrace");
12944 if (self->type == bp_static_tracepoint)
12945 fprintf_unfiltered (fp, "strace");
12946 else if (self->type == bp_tracepoint)
12947 fprintf_unfiltered (fp, "trace");
12948 else
12949 internal_error (__FILE__, __LINE__,
12950 _("unhandled tracepoint type %d"), (int) self->type);
12951
12952 fprintf_unfiltered (fp, " %s", self->addr_string);
12953 print_recreate_thread (self, fp);
12954
12955 if (tp->pass_count)
12956 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
12957 }
12958
12959 static void
12960 tracepoint_create_sals_from_address (char **arg,
12961 struct linespec_result *canonical,
12962 enum bptype type_wanted,
12963 char *addr_start, char **copy_arg)
12964 {
12965 create_sals_from_address_default (arg, canonical, type_wanted,
12966 addr_start, copy_arg);
12967 }
12968
12969 static void
12970 tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12971 struct linespec_result *canonical,
12972 struct linespec_sals *lsal,
12973 char *cond_string,
12974 char *extra_string,
12975 enum bptype type_wanted,
12976 enum bpdisp disposition,
12977 int thread,
12978 int task, int ignore_count,
12979 const struct breakpoint_ops *ops,
12980 int from_tty, int enabled,
12981 int internal, unsigned flags)
12982 {
12983 create_breakpoints_sal_default (gdbarch, canonical, lsal,
12984 cond_string, extra_string,
12985 type_wanted,
12986 disposition, thread, task,
12987 ignore_count, ops, from_tty,
12988 enabled, internal, flags);
12989 }
12990
12991 static void
12992 tracepoint_decode_linespec (struct breakpoint *b, char **s,
12993 struct symtabs_and_lines *sals)
12994 {
12995 decode_linespec_default (b, s, sals);
12996 }
12997
12998 struct breakpoint_ops tracepoint_breakpoint_ops;
12999
13000 /* The breakpoint_ops structure to be use on tracepoints placed in a
13001 static probe. */
13002
13003 static void
13004 tracepoint_probe_create_sals_from_address (char **arg,
13005 struct linespec_result *canonical,
13006 enum bptype type_wanted,
13007 char *addr_start, char **copy_arg)
13008 {
13009 /* We use the same method for breakpoint on probes. */
13010 bkpt_probe_create_sals_from_address (arg, canonical, type_wanted,
13011 addr_start, copy_arg);
13012 }
13013
13014 static void
13015 tracepoint_probe_decode_linespec (struct breakpoint *b, char **s,
13016 struct symtabs_and_lines *sals)
13017 {
13018 /* We use the same method for breakpoint on probes. */
13019 bkpt_probe_decode_linespec (b, s, sals);
13020 }
13021
13022 static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13023
13024 /* The breakpoint_ops structure to be used on static tracepoints with
13025 markers (`-m'). */
13026
13027 static void
13028 strace_marker_create_sals_from_address (char **arg,
13029 struct linespec_result *canonical,
13030 enum bptype type_wanted,
13031 char *addr_start, char **copy_arg)
13032 {
13033 struct linespec_sals lsal;
13034
13035 lsal.sals = decode_static_tracepoint_spec (arg);
13036
13037 *copy_arg = savestring (addr_start, *arg - addr_start);
13038
13039 canonical->addr_string = xstrdup (*copy_arg);
13040 lsal.canonical = xstrdup (*copy_arg);
13041 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13042 }
13043
13044 static void
13045 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13046 struct linespec_result *canonical,
13047 struct linespec_sals *lsal,
13048 char *cond_string,
13049 char *extra_string,
13050 enum bptype type_wanted,
13051 enum bpdisp disposition,
13052 int thread,
13053 int task, int ignore_count,
13054 const struct breakpoint_ops *ops,
13055 int from_tty, int enabled,
13056 int internal, unsigned flags)
13057 {
13058 int i;
13059
13060 /* If the user is creating a static tracepoint by marker id
13061 (strace -m MARKER_ID), then store the sals index, so that
13062 breakpoint_re_set can try to match up which of the newly
13063 found markers corresponds to this one, and, don't try to
13064 expand multiple locations for each sal, given than SALS
13065 already should contain all sals for MARKER_ID. */
13066
13067 for (i = 0; i < lsal->sals.nelts; ++i)
13068 {
13069 struct symtabs_and_lines expanded;
13070 struct tracepoint *tp;
13071 struct cleanup *old_chain;
13072 char *addr_string;
13073
13074 expanded.nelts = 1;
13075 expanded.sals = &lsal->sals.sals[i];
13076
13077 addr_string = xstrdup (canonical->addr_string);
13078 old_chain = make_cleanup (xfree, addr_string);
13079
13080 tp = XCNEW (struct tracepoint);
13081 init_breakpoint_sal (&tp->base, gdbarch, expanded,
13082 addr_string, NULL,
13083 cond_string, extra_string,
13084 type_wanted, disposition,
13085 thread, task, ignore_count, ops,
13086 from_tty, enabled, internal, flags,
13087 canonical->special_display);
13088 /* Given that its possible to have multiple markers with
13089 the same string id, if the user is creating a static
13090 tracepoint by marker id ("strace -m MARKER_ID"), then
13091 store the sals index, so that breakpoint_re_set can
13092 try to match up which of the newly found markers
13093 corresponds to this one */
13094 tp->static_trace_marker_id_idx = i;
13095
13096 install_breakpoint (internal, &tp->base, 0);
13097
13098 discard_cleanups (old_chain);
13099 }
13100 }
13101
13102 static void
13103 strace_marker_decode_linespec (struct breakpoint *b, char **s,
13104 struct symtabs_and_lines *sals)
13105 {
13106 struct tracepoint *tp = (struct tracepoint *) b;
13107
13108 *sals = decode_static_tracepoint_spec (s);
13109 if (sals->nelts > tp->static_trace_marker_id_idx)
13110 {
13111 sals->sals[0] = sals->sals[tp->static_trace_marker_id_idx];
13112 sals->nelts = 1;
13113 }
13114 else
13115 error (_("marker %s not found"), tp->static_trace_marker_id);
13116 }
13117
13118 static struct breakpoint_ops strace_marker_breakpoint_ops;
13119
13120 static int
13121 strace_marker_p (struct breakpoint *b)
13122 {
13123 return b->ops == &strace_marker_breakpoint_ops;
13124 }
13125
13126 /* Delete a breakpoint and clean up all traces of it in the data
13127 structures. */
13128
13129 void
13130 delete_breakpoint (struct breakpoint *bpt)
13131 {
13132 struct breakpoint *b;
13133
13134 gdb_assert (bpt != NULL);
13135
13136 /* Has this bp already been deleted? This can happen because
13137 multiple lists can hold pointers to bp's. bpstat lists are
13138 especial culprits.
13139
13140 One example of this happening is a watchpoint's scope bp. When
13141 the scope bp triggers, we notice that the watchpoint is out of
13142 scope, and delete it. We also delete its scope bp. But the
13143 scope bp is marked "auto-deleting", and is already on a bpstat.
13144 That bpstat is then checked for auto-deleting bp's, which are
13145 deleted.
13146
13147 A real solution to this problem might involve reference counts in
13148 bp's, and/or giving them pointers back to their referencing
13149 bpstat's, and teaching delete_breakpoint to only free a bp's
13150 storage when no more references were extent. A cheaper bandaid
13151 was chosen. */
13152 if (bpt->type == bp_none)
13153 return;
13154
13155 /* At least avoid this stale reference until the reference counting
13156 of breakpoints gets resolved. */
13157 if (bpt->related_breakpoint != bpt)
13158 {
13159 struct breakpoint *related;
13160 struct watchpoint *w;
13161
13162 if (bpt->type == bp_watchpoint_scope)
13163 w = (struct watchpoint *) bpt->related_breakpoint;
13164 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
13165 w = (struct watchpoint *) bpt;
13166 else
13167 w = NULL;
13168 if (w != NULL)
13169 watchpoint_del_at_next_stop (w);
13170
13171 /* Unlink bpt from the bpt->related_breakpoint ring. */
13172 for (related = bpt; related->related_breakpoint != bpt;
13173 related = related->related_breakpoint);
13174 related->related_breakpoint = bpt->related_breakpoint;
13175 bpt->related_breakpoint = bpt;
13176 }
13177
13178 /* watch_command_1 creates a watchpoint but only sets its number if
13179 update_watchpoint succeeds in creating its bp_locations. If there's
13180 a problem in that process, we'll be asked to delete the half-created
13181 watchpoint. In that case, don't announce the deletion. */
13182 if (bpt->number)
13183 observer_notify_breakpoint_deleted (bpt);
13184
13185 if (breakpoint_chain == bpt)
13186 breakpoint_chain = bpt->next;
13187
13188 ALL_BREAKPOINTS (b)
13189 if (b->next == bpt)
13190 {
13191 b->next = bpt->next;
13192 break;
13193 }
13194
13195 /* Be sure no bpstat's are pointing at the breakpoint after it's
13196 been freed. */
13197 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
13198 in all threads for now. Note that we cannot just remove bpstats
13199 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13200 commands are associated with the bpstat; if we remove it here,
13201 then the later call to bpstat_do_actions (&stop_bpstat); in
13202 event-top.c won't do anything, and temporary breakpoints with
13203 commands won't work. */
13204
13205 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13206
13207 /* Now that breakpoint is removed from breakpoint list, update the
13208 global location list. This will remove locations that used to
13209 belong to this breakpoint. Do this before freeing the breakpoint
13210 itself, since remove_breakpoint looks at location's owner. It
13211 might be better design to have location completely
13212 self-contained, but it's not the case now. */
13213 update_global_location_list (0);
13214
13215 bpt->ops->dtor (bpt);
13216 /* On the chance that someone will soon try again to delete this
13217 same bp, we mark it as deleted before freeing its storage. */
13218 bpt->type = bp_none;
13219 xfree (bpt);
13220 }
13221
13222 static void
13223 do_delete_breakpoint_cleanup (void *b)
13224 {
13225 delete_breakpoint (b);
13226 }
13227
13228 struct cleanup *
13229 make_cleanup_delete_breakpoint (struct breakpoint *b)
13230 {
13231 return make_cleanup (do_delete_breakpoint_cleanup, b);
13232 }
13233
13234 /* Iterator function to call a user-provided callback function once
13235 for each of B and its related breakpoints. */
13236
13237 static void
13238 iterate_over_related_breakpoints (struct breakpoint *b,
13239 void (*function) (struct breakpoint *,
13240 void *),
13241 void *data)
13242 {
13243 struct breakpoint *related;
13244
13245 related = b;
13246 do
13247 {
13248 struct breakpoint *next;
13249
13250 /* FUNCTION may delete RELATED. */
13251 next = related->related_breakpoint;
13252
13253 if (next == related)
13254 {
13255 /* RELATED is the last ring entry. */
13256 function (related, data);
13257
13258 /* FUNCTION may have deleted it, so we'd never reach back to
13259 B. There's nothing left to do anyway, so just break
13260 out. */
13261 break;
13262 }
13263 else
13264 function (related, data);
13265
13266 related = next;
13267 }
13268 while (related != b);
13269 }
13270
13271 static void
13272 do_delete_breakpoint (struct breakpoint *b, void *ignore)
13273 {
13274 delete_breakpoint (b);
13275 }
13276
13277 /* A callback for map_breakpoint_numbers that calls
13278 delete_breakpoint. */
13279
13280 static void
13281 do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
13282 {
13283 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
13284 }
13285
13286 void
13287 delete_command (char *arg, int from_tty)
13288 {
13289 struct breakpoint *b, *b_tmp;
13290
13291 dont_repeat ();
13292
13293 if (arg == 0)
13294 {
13295 int breaks_to_delete = 0;
13296
13297 /* Delete all breakpoints if no argument. Do not delete
13298 internal breakpoints, these have to be deleted with an
13299 explicit breakpoint number argument. */
13300 ALL_BREAKPOINTS (b)
13301 if (user_breakpoint_p (b))
13302 {
13303 breaks_to_delete = 1;
13304 break;
13305 }
13306
13307 /* Ask user only if there are some breakpoints to delete. */
13308 if (!from_tty
13309 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
13310 {
13311 ALL_BREAKPOINTS_SAFE (b, b_tmp)
13312 if (user_breakpoint_p (b))
13313 delete_breakpoint (b);
13314 }
13315 }
13316 else
13317 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
13318 }
13319
13320 static int
13321 all_locations_are_pending (struct bp_location *loc)
13322 {
13323 for (; loc; loc = loc->next)
13324 if (!loc->shlib_disabled
13325 && !loc->pspace->executing_startup)
13326 return 0;
13327 return 1;
13328 }
13329
13330 /* Subroutine of update_breakpoint_locations to simplify it.
13331 Return non-zero if multiple fns in list LOC have the same name.
13332 Null names are ignored. */
13333
13334 static int
13335 ambiguous_names_p (struct bp_location *loc)
13336 {
13337 struct bp_location *l;
13338 htab_t htab = htab_create_alloc (13, htab_hash_string,
13339 (int (*) (const void *,
13340 const void *)) streq,
13341 NULL, xcalloc, xfree);
13342
13343 for (l = loc; l != NULL; l = l->next)
13344 {
13345 const char **slot;
13346 const char *name = l->function_name;
13347
13348 /* Allow for some names to be NULL, ignore them. */
13349 if (name == NULL)
13350 continue;
13351
13352 slot = (const char **) htab_find_slot (htab, (const void *) name,
13353 INSERT);
13354 /* NOTE: We can assume slot != NULL here because xcalloc never
13355 returns NULL. */
13356 if (*slot != NULL)
13357 {
13358 htab_delete (htab);
13359 return 1;
13360 }
13361 *slot = name;
13362 }
13363
13364 htab_delete (htab);
13365 return 0;
13366 }
13367
13368 /* When symbols change, it probably means the sources changed as well,
13369 and it might mean the static tracepoint markers are no longer at
13370 the same address or line numbers they used to be at last we
13371 checked. Losing your static tracepoints whenever you rebuild is
13372 undesirable. This function tries to resync/rematch gdb static
13373 tracepoints with the markers on the target, for static tracepoints
13374 that have not been set by marker id. Static tracepoint that have
13375 been set by marker id are reset by marker id in breakpoint_re_set.
13376 The heuristic is:
13377
13378 1) For a tracepoint set at a specific address, look for a marker at
13379 the old PC. If one is found there, assume to be the same marker.
13380 If the name / string id of the marker found is different from the
13381 previous known name, assume that means the user renamed the marker
13382 in the sources, and output a warning.
13383
13384 2) For a tracepoint set at a given line number, look for a marker
13385 at the new address of the old line number. If one is found there,
13386 assume to be the same marker. If the name / string id of the
13387 marker found is different from the previous known name, assume that
13388 means the user renamed the marker in the sources, and output a
13389 warning.
13390
13391 3) If a marker is no longer found at the same address or line, it
13392 may mean the marker no longer exists. But it may also just mean
13393 the code changed a bit. Maybe the user added a few lines of code
13394 that made the marker move up or down (in line number terms). Ask
13395 the target for info about the marker with the string id as we knew
13396 it. If found, update line number and address in the matching
13397 static tracepoint. This will get confused if there's more than one
13398 marker with the same ID (possible in UST, although unadvised
13399 precisely because it confuses tools). */
13400
13401 static struct symtab_and_line
13402 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13403 {
13404 struct tracepoint *tp = (struct tracepoint *) b;
13405 struct static_tracepoint_marker marker;
13406 CORE_ADDR pc;
13407
13408 pc = sal.pc;
13409 if (sal.line)
13410 find_line_pc (sal.symtab, sal.line, &pc);
13411
13412 if (target_static_tracepoint_marker_at (pc, &marker))
13413 {
13414 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
13415 warning (_("static tracepoint %d changed probed marker from %s to %s"),
13416 b->number,
13417 tp->static_trace_marker_id, marker.str_id);
13418
13419 xfree (tp->static_trace_marker_id);
13420 tp->static_trace_marker_id = xstrdup (marker.str_id);
13421 release_static_tracepoint_marker (&marker);
13422
13423 return sal;
13424 }
13425
13426 /* Old marker wasn't found on target at lineno. Try looking it up
13427 by string ID. */
13428 if (!sal.explicit_pc
13429 && sal.line != 0
13430 && sal.symtab != NULL
13431 && tp->static_trace_marker_id != NULL)
13432 {
13433 VEC(static_tracepoint_marker_p) *markers;
13434
13435 markers
13436 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
13437
13438 if (!VEC_empty(static_tracepoint_marker_p, markers))
13439 {
13440 struct symtab_and_line sal2;
13441 struct symbol *sym;
13442 struct static_tracepoint_marker *tpmarker;
13443 struct ui_out *uiout = current_uiout;
13444
13445 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
13446
13447 xfree (tp->static_trace_marker_id);
13448 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
13449
13450 warning (_("marker for static tracepoint %d (%s) not "
13451 "found at previous line number"),
13452 b->number, tp->static_trace_marker_id);
13453
13454 init_sal (&sal2);
13455
13456 sal2.pc = tpmarker->address;
13457
13458 sal2 = find_pc_line (tpmarker->address, 0);
13459 sym = find_pc_sect_function (tpmarker->address, NULL);
13460 ui_out_text (uiout, "Now in ");
13461 if (sym)
13462 {
13463 ui_out_field_string (uiout, "func",
13464 SYMBOL_PRINT_NAME (sym));
13465 ui_out_text (uiout, " at ");
13466 }
13467 ui_out_field_string (uiout, "file", sal2.symtab->filename);
13468 ui_out_text (uiout, ":");
13469
13470 if (ui_out_is_mi_like_p (uiout))
13471 {
13472 char *fullname = symtab_to_fullname (sal2.symtab);
13473
13474 if (fullname)
13475 ui_out_field_string (uiout, "fullname", fullname);
13476 }
13477
13478 ui_out_field_int (uiout, "line", sal2.line);
13479 ui_out_text (uiout, "\n");
13480
13481 b->loc->line_number = sal2.line;
13482
13483 xfree (b->loc->source_file);
13484 if (sym)
13485 b->loc->source_file = xstrdup (sal2.symtab->filename);
13486 else
13487 b->loc->source_file = NULL;
13488
13489 xfree (b->addr_string);
13490 b->addr_string = xstrprintf ("%s:%d",
13491 sal2.symtab->filename,
13492 b->loc->line_number);
13493
13494 /* Might be nice to check if function changed, and warn if
13495 so. */
13496
13497 release_static_tracepoint_marker (tpmarker);
13498 }
13499 }
13500 return sal;
13501 }
13502
13503 /* Returns 1 iff locations A and B are sufficiently same that
13504 we don't need to report breakpoint as changed. */
13505
13506 static int
13507 locations_are_equal (struct bp_location *a, struct bp_location *b)
13508 {
13509 while (a && b)
13510 {
13511 if (a->address != b->address)
13512 return 0;
13513
13514 if (a->shlib_disabled != b->shlib_disabled)
13515 return 0;
13516
13517 if (a->enabled != b->enabled)
13518 return 0;
13519
13520 a = a->next;
13521 b = b->next;
13522 }
13523
13524 if ((a == NULL) != (b == NULL))
13525 return 0;
13526
13527 return 1;
13528 }
13529
13530 /* Create new breakpoint locations for B (a hardware or software breakpoint)
13531 based on SALS and SALS_END. If SALS_END.NELTS is not zero, then B is
13532 a ranged breakpoint. */
13533
13534 void
13535 update_breakpoint_locations (struct breakpoint *b,
13536 struct symtabs_and_lines sals,
13537 struct symtabs_and_lines sals_end)
13538 {
13539 int i;
13540 struct bp_location *existing_locations = b->loc;
13541
13542 if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
13543 {
13544 /* Ranged breakpoints have only one start location and one end
13545 location. */
13546 b->enable_state = bp_disabled;
13547 update_global_location_list (1);
13548 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
13549 "multiple locations found\n"),
13550 b->number);
13551 return;
13552 }
13553
13554 /* If there's no new locations, and all existing locations are
13555 pending, don't do anything. This optimizes the common case where
13556 all locations are in the same shared library, that was unloaded.
13557 We'd like to retain the location, so that when the library is
13558 loaded again, we don't loose the enabled/disabled status of the
13559 individual locations. */
13560 if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
13561 return;
13562
13563 b->loc = NULL;
13564
13565 for (i = 0; i < sals.nelts; ++i)
13566 {
13567 struct bp_location *new_loc;
13568
13569 switch_to_program_space_and_thread (sals.sals[i].pspace);
13570
13571 new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
13572
13573 /* Reparse conditions, they might contain references to the
13574 old symtab. */
13575 if (b->cond_string != NULL)
13576 {
13577 char *s;
13578 volatile struct gdb_exception e;
13579
13580 s = b->cond_string;
13581 TRY_CATCH (e, RETURN_MASK_ERROR)
13582 {
13583 new_loc->cond = parse_exp_1 (&s, block_for_pc (sals.sals[i].pc),
13584 0);
13585 }
13586 if (e.reason < 0)
13587 {
13588 warning (_("failed to reevaluate condition "
13589 "for breakpoint %d: %s"),
13590 b->number, e.message);
13591 new_loc->enabled = 0;
13592 }
13593 }
13594
13595 if (sals_end.nelts)
13596 {
13597 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
13598
13599 new_loc->length = end - sals.sals[0].pc + 1;
13600 }
13601 }
13602
13603 /* Update locations of permanent breakpoints. */
13604 if (b->enable_state == bp_permanent)
13605 make_breakpoint_permanent (b);
13606
13607 /* If possible, carry over 'disable' status from existing
13608 breakpoints. */
13609 {
13610 struct bp_location *e = existing_locations;
13611 /* If there are multiple breakpoints with the same function name,
13612 e.g. for inline functions, comparing function names won't work.
13613 Instead compare pc addresses; this is just a heuristic as things
13614 may have moved, but in practice it gives the correct answer
13615 often enough until a better solution is found. */
13616 int have_ambiguous_names = ambiguous_names_p (b->loc);
13617
13618 for (; e; e = e->next)
13619 {
13620 if (!e->enabled && e->function_name)
13621 {
13622 struct bp_location *l = b->loc;
13623 if (have_ambiguous_names)
13624 {
13625 for (; l; l = l->next)
13626 if (breakpoint_locations_match (e, l))
13627 {
13628 l->enabled = 0;
13629 break;
13630 }
13631 }
13632 else
13633 {
13634 for (; l; l = l->next)
13635 if (l->function_name
13636 && strcmp (e->function_name, l->function_name) == 0)
13637 {
13638 l->enabled = 0;
13639 break;
13640 }
13641 }
13642 }
13643 }
13644 }
13645
13646 if (!locations_are_equal (existing_locations, b->loc))
13647 observer_notify_breakpoint_modified (b);
13648
13649 update_global_location_list (1);
13650 }
13651
13652 /* Find the SaL locations corresponding to the given ADDR_STRING.
13653 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
13654
13655 static struct symtabs_and_lines
13656 addr_string_to_sals (struct breakpoint *b, char *addr_string, int *found)
13657 {
13658 char *s;
13659 struct symtabs_and_lines sals = {0};
13660 volatile struct gdb_exception e;
13661
13662 gdb_assert (b->ops != NULL);
13663 s = addr_string;
13664
13665 TRY_CATCH (e, RETURN_MASK_ERROR)
13666 {
13667 b->ops->decode_linespec (b, &s, &sals);
13668 }
13669 if (e.reason < 0)
13670 {
13671 int not_found_and_ok = 0;
13672 /* For pending breakpoints, it's expected that parsing will
13673 fail until the right shared library is loaded. User has
13674 already told to create pending breakpoints and don't need
13675 extra messages. If breakpoint is in bp_shlib_disabled
13676 state, then user already saw the message about that
13677 breakpoint being disabled, and don't want to see more
13678 errors. */
13679 if (e.error == NOT_FOUND_ERROR
13680 && (b->condition_not_parsed
13681 || (b->loc && b->loc->shlib_disabled)
13682 || (b->loc && b->loc->pspace->executing_startup)
13683 || b->enable_state == bp_disabled))
13684 not_found_and_ok = 1;
13685
13686 if (!not_found_and_ok)
13687 {
13688 /* We surely don't want to warn about the same breakpoint
13689 10 times. One solution, implemented here, is disable
13690 the breakpoint on error. Another solution would be to
13691 have separate 'warning emitted' flag. Since this
13692 happens only when a binary has changed, I don't know
13693 which approach is better. */
13694 b->enable_state = bp_disabled;
13695 throw_exception (e);
13696 }
13697 }
13698
13699 if (e.reason == 0 || e.error != NOT_FOUND_ERROR)
13700 {
13701 int i;
13702
13703 for (i = 0; i < sals.nelts; ++i)
13704 resolve_sal_pc (&sals.sals[i]);
13705 if (b->condition_not_parsed && s && s[0])
13706 {
13707 char *cond_string = 0;
13708 int thread = -1;
13709 int task = 0;
13710 char *extra_string = NULL;
13711
13712 find_condition_and_thread (s, sals.sals[0].pc,
13713 &cond_string, &thread, &task,
13714 &extra_string);
13715 if (cond_string)
13716 b->cond_string = cond_string;
13717 b->thread = thread;
13718 b->task = task;
13719 if (extra_string)
13720 b->extra_string = extra_string;
13721 b->condition_not_parsed = 0;
13722 }
13723
13724 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
13725 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
13726
13727 *found = 1;
13728 }
13729 else
13730 *found = 0;
13731
13732 return sals;
13733 }
13734
13735 /* The default re_set method, for typical hardware or software
13736 breakpoints. Reevaluate the breakpoint and recreate its
13737 locations. */
13738
13739 static void
13740 breakpoint_re_set_default (struct breakpoint *b)
13741 {
13742 int found;
13743 struct symtabs_and_lines sals, sals_end;
13744 struct symtabs_and_lines expanded = {0};
13745 struct symtabs_and_lines expanded_end = {0};
13746
13747 sals = addr_string_to_sals (b, b->addr_string, &found);
13748 if (found)
13749 {
13750 make_cleanup (xfree, sals.sals);
13751 expanded = sals;
13752 }
13753
13754 if (b->addr_string_range_end)
13755 {
13756 sals_end = addr_string_to_sals (b, b->addr_string_range_end, &found);
13757 if (found)
13758 {
13759 make_cleanup (xfree, sals_end.sals);
13760 expanded_end = sals_end;
13761 }
13762 }
13763
13764 update_breakpoint_locations (b, expanded, expanded_end);
13765 }
13766
13767 /* Default method for creating SALs from an address string. It basically
13768 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
13769
13770 static void
13771 create_sals_from_address_default (char **arg,
13772 struct linespec_result *canonical,
13773 enum bptype type_wanted,
13774 char *addr_start, char **copy_arg)
13775 {
13776 parse_breakpoint_sals (arg, canonical);
13777 }
13778
13779 /* Call create_breakpoints_sal for the given arguments. This is the default
13780 function for the `create_breakpoints_sal' method of
13781 breakpoint_ops. */
13782
13783 static void
13784 create_breakpoints_sal_default (struct gdbarch *gdbarch,
13785 struct linespec_result *canonical,
13786 struct linespec_sals *lsal,
13787 char *cond_string,
13788 char *extra_string,
13789 enum bptype type_wanted,
13790 enum bpdisp disposition,
13791 int thread,
13792 int task, int ignore_count,
13793 const struct breakpoint_ops *ops,
13794 int from_tty, int enabled,
13795 int internal, unsigned flags)
13796 {
13797 create_breakpoints_sal (gdbarch, canonical, cond_string,
13798 extra_string,
13799 type_wanted, disposition,
13800 thread, task, ignore_count, ops, from_tty,
13801 enabled, internal, flags);
13802 }
13803
13804 /* Decode the line represented by S by calling decode_line_full. This is the
13805 default function for the `decode_linespec' method of breakpoint_ops. */
13806
13807 static void
13808 decode_linespec_default (struct breakpoint *b, char **s,
13809 struct symtabs_and_lines *sals)
13810 {
13811 struct linespec_result canonical;
13812
13813 init_linespec_result (&canonical);
13814 decode_line_full (s, DECODE_LINE_FUNFIRSTLINE,
13815 (struct symtab *) NULL, 0,
13816 &canonical, multiple_symbols_all,
13817 b->filter);
13818
13819 /* We should get 0 or 1 resulting SALs. */
13820 gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
13821
13822 if (VEC_length (linespec_sals, canonical.sals) > 0)
13823 {
13824 struct linespec_sals *lsal;
13825
13826 lsal = VEC_index (linespec_sals, canonical.sals, 0);
13827 *sals = lsal->sals;
13828 /* Arrange it so the destructor does not free the
13829 contents. */
13830 lsal->sals.sals = NULL;
13831 }
13832
13833 destroy_linespec_result (&canonical);
13834 }
13835
13836 /* Prepare the global context for a re-set of breakpoint B. */
13837
13838 static struct cleanup *
13839 prepare_re_set_context (struct breakpoint *b)
13840 {
13841 struct cleanup *cleanups;
13842
13843 input_radix = b->input_radix;
13844 cleanups = save_current_space_and_thread ();
13845 if (b->pspace != NULL)
13846 switch_to_program_space_and_thread (b->pspace);
13847 set_language (b->language);
13848
13849 return cleanups;
13850 }
13851
13852 /* Reset a breakpoint given it's struct breakpoint * BINT.
13853 The value we return ends up being the return value from catch_errors.
13854 Unused in this case. */
13855
13856 static int
13857 breakpoint_re_set_one (void *bint)
13858 {
13859 /* Get past catch_errs. */
13860 struct breakpoint *b = (struct breakpoint *) bint;
13861 struct cleanup *cleanups;
13862
13863 cleanups = prepare_re_set_context (b);
13864 b->ops->re_set (b);
13865 do_cleanups (cleanups);
13866 return 0;
13867 }
13868
13869 /* Re-set all breakpoints after symbols have been re-loaded. */
13870 void
13871 breakpoint_re_set (void)
13872 {
13873 struct breakpoint *b, *b_tmp;
13874 enum language save_language;
13875 int save_input_radix;
13876 struct cleanup *old_chain;
13877
13878 save_language = current_language->la_language;
13879 save_input_radix = input_radix;
13880 old_chain = save_current_program_space ();
13881
13882 ALL_BREAKPOINTS_SAFE (b, b_tmp)
13883 {
13884 /* Format possible error msg. */
13885 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
13886 b->number);
13887 struct cleanup *cleanups = make_cleanup (xfree, message);
13888 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
13889 do_cleanups (cleanups);
13890 }
13891 set_language (save_language);
13892 input_radix = save_input_radix;
13893
13894 jit_breakpoint_re_set ();
13895
13896 do_cleanups (old_chain);
13897
13898 create_overlay_event_breakpoint ();
13899 create_longjmp_master_breakpoint ();
13900 create_std_terminate_master_breakpoint ();
13901 create_exception_master_breakpoint ();
13902
13903 /* While we're at it, reset the skip list too. */
13904 skip_re_set ();
13905 }
13906 \f
13907 /* Reset the thread number of this breakpoint:
13908
13909 - If the breakpoint is for all threads, leave it as-is.
13910 - Else, reset it to the current thread for inferior_ptid. */
13911 void
13912 breakpoint_re_set_thread (struct breakpoint *b)
13913 {
13914 if (b->thread != -1)
13915 {
13916 if (in_thread_list (inferior_ptid))
13917 b->thread = pid_to_thread_id (inferior_ptid);
13918
13919 /* We're being called after following a fork. The new fork is
13920 selected as current, and unless this was a vfork will have a
13921 different program space from the original thread. Reset that
13922 as well. */
13923 b->loc->pspace = current_program_space;
13924 }
13925 }
13926
13927 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
13928 If from_tty is nonzero, it prints a message to that effect,
13929 which ends with a period (no newline). */
13930
13931 void
13932 set_ignore_count (int bptnum, int count, int from_tty)
13933 {
13934 struct breakpoint *b;
13935
13936 if (count < 0)
13937 count = 0;
13938
13939 ALL_BREAKPOINTS (b)
13940 if (b->number == bptnum)
13941 {
13942 if (is_tracepoint (b))
13943 {
13944 if (from_tty && count != 0)
13945 printf_filtered (_("Ignore count ignored for tracepoint %d."),
13946 bptnum);
13947 return;
13948 }
13949
13950 b->ignore_count = count;
13951 if (from_tty)
13952 {
13953 if (count == 0)
13954 printf_filtered (_("Will stop next time "
13955 "breakpoint %d is reached."),
13956 bptnum);
13957 else if (count == 1)
13958 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
13959 bptnum);
13960 else
13961 printf_filtered (_("Will ignore next %d "
13962 "crossings of breakpoint %d."),
13963 count, bptnum);
13964 }
13965 breakpoints_changed ();
13966 observer_notify_breakpoint_modified (b);
13967 return;
13968 }
13969
13970 error (_("No breakpoint number %d."), bptnum);
13971 }
13972
13973 /* Command to set ignore-count of breakpoint N to COUNT. */
13974
13975 static void
13976 ignore_command (char *args, int from_tty)
13977 {
13978 char *p = args;
13979 int num;
13980
13981 if (p == 0)
13982 error_no_arg (_("a breakpoint number"));
13983
13984 num = get_number (&p);
13985 if (num == 0)
13986 error (_("bad breakpoint number: '%s'"), args);
13987 if (*p == 0)
13988 error (_("Second argument (specified ignore-count) is missing."));
13989
13990 set_ignore_count (num,
13991 longest_to_int (value_as_long (parse_and_eval (p))),
13992 from_tty);
13993 if (from_tty)
13994 printf_filtered ("\n");
13995 }
13996 \f
13997 /* Call FUNCTION on each of the breakpoints
13998 whose numbers are given in ARGS. */
13999
14000 static void
14001 map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
14002 void *),
14003 void *data)
14004 {
14005 int num;
14006 struct breakpoint *b, *tmp;
14007 int match;
14008 struct get_number_or_range_state state;
14009
14010 if (args == 0)
14011 error_no_arg (_("one or more breakpoint numbers"));
14012
14013 init_number_or_range (&state, args);
14014
14015 while (!state.finished)
14016 {
14017 char *p = state.string;
14018
14019 match = 0;
14020
14021 num = get_number_or_range (&state);
14022 if (num == 0)
14023 {
14024 warning (_("bad breakpoint number at or near '%s'"), p);
14025 }
14026 else
14027 {
14028 ALL_BREAKPOINTS_SAFE (b, tmp)
14029 if (b->number == num)
14030 {
14031 match = 1;
14032 function (b, data);
14033 break;
14034 }
14035 if (match == 0)
14036 printf_unfiltered (_("No breakpoint number %d.\n"), num);
14037 }
14038 }
14039 }
14040
14041 static struct bp_location *
14042 find_location_by_number (char *number)
14043 {
14044 char *dot = strchr (number, '.');
14045 char *p1;
14046 int bp_num;
14047 int loc_num;
14048 struct breakpoint *b;
14049 struct bp_location *loc;
14050
14051 *dot = '\0';
14052
14053 p1 = number;
14054 bp_num = get_number (&p1);
14055 if (bp_num == 0)
14056 error (_("Bad breakpoint number '%s'"), number);
14057
14058 ALL_BREAKPOINTS (b)
14059 if (b->number == bp_num)
14060 {
14061 break;
14062 }
14063
14064 if (!b || b->number != bp_num)
14065 error (_("Bad breakpoint number '%s'"), number);
14066
14067 p1 = dot+1;
14068 loc_num = get_number (&p1);
14069 if (loc_num == 0)
14070 error (_("Bad breakpoint location number '%s'"), number);
14071
14072 --loc_num;
14073 loc = b->loc;
14074 for (;loc_num && loc; --loc_num, loc = loc->next)
14075 ;
14076 if (!loc)
14077 error (_("Bad breakpoint location number '%s'"), dot+1);
14078
14079 return loc;
14080 }
14081
14082
14083 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14084 If from_tty is nonzero, it prints a message to that effect,
14085 which ends with a period (no newline). */
14086
14087 void
14088 disable_breakpoint (struct breakpoint *bpt)
14089 {
14090 /* Never disable a watchpoint scope breakpoint; we want to
14091 hit them when we leave scope so we can delete both the
14092 watchpoint and its scope breakpoint at that time. */
14093 if (bpt->type == bp_watchpoint_scope)
14094 return;
14095
14096 /* You can't disable permanent breakpoints. */
14097 if (bpt->enable_state == bp_permanent)
14098 return;
14099
14100 bpt->enable_state = bp_disabled;
14101
14102 /* Mark breakpoint locations modified. */
14103 mark_breakpoint_modified (bpt);
14104
14105 if (target_supports_enable_disable_tracepoint ()
14106 && current_trace_status ()->running && is_tracepoint (bpt))
14107 {
14108 struct bp_location *location;
14109
14110 for (location = bpt->loc; location; location = location->next)
14111 target_disable_tracepoint (location);
14112 }
14113
14114 update_global_location_list (0);
14115
14116 observer_notify_breakpoint_modified (bpt);
14117 }
14118
14119 /* A callback for iterate_over_related_breakpoints. */
14120
14121 static void
14122 do_disable_breakpoint (struct breakpoint *b, void *ignore)
14123 {
14124 disable_breakpoint (b);
14125 }
14126
14127 /* A callback for map_breakpoint_numbers that calls
14128 disable_breakpoint. */
14129
14130 static void
14131 do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
14132 {
14133 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
14134 }
14135
14136 static void
14137 disable_command (char *args, int from_tty)
14138 {
14139 if (args == 0)
14140 {
14141 struct breakpoint *bpt;
14142
14143 ALL_BREAKPOINTS (bpt)
14144 if (user_breakpoint_p (bpt))
14145 disable_breakpoint (bpt);
14146 }
14147 else if (strchr (args, '.'))
14148 {
14149 struct bp_location *loc = find_location_by_number (args);
14150 if (loc)
14151 {
14152 if (loc->enabled)
14153 {
14154 loc->enabled = 0;
14155 mark_breakpoint_location_modified (loc);
14156 }
14157 if (target_supports_enable_disable_tracepoint ()
14158 && current_trace_status ()->running && loc->owner
14159 && is_tracepoint (loc->owner))
14160 target_disable_tracepoint (loc);
14161 }
14162 update_global_location_list (0);
14163 }
14164 else
14165 map_breakpoint_numbers (args, do_map_disable_breakpoint, NULL);
14166 }
14167
14168 static void
14169 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14170 int count)
14171 {
14172 int target_resources_ok;
14173
14174 if (bpt->type == bp_hardware_breakpoint)
14175 {
14176 int i;
14177 i = hw_breakpoint_used_count ();
14178 target_resources_ok =
14179 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
14180 i + 1, 0);
14181 if (target_resources_ok == 0)
14182 error (_("No hardware breakpoint support in the target."));
14183 else if (target_resources_ok < 0)
14184 error (_("Hardware breakpoints used exceeds limit."));
14185 }
14186
14187 if (is_watchpoint (bpt))
14188 {
14189 /* Initialize it just to avoid a GCC false warning. */
14190 enum enable_state orig_enable_state = 0;
14191 volatile struct gdb_exception e;
14192
14193 TRY_CATCH (e, RETURN_MASK_ALL)
14194 {
14195 struct watchpoint *w = (struct watchpoint *) bpt;
14196
14197 orig_enable_state = bpt->enable_state;
14198 bpt->enable_state = bp_enabled;
14199 update_watchpoint (w, 1 /* reparse */);
14200 }
14201 if (e.reason < 0)
14202 {
14203 bpt->enable_state = orig_enable_state;
14204 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14205 bpt->number);
14206 return;
14207 }
14208 }
14209
14210 if (bpt->enable_state != bp_permanent)
14211 bpt->enable_state = bp_enabled;
14212
14213 bpt->enable_state = bp_enabled;
14214
14215 /* Mark breakpoint locations modified. */
14216 mark_breakpoint_modified (bpt);
14217
14218 if (target_supports_enable_disable_tracepoint ()
14219 && current_trace_status ()->running && is_tracepoint (bpt))
14220 {
14221 struct bp_location *location;
14222
14223 for (location = bpt->loc; location; location = location->next)
14224 target_enable_tracepoint (location);
14225 }
14226
14227 bpt->disposition = disposition;
14228 bpt->enable_count = count;
14229 update_global_location_list (1);
14230 breakpoints_changed ();
14231
14232 observer_notify_breakpoint_modified (bpt);
14233 }
14234
14235
14236 void
14237 enable_breakpoint (struct breakpoint *bpt)
14238 {
14239 enable_breakpoint_disp (bpt, bpt->disposition, 0);
14240 }
14241
14242 static void
14243 do_enable_breakpoint (struct breakpoint *bpt, void *arg)
14244 {
14245 enable_breakpoint (bpt);
14246 }
14247
14248 /* A callback for map_breakpoint_numbers that calls
14249 enable_breakpoint. */
14250
14251 static void
14252 do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
14253 {
14254 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
14255 }
14256
14257 /* The enable command enables the specified breakpoints (or all defined
14258 breakpoints) so they once again become (or continue to be) effective
14259 in stopping the inferior. */
14260
14261 static void
14262 enable_command (char *args, int from_tty)
14263 {
14264 if (args == 0)
14265 {
14266 struct breakpoint *bpt;
14267
14268 ALL_BREAKPOINTS (bpt)
14269 if (user_breakpoint_p (bpt))
14270 enable_breakpoint (bpt);
14271 }
14272 else if (strchr (args, '.'))
14273 {
14274 struct bp_location *loc = find_location_by_number (args);
14275 if (loc)
14276 {
14277 if (!loc->enabled)
14278 {
14279 loc->enabled = 1;
14280 mark_breakpoint_location_modified (loc);
14281 }
14282 if (target_supports_enable_disable_tracepoint ()
14283 && current_trace_status ()->running && loc->owner
14284 && is_tracepoint (loc->owner))
14285 target_enable_tracepoint (loc);
14286 }
14287 update_global_location_list (1);
14288 }
14289 else
14290 map_breakpoint_numbers (args, do_map_enable_breakpoint, NULL);
14291 }
14292
14293 /* This struct packages up disposition data for application to multiple
14294 breakpoints. */
14295
14296 struct disp_data
14297 {
14298 enum bpdisp disp;
14299 int count;
14300 };
14301
14302 static void
14303 do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
14304 {
14305 struct disp_data disp_data = *(struct disp_data *) arg;
14306
14307 enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
14308 }
14309
14310 static void
14311 do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
14312 {
14313 struct disp_data disp = { disp_disable, 1 };
14314
14315 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14316 }
14317
14318 static void
14319 enable_once_command (char *args, int from_tty)
14320 {
14321 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
14322 }
14323
14324 static void
14325 do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
14326 {
14327 struct disp_data disp = { disp_disable, *(int *) countptr };
14328
14329 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14330 }
14331
14332 static void
14333 enable_count_command (char *args, int from_tty)
14334 {
14335 int count = get_number (&args);
14336
14337 map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
14338 }
14339
14340 static void
14341 do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
14342 {
14343 struct disp_data disp = { disp_del, 1 };
14344
14345 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14346 }
14347
14348 static void
14349 enable_delete_command (char *args, int from_tty)
14350 {
14351 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
14352 }
14353 \f
14354 static void
14355 set_breakpoint_cmd (char *args, int from_tty)
14356 {
14357 }
14358
14359 static void
14360 show_breakpoint_cmd (char *args, int from_tty)
14361 {
14362 }
14363
14364 /* Invalidate last known value of any hardware watchpoint if
14365 the memory which that value represents has been written to by
14366 GDB itself. */
14367
14368 static void
14369 invalidate_bp_value_on_memory_change (CORE_ADDR addr, int len,
14370 const bfd_byte *data)
14371 {
14372 struct breakpoint *bp;
14373
14374 ALL_BREAKPOINTS (bp)
14375 if (bp->enable_state == bp_enabled
14376 && bp->type == bp_hardware_watchpoint)
14377 {
14378 struct watchpoint *wp = (struct watchpoint *) bp;
14379
14380 if (wp->val_valid && wp->val)
14381 {
14382 struct bp_location *loc;
14383
14384 for (loc = bp->loc; loc != NULL; loc = loc->next)
14385 if (loc->loc_type == bp_loc_hardware_watchpoint
14386 && loc->address + loc->length > addr
14387 && addr + len > loc->address)
14388 {
14389 value_free (wp->val);
14390 wp->val = NULL;
14391 wp->val_valid = 0;
14392 }
14393 }
14394 }
14395 }
14396
14397 /* Use the last displayed codepoint's values, or nothing
14398 if they aren't valid. */
14399
14400 struct symtabs_and_lines
14401 decode_line_spec_1 (char *string, int flags)
14402 {
14403 struct symtabs_and_lines sals;
14404
14405 if (string == 0)
14406 error (_("Empty line specification."));
14407 if (last_displayed_sal_is_valid ())
14408 sals = decode_line_1 (&string, flags,
14409 get_last_displayed_symtab (),
14410 get_last_displayed_line ());
14411 else
14412 sals = decode_line_1 (&string, flags, (struct symtab *) NULL, 0);
14413 if (*string)
14414 error (_("Junk at end of line specification: %s"), string);
14415 return sals;
14416 }
14417
14418 /* Create and insert a raw software breakpoint at PC. Return an
14419 identifier, which should be used to remove the breakpoint later.
14420 In general, places which call this should be using something on the
14421 breakpoint chain instead; this function should be eliminated
14422 someday. */
14423
14424 void *
14425 deprecated_insert_raw_breakpoint (struct gdbarch *gdbarch,
14426 struct address_space *aspace, CORE_ADDR pc)
14427 {
14428 struct bp_target_info *bp_tgt;
14429
14430 bp_tgt = XZALLOC (struct bp_target_info);
14431
14432 bp_tgt->placed_address_space = aspace;
14433 bp_tgt->placed_address = pc;
14434
14435 if (target_insert_breakpoint (gdbarch, bp_tgt) != 0)
14436 {
14437 /* Could not insert the breakpoint. */
14438 xfree (bp_tgt);
14439 return NULL;
14440 }
14441
14442 return bp_tgt;
14443 }
14444
14445 /* Remove a breakpoint BP inserted by
14446 deprecated_insert_raw_breakpoint. */
14447
14448 int
14449 deprecated_remove_raw_breakpoint (struct gdbarch *gdbarch, void *bp)
14450 {
14451 struct bp_target_info *bp_tgt = bp;
14452 int ret;
14453
14454 ret = target_remove_breakpoint (gdbarch, bp_tgt);
14455 xfree (bp_tgt);
14456
14457 return ret;
14458 }
14459
14460 /* One (or perhaps two) breakpoints used for software single
14461 stepping. */
14462
14463 static void *single_step_breakpoints[2];
14464 static struct gdbarch *single_step_gdbarch[2];
14465
14466 /* Create and insert a breakpoint for software single step. */
14467
14468 void
14469 insert_single_step_breakpoint (struct gdbarch *gdbarch,
14470 struct address_space *aspace,
14471 CORE_ADDR next_pc)
14472 {
14473 void **bpt_p;
14474
14475 if (single_step_breakpoints[0] == NULL)
14476 {
14477 bpt_p = &single_step_breakpoints[0];
14478 single_step_gdbarch[0] = gdbarch;
14479 }
14480 else
14481 {
14482 gdb_assert (single_step_breakpoints[1] == NULL);
14483 bpt_p = &single_step_breakpoints[1];
14484 single_step_gdbarch[1] = gdbarch;
14485 }
14486
14487 /* NOTE drow/2006-04-11: A future improvement to this function would
14488 be to only create the breakpoints once, and actually put them on
14489 the breakpoint chain. That would let us use set_raw_breakpoint.
14490 We could adjust the addresses each time they were needed. Doing
14491 this requires corresponding changes elsewhere where single step
14492 breakpoints are handled, however. So, for now, we use this. */
14493
14494 *bpt_p = deprecated_insert_raw_breakpoint (gdbarch, aspace, next_pc);
14495 if (*bpt_p == NULL)
14496 error (_("Could not insert single-step breakpoint at %s"),
14497 paddress (gdbarch, next_pc));
14498 }
14499
14500 /* Check if the breakpoints used for software single stepping
14501 were inserted or not. */
14502
14503 int
14504 single_step_breakpoints_inserted (void)
14505 {
14506 return (single_step_breakpoints[0] != NULL
14507 || single_step_breakpoints[1] != NULL);
14508 }
14509
14510 /* Remove and delete any breakpoints used for software single step. */
14511
14512 void
14513 remove_single_step_breakpoints (void)
14514 {
14515 gdb_assert (single_step_breakpoints[0] != NULL);
14516
14517 /* See insert_single_step_breakpoint for more about this deprecated
14518 call. */
14519 deprecated_remove_raw_breakpoint (single_step_gdbarch[0],
14520 single_step_breakpoints[0]);
14521 single_step_gdbarch[0] = NULL;
14522 single_step_breakpoints[0] = NULL;
14523
14524 if (single_step_breakpoints[1] != NULL)
14525 {
14526 deprecated_remove_raw_breakpoint (single_step_gdbarch[1],
14527 single_step_breakpoints[1]);
14528 single_step_gdbarch[1] = NULL;
14529 single_step_breakpoints[1] = NULL;
14530 }
14531 }
14532
14533 /* Delete software single step breakpoints without removing them from
14534 the inferior. This is intended to be used if the inferior's address
14535 space where they were inserted is already gone, e.g. after exit or
14536 exec. */
14537
14538 void
14539 cancel_single_step_breakpoints (void)
14540 {
14541 int i;
14542
14543 for (i = 0; i < 2; i++)
14544 if (single_step_breakpoints[i])
14545 {
14546 xfree (single_step_breakpoints[i]);
14547 single_step_breakpoints[i] = NULL;
14548 single_step_gdbarch[i] = NULL;
14549 }
14550 }
14551
14552 /* Detach software single-step breakpoints from INFERIOR_PTID without
14553 removing them. */
14554
14555 static void
14556 detach_single_step_breakpoints (void)
14557 {
14558 int i;
14559
14560 for (i = 0; i < 2; i++)
14561 if (single_step_breakpoints[i])
14562 target_remove_breakpoint (single_step_gdbarch[i],
14563 single_step_breakpoints[i]);
14564 }
14565
14566 /* Check whether a software single-step breakpoint is inserted at
14567 PC. */
14568
14569 static int
14570 single_step_breakpoint_inserted_here_p (struct address_space *aspace,
14571 CORE_ADDR pc)
14572 {
14573 int i;
14574
14575 for (i = 0; i < 2; i++)
14576 {
14577 struct bp_target_info *bp_tgt = single_step_breakpoints[i];
14578 if (bp_tgt
14579 && breakpoint_address_match (bp_tgt->placed_address_space,
14580 bp_tgt->placed_address,
14581 aspace, pc))
14582 return 1;
14583 }
14584
14585 return 0;
14586 }
14587
14588 /* Returns 0 if 'bp' is NOT a syscall catchpoint,
14589 non-zero otherwise. */
14590 static int
14591 is_syscall_catchpoint_enabled (struct breakpoint *bp)
14592 {
14593 if (syscall_catchpoint_p (bp)
14594 && bp->enable_state != bp_disabled
14595 && bp->enable_state != bp_call_disabled)
14596 return 1;
14597 else
14598 return 0;
14599 }
14600
14601 int
14602 catch_syscall_enabled (void)
14603 {
14604 struct catch_syscall_inferior_data *inf_data
14605 = get_catch_syscall_inferior_data (current_inferior ());
14606
14607 return inf_data->total_syscalls_count != 0;
14608 }
14609
14610 int
14611 catching_syscall_number (int syscall_number)
14612 {
14613 struct breakpoint *bp;
14614
14615 ALL_BREAKPOINTS (bp)
14616 if (is_syscall_catchpoint_enabled (bp))
14617 {
14618 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bp;
14619
14620 if (c->syscalls_to_be_caught)
14621 {
14622 int i, iter;
14623 for (i = 0;
14624 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
14625 i++)
14626 if (syscall_number == iter)
14627 return 1;
14628 }
14629 else
14630 return 1;
14631 }
14632
14633 return 0;
14634 }
14635
14636 /* Complete syscall names. Used by "catch syscall". */
14637 static VEC (char_ptr) *
14638 catch_syscall_completer (struct cmd_list_element *cmd,
14639 char *text, char *word)
14640 {
14641 const char **list = get_syscall_names ();
14642 VEC (char_ptr) *retlist
14643 = (list == NULL) ? NULL : complete_on_enum (list, text, word);
14644
14645 xfree (list);
14646 return retlist;
14647 }
14648
14649 /* Tracepoint-specific operations. */
14650
14651 /* Set tracepoint count to NUM. */
14652 static void
14653 set_tracepoint_count (int num)
14654 {
14655 tracepoint_count = num;
14656 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
14657 }
14658
14659 static void
14660 trace_command (char *arg, int from_tty)
14661 {
14662 struct breakpoint_ops *ops;
14663 const char *arg_cp = arg;
14664
14665 if (arg && probe_linespec_to_ops (&arg_cp))
14666 ops = &tracepoint_probe_breakpoint_ops;
14667 else
14668 ops = &tracepoint_breakpoint_ops;
14669
14670 if (create_breakpoint (get_current_arch (),
14671 arg,
14672 NULL, 0, NULL, 1 /* parse arg */,
14673 0 /* tempflag */,
14674 bp_tracepoint /* type_wanted */,
14675 0 /* Ignore count */,
14676 pending_break_support,
14677 ops,
14678 from_tty,
14679 1 /* enabled */,
14680 0 /* internal */, 0))
14681 set_tracepoint_count (breakpoint_count);
14682 }
14683
14684 static void
14685 ftrace_command (char *arg, int from_tty)
14686 {
14687 if (create_breakpoint (get_current_arch (),
14688 arg,
14689 NULL, 0, NULL, 1 /* parse arg */,
14690 0 /* tempflag */,
14691 bp_fast_tracepoint /* type_wanted */,
14692 0 /* Ignore count */,
14693 pending_break_support,
14694 &tracepoint_breakpoint_ops,
14695 from_tty,
14696 1 /* enabled */,
14697 0 /* internal */, 0))
14698 set_tracepoint_count (breakpoint_count);
14699 }
14700
14701 /* strace command implementation. Creates a static tracepoint. */
14702
14703 static void
14704 strace_command (char *arg, int from_tty)
14705 {
14706 struct breakpoint_ops *ops;
14707
14708 /* Decide if we are dealing with a static tracepoint marker (`-m'),
14709 or with a normal static tracepoint. */
14710 if (arg && strncmp (arg, "-m", 2) == 0 && isspace (arg[2]))
14711 ops = &strace_marker_breakpoint_ops;
14712 else
14713 ops = &tracepoint_breakpoint_ops;
14714
14715 if (create_breakpoint (get_current_arch (),
14716 arg,
14717 NULL, 0, NULL, 1 /* parse arg */,
14718 0 /* tempflag */,
14719 bp_static_tracepoint /* type_wanted */,
14720 0 /* Ignore count */,
14721 pending_break_support,
14722 ops,
14723 from_tty,
14724 1 /* enabled */,
14725 0 /* internal */, 0))
14726 set_tracepoint_count (breakpoint_count);
14727 }
14728
14729 /* Set up a fake reader function that gets command lines from a linked
14730 list that was acquired during tracepoint uploading. */
14731
14732 static struct uploaded_tp *this_utp;
14733 static int next_cmd;
14734
14735 static char *
14736 read_uploaded_action (void)
14737 {
14738 char *rslt;
14739
14740 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
14741
14742 next_cmd++;
14743
14744 return rslt;
14745 }
14746
14747 /* Given information about a tracepoint as recorded on a target (which
14748 can be either a live system or a trace file), attempt to create an
14749 equivalent GDB tracepoint. This is not a reliable process, since
14750 the target does not necessarily have all the information used when
14751 the tracepoint was originally defined. */
14752
14753 struct tracepoint *
14754 create_tracepoint_from_upload (struct uploaded_tp *utp)
14755 {
14756 char *addr_str, small_buf[100];
14757 struct tracepoint *tp;
14758
14759 if (utp->at_string)
14760 addr_str = utp->at_string;
14761 else
14762 {
14763 /* In the absence of a source location, fall back to raw
14764 address. Since there is no way to confirm that the address
14765 means the same thing as when the trace was started, warn the
14766 user. */
14767 warning (_("Uploaded tracepoint %d has no "
14768 "source location, using raw address"),
14769 utp->number);
14770 sprintf (small_buf, "*%s", hex_string (utp->addr));
14771 addr_str = small_buf;
14772 }
14773
14774 /* There's not much we can do with a sequence of bytecodes. */
14775 if (utp->cond && !utp->cond_string)
14776 warning (_("Uploaded tracepoint %d condition "
14777 "has no source form, ignoring it"),
14778 utp->number);
14779
14780 if (!create_breakpoint (get_current_arch (),
14781 addr_str,
14782 utp->cond_string, -1, NULL,
14783 0 /* parse cond/thread */,
14784 0 /* tempflag */,
14785 utp->type /* type_wanted */,
14786 0 /* Ignore count */,
14787 pending_break_support,
14788 &tracepoint_breakpoint_ops,
14789 0 /* from_tty */,
14790 utp->enabled /* enabled */,
14791 0 /* internal */,
14792 CREATE_BREAKPOINT_FLAGS_INSERTED))
14793 return NULL;
14794
14795 set_tracepoint_count (breakpoint_count);
14796
14797 /* Get the tracepoint we just created. */
14798 tp = get_tracepoint (tracepoint_count);
14799 gdb_assert (tp != NULL);
14800
14801 if (utp->pass > 0)
14802 {
14803 sprintf (small_buf, "%d %d", utp->pass, tp->base.number);
14804
14805 trace_pass_command (small_buf, 0);
14806 }
14807
14808 /* If we have uploaded versions of the original commands, set up a
14809 special-purpose "reader" function and call the usual command line
14810 reader, then pass the result to the breakpoint command-setting
14811 function. */
14812 if (!VEC_empty (char_ptr, utp->cmd_strings))
14813 {
14814 struct command_line *cmd_list;
14815
14816 this_utp = utp;
14817 next_cmd = 0;
14818
14819 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
14820
14821 breakpoint_set_commands (&tp->base, cmd_list);
14822 }
14823 else if (!VEC_empty (char_ptr, utp->actions)
14824 || !VEC_empty (char_ptr, utp->step_actions))
14825 warning (_("Uploaded tracepoint %d actions "
14826 "have no source form, ignoring them"),
14827 utp->number);
14828
14829 /* Copy any status information that might be available. */
14830 tp->base.hit_count = utp->hit_count;
14831 tp->traceframe_usage = utp->traceframe_usage;
14832
14833 return tp;
14834 }
14835
14836 /* Print information on tracepoint number TPNUM_EXP, or all if
14837 omitted. */
14838
14839 static void
14840 tracepoints_info (char *args, int from_tty)
14841 {
14842 struct ui_out *uiout = current_uiout;
14843 int num_printed;
14844
14845 num_printed = breakpoint_1 (args, 0, is_tracepoint);
14846
14847 if (num_printed == 0)
14848 {
14849 if (args == NULL || *args == '\0')
14850 ui_out_message (uiout, 0, "No tracepoints.\n");
14851 else
14852 ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
14853 }
14854
14855 default_collect_info ();
14856 }
14857
14858 /* The 'enable trace' command enables tracepoints.
14859 Not supported by all targets. */
14860 static void
14861 enable_trace_command (char *args, int from_tty)
14862 {
14863 enable_command (args, from_tty);
14864 }
14865
14866 /* The 'disable trace' command disables tracepoints.
14867 Not supported by all targets. */
14868 static void
14869 disable_trace_command (char *args, int from_tty)
14870 {
14871 disable_command (args, from_tty);
14872 }
14873
14874 /* Remove a tracepoint (or all if no argument). */
14875 static void
14876 delete_trace_command (char *arg, int from_tty)
14877 {
14878 struct breakpoint *b, *b_tmp;
14879
14880 dont_repeat ();
14881
14882 if (arg == 0)
14883 {
14884 int breaks_to_delete = 0;
14885
14886 /* Delete all breakpoints if no argument.
14887 Do not delete internal or call-dummy breakpoints, these
14888 have to be deleted with an explicit breakpoint number
14889 argument. */
14890 ALL_TRACEPOINTS (b)
14891 if (is_tracepoint (b) && user_breakpoint_p (b))
14892 {
14893 breaks_to_delete = 1;
14894 break;
14895 }
14896
14897 /* Ask user only if there are some breakpoints to delete. */
14898 if (!from_tty
14899 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
14900 {
14901 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14902 if (is_tracepoint (b) && user_breakpoint_p (b))
14903 delete_breakpoint (b);
14904 }
14905 }
14906 else
14907 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
14908 }
14909
14910 /* Helper function for trace_pass_command. */
14911
14912 static void
14913 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
14914 {
14915 tp->pass_count = count;
14916 observer_notify_tracepoint_modified (tp->base.number);
14917 if (from_tty)
14918 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
14919 tp->base.number, count);
14920 }
14921
14922 /* Set passcount for tracepoint.
14923
14924 First command argument is passcount, second is tracepoint number.
14925 If tracepoint number omitted, apply to most recently defined.
14926 Also accepts special argument "all". */
14927
14928 static void
14929 trace_pass_command (char *args, int from_tty)
14930 {
14931 struct tracepoint *t1;
14932 unsigned int count;
14933
14934 if (args == 0 || *args == 0)
14935 error (_("passcount command requires an "
14936 "argument (count + optional TP num)"));
14937
14938 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
14939
14940 while (*args && isspace ((int) *args))
14941 args++;
14942
14943 if (*args && strncasecmp (args, "all", 3) == 0)
14944 {
14945 struct breakpoint *b;
14946
14947 args += 3; /* Skip special argument "all". */
14948 if (*args)
14949 error (_("Junk at end of arguments."));
14950
14951 ALL_TRACEPOINTS (b)
14952 {
14953 t1 = (struct tracepoint *) b;
14954 trace_pass_set_count (t1, count, from_tty);
14955 }
14956 }
14957 else if (*args == '\0')
14958 {
14959 t1 = get_tracepoint_by_number (&args, NULL, 1);
14960 if (t1)
14961 trace_pass_set_count (t1, count, from_tty);
14962 }
14963 else
14964 {
14965 struct get_number_or_range_state state;
14966
14967 init_number_or_range (&state, args);
14968 while (!state.finished)
14969 {
14970 t1 = get_tracepoint_by_number (&args, &state, 1);
14971 if (t1)
14972 trace_pass_set_count (t1, count, from_tty);
14973 }
14974 }
14975 }
14976
14977 struct tracepoint *
14978 get_tracepoint (int num)
14979 {
14980 struct breakpoint *t;
14981
14982 ALL_TRACEPOINTS (t)
14983 if (t->number == num)
14984 return (struct tracepoint *) t;
14985
14986 return NULL;
14987 }
14988
14989 /* Find the tracepoint with the given target-side number (which may be
14990 different from the tracepoint number after disconnecting and
14991 reconnecting). */
14992
14993 struct tracepoint *
14994 get_tracepoint_by_number_on_target (int num)
14995 {
14996 struct breakpoint *b;
14997
14998 ALL_TRACEPOINTS (b)
14999 {
15000 struct tracepoint *t = (struct tracepoint *) b;
15001
15002 if (t->number_on_target == num)
15003 return t;
15004 }
15005
15006 return NULL;
15007 }
15008
15009 /* Utility: parse a tracepoint number and look it up in the list.
15010 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
15011 If OPTIONAL_P is true, then if the argument is missing, the most
15012 recent tracepoint (tracepoint_count) is returned. */
15013 struct tracepoint *
15014 get_tracepoint_by_number (char **arg,
15015 struct get_number_or_range_state *state,
15016 int optional_p)
15017 {
15018 extern int tracepoint_count;
15019 struct breakpoint *t;
15020 int tpnum;
15021 char *instring = arg == NULL ? NULL : *arg;
15022
15023 if (state)
15024 {
15025 gdb_assert (!state->finished);
15026 tpnum = get_number_or_range (state);
15027 }
15028 else if (arg == NULL || *arg == NULL || ! **arg)
15029 {
15030 if (optional_p)
15031 tpnum = tracepoint_count;
15032 else
15033 error_no_arg (_("tracepoint number"));
15034 }
15035 else
15036 tpnum = get_number (arg);
15037
15038 if (tpnum <= 0)
15039 {
15040 if (instring && *instring)
15041 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15042 instring);
15043 else
15044 printf_filtered (_("Tracepoint argument missing "
15045 "and no previous tracepoint\n"));
15046 return NULL;
15047 }
15048
15049 ALL_TRACEPOINTS (t)
15050 if (t->number == tpnum)
15051 {
15052 return (struct tracepoint *) t;
15053 }
15054
15055 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15056 return NULL;
15057 }
15058
15059 void
15060 print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15061 {
15062 if (b->thread != -1)
15063 fprintf_unfiltered (fp, " thread %d", b->thread);
15064
15065 if (b->task != 0)
15066 fprintf_unfiltered (fp, " task %d", b->task);
15067
15068 fprintf_unfiltered (fp, "\n");
15069 }
15070
15071 /* Save information on user settable breakpoints (watchpoints, etc) to
15072 a new script file named FILENAME. If FILTER is non-NULL, call it
15073 on each breakpoint and only include the ones for which it returns
15074 non-zero. */
15075
15076 static void
15077 save_breakpoints (char *filename, int from_tty,
15078 int (*filter) (const struct breakpoint *))
15079 {
15080 struct breakpoint *tp;
15081 int any = 0;
15082 char *pathname;
15083 struct cleanup *cleanup;
15084 struct ui_file *fp;
15085 int extra_trace_bits = 0;
15086
15087 if (filename == 0 || *filename == 0)
15088 error (_("Argument required (file name in which to save)"));
15089
15090 /* See if we have anything to save. */
15091 ALL_BREAKPOINTS (tp)
15092 {
15093 /* Skip internal and momentary breakpoints. */
15094 if (!user_breakpoint_p (tp))
15095 continue;
15096
15097 /* If we have a filter, only save the breakpoints it accepts. */
15098 if (filter && !filter (tp))
15099 continue;
15100
15101 any = 1;
15102
15103 if (is_tracepoint (tp))
15104 {
15105 extra_trace_bits = 1;
15106
15107 /* We can stop searching. */
15108 break;
15109 }
15110 }
15111
15112 if (!any)
15113 {
15114 warning (_("Nothing to save."));
15115 return;
15116 }
15117
15118 pathname = tilde_expand (filename);
15119 cleanup = make_cleanup (xfree, pathname);
15120 fp = gdb_fopen (pathname, "w");
15121 if (!fp)
15122 error (_("Unable to open file '%s' for saving (%s)"),
15123 filename, safe_strerror (errno));
15124 make_cleanup_ui_file_delete (fp);
15125
15126 if (extra_trace_bits)
15127 save_trace_state_variables (fp);
15128
15129 ALL_BREAKPOINTS (tp)
15130 {
15131 /* Skip internal and momentary breakpoints. */
15132 if (!user_breakpoint_p (tp))
15133 continue;
15134
15135 /* If we have a filter, only save the breakpoints it accepts. */
15136 if (filter && !filter (tp))
15137 continue;
15138
15139 tp->ops->print_recreate (tp, fp);
15140
15141 /* Note, we can't rely on tp->number for anything, as we can't
15142 assume the recreated breakpoint numbers will match. Use $bpnum
15143 instead. */
15144
15145 if (tp->cond_string)
15146 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
15147
15148 if (tp->ignore_count)
15149 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
15150
15151 if (tp->commands)
15152 {
15153 volatile struct gdb_exception ex;
15154
15155 fprintf_unfiltered (fp, " commands\n");
15156
15157 ui_out_redirect (current_uiout, fp);
15158 TRY_CATCH (ex, RETURN_MASK_ALL)
15159 {
15160 print_command_lines (current_uiout, tp->commands->commands, 2);
15161 }
15162 ui_out_redirect (current_uiout, NULL);
15163
15164 if (ex.reason < 0)
15165 throw_exception (ex);
15166
15167 fprintf_unfiltered (fp, " end\n");
15168 }
15169
15170 if (tp->enable_state == bp_disabled)
15171 fprintf_unfiltered (fp, "disable\n");
15172
15173 /* If this is a multi-location breakpoint, check if the locations
15174 should be individually disabled. Watchpoint locations are
15175 special, and not user visible. */
15176 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15177 {
15178 struct bp_location *loc;
15179 int n = 1;
15180
15181 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15182 if (!loc->enabled)
15183 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
15184 }
15185 }
15186
15187 if (extra_trace_bits && *default_collect)
15188 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
15189
15190 do_cleanups (cleanup);
15191 if (from_tty)
15192 printf_filtered (_("Saved to file '%s'.\n"), filename);
15193 }
15194
15195 /* The `save breakpoints' command. */
15196
15197 static void
15198 save_breakpoints_command (char *args, int from_tty)
15199 {
15200 save_breakpoints (args, from_tty, NULL);
15201 }
15202
15203 /* The `save tracepoints' command. */
15204
15205 static void
15206 save_tracepoints_command (char *args, int from_tty)
15207 {
15208 save_breakpoints (args, from_tty, is_tracepoint);
15209 }
15210
15211 /* Create a vector of all tracepoints. */
15212
15213 VEC(breakpoint_p) *
15214 all_tracepoints (void)
15215 {
15216 VEC(breakpoint_p) *tp_vec = 0;
15217 struct breakpoint *tp;
15218
15219 ALL_TRACEPOINTS (tp)
15220 {
15221 VEC_safe_push (breakpoint_p, tp_vec, tp);
15222 }
15223
15224 return tp_vec;
15225 }
15226
15227 \f
15228 /* This help string is used for the break, hbreak, tbreak and thbreak
15229 commands. It is defined as a macro to prevent duplication.
15230 COMMAND should be a string constant containing the name of the
15231 command. */
15232 #define BREAK_ARGS_HELP(command) \
15233 command" [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15234 LOCATION may be a line number, function name, or \"*\" and an address.\n\
15235 If a line number is specified, break at start of code for that line.\n\
15236 If a function is specified, break at start of code for that function.\n\
15237 If an address is specified, break at that exact address.\n\
15238 With no LOCATION, uses current execution address of the selected\n\
15239 stack frame. This is useful for breaking on return to a stack frame.\n\
15240 \n\
15241 THREADNUM is the number from \"info threads\".\n\
15242 CONDITION is a boolean expression.\n\
15243 \n\
15244 Multiple breakpoints at one place are permitted, and useful if their\n\
15245 conditions are different.\n\
15246 \n\
15247 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15248
15249 /* List of subcommands for "catch". */
15250 static struct cmd_list_element *catch_cmdlist;
15251
15252 /* List of subcommands for "tcatch". */
15253 static struct cmd_list_element *tcatch_cmdlist;
15254
15255 void
15256 add_catch_command (char *name, char *docstring,
15257 void (*sfunc) (char *args, int from_tty,
15258 struct cmd_list_element *command),
15259 completer_ftype *completer,
15260 void *user_data_catch,
15261 void *user_data_tcatch)
15262 {
15263 struct cmd_list_element *command;
15264
15265 command = add_cmd (name, class_breakpoint, NULL, docstring,
15266 &catch_cmdlist);
15267 set_cmd_sfunc (command, sfunc);
15268 set_cmd_context (command, user_data_catch);
15269 set_cmd_completer (command, completer);
15270
15271 command = add_cmd (name, class_breakpoint, NULL, docstring,
15272 &tcatch_cmdlist);
15273 set_cmd_sfunc (command, sfunc);
15274 set_cmd_context (command, user_data_tcatch);
15275 set_cmd_completer (command, completer);
15276 }
15277
15278 static void
15279 clear_syscall_counts (struct inferior *inf)
15280 {
15281 struct catch_syscall_inferior_data *inf_data
15282 = get_catch_syscall_inferior_data (inf);
15283
15284 inf_data->total_syscalls_count = 0;
15285 inf_data->any_syscall_count = 0;
15286 VEC_free (int, inf_data->syscalls_counts);
15287 }
15288
15289 static void
15290 save_command (char *arg, int from_tty)
15291 {
15292 printf_unfiltered (_("\"save\" must be followed by "
15293 "the name of a save subcommand.\n"));
15294 help_list (save_cmdlist, "save ", -1, gdb_stdout);
15295 }
15296
15297 struct breakpoint *
15298 iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15299 void *data)
15300 {
15301 struct breakpoint *b, *b_tmp;
15302
15303 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15304 {
15305 if ((*callback) (b, data))
15306 return b;
15307 }
15308
15309 return NULL;
15310 }
15311
15312 /* Zero if any of the breakpoint's locations could be a location where
15313 functions have been inlined, nonzero otherwise. */
15314
15315 static int
15316 is_non_inline_function (struct breakpoint *b)
15317 {
15318 /* The shared library event breakpoint is set on the address of a
15319 non-inline function. */
15320 if (b->type == bp_shlib_event)
15321 return 1;
15322
15323 return 0;
15324 }
15325
15326 /* Nonzero if the specified PC cannot be a location where functions
15327 have been inlined. */
15328
15329 int
15330 pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
15331 const struct target_waitstatus *ws)
15332 {
15333 struct breakpoint *b;
15334 struct bp_location *bl;
15335
15336 ALL_BREAKPOINTS (b)
15337 {
15338 if (!is_non_inline_function (b))
15339 continue;
15340
15341 for (bl = b->loc; bl != NULL; bl = bl->next)
15342 {
15343 if (!bl->shlib_disabled
15344 && bpstat_check_location (bl, aspace, pc, ws))
15345 return 1;
15346 }
15347 }
15348
15349 return 0;
15350 }
15351
15352 void
15353 initialize_breakpoint_ops (void)
15354 {
15355 static int initialized = 0;
15356
15357 struct breakpoint_ops *ops;
15358
15359 if (initialized)
15360 return;
15361 initialized = 1;
15362
15363 /* The breakpoint_ops structure to be inherit by all kinds of
15364 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15365 internal and momentary breakpoints, etc.). */
15366 ops = &bkpt_base_breakpoint_ops;
15367 *ops = base_breakpoint_ops;
15368 ops->re_set = bkpt_re_set;
15369 ops->insert_location = bkpt_insert_location;
15370 ops->remove_location = bkpt_remove_location;
15371 ops->breakpoint_hit = bkpt_breakpoint_hit;
15372 ops->create_sals_from_address = bkpt_create_sals_from_address;
15373 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
15374 ops->decode_linespec = bkpt_decode_linespec;
15375
15376 /* The breakpoint_ops structure to be used in regular breakpoints. */
15377 ops = &bkpt_breakpoint_ops;
15378 *ops = bkpt_base_breakpoint_ops;
15379 ops->re_set = bkpt_re_set;
15380 ops->resources_needed = bkpt_resources_needed;
15381 ops->print_it = bkpt_print_it;
15382 ops->print_mention = bkpt_print_mention;
15383 ops->print_recreate = bkpt_print_recreate;
15384
15385 /* Ranged breakpoints. */
15386 ops = &ranged_breakpoint_ops;
15387 *ops = bkpt_breakpoint_ops;
15388 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15389 ops->resources_needed = resources_needed_ranged_breakpoint;
15390 ops->print_it = print_it_ranged_breakpoint;
15391 ops->print_one = print_one_ranged_breakpoint;
15392 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15393 ops->print_mention = print_mention_ranged_breakpoint;
15394 ops->print_recreate = print_recreate_ranged_breakpoint;
15395
15396 /* Internal breakpoints. */
15397 ops = &internal_breakpoint_ops;
15398 *ops = bkpt_base_breakpoint_ops;
15399 ops->re_set = internal_bkpt_re_set;
15400 ops->check_status = internal_bkpt_check_status;
15401 ops->print_it = internal_bkpt_print_it;
15402 ops->print_mention = internal_bkpt_print_mention;
15403
15404 /* Momentary breakpoints. */
15405 ops = &momentary_breakpoint_ops;
15406 *ops = bkpt_base_breakpoint_ops;
15407 ops->re_set = momentary_bkpt_re_set;
15408 ops->check_status = momentary_bkpt_check_status;
15409 ops->print_it = momentary_bkpt_print_it;
15410 ops->print_mention = momentary_bkpt_print_mention;
15411
15412 /* Probe breakpoints. */
15413 ops = &bkpt_probe_breakpoint_ops;
15414 *ops = bkpt_breakpoint_ops;
15415 ops->insert_location = bkpt_probe_insert_location;
15416 ops->remove_location = bkpt_probe_remove_location;
15417 ops->create_sals_from_address = bkpt_probe_create_sals_from_address;
15418 ops->decode_linespec = bkpt_probe_decode_linespec;
15419
15420 /* GNU v3 exception catchpoints. */
15421 ops = &gnu_v3_exception_catchpoint_ops;
15422 *ops = bkpt_breakpoint_ops;
15423 ops->print_it = print_it_exception_catchpoint;
15424 ops->print_one = print_one_exception_catchpoint;
15425 ops->print_mention = print_mention_exception_catchpoint;
15426 ops->print_recreate = print_recreate_exception_catchpoint;
15427
15428 /* Watchpoints. */
15429 ops = &watchpoint_breakpoint_ops;
15430 *ops = base_breakpoint_ops;
15431 ops->dtor = dtor_watchpoint;
15432 ops->re_set = re_set_watchpoint;
15433 ops->insert_location = insert_watchpoint;
15434 ops->remove_location = remove_watchpoint;
15435 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15436 ops->check_status = check_status_watchpoint;
15437 ops->resources_needed = resources_needed_watchpoint;
15438 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15439 ops->print_it = print_it_watchpoint;
15440 ops->print_mention = print_mention_watchpoint;
15441 ops->print_recreate = print_recreate_watchpoint;
15442
15443 /* Masked watchpoints. */
15444 ops = &masked_watchpoint_breakpoint_ops;
15445 *ops = watchpoint_breakpoint_ops;
15446 ops->insert_location = insert_masked_watchpoint;
15447 ops->remove_location = remove_masked_watchpoint;
15448 ops->resources_needed = resources_needed_masked_watchpoint;
15449 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15450 ops->print_it = print_it_masked_watchpoint;
15451 ops->print_one_detail = print_one_detail_masked_watchpoint;
15452 ops->print_mention = print_mention_masked_watchpoint;
15453 ops->print_recreate = print_recreate_masked_watchpoint;
15454
15455 /* Tracepoints. */
15456 ops = &tracepoint_breakpoint_ops;
15457 *ops = base_breakpoint_ops;
15458 ops->re_set = tracepoint_re_set;
15459 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15460 ops->print_one_detail = tracepoint_print_one_detail;
15461 ops->print_mention = tracepoint_print_mention;
15462 ops->print_recreate = tracepoint_print_recreate;
15463 ops->create_sals_from_address = tracepoint_create_sals_from_address;
15464 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
15465 ops->decode_linespec = tracepoint_decode_linespec;
15466
15467 /* Probe tracepoints. */
15468 ops = &tracepoint_probe_breakpoint_ops;
15469 *ops = tracepoint_breakpoint_ops;
15470 ops->create_sals_from_address = tracepoint_probe_create_sals_from_address;
15471 ops->decode_linespec = tracepoint_probe_decode_linespec;
15472
15473 /* Static tracepoints with marker (`-m'). */
15474 ops = &strace_marker_breakpoint_ops;
15475 *ops = tracepoint_breakpoint_ops;
15476 ops->create_sals_from_address = strace_marker_create_sals_from_address;
15477 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
15478 ops->decode_linespec = strace_marker_decode_linespec;
15479
15480 /* Fork catchpoints. */
15481 ops = &catch_fork_breakpoint_ops;
15482 *ops = base_breakpoint_ops;
15483 ops->insert_location = insert_catch_fork;
15484 ops->remove_location = remove_catch_fork;
15485 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15486 ops->print_it = print_it_catch_fork;
15487 ops->print_one = print_one_catch_fork;
15488 ops->print_mention = print_mention_catch_fork;
15489 ops->print_recreate = print_recreate_catch_fork;
15490
15491 /* Vfork catchpoints. */
15492 ops = &catch_vfork_breakpoint_ops;
15493 *ops = base_breakpoint_ops;
15494 ops->insert_location = insert_catch_vfork;
15495 ops->remove_location = remove_catch_vfork;
15496 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15497 ops->print_it = print_it_catch_vfork;
15498 ops->print_one = print_one_catch_vfork;
15499 ops->print_mention = print_mention_catch_vfork;
15500 ops->print_recreate = print_recreate_catch_vfork;
15501
15502 /* Exec catchpoints. */
15503 ops = &catch_exec_breakpoint_ops;
15504 *ops = base_breakpoint_ops;
15505 ops->dtor = dtor_catch_exec;
15506 ops->insert_location = insert_catch_exec;
15507 ops->remove_location = remove_catch_exec;
15508 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15509 ops->print_it = print_it_catch_exec;
15510 ops->print_one = print_one_catch_exec;
15511 ops->print_mention = print_mention_catch_exec;
15512 ops->print_recreate = print_recreate_catch_exec;
15513
15514 /* Syscall catchpoints. */
15515 ops = &catch_syscall_breakpoint_ops;
15516 *ops = base_breakpoint_ops;
15517 ops->dtor = dtor_catch_syscall;
15518 ops->insert_location = insert_catch_syscall;
15519 ops->remove_location = remove_catch_syscall;
15520 ops->breakpoint_hit = breakpoint_hit_catch_syscall;
15521 ops->print_it = print_it_catch_syscall;
15522 ops->print_one = print_one_catch_syscall;
15523 ops->print_mention = print_mention_catch_syscall;
15524 ops->print_recreate = print_recreate_catch_syscall;
15525
15526 /* Solib-related catchpoints. */
15527 ops = &catch_solib_breakpoint_ops;
15528 *ops = base_breakpoint_ops;
15529 ops->dtor = dtor_catch_solib;
15530 ops->insert_location = insert_catch_solib;
15531 ops->remove_location = remove_catch_solib;
15532 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15533 ops->check_status = check_status_catch_solib;
15534 ops->print_it = print_it_catch_solib;
15535 ops->print_one = print_one_catch_solib;
15536 ops->print_mention = print_mention_catch_solib;
15537 ops->print_recreate = print_recreate_catch_solib;
15538
15539 ops = &dprintf_breakpoint_ops;
15540 *ops = bkpt_base_breakpoint_ops;
15541 ops->re_set = bkpt_re_set;
15542 ops->resources_needed = bkpt_resources_needed;
15543 ops->print_it = bkpt_print_it;
15544 ops->print_mention = bkpt_print_mention;
15545 ops->print_recreate = bkpt_print_recreate;
15546 }
15547
15548 void
15549 _initialize_breakpoint (void)
15550 {
15551 struct cmd_list_element *c;
15552
15553 initialize_breakpoint_ops ();
15554
15555 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
15556 observer_attach_inferior_exit (clear_syscall_counts);
15557 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
15558
15559 breakpoint_objfile_key
15560 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
15561
15562 catch_syscall_inferior_data
15563 = register_inferior_data_with_cleanup (catch_syscall_inferior_data_cleanup);
15564
15565 breakpoint_chain = 0;
15566 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
15567 before a breakpoint is set. */
15568 breakpoint_count = 0;
15569
15570 tracepoint_count = 0;
15571
15572 add_com ("ignore", class_breakpoint, ignore_command, _("\
15573 Set ignore-count of breakpoint number N to COUNT.\n\
15574 Usage is `ignore N COUNT'."));
15575 if (xdb_commands)
15576 add_com_alias ("bc", "ignore", class_breakpoint, 1);
15577
15578 add_com ("commands", class_breakpoint, commands_command, _("\
15579 Set commands to be executed when a breakpoint is hit.\n\
15580 Give breakpoint number as argument after \"commands\".\n\
15581 With no argument, the targeted breakpoint is the last one set.\n\
15582 The commands themselves follow starting on the next line.\n\
15583 Type a line containing \"end\" to indicate the end of them.\n\
15584 Give \"silent\" as the first line to make the breakpoint silent;\n\
15585 then no output is printed when it is hit, except what the commands print."));
15586
15587 c = add_com ("condition", class_breakpoint, condition_command, _("\
15588 Specify breakpoint number N to break only if COND is true.\n\
15589 Usage is `condition N COND', where N is an integer and COND is an\n\
15590 expression to be evaluated whenever breakpoint N is reached."));
15591 set_cmd_completer (c, condition_completer);
15592
15593 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
15594 Set a temporary breakpoint.\n\
15595 Like \"break\" except the breakpoint is only temporary,\n\
15596 so it will be deleted when hit. Equivalent to \"break\" followed\n\
15597 by using \"enable delete\" on the breakpoint number.\n\
15598 \n"
15599 BREAK_ARGS_HELP ("tbreak")));
15600 set_cmd_completer (c, location_completer);
15601
15602 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
15603 Set a hardware assisted breakpoint.\n\
15604 Like \"break\" except the breakpoint requires hardware support,\n\
15605 some target hardware may not have this support.\n\
15606 \n"
15607 BREAK_ARGS_HELP ("hbreak")));
15608 set_cmd_completer (c, location_completer);
15609
15610 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
15611 Set a temporary hardware assisted breakpoint.\n\
15612 Like \"hbreak\" except the breakpoint is only temporary,\n\
15613 so it will be deleted when hit.\n\
15614 \n"
15615 BREAK_ARGS_HELP ("thbreak")));
15616 set_cmd_completer (c, location_completer);
15617
15618 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
15619 Enable some breakpoints.\n\
15620 Give breakpoint numbers (separated by spaces) as arguments.\n\
15621 With no subcommand, breakpoints are enabled until you command otherwise.\n\
15622 This is used to cancel the effect of the \"disable\" command.\n\
15623 With a subcommand you can enable temporarily."),
15624 &enablelist, "enable ", 1, &cmdlist);
15625 if (xdb_commands)
15626 add_com ("ab", class_breakpoint, enable_command, _("\
15627 Enable some breakpoints.\n\
15628 Give breakpoint numbers (separated by spaces) as arguments.\n\
15629 With no subcommand, breakpoints are enabled until you command otherwise.\n\
15630 This is used to cancel the effect of the \"disable\" command.\n\
15631 With a subcommand you can enable temporarily."));
15632
15633 add_com_alias ("en", "enable", class_breakpoint, 1);
15634
15635 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
15636 Enable some breakpoints.\n\
15637 Give breakpoint numbers (separated by spaces) as arguments.\n\
15638 This is used to cancel the effect of the \"disable\" command.\n\
15639 May be abbreviated to simply \"enable\".\n"),
15640 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
15641
15642 add_cmd ("once", no_class, enable_once_command, _("\
15643 Enable breakpoints for one hit. Give breakpoint numbers.\n\
15644 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15645 &enablebreaklist);
15646
15647 add_cmd ("delete", no_class, enable_delete_command, _("\
15648 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
15649 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15650 &enablebreaklist);
15651
15652 add_cmd ("count", no_class, enable_count_command, _("\
15653 Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
15654 If a breakpoint is hit while enabled in this fashion,\n\
15655 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15656 &enablebreaklist);
15657
15658 add_cmd ("delete", no_class, enable_delete_command, _("\
15659 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
15660 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15661 &enablelist);
15662
15663 add_cmd ("once", no_class, enable_once_command, _("\
15664 Enable breakpoints for one hit. Give breakpoint numbers.\n\
15665 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15666 &enablelist);
15667
15668 add_cmd ("count", no_class, enable_count_command, _("\
15669 Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
15670 If a breakpoint is hit while enabled in this fashion,\n\
15671 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15672 &enablelist);
15673
15674 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
15675 Disable some breakpoints.\n\
15676 Arguments are breakpoint numbers with spaces in between.\n\
15677 To disable all breakpoints, give no argument.\n\
15678 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
15679 &disablelist, "disable ", 1, &cmdlist);
15680 add_com_alias ("dis", "disable", class_breakpoint, 1);
15681 add_com_alias ("disa", "disable", class_breakpoint, 1);
15682 if (xdb_commands)
15683 add_com ("sb", class_breakpoint, disable_command, _("\
15684 Disable some breakpoints.\n\
15685 Arguments are breakpoint numbers with spaces in between.\n\
15686 To disable all breakpoints, give no argument.\n\
15687 A disabled breakpoint is not forgotten, but has no effect until re-enabled."));
15688
15689 add_cmd ("breakpoints", class_alias, disable_command, _("\
15690 Disable some breakpoints.\n\
15691 Arguments are breakpoint numbers with spaces in between.\n\
15692 To disable all breakpoints, give no argument.\n\
15693 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
15694 This command may be abbreviated \"disable\"."),
15695 &disablelist);
15696
15697 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
15698 Delete some breakpoints or auto-display expressions.\n\
15699 Arguments are breakpoint numbers with spaces in between.\n\
15700 To delete all breakpoints, give no argument.\n\
15701 \n\
15702 Also a prefix command for deletion of other GDB objects.\n\
15703 The \"unset\" command is also an alias for \"delete\"."),
15704 &deletelist, "delete ", 1, &cmdlist);
15705 add_com_alias ("d", "delete", class_breakpoint, 1);
15706 add_com_alias ("del", "delete", class_breakpoint, 1);
15707 if (xdb_commands)
15708 add_com ("db", class_breakpoint, delete_command, _("\
15709 Delete some breakpoints.\n\
15710 Arguments are breakpoint numbers with spaces in between.\n\
15711 To delete all breakpoints, give no argument.\n"));
15712
15713 add_cmd ("breakpoints", class_alias, delete_command, _("\
15714 Delete some breakpoints or auto-display expressions.\n\
15715 Arguments are breakpoint numbers with spaces in between.\n\
15716 To delete all breakpoints, give no argument.\n\
15717 This command may be abbreviated \"delete\"."),
15718 &deletelist);
15719
15720 add_com ("clear", class_breakpoint, clear_command, _("\
15721 Clear breakpoint at specified line or function.\n\
15722 Argument may be line number, function name, or \"*\" and an address.\n\
15723 If line number is specified, all breakpoints in that line are cleared.\n\
15724 If function is specified, breakpoints at beginning of function are cleared.\n\
15725 If an address is specified, breakpoints at that address are cleared.\n\
15726 \n\
15727 With no argument, clears all breakpoints in the line that the selected frame\n\
15728 is executing in.\n\
15729 \n\
15730 See also the \"delete\" command which clears breakpoints by number."));
15731 add_com_alias ("cl", "clear", class_breakpoint, 1);
15732
15733 c = add_com ("break", class_breakpoint, break_command, _("\
15734 Set breakpoint at specified line or function.\n"
15735 BREAK_ARGS_HELP ("break")));
15736 set_cmd_completer (c, location_completer);
15737
15738 add_com_alias ("b", "break", class_run, 1);
15739 add_com_alias ("br", "break", class_run, 1);
15740 add_com_alias ("bre", "break", class_run, 1);
15741 add_com_alias ("brea", "break", class_run, 1);
15742
15743 if (xdb_commands)
15744 add_com_alias ("ba", "break", class_breakpoint, 1);
15745
15746 if (dbx_commands)
15747 {
15748 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
15749 Break in function/address or break at a line in the current file."),
15750 &stoplist, "stop ", 1, &cmdlist);
15751 add_cmd ("in", class_breakpoint, stopin_command,
15752 _("Break in function or address."), &stoplist);
15753 add_cmd ("at", class_breakpoint, stopat_command,
15754 _("Break at a line in the current file."), &stoplist);
15755 add_com ("status", class_info, breakpoints_info, _("\
15756 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
15757 The \"Type\" column indicates one of:\n\
15758 \tbreakpoint - normal breakpoint\n\
15759 \twatchpoint - watchpoint\n\
15760 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15761 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15762 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15763 address and file/line number respectively.\n\
15764 \n\
15765 Convenience variable \"$_\" and default examine address for \"x\"\n\
15766 are set to the address of the last breakpoint listed unless the command\n\
15767 is prefixed with \"server \".\n\n\
15768 Convenience variable \"$bpnum\" contains the number of the last\n\
15769 breakpoint set."));
15770 }
15771
15772 add_info ("breakpoints", breakpoints_info, _("\
15773 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
15774 The \"Type\" column indicates one of:\n\
15775 \tbreakpoint - normal breakpoint\n\
15776 \twatchpoint - watchpoint\n\
15777 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15778 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15779 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15780 address and file/line number respectively.\n\
15781 \n\
15782 Convenience variable \"$_\" and default examine address for \"x\"\n\
15783 are set to the address of the last breakpoint listed unless the command\n\
15784 is prefixed with \"server \".\n\n\
15785 Convenience variable \"$bpnum\" contains the number of the last\n\
15786 breakpoint set."));
15787
15788 add_info_alias ("b", "breakpoints", 1);
15789
15790 if (xdb_commands)
15791 add_com ("lb", class_breakpoint, breakpoints_info, _("\
15792 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
15793 The \"Type\" column indicates one of:\n\
15794 \tbreakpoint - normal breakpoint\n\
15795 \twatchpoint - watchpoint\n\
15796 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15797 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15798 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15799 address and file/line number respectively.\n\
15800 \n\
15801 Convenience variable \"$_\" and default examine address for \"x\"\n\
15802 are set to the address of the last breakpoint listed unless the command\n\
15803 is prefixed with \"server \".\n\n\
15804 Convenience variable \"$bpnum\" contains the number of the last\n\
15805 breakpoint set."));
15806
15807 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
15808 Status of all breakpoints, or breakpoint number NUMBER.\n\
15809 The \"Type\" column indicates one of:\n\
15810 \tbreakpoint - normal breakpoint\n\
15811 \twatchpoint - watchpoint\n\
15812 \tlongjmp - internal breakpoint used to step through longjmp()\n\
15813 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
15814 \tuntil - internal breakpoint used by the \"until\" command\n\
15815 \tfinish - internal breakpoint used by the \"finish\" command\n\
15816 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15817 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15818 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15819 address and file/line number respectively.\n\
15820 \n\
15821 Convenience variable \"$_\" and default examine address for \"x\"\n\
15822 are set to the address of the last breakpoint listed unless the command\n\
15823 is prefixed with \"server \".\n\n\
15824 Convenience variable \"$bpnum\" contains the number of the last\n\
15825 breakpoint set."),
15826 &maintenanceinfolist);
15827
15828 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
15829 Set catchpoints to catch events."),
15830 &catch_cmdlist, "catch ",
15831 0/*allow-unknown*/, &cmdlist);
15832
15833 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
15834 Set temporary catchpoints to catch events."),
15835 &tcatch_cmdlist, "tcatch ",
15836 0/*allow-unknown*/, &cmdlist);
15837
15838 /* Add catch and tcatch sub-commands. */
15839 add_catch_command ("catch", _("\
15840 Catch an exception, when caught."),
15841 catch_catch_command,
15842 NULL,
15843 CATCH_PERMANENT,
15844 CATCH_TEMPORARY);
15845 add_catch_command ("throw", _("\
15846 Catch an exception, when thrown."),
15847 catch_throw_command,
15848 NULL,
15849 CATCH_PERMANENT,
15850 CATCH_TEMPORARY);
15851 add_catch_command ("fork", _("Catch calls to fork."),
15852 catch_fork_command_1,
15853 NULL,
15854 (void *) (uintptr_t) catch_fork_permanent,
15855 (void *) (uintptr_t) catch_fork_temporary);
15856 add_catch_command ("vfork", _("Catch calls to vfork."),
15857 catch_fork_command_1,
15858 NULL,
15859 (void *) (uintptr_t) catch_vfork_permanent,
15860 (void *) (uintptr_t) catch_vfork_temporary);
15861 add_catch_command ("exec", _("Catch calls to exec."),
15862 catch_exec_command_1,
15863 NULL,
15864 CATCH_PERMANENT,
15865 CATCH_TEMPORARY);
15866 add_catch_command ("load", _("Catch loads of shared libraries.\n\
15867 Usage: catch load [REGEX]\n\
15868 If REGEX is given, only stop for libraries matching the regular expression."),
15869 catch_load_command_1,
15870 NULL,
15871 CATCH_PERMANENT,
15872 CATCH_TEMPORARY);
15873 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
15874 Usage: catch unload [REGEX]\n\
15875 If REGEX is given, only stop for libraries matching the regular expression."),
15876 catch_unload_command_1,
15877 NULL,
15878 CATCH_PERMANENT,
15879 CATCH_TEMPORARY);
15880 add_catch_command ("syscall", _("\
15881 Catch system calls by their names and/or numbers.\n\
15882 Arguments say which system calls to catch. If no arguments\n\
15883 are given, every system call will be caught.\n\
15884 Arguments, if given, should be one or more system call names\n\
15885 (if your system supports that), or system call numbers."),
15886 catch_syscall_command_1,
15887 catch_syscall_completer,
15888 CATCH_PERMANENT,
15889 CATCH_TEMPORARY);
15890
15891 c = add_com ("watch", class_breakpoint, watch_command, _("\
15892 Set a watchpoint for an expression.\n\
15893 Usage: watch [-l|-location] EXPRESSION\n\
15894 A watchpoint stops execution of your program whenever the value of\n\
15895 an expression changes.\n\
15896 If -l or -location is given, this evaluates EXPRESSION and watches\n\
15897 the memory to which it refers."));
15898 set_cmd_completer (c, expression_completer);
15899
15900 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
15901 Set a read watchpoint for an expression.\n\
15902 Usage: rwatch [-l|-location] EXPRESSION\n\
15903 A watchpoint stops execution of your program whenever the value of\n\
15904 an expression is read.\n\
15905 If -l or -location is given, this evaluates EXPRESSION and watches\n\
15906 the memory to which it refers."));
15907 set_cmd_completer (c, expression_completer);
15908
15909 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
15910 Set a watchpoint for an expression.\n\
15911 Usage: awatch [-l|-location] EXPRESSION\n\
15912 A watchpoint stops execution of your program whenever the value of\n\
15913 an expression is either read or written.\n\
15914 If -l or -location is given, this evaluates EXPRESSION and watches\n\
15915 the memory to which it refers."));
15916 set_cmd_completer (c, expression_completer);
15917
15918 add_info ("watchpoints", watchpoints_info, _("\
15919 Status of specified watchpoints (all watchpoints if no argument)."));
15920
15921 /* XXX: cagney/2005-02-23: This should be a boolean, and should
15922 respond to changes - contrary to the description. */
15923 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
15924 &can_use_hw_watchpoints, _("\
15925 Set debugger's willingness to use watchpoint hardware."), _("\
15926 Show debugger's willingness to use watchpoint hardware."), _("\
15927 If zero, gdb will not use hardware for new watchpoints, even if\n\
15928 such is available. (However, any hardware watchpoints that were\n\
15929 created before setting this to nonzero, will continue to use watchpoint\n\
15930 hardware.)"),
15931 NULL,
15932 show_can_use_hw_watchpoints,
15933 &setlist, &showlist);
15934
15935 can_use_hw_watchpoints = 1;
15936
15937 /* Tracepoint manipulation commands. */
15938
15939 c = add_com ("trace", class_breakpoint, trace_command, _("\
15940 Set a tracepoint at specified line or function.\n\
15941 \n"
15942 BREAK_ARGS_HELP ("trace") "\n\
15943 Do \"help tracepoints\" for info on other tracepoint commands."));
15944 set_cmd_completer (c, location_completer);
15945
15946 add_com_alias ("tp", "trace", class_alias, 0);
15947 add_com_alias ("tr", "trace", class_alias, 1);
15948 add_com_alias ("tra", "trace", class_alias, 1);
15949 add_com_alias ("trac", "trace", class_alias, 1);
15950
15951 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
15952 Set a fast tracepoint at specified line or function.\n\
15953 \n"
15954 BREAK_ARGS_HELP ("ftrace") "\n\
15955 Do \"help tracepoints\" for info on other tracepoint commands."));
15956 set_cmd_completer (c, location_completer);
15957
15958 c = add_com ("strace", class_breakpoint, strace_command, _("\
15959 Set a static tracepoint at specified line, function or marker.\n\
15960 \n\
15961 strace [LOCATION] [if CONDITION]\n\
15962 LOCATION may be a line number, function name, \"*\" and an address,\n\
15963 or -m MARKER_ID.\n\
15964 If a line number is specified, probe the marker at start of code\n\
15965 for that line. If a function is specified, probe the marker at start\n\
15966 of code for that function. If an address is specified, probe the marker\n\
15967 at that exact address. If a marker id is specified, probe the marker\n\
15968 with that name. With no LOCATION, uses current execution address of\n\
15969 the selected stack frame.\n\
15970 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
15971 This collects arbitrary user data passed in the probe point call to the\n\
15972 tracing library. You can inspect it when analyzing the trace buffer,\n\
15973 by printing the $_sdata variable like any other convenience variable.\n\
15974 \n\
15975 CONDITION is a boolean expression.\n\
15976 \n\
15977 Multiple tracepoints at one place are permitted, and useful if their\n\
15978 conditions are different.\n\
15979 \n\
15980 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
15981 Do \"help tracepoints\" for info on other tracepoint commands."));
15982 set_cmd_completer (c, location_completer);
15983
15984 add_info ("tracepoints", tracepoints_info, _("\
15985 Status of specified tracepoints (all tracepoints if no argument).\n\
15986 Convenience variable \"$tpnum\" contains the number of the\n\
15987 last tracepoint set."));
15988
15989 add_info_alias ("tp", "tracepoints", 1);
15990
15991 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
15992 Delete specified tracepoints.\n\
15993 Arguments are tracepoint numbers, separated by spaces.\n\
15994 No argument means delete all tracepoints."),
15995 &deletelist);
15996
15997 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
15998 Disable specified tracepoints.\n\
15999 Arguments are tracepoint numbers, separated by spaces.\n\
16000 No argument means disable all tracepoints."),
16001 &disablelist);
16002 deprecate_cmd (c, "disable");
16003
16004 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16005 Enable specified tracepoints.\n\
16006 Arguments are tracepoint numbers, separated by spaces.\n\
16007 No argument means enable all tracepoints."),
16008 &enablelist);
16009 deprecate_cmd (c, "enable");
16010
16011 add_com ("passcount", class_trace, trace_pass_command, _("\
16012 Set the passcount for a tracepoint.\n\
16013 The trace will end when the tracepoint has been passed 'count' times.\n\
16014 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16015 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16016
16017 add_prefix_cmd ("save", class_breakpoint, save_command,
16018 _("Save breakpoint definitions as a script."),
16019 &save_cmdlist, "save ",
16020 0/*allow-unknown*/, &cmdlist);
16021
16022 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16023 Save current breakpoint definitions as a script.\n\
16024 This includes all types of breakpoints (breakpoints, watchpoints,\n\
16025 catchpoints, tracepoints). Use the 'source' command in another debug\n\
16026 session to restore them."),
16027 &save_cmdlist);
16028 set_cmd_completer (c, filename_completer);
16029
16030 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
16031 Save current tracepoint definitions as a script.\n\
16032 Use the 'source' command in another debug session to restore them."),
16033 &save_cmdlist);
16034 set_cmd_completer (c, filename_completer);
16035
16036 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16037 deprecate_cmd (c, "save tracepoints");
16038
16039 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
16040 Breakpoint specific settings\n\
16041 Configure various breakpoint-specific variables such as\n\
16042 pending breakpoint behavior"),
16043 &breakpoint_set_cmdlist, "set breakpoint ",
16044 0/*allow-unknown*/, &setlist);
16045 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
16046 Breakpoint specific settings\n\
16047 Configure various breakpoint-specific variables such as\n\
16048 pending breakpoint behavior"),
16049 &breakpoint_show_cmdlist, "show breakpoint ",
16050 0/*allow-unknown*/, &showlist);
16051
16052 add_setshow_auto_boolean_cmd ("pending", no_class,
16053 &pending_break_support, _("\
16054 Set debugger's behavior regarding pending breakpoints."), _("\
16055 Show debugger's behavior regarding pending breakpoints."), _("\
16056 If on, an unrecognized breakpoint location will cause gdb to create a\n\
16057 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16058 an error. If auto, an unrecognized breakpoint location results in a\n\
16059 user-query to see if a pending breakpoint should be created."),
16060 NULL,
16061 show_pending_break_support,
16062 &breakpoint_set_cmdlist,
16063 &breakpoint_show_cmdlist);
16064
16065 pending_break_support = AUTO_BOOLEAN_AUTO;
16066
16067 add_setshow_boolean_cmd ("auto-hw", no_class,
16068 &automatic_hardware_breakpoints, _("\
16069 Set automatic usage of hardware breakpoints."), _("\
16070 Show automatic usage of hardware breakpoints."), _("\
16071 If set, the debugger will automatically use hardware breakpoints for\n\
16072 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16073 a warning will be emitted for such breakpoints."),
16074 NULL,
16075 show_automatic_hardware_breakpoints,
16076 &breakpoint_set_cmdlist,
16077 &breakpoint_show_cmdlist);
16078
16079 add_setshow_enum_cmd ("always-inserted", class_support,
16080 always_inserted_enums, &always_inserted_mode, _("\
16081 Set mode for inserting breakpoints."), _("\
16082 Show mode for inserting breakpoints."), _("\
16083 When this mode is off, breakpoints are inserted in inferior when it is\n\
16084 resumed, and removed when execution stops. When this mode is on,\n\
16085 breakpoints are inserted immediately and removed only when the user\n\
16086 deletes the breakpoint. When this mode is auto (which is the default),\n\
16087 the behaviour depends on the non-stop setting (see help set non-stop).\n\
16088 In this case, if gdb is controlling the inferior in non-stop mode, gdb\n\
16089 behaves as if always-inserted mode is on; if gdb is controlling the\n\
16090 inferior in all-stop mode, gdb behaves as if always-inserted mode is off."),
16091 NULL,
16092 &show_always_inserted_mode,
16093 &breakpoint_set_cmdlist,
16094 &breakpoint_show_cmdlist);
16095
16096 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16097 condition_evaluation_enums,
16098 &condition_evaluation_mode_1, _("\
16099 Set mode of breakpoint condition evaluation."), _("\
16100 Show mode of breakpoint condition evaluation."), _("\
16101 When this is set to \"host\", breakpoint conditions will be\n\
16102 evaluated on the host's side by GDB. When it is set to \"target\",\n\
16103 breakpoint conditions will be downloaded to the target (if the target\n\
16104 supports such feature) and conditions will be evaluated on the target's side.\n\
16105 If this is set to \"auto\" (default), this will be automatically set to\n\
16106 \"target\" if it supports condition evaluation, otherwise it will\n\
16107 be set to \"gdb\""),
16108 &set_condition_evaluation_mode,
16109 &show_condition_evaluation_mode,
16110 &breakpoint_set_cmdlist,
16111 &breakpoint_show_cmdlist);
16112
16113 add_com ("break-range", class_breakpoint, break_range_command, _("\
16114 Set a breakpoint for an address range.\n\
16115 break-range START-LOCATION, END-LOCATION\n\
16116 where START-LOCATION and END-LOCATION can be one of the following:\n\
16117 LINENUM, for that line in the current file,\n\
16118 FILE:LINENUM, for that line in that file,\n\
16119 +OFFSET, for that number of lines after the current line\n\
16120 or the start of the range\n\
16121 FUNCTION, for the first line in that function,\n\
16122 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16123 *ADDRESS, for the instruction at that address.\n\
16124 \n\
16125 The breakpoint will stop execution of the inferior whenever it executes\n\
16126 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16127 range (including START-LOCATION and END-LOCATION)."));
16128
16129 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
16130 Set a dynamic printf at specified line or function.\n\
16131 dprintf location,format string,arg1,arg2,...\n\
16132 location may be a line number, function name, or \"*\" and an address.\n\
16133 If a line number is specified, break at start of code for that line.\n\
16134 If a function is specified, break at start of code for that function.\n\
16135 "));
16136 set_cmd_completer (c, location_completer);
16137
16138 add_setshow_enum_cmd ("dprintf-style", class_support,
16139 dprintf_style_enums, &dprintf_style, _("\
16140 Set the style of usage for dynamic printf."), _("\
16141 Show the style of usage for dynamic printf."), _("\
16142 This setting chooses how GDB will do a dynamic printf.\n\
16143 If the value is \"gdb\", then the printing is done by GDB to its own\n\
16144 console, as with the \"printf\" command.\n\
16145 If the value is \"call\", the print is done by calling a function in your\n\
16146 program; by default printf(), but you can choose a different function or\n\
16147 output stream by setting dprintf-function and dprintf-channel."),
16148 update_dprintf_commands, NULL,
16149 &setlist, &showlist);
16150
16151 dprintf_function = xstrdup ("printf");
16152 add_setshow_string_cmd ("dprintf-function", class_support,
16153 &dprintf_function, _("\
16154 Set the function to use for dynamic printf"), _("\
16155 Show the function to use for dynamic printf"), NULL,
16156 update_dprintf_commands, NULL,
16157 &setlist, &showlist);
16158
16159 dprintf_channel = xstrdup ("");
16160 add_setshow_string_cmd ("dprintf-channel", class_support,
16161 &dprintf_channel, _("\
16162 Set the channel to use for dynamic printf"), _("\
16163 Show the channel to use for dynamic printf"), NULL,
16164 update_dprintf_commands, NULL,
16165 &setlist, &showlist);
16166
16167 automatic_hardware_breakpoints = 1;
16168
16169 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
16170 }
This page took 0.408623 seconds and 4 git commands to generate.