Split TRY_CATCH into TRY + CATCH
[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
2225 if (!cond)
2226 return NULL;
2227
2228 /* We don't want to stop processing, so catch any errors
2229 that may show up. */
2230 TRY
2231 {
2232 aexpr = gen_eval_for_expr (scope, cond);
2233 }
2234
2235 CATCH (ex, RETURN_MASK_ERROR)
2236 {
2237 /* If we got here, it means the condition could not be parsed to a valid
2238 bytecode expression and thus can't be evaluated on the target's side.
2239 It's no use iterating through the conditions. */
2240 return NULL;
2241 }
2242 END_CATCH
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 const char *cmdrest;
2365 const char *format_start, *format_end;
2366 struct format_piece *fpieces;
2367 int nargs;
2368 struct gdbarch *gdbarch = get_current_arch ();
2369
2370 if (!cmd)
2371 return NULL;
2372
2373 cmdrest = cmd;
2374
2375 if (*cmdrest == ',')
2376 ++cmdrest;
2377 cmdrest = skip_spaces_const (cmdrest);
2378
2379 if (*cmdrest++ != '"')
2380 error (_("No format string following the location"));
2381
2382 format_start = cmdrest;
2383
2384 fpieces = parse_format_string (&cmdrest);
2385
2386 old_cleanups = make_cleanup (free_format_pieces_cleanup, &fpieces);
2387
2388 format_end = cmdrest;
2389
2390 if (*cmdrest++ != '"')
2391 error (_("Bad format string, non-terminated '\"'."));
2392
2393 cmdrest = skip_spaces_const (cmdrest);
2394
2395 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2396 error (_("Invalid argument syntax"));
2397
2398 if (*cmdrest == ',')
2399 cmdrest++;
2400 cmdrest = skip_spaces_const (cmdrest);
2401
2402 /* For each argument, make an expression. */
2403
2404 argvec = (struct expression **) alloca (strlen (cmd)
2405 * sizeof (struct expression *));
2406
2407 nargs = 0;
2408 while (*cmdrest != '\0')
2409 {
2410 const char *cmd1;
2411
2412 cmd1 = cmdrest;
2413 expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2414 argvec[nargs++] = expr;
2415 cmdrest = cmd1;
2416 if (*cmdrest == ',')
2417 ++cmdrest;
2418 }
2419
2420 /* We don't want to stop processing, so catch any errors
2421 that may show up. */
2422 TRY
2423 {
2424 aexpr = gen_printf (scope, gdbarch, 0, 0,
2425 format_start, format_end - format_start,
2426 fpieces, nargs, argvec);
2427 }
2428 CATCH (ex, RETURN_MASK_ERROR)
2429 {
2430 /* If we got here, it means the command could not be parsed to a valid
2431 bytecode expression and thus can't be evaluated on the target's side.
2432 It's no use iterating through the other commands. */
2433 aexpr = NULL;
2434 }
2435 END_CATCH
2436
2437 do_cleanups (old_cleanups);
2438
2439 /* We have a valid agent expression, return it. */
2440 return aexpr;
2441 }
2442
2443 /* Based on location BL, create a list of breakpoint commands to be
2444 passed on to the target. If we have duplicated locations with
2445 different commands, we will add any such to the list. */
2446
2447 static void
2448 build_target_command_list (struct bp_location *bl)
2449 {
2450 struct bp_location **locp = NULL, **loc2p;
2451 int null_command_or_parse_error = 0;
2452 int modified = bl->needs_update;
2453 struct bp_location *loc;
2454
2455 /* Release commands left over from a previous insert. */
2456 VEC_free (agent_expr_p, bl->target_info.tcommands);
2457
2458 if (!target_can_run_breakpoint_commands ())
2459 return;
2460
2461 /* For now, limit to agent-style dprintf breakpoints. */
2462 if (dprintf_style != dprintf_style_agent)
2463 return;
2464
2465 /* For now, if we have any duplicate location that isn't a dprintf,
2466 don't install the target-side commands, as that would make the
2467 breakpoint not be reported to the core, and we'd lose
2468 control. */
2469 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2470 {
2471 loc = (*loc2p);
2472 if (is_breakpoint (loc->owner)
2473 && loc->pspace->num == bl->pspace->num
2474 && loc->owner->type != bp_dprintf)
2475 return;
2476 }
2477
2478 /* Do a first pass to check for locations with no assigned
2479 conditions or conditions that fail to parse to a valid agent expression
2480 bytecode. If any of these happen, then it's no use to send conditions
2481 to the target since this location will always trigger and generate a
2482 response back to GDB. */
2483 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2484 {
2485 loc = (*loc2p);
2486 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2487 {
2488 if (modified)
2489 {
2490 struct agent_expr *aexpr;
2491
2492 /* Re-parse the commands since something changed. In that
2493 case we already freed the command bytecodes (see
2494 force_breakpoint_reinsertion). We just
2495 need to parse the command to bytecodes again. */
2496 aexpr = parse_cmd_to_aexpr (bl->address,
2497 loc->owner->extra_string);
2498 loc->cmd_bytecode = aexpr;
2499
2500 if (!aexpr)
2501 continue;
2502 }
2503
2504 /* If we have a NULL bytecode expression, it means something
2505 went wrong or we have a null command expression. */
2506 if (!loc->cmd_bytecode)
2507 {
2508 null_command_or_parse_error = 1;
2509 break;
2510 }
2511 }
2512 }
2513
2514 /* If anything failed, then we're not doing target-side commands,
2515 and so clean up. */
2516 if (null_command_or_parse_error)
2517 {
2518 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2519 {
2520 loc = (*loc2p);
2521 if (is_breakpoint (loc->owner)
2522 && loc->pspace->num == bl->pspace->num)
2523 {
2524 /* Only go as far as the first NULL bytecode is
2525 located. */
2526 if (loc->cmd_bytecode == NULL)
2527 return;
2528
2529 free_agent_expr (loc->cmd_bytecode);
2530 loc->cmd_bytecode = NULL;
2531 }
2532 }
2533 }
2534
2535 /* No NULL commands or failed bytecode generation. Build a command list
2536 for this location's address. */
2537 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2538 {
2539 loc = (*loc2p);
2540 if (loc->owner->extra_string
2541 && is_breakpoint (loc->owner)
2542 && loc->pspace->num == bl->pspace->num
2543 && loc->owner->enable_state == bp_enabled
2544 && loc->enabled)
2545 /* Add the command to the vector. This will be used later
2546 to send the commands to the target. */
2547 VEC_safe_push (agent_expr_p, bl->target_info.tcommands,
2548 loc->cmd_bytecode);
2549 }
2550
2551 bl->target_info.persist = 0;
2552 /* Maybe flag this location as persistent. */
2553 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2554 bl->target_info.persist = 1;
2555 }
2556
2557 /* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2558 location. Any error messages are printed to TMP_ERROR_STREAM; and
2559 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
2560 Returns 0 for success, 1 if the bp_location type is not supported or
2561 -1 for failure.
2562
2563 NOTE drow/2003-09-09: This routine could be broken down to an
2564 object-style method for each breakpoint or catchpoint type. */
2565 static int
2566 insert_bp_location (struct bp_location *bl,
2567 struct ui_file *tmp_error_stream,
2568 int *disabled_breaks,
2569 int *hw_breakpoint_error,
2570 int *hw_bp_error_explained_already)
2571 {
2572 enum errors bp_err = GDB_NO_ERROR;
2573 const char *bp_err_message = NULL;
2574
2575 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2576 return 0;
2577
2578 /* Note we don't initialize bl->target_info, as that wipes out
2579 the breakpoint location's shadow_contents if the breakpoint
2580 is still inserted at that location. This in turn breaks
2581 target_read_memory which depends on these buffers when
2582 a memory read is requested at the breakpoint location:
2583 Once the target_info has been wiped, we fail to see that
2584 we have a breakpoint inserted at that address and thus
2585 read the breakpoint instead of returning the data saved in
2586 the breakpoint location's shadow contents. */
2587 bl->target_info.reqstd_address = bl->address;
2588 bl->target_info.placed_address_space = bl->pspace->aspace;
2589 bl->target_info.length = bl->length;
2590
2591 /* When working with target-side conditions, we must pass all the conditions
2592 for the same breakpoint address down to the target since GDB will not
2593 insert those locations. With a list of breakpoint conditions, the target
2594 can decide when to stop and notify GDB. */
2595
2596 if (is_breakpoint (bl->owner))
2597 {
2598 build_target_condition_list (bl);
2599 build_target_command_list (bl);
2600 /* Reset the modification marker. */
2601 bl->needs_update = 0;
2602 }
2603
2604 if (bl->loc_type == bp_loc_software_breakpoint
2605 || bl->loc_type == bp_loc_hardware_breakpoint)
2606 {
2607 if (bl->owner->type != bp_hardware_breakpoint)
2608 {
2609 /* If the explicitly specified breakpoint type
2610 is not hardware breakpoint, check the memory map to see
2611 if the breakpoint address is in read only memory or not.
2612
2613 Two important cases are:
2614 - location type is not hardware breakpoint, memory
2615 is readonly. We change the type of the location to
2616 hardware breakpoint.
2617 - location type is hardware breakpoint, memory is
2618 read-write. This means we've previously made the
2619 location hardware one, but then the memory map changed,
2620 so we undo.
2621
2622 When breakpoints are removed, remove_breakpoints will use
2623 location types we've just set here, the only possible
2624 problem is that memory map has changed during running
2625 program, but it's not going to work anyway with current
2626 gdb. */
2627 struct mem_region *mr
2628 = lookup_mem_region (bl->target_info.reqstd_address);
2629
2630 if (mr)
2631 {
2632 if (automatic_hardware_breakpoints)
2633 {
2634 enum bp_loc_type new_type;
2635
2636 if (mr->attrib.mode != MEM_RW)
2637 new_type = bp_loc_hardware_breakpoint;
2638 else
2639 new_type = bp_loc_software_breakpoint;
2640
2641 if (new_type != bl->loc_type)
2642 {
2643 static int said = 0;
2644
2645 bl->loc_type = new_type;
2646 if (!said)
2647 {
2648 fprintf_filtered (gdb_stdout,
2649 _("Note: automatically using "
2650 "hardware breakpoints for "
2651 "read-only addresses.\n"));
2652 said = 1;
2653 }
2654 }
2655 }
2656 else if (bl->loc_type == bp_loc_software_breakpoint
2657 && mr->attrib.mode != MEM_RW)
2658 {
2659 fprintf_unfiltered (tmp_error_stream,
2660 _("Cannot insert breakpoint %d.\n"
2661 "Cannot set software breakpoint "
2662 "at read-only address %s\n"),
2663 bl->owner->number,
2664 paddress (bl->gdbarch, bl->address));
2665 return 1;
2666 }
2667 }
2668 }
2669
2670 /* First check to see if we have to handle an overlay. */
2671 if (overlay_debugging == ovly_off
2672 || bl->section == NULL
2673 || !(section_is_overlay (bl->section)))
2674 {
2675 /* No overlay handling: just set the breakpoint. */
2676 TRY
2677 {
2678 int val;
2679
2680 val = bl->owner->ops->insert_location (bl);
2681 if (val)
2682 bp_err = GENERIC_ERROR;
2683 }
2684 CATCH (e, RETURN_MASK_ALL)
2685 {
2686 bp_err = e.error;
2687 bp_err_message = e.message;
2688 }
2689 END_CATCH
2690 }
2691 else
2692 {
2693 /* This breakpoint is in an overlay section.
2694 Shall we set a breakpoint at the LMA? */
2695 if (!overlay_events_enabled)
2696 {
2697 /* Yes -- overlay event support is not active,
2698 so we must try to set a breakpoint at the LMA.
2699 This will not work for a hardware breakpoint. */
2700 if (bl->loc_type == bp_loc_hardware_breakpoint)
2701 warning (_("hardware breakpoint %d not supported in overlay!"),
2702 bl->owner->number);
2703 else
2704 {
2705 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2706 bl->section);
2707 /* Set a software (trap) breakpoint at the LMA. */
2708 bl->overlay_target_info = bl->target_info;
2709 bl->overlay_target_info.reqstd_address = addr;
2710
2711 /* No overlay handling: just set the breakpoint. */
2712 TRY
2713 {
2714 int val;
2715
2716 val = target_insert_breakpoint (bl->gdbarch,
2717 &bl->overlay_target_info);
2718 if (val)
2719 bp_err = GENERIC_ERROR;
2720 }
2721 CATCH (e, RETURN_MASK_ALL)
2722 {
2723 bp_err = e.error;
2724 bp_err_message = e.message;
2725 }
2726 END_CATCH
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
2740 {
2741 int val;
2742
2743 val = bl->owner->ops->insert_location (bl);
2744 if (val)
2745 bp_err = GENERIC_ERROR;
2746 }
2747 CATCH (e, RETURN_MASK_ALL)
2748 {
2749 bp_err = e.error;
2750 bp_err_message = e.message;
2751 }
2752 END_CATCH
2753 }
2754 else
2755 {
2756 /* No. This breakpoint will not be inserted.
2757 No error, but do not mark the bp as 'inserted'. */
2758 return 0;
2759 }
2760 }
2761
2762 if (bp_err != GDB_NO_ERROR)
2763 {
2764 /* Can't set the breakpoint. */
2765
2766 /* In some cases, we might not be able to insert a
2767 breakpoint in a shared library that has already been
2768 removed, but we have not yet processed the shlib unload
2769 event. Unfortunately, some targets that implement
2770 breakpoint insertion themselves can't tell why the
2771 breakpoint insertion failed (e.g., the remote target
2772 doesn't define error codes), so we must treat generic
2773 errors as memory errors. */
2774 if ((bp_err == GENERIC_ERROR || bp_err == MEMORY_ERROR)
2775 && bl->loc_type == bp_loc_software_breakpoint
2776 && (solib_name_from_address (bl->pspace, bl->address)
2777 || shared_objfile_contains_address_p (bl->pspace,
2778 bl->address)))
2779 {
2780 /* See also: disable_breakpoints_in_shlibs. */
2781 bl->shlib_disabled = 1;
2782 observer_notify_breakpoint_modified (bl->owner);
2783 if (!*disabled_breaks)
2784 {
2785 fprintf_unfiltered (tmp_error_stream,
2786 "Cannot insert breakpoint %d.\n",
2787 bl->owner->number);
2788 fprintf_unfiltered (tmp_error_stream,
2789 "Temporarily disabling shared "
2790 "library breakpoints:\n");
2791 }
2792 *disabled_breaks = 1;
2793 fprintf_unfiltered (tmp_error_stream,
2794 "breakpoint #%d\n", bl->owner->number);
2795 return 0;
2796 }
2797 else
2798 {
2799 if (bl->loc_type == bp_loc_hardware_breakpoint)
2800 {
2801 *hw_breakpoint_error = 1;
2802 *hw_bp_error_explained_already = bp_err_message != NULL;
2803 fprintf_unfiltered (tmp_error_stream,
2804 "Cannot insert hardware breakpoint %d%s",
2805 bl->owner->number, bp_err_message ? ":" : ".\n");
2806 if (bp_err_message != NULL)
2807 fprintf_unfiltered (tmp_error_stream, "%s.\n", bp_err_message);
2808 }
2809 else
2810 {
2811 if (bp_err_message == NULL)
2812 {
2813 char *message
2814 = memory_error_message (TARGET_XFER_E_IO,
2815 bl->gdbarch, bl->address);
2816 struct cleanup *old_chain = make_cleanup (xfree, message);
2817
2818 fprintf_unfiltered (tmp_error_stream,
2819 "Cannot insert breakpoint %d.\n"
2820 "%s\n",
2821 bl->owner->number, message);
2822 do_cleanups (old_chain);
2823 }
2824 else
2825 {
2826 fprintf_unfiltered (tmp_error_stream,
2827 "Cannot insert breakpoint %d: %s\n",
2828 bl->owner->number,
2829 bp_err_message);
2830 }
2831 }
2832 return 1;
2833
2834 }
2835 }
2836 else
2837 bl->inserted = 1;
2838
2839 return 0;
2840 }
2841
2842 else if (bl->loc_type == bp_loc_hardware_watchpoint
2843 /* NOTE drow/2003-09-08: This state only exists for removing
2844 watchpoints. It's not clear that it's necessary... */
2845 && bl->owner->disposition != disp_del_at_next_stop)
2846 {
2847 int val;
2848
2849 gdb_assert (bl->owner->ops != NULL
2850 && bl->owner->ops->insert_location != NULL);
2851
2852 val = bl->owner->ops->insert_location (bl);
2853
2854 /* If trying to set a read-watchpoint, and it turns out it's not
2855 supported, try emulating one with an access watchpoint. */
2856 if (val == 1 && bl->watchpoint_type == hw_read)
2857 {
2858 struct bp_location *loc, **loc_temp;
2859
2860 /* But don't try to insert it, if there's already another
2861 hw_access location that would be considered a duplicate
2862 of this one. */
2863 ALL_BP_LOCATIONS (loc, loc_temp)
2864 if (loc != bl
2865 && loc->watchpoint_type == hw_access
2866 && watchpoint_locations_match (bl, loc))
2867 {
2868 bl->duplicate = 1;
2869 bl->inserted = 1;
2870 bl->target_info = loc->target_info;
2871 bl->watchpoint_type = hw_access;
2872 val = 0;
2873 break;
2874 }
2875
2876 if (val == 1)
2877 {
2878 bl->watchpoint_type = hw_access;
2879 val = bl->owner->ops->insert_location (bl);
2880
2881 if (val)
2882 /* Back to the original value. */
2883 bl->watchpoint_type = hw_read;
2884 }
2885 }
2886
2887 bl->inserted = (val == 0);
2888 }
2889
2890 else if (bl->owner->type == bp_catchpoint)
2891 {
2892 int val;
2893
2894 gdb_assert (bl->owner->ops != NULL
2895 && bl->owner->ops->insert_location != NULL);
2896
2897 val = bl->owner->ops->insert_location (bl);
2898 if (val)
2899 {
2900 bl->owner->enable_state = bp_disabled;
2901
2902 if (val == 1)
2903 warning (_("\
2904 Error inserting catchpoint %d: Your system does not support this type\n\
2905 of catchpoint."), bl->owner->number);
2906 else
2907 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2908 }
2909
2910 bl->inserted = (val == 0);
2911
2912 /* We've already printed an error message if there was a problem
2913 inserting this catchpoint, and we've disabled the catchpoint,
2914 so just return success. */
2915 return 0;
2916 }
2917
2918 return 0;
2919 }
2920
2921 /* This function is called when program space PSPACE is about to be
2922 deleted. It takes care of updating breakpoints to not reference
2923 PSPACE anymore. */
2924
2925 void
2926 breakpoint_program_space_exit (struct program_space *pspace)
2927 {
2928 struct breakpoint *b, *b_temp;
2929 struct bp_location *loc, **loc_temp;
2930
2931 /* Remove any breakpoint that was set through this program space. */
2932 ALL_BREAKPOINTS_SAFE (b, b_temp)
2933 {
2934 if (b->pspace == pspace)
2935 delete_breakpoint (b);
2936 }
2937
2938 /* Breakpoints set through other program spaces could have locations
2939 bound to PSPACE as well. Remove those. */
2940 ALL_BP_LOCATIONS (loc, loc_temp)
2941 {
2942 struct bp_location *tmp;
2943
2944 if (loc->pspace == pspace)
2945 {
2946 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
2947 if (loc->owner->loc == loc)
2948 loc->owner->loc = loc->next;
2949 else
2950 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2951 if (tmp->next == loc)
2952 {
2953 tmp->next = loc->next;
2954 break;
2955 }
2956 }
2957 }
2958
2959 /* Now update the global location list to permanently delete the
2960 removed locations above. */
2961 update_global_location_list (UGLL_DONT_INSERT);
2962 }
2963
2964 /* Make sure all breakpoints are inserted in inferior.
2965 Throws exception on any error.
2966 A breakpoint that is already inserted won't be inserted
2967 again, so calling this function twice is safe. */
2968 void
2969 insert_breakpoints (void)
2970 {
2971 struct breakpoint *bpt;
2972
2973 ALL_BREAKPOINTS (bpt)
2974 if (is_hardware_watchpoint (bpt))
2975 {
2976 struct watchpoint *w = (struct watchpoint *) bpt;
2977
2978 update_watchpoint (w, 0 /* don't reparse. */);
2979 }
2980
2981 /* Updating watchpoints creates new locations, so update the global
2982 location list. Explicitly tell ugll to insert locations and
2983 ignore breakpoints_always_inserted_mode. */
2984 update_global_location_list (UGLL_INSERT);
2985 }
2986
2987 /* Invoke CALLBACK for each of bp_location. */
2988
2989 void
2990 iterate_over_bp_locations (walk_bp_location_callback callback)
2991 {
2992 struct bp_location *loc, **loc_tmp;
2993
2994 ALL_BP_LOCATIONS (loc, loc_tmp)
2995 {
2996 callback (loc, NULL);
2997 }
2998 }
2999
3000 /* This is used when we need to synch breakpoint conditions between GDB and the
3001 target. It is the case with deleting and disabling of breakpoints when using
3002 always-inserted mode. */
3003
3004 static void
3005 update_inserted_breakpoint_locations (void)
3006 {
3007 struct bp_location *bl, **blp_tmp;
3008 int error_flag = 0;
3009 int val = 0;
3010 int disabled_breaks = 0;
3011 int hw_breakpoint_error = 0;
3012 int hw_bp_details_reported = 0;
3013
3014 struct ui_file *tmp_error_stream = mem_fileopen ();
3015 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
3016
3017 /* Explicitly mark the warning -- this will only be printed if
3018 there was an error. */
3019 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
3020
3021 save_current_space_and_thread ();
3022
3023 ALL_BP_LOCATIONS (bl, blp_tmp)
3024 {
3025 /* We only want to update software breakpoints and hardware
3026 breakpoints. */
3027 if (!is_breakpoint (bl->owner))
3028 continue;
3029
3030 /* We only want to update locations that are already inserted
3031 and need updating. This is to avoid unwanted insertion during
3032 deletion of breakpoints. */
3033 if (!bl->inserted || (bl->inserted && !bl->needs_update))
3034 continue;
3035
3036 switch_to_program_space_and_thread (bl->pspace);
3037
3038 /* For targets that support global breakpoints, there's no need
3039 to select an inferior to insert breakpoint to. In fact, even
3040 if we aren't attached to any process yet, we should still
3041 insert breakpoints. */
3042 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3043 && ptid_equal (inferior_ptid, null_ptid))
3044 continue;
3045
3046 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
3047 &hw_breakpoint_error, &hw_bp_details_reported);
3048 if (val)
3049 error_flag = val;
3050 }
3051
3052 if (error_flag)
3053 {
3054 target_terminal_ours_for_output ();
3055 error_stream (tmp_error_stream);
3056 }
3057
3058 do_cleanups (cleanups);
3059 }
3060
3061 /* Used when starting or continuing the program. */
3062
3063 static void
3064 insert_breakpoint_locations (void)
3065 {
3066 struct breakpoint *bpt;
3067 struct bp_location *bl, **blp_tmp;
3068 int error_flag = 0;
3069 int val = 0;
3070 int disabled_breaks = 0;
3071 int hw_breakpoint_error = 0;
3072 int hw_bp_error_explained_already = 0;
3073
3074 struct ui_file *tmp_error_stream = mem_fileopen ();
3075 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
3076
3077 /* Explicitly mark the warning -- this will only be printed if
3078 there was an error. */
3079 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
3080
3081 save_current_space_and_thread ();
3082
3083 ALL_BP_LOCATIONS (bl, blp_tmp)
3084 {
3085 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
3086 continue;
3087
3088 /* There is no point inserting thread-specific breakpoints if
3089 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3090 has BL->OWNER always non-NULL. */
3091 if (bl->owner->thread != -1
3092 && !valid_thread_id (bl->owner->thread))
3093 continue;
3094
3095 switch_to_program_space_and_thread (bl->pspace);
3096
3097 /* For targets that support global breakpoints, there's no need
3098 to select an inferior to insert breakpoint to. In fact, even
3099 if we aren't attached to any process yet, we should still
3100 insert breakpoints. */
3101 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3102 && ptid_equal (inferior_ptid, null_ptid))
3103 continue;
3104
3105 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
3106 &hw_breakpoint_error, &hw_bp_error_explained_already);
3107 if (val)
3108 error_flag = val;
3109 }
3110
3111 /* If we failed to insert all locations of a watchpoint, remove
3112 them, as half-inserted watchpoint is of limited use. */
3113 ALL_BREAKPOINTS (bpt)
3114 {
3115 int some_failed = 0;
3116 struct bp_location *loc;
3117
3118 if (!is_hardware_watchpoint (bpt))
3119 continue;
3120
3121 if (!breakpoint_enabled (bpt))
3122 continue;
3123
3124 if (bpt->disposition == disp_del_at_next_stop)
3125 continue;
3126
3127 for (loc = bpt->loc; loc; loc = loc->next)
3128 if (!loc->inserted && should_be_inserted (loc))
3129 {
3130 some_failed = 1;
3131 break;
3132 }
3133 if (some_failed)
3134 {
3135 for (loc = bpt->loc; loc; loc = loc->next)
3136 if (loc->inserted)
3137 remove_breakpoint (loc, mark_uninserted);
3138
3139 hw_breakpoint_error = 1;
3140 fprintf_unfiltered (tmp_error_stream,
3141 "Could not insert hardware watchpoint %d.\n",
3142 bpt->number);
3143 error_flag = -1;
3144 }
3145 }
3146
3147 if (error_flag)
3148 {
3149 /* If a hardware breakpoint or watchpoint was inserted, add a
3150 message about possibly exhausted resources. */
3151 if (hw_breakpoint_error && !hw_bp_error_explained_already)
3152 {
3153 fprintf_unfiltered (tmp_error_stream,
3154 "Could not insert hardware breakpoints:\n\
3155 You may have requested too many hardware breakpoints/watchpoints.\n");
3156 }
3157 target_terminal_ours_for_output ();
3158 error_stream (tmp_error_stream);
3159 }
3160
3161 do_cleanups (cleanups);
3162 }
3163
3164 /* Used when the program stops.
3165 Returns zero if successful, or non-zero if there was a problem
3166 removing a breakpoint location. */
3167
3168 int
3169 remove_breakpoints (void)
3170 {
3171 struct bp_location *bl, **blp_tmp;
3172 int val = 0;
3173
3174 ALL_BP_LOCATIONS (bl, blp_tmp)
3175 {
3176 if (bl->inserted && !is_tracepoint (bl->owner))
3177 val |= remove_breakpoint (bl, mark_uninserted);
3178 }
3179 return val;
3180 }
3181
3182 /* When a thread exits, remove breakpoints that are related to
3183 that thread. */
3184
3185 static void
3186 remove_threaded_breakpoints (struct thread_info *tp, int silent)
3187 {
3188 struct breakpoint *b, *b_tmp;
3189
3190 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3191 {
3192 if (b->thread == tp->num && user_breakpoint_p (b))
3193 {
3194 b->disposition = disp_del_at_next_stop;
3195
3196 printf_filtered (_("\
3197 Thread-specific breakpoint %d deleted - thread %d no longer in the thread list.\n"),
3198 b->number, tp->num);
3199
3200 /* Hide it from the user. */
3201 b->number = 0;
3202 }
3203 }
3204 }
3205
3206 /* Remove breakpoints of process PID. */
3207
3208 int
3209 remove_breakpoints_pid (int pid)
3210 {
3211 struct bp_location *bl, **blp_tmp;
3212 int val;
3213 struct inferior *inf = find_inferior_pid (pid);
3214
3215 ALL_BP_LOCATIONS (bl, blp_tmp)
3216 {
3217 if (bl->pspace != inf->pspace)
3218 continue;
3219
3220 if (bl->inserted && !bl->target_info.persist)
3221 {
3222 val = remove_breakpoint (bl, mark_uninserted);
3223 if (val != 0)
3224 return val;
3225 }
3226 }
3227 return 0;
3228 }
3229
3230 int
3231 reattach_breakpoints (int pid)
3232 {
3233 struct cleanup *old_chain;
3234 struct bp_location *bl, **blp_tmp;
3235 int val;
3236 struct ui_file *tmp_error_stream;
3237 int dummy1 = 0, dummy2 = 0, dummy3 = 0;
3238 struct inferior *inf;
3239 struct thread_info *tp;
3240
3241 tp = any_live_thread_of_process (pid);
3242 if (tp == NULL)
3243 return 1;
3244
3245 inf = find_inferior_pid (pid);
3246 old_chain = save_inferior_ptid ();
3247
3248 inferior_ptid = tp->ptid;
3249
3250 tmp_error_stream = mem_fileopen ();
3251 make_cleanup_ui_file_delete (tmp_error_stream);
3252
3253 ALL_BP_LOCATIONS (bl, blp_tmp)
3254 {
3255 if (bl->pspace != inf->pspace)
3256 continue;
3257
3258 if (bl->inserted)
3259 {
3260 bl->inserted = 0;
3261 val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2, &dummy3);
3262 if (val != 0)
3263 {
3264 do_cleanups (old_chain);
3265 return val;
3266 }
3267 }
3268 }
3269 do_cleanups (old_chain);
3270 return 0;
3271 }
3272
3273 static int internal_breakpoint_number = -1;
3274
3275 /* Set the breakpoint number of B, depending on the value of INTERNAL.
3276 If INTERNAL is non-zero, the breakpoint number will be populated
3277 from internal_breakpoint_number and that variable decremented.
3278 Otherwise the breakpoint number will be populated from
3279 breakpoint_count and that value incremented. Internal breakpoints
3280 do not set the internal var bpnum. */
3281 static void
3282 set_breakpoint_number (int internal, struct breakpoint *b)
3283 {
3284 if (internal)
3285 b->number = internal_breakpoint_number--;
3286 else
3287 {
3288 set_breakpoint_count (breakpoint_count + 1);
3289 b->number = breakpoint_count;
3290 }
3291 }
3292
3293 static struct breakpoint *
3294 create_internal_breakpoint (struct gdbarch *gdbarch,
3295 CORE_ADDR address, enum bptype type,
3296 const struct breakpoint_ops *ops)
3297 {
3298 struct symtab_and_line sal;
3299 struct breakpoint *b;
3300
3301 init_sal (&sal); /* Initialize to zeroes. */
3302
3303 sal.pc = address;
3304 sal.section = find_pc_overlay (sal.pc);
3305 sal.pspace = current_program_space;
3306
3307 b = set_raw_breakpoint (gdbarch, sal, type, ops);
3308 b->number = internal_breakpoint_number--;
3309 b->disposition = disp_donttouch;
3310
3311 return b;
3312 }
3313
3314 static const char *const longjmp_names[] =
3315 {
3316 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3317 };
3318 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3319
3320 /* Per-objfile data private to breakpoint.c. */
3321 struct breakpoint_objfile_data
3322 {
3323 /* Minimal symbol for "_ovly_debug_event" (if any). */
3324 struct bound_minimal_symbol overlay_msym;
3325
3326 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3327 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
3328
3329 /* True if we have looked for longjmp probes. */
3330 int longjmp_searched;
3331
3332 /* SystemTap probe points for longjmp (if any). */
3333 VEC (probe_p) *longjmp_probes;
3334
3335 /* Minimal symbol for "std::terminate()" (if any). */
3336 struct bound_minimal_symbol terminate_msym;
3337
3338 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3339 struct bound_minimal_symbol exception_msym;
3340
3341 /* True if we have looked for exception probes. */
3342 int exception_searched;
3343
3344 /* SystemTap probe points for unwinding (if any). */
3345 VEC (probe_p) *exception_probes;
3346 };
3347
3348 static const struct objfile_data *breakpoint_objfile_key;
3349
3350 /* Minimal symbol not found sentinel. */
3351 static struct minimal_symbol msym_not_found;
3352
3353 /* Returns TRUE if MSYM point to the "not found" sentinel. */
3354
3355 static int
3356 msym_not_found_p (const struct minimal_symbol *msym)
3357 {
3358 return msym == &msym_not_found;
3359 }
3360
3361 /* Return per-objfile data needed by breakpoint.c.
3362 Allocate the data if necessary. */
3363
3364 static struct breakpoint_objfile_data *
3365 get_breakpoint_objfile_data (struct objfile *objfile)
3366 {
3367 struct breakpoint_objfile_data *bp_objfile_data;
3368
3369 bp_objfile_data = objfile_data (objfile, breakpoint_objfile_key);
3370 if (bp_objfile_data == NULL)
3371 {
3372 bp_objfile_data = obstack_alloc (&objfile->objfile_obstack,
3373 sizeof (*bp_objfile_data));
3374
3375 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
3376 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3377 }
3378 return bp_objfile_data;
3379 }
3380
3381 static void
3382 free_breakpoint_probes (struct objfile *obj, void *data)
3383 {
3384 struct breakpoint_objfile_data *bp_objfile_data = data;
3385
3386 VEC_free (probe_p, bp_objfile_data->longjmp_probes);
3387 VEC_free (probe_p, bp_objfile_data->exception_probes);
3388 }
3389
3390 static void
3391 create_overlay_event_breakpoint (void)
3392 {
3393 struct objfile *objfile;
3394 const char *const func_name = "_ovly_debug_event";
3395
3396 ALL_OBJFILES (objfile)
3397 {
3398 struct breakpoint *b;
3399 struct breakpoint_objfile_data *bp_objfile_data;
3400 CORE_ADDR addr;
3401
3402 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3403
3404 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
3405 continue;
3406
3407 if (bp_objfile_data->overlay_msym.minsym == NULL)
3408 {
3409 struct bound_minimal_symbol m;
3410
3411 m = lookup_minimal_symbol_text (func_name, objfile);
3412 if (m.minsym == NULL)
3413 {
3414 /* Avoid future lookups in this objfile. */
3415 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
3416 continue;
3417 }
3418 bp_objfile_data->overlay_msym = m;
3419 }
3420
3421 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
3422 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3423 bp_overlay_event,
3424 &internal_breakpoint_ops);
3425 b->addr_string = xstrdup (func_name);
3426
3427 if (overlay_debugging == ovly_auto)
3428 {
3429 b->enable_state = bp_enabled;
3430 overlay_events_enabled = 1;
3431 }
3432 else
3433 {
3434 b->enable_state = bp_disabled;
3435 overlay_events_enabled = 0;
3436 }
3437 }
3438 update_global_location_list (UGLL_MAY_INSERT);
3439 }
3440
3441 static void
3442 create_longjmp_master_breakpoint (void)
3443 {
3444 struct program_space *pspace;
3445 struct cleanup *old_chain;
3446
3447 old_chain = save_current_program_space ();
3448
3449 ALL_PSPACES (pspace)
3450 {
3451 struct objfile *objfile;
3452
3453 set_current_program_space (pspace);
3454
3455 ALL_OBJFILES (objfile)
3456 {
3457 int i;
3458 struct gdbarch *gdbarch;
3459 struct breakpoint_objfile_data *bp_objfile_data;
3460
3461 gdbarch = get_objfile_arch (objfile);
3462
3463 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3464
3465 if (!bp_objfile_data->longjmp_searched)
3466 {
3467 VEC (probe_p) *ret;
3468
3469 ret = find_probes_in_objfile (objfile, "libc", "longjmp");
3470 if (ret != NULL)
3471 {
3472 /* We are only interested in checking one element. */
3473 struct probe *p = VEC_index (probe_p, ret, 0);
3474
3475 if (!can_evaluate_probe_arguments (p))
3476 {
3477 /* We cannot use the probe interface here, because it does
3478 not know how to evaluate arguments. */
3479 VEC_free (probe_p, ret);
3480 ret = NULL;
3481 }
3482 }
3483 bp_objfile_data->longjmp_probes = ret;
3484 bp_objfile_data->longjmp_searched = 1;
3485 }
3486
3487 if (bp_objfile_data->longjmp_probes != NULL)
3488 {
3489 int i;
3490 struct probe *probe;
3491 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3492
3493 for (i = 0;
3494 VEC_iterate (probe_p,
3495 bp_objfile_data->longjmp_probes,
3496 i, probe);
3497 ++i)
3498 {
3499 struct breakpoint *b;
3500
3501 b = create_internal_breakpoint (gdbarch,
3502 get_probe_address (probe,
3503 objfile),
3504 bp_longjmp_master,
3505 &internal_breakpoint_ops);
3506 b->addr_string = xstrdup ("-probe-stap libc:longjmp");
3507 b->enable_state = bp_disabled;
3508 }
3509
3510 continue;
3511 }
3512
3513 if (!gdbarch_get_longjmp_target_p (gdbarch))
3514 continue;
3515
3516 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
3517 {
3518 struct breakpoint *b;
3519 const char *func_name;
3520 CORE_ADDR addr;
3521
3522 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3523 continue;
3524
3525 func_name = longjmp_names[i];
3526 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3527 {
3528 struct bound_minimal_symbol m;
3529
3530 m = lookup_minimal_symbol_text (func_name, objfile);
3531 if (m.minsym == NULL)
3532 {
3533 /* Prevent future lookups in this objfile. */
3534 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
3535 continue;
3536 }
3537 bp_objfile_data->longjmp_msym[i] = m;
3538 }
3539
3540 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
3541 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3542 &internal_breakpoint_ops);
3543 b->addr_string = xstrdup (func_name);
3544 b->enable_state = bp_disabled;
3545 }
3546 }
3547 }
3548 update_global_location_list (UGLL_MAY_INSERT);
3549
3550 do_cleanups (old_chain);
3551 }
3552
3553 /* Create a master std::terminate breakpoint. */
3554 static void
3555 create_std_terminate_master_breakpoint (void)
3556 {
3557 struct program_space *pspace;
3558 struct cleanup *old_chain;
3559 const char *const func_name = "std::terminate()";
3560
3561 old_chain = save_current_program_space ();
3562
3563 ALL_PSPACES (pspace)
3564 {
3565 struct objfile *objfile;
3566 CORE_ADDR addr;
3567
3568 set_current_program_space (pspace);
3569
3570 ALL_OBJFILES (objfile)
3571 {
3572 struct breakpoint *b;
3573 struct breakpoint_objfile_data *bp_objfile_data;
3574
3575 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3576
3577 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3578 continue;
3579
3580 if (bp_objfile_data->terminate_msym.minsym == NULL)
3581 {
3582 struct bound_minimal_symbol m;
3583
3584 m = lookup_minimal_symbol (func_name, NULL, objfile);
3585 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3586 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
3587 {
3588 /* Prevent future lookups in this objfile. */
3589 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3590 continue;
3591 }
3592 bp_objfile_data->terminate_msym = m;
3593 }
3594
3595 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3596 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3597 bp_std_terminate_master,
3598 &internal_breakpoint_ops);
3599 b->addr_string = xstrdup (func_name);
3600 b->enable_state = bp_disabled;
3601 }
3602 }
3603
3604 update_global_location_list (UGLL_MAY_INSERT);
3605
3606 do_cleanups (old_chain);
3607 }
3608
3609 /* Install a master breakpoint on the unwinder's debug hook. */
3610
3611 static void
3612 create_exception_master_breakpoint (void)
3613 {
3614 struct objfile *objfile;
3615 const char *const func_name = "_Unwind_DebugHook";
3616
3617 ALL_OBJFILES (objfile)
3618 {
3619 struct breakpoint *b;
3620 struct gdbarch *gdbarch;
3621 struct breakpoint_objfile_data *bp_objfile_data;
3622 CORE_ADDR addr;
3623
3624 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3625
3626 /* We prefer the SystemTap probe point if it exists. */
3627 if (!bp_objfile_data->exception_searched)
3628 {
3629 VEC (probe_p) *ret;
3630
3631 ret = find_probes_in_objfile (objfile, "libgcc", "unwind");
3632
3633 if (ret != NULL)
3634 {
3635 /* We are only interested in checking one element. */
3636 struct probe *p = VEC_index (probe_p, ret, 0);
3637
3638 if (!can_evaluate_probe_arguments (p))
3639 {
3640 /* We cannot use the probe interface here, because it does
3641 not know how to evaluate arguments. */
3642 VEC_free (probe_p, ret);
3643 ret = NULL;
3644 }
3645 }
3646 bp_objfile_data->exception_probes = ret;
3647 bp_objfile_data->exception_searched = 1;
3648 }
3649
3650 if (bp_objfile_data->exception_probes != NULL)
3651 {
3652 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3653 int i;
3654 struct probe *probe;
3655
3656 for (i = 0;
3657 VEC_iterate (probe_p,
3658 bp_objfile_data->exception_probes,
3659 i, probe);
3660 ++i)
3661 {
3662 struct breakpoint *b;
3663
3664 b = create_internal_breakpoint (gdbarch,
3665 get_probe_address (probe,
3666 objfile),
3667 bp_exception_master,
3668 &internal_breakpoint_ops);
3669 b->addr_string = xstrdup ("-probe-stap libgcc:unwind");
3670 b->enable_state = bp_disabled;
3671 }
3672
3673 continue;
3674 }
3675
3676 /* Otherwise, try the hook function. */
3677
3678 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3679 continue;
3680
3681 gdbarch = get_objfile_arch (objfile);
3682
3683 if (bp_objfile_data->exception_msym.minsym == NULL)
3684 {
3685 struct bound_minimal_symbol debug_hook;
3686
3687 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3688 if (debug_hook.minsym == NULL)
3689 {
3690 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3691 continue;
3692 }
3693
3694 bp_objfile_data->exception_msym = debug_hook;
3695 }
3696
3697 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
3698 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3699 &current_target);
3700 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3701 &internal_breakpoint_ops);
3702 b->addr_string = xstrdup (func_name);
3703 b->enable_state = bp_disabled;
3704 }
3705
3706 update_global_location_list (UGLL_MAY_INSERT);
3707 }
3708
3709 void
3710 update_breakpoints_after_exec (void)
3711 {
3712 struct breakpoint *b, *b_tmp;
3713 struct bp_location *bploc, **bplocp_tmp;
3714
3715 /* We're about to delete breakpoints from GDB's lists. If the
3716 INSERTED flag is true, GDB will try to lift the breakpoints by
3717 writing the breakpoints' "shadow contents" back into memory. The
3718 "shadow contents" are NOT valid after an exec, so GDB should not
3719 do that. Instead, the target is responsible from marking
3720 breakpoints out as soon as it detects an exec. We don't do that
3721 here instead, because there may be other attempts to delete
3722 breakpoints after detecting an exec and before reaching here. */
3723 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
3724 if (bploc->pspace == current_program_space)
3725 gdb_assert (!bploc->inserted);
3726
3727 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3728 {
3729 if (b->pspace != current_program_space)
3730 continue;
3731
3732 /* Solib breakpoints must be explicitly reset after an exec(). */
3733 if (b->type == bp_shlib_event)
3734 {
3735 delete_breakpoint (b);
3736 continue;
3737 }
3738
3739 /* JIT breakpoints must be explicitly reset after an exec(). */
3740 if (b->type == bp_jit_event)
3741 {
3742 delete_breakpoint (b);
3743 continue;
3744 }
3745
3746 /* Thread event breakpoints must be set anew after an exec(),
3747 as must overlay event and longjmp master breakpoints. */
3748 if (b->type == bp_thread_event || b->type == bp_overlay_event
3749 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3750 || b->type == bp_exception_master)
3751 {
3752 delete_breakpoint (b);
3753 continue;
3754 }
3755
3756 /* Step-resume breakpoints are meaningless after an exec(). */
3757 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3758 {
3759 delete_breakpoint (b);
3760 continue;
3761 }
3762
3763 /* Just like single-step breakpoints. */
3764 if (b->type == bp_single_step)
3765 {
3766 delete_breakpoint (b);
3767 continue;
3768 }
3769
3770 /* Longjmp and longjmp-resume breakpoints are also meaningless
3771 after an exec. */
3772 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3773 || b->type == bp_longjmp_call_dummy
3774 || b->type == bp_exception || b->type == bp_exception_resume)
3775 {
3776 delete_breakpoint (b);
3777 continue;
3778 }
3779
3780 if (b->type == bp_catchpoint)
3781 {
3782 /* For now, none of the bp_catchpoint breakpoints need to
3783 do anything at this point. In the future, if some of
3784 the catchpoints need to something, we will need to add
3785 a new method, and call this method from here. */
3786 continue;
3787 }
3788
3789 /* bp_finish is a special case. The only way we ought to be able
3790 to see one of these when an exec() has happened, is if the user
3791 caught a vfork, and then said "finish". Ordinarily a finish just
3792 carries them to the call-site of the current callee, by setting
3793 a temporary bp there and resuming. But in this case, the finish
3794 will carry them entirely through the vfork & exec.
3795
3796 We don't want to allow a bp_finish to remain inserted now. But
3797 we can't safely delete it, 'cause finish_command has a handle to
3798 the bp on a bpstat, and will later want to delete it. There's a
3799 chance (and I've seen it happen) that if we delete the bp_finish
3800 here, that its storage will get reused by the time finish_command
3801 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3802 We really must allow finish_command to delete a bp_finish.
3803
3804 In the absence of a general solution for the "how do we know
3805 it's safe to delete something others may have handles to?"
3806 problem, what we'll do here is just uninsert the bp_finish, and
3807 let finish_command delete it.
3808
3809 (We know the bp_finish is "doomed" in the sense that it's
3810 momentary, and will be deleted as soon as finish_command sees
3811 the inferior stopped. So it doesn't matter that the bp's
3812 address is probably bogus in the new a.out, unlike e.g., the
3813 solib breakpoints.) */
3814
3815 if (b->type == bp_finish)
3816 {
3817 continue;
3818 }
3819
3820 /* Without a symbolic address, we have little hope of the
3821 pre-exec() address meaning the same thing in the post-exec()
3822 a.out. */
3823 if (b->addr_string == NULL)
3824 {
3825 delete_breakpoint (b);
3826 continue;
3827 }
3828 }
3829 }
3830
3831 int
3832 detach_breakpoints (ptid_t ptid)
3833 {
3834 struct bp_location *bl, **blp_tmp;
3835 int val = 0;
3836 struct cleanup *old_chain = save_inferior_ptid ();
3837 struct inferior *inf = current_inferior ();
3838
3839 if (ptid_get_pid (ptid) == ptid_get_pid (inferior_ptid))
3840 error (_("Cannot detach breakpoints of inferior_ptid"));
3841
3842 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
3843 inferior_ptid = ptid;
3844 ALL_BP_LOCATIONS (bl, blp_tmp)
3845 {
3846 if (bl->pspace != inf->pspace)
3847 continue;
3848
3849 /* This function must physically remove breakpoints locations
3850 from the specified ptid, without modifying the breakpoint
3851 package's state. Locations of type bp_loc_other are only
3852 maintained at GDB side. So, there is no need to remove
3853 these bp_loc_other locations. Moreover, removing these
3854 would modify the breakpoint package's state. */
3855 if (bl->loc_type == bp_loc_other)
3856 continue;
3857
3858 if (bl->inserted)
3859 val |= remove_breakpoint_1 (bl, mark_inserted);
3860 }
3861
3862 do_cleanups (old_chain);
3863 return val;
3864 }
3865
3866 /* Remove the breakpoint location BL from the current address space.
3867 Note that this is used to detach breakpoints from a child fork.
3868 When we get here, the child isn't in the inferior list, and neither
3869 do we have objects to represent its address space --- we should
3870 *not* look at bl->pspace->aspace here. */
3871
3872 static int
3873 remove_breakpoint_1 (struct bp_location *bl, insertion_state_t is)
3874 {
3875 int val;
3876
3877 /* BL is never in moribund_locations by our callers. */
3878 gdb_assert (bl->owner != NULL);
3879
3880 if (bl->permanent)
3881 /* Permanent breakpoints cannot be inserted or removed. */
3882 return 0;
3883
3884 /* The type of none suggests that owner is actually deleted.
3885 This should not ever happen. */
3886 gdb_assert (bl->owner->type != bp_none);
3887
3888 if (bl->loc_type == bp_loc_software_breakpoint
3889 || bl->loc_type == bp_loc_hardware_breakpoint)
3890 {
3891 /* "Normal" instruction breakpoint: either the standard
3892 trap-instruction bp (bp_breakpoint), or a
3893 bp_hardware_breakpoint. */
3894
3895 /* First check to see if we have to handle an overlay. */
3896 if (overlay_debugging == ovly_off
3897 || bl->section == NULL
3898 || !(section_is_overlay (bl->section)))
3899 {
3900 /* No overlay handling: just remove the breakpoint. */
3901
3902 /* If we're trying to uninsert a memory breakpoint that we
3903 know is set in a dynamic object that is marked
3904 shlib_disabled, then either the dynamic object was
3905 removed with "remove-symbol-file" or with
3906 "nosharedlibrary". In the former case, we don't know
3907 whether another dynamic object might have loaded over the
3908 breakpoint's address -- the user might well let us know
3909 about it next with add-symbol-file (the whole point of
3910 add-symbol-file is letting the user manually maintain a
3911 list of dynamically loaded objects). If we have the
3912 breakpoint's shadow memory, that is, this is a software
3913 breakpoint managed by GDB, check whether the breakpoint
3914 is still inserted in memory, to avoid overwriting wrong
3915 code with stale saved shadow contents. Note that HW
3916 breakpoints don't have shadow memory, as they're
3917 implemented using a mechanism that is not dependent on
3918 being able to modify the target's memory, and as such
3919 they should always be removed. */
3920 if (bl->shlib_disabled
3921 && bl->target_info.shadow_len != 0
3922 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3923 val = 0;
3924 else
3925 val = bl->owner->ops->remove_location (bl);
3926 }
3927 else
3928 {
3929 /* This breakpoint is in an overlay section.
3930 Did we set a breakpoint at the LMA? */
3931 if (!overlay_events_enabled)
3932 {
3933 /* Yes -- overlay event support is not active, so we
3934 should have set a breakpoint at the LMA. Remove it.
3935 */
3936 /* Ignore any failures: if the LMA is in ROM, we will
3937 have already warned when we failed to insert it. */
3938 if (bl->loc_type == bp_loc_hardware_breakpoint)
3939 target_remove_hw_breakpoint (bl->gdbarch,
3940 &bl->overlay_target_info);
3941 else
3942 target_remove_breakpoint (bl->gdbarch,
3943 &bl->overlay_target_info);
3944 }
3945 /* Did we set a breakpoint at the VMA?
3946 If so, we will have marked the breakpoint 'inserted'. */
3947 if (bl->inserted)
3948 {
3949 /* Yes -- remove it. Previously we did not bother to
3950 remove the breakpoint if the section had been
3951 unmapped, but let's not rely on that being safe. We
3952 don't know what the overlay manager might do. */
3953
3954 /* However, we should remove *software* breakpoints only
3955 if the section is still mapped, or else we overwrite
3956 wrong code with the saved shadow contents. */
3957 if (bl->loc_type == bp_loc_hardware_breakpoint
3958 || section_is_mapped (bl->section))
3959 val = bl->owner->ops->remove_location (bl);
3960 else
3961 val = 0;
3962 }
3963 else
3964 {
3965 /* No -- not inserted, so no need to remove. No error. */
3966 val = 0;
3967 }
3968 }
3969
3970 /* In some cases, we might not be able to remove a breakpoint in
3971 a shared library that has already been removed, but we have
3972 not yet processed the shlib unload event. Similarly for an
3973 unloaded add-symbol-file object - the user might not yet have
3974 had the chance to remove-symbol-file it. shlib_disabled will
3975 be set if the library/object has already been removed, but
3976 the breakpoint hasn't been uninserted yet, e.g., after
3977 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3978 always-inserted mode. */
3979 if (val
3980 && (bl->loc_type == bp_loc_software_breakpoint
3981 && (bl->shlib_disabled
3982 || solib_name_from_address (bl->pspace, bl->address)
3983 || shared_objfile_contains_address_p (bl->pspace,
3984 bl->address))))
3985 val = 0;
3986
3987 if (val)
3988 return val;
3989 bl->inserted = (is == mark_inserted);
3990 }
3991 else if (bl->loc_type == bp_loc_hardware_watchpoint)
3992 {
3993 gdb_assert (bl->owner->ops != NULL
3994 && bl->owner->ops->remove_location != NULL);
3995
3996 bl->inserted = (is == mark_inserted);
3997 bl->owner->ops->remove_location (bl);
3998
3999 /* Failure to remove any of the hardware watchpoints comes here. */
4000 if ((is == mark_uninserted) && (bl->inserted))
4001 warning (_("Could not remove hardware watchpoint %d."),
4002 bl->owner->number);
4003 }
4004 else if (bl->owner->type == bp_catchpoint
4005 && breakpoint_enabled (bl->owner)
4006 && !bl->duplicate)
4007 {
4008 gdb_assert (bl->owner->ops != NULL
4009 && bl->owner->ops->remove_location != NULL);
4010
4011 val = bl->owner->ops->remove_location (bl);
4012 if (val)
4013 return val;
4014
4015 bl->inserted = (is == mark_inserted);
4016 }
4017
4018 return 0;
4019 }
4020
4021 static int
4022 remove_breakpoint (struct bp_location *bl, insertion_state_t is)
4023 {
4024 int ret;
4025 struct cleanup *old_chain;
4026
4027 /* BL is never in moribund_locations by our callers. */
4028 gdb_assert (bl->owner != NULL);
4029
4030 if (bl->permanent)
4031 /* Permanent breakpoints cannot be inserted or removed. */
4032 return 0;
4033
4034 /* The type of none suggests that owner is actually deleted.
4035 This should not ever happen. */
4036 gdb_assert (bl->owner->type != bp_none);
4037
4038 old_chain = save_current_space_and_thread ();
4039
4040 switch_to_program_space_and_thread (bl->pspace);
4041
4042 ret = remove_breakpoint_1 (bl, is);
4043
4044 do_cleanups (old_chain);
4045 return ret;
4046 }
4047
4048 /* Clear the "inserted" flag in all breakpoints. */
4049
4050 void
4051 mark_breakpoints_out (void)
4052 {
4053 struct bp_location *bl, **blp_tmp;
4054
4055 ALL_BP_LOCATIONS (bl, blp_tmp)
4056 if (bl->pspace == current_program_space
4057 && !bl->permanent)
4058 bl->inserted = 0;
4059 }
4060
4061 /* Clear the "inserted" flag in all breakpoints and delete any
4062 breakpoints which should go away between runs of the program.
4063
4064 Plus other such housekeeping that has to be done for breakpoints
4065 between runs.
4066
4067 Note: this function gets called at the end of a run (by
4068 generic_mourn_inferior) and when a run begins (by
4069 init_wait_for_inferior). */
4070
4071
4072
4073 void
4074 breakpoint_init_inferior (enum inf_context context)
4075 {
4076 struct breakpoint *b, *b_tmp;
4077 struct bp_location *bl, **blp_tmp;
4078 int ix;
4079 struct program_space *pspace = current_program_space;
4080
4081 /* If breakpoint locations are shared across processes, then there's
4082 nothing to do. */
4083 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
4084 return;
4085
4086 mark_breakpoints_out ();
4087
4088 ALL_BREAKPOINTS_SAFE (b, b_tmp)
4089 {
4090 if (b->loc && b->loc->pspace != pspace)
4091 continue;
4092
4093 switch (b->type)
4094 {
4095 case bp_call_dummy:
4096 case bp_longjmp_call_dummy:
4097
4098 /* If the call dummy breakpoint is at the entry point it will
4099 cause problems when the inferior is rerun, so we better get
4100 rid of it. */
4101
4102 case bp_watchpoint_scope:
4103
4104 /* Also get rid of scope breakpoints. */
4105
4106 case bp_shlib_event:
4107
4108 /* Also remove solib event breakpoints. Their addresses may
4109 have changed since the last time we ran the program.
4110 Actually we may now be debugging against different target;
4111 and so the solib backend that installed this breakpoint may
4112 not be used in by the target. E.g.,
4113
4114 (gdb) file prog-linux
4115 (gdb) run # native linux target
4116 ...
4117 (gdb) kill
4118 (gdb) file prog-win.exe
4119 (gdb) tar rem :9999 # remote Windows gdbserver.
4120 */
4121
4122 case bp_step_resume:
4123
4124 /* Also remove step-resume breakpoints. */
4125
4126 case bp_single_step:
4127
4128 /* Also remove single-step breakpoints. */
4129
4130 delete_breakpoint (b);
4131 break;
4132
4133 case bp_watchpoint:
4134 case bp_hardware_watchpoint:
4135 case bp_read_watchpoint:
4136 case bp_access_watchpoint:
4137 {
4138 struct watchpoint *w = (struct watchpoint *) b;
4139
4140 /* Likewise for watchpoints on local expressions. */
4141 if (w->exp_valid_block != NULL)
4142 delete_breakpoint (b);
4143 else if (context == inf_starting)
4144 {
4145 /* Reset val field to force reread of starting value in
4146 insert_breakpoints. */
4147 if (w->val)
4148 value_free (w->val);
4149 w->val = NULL;
4150 w->val_valid = 0;
4151 }
4152 }
4153 break;
4154 default:
4155 break;
4156 }
4157 }
4158
4159 /* Get rid of the moribund locations. */
4160 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
4161 decref_bp_location (&bl);
4162 VEC_free (bp_location_p, moribund_locations);
4163 }
4164
4165 /* These functions concern about actual breakpoints inserted in the
4166 target --- to e.g. check if we need to do decr_pc adjustment or if
4167 we need to hop over the bkpt --- so we check for address space
4168 match, not program space. */
4169
4170 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4171 exists at PC. It returns ordinary_breakpoint_here if it's an
4172 ordinary breakpoint, or permanent_breakpoint_here if it's a
4173 permanent breakpoint.
4174 - When continuing from a location with an ordinary breakpoint, we
4175 actually single step once before calling insert_breakpoints.
4176 - When continuing from a location with a permanent breakpoint, we
4177 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4178 the target, to advance the PC past the breakpoint. */
4179
4180 enum breakpoint_here
4181 breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
4182 {
4183 struct bp_location *bl, **blp_tmp;
4184 int any_breakpoint_here = 0;
4185
4186 ALL_BP_LOCATIONS (bl, blp_tmp)
4187 {
4188 if (bl->loc_type != bp_loc_software_breakpoint
4189 && bl->loc_type != bp_loc_hardware_breakpoint)
4190 continue;
4191
4192 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
4193 if ((breakpoint_enabled (bl->owner)
4194 || bl->permanent)
4195 && breakpoint_location_address_match (bl, aspace, pc))
4196 {
4197 if (overlay_debugging
4198 && section_is_overlay (bl->section)
4199 && !section_is_mapped (bl->section))
4200 continue; /* unmapped overlay -- can't be a match */
4201 else if (bl->permanent)
4202 return permanent_breakpoint_here;
4203 else
4204 any_breakpoint_here = 1;
4205 }
4206 }
4207
4208 return any_breakpoint_here ? ordinary_breakpoint_here : 0;
4209 }
4210
4211 /* Return true if there's a moribund breakpoint at PC. */
4212
4213 int
4214 moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
4215 {
4216 struct bp_location *loc;
4217 int ix;
4218
4219 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
4220 if (breakpoint_location_address_match (loc, aspace, pc))
4221 return 1;
4222
4223 return 0;
4224 }
4225
4226 /* Returns non-zero iff BL is inserted at PC, in address space
4227 ASPACE. */
4228
4229 static int
4230 bp_location_inserted_here_p (struct bp_location *bl,
4231 struct address_space *aspace, CORE_ADDR pc)
4232 {
4233 if (bl->inserted
4234 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4235 aspace, pc))
4236 {
4237 if (overlay_debugging
4238 && section_is_overlay (bl->section)
4239 && !section_is_mapped (bl->section))
4240 return 0; /* unmapped overlay -- can't be a match */
4241 else
4242 return 1;
4243 }
4244 return 0;
4245 }
4246
4247 /* Returns non-zero iff there's a breakpoint inserted at PC. */
4248
4249 int
4250 breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
4251 {
4252 struct bp_location **blp, **blp_tmp = NULL;
4253 struct bp_location *bl;
4254
4255 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4256 {
4257 struct bp_location *bl = *blp;
4258
4259 if (bl->loc_type != bp_loc_software_breakpoint
4260 && bl->loc_type != bp_loc_hardware_breakpoint)
4261 continue;
4262
4263 if (bp_location_inserted_here_p (bl, aspace, pc))
4264 return 1;
4265 }
4266 return 0;
4267 }
4268
4269 /* This function returns non-zero iff there is a software breakpoint
4270 inserted at PC. */
4271
4272 int
4273 software_breakpoint_inserted_here_p (struct address_space *aspace,
4274 CORE_ADDR pc)
4275 {
4276 struct bp_location **blp, **blp_tmp = NULL;
4277 struct bp_location *bl;
4278
4279 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4280 {
4281 struct bp_location *bl = *blp;
4282
4283 if (bl->loc_type != bp_loc_software_breakpoint)
4284 continue;
4285
4286 if (bp_location_inserted_here_p (bl, aspace, pc))
4287 return 1;
4288 }
4289
4290 return 0;
4291 }
4292
4293 /* See breakpoint.h. */
4294
4295 int
4296 hardware_breakpoint_inserted_here_p (struct address_space *aspace,
4297 CORE_ADDR pc)
4298 {
4299 struct bp_location **blp, **blp_tmp = NULL;
4300 struct bp_location *bl;
4301
4302 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4303 {
4304 struct bp_location *bl = *blp;
4305
4306 if (bl->loc_type != bp_loc_hardware_breakpoint)
4307 continue;
4308
4309 if (bp_location_inserted_here_p (bl, aspace, pc))
4310 return 1;
4311 }
4312
4313 return 0;
4314 }
4315
4316 int
4317 hardware_watchpoint_inserted_in_range (struct address_space *aspace,
4318 CORE_ADDR addr, ULONGEST len)
4319 {
4320 struct breakpoint *bpt;
4321
4322 ALL_BREAKPOINTS (bpt)
4323 {
4324 struct bp_location *loc;
4325
4326 if (bpt->type != bp_hardware_watchpoint
4327 && bpt->type != bp_access_watchpoint)
4328 continue;
4329
4330 if (!breakpoint_enabled (bpt))
4331 continue;
4332
4333 for (loc = bpt->loc; loc; loc = loc->next)
4334 if (loc->pspace->aspace == aspace && loc->inserted)
4335 {
4336 CORE_ADDR l, h;
4337
4338 /* Check for intersection. */
4339 l = max (loc->address, addr);
4340 h = min (loc->address + loc->length, addr + len);
4341 if (l < h)
4342 return 1;
4343 }
4344 }
4345 return 0;
4346 }
4347 \f
4348
4349 /* bpstat stuff. External routines' interfaces are documented
4350 in breakpoint.h. */
4351
4352 int
4353 is_catchpoint (struct breakpoint *ep)
4354 {
4355 return (ep->type == bp_catchpoint);
4356 }
4357
4358 /* Frees any storage that is part of a bpstat. Does not walk the
4359 'next' chain. */
4360
4361 static void
4362 bpstat_free (bpstat bs)
4363 {
4364 if (bs->old_val != NULL)
4365 value_free (bs->old_val);
4366 decref_counted_command_line (&bs->commands);
4367 decref_bp_location (&bs->bp_location_at);
4368 xfree (bs);
4369 }
4370
4371 /* Clear a bpstat so that it says we are not at any breakpoint.
4372 Also free any storage that is part of a bpstat. */
4373
4374 void
4375 bpstat_clear (bpstat *bsp)
4376 {
4377 bpstat p;
4378 bpstat q;
4379
4380 if (bsp == 0)
4381 return;
4382 p = *bsp;
4383 while (p != NULL)
4384 {
4385 q = p->next;
4386 bpstat_free (p);
4387 p = q;
4388 }
4389 *bsp = NULL;
4390 }
4391
4392 /* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4393 is part of the bpstat is copied as well. */
4394
4395 bpstat
4396 bpstat_copy (bpstat bs)
4397 {
4398 bpstat p = NULL;
4399 bpstat tmp;
4400 bpstat retval = NULL;
4401
4402 if (bs == NULL)
4403 return bs;
4404
4405 for (; bs != NULL; bs = bs->next)
4406 {
4407 tmp = (bpstat) xmalloc (sizeof (*tmp));
4408 memcpy (tmp, bs, sizeof (*tmp));
4409 incref_counted_command_line (tmp->commands);
4410 incref_bp_location (tmp->bp_location_at);
4411 if (bs->old_val != NULL)
4412 {
4413 tmp->old_val = value_copy (bs->old_val);
4414 release_value (tmp->old_val);
4415 }
4416
4417 if (p == NULL)
4418 /* This is the first thing in the chain. */
4419 retval = tmp;
4420 else
4421 p->next = tmp;
4422 p = tmp;
4423 }
4424 p->next = NULL;
4425 return retval;
4426 }
4427
4428 /* Find the bpstat associated with this breakpoint. */
4429
4430 bpstat
4431 bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
4432 {
4433 if (bsp == NULL)
4434 return NULL;
4435
4436 for (; bsp != NULL; bsp = bsp->next)
4437 {
4438 if (bsp->breakpoint_at == breakpoint)
4439 return bsp;
4440 }
4441 return NULL;
4442 }
4443
4444 /* See breakpoint.h. */
4445
4446 int
4447 bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
4448 {
4449 for (; bsp != NULL; bsp = bsp->next)
4450 {
4451 if (bsp->breakpoint_at == NULL)
4452 {
4453 /* A moribund location can never explain a signal other than
4454 GDB_SIGNAL_TRAP. */
4455 if (sig == GDB_SIGNAL_TRAP)
4456 return 1;
4457 }
4458 else
4459 {
4460 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4461 sig))
4462 return 1;
4463 }
4464 }
4465
4466 return 0;
4467 }
4468
4469 /* Put in *NUM the breakpoint number of the first breakpoint we are
4470 stopped at. *BSP upon return is a bpstat which points to the
4471 remaining breakpoints stopped at (but which is not guaranteed to be
4472 good for anything but further calls to bpstat_num).
4473
4474 Return 0 if passed a bpstat which does not indicate any breakpoints.
4475 Return -1 if stopped at a breakpoint that has been deleted since
4476 we set it.
4477 Return 1 otherwise. */
4478
4479 int
4480 bpstat_num (bpstat *bsp, int *num)
4481 {
4482 struct breakpoint *b;
4483
4484 if ((*bsp) == NULL)
4485 return 0; /* No more breakpoint values */
4486
4487 /* We assume we'll never have several bpstats that correspond to a
4488 single breakpoint -- otherwise, this function might return the
4489 same number more than once and this will look ugly. */
4490 b = (*bsp)->breakpoint_at;
4491 *bsp = (*bsp)->next;
4492 if (b == NULL)
4493 return -1; /* breakpoint that's been deleted since */
4494
4495 *num = b->number; /* We have its number */
4496 return 1;
4497 }
4498
4499 /* See breakpoint.h. */
4500
4501 void
4502 bpstat_clear_actions (void)
4503 {
4504 struct thread_info *tp;
4505 bpstat bs;
4506
4507 if (ptid_equal (inferior_ptid, null_ptid))
4508 return;
4509
4510 tp = find_thread_ptid (inferior_ptid);
4511 if (tp == NULL)
4512 return;
4513
4514 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
4515 {
4516 decref_counted_command_line (&bs->commands);
4517
4518 if (bs->old_val != NULL)
4519 {
4520 value_free (bs->old_val);
4521 bs->old_val = NULL;
4522 }
4523 }
4524 }
4525
4526 /* Called when a command is about to proceed the inferior. */
4527
4528 static void
4529 breakpoint_about_to_proceed (void)
4530 {
4531 if (!ptid_equal (inferior_ptid, null_ptid))
4532 {
4533 struct thread_info *tp = inferior_thread ();
4534
4535 /* Allow inferior function calls in breakpoint commands to not
4536 interrupt the command list. When the call finishes
4537 successfully, the inferior will be standing at the same
4538 breakpoint as if nothing happened. */
4539 if (tp->control.in_infcall)
4540 return;
4541 }
4542
4543 breakpoint_proceeded = 1;
4544 }
4545
4546 /* Stub for cleaning up our state if we error-out of a breakpoint
4547 command. */
4548 static void
4549 cleanup_executing_breakpoints (void *ignore)
4550 {
4551 executing_breakpoint_commands = 0;
4552 }
4553
4554 /* Return non-zero iff CMD as the first line of a command sequence is `silent'
4555 or its equivalent. */
4556
4557 static int
4558 command_line_is_silent (struct command_line *cmd)
4559 {
4560 return cmd && (strcmp ("silent", cmd->line) == 0
4561 || (xdb_commands && strcmp ("Q", cmd->line) == 0));
4562 }
4563
4564 /* Execute all the commands associated with all the breakpoints at
4565 this location. Any of these commands could cause the process to
4566 proceed beyond this point, etc. We look out for such changes by
4567 checking the global "breakpoint_proceeded" after each command.
4568
4569 Returns true if a breakpoint command resumed the inferior. In that
4570 case, it is the caller's responsibility to recall it again with the
4571 bpstat of the current thread. */
4572
4573 static int
4574 bpstat_do_actions_1 (bpstat *bsp)
4575 {
4576 bpstat bs;
4577 struct cleanup *old_chain;
4578 int again = 0;
4579
4580 /* Avoid endless recursion if a `source' command is contained
4581 in bs->commands. */
4582 if (executing_breakpoint_commands)
4583 return 0;
4584
4585 executing_breakpoint_commands = 1;
4586 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
4587
4588 prevent_dont_repeat ();
4589
4590 /* This pointer will iterate over the list of bpstat's. */
4591 bs = *bsp;
4592
4593 breakpoint_proceeded = 0;
4594 for (; bs != NULL; bs = bs->next)
4595 {
4596 struct counted_command_line *ccmd;
4597 struct command_line *cmd;
4598 struct cleanup *this_cmd_tree_chain;
4599
4600 /* Take ownership of the BSP's command tree, if it has one.
4601
4602 The command tree could legitimately contain commands like
4603 'step' and 'next', which call clear_proceed_status, which
4604 frees stop_bpstat's command tree. To make sure this doesn't
4605 free the tree we're executing out from under us, we need to
4606 take ownership of the tree ourselves. Since a given bpstat's
4607 commands are only executed once, we don't need to copy it; we
4608 can clear the pointer in the bpstat, and make sure we free
4609 the tree when we're done. */
4610 ccmd = bs->commands;
4611 bs->commands = NULL;
4612 this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
4613 cmd = ccmd ? ccmd->commands : NULL;
4614 if (command_line_is_silent (cmd))
4615 {
4616 /* The action has been already done by bpstat_stop_status. */
4617 cmd = cmd->next;
4618 }
4619
4620 while (cmd != NULL)
4621 {
4622 execute_control_command (cmd);
4623
4624 if (breakpoint_proceeded)
4625 break;
4626 else
4627 cmd = cmd->next;
4628 }
4629
4630 /* We can free this command tree now. */
4631 do_cleanups (this_cmd_tree_chain);
4632
4633 if (breakpoint_proceeded)
4634 {
4635 if (interpreter_async && target_can_async_p ())
4636 /* If we are in async mode, then the target might be still
4637 running, not stopped at any breakpoint, so nothing for
4638 us to do here -- just return to the event loop. */
4639 ;
4640 else
4641 /* In sync mode, when execute_control_command returns
4642 we're already standing on the next breakpoint.
4643 Breakpoint commands for that stop were not run, since
4644 execute_command does not run breakpoint commands --
4645 only command_line_handler does, but that one is not
4646 involved in execution of breakpoint commands. So, we
4647 can now execute breakpoint commands. It should be
4648 noted that making execute_command do bpstat actions is
4649 not an option -- in this case we'll have recursive
4650 invocation of bpstat for each breakpoint with a
4651 command, and can easily blow up GDB stack. Instead, we
4652 return true, which will trigger the caller to recall us
4653 with the new stop_bpstat. */
4654 again = 1;
4655 break;
4656 }
4657 }
4658 do_cleanups (old_chain);
4659 return again;
4660 }
4661
4662 void
4663 bpstat_do_actions (void)
4664 {
4665 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4666
4667 /* Do any commands attached to breakpoint we are stopped at. */
4668 while (!ptid_equal (inferior_ptid, null_ptid)
4669 && target_has_execution
4670 && !is_exited (inferior_ptid)
4671 && !is_executing (inferior_ptid))
4672 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4673 and only return when it is stopped at the next breakpoint, we
4674 keep doing breakpoint actions until it returns false to
4675 indicate the inferior was not resumed. */
4676 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
4677 break;
4678
4679 discard_cleanups (cleanup_if_error);
4680 }
4681
4682 /* Print out the (old or new) value associated with a watchpoint. */
4683
4684 static void
4685 watchpoint_value_print (struct value *val, struct ui_file *stream)
4686 {
4687 if (val == NULL)
4688 fprintf_unfiltered (stream, _("<unreadable>"));
4689 else
4690 {
4691 struct value_print_options opts;
4692 get_user_print_options (&opts);
4693 value_print (val, stream, &opts);
4694 }
4695 }
4696
4697 /* Generic routine for printing messages indicating why we
4698 stopped. The behavior of this function depends on the value
4699 'print_it' in the bpstat structure. Under some circumstances we
4700 may decide not to print anything here and delegate the task to
4701 normal_stop(). */
4702
4703 static enum print_stop_action
4704 print_bp_stop_message (bpstat bs)
4705 {
4706 switch (bs->print_it)
4707 {
4708 case print_it_noop:
4709 /* Nothing should be printed for this bpstat entry. */
4710 return PRINT_UNKNOWN;
4711 break;
4712
4713 case print_it_done:
4714 /* We still want to print the frame, but we already printed the
4715 relevant messages. */
4716 return PRINT_SRC_AND_LOC;
4717 break;
4718
4719 case print_it_normal:
4720 {
4721 struct breakpoint *b = bs->breakpoint_at;
4722
4723 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4724 which has since been deleted. */
4725 if (b == NULL)
4726 return PRINT_UNKNOWN;
4727
4728 /* Normal case. Call the breakpoint's print_it method. */
4729 return b->ops->print_it (bs);
4730 }
4731 break;
4732
4733 default:
4734 internal_error (__FILE__, __LINE__,
4735 _("print_bp_stop_message: unrecognized enum value"));
4736 break;
4737 }
4738 }
4739
4740 /* A helper function that prints a shared library stopped event. */
4741
4742 static void
4743 print_solib_event (int is_catchpoint)
4744 {
4745 int any_deleted
4746 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4747 int any_added
4748 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4749
4750 if (!is_catchpoint)
4751 {
4752 if (any_added || any_deleted)
4753 ui_out_text (current_uiout,
4754 _("Stopped due to shared library event:\n"));
4755 else
4756 ui_out_text (current_uiout,
4757 _("Stopped due to shared library event (no "
4758 "libraries added or removed)\n"));
4759 }
4760
4761 if (ui_out_is_mi_like_p (current_uiout))
4762 ui_out_field_string (current_uiout, "reason",
4763 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4764
4765 if (any_deleted)
4766 {
4767 struct cleanup *cleanup;
4768 char *name;
4769 int ix;
4770
4771 ui_out_text (current_uiout, _(" Inferior unloaded "));
4772 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4773 "removed");
4774 for (ix = 0;
4775 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4776 ix, name);
4777 ++ix)
4778 {
4779 if (ix > 0)
4780 ui_out_text (current_uiout, " ");
4781 ui_out_field_string (current_uiout, "library", name);
4782 ui_out_text (current_uiout, "\n");
4783 }
4784
4785 do_cleanups (cleanup);
4786 }
4787
4788 if (any_added)
4789 {
4790 struct so_list *iter;
4791 int ix;
4792 struct cleanup *cleanup;
4793
4794 ui_out_text (current_uiout, _(" Inferior loaded "));
4795 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4796 "added");
4797 for (ix = 0;
4798 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4799 ix, iter);
4800 ++ix)
4801 {
4802 if (ix > 0)
4803 ui_out_text (current_uiout, " ");
4804 ui_out_field_string (current_uiout, "library", iter->so_name);
4805 ui_out_text (current_uiout, "\n");
4806 }
4807
4808 do_cleanups (cleanup);
4809 }
4810 }
4811
4812 /* Print a message indicating what happened. This is called from
4813 normal_stop(). The input to this routine is the head of the bpstat
4814 list - a list of the eventpoints that caused this stop. KIND is
4815 the target_waitkind for the stopping event. This
4816 routine calls the generic print routine for printing a message
4817 about reasons for stopping. This will print (for example) the
4818 "Breakpoint n," part of the output. The return value of this
4819 routine is one of:
4820
4821 PRINT_UNKNOWN: Means we printed nothing.
4822 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4823 code to print the location. An example is
4824 "Breakpoint 1, " which should be followed by
4825 the location.
4826 PRINT_SRC_ONLY: Means we printed something, but there is no need
4827 to also print the location part of the message.
4828 An example is the catch/throw messages, which
4829 don't require a location appended to the end.
4830 PRINT_NOTHING: We have done some printing and we don't need any
4831 further info to be printed. */
4832
4833 enum print_stop_action
4834 bpstat_print (bpstat bs, int kind)
4835 {
4836 int val;
4837
4838 /* Maybe another breakpoint in the chain caused us to stop.
4839 (Currently all watchpoints go on the bpstat whether hit or not.
4840 That probably could (should) be changed, provided care is taken
4841 with respect to bpstat_explains_signal). */
4842 for (; bs; bs = bs->next)
4843 {
4844 val = print_bp_stop_message (bs);
4845 if (val == PRINT_SRC_ONLY
4846 || val == PRINT_SRC_AND_LOC
4847 || val == PRINT_NOTHING)
4848 return val;
4849 }
4850
4851 /* If we had hit a shared library event breakpoint,
4852 print_bp_stop_message would print out this message. If we hit an
4853 OS-level shared library event, do the same thing. */
4854 if (kind == TARGET_WAITKIND_LOADED)
4855 {
4856 print_solib_event (0);
4857 return PRINT_NOTHING;
4858 }
4859
4860 /* We reached the end of the chain, or we got a null BS to start
4861 with and nothing was printed. */
4862 return PRINT_UNKNOWN;
4863 }
4864
4865 /* Evaluate the expression EXP and return 1 if value is zero.
4866 This returns the inverse of the condition because it is called
4867 from catch_errors which returns 0 if an exception happened, and if an
4868 exception happens we want execution to stop.
4869 The argument is a "struct expression *" that has been cast to a
4870 "void *" to make it pass through catch_errors. */
4871
4872 static int
4873 breakpoint_cond_eval (void *exp)
4874 {
4875 struct value *mark = value_mark ();
4876 int i = !value_true (evaluate_expression ((struct expression *) exp));
4877
4878 value_free_to_mark (mark);
4879 return i;
4880 }
4881
4882 /* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
4883
4884 static bpstat
4885 bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
4886 {
4887 bpstat bs;
4888
4889 bs = (bpstat) xmalloc (sizeof (*bs));
4890 bs->next = NULL;
4891 **bs_link_pointer = bs;
4892 *bs_link_pointer = &bs->next;
4893 bs->breakpoint_at = bl->owner;
4894 bs->bp_location_at = bl;
4895 incref_bp_location (bl);
4896 /* If the condition is false, etc., don't do the commands. */
4897 bs->commands = NULL;
4898 bs->old_val = NULL;
4899 bs->print_it = print_it_normal;
4900 return bs;
4901 }
4902 \f
4903 /* The target has stopped with waitstatus WS. Check if any hardware
4904 watchpoints have triggered, according to the target. */
4905
4906 int
4907 watchpoints_triggered (struct target_waitstatus *ws)
4908 {
4909 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
4910 CORE_ADDR addr;
4911 struct breakpoint *b;
4912
4913 if (!stopped_by_watchpoint)
4914 {
4915 /* We were not stopped by a watchpoint. Mark all watchpoints
4916 as not triggered. */
4917 ALL_BREAKPOINTS (b)
4918 if (is_hardware_watchpoint (b))
4919 {
4920 struct watchpoint *w = (struct watchpoint *) b;
4921
4922 w->watchpoint_triggered = watch_triggered_no;
4923 }
4924
4925 return 0;
4926 }
4927
4928 if (!target_stopped_data_address (&current_target, &addr))
4929 {
4930 /* We were stopped by a watchpoint, but we don't know where.
4931 Mark all watchpoints as unknown. */
4932 ALL_BREAKPOINTS (b)
4933 if (is_hardware_watchpoint (b))
4934 {
4935 struct watchpoint *w = (struct watchpoint *) b;
4936
4937 w->watchpoint_triggered = watch_triggered_unknown;
4938 }
4939
4940 return 1;
4941 }
4942
4943 /* The target could report the data address. Mark watchpoints
4944 affected by this data address as triggered, and all others as not
4945 triggered. */
4946
4947 ALL_BREAKPOINTS (b)
4948 if (is_hardware_watchpoint (b))
4949 {
4950 struct watchpoint *w = (struct watchpoint *) b;
4951 struct bp_location *loc;
4952
4953 w->watchpoint_triggered = watch_triggered_no;
4954 for (loc = b->loc; loc; loc = loc->next)
4955 {
4956 if (is_masked_watchpoint (b))
4957 {
4958 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4959 CORE_ADDR start = loc->address & w->hw_wp_mask;
4960
4961 if (newaddr == start)
4962 {
4963 w->watchpoint_triggered = watch_triggered_yes;
4964 break;
4965 }
4966 }
4967 /* Exact match not required. Within range is sufficient. */
4968 else if (target_watchpoint_addr_within_range (&current_target,
4969 addr, loc->address,
4970 loc->length))
4971 {
4972 w->watchpoint_triggered = watch_triggered_yes;
4973 break;
4974 }
4975 }
4976 }
4977
4978 return 1;
4979 }
4980
4981 /* Possible return values for watchpoint_check (this can't be an enum
4982 because of check_errors). */
4983 /* The watchpoint has been deleted. */
4984 #define WP_DELETED 1
4985 /* The value has changed. */
4986 #define WP_VALUE_CHANGED 2
4987 /* The value has not changed. */
4988 #define WP_VALUE_NOT_CHANGED 3
4989 /* Ignore this watchpoint, no matter if the value changed or not. */
4990 #define WP_IGNORE 4
4991
4992 #define BP_TEMPFLAG 1
4993 #define BP_HARDWAREFLAG 2
4994
4995 /* Evaluate watchpoint condition expression and check if its value
4996 changed.
4997
4998 P should be a pointer to struct bpstat, but is defined as a void *
4999 in order for this function to be usable with catch_errors. */
5000
5001 static int
5002 watchpoint_check (void *p)
5003 {
5004 bpstat bs = (bpstat) p;
5005 struct watchpoint *b;
5006 struct frame_info *fr;
5007 int within_current_scope;
5008
5009 /* BS is built from an existing struct breakpoint. */
5010 gdb_assert (bs->breakpoint_at != NULL);
5011 b = (struct watchpoint *) bs->breakpoint_at;
5012
5013 /* If this is a local watchpoint, we only want to check if the
5014 watchpoint frame is in scope if the current thread is the thread
5015 that was used to create the watchpoint. */
5016 if (!watchpoint_in_thread_scope (b))
5017 return WP_IGNORE;
5018
5019 if (b->exp_valid_block == NULL)
5020 within_current_scope = 1;
5021 else
5022 {
5023 struct frame_info *frame = get_current_frame ();
5024 struct gdbarch *frame_arch = get_frame_arch (frame);
5025 CORE_ADDR frame_pc = get_frame_pc (frame);
5026
5027 /* in_function_epilogue_p() returns a non-zero value if we're
5028 still in the function but the stack frame has already been
5029 invalidated. Since we can't rely on the values of local
5030 variables after the stack has been destroyed, we are treating
5031 the watchpoint in that state as `not changed' without further
5032 checking. Don't mark watchpoints as changed if the current
5033 frame is in an epilogue - even if they are in some other
5034 frame, our view of the stack is likely to be wrong and
5035 frame_find_by_id could error out. */
5036 if (gdbarch_in_function_epilogue_p (frame_arch, frame_pc))
5037 return WP_IGNORE;
5038
5039 fr = frame_find_by_id (b->watchpoint_frame);
5040 within_current_scope = (fr != NULL);
5041
5042 /* If we've gotten confused in the unwinder, we might have
5043 returned a frame that can't describe this variable. */
5044 if (within_current_scope)
5045 {
5046 struct symbol *function;
5047
5048 function = get_frame_function (fr);
5049 if (function == NULL
5050 || !contained_in (b->exp_valid_block,
5051 SYMBOL_BLOCK_VALUE (function)))
5052 within_current_scope = 0;
5053 }
5054
5055 if (within_current_scope)
5056 /* If we end up stopping, the current frame will get selected
5057 in normal_stop. So this call to select_frame won't affect
5058 the user. */
5059 select_frame (fr);
5060 }
5061
5062 if (within_current_scope)
5063 {
5064 /* We use value_{,free_to_}mark because it could be a *long*
5065 time before we return to the command level and call
5066 free_all_values. We can't call free_all_values because we
5067 might be in the middle of evaluating a function call. */
5068
5069 int pc = 0;
5070 struct value *mark;
5071 struct value *new_val;
5072
5073 if (is_masked_watchpoint (&b->base))
5074 /* Since we don't know the exact trigger address (from
5075 stopped_data_address), just tell the user we've triggered
5076 a mask watchpoint. */
5077 return WP_VALUE_CHANGED;
5078
5079 mark = value_mark ();
5080 fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL, 0);
5081
5082 if (b->val_bitsize != 0)
5083 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5084
5085 /* We use value_equal_contents instead of value_equal because
5086 the latter coerces an array to a pointer, thus comparing just
5087 the address of the array instead of its contents. This is
5088 not what we want. */
5089 if ((b->val != NULL) != (new_val != NULL)
5090 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
5091 {
5092 if (new_val != NULL)
5093 {
5094 release_value (new_val);
5095 value_free_to_mark (mark);
5096 }
5097 bs->old_val = b->val;
5098 b->val = new_val;
5099 b->val_valid = 1;
5100 return WP_VALUE_CHANGED;
5101 }
5102 else
5103 {
5104 /* Nothing changed. */
5105 value_free_to_mark (mark);
5106 return WP_VALUE_NOT_CHANGED;
5107 }
5108 }
5109 else
5110 {
5111 struct ui_out *uiout = current_uiout;
5112
5113 /* This seems like the only logical thing to do because
5114 if we temporarily ignored the watchpoint, then when
5115 we reenter the block in which it is valid it contains
5116 garbage (in the case of a function, it may have two
5117 garbage values, one before and one after the prologue).
5118 So we can't even detect the first assignment to it and
5119 watch after that (since the garbage may or may not equal
5120 the first value assigned). */
5121 /* We print all the stop information in
5122 breakpoint_ops->print_it, but in this case, by the time we
5123 call breakpoint_ops->print_it this bp will be deleted
5124 already. So we have no choice but print the information
5125 here. */
5126 if (ui_out_is_mi_like_p (uiout))
5127 ui_out_field_string
5128 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
5129 ui_out_text (uiout, "\nWatchpoint ");
5130 ui_out_field_int (uiout, "wpnum", b->base.number);
5131 ui_out_text (uiout,
5132 " deleted because the program has left the block in\n\
5133 which its expression is valid.\n");
5134
5135 /* Make sure the watchpoint's commands aren't executed. */
5136 decref_counted_command_line (&b->base.commands);
5137 watchpoint_del_at_next_stop (b);
5138
5139 return WP_DELETED;
5140 }
5141 }
5142
5143 /* Return true if it looks like target has stopped due to hitting
5144 breakpoint location BL. This function does not check if we should
5145 stop, only if BL explains the stop. */
5146
5147 static int
5148 bpstat_check_location (const struct bp_location *bl,
5149 struct address_space *aspace, CORE_ADDR bp_addr,
5150 const struct target_waitstatus *ws)
5151 {
5152 struct breakpoint *b = bl->owner;
5153
5154 /* BL is from an existing breakpoint. */
5155 gdb_assert (b != NULL);
5156
5157 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
5158 }
5159
5160 /* Determine if the watched values have actually changed, and we
5161 should stop. If not, set BS->stop to 0. */
5162
5163 static void
5164 bpstat_check_watchpoint (bpstat bs)
5165 {
5166 const struct bp_location *bl;
5167 struct watchpoint *b;
5168
5169 /* BS is built for existing struct breakpoint. */
5170 bl = bs->bp_location_at;
5171 gdb_assert (bl != NULL);
5172 b = (struct watchpoint *) bs->breakpoint_at;
5173 gdb_assert (b != NULL);
5174
5175 {
5176 int must_check_value = 0;
5177
5178 if (b->base.type == bp_watchpoint)
5179 /* For a software watchpoint, we must always check the
5180 watched value. */
5181 must_check_value = 1;
5182 else if (b->watchpoint_triggered == watch_triggered_yes)
5183 /* We have a hardware watchpoint (read, write, or access)
5184 and the target earlier reported an address watched by
5185 this watchpoint. */
5186 must_check_value = 1;
5187 else if (b->watchpoint_triggered == watch_triggered_unknown
5188 && b->base.type == bp_hardware_watchpoint)
5189 /* We were stopped by a hardware watchpoint, but the target could
5190 not report the data address. We must check the watchpoint's
5191 value. Access and read watchpoints are out of luck; without
5192 a data address, we can't figure it out. */
5193 must_check_value = 1;
5194
5195 if (must_check_value)
5196 {
5197 char *message
5198 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
5199 b->base.number);
5200 struct cleanup *cleanups = make_cleanup (xfree, message);
5201 int e = catch_errors (watchpoint_check, bs, message,
5202 RETURN_MASK_ALL);
5203 do_cleanups (cleanups);
5204 switch (e)
5205 {
5206 case WP_DELETED:
5207 /* We've already printed what needs to be printed. */
5208 bs->print_it = print_it_done;
5209 /* Stop. */
5210 break;
5211 case WP_IGNORE:
5212 bs->print_it = print_it_noop;
5213 bs->stop = 0;
5214 break;
5215 case WP_VALUE_CHANGED:
5216 if (b->base.type == bp_read_watchpoint)
5217 {
5218 /* There are two cases to consider here:
5219
5220 1. We're watching the triggered memory for reads.
5221 In that case, trust the target, and always report
5222 the watchpoint hit to the user. Even though
5223 reads don't cause value changes, the value may
5224 have changed since the last time it was read, and
5225 since we're not trapping writes, we will not see
5226 those, and as such we should ignore our notion of
5227 old value.
5228
5229 2. We're watching the triggered memory for both
5230 reads and writes. There are two ways this may
5231 happen:
5232
5233 2.1. This is a target that can't break on data
5234 reads only, but can break on accesses (reads or
5235 writes), such as e.g., x86. We detect this case
5236 at the time we try to insert read watchpoints.
5237
5238 2.2. Otherwise, the target supports read
5239 watchpoints, but, the user set an access or write
5240 watchpoint watching the same memory as this read
5241 watchpoint.
5242
5243 If we're watching memory writes as well as reads,
5244 ignore watchpoint hits when we find that the
5245 value hasn't changed, as reads don't cause
5246 changes. This still gives false positives when
5247 the program writes the same value to memory as
5248 what there was already in memory (we will confuse
5249 it for a read), but it's much better than
5250 nothing. */
5251
5252 int other_write_watchpoint = 0;
5253
5254 if (bl->watchpoint_type == hw_read)
5255 {
5256 struct breakpoint *other_b;
5257
5258 ALL_BREAKPOINTS (other_b)
5259 if (other_b->type == bp_hardware_watchpoint
5260 || other_b->type == bp_access_watchpoint)
5261 {
5262 struct watchpoint *other_w =
5263 (struct watchpoint *) other_b;
5264
5265 if (other_w->watchpoint_triggered
5266 == watch_triggered_yes)
5267 {
5268 other_write_watchpoint = 1;
5269 break;
5270 }
5271 }
5272 }
5273
5274 if (other_write_watchpoint
5275 || bl->watchpoint_type == hw_access)
5276 {
5277 /* We're watching the same memory for writes,
5278 and the value changed since the last time we
5279 updated it, so this trap must be for a write.
5280 Ignore it. */
5281 bs->print_it = print_it_noop;
5282 bs->stop = 0;
5283 }
5284 }
5285 break;
5286 case WP_VALUE_NOT_CHANGED:
5287 if (b->base.type == bp_hardware_watchpoint
5288 || b->base.type == bp_watchpoint)
5289 {
5290 /* Don't stop: write watchpoints shouldn't fire if
5291 the value hasn't changed. */
5292 bs->print_it = print_it_noop;
5293 bs->stop = 0;
5294 }
5295 /* Stop. */
5296 break;
5297 default:
5298 /* Can't happen. */
5299 case 0:
5300 /* Error from catch_errors. */
5301 printf_filtered (_("Watchpoint %d deleted.\n"), b->base.number);
5302 watchpoint_del_at_next_stop (b);
5303 /* We've already printed what needs to be printed. */
5304 bs->print_it = print_it_done;
5305 break;
5306 }
5307 }
5308 else /* must_check_value == 0 */
5309 {
5310 /* This is a case where some watchpoint(s) triggered, but
5311 not at the address of this watchpoint, or else no
5312 watchpoint triggered after all. So don't print
5313 anything for this watchpoint. */
5314 bs->print_it = print_it_noop;
5315 bs->stop = 0;
5316 }
5317 }
5318 }
5319
5320 /* For breakpoints that are currently marked as telling gdb to stop,
5321 check conditions (condition proper, frame, thread and ignore count)
5322 of breakpoint referred to by BS. If we should not stop for this
5323 breakpoint, set BS->stop to 0. */
5324
5325 static void
5326 bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5327 {
5328 const struct bp_location *bl;
5329 struct breakpoint *b;
5330 int value_is_zero = 0;
5331 struct expression *cond;
5332
5333 gdb_assert (bs->stop);
5334
5335 /* BS is built for existing struct breakpoint. */
5336 bl = bs->bp_location_at;
5337 gdb_assert (bl != NULL);
5338 b = bs->breakpoint_at;
5339 gdb_assert (b != NULL);
5340
5341 /* Even if the target evaluated the condition on its end and notified GDB, we
5342 need to do so again since GDB does not know if we stopped due to a
5343 breakpoint or a single step breakpoint. */
5344
5345 if (frame_id_p (b->frame_id)
5346 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
5347 {
5348 bs->stop = 0;
5349 return;
5350 }
5351
5352 /* If this is a thread/task-specific breakpoint, don't waste cpu
5353 evaluating the condition if this isn't the specified
5354 thread/task. */
5355 if ((b->thread != -1 && b->thread != pid_to_thread_id (ptid))
5356 || (b->task != 0 && b->task != ada_get_task_number (ptid)))
5357
5358 {
5359 bs->stop = 0;
5360 return;
5361 }
5362
5363 /* Evaluate extension language breakpoints that have a "stop" method
5364 implemented. */
5365 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
5366
5367 if (is_watchpoint (b))
5368 {
5369 struct watchpoint *w = (struct watchpoint *) b;
5370
5371 cond = w->cond_exp;
5372 }
5373 else
5374 cond = bl->cond;
5375
5376 if (cond && b->disposition != disp_del_at_next_stop)
5377 {
5378 int within_current_scope = 1;
5379 struct watchpoint * w;
5380
5381 /* We use value_mark and value_free_to_mark because it could
5382 be a long time before we return to the command level and
5383 call free_all_values. We can't call free_all_values
5384 because we might be in the middle of evaluating a
5385 function call. */
5386 struct value *mark = value_mark ();
5387
5388 if (is_watchpoint (b))
5389 w = (struct watchpoint *) b;
5390 else
5391 w = NULL;
5392
5393 /* Need to select the frame, with all that implies so that
5394 the conditions will have the right context. Because we
5395 use the frame, we will not see an inlined function's
5396 variables when we arrive at a breakpoint at the start
5397 of the inlined function; the current frame will be the
5398 call site. */
5399 if (w == NULL || w->cond_exp_valid_block == NULL)
5400 select_frame (get_current_frame ());
5401 else
5402 {
5403 struct frame_info *frame;
5404
5405 /* For local watchpoint expressions, which particular
5406 instance of a local is being watched matters, so we
5407 keep track of the frame to evaluate the expression
5408 in. To evaluate the condition however, it doesn't
5409 really matter which instantiation of the function
5410 where the condition makes sense triggers the
5411 watchpoint. This allows an expression like "watch
5412 global if q > 10" set in `func', catch writes to
5413 global on all threads that call `func', or catch
5414 writes on all recursive calls of `func' by a single
5415 thread. We simply always evaluate the condition in
5416 the innermost frame that's executing where it makes
5417 sense to evaluate the condition. It seems
5418 intuitive. */
5419 frame = block_innermost_frame (w->cond_exp_valid_block);
5420 if (frame != NULL)
5421 select_frame (frame);
5422 else
5423 within_current_scope = 0;
5424 }
5425 if (within_current_scope)
5426 value_is_zero
5427 = catch_errors (breakpoint_cond_eval, cond,
5428 "Error in testing breakpoint condition:\n",
5429 RETURN_MASK_ALL);
5430 else
5431 {
5432 warning (_("Watchpoint condition cannot be tested "
5433 "in the current scope"));
5434 /* If we failed to set the right context for this
5435 watchpoint, unconditionally report it. */
5436 value_is_zero = 0;
5437 }
5438 /* FIXME-someday, should give breakpoint #. */
5439 value_free_to_mark (mark);
5440 }
5441
5442 if (cond && value_is_zero)
5443 {
5444 bs->stop = 0;
5445 }
5446 else if (b->ignore_count > 0)
5447 {
5448 b->ignore_count--;
5449 bs->stop = 0;
5450 /* Increase the hit count even though we don't stop. */
5451 ++(b->hit_count);
5452 observer_notify_breakpoint_modified (b);
5453 }
5454 }
5455
5456 /* Returns true if we need to track moribund locations of LOC's type
5457 on the current target. */
5458
5459 static int
5460 need_moribund_for_location_type (struct bp_location *loc)
5461 {
5462 return ((loc->loc_type == bp_loc_software_breakpoint
5463 && !target_supports_stopped_by_sw_breakpoint ())
5464 || (loc->loc_type == bp_loc_hardware_breakpoint
5465 && !target_supports_stopped_by_hw_breakpoint ()));
5466 }
5467
5468
5469 /* Get a bpstat associated with having just stopped at address
5470 BP_ADDR in thread PTID.
5471
5472 Determine whether we stopped at a breakpoint, etc, or whether we
5473 don't understand this stop. Result is a chain of bpstat's such
5474 that:
5475
5476 if we don't understand the stop, the result is a null pointer.
5477
5478 if we understand why we stopped, the result is not null.
5479
5480 Each element of the chain refers to a particular breakpoint or
5481 watchpoint at which we have stopped. (We may have stopped for
5482 several reasons concurrently.)
5483
5484 Each element of the chain has valid next, breakpoint_at,
5485 commands, FIXME??? fields. */
5486
5487 bpstat
5488 bpstat_stop_status (struct address_space *aspace,
5489 CORE_ADDR bp_addr, ptid_t ptid,
5490 const struct target_waitstatus *ws)
5491 {
5492 struct breakpoint *b = NULL;
5493 struct bp_location *bl;
5494 struct bp_location *loc;
5495 /* First item of allocated bpstat's. */
5496 bpstat bs_head = NULL, *bs_link = &bs_head;
5497 /* Pointer to the last thing in the chain currently. */
5498 bpstat bs;
5499 int ix;
5500 int need_remove_insert;
5501 int removed_any;
5502
5503 /* First, build the bpstat chain with locations that explain a
5504 target stop, while being careful to not set the target running,
5505 as that may invalidate locations (in particular watchpoint
5506 locations are recreated). Resuming will happen here with
5507 breakpoint conditions or watchpoint expressions that include
5508 inferior function calls. */
5509
5510 ALL_BREAKPOINTS (b)
5511 {
5512 if (!breakpoint_enabled (b))
5513 continue;
5514
5515 for (bl = b->loc; bl != NULL; bl = bl->next)
5516 {
5517 /* For hardware watchpoints, we look only at the first
5518 location. The watchpoint_check function will work on the
5519 entire expression, not the individual locations. For
5520 read watchpoints, the watchpoints_triggered function has
5521 checked all locations already. */
5522 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5523 break;
5524
5525 if (!bl->enabled || bl->shlib_disabled)
5526 continue;
5527
5528 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
5529 continue;
5530
5531 /* Come here if it's a watchpoint, or if the break address
5532 matches. */
5533
5534 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
5535 explain stop. */
5536
5537 /* Assume we stop. Should we find a watchpoint that is not
5538 actually triggered, or if the condition of the breakpoint
5539 evaluates as false, we'll reset 'stop' to 0. */
5540 bs->stop = 1;
5541 bs->print = 1;
5542
5543 /* If this is a scope breakpoint, mark the associated
5544 watchpoint as triggered so that we will handle the
5545 out-of-scope event. We'll get to the watchpoint next
5546 iteration. */
5547 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
5548 {
5549 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5550
5551 w->watchpoint_triggered = watch_triggered_yes;
5552 }
5553 }
5554 }
5555
5556 /* Check if a moribund breakpoint explains the stop. */
5557 if (!target_supports_stopped_by_sw_breakpoint ()
5558 || !target_supports_stopped_by_hw_breakpoint ())
5559 {
5560 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
5561 {
5562 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5563 && need_moribund_for_location_type (loc))
5564 {
5565 bs = bpstat_alloc (loc, &bs_link);
5566 /* For hits of moribund locations, we should just proceed. */
5567 bs->stop = 0;
5568 bs->print = 0;
5569 bs->print_it = print_it_noop;
5570 }
5571 }
5572 }
5573
5574 /* A bit of special processing for shlib breakpoints. We need to
5575 process solib loading here, so that the lists of loaded and
5576 unloaded libraries are correct before we handle "catch load" and
5577 "catch unload". */
5578 for (bs = bs_head; bs != NULL; bs = bs->next)
5579 {
5580 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
5581 {
5582 handle_solib_event ();
5583 break;
5584 }
5585 }
5586
5587 /* Now go through the locations that caused the target to stop, and
5588 check whether we're interested in reporting this stop to higher
5589 layers, or whether we should resume the target transparently. */
5590
5591 removed_any = 0;
5592
5593 for (bs = bs_head; bs != NULL; bs = bs->next)
5594 {
5595 if (!bs->stop)
5596 continue;
5597
5598 b = bs->breakpoint_at;
5599 b->ops->check_status (bs);
5600 if (bs->stop)
5601 {
5602 bpstat_check_breakpoint_conditions (bs, ptid);
5603
5604 if (bs->stop)
5605 {
5606 ++(b->hit_count);
5607 observer_notify_breakpoint_modified (b);
5608
5609 /* We will stop here. */
5610 if (b->disposition == disp_disable)
5611 {
5612 --(b->enable_count);
5613 if (b->enable_count <= 0)
5614 b->enable_state = bp_disabled;
5615 removed_any = 1;
5616 }
5617 if (b->silent)
5618 bs->print = 0;
5619 bs->commands = b->commands;
5620 incref_counted_command_line (bs->commands);
5621 if (command_line_is_silent (bs->commands
5622 ? bs->commands->commands : NULL))
5623 bs->print = 0;
5624
5625 b->ops->after_condition_true (bs);
5626 }
5627
5628 }
5629
5630 /* Print nothing for this entry if we don't stop or don't
5631 print. */
5632 if (!bs->stop || !bs->print)
5633 bs->print_it = print_it_noop;
5634 }
5635
5636 /* If we aren't stopping, the value of some hardware watchpoint may
5637 not have changed, but the intermediate memory locations we are
5638 watching may have. Don't bother if we're stopping; this will get
5639 done later. */
5640 need_remove_insert = 0;
5641 if (! bpstat_causes_stop (bs_head))
5642 for (bs = bs_head; bs != NULL; bs = bs->next)
5643 if (!bs->stop
5644 && bs->breakpoint_at
5645 && is_hardware_watchpoint (bs->breakpoint_at))
5646 {
5647 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5648
5649 update_watchpoint (w, 0 /* don't reparse. */);
5650 need_remove_insert = 1;
5651 }
5652
5653 if (need_remove_insert)
5654 update_global_location_list (UGLL_MAY_INSERT);
5655 else if (removed_any)
5656 update_global_location_list (UGLL_DONT_INSERT);
5657
5658 return bs_head;
5659 }
5660
5661 static void
5662 handle_jit_event (void)
5663 {
5664 struct frame_info *frame;
5665 struct gdbarch *gdbarch;
5666
5667 /* Switch terminal for any messages produced by
5668 breakpoint_re_set. */
5669 target_terminal_ours_for_output ();
5670
5671 frame = get_current_frame ();
5672 gdbarch = get_frame_arch (frame);
5673
5674 jit_event_handler (gdbarch);
5675
5676 target_terminal_inferior ();
5677 }
5678
5679 /* Prepare WHAT final decision for infrun. */
5680
5681 /* Decide what infrun needs to do with this bpstat. */
5682
5683 struct bpstat_what
5684 bpstat_what (bpstat bs_head)
5685 {
5686 struct bpstat_what retval;
5687 int jit_event = 0;
5688 bpstat bs;
5689
5690 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
5691 retval.call_dummy = STOP_NONE;
5692 retval.is_longjmp = 0;
5693
5694 for (bs = bs_head; bs != NULL; bs = bs->next)
5695 {
5696 /* Extract this BS's action. After processing each BS, we check
5697 if its action overrides all we've seem so far. */
5698 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5699 enum bptype bptype;
5700
5701 if (bs->breakpoint_at == NULL)
5702 {
5703 /* I suspect this can happen if it was a momentary
5704 breakpoint which has since been deleted. */
5705 bptype = bp_none;
5706 }
5707 else
5708 bptype = bs->breakpoint_at->type;
5709
5710 switch (bptype)
5711 {
5712 case bp_none:
5713 break;
5714 case bp_breakpoint:
5715 case bp_hardware_breakpoint:
5716 case bp_single_step:
5717 case bp_until:
5718 case bp_finish:
5719 case bp_shlib_event:
5720 if (bs->stop)
5721 {
5722 if (bs->print)
5723 this_action = BPSTAT_WHAT_STOP_NOISY;
5724 else
5725 this_action = BPSTAT_WHAT_STOP_SILENT;
5726 }
5727 else
5728 this_action = BPSTAT_WHAT_SINGLE;
5729 break;
5730 case bp_watchpoint:
5731 case bp_hardware_watchpoint:
5732 case bp_read_watchpoint:
5733 case bp_access_watchpoint:
5734 if (bs->stop)
5735 {
5736 if (bs->print)
5737 this_action = BPSTAT_WHAT_STOP_NOISY;
5738 else
5739 this_action = BPSTAT_WHAT_STOP_SILENT;
5740 }
5741 else
5742 {
5743 /* There was a watchpoint, but we're not stopping.
5744 This requires no further action. */
5745 }
5746 break;
5747 case bp_longjmp:
5748 case bp_longjmp_call_dummy:
5749 case bp_exception:
5750 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5751 retval.is_longjmp = bptype != bp_exception;
5752 break;
5753 case bp_longjmp_resume:
5754 case bp_exception_resume:
5755 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5756 retval.is_longjmp = bptype == bp_longjmp_resume;
5757 break;
5758 case bp_step_resume:
5759 if (bs->stop)
5760 this_action = BPSTAT_WHAT_STEP_RESUME;
5761 else
5762 {
5763 /* It is for the wrong frame. */
5764 this_action = BPSTAT_WHAT_SINGLE;
5765 }
5766 break;
5767 case bp_hp_step_resume:
5768 if (bs->stop)
5769 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5770 else
5771 {
5772 /* It is for the wrong frame. */
5773 this_action = BPSTAT_WHAT_SINGLE;
5774 }
5775 break;
5776 case bp_watchpoint_scope:
5777 case bp_thread_event:
5778 case bp_overlay_event:
5779 case bp_longjmp_master:
5780 case bp_std_terminate_master:
5781 case bp_exception_master:
5782 this_action = BPSTAT_WHAT_SINGLE;
5783 break;
5784 case bp_catchpoint:
5785 if (bs->stop)
5786 {
5787 if (bs->print)
5788 this_action = BPSTAT_WHAT_STOP_NOISY;
5789 else
5790 this_action = BPSTAT_WHAT_STOP_SILENT;
5791 }
5792 else
5793 {
5794 /* There was a catchpoint, but we're not stopping.
5795 This requires no further action. */
5796 }
5797 break;
5798 case bp_jit_event:
5799 jit_event = 1;
5800 this_action = BPSTAT_WHAT_SINGLE;
5801 break;
5802 case bp_call_dummy:
5803 /* Make sure the action is stop (silent or noisy),
5804 so infrun.c pops the dummy frame. */
5805 retval.call_dummy = STOP_STACK_DUMMY;
5806 this_action = BPSTAT_WHAT_STOP_SILENT;
5807 break;
5808 case bp_std_terminate:
5809 /* Make sure the action is stop (silent or noisy),
5810 so infrun.c pops the dummy frame. */
5811 retval.call_dummy = STOP_STD_TERMINATE;
5812 this_action = BPSTAT_WHAT_STOP_SILENT;
5813 break;
5814 case bp_tracepoint:
5815 case bp_fast_tracepoint:
5816 case bp_static_tracepoint:
5817 /* Tracepoint hits should not be reported back to GDB, and
5818 if one got through somehow, it should have been filtered
5819 out already. */
5820 internal_error (__FILE__, __LINE__,
5821 _("bpstat_what: tracepoint encountered"));
5822 break;
5823 case bp_gnu_ifunc_resolver:
5824 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5825 this_action = BPSTAT_WHAT_SINGLE;
5826 break;
5827 case bp_gnu_ifunc_resolver_return:
5828 /* The breakpoint will be removed, execution will restart from the
5829 PC of the former breakpoint. */
5830 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5831 break;
5832
5833 case bp_dprintf:
5834 if (bs->stop)
5835 this_action = BPSTAT_WHAT_STOP_SILENT;
5836 else
5837 this_action = BPSTAT_WHAT_SINGLE;
5838 break;
5839
5840 default:
5841 internal_error (__FILE__, __LINE__,
5842 _("bpstat_what: unhandled bptype %d"), (int) bptype);
5843 }
5844
5845 retval.main_action = max (retval.main_action, this_action);
5846 }
5847
5848 /* These operations may affect the bs->breakpoint_at state so they are
5849 delayed after MAIN_ACTION is decided above. */
5850
5851 if (jit_event)
5852 {
5853 if (debug_infrun)
5854 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_jit_event\n");
5855
5856 handle_jit_event ();
5857 }
5858
5859 for (bs = bs_head; bs != NULL; bs = bs->next)
5860 {
5861 struct breakpoint *b = bs->breakpoint_at;
5862
5863 if (b == NULL)
5864 continue;
5865 switch (b->type)
5866 {
5867 case bp_gnu_ifunc_resolver:
5868 gnu_ifunc_resolver_stop (b);
5869 break;
5870 case bp_gnu_ifunc_resolver_return:
5871 gnu_ifunc_resolver_return_stop (b);
5872 break;
5873 }
5874 }
5875
5876 return retval;
5877 }
5878
5879 /* Nonzero if we should step constantly (e.g. watchpoints on machines
5880 without hardware support). This isn't related to a specific bpstat,
5881 just to things like whether watchpoints are set. */
5882
5883 int
5884 bpstat_should_step (void)
5885 {
5886 struct breakpoint *b;
5887
5888 ALL_BREAKPOINTS (b)
5889 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
5890 return 1;
5891 return 0;
5892 }
5893
5894 int
5895 bpstat_causes_stop (bpstat bs)
5896 {
5897 for (; bs != NULL; bs = bs->next)
5898 if (bs->stop)
5899 return 1;
5900
5901 return 0;
5902 }
5903
5904 \f
5905
5906 /* Compute a string of spaces suitable to indent the next line
5907 so it starts at the position corresponding to the table column
5908 named COL_NAME in the currently active table of UIOUT. */
5909
5910 static char *
5911 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5912 {
5913 static char wrap_indent[80];
5914 int i, total_width, width, align;
5915 char *text;
5916
5917 total_width = 0;
5918 for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
5919 {
5920 if (strcmp (text, col_name) == 0)
5921 {
5922 gdb_assert (total_width < sizeof wrap_indent);
5923 memset (wrap_indent, ' ', total_width);
5924 wrap_indent[total_width] = 0;
5925
5926 return wrap_indent;
5927 }
5928
5929 total_width += width + 1;
5930 }
5931
5932 return NULL;
5933 }
5934
5935 /* Determine if the locations of this breakpoint will have their conditions
5936 evaluated by the target, host or a mix of both. Returns the following:
5937
5938 "host": Host evals condition.
5939 "host or target": Host or Target evals condition.
5940 "target": Target evals condition.
5941 */
5942
5943 static const char *
5944 bp_condition_evaluator (struct breakpoint *b)
5945 {
5946 struct bp_location *bl;
5947 char host_evals = 0;
5948 char target_evals = 0;
5949
5950 if (!b)
5951 return NULL;
5952
5953 if (!is_breakpoint (b))
5954 return NULL;
5955
5956 if (gdb_evaluates_breakpoint_condition_p ()
5957 || !target_supports_evaluation_of_breakpoint_conditions ())
5958 return condition_evaluation_host;
5959
5960 for (bl = b->loc; bl; bl = bl->next)
5961 {
5962 if (bl->cond_bytecode)
5963 target_evals++;
5964 else
5965 host_evals++;
5966 }
5967
5968 if (host_evals && target_evals)
5969 return condition_evaluation_both;
5970 else if (target_evals)
5971 return condition_evaluation_target;
5972 else
5973 return condition_evaluation_host;
5974 }
5975
5976 /* Determine the breakpoint location's condition evaluator. This is
5977 similar to bp_condition_evaluator, but for locations. */
5978
5979 static const char *
5980 bp_location_condition_evaluator (struct bp_location *bl)
5981 {
5982 if (bl && !is_breakpoint (bl->owner))
5983 return NULL;
5984
5985 if (gdb_evaluates_breakpoint_condition_p ()
5986 || !target_supports_evaluation_of_breakpoint_conditions ())
5987 return condition_evaluation_host;
5988
5989 if (bl && bl->cond_bytecode)
5990 return condition_evaluation_target;
5991 else
5992 return condition_evaluation_host;
5993 }
5994
5995 /* Print the LOC location out of the list of B->LOC locations. */
5996
5997 static void
5998 print_breakpoint_location (struct breakpoint *b,
5999 struct bp_location *loc)
6000 {
6001 struct ui_out *uiout = current_uiout;
6002 struct cleanup *old_chain = save_current_program_space ();
6003
6004 if (loc != NULL && loc->shlib_disabled)
6005 loc = NULL;
6006
6007 if (loc != NULL)
6008 set_current_program_space (loc->pspace);
6009
6010 if (b->display_canonical)
6011 ui_out_field_string (uiout, "what", b->addr_string);
6012 else if (loc && loc->symtab)
6013 {
6014 struct symbol *sym
6015 = find_pc_sect_function (loc->address, loc->section);
6016 if (sym)
6017 {
6018 ui_out_text (uiout, "in ");
6019 ui_out_field_string (uiout, "func",
6020 SYMBOL_PRINT_NAME (sym));
6021 ui_out_text (uiout, " ");
6022 ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
6023 ui_out_text (uiout, "at ");
6024 }
6025 ui_out_field_string (uiout, "file",
6026 symtab_to_filename_for_display (loc->symtab));
6027 ui_out_text (uiout, ":");
6028
6029 if (ui_out_is_mi_like_p (uiout))
6030 ui_out_field_string (uiout, "fullname",
6031 symtab_to_fullname (loc->symtab));
6032
6033 ui_out_field_int (uiout, "line", loc->line_number);
6034 }
6035 else if (loc)
6036 {
6037 struct ui_file *stb = mem_fileopen ();
6038 struct cleanup *stb_chain = make_cleanup_ui_file_delete (stb);
6039
6040 print_address_symbolic (loc->gdbarch, loc->address, stb,
6041 demangle, "");
6042 ui_out_field_stream (uiout, "at", stb);
6043
6044 do_cleanups (stb_chain);
6045 }
6046 else
6047 ui_out_field_string (uiout, "pending", b->addr_string);
6048
6049 if (loc && is_breakpoint (b)
6050 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6051 && bp_condition_evaluator (b) == condition_evaluation_both)
6052 {
6053 ui_out_text (uiout, " (");
6054 ui_out_field_string (uiout, "evaluated-by",
6055 bp_location_condition_evaluator (loc));
6056 ui_out_text (uiout, ")");
6057 }
6058
6059 do_cleanups (old_chain);
6060 }
6061
6062 static const char *
6063 bptype_string (enum bptype type)
6064 {
6065 struct ep_type_description
6066 {
6067 enum bptype type;
6068 char *description;
6069 };
6070 static struct ep_type_description bptypes[] =
6071 {
6072 {bp_none, "?deleted?"},
6073 {bp_breakpoint, "breakpoint"},
6074 {bp_hardware_breakpoint, "hw breakpoint"},
6075 {bp_single_step, "sw single-step"},
6076 {bp_until, "until"},
6077 {bp_finish, "finish"},
6078 {bp_watchpoint, "watchpoint"},
6079 {bp_hardware_watchpoint, "hw watchpoint"},
6080 {bp_read_watchpoint, "read watchpoint"},
6081 {bp_access_watchpoint, "acc watchpoint"},
6082 {bp_longjmp, "longjmp"},
6083 {bp_longjmp_resume, "longjmp resume"},
6084 {bp_longjmp_call_dummy, "longjmp for call dummy"},
6085 {bp_exception, "exception"},
6086 {bp_exception_resume, "exception resume"},
6087 {bp_step_resume, "step resume"},
6088 {bp_hp_step_resume, "high-priority step resume"},
6089 {bp_watchpoint_scope, "watchpoint scope"},
6090 {bp_call_dummy, "call dummy"},
6091 {bp_std_terminate, "std::terminate"},
6092 {bp_shlib_event, "shlib events"},
6093 {bp_thread_event, "thread events"},
6094 {bp_overlay_event, "overlay events"},
6095 {bp_longjmp_master, "longjmp master"},
6096 {bp_std_terminate_master, "std::terminate master"},
6097 {bp_exception_master, "exception master"},
6098 {bp_catchpoint, "catchpoint"},
6099 {bp_tracepoint, "tracepoint"},
6100 {bp_fast_tracepoint, "fast tracepoint"},
6101 {bp_static_tracepoint, "static tracepoint"},
6102 {bp_dprintf, "dprintf"},
6103 {bp_jit_event, "jit events"},
6104 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6105 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
6106 };
6107
6108 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6109 || ((int) type != bptypes[(int) type].type))
6110 internal_error (__FILE__, __LINE__,
6111 _("bptypes table does not describe type #%d."),
6112 (int) type);
6113
6114 return bptypes[(int) type].description;
6115 }
6116
6117 /* For MI, output a field named 'thread-groups' with a list as the value.
6118 For CLI, prefix the list with the string 'inf'. */
6119
6120 static void
6121 output_thread_groups (struct ui_out *uiout,
6122 const char *field_name,
6123 VEC(int) *inf_num,
6124 int mi_only)
6125 {
6126 struct cleanup *back_to;
6127 int is_mi = ui_out_is_mi_like_p (uiout);
6128 int inf;
6129 int i;
6130
6131 /* For backward compatibility, don't display inferiors in CLI unless
6132 there are several. Always display them for MI. */
6133 if (!is_mi && mi_only)
6134 return;
6135
6136 back_to = make_cleanup_ui_out_list_begin_end (uiout, field_name);
6137
6138 for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
6139 {
6140 if (is_mi)
6141 {
6142 char mi_group[10];
6143
6144 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
6145 ui_out_field_string (uiout, NULL, mi_group);
6146 }
6147 else
6148 {
6149 if (i == 0)
6150 ui_out_text (uiout, " inf ");
6151 else
6152 ui_out_text (uiout, ", ");
6153
6154 ui_out_text (uiout, plongest (inf));
6155 }
6156 }
6157
6158 do_cleanups (back_to);
6159 }
6160
6161 /* Print B to gdb_stdout. */
6162
6163 static void
6164 print_one_breakpoint_location (struct breakpoint *b,
6165 struct bp_location *loc,
6166 int loc_number,
6167 struct bp_location **last_loc,
6168 int allflag)
6169 {
6170 struct command_line *l;
6171 static char bpenables[] = "nynny";
6172
6173 struct ui_out *uiout = current_uiout;
6174 int header_of_multiple = 0;
6175 int part_of_multiple = (loc != NULL);
6176 struct value_print_options opts;
6177
6178 get_user_print_options (&opts);
6179
6180 gdb_assert (!loc || loc_number != 0);
6181 /* See comment in print_one_breakpoint concerning treatment of
6182 breakpoints with single disabled location. */
6183 if (loc == NULL
6184 && (b->loc != NULL
6185 && (b->loc->next != NULL || !b->loc->enabled)))
6186 header_of_multiple = 1;
6187 if (loc == NULL)
6188 loc = b->loc;
6189
6190 annotate_record ();
6191
6192 /* 1 */
6193 annotate_field (0);
6194 if (part_of_multiple)
6195 {
6196 char *formatted;
6197 formatted = xstrprintf ("%d.%d", b->number, loc_number);
6198 ui_out_field_string (uiout, "number", formatted);
6199 xfree (formatted);
6200 }
6201 else
6202 {
6203 ui_out_field_int (uiout, "number", b->number);
6204 }
6205
6206 /* 2 */
6207 annotate_field (1);
6208 if (part_of_multiple)
6209 ui_out_field_skip (uiout, "type");
6210 else
6211 ui_out_field_string (uiout, "type", bptype_string (b->type));
6212
6213 /* 3 */
6214 annotate_field (2);
6215 if (part_of_multiple)
6216 ui_out_field_skip (uiout, "disp");
6217 else
6218 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
6219
6220
6221 /* 4 */
6222 annotate_field (3);
6223 if (part_of_multiple)
6224 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
6225 else
6226 ui_out_field_fmt (uiout, "enabled", "%c",
6227 bpenables[(int) b->enable_state]);
6228 ui_out_spaces (uiout, 2);
6229
6230
6231 /* 5 and 6 */
6232 if (b->ops != NULL && b->ops->print_one != NULL)
6233 {
6234 /* Although the print_one can possibly print all locations,
6235 calling it here is not likely to get any nice result. So,
6236 make sure there's just one location. */
6237 gdb_assert (b->loc == NULL || b->loc->next == NULL);
6238 b->ops->print_one (b, last_loc);
6239 }
6240 else
6241 switch (b->type)
6242 {
6243 case bp_none:
6244 internal_error (__FILE__, __LINE__,
6245 _("print_one_breakpoint: bp_none encountered\n"));
6246 break;
6247
6248 case bp_watchpoint:
6249 case bp_hardware_watchpoint:
6250 case bp_read_watchpoint:
6251 case bp_access_watchpoint:
6252 {
6253 struct watchpoint *w = (struct watchpoint *) b;
6254
6255 /* Field 4, the address, is omitted (which makes the columns
6256 not line up too nicely with the headers, but the effect
6257 is relatively readable). */
6258 if (opts.addressprint)
6259 ui_out_field_skip (uiout, "addr");
6260 annotate_field (5);
6261 ui_out_field_string (uiout, "what", w->exp_string);
6262 }
6263 break;
6264
6265 case bp_breakpoint:
6266 case bp_hardware_breakpoint:
6267 case bp_single_step:
6268 case bp_until:
6269 case bp_finish:
6270 case bp_longjmp:
6271 case bp_longjmp_resume:
6272 case bp_longjmp_call_dummy:
6273 case bp_exception:
6274 case bp_exception_resume:
6275 case bp_step_resume:
6276 case bp_hp_step_resume:
6277 case bp_watchpoint_scope:
6278 case bp_call_dummy:
6279 case bp_std_terminate:
6280 case bp_shlib_event:
6281 case bp_thread_event:
6282 case bp_overlay_event:
6283 case bp_longjmp_master:
6284 case bp_std_terminate_master:
6285 case bp_exception_master:
6286 case bp_tracepoint:
6287 case bp_fast_tracepoint:
6288 case bp_static_tracepoint:
6289 case bp_dprintf:
6290 case bp_jit_event:
6291 case bp_gnu_ifunc_resolver:
6292 case bp_gnu_ifunc_resolver_return:
6293 if (opts.addressprint)
6294 {
6295 annotate_field (4);
6296 if (header_of_multiple)
6297 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
6298 else if (b->loc == NULL || loc->shlib_disabled)
6299 ui_out_field_string (uiout, "addr", "<PENDING>");
6300 else
6301 ui_out_field_core_addr (uiout, "addr",
6302 loc->gdbarch, loc->address);
6303 }
6304 annotate_field (5);
6305 if (!header_of_multiple)
6306 print_breakpoint_location (b, loc);
6307 if (b->loc)
6308 *last_loc = b->loc;
6309 break;
6310 }
6311
6312
6313 if (loc != NULL && !header_of_multiple)
6314 {
6315 struct inferior *inf;
6316 VEC(int) *inf_num = NULL;
6317 int mi_only = 1;
6318
6319 ALL_INFERIORS (inf)
6320 {
6321 if (inf->pspace == loc->pspace)
6322 VEC_safe_push (int, inf_num, inf->num);
6323 }
6324
6325 /* For backward compatibility, don't display inferiors in CLI unless
6326 there are several. Always display for MI. */
6327 if (allflag
6328 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6329 && (number_of_program_spaces () > 1
6330 || number_of_inferiors () > 1)
6331 /* LOC is for existing B, it cannot be in
6332 moribund_locations and thus having NULL OWNER. */
6333 && loc->owner->type != bp_catchpoint))
6334 mi_only = 0;
6335 output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6336 VEC_free (int, inf_num);
6337 }
6338
6339 if (!part_of_multiple)
6340 {
6341 if (b->thread != -1)
6342 {
6343 /* FIXME: This seems to be redundant and lost here; see the
6344 "stop only in" line a little further down. */
6345 ui_out_text (uiout, " thread ");
6346 ui_out_field_int (uiout, "thread", b->thread);
6347 }
6348 else if (b->task != 0)
6349 {
6350 ui_out_text (uiout, " task ");
6351 ui_out_field_int (uiout, "task", b->task);
6352 }
6353 }
6354
6355 ui_out_text (uiout, "\n");
6356
6357 if (!part_of_multiple)
6358 b->ops->print_one_detail (b, uiout);
6359
6360 if (part_of_multiple && frame_id_p (b->frame_id))
6361 {
6362 annotate_field (6);
6363 ui_out_text (uiout, "\tstop only in stack frame at ");
6364 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
6365 the frame ID. */
6366 ui_out_field_core_addr (uiout, "frame",
6367 b->gdbarch, b->frame_id.stack_addr);
6368 ui_out_text (uiout, "\n");
6369 }
6370
6371 if (!part_of_multiple && b->cond_string)
6372 {
6373 annotate_field (7);
6374 if (is_tracepoint (b))
6375 ui_out_text (uiout, "\ttrace only if ");
6376 else
6377 ui_out_text (uiout, "\tstop only if ");
6378 ui_out_field_string (uiout, "cond", b->cond_string);
6379
6380 /* Print whether the target is doing the breakpoint's condition
6381 evaluation. If GDB is doing the evaluation, don't print anything. */
6382 if (is_breakpoint (b)
6383 && breakpoint_condition_evaluation_mode ()
6384 == condition_evaluation_target)
6385 {
6386 ui_out_text (uiout, " (");
6387 ui_out_field_string (uiout, "evaluated-by",
6388 bp_condition_evaluator (b));
6389 ui_out_text (uiout, " evals)");
6390 }
6391 ui_out_text (uiout, "\n");
6392 }
6393
6394 if (!part_of_multiple && b->thread != -1)
6395 {
6396 /* FIXME should make an annotation for this. */
6397 ui_out_text (uiout, "\tstop only in thread ");
6398 ui_out_field_int (uiout, "thread", b->thread);
6399 ui_out_text (uiout, "\n");
6400 }
6401
6402 if (!part_of_multiple)
6403 {
6404 if (b->hit_count)
6405 {
6406 /* FIXME should make an annotation for this. */
6407 if (is_catchpoint (b))
6408 ui_out_text (uiout, "\tcatchpoint");
6409 else if (is_tracepoint (b))
6410 ui_out_text (uiout, "\ttracepoint");
6411 else
6412 ui_out_text (uiout, "\tbreakpoint");
6413 ui_out_text (uiout, " already hit ");
6414 ui_out_field_int (uiout, "times", b->hit_count);
6415 if (b->hit_count == 1)
6416 ui_out_text (uiout, " time\n");
6417 else
6418 ui_out_text (uiout, " times\n");
6419 }
6420 else
6421 {
6422 /* Output the count also if it is zero, but only if this is mi. */
6423 if (ui_out_is_mi_like_p (uiout))
6424 ui_out_field_int (uiout, "times", b->hit_count);
6425 }
6426 }
6427
6428 if (!part_of_multiple && b->ignore_count)
6429 {
6430 annotate_field (8);
6431 ui_out_text (uiout, "\tignore next ");
6432 ui_out_field_int (uiout, "ignore", b->ignore_count);
6433 ui_out_text (uiout, " hits\n");
6434 }
6435
6436 /* Note that an enable count of 1 corresponds to "enable once"
6437 behavior, which is reported by the combination of enablement and
6438 disposition, so we don't need to mention it here. */
6439 if (!part_of_multiple && b->enable_count > 1)
6440 {
6441 annotate_field (8);
6442 ui_out_text (uiout, "\tdisable after ");
6443 /* Tweak the wording to clarify that ignore and enable counts
6444 are distinct, and have additive effect. */
6445 if (b->ignore_count)
6446 ui_out_text (uiout, "additional ");
6447 else
6448 ui_out_text (uiout, "next ");
6449 ui_out_field_int (uiout, "enable", b->enable_count);
6450 ui_out_text (uiout, " hits\n");
6451 }
6452
6453 if (!part_of_multiple && is_tracepoint (b))
6454 {
6455 struct tracepoint *tp = (struct tracepoint *) b;
6456
6457 if (tp->traceframe_usage)
6458 {
6459 ui_out_text (uiout, "\ttrace buffer usage ");
6460 ui_out_field_int (uiout, "traceframe-usage", tp->traceframe_usage);
6461 ui_out_text (uiout, " bytes\n");
6462 }
6463 }
6464
6465 l = b->commands ? b->commands->commands : NULL;
6466 if (!part_of_multiple && l)
6467 {
6468 struct cleanup *script_chain;
6469
6470 annotate_field (9);
6471 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
6472 print_command_lines (uiout, l, 4);
6473 do_cleanups (script_chain);
6474 }
6475
6476 if (is_tracepoint (b))
6477 {
6478 struct tracepoint *t = (struct tracepoint *) b;
6479
6480 if (!part_of_multiple && t->pass_count)
6481 {
6482 annotate_field (10);
6483 ui_out_text (uiout, "\tpass count ");
6484 ui_out_field_int (uiout, "pass", t->pass_count);
6485 ui_out_text (uiout, " \n");
6486 }
6487
6488 /* Don't display it when tracepoint or tracepoint location is
6489 pending. */
6490 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6491 {
6492 annotate_field (11);
6493
6494 if (ui_out_is_mi_like_p (uiout))
6495 ui_out_field_string (uiout, "installed",
6496 loc->inserted ? "y" : "n");
6497 else
6498 {
6499 if (loc->inserted)
6500 ui_out_text (uiout, "\t");
6501 else
6502 ui_out_text (uiout, "\tnot ");
6503 ui_out_text (uiout, "installed on target\n");
6504 }
6505 }
6506 }
6507
6508 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
6509 {
6510 if (is_watchpoint (b))
6511 {
6512 struct watchpoint *w = (struct watchpoint *) b;
6513
6514 ui_out_field_string (uiout, "original-location", w->exp_string);
6515 }
6516 else if (b->addr_string)
6517 ui_out_field_string (uiout, "original-location", b->addr_string);
6518 }
6519 }
6520
6521 static void
6522 print_one_breakpoint (struct breakpoint *b,
6523 struct bp_location **last_loc,
6524 int allflag)
6525 {
6526 struct cleanup *bkpt_chain;
6527 struct ui_out *uiout = current_uiout;
6528
6529 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
6530
6531 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
6532 do_cleanups (bkpt_chain);
6533
6534 /* If this breakpoint has custom print function,
6535 it's already printed. Otherwise, print individual
6536 locations, if any. */
6537 if (b->ops == NULL || b->ops->print_one == NULL)
6538 {
6539 /* If breakpoint has a single location that is disabled, we
6540 print it as if it had several locations, since otherwise it's
6541 hard to represent "breakpoint enabled, location disabled"
6542 situation.
6543
6544 Note that while hardware watchpoints have several locations
6545 internally, that's not a property exposed to user. */
6546 if (b->loc
6547 && !is_hardware_watchpoint (b)
6548 && (b->loc->next || !b->loc->enabled))
6549 {
6550 struct bp_location *loc;
6551 int n = 1;
6552
6553 for (loc = b->loc; loc; loc = loc->next, ++n)
6554 {
6555 struct cleanup *inner2 =
6556 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
6557 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
6558 do_cleanups (inner2);
6559 }
6560 }
6561 }
6562 }
6563
6564 static int
6565 breakpoint_address_bits (struct breakpoint *b)
6566 {
6567 int print_address_bits = 0;
6568 struct bp_location *loc;
6569
6570 for (loc = b->loc; loc; loc = loc->next)
6571 {
6572 int addr_bit;
6573
6574 /* Software watchpoints that aren't watching memory don't have
6575 an address to print. */
6576 if (b->type == bp_watchpoint && loc->watchpoint_type == -1)
6577 continue;
6578
6579 addr_bit = gdbarch_addr_bit (loc->gdbarch);
6580 if (addr_bit > print_address_bits)
6581 print_address_bits = addr_bit;
6582 }
6583
6584 return print_address_bits;
6585 }
6586
6587 struct captured_breakpoint_query_args
6588 {
6589 int bnum;
6590 };
6591
6592 static int
6593 do_captured_breakpoint_query (struct ui_out *uiout, void *data)
6594 {
6595 struct captured_breakpoint_query_args *args = data;
6596 struct breakpoint *b;
6597 struct bp_location *dummy_loc = NULL;
6598
6599 ALL_BREAKPOINTS (b)
6600 {
6601 if (args->bnum == b->number)
6602 {
6603 print_one_breakpoint (b, &dummy_loc, 0);
6604 return GDB_RC_OK;
6605 }
6606 }
6607 return GDB_RC_NONE;
6608 }
6609
6610 enum gdb_rc
6611 gdb_breakpoint_query (struct ui_out *uiout, int bnum,
6612 char **error_message)
6613 {
6614 struct captured_breakpoint_query_args args;
6615
6616 args.bnum = bnum;
6617 /* For the moment we don't trust print_one_breakpoint() to not throw
6618 an error. */
6619 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
6620 error_message, RETURN_MASK_ALL) < 0)
6621 return GDB_RC_FAIL;
6622 else
6623 return GDB_RC_OK;
6624 }
6625
6626 /* Return true if this breakpoint was set by the user, false if it is
6627 internal or momentary. */
6628
6629 int
6630 user_breakpoint_p (struct breakpoint *b)
6631 {
6632 return b->number > 0;
6633 }
6634
6635 /* Print information on user settable breakpoint (watchpoint, etc)
6636 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6637 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6638 FILTER is non-NULL, call it on each breakpoint and only include the
6639 ones for which it returns non-zero. Return the total number of
6640 breakpoints listed. */
6641
6642 static int
6643 breakpoint_1 (char *args, int allflag,
6644 int (*filter) (const struct breakpoint *))
6645 {
6646 struct breakpoint *b;
6647 struct bp_location *last_loc = NULL;
6648 int nr_printable_breakpoints;
6649 struct cleanup *bkpttbl_chain;
6650 struct value_print_options opts;
6651 int print_address_bits = 0;
6652 int print_type_col_width = 14;
6653 struct ui_out *uiout = current_uiout;
6654
6655 get_user_print_options (&opts);
6656
6657 /* Compute the number of rows in the table, as well as the size
6658 required for address fields. */
6659 nr_printable_breakpoints = 0;
6660 ALL_BREAKPOINTS (b)
6661 {
6662 /* If we have a filter, only list the breakpoints it accepts. */
6663 if (filter && !filter (b))
6664 continue;
6665
6666 /* If we have an "args" string, it is a list of breakpoints to
6667 accept. Skip the others. */
6668 if (args != NULL && *args != '\0')
6669 {
6670 if (allflag && parse_and_eval_long (args) != b->number)
6671 continue;
6672 if (!allflag && !number_is_in_list (args, b->number))
6673 continue;
6674 }
6675
6676 if (allflag || user_breakpoint_p (b))
6677 {
6678 int addr_bit, type_len;
6679
6680 addr_bit = breakpoint_address_bits (b);
6681 if (addr_bit > print_address_bits)
6682 print_address_bits = addr_bit;
6683
6684 type_len = strlen (bptype_string (b->type));
6685 if (type_len > print_type_col_width)
6686 print_type_col_width = type_len;
6687
6688 nr_printable_breakpoints++;
6689 }
6690 }
6691
6692 if (opts.addressprint)
6693 bkpttbl_chain
6694 = make_cleanup_ui_out_table_begin_end (uiout, 6,
6695 nr_printable_breakpoints,
6696 "BreakpointTable");
6697 else
6698 bkpttbl_chain
6699 = make_cleanup_ui_out_table_begin_end (uiout, 5,
6700 nr_printable_breakpoints,
6701 "BreakpointTable");
6702
6703 if (nr_printable_breakpoints > 0)
6704 annotate_breakpoints_headers ();
6705 if (nr_printable_breakpoints > 0)
6706 annotate_field (0);
6707 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
6708 if (nr_printable_breakpoints > 0)
6709 annotate_field (1);
6710 ui_out_table_header (uiout, print_type_col_width, ui_left,
6711 "type", "Type"); /* 2 */
6712 if (nr_printable_breakpoints > 0)
6713 annotate_field (2);
6714 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
6715 if (nr_printable_breakpoints > 0)
6716 annotate_field (3);
6717 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
6718 if (opts.addressprint)
6719 {
6720 if (nr_printable_breakpoints > 0)
6721 annotate_field (4);
6722 if (print_address_bits <= 32)
6723 ui_out_table_header (uiout, 10, ui_left,
6724 "addr", "Address"); /* 5 */
6725 else
6726 ui_out_table_header (uiout, 18, ui_left,
6727 "addr", "Address"); /* 5 */
6728 }
6729 if (nr_printable_breakpoints > 0)
6730 annotate_field (5);
6731 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
6732 ui_out_table_body (uiout);
6733 if (nr_printable_breakpoints > 0)
6734 annotate_breakpoints_table ();
6735
6736 ALL_BREAKPOINTS (b)
6737 {
6738 QUIT;
6739 /* If we have a filter, only list the breakpoints it accepts. */
6740 if (filter && !filter (b))
6741 continue;
6742
6743 /* If we have an "args" string, it is a list of breakpoints to
6744 accept. Skip the others. */
6745
6746 if (args != NULL && *args != '\0')
6747 {
6748 if (allflag) /* maintenance info breakpoint */
6749 {
6750 if (parse_and_eval_long (args) != b->number)
6751 continue;
6752 }
6753 else /* all others */
6754 {
6755 if (!number_is_in_list (args, b->number))
6756 continue;
6757 }
6758 }
6759 /* We only print out user settable breakpoints unless the
6760 allflag is set. */
6761 if (allflag || user_breakpoint_p (b))
6762 print_one_breakpoint (b, &last_loc, allflag);
6763 }
6764
6765 do_cleanups (bkpttbl_chain);
6766
6767 if (nr_printable_breakpoints == 0)
6768 {
6769 /* If there's a filter, let the caller decide how to report
6770 empty list. */
6771 if (!filter)
6772 {
6773 if (args == NULL || *args == '\0')
6774 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
6775 else
6776 ui_out_message (uiout, 0,
6777 "No breakpoint or watchpoint matching '%s'.\n",
6778 args);
6779 }
6780 }
6781 else
6782 {
6783 if (last_loc && !server_command)
6784 set_next_address (last_loc->gdbarch, last_loc->address);
6785 }
6786
6787 /* FIXME? Should this be moved up so that it is only called when
6788 there have been breakpoints? */
6789 annotate_breakpoints_table_end ();
6790
6791 return nr_printable_breakpoints;
6792 }
6793
6794 /* Display the value of default-collect in a way that is generally
6795 compatible with the breakpoint list. */
6796
6797 static void
6798 default_collect_info (void)
6799 {
6800 struct ui_out *uiout = current_uiout;
6801
6802 /* If it has no value (which is frequently the case), say nothing; a
6803 message like "No default-collect." gets in user's face when it's
6804 not wanted. */
6805 if (!*default_collect)
6806 return;
6807
6808 /* The following phrase lines up nicely with per-tracepoint collect
6809 actions. */
6810 ui_out_text (uiout, "default collect ");
6811 ui_out_field_string (uiout, "default-collect", default_collect);
6812 ui_out_text (uiout, " \n");
6813 }
6814
6815 static void
6816 breakpoints_info (char *args, int from_tty)
6817 {
6818 breakpoint_1 (args, 0, NULL);
6819
6820 default_collect_info ();
6821 }
6822
6823 static void
6824 watchpoints_info (char *args, int from_tty)
6825 {
6826 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
6827 struct ui_out *uiout = current_uiout;
6828
6829 if (num_printed == 0)
6830 {
6831 if (args == NULL || *args == '\0')
6832 ui_out_message (uiout, 0, "No watchpoints.\n");
6833 else
6834 ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
6835 }
6836 }
6837
6838 static void
6839 maintenance_info_breakpoints (char *args, int from_tty)
6840 {
6841 breakpoint_1 (args, 1, NULL);
6842
6843 default_collect_info ();
6844 }
6845
6846 static int
6847 breakpoint_has_pc (struct breakpoint *b,
6848 struct program_space *pspace,
6849 CORE_ADDR pc, struct obj_section *section)
6850 {
6851 struct bp_location *bl = b->loc;
6852
6853 for (; bl; bl = bl->next)
6854 {
6855 if (bl->pspace == pspace
6856 && bl->address == pc
6857 && (!overlay_debugging || bl->section == section))
6858 return 1;
6859 }
6860 return 0;
6861 }
6862
6863 /* Print a message describing any user-breakpoints set at PC. This
6864 concerns with logical breakpoints, so we match program spaces, not
6865 address spaces. */
6866
6867 static void
6868 describe_other_breakpoints (struct gdbarch *gdbarch,
6869 struct program_space *pspace, CORE_ADDR pc,
6870 struct obj_section *section, int thread)
6871 {
6872 int others = 0;
6873 struct breakpoint *b;
6874
6875 ALL_BREAKPOINTS (b)
6876 others += (user_breakpoint_p (b)
6877 && breakpoint_has_pc (b, pspace, pc, section));
6878 if (others > 0)
6879 {
6880 if (others == 1)
6881 printf_filtered (_("Note: breakpoint "));
6882 else /* if (others == ???) */
6883 printf_filtered (_("Note: breakpoints "));
6884 ALL_BREAKPOINTS (b)
6885 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
6886 {
6887 others--;
6888 printf_filtered ("%d", b->number);
6889 if (b->thread == -1 && thread != -1)
6890 printf_filtered (" (all threads)");
6891 else if (b->thread != -1)
6892 printf_filtered (" (thread %d)", b->thread);
6893 printf_filtered ("%s%s ",
6894 ((b->enable_state == bp_disabled
6895 || b->enable_state == bp_call_disabled)
6896 ? " (disabled)"
6897 : ""),
6898 (others > 1) ? ","
6899 : ((others == 1) ? " and" : ""));
6900 }
6901 printf_filtered (_("also set at pc "));
6902 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
6903 printf_filtered (".\n");
6904 }
6905 }
6906 \f
6907
6908 /* Return true iff it is meaningful to use the address member of
6909 BPT. For some breakpoint types, the address member is irrelevant
6910 and it makes no sense to attempt to compare it to other addresses
6911 (or use it for any other purpose either).
6912
6913 More specifically, each of the following breakpoint types will
6914 always have a zero valued address and we don't want to mark
6915 breakpoints of any of these types to be a duplicate of an actual
6916 breakpoint at address zero:
6917
6918 bp_watchpoint
6919 bp_catchpoint
6920
6921 */
6922
6923 static int
6924 breakpoint_address_is_meaningful (struct breakpoint *bpt)
6925 {
6926 enum bptype type = bpt->type;
6927
6928 return (type != bp_watchpoint && type != bp_catchpoint);
6929 }
6930
6931 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6932 true if LOC1 and LOC2 represent the same watchpoint location. */
6933
6934 static int
6935 watchpoint_locations_match (struct bp_location *loc1,
6936 struct bp_location *loc2)
6937 {
6938 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6939 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6940
6941 /* Both of them must exist. */
6942 gdb_assert (w1 != NULL);
6943 gdb_assert (w2 != NULL);
6944
6945 /* If the target can evaluate the condition expression in hardware,
6946 then we we need to insert both watchpoints even if they are at
6947 the same place. Otherwise the watchpoint will only trigger when
6948 the condition of whichever watchpoint was inserted evaluates to
6949 true, not giving a chance for GDB to check the condition of the
6950 other watchpoint. */
6951 if ((w1->cond_exp
6952 && target_can_accel_watchpoint_condition (loc1->address,
6953 loc1->length,
6954 loc1->watchpoint_type,
6955 w1->cond_exp))
6956 || (w2->cond_exp
6957 && target_can_accel_watchpoint_condition (loc2->address,
6958 loc2->length,
6959 loc2->watchpoint_type,
6960 w2->cond_exp)))
6961 return 0;
6962
6963 /* Note that this checks the owner's type, not the location's. In
6964 case the target does not support read watchpoints, but does
6965 support access watchpoints, we'll have bp_read_watchpoint
6966 watchpoints with hw_access locations. Those should be considered
6967 duplicates of hw_read locations. The hw_read locations will
6968 become hw_access locations later. */
6969 return (loc1->owner->type == loc2->owner->type
6970 && loc1->pspace->aspace == loc2->pspace->aspace
6971 && loc1->address == loc2->address
6972 && loc1->length == loc2->length);
6973 }
6974
6975 /* See breakpoint.h. */
6976
6977 int
6978 breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
6979 struct address_space *aspace2, CORE_ADDR addr2)
6980 {
6981 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6982 || aspace1 == aspace2)
6983 && addr1 == addr2);
6984 }
6985
6986 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
6987 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
6988 matches ASPACE2. On targets that have global breakpoints, the address
6989 space doesn't really matter. */
6990
6991 static int
6992 breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
6993 int len1, struct address_space *aspace2,
6994 CORE_ADDR addr2)
6995 {
6996 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6997 || aspace1 == aspace2)
6998 && addr2 >= addr1 && addr2 < addr1 + len1);
6999 }
7000
7001 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
7002 a ranged breakpoint. In most targets, a match happens only if ASPACE
7003 matches the breakpoint's address space. On targets that have global
7004 breakpoints, the address space doesn't really matter. */
7005
7006 static int
7007 breakpoint_location_address_match (struct bp_location *bl,
7008 struct address_space *aspace,
7009 CORE_ADDR addr)
7010 {
7011 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
7012 aspace, addr)
7013 || (bl->length
7014 && breakpoint_address_match_range (bl->pspace->aspace,
7015 bl->address, bl->length,
7016 aspace, addr)));
7017 }
7018
7019 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
7020 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
7021 true, otherwise returns false. */
7022
7023 static int
7024 tracepoint_locations_match (struct bp_location *loc1,
7025 struct bp_location *loc2)
7026 {
7027 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
7028 /* Since tracepoint locations are never duplicated with others', tracepoint
7029 locations at the same address of different tracepoints are regarded as
7030 different locations. */
7031 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7032 else
7033 return 0;
7034 }
7035
7036 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
7037 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
7038 represent the same location. */
7039
7040 static int
7041 breakpoint_locations_match (struct bp_location *loc1,
7042 struct bp_location *loc2)
7043 {
7044 int hw_point1, hw_point2;
7045
7046 /* Both of them must not be in moribund_locations. */
7047 gdb_assert (loc1->owner != NULL);
7048 gdb_assert (loc2->owner != NULL);
7049
7050 hw_point1 = is_hardware_watchpoint (loc1->owner);
7051 hw_point2 = is_hardware_watchpoint (loc2->owner);
7052
7053 if (hw_point1 != hw_point2)
7054 return 0;
7055 else if (hw_point1)
7056 return watchpoint_locations_match (loc1, loc2);
7057 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7058 return tracepoint_locations_match (loc1, loc2);
7059 else
7060 /* We compare bp_location.length in order to cover ranged breakpoints. */
7061 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7062 loc2->pspace->aspace, loc2->address)
7063 && loc1->length == loc2->length);
7064 }
7065
7066 static void
7067 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7068 int bnum, int have_bnum)
7069 {
7070 /* The longest string possibly returned by hex_string_custom
7071 is 50 chars. These must be at least that big for safety. */
7072 char astr1[64];
7073 char astr2[64];
7074
7075 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7076 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
7077 if (have_bnum)
7078 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
7079 bnum, astr1, astr2);
7080 else
7081 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
7082 }
7083
7084 /* Adjust a breakpoint's address to account for architectural
7085 constraints on breakpoint placement. Return the adjusted address.
7086 Note: Very few targets require this kind of adjustment. For most
7087 targets, this function is simply the identity function. */
7088
7089 static CORE_ADDR
7090 adjust_breakpoint_address (struct gdbarch *gdbarch,
7091 CORE_ADDR bpaddr, enum bptype bptype)
7092 {
7093 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
7094 {
7095 /* Very few targets need any kind of breakpoint adjustment. */
7096 return bpaddr;
7097 }
7098 else if (bptype == bp_watchpoint
7099 || bptype == bp_hardware_watchpoint
7100 || bptype == bp_read_watchpoint
7101 || bptype == bp_access_watchpoint
7102 || bptype == bp_catchpoint)
7103 {
7104 /* Watchpoints and the various bp_catch_* eventpoints should not
7105 have their addresses modified. */
7106 return bpaddr;
7107 }
7108 else if (bptype == bp_single_step)
7109 {
7110 /* Single-step breakpoints should not have their addresses
7111 modified. If there's any architectural constrain that
7112 applies to this address, then it should have already been
7113 taken into account when the breakpoint was created in the
7114 first place. If we didn't do this, stepping through e.g.,
7115 Thumb-2 IT blocks would break. */
7116 return bpaddr;
7117 }
7118 else
7119 {
7120 CORE_ADDR adjusted_bpaddr;
7121
7122 /* Some targets have architectural constraints on the placement
7123 of breakpoint instructions. Obtain the adjusted address. */
7124 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
7125
7126 /* An adjusted breakpoint address can significantly alter
7127 a user's expectations. Print a warning if an adjustment
7128 is required. */
7129 if (adjusted_bpaddr != bpaddr)
7130 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7131
7132 return adjusted_bpaddr;
7133 }
7134 }
7135
7136 void
7137 init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
7138 struct breakpoint *owner)
7139 {
7140 memset (loc, 0, sizeof (*loc));
7141
7142 gdb_assert (ops != NULL);
7143
7144 loc->ops = ops;
7145 loc->owner = owner;
7146 loc->cond = NULL;
7147 loc->cond_bytecode = NULL;
7148 loc->shlib_disabled = 0;
7149 loc->enabled = 1;
7150
7151 switch (owner->type)
7152 {
7153 case bp_breakpoint:
7154 case bp_single_step:
7155 case bp_until:
7156 case bp_finish:
7157 case bp_longjmp:
7158 case bp_longjmp_resume:
7159 case bp_longjmp_call_dummy:
7160 case bp_exception:
7161 case bp_exception_resume:
7162 case bp_step_resume:
7163 case bp_hp_step_resume:
7164 case bp_watchpoint_scope:
7165 case bp_call_dummy:
7166 case bp_std_terminate:
7167 case bp_shlib_event:
7168 case bp_thread_event:
7169 case bp_overlay_event:
7170 case bp_jit_event:
7171 case bp_longjmp_master:
7172 case bp_std_terminate_master:
7173 case bp_exception_master:
7174 case bp_gnu_ifunc_resolver:
7175 case bp_gnu_ifunc_resolver_return:
7176 case bp_dprintf:
7177 loc->loc_type = bp_loc_software_breakpoint;
7178 mark_breakpoint_location_modified (loc);
7179 break;
7180 case bp_hardware_breakpoint:
7181 loc->loc_type = bp_loc_hardware_breakpoint;
7182 mark_breakpoint_location_modified (loc);
7183 break;
7184 case bp_hardware_watchpoint:
7185 case bp_read_watchpoint:
7186 case bp_access_watchpoint:
7187 loc->loc_type = bp_loc_hardware_watchpoint;
7188 break;
7189 case bp_watchpoint:
7190 case bp_catchpoint:
7191 case bp_tracepoint:
7192 case bp_fast_tracepoint:
7193 case bp_static_tracepoint:
7194 loc->loc_type = bp_loc_other;
7195 break;
7196 default:
7197 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
7198 }
7199
7200 loc->refc = 1;
7201 }
7202
7203 /* Allocate a struct bp_location. */
7204
7205 static struct bp_location *
7206 allocate_bp_location (struct breakpoint *bpt)
7207 {
7208 return bpt->ops->allocate_location (bpt);
7209 }
7210
7211 static void
7212 free_bp_location (struct bp_location *loc)
7213 {
7214 loc->ops->dtor (loc);
7215 xfree (loc);
7216 }
7217
7218 /* Increment reference count. */
7219
7220 static void
7221 incref_bp_location (struct bp_location *bl)
7222 {
7223 ++bl->refc;
7224 }
7225
7226 /* Decrement reference count. If the reference count reaches 0,
7227 destroy the bp_location. Sets *BLP to NULL. */
7228
7229 static void
7230 decref_bp_location (struct bp_location **blp)
7231 {
7232 gdb_assert ((*blp)->refc > 0);
7233
7234 if (--(*blp)->refc == 0)
7235 free_bp_location (*blp);
7236 *blp = NULL;
7237 }
7238
7239 /* Add breakpoint B at the end of the global breakpoint chain. */
7240
7241 static void
7242 add_to_breakpoint_chain (struct breakpoint *b)
7243 {
7244 struct breakpoint *b1;
7245
7246 /* Add this breakpoint to the end of the chain so that a list of
7247 breakpoints will come out in order of increasing numbers. */
7248
7249 b1 = breakpoint_chain;
7250 if (b1 == 0)
7251 breakpoint_chain = b;
7252 else
7253 {
7254 while (b1->next)
7255 b1 = b1->next;
7256 b1->next = b;
7257 }
7258 }
7259
7260 /* Initializes breakpoint B with type BPTYPE and no locations yet. */
7261
7262 static void
7263 init_raw_breakpoint_without_location (struct breakpoint *b,
7264 struct gdbarch *gdbarch,
7265 enum bptype bptype,
7266 const struct breakpoint_ops *ops)
7267 {
7268 memset (b, 0, sizeof (*b));
7269
7270 gdb_assert (ops != NULL);
7271
7272 b->ops = ops;
7273 b->type = bptype;
7274 b->gdbarch = gdbarch;
7275 b->language = current_language->la_language;
7276 b->input_radix = input_radix;
7277 b->thread = -1;
7278 b->enable_state = bp_enabled;
7279 b->next = 0;
7280 b->silent = 0;
7281 b->ignore_count = 0;
7282 b->commands = NULL;
7283 b->frame_id = null_frame_id;
7284 b->condition_not_parsed = 0;
7285 b->py_bp_object = NULL;
7286 b->related_breakpoint = b;
7287 }
7288
7289 /* Helper to set_raw_breakpoint below. Creates a breakpoint
7290 that has type BPTYPE and has no locations as yet. */
7291
7292 static struct breakpoint *
7293 set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
7294 enum bptype bptype,
7295 const struct breakpoint_ops *ops)
7296 {
7297 struct breakpoint *b = XNEW (struct breakpoint);
7298
7299 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7300 add_to_breakpoint_chain (b);
7301 return b;
7302 }
7303
7304 /* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7305 resolutions should be made as the user specified the location explicitly
7306 enough. */
7307
7308 static void
7309 set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
7310 {
7311 gdb_assert (loc->owner != NULL);
7312
7313 if (loc->owner->type == bp_breakpoint
7314 || loc->owner->type == bp_hardware_breakpoint
7315 || is_tracepoint (loc->owner))
7316 {
7317 int is_gnu_ifunc;
7318 const char *function_name;
7319 CORE_ADDR func_addr;
7320
7321 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
7322 &func_addr, NULL, &is_gnu_ifunc);
7323
7324 if (is_gnu_ifunc && !explicit_loc)
7325 {
7326 struct breakpoint *b = loc->owner;
7327
7328 gdb_assert (loc->pspace == current_program_space);
7329 if (gnu_ifunc_resolve_name (function_name,
7330 &loc->requested_address))
7331 {
7332 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7333 loc->address = adjust_breakpoint_address (loc->gdbarch,
7334 loc->requested_address,
7335 b->type);
7336 }
7337 else if (b->type == bp_breakpoint && b->loc == loc
7338 && loc->next == NULL && b->related_breakpoint == b)
7339 {
7340 /* Create only the whole new breakpoint of this type but do not
7341 mess more complicated breakpoints with multiple locations. */
7342 b->type = bp_gnu_ifunc_resolver;
7343 /* Remember the resolver's address for use by the return
7344 breakpoint. */
7345 loc->related_address = func_addr;
7346 }
7347 }
7348
7349 if (function_name)
7350 loc->function_name = xstrdup (function_name);
7351 }
7352 }
7353
7354 /* Attempt to determine architecture of location identified by SAL. */
7355 struct gdbarch *
7356 get_sal_arch (struct symtab_and_line sal)
7357 {
7358 if (sal.section)
7359 return get_objfile_arch (sal.section->objfile);
7360 if (sal.symtab)
7361 return get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
7362
7363 return NULL;
7364 }
7365
7366 /* Low level routine for partially initializing a breakpoint of type
7367 BPTYPE. The newly created breakpoint's address, section, source
7368 file name, and line number are provided by SAL.
7369
7370 It is expected that the caller will complete the initialization of
7371 the newly created breakpoint struct as well as output any status
7372 information regarding the creation of a new breakpoint. */
7373
7374 static void
7375 init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
7376 struct symtab_and_line sal, enum bptype bptype,
7377 const struct breakpoint_ops *ops)
7378 {
7379 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7380
7381 add_location_to_breakpoint (b, &sal);
7382
7383 if (bptype != bp_catchpoint)
7384 gdb_assert (sal.pspace != NULL);
7385
7386 /* Store the program space that was used to set the breakpoint,
7387 except for ordinary breakpoints, which are independent of the
7388 program space. */
7389 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7390 b->pspace = sal.pspace;
7391 }
7392
7393 /* set_raw_breakpoint is a low level routine for allocating and
7394 partially initializing a breakpoint of type BPTYPE. The newly
7395 created breakpoint's address, section, source file name, and line
7396 number are provided by SAL. The newly created and partially
7397 initialized breakpoint is added to the breakpoint chain and
7398 is also returned as the value of this function.
7399
7400 It is expected that the caller will complete the initialization of
7401 the newly created breakpoint struct as well as output any status
7402 information regarding the creation of a new breakpoint. In
7403 particular, set_raw_breakpoint does NOT set the breakpoint
7404 number! Care should be taken to not allow an error to occur
7405 prior to completing the initialization of the breakpoint. If this
7406 should happen, a bogus breakpoint will be left on the chain. */
7407
7408 struct breakpoint *
7409 set_raw_breakpoint (struct gdbarch *gdbarch,
7410 struct symtab_and_line sal, enum bptype bptype,
7411 const struct breakpoint_ops *ops)
7412 {
7413 struct breakpoint *b = XNEW (struct breakpoint);
7414
7415 init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
7416 add_to_breakpoint_chain (b);
7417 return b;
7418 }
7419
7420
7421 /* Note that the breakpoint object B describes a permanent breakpoint
7422 instruction, hard-wired into the inferior's code. */
7423 void
7424 make_breakpoint_permanent (struct breakpoint *b)
7425 {
7426 struct bp_location *bl;
7427
7428 /* By definition, permanent breakpoints are already present in the
7429 code. Mark all locations as inserted. For now,
7430 make_breakpoint_permanent is called in just one place, so it's
7431 hard to say if it's reasonable to have permanent breakpoint with
7432 multiple locations or not, but it's easy to implement. */
7433 for (bl = b->loc; bl; bl = bl->next)
7434 {
7435 bl->permanent = 1;
7436 bl->inserted = 1;
7437 }
7438 }
7439
7440 /* Call this routine when stepping and nexting to enable a breakpoint
7441 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7442 initiated the operation. */
7443
7444 void
7445 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
7446 {
7447 struct breakpoint *b, *b_tmp;
7448 int thread = tp->num;
7449
7450 /* To avoid having to rescan all objfile symbols at every step,
7451 we maintain a list of continually-inserted but always disabled
7452 longjmp "master" breakpoints. Here, we simply create momentary
7453 clones of those and enable them for the requested thread. */
7454 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7455 if (b->pspace == current_program_space
7456 && (b->type == bp_longjmp_master
7457 || b->type == bp_exception_master))
7458 {
7459 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7460 struct breakpoint *clone;
7461
7462 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7463 after their removal. */
7464 clone = momentary_breakpoint_from_master (b, type,
7465 &longjmp_breakpoint_ops, 1);
7466 clone->thread = thread;
7467 }
7468
7469 tp->initiating_frame = frame;
7470 }
7471
7472 /* Delete all longjmp breakpoints from THREAD. */
7473 void
7474 delete_longjmp_breakpoint (int thread)
7475 {
7476 struct breakpoint *b, *b_tmp;
7477
7478 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7479 if (b->type == bp_longjmp || b->type == bp_exception)
7480 {
7481 if (b->thread == thread)
7482 delete_breakpoint (b);
7483 }
7484 }
7485
7486 void
7487 delete_longjmp_breakpoint_at_next_stop (int thread)
7488 {
7489 struct breakpoint *b, *b_tmp;
7490
7491 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7492 if (b->type == bp_longjmp || b->type == bp_exception)
7493 {
7494 if (b->thread == thread)
7495 b->disposition = disp_del_at_next_stop;
7496 }
7497 }
7498
7499 /* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7500 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7501 pointer to any of them. Return NULL if this system cannot place longjmp
7502 breakpoints. */
7503
7504 struct breakpoint *
7505 set_longjmp_breakpoint_for_call_dummy (void)
7506 {
7507 struct breakpoint *b, *retval = NULL;
7508
7509 ALL_BREAKPOINTS (b)
7510 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7511 {
7512 struct breakpoint *new_b;
7513
7514 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7515 &momentary_breakpoint_ops,
7516 1);
7517 new_b->thread = pid_to_thread_id (inferior_ptid);
7518
7519 /* Link NEW_B into the chain of RETVAL breakpoints. */
7520
7521 gdb_assert (new_b->related_breakpoint == new_b);
7522 if (retval == NULL)
7523 retval = new_b;
7524 new_b->related_breakpoint = retval;
7525 while (retval->related_breakpoint != new_b->related_breakpoint)
7526 retval = retval->related_breakpoint;
7527 retval->related_breakpoint = new_b;
7528 }
7529
7530 return retval;
7531 }
7532
7533 /* Verify all existing dummy frames and their associated breakpoints for
7534 TP. Remove those which can no longer be found in the current frame
7535 stack.
7536
7537 You should call this function only at places where it is safe to currently
7538 unwind the whole stack. Failed stack unwind would discard live dummy
7539 frames. */
7540
7541 void
7542 check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
7543 {
7544 struct breakpoint *b, *b_tmp;
7545
7546 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7547 if (b->type == bp_longjmp_call_dummy && b->thread == tp->num)
7548 {
7549 struct breakpoint *dummy_b = b->related_breakpoint;
7550
7551 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7552 dummy_b = dummy_b->related_breakpoint;
7553 if (dummy_b->type != bp_call_dummy
7554 || frame_find_by_id (dummy_b->frame_id) != NULL)
7555 continue;
7556
7557 dummy_frame_discard (dummy_b->frame_id, tp->ptid);
7558
7559 while (b->related_breakpoint != b)
7560 {
7561 if (b_tmp == b->related_breakpoint)
7562 b_tmp = b->related_breakpoint->next;
7563 delete_breakpoint (b->related_breakpoint);
7564 }
7565 delete_breakpoint (b);
7566 }
7567 }
7568
7569 void
7570 enable_overlay_breakpoints (void)
7571 {
7572 struct breakpoint *b;
7573
7574 ALL_BREAKPOINTS (b)
7575 if (b->type == bp_overlay_event)
7576 {
7577 b->enable_state = bp_enabled;
7578 update_global_location_list (UGLL_MAY_INSERT);
7579 overlay_events_enabled = 1;
7580 }
7581 }
7582
7583 void
7584 disable_overlay_breakpoints (void)
7585 {
7586 struct breakpoint *b;
7587
7588 ALL_BREAKPOINTS (b)
7589 if (b->type == bp_overlay_event)
7590 {
7591 b->enable_state = bp_disabled;
7592 update_global_location_list (UGLL_DONT_INSERT);
7593 overlay_events_enabled = 0;
7594 }
7595 }
7596
7597 /* Set an active std::terminate breakpoint for each std::terminate
7598 master breakpoint. */
7599 void
7600 set_std_terminate_breakpoint (void)
7601 {
7602 struct breakpoint *b, *b_tmp;
7603
7604 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7605 if (b->pspace == current_program_space
7606 && b->type == bp_std_terminate_master)
7607 {
7608 momentary_breakpoint_from_master (b, bp_std_terminate,
7609 &momentary_breakpoint_ops, 1);
7610 }
7611 }
7612
7613 /* Delete all the std::terminate breakpoints. */
7614 void
7615 delete_std_terminate_breakpoint (void)
7616 {
7617 struct breakpoint *b, *b_tmp;
7618
7619 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7620 if (b->type == bp_std_terminate)
7621 delete_breakpoint (b);
7622 }
7623
7624 struct breakpoint *
7625 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7626 {
7627 struct breakpoint *b;
7628
7629 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7630 &internal_breakpoint_ops);
7631
7632 b->enable_state = bp_enabled;
7633 /* addr_string has to be used or breakpoint_re_set will delete me. */
7634 b->addr_string
7635 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
7636
7637 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7638
7639 return b;
7640 }
7641
7642 void
7643 remove_thread_event_breakpoints (void)
7644 {
7645 struct breakpoint *b, *b_tmp;
7646
7647 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7648 if (b->type == bp_thread_event
7649 && b->loc->pspace == current_program_space)
7650 delete_breakpoint (b);
7651 }
7652
7653 struct lang_and_radix
7654 {
7655 enum language lang;
7656 int radix;
7657 };
7658
7659 /* Create a breakpoint for JIT code registration and unregistration. */
7660
7661 struct breakpoint *
7662 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7663 {
7664 struct breakpoint *b;
7665
7666 b = create_internal_breakpoint (gdbarch, address, bp_jit_event,
7667 &internal_breakpoint_ops);
7668 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7669 return b;
7670 }
7671
7672 /* Remove JIT code registration and unregistration breakpoint(s). */
7673
7674 void
7675 remove_jit_event_breakpoints (void)
7676 {
7677 struct breakpoint *b, *b_tmp;
7678
7679 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7680 if (b->type == bp_jit_event
7681 && b->loc->pspace == current_program_space)
7682 delete_breakpoint (b);
7683 }
7684
7685 void
7686 remove_solib_event_breakpoints (void)
7687 {
7688 struct breakpoint *b, *b_tmp;
7689
7690 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7691 if (b->type == bp_shlib_event
7692 && b->loc->pspace == current_program_space)
7693 delete_breakpoint (b);
7694 }
7695
7696 /* See breakpoint.h. */
7697
7698 void
7699 remove_solib_event_breakpoints_at_next_stop (void)
7700 {
7701 struct breakpoint *b, *b_tmp;
7702
7703 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7704 if (b->type == bp_shlib_event
7705 && b->loc->pspace == current_program_space)
7706 b->disposition = disp_del_at_next_stop;
7707 }
7708
7709 /* Helper for create_solib_event_breakpoint /
7710 create_and_insert_solib_event_breakpoint. Allows specifying which
7711 INSERT_MODE to pass through to update_global_location_list. */
7712
7713 static struct breakpoint *
7714 create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7715 enum ugll_insert_mode insert_mode)
7716 {
7717 struct breakpoint *b;
7718
7719 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7720 &internal_breakpoint_ops);
7721 update_global_location_list_nothrow (insert_mode);
7722 return b;
7723 }
7724
7725 struct breakpoint *
7726 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7727 {
7728 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7729 }
7730
7731 /* See breakpoint.h. */
7732
7733 struct breakpoint *
7734 create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7735 {
7736 struct breakpoint *b;
7737
7738 /* Explicitly tell update_global_location_list to insert
7739 locations. */
7740 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
7741 if (!b->loc->inserted)
7742 {
7743 delete_breakpoint (b);
7744 return NULL;
7745 }
7746 return b;
7747 }
7748
7749 /* Disable any breakpoints that are on code in shared libraries. Only
7750 apply to enabled breakpoints, disabled ones can just stay disabled. */
7751
7752 void
7753 disable_breakpoints_in_shlibs (void)
7754 {
7755 struct bp_location *loc, **locp_tmp;
7756
7757 ALL_BP_LOCATIONS (loc, locp_tmp)
7758 {
7759 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7760 struct breakpoint *b = loc->owner;
7761
7762 /* We apply the check to all breakpoints, including disabled for
7763 those with loc->duplicate set. This is so that when breakpoint
7764 becomes enabled, or the duplicate is removed, gdb will try to
7765 insert all breakpoints. If we don't set shlib_disabled here,
7766 we'll try to insert those breakpoints and fail. */
7767 if (((b->type == bp_breakpoint)
7768 || (b->type == bp_jit_event)
7769 || (b->type == bp_hardware_breakpoint)
7770 || (is_tracepoint (b)))
7771 && loc->pspace == current_program_space
7772 && !loc->shlib_disabled
7773 && solib_name_from_address (loc->pspace, loc->address)
7774 )
7775 {
7776 loc->shlib_disabled = 1;
7777 }
7778 }
7779 }
7780
7781 /* Disable any breakpoints and tracepoints that are in SOLIB upon
7782 notification of unloaded_shlib. Only apply to enabled breakpoints,
7783 disabled ones can just stay disabled. */
7784
7785 static void
7786 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7787 {
7788 struct bp_location *loc, **locp_tmp;
7789 int disabled_shlib_breaks = 0;
7790
7791 /* SunOS a.out shared libraries are always mapped, so do not
7792 disable breakpoints; they will only be reported as unloaded
7793 through clear_solib when GDB discards its shared library
7794 list. See clear_solib for more information. */
7795 if (exec_bfd != NULL
7796 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
7797 return;
7798
7799 ALL_BP_LOCATIONS (loc, locp_tmp)
7800 {
7801 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7802 struct breakpoint *b = loc->owner;
7803
7804 if (solib->pspace == loc->pspace
7805 && !loc->shlib_disabled
7806 && (((b->type == bp_breakpoint
7807 || b->type == bp_jit_event
7808 || b->type == bp_hardware_breakpoint)
7809 && (loc->loc_type == bp_loc_hardware_breakpoint
7810 || loc->loc_type == bp_loc_software_breakpoint))
7811 || is_tracepoint (b))
7812 && solib_contains_address_p (solib, loc->address))
7813 {
7814 loc->shlib_disabled = 1;
7815 /* At this point, we cannot rely on remove_breakpoint
7816 succeeding so we must mark the breakpoint as not inserted
7817 to prevent future errors occurring in remove_breakpoints. */
7818 loc->inserted = 0;
7819
7820 /* This may cause duplicate notifications for the same breakpoint. */
7821 observer_notify_breakpoint_modified (b);
7822
7823 if (!disabled_shlib_breaks)
7824 {
7825 target_terminal_ours_for_output ();
7826 warning (_("Temporarily disabling breakpoints "
7827 "for unloaded shared library \"%s\""),
7828 solib->so_name);
7829 }
7830 disabled_shlib_breaks = 1;
7831 }
7832 }
7833 }
7834
7835 /* Disable any breakpoints and tracepoints in OBJFILE upon
7836 notification of free_objfile. Only apply to enabled breakpoints,
7837 disabled ones can just stay disabled. */
7838
7839 static void
7840 disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7841 {
7842 struct breakpoint *b;
7843
7844 if (objfile == NULL)
7845 return;
7846
7847 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7848 managed by the user with add-symbol-file/remove-symbol-file.
7849 Similarly to how breakpoints in shared libraries are handled in
7850 response to "nosharedlibrary", mark breakpoints in such modules
7851 shlib_disabled so they end up uninserted on the next global
7852 location list update. Shared libraries not loaded by the user
7853 aren't handled here -- they're already handled in
7854 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7855 solib_unloaded observer. We skip objfiles that are not
7856 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7857 main objfile). */
7858 if ((objfile->flags & OBJF_SHARED) == 0
7859 || (objfile->flags & OBJF_USERLOADED) == 0)
7860 return;
7861
7862 ALL_BREAKPOINTS (b)
7863 {
7864 struct bp_location *loc;
7865 int bp_modified = 0;
7866
7867 if (!is_breakpoint (b) && !is_tracepoint (b))
7868 continue;
7869
7870 for (loc = b->loc; loc != NULL; loc = loc->next)
7871 {
7872 CORE_ADDR loc_addr = loc->address;
7873
7874 if (loc->loc_type != bp_loc_hardware_breakpoint
7875 && loc->loc_type != bp_loc_software_breakpoint)
7876 continue;
7877
7878 if (loc->shlib_disabled != 0)
7879 continue;
7880
7881 if (objfile->pspace != loc->pspace)
7882 continue;
7883
7884 if (loc->loc_type != bp_loc_hardware_breakpoint
7885 && loc->loc_type != bp_loc_software_breakpoint)
7886 continue;
7887
7888 if (is_addr_in_objfile (loc_addr, objfile))
7889 {
7890 loc->shlib_disabled = 1;
7891 /* At this point, we don't know whether the object was
7892 unmapped from the inferior or not, so leave the
7893 inserted flag alone. We'll handle failure to
7894 uninsert quietly, in case the object was indeed
7895 unmapped. */
7896
7897 mark_breakpoint_location_modified (loc);
7898
7899 bp_modified = 1;
7900 }
7901 }
7902
7903 if (bp_modified)
7904 observer_notify_breakpoint_modified (b);
7905 }
7906 }
7907
7908 /* FORK & VFORK catchpoints. */
7909
7910 /* An instance of this type is used to represent a fork or vfork
7911 catchpoint. It includes a "struct breakpoint" as a kind of base
7912 class; users downcast to "struct breakpoint *" when needed. A
7913 breakpoint is really of this type iff its ops pointer points to
7914 CATCH_FORK_BREAKPOINT_OPS. */
7915
7916 struct fork_catchpoint
7917 {
7918 /* The base class. */
7919 struct breakpoint base;
7920
7921 /* Process id of a child process whose forking triggered this
7922 catchpoint. This field is only valid immediately after this
7923 catchpoint has triggered. */
7924 ptid_t forked_inferior_pid;
7925 };
7926
7927 /* Implement the "insert" breakpoint_ops method for fork
7928 catchpoints. */
7929
7930 static int
7931 insert_catch_fork (struct bp_location *bl)
7932 {
7933 return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
7934 }
7935
7936 /* Implement the "remove" breakpoint_ops method for fork
7937 catchpoints. */
7938
7939 static int
7940 remove_catch_fork (struct bp_location *bl)
7941 {
7942 return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
7943 }
7944
7945 /* Implement the "breakpoint_hit" breakpoint_ops method for fork
7946 catchpoints. */
7947
7948 static int
7949 breakpoint_hit_catch_fork (const struct bp_location *bl,
7950 struct address_space *aspace, CORE_ADDR bp_addr,
7951 const struct target_waitstatus *ws)
7952 {
7953 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7954
7955 if (ws->kind != TARGET_WAITKIND_FORKED)
7956 return 0;
7957
7958 c->forked_inferior_pid = ws->value.related_pid;
7959 return 1;
7960 }
7961
7962 /* Implement the "print_it" breakpoint_ops method for fork
7963 catchpoints. */
7964
7965 static enum print_stop_action
7966 print_it_catch_fork (bpstat bs)
7967 {
7968 struct ui_out *uiout = current_uiout;
7969 struct breakpoint *b = bs->breakpoint_at;
7970 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
7971
7972 annotate_catchpoint (b->number);
7973 if (b->disposition == disp_del)
7974 ui_out_text (uiout, "\nTemporary catchpoint ");
7975 else
7976 ui_out_text (uiout, "\nCatchpoint ");
7977 if (ui_out_is_mi_like_p (uiout))
7978 {
7979 ui_out_field_string (uiout, "reason",
7980 async_reason_lookup (EXEC_ASYNC_FORK));
7981 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7982 }
7983 ui_out_field_int (uiout, "bkptno", b->number);
7984 ui_out_text (uiout, " (forked process ");
7985 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
7986 ui_out_text (uiout, "), ");
7987 return PRINT_SRC_AND_LOC;
7988 }
7989
7990 /* Implement the "print_one" breakpoint_ops method for fork
7991 catchpoints. */
7992
7993 static void
7994 print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
7995 {
7996 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
7997 struct value_print_options opts;
7998 struct ui_out *uiout = current_uiout;
7999
8000 get_user_print_options (&opts);
8001
8002 /* Field 4, the address, is omitted (which makes the columns not
8003 line up too nicely with the headers, but the effect is relatively
8004 readable). */
8005 if (opts.addressprint)
8006 ui_out_field_skip (uiout, "addr");
8007 annotate_field (5);
8008 ui_out_text (uiout, "fork");
8009 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
8010 {
8011 ui_out_text (uiout, ", process ");
8012 ui_out_field_int (uiout, "what",
8013 ptid_get_pid (c->forked_inferior_pid));
8014 ui_out_spaces (uiout, 1);
8015 }
8016
8017 if (ui_out_is_mi_like_p (uiout))
8018 ui_out_field_string (uiout, "catch-type", "fork");
8019 }
8020
8021 /* Implement the "print_mention" breakpoint_ops method for fork
8022 catchpoints. */
8023
8024 static void
8025 print_mention_catch_fork (struct breakpoint *b)
8026 {
8027 printf_filtered (_("Catchpoint %d (fork)"), b->number);
8028 }
8029
8030 /* Implement the "print_recreate" breakpoint_ops method for fork
8031 catchpoints. */
8032
8033 static void
8034 print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
8035 {
8036 fprintf_unfiltered (fp, "catch fork");
8037 print_recreate_thread (b, fp);
8038 }
8039
8040 /* The breakpoint_ops structure to be used in fork catchpoints. */
8041
8042 static struct breakpoint_ops catch_fork_breakpoint_ops;
8043
8044 /* Implement the "insert" breakpoint_ops method for vfork
8045 catchpoints. */
8046
8047 static int
8048 insert_catch_vfork (struct bp_location *bl)
8049 {
8050 return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
8051 }
8052
8053 /* Implement the "remove" breakpoint_ops method for vfork
8054 catchpoints. */
8055
8056 static int
8057 remove_catch_vfork (struct bp_location *bl)
8058 {
8059 return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
8060 }
8061
8062 /* Implement the "breakpoint_hit" breakpoint_ops method for vfork
8063 catchpoints. */
8064
8065 static int
8066 breakpoint_hit_catch_vfork (const struct bp_location *bl,
8067 struct address_space *aspace, CORE_ADDR bp_addr,
8068 const struct target_waitstatus *ws)
8069 {
8070 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8071
8072 if (ws->kind != TARGET_WAITKIND_VFORKED)
8073 return 0;
8074
8075 c->forked_inferior_pid = ws->value.related_pid;
8076 return 1;
8077 }
8078
8079 /* Implement the "print_it" breakpoint_ops method for vfork
8080 catchpoints. */
8081
8082 static enum print_stop_action
8083 print_it_catch_vfork (bpstat bs)
8084 {
8085 struct ui_out *uiout = current_uiout;
8086 struct breakpoint *b = bs->breakpoint_at;
8087 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8088
8089 annotate_catchpoint (b->number);
8090 if (b->disposition == disp_del)
8091 ui_out_text (uiout, "\nTemporary catchpoint ");
8092 else
8093 ui_out_text (uiout, "\nCatchpoint ");
8094 if (ui_out_is_mi_like_p (uiout))
8095 {
8096 ui_out_field_string (uiout, "reason",
8097 async_reason_lookup (EXEC_ASYNC_VFORK));
8098 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8099 }
8100 ui_out_field_int (uiout, "bkptno", b->number);
8101 ui_out_text (uiout, " (vforked process ");
8102 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
8103 ui_out_text (uiout, "), ");
8104 return PRINT_SRC_AND_LOC;
8105 }
8106
8107 /* Implement the "print_one" breakpoint_ops method for vfork
8108 catchpoints. */
8109
8110 static void
8111 print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
8112 {
8113 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8114 struct value_print_options opts;
8115 struct ui_out *uiout = current_uiout;
8116
8117 get_user_print_options (&opts);
8118 /* Field 4, the address, is omitted (which makes the columns not
8119 line up too nicely with the headers, but the effect is relatively
8120 readable). */
8121 if (opts.addressprint)
8122 ui_out_field_skip (uiout, "addr");
8123 annotate_field (5);
8124 ui_out_text (uiout, "vfork");
8125 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
8126 {
8127 ui_out_text (uiout, ", process ");
8128 ui_out_field_int (uiout, "what",
8129 ptid_get_pid (c->forked_inferior_pid));
8130 ui_out_spaces (uiout, 1);
8131 }
8132
8133 if (ui_out_is_mi_like_p (uiout))
8134 ui_out_field_string (uiout, "catch-type", "vfork");
8135 }
8136
8137 /* Implement the "print_mention" breakpoint_ops method for vfork
8138 catchpoints. */
8139
8140 static void
8141 print_mention_catch_vfork (struct breakpoint *b)
8142 {
8143 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
8144 }
8145
8146 /* Implement the "print_recreate" breakpoint_ops method for vfork
8147 catchpoints. */
8148
8149 static void
8150 print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8151 {
8152 fprintf_unfiltered (fp, "catch vfork");
8153 print_recreate_thread (b, fp);
8154 }
8155
8156 /* The breakpoint_ops structure to be used in vfork catchpoints. */
8157
8158 static struct breakpoint_ops catch_vfork_breakpoint_ops;
8159
8160 /* An instance of this type is used to represent an solib catchpoint.
8161 It includes a "struct breakpoint" as a kind of base class; users
8162 downcast to "struct breakpoint *" when needed. A breakpoint is
8163 really of this type iff its ops pointer points to
8164 CATCH_SOLIB_BREAKPOINT_OPS. */
8165
8166 struct solib_catchpoint
8167 {
8168 /* The base class. */
8169 struct breakpoint base;
8170
8171 /* True for "catch load", false for "catch unload". */
8172 unsigned char is_load;
8173
8174 /* Regular expression to match, if any. COMPILED is only valid when
8175 REGEX is non-NULL. */
8176 char *regex;
8177 regex_t compiled;
8178 };
8179
8180 static void
8181 dtor_catch_solib (struct breakpoint *b)
8182 {
8183 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8184
8185 if (self->regex)
8186 regfree (&self->compiled);
8187 xfree (self->regex);
8188
8189 base_breakpoint_ops.dtor (b);
8190 }
8191
8192 static int
8193 insert_catch_solib (struct bp_location *ignore)
8194 {
8195 return 0;
8196 }
8197
8198 static int
8199 remove_catch_solib (struct bp_location *ignore)
8200 {
8201 return 0;
8202 }
8203
8204 static int
8205 breakpoint_hit_catch_solib (const struct bp_location *bl,
8206 struct address_space *aspace,
8207 CORE_ADDR bp_addr,
8208 const struct target_waitstatus *ws)
8209 {
8210 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8211 struct breakpoint *other;
8212
8213 if (ws->kind == TARGET_WAITKIND_LOADED)
8214 return 1;
8215
8216 ALL_BREAKPOINTS (other)
8217 {
8218 struct bp_location *other_bl;
8219
8220 if (other == bl->owner)
8221 continue;
8222
8223 if (other->type != bp_shlib_event)
8224 continue;
8225
8226 if (self->base.pspace != NULL && other->pspace != self->base.pspace)
8227 continue;
8228
8229 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8230 {
8231 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8232 return 1;
8233 }
8234 }
8235
8236 return 0;
8237 }
8238
8239 static void
8240 check_status_catch_solib (struct bpstats *bs)
8241 {
8242 struct solib_catchpoint *self
8243 = (struct solib_catchpoint *) bs->breakpoint_at;
8244 int ix;
8245
8246 if (self->is_load)
8247 {
8248 struct so_list *iter;
8249
8250 for (ix = 0;
8251 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
8252 ix, iter);
8253 ++ix)
8254 {
8255 if (!self->regex
8256 || regexec (&self->compiled, iter->so_name, 0, NULL, 0) == 0)
8257 return;
8258 }
8259 }
8260 else
8261 {
8262 char *iter;
8263
8264 for (ix = 0;
8265 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
8266 ix, iter);
8267 ++ix)
8268 {
8269 if (!self->regex
8270 || regexec (&self->compiled, iter, 0, NULL, 0) == 0)
8271 return;
8272 }
8273 }
8274
8275 bs->stop = 0;
8276 bs->print_it = print_it_noop;
8277 }
8278
8279 static enum print_stop_action
8280 print_it_catch_solib (bpstat bs)
8281 {
8282 struct breakpoint *b = bs->breakpoint_at;
8283 struct ui_out *uiout = current_uiout;
8284
8285 annotate_catchpoint (b->number);
8286 if (b->disposition == disp_del)
8287 ui_out_text (uiout, "\nTemporary catchpoint ");
8288 else
8289 ui_out_text (uiout, "\nCatchpoint ");
8290 ui_out_field_int (uiout, "bkptno", b->number);
8291 ui_out_text (uiout, "\n");
8292 if (ui_out_is_mi_like_p (uiout))
8293 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8294 print_solib_event (1);
8295 return PRINT_SRC_AND_LOC;
8296 }
8297
8298 static void
8299 print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8300 {
8301 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8302 struct value_print_options opts;
8303 struct ui_out *uiout = current_uiout;
8304 char *msg;
8305
8306 get_user_print_options (&opts);
8307 /* Field 4, the address, is omitted (which makes the columns not
8308 line up too nicely with the headers, but the effect is relatively
8309 readable). */
8310 if (opts.addressprint)
8311 {
8312 annotate_field (4);
8313 ui_out_field_skip (uiout, "addr");
8314 }
8315
8316 annotate_field (5);
8317 if (self->is_load)
8318 {
8319 if (self->regex)
8320 msg = xstrprintf (_("load of library matching %s"), self->regex);
8321 else
8322 msg = xstrdup (_("load of library"));
8323 }
8324 else
8325 {
8326 if (self->regex)
8327 msg = xstrprintf (_("unload of library matching %s"), self->regex);
8328 else
8329 msg = xstrdup (_("unload of library"));
8330 }
8331 ui_out_field_string (uiout, "what", msg);
8332 xfree (msg);
8333
8334 if (ui_out_is_mi_like_p (uiout))
8335 ui_out_field_string (uiout, "catch-type",
8336 self->is_load ? "load" : "unload");
8337 }
8338
8339 static void
8340 print_mention_catch_solib (struct breakpoint *b)
8341 {
8342 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8343
8344 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8345 self->is_load ? "load" : "unload");
8346 }
8347
8348 static void
8349 print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8350 {
8351 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8352
8353 fprintf_unfiltered (fp, "%s %s",
8354 b->disposition == disp_del ? "tcatch" : "catch",
8355 self->is_load ? "load" : "unload");
8356 if (self->regex)
8357 fprintf_unfiltered (fp, " %s", self->regex);
8358 fprintf_unfiltered (fp, "\n");
8359 }
8360
8361 static struct breakpoint_ops catch_solib_breakpoint_ops;
8362
8363 /* Shared helper function (MI and CLI) for creating and installing
8364 a shared object event catchpoint. If IS_LOAD is non-zero then
8365 the events to be caught are load events, otherwise they are
8366 unload events. If IS_TEMP is non-zero the catchpoint is a
8367 temporary one. If ENABLED is non-zero the catchpoint is
8368 created in an enabled state. */
8369
8370 void
8371 add_solib_catchpoint (char *arg, int is_load, int is_temp, int enabled)
8372 {
8373 struct solib_catchpoint *c;
8374 struct gdbarch *gdbarch = get_current_arch ();
8375 struct cleanup *cleanup;
8376
8377 if (!arg)
8378 arg = "";
8379 arg = skip_spaces (arg);
8380
8381 c = XCNEW (struct solib_catchpoint);
8382 cleanup = make_cleanup (xfree, c);
8383
8384 if (*arg != '\0')
8385 {
8386 int errcode;
8387
8388 errcode = regcomp (&c->compiled, arg, REG_NOSUB);
8389 if (errcode != 0)
8390 {
8391 char *err = get_regcomp_error (errcode, &c->compiled);
8392
8393 make_cleanup (xfree, err);
8394 error (_("Invalid regexp (%s): %s"), err, arg);
8395 }
8396 c->regex = xstrdup (arg);
8397 }
8398
8399 c->is_load = is_load;
8400 init_catchpoint (&c->base, gdbarch, is_temp, NULL,
8401 &catch_solib_breakpoint_ops);
8402
8403 c->base.enable_state = enabled ? bp_enabled : bp_disabled;
8404
8405 discard_cleanups (cleanup);
8406 install_breakpoint (0, &c->base, 1);
8407 }
8408
8409 /* A helper function that does all the work for "catch load" and
8410 "catch unload". */
8411
8412 static void
8413 catch_load_or_unload (char *arg, int from_tty, int is_load,
8414 struct cmd_list_element *command)
8415 {
8416 int tempflag;
8417 const int enabled = 1;
8418
8419 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8420
8421 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8422 }
8423
8424 static void
8425 catch_load_command_1 (char *arg, int from_tty,
8426 struct cmd_list_element *command)
8427 {
8428 catch_load_or_unload (arg, from_tty, 1, command);
8429 }
8430
8431 static void
8432 catch_unload_command_1 (char *arg, int from_tty,
8433 struct cmd_list_element *command)
8434 {
8435 catch_load_or_unload (arg, from_tty, 0, command);
8436 }
8437
8438 /* An instance of this type is used to represent a syscall catchpoint.
8439 It includes a "struct breakpoint" as a kind of base class; users
8440 downcast to "struct breakpoint *" when needed. A breakpoint is
8441 really of this type iff its ops pointer points to
8442 CATCH_SYSCALL_BREAKPOINT_OPS. */
8443
8444 struct syscall_catchpoint
8445 {
8446 /* The base class. */
8447 struct breakpoint base;
8448
8449 /* Syscall numbers used for the 'catch syscall' feature. If no
8450 syscall has been specified for filtering, its value is NULL.
8451 Otherwise, it holds a list of all syscalls to be caught. The
8452 list elements are allocated with xmalloc. */
8453 VEC(int) *syscalls_to_be_caught;
8454 };
8455
8456 /* Implement the "dtor" breakpoint_ops method for syscall
8457 catchpoints. */
8458
8459 static void
8460 dtor_catch_syscall (struct breakpoint *b)
8461 {
8462 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8463
8464 VEC_free (int, c->syscalls_to_be_caught);
8465
8466 base_breakpoint_ops.dtor (b);
8467 }
8468
8469 static const struct inferior_data *catch_syscall_inferior_data = NULL;
8470
8471 struct catch_syscall_inferior_data
8472 {
8473 /* We keep a count of the number of times the user has requested a
8474 particular syscall to be tracked, and pass this information to the
8475 target. This lets capable targets implement filtering directly. */
8476
8477 /* Number of times that "any" syscall is requested. */
8478 int any_syscall_count;
8479
8480 /* Count of each system call. */
8481 VEC(int) *syscalls_counts;
8482
8483 /* This counts all syscall catch requests, so we can readily determine
8484 if any catching is necessary. */
8485 int total_syscalls_count;
8486 };
8487
8488 static struct catch_syscall_inferior_data*
8489 get_catch_syscall_inferior_data (struct inferior *inf)
8490 {
8491 struct catch_syscall_inferior_data *inf_data;
8492
8493 inf_data = inferior_data (inf, catch_syscall_inferior_data);
8494 if (inf_data == NULL)
8495 {
8496 inf_data = XCNEW (struct catch_syscall_inferior_data);
8497 set_inferior_data (inf, catch_syscall_inferior_data, inf_data);
8498 }
8499
8500 return inf_data;
8501 }
8502
8503 static void
8504 catch_syscall_inferior_data_cleanup (struct inferior *inf, void *arg)
8505 {
8506 xfree (arg);
8507 }
8508
8509
8510 /* Implement the "insert" breakpoint_ops method for syscall
8511 catchpoints. */
8512
8513 static int
8514 insert_catch_syscall (struct bp_location *bl)
8515 {
8516 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
8517 struct inferior *inf = current_inferior ();
8518 struct catch_syscall_inferior_data *inf_data
8519 = get_catch_syscall_inferior_data (inf);
8520
8521 ++inf_data->total_syscalls_count;
8522 if (!c->syscalls_to_be_caught)
8523 ++inf_data->any_syscall_count;
8524 else
8525 {
8526 int i, iter;
8527
8528 for (i = 0;
8529 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
8530 i++)
8531 {
8532 int elem;
8533
8534 if (iter >= VEC_length (int, inf_data->syscalls_counts))
8535 {
8536 int old_size = VEC_length (int, inf_data->syscalls_counts);
8537 uintptr_t vec_addr_offset
8538 = old_size * ((uintptr_t) sizeof (int));
8539 uintptr_t vec_addr;
8540 VEC_safe_grow (int, inf_data->syscalls_counts, iter + 1);
8541 vec_addr = ((uintptr_t) VEC_address (int,
8542 inf_data->syscalls_counts)
8543 + vec_addr_offset);
8544 memset ((void *) vec_addr, 0,
8545 (iter + 1 - old_size) * sizeof (int));
8546 }
8547 elem = VEC_index (int, inf_data->syscalls_counts, iter);
8548 VEC_replace (int, inf_data->syscalls_counts, iter, ++elem);
8549 }
8550 }
8551
8552 return target_set_syscall_catchpoint (ptid_get_pid (inferior_ptid),
8553 inf_data->total_syscalls_count != 0,
8554 inf_data->any_syscall_count,
8555 VEC_length (int,
8556 inf_data->syscalls_counts),
8557 VEC_address (int,
8558 inf_data->syscalls_counts));
8559 }
8560
8561 /* Implement the "remove" breakpoint_ops method for syscall
8562 catchpoints. */
8563
8564 static int
8565 remove_catch_syscall (struct bp_location *bl)
8566 {
8567 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bl->owner;
8568 struct inferior *inf = current_inferior ();
8569 struct catch_syscall_inferior_data *inf_data
8570 = get_catch_syscall_inferior_data (inf);
8571
8572 --inf_data->total_syscalls_count;
8573 if (!c->syscalls_to_be_caught)
8574 --inf_data->any_syscall_count;
8575 else
8576 {
8577 int i, iter;
8578
8579 for (i = 0;
8580 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
8581 i++)
8582 {
8583 int elem;
8584 if (iter >= VEC_length (int, inf_data->syscalls_counts))
8585 /* Shouldn't happen. */
8586 continue;
8587 elem = VEC_index (int, inf_data->syscalls_counts, iter);
8588 VEC_replace (int, inf_data->syscalls_counts, iter, --elem);
8589 }
8590 }
8591
8592 return target_set_syscall_catchpoint (ptid_get_pid (inferior_ptid),
8593 inf_data->total_syscalls_count != 0,
8594 inf_data->any_syscall_count,
8595 VEC_length (int,
8596 inf_data->syscalls_counts),
8597 VEC_address (int,
8598 inf_data->syscalls_counts));
8599 }
8600
8601 /* Implement the "breakpoint_hit" breakpoint_ops method for syscall
8602 catchpoints. */
8603
8604 static int
8605 breakpoint_hit_catch_syscall (const struct bp_location *bl,
8606 struct address_space *aspace, CORE_ADDR bp_addr,
8607 const struct target_waitstatus *ws)
8608 {
8609 /* We must check if we are catching specific syscalls in this
8610 breakpoint. If we are, then we must guarantee that the called
8611 syscall is the same syscall we are catching. */
8612 int syscall_number = 0;
8613 const struct syscall_catchpoint *c
8614 = (const struct syscall_catchpoint *) bl->owner;
8615
8616 if (ws->kind != TARGET_WAITKIND_SYSCALL_ENTRY
8617 && ws->kind != TARGET_WAITKIND_SYSCALL_RETURN)
8618 return 0;
8619
8620 syscall_number = ws->value.syscall_number;
8621
8622 /* Now, checking if the syscall is the same. */
8623 if (c->syscalls_to_be_caught)
8624 {
8625 int i, iter;
8626
8627 for (i = 0;
8628 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
8629 i++)
8630 if (syscall_number == iter)
8631 return 1;
8632
8633 return 0;
8634 }
8635
8636 return 1;
8637 }
8638
8639 /* Implement the "print_it" breakpoint_ops method for syscall
8640 catchpoints. */
8641
8642 static enum print_stop_action
8643 print_it_catch_syscall (bpstat bs)
8644 {
8645 struct ui_out *uiout = current_uiout;
8646 struct breakpoint *b = bs->breakpoint_at;
8647 /* These are needed because we want to know in which state a
8648 syscall is. It can be in the TARGET_WAITKIND_SYSCALL_ENTRY
8649 or TARGET_WAITKIND_SYSCALL_RETURN, and depending on it we
8650 must print "called syscall" or "returned from syscall". */
8651 ptid_t ptid;
8652 struct target_waitstatus last;
8653 struct syscall s;
8654 struct gdbarch *gdbarch = bs->bp_location_at->gdbarch;
8655
8656 get_last_target_status (&ptid, &last);
8657
8658 get_syscall_by_number (gdbarch, last.value.syscall_number, &s);
8659
8660 annotate_catchpoint (b->number);
8661
8662 if (b->disposition == disp_del)
8663 ui_out_text (uiout, "\nTemporary catchpoint ");
8664 else
8665 ui_out_text (uiout, "\nCatchpoint ");
8666 if (ui_out_is_mi_like_p (uiout))
8667 {
8668 ui_out_field_string (uiout, "reason",
8669 async_reason_lookup (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY
8670 ? EXEC_ASYNC_SYSCALL_ENTRY
8671 : EXEC_ASYNC_SYSCALL_RETURN));
8672 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8673 }
8674 ui_out_field_int (uiout, "bkptno", b->number);
8675
8676 if (last.kind == TARGET_WAITKIND_SYSCALL_ENTRY)
8677 ui_out_text (uiout, " (call to syscall ");
8678 else
8679 ui_out_text (uiout, " (returned from syscall ");
8680
8681 if (s.name == NULL || ui_out_is_mi_like_p (uiout))
8682 ui_out_field_int (uiout, "syscall-number", last.value.syscall_number);
8683 if (s.name != NULL)
8684 ui_out_field_string (uiout, "syscall-name", s.name);
8685
8686 ui_out_text (uiout, "), ");
8687
8688 return PRINT_SRC_AND_LOC;
8689 }
8690
8691 /* Implement the "print_one" breakpoint_ops method for syscall
8692 catchpoints. */
8693
8694 static void
8695 print_one_catch_syscall (struct breakpoint *b,
8696 struct bp_location **last_loc)
8697 {
8698 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8699 struct value_print_options opts;
8700 struct ui_out *uiout = current_uiout;
8701 struct gdbarch *gdbarch = b->loc->gdbarch;
8702
8703 get_user_print_options (&opts);
8704 /* Field 4, the address, is omitted (which makes the columns not
8705 line up too nicely with the headers, but the effect is relatively
8706 readable). */
8707 if (opts.addressprint)
8708 ui_out_field_skip (uiout, "addr");
8709 annotate_field (5);
8710
8711 if (c->syscalls_to_be_caught
8712 && VEC_length (int, c->syscalls_to_be_caught) > 1)
8713 ui_out_text (uiout, "syscalls \"");
8714 else
8715 ui_out_text (uiout, "syscall \"");
8716
8717 if (c->syscalls_to_be_caught)
8718 {
8719 int i, iter;
8720 char *text = xstrprintf ("%s", "");
8721
8722 for (i = 0;
8723 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
8724 i++)
8725 {
8726 char *x = text;
8727 struct syscall s;
8728 get_syscall_by_number (gdbarch, iter, &s);
8729
8730 if (s.name != NULL)
8731 text = xstrprintf ("%s%s, ", text, s.name);
8732 else
8733 text = xstrprintf ("%s%d, ", text, iter);
8734
8735 /* We have to xfree the last 'text' (now stored at 'x')
8736 because xstrprintf dynamically allocates new space for it
8737 on every call. */
8738 xfree (x);
8739 }
8740 /* Remove the last comma. */
8741 text[strlen (text) - 2] = '\0';
8742 ui_out_field_string (uiout, "what", text);
8743 }
8744 else
8745 ui_out_field_string (uiout, "what", "<any syscall>");
8746 ui_out_text (uiout, "\" ");
8747
8748 if (ui_out_is_mi_like_p (uiout))
8749 ui_out_field_string (uiout, "catch-type", "syscall");
8750 }
8751
8752 /* Implement the "print_mention" breakpoint_ops method for syscall
8753 catchpoints. */
8754
8755 static void
8756 print_mention_catch_syscall (struct breakpoint *b)
8757 {
8758 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8759 struct gdbarch *gdbarch = b->loc->gdbarch;
8760
8761 if (c->syscalls_to_be_caught)
8762 {
8763 int i, iter;
8764
8765 if (VEC_length (int, c->syscalls_to_be_caught) > 1)
8766 printf_filtered (_("Catchpoint %d (syscalls"), b->number);
8767 else
8768 printf_filtered (_("Catchpoint %d (syscall"), b->number);
8769
8770 for (i = 0;
8771 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
8772 i++)
8773 {
8774 struct syscall s;
8775 get_syscall_by_number (gdbarch, iter, &s);
8776
8777 if (s.name)
8778 printf_filtered (" '%s' [%d]", s.name, s.number);
8779 else
8780 printf_filtered (" %d", s.number);
8781 }
8782 printf_filtered (")");
8783 }
8784 else
8785 printf_filtered (_("Catchpoint %d (any syscall)"),
8786 b->number);
8787 }
8788
8789 /* Implement the "print_recreate" breakpoint_ops method for syscall
8790 catchpoints. */
8791
8792 static void
8793 print_recreate_catch_syscall (struct breakpoint *b, struct ui_file *fp)
8794 {
8795 struct syscall_catchpoint *c = (struct syscall_catchpoint *) b;
8796 struct gdbarch *gdbarch = b->loc->gdbarch;
8797
8798 fprintf_unfiltered (fp, "catch syscall");
8799
8800 if (c->syscalls_to_be_caught)
8801 {
8802 int i, iter;
8803
8804 for (i = 0;
8805 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
8806 i++)
8807 {
8808 struct syscall s;
8809
8810 get_syscall_by_number (gdbarch, iter, &s);
8811 if (s.name)
8812 fprintf_unfiltered (fp, " %s", s.name);
8813 else
8814 fprintf_unfiltered (fp, " %d", s.number);
8815 }
8816 }
8817 print_recreate_thread (b, fp);
8818 }
8819
8820 /* The breakpoint_ops structure to be used in syscall catchpoints. */
8821
8822 static struct breakpoint_ops catch_syscall_breakpoint_ops;
8823
8824 /* Returns non-zero if 'b' is a syscall catchpoint. */
8825
8826 static int
8827 syscall_catchpoint_p (struct breakpoint *b)
8828 {
8829 return (b->ops == &catch_syscall_breakpoint_ops);
8830 }
8831
8832 /* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8833 is non-zero, then make the breakpoint temporary. If COND_STRING is
8834 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8835 the breakpoint_ops structure associated to the catchpoint. */
8836
8837 void
8838 init_catchpoint (struct breakpoint *b,
8839 struct gdbarch *gdbarch, int tempflag,
8840 char *cond_string,
8841 const struct breakpoint_ops *ops)
8842 {
8843 struct symtab_and_line sal;
8844
8845 init_sal (&sal);
8846 sal.pspace = current_program_space;
8847
8848 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
8849
8850 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
8851 b->disposition = tempflag ? disp_del : disp_donttouch;
8852 }
8853
8854 void
8855 install_breakpoint (int internal, struct breakpoint *b, int update_gll)
8856 {
8857 add_to_breakpoint_chain (b);
8858 set_breakpoint_number (internal, b);
8859 if (is_tracepoint (b))
8860 set_tracepoint_count (breakpoint_count);
8861 if (!internal)
8862 mention (b);
8863 observer_notify_breakpoint_created (b);
8864
8865 if (update_gll)
8866 update_global_location_list (UGLL_MAY_INSERT);
8867 }
8868
8869 static void
8870 create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8871 int tempflag, char *cond_string,
8872 const struct breakpoint_ops *ops)
8873 {
8874 struct fork_catchpoint *c = XNEW (struct fork_catchpoint);
8875
8876 init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
8877
8878 c->forked_inferior_pid = null_ptid;
8879
8880 install_breakpoint (0, &c->base, 1);
8881 }
8882
8883 /* Exec catchpoints. */
8884
8885 /* An instance of this type is used to represent an exec catchpoint.
8886 It includes a "struct breakpoint" as a kind of base class; users
8887 downcast to "struct breakpoint *" when needed. A breakpoint is
8888 really of this type iff its ops pointer points to
8889 CATCH_EXEC_BREAKPOINT_OPS. */
8890
8891 struct exec_catchpoint
8892 {
8893 /* The base class. */
8894 struct breakpoint base;
8895
8896 /* Filename of a program whose exec triggered this catchpoint.
8897 This field is only valid immediately after this catchpoint has
8898 triggered. */
8899 char *exec_pathname;
8900 };
8901
8902 /* Implement the "dtor" breakpoint_ops method for exec
8903 catchpoints. */
8904
8905 static void
8906 dtor_catch_exec (struct breakpoint *b)
8907 {
8908 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8909
8910 xfree (c->exec_pathname);
8911
8912 base_breakpoint_ops.dtor (b);
8913 }
8914
8915 static int
8916 insert_catch_exec (struct bp_location *bl)
8917 {
8918 return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
8919 }
8920
8921 static int
8922 remove_catch_exec (struct bp_location *bl)
8923 {
8924 return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
8925 }
8926
8927 static int
8928 breakpoint_hit_catch_exec (const struct bp_location *bl,
8929 struct address_space *aspace, CORE_ADDR bp_addr,
8930 const struct target_waitstatus *ws)
8931 {
8932 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8933
8934 if (ws->kind != TARGET_WAITKIND_EXECD)
8935 return 0;
8936
8937 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8938 return 1;
8939 }
8940
8941 static enum print_stop_action
8942 print_it_catch_exec (bpstat bs)
8943 {
8944 struct ui_out *uiout = current_uiout;
8945 struct breakpoint *b = bs->breakpoint_at;
8946 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8947
8948 annotate_catchpoint (b->number);
8949 if (b->disposition == disp_del)
8950 ui_out_text (uiout, "\nTemporary catchpoint ");
8951 else
8952 ui_out_text (uiout, "\nCatchpoint ");
8953 if (ui_out_is_mi_like_p (uiout))
8954 {
8955 ui_out_field_string (uiout, "reason",
8956 async_reason_lookup (EXEC_ASYNC_EXEC));
8957 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8958 }
8959 ui_out_field_int (uiout, "bkptno", b->number);
8960 ui_out_text (uiout, " (exec'd ");
8961 ui_out_field_string (uiout, "new-exec", c->exec_pathname);
8962 ui_out_text (uiout, "), ");
8963
8964 return PRINT_SRC_AND_LOC;
8965 }
8966
8967 static void
8968 print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
8969 {
8970 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8971 struct value_print_options opts;
8972 struct ui_out *uiout = current_uiout;
8973
8974 get_user_print_options (&opts);
8975
8976 /* Field 4, the address, is omitted (which makes the columns
8977 not line up too nicely with the headers, but the effect
8978 is relatively readable). */
8979 if (opts.addressprint)
8980 ui_out_field_skip (uiout, "addr");
8981 annotate_field (5);
8982 ui_out_text (uiout, "exec");
8983 if (c->exec_pathname != NULL)
8984 {
8985 ui_out_text (uiout, ", program \"");
8986 ui_out_field_string (uiout, "what", c->exec_pathname);
8987 ui_out_text (uiout, "\" ");
8988 }
8989
8990 if (ui_out_is_mi_like_p (uiout))
8991 ui_out_field_string (uiout, "catch-type", "exec");
8992 }
8993
8994 static void
8995 print_mention_catch_exec (struct breakpoint *b)
8996 {
8997 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8998 }
8999
9000 /* Implement the "print_recreate" breakpoint_ops method for exec
9001 catchpoints. */
9002
9003 static void
9004 print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
9005 {
9006 fprintf_unfiltered (fp, "catch exec");
9007 print_recreate_thread (b, fp);
9008 }
9009
9010 static struct breakpoint_ops catch_exec_breakpoint_ops;
9011
9012 static void
9013 create_syscall_event_catchpoint (int tempflag, VEC(int) *filter,
9014 const struct breakpoint_ops *ops)
9015 {
9016 struct syscall_catchpoint *c;
9017 struct gdbarch *gdbarch = get_current_arch ();
9018
9019 c = XNEW (struct syscall_catchpoint);
9020 init_catchpoint (&c->base, gdbarch, tempflag, NULL, ops);
9021 c->syscalls_to_be_caught = filter;
9022
9023 install_breakpoint (0, &c->base, 1);
9024 }
9025
9026 static int
9027 hw_breakpoint_used_count (void)
9028 {
9029 int i = 0;
9030 struct breakpoint *b;
9031 struct bp_location *bl;
9032
9033 ALL_BREAKPOINTS (b)
9034 {
9035 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
9036 for (bl = b->loc; bl; bl = bl->next)
9037 {
9038 /* Special types of hardware breakpoints may use more than
9039 one register. */
9040 i += b->ops->resources_needed (bl);
9041 }
9042 }
9043
9044 return i;
9045 }
9046
9047 /* Returns the resources B would use if it were a hardware
9048 watchpoint. */
9049
9050 static int
9051 hw_watchpoint_use_count (struct breakpoint *b)
9052 {
9053 int i = 0;
9054 struct bp_location *bl;
9055
9056 if (!breakpoint_enabled (b))
9057 return 0;
9058
9059 for (bl = b->loc; bl; bl = bl->next)
9060 {
9061 /* Special types of hardware watchpoints may use more than
9062 one register. */
9063 i += b->ops->resources_needed (bl);
9064 }
9065
9066 return i;
9067 }
9068
9069 /* Returns the sum the used resources of all hardware watchpoints of
9070 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
9071 the sum of the used resources of all hardware watchpoints of other
9072 types _not_ TYPE. */
9073
9074 static int
9075 hw_watchpoint_used_count_others (struct breakpoint *except,
9076 enum bptype type, int *other_type_used)
9077 {
9078 int i = 0;
9079 struct breakpoint *b;
9080
9081 *other_type_used = 0;
9082 ALL_BREAKPOINTS (b)
9083 {
9084 if (b == except)
9085 continue;
9086 if (!breakpoint_enabled (b))
9087 continue;
9088
9089 if (b->type == type)
9090 i += hw_watchpoint_use_count (b);
9091 else if (is_hardware_watchpoint (b))
9092 *other_type_used = 1;
9093 }
9094
9095 return i;
9096 }
9097
9098 void
9099 disable_watchpoints_before_interactive_call_start (void)
9100 {
9101 struct breakpoint *b;
9102
9103 ALL_BREAKPOINTS (b)
9104 {
9105 if (is_watchpoint (b) && breakpoint_enabled (b))
9106 {
9107 b->enable_state = bp_call_disabled;
9108 update_global_location_list (UGLL_DONT_INSERT);
9109 }
9110 }
9111 }
9112
9113 void
9114 enable_watchpoints_after_interactive_call_stop (void)
9115 {
9116 struct breakpoint *b;
9117
9118 ALL_BREAKPOINTS (b)
9119 {
9120 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
9121 {
9122 b->enable_state = bp_enabled;
9123 update_global_location_list (UGLL_MAY_INSERT);
9124 }
9125 }
9126 }
9127
9128 void
9129 disable_breakpoints_before_startup (void)
9130 {
9131 current_program_space->executing_startup = 1;
9132 update_global_location_list (UGLL_DONT_INSERT);
9133 }
9134
9135 void
9136 enable_breakpoints_after_startup (void)
9137 {
9138 current_program_space->executing_startup = 0;
9139 breakpoint_re_set ();
9140 }
9141
9142 /* Create a new single-step breakpoint for thread THREAD, with no
9143 locations. */
9144
9145 static struct breakpoint *
9146 new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
9147 {
9148 struct breakpoint *b = XNEW (struct breakpoint);
9149
9150 init_raw_breakpoint_without_location (b, gdbarch, bp_single_step,
9151 &momentary_breakpoint_ops);
9152
9153 b->disposition = disp_donttouch;
9154 b->frame_id = null_frame_id;
9155
9156 b->thread = thread;
9157 gdb_assert (b->thread != 0);
9158
9159 add_to_breakpoint_chain (b);
9160
9161 return b;
9162 }
9163
9164 /* Set a momentary breakpoint of type TYPE at address specified by
9165 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
9166 frame. */
9167
9168 struct breakpoint *
9169 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
9170 struct frame_id frame_id, enum bptype type)
9171 {
9172 struct breakpoint *b;
9173
9174 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
9175 tail-called one. */
9176 gdb_assert (!frame_id_artificial_p (frame_id));
9177
9178 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
9179 b->enable_state = bp_enabled;
9180 b->disposition = disp_donttouch;
9181 b->frame_id = frame_id;
9182
9183 /* If we're debugging a multi-threaded program, then we want
9184 momentary breakpoints to be active in only a single thread of
9185 control. */
9186 if (in_thread_list (inferior_ptid))
9187 b->thread = pid_to_thread_id (inferior_ptid);
9188
9189 update_global_location_list_nothrow (UGLL_MAY_INSERT);
9190
9191 return b;
9192 }
9193
9194 /* Make a momentary breakpoint based on the master breakpoint ORIG.
9195 The new breakpoint will have type TYPE, use OPS as its
9196 breakpoint_ops, and will set enabled to LOC_ENABLED. */
9197
9198 static struct breakpoint *
9199 momentary_breakpoint_from_master (struct breakpoint *orig,
9200 enum bptype type,
9201 const struct breakpoint_ops *ops,
9202 int loc_enabled)
9203 {
9204 struct breakpoint *copy;
9205
9206 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
9207 copy->loc = allocate_bp_location (copy);
9208 set_breakpoint_location_function (copy->loc, 1);
9209
9210 copy->loc->gdbarch = orig->loc->gdbarch;
9211 copy->loc->requested_address = orig->loc->requested_address;
9212 copy->loc->address = orig->loc->address;
9213 copy->loc->section = orig->loc->section;
9214 copy->loc->pspace = orig->loc->pspace;
9215 copy->loc->probe = orig->loc->probe;
9216 copy->loc->line_number = orig->loc->line_number;
9217 copy->loc->symtab = orig->loc->symtab;
9218 copy->loc->enabled = loc_enabled;
9219 copy->frame_id = orig->frame_id;
9220 copy->thread = orig->thread;
9221 copy->pspace = orig->pspace;
9222
9223 copy->enable_state = bp_enabled;
9224 copy->disposition = disp_donttouch;
9225 copy->number = internal_breakpoint_number--;
9226
9227 update_global_location_list_nothrow (UGLL_DONT_INSERT);
9228 return copy;
9229 }
9230
9231 /* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
9232 ORIG is NULL. */
9233
9234 struct breakpoint *
9235 clone_momentary_breakpoint (struct breakpoint *orig)
9236 {
9237 /* If there's nothing to clone, then return nothing. */
9238 if (orig == NULL)
9239 return NULL;
9240
9241 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
9242 }
9243
9244 struct breakpoint *
9245 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
9246 enum bptype type)
9247 {
9248 struct symtab_and_line sal;
9249
9250 sal = find_pc_line (pc, 0);
9251 sal.pc = pc;
9252 sal.section = find_pc_overlay (pc);
9253 sal.explicit_pc = 1;
9254
9255 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
9256 }
9257 \f
9258
9259 /* Tell the user we have just set a breakpoint B. */
9260
9261 static void
9262 mention (struct breakpoint *b)
9263 {
9264 b->ops->print_mention (b);
9265 if (ui_out_is_mi_like_p (current_uiout))
9266 return;
9267 printf_filtered ("\n");
9268 }
9269 \f
9270
9271 static int bp_loc_is_permanent (struct bp_location *loc);
9272
9273 static struct bp_location *
9274 add_location_to_breakpoint (struct breakpoint *b,
9275 const struct symtab_and_line *sal)
9276 {
9277 struct bp_location *loc, **tmp;
9278 CORE_ADDR adjusted_address;
9279 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
9280
9281 if (loc_gdbarch == NULL)
9282 loc_gdbarch = b->gdbarch;
9283
9284 /* Adjust the breakpoint's address prior to allocating a location.
9285 Once we call allocate_bp_location(), that mostly uninitialized
9286 location will be placed on the location chain. Adjustment of the
9287 breakpoint may cause target_read_memory() to be called and we do
9288 not want its scan of the location chain to find a breakpoint and
9289 location that's only been partially initialized. */
9290 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
9291 sal->pc, b->type);
9292
9293 /* Sort the locations by their ADDRESS. */
9294 loc = allocate_bp_location (b);
9295 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
9296 tmp = &((*tmp)->next))
9297 ;
9298 loc->next = *tmp;
9299 *tmp = loc;
9300
9301 loc->requested_address = sal->pc;
9302 loc->address = adjusted_address;
9303 loc->pspace = sal->pspace;
9304 loc->probe.probe = sal->probe;
9305 loc->probe.objfile = sal->objfile;
9306 gdb_assert (loc->pspace != NULL);
9307 loc->section = sal->section;
9308 loc->gdbarch = loc_gdbarch;
9309 loc->line_number = sal->line;
9310 loc->symtab = sal->symtab;
9311
9312 set_breakpoint_location_function (loc,
9313 sal->explicit_pc || sal->explicit_line);
9314
9315 if (bp_loc_is_permanent (loc))
9316 {
9317 loc->inserted = 1;
9318 loc->permanent = 1;
9319 }
9320
9321 return loc;
9322 }
9323 \f
9324
9325 /* See breakpoint.h. */
9326
9327 int
9328 program_breakpoint_here_p (struct gdbarch *gdbarch, CORE_ADDR address)
9329 {
9330 int len;
9331 CORE_ADDR addr;
9332 const gdb_byte *bpoint;
9333 gdb_byte *target_mem;
9334 struct cleanup *cleanup;
9335 int retval = 0;
9336
9337 addr = address;
9338 bpoint = gdbarch_breakpoint_from_pc (gdbarch, &addr, &len);
9339
9340 /* Software breakpoints unsupported? */
9341 if (bpoint == NULL)
9342 return 0;
9343
9344 target_mem = alloca (len);
9345
9346 /* Enable the automatic memory restoration from breakpoints while
9347 we read the memory. Otherwise we could say about our temporary
9348 breakpoints they are permanent. */
9349 cleanup = make_show_memory_breakpoints_cleanup (0);
9350
9351 if (target_read_memory (address, target_mem, len) == 0
9352 && memcmp (target_mem, bpoint, len) == 0)
9353 retval = 1;
9354
9355 do_cleanups (cleanup);
9356
9357 return retval;
9358 }
9359
9360 /* Return 1 if LOC is pointing to a permanent breakpoint,
9361 return 0 otherwise. */
9362
9363 static int
9364 bp_loc_is_permanent (struct bp_location *loc)
9365 {
9366 struct cleanup *cleanup;
9367 int retval;
9368
9369 gdb_assert (loc != NULL);
9370
9371 /* bp_call_dummy breakpoint locations are usually memory locations
9372 where GDB just wrote a breakpoint instruction, making it look
9373 as if there is a permanent breakpoint at that location. Considering
9374 it permanent makes GDB rely on that breakpoint instruction to stop
9375 the program, thus removing the need to insert its own breakpoint
9376 there. This is normally expected to work, except that some versions
9377 of QEMU (Eg: QEMU 2.0.0 for SPARC) just report a fatal problem (Trap
9378 0x02 while interrupts disabled, Error state) instead of reporting
9379 a SIGTRAP. QEMU should probably be fixed, but in the interest of
9380 compatibility with versions that behave this way, we always consider
9381 bp_call_dummy breakpoint locations as non-permanent. */
9382 if (loc->owner->type == bp_call_dummy)
9383 return 0;
9384
9385 cleanup = save_current_space_and_thread ();
9386 switch_to_program_space_and_thread (loc->pspace);
9387
9388 retval = program_breakpoint_here_p (loc->gdbarch, loc->address);
9389
9390 do_cleanups (cleanup);
9391
9392 return retval;
9393 }
9394
9395 /* Build a command list for the dprintf corresponding to the current
9396 settings of the dprintf style options. */
9397
9398 static void
9399 update_dprintf_command_list (struct breakpoint *b)
9400 {
9401 char *dprintf_args = b->extra_string;
9402 char *printf_line = NULL;
9403
9404 if (!dprintf_args)
9405 return;
9406
9407 dprintf_args = skip_spaces (dprintf_args);
9408
9409 /* Allow a comma, as it may have terminated a location, but don't
9410 insist on it. */
9411 if (*dprintf_args == ',')
9412 ++dprintf_args;
9413 dprintf_args = skip_spaces (dprintf_args);
9414
9415 if (*dprintf_args != '"')
9416 error (_("Bad format string, missing '\"'."));
9417
9418 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
9419 printf_line = xstrprintf ("printf %s", dprintf_args);
9420 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
9421 {
9422 if (!dprintf_function)
9423 error (_("No function supplied for dprintf call"));
9424
9425 if (dprintf_channel && strlen (dprintf_channel) > 0)
9426 printf_line = xstrprintf ("call (void) %s (%s,%s)",
9427 dprintf_function,
9428 dprintf_channel,
9429 dprintf_args);
9430 else
9431 printf_line = xstrprintf ("call (void) %s (%s)",
9432 dprintf_function,
9433 dprintf_args);
9434 }
9435 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
9436 {
9437 if (target_can_run_breakpoint_commands ())
9438 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
9439 else
9440 {
9441 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
9442 printf_line = xstrprintf ("printf %s", dprintf_args);
9443 }
9444 }
9445 else
9446 internal_error (__FILE__, __LINE__,
9447 _("Invalid dprintf style."));
9448
9449 gdb_assert (printf_line != NULL);
9450 /* Manufacture a printf sequence. */
9451 {
9452 struct command_line *printf_cmd_line
9453 = xmalloc (sizeof (struct command_line));
9454
9455 printf_cmd_line = xmalloc (sizeof (struct command_line));
9456 printf_cmd_line->control_type = simple_control;
9457 printf_cmd_line->body_count = 0;
9458 printf_cmd_line->body_list = NULL;
9459 printf_cmd_line->next = NULL;
9460 printf_cmd_line->line = printf_line;
9461
9462 breakpoint_set_commands (b, printf_cmd_line);
9463 }
9464 }
9465
9466 /* Update all dprintf commands, making their command lists reflect
9467 current style settings. */
9468
9469 static void
9470 update_dprintf_commands (char *args, int from_tty,
9471 struct cmd_list_element *c)
9472 {
9473 struct breakpoint *b;
9474
9475 ALL_BREAKPOINTS (b)
9476 {
9477 if (b->type == bp_dprintf)
9478 update_dprintf_command_list (b);
9479 }
9480 }
9481
9482 /* Create a breakpoint with SAL as location. Use ADDR_STRING
9483 as textual description of the location, and COND_STRING
9484 as condition expression. */
9485
9486 static void
9487 init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
9488 struct symtabs_and_lines sals, char *addr_string,
9489 char *filter, char *cond_string,
9490 char *extra_string,
9491 enum bptype type, enum bpdisp disposition,
9492 int thread, int task, int ignore_count,
9493 const struct breakpoint_ops *ops, int from_tty,
9494 int enabled, int internal, unsigned flags,
9495 int display_canonical)
9496 {
9497 int i;
9498
9499 if (type == bp_hardware_breakpoint)
9500 {
9501 int target_resources_ok;
9502
9503 i = hw_breakpoint_used_count ();
9504 target_resources_ok =
9505 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9506 i + 1, 0);
9507 if (target_resources_ok == 0)
9508 error (_("No hardware breakpoint support in the target."));
9509 else if (target_resources_ok < 0)
9510 error (_("Hardware breakpoints used exceeds limit."));
9511 }
9512
9513 gdb_assert (sals.nelts > 0);
9514
9515 for (i = 0; i < sals.nelts; ++i)
9516 {
9517 struct symtab_and_line sal = sals.sals[i];
9518 struct bp_location *loc;
9519
9520 if (from_tty)
9521 {
9522 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9523 if (!loc_gdbarch)
9524 loc_gdbarch = gdbarch;
9525
9526 describe_other_breakpoints (loc_gdbarch,
9527 sal.pspace, sal.pc, sal.section, thread);
9528 }
9529
9530 if (i == 0)
9531 {
9532 init_raw_breakpoint (b, gdbarch, sal, type, ops);
9533 b->thread = thread;
9534 b->task = task;
9535
9536 b->cond_string = cond_string;
9537 b->extra_string = extra_string;
9538 b->ignore_count = ignore_count;
9539 b->enable_state = enabled ? bp_enabled : bp_disabled;
9540 b->disposition = disposition;
9541
9542 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9543 b->loc->inserted = 1;
9544
9545 if (type == bp_static_tracepoint)
9546 {
9547 struct tracepoint *t = (struct tracepoint *) b;
9548 struct static_tracepoint_marker marker;
9549
9550 if (strace_marker_p (b))
9551 {
9552 /* We already know the marker exists, otherwise, we
9553 wouldn't see a sal for it. */
9554 char *p = &addr_string[3];
9555 char *endp;
9556 char *marker_str;
9557
9558 p = skip_spaces (p);
9559
9560 endp = skip_to_space (p);
9561
9562 marker_str = savestring (p, endp - p);
9563 t->static_trace_marker_id = marker_str;
9564
9565 printf_filtered (_("Probed static tracepoint "
9566 "marker \"%s\"\n"),
9567 t->static_trace_marker_id);
9568 }
9569 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
9570 {
9571 t->static_trace_marker_id = xstrdup (marker.str_id);
9572 release_static_tracepoint_marker (&marker);
9573
9574 printf_filtered (_("Probed static tracepoint "
9575 "marker \"%s\"\n"),
9576 t->static_trace_marker_id);
9577 }
9578 else
9579 warning (_("Couldn't determine the static "
9580 "tracepoint marker to probe"));
9581 }
9582
9583 loc = b->loc;
9584 }
9585 else
9586 {
9587 loc = add_location_to_breakpoint (b, &sal);
9588 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9589 loc->inserted = 1;
9590 }
9591
9592 if (b->cond_string)
9593 {
9594 const char *arg = b->cond_string;
9595
9596 loc->cond = parse_exp_1 (&arg, loc->address,
9597 block_for_pc (loc->address), 0);
9598 if (*arg)
9599 error (_("Garbage '%s' follows condition"), arg);
9600 }
9601
9602 /* Dynamic printf requires and uses additional arguments on the
9603 command line, otherwise it's an error. */
9604 if (type == bp_dprintf)
9605 {
9606 if (b->extra_string)
9607 update_dprintf_command_list (b);
9608 else
9609 error (_("Format string required"));
9610 }
9611 else if (b->extra_string)
9612 error (_("Garbage '%s' at end of command"), b->extra_string);
9613 }
9614
9615 b->display_canonical = display_canonical;
9616 if (addr_string)
9617 b->addr_string = addr_string;
9618 else
9619 /* addr_string has to be used or breakpoint_re_set will delete
9620 me. */
9621 b->addr_string
9622 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
9623 b->filter = filter;
9624 }
9625
9626 static void
9627 create_breakpoint_sal (struct gdbarch *gdbarch,
9628 struct symtabs_and_lines sals, char *addr_string,
9629 char *filter, char *cond_string,
9630 char *extra_string,
9631 enum bptype type, enum bpdisp disposition,
9632 int thread, int task, int ignore_count,
9633 const struct breakpoint_ops *ops, int from_tty,
9634 int enabled, int internal, unsigned flags,
9635 int display_canonical)
9636 {
9637 struct breakpoint *b;
9638 struct cleanup *old_chain;
9639
9640 if (is_tracepoint_type (type))
9641 {
9642 struct tracepoint *t;
9643
9644 t = XCNEW (struct tracepoint);
9645 b = &t->base;
9646 }
9647 else
9648 b = XNEW (struct breakpoint);
9649
9650 old_chain = make_cleanup (xfree, b);
9651
9652 init_breakpoint_sal (b, gdbarch,
9653 sals, addr_string,
9654 filter, cond_string, extra_string,
9655 type, disposition,
9656 thread, task, ignore_count,
9657 ops, from_tty,
9658 enabled, internal, flags,
9659 display_canonical);
9660 discard_cleanups (old_chain);
9661
9662 install_breakpoint (internal, b, 0);
9663 }
9664
9665 /* Add SALS.nelts breakpoints to the breakpoint table. For each
9666 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9667 value. COND_STRING, if not NULL, specified the condition to be
9668 used for all breakpoints. Essentially the only case where
9669 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9670 function. In that case, it's still not possible to specify
9671 separate conditions for different overloaded functions, so
9672 we take just a single condition string.
9673
9674 NOTE: If the function succeeds, the caller is expected to cleanup
9675 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
9676 array contents). If the function fails (error() is called), the
9677 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
9678 COND and SALS arrays and each of those arrays contents. */
9679
9680 static void
9681 create_breakpoints_sal (struct gdbarch *gdbarch,
9682 struct linespec_result *canonical,
9683 char *cond_string, char *extra_string,
9684 enum bptype type, enum bpdisp disposition,
9685 int thread, int task, int ignore_count,
9686 const struct breakpoint_ops *ops, int from_tty,
9687 int enabled, int internal, unsigned flags)
9688 {
9689 int i;
9690 struct linespec_sals *lsal;
9691
9692 if (canonical->pre_expanded)
9693 gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
9694
9695 for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
9696 {
9697 /* Note that 'addr_string' can be NULL in the case of a plain
9698 'break', without arguments. */
9699 char *addr_string = (canonical->addr_string
9700 ? xstrdup (canonical->addr_string)
9701 : NULL);
9702 char *filter_string = lsal->canonical ? xstrdup (lsal->canonical) : NULL;
9703 struct cleanup *inner = make_cleanup (xfree, addr_string);
9704
9705 make_cleanup (xfree, filter_string);
9706 create_breakpoint_sal (gdbarch, lsal->sals,
9707 addr_string,
9708 filter_string,
9709 cond_string, extra_string,
9710 type, disposition,
9711 thread, task, ignore_count, ops,
9712 from_tty, enabled, internal, flags,
9713 canonical->special_display);
9714 discard_cleanups (inner);
9715 }
9716 }
9717
9718 /* Parse ADDRESS which is assumed to be a SAL specification possibly
9719 followed by conditionals. On return, SALS contains an array of SAL
9720 addresses found. ADDR_STRING contains a vector of (canonical)
9721 address strings. ADDRESS points to the end of the SAL.
9722
9723 The array and the line spec strings are allocated on the heap, it is
9724 the caller's responsibility to free them. */
9725
9726 static void
9727 parse_breakpoint_sals (char **address,
9728 struct linespec_result *canonical)
9729 {
9730 /* If no arg given, or if first arg is 'if ', use the default
9731 breakpoint. */
9732 if ((*address) == NULL
9733 || (startswith ((*address), "if") && isspace ((*address)[2])))
9734 {
9735 /* The last displayed codepoint, if it's valid, is our default breakpoint
9736 address. */
9737 if (last_displayed_sal_is_valid ())
9738 {
9739 struct linespec_sals lsal;
9740 struct symtab_and_line sal;
9741 CORE_ADDR pc;
9742
9743 init_sal (&sal); /* Initialize to zeroes. */
9744 lsal.sals.sals = (struct symtab_and_line *)
9745 xmalloc (sizeof (struct symtab_and_line));
9746
9747 /* Set sal's pspace, pc, symtab, and line to the values
9748 corresponding to the last call to print_frame_info.
9749 Be sure to reinitialize LINE with NOTCURRENT == 0
9750 as the breakpoint line number is inappropriate otherwise.
9751 find_pc_line would adjust PC, re-set it back. */
9752 get_last_displayed_sal (&sal);
9753 pc = sal.pc;
9754 sal = find_pc_line (pc, 0);
9755
9756 /* "break" without arguments is equivalent to "break *PC"
9757 where PC is the last displayed codepoint's address. So
9758 make sure to set sal.explicit_pc to prevent GDB from
9759 trying to expand the list of sals to include all other
9760 instances with the same symtab and line. */
9761 sal.pc = pc;
9762 sal.explicit_pc = 1;
9763
9764 lsal.sals.sals[0] = sal;
9765 lsal.sals.nelts = 1;
9766 lsal.canonical = NULL;
9767
9768 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
9769 }
9770 else
9771 error (_("No default breakpoint address now."));
9772 }
9773 else
9774 {
9775 struct symtab_and_line cursal = get_current_source_symtab_and_line ();
9776
9777 /* Force almost all breakpoints to be in terms of the
9778 current_source_symtab (which is decode_line_1's default).
9779 This should produce the results we want almost all of the
9780 time while leaving default_breakpoint_* alone.
9781
9782 ObjC: However, don't match an Objective-C method name which
9783 may have a '+' or '-' succeeded by a '['. */
9784 if (last_displayed_sal_is_valid ()
9785 && (!cursal.symtab
9786 || ((strchr ("+-", (*address)[0]) != NULL)
9787 && ((*address)[1] != '['))))
9788 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
9789 get_last_displayed_symtab (),
9790 get_last_displayed_line (),
9791 canonical, NULL, NULL);
9792 else
9793 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
9794 cursal.symtab, cursal.line, canonical, NULL, NULL);
9795 }
9796 }
9797
9798
9799 /* Convert each SAL into a real PC. Verify that the PC can be
9800 inserted as a breakpoint. If it can't throw an error. */
9801
9802 static void
9803 breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
9804 {
9805 int i;
9806
9807 for (i = 0; i < sals->nelts; i++)
9808 resolve_sal_pc (&sals->sals[i]);
9809 }
9810
9811 /* Fast tracepoints may have restrictions on valid locations. For
9812 instance, a fast tracepoint using a jump instead of a trap will
9813 likely have to overwrite more bytes than a trap would, and so can
9814 only be placed where the instruction is longer than the jump, or a
9815 multi-instruction sequence does not have a jump into the middle of
9816 it, etc. */
9817
9818 static void
9819 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9820 struct symtabs_and_lines *sals)
9821 {
9822 int i, rslt;
9823 struct symtab_and_line *sal;
9824 char *msg;
9825 struct cleanup *old_chain;
9826
9827 for (i = 0; i < sals->nelts; i++)
9828 {
9829 struct gdbarch *sarch;
9830
9831 sal = &sals->sals[i];
9832
9833 sarch = get_sal_arch (*sal);
9834 /* We fall back to GDBARCH if there is no architecture
9835 associated with SAL. */
9836 if (sarch == NULL)
9837 sarch = gdbarch;
9838 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc,
9839 NULL, &msg);
9840 old_chain = make_cleanup (xfree, msg);
9841
9842 if (!rslt)
9843 error (_("May not have a fast tracepoint at 0x%s%s"),
9844 paddress (sarch, sal->pc), (msg ? msg : ""));
9845
9846 do_cleanups (old_chain);
9847 }
9848 }
9849
9850 /* Issue an invalid thread ID error. */
9851
9852 static void ATTRIBUTE_NORETURN
9853 invalid_thread_id_error (int id)
9854 {
9855 error (_("Unknown thread %d."), id);
9856 }
9857
9858 /* Given TOK, a string specification of condition and thread, as
9859 accepted by the 'break' command, extract the condition
9860 string and thread number and set *COND_STRING and *THREAD.
9861 PC identifies the context at which the condition should be parsed.
9862 If no condition is found, *COND_STRING is set to NULL.
9863 If no thread is found, *THREAD is set to -1. */
9864
9865 static void
9866 find_condition_and_thread (const char *tok, CORE_ADDR pc,
9867 char **cond_string, int *thread, int *task,
9868 char **rest)
9869 {
9870 *cond_string = NULL;
9871 *thread = -1;
9872 *task = 0;
9873 *rest = NULL;
9874
9875 while (tok && *tok)
9876 {
9877 const char *end_tok;
9878 int toklen;
9879 const char *cond_start = NULL;
9880 const char *cond_end = NULL;
9881
9882 tok = skip_spaces_const (tok);
9883
9884 if ((*tok == '"' || *tok == ',') && rest)
9885 {
9886 *rest = savestring (tok, strlen (tok));
9887 return;
9888 }
9889
9890 end_tok = skip_to_space_const (tok);
9891
9892 toklen = end_tok - tok;
9893
9894 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9895 {
9896 struct expression *expr;
9897
9898 tok = cond_start = end_tok + 1;
9899 expr = parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
9900 xfree (expr);
9901 cond_end = tok;
9902 *cond_string = savestring (cond_start, cond_end - cond_start);
9903 }
9904 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9905 {
9906 char *tmptok;
9907
9908 tok = end_tok + 1;
9909 *thread = strtol (tok, &tmptok, 0);
9910 if (tok == tmptok)
9911 error (_("Junk after thread keyword."));
9912 if (!valid_thread_id (*thread))
9913 invalid_thread_id_error (*thread);
9914 tok = tmptok;
9915 }
9916 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9917 {
9918 char *tmptok;
9919
9920 tok = end_tok + 1;
9921 *task = strtol (tok, &tmptok, 0);
9922 if (tok == tmptok)
9923 error (_("Junk after task keyword."));
9924 if (!valid_task_id (*task))
9925 error (_("Unknown task %d."), *task);
9926 tok = tmptok;
9927 }
9928 else if (rest)
9929 {
9930 *rest = savestring (tok, strlen (tok));
9931 return;
9932 }
9933 else
9934 error (_("Junk at end of arguments."));
9935 }
9936 }
9937
9938 /* Decode a static tracepoint marker spec. */
9939
9940 static struct symtabs_and_lines
9941 decode_static_tracepoint_spec (char **arg_p)
9942 {
9943 VEC(static_tracepoint_marker_p) *markers = NULL;
9944 struct symtabs_and_lines sals;
9945 struct cleanup *old_chain;
9946 char *p = &(*arg_p)[3];
9947 char *endp;
9948 char *marker_str;
9949 int i;
9950
9951 p = skip_spaces (p);
9952
9953 endp = skip_to_space (p);
9954
9955 marker_str = savestring (p, endp - p);
9956 old_chain = make_cleanup (xfree, marker_str);
9957
9958 markers = target_static_tracepoint_markers_by_strid (marker_str);
9959 if (VEC_empty(static_tracepoint_marker_p, markers))
9960 error (_("No known static tracepoint marker named %s"), marker_str);
9961
9962 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
9963 sals.sals = xmalloc (sizeof *sals.sals * sals.nelts);
9964
9965 for (i = 0; i < sals.nelts; i++)
9966 {
9967 struct static_tracepoint_marker *marker;
9968
9969 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9970
9971 init_sal (&sals.sals[i]);
9972
9973 sals.sals[i] = find_pc_line (marker->address, 0);
9974 sals.sals[i].pc = marker->address;
9975
9976 release_static_tracepoint_marker (marker);
9977 }
9978
9979 do_cleanups (old_chain);
9980
9981 *arg_p = endp;
9982 return sals;
9983 }
9984
9985 /* Set a breakpoint. This function is shared between CLI and MI
9986 functions for setting a breakpoint. This function has two major
9987 modes of operations, selected by the PARSE_ARG parameter. If
9988 non-zero, the function will parse ARG, extracting location,
9989 condition, thread and extra string. Otherwise, ARG is just the
9990 breakpoint's location, with condition, thread, and extra string
9991 specified by the COND_STRING, THREAD and EXTRA_STRING parameters.
9992 If INTERNAL is non-zero, the breakpoint number will be allocated
9993 from the internal breakpoint count. Returns true if any breakpoint
9994 was created; false otherwise. */
9995
9996 int
9997 create_breakpoint (struct gdbarch *gdbarch,
9998 char *arg, char *cond_string,
9999 int thread, char *extra_string,
10000 int parse_arg,
10001 int tempflag, enum bptype type_wanted,
10002 int ignore_count,
10003 enum auto_boolean pending_break_support,
10004 const struct breakpoint_ops *ops,
10005 int from_tty, int enabled, int internal,
10006 unsigned flags)
10007 {
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
10022 {
10023 ops->create_sals_from_address (&arg, &canonical, type_wanted,
10024 addr_start, &copy_arg);
10025 }
10026 CATCH (e, RETURN_MASK_ERROR)
10027 {
10028 /* If caller is interested in rc value from parse, set
10029 value. */
10030 if (e.error == NOT_FOUND_ERROR)
10031 {
10032 /* If pending breakpoint support is turned off, throw
10033 error. */
10034
10035 if (pending_break_support == AUTO_BOOLEAN_FALSE)
10036 throw_exception (e);
10037
10038 exception_print (gdb_stderr, e);
10039
10040 /* If pending breakpoint support is auto query and the user
10041 selects no, then simply return the error code. */
10042 if (pending_break_support == AUTO_BOOLEAN_AUTO
10043 && !nquery (_("Make %s pending on future shared library load? "),
10044 bptype_string (type_wanted)))
10045 return 0;
10046
10047 /* At this point, either the user was queried about setting
10048 a pending breakpoint and selected yes, or pending
10049 breakpoint behavior is on and thus a pending breakpoint
10050 is defaulted on behalf of the user. */
10051 {
10052 struct linespec_sals lsal;
10053
10054 copy_arg = xstrdup (addr_start);
10055 lsal.canonical = xstrdup (copy_arg);
10056 lsal.sals.nelts = 1;
10057 lsal.sals.sals = XNEW (struct symtab_and_line);
10058 init_sal (&lsal.sals.sals[0]);
10059 pending = 1;
10060 VEC_safe_push (linespec_sals, canonical.sals, &lsal);
10061 }
10062 }
10063 else
10064 throw_exception (e);
10065 }
10066 END_CATCH
10067
10068 if (VEC_empty (linespec_sals, canonical.sals))
10069 return 0;
10070
10071 /* Create a chain of things that always need to be cleaned up. */
10072 old_chain = make_cleanup_destroy_linespec_result (&canonical);
10073
10074 /* ----------------------------- SNIP -----------------------------
10075 Anything added to the cleanup chain beyond this point is assumed
10076 to be part of a breakpoint. If the breakpoint create succeeds
10077 then the memory is not reclaimed. */
10078 bkpt_chain = make_cleanup (null_cleanup, 0);
10079
10080 /* Resolve all line numbers to PC's and verify that the addresses
10081 are ok for the target. */
10082 if (!pending)
10083 {
10084 int ix;
10085 struct linespec_sals *iter;
10086
10087 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
10088 breakpoint_sals_to_pc (&iter->sals);
10089 }
10090
10091 /* Fast tracepoints may have additional restrictions on location. */
10092 if (!pending && type_wanted == bp_fast_tracepoint)
10093 {
10094 int ix;
10095 struct linespec_sals *iter;
10096
10097 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
10098 check_fast_tracepoint_sals (gdbarch, &iter->sals);
10099 }
10100
10101 /* Verify that condition can be parsed, before setting any
10102 breakpoints. Allocate a separate condition expression for each
10103 breakpoint. */
10104 if (!pending)
10105 {
10106 if (parse_arg)
10107 {
10108 char *rest;
10109 struct linespec_sals *lsal;
10110
10111 lsal = VEC_index (linespec_sals, canonical.sals, 0);
10112
10113 /* Here we only parse 'arg' to separate condition
10114 from thread number, so parsing in context of first
10115 sal is OK. When setting the breakpoint we'll
10116 re-parse it in context of each sal. */
10117
10118 find_condition_and_thread (arg, lsal->sals.sals[0].pc, &cond_string,
10119 &thread, &task, &rest);
10120 if (cond_string)
10121 make_cleanup (xfree, cond_string);
10122 if (rest)
10123 make_cleanup (xfree, rest);
10124 if (rest)
10125 extra_string = rest;
10126 }
10127 else
10128 {
10129 if (*arg != '\0')
10130 error (_("Garbage '%s' at end of location"), arg);
10131
10132 /* Create a private copy of condition string. */
10133 if (cond_string)
10134 {
10135 cond_string = xstrdup (cond_string);
10136 make_cleanup (xfree, cond_string);
10137 }
10138 /* Create a private copy of any extra string. */
10139 if (extra_string)
10140 {
10141 extra_string = xstrdup (extra_string);
10142 make_cleanup (xfree, extra_string);
10143 }
10144 }
10145
10146 ops->create_breakpoints_sal (gdbarch, &canonical,
10147 cond_string, extra_string, type_wanted,
10148 tempflag ? disp_del : disp_donttouch,
10149 thread, task, ignore_count, ops,
10150 from_tty, enabled, internal, flags);
10151 }
10152 else
10153 {
10154 struct breakpoint *b;
10155
10156 make_cleanup (xfree, copy_arg);
10157
10158 if (is_tracepoint_type (type_wanted))
10159 {
10160 struct tracepoint *t;
10161
10162 t = XCNEW (struct tracepoint);
10163 b = &t->base;
10164 }
10165 else
10166 b = XNEW (struct breakpoint);
10167
10168 init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops);
10169
10170 b->addr_string = copy_arg;
10171 if (parse_arg)
10172 b->cond_string = NULL;
10173 else
10174 {
10175 /* Create a private copy of condition string. */
10176 if (cond_string)
10177 {
10178 cond_string = xstrdup (cond_string);
10179 make_cleanup (xfree, cond_string);
10180 }
10181 b->cond_string = cond_string;
10182 }
10183 b->extra_string = NULL;
10184 b->ignore_count = ignore_count;
10185 b->disposition = tempflag ? disp_del : disp_donttouch;
10186 b->condition_not_parsed = 1;
10187 b->enable_state = enabled ? bp_enabled : bp_disabled;
10188 if ((type_wanted != bp_breakpoint
10189 && type_wanted != bp_hardware_breakpoint) || thread != -1)
10190 b->pspace = current_program_space;
10191
10192 install_breakpoint (internal, b, 0);
10193 }
10194
10195 if (VEC_length (linespec_sals, canonical.sals) > 1)
10196 {
10197 warning (_("Multiple breakpoints were set.\nUse the "
10198 "\"delete\" command to delete unwanted breakpoints."));
10199 prev_breakpoint_count = prev_bkpt_count;
10200 }
10201
10202 /* That's it. Discard the cleanups for data inserted into the
10203 breakpoint. */
10204 discard_cleanups (bkpt_chain);
10205 /* But cleanup everything else. */
10206 do_cleanups (old_chain);
10207
10208 /* error call may happen here - have BKPT_CHAIN already discarded. */
10209 update_global_location_list (UGLL_MAY_INSERT);
10210
10211 return 1;
10212 }
10213
10214 /* Set a breakpoint.
10215 ARG is a string describing breakpoint address,
10216 condition, and thread.
10217 FLAG specifies if a breakpoint is hardware on,
10218 and if breakpoint is temporary, using BP_HARDWARE_FLAG
10219 and BP_TEMPFLAG. */
10220
10221 static void
10222 break_command_1 (char *arg, int flag, int from_tty)
10223 {
10224 int tempflag = flag & BP_TEMPFLAG;
10225 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
10226 ? bp_hardware_breakpoint
10227 : bp_breakpoint);
10228 struct breakpoint_ops *ops;
10229 const char *arg_cp = arg;
10230
10231 /* Matching breakpoints on probes. */
10232 if (arg && probe_linespec_to_ops (&arg_cp) != NULL)
10233 ops = &bkpt_probe_breakpoint_ops;
10234 else
10235 ops = &bkpt_breakpoint_ops;
10236
10237 create_breakpoint (get_current_arch (),
10238 arg,
10239 NULL, 0, NULL, 1 /* parse arg */,
10240 tempflag, type_wanted,
10241 0 /* Ignore count */,
10242 pending_break_support,
10243 ops,
10244 from_tty,
10245 1 /* enabled */,
10246 0 /* internal */,
10247 0);
10248 }
10249
10250 /* Helper function for break_command_1 and disassemble_command. */
10251
10252 void
10253 resolve_sal_pc (struct symtab_and_line *sal)
10254 {
10255 CORE_ADDR pc;
10256
10257 if (sal->pc == 0 && sal->symtab != NULL)
10258 {
10259 if (!find_line_pc (sal->symtab, sal->line, &pc))
10260 error (_("No line %d in file \"%s\"."),
10261 sal->line, symtab_to_filename_for_display (sal->symtab));
10262 sal->pc = pc;
10263
10264 /* If this SAL corresponds to a breakpoint inserted using a line
10265 number, then skip the function prologue if necessary. */
10266 if (sal->explicit_line)
10267 skip_prologue_sal (sal);
10268 }
10269
10270 if (sal->section == 0 && sal->symtab != NULL)
10271 {
10272 const struct blockvector *bv;
10273 const struct block *b;
10274 struct symbol *sym;
10275
10276 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
10277 SYMTAB_COMPUNIT (sal->symtab));
10278 if (bv != NULL)
10279 {
10280 sym = block_linkage_function (b);
10281 if (sym != NULL)
10282 {
10283 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
10284 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
10285 sym);
10286 }
10287 else
10288 {
10289 /* It really is worthwhile to have the section, so we'll
10290 just have to look harder. This case can be executed
10291 if we have line numbers but no functions (as can
10292 happen in assembly source). */
10293
10294 struct bound_minimal_symbol msym;
10295 struct cleanup *old_chain = save_current_space_and_thread ();
10296
10297 switch_to_program_space_and_thread (sal->pspace);
10298
10299 msym = lookup_minimal_symbol_by_pc (sal->pc);
10300 if (msym.minsym)
10301 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
10302
10303 do_cleanups (old_chain);
10304 }
10305 }
10306 }
10307 }
10308
10309 void
10310 break_command (char *arg, int from_tty)
10311 {
10312 break_command_1 (arg, 0, from_tty);
10313 }
10314
10315 void
10316 tbreak_command (char *arg, int from_tty)
10317 {
10318 break_command_1 (arg, BP_TEMPFLAG, from_tty);
10319 }
10320
10321 static void
10322 hbreak_command (char *arg, int from_tty)
10323 {
10324 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
10325 }
10326
10327 static void
10328 thbreak_command (char *arg, int from_tty)
10329 {
10330 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
10331 }
10332
10333 static void
10334 stop_command (char *arg, int from_tty)
10335 {
10336 printf_filtered (_("Specify the type of breakpoint to set.\n\
10337 Usage: stop in <function | address>\n\
10338 stop at <line>\n"));
10339 }
10340
10341 static void
10342 stopin_command (char *arg, int from_tty)
10343 {
10344 int badInput = 0;
10345
10346 if (arg == (char *) NULL)
10347 badInput = 1;
10348 else if (*arg != '*')
10349 {
10350 char *argptr = arg;
10351 int hasColon = 0;
10352
10353 /* Look for a ':'. If this is a line number specification, then
10354 say it is bad, otherwise, it should be an address or
10355 function/method name. */
10356 while (*argptr && !hasColon)
10357 {
10358 hasColon = (*argptr == ':');
10359 argptr++;
10360 }
10361
10362 if (hasColon)
10363 badInput = (*argptr != ':'); /* Not a class::method */
10364 else
10365 badInput = isdigit (*arg); /* a simple line number */
10366 }
10367
10368 if (badInput)
10369 printf_filtered (_("Usage: stop in <function | address>\n"));
10370 else
10371 break_command_1 (arg, 0, from_tty);
10372 }
10373
10374 static void
10375 stopat_command (char *arg, int from_tty)
10376 {
10377 int badInput = 0;
10378
10379 if (arg == (char *) NULL || *arg == '*') /* no line number */
10380 badInput = 1;
10381 else
10382 {
10383 char *argptr = arg;
10384 int hasColon = 0;
10385
10386 /* Look for a ':'. If there is a '::' then get out, otherwise
10387 it is probably a line number. */
10388 while (*argptr && !hasColon)
10389 {
10390 hasColon = (*argptr == ':');
10391 argptr++;
10392 }
10393
10394 if (hasColon)
10395 badInput = (*argptr == ':'); /* we have class::method */
10396 else
10397 badInput = !isdigit (*arg); /* not a line number */
10398 }
10399
10400 if (badInput)
10401 printf_filtered (_("Usage: stop at <line>\n"));
10402 else
10403 break_command_1 (arg, 0, from_tty);
10404 }
10405
10406 /* The dynamic printf command is mostly like a regular breakpoint, but
10407 with a prewired command list consisting of a single output command,
10408 built from extra arguments supplied on the dprintf command
10409 line. */
10410
10411 static void
10412 dprintf_command (char *arg, int from_tty)
10413 {
10414 create_breakpoint (get_current_arch (),
10415 arg,
10416 NULL, 0, NULL, 1 /* parse arg */,
10417 0, bp_dprintf,
10418 0 /* Ignore count */,
10419 pending_break_support,
10420 &dprintf_breakpoint_ops,
10421 from_tty,
10422 1 /* enabled */,
10423 0 /* internal */,
10424 0);
10425 }
10426
10427 static void
10428 agent_printf_command (char *arg, int from_tty)
10429 {
10430 error (_("May only run agent-printf on the target"));
10431 }
10432
10433 /* Implement the "breakpoint_hit" breakpoint_ops method for
10434 ranged breakpoints. */
10435
10436 static int
10437 breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
10438 struct address_space *aspace,
10439 CORE_ADDR bp_addr,
10440 const struct target_waitstatus *ws)
10441 {
10442 if (ws->kind != TARGET_WAITKIND_STOPPED
10443 || ws->value.sig != GDB_SIGNAL_TRAP)
10444 return 0;
10445
10446 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
10447 bl->length, aspace, bp_addr);
10448 }
10449
10450 /* Implement the "resources_needed" breakpoint_ops method for
10451 ranged breakpoints. */
10452
10453 static int
10454 resources_needed_ranged_breakpoint (const struct bp_location *bl)
10455 {
10456 return target_ranged_break_num_registers ();
10457 }
10458
10459 /* Implement the "print_it" breakpoint_ops method for
10460 ranged breakpoints. */
10461
10462 static enum print_stop_action
10463 print_it_ranged_breakpoint (bpstat bs)
10464 {
10465 struct breakpoint *b = bs->breakpoint_at;
10466 struct bp_location *bl = b->loc;
10467 struct ui_out *uiout = current_uiout;
10468
10469 gdb_assert (b->type == bp_hardware_breakpoint);
10470
10471 /* Ranged breakpoints have only one location. */
10472 gdb_assert (bl && bl->next == NULL);
10473
10474 annotate_breakpoint (b->number);
10475 if (b->disposition == disp_del)
10476 ui_out_text (uiout, "\nTemporary ranged breakpoint ");
10477 else
10478 ui_out_text (uiout, "\nRanged breakpoint ");
10479 if (ui_out_is_mi_like_p (uiout))
10480 {
10481 ui_out_field_string (uiout, "reason",
10482 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
10483 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
10484 }
10485 ui_out_field_int (uiout, "bkptno", b->number);
10486 ui_out_text (uiout, ", ");
10487
10488 return PRINT_SRC_AND_LOC;
10489 }
10490
10491 /* Implement the "print_one" breakpoint_ops method for
10492 ranged breakpoints. */
10493
10494 static void
10495 print_one_ranged_breakpoint (struct breakpoint *b,
10496 struct bp_location **last_loc)
10497 {
10498 struct bp_location *bl = b->loc;
10499 struct value_print_options opts;
10500 struct ui_out *uiout = current_uiout;
10501
10502 /* Ranged breakpoints have only one location. */
10503 gdb_assert (bl && bl->next == NULL);
10504
10505 get_user_print_options (&opts);
10506
10507 if (opts.addressprint)
10508 /* We don't print the address range here, it will be printed later
10509 by print_one_detail_ranged_breakpoint. */
10510 ui_out_field_skip (uiout, "addr");
10511 annotate_field (5);
10512 print_breakpoint_location (b, bl);
10513 *last_loc = bl;
10514 }
10515
10516 /* Implement the "print_one_detail" breakpoint_ops method for
10517 ranged breakpoints. */
10518
10519 static void
10520 print_one_detail_ranged_breakpoint (const struct breakpoint *b,
10521 struct ui_out *uiout)
10522 {
10523 CORE_ADDR address_start, address_end;
10524 struct bp_location *bl = b->loc;
10525 struct ui_file *stb = mem_fileopen ();
10526 struct cleanup *cleanup = make_cleanup_ui_file_delete (stb);
10527
10528 gdb_assert (bl);
10529
10530 address_start = bl->address;
10531 address_end = address_start + bl->length - 1;
10532
10533 ui_out_text (uiout, "\taddress range: ");
10534 fprintf_unfiltered (stb, "[%s, %s]",
10535 print_core_address (bl->gdbarch, address_start),
10536 print_core_address (bl->gdbarch, address_end));
10537 ui_out_field_stream (uiout, "addr", stb);
10538 ui_out_text (uiout, "\n");
10539
10540 do_cleanups (cleanup);
10541 }
10542
10543 /* Implement the "print_mention" breakpoint_ops method for
10544 ranged breakpoints. */
10545
10546 static void
10547 print_mention_ranged_breakpoint (struct breakpoint *b)
10548 {
10549 struct bp_location *bl = b->loc;
10550 struct ui_out *uiout = current_uiout;
10551
10552 gdb_assert (bl);
10553 gdb_assert (b->type == bp_hardware_breakpoint);
10554
10555 if (ui_out_is_mi_like_p (uiout))
10556 return;
10557
10558 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
10559 b->number, paddress (bl->gdbarch, bl->address),
10560 paddress (bl->gdbarch, bl->address + bl->length - 1));
10561 }
10562
10563 /* Implement the "print_recreate" breakpoint_ops method for
10564 ranged breakpoints. */
10565
10566 static void
10567 print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
10568 {
10569 fprintf_unfiltered (fp, "break-range %s, %s", b->addr_string,
10570 b->addr_string_range_end);
10571 print_recreate_thread (b, fp);
10572 }
10573
10574 /* The breakpoint_ops structure to be used in ranged breakpoints. */
10575
10576 static struct breakpoint_ops ranged_breakpoint_ops;
10577
10578 /* Find the address where the end of the breakpoint range should be
10579 placed, given the SAL of the end of the range. This is so that if
10580 the user provides a line number, the end of the range is set to the
10581 last instruction of the given line. */
10582
10583 static CORE_ADDR
10584 find_breakpoint_range_end (struct symtab_and_line sal)
10585 {
10586 CORE_ADDR end;
10587
10588 /* If the user provided a PC value, use it. Otherwise,
10589 find the address of the end of the given location. */
10590 if (sal.explicit_pc)
10591 end = sal.pc;
10592 else
10593 {
10594 int ret;
10595 CORE_ADDR start;
10596
10597 ret = find_line_pc_range (sal, &start, &end);
10598 if (!ret)
10599 error (_("Could not find location of the end of the range."));
10600
10601 /* find_line_pc_range returns the start of the next line. */
10602 end--;
10603 }
10604
10605 return end;
10606 }
10607
10608 /* Implement the "break-range" CLI command. */
10609
10610 static void
10611 break_range_command (char *arg, int from_tty)
10612 {
10613 char *arg_start, *addr_string_start, *addr_string_end;
10614 struct linespec_result canonical_start, canonical_end;
10615 int bp_count, can_use_bp, length;
10616 CORE_ADDR end;
10617 struct breakpoint *b;
10618 struct symtab_and_line sal_start, sal_end;
10619 struct cleanup *cleanup_bkpt;
10620 struct linespec_sals *lsal_start, *lsal_end;
10621
10622 /* We don't support software ranged breakpoints. */
10623 if (target_ranged_break_num_registers () < 0)
10624 error (_("This target does not support hardware ranged breakpoints."));
10625
10626 bp_count = hw_breakpoint_used_count ();
10627 bp_count += target_ranged_break_num_registers ();
10628 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10629 bp_count, 0);
10630 if (can_use_bp < 0)
10631 error (_("Hardware breakpoints used exceeds limit."));
10632
10633 arg = skip_spaces (arg);
10634 if (arg == NULL || arg[0] == '\0')
10635 error(_("No address range specified."));
10636
10637 init_linespec_result (&canonical_start);
10638
10639 arg_start = arg;
10640 parse_breakpoint_sals (&arg, &canonical_start);
10641
10642 cleanup_bkpt = make_cleanup_destroy_linespec_result (&canonical_start);
10643
10644 if (arg[0] != ',')
10645 error (_("Too few arguments."));
10646 else if (VEC_empty (linespec_sals, canonical_start.sals))
10647 error (_("Could not find location of the beginning of the range."));
10648
10649 lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
10650
10651 if (VEC_length (linespec_sals, canonical_start.sals) > 1
10652 || lsal_start->sals.nelts != 1)
10653 error (_("Cannot create a ranged breakpoint with multiple locations."));
10654
10655 sal_start = lsal_start->sals.sals[0];
10656 addr_string_start = savestring (arg_start, arg - arg_start);
10657 make_cleanup (xfree, addr_string_start);
10658
10659 arg++; /* Skip the comma. */
10660 arg = skip_spaces (arg);
10661
10662 /* Parse the end location. */
10663
10664 init_linespec_result (&canonical_end);
10665 arg_start = arg;
10666
10667 /* We call decode_line_full directly here instead of using
10668 parse_breakpoint_sals because we need to specify the start location's
10669 symtab and line as the default symtab and line for the end of the
10670 range. This makes it possible to have ranges like "foo.c:27, +14",
10671 where +14 means 14 lines from the start location. */
10672 decode_line_full (&arg, DECODE_LINE_FUNFIRSTLINE,
10673 sal_start.symtab, sal_start.line,
10674 &canonical_end, NULL, NULL);
10675
10676 make_cleanup_destroy_linespec_result (&canonical_end);
10677
10678 if (VEC_empty (linespec_sals, canonical_end.sals))
10679 error (_("Could not find location of the end of the range."));
10680
10681 lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
10682 if (VEC_length (linespec_sals, canonical_end.sals) > 1
10683 || lsal_end->sals.nelts != 1)
10684 error (_("Cannot create a ranged breakpoint with multiple locations."));
10685
10686 sal_end = lsal_end->sals.sals[0];
10687 addr_string_end = savestring (arg_start, arg - arg_start);
10688 make_cleanup (xfree, addr_string_end);
10689
10690 end = find_breakpoint_range_end (sal_end);
10691 if (sal_start.pc > end)
10692 error (_("Invalid address range, end precedes start."));
10693
10694 length = end - sal_start.pc + 1;
10695 if (length < 0)
10696 /* Length overflowed. */
10697 error (_("Address range too large."));
10698 else if (length == 1)
10699 {
10700 /* This range is simple enough to be handled by
10701 the `hbreak' command. */
10702 hbreak_command (addr_string_start, 1);
10703
10704 do_cleanups (cleanup_bkpt);
10705
10706 return;
10707 }
10708
10709 /* Now set up the breakpoint. */
10710 b = set_raw_breakpoint (get_current_arch (), sal_start,
10711 bp_hardware_breakpoint, &ranged_breakpoint_ops);
10712 set_breakpoint_count (breakpoint_count + 1);
10713 b->number = breakpoint_count;
10714 b->disposition = disp_donttouch;
10715 b->addr_string = xstrdup (addr_string_start);
10716 b->addr_string_range_end = xstrdup (addr_string_end);
10717 b->loc->length = length;
10718
10719 do_cleanups (cleanup_bkpt);
10720
10721 mention (b);
10722 observer_notify_breakpoint_created (b);
10723 update_global_location_list (UGLL_MAY_INSERT);
10724 }
10725
10726 /* Return non-zero if EXP is verified as constant. Returned zero
10727 means EXP is variable. Also the constant detection may fail for
10728 some constant expressions and in such case still falsely return
10729 zero. */
10730
10731 static int
10732 watchpoint_exp_is_const (const struct expression *exp)
10733 {
10734 int i = exp->nelts;
10735
10736 while (i > 0)
10737 {
10738 int oplenp, argsp;
10739
10740 /* We are only interested in the descriptor of each element. */
10741 operator_length (exp, i, &oplenp, &argsp);
10742 i -= oplenp;
10743
10744 switch (exp->elts[i].opcode)
10745 {
10746 case BINOP_ADD:
10747 case BINOP_SUB:
10748 case BINOP_MUL:
10749 case BINOP_DIV:
10750 case BINOP_REM:
10751 case BINOP_MOD:
10752 case BINOP_LSH:
10753 case BINOP_RSH:
10754 case BINOP_LOGICAL_AND:
10755 case BINOP_LOGICAL_OR:
10756 case BINOP_BITWISE_AND:
10757 case BINOP_BITWISE_IOR:
10758 case BINOP_BITWISE_XOR:
10759 case BINOP_EQUAL:
10760 case BINOP_NOTEQUAL:
10761 case BINOP_LESS:
10762 case BINOP_GTR:
10763 case BINOP_LEQ:
10764 case BINOP_GEQ:
10765 case BINOP_REPEAT:
10766 case BINOP_COMMA:
10767 case BINOP_EXP:
10768 case BINOP_MIN:
10769 case BINOP_MAX:
10770 case BINOP_INTDIV:
10771 case BINOP_CONCAT:
10772 case TERNOP_COND:
10773 case TERNOP_SLICE:
10774
10775 case OP_LONG:
10776 case OP_DOUBLE:
10777 case OP_DECFLOAT:
10778 case OP_LAST:
10779 case OP_COMPLEX:
10780 case OP_STRING:
10781 case OP_ARRAY:
10782 case OP_TYPE:
10783 case OP_TYPEOF:
10784 case OP_DECLTYPE:
10785 case OP_TYPEID:
10786 case OP_NAME:
10787 case OP_OBJC_NSSTRING:
10788
10789 case UNOP_NEG:
10790 case UNOP_LOGICAL_NOT:
10791 case UNOP_COMPLEMENT:
10792 case UNOP_ADDR:
10793 case UNOP_HIGH:
10794 case UNOP_CAST:
10795
10796 case UNOP_CAST_TYPE:
10797 case UNOP_REINTERPRET_CAST:
10798 case UNOP_DYNAMIC_CAST:
10799 /* Unary, binary and ternary operators: We have to check
10800 their operands. If they are constant, then so is the
10801 result of that operation. For instance, if A and B are
10802 determined to be constants, then so is "A + B".
10803
10804 UNOP_IND is one exception to the rule above, because the
10805 value of *ADDR is not necessarily a constant, even when
10806 ADDR is. */
10807 break;
10808
10809 case OP_VAR_VALUE:
10810 /* Check whether the associated symbol is a constant.
10811
10812 We use SYMBOL_CLASS rather than TYPE_CONST because it's
10813 possible that a buggy compiler could mark a variable as
10814 constant even when it is not, and TYPE_CONST would return
10815 true in this case, while SYMBOL_CLASS wouldn't.
10816
10817 We also have to check for function symbols because they
10818 are always constant. */
10819 {
10820 struct symbol *s = exp->elts[i + 2].symbol;
10821
10822 if (SYMBOL_CLASS (s) != LOC_BLOCK
10823 && SYMBOL_CLASS (s) != LOC_CONST
10824 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10825 return 0;
10826 break;
10827 }
10828
10829 /* The default action is to return 0 because we are using
10830 the optimistic approach here: If we don't know something,
10831 then it is not a constant. */
10832 default:
10833 return 0;
10834 }
10835 }
10836
10837 return 1;
10838 }
10839
10840 /* Implement the "dtor" breakpoint_ops method for watchpoints. */
10841
10842 static void
10843 dtor_watchpoint (struct breakpoint *self)
10844 {
10845 struct watchpoint *w = (struct watchpoint *) self;
10846
10847 xfree (w->cond_exp);
10848 xfree (w->exp);
10849 xfree (w->exp_string);
10850 xfree (w->exp_string_reparse);
10851 value_free (w->val);
10852
10853 base_breakpoint_ops.dtor (self);
10854 }
10855
10856 /* Implement the "re_set" breakpoint_ops method for watchpoints. */
10857
10858 static void
10859 re_set_watchpoint (struct breakpoint *b)
10860 {
10861 struct watchpoint *w = (struct watchpoint *) b;
10862
10863 /* Watchpoint can be either on expression using entirely global
10864 variables, or it can be on local variables.
10865
10866 Watchpoints of the first kind are never auto-deleted, and even
10867 persist across program restarts. Since they can use variables
10868 from shared libraries, we need to reparse expression as libraries
10869 are loaded and unloaded.
10870
10871 Watchpoints on local variables can also change meaning as result
10872 of solib event. For example, if a watchpoint uses both a local
10873 and a global variables in expression, it's a local watchpoint,
10874 but unloading of a shared library will make the expression
10875 invalid. This is not a very common use case, but we still
10876 re-evaluate expression, to avoid surprises to the user.
10877
10878 Note that for local watchpoints, we re-evaluate it only if
10879 watchpoints frame id is still valid. If it's not, it means the
10880 watchpoint is out of scope and will be deleted soon. In fact,
10881 I'm not sure we'll ever be called in this case.
10882
10883 If a local watchpoint's frame id is still valid, then
10884 w->exp_valid_block is likewise valid, and we can safely use it.
10885
10886 Don't do anything about disabled watchpoints, since they will be
10887 reevaluated again when enabled. */
10888 update_watchpoint (w, 1 /* reparse */);
10889 }
10890
10891 /* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10892
10893 static int
10894 insert_watchpoint (struct bp_location *bl)
10895 {
10896 struct watchpoint *w = (struct watchpoint *) bl->owner;
10897 int length = w->exact ? 1 : bl->length;
10898
10899 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
10900 w->cond_exp);
10901 }
10902
10903 /* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10904
10905 static int
10906 remove_watchpoint (struct bp_location *bl)
10907 {
10908 struct watchpoint *w = (struct watchpoint *) bl->owner;
10909 int length = w->exact ? 1 : bl->length;
10910
10911 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
10912 w->cond_exp);
10913 }
10914
10915 static int
10916 breakpoint_hit_watchpoint (const struct bp_location *bl,
10917 struct address_space *aspace, CORE_ADDR bp_addr,
10918 const struct target_waitstatus *ws)
10919 {
10920 struct breakpoint *b = bl->owner;
10921 struct watchpoint *w = (struct watchpoint *) b;
10922
10923 /* Continuable hardware watchpoints are treated as non-existent if the
10924 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10925 some data address). Otherwise gdb won't stop on a break instruction
10926 in the code (not from a breakpoint) when a hardware watchpoint has
10927 been defined. Also skip watchpoints which we know did not trigger
10928 (did not match the data address). */
10929 if (is_hardware_watchpoint (b)
10930 && w->watchpoint_triggered == watch_triggered_no)
10931 return 0;
10932
10933 return 1;
10934 }
10935
10936 static void
10937 check_status_watchpoint (bpstat bs)
10938 {
10939 gdb_assert (is_watchpoint (bs->breakpoint_at));
10940
10941 bpstat_check_watchpoint (bs);
10942 }
10943
10944 /* Implement the "resources_needed" breakpoint_ops method for
10945 hardware watchpoints. */
10946
10947 static int
10948 resources_needed_watchpoint (const struct bp_location *bl)
10949 {
10950 struct watchpoint *w = (struct watchpoint *) bl->owner;
10951 int length = w->exact? 1 : bl->length;
10952
10953 return target_region_ok_for_hw_watchpoint (bl->address, length);
10954 }
10955
10956 /* Implement the "works_in_software_mode" breakpoint_ops method for
10957 hardware watchpoints. */
10958
10959 static int
10960 works_in_software_mode_watchpoint (const struct breakpoint *b)
10961 {
10962 /* Read and access watchpoints only work with hardware support. */
10963 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
10964 }
10965
10966 static enum print_stop_action
10967 print_it_watchpoint (bpstat bs)
10968 {
10969 struct cleanup *old_chain;
10970 struct breakpoint *b;
10971 struct ui_file *stb;
10972 enum print_stop_action result;
10973 struct watchpoint *w;
10974 struct ui_out *uiout = current_uiout;
10975
10976 gdb_assert (bs->bp_location_at != NULL);
10977
10978 b = bs->breakpoint_at;
10979 w = (struct watchpoint *) b;
10980
10981 stb = mem_fileopen ();
10982 old_chain = make_cleanup_ui_file_delete (stb);
10983
10984 switch (b->type)
10985 {
10986 case bp_watchpoint:
10987 case bp_hardware_watchpoint:
10988 annotate_watchpoint (b->number);
10989 if (ui_out_is_mi_like_p (uiout))
10990 ui_out_field_string
10991 (uiout, "reason",
10992 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10993 mention (b);
10994 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10995 ui_out_text (uiout, "\nOld value = ");
10996 watchpoint_value_print (bs->old_val, stb);
10997 ui_out_field_stream (uiout, "old", stb);
10998 ui_out_text (uiout, "\nNew value = ");
10999 watchpoint_value_print (w->val, stb);
11000 ui_out_field_stream (uiout, "new", stb);
11001 ui_out_text (uiout, "\n");
11002 /* More than one watchpoint may have been triggered. */
11003 result = PRINT_UNKNOWN;
11004 break;
11005
11006 case bp_read_watchpoint:
11007 if (ui_out_is_mi_like_p (uiout))
11008 ui_out_field_string
11009 (uiout, "reason",
11010 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
11011 mention (b);
11012 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
11013 ui_out_text (uiout, "\nValue = ");
11014 watchpoint_value_print (w->val, stb);
11015 ui_out_field_stream (uiout, "value", stb);
11016 ui_out_text (uiout, "\n");
11017 result = PRINT_UNKNOWN;
11018 break;
11019
11020 case bp_access_watchpoint:
11021 if (bs->old_val != NULL)
11022 {
11023 annotate_watchpoint (b->number);
11024 if (ui_out_is_mi_like_p (uiout))
11025 ui_out_field_string
11026 (uiout, "reason",
11027 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
11028 mention (b);
11029 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
11030 ui_out_text (uiout, "\nOld value = ");
11031 watchpoint_value_print (bs->old_val, stb);
11032 ui_out_field_stream (uiout, "old", stb);
11033 ui_out_text (uiout, "\nNew value = ");
11034 }
11035 else
11036 {
11037 mention (b);
11038 if (ui_out_is_mi_like_p (uiout))
11039 ui_out_field_string
11040 (uiout, "reason",
11041 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
11042 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
11043 ui_out_text (uiout, "\nValue = ");
11044 }
11045 watchpoint_value_print (w->val, stb);
11046 ui_out_field_stream (uiout, "new", stb);
11047 ui_out_text (uiout, "\n");
11048 result = PRINT_UNKNOWN;
11049 break;
11050 default:
11051 result = PRINT_UNKNOWN;
11052 }
11053
11054 do_cleanups (old_chain);
11055 return result;
11056 }
11057
11058 /* Implement the "print_mention" breakpoint_ops method for hardware
11059 watchpoints. */
11060
11061 static void
11062 print_mention_watchpoint (struct breakpoint *b)
11063 {
11064 struct cleanup *ui_out_chain;
11065 struct watchpoint *w = (struct watchpoint *) b;
11066 struct ui_out *uiout = current_uiout;
11067
11068 switch (b->type)
11069 {
11070 case bp_watchpoint:
11071 ui_out_text (uiout, "Watchpoint ");
11072 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
11073 break;
11074 case bp_hardware_watchpoint:
11075 ui_out_text (uiout, "Hardware watchpoint ");
11076 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
11077 break;
11078 case bp_read_watchpoint:
11079 ui_out_text (uiout, "Hardware read watchpoint ");
11080 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
11081 break;
11082 case bp_access_watchpoint:
11083 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
11084 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
11085 break;
11086 default:
11087 internal_error (__FILE__, __LINE__,
11088 _("Invalid hardware watchpoint type."));
11089 }
11090
11091 ui_out_field_int (uiout, "number", b->number);
11092 ui_out_text (uiout, ": ");
11093 ui_out_field_string (uiout, "exp", w->exp_string);
11094 do_cleanups (ui_out_chain);
11095 }
11096
11097 /* Implement the "print_recreate" breakpoint_ops method for
11098 watchpoints. */
11099
11100 static void
11101 print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
11102 {
11103 struct watchpoint *w = (struct watchpoint *) b;
11104
11105 switch (b->type)
11106 {
11107 case bp_watchpoint:
11108 case bp_hardware_watchpoint:
11109 fprintf_unfiltered (fp, "watch");
11110 break;
11111 case bp_read_watchpoint:
11112 fprintf_unfiltered (fp, "rwatch");
11113 break;
11114 case bp_access_watchpoint:
11115 fprintf_unfiltered (fp, "awatch");
11116 break;
11117 default:
11118 internal_error (__FILE__, __LINE__,
11119 _("Invalid watchpoint type."));
11120 }
11121
11122 fprintf_unfiltered (fp, " %s", w->exp_string);
11123 print_recreate_thread (b, fp);
11124 }
11125
11126 /* Implement the "explains_signal" breakpoint_ops method for
11127 watchpoints. */
11128
11129 static int
11130 explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
11131 {
11132 /* A software watchpoint cannot cause a signal other than
11133 GDB_SIGNAL_TRAP. */
11134 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
11135 return 0;
11136
11137 return 1;
11138 }
11139
11140 /* The breakpoint_ops structure to be used in hardware watchpoints. */
11141
11142 static struct breakpoint_ops watchpoint_breakpoint_ops;
11143
11144 /* Implement the "insert" breakpoint_ops method for
11145 masked hardware watchpoints. */
11146
11147 static int
11148 insert_masked_watchpoint (struct bp_location *bl)
11149 {
11150 struct watchpoint *w = (struct watchpoint *) bl->owner;
11151
11152 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
11153 bl->watchpoint_type);
11154 }
11155
11156 /* Implement the "remove" breakpoint_ops method for
11157 masked hardware watchpoints. */
11158
11159 static int
11160 remove_masked_watchpoint (struct bp_location *bl)
11161 {
11162 struct watchpoint *w = (struct watchpoint *) bl->owner;
11163
11164 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
11165 bl->watchpoint_type);
11166 }
11167
11168 /* Implement the "resources_needed" breakpoint_ops method for
11169 masked hardware watchpoints. */
11170
11171 static int
11172 resources_needed_masked_watchpoint (const struct bp_location *bl)
11173 {
11174 struct watchpoint *w = (struct watchpoint *) bl->owner;
11175
11176 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
11177 }
11178
11179 /* Implement the "works_in_software_mode" breakpoint_ops method for
11180 masked hardware watchpoints. */
11181
11182 static int
11183 works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
11184 {
11185 return 0;
11186 }
11187
11188 /* Implement the "print_it" breakpoint_ops method for
11189 masked hardware watchpoints. */
11190
11191 static enum print_stop_action
11192 print_it_masked_watchpoint (bpstat bs)
11193 {
11194 struct breakpoint *b = bs->breakpoint_at;
11195 struct ui_out *uiout = current_uiout;
11196
11197 /* Masked watchpoints have only one location. */
11198 gdb_assert (b->loc && b->loc->next == NULL);
11199
11200 switch (b->type)
11201 {
11202 case bp_hardware_watchpoint:
11203 annotate_watchpoint (b->number);
11204 if (ui_out_is_mi_like_p (uiout))
11205 ui_out_field_string
11206 (uiout, "reason",
11207 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
11208 break;
11209
11210 case bp_read_watchpoint:
11211 if (ui_out_is_mi_like_p (uiout))
11212 ui_out_field_string
11213 (uiout, "reason",
11214 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
11215 break;
11216
11217 case bp_access_watchpoint:
11218 if (ui_out_is_mi_like_p (uiout))
11219 ui_out_field_string
11220 (uiout, "reason",
11221 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
11222 break;
11223 default:
11224 internal_error (__FILE__, __LINE__,
11225 _("Invalid hardware watchpoint type."));
11226 }
11227
11228 mention (b);
11229 ui_out_text (uiout, _("\n\
11230 Check the underlying instruction at PC for the memory\n\
11231 address and value which triggered this watchpoint.\n"));
11232 ui_out_text (uiout, "\n");
11233
11234 /* More than one watchpoint may have been triggered. */
11235 return PRINT_UNKNOWN;
11236 }
11237
11238 /* Implement the "print_one_detail" breakpoint_ops method for
11239 masked hardware watchpoints. */
11240
11241 static void
11242 print_one_detail_masked_watchpoint (const struct breakpoint *b,
11243 struct ui_out *uiout)
11244 {
11245 struct watchpoint *w = (struct watchpoint *) b;
11246
11247 /* Masked watchpoints have only one location. */
11248 gdb_assert (b->loc && b->loc->next == NULL);
11249
11250 ui_out_text (uiout, "\tmask ");
11251 ui_out_field_core_addr (uiout, "mask", b->loc->gdbarch, w->hw_wp_mask);
11252 ui_out_text (uiout, "\n");
11253 }
11254
11255 /* Implement the "print_mention" breakpoint_ops method for
11256 masked hardware watchpoints. */
11257
11258 static void
11259 print_mention_masked_watchpoint (struct breakpoint *b)
11260 {
11261 struct watchpoint *w = (struct watchpoint *) b;
11262 struct ui_out *uiout = current_uiout;
11263 struct cleanup *ui_out_chain;
11264
11265 switch (b->type)
11266 {
11267 case bp_hardware_watchpoint:
11268 ui_out_text (uiout, "Masked hardware watchpoint ");
11269 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
11270 break;
11271 case bp_read_watchpoint:
11272 ui_out_text (uiout, "Masked hardware read watchpoint ");
11273 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
11274 break;
11275 case bp_access_watchpoint:
11276 ui_out_text (uiout, "Masked hardware access (read/write) watchpoint ");
11277 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
11278 break;
11279 default:
11280 internal_error (__FILE__, __LINE__,
11281 _("Invalid hardware watchpoint type."));
11282 }
11283
11284 ui_out_field_int (uiout, "number", b->number);
11285 ui_out_text (uiout, ": ");
11286 ui_out_field_string (uiout, "exp", w->exp_string);
11287 do_cleanups (ui_out_chain);
11288 }
11289
11290 /* Implement the "print_recreate" breakpoint_ops method for
11291 masked hardware watchpoints. */
11292
11293 static void
11294 print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
11295 {
11296 struct watchpoint *w = (struct watchpoint *) b;
11297 char tmp[40];
11298
11299 switch (b->type)
11300 {
11301 case bp_hardware_watchpoint:
11302 fprintf_unfiltered (fp, "watch");
11303 break;
11304 case bp_read_watchpoint:
11305 fprintf_unfiltered (fp, "rwatch");
11306 break;
11307 case bp_access_watchpoint:
11308 fprintf_unfiltered (fp, "awatch");
11309 break;
11310 default:
11311 internal_error (__FILE__, __LINE__,
11312 _("Invalid hardware watchpoint type."));
11313 }
11314
11315 sprintf_vma (tmp, w->hw_wp_mask);
11316 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
11317 print_recreate_thread (b, fp);
11318 }
11319
11320 /* The breakpoint_ops structure to be used in masked hardware watchpoints. */
11321
11322 static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
11323
11324 /* Tell whether the given watchpoint is a masked hardware watchpoint. */
11325
11326 static int
11327 is_masked_watchpoint (const struct breakpoint *b)
11328 {
11329 return b->ops == &masked_watchpoint_breakpoint_ops;
11330 }
11331
11332 /* accessflag: hw_write: watch write,
11333 hw_read: watch read,
11334 hw_access: watch access (read or write) */
11335 static void
11336 watch_command_1 (const char *arg, int accessflag, int from_tty,
11337 int just_location, int internal)
11338 {
11339 struct breakpoint *b, *scope_breakpoint = NULL;
11340 struct expression *exp;
11341 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
11342 struct value *val, *mark, *result;
11343 int saved_bitpos = 0, saved_bitsize = 0;
11344 struct frame_info *frame;
11345 const char *exp_start = NULL;
11346 const char *exp_end = NULL;
11347 const char *tok, *end_tok;
11348 int toklen = -1;
11349 const char *cond_start = NULL;
11350 const char *cond_end = NULL;
11351 enum bptype bp_type;
11352 int thread = -1;
11353 int pc = 0;
11354 /* Flag to indicate whether we are going to use masks for
11355 the hardware watchpoint. */
11356 int use_mask = 0;
11357 CORE_ADDR mask = 0;
11358 struct watchpoint *w;
11359 char *expression;
11360 struct cleanup *back_to;
11361
11362 /* Make sure that we actually have parameters to parse. */
11363 if (arg != NULL && arg[0] != '\0')
11364 {
11365 const char *value_start;
11366
11367 exp_end = arg + strlen (arg);
11368
11369 /* Look for "parameter value" pairs at the end
11370 of the arguments string. */
11371 for (tok = exp_end - 1; tok > arg; tok--)
11372 {
11373 /* Skip whitespace at the end of the argument list. */
11374 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11375 tok--;
11376
11377 /* Find the beginning of the last token.
11378 This is the value of the parameter. */
11379 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11380 tok--;
11381 value_start = tok + 1;
11382
11383 /* Skip whitespace. */
11384 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11385 tok--;
11386
11387 end_tok = tok;
11388
11389 /* Find the beginning of the second to last token.
11390 This is the parameter itself. */
11391 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11392 tok--;
11393 tok++;
11394 toklen = end_tok - tok + 1;
11395
11396 if (toklen == 6 && startswith (tok, "thread"))
11397 {
11398 /* At this point we've found a "thread" token, which means
11399 the user is trying to set a watchpoint that triggers
11400 only in a specific thread. */
11401 char *endp;
11402
11403 if (thread != -1)
11404 error(_("You can specify only one thread."));
11405
11406 /* Extract the thread ID from the next token. */
11407 thread = strtol (value_start, &endp, 0);
11408
11409 /* Check if the user provided a valid numeric value for the
11410 thread ID. */
11411 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
11412 error (_("Invalid thread ID specification %s."), value_start);
11413
11414 /* Check if the thread actually exists. */
11415 if (!valid_thread_id (thread))
11416 invalid_thread_id_error (thread);
11417 }
11418 else if (toklen == 4 && startswith (tok, "mask"))
11419 {
11420 /* We've found a "mask" token, which means the user wants to
11421 create a hardware watchpoint that is going to have the mask
11422 facility. */
11423 struct value *mask_value, *mark;
11424
11425 if (use_mask)
11426 error(_("You can specify only one mask."));
11427
11428 use_mask = just_location = 1;
11429
11430 mark = value_mark ();
11431 mask_value = parse_to_comma_and_eval (&value_start);
11432 mask = value_as_address (mask_value);
11433 value_free_to_mark (mark);
11434 }
11435 else
11436 /* We didn't recognize what we found. We should stop here. */
11437 break;
11438
11439 /* Truncate the string and get rid of the "parameter value" pair before
11440 the arguments string is parsed by the parse_exp_1 function. */
11441 exp_end = tok;
11442 }
11443 }
11444 else
11445 exp_end = arg;
11446
11447 /* Parse the rest of the arguments. From here on out, everything
11448 is in terms of a newly allocated string instead of the original
11449 ARG. */
11450 innermost_block = NULL;
11451 expression = savestring (arg, exp_end - arg);
11452 back_to = make_cleanup (xfree, expression);
11453 exp_start = arg = expression;
11454 exp = parse_exp_1 (&arg, 0, 0, 0);
11455 exp_end = arg;
11456 /* Remove trailing whitespace from the expression before saving it.
11457 This makes the eventual display of the expression string a bit
11458 prettier. */
11459 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
11460 --exp_end;
11461
11462 /* Checking if the expression is not constant. */
11463 if (watchpoint_exp_is_const (exp))
11464 {
11465 int len;
11466
11467 len = exp_end - exp_start;
11468 while (len > 0 && isspace (exp_start[len - 1]))
11469 len--;
11470 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
11471 }
11472
11473 exp_valid_block = innermost_block;
11474 mark = value_mark ();
11475 fetch_subexp_value (exp, &pc, &val, &result, NULL, just_location);
11476
11477 if (val != NULL && just_location)
11478 {
11479 saved_bitpos = value_bitpos (val);
11480 saved_bitsize = value_bitsize (val);
11481 }
11482
11483 if (just_location)
11484 {
11485 int ret;
11486
11487 exp_valid_block = NULL;
11488 val = value_addr (result);
11489 release_value (val);
11490 value_free_to_mark (mark);
11491
11492 if (use_mask)
11493 {
11494 ret = target_masked_watch_num_registers (value_as_address (val),
11495 mask);
11496 if (ret == -1)
11497 error (_("This target does not support masked watchpoints."));
11498 else if (ret == -2)
11499 error (_("Invalid mask or memory region."));
11500 }
11501 }
11502 else if (val != NULL)
11503 release_value (val);
11504
11505 tok = skip_spaces_const (arg);
11506 end_tok = skip_to_space_const (tok);
11507
11508 toklen = end_tok - tok;
11509 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
11510 {
11511 struct expression *cond;
11512
11513 innermost_block = NULL;
11514 tok = cond_start = end_tok + 1;
11515 cond = parse_exp_1 (&tok, 0, 0, 0);
11516
11517 /* The watchpoint expression may not be local, but the condition
11518 may still be. E.g.: `watch global if local > 0'. */
11519 cond_exp_valid_block = innermost_block;
11520
11521 xfree (cond);
11522 cond_end = tok;
11523 }
11524 if (*tok)
11525 error (_("Junk at end of command."));
11526
11527 frame = block_innermost_frame (exp_valid_block);
11528
11529 /* If the expression is "local", then set up a "watchpoint scope"
11530 breakpoint at the point where we've left the scope of the watchpoint
11531 expression. Create the scope breakpoint before the watchpoint, so
11532 that we will encounter it first in bpstat_stop_status. */
11533 if (exp_valid_block && frame)
11534 {
11535 if (frame_id_p (frame_unwind_caller_id (frame)))
11536 {
11537 scope_breakpoint
11538 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
11539 frame_unwind_caller_pc (frame),
11540 bp_watchpoint_scope,
11541 &momentary_breakpoint_ops);
11542
11543 scope_breakpoint->enable_state = bp_enabled;
11544
11545 /* Automatically delete the breakpoint when it hits. */
11546 scope_breakpoint->disposition = disp_del;
11547
11548 /* Only break in the proper frame (help with recursion). */
11549 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
11550
11551 /* Set the address at which we will stop. */
11552 scope_breakpoint->loc->gdbarch
11553 = frame_unwind_caller_arch (frame);
11554 scope_breakpoint->loc->requested_address
11555 = frame_unwind_caller_pc (frame);
11556 scope_breakpoint->loc->address
11557 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
11558 scope_breakpoint->loc->requested_address,
11559 scope_breakpoint->type);
11560 }
11561 }
11562
11563 /* Now set up the breakpoint. We create all watchpoints as hardware
11564 watchpoints here even if hardware watchpoints are turned off, a call
11565 to update_watchpoint later in this function will cause the type to
11566 drop back to bp_watchpoint (software watchpoint) if required. */
11567
11568 if (accessflag == hw_read)
11569 bp_type = bp_read_watchpoint;
11570 else if (accessflag == hw_access)
11571 bp_type = bp_access_watchpoint;
11572 else
11573 bp_type = bp_hardware_watchpoint;
11574
11575 w = XCNEW (struct watchpoint);
11576 b = &w->base;
11577 if (use_mask)
11578 init_raw_breakpoint_without_location (b, NULL, bp_type,
11579 &masked_watchpoint_breakpoint_ops);
11580 else
11581 init_raw_breakpoint_without_location (b, NULL, bp_type,
11582 &watchpoint_breakpoint_ops);
11583 b->thread = thread;
11584 b->disposition = disp_donttouch;
11585 b->pspace = current_program_space;
11586 w->exp = exp;
11587 w->exp_valid_block = exp_valid_block;
11588 w->cond_exp_valid_block = cond_exp_valid_block;
11589 if (just_location)
11590 {
11591 struct type *t = value_type (val);
11592 CORE_ADDR addr = value_as_address (val);
11593 char *name;
11594
11595 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
11596 name = type_to_string (t);
11597
11598 w->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
11599 core_addr_to_string (addr));
11600 xfree (name);
11601
11602 w->exp_string = xstrprintf ("-location %.*s",
11603 (int) (exp_end - exp_start), exp_start);
11604
11605 /* The above expression is in C. */
11606 b->language = language_c;
11607 }
11608 else
11609 w->exp_string = savestring (exp_start, exp_end - exp_start);
11610
11611 if (use_mask)
11612 {
11613 w->hw_wp_mask = mask;
11614 }
11615 else
11616 {
11617 w->val = val;
11618 w->val_bitpos = saved_bitpos;
11619 w->val_bitsize = saved_bitsize;
11620 w->val_valid = 1;
11621 }
11622
11623 if (cond_start)
11624 b->cond_string = savestring (cond_start, cond_end - cond_start);
11625 else
11626 b->cond_string = 0;
11627
11628 if (frame)
11629 {
11630 w->watchpoint_frame = get_frame_id (frame);
11631 w->watchpoint_thread = inferior_ptid;
11632 }
11633 else
11634 {
11635 w->watchpoint_frame = null_frame_id;
11636 w->watchpoint_thread = null_ptid;
11637 }
11638
11639 if (scope_breakpoint != NULL)
11640 {
11641 /* The scope breakpoint is related to the watchpoint. We will
11642 need to act on them together. */
11643 b->related_breakpoint = scope_breakpoint;
11644 scope_breakpoint->related_breakpoint = b;
11645 }
11646
11647 if (!just_location)
11648 value_free_to_mark (mark);
11649
11650 TRY
11651 {
11652 /* Finally update the new watchpoint. This creates the locations
11653 that should be inserted. */
11654 update_watchpoint (w, 1);
11655 }
11656 CATCH (e, RETURN_MASK_ALL)
11657 {
11658 delete_breakpoint (b);
11659 throw_exception (e);
11660 }
11661 END_CATCH
11662
11663 install_breakpoint (internal, b, 1);
11664 do_cleanups (back_to);
11665 }
11666
11667 /* Return count of debug registers needed to watch the given expression.
11668 If the watchpoint cannot be handled in hardware return zero. */
11669
11670 static int
11671 can_use_hardware_watchpoint (struct value *v)
11672 {
11673 int found_memory_cnt = 0;
11674 struct value *head = v;
11675
11676 /* Did the user specifically forbid us to use hardware watchpoints? */
11677 if (!can_use_hw_watchpoints)
11678 return 0;
11679
11680 /* Make sure that the value of the expression depends only upon
11681 memory contents, and values computed from them within GDB. If we
11682 find any register references or function calls, we can't use a
11683 hardware watchpoint.
11684
11685 The idea here is that evaluating an expression generates a series
11686 of values, one holding the value of every subexpression. (The
11687 expression a*b+c has five subexpressions: a, b, a*b, c, and
11688 a*b+c.) GDB's values hold almost enough information to establish
11689 the criteria given above --- they identify memory lvalues,
11690 register lvalues, computed values, etcetera. So we can evaluate
11691 the expression, and then scan the chain of values that leaves
11692 behind to decide whether we can detect any possible change to the
11693 expression's final value using only hardware watchpoints.
11694
11695 However, I don't think that the values returned by inferior
11696 function calls are special in any way. So this function may not
11697 notice that an expression involving an inferior function call
11698 can't be watched with hardware watchpoints. FIXME. */
11699 for (; v; v = value_next (v))
11700 {
11701 if (VALUE_LVAL (v) == lval_memory)
11702 {
11703 if (v != head && value_lazy (v))
11704 /* A lazy memory lvalue in the chain is one that GDB never
11705 needed to fetch; we either just used its address (e.g.,
11706 `a' in `a.b') or we never needed it at all (e.g., `a'
11707 in `a,b'). This doesn't apply to HEAD; if that is
11708 lazy then it was not readable, but watch it anyway. */
11709 ;
11710 else
11711 {
11712 /* Ahh, memory we actually used! Check if we can cover
11713 it with hardware watchpoints. */
11714 struct type *vtype = check_typedef (value_type (v));
11715
11716 /* We only watch structs and arrays if user asked for it
11717 explicitly, never if they just happen to appear in a
11718 middle of some value chain. */
11719 if (v == head
11720 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11721 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11722 {
11723 CORE_ADDR vaddr = value_address (v);
11724 int len;
11725 int num_regs;
11726
11727 len = (target_exact_watchpoints
11728 && is_scalar_type_recursive (vtype))?
11729 1 : TYPE_LENGTH (value_type (v));
11730
11731 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11732 if (!num_regs)
11733 return 0;
11734 else
11735 found_memory_cnt += num_regs;
11736 }
11737 }
11738 }
11739 else if (VALUE_LVAL (v) != not_lval
11740 && deprecated_value_modifiable (v) == 0)
11741 return 0; /* These are values from the history (e.g., $1). */
11742 else if (VALUE_LVAL (v) == lval_register)
11743 return 0; /* Cannot watch a register with a HW watchpoint. */
11744 }
11745
11746 /* The expression itself looks suitable for using a hardware
11747 watchpoint, but give the target machine a chance to reject it. */
11748 return found_memory_cnt;
11749 }
11750
11751 void
11752 watch_command_wrapper (char *arg, int from_tty, int internal)
11753 {
11754 watch_command_1 (arg, hw_write, from_tty, 0, internal);
11755 }
11756
11757 /* A helper function that looks for the "-location" argument and then
11758 calls watch_command_1. */
11759
11760 static void
11761 watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11762 {
11763 int just_location = 0;
11764
11765 if (arg
11766 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11767 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11768 {
11769 arg = skip_spaces (arg);
11770 just_location = 1;
11771 }
11772
11773 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
11774 }
11775
11776 static void
11777 watch_command (char *arg, int from_tty)
11778 {
11779 watch_maybe_just_location (arg, hw_write, from_tty);
11780 }
11781
11782 void
11783 rwatch_command_wrapper (char *arg, int from_tty, int internal)
11784 {
11785 watch_command_1 (arg, hw_read, from_tty, 0, internal);
11786 }
11787
11788 static void
11789 rwatch_command (char *arg, int from_tty)
11790 {
11791 watch_maybe_just_location (arg, hw_read, from_tty);
11792 }
11793
11794 void
11795 awatch_command_wrapper (char *arg, int from_tty, int internal)
11796 {
11797 watch_command_1 (arg, hw_access, from_tty, 0, internal);
11798 }
11799
11800 static void
11801 awatch_command (char *arg, int from_tty)
11802 {
11803 watch_maybe_just_location (arg, hw_access, from_tty);
11804 }
11805 \f
11806
11807 /* Helper routines for the until_command routine in infcmd.c. Here
11808 because it uses the mechanisms of breakpoints. */
11809
11810 struct until_break_command_continuation_args
11811 {
11812 struct breakpoint *breakpoint;
11813 struct breakpoint *breakpoint2;
11814 int thread_num;
11815 };
11816
11817 /* This function is called by fetch_inferior_event via the
11818 cmd_continuation pointer, to complete the until command. It takes
11819 care of cleaning up the temporary breakpoints set up by the until
11820 command. */
11821 static void
11822 until_break_command_continuation (void *arg, int err)
11823 {
11824 struct until_break_command_continuation_args *a = arg;
11825
11826 delete_breakpoint (a->breakpoint);
11827 if (a->breakpoint2)
11828 delete_breakpoint (a->breakpoint2);
11829 delete_longjmp_breakpoint (a->thread_num);
11830 }
11831
11832 void
11833 until_break_command (char *arg, int from_tty, int anywhere)
11834 {
11835 struct symtabs_and_lines sals;
11836 struct symtab_and_line sal;
11837 struct frame_info *frame;
11838 struct gdbarch *frame_gdbarch;
11839 struct frame_id stack_frame_id;
11840 struct frame_id caller_frame_id;
11841 struct breakpoint *breakpoint;
11842 struct breakpoint *breakpoint2 = NULL;
11843 struct cleanup *old_chain;
11844 int thread;
11845 struct thread_info *tp;
11846
11847 clear_proceed_status (0);
11848
11849 /* Set a breakpoint where the user wants it and at return from
11850 this function. */
11851
11852 if (last_displayed_sal_is_valid ())
11853 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
11854 get_last_displayed_symtab (),
11855 get_last_displayed_line ());
11856 else
11857 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
11858 (struct symtab *) NULL, 0);
11859
11860 if (sals.nelts != 1)
11861 error (_("Couldn't get information on specified line."));
11862
11863 sal = sals.sals[0];
11864 xfree (sals.sals); /* malloc'd, so freed. */
11865
11866 if (*arg)
11867 error (_("Junk at end of arguments."));
11868
11869 resolve_sal_pc (&sal);
11870
11871 tp = inferior_thread ();
11872 thread = tp->num;
11873
11874 old_chain = make_cleanup (null_cleanup, NULL);
11875
11876 /* Note linespec handling above invalidates the frame chain.
11877 Installing a breakpoint also invalidates the frame chain (as it
11878 may need to switch threads), so do any frame handling before
11879 that. */
11880
11881 frame = get_selected_frame (NULL);
11882 frame_gdbarch = get_frame_arch (frame);
11883 stack_frame_id = get_stack_frame_id (frame);
11884 caller_frame_id = frame_unwind_caller_id (frame);
11885
11886 /* Keep within the current frame, or in frames called by the current
11887 one. */
11888
11889 if (frame_id_p (caller_frame_id))
11890 {
11891 struct symtab_and_line sal2;
11892
11893 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11894 sal2.pc = frame_unwind_caller_pc (frame);
11895 breakpoint2 = set_momentary_breakpoint (frame_unwind_caller_arch (frame),
11896 sal2,
11897 caller_frame_id,
11898 bp_until);
11899 make_cleanup_delete_breakpoint (breakpoint2);
11900
11901 set_longjmp_breakpoint (tp, caller_frame_id);
11902 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
11903 }
11904
11905 /* set_momentary_breakpoint could invalidate FRAME. */
11906 frame = NULL;
11907
11908 if (anywhere)
11909 /* If the user told us to continue until a specified location,
11910 we don't specify a frame at which we need to stop. */
11911 breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11912 null_frame_id, bp_until);
11913 else
11914 /* Otherwise, specify the selected frame, because we want to stop
11915 only at the very same frame. */
11916 breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11917 stack_frame_id, bp_until);
11918 make_cleanup_delete_breakpoint (breakpoint);
11919
11920 proceed (-1, GDB_SIGNAL_DEFAULT, 0);
11921
11922 /* If we are running asynchronously, and proceed call above has
11923 actually managed to start the target, arrange for breakpoints to
11924 be deleted when the target stops. Otherwise, we're already
11925 stopped and delete breakpoints via cleanup chain. */
11926
11927 if (target_can_async_p () && is_running (inferior_ptid))
11928 {
11929 struct until_break_command_continuation_args *args;
11930 args = xmalloc (sizeof (*args));
11931
11932 args->breakpoint = breakpoint;
11933 args->breakpoint2 = breakpoint2;
11934 args->thread_num = thread;
11935
11936 discard_cleanups (old_chain);
11937 add_continuation (inferior_thread (),
11938 until_break_command_continuation, args,
11939 xfree);
11940 }
11941 else
11942 do_cleanups (old_chain);
11943 }
11944
11945 /* This function attempts to parse an optional "if <cond>" clause
11946 from the arg string. If one is not found, it returns NULL.
11947
11948 Else, it returns a pointer to the condition string. (It does not
11949 attempt to evaluate the string against a particular block.) And,
11950 it updates arg to point to the first character following the parsed
11951 if clause in the arg string. */
11952
11953 char *
11954 ep_parse_optional_if_clause (char **arg)
11955 {
11956 char *cond_string;
11957
11958 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
11959 return NULL;
11960
11961 /* Skip the "if" keyword. */
11962 (*arg) += 2;
11963
11964 /* Skip any extra leading whitespace, and record the start of the
11965 condition string. */
11966 *arg = skip_spaces (*arg);
11967 cond_string = *arg;
11968
11969 /* Assume that the condition occupies the remainder of the arg
11970 string. */
11971 (*arg) += strlen (cond_string);
11972
11973 return cond_string;
11974 }
11975
11976 /* Commands to deal with catching events, such as signals, exceptions,
11977 process start/exit, etc. */
11978
11979 typedef enum
11980 {
11981 catch_fork_temporary, catch_vfork_temporary,
11982 catch_fork_permanent, catch_vfork_permanent
11983 }
11984 catch_fork_kind;
11985
11986 static void
11987 catch_fork_command_1 (char *arg, int from_tty,
11988 struct cmd_list_element *command)
11989 {
11990 struct gdbarch *gdbarch = get_current_arch ();
11991 char *cond_string = NULL;
11992 catch_fork_kind fork_kind;
11993 int tempflag;
11994
11995 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11996 tempflag = (fork_kind == catch_fork_temporary
11997 || fork_kind == catch_vfork_temporary);
11998
11999 if (!arg)
12000 arg = "";
12001 arg = skip_spaces (arg);
12002
12003 /* The allowed syntax is:
12004 catch [v]fork
12005 catch [v]fork if <cond>
12006
12007 First, check if there's an if clause. */
12008 cond_string = ep_parse_optional_if_clause (&arg);
12009
12010 if ((*arg != '\0') && !isspace (*arg))
12011 error (_("Junk at end of arguments."));
12012
12013 /* If this target supports it, create a fork or vfork catchpoint
12014 and enable reporting of such events. */
12015 switch (fork_kind)
12016 {
12017 case catch_fork_temporary:
12018 case catch_fork_permanent:
12019 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
12020 &catch_fork_breakpoint_ops);
12021 break;
12022 case catch_vfork_temporary:
12023 case catch_vfork_permanent:
12024 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
12025 &catch_vfork_breakpoint_ops);
12026 break;
12027 default:
12028 error (_("unsupported or unknown fork kind; cannot catch it"));
12029 break;
12030 }
12031 }
12032
12033 static void
12034 catch_exec_command_1 (char *arg, int from_tty,
12035 struct cmd_list_element *command)
12036 {
12037 struct exec_catchpoint *c;
12038 struct gdbarch *gdbarch = get_current_arch ();
12039 int tempflag;
12040 char *cond_string = NULL;
12041
12042 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
12043
12044 if (!arg)
12045 arg = "";
12046 arg = skip_spaces (arg);
12047
12048 /* The allowed syntax is:
12049 catch exec
12050 catch exec if <cond>
12051
12052 First, check if there's an if clause. */
12053 cond_string = ep_parse_optional_if_clause (&arg);
12054
12055 if ((*arg != '\0') && !isspace (*arg))
12056 error (_("Junk at end of arguments."));
12057
12058 c = XNEW (struct exec_catchpoint);
12059 init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
12060 &catch_exec_breakpoint_ops);
12061 c->exec_pathname = NULL;
12062
12063 install_breakpoint (0, &c->base, 1);
12064 }
12065
12066 void
12067 init_ada_exception_breakpoint (struct breakpoint *b,
12068 struct gdbarch *gdbarch,
12069 struct symtab_and_line sal,
12070 char *addr_string,
12071 const struct breakpoint_ops *ops,
12072 int tempflag,
12073 int enabled,
12074 int from_tty)
12075 {
12076 if (from_tty)
12077 {
12078 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
12079 if (!loc_gdbarch)
12080 loc_gdbarch = gdbarch;
12081
12082 describe_other_breakpoints (loc_gdbarch,
12083 sal.pspace, sal.pc, sal.section, -1);
12084 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
12085 version for exception catchpoints, because two catchpoints
12086 used for different exception names will use the same address.
12087 In this case, a "breakpoint ... also set at..." warning is
12088 unproductive. Besides, the warning phrasing is also a bit
12089 inappropriate, we should use the word catchpoint, and tell
12090 the user what type of catchpoint it is. The above is good
12091 enough for now, though. */
12092 }
12093
12094 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
12095
12096 b->enable_state = enabled ? bp_enabled : bp_disabled;
12097 b->disposition = tempflag ? disp_del : disp_donttouch;
12098 b->addr_string = addr_string;
12099 b->language = language_ada;
12100 }
12101
12102 /* Splits the argument using space as delimiter. Returns an xmalloc'd
12103 filter list, or NULL if no filtering is required. */
12104 static VEC(int) *
12105 catch_syscall_split_args (char *arg)
12106 {
12107 VEC(int) *result = NULL;
12108 struct cleanup *cleanup = make_cleanup (VEC_cleanup (int), &result);
12109 struct gdbarch *gdbarch = target_gdbarch ();
12110
12111 while (*arg != '\0')
12112 {
12113 int i, syscall_number;
12114 char *endptr;
12115 char cur_name[128];
12116 struct syscall s;
12117
12118 /* Skip whitespace. */
12119 arg = skip_spaces (arg);
12120
12121 for (i = 0; i < 127 && arg[i] && !isspace (arg[i]); ++i)
12122 cur_name[i] = arg[i];
12123 cur_name[i] = '\0';
12124 arg += i;
12125
12126 /* Check if the user provided a syscall name or a number. */
12127 syscall_number = (int) strtol (cur_name, &endptr, 0);
12128 if (*endptr == '\0')
12129 get_syscall_by_number (gdbarch, syscall_number, &s);
12130 else
12131 {
12132 /* We have a name. Let's check if it's valid and convert it
12133 to a number. */
12134 get_syscall_by_name (gdbarch, cur_name, &s);
12135
12136 if (s.number == UNKNOWN_SYSCALL)
12137 /* Here we have to issue an error instead of a warning,
12138 because GDB cannot do anything useful if there's no
12139 syscall number to be caught. */
12140 error (_("Unknown syscall name '%s'."), cur_name);
12141 }
12142
12143 /* Ok, it's valid. */
12144 VEC_safe_push (int, result, s.number);
12145 }
12146
12147 discard_cleanups (cleanup);
12148 return result;
12149 }
12150
12151 /* Implement the "catch syscall" command. */
12152
12153 static void
12154 catch_syscall_command_1 (char *arg, int from_tty,
12155 struct cmd_list_element *command)
12156 {
12157 int tempflag;
12158 VEC(int) *filter;
12159 struct syscall s;
12160 struct gdbarch *gdbarch = get_current_arch ();
12161
12162 /* Checking if the feature if supported. */
12163 if (gdbarch_get_syscall_number_p (gdbarch) == 0)
12164 error (_("The feature 'catch syscall' is not supported on \
12165 this architecture yet."));
12166
12167 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
12168
12169 arg = skip_spaces (arg);
12170
12171 /* We need to do this first "dummy" translation in order
12172 to get the syscall XML file loaded or, most important,
12173 to display a warning to the user if there's no XML file
12174 for his/her architecture. */
12175 get_syscall_by_number (gdbarch, 0, &s);
12176
12177 /* The allowed syntax is:
12178 catch syscall
12179 catch syscall <name | number> [<name | number> ... <name | number>]
12180
12181 Let's check if there's a syscall name. */
12182
12183 if (arg != NULL)
12184 filter = catch_syscall_split_args (arg);
12185 else
12186 filter = NULL;
12187
12188 create_syscall_event_catchpoint (tempflag, filter,
12189 &catch_syscall_breakpoint_ops);
12190 }
12191
12192 static void
12193 catch_command (char *arg, int from_tty)
12194 {
12195 error (_("Catch requires an event name."));
12196 }
12197 \f
12198
12199 static void
12200 tcatch_command (char *arg, int from_tty)
12201 {
12202 error (_("Catch requires an event name."));
12203 }
12204
12205 /* A qsort comparison function that sorts breakpoints in order. */
12206
12207 static int
12208 compare_breakpoints (const void *a, const void *b)
12209 {
12210 const breakpoint_p *ba = a;
12211 uintptr_t ua = (uintptr_t) *ba;
12212 const breakpoint_p *bb = b;
12213 uintptr_t ub = (uintptr_t) *bb;
12214
12215 if ((*ba)->number < (*bb)->number)
12216 return -1;
12217 else if ((*ba)->number > (*bb)->number)
12218 return 1;
12219
12220 /* Now sort by address, in case we see, e..g, two breakpoints with
12221 the number 0. */
12222 if (ua < ub)
12223 return -1;
12224 return ua > ub ? 1 : 0;
12225 }
12226
12227 /* Delete breakpoints by address or line. */
12228
12229 static void
12230 clear_command (char *arg, int from_tty)
12231 {
12232 struct breakpoint *b, *prev;
12233 VEC(breakpoint_p) *found = 0;
12234 int ix;
12235 int default_match;
12236 struct symtabs_and_lines sals;
12237 struct symtab_and_line sal;
12238 int i;
12239 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
12240
12241 if (arg)
12242 {
12243 sals = decode_line_with_current_source (arg,
12244 (DECODE_LINE_FUNFIRSTLINE
12245 | DECODE_LINE_LIST_MODE));
12246 make_cleanup (xfree, sals.sals);
12247 default_match = 0;
12248 }
12249 else
12250 {
12251 sals.sals = (struct symtab_and_line *)
12252 xmalloc (sizeof (struct symtab_and_line));
12253 make_cleanup (xfree, sals.sals);
12254 init_sal (&sal); /* Initialize to zeroes. */
12255
12256 /* Set sal's line, symtab, pc, and pspace to the values
12257 corresponding to the last call to print_frame_info. If the
12258 codepoint is not valid, this will set all the fields to 0. */
12259 get_last_displayed_sal (&sal);
12260 if (sal.symtab == 0)
12261 error (_("No source file specified."));
12262
12263 sals.sals[0] = sal;
12264 sals.nelts = 1;
12265
12266 default_match = 1;
12267 }
12268
12269 /* We don't call resolve_sal_pc here. That's not as bad as it
12270 seems, because all existing breakpoints typically have both
12271 file/line and pc set. So, if clear is given file/line, we can
12272 match this to existing breakpoint without obtaining pc at all.
12273
12274 We only support clearing given the address explicitly
12275 present in breakpoint table. Say, we've set breakpoint
12276 at file:line. There were several PC values for that file:line,
12277 due to optimization, all in one block.
12278
12279 We've picked one PC value. If "clear" is issued with another
12280 PC corresponding to the same file:line, the breakpoint won't
12281 be cleared. We probably can still clear the breakpoint, but
12282 since the other PC value is never presented to user, user
12283 can only find it by guessing, and it does not seem important
12284 to support that. */
12285
12286 /* For each line spec given, delete bps which correspond to it. Do
12287 it in two passes, solely to preserve the current behavior that
12288 from_tty is forced true if we delete more than one
12289 breakpoint. */
12290
12291 found = NULL;
12292 make_cleanup (VEC_cleanup (breakpoint_p), &found);
12293 for (i = 0; i < sals.nelts; i++)
12294 {
12295 const char *sal_fullname;
12296
12297 /* If exact pc given, clear bpts at that pc.
12298 If line given (pc == 0), clear all bpts on specified line.
12299 If defaulting, clear all bpts on default line
12300 or at default pc.
12301
12302 defaulting sal.pc != 0 tests to do
12303
12304 0 1 pc
12305 1 1 pc _and_ line
12306 0 0 line
12307 1 0 <can't happen> */
12308
12309 sal = sals.sals[i];
12310 sal_fullname = (sal.symtab == NULL
12311 ? NULL : symtab_to_fullname (sal.symtab));
12312
12313 /* Find all matching breakpoints and add them to 'found'. */
12314 ALL_BREAKPOINTS (b)
12315 {
12316 int match = 0;
12317 /* Are we going to delete b? */
12318 if (b->type != bp_none && !is_watchpoint (b))
12319 {
12320 struct bp_location *loc = b->loc;
12321 for (; loc; loc = loc->next)
12322 {
12323 /* If the user specified file:line, don't allow a PC
12324 match. This matches historical gdb behavior. */
12325 int pc_match = (!sal.explicit_line
12326 && sal.pc
12327 && (loc->pspace == sal.pspace)
12328 && (loc->address == sal.pc)
12329 && (!section_is_overlay (loc->section)
12330 || loc->section == sal.section));
12331 int line_match = 0;
12332
12333 if ((default_match || sal.explicit_line)
12334 && loc->symtab != NULL
12335 && sal_fullname != NULL
12336 && sal.pspace == loc->pspace
12337 && loc->line_number == sal.line
12338 && filename_cmp (symtab_to_fullname (loc->symtab),
12339 sal_fullname) == 0)
12340 line_match = 1;
12341
12342 if (pc_match || line_match)
12343 {
12344 match = 1;
12345 break;
12346 }
12347 }
12348 }
12349
12350 if (match)
12351 VEC_safe_push(breakpoint_p, found, b);
12352 }
12353 }
12354
12355 /* Now go thru the 'found' chain and delete them. */
12356 if (VEC_empty(breakpoint_p, found))
12357 {
12358 if (arg)
12359 error (_("No breakpoint at %s."), arg);
12360 else
12361 error (_("No breakpoint at this line."));
12362 }
12363
12364 /* Remove duplicates from the vec. */
12365 qsort (VEC_address (breakpoint_p, found),
12366 VEC_length (breakpoint_p, found),
12367 sizeof (breakpoint_p),
12368 compare_breakpoints);
12369 prev = VEC_index (breakpoint_p, found, 0);
12370 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
12371 {
12372 if (b == prev)
12373 {
12374 VEC_ordered_remove (breakpoint_p, found, ix);
12375 --ix;
12376 }
12377 }
12378
12379 if (VEC_length(breakpoint_p, found) > 1)
12380 from_tty = 1; /* Always report if deleted more than one. */
12381 if (from_tty)
12382 {
12383 if (VEC_length(breakpoint_p, found) == 1)
12384 printf_unfiltered (_("Deleted breakpoint "));
12385 else
12386 printf_unfiltered (_("Deleted breakpoints "));
12387 }
12388
12389 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
12390 {
12391 if (from_tty)
12392 printf_unfiltered ("%d ", b->number);
12393 delete_breakpoint (b);
12394 }
12395 if (from_tty)
12396 putchar_unfiltered ('\n');
12397
12398 do_cleanups (cleanups);
12399 }
12400 \f
12401 /* Delete breakpoint in BS if they are `delete' breakpoints and
12402 all breakpoints that are marked for deletion, whether hit or not.
12403 This is called after any breakpoint is hit, or after errors. */
12404
12405 void
12406 breakpoint_auto_delete (bpstat bs)
12407 {
12408 struct breakpoint *b, *b_tmp;
12409
12410 for (; bs; bs = bs->next)
12411 if (bs->breakpoint_at
12412 && bs->breakpoint_at->disposition == disp_del
12413 && bs->stop)
12414 delete_breakpoint (bs->breakpoint_at);
12415
12416 ALL_BREAKPOINTS_SAFE (b, b_tmp)
12417 {
12418 if (b->disposition == disp_del_at_next_stop)
12419 delete_breakpoint (b);
12420 }
12421 }
12422
12423 /* A comparison function for bp_location AP and BP being interfaced to
12424 qsort. Sort elements primarily by their ADDRESS (no matter what
12425 does breakpoint_address_is_meaningful say for its OWNER),
12426 secondarily by ordering first permanent elements and
12427 terciarily just ensuring the array is sorted stable way despite
12428 qsort being an unstable algorithm. */
12429
12430 static int
12431 bp_location_compare (const void *ap, const void *bp)
12432 {
12433 struct bp_location *a = *(void **) ap;
12434 struct bp_location *b = *(void **) bp;
12435
12436 if (a->address != b->address)
12437 return (a->address > b->address) - (a->address < b->address);
12438
12439 /* Sort locations at the same address by their pspace number, keeping
12440 locations of the same inferior (in a multi-inferior environment)
12441 grouped. */
12442
12443 if (a->pspace->num != b->pspace->num)
12444 return ((a->pspace->num > b->pspace->num)
12445 - (a->pspace->num < b->pspace->num));
12446
12447 /* Sort permanent breakpoints first. */
12448 if (a->permanent != b->permanent)
12449 return (a->permanent < b->permanent) - (a->permanent > b->permanent);
12450
12451 /* Make the internal GDB representation stable across GDB runs
12452 where A and B memory inside GDB can differ. Breakpoint locations of
12453 the same type at the same address can be sorted in arbitrary order. */
12454
12455 if (a->owner->number != b->owner->number)
12456 return ((a->owner->number > b->owner->number)
12457 - (a->owner->number < b->owner->number));
12458
12459 return (a > b) - (a < b);
12460 }
12461
12462 /* Set bp_location_placed_address_before_address_max and
12463 bp_location_shadow_len_after_address_max according to the current
12464 content of the bp_location array. */
12465
12466 static void
12467 bp_location_target_extensions_update (void)
12468 {
12469 struct bp_location *bl, **blp_tmp;
12470
12471 bp_location_placed_address_before_address_max = 0;
12472 bp_location_shadow_len_after_address_max = 0;
12473
12474 ALL_BP_LOCATIONS (bl, blp_tmp)
12475 {
12476 CORE_ADDR start, end, addr;
12477
12478 if (!bp_location_has_shadow (bl))
12479 continue;
12480
12481 start = bl->target_info.placed_address;
12482 end = start + bl->target_info.shadow_len;
12483
12484 gdb_assert (bl->address >= start);
12485 addr = bl->address - start;
12486 if (addr > bp_location_placed_address_before_address_max)
12487 bp_location_placed_address_before_address_max = addr;
12488
12489 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
12490
12491 gdb_assert (bl->address < end);
12492 addr = end - bl->address;
12493 if (addr > bp_location_shadow_len_after_address_max)
12494 bp_location_shadow_len_after_address_max = addr;
12495 }
12496 }
12497
12498 /* Download tracepoint locations if they haven't been. */
12499
12500 static void
12501 download_tracepoint_locations (void)
12502 {
12503 struct breakpoint *b;
12504 struct cleanup *old_chain;
12505
12506 if (!target_can_download_tracepoint ())
12507 return;
12508
12509 old_chain = save_current_space_and_thread ();
12510
12511 ALL_TRACEPOINTS (b)
12512 {
12513 struct bp_location *bl;
12514 struct tracepoint *t;
12515 int bp_location_downloaded = 0;
12516
12517 if ((b->type == bp_fast_tracepoint
12518 ? !may_insert_fast_tracepoints
12519 : !may_insert_tracepoints))
12520 continue;
12521
12522 for (bl = b->loc; bl; bl = bl->next)
12523 {
12524 /* In tracepoint, locations are _never_ duplicated, so
12525 should_be_inserted is equivalent to
12526 unduplicated_should_be_inserted. */
12527 if (!should_be_inserted (bl) || bl->inserted)
12528 continue;
12529
12530 switch_to_program_space_and_thread (bl->pspace);
12531
12532 target_download_tracepoint (bl);
12533
12534 bl->inserted = 1;
12535 bp_location_downloaded = 1;
12536 }
12537 t = (struct tracepoint *) b;
12538 t->number_on_target = b->number;
12539 if (bp_location_downloaded)
12540 observer_notify_breakpoint_modified (b);
12541 }
12542
12543 do_cleanups (old_chain);
12544 }
12545
12546 /* Swap the insertion/duplication state between two locations. */
12547
12548 static void
12549 swap_insertion (struct bp_location *left, struct bp_location *right)
12550 {
12551 const int left_inserted = left->inserted;
12552 const int left_duplicate = left->duplicate;
12553 const int left_needs_update = left->needs_update;
12554 const struct bp_target_info left_target_info = left->target_info;
12555
12556 /* Locations of tracepoints can never be duplicated. */
12557 if (is_tracepoint (left->owner))
12558 gdb_assert (!left->duplicate);
12559 if (is_tracepoint (right->owner))
12560 gdb_assert (!right->duplicate);
12561
12562 left->inserted = right->inserted;
12563 left->duplicate = right->duplicate;
12564 left->needs_update = right->needs_update;
12565 left->target_info = right->target_info;
12566 right->inserted = left_inserted;
12567 right->duplicate = left_duplicate;
12568 right->needs_update = left_needs_update;
12569 right->target_info = left_target_info;
12570 }
12571
12572 /* Force the re-insertion of the locations at ADDRESS. This is called
12573 once a new/deleted/modified duplicate location is found and we are evaluating
12574 conditions on the target's side. Such conditions need to be updated on
12575 the target. */
12576
12577 static void
12578 force_breakpoint_reinsertion (struct bp_location *bl)
12579 {
12580 struct bp_location **locp = NULL, **loc2p;
12581 struct bp_location *loc;
12582 CORE_ADDR address = 0;
12583 int pspace_num;
12584
12585 address = bl->address;
12586 pspace_num = bl->pspace->num;
12587
12588 /* This is only meaningful if the target is
12589 evaluating conditions and if the user has
12590 opted for condition evaluation on the target's
12591 side. */
12592 if (gdb_evaluates_breakpoint_condition_p ()
12593 || !target_supports_evaluation_of_breakpoint_conditions ())
12594 return;
12595
12596 /* Flag all breakpoint locations with this address and
12597 the same program space as the location
12598 as "its condition has changed". We need to
12599 update the conditions on the target's side. */
12600 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
12601 {
12602 loc = *loc2p;
12603
12604 if (!is_breakpoint (loc->owner)
12605 || pspace_num != loc->pspace->num)
12606 continue;
12607
12608 /* Flag the location appropriately. We use a different state to
12609 let everyone know that we already updated the set of locations
12610 with addr bl->address and program space bl->pspace. This is so
12611 we don't have to keep calling these functions just to mark locations
12612 that have already been marked. */
12613 loc->condition_changed = condition_updated;
12614
12615 /* Free the agent expression bytecode as well. We will compute
12616 it later on. */
12617 if (loc->cond_bytecode)
12618 {
12619 free_agent_expr (loc->cond_bytecode);
12620 loc->cond_bytecode = NULL;
12621 }
12622 }
12623 }
12624 /* Called whether new breakpoints are created, or existing breakpoints
12625 deleted, to update the global location list and recompute which
12626 locations are duplicate of which.
12627
12628 The INSERT_MODE flag determines whether locations may not, may, or
12629 shall be inserted now. See 'enum ugll_insert_mode' for more
12630 info. */
12631
12632 static void
12633 update_global_location_list (enum ugll_insert_mode insert_mode)
12634 {
12635 struct breakpoint *b;
12636 struct bp_location **locp, *loc;
12637 struct cleanup *cleanups;
12638 /* Last breakpoint location address that was marked for update. */
12639 CORE_ADDR last_addr = 0;
12640 /* Last breakpoint location program space that was marked for update. */
12641 int last_pspace_num = -1;
12642
12643 /* Used in the duplicates detection below. When iterating over all
12644 bp_locations, points to the first bp_location of a given address.
12645 Breakpoints and watchpoints of different types are never
12646 duplicates of each other. Keep one pointer for each type of
12647 breakpoint/watchpoint, so we only need to loop over all locations
12648 once. */
12649 struct bp_location *bp_loc_first; /* breakpoint */
12650 struct bp_location *wp_loc_first; /* hardware watchpoint */
12651 struct bp_location *awp_loc_first; /* access watchpoint */
12652 struct bp_location *rwp_loc_first; /* read watchpoint */
12653
12654 /* Saved former bp_location array which we compare against the newly
12655 built bp_location from the current state of ALL_BREAKPOINTS. */
12656 struct bp_location **old_location, **old_locp;
12657 unsigned old_location_count;
12658
12659 old_location = bp_location;
12660 old_location_count = bp_location_count;
12661 bp_location = NULL;
12662 bp_location_count = 0;
12663 cleanups = make_cleanup (xfree, old_location);
12664
12665 ALL_BREAKPOINTS (b)
12666 for (loc = b->loc; loc; loc = loc->next)
12667 bp_location_count++;
12668
12669 bp_location = xmalloc (sizeof (*bp_location) * bp_location_count);
12670 locp = bp_location;
12671 ALL_BREAKPOINTS (b)
12672 for (loc = b->loc; loc; loc = loc->next)
12673 *locp++ = loc;
12674 qsort (bp_location, bp_location_count, sizeof (*bp_location),
12675 bp_location_compare);
12676
12677 bp_location_target_extensions_update ();
12678
12679 /* Identify bp_location instances that are no longer present in the
12680 new list, and therefore should be freed. Note that it's not
12681 necessary that those locations should be removed from inferior --
12682 if there's another location at the same address (previously
12683 marked as duplicate), we don't need to remove/insert the
12684 location.
12685
12686 LOCP is kept in sync with OLD_LOCP, each pointing to the current
12687 and former bp_location array state respectively. */
12688
12689 locp = bp_location;
12690 for (old_locp = old_location; old_locp < old_location + old_location_count;
12691 old_locp++)
12692 {
12693 struct bp_location *old_loc = *old_locp;
12694 struct bp_location **loc2p;
12695
12696 /* Tells if 'old_loc' is found among the new locations. If
12697 not, we have to free it. */
12698 int found_object = 0;
12699 /* Tells if the location should remain inserted in the target. */
12700 int keep_in_target = 0;
12701 int removed = 0;
12702
12703 /* Skip LOCP entries which will definitely never be needed.
12704 Stop either at or being the one matching OLD_LOC. */
12705 while (locp < bp_location + bp_location_count
12706 && (*locp)->address < old_loc->address)
12707 locp++;
12708
12709 for (loc2p = locp;
12710 (loc2p < bp_location + bp_location_count
12711 && (*loc2p)->address == old_loc->address);
12712 loc2p++)
12713 {
12714 /* Check if this is a new/duplicated location or a duplicated
12715 location that had its condition modified. If so, we want to send
12716 its condition to the target if evaluation of conditions is taking
12717 place there. */
12718 if ((*loc2p)->condition_changed == condition_modified
12719 && (last_addr != old_loc->address
12720 || last_pspace_num != old_loc->pspace->num))
12721 {
12722 force_breakpoint_reinsertion (*loc2p);
12723 last_pspace_num = old_loc->pspace->num;
12724 }
12725
12726 if (*loc2p == old_loc)
12727 found_object = 1;
12728 }
12729
12730 /* We have already handled this address, update it so that we don't
12731 have to go through updates again. */
12732 last_addr = old_loc->address;
12733
12734 /* Target-side condition evaluation: Handle deleted locations. */
12735 if (!found_object)
12736 force_breakpoint_reinsertion (old_loc);
12737
12738 /* If this location is no longer present, and inserted, look if
12739 there's maybe a new location at the same address. If so,
12740 mark that one inserted, and don't remove this one. This is
12741 needed so that we don't have a time window where a breakpoint
12742 at certain location is not inserted. */
12743
12744 if (old_loc->inserted)
12745 {
12746 /* If the location is inserted now, we might have to remove
12747 it. */
12748
12749 if (found_object && should_be_inserted (old_loc))
12750 {
12751 /* The location is still present in the location list,
12752 and still should be inserted. Don't do anything. */
12753 keep_in_target = 1;
12754 }
12755 else
12756 {
12757 /* This location still exists, but it won't be kept in the
12758 target since it may have been disabled. We proceed to
12759 remove its target-side condition. */
12760
12761 /* The location is either no longer present, or got
12762 disabled. See if there's another location at the
12763 same address, in which case we don't need to remove
12764 this one from the target. */
12765
12766 /* OLD_LOC comes from existing struct breakpoint. */
12767 if (breakpoint_address_is_meaningful (old_loc->owner))
12768 {
12769 for (loc2p = locp;
12770 (loc2p < bp_location + bp_location_count
12771 && (*loc2p)->address == old_loc->address);
12772 loc2p++)
12773 {
12774 struct bp_location *loc2 = *loc2p;
12775
12776 if (breakpoint_locations_match (loc2, old_loc))
12777 {
12778 /* Read watchpoint locations are switched to
12779 access watchpoints, if the former are not
12780 supported, but the latter are. */
12781 if (is_hardware_watchpoint (old_loc->owner))
12782 {
12783 gdb_assert (is_hardware_watchpoint (loc2->owner));
12784 loc2->watchpoint_type = old_loc->watchpoint_type;
12785 }
12786
12787 /* loc2 is a duplicated location. We need to check
12788 if it should be inserted in case it will be
12789 unduplicated. */
12790 if (loc2 != old_loc
12791 && unduplicated_should_be_inserted (loc2))
12792 {
12793 swap_insertion (old_loc, loc2);
12794 keep_in_target = 1;
12795 break;
12796 }
12797 }
12798 }
12799 }
12800 }
12801
12802 if (!keep_in_target)
12803 {
12804 if (remove_breakpoint (old_loc, mark_uninserted))
12805 {
12806 /* This is just about all we can do. We could keep
12807 this location on the global list, and try to
12808 remove it next time, but there's no particular
12809 reason why we will succeed next time.
12810
12811 Note that at this point, old_loc->owner is still
12812 valid, as delete_breakpoint frees the breakpoint
12813 only after calling us. */
12814 printf_filtered (_("warning: Error removing "
12815 "breakpoint %d\n"),
12816 old_loc->owner->number);
12817 }
12818 removed = 1;
12819 }
12820 }
12821
12822 if (!found_object)
12823 {
12824 if (removed && non_stop
12825 && need_moribund_for_location_type (old_loc))
12826 {
12827 /* This location was removed from the target. In
12828 non-stop mode, a race condition is possible where
12829 we've removed a breakpoint, but stop events for that
12830 breakpoint are already queued and will arrive later.
12831 We apply an heuristic to be able to distinguish such
12832 SIGTRAPs from other random SIGTRAPs: we keep this
12833 breakpoint location for a bit, and will retire it
12834 after we see some number of events. The theory here
12835 is that reporting of events should, "on the average",
12836 be fair, so after a while we'll see events from all
12837 threads that have anything of interest, and no longer
12838 need to keep this breakpoint location around. We
12839 don't hold locations forever so to reduce chances of
12840 mistaking a non-breakpoint SIGTRAP for a breakpoint
12841 SIGTRAP.
12842
12843 The heuristic failing can be disastrous on
12844 decr_pc_after_break targets.
12845
12846 On decr_pc_after_break targets, like e.g., x86-linux,
12847 if we fail to recognize a late breakpoint SIGTRAP,
12848 because events_till_retirement has reached 0 too
12849 soon, we'll fail to do the PC adjustment, and report
12850 a random SIGTRAP to the user. When the user resumes
12851 the inferior, it will most likely immediately crash
12852 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
12853 corrupted, because of being resumed e.g., in the
12854 middle of a multi-byte instruction, or skipped a
12855 one-byte instruction. This was actually seen happen
12856 on native x86-linux, and should be less rare on
12857 targets that do not support new thread events, like
12858 remote, due to the heuristic depending on
12859 thread_count.
12860
12861 Mistaking a random SIGTRAP for a breakpoint trap
12862 causes similar symptoms (PC adjustment applied when
12863 it shouldn't), but then again, playing with SIGTRAPs
12864 behind the debugger's back is asking for trouble.
12865
12866 Since hardware watchpoint traps are always
12867 distinguishable from other traps, so we don't need to
12868 apply keep hardware watchpoint moribund locations
12869 around. We simply always ignore hardware watchpoint
12870 traps we can no longer explain. */
12871
12872 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12873 old_loc->owner = NULL;
12874
12875 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
12876 }
12877 else
12878 {
12879 old_loc->owner = NULL;
12880 decref_bp_location (&old_loc);
12881 }
12882 }
12883 }
12884
12885 /* Rescan breakpoints at the same address and section, marking the
12886 first one as "first" and any others as "duplicates". This is so
12887 that the bpt instruction is only inserted once. If we have a
12888 permanent breakpoint at the same place as BPT, make that one the
12889 official one, and the rest as duplicates. Permanent breakpoints
12890 are sorted first for the same address.
12891
12892 Do the same for hardware watchpoints, but also considering the
12893 watchpoint's type (regular/access/read) and length. */
12894
12895 bp_loc_first = NULL;
12896 wp_loc_first = NULL;
12897 awp_loc_first = NULL;
12898 rwp_loc_first = NULL;
12899 ALL_BP_LOCATIONS (loc, locp)
12900 {
12901 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12902 non-NULL. */
12903 struct bp_location **loc_first_p;
12904 b = loc->owner;
12905
12906 if (!unduplicated_should_be_inserted (loc)
12907 || !breakpoint_address_is_meaningful (b)
12908 /* Don't detect duplicate for tracepoint locations because they are
12909 never duplicated. See the comments in field `duplicate' of
12910 `struct bp_location'. */
12911 || is_tracepoint (b))
12912 {
12913 /* Clear the condition modification flag. */
12914 loc->condition_changed = condition_unchanged;
12915 continue;
12916 }
12917
12918 /* Permanent breakpoint should always be inserted. */
12919 if (loc->permanent && ! loc->inserted)
12920 internal_error (__FILE__, __LINE__,
12921 _("allegedly permanent breakpoint is not "
12922 "actually inserted"));
12923
12924 if (b->type == bp_hardware_watchpoint)
12925 loc_first_p = &wp_loc_first;
12926 else if (b->type == bp_read_watchpoint)
12927 loc_first_p = &rwp_loc_first;
12928 else if (b->type == bp_access_watchpoint)
12929 loc_first_p = &awp_loc_first;
12930 else
12931 loc_first_p = &bp_loc_first;
12932
12933 if (*loc_first_p == NULL
12934 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12935 || !breakpoint_locations_match (loc, *loc_first_p))
12936 {
12937 *loc_first_p = loc;
12938 loc->duplicate = 0;
12939
12940 if (is_breakpoint (loc->owner) && loc->condition_changed)
12941 {
12942 loc->needs_update = 1;
12943 /* Clear the condition modification flag. */
12944 loc->condition_changed = condition_unchanged;
12945 }
12946 continue;
12947 }
12948
12949
12950 /* This and the above ensure the invariant that the first location
12951 is not duplicated, and is the inserted one.
12952 All following are marked as duplicated, and are not inserted. */
12953 if (loc->inserted)
12954 swap_insertion (loc, *loc_first_p);
12955 loc->duplicate = 1;
12956
12957 /* Clear the condition modification flag. */
12958 loc->condition_changed = condition_unchanged;
12959
12960 if (loc->inserted && !loc->permanent
12961 && (*loc_first_p)->permanent)
12962 internal_error (__FILE__, __LINE__,
12963 _("another breakpoint was inserted on top of "
12964 "a permanent breakpoint"));
12965 }
12966
12967 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
12968 {
12969 if (insert_mode != UGLL_DONT_INSERT)
12970 insert_breakpoint_locations ();
12971 else
12972 {
12973 /* Even though the caller told us to not insert new
12974 locations, we may still need to update conditions on the
12975 target's side of breakpoints that were already inserted
12976 if the target is evaluating breakpoint conditions. We
12977 only update conditions for locations that are marked
12978 "needs_update". */
12979 update_inserted_breakpoint_locations ();
12980 }
12981 }
12982
12983 if (insert_mode != UGLL_DONT_INSERT)
12984 download_tracepoint_locations ();
12985
12986 do_cleanups (cleanups);
12987 }
12988
12989 void
12990 breakpoint_retire_moribund (void)
12991 {
12992 struct bp_location *loc;
12993 int ix;
12994
12995 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12996 if (--(loc->events_till_retirement) == 0)
12997 {
12998 decref_bp_location (&loc);
12999 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
13000 --ix;
13001 }
13002 }
13003
13004 static void
13005 update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
13006 {
13007
13008 TRY
13009 {
13010 update_global_location_list (insert_mode);
13011 }
13012 CATCH (e, RETURN_MASK_ERROR)
13013 {
13014 }
13015 END_CATCH
13016 }
13017
13018 /* Clear BKP from a BPS. */
13019
13020 static void
13021 bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
13022 {
13023 bpstat bs;
13024
13025 for (bs = bps; bs; bs = bs->next)
13026 if (bs->breakpoint_at == bpt)
13027 {
13028 bs->breakpoint_at = NULL;
13029 bs->old_val = NULL;
13030 /* bs->commands will be freed later. */
13031 }
13032 }
13033
13034 /* Callback for iterate_over_threads. */
13035 static int
13036 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
13037 {
13038 struct breakpoint *bpt = data;
13039
13040 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
13041 return 0;
13042 }
13043
13044 /* Helper for breakpoint and tracepoint breakpoint_ops->mention
13045 callbacks. */
13046
13047 static void
13048 say_where (struct breakpoint *b)
13049 {
13050 struct value_print_options opts;
13051
13052 get_user_print_options (&opts);
13053
13054 /* i18n: cagney/2005-02-11: Below needs to be merged into a
13055 single string. */
13056 if (b->loc == NULL)
13057 {
13058 printf_filtered (_(" (%s) pending."), b->addr_string);
13059 }
13060 else
13061 {
13062 if (opts.addressprint || b->loc->symtab == NULL)
13063 {
13064 printf_filtered (" at ");
13065 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
13066 gdb_stdout);
13067 }
13068 if (b->loc->symtab != NULL)
13069 {
13070 /* If there is a single location, we can print the location
13071 more nicely. */
13072 if (b->loc->next == NULL)
13073 printf_filtered (": file %s, line %d.",
13074 symtab_to_filename_for_display (b->loc->symtab),
13075 b->loc->line_number);
13076 else
13077 /* This is not ideal, but each location may have a
13078 different file name, and this at least reflects the
13079 real situation somewhat. */
13080 printf_filtered (": %s.", b->addr_string);
13081 }
13082
13083 if (b->loc->next)
13084 {
13085 struct bp_location *loc = b->loc;
13086 int n = 0;
13087 for (; loc; loc = loc->next)
13088 ++n;
13089 printf_filtered (" (%d locations)", n);
13090 }
13091 }
13092 }
13093
13094 /* Default bp_location_ops methods. */
13095
13096 static void
13097 bp_location_dtor (struct bp_location *self)
13098 {
13099 xfree (self->cond);
13100 if (self->cond_bytecode)
13101 free_agent_expr (self->cond_bytecode);
13102 xfree (self->function_name);
13103
13104 VEC_free (agent_expr_p, self->target_info.conditions);
13105 VEC_free (agent_expr_p, self->target_info.tcommands);
13106 }
13107
13108 static const struct bp_location_ops bp_location_ops =
13109 {
13110 bp_location_dtor
13111 };
13112
13113 /* Default breakpoint_ops methods all breakpoint_ops ultimately
13114 inherit from. */
13115
13116 static void
13117 base_breakpoint_dtor (struct breakpoint *self)
13118 {
13119 decref_counted_command_line (&self->commands);
13120 xfree (self->cond_string);
13121 xfree (self->extra_string);
13122 xfree (self->addr_string);
13123 xfree (self->filter);
13124 xfree (self->addr_string_range_end);
13125 }
13126
13127 static struct bp_location *
13128 base_breakpoint_allocate_location (struct breakpoint *self)
13129 {
13130 struct bp_location *loc;
13131
13132 loc = XNEW (struct bp_location);
13133 init_bp_location (loc, &bp_location_ops, self);
13134 return loc;
13135 }
13136
13137 static void
13138 base_breakpoint_re_set (struct breakpoint *b)
13139 {
13140 /* Nothing to re-set. */
13141 }
13142
13143 #define internal_error_pure_virtual_called() \
13144 gdb_assert_not_reached ("pure virtual function called")
13145
13146 static int
13147 base_breakpoint_insert_location (struct bp_location *bl)
13148 {
13149 internal_error_pure_virtual_called ();
13150 }
13151
13152 static int
13153 base_breakpoint_remove_location (struct bp_location *bl)
13154 {
13155 internal_error_pure_virtual_called ();
13156 }
13157
13158 static int
13159 base_breakpoint_breakpoint_hit (const struct bp_location *bl,
13160 struct address_space *aspace,
13161 CORE_ADDR bp_addr,
13162 const struct target_waitstatus *ws)
13163 {
13164 internal_error_pure_virtual_called ();
13165 }
13166
13167 static void
13168 base_breakpoint_check_status (bpstat bs)
13169 {
13170 /* Always stop. */
13171 }
13172
13173 /* A "works_in_software_mode" breakpoint_ops method that just internal
13174 errors. */
13175
13176 static int
13177 base_breakpoint_works_in_software_mode (const struct breakpoint *b)
13178 {
13179 internal_error_pure_virtual_called ();
13180 }
13181
13182 /* A "resources_needed" breakpoint_ops method that just internal
13183 errors. */
13184
13185 static int
13186 base_breakpoint_resources_needed (const struct bp_location *bl)
13187 {
13188 internal_error_pure_virtual_called ();
13189 }
13190
13191 static enum print_stop_action
13192 base_breakpoint_print_it (bpstat bs)
13193 {
13194 internal_error_pure_virtual_called ();
13195 }
13196
13197 static void
13198 base_breakpoint_print_one_detail (const struct breakpoint *self,
13199 struct ui_out *uiout)
13200 {
13201 /* nothing */
13202 }
13203
13204 static void
13205 base_breakpoint_print_mention (struct breakpoint *b)
13206 {
13207 internal_error_pure_virtual_called ();
13208 }
13209
13210 static void
13211 base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
13212 {
13213 internal_error_pure_virtual_called ();
13214 }
13215
13216 static void
13217 base_breakpoint_create_sals_from_address (char **arg,
13218 struct linespec_result *canonical,
13219 enum bptype type_wanted,
13220 char *addr_start,
13221 char **copy_arg)
13222 {
13223 internal_error_pure_virtual_called ();
13224 }
13225
13226 static void
13227 base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13228 struct linespec_result *c,
13229 char *cond_string,
13230 char *extra_string,
13231 enum bptype type_wanted,
13232 enum bpdisp disposition,
13233 int thread,
13234 int task, int ignore_count,
13235 const struct breakpoint_ops *o,
13236 int from_tty, int enabled,
13237 int internal, unsigned flags)
13238 {
13239 internal_error_pure_virtual_called ();
13240 }
13241
13242 static void
13243 base_breakpoint_decode_linespec (struct breakpoint *b, char **s,
13244 struct symtabs_and_lines *sals)
13245 {
13246 internal_error_pure_virtual_called ();
13247 }
13248
13249 /* The default 'explains_signal' method. */
13250
13251 static int
13252 base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
13253 {
13254 return 1;
13255 }
13256
13257 /* The default "after_condition_true" method. */
13258
13259 static void
13260 base_breakpoint_after_condition_true (struct bpstats *bs)
13261 {
13262 /* Nothing to do. */
13263 }
13264
13265 struct breakpoint_ops base_breakpoint_ops =
13266 {
13267 base_breakpoint_dtor,
13268 base_breakpoint_allocate_location,
13269 base_breakpoint_re_set,
13270 base_breakpoint_insert_location,
13271 base_breakpoint_remove_location,
13272 base_breakpoint_breakpoint_hit,
13273 base_breakpoint_check_status,
13274 base_breakpoint_resources_needed,
13275 base_breakpoint_works_in_software_mode,
13276 base_breakpoint_print_it,
13277 NULL,
13278 base_breakpoint_print_one_detail,
13279 base_breakpoint_print_mention,
13280 base_breakpoint_print_recreate,
13281 base_breakpoint_create_sals_from_address,
13282 base_breakpoint_create_breakpoints_sal,
13283 base_breakpoint_decode_linespec,
13284 base_breakpoint_explains_signal,
13285 base_breakpoint_after_condition_true,
13286 };
13287
13288 /* Default breakpoint_ops methods. */
13289
13290 static void
13291 bkpt_re_set (struct breakpoint *b)
13292 {
13293 /* FIXME: is this still reachable? */
13294 if (b->addr_string == NULL)
13295 {
13296 /* Anything without a string can't be re-set. */
13297 delete_breakpoint (b);
13298 return;
13299 }
13300
13301 breakpoint_re_set_default (b);
13302 }
13303
13304 static int
13305 bkpt_insert_location (struct bp_location *bl)
13306 {
13307 if (bl->loc_type == bp_loc_hardware_breakpoint)
13308 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
13309 else
13310 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
13311 }
13312
13313 static int
13314 bkpt_remove_location (struct bp_location *bl)
13315 {
13316 if (bl->loc_type == bp_loc_hardware_breakpoint)
13317 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
13318 else
13319 return target_remove_breakpoint (bl->gdbarch, &bl->target_info);
13320 }
13321
13322 static int
13323 bkpt_breakpoint_hit (const struct bp_location *bl,
13324 struct address_space *aspace, CORE_ADDR bp_addr,
13325 const struct target_waitstatus *ws)
13326 {
13327 if (ws->kind != TARGET_WAITKIND_STOPPED
13328 || ws->value.sig != GDB_SIGNAL_TRAP)
13329 return 0;
13330
13331 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
13332 aspace, bp_addr))
13333 return 0;
13334
13335 if (overlay_debugging /* unmapped overlay section */
13336 && section_is_overlay (bl->section)
13337 && !section_is_mapped (bl->section))
13338 return 0;
13339
13340 return 1;
13341 }
13342
13343 static int
13344 dprintf_breakpoint_hit (const struct bp_location *bl,
13345 struct address_space *aspace, CORE_ADDR bp_addr,
13346 const struct target_waitstatus *ws)
13347 {
13348 if (dprintf_style == dprintf_style_agent
13349 && target_can_run_breakpoint_commands ())
13350 {
13351 /* An agent-style dprintf never causes a stop. If we see a trap
13352 for this address it must be for a breakpoint that happens to
13353 be set at the same address. */
13354 return 0;
13355 }
13356
13357 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
13358 }
13359
13360 static int
13361 bkpt_resources_needed (const struct bp_location *bl)
13362 {
13363 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
13364
13365 return 1;
13366 }
13367
13368 static enum print_stop_action
13369 bkpt_print_it (bpstat bs)
13370 {
13371 struct breakpoint *b;
13372 const struct bp_location *bl;
13373 int bp_temp;
13374 struct ui_out *uiout = current_uiout;
13375
13376 gdb_assert (bs->bp_location_at != NULL);
13377
13378 bl = bs->bp_location_at;
13379 b = bs->breakpoint_at;
13380
13381 bp_temp = b->disposition == disp_del;
13382 if (bl->address != bl->requested_address)
13383 breakpoint_adjustment_warning (bl->requested_address,
13384 bl->address,
13385 b->number, 1);
13386 annotate_breakpoint (b->number);
13387 if (bp_temp)
13388 ui_out_text (uiout, "\nTemporary breakpoint ");
13389 else
13390 ui_out_text (uiout, "\nBreakpoint ");
13391 if (ui_out_is_mi_like_p (uiout))
13392 {
13393 ui_out_field_string (uiout, "reason",
13394 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
13395 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
13396 }
13397 ui_out_field_int (uiout, "bkptno", b->number);
13398 ui_out_text (uiout, ", ");
13399
13400 return PRINT_SRC_AND_LOC;
13401 }
13402
13403 static void
13404 bkpt_print_mention (struct breakpoint *b)
13405 {
13406 if (ui_out_is_mi_like_p (current_uiout))
13407 return;
13408
13409 switch (b->type)
13410 {
13411 case bp_breakpoint:
13412 case bp_gnu_ifunc_resolver:
13413 if (b->disposition == disp_del)
13414 printf_filtered (_("Temporary breakpoint"));
13415 else
13416 printf_filtered (_("Breakpoint"));
13417 printf_filtered (_(" %d"), b->number);
13418 if (b->type == bp_gnu_ifunc_resolver)
13419 printf_filtered (_(" at gnu-indirect-function resolver"));
13420 break;
13421 case bp_hardware_breakpoint:
13422 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
13423 break;
13424 case bp_dprintf:
13425 printf_filtered (_("Dprintf %d"), b->number);
13426 break;
13427 }
13428
13429 say_where (b);
13430 }
13431
13432 static void
13433 bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13434 {
13435 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
13436 fprintf_unfiltered (fp, "tbreak");
13437 else if (tp->type == bp_breakpoint)
13438 fprintf_unfiltered (fp, "break");
13439 else if (tp->type == bp_hardware_breakpoint
13440 && tp->disposition == disp_del)
13441 fprintf_unfiltered (fp, "thbreak");
13442 else if (tp->type == bp_hardware_breakpoint)
13443 fprintf_unfiltered (fp, "hbreak");
13444 else
13445 internal_error (__FILE__, __LINE__,
13446 _("unhandled breakpoint type %d"), (int) tp->type);
13447
13448 fprintf_unfiltered (fp, " %s", tp->addr_string);
13449 print_recreate_thread (tp, fp);
13450 }
13451
13452 static void
13453 bkpt_create_sals_from_address (char **arg,
13454 struct linespec_result *canonical,
13455 enum bptype type_wanted,
13456 char *addr_start, char **copy_arg)
13457 {
13458 create_sals_from_address_default (arg, canonical, type_wanted,
13459 addr_start, copy_arg);
13460 }
13461
13462 static void
13463 bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
13464 struct linespec_result *canonical,
13465 char *cond_string,
13466 char *extra_string,
13467 enum bptype type_wanted,
13468 enum bpdisp disposition,
13469 int thread,
13470 int task, int ignore_count,
13471 const struct breakpoint_ops *ops,
13472 int from_tty, int enabled,
13473 int internal, unsigned flags)
13474 {
13475 create_breakpoints_sal_default (gdbarch, canonical,
13476 cond_string, extra_string,
13477 type_wanted,
13478 disposition, thread, task,
13479 ignore_count, ops, from_tty,
13480 enabled, internal, flags);
13481 }
13482
13483 static void
13484 bkpt_decode_linespec (struct breakpoint *b, char **s,
13485 struct symtabs_and_lines *sals)
13486 {
13487 decode_linespec_default (b, s, sals);
13488 }
13489
13490 /* Virtual table for internal breakpoints. */
13491
13492 static void
13493 internal_bkpt_re_set (struct breakpoint *b)
13494 {
13495 switch (b->type)
13496 {
13497 /* Delete overlay event and longjmp master breakpoints; they
13498 will be reset later by breakpoint_re_set. */
13499 case bp_overlay_event:
13500 case bp_longjmp_master:
13501 case bp_std_terminate_master:
13502 case bp_exception_master:
13503 delete_breakpoint (b);
13504 break;
13505
13506 /* This breakpoint is special, it's set up when the inferior
13507 starts and we really don't want to touch it. */
13508 case bp_shlib_event:
13509
13510 /* Like bp_shlib_event, this breakpoint type is special. Once
13511 it is set up, we do not want to touch it. */
13512 case bp_thread_event:
13513 break;
13514 }
13515 }
13516
13517 static void
13518 internal_bkpt_check_status (bpstat bs)
13519 {
13520 if (bs->breakpoint_at->type == bp_shlib_event)
13521 {
13522 /* If requested, stop when the dynamic linker notifies GDB of
13523 events. This allows the user to get control and place
13524 breakpoints in initializer routines for dynamically loaded
13525 objects (among other things). */
13526 bs->stop = stop_on_solib_events;
13527 bs->print = stop_on_solib_events;
13528 }
13529 else
13530 bs->stop = 0;
13531 }
13532
13533 static enum print_stop_action
13534 internal_bkpt_print_it (bpstat bs)
13535 {
13536 struct breakpoint *b;
13537
13538 b = bs->breakpoint_at;
13539
13540 switch (b->type)
13541 {
13542 case bp_shlib_event:
13543 /* Did we stop because the user set the stop_on_solib_events
13544 variable? (If so, we report this as a generic, "Stopped due
13545 to shlib event" message.) */
13546 print_solib_event (0);
13547 break;
13548
13549 case bp_thread_event:
13550 /* Not sure how we will get here.
13551 GDB should not stop for these breakpoints. */
13552 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
13553 break;
13554
13555 case bp_overlay_event:
13556 /* By analogy with the thread event, GDB should not stop for these. */
13557 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
13558 break;
13559
13560 case bp_longjmp_master:
13561 /* These should never be enabled. */
13562 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
13563 break;
13564
13565 case bp_std_terminate_master:
13566 /* These should never be enabled. */
13567 printf_filtered (_("std::terminate Master Breakpoint: "
13568 "gdb should not stop!\n"));
13569 break;
13570
13571 case bp_exception_master:
13572 /* These should never be enabled. */
13573 printf_filtered (_("Exception Master Breakpoint: "
13574 "gdb should not stop!\n"));
13575 break;
13576 }
13577
13578 return PRINT_NOTHING;
13579 }
13580
13581 static void
13582 internal_bkpt_print_mention (struct breakpoint *b)
13583 {
13584 /* Nothing to mention. These breakpoints are internal. */
13585 }
13586
13587 /* Virtual table for momentary breakpoints */
13588
13589 static void
13590 momentary_bkpt_re_set (struct breakpoint *b)
13591 {
13592 /* Keep temporary breakpoints, which can be encountered when we step
13593 over a dlopen call and solib_add is resetting the breakpoints.
13594 Otherwise these should have been blown away via the cleanup chain
13595 or by breakpoint_init_inferior when we rerun the executable. */
13596 }
13597
13598 static void
13599 momentary_bkpt_check_status (bpstat bs)
13600 {
13601 /* Nothing. The point of these breakpoints is causing a stop. */
13602 }
13603
13604 static enum print_stop_action
13605 momentary_bkpt_print_it (bpstat bs)
13606 {
13607 struct ui_out *uiout = current_uiout;
13608
13609 if (ui_out_is_mi_like_p (uiout))
13610 {
13611 struct breakpoint *b = bs->breakpoint_at;
13612
13613 switch (b->type)
13614 {
13615 case bp_finish:
13616 ui_out_field_string
13617 (uiout, "reason",
13618 async_reason_lookup (EXEC_ASYNC_FUNCTION_FINISHED));
13619 break;
13620
13621 case bp_until:
13622 ui_out_field_string
13623 (uiout, "reason",
13624 async_reason_lookup (EXEC_ASYNC_LOCATION_REACHED));
13625 break;
13626 }
13627 }
13628
13629 return PRINT_UNKNOWN;
13630 }
13631
13632 static void
13633 momentary_bkpt_print_mention (struct breakpoint *b)
13634 {
13635 /* Nothing to mention. These breakpoints are internal. */
13636 }
13637
13638 /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
13639
13640 It gets cleared already on the removal of the first one of such placed
13641 breakpoints. This is OK as they get all removed altogether. */
13642
13643 static void
13644 longjmp_bkpt_dtor (struct breakpoint *self)
13645 {
13646 struct thread_info *tp = find_thread_id (self->thread);
13647
13648 if (tp)
13649 tp->initiating_frame = null_frame_id;
13650
13651 momentary_breakpoint_ops.dtor (self);
13652 }
13653
13654 /* Specific methods for probe breakpoints. */
13655
13656 static int
13657 bkpt_probe_insert_location (struct bp_location *bl)
13658 {
13659 int v = bkpt_insert_location (bl);
13660
13661 if (v == 0)
13662 {
13663 /* The insertion was successful, now let's set the probe's semaphore
13664 if needed. */
13665 if (bl->probe.probe->pops->set_semaphore != NULL)
13666 bl->probe.probe->pops->set_semaphore (bl->probe.probe,
13667 bl->probe.objfile,
13668 bl->gdbarch);
13669 }
13670
13671 return v;
13672 }
13673
13674 static int
13675 bkpt_probe_remove_location (struct bp_location *bl)
13676 {
13677 /* Let's clear the semaphore before removing the location. */
13678 if (bl->probe.probe->pops->clear_semaphore != NULL)
13679 bl->probe.probe->pops->clear_semaphore (bl->probe.probe,
13680 bl->probe.objfile,
13681 bl->gdbarch);
13682
13683 return bkpt_remove_location (bl);
13684 }
13685
13686 static void
13687 bkpt_probe_create_sals_from_address (char **arg,
13688 struct linespec_result *canonical,
13689 enum bptype type_wanted,
13690 char *addr_start, char **copy_arg)
13691 {
13692 struct linespec_sals lsal;
13693
13694 lsal.sals = parse_probes (arg, canonical);
13695
13696 *copy_arg = xstrdup (canonical->addr_string);
13697 lsal.canonical = xstrdup (*copy_arg);
13698
13699 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13700 }
13701
13702 static void
13703 bkpt_probe_decode_linespec (struct breakpoint *b, char **s,
13704 struct symtabs_and_lines *sals)
13705 {
13706 *sals = parse_probes (s, NULL);
13707 if (!sals->sals)
13708 error (_("probe not found"));
13709 }
13710
13711 /* The breakpoint_ops structure to be used in tracepoints. */
13712
13713 static void
13714 tracepoint_re_set (struct breakpoint *b)
13715 {
13716 breakpoint_re_set_default (b);
13717 }
13718
13719 static int
13720 tracepoint_breakpoint_hit (const struct bp_location *bl,
13721 struct address_space *aspace, CORE_ADDR bp_addr,
13722 const struct target_waitstatus *ws)
13723 {
13724 /* By definition, the inferior does not report stops at
13725 tracepoints. */
13726 return 0;
13727 }
13728
13729 static void
13730 tracepoint_print_one_detail (const struct breakpoint *self,
13731 struct ui_out *uiout)
13732 {
13733 struct tracepoint *tp = (struct tracepoint *) self;
13734 if (tp->static_trace_marker_id)
13735 {
13736 gdb_assert (self->type == bp_static_tracepoint);
13737
13738 ui_out_text (uiout, "\tmarker id is ");
13739 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
13740 tp->static_trace_marker_id);
13741 ui_out_text (uiout, "\n");
13742 }
13743 }
13744
13745 static void
13746 tracepoint_print_mention (struct breakpoint *b)
13747 {
13748 if (ui_out_is_mi_like_p (current_uiout))
13749 return;
13750
13751 switch (b->type)
13752 {
13753 case bp_tracepoint:
13754 printf_filtered (_("Tracepoint"));
13755 printf_filtered (_(" %d"), b->number);
13756 break;
13757 case bp_fast_tracepoint:
13758 printf_filtered (_("Fast tracepoint"));
13759 printf_filtered (_(" %d"), b->number);
13760 break;
13761 case bp_static_tracepoint:
13762 printf_filtered (_("Static tracepoint"));
13763 printf_filtered (_(" %d"), b->number);
13764 break;
13765 default:
13766 internal_error (__FILE__, __LINE__,
13767 _("unhandled tracepoint type %d"), (int) b->type);
13768 }
13769
13770 say_where (b);
13771 }
13772
13773 static void
13774 tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
13775 {
13776 struct tracepoint *tp = (struct tracepoint *) self;
13777
13778 if (self->type == bp_fast_tracepoint)
13779 fprintf_unfiltered (fp, "ftrace");
13780 if (self->type == bp_static_tracepoint)
13781 fprintf_unfiltered (fp, "strace");
13782 else if (self->type == bp_tracepoint)
13783 fprintf_unfiltered (fp, "trace");
13784 else
13785 internal_error (__FILE__, __LINE__,
13786 _("unhandled tracepoint type %d"), (int) self->type);
13787
13788 fprintf_unfiltered (fp, " %s", self->addr_string);
13789 print_recreate_thread (self, fp);
13790
13791 if (tp->pass_count)
13792 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
13793 }
13794
13795 static void
13796 tracepoint_create_sals_from_address (char **arg,
13797 struct linespec_result *canonical,
13798 enum bptype type_wanted,
13799 char *addr_start, char **copy_arg)
13800 {
13801 create_sals_from_address_default (arg, canonical, type_wanted,
13802 addr_start, copy_arg);
13803 }
13804
13805 static void
13806 tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13807 struct linespec_result *canonical,
13808 char *cond_string,
13809 char *extra_string,
13810 enum bptype type_wanted,
13811 enum bpdisp disposition,
13812 int thread,
13813 int task, int ignore_count,
13814 const struct breakpoint_ops *ops,
13815 int from_tty, int enabled,
13816 int internal, unsigned flags)
13817 {
13818 create_breakpoints_sal_default (gdbarch, canonical,
13819 cond_string, extra_string,
13820 type_wanted,
13821 disposition, thread, task,
13822 ignore_count, ops, from_tty,
13823 enabled, internal, flags);
13824 }
13825
13826 static void
13827 tracepoint_decode_linespec (struct breakpoint *b, char **s,
13828 struct symtabs_and_lines *sals)
13829 {
13830 decode_linespec_default (b, s, sals);
13831 }
13832
13833 struct breakpoint_ops tracepoint_breakpoint_ops;
13834
13835 /* The breakpoint_ops structure to be use on tracepoints placed in a
13836 static probe. */
13837
13838 static void
13839 tracepoint_probe_create_sals_from_address (char **arg,
13840 struct linespec_result *canonical,
13841 enum bptype type_wanted,
13842 char *addr_start, char **copy_arg)
13843 {
13844 /* We use the same method for breakpoint on probes. */
13845 bkpt_probe_create_sals_from_address (arg, canonical, type_wanted,
13846 addr_start, copy_arg);
13847 }
13848
13849 static void
13850 tracepoint_probe_decode_linespec (struct breakpoint *b, char **s,
13851 struct symtabs_and_lines *sals)
13852 {
13853 /* We use the same method for breakpoint on probes. */
13854 bkpt_probe_decode_linespec (b, s, sals);
13855 }
13856
13857 static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13858
13859 /* Dprintf breakpoint_ops methods. */
13860
13861 static void
13862 dprintf_re_set (struct breakpoint *b)
13863 {
13864 breakpoint_re_set_default (b);
13865
13866 /* This breakpoint could have been pending, and be resolved now, and
13867 if so, we should now have the extra string. If we don't, the
13868 dprintf was malformed when created, but we couldn't tell because
13869 we can't extract the extra string until the location is
13870 resolved. */
13871 if (b->loc != NULL && b->extra_string == NULL)
13872 error (_("Format string required"));
13873
13874 /* 1 - connect to target 1, that can run breakpoint commands.
13875 2 - create a dprintf, which resolves fine.
13876 3 - disconnect from target 1
13877 4 - connect to target 2, that can NOT run breakpoint commands.
13878
13879 After steps #3/#4, you'll want the dprintf command list to
13880 be updated, because target 1 and 2 may well return different
13881 answers for target_can_run_breakpoint_commands().
13882 Given absence of finer grained resetting, we get to do
13883 it all the time. */
13884 if (b->extra_string != NULL)
13885 update_dprintf_command_list (b);
13886 }
13887
13888 /* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13889
13890 static void
13891 dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13892 {
13893 fprintf_unfiltered (fp, "dprintf %s%s", tp->addr_string,
13894 tp->extra_string);
13895 print_recreate_thread (tp, fp);
13896 }
13897
13898 /* Implement the "after_condition_true" breakpoint_ops method for
13899 dprintf.
13900
13901 dprintf's are implemented with regular commands in their command
13902 list, but we run the commands here instead of before presenting the
13903 stop to the user, as dprintf's don't actually cause a stop. This
13904 also makes it so that the commands of multiple dprintfs at the same
13905 address are all handled. */
13906
13907 static void
13908 dprintf_after_condition_true (struct bpstats *bs)
13909 {
13910 struct cleanup *old_chain;
13911 struct bpstats tmp_bs = { NULL };
13912 struct bpstats *tmp_bs_p = &tmp_bs;
13913
13914 /* dprintf's never cause a stop. This wasn't set in the
13915 check_status hook instead because that would make the dprintf's
13916 condition not be evaluated. */
13917 bs->stop = 0;
13918
13919 /* Run the command list here. Take ownership of it instead of
13920 copying. We never want these commands to run later in
13921 bpstat_do_actions, if a breakpoint that causes a stop happens to
13922 be set at same address as this dprintf, or even if running the
13923 commands here throws. */
13924 tmp_bs.commands = bs->commands;
13925 bs->commands = NULL;
13926 old_chain = make_cleanup_decref_counted_command_line (&tmp_bs.commands);
13927
13928 bpstat_do_actions_1 (&tmp_bs_p);
13929
13930 /* 'tmp_bs.commands' will usually be NULL by now, but
13931 bpstat_do_actions_1 may return early without processing the whole
13932 list. */
13933 do_cleanups (old_chain);
13934 }
13935
13936 /* The breakpoint_ops structure to be used on static tracepoints with
13937 markers (`-m'). */
13938
13939 static void
13940 strace_marker_create_sals_from_address (char **arg,
13941 struct linespec_result *canonical,
13942 enum bptype type_wanted,
13943 char *addr_start, char **copy_arg)
13944 {
13945 struct linespec_sals lsal;
13946
13947 lsal.sals = decode_static_tracepoint_spec (arg);
13948
13949 *copy_arg = savestring (addr_start, *arg - addr_start);
13950
13951 canonical->addr_string = xstrdup (*copy_arg);
13952 lsal.canonical = xstrdup (*copy_arg);
13953 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13954 }
13955
13956 static void
13957 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13958 struct linespec_result *canonical,
13959 char *cond_string,
13960 char *extra_string,
13961 enum bptype type_wanted,
13962 enum bpdisp disposition,
13963 int thread,
13964 int task, int ignore_count,
13965 const struct breakpoint_ops *ops,
13966 int from_tty, int enabled,
13967 int internal, unsigned flags)
13968 {
13969 int i;
13970 struct linespec_sals *lsal = VEC_index (linespec_sals,
13971 canonical->sals, 0);
13972
13973 /* If the user is creating a static tracepoint by marker id
13974 (strace -m MARKER_ID), then store the sals index, so that
13975 breakpoint_re_set can try to match up which of the newly
13976 found markers corresponds to this one, and, don't try to
13977 expand multiple locations for each sal, given than SALS
13978 already should contain all sals for MARKER_ID. */
13979
13980 for (i = 0; i < lsal->sals.nelts; ++i)
13981 {
13982 struct symtabs_and_lines expanded;
13983 struct tracepoint *tp;
13984 struct cleanup *old_chain;
13985 char *addr_string;
13986
13987 expanded.nelts = 1;
13988 expanded.sals = &lsal->sals.sals[i];
13989
13990 addr_string = xstrdup (canonical->addr_string);
13991 old_chain = make_cleanup (xfree, addr_string);
13992
13993 tp = XCNEW (struct tracepoint);
13994 init_breakpoint_sal (&tp->base, gdbarch, expanded,
13995 addr_string, NULL,
13996 cond_string, extra_string,
13997 type_wanted, disposition,
13998 thread, task, ignore_count, ops,
13999 from_tty, enabled, internal, flags,
14000 canonical->special_display);
14001 /* Given that its possible to have multiple markers with
14002 the same string id, if the user is creating a static
14003 tracepoint by marker id ("strace -m MARKER_ID"), then
14004 store the sals index, so that breakpoint_re_set can
14005 try to match up which of the newly found markers
14006 corresponds to this one */
14007 tp->static_trace_marker_id_idx = i;
14008
14009 install_breakpoint (internal, &tp->base, 0);
14010
14011 discard_cleanups (old_chain);
14012 }
14013 }
14014
14015 static void
14016 strace_marker_decode_linespec (struct breakpoint *b, char **s,
14017 struct symtabs_and_lines *sals)
14018 {
14019 struct tracepoint *tp = (struct tracepoint *) b;
14020
14021 *sals = decode_static_tracepoint_spec (s);
14022 if (sals->nelts > tp->static_trace_marker_id_idx)
14023 {
14024 sals->sals[0] = sals->sals[tp->static_trace_marker_id_idx];
14025 sals->nelts = 1;
14026 }
14027 else
14028 error (_("marker %s not found"), tp->static_trace_marker_id);
14029 }
14030
14031 static struct breakpoint_ops strace_marker_breakpoint_ops;
14032
14033 static int
14034 strace_marker_p (struct breakpoint *b)
14035 {
14036 return b->ops == &strace_marker_breakpoint_ops;
14037 }
14038
14039 /* Delete a breakpoint and clean up all traces of it in the data
14040 structures. */
14041
14042 void
14043 delete_breakpoint (struct breakpoint *bpt)
14044 {
14045 struct breakpoint *b;
14046
14047 gdb_assert (bpt != NULL);
14048
14049 /* Has this bp already been deleted? This can happen because
14050 multiple lists can hold pointers to bp's. bpstat lists are
14051 especial culprits.
14052
14053 One example of this happening is a watchpoint's scope bp. When
14054 the scope bp triggers, we notice that the watchpoint is out of
14055 scope, and delete it. We also delete its scope bp. But the
14056 scope bp is marked "auto-deleting", and is already on a bpstat.
14057 That bpstat is then checked for auto-deleting bp's, which are
14058 deleted.
14059
14060 A real solution to this problem might involve reference counts in
14061 bp's, and/or giving them pointers back to their referencing
14062 bpstat's, and teaching delete_breakpoint to only free a bp's
14063 storage when no more references were extent. A cheaper bandaid
14064 was chosen. */
14065 if (bpt->type == bp_none)
14066 return;
14067
14068 /* At least avoid this stale reference until the reference counting
14069 of breakpoints gets resolved. */
14070 if (bpt->related_breakpoint != bpt)
14071 {
14072 struct breakpoint *related;
14073 struct watchpoint *w;
14074
14075 if (bpt->type == bp_watchpoint_scope)
14076 w = (struct watchpoint *) bpt->related_breakpoint;
14077 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
14078 w = (struct watchpoint *) bpt;
14079 else
14080 w = NULL;
14081 if (w != NULL)
14082 watchpoint_del_at_next_stop (w);
14083
14084 /* Unlink bpt from the bpt->related_breakpoint ring. */
14085 for (related = bpt; related->related_breakpoint != bpt;
14086 related = related->related_breakpoint);
14087 related->related_breakpoint = bpt->related_breakpoint;
14088 bpt->related_breakpoint = bpt;
14089 }
14090
14091 /* watch_command_1 creates a watchpoint but only sets its number if
14092 update_watchpoint succeeds in creating its bp_locations. If there's
14093 a problem in that process, we'll be asked to delete the half-created
14094 watchpoint. In that case, don't announce the deletion. */
14095 if (bpt->number)
14096 observer_notify_breakpoint_deleted (bpt);
14097
14098 if (breakpoint_chain == bpt)
14099 breakpoint_chain = bpt->next;
14100
14101 ALL_BREAKPOINTS (b)
14102 if (b->next == bpt)
14103 {
14104 b->next = bpt->next;
14105 break;
14106 }
14107
14108 /* Be sure no bpstat's are pointing at the breakpoint after it's
14109 been freed. */
14110 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
14111 in all threads for now. Note that we cannot just remove bpstats
14112 pointing at bpt from the stop_bpstat list entirely, as breakpoint
14113 commands are associated with the bpstat; if we remove it here,
14114 then the later call to bpstat_do_actions (&stop_bpstat); in
14115 event-top.c won't do anything, and temporary breakpoints with
14116 commands won't work. */
14117
14118 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
14119
14120 /* Now that breakpoint is removed from breakpoint list, update the
14121 global location list. This will remove locations that used to
14122 belong to this breakpoint. Do this before freeing the breakpoint
14123 itself, since remove_breakpoint looks at location's owner. It
14124 might be better design to have location completely
14125 self-contained, but it's not the case now. */
14126 update_global_location_list (UGLL_DONT_INSERT);
14127
14128 bpt->ops->dtor (bpt);
14129 /* On the chance that someone will soon try again to delete this
14130 same bp, we mark it as deleted before freeing its storage. */
14131 bpt->type = bp_none;
14132 xfree (bpt);
14133 }
14134
14135 static void
14136 do_delete_breakpoint_cleanup (void *b)
14137 {
14138 delete_breakpoint (b);
14139 }
14140
14141 struct cleanup *
14142 make_cleanup_delete_breakpoint (struct breakpoint *b)
14143 {
14144 return make_cleanup (do_delete_breakpoint_cleanup, b);
14145 }
14146
14147 /* Iterator function to call a user-provided callback function once
14148 for each of B and its related breakpoints. */
14149
14150 static void
14151 iterate_over_related_breakpoints (struct breakpoint *b,
14152 void (*function) (struct breakpoint *,
14153 void *),
14154 void *data)
14155 {
14156 struct breakpoint *related;
14157
14158 related = b;
14159 do
14160 {
14161 struct breakpoint *next;
14162
14163 /* FUNCTION may delete RELATED. */
14164 next = related->related_breakpoint;
14165
14166 if (next == related)
14167 {
14168 /* RELATED is the last ring entry. */
14169 function (related, data);
14170
14171 /* FUNCTION may have deleted it, so we'd never reach back to
14172 B. There's nothing left to do anyway, so just break
14173 out. */
14174 break;
14175 }
14176 else
14177 function (related, data);
14178
14179 related = next;
14180 }
14181 while (related != b);
14182 }
14183
14184 static void
14185 do_delete_breakpoint (struct breakpoint *b, void *ignore)
14186 {
14187 delete_breakpoint (b);
14188 }
14189
14190 /* A callback for map_breakpoint_numbers that calls
14191 delete_breakpoint. */
14192
14193 static void
14194 do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
14195 {
14196 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
14197 }
14198
14199 void
14200 delete_command (char *arg, int from_tty)
14201 {
14202 struct breakpoint *b, *b_tmp;
14203
14204 dont_repeat ();
14205
14206 if (arg == 0)
14207 {
14208 int breaks_to_delete = 0;
14209
14210 /* Delete all breakpoints if no argument. Do not delete
14211 internal breakpoints, these have to be deleted with an
14212 explicit breakpoint number argument. */
14213 ALL_BREAKPOINTS (b)
14214 if (user_breakpoint_p (b))
14215 {
14216 breaks_to_delete = 1;
14217 break;
14218 }
14219
14220 /* Ask user only if there are some breakpoints to delete. */
14221 if (!from_tty
14222 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
14223 {
14224 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14225 if (user_breakpoint_p (b))
14226 delete_breakpoint (b);
14227 }
14228 }
14229 else
14230 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
14231 }
14232
14233 static int
14234 all_locations_are_pending (struct bp_location *loc)
14235 {
14236 for (; loc; loc = loc->next)
14237 if (!loc->shlib_disabled
14238 && !loc->pspace->executing_startup)
14239 return 0;
14240 return 1;
14241 }
14242
14243 /* Subroutine of update_breakpoint_locations to simplify it.
14244 Return non-zero if multiple fns in list LOC have the same name.
14245 Null names are ignored. */
14246
14247 static int
14248 ambiguous_names_p (struct bp_location *loc)
14249 {
14250 struct bp_location *l;
14251 htab_t htab = htab_create_alloc (13, htab_hash_string,
14252 (int (*) (const void *,
14253 const void *)) streq,
14254 NULL, xcalloc, xfree);
14255
14256 for (l = loc; l != NULL; l = l->next)
14257 {
14258 const char **slot;
14259 const char *name = l->function_name;
14260
14261 /* Allow for some names to be NULL, ignore them. */
14262 if (name == NULL)
14263 continue;
14264
14265 slot = (const char **) htab_find_slot (htab, (const void *) name,
14266 INSERT);
14267 /* NOTE: We can assume slot != NULL here because xcalloc never
14268 returns NULL. */
14269 if (*slot != NULL)
14270 {
14271 htab_delete (htab);
14272 return 1;
14273 }
14274 *slot = name;
14275 }
14276
14277 htab_delete (htab);
14278 return 0;
14279 }
14280
14281 /* When symbols change, it probably means the sources changed as well,
14282 and it might mean the static tracepoint markers are no longer at
14283 the same address or line numbers they used to be at last we
14284 checked. Losing your static tracepoints whenever you rebuild is
14285 undesirable. This function tries to resync/rematch gdb static
14286 tracepoints with the markers on the target, for static tracepoints
14287 that have not been set by marker id. Static tracepoint that have
14288 been set by marker id are reset by marker id in breakpoint_re_set.
14289 The heuristic is:
14290
14291 1) For a tracepoint set at a specific address, look for a marker at
14292 the old PC. If one is found there, assume to be the same marker.
14293 If the name / string id of the marker found is different from the
14294 previous known name, assume that means the user renamed the marker
14295 in the sources, and output a warning.
14296
14297 2) For a tracepoint set at a given line number, look for a marker
14298 at the new address of the old line number. If one is found there,
14299 assume to be the same marker. If the name / string id of the
14300 marker found is different from the previous known name, assume that
14301 means the user renamed the marker in the sources, and output a
14302 warning.
14303
14304 3) If a marker is no longer found at the same address or line, it
14305 may mean the marker no longer exists. But it may also just mean
14306 the code changed a bit. Maybe the user added a few lines of code
14307 that made the marker move up or down (in line number terms). Ask
14308 the target for info about the marker with the string id as we knew
14309 it. If found, update line number and address in the matching
14310 static tracepoint. This will get confused if there's more than one
14311 marker with the same ID (possible in UST, although unadvised
14312 precisely because it confuses tools). */
14313
14314 static struct symtab_and_line
14315 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
14316 {
14317 struct tracepoint *tp = (struct tracepoint *) b;
14318 struct static_tracepoint_marker marker;
14319 CORE_ADDR pc;
14320
14321 pc = sal.pc;
14322 if (sal.line)
14323 find_line_pc (sal.symtab, sal.line, &pc);
14324
14325 if (target_static_tracepoint_marker_at (pc, &marker))
14326 {
14327 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
14328 warning (_("static tracepoint %d changed probed marker from %s to %s"),
14329 b->number,
14330 tp->static_trace_marker_id, marker.str_id);
14331
14332 xfree (tp->static_trace_marker_id);
14333 tp->static_trace_marker_id = xstrdup (marker.str_id);
14334 release_static_tracepoint_marker (&marker);
14335
14336 return sal;
14337 }
14338
14339 /* Old marker wasn't found on target at lineno. Try looking it up
14340 by string ID. */
14341 if (!sal.explicit_pc
14342 && sal.line != 0
14343 && sal.symtab != NULL
14344 && tp->static_trace_marker_id != NULL)
14345 {
14346 VEC(static_tracepoint_marker_p) *markers;
14347
14348 markers
14349 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
14350
14351 if (!VEC_empty(static_tracepoint_marker_p, markers))
14352 {
14353 struct symtab_and_line sal2;
14354 struct symbol *sym;
14355 struct static_tracepoint_marker *tpmarker;
14356 struct ui_out *uiout = current_uiout;
14357
14358 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
14359
14360 xfree (tp->static_trace_marker_id);
14361 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
14362
14363 warning (_("marker for static tracepoint %d (%s) not "
14364 "found at previous line number"),
14365 b->number, tp->static_trace_marker_id);
14366
14367 init_sal (&sal2);
14368
14369 sal2.pc = tpmarker->address;
14370
14371 sal2 = find_pc_line (tpmarker->address, 0);
14372 sym = find_pc_sect_function (tpmarker->address, NULL);
14373 ui_out_text (uiout, "Now in ");
14374 if (sym)
14375 {
14376 ui_out_field_string (uiout, "func",
14377 SYMBOL_PRINT_NAME (sym));
14378 ui_out_text (uiout, " at ");
14379 }
14380 ui_out_field_string (uiout, "file",
14381 symtab_to_filename_for_display (sal2.symtab));
14382 ui_out_text (uiout, ":");
14383
14384 if (ui_out_is_mi_like_p (uiout))
14385 {
14386 const char *fullname = symtab_to_fullname (sal2.symtab);
14387
14388 ui_out_field_string (uiout, "fullname", fullname);
14389 }
14390
14391 ui_out_field_int (uiout, "line", sal2.line);
14392 ui_out_text (uiout, "\n");
14393
14394 b->loc->line_number = sal2.line;
14395 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
14396
14397 xfree (b->addr_string);
14398 b->addr_string = xstrprintf ("%s:%d",
14399 symtab_to_filename_for_display (sal2.symtab),
14400 b->loc->line_number);
14401
14402 /* Might be nice to check if function changed, and warn if
14403 so. */
14404
14405 release_static_tracepoint_marker (tpmarker);
14406 }
14407 }
14408 return sal;
14409 }
14410
14411 /* Returns 1 iff locations A and B are sufficiently same that
14412 we don't need to report breakpoint as changed. */
14413
14414 static int
14415 locations_are_equal (struct bp_location *a, struct bp_location *b)
14416 {
14417 while (a && b)
14418 {
14419 if (a->address != b->address)
14420 return 0;
14421
14422 if (a->shlib_disabled != b->shlib_disabled)
14423 return 0;
14424
14425 if (a->enabled != b->enabled)
14426 return 0;
14427
14428 a = a->next;
14429 b = b->next;
14430 }
14431
14432 if ((a == NULL) != (b == NULL))
14433 return 0;
14434
14435 return 1;
14436 }
14437
14438 /* Create new breakpoint locations for B (a hardware or software breakpoint)
14439 based on SALS and SALS_END. If SALS_END.NELTS is not zero, then B is
14440 a ranged breakpoint. */
14441
14442 void
14443 update_breakpoint_locations (struct breakpoint *b,
14444 struct symtabs_and_lines sals,
14445 struct symtabs_and_lines sals_end)
14446 {
14447 int i;
14448 struct bp_location *existing_locations = b->loc;
14449
14450 if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
14451 {
14452 /* Ranged breakpoints have only one start location and one end
14453 location. */
14454 b->enable_state = bp_disabled;
14455 update_global_location_list (UGLL_MAY_INSERT);
14456 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
14457 "multiple locations found\n"),
14458 b->number);
14459 return;
14460 }
14461
14462 /* If there's no new locations, and all existing locations are
14463 pending, don't do anything. This optimizes the common case where
14464 all locations are in the same shared library, that was unloaded.
14465 We'd like to retain the location, so that when the library is
14466 loaded again, we don't loose the enabled/disabled status of the
14467 individual locations. */
14468 if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
14469 return;
14470
14471 b->loc = NULL;
14472
14473 for (i = 0; i < sals.nelts; ++i)
14474 {
14475 struct bp_location *new_loc;
14476
14477 switch_to_program_space_and_thread (sals.sals[i].pspace);
14478
14479 new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
14480
14481 /* Reparse conditions, they might contain references to the
14482 old symtab. */
14483 if (b->cond_string != NULL)
14484 {
14485 const char *s;
14486
14487 s = b->cond_string;
14488 TRY
14489 {
14490 new_loc->cond = parse_exp_1 (&s, sals.sals[i].pc,
14491 block_for_pc (sals.sals[i].pc),
14492 0);
14493 }
14494 CATCH (e, RETURN_MASK_ERROR)
14495 {
14496 warning (_("failed to reevaluate condition "
14497 "for breakpoint %d: %s"),
14498 b->number, e.message);
14499 new_loc->enabled = 0;
14500 }
14501 END_CATCH
14502 }
14503
14504 if (sals_end.nelts)
14505 {
14506 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
14507
14508 new_loc->length = end - sals.sals[0].pc + 1;
14509 }
14510 }
14511
14512 /* If possible, carry over 'disable' status from existing
14513 breakpoints. */
14514 {
14515 struct bp_location *e = existing_locations;
14516 /* If there are multiple breakpoints with the same function name,
14517 e.g. for inline functions, comparing function names won't work.
14518 Instead compare pc addresses; this is just a heuristic as things
14519 may have moved, but in practice it gives the correct answer
14520 often enough until a better solution is found. */
14521 int have_ambiguous_names = ambiguous_names_p (b->loc);
14522
14523 for (; e; e = e->next)
14524 {
14525 if (!e->enabled && e->function_name)
14526 {
14527 struct bp_location *l = b->loc;
14528 if (have_ambiguous_names)
14529 {
14530 for (; l; l = l->next)
14531 if (breakpoint_locations_match (e, l))
14532 {
14533 l->enabled = 0;
14534 break;
14535 }
14536 }
14537 else
14538 {
14539 for (; l; l = l->next)
14540 if (l->function_name
14541 && strcmp (e->function_name, l->function_name) == 0)
14542 {
14543 l->enabled = 0;
14544 break;
14545 }
14546 }
14547 }
14548 }
14549 }
14550
14551 if (!locations_are_equal (existing_locations, b->loc))
14552 observer_notify_breakpoint_modified (b);
14553
14554 update_global_location_list (UGLL_MAY_INSERT);
14555 }
14556
14557 /* Find the SaL locations corresponding to the given ADDR_STRING.
14558 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
14559
14560 static struct symtabs_and_lines
14561 addr_string_to_sals (struct breakpoint *b, char *addr_string, int *found)
14562 {
14563 char *s;
14564 struct symtabs_and_lines sals = {0};
14565 struct gdb_exception exception = exception_none;
14566
14567 gdb_assert (b->ops != NULL);
14568 s = addr_string;
14569
14570 TRY
14571 {
14572 b->ops->decode_linespec (b, &s, &sals);
14573 }
14574 CATCH (e, RETURN_MASK_ERROR)
14575 {
14576 int not_found_and_ok = 0;
14577
14578 exception = e;
14579
14580 /* For pending breakpoints, it's expected that parsing will
14581 fail until the right shared library is loaded. User has
14582 already told to create pending breakpoints and don't need
14583 extra messages. If breakpoint is in bp_shlib_disabled
14584 state, then user already saw the message about that
14585 breakpoint being disabled, and don't want to see more
14586 errors. */
14587 if (e.error == NOT_FOUND_ERROR
14588 && (b->condition_not_parsed
14589 || (b->loc && b->loc->shlib_disabled)
14590 || (b->loc && b->loc->pspace->executing_startup)
14591 || b->enable_state == bp_disabled))
14592 not_found_and_ok = 1;
14593
14594 if (!not_found_and_ok)
14595 {
14596 /* We surely don't want to warn about the same breakpoint
14597 10 times. One solution, implemented here, is disable
14598 the breakpoint on error. Another solution would be to
14599 have separate 'warning emitted' flag. Since this
14600 happens only when a binary has changed, I don't know
14601 which approach is better. */
14602 b->enable_state = bp_disabled;
14603 throw_exception (e);
14604 }
14605 }
14606 END_CATCH
14607
14608 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
14609 {
14610 int i;
14611
14612 for (i = 0; i < sals.nelts; ++i)
14613 resolve_sal_pc (&sals.sals[i]);
14614 if (b->condition_not_parsed && s && s[0])
14615 {
14616 char *cond_string, *extra_string;
14617 int thread, task;
14618
14619 find_condition_and_thread (s, sals.sals[0].pc,
14620 &cond_string, &thread, &task,
14621 &extra_string);
14622 if (cond_string)
14623 b->cond_string = cond_string;
14624 b->thread = thread;
14625 b->task = task;
14626 if (extra_string)
14627 b->extra_string = extra_string;
14628 b->condition_not_parsed = 0;
14629 }
14630
14631 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
14632 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
14633
14634 *found = 1;
14635 }
14636 else
14637 *found = 0;
14638
14639 return sals;
14640 }
14641
14642 /* The default re_set method, for typical hardware or software
14643 breakpoints. Reevaluate the breakpoint and recreate its
14644 locations. */
14645
14646 static void
14647 breakpoint_re_set_default (struct breakpoint *b)
14648 {
14649 int found;
14650 struct symtabs_and_lines sals, sals_end;
14651 struct symtabs_and_lines expanded = {0};
14652 struct symtabs_and_lines expanded_end = {0};
14653
14654 sals = addr_string_to_sals (b, b->addr_string, &found);
14655 if (found)
14656 {
14657 make_cleanup (xfree, sals.sals);
14658 expanded = sals;
14659 }
14660
14661 if (b->addr_string_range_end)
14662 {
14663 sals_end = addr_string_to_sals (b, b->addr_string_range_end, &found);
14664 if (found)
14665 {
14666 make_cleanup (xfree, sals_end.sals);
14667 expanded_end = sals_end;
14668 }
14669 }
14670
14671 update_breakpoint_locations (b, expanded, expanded_end);
14672 }
14673
14674 /* Default method for creating SALs from an address string. It basically
14675 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
14676
14677 static void
14678 create_sals_from_address_default (char **arg,
14679 struct linespec_result *canonical,
14680 enum bptype type_wanted,
14681 char *addr_start, char **copy_arg)
14682 {
14683 parse_breakpoint_sals (arg, canonical);
14684 }
14685
14686 /* Call create_breakpoints_sal for the given arguments. This is the default
14687 function for the `create_breakpoints_sal' method of
14688 breakpoint_ops. */
14689
14690 static void
14691 create_breakpoints_sal_default (struct gdbarch *gdbarch,
14692 struct linespec_result *canonical,
14693 char *cond_string,
14694 char *extra_string,
14695 enum bptype type_wanted,
14696 enum bpdisp disposition,
14697 int thread,
14698 int task, int ignore_count,
14699 const struct breakpoint_ops *ops,
14700 int from_tty, int enabled,
14701 int internal, unsigned flags)
14702 {
14703 create_breakpoints_sal (gdbarch, canonical, cond_string,
14704 extra_string,
14705 type_wanted, disposition,
14706 thread, task, ignore_count, ops, from_tty,
14707 enabled, internal, flags);
14708 }
14709
14710 /* Decode the line represented by S by calling decode_line_full. This is the
14711 default function for the `decode_linespec' method of breakpoint_ops. */
14712
14713 static void
14714 decode_linespec_default (struct breakpoint *b, char **s,
14715 struct symtabs_and_lines *sals)
14716 {
14717 struct linespec_result canonical;
14718
14719 init_linespec_result (&canonical);
14720 decode_line_full (s, DECODE_LINE_FUNFIRSTLINE,
14721 (struct symtab *) NULL, 0,
14722 &canonical, multiple_symbols_all,
14723 b->filter);
14724
14725 /* We should get 0 or 1 resulting SALs. */
14726 gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
14727
14728 if (VEC_length (linespec_sals, canonical.sals) > 0)
14729 {
14730 struct linespec_sals *lsal;
14731
14732 lsal = VEC_index (linespec_sals, canonical.sals, 0);
14733 *sals = lsal->sals;
14734 /* Arrange it so the destructor does not free the
14735 contents. */
14736 lsal->sals.sals = NULL;
14737 }
14738
14739 destroy_linespec_result (&canonical);
14740 }
14741
14742 /* Prepare the global context for a re-set of breakpoint B. */
14743
14744 static struct cleanup *
14745 prepare_re_set_context (struct breakpoint *b)
14746 {
14747 struct cleanup *cleanups;
14748
14749 input_radix = b->input_radix;
14750 cleanups = save_current_space_and_thread ();
14751 if (b->pspace != NULL)
14752 switch_to_program_space_and_thread (b->pspace);
14753 set_language (b->language);
14754
14755 return cleanups;
14756 }
14757
14758 /* Reset a breakpoint given it's struct breakpoint * BINT.
14759 The value we return ends up being the return value from catch_errors.
14760 Unused in this case. */
14761
14762 static int
14763 breakpoint_re_set_one (void *bint)
14764 {
14765 /* Get past catch_errs. */
14766 struct breakpoint *b = (struct breakpoint *) bint;
14767 struct cleanup *cleanups;
14768
14769 cleanups = prepare_re_set_context (b);
14770 b->ops->re_set (b);
14771 do_cleanups (cleanups);
14772 return 0;
14773 }
14774
14775 /* Re-set all breakpoints after symbols have been re-loaded. */
14776 void
14777 breakpoint_re_set (void)
14778 {
14779 struct breakpoint *b, *b_tmp;
14780 enum language save_language;
14781 int save_input_radix;
14782 struct cleanup *old_chain;
14783
14784 save_language = current_language->la_language;
14785 save_input_radix = input_radix;
14786 old_chain = save_current_program_space ();
14787
14788 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14789 {
14790 /* Format possible error msg. */
14791 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
14792 b->number);
14793 struct cleanup *cleanups = make_cleanup (xfree, message);
14794 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
14795 do_cleanups (cleanups);
14796 }
14797 set_language (save_language);
14798 input_radix = save_input_radix;
14799
14800 jit_breakpoint_re_set ();
14801
14802 do_cleanups (old_chain);
14803
14804 create_overlay_event_breakpoint ();
14805 create_longjmp_master_breakpoint ();
14806 create_std_terminate_master_breakpoint ();
14807 create_exception_master_breakpoint ();
14808 }
14809 \f
14810 /* Reset the thread number of this breakpoint:
14811
14812 - If the breakpoint is for all threads, leave it as-is.
14813 - Else, reset it to the current thread for inferior_ptid. */
14814 void
14815 breakpoint_re_set_thread (struct breakpoint *b)
14816 {
14817 if (b->thread != -1)
14818 {
14819 if (in_thread_list (inferior_ptid))
14820 b->thread = pid_to_thread_id (inferior_ptid);
14821
14822 /* We're being called after following a fork. The new fork is
14823 selected as current, and unless this was a vfork will have a
14824 different program space from the original thread. Reset that
14825 as well. */
14826 b->loc->pspace = current_program_space;
14827 }
14828 }
14829
14830 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14831 If from_tty is nonzero, it prints a message to that effect,
14832 which ends with a period (no newline). */
14833
14834 void
14835 set_ignore_count (int bptnum, int count, int from_tty)
14836 {
14837 struct breakpoint *b;
14838
14839 if (count < 0)
14840 count = 0;
14841
14842 ALL_BREAKPOINTS (b)
14843 if (b->number == bptnum)
14844 {
14845 if (is_tracepoint (b))
14846 {
14847 if (from_tty && count != 0)
14848 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14849 bptnum);
14850 return;
14851 }
14852
14853 b->ignore_count = count;
14854 if (from_tty)
14855 {
14856 if (count == 0)
14857 printf_filtered (_("Will stop next time "
14858 "breakpoint %d is reached."),
14859 bptnum);
14860 else if (count == 1)
14861 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
14862 bptnum);
14863 else
14864 printf_filtered (_("Will ignore next %d "
14865 "crossings of breakpoint %d."),
14866 count, bptnum);
14867 }
14868 observer_notify_breakpoint_modified (b);
14869 return;
14870 }
14871
14872 error (_("No breakpoint number %d."), bptnum);
14873 }
14874
14875 /* Command to set ignore-count of breakpoint N to COUNT. */
14876
14877 static void
14878 ignore_command (char *args, int from_tty)
14879 {
14880 char *p = args;
14881 int num;
14882
14883 if (p == 0)
14884 error_no_arg (_("a breakpoint number"));
14885
14886 num = get_number (&p);
14887 if (num == 0)
14888 error (_("bad breakpoint number: '%s'"), args);
14889 if (*p == 0)
14890 error (_("Second argument (specified ignore-count) is missing."));
14891
14892 set_ignore_count (num,
14893 longest_to_int (value_as_long (parse_and_eval (p))),
14894 from_tty);
14895 if (from_tty)
14896 printf_filtered ("\n");
14897 }
14898 \f
14899 /* Call FUNCTION on each of the breakpoints
14900 whose numbers are given in ARGS. */
14901
14902 static void
14903 map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
14904 void *),
14905 void *data)
14906 {
14907 int num;
14908 struct breakpoint *b, *tmp;
14909 int match;
14910 struct get_number_or_range_state state;
14911
14912 if (args == 0 || *args == '\0')
14913 error_no_arg (_("one or more breakpoint numbers"));
14914
14915 init_number_or_range (&state, args);
14916
14917 while (!state.finished)
14918 {
14919 const char *p = state.string;
14920
14921 match = 0;
14922
14923 num = get_number_or_range (&state);
14924 if (num == 0)
14925 {
14926 warning (_("bad breakpoint number at or near '%s'"), p);
14927 }
14928 else
14929 {
14930 ALL_BREAKPOINTS_SAFE (b, tmp)
14931 if (b->number == num)
14932 {
14933 match = 1;
14934 function (b, data);
14935 break;
14936 }
14937 if (match == 0)
14938 printf_unfiltered (_("No breakpoint number %d.\n"), num);
14939 }
14940 }
14941 }
14942
14943 static struct bp_location *
14944 find_location_by_number (char *number)
14945 {
14946 char *dot = strchr (number, '.');
14947 char *p1;
14948 int bp_num;
14949 int loc_num;
14950 struct breakpoint *b;
14951 struct bp_location *loc;
14952
14953 *dot = '\0';
14954
14955 p1 = number;
14956 bp_num = get_number (&p1);
14957 if (bp_num == 0)
14958 error (_("Bad breakpoint number '%s'"), number);
14959
14960 ALL_BREAKPOINTS (b)
14961 if (b->number == bp_num)
14962 {
14963 break;
14964 }
14965
14966 if (!b || b->number != bp_num)
14967 error (_("Bad breakpoint number '%s'"), number);
14968
14969 p1 = dot+1;
14970 loc_num = get_number (&p1);
14971 if (loc_num == 0)
14972 error (_("Bad breakpoint location number '%s'"), number);
14973
14974 --loc_num;
14975 loc = b->loc;
14976 for (;loc_num && loc; --loc_num, loc = loc->next)
14977 ;
14978 if (!loc)
14979 error (_("Bad breakpoint location number '%s'"), dot+1);
14980
14981 return loc;
14982 }
14983
14984
14985 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14986 If from_tty is nonzero, it prints a message to that effect,
14987 which ends with a period (no newline). */
14988
14989 void
14990 disable_breakpoint (struct breakpoint *bpt)
14991 {
14992 /* Never disable a watchpoint scope breakpoint; we want to
14993 hit them when we leave scope so we can delete both the
14994 watchpoint and its scope breakpoint at that time. */
14995 if (bpt->type == bp_watchpoint_scope)
14996 return;
14997
14998 bpt->enable_state = bp_disabled;
14999
15000 /* Mark breakpoint locations modified. */
15001 mark_breakpoint_modified (bpt);
15002
15003 if (target_supports_enable_disable_tracepoint ()
15004 && current_trace_status ()->running && is_tracepoint (bpt))
15005 {
15006 struct bp_location *location;
15007
15008 for (location = bpt->loc; location; location = location->next)
15009 target_disable_tracepoint (location);
15010 }
15011
15012 update_global_location_list (UGLL_DONT_INSERT);
15013
15014 observer_notify_breakpoint_modified (bpt);
15015 }
15016
15017 /* A callback for iterate_over_related_breakpoints. */
15018
15019 static void
15020 do_disable_breakpoint (struct breakpoint *b, void *ignore)
15021 {
15022 disable_breakpoint (b);
15023 }
15024
15025 /* A callback for map_breakpoint_numbers that calls
15026 disable_breakpoint. */
15027
15028 static void
15029 do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
15030 {
15031 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
15032 }
15033
15034 static void
15035 disable_command (char *args, int from_tty)
15036 {
15037 if (args == 0)
15038 {
15039 struct breakpoint *bpt;
15040
15041 ALL_BREAKPOINTS (bpt)
15042 if (user_breakpoint_p (bpt))
15043 disable_breakpoint (bpt);
15044 }
15045 else
15046 {
15047 char *num = extract_arg (&args);
15048
15049 while (num)
15050 {
15051 if (strchr (num, '.'))
15052 {
15053 struct bp_location *loc = find_location_by_number (num);
15054
15055 if (loc)
15056 {
15057 if (loc->enabled)
15058 {
15059 loc->enabled = 0;
15060 mark_breakpoint_location_modified (loc);
15061 }
15062 if (target_supports_enable_disable_tracepoint ()
15063 && current_trace_status ()->running && loc->owner
15064 && is_tracepoint (loc->owner))
15065 target_disable_tracepoint (loc);
15066 }
15067 update_global_location_list (UGLL_DONT_INSERT);
15068 }
15069 else
15070 map_breakpoint_numbers (num, do_map_disable_breakpoint, NULL);
15071 num = extract_arg (&args);
15072 }
15073 }
15074 }
15075
15076 static void
15077 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
15078 int count)
15079 {
15080 int target_resources_ok;
15081
15082 if (bpt->type == bp_hardware_breakpoint)
15083 {
15084 int i;
15085 i = hw_breakpoint_used_count ();
15086 target_resources_ok =
15087 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
15088 i + 1, 0);
15089 if (target_resources_ok == 0)
15090 error (_("No hardware breakpoint support in the target."));
15091 else if (target_resources_ok < 0)
15092 error (_("Hardware breakpoints used exceeds limit."));
15093 }
15094
15095 if (is_watchpoint (bpt))
15096 {
15097 /* Initialize it just to avoid a GCC false warning. */
15098 enum enable_state orig_enable_state = 0;
15099
15100 TRY
15101 {
15102 struct watchpoint *w = (struct watchpoint *) bpt;
15103
15104 orig_enable_state = bpt->enable_state;
15105 bpt->enable_state = bp_enabled;
15106 update_watchpoint (w, 1 /* reparse */);
15107 }
15108 CATCH (e, RETURN_MASK_ALL)
15109 {
15110 bpt->enable_state = orig_enable_state;
15111 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
15112 bpt->number);
15113 return;
15114 }
15115 END_CATCH
15116 }
15117
15118 bpt->enable_state = bp_enabled;
15119
15120 /* Mark breakpoint locations modified. */
15121 mark_breakpoint_modified (bpt);
15122
15123 if (target_supports_enable_disable_tracepoint ()
15124 && current_trace_status ()->running && is_tracepoint (bpt))
15125 {
15126 struct bp_location *location;
15127
15128 for (location = bpt->loc; location; location = location->next)
15129 target_enable_tracepoint (location);
15130 }
15131
15132 bpt->disposition = disposition;
15133 bpt->enable_count = count;
15134 update_global_location_list (UGLL_MAY_INSERT);
15135
15136 observer_notify_breakpoint_modified (bpt);
15137 }
15138
15139
15140 void
15141 enable_breakpoint (struct breakpoint *bpt)
15142 {
15143 enable_breakpoint_disp (bpt, bpt->disposition, 0);
15144 }
15145
15146 static void
15147 do_enable_breakpoint (struct breakpoint *bpt, void *arg)
15148 {
15149 enable_breakpoint (bpt);
15150 }
15151
15152 /* A callback for map_breakpoint_numbers that calls
15153 enable_breakpoint. */
15154
15155 static void
15156 do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
15157 {
15158 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
15159 }
15160
15161 /* The enable command enables the specified breakpoints (or all defined
15162 breakpoints) so they once again become (or continue to be) effective
15163 in stopping the inferior. */
15164
15165 static void
15166 enable_command (char *args, int from_tty)
15167 {
15168 if (args == 0)
15169 {
15170 struct breakpoint *bpt;
15171
15172 ALL_BREAKPOINTS (bpt)
15173 if (user_breakpoint_p (bpt))
15174 enable_breakpoint (bpt);
15175 }
15176 else
15177 {
15178 char *num = extract_arg (&args);
15179
15180 while (num)
15181 {
15182 if (strchr (num, '.'))
15183 {
15184 struct bp_location *loc = find_location_by_number (num);
15185
15186 if (loc)
15187 {
15188 if (!loc->enabled)
15189 {
15190 loc->enabled = 1;
15191 mark_breakpoint_location_modified (loc);
15192 }
15193 if (target_supports_enable_disable_tracepoint ()
15194 && current_trace_status ()->running && loc->owner
15195 && is_tracepoint (loc->owner))
15196 target_enable_tracepoint (loc);
15197 }
15198 update_global_location_list (UGLL_MAY_INSERT);
15199 }
15200 else
15201 map_breakpoint_numbers (num, do_map_enable_breakpoint, NULL);
15202 num = extract_arg (&args);
15203 }
15204 }
15205 }
15206
15207 /* This struct packages up disposition data for application to multiple
15208 breakpoints. */
15209
15210 struct disp_data
15211 {
15212 enum bpdisp disp;
15213 int count;
15214 };
15215
15216 static void
15217 do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
15218 {
15219 struct disp_data disp_data = *(struct disp_data *) arg;
15220
15221 enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
15222 }
15223
15224 static void
15225 do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
15226 {
15227 struct disp_data disp = { disp_disable, 1 };
15228
15229 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
15230 }
15231
15232 static void
15233 enable_once_command (char *args, int from_tty)
15234 {
15235 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
15236 }
15237
15238 static void
15239 do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
15240 {
15241 struct disp_data disp = { disp_disable, *(int *) countptr };
15242
15243 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
15244 }
15245
15246 static void
15247 enable_count_command (char *args, int from_tty)
15248 {
15249 int count;
15250
15251 if (args == NULL)
15252 error_no_arg (_("hit count"));
15253
15254 count = get_number (&args);
15255
15256 map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
15257 }
15258
15259 static void
15260 do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
15261 {
15262 struct disp_data disp = { disp_del, 1 };
15263
15264 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
15265 }
15266
15267 static void
15268 enable_delete_command (char *args, int from_tty)
15269 {
15270 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
15271 }
15272 \f
15273 static void
15274 set_breakpoint_cmd (char *args, int from_tty)
15275 {
15276 }
15277
15278 static void
15279 show_breakpoint_cmd (char *args, int from_tty)
15280 {
15281 }
15282
15283 /* Invalidate last known value of any hardware watchpoint if
15284 the memory which that value represents has been written to by
15285 GDB itself. */
15286
15287 static void
15288 invalidate_bp_value_on_memory_change (struct inferior *inferior,
15289 CORE_ADDR addr, ssize_t len,
15290 const bfd_byte *data)
15291 {
15292 struct breakpoint *bp;
15293
15294 ALL_BREAKPOINTS (bp)
15295 if (bp->enable_state == bp_enabled
15296 && bp->type == bp_hardware_watchpoint)
15297 {
15298 struct watchpoint *wp = (struct watchpoint *) bp;
15299
15300 if (wp->val_valid && wp->val)
15301 {
15302 struct bp_location *loc;
15303
15304 for (loc = bp->loc; loc != NULL; loc = loc->next)
15305 if (loc->loc_type == bp_loc_hardware_watchpoint
15306 && loc->address + loc->length > addr
15307 && addr + len > loc->address)
15308 {
15309 value_free (wp->val);
15310 wp->val = NULL;
15311 wp->val_valid = 0;
15312 }
15313 }
15314 }
15315 }
15316
15317 /* Create and insert a breakpoint for software single step. */
15318
15319 void
15320 insert_single_step_breakpoint (struct gdbarch *gdbarch,
15321 struct address_space *aspace,
15322 CORE_ADDR next_pc)
15323 {
15324 struct thread_info *tp = inferior_thread ();
15325 struct symtab_and_line sal;
15326 CORE_ADDR pc = next_pc;
15327
15328 if (tp->control.single_step_breakpoints == NULL)
15329 {
15330 tp->control.single_step_breakpoints
15331 = new_single_step_breakpoint (tp->num, gdbarch);
15332 }
15333
15334 sal = find_pc_line (pc, 0);
15335 sal.pc = pc;
15336 sal.section = find_pc_overlay (pc);
15337 sal.explicit_pc = 1;
15338 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
15339
15340 update_global_location_list (UGLL_INSERT);
15341 }
15342
15343 /* See breakpoint.h. */
15344
15345 int
15346 breakpoint_has_location_inserted_here (struct breakpoint *bp,
15347 struct address_space *aspace,
15348 CORE_ADDR pc)
15349 {
15350 struct bp_location *loc;
15351
15352 for (loc = bp->loc; loc != NULL; loc = loc->next)
15353 if (loc->inserted
15354 && breakpoint_location_address_match (loc, aspace, pc))
15355 return 1;
15356
15357 return 0;
15358 }
15359
15360 /* Check whether a software single-step breakpoint is inserted at
15361 PC. */
15362
15363 int
15364 single_step_breakpoint_inserted_here_p (struct address_space *aspace,
15365 CORE_ADDR pc)
15366 {
15367 struct breakpoint *bpt;
15368
15369 ALL_BREAKPOINTS (bpt)
15370 {
15371 if (bpt->type == bp_single_step
15372 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
15373 return 1;
15374 }
15375 return 0;
15376 }
15377
15378 /* Returns 0 if 'bp' is NOT a syscall catchpoint,
15379 non-zero otherwise. */
15380 static int
15381 is_syscall_catchpoint_enabled (struct breakpoint *bp)
15382 {
15383 if (syscall_catchpoint_p (bp)
15384 && bp->enable_state != bp_disabled
15385 && bp->enable_state != bp_call_disabled)
15386 return 1;
15387 else
15388 return 0;
15389 }
15390
15391 int
15392 catch_syscall_enabled (void)
15393 {
15394 struct catch_syscall_inferior_data *inf_data
15395 = get_catch_syscall_inferior_data (current_inferior ());
15396
15397 return inf_data->total_syscalls_count != 0;
15398 }
15399
15400 int
15401 catching_syscall_number (int syscall_number)
15402 {
15403 struct breakpoint *bp;
15404
15405 ALL_BREAKPOINTS (bp)
15406 if (is_syscall_catchpoint_enabled (bp))
15407 {
15408 struct syscall_catchpoint *c = (struct syscall_catchpoint *) bp;
15409
15410 if (c->syscalls_to_be_caught)
15411 {
15412 int i, iter;
15413 for (i = 0;
15414 VEC_iterate (int, c->syscalls_to_be_caught, i, iter);
15415 i++)
15416 if (syscall_number == iter)
15417 return 1;
15418 }
15419 else
15420 return 1;
15421 }
15422
15423 return 0;
15424 }
15425
15426 /* Complete syscall names. Used by "catch syscall". */
15427 static VEC (char_ptr) *
15428 catch_syscall_completer (struct cmd_list_element *cmd,
15429 const char *text, const char *word)
15430 {
15431 const char **list = get_syscall_names (get_current_arch ());
15432 VEC (char_ptr) *retlist
15433 = (list == NULL) ? NULL : complete_on_enum (list, word, word);
15434
15435 xfree (list);
15436 return retlist;
15437 }
15438
15439 /* Tracepoint-specific operations. */
15440
15441 /* Set tracepoint count to NUM. */
15442 static void
15443 set_tracepoint_count (int num)
15444 {
15445 tracepoint_count = num;
15446 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
15447 }
15448
15449 static void
15450 trace_command (char *arg, int from_tty)
15451 {
15452 struct breakpoint_ops *ops;
15453 const char *arg_cp = arg;
15454
15455 if (arg && probe_linespec_to_ops (&arg_cp))
15456 ops = &tracepoint_probe_breakpoint_ops;
15457 else
15458 ops = &tracepoint_breakpoint_ops;
15459
15460 create_breakpoint (get_current_arch (),
15461 arg,
15462 NULL, 0, NULL, 1 /* parse arg */,
15463 0 /* tempflag */,
15464 bp_tracepoint /* type_wanted */,
15465 0 /* Ignore count */,
15466 pending_break_support,
15467 ops,
15468 from_tty,
15469 1 /* enabled */,
15470 0 /* internal */, 0);
15471 }
15472
15473 static void
15474 ftrace_command (char *arg, int from_tty)
15475 {
15476 create_breakpoint (get_current_arch (),
15477 arg,
15478 NULL, 0, NULL, 1 /* parse arg */,
15479 0 /* tempflag */,
15480 bp_fast_tracepoint /* type_wanted */,
15481 0 /* Ignore count */,
15482 pending_break_support,
15483 &tracepoint_breakpoint_ops,
15484 from_tty,
15485 1 /* enabled */,
15486 0 /* internal */, 0);
15487 }
15488
15489 /* strace command implementation. Creates a static tracepoint. */
15490
15491 static void
15492 strace_command (char *arg, int from_tty)
15493 {
15494 struct breakpoint_ops *ops;
15495
15496 /* Decide if we are dealing with a static tracepoint marker (`-m'),
15497 or with a normal static tracepoint. */
15498 if (arg && startswith (arg, "-m") && isspace (arg[2]))
15499 ops = &strace_marker_breakpoint_ops;
15500 else
15501 ops = &tracepoint_breakpoint_ops;
15502
15503 create_breakpoint (get_current_arch (),
15504 arg,
15505 NULL, 0, NULL, 1 /* parse arg */,
15506 0 /* tempflag */,
15507 bp_static_tracepoint /* type_wanted */,
15508 0 /* Ignore count */,
15509 pending_break_support,
15510 ops,
15511 from_tty,
15512 1 /* enabled */,
15513 0 /* internal */, 0);
15514 }
15515
15516 /* Set up a fake reader function that gets command lines from a linked
15517 list that was acquired during tracepoint uploading. */
15518
15519 static struct uploaded_tp *this_utp;
15520 static int next_cmd;
15521
15522 static char *
15523 read_uploaded_action (void)
15524 {
15525 char *rslt;
15526
15527 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
15528
15529 next_cmd++;
15530
15531 return rslt;
15532 }
15533
15534 /* Given information about a tracepoint as recorded on a target (which
15535 can be either a live system or a trace file), attempt to create an
15536 equivalent GDB tracepoint. This is not a reliable process, since
15537 the target does not necessarily have all the information used when
15538 the tracepoint was originally defined. */
15539
15540 struct tracepoint *
15541 create_tracepoint_from_upload (struct uploaded_tp *utp)
15542 {
15543 char *addr_str, small_buf[100];
15544 struct tracepoint *tp;
15545
15546 if (utp->at_string)
15547 addr_str = utp->at_string;
15548 else
15549 {
15550 /* In the absence of a source location, fall back to raw
15551 address. Since there is no way to confirm that the address
15552 means the same thing as when the trace was started, warn the
15553 user. */
15554 warning (_("Uploaded tracepoint %d has no "
15555 "source location, using raw address"),
15556 utp->number);
15557 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
15558 addr_str = small_buf;
15559 }
15560
15561 /* There's not much we can do with a sequence of bytecodes. */
15562 if (utp->cond && !utp->cond_string)
15563 warning (_("Uploaded tracepoint %d condition "
15564 "has no source form, ignoring it"),
15565 utp->number);
15566
15567 if (!create_breakpoint (get_current_arch (),
15568 addr_str,
15569 utp->cond_string, -1, NULL,
15570 0 /* parse cond/thread */,
15571 0 /* tempflag */,
15572 utp->type /* type_wanted */,
15573 0 /* Ignore count */,
15574 pending_break_support,
15575 &tracepoint_breakpoint_ops,
15576 0 /* from_tty */,
15577 utp->enabled /* enabled */,
15578 0 /* internal */,
15579 CREATE_BREAKPOINT_FLAGS_INSERTED))
15580 return NULL;
15581
15582 /* Get the tracepoint we just created. */
15583 tp = get_tracepoint (tracepoint_count);
15584 gdb_assert (tp != NULL);
15585
15586 if (utp->pass > 0)
15587 {
15588 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
15589 tp->base.number);
15590
15591 trace_pass_command (small_buf, 0);
15592 }
15593
15594 /* If we have uploaded versions of the original commands, set up a
15595 special-purpose "reader" function and call the usual command line
15596 reader, then pass the result to the breakpoint command-setting
15597 function. */
15598 if (!VEC_empty (char_ptr, utp->cmd_strings))
15599 {
15600 struct command_line *cmd_list;
15601
15602 this_utp = utp;
15603 next_cmd = 0;
15604
15605 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
15606
15607 breakpoint_set_commands (&tp->base, cmd_list);
15608 }
15609 else if (!VEC_empty (char_ptr, utp->actions)
15610 || !VEC_empty (char_ptr, utp->step_actions))
15611 warning (_("Uploaded tracepoint %d actions "
15612 "have no source form, ignoring them"),
15613 utp->number);
15614
15615 /* Copy any status information that might be available. */
15616 tp->base.hit_count = utp->hit_count;
15617 tp->traceframe_usage = utp->traceframe_usage;
15618
15619 return tp;
15620 }
15621
15622 /* Print information on tracepoint number TPNUM_EXP, or all if
15623 omitted. */
15624
15625 static void
15626 tracepoints_info (char *args, int from_tty)
15627 {
15628 struct ui_out *uiout = current_uiout;
15629 int num_printed;
15630
15631 num_printed = breakpoint_1 (args, 0, is_tracepoint);
15632
15633 if (num_printed == 0)
15634 {
15635 if (args == NULL || *args == '\0')
15636 ui_out_message (uiout, 0, "No tracepoints.\n");
15637 else
15638 ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
15639 }
15640
15641 default_collect_info ();
15642 }
15643
15644 /* The 'enable trace' command enables tracepoints.
15645 Not supported by all targets. */
15646 static void
15647 enable_trace_command (char *args, int from_tty)
15648 {
15649 enable_command (args, from_tty);
15650 }
15651
15652 /* The 'disable trace' command disables tracepoints.
15653 Not supported by all targets. */
15654 static void
15655 disable_trace_command (char *args, int from_tty)
15656 {
15657 disable_command (args, from_tty);
15658 }
15659
15660 /* Remove a tracepoint (or all if no argument). */
15661 static void
15662 delete_trace_command (char *arg, int from_tty)
15663 {
15664 struct breakpoint *b, *b_tmp;
15665
15666 dont_repeat ();
15667
15668 if (arg == 0)
15669 {
15670 int breaks_to_delete = 0;
15671
15672 /* Delete all breakpoints if no argument.
15673 Do not delete internal or call-dummy breakpoints, these
15674 have to be deleted with an explicit breakpoint number
15675 argument. */
15676 ALL_TRACEPOINTS (b)
15677 if (is_tracepoint (b) && user_breakpoint_p (b))
15678 {
15679 breaks_to_delete = 1;
15680 break;
15681 }
15682
15683 /* Ask user only if there are some breakpoints to delete. */
15684 if (!from_tty
15685 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
15686 {
15687 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15688 if (is_tracepoint (b) && user_breakpoint_p (b))
15689 delete_breakpoint (b);
15690 }
15691 }
15692 else
15693 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
15694 }
15695
15696 /* Helper function for trace_pass_command. */
15697
15698 static void
15699 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
15700 {
15701 tp->pass_count = count;
15702 observer_notify_breakpoint_modified (&tp->base);
15703 if (from_tty)
15704 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
15705 tp->base.number, count);
15706 }
15707
15708 /* Set passcount for tracepoint.
15709
15710 First command argument is passcount, second is tracepoint number.
15711 If tracepoint number omitted, apply to most recently defined.
15712 Also accepts special argument "all". */
15713
15714 static void
15715 trace_pass_command (char *args, int from_tty)
15716 {
15717 struct tracepoint *t1;
15718 unsigned int count;
15719
15720 if (args == 0 || *args == 0)
15721 error (_("passcount command requires an "
15722 "argument (count + optional TP num)"));
15723
15724 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
15725
15726 args = skip_spaces (args);
15727 if (*args && strncasecmp (args, "all", 3) == 0)
15728 {
15729 struct breakpoint *b;
15730
15731 args += 3; /* Skip special argument "all". */
15732 if (*args)
15733 error (_("Junk at end of arguments."));
15734
15735 ALL_TRACEPOINTS (b)
15736 {
15737 t1 = (struct tracepoint *) b;
15738 trace_pass_set_count (t1, count, from_tty);
15739 }
15740 }
15741 else if (*args == '\0')
15742 {
15743 t1 = get_tracepoint_by_number (&args, NULL);
15744 if (t1)
15745 trace_pass_set_count (t1, count, from_tty);
15746 }
15747 else
15748 {
15749 struct get_number_or_range_state state;
15750
15751 init_number_or_range (&state, args);
15752 while (!state.finished)
15753 {
15754 t1 = get_tracepoint_by_number (&args, &state);
15755 if (t1)
15756 trace_pass_set_count (t1, count, from_tty);
15757 }
15758 }
15759 }
15760
15761 struct tracepoint *
15762 get_tracepoint (int num)
15763 {
15764 struct breakpoint *t;
15765
15766 ALL_TRACEPOINTS (t)
15767 if (t->number == num)
15768 return (struct tracepoint *) t;
15769
15770 return NULL;
15771 }
15772
15773 /* Find the tracepoint with the given target-side number (which may be
15774 different from the tracepoint number after disconnecting and
15775 reconnecting). */
15776
15777 struct tracepoint *
15778 get_tracepoint_by_number_on_target (int num)
15779 {
15780 struct breakpoint *b;
15781
15782 ALL_TRACEPOINTS (b)
15783 {
15784 struct tracepoint *t = (struct tracepoint *) b;
15785
15786 if (t->number_on_target == num)
15787 return t;
15788 }
15789
15790 return NULL;
15791 }
15792
15793 /* Utility: parse a tracepoint number and look it up in the list.
15794 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
15795 If the argument is missing, the most recent tracepoint
15796 (tracepoint_count) is returned. */
15797
15798 struct tracepoint *
15799 get_tracepoint_by_number (char **arg,
15800 struct get_number_or_range_state *state)
15801 {
15802 struct breakpoint *t;
15803 int tpnum;
15804 char *instring = arg == NULL ? NULL : *arg;
15805
15806 if (state)
15807 {
15808 gdb_assert (!state->finished);
15809 tpnum = get_number_or_range (state);
15810 }
15811 else if (arg == NULL || *arg == NULL || ! **arg)
15812 tpnum = tracepoint_count;
15813 else
15814 tpnum = get_number (arg);
15815
15816 if (tpnum <= 0)
15817 {
15818 if (instring && *instring)
15819 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15820 instring);
15821 else
15822 printf_filtered (_("No previous tracepoint\n"));
15823 return NULL;
15824 }
15825
15826 ALL_TRACEPOINTS (t)
15827 if (t->number == tpnum)
15828 {
15829 return (struct tracepoint *) t;
15830 }
15831
15832 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15833 return NULL;
15834 }
15835
15836 void
15837 print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15838 {
15839 if (b->thread != -1)
15840 fprintf_unfiltered (fp, " thread %d", b->thread);
15841
15842 if (b->task != 0)
15843 fprintf_unfiltered (fp, " task %d", b->task);
15844
15845 fprintf_unfiltered (fp, "\n");
15846 }
15847
15848 /* Save information on user settable breakpoints (watchpoints, etc) to
15849 a new script file named FILENAME. If FILTER is non-NULL, call it
15850 on each breakpoint and only include the ones for which it returns
15851 non-zero. */
15852
15853 static void
15854 save_breakpoints (char *filename, int from_tty,
15855 int (*filter) (const struct breakpoint *))
15856 {
15857 struct breakpoint *tp;
15858 int any = 0;
15859 struct cleanup *cleanup;
15860 struct ui_file *fp;
15861 int extra_trace_bits = 0;
15862
15863 if (filename == 0 || *filename == 0)
15864 error (_("Argument required (file name in which to save)"));
15865
15866 /* See if we have anything to save. */
15867 ALL_BREAKPOINTS (tp)
15868 {
15869 /* Skip internal and momentary breakpoints. */
15870 if (!user_breakpoint_p (tp))
15871 continue;
15872
15873 /* If we have a filter, only save the breakpoints it accepts. */
15874 if (filter && !filter (tp))
15875 continue;
15876
15877 any = 1;
15878
15879 if (is_tracepoint (tp))
15880 {
15881 extra_trace_bits = 1;
15882
15883 /* We can stop searching. */
15884 break;
15885 }
15886 }
15887
15888 if (!any)
15889 {
15890 warning (_("Nothing to save."));
15891 return;
15892 }
15893
15894 filename = tilde_expand (filename);
15895 cleanup = make_cleanup (xfree, filename);
15896 fp = gdb_fopen (filename, "w");
15897 if (!fp)
15898 error (_("Unable to open file '%s' for saving (%s)"),
15899 filename, safe_strerror (errno));
15900 make_cleanup_ui_file_delete (fp);
15901
15902 if (extra_trace_bits)
15903 save_trace_state_variables (fp);
15904
15905 ALL_BREAKPOINTS (tp)
15906 {
15907 /* Skip internal and momentary breakpoints. */
15908 if (!user_breakpoint_p (tp))
15909 continue;
15910
15911 /* If we have a filter, only save the breakpoints it accepts. */
15912 if (filter && !filter (tp))
15913 continue;
15914
15915 tp->ops->print_recreate (tp, fp);
15916
15917 /* Note, we can't rely on tp->number for anything, as we can't
15918 assume the recreated breakpoint numbers will match. Use $bpnum
15919 instead. */
15920
15921 if (tp->cond_string)
15922 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
15923
15924 if (tp->ignore_count)
15925 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
15926
15927 if (tp->type != bp_dprintf && tp->commands)
15928 {
15929
15930 fprintf_unfiltered (fp, " commands\n");
15931
15932 ui_out_redirect (current_uiout, fp);
15933 TRY
15934 {
15935 print_command_lines (current_uiout, tp->commands->commands, 2);
15936 }
15937 ui_out_redirect (current_uiout, NULL);
15938
15939 CATCH (ex, RETURN_MASK_ALL)
15940 {
15941 throw_exception (ex);
15942 }
15943 END_CATCH
15944
15945 fprintf_unfiltered (fp, " end\n");
15946 }
15947
15948 if (tp->enable_state == bp_disabled)
15949 fprintf_unfiltered (fp, "disable $bpnum\n");
15950
15951 /* If this is a multi-location breakpoint, check if the locations
15952 should be individually disabled. Watchpoint locations are
15953 special, and not user visible. */
15954 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15955 {
15956 struct bp_location *loc;
15957 int n = 1;
15958
15959 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15960 if (!loc->enabled)
15961 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
15962 }
15963 }
15964
15965 if (extra_trace_bits && *default_collect)
15966 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
15967
15968 if (from_tty)
15969 printf_filtered (_("Saved to file '%s'.\n"), filename);
15970 do_cleanups (cleanup);
15971 }
15972
15973 /* The `save breakpoints' command. */
15974
15975 static void
15976 save_breakpoints_command (char *args, int from_tty)
15977 {
15978 save_breakpoints (args, from_tty, NULL);
15979 }
15980
15981 /* The `save tracepoints' command. */
15982
15983 static void
15984 save_tracepoints_command (char *args, int from_tty)
15985 {
15986 save_breakpoints (args, from_tty, is_tracepoint);
15987 }
15988
15989 /* Create a vector of all tracepoints. */
15990
15991 VEC(breakpoint_p) *
15992 all_tracepoints (void)
15993 {
15994 VEC(breakpoint_p) *tp_vec = 0;
15995 struct breakpoint *tp;
15996
15997 ALL_TRACEPOINTS (tp)
15998 {
15999 VEC_safe_push (breakpoint_p, tp_vec, tp);
16000 }
16001
16002 return tp_vec;
16003 }
16004
16005 \f
16006 /* This help string is used for the break, hbreak, tbreak and thbreak
16007 commands. It is defined as a macro to prevent duplication.
16008 COMMAND should be a string constant containing the name of the
16009 command. */
16010 #define BREAK_ARGS_HELP(command) \
16011 command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
16012 PROBE_MODIFIER shall be present if the command is to be placed in a\n\
16013 probe point. Accepted values are `-probe' (for a generic, automatically\n\
16014 guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
16015 `-probe-dtrace' (for a DTrace probe).\n\
16016 LOCATION may be a line number, function name, or \"*\" and an address.\n\
16017 If a line number is specified, break at start of code for that line.\n\
16018 If a function is specified, break at start of code for that function.\n\
16019 If an address is specified, break at that exact address.\n\
16020 With no LOCATION, uses current execution address of the selected\n\
16021 stack frame. This is useful for breaking on return to a stack frame.\n\
16022 \n\
16023 THREADNUM is the number from \"info threads\".\n\
16024 CONDITION is a boolean expression.\n\
16025 \n\
16026 Multiple breakpoints at one place are permitted, and useful if their\n\
16027 conditions are different.\n\
16028 \n\
16029 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
16030
16031 /* List of subcommands for "catch". */
16032 static struct cmd_list_element *catch_cmdlist;
16033
16034 /* List of subcommands for "tcatch". */
16035 static struct cmd_list_element *tcatch_cmdlist;
16036
16037 void
16038 add_catch_command (char *name, char *docstring,
16039 cmd_sfunc_ftype *sfunc,
16040 completer_ftype *completer,
16041 void *user_data_catch,
16042 void *user_data_tcatch)
16043 {
16044 struct cmd_list_element *command;
16045
16046 command = add_cmd (name, class_breakpoint, NULL, docstring,
16047 &catch_cmdlist);
16048 set_cmd_sfunc (command, sfunc);
16049 set_cmd_context (command, user_data_catch);
16050 set_cmd_completer (command, completer);
16051
16052 command = add_cmd (name, class_breakpoint, NULL, docstring,
16053 &tcatch_cmdlist);
16054 set_cmd_sfunc (command, sfunc);
16055 set_cmd_context (command, user_data_tcatch);
16056 set_cmd_completer (command, completer);
16057 }
16058
16059 static void
16060 clear_syscall_counts (struct inferior *inf)
16061 {
16062 struct catch_syscall_inferior_data *inf_data
16063 = get_catch_syscall_inferior_data (inf);
16064
16065 inf_data->total_syscalls_count = 0;
16066 inf_data->any_syscall_count = 0;
16067 VEC_free (int, inf_data->syscalls_counts);
16068 }
16069
16070 static void
16071 save_command (char *arg, int from_tty)
16072 {
16073 printf_unfiltered (_("\"save\" must be followed by "
16074 "the name of a save subcommand.\n"));
16075 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
16076 }
16077
16078 struct breakpoint *
16079 iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
16080 void *data)
16081 {
16082 struct breakpoint *b, *b_tmp;
16083
16084 ALL_BREAKPOINTS_SAFE (b, b_tmp)
16085 {
16086 if ((*callback) (b, data))
16087 return b;
16088 }
16089
16090 return NULL;
16091 }
16092
16093 /* Zero if any of the breakpoint's locations could be a location where
16094 functions have been inlined, nonzero otherwise. */
16095
16096 static int
16097 is_non_inline_function (struct breakpoint *b)
16098 {
16099 /* The shared library event breakpoint is set on the address of a
16100 non-inline function. */
16101 if (b->type == bp_shlib_event)
16102 return 1;
16103
16104 return 0;
16105 }
16106
16107 /* Nonzero if the specified PC cannot be a location where functions
16108 have been inlined. */
16109
16110 int
16111 pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
16112 const struct target_waitstatus *ws)
16113 {
16114 struct breakpoint *b;
16115 struct bp_location *bl;
16116
16117 ALL_BREAKPOINTS (b)
16118 {
16119 if (!is_non_inline_function (b))
16120 continue;
16121
16122 for (bl = b->loc; bl != NULL; bl = bl->next)
16123 {
16124 if (!bl->shlib_disabled
16125 && bpstat_check_location (bl, aspace, pc, ws))
16126 return 1;
16127 }
16128 }
16129
16130 return 0;
16131 }
16132
16133 /* Remove any references to OBJFILE which is going to be freed. */
16134
16135 void
16136 breakpoint_free_objfile (struct objfile *objfile)
16137 {
16138 struct bp_location **locp, *loc;
16139
16140 ALL_BP_LOCATIONS (loc, locp)
16141 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
16142 loc->symtab = NULL;
16143 }
16144
16145 void
16146 initialize_breakpoint_ops (void)
16147 {
16148 static int initialized = 0;
16149
16150 struct breakpoint_ops *ops;
16151
16152 if (initialized)
16153 return;
16154 initialized = 1;
16155
16156 /* The breakpoint_ops structure to be inherit by all kinds of
16157 breakpoints (real breakpoints, i.e., user "break" breakpoints,
16158 internal and momentary breakpoints, etc.). */
16159 ops = &bkpt_base_breakpoint_ops;
16160 *ops = base_breakpoint_ops;
16161 ops->re_set = bkpt_re_set;
16162 ops->insert_location = bkpt_insert_location;
16163 ops->remove_location = bkpt_remove_location;
16164 ops->breakpoint_hit = bkpt_breakpoint_hit;
16165 ops->create_sals_from_address = bkpt_create_sals_from_address;
16166 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
16167 ops->decode_linespec = bkpt_decode_linespec;
16168
16169 /* The breakpoint_ops structure to be used in regular breakpoints. */
16170 ops = &bkpt_breakpoint_ops;
16171 *ops = bkpt_base_breakpoint_ops;
16172 ops->re_set = bkpt_re_set;
16173 ops->resources_needed = bkpt_resources_needed;
16174 ops->print_it = bkpt_print_it;
16175 ops->print_mention = bkpt_print_mention;
16176 ops->print_recreate = bkpt_print_recreate;
16177
16178 /* Ranged breakpoints. */
16179 ops = &ranged_breakpoint_ops;
16180 *ops = bkpt_breakpoint_ops;
16181 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
16182 ops->resources_needed = resources_needed_ranged_breakpoint;
16183 ops->print_it = print_it_ranged_breakpoint;
16184 ops->print_one = print_one_ranged_breakpoint;
16185 ops->print_one_detail = print_one_detail_ranged_breakpoint;
16186 ops->print_mention = print_mention_ranged_breakpoint;
16187 ops->print_recreate = print_recreate_ranged_breakpoint;
16188
16189 /* Internal breakpoints. */
16190 ops = &internal_breakpoint_ops;
16191 *ops = bkpt_base_breakpoint_ops;
16192 ops->re_set = internal_bkpt_re_set;
16193 ops->check_status = internal_bkpt_check_status;
16194 ops->print_it = internal_bkpt_print_it;
16195 ops->print_mention = internal_bkpt_print_mention;
16196
16197 /* Momentary breakpoints. */
16198 ops = &momentary_breakpoint_ops;
16199 *ops = bkpt_base_breakpoint_ops;
16200 ops->re_set = momentary_bkpt_re_set;
16201 ops->check_status = momentary_bkpt_check_status;
16202 ops->print_it = momentary_bkpt_print_it;
16203 ops->print_mention = momentary_bkpt_print_mention;
16204
16205 /* Momentary breakpoints for bp_longjmp and bp_exception. */
16206 ops = &longjmp_breakpoint_ops;
16207 *ops = momentary_breakpoint_ops;
16208 ops->dtor = longjmp_bkpt_dtor;
16209
16210 /* Probe breakpoints. */
16211 ops = &bkpt_probe_breakpoint_ops;
16212 *ops = bkpt_breakpoint_ops;
16213 ops->insert_location = bkpt_probe_insert_location;
16214 ops->remove_location = bkpt_probe_remove_location;
16215 ops->create_sals_from_address = bkpt_probe_create_sals_from_address;
16216 ops->decode_linespec = bkpt_probe_decode_linespec;
16217
16218 /* Watchpoints. */
16219 ops = &watchpoint_breakpoint_ops;
16220 *ops = base_breakpoint_ops;
16221 ops->dtor = dtor_watchpoint;
16222 ops->re_set = re_set_watchpoint;
16223 ops->insert_location = insert_watchpoint;
16224 ops->remove_location = remove_watchpoint;
16225 ops->breakpoint_hit = breakpoint_hit_watchpoint;
16226 ops->check_status = check_status_watchpoint;
16227 ops->resources_needed = resources_needed_watchpoint;
16228 ops->works_in_software_mode = works_in_software_mode_watchpoint;
16229 ops->print_it = print_it_watchpoint;
16230 ops->print_mention = print_mention_watchpoint;
16231 ops->print_recreate = print_recreate_watchpoint;
16232 ops->explains_signal = explains_signal_watchpoint;
16233
16234 /* Masked watchpoints. */
16235 ops = &masked_watchpoint_breakpoint_ops;
16236 *ops = watchpoint_breakpoint_ops;
16237 ops->insert_location = insert_masked_watchpoint;
16238 ops->remove_location = remove_masked_watchpoint;
16239 ops->resources_needed = resources_needed_masked_watchpoint;
16240 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
16241 ops->print_it = print_it_masked_watchpoint;
16242 ops->print_one_detail = print_one_detail_masked_watchpoint;
16243 ops->print_mention = print_mention_masked_watchpoint;
16244 ops->print_recreate = print_recreate_masked_watchpoint;
16245
16246 /* Tracepoints. */
16247 ops = &tracepoint_breakpoint_ops;
16248 *ops = base_breakpoint_ops;
16249 ops->re_set = tracepoint_re_set;
16250 ops->breakpoint_hit = tracepoint_breakpoint_hit;
16251 ops->print_one_detail = tracepoint_print_one_detail;
16252 ops->print_mention = tracepoint_print_mention;
16253 ops->print_recreate = tracepoint_print_recreate;
16254 ops->create_sals_from_address = tracepoint_create_sals_from_address;
16255 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
16256 ops->decode_linespec = tracepoint_decode_linespec;
16257
16258 /* Probe tracepoints. */
16259 ops = &tracepoint_probe_breakpoint_ops;
16260 *ops = tracepoint_breakpoint_ops;
16261 ops->create_sals_from_address = tracepoint_probe_create_sals_from_address;
16262 ops->decode_linespec = tracepoint_probe_decode_linespec;
16263
16264 /* Static tracepoints with marker (`-m'). */
16265 ops = &strace_marker_breakpoint_ops;
16266 *ops = tracepoint_breakpoint_ops;
16267 ops->create_sals_from_address = strace_marker_create_sals_from_address;
16268 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
16269 ops->decode_linespec = strace_marker_decode_linespec;
16270
16271 /* Fork catchpoints. */
16272 ops = &catch_fork_breakpoint_ops;
16273 *ops = base_breakpoint_ops;
16274 ops->insert_location = insert_catch_fork;
16275 ops->remove_location = remove_catch_fork;
16276 ops->breakpoint_hit = breakpoint_hit_catch_fork;
16277 ops->print_it = print_it_catch_fork;
16278 ops->print_one = print_one_catch_fork;
16279 ops->print_mention = print_mention_catch_fork;
16280 ops->print_recreate = print_recreate_catch_fork;
16281
16282 /* Vfork catchpoints. */
16283 ops = &catch_vfork_breakpoint_ops;
16284 *ops = base_breakpoint_ops;
16285 ops->insert_location = insert_catch_vfork;
16286 ops->remove_location = remove_catch_vfork;
16287 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
16288 ops->print_it = print_it_catch_vfork;
16289 ops->print_one = print_one_catch_vfork;
16290 ops->print_mention = print_mention_catch_vfork;
16291 ops->print_recreate = print_recreate_catch_vfork;
16292
16293 /* Exec catchpoints. */
16294 ops = &catch_exec_breakpoint_ops;
16295 *ops = base_breakpoint_ops;
16296 ops->dtor = dtor_catch_exec;
16297 ops->insert_location = insert_catch_exec;
16298 ops->remove_location = remove_catch_exec;
16299 ops->breakpoint_hit = breakpoint_hit_catch_exec;
16300 ops->print_it = print_it_catch_exec;
16301 ops->print_one = print_one_catch_exec;
16302 ops->print_mention = print_mention_catch_exec;
16303 ops->print_recreate = print_recreate_catch_exec;
16304
16305 /* Syscall catchpoints. */
16306 ops = &catch_syscall_breakpoint_ops;
16307 *ops = base_breakpoint_ops;
16308 ops->dtor = dtor_catch_syscall;
16309 ops->insert_location = insert_catch_syscall;
16310 ops->remove_location = remove_catch_syscall;
16311 ops->breakpoint_hit = breakpoint_hit_catch_syscall;
16312 ops->print_it = print_it_catch_syscall;
16313 ops->print_one = print_one_catch_syscall;
16314 ops->print_mention = print_mention_catch_syscall;
16315 ops->print_recreate = print_recreate_catch_syscall;
16316
16317 /* Solib-related catchpoints. */
16318 ops = &catch_solib_breakpoint_ops;
16319 *ops = base_breakpoint_ops;
16320 ops->dtor = dtor_catch_solib;
16321 ops->insert_location = insert_catch_solib;
16322 ops->remove_location = remove_catch_solib;
16323 ops->breakpoint_hit = breakpoint_hit_catch_solib;
16324 ops->check_status = check_status_catch_solib;
16325 ops->print_it = print_it_catch_solib;
16326 ops->print_one = print_one_catch_solib;
16327 ops->print_mention = print_mention_catch_solib;
16328 ops->print_recreate = print_recreate_catch_solib;
16329
16330 ops = &dprintf_breakpoint_ops;
16331 *ops = bkpt_base_breakpoint_ops;
16332 ops->re_set = dprintf_re_set;
16333 ops->resources_needed = bkpt_resources_needed;
16334 ops->print_it = bkpt_print_it;
16335 ops->print_mention = bkpt_print_mention;
16336 ops->print_recreate = dprintf_print_recreate;
16337 ops->after_condition_true = dprintf_after_condition_true;
16338 ops->breakpoint_hit = dprintf_breakpoint_hit;
16339 }
16340
16341 /* Chain containing all defined "enable breakpoint" subcommands. */
16342
16343 static struct cmd_list_element *enablebreaklist = NULL;
16344
16345 void
16346 _initialize_breakpoint (void)
16347 {
16348 struct cmd_list_element *c;
16349
16350 initialize_breakpoint_ops ();
16351
16352 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
16353 observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
16354 observer_attach_inferior_exit (clear_syscall_counts);
16355 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
16356
16357 breakpoint_objfile_key
16358 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
16359
16360 catch_syscall_inferior_data
16361 = register_inferior_data_with_cleanup (NULL,
16362 catch_syscall_inferior_data_cleanup);
16363
16364 breakpoint_chain = 0;
16365 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
16366 before a breakpoint is set. */
16367 breakpoint_count = 0;
16368
16369 tracepoint_count = 0;
16370
16371 add_com ("ignore", class_breakpoint, ignore_command, _("\
16372 Set ignore-count of breakpoint number N to COUNT.\n\
16373 Usage is `ignore N COUNT'."));
16374 if (xdb_commands)
16375 add_com_alias ("bc", "ignore", class_breakpoint, 1);
16376
16377 add_com ("commands", class_breakpoint, commands_command, _("\
16378 Set commands to be executed when a breakpoint is hit.\n\
16379 Give breakpoint number as argument after \"commands\".\n\
16380 With no argument, the targeted breakpoint is the last one set.\n\
16381 The commands themselves follow starting on the next line.\n\
16382 Type a line containing \"end\" to indicate the end of them.\n\
16383 Give \"silent\" as the first line to make the breakpoint silent;\n\
16384 then no output is printed when it is hit, except what the commands print."));
16385
16386 c = add_com ("condition", class_breakpoint, condition_command, _("\
16387 Specify breakpoint number N to break only if COND is true.\n\
16388 Usage is `condition N COND', where N is an integer and COND is an\n\
16389 expression to be evaluated whenever breakpoint N is reached."));
16390 set_cmd_completer (c, condition_completer);
16391
16392 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
16393 Set a temporary breakpoint.\n\
16394 Like \"break\" except the breakpoint is only temporary,\n\
16395 so it will be deleted when hit. Equivalent to \"break\" followed\n\
16396 by using \"enable delete\" on the breakpoint number.\n\
16397 \n"
16398 BREAK_ARGS_HELP ("tbreak")));
16399 set_cmd_completer (c, location_completer);
16400
16401 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
16402 Set a hardware assisted breakpoint.\n\
16403 Like \"break\" except the breakpoint requires hardware support,\n\
16404 some target hardware may not have this support.\n\
16405 \n"
16406 BREAK_ARGS_HELP ("hbreak")));
16407 set_cmd_completer (c, location_completer);
16408
16409 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
16410 Set a temporary hardware assisted breakpoint.\n\
16411 Like \"hbreak\" except the breakpoint is only temporary,\n\
16412 so it will be deleted when hit.\n\
16413 \n"
16414 BREAK_ARGS_HELP ("thbreak")));
16415 set_cmd_completer (c, location_completer);
16416
16417 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
16418 Enable some breakpoints.\n\
16419 Give breakpoint numbers (separated by spaces) as arguments.\n\
16420 With no subcommand, breakpoints are enabled until you command otherwise.\n\
16421 This is used to cancel the effect of the \"disable\" command.\n\
16422 With a subcommand you can enable temporarily."),
16423 &enablelist, "enable ", 1, &cmdlist);
16424 if (xdb_commands)
16425 add_com ("ab", class_breakpoint, enable_command, _("\
16426 Enable some breakpoints.\n\
16427 Give breakpoint numbers (separated by spaces) as arguments.\n\
16428 With no subcommand, breakpoints are enabled until you command otherwise.\n\
16429 This is used to cancel the effect of the \"disable\" command.\n\
16430 With a subcommand you can enable temporarily."));
16431
16432 add_com_alias ("en", "enable", class_breakpoint, 1);
16433
16434 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
16435 Enable some breakpoints.\n\
16436 Give breakpoint numbers (separated by spaces) as arguments.\n\
16437 This is used to cancel the effect of the \"disable\" command.\n\
16438 May be abbreviated to simply \"enable\".\n"),
16439 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
16440
16441 add_cmd ("once", no_class, enable_once_command, _("\
16442 Enable breakpoints for one hit. Give breakpoint numbers.\n\
16443 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
16444 &enablebreaklist);
16445
16446 add_cmd ("delete", no_class, enable_delete_command, _("\
16447 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16448 If a breakpoint is hit while enabled in this fashion, it is deleted."),
16449 &enablebreaklist);
16450
16451 add_cmd ("count", no_class, enable_count_command, _("\
16452 Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16453 If a breakpoint is hit while enabled in this fashion,\n\
16454 the count is decremented; when it reaches zero, the breakpoint is disabled."),
16455 &enablebreaklist);
16456
16457 add_cmd ("delete", no_class, enable_delete_command, _("\
16458 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16459 If a breakpoint is hit while enabled in this fashion, it is deleted."),
16460 &enablelist);
16461
16462 add_cmd ("once", no_class, enable_once_command, _("\
16463 Enable breakpoints for one hit. Give breakpoint numbers.\n\
16464 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
16465 &enablelist);
16466
16467 add_cmd ("count", no_class, enable_count_command, _("\
16468 Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16469 If a breakpoint is hit while enabled in this fashion,\n\
16470 the count is decremented; when it reaches zero, the breakpoint is disabled."),
16471 &enablelist);
16472
16473 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
16474 Disable some breakpoints.\n\
16475 Arguments are breakpoint numbers with spaces in between.\n\
16476 To disable all breakpoints, give no argument.\n\
16477 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
16478 &disablelist, "disable ", 1, &cmdlist);
16479 add_com_alias ("dis", "disable", class_breakpoint, 1);
16480 add_com_alias ("disa", "disable", class_breakpoint, 1);
16481 if (xdb_commands)
16482 add_com ("sb", class_breakpoint, disable_command, _("\
16483 Disable some breakpoints.\n\
16484 Arguments are breakpoint numbers with spaces in between.\n\
16485 To disable all breakpoints, give no argument.\n\
16486 A disabled breakpoint is not forgotten, but has no effect until re-enabled."));
16487
16488 add_cmd ("breakpoints", class_alias, disable_command, _("\
16489 Disable some breakpoints.\n\
16490 Arguments are breakpoint numbers with spaces in between.\n\
16491 To disable all breakpoints, give no argument.\n\
16492 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
16493 This command may be abbreviated \"disable\"."),
16494 &disablelist);
16495
16496 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
16497 Delete some breakpoints or auto-display expressions.\n\
16498 Arguments are breakpoint numbers with spaces in between.\n\
16499 To delete all breakpoints, give no argument.\n\
16500 \n\
16501 Also a prefix command for deletion of other GDB objects.\n\
16502 The \"unset\" command is also an alias for \"delete\"."),
16503 &deletelist, "delete ", 1, &cmdlist);
16504 add_com_alias ("d", "delete", class_breakpoint, 1);
16505 add_com_alias ("del", "delete", class_breakpoint, 1);
16506 if (xdb_commands)
16507 add_com ("db", class_breakpoint, delete_command, _("\
16508 Delete some breakpoints.\n\
16509 Arguments are breakpoint numbers with spaces in between.\n\
16510 To delete all breakpoints, give no argument.\n"));
16511
16512 add_cmd ("breakpoints", class_alias, delete_command, _("\
16513 Delete some breakpoints or auto-display expressions.\n\
16514 Arguments are breakpoint numbers with spaces in between.\n\
16515 To delete all breakpoints, give no argument.\n\
16516 This command may be abbreviated \"delete\"."),
16517 &deletelist);
16518
16519 add_com ("clear", class_breakpoint, clear_command, _("\
16520 Clear breakpoint at specified line or function.\n\
16521 Argument may be line number, function name, or \"*\" and an address.\n\
16522 If line number is specified, all breakpoints in that line are cleared.\n\
16523 If function is specified, breakpoints at beginning of function are cleared.\n\
16524 If an address is specified, breakpoints at that address are cleared.\n\
16525 \n\
16526 With no argument, clears all breakpoints in the line that the selected frame\n\
16527 is executing in.\n\
16528 \n\
16529 See also the \"delete\" command which clears breakpoints by number."));
16530 add_com_alias ("cl", "clear", class_breakpoint, 1);
16531
16532 c = add_com ("break", class_breakpoint, break_command, _("\
16533 Set breakpoint at specified line or function.\n"
16534 BREAK_ARGS_HELP ("break")));
16535 set_cmd_completer (c, location_completer);
16536
16537 add_com_alias ("b", "break", class_run, 1);
16538 add_com_alias ("br", "break", class_run, 1);
16539 add_com_alias ("bre", "break", class_run, 1);
16540 add_com_alias ("brea", "break", class_run, 1);
16541
16542 if (xdb_commands)
16543 add_com_alias ("ba", "break", class_breakpoint, 1);
16544
16545 if (dbx_commands)
16546 {
16547 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
16548 Break in function/address or break at a line in the current file."),
16549 &stoplist, "stop ", 1, &cmdlist);
16550 add_cmd ("in", class_breakpoint, stopin_command,
16551 _("Break in function or address."), &stoplist);
16552 add_cmd ("at", class_breakpoint, stopat_command,
16553 _("Break at a line in the current file."), &stoplist);
16554 add_com ("status", class_info, breakpoints_info, _("\
16555 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
16556 The \"Type\" column indicates one of:\n\
16557 \tbreakpoint - normal breakpoint\n\
16558 \twatchpoint - watchpoint\n\
16559 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16560 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16561 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16562 address and file/line number respectively.\n\
16563 \n\
16564 Convenience variable \"$_\" and default examine address for \"x\"\n\
16565 are set to the address of the last breakpoint listed unless the command\n\
16566 is prefixed with \"server \".\n\n\
16567 Convenience variable \"$bpnum\" contains the number of the last\n\
16568 breakpoint set."));
16569 }
16570
16571 add_info ("breakpoints", breakpoints_info, _("\
16572 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
16573 The \"Type\" column indicates one of:\n\
16574 \tbreakpoint - normal breakpoint\n\
16575 \twatchpoint - watchpoint\n\
16576 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16577 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16578 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16579 address and file/line number respectively.\n\
16580 \n\
16581 Convenience variable \"$_\" and default examine address for \"x\"\n\
16582 are set to the address of the last breakpoint listed unless the command\n\
16583 is prefixed with \"server \".\n\n\
16584 Convenience variable \"$bpnum\" contains the number of the last\n\
16585 breakpoint set."));
16586
16587 add_info_alias ("b", "breakpoints", 1);
16588
16589 if (xdb_commands)
16590 add_com ("lb", class_breakpoint, breakpoints_info, _("\
16591 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
16592 The \"Type\" column indicates one of:\n\
16593 \tbreakpoint - normal breakpoint\n\
16594 \twatchpoint - watchpoint\n\
16595 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16596 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16597 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16598 address and file/line number respectively.\n\
16599 \n\
16600 Convenience variable \"$_\" and default examine address for \"x\"\n\
16601 are set to the address of the last breakpoint listed unless the command\n\
16602 is prefixed with \"server \".\n\n\
16603 Convenience variable \"$bpnum\" contains the number of the last\n\
16604 breakpoint set."));
16605
16606 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
16607 Status of all breakpoints, or breakpoint number NUMBER.\n\
16608 The \"Type\" column indicates one of:\n\
16609 \tbreakpoint - normal breakpoint\n\
16610 \twatchpoint - watchpoint\n\
16611 \tlongjmp - internal breakpoint used to step through longjmp()\n\
16612 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
16613 \tuntil - internal breakpoint used by the \"until\" command\n\
16614 \tfinish - internal breakpoint used by the \"finish\" command\n\
16615 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16616 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16617 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16618 address and file/line number respectively.\n\
16619 \n\
16620 Convenience variable \"$_\" and default examine address for \"x\"\n\
16621 are set to the address of the last breakpoint listed unless the command\n\
16622 is prefixed with \"server \".\n\n\
16623 Convenience variable \"$bpnum\" contains the number of the last\n\
16624 breakpoint set."),
16625 &maintenanceinfolist);
16626
16627 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
16628 Set catchpoints to catch events."),
16629 &catch_cmdlist, "catch ",
16630 0/*allow-unknown*/, &cmdlist);
16631
16632 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
16633 Set temporary catchpoints to catch events."),
16634 &tcatch_cmdlist, "tcatch ",
16635 0/*allow-unknown*/, &cmdlist);
16636
16637 add_catch_command ("fork", _("Catch calls to fork."),
16638 catch_fork_command_1,
16639 NULL,
16640 (void *) (uintptr_t) catch_fork_permanent,
16641 (void *) (uintptr_t) catch_fork_temporary);
16642 add_catch_command ("vfork", _("Catch calls to vfork."),
16643 catch_fork_command_1,
16644 NULL,
16645 (void *) (uintptr_t) catch_vfork_permanent,
16646 (void *) (uintptr_t) catch_vfork_temporary);
16647 add_catch_command ("exec", _("Catch calls to exec."),
16648 catch_exec_command_1,
16649 NULL,
16650 CATCH_PERMANENT,
16651 CATCH_TEMPORARY);
16652 add_catch_command ("load", _("Catch loads of shared libraries.\n\
16653 Usage: catch load [REGEX]\n\
16654 If REGEX is given, only stop for libraries matching the regular expression."),
16655 catch_load_command_1,
16656 NULL,
16657 CATCH_PERMANENT,
16658 CATCH_TEMPORARY);
16659 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
16660 Usage: catch unload [REGEX]\n\
16661 If REGEX is given, only stop for libraries matching the regular expression."),
16662 catch_unload_command_1,
16663 NULL,
16664 CATCH_PERMANENT,
16665 CATCH_TEMPORARY);
16666 add_catch_command ("syscall", _("\
16667 Catch system calls by their names and/or numbers.\n\
16668 Arguments say which system calls to catch. If no arguments\n\
16669 are given, every system call will be caught.\n\
16670 Arguments, if given, should be one or more system call names\n\
16671 (if your system supports that), or system call numbers."),
16672 catch_syscall_command_1,
16673 catch_syscall_completer,
16674 CATCH_PERMANENT,
16675 CATCH_TEMPORARY);
16676
16677 c = add_com ("watch", class_breakpoint, watch_command, _("\
16678 Set a watchpoint for an expression.\n\
16679 Usage: watch [-l|-location] EXPRESSION\n\
16680 A watchpoint stops execution of your program whenever the value of\n\
16681 an expression changes.\n\
16682 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16683 the memory to which it refers."));
16684 set_cmd_completer (c, expression_completer);
16685
16686 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
16687 Set a read watchpoint for an expression.\n\
16688 Usage: rwatch [-l|-location] EXPRESSION\n\
16689 A watchpoint stops execution of your program whenever the value of\n\
16690 an expression is read.\n\
16691 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16692 the memory to which it refers."));
16693 set_cmd_completer (c, expression_completer);
16694
16695 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
16696 Set a watchpoint for an expression.\n\
16697 Usage: awatch [-l|-location] EXPRESSION\n\
16698 A watchpoint stops execution of your program whenever the value of\n\
16699 an expression is either read or written.\n\
16700 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16701 the memory to which it refers."));
16702 set_cmd_completer (c, expression_completer);
16703
16704 add_info ("watchpoints", watchpoints_info, _("\
16705 Status of specified watchpoints (all watchpoints if no argument)."));
16706
16707 /* XXX: cagney/2005-02-23: This should be a boolean, and should
16708 respond to changes - contrary to the description. */
16709 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
16710 &can_use_hw_watchpoints, _("\
16711 Set debugger's willingness to use watchpoint hardware."), _("\
16712 Show debugger's willingness to use watchpoint hardware."), _("\
16713 If zero, gdb will not use hardware for new watchpoints, even if\n\
16714 such is available. (However, any hardware watchpoints that were\n\
16715 created before setting this to nonzero, will continue to use watchpoint\n\
16716 hardware.)"),
16717 NULL,
16718 show_can_use_hw_watchpoints,
16719 &setlist, &showlist);
16720
16721 can_use_hw_watchpoints = 1;
16722
16723 /* Tracepoint manipulation commands. */
16724
16725 c = add_com ("trace", class_breakpoint, trace_command, _("\
16726 Set a tracepoint at specified line or function.\n\
16727 \n"
16728 BREAK_ARGS_HELP ("trace") "\n\
16729 Do \"help tracepoints\" for info on other tracepoint commands."));
16730 set_cmd_completer (c, location_completer);
16731
16732 add_com_alias ("tp", "trace", class_alias, 0);
16733 add_com_alias ("tr", "trace", class_alias, 1);
16734 add_com_alias ("tra", "trace", class_alias, 1);
16735 add_com_alias ("trac", "trace", class_alias, 1);
16736
16737 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
16738 Set a fast tracepoint at specified line or function.\n\
16739 \n"
16740 BREAK_ARGS_HELP ("ftrace") "\n\
16741 Do \"help tracepoints\" for info on other tracepoint commands."));
16742 set_cmd_completer (c, location_completer);
16743
16744 c = add_com ("strace", class_breakpoint, strace_command, _("\
16745 Set a static tracepoint at specified line, function or marker.\n\
16746 \n\
16747 strace [LOCATION] [if CONDITION]\n\
16748 LOCATION may be a line number, function name, \"*\" and an address,\n\
16749 or -m MARKER_ID.\n\
16750 If a line number is specified, probe the marker at start of code\n\
16751 for that line. If a function is specified, probe the marker at start\n\
16752 of code for that function. If an address is specified, probe the marker\n\
16753 at that exact address. If a marker id is specified, probe the marker\n\
16754 with that name. With no LOCATION, uses current execution address of\n\
16755 the selected stack frame.\n\
16756 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16757 This collects arbitrary user data passed in the probe point call to the\n\
16758 tracing library. You can inspect it when analyzing the trace buffer,\n\
16759 by printing the $_sdata variable like any other convenience variable.\n\
16760 \n\
16761 CONDITION is a boolean expression.\n\
16762 \n\
16763 Multiple tracepoints at one place are permitted, and useful if their\n\
16764 conditions are different.\n\
16765 \n\
16766 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16767 Do \"help tracepoints\" for info on other tracepoint commands."));
16768 set_cmd_completer (c, location_completer);
16769
16770 add_info ("tracepoints", tracepoints_info, _("\
16771 Status of specified tracepoints (all tracepoints if no argument).\n\
16772 Convenience variable \"$tpnum\" contains the number of the\n\
16773 last tracepoint set."));
16774
16775 add_info_alias ("tp", "tracepoints", 1);
16776
16777 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16778 Delete specified tracepoints.\n\
16779 Arguments are tracepoint numbers, separated by spaces.\n\
16780 No argument means delete all tracepoints."),
16781 &deletelist);
16782 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
16783
16784 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16785 Disable specified tracepoints.\n\
16786 Arguments are tracepoint numbers, separated by spaces.\n\
16787 No argument means disable all tracepoints."),
16788 &disablelist);
16789 deprecate_cmd (c, "disable");
16790
16791 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16792 Enable specified tracepoints.\n\
16793 Arguments are tracepoint numbers, separated by spaces.\n\
16794 No argument means enable all tracepoints."),
16795 &enablelist);
16796 deprecate_cmd (c, "enable");
16797
16798 add_com ("passcount", class_trace, trace_pass_command, _("\
16799 Set the passcount for a tracepoint.\n\
16800 The trace will end when the tracepoint has been passed 'count' times.\n\
16801 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16802 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16803
16804 add_prefix_cmd ("save", class_breakpoint, save_command,
16805 _("Save breakpoint definitions as a script."),
16806 &save_cmdlist, "save ",
16807 0/*allow-unknown*/, &cmdlist);
16808
16809 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16810 Save current breakpoint definitions as a script.\n\
16811 This includes all types of breakpoints (breakpoints, watchpoints,\n\
16812 catchpoints, tracepoints). Use the 'source' command in another debug\n\
16813 session to restore them."),
16814 &save_cmdlist);
16815 set_cmd_completer (c, filename_completer);
16816
16817 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
16818 Save current tracepoint definitions as a script.\n\
16819 Use the 'source' command in another debug session to restore them."),
16820 &save_cmdlist);
16821 set_cmd_completer (c, filename_completer);
16822
16823 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16824 deprecate_cmd (c, "save tracepoints");
16825
16826 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
16827 Breakpoint specific settings\n\
16828 Configure various breakpoint-specific variables such as\n\
16829 pending breakpoint behavior"),
16830 &breakpoint_set_cmdlist, "set breakpoint ",
16831 0/*allow-unknown*/, &setlist);
16832 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
16833 Breakpoint specific settings\n\
16834 Configure various breakpoint-specific variables such as\n\
16835 pending breakpoint behavior"),
16836 &breakpoint_show_cmdlist, "show breakpoint ",
16837 0/*allow-unknown*/, &showlist);
16838
16839 add_setshow_auto_boolean_cmd ("pending", no_class,
16840 &pending_break_support, _("\
16841 Set debugger's behavior regarding pending breakpoints."), _("\
16842 Show debugger's behavior regarding pending breakpoints."), _("\
16843 If on, an unrecognized breakpoint location will cause gdb to create a\n\
16844 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16845 an error. If auto, an unrecognized breakpoint location results in a\n\
16846 user-query to see if a pending breakpoint should be created."),
16847 NULL,
16848 show_pending_break_support,
16849 &breakpoint_set_cmdlist,
16850 &breakpoint_show_cmdlist);
16851
16852 pending_break_support = AUTO_BOOLEAN_AUTO;
16853
16854 add_setshow_boolean_cmd ("auto-hw", no_class,
16855 &automatic_hardware_breakpoints, _("\
16856 Set automatic usage of hardware breakpoints."), _("\
16857 Show automatic usage of hardware breakpoints."), _("\
16858 If set, the debugger will automatically use hardware breakpoints for\n\
16859 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16860 a warning will be emitted for such breakpoints."),
16861 NULL,
16862 show_automatic_hardware_breakpoints,
16863 &breakpoint_set_cmdlist,
16864 &breakpoint_show_cmdlist);
16865
16866 add_setshow_boolean_cmd ("always-inserted", class_support,
16867 &always_inserted_mode, _("\
16868 Set mode for inserting breakpoints."), _("\
16869 Show mode for inserting breakpoints."), _("\
16870 When this mode is on, breakpoints are inserted immediately as soon as\n\
16871 they're created, kept inserted even when execution stops, and removed\n\
16872 only when the user deletes them. When this mode is off (the default),\n\
16873 breakpoints are inserted only when execution continues, and removed\n\
16874 when execution stops."),
16875 NULL,
16876 &show_always_inserted_mode,
16877 &breakpoint_set_cmdlist,
16878 &breakpoint_show_cmdlist);
16879
16880 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16881 condition_evaluation_enums,
16882 &condition_evaluation_mode_1, _("\
16883 Set mode of breakpoint condition evaluation."), _("\
16884 Show mode of breakpoint condition evaluation."), _("\
16885 When this is set to \"host\", breakpoint conditions will be\n\
16886 evaluated on the host's side by GDB. When it is set to \"target\",\n\
16887 breakpoint conditions will be downloaded to the target (if the target\n\
16888 supports such feature) and conditions will be evaluated on the target's side.\n\
16889 If this is set to \"auto\" (default), this will be automatically set to\n\
16890 \"target\" if it supports condition evaluation, otherwise it will\n\
16891 be set to \"gdb\""),
16892 &set_condition_evaluation_mode,
16893 &show_condition_evaluation_mode,
16894 &breakpoint_set_cmdlist,
16895 &breakpoint_show_cmdlist);
16896
16897 add_com ("break-range", class_breakpoint, break_range_command, _("\
16898 Set a breakpoint for an address range.\n\
16899 break-range START-LOCATION, END-LOCATION\n\
16900 where START-LOCATION and END-LOCATION can be one of the following:\n\
16901 LINENUM, for that line in the current file,\n\
16902 FILE:LINENUM, for that line in that file,\n\
16903 +OFFSET, for that number of lines after the current line\n\
16904 or the start of the range\n\
16905 FUNCTION, for the first line in that function,\n\
16906 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16907 *ADDRESS, for the instruction at that address.\n\
16908 \n\
16909 The breakpoint will stop execution of the inferior whenever it executes\n\
16910 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16911 range (including START-LOCATION and END-LOCATION)."));
16912
16913 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
16914 Set a dynamic printf at specified line or function.\n\
16915 dprintf location,format string,arg1,arg2,...\n\
16916 location may be a line number, function name, or \"*\" and an address.\n\
16917 If a line number is specified, break at start of code for that line.\n\
16918 If a function is specified, break at start of code for that function."));
16919 set_cmd_completer (c, location_completer);
16920
16921 add_setshow_enum_cmd ("dprintf-style", class_support,
16922 dprintf_style_enums, &dprintf_style, _("\
16923 Set the style of usage for dynamic printf."), _("\
16924 Show the style of usage for dynamic printf."), _("\
16925 This setting chooses how GDB will do a dynamic printf.\n\
16926 If the value is \"gdb\", then the printing is done by GDB to its own\n\
16927 console, as with the \"printf\" command.\n\
16928 If the value is \"call\", the print is done by calling a function in your\n\
16929 program; by default printf(), but you can choose a different function or\n\
16930 output stream by setting dprintf-function and dprintf-channel."),
16931 update_dprintf_commands, NULL,
16932 &setlist, &showlist);
16933
16934 dprintf_function = xstrdup ("printf");
16935 add_setshow_string_cmd ("dprintf-function", class_support,
16936 &dprintf_function, _("\
16937 Set the function to use for dynamic printf"), _("\
16938 Show the function to use for dynamic printf"), NULL,
16939 update_dprintf_commands, NULL,
16940 &setlist, &showlist);
16941
16942 dprintf_channel = xstrdup ("");
16943 add_setshow_string_cmd ("dprintf-channel", class_support,
16944 &dprintf_channel, _("\
16945 Set the channel to use for dynamic printf"), _("\
16946 Show the channel to use for dynamic printf"), NULL,
16947 update_dprintf_commands, NULL,
16948 &setlist, &showlist);
16949
16950 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16951 &disconnected_dprintf, _("\
16952 Set whether dprintf continues after GDB disconnects."), _("\
16953 Show whether dprintf continues after GDB disconnects."), _("\
16954 Use this to let dprintf commands continue to hit and produce output\n\
16955 even if GDB disconnects or detaches from the target."),
16956 NULL,
16957 NULL,
16958 &setlist, &showlist);
16959
16960 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16961 agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16962 (target agent only) This is useful for formatted output in user-defined commands."));
16963
16964 automatic_hardware_breakpoints = 1;
16965
16966 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
16967 observer_attach_thread_exit (remove_threaded_breakpoints);
16968 }
This page took 0.91875 seconds and 4 git commands to generate.