Teach GDB about targets that can tell whether a trap is a breakpoint event
[deliverable/binutils-gdb.git] / gdb / breakpoint.c
1 /* Everything about breakpoints, for GDB.
2
3 Copyright (C) 1986-2015 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21 #include "arch-utils.h"
22 #include <ctype.h>
23 #include "hashtab.h"
24 #include "symtab.h"
25 #include "frame.h"
26 #include "breakpoint.h"
27 #include "tracepoint.h"
28 #include "gdbtypes.h"
29 #include "expression.h"
30 #include "gdbcore.h"
31 #include "gdbcmd.h"
32 #include "value.h"
33 #include "command.h"
34 #include "inferior.h"
35 #include "infrun.h"
36 #include "gdbthread.h"
37 #include "target.h"
38 #include "language.h"
39 #include "gdb-demangle.h"
40 #include "filenames.h"
41 #include "annotate.h"
42 #include "symfile.h"
43 #include "objfiles.h"
44 #include "source.h"
45 #include "linespec.h"
46 #include "completer.h"
47 #include "gdb.h"
48 #include "ui-out.h"
49 #include "cli/cli-script.h"
50 #include "block.h"
51 #include "solib.h"
52 #include "solist.h"
53 #include "observer.h"
54 #include "memattr.h"
55 #include "ada-lang.h"
56 #include "top.h"
57 #include "valprint.h"
58 #include "jit.h"
59 #include "xml-syscall.h"
60 #include "parser-defs.h"
61 #include "gdb_regex.h"
62 #include "probe.h"
63 #include "cli/cli-utils.h"
64 #include "continuations.h"
65 #include "stack.h"
66 #include "skip.h"
67 #include "ax-gdb.h"
68 #include "dummy-frame.h"
69 #include "interps.h"
70 #include "format.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 "extension.h"
81
82 /* Enums for exception-handling support. */
83 enum exception_event_kind
84 {
85 EX_EVENT_THROW,
86 EX_EVENT_RETHROW,
87 EX_EVENT_CATCH
88 };
89
90 /* Prototypes for local functions. */
91
92 static void enable_delete_command (char *, int);
93
94 static void enable_once_command (char *, int);
95
96 static void enable_count_command (char *, int);
97
98 static void disable_command (char *, int);
99
100 static void enable_command (char *, int);
101
102 static void map_breakpoint_numbers (char *, void (*) (struct breakpoint *,
103 void *),
104 void *);
105
106 static void ignore_command (char *, int);
107
108 static int breakpoint_re_set_one (void *);
109
110 static void breakpoint_re_set_default (struct breakpoint *);
111
112 static void create_sals_from_address_default (char **,
113 struct linespec_result *,
114 enum bptype, char *,
115 char **);
116
117 static void create_breakpoints_sal_default (struct gdbarch *,
118 struct linespec_result *,
119 char *, char *, enum bptype,
120 enum bpdisp, int, int,
121 int,
122 const struct breakpoint_ops *,
123 int, int, int, unsigned);
124
125 static void decode_linespec_default (struct breakpoint *, char **,
126 struct symtabs_and_lines *);
127
128 static void clear_command (char *, int);
129
130 static void catch_command (char *, int);
131
132 static int can_use_hardware_watchpoint (struct value *);
133
134 static void break_command_1 (char *, int, int);
135
136 static void mention (struct breakpoint *);
137
138 static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
139 enum bptype,
140 const struct breakpoint_ops *);
141 static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
142 const struct symtab_and_line *);
143
144 /* This function is used in gdbtk sources and thus can not be made
145 static. */
146 struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
147 struct symtab_and_line,
148 enum bptype,
149 const struct breakpoint_ops *);
150
151 static struct breakpoint *
152 momentary_breakpoint_from_master (struct breakpoint *orig,
153 enum bptype type,
154 const struct breakpoint_ops *ops,
155 int loc_enabled);
156
157 static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
158
159 static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
160 CORE_ADDR bpaddr,
161 enum bptype bptype);
162
163 static void describe_other_breakpoints (struct gdbarch *,
164 struct program_space *, CORE_ADDR,
165 struct obj_section *, int);
166
167 static int watchpoint_locations_match (struct bp_location *loc1,
168 struct bp_location *loc2);
169
170 static int breakpoint_location_address_match (struct bp_location *bl,
171 struct address_space *aspace,
172 CORE_ADDR addr);
173
174 static void breakpoints_info (char *, int);
175
176 static void watchpoints_info (char *, int);
177
178 static int breakpoint_1 (char *, int,
179 int (*) (const struct breakpoint *));
180
181 static int breakpoint_cond_eval (void *);
182
183 static void cleanup_executing_breakpoints (void *);
184
185 static void commands_command (char *, int);
186
187 static void condition_command (char *, int);
188
189 typedef enum
190 {
191 mark_inserted,
192 mark_uninserted
193 }
194 insertion_state_t;
195
196 static int remove_breakpoint (struct bp_location *, insertion_state_t);
197 static int remove_breakpoint_1 (struct bp_location *, insertion_state_t);
198
199 static enum print_stop_action print_bp_stop_message (bpstat bs);
200
201 static int watchpoint_check (void *);
202
203 static void maintenance_info_breakpoints (char *, int);
204
205 static int hw_breakpoint_used_count (void);
206
207 static int hw_watchpoint_use_count (struct breakpoint *);
208
209 static int hw_watchpoint_used_count_others (struct breakpoint *except,
210 enum bptype type,
211 int *other_type_used);
212
213 static void hbreak_command (char *, int);
214
215 static void thbreak_command (char *, int);
216
217 static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
218 int count);
219
220 static void stop_command (char *arg, int from_tty);
221
222 static void stopin_command (char *arg, int from_tty);
223
224 static void stopat_command (char *arg, int from_tty);
225
226 static void tcatch_command (char *arg, int from_tty);
227
228 static void free_bp_location (struct bp_location *loc);
229 static void incref_bp_location (struct bp_location *loc);
230 static void decref_bp_location (struct bp_location **loc);
231
232 static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
233
234 /* update_global_location_list's modes of operation wrt to whether to
235 insert locations now. */
236 enum ugll_insert_mode
237 {
238 /* Don't insert any breakpoint locations into the inferior, only
239 remove already-inserted locations that no longer should be
240 inserted. Functions that delete a breakpoint or breakpoints
241 should specify this mode, so that deleting a breakpoint doesn't
242 have the side effect of inserting the locations of other
243 breakpoints that are marked not-inserted, but should_be_inserted
244 returns true on them.
245
246 This behavior is useful is situations close to tear-down -- e.g.,
247 after an exec, while the target still has execution, but
248 breakpoint shadows of the previous executable image should *NOT*
249 be restored to the new image; or before detaching, where the
250 target still has execution and wants to delete breakpoints from
251 GDB's lists, and all breakpoints had already been removed from
252 the inferior. */
253 UGLL_DONT_INSERT,
254
255 /* May insert breakpoints iff breakpoints_should_be_inserted_now
256 claims breakpoints should be inserted now. */
257 UGLL_MAY_INSERT,
258
259 /* Insert locations now, irrespective of
260 breakpoints_should_be_inserted_now. E.g., say all threads are
261 stopped right now, and the user did "continue". We need to
262 insert breakpoints _before_ resuming the target, but
263 UGLL_MAY_INSERT wouldn't insert them, because
264 breakpoints_should_be_inserted_now returns false at that point,
265 as no thread is running yet. */
266 UGLL_INSERT
267 };
268
269 static void update_global_location_list (enum ugll_insert_mode);
270
271 static void update_global_location_list_nothrow (enum ugll_insert_mode);
272
273 static int is_hardware_watchpoint (const struct breakpoint *bpt);
274
275 static void insert_breakpoint_locations (void);
276
277 static int syscall_catchpoint_p (struct breakpoint *b);
278
279 static void tracepoints_info (char *, int);
280
281 static void delete_trace_command (char *, int);
282
283 static void enable_trace_command (char *, int);
284
285 static void disable_trace_command (char *, int);
286
287 static void trace_pass_command (char *, int);
288
289 static void set_tracepoint_count (int num);
290
291 static int is_masked_watchpoint (const struct breakpoint *b);
292
293 static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
294
295 /* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
296 otherwise. */
297
298 static int strace_marker_p (struct breakpoint *b);
299
300 /* The breakpoint_ops structure to be inherited by all breakpoint_ops
301 that are implemented on top of software or hardware breakpoints
302 (user breakpoints, internal and momentary breakpoints, etc.). */
303 static struct breakpoint_ops bkpt_base_breakpoint_ops;
304
305 /* Internal breakpoints class type. */
306 static struct breakpoint_ops internal_breakpoint_ops;
307
308 /* Momentary breakpoints class type. */
309 static struct breakpoint_ops momentary_breakpoint_ops;
310
311 /* Momentary breakpoints for bp_longjmp and bp_exception class type. */
312 static struct breakpoint_ops longjmp_breakpoint_ops;
313
314 /* The breakpoint_ops structure to be used in regular user created
315 breakpoints. */
316 struct breakpoint_ops bkpt_breakpoint_ops;
317
318 /* Breakpoints set on probes. */
319 static struct breakpoint_ops bkpt_probe_breakpoint_ops;
320
321 /* Dynamic printf class type. */
322 struct breakpoint_ops dprintf_breakpoint_ops;
323
324 /* The style in which to perform a dynamic printf. This is a user
325 option because different output options have different tradeoffs;
326 if GDB does the printing, there is better error handling if there
327 is a problem with any of the arguments, but using an inferior
328 function lets you have special-purpose printers and sending of
329 output to the same place as compiled-in print functions. */
330
331 static const char dprintf_style_gdb[] = "gdb";
332 static const char dprintf_style_call[] = "call";
333 static const char dprintf_style_agent[] = "agent";
334 static const char *const dprintf_style_enums[] = {
335 dprintf_style_gdb,
336 dprintf_style_call,
337 dprintf_style_agent,
338 NULL
339 };
340 static const char *dprintf_style = dprintf_style_gdb;
341
342 /* The function to use for dynamic printf if the preferred style is to
343 call into the inferior. The value is simply a string that is
344 copied into the command, so it can be anything that GDB can
345 evaluate to a callable address, not necessarily a function name. */
346
347 static char *dprintf_function = "";
348
349 /* The channel to use for dynamic printf if the preferred style is to
350 call into the inferior; if a nonempty string, it will be passed to
351 the call as the first argument, with the format string as the
352 second. As with the dprintf function, this can be anything that
353 GDB knows how to evaluate, so in addition to common choices like
354 "stderr", this could be an app-specific expression like
355 "mystreams[curlogger]". */
356
357 static char *dprintf_channel = "";
358
359 /* True if dprintf commands should continue to operate even if GDB
360 has disconnected. */
361 static int disconnected_dprintf = 1;
362
363 /* A reference-counted struct command_line. This lets multiple
364 breakpoints share a single command list. */
365 struct counted_command_line
366 {
367 /* The reference count. */
368 int refc;
369
370 /* The command list. */
371 struct command_line *commands;
372 };
373
374 struct command_line *
375 breakpoint_commands (struct breakpoint *b)
376 {
377 return b->commands ? b->commands->commands : NULL;
378 }
379
380 /* Flag indicating that a command has proceeded the inferior past the
381 current breakpoint. */
382
383 static int breakpoint_proceeded;
384
385 const char *
386 bpdisp_text (enum bpdisp disp)
387 {
388 /* NOTE: the following values are a part of MI protocol and
389 represent values of 'disp' field returned when inferior stops at
390 a breakpoint. */
391 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
392
393 return bpdisps[(int) disp];
394 }
395
396 /* Prototypes for exported functions. */
397 /* If FALSE, gdb will not use hardware support for watchpoints, even
398 if such is available. */
399 static int can_use_hw_watchpoints;
400
401 static void
402 show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
403 struct cmd_list_element *c,
404 const char *value)
405 {
406 fprintf_filtered (file,
407 _("Debugger's willingness to use "
408 "watchpoint hardware is %s.\n"),
409 value);
410 }
411
412 /* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
413 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
414 for unrecognized breakpoint locations.
415 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
416 static enum auto_boolean pending_break_support;
417 static void
418 show_pending_break_support (struct ui_file *file, int from_tty,
419 struct cmd_list_element *c,
420 const char *value)
421 {
422 fprintf_filtered (file,
423 _("Debugger's behavior regarding "
424 "pending breakpoints is %s.\n"),
425 value);
426 }
427
428 /* If 1, gdb will automatically use hardware breakpoints for breakpoints
429 set with "break" but falling in read-only memory.
430 If 0, gdb will warn about such breakpoints, but won't automatically
431 use hardware breakpoints. */
432 static int automatic_hardware_breakpoints;
433 static void
434 show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
435 struct cmd_list_element *c,
436 const char *value)
437 {
438 fprintf_filtered (file,
439 _("Automatic usage of hardware breakpoints is %s.\n"),
440 value);
441 }
442
443 /* If on, GDB keeps breakpoints inserted even if the inferior is
444 stopped, and immediately inserts any new breakpoints as soon as
445 they're created. If off (default), GDB keeps breakpoints off of
446 the target as long as possible. That is, it delays inserting
447 breakpoints until the next resume, and removes them again when the
448 target fully stops. This is a bit safer in case GDB crashes while
449 processing user input. */
450 static int always_inserted_mode = 0;
451
452 static void
453 show_always_inserted_mode (struct ui_file *file, int from_tty,
454 struct cmd_list_element *c, const char *value)
455 {
456 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
457 value);
458 }
459
460 /* See breakpoint.h. */
461
462 int
463 breakpoints_should_be_inserted_now (void)
464 {
465 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
466 {
467 /* If breakpoints are global, they should be inserted even if no
468 thread under gdb's control is running, or even if there are
469 no threads under GDB's control yet. */
470 return 1;
471 }
472 else if (target_has_execution)
473 {
474 if (always_inserted_mode)
475 {
476 /* The user wants breakpoints inserted even if all threads
477 are stopped. */
478 return 1;
479 }
480
481 if (threads_are_executing ())
482 return 1;
483 }
484 return 0;
485 }
486
487 static const char condition_evaluation_both[] = "host or target";
488
489 /* Modes for breakpoint condition evaluation. */
490 static const char condition_evaluation_auto[] = "auto";
491 static const char condition_evaluation_host[] = "host";
492 static const char condition_evaluation_target[] = "target";
493 static const char *const condition_evaluation_enums[] = {
494 condition_evaluation_auto,
495 condition_evaluation_host,
496 condition_evaluation_target,
497 NULL
498 };
499
500 /* Global that holds the current mode for breakpoint condition evaluation. */
501 static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
502
503 /* Global that we use to display information to the user (gets its value from
504 condition_evaluation_mode_1. */
505 static const char *condition_evaluation_mode = condition_evaluation_auto;
506
507 /* Translate a condition evaluation mode MODE into either "host"
508 or "target". This is used mostly to translate from "auto" to the
509 real setting that is being used. It returns the translated
510 evaluation mode. */
511
512 static const char *
513 translate_condition_evaluation_mode (const char *mode)
514 {
515 if (mode == condition_evaluation_auto)
516 {
517 if (target_supports_evaluation_of_breakpoint_conditions ())
518 return condition_evaluation_target;
519 else
520 return condition_evaluation_host;
521 }
522 else
523 return mode;
524 }
525
526 /* Discovers what condition_evaluation_auto translates to. */
527
528 static const char *
529 breakpoint_condition_evaluation_mode (void)
530 {
531 return translate_condition_evaluation_mode (condition_evaluation_mode);
532 }
533
534 /* Return true if GDB should evaluate breakpoint conditions or false
535 otherwise. */
536
537 static int
538 gdb_evaluates_breakpoint_condition_p (void)
539 {
540 const char *mode = breakpoint_condition_evaluation_mode ();
541
542 return (mode == condition_evaluation_host);
543 }
544
545 void _initialize_breakpoint (void);
546
547 /* Are we executing breakpoint commands? */
548 static int executing_breakpoint_commands;
549
550 /* Are overlay event breakpoints enabled? */
551 static int overlay_events_enabled;
552
553 /* See description in breakpoint.h. */
554 int target_exact_watchpoints = 0;
555
556 /* Walk the following statement or block through all breakpoints.
557 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
558 current breakpoint. */
559
560 #define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
561
562 #define ALL_BREAKPOINTS_SAFE(B,TMP) \
563 for (B = breakpoint_chain; \
564 B ? (TMP=B->next, 1): 0; \
565 B = TMP)
566
567 /* Similar iterator for the low-level breakpoints. SAFE variant is
568 not provided so update_global_location_list must not be called
569 while executing the block of ALL_BP_LOCATIONS. */
570
571 #define ALL_BP_LOCATIONS(B,BP_TMP) \
572 for (BP_TMP = bp_location; \
573 BP_TMP < bp_location + bp_location_count && (B = *BP_TMP); \
574 BP_TMP++)
575
576 /* Iterates through locations with address ADDRESS for the currently selected
577 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
578 to where the loop should start from.
579 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
580 appropriate location to start with. */
581
582 #define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
583 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
584 BP_LOCP_TMP = BP_LOCP_START; \
585 BP_LOCP_START \
586 && (BP_LOCP_TMP < bp_location + bp_location_count \
587 && (*BP_LOCP_TMP)->address == ADDRESS); \
588 BP_LOCP_TMP++)
589
590 /* Iterator for tracepoints only. */
591
592 #define ALL_TRACEPOINTS(B) \
593 for (B = breakpoint_chain; B; B = B->next) \
594 if (is_tracepoint (B))
595
596 /* Chains of all breakpoints defined. */
597
598 struct breakpoint *breakpoint_chain;
599
600 /* Array is sorted by bp_location_compare - primarily by the ADDRESS. */
601
602 static struct bp_location **bp_location;
603
604 /* Number of elements of BP_LOCATION. */
605
606 static unsigned bp_location_count;
607
608 /* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
609 ADDRESS for the current elements of BP_LOCATION which get a valid
610 result from bp_location_has_shadow. You can use it for roughly
611 limiting the subrange of BP_LOCATION to scan for shadow bytes for
612 an address you need to read. */
613
614 static CORE_ADDR bp_location_placed_address_before_address_max;
615
616 /* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
617 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
618 BP_LOCATION which get a valid result from bp_location_has_shadow.
619 You can use it for roughly limiting the subrange of BP_LOCATION to
620 scan for shadow bytes for an address you need to read. */
621
622 static CORE_ADDR bp_location_shadow_len_after_address_max;
623
624 /* The locations that no longer correspond to any breakpoint, unlinked
625 from bp_location array, but for which a hit may still be reported
626 by a target. */
627 VEC(bp_location_p) *moribund_locations = NULL;
628
629 /* Number of last breakpoint made. */
630
631 static int breakpoint_count;
632
633 /* The value of `breakpoint_count' before the last command that
634 created breakpoints. If the last (break-like) command created more
635 than one breakpoint, then the difference between BREAKPOINT_COUNT
636 and PREV_BREAKPOINT_COUNT is more than one. */
637 static int prev_breakpoint_count;
638
639 /* Number of last tracepoint made. */
640
641 static int tracepoint_count;
642
643 static struct cmd_list_element *breakpoint_set_cmdlist;
644 static struct cmd_list_element *breakpoint_show_cmdlist;
645 struct cmd_list_element *save_cmdlist;
646
647 /* Return whether a breakpoint is an active enabled breakpoint. */
648 static int
649 breakpoint_enabled (struct breakpoint *b)
650 {
651 return (b->enable_state == bp_enabled);
652 }
653
654 /* Set breakpoint count to NUM. */
655
656 static void
657 set_breakpoint_count (int num)
658 {
659 prev_breakpoint_count = breakpoint_count;
660 breakpoint_count = num;
661 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
662 }
663
664 /* Used by `start_rbreak_breakpoints' below, to record the current
665 breakpoint count before "rbreak" creates any breakpoint. */
666 static int rbreak_start_breakpoint_count;
667
668 /* Called at the start an "rbreak" command to record the first
669 breakpoint made. */
670
671 void
672 start_rbreak_breakpoints (void)
673 {
674 rbreak_start_breakpoint_count = breakpoint_count;
675 }
676
677 /* Called at the end of an "rbreak" command to record the last
678 breakpoint made. */
679
680 void
681 end_rbreak_breakpoints (void)
682 {
683 prev_breakpoint_count = rbreak_start_breakpoint_count;
684 }
685
686 /* Used in run_command to zero the hit count when a new run starts. */
687
688 void
689 clear_breakpoint_hit_counts (void)
690 {
691 struct breakpoint *b;
692
693 ALL_BREAKPOINTS (b)
694 b->hit_count = 0;
695 }
696
697 /* Allocate a new counted_command_line with reference count of 1.
698 The new structure owns COMMANDS. */
699
700 static struct counted_command_line *
701 alloc_counted_command_line (struct command_line *commands)
702 {
703 struct counted_command_line *result
704 = xmalloc (sizeof (struct counted_command_line));
705
706 result->refc = 1;
707 result->commands = commands;
708 return result;
709 }
710
711 /* Increment reference count. This does nothing if CMD is NULL. */
712
713 static void
714 incref_counted_command_line (struct counted_command_line *cmd)
715 {
716 if (cmd)
717 ++cmd->refc;
718 }
719
720 /* Decrement reference count. If the reference count reaches 0,
721 destroy the counted_command_line. Sets *CMDP to NULL. This does
722 nothing if *CMDP is NULL. */
723
724 static void
725 decref_counted_command_line (struct counted_command_line **cmdp)
726 {
727 if (*cmdp)
728 {
729 if (--(*cmdp)->refc == 0)
730 {
731 free_command_lines (&(*cmdp)->commands);
732 xfree (*cmdp);
733 }
734 *cmdp = NULL;
735 }
736 }
737
738 /* A cleanup function that calls decref_counted_command_line. */
739
740 static void
741 do_cleanup_counted_command_line (void *arg)
742 {
743 decref_counted_command_line (arg);
744 }
745
746 /* Create a cleanup that calls decref_counted_command_line on the
747 argument. */
748
749 static struct cleanup *
750 make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
751 {
752 return make_cleanup (do_cleanup_counted_command_line, cmdp);
753 }
754
755 \f
756 /* Return the breakpoint with the specified number, or NULL
757 if the number does not refer to an existing breakpoint. */
758
759 struct breakpoint *
760 get_breakpoint (int num)
761 {
762 struct breakpoint *b;
763
764 ALL_BREAKPOINTS (b)
765 if (b->number == num)
766 return b;
767
768 return NULL;
769 }
770
771 \f
772
773 /* Mark locations as "conditions have changed" in case the target supports
774 evaluating conditions on its side. */
775
776 static void
777 mark_breakpoint_modified (struct breakpoint *b)
778 {
779 struct bp_location *loc;
780
781 /* This is only meaningful if the target is
782 evaluating conditions and if the user has
783 opted for condition evaluation on the target's
784 side. */
785 if (gdb_evaluates_breakpoint_condition_p ()
786 || !target_supports_evaluation_of_breakpoint_conditions ())
787 return;
788
789 if (!is_breakpoint (b))
790 return;
791
792 for (loc = b->loc; loc; loc = loc->next)
793 loc->condition_changed = condition_modified;
794 }
795
796 /* Mark location as "conditions have changed" in case the target supports
797 evaluating conditions on its side. */
798
799 static void
800 mark_breakpoint_location_modified (struct bp_location *loc)
801 {
802 /* This is only meaningful if the target is
803 evaluating conditions and if the user has
804 opted for condition evaluation on the target's
805 side. */
806 if (gdb_evaluates_breakpoint_condition_p ()
807 || !target_supports_evaluation_of_breakpoint_conditions ())
808
809 return;
810
811 if (!is_breakpoint (loc->owner))
812 return;
813
814 loc->condition_changed = condition_modified;
815 }
816
817 /* Sets the condition-evaluation mode using the static global
818 condition_evaluation_mode. */
819
820 static void
821 set_condition_evaluation_mode (char *args, int from_tty,
822 struct cmd_list_element *c)
823 {
824 const char *old_mode, *new_mode;
825
826 if ((condition_evaluation_mode_1 == condition_evaluation_target)
827 && !target_supports_evaluation_of_breakpoint_conditions ())
828 {
829 condition_evaluation_mode_1 = condition_evaluation_mode;
830 warning (_("Target does not support breakpoint condition evaluation.\n"
831 "Using host evaluation mode instead."));
832 return;
833 }
834
835 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
836 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
837
838 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
839 settings was "auto". */
840 condition_evaluation_mode = condition_evaluation_mode_1;
841
842 /* Only update the mode if the user picked a different one. */
843 if (new_mode != old_mode)
844 {
845 struct bp_location *loc, **loc_tmp;
846 /* If the user switched to a different evaluation mode, we
847 need to synch the changes with the target as follows:
848
849 "host" -> "target": Send all (valid) conditions to the target.
850 "target" -> "host": Remove all the conditions from the target.
851 */
852
853 if (new_mode == condition_evaluation_target)
854 {
855 /* Mark everything modified and synch conditions with the
856 target. */
857 ALL_BP_LOCATIONS (loc, loc_tmp)
858 mark_breakpoint_location_modified (loc);
859 }
860 else
861 {
862 /* Manually mark non-duplicate locations to synch conditions
863 with the target. We do this to remove all the conditions the
864 target knows about. */
865 ALL_BP_LOCATIONS (loc, loc_tmp)
866 if (is_breakpoint (loc->owner) && loc->inserted)
867 loc->needs_update = 1;
868 }
869
870 /* Do the update. */
871 update_global_location_list (UGLL_MAY_INSERT);
872 }
873
874 return;
875 }
876
877 /* Shows the current mode of breakpoint condition evaluation. Explicitly shows
878 what "auto" is translating to. */
879
880 static void
881 show_condition_evaluation_mode (struct ui_file *file, int from_tty,
882 struct cmd_list_element *c, const char *value)
883 {
884 if (condition_evaluation_mode == condition_evaluation_auto)
885 fprintf_filtered (file,
886 _("Breakpoint condition evaluation "
887 "mode is %s (currently %s).\n"),
888 value,
889 breakpoint_condition_evaluation_mode ());
890 else
891 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
892 value);
893 }
894
895 /* A comparison function for bp_location AP and BP that is used by
896 bsearch. This comparison function only cares about addresses, unlike
897 the more general bp_location_compare function. */
898
899 static int
900 bp_location_compare_addrs (const void *ap, const void *bp)
901 {
902 struct bp_location *a = *(void **) ap;
903 struct bp_location *b = *(void **) bp;
904
905 if (a->address == b->address)
906 return 0;
907 else
908 return ((a->address > b->address) - (a->address < b->address));
909 }
910
911 /* Helper function to skip all bp_locations with addresses
912 less than ADDRESS. It returns the first bp_location that
913 is greater than or equal to ADDRESS. If none is found, just
914 return NULL. */
915
916 static struct bp_location **
917 get_first_locp_gte_addr (CORE_ADDR address)
918 {
919 struct bp_location dummy_loc;
920 struct bp_location *dummy_locp = &dummy_loc;
921 struct bp_location **locp_found = NULL;
922
923 /* Initialize the dummy location's address field. */
924 memset (&dummy_loc, 0, sizeof (struct bp_location));
925 dummy_loc.address = address;
926
927 /* Find a close match to the first location at ADDRESS. */
928 locp_found = bsearch (&dummy_locp, bp_location, bp_location_count,
929 sizeof (struct bp_location **),
930 bp_location_compare_addrs);
931
932 /* Nothing was found, nothing left to do. */
933 if (locp_found == NULL)
934 return NULL;
935
936 /* We may have found a location that is at ADDRESS but is not the first in the
937 location's list. Go backwards (if possible) and locate the first one. */
938 while ((locp_found - 1) >= bp_location
939 && (*(locp_found - 1))->address == address)
940 locp_found--;
941
942 return locp_found;
943 }
944
945 void
946 set_breakpoint_condition (struct breakpoint *b, char *exp,
947 int from_tty)
948 {
949 xfree (b->cond_string);
950 b->cond_string = NULL;
951
952 if (is_watchpoint (b))
953 {
954 struct watchpoint *w = (struct watchpoint *) b;
955
956 xfree (w->cond_exp);
957 w->cond_exp = NULL;
958 }
959 else
960 {
961 struct bp_location *loc;
962
963 for (loc = b->loc; loc; loc = loc->next)
964 {
965 xfree (loc->cond);
966 loc->cond = NULL;
967
968 /* No need to free the condition agent expression
969 bytecode (if we have one). We will handle this
970 when we go through update_global_location_list. */
971 }
972 }
973
974 if (*exp == 0)
975 {
976 if (from_tty)
977 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
978 }
979 else
980 {
981 const char *arg = exp;
982
983 /* I don't know if it matters whether this is the string the user
984 typed in or the decompiled expression. */
985 b->cond_string = xstrdup (arg);
986 b->condition_not_parsed = 0;
987
988 if (is_watchpoint (b))
989 {
990 struct watchpoint *w = (struct watchpoint *) b;
991
992 innermost_block = NULL;
993 arg = exp;
994 w->cond_exp = parse_exp_1 (&arg, 0, 0, 0);
995 if (*arg)
996 error (_("Junk at end of expression"));
997 w->cond_exp_valid_block = innermost_block;
998 }
999 else
1000 {
1001 struct bp_location *loc;
1002
1003 for (loc = b->loc; loc; loc = loc->next)
1004 {
1005 arg = exp;
1006 loc->cond =
1007 parse_exp_1 (&arg, loc->address,
1008 block_for_pc (loc->address), 0);
1009 if (*arg)
1010 error (_("Junk at end of expression"));
1011 }
1012 }
1013 }
1014 mark_breakpoint_modified (b);
1015
1016 observer_notify_breakpoint_modified (b);
1017 }
1018
1019 /* Completion for the "condition" command. */
1020
1021 static VEC (char_ptr) *
1022 condition_completer (struct cmd_list_element *cmd,
1023 const char *text, const char *word)
1024 {
1025 const char *space;
1026
1027 text = skip_spaces_const (text);
1028 space = skip_to_space_const (text);
1029 if (*space == '\0')
1030 {
1031 int len;
1032 struct breakpoint *b;
1033 VEC (char_ptr) *result = NULL;
1034
1035 if (text[0] == '$')
1036 {
1037 /* We don't support completion of history indices. */
1038 if (isdigit (text[1]))
1039 return NULL;
1040 return complete_internalvar (&text[1]);
1041 }
1042
1043 /* We're completing the breakpoint number. */
1044 len = strlen (text);
1045
1046 ALL_BREAKPOINTS (b)
1047 {
1048 char number[50];
1049
1050 xsnprintf (number, sizeof (number), "%d", b->number);
1051
1052 if (strncmp (number, text, len) == 0)
1053 VEC_safe_push (char_ptr, result, xstrdup (number));
1054 }
1055
1056 return result;
1057 }
1058
1059 /* We're completing the expression part. */
1060 text = skip_spaces_const (space);
1061 return expression_completer (cmd, text, word);
1062 }
1063
1064 /* condition N EXP -- set break condition of breakpoint N to EXP. */
1065
1066 static void
1067 condition_command (char *arg, int from_tty)
1068 {
1069 struct breakpoint *b;
1070 char *p;
1071 int bnum;
1072
1073 if (arg == 0)
1074 error_no_arg (_("breakpoint number"));
1075
1076 p = arg;
1077 bnum = get_number (&p);
1078 if (bnum == 0)
1079 error (_("Bad breakpoint argument: '%s'"), arg);
1080
1081 ALL_BREAKPOINTS (b)
1082 if (b->number == bnum)
1083 {
1084 /* Check if this breakpoint has a "stop" method implemented in an
1085 extension language. This method and conditions entered into GDB
1086 from the CLI are mutually exclusive. */
1087 const struct extension_language_defn *extlang
1088 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1089
1090 if (extlang != NULL)
1091 {
1092 error (_("Only one stop condition allowed. There is currently"
1093 " a %s stop condition defined for this breakpoint."),
1094 ext_lang_capitalized_name (extlang));
1095 }
1096 set_breakpoint_condition (b, p, from_tty);
1097
1098 if (is_breakpoint (b))
1099 update_global_location_list (UGLL_MAY_INSERT);
1100
1101 return;
1102 }
1103
1104 error (_("No breakpoint number %d."), bnum);
1105 }
1106
1107 /* Check that COMMAND do not contain commands that are suitable
1108 only for tracepoints and not suitable for ordinary breakpoints.
1109 Throw if any such commands is found. */
1110
1111 static void
1112 check_no_tracepoint_commands (struct command_line *commands)
1113 {
1114 struct command_line *c;
1115
1116 for (c = commands; c; c = c->next)
1117 {
1118 int i;
1119
1120 if (c->control_type == while_stepping_control)
1121 error (_("The 'while-stepping' command can "
1122 "only be used for tracepoints"));
1123
1124 for (i = 0; i < c->body_count; ++i)
1125 check_no_tracepoint_commands ((c->body_list)[i]);
1126
1127 /* Not that command parsing removes leading whitespace and comment
1128 lines and also empty lines. So, we only need to check for
1129 command directly. */
1130 if (strstr (c->line, "collect ") == c->line)
1131 error (_("The 'collect' command can only be used for tracepoints"));
1132
1133 if (strstr (c->line, "teval ") == c->line)
1134 error (_("The 'teval' command can only be used for tracepoints"));
1135 }
1136 }
1137
1138 /* Encapsulate tests for different types of tracepoints. */
1139
1140 static int
1141 is_tracepoint_type (enum bptype type)
1142 {
1143 return (type == bp_tracepoint
1144 || type == bp_fast_tracepoint
1145 || type == bp_static_tracepoint);
1146 }
1147
1148 int
1149 is_tracepoint (const struct breakpoint *b)
1150 {
1151 return is_tracepoint_type (b->type);
1152 }
1153
1154 /* A helper function that validates that COMMANDS are valid for a
1155 breakpoint. This function will throw an exception if a problem is
1156 found. */
1157
1158 static void
1159 validate_commands_for_breakpoint (struct breakpoint *b,
1160 struct command_line *commands)
1161 {
1162 if (is_tracepoint (b))
1163 {
1164 struct tracepoint *t = (struct tracepoint *) b;
1165 struct command_line *c;
1166 struct command_line *while_stepping = 0;
1167
1168 /* Reset the while-stepping step count. The previous commands
1169 might have included a while-stepping action, while the new
1170 ones might not. */
1171 t->step_count = 0;
1172
1173 /* We need to verify that each top-level element of commands is
1174 valid for tracepoints, that there's at most one
1175 while-stepping element, and that the while-stepping's body
1176 has valid tracing commands excluding nested while-stepping.
1177 We also need to validate the tracepoint action line in the
1178 context of the tracepoint --- validate_actionline actually
1179 has side effects, like setting the tracepoint's
1180 while-stepping STEP_COUNT, in addition to checking if the
1181 collect/teval actions parse and make sense in the
1182 tracepoint's context. */
1183 for (c = commands; c; c = c->next)
1184 {
1185 if (c->control_type == while_stepping_control)
1186 {
1187 if (b->type == bp_fast_tracepoint)
1188 error (_("The 'while-stepping' command "
1189 "cannot be used for fast tracepoint"));
1190 else if (b->type == bp_static_tracepoint)
1191 error (_("The 'while-stepping' command "
1192 "cannot be used for static tracepoint"));
1193
1194 if (while_stepping)
1195 error (_("The 'while-stepping' command "
1196 "can be used only once"));
1197 else
1198 while_stepping = c;
1199 }
1200
1201 validate_actionline (c->line, b);
1202 }
1203 if (while_stepping)
1204 {
1205 struct command_line *c2;
1206
1207 gdb_assert (while_stepping->body_count == 1);
1208 c2 = while_stepping->body_list[0];
1209 for (; c2; c2 = c2->next)
1210 {
1211 if (c2->control_type == while_stepping_control)
1212 error (_("The 'while-stepping' command cannot be nested"));
1213 }
1214 }
1215 }
1216 else
1217 {
1218 check_no_tracepoint_commands (commands);
1219 }
1220 }
1221
1222 /* Return a vector of all the static tracepoints set at ADDR. The
1223 caller is responsible for releasing the vector. */
1224
1225 VEC(breakpoint_p) *
1226 static_tracepoints_here (CORE_ADDR addr)
1227 {
1228 struct breakpoint *b;
1229 VEC(breakpoint_p) *found = 0;
1230 struct bp_location *loc;
1231
1232 ALL_BREAKPOINTS (b)
1233 if (b->type == bp_static_tracepoint)
1234 {
1235 for (loc = b->loc; loc; loc = loc->next)
1236 if (loc->address == addr)
1237 VEC_safe_push(breakpoint_p, found, b);
1238 }
1239
1240 return found;
1241 }
1242
1243 /* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
1244 validate that only allowed commands are included. */
1245
1246 void
1247 breakpoint_set_commands (struct breakpoint *b,
1248 struct command_line *commands)
1249 {
1250 validate_commands_for_breakpoint (b, commands);
1251
1252 decref_counted_command_line (&b->commands);
1253 b->commands = alloc_counted_command_line (commands);
1254 observer_notify_breakpoint_modified (b);
1255 }
1256
1257 /* Set the internal `silent' flag on the breakpoint. Note that this
1258 is not the same as the "silent" that may appear in the breakpoint's
1259 commands. */
1260
1261 void
1262 breakpoint_set_silent (struct breakpoint *b, int silent)
1263 {
1264 int old_silent = b->silent;
1265
1266 b->silent = silent;
1267 if (old_silent != silent)
1268 observer_notify_breakpoint_modified (b);
1269 }
1270
1271 /* Set the thread for this breakpoint. If THREAD is -1, make the
1272 breakpoint work for any thread. */
1273
1274 void
1275 breakpoint_set_thread (struct breakpoint *b, int thread)
1276 {
1277 int old_thread = b->thread;
1278
1279 b->thread = thread;
1280 if (old_thread != thread)
1281 observer_notify_breakpoint_modified (b);
1282 }
1283
1284 /* Set the task for this breakpoint. If TASK is 0, make the
1285 breakpoint work for any task. */
1286
1287 void
1288 breakpoint_set_task (struct breakpoint *b, int task)
1289 {
1290 int old_task = b->task;
1291
1292 b->task = task;
1293 if (old_task != task)
1294 observer_notify_breakpoint_modified (b);
1295 }
1296
1297 void
1298 check_tracepoint_command (char *line, void *closure)
1299 {
1300 struct breakpoint *b = closure;
1301
1302 validate_actionline (line, b);
1303 }
1304
1305 /* A structure used to pass information through
1306 map_breakpoint_numbers. */
1307
1308 struct commands_info
1309 {
1310 /* True if the command was typed at a tty. */
1311 int from_tty;
1312
1313 /* The breakpoint range spec. */
1314 char *arg;
1315
1316 /* Non-NULL if the body of the commands are being read from this
1317 already-parsed command. */
1318 struct command_line *control;
1319
1320 /* The command lines read from the user, or NULL if they have not
1321 yet been read. */
1322 struct counted_command_line *cmd;
1323 };
1324
1325 /* A callback for map_breakpoint_numbers that sets the commands for
1326 commands_command. */
1327
1328 static void
1329 do_map_commands_command (struct breakpoint *b, void *data)
1330 {
1331 struct commands_info *info = data;
1332
1333 if (info->cmd == NULL)
1334 {
1335 struct command_line *l;
1336
1337 if (info->control != NULL)
1338 l = copy_command_lines (info->control->body_list[0]);
1339 else
1340 {
1341 struct cleanup *old_chain;
1342 char *str;
1343
1344 str = xstrprintf (_("Type commands for breakpoint(s) "
1345 "%s, one per line."),
1346 info->arg);
1347
1348 old_chain = make_cleanup (xfree, str);
1349
1350 l = read_command_lines (str,
1351 info->from_tty, 1,
1352 (is_tracepoint (b)
1353 ? check_tracepoint_command : 0),
1354 b);
1355
1356 do_cleanups (old_chain);
1357 }
1358
1359 info->cmd = alloc_counted_command_line (l);
1360 }
1361
1362 /* If a breakpoint was on the list more than once, we don't need to
1363 do anything. */
1364 if (b->commands != info->cmd)
1365 {
1366 validate_commands_for_breakpoint (b, info->cmd->commands);
1367 incref_counted_command_line (info->cmd);
1368 decref_counted_command_line (&b->commands);
1369 b->commands = info->cmd;
1370 observer_notify_breakpoint_modified (b);
1371 }
1372 }
1373
1374 static void
1375 commands_command_1 (char *arg, int from_tty,
1376 struct command_line *control)
1377 {
1378 struct cleanup *cleanups;
1379 struct commands_info info;
1380
1381 info.from_tty = from_tty;
1382 info.control = control;
1383 info.cmd = NULL;
1384 /* If we read command lines from the user, then `info' will hold an
1385 extra reference to the commands that we must clean up. */
1386 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
1387
1388 if (arg == NULL || !*arg)
1389 {
1390 if (breakpoint_count - prev_breakpoint_count > 1)
1391 arg = xstrprintf ("%d-%d", prev_breakpoint_count + 1,
1392 breakpoint_count);
1393 else if (breakpoint_count > 0)
1394 arg = xstrprintf ("%d", breakpoint_count);
1395 else
1396 {
1397 /* So that we don't try to free the incoming non-NULL
1398 argument in the cleanup below. Mapping breakpoint
1399 numbers will fail in this case. */
1400 arg = NULL;
1401 }
1402 }
1403 else
1404 /* The command loop has some static state, so we need to preserve
1405 our argument. */
1406 arg = xstrdup (arg);
1407
1408 if (arg != NULL)
1409 make_cleanup (xfree, arg);
1410
1411 info.arg = arg;
1412
1413 map_breakpoint_numbers (arg, do_map_commands_command, &info);
1414
1415 if (info.cmd == NULL)
1416 error (_("No breakpoints specified."));
1417
1418 do_cleanups (cleanups);
1419 }
1420
1421 static void
1422 commands_command (char *arg, int from_tty)
1423 {
1424 commands_command_1 (arg, from_tty, NULL);
1425 }
1426
1427 /* Like commands_command, but instead of reading the commands from
1428 input stream, takes them from an already parsed command structure.
1429
1430 This is used by cli-script.c to DTRT with breakpoint commands
1431 that are part of if and while bodies. */
1432 enum command_control_type
1433 commands_from_control_command (char *arg, struct command_line *cmd)
1434 {
1435 commands_command_1 (arg, 0, cmd);
1436 return simple_control;
1437 }
1438
1439 /* Return non-zero if BL->TARGET_INFO contains valid information. */
1440
1441 static int
1442 bp_location_has_shadow (struct bp_location *bl)
1443 {
1444 if (bl->loc_type != bp_loc_software_breakpoint)
1445 return 0;
1446 if (!bl->inserted)
1447 return 0;
1448 if (bl->target_info.shadow_len == 0)
1449 /* BL isn't valid, or doesn't shadow memory. */
1450 return 0;
1451 return 1;
1452 }
1453
1454 /* Update BUF, which is LEN bytes read from the target address
1455 MEMADDR, by replacing a memory breakpoint with its shadowed
1456 contents.
1457
1458 If READBUF is not NULL, this buffer must not overlap with the of
1459 the breakpoint location's shadow_contents buffer. Otherwise, a
1460 failed assertion internal error will be raised. */
1461
1462 static void
1463 one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1464 const gdb_byte *writebuf_org,
1465 ULONGEST memaddr, LONGEST len,
1466 struct bp_target_info *target_info,
1467 struct gdbarch *gdbarch)
1468 {
1469 /* Now do full processing of the found relevant range of elements. */
1470 CORE_ADDR bp_addr = 0;
1471 int bp_size = 0;
1472 int bptoffset = 0;
1473
1474 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1475 current_program_space->aspace, 0))
1476 {
1477 /* The breakpoint is inserted in a different address space. */
1478 return;
1479 }
1480
1481 /* Addresses and length of the part of the breakpoint that
1482 we need to copy. */
1483 bp_addr = target_info->placed_address;
1484 bp_size = target_info->shadow_len;
1485
1486 if (bp_addr + bp_size <= memaddr)
1487 {
1488 /* The breakpoint is entirely before the chunk of memory we are
1489 reading. */
1490 return;
1491 }
1492
1493 if (bp_addr >= memaddr + len)
1494 {
1495 /* The breakpoint is entirely after the chunk of memory we are
1496 reading. */
1497 return;
1498 }
1499
1500 /* Offset within shadow_contents. */
1501 if (bp_addr < memaddr)
1502 {
1503 /* Only copy the second part of the breakpoint. */
1504 bp_size -= memaddr - bp_addr;
1505 bptoffset = memaddr - bp_addr;
1506 bp_addr = memaddr;
1507 }
1508
1509 if (bp_addr + bp_size > memaddr + len)
1510 {
1511 /* Only copy the first part of the breakpoint. */
1512 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1513 }
1514
1515 if (readbuf != NULL)
1516 {
1517 /* Verify that the readbuf buffer does not overlap with the
1518 shadow_contents buffer. */
1519 gdb_assert (target_info->shadow_contents >= readbuf + len
1520 || readbuf >= (target_info->shadow_contents
1521 + target_info->shadow_len));
1522
1523 /* Update the read buffer with this inserted breakpoint's
1524 shadow. */
1525 memcpy (readbuf + bp_addr - memaddr,
1526 target_info->shadow_contents + bptoffset, bp_size);
1527 }
1528 else
1529 {
1530 const unsigned char *bp;
1531 CORE_ADDR addr = target_info->reqstd_address;
1532 int placed_size;
1533
1534 /* Update the shadow with what we want to write to memory. */
1535 memcpy (target_info->shadow_contents + bptoffset,
1536 writebuf_org + bp_addr - memaddr, bp_size);
1537
1538 /* Determine appropriate breakpoint contents and size for this
1539 address. */
1540 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
1541
1542 /* Update the final write buffer with this inserted
1543 breakpoint's INSN. */
1544 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1545 }
1546 }
1547
1548 /* Update BUF, which is LEN bytes read from the target address MEMADDR,
1549 by replacing any memory breakpoints with their shadowed contents.
1550
1551 If READBUF is not NULL, this buffer must not overlap with any of
1552 the breakpoint location's shadow_contents buffers. Otherwise,
1553 a failed assertion internal error will be raised.
1554
1555 The range of shadowed area by each bp_location is:
1556 bl->address - bp_location_placed_address_before_address_max
1557 up to bl->address + bp_location_shadow_len_after_address_max
1558 The range we were requested to resolve shadows for is:
1559 memaddr ... memaddr + len
1560 Thus the safe cutoff boundaries for performance optimization are
1561 memaddr + len <= (bl->address
1562 - bp_location_placed_address_before_address_max)
1563 and:
1564 bl->address + bp_location_shadow_len_after_address_max <= memaddr */
1565
1566 void
1567 breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1568 const gdb_byte *writebuf_org,
1569 ULONGEST memaddr, LONGEST len)
1570 {
1571 /* Left boundary, right boundary and median element of our binary
1572 search. */
1573 unsigned bc_l, bc_r, bc;
1574 size_t i;
1575
1576 /* Find BC_L which is a leftmost element which may affect BUF
1577 content. It is safe to report lower value but a failure to
1578 report higher one. */
1579
1580 bc_l = 0;
1581 bc_r = bp_location_count;
1582 while (bc_l + 1 < bc_r)
1583 {
1584 struct bp_location *bl;
1585
1586 bc = (bc_l + bc_r) / 2;
1587 bl = bp_location[bc];
1588
1589 /* Check first BL->ADDRESS will not overflow due to the added
1590 constant. Then advance the left boundary only if we are sure
1591 the BC element can in no way affect the BUF content (MEMADDR
1592 to MEMADDR + LEN range).
1593
1594 Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1595 offset so that we cannot miss a breakpoint with its shadow
1596 range tail still reaching MEMADDR. */
1597
1598 if ((bl->address + bp_location_shadow_len_after_address_max
1599 >= bl->address)
1600 && (bl->address + bp_location_shadow_len_after_address_max
1601 <= memaddr))
1602 bc_l = bc;
1603 else
1604 bc_r = bc;
1605 }
1606
1607 /* Due to the binary search above, we need to make sure we pick the
1608 first location that's at BC_L's address. E.g., if there are
1609 multiple locations at the same address, BC_L may end up pointing
1610 at a duplicate location, and miss the "master"/"inserted"
1611 location. Say, given locations L1, L2 and L3 at addresses A and
1612 B:
1613
1614 L1@A, L2@A, L3@B, ...
1615
1616 BC_L could end up pointing at location L2, while the "master"
1617 location could be L1. Since the `loc->inserted' flag is only set
1618 on "master" locations, we'd forget to restore the shadow of L1
1619 and L2. */
1620 while (bc_l > 0
1621 && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1622 bc_l--;
1623
1624 /* Now do full processing of the found relevant range of elements. */
1625
1626 for (bc = bc_l; bc < bp_location_count; bc++)
1627 {
1628 struct bp_location *bl = bp_location[bc];
1629 CORE_ADDR bp_addr = 0;
1630 int bp_size = 0;
1631 int bptoffset = 0;
1632
1633 /* bp_location array has BL->OWNER always non-NULL. */
1634 if (bl->owner->type == bp_none)
1635 warning (_("reading through apparently deleted breakpoint #%d?"),
1636 bl->owner->number);
1637
1638 /* Performance optimization: any further element can no longer affect BUF
1639 content. */
1640
1641 if (bl->address >= bp_location_placed_address_before_address_max
1642 && memaddr + len <= (bl->address
1643 - bp_location_placed_address_before_address_max))
1644 break;
1645
1646 if (!bp_location_has_shadow (bl))
1647 continue;
1648
1649 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1650 memaddr, len, &bl->target_info, bl->gdbarch);
1651 }
1652 }
1653
1654 \f
1655
1656 /* Return true if BPT is either a software breakpoint or a hardware
1657 breakpoint. */
1658
1659 int
1660 is_breakpoint (const struct breakpoint *bpt)
1661 {
1662 return (bpt->type == bp_breakpoint
1663 || bpt->type == bp_hardware_breakpoint
1664 || bpt->type == bp_dprintf);
1665 }
1666
1667 /* Return true if BPT is of any hardware watchpoint kind. */
1668
1669 static int
1670 is_hardware_watchpoint (const struct breakpoint *bpt)
1671 {
1672 return (bpt->type == bp_hardware_watchpoint
1673 || bpt->type == bp_read_watchpoint
1674 || bpt->type == bp_access_watchpoint);
1675 }
1676
1677 /* Return true if BPT is of any watchpoint kind, hardware or
1678 software. */
1679
1680 int
1681 is_watchpoint (const struct breakpoint *bpt)
1682 {
1683 return (is_hardware_watchpoint (bpt)
1684 || bpt->type == bp_watchpoint);
1685 }
1686
1687 /* Returns true if the current thread and its running state are safe
1688 to evaluate or update watchpoint B. Watchpoints on local
1689 expressions need to be evaluated in the context of the thread that
1690 was current when the watchpoint was created, and, that thread needs
1691 to be stopped to be able to select the correct frame context.
1692 Watchpoints on global expressions can be evaluated on any thread,
1693 and in any state. It is presently left to the target allowing
1694 memory accesses when threads are running. */
1695
1696 static int
1697 watchpoint_in_thread_scope (struct watchpoint *b)
1698 {
1699 return (b->base.pspace == current_program_space
1700 && (ptid_equal (b->watchpoint_thread, null_ptid)
1701 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1702 && !is_executing (inferior_ptid))));
1703 }
1704
1705 /* Set watchpoint B to disp_del_at_next_stop, even including its possible
1706 associated bp_watchpoint_scope breakpoint. */
1707
1708 static void
1709 watchpoint_del_at_next_stop (struct watchpoint *w)
1710 {
1711 struct breakpoint *b = &w->base;
1712
1713 if (b->related_breakpoint != b)
1714 {
1715 gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1716 gdb_assert (b->related_breakpoint->related_breakpoint == b);
1717 b->related_breakpoint->disposition = disp_del_at_next_stop;
1718 b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1719 b->related_breakpoint = b;
1720 }
1721 b->disposition = disp_del_at_next_stop;
1722 }
1723
1724 /* Extract a bitfield value from value VAL using the bit parameters contained in
1725 watchpoint W. */
1726
1727 static struct value *
1728 extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1729 {
1730 struct value *bit_val;
1731
1732 if (val == NULL)
1733 return NULL;
1734
1735 bit_val = allocate_value (value_type (val));
1736
1737 unpack_value_bitfield (bit_val,
1738 w->val_bitpos,
1739 w->val_bitsize,
1740 value_contents_for_printing (val),
1741 value_offset (val),
1742 val);
1743
1744 return bit_val;
1745 }
1746
1747 /* Assuming that B is a watchpoint:
1748 - Reparse watchpoint expression, if REPARSE is non-zero
1749 - Evaluate expression and store the result in B->val
1750 - Evaluate the condition if there is one, and store the result
1751 in b->loc->cond.
1752 - Update the list of values that must be watched in B->loc.
1753
1754 If the watchpoint disposition is disp_del_at_next_stop, then do
1755 nothing. If this is local watchpoint that is out of scope, delete
1756 it.
1757
1758 Even with `set breakpoint always-inserted on' the watchpoints are
1759 removed + inserted on each stop here. Normal breakpoints must
1760 never be removed because they might be missed by a running thread
1761 when debugging in non-stop mode. On the other hand, hardware
1762 watchpoints (is_hardware_watchpoint; processed here) are specific
1763 to each LWP since they are stored in each LWP's hardware debug
1764 registers. Therefore, such LWP must be stopped first in order to
1765 be able to modify its hardware watchpoints.
1766
1767 Hardware watchpoints must be reset exactly once after being
1768 presented to the user. It cannot be done sooner, because it would
1769 reset the data used to present the watchpoint hit to the user. And
1770 it must not be done later because it could display the same single
1771 watchpoint hit during multiple GDB stops. Note that the latter is
1772 relevant only to the hardware watchpoint types bp_read_watchpoint
1773 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1774 not user-visible - its hit is suppressed if the memory content has
1775 not changed.
1776
1777 The following constraints influence the location where we can reset
1778 hardware watchpoints:
1779
1780 * target_stopped_by_watchpoint and target_stopped_data_address are
1781 called several times when GDB stops.
1782
1783 [linux]
1784 * Multiple hardware watchpoints can be hit at the same time,
1785 causing GDB to stop. GDB only presents one hardware watchpoint
1786 hit at a time as the reason for stopping, and all the other hits
1787 are presented later, one after the other, each time the user
1788 requests the execution to be resumed. Execution is not resumed
1789 for the threads still having pending hit event stored in
1790 LWP_INFO->STATUS. While the watchpoint is already removed from
1791 the inferior on the first stop the thread hit event is kept being
1792 reported from its cached value by linux_nat_stopped_data_address
1793 until the real thread resume happens after the watchpoint gets
1794 presented and thus its LWP_INFO->STATUS gets reset.
1795
1796 Therefore the hardware watchpoint hit can get safely reset on the
1797 watchpoint removal from inferior. */
1798
1799 static void
1800 update_watchpoint (struct watchpoint *b, int reparse)
1801 {
1802 int within_current_scope;
1803 struct frame_id saved_frame_id;
1804 int frame_saved;
1805
1806 /* If this is a local watchpoint, we only want to check if the
1807 watchpoint frame is in scope if the current thread is the thread
1808 that was used to create the watchpoint. */
1809 if (!watchpoint_in_thread_scope (b))
1810 return;
1811
1812 if (b->base.disposition == disp_del_at_next_stop)
1813 return;
1814
1815 frame_saved = 0;
1816
1817 /* Determine if the watchpoint is within scope. */
1818 if (b->exp_valid_block == NULL)
1819 within_current_scope = 1;
1820 else
1821 {
1822 struct frame_info *fi = get_current_frame ();
1823 struct gdbarch *frame_arch = get_frame_arch (fi);
1824 CORE_ADDR frame_pc = get_frame_pc (fi);
1825
1826 /* If we're in a function epilogue, unwinding may not work
1827 properly, so do not attempt to recreate locations at this
1828 point. See similar comments in watchpoint_check. */
1829 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
1830 return;
1831
1832 /* Save the current frame's ID so we can restore it after
1833 evaluating the watchpoint expression on its own frame. */
1834 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1835 took a frame parameter, so that we didn't have to change the
1836 selected frame. */
1837 frame_saved = 1;
1838 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1839
1840 fi = frame_find_by_id (b->watchpoint_frame);
1841 within_current_scope = (fi != NULL);
1842 if (within_current_scope)
1843 select_frame (fi);
1844 }
1845
1846 /* We don't free locations. They are stored in the bp_location array
1847 and update_global_location_list will eventually delete them and
1848 remove breakpoints if needed. */
1849 b->base.loc = NULL;
1850
1851 if (within_current_scope && reparse)
1852 {
1853 const char *s;
1854
1855 if (b->exp)
1856 {
1857 xfree (b->exp);
1858 b->exp = NULL;
1859 }
1860 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1861 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
1862 /* If the meaning of expression itself changed, the old value is
1863 no longer relevant. We don't want to report a watchpoint hit
1864 to the user when the old value and the new value may actually
1865 be completely different objects. */
1866 value_free (b->val);
1867 b->val = NULL;
1868 b->val_valid = 0;
1869
1870 /* Note that unlike with breakpoints, the watchpoint's condition
1871 expression is stored in the breakpoint object, not in the
1872 locations (re)created below. */
1873 if (b->base.cond_string != NULL)
1874 {
1875 if (b->cond_exp != NULL)
1876 {
1877 xfree (b->cond_exp);
1878 b->cond_exp = NULL;
1879 }
1880
1881 s = b->base.cond_string;
1882 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
1883 }
1884 }
1885
1886 /* If we failed to parse the expression, for example because
1887 it refers to a global variable in a not-yet-loaded shared library,
1888 don't try to insert watchpoint. We don't automatically delete
1889 such watchpoint, though, since failure to parse expression
1890 is different from out-of-scope watchpoint. */
1891 if (!target_has_execution)
1892 {
1893 /* Without execution, memory can't change. No use to try and
1894 set watchpoint locations. The watchpoint will be reset when
1895 the target gains execution, through breakpoint_re_set. */
1896 if (!can_use_hw_watchpoints)
1897 {
1898 if (b->base.ops->works_in_software_mode (&b->base))
1899 b->base.type = bp_watchpoint;
1900 else
1901 error (_("Can't set read/access watchpoint when "
1902 "hardware watchpoints are disabled."));
1903 }
1904 }
1905 else if (within_current_scope && b->exp)
1906 {
1907 int pc = 0;
1908 struct value *val_chain, *v, *result, *next;
1909 struct program_space *frame_pspace;
1910
1911 fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain, 0);
1912
1913 /* Avoid setting b->val if it's already set. The meaning of
1914 b->val is 'the last value' user saw, and we should update
1915 it only if we reported that last value to user. As it
1916 happens, the code that reports it updates b->val directly.
1917 We don't keep track of the memory value for masked
1918 watchpoints. */
1919 if (!b->val_valid && !is_masked_watchpoint (&b->base))
1920 {
1921 if (b->val_bitsize != 0)
1922 {
1923 v = extract_bitfield_from_watchpoint_value (b, v);
1924 if (v != NULL)
1925 release_value (v);
1926 }
1927 b->val = v;
1928 b->val_valid = 1;
1929 }
1930
1931 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1932
1933 /* Look at each value on the value chain. */
1934 for (v = val_chain; v; v = value_next (v))
1935 {
1936 /* If it's a memory location, and GDB actually needed
1937 its contents to evaluate the expression, then we
1938 must watch it. If the first value returned is
1939 still lazy, that means an error occurred reading it;
1940 watch it anyway in case it becomes readable. */
1941 if (VALUE_LVAL (v) == lval_memory
1942 && (v == val_chain || ! value_lazy (v)))
1943 {
1944 struct type *vtype = check_typedef (value_type (v));
1945
1946 /* We only watch structs and arrays if user asked
1947 for it explicitly, never if they just happen to
1948 appear in the middle of some value chain. */
1949 if (v == result
1950 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1951 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1952 {
1953 CORE_ADDR addr;
1954 int type;
1955 struct bp_location *loc, **tmp;
1956 int bitpos = 0, bitsize = 0;
1957
1958 if (value_bitsize (v) != 0)
1959 {
1960 /* Extract the bit parameters out from the bitfield
1961 sub-expression. */
1962 bitpos = value_bitpos (v);
1963 bitsize = value_bitsize (v);
1964 }
1965 else if (v == result && b->val_bitsize != 0)
1966 {
1967 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
1968 lvalue whose bit parameters are saved in the fields
1969 VAL_BITPOS and VAL_BITSIZE. */
1970 bitpos = b->val_bitpos;
1971 bitsize = b->val_bitsize;
1972 }
1973
1974 addr = value_address (v);
1975 if (bitsize != 0)
1976 {
1977 /* Skip the bytes that don't contain the bitfield. */
1978 addr += bitpos / 8;
1979 }
1980
1981 type = hw_write;
1982 if (b->base.type == bp_read_watchpoint)
1983 type = hw_read;
1984 else if (b->base.type == bp_access_watchpoint)
1985 type = hw_access;
1986
1987 loc = allocate_bp_location (&b->base);
1988 for (tmp = &(b->base.loc); *tmp != NULL; tmp = &((*tmp)->next))
1989 ;
1990 *tmp = loc;
1991 loc->gdbarch = get_type_arch (value_type (v));
1992
1993 loc->pspace = frame_pspace;
1994 loc->address = addr;
1995
1996 if (bitsize != 0)
1997 {
1998 /* Just cover the bytes that make up the bitfield. */
1999 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2000 }
2001 else
2002 loc->length = TYPE_LENGTH (value_type (v));
2003
2004 loc->watchpoint_type = type;
2005 }
2006 }
2007 }
2008
2009 /* Change the type of breakpoint between hardware assisted or
2010 an ordinary watchpoint depending on the hardware support
2011 and free hardware slots. REPARSE is set when the inferior
2012 is started. */
2013 if (reparse)
2014 {
2015 int reg_cnt;
2016 enum bp_loc_type loc_type;
2017 struct bp_location *bl;
2018
2019 reg_cnt = can_use_hardware_watchpoint (val_chain);
2020
2021 if (reg_cnt)
2022 {
2023 int i, target_resources_ok, other_type_used;
2024 enum bptype type;
2025
2026 /* Use an exact watchpoint when there's only one memory region to be
2027 watched, and only one debug register is needed to watch it. */
2028 b->exact = target_exact_watchpoints && reg_cnt == 1;
2029
2030 /* We need to determine how many resources are already
2031 used for all other hardware watchpoints plus this one
2032 to see if we still have enough resources to also fit
2033 this watchpoint in as well. */
2034
2035 /* If this is a software watchpoint, we try to turn it
2036 to a hardware one -- count resources as if B was of
2037 hardware watchpoint type. */
2038 type = b->base.type;
2039 if (type == bp_watchpoint)
2040 type = bp_hardware_watchpoint;
2041
2042 /* This watchpoint may or may not have been placed on
2043 the list yet at this point (it won't be in the list
2044 if we're trying to create it for the first time,
2045 through watch_command), so always account for it
2046 manually. */
2047
2048 /* Count resources used by all watchpoints except B. */
2049 i = hw_watchpoint_used_count_others (&b->base, type, &other_type_used);
2050
2051 /* Add in the resources needed for B. */
2052 i += hw_watchpoint_use_count (&b->base);
2053
2054 target_resources_ok
2055 = target_can_use_hardware_watchpoint (type, i, other_type_used);
2056 if (target_resources_ok <= 0)
2057 {
2058 int sw_mode = b->base.ops->works_in_software_mode (&b->base);
2059
2060 if (target_resources_ok == 0 && !sw_mode)
2061 error (_("Target does not support this type of "
2062 "hardware watchpoint."));
2063 else if (target_resources_ok < 0 && !sw_mode)
2064 error (_("There are not enough available hardware "
2065 "resources for this watchpoint."));
2066
2067 /* Downgrade to software watchpoint. */
2068 b->base.type = bp_watchpoint;
2069 }
2070 else
2071 {
2072 /* If this was a software watchpoint, we've just
2073 found we have enough resources to turn it to a
2074 hardware watchpoint. Otherwise, this is a
2075 nop. */
2076 b->base.type = type;
2077 }
2078 }
2079 else if (!b->base.ops->works_in_software_mode (&b->base))
2080 {
2081 if (!can_use_hw_watchpoints)
2082 error (_("Can't set read/access watchpoint when "
2083 "hardware watchpoints are disabled."));
2084 else
2085 error (_("Expression cannot be implemented with "
2086 "read/access watchpoint."));
2087 }
2088 else
2089 b->base.type = bp_watchpoint;
2090
2091 loc_type = (b->base.type == bp_watchpoint? bp_loc_other
2092 : bp_loc_hardware_watchpoint);
2093 for (bl = b->base.loc; bl; bl = bl->next)
2094 bl->loc_type = loc_type;
2095 }
2096
2097 for (v = val_chain; v; v = next)
2098 {
2099 next = value_next (v);
2100 if (v != b->val)
2101 value_free (v);
2102 }
2103
2104 /* If a software watchpoint is not watching any memory, then the
2105 above left it without any location set up. But,
2106 bpstat_stop_status requires a location to be able to report
2107 stops, so make sure there's at least a dummy one. */
2108 if (b->base.type == bp_watchpoint && b->base.loc == NULL)
2109 {
2110 struct breakpoint *base = &b->base;
2111 base->loc = allocate_bp_location (base);
2112 base->loc->pspace = frame_pspace;
2113 base->loc->address = -1;
2114 base->loc->length = -1;
2115 base->loc->watchpoint_type = -1;
2116 }
2117 }
2118 else if (!within_current_scope)
2119 {
2120 printf_filtered (_("\
2121 Watchpoint %d deleted because the program has left the block\n\
2122 in which its expression is valid.\n"),
2123 b->base.number);
2124 watchpoint_del_at_next_stop (b);
2125 }
2126
2127 /* Restore the selected frame. */
2128 if (frame_saved)
2129 select_frame (frame_find_by_id (saved_frame_id));
2130 }
2131
2132
2133 /* Returns 1 iff breakpoint location should be
2134 inserted in the inferior. We don't differentiate the type of BL's owner
2135 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2136 breakpoint_ops is not defined, because in insert_bp_location,
2137 tracepoint's insert_location will not be called. */
2138 static int
2139 should_be_inserted (struct bp_location *bl)
2140 {
2141 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
2142 return 0;
2143
2144 if (bl->owner->disposition == disp_del_at_next_stop)
2145 return 0;
2146
2147 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
2148 return 0;
2149
2150 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2151 return 0;
2152
2153 /* This is set for example, when we're attached to the parent of a
2154 vfork, and have detached from the child. The child is running
2155 free, and we expect it to do an exec or exit, at which point the
2156 OS makes the parent schedulable again (and the target reports
2157 that the vfork is done). Until the child is done with the shared
2158 memory region, do not insert breakpoints in the parent, otherwise
2159 the child could still trip on the parent's breakpoints. Since
2160 the parent is blocked anyway, it won't miss any breakpoint. */
2161 if (bl->pspace->breakpoints_not_allowed)
2162 return 0;
2163
2164 /* Don't insert a breakpoint if we're trying to step past its
2165 location. */
2166 if ((bl->loc_type == bp_loc_software_breakpoint
2167 || bl->loc_type == bp_loc_hardware_breakpoint)
2168 && stepping_past_instruction_at (bl->pspace->aspace,
2169 bl->address))
2170 {
2171 if (debug_infrun)
2172 {
2173 fprintf_unfiltered (gdb_stdlog,
2174 "infrun: skipping breakpoint: "
2175 "stepping past insn at: %s\n",
2176 paddress (bl->gdbarch, bl->address));
2177 }
2178 return 0;
2179 }
2180
2181 /* Don't insert watchpoints if we're trying to step past the
2182 instruction that triggered one. */
2183 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2184 && stepping_past_nonsteppable_watchpoint ())
2185 {
2186 if (debug_infrun)
2187 {
2188 fprintf_unfiltered (gdb_stdlog,
2189 "infrun: stepping past non-steppable watchpoint. "
2190 "skipping watchpoint at %s:%d\n",
2191 paddress (bl->gdbarch, bl->address),
2192 bl->length);
2193 }
2194 return 0;
2195 }
2196
2197 return 1;
2198 }
2199
2200 /* Same as should_be_inserted but does the check assuming
2201 that the location is not duplicated. */
2202
2203 static int
2204 unduplicated_should_be_inserted (struct bp_location *bl)
2205 {
2206 int result;
2207 const int save_duplicate = bl->duplicate;
2208
2209 bl->duplicate = 0;
2210 result = should_be_inserted (bl);
2211 bl->duplicate = save_duplicate;
2212 return result;
2213 }
2214
2215 /* Parses a conditional described by an expression COND into an
2216 agent expression bytecode suitable for evaluation
2217 by the bytecode interpreter. Return NULL if there was
2218 any error during parsing. */
2219
2220 static struct agent_expr *
2221 parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2222 {
2223 struct agent_expr *aexpr = NULL;
2224 volatile struct gdb_exception ex;
2225
2226 if (!cond)
2227 return NULL;
2228
2229 /* We don't want to stop processing, so catch any errors
2230 that may show up. */
2231 TRY_CATCH (ex, RETURN_MASK_ERROR)
2232 {
2233 aexpr = gen_eval_for_expr (scope, cond);
2234 }
2235
2236 if (ex.reason < 0)
2237 {
2238 /* If we got here, it means the condition could not be parsed to a valid
2239 bytecode expression and thus can't be evaluated on the target's side.
2240 It's no use iterating through the conditions. */
2241 return NULL;
2242 }
2243
2244 /* We have a valid agent expression. */
2245 return aexpr;
2246 }
2247
2248 /* Based on location BL, create a list of breakpoint conditions to be
2249 passed on to the target. If we have duplicated locations with different
2250 conditions, we will add such conditions to the list. The idea is that the
2251 target will evaluate the list of conditions and will only notify GDB when
2252 one of them is true. */
2253
2254 static void
2255 build_target_condition_list (struct bp_location *bl)
2256 {
2257 struct bp_location **locp = NULL, **loc2p;
2258 int null_condition_or_parse_error = 0;
2259 int modified = bl->needs_update;
2260 struct bp_location *loc;
2261
2262 /* Release conditions left over from a previous insert. */
2263 VEC_free (agent_expr_p, bl->target_info.conditions);
2264
2265 /* This is only meaningful if the target is
2266 evaluating conditions and if the user has
2267 opted for condition evaluation on the target's
2268 side. */
2269 if (gdb_evaluates_breakpoint_condition_p ()
2270 || !target_supports_evaluation_of_breakpoint_conditions ())
2271 return;
2272
2273 /* Do a first pass to check for locations with no assigned
2274 conditions or conditions that fail to parse to a valid agent expression
2275 bytecode. If any of these happen, then it's no use to send conditions
2276 to the target since this location will always trigger and generate a
2277 response back to GDB. */
2278 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2279 {
2280 loc = (*loc2p);
2281 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2282 {
2283 if (modified)
2284 {
2285 struct agent_expr *aexpr;
2286
2287 /* Re-parse the conditions since something changed. In that
2288 case we already freed the condition bytecodes (see
2289 force_breakpoint_reinsertion). We just
2290 need to parse the condition to bytecodes again. */
2291 aexpr = parse_cond_to_aexpr (bl->address, loc->cond);
2292 loc->cond_bytecode = aexpr;
2293
2294 /* Check if we managed to parse the conditional expression
2295 correctly. If not, we will not send this condition
2296 to the target. */
2297 if (aexpr)
2298 continue;
2299 }
2300
2301 /* If we have a NULL bytecode expression, it means something
2302 went wrong or we have a null condition expression. */
2303 if (!loc->cond_bytecode)
2304 {
2305 null_condition_or_parse_error = 1;
2306 break;
2307 }
2308 }
2309 }
2310
2311 /* If any of these happened, it means we will have to evaluate the conditions
2312 for the location's address on gdb's side. It is no use keeping bytecodes
2313 for all the other duplicate locations, thus we free all of them here.
2314
2315 This is so we have a finer control over which locations' conditions are
2316 being evaluated by GDB or the remote stub. */
2317 if (null_condition_or_parse_error)
2318 {
2319 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2320 {
2321 loc = (*loc2p);
2322 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2323 {
2324 /* Only go as far as the first NULL bytecode is
2325 located. */
2326 if (!loc->cond_bytecode)
2327 return;
2328
2329 free_agent_expr (loc->cond_bytecode);
2330 loc->cond_bytecode = NULL;
2331 }
2332 }
2333 }
2334
2335 /* No NULL conditions or failed bytecode generation. Build a condition list
2336 for this location's address. */
2337 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2338 {
2339 loc = (*loc2p);
2340 if (loc->cond
2341 && is_breakpoint (loc->owner)
2342 && loc->pspace->num == bl->pspace->num
2343 && loc->owner->enable_state == bp_enabled
2344 && loc->enabled)
2345 /* Add the condition to the vector. This will be used later to send the
2346 conditions to the target. */
2347 VEC_safe_push (agent_expr_p, bl->target_info.conditions,
2348 loc->cond_bytecode);
2349 }
2350
2351 return;
2352 }
2353
2354 /* Parses a command described by string CMD into an agent expression
2355 bytecode suitable for evaluation by the bytecode interpreter.
2356 Return NULL if there was any error during parsing. */
2357
2358 static struct agent_expr *
2359 parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2360 {
2361 struct cleanup *old_cleanups = 0;
2362 struct expression *expr, **argvec;
2363 struct agent_expr *aexpr = NULL;
2364 volatile struct gdb_exception ex;
2365 const char *cmdrest;
2366 const char *format_start, *format_end;
2367 struct format_piece *fpieces;
2368 int nargs;
2369 struct gdbarch *gdbarch = get_current_arch ();
2370
2371 if (!cmd)
2372 return NULL;
2373
2374 cmdrest = cmd;
2375
2376 if (*cmdrest == ',')
2377 ++cmdrest;
2378 cmdrest = skip_spaces_const (cmdrest);
2379
2380 if (*cmdrest++ != '"')
2381 error (_("No format string following the location"));
2382
2383 format_start = cmdrest;
2384
2385 fpieces = parse_format_string (&cmdrest);
2386
2387 old_cleanups = make_cleanup (free_format_pieces_cleanup, &fpieces);
2388
2389 format_end = cmdrest;
2390
2391 if (*cmdrest++ != '"')
2392 error (_("Bad format string, non-terminated '\"'."));
2393
2394 cmdrest = skip_spaces_const (cmdrest);
2395
2396 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2397 error (_("Invalid argument syntax"));
2398
2399 if (*cmdrest == ',')
2400 cmdrest++;
2401 cmdrest = skip_spaces_const (cmdrest);
2402
2403 /* For each argument, make an expression. */
2404
2405 argvec = (struct expression **) alloca (strlen (cmd)
2406 * sizeof (struct expression *));
2407
2408 nargs = 0;
2409 while (*cmdrest != '\0')
2410 {
2411 const char *cmd1;
2412
2413 cmd1 = cmdrest;
2414 expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2415 argvec[nargs++] = expr;
2416 cmdrest = cmd1;
2417 if (*cmdrest == ',')
2418 ++cmdrest;
2419 }
2420
2421 /* We don't want to stop processing, so catch any errors
2422 that may show up. */
2423 TRY_CATCH (ex, RETURN_MASK_ERROR)
2424 {
2425 aexpr = gen_printf (scope, gdbarch, 0, 0,
2426 format_start, format_end - format_start,
2427 fpieces, nargs, argvec);
2428 }
2429
2430 do_cleanups (old_cleanups);
2431
2432 if (ex.reason < 0)
2433 {
2434 /* If we got here, it means the command could not be parsed to a valid
2435 bytecode expression and thus can't be evaluated on the target's side.
2436 It's no use iterating through the other commands. */
2437 return NULL;
2438 }
2439
2440 /* We have a valid agent expression, return it. */
2441 return aexpr;
2442 }
2443
2444 /* Based on location BL, create a list of breakpoint commands to be
2445 passed on to the target. If we have duplicated locations with
2446 different commands, we will add any such to the list. */
2447
2448 static void
2449 build_target_command_list (struct bp_location *bl)
2450 {
2451 struct bp_location **locp = NULL, **loc2p;
2452 int null_command_or_parse_error = 0;
2453 int modified = bl->needs_update;
2454 struct bp_location *loc;
2455
2456 /* Release commands left over from a previous insert. */
2457 VEC_free (agent_expr_p, bl->target_info.tcommands);
2458
2459 if (!target_can_run_breakpoint_commands ())
2460 return;
2461
2462 /* For now, limit to agent-style dprintf breakpoints. */
2463 if (dprintf_style != dprintf_style_agent)
2464 return;
2465
2466 /* For now, if we have any duplicate location that isn't a dprintf,
2467 don't install the target-side commands, as that would make the
2468 breakpoint not be reported to the core, and we'd lose
2469 control. */
2470 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2471 {
2472 loc = (*loc2p);
2473 if (is_breakpoint (loc->owner)
2474 && loc->pspace->num == bl->pspace->num
2475 && loc->owner->type != bp_dprintf)
2476 return;
2477 }
2478
2479 /* Do a first pass to check for locations with no assigned
2480 conditions or conditions that fail to parse to a valid agent expression
2481 bytecode. If any of these happen, then it's no use to send conditions
2482 to the target since this location will always trigger and generate a
2483 response back to GDB. */
2484 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2485 {
2486 loc = (*loc2p);
2487 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2488 {
2489 if (modified)
2490 {
2491 struct agent_expr *aexpr;
2492
2493 /* Re-parse the commands since something changed. In that
2494 case we already freed the command bytecodes (see
2495 force_breakpoint_reinsertion). We just
2496 need to parse the command to bytecodes again. */
2497 aexpr = parse_cmd_to_aexpr (bl->address,
2498 loc->owner->extra_string);
2499 loc->cmd_bytecode = aexpr;
2500
2501 if (!aexpr)
2502 continue;
2503 }
2504
2505 /* If we have a NULL bytecode expression, it means something
2506 went wrong or we have a null command expression. */
2507 if (!loc->cmd_bytecode)
2508 {
2509 null_command_or_parse_error = 1;
2510 break;
2511 }
2512 }
2513 }
2514
2515 /* If anything failed, then we're not doing target-side commands,
2516 and so clean up. */
2517 if (null_command_or_parse_error)
2518 {
2519 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2520 {
2521 loc = (*loc2p);
2522 if (is_breakpoint (loc->owner)
2523 && loc->pspace->num == bl->pspace->num)
2524 {
2525 /* Only go as far as the first NULL bytecode is
2526 located. */
2527 if (loc->cmd_bytecode == NULL)
2528 return;
2529
2530 free_agent_expr (loc->cmd_bytecode);
2531 loc->cmd_bytecode = NULL;
2532 }
2533 }
2534 }
2535
2536 /* No NULL commands or failed bytecode generation. Build a command list
2537 for this location's address. */
2538 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2539 {
2540 loc = (*loc2p);
2541 if (loc->owner->extra_string
2542 && is_breakpoint (loc->owner)
2543 && loc->pspace->num == bl->pspace->num
2544 && loc->owner->enable_state == bp_enabled
2545 && loc->enabled)
2546 /* Add the command to the vector. This will be used later
2547 to send the commands to the target. */
2548 VEC_safe_push (agent_expr_p, bl->target_info.tcommands,
2549 loc->cmd_bytecode);
2550 }
2551
2552 bl->target_info.persist = 0;
2553 /* Maybe flag this location as persistent. */
2554 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2555 bl->target_info.persist = 1;
2556 }
2557
2558 /* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2559 location. Any error messages are printed to TMP_ERROR_STREAM; and
2560 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
2561 Returns 0 for success, 1 if the bp_location type is not supported or
2562 -1 for failure.
2563
2564 NOTE drow/2003-09-09: This routine could be broken down to an
2565 object-style method for each breakpoint or catchpoint type. */
2566 static int
2567 insert_bp_location (struct bp_location *bl,
2568 struct ui_file *tmp_error_stream,
2569 int *disabled_breaks,
2570 int *hw_breakpoint_error,
2571 int *hw_bp_error_explained_already)
2572 {
2573 enum errors bp_err = GDB_NO_ERROR;
2574 const char *bp_err_message = NULL;
2575 volatile struct gdb_exception e;
2576
2577 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2578 return 0;
2579
2580 /* Note we don't initialize bl->target_info, as that wipes out
2581 the breakpoint location's shadow_contents if the breakpoint
2582 is still inserted at that location. This in turn breaks
2583 target_read_memory which depends on these buffers when
2584 a memory read is requested at the breakpoint location:
2585 Once the target_info has been wiped, we fail to see that
2586 we have a breakpoint inserted at that address and thus
2587 read the breakpoint instead of returning the data saved in
2588 the breakpoint location's shadow contents. */
2589 bl->target_info.reqstd_address = bl->address;
2590 bl->target_info.placed_address_space = bl->pspace->aspace;
2591 bl->target_info.length = bl->length;
2592
2593 /* When working with target-side conditions, we must pass all the conditions
2594 for the same breakpoint address down to the target since GDB will not
2595 insert those locations. With a list of breakpoint conditions, the target
2596 can decide when to stop and notify GDB. */
2597
2598 if (is_breakpoint (bl->owner))
2599 {
2600 build_target_condition_list (bl);
2601 build_target_command_list (bl);
2602 /* Reset the modification marker. */
2603 bl->needs_update = 0;
2604 }
2605
2606 if (bl->loc_type == bp_loc_software_breakpoint
2607 || bl->loc_type == bp_loc_hardware_breakpoint)
2608 {
2609 if (bl->owner->type != bp_hardware_breakpoint)
2610 {
2611 /* If the explicitly specified breakpoint type
2612 is not hardware breakpoint, check the memory map to see
2613 if the breakpoint address is in read only memory or not.
2614
2615 Two important cases are:
2616 - location type is not hardware breakpoint, memory
2617 is readonly. We change the type of the location to
2618 hardware breakpoint.
2619 - location type is hardware breakpoint, memory is
2620 read-write. This means we've previously made the
2621 location hardware one, but then the memory map changed,
2622 so we undo.
2623
2624 When breakpoints are removed, remove_breakpoints will use
2625 location types we've just set here, the only possible
2626 problem is that memory map has changed during running
2627 program, but it's not going to work anyway with current
2628 gdb. */
2629 struct mem_region *mr
2630 = lookup_mem_region (bl->target_info.reqstd_address);
2631
2632 if (mr)
2633 {
2634 if (automatic_hardware_breakpoints)
2635 {
2636 enum bp_loc_type new_type;
2637
2638 if (mr->attrib.mode != MEM_RW)
2639 new_type = bp_loc_hardware_breakpoint;
2640 else
2641 new_type = bp_loc_software_breakpoint;
2642
2643 if (new_type != bl->loc_type)
2644 {
2645 static int said = 0;
2646
2647 bl->loc_type = new_type;
2648 if (!said)
2649 {
2650 fprintf_filtered (gdb_stdout,
2651 _("Note: automatically using "
2652 "hardware breakpoints for "
2653 "read-only addresses.\n"));
2654 said = 1;
2655 }
2656 }
2657 }
2658 else if (bl->loc_type == bp_loc_software_breakpoint
2659 && mr->attrib.mode != MEM_RW)
2660 {
2661 fprintf_unfiltered (tmp_error_stream,
2662 _("Cannot insert breakpoint %d.\n"
2663 "Cannot set software breakpoint "
2664 "at read-only address %s\n"),
2665 bl->owner->number,
2666 paddress (bl->gdbarch, bl->address));
2667 return 1;
2668 }
2669 }
2670 }
2671
2672 /* First check to see if we have to handle an overlay. */
2673 if (overlay_debugging == ovly_off
2674 || bl->section == NULL
2675 || !(section_is_overlay (bl->section)))
2676 {
2677 /* No overlay handling: just set the breakpoint. */
2678 TRY_CATCH (e, RETURN_MASK_ALL)
2679 {
2680 int val;
2681
2682 val = bl->owner->ops->insert_location (bl);
2683 if (val)
2684 bp_err = GENERIC_ERROR;
2685 }
2686 if (e.reason < 0)
2687 {
2688 bp_err = e.error;
2689 bp_err_message = e.message;
2690 }
2691 }
2692 else
2693 {
2694 /* This breakpoint is in an overlay section.
2695 Shall we set a breakpoint at the LMA? */
2696 if (!overlay_events_enabled)
2697 {
2698 /* Yes -- overlay event support is not active,
2699 so we must try to set a breakpoint at the LMA.
2700 This will not work for a hardware breakpoint. */
2701 if (bl->loc_type == bp_loc_hardware_breakpoint)
2702 warning (_("hardware breakpoint %d not supported in overlay!"),
2703 bl->owner->number);
2704 else
2705 {
2706 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2707 bl->section);
2708 /* Set a software (trap) breakpoint at the LMA. */
2709 bl->overlay_target_info = bl->target_info;
2710 bl->overlay_target_info.reqstd_address = addr;
2711
2712 /* No overlay handling: just set the breakpoint. */
2713 TRY_CATCH (e, RETURN_MASK_ALL)
2714 {
2715 int val;
2716
2717 val = target_insert_breakpoint (bl->gdbarch,
2718 &bl->overlay_target_info);
2719 if (val)
2720 bp_err = GENERIC_ERROR;
2721 }
2722 if (e.reason < 0)
2723 {
2724 bp_err = e.error;
2725 bp_err_message = e.message;
2726 }
2727
2728 if (bp_err != GDB_NO_ERROR)
2729 fprintf_unfiltered (tmp_error_stream,
2730 "Overlay breakpoint %d "
2731 "failed: in ROM?\n",
2732 bl->owner->number);
2733 }
2734 }
2735 /* Shall we set a breakpoint at the VMA? */
2736 if (section_is_mapped (bl->section))
2737 {
2738 /* Yes. This overlay section is mapped into memory. */
2739 TRY_CATCH (e, RETURN_MASK_ALL)
2740 {
2741 int val;
2742
2743 val = bl->owner->ops->insert_location (bl);
2744 if (val)
2745 bp_err = GENERIC_ERROR;
2746 }
2747 if (e.reason < 0)
2748 {
2749 bp_err = e.error;
2750 bp_err_message = e.message;
2751 }
2752 }
2753 else
2754 {
2755 /* No. This breakpoint will not be inserted.
2756 No error, but do not mark the bp as 'inserted'. */
2757 return 0;
2758 }
2759 }
2760
2761 if (bp_err != GDB_NO_ERROR)
2762 {
2763 /* Can't set the breakpoint. */
2764
2765 /* In some cases, we might not be able to insert a
2766 breakpoint in a shared library that has already been
2767 removed, but we have not yet processed the shlib unload
2768 event. Unfortunately, some targets that implement
2769 breakpoint insertion themselves can't tell why the
2770 breakpoint insertion failed (e.g., the remote target
2771 doesn't define error codes), so we must treat generic
2772 errors as memory errors. */
2773 if ((bp_err == GENERIC_ERROR || bp_err == MEMORY_ERROR)
2774 && bl->loc_type == bp_loc_software_breakpoint
2775 && (solib_name_from_address (bl->pspace, bl->address)
2776 || shared_objfile_contains_address_p (bl->pspace,
2777 bl->address)))
2778 {
2779 /* See also: disable_breakpoints_in_shlibs. */
2780 bl->shlib_disabled = 1;
2781 observer_notify_breakpoint_modified (bl->owner);
2782 if (!*disabled_breaks)
2783 {
2784 fprintf_unfiltered (tmp_error_stream,
2785 "Cannot insert breakpoint %d.\n",
2786 bl->owner->number);
2787 fprintf_unfiltered (tmp_error_stream,
2788 "Temporarily disabling shared "
2789 "library breakpoints:\n");
2790 }
2791 *disabled_breaks = 1;
2792 fprintf_unfiltered (tmp_error_stream,
2793 "breakpoint #%d\n", bl->owner->number);
2794 return 0;
2795 }
2796 else
2797 {
2798 if (bl->loc_type == bp_loc_hardware_breakpoint)
2799 {
2800 *hw_breakpoint_error = 1;
2801 *hw_bp_error_explained_already = bp_err_message != NULL;
2802 fprintf_unfiltered (tmp_error_stream,
2803 "Cannot insert hardware breakpoint %d%s",
2804 bl->owner->number, bp_err_message ? ":" : ".\n");
2805 if (bp_err_message != NULL)
2806 fprintf_unfiltered (tmp_error_stream, "%s.\n", bp_err_message);
2807 }
2808 else
2809 {
2810 if (bp_err_message == NULL)
2811 {
2812 char *message
2813 = memory_error_message (TARGET_XFER_E_IO,
2814 bl->gdbarch, bl->address);
2815 struct cleanup *old_chain = make_cleanup (xfree, message);
2816
2817 fprintf_unfiltered (tmp_error_stream,
2818 "Cannot insert breakpoint %d.\n"
2819 "%s\n",
2820 bl->owner->number, message);
2821 do_cleanups (old_chain);
2822 }
2823 else
2824 {
2825 fprintf_unfiltered (tmp_error_stream,
2826 "Cannot insert breakpoint %d: %s\n",
2827 bl->owner->number,
2828 bp_err_message);
2829 }
2830 }
2831 return 1;
2832
2833 }
2834 }
2835 else
2836 bl->inserted = 1;
2837
2838 return 0;
2839 }
2840
2841 else if (bl->loc_type == bp_loc_hardware_watchpoint
2842 /* NOTE drow/2003-09-08: This state only exists for removing
2843 watchpoints. It's not clear that it's necessary... */
2844 && bl->owner->disposition != disp_del_at_next_stop)
2845 {
2846 int val;
2847
2848 gdb_assert (bl->owner->ops != NULL
2849 && bl->owner->ops->insert_location != NULL);
2850
2851 val = bl->owner->ops->insert_location (bl);
2852
2853 /* If trying to set a read-watchpoint, and it turns out it's not
2854 supported, try emulating one with an access watchpoint. */
2855 if (val == 1 && bl->watchpoint_type == hw_read)
2856 {
2857 struct bp_location *loc, **loc_temp;
2858
2859 /* But don't try to insert it, if there's already another
2860 hw_access location that would be considered a duplicate
2861 of this one. */
2862 ALL_BP_LOCATIONS (loc, loc_temp)
2863 if (loc != bl
2864 && loc->watchpoint_type == hw_access
2865 && watchpoint_locations_match (bl, loc))
2866 {
2867 bl->duplicate = 1;
2868 bl->inserted = 1;
2869 bl->target_info = loc->target_info;
2870 bl->watchpoint_type = hw_access;
2871 val = 0;
2872 break;
2873 }
2874
2875 if (val == 1)
2876 {
2877 bl->watchpoint_type = hw_access;
2878 val = bl->owner->ops->insert_location (bl);
2879
2880 if (val)
2881 /* Back to the original value. */
2882 bl->watchpoint_type = hw_read;
2883 }
2884 }
2885
2886 bl->inserted = (val == 0);
2887 }
2888
2889 else if (bl->owner->type == bp_catchpoint)
2890 {
2891 int val;
2892
2893 gdb_assert (bl->owner->ops != NULL
2894 && bl->owner->ops->insert_location != NULL);
2895
2896 val = bl->owner->ops->insert_location (bl);
2897 if (val)
2898 {
2899 bl->owner->enable_state = bp_disabled;
2900
2901 if (val == 1)
2902 warning (_("\
2903 Error inserting catchpoint %d: Your system does not support this type\n\
2904 of catchpoint."), bl->owner->number);
2905 else
2906 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2907 }
2908
2909 bl->inserted = (val == 0);
2910
2911 /* We've already printed an error message if there was a problem
2912 inserting this catchpoint, and we've disabled the catchpoint,
2913 so just return success. */
2914 return 0;
2915 }
2916
2917 return 0;
2918 }
2919
2920 /* This function is called when program space PSPACE is about to be
2921 deleted. It takes care of updating breakpoints to not reference
2922 PSPACE anymore. */
2923
2924 void
2925 breakpoint_program_space_exit (struct program_space *pspace)
2926 {
2927 struct breakpoint *b, *b_temp;
2928 struct bp_location *loc, **loc_temp;
2929
2930 /* Remove any breakpoint that was set through this program space. */
2931 ALL_BREAKPOINTS_SAFE (b, b_temp)
2932 {
2933 if (b->pspace == pspace)
2934 delete_breakpoint (b);
2935 }
2936
2937 /* Breakpoints set through other program spaces could have locations
2938 bound to PSPACE as well. Remove those. */
2939 ALL_BP_LOCATIONS (loc, loc_temp)
2940 {
2941 struct bp_location *tmp;
2942
2943 if (loc->pspace == pspace)
2944 {
2945 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
2946 if (loc->owner->loc == loc)
2947 loc->owner->loc = loc->next;
2948 else
2949 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2950 if (tmp->next == loc)
2951 {
2952 tmp->next = loc->next;
2953 break;
2954 }
2955 }
2956 }
2957
2958 /* Now update the global location list to permanently delete the
2959 removed locations above. */
2960 update_global_location_list (UGLL_DONT_INSERT);
2961 }
2962
2963 /* Make sure all breakpoints are inserted in inferior.
2964 Throws exception on any error.
2965 A breakpoint that is already inserted won't be inserted
2966 again, so calling this function twice is safe. */
2967 void
2968 insert_breakpoints (void)
2969 {
2970 struct breakpoint *bpt;
2971
2972 ALL_BREAKPOINTS (bpt)
2973 if (is_hardware_watchpoint (bpt))
2974 {
2975 struct watchpoint *w = (struct watchpoint *) bpt;
2976
2977 update_watchpoint (w, 0 /* don't reparse. */);
2978 }
2979
2980 /* Updating watchpoints creates new locations, so update the global
2981 location list. Explicitly tell ugll to insert locations and
2982 ignore breakpoints_always_inserted_mode. */
2983 update_global_location_list (UGLL_INSERT);
2984 }
2985
2986 /* Invoke CALLBACK for each of bp_location. */
2987
2988 void
2989 iterate_over_bp_locations (walk_bp_location_callback callback)
2990 {
2991 struct bp_location *loc, **loc_tmp;
2992
2993 ALL_BP_LOCATIONS (loc, loc_tmp)
2994 {
2995 callback (loc, NULL);
2996 }
2997 }
2998
2999 /* This is used when we need to synch breakpoint conditions between GDB and the
3000 target. It is the case with deleting and disabling of breakpoints when using
3001 always-inserted mode. */
3002
3003 static void
3004 update_inserted_breakpoint_locations (void)
3005 {
3006 struct bp_location *bl, **blp_tmp;
3007 int error_flag = 0;
3008 int val = 0;
3009 int disabled_breaks = 0;
3010 int hw_breakpoint_error = 0;
3011 int hw_bp_details_reported = 0;
3012
3013 struct ui_file *tmp_error_stream = mem_fileopen ();
3014 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
3015
3016 /* Explicitly mark the warning -- this will only be printed if
3017 there was an error. */
3018 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
3019
3020 save_current_space_and_thread ();
3021
3022 ALL_BP_LOCATIONS (bl, blp_tmp)
3023 {
3024 /* We only want to update software breakpoints and hardware
3025 breakpoints. */
3026 if (!is_breakpoint (bl->owner))
3027 continue;
3028
3029 /* We only want to update locations that are already inserted
3030 and need updating. This is to avoid unwanted insertion during
3031 deletion of breakpoints. */
3032 if (!bl->inserted || (bl->inserted && !bl->needs_update))
3033 continue;
3034
3035 switch_to_program_space_and_thread (bl->pspace);
3036
3037 /* For targets that support global breakpoints, there's no need
3038 to select an inferior to insert breakpoint to. In fact, even
3039 if we aren't attached to any process yet, we should still
3040 insert breakpoints. */
3041 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3042 && ptid_equal (inferior_ptid, null_ptid))
3043 continue;
3044
3045 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
3046 &hw_breakpoint_error, &hw_bp_details_reported);
3047 if (val)
3048 error_flag = val;
3049 }
3050
3051 if (error_flag)
3052 {
3053 target_terminal_ours_for_output ();
3054 error_stream (tmp_error_stream);
3055 }
3056
3057 do_cleanups (cleanups);
3058 }
3059
3060 /* Used when starting or continuing the program. */
3061
3062 static void
3063 insert_breakpoint_locations (void)
3064 {
3065 struct breakpoint *bpt;
3066 struct bp_location *bl, **blp_tmp;
3067 int error_flag = 0;
3068 int val = 0;
3069 int disabled_breaks = 0;
3070 int hw_breakpoint_error = 0;
3071 int hw_bp_error_explained_already = 0;
3072
3073 struct ui_file *tmp_error_stream = mem_fileopen ();
3074 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
3075
3076 /* Explicitly mark the warning -- this will only be printed if
3077 there was an error. */
3078 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
3079
3080 save_current_space_and_thread ();
3081
3082 ALL_BP_LOCATIONS (bl, blp_tmp)
3083 {
3084 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
3085 continue;
3086
3087 /* There is no point inserting thread-specific breakpoints if
3088 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3089 has BL->OWNER always non-NULL. */
3090 if (bl->owner->thread != -1
3091 && !valid_thread_id (bl->owner->thread))
3092 continue;
3093
3094 switch_to_program_space_and_thread (bl->pspace);
3095
3096 /* For targets that support global breakpoints, there's no need
3097 to select an inferior to insert breakpoint to. In fact, even
3098 if we aren't attached to any process yet, we should still
3099 insert breakpoints. */
3100 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3101 && ptid_equal (inferior_ptid, null_ptid))
3102 continue;
3103
3104 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
3105 &hw_breakpoint_error, &hw_bp_error_explained_already);
3106 if (val)
3107 error_flag = val;
3108 }
3109
3110 /* If we failed to insert all locations of a watchpoint, remove
3111 them, as half-inserted watchpoint is of limited use. */
3112 ALL_BREAKPOINTS (bpt)
3113 {
3114 int some_failed = 0;
3115 struct bp_location *loc;
3116
3117 if (!is_hardware_watchpoint (bpt))
3118 continue;
3119
3120 if (!breakpoint_enabled (bpt))
3121 continue;
3122
3123 if (bpt->disposition == disp_del_at_next_stop)
3124 continue;
3125
3126 for (loc = bpt->loc; loc; loc = loc->next)
3127 if (!loc->inserted && should_be_inserted (loc))
3128 {
3129 some_failed = 1;
3130 break;
3131 }
3132 if (some_failed)
3133 {
3134 for (loc = bpt->loc; loc; loc = loc->next)
3135 if (loc->inserted)
3136 remove_breakpoint (loc, mark_uninserted);
3137
3138 hw_breakpoint_error = 1;
3139 fprintf_unfiltered (tmp_error_stream,
3140 "Could not insert hardware watchpoint %d.\n",
3141 bpt->number);
3142 error_flag = -1;
3143 }
3144 }
3145
3146 if (error_flag)
3147 {
3148 /* If a hardware breakpoint or watchpoint was inserted, add a
3149 message about possibly exhausted resources. */
3150 if (hw_breakpoint_error && !hw_bp_error_explained_already)
3151 {
3152 fprintf_unfiltered (tmp_error_stream,
3153 "Could not insert hardware breakpoints:\n\
3154 You may have requested too many hardware breakpoints/watchpoints.\n");
3155 }
3156 target_terminal_ours_for_output ();
3157 error_stream (tmp_error_stream);
3158 }
3159
3160 do_cleanups (cleanups);
3161 }
3162
3163 /* Used when the program stops.
3164 Returns zero if successful, or non-zero if there was a problem
3165 removing a breakpoint location. */
3166
3167 int
3168 remove_breakpoints (void)
3169 {
3170 struct bp_location *bl, **blp_tmp;
3171 int val = 0;
3172
3173 ALL_BP_LOCATIONS (bl, blp_tmp)
3174 {
3175 if (bl->inserted && !is_tracepoint (bl->owner))
3176 val |= remove_breakpoint (bl, mark_uninserted);
3177 }
3178 return val;
3179 }
3180
3181 /* When a thread exits, remove breakpoints that are related to
3182 that thread. */
3183
3184 static void
3185 remove_threaded_breakpoints (struct thread_info *tp, int silent)
3186 {
3187 struct breakpoint *b, *b_tmp;
3188
3189 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3190 {
3191 if (b->thread == tp->num && user_breakpoint_p (b))
3192 {
3193 b->disposition = disp_del_at_next_stop;
3194
3195 printf_filtered (_("\
3196 Thread-specific breakpoint %d deleted - thread %d no longer in the thread list.\n"),
3197 b->number, tp->num);
3198
3199 /* Hide it from the user. */
3200 b->number = 0;
3201 }
3202 }
3203 }
3204
3205 /* Remove breakpoints of process PID. */
3206
3207 int
3208 remove_breakpoints_pid (int pid)
3209 {
3210 struct bp_location *bl, **blp_tmp;
3211 int val;
3212 struct inferior *inf = find_inferior_pid (pid);
3213
3214 ALL_BP_LOCATIONS (bl, blp_tmp)
3215 {
3216 if (bl->pspace != inf->pspace)
3217 continue;
3218
3219 if (bl->inserted && !bl->target_info.persist)
3220 {
3221 val = remove_breakpoint (bl, mark_uninserted);
3222 if (val != 0)
3223 return val;
3224 }
3225 }
3226 return 0;
3227 }
3228
3229 int
3230 reattach_breakpoints (int pid)
3231 {
3232 struct cleanup *old_chain;
3233 struct bp_location *bl, **blp_tmp;
3234 int val;
3235 struct ui_file *tmp_error_stream;
3236 int dummy1 = 0, dummy2 = 0, dummy3 = 0;
3237 struct inferior *inf;
3238 struct thread_info *tp;
3239
3240 tp = any_live_thread_of_process (pid);
3241 if (tp == NULL)
3242 return 1;
3243
3244 inf = find_inferior_pid (pid);
3245 old_chain = save_inferior_ptid ();
3246
3247 inferior_ptid = tp->ptid;
3248
3249 tmp_error_stream = mem_fileopen ();
3250 make_cleanup_ui_file_delete (tmp_error_stream);
3251
3252 ALL_BP_LOCATIONS (bl, blp_tmp)
3253 {
3254 if (bl->pspace != inf->pspace)
3255 continue;
3256
3257 if (bl->inserted)
3258 {
3259 bl->inserted = 0;
3260 val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2, &dummy3);
3261 if (val != 0)
3262 {
3263 do_cleanups (old_chain);
3264 return val;
3265 }
3266 }
3267 }
3268 do_cleanups (old_chain);
3269 return 0;
3270 }
3271
3272 static int internal_breakpoint_number = -1;
3273
3274 /* Set the breakpoint number of B, depending on the value of INTERNAL.
3275 If INTERNAL is non-zero, the breakpoint number will be populated
3276 from internal_breakpoint_number and that variable decremented.
3277 Otherwise the breakpoint number will be populated from
3278 breakpoint_count and that value incremented. Internal breakpoints
3279 do not set the internal var bpnum. */
3280 static void
3281 set_breakpoint_number (int internal, struct breakpoint *b)
3282 {
3283 if (internal)
3284 b->number = internal_breakpoint_number--;
3285 else
3286 {
3287 set_breakpoint_count (breakpoint_count + 1);
3288 b->number = breakpoint_count;
3289 }
3290 }
3291
3292 static struct breakpoint *
3293 create_internal_breakpoint (struct gdbarch *gdbarch,
3294 CORE_ADDR address, enum bptype type,
3295 const struct breakpoint_ops *ops)
3296 {
3297 struct symtab_and_line sal;
3298 struct breakpoint *b;
3299
3300 init_sal (&sal); /* Initialize to zeroes. */
3301
3302 sal.pc = address;
3303 sal.section = find_pc_overlay (sal.pc);
3304 sal.pspace = current_program_space;
3305
3306 b = set_raw_breakpoint (gdbarch, sal, type, ops);
3307 b->number = internal_breakpoint_number--;
3308 b->disposition = disp_donttouch;
3309
3310 return b;
3311 }
3312
3313 static const char *const longjmp_names[] =
3314 {
3315 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3316 };
3317 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3318
3319 /* Per-objfile data private to breakpoint.c. */
3320 struct breakpoint_objfile_data
3321 {
3322 /* Minimal symbol for "_ovly_debug_event" (if any). */
3323 struct bound_minimal_symbol overlay_msym;
3324
3325 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3326 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
3327
3328 /* True if we have looked for longjmp probes. */
3329 int longjmp_searched;
3330
3331 /* SystemTap probe points for longjmp (if any). */
3332 VEC (probe_p) *longjmp_probes;
3333
3334 /* Minimal symbol for "std::terminate()" (if any). */
3335 struct bound_minimal_symbol terminate_msym;
3336
3337 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3338 struct bound_minimal_symbol exception_msym;
3339
3340 /* True if we have looked for exception probes. */
3341 int exception_searched;
3342
3343 /* SystemTap probe points for unwinding (if any). */
3344 VEC (probe_p) *exception_probes;
3345 };
3346
3347 static const struct objfile_data *breakpoint_objfile_key;
3348
3349 /* Minimal symbol not found sentinel. */
3350 static struct minimal_symbol msym_not_found;
3351
3352 /* Returns TRUE if MSYM point to the "not found" sentinel. */
3353
3354 static int
3355 msym_not_found_p (const struct minimal_symbol *msym)
3356 {
3357 return msym == &msym_not_found;
3358 }
3359
3360 /* Return per-objfile data needed by breakpoint.c.
3361 Allocate the data if necessary. */
3362
3363 static struct breakpoint_objfile_data *
3364 get_breakpoint_objfile_data (struct objfile *objfile)
3365 {
3366 struct breakpoint_objfile_data *bp_objfile_data;
3367
3368 bp_objfile_data = objfile_data (objfile, breakpoint_objfile_key);
3369 if (bp_objfile_data == NULL)
3370 {
3371 bp_objfile_data = obstack_alloc (&objfile->objfile_obstack,
3372 sizeof (*bp_objfile_data));
3373
3374 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
3375 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3376 }
3377 return bp_objfile_data;
3378 }
3379
3380 static void
3381 free_breakpoint_probes (struct objfile *obj, void *data)
3382 {
3383 struct breakpoint_objfile_data *bp_objfile_data = data;
3384
3385 VEC_free (probe_p, bp_objfile_data->longjmp_probes);
3386 VEC_free (probe_p, bp_objfile_data->exception_probes);
3387 }
3388
3389 static void
3390 create_overlay_event_breakpoint (void)
3391 {
3392 struct objfile *objfile;
3393 const char *const func_name = "_ovly_debug_event";
3394
3395 ALL_OBJFILES (objfile)
3396 {
3397 struct breakpoint *b;
3398 struct breakpoint_objfile_data *bp_objfile_data;
3399 CORE_ADDR addr;
3400
3401 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3402
3403 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
3404 continue;
3405
3406 if (bp_objfile_data->overlay_msym.minsym == NULL)
3407 {
3408 struct bound_minimal_symbol m;
3409
3410 m = lookup_minimal_symbol_text (func_name, objfile);
3411 if (m.minsym == NULL)
3412 {
3413 /* Avoid future lookups in this objfile. */
3414 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
3415 continue;
3416 }
3417 bp_objfile_data->overlay_msym = m;
3418 }
3419
3420 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
3421 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3422 bp_overlay_event,
3423 &internal_breakpoint_ops);
3424 b->addr_string = xstrdup (func_name);
3425
3426 if (overlay_debugging == ovly_auto)
3427 {
3428 b->enable_state = bp_enabled;
3429 overlay_events_enabled = 1;
3430 }
3431 else
3432 {
3433 b->enable_state = bp_disabled;
3434 overlay_events_enabled = 0;
3435 }
3436 }
3437 update_global_location_list (UGLL_MAY_INSERT);
3438 }
3439
3440 static void
3441 create_longjmp_master_breakpoint (void)
3442 {
3443 struct program_space *pspace;
3444 struct cleanup *old_chain;
3445
3446 old_chain = save_current_program_space ();
3447
3448 ALL_PSPACES (pspace)
3449 {
3450 struct objfile *objfile;
3451
3452 set_current_program_space (pspace);
3453
3454 ALL_OBJFILES (objfile)
3455 {
3456 int i;
3457 struct gdbarch *gdbarch;
3458 struct breakpoint_objfile_data *bp_objfile_data;
3459
3460 gdbarch = get_objfile_arch (objfile);
3461
3462 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3463
3464 if (!bp_objfile_data->longjmp_searched)
3465 {
3466 VEC (probe_p) *ret;
3467
3468 ret = find_probes_in_objfile (objfile, "libc", "longjmp");
3469 if (ret != NULL)
3470 {
3471 /* We are only interested in checking one element. */
3472 struct probe *p = VEC_index (probe_p, ret, 0);
3473
3474 if (!can_evaluate_probe_arguments (p))
3475 {
3476 /* We cannot use the probe interface here, because it does
3477 not know how to evaluate arguments. */
3478 VEC_free (probe_p, ret);
3479 ret = NULL;
3480 }
3481 }
3482 bp_objfile_data->longjmp_probes = ret;
3483 bp_objfile_data->longjmp_searched = 1;
3484 }
3485
3486 if (bp_objfile_data->longjmp_probes != NULL)
3487 {
3488 int i;
3489 struct probe *probe;
3490 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3491
3492 for (i = 0;
3493 VEC_iterate (probe_p,
3494 bp_objfile_data->longjmp_probes,
3495 i, probe);
3496 ++i)
3497 {
3498 struct breakpoint *b;
3499
3500 b = create_internal_breakpoint (gdbarch,
3501 get_probe_address (probe,
3502 objfile),
3503 bp_longjmp_master,
3504 &internal_breakpoint_ops);
3505 b->addr_string = xstrdup ("-probe-stap libc:longjmp");
3506 b->enable_state = bp_disabled;
3507 }
3508
3509 continue;
3510 }
3511
3512 if (!gdbarch_get_longjmp_target_p (gdbarch))
3513 continue;
3514
3515 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
3516 {
3517 struct breakpoint *b;
3518 const char *func_name;
3519 CORE_ADDR addr;
3520
3521 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3522 continue;
3523
3524 func_name = longjmp_names[i];
3525 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3526 {
3527 struct bound_minimal_symbol m;
3528
3529 m = lookup_minimal_symbol_text (func_name, objfile);
3530 if (m.minsym == NULL)
3531 {
3532 /* Prevent future lookups in this objfile. */
3533 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
3534 continue;
3535 }
3536 bp_objfile_data->longjmp_msym[i] = m;
3537 }
3538
3539 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
3540 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3541 &internal_breakpoint_ops);
3542 b->addr_string = xstrdup (func_name);
3543 b->enable_state = bp_disabled;
3544 }
3545 }
3546 }
3547 update_global_location_list (UGLL_MAY_INSERT);
3548
3549 do_cleanups (old_chain);
3550 }
3551
3552 /* Create a master std::terminate breakpoint. */
3553 static void
3554 create_std_terminate_master_breakpoint (void)
3555 {
3556 struct program_space *pspace;
3557 struct cleanup *old_chain;
3558 const char *const func_name = "std::terminate()";
3559
3560 old_chain = save_current_program_space ();
3561
3562 ALL_PSPACES (pspace)
3563 {
3564 struct objfile *objfile;
3565 CORE_ADDR addr;
3566
3567 set_current_program_space (pspace);
3568
3569 ALL_OBJFILES (objfile)
3570 {
3571 struct breakpoint *b;
3572 struct breakpoint_objfile_data *bp_objfile_data;
3573
3574 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3575
3576 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3577 continue;
3578
3579 if (bp_objfile_data->terminate_msym.minsym == NULL)
3580 {
3581 struct bound_minimal_symbol m;
3582
3583 m = lookup_minimal_symbol (func_name, NULL, objfile);
3584 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3585 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
3586 {
3587 /* Prevent future lookups in this objfile. */
3588 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3589 continue;
3590 }
3591 bp_objfile_data->terminate_msym = m;
3592 }
3593
3594 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3595 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3596 bp_std_terminate_master,
3597 &internal_breakpoint_ops);
3598 b->addr_string = xstrdup (func_name);
3599 b->enable_state = bp_disabled;
3600 }
3601 }
3602
3603 update_global_location_list (UGLL_MAY_INSERT);
3604
3605 do_cleanups (old_chain);
3606 }
3607
3608 /* Install a master breakpoint on the unwinder's debug hook. */
3609
3610 static void
3611 create_exception_master_breakpoint (void)
3612 {
3613 struct objfile *objfile;
3614 const char *const func_name = "_Unwind_DebugHook";
3615
3616 ALL_OBJFILES (objfile)
3617 {
3618 struct breakpoint *b;
3619 struct gdbarch *gdbarch;
3620 struct breakpoint_objfile_data *bp_objfile_data;
3621 CORE_ADDR addr;
3622
3623 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3624
3625 /* We prefer the SystemTap probe point if it exists. */
3626 if (!bp_objfile_data->exception_searched)
3627 {
3628 VEC (probe_p) *ret;
3629
3630 ret = find_probes_in_objfile (objfile, "libgcc", "unwind");
3631
3632 if (ret != NULL)
3633 {
3634 /* We are only interested in checking one element. */
3635 struct probe *p = VEC_index (probe_p, ret, 0);
3636
3637 if (!can_evaluate_probe_arguments (p))
3638 {
3639 /* We cannot use the probe interface here, because it does
3640 not know how to evaluate arguments. */
3641 VEC_free (probe_p, ret);
3642 ret = NULL;
3643 }
3644 }
3645 bp_objfile_data->exception_probes = ret;
3646 bp_objfile_data->exception_searched = 1;
3647 }
3648
3649 if (bp_objfile_data->exception_probes != NULL)
3650 {
3651 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3652 int i;
3653 struct probe *probe;
3654
3655 for (i = 0;
3656 VEC_iterate (probe_p,
3657 bp_objfile_data->exception_probes,
3658 i, probe);
3659 ++i)
3660 {
3661 struct breakpoint *b;
3662
3663 b = create_internal_breakpoint (gdbarch,
3664 get_probe_address (probe,
3665 objfile),
3666 bp_exception_master,
3667 &internal_breakpoint_ops);
3668 b->addr_string = xstrdup ("-probe-stap libgcc:unwind");
3669 b->enable_state = bp_disabled;
3670 }
3671
3672 continue;
3673 }
3674
3675 /* Otherwise, try the hook function. */
3676
3677 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3678 continue;
3679
3680 gdbarch = get_objfile_arch (objfile);
3681
3682 if (bp_objfile_data->exception_msym.minsym == NULL)
3683 {
3684 struct bound_minimal_symbol debug_hook;
3685
3686 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3687 if (debug_hook.minsym == NULL)
3688 {
3689 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3690 continue;
3691 }
3692
3693 bp_objfile_data->exception_msym = debug_hook;
3694 }
3695
3696 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
3697 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3698 &current_target);
3699 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3700 &internal_breakpoint_ops);
3701 b->addr_string = xstrdup (func_name);
3702 b->enable_state = bp_disabled;
3703 }
3704
3705 update_global_location_list (UGLL_MAY_INSERT);
3706 }
3707
3708 void
3709 update_breakpoints_after_exec (void)
3710 {
3711 struct breakpoint *b, *b_tmp;
3712 struct bp_location *bploc, **bplocp_tmp;
3713
3714 /* We're about to delete breakpoints from GDB's lists. If the
3715 INSERTED flag is true, GDB will try to lift the breakpoints by
3716 writing the breakpoints' "shadow contents" back into memory. The
3717 "shadow contents" are NOT valid after an exec, so GDB should not
3718 do that. Instead, the target is responsible from marking
3719 breakpoints out as soon as it detects an exec. We don't do that
3720 here instead, because there may be other attempts to delete
3721 breakpoints after detecting an exec and before reaching here. */
3722 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
3723 if (bploc->pspace == current_program_space)
3724 gdb_assert (!bploc->inserted);
3725
3726 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3727 {
3728 if (b->pspace != current_program_space)
3729 continue;
3730
3731 /* Solib breakpoints must be explicitly reset after an exec(). */
3732 if (b->type == bp_shlib_event)
3733 {
3734 delete_breakpoint (b);
3735 continue;
3736 }
3737
3738 /* JIT breakpoints must be explicitly reset after an exec(). */
3739 if (b->type == bp_jit_event)
3740 {
3741 delete_breakpoint (b);
3742 continue;
3743 }
3744
3745 /* Thread event breakpoints must be set anew after an exec(),
3746 as must overlay event and longjmp master breakpoints. */
3747 if (b->type == bp_thread_event || b->type == bp_overlay_event
3748 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3749 || b->type == bp_exception_master)
3750 {
3751 delete_breakpoint (b);
3752 continue;
3753 }
3754
3755 /* Step-resume breakpoints are meaningless after an exec(). */
3756 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3757 {
3758 delete_breakpoint (b);
3759 continue;
3760 }
3761
3762 /* Just like single-step breakpoints. */
3763 if (b->type == bp_single_step)
3764 {
3765 delete_breakpoint (b);
3766 continue;
3767 }
3768
3769 /* Longjmp and longjmp-resume breakpoints are also meaningless
3770 after an exec. */
3771 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3772 || b->type == bp_longjmp_call_dummy
3773 || b->type == bp_exception || b->type == bp_exception_resume)
3774 {
3775 delete_breakpoint (b);
3776 continue;
3777 }
3778
3779 if (b->type == bp_catchpoint)
3780 {
3781 /* For now, none of the bp_catchpoint breakpoints need to
3782 do anything at this point. In the future, if some of
3783 the catchpoints need to something, we will need to add
3784 a new method, and call this method from here. */
3785 continue;
3786 }
3787
3788 /* bp_finish is a special case. The only way we ought to be able
3789 to see one of these when an exec() has happened, is if the user
3790 caught a vfork, and then said "finish". Ordinarily a finish just
3791 carries them to the call-site of the current callee, by setting
3792 a temporary bp there and resuming. But in this case, the finish
3793 will carry them entirely through the vfork & exec.
3794
3795 We don't want to allow a bp_finish to remain inserted now. But
3796 we can't safely delete it, 'cause finish_command has a handle to
3797 the bp on a bpstat, and will later want to delete it. There's a
3798 chance (and I've seen it happen) that if we delete the bp_finish
3799 here, that its storage will get reused by the time finish_command
3800 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3801 We really must allow finish_command to delete a bp_finish.
3802
3803 In the absence of a general solution for the "how do we know
3804 it's safe to delete something others may have handles to?"
3805 problem, what we'll do here is just uninsert the bp_finish, and
3806 let finish_command delete it.
3807
3808 (We know the bp_finish is "doomed" in the sense that it's
3809 momentary, and will be deleted as soon as finish_command sees
3810 the inferior stopped. So it doesn't matter that the bp's
3811 address is probably bogus in the new a.out, unlike e.g., the
3812 solib breakpoints.) */
3813
3814 if (b->type == bp_finish)
3815 {
3816 continue;
3817 }
3818
3819 /* Without a symbolic address, we have little hope of the
3820 pre-exec() address meaning the same thing in the post-exec()
3821 a.out. */
3822 if (b->addr_string == NULL)
3823 {
3824 delete_breakpoint (b);
3825 continue;
3826 }
3827 }
3828 }
3829
3830 int
3831 detach_breakpoints (ptid_t ptid)
3832 {
3833 struct bp_location *bl, **blp_tmp;
3834 int val = 0;
3835 struct cleanup *old_chain = save_inferior_ptid ();
3836 struct inferior *inf = current_inferior ();
3837
3838 if (ptid_get_pid (ptid) == ptid_get_pid (inferior_ptid))
3839 error (_("Cannot detach breakpoints of inferior_ptid"));
3840
3841 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
3842 inferior_ptid = ptid;
3843 ALL_BP_LOCATIONS (bl, blp_tmp)
3844 {
3845 if (bl->pspace != inf->pspace)
3846 continue;
3847
3848 /* This function must physically remove breakpoints locations
3849 from the specified ptid, without modifying the breakpoint
3850 package's state. Locations of type bp_loc_other are only
3851 maintained at GDB side. So, there is no need to remove
3852 these bp_loc_other locations. Moreover, removing these
3853 would modify the breakpoint package's state. */
3854 if (bl->loc_type == bp_loc_other)
3855 continue;
3856
3857 if (bl->inserted)
3858 val |= remove_breakpoint_1 (bl, mark_inserted);
3859 }
3860
3861 do_cleanups (old_chain);
3862 return val;
3863 }
3864
3865 /* Remove the breakpoint location BL from the current address space.
3866 Note that this is used to detach breakpoints from a child fork.
3867 When we get here, the child isn't in the inferior list, and neither
3868 do we have objects to represent its address space --- we should
3869 *not* look at bl->pspace->aspace here. */
3870
3871 static int
3872 remove_breakpoint_1 (struct bp_location *bl, insertion_state_t is)
3873 {
3874 int val;
3875
3876 /* BL is never in moribund_locations by our callers. */
3877 gdb_assert (bl->owner != NULL);
3878
3879 if (bl->permanent)
3880 /* Permanent breakpoints cannot be inserted or removed. */
3881 return 0;
3882
3883 /* The type of none suggests that owner is actually deleted.
3884 This should not ever happen. */
3885 gdb_assert (bl->owner->type != bp_none);
3886
3887 if (bl->loc_type == bp_loc_software_breakpoint
3888 || bl->loc_type == bp_loc_hardware_breakpoint)
3889 {
3890 /* "Normal" instruction breakpoint: either the standard
3891 trap-instruction bp (bp_breakpoint), or a
3892 bp_hardware_breakpoint. */
3893
3894 /* First check to see if we have to handle an overlay. */
3895 if (overlay_debugging == ovly_off
3896 || bl->section == NULL
3897 || !(section_is_overlay (bl->section)))
3898 {
3899 /* No overlay handling: just remove the breakpoint. */
3900
3901 /* If we're trying to uninsert a memory breakpoint that we
3902 know is set in a dynamic object that is marked
3903 shlib_disabled, then either the dynamic object was
3904 removed with "remove-symbol-file" or with
3905 "nosharedlibrary". In the former case, we don't know
3906 whether another dynamic object might have loaded over the
3907 breakpoint's address -- the user might well let us know
3908 about it next with add-symbol-file (the whole point of
3909 add-symbol-file is letting the user manually maintain a
3910 list of dynamically loaded objects). If we have the
3911 breakpoint's shadow memory, that is, this is a software
3912 breakpoint managed by GDB, check whether the breakpoint
3913 is still inserted in memory, to avoid overwriting wrong
3914 code with stale saved shadow contents. Note that HW
3915 breakpoints don't have shadow memory, as they're
3916 implemented using a mechanism that is not dependent on
3917 being able to modify the target's memory, and as such
3918 they should always be removed. */
3919 if (bl->shlib_disabled
3920 && bl->target_info.shadow_len != 0
3921 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3922 val = 0;
3923 else
3924 val = bl->owner->ops->remove_location (bl);
3925 }
3926 else
3927 {
3928 /* This breakpoint is in an overlay section.
3929 Did we set a breakpoint at the LMA? */
3930 if (!overlay_events_enabled)
3931 {
3932 /* Yes -- overlay event support is not active, so we
3933 should have set a breakpoint at the LMA. Remove it.
3934 */
3935 /* Ignore any failures: if the LMA is in ROM, we will
3936 have already warned when we failed to insert it. */
3937 if (bl->loc_type == bp_loc_hardware_breakpoint)
3938 target_remove_hw_breakpoint (bl->gdbarch,
3939 &bl->overlay_target_info);
3940 else
3941 target_remove_breakpoint (bl->gdbarch,
3942 &bl->overlay_target_info);
3943 }
3944 /* Did we set a breakpoint at the VMA?
3945 If so, we will have marked the breakpoint 'inserted'. */
3946 if (bl->inserted)
3947 {
3948 /* Yes -- remove it. Previously we did not bother to
3949 remove the breakpoint if the section had been
3950 unmapped, but let's not rely on that being safe. We
3951 don't know what the overlay manager might do. */
3952
3953 /* However, we should remove *software* breakpoints only
3954 if the section is still mapped, or else we overwrite
3955 wrong code with the saved shadow contents. */
3956 if (bl->loc_type == bp_loc_hardware_breakpoint
3957 || section_is_mapped (bl->section))
3958 val = bl->owner->ops->remove_location (bl);
3959 else
3960 val = 0;
3961 }
3962 else
3963 {
3964 /* No -- not inserted, so no need to remove. No error. */
3965 val = 0;
3966 }
3967 }
3968
3969 /* In some cases, we might not be able to remove a breakpoint in
3970 a shared library that has already been removed, but we have
3971 not yet processed the shlib unload event. Similarly for an
3972 unloaded add-symbol-file object - the user might not yet have
3973 had the chance to remove-symbol-file it. shlib_disabled will
3974 be set if the library/object has already been removed, but
3975 the breakpoint hasn't been uninserted yet, e.g., after
3976 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3977 always-inserted mode. */
3978 if (val
3979 && (bl->loc_type == bp_loc_software_breakpoint
3980 && (bl->shlib_disabled
3981 || solib_name_from_address (bl->pspace, bl->address)
3982 || shared_objfile_contains_address_p (bl->pspace,
3983 bl->address))))
3984 val = 0;
3985
3986 if (val)
3987 return val;
3988 bl->inserted = (is == mark_inserted);
3989 }
3990 else if (bl->loc_type == bp_loc_hardware_watchpoint)
3991 {
3992 gdb_assert (bl->owner->ops != NULL
3993 && bl->owner->ops->remove_location != NULL);
3994
3995 bl->inserted = (is == mark_inserted);
3996 bl->owner->ops->remove_location (bl);
3997
3998 /* Failure to remove any of the hardware watchpoints comes here. */
3999 if ((is == mark_uninserted) && (bl->inserted))
4000 warning (_("Could not remove hardware watchpoint %d."),
4001 bl->owner->number);
4002 }
4003 else if (bl->owner->type == bp_catchpoint
4004 && breakpoint_enabled (bl->owner)
4005 && !bl->duplicate)
4006 {
4007 gdb_assert (bl->owner->ops != NULL
4008 && bl->owner->ops->remove_location != NULL);
4009
4010 val = bl->owner->ops->remove_location (bl);
4011 if (val)
4012 return val;
4013
4014 bl->inserted = (is == mark_inserted);
4015 }
4016
4017 return 0;
4018 }
4019
4020 static int
4021 remove_breakpoint (struct bp_location *bl, insertion_state_t is)
4022 {
4023 int ret;
4024 struct cleanup *old_chain;
4025
4026 /* BL is never in moribund_locations by our callers. */
4027 gdb_assert (bl->owner != NULL);
4028
4029 if (bl->permanent)
4030 /* Permanent breakpoints cannot be inserted or removed. */
4031 return 0;
4032
4033 /* The type of none suggests that owner is actually deleted.
4034 This should not ever happen. */
4035 gdb_assert (bl->owner->type != bp_none);
4036
4037 old_chain = save_current_space_and_thread ();
4038
4039 switch_to_program_space_and_thread (bl->pspace);
4040
4041 ret = remove_breakpoint_1 (bl, is);
4042
4043 do_cleanups (old_chain);
4044 return ret;
4045 }
4046
4047 /* Clear the "inserted" flag in all breakpoints. */
4048
4049 void
4050 mark_breakpoints_out (void)
4051 {
4052 struct bp_location *bl, **blp_tmp;
4053
4054 ALL_BP_LOCATIONS (bl, blp_tmp)
4055 if (bl->pspace == current_program_space
4056 && !bl->permanent)
4057 bl->inserted = 0;
4058 }
4059
4060 /* Clear the "inserted" flag in all breakpoints and delete any
4061 breakpoints which should go away between runs of the program.
4062
4063 Plus other such housekeeping that has to be done for breakpoints
4064 between runs.
4065
4066 Note: this function gets called at the end of a run (by
4067 generic_mourn_inferior) and when a run begins (by
4068 init_wait_for_inferior). */
4069
4070
4071
4072 void
4073 breakpoint_init_inferior (enum inf_context context)
4074 {
4075 struct breakpoint *b, *b_tmp;
4076 struct bp_location *bl, **blp_tmp;
4077 int ix;
4078 struct program_space *pspace = current_program_space;
4079
4080 /* If breakpoint locations are shared across processes, then there's
4081 nothing to do. */
4082 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
4083 return;
4084
4085 mark_breakpoints_out ();
4086
4087 ALL_BREAKPOINTS_SAFE (b, b_tmp)
4088 {
4089 if (b->loc && b->loc->pspace != pspace)
4090 continue;
4091
4092 switch (b->type)
4093 {
4094 case bp_call_dummy:
4095 case bp_longjmp_call_dummy:
4096
4097 /* If the call dummy breakpoint is at the entry point it will
4098 cause problems when the inferior is rerun, so we better get
4099 rid of it. */
4100
4101 case bp_watchpoint_scope:
4102
4103 /* Also get rid of scope breakpoints. */
4104
4105 case bp_shlib_event:
4106
4107 /* Also remove solib event breakpoints. Their addresses may
4108 have changed since the last time we ran the program.
4109 Actually we may now be debugging against different target;
4110 and so the solib backend that installed this breakpoint may
4111 not be used in by the target. E.g.,
4112
4113 (gdb) file prog-linux
4114 (gdb) run # native linux target
4115 ...
4116 (gdb) kill
4117 (gdb) file prog-win.exe
4118 (gdb) tar rem :9999 # remote Windows gdbserver.
4119 */
4120
4121 case bp_step_resume:
4122
4123 /* Also remove step-resume breakpoints. */
4124
4125 case bp_single_step:
4126
4127 /* Also remove single-step breakpoints. */
4128
4129 delete_breakpoint (b);
4130 break;
4131
4132 case bp_watchpoint:
4133 case bp_hardware_watchpoint:
4134 case bp_read_watchpoint:
4135 case bp_access_watchpoint:
4136 {
4137 struct watchpoint *w = (struct watchpoint *) b;
4138
4139 /* Likewise for watchpoints on local expressions. */
4140 if (w->exp_valid_block != NULL)
4141 delete_breakpoint (b);
4142 else if (context == inf_starting)
4143 {
4144 /* Reset val field to force reread of starting value in
4145 insert_breakpoints. */
4146 if (w->val)
4147 value_free (w->val);
4148 w->val = NULL;
4149 w->val_valid = 0;
4150 }
4151 }
4152 break;
4153 default:
4154 break;
4155 }
4156 }
4157
4158 /* Get rid of the moribund locations. */
4159 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
4160 decref_bp_location (&bl);
4161 VEC_free (bp_location_p, moribund_locations);
4162 }
4163
4164 /* These functions concern about actual breakpoints inserted in the
4165 target --- to e.g. check if we need to do decr_pc adjustment or if
4166 we need to hop over the bkpt --- so we check for address space
4167 match, not program space. */
4168
4169 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4170 exists at PC. It returns ordinary_breakpoint_here if it's an
4171 ordinary breakpoint, or permanent_breakpoint_here if it's a
4172 permanent breakpoint.
4173 - When continuing from a location with an ordinary breakpoint, we
4174 actually single step once before calling insert_breakpoints.
4175 - When continuing from a location with a permanent breakpoint, we
4176 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4177 the target, to advance the PC past the breakpoint. */
4178
4179 enum breakpoint_here
4180 breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
4181 {
4182 struct bp_location *bl, **blp_tmp;
4183 int any_breakpoint_here = 0;
4184
4185 ALL_BP_LOCATIONS (bl, blp_tmp)
4186 {
4187 if (bl->loc_type != bp_loc_software_breakpoint
4188 && bl->loc_type != bp_loc_hardware_breakpoint)
4189 continue;
4190
4191 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
4192 if ((breakpoint_enabled (bl->owner)
4193 || bl->permanent)
4194 && breakpoint_location_address_match (bl, aspace, pc))
4195 {
4196 if (overlay_debugging
4197 && section_is_overlay (bl->section)
4198 && !section_is_mapped (bl->section))
4199 continue; /* unmapped overlay -- can't be a match */
4200 else if (bl->permanent)
4201 return permanent_breakpoint_here;
4202 else
4203 any_breakpoint_here = 1;
4204 }
4205 }
4206
4207 return any_breakpoint_here ? ordinary_breakpoint_here : 0;
4208 }
4209
4210 /* Return true if there's a moribund breakpoint at PC. */
4211
4212 int
4213 moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
4214 {
4215 struct bp_location *loc;
4216 int ix;
4217
4218 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
4219 if (breakpoint_location_address_match (loc, aspace, pc))
4220 return 1;
4221
4222 return 0;
4223 }
4224
4225 /* Returns non-zero iff BL is inserted at PC, in address space
4226 ASPACE. */
4227
4228 static int
4229 bp_location_inserted_here_p (struct bp_location *bl,
4230 struct address_space *aspace, CORE_ADDR pc)
4231 {
4232 if (bl->inserted
4233 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4234 aspace, pc))
4235 {
4236 if (overlay_debugging
4237 && section_is_overlay (bl->section)
4238 && !section_is_mapped (bl->section))
4239 return 0; /* unmapped overlay -- can't be a match */
4240 else
4241 return 1;
4242 }
4243 return 0;
4244 }
4245
4246 /* Returns non-zero iff there's a breakpoint inserted at PC. */
4247
4248 int
4249 breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
4250 {
4251 struct bp_location **blp, **blp_tmp = NULL;
4252 struct bp_location *bl;
4253
4254 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4255 {
4256 struct bp_location *bl = *blp;
4257
4258 if (bl->loc_type != bp_loc_software_breakpoint
4259 && bl->loc_type != bp_loc_hardware_breakpoint)
4260 continue;
4261
4262 if (bp_location_inserted_here_p (bl, aspace, pc))
4263 return 1;
4264 }
4265 return 0;
4266 }
4267
4268 /* This function returns non-zero iff there is a software breakpoint
4269 inserted at PC. */
4270
4271 int
4272 software_breakpoint_inserted_here_p (struct address_space *aspace,
4273 CORE_ADDR pc)
4274 {
4275 struct bp_location **blp, **blp_tmp = NULL;
4276 struct bp_location *bl;
4277
4278 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4279 {
4280 struct bp_location *bl = *blp;
4281
4282 if (bl->loc_type != bp_loc_software_breakpoint)
4283 continue;
4284
4285 if (bp_location_inserted_here_p (bl, aspace, pc))
4286 return 1;
4287 }
4288
4289 return 0;
4290 }
4291
4292 /* See breakpoint.h. */
4293
4294 int
4295 hardware_breakpoint_inserted_here_p (struct address_space *aspace,
4296 CORE_ADDR pc)
4297 {
4298 struct bp_location **blp, **blp_tmp = NULL;
4299 struct bp_location *bl;
4300
4301 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4302 {
4303 struct bp_location *bl = *blp;
4304
4305 if (bl->loc_type != bp_loc_hardware_breakpoint)
4306 continue;
4307
4308 if (bp_location_inserted_here_p (bl, aspace, pc))
4309 return 1;
4310 }
4311
4312 return 0;
4313 }
4314
4315 int
4316 hardware_watchpoint_inserted_in_range (struct address_space *aspace,
4317 CORE_ADDR addr, ULONGEST len)
4318 {
4319 struct breakpoint *bpt;
4320
4321 ALL_BREAKPOINTS (bpt)
4322 {
4323 struct bp_location *loc;
4324
4325 if (bpt->type != bp_hardware_watchpoint
4326 && bpt->type != bp_access_watchpoint)
4327 continue;
4328
4329 if (!breakpoint_enabled (bpt))
4330 continue;
4331
4332 for (loc = bpt->loc; loc; loc = loc->next)
4333 if (loc->pspace->aspace == aspace && loc->inserted)
4334 {
4335 CORE_ADDR l, h;
4336
4337 /* Check for intersection. */
4338 l = max (loc->address, addr);
4339 h = min (loc->address + loc->length, addr + len);
4340 if (l < h)
4341 return 1;
4342 }
4343 }
4344 return 0;
4345 }
4346 \f
4347
4348 /* bpstat stuff. External routines' interfaces are documented
4349 in breakpoint.h. */
4350
4351 int
4352 is_catchpoint (struct breakpoint *ep)
4353 {
4354 return (ep->type == bp_catchpoint);
4355 }
4356
4357 /* Frees any storage that is part of a bpstat. Does not walk the
4358 'next' chain. */
4359
4360 static void
4361 bpstat_free (bpstat bs)
4362 {
4363 if (bs->old_val != NULL)
4364 value_free (bs->old_val);
4365 decref_counted_command_line (&bs->commands);
4366 decref_bp_location (&bs->bp_location_at);
4367 xfree (bs);
4368 }
4369
4370 /* Clear a bpstat so that it says we are not at any breakpoint.
4371 Also free any storage that is part of a bpstat. */
4372
4373 void
4374 bpstat_clear (bpstat *bsp)
4375 {
4376 bpstat p;
4377 bpstat q;
4378
4379 if (bsp == 0)
4380 return;
4381 p = *bsp;
4382 while (p != NULL)
4383 {
4384 q = p->next;
4385 bpstat_free (p);
4386 p = q;
4387 }
4388 *bsp = NULL;
4389 }
4390
4391 /* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4392 is part of the bpstat is copied as well. */
4393
4394 bpstat
4395 bpstat_copy (bpstat bs)
4396 {
4397 bpstat p = NULL;
4398 bpstat tmp;
4399 bpstat retval = NULL;
4400
4401 if (bs == NULL)
4402 return bs;
4403
4404 for (; bs != NULL; bs = bs->next)
4405 {
4406 tmp = (bpstat) xmalloc (sizeof (*tmp));
4407 memcpy (tmp, bs, sizeof (*tmp));
4408 incref_counted_command_line (tmp->commands);
4409 incref_bp_location (tmp->bp_location_at);
4410 if (bs->old_val != NULL)
4411 {
4412 tmp->old_val = value_copy (bs->old_val);
4413 release_value (tmp->old_val);
4414 }
4415
4416 if (p == NULL)
4417 /* This is the first thing in the chain. */
4418 retval = tmp;
4419 else
4420 p->next = tmp;
4421 p = tmp;
4422 }
4423 p->next = NULL;
4424 return retval;
4425 }
4426
4427 /* Find the bpstat associated with this breakpoint. */
4428
4429 bpstat
4430 bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
4431 {
4432 if (bsp == NULL)
4433 return NULL;
4434
4435 for (; bsp != NULL; bsp = bsp->next)
4436 {
4437 if (bsp->breakpoint_at == breakpoint)
4438 return bsp;
4439 }
4440 return NULL;
4441 }
4442
4443 /* See breakpoint.h. */
4444
4445 int
4446 bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
4447 {
4448 for (; bsp != NULL; bsp = bsp->next)
4449 {
4450 if (bsp->breakpoint_at == NULL)
4451 {
4452 /* A moribund location can never explain a signal other than
4453 GDB_SIGNAL_TRAP. */
4454 if (sig == GDB_SIGNAL_TRAP)
4455 return 1;
4456 }
4457 else
4458 {
4459 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4460 sig))
4461 return 1;
4462 }
4463 }
4464
4465 return 0;
4466 }
4467
4468 /* Put in *NUM the breakpoint number of the first breakpoint we are
4469 stopped at. *BSP upon return is a bpstat which points to the
4470 remaining breakpoints stopped at (but which is not guaranteed to be
4471 good for anything but further calls to bpstat_num).
4472
4473 Return 0 if passed a bpstat which does not indicate any breakpoints.
4474 Return -1 if stopped at a breakpoint that has been deleted since
4475 we set it.
4476 Return 1 otherwise. */
4477
4478 int
4479 bpstat_num (bpstat *bsp, int *num)
4480 {
4481 struct breakpoint *b;
4482
4483 if ((*bsp) == NULL)
4484 return 0; /* No more breakpoint values */
4485
4486 /* We assume we'll never have several bpstats that correspond to a
4487 single breakpoint -- otherwise, this function might return the
4488 same number more than once and this will look ugly. */
4489 b = (*bsp)->breakpoint_at;
4490 *bsp = (*bsp)->next;
4491 if (b == NULL)
4492 return -1; /* breakpoint that's been deleted since */
4493
4494 *num = b->number; /* We have its number */
4495 return 1;
4496 }
4497
4498 /* See breakpoint.h. */
4499
4500 void
4501 bpstat_clear_actions (void)
4502 {
4503 struct thread_info *tp;
4504 bpstat bs;
4505
4506 if (ptid_equal (inferior_ptid, null_ptid))
4507 return;
4508
4509 tp = find_thread_ptid (inferior_ptid);
4510 if (tp == NULL)
4511 return;
4512
4513 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
4514 {
4515 decref_counted_command_line (&bs->commands);
4516
4517 if (bs->old_val != NULL)
4518 {
4519 value_free (bs->old_val);
4520 bs->old_val = NULL;
4521 }
4522 }
4523 }
4524
4525 /* Called when a command is about to proceed the inferior. */
4526
4527 static void
4528 breakpoint_about_to_proceed (void)
4529 {
4530 if (!ptid_equal (inferior_ptid, null_ptid))
4531 {
4532 struct thread_info *tp = inferior_thread ();
4533
4534 /* Allow inferior function calls in breakpoint commands to not
4535 interrupt the command list. When the call finishes
4536 successfully, the inferior will be standing at the same
4537 breakpoint as if nothing happened. */
4538 if (tp->control.in_infcall)
4539 return;
4540 }
4541
4542 breakpoint_proceeded = 1;
4543 }
4544
4545 /* Stub for cleaning up our state if we error-out of a breakpoint
4546 command. */
4547 static void
4548 cleanup_executing_breakpoints (void *ignore)
4549 {
4550 executing_breakpoint_commands = 0;
4551 }
4552
4553 /* Return non-zero iff CMD as the first line of a command sequence is `silent'
4554 or its equivalent. */
4555
4556 static int
4557 command_line_is_silent (struct command_line *cmd)
4558 {
4559 return cmd && (strcmp ("silent", cmd->line) == 0
4560 || (xdb_commands && strcmp ("Q", cmd->line) == 0));
4561 }
4562
4563 /* Execute all the commands associated with all the breakpoints at
4564 this location. Any of these commands could cause the process to
4565 proceed beyond this point, etc. We look out for such changes by
4566 checking the global "breakpoint_proceeded" after each command.
4567
4568 Returns true if a breakpoint command resumed the inferior. In that
4569 case, it is the caller's responsibility to recall it again with the
4570 bpstat of the current thread. */
4571
4572 static int
4573 bpstat_do_actions_1 (bpstat *bsp)
4574 {
4575 bpstat bs;
4576 struct cleanup *old_chain;
4577 int again = 0;
4578
4579 /* Avoid endless recursion if a `source' command is contained
4580 in bs->commands. */
4581 if (executing_breakpoint_commands)
4582 return 0;
4583
4584 executing_breakpoint_commands = 1;
4585 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
4586
4587 prevent_dont_repeat ();
4588
4589 /* This pointer will iterate over the list of bpstat's. */
4590 bs = *bsp;
4591
4592 breakpoint_proceeded = 0;
4593 for (; bs != NULL; bs = bs->next)
4594 {
4595 struct counted_command_line *ccmd;
4596 struct command_line *cmd;
4597 struct cleanup *this_cmd_tree_chain;
4598
4599 /* Take ownership of the BSP's command tree, if it has one.
4600
4601 The command tree could legitimately contain commands like
4602 'step' and 'next', which call clear_proceed_status, which
4603 frees stop_bpstat's command tree. To make sure this doesn't
4604 free the tree we're executing out from under us, we need to
4605 take ownership of the tree ourselves. Since a given bpstat's
4606 commands are only executed once, we don't need to copy it; we
4607 can clear the pointer in the bpstat, and make sure we free
4608 the tree when we're done. */
4609 ccmd = bs->commands;
4610 bs->commands = NULL;
4611 this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
4612 cmd = ccmd ? ccmd->commands : NULL;
4613 if (command_line_is_silent (cmd))
4614 {
4615 /* The action has been already done by bpstat_stop_status. */
4616 cmd = cmd->next;
4617 }
4618
4619 while (cmd != NULL)
4620 {
4621 execute_control_command (cmd);
4622
4623 if (breakpoint_proceeded)
4624 break;
4625 else
4626 cmd = cmd->next;
4627 }
4628
4629 /* We can free this command tree now. */
4630 do_cleanups (this_cmd_tree_chain);
4631
4632 if (breakpoint_proceeded)
4633 {
4634 if (interpreter_async && target_can_async_p ())
4635 /* If we are in async mode, then the target might be still
4636 running, not stopped at any breakpoint, so nothing for
4637 us to do here -- just return to the event loop. */
4638 ;
4639 else
4640 /* In sync mode, when execute_control_command returns
4641 we're already standing on the next breakpoint.
4642 Breakpoint commands for that stop were not run, since
4643 execute_command does not run breakpoint commands --
4644 only command_line_handler does, but that one is not
4645 involved in execution of breakpoint commands. So, we
4646 can now execute breakpoint commands. It should be
4647 noted that making execute_command do bpstat actions is
4648 not an option -- in this case we'll have recursive
4649 invocation of bpstat for each breakpoint with a
4650 command, and can easily blow up GDB stack. Instead, we
4651 return true, which will trigger the caller to recall us
4652 with the new stop_bpstat. */
4653 again = 1;
4654 break;
4655 }
4656 }
4657 do_cleanups (old_chain);
4658 return again;
4659 }
4660
4661 void
4662 bpstat_do_actions (void)
4663 {
4664 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4665
4666 /* Do any commands attached to breakpoint we are stopped at. */
4667 while (!ptid_equal (inferior_ptid, null_ptid)
4668 && target_has_execution
4669 && !is_exited (inferior_ptid)
4670 && !is_executing (inferior_ptid))
4671 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4672 and only return when it is stopped at the next breakpoint, we
4673 keep doing breakpoint actions until it returns false to
4674 indicate the inferior was not resumed. */
4675 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
4676 break;
4677
4678 discard_cleanups (cleanup_if_error);
4679 }
4680
4681 /* Print out the (old or new) value associated with a watchpoint. */
4682
4683 static void
4684 watchpoint_value_print (struct value *val, struct ui_file *stream)
4685 {
4686 if (val == NULL)
4687 fprintf_unfiltered (stream, _("<unreadable>"));
4688 else
4689 {
4690 struct value_print_options opts;
4691 get_user_print_options (&opts);
4692 value_print (val, stream, &opts);
4693 }
4694 }
4695
4696 /* Generic routine for printing messages indicating why we
4697 stopped. The behavior of this function depends on the value
4698 'print_it' in the bpstat structure. Under some circumstances we
4699 may decide not to print anything here and delegate the task to
4700 normal_stop(). */
4701
4702 static enum print_stop_action
4703 print_bp_stop_message (bpstat bs)
4704 {
4705 switch (bs->print_it)
4706 {
4707 case print_it_noop:
4708 /* Nothing should be printed for this bpstat entry. */
4709 return PRINT_UNKNOWN;
4710 break;
4711
4712 case print_it_done:
4713 /* We still want to print the frame, but we already printed the
4714 relevant messages. */
4715 return PRINT_SRC_AND_LOC;
4716 break;
4717
4718 case print_it_normal:
4719 {
4720 struct breakpoint *b = bs->breakpoint_at;
4721
4722 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4723 which has since been deleted. */
4724 if (b == NULL)
4725 return PRINT_UNKNOWN;
4726
4727 /* Normal case. Call the breakpoint's print_it method. */
4728 return b->ops->print_it (bs);
4729 }
4730 break;
4731
4732 default:
4733 internal_error (__FILE__, __LINE__,
4734 _("print_bp_stop_message: unrecognized enum value"));
4735 break;
4736 }
4737 }
4738
4739 /* A helper function that prints a shared library stopped event. */
4740
4741 static void
4742 print_solib_event (int is_catchpoint)
4743 {
4744 int any_deleted
4745 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4746 int any_added
4747 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4748
4749 if (!is_catchpoint)
4750 {
4751 if (any_added || any_deleted)
4752 ui_out_text (current_uiout,
4753 _("Stopped due to shared library event:\n"));
4754 else
4755 ui_out_text (current_uiout,
4756 _("Stopped due to shared library event (no "
4757 "libraries added or removed)\n"));
4758 }
4759
4760 if (ui_out_is_mi_like_p (current_uiout))
4761 ui_out_field_string (current_uiout, "reason",
4762 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4763
4764 if (any_deleted)
4765 {
4766 struct cleanup *cleanup;
4767 char *name;
4768 int ix;
4769
4770 ui_out_text (current_uiout, _(" Inferior unloaded "));
4771 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4772 "removed");
4773 for (ix = 0;
4774 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4775 ix, name);
4776 ++ix)
4777 {
4778 if (ix > 0)
4779 ui_out_text (current_uiout, " ");
4780 ui_out_field_string (current_uiout, "library", name);
4781 ui_out_text (current_uiout, "\n");
4782 }
4783
4784 do_cleanups (cleanup);
4785 }
4786
4787 if (any_added)
4788 {
4789 struct so_list *iter;
4790 int ix;
4791 struct cleanup *cleanup;
4792
4793 ui_out_text (current_uiout, _(" Inferior loaded "));
4794 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4795 "added");
4796 for (ix = 0;
4797 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4798 ix, iter);
4799 ++ix)
4800 {
4801 if (ix > 0)
4802 ui_out_text (current_uiout, " ");
4803 ui_out_field_string (current_uiout, "library", iter->so_name);
4804 ui_out_text (current_uiout, "\n");
4805 }
4806
4807 do_cleanups (cleanup);
4808 }
4809 }
4810
4811 /* Print a message indicating what happened. This is called from
4812 normal_stop(). The input to this routine is the head of the bpstat
4813 list - a list of the eventpoints that caused this stop. KIND is
4814 the target_waitkind for the stopping event. This
4815 routine calls the generic print routine for printing a message
4816 about reasons for stopping. This will print (for example) the
4817 "Breakpoint n," part of the output. The return value of this
4818 routine is one of:
4819
4820 PRINT_UNKNOWN: Means we printed nothing.
4821 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4822 code to print the location. An example is
4823 "Breakpoint 1, " which should be followed by
4824 the location.
4825 PRINT_SRC_ONLY: Means we printed something, but there is no need
4826 to also print the location part of the message.
4827 An example is the catch/throw messages, which
4828 don't require a location appended to the end.
4829 PRINT_NOTHING: We have done some printing and we don't need any
4830 further info to be printed. */
4831
4832 enum print_stop_action
4833 bpstat_print (bpstat bs, int kind)
4834 {
4835 int val;
4836
4837 /* Maybe another breakpoint in the chain caused us to stop.
4838 (Currently all watchpoints go on the bpstat whether hit or not.
4839 That probably could (should) be changed, provided care is taken
4840 with respect to bpstat_explains_signal). */
4841 for (; bs; bs = bs->next)
4842 {
4843 val = print_bp_stop_message (bs);
4844 if (val == PRINT_SRC_ONLY
4845 || val == PRINT_SRC_AND_LOC
4846 || val == PRINT_NOTHING)
4847 return val;
4848 }
4849
4850 /* If we had hit a shared library event breakpoint,
4851 print_bp_stop_message would print out this message. If we hit an
4852 OS-level shared library event, do the same thing. */
4853 if (kind == TARGET_WAITKIND_LOADED)
4854 {
4855 print_solib_event (0);
4856 return PRINT_NOTHING;
4857 }
4858
4859 /* We reached the end of the chain, or we got a null BS to start
4860 with and nothing was printed. */
4861 return PRINT_UNKNOWN;
4862 }
4863
4864 /* Evaluate the expression EXP and return 1 if value is zero.
4865 This returns the inverse of the condition because it is called
4866 from catch_errors which returns 0 if an exception happened, and if an
4867 exception happens we want execution to stop.
4868 The argument is a "struct expression *" that has been cast to a
4869 "void *" to make it pass through catch_errors. */
4870
4871 static int
4872 breakpoint_cond_eval (void *exp)
4873 {
4874 struct value *mark = value_mark ();
4875 int i = !value_true (evaluate_expression ((struct expression *) exp));
4876
4877 value_free_to_mark (mark);
4878 return i;
4879 }
4880
4881 /* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
4882
4883 static bpstat
4884 bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
4885 {
4886 bpstat bs;
4887
4888 bs = (bpstat) xmalloc (sizeof (*bs));
4889 bs->next = NULL;
4890 **bs_link_pointer = bs;
4891 *bs_link_pointer = &bs->next;
4892 bs->breakpoint_at = bl->owner;
4893 bs->bp_location_at = bl;
4894 incref_bp_location (bl);
4895 /* If the condition is false, etc., don't do the commands. */
4896 bs->commands = NULL;
4897 bs->old_val = NULL;
4898 bs->print_it = print_it_normal;
4899 return bs;
4900 }
4901 \f
4902 /* The target has stopped with waitstatus WS. Check if any hardware
4903 watchpoints have triggered, according to the target. */
4904
4905 int
4906 watchpoints_triggered (struct target_waitstatus *ws)
4907 {
4908 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
4909 CORE_ADDR addr;
4910 struct breakpoint *b;
4911
4912 if (!stopped_by_watchpoint)
4913 {
4914 /* We were not stopped by a watchpoint. Mark all watchpoints
4915 as not triggered. */
4916 ALL_BREAKPOINTS (b)
4917 if (is_hardware_watchpoint (b))
4918 {
4919 struct watchpoint *w = (struct watchpoint *) b;
4920
4921 w->watchpoint_triggered = watch_triggered_no;
4922 }
4923
4924 return 0;
4925 }
4926
4927 if (!target_stopped_data_address (&current_target, &addr))
4928 {
4929 /* We were stopped by a watchpoint, but we don't know where.
4930 Mark all watchpoints as unknown. */
4931 ALL_BREAKPOINTS (b)
4932 if (is_hardware_watchpoint (b))
4933 {
4934 struct watchpoint *w = (struct watchpoint *) b;
4935
4936 w->watchpoint_triggered = watch_triggered_unknown;
4937 }
4938
4939 return 1;
4940 }
4941
4942 /* The target could report the data address. Mark watchpoints
4943 affected by this data address as triggered, and all others as not
4944 triggered. */
4945
4946 ALL_BREAKPOINTS (b)
4947 if (is_hardware_watchpoint (b))
4948 {
4949 struct watchpoint *w = (struct watchpoint *) b;
4950 struct bp_location *loc;
4951
4952 w->watchpoint_triggered = watch_triggered_no;
4953 for (loc = b->loc; loc; loc = loc->next)
4954 {
4955 if (is_masked_watchpoint (b))
4956 {
4957 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4958 CORE_ADDR start = loc->address & w->hw_wp_mask;
4959
4960 if (newaddr == start)
4961 {
4962 w->watchpoint_triggered = watch_triggered_yes;
4963 break;
4964 }
4965 }
4966 /* Exact match not required. Within range is sufficient. */
4967 else if (target_watchpoint_addr_within_range (&current_target,
4968 addr, loc->address,
4969 loc->length))
4970 {
4971 w->watchpoint_triggered = watch_triggered_yes;
4972 break;
4973 }
4974 }
4975 }
4976
4977 return 1;
4978 }
4979
4980 /* Possible return values for watchpoint_check (this can't be an enum
4981 because of check_errors). */
4982 /* The watchpoint has been deleted. */
4983 #define WP_DELETED 1
4984 /* The value has changed. */
4985 #define WP_VALUE_CHANGED 2
4986 /* The value has not changed. */
4987 #define WP_VALUE_NOT_CHANGED 3
4988 /* Ignore this watchpoint, no matter if the value changed or not. */
4989 #define WP_IGNORE 4
4990
4991 #define BP_TEMPFLAG 1
4992 #define BP_HARDWAREFLAG 2
4993
4994 /* Evaluate watchpoint condition expression and check if its value
4995 changed.
4996
4997 P should be a pointer to struct bpstat, but is defined as a void *
4998 in order for this function to be usable with catch_errors. */
4999
5000 static int
5001 watchpoint_check (void *p)
5002 {
5003 bpstat bs = (bpstat) p;
5004 struct watchpoint *b;
5005 struct frame_info *fr;
5006 int within_current_scope;
5007
5008 /* BS is built from an existing struct breakpoint. */
5009 gdb_assert (bs->breakpoint_at != NULL);
5010 b = (struct watchpoint *) bs->breakpoint_at;
5011
5012 /* If this is a local watchpoint, we only want to check if the
5013 watchpoint frame is in scope if the current thread is the thread
5014 that was used to create the watchpoint. */
5015 if (!watchpoint_in_thread_scope (b))
5016 return WP_IGNORE;
5017
5018 if (b->exp_valid_block == NULL)
5019 within_current_scope = 1;
5020 else
5021 {
5022 struct frame_info *frame = get_current_frame ();
5023 struct gdbarch *frame_arch = get_frame_arch (frame);
5024 CORE_ADDR frame_pc = get_frame_pc (frame);
5025
5026 /* in_function_epilogue_p() returns a non-zero value if we're
5027 still in the function but the stack frame has already been
5028 invalidated. Since we can't rely on the values of local
5029 variables after the stack has been destroyed, we are treating
5030 the watchpoint in that state as `not changed' without further
5031 checking. Don't mark watchpoints as changed if the current
5032 frame is in an epilogue - even if they are in some other
5033 frame, our view of the stack is likely to be wrong and
5034 frame_find_by_id could error out. */
5035 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
5036 return WP_IGNORE;
5037
5038 fr = frame_find_by_id (b->watchpoint_frame);
5039 within_current_scope = (fr != NULL);
5040
5041 /* If we've gotten confused in the unwinder, we might have
5042 returned a frame that can't describe this variable. */
5043 if (within_current_scope)
5044 {
5045 struct symbol *function;
5046
5047 function = get_frame_function (fr);
5048 if (function == NULL
5049 || !contained_in (b->exp_valid_block,
5050 SYMBOL_BLOCK_VALUE (function)))
5051 within_current_scope = 0;
5052 }
5053
5054 if (within_current_scope)
5055 /* If we end up stopping, the current frame will get selected
5056 in normal_stop. So this call to select_frame won't affect
5057 the user. */
5058 select_frame (fr);
5059 }
5060
5061 if (within_current_scope)
5062 {
5063 /* We use value_{,free_to_}mark because it could be a *long*
5064 time before we return to the command level and call
5065 free_all_values. We can't call free_all_values because we
5066 might be in the middle of evaluating a function call. */
5067
5068 int pc = 0;
5069 struct value *mark;
5070 struct value *new_val;
5071
5072 if (is_masked_watchpoint (&b->base))
5073 /* Since we don't know the exact trigger address (from
5074 stopped_data_address), just tell the user we've triggered
5075 a mask watchpoint. */
5076 return WP_VALUE_CHANGED;
5077
5078 mark = value_mark ();
5079 fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL, 0);
5080
5081 if (b->val_bitsize != 0)
5082 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5083
5084 /* We use value_equal_contents instead of value_equal because
5085 the latter coerces an array to a pointer, thus comparing just
5086 the address of the array instead of its contents. This is
5087 not what we want. */
5088 if ((b->val != NULL) != (new_val != NULL)
5089 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
5090 {
5091 if (new_val != NULL)
5092 {
5093 release_value (new_val);
5094 value_free_to_mark (mark);
5095 }
5096 bs->old_val = b->val;
5097 b->val = new_val;
5098 b->val_valid = 1;
5099 return WP_VALUE_CHANGED;
5100 }
5101 else
5102 {
5103 /* Nothing changed. */
5104 value_free_to_mark (mark);
5105 return WP_VALUE_NOT_CHANGED;
5106 }
5107 }
5108 else
5109 {
5110 struct ui_out *uiout = current_uiout;
5111
5112 /* This seems like the only logical thing to do because
5113 if we temporarily ignored the watchpoint, then when
5114 we reenter the block in which it is valid it contains
5115 garbage (in the case of a function, it may have two
5116 garbage values, one before and one after the prologue).
5117 So we can't even detect the first assignment to it and
5118 watch after that (since the garbage may or may not equal
5119 the first value assigned). */
5120 /* We print all the stop information in
5121 breakpoint_ops->print_it, but in this case, by the time we
5122 call breakpoint_ops->print_it this bp will be deleted
5123 already. So we have no choice but print the information
5124 here. */
5125 if (ui_out_is_mi_like_p (uiout))
5126 ui_out_field_string
5127 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
5128 ui_out_text (uiout, "\nWatchpoint ");
5129 ui_out_field_int (uiout, "wpnum", b->base.number);
5130 ui_out_text (uiout,
5131 " deleted because the program has left the block in\n\
5132 which its expression is valid.\n");
5133
5134 /* Make sure the watchpoint's commands aren't executed. */
5135 decref_counted_command_line (&b->base.commands);
5136 watchpoint_del_at_next_stop (b);
5137
5138 return WP_DELETED;
5139 }
5140 }
5141
5142 /* Return true if it looks like target has stopped due to hitting
5143 breakpoint location BL. This function does not check if we should
5144 stop, only if BL explains the stop. */
5145
5146 static int
5147 bpstat_check_location (const struct bp_location *bl,
5148 struct address_space *aspace, CORE_ADDR bp_addr,
5149 const struct target_waitstatus *ws)
5150 {
5151 struct breakpoint *b = bl->owner;
5152
5153 /* BL is from an existing breakpoint. */
5154 gdb_assert (b != NULL);
5155
5156 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
5157 }
5158
5159 /* Determine if the watched values have actually changed, and we
5160 should stop. If not, set BS->stop to 0. */
5161
5162 static void
5163 bpstat_check_watchpoint (bpstat bs)
5164 {
5165 const struct bp_location *bl;
5166 struct watchpoint *b;
5167
5168 /* BS is built for existing struct breakpoint. */
5169 bl = bs->bp_location_at;
5170 gdb_assert (bl != NULL);
5171 b = (struct watchpoint *) bs->breakpoint_at;
5172 gdb_assert (b != NULL);
5173
5174 {
5175 int must_check_value = 0;
5176
5177 if (b->base.type == bp_watchpoint)
5178 /* For a software watchpoint, we must always check the
5179 watched value. */
5180 must_check_value = 1;
5181 else if (b->watchpoint_triggered == watch_triggered_yes)
5182 /* We have a hardware watchpoint (read, write, or access)
5183 and the target earlier reported an address watched by
5184 this watchpoint. */
5185 must_check_value = 1;
5186 else if (b->watchpoint_triggered == watch_triggered_unknown
5187 && b->base.type == bp_hardware_watchpoint)
5188 /* We were stopped by a hardware watchpoint, but the target could
5189 not report the data address. We must check the watchpoint's
5190 value. Access and read watchpoints are out of luck; without
5191 a data address, we can't figure it out. */
5192 must_check_value = 1;
5193
5194 if (must_check_value)
5195 {
5196 char *message
5197 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
5198 b->base.number);
5199 struct cleanup *cleanups = make_cleanup (xfree, message);
5200 int e = catch_errors (watchpoint_check, bs, message,
5201 RETURN_MASK_ALL);
5202 do_cleanups (cleanups);
5203 switch (e)
5204 {
5205 case WP_DELETED:
5206 /* We've already printed what needs to be printed. */
5207 bs->print_it = print_it_done;
5208 /* Stop. */
5209 break;
5210 case WP_IGNORE:
5211 bs->print_it = print_it_noop;
5212 bs->stop = 0;
5213 break;
5214 case WP_VALUE_CHANGED:
5215 if (b->base.type == bp_read_watchpoint)
5216 {
5217 /* There are two cases to consider here:
5218
5219 1. We're watching the triggered memory for reads.
5220 In that case, trust the target, and always report
5221 the watchpoint hit to the user. Even though
5222 reads don't cause value changes, the value may
5223 have changed since the last time it was read, and
5224 since we're not trapping writes, we will not see
5225 those, and as such we should ignore our notion of
5226 old value.
5227
5228 2. We're watching the triggered memory for both
5229 reads and writes. There are two ways this may
5230 happen:
5231
5232 2.1. This is a target that can't break on data
5233 reads only, but can break on accesses (reads or
5234 writes), such as e.g., x86. We detect this case
5235 at the time we try to insert read watchpoints.
5236
5237 2.2. Otherwise, the target supports read
5238 watchpoints, but, the user set an access or write
5239 watchpoint watching the same memory as this read
5240 watchpoint.
5241
5242 If we're watching memory writes as well as reads,
5243 ignore watchpoint hits when we find that the
5244 value hasn't changed, as reads don't cause
5245 changes. This still gives false positives when
5246 the program writes the same value to memory as
5247 what there was already in memory (we will confuse
5248 it for a read), but it's much better than
5249 nothing. */
5250
5251 int other_write_watchpoint = 0;
5252
5253 if (bl->watchpoint_type == hw_read)
5254 {
5255 struct breakpoint *other_b;
5256
5257 ALL_BREAKPOINTS (other_b)
5258 if (other_b->type == bp_hardware_watchpoint
5259 || other_b->type == bp_access_watchpoint)
5260 {
5261 struct watchpoint *other_w =
5262 (struct watchpoint *) other_b;
5263
5264 if (other_w->watchpoint_triggered
5265 == watch_triggered_yes)
5266 {
5267 other_write_watchpoint = 1;
5268 break;
5269 }
5270 }
5271 }
5272
5273 if (other_write_watchpoint
5274 || bl->watchpoint_type == hw_access)
5275 {
5276 /* We're watching the same memory for writes,
5277 and the value changed since the last time we
5278 updated it, so this trap must be for a write.
5279 Ignore it. */
5280 bs->print_it = print_it_noop;
5281 bs->stop = 0;
5282 }
5283 }
5284 break;
5285 case WP_VALUE_NOT_CHANGED:
5286 if (b->base.type == bp_hardware_watchpoint
5287 || b->base.type == bp_watchpoint)
5288 {
5289 /* Don't stop: write watchpoints shouldn't fire if
5290 the value hasn't changed. */
5291 bs->print_it = print_it_noop;
5292 bs->stop = 0;
5293 }
5294 /* Stop. */
5295 break;
5296 default:
5297 /* Can't happen. */
5298 case 0:
5299 /* Error from catch_errors. */
5300 printf_filtered (_("Watchpoint %d deleted.\n"), b->base.number);
5301 watchpoint_del_at_next_stop (b);
5302 /* We've already printed what needs to be printed. */
5303 bs->print_it = print_it_done;
5304 break;
5305 }
5306 }
5307 else /* must_check_value == 0 */
5308 {
5309 /* This is a case where some watchpoint(s) triggered, but
5310 not at the address of this watchpoint, or else no
5311 watchpoint triggered after all. So don't print
5312 anything for this watchpoint. */
5313 bs->print_it = print_it_noop;
5314 bs->stop = 0;
5315 }
5316 }
5317 }
5318
5319 /* For breakpoints that are currently marked as telling gdb to stop,
5320 check conditions (condition proper, frame, thread and ignore count)
5321 of breakpoint referred to by BS. If we should not stop for this
5322 breakpoint, set BS->stop to 0. */
5323
5324 static void
5325 bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5326 {
5327 const struct bp_location *bl;
5328 struct breakpoint *b;
5329 int value_is_zero = 0;
5330 struct expression *cond;
5331
5332 gdb_assert (bs->stop);
5333
5334 /* BS is built for existing struct breakpoint. */
5335 bl = bs->bp_location_at;
5336 gdb_assert (bl != NULL);
5337 b = bs->breakpoint_at;
5338 gdb_assert (b != NULL);
5339
5340 /* Even if the target evaluated the condition on its end and notified GDB, we
5341 need to do so again since GDB does not know if we stopped due to a
5342 breakpoint or a single step breakpoint. */
5343
5344 if (frame_id_p (b->frame_id)
5345 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
5346 {
5347 bs->stop = 0;
5348 return;
5349 }
5350
5351 /* If this is a thread/task-specific breakpoint, don't waste cpu
5352 evaluating the condition if this isn't the specified
5353 thread/task. */
5354 if ((b->thread != -1 && b->thread != pid_to_thread_id (ptid))
5355 || (b->task != 0 && b->task != ada_get_task_number (ptid)))
5356
5357 {
5358 bs->stop = 0;
5359 return;
5360 }
5361
5362 /* Evaluate extension language breakpoints that have a "stop" method
5363 implemented. */
5364 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
5365
5366 if (is_watchpoint (b))
5367 {
5368 struct watchpoint *w = (struct watchpoint *) b;
5369
5370 cond = w->cond_exp;
5371 }
5372 else
5373 cond = bl->cond;
5374
5375 if (cond && b->disposition != disp_del_at_next_stop)
5376 {
5377 int within_current_scope = 1;
5378 struct watchpoint * w;
5379
5380 /* We use value_mark and value_free_to_mark because it could
5381 be a long time before we return to the command level and
5382 call free_all_values. We can't call free_all_values
5383 because we might be in the middle of evaluating a
5384 function call. */
5385 struct value *mark = value_mark ();
5386
5387 if (is_watchpoint (b))
5388 w = (struct watchpoint *) b;
5389 else
5390 w = NULL;
5391
5392 /* Need to select the frame, with all that implies so that
5393 the conditions will have the right context. Because we
5394 use the frame, we will not see an inlined function's
5395 variables when we arrive at a breakpoint at the start
5396 of the inlined function; the current frame will be the
5397 call site. */
5398 if (w == NULL || w->cond_exp_valid_block == NULL)
5399 select_frame (get_current_frame ());
5400 else
5401 {
5402 struct frame_info *frame;
5403
5404 /* For local watchpoint expressions, which particular
5405 instance of a local is being watched matters, so we
5406 keep track of the frame to evaluate the expression
5407 in. To evaluate the condition however, it doesn't
5408 really matter which instantiation of the function
5409 where the condition makes sense triggers the
5410 watchpoint. This allows an expression like "watch
5411 global if q > 10" set in `func', catch writes to
5412 global on all threads that call `func', or catch
5413 writes on all recursive calls of `func' by a single
5414 thread. We simply always evaluate the condition in
5415 the innermost frame that's executing where it makes
5416 sense to evaluate the condition. It seems
5417 intuitive. */
5418 frame = block_innermost_frame (w->cond_exp_valid_block);
5419 if (frame != NULL)
5420 select_frame (frame);
5421 else
5422 within_current_scope = 0;
5423 }
5424 if (within_current_scope)
5425 value_is_zero
5426 = catch_errors (breakpoint_cond_eval, cond,
5427 "Error in testing breakpoint condition:\n",
5428 RETURN_MASK_ALL);
5429 else
5430 {
5431 warning (_("Watchpoint condition cannot be tested "
5432 "in the current scope"));
5433 /* If we failed to set the right context for this
5434 watchpoint, unconditionally report it. */
5435 value_is_zero = 0;
5436 }
5437 /* FIXME-someday, should give breakpoint #. */
5438 value_free_to_mark (mark);
5439 }
5440
5441 if (cond && value_is_zero)
5442 {
5443 bs->stop = 0;
5444 }
5445 else if (b->ignore_count > 0)
5446 {
5447 b->ignore_count--;
5448 bs->stop = 0;
5449 /* Increase the hit count even though we don't stop. */
5450 ++(b->hit_count);
5451 observer_notify_breakpoint_modified (b);
5452 }
5453 }
5454
5455 /* Returns true if we need to track moribund locations of LOC's type
5456 on the current target. */
5457
5458 static int
5459 need_moribund_for_location_type (struct bp_location *loc)
5460 {
5461 return ((loc->loc_type == bp_loc_software_breakpoint
5462 && !target_supports_stopped_by_sw_breakpoint ())
5463 || (loc->loc_type == bp_loc_hardware_breakpoint
5464 && !target_supports_stopped_by_hw_breakpoint ()));
5465 }
5466
5467
5468 /* Get a bpstat associated with having just stopped at address
5469 BP_ADDR in thread PTID.
5470
5471 Determine whether we stopped at a breakpoint, etc, or whether we
5472 don't understand this stop. Result is a chain of bpstat's such
5473 that:
5474
5475 if we don't understand the stop, the result is a null pointer.
5476
5477 if we understand why we stopped, the result is not null.
5478
5479 Each element of the chain refers to a particular breakpoint or
5480 watchpoint at which we have stopped. (We may have stopped for
5481 several reasons concurrently.)
5482
5483 Each element of the chain has valid next, breakpoint_at,
5484 commands, FIXME??? fields. */
5485
5486 bpstat
5487 bpstat_stop_status (struct address_space *aspace,
5488 CORE_ADDR bp_addr, ptid_t ptid,
5489 const struct target_waitstatus *ws)
5490 {
5491 struct breakpoint *b = NULL;
5492 struct bp_location *bl;
5493 struct bp_location *loc;
5494 /* First item of allocated bpstat's. */
5495 bpstat bs_head = NULL, *bs_link = &bs_head;
5496 /* Pointer to the last thing in the chain currently. */
5497 bpstat bs;
5498 int ix;
5499 int need_remove_insert;
5500 int removed_any;
5501
5502 /* First, build the bpstat chain with locations that explain a
5503 target stop, while being careful to not set the target running,
5504 as that may invalidate locations (in particular watchpoint
5505 locations are recreated). Resuming will happen here with
5506 breakpoint conditions or watchpoint expressions that include
5507 inferior function calls. */
5508
5509 ALL_BREAKPOINTS (b)
5510 {
5511 if (!breakpoint_enabled (b))
5512 continue;
5513
5514 for (bl = b->loc; bl != NULL; bl = bl->next)
5515 {
5516 /* For hardware watchpoints, we look only at the first
5517 location. The watchpoint_check function will work on the
5518 entire expression, not the individual locations. For
5519 read watchpoints, the watchpoints_triggered function has
5520 checked all locations already. */
5521 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5522 break;
5523
5524 if (!bl->enabled || bl->shlib_disabled)
5525 continue;
5526
5527 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
5528 continue;
5529
5530 /* Come here if it's a watchpoint, or if the break address
5531 matches. */
5532
5533 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
5534 explain stop. */
5535
5536 /* Assume we stop. Should we find a watchpoint that is not
5537 actually triggered, or if the condition of the breakpoint
5538 evaluates as false, we'll reset 'stop' to 0. */
5539 bs->stop = 1;
5540 bs->print = 1;
5541
5542 /* If this is a scope breakpoint, mark the associated
5543 watchpoint as triggered so that we will handle the
5544 out-of-scope event. We'll get to the watchpoint next
5545 iteration. */
5546 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
5547 {
5548 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5549
5550 w->watchpoint_triggered = watch_triggered_yes;
5551 }
5552 }
5553 }
5554
5555 /* Check if a moribund breakpoint explains the stop. */
5556 if (!target_supports_stopped_by_sw_breakpoint ()
5557 || !target_supports_stopped_by_hw_breakpoint ())
5558 {
5559 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
5560 {
5561 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5562 && need_moribund_for_location_type (loc))
5563 {
5564 bs = bpstat_alloc (loc, &bs_link);
5565 /* For hits of moribund locations, we should just proceed. */
5566 bs->stop = 0;
5567 bs->print = 0;
5568 bs->print_it = print_it_noop;
5569 }
5570 }
5571 }
5572
5573 /* A bit of special processing for shlib breakpoints. We need to
5574 process solib loading here, so that the lists of loaded and
5575 unloaded libraries are correct before we handle "catch load" and
5576 "catch unload". */
5577 for (bs = bs_head; bs != NULL; bs = bs->next)
5578 {
5579 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
5580 {
5581 handle_solib_event ();
5582 break;
5583 }
5584 }
5585
5586 /* Now go through the locations that caused the target to stop, and
5587 check whether we're interested in reporting this stop to higher
5588 layers, or whether we should resume the target transparently. */
5589
5590 removed_any = 0;
5591
5592 for (bs = bs_head; bs != NULL; bs = bs->next)
5593 {
5594 if (!bs->stop)
5595 continue;
5596
5597 b = bs->breakpoint_at;
5598 b->ops->check_status (bs);
5599 if (bs->stop)
5600 {
5601 bpstat_check_breakpoint_conditions (bs, ptid);
5602
5603 if (bs->stop)
5604 {
5605 ++(b->hit_count);
5606 observer_notify_breakpoint_modified (b);
5607
5608 /* We will stop here. */
5609 if (b->disposition == disp_disable)
5610 {
5611 --(b->enable_count);
5612 if (b->enable_count <= 0)
5613 b->enable_state = bp_disabled;
5614 removed_any = 1;
5615 }
5616 if (b->silent)
5617 bs->print = 0;
5618 bs->commands = b->commands;
5619 incref_counted_command_line (bs->commands);
5620 if (command_line_is_silent (bs->commands
5621 ? bs->commands->commands : NULL))
5622 bs->print = 0;
5623
5624 b->ops->after_condition_true (bs);
5625 }
5626
5627 }
5628
5629 /* Print nothing for this entry if we don't stop or don't
5630 print. */
5631 if (!bs->stop || !bs->print)
5632 bs->print_it = print_it_noop;
5633 }
5634
5635 /* If we aren't stopping, the value of some hardware watchpoint may
5636 not have changed, but the intermediate memory locations we are
5637 watching may have. Don't bother if we're stopping; this will get
5638 done later. */
5639 need_remove_insert = 0;
5640 if (! bpstat_causes_stop (bs_head))
5641 for (bs = bs_head; bs != NULL; bs = bs->next)
5642 if (!bs->stop
5643 && bs->breakpoint_at
5644 && is_hardware_watchpoint (bs->breakpoint_at))
5645 {
5646 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5647
5648 update_watchpoint (w, 0 /* don't reparse. */);
5649 need_remove_insert = 1;
5650 }
5651
5652 if (need_remove_insert)
5653 update_global_location_list (UGLL_MAY_INSERT);
5654 else if (removed_any)
5655 update_global_location_list (UGLL_DONT_INSERT);
5656
5657 return bs_head;
5658 }
5659
5660 static void
5661 handle_jit_event (void)
5662 {
5663 struct frame_info *frame;
5664 struct gdbarch *gdbarch;
5665
5666 /* Switch terminal for any messages produced by
5667 breakpoint_re_set. */
5668 target_terminal_ours_for_output ();
5669
5670 frame = get_current_frame ();
5671 gdbarch = get_frame_arch (frame);
5672
5673 jit_event_handler (gdbarch);
5674
5675 target_terminal_inferior ();
5676 }
5677
5678 /* Prepare WHAT final decision for infrun. */
5679
5680 /* Decide what infrun needs to do with this bpstat. */
5681
5682 struct bpstat_what
5683 bpstat_what (bpstat bs_head)
5684 {
5685 struct bpstat_what retval;
5686 int jit_event = 0;
5687 bpstat bs;
5688
5689 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
5690 retval.call_dummy = STOP_NONE;
5691 retval.is_longjmp = 0;
5692
5693 for (bs = bs_head; bs != NULL; bs = bs->next)
5694 {
5695 /* Extract this BS's action. After processing each BS, we check
5696 if its action overrides all we've seem so far. */
5697 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5698 enum bptype bptype;
5699
5700 if (bs->breakpoint_at == NULL)
5701 {
5702 /* I suspect this can happen if it was a momentary
5703 breakpoint which has since been deleted. */
5704 bptype = bp_none;
5705 }
5706 else
5707 bptype = bs->breakpoint_at->type;
5708
5709 switch (bptype)
5710 {
5711 case bp_none:
5712 break;
5713 case bp_breakpoint:
5714 case bp_hardware_breakpoint:
5715 case bp_single_step:
5716 case bp_until:
5717 case bp_finish:
5718 case bp_shlib_event:
5719 if (bs->stop)
5720 {
5721 if (bs->print)
5722 this_action = BPSTAT_WHAT_STOP_NOISY;
5723 else
5724 this_action = BPSTAT_WHAT_STOP_SILENT;
5725 }
5726 else
5727 this_action = BPSTAT_WHAT_SINGLE;
5728 break;
5729 case bp_watchpoint:
5730 case bp_hardware_watchpoint:
5731 case bp_read_watchpoint:
5732 case bp_access_watchpoint:
5733 if (bs->stop)
5734 {
5735 if (bs->print)
5736 this_action = BPSTAT_WHAT_STOP_NOISY;
5737 else
5738 this_action = BPSTAT_WHAT_STOP_SILENT;
5739 }
5740 else
5741 {
5742 /* There was a watchpoint, but we're not stopping.
5743 This requires no further action. */
5744 }
5745 break;
5746 case bp_longjmp:
5747 case bp_longjmp_call_dummy:
5748 case bp_exception:
5749 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5750 retval.is_longjmp = bptype != bp_exception;
5751 break;
5752 case bp_longjmp_resume:
5753 case bp_exception_resume:
5754 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5755 retval.is_longjmp = bptype == bp_longjmp_resume;
5756 break;
5757 case bp_step_resume:
5758 if (bs->stop)
5759 this_action = BPSTAT_WHAT_STEP_RESUME;
5760 else
5761 {
5762 /* It is for the wrong frame. */
5763 this_action = BPSTAT_WHAT_SINGLE;
5764 }
5765 break;
5766 case bp_hp_step_resume:
5767 if (bs->stop)
5768 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5769 else
5770 {
5771 /* It is for the wrong frame. */
5772 this_action = BPSTAT_WHAT_SINGLE;
5773 }
5774 break;
5775 case bp_watchpoint_scope:
5776 case bp_thread_event:
5777 case bp_overlay_event:
5778 case bp_longjmp_master:
5779 case bp_std_terminate_master:
5780 case bp_exception_master:
5781 this_action = BPSTAT_WHAT_SINGLE;
5782 break;
5783 case bp_catchpoint:
5784 if (bs->stop)
5785 {
5786 if (bs->print)
5787 this_action = BPSTAT_WHAT_STOP_NOISY;
5788 else
5789 this_action = BPSTAT_WHAT_STOP_SILENT;
5790 }
5791 else
5792 {
5793 /* There was a catchpoint, but we're not stopping.
5794 This requires no further action. */
5795 }
5796 break;
5797 case bp_jit_event:
5798 jit_event = 1;
5799 this_action = BPSTAT_WHAT_SINGLE;
5800 break;
5801 case bp_call_dummy:
5802 /* Make sure the action is stop (silent or noisy),
5803 so infrun.c pops the dummy frame. */
5804 retval.call_dummy = STOP_STACK_DUMMY;
5805 this_action = BPSTAT_WHAT_STOP_SILENT;
5806 break;
5807 case bp_std_terminate:
5808 /* Make sure the action is stop (silent or noisy),
5809 so infrun.c pops the dummy frame. */
5810 retval.call_dummy = STOP_STD_TERMINATE;
5811 this_action = BPSTAT_WHAT_STOP_SILENT;
5812 break;
5813 case bp_tracepoint:
5814 case bp_fast_tracepoint:
5815 case bp_static_tracepoint:
5816 /* Tracepoint hits should not be reported back to GDB, and
5817 if one got through somehow, it should have been filtered
5818 out already. */
5819 internal_error (__FILE__, __LINE__,
5820 _("bpstat_what: tracepoint encountered"));
5821 break;
5822 case bp_gnu_ifunc_resolver:
5823 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5824 this_action = BPSTAT_WHAT_SINGLE;
5825 break;
5826 case bp_gnu_ifunc_resolver_return:
5827 /* The breakpoint will be removed, execution will restart from the
5828 PC of the former breakpoint. */
5829 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5830 break;
5831
5832 case bp_dprintf:
5833 if (bs->stop)
5834 this_action = BPSTAT_WHAT_STOP_SILENT;
5835 else
5836 this_action = BPSTAT_WHAT_SINGLE;
5837 break;
5838
5839 default:
5840 internal_error (__FILE__, __LINE__,
5841 _("bpstat_what: unhandled bptype %d"), (int) bptype);
5842 }
5843
5844 retval.main_action = max (retval.main_action, this_action);
5845 }
5846
5847 /* These operations may affect the bs->breakpoint_at state so they are
5848 delayed after MAIN_ACTION is decided above. */
5849
5850 if (jit_event)
5851 {
5852 if (debug_infrun)
5853 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_jit_event\n");
5854
5855 handle_jit_event ();
5856 }
5857
5858 for (bs = bs_head; bs != NULL; bs = bs->next)
5859 {
5860 struct breakpoint *b = bs->breakpoint_at;
5861
5862 if (b == NULL)
5863 continue;
5864 switch (b->type)
5865 {
5866 case bp_gnu_ifunc_resolver:
5867 gnu_ifunc_resolver_stop (b);
5868 break;
5869 case bp_gnu_ifunc_resolver_return:
5870 gnu_ifunc_resolver_return_stop (b);
5871 break;
5872 }
5873 }
5874
5875 return retval;
5876 }
5877
5878 /* Nonzero if we should step constantly (e.g. watchpoints on machines
5879 without hardware support). This isn't related to a specific bpstat,
5880 just to things like whether watchpoints are set. */
5881
5882 int
5883 bpstat_should_step (void)
5884 {
5885 struct breakpoint *b;
5886
5887 ALL_BREAKPOINTS (b)
5888 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
5889 return 1;
5890 return 0;
5891 }
5892
5893 int
5894 bpstat_causes_stop (bpstat bs)
5895 {
5896 for (; bs != NULL; bs = bs->next)
5897 if (bs->stop)
5898 return 1;
5899
5900 return 0;
5901 }
5902
5903 \f
5904
5905 /* Compute a string of spaces suitable to indent the next line
5906 so it starts at the position corresponding to the table column
5907 named COL_NAME in the currently active table of UIOUT. */
5908
5909 static char *
5910 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5911 {
5912 static char wrap_indent[80];
5913 int i, total_width, width, align;
5914 char *text;
5915
5916 total_width = 0;
5917 for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
5918 {
5919 if (strcmp (text, col_name) == 0)
5920 {
5921 gdb_assert (total_width < sizeof wrap_indent);
5922 memset (wrap_indent, ' ', total_width);
5923 wrap_indent[total_width] = 0;
5924
5925 return wrap_indent;
5926 }
5927
5928 total_width += width + 1;
5929 }
5930
5931 return NULL;
5932 }
5933
5934 /* Determine if the locations of this breakpoint will have their conditions
5935 evaluated by the target, host or a mix of both. Returns the following:
5936
5937 "host": Host evals condition.
5938 "host or target": Host or Target evals condition.
5939 "target": Target evals condition.
5940 */
5941
5942 static const char *
5943 bp_condition_evaluator (struct breakpoint *b)
5944 {
5945 struct bp_location *bl;
5946 char host_evals = 0;
5947 char target_evals = 0;
5948
5949 if (!b)
5950 return NULL;
5951
5952 if (!is_breakpoint (b))
5953 return NULL;
5954
5955 if (gdb_evaluates_breakpoint_condition_p ()
5956 || !target_supports_evaluation_of_breakpoint_conditions ())
5957 return condition_evaluation_host;
5958
5959 for (bl = b->loc; bl; bl = bl->next)
5960 {
5961 if (bl->cond_bytecode)
5962 target_evals++;
5963 else
5964 host_evals++;
5965 }
5966
5967 if (host_evals && target_evals)
5968 return condition_evaluation_both;
5969 else if (target_evals)
5970 return condition_evaluation_target;
5971 else
5972 return condition_evaluation_host;
5973 }
5974
5975 /* Determine the breakpoint location's condition evaluator. This is
5976 similar to bp_condition_evaluator, but for locations. */
5977
5978 static const char *
5979 bp_location_condition_evaluator (struct bp_location *bl)
5980 {
5981 if (bl && !is_breakpoint (bl->owner))
5982 return NULL;
5983
5984 if (gdb_evaluates_breakpoint_condition_p ()
5985 || !target_supports_evaluation_of_breakpoint_conditions ())
5986 return condition_evaluation_host;
5987
5988 if (bl && bl->cond_bytecode)
5989 return condition_evaluation_target;
5990 else
5991 return condition_evaluation_host;
5992 }
5993
5994 /* Print the LOC location out of the list of B->LOC locations. */
5995
5996 static void
5997 print_breakpoint_location (struct breakpoint *b,
5998 struct bp_location *loc)
5999 {
6000 struct ui_out *uiout = current_uiout;
6001 struct cleanup *old_chain = save_current_program_space ();
6002
6003 if (loc != NULL && loc->shlib_disabled)
6004 loc = NULL;
6005
6006 if (loc != NULL)
6007 set_current_program_space (loc->pspace);
6008
6009 if (b->display_canonical)
6010 ui_out_field_string (uiout, "what", b->addr_string);
6011 else if (loc && loc->symtab)
6012 {
6013 struct symbol *sym
6014 = find_pc_sect_function (loc->address, loc->section);
6015 if (sym)
6016 {
6017 ui_out_text (uiout, "in ");
6018 ui_out_field_string (uiout, "func",
6019 SYMBOL_PRINT_NAME (sym));
6020 ui_out_text (uiout, " ");
6021 ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
6022 ui_out_text (uiout, "at ");
6023 }
6024 ui_out_field_string (uiout, "file",
6025 symtab_to_filename_for_display (loc->symtab));
6026 ui_out_text (uiout, ":");
6027
6028 if (ui_out_is_mi_like_p (uiout))
6029 ui_out_field_string (uiout, "fullname",
6030 symtab_to_fullname (loc->symtab));
6031
6032 ui_out_field_int (uiout, "line", loc->line_number);
6033 }
6034 else if (loc)
6035 {
6036 struct ui_file *stb = mem_fileopen ();
6037 struct cleanup *stb_chain = make_cleanup_ui_file_delete (stb);
6038
6039 print_address_symbolic (loc->gdbarch, loc->address, stb,
6040 demangle, "");
6041 ui_out_field_stream (uiout, "at", stb);
6042
6043 do_cleanups (stb_chain);
6044 }
6045 else
6046 ui_out_field_string (uiout, "pending", b->addr_string);
6047
6048 if (loc && is_breakpoint (b)
6049 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6050 && bp_condition_evaluator (b) == condition_evaluation_both)
6051 {
6052 ui_out_text (uiout, " (");
6053 ui_out_field_string (uiout, "evaluated-by",
6054 bp_location_condition_evaluator (loc));
6055 ui_out_text (uiout, ")");
6056 }
6057
6058 do_cleanups (old_chain);
6059 }
6060
6061 static const char *
6062 bptype_string (enum bptype type)
6063 {
6064 struct ep_type_description
6065 {
6066 enum bptype type;
6067 char *description;
6068 };
6069 static struct ep_type_description bptypes[] =
6070 {
6071 {bp_none, "?deleted?"},
6072 {bp_breakpoint, "breakpoint"},
6073 {bp_hardware_breakpoint, "hw breakpoint"},
6074 {bp_single_step, "sw single-step"},
6075 {bp_until, "until"},
6076 {bp_finish, "finish"},
6077 {bp_watchpoint, "watchpoint"},
6078 {bp_hardware_watchpoint, "hw watchpoint"},
6079 {bp_read_watchpoint, "read watchpoint"},
6080 {bp_access_watchpoint, "acc watchpoint"},
6081 {bp_longjmp, "longjmp"},
6082 {bp_longjmp_resume, "longjmp resume"},
6083 {bp_longjmp_call_dummy, "longjmp for call dummy"},
6084 {bp_exception, "exception"},
6085 {bp_exception_resume, "exception resume"},
6086 {bp_step_resume, "step resume"},
6087 {bp_hp_step_resume, "high-priority step resume"},
6088 {bp_watchpoint_scope, "watchpoint scope"},
6089 {bp_call_dummy, "call dummy"},
6090 {bp_std_terminate, "std::terminate"},
6091 {bp_shlib_event, "shlib events"},
6092 {bp_thread_event, "thread events"},
6093 {bp_overlay_event, "overlay events"},
6094 {bp_longjmp_master, "longjmp master"},
6095 {bp_std_terminate_master, "std::terminate master"},
6096 {bp_exception_master, "exception master"},
6097 {bp_catchpoint, "catchpoint"},
6098 {bp_tracepoint, "tracepoint"},
6099 {bp_fast_tracepoint, "fast tracepoint"},
6100 {bp_static_tracepoint, "static tracepoint"},
6101 {bp_dprintf, "dprintf"},
6102 {bp_jit_event, "jit events"},
6103 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6104 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
6105 };
6106
6107 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6108 || ((int) type != bptypes[(int) type].type))
6109 internal_error (__FILE__, __LINE__,
6110 _("bptypes table does not describe type #%d."),
6111 (int) type);
6112
6113 return bptypes[(int) type].description;
6114 }
6115
6116 /* For MI, output a field named 'thread-groups' with a list as the value.
6117 For CLI, prefix the list with the string 'inf'. */
6118
6119 static void
6120 output_thread_groups (struct ui_out *uiout,
6121 const char *field_name,
6122 VEC(int) *inf_num,
6123 int mi_only)
6124 {
6125 struct cleanup *back_to;
6126 int is_mi = ui_out_is_mi_like_p (uiout);
6127 int inf;
6128 int i;
6129
6130 /* For backward compatibility, don't display inferiors in CLI unless
6131 there are several. Always display them for MI. */
6132 if (!is_mi && mi_only)
6133 return;
6134
6135 back_to = make_cleanup_ui_out_list_begin_end (uiout, field_name);
6136
6137 for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
6138 {
6139 if (is_mi)
6140 {
6141 char mi_group[10];
6142
6143 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
6144 ui_out_field_string (uiout, NULL, mi_group);
6145 }
6146 else
6147 {
6148 if (i == 0)
6149 ui_out_text (uiout, " inf ");
6150 else
6151 ui_out_text (uiout, ", ");
6152
6153 ui_out_text (uiout, plongest (inf));
6154 }
6155 }
6156
6157 do_cleanups (back_to);
6158 }
6159
6160 /* Print B to gdb_stdout. */
6161
6162 static void
6163 print_one_breakpoint_location (struct breakpoint *b,
6164 struct bp_location *loc,
6165 int loc_number,
6166 struct bp_location **last_loc,
6167 int allflag)
6168 {
6169 struct command_line *l;
6170 static char bpenables[] = "nynny";
6171
6172 struct ui_out *uiout = current_uiout;
6173 int header_of_multiple = 0;
6174 int part_of_multiple = (loc != NULL);
6175 struct value_print_options opts;
6176
6177 get_user_print_options (&opts);
6178
6179 gdb_assert (!loc || loc_number != 0);
6180 /* See comment in print_one_breakpoint concerning treatment of
6181 breakpoints with single disabled location. */
6182 if (loc == NULL
6183 && (b->loc != NULL
6184 && (b->loc->next != NULL || !b->loc->enabled)))
6185 header_of_multiple = 1;
6186 if (loc == NULL)
6187 loc = b->loc;
6188
6189 annotate_record ();
6190
6191 /* 1 */
6192 annotate_field (0);
6193 if (part_of_multiple)
6194 {
6195 char *formatted;
6196 formatted = xstrprintf ("%d.%d", b->number, loc_number);
6197 ui_out_field_string (uiout, "number", formatted);
6198 xfree (formatted);
6199 }
6200 else
6201 {
6202 ui_out_field_int (uiout, "number", b->number);
6203 }
6204
6205 /* 2 */
6206 annotate_field (1);
6207 if (part_of_multiple)
6208 ui_out_field_skip (uiout, "type");
6209 else
6210 ui_out_field_string (uiout, "type", bptype_string (b->type));
6211
6212 /* 3 */
6213 annotate_field (2);
6214 if (part_of_multiple)
6215 ui_out_field_skip (uiout, "disp");
6216 else
6217 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
6218
6219
6220 /* 4 */
6221 annotate_field (3);
6222 if (part_of_multiple)
6223 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
6224 else
6225 ui_out_field_fmt (uiout, "enabled", "%c",
6226 bpenables[(int) b->enable_state]);
6227 ui_out_spaces (uiout, 2);
6228
6229
6230 /* 5 and 6 */
6231 if (b->ops != NULL && b->ops->print_one != NULL)
6232 {
6233 /* Although the print_one can possibly print all locations,
6234 calling it here is not likely to get any nice result. So,
6235 make sure there's just one location. */
6236 gdb_assert (b->loc == NULL || b->loc->next == NULL);
6237 b->ops->print_one (b, last_loc);
6238 }
6239 else
6240 switch (b->type)
6241 {
6242 case bp_none:
6243 internal_error (__FILE__, __LINE__,
6244 _("print_one_breakpoint: bp_none encountered\n"));
6245 break;
6246
6247 case bp_watchpoint:
6248 case bp_hardware_watchpoint:
6249 case bp_read_watchpoint:
6250 case bp_access_watchpoint:
6251 {
6252 struct watchpoint *w = (struct watchpoint *) b;
6253
6254 /* Field 4, the address, is omitted (which makes the columns
6255 not line up too nicely with the headers, but the effect
6256 is relatively readable). */
6257 if (opts.addressprint)
6258 ui_out_field_skip (uiout, "addr");
6259 annotate_field (5);
6260 ui_out_field_string (uiout, "what", w->exp_string);
6261 }
6262 break;
6263
6264 case bp_breakpoint:
6265 case bp_hardware_breakpoint:
6266 case bp_single_step:
6267 case bp_until:
6268 case bp_finish:
6269 case bp_longjmp:
6270 case bp_longjmp_resume:
6271 case bp_longjmp_call_dummy:
6272 case bp_exception:
6273 case bp_exception_resume:
6274 case bp_step_resume:
6275 case bp_hp_step_resume:
6276 case bp_watchpoint_scope:
6277 case bp_call_dummy:
6278 case bp_std_terminate:
6279 case bp_shlib_event:
6280 case bp_thread_event:
6281 case bp_overlay_event:
6282 case bp_longjmp_master:
6283 case bp_std_terminate_master:
6284 case bp_exception_master:
6285 case bp_tracepoint:
6286 case bp_fast_tracepoint:
6287 case bp_static_tracepoint:
6288 case bp_dprintf:
6289 case bp_jit_event:
6290 case bp_gnu_ifunc_resolver:
6291 case bp_gnu_ifunc_resolver_return:
6292 if (opts.addressprint)
6293 {
6294 annotate_field (4);
6295 if (header_of_multiple)
6296 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
6297 else if (b->loc == NULL || loc->shlib_disabled)
6298 ui_out_field_string (uiout, "addr", "<PENDING>");
6299 else
6300 ui_out_field_core_addr (uiout, "addr",
6301 loc->gdbarch, loc->address);
6302 }
6303 annotate_field (5);
6304 if (!header_of_multiple)
6305 print_breakpoint_location (b, loc);
6306 if (b->loc)
6307 *last_loc = b->loc;
6308 break;
6309 }
6310
6311
6312 if (loc != NULL && !header_of_multiple)
6313 {
6314 struct inferior *inf;
6315 VEC(int) *inf_num = NULL;
6316 int mi_only = 1;
6317
6318 ALL_INFERIORS (inf)
6319 {
6320 if (inf->pspace == loc->pspace)
6321 VEC_safe_push (int, inf_num, inf->num);
6322 }
6323
6324 /* For backward compatibility, don't display inferiors in CLI unless
6325 there are several. Always display for MI. */
6326 if (allflag
6327 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6328 && (number_of_program_spaces () > 1
6329 || number_of_inferiors () > 1)
6330 /* LOC is for existing B, it cannot be in
6331 moribund_locations and thus having NULL OWNER. */
6332 && loc->owner->type != bp_catchpoint))
6333 mi_only = 0;
6334 output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6335 VEC_free (int, inf_num);
6336 }
6337
6338 if (!part_of_multiple)
6339 {
6340 if (b->thread != -1)
6341 {
6342 /* FIXME: This seems to be redundant and lost here; see the
6343 "stop only in" line a little further down. */
6344 ui_out_text (uiout, " thread ");
6345 ui_out_field_int (uiout, "thread", b->thread);
6346 }
6347 else if (b->task != 0)
6348 {
6349 ui_out_text (uiout, " task ");
6350 ui_out_field_int (uiout, "task", b->task);
6351 }
6352 }
6353
6354 ui_out_text (uiout, "\n");
6355
6356 if (!part_of_multiple)
6357 b->ops->print_one_detail (b, uiout);
6358
6359 if (part_of_multiple && frame_id_p (b->frame_id))
6360 {
6361 annotate_field (6);
6362 ui_out_text (uiout, "\tstop only in stack frame at ");
6363 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
6364 the frame ID. */
6365 ui_out_field_core_addr (uiout, "frame",
6366 b->gdbarch, b->frame_id.stack_addr);
6367 ui_out_text (uiout, "\n");
6368 }
6369
6370 if (!part_of_multiple && b->cond_string)
6371 {
6372 annotate_field (7);
6373 if (is_tracepoint (b))
6374 ui_out_text (uiout, "\ttrace only if ");
6375 else
6376 ui_out_text (uiout, "\tstop only if ");
6377 ui_out_field_string (uiout, "cond", b->cond_string);
6378
6379 /* Print whether the target is doing the breakpoint's condition
6380 evaluation. If GDB is doing the evaluation, don't print anything. */
6381 if (is_breakpoint (b)
6382 && breakpoint_condition_evaluation_mode ()
6383 == condition_evaluation_target)
6384 {
6385 ui_out_text (uiout, " (");
6386 ui_out_field_string (uiout, "evaluated-by",
6387 bp_condition_evaluator (b));
6388 ui_out_text (uiout, " evals)");
6389 }
6390 ui_out_text (uiout, "\n");
6391 }
6392
6393 if (!part_of_multiple && b->thread != -1)
6394 {
6395 /* FIXME should make an annotation for this. */
6396 ui_out_text (uiout, "\tstop only in thread ");
6397 ui_out_field_int (uiout, "thread", b->thread);
6398 ui_out_text (uiout, "\n");
6399 }
6400
6401 if (!part_of_multiple)
6402 {
6403 if (b->hit_count)
6404 {
6405 /* FIXME should make an annotation for this. */
6406 if (is_catchpoint (b))
6407 ui_out_text (uiout, "\tcatchpoint");
6408 else if (is_tracepoint (b))
6409 ui_out_text (uiout, "\ttracepoint");
6410 else
6411 ui_out_text (uiout, "\tbreakpoint");
6412 ui_out_text (uiout, " already hit ");
6413 ui_out_field_int (uiout, "times", b->hit_count);
6414 if (b->hit_count == 1)
6415 ui_out_text (uiout, " time\n");
6416 else
6417 ui_out_text (uiout, " times\n");
6418 }
6419 else
6420 {
6421 /* Output the count also if it is zero, but only if this is mi. */
6422 if (ui_out_is_mi_like_p (uiout))
6423 ui_out_field_int (uiout, "times", b->hit_count);
6424 }
6425 }
6426
6427 if (!part_of_multiple && b->ignore_count)
6428 {
6429 annotate_field (8);
6430 ui_out_text (uiout, "\tignore next ");
6431 ui_out_field_int (uiout, "ignore", b->ignore_count);
6432 ui_out_text (uiout, " hits\n");
6433 }
6434
6435 /* Note that an enable count of 1 corresponds to "enable once"
6436 behavior, which is reported by the combination of enablement and
6437 disposition, so we don't need to mention it here. */
6438 if (!part_of_multiple && b->enable_count > 1)
6439 {
6440 annotate_field (8);
6441 ui_out_text (uiout, "\tdisable after ");
6442 /* Tweak the wording to clarify that ignore and enable counts
6443 are distinct, and have additive effect. */
6444 if (b->ignore_count)
6445 ui_out_text (uiout, "additional ");
6446 else
6447 ui_out_text (uiout, "next ");
6448 ui_out_field_int (uiout, "enable", b->enable_count);
6449 ui_out_text (uiout, " hits\n");
6450 }
6451
6452 if (!part_of_multiple && is_tracepoint (b))
6453 {
6454 struct tracepoint *tp = (struct tracepoint *) b;
6455
6456 if (tp->traceframe_usage)
6457 {
6458 ui_out_text (uiout, "\ttrace buffer usage ");
6459 ui_out_field_int (uiout, "traceframe-usage", tp->traceframe_usage);
6460 ui_out_text (uiout, " bytes\n");
6461 }
6462 }
6463
6464 l = b->commands ? b->commands->commands : NULL;
6465 if (!part_of_multiple && l)
6466 {
6467 struct cleanup *script_chain;
6468
6469 annotate_field (9);
6470 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
6471 print_command_lines (uiout, l, 4);
6472 do_cleanups (script_chain);
6473 }
6474
6475 if (is_tracepoint (b))
6476 {
6477 struct tracepoint *t = (struct tracepoint *) b;
6478
6479 if (!part_of_multiple && t->pass_count)
6480 {
6481 annotate_field (10);
6482 ui_out_text (uiout, "\tpass count ");
6483 ui_out_field_int (uiout, "pass", t->pass_count);
6484 ui_out_text (uiout, " \n");
6485 }
6486
6487 /* Don't display it when tracepoint or tracepoint location is
6488 pending. */
6489 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6490 {
6491 annotate_field (11);
6492
6493 if (ui_out_is_mi_like_p (uiout))
6494 ui_out_field_string (uiout, "installed",
6495 loc->inserted ? "y" : "n");
6496 else
6497 {
6498 if (loc->inserted)
6499 ui_out_text (uiout, "\t");
6500 else
6501 ui_out_text (uiout, "\tnot ");
6502 ui_out_text (uiout, "installed on target\n");
6503 }
6504 }
6505 }
6506
6507 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
6508 {
6509 if (is_watchpoint (b))
6510 {
6511 struct watchpoint *w = (struct watchpoint *) b;
6512
6513 ui_out_field_string (uiout, "original-location", w->exp_string);
6514 }
6515 else if (b->addr_string)
6516 ui_out_field_string (uiout, "original-location", b->addr_string);
6517 }
6518 }
6519
6520 static void
6521 print_one_breakpoint (struct breakpoint *b,
6522 struct bp_location **last_loc,
6523 int allflag)
6524 {
6525 struct cleanup *bkpt_chain;
6526 struct ui_out *uiout = current_uiout;
6527
6528 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
6529
6530 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
6531 do_cleanups (bkpt_chain);
6532
6533 /* If this breakpoint has custom print function,
6534 it's already printed. Otherwise, print individual
6535 locations, if any. */
6536 if (b->ops == NULL || b->ops->print_one == NULL)
6537 {
6538 /* If breakpoint has a single location that is disabled, we
6539 print it as if it had several locations, since otherwise it's
6540 hard to represent "breakpoint enabled, location disabled"
6541 situation.
6542
6543 Note that while hardware watchpoints have several locations
6544 internally, that's not a property exposed to user. */
6545 if (b->loc
6546 && !is_hardware_watchpoint (b)
6547 && (b->loc->next || !b->loc->enabled))
6548 {
6549 struct bp_location *loc;
6550 int n = 1;
6551
6552 for (loc = b->loc; loc; loc = loc->next, ++n)
6553 {
6554 struct cleanup *inner2 =
6555 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
6556 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
6557 do_cleanups (inner2);
6558 }
6559 }
6560 }
6561 }
6562
6563 static int
6564 breakpoint_address_bits (struct breakpoint *b)
6565 {
6566 int print_address_bits = 0;
6567 struct bp_location *loc;
6568
6569 for (loc = b->loc; loc; loc = loc->next)
6570 {
6571 int addr_bit;
6572
6573 /* Software watchpoints that aren't watching memory don't have
6574 an address to print. */
6575 if (b->type == bp_watchpoint && loc->watchpoint_type == -1)
6576 continue;
6577
6578 addr_bit = gdbarch_addr_bit (loc->gdbarch);
6579 if (addr_bit > print_address_bits)
6580 print_address_bits = addr_bit;
6581 }
6582
6583 return print_address_bits;
6584 }
6585
6586 struct captured_breakpoint_query_args
6587 {
6588 int bnum;
6589 };
6590
6591 static int
6592 do_captured_breakpoint_query (struct ui_out *uiout, void *data)
6593 {
6594 struct captured_breakpoint_query_args *args = data;
6595 struct breakpoint *b;
6596 struct bp_location *dummy_loc = NULL;
6597
6598 ALL_BREAKPOINTS (b)
6599 {
6600 if (args->bnum == b->number)
6601 {
6602 print_one_breakpoint (b, &dummy_loc, 0);
6603 return GDB_RC_OK;
6604 }
6605 }
6606 return GDB_RC_NONE;
6607 }
6608
6609 enum gdb_rc
6610 gdb_breakpoint_query (struct ui_out *uiout, int bnum,
6611 char **error_message)
6612 {
6613 struct captured_breakpoint_query_args args;
6614
6615 args.bnum = bnum;
6616 /* For the moment we don't trust print_one_breakpoint() to not throw
6617 an error. */
6618 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
6619 error_message, RETURN_MASK_ALL) < 0)
6620 return GDB_RC_FAIL;
6621 else
6622 return GDB_RC_OK;
6623 }
6624
6625 /* Return true if this breakpoint was set by the user, false if it is
6626 internal or momentary. */
6627
6628 int
6629 user_breakpoint_p (struct breakpoint *b)
6630 {
6631 return b->number > 0;
6632 }
6633
6634 /* Print information on user settable breakpoint (watchpoint, etc)
6635 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6636 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6637 FILTER is non-NULL, call it on each breakpoint and only include the
6638 ones for which it returns non-zero. Return the total number of
6639 breakpoints listed. */
6640
6641 static int
6642 breakpoint_1 (char *args, int allflag,
6643 int (*filter) (const struct breakpoint *))
6644 {
6645 struct breakpoint *b;
6646 struct bp_location *last_loc = NULL;
6647 int nr_printable_breakpoints;
6648 struct cleanup *bkpttbl_chain;
6649 struct value_print_options opts;
6650 int print_address_bits = 0;
6651 int print_type_col_width = 14;
6652 struct ui_out *uiout = current_uiout;
6653
6654 get_user_print_options (&opts);
6655
6656 /* Compute the number of rows in the table, as well as the size
6657 required for address fields. */
6658 nr_printable_breakpoints = 0;
6659 ALL_BREAKPOINTS (b)
6660 {
6661 /* If we have a filter, only list the breakpoints it accepts. */
6662 if (filter && !filter (b))
6663 continue;
6664
6665 /* If we have an "args" string, it is a list of breakpoints to
6666 accept. Skip the others. */
6667 if (args != NULL && *args != '\0')
6668 {
6669 if (allflag && parse_and_eval_long (args) != b->number)
6670 continue;
6671 if (!allflag && !number_is_in_list (args, b->number))
6672 continue;
6673 }
6674
6675 if (allflag || user_breakpoint_p (b))
6676 {
6677 int addr_bit, type_len;
6678
6679 addr_bit = breakpoint_address_bits (b);
6680 if (addr_bit > print_address_bits)
6681 print_address_bits = addr_bit;
6682
6683 type_len = strlen (bptype_string (b->type));
6684 if (type_len > print_type_col_width)
6685 print_type_col_width = type_len;
6686
6687 nr_printable_breakpoints++;
6688 }
6689 }
6690
6691 if (opts.addressprint)
6692 bkpttbl_chain
6693 = make_cleanup_ui_out_table_begin_end (uiout, 6,
6694 nr_printable_breakpoints,
6695 "BreakpointTable");
6696 else
6697 bkpttbl_chain
6698 = make_cleanup_ui_out_table_begin_end (uiout, 5,
6699 nr_printable_breakpoints,
6700 "BreakpointTable");
6701
6702 if (nr_printable_breakpoints > 0)
6703 annotate_breakpoints_headers ();
6704 if (nr_printable_breakpoints > 0)
6705 annotate_field (0);
6706 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
6707 if (nr_printable_breakpoints > 0)
6708 annotate_field (1);
6709 ui_out_table_header (uiout, print_type_col_width, ui_left,
6710 "type", "Type"); /* 2 */
6711 if (nr_printable_breakpoints > 0)
6712 annotate_field (2);
6713 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
6714 if (nr_printable_breakpoints > 0)
6715 annotate_field (3);
6716 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
6717 if (opts.addressprint)
6718 {
6719 if (nr_printable_breakpoints > 0)
6720 annotate_field (4);
6721 if (print_address_bits <= 32)
6722 ui_out_table_header (uiout, 10, ui_left,
6723 "addr", "Address"); /* 5 */
6724 else
6725 ui_out_table_header (uiout, 18, ui_left,
6726 "addr", "Address"); /* 5 */
6727 }
6728 if (nr_printable_breakpoints > 0)
6729 annotate_field (5);
6730 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
6731 ui_out_table_body (uiout);
6732 if (nr_printable_breakpoints > 0)
6733 annotate_breakpoints_table ();
6734
6735 ALL_BREAKPOINTS (b)
6736 {
6737 QUIT;
6738 /* If we have a filter, only list the breakpoints it accepts. */
6739 if (filter && !filter (b))
6740 continue;
6741
6742 /* If we have an "args" string, it is a list of breakpoints to
6743 accept. Skip the others. */
6744
6745 if (args != NULL && *args != '\0')
6746 {
6747 if (allflag) /* maintenance info breakpoint */
6748 {
6749 if (parse_and_eval_long (args) != b->number)
6750 continue;
6751 }
6752 else /* all others */
6753 {
6754 if (!number_is_in_list (args, b->number))
6755 continue;
6756 }
6757 }
6758 /* We only print out user settable breakpoints unless the
6759 allflag is set. */
6760 if (allflag || user_breakpoint_p (b))
6761 print_one_breakpoint (b, &last_loc, allflag);
6762 }
6763
6764 do_cleanups (bkpttbl_chain);
6765
6766 if (nr_printable_breakpoints == 0)
6767 {
6768 /* If there's a filter, let the caller decide how to report
6769 empty list. */
6770 if (!filter)
6771 {
6772 if (args == NULL || *args == '\0')
6773 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
6774 else
6775 ui_out_message (uiout, 0,
6776 "No breakpoint or watchpoint matching '%s'.\n",
6777 args);
6778 }
6779 }
6780 else
6781 {
6782 if (last_loc && !server_command)
6783 set_next_address (last_loc->gdbarch, last_loc->address);
6784 }
6785
6786 /* FIXME? Should this be moved up so that it is only called when
6787 there have been breakpoints? */
6788 annotate_breakpoints_table_end ();
6789
6790 return nr_printable_breakpoints;
6791 }
6792
6793 /* Display the value of default-collect in a way that is generally
6794 compatible with the breakpoint list. */
6795
6796 static void
6797 default_collect_info (void)
6798 {
6799 struct ui_out *uiout = current_uiout;
6800
6801 /* If it has no value (which is frequently the case), say nothing; a
6802 message like "No default-collect." gets in user's face when it's
6803 not wanted. */
6804 if (!*default_collect)
6805 return;
6806
6807 /* The following phrase lines up nicely with per-tracepoint collect
6808 actions. */
6809 ui_out_text (uiout, "default collect ");
6810 ui_out_field_string (uiout, "default-collect", default_collect);
6811 ui_out_text (uiout, " \n");
6812 }
6813
6814 static void
6815 breakpoints_info (char *args, int from_tty)
6816 {
6817 breakpoint_1 (args, 0, NULL);
6818
6819 default_collect_info ();
6820 }
6821
6822 static void
6823 watchpoints_info (char *args, int from_tty)
6824 {
6825 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
6826 struct ui_out *uiout = current_uiout;
6827
6828 if (num_printed == 0)
6829 {
6830 if (args == NULL || *args == '\0')
6831 ui_out_message (uiout, 0, "No watchpoints.\n");
6832 else
6833 ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
6834 }
6835 }
6836
6837 static void
6838 maintenance_info_breakpoints (char *args, int from_tty)
6839 {
6840 breakpoint_1 (args, 1, NULL);
6841
6842 default_collect_info ();
6843 }
6844
6845 static int
6846 breakpoint_has_pc (struct breakpoint *b,
6847 struct program_space *pspace,
6848 CORE_ADDR pc, struct obj_section *section)
6849 {
6850 struct bp_location *bl = b->loc;
6851
6852 for (; bl; bl = bl->next)
6853 {
6854 if (bl->pspace == pspace
6855 && bl->address == pc
6856 && (!overlay_debugging || bl->section == section))
6857 return 1;
6858 }
6859 return 0;
6860 }
6861
6862 /* Print a message describing any user-breakpoints set at PC. This
6863 concerns with logical breakpoints, so we match program spaces, not
6864 address spaces. */
6865
6866 static void
6867 describe_other_breakpoints (struct gdbarch *gdbarch,
6868 struct program_space *pspace, CORE_ADDR pc,
6869 struct obj_section *section, int thread)
6870 {
6871 int others = 0;
6872 struct breakpoint *b;
6873
6874 ALL_BREAKPOINTS (b)
6875 others += (user_breakpoint_p (b)
6876 && breakpoint_has_pc (b, pspace, pc, section));
6877 if (others > 0)
6878 {
6879 if (others == 1)
6880 printf_filtered (_("Note: breakpoint "));
6881 else /* if (others == ???) */
6882 printf_filtered (_("Note: breakpoints "));
6883 ALL_BREAKPOINTS (b)
6884 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
6885 {
6886 others--;
6887 printf_filtered ("%d", b->number);
6888 if (b->thread == -1 && thread != -1)
6889 printf_filtered (" (all threads)");
6890 else if (b->thread != -1)
6891 printf_filtered (" (thread %d)", b->thread);
6892 printf_filtered ("%s%s ",
6893 ((b->enable_state == bp_disabled
6894 || b->enable_state == bp_call_disabled)
6895 ? " (disabled)"
6896 : ""),
6897 (others > 1) ? ","
6898 : ((others == 1) ? " and" : ""));
6899 }
6900 printf_filtered (_("also set at pc "));
6901 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
6902 printf_filtered (".\n");
6903 }
6904 }
6905 \f
6906
6907 /* Return true iff it is meaningful to use the address member of
6908 BPT. For some breakpoint types, the address member is irrelevant
6909 and it makes no sense to attempt to compare it to other addresses
6910 (or use it for any other purpose either).
6911
6912 More specifically, each of the following breakpoint types will
6913 always have a zero valued address and we don't want to mark
6914 breakpoints of any of these types to be a duplicate of an actual
6915 breakpoint at address zero:
6916
6917 bp_watchpoint
6918 bp_catchpoint
6919
6920 */
6921
6922 static int
6923 breakpoint_address_is_meaningful (struct breakpoint *bpt)
6924 {
6925 enum bptype type = bpt->type;
6926
6927 return (type != bp_watchpoint && type != bp_catchpoint);
6928 }
6929
6930 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6931 true if LOC1 and LOC2 represent the same watchpoint location. */
6932
6933 static int
6934 watchpoint_locations_match (struct bp_location *loc1,
6935 struct bp_location *loc2)
6936 {
6937 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6938 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6939
6940 /* Both of them must exist. */
6941 gdb_assert (w1 != NULL);
6942 gdb_assert (w2 != NULL);
6943
6944 /* If the target can evaluate the condition expression in hardware,
6945 then we we need to insert both watchpoints even if they are at
6946 the same place. Otherwise the watchpoint will only trigger when
6947 the condition of whichever watchpoint was inserted evaluates to
6948 true, not giving a chance for GDB to check the condition of the
6949 other watchpoint. */
6950 if ((w1->cond_exp
6951 && target_can_accel_watchpoint_condition (loc1->address,
6952 loc1->length,
6953 loc1->watchpoint_type,
6954 w1->cond_exp))
6955 || (w2->cond_exp
6956 && target_can_accel_watchpoint_condition (loc2->address,
6957 loc2->length,
6958 loc2->watchpoint_type,
6959 w2->cond_exp)))
6960 return 0;
6961
6962 /* Note that this checks the owner's type, not the location's. In
6963 case the target does not support read watchpoints, but does
6964 support access watchpoints, we'll have bp_read_watchpoint
6965 watchpoints with hw_access locations. Those should be considered
6966 duplicates of hw_read locations. The hw_read locations will
6967 become hw_access locations later. */
6968 return (loc1->owner->type == loc2->owner->type
6969 && loc1->pspace->aspace == loc2->pspace->aspace
6970 && loc1->address == loc2->address
6971 && loc1->length == loc2->length);
6972 }
6973
6974 /* See breakpoint.h. */
6975
6976 int
6977 breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
6978 struct address_space *aspace2, CORE_ADDR addr2)
6979 {
6980 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6981 || aspace1 == aspace2)
6982 && addr1 == addr2);
6983 }
6984
6985 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6986 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6987 matches ASPACE2. On targets that have global breakpoints, the address
6988 space doesn't really matter. */
6989
6990 static int
6991 breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
6992 int len1, struct address_space *aspace2,
6993 CORE_ADDR addr2)
6994 {
6995 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6996 || aspace1 == aspace2)
6997 && addr2 >= addr1 && addr2 < addr1 + len1);
6998 }
6999
7000 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
7001 a ranged breakpoint. In most targets, a match happens only if ASPACE
7002 matches the breakpoint's address space. On targets that have global
7003 breakpoints, the address space doesn't really matter. */
7004
7005 static int
7006 breakpoint_location_address_match (struct bp_location *bl,
7007 struct address_space *aspace,
7008 CORE_ADDR addr)
7009 {
7010 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
7011 aspace, addr)
7012 || (bl->length
7013 && breakpoint_address_match_range (bl->pspace->aspace,
7014 bl->address, bl->length,
7015 aspace, addr)));
7016 }
7017
7018 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
7019 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
7020 true, otherwise returns false. */
7021
7022 static int
7023 tracepoint_locations_match (struct bp_location *loc1,
7024 struct bp_location *loc2)
7025 {
7026 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
7027 /* Since tracepoint locations are never duplicated with others', tracepoint
7028 locations at the same address of different tracepoints are regarded as
7029 different locations. */
7030 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7031 else
7032 return 0;
7033 }
7034
7035 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
7036 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
7037 represent the same location. */
7038
7039 static int
7040 breakpoint_locations_match (struct bp_location *loc1,
7041 struct bp_location *loc2)
7042 {
7043 int hw_point1, hw_point2;
7044
7045 /* Both of them must not be in moribund_locations. */
7046 gdb_assert (loc1->owner != NULL);
7047 gdb_assert (loc2->owner != NULL);
7048
7049 hw_point1 = is_hardware_watchpoint (loc1->owner);
7050 hw_point2 = is_hardware_watchpoint (loc2->owner);
7051
7052 if (hw_point1 != hw_point2)
7053 return 0;
7054 else if (hw_point1)
7055 return watchpoint_locations_match (loc1, loc2);
7056 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7057 return tracepoint_locations_match (loc1, loc2);
7058 else
7059 /* We compare bp_location.length in order to cover ranged breakpoints. */
7060 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7061 loc2->pspace->aspace, loc2->address)
7062 && loc1->length == loc2->length);
7063 }
7064
7065 static void
7066 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7067 int bnum, int have_bnum)
7068 {
7069 /* The longest string possibly returned by hex_string_custom
7070 is 50 chars. These must be at least that big for safety. */
7071 char astr1[64];
7072 char astr2[64];
7073
7074 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7075 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
7076 if (have_bnum)
7077 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
7078 bnum, astr1, astr2);
7079 else
7080 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
7081 }
7082
7083 /* Adjust a breakpoint's address to account for architectural
7084 constraints on breakpoint placement. Return the adjusted address.
7085 Note: Very few targets require this kind of adjustment. For most
7086 targets, this function is simply the identity function. */
7087
7088 static CORE_ADDR
7089 adjust_breakpoint_address (struct gdbarch *gdbarch,
7090 CORE_ADDR bpaddr, enum bptype bptype)
7091 {
7092 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
7093 {
7094 /* Very few targets need any kind of breakpoint adjustment. */
7095 return bpaddr;
7096 }
7097 else if (bptype == bp_watchpoint
7098 || bptype == bp_hardware_watchpoint
7099 || bptype == bp_read_watchpoint
7100 || bptype == bp_access_watchpoint
7101 || bptype == bp_catchpoint)
7102 {
7103 /* Watchpoints and the various bp_catch_* eventpoints should not
7104 have their addresses modified. */
7105 return bpaddr;
7106 }
7107 else if (bptype == bp_single_step)
7108 {
7109 /* Single-step breakpoints should not have their addresses
7110 modified. If there's any architectural constrain that
7111 applies to this address, then it should have already been
7112 taken into account when the breakpoint was created in the
7113 first place. If we didn't do this, stepping through e.g.,
7114 Thumb-2 IT blocks would break. */
7115 return bpaddr;
7116 }
7117 else
7118 {
7119 CORE_ADDR adjusted_bpaddr;
7120
7121 /* Some targets have architectural constraints on the placement
7122 of breakpoint instructions. Obtain the adjusted address. */
7123 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
7124
7125 /* An adjusted breakpoint address can significantly alter
7126 a user's expectations. Print a warning if an adjustment
7127 is required. */
7128 if (adjusted_bpaddr != bpaddr)
7129 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7130
7131 return adjusted_bpaddr;
7132 }
7133 }
7134
7135 void
7136 init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
7137 struct breakpoint *owner)
7138 {
7139 memset (loc, 0, sizeof (*loc));
7140
7141 gdb_assert (ops != NULL);
7142
7143 loc->ops = ops;
7144 loc->owner = owner;
7145 loc->cond = NULL;
7146 loc->cond_bytecode = NULL;
7147 loc->shlib_disabled = 0;
7148 loc->enabled = 1;
7149
7150 switch (owner->type)
7151 {
7152 case bp_breakpoint:
7153 case bp_single_step:
7154 case bp_until:
7155 case bp_finish:
7156 case bp_longjmp:
7157 case bp_longjmp_resume:
7158 case bp_longjmp_call_dummy:
7159 case bp_exception:
7160 case bp_exception_resume:
7161 case bp_step_resume:
7162 case bp_hp_step_resume:
7163 case bp_watchpoint_scope:
7164 case bp_call_dummy:
7165 case bp_std_terminate:
7166 case bp_shlib_event:
7167 case bp_thread_event:
7168 case bp_overlay_event:
7169 case bp_jit_event:
7170 case bp_longjmp_master:
7171 case bp_std_terminate_master:
7172 case bp_exception_master:
7173 case bp_gnu_ifunc_resolver:
7174 case bp_gnu_ifunc_resolver_return:
7175 case bp_dprintf:
7176 loc->loc_type = bp_loc_software_breakpoint;
7177 mark_breakpoint_location_modified (loc);
7178 break;
7179 case bp_hardware_breakpoint:
7180 loc->loc_type = bp_loc_hardware_breakpoint;
7181 mark_breakpoint_location_modified (loc);
7182 break;
7183 case bp_hardware_watchpoint:
7184 case bp_read_watchpoint:
7185 case bp_access_watchpoint:
7186 loc->loc_type = bp_loc_hardware_watchpoint;
7187 break;
7188 case bp_watchpoint:
7189 case bp_catchpoint:
7190 case bp_tracepoint:
7191 case bp_fast_tracepoint:
7192 case bp_static_tracepoint:
7193 loc->loc_type = bp_loc_other;
7194 break;
7195 default:
7196 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
7197 }
7198
7199 loc->refc = 1;
7200 }
7201
7202 /* Allocate a struct bp_location. */
7203
7204 static struct bp_location *
7205 allocate_bp_location (struct breakpoint *bpt)
7206 {
7207 return bpt->ops->allocate_location (bpt);
7208 }
7209
7210 static void
7211 free_bp_location (struct bp_location *loc)
7212 {
7213 loc->ops->dtor (loc);
7214 xfree (loc);
7215 }
7216
7217 /* Increment reference count. */
7218
7219 static void
7220 incref_bp_location (struct bp_location *bl)
7221 {
7222 ++bl->refc;
7223 }
7224
7225 /* Decrement reference count. If the reference count reaches 0,
7226 destroy the bp_location. Sets *BLP to NULL. */
7227
7228 static void
7229 decref_bp_location (struct bp_location **blp)
7230 {
7231 gdb_assert ((*blp)->refc > 0);
7232
7233 if (--(*blp)->refc == 0)
7234 free_bp_location (*blp);
7235 *blp = NULL;
7236 }
7237
7238 /* Add breakpoint B at the end of the global breakpoint chain. */
7239
7240 static void
7241 add_to_breakpoint_chain (struct breakpoint *b)
7242 {
7243 struct breakpoint *b1;
7244
7245 /* Add this breakpoint to the end of the chain so that a list of
7246 breakpoints will come out in order of increasing numbers. */
7247
7248 b1 = breakpoint_chain;
7249 if (b1 == 0)
7250 breakpoint_chain = b;
7251 else
7252 {
7253 while (b1->next)
7254 b1 = b1->next;
7255 b1->next = b;
7256 }
7257 }
7258
7259 /* Initializes breakpoint B with type BPTYPE and no locations yet. */
7260
7261 static void
7262 init_raw_breakpoint_without_location (struct breakpoint *b,
7263 struct gdbarch *gdbarch,
7264 enum bptype bptype,
7265 const struct breakpoint_ops *ops)
7266 {
7267 memset (b, 0, sizeof (*b));
7268
7269 gdb_assert (ops != NULL);
7270
7271 b->ops = ops;
7272 b->type = bptype;
7273 b->gdbarch = gdbarch;
7274 b->language = current_language->la_language;
7275 b->input_radix = input_radix;
7276 b->thread = -1;
7277 b->enable_state = bp_enabled;
7278 b->next = 0;
7279 b->silent = 0;
7280 b->ignore_count = 0;
7281 b->commands = NULL;
7282 b->frame_id = null_frame_id;
7283 b->condition_not_parsed = 0;
7284 b->py_bp_object = NULL;
7285 b->related_breakpoint = b;
7286 }
7287
7288 /* Helper to set_raw_breakpoint below. Creates a breakpoint
7289 that has type BPTYPE and has no locations as yet. */
7290
7291 static struct breakpoint *
7292 set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
7293 enum bptype bptype,
7294 const struct breakpoint_ops *ops)
7295 {
7296 struct breakpoint *b = XNEW (struct breakpoint);
7297
7298 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7299 add_to_breakpoint_chain (b);
7300 return b;
7301 }
7302
7303 /* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7304 resolutions should be made as the user specified the location explicitly
7305 enough. */
7306
7307 static void
7308 set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
7309 {
7310 gdb_assert (loc->owner != NULL);
7311
7312 if (loc->owner->type == bp_breakpoint
7313 || loc->owner->type == bp_hardware_breakpoint
7314 || is_tracepoint (loc->owner))
7315 {
7316 int is_gnu_ifunc;
7317 const char *function_name;
7318 CORE_ADDR func_addr;
7319
7320 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
7321 &func_addr, NULL, &is_gnu_ifunc);
7322
7323 if (is_gnu_ifunc && !explicit_loc)
7324 {
7325 struct breakpoint *b = loc->owner;
7326
7327 gdb_assert (loc->pspace == current_program_space);
7328 if (gnu_ifunc_resolve_name (function_name,
7329 &loc->requested_address))
7330 {
7331 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7332 loc->address = adjust_breakpoint_address (loc->gdbarch,
7333 loc->requested_address,
7334 b->type);
7335 }
7336 else if (b->type == bp_breakpoint && b->loc == loc
7337 && loc->next == NULL && b->related_breakpoint == b)
7338 {
7339 /* Create only the whole new breakpoint of this type but do not
7340 mess more complicated breakpoints with multiple locations. */
7341 b->type = bp_gnu_ifunc_resolver;
7342 /* Remember the resolver's address for use by the return
7343 breakpoint. */
7344 loc->related_address = func_addr;
7345 }
7346 }
7347
7348 if (function_name)
7349 loc->function_name = xstrdup (function_name);
7350 }
7351 }
7352
7353 /* Attempt to determine architecture of location identified by SAL. */
7354 struct gdbarch *
7355 get_sal_arch (struct symtab_and_line sal)
7356 {
7357 if (sal.section)
7358 return get_objfile_arch (sal.section->objfile);
7359 if (sal.symtab)
7360 return get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
7361
7362 return NULL;
7363 }
7364
7365 /* Low level routine for partially initializing a breakpoint of type
7366 BPTYPE. The newly created breakpoint's address, section, source
7367 file name, and line number are provided by SAL.
7368
7369 It is expected that the caller will complete the initialization of
7370 the newly created breakpoint struct as well as output any status
7371 information regarding the creation of a new breakpoint. */
7372
7373 static void
7374 init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
7375 struct symtab_and_line sal, enum bptype bptype,
7376 const struct breakpoint_ops *ops)
7377 {
7378 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7379
7380 add_location_to_breakpoint (b, &sal);
7381
7382 if (bptype != bp_catchpoint)
7383 gdb_assert (sal.pspace != NULL);
7384
7385 /* Store the program space that was used to set the breakpoint,
7386 except for ordinary breakpoints, which are independent of the
7387 program space. */
7388 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7389 b->pspace = sal.pspace;
7390 }
7391
7392 /* set_raw_breakpoint is a low level routine for allocating and
7393 partially initializing a breakpoint of type BPTYPE. The newly
7394 created breakpoint's address, section, source file name, and line
7395 number are provided by SAL. The newly created and partially
7396 initialized breakpoint is added to the breakpoint chain and
7397 is also returned as the value of this function.
7398
7399 It is expected that the caller will complete the initialization of
7400 the newly created breakpoint struct as well as output any status
7401 information regarding the creation of a new breakpoint. In
7402 particular, set_raw_breakpoint does NOT set the breakpoint
7403 number! Care should be taken to not allow an error to occur
7404 prior to completing the initialization of the breakpoint. If this
7405 should happen, a bogus breakpoint will be left on the chain. */
7406
7407 struct breakpoint *
7408 set_raw_breakpoint (struct gdbarch *gdbarch,
7409 struct symtab_and_line sal, enum bptype bptype,
7410 const struct breakpoint_ops *ops)
7411 {
7412 struct breakpoint *b = XNEW (struct breakpoint);
7413
7414 init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
7415 add_to_breakpoint_chain (b);
7416 return b;
7417 }
7418
7419
7420 /* Note that the breakpoint object B describes a permanent breakpoint
7421 instruction, hard-wired into the inferior's code. */
7422 void
7423 make_breakpoint_permanent (struct breakpoint *b)
7424 {
7425 struct bp_location *bl;
7426
7427 /* By definition, permanent breakpoints are already present in the
7428 code. Mark all locations as inserted. For now,
7429 make_breakpoint_permanent is called in just one place, so it's
7430 hard to say if it's reasonable to have permanent breakpoint with
7431 multiple locations or not, but it's easy to implement. */
7432 for (bl = b->loc; bl; bl = bl->next)
7433 {
7434 bl->permanent = 1;
7435 bl->inserted = 1;
7436 }
7437 }
7438
7439 /* Call this routine when stepping and nexting to enable a breakpoint
7440 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7441 initiated the operation. */
7442
7443 void
7444 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
7445 {
7446 struct breakpoint *b, *b_tmp;
7447 int thread = tp->num;
7448
7449 /* To avoid having to rescan all objfile symbols at every step,
7450 we maintain a list of continually-inserted but always disabled
7451 longjmp "master" breakpoints. Here, we simply create momentary
7452 clones of those and enable them for the requested thread. */
7453 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7454 if (b->pspace == current_program_space
7455 && (b->type == bp_longjmp_master
7456 || b->type == bp_exception_master))
7457 {
7458 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7459 struct breakpoint *clone;
7460
7461 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7462 after their removal. */
7463 clone = momentary_breakpoint_from_master (b, type,
7464 &longjmp_breakpoint_ops, 1);
7465 clone->thread = thread;
7466 }
7467
7468 tp->initiating_frame = frame;
7469 }
7470
7471 /* Delete all longjmp breakpoints from THREAD. */
7472 void
7473 delete_longjmp_breakpoint (int thread)
7474 {
7475 struct breakpoint *b, *b_tmp;
7476
7477 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7478 if (b->type == bp_longjmp || b->type == bp_exception)
7479 {
7480 if (b->thread == thread)
7481 delete_breakpoint (b);
7482 }
7483 }
7484
7485 void
7486 delete_longjmp_breakpoint_at_next_stop (int thread)
7487 {
7488 struct breakpoint *b, *b_tmp;
7489
7490 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7491 if (b->type == bp_longjmp || b->type == bp_exception)
7492 {
7493 if (b->thread == thread)
7494 b->disposition = disp_del_at_next_stop;
7495 }
7496 }
7497
7498 /* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7499 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7500 pointer to any of them. Return NULL if this system cannot place longjmp
7501 breakpoints. */
7502
7503 struct breakpoint *
7504 set_longjmp_breakpoint_for_call_dummy (void)
7505 {
7506 struct breakpoint *b, *retval = NULL;
7507
7508 ALL_BREAKPOINTS (b)
7509 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7510 {
7511 struct breakpoint *new_b;
7512
7513 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7514 &momentary_breakpoint_ops,
7515 1);
7516 new_b->thread = pid_to_thread_id (inferior_ptid);
7517
7518 /* Link NEW_B into the chain of RETVAL breakpoints. */
7519
7520 gdb_assert (new_b->related_breakpoint == new_b);
7521 if (retval == NULL)
7522 retval = new_b;
7523 new_b->related_breakpoint = retval;
7524 while (retval->related_breakpoint != new_b->related_breakpoint)
7525 retval = retval->related_breakpoint;
7526 retval->related_breakpoint = new_b;
7527 }
7528
7529 return retval;
7530 }
7531
7532 /* Verify all existing dummy frames and their associated breakpoints for
7533 TP. Remove those which can no longer be found in the current frame
7534 stack.
7535
7536 You should call this function only at places where it is safe to currently
7537 unwind the whole stack. Failed stack unwind would discard live dummy
7538 frames. */
7539
7540 void
7541 check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
7542 {
7543 struct breakpoint *b, *b_tmp;
7544
7545 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7546 if (b->type == bp_longjmp_call_dummy && b->thread == tp->num)
7547 {
7548 struct breakpoint *dummy_b = b->related_breakpoint;
7549
7550 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7551 dummy_b = dummy_b->related_breakpoint;
7552 if (dummy_b->type != bp_call_dummy
7553 || frame_find_by_id (dummy_b->frame_id) != NULL)
7554 continue;
7555
7556 dummy_frame_discard (dummy_b->frame_id, tp->ptid);
7557
7558 while (b->related_breakpoint != b)
7559 {
7560 if (b_tmp == b->related_breakpoint)
7561 b_tmp = b->related_breakpoint->next;
7562 delete_breakpoint (b->related_breakpoint);
7563 }
7564 delete_breakpoint (b);
7565 }
7566 }
7567
7568 void
7569 enable_overlay_breakpoints (void)
7570 {
7571 struct breakpoint *b;
7572
7573 ALL_BREAKPOINTS (b)
7574 if (b->type == bp_overlay_event)
7575 {
7576 b->enable_state = bp_enabled;
7577 update_global_location_list (UGLL_MAY_INSERT);
7578 overlay_events_enabled = 1;
7579 }
7580 }
7581
7582 void
7583 disable_overlay_breakpoints (void)
7584 {
7585 struct breakpoint *b;
7586
7587 ALL_BREAKPOINTS (b)
7588 if (b->type == bp_overlay_event)
7589 {
7590 b->enable_state = bp_disabled;
7591 update_global_location_list (UGLL_DONT_INSERT);
7592 overlay_events_enabled = 0;
7593 }
7594 }
7595
7596 /* Set an active std::terminate breakpoint for each std::terminate
7597 master breakpoint. */
7598 void
7599 set_std_terminate_breakpoint (void)
7600 {
7601 struct breakpoint *b, *b_tmp;
7602
7603 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7604 if (b->pspace == current_program_space
7605 && b->type == bp_std_terminate_master)
7606 {
7607 momentary_breakpoint_from_master (b, bp_std_terminate,
7608 &momentary_breakpoint_ops, 1);
7609 }
7610 }
7611
7612 /* Delete all the std::terminate breakpoints. */
7613 void
7614 delete_std_terminate_breakpoint (void)
7615 {
7616 struct breakpoint *b, *b_tmp;
7617
7618 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7619 if (b->type == bp_std_terminate)
7620 delete_breakpoint (b);
7621 }
7622
7623 struct breakpoint *
7624 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7625 {
7626 struct breakpoint *b;
7627
7628 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7629 &internal_breakpoint_ops);
7630
7631 b->enable_state = bp_enabled;
7632 /* addr_string has to be used or breakpoint_re_set will delete me. */
7633 b->addr_string
7634 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
7635
7636 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7637
7638 return b;
7639 }
7640
7641 void
7642 remove_thread_event_breakpoints (void)
7643 {
7644 struct breakpoint *b, *b_tmp;
7645
7646 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7647 if (b->type == bp_thread_event
7648 && b->loc->pspace == current_program_space)
7649 delete_breakpoint (b);
7650 }
7651
7652 struct lang_and_radix
7653 {
7654 enum language lang;
7655 int radix;
7656 };
7657
7658 /* Create a breakpoint for JIT code registration and unregistration. */
7659
7660 struct breakpoint *
7661 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7662 {
7663 struct breakpoint *b;
7664
7665 b = create_internal_breakpoint (gdbarch, address, bp_jit_event,
7666 &internal_breakpoint_ops);
7667 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7668 return b;
7669 }
7670
7671 /* Remove JIT code registration and unregistration breakpoint(s). */
7672
7673 void
7674 remove_jit_event_breakpoints (void)
7675 {
7676 struct breakpoint *b, *b_tmp;
7677
7678 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7679 if (b->type == bp_jit_event
7680 && b->loc->pspace == current_program_space)
7681 delete_breakpoint (b);
7682 }
7683
7684 void
7685 remove_solib_event_breakpoints (void)
7686 {
7687 struct breakpoint *b, *b_tmp;
7688
7689 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7690 if (b->type == bp_shlib_event
7691 && b->loc->pspace == current_program_space)
7692 delete_breakpoint (b);
7693 }
7694
7695 /* See breakpoint.h. */
7696
7697 void
7698 remove_solib_event_breakpoints_at_next_stop (void)
7699 {
7700 struct breakpoint *b, *b_tmp;
7701
7702 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7703 if (b->type == bp_shlib_event
7704 && b->loc->pspace == current_program_space)
7705 b->disposition = disp_del_at_next_stop;
7706 }
7707
7708 /* Helper for create_solib_event_breakpoint /
7709 create_and_insert_solib_event_breakpoint. Allows specifying which
7710 INSERT_MODE to pass through to update_global_location_list. */
7711
7712 static struct breakpoint *
7713 create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7714 enum ugll_insert_mode insert_mode)
7715 {
7716 struct breakpoint *b;
7717
7718 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7719 &internal_breakpoint_ops);
7720 update_global_location_list_nothrow (insert_mode);
7721 return b;
7722 }
7723
7724 struct breakpoint *
7725 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7726 {
7727 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7728 }
7729
7730 /* See breakpoint.h. */
7731
7732 struct breakpoint *
7733 create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7734 {
7735 struct breakpoint *b;
7736
7737 /* Explicitly tell update_global_location_list to insert
7738 locations. */
7739 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
7740 if (!b->loc->inserted)
7741 {
7742 delete_breakpoint (b);
7743 return NULL;
7744 }
7745 return b;
7746 }
7747
7748 /* Disable any breakpoints that are on code in shared libraries. Only
7749 apply to enabled breakpoints, disabled ones can just stay disabled. */
7750
7751 void
7752 disable_breakpoints_in_shlibs (void)
7753 {
7754 struct bp_location *loc, **locp_tmp;
7755
7756 ALL_BP_LOCATIONS (loc, locp_tmp)
7757 {
7758 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7759 struct breakpoint *b = loc->owner;
7760
7761 /* We apply the check to all breakpoints, including disabled for
7762 those with loc->duplicate set. This is so that when breakpoint
7763 becomes enabled, or the duplicate is removed, gdb will try to
7764 insert all breakpoints. If we don't set shlib_disabled here,
7765 we'll try to insert those breakpoints and fail. */
7766 if (((b->type == bp_breakpoint)
7767 || (b->type == bp_jit_event)
7768 || (b->type == bp_hardware_breakpoint)
7769 || (is_tracepoint (b)))
7770 && loc->pspace == current_program_space
7771 && !loc->shlib_disabled
7772 && solib_name_from_address (loc->pspace, loc->address)
7773 )
7774 {
7775 loc->shlib_disabled = 1;
7776 }
7777 }
7778 }
7779
7780 /* Disable any breakpoints and tracepoints that are in SOLIB upon
7781 notification of unloaded_shlib. Only apply to enabled breakpoints,
7782 disabled ones can just stay disabled. */
7783
7784 static void
7785 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7786 {
7787 struct bp_location *loc, **locp_tmp;
7788 int disabled_shlib_breaks = 0;
7789
7790 /* SunOS a.out shared libraries are always mapped, so do not
7791 disable breakpoints; they will only be reported as unloaded
7792 through clear_solib when GDB discards its shared library
7793 list. See clear_solib for more information. */
7794 if (exec_bfd != NULL
7795 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
7796 return;
7797
7798 ALL_BP_LOCATIONS (loc, locp_tmp)
7799 {
7800 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7801 struct breakpoint *b = loc->owner;
7802
7803 if (solib->pspace == loc->pspace
7804 && !loc->shlib_disabled
7805 && (((b->type == bp_breakpoint
7806 || b->type == bp_jit_event
7807 || b->type == bp_hardware_breakpoint)
7808 && (loc->loc_type == bp_loc_hardware_breakpoint
7809 || loc->loc_type == bp_loc_software_breakpoint))
7810 || is_tracepoint (b))
7811 && solib_contains_address_p (solib, loc->address))
7812 {
7813 loc->shlib_disabled = 1;
7814 /* At this point, we cannot rely on remove_breakpoint
7815 succeeding so we must mark the breakpoint as not inserted
7816 to prevent future errors occurring in remove_breakpoints. */
7817 loc->inserted = 0;
7818
7819 /* This may cause duplicate notifications for the same breakpoint. */
7820 observer_notify_breakpoint_modified (b);
7821
7822 if (!disabled_shlib_breaks)
7823 {
7824 target_terminal_ours_for_output ();
7825 warning (_("Temporarily disabling breakpoints "
7826 "for unloaded shared library \"%s\""),
7827 solib->so_name);
7828 }
7829 disabled_shlib_breaks = 1;
7830 }
7831 }
7832 }
7833
7834 /* Disable any breakpoints and tracepoints in OBJFILE upon
7835 notification of free_objfile. Only apply to enabled breakpoints,
7836 disabled ones can just stay disabled. */
7837
7838 static void
7839 disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7840 {
7841 struct breakpoint *b;
7842
7843 if (objfile == NULL)
7844 return;
7845
7846 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7847 managed by the user with add-symbol-file/remove-symbol-file.
7848 Similarly to how breakpoints in shared libraries are handled in
7849 response to "nosharedlibrary", mark breakpoints in such modules
7850 shlib_disabled so they end up uninserted on the next global
7851 location list update. Shared libraries not loaded by the user
7852 aren't handled here -- they're already handled in
7853 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7854 solib_unloaded observer. We skip objfiles that are not
7855 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7856 main objfile). */
7857 if ((objfile->flags & OBJF_SHARED) == 0
7858 || (objfile->flags & OBJF_USERLOADED) == 0)
7859 return;
7860
7861 ALL_BREAKPOINTS (b)
7862 {
7863 struct bp_location *loc;
7864 int bp_modified = 0;
7865
7866 if (!is_breakpoint (b) && !is_tracepoint (b))
7867 continue;
7868
7869 for (loc = b->loc; loc != NULL; loc = loc->next)
7870 {
7871 CORE_ADDR loc_addr = loc->address;
7872
7873 if (loc->loc_type != bp_loc_hardware_breakpoint
7874 && loc->loc_type != bp_loc_software_breakpoint)
7875 continue;
7876
7877 if (loc->shlib_disabled != 0)
7878 continue;
7879
7880 if (objfile->pspace != loc->pspace)
7881 continue;
7882
7883 if (loc->loc_type != bp_loc_hardware_breakpoint
7884 && loc->loc_type != bp_loc_software_breakpoint)
7885 continue;
7886
7887 if (is_addr_in_objfile (loc_addr, objfile))
7888 {
7889 loc->shlib_disabled = 1;
7890 /* At this point, we don't know whether the object was
7891 unmapped from the inferior or not, so leave the
7892 inserted flag alone. We'll handle failure to
7893 uninsert quietly, in case the object was indeed
7894 unmapped. */
7895
7896 mark_breakpoint_location_modified (loc);
7897
7898 bp_modified = 1;
7899 }
7900 }
7901
7902 if (bp_modified)
7903 observer_notify_breakpoint_modified (b);
7904 }
7905 }
7906
7907 /* FORK & VFORK catchpoints. */
7908
7909 /* An instance of this type is used to represent a fork or vfork
7910 catchpoint. It includes a "struct breakpoint" as a kind of base
7911 class; users downcast to "struct breakpoint *" when needed. A
7912 breakpoint is really of this type iff its ops pointer points to
7913 CATCH_FORK_BREAKPOINT_OPS. */
7914
7915 struct fork_catchpoint
7916 {
7917 /* The base class. */
7918 struct breakpoint base;
7919
7920 /* Process id of a child process whose forking triggered this
7921 catchpoint. This field is only valid immediately after this
7922 catchpoint has triggered. */
7923 ptid_t forked_inferior_pid;
7924 };
7925
7926 /* Implement the "insert" breakpoint_ops method for fork
7927 catchpoints. */
7928
7929 static int
7930 insert_catch_fork (struct bp_location *bl)
7931 {
7932 return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
7933 }
7934
7935 /* Implement the "remove" breakpoint_ops method for fork
7936 catchpoints. */
7937
7938 static int
7939 remove_catch_fork (struct bp_location *bl)
7940 {
7941 return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
7942 }
7943
7944 /* Implement the "breakpoint_hit" breakpoint_ops method for fork
7945 catchpoints. */
7946
7947 static int
7948 breakpoint_hit_catch_fork (const struct bp_location *bl,
7949 struct address_space *aspace, CORE_ADDR bp_addr,
7950 const struct target_waitstatus *ws)
7951 {
7952 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7953
7954 if (ws->kind != TARGET_WAITKIND_FORKED)
7955 return 0;
7956
7957 c->forked_inferior_pid = ws->value.related_pid;
7958 return 1;
7959 }
7960
7961 /* Implement the "print_it" breakpoint_ops method for fork
7962 catchpoints. */
7963
7964 static enum print_stop_action
7965 print_it_catch_fork (bpstat bs)
7966 {
7967 struct ui_out *uiout = current_uiout;
7968 struct breakpoint *b = bs->breakpoint_at;
7969 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
7970
7971 annotate_catchpoint (b->number);
7972 if (b->disposition == disp_del)
7973 ui_out_text (uiout, "\nTemporary catchpoint ");
7974 else
7975 ui_out_text (uiout, "\nCatchpoint ");
7976 if (ui_out_is_mi_like_p (uiout))
7977 {
7978 ui_out_field_string (uiout, "reason",
7979 async_reason_lookup (EXEC_ASYNC_FORK));
7980 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7981 }
7982 ui_out_field_int (uiout, "bkptno", b->number);
7983 ui_out_text (uiout, " (forked process ");
7984 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
7985 ui_out_text (uiout, "), ");
7986 return PRINT_SRC_AND_LOC;
7987 }
7988
7989 /* Implement the "print_one" breakpoint_ops method for fork
7990 catchpoints. */
7991
7992 static void
7993 print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
7994 {
7995 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7996 struct value_print_options opts;
7997 struct ui_out *uiout = current_uiout;
7998
7999 get_user_print_options (&opts);
8000
8001 /* Field 4, the address, is omitted (which makes the columns not
8002 line up too nicely with the headers, but the effect is relatively
8003 readable). */
8004 if (opts.addressprint)
8005 ui_out_field_skip (uiout, "addr");
8006 annotate_field (5);
8007 ui_out_text (uiout, "fork");
8008 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
8009 {
8010 ui_out_text (uiout, ", process ");
8011 ui_out_field_int (uiout, "what",
8012 ptid_get_pid (c->forked_inferior_pid));
8013 ui_out_spaces (uiout, 1);
8014 }
8015
8016 if (ui_out_is_mi_like_p (uiout))
8017 ui_out_field_string (uiout, "catch-type", "fork");
8018 }
8019
8020 /* Implement the "print_mention" breakpoint_ops method for fork
8021 catchpoints. */
8022
8023 static void
8024 print_mention_catch_fork (struct breakpoint *b)
8025 {
8026 printf_filtered (_("Catchpoint %d (fork)"), b->number);
8027 }
8028
8029 /* Implement the "print_recreate" breakpoint_ops method for fork
8030 catchpoints. */
8031
8032 static void
8033 print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
8034 {
8035 fprintf_unfiltered (fp, "catch fork");
8036 print_recreate_thread (b, fp);
8037 }
8038
8039 /* The breakpoint_ops structure to be used in fork catchpoints. */
8040
8041 static struct breakpoint_ops catch_fork_breakpoint_ops;
8042
8043 /* Implement the "insert" breakpoint_ops method for vfork
8044 catchpoints. */
8045
8046 static int
8047 insert_catch_vfork (struct bp_location *bl)
8048 {
8049 return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
8050 }
8051
8052 /* Implement the "remove" breakpoint_ops method for vfork
8053 catchpoints. */
8054
8055 static int
8056 remove_catch_vfork (struct bp_location *bl)
8057 {
8058 return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
8059 }
8060
8061 /* Implement the "breakpoint_hit" breakpoint_ops method for vfork
8062 catchpoints. */
8063
8064 static int
8065 breakpoint_hit_catch_vfork (const struct bp_location *bl,
8066 struct address_space *aspace, CORE_ADDR bp_addr,
8067 const struct target_waitstatus *ws)
8068 {
8069 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8070
8071 if (ws->kind != TARGET_WAITKIND_VFORKED)
8072 return 0;
8073
8074 c->forked_inferior_pid = ws->value.related_pid;
8075 return 1;
8076 }
8077
8078 /* Implement the "print_it" breakpoint_ops method for vfork
8079 catchpoints. */
8080
8081 static enum print_stop_action
8082 print_it_catch_vfork (bpstat bs)
8083 {
8084 struct ui_out *uiout = current_uiout;
8085 struct breakpoint *b = bs->breakpoint_at;
8086 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8087
8088 annotate_catchpoint (b->number);
8089 if (b->disposition == disp_del)
8090 ui_out_text (uiout, "\nTemporary catchpoint ");
8091 else
8092 ui_out_text (uiout, "\nCatchpoint ");
8093 if (ui_out_is_mi_like_p (uiout))
8094 {
8095 ui_out_field_string (uiout, "reason",
8096 async_reason_lookup (EXEC_ASYNC_VFORK));
8097 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8098 }
8099 ui_out_field_int (uiout, "bkptno", b->number);
8100 ui_out_text (uiout, " (vforked process ");
8101 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
8102 ui_out_text (uiout, "), ");
8103 return PRINT_SRC_AND_LOC;
8104 }
8105
8106 /* Implement the "print_one" breakpoint_ops method for vfork
8107 catchpoints. */
8108
8109 static void
8110 print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
8111 {
8112 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8113 struct value_print_options opts;
8114 struct ui_out *uiout = current_uiout;
8115
8116 get_user_print_options (&opts);
8117 /* Field 4, the address, is omitted (which makes the columns not
8118 line up too nicely with the headers, but the effect is relatively
8119 readable). */
8120 if (opts.addressprint)
8121 ui_out_field_skip (uiout, "addr");
8122 annotate_field (5);
8123 ui_out_text (uiout, "vfork");
8124 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
8125 {
8126 ui_out_text (uiout, ", process ");
8127 ui_out_field_int (uiout, "what",
8128 ptid_get_pid (c->forked_inferior_pid));
8129 ui_out_spaces (uiout, 1);
8130 }
8131
8132 if (ui_out_is_mi_like_p (uiout))
8133 ui_out_field_string (uiout, "catch-type", "vfork");
8134 }
8135
8136 /* Implement the "print_mention" breakpoint_ops method for vfork
8137 catchpoints. */
8138
8139 static void
8140 print_mention_catch_vfork (struct breakpoint *b)
8141 {
8142 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
8143 }
8144
8145 /* Implement the "print_recreate" breakpoint_ops method for vfork
8146 catchpoints. */
8147
8148 static void
8149 print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8150 {
8151 fprintf_unfiltered (fp, "catch vfork");
8152 print_recreate_thread (b, fp);
8153 }
8154
8155 /* The breakpoint_ops structure to be used in vfork catchpoints. */
8156
8157 static struct breakpoint_ops catch_vfork_breakpoint_ops;
8158
8159 /* An instance of this type is used to represent an solib catchpoint.
8160 It includes a "struct breakpoint" as a kind of base class; users
8161 downcast to "struct breakpoint *" when needed. A breakpoint is
8162 really of this type iff its ops pointer points to
8163 CATCH_SOLIB_BREAKPOINT_OPS. */
8164
8165 struct solib_catchpoint
8166 {
8167 /* The base class. */
8168 struct breakpoint base;
8169
8170 /* True for "catch load", false for "catch unload". */
8171 unsigned char is_load;
8172
8173 /* Regular expression to match, if any. COMPILED is only valid when
8174 REGEX is non-NULL. */
8175 char *regex;
8176 regex_t compiled;
8177 };
8178
8179 static void
8180 dtor_catch_solib (struct breakpoint *b)
8181 {
8182 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8183
8184 if (self->regex)
8185 regfree (&self->compiled);
8186 xfree (self->regex);
8187
8188 base_breakpoint_ops.dtor (b);
8189 }
8190
8191 static int
8192 insert_catch_solib (struct bp_location *ignore)
8193 {
8194 return 0;
8195 }
8196
8197 static int
8198 remove_catch_solib (struct bp_location *ignore)
8199 {
8200 return 0;
8201 }
8202
8203 static int
8204 breakpoint_hit_catch_solib (const struct bp_location *bl,
8205 struct address_space *aspace,
8206 CORE_ADDR bp_addr,
8207 const struct target_waitstatus *ws)
8208 {
8209 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8210 struct breakpoint *other;
8211
8212 if (ws->kind == TARGET_WAITKIND_LOADED)
8213 return 1;
8214
8215 ALL_BREAKPOINTS (other)
8216 {
8217 struct bp_location *other_bl;
8218
8219 if (other == bl->owner)
8220 continue;
8221
8222 if (other->type != bp_shlib_event)
8223 continue;
8224
8225 if (self->base.pspace != NULL && other->pspace != self->base.pspace)
8226 continue;
8227
8228 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8229 {
8230 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8231 return 1;
8232 }
8233 }
8234
8235 return 0;
8236 }
8237
8238 static void
8239 check_status_catch_solib (struct bpstats *bs)
8240 {
8241 struct solib_catchpoint *self
8242 = (struct solib_catchpoint *) bs->breakpoint_at;
8243 int ix;
8244
8245 if (self->is_load)
8246 {
8247 struct so_list *iter;
8248
8249 for (ix = 0;
8250 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
8251 ix, iter);
8252 ++ix)
8253 {
8254 if (!self->regex
8255 || regexec (&self->compiled, iter->so_name, 0, NULL, 0) == 0)
8256 return;
8257 }
8258 }
8259 else
8260 {
8261 char *iter;
8262
8263 for (ix = 0;
8264 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
8265 ix, iter);
8266 ++ix)
8267 {
8268 if (!self->regex
8269 || regexec (&self->compiled, iter, 0, NULL, 0) == 0)
8270 return;
8271 }
8272 }
8273
8274 bs->stop = 0;
8275 bs->print_it = print_it_noop;
8276 }
8277
8278 static enum print_stop_action
8279 print_it_catch_solib (bpstat bs)
8280 {
8281 struct breakpoint *b = bs->breakpoint_at;
8282 struct ui_out *uiout = current_uiout;
8283
8284 annotate_catchpoint (b->number);
8285 if (b->disposition == disp_del)
8286 ui_out_text (uiout, "\nTemporary catchpoint ");
8287 else
8288 ui_out_text (uiout, "\nCatchpoint ");
8289 ui_out_field_int (uiout, "bkptno", b->number);
8290 ui_out_text (uiout, "\n");
8291 if (ui_out_is_mi_like_p (uiout))
8292 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8293 print_solib_event (1);
8294 return PRINT_SRC_AND_LOC;
8295 }
8296
8297 static void
8298 print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8299 {
8300 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8301 struct value_print_options opts;
8302 struct ui_out *uiout = current_uiout;
8303 char *msg;
8304
8305 get_user_print_options (&opts);
8306 /* Field 4, the address, is omitted (which makes the columns not
8307 line up too nicely with the headers, but the effect is relatively
8308 readable). */
8309 if (opts.addressprint)
8310 {
8311 annotate_field (4);
8312 ui_out_field_skip (uiout, "addr");
8313 }
8314
8315 annotate_field (5);
8316 if (self->is_load)
8317 {
8318 if (self->regex)
8319 msg = xstrprintf (_("load of library matching %s"), self->regex);
8320 else
8321 msg = xstrdup (_("load of library"));
8322 }
8323 else
8324 {
8325 if (self->regex)
8326 msg = xstrprintf (_("unload of library matching %s"), self->regex);
8327 else
8328 msg = xstrdup (_("unload of library"));
8329 }
8330 ui_out_field_string (uiout, "what", msg);
8331 xfree (msg);
8332
8333 if (ui_out_is_mi_like_p (uiout))
8334 ui_out_field_string (uiout, "catch-type",
8335 self->is_load ? "load" : "unload");
8336 }
8337
8338 static void
8339 print_mention_catch_solib (struct breakpoint *b)
8340 {
8341 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8342
8343 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8344 self->is_load ? "load" : "unload");
8345 }
8346
8347 static void
8348 print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8349 {
8350 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8351
8352 fprintf_unfiltered (fp, "%s %s",
8353 b->disposition == disp_del ? "tcatch" : "catch",
8354 self->is_load ? "load" : "unload");
8355 if (self->regex)
8356 fprintf_unfiltered (fp, " %s", self->regex);
8357 fprintf_unfiltered (fp, "\n");
8358 }
8359
8360 static struct breakpoint_ops catch_solib_breakpoint_ops;
8361
8362 /* Shared helper function (MI and CLI) for creating and installing
8363 a shared object event catchpoint. If IS_LOAD is non-zero then
8364 the events to be caught are load events, otherwise they are
8365 unload events. If IS_TEMP is non-zero the catchpoint is a
8366 temporary one. If ENABLED is non-zero the catchpoint is
8367 created in an enabled state. */
8368
8369 void
8370 add_solib_catchpoint (char *arg, int is_load, int is_temp, int enabled)
8371 {
8372 struct solib_catchpoint *c;
8373 struct gdbarch *gdbarch = get_current_arch ();
8374 struct cleanup *cleanup;
8375
8376 if (!arg)
8377 arg = "";
8378 arg = skip_spaces (arg);
8379
8380 c = XCNEW (struct solib_catchpoint);
8381 cleanup = make_cleanup (xfree, c);
8382
8383 if (*arg != '\0')
8384 {
8385 int errcode;
8386
8387 errcode = regcomp (&c->compiled, arg, REG_NOSUB);
8388 if (errcode != 0)
8389 {
8390 char *err = get_regcomp_error (errcode, &c->compiled);
8391
8392 make_cleanup (xfree, err);
8393 error (_("Invalid regexp (%s): %s"), err, arg);
8394 }
8395 c->regex = xstrdup (arg);
8396 }
8397
8398 c->is_load = is_load;
8399 init_catchpoint (&c->base, gdbarch, is_temp, NULL,
8400 &catch_solib_breakpoint_ops);
8401
8402 c->base.enable_state = enabled ? bp_enabled : bp_disabled;
8403
8404 discard_cleanups (cleanup);
8405 install_breakpoint (0, &c->base, 1);
8406 }
8407
8408 /* A helper function that does all the work for "catch load" and
8409 "catch unload". */
8410
8411 static void
8412 catch_load_or_unload (char *arg, int from_tty, int is_load,
8413 struct cmd_list_element *command)
8414 {
8415 int tempflag;
8416 const int enabled = 1;
8417
8418 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8419
8420 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8421 }
8422
8423 static void
8424 catch_load_command_1 (char *arg, int from_tty,
8425 struct cmd_list_element *command)
8426 {
8427 catch_load_or_unload (arg, from_tty, 1, command);
8428 }
8429
8430 static void
8431 catch_unload_command_1 (char *arg, int from_tty,
8432 struct cmd_list_element *command)
8433 {
8434 catch_load_or_unload (arg, from_tty, 0, command);
8435 }
8436
8437 /* An instance of this type is used to represent a syscall catchpoint.
8438 It includes a "struct breakpoint" as a kind of base class; users
8439 downcast to "struct breakpoint *" when needed. A breakpoint is
8440 really of this type iff its ops pointer points to
8441 CATCH_SYSCALL_BREAKPOINT_OPS. */
8442
8443 struct syscall_catchpoint
8444 {
8445 /* The base class. */
8446 struct breakpoint base;
8447
8448 /* Syscall numbers used for the 'catch syscall' feature. If no
8449 syscall has been specified for filtering, its value is NULL.
8450 Otherwise, it holds a list of all syscalls to be caught. The
8451 list elements are allocated with xmalloc. */
8452 VEC(int) *syscalls_to_be_caught;
8453 };
8454
8455 /* Implement the "dtor" breakpoint_ops method for syscall
8456 catchpoints. */
8457
8458 static void
8459 dtor_catch_syscall (struct breakpoint *b)
8460 {
8461 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8462
8463 VEC_free (int, c->syscalls_to_be_caught);
8464
8465 base_breakpoint_ops.dtor (b);
8466 }
8467
8468 static const struct inferior_data *catch_syscall_inferior_data = NULL;
8469
8470 struct catch_syscall_inferior_data
8471 {
8472 /* We keep a count of the number of times the user has requested a
8473 particular syscall to be tracked, and pass this information to the
8474 target. This lets capable targets implement filtering directly. */
8475
8476 /* Number of times that "any" syscall is requested. */
8477 int any_syscall_count;
8478
8479 /* Count of each system call. */
8480 VEC(int) *syscalls_counts;
8481
8482 /* This counts all syscall catch requests, so we can readily determine
8483 if any catching is necessary. */
8484 int total_syscalls_count;
8485 };
8486
8487 static struct catch_syscall_inferior_data*
8488 get_catch_syscall_inferior_data (struct inferior *inf)
8489 {
8490 struct catch_syscall_inferior_data *inf_data;
8491
8492 inf_data = inferior_data (inf, catch_syscall_inferior_data);
8493 if (inf_data == NULL)
8494 {
8495 inf_data = XCNEW (struct catch_syscall_inferior_data);
8496 set_inferior_data (inf, catch_syscall_inferior_data, inf_data);
8497 }
8498
8499 return inf_data;
8500 }
8501
8502 static void
8503 catch_syscall_inferior_data_cleanup (struct inferior *inf, void *arg)
8504 {
8505 xfree (arg);
8506 }
8507
8508
8509 /* Implement the "insert" breakpoint_ops method for syscall
8510 catchpoints. */
8511
8512 static int
8513 insert_catch_syscall (struct bp_location *bl)
8514 {
8515 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
8516 struct inferior *inf = current_inferior ();
8517 struct catch_syscall_inferior_data *inf_data
8518 = get_catch_syscall_inferior_data (inf);
8519
8520 ++inf_data->total_syscalls_count;
8521 if (!c->syscalls_to_be_caught)
8522 ++inf_data->any_syscall_count;
8523 else
8524 {
8525 int i, iter;
8526
8527 for (i = 0;
8528 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
8529 i++)
8530 {
8531 int elem;
8532
8533 if (iter >= VEC_length (int, inf_data->syscalls_counts))
8534 {
8535 int old_size = VEC_length (int, inf_data->syscalls_counts);
8536 uintptr_t vec_addr_offset
8537 = old_size * ((uintptr_t) sizeof (int));
8538 uintptr_t vec_addr;
8539 VEC_safe_grow (int, inf_data->syscalls_counts, iter + 1);
8540 vec_addr = ((uintptr_t) VEC_address (int,
8541 inf_data->syscalls_counts)
8542 + vec_addr_offset);
8543 memset ((void *) vec_addr, 0,
8544 (iter + 1 - old_size) * sizeof (int));
8545 }
8546 elem = VEC_index (int, inf_data->syscalls_counts, iter);
8547 VEC_replace (int, inf_data->syscalls_counts, iter, ++elem);
8548 }
8549 }
8550
8551 return target_set_syscall_catchpoint (ptid_get_pid (inferior_ptid),
8552 inf_data->total_syscalls_count != 0,
8553 inf_data->any_syscall_count,
8554 VEC_length (int,
8555 inf_data->syscalls_counts),
8556 VEC_address (int,
8557 inf_data->syscalls_counts));
8558 }
8559
8560 /* Implement the "remove" breakpoint_ops method for syscall
8561 catchpoints. */
8562
8563 static int
8564 remove_catch_syscall (struct bp_location *bl)
8565 {
8566 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
8567 struct inferior *inf = current_inferior ();
8568 struct catch_syscall_inferior_data *inf_data
8569 = get_catch_syscall_inferior_data (inf);
8570
8571 --inf_data->total_syscalls_count;
8572 if (!c->syscalls_to_be_caught)
8573 --inf_data->any_syscall_count;
8574 else
8575 {
8576 int i, iter;
8577
8578 for (i = 0;
8579 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
8580 i++)
8581 {
8582 int elem;
8583 if (iter >= VEC_length (int, inf_data->syscalls_counts))
8584 /* Shouldn't happen. */
8585 continue;
8586 elem = VEC_index (int, inf_data->syscalls_counts, iter);
8587 VEC_replace (int, inf_data->syscalls_counts, iter, --elem);
8588 }
8589 }
8590
8591 return target_set_syscall_catchpoint (ptid_get_pid (inferior_ptid),
8592 inf_data->total_syscalls_count != 0,
8593 inf_data->any_syscall_count,
8594 VEC_length (int,
8595 inf_data->syscalls_counts),
8596 VEC_address (int,
8597 inf_data->syscalls_counts));
8598 }
8599
8600 /* Implement the "breakpoint_hit" breakpoint_ops method for syscall
8601 catchpoints. */
8602
8603 static int
8604 breakpoint_hit_catch_syscall (const struct bp_location *bl,
8605 struct address_space *aspace, CORE_ADDR bp_addr,
8606 const struct target_waitstatus *ws)
8607 {
8608 /* We must check if we are catching specific syscalls in this
8609 breakpoint. If we are, then we must guarantee that the called
8610 syscall is the same syscall we are catching. */
8611 int syscall_number = 0;
8612 const struct syscall_catchpoint *c
8613 = (const struct syscall_catchpoint *) bl->owner;
8614
8615 if (ws->kind != TARGET_WAITKIND_SYSCALL_ENTRY
8616 && ws->kind != TARGET_WAITKIND_SYSCALL_RETURN)
8617 return 0;
8618
8619 syscall_number = ws->value.syscall_number;
8620
8621 /* Now, checking if the syscall is the same. */
8622 if (c->syscalls_to_be_caught)
8623 {
8624 int i, iter;
8625
8626 for (i = 0;
8627 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
8628 i++)
8629 if (syscall_number == iter)
8630 return 1;
8631
8632 return 0;
8633 }
8634
8635 return 1;
8636 }
8637
8638 /* Implement the "print_it" breakpoint_ops method for syscall
8639 catchpoints. */
8640
8641 static enum print_stop_action
8642 print_it_catch_syscall (bpstat bs)
8643 {
8644 struct ui_out *uiout = current_uiout;
8645 struct breakpoint *b = bs->breakpoint_at;
8646 /* These are needed because we want to know in which state a
8647 syscall is. It can be in the TARGET_WAITKIND_SYSCALL_ENTRY
8648 or TARGET_WAITKIND_SYSCALL_RETURN, and depending on it we
8649 must print "called syscall" or "returned from syscall". */
8650 ptid_t ptid;
8651 struct target_waitstatus last;
8652 struct syscall s;
8653 struct gdbarch *gdbarch = bs->bp_location_at->gdbarch;
8654
8655 get_last_target_status (&ptid, &last);
8656
8657 get_syscall_by_number (gdbarch, last.value.syscall_number, &s);
8658
8659 annotate_catchpoint (b->number);
8660
8661 if (b->disposition == disp_del)
8662 ui_out_text (uiout, "\nTemporary catchpoint ");
8663 else
8664 ui_out_text (uiout, "\nCatchpoint ");
8665 if (ui_out_is_mi_like_p (uiout))
8666 {
8667 ui_out_field_string (uiout, "reason",
8668 async_reason_lookup (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY
8669 ? EXEC_ASYNC_SYSCALL_ENTRY
8670 : EXEC_ASYNC_SYSCALL_RETURN));
8671 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8672 }
8673 ui_out_field_int (uiout, "bkptno", b->number);
8674
8675 if (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY)
8676 ui_out_text (uiout, " (call to syscall ");
8677 else
8678 ui_out_text (uiout, " (returned from syscall ");
8679
8680 if (s.name == NULL || ui_out_is_mi_like_p (uiout))
8681 ui_out_field_int (uiout, "syscall-number", last.value.syscall_number);
8682 if (s.name != NULL)
8683 ui_out_field_string (uiout, "syscall-name", s.name);
8684
8685 ui_out_text (uiout, "), ");
8686
8687 return PRINT_SRC_AND_LOC;
8688 }
8689
8690 /* Implement the "print_one" breakpoint_ops method for syscall
8691 catchpoints. */
8692
8693 static void
8694 print_one_catch_syscall (struct breakpoint *b,
8695 struct bp_location **last_loc)
8696 {
8697 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8698 struct value_print_options opts;
8699 struct ui_out *uiout = current_uiout;
8700 struct gdbarch *gdbarch = b->loc->gdbarch;
8701
8702 get_user_print_options (&opts);
8703 /* Field 4, the address, is omitted (which makes the columns not
8704 line up too nicely with the headers, but the effect is relatively
8705 readable). */
8706 if (opts.addressprint)
8707 ui_out_field_skip (uiout, "addr");
8708 annotate_field (5);
8709
8710 if (c->syscalls_to_be_caught
8711 && VEC_length (int, c->syscalls_to_be_caught) > 1)
8712 ui_out_text (uiout, "syscalls \"");
8713 else
8714 ui_out_text (uiout, "syscall \"");
8715
8716 if (c->syscalls_to_be_caught)
8717 {
8718 int i, iter;
8719 char *text = xstrprintf ("%s", "");
8720
8721 for (i = 0;
8722 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
8723 i++)
8724 {
8725 char *x = text;
8726 struct syscall s;
8727 get_syscall_by_number (gdbarch, iter, &s);
8728
8729 if (s.name != NULL)
8730 text = xstrprintf ("%s%s, ", text, s.name);
8731 else
8732 text = xstrprintf ("%s%d, ", text, iter);
8733
8734 /* We have to xfree the last 'text' (now stored at 'x')
8735 because xstrprintf dynamically allocates new space for it
8736 on every call. */
8737 xfree (x);
8738 }
8739 /* Remove the last comma. */
8740 text[strlen (text) - 2] = '\0';
8741 ui_out_field_string (uiout, "what", text);
8742 }
8743 else
8744 ui_out_field_string (uiout, "what", "<any syscall>");
8745 ui_out_text (uiout, "\" ");
8746
8747 if (ui_out_is_mi_like_p (uiout))
8748 ui_out_field_string (uiout, "catch-type", "syscall");
8749 }
8750
8751 /* Implement the "print_mention" breakpoint_ops method for syscall
8752 catchpoints. */
8753
8754 static void
8755 print_mention_catch_syscall (struct breakpoint *b)
8756 {
8757 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8758 struct gdbarch *gdbarch = b->loc->gdbarch;
8759
8760 if (c->syscalls_to_be_caught)
8761 {
8762 int i, iter;
8763
8764 if (VEC_length (int, c->syscalls_to_be_caught) > 1)
8765 printf_filtered (_("Catchpoint %d (syscalls"), b->number);
8766 else
8767 printf_filtered (_("Catchpoint %d (syscall"), b->number);
8768
8769 for (i = 0;
8770 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
8771 i++)
8772 {
8773 struct syscall s;
8774 get_syscall_by_number (gdbarch, iter, &s);
8775
8776 if (s.name)
8777 printf_filtered (" '%s' [%d]", s.name, s.number);
8778 else
8779 printf_filtered (" %d", s.number);
8780 }
8781 printf_filtered (")");
8782 }
8783 else
8784 printf_filtered (_("Catchpoint %d (any syscall)"),
8785 b->number);
8786 }
8787
8788 /* Implement the "print_recreate" breakpoint_ops method for syscall
8789 catchpoints. */
8790
8791 static void
8792 print_recreate_catch_syscall (struct breakpoint *b, struct ui_file *fp)
8793 {
8794 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8795 struct gdbarch *gdbarch = b->loc->gdbarch;
8796
8797 fprintf_unfiltered (fp, "catch syscall");
8798
8799 if (c->syscalls_to_be_caught)
8800 {
8801 int i, iter;
8802
8803 for (i = 0;
8804 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
8805 i++)
8806 {
8807 struct syscall s;
8808
8809 get_syscall_by_number (gdbarch, iter, &s);
8810 if (s.name)
8811 fprintf_unfiltered (fp, " %s", s.name);
8812 else
8813 fprintf_unfiltered (fp, " %d", s.number);
8814 }
8815 }
8816 print_recreate_thread (b, fp);
8817 }
8818
8819 /* The breakpoint_ops structure to be used in syscall catchpoints. */
8820
8821 static struct breakpoint_ops catch_syscall_breakpoint_ops;
8822
8823 /* Returns non-zero if 'b' is a syscall catchpoint. */
8824
8825 static int
8826 syscall_catchpoint_p (struct breakpoint *b)
8827 {
8828 return (b->ops == &catch_syscall_breakpoint_ops);
8829 }
8830
8831 /* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8832 is non-zero, then make the breakpoint temporary. If COND_STRING is
8833 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8834 the breakpoint_ops structure associated to the catchpoint. */
8835
8836 void
8837 init_catchpoint (struct breakpoint *b,
8838 struct gdbarch *gdbarch, int tempflag,
8839 char *cond_string,
8840 const struct breakpoint_ops *ops)
8841 {
8842 struct symtab_and_line sal;
8843
8844 init_sal (&sal);
8845 sal.pspace = current_program_space;
8846
8847 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
8848
8849 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
8850 b->disposition = tempflag ? disp_del : disp_donttouch;
8851 }
8852
8853 void
8854 install_breakpoint (int internal, struct breakpoint *b, int update_gll)
8855 {
8856 add_to_breakpoint_chain (b);
8857 set_breakpoint_number (internal, b);
8858 if (is_tracepoint (b))
8859 set_tracepoint_count (breakpoint_count);
8860 if (!internal)
8861 mention (b);
8862 observer_notify_breakpoint_created (b);
8863
8864 if (update_gll)
8865 update_global_location_list (UGLL_MAY_INSERT);
8866 }
8867
8868 static void
8869 create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8870 int tempflag, char *cond_string,
8871 const struct breakpoint_ops *ops)
8872 {
8873 struct fork_catchpoint *c = XNEW (struct fork_catchpoint);
8874
8875 init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
8876
8877 c->forked_inferior_pid = null_ptid;
8878
8879 install_breakpoint (0, &c->base, 1);
8880 }
8881
8882 /* Exec catchpoints. */
8883
8884 /* An instance of this type is used to represent an exec catchpoint.
8885 It includes a "struct breakpoint" as a kind of base class; users
8886 downcast to "struct breakpoint *" when needed. A breakpoint is
8887 really of this type iff its ops pointer points to
8888 CATCH_EXEC_BREAKPOINT_OPS. */
8889
8890 struct exec_catchpoint
8891 {
8892 /* The base class. */
8893 struct breakpoint base;
8894
8895 /* Filename of a program whose exec triggered this catchpoint.
8896 This field is only valid immediately after this catchpoint has
8897 triggered. */
8898 char *exec_pathname;
8899 };
8900
8901 /* Implement the "dtor" breakpoint_ops method for exec
8902 catchpoints. */
8903
8904 static void
8905 dtor_catch_exec (struct breakpoint *b)
8906 {
8907 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8908
8909 xfree (c->exec_pathname);
8910
8911 base_breakpoint_ops.dtor (b);
8912 }
8913
8914 static int
8915 insert_catch_exec (struct bp_location *bl)
8916 {
8917 return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
8918 }
8919
8920 static int
8921 remove_catch_exec (struct bp_location *bl)
8922 {
8923 return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
8924 }
8925
8926 static int
8927 breakpoint_hit_catch_exec (const struct bp_location *bl,
8928 struct address_space *aspace, CORE_ADDR bp_addr,
8929 const struct target_waitstatus *ws)
8930 {
8931 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8932
8933 if (ws->kind != TARGET_WAITKIND_EXECD)
8934 return 0;
8935
8936 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8937 return 1;
8938 }
8939
8940 static enum print_stop_action
8941 print_it_catch_exec (bpstat bs)
8942 {
8943 struct ui_out *uiout = current_uiout;
8944 struct breakpoint *b = bs->breakpoint_at;
8945 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8946
8947 annotate_catchpoint (b->number);
8948 if (b->disposition == disp_del)
8949 ui_out_text (uiout, "\nTemporary catchpoint ");
8950 else
8951 ui_out_text (uiout, "\nCatchpoint ");
8952 if (ui_out_is_mi_like_p (uiout))
8953 {
8954 ui_out_field_string (uiout, "reason",
8955 async_reason_lookup (EXEC_ASYNC_EXEC));
8956 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8957 }
8958 ui_out_field_int (uiout, "bkptno", b->number);
8959 ui_out_text (uiout, " (exec'd ");
8960 ui_out_field_string (uiout, "new-exec", c->exec_pathname);
8961 ui_out_text (uiout, "), ");
8962
8963 return PRINT_SRC_AND_LOC;
8964 }
8965
8966 static void
8967 print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
8968 {
8969 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8970 struct value_print_options opts;
8971 struct ui_out *uiout = current_uiout;
8972
8973 get_user_print_options (&opts);
8974
8975 /* Field 4, the address, is omitted (which makes the columns
8976 not line up too nicely with the headers, but the effect
8977 is relatively readable). */
8978 if (opts.addressprint)
8979 ui_out_field_skip (uiout, "addr");
8980 annotate_field (5);
8981 ui_out_text (uiout, "exec");
8982 if (c->exec_pathname != NULL)
8983 {
8984 ui_out_text (uiout, ", program \"");
8985 ui_out_field_string (uiout, "what", c->exec_pathname);
8986 ui_out_text (uiout, "\" ");
8987 }
8988
8989 if (ui_out_is_mi_like_p (uiout))
8990 ui_out_field_string (uiout, "catch-type", "exec");
8991 }
8992
8993 static void
8994 print_mention_catch_exec (struct breakpoint *b)
8995 {
8996 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8997 }
8998
8999 /* Implement the "print_recreate" breakpoint_ops method for exec
9000 catchpoints. */
9001
9002 static void
9003 print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
9004 {
9005 fprintf_unfiltered (fp, "catch exec");
9006 print_recreate_thread (b, fp);
9007 }
9008
9009 static struct breakpoint_ops catch_exec_breakpoint_ops;
9010
9011 static void
9012 create_syscall_event_catchpoint (int tempflag, VEC(int) *filter,
9013 const struct breakpoint_ops *ops)
9014 {
9015 struct syscall_catchpoint *c;
9016 struct gdbarch *gdbarch = get_current_arch ();
9017
9018 c = XNEW (struct syscall_catchpoint);
9019 init_catchpoint (&c->base, gdbarch, tempflag, NULL, ops);
9020 c->syscalls_to_be_caught = filter;
9021
9022 install_breakpoint (0, &c->base, 1);
9023 }
9024
9025 static int
9026 hw_breakpoint_used_count (void)
9027 {
9028 int i = 0;
9029 struct breakpoint *b;
9030 struct bp_location *bl;
9031
9032 ALL_BREAKPOINTS (b)
9033 {
9034 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
9035 for (bl = b->loc; bl; bl = bl->next)
9036 {
9037 /* Special types of hardware breakpoints may use more than
9038 one register. */
9039 i += b->ops->resources_needed (bl);
9040 }
9041 }
9042
9043 return i;
9044 }
9045
9046 /* Returns the resources B would use if it were a hardware
9047 watchpoint. */
9048
9049 static int
9050 hw_watchpoint_use_count (struct breakpoint *b)
9051 {
9052 int i = 0;
9053 struct bp_location *bl;
9054
9055 if (!breakpoint_enabled (b))
9056 return 0;
9057
9058 for (bl = b->loc; bl; bl = bl->next)
9059 {
9060 /* Special types of hardware watchpoints may use more than
9061 one register. */
9062 i += b->ops->resources_needed (bl);
9063 }
9064
9065 return i;
9066 }
9067
9068 /* Returns the sum the used resources of all hardware watchpoints of
9069 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
9070 the sum of the used resources of all hardware watchpoints of other
9071 types _not_ TYPE. */
9072
9073 static int
9074 hw_watchpoint_used_count_others (struct breakpoint *except,
9075 enum bptype type, int *other_type_used)
9076 {
9077 int i = 0;
9078 struct breakpoint *b;
9079
9080 *other_type_used = 0;
9081 ALL_BREAKPOINTS (b)
9082 {
9083 if (b == except)
9084 continue;
9085 if (!breakpoint_enabled (b))
9086 continue;
9087
9088 if (b->type == type)
9089 i += hw_watchpoint_use_count (b);
9090 else if (is_hardware_watchpoint (b))
9091 *other_type_used = 1;
9092 }
9093
9094 return i;
9095 }
9096
9097 void
9098 disable_watchpoints_before_interactive_call_start (void)
9099 {
9100 struct breakpoint *b;
9101
9102 ALL_BREAKPOINTS (b)
9103 {
9104 if (is_watchpoint (b) && breakpoint_enabled (b))
9105 {
9106 b->enable_state = bp_call_disabled;
9107 update_global_location_list (UGLL_DONT_INSERT);
9108 }
9109 }
9110 }
9111
9112 void
9113 enable_watchpoints_after_interactive_call_stop (void)
9114 {
9115 struct breakpoint *b;
9116
9117 ALL_BREAKPOINTS (b)
9118 {
9119 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
9120 {
9121 b->enable_state = bp_enabled;
9122 update_global_location_list (UGLL_MAY_INSERT);
9123 }
9124 }
9125 }
9126
9127 void
9128 disable_breakpoints_before_startup (void)
9129 {
9130 current_program_space->executing_startup = 1;
9131 update_global_location_list (UGLL_DONT_INSERT);
9132 }
9133
9134 void
9135 enable_breakpoints_after_startup (void)
9136 {
9137 current_program_space->executing_startup = 0;
9138 breakpoint_re_set ();
9139 }
9140
9141 /* Create a new single-step breakpoint for thread THREAD, with no
9142 locations. */
9143
9144 static struct breakpoint *
9145 new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
9146 {
9147 struct breakpoint *b = XNEW (struct breakpoint);
9148
9149 init_raw_breakpoint_without_location (b, gdbarch, bp_single_step,
9150 &momentary_breakpoint_ops);
9151
9152 b->disposition = disp_donttouch;
9153 b->frame_id = null_frame_id;
9154
9155 b->thread = thread;
9156 gdb_assert (b->thread != 0);
9157
9158 add_to_breakpoint_chain (b);
9159
9160 return b;
9161 }
9162
9163 /* Set a momentary breakpoint of type TYPE at address specified by
9164 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
9165 frame. */
9166
9167 struct breakpoint *
9168 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
9169 struct frame_id frame_id, enum bptype type)
9170 {
9171 struct breakpoint *b;
9172
9173 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
9174 tail-called one. */
9175 gdb_assert (!frame_id_artificial_p (frame_id));
9176
9177 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
9178 b->enable_state = bp_enabled;
9179 b->disposition = disp_donttouch;
9180 b->frame_id = frame_id;
9181
9182 /* If we're debugging a multi-threaded program, then we want
9183 momentary breakpoints to be active in only a single thread of
9184 control. */
9185 if (in_thread_list (inferior_ptid))
9186 b->thread = pid_to_thread_id (inferior_ptid);
9187
9188 update_global_location_list_nothrow (UGLL_MAY_INSERT);
9189
9190 return b;
9191 }
9192
9193 /* Make a momentary breakpoint based on the master breakpoint ORIG.
9194 The new breakpoint will have type TYPE, use OPS as its
9195 breakpoint_ops, and will set enabled to LOC_ENABLED. */
9196
9197 static struct breakpoint *
9198 momentary_breakpoint_from_master (struct breakpoint *orig,
9199 enum bptype type,
9200 const struct breakpoint_ops *ops,
9201 int loc_enabled)
9202 {
9203 struct breakpoint *copy;
9204
9205 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
9206 copy->loc = allocate_bp_location (copy);
9207 set_breakpoint_location_function (copy->loc, 1);
9208
9209 copy->loc->gdbarch = orig->loc->gdbarch;
9210 copy->loc->requested_address = orig->loc->requested_address;
9211 copy->loc->address = orig->loc->address;
9212 copy->loc->section = orig->loc->section;
9213 copy->loc->pspace = orig->loc->pspace;
9214 copy->loc->probe = orig->loc->probe;
9215 copy->loc->line_number = orig->loc->line_number;
9216 copy->loc->symtab = orig->loc->symtab;
9217 copy->loc->enabled = loc_enabled;
9218 copy->frame_id = orig->frame_id;
9219 copy->thread = orig->thread;
9220 copy->pspace = orig->pspace;
9221
9222 copy->enable_state = bp_enabled;
9223 copy->disposition = disp_donttouch;
9224 copy->number = internal_breakpoint_number--;
9225
9226 update_global_location_list_nothrow (UGLL_DONT_INSERT);
9227 return copy;
9228 }
9229
9230 /* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
9231 ORIG is NULL. */
9232
9233 struct breakpoint *
9234 clone_momentary_breakpoint (struct breakpoint *orig)
9235 {
9236 /* If there's nothing to clone, then return nothing. */
9237 if (orig == NULL)
9238 return NULL;
9239
9240 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
9241 }
9242
9243 struct breakpoint *
9244 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
9245 enum bptype type)
9246 {
9247 struct symtab_and_line sal;
9248
9249 sal = find_pc_line (pc, 0);
9250 sal.pc = pc;
9251 sal.section = find_pc_overlay (pc);
9252 sal.explicit_pc = 1;
9253
9254 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
9255 }
9256 \f
9257
9258 /* Tell the user we have just set a breakpoint B. */
9259
9260 static void
9261 mention (struct breakpoint *b)
9262 {
9263 b->ops->print_mention (b);
9264 if (ui_out_is_mi_like_p (current_uiout))
9265 return;
9266 printf_filtered ("\n");
9267 }
9268 \f
9269
9270 static int bp_loc_is_permanent (struct bp_location *loc);
9271
9272 static struct bp_location *
9273 add_location_to_breakpoint (struct breakpoint *b,
9274 const struct symtab_and_line *sal)
9275 {
9276 struct bp_location *loc, **tmp;
9277 CORE_ADDR adjusted_address;
9278 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
9279
9280 if (loc_gdbarch == NULL)
9281 loc_gdbarch = b->gdbarch;
9282
9283 /* Adjust the breakpoint's address prior to allocating a location.
9284 Once we call allocate_bp_location(), that mostly uninitialized
9285 location will be placed on the location chain. Adjustment of the
9286 breakpoint may cause target_read_memory() to be called and we do
9287 not want its scan of the location chain to find a breakpoint and
9288 location that's only been partially initialized. */
9289 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
9290 sal->pc, b->type);
9291
9292 /* Sort the locations by their ADDRESS. */
9293 loc = allocate_bp_location (b);
9294 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
9295 tmp = &((*tmp)->next))
9296 ;
9297 loc->next = *tmp;
9298 *tmp = loc;
9299
9300 loc->requested_address = sal->pc;
9301 loc->address = adjusted_address;
9302 loc->pspace = sal->pspace;
9303 loc->probe.probe = sal->probe;
9304 loc->probe.objfile = sal->objfile;
9305 gdb_assert (loc->pspace != NULL);
9306 loc->section = sal->section;
9307 loc->gdbarch = loc_gdbarch;
9308 loc->line_number = sal->line;
9309 loc->symtab = sal->symtab;
9310
9311 set_breakpoint_location_function (loc,
9312 sal->explicit_pc || sal->explicit_line);
9313
9314 if (bp_loc_is_permanent (loc))
9315 {
9316 loc->inserted = 1;
9317 loc->permanent = 1;
9318 }
9319
9320 return loc;
9321 }
9322 \f
9323
9324 /* See breakpoint.h. */
9325
9326 int
9327 program_breakpoint_here_p (struct gdbarch *gdbarch, CORE_ADDR address)
9328 {
9329 int len;
9330 CORE_ADDR addr;
9331 const gdb_byte *bpoint;
9332 gdb_byte *target_mem;
9333 struct cleanup *cleanup;
9334 int retval = 0;
9335
9336 addr = address;
9337 bpoint = gdbarch_breakpoint_from_pc (gdbarch, &addr, &len);
9338
9339 /* Software breakpoints unsupported? */
9340 if (bpoint == NULL)
9341 return 0;
9342
9343 target_mem = alloca (len);
9344
9345 /* Enable the automatic memory restoration from breakpoints while
9346 we read the memory. Otherwise we could say about our temporary
9347 breakpoints they are permanent. */
9348 cleanup = make_show_memory_breakpoints_cleanup (0);
9349
9350 if (target_read_memory (address, target_mem, len) == 0
9351 && memcmp (target_mem, bpoint, len) == 0)
9352 retval = 1;
9353
9354 do_cleanups (cleanup);
9355
9356 return retval;
9357 }
9358
9359 /* Return 1 if LOC is pointing to a permanent breakpoint,
9360 return 0 otherwise. */
9361
9362 static int
9363 bp_loc_is_permanent (struct bp_location *loc)
9364 {
9365 struct cleanup *cleanup;
9366 int retval;
9367
9368 gdb_assert (loc != NULL);
9369
9370 /* bp_call_dummy breakpoint locations are usually memory locations
9371 where GDB just wrote a breakpoint instruction, making it look
9372 as if there is a permanent breakpoint at that location. Considering
9373 it permanent makes GDB rely on that breakpoint instruction to stop
9374 the program, thus removing the need to insert its own breakpoint
9375 there. This is normally expected to work, except that some versions
9376 of QEMU (Eg: QEMU 2.0.0 for SPARC) just report a fatal problem (Trap
9377 0x02 while interrupts disabled, Error state) instead of reporting
9378 a SIGTRAP. QEMU should probably be fixed, but in the interest of
9379 compatibility with versions that behave this way, we always consider
9380 bp_call_dummy breakpoint locations as non-permanent. */
9381 if (loc->owner->type == bp_call_dummy)
9382 return 0;
9383
9384 cleanup = save_current_space_and_thread ();
9385 switch_to_program_space_and_thread (loc->pspace);
9386
9387 retval = program_breakpoint_here_p (loc->gdbarch, loc->address);
9388
9389 do_cleanups (cleanup);
9390
9391 return retval;
9392 }
9393
9394 /* Build a command list for the dprintf corresponding to the current
9395 settings of the dprintf style options. */
9396
9397 static void
9398 update_dprintf_command_list (struct breakpoint *b)
9399 {
9400 char *dprintf_args = b->extra_string;
9401 char *printf_line = NULL;
9402
9403 if (!dprintf_args)
9404 return;
9405
9406 dprintf_args = skip_spaces (dprintf_args);
9407
9408 /* Allow a comma, as it may have terminated a location, but don't
9409 insist on it. */
9410 if (*dprintf_args == ',')
9411 ++dprintf_args;
9412 dprintf_args = skip_spaces (dprintf_args);
9413
9414 if (*dprintf_args != '"')
9415 error (_("Bad format string, missing '\"'."));
9416
9417 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
9418 printf_line = xstrprintf ("printf %s", dprintf_args);
9419 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
9420 {
9421 if (!dprintf_function)
9422 error (_("No function supplied for dprintf call"));
9423
9424 if (dprintf_channel && strlen (dprintf_channel) > 0)
9425 printf_line = xstrprintf ("call (void) %s (%s,%s)",
9426 dprintf_function,
9427 dprintf_channel,
9428 dprintf_args);
9429 else
9430 printf_line = xstrprintf ("call (void) %s (%s)",
9431 dprintf_function,
9432 dprintf_args);
9433 }
9434 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
9435 {
9436 if (target_can_run_breakpoint_commands ())
9437 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
9438 else
9439 {
9440 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
9441 printf_line = xstrprintf ("printf %s", dprintf_args);
9442 }
9443 }
9444 else
9445 internal_error (__FILE__, __LINE__,
9446 _("Invalid dprintf style."));
9447
9448 gdb_assert (printf_line != NULL);
9449 /* Manufacture a printf sequence. */
9450 {
9451 struct command_line *printf_cmd_line
9452 = xmalloc (sizeof (struct command_line));
9453
9454 printf_cmd_line = xmalloc (sizeof (struct command_line));
9455 printf_cmd_line->control_type = simple_control;
9456 printf_cmd_line->body_count = 0;
9457 printf_cmd_line->body_list = NULL;
9458 printf_cmd_line->next = NULL;
9459 printf_cmd_line->line = printf_line;
9460
9461 breakpoint_set_commands (b, printf_cmd_line);
9462 }
9463 }
9464
9465 /* Update all dprintf commands, making their command lists reflect
9466 current style settings. */
9467
9468 static void
9469 update_dprintf_commands (char *args, int from_tty,
9470 struct cmd_list_element *c)
9471 {
9472 struct breakpoint *b;
9473
9474 ALL_BREAKPOINTS (b)
9475 {
9476 if (b->type == bp_dprintf)
9477 update_dprintf_command_list (b);
9478 }
9479 }
9480
9481 /* Create a breakpoint with SAL as location. Use ADDR_STRING
9482 as textual description of the location, and COND_STRING
9483 as condition expression. */
9484
9485 static void
9486 init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
9487 struct symtabs_and_lines sals, char *addr_string,
9488 char *filter, char *cond_string,
9489 char *extra_string,
9490 enum bptype type, enum bpdisp disposition,
9491 int thread, int task, int ignore_count,
9492 const struct breakpoint_ops *ops, int from_tty,
9493 int enabled, int internal, unsigned flags,
9494 int display_canonical)
9495 {
9496 int i;
9497
9498 if (type == bp_hardware_breakpoint)
9499 {
9500 int target_resources_ok;
9501
9502 i = hw_breakpoint_used_count ();
9503 target_resources_ok =
9504 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9505 i + 1, 0);
9506 if (target_resources_ok == 0)
9507 error (_("No hardware breakpoint support in the target."));
9508 else if (target_resources_ok < 0)
9509 error (_("Hardware breakpoints used exceeds limit."));
9510 }
9511
9512 gdb_assert (sals.nelts > 0);
9513
9514 for (i = 0; i < sals.nelts; ++i)
9515 {
9516 struct symtab_and_line sal = sals.sals[i];
9517 struct bp_location *loc;
9518
9519 if (from_tty)
9520 {
9521 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9522 if (!loc_gdbarch)
9523 loc_gdbarch = gdbarch;
9524
9525 describe_other_breakpoints (loc_gdbarch,
9526 sal.pspace, sal.pc, sal.section, thread);
9527 }
9528
9529 if (i == 0)
9530 {
9531 init_raw_breakpoint (b, gdbarch, sal, type, ops);
9532 b->thread = thread;
9533 b->task = task;
9534
9535 b->cond_string = cond_string;
9536 b->extra_string = extra_string;
9537 b->ignore_count = ignore_count;
9538 b->enable_state = enabled ? bp_enabled : bp_disabled;
9539 b->disposition = disposition;
9540
9541 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9542 b->loc->inserted = 1;
9543
9544 if (type == bp_static_tracepoint)
9545 {
9546 struct tracepoint *t = (struct tracepoint *) b;
9547 struct static_tracepoint_marker marker;
9548
9549 if (strace_marker_p (b))
9550 {
9551 /* We already know the marker exists, otherwise, we
9552 wouldn't see a sal for it. */
9553 char *p = &addr_string[3];
9554 char *endp;
9555 char *marker_str;
9556
9557 p = skip_spaces (p);
9558
9559 endp = skip_to_space (p);
9560
9561 marker_str = savestring (p, endp - p);
9562 t->static_trace_marker_id = marker_str;
9563
9564 printf_filtered (_("Probed static tracepoint "
9565 "marker \"%s\"\n"),
9566 t->static_trace_marker_id);
9567 }
9568 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
9569 {
9570 t->static_trace_marker_id = xstrdup (marker.str_id);
9571 release_static_tracepoint_marker (&marker);
9572
9573 printf_filtered (_("Probed static tracepoint "
9574 "marker \"%s\"\n"),
9575 t->static_trace_marker_id);
9576 }
9577 else
9578 warning (_("Couldn't determine the static "
9579 "tracepoint marker to probe"));
9580 }
9581
9582 loc = b->loc;
9583 }
9584 else
9585 {
9586 loc = add_location_to_breakpoint (b, &sal);
9587 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9588 loc->inserted = 1;
9589 }
9590
9591 if (b->cond_string)
9592 {
9593 const char *arg = b->cond_string;
9594
9595 loc->cond = parse_exp_1 (&arg, loc->address,
9596 block_for_pc (loc->address), 0);
9597 if (*arg)
9598 error (_("Garbage '%s' follows condition"), arg);
9599 }
9600
9601 /* Dynamic printf requires and uses additional arguments on the
9602 command line, otherwise it's an error. */
9603 if (type == bp_dprintf)
9604 {
9605 if (b->extra_string)
9606 update_dprintf_command_list (b);
9607 else
9608 error (_("Format string required"));
9609 }
9610 else if (b->extra_string)
9611 error (_("Garbage '%s' at end of command"), b->extra_string);
9612 }
9613
9614 b->display_canonical = display_canonical;
9615 if (addr_string)
9616 b->addr_string = addr_string;
9617 else
9618 /* addr_string has to be used or breakpoint_re_set will delete
9619 me. */
9620 b->addr_string
9621 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
9622 b->filter = filter;
9623 }
9624
9625 static void
9626 create_breakpoint_sal (struct gdbarch *gdbarch,
9627 struct symtabs_and_lines sals, char *addr_string,
9628 char *filter, char *cond_string,
9629 char *extra_string,
9630 enum bptype type, enum bpdisp disposition,
9631 int thread, int task, int ignore_count,
9632 const struct breakpoint_ops *ops, int from_tty,
9633 int enabled, int internal, unsigned flags,
9634 int display_canonical)
9635 {
9636 struct breakpoint *b;
9637 struct cleanup *old_chain;
9638
9639 if (is_tracepoint_type (type))
9640 {
9641 struct tracepoint *t;
9642
9643 t = XCNEW (struct tracepoint);
9644 b = &t->base;
9645 }
9646 else
9647 b = XNEW (struct breakpoint);
9648
9649 old_chain = make_cleanup (xfree, b);
9650
9651 init_breakpoint_sal (b, gdbarch,
9652 sals, addr_string,
9653 filter, cond_string, extra_string,
9654 type, disposition,
9655 thread, task, ignore_count,
9656 ops, from_tty,
9657 enabled, internal, flags,
9658 display_canonical);
9659 discard_cleanups (old_chain);
9660
9661 install_breakpoint (internal, b, 0);
9662 }
9663
9664 /* Add SALS.nelts breakpoints to the breakpoint table. For each
9665 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9666 value. COND_STRING, if not NULL, specified the condition to be
9667 used for all breakpoints. Essentially the only case where
9668 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9669 function. In that case, it's still not possible to specify
9670 separate conditions for different overloaded functions, so
9671 we take just a single condition string.
9672
9673 NOTE: If the function succeeds, the caller is expected to cleanup
9674 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
9675 array contents). If the function fails (error() is called), the
9676 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
9677 COND and SALS arrays and each of those arrays contents. */
9678
9679 static void
9680 create_breakpoints_sal (struct gdbarch *gdbarch,
9681 struct linespec_result *canonical,
9682 char *cond_string, char *extra_string,
9683 enum bptype type, enum bpdisp disposition,
9684 int thread, int task, int ignore_count,
9685 const struct breakpoint_ops *ops, int from_tty,
9686 int enabled, int internal, unsigned flags)
9687 {
9688 int i;
9689 struct linespec_sals *lsal;
9690
9691 if (canonical->pre_expanded)
9692 gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
9693
9694 for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
9695 {
9696 /* Note that 'addr_string' can be NULL in the case of a plain
9697 'break', without arguments. */
9698 char *addr_string = (canonical->addr_string
9699 ? xstrdup (canonical->addr_string)
9700 : NULL);
9701 char *filter_string = lsal->canonical ? xstrdup (lsal->canonical) : NULL;
9702 struct cleanup *inner = make_cleanup (xfree, addr_string);
9703
9704 make_cleanup (xfree, filter_string);
9705 create_breakpoint_sal (gdbarch, lsal->sals,
9706 addr_string,
9707 filter_string,
9708 cond_string, extra_string,
9709 type, disposition,
9710 thread, task, ignore_count, ops,
9711 from_tty, enabled, internal, flags,
9712 canonical->special_display);
9713 discard_cleanups (inner);
9714 }
9715 }
9716
9717 /* Parse ADDRESS which is assumed to be a SAL specification possibly
9718 followed by conditionals. On return, SALS contains an array of SAL
9719 addresses found. ADDR_STRING contains a vector of (canonical)
9720 address strings. ADDRESS points to the end of the SAL.
9721
9722 The array and the line spec strings are allocated on the heap, it is
9723 the caller's responsibility to free them. */
9724
9725 static void
9726 parse_breakpoint_sals (char **address,
9727 struct linespec_result *canonical)
9728 {
9729 /* If no arg given, or if first arg is 'if ', use the default
9730 breakpoint. */
9731 if ((*address) == NULL
9732 || (strncmp ((*address), "if", 2) == 0 && isspace ((*address)[2])))
9733 {
9734 /* The last displayed codepoint, if it's valid, is our default breakpoint
9735 address. */
9736 if (last_displayed_sal_is_valid ())
9737 {
9738 struct linespec_sals lsal;
9739 struct symtab_and_line sal;
9740 CORE_ADDR pc;
9741
9742 init_sal (&sal); /* Initialize to zeroes. */
9743 lsal.sals.sals = (struct symtab_and_line *)
9744 xmalloc (sizeof (struct symtab_and_line));
9745
9746 /* Set sal's pspace, pc, symtab, and line to the values
9747 corresponding to the last call to print_frame_info.
9748 Be sure to reinitialize LINE with NOTCURRENT == 0
9749 as the breakpoint line number is inappropriate otherwise.
9750 find_pc_line would adjust PC, re-set it back. */
9751 get_last_displayed_sal (&sal);
9752 pc = sal.pc;
9753 sal = find_pc_line (pc, 0);
9754
9755 /* "break" without arguments is equivalent to "break *PC"
9756 where PC is the last displayed codepoint's address. So
9757 make sure to set sal.explicit_pc to prevent GDB from
9758 trying to expand the list of sals to include all other
9759 instances with the same symtab and line. */
9760 sal.pc = pc;
9761 sal.explicit_pc = 1;
9762
9763 lsal.sals.sals[0] = sal;
9764 lsal.sals.nelts = 1;
9765 lsal.canonical = NULL;
9766
9767 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
9768 }
9769 else
9770 error (_("No default breakpoint address now."));
9771 }
9772 else
9773 {
9774 struct symtab_and_line cursal = get_current_source_symtab_and_line ();
9775
9776 /* Force almost all breakpoints to be in terms of the
9777 current_source_symtab (which is decode_line_1's default).
9778 This should produce the results we want almost all of the
9779 time while leaving default_breakpoint_* alone.
9780
9781 ObjC: However, don't match an Objective-C method name which
9782 may have a '+' or '-' succeeded by a '['. */
9783 if (last_displayed_sal_is_valid ()
9784 && (!cursal.symtab
9785 || ((strchr ("+-", (*address)[0]) != NULL)
9786 && ((*address)[1] != '['))))
9787 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
9788 get_last_displayed_symtab (),
9789 get_last_displayed_line (),
9790 canonical, NULL, NULL);
9791 else
9792 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
9793 cursal.symtab, cursal.line, canonical, NULL, NULL);
9794 }
9795 }
9796
9797
9798 /* Convert each SAL into a real PC. Verify that the PC can be
9799 inserted as a breakpoint. If it can't throw an error. */
9800
9801 static void
9802 breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
9803 {
9804 int i;
9805
9806 for (i = 0; i < sals->nelts; i++)
9807 resolve_sal_pc (&sals->sals[i]);
9808 }
9809
9810 /* Fast tracepoints may have restrictions on valid locations. For
9811 instance, a fast tracepoint using a jump instead of a trap will
9812 likely have to overwrite more bytes than a trap would, and so can
9813 only be placed where the instruction is longer than the jump, or a
9814 multi-instruction sequence does not have a jump into the middle of
9815 it, etc. */
9816
9817 static void
9818 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9819 struct symtabs_and_lines *sals)
9820 {
9821 int i, rslt;
9822 struct symtab_and_line *sal;
9823 char *msg;
9824 struct cleanup *old_chain;
9825
9826 for (i = 0; i < sals->nelts; i++)
9827 {
9828 struct gdbarch *sarch;
9829
9830 sal = &sals->sals[i];
9831
9832 sarch = get_sal_arch (*sal);
9833 /* We fall back to GDBARCH if there is no architecture
9834 associated with SAL. */
9835 if (sarch == NULL)
9836 sarch = gdbarch;
9837 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc,
9838 NULL, &msg);
9839 old_chain = make_cleanup (xfree, msg);
9840
9841 if (!rslt)
9842 error (_("May not have a fast tracepoint at 0x%s%s"),
9843 paddress (sarch, sal->pc), (msg ? msg : ""));
9844
9845 do_cleanups (old_chain);
9846 }
9847 }
9848
9849 /* Issue an invalid thread ID error. */
9850
9851 static void ATTRIBUTE_NORETURN
9852 invalid_thread_id_error (int id)
9853 {
9854 error (_("Unknown thread %d."), id);
9855 }
9856
9857 /* Given TOK, a string specification of condition and thread, as
9858 accepted by the 'break' command, extract the condition
9859 string and thread number and set *COND_STRING and *THREAD.
9860 PC identifies the context at which the condition should be parsed.
9861 If no condition is found, *COND_STRING is set to NULL.
9862 If no thread is found, *THREAD is set to -1. */
9863
9864 static void
9865 find_condition_and_thread (const char *tok, CORE_ADDR pc,
9866 char **cond_string, int *thread, int *task,
9867 char **rest)
9868 {
9869 *cond_string = NULL;
9870 *thread = -1;
9871 *task = 0;
9872 *rest = NULL;
9873
9874 while (tok && *tok)
9875 {
9876 const char *end_tok;
9877 int toklen;
9878 const char *cond_start = NULL;
9879 const char *cond_end = NULL;
9880
9881 tok = skip_spaces_const (tok);
9882
9883 if ((*tok == '"' || *tok == ',') && rest)
9884 {
9885 *rest = savestring (tok, strlen (tok));
9886 return;
9887 }
9888
9889 end_tok = skip_to_space_const (tok);
9890
9891 toklen = end_tok - tok;
9892
9893 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9894 {
9895 struct expression *expr;
9896
9897 tok = cond_start = end_tok + 1;
9898 expr = parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
9899 xfree (expr);
9900 cond_end = tok;
9901 *cond_string = savestring (cond_start, cond_end - cond_start);
9902 }
9903 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9904 {
9905 char *tmptok;
9906
9907 tok = end_tok + 1;
9908 *thread = strtol (tok, &tmptok, 0);
9909 if (tok == tmptok)
9910 error (_("Junk after thread keyword."));
9911 if (!valid_thread_id (*thread))
9912 invalid_thread_id_error (*thread);
9913 tok = tmptok;
9914 }
9915 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9916 {
9917 char *tmptok;
9918
9919 tok = end_tok + 1;
9920 *task = strtol (tok, &tmptok, 0);
9921 if (tok == tmptok)
9922 error (_("Junk after task keyword."));
9923 if (!valid_task_id (*task))
9924 error (_("Unknown task %d."), *task);
9925 tok = tmptok;
9926 }
9927 else if (rest)
9928 {
9929 *rest = savestring (tok, strlen (tok));
9930 return;
9931 }
9932 else
9933 error (_("Junk at end of arguments."));
9934 }
9935 }
9936
9937 /* Decode a static tracepoint marker spec. */
9938
9939 static struct symtabs_and_lines
9940 decode_static_tracepoint_spec (char **arg_p)
9941 {
9942 VEC(static_tracepoint_marker_p) *markers = NULL;
9943 struct symtabs_and_lines sals;
9944 struct cleanup *old_chain;
9945 char *p = &(*arg_p)[3];
9946 char *endp;
9947 char *marker_str;
9948 int i;
9949
9950 p = skip_spaces (p);
9951
9952 endp = skip_to_space (p);
9953
9954 marker_str = savestring (p, endp - p);
9955 old_chain = make_cleanup (xfree, marker_str);
9956
9957 markers = target_static_tracepoint_markers_by_strid (marker_str);
9958 if (VEC_empty(static_tracepoint_marker_p, markers))
9959 error (_("No known static tracepoint marker named %s"), marker_str);
9960
9961 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
9962 sals.sals = xmalloc (sizeof *sals.sals * sals.nelts);
9963
9964 for (i = 0; i < sals.nelts; i++)
9965 {
9966 struct static_tracepoint_marker *marker;
9967
9968 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9969
9970 init_sal (&sals.sals[i]);
9971
9972 sals.sals[i] = find_pc_line (marker->address, 0);
9973 sals.sals[i].pc = marker->address;
9974
9975 release_static_tracepoint_marker (marker);
9976 }
9977
9978 do_cleanups (old_chain);
9979
9980 *arg_p = endp;
9981 return sals;
9982 }
9983
9984 /* Set a breakpoint. This function is shared between CLI and MI
9985 functions for setting a breakpoint. This function has two major
9986 modes of operations, selected by the PARSE_ARG parameter. If
9987 non-zero, the function will parse ARG, extracting location,
9988 condition, thread and extra string. Otherwise, ARG is just the
9989 breakpoint's location, with condition, thread, and extra string
9990 specified by the COND_STRING, THREAD and EXTRA_STRING parameters.
9991 If INTERNAL is non-zero, the breakpoint number will be allocated
9992 from the internal breakpoint count. Returns true if any breakpoint
9993 was created; false otherwise. */
9994
9995 int
9996 create_breakpoint (struct gdbarch *gdbarch,
9997 char *arg, char *cond_string,
9998 int thread, char *extra_string,
9999 int parse_arg,
10000 int tempflag, enum bptype type_wanted,
10001 int ignore_count,
10002 enum auto_boolean pending_break_support,
10003 const struct breakpoint_ops *ops,
10004 int from_tty, int enabled, int internal,
10005 unsigned flags)
10006 {
10007 volatile struct gdb_exception e;
10008 char *copy_arg = NULL;
10009 char *addr_start = arg;
10010 struct linespec_result canonical;
10011 struct cleanup *old_chain;
10012 struct cleanup *bkpt_chain = NULL;
10013 int pending = 0;
10014 int task = 0;
10015 int prev_bkpt_count = breakpoint_count;
10016
10017 gdb_assert (ops != NULL);
10018
10019 init_linespec_result (&canonical);
10020
10021 TRY_CATCH (e, RETURN_MASK_ALL)
10022 {
10023 ops->create_sals_from_address (&arg, &canonical, type_wanted,
10024 addr_start, &copy_arg);
10025 }
10026
10027 /* If caller is interested in rc value from parse, set value. */
10028 switch (e.reason)
10029 {
10030 case GDB_NO_ERROR:
10031 if (VEC_empty (linespec_sals, canonical.sals))
10032 return 0;
10033 break;
10034 case RETURN_ERROR:
10035 switch (e.error)
10036 {
10037 case NOT_FOUND_ERROR:
10038
10039 /* If pending breakpoint support is turned off, throw
10040 error. */
10041
10042 if (pending_break_support == AUTO_BOOLEAN_FALSE)
10043 throw_exception (e);
10044
10045 exception_print (gdb_stderr, e);
10046
10047 /* If pending breakpoint support is auto query and the user
10048 selects no, then simply return the error code. */
10049 if (pending_break_support == AUTO_BOOLEAN_AUTO
10050 && !nquery (_("Make %s pending on future shared library load? "),
10051 bptype_string (type_wanted)))
10052 return 0;
10053
10054 /* At this point, either the user was queried about setting
10055 a pending breakpoint and selected yes, or pending
10056 breakpoint behavior is on and thus a pending breakpoint
10057 is defaulted on behalf of the user. */
10058 {
10059 struct linespec_sals lsal;
10060
10061 copy_arg = xstrdup (addr_start);
10062 lsal.canonical = xstrdup (copy_arg);
10063 lsal.sals.nelts = 1;
10064 lsal.sals.sals = XNEW (struct symtab_and_line);
10065 init_sal (&lsal.sals.sals[0]);
10066 pending = 1;
10067 VEC_safe_push (linespec_sals, canonical.sals, &lsal);
10068 }
10069 break;
10070 default:
10071 throw_exception (e);
10072 }
10073 break;
10074 default:
10075 throw_exception (e);
10076 }
10077
10078 /* Create a chain of things that always need to be cleaned up. */
10079 old_chain = make_cleanup_destroy_linespec_result (&canonical);
10080
10081 /* ----------------------------- SNIP -----------------------------
10082 Anything added to the cleanup chain beyond this point is assumed
10083 to be part of a breakpoint. If the breakpoint create succeeds
10084 then the memory is not reclaimed. */
10085 bkpt_chain = make_cleanup (null_cleanup, 0);
10086
10087 /* Resolve all line numbers to PC's and verify that the addresses
10088 are ok for the target. */
10089 if (!pending)
10090 {
10091 int ix;
10092 struct linespec_sals *iter;
10093
10094 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
10095 breakpoint_sals_to_pc (&iter->sals);
10096 }
10097
10098 /* Fast tracepoints may have additional restrictions on location. */
10099 if (!pending && type_wanted == bp_fast_tracepoint)
10100 {
10101 int ix;
10102 struct linespec_sals *iter;
10103
10104 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
10105 check_fast_tracepoint_sals (gdbarch, &iter->sals);
10106 }
10107
10108 /* Verify that condition can be parsed, before setting any
10109 breakpoints. Allocate a separate condition expression for each
10110 breakpoint. */
10111 if (!pending)
10112 {
10113 if (parse_arg)
10114 {
10115 char *rest;
10116 struct linespec_sals *lsal;
10117
10118 lsal = VEC_index (linespec_sals, canonical.sals, 0);
10119
10120 /* Here we only parse 'arg' to separate condition
10121 from thread number, so parsing in context of first
10122 sal is OK. When setting the breakpoint we'll
10123 re-parse it in context of each sal. */
10124
10125 find_condition_and_thread (arg, lsal->sals.sals[0].pc, &cond_string,
10126 &thread, &task, &rest);
10127 if (cond_string)
10128 make_cleanup (xfree, cond_string);
10129 if (rest)
10130 make_cleanup (xfree, rest);
10131 if (rest)
10132 extra_string = rest;
10133 }
10134 else
10135 {
10136 if (*arg != '\0')
10137 error (_("Garbage '%s' at end of location"), arg);
10138
10139 /* Create a private copy of condition string. */
10140 if (cond_string)
10141 {
10142 cond_string = xstrdup (cond_string);
10143 make_cleanup (xfree, cond_string);
10144 }
10145 /* Create a private copy of any extra string. */
10146 if (extra_string)
10147 {
10148 extra_string = xstrdup (extra_string);
10149 make_cleanup (xfree, extra_string);
10150 }
10151 }
10152
10153 ops->create_breakpoints_sal (gdbarch, &canonical,
10154 cond_string, extra_string, type_wanted,
10155 tempflag ? disp_del : disp_donttouch,
10156 thread, task, ignore_count, ops,
10157 from_tty, enabled, internal, flags);
10158 }
10159 else
10160 {
10161 struct breakpoint *b;
10162
10163 make_cleanup (xfree, copy_arg);
10164
10165 if (is_tracepoint_type (type_wanted))
10166 {
10167 struct tracepoint *t;
10168
10169 t = XCNEW (struct tracepoint);
10170 b = &t->base;
10171 }
10172 else
10173 b = XNEW (struct breakpoint);
10174
10175 init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops);
10176
10177 b->addr_string = copy_arg;
10178 if (parse_arg)
10179 b->cond_string = NULL;
10180 else
10181 {
10182 /* Create a private copy of condition string. */
10183 if (cond_string)
10184 {
10185 cond_string = xstrdup (cond_string);
10186 make_cleanup (xfree, cond_string);
10187 }
10188 b->cond_string = cond_string;
10189 }
10190 b->extra_string = NULL;
10191 b->ignore_count = ignore_count;
10192 b->disposition = tempflag ? disp_del : disp_donttouch;
10193 b->condition_not_parsed = 1;
10194 b->enable_state = enabled ? bp_enabled : bp_disabled;
10195 if ((type_wanted != bp_breakpoint
10196 && type_wanted != bp_hardware_breakpoint) || thread != -1)
10197 b->pspace = current_program_space;
10198
10199 install_breakpoint (internal, b, 0);
10200 }
10201
10202 if (VEC_length (linespec_sals, canonical.sals) > 1)
10203 {
10204 warning (_("Multiple breakpoints were set.\nUse the "
10205 "\"delete\" command to delete unwanted breakpoints."));
10206 prev_breakpoint_count = prev_bkpt_count;
10207 }
10208
10209 /* That's it. Discard the cleanups for data inserted into the
10210 breakpoint. */
10211 discard_cleanups (bkpt_chain);
10212 /* But cleanup everything else. */
10213 do_cleanups (old_chain);
10214
10215 /* error call may happen here - have BKPT_CHAIN already discarded. */
10216 update_global_location_list (UGLL_MAY_INSERT);
10217
10218 return 1;
10219 }
10220
10221 /* Set a breakpoint.
10222 ARG is a string describing breakpoint address,
10223 condition, and thread.
10224 FLAG specifies if a breakpoint is hardware on,
10225 and if breakpoint is temporary, using BP_HARDWARE_FLAG
10226 and BP_TEMPFLAG. */
10227
10228 static void
10229 break_command_1 (char *arg, int flag, int from_tty)
10230 {
10231 int tempflag = flag & BP_TEMPFLAG;
10232 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
10233 ? bp_hardware_breakpoint
10234 : bp_breakpoint);
10235 struct breakpoint_ops *ops;
10236 const char *arg_cp = arg;
10237
10238 /* Matching breakpoints on probes. */
10239 if (arg && probe_linespec_to_ops (&arg_cp) != NULL)
10240 ops = &bkpt_probe_breakpoint_ops;
10241 else
10242 ops = &bkpt_breakpoint_ops;
10243
10244 create_breakpoint (get_current_arch (),
10245 arg,
10246 NULL, 0, NULL, 1 /* parse arg */,
10247 tempflag, type_wanted,
10248 0 /* Ignore count */,
10249 pending_break_support,
10250 ops,
10251 from_tty,
10252 1 /* enabled */,
10253 0 /* internal */,
10254 0);
10255 }
10256
10257 /* Helper function for break_command_1 and disassemble_command. */
10258
10259 void
10260 resolve_sal_pc (struct symtab_and_line *sal)
10261 {
10262 CORE_ADDR pc;
10263
10264 if (sal->pc == 0 && sal->symtab != NULL)
10265 {
10266 if (!find_line_pc (sal->symtab, sal->line, &pc))
10267 error (_("No line %d in file \"%s\"."),
10268 sal->line, symtab_to_filename_for_display (sal->symtab));
10269 sal->pc = pc;
10270
10271 /* If this SAL corresponds to a breakpoint inserted using a line
10272 number, then skip the function prologue if necessary. */
10273 if (sal->explicit_line)
10274 skip_prologue_sal (sal);
10275 }
10276
10277 if (sal->section == 0 && sal->symtab != NULL)
10278 {
10279 const struct blockvector *bv;
10280 const struct block *b;
10281 struct symbol *sym;
10282
10283 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
10284 SYMTAB_COMPUNIT (sal->symtab));
10285 if (bv != NULL)
10286 {
10287 sym = block_linkage_function (b);
10288 if (sym != NULL)
10289 {
10290 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
10291 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
10292 sym);
10293 }
10294 else
10295 {
10296 /* It really is worthwhile to have the section, so we'll
10297 just have to look harder. This case can be executed
10298 if we have line numbers but no functions (as can
10299 happen in assembly source). */
10300
10301 struct bound_minimal_symbol msym;
10302 struct cleanup *old_chain = save_current_space_and_thread ();
10303
10304 switch_to_program_space_and_thread (sal->pspace);
10305
10306 msym = lookup_minimal_symbol_by_pc (sal->pc);
10307 if (msym.minsym)
10308 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
10309
10310 do_cleanups (old_chain);
10311 }
10312 }
10313 }
10314 }
10315
10316 void
10317 break_command (char *arg, int from_tty)
10318 {
10319 break_command_1 (arg, 0, from_tty);
10320 }
10321
10322 void
10323 tbreak_command (char *arg, int from_tty)
10324 {
10325 break_command_1 (arg, BP_TEMPFLAG, from_tty);
10326 }
10327
10328 static void
10329 hbreak_command (char *arg, int from_tty)
10330 {
10331 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
10332 }
10333
10334 static void
10335 thbreak_command (char *arg, int from_tty)
10336 {
10337 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
10338 }
10339
10340 static void
10341 stop_command (char *arg, int from_tty)
10342 {
10343 printf_filtered (_("Specify the type of breakpoint to set.\n\
10344 Usage: stop in <function | address>\n\
10345 stop at <line>\n"));
10346 }
10347
10348 static void
10349 stopin_command (char *arg, int from_tty)
10350 {
10351 int badInput = 0;
10352
10353 if (arg == (char *) NULL)
10354 badInput = 1;
10355 else if (*arg != '*')
10356 {
10357 char *argptr = arg;
10358 int hasColon = 0;
10359
10360 /* Look for a ':'. If this is a line number specification, then
10361 say it is bad, otherwise, it should be an address or
10362 function/method name. */
10363 while (*argptr && !hasColon)
10364 {
10365 hasColon = (*argptr == ':');
10366 argptr++;
10367 }
10368
10369 if (hasColon)
10370 badInput = (*argptr != ':'); /* Not a class::method */
10371 else
10372 badInput = isdigit (*arg); /* a simple line number */
10373 }
10374
10375 if (badInput)
10376 printf_filtered (_("Usage: stop in <function | address>\n"));
10377 else
10378 break_command_1 (arg, 0, from_tty);
10379 }
10380
10381 static void
10382 stopat_command (char *arg, int from_tty)
10383 {
10384 int badInput = 0;
10385
10386 if (arg == (char *) NULL || *arg == '*') /* no line number */
10387 badInput = 1;
10388 else
10389 {
10390 char *argptr = arg;
10391 int hasColon = 0;
10392
10393 /* Look for a ':'. If there is a '::' then get out, otherwise
10394 it is probably a line number. */
10395 while (*argptr && !hasColon)
10396 {
10397 hasColon = (*argptr == ':');
10398 argptr++;
10399 }
10400
10401 if (hasColon)
10402 badInput = (*argptr == ':'); /* we have class::method */
10403 else
10404 badInput = !isdigit (*arg); /* not a line number */
10405 }
10406
10407 if (badInput)
10408 printf_filtered (_("Usage: stop at <line>\n"));
10409 else
10410 break_command_1 (arg, 0, from_tty);
10411 }
10412
10413 /* The dynamic printf command is mostly like a regular breakpoint, but
10414 with a prewired command list consisting of a single output command,
10415 built from extra arguments supplied on the dprintf command
10416 line. */
10417
10418 static void
10419 dprintf_command (char *arg, int from_tty)
10420 {
10421 create_breakpoint (get_current_arch (),
10422 arg,
10423 NULL, 0, NULL, 1 /* parse arg */,
10424 0, bp_dprintf,
10425 0 /* Ignore count */,
10426 pending_break_support,
10427 &dprintf_breakpoint_ops,
10428 from_tty,
10429 1 /* enabled */,
10430 0 /* internal */,
10431 0);
10432 }
10433
10434 static void
10435 agent_printf_command (char *arg, int from_tty)
10436 {
10437 error (_("May only run agent-printf on the target"));
10438 }
10439
10440 /* Implement the "breakpoint_hit" breakpoint_ops method for
10441 ranged breakpoints. */
10442
10443 static int
10444 breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
10445 struct address_space *aspace,
10446 CORE_ADDR bp_addr,
10447 const struct target_waitstatus *ws)
10448 {
10449 if (ws->kind != TARGET_WAITKIND_STOPPED
10450 || ws->value.sig != GDB_SIGNAL_TRAP)
10451 return 0;
10452
10453 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
10454 bl->length, aspace, bp_addr);
10455 }
10456
10457 /* Implement the "resources_needed" breakpoint_ops method for
10458 ranged breakpoints. */
10459
10460 static int
10461 resources_needed_ranged_breakpoint (const struct bp_location *bl)
10462 {
10463 return target_ranged_break_num_registers ();
10464 }
10465
10466 /* Implement the "print_it" breakpoint_ops method for
10467 ranged breakpoints. */
10468
10469 static enum print_stop_action
10470 print_it_ranged_breakpoint (bpstat bs)
10471 {
10472 struct breakpoint *b = bs->breakpoint_at;
10473 struct bp_location *bl = b->loc;
10474 struct ui_out *uiout = current_uiout;
10475
10476 gdb_assert (b->type == bp_hardware_breakpoint);
10477
10478 /* Ranged breakpoints have only one location. */
10479 gdb_assert (bl && bl->next == NULL);
10480
10481 annotate_breakpoint (b->number);
10482 if (b->disposition == disp_del)
10483 ui_out_text (uiout, "\nTemporary ranged breakpoint ");
10484 else
10485 ui_out_text (uiout, "\nRanged breakpoint ");
10486 if (ui_out_is_mi_like_p (uiout))
10487 {
10488 ui_out_field_string (uiout, "reason",
10489 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
10490 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
10491 }
10492 ui_out_field_int (uiout, "bkptno", b->number);
10493 ui_out_text (uiout, ", ");
10494
10495 return PRINT_SRC_AND_LOC;
10496 }
10497
10498 /* Implement the "print_one" breakpoint_ops method for
10499 ranged breakpoints. */
10500
10501 static void
10502 print_one_ranged_breakpoint (struct breakpoint *b,
10503 struct bp_location **last_loc)
10504 {
10505 struct bp_location *bl = b->loc;
10506 struct value_print_options opts;
10507 struct ui_out *uiout = current_uiout;
10508
10509 /* Ranged breakpoints have only one location. */
10510 gdb_assert (bl && bl->next == NULL);
10511
10512 get_user_print_options (&opts);
10513
10514 if (opts.addressprint)
10515 /* We don't print the address range here, it will be printed later
10516 by print_one_detail_ranged_breakpoint. */
10517 ui_out_field_skip (uiout, "addr");
10518 annotate_field (5);
10519 print_breakpoint_location (b, bl);
10520 *last_loc = bl;
10521 }
10522
10523 /* Implement the "print_one_detail" breakpoint_ops method for
10524 ranged breakpoints. */
10525
10526 static void
10527 print_one_detail_ranged_breakpoint (const struct breakpoint *b,
10528 struct ui_out *uiout)
10529 {
10530 CORE_ADDR address_start, address_end;
10531 struct bp_location *bl = b->loc;
10532 struct ui_file *stb = mem_fileopen ();
10533 struct cleanup *cleanup = make_cleanup_ui_file_delete (stb);
10534
10535 gdb_assert (bl);
10536
10537 address_start = bl->address;
10538 address_end = address_start + bl->length - 1;
10539
10540 ui_out_text (uiout, "\taddress range: ");
10541 fprintf_unfiltered (stb, "[%s, %s]",
10542 print_core_address (bl->gdbarch, address_start),
10543 print_core_address (bl->gdbarch, address_end));
10544 ui_out_field_stream (uiout, "addr", stb);
10545 ui_out_text (uiout, "\n");
10546
10547 do_cleanups (cleanup);
10548 }
10549
10550 /* Implement the "print_mention" breakpoint_ops method for
10551 ranged breakpoints. */
10552
10553 static void
10554 print_mention_ranged_breakpoint (struct breakpoint *b)
10555 {
10556 struct bp_location *bl = b->loc;
10557 struct ui_out *uiout = current_uiout;
10558
10559 gdb_assert (bl);
10560 gdb_assert (b->type == bp_hardware_breakpoint);
10561
10562 if (ui_out_is_mi_like_p (uiout))
10563 return;
10564
10565 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
10566 b->number, paddress (bl->gdbarch, bl->address),
10567 paddress (bl->gdbarch, bl->address + bl->length - 1));
10568 }
10569
10570 /* Implement the "print_recreate" breakpoint_ops method for
10571 ranged breakpoints. */
10572
10573 static void
10574 print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
10575 {
10576 fprintf_unfiltered (fp, "break-range %s, %s", b->addr_string,
10577 b->addr_string_range_end);
10578 print_recreate_thread (b, fp);
10579 }
10580
10581 /* The breakpoint_ops structure to be used in ranged breakpoints. */
10582
10583 static struct breakpoint_ops ranged_breakpoint_ops;
10584
10585 /* Find the address where the end of the breakpoint range should be
10586 placed, given the SAL of the end of the range. This is so that if
10587 the user provides a line number, the end of the range is set to the
10588 last instruction of the given line. */
10589
10590 static CORE_ADDR
10591 find_breakpoint_range_end (struct symtab_and_line sal)
10592 {
10593 CORE_ADDR end;
10594
10595 /* If the user provided a PC value, use it. Otherwise,
10596 find the address of the end of the given location. */
10597 if (sal.explicit_pc)
10598 end = sal.pc;
10599 else
10600 {
10601 int ret;
10602 CORE_ADDR start;
10603
10604 ret = find_line_pc_range (sal, &start, &end);
10605 if (!ret)
10606 error (_("Could not find location of the end of the range."));
10607
10608 /* find_line_pc_range returns the start of the next line. */
10609 end--;
10610 }
10611
10612 return end;
10613 }
10614
10615 /* Implement the "break-range" CLI command. */
10616
10617 static void
10618 break_range_command (char *arg, int from_tty)
10619 {
10620 char *arg_start, *addr_string_start, *addr_string_end;
10621 struct linespec_result canonical_start, canonical_end;
10622 int bp_count, can_use_bp, length;
10623 CORE_ADDR end;
10624 struct breakpoint *b;
10625 struct symtab_and_line sal_start, sal_end;
10626 struct cleanup *cleanup_bkpt;
10627 struct linespec_sals *lsal_start, *lsal_end;
10628
10629 /* We don't support software ranged breakpoints. */
10630 if (target_ranged_break_num_registers () < 0)
10631 error (_("This target does not support hardware ranged breakpoints."));
10632
10633 bp_count = hw_breakpoint_used_count ();
10634 bp_count += target_ranged_break_num_registers ();
10635 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10636 bp_count, 0);
10637 if (can_use_bp < 0)
10638 error (_("Hardware breakpoints used exceeds limit."));
10639
10640 arg = skip_spaces (arg);
10641 if (arg == NULL || arg[0] == '\0')
10642 error(_("No address range specified."));
10643
10644 init_linespec_result (&canonical_start);
10645
10646 arg_start = arg;
10647 parse_breakpoint_sals (&arg, &canonical_start);
10648
10649 cleanup_bkpt = make_cleanup_destroy_linespec_result (&canonical_start);
10650
10651 if (arg[0] != ',')
10652 error (_("Too few arguments."));
10653 else if (VEC_empty (linespec_sals, canonical_start.sals))
10654 error (_("Could not find location of the beginning of the range."));
10655
10656 lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
10657
10658 if (VEC_length (linespec_sals, canonical_start.sals) > 1
10659 || lsal_start->sals.nelts != 1)
10660 error (_("Cannot create a ranged breakpoint with multiple locations."));
10661
10662 sal_start = lsal_start->sals.sals[0];
10663 addr_string_start = savestring (arg_start, arg - arg_start);
10664 make_cleanup (xfree, addr_string_start);
10665
10666 arg++; /* Skip the comma. */
10667 arg = skip_spaces (arg);
10668
10669 /* Parse the end location. */
10670
10671 init_linespec_result (&canonical_end);
10672 arg_start = arg;
10673
10674 /* We call decode_line_full directly here instead of using
10675 parse_breakpoint_sals because we need to specify the start location's
10676 symtab and line as the default symtab and line for the end of the
10677 range. This makes it possible to have ranges like "foo.c:27, +14",
10678 where +14 means 14 lines from the start location. */
10679 decode_line_full (&arg, DECODE_LINE_FUNFIRSTLINE,
10680 sal_start.symtab, sal_start.line,
10681 &canonical_end, NULL, NULL);
10682
10683 make_cleanup_destroy_linespec_result (&canonical_end);
10684
10685 if (VEC_empty (linespec_sals, canonical_end.sals))
10686 error (_("Could not find location of the end of the range."));
10687
10688 lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
10689 if (VEC_length (linespec_sals, canonical_end.sals) > 1
10690 || lsal_end->sals.nelts != 1)
10691 error (_("Cannot create a ranged breakpoint with multiple locations."));
10692
10693 sal_end = lsal_end->sals.sals[0];
10694 addr_string_end = savestring (arg_start, arg - arg_start);
10695 make_cleanup (xfree, addr_string_end);
10696
10697 end = find_breakpoint_range_end (sal_end);
10698 if (sal_start.pc > end)
10699 error (_("Invalid address range, end precedes start."));
10700
10701 length = end - sal_start.pc + 1;
10702 if (length < 0)
10703 /* Length overflowed. */
10704 error (_("Address range too large."));
10705 else if (length == 1)
10706 {
10707 /* This range is simple enough to be handled by
10708 the `hbreak' command. */
10709 hbreak_command (addr_string_start, 1);
10710
10711 do_cleanups (cleanup_bkpt);
10712
10713 return;
10714 }
10715
10716 /* Now set up the breakpoint. */
10717 b = set_raw_breakpoint (get_current_arch (), sal_start,
10718 bp_hardware_breakpoint, &ranged_breakpoint_ops);
10719 set_breakpoint_count (breakpoint_count + 1);
10720 b->number = breakpoint_count;
10721 b->disposition = disp_donttouch;
10722 b->addr_string = xstrdup (addr_string_start);
10723 b->addr_string_range_end = xstrdup (addr_string_end);
10724 b->loc->length = length;
10725
10726 do_cleanups (cleanup_bkpt);
10727
10728 mention (b);
10729 observer_notify_breakpoint_created (b);
10730 update_global_location_list (UGLL_MAY_INSERT);
10731 }
10732
10733 /* Return non-zero if EXP is verified as constant. Returned zero
10734 means EXP is variable. Also the constant detection may fail for
10735 some constant expressions and in such case still falsely return
10736 zero. */
10737
10738 static int
10739 watchpoint_exp_is_const (const struct expression *exp)
10740 {
10741 int i = exp->nelts;
10742
10743 while (i > 0)
10744 {
10745 int oplenp, argsp;
10746
10747 /* We are only interested in the descriptor of each element. */
10748 operator_length (exp, i, &oplenp, &argsp);
10749 i -= oplenp;
10750
10751 switch (exp->elts[i].opcode)
10752 {
10753 case BINOP_ADD:
10754 case BINOP_SUB:
10755 case BINOP_MUL:
10756 case BINOP_DIV:
10757 case BINOP_REM:
10758 case BINOP_MOD:
10759 case BINOP_LSH:
10760 case BINOP_RSH:
10761 case BINOP_LOGICAL_AND:
10762 case BINOP_LOGICAL_OR:
10763 case BINOP_BITWISE_AND:
10764 case BINOP_BITWISE_IOR:
10765 case BINOP_BITWISE_XOR:
10766 case BINOP_EQUAL:
10767 case BINOP_NOTEQUAL:
10768 case BINOP_LESS:
10769 case BINOP_GTR:
10770 case BINOP_LEQ:
10771 case BINOP_GEQ:
10772 case BINOP_REPEAT:
10773 case BINOP_COMMA:
10774 case BINOP_EXP:
10775 case BINOP_MIN:
10776 case BINOP_MAX:
10777 case BINOP_INTDIV:
10778 case BINOP_CONCAT:
10779 case TERNOP_COND:
10780 case TERNOP_SLICE:
10781
10782 case OP_LONG:
10783 case OP_DOUBLE:
10784 case OP_DECFLOAT:
10785 case OP_LAST:
10786 case OP_COMPLEX:
10787 case OP_STRING:
10788 case OP_ARRAY:
10789 case OP_TYPE:
10790 case OP_TYPEOF:
10791 case OP_DECLTYPE:
10792 case OP_TYPEID:
10793 case OP_NAME:
10794 case OP_OBJC_NSSTRING:
10795
10796 case UNOP_NEG:
10797 case UNOP_LOGICAL_NOT:
10798 case UNOP_COMPLEMENT:
10799 case UNOP_ADDR:
10800 case UNOP_HIGH:
10801 case UNOP_CAST:
10802
10803 case UNOP_CAST_TYPE:
10804 case UNOP_REINTERPRET_CAST:
10805 case UNOP_DYNAMIC_CAST:
10806 /* Unary, binary and ternary operators: We have to check
10807 their operands. If they are constant, then so is the
10808 result of that operation. For instance, if A and B are
10809 determined to be constants, then so is "A + B".
10810
10811 UNOP_IND is one exception to the rule above, because the
10812 value of *ADDR is not necessarily a constant, even when
10813 ADDR is. */
10814 break;
10815
10816 case OP_VAR_VALUE:
10817 /* Check whether the associated symbol is a constant.
10818
10819 We use SYMBOL_CLASS rather than TYPE_CONST because it's
10820 possible that a buggy compiler could mark a variable as
10821 constant even when it is not, and TYPE_CONST would return
10822 true in this case, while SYMBOL_CLASS wouldn't.
10823
10824 We also have to check for function symbols because they
10825 are always constant. */
10826 {
10827 struct symbol *s = exp->elts[i + 2].symbol;
10828
10829 if (SYMBOL_CLASS (s) != LOC_BLOCK
10830 && SYMBOL_CLASS (s) != LOC_CONST
10831 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10832 return 0;
10833 break;
10834 }
10835
10836 /* The default action is to return 0 because we are using
10837 the optimistic approach here: If we don't know something,
10838 then it is not a constant. */
10839 default:
10840 return 0;
10841 }
10842 }
10843
10844 return 1;
10845 }
10846
10847 /* Implement the "dtor" breakpoint_ops method for watchpoints. */
10848
10849 static void
10850 dtor_watchpoint (struct breakpoint *self)
10851 {
10852 struct watchpoint *w = (struct watchpoint *) self;
10853
10854 xfree (w->cond_exp);
10855 xfree (w->exp);
10856 xfree (w->exp_string);
10857 xfree (w->exp_string_reparse);
10858 value_free (w->val);
10859
10860 base_breakpoint_ops.dtor (self);
10861 }
10862
10863 /* Implement the "re_set" breakpoint_ops method for watchpoints. */
10864
10865 static void
10866 re_set_watchpoint (struct breakpoint *b)
10867 {
10868 struct watchpoint *w = (struct watchpoint *) b;
10869
10870 /* Watchpoint can be either on expression using entirely global
10871 variables, or it can be on local variables.
10872
10873 Watchpoints of the first kind are never auto-deleted, and even
10874 persist across program restarts. Since they can use variables
10875 from shared libraries, we need to reparse expression as libraries
10876 are loaded and unloaded.
10877
10878 Watchpoints on local variables can also change meaning as result
10879 of solib event. For example, if a watchpoint uses both a local
10880 and a global variables in expression, it's a local watchpoint,
10881 but unloading of a shared library will make the expression
10882 invalid. This is not a very common use case, but we still
10883 re-evaluate expression, to avoid surprises to the user.
10884
10885 Note that for local watchpoints, we re-evaluate it only if
10886 watchpoints frame id is still valid. If it's not, it means the
10887 watchpoint is out of scope and will be deleted soon. In fact,
10888 I'm not sure we'll ever be called in this case.
10889
10890 If a local watchpoint's frame id is still valid, then
10891 w->exp_valid_block is likewise valid, and we can safely use it.
10892
10893 Don't do anything about disabled watchpoints, since they will be
10894 reevaluated again when enabled. */
10895 update_watchpoint (w, 1 /* reparse */);
10896 }
10897
10898 /* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10899
10900 static int
10901 insert_watchpoint (struct bp_location *bl)
10902 {
10903 struct watchpoint *w = (struct watchpoint *) bl->owner;
10904 int length = w->exact ? 1 : bl->length;
10905
10906 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
10907 w->cond_exp);
10908 }
10909
10910 /* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10911
10912 static int
10913 remove_watchpoint (struct bp_location *bl)
10914 {
10915 struct watchpoint *w = (struct watchpoint *) bl->owner;
10916 int length = w->exact ? 1 : bl->length;
10917
10918 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
10919 w->cond_exp);
10920 }
10921
10922 static int
10923 breakpoint_hit_watchpoint (const struct bp_location *bl,
10924 struct address_space *aspace, CORE_ADDR bp_addr,
10925 const struct target_waitstatus *ws)
10926 {
10927 struct breakpoint *b = bl->owner;
10928 struct watchpoint *w = (struct watchpoint *) b;
10929
10930 /* Continuable hardware watchpoints are treated as non-existent if the
10931 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10932 some data address). Otherwise gdb won't stop on a break instruction
10933 in the code (not from a breakpoint) when a hardware watchpoint has
10934 been defined. Also skip watchpoints which we know did not trigger
10935 (did not match the data address). */
10936 if (is_hardware_watchpoint (b)
10937 && w->watchpoint_triggered == watch_triggered_no)
10938 return 0;
10939
10940 return 1;
10941 }
10942
10943 static void
10944 check_status_watchpoint (bpstat bs)
10945 {
10946 gdb_assert (is_watchpoint (bs->breakpoint_at));
10947
10948 bpstat_check_watchpoint (bs);
10949 }
10950
10951 /* Implement the "resources_needed" breakpoint_ops method for
10952 hardware watchpoints. */
10953
10954 static int
10955 resources_needed_watchpoint (const struct bp_location *bl)
10956 {
10957 struct watchpoint *w = (struct watchpoint *) bl->owner;
10958 int length = w->exact? 1 : bl->length;
10959
10960 return target_region_ok_for_hw_watchpoint (bl->address, length);
10961 }
10962
10963 /* Implement the "works_in_software_mode" breakpoint_ops method for
10964 hardware watchpoints. */
10965
10966 static int
10967 works_in_software_mode_watchpoint (const struct breakpoint *b)
10968 {
10969 /* Read and access watchpoints only work with hardware support. */
10970 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
10971 }
10972
10973 static enum print_stop_action
10974 print_it_watchpoint (bpstat bs)
10975 {
10976 struct cleanup *old_chain;
10977 struct breakpoint *b;
10978 struct ui_file *stb;
10979 enum print_stop_action result;
10980 struct watchpoint *w;
10981 struct ui_out *uiout = current_uiout;
10982
10983 gdb_assert (bs->bp_location_at != NULL);
10984
10985 b = bs->breakpoint_at;
10986 w = (struct watchpoint *) b;
10987
10988 stb = mem_fileopen ();
10989 old_chain = make_cleanup_ui_file_delete (stb);
10990
10991 switch (b->type)
10992 {
10993 case bp_watchpoint:
10994 case bp_hardware_watchpoint:
10995 annotate_watchpoint (b->number);
10996 if (ui_out_is_mi_like_p (uiout))
10997 ui_out_field_string
10998 (uiout, "reason",
10999 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
11000 mention (b);
11001 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
11002 ui_out_text (uiout, "\nOld value = ");
11003 watchpoint_value_print (bs->old_val, stb);
11004 ui_out_field_stream (uiout, "old", stb);
11005 ui_out_text (uiout, "\nNew value = ");
11006 watchpoint_value_print (w->val, stb);
11007 ui_out_field_stream (uiout, "new", stb);
11008 ui_out_text (uiout, "\n");
11009 /* More than one watchpoint may have been triggered. */
11010 result = PRINT_UNKNOWN;
11011 break;
11012
11013 case bp_read_watchpoint:
11014 if (ui_out_is_mi_like_p (uiout))
11015 ui_out_field_string
11016 (uiout, "reason",
11017 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
11018 mention (b);
11019 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
11020 ui_out_text (uiout, "\nValue = ");
11021 watchpoint_value_print (w->val, stb);
11022 ui_out_field_stream (uiout, "value", stb);
11023 ui_out_text (uiout, "\n");
11024 result = PRINT_UNKNOWN;
11025 break;
11026
11027 case bp_access_watchpoint:
11028 if (bs->old_val != NULL)
11029 {
11030 annotate_watchpoint (b->number);
11031 if (ui_out_is_mi_like_p (uiout))
11032 ui_out_field_string
11033 (uiout, "reason",
11034 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
11035 mention (b);
11036 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
11037 ui_out_text (uiout, "\nOld value = ");
11038 watchpoint_value_print (bs->old_val, stb);
11039 ui_out_field_stream (uiout, "old", stb);
11040 ui_out_text (uiout, "\nNew value = ");
11041 }
11042 else
11043 {
11044 mention (b);
11045 if (ui_out_is_mi_like_p (uiout))
11046 ui_out_field_string
11047 (uiout, "reason",
11048 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
11049 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
11050 ui_out_text (uiout, "\nValue = ");
11051 }
11052 watchpoint_value_print (w->val, stb);
11053 ui_out_field_stream (uiout, "new", stb);
11054 ui_out_text (uiout, "\n");
11055 result = PRINT_UNKNOWN;
11056 break;
11057 default:
11058 result = PRINT_UNKNOWN;
11059 }
11060
11061 do_cleanups (old_chain);
11062 return result;
11063 }
11064
11065 /* Implement the "print_mention" breakpoint_ops method for hardware
11066 watchpoints. */
11067
11068 static void
11069 print_mention_watchpoint (struct breakpoint *b)
11070 {
11071 struct cleanup *ui_out_chain;
11072 struct watchpoint *w = (struct watchpoint *) b;
11073 struct ui_out *uiout = current_uiout;
11074
11075 switch (b->type)
11076 {
11077 case bp_watchpoint:
11078 ui_out_text (uiout, "Watchpoint ");
11079 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
11080 break;
11081 case bp_hardware_watchpoint:
11082 ui_out_text (uiout, "Hardware watchpoint ");
11083 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
11084 break;
11085 case bp_read_watchpoint:
11086 ui_out_text (uiout, "Hardware read watchpoint ");
11087 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
11088 break;
11089 case bp_access_watchpoint:
11090 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
11091 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
11092 break;
11093 default:
11094 internal_error (__FILE__, __LINE__,
11095 _("Invalid hardware watchpoint type."));
11096 }
11097
11098 ui_out_field_int (uiout, "number", b->number);
11099 ui_out_text (uiout, ": ");
11100 ui_out_field_string (uiout, "exp", w->exp_string);
11101 do_cleanups (ui_out_chain);
11102 }
11103
11104 /* Implement the "print_recreate" breakpoint_ops method for
11105 watchpoints. */
11106
11107 static void
11108 print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
11109 {
11110 struct watchpoint *w = (struct watchpoint *) b;
11111
11112 switch (b->type)
11113 {
11114 case bp_watchpoint:
11115 case bp_hardware_watchpoint:
11116 fprintf_unfiltered (fp, "watch");
11117 break;
11118 case bp_read_watchpoint:
11119 fprintf_unfiltered (fp, "rwatch");
11120 break;
11121 case bp_access_watchpoint:
11122 fprintf_unfiltered (fp, "awatch");
11123 break;
11124 default:
11125 internal_error (__FILE__, __LINE__,
11126 _("Invalid watchpoint type."));
11127 }
11128
11129 fprintf_unfiltered (fp, " %s", w->exp_string);
11130 print_recreate_thread (b, fp);
11131 }
11132
11133 /* Implement the "explains_signal" breakpoint_ops method for
11134 watchpoints. */
11135
11136 static int
11137 explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
11138 {
11139 /* A software watchpoint cannot cause a signal other than
11140 GDB_SIGNAL_TRAP. */
11141 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
11142 return 0;
11143
11144 return 1;
11145 }
11146
11147 /* The breakpoint_ops structure to be used in hardware watchpoints. */
11148
11149 static struct breakpoint_ops watchpoint_breakpoint_ops;
11150
11151 /* Implement the "insert" breakpoint_ops method for
11152 masked hardware watchpoints. */
11153
11154 static int
11155 insert_masked_watchpoint (struct bp_location *bl)
11156 {
11157 struct watchpoint *w = (struct watchpoint *) bl->owner;
11158
11159 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
11160 bl->watchpoint_type);
11161 }
11162
11163 /* Implement the "remove" breakpoint_ops method for
11164 masked hardware watchpoints. */
11165
11166 static int
11167 remove_masked_watchpoint (struct bp_location *bl)
11168 {
11169 struct watchpoint *w = (struct watchpoint *) bl->owner;
11170
11171 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
11172 bl->watchpoint_type);
11173 }
11174
11175 /* Implement the "resources_needed" breakpoint_ops method for
11176 masked hardware watchpoints. */
11177
11178 static int
11179 resources_needed_masked_watchpoint (const struct bp_location *bl)
11180 {
11181 struct watchpoint *w = (struct watchpoint *) bl->owner;
11182
11183 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
11184 }
11185
11186 /* Implement the "works_in_software_mode" breakpoint_ops method for
11187 masked hardware watchpoints. */
11188
11189 static int
11190 works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
11191 {
11192 return 0;
11193 }
11194
11195 /* Implement the "print_it" breakpoint_ops method for
11196 masked hardware watchpoints. */
11197
11198 static enum print_stop_action
11199 print_it_masked_watchpoint (bpstat bs)
11200 {
11201 struct breakpoint *b = bs->breakpoint_at;
11202 struct ui_out *uiout = current_uiout;
11203
11204 /* Masked watchpoints have only one location. */
11205 gdb_assert (b->loc && b->loc->next == NULL);
11206
11207 switch (b->type)
11208 {
11209 case bp_hardware_watchpoint:
11210 annotate_watchpoint (b->number);
11211 if (ui_out_is_mi_like_p (uiout))
11212 ui_out_field_string
11213 (uiout, "reason",
11214 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
11215 break;
11216
11217 case bp_read_watchpoint:
11218 if (ui_out_is_mi_like_p (uiout))
11219 ui_out_field_string
11220 (uiout, "reason",
11221 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
11222 break;
11223
11224 case bp_access_watchpoint:
11225 if (ui_out_is_mi_like_p (uiout))
11226 ui_out_field_string
11227 (uiout, "reason",
11228 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
11229 break;
11230 default:
11231 internal_error (__FILE__, __LINE__,
11232 _("Invalid hardware watchpoint type."));
11233 }
11234
11235 mention (b);
11236 ui_out_text (uiout, _("\n\
11237 Check the underlying instruction at PC for the memory\n\
11238 address and value which triggered this watchpoint.\n"));
11239 ui_out_text (uiout, "\n");
11240
11241 /* More than one watchpoint may have been triggered. */
11242 return PRINT_UNKNOWN;
11243 }
11244
11245 /* Implement the "print_one_detail" breakpoint_ops method for
11246 masked hardware watchpoints. */
11247
11248 static void
11249 print_one_detail_masked_watchpoint (const struct breakpoint *b,
11250 struct ui_out *uiout)
11251 {
11252 struct watchpoint *w = (struct watchpoint *) b;
11253
11254 /* Masked watchpoints have only one location. */
11255 gdb_assert (b->loc && b->loc->next == NULL);
11256
11257 ui_out_text (uiout, "\tmask ");
11258 ui_out_field_core_addr (uiout, "mask", b->loc->gdbarch, w->hw_wp_mask);
11259 ui_out_text (uiout, "\n");
11260 }
11261
11262 /* Implement the "print_mention" breakpoint_ops method for
11263 masked hardware watchpoints. */
11264
11265 static void
11266 print_mention_masked_watchpoint (struct breakpoint *b)
11267 {
11268 struct watchpoint *w = (struct watchpoint *) b;
11269 struct ui_out *uiout = current_uiout;
11270 struct cleanup *ui_out_chain;
11271
11272 switch (b->type)
11273 {
11274 case bp_hardware_watchpoint:
11275 ui_out_text (uiout, "Masked hardware watchpoint ");
11276 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
11277 break;
11278 case bp_read_watchpoint:
11279 ui_out_text (uiout, "Masked hardware read watchpoint ");
11280 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
11281 break;
11282 case bp_access_watchpoint:
11283 ui_out_text (uiout, "Masked hardware access (read/write) watchpoint ");
11284 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
11285 break;
11286 default:
11287 internal_error (__FILE__, __LINE__,
11288 _("Invalid hardware watchpoint type."));
11289 }
11290
11291 ui_out_field_int (uiout, "number", b->number);
11292 ui_out_text (uiout, ": ");
11293 ui_out_field_string (uiout, "exp", w->exp_string);
11294 do_cleanups (ui_out_chain);
11295 }
11296
11297 /* Implement the "print_recreate" breakpoint_ops method for
11298 masked hardware watchpoints. */
11299
11300 static void
11301 print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
11302 {
11303 struct watchpoint *w = (struct watchpoint *) b;
11304 char tmp[40];
11305
11306 switch (b->type)
11307 {
11308 case bp_hardware_watchpoint:
11309 fprintf_unfiltered (fp, "watch");
11310 break;
11311 case bp_read_watchpoint:
11312 fprintf_unfiltered (fp, "rwatch");
11313 break;
11314 case bp_access_watchpoint:
11315 fprintf_unfiltered (fp, "awatch");
11316 break;
11317 default:
11318 internal_error (__FILE__, __LINE__,
11319 _("Invalid hardware watchpoint type."));
11320 }
11321
11322 sprintf_vma (tmp, w->hw_wp_mask);
11323 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
11324 print_recreate_thread (b, fp);
11325 }
11326
11327 /* The breakpoint_ops structure to be used in masked hardware watchpoints. */
11328
11329 static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
11330
11331 /* Tell whether the given watchpoint is a masked hardware watchpoint. */
11332
11333 static int
11334 is_masked_watchpoint (const struct breakpoint *b)
11335 {
11336 return b->ops == &masked_watchpoint_breakpoint_ops;
11337 }
11338
11339 /* accessflag: hw_write: watch write,
11340 hw_read: watch read,
11341 hw_access: watch access (read or write) */
11342 static void
11343 watch_command_1 (const char *arg, int accessflag, int from_tty,
11344 int just_location, int internal)
11345 {
11346 volatile struct gdb_exception e;
11347 struct breakpoint *b, *scope_breakpoint = NULL;
11348 struct expression *exp;
11349 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
11350 struct value *val, *mark, *result;
11351 int saved_bitpos = 0, saved_bitsize = 0;
11352 struct frame_info *frame;
11353 const char *exp_start = NULL;
11354 const char *exp_end = NULL;
11355 const char *tok, *end_tok;
11356 int toklen = -1;
11357 const char *cond_start = NULL;
11358 const char *cond_end = NULL;
11359 enum bptype bp_type;
11360 int thread = -1;
11361 int pc = 0;
11362 /* Flag to indicate whether we are going to use masks for
11363 the hardware watchpoint. */
11364 int use_mask = 0;
11365 CORE_ADDR mask = 0;
11366 struct watchpoint *w;
11367 char *expression;
11368 struct cleanup *back_to;
11369
11370 /* Make sure that we actually have parameters to parse. */
11371 if (arg != NULL && arg[0] != '\0')
11372 {
11373 const char *value_start;
11374
11375 exp_end = arg + strlen (arg);
11376
11377 /* Look for "parameter value" pairs at the end
11378 of the arguments string. */
11379 for (tok = exp_end - 1; tok > arg; tok--)
11380 {
11381 /* Skip whitespace at the end of the argument list. */
11382 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11383 tok--;
11384
11385 /* Find the beginning of the last token.
11386 This is the value of the parameter. */
11387 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11388 tok--;
11389 value_start = tok + 1;
11390
11391 /* Skip whitespace. */
11392 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11393 tok--;
11394
11395 end_tok = tok;
11396
11397 /* Find the beginning of the second to last token.
11398 This is the parameter itself. */
11399 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11400 tok--;
11401 tok++;
11402 toklen = end_tok - tok + 1;
11403
11404 if (toklen == 6 && !strncmp (tok, "thread", 6))
11405 {
11406 /* At this point we've found a "thread" token, which means
11407 the user is trying to set a watchpoint that triggers
11408 only in a specific thread. */
11409 char *endp;
11410
11411 if (thread != -1)
11412 error(_("You can specify only one thread."));
11413
11414 /* Extract the thread ID from the next token. */
11415 thread = strtol (value_start, &endp, 0);
11416
11417 /* Check if the user provided a valid numeric value for the
11418 thread ID. */
11419 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
11420 error (_("Invalid thread ID specification %s."), value_start);
11421
11422 /* Check if the thread actually exists. */
11423 if (!valid_thread_id (thread))
11424 invalid_thread_id_error (thread);
11425 }
11426 else if (toklen == 4 && !strncmp (tok, "mask", 4))
11427 {
11428 /* We've found a "mask" token, which means the user wants to
11429 create a hardware watchpoint that is going to have the mask
11430 facility. */
11431 struct value *mask_value, *mark;
11432
11433 if (use_mask)
11434 error(_("You can specify only one mask."));
11435
11436 use_mask = just_location = 1;
11437
11438 mark = value_mark ();
11439 mask_value = parse_to_comma_and_eval (&value_start);
11440 mask = value_as_address (mask_value);
11441 value_free_to_mark (mark);
11442 }
11443 else
11444 /* We didn't recognize what we found. We should stop here. */
11445 break;
11446
11447 /* Truncate the string and get rid of the "parameter value" pair before
11448 the arguments string is parsed by the parse_exp_1 function. */
11449 exp_end = tok;
11450 }
11451 }
11452 else
11453 exp_end = arg;
11454
11455 /* Parse the rest of the arguments. From here on out, everything
11456 is in terms of a newly allocated string instead of the original
11457 ARG. */
11458 innermost_block = NULL;
11459 expression = savestring (arg, exp_end - arg);
11460 back_to = make_cleanup (xfree, expression);
11461 exp_start = arg = expression;
11462 exp = parse_exp_1 (&arg, 0, 0, 0);
11463 exp_end = arg;
11464 /* Remove trailing whitespace from the expression before saving it.
11465 This makes the eventual display of the expression string a bit
11466 prettier. */
11467 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
11468 --exp_end;
11469
11470 /* Checking if the expression is not constant. */
11471 if (watchpoint_exp_is_const (exp))
11472 {
11473 int len;
11474
11475 len = exp_end - exp_start;
11476 while (len > 0 && isspace (exp_start[len - 1]))
11477 len--;
11478 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
11479 }
11480
11481 exp_valid_block = innermost_block;
11482 mark = value_mark ();
11483 fetch_subexp_value (exp, &pc, &val, &result, NULL, just_location);
11484
11485 if (val != NULL && just_location)
11486 {
11487 saved_bitpos = value_bitpos (val);
11488 saved_bitsize = value_bitsize (val);
11489 }
11490
11491 if (just_location)
11492 {
11493 int ret;
11494
11495 exp_valid_block = NULL;
11496 val = value_addr (result);
11497 release_value (val);
11498 value_free_to_mark (mark);
11499
11500 if (use_mask)
11501 {
11502 ret = target_masked_watch_num_registers (value_as_address (val),
11503 mask);
11504 if (ret == -1)
11505 error (_("This target does not support masked watchpoints."));
11506 else if (ret == -2)
11507 error (_("Invalid mask or memory region."));
11508 }
11509 }
11510 else if (val != NULL)
11511 release_value (val);
11512
11513 tok = skip_spaces_const (arg);
11514 end_tok = skip_to_space_const (tok);
11515
11516 toklen = end_tok - tok;
11517 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
11518 {
11519 struct expression *cond;
11520
11521 innermost_block = NULL;
11522 tok = cond_start = end_tok + 1;
11523 cond = parse_exp_1 (&tok, 0, 0, 0);
11524
11525 /* The watchpoint expression may not be local, but the condition
11526 may still be. E.g.: `watch global if local > 0'. */
11527 cond_exp_valid_block = innermost_block;
11528
11529 xfree (cond);
11530 cond_end = tok;
11531 }
11532 if (*tok)
11533 error (_("Junk at end of command."));
11534
11535 frame = block_innermost_frame (exp_valid_block);
11536
11537 /* If the expression is "local", then set up a "watchpoint scope"
11538 breakpoint at the point where we've left the scope of the watchpoint
11539 expression. Create the scope breakpoint before the watchpoint, so
11540 that we will encounter it first in bpstat_stop_status. */
11541 if (exp_valid_block && frame)
11542 {
11543 if (frame_id_p (frame_unwind_caller_id (frame)))
11544 {
11545 scope_breakpoint
11546 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
11547 frame_unwind_caller_pc (frame),
11548 bp_watchpoint_scope,
11549 &momentary_breakpoint_ops);
11550
11551 scope_breakpoint->enable_state = bp_enabled;
11552
11553 /* Automatically delete the breakpoint when it hits. */
11554 scope_breakpoint->disposition = disp_del;
11555
11556 /* Only break in the proper frame (help with recursion). */
11557 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
11558
11559 /* Set the address at which we will stop. */
11560 scope_breakpoint->loc->gdbarch
11561 = frame_unwind_caller_arch (frame);
11562 scope_breakpoint->loc->requested_address
11563 = frame_unwind_caller_pc (frame);
11564 scope_breakpoint->loc->address
11565 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
11566 scope_breakpoint->loc->requested_address,
11567 scope_breakpoint->type);
11568 }
11569 }
11570
11571 /* Now set up the breakpoint. We create all watchpoints as hardware
11572 watchpoints here even if hardware watchpoints are turned off, a call
11573 to update_watchpoint later in this function will cause the type to
11574 drop back to bp_watchpoint (software watchpoint) if required. */
11575
11576 if (accessflag == hw_read)
11577 bp_type = bp_read_watchpoint;
11578 else if (accessflag == hw_access)
11579 bp_type = bp_access_watchpoint;
11580 else
11581 bp_type = bp_hardware_watchpoint;
11582
11583 w = XCNEW (struct watchpoint);
11584 b = &w->base;
11585 if (use_mask)
11586 init_raw_breakpoint_without_location (b, NULL, bp_type,
11587 &masked_watchpoint_breakpoint_ops);
11588 else
11589 init_raw_breakpoint_without_location (b, NULL, bp_type,
11590 &watchpoint_breakpoint_ops);
11591 b->thread = thread;
11592 b->disposition = disp_donttouch;
11593 b->pspace = current_program_space;
11594 w->exp = exp;
11595 w->exp_valid_block = exp_valid_block;
11596 w->cond_exp_valid_block = cond_exp_valid_block;
11597 if (just_location)
11598 {
11599 struct type *t = value_type (val);
11600 CORE_ADDR addr = value_as_address (val);
11601 char *name;
11602
11603 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
11604 name = type_to_string (t);
11605
11606 w->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
11607 core_addr_to_string (addr));
11608 xfree (name);
11609
11610 w->exp_string = xstrprintf ("-location %.*s",
11611 (int) (exp_end - exp_start), exp_start);
11612
11613 /* The above expression is in C. */
11614 b->language = language_c;
11615 }
11616 else
11617 w->exp_string = savestring (exp_start, exp_end - exp_start);
11618
11619 if (use_mask)
11620 {
11621 w->hw_wp_mask = mask;
11622 }
11623 else
11624 {
11625 w->val = val;
11626 w->val_bitpos = saved_bitpos;
11627 w->val_bitsize = saved_bitsize;
11628 w->val_valid = 1;
11629 }
11630
11631 if (cond_start)
11632 b->cond_string = savestring (cond_start, cond_end - cond_start);
11633 else
11634 b->cond_string = 0;
11635
11636 if (frame)
11637 {
11638 w->watchpoint_frame = get_frame_id (frame);
11639 w->watchpoint_thread = inferior_ptid;
11640 }
11641 else
11642 {
11643 w->watchpoint_frame = null_frame_id;
11644 w->watchpoint_thread = null_ptid;
11645 }
11646
11647 if (scope_breakpoint != NULL)
11648 {
11649 /* The scope breakpoint is related to the watchpoint. We will
11650 need to act on them together. */
11651 b->related_breakpoint = scope_breakpoint;
11652 scope_breakpoint->related_breakpoint = b;
11653 }
11654
11655 if (!just_location)
11656 value_free_to_mark (mark);
11657
11658 TRY_CATCH (e, RETURN_MASK_ALL)
11659 {
11660 /* Finally update the new watchpoint. This creates the locations
11661 that should be inserted. */
11662 update_watchpoint (w, 1);
11663 }
11664 if (e.reason < 0)
11665 {
11666 delete_breakpoint (b);
11667 throw_exception (e);
11668 }
11669
11670 install_breakpoint (internal, b, 1);
11671 do_cleanups (back_to);
11672 }
11673
11674 /* Return count of debug registers needed to watch the given expression.
11675 If the watchpoint cannot be handled in hardware return zero. */
11676
11677 static int
11678 can_use_hardware_watchpoint (struct value *v)
11679 {
11680 int found_memory_cnt = 0;
11681 struct value *head = v;
11682
11683 /* Did the user specifically forbid us to use hardware watchpoints? */
11684 if (!can_use_hw_watchpoints)
11685 return 0;
11686
11687 /* Make sure that the value of the expression depends only upon
11688 memory contents, and values computed from them within GDB. If we
11689 find any register references or function calls, we can't use a
11690 hardware watchpoint.
11691
11692 The idea here is that evaluating an expression generates a series
11693 of values, one holding the value of every subexpression. (The
11694 expression a*b+c has five subexpressions: a, b, a*b, c, and
11695 a*b+c.) GDB's values hold almost enough information to establish
11696 the criteria given above --- they identify memory lvalues,
11697 register lvalues, computed values, etcetera. So we can evaluate
11698 the expression, and then scan the chain of values that leaves
11699 behind to decide whether we can detect any possible change to the
11700 expression's final value using only hardware watchpoints.
11701
11702 However, I don't think that the values returned by inferior
11703 function calls are special in any way. So this function may not
11704 notice that an expression involving an inferior function call
11705 can't be watched with hardware watchpoints. FIXME. */
11706 for (; v; v = value_next (v))
11707 {
11708 if (VALUE_LVAL (v) == lval_memory)
11709 {
11710 if (v != head && value_lazy (v))
11711 /* A lazy memory lvalue in the chain is one that GDB never
11712 needed to fetch; we either just used its address (e.g.,
11713 `a' in `a.b') or we never needed it at all (e.g., `a'
11714 in `a,b'). This doesn't apply to HEAD; if that is
11715 lazy then it was not readable, but watch it anyway. */
11716 ;
11717 else
11718 {
11719 /* Ahh, memory we actually used! Check if we can cover
11720 it with hardware watchpoints. */
11721 struct type *vtype = check_typedef (value_type (v));
11722
11723 /* We only watch structs and arrays if user asked for it
11724 explicitly, never if they just happen to appear in a
11725 middle of some value chain. */
11726 if (v == head
11727 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11728 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11729 {
11730 CORE_ADDR vaddr = value_address (v);
11731 int len;
11732 int num_regs;
11733
11734 len = (target_exact_watchpoints
11735 && is_scalar_type_recursive (vtype))?
11736 1 : TYPE_LENGTH (value_type (v));
11737
11738 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11739 if (!num_regs)
11740 return 0;
11741 else
11742 found_memory_cnt += num_regs;
11743 }
11744 }
11745 }
11746 else if (VALUE_LVAL (v) != not_lval
11747 && deprecated_value_modifiable (v) == 0)
11748 return 0; /* These are values from the history (e.g., $1). */
11749 else if (VALUE_LVAL (v) == lval_register)
11750 return 0; /* Cannot watch a register with a HW watchpoint. */
11751 }
11752
11753 /* The expression itself looks suitable for using a hardware
11754 watchpoint, but give the target machine a chance to reject it. */
11755 return found_memory_cnt;
11756 }
11757
11758 void
11759 watch_command_wrapper (char *arg, int from_tty, int internal)
11760 {
11761 watch_command_1 (arg, hw_write, from_tty, 0, internal);
11762 }
11763
11764 /* A helper function that looks for the "-location" argument and then
11765 calls watch_command_1. */
11766
11767 static void
11768 watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11769 {
11770 int just_location = 0;
11771
11772 if (arg
11773 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11774 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11775 {
11776 arg = skip_spaces (arg);
11777 just_location = 1;
11778 }
11779
11780 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
11781 }
11782
11783 static void
11784 watch_command (char *arg, int from_tty)
11785 {
11786 watch_maybe_just_location (arg, hw_write, from_tty);
11787 }
11788
11789 void
11790 rwatch_command_wrapper (char *arg, int from_tty, int internal)
11791 {
11792 watch_command_1 (arg, hw_read, from_tty, 0, internal);
11793 }
11794
11795 static void
11796 rwatch_command (char *arg, int from_tty)
11797 {
11798 watch_maybe_just_location (arg, hw_read, from_tty);
11799 }
11800
11801 void
11802 awatch_command_wrapper (char *arg, int from_tty, int internal)
11803 {
11804 watch_command_1 (arg, hw_access, from_tty, 0, internal);
11805 }
11806
11807 static void
11808 awatch_command (char *arg, int from_tty)
11809 {
11810 watch_maybe_just_location (arg, hw_access, from_tty);
11811 }
11812 \f
11813
11814 /* Helper routines for the until_command routine in infcmd.c. Here
11815 because it uses the mechanisms of breakpoints. */
11816
11817 struct until_break_command_continuation_args
11818 {
11819 struct breakpoint *breakpoint;
11820 struct breakpoint *breakpoint2;
11821 int thread_num;
11822 };
11823
11824 /* This function is called by fetch_inferior_event via the
11825 cmd_continuation pointer, to complete the until command. It takes
11826 care of cleaning up the temporary breakpoints set up by the until
11827 command. */
11828 static void
11829 until_break_command_continuation (void *arg, int err)
11830 {
11831 struct until_break_command_continuation_args *a = arg;
11832
11833 delete_breakpoint (a->breakpoint);
11834 if (a->breakpoint2)
11835 delete_breakpoint (a->breakpoint2);
11836 delete_longjmp_breakpoint (a->thread_num);
11837 }
11838
11839 void
11840 until_break_command (char *arg, int from_tty, int anywhere)
11841 {
11842 struct symtabs_and_lines sals;
11843 struct symtab_and_line sal;
11844 struct frame_info *frame;
11845 struct gdbarch *frame_gdbarch;
11846 struct frame_id stack_frame_id;
11847 struct frame_id caller_frame_id;
11848 struct breakpoint *breakpoint;
11849 struct breakpoint *breakpoint2 = NULL;
11850 struct cleanup *old_chain;
11851 int thread;
11852 struct thread_info *tp;
11853
11854 clear_proceed_status (0);
11855
11856 /* Set a breakpoint where the user wants it and at return from
11857 this function. */
11858
11859 if (last_displayed_sal_is_valid ())
11860 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
11861 get_last_displayed_symtab (),
11862 get_last_displayed_line ());
11863 else
11864 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
11865 (struct symtab *) NULL, 0);
11866
11867 if (sals.nelts != 1)
11868 error (_("Couldn't get information on specified line."));
11869
11870 sal = sals.sals[0];
11871 xfree (sals.sals); /* malloc'd, so freed. */
11872
11873 if (*arg)
11874 error (_("Junk at end of arguments."));
11875
11876 resolve_sal_pc (&sal);
11877
11878 tp = inferior_thread ();
11879 thread = tp->num;
11880
11881 old_chain = make_cleanup (null_cleanup, NULL);
11882
11883 /* Note linespec handling above invalidates the frame chain.
11884 Installing a breakpoint also invalidates the frame chain (as it
11885 may need to switch threads), so do any frame handling before
11886 that. */
11887
11888 frame = get_selected_frame (NULL);
11889 frame_gdbarch = get_frame_arch (frame);
11890 stack_frame_id = get_stack_frame_id (frame);
11891 caller_frame_id = frame_unwind_caller_id (frame);
11892
11893 /* Keep within the current frame, or in frames called by the current
11894 one. */
11895
11896 if (frame_id_p (caller_frame_id))
11897 {
11898 struct symtab_and_line sal2;
11899
11900 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11901 sal2.pc = frame_unwind_caller_pc (frame);
11902 breakpoint2 = set_momentary_breakpoint (frame_unwind_caller_arch (frame),
11903 sal2,
11904 caller_frame_id,
11905 bp_until);
11906 make_cleanup_delete_breakpoint (breakpoint2);
11907
11908 set_longjmp_breakpoint (tp, caller_frame_id);
11909 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
11910 }
11911
11912 /* set_momentary_breakpoint could invalidate FRAME. */
11913 frame = NULL;
11914
11915 if (anywhere)
11916 /* If the user told us to continue until a specified location,
11917 we don't specify a frame at which we need to stop. */
11918 breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11919 null_frame_id, bp_until);
11920 else
11921 /* Otherwise, specify the selected frame, because we want to stop
11922 only at the very same frame. */
11923 breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11924 stack_frame_id, bp_until);
11925 make_cleanup_delete_breakpoint (breakpoint);
11926
11927 proceed (-1, GDB_SIGNAL_DEFAULT, 0);
11928
11929 /* If we are running asynchronously, and proceed call above has
11930 actually managed to start the target, arrange for breakpoints to
11931 be deleted when the target stops. Otherwise, we're already
11932 stopped and delete breakpoints via cleanup chain. */
11933
11934 if (target_can_async_p () && is_running (inferior_ptid))
11935 {
11936 struct until_break_command_continuation_args *args;
11937 args = xmalloc (sizeof (*args));
11938
11939 args->breakpoint = breakpoint;
11940 args->breakpoint2 = breakpoint2;
11941 args->thread_num = thread;
11942
11943 discard_cleanups (old_chain);
11944 add_continuation (inferior_thread (),
11945 until_break_command_continuation, args,
11946 xfree);
11947 }
11948 else
11949 do_cleanups (old_chain);
11950 }
11951
11952 /* This function attempts to parse an optional "if <cond>" clause
11953 from the arg string. If one is not found, it returns NULL.
11954
11955 Else, it returns a pointer to the condition string. (It does not
11956 attempt to evaluate the string against a particular block.) And,
11957 it updates arg to point to the first character following the parsed
11958 if clause in the arg string. */
11959
11960 char *
11961 ep_parse_optional_if_clause (char **arg)
11962 {
11963 char *cond_string;
11964
11965 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
11966 return NULL;
11967
11968 /* Skip the "if" keyword. */
11969 (*arg) += 2;
11970
11971 /* Skip any extra leading whitespace, and record the start of the
11972 condition string. */
11973 *arg = skip_spaces (*arg);
11974 cond_string = *arg;
11975
11976 /* Assume that the condition occupies the remainder of the arg
11977 string. */
11978 (*arg) += strlen (cond_string);
11979
11980 return cond_string;
11981 }
11982
11983 /* Commands to deal with catching events, such as signals, exceptions,
11984 process start/exit, etc. */
11985
11986 typedef enum
11987 {
11988 catch_fork_temporary, catch_vfork_temporary,
11989 catch_fork_permanent, catch_vfork_permanent
11990 }
11991 catch_fork_kind;
11992
11993 static void
11994 catch_fork_command_1 (char *arg, int from_tty,
11995 struct cmd_list_element *command)
11996 {
11997 struct gdbarch *gdbarch = get_current_arch ();
11998 char *cond_string = NULL;
11999 catch_fork_kind fork_kind;
12000 int tempflag;
12001
12002 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
12003 tempflag = (fork_kind == catch_fork_temporary
12004 || fork_kind == catch_vfork_temporary);
12005
12006 if (!arg)
12007 arg = "";
12008 arg = skip_spaces (arg);
12009
12010 /* The allowed syntax is:
12011 catch [v]fork
12012 catch [v]fork if <cond>
12013
12014 First, check if there's an if clause. */
12015 cond_string = ep_parse_optional_if_clause (&arg);
12016
12017 if ((*arg != '\0') && !isspace (*arg))
12018 error (_("Junk at end of arguments."));
12019
12020 /* If this target supports it, create a fork or vfork catchpoint
12021 and enable reporting of such events. */
12022 switch (fork_kind)
12023 {
12024 case catch_fork_temporary:
12025 case catch_fork_permanent:
12026 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
12027 &catch_fork_breakpoint_ops);
12028 break;
12029 case catch_vfork_temporary:
12030 case catch_vfork_permanent:
12031 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
12032 &catch_vfork_breakpoint_ops);
12033 break;
12034 default:
12035 error (_("unsupported or unknown fork kind; cannot catch it"));
12036 break;
12037 }
12038 }
12039
12040 static void
12041 catch_exec_command_1 (char *arg, int from_tty,
12042 struct cmd_list_element *command)
12043 {
12044 struct exec_catchpoint *c;
12045 struct gdbarch *gdbarch = get_current_arch ();
12046 int tempflag;
12047 char *cond_string = NULL;
12048
12049 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
12050
12051 if (!arg)
12052 arg = "";
12053 arg = skip_spaces (arg);
12054
12055 /* The allowed syntax is:
12056 catch exec
12057 catch exec if <cond>
12058
12059 First, check if there's an if clause. */
12060 cond_string = ep_parse_optional_if_clause (&arg);
12061
12062 if ((*arg != '\0') && !isspace (*arg))
12063 error (_("Junk at end of arguments."));
12064
12065 c = XNEW (struct exec_catchpoint);
12066 init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
12067 &catch_exec_breakpoint_ops);
12068 c->exec_pathname = NULL;
12069
12070 install_breakpoint (0, &c->base, 1);
12071 }
12072
12073 void
12074 init_ada_exception_breakpoint (struct breakpoint *b,
12075 struct gdbarch *gdbarch,
12076 struct symtab_and_line sal,
12077 char *addr_string,
12078 const struct breakpoint_ops *ops,
12079 int tempflag,
12080 int enabled,
12081 int from_tty)
12082 {
12083 if (from_tty)
12084 {
12085 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
12086 if (!loc_gdbarch)
12087 loc_gdbarch = gdbarch;
12088
12089 describe_other_breakpoints (loc_gdbarch,
12090 sal.pspace, sal.pc, sal.section, -1);
12091 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
12092 version for exception catchpoints, because two catchpoints
12093 used for different exception names will use the same address.
12094 In this case, a "breakpoint ... also set at..." warning is
12095 unproductive. Besides, the warning phrasing is also a bit
12096 inappropriate, we should use the word catchpoint, and tell
12097 the user what type of catchpoint it is. The above is good
12098 enough for now, though. */
12099 }
12100
12101 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
12102
12103 b->enable_state = enabled ? bp_enabled : bp_disabled;
12104 b->disposition = tempflag ? disp_del : disp_donttouch;
12105 b->addr_string = addr_string;
12106 b->language = language_ada;
12107 }
12108
12109 /* Splits the argument using space as delimiter. Returns an xmalloc'd
12110 filter list, or NULL if no filtering is required. */
12111 static VEC(int) *
12112 catch_syscall_split_args (char *arg)
12113 {
12114 VEC(int) *result = NULL;
12115 struct cleanup *cleanup = make_cleanup (VEC_cleanup (int), &result);
12116 struct gdbarch *gdbarch = target_gdbarch ();
12117
12118 while (*arg != '\0')
12119 {
12120 int i, syscall_number;
12121 char *endptr;
12122 char cur_name[128];
12123 struct syscall s;
12124
12125 /* Skip whitespace. */
12126 arg = skip_spaces (arg);
12127
12128 for (i = 0; i < 127 && arg[i] && !isspace (arg[i]); ++i)
12129 cur_name[i] = arg[i];
12130 cur_name[i] = '\0';
12131 arg += i;
12132
12133 /* Check if the user provided a syscall name or a number. */
12134 syscall_number = (int) strtol (cur_name, &endptr, 0);
12135 if (*endptr == '\0')
12136 get_syscall_by_number (gdbarch, syscall_number, &s);
12137 else
12138 {
12139 /* We have a name. Let's check if it's valid and convert it
12140 to a number. */
12141 get_syscall_by_name (gdbarch, cur_name, &s);
12142
12143 if (s.number == UNKNOWN_SYSCALL)
12144 /* Here we have to issue an error instead of a warning,
12145 because GDB cannot do anything useful if there's no
12146 syscall number to be caught. */
12147 error (_("Unknown syscall name '%s'."), cur_name);
12148 }
12149
12150 /* Ok, it's valid. */
12151 VEC_safe_push (int, result, s.number);
12152 }
12153
12154 discard_cleanups (cleanup);
12155 return result;
12156 }
12157
12158 /* Implement the "catch syscall" command. */
12159
12160 static void
12161 catch_syscall_command_1 (char *arg, int from_tty,
12162 struct cmd_list_element *command)
12163 {
12164 int tempflag;
12165 VEC(int) *filter;
12166 struct syscall s;
12167 struct gdbarch *gdbarch = get_current_arch ();
12168
12169 /* Checking if the feature if supported. */
12170 if (gdbarch_get_syscall_number_p (gdbarch) == 0)
12171 error (_("The feature 'catch syscall' is not supported on \
12172 this architecture yet."));
12173
12174 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
12175
12176 arg = skip_spaces (arg);
12177
12178 /* We need to do this first "dummy" translation in order
12179 to get the syscall XML file loaded or, most important,
12180 to display a warning to the user if there's no XML file
12181 for his/her architecture. */
12182 get_syscall_by_number (gdbarch, 0, &s);
12183
12184 /* The allowed syntax is:
12185 catch syscall
12186 catch syscall <name | number> [<name | number> ... <name | number>]
12187
12188 Let's check if there's a syscall name. */
12189
12190 if (arg != NULL)
12191 filter = catch_syscall_split_args (arg);
12192 else
12193 filter = NULL;
12194
12195 create_syscall_event_catchpoint (tempflag, filter,
12196 &catch_syscall_breakpoint_ops);
12197 }
12198
12199 static void
12200 catch_command (char *arg, int from_tty)
12201 {
12202 error (_("Catch requires an event name."));
12203 }
12204 \f
12205
12206 static void
12207 tcatch_command (char *arg, int from_tty)
12208 {
12209 error (_("Catch requires an event name."));
12210 }
12211
12212 /* A qsort comparison function that sorts breakpoints in order. */
12213
12214 static int
12215 compare_breakpoints (const void *a, const void *b)
12216 {
12217 const breakpoint_p *ba = a;
12218 uintptr_t ua = (uintptr_t) *ba;
12219 const breakpoint_p *bb = b;
12220 uintptr_t ub = (uintptr_t) *bb;
12221
12222 if ((*ba)->number < (*bb)->number)
12223 return -1;
12224 else if ((*ba)->number > (*bb)->number)
12225 return 1;
12226
12227 /* Now sort by address, in case we see, e..g, two breakpoints with
12228 the number 0. */
12229 if (ua < ub)
12230 return -1;
12231 return ua > ub ? 1 : 0;
12232 }
12233
12234 /* Delete breakpoints by address or line. */
12235
12236 static void
12237 clear_command (char *arg, int from_tty)
12238 {
12239 struct breakpoint *b, *prev;
12240 VEC(breakpoint_p) *found = 0;
12241 int ix;
12242 int default_match;
12243 struct symtabs_and_lines sals;
12244 struct symtab_and_line sal;
12245 int i;
12246 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
12247
12248 if (arg)
12249 {
12250 sals = decode_line_with_current_source (arg,
12251 (DECODE_LINE_FUNFIRSTLINE
12252 | DECODE_LINE_LIST_MODE));
12253 make_cleanup (xfree, sals.sals);
12254 default_match = 0;
12255 }
12256 else
12257 {
12258 sals.sals = (struct symtab_and_line *)
12259 xmalloc (sizeof (struct symtab_and_line));
12260 make_cleanup (xfree, sals.sals);
12261 init_sal (&sal); /* Initialize to zeroes. */
12262
12263 /* Set sal's line, symtab, pc, and pspace to the values
12264 corresponding to the last call to print_frame_info. If the
12265 codepoint is not valid, this will set all the fields to 0. */
12266 get_last_displayed_sal (&sal);
12267 if (sal.symtab == 0)
12268 error (_("No source file specified."));
12269
12270 sals.sals[0] = sal;
12271 sals.nelts = 1;
12272
12273 default_match = 1;
12274 }
12275
12276 /* We don't call resolve_sal_pc here. That's not as bad as it
12277 seems, because all existing breakpoints typically have both
12278 file/line and pc set. So, if clear is given file/line, we can
12279 match this to existing breakpoint without obtaining pc at all.
12280
12281 We only support clearing given the address explicitly
12282 present in breakpoint table. Say, we've set breakpoint
12283 at file:line. There were several PC values for that file:line,
12284 due to optimization, all in one block.
12285
12286 We've picked one PC value. If "clear" is issued with another
12287 PC corresponding to the same file:line, the breakpoint won't
12288 be cleared. We probably can still clear the breakpoint, but
12289 since the other PC value is never presented to user, user
12290 can only find it by guessing, and it does not seem important
12291 to support that. */
12292
12293 /* For each line spec given, delete bps which correspond to it. Do
12294 it in two passes, solely to preserve the current behavior that
12295 from_tty is forced true if we delete more than one
12296 breakpoint. */
12297
12298 found = NULL;
12299 make_cleanup (VEC_cleanup (breakpoint_p), &found);
12300 for (i = 0; i < sals.nelts; i++)
12301 {
12302 const char *sal_fullname;
12303
12304 /* If exact pc given, clear bpts at that pc.
12305 If line given (pc == 0), clear all bpts on specified line.
12306 If defaulting, clear all bpts on default line
12307 or at default pc.
12308
12309 defaulting sal.pc != 0 tests to do
12310
12311 0 1 pc
12312 1 1 pc _and_ line
12313 0 0 line
12314 1 0 <can't happen> */
12315
12316 sal = sals.sals[i];
12317 sal_fullname = (sal.symtab == NULL
12318 ? NULL : symtab_to_fullname (sal.symtab));
12319
12320 /* Find all matching breakpoints and add them to 'found'. */
12321 ALL_BREAKPOINTS (b)
12322 {
12323 int match = 0;
12324 /* Are we going to delete b? */
12325 if (b->type != bp_none && !is_watchpoint (b))
12326 {
12327 struct bp_location *loc = b->loc;
12328 for (; loc; loc = loc->next)
12329 {
12330 /* If the user specified file:line, don't allow a PC
12331 match. This matches historical gdb behavior. */
12332 int pc_match = (!sal.explicit_line
12333 && sal.pc
12334 && (loc->pspace == sal.pspace)
12335 && (loc->address == sal.pc)
12336 && (!section_is_overlay (loc->section)
12337 || loc->section == sal.section));
12338 int line_match = 0;
12339
12340 if ((default_match || sal.explicit_line)
12341 && loc->symtab != NULL
12342 && sal_fullname != NULL
12343 && sal.pspace == loc->pspace
12344 && loc->line_number == sal.line
12345 && filename_cmp (symtab_to_fullname (loc->symtab),
12346 sal_fullname) == 0)
12347 line_match = 1;
12348
12349 if (pc_match || line_match)
12350 {
12351 match = 1;
12352 break;
12353 }
12354 }
12355 }
12356
12357 if (match)
12358 VEC_safe_push(breakpoint_p, found, b);
12359 }
12360 }
12361
12362 /* Now go thru the 'found' chain and delete them. */
12363 if (VEC_empty(breakpoint_p, found))
12364 {
12365 if (arg)
12366 error (_("No breakpoint at %s."), arg);
12367 else
12368 error (_("No breakpoint at this line."));
12369 }
12370
12371 /* Remove duplicates from the vec. */
12372 qsort (VEC_address (breakpoint_p, found),
12373 VEC_length (breakpoint_p, found),
12374 sizeof (breakpoint_p),
12375 compare_breakpoints);
12376 prev = VEC_index (breakpoint_p, found, 0);
12377 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
12378 {
12379 if (b == prev)
12380 {
12381 VEC_ordered_remove (breakpoint_p, found, ix);
12382 --ix;
12383 }
12384 }
12385
12386 if (VEC_length(breakpoint_p, found) > 1)
12387 from_tty = 1; /* Always report if deleted more than one. */
12388 if (from_tty)
12389 {
12390 if (VEC_length(breakpoint_p, found) == 1)
12391 printf_unfiltered (_("Deleted breakpoint "));
12392 else
12393 printf_unfiltered (_("Deleted breakpoints "));
12394 }
12395
12396 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
12397 {
12398 if (from_tty)
12399 printf_unfiltered ("%d ", b->number);
12400 delete_breakpoint (b);
12401 }
12402 if (from_tty)
12403 putchar_unfiltered ('\n');
12404
12405 do_cleanups (cleanups);
12406 }
12407 \f
12408 /* Delete breakpoint in BS if they are `delete' breakpoints and
12409 all breakpoints that are marked for deletion, whether hit or not.
12410 This is called after any breakpoint is hit, or after errors. */
12411
12412 void
12413 breakpoint_auto_delete (bpstat bs)
12414 {
12415 struct breakpoint *b, *b_tmp;
12416
12417 for (; bs; bs = bs->next)
12418 if (bs->breakpoint_at
12419 && bs->breakpoint_at->disposition == disp_del
12420 && bs->stop)
12421 delete_breakpoint (bs->breakpoint_at);
12422
12423 ALL_BREAKPOINTS_SAFE (b, b_tmp)
12424 {
12425 if (b->disposition == disp_del_at_next_stop)
12426 delete_breakpoint (b);
12427 }
12428 }
12429
12430 /* A comparison function for bp_location AP and BP being interfaced to
12431 qsort. Sort elements primarily by their ADDRESS (no matter what
12432 does breakpoint_address_is_meaningful say for its OWNER),
12433 secondarily by ordering first permanent elements and
12434 terciarily just ensuring the array is sorted stable way despite
12435 qsort being an unstable algorithm. */
12436
12437 static int
12438 bp_location_compare (const void *ap, const void *bp)
12439 {
12440 struct bp_location *a = *(void **) ap;
12441 struct bp_location *b = *(void **) bp;
12442
12443 if (a->address != b->address)
12444 return (a->address > b->address) - (a->address < b->address);
12445
12446 /* Sort locations at the same address by their pspace number, keeping
12447 locations of the same inferior (in a multi-inferior environment)
12448 grouped. */
12449
12450 if (a->pspace->num != b->pspace->num)
12451 return ((a->pspace->num > b->pspace->num)
12452 - (a->pspace->num < b->pspace->num));
12453
12454 /* Sort permanent breakpoints first. */
12455 if (a->permanent != b->permanent)
12456 return (a->permanent < b->permanent) - (a->permanent > b->permanent);
12457
12458 /* Make the internal GDB representation stable across GDB runs
12459 where A and B memory inside GDB can differ. Breakpoint locations of
12460 the same type at the same address can be sorted in arbitrary order. */
12461
12462 if (a->owner->number != b->owner->number)
12463 return ((a->owner->number > b->owner->number)
12464 - (a->owner->number < b->owner->number));
12465
12466 return (a > b) - (a < b);
12467 }
12468
12469 /* Set bp_location_placed_address_before_address_max and
12470 bp_location_shadow_len_after_address_max according to the current
12471 content of the bp_location array. */
12472
12473 static void
12474 bp_location_target_extensions_update (void)
12475 {
12476 struct bp_location *bl, **blp_tmp;
12477
12478 bp_location_placed_address_before_address_max = 0;
12479 bp_location_shadow_len_after_address_max = 0;
12480
12481 ALL_BP_LOCATIONS (bl, blp_tmp)
12482 {
12483 CORE_ADDR start, end, addr;
12484
12485 if (!bp_location_has_shadow (bl))
12486 continue;
12487
12488 start = bl->target_info.placed_address;
12489 end = start + bl->target_info.shadow_len;
12490
12491 gdb_assert (bl->address >= start);
12492 addr = bl->address - start;
12493 if (addr > bp_location_placed_address_before_address_max)
12494 bp_location_placed_address_before_address_max = addr;
12495
12496 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
12497
12498 gdb_assert (bl->address < end);
12499 addr = end - bl->address;
12500 if (addr > bp_location_shadow_len_after_address_max)
12501 bp_location_shadow_len_after_address_max = addr;
12502 }
12503 }
12504
12505 /* Download tracepoint locations if they haven't been. */
12506
12507 static void
12508 download_tracepoint_locations (void)
12509 {
12510 struct breakpoint *b;
12511 struct cleanup *old_chain;
12512
12513 if (!target_can_download_tracepoint ())
12514 return;
12515
12516 old_chain = save_current_space_and_thread ();
12517
12518 ALL_TRACEPOINTS (b)
12519 {
12520 struct bp_location *bl;
12521 struct tracepoint *t;
12522 int bp_location_downloaded = 0;
12523
12524 if ((b->type == bp_fast_tracepoint
12525 ? !may_insert_fast_tracepoints
12526 : !may_insert_tracepoints))
12527 continue;
12528
12529 for (bl = b->loc; bl; bl = bl->next)
12530 {
12531 /* In tracepoint, locations are _never_ duplicated, so
12532 should_be_inserted is equivalent to
12533 unduplicated_should_be_inserted. */
12534 if (!should_be_inserted (bl) || bl->inserted)
12535 continue;
12536
12537 switch_to_program_space_and_thread (bl->pspace);
12538
12539 target_download_tracepoint (bl);
12540
12541 bl->inserted = 1;
12542 bp_location_downloaded = 1;
12543 }
12544 t = (struct tracepoint *) b;
12545 t->number_on_target = b->number;
12546 if (bp_location_downloaded)
12547 observer_notify_breakpoint_modified (b);
12548 }
12549
12550 do_cleanups (old_chain);
12551 }
12552
12553 /* Swap the insertion/duplication state between two locations. */
12554
12555 static void
12556 swap_insertion (struct bp_location *left, struct bp_location *right)
12557 {
12558 const int left_inserted = left->inserted;
12559 const int left_duplicate = left->duplicate;
12560 const int left_needs_update = left->needs_update;
12561 const struct bp_target_info left_target_info = left->target_info;
12562
12563 /* Locations of tracepoints can never be duplicated. */
12564 if (is_tracepoint (left->owner))
12565 gdb_assert (!left->duplicate);
12566 if (is_tracepoint (right->owner))
12567 gdb_assert (!right->duplicate);
12568
12569 left->inserted = right->inserted;
12570 left->duplicate = right->duplicate;
12571 left->needs_update = right->needs_update;
12572 left->target_info = right->target_info;
12573 right->inserted = left_inserted;
12574 right->duplicate = left_duplicate;
12575 right->needs_update = left_needs_update;
12576 right->target_info = left_target_info;
12577 }
12578
12579 /* Force the re-insertion of the locations at ADDRESS. This is called
12580 once a new/deleted/modified duplicate location is found and we are evaluating
12581 conditions on the target's side. Such conditions need to be updated on
12582 the target. */
12583
12584 static void
12585 force_breakpoint_reinsertion (struct bp_location *bl)
12586 {
12587 struct bp_location **locp = NULL, **loc2p;
12588 struct bp_location *loc;
12589 CORE_ADDR address = 0;
12590 int pspace_num;
12591
12592 address = bl->address;
12593 pspace_num = bl->pspace->num;
12594
12595 /* This is only meaningful if the target is
12596 evaluating conditions and if the user has
12597 opted for condition evaluation on the target's
12598 side. */
12599 if (gdb_evaluates_breakpoint_condition_p ()
12600 || !target_supports_evaluation_of_breakpoint_conditions ())
12601 return;
12602
12603 /* Flag all breakpoint locations with this address and
12604 the same program space as the location
12605 as "its condition has changed". We need to
12606 update the conditions on the target's side. */
12607 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
12608 {
12609 loc = *loc2p;
12610
12611 if (!is_breakpoint (loc->owner)
12612 || pspace_num != loc->pspace->num)
12613 continue;
12614
12615 /* Flag the location appropriately. We use a different state to
12616 let everyone know that we already updated the set of locations
12617 with addr bl->address and program space bl->pspace. This is so
12618 we don't have to keep calling these functions just to mark locations
12619 that have already been marked. */
12620 loc->condition_changed = condition_updated;
12621
12622 /* Free the agent expression bytecode as well. We will compute
12623 it later on. */
12624 if (loc->cond_bytecode)
12625 {
12626 free_agent_expr (loc->cond_bytecode);
12627 loc->cond_bytecode = NULL;
12628 }
12629 }
12630 }
12631 /* Called whether new breakpoints are created, or existing breakpoints
12632 deleted, to update the global location list and recompute which
12633 locations are duplicate of which.
12634
12635 The INSERT_MODE flag determines whether locations may not, may, or
12636 shall be inserted now. See 'enum ugll_insert_mode' for more
12637 info. */
12638
12639 static void
12640 update_global_location_list (enum ugll_insert_mode insert_mode)
12641 {
12642 struct breakpoint *b;
12643 struct bp_location **locp, *loc;
12644 struct cleanup *cleanups;
12645 /* Last breakpoint location address that was marked for update. */
12646 CORE_ADDR last_addr = 0;
12647 /* Last breakpoint location program space that was marked for update. */
12648 int last_pspace_num = -1;
12649
12650 /* Used in the duplicates detection below. When iterating over all
12651 bp_locations, points to the first bp_location of a given address.
12652 Breakpoints and watchpoints of different types are never
12653 duplicates of each other. Keep one pointer for each type of
12654 breakpoint/watchpoint, so we only need to loop over all locations
12655 once. */
12656 struct bp_location *bp_loc_first; /* breakpoint */
12657 struct bp_location *wp_loc_first; /* hardware watchpoint */
12658 struct bp_location *awp_loc_first; /* access watchpoint */
12659 struct bp_location *rwp_loc_first; /* read watchpoint */
12660
12661 /* Saved former bp_location array which we compare against the newly
12662 built bp_location from the current state of ALL_BREAKPOINTS. */
12663 struct bp_location **old_location, **old_locp;
12664 unsigned old_location_count;
12665
12666 old_location = bp_location;
12667 old_location_count = bp_location_count;
12668 bp_location = NULL;
12669 bp_location_count = 0;
12670 cleanups = make_cleanup (xfree, old_location);
12671
12672 ALL_BREAKPOINTS (b)
12673 for (loc = b->loc; loc; loc = loc->next)
12674 bp_location_count++;
12675
12676 bp_location = xmalloc (sizeof (*bp_location) * bp_location_count);
12677 locp = bp_location;
12678 ALL_BREAKPOINTS (b)
12679 for (loc = b->loc; loc; loc = loc->next)
12680 *locp++ = loc;
12681 qsort (bp_location, bp_location_count, sizeof (*bp_location),
12682 bp_location_compare);
12683
12684 bp_location_target_extensions_update ();
12685
12686 /* Identify bp_location instances that are no longer present in the
12687 new list, and therefore should be freed. Note that it's not
12688 necessary that those locations should be removed from inferior --
12689 if there's another location at the same address (previously
12690 marked as duplicate), we don't need to remove/insert the
12691 location.
12692
12693 LOCP is kept in sync with OLD_LOCP, each pointing to the current
12694 and former bp_location array state respectively. */
12695
12696 locp = bp_location;
12697 for (old_locp = old_location; old_locp < old_location + old_location_count;
12698 old_locp++)
12699 {
12700 struct bp_location *old_loc = *old_locp;
12701 struct bp_location **loc2p;
12702
12703 /* Tells if 'old_loc' is found among the new locations. If
12704 not, we have to free it. */
12705 int found_object = 0;
12706 /* Tells if the location should remain inserted in the target. */
12707 int keep_in_target = 0;
12708 int removed = 0;
12709
12710 /* Skip LOCP entries which will definitely never be needed.
12711 Stop either at or being the one matching OLD_LOC. */
12712 while (locp < bp_location + bp_location_count
12713 && (*locp)->address < old_loc->address)
12714 locp++;
12715
12716 for (loc2p = locp;
12717 (loc2p < bp_location + bp_location_count
12718 && (*loc2p)->address == old_loc->address);
12719 loc2p++)
12720 {
12721 /* Check if this is a new/duplicated location or a duplicated
12722 location that had its condition modified. If so, we want to send
12723 its condition to the target if evaluation of conditions is taking
12724 place there. */
12725 if ((*loc2p)->condition_changed == condition_modified
12726 && (last_addr != old_loc->address
12727 || last_pspace_num != old_loc->pspace->num))
12728 {
12729 force_breakpoint_reinsertion (*loc2p);
12730 last_pspace_num = old_loc->pspace->num;
12731 }
12732
12733 if (*loc2p == old_loc)
12734 found_object = 1;
12735 }
12736
12737 /* We have already handled this address, update it so that we don't
12738 have to go through updates again. */
12739 last_addr = old_loc->address;
12740
12741 /* Target-side condition evaluation: Handle deleted locations. */
12742 if (!found_object)
12743 force_breakpoint_reinsertion (old_loc);
12744
12745 /* If this location is no longer present, and inserted, look if
12746 there's maybe a new location at the same address. If so,
12747 mark that one inserted, and don't remove this one. This is
12748 needed so that we don't have a time window where a breakpoint
12749 at certain location is not inserted. */
12750
12751 if (old_loc->inserted)
12752 {
12753 /* If the location is inserted now, we might have to remove
12754 it. */
12755
12756 if (found_object && should_be_inserted (old_loc))
12757 {
12758 /* The location is still present in the location list,
12759 and still should be inserted. Don't do anything. */
12760 keep_in_target = 1;
12761 }
12762 else
12763 {
12764 /* This location still exists, but it won't be kept in the
12765 target since it may have been disabled. We proceed to
12766 remove its target-side condition. */
12767
12768 /* The location is either no longer present, or got
12769 disabled. See if there's another location at the
12770 same address, in which case we don't need to remove
12771 this one from the target. */
12772
12773 /* OLD_LOC comes from existing struct breakpoint. */
12774 if (breakpoint_address_is_meaningful (old_loc->owner))
12775 {
12776 for (loc2p = locp;
12777 (loc2p < bp_location + bp_location_count
12778 && (*loc2p)->address == old_loc->address);
12779 loc2p++)
12780 {
12781 struct bp_location *loc2 = *loc2p;
12782
12783 if (breakpoint_locations_match (loc2, old_loc))
12784 {
12785 /* Read watchpoint locations are switched to
12786 access watchpoints, if the former are not
12787 supported, but the latter are. */
12788 if (is_hardware_watchpoint (old_loc->owner))
12789 {
12790 gdb_assert (is_hardware_watchpoint (loc2->owner));
12791 loc2->watchpoint_type = old_loc->watchpoint_type;
12792 }
12793
12794 /* loc2 is a duplicated location. We need to check
12795 if it should be inserted in case it will be
12796 unduplicated. */
12797 if (loc2 != old_loc
12798 && unduplicated_should_be_inserted (loc2))
12799 {
12800 swap_insertion (old_loc, loc2);
12801 keep_in_target = 1;
12802 break;
12803 }
12804 }
12805 }
12806 }
12807 }
12808
12809 if (!keep_in_target)
12810 {
12811 if (remove_breakpoint (old_loc, mark_uninserted))
12812 {
12813 /* This is just about all we can do. We could keep
12814 this location on the global list, and try to
12815 remove it next time, but there's no particular
12816 reason why we will succeed next time.
12817
12818 Note that at this point, old_loc->owner is still
12819 valid, as delete_breakpoint frees the breakpoint
12820 only after calling us. */
12821 printf_filtered (_("warning: Error removing "
12822 "breakpoint %d\n"),
12823 old_loc->owner->number);
12824 }
12825 removed = 1;
12826 }
12827 }
12828
12829 if (!found_object)
12830 {
12831 if (removed && non_stop
12832 && need_moribund_for_location_type (old_loc))
12833 {
12834 /* This location was removed from the target. In
12835 non-stop mode, a race condition is possible where
12836 we've removed a breakpoint, but stop events for that
12837 breakpoint are already queued and will arrive later.
12838 We apply an heuristic to be able to distinguish such
12839 SIGTRAPs from other random SIGTRAPs: we keep this
12840 breakpoint location for a bit, and will retire it
12841 after we see some number of events. The theory here
12842 is that reporting of events should, "on the average",
12843 be fair, so after a while we'll see events from all
12844 threads that have anything of interest, and no longer
12845 need to keep this breakpoint location around. We
12846 don't hold locations forever so to reduce chances of
12847 mistaking a non-breakpoint SIGTRAP for a breakpoint
12848 SIGTRAP.
12849
12850 The heuristic failing can be disastrous on
12851 decr_pc_after_break targets.
12852
12853 On decr_pc_after_break targets, like e.g., x86-linux,
12854 if we fail to recognize a late breakpoint SIGTRAP,
12855 because events_till_retirement has reached 0 too
12856 soon, we'll fail to do the PC adjustment, and report
12857 a random SIGTRAP to the user. When the user resumes
12858 the inferior, it will most likely immediately crash
12859 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
12860 corrupted, because of being resumed e.g., in the
12861 middle of a multi-byte instruction, or skipped a
12862 one-byte instruction. This was actually seen happen
12863 on native x86-linux, and should be less rare on
12864 targets that do not support new thread events, like
12865 remote, due to the heuristic depending on
12866 thread_count.
12867
12868 Mistaking a random SIGTRAP for a breakpoint trap
12869 causes similar symptoms (PC adjustment applied when
12870 it shouldn't), but then again, playing with SIGTRAPs
12871 behind the debugger's back is asking for trouble.
12872
12873 Since hardware watchpoint traps are always
12874 distinguishable from other traps, so we don't need to
12875 apply keep hardware watchpoint moribund locations
12876 around. We simply always ignore hardware watchpoint
12877 traps we can no longer explain. */
12878
12879 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12880 old_loc->owner = NULL;
12881
12882 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
12883 }
12884 else
12885 {
12886 old_loc->owner = NULL;
12887 decref_bp_location (&old_loc);
12888 }
12889 }
12890 }
12891
12892 /* Rescan breakpoints at the same address and section, marking the
12893 first one as "first" and any others as "duplicates". This is so
12894 that the bpt instruction is only inserted once. If we have a
12895 permanent breakpoint at the same place as BPT, make that one the
12896 official one, and the rest as duplicates. Permanent breakpoints
12897 are sorted first for the same address.
12898
12899 Do the same for hardware watchpoints, but also considering the
12900 watchpoint's type (regular/access/read) and length. */
12901
12902 bp_loc_first = NULL;
12903 wp_loc_first = NULL;
12904 awp_loc_first = NULL;
12905 rwp_loc_first = NULL;
12906 ALL_BP_LOCATIONS (loc, locp)
12907 {
12908 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12909 non-NULL. */
12910 struct bp_location **loc_first_p;
12911 b = loc->owner;
12912
12913 if (!unduplicated_should_be_inserted (loc)
12914 || !breakpoint_address_is_meaningful (b)
12915 /* Don't detect duplicate for tracepoint locations because they are
12916 never duplicated. See the comments in field `duplicate' of
12917 `struct bp_location'. */
12918 || is_tracepoint (b))
12919 {
12920 /* Clear the condition modification flag. */
12921 loc->condition_changed = condition_unchanged;
12922 continue;
12923 }
12924
12925 /* Permanent breakpoint should always be inserted. */
12926 if (loc->permanent && ! loc->inserted)
12927 internal_error (__FILE__, __LINE__,
12928 _("allegedly permanent breakpoint is not "
12929 "actually inserted"));
12930
12931 if (b->type == bp_hardware_watchpoint)
12932 loc_first_p = &wp_loc_first;
12933 else if (b->type == bp_read_watchpoint)
12934 loc_first_p = &rwp_loc_first;
12935 else if (b->type == bp_access_watchpoint)
12936 loc_first_p = &awp_loc_first;
12937 else
12938 loc_first_p = &bp_loc_first;
12939
12940 if (*loc_first_p == NULL
12941 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12942 || !breakpoint_locations_match (loc, *loc_first_p))
12943 {
12944 *loc_first_p = loc;
12945 loc->duplicate = 0;
12946
12947 if (is_breakpoint (loc->owner) && loc->condition_changed)
12948 {
12949 loc->needs_update = 1;
12950 /* Clear the condition modification flag. */
12951 loc->condition_changed = condition_unchanged;
12952 }
12953 continue;
12954 }
12955
12956
12957 /* This and the above ensure the invariant that the first location
12958 is not duplicated, and is the inserted one.
12959 All following are marked as duplicated, and are not inserted. */
12960 if (loc->inserted)
12961 swap_insertion (loc, *loc_first_p);
12962 loc->duplicate = 1;
12963
12964 /* Clear the condition modification flag. */
12965 loc->condition_changed = condition_unchanged;
12966
12967 if (loc->inserted && !loc->permanent
12968 && (*loc_first_p)->permanent)
12969 internal_error (__FILE__, __LINE__,
12970 _("another breakpoint was inserted on top of "
12971 "a permanent breakpoint"));
12972 }
12973
12974 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
12975 {
12976 if (insert_mode != UGLL_DONT_INSERT)
12977 insert_breakpoint_locations ();
12978 else
12979 {
12980 /* Even though the caller told us to not insert new
12981 locations, we may still need to update conditions on the
12982 target's side of breakpoints that were already inserted
12983 if the target is evaluating breakpoint conditions. We
12984 only update conditions for locations that are marked
12985 "needs_update". */
12986 update_inserted_breakpoint_locations ();
12987 }
12988 }
12989
12990 if (insert_mode != UGLL_DONT_INSERT)
12991 download_tracepoint_locations ();
12992
12993 do_cleanups (cleanups);
12994 }
12995
12996 void
12997 breakpoint_retire_moribund (void)
12998 {
12999 struct bp_location *loc;
13000 int ix;
13001
13002 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
13003 if (--(loc->events_till_retirement) == 0)
13004 {
13005 decref_bp_location (&loc);
13006 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
13007 --ix;
13008 }
13009 }
13010
13011 static void
13012 update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
13013 {
13014 volatile struct gdb_exception e;
13015
13016 TRY_CATCH (e, RETURN_MASK_ERROR)
13017 update_global_location_list (insert_mode);
13018 }
13019
13020 /* Clear BKP from a BPS. */
13021
13022 static void
13023 bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
13024 {
13025 bpstat bs;
13026
13027 for (bs = bps; bs; bs = bs->next)
13028 if (bs->breakpoint_at == bpt)
13029 {
13030 bs->breakpoint_at = NULL;
13031 bs->old_val = NULL;
13032 /* bs->commands will be freed later. */
13033 }
13034 }
13035
13036 /* Callback for iterate_over_threads. */
13037 static int
13038 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
13039 {
13040 struct breakpoint *bpt = data;
13041
13042 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
13043 return 0;
13044 }
13045
13046 /* Helper for breakpoint and tracepoint breakpoint_ops->mention
13047 callbacks. */
13048
13049 static void
13050 say_where (struct breakpoint *b)
13051 {
13052 struct value_print_options opts;
13053
13054 get_user_print_options (&opts);
13055
13056 /* i18n: cagney/2005-02-11: Below needs to be merged into a
13057 single string. */
13058 if (b->loc == NULL)
13059 {
13060 printf_filtered (_(" (%s) pending."), b->addr_string);
13061 }
13062 else
13063 {
13064 if (opts.addressprint || b->loc->symtab == NULL)
13065 {
13066 printf_filtered (" at ");
13067 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
13068 gdb_stdout);
13069 }
13070 if (b->loc->symtab != NULL)
13071 {
13072 /* If there is a single location, we can print the location
13073 more nicely. */
13074 if (b->loc->next == NULL)
13075 printf_filtered (": file %s, line %d.",
13076 symtab_to_filename_for_display (b->loc->symtab),
13077 b->loc->line_number);
13078 else
13079 /* This is not ideal, but each location may have a
13080 different file name, and this at least reflects the
13081 real situation somewhat. */
13082 printf_filtered (": %s.", b->addr_string);
13083 }
13084
13085 if (b->loc->next)
13086 {
13087 struct bp_location *loc = b->loc;
13088 int n = 0;
13089 for (; loc; loc = loc->next)
13090 ++n;
13091 printf_filtered (" (%d locations)", n);
13092 }
13093 }
13094 }
13095
13096 /* Default bp_location_ops methods. */
13097
13098 static void
13099 bp_location_dtor (struct bp_location *self)
13100 {
13101 xfree (self->cond);
13102 if (self->cond_bytecode)
13103 free_agent_expr (self->cond_bytecode);
13104 xfree (self->function_name);
13105
13106 VEC_free (agent_expr_p, self->target_info.conditions);
13107 VEC_free (agent_expr_p, self->target_info.tcommands);
13108 }
13109
13110 static const struct bp_location_ops bp_location_ops =
13111 {
13112 bp_location_dtor
13113 };
13114
13115 /* Default breakpoint_ops methods all breakpoint_ops ultimately
13116 inherit from. */
13117
13118 static void
13119 base_breakpoint_dtor (struct breakpoint *self)
13120 {
13121 decref_counted_command_line (&self->commands);
13122 xfree (self->cond_string);
13123 xfree (self->extra_string);
13124 xfree (self->addr_string);
13125 xfree (self->filter);
13126 xfree (self->addr_string_range_end);
13127 }
13128
13129 static struct bp_location *
13130 base_breakpoint_allocate_location (struct breakpoint *self)
13131 {
13132 struct bp_location *loc;
13133
13134 loc = XNEW (struct bp_location);
13135 init_bp_location (loc, &bp_location_ops, self);
13136 return loc;
13137 }
13138
13139 static void
13140 base_breakpoint_re_set (struct breakpoint *b)
13141 {
13142 /* Nothing to re-set. */
13143 }
13144
13145 #define internal_error_pure_virtual_called() \
13146 gdb_assert_not_reached ("pure virtual function called")
13147
13148 static int
13149 base_breakpoint_insert_location (struct bp_location *bl)
13150 {
13151 internal_error_pure_virtual_called ();
13152 }
13153
13154 static int
13155 base_breakpoint_remove_location (struct bp_location *bl)
13156 {
13157 internal_error_pure_virtual_called ();
13158 }
13159
13160 static int
13161 base_breakpoint_breakpoint_hit (const struct bp_location *bl,
13162 struct address_space *aspace,
13163 CORE_ADDR bp_addr,
13164 const struct target_waitstatus *ws)
13165 {
13166 internal_error_pure_virtual_called ();
13167 }
13168
13169 static void
13170 base_breakpoint_check_status (bpstat bs)
13171 {
13172 /* Always stop. */
13173 }
13174
13175 /* A "works_in_software_mode" breakpoint_ops method that just internal
13176 errors. */
13177
13178 static int
13179 base_breakpoint_works_in_software_mode (const struct breakpoint *b)
13180 {
13181 internal_error_pure_virtual_called ();
13182 }
13183
13184 /* A "resources_needed" breakpoint_ops method that just internal
13185 errors. */
13186
13187 static int
13188 base_breakpoint_resources_needed (const struct bp_location *bl)
13189 {
13190 internal_error_pure_virtual_called ();
13191 }
13192
13193 static enum print_stop_action
13194 base_breakpoint_print_it (bpstat bs)
13195 {
13196 internal_error_pure_virtual_called ();
13197 }
13198
13199 static void
13200 base_breakpoint_print_one_detail (const struct breakpoint *self,
13201 struct ui_out *uiout)
13202 {
13203 /* nothing */
13204 }
13205
13206 static void
13207 base_breakpoint_print_mention (struct breakpoint *b)
13208 {
13209 internal_error_pure_virtual_called ();
13210 }
13211
13212 static void
13213 base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
13214 {
13215 internal_error_pure_virtual_called ();
13216 }
13217
13218 static void
13219 base_breakpoint_create_sals_from_address (char **arg,
13220 struct linespec_result *canonical,
13221 enum bptype type_wanted,
13222 char *addr_start,
13223 char **copy_arg)
13224 {
13225 internal_error_pure_virtual_called ();
13226 }
13227
13228 static void
13229 base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13230 struct linespec_result *c,
13231 char *cond_string,
13232 char *extra_string,
13233 enum bptype type_wanted,
13234 enum bpdisp disposition,
13235 int thread,
13236 int task, int ignore_count,
13237 const struct breakpoint_ops *o,
13238 int from_tty, int enabled,
13239 int internal, unsigned flags)
13240 {
13241 internal_error_pure_virtual_called ();
13242 }
13243
13244 static void
13245 base_breakpoint_decode_linespec (struct breakpoint *b, char **s,
13246 struct symtabs_and_lines *sals)
13247 {
13248 internal_error_pure_virtual_called ();
13249 }
13250
13251 /* The default 'explains_signal' method. */
13252
13253 static int
13254 base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
13255 {
13256 return 1;
13257 }
13258
13259 /* The default "after_condition_true" method. */
13260
13261 static void
13262 base_breakpoint_after_condition_true (struct bpstats *bs)
13263 {
13264 /* Nothing to do. */
13265 }
13266
13267 struct breakpoint_ops base_breakpoint_ops =
13268 {
13269 base_breakpoint_dtor,
13270 base_breakpoint_allocate_location,
13271 base_breakpoint_re_set,
13272 base_breakpoint_insert_location,
13273 base_breakpoint_remove_location,
13274 base_breakpoint_breakpoint_hit,
13275 base_breakpoint_check_status,
13276 base_breakpoint_resources_needed,
13277 base_breakpoint_works_in_software_mode,
13278 base_breakpoint_print_it,
13279 NULL,
13280 base_breakpoint_print_one_detail,
13281 base_breakpoint_print_mention,
13282 base_breakpoint_print_recreate,
13283 base_breakpoint_create_sals_from_address,
13284 base_breakpoint_create_breakpoints_sal,
13285 base_breakpoint_decode_linespec,
13286 base_breakpoint_explains_signal,
13287 base_breakpoint_after_condition_true,
13288 };
13289
13290 /* Default breakpoint_ops methods. */
13291
13292 static void
13293 bkpt_re_set (struct breakpoint *b)
13294 {
13295 /* FIXME: is this still reachable? */
13296 if (b->addr_string == NULL)
13297 {
13298 /* Anything without a string can't be re-set. */
13299 delete_breakpoint (b);
13300 return;
13301 }
13302
13303 breakpoint_re_set_default (b);
13304 }
13305
13306 static int
13307 bkpt_insert_location (struct bp_location *bl)
13308 {
13309 if (bl->loc_type == bp_loc_hardware_breakpoint)
13310 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
13311 else
13312 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
13313 }
13314
13315 static int
13316 bkpt_remove_location (struct bp_location *bl)
13317 {
13318 if (bl->loc_type == bp_loc_hardware_breakpoint)
13319 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
13320 else
13321 return target_remove_breakpoint (bl->gdbarch, &bl->target_info);
13322 }
13323
13324 static int
13325 bkpt_breakpoint_hit (const struct bp_location *bl,
13326 struct address_space *aspace, CORE_ADDR bp_addr,
13327 const struct target_waitstatus *ws)
13328 {
13329 if (ws->kind != TARGET_WAITKIND_STOPPED
13330 || ws->value.sig != GDB_SIGNAL_TRAP)
13331 return 0;
13332
13333 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
13334 aspace, bp_addr))
13335 return 0;
13336
13337 if (overlay_debugging /* unmapped overlay section */
13338 && section_is_overlay (bl->section)
13339 && !section_is_mapped (bl->section))
13340 return 0;
13341
13342 return 1;
13343 }
13344
13345 static int
13346 dprintf_breakpoint_hit (const struct bp_location *bl,
13347 struct address_space *aspace, CORE_ADDR bp_addr,
13348 const struct target_waitstatus *ws)
13349 {
13350 if (dprintf_style == dprintf_style_agent
13351 && target_can_run_breakpoint_commands ())
13352 {
13353 /* An agent-style dprintf never causes a stop. If we see a trap
13354 for this address it must be for a breakpoint that happens to
13355 be set at the same address. */
13356 return 0;
13357 }
13358
13359 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
13360 }
13361
13362 static int
13363 bkpt_resources_needed (const struct bp_location *bl)
13364 {
13365 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
13366
13367 return 1;
13368 }
13369
13370 static enum print_stop_action
13371 bkpt_print_it (bpstat bs)
13372 {
13373 struct breakpoint *b;
13374 const struct bp_location *bl;
13375 int bp_temp;
13376 struct ui_out *uiout = current_uiout;
13377
13378 gdb_assert (bs->bp_location_at != NULL);
13379
13380 bl = bs->bp_location_at;
13381 b = bs->breakpoint_at;
13382
13383 bp_temp = b->disposition == disp_del;
13384 if (bl->address != bl->requested_address)
13385 breakpoint_adjustment_warning (bl->requested_address,
13386 bl->address,
13387 b->number, 1);
13388 annotate_breakpoint (b->number);
13389 if (bp_temp)
13390 ui_out_text (uiout, "\nTemporary breakpoint ");
13391 else
13392 ui_out_text (uiout, "\nBreakpoint ");
13393 if (ui_out_is_mi_like_p (uiout))
13394 {
13395 ui_out_field_string (uiout, "reason",
13396 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
13397 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
13398 }
13399 ui_out_field_int (uiout, "bkptno", b->number);
13400 ui_out_text (uiout, ", ");
13401
13402 return PRINT_SRC_AND_LOC;
13403 }
13404
13405 static void
13406 bkpt_print_mention (struct breakpoint *b)
13407 {
13408 if (ui_out_is_mi_like_p (current_uiout))
13409 return;
13410
13411 switch (b->type)
13412 {
13413 case bp_breakpoint:
13414 case bp_gnu_ifunc_resolver:
13415 if (b->disposition == disp_del)
13416 printf_filtered (_("Temporary breakpoint"));
13417 else
13418 printf_filtered (_("Breakpoint"));
13419 printf_filtered (_(" %d"), b->number);
13420 if (b->type == bp_gnu_ifunc_resolver)
13421 printf_filtered (_(" at gnu-indirect-function resolver"));
13422 break;
13423 case bp_hardware_breakpoint:
13424 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
13425 break;
13426 case bp_dprintf:
13427 printf_filtered (_("Dprintf %d"), b->number);
13428 break;
13429 }
13430
13431 say_where (b);
13432 }
13433
13434 static void
13435 bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13436 {
13437 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
13438 fprintf_unfiltered (fp, "tbreak");
13439 else if (tp->type == bp_breakpoint)
13440 fprintf_unfiltered (fp, "break");
13441 else if (tp->type == bp_hardware_breakpoint
13442 && tp->disposition == disp_del)
13443 fprintf_unfiltered (fp, "thbreak");
13444 else if (tp->type == bp_hardware_breakpoint)
13445 fprintf_unfiltered (fp, "hbreak");
13446 else
13447 internal_error (__FILE__, __LINE__,
13448 _("unhandled breakpoint type %d"), (int) tp->type);
13449
13450 fprintf_unfiltered (fp, " %s", tp->addr_string);
13451 print_recreate_thread (tp, fp);
13452 }
13453
13454 static void
13455 bkpt_create_sals_from_address (char **arg,
13456 struct linespec_result *canonical,
13457 enum bptype type_wanted,
13458 char *addr_start, char **copy_arg)
13459 {
13460 create_sals_from_address_default (arg, canonical, type_wanted,
13461 addr_start, copy_arg);
13462 }
13463
13464 static void
13465 bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
13466 struct linespec_result *canonical,
13467 char *cond_string,
13468 char *extra_string,
13469 enum bptype type_wanted,
13470 enum bpdisp disposition,
13471 int thread,
13472 int task, int ignore_count,
13473 const struct breakpoint_ops *ops,
13474 int from_tty, int enabled,
13475 int internal, unsigned flags)
13476 {
13477 create_breakpoints_sal_default (gdbarch, canonical,
13478 cond_string, extra_string,
13479 type_wanted,
13480 disposition, thread, task,
13481 ignore_count, ops, from_tty,
13482 enabled, internal, flags);
13483 }
13484
13485 static void
13486 bkpt_decode_linespec (struct breakpoint *b, char **s,
13487 struct symtabs_and_lines *sals)
13488 {
13489 decode_linespec_default (b, s, sals);
13490 }
13491
13492 /* Virtual table for internal breakpoints. */
13493
13494 static void
13495 internal_bkpt_re_set (struct breakpoint *b)
13496 {
13497 switch (b->type)
13498 {
13499 /* Delete overlay event and longjmp master breakpoints; they
13500 will be reset later by breakpoint_re_set. */
13501 case bp_overlay_event:
13502 case bp_longjmp_master:
13503 case bp_std_terminate_master:
13504 case bp_exception_master:
13505 delete_breakpoint (b);
13506 break;
13507
13508 /* This breakpoint is special, it's set up when the inferior
13509 starts and we really don't want to touch it. */
13510 case bp_shlib_event:
13511
13512 /* Like bp_shlib_event, this breakpoint type is special. Once
13513 it is set up, we do not want to touch it. */
13514 case bp_thread_event:
13515 break;
13516 }
13517 }
13518
13519 static void
13520 internal_bkpt_check_status (bpstat bs)
13521 {
13522 if (bs->breakpoint_at->type == bp_shlib_event)
13523 {
13524 /* If requested, stop when the dynamic linker notifies GDB of
13525 events. This allows the user to get control and place
13526 breakpoints in initializer routines for dynamically loaded
13527 objects (among other things). */
13528 bs->stop = stop_on_solib_events;
13529 bs->print = stop_on_solib_events;
13530 }
13531 else
13532 bs->stop = 0;
13533 }
13534
13535 static enum print_stop_action
13536 internal_bkpt_print_it (bpstat bs)
13537 {
13538 struct breakpoint *b;
13539
13540 b = bs->breakpoint_at;
13541
13542 switch (b->type)
13543 {
13544 case bp_shlib_event:
13545 /* Did we stop because the user set the stop_on_solib_events
13546 variable? (If so, we report this as a generic, "Stopped due
13547 to shlib event" message.) */
13548 print_solib_event (0);
13549 break;
13550
13551 case bp_thread_event:
13552 /* Not sure how we will get here.
13553 GDB should not stop for these breakpoints. */
13554 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
13555 break;
13556
13557 case bp_overlay_event:
13558 /* By analogy with the thread event, GDB should not stop for these. */
13559 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
13560 break;
13561
13562 case bp_longjmp_master:
13563 /* These should never be enabled. */
13564 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
13565 break;
13566
13567 case bp_std_terminate_master:
13568 /* These should never be enabled. */
13569 printf_filtered (_("std::terminate Master Breakpoint: "
13570 "gdb should not stop!\n"));
13571 break;
13572
13573 case bp_exception_master:
13574 /* These should never be enabled. */
13575 printf_filtered (_("Exception Master Breakpoint: "
13576 "gdb should not stop!\n"));
13577 break;
13578 }
13579
13580 return PRINT_NOTHING;
13581 }
13582
13583 static void
13584 internal_bkpt_print_mention (struct breakpoint *b)
13585 {
13586 /* Nothing to mention. These breakpoints are internal. */
13587 }
13588
13589 /* Virtual table for momentary breakpoints */
13590
13591 static void
13592 momentary_bkpt_re_set (struct breakpoint *b)
13593 {
13594 /* Keep temporary breakpoints, which can be encountered when we step
13595 over a dlopen call and solib_add is resetting the breakpoints.
13596 Otherwise these should have been blown away via the cleanup chain
13597 or by breakpoint_init_inferior when we rerun the executable. */
13598 }
13599
13600 static void
13601 momentary_bkpt_check_status (bpstat bs)
13602 {
13603 /* Nothing. The point of these breakpoints is causing a stop. */
13604 }
13605
13606 static enum print_stop_action
13607 momentary_bkpt_print_it (bpstat bs)
13608 {
13609 struct ui_out *uiout = current_uiout;
13610
13611 if (ui_out_is_mi_like_p (uiout))
13612 {
13613 struct breakpoint *b = bs->breakpoint_at;
13614
13615 switch (b->type)
13616 {
13617 case bp_finish:
13618 ui_out_field_string
13619 (uiout, "reason",
13620 async_reason_lookup (EXEC_ASYNC_FUNCTION_FINISHED));
13621 break;
13622
13623 case bp_until:
13624 ui_out_field_string
13625 (uiout, "reason",
13626 async_reason_lookup (EXEC_ASYNC_LOCATION_REACHED));
13627 break;
13628 }
13629 }
13630
13631 return PRINT_UNKNOWN;
13632 }
13633
13634 static void
13635 momentary_bkpt_print_mention (struct breakpoint *b)
13636 {
13637 /* Nothing to mention. These breakpoints are internal. */
13638 }
13639
13640 /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
13641
13642 It gets cleared already on the removal of the first one of such placed
13643 breakpoints. This is OK as they get all removed altogether. */
13644
13645 static void
13646 longjmp_bkpt_dtor (struct breakpoint *self)
13647 {
13648 struct thread_info *tp = find_thread_id (self->thread);
13649
13650 if (tp)
13651 tp->initiating_frame = null_frame_id;
13652
13653 momentary_breakpoint_ops.dtor (self);
13654 }
13655
13656 /* Specific methods for probe breakpoints. */
13657
13658 static int
13659 bkpt_probe_insert_location (struct bp_location *bl)
13660 {
13661 int v = bkpt_insert_location (bl);
13662
13663 if (v == 0)
13664 {
13665 /* The insertion was successful, now let's set the probe's semaphore
13666 if needed. */
13667 if (bl->probe.probe->pops->set_semaphore != NULL)
13668 bl->probe.probe->pops->set_semaphore (bl->probe.probe,
13669 bl->probe.objfile,
13670 bl->gdbarch);
13671 }
13672
13673 return v;
13674 }
13675
13676 static int
13677 bkpt_probe_remove_location (struct bp_location *bl)
13678 {
13679 /* Let's clear the semaphore before removing the location. */
13680 if (bl->probe.probe->pops->clear_semaphore != NULL)
13681 bl->probe.probe->pops->clear_semaphore (bl->probe.probe,
13682 bl->probe.objfile,
13683 bl->gdbarch);
13684
13685 return bkpt_remove_location (bl);
13686 }
13687
13688 static void
13689 bkpt_probe_create_sals_from_address (char **arg,
13690 struct linespec_result *canonical,
13691 enum bptype type_wanted,
13692 char *addr_start, char **copy_arg)
13693 {
13694 struct linespec_sals lsal;
13695
13696 lsal.sals = parse_probes (arg, canonical);
13697
13698 *copy_arg = xstrdup (canonical->addr_string);
13699 lsal.canonical = xstrdup (*copy_arg);
13700
13701 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13702 }
13703
13704 static void
13705 bkpt_probe_decode_linespec (struct breakpoint *b, char **s,
13706 struct symtabs_and_lines *sals)
13707 {
13708 *sals = parse_probes (s, NULL);
13709 if (!sals->sals)
13710 error (_("probe not found"));
13711 }
13712
13713 /* The breakpoint_ops structure to be used in tracepoints. */
13714
13715 static void
13716 tracepoint_re_set (struct breakpoint *b)
13717 {
13718 breakpoint_re_set_default (b);
13719 }
13720
13721 static int
13722 tracepoint_breakpoint_hit (const struct bp_location *bl,
13723 struct address_space *aspace, CORE_ADDR bp_addr,
13724 const struct target_waitstatus *ws)
13725 {
13726 /* By definition, the inferior does not report stops at
13727 tracepoints. */
13728 return 0;
13729 }
13730
13731 static void
13732 tracepoint_print_one_detail (const struct breakpoint *self,
13733 struct ui_out *uiout)
13734 {
13735 struct tracepoint *tp = (struct tracepoint *) self;
13736 if (tp->static_trace_marker_id)
13737 {
13738 gdb_assert (self->type == bp_static_tracepoint);
13739
13740 ui_out_text (uiout, "\tmarker id is ");
13741 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
13742 tp->static_trace_marker_id);
13743 ui_out_text (uiout, "\n");
13744 }
13745 }
13746
13747 static void
13748 tracepoint_print_mention (struct breakpoint *b)
13749 {
13750 if (ui_out_is_mi_like_p (current_uiout))
13751 return;
13752
13753 switch (b->type)
13754 {
13755 case bp_tracepoint:
13756 printf_filtered (_("Tracepoint"));
13757 printf_filtered (_(" %d"), b->number);
13758 break;
13759 case bp_fast_tracepoint:
13760 printf_filtered (_("Fast tracepoint"));
13761 printf_filtered (_(" %d"), b->number);
13762 break;
13763 case bp_static_tracepoint:
13764 printf_filtered (_("Static tracepoint"));
13765 printf_filtered (_(" %d"), b->number);
13766 break;
13767 default:
13768 internal_error (__FILE__, __LINE__,
13769 _("unhandled tracepoint type %d"), (int) b->type);
13770 }
13771
13772 say_where (b);
13773 }
13774
13775 static void
13776 tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
13777 {
13778 struct tracepoint *tp = (struct tracepoint *) self;
13779
13780 if (self->type == bp_fast_tracepoint)
13781 fprintf_unfiltered (fp, "ftrace");
13782 if (self->type == bp_static_tracepoint)
13783 fprintf_unfiltered (fp, "strace");
13784 else if (self->type == bp_tracepoint)
13785 fprintf_unfiltered (fp, "trace");
13786 else
13787 internal_error (__FILE__, __LINE__,
13788 _("unhandled tracepoint type %d"), (int) self->type);
13789
13790 fprintf_unfiltered (fp, " %s", self->addr_string);
13791 print_recreate_thread (self, fp);
13792
13793 if (tp->pass_count)
13794 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
13795 }
13796
13797 static void
13798 tracepoint_create_sals_from_address (char **arg,
13799 struct linespec_result *canonical,
13800 enum bptype type_wanted,
13801 char *addr_start, char **copy_arg)
13802 {
13803 create_sals_from_address_default (arg, canonical, type_wanted,
13804 addr_start, copy_arg);
13805 }
13806
13807 static void
13808 tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13809 struct linespec_result *canonical,
13810 char *cond_string,
13811 char *extra_string,
13812 enum bptype type_wanted,
13813 enum bpdisp disposition,
13814 int thread,
13815 int task, int ignore_count,
13816 const struct breakpoint_ops *ops,
13817 int from_tty, int enabled,
13818 int internal, unsigned flags)
13819 {
13820 create_breakpoints_sal_default (gdbarch, canonical,
13821 cond_string, extra_string,
13822 type_wanted,
13823 disposition, thread, task,
13824 ignore_count, ops, from_tty,
13825 enabled, internal, flags);
13826 }
13827
13828 static void
13829 tracepoint_decode_linespec (struct breakpoint *b, char **s,
13830 struct symtabs_and_lines *sals)
13831 {
13832 decode_linespec_default (b, s, sals);
13833 }
13834
13835 struct breakpoint_ops tracepoint_breakpoint_ops;
13836
13837 /* The breakpoint_ops structure to be use on tracepoints placed in a
13838 static probe. */
13839
13840 static void
13841 tracepoint_probe_create_sals_from_address (char **arg,
13842 struct linespec_result *canonical,
13843 enum bptype type_wanted,
13844 char *addr_start, char **copy_arg)
13845 {
13846 /* We use the same method for breakpoint on probes. */
13847 bkpt_probe_create_sals_from_address (arg, canonical, type_wanted,
13848 addr_start, copy_arg);
13849 }
13850
13851 static void
13852 tracepoint_probe_decode_linespec (struct breakpoint *b, char **s,
13853 struct symtabs_and_lines *sals)
13854 {
13855 /* We use the same method for breakpoint on probes. */
13856 bkpt_probe_decode_linespec (b, s, sals);
13857 }
13858
13859 static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13860
13861 /* Dprintf breakpoint_ops methods. */
13862
13863 static void
13864 dprintf_re_set (struct breakpoint *b)
13865 {
13866 breakpoint_re_set_default (b);
13867
13868 /* This breakpoint could have been pending, and be resolved now, and
13869 if so, we should now have the extra string. If we don't, the
13870 dprintf was malformed when created, but we couldn't tell because
13871 we can't extract the extra string until the location is
13872 resolved. */
13873 if (b->loc != NULL && b->extra_string == NULL)
13874 error (_("Format string required"));
13875
13876 /* 1 - connect to target 1, that can run breakpoint commands.
13877 2 - create a dprintf, which resolves fine.
13878 3 - disconnect from target 1
13879 4 - connect to target 2, that can NOT run breakpoint commands.
13880
13881 After steps #3/#4, you'll want the dprintf command list to
13882 be updated, because target 1 and 2 may well return different
13883 answers for target_can_run_breakpoint_commands().
13884 Given absence of finer grained resetting, we get to do
13885 it all the time. */
13886 if (b->extra_string != NULL)
13887 update_dprintf_command_list (b);
13888 }
13889
13890 /* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13891
13892 static void
13893 dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13894 {
13895 fprintf_unfiltered (fp, "dprintf %s%s", tp->addr_string,
13896 tp->extra_string);
13897 print_recreate_thread (tp, fp);
13898 }
13899
13900 /* Implement the "after_condition_true" breakpoint_ops method for
13901 dprintf.
13902
13903 dprintf's are implemented with regular commands in their command
13904 list, but we run the commands here instead of before presenting the
13905 stop to the user, as dprintf's don't actually cause a stop. This
13906 also makes it so that the commands of multiple dprintfs at the same
13907 address are all handled. */
13908
13909 static void
13910 dprintf_after_condition_true (struct bpstats *bs)
13911 {
13912 struct cleanup *old_chain;
13913 struct bpstats tmp_bs = { NULL };
13914 struct bpstats *tmp_bs_p = &tmp_bs;
13915
13916 /* dprintf's never cause a stop. This wasn't set in the
13917 check_status hook instead because that would make the dprintf's
13918 condition not be evaluated. */
13919 bs->stop = 0;
13920
13921 /* Run the command list here. Take ownership of it instead of
13922 copying. We never want these commands to run later in
13923 bpstat_do_actions, if a breakpoint that causes a stop happens to
13924 be set at same address as this dprintf, or even if running the
13925 commands here throws. */
13926 tmp_bs.commands = bs->commands;
13927 bs->commands = NULL;
13928 old_chain = make_cleanup_decref_counted_command_line (&tmp_bs.commands);
13929
13930 bpstat_do_actions_1 (&tmp_bs_p);
13931
13932 /* 'tmp_bs.commands' will usually be NULL by now, but
13933 bpstat_do_actions_1 may return early without processing the whole
13934 list. */
13935 do_cleanups (old_chain);
13936 }
13937
13938 /* The breakpoint_ops structure to be used on static tracepoints with
13939 markers (`-m'). */
13940
13941 static void
13942 strace_marker_create_sals_from_address (char **arg,
13943 struct linespec_result *canonical,
13944 enum bptype type_wanted,
13945 char *addr_start, char **copy_arg)
13946 {
13947 struct linespec_sals lsal;
13948
13949 lsal.sals = decode_static_tracepoint_spec (arg);
13950
13951 *copy_arg = savestring (addr_start, *arg - addr_start);
13952
13953 canonical->addr_string = xstrdup (*copy_arg);
13954 lsal.canonical = xstrdup (*copy_arg);
13955 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13956 }
13957
13958 static void
13959 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13960 struct linespec_result *canonical,
13961 char *cond_string,
13962 char *extra_string,
13963 enum bptype type_wanted,
13964 enum bpdisp disposition,
13965 int thread,
13966 int task, int ignore_count,
13967 const struct breakpoint_ops *ops,
13968 int from_tty, int enabled,
13969 int internal, unsigned flags)
13970 {
13971 int i;
13972 struct linespec_sals *lsal = VEC_index (linespec_sals,
13973 canonical->sals, 0);
13974
13975 /* If the user is creating a static tracepoint by marker id
13976 (strace -m MARKER_ID), then store the sals index, so that
13977 breakpoint_re_set can try to match up which of the newly
13978 found markers corresponds to this one, and, don't try to
13979 expand multiple locations for each sal, given than SALS
13980 already should contain all sals for MARKER_ID. */
13981
13982 for (i = 0; i < lsal->sals.nelts; ++i)
13983 {
13984 struct symtabs_and_lines expanded;
13985 struct tracepoint *tp;
13986 struct cleanup *old_chain;
13987 char *addr_string;
13988
13989 expanded.nelts = 1;
13990 expanded.sals = &lsal->sals.sals[i];
13991
13992 addr_string = xstrdup (canonical->addr_string);
13993 old_chain = make_cleanup (xfree, addr_string);
13994
13995 tp = XCNEW (struct tracepoint);
13996 init_breakpoint_sal (&tp->base, gdbarch, expanded,
13997 addr_string, NULL,
13998 cond_string, extra_string,
13999 type_wanted, disposition,
14000 thread, task, ignore_count, ops,
14001 from_tty, enabled, internal, flags,
14002 canonical->special_display);
14003 /* Given that its possible to have multiple markers with
14004 the same string id, if the user is creating a static
14005 tracepoint by marker id ("strace -m MARKER_ID"), then
14006 store the sals index, so that breakpoint_re_set can
14007 try to match up which of the newly found markers
14008 corresponds to this one */
14009 tp->static_trace_marker_id_idx = i;
14010
14011 install_breakpoint (internal, &tp->base, 0);
14012
14013 discard_cleanups (old_chain);
14014 }
14015 }
14016
14017 static void
14018 strace_marker_decode_linespec (struct breakpoint *b, char **s,
14019 struct symtabs_and_lines *sals)
14020 {
14021 struct tracepoint *tp = (struct tracepoint *) b;
14022
14023 *sals = decode_static_tracepoint_spec (s);
14024 if (sals->nelts > tp->static_trace_marker_id_idx)
14025 {
14026 sals->sals[0] = sals->sals[tp->static_trace_marker_id_idx];
14027 sals->nelts = 1;
14028 }
14029 else
14030 error (_("marker %s not found"), tp->static_trace_marker_id);
14031 }
14032
14033 static struct breakpoint_ops strace_marker_breakpoint_ops;
14034
14035 static int
14036 strace_marker_p (struct breakpoint *b)
14037 {
14038 return b->ops == &strace_marker_breakpoint_ops;
14039 }
14040
14041 /* Delete a breakpoint and clean up all traces of it in the data
14042 structures. */
14043
14044 void
14045 delete_breakpoint (struct breakpoint *bpt)
14046 {
14047 struct breakpoint *b;
14048
14049 gdb_assert (bpt != NULL);
14050
14051 /* Has this bp already been deleted? This can happen because
14052 multiple lists can hold pointers to bp's. bpstat lists are
14053 especial culprits.
14054
14055 One example of this happening is a watchpoint's scope bp. When
14056 the scope bp triggers, we notice that the watchpoint is out of
14057 scope, and delete it. We also delete its scope bp. But the
14058 scope bp is marked "auto-deleting", and is already on a bpstat.
14059 That bpstat is then checked for auto-deleting bp's, which are
14060 deleted.
14061
14062 A real solution to this problem might involve reference counts in
14063 bp's, and/or giving them pointers back to their referencing
14064 bpstat's, and teaching delete_breakpoint to only free a bp's
14065 storage when no more references were extent. A cheaper bandaid
14066 was chosen. */
14067 if (bpt->type == bp_none)
14068 return;
14069
14070 /* At least avoid this stale reference until the reference counting
14071 of breakpoints gets resolved. */
14072 if (bpt->related_breakpoint != bpt)
14073 {
14074 struct breakpoint *related;
14075 struct watchpoint *w;
14076
14077 if (bpt->type == bp_watchpoint_scope)
14078 w = (struct watchpoint *) bpt->related_breakpoint;
14079 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
14080 w = (struct watchpoint *) bpt;
14081 else
14082 w = NULL;
14083 if (w != NULL)
14084 watchpoint_del_at_next_stop (w);
14085
14086 /* Unlink bpt from the bpt->related_breakpoint ring. */
14087 for (related = bpt; related->related_breakpoint != bpt;
14088 related = related->related_breakpoint);
14089 related->related_breakpoint = bpt->related_breakpoint;
14090 bpt->related_breakpoint = bpt;
14091 }
14092
14093 /* watch_command_1 creates a watchpoint but only sets its number if
14094 update_watchpoint succeeds in creating its bp_locations. If there's
14095 a problem in that process, we'll be asked to delete the half-created
14096 watchpoint. In that case, don't announce the deletion. */
14097 if (bpt->number)
14098 observer_notify_breakpoint_deleted (bpt);
14099
14100 if (breakpoint_chain == bpt)
14101 breakpoint_chain = bpt->next;
14102
14103 ALL_BREAKPOINTS (b)
14104 if (b->next == bpt)
14105 {
14106 b->next = bpt->next;
14107 break;
14108 }
14109
14110 /* Be sure no bpstat's are pointing at the breakpoint after it's
14111 been freed. */
14112 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
14113 in all threads for now. Note that we cannot just remove bpstats
14114 pointing at bpt from the stop_bpstat list entirely, as breakpoint
14115 commands are associated with the bpstat; if we remove it here,
14116 then the later call to bpstat_do_actions (&stop_bpstat); in
14117 event-top.c won't do anything, and temporary breakpoints with
14118 commands won't work. */
14119
14120 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
14121
14122 /* Now that breakpoint is removed from breakpoint list, update the
14123 global location list. This will remove locations that used to
14124 belong to this breakpoint. Do this before freeing the breakpoint
14125 itself, since remove_breakpoint looks at location's owner. It
14126 might be better design to have location completely
14127 self-contained, but it's not the case now. */
14128 update_global_location_list (UGLL_DONT_INSERT);
14129
14130 bpt->ops->dtor (bpt);
14131 /* On the chance that someone will soon try again to delete this
14132 same bp, we mark it as deleted before freeing its storage. */
14133 bpt->type = bp_none;
14134 xfree (bpt);
14135 }
14136
14137 static void
14138 do_delete_breakpoint_cleanup (void *b)
14139 {
14140 delete_breakpoint (b);
14141 }
14142
14143 struct cleanup *
14144 make_cleanup_delete_breakpoint (struct breakpoint *b)
14145 {
14146 return make_cleanup (do_delete_breakpoint_cleanup, b);
14147 }
14148
14149 /* Iterator function to call a user-provided callback function once
14150 for each of B and its related breakpoints. */
14151
14152 static void
14153 iterate_over_related_breakpoints (struct breakpoint *b,
14154 void (*function) (struct breakpoint *,
14155 void *),
14156 void *data)
14157 {
14158 struct breakpoint *related;
14159
14160 related = b;
14161 do
14162 {
14163 struct breakpoint *next;
14164
14165 /* FUNCTION may delete RELATED. */
14166 next = related->related_breakpoint;
14167
14168 if (next == related)
14169 {
14170 /* RELATED is the last ring entry. */
14171 function (related, data);
14172
14173 /* FUNCTION may have deleted it, so we'd never reach back to
14174 B. There's nothing left to do anyway, so just break
14175 out. */
14176 break;
14177 }
14178 else
14179 function (related, data);
14180
14181 related = next;
14182 }
14183 while (related != b);
14184 }
14185
14186 static void
14187 do_delete_breakpoint (struct breakpoint *b, void *ignore)
14188 {
14189 delete_breakpoint (b);
14190 }
14191
14192 /* A callback for map_breakpoint_numbers that calls
14193 delete_breakpoint. */
14194
14195 static void
14196 do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
14197 {
14198 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
14199 }
14200
14201 void
14202 delete_command (char *arg, int from_tty)
14203 {
14204 struct breakpoint *b, *b_tmp;
14205
14206 dont_repeat ();
14207
14208 if (arg == 0)
14209 {
14210 int breaks_to_delete = 0;
14211
14212 /* Delete all breakpoints if no argument. Do not delete
14213 internal breakpoints, these have to be deleted with an
14214 explicit breakpoint number argument. */
14215 ALL_BREAKPOINTS (b)
14216 if (user_breakpoint_p (b))
14217 {
14218 breaks_to_delete = 1;
14219 break;
14220 }
14221
14222 /* Ask user only if there are some breakpoints to delete. */
14223 if (!from_tty
14224 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
14225 {
14226 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14227 if (user_breakpoint_p (b))
14228 delete_breakpoint (b);
14229 }
14230 }
14231 else
14232 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
14233 }
14234
14235 static int
14236 all_locations_are_pending (struct bp_location *loc)
14237 {
14238 for (; loc; loc = loc->next)
14239 if (!loc->shlib_disabled
14240 && !loc->pspace->executing_startup)
14241 return 0;
14242 return 1;
14243 }
14244
14245 /* Subroutine of update_breakpoint_locations to simplify it.
14246 Return non-zero if multiple fns in list LOC have the same name.
14247 Null names are ignored. */
14248
14249 static int
14250 ambiguous_names_p (struct bp_location *loc)
14251 {
14252 struct bp_location *l;
14253 htab_t htab = htab_create_alloc (13, htab_hash_string,
14254 (int (*) (const void *,
14255 const void *)) streq,
14256 NULL, xcalloc, xfree);
14257
14258 for (l = loc; l != NULL; l = l->next)
14259 {
14260 const char **slot;
14261 const char *name = l->function_name;
14262
14263 /* Allow for some names to be NULL, ignore them. */
14264 if (name == NULL)
14265 continue;
14266
14267 slot = (const char **) htab_find_slot (htab, (const void *) name,
14268 INSERT);
14269 /* NOTE: We can assume slot != NULL here because xcalloc never
14270 returns NULL. */
14271 if (*slot != NULL)
14272 {
14273 htab_delete (htab);
14274 return 1;
14275 }
14276 *slot = name;
14277 }
14278
14279 htab_delete (htab);
14280 return 0;
14281 }
14282
14283 /* When symbols change, it probably means the sources changed as well,
14284 and it might mean the static tracepoint markers are no longer at
14285 the same address or line numbers they used to be at last we
14286 checked. Losing your static tracepoints whenever you rebuild is
14287 undesirable. This function tries to resync/rematch gdb static
14288 tracepoints with the markers on the target, for static tracepoints
14289 that have not been set by marker id. Static tracepoint that have
14290 been set by marker id are reset by marker id in breakpoint_re_set.
14291 The heuristic is:
14292
14293 1) For a tracepoint set at a specific address, look for a marker at
14294 the old PC. If one is found there, assume to be the same marker.
14295 If the name / string id of the marker found is different from the
14296 previous known name, assume that means the user renamed the marker
14297 in the sources, and output a warning.
14298
14299 2) For a tracepoint set at a given line number, look for a marker
14300 at the new address of the old line number. If one is found there,
14301 assume to be the same marker. If the name / string id of the
14302 marker found is different from the previous known name, assume that
14303 means the user renamed the marker in the sources, and output a
14304 warning.
14305
14306 3) If a marker is no longer found at the same address or line, it
14307 may mean the marker no longer exists. But it may also just mean
14308 the code changed a bit. Maybe the user added a few lines of code
14309 that made the marker move up or down (in line number terms). Ask
14310 the target for info about the marker with the string id as we knew
14311 it. If found, update line number and address in the matching
14312 static tracepoint. This will get confused if there's more than one
14313 marker with the same ID (possible in UST, although unadvised
14314 precisely because it confuses tools). */
14315
14316 static struct symtab_and_line
14317 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
14318 {
14319 struct tracepoint *tp = (struct tracepoint *) b;
14320 struct static_tracepoint_marker marker;
14321 CORE_ADDR pc;
14322
14323 pc = sal.pc;
14324 if (sal.line)
14325 find_line_pc (sal.symtab, sal.line, &pc);
14326
14327 if (target_static_tracepoint_marker_at (pc, &marker))
14328 {
14329 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
14330 warning (_("static tracepoint %d changed probed marker from %s to %s"),
14331 b->number,
14332 tp->static_trace_marker_id, marker.str_id);
14333
14334 xfree (tp->static_trace_marker_id);
14335 tp->static_trace_marker_id = xstrdup (marker.str_id);
14336 release_static_tracepoint_marker (&marker);
14337
14338 return sal;
14339 }
14340
14341 /* Old marker wasn't found on target at lineno. Try looking it up
14342 by string ID. */
14343 if (!sal.explicit_pc
14344 && sal.line != 0
14345 && sal.symtab != NULL
14346 && tp->static_trace_marker_id != NULL)
14347 {
14348 VEC(static_tracepoint_marker_p) *markers;
14349
14350 markers
14351 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
14352
14353 if (!VEC_empty(static_tracepoint_marker_p, markers))
14354 {
14355 struct symtab_and_line sal2;
14356 struct symbol *sym;
14357 struct static_tracepoint_marker *tpmarker;
14358 struct ui_out *uiout = current_uiout;
14359
14360 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
14361
14362 xfree (tp->static_trace_marker_id);
14363 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
14364
14365 warning (_("marker for static tracepoint %d (%s) not "
14366 "found at previous line number"),
14367 b->number, tp->static_trace_marker_id);
14368
14369 init_sal (&sal2);
14370
14371 sal2.pc = tpmarker->address;
14372
14373 sal2 = find_pc_line (tpmarker->address, 0);
14374 sym = find_pc_sect_function (tpmarker->address, NULL);
14375 ui_out_text (uiout, "Now in ");
14376 if (sym)
14377 {
14378 ui_out_field_string (uiout, "func",
14379 SYMBOL_PRINT_NAME (sym));
14380 ui_out_text (uiout, " at ");
14381 }
14382 ui_out_field_string (uiout, "file",
14383 symtab_to_filename_for_display (sal2.symtab));
14384 ui_out_text (uiout, ":");
14385
14386 if (ui_out_is_mi_like_p (uiout))
14387 {
14388 const char *fullname = symtab_to_fullname (sal2.symtab);
14389
14390 ui_out_field_string (uiout, "fullname", fullname);
14391 }
14392
14393 ui_out_field_int (uiout, "line", sal2.line);
14394 ui_out_text (uiout, "\n");
14395
14396 b->loc->line_number = sal2.line;
14397 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
14398
14399 xfree (b->addr_string);
14400 b->addr_string = xstrprintf ("%s:%d",
14401 symtab_to_filename_for_display (sal2.symtab),
14402 b->loc->line_number);
14403
14404 /* Might be nice to check if function changed, and warn if
14405 so. */
14406
14407 release_static_tracepoint_marker (tpmarker);
14408 }
14409 }
14410 return sal;
14411 }
14412
14413 /* Returns 1 iff locations A and B are sufficiently same that
14414 we don't need to report breakpoint as changed. */
14415
14416 static int
14417 locations_are_equal (struct bp_location *a, struct bp_location *b)
14418 {
14419 while (a && b)
14420 {
14421 if (a->address != b->address)
14422 return 0;
14423
14424 if (a->shlib_disabled != b->shlib_disabled)
14425 return 0;
14426
14427 if (a->enabled != b->enabled)
14428 return 0;
14429
14430 a = a->next;
14431 b = b->next;
14432 }
14433
14434 if ((a == NULL) != (b == NULL))
14435 return 0;
14436
14437 return 1;
14438 }
14439
14440 /* Create new breakpoint locations for B (a hardware or software breakpoint)
14441 based on SALS and SALS_END. If SALS_END.NELTS is not zero, then B is
14442 a ranged breakpoint. */
14443
14444 void
14445 update_breakpoint_locations (struct breakpoint *b,
14446 struct symtabs_and_lines sals,
14447 struct symtabs_and_lines sals_end)
14448 {
14449 int i;
14450 struct bp_location *existing_locations = b->loc;
14451
14452 if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
14453 {
14454 /* Ranged breakpoints have only one start location and one end
14455 location. */
14456 b->enable_state = bp_disabled;
14457 update_global_location_list (UGLL_MAY_INSERT);
14458 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
14459 "multiple locations found\n"),
14460 b->number);
14461 return;
14462 }
14463
14464 /* If there's no new locations, and all existing locations are
14465 pending, don't do anything. This optimizes the common case where
14466 all locations are in the same shared library, that was unloaded.
14467 We'd like to retain the location, so that when the library is
14468 loaded again, we don't loose the enabled/disabled status of the
14469 individual locations. */
14470 if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
14471 return;
14472
14473 b->loc = NULL;
14474
14475 for (i = 0; i < sals.nelts; ++i)
14476 {
14477 struct bp_location *new_loc;
14478
14479 switch_to_program_space_and_thread (sals.sals[i].pspace);
14480
14481 new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
14482
14483 /* Reparse conditions, they might contain references to the
14484 old symtab. */
14485 if (b->cond_string != NULL)
14486 {
14487 const char *s;
14488 volatile struct gdb_exception e;
14489
14490 s = b->cond_string;
14491 TRY_CATCH (e, RETURN_MASK_ERROR)
14492 {
14493 new_loc->cond = parse_exp_1 (&s, sals.sals[i].pc,
14494 block_for_pc (sals.sals[i].pc),
14495 0);
14496 }
14497 if (e.reason < 0)
14498 {
14499 warning (_("failed to reevaluate condition "
14500 "for breakpoint %d: %s"),
14501 b->number, e.message);
14502 new_loc->enabled = 0;
14503 }
14504 }
14505
14506 if (sals_end.nelts)
14507 {
14508 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
14509
14510 new_loc->length = end - sals.sals[0].pc + 1;
14511 }
14512 }
14513
14514 /* If possible, carry over 'disable' status from existing
14515 breakpoints. */
14516 {
14517 struct bp_location *e = existing_locations;
14518 /* If there are multiple breakpoints with the same function name,
14519 e.g. for inline functions, comparing function names won't work.
14520 Instead compare pc addresses; this is just a heuristic as things
14521 may have moved, but in practice it gives the correct answer
14522 often enough until a better solution is found. */
14523 int have_ambiguous_names = ambiguous_names_p (b->loc);
14524
14525 for (; e; e = e->next)
14526 {
14527 if (!e->enabled && e->function_name)
14528 {
14529 struct bp_location *l = b->loc;
14530 if (have_ambiguous_names)
14531 {
14532 for (; l; l = l->next)
14533 if (breakpoint_locations_match (e, l))
14534 {
14535 l->enabled = 0;
14536 break;
14537 }
14538 }
14539 else
14540 {
14541 for (; l; l = l->next)
14542 if (l->function_name
14543 && strcmp (e->function_name, l->function_name) == 0)
14544 {
14545 l->enabled = 0;
14546 break;
14547 }
14548 }
14549 }
14550 }
14551 }
14552
14553 if (!locations_are_equal (existing_locations, b->loc))
14554 observer_notify_breakpoint_modified (b);
14555
14556 update_global_location_list (UGLL_MAY_INSERT);
14557 }
14558
14559 /* Find the SaL locations corresponding to the given ADDR_STRING.
14560 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
14561
14562 static struct symtabs_and_lines
14563 addr_string_to_sals (struct breakpoint *b, char *addr_string, int *found)
14564 {
14565 char *s;
14566 struct symtabs_and_lines sals = {0};
14567 volatile struct gdb_exception e;
14568
14569 gdb_assert (b->ops != NULL);
14570 s = addr_string;
14571
14572 TRY_CATCH (e, RETURN_MASK_ERROR)
14573 {
14574 b->ops->decode_linespec (b, &s, &sals);
14575 }
14576 if (e.reason < 0)
14577 {
14578 int not_found_and_ok = 0;
14579 /* For pending breakpoints, it's expected that parsing will
14580 fail until the right shared library is loaded. User has
14581 already told to create pending breakpoints and don't need
14582 extra messages. If breakpoint is in bp_shlib_disabled
14583 state, then user already saw the message about that
14584 breakpoint being disabled, and don't want to see more
14585 errors. */
14586 if (e.error == NOT_FOUND_ERROR
14587 && (b->condition_not_parsed
14588 || (b->loc && b->loc->shlib_disabled)
14589 || (b->loc && b->loc->pspace->executing_startup)
14590 || b->enable_state == bp_disabled))
14591 not_found_and_ok = 1;
14592
14593 if (!not_found_and_ok)
14594 {
14595 /* We surely don't want to warn about the same breakpoint
14596 10 times. One solution, implemented here, is disable
14597 the breakpoint on error. Another solution would be to
14598 have separate 'warning emitted' flag. Since this
14599 happens only when a binary has changed, I don't know
14600 which approach is better. */
14601 b->enable_state = bp_disabled;
14602 throw_exception (e);
14603 }
14604 }
14605
14606 if (e.reason == 0 || e.error != NOT_FOUND_ERROR)
14607 {
14608 int i;
14609
14610 for (i = 0; i < sals.nelts; ++i)
14611 resolve_sal_pc (&sals.sals[i]);
14612 if (b->condition_not_parsed && s && s[0])
14613 {
14614 char *cond_string, *extra_string;
14615 int thread, task;
14616
14617 find_condition_and_thread (s, sals.sals[0].pc,
14618 &cond_string, &thread, &task,
14619 &extra_string);
14620 if (cond_string)
14621 b->cond_string = cond_string;
14622 b->thread = thread;
14623 b->task = task;
14624 if (extra_string)
14625 b->extra_string = extra_string;
14626 b->condition_not_parsed = 0;
14627 }
14628
14629 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
14630 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
14631
14632 *found = 1;
14633 }
14634 else
14635 *found = 0;
14636
14637 return sals;
14638 }
14639
14640 /* The default re_set method, for typical hardware or software
14641 breakpoints. Reevaluate the breakpoint and recreate its
14642 locations. */
14643
14644 static void
14645 breakpoint_re_set_default (struct breakpoint *b)
14646 {
14647 int found;
14648 struct symtabs_and_lines sals, sals_end;
14649 struct symtabs_and_lines expanded = {0};
14650 struct symtabs_and_lines expanded_end = {0};
14651
14652 sals = addr_string_to_sals (b, b->addr_string, &found);
14653 if (found)
14654 {
14655 make_cleanup (xfree, sals.sals);
14656 expanded = sals;
14657 }
14658
14659 if (b->addr_string_range_end)
14660 {
14661 sals_end = addr_string_to_sals (b, b->addr_string_range_end, &found);
14662 if (found)
14663 {
14664 make_cleanup (xfree, sals_end.sals);
14665 expanded_end = sals_end;
14666 }
14667 }
14668
14669 update_breakpoint_locations (b, expanded, expanded_end);
14670 }
14671
14672 /* Default method for creating SALs from an address string. It basically
14673 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
14674
14675 static void
14676 create_sals_from_address_default (char **arg,
14677 struct linespec_result *canonical,
14678 enum bptype type_wanted,
14679 char *addr_start, char **copy_arg)
14680 {
14681 parse_breakpoint_sals (arg, canonical);
14682 }
14683
14684 /* Call create_breakpoints_sal for the given arguments. This is the default
14685 function for the `create_breakpoints_sal' method of
14686 breakpoint_ops. */
14687
14688 static void
14689 create_breakpoints_sal_default (struct gdbarch *gdbarch,
14690 struct linespec_result *canonical,
14691 char *cond_string,
14692 char *extra_string,
14693 enum bptype type_wanted,
14694 enum bpdisp disposition,
14695 int thread,
14696 int task, int ignore_count,
14697 const struct breakpoint_ops *ops,
14698 int from_tty, int enabled,
14699 int internal, unsigned flags)
14700 {
14701 create_breakpoints_sal (gdbarch, canonical, cond_string,
14702 extra_string,
14703 type_wanted, disposition,
14704 thread, task, ignore_count, ops, from_tty,
14705 enabled, internal, flags);
14706 }
14707
14708 /* Decode the line represented by S by calling decode_line_full. This is the
14709 default function for the `decode_linespec' method of breakpoint_ops. */
14710
14711 static void
14712 decode_linespec_default (struct breakpoint *b, char **s,
14713 struct symtabs_and_lines *sals)
14714 {
14715 struct linespec_result canonical;
14716
14717 init_linespec_result (&canonical);
14718 decode_line_full (s, DECODE_LINE_FUNFIRSTLINE,
14719 (struct symtab *) NULL, 0,
14720 &canonical, multiple_symbols_all,
14721 b->filter);
14722
14723 /* We should get 0 or 1 resulting SALs. */
14724 gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
14725
14726 if (VEC_length (linespec_sals, canonical.sals) > 0)
14727 {
14728 struct linespec_sals *lsal;
14729
14730 lsal = VEC_index (linespec_sals, canonical.sals, 0);
14731 *sals = lsal->sals;
14732 /* Arrange it so the destructor does not free the
14733 contents. */
14734 lsal->sals.sals = NULL;
14735 }
14736
14737 destroy_linespec_result (&canonical);
14738 }
14739
14740 /* Prepare the global context for a re-set of breakpoint B. */
14741
14742 static struct cleanup *
14743 prepare_re_set_context (struct breakpoint *b)
14744 {
14745 struct cleanup *cleanups;
14746
14747 input_radix = b->input_radix;
14748 cleanups = save_current_space_and_thread ();
14749 if (b->pspace != NULL)
14750 switch_to_program_space_and_thread (b->pspace);
14751 set_language (b->language);
14752
14753 return cleanups;
14754 }
14755
14756 /* Reset a breakpoint given it's struct breakpoint * BINT.
14757 The value we return ends up being the return value from catch_errors.
14758 Unused in this case. */
14759
14760 static int
14761 breakpoint_re_set_one (void *bint)
14762 {
14763 /* Get past catch_errs. */
14764 struct breakpoint *b = (struct breakpoint *) bint;
14765 struct cleanup *cleanups;
14766
14767 cleanups = prepare_re_set_context (b);
14768 b->ops->re_set (b);
14769 do_cleanups (cleanups);
14770 return 0;
14771 }
14772
14773 /* Re-set all breakpoints after symbols have been re-loaded. */
14774 void
14775 breakpoint_re_set (void)
14776 {
14777 struct breakpoint *b, *b_tmp;
14778 enum language save_language;
14779 int save_input_radix;
14780 struct cleanup *old_chain;
14781
14782 save_language = current_language->la_language;
14783 save_input_radix = input_radix;
14784 old_chain = save_current_program_space ();
14785
14786 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14787 {
14788 /* Format possible error msg. */
14789 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
14790 b->number);
14791 struct cleanup *cleanups = make_cleanup (xfree, message);
14792 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
14793 do_cleanups (cleanups);
14794 }
14795 set_language (save_language);
14796 input_radix = save_input_radix;
14797
14798 jit_breakpoint_re_set ();
14799
14800 do_cleanups (old_chain);
14801
14802 create_overlay_event_breakpoint ();
14803 create_longjmp_master_breakpoint ();
14804 create_std_terminate_master_breakpoint ();
14805 create_exception_master_breakpoint ();
14806 }
14807 \f
14808 /* Reset the thread number of this breakpoint:
14809
14810 - If the breakpoint is for all threads, leave it as-is.
14811 - Else, reset it to the current thread for inferior_ptid. */
14812 void
14813 breakpoint_re_set_thread (struct breakpoint *b)
14814 {
14815 if (b->thread != -1)
14816 {
14817 if (in_thread_list (inferior_ptid))
14818 b->thread = pid_to_thread_id (inferior_ptid);
14819
14820 /* We're being called after following a fork. The new fork is
14821 selected as current, and unless this was a vfork will have a
14822 different program space from the original thread. Reset that
14823 as well. */
14824 b->loc->pspace = current_program_space;
14825 }
14826 }
14827
14828 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14829 If from_tty is nonzero, it prints a message to that effect,
14830 which ends with a period (no newline). */
14831
14832 void
14833 set_ignore_count (int bptnum, int count, int from_tty)
14834 {
14835 struct breakpoint *b;
14836
14837 if (count < 0)
14838 count = 0;
14839
14840 ALL_BREAKPOINTS (b)
14841 if (b->number == bptnum)
14842 {
14843 if (is_tracepoint (b))
14844 {
14845 if (from_tty && count != 0)
14846 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14847 bptnum);
14848 return;
14849 }
14850
14851 b->ignore_count = count;
14852 if (from_tty)
14853 {
14854 if (count == 0)
14855 printf_filtered (_("Will stop next time "
14856 "breakpoint %d is reached."),
14857 bptnum);
14858 else if (count == 1)
14859 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
14860 bptnum);
14861 else
14862 printf_filtered (_("Will ignore next %d "
14863 "crossings of breakpoint %d."),
14864 count, bptnum);
14865 }
14866 observer_notify_breakpoint_modified (b);
14867 return;
14868 }
14869
14870 error (_("No breakpoint number %d."), bptnum);
14871 }
14872
14873 /* Command to set ignore-count of breakpoint N to COUNT. */
14874
14875 static void
14876 ignore_command (char *args, int from_tty)
14877 {
14878 char *p = args;
14879 int num;
14880
14881 if (p == 0)
14882 error_no_arg (_("a breakpoint number"));
14883
14884 num = get_number (&p);
14885 if (num == 0)
14886 error (_("bad breakpoint number: '%s'"), args);
14887 if (*p == 0)
14888 error (_("Second argument (specified ignore-count) is missing."));
14889
14890 set_ignore_count (num,
14891 longest_to_int (value_as_long (parse_and_eval (p))),
14892 from_tty);
14893 if (from_tty)
14894 printf_filtered ("\n");
14895 }
14896 \f
14897 /* Call FUNCTION on each of the breakpoints
14898 whose numbers are given in ARGS. */
14899
14900 static void
14901 map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
14902 void *),
14903 void *data)
14904 {
14905 int num;
14906 struct breakpoint *b, *tmp;
14907 int match;
14908 struct get_number_or_range_state state;
14909
14910 if (args == 0 || *args == '\0')
14911 error_no_arg (_("one or more breakpoint numbers"));
14912
14913 init_number_or_range (&state, args);
14914
14915 while (!state.finished)
14916 {
14917 const char *p = state.string;
14918
14919 match = 0;
14920
14921 num = get_number_or_range (&state);
14922 if (num == 0)
14923 {
14924 warning (_("bad breakpoint number at or near '%s'"), p);
14925 }
14926 else
14927 {
14928 ALL_BREAKPOINTS_SAFE (b, tmp)
14929 if (b->number == num)
14930 {
14931 match = 1;
14932 function (b, data);
14933 break;
14934 }
14935 if (match == 0)
14936 printf_unfiltered (_("No breakpoint number %d.\n"), num);
14937 }
14938 }
14939 }
14940
14941 static struct bp_location *
14942 find_location_by_number (char *number)
14943 {
14944 char *dot = strchr (number, '.');
14945 char *p1;
14946 int bp_num;
14947 int loc_num;
14948 struct breakpoint *b;
14949 struct bp_location *loc;
14950
14951 *dot = '\0';
14952
14953 p1 = number;
14954 bp_num = get_number (&p1);
14955 if (bp_num == 0)
14956 error (_("Bad breakpoint number '%s'"), number);
14957
14958 ALL_BREAKPOINTS (b)
14959 if (b->number == bp_num)
14960 {
14961 break;
14962 }
14963
14964 if (!b || b->number != bp_num)
14965 error (_("Bad breakpoint number '%s'"), number);
14966
14967 p1 = dot+1;
14968 loc_num = get_number (&p1);
14969 if (loc_num == 0)
14970 error (_("Bad breakpoint location number '%s'"), number);
14971
14972 --loc_num;
14973 loc = b->loc;
14974 for (;loc_num && loc; --loc_num, loc = loc->next)
14975 ;
14976 if (!loc)
14977 error (_("Bad breakpoint location number '%s'"), dot+1);
14978
14979 return loc;
14980 }
14981
14982
14983 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14984 If from_tty is nonzero, it prints a message to that effect,
14985 which ends with a period (no newline). */
14986
14987 void
14988 disable_breakpoint (struct breakpoint *bpt)
14989 {
14990 /* Never disable a watchpoint scope breakpoint; we want to
14991 hit them when we leave scope so we can delete both the
14992 watchpoint and its scope breakpoint at that time. */
14993 if (bpt->type == bp_watchpoint_scope)
14994 return;
14995
14996 bpt->enable_state = bp_disabled;
14997
14998 /* Mark breakpoint locations modified. */
14999 mark_breakpoint_modified (bpt);
15000
15001 if (target_supports_enable_disable_tracepoint ()
15002 && current_trace_status ()->running && is_tracepoint (bpt))
15003 {
15004 struct bp_location *location;
15005
15006 for (location = bpt->loc; location; location = location->next)
15007 target_disable_tracepoint (location);
15008 }
15009
15010 update_global_location_list (UGLL_DONT_INSERT);
15011
15012 observer_notify_breakpoint_modified (bpt);
15013 }
15014
15015 /* A callback for iterate_over_related_breakpoints. */
15016
15017 static void
15018 do_disable_breakpoint (struct breakpoint *b, void *ignore)
15019 {
15020 disable_breakpoint (b);
15021 }
15022
15023 /* A callback for map_breakpoint_numbers that calls
15024 disable_breakpoint. */
15025
15026 static void
15027 do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
15028 {
15029 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
15030 }
15031
15032 static void
15033 disable_command (char *args, int from_tty)
15034 {
15035 if (args == 0)
15036 {
15037 struct breakpoint *bpt;
15038
15039 ALL_BREAKPOINTS (bpt)
15040 if (user_breakpoint_p (bpt))
15041 disable_breakpoint (bpt);
15042 }
15043 else
15044 {
15045 char *num = extract_arg (&args);
15046
15047 while (num)
15048 {
15049 if (strchr (num, '.'))
15050 {
15051 struct bp_location *loc = find_location_by_number (num);
15052
15053 if (loc)
15054 {
15055 if (loc->enabled)
15056 {
15057 loc->enabled = 0;
15058 mark_breakpoint_location_modified (loc);
15059 }
15060 if (target_supports_enable_disable_tracepoint ()
15061 && current_trace_status ()->running && loc->owner
15062 && is_tracepoint (loc->owner))
15063 target_disable_tracepoint (loc);
15064 }
15065 update_global_location_list (UGLL_DONT_INSERT);
15066 }
15067 else
15068 map_breakpoint_numbers (num, do_map_disable_breakpoint, NULL);
15069 num = extract_arg (&args);
15070 }
15071 }
15072 }
15073
15074 static void
15075 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
15076 int count)
15077 {
15078 int target_resources_ok;
15079
15080 if (bpt->type == bp_hardware_breakpoint)
15081 {
15082 int i;
15083 i = hw_breakpoint_used_count ();
15084 target_resources_ok =
15085 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
15086 i + 1, 0);
15087 if (target_resources_ok == 0)
15088 error (_("No hardware breakpoint support in the target."));
15089 else if (target_resources_ok < 0)
15090 error (_("Hardware breakpoints used exceeds limit."));
15091 }
15092
15093 if (is_watchpoint (bpt))
15094 {
15095 /* Initialize it just to avoid a GCC false warning. */
15096 enum enable_state orig_enable_state = 0;
15097 volatile struct gdb_exception e;
15098
15099 TRY_CATCH (e, RETURN_MASK_ALL)
15100 {
15101 struct watchpoint *w = (struct watchpoint *) bpt;
15102
15103 orig_enable_state = bpt->enable_state;
15104 bpt->enable_state = bp_enabled;
15105 update_watchpoint (w, 1 /* reparse */);
15106 }
15107 if (e.reason < 0)
15108 {
15109 bpt->enable_state = orig_enable_state;
15110 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
15111 bpt->number);
15112 return;
15113 }
15114 }
15115
15116 bpt->enable_state = bp_enabled;
15117
15118 /* Mark breakpoint locations modified. */
15119 mark_breakpoint_modified (bpt);
15120
15121 if (target_supports_enable_disable_tracepoint ()
15122 && current_trace_status ()->running && is_tracepoint (bpt))
15123 {
15124 struct bp_location *location;
15125
15126 for (location = bpt->loc; location; location = location->next)
15127 target_enable_tracepoint (location);
15128 }
15129
15130 bpt->disposition = disposition;
15131 bpt->enable_count = count;
15132 update_global_location_list (UGLL_MAY_INSERT);
15133
15134 observer_notify_breakpoint_modified (bpt);
15135 }
15136
15137
15138 void
15139 enable_breakpoint (struct breakpoint *bpt)
15140 {
15141 enable_breakpoint_disp (bpt, bpt->disposition, 0);
15142 }
15143
15144 static void
15145 do_enable_breakpoint (struct breakpoint *bpt, void *arg)
15146 {
15147 enable_breakpoint (bpt);
15148 }
15149
15150 /* A callback for map_breakpoint_numbers that calls
15151 enable_breakpoint. */
15152
15153 static void
15154 do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
15155 {
15156 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
15157 }
15158
15159 /* The enable command enables the specified breakpoints (or all defined
15160 breakpoints) so they once again become (or continue to be) effective
15161 in stopping the inferior. */
15162
15163 static void
15164 enable_command (char *args, int from_tty)
15165 {
15166 if (args == 0)
15167 {
15168 struct breakpoint *bpt;
15169
15170 ALL_BREAKPOINTS (bpt)
15171 if (user_breakpoint_p (bpt))
15172 enable_breakpoint (bpt);
15173 }
15174 else
15175 {
15176 char *num = extract_arg (&args);
15177
15178 while (num)
15179 {
15180 if (strchr (num, '.'))
15181 {
15182 struct bp_location *loc = find_location_by_number (num);
15183
15184 if (loc)
15185 {
15186 if (!loc->enabled)
15187 {
15188 loc->enabled = 1;
15189 mark_breakpoint_location_modified (loc);
15190 }
15191 if (target_supports_enable_disable_tracepoint ()
15192 && current_trace_status ()->running && loc->owner
15193 && is_tracepoint (loc->owner))
15194 target_enable_tracepoint (loc);
15195 }
15196 update_global_location_list (UGLL_MAY_INSERT);
15197 }
15198 else
15199 map_breakpoint_numbers (num, do_map_enable_breakpoint, NULL);
15200 num = extract_arg (&args);
15201 }
15202 }
15203 }
15204
15205 /* This struct packages up disposition data for application to multiple
15206 breakpoints. */
15207
15208 struct disp_data
15209 {
15210 enum bpdisp disp;
15211 int count;
15212 };
15213
15214 static void
15215 do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
15216 {
15217 struct disp_data disp_data = *(struct disp_data *) arg;
15218
15219 enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
15220 }
15221
15222 static void
15223 do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
15224 {
15225 struct disp_data disp = { disp_disable, 1 };
15226
15227 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
15228 }
15229
15230 static void
15231 enable_once_command (char *args, int from_tty)
15232 {
15233 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
15234 }
15235
15236 static void
15237 do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
15238 {
15239 struct disp_data disp = { disp_disable, *(int *) countptr };
15240
15241 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
15242 }
15243
15244 static void
15245 enable_count_command (char *args, int from_tty)
15246 {
15247 int count;
15248
15249 if (args == NULL)
15250 error_no_arg (_("hit count"));
15251
15252 count = get_number (&args);
15253
15254 map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
15255 }
15256
15257 static void
15258 do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
15259 {
15260 struct disp_data disp = { disp_del, 1 };
15261
15262 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
15263 }
15264
15265 static void
15266 enable_delete_command (char *args, int from_tty)
15267 {
15268 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
15269 }
15270 \f
15271 static void
15272 set_breakpoint_cmd (char *args, int from_tty)
15273 {
15274 }
15275
15276 static void
15277 show_breakpoint_cmd (char *args, int from_tty)
15278 {
15279 }
15280
15281 /* Invalidate last known value of any hardware watchpoint if
15282 the memory which that value represents has been written to by
15283 GDB itself. */
15284
15285 static void
15286 invalidate_bp_value_on_memory_change (struct inferior *inferior,
15287 CORE_ADDR addr, ssize_t len,
15288 const bfd_byte *data)
15289 {
15290 struct breakpoint *bp;
15291
15292 ALL_BREAKPOINTS (bp)
15293 if (bp->enable_state == bp_enabled
15294 && bp->type == bp_hardware_watchpoint)
15295 {
15296 struct watchpoint *wp = (struct watchpoint *) bp;
15297
15298 if (wp->val_valid && wp->val)
15299 {
15300 struct bp_location *loc;
15301
15302 for (loc = bp->loc; loc != NULL; loc = loc->next)
15303 if (loc->loc_type == bp_loc_hardware_watchpoint
15304 && loc->address + loc->length > addr
15305 && addr + len > loc->address)
15306 {
15307 value_free (wp->val);
15308 wp->val = NULL;
15309 wp->val_valid = 0;
15310 }
15311 }
15312 }
15313 }
15314
15315 /* Create and insert a breakpoint for software single step. */
15316
15317 void
15318 insert_single_step_breakpoint (struct gdbarch *gdbarch,
15319 struct address_space *aspace,
15320 CORE_ADDR next_pc)
15321 {
15322 struct thread_info *tp = inferior_thread ();
15323 struct symtab_and_line sal;
15324 CORE_ADDR pc = next_pc;
15325
15326 if (tp->control.single_step_breakpoints == NULL)
15327 {
15328 tp->control.single_step_breakpoints
15329 = new_single_step_breakpoint (tp->num, gdbarch);
15330 }
15331
15332 sal = find_pc_line (pc, 0);
15333 sal.pc = pc;
15334 sal.section = find_pc_overlay (pc);
15335 sal.explicit_pc = 1;
15336 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
15337
15338 update_global_location_list (UGLL_INSERT);
15339 }
15340
15341 /* See breakpoint.h. */
15342
15343 int
15344 breakpoint_has_location_inserted_here (struct breakpoint *bp,
15345 struct address_space *aspace,
15346 CORE_ADDR pc)
15347 {
15348 struct bp_location *loc;
15349
15350 for (loc = bp->loc; loc != NULL; loc = loc->next)
15351 if (loc->inserted
15352 && breakpoint_location_address_match (loc, aspace, pc))
15353 return 1;
15354
15355 return 0;
15356 }
15357
15358 /* Check whether a software single-step breakpoint is inserted at
15359 PC. */
15360
15361 int
15362 single_step_breakpoint_inserted_here_p (struct address_space *aspace,
15363 CORE_ADDR pc)
15364 {
15365 struct breakpoint *bpt;
15366
15367 ALL_BREAKPOINTS (bpt)
15368 {
15369 if (bpt->type == bp_single_step
15370 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
15371 return 1;
15372 }
15373 return 0;
15374 }
15375
15376 /* Returns 0 if 'bp' is NOT a syscall catchpoint,
15377 non-zero otherwise. */
15378 static int
15379 is_syscall_catchpoint_enabled (struct breakpoint *bp)
15380 {
15381 if (syscall_catchpoint_p (bp)
15382 && bp->enable_state != bp_disabled
15383 && bp->enable_state != bp_call_disabled)
15384 return 1;
15385 else
15386 return 0;
15387 }
15388
15389 int
15390 catch_syscall_enabled (void)
15391 {
15392 struct catch_syscall_inferior_data *inf_data
15393 = get_catch_syscall_inferior_data (current_inferior ());
15394
15395 return inf_data->total_syscalls_count != 0;
15396 }
15397
15398 int
15399 catching_syscall_number (int syscall_number)
15400 {
15401 struct breakpoint *bp;
15402
15403 ALL_BREAKPOINTS (bp)
15404 if (is_syscall_catchpoint_enabled (bp))
15405 {
15406 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bp;
15407
15408 if (c->syscalls_to_be_caught)
15409 {
15410 int i, iter;
15411 for (i = 0;
15412 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
15413 i++)
15414 if (syscall_number == iter)
15415 return 1;
15416 }
15417 else
15418 return 1;
15419 }
15420
15421 return 0;
15422 }
15423
15424 /* Complete syscall names. Used by "catch syscall". */
15425 static VEC (char_ptr) *
15426 catch_syscall_completer (struct cmd_list_element *cmd,
15427 const char *text, const char *word)
15428 {
15429 const char **list = get_syscall_names (get_current_arch ());
15430 VEC (char_ptr) *retlist
15431 = (list == NULL) ? NULL : complete_on_enum (list, word, word);
15432
15433 xfree (list);
15434 return retlist;
15435 }
15436
15437 /* Tracepoint-specific operations. */
15438
15439 /* Set tracepoint count to NUM. */
15440 static void
15441 set_tracepoint_count (int num)
15442 {
15443 tracepoint_count = num;
15444 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
15445 }
15446
15447 static void
15448 trace_command (char *arg, int from_tty)
15449 {
15450 struct breakpoint_ops *ops;
15451 const char *arg_cp = arg;
15452
15453 if (arg && probe_linespec_to_ops (&arg_cp))
15454 ops = &tracepoint_probe_breakpoint_ops;
15455 else
15456 ops = &tracepoint_breakpoint_ops;
15457
15458 create_breakpoint (get_current_arch (),
15459 arg,
15460 NULL, 0, NULL, 1 /* parse arg */,
15461 0 /* tempflag */,
15462 bp_tracepoint /* type_wanted */,
15463 0 /* Ignore count */,
15464 pending_break_support,
15465 ops,
15466 from_tty,
15467 1 /* enabled */,
15468 0 /* internal */, 0);
15469 }
15470
15471 static void
15472 ftrace_command (char *arg, int from_tty)
15473 {
15474 create_breakpoint (get_current_arch (),
15475 arg,
15476 NULL, 0, NULL, 1 /* parse arg */,
15477 0 /* tempflag */,
15478 bp_fast_tracepoint /* type_wanted */,
15479 0 /* Ignore count */,
15480 pending_break_support,
15481 &tracepoint_breakpoint_ops,
15482 from_tty,
15483 1 /* enabled */,
15484 0 /* internal */, 0);
15485 }
15486
15487 /* strace command implementation. Creates a static tracepoint. */
15488
15489 static void
15490 strace_command (char *arg, int from_tty)
15491 {
15492 struct breakpoint_ops *ops;
15493
15494 /* Decide if we are dealing with a static tracepoint marker (`-m'),
15495 or with a normal static tracepoint. */
15496 if (arg && strncmp (arg, "-m", 2) == 0 && isspace (arg[2]))
15497 ops = &strace_marker_breakpoint_ops;
15498 else
15499 ops = &tracepoint_breakpoint_ops;
15500
15501 create_breakpoint (get_current_arch (),
15502 arg,
15503 NULL, 0, NULL, 1 /* parse arg */,
15504 0 /* tempflag */,
15505 bp_static_tracepoint /* type_wanted */,
15506 0 /* Ignore count */,
15507 pending_break_support,
15508 ops,
15509 from_tty,
15510 1 /* enabled */,
15511 0 /* internal */, 0);
15512 }
15513
15514 /* Set up a fake reader function that gets command lines from a linked
15515 list that was acquired during tracepoint uploading. */
15516
15517 static struct uploaded_tp *this_utp;
15518 static int next_cmd;
15519
15520 static char *
15521 read_uploaded_action (void)
15522 {
15523 char *rslt;
15524
15525 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
15526
15527 next_cmd++;
15528
15529 return rslt;
15530 }
15531
15532 /* Given information about a tracepoint as recorded on a target (which
15533 can be either a live system or a trace file), attempt to create an
15534 equivalent GDB tracepoint. This is not a reliable process, since
15535 the target does not necessarily have all the information used when
15536 the tracepoint was originally defined. */
15537
15538 struct tracepoint *
15539 create_tracepoint_from_upload (struct uploaded_tp *utp)
15540 {
15541 char *addr_str, small_buf[100];
15542 struct tracepoint *tp;
15543
15544 if (utp->at_string)
15545 addr_str = utp->at_string;
15546 else
15547 {
15548 /* In the absence of a source location, fall back to raw
15549 address. Since there is no way to confirm that the address
15550 means the same thing as when the trace was started, warn the
15551 user. */
15552 warning (_("Uploaded tracepoint %d has no "
15553 "source location, using raw address"),
15554 utp->number);
15555 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
15556 addr_str = small_buf;
15557 }
15558
15559 /* There's not much we can do with a sequence of bytecodes. */
15560 if (utp->cond && !utp->cond_string)
15561 warning (_("Uploaded tracepoint %d condition "
15562 "has no source form, ignoring it"),
15563 utp->number);
15564
15565 if (!create_breakpoint (get_current_arch (),
15566 addr_str,
15567 utp->cond_string, -1, NULL,
15568 0 /* parse cond/thread */,
15569 0 /* tempflag */,
15570 utp->type /* type_wanted */,
15571 0 /* Ignore count */,
15572 pending_break_support,
15573 &tracepoint_breakpoint_ops,
15574 0 /* from_tty */,
15575 utp->enabled /* enabled */,
15576 0 /* internal */,
15577 CREATE_BREAKPOINT_FLAGS_INSERTED))
15578 return NULL;
15579
15580 /* Get the tracepoint we just created. */
15581 tp = get_tracepoint (tracepoint_count);
15582 gdb_assert (tp != NULL);
15583
15584 if (utp->pass > 0)
15585 {
15586 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
15587 tp->base.number);
15588
15589 trace_pass_command (small_buf, 0);
15590 }
15591
15592 /* If we have uploaded versions of the original commands, set up a
15593 special-purpose "reader" function and call the usual command line
15594 reader, then pass the result to the breakpoint command-setting
15595 function. */
15596 if (!VEC_empty (char_ptr, utp->cmd_strings))
15597 {
15598 struct command_line *cmd_list;
15599
15600 this_utp = utp;
15601 next_cmd = 0;
15602
15603 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
15604
15605 breakpoint_set_commands (&tp->base, cmd_list);
15606 }
15607 else if (!VEC_empty (char_ptr, utp->actions)
15608 || !VEC_empty (char_ptr, utp->step_actions))
15609 warning (_("Uploaded tracepoint %d actions "
15610 "have no source form, ignoring them"),
15611 utp->number);
15612
15613 /* Copy any status information that might be available. */
15614 tp->base.hit_count = utp->hit_count;
15615 tp->traceframe_usage = utp->traceframe_usage;
15616
15617 return tp;
15618 }
15619
15620 /* Print information on tracepoint number TPNUM_EXP, or all if
15621 omitted. */
15622
15623 static void
15624 tracepoints_info (char *args, int from_tty)
15625 {
15626 struct ui_out *uiout = current_uiout;
15627 int num_printed;
15628
15629 num_printed = breakpoint_1 (args, 0, is_tracepoint);
15630
15631 if (num_printed == 0)
15632 {
15633 if (args == NULL || *args == '\0')
15634 ui_out_message (uiout, 0, "No tracepoints.\n");
15635 else
15636 ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
15637 }
15638
15639 default_collect_info ();
15640 }
15641
15642 /* The 'enable trace' command enables tracepoints.
15643 Not supported by all targets. */
15644 static void
15645 enable_trace_command (char *args, int from_tty)
15646 {
15647 enable_command (args, from_tty);
15648 }
15649
15650 /* The 'disable trace' command disables tracepoints.
15651 Not supported by all targets. */
15652 static void
15653 disable_trace_command (char *args, int from_tty)
15654 {
15655 disable_command (args, from_tty);
15656 }
15657
15658 /* Remove a tracepoint (or all if no argument). */
15659 static void
15660 delete_trace_command (char *arg, int from_tty)
15661 {
15662 struct breakpoint *b, *b_tmp;
15663
15664 dont_repeat ();
15665
15666 if (arg == 0)
15667 {
15668 int breaks_to_delete = 0;
15669
15670 /* Delete all breakpoints if no argument.
15671 Do not delete internal or call-dummy breakpoints, these
15672 have to be deleted with an explicit breakpoint number
15673 argument. */
15674 ALL_TRACEPOINTS (b)
15675 if (is_tracepoint (b) && user_breakpoint_p (b))
15676 {
15677 breaks_to_delete = 1;
15678 break;
15679 }
15680
15681 /* Ask user only if there are some breakpoints to delete. */
15682 if (!from_tty
15683 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
15684 {
15685 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15686 if (is_tracepoint (b) && user_breakpoint_p (b))
15687 delete_breakpoint (b);
15688 }
15689 }
15690 else
15691 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
15692 }
15693
15694 /* Helper function for trace_pass_command. */
15695
15696 static void
15697 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
15698 {
15699 tp->pass_count = count;
15700 observer_notify_breakpoint_modified (&tp->base);
15701 if (from_tty)
15702 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
15703 tp->base.number, count);
15704 }
15705
15706 /* Set passcount for tracepoint.
15707
15708 First command argument is passcount, second is tracepoint number.
15709 If tracepoint number omitted, apply to most recently defined.
15710 Also accepts special argument "all". */
15711
15712 static void
15713 trace_pass_command (char *args, int from_tty)
15714 {
15715 struct tracepoint *t1;
15716 unsigned int count;
15717
15718 if (args == 0 || *args == 0)
15719 error (_("passcount command requires an "
15720 "argument (count + optional TP num)"));
15721
15722 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
15723
15724 args = skip_spaces (args);
15725 if (*args && strncasecmp (args, "all", 3) == 0)
15726 {
15727 struct breakpoint *b;
15728
15729 args += 3; /* Skip special argument "all". */
15730 if (*args)
15731 error (_("Junk at end of arguments."));
15732
15733 ALL_TRACEPOINTS (b)
15734 {
15735 t1 = (struct tracepoint *) b;
15736 trace_pass_set_count (t1, count, from_tty);
15737 }
15738 }
15739 else if (*args == '\0')
15740 {
15741 t1 = get_tracepoint_by_number (&args, NULL);
15742 if (t1)
15743 trace_pass_set_count (t1, count, from_tty);
15744 }
15745 else
15746 {
15747 struct get_number_or_range_state state;
15748
15749 init_number_or_range (&state, args);
15750 while (!state.finished)
15751 {
15752 t1 = get_tracepoint_by_number (&args, &state);
15753 if (t1)
15754 trace_pass_set_count (t1, count, from_tty);
15755 }
15756 }
15757 }
15758
15759 struct tracepoint *
15760 get_tracepoint (int num)
15761 {
15762 struct breakpoint *t;
15763
15764 ALL_TRACEPOINTS (t)
15765 if (t->number == num)
15766 return (struct tracepoint *) t;
15767
15768 return NULL;
15769 }
15770
15771 /* Find the tracepoint with the given target-side number (which may be
15772 different from the tracepoint number after disconnecting and
15773 reconnecting). */
15774
15775 struct tracepoint *
15776 get_tracepoint_by_number_on_target (int num)
15777 {
15778 struct breakpoint *b;
15779
15780 ALL_TRACEPOINTS (b)
15781 {
15782 struct tracepoint *t = (struct tracepoint *) b;
15783
15784 if (t->number_on_target == num)
15785 return t;
15786 }
15787
15788 return NULL;
15789 }
15790
15791 /* Utility: parse a tracepoint number and look it up in the list.
15792 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
15793 If the argument is missing, the most recent tracepoint
15794 (tracepoint_count) is returned. */
15795
15796 struct tracepoint *
15797 get_tracepoint_by_number (char **arg,
15798 struct get_number_or_range_state *state)
15799 {
15800 struct breakpoint *t;
15801 int tpnum;
15802 char *instring = arg == NULL ? NULL : *arg;
15803
15804 if (state)
15805 {
15806 gdb_assert (!state->finished);
15807 tpnum = get_number_or_range (state);
15808 }
15809 else if (arg == NULL || *arg == NULL || ! **arg)
15810 tpnum = tracepoint_count;
15811 else
15812 tpnum = get_number (arg);
15813
15814 if (tpnum <= 0)
15815 {
15816 if (instring && *instring)
15817 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15818 instring);
15819 else
15820 printf_filtered (_("No previous tracepoint\n"));
15821 return NULL;
15822 }
15823
15824 ALL_TRACEPOINTS (t)
15825 if (t->number == tpnum)
15826 {
15827 return (struct tracepoint *) t;
15828 }
15829
15830 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15831 return NULL;
15832 }
15833
15834 void
15835 print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15836 {
15837 if (b->thread != -1)
15838 fprintf_unfiltered (fp, " thread %d", b->thread);
15839
15840 if (b->task != 0)
15841 fprintf_unfiltered (fp, " task %d", b->task);
15842
15843 fprintf_unfiltered (fp, "\n");
15844 }
15845
15846 /* Save information on user settable breakpoints (watchpoints, etc) to
15847 a new script file named FILENAME. If FILTER is non-NULL, call it
15848 on each breakpoint and only include the ones for which it returns
15849 non-zero. */
15850
15851 static void
15852 save_breakpoints (char *filename, int from_tty,
15853 int (*filter) (const struct breakpoint *))
15854 {
15855 struct breakpoint *tp;
15856 int any = 0;
15857 struct cleanup *cleanup;
15858 struct ui_file *fp;
15859 int extra_trace_bits = 0;
15860
15861 if (filename == 0 || *filename == 0)
15862 error (_("Argument required (file name in which to save)"));
15863
15864 /* See if we have anything to save. */
15865 ALL_BREAKPOINTS (tp)
15866 {
15867 /* Skip internal and momentary breakpoints. */
15868 if (!user_breakpoint_p (tp))
15869 continue;
15870
15871 /* If we have a filter, only save the breakpoints it accepts. */
15872 if (filter && !filter (tp))
15873 continue;
15874
15875 any = 1;
15876
15877 if (is_tracepoint (tp))
15878 {
15879 extra_trace_bits = 1;
15880
15881 /* We can stop searching. */
15882 break;
15883 }
15884 }
15885
15886 if (!any)
15887 {
15888 warning (_("Nothing to save."));
15889 return;
15890 }
15891
15892 filename = tilde_expand (filename);
15893 cleanup = make_cleanup (xfree, filename);
15894 fp = gdb_fopen (filename, "w");
15895 if (!fp)
15896 error (_("Unable to open file '%s' for saving (%s)"),
15897 filename, safe_strerror (errno));
15898 make_cleanup_ui_file_delete (fp);
15899
15900 if (extra_trace_bits)
15901 save_trace_state_variables (fp);
15902
15903 ALL_BREAKPOINTS (tp)
15904 {
15905 /* Skip internal and momentary breakpoints. */
15906 if (!user_breakpoint_p (tp))
15907 continue;
15908
15909 /* If we have a filter, only save the breakpoints it accepts. */
15910 if (filter && !filter (tp))
15911 continue;
15912
15913 tp->ops->print_recreate (tp, fp);
15914
15915 /* Note, we can't rely on tp->number for anything, as we can't
15916 assume the recreated breakpoint numbers will match. Use $bpnum
15917 instead. */
15918
15919 if (tp->cond_string)
15920 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
15921
15922 if (tp->ignore_count)
15923 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
15924
15925 if (tp->type != bp_dprintf && tp->commands)
15926 {
15927 volatile struct gdb_exception ex;
15928
15929 fprintf_unfiltered (fp, " commands\n");
15930
15931 ui_out_redirect (current_uiout, fp);
15932 TRY_CATCH (ex, RETURN_MASK_ALL)
15933 {
15934 print_command_lines (current_uiout, tp->commands->commands, 2);
15935 }
15936 ui_out_redirect (current_uiout, NULL);
15937
15938 if (ex.reason < 0)
15939 throw_exception (ex);
15940
15941 fprintf_unfiltered (fp, " end\n");
15942 }
15943
15944 if (tp->enable_state == bp_disabled)
15945 fprintf_unfiltered (fp, "disable $bpnum\n");
15946
15947 /* If this is a multi-location breakpoint, check if the locations
15948 should be individually disabled. Watchpoint locations are
15949 special, and not user visible. */
15950 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15951 {
15952 struct bp_location *loc;
15953 int n = 1;
15954
15955 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15956 if (!loc->enabled)
15957 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
15958 }
15959 }
15960
15961 if (extra_trace_bits && *default_collect)
15962 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
15963
15964 if (from_tty)
15965 printf_filtered (_("Saved to file '%s'.\n"), filename);
15966 do_cleanups (cleanup);
15967 }
15968
15969 /* The `save breakpoints' command. */
15970
15971 static void
15972 save_breakpoints_command (char *args, int from_tty)
15973 {
15974 save_breakpoints (args, from_tty, NULL);
15975 }
15976
15977 /* The `save tracepoints' command. */
15978
15979 static void
15980 save_tracepoints_command (char *args, int from_tty)
15981 {
15982 save_breakpoints (args, from_tty, is_tracepoint);
15983 }
15984
15985 /* Create a vector of all tracepoints. */
15986
15987 VEC(breakpoint_p) *
15988 all_tracepoints (void)
15989 {
15990 VEC(breakpoint_p) *tp_vec = 0;
15991 struct breakpoint *tp;
15992
15993 ALL_TRACEPOINTS (tp)
15994 {
15995 VEC_safe_push (breakpoint_p, tp_vec, tp);
15996 }
15997
15998 return tp_vec;
15999 }
16000
16001 \f
16002 /* This help string is used for the break, hbreak, tbreak and thbreak
16003 commands. It is defined as a macro to prevent duplication.
16004 COMMAND should be a string constant containing the name of the
16005 command. */
16006 #define BREAK_ARGS_HELP(command) \
16007 command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
16008 PROBE_MODIFIER shall be present if the command is to be placed in a\n\
16009 probe point. Accepted values are `-probe' (for a generic, automatically\n\
16010 guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
16011 `-probe-dtrace' (for a DTrace probe).\n\
16012 LOCATION may be a line number, function name, or \"*\" and an address.\n\
16013 If a line number is specified, break at start of code for that line.\n\
16014 If a function is specified, break at start of code for that function.\n\
16015 If an address is specified, break at that exact address.\n\
16016 With no LOCATION, uses current execution address of the selected\n\
16017 stack frame. This is useful for breaking on return to a stack frame.\n\
16018 \n\
16019 THREADNUM is the number from \"info threads\".\n\
16020 CONDITION is a boolean expression.\n\
16021 \n\
16022 Multiple breakpoints at one place are permitted, and useful if their\n\
16023 conditions are different.\n\
16024 \n\
16025 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
16026
16027 /* List of subcommands for "catch". */
16028 static struct cmd_list_element *catch_cmdlist;
16029
16030 /* List of subcommands for "tcatch". */
16031 static struct cmd_list_element *tcatch_cmdlist;
16032
16033 void
16034 add_catch_command (char *name, char *docstring,
16035 cmd_sfunc_ftype *sfunc,
16036 completer_ftype *completer,
16037 void *user_data_catch,
16038 void *user_data_tcatch)
16039 {
16040 struct cmd_list_element *command;
16041
16042 command = add_cmd (name, class_breakpoint, NULL, docstring,
16043 &catch_cmdlist);
16044 set_cmd_sfunc (command, sfunc);
16045 set_cmd_context (command, user_data_catch);
16046 set_cmd_completer (command, completer);
16047
16048 command = add_cmd (name, class_breakpoint, NULL, docstring,
16049 &tcatch_cmdlist);
16050 set_cmd_sfunc (command, sfunc);
16051 set_cmd_context (command, user_data_tcatch);
16052 set_cmd_completer (command, completer);
16053 }
16054
16055 static void
16056 clear_syscall_counts (struct inferior *inf)
16057 {
16058 struct catch_syscall_inferior_data *inf_data
16059 = get_catch_syscall_inferior_data (inf);
16060
16061 inf_data->total_syscalls_count = 0;
16062 inf_data->any_syscall_count = 0;
16063 VEC_free (int, inf_data->syscalls_counts);
16064 }
16065
16066 static void
16067 save_command (char *arg, int from_tty)
16068 {
16069 printf_unfiltered (_("\"save\" must be followed by "
16070 "the name of a save subcommand.\n"));
16071 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
16072 }
16073
16074 struct breakpoint *
16075 iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
16076 void *data)
16077 {
16078 struct breakpoint *b, *b_tmp;
16079
16080 ALL_BREAKPOINTS_SAFE (b, b_tmp)
16081 {
16082 if ((*callback) (b, data))
16083 return b;
16084 }
16085
16086 return NULL;
16087 }
16088
16089 /* Zero if any of the breakpoint's locations could be a location where
16090 functions have been inlined, nonzero otherwise. */
16091
16092 static int
16093 is_non_inline_function (struct breakpoint *b)
16094 {
16095 /* The shared library event breakpoint is set on the address of a
16096 non-inline function. */
16097 if (b->type == bp_shlib_event)
16098 return 1;
16099
16100 return 0;
16101 }
16102
16103 /* Nonzero if the specified PC cannot be a location where functions
16104 have been inlined. */
16105
16106 int
16107 pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
16108 const struct target_waitstatus *ws)
16109 {
16110 struct breakpoint *b;
16111 struct bp_location *bl;
16112
16113 ALL_BREAKPOINTS (b)
16114 {
16115 if (!is_non_inline_function (b))
16116 continue;
16117
16118 for (bl = b->loc; bl != NULL; bl = bl->next)
16119 {
16120 if (!bl->shlib_disabled
16121 && bpstat_check_location (bl, aspace, pc, ws))
16122 return 1;
16123 }
16124 }
16125
16126 return 0;
16127 }
16128
16129 /* Remove any references to OBJFILE which is going to be freed. */
16130
16131 void
16132 breakpoint_free_objfile (struct objfile *objfile)
16133 {
16134 struct bp_location **locp, *loc;
16135
16136 ALL_BP_LOCATIONS (loc, locp)
16137 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
16138 loc->symtab = NULL;
16139 }
16140
16141 void
16142 initialize_breakpoint_ops (void)
16143 {
16144 static int initialized = 0;
16145
16146 struct breakpoint_ops *ops;
16147
16148 if (initialized)
16149 return;
16150 initialized = 1;
16151
16152 /* The breakpoint_ops structure to be inherit by all kinds of
16153 breakpoints (real breakpoints, i.e., user "break" breakpoints,
16154 internal and momentary breakpoints, etc.). */
16155 ops = &bkpt_base_breakpoint_ops;
16156 *ops = base_breakpoint_ops;
16157 ops->re_set = bkpt_re_set;
16158 ops->insert_location = bkpt_insert_location;
16159 ops->remove_location = bkpt_remove_location;
16160 ops->breakpoint_hit = bkpt_breakpoint_hit;
16161 ops->create_sals_from_address = bkpt_create_sals_from_address;
16162 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
16163 ops->decode_linespec = bkpt_decode_linespec;
16164
16165 /* The breakpoint_ops structure to be used in regular breakpoints. */
16166 ops = &bkpt_breakpoint_ops;
16167 *ops = bkpt_base_breakpoint_ops;
16168 ops->re_set = bkpt_re_set;
16169 ops->resources_needed = bkpt_resources_needed;
16170 ops->print_it = bkpt_print_it;
16171 ops->print_mention = bkpt_print_mention;
16172 ops->print_recreate = bkpt_print_recreate;
16173
16174 /* Ranged breakpoints. */
16175 ops = &ranged_breakpoint_ops;
16176 *ops = bkpt_breakpoint_ops;
16177 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
16178 ops->resources_needed = resources_needed_ranged_breakpoint;
16179 ops->print_it = print_it_ranged_breakpoint;
16180 ops->print_one = print_one_ranged_breakpoint;
16181 ops->print_one_detail = print_one_detail_ranged_breakpoint;
16182 ops->print_mention = print_mention_ranged_breakpoint;
16183 ops->print_recreate = print_recreate_ranged_breakpoint;
16184
16185 /* Internal breakpoints. */
16186 ops = &internal_breakpoint_ops;
16187 *ops = bkpt_base_breakpoint_ops;
16188 ops->re_set = internal_bkpt_re_set;
16189 ops->check_status = internal_bkpt_check_status;
16190 ops->print_it = internal_bkpt_print_it;
16191 ops->print_mention = internal_bkpt_print_mention;
16192
16193 /* Momentary breakpoints. */
16194 ops = &momentary_breakpoint_ops;
16195 *ops = bkpt_base_breakpoint_ops;
16196 ops->re_set = momentary_bkpt_re_set;
16197 ops->check_status = momentary_bkpt_check_status;
16198 ops->print_it = momentary_bkpt_print_it;
16199 ops->print_mention = momentary_bkpt_print_mention;
16200
16201 /* Momentary breakpoints for bp_longjmp and bp_exception. */
16202 ops = &longjmp_breakpoint_ops;
16203 *ops = momentary_breakpoint_ops;
16204 ops->dtor = longjmp_bkpt_dtor;
16205
16206 /* Probe breakpoints. */
16207 ops = &bkpt_probe_breakpoint_ops;
16208 *ops = bkpt_breakpoint_ops;
16209 ops->insert_location = bkpt_probe_insert_location;
16210 ops->remove_location = bkpt_probe_remove_location;
16211 ops->create_sals_from_address = bkpt_probe_create_sals_from_address;
16212 ops->decode_linespec = bkpt_probe_decode_linespec;
16213
16214 /* Watchpoints. */
16215 ops = &watchpoint_breakpoint_ops;
16216 *ops = base_breakpoint_ops;
16217 ops->dtor = dtor_watchpoint;
16218 ops->re_set = re_set_watchpoint;
16219 ops->insert_location = insert_watchpoint;
16220 ops->remove_location = remove_watchpoint;
16221 ops->breakpoint_hit = breakpoint_hit_watchpoint;
16222 ops->check_status = check_status_watchpoint;
16223 ops->resources_needed = resources_needed_watchpoint;
16224 ops->works_in_software_mode = works_in_software_mode_watchpoint;
16225 ops->print_it = print_it_watchpoint;
16226 ops->print_mention = print_mention_watchpoint;
16227 ops->print_recreate = print_recreate_watchpoint;
16228 ops->explains_signal = explains_signal_watchpoint;
16229
16230 /* Masked watchpoints. */
16231 ops = &masked_watchpoint_breakpoint_ops;
16232 *ops = watchpoint_breakpoint_ops;
16233 ops->insert_location = insert_masked_watchpoint;
16234 ops->remove_location = remove_masked_watchpoint;
16235 ops->resources_needed = resources_needed_masked_watchpoint;
16236 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
16237 ops->print_it = print_it_masked_watchpoint;
16238 ops->print_one_detail = print_one_detail_masked_watchpoint;
16239 ops->print_mention = print_mention_masked_watchpoint;
16240 ops->print_recreate = print_recreate_masked_watchpoint;
16241
16242 /* Tracepoints. */
16243 ops = &tracepoint_breakpoint_ops;
16244 *ops = base_breakpoint_ops;
16245 ops->re_set = tracepoint_re_set;
16246 ops->breakpoint_hit = tracepoint_breakpoint_hit;
16247 ops->print_one_detail = tracepoint_print_one_detail;
16248 ops->print_mention = tracepoint_print_mention;
16249 ops->print_recreate = tracepoint_print_recreate;
16250 ops->create_sals_from_address = tracepoint_create_sals_from_address;
16251 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
16252 ops->decode_linespec = tracepoint_decode_linespec;
16253
16254 /* Probe tracepoints. */
16255 ops = &tracepoint_probe_breakpoint_ops;
16256 *ops = tracepoint_breakpoint_ops;
16257 ops->create_sals_from_address = tracepoint_probe_create_sals_from_address;
16258 ops->decode_linespec = tracepoint_probe_decode_linespec;
16259
16260 /* Static tracepoints with marker (`-m'). */
16261 ops = &strace_marker_breakpoint_ops;
16262 *ops = tracepoint_breakpoint_ops;
16263 ops->create_sals_from_address = strace_marker_create_sals_from_address;
16264 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
16265 ops->decode_linespec = strace_marker_decode_linespec;
16266
16267 /* Fork catchpoints. */
16268 ops = &catch_fork_breakpoint_ops;
16269 *ops = base_breakpoint_ops;
16270 ops->insert_location = insert_catch_fork;
16271 ops->remove_location = remove_catch_fork;
16272 ops->breakpoint_hit = breakpoint_hit_catch_fork;
16273 ops->print_it = print_it_catch_fork;
16274 ops->print_one = print_one_catch_fork;
16275 ops->print_mention = print_mention_catch_fork;
16276 ops->print_recreate = print_recreate_catch_fork;
16277
16278 /* Vfork catchpoints. */
16279 ops = &catch_vfork_breakpoint_ops;
16280 *ops = base_breakpoint_ops;
16281 ops->insert_location = insert_catch_vfork;
16282 ops->remove_location = remove_catch_vfork;
16283 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
16284 ops->print_it = print_it_catch_vfork;
16285 ops->print_one = print_one_catch_vfork;
16286 ops->print_mention = print_mention_catch_vfork;
16287 ops->print_recreate = print_recreate_catch_vfork;
16288
16289 /* Exec catchpoints. */
16290 ops = &catch_exec_breakpoint_ops;
16291 *ops = base_breakpoint_ops;
16292 ops->dtor = dtor_catch_exec;
16293 ops->insert_location = insert_catch_exec;
16294 ops->remove_location = remove_catch_exec;
16295 ops->breakpoint_hit = breakpoint_hit_catch_exec;
16296 ops->print_it = print_it_catch_exec;
16297 ops->print_one = print_one_catch_exec;
16298 ops->print_mention = print_mention_catch_exec;
16299 ops->print_recreate = print_recreate_catch_exec;
16300
16301 /* Syscall catchpoints. */
16302 ops = &catch_syscall_breakpoint_ops;
16303 *ops = base_breakpoint_ops;
16304 ops->dtor = dtor_catch_syscall;
16305 ops->insert_location = insert_catch_syscall;
16306 ops->remove_location = remove_catch_syscall;
16307 ops->breakpoint_hit = breakpoint_hit_catch_syscall;
16308 ops->print_it = print_it_catch_syscall;
16309 ops->print_one = print_one_catch_syscall;
16310 ops->print_mention = print_mention_catch_syscall;
16311 ops->print_recreate = print_recreate_catch_syscall;
16312
16313 /* Solib-related catchpoints. */
16314 ops = &catch_solib_breakpoint_ops;
16315 *ops = base_breakpoint_ops;
16316 ops->dtor = dtor_catch_solib;
16317 ops->insert_location = insert_catch_solib;
16318 ops->remove_location = remove_catch_solib;
16319 ops->breakpoint_hit = breakpoint_hit_catch_solib;
16320 ops->check_status = check_status_catch_solib;
16321 ops->print_it = print_it_catch_solib;
16322 ops->print_one = print_one_catch_solib;
16323 ops->print_mention = print_mention_catch_solib;
16324 ops->print_recreate = print_recreate_catch_solib;
16325
16326 ops = &dprintf_breakpoint_ops;
16327 *ops = bkpt_base_breakpoint_ops;
16328 ops->re_set = dprintf_re_set;
16329 ops->resources_needed = bkpt_resources_needed;
16330 ops->print_it = bkpt_print_it;
16331 ops->print_mention = bkpt_print_mention;
16332 ops->print_recreate = dprintf_print_recreate;
16333 ops->after_condition_true = dprintf_after_condition_true;
16334 ops->breakpoint_hit = dprintf_breakpoint_hit;
16335 }
16336
16337 /* Chain containing all defined "enable breakpoint" subcommands. */
16338
16339 static struct cmd_list_element *enablebreaklist = NULL;
16340
16341 void
16342 _initialize_breakpoint (void)
16343 {
16344 struct cmd_list_element *c;
16345
16346 initialize_breakpoint_ops ();
16347
16348 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
16349 observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
16350 observer_attach_inferior_exit (clear_syscall_counts);
16351 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
16352
16353 breakpoint_objfile_key
16354 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
16355
16356 catch_syscall_inferior_data
16357 = register_inferior_data_with_cleanup (NULL,
16358 catch_syscall_inferior_data_cleanup);
16359
16360 breakpoint_chain = 0;
16361 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
16362 before a breakpoint is set. */
16363 breakpoint_count = 0;
16364
16365 tracepoint_count = 0;
16366
16367 add_com ("ignore", class_breakpoint, ignore_command, _("\
16368 Set ignore-count of breakpoint number N to COUNT.\n\
16369 Usage is `ignore N COUNT'."));
16370 if (xdb_commands)
16371 add_com_alias ("bc", "ignore", class_breakpoint, 1);
16372
16373 add_com ("commands", class_breakpoint, commands_command, _("\
16374 Set commands to be executed when a breakpoint is hit.\n\
16375 Give breakpoint number as argument after \"commands\".\n\
16376 With no argument, the targeted breakpoint is the last one set.\n\
16377 The commands themselves follow starting on the next line.\n\
16378 Type a line containing \"end\" to indicate the end of them.\n\
16379 Give \"silent\" as the first line to make the breakpoint silent;\n\
16380 then no output is printed when it is hit, except what the commands print."));
16381
16382 c = add_com ("condition", class_breakpoint, condition_command, _("\
16383 Specify breakpoint number N to break only if COND is true.\n\
16384 Usage is `condition N COND', where N is an integer and COND is an\n\
16385 expression to be evaluated whenever breakpoint N is reached."));
16386 set_cmd_completer (c, condition_completer);
16387
16388 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
16389 Set a temporary breakpoint.\n\
16390 Like \"break\" except the breakpoint is only temporary,\n\
16391 so it will be deleted when hit. Equivalent to \"break\" followed\n\
16392 by using \"enable delete\" on the breakpoint number.\n\
16393 \n"
16394 BREAK_ARGS_HELP ("tbreak")));
16395 set_cmd_completer (c, location_completer);
16396
16397 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
16398 Set a hardware assisted breakpoint.\n\
16399 Like \"break\" except the breakpoint requires hardware support,\n\
16400 some target hardware may not have this support.\n\
16401 \n"
16402 BREAK_ARGS_HELP ("hbreak")));
16403 set_cmd_completer (c, location_completer);
16404
16405 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
16406 Set a temporary hardware assisted breakpoint.\n\
16407 Like \"hbreak\" except the breakpoint is only temporary,\n\
16408 so it will be deleted when hit.\n\
16409 \n"
16410 BREAK_ARGS_HELP ("thbreak")));
16411 set_cmd_completer (c, location_completer);
16412
16413 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
16414 Enable some breakpoints.\n\
16415 Give breakpoint numbers (separated by spaces) as arguments.\n\
16416 With no subcommand, breakpoints are enabled until you command otherwise.\n\
16417 This is used to cancel the effect of the \"disable\" command.\n\
16418 With a subcommand you can enable temporarily."),
16419 &enablelist, "enable ", 1, &cmdlist);
16420 if (xdb_commands)
16421 add_com ("ab", class_breakpoint, enable_command, _("\
16422 Enable some breakpoints.\n\
16423 Give breakpoint numbers (separated by spaces) as arguments.\n\
16424 With no subcommand, breakpoints are enabled until you command otherwise.\n\
16425 This is used to cancel the effect of the \"disable\" command.\n\
16426 With a subcommand you can enable temporarily."));
16427
16428 add_com_alias ("en", "enable", class_breakpoint, 1);
16429
16430 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
16431 Enable some breakpoints.\n\
16432 Give breakpoint numbers (separated by spaces) as arguments.\n\
16433 This is used to cancel the effect of the \"disable\" command.\n\
16434 May be abbreviated to simply \"enable\".\n"),
16435 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
16436
16437 add_cmd ("once", no_class, enable_once_command, _("\
16438 Enable breakpoints for one hit. Give breakpoint numbers.\n\
16439 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
16440 &enablebreaklist);
16441
16442 add_cmd ("delete", no_class, enable_delete_command, _("\
16443 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16444 If a breakpoint is hit while enabled in this fashion, it is deleted."),
16445 &enablebreaklist);
16446
16447 add_cmd ("count", no_class, enable_count_command, _("\
16448 Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16449 If a breakpoint is hit while enabled in this fashion,\n\
16450 the count is decremented; when it reaches zero, the breakpoint is disabled."),
16451 &enablebreaklist);
16452
16453 add_cmd ("delete", no_class, enable_delete_command, _("\
16454 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16455 If a breakpoint is hit while enabled in this fashion, it is deleted."),
16456 &enablelist);
16457
16458 add_cmd ("once", no_class, enable_once_command, _("\
16459 Enable breakpoints for one hit. Give breakpoint numbers.\n\
16460 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
16461 &enablelist);
16462
16463 add_cmd ("count", no_class, enable_count_command, _("\
16464 Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16465 If a breakpoint is hit while enabled in this fashion,\n\
16466 the count is decremented; when it reaches zero, the breakpoint is disabled."),
16467 &enablelist);
16468
16469 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
16470 Disable some breakpoints.\n\
16471 Arguments are breakpoint numbers with spaces in between.\n\
16472 To disable all breakpoints, give no argument.\n\
16473 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
16474 &disablelist, "disable ", 1, &cmdlist);
16475 add_com_alias ("dis", "disable", class_breakpoint, 1);
16476 add_com_alias ("disa", "disable", class_breakpoint, 1);
16477 if (xdb_commands)
16478 add_com ("sb", class_breakpoint, disable_command, _("\
16479 Disable some breakpoints.\n\
16480 Arguments are breakpoint numbers with spaces in between.\n\
16481 To disable all breakpoints, give no argument.\n\
16482 A disabled breakpoint is not forgotten, but has no effect until re-enabled."));
16483
16484 add_cmd ("breakpoints", class_alias, disable_command, _("\
16485 Disable some breakpoints.\n\
16486 Arguments are breakpoint numbers with spaces in between.\n\
16487 To disable all breakpoints, give no argument.\n\
16488 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
16489 This command may be abbreviated \"disable\"."),
16490 &disablelist);
16491
16492 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
16493 Delete some breakpoints or auto-display expressions.\n\
16494 Arguments are breakpoint numbers with spaces in between.\n\
16495 To delete all breakpoints, give no argument.\n\
16496 \n\
16497 Also a prefix command for deletion of other GDB objects.\n\
16498 The \"unset\" command is also an alias for \"delete\"."),
16499 &deletelist, "delete ", 1, &cmdlist);
16500 add_com_alias ("d", "delete", class_breakpoint, 1);
16501 add_com_alias ("del", "delete", class_breakpoint, 1);
16502 if (xdb_commands)
16503 add_com ("db", class_breakpoint, delete_command, _("\
16504 Delete some breakpoints.\n\
16505 Arguments are breakpoint numbers with spaces in between.\n\
16506 To delete all breakpoints, give no argument.\n"));
16507
16508 add_cmd ("breakpoints", class_alias, delete_command, _("\
16509 Delete some breakpoints or auto-display expressions.\n\
16510 Arguments are breakpoint numbers with spaces in between.\n\
16511 To delete all breakpoints, give no argument.\n\
16512 This command may be abbreviated \"delete\"."),
16513 &deletelist);
16514
16515 add_com ("clear", class_breakpoint, clear_command, _("\
16516 Clear breakpoint at specified line or function.\n\
16517 Argument may be line number, function name, or \"*\" and an address.\n\
16518 If line number is specified, all breakpoints in that line are cleared.\n\
16519 If function is specified, breakpoints at beginning of function are cleared.\n\
16520 If an address is specified, breakpoints at that address are cleared.\n\
16521 \n\
16522 With no argument, clears all breakpoints in the line that the selected frame\n\
16523 is executing in.\n\
16524 \n\
16525 See also the \"delete\" command which clears breakpoints by number."));
16526 add_com_alias ("cl", "clear", class_breakpoint, 1);
16527
16528 c = add_com ("break", class_breakpoint, break_command, _("\
16529 Set breakpoint at specified line or function.\n"
16530 BREAK_ARGS_HELP ("break")));
16531 set_cmd_completer (c, location_completer);
16532
16533 add_com_alias ("b", "break", class_run, 1);
16534 add_com_alias ("br", "break", class_run, 1);
16535 add_com_alias ("bre", "break", class_run, 1);
16536 add_com_alias ("brea", "break", class_run, 1);
16537
16538 if (xdb_commands)
16539 add_com_alias ("ba", "break", class_breakpoint, 1);
16540
16541 if (dbx_commands)
16542 {
16543 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
16544 Break in function/address or break at a line in the current file."),
16545 &stoplist, "stop ", 1, &cmdlist);
16546 add_cmd ("in", class_breakpoint, stopin_command,
16547 _("Break in function or address."), &stoplist);
16548 add_cmd ("at", class_breakpoint, stopat_command,
16549 _("Break at a line in the current file."), &stoplist);
16550 add_com ("status", class_info, breakpoints_info, _("\
16551 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
16552 The \"Type\" column indicates one of:\n\
16553 \tbreakpoint - normal breakpoint\n\
16554 \twatchpoint - watchpoint\n\
16555 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16556 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16557 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16558 address and file/line number respectively.\n\
16559 \n\
16560 Convenience variable \"$_\" and default examine address for \"x\"\n\
16561 are set to the address of the last breakpoint listed unless the command\n\
16562 is prefixed with \"server \".\n\n\
16563 Convenience variable \"$bpnum\" contains the number of the last\n\
16564 breakpoint set."));
16565 }
16566
16567 add_info ("breakpoints", breakpoints_info, _("\
16568 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
16569 The \"Type\" column indicates one of:\n\
16570 \tbreakpoint - normal breakpoint\n\
16571 \twatchpoint - watchpoint\n\
16572 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16573 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16574 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16575 address and file/line number respectively.\n\
16576 \n\
16577 Convenience variable \"$_\" and default examine address for \"x\"\n\
16578 are set to the address of the last breakpoint listed unless the command\n\
16579 is prefixed with \"server \".\n\n\
16580 Convenience variable \"$bpnum\" contains the number of the last\n\
16581 breakpoint set."));
16582
16583 add_info_alias ("b", "breakpoints", 1);
16584
16585 if (xdb_commands)
16586 add_com ("lb", class_breakpoint, breakpoints_info, _("\
16587 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
16588 The \"Type\" column indicates one of:\n\
16589 \tbreakpoint - normal breakpoint\n\
16590 \twatchpoint - watchpoint\n\
16591 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16592 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16593 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16594 address and file/line number respectively.\n\
16595 \n\
16596 Convenience variable \"$_\" and default examine address for \"x\"\n\
16597 are set to the address of the last breakpoint listed unless the command\n\
16598 is prefixed with \"server \".\n\n\
16599 Convenience variable \"$bpnum\" contains the number of the last\n\
16600 breakpoint set."));
16601
16602 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
16603 Status of all breakpoints, or breakpoint number NUMBER.\n\
16604 The \"Type\" column indicates one of:\n\
16605 \tbreakpoint - normal breakpoint\n\
16606 \twatchpoint - watchpoint\n\
16607 \tlongjmp - internal breakpoint used to step through longjmp()\n\
16608 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
16609 \tuntil - internal breakpoint used by the \"until\" command\n\
16610 \tfinish - internal breakpoint used by the \"finish\" command\n\
16611 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16612 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16613 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16614 address and file/line number respectively.\n\
16615 \n\
16616 Convenience variable \"$_\" and default examine address for \"x\"\n\
16617 are set to the address of the last breakpoint listed unless the command\n\
16618 is prefixed with \"server \".\n\n\
16619 Convenience variable \"$bpnum\" contains the number of the last\n\
16620 breakpoint set."),
16621 &maintenanceinfolist);
16622
16623 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
16624 Set catchpoints to catch events."),
16625 &catch_cmdlist, "catch ",
16626 0/*allow-unknown*/, &cmdlist);
16627
16628 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
16629 Set temporary catchpoints to catch events."),
16630 &tcatch_cmdlist, "tcatch ",
16631 0/*allow-unknown*/, &cmdlist);
16632
16633 add_catch_command ("fork", _("Catch calls to fork."),
16634 catch_fork_command_1,
16635 NULL,
16636 (void *) (uintptr_t) catch_fork_permanent,
16637 (void *) (uintptr_t) catch_fork_temporary);
16638 add_catch_command ("vfork", _("Catch calls to vfork."),
16639 catch_fork_command_1,
16640 NULL,
16641 (void *) (uintptr_t) catch_vfork_permanent,
16642 (void *) (uintptr_t) catch_vfork_temporary);
16643 add_catch_command ("exec", _("Catch calls to exec."),
16644 catch_exec_command_1,
16645 NULL,
16646 CATCH_PERMANENT,
16647 CATCH_TEMPORARY);
16648 add_catch_command ("load", _("Catch loads of shared libraries.\n\
16649 Usage: catch load [REGEX]\n\
16650 If REGEX is given, only stop for libraries matching the regular expression."),
16651 catch_load_command_1,
16652 NULL,
16653 CATCH_PERMANENT,
16654 CATCH_TEMPORARY);
16655 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
16656 Usage: catch unload [REGEX]\n\
16657 If REGEX is given, only stop for libraries matching the regular expression."),
16658 catch_unload_command_1,
16659 NULL,
16660 CATCH_PERMANENT,
16661 CATCH_TEMPORARY);
16662 add_catch_command ("syscall", _("\
16663 Catch system calls by their names and/or numbers.\n\
16664 Arguments say which system calls to catch. If no arguments\n\
16665 are given, every system call will be caught.\n\
16666 Arguments, if given, should be one or more system call names\n\
16667 (if your system supports that), or system call numbers."),
16668 catch_syscall_command_1,
16669 catch_syscall_completer,
16670 CATCH_PERMANENT,
16671 CATCH_TEMPORARY);
16672
16673 c = add_com ("watch", class_breakpoint, watch_command, _("\
16674 Set a watchpoint for an expression.\n\
16675 Usage: watch [-l|-location] EXPRESSION\n\
16676 A watchpoint stops execution of your program whenever the value of\n\
16677 an expression changes.\n\
16678 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16679 the memory to which it refers."));
16680 set_cmd_completer (c, expression_completer);
16681
16682 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
16683 Set a read watchpoint for an expression.\n\
16684 Usage: rwatch [-l|-location] EXPRESSION\n\
16685 A watchpoint stops execution of your program whenever the value of\n\
16686 an expression is read.\n\
16687 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16688 the memory to which it refers."));
16689 set_cmd_completer (c, expression_completer);
16690
16691 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
16692 Set a watchpoint for an expression.\n\
16693 Usage: awatch [-l|-location] EXPRESSION\n\
16694 A watchpoint stops execution of your program whenever the value of\n\
16695 an expression is either read or written.\n\
16696 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16697 the memory to which it refers."));
16698 set_cmd_completer (c, expression_completer);
16699
16700 add_info ("watchpoints", watchpoints_info, _("\
16701 Status of specified watchpoints (all watchpoints if no argument)."));
16702
16703 /* XXX: cagney/2005-02-23: This should be a boolean, and should
16704 respond to changes - contrary to the description. */
16705 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
16706 &can_use_hw_watchpoints, _("\
16707 Set debugger's willingness to use watchpoint hardware."), _("\
16708 Show debugger's willingness to use watchpoint hardware."), _("\
16709 If zero, gdb will not use hardware for new watchpoints, even if\n\
16710 such is available. (However, any hardware watchpoints that were\n\
16711 created before setting this to nonzero, will continue to use watchpoint\n\
16712 hardware.)"),
16713 NULL,
16714 show_can_use_hw_watchpoints,
16715 &setlist, &showlist);
16716
16717 can_use_hw_watchpoints = 1;
16718
16719 /* Tracepoint manipulation commands. */
16720
16721 c = add_com ("trace", class_breakpoint, trace_command, _("\
16722 Set a tracepoint at specified line or function.\n\
16723 \n"
16724 BREAK_ARGS_HELP ("trace") "\n\
16725 Do \"help tracepoints\" for info on other tracepoint commands."));
16726 set_cmd_completer (c, location_completer);
16727
16728 add_com_alias ("tp", "trace", class_alias, 0);
16729 add_com_alias ("tr", "trace", class_alias, 1);
16730 add_com_alias ("tra", "trace", class_alias, 1);
16731 add_com_alias ("trac", "trace", class_alias, 1);
16732
16733 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
16734 Set a fast tracepoint at specified line or function.\n\
16735 \n"
16736 BREAK_ARGS_HELP ("ftrace") "\n\
16737 Do \"help tracepoints\" for info on other tracepoint commands."));
16738 set_cmd_completer (c, location_completer);
16739
16740 c = add_com ("strace", class_breakpoint, strace_command, _("\
16741 Set a static tracepoint at specified line, function or marker.\n\
16742 \n\
16743 strace [LOCATION] [if CONDITION]\n\
16744 LOCATION may be a line number, function name, \"*\" and an address,\n\
16745 or -m MARKER_ID.\n\
16746 If a line number is specified, probe the marker at start of code\n\
16747 for that line. If a function is specified, probe the marker at start\n\
16748 of code for that function. If an address is specified, probe the marker\n\
16749 at that exact address. If a marker id is specified, probe the marker\n\
16750 with that name. With no LOCATION, uses current execution address of\n\
16751 the selected stack frame.\n\
16752 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16753 This collects arbitrary user data passed in the probe point call to the\n\
16754 tracing library. You can inspect it when analyzing the trace buffer,\n\
16755 by printing the $_sdata variable like any other convenience variable.\n\
16756 \n\
16757 CONDITION is a boolean expression.\n\
16758 \n\
16759 Multiple tracepoints at one place are permitted, and useful if their\n\
16760 conditions are different.\n\
16761 \n\
16762 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16763 Do \"help tracepoints\" for info on other tracepoint commands."));
16764 set_cmd_completer (c, location_completer);
16765
16766 add_info ("tracepoints", tracepoints_info, _("\
16767 Status of specified tracepoints (all tracepoints if no argument).\n\
16768 Convenience variable \"$tpnum\" contains the number of the\n\
16769 last tracepoint set."));
16770
16771 add_info_alias ("tp", "tracepoints", 1);
16772
16773 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16774 Delete specified tracepoints.\n\
16775 Arguments are tracepoint numbers, separated by spaces.\n\
16776 No argument means delete all tracepoints."),
16777 &deletelist);
16778 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
16779
16780 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16781 Disable specified tracepoints.\n\
16782 Arguments are tracepoint numbers, separated by spaces.\n\
16783 No argument means disable all tracepoints."),
16784 &disablelist);
16785 deprecate_cmd (c, "disable");
16786
16787 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16788 Enable specified tracepoints.\n\
16789 Arguments are tracepoint numbers, separated by spaces.\n\
16790 No argument means enable all tracepoints."),
16791 &enablelist);
16792 deprecate_cmd (c, "enable");
16793
16794 add_com ("passcount", class_trace, trace_pass_command, _("\
16795 Set the passcount for a tracepoint.\n\
16796 The trace will end when the tracepoint has been passed 'count' times.\n\
16797 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16798 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16799
16800 add_prefix_cmd ("save", class_breakpoint, save_command,
16801 _("Save breakpoint definitions as a script."),
16802 &save_cmdlist, "save ",
16803 0/*allow-unknown*/, &cmdlist);
16804
16805 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16806 Save current breakpoint definitions as a script.\n\
16807 This includes all types of breakpoints (breakpoints, watchpoints,\n\
16808 catchpoints, tracepoints). Use the 'source' command in another debug\n\
16809 session to restore them."),
16810 &save_cmdlist);
16811 set_cmd_completer (c, filename_completer);
16812
16813 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
16814 Save current tracepoint definitions as a script.\n\
16815 Use the 'source' command in another debug session to restore them."),
16816 &save_cmdlist);
16817 set_cmd_completer (c, filename_completer);
16818
16819 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16820 deprecate_cmd (c, "save tracepoints");
16821
16822 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
16823 Breakpoint specific settings\n\
16824 Configure various breakpoint-specific variables such as\n\
16825 pending breakpoint behavior"),
16826 &breakpoint_set_cmdlist, "set breakpoint ",
16827 0/*allow-unknown*/, &setlist);
16828 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
16829 Breakpoint specific settings\n\
16830 Configure various breakpoint-specific variables such as\n\
16831 pending breakpoint behavior"),
16832 &breakpoint_show_cmdlist, "show breakpoint ",
16833 0/*allow-unknown*/, &showlist);
16834
16835 add_setshow_auto_boolean_cmd ("pending", no_class,
16836 &pending_break_support, _("\
16837 Set debugger's behavior regarding pending breakpoints."), _("\
16838 Show debugger's behavior regarding pending breakpoints."), _("\
16839 If on, an unrecognized breakpoint location will cause gdb to create a\n\
16840 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16841 an error. If auto, an unrecognized breakpoint location results in a\n\
16842 user-query to see if a pending breakpoint should be created."),
16843 NULL,
16844 show_pending_break_support,
16845 &breakpoint_set_cmdlist,
16846 &breakpoint_show_cmdlist);
16847
16848 pending_break_support = AUTO_BOOLEAN_AUTO;
16849
16850 add_setshow_boolean_cmd ("auto-hw", no_class,
16851 &automatic_hardware_breakpoints, _("\
16852 Set automatic usage of hardware breakpoints."), _("\
16853 Show automatic usage of hardware breakpoints."), _("\
16854 If set, the debugger will automatically use hardware breakpoints for\n\
16855 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16856 a warning will be emitted for such breakpoints."),
16857 NULL,
16858 show_automatic_hardware_breakpoints,
16859 &breakpoint_set_cmdlist,
16860 &breakpoint_show_cmdlist);
16861
16862 add_setshow_boolean_cmd ("always-inserted", class_support,
16863 &always_inserted_mode, _("\
16864 Set mode for inserting breakpoints."), _("\
16865 Show mode for inserting breakpoints."), _("\
16866 When this mode is on, breakpoints are inserted immediately as soon as\n\
16867 they're created, kept inserted even when execution stops, and removed\n\
16868 only when the user deletes them. When this mode is off (the default),\n\
16869 breakpoints are inserted only when execution continues, and removed\n\
16870 when execution stops."),
16871 NULL,
16872 &show_always_inserted_mode,
16873 &breakpoint_set_cmdlist,
16874 &breakpoint_show_cmdlist);
16875
16876 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16877 condition_evaluation_enums,
16878 &condition_evaluation_mode_1, _("\
16879 Set mode of breakpoint condition evaluation."), _("\
16880 Show mode of breakpoint condition evaluation."), _("\
16881 When this is set to \"host\", breakpoint conditions will be\n\
16882 evaluated on the host's side by GDB. When it is set to \"target\",\n\
16883 breakpoint conditions will be downloaded to the target (if the target\n\
16884 supports such feature) and conditions will be evaluated on the target's side.\n\
16885 If this is set to \"auto\" (default), this will be automatically set to\n\
16886 \"target\" if it supports condition evaluation, otherwise it will\n\
16887 be set to \"gdb\""),
16888 &set_condition_evaluation_mode,
16889 &show_condition_evaluation_mode,
16890 &breakpoint_set_cmdlist,
16891 &breakpoint_show_cmdlist);
16892
16893 add_com ("break-range", class_breakpoint, break_range_command, _("\
16894 Set a breakpoint for an address range.\n\
16895 break-range START-LOCATION, END-LOCATION\n\
16896 where START-LOCATION and END-LOCATION can be one of the following:\n\
16897 LINENUM, for that line in the current file,\n\
16898 FILE:LINENUM, for that line in that file,\n\
16899 +OFFSET, for that number of lines after the current line\n\
16900 or the start of the range\n\
16901 FUNCTION, for the first line in that function,\n\
16902 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16903 *ADDRESS, for the instruction at that address.\n\
16904 \n\
16905 The breakpoint will stop execution of the inferior whenever it executes\n\
16906 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16907 range (including START-LOCATION and END-LOCATION)."));
16908
16909 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
16910 Set a dynamic printf at specified line or function.\n\
16911 dprintf location,format string,arg1,arg2,...\n\
16912 location may be a line number, function name, or \"*\" and an address.\n\
16913 If a line number is specified, break at start of code for that line.\n\
16914 If a function is specified, break at start of code for that function."));
16915 set_cmd_completer (c, location_completer);
16916
16917 add_setshow_enum_cmd ("dprintf-style", class_support,
16918 dprintf_style_enums, &dprintf_style, _("\
16919 Set the style of usage for dynamic printf."), _("\
16920 Show the style of usage for dynamic printf."), _("\
16921 This setting chooses how GDB will do a dynamic printf.\n\
16922 If the value is \"gdb\", then the printing is done by GDB to its own\n\
16923 console, as with the \"printf\" command.\n\
16924 If the value is \"call\", the print is done by calling a function in your\n\
16925 program; by default printf(), but you can choose a different function or\n\
16926 output stream by setting dprintf-function and dprintf-channel."),
16927 update_dprintf_commands, NULL,
16928 &setlist, &showlist);
16929
16930 dprintf_function = xstrdup ("printf");
16931 add_setshow_string_cmd ("dprintf-function", class_support,
16932 &dprintf_function, _("\
16933 Set the function to use for dynamic printf"), _("\
16934 Show the function to use for dynamic printf"), NULL,
16935 update_dprintf_commands, NULL,
16936 &setlist, &showlist);
16937
16938 dprintf_channel = xstrdup ("");
16939 add_setshow_string_cmd ("dprintf-channel", class_support,
16940 &dprintf_channel, _("\
16941 Set the channel to use for dynamic printf"), _("\
16942 Show the channel to use for dynamic printf"), NULL,
16943 update_dprintf_commands, NULL,
16944 &setlist, &showlist);
16945
16946 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16947 &disconnected_dprintf, _("\
16948 Set whether dprintf continues after GDB disconnects."), _("\
16949 Show whether dprintf continues after GDB disconnects."), _("\
16950 Use this to let dprintf commands continue to hit and produce output\n\
16951 even if GDB disconnects or detaches from the target."),
16952 NULL,
16953 NULL,
16954 &setlist, &showlist);
16955
16956 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16957 agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16958 (target agent only) This is useful for formatted output in user-defined commands."));
16959
16960 automatic_hardware_breakpoints = 1;
16961
16962 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
16963 observer_attach_thread_exit (remove_threaded_breakpoints);
16964 }
This page took 0.603637 seconds and 4 git commands to generate.