Automatic date update in version.in
[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 "parser-defs.h"
60 #include "gdb_regex.h"
61 #include "probe.h"
62 #include "cli/cli-utils.h"
63 #include "continuations.h"
64 #include "stack.h"
65 #include "skip.h"
66 #include "ax-gdb.h"
67 #include "dummy-frame.h"
68 #include "interps.h"
69 #include "format.h"
70
71 /* readline include files */
72 #include "readline/readline.h"
73 #include "readline/history.h"
74
75 /* readline defines this. */
76 #undef savestring
77
78 #include "mi/mi-common.h"
79 #include "extension.h"
80
81 /* Enums for exception-handling support. */
82 enum exception_event_kind
83 {
84 EX_EVENT_THROW,
85 EX_EVENT_RETHROW,
86 EX_EVENT_CATCH
87 };
88
89 /* Prototypes for local functions. */
90
91 static void enable_delete_command (char *, int);
92
93 static void enable_once_command (char *, int);
94
95 static void enable_count_command (char *, int);
96
97 static void disable_command (char *, int);
98
99 static void enable_command (char *, int);
100
101 static void map_breakpoint_numbers (char *, void (*) (struct breakpoint *,
102 void *),
103 void *);
104
105 static void ignore_command (char *, int);
106
107 static int breakpoint_re_set_one (void *);
108
109 static void breakpoint_re_set_default (struct breakpoint *);
110
111 static void create_sals_from_address_default (char **,
112 struct linespec_result *,
113 enum bptype, char *,
114 char **);
115
116 static void create_breakpoints_sal_default (struct gdbarch *,
117 struct linespec_result *,
118 char *, char *, enum bptype,
119 enum bpdisp, int, int,
120 int,
121 const struct breakpoint_ops *,
122 int, int, int, unsigned);
123
124 static void decode_linespec_default (struct breakpoint *, char **,
125 struct symtabs_and_lines *);
126
127 static void clear_command (char *, int);
128
129 static void catch_command (char *, int);
130
131 static int can_use_hardware_watchpoint (struct value *);
132
133 static void break_command_1 (char *, int, int);
134
135 static void mention (struct breakpoint *);
136
137 static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
138 enum bptype,
139 const struct breakpoint_ops *);
140 static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
141 const struct symtab_and_line *);
142
143 /* This function is used in gdbtk sources and thus can not be made
144 static. */
145 struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
146 struct symtab_and_line,
147 enum bptype,
148 const struct breakpoint_ops *);
149
150 static struct breakpoint *
151 momentary_breakpoint_from_master (struct breakpoint *orig,
152 enum bptype type,
153 const struct breakpoint_ops *ops,
154 int loc_enabled);
155
156 static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
157
158 static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
159 CORE_ADDR bpaddr,
160 enum bptype bptype);
161
162 static void describe_other_breakpoints (struct gdbarch *,
163 struct program_space *, CORE_ADDR,
164 struct obj_section *, int);
165
166 static int watchpoint_locations_match (struct bp_location *loc1,
167 struct bp_location *loc2);
168
169 static int breakpoint_location_address_match (struct bp_location *bl,
170 struct address_space *aspace,
171 CORE_ADDR addr);
172
173 static void breakpoints_info (char *, int);
174
175 static void watchpoints_info (char *, int);
176
177 static int breakpoint_1 (char *, int,
178 int (*) (const struct breakpoint *));
179
180 static int breakpoint_cond_eval (void *);
181
182 static void cleanup_executing_breakpoints (void *);
183
184 static void commands_command (char *, int);
185
186 static void condition_command (char *, int);
187
188 typedef enum
189 {
190 mark_inserted,
191 mark_uninserted
192 }
193 insertion_state_t;
194
195 static int remove_breakpoint (struct bp_location *, insertion_state_t);
196 static int remove_breakpoint_1 (struct bp_location *, insertion_state_t);
197
198 static enum print_stop_action print_bp_stop_message (bpstat bs);
199
200 static int watchpoint_check (void *);
201
202 static void maintenance_info_breakpoints (char *, int);
203
204 static int hw_breakpoint_used_count (void);
205
206 static int hw_watchpoint_use_count (struct breakpoint *);
207
208 static int hw_watchpoint_used_count_others (struct breakpoint *except,
209 enum bptype type,
210 int *other_type_used);
211
212 static void hbreak_command (char *, int);
213
214 static void thbreak_command (char *, int);
215
216 static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
217 int count);
218
219 static void stop_command (char *arg, int from_tty);
220
221 static void stopin_command (char *arg, int from_tty);
222
223 static void stopat_command (char *arg, int from_tty);
224
225 static void tcatch_command (char *arg, int from_tty);
226
227 static void free_bp_location (struct bp_location *loc);
228 static void incref_bp_location (struct bp_location *loc);
229 static void decref_bp_location (struct bp_location **loc);
230
231 static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
232
233 /* update_global_location_list's modes of operation wrt to whether to
234 insert locations now. */
235 enum ugll_insert_mode
236 {
237 /* Don't insert any breakpoint locations into the inferior, only
238 remove already-inserted locations that no longer should be
239 inserted. Functions that delete a breakpoint or breakpoints
240 should specify this mode, so that deleting a breakpoint doesn't
241 have the side effect of inserting the locations of other
242 breakpoints that are marked not-inserted, but should_be_inserted
243 returns true on them.
244
245 This behavior is useful is situations close to tear-down -- e.g.,
246 after an exec, while the target still has execution, but
247 breakpoint shadows of the previous executable image should *NOT*
248 be restored to the new image; or before detaching, where the
249 target still has execution and wants to delete breakpoints from
250 GDB's lists, and all breakpoints had already been removed from
251 the inferior. */
252 UGLL_DONT_INSERT,
253
254 /* May insert breakpoints iff breakpoints_should_be_inserted_now
255 claims breakpoints should be inserted now. */
256 UGLL_MAY_INSERT,
257
258 /* Insert locations now, irrespective of
259 breakpoints_should_be_inserted_now. E.g., say all threads are
260 stopped right now, and the user did "continue". We need to
261 insert breakpoints _before_ resuming the target, but
262 UGLL_MAY_INSERT wouldn't insert them, because
263 breakpoints_should_be_inserted_now returns false at that point,
264 as no thread is running yet. */
265 UGLL_INSERT
266 };
267
268 static void update_global_location_list (enum ugll_insert_mode);
269
270 static void update_global_location_list_nothrow (enum ugll_insert_mode);
271
272 static int is_hardware_watchpoint (const struct breakpoint *bpt);
273
274 static void insert_breakpoint_locations (void);
275
276 static void tracepoints_info (char *, int);
277
278 static void delete_trace_command (char *, int);
279
280 static void enable_trace_command (char *, int);
281
282 static void disable_trace_command (char *, int);
283
284 static void trace_pass_command (char *, int);
285
286 static void set_tracepoint_count (int num);
287
288 static int is_masked_watchpoint (const struct breakpoint *b);
289
290 static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
291
292 /* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
293 otherwise. */
294
295 static int strace_marker_p (struct breakpoint *b);
296
297 /* The breakpoint_ops structure to be inherited by all breakpoint_ops
298 that are implemented on top of software or hardware breakpoints
299 (user breakpoints, internal and momentary breakpoints, etc.). */
300 static struct breakpoint_ops bkpt_base_breakpoint_ops;
301
302 /* Internal breakpoints class type. */
303 static struct breakpoint_ops internal_breakpoint_ops;
304
305 /* Momentary breakpoints class type. */
306 static struct breakpoint_ops momentary_breakpoint_ops;
307
308 /* Momentary breakpoints for bp_longjmp and bp_exception class type. */
309 static struct breakpoint_ops longjmp_breakpoint_ops;
310
311 /* The breakpoint_ops structure to be used in regular user created
312 breakpoints. */
313 struct breakpoint_ops bkpt_breakpoint_ops;
314
315 /* Breakpoints set on probes. */
316 static struct breakpoint_ops bkpt_probe_breakpoint_ops;
317
318 /* Dynamic printf class type. */
319 struct breakpoint_ops dprintf_breakpoint_ops;
320
321 /* The style in which to perform a dynamic printf. This is a user
322 option because different output options have different tradeoffs;
323 if GDB does the printing, there is better error handling if there
324 is a problem with any of the arguments, but using an inferior
325 function lets you have special-purpose printers and sending of
326 output to the same place as compiled-in print functions. */
327
328 static const char dprintf_style_gdb[] = "gdb";
329 static const char dprintf_style_call[] = "call";
330 static const char dprintf_style_agent[] = "agent";
331 static const char *const dprintf_style_enums[] = {
332 dprintf_style_gdb,
333 dprintf_style_call,
334 dprintf_style_agent,
335 NULL
336 };
337 static const char *dprintf_style = dprintf_style_gdb;
338
339 /* The function to use for dynamic printf if the preferred style is to
340 call into the inferior. The value is simply a string that is
341 copied into the command, so it can be anything that GDB can
342 evaluate to a callable address, not necessarily a function name. */
343
344 static char *dprintf_function = "";
345
346 /* The channel to use for dynamic printf if the preferred style is to
347 call into the inferior; if a nonempty string, it will be passed to
348 the call as the first argument, with the format string as the
349 second. As with the dprintf function, this can be anything that
350 GDB knows how to evaluate, so in addition to common choices like
351 "stderr", this could be an app-specific expression like
352 "mystreams[curlogger]". */
353
354 static char *dprintf_channel = "";
355
356 /* True if dprintf commands should continue to operate even if GDB
357 has disconnected. */
358 static int disconnected_dprintf = 1;
359
360 /* A reference-counted struct command_line. This lets multiple
361 breakpoints share a single command list. */
362 struct counted_command_line
363 {
364 /* The reference count. */
365 int refc;
366
367 /* The command list. */
368 struct command_line *commands;
369 };
370
371 struct command_line *
372 breakpoint_commands (struct breakpoint *b)
373 {
374 return b->commands ? b->commands->commands : NULL;
375 }
376
377 /* Flag indicating that a command has proceeded the inferior past the
378 current breakpoint. */
379
380 static int breakpoint_proceeded;
381
382 const char *
383 bpdisp_text (enum bpdisp disp)
384 {
385 /* NOTE: the following values are a part of MI protocol and
386 represent values of 'disp' field returned when inferior stops at
387 a breakpoint. */
388 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
389
390 return bpdisps[(int) disp];
391 }
392
393 /* Prototypes for exported functions. */
394 /* If FALSE, gdb will not use hardware support for watchpoints, even
395 if such is available. */
396 static int can_use_hw_watchpoints;
397
398 static void
399 show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
400 struct cmd_list_element *c,
401 const char *value)
402 {
403 fprintf_filtered (file,
404 _("Debugger's willingness to use "
405 "watchpoint hardware is %s.\n"),
406 value);
407 }
408
409 /* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
410 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
411 for unrecognized breakpoint locations.
412 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
413 static enum auto_boolean pending_break_support;
414 static void
415 show_pending_break_support (struct ui_file *file, int from_tty,
416 struct cmd_list_element *c,
417 const char *value)
418 {
419 fprintf_filtered (file,
420 _("Debugger's behavior regarding "
421 "pending breakpoints is %s.\n"),
422 value);
423 }
424
425 /* If 1, gdb will automatically use hardware breakpoints for breakpoints
426 set with "break" but falling in read-only memory.
427 If 0, gdb will warn about such breakpoints, but won't automatically
428 use hardware breakpoints. */
429 static int automatic_hardware_breakpoints;
430 static void
431 show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
432 struct cmd_list_element *c,
433 const char *value)
434 {
435 fprintf_filtered (file,
436 _("Automatic usage of hardware breakpoints is %s.\n"),
437 value);
438 }
439
440 /* If on, GDB keeps breakpoints inserted even if the inferior is
441 stopped, and immediately inserts any new breakpoints as soon as
442 they're created. If off (default), GDB keeps breakpoints off of
443 the target as long as possible. That is, it delays inserting
444 breakpoints until the next resume, and removes them again when the
445 target fully stops. This is a bit safer in case GDB crashes while
446 processing user input. */
447 static int always_inserted_mode = 0;
448
449 static void
450 show_always_inserted_mode (struct ui_file *file, int from_tty,
451 struct cmd_list_element *c, const char *value)
452 {
453 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
454 value);
455 }
456
457 /* See breakpoint.h. */
458
459 int
460 breakpoints_should_be_inserted_now (void)
461 {
462 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
463 {
464 /* If breakpoints are global, they should be inserted even if no
465 thread under gdb's control is running, or even if there are
466 no threads under GDB's control yet. */
467 return 1;
468 }
469 else if (target_has_execution)
470 {
471 struct thread_info *tp;
472
473 if (always_inserted_mode)
474 {
475 /* The user wants breakpoints inserted even if all threads
476 are stopped. */
477 return 1;
478 }
479
480 if (threads_are_executing ())
481 return 1;
482
483 /* Don't remove breakpoints yet if, even though all threads are
484 stopped, we still have events to process. */
485 ALL_NON_EXITED_THREADS (tp)
486 if (tp->resumed
487 && tp->suspend.waitstatus_pending_p)
488 return 1;
489 }
490 return 0;
491 }
492
493 static const char condition_evaluation_both[] = "host or target";
494
495 /* Modes for breakpoint condition evaluation. */
496 static const char condition_evaluation_auto[] = "auto";
497 static const char condition_evaluation_host[] = "host";
498 static const char condition_evaluation_target[] = "target";
499 static const char *const condition_evaluation_enums[] = {
500 condition_evaluation_auto,
501 condition_evaluation_host,
502 condition_evaluation_target,
503 NULL
504 };
505
506 /* Global that holds the current mode for breakpoint condition evaluation. */
507 static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
508
509 /* Global that we use to display information to the user (gets its value from
510 condition_evaluation_mode_1. */
511 static const char *condition_evaluation_mode = condition_evaluation_auto;
512
513 /* Translate a condition evaluation mode MODE into either "host"
514 or "target". This is used mostly to translate from "auto" to the
515 real setting that is being used. It returns the translated
516 evaluation mode. */
517
518 static const char *
519 translate_condition_evaluation_mode (const char *mode)
520 {
521 if (mode == condition_evaluation_auto)
522 {
523 if (target_supports_evaluation_of_breakpoint_conditions ())
524 return condition_evaluation_target;
525 else
526 return condition_evaluation_host;
527 }
528 else
529 return mode;
530 }
531
532 /* Discovers what condition_evaluation_auto translates to. */
533
534 static const char *
535 breakpoint_condition_evaluation_mode (void)
536 {
537 return translate_condition_evaluation_mode (condition_evaluation_mode);
538 }
539
540 /* Return true if GDB should evaluate breakpoint conditions or false
541 otherwise. */
542
543 static int
544 gdb_evaluates_breakpoint_condition_p (void)
545 {
546 const char *mode = breakpoint_condition_evaluation_mode ();
547
548 return (mode == condition_evaluation_host);
549 }
550
551 void _initialize_breakpoint (void);
552
553 /* Are we executing breakpoint commands? */
554 static int executing_breakpoint_commands;
555
556 /* Are overlay event breakpoints enabled? */
557 static int overlay_events_enabled;
558
559 /* See description in breakpoint.h. */
560 int target_exact_watchpoints = 0;
561
562 /* Walk the following statement or block through all breakpoints.
563 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
564 current breakpoint. */
565
566 #define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
567
568 #define ALL_BREAKPOINTS_SAFE(B,TMP) \
569 for (B = breakpoint_chain; \
570 B ? (TMP=B->next, 1): 0; \
571 B = TMP)
572
573 /* Similar iterator for the low-level breakpoints. SAFE variant is
574 not provided so update_global_location_list must not be called
575 while executing the block of ALL_BP_LOCATIONS. */
576
577 #define ALL_BP_LOCATIONS(B,BP_TMP) \
578 for (BP_TMP = bp_location; \
579 BP_TMP < bp_location + bp_location_count && (B = *BP_TMP); \
580 BP_TMP++)
581
582 /* Iterates through locations with address ADDRESS for the currently selected
583 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
584 to where the loop should start from.
585 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
586 appropriate location to start with. */
587
588 #define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
589 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
590 BP_LOCP_TMP = BP_LOCP_START; \
591 BP_LOCP_START \
592 && (BP_LOCP_TMP < bp_location + bp_location_count \
593 && (*BP_LOCP_TMP)->address == ADDRESS); \
594 BP_LOCP_TMP++)
595
596 /* Iterator for tracepoints only. */
597
598 #define ALL_TRACEPOINTS(B) \
599 for (B = breakpoint_chain; B; B = B->next) \
600 if (is_tracepoint (B))
601
602 /* Chains of all breakpoints defined. */
603
604 struct breakpoint *breakpoint_chain;
605
606 /* Array is sorted by bp_location_compare - primarily by the ADDRESS. */
607
608 static struct bp_location **bp_location;
609
610 /* Number of elements of BP_LOCATION. */
611
612 static unsigned bp_location_count;
613
614 /* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
615 ADDRESS for the current elements of BP_LOCATION which get a valid
616 result from bp_location_has_shadow. You can use it for roughly
617 limiting the subrange of BP_LOCATION to scan for shadow bytes for
618 an address you need to read. */
619
620 static CORE_ADDR bp_location_placed_address_before_address_max;
621
622 /* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
623 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
624 BP_LOCATION which get a valid result from bp_location_has_shadow.
625 You can use it for roughly limiting the subrange of BP_LOCATION to
626 scan for shadow bytes for an address you need to read. */
627
628 static CORE_ADDR bp_location_shadow_len_after_address_max;
629
630 /* The locations that no longer correspond to any breakpoint, unlinked
631 from bp_location array, but for which a hit may still be reported
632 by a target. */
633 VEC(bp_location_p) *moribund_locations = NULL;
634
635 /* Number of last breakpoint made. */
636
637 static int breakpoint_count;
638
639 /* The value of `breakpoint_count' before the last command that
640 created breakpoints. If the last (break-like) command created more
641 than one breakpoint, then the difference between BREAKPOINT_COUNT
642 and PREV_BREAKPOINT_COUNT is more than one. */
643 static int prev_breakpoint_count;
644
645 /* Number of last tracepoint made. */
646
647 static int tracepoint_count;
648
649 static struct cmd_list_element *breakpoint_set_cmdlist;
650 static struct cmd_list_element *breakpoint_show_cmdlist;
651 struct cmd_list_element *save_cmdlist;
652
653 /* See declaration at breakpoint.h. */
654
655 struct breakpoint *
656 breakpoint_find_if (int (*func) (struct breakpoint *b, void *d),
657 void *user_data)
658 {
659 struct breakpoint *b = NULL;
660
661 ALL_BREAKPOINTS (b)
662 {
663 if (func (b, user_data) != 0)
664 break;
665 }
666
667 return b;
668 }
669
670 /* Return whether a breakpoint is an active enabled breakpoint. */
671 static int
672 breakpoint_enabled (struct breakpoint *b)
673 {
674 return (b->enable_state == bp_enabled);
675 }
676
677 /* Set breakpoint count to NUM. */
678
679 static void
680 set_breakpoint_count (int num)
681 {
682 prev_breakpoint_count = breakpoint_count;
683 breakpoint_count = num;
684 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
685 }
686
687 /* Used by `start_rbreak_breakpoints' below, to record the current
688 breakpoint count before "rbreak" creates any breakpoint. */
689 static int rbreak_start_breakpoint_count;
690
691 /* Called at the start an "rbreak" command to record the first
692 breakpoint made. */
693
694 void
695 start_rbreak_breakpoints (void)
696 {
697 rbreak_start_breakpoint_count = breakpoint_count;
698 }
699
700 /* Called at the end of an "rbreak" command to record the last
701 breakpoint made. */
702
703 void
704 end_rbreak_breakpoints (void)
705 {
706 prev_breakpoint_count = rbreak_start_breakpoint_count;
707 }
708
709 /* Used in run_command to zero the hit count when a new run starts. */
710
711 void
712 clear_breakpoint_hit_counts (void)
713 {
714 struct breakpoint *b;
715
716 ALL_BREAKPOINTS (b)
717 b->hit_count = 0;
718 }
719
720 /* Allocate a new counted_command_line with reference count of 1.
721 The new structure owns COMMANDS. */
722
723 static struct counted_command_line *
724 alloc_counted_command_line (struct command_line *commands)
725 {
726 struct counted_command_line *result
727 = xmalloc (sizeof (struct counted_command_line));
728
729 result->refc = 1;
730 result->commands = commands;
731 return result;
732 }
733
734 /* Increment reference count. This does nothing if CMD is NULL. */
735
736 static void
737 incref_counted_command_line (struct counted_command_line *cmd)
738 {
739 if (cmd)
740 ++cmd->refc;
741 }
742
743 /* Decrement reference count. If the reference count reaches 0,
744 destroy the counted_command_line. Sets *CMDP to NULL. This does
745 nothing if *CMDP is NULL. */
746
747 static void
748 decref_counted_command_line (struct counted_command_line **cmdp)
749 {
750 if (*cmdp)
751 {
752 if (--(*cmdp)->refc == 0)
753 {
754 free_command_lines (&(*cmdp)->commands);
755 xfree (*cmdp);
756 }
757 *cmdp = NULL;
758 }
759 }
760
761 /* A cleanup function that calls decref_counted_command_line. */
762
763 static void
764 do_cleanup_counted_command_line (void *arg)
765 {
766 decref_counted_command_line (arg);
767 }
768
769 /* Create a cleanup that calls decref_counted_command_line on the
770 argument. */
771
772 static struct cleanup *
773 make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
774 {
775 return make_cleanup (do_cleanup_counted_command_line, cmdp);
776 }
777
778 \f
779 /* Return the breakpoint with the specified number, or NULL
780 if the number does not refer to an existing breakpoint. */
781
782 struct breakpoint *
783 get_breakpoint (int num)
784 {
785 struct breakpoint *b;
786
787 ALL_BREAKPOINTS (b)
788 if (b->number == num)
789 return b;
790
791 return NULL;
792 }
793
794 \f
795
796 /* Mark locations as "conditions have changed" in case the target supports
797 evaluating conditions on its side. */
798
799 static void
800 mark_breakpoint_modified (struct breakpoint *b)
801 {
802 struct bp_location *loc;
803
804 /* This is only meaningful if the target is
805 evaluating conditions and if the user has
806 opted for condition evaluation on the target's
807 side. */
808 if (gdb_evaluates_breakpoint_condition_p ()
809 || !target_supports_evaluation_of_breakpoint_conditions ())
810 return;
811
812 if (!is_breakpoint (b))
813 return;
814
815 for (loc = b->loc; loc; loc = loc->next)
816 loc->condition_changed = condition_modified;
817 }
818
819 /* Mark location as "conditions have changed" in case the target supports
820 evaluating conditions on its side. */
821
822 static void
823 mark_breakpoint_location_modified (struct bp_location *loc)
824 {
825 /* This is only meaningful if the target is
826 evaluating conditions and if the user has
827 opted for condition evaluation on the target's
828 side. */
829 if (gdb_evaluates_breakpoint_condition_p ()
830 || !target_supports_evaluation_of_breakpoint_conditions ())
831
832 return;
833
834 if (!is_breakpoint (loc->owner))
835 return;
836
837 loc->condition_changed = condition_modified;
838 }
839
840 /* Sets the condition-evaluation mode using the static global
841 condition_evaluation_mode. */
842
843 static void
844 set_condition_evaluation_mode (char *args, int from_tty,
845 struct cmd_list_element *c)
846 {
847 const char *old_mode, *new_mode;
848
849 if ((condition_evaluation_mode_1 == condition_evaluation_target)
850 && !target_supports_evaluation_of_breakpoint_conditions ())
851 {
852 condition_evaluation_mode_1 = condition_evaluation_mode;
853 warning (_("Target does not support breakpoint condition evaluation.\n"
854 "Using host evaluation mode instead."));
855 return;
856 }
857
858 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
859 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
860
861 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
862 settings was "auto". */
863 condition_evaluation_mode = condition_evaluation_mode_1;
864
865 /* Only update the mode if the user picked a different one. */
866 if (new_mode != old_mode)
867 {
868 struct bp_location *loc, **loc_tmp;
869 /* If the user switched to a different evaluation mode, we
870 need to synch the changes with the target as follows:
871
872 "host" -> "target": Send all (valid) conditions to the target.
873 "target" -> "host": Remove all the conditions from the target.
874 */
875
876 if (new_mode == condition_evaluation_target)
877 {
878 /* Mark everything modified and synch conditions with the
879 target. */
880 ALL_BP_LOCATIONS (loc, loc_tmp)
881 mark_breakpoint_location_modified (loc);
882 }
883 else
884 {
885 /* Manually mark non-duplicate locations to synch conditions
886 with the target. We do this to remove all the conditions the
887 target knows about. */
888 ALL_BP_LOCATIONS (loc, loc_tmp)
889 if (is_breakpoint (loc->owner) && loc->inserted)
890 loc->needs_update = 1;
891 }
892
893 /* Do the update. */
894 update_global_location_list (UGLL_MAY_INSERT);
895 }
896
897 return;
898 }
899
900 /* Shows the current mode of breakpoint condition evaluation. Explicitly shows
901 what "auto" is translating to. */
902
903 static void
904 show_condition_evaluation_mode (struct ui_file *file, int from_tty,
905 struct cmd_list_element *c, const char *value)
906 {
907 if (condition_evaluation_mode == condition_evaluation_auto)
908 fprintf_filtered (file,
909 _("Breakpoint condition evaluation "
910 "mode is %s (currently %s).\n"),
911 value,
912 breakpoint_condition_evaluation_mode ());
913 else
914 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
915 value);
916 }
917
918 /* A comparison function for bp_location AP and BP that is used by
919 bsearch. This comparison function only cares about addresses, unlike
920 the more general bp_location_compare function. */
921
922 static int
923 bp_location_compare_addrs (const void *ap, const void *bp)
924 {
925 struct bp_location *a = *(void **) ap;
926 struct bp_location *b = *(void **) bp;
927
928 if (a->address == b->address)
929 return 0;
930 else
931 return ((a->address > b->address) - (a->address < b->address));
932 }
933
934 /* Helper function to skip all bp_locations with addresses
935 less than ADDRESS. It returns the first bp_location that
936 is greater than or equal to ADDRESS. If none is found, just
937 return NULL. */
938
939 static struct bp_location **
940 get_first_locp_gte_addr (CORE_ADDR address)
941 {
942 struct bp_location dummy_loc;
943 struct bp_location *dummy_locp = &dummy_loc;
944 struct bp_location **locp_found = NULL;
945
946 /* Initialize the dummy location's address field. */
947 memset (&dummy_loc, 0, sizeof (struct bp_location));
948 dummy_loc.address = address;
949
950 /* Find a close match to the first location at ADDRESS. */
951 locp_found = bsearch (&dummy_locp, bp_location, bp_location_count,
952 sizeof (struct bp_location **),
953 bp_location_compare_addrs);
954
955 /* Nothing was found, nothing left to do. */
956 if (locp_found == NULL)
957 return NULL;
958
959 /* We may have found a location that is at ADDRESS but is not the first in the
960 location's list. Go backwards (if possible) and locate the first one. */
961 while ((locp_found - 1) >= bp_location
962 && (*(locp_found - 1))->address == address)
963 locp_found--;
964
965 return locp_found;
966 }
967
968 void
969 set_breakpoint_condition (struct breakpoint *b, const char *exp,
970 int from_tty)
971 {
972 xfree (b->cond_string);
973 b->cond_string = NULL;
974
975 if (is_watchpoint (b))
976 {
977 struct watchpoint *w = (struct watchpoint *) b;
978
979 xfree (w->cond_exp);
980 w->cond_exp = NULL;
981 }
982 else
983 {
984 struct bp_location *loc;
985
986 for (loc = b->loc; loc; loc = loc->next)
987 {
988 xfree (loc->cond);
989 loc->cond = NULL;
990
991 /* No need to free the condition agent expression
992 bytecode (if we have one). We will handle this
993 when we go through update_global_location_list. */
994 }
995 }
996
997 if (*exp == 0)
998 {
999 if (from_tty)
1000 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
1001 }
1002 else
1003 {
1004 const char *arg = exp;
1005
1006 /* I don't know if it matters whether this is the string the user
1007 typed in or the decompiled expression. */
1008 b->cond_string = xstrdup (arg);
1009 b->condition_not_parsed = 0;
1010
1011 if (is_watchpoint (b))
1012 {
1013 struct watchpoint *w = (struct watchpoint *) b;
1014
1015 innermost_block = NULL;
1016 arg = exp;
1017 w->cond_exp = parse_exp_1 (&arg, 0, 0, 0);
1018 if (*arg)
1019 error (_("Junk at end of expression"));
1020 w->cond_exp_valid_block = innermost_block;
1021 }
1022 else
1023 {
1024 struct bp_location *loc;
1025
1026 for (loc = b->loc; loc; loc = loc->next)
1027 {
1028 arg = exp;
1029 loc->cond =
1030 parse_exp_1 (&arg, loc->address,
1031 block_for_pc (loc->address), 0);
1032 if (*arg)
1033 error (_("Junk at end of expression"));
1034 }
1035 }
1036 }
1037 mark_breakpoint_modified (b);
1038
1039 observer_notify_breakpoint_modified (b);
1040 }
1041
1042 /* Completion for the "condition" command. */
1043
1044 static VEC (char_ptr) *
1045 condition_completer (struct cmd_list_element *cmd,
1046 const char *text, const char *word)
1047 {
1048 const char *space;
1049
1050 text = skip_spaces_const (text);
1051 space = skip_to_space_const (text);
1052 if (*space == '\0')
1053 {
1054 int len;
1055 struct breakpoint *b;
1056 VEC (char_ptr) *result = NULL;
1057
1058 if (text[0] == '$')
1059 {
1060 /* We don't support completion of history indices. */
1061 if (isdigit (text[1]))
1062 return NULL;
1063 return complete_internalvar (&text[1]);
1064 }
1065
1066 /* We're completing the breakpoint number. */
1067 len = strlen (text);
1068
1069 ALL_BREAKPOINTS (b)
1070 {
1071 char number[50];
1072
1073 xsnprintf (number, sizeof (number), "%d", b->number);
1074
1075 if (strncmp (number, text, len) == 0)
1076 VEC_safe_push (char_ptr, result, xstrdup (number));
1077 }
1078
1079 return result;
1080 }
1081
1082 /* We're completing the expression part. */
1083 text = skip_spaces_const (space);
1084 return expression_completer (cmd, text, word);
1085 }
1086
1087 /* condition N EXP -- set break condition of breakpoint N to EXP. */
1088
1089 static void
1090 condition_command (char *arg, int from_tty)
1091 {
1092 struct breakpoint *b;
1093 char *p;
1094 int bnum;
1095
1096 if (arg == 0)
1097 error_no_arg (_("breakpoint number"));
1098
1099 p = arg;
1100 bnum = get_number (&p);
1101 if (bnum == 0)
1102 error (_("Bad breakpoint argument: '%s'"), arg);
1103
1104 ALL_BREAKPOINTS (b)
1105 if (b->number == bnum)
1106 {
1107 /* Check if this breakpoint has a "stop" method implemented in an
1108 extension language. This method and conditions entered into GDB
1109 from the CLI are mutually exclusive. */
1110 const struct extension_language_defn *extlang
1111 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1112
1113 if (extlang != NULL)
1114 {
1115 error (_("Only one stop condition allowed. There is currently"
1116 " a %s stop condition defined for this breakpoint."),
1117 ext_lang_capitalized_name (extlang));
1118 }
1119 set_breakpoint_condition (b, p, from_tty);
1120
1121 if (is_breakpoint (b))
1122 update_global_location_list (UGLL_MAY_INSERT);
1123
1124 return;
1125 }
1126
1127 error (_("No breakpoint number %d."), bnum);
1128 }
1129
1130 /* Check that COMMAND do not contain commands that are suitable
1131 only for tracepoints and not suitable for ordinary breakpoints.
1132 Throw if any such commands is found. */
1133
1134 static void
1135 check_no_tracepoint_commands (struct command_line *commands)
1136 {
1137 struct command_line *c;
1138
1139 for (c = commands; c; c = c->next)
1140 {
1141 int i;
1142
1143 if (c->control_type == while_stepping_control)
1144 error (_("The 'while-stepping' command can "
1145 "only be used for tracepoints"));
1146
1147 for (i = 0; i < c->body_count; ++i)
1148 check_no_tracepoint_commands ((c->body_list)[i]);
1149
1150 /* Not that command parsing removes leading whitespace and comment
1151 lines and also empty lines. So, we only need to check for
1152 command directly. */
1153 if (strstr (c->line, "collect ") == c->line)
1154 error (_("The 'collect' command can only be used for tracepoints"));
1155
1156 if (strstr (c->line, "teval ") == c->line)
1157 error (_("The 'teval' command can only be used for tracepoints"));
1158 }
1159 }
1160
1161 /* Encapsulate tests for different types of tracepoints. */
1162
1163 static int
1164 is_tracepoint_type (enum bptype type)
1165 {
1166 return (type == bp_tracepoint
1167 || type == bp_fast_tracepoint
1168 || type == bp_static_tracepoint);
1169 }
1170
1171 int
1172 is_tracepoint (const struct breakpoint *b)
1173 {
1174 return is_tracepoint_type (b->type);
1175 }
1176
1177 /* A helper function that validates that COMMANDS are valid for a
1178 breakpoint. This function will throw an exception if a problem is
1179 found. */
1180
1181 static void
1182 validate_commands_for_breakpoint (struct breakpoint *b,
1183 struct command_line *commands)
1184 {
1185 if (is_tracepoint (b))
1186 {
1187 struct tracepoint *t = (struct tracepoint *) b;
1188 struct command_line *c;
1189 struct command_line *while_stepping = 0;
1190
1191 /* Reset the while-stepping step count. The previous commands
1192 might have included a while-stepping action, while the new
1193 ones might not. */
1194 t->step_count = 0;
1195
1196 /* We need to verify that each top-level element of commands is
1197 valid for tracepoints, that there's at most one
1198 while-stepping element, and that the while-stepping's body
1199 has valid tracing commands excluding nested while-stepping.
1200 We also need to validate the tracepoint action line in the
1201 context of the tracepoint --- validate_actionline actually
1202 has side effects, like setting the tracepoint's
1203 while-stepping STEP_COUNT, in addition to checking if the
1204 collect/teval actions parse and make sense in the
1205 tracepoint's context. */
1206 for (c = commands; c; c = c->next)
1207 {
1208 if (c->control_type == while_stepping_control)
1209 {
1210 if (b->type == bp_fast_tracepoint)
1211 error (_("The 'while-stepping' command "
1212 "cannot be used for fast tracepoint"));
1213 else if (b->type == bp_static_tracepoint)
1214 error (_("The 'while-stepping' command "
1215 "cannot be used for static tracepoint"));
1216
1217 if (while_stepping)
1218 error (_("The 'while-stepping' command "
1219 "can be used only once"));
1220 else
1221 while_stepping = c;
1222 }
1223
1224 validate_actionline (c->line, b);
1225 }
1226 if (while_stepping)
1227 {
1228 struct command_line *c2;
1229
1230 gdb_assert (while_stepping->body_count == 1);
1231 c2 = while_stepping->body_list[0];
1232 for (; c2; c2 = c2->next)
1233 {
1234 if (c2->control_type == while_stepping_control)
1235 error (_("The 'while-stepping' command cannot be nested"));
1236 }
1237 }
1238 }
1239 else
1240 {
1241 check_no_tracepoint_commands (commands);
1242 }
1243 }
1244
1245 /* Return a vector of all the static tracepoints set at ADDR. The
1246 caller is responsible for releasing the vector. */
1247
1248 VEC(breakpoint_p) *
1249 static_tracepoints_here (CORE_ADDR addr)
1250 {
1251 struct breakpoint *b;
1252 VEC(breakpoint_p) *found = 0;
1253 struct bp_location *loc;
1254
1255 ALL_BREAKPOINTS (b)
1256 if (b->type == bp_static_tracepoint)
1257 {
1258 for (loc = b->loc; loc; loc = loc->next)
1259 if (loc->address == addr)
1260 VEC_safe_push(breakpoint_p, found, b);
1261 }
1262
1263 return found;
1264 }
1265
1266 /* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
1267 validate that only allowed commands are included. */
1268
1269 void
1270 breakpoint_set_commands (struct breakpoint *b,
1271 struct command_line *commands)
1272 {
1273 validate_commands_for_breakpoint (b, commands);
1274
1275 decref_counted_command_line (&b->commands);
1276 b->commands = alloc_counted_command_line (commands);
1277 observer_notify_breakpoint_modified (b);
1278 }
1279
1280 /* Set the internal `silent' flag on the breakpoint. Note that this
1281 is not the same as the "silent" that may appear in the breakpoint's
1282 commands. */
1283
1284 void
1285 breakpoint_set_silent (struct breakpoint *b, int silent)
1286 {
1287 int old_silent = b->silent;
1288
1289 b->silent = silent;
1290 if (old_silent != silent)
1291 observer_notify_breakpoint_modified (b);
1292 }
1293
1294 /* Set the thread for this breakpoint. If THREAD is -1, make the
1295 breakpoint work for any thread. */
1296
1297 void
1298 breakpoint_set_thread (struct breakpoint *b, int thread)
1299 {
1300 int old_thread = b->thread;
1301
1302 b->thread = thread;
1303 if (old_thread != thread)
1304 observer_notify_breakpoint_modified (b);
1305 }
1306
1307 /* Set the task for this breakpoint. If TASK is 0, make the
1308 breakpoint work for any task. */
1309
1310 void
1311 breakpoint_set_task (struct breakpoint *b, int task)
1312 {
1313 int old_task = b->task;
1314
1315 b->task = task;
1316 if (old_task != task)
1317 observer_notify_breakpoint_modified (b);
1318 }
1319
1320 void
1321 check_tracepoint_command (char *line, void *closure)
1322 {
1323 struct breakpoint *b = closure;
1324
1325 validate_actionline (line, b);
1326 }
1327
1328 /* A structure used to pass information through
1329 map_breakpoint_numbers. */
1330
1331 struct commands_info
1332 {
1333 /* True if the command was typed at a tty. */
1334 int from_tty;
1335
1336 /* The breakpoint range spec. */
1337 char *arg;
1338
1339 /* Non-NULL if the body of the commands are being read from this
1340 already-parsed command. */
1341 struct command_line *control;
1342
1343 /* The command lines read from the user, or NULL if they have not
1344 yet been read. */
1345 struct counted_command_line *cmd;
1346 };
1347
1348 /* A callback for map_breakpoint_numbers that sets the commands for
1349 commands_command. */
1350
1351 static void
1352 do_map_commands_command (struct breakpoint *b, void *data)
1353 {
1354 struct commands_info *info = data;
1355
1356 if (info->cmd == NULL)
1357 {
1358 struct command_line *l;
1359
1360 if (info->control != NULL)
1361 l = copy_command_lines (info->control->body_list[0]);
1362 else
1363 {
1364 struct cleanup *old_chain;
1365 char *str;
1366
1367 str = xstrprintf (_("Type commands for breakpoint(s) "
1368 "%s, one per line."),
1369 info->arg);
1370
1371 old_chain = make_cleanup (xfree, str);
1372
1373 l = read_command_lines (str,
1374 info->from_tty, 1,
1375 (is_tracepoint (b)
1376 ? check_tracepoint_command : 0),
1377 b);
1378
1379 do_cleanups (old_chain);
1380 }
1381
1382 info->cmd = alloc_counted_command_line (l);
1383 }
1384
1385 /* If a breakpoint was on the list more than once, we don't need to
1386 do anything. */
1387 if (b->commands != info->cmd)
1388 {
1389 validate_commands_for_breakpoint (b, info->cmd->commands);
1390 incref_counted_command_line (info->cmd);
1391 decref_counted_command_line (&b->commands);
1392 b->commands = info->cmd;
1393 observer_notify_breakpoint_modified (b);
1394 }
1395 }
1396
1397 static void
1398 commands_command_1 (char *arg, int from_tty,
1399 struct command_line *control)
1400 {
1401 struct cleanup *cleanups;
1402 struct commands_info info;
1403
1404 info.from_tty = from_tty;
1405 info.control = control;
1406 info.cmd = NULL;
1407 /* If we read command lines from the user, then `info' will hold an
1408 extra reference to the commands that we must clean up. */
1409 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
1410
1411 if (arg == NULL || !*arg)
1412 {
1413 if (breakpoint_count - prev_breakpoint_count > 1)
1414 arg = xstrprintf ("%d-%d", prev_breakpoint_count + 1,
1415 breakpoint_count);
1416 else if (breakpoint_count > 0)
1417 arg = xstrprintf ("%d", breakpoint_count);
1418 else
1419 {
1420 /* So that we don't try to free the incoming non-NULL
1421 argument in the cleanup below. Mapping breakpoint
1422 numbers will fail in this case. */
1423 arg = NULL;
1424 }
1425 }
1426 else
1427 /* The command loop has some static state, so we need to preserve
1428 our argument. */
1429 arg = xstrdup (arg);
1430
1431 if (arg != NULL)
1432 make_cleanup (xfree, arg);
1433
1434 info.arg = arg;
1435
1436 map_breakpoint_numbers (arg, do_map_commands_command, &info);
1437
1438 if (info.cmd == NULL)
1439 error (_("No breakpoints specified."));
1440
1441 do_cleanups (cleanups);
1442 }
1443
1444 static void
1445 commands_command (char *arg, int from_tty)
1446 {
1447 commands_command_1 (arg, from_tty, NULL);
1448 }
1449
1450 /* Like commands_command, but instead of reading the commands from
1451 input stream, takes them from an already parsed command structure.
1452
1453 This is used by cli-script.c to DTRT with breakpoint commands
1454 that are part of if and while bodies. */
1455 enum command_control_type
1456 commands_from_control_command (char *arg, struct command_line *cmd)
1457 {
1458 commands_command_1 (arg, 0, cmd);
1459 return simple_control;
1460 }
1461
1462 /* Return non-zero if BL->TARGET_INFO contains valid information. */
1463
1464 static int
1465 bp_location_has_shadow (struct bp_location *bl)
1466 {
1467 if (bl->loc_type != bp_loc_software_breakpoint)
1468 return 0;
1469 if (!bl->inserted)
1470 return 0;
1471 if (bl->target_info.shadow_len == 0)
1472 /* BL isn't valid, or doesn't shadow memory. */
1473 return 0;
1474 return 1;
1475 }
1476
1477 /* Update BUF, which is LEN bytes read from the target address
1478 MEMADDR, by replacing a memory breakpoint with its shadowed
1479 contents.
1480
1481 If READBUF is not NULL, this buffer must not overlap with the of
1482 the breakpoint location's shadow_contents buffer. Otherwise, a
1483 failed assertion internal error will be raised. */
1484
1485 static void
1486 one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1487 const gdb_byte *writebuf_org,
1488 ULONGEST memaddr, LONGEST len,
1489 struct bp_target_info *target_info,
1490 struct gdbarch *gdbarch)
1491 {
1492 /* Now do full processing of the found relevant range of elements. */
1493 CORE_ADDR bp_addr = 0;
1494 int bp_size = 0;
1495 int bptoffset = 0;
1496
1497 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1498 current_program_space->aspace, 0))
1499 {
1500 /* The breakpoint is inserted in a different address space. */
1501 return;
1502 }
1503
1504 /* Addresses and length of the part of the breakpoint that
1505 we need to copy. */
1506 bp_addr = target_info->placed_address;
1507 bp_size = target_info->shadow_len;
1508
1509 if (bp_addr + bp_size <= memaddr)
1510 {
1511 /* The breakpoint is entirely before the chunk of memory we are
1512 reading. */
1513 return;
1514 }
1515
1516 if (bp_addr >= memaddr + len)
1517 {
1518 /* The breakpoint is entirely after the chunk of memory we are
1519 reading. */
1520 return;
1521 }
1522
1523 /* Offset within shadow_contents. */
1524 if (bp_addr < memaddr)
1525 {
1526 /* Only copy the second part of the breakpoint. */
1527 bp_size -= memaddr - bp_addr;
1528 bptoffset = memaddr - bp_addr;
1529 bp_addr = memaddr;
1530 }
1531
1532 if (bp_addr + bp_size > memaddr + len)
1533 {
1534 /* Only copy the first part of the breakpoint. */
1535 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1536 }
1537
1538 if (readbuf != NULL)
1539 {
1540 /* Verify that the readbuf buffer does not overlap with the
1541 shadow_contents buffer. */
1542 gdb_assert (target_info->shadow_contents >= readbuf + len
1543 || readbuf >= (target_info->shadow_contents
1544 + target_info->shadow_len));
1545
1546 /* Update the read buffer with this inserted breakpoint's
1547 shadow. */
1548 memcpy (readbuf + bp_addr - memaddr,
1549 target_info->shadow_contents + bptoffset, bp_size);
1550 }
1551 else
1552 {
1553 const unsigned char *bp;
1554 CORE_ADDR addr = target_info->reqstd_address;
1555 int placed_size;
1556
1557 /* Update the shadow with what we want to write to memory. */
1558 memcpy (target_info->shadow_contents + bptoffset,
1559 writebuf_org + bp_addr - memaddr, bp_size);
1560
1561 /* Determine appropriate breakpoint contents and size for this
1562 address. */
1563 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
1564
1565 /* Update the final write buffer with this inserted
1566 breakpoint's INSN. */
1567 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1568 }
1569 }
1570
1571 /* Update BUF, which is LEN bytes read from the target address MEMADDR,
1572 by replacing any memory breakpoints with their shadowed contents.
1573
1574 If READBUF is not NULL, this buffer must not overlap with any of
1575 the breakpoint location's shadow_contents buffers. Otherwise,
1576 a failed assertion internal error will be raised.
1577
1578 The range of shadowed area by each bp_location is:
1579 bl->address - bp_location_placed_address_before_address_max
1580 up to bl->address + bp_location_shadow_len_after_address_max
1581 The range we were requested to resolve shadows for is:
1582 memaddr ... memaddr + len
1583 Thus the safe cutoff boundaries for performance optimization are
1584 memaddr + len <= (bl->address
1585 - bp_location_placed_address_before_address_max)
1586 and:
1587 bl->address + bp_location_shadow_len_after_address_max <= memaddr */
1588
1589 void
1590 breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1591 const gdb_byte *writebuf_org,
1592 ULONGEST memaddr, LONGEST len)
1593 {
1594 /* Left boundary, right boundary and median element of our binary
1595 search. */
1596 unsigned bc_l, bc_r, bc;
1597 size_t i;
1598
1599 /* Find BC_L which is a leftmost element which may affect BUF
1600 content. It is safe to report lower value but a failure to
1601 report higher one. */
1602
1603 bc_l = 0;
1604 bc_r = bp_location_count;
1605 while (bc_l + 1 < bc_r)
1606 {
1607 struct bp_location *bl;
1608
1609 bc = (bc_l + bc_r) / 2;
1610 bl = bp_location[bc];
1611
1612 /* Check first BL->ADDRESS will not overflow due to the added
1613 constant. Then advance the left boundary only if we are sure
1614 the BC element can in no way affect the BUF content (MEMADDR
1615 to MEMADDR + LEN range).
1616
1617 Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1618 offset so that we cannot miss a breakpoint with its shadow
1619 range tail still reaching MEMADDR. */
1620
1621 if ((bl->address + bp_location_shadow_len_after_address_max
1622 >= bl->address)
1623 && (bl->address + bp_location_shadow_len_after_address_max
1624 <= memaddr))
1625 bc_l = bc;
1626 else
1627 bc_r = bc;
1628 }
1629
1630 /* Due to the binary search above, we need to make sure we pick the
1631 first location that's at BC_L's address. E.g., if there are
1632 multiple locations at the same address, BC_L may end up pointing
1633 at a duplicate location, and miss the "master"/"inserted"
1634 location. Say, given locations L1, L2 and L3 at addresses A and
1635 B:
1636
1637 L1@A, L2@A, L3@B, ...
1638
1639 BC_L could end up pointing at location L2, while the "master"
1640 location could be L1. Since the `loc->inserted' flag is only set
1641 on "master" locations, we'd forget to restore the shadow of L1
1642 and L2. */
1643 while (bc_l > 0
1644 && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1645 bc_l--;
1646
1647 /* Now do full processing of the found relevant range of elements. */
1648
1649 for (bc = bc_l; bc < bp_location_count; bc++)
1650 {
1651 struct bp_location *bl = bp_location[bc];
1652 CORE_ADDR bp_addr = 0;
1653 int bp_size = 0;
1654 int bptoffset = 0;
1655
1656 /* bp_location array has BL->OWNER always non-NULL. */
1657 if (bl->owner->type == bp_none)
1658 warning (_("reading through apparently deleted breakpoint #%d?"),
1659 bl->owner->number);
1660
1661 /* Performance optimization: any further element can no longer affect BUF
1662 content. */
1663
1664 if (bl->address >= bp_location_placed_address_before_address_max
1665 && memaddr + len <= (bl->address
1666 - bp_location_placed_address_before_address_max))
1667 break;
1668
1669 if (!bp_location_has_shadow (bl))
1670 continue;
1671
1672 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1673 memaddr, len, &bl->target_info, bl->gdbarch);
1674 }
1675 }
1676
1677 \f
1678
1679 /* Return true if BPT is either a software breakpoint or a hardware
1680 breakpoint. */
1681
1682 int
1683 is_breakpoint (const struct breakpoint *bpt)
1684 {
1685 return (bpt->type == bp_breakpoint
1686 || bpt->type == bp_hardware_breakpoint
1687 || bpt->type == bp_dprintf);
1688 }
1689
1690 /* Return true if BPT is of any hardware watchpoint kind. */
1691
1692 static int
1693 is_hardware_watchpoint (const struct breakpoint *bpt)
1694 {
1695 return (bpt->type == bp_hardware_watchpoint
1696 || bpt->type == bp_read_watchpoint
1697 || bpt->type == bp_access_watchpoint);
1698 }
1699
1700 /* Return true if BPT is of any watchpoint kind, hardware or
1701 software. */
1702
1703 int
1704 is_watchpoint (const struct breakpoint *bpt)
1705 {
1706 return (is_hardware_watchpoint (bpt)
1707 || bpt->type == bp_watchpoint);
1708 }
1709
1710 /* Returns true if the current thread and its running state are safe
1711 to evaluate or update watchpoint B. Watchpoints on local
1712 expressions need to be evaluated in the context of the thread that
1713 was current when the watchpoint was created, and, that thread needs
1714 to be stopped to be able to select the correct frame context.
1715 Watchpoints on global expressions can be evaluated on any thread,
1716 and in any state. It is presently left to the target allowing
1717 memory accesses when threads are running. */
1718
1719 static int
1720 watchpoint_in_thread_scope (struct watchpoint *b)
1721 {
1722 return (b->base.pspace == current_program_space
1723 && (ptid_equal (b->watchpoint_thread, null_ptid)
1724 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1725 && !is_executing (inferior_ptid))));
1726 }
1727
1728 /* Set watchpoint B to disp_del_at_next_stop, even including its possible
1729 associated bp_watchpoint_scope breakpoint. */
1730
1731 static void
1732 watchpoint_del_at_next_stop (struct watchpoint *w)
1733 {
1734 struct breakpoint *b = &w->base;
1735
1736 if (b->related_breakpoint != b)
1737 {
1738 gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1739 gdb_assert (b->related_breakpoint->related_breakpoint == b);
1740 b->related_breakpoint->disposition = disp_del_at_next_stop;
1741 b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1742 b->related_breakpoint = b;
1743 }
1744 b->disposition = disp_del_at_next_stop;
1745 }
1746
1747 /* Extract a bitfield value from value VAL using the bit parameters contained in
1748 watchpoint W. */
1749
1750 static struct value *
1751 extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1752 {
1753 struct value *bit_val;
1754
1755 if (val == NULL)
1756 return NULL;
1757
1758 bit_val = allocate_value (value_type (val));
1759
1760 unpack_value_bitfield (bit_val,
1761 w->val_bitpos,
1762 w->val_bitsize,
1763 value_contents_for_printing (val),
1764 value_offset (val),
1765 val);
1766
1767 return bit_val;
1768 }
1769
1770 /* Assuming that B is a watchpoint:
1771 - Reparse watchpoint expression, if REPARSE is non-zero
1772 - Evaluate expression and store the result in B->val
1773 - Evaluate the condition if there is one, and store the result
1774 in b->loc->cond.
1775 - Update the list of values that must be watched in B->loc.
1776
1777 If the watchpoint disposition is disp_del_at_next_stop, then do
1778 nothing. If this is local watchpoint that is out of scope, delete
1779 it.
1780
1781 Even with `set breakpoint always-inserted on' the watchpoints are
1782 removed + inserted on each stop here. Normal breakpoints must
1783 never be removed because they might be missed by a running thread
1784 when debugging in non-stop mode. On the other hand, hardware
1785 watchpoints (is_hardware_watchpoint; processed here) are specific
1786 to each LWP since they are stored in each LWP's hardware debug
1787 registers. Therefore, such LWP must be stopped first in order to
1788 be able to modify its hardware watchpoints.
1789
1790 Hardware watchpoints must be reset exactly once after being
1791 presented to the user. It cannot be done sooner, because it would
1792 reset the data used to present the watchpoint hit to the user. And
1793 it must not be done later because it could display the same single
1794 watchpoint hit during multiple GDB stops. Note that the latter is
1795 relevant only to the hardware watchpoint types bp_read_watchpoint
1796 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1797 not user-visible - its hit is suppressed if the memory content has
1798 not changed.
1799
1800 The following constraints influence the location where we can reset
1801 hardware watchpoints:
1802
1803 * target_stopped_by_watchpoint and target_stopped_data_address are
1804 called several times when GDB stops.
1805
1806 [linux]
1807 * Multiple hardware watchpoints can be hit at the same time,
1808 causing GDB to stop. GDB only presents one hardware watchpoint
1809 hit at a time as the reason for stopping, and all the other hits
1810 are presented later, one after the other, each time the user
1811 requests the execution to be resumed. Execution is not resumed
1812 for the threads still having pending hit event stored in
1813 LWP_INFO->STATUS. While the watchpoint is already removed from
1814 the inferior on the first stop the thread hit event is kept being
1815 reported from its cached value by linux_nat_stopped_data_address
1816 until the real thread resume happens after the watchpoint gets
1817 presented and thus its LWP_INFO->STATUS gets reset.
1818
1819 Therefore the hardware watchpoint hit can get safely reset on the
1820 watchpoint removal from inferior. */
1821
1822 static void
1823 update_watchpoint (struct watchpoint *b, int reparse)
1824 {
1825 int within_current_scope;
1826 struct frame_id saved_frame_id;
1827 int frame_saved;
1828
1829 /* If this is a local watchpoint, we only want to check if the
1830 watchpoint frame is in scope if the current thread is the thread
1831 that was used to create the watchpoint. */
1832 if (!watchpoint_in_thread_scope (b))
1833 return;
1834
1835 if (b->base.disposition == disp_del_at_next_stop)
1836 return;
1837
1838 frame_saved = 0;
1839
1840 /* Determine if the watchpoint is within scope. */
1841 if (b->exp_valid_block == NULL)
1842 within_current_scope = 1;
1843 else
1844 {
1845 struct frame_info *fi = get_current_frame ();
1846 struct gdbarch *frame_arch = get_frame_arch (fi);
1847 CORE_ADDR frame_pc = get_frame_pc (fi);
1848
1849 /* If we're at a point where the stack has been destroyed
1850 (e.g. in a function epilogue), unwinding may not work
1851 properly. Do not attempt to recreate locations at this
1852 point. See similar comments in watchpoint_check. */
1853 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
1854 return;
1855
1856 /* Save the current frame's ID so we can restore it after
1857 evaluating the watchpoint expression on its own frame. */
1858 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1859 took a frame parameter, so that we didn't have to change the
1860 selected frame. */
1861 frame_saved = 1;
1862 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1863
1864 fi = frame_find_by_id (b->watchpoint_frame);
1865 within_current_scope = (fi != NULL);
1866 if (within_current_scope)
1867 select_frame (fi);
1868 }
1869
1870 /* We don't free locations. They are stored in the bp_location array
1871 and update_global_location_list will eventually delete them and
1872 remove breakpoints if needed. */
1873 b->base.loc = NULL;
1874
1875 if (within_current_scope && reparse)
1876 {
1877 const char *s;
1878
1879 if (b->exp)
1880 {
1881 xfree (b->exp);
1882 b->exp = NULL;
1883 }
1884 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1885 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
1886 /* If the meaning of expression itself changed, the old value is
1887 no longer relevant. We don't want to report a watchpoint hit
1888 to the user when the old value and the new value may actually
1889 be completely different objects. */
1890 value_free (b->val);
1891 b->val = NULL;
1892 b->val_valid = 0;
1893
1894 /* Note that unlike with breakpoints, the watchpoint's condition
1895 expression is stored in the breakpoint object, not in the
1896 locations (re)created below. */
1897 if (b->base.cond_string != NULL)
1898 {
1899 if (b->cond_exp != NULL)
1900 {
1901 xfree (b->cond_exp);
1902 b->cond_exp = NULL;
1903 }
1904
1905 s = b->base.cond_string;
1906 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
1907 }
1908 }
1909
1910 /* If we failed to parse the expression, for example because
1911 it refers to a global variable in a not-yet-loaded shared library,
1912 don't try to insert watchpoint. We don't automatically delete
1913 such watchpoint, though, since failure to parse expression
1914 is different from out-of-scope watchpoint. */
1915 if (!target_has_execution)
1916 {
1917 /* Without execution, memory can't change. No use to try and
1918 set watchpoint locations. The watchpoint will be reset when
1919 the target gains execution, through breakpoint_re_set. */
1920 if (!can_use_hw_watchpoints)
1921 {
1922 if (b->base.ops->works_in_software_mode (&b->base))
1923 b->base.type = bp_watchpoint;
1924 else
1925 error (_("Can't set read/access watchpoint when "
1926 "hardware watchpoints are disabled."));
1927 }
1928 }
1929 else if (within_current_scope && b->exp)
1930 {
1931 int pc = 0;
1932 struct value *val_chain, *v, *result, *next;
1933 struct program_space *frame_pspace;
1934
1935 fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain, 0);
1936
1937 /* Avoid setting b->val if it's already set. The meaning of
1938 b->val is 'the last value' user saw, and we should update
1939 it only if we reported that last value to user. As it
1940 happens, the code that reports it updates b->val directly.
1941 We don't keep track of the memory value for masked
1942 watchpoints. */
1943 if (!b->val_valid && !is_masked_watchpoint (&b->base))
1944 {
1945 if (b->val_bitsize != 0)
1946 {
1947 v = extract_bitfield_from_watchpoint_value (b, v);
1948 if (v != NULL)
1949 release_value (v);
1950 }
1951 b->val = v;
1952 b->val_valid = 1;
1953 }
1954
1955 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1956
1957 /* Look at each value on the value chain. */
1958 for (v = val_chain; v; v = value_next (v))
1959 {
1960 /* If it's a memory location, and GDB actually needed
1961 its contents to evaluate the expression, then we
1962 must watch it. If the first value returned is
1963 still lazy, that means an error occurred reading it;
1964 watch it anyway in case it becomes readable. */
1965 if (VALUE_LVAL (v) == lval_memory
1966 && (v == val_chain || ! value_lazy (v)))
1967 {
1968 struct type *vtype = check_typedef (value_type (v));
1969
1970 /* We only watch structs and arrays if user asked
1971 for it explicitly, never if they just happen to
1972 appear in the middle of some value chain. */
1973 if (v == result
1974 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1975 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1976 {
1977 CORE_ADDR addr;
1978 enum target_hw_bp_type type;
1979 struct bp_location *loc, **tmp;
1980 int bitpos = 0, bitsize = 0;
1981
1982 if (value_bitsize (v) != 0)
1983 {
1984 /* Extract the bit parameters out from the bitfield
1985 sub-expression. */
1986 bitpos = value_bitpos (v);
1987 bitsize = value_bitsize (v);
1988 }
1989 else if (v == result && b->val_bitsize != 0)
1990 {
1991 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
1992 lvalue whose bit parameters are saved in the fields
1993 VAL_BITPOS and VAL_BITSIZE. */
1994 bitpos = b->val_bitpos;
1995 bitsize = b->val_bitsize;
1996 }
1997
1998 addr = value_address (v);
1999 if (bitsize != 0)
2000 {
2001 /* Skip the bytes that don't contain the bitfield. */
2002 addr += bitpos / 8;
2003 }
2004
2005 type = hw_write;
2006 if (b->base.type == bp_read_watchpoint)
2007 type = hw_read;
2008 else if (b->base.type == bp_access_watchpoint)
2009 type = hw_access;
2010
2011 loc = allocate_bp_location (&b->base);
2012 for (tmp = &(b->base.loc); *tmp != NULL; tmp = &((*tmp)->next))
2013 ;
2014 *tmp = loc;
2015 loc->gdbarch = get_type_arch (value_type (v));
2016
2017 loc->pspace = frame_pspace;
2018 loc->address = addr;
2019
2020 if (bitsize != 0)
2021 {
2022 /* Just cover the bytes that make up the bitfield. */
2023 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2024 }
2025 else
2026 loc->length = TYPE_LENGTH (value_type (v));
2027
2028 loc->watchpoint_type = type;
2029 }
2030 }
2031 }
2032
2033 /* Change the type of breakpoint between hardware assisted or
2034 an ordinary watchpoint depending on the hardware support
2035 and free hardware slots. REPARSE is set when the inferior
2036 is started. */
2037 if (reparse)
2038 {
2039 int reg_cnt;
2040 enum bp_loc_type loc_type;
2041 struct bp_location *bl;
2042
2043 reg_cnt = can_use_hardware_watchpoint (val_chain);
2044
2045 if (reg_cnt)
2046 {
2047 int i, target_resources_ok, other_type_used;
2048 enum bptype type;
2049
2050 /* Use an exact watchpoint when there's only one memory region to be
2051 watched, and only one debug register is needed to watch it. */
2052 b->exact = target_exact_watchpoints && reg_cnt == 1;
2053
2054 /* We need to determine how many resources are already
2055 used for all other hardware watchpoints plus this one
2056 to see if we still have enough resources to also fit
2057 this watchpoint in as well. */
2058
2059 /* If this is a software watchpoint, we try to turn it
2060 to a hardware one -- count resources as if B was of
2061 hardware watchpoint type. */
2062 type = b->base.type;
2063 if (type == bp_watchpoint)
2064 type = bp_hardware_watchpoint;
2065
2066 /* This watchpoint may or may not have been placed on
2067 the list yet at this point (it won't be in the list
2068 if we're trying to create it for the first time,
2069 through watch_command), so always account for it
2070 manually. */
2071
2072 /* Count resources used by all watchpoints except B. */
2073 i = hw_watchpoint_used_count_others (&b->base, type, &other_type_used);
2074
2075 /* Add in the resources needed for B. */
2076 i += hw_watchpoint_use_count (&b->base);
2077
2078 target_resources_ok
2079 = target_can_use_hardware_watchpoint (type, i, other_type_used);
2080 if (target_resources_ok <= 0)
2081 {
2082 int sw_mode = b->base.ops->works_in_software_mode (&b->base);
2083
2084 if (target_resources_ok == 0 && !sw_mode)
2085 error (_("Target does not support this type of "
2086 "hardware watchpoint."));
2087 else if (target_resources_ok < 0 && !sw_mode)
2088 error (_("There are not enough available hardware "
2089 "resources for this watchpoint."));
2090
2091 /* Downgrade to software watchpoint. */
2092 b->base.type = bp_watchpoint;
2093 }
2094 else
2095 {
2096 /* If this was a software watchpoint, we've just
2097 found we have enough resources to turn it to a
2098 hardware watchpoint. Otherwise, this is a
2099 nop. */
2100 b->base.type = type;
2101 }
2102 }
2103 else if (!b->base.ops->works_in_software_mode (&b->base))
2104 {
2105 if (!can_use_hw_watchpoints)
2106 error (_("Can't set read/access watchpoint when "
2107 "hardware watchpoints are disabled."));
2108 else
2109 error (_("Expression cannot be implemented with "
2110 "read/access watchpoint."));
2111 }
2112 else
2113 b->base.type = bp_watchpoint;
2114
2115 loc_type = (b->base.type == bp_watchpoint? bp_loc_other
2116 : bp_loc_hardware_watchpoint);
2117 for (bl = b->base.loc; bl; bl = bl->next)
2118 bl->loc_type = loc_type;
2119 }
2120
2121 for (v = val_chain; v; v = next)
2122 {
2123 next = value_next (v);
2124 if (v != b->val)
2125 value_free (v);
2126 }
2127
2128 /* If a software watchpoint is not watching any memory, then the
2129 above left it without any location set up. But,
2130 bpstat_stop_status requires a location to be able to report
2131 stops, so make sure there's at least a dummy one. */
2132 if (b->base.type == bp_watchpoint && b->base.loc == NULL)
2133 {
2134 struct breakpoint *base = &b->base;
2135 base->loc = allocate_bp_location (base);
2136 base->loc->pspace = frame_pspace;
2137 base->loc->address = -1;
2138 base->loc->length = -1;
2139 base->loc->watchpoint_type = -1;
2140 }
2141 }
2142 else if (!within_current_scope)
2143 {
2144 printf_filtered (_("\
2145 Watchpoint %d deleted because the program has left the block\n\
2146 in which its expression is valid.\n"),
2147 b->base.number);
2148 watchpoint_del_at_next_stop (b);
2149 }
2150
2151 /* Restore the selected frame. */
2152 if (frame_saved)
2153 select_frame (frame_find_by_id (saved_frame_id));
2154 }
2155
2156
2157 /* Returns 1 iff breakpoint location should be
2158 inserted in the inferior. We don't differentiate the type of BL's owner
2159 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2160 breakpoint_ops is not defined, because in insert_bp_location,
2161 tracepoint's insert_location will not be called. */
2162 static int
2163 should_be_inserted (struct bp_location *bl)
2164 {
2165 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
2166 return 0;
2167
2168 if (bl->owner->disposition == disp_del_at_next_stop)
2169 return 0;
2170
2171 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
2172 return 0;
2173
2174 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2175 return 0;
2176
2177 /* This is set for example, when we're attached to the parent of a
2178 vfork, and have detached from the child. The child is running
2179 free, and we expect it to do an exec or exit, at which point the
2180 OS makes the parent schedulable again (and the target reports
2181 that the vfork is done). Until the child is done with the shared
2182 memory region, do not insert breakpoints in the parent, otherwise
2183 the child could still trip on the parent's breakpoints. Since
2184 the parent is blocked anyway, it won't miss any breakpoint. */
2185 if (bl->pspace->breakpoints_not_allowed)
2186 return 0;
2187
2188 /* Don't insert a breakpoint if we're trying to step past its
2189 location. */
2190 if ((bl->loc_type == bp_loc_software_breakpoint
2191 || bl->loc_type == bp_loc_hardware_breakpoint)
2192 && stepping_past_instruction_at (bl->pspace->aspace,
2193 bl->address))
2194 {
2195 if (debug_infrun)
2196 {
2197 fprintf_unfiltered (gdb_stdlog,
2198 "infrun: skipping breakpoint: "
2199 "stepping past insn at: %s\n",
2200 paddress (bl->gdbarch, bl->address));
2201 }
2202 return 0;
2203 }
2204
2205 /* Don't insert watchpoints if we're trying to step past the
2206 instruction that triggered one. */
2207 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2208 && stepping_past_nonsteppable_watchpoint ())
2209 {
2210 if (debug_infrun)
2211 {
2212 fprintf_unfiltered (gdb_stdlog,
2213 "infrun: stepping past non-steppable watchpoint. "
2214 "skipping watchpoint at %s:%d\n",
2215 paddress (bl->gdbarch, bl->address),
2216 bl->length);
2217 }
2218 return 0;
2219 }
2220
2221 return 1;
2222 }
2223
2224 /* Same as should_be_inserted but does the check assuming
2225 that the location is not duplicated. */
2226
2227 static int
2228 unduplicated_should_be_inserted (struct bp_location *bl)
2229 {
2230 int result;
2231 const int save_duplicate = bl->duplicate;
2232
2233 bl->duplicate = 0;
2234 result = should_be_inserted (bl);
2235 bl->duplicate = save_duplicate;
2236 return result;
2237 }
2238
2239 /* Parses a conditional described by an expression COND into an
2240 agent expression bytecode suitable for evaluation
2241 by the bytecode interpreter. Return NULL if there was
2242 any error during parsing. */
2243
2244 static struct agent_expr *
2245 parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2246 {
2247 struct agent_expr *aexpr = NULL;
2248
2249 if (!cond)
2250 return NULL;
2251
2252 /* We don't want to stop processing, so catch any errors
2253 that may show up. */
2254 TRY
2255 {
2256 aexpr = gen_eval_for_expr (scope, cond);
2257 }
2258
2259 CATCH (ex, RETURN_MASK_ERROR)
2260 {
2261 /* If we got here, it means the condition could not be parsed to a valid
2262 bytecode expression and thus can't be evaluated on the target's side.
2263 It's no use iterating through the conditions. */
2264 return NULL;
2265 }
2266 END_CATCH
2267
2268 /* We have a valid agent expression. */
2269 return aexpr;
2270 }
2271
2272 /* Based on location BL, create a list of breakpoint conditions to be
2273 passed on to the target. If we have duplicated locations with different
2274 conditions, we will add such conditions to the list. The idea is that the
2275 target will evaluate the list of conditions and will only notify GDB when
2276 one of them is true. */
2277
2278 static void
2279 build_target_condition_list (struct bp_location *bl)
2280 {
2281 struct bp_location **locp = NULL, **loc2p;
2282 int null_condition_or_parse_error = 0;
2283 int modified = bl->needs_update;
2284 struct bp_location *loc;
2285
2286 /* Release conditions left over from a previous insert. */
2287 VEC_free (agent_expr_p, bl->target_info.conditions);
2288
2289 /* This is only meaningful if the target is
2290 evaluating conditions and if the user has
2291 opted for condition evaluation on the target's
2292 side. */
2293 if (gdb_evaluates_breakpoint_condition_p ()
2294 || !target_supports_evaluation_of_breakpoint_conditions ())
2295 return;
2296
2297 /* Do a first pass to check for locations with no assigned
2298 conditions or conditions that fail to parse to a valid agent expression
2299 bytecode. If any of these happen, then it's no use to send conditions
2300 to the target since this location will always trigger and generate a
2301 response back to GDB. */
2302 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2303 {
2304 loc = (*loc2p);
2305 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2306 {
2307 if (modified)
2308 {
2309 struct agent_expr *aexpr;
2310
2311 /* Re-parse the conditions since something changed. In that
2312 case we already freed the condition bytecodes (see
2313 force_breakpoint_reinsertion). We just
2314 need to parse the condition to bytecodes again. */
2315 aexpr = parse_cond_to_aexpr (bl->address, loc->cond);
2316 loc->cond_bytecode = aexpr;
2317
2318 /* Check if we managed to parse the conditional expression
2319 correctly. If not, we will not send this condition
2320 to the target. */
2321 if (aexpr)
2322 continue;
2323 }
2324
2325 /* If we have a NULL bytecode expression, it means something
2326 went wrong or we have a null condition expression. */
2327 if (!loc->cond_bytecode)
2328 {
2329 null_condition_or_parse_error = 1;
2330 break;
2331 }
2332 }
2333 }
2334
2335 /* If any of these happened, it means we will have to evaluate the conditions
2336 for the location's address on gdb's side. It is no use keeping bytecodes
2337 for all the other duplicate locations, thus we free all of them here.
2338
2339 This is so we have a finer control over which locations' conditions are
2340 being evaluated by GDB or the remote stub. */
2341 if (null_condition_or_parse_error)
2342 {
2343 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2344 {
2345 loc = (*loc2p);
2346 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2347 {
2348 /* Only go as far as the first NULL bytecode is
2349 located. */
2350 if (!loc->cond_bytecode)
2351 return;
2352
2353 free_agent_expr (loc->cond_bytecode);
2354 loc->cond_bytecode = NULL;
2355 }
2356 }
2357 }
2358
2359 /* No NULL conditions or failed bytecode generation. Build a condition list
2360 for this location's address. */
2361 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2362 {
2363 loc = (*loc2p);
2364 if (loc->cond
2365 && is_breakpoint (loc->owner)
2366 && loc->pspace->num == bl->pspace->num
2367 && loc->owner->enable_state == bp_enabled
2368 && loc->enabled)
2369 /* Add the condition to the vector. This will be used later to send the
2370 conditions to the target. */
2371 VEC_safe_push (agent_expr_p, bl->target_info.conditions,
2372 loc->cond_bytecode);
2373 }
2374
2375 return;
2376 }
2377
2378 /* Parses a command described by string CMD into an agent expression
2379 bytecode suitable for evaluation by the bytecode interpreter.
2380 Return NULL if there was any error during parsing. */
2381
2382 static struct agent_expr *
2383 parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2384 {
2385 struct cleanup *old_cleanups = 0;
2386 struct expression *expr, **argvec;
2387 struct agent_expr *aexpr = NULL;
2388 const char *cmdrest;
2389 const char *format_start, *format_end;
2390 struct format_piece *fpieces;
2391 int nargs;
2392 struct gdbarch *gdbarch = get_current_arch ();
2393
2394 if (!cmd)
2395 return NULL;
2396
2397 cmdrest = cmd;
2398
2399 if (*cmdrest == ',')
2400 ++cmdrest;
2401 cmdrest = skip_spaces_const (cmdrest);
2402
2403 if (*cmdrest++ != '"')
2404 error (_("No format string following the location"));
2405
2406 format_start = cmdrest;
2407
2408 fpieces = parse_format_string (&cmdrest);
2409
2410 old_cleanups = make_cleanup (free_format_pieces_cleanup, &fpieces);
2411
2412 format_end = cmdrest;
2413
2414 if (*cmdrest++ != '"')
2415 error (_("Bad format string, non-terminated '\"'."));
2416
2417 cmdrest = skip_spaces_const (cmdrest);
2418
2419 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2420 error (_("Invalid argument syntax"));
2421
2422 if (*cmdrest == ',')
2423 cmdrest++;
2424 cmdrest = skip_spaces_const (cmdrest);
2425
2426 /* For each argument, make an expression. */
2427
2428 argvec = (struct expression **) alloca (strlen (cmd)
2429 * sizeof (struct expression *));
2430
2431 nargs = 0;
2432 while (*cmdrest != '\0')
2433 {
2434 const char *cmd1;
2435
2436 cmd1 = cmdrest;
2437 expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2438 argvec[nargs++] = expr;
2439 cmdrest = cmd1;
2440 if (*cmdrest == ',')
2441 ++cmdrest;
2442 }
2443
2444 /* We don't want to stop processing, so catch any errors
2445 that may show up. */
2446 TRY
2447 {
2448 aexpr = gen_printf (scope, gdbarch, 0, 0,
2449 format_start, format_end - format_start,
2450 fpieces, nargs, argvec);
2451 }
2452 CATCH (ex, RETURN_MASK_ERROR)
2453 {
2454 /* If we got here, it means the command could not be parsed to a valid
2455 bytecode expression and thus can't be evaluated on the target's side.
2456 It's no use iterating through the other commands. */
2457 aexpr = NULL;
2458 }
2459 END_CATCH
2460
2461 do_cleanups (old_cleanups);
2462
2463 /* We have a valid agent expression, return it. */
2464 return aexpr;
2465 }
2466
2467 /* Based on location BL, create a list of breakpoint commands to be
2468 passed on to the target. If we have duplicated locations with
2469 different commands, we will add any such to the list. */
2470
2471 static void
2472 build_target_command_list (struct bp_location *bl)
2473 {
2474 struct bp_location **locp = NULL, **loc2p;
2475 int null_command_or_parse_error = 0;
2476 int modified = bl->needs_update;
2477 struct bp_location *loc;
2478
2479 /* Release commands left over from a previous insert. */
2480 VEC_free (agent_expr_p, bl->target_info.tcommands);
2481
2482 if (!target_can_run_breakpoint_commands ())
2483 return;
2484
2485 /* For now, limit to agent-style dprintf breakpoints. */
2486 if (dprintf_style != dprintf_style_agent)
2487 return;
2488
2489 /* For now, if we have any duplicate location that isn't a dprintf,
2490 don't install the target-side commands, as that would make the
2491 breakpoint not be reported to the core, and we'd lose
2492 control. */
2493 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2494 {
2495 loc = (*loc2p);
2496 if (is_breakpoint (loc->owner)
2497 && loc->pspace->num == bl->pspace->num
2498 && loc->owner->type != bp_dprintf)
2499 return;
2500 }
2501
2502 /* Do a first pass to check for locations with no assigned
2503 conditions or conditions that fail to parse to a valid agent expression
2504 bytecode. If any of these happen, then it's no use to send conditions
2505 to the target since this location will always trigger and generate a
2506 response back to GDB. */
2507 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2508 {
2509 loc = (*loc2p);
2510 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2511 {
2512 if (modified)
2513 {
2514 struct agent_expr *aexpr;
2515
2516 /* Re-parse the commands since something changed. In that
2517 case we already freed the command bytecodes (see
2518 force_breakpoint_reinsertion). We just
2519 need to parse the command to bytecodes again. */
2520 aexpr = parse_cmd_to_aexpr (bl->address,
2521 loc->owner->extra_string);
2522 loc->cmd_bytecode = aexpr;
2523
2524 if (!aexpr)
2525 continue;
2526 }
2527
2528 /* If we have a NULL bytecode expression, it means something
2529 went wrong or we have a null command expression. */
2530 if (!loc->cmd_bytecode)
2531 {
2532 null_command_or_parse_error = 1;
2533 break;
2534 }
2535 }
2536 }
2537
2538 /* If anything failed, then we're not doing target-side commands,
2539 and so clean up. */
2540 if (null_command_or_parse_error)
2541 {
2542 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2543 {
2544 loc = (*loc2p);
2545 if (is_breakpoint (loc->owner)
2546 && loc->pspace->num == bl->pspace->num)
2547 {
2548 /* Only go as far as the first NULL bytecode is
2549 located. */
2550 if (loc->cmd_bytecode == NULL)
2551 return;
2552
2553 free_agent_expr (loc->cmd_bytecode);
2554 loc->cmd_bytecode = NULL;
2555 }
2556 }
2557 }
2558
2559 /* No NULL commands or failed bytecode generation. Build a command list
2560 for this location's address. */
2561 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2562 {
2563 loc = (*loc2p);
2564 if (loc->owner->extra_string
2565 && is_breakpoint (loc->owner)
2566 && loc->pspace->num == bl->pspace->num
2567 && loc->owner->enable_state == bp_enabled
2568 && loc->enabled)
2569 /* Add the command to the vector. This will be used later
2570 to send the commands to the target. */
2571 VEC_safe_push (agent_expr_p, bl->target_info.tcommands,
2572 loc->cmd_bytecode);
2573 }
2574
2575 bl->target_info.persist = 0;
2576 /* Maybe flag this location as persistent. */
2577 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2578 bl->target_info.persist = 1;
2579 }
2580
2581 /* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2582 location. Any error messages are printed to TMP_ERROR_STREAM; and
2583 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
2584 Returns 0 for success, 1 if the bp_location type is not supported or
2585 -1 for failure.
2586
2587 NOTE drow/2003-09-09: This routine could be broken down to an
2588 object-style method for each breakpoint or catchpoint type. */
2589 static int
2590 insert_bp_location (struct bp_location *bl,
2591 struct ui_file *tmp_error_stream,
2592 int *disabled_breaks,
2593 int *hw_breakpoint_error,
2594 int *hw_bp_error_explained_already)
2595 {
2596 enum errors bp_err = GDB_NO_ERROR;
2597 const char *bp_err_message = NULL;
2598
2599 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2600 return 0;
2601
2602 /* Note we don't initialize bl->target_info, as that wipes out
2603 the breakpoint location's shadow_contents if the breakpoint
2604 is still inserted at that location. This in turn breaks
2605 target_read_memory which depends on these buffers when
2606 a memory read is requested at the breakpoint location:
2607 Once the target_info has been wiped, we fail to see that
2608 we have a breakpoint inserted at that address and thus
2609 read the breakpoint instead of returning the data saved in
2610 the breakpoint location's shadow contents. */
2611 bl->target_info.reqstd_address = bl->address;
2612 bl->target_info.placed_address_space = bl->pspace->aspace;
2613 bl->target_info.length = bl->length;
2614
2615 /* When working with target-side conditions, we must pass all the conditions
2616 for the same breakpoint address down to the target since GDB will not
2617 insert those locations. With a list of breakpoint conditions, the target
2618 can decide when to stop and notify GDB. */
2619
2620 if (is_breakpoint (bl->owner))
2621 {
2622 build_target_condition_list (bl);
2623 build_target_command_list (bl);
2624 /* Reset the modification marker. */
2625 bl->needs_update = 0;
2626 }
2627
2628 if (bl->loc_type == bp_loc_software_breakpoint
2629 || bl->loc_type == bp_loc_hardware_breakpoint)
2630 {
2631 if (bl->owner->type != bp_hardware_breakpoint)
2632 {
2633 /* If the explicitly specified breakpoint type
2634 is not hardware breakpoint, check the memory map to see
2635 if the breakpoint address is in read only memory or not.
2636
2637 Two important cases are:
2638 - location type is not hardware breakpoint, memory
2639 is readonly. We change the type of the location to
2640 hardware breakpoint.
2641 - location type is hardware breakpoint, memory is
2642 read-write. This means we've previously made the
2643 location hardware one, but then the memory map changed,
2644 so we undo.
2645
2646 When breakpoints are removed, remove_breakpoints will use
2647 location types we've just set here, the only possible
2648 problem is that memory map has changed during running
2649 program, but it's not going to work anyway with current
2650 gdb. */
2651 struct mem_region *mr
2652 = lookup_mem_region (bl->target_info.reqstd_address);
2653
2654 if (mr)
2655 {
2656 if (automatic_hardware_breakpoints)
2657 {
2658 enum bp_loc_type new_type;
2659
2660 if (mr->attrib.mode != MEM_RW)
2661 new_type = bp_loc_hardware_breakpoint;
2662 else
2663 new_type = bp_loc_software_breakpoint;
2664
2665 if (new_type != bl->loc_type)
2666 {
2667 static int said = 0;
2668
2669 bl->loc_type = new_type;
2670 if (!said)
2671 {
2672 fprintf_filtered (gdb_stdout,
2673 _("Note: automatically using "
2674 "hardware breakpoints for "
2675 "read-only addresses.\n"));
2676 said = 1;
2677 }
2678 }
2679 }
2680 else if (bl->loc_type == bp_loc_software_breakpoint
2681 && mr->attrib.mode != MEM_RW)
2682 {
2683 fprintf_unfiltered (tmp_error_stream,
2684 _("Cannot insert breakpoint %d.\n"
2685 "Cannot set software breakpoint "
2686 "at read-only address %s\n"),
2687 bl->owner->number,
2688 paddress (bl->gdbarch, bl->address));
2689 return 1;
2690 }
2691 }
2692 }
2693
2694 /* First check to see if we have to handle an overlay. */
2695 if (overlay_debugging == ovly_off
2696 || bl->section == NULL
2697 || !(section_is_overlay (bl->section)))
2698 {
2699 /* No overlay handling: just set the breakpoint. */
2700 TRY
2701 {
2702 int val;
2703
2704 val = bl->owner->ops->insert_location (bl);
2705 if (val)
2706 bp_err = GENERIC_ERROR;
2707 }
2708 CATCH (e, RETURN_MASK_ALL)
2709 {
2710 bp_err = e.error;
2711 bp_err_message = e.message;
2712 }
2713 END_CATCH
2714 }
2715 else
2716 {
2717 /* This breakpoint is in an overlay section.
2718 Shall we set a breakpoint at the LMA? */
2719 if (!overlay_events_enabled)
2720 {
2721 /* Yes -- overlay event support is not active,
2722 so we must try to set a breakpoint at the LMA.
2723 This will not work for a hardware breakpoint. */
2724 if (bl->loc_type == bp_loc_hardware_breakpoint)
2725 warning (_("hardware breakpoint %d not supported in overlay!"),
2726 bl->owner->number);
2727 else
2728 {
2729 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2730 bl->section);
2731 /* Set a software (trap) breakpoint at the LMA. */
2732 bl->overlay_target_info = bl->target_info;
2733 bl->overlay_target_info.reqstd_address = addr;
2734
2735 /* No overlay handling: just set the breakpoint. */
2736 TRY
2737 {
2738 int val;
2739
2740 val = target_insert_breakpoint (bl->gdbarch,
2741 &bl->overlay_target_info);
2742 if (val)
2743 bp_err = GENERIC_ERROR;
2744 }
2745 CATCH (e, RETURN_MASK_ALL)
2746 {
2747 bp_err = e.error;
2748 bp_err_message = e.message;
2749 }
2750 END_CATCH
2751
2752 if (bp_err != GDB_NO_ERROR)
2753 fprintf_unfiltered (tmp_error_stream,
2754 "Overlay breakpoint %d "
2755 "failed: in ROM?\n",
2756 bl->owner->number);
2757 }
2758 }
2759 /* Shall we set a breakpoint at the VMA? */
2760 if (section_is_mapped (bl->section))
2761 {
2762 /* Yes. This overlay section is mapped into memory. */
2763 TRY
2764 {
2765 int val;
2766
2767 val = bl->owner->ops->insert_location (bl);
2768 if (val)
2769 bp_err = GENERIC_ERROR;
2770 }
2771 CATCH (e, RETURN_MASK_ALL)
2772 {
2773 bp_err = e.error;
2774 bp_err_message = e.message;
2775 }
2776 END_CATCH
2777 }
2778 else
2779 {
2780 /* No. This breakpoint will not be inserted.
2781 No error, but do not mark the bp as 'inserted'. */
2782 return 0;
2783 }
2784 }
2785
2786 if (bp_err != GDB_NO_ERROR)
2787 {
2788 /* Can't set the breakpoint. */
2789
2790 /* In some cases, we might not be able to insert a
2791 breakpoint in a shared library that has already been
2792 removed, but we have not yet processed the shlib unload
2793 event. Unfortunately, some targets that implement
2794 breakpoint insertion themselves can't tell why the
2795 breakpoint insertion failed (e.g., the remote target
2796 doesn't define error codes), so we must treat generic
2797 errors as memory errors. */
2798 if ((bp_err == GENERIC_ERROR || bp_err == MEMORY_ERROR)
2799 && bl->loc_type == bp_loc_software_breakpoint
2800 && (solib_name_from_address (bl->pspace, bl->address)
2801 || shared_objfile_contains_address_p (bl->pspace,
2802 bl->address)))
2803 {
2804 /* See also: disable_breakpoints_in_shlibs. */
2805 bl->shlib_disabled = 1;
2806 observer_notify_breakpoint_modified (bl->owner);
2807 if (!*disabled_breaks)
2808 {
2809 fprintf_unfiltered (tmp_error_stream,
2810 "Cannot insert breakpoint %d.\n",
2811 bl->owner->number);
2812 fprintf_unfiltered (tmp_error_stream,
2813 "Temporarily disabling shared "
2814 "library breakpoints:\n");
2815 }
2816 *disabled_breaks = 1;
2817 fprintf_unfiltered (tmp_error_stream,
2818 "breakpoint #%d\n", bl->owner->number);
2819 return 0;
2820 }
2821 else
2822 {
2823 if (bl->loc_type == bp_loc_hardware_breakpoint)
2824 {
2825 *hw_breakpoint_error = 1;
2826 *hw_bp_error_explained_already = bp_err_message != NULL;
2827 fprintf_unfiltered (tmp_error_stream,
2828 "Cannot insert hardware breakpoint %d%s",
2829 bl->owner->number, bp_err_message ? ":" : ".\n");
2830 if (bp_err_message != NULL)
2831 fprintf_unfiltered (tmp_error_stream, "%s.\n", bp_err_message);
2832 }
2833 else
2834 {
2835 if (bp_err_message == NULL)
2836 {
2837 char *message
2838 = memory_error_message (TARGET_XFER_E_IO,
2839 bl->gdbarch, bl->address);
2840 struct cleanup *old_chain = make_cleanup (xfree, message);
2841
2842 fprintf_unfiltered (tmp_error_stream,
2843 "Cannot insert breakpoint %d.\n"
2844 "%s\n",
2845 bl->owner->number, message);
2846 do_cleanups (old_chain);
2847 }
2848 else
2849 {
2850 fprintf_unfiltered (tmp_error_stream,
2851 "Cannot insert breakpoint %d: %s\n",
2852 bl->owner->number,
2853 bp_err_message);
2854 }
2855 }
2856 return 1;
2857
2858 }
2859 }
2860 else
2861 bl->inserted = 1;
2862
2863 return 0;
2864 }
2865
2866 else if (bl->loc_type == bp_loc_hardware_watchpoint
2867 /* NOTE drow/2003-09-08: This state only exists for removing
2868 watchpoints. It's not clear that it's necessary... */
2869 && bl->owner->disposition != disp_del_at_next_stop)
2870 {
2871 int val;
2872
2873 gdb_assert (bl->owner->ops != NULL
2874 && bl->owner->ops->insert_location != NULL);
2875
2876 val = bl->owner->ops->insert_location (bl);
2877
2878 /* If trying to set a read-watchpoint, and it turns out it's not
2879 supported, try emulating one with an access watchpoint. */
2880 if (val == 1 && bl->watchpoint_type == hw_read)
2881 {
2882 struct bp_location *loc, **loc_temp;
2883
2884 /* But don't try to insert it, if there's already another
2885 hw_access location that would be considered a duplicate
2886 of this one. */
2887 ALL_BP_LOCATIONS (loc, loc_temp)
2888 if (loc != bl
2889 && loc->watchpoint_type == hw_access
2890 && watchpoint_locations_match (bl, loc))
2891 {
2892 bl->duplicate = 1;
2893 bl->inserted = 1;
2894 bl->target_info = loc->target_info;
2895 bl->watchpoint_type = hw_access;
2896 val = 0;
2897 break;
2898 }
2899
2900 if (val == 1)
2901 {
2902 bl->watchpoint_type = hw_access;
2903 val = bl->owner->ops->insert_location (bl);
2904
2905 if (val)
2906 /* Back to the original value. */
2907 bl->watchpoint_type = hw_read;
2908 }
2909 }
2910
2911 bl->inserted = (val == 0);
2912 }
2913
2914 else if (bl->owner->type == bp_catchpoint)
2915 {
2916 int val;
2917
2918 gdb_assert (bl->owner->ops != NULL
2919 && bl->owner->ops->insert_location != NULL);
2920
2921 val = bl->owner->ops->insert_location (bl);
2922 if (val)
2923 {
2924 bl->owner->enable_state = bp_disabled;
2925
2926 if (val == 1)
2927 warning (_("\
2928 Error inserting catchpoint %d: Your system does not support this type\n\
2929 of catchpoint."), bl->owner->number);
2930 else
2931 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2932 }
2933
2934 bl->inserted = (val == 0);
2935
2936 /* We've already printed an error message if there was a problem
2937 inserting this catchpoint, and we've disabled the catchpoint,
2938 so just return success. */
2939 return 0;
2940 }
2941
2942 return 0;
2943 }
2944
2945 /* This function is called when program space PSPACE is about to be
2946 deleted. It takes care of updating breakpoints to not reference
2947 PSPACE anymore. */
2948
2949 void
2950 breakpoint_program_space_exit (struct program_space *pspace)
2951 {
2952 struct breakpoint *b, *b_temp;
2953 struct bp_location *loc, **loc_temp;
2954
2955 /* Remove any breakpoint that was set through this program space. */
2956 ALL_BREAKPOINTS_SAFE (b, b_temp)
2957 {
2958 if (b->pspace == pspace)
2959 delete_breakpoint (b);
2960 }
2961
2962 /* Breakpoints set through other program spaces could have locations
2963 bound to PSPACE as well. Remove those. */
2964 ALL_BP_LOCATIONS (loc, loc_temp)
2965 {
2966 struct bp_location *tmp;
2967
2968 if (loc->pspace == pspace)
2969 {
2970 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
2971 if (loc->owner->loc == loc)
2972 loc->owner->loc = loc->next;
2973 else
2974 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2975 if (tmp->next == loc)
2976 {
2977 tmp->next = loc->next;
2978 break;
2979 }
2980 }
2981 }
2982
2983 /* Now update the global location list to permanently delete the
2984 removed locations above. */
2985 update_global_location_list (UGLL_DONT_INSERT);
2986 }
2987
2988 /* Make sure all breakpoints are inserted in inferior.
2989 Throws exception on any error.
2990 A breakpoint that is already inserted won't be inserted
2991 again, so calling this function twice is safe. */
2992 void
2993 insert_breakpoints (void)
2994 {
2995 struct breakpoint *bpt;
2996
2997 ALL_BREAKPOINTS (bpt)
2998 if (is_hardware_watchpoint (bpt))
2999 {
3000 struct watchpoint *w = (struct watchpoint *) bpt;
3001
3002 update_watchpoint (w, 0 /* don't reparse. */);
3003 }
3004
3005 /* Updating watchpoints creates new locations, so update the global
3006 location list. Explicitly tell ugll to insert locations and
3007 ignore breakpoints_always_inserted_mode. */
3008 update_global_location_list (UGLL_INSERT);
3009 }
3010
3011 /* Invoke CALLBACK for each of bp_location. */
3012
3013 void
3014 iterate_over_bp_locations (walk_bp_location_callback callback)
3015 {
3016 struct bp_location *loc, **loc_tmp;
3017
3018 ALL_BP_LOCATIONS (loc, loc_tmp)
3019 {
3020 callback (loc, NULL);
3021 }
3022 }
3023
3024 /* This is used when we need to synch breakpoint conditions between GDB and the
3025 target. It is the case with deleting and disabling of breakpoints when using
3026 always-inserted mode. */
3027
3028 static void
3029 update_inserted_breakpoint_locations (void)
3030 {
3031 struct bp_location *bl, **blp_tmp;
3032 int error_flag = 0;
3033 int val = 0;
3034 int disabled_breaks = 0;
3035 int hw_breakpoint_error = 0;
3036 int hw_bp_details_reported = 0;
3037
3038 struct ui_file *tmp_error_stream = mem_fileopen ();
3039 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
3040
3041 /* Explicitly mark the warning -- this will only be printed if
3042 there was an error. */
3043 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
3044
3045 save_current_space_and_thread ();
3046
3047 ALL_BP_LOCATIONS (bl, blp_tmp)
3048 {
3049 /* We only want to update software breakpoints and hardware
3050 breakpoints. */
3051 if (!is_breakpoint (bl->owner))
3052 continue;
3053
3054 /* We only want to update locations that are already inserted
3055 and need updating. This is to avoid unwanted insertion during
3056 deletion of breakpoints. */
3057 if (!bl->inserted || (bl->inserted && !bl->needs_update))
3058 continue;
3059
3060 switch_to_program_space_and_thread (bl->pspace);
3061
3062 /* For targets that support global breakpoints, there's no need
3063 to select an inferior to insert breakpoint to. In fact, even
3064 if we aren't attached to any process yet, we should still
3065 insert breakpoints. */
3066 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3067 && ptid_equal (inferior_ptid, null_ptid))
3068 continue;
3069
3070 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
3071 &hw_breakpoint_error, &hw_bp_details_reported);
3072 if (val)
3073 error_flag = val;
3074 }
3075
3076 if (error_flag)
3077 {
3078 target_terminal_ours_for_output ();
3079 error_stream (tmp_error_stream);
3080 }
3081
3082 do_cleanups (cleanups);
3083 }
3084
3085 /* Used when starting or continuing the program. */
3086
3087 static void
3088 insert_breakpoint_locations (void)
3089 {
3090 struct breakpoint *bpt;
3091 struct bp_location *bl, **blp_tmp;
3092 int error_flag = 0;
3093 int val = 0;
3094 int disabled_breaks = 0;
3095 int hw_breakpoint_error = 0;
3096 int hw_bp_error_explained_already = 0;
3097
3098 struct ui_file *tmp_error_stream = mem_fileopen ();
3099 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
3100
3101 /* Explicitly mark the warning -- this will only be printed if
3102 there was an error. */
3103 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
3104
3105 save_current_space_and_thread ();
3106
3107 ALL_BP_LOCATIONS (bl, blp_tmp)
3108 {
3109 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
3110 continue;
3111
3112 /* There is no point inserting thread-specific breakpoints if
3113 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3114 has BL->OWNER always non-NULL. */
3115 if (bl->owner->thread != -1
3116 && !valid_thread_id (bl->owner->thread))
3117 continue;
3118
3119 switch_to_program_space_and_thread (bl->pspace);
3120
3121 /* For targets that support global breakpoints, there's no need
3122 to select an inferior to insert breakpoint to. In fact, even
3123 if we aren't attached to any process yet, we should still
3124 insert breakpoints. */
3125 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3126 && ptid_equal (inferior_ptid, null_ptid))
3127 continue;
3128
3129 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
3130 &hw_breakpoint_error, &hw_bp_error_explained_already);
3131 if (val)
3132 error_flag = val;
3133 }
3134
3135 /* If we failed to insert all locations of a watchpoint, remove
3136 them, as half-inserted watchpoint is of limited use. */
3137 ALL_BREAKPOINTS (bpt)
3138 {
3139 int some_failed = 0;
3140 struct bp_location *loc;
3141
3142 if (!is_hardware_watchpoint (bpt))
3143 continue;
3144
3145 if (!breakpoint_enabled (bpt))
3146 continue;
3147
3148 if (bpt->disposition == disp_del_at_next_stop)
3149 continue;
3150
3151 for (loc = bpt->loc; loc; loc = loc->next)
3152 if (!loc->inserted && should_be_inserted (loc))
3153 {
3154 some_failed = 1;
3155 break;
3156 }
3157 if (some_failed)
3158 {
3159 for (loc = bpt->loc; loc; loc = loc->next)
3160 if (loc->inserted)
3161 remove_breakpoint (loc, mark_uninserted);
3162
3163 hw_breakpoint_error = 1;
3164 fprintf_unfiltered (tmp_error_stream,
3165 "Could not insert hardware watchpoint %d.\n",
3166 bpt->number);
3167 error_flag = -1;
3168 }
3169 }
3170
3171 if (error_flag)
3172 {
3173 /* If a hardware breakpoint or watchpoint was inserted, add a
3174 message about possibly exhausted resources. */
3175 if (hw_breakpoint_error && !hw_bp_error_explained_already)
3176 {
3177 fprintf_unfiltered (tmp_error_stream,
3178 "Could not insert hardware breakpoints:\n\
3179 You may have requested too many hardware breakpoints/watchpoints.\n");
3180 }
3181 target_terminal_ours_for_output ();
3182 error_stream (tmp_error_stream);
3183 }
3184
3185 do_cleanups (cleanups);
3186 }
3187
3188 /* Used when the program stops.
3189 Returns zero if successful, or non-zero if there was a problem
3190 removing a breakpoint location. */
3191
3192 int
3193 remove_breakpoints (void)
3194 {
3195 struct bp_location *bl, **blp_tmp;
3196 int val = 0;
3197
3198 ALL_BP_LOCATIONS (bl, blp_tmp)
3199 {
3200 if (bl->inserted && !is_tracepoint (bl->owner))
3201 val |= remove_breakpoint (bl, mark_uninserted);
3202 }
3203 return val;
3204 }
3205
3206 /* When a thread exits, remove breakpoints that are related to
3207 that thread. */
3208
3209 static void
3210 remove_threaded_breakpoints (struct thread_info *tp, int silent)
3211 {
3212 struct breakpoint *b, *b_tmp;
3213
3214 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3215 {
3216 if (b->thread == tp->num && user_breakpoint_p (b))
3217 {
3218 b->disposition = disp_del_at_next_stop;
3219
3220 printf_filtered (_("\
3221 Thread-specific breakpoint %d deleted - thread %d no longer in the thread list.\n"),
3222 b->number, tp->num);
3223
3224 /* Hide it from the user. */
3225 b->number = 0;
3226 }
3227 }
3228 }
3229
3230 /* Remove breakpoints of process PID. */
3231
3232 int
3233 remove_breakpoints_pid (int pid)
3234 {
3235 struct bp_location *bl, **blp_tmp;
3236 int val;
3237 struct inferior *inf = find_inferior_pid (pid);
3238
3239 ALL_BP_LOCATIONS (bl, blp_tmp)
3240 {
3241 if (bl->pspace != inf->pspace)
3242 continue;
3243
3244 if (bl->inserted && !bl->target_info.persist)
3245 {
3246 val = remove_breakpoint (bl, mark_uninserted);
3247 if (val != 0)
3248 return val;
3249 }
3250 }
3251 return 0;
3252 }
3253
3254 int
3255 reattach_breakpoints (int pid)
3256 {
3257 struct cleanup *old_chain;
3258 struct bp_location *bl, **blp_tmp;
3259 int val;
3260 struct ui_file *tmp_error_stream;
3261 int dummy1 = 0, dummy2 = 0, dummy3 = 0;
3262 struct inferior *inf;
3263 struct thread_info *tp;
3264
3265 tp = any_live_thread_of_process (pid);
3266 if (tp == NULL)
3267 return 1;
3268
3269 inf = find_inferior_pid (pid);
3270 old_chain = save_inferior_ptid ();
3271
3272 inferior_ptid = tp->ptid;
3273
3274 tmp_error_stream = mem_fileopen ();
3275 make_cleanup_ui_file_delete (tmp_error_stream);
3276
3277 ALL_BP_LOCATIONS (bl, blp_tmp)
3278 {
3279 if (bl->pspace != inf->pspace)
3280 continue;
3281
3282 if (bl->inserted)
3283 {
3284 bl->inserted = 0;
3285 val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2, &dummy3);
3286 if (val != 0)
3287 {
3288 do_cleanups (old_chain);
3289 return val;
3290 }
3291 }
3292 }
3293 do_cleanups (old_chain);
3294 return 0;
3295 }
3296
3297 static int internal_breakpoint_number = -1;
3298
3299 /* Set the breakpoint number of B, depending on the value of INTERNAL.
3300 If INTERNAL is non-zero, the breakpoint number will be populated
3301 from internal_breakpoint_number and that variable decremented.
3302 Otherwise the breakpoint number will be populated from
3303 breakpoint_count and that value incremented. Internal breakpoints
3304 do not set the internal var bpnum. */
3305 static void
3306 set_breakpoint_number (int internal, struct breakpoint *b)
3307 {
3308 if (internal)
3309 b->number = internal_breakpoint_number--;
3310 else
3311 {
3312 set_breakpoint_count (breakpoint_count + 1);
3313 b->number = breakpoint_count;
3314 }
3315 }
3316
3317 static struct breakpoint *
3318 create_internal_breakpoint (struct gdbarch *gdbarch,
3319 CORE_ADDR address, enum bptype type,
3320 const struct breakpoint_ops *ops)
3321 {
3322 struct symtab_and_line sal;
3323 struct breakpoint *b;
3324
3325 init_sal (&sal); /* Initialize to zeroes. */
3326
3327 sal.pc = address;
3328 sal.section = find_pc_overlay (sal.pc);
3329 sal.pspace = current_program_space;
3330
3331 b = set_raw_breakpoint (gdbarch, sal, type, ops);
3332 b->number = internal_breakpoint_number--;
3333 b->disposition = disp_donttouch;
3334
3335 return b;
3336 }
3337
3338 static const char *const longjmp_names[] =
3339 {
3340 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3341 };
3342 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3343
3344 /* Per-objfile data private to breakpoint.c. */
3345 struct breakpoint_objfile_data
3346 {
3347 /* Minimal symbol for "_ovly_debug_event" (if any). */
3348 struct bound_minimal_symbol overlay_msym;
3349
3350 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3351 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
3352
3353 /* True if we have looked for longjmp probes. */
3354 int longjmp_searched;
3355
3356 /* SystemTap probe points for longjmp (if any). */
3357 VEC (probe_p) *longjmp_probes;
3358
3359 /* Minimal symbol for "std::terminate()" (if any). */
3360 struct bound_minimal_symbol terminate_msym;
3361
3362 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3363 struct bound_minimal_symbol exception_msym;
3364
3365 /* True if we have looked for exception probes. */
3366 int exception_searched;
3367
3368 /* SystemTap probe points for unwinding (if any). */
3369 VEC (probe_p) *exception_probes;
3370 };
3371
3372 static const struct objfile_data *breakpoint_objfile_key;
3373
3374 /* Minimal symbol not found sentinel. */
3375 static struct minimal_symbol msym_not_found;
3376
3377 /* Returns TRUE if MSYM point to the "not found" sentinel. */
3378
3379 static int
3380 msym_not_found_p (const struct minimal_symbol *msym)
3381 {
3382 return msym == &msym_not_found;
3383 }
3384
3385 /* Return per-objfile data needed by breakpoint.c.
3386 Allocate the data if necessary. */
3387
3388 static struct breakpoint_objfile_data *
3389 get_breakpoint_objfile_data (struct objfile *objfile)
3390 {
3391 struct breakpoint_objfile_data *bp_objfile_data;
3392
3393 bp_objfile_data = objfile_data (objfile, breakpoint_objfile_key);
3394 if (bp_objfile_data == NULL)
3395 {
3396 bp_objfile_data = obstack_alloc (&objfile->objfile_obstack,
3397 sizeof (*bp_objfile_data));
3398
3399 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
3400 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3401 }
3402 return bp_objfile_data;
3403 }
3404
3405 static void
3406 free_breakpoint_probes (struct objfile *obj, void *data)
3407 {
3408 struct breakpoint_objfile_data *bp_objfile_data = data;
3409
3410 VEC_free (probe_p, bp_objfile_data->longjmp_probes);
3411 VEC_free (probe_p, bp_objfile_data->exception_probes);
3412 }
3413
3414 static void
3415 create_overlay_event_breakpoint (void)
3416 {
3417 struct objfile *objfile;
3418 const char *const func_name = "_ovly_debug_event";
3419
3420 ALL_OBJFILES (objfile)
3421 {
3422 struct breakpoint *b;
3423 struct breakpoint_objfile_data *bp_objfile_data;
3424 CORE_ADDR addr;
3425
3426 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3427
3428 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
3429 continue;
3430
3431 if (bp_objfile_data->overlay_msym.minsym == NULL)
3432 {
3433 struct bound_minimal_symbol m;
3434
3435 m = lookup_minimal_symbol_text (func_name, objfile);
3436 if (m.minsym == NULL)
3437 {
3438 /* Avoid future lookups in this objfile. */
3439 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
3440 continue;
3441 }
3442 bp_objfile_data->overlay_msym = m;
3443 }
3444
3445 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
3446 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3447 bp_overlay_event,
3448 &internal_breakpoint_ops);
3449 b->addr_string = xstrdup (func_name);
3450
3451 if (overlay_debugging == ovly_auto)
3452 {
3453 b->enable_state = bp_enabled;
3454 overlay_events_enabled = 1;
3455 }
3456 else
3457 {
3458 b->enable_state = bp_disabled;
3459 overlay_events_enabled = 0;
3460 }
3461 }
3462 update_global_location_list (UGLL_MAY_INSERT);
3463 }
3464
3465 static void
3466 create_longjmp_master_breakpoint (void)
3467 {
3468 struct program_space *pspace;
3469 struct cleanup *old_chain;
3470
3471 old_chain = save_current_program_space ();
3472
3473 ALL_PSPACES (pspace)
3474 {
3475 struct objfile *objfile;
3476
3477 set_current_program_space (pspace);
3478
3479 ALL_OBJFILES (objfile)
3480 {
3481 int i;
3482 struct gdbarch *gdbarch;
3483 struct breakpoint_objfile_data *bp_objfile_data;
3484
3485 gdbarch = get_objfile_arch (objfile);
3486
3487 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3488
3489 if (!bp_objfile_data->longjmp_searched)
3490 {
3491 VEC (probe_p) *ret;
3492
3493 ret = find_probes_in_objfile (objfile, "libc", "longjmp");
3494 if (ret != NULL)
3495 {
3496 /* We are only interested in checking one element. */
3497 struct probe *p = VEC_index (probe_p, ret, 0);
3498
3499 if (!can_evaluate_probe_arguments (p))
3500 {
3501 /* We cannot use the probe interface here, because it does
3502 not know how to evaluate arguments. */
3503 VEC_free (probe_p, ret);
3504 ret = NULL;
3505 }
3506 }
3507 bp_objfile_data->longjmp_probes = ret;
3508 bp_objfile_data->longjmp_searched = 1;
3509 }
3510
3511 if (bp_objfile_data->longjmp_probes != NULL)
3512 {
3513 int i;
3514 struct probe *probe;
3515 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3516
3517 for (i = 0;
3518 VEC_iterate (probe_p,
3519 bp_objfile_data->longjmp_probes,
3520 i, probe);
3521 ++i)
3522 {
3523 struct breakpoint *b;
3524
3525 b = create_internal_breakpoint (gdbarch,
3526 get_probe_address (probe,
3527 objfile),
3528 bp_longjmp_master,
3529 &internal_breakpoint_ops);
3530 b->addr_string = xstrdup ("-probe-stap libc:longjmp");
3531 b->enable_state = bp_disabled;
3532 }
3533
3534 continue;
3535 }
3536
3537 if (!gdbarch_get_longjmp_target_p (gdbarch))
3538 continue;
3539
3540 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
3541 {
3542 struct breakpoint *b;
3543 const char *func_name;
3544 CORE_ADDR addr;
3545
3546 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3547 continue;
3548
3549 func_name = longjmp_names[i];
3550 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3551 {
3552 struct bound_minimal_symbol m;
3553
3554 m = lookup_minimal_symbol_text (func_name, objfile);
3555 if (m.minsym == NULL)
3556 {
3557 /* Prevent future lookups in this objfile. */
3558 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
3559 continue;
3560 }
3561 bp_objfile_data->longjmp_msym[i] = m;
3562 }
3563
3564 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
3565 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3566 &internal_breakpoint_ops);
3567 b->addr_string = xstrdup (func_name);
3568 b->enable_state = bp_disabled;
3569 }
3570 }
3571 }
3572 update_global_location_list (UGLL_MAY_INSERT);
3573
3574 do_cleanups (old_chain);
3575 }
3576
3577 /* Create a master std::terminate breakpoint. */
3578 static void
3579 create_std_terminate_master_breakpoint (void)
3580 {
3581 struct program_space *pspace;
3582 struct cleanup *old_chain;
3583 const char *const func_name = "std::terminate()";
3584
3585 old_chain = save_current_program_space ();
3586
3587 ALL_PSPACES (pspace)
3588 {
3589 struct objfile *objfile;
3590 CORE_ADDR addr;
3591
3592 set_current_program_space (pspace);
3593
3594 ALL_OBJFILES (objfile)
3595 {
3596 struct breakpoint *b;
3597 struct breakpoint_objfile_data *bp_objfile_data;
3598
3599 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3600
3601 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3602 continue;
3603
3604 if (bp_objfile_data->terminate_msym.minsym == NULL)
3605 {
3606 struct bound_minimal_symbol m;
3607
3608 m = lookup_minimal_symbol (func_name, NULL, objfile);
3609 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3610 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
3611 {
3612 /* Prevent future lookups in this objfile. */
3613 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3614 continue;
3615 }
3616 bp_objfile_data->terminate_msym = m;
3617 }
3618
3619 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3620 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3621 bp_std_terminate_master,
3622 &internal_breakpoint_ops);
3623 b->addr_string = xstrdup (func_name);
3624 b->enable_state = bp_disabled;
3625 }
3626 }
3627
3628 update_global_location_list (UGLL_MAY_INSERT);
3629
3630 do_cleanups (old_chain);
3631 }
3632
3633 /* Install a master breakpoint on the unwinder's debug hook. */
3634
3635 static void
3636 create_exception_master_breakpoint (void)
3637 {
3638 struct objfile *objfile;
3639 const char *const func_name = "_Unwind_DebugHook";
3640
3641 ALL_OBJFILES (objfile)
3642 {
3643 struct breakpoint *b;
3644 struct gdbarch *gdbarch;
3645 struct breakpoint_objfile_data *bp_objfile_data;
3646 CORE_ADDR addr;
3647
3648 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3649
3650 /* We prefer the SystemTap probe point if it exists. */
3651 if (!bp_objfile_data->exception_searched)
3652 {
3653 VEC (probe_p) *ret;
3654
3655 ret = find_probes_in_objfile (objfile, "libgcc", "unwind");
3656
3657 if (ret != NULL)
3658 {
3659 /* We are only interested in checking one element. */
3660 struct probe *p = VEC_index (probe_p, ret, 0);
3661
3662 if (!can_evaluate_probe_arguments (p))
3663 {
3664 /* We cannot use the probe interface here, because it does
3665 not know how to evaluate arguments. */
3666 VEC_free (probe_p, ret);
3667 ret = NULL;
3668 }
3669 }
3670 bp_objfile_data->exception_probes = ret;
3671 bp_objfile_data->exception_searched = 1;
3672 }
3673
3674 if (bp_objfile_data->exception_probes != NULL)
3675 {
3676 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3677 int i;
3678 struct probe *probe;
3679
3680 for (i = 0;
3681 VEC_iterate (probe_p,
3682 bp_objfile_data->exception_probes,
3683 i, probe);
3684 ++i)
3685 {
3686 struct breakpoint *b;
3687
3688 b = create_internal_breakpoint (gdbarch,
3689 get_probe_address (probe,
3690 objfile),
3691 bp_exception_master,
3692 &internal_breakpoint_ops);
3693 b->addr_string = xstrdup ("-probe-stap libgcc:unwind");
3694 b->enable_state = bp_disabled;
3695 }
3696
3697 continue;
3698 }
3699
3700 /* Otherwise, try the hook function. */
3701
3702 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3703 continue;
3704
3705 gdbarch = get_objfile_arch (objfile);
3706
3707 if (bp_objfile_data->exception_msym.minsym == NULL)
3708 {
3709 struct bound_minimal_symbol debug_hook;
3710
3711 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3712 if (debug_hook.minsym == NULL)
3713 {
3714 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3715 continue;
3716 }
3717
3718 bp_objfile_data->exception_msym = debug_hook;
3719 }
3720
3721 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
3722 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3723 &current_target);
3724 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3725 &internal_breakpoint_ops);
3726 b->addr_string = xstrdup (func_name);
3727 b->enable_state = bp_disabled;
3728 }
3729
3730 update_global_location_list (UGLL_MAY_INSERT);
3731 }
3732
3733 void
3734 update_breakpoints_after_exec (void)
3735 {
3736 struct breakpoint *b, *b_tmp;
3737 struct bp_location *bploc, **bplocp_tmp;
3738
3739 /* We're about to delete breakpoints from GDB's lists. If the
3740 INSERTED flag is true, GDB will try to lift the breakpoints by
3741 writing the breakpoints' "shadow contents" back into memory. The
3742 "shadow contents" are NOT valid after an exec, so GDB should not
3743 do that. Instead, the target is responsible from marking
3744 breakpoints out as soon as it detects an exec. We don't do that
3745 here instead, because there may be other attempts to delete
3746 breakpoints after detecting an exec and before reaching here. */
3747 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
3748 if (bploc->pspace == current_program_space)
3749 gdb_assert (!bploc->inserted);
3750
3751 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3752 {
3753 if (b->pspace != current_program_space)
3754 continue;
3755
3756 /* Solib breakpoints must be explicitly reset after an exec(). */
3757 if (b->type == bp_shlib_event)
3758 {
3759 delete_breakpoint (b);
3760 continue;
3761 }
3762
3763 /* JIT breakpoints must be explicitly reset after an exec(). */
3764 if (b->type == bp_jit_event)
3765 {
3766 delete_breakpoint (b);
3767 continue;
3768 }
3769
3770 /* Thread event breakpoints must be set anew after an exec(),
3771 as must overlay event and longjmp master breakpoints. */
3772 if (b->type == bp_thread_event || b->type == bp_overlay_event
3773 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3774 || b->type == bp_exception_master)
3775 {
3776 delete_breakpoint (b);
3777 continue;
3778 }
3779
3780 /* Step-resume breakpoints are meaningless after an exec(). */
3781 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3782 {
3783 delete_breakpoint (b);
3784 continue;
3785 }
3786
3787 /* Just like single-step breakpoints. */
3788 if (b->type == bp_single_step)
3789 {
3790 delete_breakpoint (b);
3791 continue;
3792 }
3793
3794 /* Longjmp and longjmp-resume breakpoints are also meaningless
3795 after an exec. */
3796 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3797 || b->type == bp_longjmp_call_dummy
3798 || b->type == bp_exception || b->type == bp_exception_resume)
3799 {
3800 delete_breakpoint (b);
3801 continue;
3802 }
3803
3804 if (b->type == bp_catchpoint)
3805 {
3806 /* For now, none of the bp_catchpoint breakpoints need to
3807 do anything at this point. In the future, if some of
3808 the catchpoints need to something, we will need to add
3809 a new method, and call this method from here. */
3810 continue;
3811 }
3812
3813 /* bp_finish is a special case. The only way we ought to be able
3814 to see one of these when an exec() has happened, is if the user
3815 caught a vfork, and then said "finish". Ordinarily a finish just
3816 carries them to the call-site of the current callee, by setting
3817 a temporary bp there and resuming. But in this case, the finish
3818 will carry them entirely through the vfork & exec.
3819
3820 We don't want to allow a bp_finish to remain inserted now. But
3821 we can't safely delete it, 'cause finish_command has a handle to
3822 the bp on a bpstat, and will later want to delete it. There's a
3823 chance (and I've seen it happen) that if we delete the bp_finish
3824 here, that its storage will get reused by the time finish_command
3825 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3826 We really must allow finish_command to delete a bp_finish.
3827
3828 In the absence of a general solution for the "how do we know
3829 it's safe to delete something others may have handles to?"
3830 problem, what we'll do here is just uninsert the bp_finish, and
3831 let finish_command delete it.
3832
3833 (We know the bp_finish is "doomed" in the sense that it's
3834 momentary, and will be deleted as soon as finish_command sees
3835 the inferior stopped. So it doesn't matter that the bp's
3836 address is probably bogus in the new a.out, unlike e.g., the
3837 solib breakpoints.) */
3838
3839 if (b->type == bp_finish)
3840 {
3841 continue;
3842 }
3843
3844 /* Without a symbolic address, we have little hope of the
3845 pre-exec() address meaning the same thing in the post-exec()
3846 a.out. */
3847 if (b->addr_string == NULL)
3848 {
3849 delete_breakpoint (b);
3850 continue;
3851 }
3852 }
3853 }
3854
3855 int
3856 detach_breakpoints (ptid_t ptid)
3857 {
3858 struct bp_location *bl, **blp_tmp;
3859 int val = 0;
3860 struct cleanup *old_chain = save_inferior_ptid ();
3861 struct inferior *inf = current_inferior ();
3862
3863 if (ptid_get_pid (ptid) == ptid_get_pid (inferior_ptid))
3864 error (_("Cannot detach breakpoints of inferior_ptid"));
3865
3866 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
3867 inferior_ptid = ptid;
3868 ALL_BP_LOCATIONS (bl, blp_tmp)
3869 {
3870 if (bl->pspace != inf->pspace)
3871 continue;
3872
3873 /* This function must physically remove breakpoints locations
3874 from the specified ptid, without modifying the breakpoint
3875 package's state. Locations of type bp_loc_other are only
3876 maintained at GDB side. So, there is no need to remove
3877 these bp_loc_other locations. Moreover, removing these
3878 would modify the breakpoint package's state. */
3879 if (bl->loc_type == bp_loc_other)
3880 continue;
3881
3882 if (bl->inserted)
3883 val |= remove_breakpoint_1 (bl, mark_inserted);
3884 }
3885
3886 do_cleanups (old_chain);
3887 return val;
3888 }
3889
3890 /* Remove the breakpoint location BL from the current address space.
3891 Note that this is used to detach breakpoints from a child fork.
3892 When we get here, the child isn't in the inferior list, and neither
3893 do we have objects to represent its address space --- we should
3894 *not* look at bl->pspace->aspace here. */
3895
3896 static int
3897 remove_breakpoint_1 (struct bp_location *bl, insertion_state_t is)
3898 {
3899 int val;
3900
3901 /* BL is never in moribund_locations by our callers. */
3902 gdb_assert (bl->owner != NULL);
3903
3904 /* The type of none suggests that owner is actually deleted.
3905 This should not ever happen. */
3906 gdb_assert (bl->owner->type != bp_none);
3907
3908 if (bl->loc_type == bp_loc_software_breakpoint
3909 || bl->loc_type == bp_loc_hardware_breakpoint)
3910 {
3911 /* "Normal" instruction breakpoint: either the standard
3912 trap-instruction bp (bp_breakpoint), or a
3913 bp_hardware_breakpoint. */
3914
3915 /* First check to see if we have to handle an overlay. */
3916 if (overlay_debugging == ovly_off
3917 || bl->section == NULL
3918 || !(section_is_overlay (bl->section)))
3919 {
3920 /* No overlay handling: just remove the breakpoint. */
3921
3922 /* If we're trying to uninsert a memory breakpoint that we
3923 know is set in a dynamic object that is marked
3924 shlib_disabled, then either the dynamic object was
3925 removed with "remove-symbol-file" or with
3926 "nosharedlibrary". In the former case, we don't know
3927 whether another dynamic object might have loaded over the
3928 breakpoint's address -- the user might well let us know
3929 about it next with add-symbol-file (the whole point of
3930 add-symbol-file is letting the user manually maintain a
3931 list of dynamically loaded objects). If we have the
3932 breakpoint's shadow memory, that is, this is a software
3933 breakpoint managed by GDB, check whether the breakpoint
3934 is still inserted in memory, to avoid overwriting wrong
3935 code with stale saved shadow contents. Note that HW
3936 breakpoints don't have shadow memory, as they're
3937 implemented using a mechanism that is not dependent on
3938 being able to modify the target's memory, and as such
3939 they should always be removed. */
3940 if (bl->shlib_disabled
3941 && bl->target_info.shadow_len != 0
3942 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3943 val = 0;
3944 else
3945 val = bl->owner->ops->remove_location (bl);
3946 }
3947 else
3948 {
3949 /* This breakpoint is in an overlay section.
3950 Did we set a breakpoint at the LMA? */
3951 if (!overlay_events_enabled)
3952 {
3953 /* Yes -- overlay event support is not active, so we
3954 should have set a breakpoint at the LMA. Remove it.
3955 */
3956 /* Ignore any failures: if the LMA is in ROM, we will
3957 have already warned when we failed to insert it. */
3958 if (bl->loc_type == bp_loc_hardware_breakpoint)
3959 target_remove_hw_breakpoint (bl->gdbarch,
3960 &bl->overlay_target_info);
3961 else
3962 target_remove_breakpoint (bl->gdbarch,
3963 &bl->overlay_target_info);
3964 }
3965 /* Did we set a breakpoint at the VMA?
3966 If so, we will have marked the breakpoint 'inserted'. */
3967 if (bl->inserted)
3968 {
3969 /* Yes -- remove it. Previously we did not bother to
3970 remove the breakpoint if the section had been
3971 unmapped, but let's not rely on that being safe. We
3972 don't know what the overlay manager might do. */
3973
3974 /* However, we should remove *software* breakpoints only
3975 if the section is still mapped, or else we overwrite
3976 wrong code with the saved shadow contents. */
3977 if (bl->loc_type == bp_loc_hardware_breakpoint
3978 || section_is_mapped (bl->section))
3979 val = bl->owner->ops->remove_location (bl);
3980 else
3981 val = 0;
3982 }
3983 else
3984 {
3985 /* No -- not inserted, so no need to remove. No error. */
3986 val = 0;
3987 }
3988 }
3989
3990 /* In some cases, we might not be able to remove a breakpoint in
3991 a shared library that has already been removed, but we have
3992 not yet processed the shlib unload event. Similarly for an
3993 unloaded add-symbol-file object - the user might not yet have
3994 had the chance to remove-symbol-file it. shlib_disabled will
3995 be set if the library/object has already been removed, but
3996 the breakpoint hasn't been uninserted yet, e.g., after
3997 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3998 always-inserted mode. */
3999 if (val
4000 && (bl->loc_type == bp_loc_software_breakpoint
4001 && (bl->shlib_disabled
4002 || solib_name_from_address (bl->pspace, bl->address)
4003 || shared_objfile_contains_address_p (bl->pspace,
4004 bl->address))))
4005 val = 0;
4006
4007 if (val)
4008 return val;
4009 bl->inserted = (is == mark_inserted);
4010 }
4011 else if (bl->loc_type == bp_loc_hardware_watchpoint)
4012 {
4013 gdb_assert (bl->owner->ops != NULL
4014 && bl->owner->ops->remove_location != NULL);
4015
4016 bl->inserted = (is == mark_inserted);
4017 bl->owner->ops->remove_location (bl);
4018
4019 /* Failure to remove any of the hardware watchpoints comes here. */
4020 if ((is == mark_uninserted) && (bl->inserted))
4021 warning (_("Could not remove hardware watchpoint %d."),
4022 bl->owner->number);
4023 }
4024 else if (bl->owner->type == bp_catchpoint
4025 && breakpoint_enabled (bl->owner)
4026 && !bl->duplicate)
4027 {
4028 gdb_assert (bl->owner->ops != NULL
4029 && bl->owner->ops->remove_location != NULL);
4030
4031 val = bl->owner->ops->remove_location (bl);
4032 if (val)
4033 return val;
4034
4035 bl->inserted = (is == mark_inserted);
4036 }
4037
4038 return 0;
4039 }
4040
4041 static int
4042 remove_breakpoint (struct bp_location *bl, insertion_state_t is)
4043 {
4044 int ret;
4045 struct cleanup *old_chain;
4046
4047 /* BL is never in moribund_locations by our callers. */
4048 gdb_assert (bl->owner != NULL);
4049
4050 /* The type of none suggests that owner is actually deleted.
4051 This should not ever happen. */
4052 gdb_assert (bl->owner->type != bp_none);
4053
4054 old_chain = save_current_space_and_thread ();
4055
4056 switch_to_program_space_and_thread (bl->pspace);
4057
4058 ret = remove_breakpoint_1 (bl, is);
4059
4060 do_cleanups (old_chain);
4061 return ret;
4062 }
4063
4064 /* Clear the "inserted" flag in all breakpoints. */
4065
4066 void
4067 mark_breakpoints_out (void)
4068 {
4069 struct bp_location *bl, **blp_tmp;
4070
4071 ALL_BP_LOCATIONS (bl, blp_tmp)
4072 if (bl->pspace == current_program_space)
4073 bl->inserted = 0;
4074 }
4075
4076 /* Clear the "inserted" flag in all breakpoints and delete any
4077 breakpoints which should go away between runs of the program.
4078
4079 Plus other such housekeeping that has to be done for breakpoints
4080 between runs.
4081
4082 Note: this function gets called at the end of a run (by
4083 generic_mourn_inferior) and when a run begins (by
4084 init_wait_for_inferior). */
4085
4086
4087
4088 void
4089 breakpoint_init_inferior (enum inf_context context)
4090 {
4091 struct breakpoint *b, *b_tmp;
4092 struct bp_location *bl, **blp_tmp;
4093 int ix;
4094 struct program_space *pspace = current_program_space;
4095
4096 /* If breakpoint locations are shared across processes, then there's
4097 nothing to do. */
4098 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
4099 return;
4100
4101 mark_breakpoints_out ();
4102
4103 ALL_BREAKPOINTS_SAFE (b, b_tmp)
4104 {
4105 if (b->loc && b->loc->pspace != pspace)
4106 continue;
4107
4108 switch (b->type)
4109 {
4110 case bp_call_dummy:
4111 case bp_longjmp_call_dummy:
4112
4113 /* If the call dummy breakpoint is at the entry point it will
4114 cause problems when the inferior is rerun, so we better get
4115 rid of it. */
4116
4117 case bp_watchpoint_scope:
4118
4119 /* Also get rid of scope breakpoints. */
4120
4121 case bp_shlib_event:
4122
4123 /* Also remove solib event breakpoints. Their addresses may
4124 have changed since the last time we ran the program.
4125 Actually we may now be debugging against different target;
4126 and so the solib backend that installed this breakpoint may
4127 not be used in by the target. E.g.,
4128
4129 (gdb) file prog-linux
4130 (gdb) run # native linux target
4131 ...
4132 (gdb) kill
4133 (gdb) file prog-win.exe
4134 (gdb) tar rem :9999 # remote Windows gdbserver.
4135 */
4136
4137 case bp_step_resume:
4138
4139 /* Also remove step-resume breakpoints. */
4140
4141 case bp_single_step:
4142
4143 /* Also remove single-step breakpoints. */
4144
4145 delete_breakpoint (b);
4146 break;
4147
4148 case bp_watchpoint:
4149 case bp_hardware_watchpoint:
4150 case bp_read_watchpoint:
4151 case bp_access_watchpoint:
4152 {
4153 struct watchpoint *w = (struct watchpoint *) b;
4154
4155 /* Likewise for watchpoints on local expressions. */
4156 if (w->exp_valid_block != NULL)
4157 delete_breakpoint (b);
4158 else if (context == inf_starting)
4159 {
4160 /* Reset val field to force reread of starting value in
4161 insert_breakpoints. */
4162 if (w->val)
4163 value_free (w->val);
4164 w->val = NULL;
4165 w->val_valid = 0;
4166 }
4167 }
4168 break;
4169 default:
4170 break;
4171 }
4172 }
4173
4174 /* Get rid of the moribund locations. */
4175 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
4176 decref_bp_location (&bl);
4177 VEC_free (bp_location_p, moribund_locations);
4178 }
4179
4180 /* These functions concern about actual breakpoints inserted in the
4181 target --- to e.g. check if we need to do decr_pc adjustment or if
4182 we need to hop over the bkpt --- so we check for address space
4183 match, not program space. */
4184
4185 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4186 exists at PC. It returns ordinary_breakpoint_here if it's an
4187 ordinary breakpoint, or permanent_breakpoint_here if it's a
4188 permanent breakpoint.
4189 - When continuing from a location with an ordinary breakpoint, we
4190 actually single step once before calling insert_breakpoints.
4191 - When continuing from a location with a permanent breakpoint, we
4192 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4193 the target, to advance the PC past the breakpoint. */
4194
4195 enum breakpoint_here
4196 breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
4197 {
4198 struct bp_location *bl, **blp_tmp;
4199 int any_breakpoint_here = 0;
4200
4201 ALL_BP_LOCATIONS (bl, blp_tmp)
4202 {
4203 if (bl->loc_type != bp_loc_software_breakpoint
4204 && bl->loc_type != bp_loc_hardware_breakpoint)
4205 continue;
4206
4207 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
4208 if ((breakpoint_enabled (bl->owner)
4209 || bl->permanent)
4210 && breakpoint_location_address_match (bl, aspace, pc))
4211 {
4212 if (overlay_debugging
4213 && section_is_overlay (bl->section)
4214 && !section_is_mapped (bl->section))
4215 continue; /* unmapped overlay -- can't be a match */
4216 else if (bl->permanent)
4217 return permanent_breakpoint_here;
4218 else
4219 any_breakpoint_here = 1;
4220 }
4221 }
4222
4223 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
4224 }
4225
4226 /* Return true if there's a moribund breakpoint at PC. */
4227
4228 int
4229 moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
4230 {
4231 struct bp_location *loc;
4232 int ix;
4233
4234 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
4235 if (breakpoint_location_address_match (loc, aspace, pc))
4236 return 1;
4237
4238 return 0;
4239 }
4240
4241 /* Returns non-zero iff BL is inserted at PC, in address space
4242 ASPACE. */
4243
4244 static int
4245 bp_location_inserted_here_p (struct bp_location *bl,
4246 struct address_space *aspace, CORE_ADDR pc)
4247 {
4248 if (bl->inserted
4249 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4250 aspace, pc))
4251 {
4252 if (overlay_debugging
4253 && section_is_overlay (bl->section)
4254 && !section_is_mapped (bl->section))
4255 return 0; /* unmapped overlay -- can't be a match */
4256 else
4257 return 1;
4258 }
4259 return 0;
4260 }
4261
4262 /* Returns non-zero iff there's a breakpoint inserted at PC. */
4263
4264 int
4265 breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
4266 {
4267 struct bp_location **blp, **blp_tmp = NULL;
4268 struct bp_location *bl;
4269
4270 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4271 {
4272 struct bp_location *bl = *blp;
4273
4274 if (bl->loc_type != bp_loc_software_breakpoint
4275 && bl->loc_type != bp_loc_hardware_breakpoint)
4276 continue;
4277
4278 if (bp_location_inserted_here_p (bl, aspace, pc))
4279 return 1;
4280 }
4281 return 0;
4282 }
4283
4284 /* This function returns non-zero iff there is a software breakpoint
4285 inserted at PC. */
4286
4287 int
4288 software_breakpoint_inserted_here_p (struct address_space *aspace,
4289 CORE_ADDR pc)
4290 {
4291 struct bp_location **blp, **blp_tmp = NULL;
4292 struct bp_location *bl;
4293
4294 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4295 {
4296 struct bp_location *bl = *blp;
4297
4298 if (bl->loc_type != bp_loc_software_breakpoint)
4299 continue;
4300
4301 if (bp_location_inserted_here_p (bl, aspace, pc))
4302 return 1;
4303 }
4304
4305 return 0;
4306 }
4307
4308 /* See breakpoint.h. */
4309
4310 int
4311 hardware_breakpoint_inserted_here_p (struct address_space *aspace,
4312 CORE_ADDR pc)
4313 {
4314 struct bp_location **blp, **blp_tmp = NULL;
4315 struct bp_location *bl;
4316
4317 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4318 {
4319 struct bp_location *bl = *blp;
4320
4321 if (bl->loc_type != bp_loc_hardware_breakpoint)
4322 continue;
4323
4324 if (bp_location_inserted_here_p (bl, aspace, pc))
4325 return 1;
4326 }
4327
4328 return 0;
4329 }
4330
4331 int
4332 hardware_watchpoint_inserted_in_range (struct address_space *aspace,
4333 CORE_ADDR addr, ULONGEST len)
4334 {
4335 struct breakpoint *bpt;
4336
4337 ALL_BREAKPOINTS (bpt)
4338 {
4339 struct bp_location *loc;
4340
4341 if (bpt->type != bp_hardware_watchpoint
4342 && bpt->type != bp_access_watchpoint)
4343 continue;
4344
4345 if (!breakpoint_enabled (bpt))
4346 continue;
4347
4348 for (loc = bpt->loc; loc; loc = loc->next)
4349 if (loc->pspace->aspace == aspace && loc->inserted)
4350 {
4351 CORE_ADDR l, h;
4352
4353 /* Check for intersection. */
4354 l = max (loc->address, addr);
4355 h = min (loc->address + loc->length, addr + len);
4356 if (l < h)
4357 return 1;
4358 }
4359 }
4360 return 0;
4361 }
4362 \f
4363
4364 /* bpstat stuff. External routines' interfaces are documented
4365 in breakpoint.h. */
4366
4367 int
4368 is_catchpoint (struct breakpoint *ep)
4369 {
4370 return (ep->type == bp_catchpoint);
4371 }
4372
4373 /* Frees any storage that is part of a bpstat. Does not walk the
4374 'next' chain. */
4375
4376 static void
4377 bpstat_free (bpstat bs)
4378 {
4379 if (bs->old_val != NULL)
4380 value_free (bs->old_val);
4381 decref_counted_command_line (&bs->commands);
4382 decref_bp_location (&bs->bp_location_at);
4383 xfree (bs);
4384 }
4385
4386 /* Clear a bpstat so that it says we are not at any breakpoint.
4387 Also free any storage that is part of a bpstat. */
4388
4389 void
4390 bpstat_clear (bpstat *bsp)
4391 {
4392 bpstat p;
4393 bpstat q;
4394
4395 if (bsp == 0)
4396 return;
4397 p = *bsp;
4398 while (p != NULL)
4399 {
4400 q = p->next;
4401 bpstat_free (p);
4402 p = q;
4403 }
4404 *bsp = NULL;
4405 }
4406
4407 /* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4408 is part of the bpstat is copied as well. */
4409
4410 bpstat
4411 bpstat_copy (bpstat bs)
4412 {
4413 bpstat p = NULL;
4414 bpstat tmp;
4415 bpstat retval = NULL;
4416
4417 if (bs == NULL)
4418 return bs;
4419
4420 for (; bs != NULL; bs = bs->next)
4421 {
4422 tmp = (bpstat) xmalloc (sizeof (*tmp));
4423 memcpy (tmp, bs, sizeof (*tmp));
4424 incref_counted_command_line (tmp->commands);
4425 incref_bp_location (tmp->bp_location_at);
4426 if (bs->old_val != NULL)
4427 {
4428 tmp->old_val = value_copy (bs->old_val);
4429 release_value (tmp->old_val);
4430 }
4431
4432 if (p == NULL)
4433 /* This is the first thing in the chain. */
4434 retval = tmp;
4435 else
4436 p->next = tmp;
4437 p = tmp;
4438 }
4439 p->next = NULL;
4440 return retval;
4441 }
4442
4443 /* Find the bpstat associated with this breakpoint. */
4444
4445 bpstat
4446 bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
4447 {
4448 if (bsp == NULL)
4449 return NULL;
4450
4451 for (; bsp != NULL; bsp = bsp->next)
4452 {
4453 if (bsp->breakpoint_at == breakpoint)
4454 return bsp;
4455 }
4456 return NULL;
4457 }
4458
4459 /* See breakpoint.h. */
4460
4461 int
4462 bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
4463 {
4464 for (; bsp != NULL; bsp = bsp->next)
4465 {
4466 if (bsp->breakpoint_at == NULL)
4467 {
4468 /* A moribund location can never explain a signal other than
4469 GDB_SIGNAL_TRAP. */
4470 if (sig == GDB_SIGNAL_TRAP)
4471 return 1;
4472 }
4473 else
4474 {
4475 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4476 sig))
4477 return 1;
4478 }
4479 }
4480
4481 return 0;
4482 }
4483
4484 /* Put in *NUM the breakpoint number of the first breakpoint we are
4485 stopped at. *BSP upon return is a bpstat which points to the
4486 remaining breakpoints stopped at (but which is not guaranteed to be
4487 good for anything but further calls to bpstat_num).
4488
4489 Return 0 if passed a bpstat which does not indicate any breakpoints.
4490 Return -1 if stopped at a breakpoint that has been deleted since
4491 we set it.
4492 Return 1 otherwise. */
4493
4494 int
4495 bpstat_num (bpstat *bsp, int *num)
4496 {
4497 struct breakpoint *b;
4498
4499 if ((*bsp) == NULL)
4500 return 0; /* No more breakpoint values */
4501
4502 /* We assume we'll never have several bpstats that correspond to a
4503 single breakpoint -- otherwise, this function might return the
4504 same number more than once and this will look ugly. */
4505 b = (*bsp)->breakpoint_at;
4506 *bsp = (*bsp)->next;
4507 if (b == NULL)
4508 return -1; /* breakpoint that's been deleted since */
4509
4510 *num = b->number; /* We have its number */
4511 return 1;
4512 }
4513
4514 /* See breakpoint.h. */
4515
4516 void
4517 bpstat_clear_actions (void)
4518 {
4519 struct thread_info *tp;
4520 bpstat bs;
4521
4522 if (ptid_equal (inferior_ptid, null_ptid))
4523 return;
4524
4525 tp = find_thread_ptid (inferior_ptid);
4526 if (tp == NULL)
4527 return;
4528
4529 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
4530 {
4531 decref_counted_command_line (&bs->commands);
4532
4533 if (bs->old_val != NULL)
4534 {
4535 value_free (bs->old_val);
4536 bs->old_val = NULL;
4537 }
4538 }
4539 }
4540
4541 /* Called when a command is about to proceed the inferior. */
4542
4543 static void
4544 breakpoint_about_to_proceed (void)
4545 {
4546 if (!ptid_equal (inferior_ptid, null_ptid))
4547 {
4548 struct thread_info *tp = inferior_thread ();
4549
4550 /* Allow inferior function calls in breakpoint commands to not
4551 interrupt the command list. When the call finishes
4552 successfully, the inferior will be standing at the same
4553 breakpoint as if nothing happened. */
4554 if (tp->control.in_infcall)
4555 return;
4556 }
4557
4558 breakpoint_proceeded = 1;
4559 }
4560
4561 /* Stub for cleaning up our state if we error-out of a breakpoint
4562 command. */
4563 static void
4564 cleanup_executing_breakpoints (void *ignore)
4565 {
4566 executing_breakpoint_commands = 0;
4567 }
4568
4569 /* Return non-zero iff CMD as the first line of a command sequence is `silent'
4570 or its equivalent. */
4571
4572 static int
4573 command_line_is_silent (struct command_line *cmd)
4574 {
4575 return cmd && (strcmp ("silent", cmd->line) == 0);
4576 }
4577
4578 /* Execute all the commands associated with all the breakpoints at
4579 this location. Any of these commands could cause the process to
4580 proceed beyond this point, etc. We look out for such changes by
4581 checking the global "breakpoint_proceeded" after each command.
4582
4583 Returns true if a breakpoint command resumed the inferior. In that
4584 case, it is the caller's responsibility to recall it again with the
4585 bpstat of the current thread. */
4586
4587 static int
4588 bpstat_do_actions_1 (bpstat *bsp)
4589 {
4590 bpstat bs;
4591 struct cleanup *old_chain;
4592 int again = 0;
4593
4594 /* Avoid endless recursion if a `source' command is contained
4595 in bs->commands. */
4596 if (executing_breakpoint_commands)
4597 return 0;
4598
4599 executing_breakpoint_commands = 1;
4600 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
4601
4602 prevent_dont_repeat ();
4603
4604 /* This pointer will iterate over the list of bpstat's. */
4605 bs = *bsp;
4606
4607 breakpoint_proceeded = 0;
4608 for (; bs != NULL; bs = bs->next)
4609 {
4610 struct counted_command_line *ccmd;
4611 struct command_line *cmd;
4612 struct cleanup *this_cmd_tree_chain;
4613
4614 /* Take ownership of the BSP's command tree, if it has one.
4615
4616 The command tree could legitimately contain commands like
4617 'step' and 'next', which call clear_proceed_status, which
4618 frees stop_bpstat's command tree. To make sure this doesn't
4619 free the tree we're executing out from under us, we need to
4620 take ownership of the tree ourselves. Since a given bpstat's
4621 commands are only executed once, we don't need to copy it; we
4622 can clear the pointer in the bpstat, and make sure we free
4623 the tree when we're done. */
4624 ccmd = bs->commands;
4625 bs->commands = NULL;
4626 this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
4627 cmd = ccmd ? ccmd->commands : NULL;
4628 if (command_line_is_silent (cmd))
4629 {
4630 /* The action has been already done by bpstat_stop_status. */
4631 cmd = cmd->next;
4632 }
4633
4634 while (cmd != NULL)
4635 {
4636 execute_control_command (cmd);
4637
4638 if (breakpoint_proceeded)
4639 break;
4640 else
4641 cmd = cmd->next;
4642 }
4643
4644 /* We can free this command tree now. */
4645 do_cleanups (this_cmd_tree_chain);
4646
4647 if (breakpoint_proceeded)
4648 {
4649 if (interpreter_async && target_can_async_p ())
4650 /* If we are in async mode, then the target might be still
4651 running, not stopped at any breakpoint, so nothing for
4652 us to do here -- just return to the event loop. */
4653 ;
4654 else
4655 /* In sync mode, when execute_control_command returns
4656 we're already standing on the next breakpoint.
4657 Breakpoint commands for that stop were not run, since
4658 execute_command does not run breakpoint commands --
4659 only command_line_handler does, but that one is not
4660 involved in execution of breakpoint commands. So, we
4661 can now execute breakpoint commands. It should be
4662 noted that making execute_command do bpstat actions is
4663 not an option -- in this case we'll have recursive
4664 invocation of bpstat for each breakpoint with a
4665 command, and can easily blow up GDB stack. Instead, we
4666 return true, which will trigger the caller to recall us
4667 with the new stop_bpstat. */
4668 again = 1;
4669 break;
4670 }
4671 }
4672 do_cleanups (old_chain);
4673 return again;
4674 }
4675
4676 void
4677 bpstat_do_actions (void)
4678 {
4679 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4680
4681 /* Do any commands attached to breakpoint we are stopped at. */
4682 while (!ptid_equal (inferior_ptid, null_ptid)
4683 && target_has_execution
4684 && !is_exited (inferior_ptid)
4685 && !is_executing (inferior_ptid))
4686 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4687 and only return when it is stopped at the next breakpoint, we
4688 keep doing breakpoint actions until it returns false to
4689 indicate the inferior was not resumed. */
4690 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
4691 break;
4692
4693 discard_cleanups (cleanup_if_error);
4694 }
4695
4696 /* Print out the (old or new) value associated with a watchpoint. */
4697
4698 static void
4699 watchpoint_value_print (struct value *val, struct ui_file *stream)
4700 {
4701 if (val == NULL)
4702 fprintf_unfiltered (stream, _("<unreadable>"));
4703 else
4704 {
4705 struct value_print_options opts;
4706 get_user_print_options (&opts);
4707 value_print (val, stream, &opts);
4708 }
4709 }
4710
4711 /* Generic routine for printing messages indicating why we
4712 stopped. The behavior of this function depends on the value
4713 'print_it' in the bpstat structure. Under some circumstances we
4714 may decide not to print anything here and delegate the task to
4715 normal_stop(). */
4716
4717 static enum print_stop_action
4718 print_bp_stop_message (bpstat bs)
4719 {
4720 switch (bs->print_it)
4721 {
4722 case print_it_noop:
4723 /* Nothing should be printed for this bpstat entry. */
4724 return PRINT_UNKNOWN;
4725 break;
4726
4727 case print_it_done:
4728 /* We still want to print the frame, but we already printed the
4729 relevant messages. */
4730 return PRINT_SRC_AND_LOC;
4731 break;
4732
4733 case print_it_normal:
4734 {
4735 struct breakpoint *b = bs->breakpoint_at;
4736
4737 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4738 which has since been deleted. */
4739 if (b == NULL)
4740 return PRINT_UNKNOWN;
4741
4742 /* Normal case. Call the breakpoint's print_it method. */
4743 return b->ops->print_it (bs);
4744 }
4745 break;
4746
4747 default:
4748 internal_error (__FILE__, __LINE__,
4749 _("print_bp_stop_message: unrecognized enum value"));
4750 break;
4751 }
4752 }
4753
4754 /* A helper function that prints a shared library stopped event. */
4755
4756 static void
4757 print_solib_event (int is_catchpoint)
4758 {
4759 int any_deleted
4760 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4761 int any_added
4762 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4763
4764 if (!is_catchpoint)
4765 {
4766 if (any_added || any_deleted)
4767 ui_out_text (current_uiout,
4768 _("Stopped due to shared library event:\n"));
4769 else
4770 ui_out_text (current_uiout,
4771 _("Stopped due to shared library event (no "
4772 "libraries added or removed)\n"));
4773 }
4774
4775 if (ui_out_is_mi_like_p (current_uiout))
4776 ui_out_field_string (current_uiout, "reason",
4777 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4778
4779 if (any_deleted)
4780 {
4781 struct cleanup *cleanup;
4782 char *name;
4783 int ix;
4784
4785 ui_out_text (current_uiout, _(" Inferior unloaded "));
4786 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4787 "removed");
4788 for (ix = 0;
4789 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4790 ix, name);
4791 ++ix)
4792 {
4793 if (ix > 0)
4794 ui_out_text (current_uiout, " ");
4795 ui_out_field_string (current_uiout, "library", name);
4796 ui_out_text (current_uiout, "\n");
4797 }
4798
4799 do_cleanups (cleanup);
4800 }
4801
4802 if (any_added)
4803 {
4804 struct so_list *iter;
4805 int ix;
4806 struct cleanup *cleanup;
4807
4808 ui_out_text (current_uiout, _(" Inferior loaded "));
4809 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4810 "added");
4811 for (ix = 0;
4812 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4813 ix, iter);
4814 ++ix)
4815 {
4816 if (ix > 0)
4817 ui_out_text (current_uiout, " ");
4818 ui_out_field_string (current_uiout, "library", iter->so_name);
4819 ui_out_text (current_uiout, "\n");
4820 }
4821
4822 do_cleanups (cleanup);
4823 }
4824 }
4825
4826 /* Print a message indicating what happened. This is called from
4827 normal_stop(). The input to this routine is the head of the bpstat
4828 list - a list of the eventpoints that caused this stop. KIND is
4829 the target_waitkind for the stopping event. This
4830 routine calls the generic print routine for printing a message
4831 about reasons for stopping. This will print (for example) the
4832 "Breakpoint n," part of the output. The return value of this
4833 routine is one of:
4834
4835 PRINT_UNKNOWN: Means we printed nothing.
4836 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4837 code to print the location. An example is
4838 "Breakpoint 1, " which should be followed by
4839 the location.
4840 PRINT_SRC_ONLY: Means we printed something, but there is no need
4841 to also print the location part of the message.
4842 An example is the catch/throw messages, which
4843 don't require a location appended to the end.
4844 PRINT_NOTHING: We have done some printing and we don't need any
4845 further info to be printed. */
4846
4847 enum print_stop_action
4848 bpstat_print (bpstat bs, int kind)
4849 {
4850 enum print_stop_action val;
4851
4852 /* Maybe another breakpoint in the chain caused us to stop.
4853 (Currently all watchpoints go on the bpstat whether hit or not.
4854 That probably could (should) be changed, provided care is taken
4855 with respect to bpstat_explains_signal). */
4856 for (; bs; bs = bs->next)
4857 {
4858 val = print_bp_stop_message (bs);
4859 if (val == PRINT_SRC_ONLY
4860 || val == PRINT_SRC_AND_LOC
4861 || val == PRINT_NOTHING)
4862 return val;
4863 }
4864
4865 /* If we had hit a shared library event breakpoint,
4866 print_bp_stop_message would print out this message. If we hit an
4867 OS-level shared library event, do the same thing. */
4868 if (kind == TARGET_WAITKIND_LOADED)
4869 {
4870 print_solib_event (0);
4871 return PRINT_NOTHING;
4872 }
4873
4874 /* We reached the end of the chain, or we got a null BS to start
4875 with and nothing was printed. */
4876 return PRINT_UNKNOWN;
4877 }
4878
4879 /* Evaluate the expression EXP and return 1 if value is zero.
4880 This returns the inverse of the condition because it is called
4881 from catch_errors which returns 0 if an exception happened, and if an
4882 exception happens we want execution to stop.
4883 The argument is a "struct expression *" that has been cast to a
4884 "void *" to make it pass through catch_errors. */
4885
4886 static int
4887 breakpoint_cond_eval (void *exp)
4888 {
4889 struct value *mark = value_mark ();
4890 int i = !value_true (evaluate_expression ((struct expression *) exp));
4891
4892 value_free_to_mark (mark);
4893 return i;
4894 }
4895
4896 /* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
4897
4898 static bpstat
4899 bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
4900 {
4901 bpstat bs;
4902
4903 bs = (bpstat) xmalloc (sizeof (*bs));
4904 bs->next = NULL;
4905 **bs_link_pointer = bs;
4906 *bs_link_pointer = &bs->next;
4907 bs->breakpoint_at = bl->owner;
4908 bs->bp_location_at = bl;
4909 incref_bp_location (bl);
4910 /* If the condition is false, etc., don't do the commands. */
4911 bs->commands = NULL;
4912 bs->old_val = NULL;
4913 bs->print_it = print_it_normal;
4914 return bs;
4915 }
4916 \f
4917 /* The target has stopped with waitstatus WS. Check if any hardware
4918 watchpoints have triggered, according to the target. */
4919
4920 int
4921 watchpoints_triggered (struct target_waitstatus *ws)
4922 {
4923 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
4924 CORE_ADDR addr;
4925 struct breakpoint *b;
4926
4927 if (!stopped_by_watchpoint)
4928 {
4929 /* We were not stopped by a watchpoint. Mark all watchpoints
4930 as not triggered. */
4931 ALL_BREAKPOINTS (b)
4932 if (is_hardware_watchpoint (b))
4933 {
4934 struct watchpoint *w = (struct watchpoint *) b;
4935
4936 w->watchpoint_triggered = watch_triggered_no;
4937 }
4938
4939 return 0;
4940 }
4941
4942 if (!target_stopped_data_address (&current_target, &addr))
4943 {
4944 /* We were stopped by a watchpoint, but we don't know where.
4945 Mark all watchpoints as unknown. */
4946 ALL_BREAKPOINTS (b)
4947 if (is_hardware_watchpoint (b))
4948 {
4949 struct watchpoint *w = (struct watchpoint *) b;
4950
4951 w->watchpoint_triggered = watch_triggered_unknown;
4952 }
4953
4954 return 1;
4955 }
4956
4957 /* The target could report the data address. Mark watchpoints
4958 affected by this data address as triggered, and all others as not
4959 triggered. */
4960
4961 ALL_BREAKPOINTS (b)
4962 if (is_hardware_watchpoint (b))
4963 {
4964 struct watchpoint *w = (struct watchpoint *) b;
4965 struct bp_location *loc;
4966
4967 w->watchpoint_triggered = watch_triggered_no;
4968 for (loc = b->loc; loc; loc = loc->next)
4969 {
4970 if (is_masked_watchpoint (b))
4971 {
4972 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4973 CORE_ADDR start = loc->address & w->hw_wp_mask;
4974
4975 if (newaddr == start)
4976 {
4977 w->watchpoint_triggered = watch_triggered_yes;
4978 break;
4979 }
4980 }
4981 /* Exact match not required. Within range is sufficient. */
4982 else if (target_watchpoint_addr_within_range (&current_target,
4983 addr, loc->address,
4984 loc->length))
4985 {
4986 w->watchpoint_triggered = watch_triggered_yes;
4987 break;
4988 }
4989 }
4990 }
4991
4992 return 1;
4993 }
4994
4995 /* Possible return values for watchpoint_check (this can't be an enum
4996 because of check_errors). */
4997 /* The watchpoint has been deleted. */
4998 #define WP_DELETED 1
4999 /* The value has changed. */
5000 #define WP_VALUE_CHANGED 2
5001 /* The value has not changed. */
5002 #define WP_VALUE_NOT_CHANGED 3
5003 /* Ignore this watchpoint, no matter if the value changed or not. */
5004 #define WP_IGNORE 4
5005
5006 #define BP_TEMPFLAG 1
5007 #define BP_HARDWAREFLAG 2
5008
5009 /* Evaluate watchpoint condition expression and check if its value
5010 changed.
5011
5012 P should be a pointer to struct bpstat, but is defined as a void *
5013 in order for this function to be usable with catch_errors. */
5014
5015 static int
5016 watchpoint_check (void *p)
5017 {
5018 bpstat bs = (bpstat) p;
5019 struct watchpoint *b;
5020 struct frame_info *fr;
5021 int within_current_scope;
5022
5023 /* BS is built from an existing struct breakpoint. */
5024 gdb_assert (bs->breakpoint_at != NULL);
5025 b = (struct watchpoint *) bs->breakpoint_at;
5026
5027 /* If this is a local watchpoint, we only want to check if the
5028 watchpoint frame is in scope if the current thread is the thread
5029 that was used to create the watchpoint. */
5030 if (!watchpoint_in_thread_scope (b))
5031 return WP_IGNORE;
5032
5033 if (b->exp_valid_block == NULL)
5034 within_current_scope = 1;
5035 else
5036 {
5037 struct frame_info *frame = get_current_frame ();
5038 struct gdbarch *frame_arch = get_frame_arch (frame);
5039 CORE_ADDR frame_pc = get_frame_pc (frame);
5040
5041 /* stack_frame_destroyed_p() returns a non-zero value if we're
5042 still in the function but the stack frame has already been
5043 invalidated. Since we can't rely on the values of local
5044 variables after the stack has been destroyed, we are treating
5045 the watchpoint in that state as `not changed' without further
5046 checking. Don't mark watchpoints as changed if the current
5047 frame is in an epilogue - even if they are in some other
5048 frame, our view of the stack is likely to be wrong and
5049 frame_find_by_id could error out. */
5050 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
5051 return WP_IGNORE;
5052
5053 fr = frame_find_by_id (b->watchpoint_frame);
5054 within_current_scope = (fr != NULL);
5055
5056 /* If we've gotten confused in the unwinder, we might have
5057 returned a frame that can't describe this variable. */
5058 if (within_current_scope)
5059 {
5060 struct symbol *function;
5061
5062 function = get_frame_function (fr);
5063 if (function == NULL
5064 || !contained_in (b->exp_valid_block,
5065 SYMBOL_BLOCK_VALUE (function)))
5066 within_current_scope = 0;
5067 }
5068
5069 if (within_current_scope)
5070 /* If we end up stopping, the current frame will get selected
5071 in normal_stop. So this call to select_frame won't affect
5072 the user. */
5073 select_frame (fr);
5074 }
5075
5076 if (within_current_scope)
5077 {
5078 /* We use value_{,free_to_}mark because it could be a *long*
5079 time before we return to the command level and call
5080 free_all_values. We can't call free_all_values because we
5081 might be in the middle of evaluating a function call. */
5082
5083 int pc = 0;
5084 struct value *mark;
5085 struct value *new_val;
5086
5087 if (is_masked_watchpoint (&b->base))
5088 /* Since we don't know the exact trigger address (from
5089 stopped_data_address), just tell the user we've triggered
5090 a mask watchpoint. */
5091 return WP_VALUE_CHANGED;
5092
5093 mark = value_mark ();
5094 fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL, 0);
5095
5096 if (b->val_bitsize != 0)
5097 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5098
5099 /* We use value_equal_contents instead of value_equal because
5100 the latter coerces an array to a pointer, thus comparing just
5101 the address of the array instead of its contents. This is
5102 not what we want. */
5103 if ((b->val != NULL) != (new_val != NULL)
5104 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
5105 {
5106 if (new_val != NULL)
5107 {
5108 release_value (new_val);
5109 value_free_to_mark (mark);
5110 }
5111 bs->old_val = b->val;
5112 b->val = new_val;
5113 b->val_valid = 1;
5114 return WP_VALUE_CHANGED;
5115 }
5116 else
5117 {
5118 /* Nothing changed. */
5119 value_free_to_mark (mark);
5120 return WP_VALUE_NOT_CHANGED;
5121 }
5122 }
5123 else
5124 {
5125 struct ui_out *uiout = current_uiout;
5126
5127 /* This seems like the only logical thing to do because
5128 if we temporarily ignored the watchpoint, then when
5129 we reenter the block in which it is valid it contains
5130 garbage (in the case of a function, it may have two
5131 garbage values, one before and one after the prologue).
5132 So we can't even detect the first assignment to it and
5133 watch after that (since the garbage may or may not equal
5134 the first value assigned). */
5135 /* We print all the stop information in
5136 breakpoint_ops->print_it, but in this case, by the time we
5137 call breakpoint_ops->print_it this bp will be deleted
5138 already. So we have no choice but print the information
5139 here. */
5140 if (ui_out_is_mi_like_p (uiout))
5141 ui_out_field_string
5142 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
5143 ui_out_text (uiout, "\nWatchpoint ");
5144 ui_out_field_int (uiout, "wpnum", b->base.number);
5145 ui_out_text (uiout,
5146 " deleted because the program has left the block in\n\
5147 which its expression is valid.\n");
5148
5149 /* Make sure the watchpoint's commands aren't executed. */
5150 decref_counted_command_line (&b->base.commands);
5151 watchpoint_del_at_next_stop (b);
5152
5153 return WP_DELETED;
5154 }
5155 }
5156
5157 /* Return true if it looks like target has stopped due to hitting
5158 breakpoint location BL. This function does not check if we should
5159 stop, only if BL explains the stop. */
5160
5161 static int
5162 bpstat_check_location (const struct bp_location *bl,
5163 struct address_space *aspace, CORE_ADDR bp_addr,
5164 const struct target_waitstatus *ws)
5165 {
5166 struct breakpoint *b = bl->owner;
5167
5168 /* BL is from an existing breakpoint. */
5169 gdb_assert (b != NULL);
5170
5171 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
5172 }
5173
5174 /* Determine if the watched values have actually changed, and we
5175 should stop. If not, set BS->stop to 0. */
5176
5177 static void
5178 bpstat_check_watchpoint (bpstat bs)
5179 {
5180 const struct bp_location *bl;
5181 struct watchpoint *b;
5182
5183 /* BS is built for existing struct breakpoint. */
5184 bl = bs->bp_location_at;
5185 gdb_assert (bl != NULL);
5186 b = (struct watchpoint *) bs->breakpoint_at;
5187 gdb_assert (b != NULL);
5188
5189 {
5190 int must_check_value = 0;
5191
5192 if (b->base.type == bp_watchpoint)
5193 /* For a software watchpoint, we must always check the
5194 watched value. */
5195 must_check_value = 1;
5196 else if (b->watchpoint_triggered == watch_triggered_yes)
5197 /* We have a hardware watchpoint (read, write, or access)
5198 and the target earlier reported an address watched by
5199 this watchpoint. */
5200 must_check_value = 1;
5201 else if (b->watchpoint_triggered == watch_triggered_unknown
5202 && b->base.type == bp_hardware_watchpoint)
5203 /* We were stopped by a hardware watchpoint, but the target could
5204 not report the data address. We must check the watchpoint's
5205 value. Access and read watchpoints are out of luck; without
5206 a data address, we can't figure it out. */
5207 must_check_value = 1;
5208
5209 if (must_check_value)
5210 {
5211 char *message
5212 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
5213 b->base.number);
5214 struct cleanup *cleanups = make_cleanup (xfree, message);
5215 int e = catch_errors (watchpoint_check, bs, message,
5216 RETURN_MASK_ALL);
5217 do_cleanups (cleanups);
5218 switch (e)
5219 {
5220 case WP_DELETED:
5221 /* We've already printed what needs to be printed. */
5222 bs->print_it = print_it_done;
5223 /* Stop. */
5224 break;
5225 case WP_IGNORE:
5226 bs->print_it = print_it_noop;
5227 bs->stop = 0;
5228 break;
5229 case WP_VALUE_CHANGED:
5230 if (b->base.type == bp_read_watchpoint)
5231 {
5232 /* There are two cases to consider here:
5233
5234 1. We're watching the triggered memory for reads.
5235 In that case, trust the target, and always report
5236 the watchpoint hit to the user. Even though
5237 reads don't cause value changes, the value may
5238 have changed since the last time it was read, and
5239 since we're not trapping writes, we will not see
5240 those, and as such we should ignore our notion of
5241 old value.
5242
5243 2. We're watching the triggered memory for both
5244 reads and writes. There are two ways this may
5245 happen:
5246
5247 2.1. This is a target that can't break on data
5248 reads only, but can break on accesses (reads or
5249 writes), such as e.g., x86. We detect this case
5250 at the time we try to insert read watchpoints.
5251
5252 2.2. Otherwise, the target supports read
5253 watchpoints, but, the user set an access or write
5254 watchpoint watching the same memory as this read
5255 watchpoint.
5256
5257 If we're watching memory writes as well as reads,
5258 ignore watchpoint hits when we find that the
5259 value hasn't changed, as reads don't cause
5260 changes. This still gives false positives when
5261 the program writes the same value to memory as
5262 what there was already in memory (we will confuse
5263 it for a read), but it's much better than
5264 nothing. */
5265
5266 int other_write_watchpoint = 0;
5267
5268 if (bl->watchpoint_type == hw_read)
5269 {
5270 struct breakpoint *other_b;
5271
5272 ALL_BREAKPOINTS (other_b)
5273 if (other_b->type == bp_hardware_watchpoint
5274 || other_b->type == bp_access_watchpoint)
5275 {
5276 struct watchpoint *other_w =
5277 (struct watchpoint *) other_b;
5278
5279 if (other_w->watchpoint_triggered
5280 == watch_triggered_yes)
5281 {
5282 other_write_watchpoint = 1;
5283 break;
5284 }
5285 }
5286 }
5287
5288 if (other_write_watchpoint
5289 || bl->watchpoint_type == hw_access)
5290 {
5291 /* We're watching the same memory for writes,
5292 and the value changed since the last time we
5293 updated it, so this trap must be for a write.
5294 Ignore it. */
5295 bs->print_it = print_it_noop;
5296 bs->stop = 0;
5297 }
5298 }
5299 break;
5300 case WP_VALUE_NOT_CHANGED:
5301 if (b->base.type == bp_hardware_watchpoint
5302 || b->base.type == bp_watchpoint)
5303 {
5304 /* Don't stop: write watchpoints shouldn't fire if
5305 the value hasn't changed. */
5306 bs->print_it = print_it_noop;
5307 bs->stop = 0;
5308 }
5309 /* Stop. */
5310 break;
5311 default:
5312 /* Can't happen. */
5313 case 0:
5314 /* Error from catch_errors. */
5315 printf_filtered (_("Watchpoint %d deleted.\n"), b->base.number);
5316 watchpoint_del_at_next_stop (b);
5317 /* We've already printed what needs to be printed. */
5318 bs->print_it = print_it_done;
5319 break;
5320 }
5321 }
5322 else /* must_check_value == 0 */
5323 {
5324 /* This is a case where some watchpoint(s) triggered, but
5325 not at the address of this watchpoint, or else no
5326 watchpoint triggered after all. So don't print
5327 anything for this watchpoint. */
5328 bs->print_it = print_it_noop;
5329 bs->stop = 0;
5330 }
5331 }
5332 }
5333
5334 /* For breakpoints that are currently marked as telling gdb to stop,
5335 check conditions (condition proper, frame, thread and ignore count)
5336 of breakpoint referred to by BS. If we should not stop for this
5337 breakpoint, set BS->stop to 0. */
5338
5339 static void
5340 bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5341 {
5342 const struct bp_location *bl;
5343 struct breakpoint *b;
5344 int value_is_zero = 0;
5345 struct expression *cond;
5346
5347 gdb_assert (bs->stop);
5348
5349 /* BS is built for existing struct breakpoint. */
5350 bl = bs->bp_location_at;
5351 gdb_assert (bl != NULL);
5352 b = bs->breakpoint_at;
5353 gdb_assert (b != NULL);
5354
5355 /* Even if the target evaluated the condition on its end and notified GDB, we
5356 need to do so again since GDB does not know if we stopped due to a
5357 breakpoint or a single step breakpoint. */
5358
5359 if (frame_id_p (b->frame_id)
5360 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
5361 {
5362 bs->stop = 0;
5363 return;
5364 }
5365
5366 /* If this is a thread/task-specific breakpoint, don't waste cpu
5367 evaluating the condition if this isn't the specified
5368 thread/task. */
5369 if ((b->thread != -1 && b->thread != pid_to_thread_id (ptid))
5370 || (b->task != 0 && b->task != ada_get_task_number (ptid)))
5371
5372 {
5373 bs->stop = 0;
5374 return;
5375 }
5376
5377 /* Evaluate extension language breakpoints that have a "stop" method
5378 implemented. */
5379 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
5380
5381 if (is_watchpoint (b))
5382 {
5383 struct watchpoint *w = (struct watchpoint *) b;
5384
5385 cond = w->cond_exp;
5386 }
5387 else
5388 cond = bl->cond;
5389
5390 if (cond && b->disposition != disp_del_at_next_stop)
5391 {
5392 int within_current_scope = 1;
5393 struct watchpoint * w;
5394
5395 /* We use value_mark and value_free_to_mark because it could
5396 be a long time before we return to the command level and
5397 call free_all_values. We can't call free_all_values
5398 because we might be in the middle of evaluating a
5399 function call. */
5400 struct value *mark = value_mark ();
5401
5402 if (is_watchpoint (b))
5403 w = (struct watchpoint *) b;
5404 else
5405 w = NULL;
5406
5407 /* Need to select the frame, with all that implies so that
5408 the conditions will have the right context. Because we
5409 use the frame, we will not see an inlined function's
5410 variables when we arrive at a breakpoint at the start
5411 of the inlined function; the current frame will be the
5412 call site. */
5413 if (w == NULL || w->cond_exp_valid_block == NULL)
5414 select_frame (get_current_frame ());
5415 else
5416 {
5417 struct frame_info *frame;
5418
5419 /* For local watchpoint expressions, which particular
5420 instance of a local is being watched matters, so we
5421 keep track of the frame to evaluate the expression
5422 in. To evaluate the condition however, it doesn't
5423 really matter which instantiation of the function
5424 where the condition makes sense triggers the
5425 watchpoint. This allows an expression like "watch
5426 global if q > 10" set in `func', catch writes to
5427 global on all threads that call `func', or catch
5428 writes on all recursive calls of `func' by a single
5429 thread. We simply always evaluate the condition in
5430 the innermost frame that's executing where it makes
5431 sense to evaluate the condition. It seems
5432 intuitive. */
5433 frame = block_innermost_frame (w->cond_exp_valid_block);
5434 if (frame != NULL)
5435 select_frame (frame);
5436 else
5437 within_current_scope = 0;
5438 }
5439 if (within_current_scope)
5440 value_is_zero
5441 = catch_errors (breakpoint_cond_eval, cond,
5442 "Error in testing breakpoint condition:\n",
5443 RETURN_MASK_ALL);
5444 else
5445 {
5446 warning (_("Watchpoint condition cannot be tested "
5447 "in the current scope"));
5448 /* If we failed to set the right context for this
5449 watchpoint, unconditionally report it. */
5450 value_is_zero = 0;
5451 }
5452 /* FIXME-someday, should give breakpoint #. */
5453 value_free_to_mark (mark);
5454 }
5455
5456 if (cond && value_is_zero)
5457 {
5458 bs->stop = 0;
5459 }
5460 else if (b->ignore_count > 0)
5461 {
5462 b->ignore_count--;
5463 bs->stop = 0;
5464 /* Increase the hit count even though we don't stop. */
5465 ++(b->hit_count);
5466 observer_notify_breakpoint_modified (b);
5467 }
5468 }
5469
5470 /* Returns true if we need to track moribund locations of LOC's type
5471 on the current target. */
5472
5473 static int
5474 need_moribund_for_location_type (struct bp_location *loc)
5475 {
5476 return ((loc->loc_type == bp_loc_software_breakpoint
5477 && !target_supports_stopped_by_sw_breakpoint ())
5478 || (loc->loc_type == bp_loc_hardware_breakpoint
5479 && !target_supports_stopped_by_hw_breakpoint ()));
5480 }
5481
5482
5483 /* Get a bpstat associated with having just stopped at address
5484 BP_ADDR in thread PTID.
5485
5486 Determine whether we stopped at a breakpoint, etc, or whether we
5487 don't understand this stop. Result is a chain of bpstat's such
5488 that:
5489
5490 if we don't understand the stop, the result is a null pointer.
5491
5492 if we understand why we stopped, the result is not null.
5493
5494 Each element of the chain refers to a particular breakpoint or
5495 watchpoint at which we have stopped. (We may have stopped for
5496 several reasons concurrently.)
5497
5498 Each element of the chain has valid next, breakpoint_at,
5499 commands, FIXME??? fields. */
5500
5501 bpstat
5502 bpstat_stop_status (struct address_space *aspace,
5503 CORE_ADDR bp_addr, ptid_t ptid,
5504 const struct target_waitstatus *ws)
5505 {
5506 struct breakpoint *b = NULL;
5507 struct bp_location *bl;
5508 struct bp_location *loc;
5509 /* First item of allocated bpstat's. */
5510 bpstat bs_head = NULL, *bs_link = &bs_head;
5511 /* Pointer to the last thing in the chain currently. */
5512 bpstat bs;
5513 int ix;
5514 int need_remove_insert;
5515 int removed_any;
5516
5517 /* First, build the bpstat chain with locations that explain a
5518 target stop, while being careful to not set the target running,
5519 as that may invalidate locations (in particular watchpoint
5520 locations are recreated). Resuming will happen here with
5521 breakpoint conditions or watchpoint expressions that include
5522 inferior function calls. */
5523
5524 ALL_BREAKPOINTS (b)
5525 {
5526 if (!breakpoint_enabled (b))
5527 continue;
5528
5529 for (bl = b->loc; bl != NULL; bl = bl->next)
5530 {
5531 /* For hardware watchpoints, we look only at the first
5532 location. The watchpoint_check function will work on the
5533 entire expression, not the individual locations. For
5534 read watchpoints, the watchpoints_triggered function has
5535 checked all locations already. */
5536 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5537 break;
5538
5539 if (!bl->enabled || bl->shlib_disabled)
5540 continue;
5541
5542 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
5543 continue;
5544
5545 /* Come here if it's a watchpoint, or if the break address
5546 matches. */
5547
5548 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
5549 explain stop. */
5550
5551 /* Assume we stop. Should we find a watchpoint that is not
5552 actually triggered, or if the condition of the breakpoint
5553 evaluates as false, we'll reset 'stop' to 0. */
5554 bs->stop = 1;
5555 bs->print = 1;
5556
5557 /* If this is a scope breakpoint, mark the associated
5558 watchpoint as triggered so that we will handle the
5559 out-of-scope event. We'll get to the watchpoint next
5560 iteration. */
5561 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
5562 {
5563 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5564
5565 w->watchpoint_triggered = watch_triggered_yes;
5566 }
5567 }
5568 }
5569
5570 /* Check if a moribund breakpoint explains the stop. */
5571 if (!target_supports_stopped_by_sw_breakpoint ()
5572 || !target_supports_stopped_by_hw_breakpoint ())
5573 {
5574 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
5575 {
5576 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5577 && need_moribund_for_location_type (loc))
5578 {
5579 bs = bpstat_alloc (loc, &bs_link);
5580 /* For hits of moribund locations, we should just proceed. */
5581 bs->stop = 0;
5582 bs->print = 0;
5583 bs->print_it = print_it_noop;
5584 }
5585 }
5586 }
5587
5588 /* A bit of special processing for shlib breakpoints. We need to
5589 process solib loading here, so that the lists of loaded and
5590 unloaded libraries are correct before we handle "catch load" and
5591 "catch unload". */
5592 for (bs = bs_head; bs != NULL; bs = bs->next)
5593 {
5594 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
5595 {
5596 handle_solib_event ();
5597 break;
5598 }
5599 }
5600
5601 /* Now go through the locations that caused the target to stop, and
5602 check whether we're interested in reporting this stop to higher
5603 layers, or whether we should resume the target transparently. */
5604
5605 removed_any = 0;
5606
5607 for (bs = bs_head; bs != NULL; bs = bs->next)
5608 {
5609 if (!bs->stop)
5610 continue;
5611
5612 b = bs->breakpoint_at;
5613 b->ops->check_status (bs);
5614 if (bs->stop)
5615 {
5616 bpstat_check_breakpoint_conditions (bs, ptid);
5617
5618 if (bs->stop)
5619 {
5620 ++(b->hit_count);
5621 observer_notify_breakpoint_modified (b);
5622
5623 /* We will stop here. */
5624 if (b->disposition == disp_disable)
5625 {
5626 --(b->enable_count);
5627 if (b->enable_count <= 0)
5628 b->enable_state = bp_disabled;
5629 removed_any = 1;
5630 }
5631 if (b->silent)
5632 bs->print = 0;
5633 bs->commands = b->commands;
5634 incref_counted_command_line (bs->commands);
5635 if (command_line_is_silent (bs->commands
5636 ? bs->commands->commands : NULL))
5637 bs->print = 0;
5638
5639 b->ops->after_condition_true (bs);
5640 }
5641
5642 }
5643
5644 /* Print nothing for this entry if we don't stop or don't
5645 print. */
5646 if (!bs->stop || !bs->print)
5647 bs->print_it = print_it_noop;
5648 }
5649
5650 /* If we aren't stopping, the value of some hardware watchpoint may
5651 not have changed, but the intermediate memory locations we are
5652 watching may have. Don't bother if we're stopping; this will get
5653 done later. */
5654 need_remove_insert = 0;
5655 if (! bpstat_causes_stop (bs_head))
5656 for (bs = bs_head; bs != NULL; bs = bs->next)
5657 if (!bs->stop
5658 && bs->breakpoint_at
5659 && is_hardware_watchpoint (bs->breakpoint_at))
5660 {
5661 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5662
5663 update_watchpoint (w, 0 /* don't reparse. */);
5664 need_remove_insert = 1;
5665 }
5666
5667 if (need_remove_insert)
5668 update_global_location_list (UGLL_MAY_INSERT);
5669 else if (removed_any)
5670 update_global_location_list (UGLL_DONT_INSERT);
5671
5672 return bs_head;
5673 }
5674
5675 static void
5676 handle_jit_event (void)
5677 {
5678 struct frame_info *frame;
5679 struct gdbarch *gdbarch;
5680
5681 /* Switch terminal for any messages produced by
5682 breakpoint_re_set. */
5683 target_terminal_ours_for_output ();
5684
5685 frame = get_current_frame ();
5686 gdbarch = get_frame_arch (frame);
5687
5688 jit_event_handler (gdbarch);
5689
5690 target_terminal_inferior ();
5691 }
5692
5693 /* Prepare WHAT final decision for infrun. */
5694
5695 /* Decide what infrun needs to do with this bpstat. */
5696
5697 struct bpstat_what
5698 bpstat_what (bpstat bs_head)
5699 {
5700 struct bpstat_what retval;
5701 int jit_event = 0;
5702 bpstat bs;
5703
5704 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
5705 retval.call_dummy = STOP_NONE;
5706 retval.is_longjmp = 0;
5707
5708 for (bs = bs_head; bs != NULL; bs = bs->next)
5709 {
5710 /* Extract this BS's action. After processing each BS, we check
5711 if its action overrides all we've seem so far. */
5712 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5713 enum bptype bptype;
5714
5715 if (bs->breakpoint_at == NULL)
5716 {
5717 /* I suspect this can happen if it was a momentary
5718 breakpoint which has since been deleted. */
5719 bptype = bp_none;
5720 }
5721 else
5722 bptype = bs->breakpoint_at->type;
5723
5724 switch (bptype)
5725 {
5726 case bp_none:
5727 break;
5728 case bp_breakpoint:
5729 case bp_hardware_breakpoint:
5730 case bp_single_step:
5731 case bp_until:
5732 case bp_finish:
5733 case bp_shlib_event:
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 this_action = BPSTAT_WHAT_SINGLE;
5743 break;
5744 case bp_watchpoint:
5745 case bp_hardware_watchpoint:
5746 case bp_read_watchpoint:
5747 case bp_access_watchpoint:
5748 if (bs->stop)
5749 {
5750 if (bs->print)
5751 this_action = BPSTAT_WHAT_STOP_NOISY;
5752 else
5753 this_action = BPSTAT_WHAT_STOP_SILENT;
5754 }
5755 else
5756 {
5757 /* There was a watchpoint, but we're not stopping.
5758 This requires no further action. */
5759 }
5760 break;
5761 case bp_longjmp:
5762 case bp_longjmp_call_dummy:
5763 case bp_exception:
5764 if (bs->stop)
5765 {
5766 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5767 retval.is_longjmp = bptype != bp_exception;
5768 }
5769 else
5770 this_action = BPSTAT_WHAT_SINGLE;
5771 break;
5772 case bp_longjmp_resume:
5773 case bp_exception_resume:
5774 if (bs->stop)
5775 {
5776 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5777 retval.is_longjmp = bptype == bp_longjmp_resume;
5778 }
5779 else
5780 this_action = BPSTAT_WHAT_SINGLE;
5781 break;
5782 case bp_step_resume:
5783 if (bs->stop)
5784 this_action = BPSTAT_WHAT_STEP_RESUME;
5785 else
5786 {
5787 /* It is for the wrong frame. */
5788 this_action = BPSTAT_WHAT_SINGLE;
5789 }
5790 break;
5791 case bp_hp_step_resume:
5792 if (bs->stop)
5793 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5794 else
5795 {
5796 /* It is for the wrong frame. */
5797 this_action = BPSTAT_WHAT_SINGLE;
5798 }
5799 break;
5800 case bp_watchpoint_scope:
5801 case bp_thread_event:
5802 case bp_overlay_event:
5803 case bp_longjmp_master:
5804 case bp_std_terminate_master:
5805 case bp_exception_master:
5806 this_action = BPSTAT_WHAT_SINGLE;
5807 break;
5808 case bp_catchpoint:
5809 if (bs->stop)
5810 {
5811 if (bs->print)
5812 this_action = BPSTAT_WHAT_STOP_NOISY;
5813 else
5814 this_action = BPSTAT_WHAT_STOP_SILENT;
5815 }
5816 else
5817 {
5818 /* There was a catchpoint, but we're not stopping.
5819 This requires no further action. */
5820 }
5821 break;
5822 case bp_jit_event:
5823 jit_event = 1;
5824 this_action = BPSTAT_WHAT_SINGLE;
5825 break;
5826 case bp_call_dummy:
5827 /* Make sure the action is stop (silent or noisy),
5828 so infrun.c pops the dummy frame. */
5829 retval.call_dummy = STOP_STACK_DUMMY;
5830 this_action = BPSTAT_WHAT_STOP_SILENT;
5831 break;
5832 case bp_std_terminate:
5833 /* Make sure the action is stop (silent or noisy),
5834 so infrun.c pops the dummy frame. */
5835 retval.call_dummy = STOP_STD_TERMINATE;
5836 this_action = BPSTAT_WHAT_STOP_SILENT;
5837 break;
5838 case bp_tracepoint:
5839 case bp_fast_tracepoint:
5840 case bp_static_tracepoint:
5841 /* Tracepoint hits should not be reported back to GDB, and
5842 if one got through somehow, it should have been filtered
5843 out already. */
5844 internal_error (__FILE__, __LINE__,
5845 _("bpstat_what: tracepoint encountered"));
5846 break;
5847 case bp_gnu_ifunc_resolver:
5848 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5849 this_action = BPSTAT_WHAT_SINGLE;
5850 break;
5851 case bp_gnu_ifunc_resolver_return:
5852 /* The breakpoint will be removed, execution will restart from the
5853 PC of the former breakpoint. */
5854 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5855 break;
5856
5857 case bp_dprintf:
5858 if (bs->stop)
5859 this_action = BPSTAT_WHAT_STOP_SILENT;
5860 else
5861 this_action = BPSTAT_WHAT_SINGLE;
5862 break;
5863
5864 default:
5865 internal_error (__FILE__, __LINE__,
5866 _("bpstat_what: unhandled bptype %d"), (int) bptype);
5867 }
5868
5869 retval.main_action = max (retval.main_action, this_action);
5870 }
5871
5872 /* These operations may affect the bs->breakpoint_at state so they are
5873 delayed after MAIN_ACTION is decided above. */
5874
5875 if (jit_event)
5876 {
5877 if (debug_infrun)
5878 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_jit_event\n");
5879
5880 handle_jit_event ();
5881 }
5882
5883 for (bs = bs_head; bs != NULL; bs = bs->next)
5884 {
5885 struct breakpoint *b = bs->breakpoint_at;
5886
5887 if (b == NULL)
5888 continue;
5889 switch (b->type)
5890 {
5891 case bp_gnu_ifunc_resolver:
5892 gnu_ifunc_resolver_stop (b);
5893 break;
5894 case bp_gnu_ifunc_resolver_return:
5895 gnu_ifunc_resolver_return_stop (b);
5896 break;
5897 }
5898 }
5899
5900 return retval;
5901 }
5902
5903 /* Nonzero if we should step constantly (e.g. watchpoints on machines
5904 without hardware support). This isn't related to a specific bpstat,
5905 just to things like whether watchpoints are set. */
5906
5907 int
5908 bpstat_should_step (void)
5909 {
5910 struct breakpoint *b;
5911
5912 ALL_BREAKPOINTS (b)
5913 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
5914 return 1;
5915 return 0;
5916 }
5917
5918 int
5919 bpstat_causes_stop (bpstat bs)
5920 {
5921 for (; bs != NULL; bs = bs->next)
5922 if (bs->stop)
5923 return 1;
5924
5925 return 0;
5926 }
5927
5928 \f
5929
5930 /* Compute a string of spaces suitable to indent the next line
5931 so it starts at the position corresponding to the table column
5932 named COL_NAME in the currently active table of UIOUT. */
5933
5934 static char *
5935 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5936 {
5937 static char wrap_indent[80];
5938 int i, total_width, width, align;
5939 char *text;
5940
5941 total_width = 0;
5942 for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
5943 {
5944 if (strcmp (text, col_name) == 0)
5945 {
5946 gdb_assert (total_width < sizeof wrap_indent);
5947 memset (wrap_indent, ' ', total_width);
5948 wrap_indent[total_width] = 0;
5949
5950 return wrap_indent;
5951 }
5952
5953 total_width += width + 1;
5954 }
5955
5956 return NULL;
5957 }
5958
5959 /* Determine if the locations of this breakpoint will have their conditions
5960 evaluated by the target, host or a mix of both. Returns the following:
5961
5962 "host": Host evals condition.
5963 "host or target": Host or Target evals condition.
5964 "target": Target evals condition.
5965 */
5966
5967 static const char *
5968 bp_condition_evaluator (struct breakpoint *b)
5969 {
5970 struct bp_location *bl;
5971 char host_evals = 0;
5972 char target_evals = 0;
5973
5974 if (!b)
5975 return NULL;
5976
5977 if (!is_breakpoint (b))
5978 return NULL;
5979
5980 if (gdb_evaluates_breakpoint_condition_p ()
5981 || !target_supports_evaluation_of_breakpoint_conditions ())
5982 return condition_evaluation_host;
5983
5984 for (bl = b->loc; bl; bl = bl->next)
5985 {
5986 if (bl->cond_bytecode)
5987 target_evals++;
5988 else
5989 host_evals++;
5990 }
5991
5992 if (host_evals && target_evals)
5993 return condition_evaluation_both;
5994 else if (target_evals)
5995 return condition_evaluation_target;
5996 else
5997 return condition_evaluation_host;
5998 }
5999
6000 /* Determine the breakpoint location's condition evaluator. This is
6001 similar to bp_condition_evaluator, but for locations. */
6002
6003 static const char *
6004 bp_location_condition_evaluator (struct bp_location *bl)
6005 {
6006 if (bl && !is_breakpoint (bl->owner))
6007 return NULL;
6008
6009 if (gdb_evaluates_breakpoint_condition_p ()
6010 || !target_supports_evaluation_of_breakpoint_conditions ())
6011 return condition_evaluation_host;
6012
6013 if (bl && bl->cond_bytecode)
6014 return condition_evaluation_target;
6015 else
6016 return condition_evaluation_host;
6017 }
6018
6019 /* Print the LOC location out of the list of B->LOC locations. */
6020
6021 static void
6022 print_breakpoint_location (struct breakpoint *b,
6023 struct bp_location *loc)
6024 {
6025 struct ui_out *uiout = current_uiout;
6026 struct cleanup *old_chain = save_current_program_space ();
6027
6028 if (loc != NULL && loc->shlib_disabled)
6029 loc = NULL;
6030
6031 if (loc != NULL)
6032 set_current_program_space (loc->pspace);
6033
6034 if (b->display_canonical)
6035 ui_out_field_string (uiout, "what", b->addr_string);
6036 else if (loc && loc->symtab)
6037 {
6038 struct symbol *sym
6039 = find_pc_sect_function (loc->address, loc->section);
6040 if (sym)
6041 {
6042 ui_out_text (uiout, "in ");
6043 ui_out_field_string (uiout, "func",
6044 SYMBOL_PRINT_NAME (sym));
6045 ui_out_text (uiout, " ");
6046 ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
6047 ui_out_text (uiout, "at ");
6048 }
6049 ui_out_field_string (uiout, "file",
6050 symtab_to_filename_for_display (loc->symtab));
6051 ui_out_text (uiout, ":");
6052
6053 if (ui_out_is_mi_like_p (uiout))
6054 ui_out_field_string (uiout, "fullname",
6055 symtab_to_fullname (loc->symtab));
6056
6057 ui_out_field_int (uiout, "line", loc->line_number);
6058 }
6059 else if (loc)
6060 {
6061 struct ui_file *stb = mem_fileopen ();
6062 struct cleanup *stb_chain = make_cleanup_ui_file_delete (stb);
6063
6064 print_address_symbolic (loc->gdbarch, loc->address, stb,
6065 demangle, "");
6066 ui_out_field_stream (uiout, "at", stb);
6067
6068 do_cleanups (stb_chain);
6069 }
6070 else
6071 ui_out_field_string (uiout, "pending", b->addr_string);
6072
6073 if (loc && is_breakpoint (b)
6074 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6075 && bp_condition_evaluator (b) == condition_evaluation_both)
6076 {
6077 ui_out_text (uiout, " (");
6078 ui_out_field_string (uiout, "evaluated-by",
6079 bp_location_condition_evaluator (loc));
6080 ui_out_text (uiout, ")");
6081 }
6082
6083 do_cleanups (old_chain);
6084 }
6085
6086 static const char *
6087 bptype_string (enum bptype type)
6088 {
6089 struct ep_type_description
6090 {
6091 enum bptype type;
6092 char *description;
6093 };
6094 static struct ep_type_description bptypes[] =
6095 {
6096 {bp_none, "?deleted?"},
6097 {bp_breakpoint, "breakpoint"},
6098 {bp_hardware_breakpoint, "hw breakpoint"},
6099 {bp_single_step, "sw single-step"},
6100 {bp_until, "until"},
6101 {bp_finish, "finish"},
6102 {bp_watchpoint, "watchpoint"},
6103 {bp_hardware_watchpoint, "hw watchpoint"},
6104 {bp_read_watchpoint, "read watchpoint"},
6105 {bp_access_watchpoint, "acc watchpoint"},
6106 {bp_longjmp, "longjmp"},
6107 {bp_longjmp_resume, "longjmp resume"},
6108 {bp_longjmp_call_dummy, "longjmp for call dummy"},
6109 {bp_exception, "exception"},
6110 {bp_exception_resume, "exception resume"},
6111 {bp_step_resume, "step resume"},
6112 {bp_hp_step_resume, "high-priority step resume"},
6113 {bp_watchpoint_scope, "watchpoint scope"},
6114 {bp_call_dummy, "call dummy"},
6115 {bp_std_terminate, "std::terminate"},
6116 {bp_shlib_event, "shlib events"},
6117 {bp_thread_event, "thread events"},
6118 {bp_overlay_event, "overlay events"},
6119 {bp_longjmp_master, "longjmp master"},
6120 {bp_std_terminate_master, "std::terminate master"},
6121 {bp_exception_master, "exception master"},
6122 {bp_catchpoint, "catchpoint"},
6123 {bp_tracepoint, "tracepoint"},
6124 {bp_fast_tracepoint, "fast tracepoint"},
6125 {bp_static_tracepoint, "static tracepoint"},
6126 {bp_dprintf, "dprintf"},
6127 {bp_jit_event, "jit events"},
6128 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6129 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
6130 };
6131
6132 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6133 || ((int) type != bptypes[(int) type].type))
6134 internal_error (__FILE__, __LINE__,
6135 _("bptypes table does not describe type #%d."),
6136 (int) type);
6137
6138 return bptypes[(int) type].description;
6139 }
6140
6141 /* For MI, output a field named 'thread-groups' with a list as the value.
6142 For CLI, prefix the list with the string 'inf'. */
6143
6144 static void
6145 output_thread_groups (struct ui_out *uiout,
6146 const char *field_name,
6147 VEC(int) *inf_num,
6148 int mi_only)
6149 {
6150 struct cleanup *back_to;
6151 int is_mi = ui_out_is_mi_like_p (uiout);
6152 int inf;
6153 int i;
6154
6155 /* For backward compatibility, don't display inferiors in CLI unless
6156 there are several. Always display them for MI. */
6157 if (!is_mi && mi_only)
6158 return;
6159
6160 back_to = make_cleanup_ui_out_list_begin_end (uiout, field_name);
6161
6162 for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
6163 {
6164 if (is_mi)
6165 {
6166 char mi_group[10];
6167
6168 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
6169 ui_out_field_string (uiout, NULL, mi_group);
6170 }
6171 else
6172 {
6173 if (i == 0)
6174 ui_out_text (uiout, " inf ");
6175 else
6176 ui_out_text (uiout, ", ");
6177
6178 ui_out_text (uiout, plongest (inf));
6179 }
6180 }
6181
6182 do_cleanups (back_to);
6183 }
6184
6185 /* Print B to gdb_stdout. */
6186
6187 static void
6188 print_one_breakpoint_location (struct breakpoint *b,
6189 struct bp_location *loc,
6190 int loc_number,
6191 struct bp_location **last_loc,
6192 int allflag)
6193 {
6194 struct command_line *l;
6195 static char bpenables[] = "nynny";
6196
6197 struct ui_out *uiout = current_uiout;
6198 int header_of_multiple = 0;
6199 int part_of_multiple = (loc != NULL);
6200 struct value_print_options opts;
6201
6202 get_user_print_options (&opts);
6203
6204 gdb_assert (!loc || loc_number != 0);
6205 /* See comment in print_one_breakpoint concerning treatment of
6206 breakpoints with single disabled location. */
6207 if (loc == NULL
6208 && (b->loc != NULL
6209 && (b->loc->next != NULL || !b->loc->enabled)))
6210 header_of_multiple = 1;
6211 if (loc == NULL)
6212 loc = b->loc;
6213
6214 annotate_record ();
6215
6216 /* 1 */
6217 annotate_field (0);
6218 if (part_of_multiple)
6219 {
6220 char *formatted;
6221 formatted = xstrprintf ("%d.%d", b->number, loc_number);
6222 ui_out_field_string (uiout, "number", formatted);
6223 xfree (formatted);
6224 }
6225 else
6226 {
6227 ui_out_field_int (uiout, "number", b->number);
6228 }
6229
6230 /* 2 */
6231 annotate_field (1);
6232 if (part_of_multiple)
6233 ui_out_field_skip (uiout, "type");
6234 else
6235 ui_out_field_string (uiout, "type", bptype_string (b->type));
6236
6237 /* 3 */
6238 annotate_field (2);
6239 if (part_of_multiple)
6240 ui_out_field_skip (uiout, "disp");
6241 else
6242 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
6243
6244
6245 /* 4 */
6246 annotate_field (3);
6247 if (part_of_multiple)
6248 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
6249 else
6250 ui_out_field_fmt (uiout, "enabled", "%c",
6251 bpenables[(int) b->enable_state]);
6252 ui_out_spaces (uiout, 2);
6253
6254
6255 /* 5 and 6 */
6256 if (b->ops != NULL && b->ops->print_one != NULL)
6257 {
6258 /* Although the print_one can possibly print all locations,
6259 calling it here is not likely to get any nice result. So,
6260 make sure there's just one location. */
6261 gdb_assert (b->loc == NULL || b->loc->next == NULL);
6262 b->ops->print_one (b, last_loc);
6263 }
6264 else
6265 switch (b->type)
6266 {
6267 case bp_none:
6268 internal_error (__FILE__, __LINE__,
6269 _("print_one_breakpoint: bp_none encountered\n"));
6270 break;
6271
6272 case bp_watchpoint:
6273 case bp_hardware_watchpoint:
6274 case bp_read_watchpoint:
6275 case bp_access_watchpoint:
6276 {
6277 struct watchpoint *w = (struct watchpoint *) b;
6278
6279 /* Field 4, the address, is omitted (which makes the columns
6280 not line up too nicely with the headers, but the effect
6281 is relatively readable). */
6282 if (opts.addressprint)
6283 ui_out_field_skip (uiout, "addr");
6284 annotate_field (5);
6285 ui_out_field_string (uiout, "what", w->exp_string);
6286 }
6287 break;
6288
6289 case bp_breakpoint:
6290 case bp_hardware_breakpoint:
6291 case bp_single_step:
6292 case bp_until:
6293 case bp_finish:
6294 case bp_longjmp:
6295 case bp_longjmp_resume:
6296 case bp_longjmp_call_dummy:
6297 case bp_exception:
6298 case bp_exception_resume:
6299 case bp_step_resume:
6300 case bp_hp_step_resume:
6301 case bp_watchpoint_scope:
6302 case bp_call_dummy:
6303 case bp_std_terminate:
6304 case bp_shlib_event:
6305 case bp_thread_event:
6306 case bp_overlay_event:
6307 case bp_longjmp_master:
6308 case bp_std_terminate_master:
6309 case bp_exception_master:
6310 case bp_tracepoint:
6311 case bp_fast_tracepoint:
6312 case bp_static_tracepoint:
6313 case bp_dprintf:
6314 case bp_jit_event:
6315 case bp_gnu_ifunc_resolver:
6316 case bp_gnu_ifunc_resolver_return:
6317 if (opts.addressprint)
6318 {
6319 annotate_field (4);
6320 if (header_of_multiple)
6321 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
6322 else if (b->loc == NULL || loc->shlib_disabled)
6323 ui_out_field_string (uiout, "addr", "<PENDING>");
6324 else
6325 ui_out_field_core_addr (uiout, "addr",
6326 loc->gdbarch, loc->address);
6327 }
6328 annotate_field (5);
6329 if (!header_of_multiple)
6330 print_breakpoint_location (b, loc);
6331 if (b->loc)
6332 *last_loc = b->loc;
6333 break;
6334 }
6335
6336
6337 if (loc != NULL && !header_of_multiple)
6338 {
6339 struct inferior *inf;
6340 VEC(int) *inf_num = NULL;
6341 int mi_only = 1;
6342
6343 ALL_INFERIORS (inf)
6344 {
6345 if (inf->pspace == loc->pspace)
6346 VEC_safe_push (int, inf_num, inf->num);
6347 }
6348
6349 /* For backward compatibility, don't display inferiors in CLI unless
6350 there are several. Always display for MI. */
6351 if (allflag
6352 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6353 && (number_of_program_spaces () > 1
6354 || number_of_inferiors () > 1)
6355 /* LOC is for existing B, it cannot be in
6356 moribund_locations and thus having NULL OWNER. */
6357 && loc->owner->type != bp_catchpoint))
6358 mi_only = 0;
6359 output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6360 VEC_free (int, inf_num);
6361 }
6362
6363 if (!part_of_multiple)
6364 {
6365 if (b->thread != -1)
6366 {
6367 /* FIXME: This seems to be redundant and lost here; see the
6368 "stop only in" line a little further down. */
6369 ui_out_text (uiout, " thread ");
6370 ui_out_field_int (uiout, "thread", b->thread);
6371 }
6372 else if (b->task != 0)
6373 {
6374 ui_out_text (uiout, " task ");
6375 ui_out_field_int (uiout, "task", b->task);
6376 }
6377 }
6378
6379 ui_out_text (uiout, "\n");
6380
6381 if (!part_of_multiple)
6382 b->ops->print_one_detail (b, uiout);
6383
6384 if (part_of_multiple && frame_id_p (b->frame_id))
6385 {
6386 annotate_field (6);
6387 ui_out_text (uiout, "\tstop only in stack frame at ");
6388 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
6389 the frame ID. */
6390 ui_out_field_core_addr (uiout, "frame",
6391 b->gdbarch, b->frame_id.stack_addr);
6392 ui_out_text (uiout, "\n");
6393 }
6394
6395 if (!part_of_multiple && b->cond_string)
6396 {
6397 annotate_field (7);
6398 if (is_tracepoint (b))
6399 ui_out_text (uiout, "\ttrace only if ");
6400 else
6401 ui_out_text (uiout, "\tstop only if ");
6402 ui_out_field_string (uiout, "cond", b->cond_string);
6403
6404 /* Print whether the target is doing the breakpoint's condition
6405 evaluation. If GDB is doing the evaluation, don't print anything. */
6406 if (is_breakpoint (b)
6407 && breakpoint_condition_evaluation_mode ()
6408 == condition_evaluation_target)
6409 {
6410 ui_out_text (uiout, " (");
6411 ui_out_field_string (uiout, "evaluated-by",
6412 bp_condition_evaluator (b));
6413 ui_out_text (uiout, " evals)");
6414 }
6415 ui_out_text (uiout, "\n");
6416 }
6417
6418 if (!part_of_multiple && b->thread != -1)
6419 {
6420 /* FIXME should make an annotation for this. */
6421 ui_out_text (uiout, "\tstop only in thread ");
6422 ui_out_field_int (uiout, "thread", b->thread);
6423 ui_out_text (uiout, "\n");
6424 }
6425
6426 if (!part_of_multiple)
6427 {
6428 if (b->hit_count)
6429 {
6430 /* FIXME should make an annotation for this. */
6431 if (is_catchpoint (b))
6432 ui_out_text (uiout, "\tcatchpoint");
6433 else if (is_tracepoint (b))
6434 ui_out_text (uiout, "\ttracepoint");
6435 else
6436 ui_out_text (uiout, "\tbreakpoint");
6437 ui_out_text (uiout, " already hit ");
6438 ui_out_field_int (uiout, "times", b->hit_count);
6439 if (b->hit_count == 1)
6440 ui_out_text (uiout, " time\n");
6441 else
6442 ui_out_text (uiout, " times\n");
6443 }
6444 else
6445 {
6446 /* Output the count also if it is zero, but only if this is mi. */
6447 if (ui_out_is_mi_like_p (uiout))
6448 ui_out_field_int (uiout, "times", b->hit_count);
6449 }
6450 }
6451
6452 if (!part_of_multiple && b->ignore_count)
6453 {
6454 annotate_field (8);
6455 ui_out_text (uiout, "\tignore next ");
6456 ui_out_field_int (uiout, "ignore", b->ignore_count);
6457 ui_out_text (uiout, " hits\n");
6458 }
6459
6460 /* Note that an enable count of 1 corresponds to "enable once"
6461 behavior, which is reported by the combination of enablement and
6462 disposition, so we don't need to mention it here. */
6463 if (!part_of_multiple && b->enable_count > 1)
6464 {
6465 annotate_field (8);
6466 ui_out_text (uiout, "\tdisable after ");
6467 /* Tweak the wording to clarify that ignore and enable counts
6468 are distinct, and have additive effect. */
6469 if (b->ignore_count)
6470 ui_out_text (uiout, "additional ");
6471 else
6472 ui_out_text (uiout, "next ");
6473 ui_out_field_int (uiout, "enable", b->enable_count);
6474 ui_out_text (uiout, " hits\n");
6475 }
6476
6477 if (!part_of_multiple && is_tracepoint (b))
6478 {
6479 struct tracepoint *tp = (struct tracepoint *) b;
6480
6481 if (tp->traceframe_usage)
6482 {
6483 ui_out_text (uiout, "\ttrace buffer usage ");
6484 ui_out_field_int (uiout, "traceframe-usage", tp->traceframe_usage);
6485 ui_out_text (uiout, " bytes\n");
6486 }
6487 }
6488
6489 l = b->commands ? b->commands->commands : NULL;
6490 if (!part_of_multiple && l)
6491 {
6492 struct cleanup *script_chain;
6493
6494 annotate_field (9);
6495 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
6496 print_command_lines (uiout, l, 4);
6497 do_cleanups (script_chain);
6498 }
6499
6500 if (is_tracepoint (b))
6501 {
6502 struct tracepoint *t = (struct tracepoint *) b;
6503
6504 if (!part_of_multiple && t->pass_count)
6505 {
6506 annotate_field (10);
6507 ui_out_text (uiout, "\tpass count ");
6508 ui_out_field_int (uiout, "pass", t->pass_count);
6509 ui_out_text (uiout, " \n");
6510 }
6511
6512 /* Don't display it when tracepoint or tracepoint location is
6513 pending. */
6514 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6515 {
6516 annotate_field (11);
6517
6518 if (ui_out_is_mi_like_p (uiout))
6519 ui_out_field_string (uiout, "installed",
6520 loc->inserted ? "y" : "n");
6521 else
6522 {
6523 if (loc->inserted)
6524 ui_out_text (uiout, "\t");
6525 else
6526 ui_out_text (uiout, "\tnot ");
6527 ui_out_text (uiout, "installed on target\n");
6528 }
6529 }
6530 }
6531
6532 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
6533 {
6534 if (is_watchpoint (b))
6535 {
6536 struct watchpoint *w = (struct watchpoint *) b;
6537
6538 ui_out_field_string (uiout, "original-location", w->exp_string);
6539 }
6540 else if (b->addr_string)
6541 ui_out_field_string (uiout, "original-location", b->addr_string);
6542 }
6543 }
6544
6545 static void
6546 print_one_breakpoint (struct breakpoint *b,
6547 struct bp_location **last_loc,
6548 int allflag)
6549 {
6550 struct cleanup *bkpt_chain;
6551 struct ui_out *uiout = current_uiout;
6552
6553 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
6554
6555 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
6556 do_cleanups (bkpt_chain);
6557
6558 /* If this breakpoint has custom print function,
6559 it's already printed. Otherwise, print individual
6560 locations, if any. */
6561 if (b->ops == NULL || b->ops->print_one == NULL)
6562 {
6563 /* If breakpoint has a single location that is disabled, we
6564 print it as if it had several locations, since otherwise it's
6565 hard to represent "breakpoint enabled, location disabled"
6566 situation.
6567
6568 Note that while hardware watchpoints have several locations
6569 internally, that's not a property exposed to user. */
6570 if (b->loc
6571 && !is_hardware_watchpoint (b)
6572 && (b->loc->next || !b->loc->enabled))
6573 {
6574 struct bp_location *loc;
6575 int n = 1;
6576
6577 for (loc = b->loc; loc; loc = loc->next, ++n)
6578 {
6579 struct cleanup *inner2 =
6580 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
6581 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
6582 do_cleanups (inner2);
6583 }
6584 }
6585 }
6586 }
6587
6588 static int
6589 breakpoint_address_bits (struct breakpoint *b)
6590 {
6591 int print_address_bits = 0;
6592 struct bp_location *loc;
6593
6594 for (loc = b->loc; loc; loc = loc->next)
6595 {
6596 int addr_bit;
6597
6598 /* Software watchpoints that aren't watching memory don't have
6599 an address to print. */
6600 if (b->type == bp_watchpoint && loc->watchpoint_type == -1)
6601 continue;
6602
6603 addr_bit = gdbarch_addr_bit (loc->gdbarch);
6604 if (addr_bit > print_address_bits)
6605 print_address_bits = addr_bit;
6606 }
6607
6608 return print_address_bits;
6609 }
6610
6611 struct captured_breakpoint_query_args
6612 {
6613 int bnum;
6614 };
6615
6616 static int
6617 do_captured_breakpoint_query (struct ui_out *uiout, void *data)
6618 {
6619 struct captured_breakpoint_query_args *args = data;
6620 struct breakpoint *b;
6621 struct bp_location *dummy_loc = NULL;
6622
6623 ALL_BREAKPOINTS (b)
6624 {
6625 if (args->bnum == b->number)
6626 {
6627 print_one_breakpoint (b, &dummy_loc, 0);
6628 return GDB_RC_OK;
6629 }
6630 }
6631 return GDB_RC_NONE;
6632 }
6633
6634 enum gdb_rc
6635 gdb_breakpoint_query (struct ui_out *uiout, int bnum,
6636 char **error_message)
6637 {
6638 struct captured_breakpoint_query_args args;
6639
6640 args.bnum = bnum;
6641 /* For the moment we don't trust print_one_breakpoint() to not throw
6642 an error. */
6643 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
6644 error_message, RETURN_MASK_ALL) < 0)
6645 return GDB_RC_FAIL;
6646 else
6647 return GDB_RC_OK;
6648 }
6649
6650 /* Return true if this breakpoint was set by the user, false if it is
6651 internal or momentary. */
6652
6653 int
6654 user_breakpoint_p (struct breakpoint *b)
6655 {
6656 return b->number > 0;
6657 }
6658
6659 /* Print information on user settable breakpoint (watchpoint, etc)
6660 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6661 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6662 FILTER is non-NULL, call it on each breakpoint and only include the
6663 ones for which it returns non-zero. Return the total number of
6664 breakpoints listed. */
6665
6666 static int
6667 breakpoint_1 (char *args, int allflag,
6668 int (*filter) (const struct breakpoint *))
6669 {
6670 struct breakpoint *b;
6671 struct bp_location *last_loc = NULL;
6672 int nr_printable_breakpoints;
6673 struct cleanup *bkpttbl_chain;
6674 struct value_print_options opts;
6675 int print_address_bits = 0;
6676 int print_type_col_width = 14;
6677 struct ui_out *uiout = current_uiout;
6678
6679 get_user_print_options (&opts);
6680
6681 /* Compute the number of rows in the table, as well as the size
6682 required for address fields. */
6683 nr_printable_breakpoints = 0;
6684 ALL_BREAKPOINTS (b)
6685 {
6686 /* If we have a filter, only list the breakpoints it accepts. */
6687 if (filter && !filter (b))
6688 continue;
6689
6690 /* If we have an "args" string, it is a list of breakpoints to
6691 accept. Skip the others. */
6692 if (args != NULL && *args != '\0')
6693 {
6694 if (allflag && parse_and_eval_long (args) != b->number)
6695 continue;
6696 if (!allflag && !number_is_in_list (args, b->number))
6697 continue;
6698 }
6699
6700 if (allflag || user_breakpoint_p (b))
6701 {
6702 int addr_bit, type_len;
6703
6704 addr_bit = breakpoint_address_bits (b);
6705 if (addr_bit > print_address_bits)
6706 print_address_bits = addr_bit;
6707
6708 type_len = strlen (bptype_string (b->type));
6709 if (type_len > print_type_col_width)
6710 print_type_col_width = type_len;
6711
6712 nr_printable_breakpoints++;
6713 }
6714 }
6715
6716 if (opts.addressprint)
6717 bkpttbl_chain
6718 = make_cleanup_ui_out_table_begin_end (uiout, 6,
6719 nr_printable_breakpoints,
6720 "BreakpointTable");
6721 else
6722 bkpttbl_chain
6723 = make_cleanup_ui_out_table_begin_end (uiout, 5,
6724 nr_printable_breakpoints,
6725 "BreakpointTable");
6726
6727 if (nr_printable_breakpoints > 0)
6728 annotate_breakpoints_headers ();
6729 if (nr_printable_breakpoints > 0)
6730 annotate_field (0);
6731 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
6732 if (nr_printable_breakpoints > 0)
6733 annotate_field (1);
6734 ui_out_table_header (uiout, print_type_col_width, ui_left,
6735 "type", "Type"); /* 2 */
6736 if (nr_printable_breakpoints > 0)
6737 annotate_field (2);
6738 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
6739 if (nr_printable_breakpoints > 0)
6740 annotate_field (3);
6741 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
6742 if (opts.addressprint)
6743 {
6744 if (nr_printable_breakpoints > 0)
6745 annotate_field (4);
6746 if (print_address_bits <= 32)
6747 ui_out_table_header (uiout, 10, ui_left,
6748 "addr", "Address"); /* 5 */
6749 else
6750 ui_out_table_header (uiout, 18, ui_left,
6751 "addr", "Address"); /* 5 */
6752 }
6753 if (nr_printable_breakpoints > 0)
6754 annotate_field (5);
6755 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
6756 ui_out_table_body (uiout);
6757 if (nr_printable_breakpoints > 0)
6758 annotate_breakpoints_table ();
6759
6760 ALL_BREAKPOINTS (b)
6761 {
6762 QUIT;
6763 /* If we have a filter, only list the breakpoints it accepts. */
6764 if (filter && !filter (b))
6765 continue;
6766
6767 /* If we have an "args" string, it is a list of breakpoints to
6768 accept. Skip the others. */
6769
6770 if (args != NULL && *args != '\0')
6771 {
6772 if (allflag) /* maintenance info breakpoint */
6773 {
6774 if (parse_and_eval_long (args) != b->number)
6775 continue;
6776 }
6777 else /* all others */
6778 {
6779 if (!number_is_in_list (args, b->number))
6780 continue;
6781 }
6782 }
6783 /* We only print out user settable breakpoints unless the
6784 allflag is set. */
6785 if (allflag || user_breakpoint_p (b))
6786 print_one_breakpoint (b, &last_loc, allflag);
6787 }
6788
6789 do_cleanups (bkpttbl_chain);
6790
6791 if (nr_printable_breakpoints == 0)
6792 {
6793 /* If there's a filter, let the caller decide how to report
6794 empty list. */
6795 if (!filter)
6796 {
6797 if (args == NULL || *args == '\0')
6798 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
6799 else
6800 ui_out_message (uiout, 0,
6801 "No breakpoint or watchpoint matching '%s'.\n",
6802 args);
6803 }
6804 }
6805 else
6806 {
6807 if (last_loc && !server_command)
6808 set_next_address (last_loc->gdbarch, last_loc->address);
6809 }
6810
6811 /* FIXME? Should this be moved up so that it is only called when
6812 there have been breakpoints? */
6813 annotate_breakpoints_table_end ();
6814
6815 return nr_printable_breakpoints;
6816 }
6817
6818 /* Display the value of default-collect in a way that is generally
6819 compatible with the breakpoint list. */
6820
6821 static void
6822 default_collect_info (void)
6823 {
6824 struct ui_out *uiout = current_uiout;
6825
6826 /* If it has no value (which is frequently the case), say nothing; a
6827 message like "No default-collect." gets in user's face when it's
6828 not wanted. */
6829 if (!*default_collect)
6830 return;
6831
6832 /* The following phrase lines up nicely with per-tracepoint collect
6833 actions. */
6834 ui_out_text (uiout, "default collect ");
6835 ui_out_field_string (uiout, "default-collect", default_collect);
6836 ui_out_text (uiout, " \n");
6837 }
6838
6839 static void
6840 breakpoints_info (char *args, int from_tty)
6841 {
6842 breakpoint_1 (args, 0, NULL);
6843
6844 default_collect_info ();
6845 }
6846
6847 static void
6848 watchpoints_info (char *args, int from_tty)
6849 {
6850 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
6851 struct ui_out *uiout = current_uiout;
6852
6853 if (num_printed == 0)
6854 {
6855 if (args == NULL || *args == '\0')
6856 ui_out_message (uiout, 0, "No watchpoints.\n");
6857 else
6858 ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
6859 }
6860 }
6861
6862 static void
6863 maintenance_info_breakpoints (char *args, int from_tty)
6864 {
6865 breakpoint_1 (args, 1, NULL);
6866
6867 default_collect_info ();
6868 }
6869
6870 static int
6871 breakpoint_has_pc (struct breakpoint *b,
6872 struct program_space *pspace,
6873 CORE_ADDR pc, struct obj_section *section)
6874 {
6875 struct bp_location *bl = b->loc;
6876
6877 for (; bl; bl = bl->next)
6878 {
6879 if (bl->pspace == pspace
6880 && bl->address == pc
6881 && (!overlay_debugging || bl->section == section))
6882 return 1;
6883 }
6884 return 0;
6885 }
6886
6887 /* Print a message describing any user-breakpoints set at PC. This
6888 concerns with logical breakpoints, so we match program spaces, not
6889 address spaces. */
6890
6891 static void
6892 describe_other_breakpoints (struct gdbarch *gdbarch,
6893 struct program_space *pspace, CORE_ADDR pc,
6894 struct obj_section *section, int thread)
6895 {
6896 int others = 0;
6897 struct breakpoint *b;
6898
6899 ALL_BREAKPOINTS (b)
6900 others += (user_breakpoint_p (b)
6901 && breakpoint_has_pc (b, pspace, pc, section));
6902 if (others > 0)
6903 {
6904 if (others == 1)
6905 printf_filtered (_("Note: breakpoint "));
6906 else /* if (others == ???) */
6907 printf_filtered (_("Note: breakpoints "));
6908 ALL_BREAKPOINTS (b)
6909 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
6910 {
6911 others--;
6912 printf_filtered ("%d", b->number);
6913 if (b->thread == -1 && thread != -1)
6914 printf_filtered (" (all threads)");
6915 else if (b->thread != -1)
6916 printf_filtered (" (thread %d)", b->thread);
6917 printf_filtered ("%s%s ",
6918 ((b->enable_state == bp_disabled
6919 || b->enable_state == bp_call_disabled)
6920 ? " (disabled)"
6921 : ""),
6922 (others > 1) ? ","
6923 : ((others == 1) ? " and" : ""));
6924 }
6925 printf_filtered (_("also set at pc "));
6926 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
6927 printf_filtered (".\n");
6928 }
6929 }
6930 \f
6931
6932 /* Return true iff it is meaningful to use the address member of
6933 BPT. For some breakpoint types, the address member is irrelevant
6934 and it makes no sense to attempt to compare it to other addresses
6935 (or use it for any other purpose either).
6936
6937 More specifically, each of the following breakpoint types will
6938 always have a zero valued address and we don't want to mark
6939 breakpoints of any of these types to be a duplicate of an actual
6940 breakpoint at address zero:
6941
6942 bp_watchpoint
6943 bp_catchpoint
6944
6945 */
6946
6947 static int
6948 breakpoint_address_is_meaningful (struct breakpoint *bpt)
6949 {
6950 enum bptype type = bpt->type;
6951
6952 return (type != bp_watchpoint && type != bp_catchpoint);
6953 }
6954
6955 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6956 true if LOC1 and LOC2 represent the same watchpoint location. */
6957
6958 static int
6959 watchpoint_locations_match (struct bp_location *loc1,
6960 struct bp_location *loc2)
6961 {
6962 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6963 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6964
6965 /* Both of them must exist. */
6966 gdb_assert (w1 != NULL);
6967 gdb_assert (w2 != NULL);
6968
6969 /* If the target can evaluate the condition expression in hardware,
6970 then we we need to insert both watchpoints even if they are at
6971 the same place. Otherwise the watchpoint will only trigger when
6972 the condition of whichever watchpoint was inserted evaluates to
6973 true, not giving a chance for GDB to check the condition of the
6974 other watchpoint. */
6975 if ((w1->cond_exp
6976 && target_can_accel_watchpoint_condition (loc1->address,
6977 loc1->length,
6978 loc1->watchpoint_type,
6979 w1->cond_exp))
6980 || (w2->cond_exp
6981 && target_can_accel_watchpoint_condition (loc2->address,
6982 loc2->length,
6983 loc2->watchpoint_type,
6984 w2->cond_exp)))
6985 return 0;
6986
6987 /* Note that this checks the owner's type, not the location's. In
6988 case the target does not support read watchpoints, but does
6989 support access watchpoints, we'll have bp_read_watchpoint
6990 watchpoints with hw_access locations. Those should be considered
6991 duplicates of hw_read locations. The hw_read locations will
6992 become hw_access locations later. */
6993 return (loc1->owner->type == loc2->owner->type
6994 && loc1->pspace->aspace == loc2->pspace->aspace
6995 && loc1->address == loc2->address
6996 && loc1->length == loc2->length);
6997 }
6998
6999 /* See breakpoint.h. */
7000
7001 int
7002 breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
7003 struct address_space *aspace2, CORE_ADDR addr2)
7004 {
7005 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
7006 || aspace1 == aspace2)
7007 && addr1 == addr2);
7008 }
7009
7010 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
7011 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
7012 matches ASPACE2. On targets that have global breakpoints, the address
7013 space doesn't really matter. */
7014
7015 static int
7016 breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
7017 int len1, struct address_space *aspace2,
7018 CORE_ADDR addr2)
7019 {
7020 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
7021 || aspace1 == aspace2)
7022 && addr2 >= addr1 && addr2 < addr1 + len1);
7023 }
7024
7025 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
7026 a ranged breakpoint. In most targets, a match happens only if ASPACE
7027 matches the breakpoint's address space. On targets that have global
7028 breakpoints, the address space doesn't really matter. */
7029
7030 static int
7031 breakpoint_location_address_match (struct bp_location *bl,
7032 struct address_space *aspace,
7033 CORE_ADDR addr)
7034 {
7035 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
7036 aspace, addr)
7037 || (bl->length
7038 && breakpoint_address_match_range (bl->pspace->aspace,
7039 bl->address, bl->length,
7040 aspace, addr)));
7041 }
7042
7043 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
7044 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
7045 true, otherwise returns false. */
7046
7047 static int
7048 tracepoint_locations_match (struct bp_location *loc1,
7049 struct bp_location *loc2)
7050 {
7051 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
7052 /* Since tracepoint locations are never duplicated with others', tracepoint
7053 locations at the same address of different tracepoints are regarded as
7054 different locations. */
7055 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7056 else
7057 return 0;
7058 }
7059
7060 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
7061 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
7062 represent the same location. */
7063
7064 static int
7065 breakpoint_locations_match (struct bp_location *loc1,
7066 struct bp_location *loc2)
7067 {
7068 int hw_point1, hw_point2;
7069
7070 /* Both of them must not be in moribund_locations. */
7071 gdb_assert (loc1->owner != NULL);
7072 gdb_assert (loc2->owner != NULL);
7073
7074 hw_point1 = is_hardware_watchpoint (loc1->owner);
7075 hw_point2 = is_hardware_watchpoint (loc2->owner);
7076
7077 if (hw_point1 != hw_point2)
7078 return 0;
7079 else if (hw_point1)
7080 return watchpoint_locations_match (loc1, loc2);
7081 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7082 return tracepoint_locations_match (loc1, loc2);
7083 else
7084 /* We compare bp_location.length in order to cover ranged breakpoints. */
7085 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7086 loc2->pspace->aspace, loc2->address)
7087 && loc1->length == loc2->length);
7088 }
7089
7090 static void
7091 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7092 int bnum, int have_bnum)
7093 {
7094 /* The longest string possibly returned by hex_string_custom
7095 is 50 chars. These must be at least that big for safety. */
7096 char astr1[64];
7097 char astr2[64];
7098
7099 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7100 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
7101 if (have_bnum)
7102 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
7103 bnum, astr1, astr2);
7104 else
7105 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
7106 }
7107
7108 /* Adjust a breakpoint's address to account for architectural
7109 constraints on breakpoint placement. Return the adjusted address.
7110 Note: Very few targets require this kind of adjustment. For most
7111 targets, this function is simply the identity function. */
7112
7113 static CORE_ADDR
7114 adjust_breakpoint_address (struct gdbarch *gdbarch,
7115 CORE_ADDR bpaddr, enum bptype bptype)
7116 {
7117 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
7118 {
7119 /* Very few targets need any kind of breakpoint adjustment. */
7120 return bpaddr;
7121 }
7122 else if (bptype == bp_watchpoint
7123 || bptype == bp_hardware_watchpoint
7124 || bptype == bp_read_watchpoint
7125 || bptype == bp_access_watchpoint
7126 || bptype == bp_catchpoint)
7127 {
7128 /* Watchpoints and the various bp_catch_* eventpoints should not
7129 have their addresses modified. */
7130 return bpaddr;
7131 }
7132 else if (bptype == bp_single_step)
7133 {
7134 /* Single-step breakpoints should not have their addresses
7135 modified. If there's any architectural constrain that
7136 applies to this address, then it should have already been
7137 taken into account when the breakpoint was created in the
7138 first place. If we didn't do this, stepping through e.g.,
7139 Thumb-2 IT blocks would break. */
7140 return bpaddr;
7141 }
7142 else
7143 {
7144 CORE_ADDR adjusted_bpaddr;
7145
7146 /* Some targets have architectural constraints on the placement
7147 of breakpoint instructions. Obtain the adjusted address. */
7148 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
7149
7150 /* An adjusted breakpoint address can significantly alter
7151 a user's expectations. Print a warning if an adjustment
7152 is required. */
7153 if (adjusted_bpaddr != bpaddr)
7154 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7155
7156 return adjusted_bpaddr;
7157 }
7158 }
7159
7160 void
7161 init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
7162 struct breakpoint *owner)
7163 {
7164 memset (loc, 0, sizeof (*loc));
7165
7166 gdb_assert (ops != NULL);
7167
7168 loc->ops = ops;
7169 loc->owner = owner;
7170 loc->cond = NULL;
7171 loc->cond_bytecode = NULL;
7172 loc->shlib_disabled = 0;
7173 loc->enabled = 1;
7174
7175 switch (owner->type)
7176 {
7177 case bp_breakpoint:
7178 case bp_single_step:
7179 case bp_until:
7180 case bp_finish:
7181 case bp_longjmp:
7182 case bp_longjmp_resume:
7183 case bp_longjmp_call_dummy:
7184 case bp_exception:
7185 case bp_exception_resume:
7186 case bp_step_resume:
7187 case bp_hp_step_resume:
7188 case bp_watchpoint_scope:
7189 case bp_call_dummy:
7190 case bp_std_terminate:
7191 case bp_shlib_event:
7192 case bp_thread_event:
7193 case bp_overlay_event:
7194 case bp_jit_event:
7195 case bp_longjmp_master:
7196 case bp_std_terminate_master:
7197 case bp_exception_master:
7198 case bp_gnu_ifunc_resolver:
7199 case bp_gnu_ifunc_resolver_return:
7200 case bp_dprintf:
7201 loc->loc_type = bp_loc_software_breakpoint;
7202 mark_breakpoint_location_modified (loc);
7203 break;
7204 case bp_hardware_breakpoint:
7205 loc->loc_type = bp_loc_hardware_breakpoint;
7206 mark_breakpoint_location_modified (loc);
7207 break;
7208 case bp_hardware_watchpoint:
7209 case bp_read_watchpoint:
7210 case bp_access_watchpoint:
7211 loc->loc_type = bp_loc_hardware_watchpoint;
7212 break;
7213 case bp_watchpoint:
7214 case bp_catchpoint:
7215 case bp_tracepoint:
7216 case bp_fast_tracepoint:
7217 case bp_static_tracepoint:
7218 loc->loc_type = bp_loc_other;
7219 break;
7220 default:
7221 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
7222 }
7223
7224 loc->refc = 1;
7225 }
7226
7227 /* Allocate a struct bp_location. */
7228
7229 static struct bp_location *
7230 allocate_bp_location (struct breakpoint *bpt)
7231 {
7232 return bpt->ops->allocate_location (bpt);
7233 }
7234
7235 static void
7236 free_bp_location (struct bp_location *loc)
7237 {
7238 loc->ops->dtor (loc);
7239 xfree (loc);
7240 }
7241
7242 /* Increment reference count. */
7243
7244 static void
7245 incref_bp_location (struct bp_location *bl)
7246 {
7247 ++bl->refc;
7248 }
7249
7250 /* Decrement reference count. If the reference count reaches 0,
7251 destroy the bp_location. Sets *BLP to NULL. */
7252
7253 static void
7254 decref_bp_location (struct bp_location **blp)
7255 {
7256 gdb_assert ((*blp)->refc > 0);
7257
7258 if (--(*blp)->refc == 0)
7259 free_bp_location (*blp);
7260 *blp = NULL;
7261 }
7262
7263 /* Add breakpoint B at the end of the global breakpoint chain. */
7264
7265 static void
7266 add_to_breakpoint_chain (struct breakpoint *b)
7267 {
7268 struct breakpoint *b1;
7269
7270 /* Add this breakpoint to the end of the chain so that a list of
7271 breakpoints will come out in order of increasing numbers. */
7272
7273 b1 = breakpoint_chain;
7274 if (b1 == 0)
7275 breakpoint_chain = b;
7276 else
7277 {
7278 while (b1->next)
7279 b1 = b1->next;
7280 b1->next = b;
7281 }
7282 }
7283
7284 /* Initializes breakpoint B with type BPTYPE and no locations yet. */
7285
7286 static void
7287 init_raw_breakpoint_without_location (struct breakpoint *b,
7288 struct gdbarch *gdbarch,
7289 enum bptype bptype,
7290 const struct breakpoint_ops *ops)
7291 {
7292 memset (b, 0, sizeof (*b));
7293
7294 gdb_assert (ops != NULL);
7295
7296 b->ops = ops;
7297 b->type = bptype;
7298 b->gdbarch = gdbarch;
7299 b->language = current_language->la_language;
7300 b->input_radix = input_radix;
7301 b->thread = -1;
7302 b->enable_state = bp_enabled;
7303 b->next = 0;
7304 b->silent = 0;
7305 b->ignore_count = 0;
7306 b->commands = NULL;
7307 b->frame_id = null_frame_id;
7308 b->condition_not_parsed = 0;
7309 b->py_bp_object = NULL;
7310 b->related_breakpoint = b;
7311 }
7312
7313 /* Helper to set_raw_breakpoint below. Creates a breakpoint
7314 that has type BPTYPE and has no locations as yet. */
7315
7316 static struct breakpoint *
7317 set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
7318 enum bptype bptype,
7319 const struct breakpoint_ops *ops)
7320 {
7321 struct breakpoint *b = XNEW (struct breakpoint);
7322
7323 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7324 add_to_breakpoint_chain (b);
7325 return b;
7326 }
7327
7328 /* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7329 resolutions should be made as the user specified the location explicitly
7330 enough. */
7331
7332 static void
7333 set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
7334 {
7335 gdb_assert (loc->owner != NULL);
7336
7337 if (loc->owner->type == bp_breakpoint
7338 || loc->owner->type == bp_hardware_breakpoint
7339 || is_tracepoint (loc->owner))
7340 {
7341 int is_gnu_ifunc;
7342 const char *function_name;
7343 CORE_ADDR func_addr;
7344
7345 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
7346 &func_addr, NULL, &is_gnu_ifunc);
7347
7348 if (is_gnu_ifunc && !explicit_loc)
7349 {
7350 struct breakpoint *b = loc->owner;
7351
7352 gdb_assert (loc->pspace == current_program_space);
7353 if (gnu_ifunc_resolve_name (function_name,
7354 &loc->requested_address))
7355 {
7356 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7357 loc->address = adjust_breakpoint_address (loc->gdbarch,
7358 loc->requested_address,
7359 b->type);
7360 }
7361 else if (b->type == bp_breakpoint && b->loc == loc
7362 && loc->next == NULL && b->related_breakpoint == b)
7363 {
7364 /* Create only the whole new breakpoint of this type but do not
7365 mess more complicated breakpoints with multiple locations. */
7366 b->type = bp_gnu_ifunc_resolver;
7367 /* Remember the resolver's address for use by the return
7368 breakpoint. */
7369 loc->related_address = func_addr;
7370 }
7371 }
7372
7373 if (function_name)
7374 loc->function_name = xstrdup (function_name);
7375 }
7376 }
7377
7378 /* Attempt to determine architecture of location identified by SAL. */
7379 struct gdbarch *
7380 get_sal_arch (struct symtab_and_line sal)
7381 {
7382 if (sal.section)
7383 return get_objfile_arch (sal.section->objfile);
7384 if (sal.symtab)
7385 return get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
7386
7387 return NULL;
7388 }
7389
7390 /* Low level routine for partially initializing a breakpoint of type
7391 BPTYPE. The newly created breakpoint's address, section, source
7392 file name, and line number are provided by SAL.
7393
7394 It is expected that the caller will complete the initialization of
7395 the newly created breakpoint struct as well as output any status
7396 information regarding the creation of a new breakpoint. */
7397
7398 static void
7399 init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
7400 struct symtab_and_line sal, enum bptype bptype,
7401 const struct breakpoint_ops *ops)
7402 {
7403 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7404
7405 add_location_to_breakpoint (b, &sal);
7406
7407 if (bptype != bp_catchpoint)
7408 gdb_assert (sal.pspace != NULL);
7409
7410 /* Store the program space that was used to set the breakpoint,
7411 except for ordinary breakpoints, which are independent of the
7412 program space. */
7413 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7414 b->pspace = sal.pspace;
7415 }
7416
7417 /* set_raw_breakpoint is a low level routine for allocating and
7418 partially initializing a breakpoint of type BPTYPE. The newly
7419 created breakpoint's address, section, source file name, and line
7420 number are provided by SAL. The newly created and partially
7421 initialized breakpoint is added to the breakpoint chain and
7422 is also returned as the value of this function.
7423
7424 It is expected that the caller will complete the initialization of
7425 the newly created breakpoint struct as well as output any status
7426 information regarding the creation of a new breakpoint. In
7427 particular, set_raw_breakpoint does NOT set the breakpoint
7428 number! Care should be taken to not allow an error to occur
7429 prior to completing the initialization of the breakpoint. If this
7430 should happen, a bogus breakpoint will be left on the chain. */
7431
7432 struct breakpoint *
7433 set_raw_breakpoint (struct gdbarch *gdbarch,
7434 struct symtab_and_line sal, enum bptype bptype,
7435 const struct breakpoint_ops *ops)
7436 {
7437 struct breakpoint *b = XNEW (struct breakpoint);
7438
7439 init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
7440 add_to_breakpoint_chain (b);
7441 return b;
7442 }
7443
7444 /* Call this routine when stepping and nexting to enable a breakpoint
7445 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7446 initiated the operation. */
7447
7448 void
7449 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
7450 {
7451 struct breakpoint *b, *b_tmp;
7452 int thread = tp->num;
7453
7454 /* To avoid having to rescan all objfile symbols at every step,
7455 we maintain a list of continually-inserted but always disabled
7456 longjmp "master" breakpoints. Here, we simply create momentary
7457 clones of those and enable them for the requested thread. */
7458 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7459 if (b->pspace == current_program_space
7460 && (b->type == bp_longjmp_master
7461 || b->type == bp_exception_master))
7462 {
7463 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7464 struct breakpoint *clone;
7465
7466 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7467 after their removal. */
7468 clone = momentary_breakpoint_from_master (b, type,
7469 &longjmp_breakpoint_ops, 1);
7470 clone->thread = thread;
7471 }
7472
7473 tp->initiating_frame = frame;
7474 }
7475
7476 /* Delete all longjmp breakpoints from THREAD. */
7477 void
7478 delete_longjmp_breakpoint (int thread)
7479 {
7480 struct breakpoint *b, *b_tmp;
7481
7482 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7483 if (b->type == bp_longjmp || b->type == bp_exception)
7484 {
7485 if (b->thread == thread)
7486 delete_breakpoint (b);
7487 }
7488 }
7489
7490 void
7491 delete_longjmp_breakpoint_at_next_stop (int thread)
7492 {
7493 struct breakpoint *b, *b_tmp;
7494
7495 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7496 if (b->type == bp_longjmp || b->type == bp_exception)
7497 {
7498 if (b->thread == thread)
7499 b->disposition = disp_del_at_next_stop;
7500 }
7501 }
7502
7503 /* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7504 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7505 pointer to any of them. Return NULL if this system cannot place longjmp
7506 breakpoints. */
7507
7508 struct breakpoint *
7509 set_longjmp_breakpoint_for_call_dummy (void)
7510 {
7511 struct breakpoint *b, *retval = NULL;
7512
7513 ALL_BREAKPOINTS (b)
7514 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7515 {
7516 struct breakpoint *new_b;
7517
7518 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7519 &momentary_breakpoint_ops,
7520 1);
7521 new_b->thread = pid_to_thread_id (inferior_ptid);
7522
7523 /* Link NEW_B into the chain of RETVAL breakpoints. */
7524
7525 gdb_assert (new_b->related_breakpoint == new_b);
7526 if (retval == NULL)
7527 retval = new_b;
7528 new_b->related_breakpoint = retval;
7529 while (retval->related_breakpoint != new_b->related_breakpoint)
7530 retval = retval->related_breakpoint;
7531 retval->related_breakpoint = new_b;
7532 }
7533
7534 return retval;
7535 }
7536
7537 /* Verify all existing dummy frames and their associated breakpoints for
7538 TP. Remove those which can no longer be found in the current frame
7539 stack.
7540
7541 You should call this function only at places where it is safe to currently
7542 unwind the whole stack. Failed stack unwind would discard live dummy
7543 frames. */
7544
7545 void
7546 check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
7547 {
7548 struct breakpoint *b, *b_tmp;
7549
7550 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7551 if (b->type == bp_longjmp_call_dummy && b->thread == tp->num)
7552 {
7553 struct breakpoint *dummy_b = b->related_breakpoint;
7554
7555 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7556 dummy_b = dummy_b->related_breakpoint;
7557 if (dummy_b->type != bp_call_dummy
7558 || frame_find_by_id (dummy_b->frame_id) != NULL)
7559 continue;
7560
7561 dummy_frame_discard (dummy_b->frame_id, tp->ptid);
7562
7563 while (b->related_breakpoint != b)
7564 {
7565 if (b_tmp == b->related_breakpoint)
7566 b_tmp = b->related_breakpoint->next;
7567 delete_breakpoint (b->related_breakpoint);
7568 }
7569 delete_breakpoint (b);
7570 }
7571 }
7572
7573 void
7574 enable_overlay_breakpoints (void)
7575 {
7576 struct breakpoint *b;
7577
7578 ALL_BREAKPOINTS (b)
7579 if (b->type == bp_overlay_event)
7580 {
7581 b->enable_state = bp_enabled;
7582 update_global_location_list (UGLL_MAY_INSERT);
7583 overlay_events_enabled = 1;
7584 }
7585 }
7586
7587 void
7588 disable_overlay_breakpoints (void)
7589 {
7590 struct breakpoint *b;
7591
7592 ALL_BREAKPOINTS (b)
7593 if (b->type == bp_overlay_event)
7594 {
7595 b->enable_state = bp_disabled;
7596 update_global_location_list (UGLL_DONT_INSERT);
7597 overlay_events_enabled = 0;
7598 }
7599 }
7600
7601 /* Set an active std::terminate breakpoint for each std::terminate
7602 master breakpoint. */
7603 void
7604 set_std_terminate_breakpoint (void)
7605 {
7606 struct breakpoint *b, *b_tmp;
7607
7608 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7609 if (b->pspace == current_program_space
7610 && b->type == bp_std_terminate_master)
7611 {
7612 momentary_breakpoint_from_master (b, bp_std_terminate,
7613 &momentary_breakpoint_ops, 1);
7614 }
7615 }
7616
7617 /* Delete all the std::terminate breakpoints. */
7618 void
7619 delete_std_terminate_breakpoint (void)
7620 {
7621 struct breakpoint *b, *b_tmp;
7622
7623 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7624 if (b->type == bp_std_terminate)
7625 delete_breakpoint (b);
7626 }
7627
7628 struct breakpoint *
7629 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7630 {
7631 struct breakpoint *b;
7632
7633 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7634 &internal_breakpoint_ops);
7635
7636 b->enable_state = bp_enabled;
7637 /* addr_string has to be used or breakpoint_re_set will delete me. */
7638 b->addr_string
7639 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
7640
7641 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7642
7643 return b;
7644 }
7645
7646 void
7647 remove_thread_event_breakpoints (void)
7648 {
7649 struct breakpoint *b, *b_tmp;
7650
7651 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7652 if (b->type == bp_thread_event
7653 && b->loc->pspace == current_program_space)
7654 delete_breakpoint (b);
7655 }
7656
7657 struct lang_and_radix
7658 {
7659 enum language lang;
7660 int radix;
7661 };
7662
7663 /* Create a breakpoint for JIT code registration and unregistration. */
7664
7665 struct breakpoint *
7666 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7667 {
7668 struct breakpoint *b;
7669
7670 b = create_internal_breakpoint (gdbarch, address, bp_jit_event,
7671 &internal_breakpoint_ops);
7672 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7673 return b;
7674 }
7675
7676 /* Remove JIT code registration and unregistration breakpoint(s). */
7677
7678 void
7679 remove_jit_event_breakpoints (void)
7680 {
7681 struct breakpoint *b, *b_tmp;
7682
7683 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7684 if (b->type == bp_jit_event
7685 && b->loc->pspace == current_program_space)
7686 delete_breakpoint (b);
7687 }
7688
7689 void
7690 remove_solib_event_breakpoints (void)
7691 {
7692 struct breakpoint *b, *b_tmp;
7693
7694 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7695 if (b->type == bp_shlib_event
7696 && b->loc->pspace == current_program_space)
7697 delete_breakpoint (b);
7698 }
7699
7700 /* See breakpoint.h. */
7701
7702 void
7703 remove_solib_event_breakpoints_at_next_stop (void)
7704 {
7705 struct breakpoint *b, *b_tmp;
7706
7707 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7708 if (b->type == bp_shlib_event
7709 && b->loc->pspace == current_program_space)
7710 b->disposition = disp_del_at_next_stop;
7711 }
7712
7713 /* Helper for create_solib_event_breakpoint /
7714 create_and_insert_solib_event_breakpoint. Allows specifying which
7715 INSERT_MODE to pass through to update_global_location_list. */
7716
7717 static struct breakpoint *
7718 create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7719 enum ugll_insert_mode insert_mode)
7720 {
7721 struct breakpoint *b;
7722
7723 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7724 &internal_breakpoint_ops);
7725 update_global_location_list_nothrow (insert_mode);
7726 return b;
7727 }
7728
7729 struct breakpoint *
7730 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7731 {
7732 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7733 }
7734
7735 /* See breakpoint.h. */
7736
7737 struct breakpoint *
7738 create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7739 {
7740 struct breakpoint *b;
7741
7742 /* Explicitly tell update_global_location_list to insert
7743 locations. */
7744 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
7745 if (!b->loc->inserted)
7746 {
7747 delete_breakpoint (b);
7748 return NULL;
7749 }
7750 return b;
7751 }
7752
7753 /* Disable any breakpoints that are on code in shared libraries. Only
7754 apply to enabled breakpoints, disabled ones can just stay disabled. */
7755
7756 void
7757 disable_breakpoints_in_shlibs (void)
7758 {
7759 struct bp_location *loc, **locp_tmp;
7760
7761 ALL_BP_LOCATIONS (loc, locp_tmp)
7762 {
7763 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7764 struct breakpoint *b = loc->owner;
7765
7766 /* We apply the check to all breakpoints, including disabled for
7767 those with loc->duplicate set. This is so that when breakpoint
7768 becomes enabled, or the duplicate is removed, gdb will try to
7769 insert all breakpoints. If we don't set shlib_disabled here,
7770 we'll try to insert those breakpoints and fail. */
7771 if (((b->type == bp_breakpoint)
7772 || (b->type == bp_jit_event)
7773 || (b->type == bp_hardware_breakpoint)
7774 || (is_tracepoint (b)))
7775 && loc->pspace == current_program_space
7776 && !loc->shlib_disabled
7777 && solib_name_from_address (loc->pspace, loc->address)
7778 )
7779 {
7780 loc->shlib_disabled = 1;
7781 }
7782 }
7783 }
7784
7785 /* Disable any breakpoints and tracepoints that are in SOLIB upon
7786 notification of unloaded_shlib. Only apply to enabled breakpoints,
7787 disabled ones can just stay disabled. */
7788
7789 static void
7790 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7791 {
7792 struct bp_location *loc, **locp_tmp;
7793 int disabled_shlib_breaks = 0;
7794
7795 /* SunOS a.out shared libraries are always mapped, so do not
7796 disable breakpoints; they will only be reported as unloaded
7797 through clear_solib when GDB discards its shared library
7798 list. See clear_solib for more information. */
7799 if (exec_bfd != NULL
7800 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
7801 return;
7802
7803 ALL_BP_LOCATIONS (loc, locp_tmp)
7804 {
7805 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7806 struct breakpoint *b = loc->owner;
7807
7808 if (solib->pspace == loc->pspace
7809 && !loc->shlib_disabled
7810 && (((b->type == bp_breakpoint
7811 || b->type == bp_jit_event
7812 || b->type == bp_hardware_breakpoint)
7813 && (loc->loc_type == bp_loc_hardware_breakpoint
7814 || loc->loc_type == bp_loc_software_breakpoint))
7815 || is_tracepoint (b))
7816 && solib_contains_address_p (solib, loc->address))
7817 {
7818 loc->shlib_disabled = 1;
7819 /* At this point, we cannot rely on remove_breakpoint
7820 succeeding so we must mark the breakpoint as not inserted
7821 to prevent future errors occurring in remove_breakpoints. */
7822 loc->inserted = 0;
7823
7824 /* This may cause duplicate notifications for the same breakpoint. */
7825 observer_notify_breakpoint_modified (b);
7826
7827 if (!disabled_shlib_breaks)
7828 {
7829 target_terminal_ours_for_output ();
7830 warning (_("Temporarily disabling breakpoints "
7831 "for unloaded shared library \"%s\""),
7832 solib->so_name);
7833 }
7834 disabled_shlib_breaks = 1;
7835 }
7836 }
7837 }
7838
7839 /* Disable any breakpoints and tracepoints in OBJFILE upon
7840 notification of free_objfile. Only apply to enabled breakpoints,
7841 disabled ones can just stay disabled. */
7842
7843 static void
7844 disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7845 {
7846 struct breakpoint *b;
7847
7848 if (objfile == NULL)
7849 return;
7850
7851 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7852 managed by the user with add-symbol-file/remove-symbol-file.
7853 Similarly to how breakpoints in shared libraries are handled in
7854 response to "nosharedlibrary", mark breakpoints in such modules
7855 shlib_disabled so they end up uninserted on the next global
7856 location list update. Shared libraries not loaded by the user
7857 aren't handled here -- they're already handled in
7858 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7859 solib_unloaded observer. We skip objfiles that are not
7860 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7861 main objfile). */
7862 if ((objfile->flags & OBJF_SHARED) == 0
7863 || (objfile->flags & OBJF_USERLOADED) == 0)
7864 return;
7865
7866 ALL_BREAKPOINTS (b)
7867 {
7868 struct bp_location *loc;
7869 int bp_modified = 0;
7870
7871 if (!is_breakpoint (b) && !is_tracepoint (b))
7872 continue;
7873
7874 for (loc = b->loc; loc != NULL; loc = loc->next)
7875 {
7876 CORE_ADDR loc_addr = loc->address;
7877
7878 if (loc->loc_type != bp_loc_hardware_breakpoint
7879 && loc->loc_type != bp_loc_software_breakpoint)
7880 continue;
7881
7882 if (loc->shlib_disabled != 0)
7883 continue;
7884
7885 if (objfile->pspace != loc->pspace)
7886 continue;
7887
7888 if (loc->loc_type != bp_loc_hardware_breakpoint
7889 && loc->loc_type != bp_loc_software_breakpoint)
7890 continue;
7891
7892 if (is_addr_in_objfile (loc_addr, objfile))
7893 {
7894 loc->shlib_disabled = 1;
7895 /* At this point, we don't know whether the object was
7896 unmapped from the inferior or not, so leave the
7897 inserted flag alone. We'll handle failure to
7898 uninsert quietly, in case the object was indeed
7899 unmapped. */
7900
7901 mark_breakpoint_location_modified (loc);
7902
7903 bp_modified = 1;
7904 }
7905 }
7906
7907 if (bp_modified)
7908 observer_notify_breakpoint_modified (b);
7909 }
7910 }
7911
7912 /* FORK & VFORK catchpoints. */
7913
7914 /* An instance of this type is used to represent a fork or vfork
7915 catchpoint. It includes a "struct breakpoint" as a kind of base
7916 class; users downcast to "struct breakpoint *" when needed. A
7917 breakpoint is really of this type iff its ops pointer points to
7918 CATCH_FORK_BREAKPOINT_OPS. */
7919
7920 struct fork_catchpoint
7921 {
7922 /* The base class. */
7923 struct breakpoint base;
7924
7925 /* Process id of a child process whose forking triggered this
7926 catchpoint. This field is only valid immediately after this
7927 catchpoint has triggered. */
7928 ptid_t forked_inferior_pid;
7929 };
7930
7931 /* Implement the "insert" breakpoint_ops method for fork
7932 catchpoints. */
7933
7934 static int
7935 insert_catch_fork (struct bp_location *bl)
7936 {
7937 return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
7938 }
7939
7940 /* Implement the "remove" breakpoint_ops method for fork
7941 catchpoints. */
7942
7943 static int
7944 remove_catch_fork (struct bp_location *bl)
7945 {
7946 return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
7947 }
7948
7949 /* Implement the "breakpoint_hit" breakpoint_ops method for fork
7950 catchpoints. */
7951
7952 static int
7953 breakpoint_hit_catch_fork (const struct bp_location *bl,
7954 struct address_space *aspace, CORE_ADDR bp_addr,
7955 const struct target_waitstatus *ws)
7956 {
7957 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7958
7959 if (ws->kind != TARGET_WAITKIND_FORKED)
7960 return 0;
7961
7962 c->forked_inferior_pid = ws->value.related_pid;
7963 return 1;
7964 }
7965
7966 /* Implement the "print_it" breakpoint_ops method for fork
7967 catchpoints. */
7968
7969 static enum print_stop_action
7970 print_it_catch_fork (bpstat bs)
7971 {
7972 struct ui_out *uiout = current_uiout;
7973 struct breakpoint *b = bs->breakpoint_at;
7974 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
7975
7976 annotate_catchpoint (b->number);
7977 if (b->disposition == disp_del)
7978 ui_out_text (uiout, "\nTemporary catchpoint ");
7979 else
7980 ui_out_text (uiout, "\nCatchpoint ");
7981 if (ui_out_is_mi_like_p (uiout))
7982 {
7983 ui_out_field_string (uiout, "reason",
7984 async_reason_lookup (EXEC_ASYNC_FORK));
7985 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7986 }
7987 ui_out_field_int (uiout, "bkptno", b->number);
7988 ui_out_text (uiout, " (forked process ");
7989 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
7990 ui_out_text (uiout, "), ");
7991 return PRINT_SRC_AND_LOC;
7992 }
7993
7994 /* Implement the "print_one" breakpoint_ops method for fork
7995 catchpoints. */
7996
7997 static void
7998 print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
7999 {
8000 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8001 struct value_print_options opts;
8002 struct ui_out *uiout = current_uiout;
8003
8004 get_user_print_options (&opts);
8005
8006 /* Field 4, the address, is omitted (which makes the columns not
8007 line up too nicely with the headers, but the effect is relatively
8008 readable). */
8009 if (opts.addressprint)
8010 ui_out_field_skip (uiout, "addr");
8011 annotate_field (5);
8012 ui_out_text (uiout, "fork");
8013 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
8014 {
8015 ui_out_text (uiout, ", process ");
8016 ui_out_field_int (uiout, "what",
8017 ptid_get_pid (c->forked_inferior_pid));
8018 ui_out_spaces (uiout, 1);
8019 }
8020
8021 if (ui_out_is_mi_like_p (uiout))
8022 ui_out_field_string (uiout, "catch-type", "fork");
8023 }
8024
8025 /* Implement the "print_mention" breakpoint_ops method for fork
8026 catchpoints. */
8027
8028 static void
8029 print_mention_catch_fork (struct breakpoint *b)
8030 {
8031 printf_filtered (_("Catchpoint %d (fork)"), b->number);
8032 }
8033
8034 /* Implement the "print_recreate" breakpoint_ops method for fork
8035 catchpoints. */
8036
8037 static void
8038 print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
8039 {
8040 fprintf_unfiltered (fp, "catch fork");
8041 print_recreate_thread (b, fp);
8042 }
8043
8044 /* The breakpoint_ops structure to be used in fork catchpoints. */
8045
8046 static struct breakpoint_ops catch_fork_breakpoint_ops;
8047
8048 /* Implement the "insert" breakpoint_ops method for vfork
8049 catchpoints. */
8050
8051 static int
8052 insert_catch_vfork (struct bp_location *bl)
8053 {
8054 return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
8055 }
8056
8057 /* Implement the "remove" breakpoint_ops method for vfork
8058 catchpoints. */
8059
8060 static int
8061 remove_catch_vfork (struct bp_location *bl)
8062 {
8063 return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
8064 }
8065
8066 /* Implement the "breakpoint_hit" breakpoint_ops method for vfork
8067 catchpoints. */
8068
8069 static int
8070 breakpoint_hit_catch_vfork (const struct bp_location *bl,
8071 struct address_space *aspace, CORE_ADDR bp_addr,
8072 const struct target_waitstatus *ws)
8073 {
8074 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8075
8076 if (ws->kind != TARGET_WAITKIND_VFORKED)
8077 return 0;
8078
8079 c->forked_inferior_pid = ws->value.related_pid;
8080 return 1;
8081 }
8082
8083 /* Implement the "print_it" breakpoint_ops method for vfork
8084 catchpoints. */
8085
8086 static enum print_stop_action
8087 print_it_catch_vfork (bpstat bs)
8088 {
8089 struct ui_out *uiout = current_uiout;
8090 struct breakpoint *b = bs->breakpoint_at;
8091 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8092
8093 annotate_catchpoint (b->number);
8094 if (b->disposition == disp_del)
8095 ui_out_text (uiout, "\nTemporary catchpoint ");
8096 else
8097 ui_out_text (uiout, "\nCatchpoint ");
8098 if (ui_out_is_mi_like_p (uiout))
8099 {
8100 ui_out_field_string (uiout, "reason",
8101 async_reason_lookup (EXEC_ASYNC_VFORK));
8102 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8103 }
8104 ui_out_field_int (uiout, "bkptno", b->number);
8105 ui_out_text (uiout, " (vforked process ");
8106 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
8107 ui_out_text (uiout, "), ");
8108 return PRINT_SRC_AND_LOC;
8109 }
8110
8111 /* Implement the "print_one" breakpoint_ops method for vfork
8112 catchpoints. */
8113
8114 static void
8115 print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
8116 {
8117 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8118 struct value_print_options opts;
8119 struct ui_out *uiout = current_uiout;
8120
8121 get_user_print_options (&opts);
8122 /* Field 4, the address, is omitted (which makes the columns not
8123 line up too nicely with the headers, but the effect is relatively
8124 readable). */
8125 if (opts.addressprint)
8126 ui_out_field_skip (uiout, "addr");
8127 annotate_field (5);
8128 ui_out_text (uiout, "vfork");
8129 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
8130 {
8131 ui_out_text (uiout, ", process ");
8132 ui_out_field_int (uiout, "what",
8133 ptid_get_pid (c->forked_inferior_pid));
8134 ui_out_spaces (uiout, 1);
8135 }
8136
8137 if (ui_out_is_mi_like_p (uiout))
8138 ui_out_field_string (uiout, "catch-type", "vfork");
8139 }
8140
8141 /* Implement the "print_mention" breakpoint_ops method for vfork
8142 catchpoints. */
8143
8144 static void
8145 print_mention_catch_vfork (struct breakpoint *b)
8146 {
8147 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
8148 }
8149
8150 /* Implement the "print_recreate" breakpoint_ops method for vfork
8151 catchpoints. */
8152
8153 static void
8154 print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8155 {
8156 fprintf_unfiltered (fp, "catch vfork");
8157 print_recreate_thread (b, fp);
8158 }
8159
8160 /* The breakpoint_ops structure to be used in vfork catchpoints. */
8161
8162 static struct breakpoint_ops catch_vfork_breakpoint_ops;
8163
8164 /* An instance of this type is used to represent an solib catchpoint.
8165 It includes a "struct breakpoint" as a kind of base class; users
8166 downcast to "struct breakpoint *" when needed. A breakpoint is
8167 really of this type iff its ops pointer points to
8168 CATCH_SOLIB_BREAKPOINT_OPS. */
8169
8170 struct solib_catchpoint
8171 {
8172 /* The base class. */
8173 struct breakpoint base;
8174
8175 /* True for "catch load", false for "catch unload". */
8176 unsigned char is_load;
8177
8178 /* Regular expression to match, if any. COMPILED is only valid when
8179 REGEX is non-NULL. */
8180 char *regex;
8181 regex_t compiled;
8182 };
8183
8184 static void
8185 dtor_catch_solib (struct breakpoint *b)
8186 {
8187 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8188
8189 if (self->regex)
8190 regfree (&self->compiled);
8191 xfree (self->regex);
8192
8193 base_breakpoint_ops.dtor (b);
8194 }
8195
8196 static int
8197 insert_catch_solib (struct bp_location *ignore)
8198 {
8199 return 0;
8200 }
8201
8202 static int
8203 remove_catch_solib (struct bp_location *ignore)
8204 {
8205 return 0;
8206 }
8207
8208 static int
8209 breakpoint_hit_catch_solib (const struct bp_location *bl,
8210 struct address_space *aspace,
8211 CORE_ADDR bp_addr,
8212 const struct target_waitstatus *ws)
8213 {
8214 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8215 struct breakpoint *other;
8216
8217 if (ws->kind == TARGET_WAITKIND_LOADED)
8218 return 1;
8219
8220 ALL_BREAKPOINTS (other)
8221 {
8222 struct bp_location *other_bl;
8223
8224 if (other == bl->owner)
8225 continue;
8226
8227 if (other->type != bp_shlib_event)
8228 continue;
8229
8230 if (self->base.pspace != NULL && other->pspace != self->base.pspace)
8231 continue;
8232
8233 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8234 {
8235 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8236 return 1;
8237 }
8238 }
8239
8240 return 0;
8241 }
8242
8243 static void
8244 check_status_catch_solib (struct bpstats *bs)
8245 {
8246 struct solib_catchpoint *self
8247 = (struct solib_catchpoint *) bs->breakpoint_at;
8248 int ix;
8249
8250 if (self->is_load)
8251 {
8252 struct so_list *iter;
8253
8254 for (ix = 0;
8255 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
8256 ix, iter);
8257 ++ix)
8258 {
8259 if (!self->regex
8260 || regexec (&self->compiled, iter->so_name, 0, NULL, 0) == 0)
8261 return;
8262 }
8263 }
8264 else
8265 {
8266 char *iter;
8267
8268 for (ix = 0;
8269 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
8270 ix, iter);
8271 ++ix)
8272 {
8273 if (!self->regex
8274 || regexec (&self->compiled, iter, 0, NULL, 0) == 0)
8275 return;
8276 }
8277 }
8278
8279 bs->stop = 0;
8280 bs->print_it = print_it_noop;
8281 }
8282
8283 static enum print_stop_action
8284 print_it_catch_solib (bpstat bs)
8285 {
8286 struct breakpoint *b = bs->breakpoint_at;
8287 struct ui_out *uiout = current_uiout;
8288
8289 annotate_catchpoint (b->number);
8290 if (b->disposition == disp_del)
8291 ui_out_text (uiout, "\nTemporary catchpoint ");
8292 else
8293 ui_out_text (uiout, "\nCatchpoint ");
8294 ui_out_field_int (uiout, "bkptno", b->number);
8295 ui_out_text (uiout, "\n");
8296 if (ui_out_is_mi_like_p (uiout))
8297 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8298 print_solib_event (1);
8299 return PRINT_SRC_AND_LOC;
8300 }
8301
8302 static void
8303 print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8304 {
8305 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8306 struct value_print_options opts;
8307 struct ui_out *uiout = current_uiout;
8308 char *msg;
8309
8310 get_user_print_options (&opts);
8311 /* Field 4, the address, is omitted (which makes the columns not
8312 line up too nicely with the headers, but the effect is relatively
8313 readable). */
8314 if (opts.addressprint)
8315 {
8316 annotate_field (4);
8317 ui_out_field_skip (uiout, "addr");
8318 }
8319
8320 annotate_field (5);
8321 if (self->is_load)
8322 {
8323 if (self->regex)
8324 msg = xstrprintf (_("load of library matching %s"), self->regex);
8325 else
8326 msg = xstrdup (_("load of library"));
8327 }
8328 else
8329 {
8330 if (self->regex)
8331 msg = xstrprintf (_("unload of library matching %s"), self->regex);
8332 else
8333 msg = xstrdup (_("unload of library"));
8334 }
8335 ui_out_field_string (uiout, "what", msg);
8336 xfree (msg);
8337
8338 if (ui_out_is_mi_like_p (uiout))
8339 ui_out_field_string (uiout, "catch-type",
8340 self->is_load ? "load" : "unload");
8341 }
8342
8343 static void
8344 print_mention_catch_solib (struct breakpoint *b)
8345 {
8346 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8347
8348 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8349 self->is_load ? "load" : "unload");
8350 }
8351
8352 static void
8353 print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8354 {
8355 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8356
8357 fprintf_unfiltered (fp, "%s %s",
8358 b->disposition == disp_del ? "tcatch" : "catch",
8359 self->is_load ? "load" : "unload");
8360 if (self->regex)
8361 fprintf_unfiltered (fp, " %s", self->regex);
8362 fprintf_unfiltered (fp, "\n");
8363 }
8364
8365 static struct breakpoint_ops catch_solib_breakpoint_ops;
8366
8367 /* Shared helper function (MI and CLI) for creating and installing
8368 a shared object event catchpoint. If IS_LOAD is non-zero then
8369 the events to be caught are load events, otherwise they are
8370 unload events. If IS_TEMP is non-zero the catchpoint is a
8371 temporary one. If ENABLED is non-zero the catchpoint is
8372 created in an enabled state. */
8373
8374 void
8375 add_solib_catchpoint (char *arg, int is_load, int is_temp, int enabled)
8376 {
8377 struct solib_catchpoint *c;
8378 struct gdbarch *gdbarch = get_current_arch ();
8379 struct cleanup *cleanup;
8380
8381 if (!arg)
8382 arg = "";
8383 arg = skip_spaces (arg);
8384
8385 c = XCNEW (struct solib_catchpoint);
8386 cleanup = make_cleanup (xfree, c);
8387
8388 if (*arg != '\0')
8389 {
8390 int errcode;
8391
8392 errcode = regcomp (&c->compiled, arg, REG_NOSUB);
8393 if (errcode != 0)
8394 {
8395 char *err = get_regcomp_error (errcode, &c->compiled);
8396
8397 make_cleanup (xfree, err);
8398 error (_("Invalid regexp (%s): %s"), err, arg);
8399 }
8400 c->regex = xstrdup (arg);
8401 }
8402
8403 c->is_load = is_load;
8404 init_catchpoint (&c->base, gdbarch, is_temp, NULL,
8405 &catch_solib_breakpoint_ops);
8406
8407 c->base.enable_state = enabled ? bp_enabled : bp_disabled;
8408
8409 discard_cleanups (cleanup);
8410 install_breakpoint (0, &c->base, 1);
8411 }
8412
8413 /* A helper function that does all the work for "catch load" and
8414 "catch unload". */
8415
8416 static void
8417 catch_load_or_unload (char *arg, int from_tty, int is_load,
8418 struct cmd_list_element *command)
8419 {
8420 int tempflag;
8421 const int enabled = 1;
8422
8423 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8424
8425 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8426 }
8427
8428 static void
8429 catch_load_command_1 (char *arg, int from_tty,
8430 struct cmd_list_element *command)
8431 {
8432 catch_load_or_unload (arg, from_tty, 1, command);
8433 }
8434
8435 static void
8436 catch_unload_command_1 (char *arg, int from_tty,
8437 struct cmd_list_element *command)
8438 {
8439 catch_load_or_unload (arg, from_tty, 0, command);
8440 }
8441
8442 /* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8443 is non-zero, then make the breakpoint temporary. If COND_STRING is
8444 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8445 the breakpoint_ops structure associated to the catchpoint. */
8446
8447 void
8448 init_catchpoint (struct breakpoint *b,
8449 struct gdbarch *gdbarch, int tempflag,
8450 char *cond_string,
8451 const struct breakpoint_ops *ops)
8452 {
8453 struct symtab_and_line sal;
8454
8455 init_sal (&sal);
8456 sal.pspace = current_program_space;
8457
8458 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
8459
8460 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
8461 b->disposition = tempflag ? disp_del : disp_donttouch;
8462 }
8463
8464 void
8465 install_breakpoint (int internal, struct breakpoint *b, int update_gll)
8466 {
8467 add_to_breakpoint_chain (b);
8468 set_breakpoint_number (internal, b);
8469 if (is_tracepoint (b))
8470 set_tracepoint_count (breakpoint_count);
8471 if (!internal)
8472 mention (b);
8473 observer_notify_breakpoint_created (b);
8474
8475 if (update_gll)
8476 update_global_location_list (UGLL_MAY_INSERT);
8477 }
8478
8479 static void
8480 create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8481 int tempflag, char *cond_string,
8482 const struct breakpoint_ops *ops)
8483 {
8484 struct fork_catchpoint *c = XNEW (struct fork_catchpoint);
8485
8486 init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
8487
8488 c->forked_inferior_pid = null_ptid;
8489
8490 install_breakpoint (0, &c->base, 1);
8491 }
8492
8493 /* Exec catchpoints. */
8494
8495 /* An instance of this type is used to represent an exec catchpoint.
8496 It includes a "struct breakpoint" as a kind of base class; users
8497 downcast to "struct breakpoint *" when needed. A breakpoint is
8498 really of this type iff its ops pointer points to
8499 CATCH_EXEC_BREAKPOINT_OPS. */
8500
8501 struct exec_catchpoint
8502 {
8503 /* The base class. */
8504 struct breakpoint base;
8505
8506 /* Filename of a program whose exec triggered this catchpoint.
8507 This field is only valid immediately after this catchpoint has
8508 triggered. */
8509 char *exec_pathname;
8510 };
8511
8512 /* Implement the "dtor" breakpoint_ops method for exec
8513 catchpoints. */
8514
8515 static void
8516 dtor_catch_exec (struct breakpoint *b)
8517 {
8518 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8519
8520 xfree (c->exec_pathname);
8521
8522 base_breakpoint_ops.dtor (b);
8523 }
8524
8525 static int
8526 insert_catch_exec (struct bp_location *bl)
8527 {
8528 return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
8529 }
8530
8531 static int
8532 remove_catch_exec (struct bp_location *bl)
8533 {
8534 return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
8535 }
8536
8537 static int
8538 breakpoint_hit_catch_exec (const struct bp_location *bl,
8539 struct address_space *aspace, CORE_ADDR bp_addr,
8540 const struct target_waitstatus *ws)
8541 {
8542 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8543
8544 if (ws->kind != TARGET_WAITKIND_EXECD)
8545 return 0;
8546
8547 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8548 return 1;
8549 }
8550
8551 static enum print_stop_action
8552 print_it_catch_exec (bpstat bs)
8553 {
8554 struct ui_out *uiout = current_uiout;
8555 struct breakpoint *b = bs->breakpoint_at;
8556 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8557
8558 annotate_catchpoint (b->number);
8559 if (b->disposition == disp_del)
8560 ui_out_text (uiout, "\nTemporary catchpoint ");
8561 else
8562 ui_out_text (uiout, "\nCatchpoint ");
8563 if (ui_out_is_mi_like_p (uiout))
8564 {
8565 ui_out_field_string (uiout, "reason",
8566 async_reason_lookup (EXEC_ASYNC_EXEC));
8567 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8568 }
8569 ui_out_field_int (uiout, "bkptno", b->number);
8570 ui_out_text (uiout, " (exec'd ");
8571 ui_out_field_string (uiout, "new-exec", c->exec_pathname);
8572 ui_out_text (uiout, "), ");
8573
8574 return PRINT_SRC_AND_LOC;
8575 }
8576
8577 static void
8578 print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
8579 {
8580 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8581 struct value_print_options opts;
8582 struct ui_out *uiout = current_uiout;
8583
8584 get_user_print_options (&opts);
8585
8586 /* Field 4, the address, is omitted (which makes the columns
8587 not line up too nicely with the headers, but the effect
8588 is relatively readable). */
8589 if (opts.addressprint)
8590 ui_out_field_skip (uiout, "addr");
8591 annotate_field (5);
8592 ui_out_text (uiout, "exec");
8593 if (c->exec_pathname != NULL)
8594 {
8595 ui_out_text (uiout, ", program \"");
8596 ui_out_field_string (uiout, "what", c->exec_pathname);
8597 ui_out_text (uiout, "\" ");
8598 }
8599
8600 if (ui_out_is_mi_like_p (uiout))
8601 ui_out_field_string (uiout, "catch-type", "exec");
8602 }
8603
8604 static void
8605 print_mention_catch_exec (struct breakpoint *b)
8606 {
8607 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8608 }
8609
8610 /* Implement the "print_recreate" breakpoint_ops method for exec
8611 catchpoints. */
8612
8613 static void
8614 print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8615 {
8616 fprintf_unfiltered (fp, "catch exec");
8617 print_recreate_thread (b, fp);
8618 }
8619
8620 static struct breakpoint_ops catch_exec_breakpoint_ops;
8621
8622 static int
8623 hw_breakpoint_used_count (void)
8624 {
8625 int i = 0;
8626 struct breakpoint *b;
8627 struct bp_location *bl;
8628
8629 ALL_BREAKPOINTS (b)
8630 {
8631 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
8632 for (bl = b->loc; bl; bl = bl->next)
8633 {
8634 /* Special types of hardware breakpoints may use more than
8635 one register. */
8636 i += b->ops->resources_needed (bl);
8637 }
8638 }
8639
8640 return i;
8641 }
8642
8643 /* Returns the resources B would use if it were a hardware
8644 watchpoint. */
8645
8646 static int
8647 hw_watchpoint_use_count (struct breakpoint *b)
8648 {
8649 int i = 0;
8650 struct bp_location *bl;
8651
8652 if (!breakpoint_enabled (b))
8653 return 0;
8654
8655 for (bl = b->loc; bl; bl = bl->next)
8656 {
8657 /* Special types of hardware watchpoints may use more than
8658 one register. */
8659 i += b->ops->resources_needed (bl);
8660 }
8661
8662 return i;
8663 }
8664
8665 /* Returns the sum the used resources of all hardware watchpoints of
8666 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8667 the sum of the used resources of all hardware watchpoints of other
8668 types _not_ TYPE. */
8669
8670 static int
8671 hw_watchpoint_used_count_others (struct breakpoint *except,
8672 enum bptype type, int *other_type_used)
8673 {
8674 int i = 0;
8675 struct breakpoint *b;
8676
8677 *other_type_used = 0;
8678 ALL_BREAKPOINTS (b)
8679 {
8680 if (b == except)
8681 continue;
8682 if (!breakpoint_enabled (b))
8683 continue;
8684
8685 if (b->type == type)
8686 i += hw_watchpoint_use_count (b);
8687 else if (is_hardware_watchpoint (b))
8688 *other_type_used = 1;
8689 }
8690
8691 return i;
8692 }
8693
8694 void
8695 disable_watchpoints_before_interactive_call_start (void)
8696 {
8697 struct breakpoint *b;
8698
8699 ALL_BREAKPOINTS (b)
8700 {
8701 if (is_watchpoint (b) && breakpoint_enabled (b))
8702 {
8703 b->enable_state = bp_call_disabled;
8704 update_global_location_list (UGLL_DONT_INSERT);
8705 }
8706 }
8707 }
8708
8709 void
8710 enable_watchpoints_after_interactive_call_stop (void)
8711 {
8712 struct breakpoint *b;
8713
8714 ALL_BREAKPOINTS (b)
8715 {
8716 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
8717 {
8718 b->enable_state = bp_enabled;
8719 update_global_location_list (UGLL_MAY_INSERT);
8720 }
8721 }
8722 }
8723
8724 void
8725 disable_breakpoints_before_startup (void)
8726 {
8727 current_program_space->executing_startup = 1;
8728 update_global_location_list (UGLL_DONT_INSERT);
8729 }
8730
8731 void
8732 enable_breakpoints_after_startup (void)
8733 {
8734 current_program_space->executing_startup = 0;
8735 breakpoint_re_set ();
8736 }
8737
8738 /* Create a new single-step breakpoint for thread THREAD, with no
8739 locations. */
8740
8741 static struct breakpoint *
8742 new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8743 {
8744 struct breakpoint *b = XNEW (struct breakpoint);
8745
8746 init_raw_breakpoint_without_location (b, gdbarch, bp_single_step,
8747 &momentary_breakpoint_ops);
8748
8749 b->disposition = disp_donttouch;
8750 b->frame_id = null_frame_id;
8751
8752 b->thread = thread;
8753 gdb_assert (b->thread != 0);
8754
8755 add_to_breakpoint_chain (b);
8756
8757 return b;
8758 }
8759
8760 /* Set a momentary breakpoint of type TYPE at address specified by
8761 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8762 frame. */
8763
8764 struct breakpoint *
8765 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8766 struct frame_id frame_id, enum bptype type)
8767 {
8768 struct breakpoint *b;
8769
8770 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8771 tail-called one. */
8772 gdb_assert (!frame_id_artificial_p (frame_id));
8773
8774 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
8775 b->enable_state = bp_enabled;
8776 b->disposition = disp_donttouch;
8777 b->frame_id = frame_id;
8778
8779 /* If we're debugging a multi-threaded program, then we want
8780 momentary breakpoints to be active in only a single thread of
8781 control. */
8782 if (in_thread_list (inferior_ptid))
8783 b->thread = pid_to_thread_id (inferior_ptid);
8784
8785 update_global_location_list_nothrow (UGLL_MAY_INSERT);
8786
8787 return b;
8788 }
8789
8790 /* Make a momentary breakpoint based on the master breakpoint ORIG.
8791 The new breakpoint will have type TYPE, use OPS as its
8792 breakpoint_ops, and will set enabled to LOC_ENABLED. */
8793
8794 static struct breakpoint *
8795 momentary_breakpoint_from_master (struct breakpoint *orig,
8796 enum bptype type,
8797 const struct breakpoint_ops *ops,
8798 int loc_enabled)
8799 {
8800 struct breakpoint *copy;
8801
8802 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
8803 copy->loc = allocate_bp_location (copy);
8804 set_breakpoint_location_function (copy->loc, 1);
8805
8806 copy->loc->gdbarch = orig->loc->gdbarch;
8807 copy->loc->requested_address = orig->loc->requested_address;
8808 copy->loc->address = orig->loc->address;
8809 copy->loc->section = orig->loc->section;
8810 copy->loc->pspace = orig->loc->pspace;
8811 copy->loc->probe = orig->loc->probe;
8812 copy->loc->line_number = orig->loc->line_number;
8813 copy->loc->symtab = orig->loc->symtab;
8814 copy->loc->enabled = loc_enabled;
8815 copy->frame_id = orig->frame_id;
8816 copy->thread = orig->thread;
8817 copy->pspace = orig->pspace;
8818
8819 copy->enable_state = bp_enabled;
8820 copy->disposition = disp_donttouch;
8821 copy->number = internal_breakpoint_number--;
8822
8823 update_global_location_list_nothrow (UGLL_DONT_INSERT);
8824 return copy;
8825 }
8826
8827 /* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8828 ORIG is NULL. */
8829
8830 struct breakpoint *
8831 clone_momentary_breakpoint (struct breakpoint *orig)
8832 {
8833 /* If there's nothing to clone, then return nothing. */
8834 if (orig == NULL)
8835 return NULL;
8836
8837 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
8838 }
8839
8840 struct breakpoint *
8841 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8842 enum bptype type)
8843 {
8844 struct symtab_and_line sal;
8845
8846 sal = find_pc_line (pc, 0);
8847 sal.pc = pc;
8848 sal.section = find_pc_overlay (pc);
8849 sal.explicit_pc = 1;
8850
8851 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
8852 }
8853 \f
8854
8855 /* Tell the user we have just set a breakpoint B. */
8856
8857 static void
8858 mention (struct breakpoint *b)
8859 {
8860 b->ops->print_mention (b);
8861 if (ui_out_is_mi_like_p (current_uiout))
8862 return;
8863 printf_filtered ("\n");
8864 }
8865 \f
8866
8867 static int bp_loc_is_permanent (struct bp_location *loc);
8868
8869 static struct bp_location *
8870 add_location_to_breakpoint (struct breakpoint *b,
8871 const struct symtab_and_line *sal)
8872 {
8873 struct bp_location *loc, **tmp;
8874 CORE_ADDR adjusted_address;
8875 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8876
8877 if (loc_gdbarch == NULL)
8878 loc_gdbarch = b->gdbarch;
8879
8880 /* Adjust the breakpoint's address prior to allocating a location.
8881 Once we call allocate_bp_location(), that mostly uninitialized
8882 location will be placed on the location chain. Adjustment of the
8883 breakpoint may cause target_read_memory() to be called and we do
8884 not want its scan of the location chain to find a breakpoint and
8885 location that's only been partially initialized. */
8886 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8887 sal->pc, b->type);
8888
8889 /* Sort the locations by their ADDRESS. */
8890 loc = allocate_bp_location (b);
8891 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8892 tmp = &((*tmp)->next))
8893 ;
8894 loc->next = *tmp;
8895 *tmp = loc;
8896
8897 loc->requested_address = sal->pc;
8898 loc->address = adjusted_address;
8899 loc->pspace = sal->pspace;
8900 loc->probe.probe = sal->probe;
8901 loc->probe.objfile = sal->objfile;
8902 gdb_assert (loc->pspace != NULL);
8903 loc->section = sal->section;
8904 loc->gdbarch = loc_gdbarch;
8905 loc->line_number = sal->line;
8906 loc->symtab = sal->symtab;
8907
8908 set_breakpoint_location_function (loc,
8909 sal->explicit_pc || sal->explicit_line);
8910
8911 /* While by definition, permanent breakpoints are already present in the
8912 code, we don't mark the location as inserted. Normally one would expect
8913 that GDB could rely on that breakpoint instruction to stop the program,
8914 thus removing the need to insert its own breakpoint, except that executing
8915 the breakpoint instruction can kill the target instead of reporting a
8916 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8917 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8918 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8919 breakpoint be inserted normally results in QEMU knowing about the GDB
8920 breakpoint, and thus trap before the breakpoint instruction is executed.
8921 (If GDB later needs to continue execution past the permanent breakpoint,
8922 it manually increments the PC, thus avoiding executing the breakpoint
8923 instruction.) */
8924 if (bp_loc_is_permanent (loc))
8925 loc->permanent = 1;
8926
8927 return loc;
8928 }
8929 \f
8930
8931 /* See breakpoint.h. */
8932
8933 int
8934 program_breakpoint_here_p (struct gdbarch *gdbarch, CORE_ADDR address)
8935 {
8936 int len;
8937 CORE_ADDR addr;
8938 const gdb_byte *bpoint;
8939 gdb_byte *target_mem;
8940 struct cleanup *cleanup;
8941 int retval = 0;
8942
8943 addr = address;
8944 bpoint = gdbarch_breakpoint_from_pc (gdbarch, &addr, &len);
8945
8946 /* Software breakpoints unsupported? */
8947 if (bpoint == NULL)
8948 return 0;
8949
8950 target_mem = alloca (len);
8951
8952 /* Enable the automatic memory restoration from breakpoints while
8953 we read the memory. Otherwise we could say about our temporary
8954 breakpoints they are permanent. */
8955 cleanup = make_show_memory_breakpoints_cleanup (0);
8956
8957 if (target_read_memory (address, target_mem, len) == 0
8958 && memcmp (target_mem, bpoint, len) == 0)
8959 retval = 1;
8960
8961 do_cleanups (cleanup);
8962
8963 return retval;
8964 }
8965
8966 /* Return 1 if LOC is pointing to a permanent breakpoint,
8967 return 0 otherwise. */
8968
8969 static int
8970 bp_loc_is_permanent (struct bp_location *loc)
8971 {
8972 struct cleanup *cleanup;
8973 int retval;
8974
8975 gdb_assert (loc != NULL);
8976
8977 cleanup = save_current_space_and_thread ();
8978 switch_to_program_space_and_thread (loc->pspace);
8979
8980 retval = program_breakpoint_here_p (loc->gdbarch, loc->address);
8981
8982 do_cleanups (cleanup);
8983
8984 return retval;
8985 }
8986
8987 /* Build a command list for the dprintf corresponding to the current
8988 settings of the dprintf style options. */
8989
8990 static void
8991 update_dprintf_command_list (struct breakpoint *b)
8992 {
8993 char *dprintf_args = b->extra_string;
8994 char *printf_line = NULL;
8995
8996 if (!dprintf_args)
8997 return;
8998
8999 dprintf_args = skip_spaces (dprintf_args);
9000
9001 /* Allow a comma, as it may have terminated a location, but don't
9002 insist on it. */
9003 if (*dprintf_args == ',')
9004 ++dprintf_args;
9005 dprintf_args = skip_spaces (dprintf_args);
9006
9007 if (*dprintf_args != '"')
9008 error (_("Bad format string, missing '\"'."));
9009
9010 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
9011 printf_line = xstrprintf ("printf %s", dprintf_args);
9012 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
9013 {
9014 if (!dprintf_function)
9015 error (_("No function supplied for dprintf call"));
9016
9017 if (dprintf_channel && strlen (dprintf_channel) > 0)
9018 printf_line = xstrprintf ("call (void) %s (%s,%s)",
9019 dprintf_function,
9020 dprintf_channel,
9021 dprintf_args);
9022 else
9023 printf_line = xstrprintf ("call (void) %s (%s)",
9024 dprintf_function,
9025 dprintf_args);
9026 }
9027 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
9028 {
9029 if (target_can_run_breakpoint_commands ())
9030 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
9031 else
9032 {
9033 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
9034 printf_line = xstrprintf ("printf %s", dprintf_args);
9035 }
9036 }
9037 else
9038 internal_error (__FILE__, __LINE__,
9039 _("Invalid dprintf style."));
9040
9041 gdb_assert (printf_line != NULL);
9042 /* Manufacture a printf sequence. */
9043 {
9044 struct command_line *printf_cmd_line
9045 = xmalloc (sizeof (struct command_line));
9046
9047 printf_cmd_line->control_type = simple_control;
9048 printf_cmd_line->body_count = 0;
9049 printf_cmd_line->body_list = NULL;
9050 printf_cmd_line->next = NULL;
9051 printf_cmd_line->line = printf_line;
9052
9053 breakpoint_set_commands (b, printf_cmd_line);
9054 }
9055 }
9056
9057 /* Update all dprintf commands, making their command lists reflect
9058 current style settings. */
9059
9060 static void
9061 update_dprintf_commands (char *args, int from_tty,
9062 struct cmd_list_element *c)
9063 {
9064 struct breakpoint *b;
9065
9066 ALL_BREAKPOINTS (b)
9067 {
9068 if (b->type == bp_dprintf)
9069 update_dprintf_command_list (b);
9070 }
9071 }
9072
9073 /* Create a breakpoint with SAL as location. Use ADDR_STRING
9074 as textual description of the location, and COND_STRING
9075 as condition expression. */
9076
9077 static void
9078 init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
9079 struct symtabs_and_lines sals, char *addr_string,
9080 char *filter, char *cond_string,
9081 char *extra_string,
9082 enum bptype type, enum bpdisp disposition,
9083 int thread, int task, int ignore_count,
9084 const struct breakpoint_ops *ops, int from_tty,
9085 int enabled, int internal, unsigned flags,
9086 int display_canonical)
9087 {
9088 int i;
9089
9090 if (type == bp_hardware_breakpoint)
9091 {
9092 int target_resources_ok;
9093
9094 i = hw_breakpoint_used_count ();
9095 target_resources_ok =
9096 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9097 i + 1, 0);
9098 if (target_resources_ok == 0)
9099 error (_("No hardware breakpoint support in the target."));
9100 else if (target_resources_ok < 0)
9101 error (_("Hardware breakpoints used exceeds limit."));
9102 }
9103
9104 gdb_assert (sals.nelts > 0);
9105
9106 for (i = 0; i < sals.nelts; ++i)
9107 {
9108 struct symtab_and_line sal = sals.sals[i];
9109 struct bp_location *loc;
9110
9111 if (from_tty)
9112 {
9113 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9114 if (!loc_gdbarch)
9115 loc_gdbarch = gdbarch;
9116
9117 describe_other_breakpoints (loc_gdbarch,
9118 sal.pspace, sal.pc, sal.section, thread);
9119 }
9120
9121 if (i == 0)
9122 {
9123 init_raw_breakpoint (b, gdbarch, sal, type, ops);
9124 b->thread = thread;
9125 b->task = task;
9126
9127 b->cond_string = cond_string;
9128 b->extra_string = extra_string;
9129 b->ignore_count = ignore_count;
9130 b->enable_state = enabled ? bp_enabled : bp_disabled;
9131 b->disposition = disposition;
9132
9133 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9134 b->loc->inserted = 1;
9135
9136 if (type == bp_static_tracepoint)
9137 {
9138 struct tracepoint *t = (struct tracepoint *) b;
9139 struct static_tracepoint_marker marker;
9140
9141 if (strace_marker_p (b))
9142 {
9143 /* We already know the marker exists, otherwise, we
9144 wouldn't see a sal for it. */
9145 char *p = &addr_string[3];
9146 char *endp;
9147 char *marker_str;
9148
9149 p = skip_spaces (p);
9150
9151 endp = skip_to_space (p);
9152
9153 marker_str = savestring (p, endp - p);
9154 t->static_trace_marker_id = marker_str;
9155
9156 printf_filtered (_("Probed static tracepoint "
9157 "marker \"%s\"\n"),
9158 t->static_trace_marker_id);
9159 }
9160 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
9161 {
9162 t->static_trace_marker_id = xstrdup (marker.str_id);
9163 release_static_tracepoint_marker (&marker);
9164
9165 printf_filtered (_("Probed static tracepoint "
9166 "marker \"%s\"\n"),
9167 t->static_trace_marker_id);
9168 }
9169 else
9170 warning (_("Couldn't determine the static "
9171 "tracepoint marker to probe"));
9172 }
9173
9174 loc = b->loc;
9175 }
9176 else
9177 {
9178 loc = add_location_to_breakpoint (b, &sal);
9179 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9180 loc->inserted = 1;
9181 }
9182
9183 if (b->cond_string)
9184 {
9185 const char *arg = b->cond_string;
9186
9187 loc->cond = parse_exp_1 (&arg, loc->address,
9188 block_for_pc (loc->address), 0);
9189 if (*arg)
9190 error (_("Garbage '%s' follows condition"), arg);
9191 }
9192
9193 /* Dynamic printf requires and uses additional arguments on the
9194 command line, otherwise it's an error. */
9195 if (type == bp_dprintf)
9196 {
9197 if (b->extra_string)
9198 update_dprintf_command_list (b);
9199 else
9200 error (_("Format string required"));
9201 }
9202 else if (b->extra_string)
9203 error (_("Garbage '%s' at end of command"), b->extra_string);
9204 }
9205
9206 b->display_canonical = display_canonical;
9207 if (addr_string)
9208 b->addr_string = addr_string;
9209 else
9210 /* addr_string has to be used or breakpoint_re_set will delete
9211 me. */
9212 b->addr_string
9213 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
9214 b->filter = filter;
9215 }
9216
9217 static void
9218 create_breakpoint_sal (struct gdbarch *gdbarch,
9219 struct symtabs_and_lines sals, char *addr_string,
9220 char *filter, char *cond_string,
9221 char *extra_string,
9222 enum bptype type, enum bpdisp disposition,
9223 int thread, int task, int ignore_count,
9224 const struct breakpoint_ops *ops, int from_tty,
9225 int enabled, int internal, unsigned flags,
9226 int display_canonical)
9227 {
9228 struct breakpoint *b;
9229 struct cleanup *old_chain;
9230
9231 if (is_tracepoint_type (type))
9232 {
9233 struct tracepoint *t;
9234
9235 t = XCNEW (struct tracepoint);
9236 b = &t->base;
9237 }
9238 else
9239 b = XNEW (struct breakpoint);
9240
9241 old_chain = make_cleanup (xfree, b);
9242
9243 init_breakpoint_sal (b, gdbarch,
9244 sals, addr_string,
9245 filter, cond_string, extra_string,
9246 type, disposition,
9247 thread, task, ignore_count,
9248 ops, from_tty,
9249 enabled, internal, flags,
9250 display_canonical);
9251 discard_cleanups (old_chain);
9252
9253 install_breakpoint (internal, b, 0);
9254 }
9255
9256 /* Add SALS.nelts breakpoints to the breakpoint table. For each
9257 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9258 value. COND_STRING, if not NULL, specified the condition to be
9259 used for all breakpoints. Essentially the only case where
9260 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9261 function. In that case, it's still not possible to specify
9262 separate conditions for different overloaded functions, so
9263 we take just a single condition string.
9264
9265 NOTE: If the function succeeds, the caller is expected to cleanup
9266 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
9267 array contents). If the function fails (error() is called), the
9268 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
9269 COND and SALS arrays and each of those arrays contents. */
9270
9271 static void
9272 create_breakpoints_sal (struct gdbarch *gdbarch,
9273 struct linespec_result *canonical,
9274 char *cond_string, char *extra_string,
9275 enum bptype type, enum bpdisp disposition,
9276 int thread, int task, int ignore_count,
9277 const struct breakpoint_ops *ops, int from_tty,
9278 int enabled, int internal, unsigned flags)
9279 {
9280 int i;
9281 struct linespec_sals *lsal;
9282
9283 if (canonical->pre_expanded)
9284 gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
9285
9286 for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
9287 {
9288 /* Note that 'addr_string' can be NULL in the case of a plain
9289 'break', without arguments. */
9290 char *addr_string = (canonical->addr_string
9291 ? xstrdup (canonical->addr_string)
9292 : NULL);
9293 char *filter_string = lsal->canonical ? xstrdup (lsal->canonical) : NULL;
9294 struct cleanup *inner = make_cleanup (xfree, addr_string);
9295
9296 make_cleanup (xfree, filter_string);
9297 create_breakpoint_sal (gdbarch, lsal->sals,
9298 addr_string,
9299 filter_string,
9300 cond_string, extra_string,
9301 type, disposition,
9302 thread, task, ignore_count, ops,
9303 from_tty, enabled, internal, flags,
9304 canonical->special_display);
9305 discard_cleanups (inner);
9306 }
9307 }
9308
9309 /* Parse ADDRESS which is assumed to be a SAL specification possibly
9310 followed by conditionals. On return, SALS contains an array of SAL
9311 addresses found. ADDR_STRING contains a vector of (canonical)
9312 address strings. ADDRESS points to the end of the SAL.
9313
9314 The array and the line spec strings are allocated on the heap, it is
9315 the caller's responsibility to free them. */
9316
9317 static void
9318 parse_breakpoint_sals (char **address,
9319 struct linespec_result *canonical)
9320 {
9321 /* If no arg given, or if first arg is 'if ', use the default
9322 breakpoint. */
9323 if ((*address) == NULL || linespec_lexer_lex_keyword (*address))
9324 {
9325 /* The last displayed codepoint, if it's valid, is our default breakpoint
9326 address. */
9327 if (last_displayed_sal_is_valid ())
9328 {
9329 struct linespec_sals lsal;
9330 struct symtab_and_line sal;
9331 CORE_ADDR pc;
9332
9333 init_sal (&sal); /* Initialize to zeroes. */
9334 lsal.sals.sals = (struct symtab_and_line *)
9335 xmalloc (sizeof (struct symtab_and_line));
9336
9337 /* Set sal's pspace, pc, symtab, and line to the values
9338 corresponding to the last call to print_frame_info.
9339 Be sure to reinitialize LINE with NOTCURRENT == 0
9340 as the breakpoint line number is inappropriate otherwise.
9341 find_pc_line would adjust PC, re-set it back. */
9342 get_last_displayed_sal (&sal);
9343 pc = sal.pc;
9344 sal = find_pc_line (pc, 0);
9345
9346 /* "break" without arguments is equivalent to "break *PC"
9347 where PC is the last displayed codepoint's address. So
9348 make sure to set sal.explicit_pc to prevent GDB from
9349 trying to expand the list of sals to include all other
9350 instances with the same symtab and line. */
9351 sal.pc = pc;
9352 sal.explicit_pc = 1;
9353
9354 lsal.sals.sals[0] = sal;
9355 lsal.sals.nelts = 1;
9356 lsal.canonical = NULL;
9357
9358 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
9359 }
9360 else
9361 error (_("No default breakpoint address now."));
9362 }
9363 else
9364 {
9365 struct symtab_and_line cursal = get_current_source_symtab_and_line ();
9366
9367 /* Force almost all breakpoints to be in terms of the
9368 current_source_symtab (which is decode_line_1's default).
9369 This should produce the results we want almost all of the
9370 time while leaving default_breakpoint_* alone.
9371
9372 ObjC: However, don't match an Objective-C method name which
9373 may have a '+' or '-' succeeded by a '['. */
9374 if (last_displayed_sal_is_valid ()
9375 && (!cursal.symtab
9376 || ((strchr ("+-", (*address)[0]) != NULL)
9377 && ((*address)[1] != '['))))
9378 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
9379 get_last_displayed_symtab (),
9380 get_last_displayed_line (),
9381 canonical, NULL, NULL);
9382 else
9383 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
9384 cursal.symtab, cursal.line, canonical, NULL, NULL);
9385 }
9386 }
9387
9388
9389 /* Convert each SAL into a real PC. Verify that the PC can be
9390 inserted as a breakpoint. If it can't throw an error. */
9391
9392 static void
9393 breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
9394 {
9395 int i;
9396
9397 for (i = 0; i < sals->nelts; i++)
9398 resolve_sal_pc (&sals->sals[i]);
9399 }
9400
9401 /* Fast tracepoints may have restrictions on valid locations. For
9402 instance, a fast tracepoint using a jump instead of a trap will
9403 likely have to overwrite more bytes than a trap would, and so can
9404 only be placed where the instruction is longer than the jump, or a
9405 multi-instruction sequence does not have a jump into the middle of
9406 it, etc. */
9407
9408 static void
9409 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9410 struct symtabs_and_lines *sals)
9411 {
9412 int i, rslt;
9413 struct symtab_and_line *sal;
9414 char *msg;
9415 struct cleanup *old_chain;
9416
9417 for (i = 0; i < sals->nelts; i++)
9418 {
9419 struct gdbarch *sarch;
9420
9421 sal = &sals->sals[i];
9422
9423 sarch = get_sal_arch (*sal);
9424 /* We fall back to GDBARCH if there is no architecture
9425 associated with SAL. */
9426 if (sarch == NULL)
9427 sarch = gdbarch;
9428 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc, &msg);
9429 old_chain = make_cleanup (xfree, msg);
9430
9431 if (!rslt)
9432 error (_("May not have a fast tracepoint at 0x%s%s"),
9433 paddress (sarch, sal->pc), (msg ? msg : ""));
9434
9435 do_cleanups (old_chain);
9436 }
9437 }
9438
9439 /* Issue an invalid thread ID error. */
9440
9441 static void ATTRIBUTE_NORETURN
9442 invalid_thread_id_error (int id)
9443 {
9444 error (_("Unknown thread %d."), id);
9445 }
9446
9447 /* Given TOK, a string specification of condition and thread, as
9448 accepted by the 'break' command, extract the condition
9449 string and thread number and set *COND_STRING and *THREAD.
9450 PC identifies the context at which the condition should be parsed.
9451 If no condition is found, *COND_STRING is set to NULL.
9452 If no thread is found, *THREAD is set to -1. */
9453
9454 static void
9455 find_condition_and_thread (const char *tok, CORE_ADDR pc,
9456 char **cond_string, int *thread, int *task,
9457 char **rest)
9458 {
9459 *cond_string = NULL;
9460 *thread = -1;
9461 *task = 0;
9462 *rest = NULL;
9463
9464 while (tok && *tok)
9465 {
9466 const char *end_tok;
9467 int toklen;
9468 const char *cond_start = NULL;
9469 const char *cond_end = NULL;
9470
9471 tok = skip_spaces_const (tok);
9472
9473 if ((*tok == '"' || *tok == ',') && rest)
9474 {
9475 *rest = savestring (tok, strlen (tok));
9476 return;
9477 }
9478
9479 end_tok = skip_to_space_const (tok);
9480
9481 toklen = end_tok - tok;
9482
9483 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9484 {
9485 struct expression *expr;
9486
9487 tok = cond_start = end_tok + 1;
9488 expr = parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
9489 xfree (expr);
9490 cond_end = tok;
9491 *cond_string = savestring (cond_start, cond_end - cond_start);
9492 }
9493 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9494 {
9495 char *tmptok;
9496
9497 tok = end_tok + 1;
9498 *thread = strtol (tok, &tmptok, 0);
9499 if (tok == tmptok)
9500 error (_("Junk after thread keyword."));
9501 if (!valid_thread_id (*thread))
9502 invalid_thread_id_error (*thread);
9503 tok = tmptok;
9504 }
9505 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9506 {
9507 char *tmptok;
9508
9509 tok = end_tok + 1;
9510 *task = strtol (tok, &tmptok, 0);
9511 if (tok == tmptok)
9512 error (_("Junk after task keyword."));
9513 if (!valid_task_id (*task))
9514 error (_("Unknown task %d."), *task);
9515 tok = tmptok;
9516 }
9517 else if (rest)
9518 {
9519 *rest = savestring (tok, strlen (tok));
9520 return;
9521 }
9522 else
9523 error (_("Junk at end of arguments."));
9524 }
9525 }
9526
9527 /* Decode a static tracepoint marker spec. */
9528
9529 static struct symtabs_and_lines
9530 decode_static_tracepoint_spec (char **arg_p)
9531 {
9532 VEC(static_tracepoint_marker_p) *markers = NULL;
9533 struct symtabs_and_lines sals;
9534 struct cleanup *old_chain;
9535 char *p = &(*arg_p)[3];
9536 char *endp;
9537 char *marker_str;
9538 int i;
9539
9540 p = skip_spaces (p);
9541
9542 endp = skip_to_space (p);
9543
9544 marker_str = savestring (p, endp - p);
9545 old_chain = make_cleanup (xfree, marker_str);
9546
9547 markers = target_static_tracepoint_markers_by_strid (marker_str);
9548 if (VEC_empty(static_tracepoint_marker_p, markers))
9549 error (_("No known static tracepoint marker named %s"), marker_str);
9550
9551 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
9552 sals.sals = xmalloc (sizeof *sals.sals * sals.nelts);
9553
9554 for (i = 0; i < sals.nelts; i++)
9555 {
9556 struct static_tracepoint_marker *marker;
9557
9558 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9559
9560 init_sal (&sals.sals[i]);
9561
9562 sals.sals[i] = find_pc_line (marker->address, 0);
9563 sals.sals[i].pc = marker->address;
9564
9565 release_static_tracepoint_marker (marker);
9566 }
9567
9568 do_cleanups (old_chain);
9569
9570 *arg_p = endp;
9571 return sals;
9572 }
9573
9574 /* Set a breakpoint. This function is shared between CLI and MI
9575 functions for setting a breakpoint. This function has two major
9576 modes of operations, selected by the PARSE_ARG parameter. If
9577 non-zero, the function will parse ARG, extracting location,
9578 condition, thread and extra string. Otherwise, ARG is just the
9579 breakpoint's location, with condition, thread, and extra string
9580 specified by the COND_STRING, THREAD and EXTRA_STRING parameters.
9581 If INTERNAL is non-zero, the breakpoint number will be allocated
9582 from the internal breakpoint count. Returns true if any breakpoint
9583 was created; false otherwise. */
9584
9585 int
9586 create_breakpoint (struct gdbarch *gdbarch,
9587 char *arg, char *cond_string,
9588 int thread, char *extra_string,
9589 int parse_arg,
9590 int tempflag, enum bptype type_wanted,
9591 int ignore_count,
9592 enum auto_boolean pending_break_support,
9593 const struct breakpoint_ops *ops,
9594 int from_tty, int enabled, int internal,
9595 unsigned flags)
9596 {
9597 char *copy_arg = NULL;
9598 char *addr_start = arg;
9599 struct linespec_result canonical;
9600 struct cleanup *old_chain;
9601 struct cleanup *bkpt_chain = NULL;
9602 int pending = 0;
9603 int task = 0;
9604 int prev_bkpt_count = breakpoint_count;
9605
9606 gdb_assert (ops != NULL);
9607
9608 init_linespec_result (&canonical);
9609
9610 TRY
9611 {
9612 ops->create_sals_from_address (&arg, &canonical, type_wanted,
9613 addr_start, &copy_arg);
9614 }
9615 CATCH (e, RETURN_MASK_ERROR)
9616 {
9617 /* If caller is interested in rc value from parse, set
9618 value. */
9619 if (e.error == NOT_FOUND_ERROR)
9620 {
9621 /* If pending breakpoint support is turned off, throw
9622 error. */
9623
9624 if (pending_break_support == AUTO_BOOLEAN_FALSE)
9625 throw_exception (e);
9626
9627 exception_print (gdb_stderr, e);
9628
9629 /* If pending breakpoint support is auto query and the user
9630 selects no, then simply return the error code. */
9631 if (pending_break_support == AUTO_BOOLEAN_AUTO
9632 && !nquery (_("Make %s pending on future shared library load? "),
9633 bptype_string (type_wanted)))
9634 return 0;
9635
9636 /* At this point, either the user was queried about setting
9637 a pending breakpoint and selected yes, or pending
9638 breakpoint behavior is on and thus a pending breakpoint
9639 is defaulted on behalf of the user. */
9640 {
9641 struct linespec_sals lsal;
9642
9643 copy_arg = xstrdup (addr_start);
9644 lsal.canonical = xstrdup (copy_arg);
9645 lsal.sals.nelts = 1;
9646 lsal.sals.sals = XNEW (struct symtab_and_line);
9647 init_sal (&lsal.sals.sals[0]);
9648 pending = 1;
9649 VEC_safe_push (linespec_sals, canonical.sals, &lsal);
9650 }
9651 }
9652 else
9653 throw_exception (e);
9654 }
9655 END_CATCH
9656
9657 if (VEC_empty (linespec_sals, canonical.sals))
9658 return 0;
9659
9660 /* Create a chain of things that always need to be cleaned up. */
9661 old_chain = make_cleanup_destroy_linespec_result (&canonical);
9662
9663 /* ----------------------------- SNIP -----------------------------
9664 Anything added to the cleanup chain beyond this point is assumed
9665 to be part of a breakpoint. If the breakpoint create succeeds
9666 then the memory is not reclaimed. */
9667 bkpt_chain = make_cleanup (null_cleanup, 0);
9668
9669 /* Resolve all line numbers to PC's and verify that the addresses
9670 are ok for the target. */
9671 if (!pending)
9672 {
9673 int ix;
9674 struct linespec_sals *iter;
9675
9676 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9677 breakpoint_sals_to_pc (&iter->sals);
9678 }
9679
9680 /* Fast tracepoints may have additional restrictions on location. */
9681 if (!pending && type_wanted == bp_fast_tracepoint)
9682 {
9683 int ix;
9684 struct linespec_sals *iter;
9685
9686 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9687 check_fast_tracepoint_sals (gdbarch, &iter->sals);
9688 }
9689
9690 /* Verify that condition can be parsed, before setting any
9691 breakpoints. Allocate a separate condition expression for each
9692 breakpoint. */
9693 if (!pending)
9694 {
9695 if (parse_arg)
9696 {
9697 char *rest;
9698 struct linespec_sals *lsal;
9699
9700 lsal = VEC_index (linespec_sals, canonical.sals, 0);
9701
9702 /* Here we only parse 'arg' to separate condition
9703 from thread number, so parsing in context of first
9704 sal is OK. When setting the breakpoint we'll
9705 re-parse it in context of each sal. */
9706
9707 find_condition_and_thread (arg, lsal->sals.sals[0].pc, &cond_string,
9708 &thread, &task, &rest);
9709 if (cond_string)
9710 make_cleanup (xfree, cond_string);
9711 if (rest)
9712 make_cleanup (xfree, rest);
9713 if (rest)
9714 extra_string = rest;
9715 }
9716 else
9717 {
9718 if (*arg != '\0')
9719 error (_("Garbage '%s' at end of location"), arg);
9720
9721 /* Create a private copy of condition string. */
9722 if (cond_string)
9723 {
9724 cond_string = xstrdup (cond_string);
9725 make_cleanup (xfree, cond_string);
9726 }
9727 /* Create a private copy of any extra string. */
9728 if (extra_string)
9729 {
9730 extra_string = xstrdup (extra_string);
9731 make_cleanup (xfree, extra_string);
9732 }
9733 }
9734
9735 ops->create_breakpoints_sal (gdbarch, &canonical,
9736 cond_string, extra_string, type_wanted,
9737 tempflag ? disp_del : disp_donttouch,
9738 thread, task, ignore_count, ops,
9739 from_tty, enabled, internal, flags);
9740 }
9741 else
9742 {
9743 struct breakpoint *b;
9744
9745 make_cleanup (xfree, copy_arg);
9746
9747 if (is_tracepoint_type (type_wanted))
9748 {
9749 struct tracepoint *t;
9750
9751 t = XCNEW (struct tracepoint);
9752 b = &t->base;
9753 }
9754 else
9755 b = XNEW (struct breakpoint);
9756
9757 init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops);
9758
9759 b->addr_string = copy_arg;
9760 if (parse_arg)
9761 {
9762 b->cond_string = NULL;
9763 b->extra_string = NULL;
9764 }
9765 else
9766 {
9767 /* Create a private copy of condition string. */
9768 if (cond_string)
9769 {
9770 cond_string = xstrdup (cond_string);
9771 make_cleanup (xfree, cond_string);
9772 }
9773 /* Create a private copy of any extra string. */
9774 if (extra_string != NULL)
9775 {
9776 extra_string = xstrdup (extra_string);
9777 make_cleanup (xfree, extra_string);
9778 }
9779 b->cond_string = cond_string;
9780 b->extra_string = extra_string;
9781 b->thread = thread;
9782 }
9783 b->ignore_count = ignore_count;
9784 b->disposition = tempflag ? disp_del : disp_donttouch;
9785 b->condition_not_parsed = 1;
9786 b->enable_state = enabled ? bp_enabled : bp_disabled;
9787 if ((type_wanted != bp_breakpoint
9788 && type_wanted != bp_hardware_breakpoint) || thread != -1)
9789 b->pspace = current_program_space;
9790
9791 install_breakpoint (internal, b, 0);
9792 }
9793
9794 if (VEC_length (linespec_sals, canonical.sals) > 1)
9795 {
9796 warning (_("Multiple breakpoints were set.\nUse the "
9797 "\"delete\" command to delete unwanted breakpoints."));
9798 prev_breakpoint_count = prev_bkpt_count;
9799 }
9800
9801 /* That's it. Discard the cleanups for data inserted into the
9802 breakpoint. */
9803 discard_cleanups (bkpt_chain);
9804 /* But cleanup everything else. */
9805 do_cleanups (old_chain);
9806
9807 /* error call may happen here - have BKPT_CHAIN already discarded. */
9808 update_global_location_list (UGLL_MAY_INSERT);
9809
9810 return 1;
9811 }
9812
9813 /* Set a breakpoint.
9814 ARG is a string describing breakpoint address,
9815 condition, and thread.
9816 FLAG specifies if a breakpoint is hardware on,
9817 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9818 and BP_TEMPFLAG. */
9819
9820 static void
9821 break_command_1 (char *arg, int flag, int from_tty)
9822 {
9823 int tempflag = flag & BP_TEMPFLAG;
9824 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9825 ? bp_hardware_breakpoint
9826 : bp_breakpoint);
9827 struct breakpoint_ops *ops;
9828 const char *arg_cp = arg;
9829
9830 /* Matching breakpoints on probes. */
9831 if (arg && probe_linespec_to_ops (&arg_cp) != NULL)
9832 ops = &bkpt_probe_breakpoint_ops;
9833 else
9834 ops = &bkpt_breakpoint_ops;
9835
9836 create_breakpoint (get_current_arch (),
9837 arg,
9838 NULL, 0, NULL, 1 /* parse arg */,
9839 tempflag, type_wanted,
9840 0 /* Ignore count */,
9841 pending_break_support,
9842 ops,
9843 from_tty,
9844 1 /* enabled */,
9845 0 /* internal */,
9846 0);
9847 }
9848
9849 /* Helper function for break_command_1 and disassemble_command. */
9850
9851 void
9852 resolve_sal_pc (struct symtab_and_line *sal)
9853 {
9854 CORE_ADDR pc;
9855
9856 if (sal->pc == 0 && sal->symtab != NULL)
9857 {
9858 if (!find_line_pc (sal->symtab, sal->line, &pc))
9859 error (_("No line %d in file \"%s\"."),
9860 sal->line, symtab_to_filename_for_display (sal->symtab));
9861 sal->pc = pc;
9862
9863 /* If this SAL corresponds to a breakpoint inserted using a line
9864 number, then skip the function prologue if necessary. */
9865 if (sal->explicit_line)
9866 skip_prologue_sal (sal);
9867 }
9868
9869 if (sal->section == 0 && sal->symtab != NULL)
9870 {
9871 const struct blockvector *bv;
9872 const struct block *b;
9873 struct symbol *sym;
9874
9875 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9876 SYMTAB_COMPUNIT (sal->symtab));
9877 if (bv != NULL)
9878 {
9879 sym = block_linkage_function (b);
9880 if (sym != NULL)
9881 {
9882 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
9883 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
9884 sym);
9885 }
9886 else
9887 {
9888 /* It really is worthwhile to have the section, so we'll
9889 just have to look harder. This case can be executed
9890 if we have line numbers but no functions (as can
9891 happen in assembly source). */
9892
9893 struct bound_minimal_symbol msym;
9894 struct cleanup *old_chain = save_current_space_and_thread ();
9895
9896 switch_to_program_space_and_thread (sal->pspace);
9897
9898 msym = lookup_minimal_symbol_by_pc (sal->pc);
9899 if (msym.minsym)
9900 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
9901
9902 do_cleanups (old_chain);
9903 }
9904 }
9905 }
9906 }
9907
9908 void
9909 break_command (char *arg, int from_tty)
9910 {
9911 break_command_1 (arg, 0, from_tty);
9912 }
9913
9914 void
9915 tbreak_command (char *arg, int from_tty)
9916 {
9917 break_command_1 (arg, BP_TEMPFLAG, from_tty);
9918 }
9919
9920 static void
9921 hbreak_command (char *arg, int from_tty)
9922 {
9923 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
9924 }
9925
9926 static void
9927 thbreak_command (char *arg, int from_tty)
9928 {
9929 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
9930 }
9931
9932 static void
9933 stop_command (char *arg, int from_tty)
9934 {
9935 printf_filtered (_("Specify the type of breakpoint to set.\n\
9936 Usage: stop in <function | address>\n\
9937 stop at <line>\n"));
9938 }
9939
9940 static void
9941 stopin_command (char *arg, int from_tty)
9942 {
9943 int badInput = 0;
9944
9945 if (arg == (char *) NULL)
9946 badInput = 1;
9947 else if (*arg != '*')
9948 {
9949 char *argptr = arg;
9950 int hasColon = 0;
9951
9952 /* Look for a ':'. If this is a line number specification, then
9953 say it is bad, otherwise, it should be an address or
9954 function/method name. */
9955 while (*argptr && !hasColon)
9956 {
9957 hasColon = (*argptr == ':');
9958 argptr++;
9959 }
9960
9961 if (hasColon)
9962 badInput = (*argptr != ':'); /* Not a class::method */
9963 else
9964 badInput = isdigit (*arg); /* a simple line number */
9965 }
9966
9967 if (badInput)
9968 printf_filtered (_("Usage: stop in <function | address>\n"));
9969 else
9970 break_command_1 (arg, 0, from_tty);
9971 }
9972
9973 static void
9974 stopat_command (char *arg, int from_tty)
9975 {
9976 int badInput = 0;
9977
9978 if (arg == (char *) NULL || *arg == '*') /* no line number */
9979 badInput = 1;
9980 else
9981 {
9982 char *argptr = arg;
9983 int hasColon = 0;
9984
9985 /* Look for a ':'. If there is a '::' then get out, otherwise
9986 it is probably a line number. */
9987 while (*argptr && !hasColon)
9988 {
9989 hasColon = (*argptr == ':');
9990 argptr++;
9991 }
9992
9993 if (hasColon)
9994 badInput = (*argptr == ':'); /* we have class::method */
9995 else
9996 badInput = !isdigit (*arg); /* not a line number */
9997 }
9998
9999 if (badInput)
10000 printf_filtered (_("Usage: stop at <line>\n"));
10001 else
10002 break_command_1 (arg, 0, from_tty);
10003 }
10004
10005 /* The dynamic printf command is mostly like a regular breakpoint, but
10006 with a prewired command list consisting of a single output command,
10007 built from extra arguments supplied on the dprintf command
10008 line. */
10009
10010 static void
10011 dprintf_command (char *arg, int from_tty)
10012 {
10013 create_breakpoint (get_current_arch (),
10014 arg,
10015 NULL, 0, NULL, 1 /* parse arg */,
10016 0, bp_dprintf,
10017 0 /* Ignore count */,
10018 pending_break_support,
10019 &dprintf_breakpoint_ops,
10020 from_tty,
10021 1 /* enabled */,
10022 0 /* internal */,
10023 0);
10024 }
10025
10026 static void
10027 agent_printf_command (char *arg, int from_tty)
10028 {
10029 error (_("May only run agent-printf on the target"));
10030 }
10031
10032 /* Implement the "breakpoint_hit" breakpoint_ops method for
10033 ranged breakpoints. */
10034
10035 static int
10036 breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
10037 struct address_space *aspace,
10038 CORE_ADDR bp_addr,
10039 const struct target_waitstatus *ws)
10040 {
10041 if (ws->kind != TARGET_WAITKIND_STOPPED
10042 || ws->value.sig != GDB_SIGNAL_TRAP)
10043 return 0;
10044
10045 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
10046 bl->length, aspace, bp_addr);
10047 }
10048
10049 /* Implement the "resources_needed" breakpoint_ops method for
10050 ranged breakpoints. */
10051
10052 static int
10053 resources_needed_ranged_breakpoint (const struct bp_location *bl)
10054 {
10055 return target_ranged_break_num_registers ();
10056 }
10057
10058 /* Implement the "print_it" breakpoint_ops method for
10059 ranged breakpoints. */
10060
10061 static enum print_stop_action
10062 print_it_ranged_breakpoint (bpstat bs)
10063 {
10064 struct breakpoint *b = bs->breakpoint_at;
10065 struct bp_location *bl = b->loc;
10066 struct ui_out *uiout = current_uiout;
10067
10068 gdb_assert (b->type == bp_hardware_breakpoint);
10069
10070 /* Ranged breakpoints have only one location. */
10071 gdb_assert (bl && bl->next == NULL);
10072
10073 annotate_breakpoint (b->number);
10074 if (b->disposition == disp_del)
10075 ui_out_text (uiout, "\nTemporary ranged breakpoint ");
10076 else
10077 ui_out_text (uiout, "\nRanged breakpoint ");
10078 if (ui_out_is_mi_like_p (uiout))
10079 {
10080 ui_out_field_string (uiout, "reason",
10081 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
10082 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
10083 }
10084 ui_out_field_int (uiout, "bkptno", b->number);
10085 ui_out_text (uiout, ", ");
10086
10087 return PRINT_SRC_AND_LOC;
10088 }
10089
10090 /* Implement the "print_one" breakpoint_ops method for
10091 ranged breakpoints. */
10092
10093 static void
10094 print_one_ranged_breakpoint (struct breakpoint *b,
10095 struct bp_location **last_loc)
10096 {
10097 struct bp_location *bl = b->loc;
10098 struct value_print_options opts;
10099 struct ui_out *uiout = current_uiout;
10100
10101 /* Ranged breakpoints have only one location. */
10102 gdb_assert (bl && bl->next == NULL);
10103
10104 get_user_print_options (&opts);
10105
10106 if (opts.addressprint)
10107 /* We don't print the address range here, it will be printed later
10108 by print_one_detail_ranged_breakpoint. */
10109 ui_out_field_skip (uiout, "addr");
10110 annotate_field (5);
10111 print_breakpoint_location (b, bl);
10112 *last_loc = bl;
10113 }
10114
10115 /* Implement the "print_one_detail" breakpoint_ops method for
10116 ranged breakpoints. */
10117
10118 static void
10119 print_one_detail_ranged_breakpoint (const struct breakpoint *b,
10120 struct ui_out *uiout)
10121 {
10122 CORE_ADDR address_start, address_end;
10123 struct bp_location *bl = b->loc;
10124 struct ui_file *stb = mem_fileopen ();
10125 struct cleanup *cleanup = make_cleanup_ui_file_delete (stb);
10126
10127 gdb_assert (bl);
10128
10129 address_start = bl->address;
10130 address_end = address_start + bl->length - 1;
10131
10132 ui_out_text (uiout, "\taddress range: ");
10133 fprintf_unfiltered (stb, "[%s, %s]",
10134 print_core_address (bl->gdbarch, address_start),
10135 print_core_address (bl->gdbarch, address_end));
10136 ui_out_field_stream (uiout, "addr", stb);
10137 ui_out_text (uiout, "\n");
10138
10139 do_cleanups (cleanup);
10140 }
10141
10142 /* Implement the "print_mention" breakpoint_ops method for
10143 ranged breakpoints. */
10144
10145 static void
10146 print_mention_ranged_breakpoint (struct breakpoint *b)
10147 {
10148 struct bp_location *bl = b->loc;
10149 struct ui_out *uiout = current_uiout;
10150
10151 gdb_assert (bl);
10152 gdb_assert (b->type == bp_hardware_breakpoint);
10153
10154 if (ui_out_is_mi_like_p (uiout))
10155 return;
10156
10157 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
10158 b->number, paddress (bl->gdbarch, bl->address),
10159 paddress (bl->gdbarch, bl->address + bl->length - 1));
10160 }
10161
10162 /* Implement the "print_recreate" breakpoint_ops method for
10163 ranged breakpoints. */
10164
10165 static void
10166 print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
10167 {
10168 fprintf_unfiltered (fp, "break-range %s, %s", b->addr_string,
10169 b->addr_string_range_end);
10170 print_recreate_thread (b, fp);
10171 }
10172
10173 /* The breakpoint_ops structure to be used in ranged breakpoints. */
10174
10175 static struct breakpoint_ops ranged_breakpoint_ops;
10176
10177 /* Find the address where the end of the breakpoint range should be
10178 placed, given the SAL of the end of the range. This is so that if
10179 the user provides a line number, the end of the range is set to the
10180 last instruction of the given line. */
10181
10182 static CORE_ADDR
10183 find_breakpoint_range_end (struct symtab_and_line sal)
10184 {
10185 CORE_ADDR end;
10186
10187 /* If the user provided a PC value, use it. Otherwise,
10188 find the address of the end of the given location. */
10189 if (sal.explicit_pc)
10190 end = sal.pc;
10191 else
10192 {
10193 int ret;
10194 CORE_ADDR start;
10195
10196 ret = find_line_pc_range (sal, &start, &end);
10197 if (!ret)
10198 error (_("Could not find location of the end of the range."));
10199
10200 /* find_line_pc_range returns the start of the next line. */
10201 end--;
10202 }
10203
10204 return end;
10205 }
10206
10207 /* Implement the "break-range" CLI command. */
10208
10209 static void
10210 break_range_command (char *arg, int from_tty)
10211 {
10212 char *arg_start, *addr_string_start, *addr_string_end;
10213 struct linespec_result canonical_start, canonical_end;
10214 int bp_count, can_use_bp, length;
10215 CORE_ADDR end;
10216 struct breakpoint *b;
10217 struct symtab_and_line sal_start, sal_end;
10218 struct cleanup *cleanup_bkpt;
10219 struct linespec_sals *lsal_start, *lsal_end;
10220
10221 /* We don't support software ranged breakpoints. */
10222 if (target_ranged_break_num_registers () < 0)
10223 error (_("This target does not support hardware ranged breakpoints."));
10224
10225 bp_count = hw_breakpoint_used_count ();
10226 bp_count += target_ranged_break_num_registers ();
10227 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10228 bp_count, 0);
10229 if (can_use_bp < 0)
10230 error (_("Hardware breakpoints used exceeds limit."));
10231
10232 arg = skip_spaces (arg);
10233 if (arg == NULL || arg[0] == '\0')
10234 error(_("No address range specified."));
10235
10236 init_linespec_result (&canonical_start);
10237
10238 arg_start = arg;
10239 parse_breakpoint_sals (&arg, &canonical_start);
10240
10241 cleanup_bkpt = make_cleanup_destroy_linespec_result (&canonical_start);
10242
10243 if (arg[0] != ',')
10244 error (_("Too few arguments."));
10245 else if (VEC_empty (linespec_sals, canonical_start.sals))
10246 error (_("Could not find location of the beginning of the range."));
10247
10248 lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
10249
10250 if (VEC_length (linespec_sals, canonical_start.sals) > 1
10251 || lsal_start->sals.nelts != 1)
10252 error (_("Cannot create a ranged breakpoint with multiple locations."));
10253
10254 sal_start = lsal_start->sals.sals[0];
10255 addr_string_start = savestring (arg_start, arg - arg_start);
10256 make_cleanup (xfree, addr_string_start);
10257
10258 arg++; /* Skip the comma. */
10259 arg = skip_spaces (arg);
10260
10261 /* Parse the end location. */
10262
10263 init_linespec_result (&canonical_end);
10264 arg_start = arg;
10265
10266 /* We call decode_line_full directly here instead of using
10267 parse_breakpoint_sals because we need to specify the start location's
10268 symtab and line as the default symtab and line for the end of the
10269 range. This makes it possible to have ranges like "foo.c:27, +14",
10270 where +14 means 14 lines from the start location. */
10271 decode_line_full (&arg, DECODE_LINE_FUNFIRSTLINE,
10272 sal_start.symtab, sal_start.line,
10273 &canonical_end, NULL, NULL);
10274
10275 make_cleanup_destroy_linespec_result (&canonical_end);
10276
10277 if (VEC_empty (linespec_sals, canonical_end.sals))
10278 error (_("Could not find location of the end of the range."));
10279
10280 lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
10281 if (VEC_length (linespec_sals, canonical_end.sals) > 1
10282 || lsal_end->sals.nelts != 1)
10283 error (_("Cannot create a ranged breakpoint with multiple locations."));
10284
10285 sal_end = lsal_end->sals.sals[0];
10286 addr_string_end = savestring (arg_start, arg - arg_start);
10287 make_cleanup (xfree, addr_string_end);
10288
10289 end = find_breakpoint_range_end (sal_end);
10290 if (sal_start.pc > end)
10291 error (_("Invalid address range, end precedes start."));
10292
10293 length = end - sal_start.pc + 1;
10294 if (length < 0)
10295 /* Length overflowed. */
10296 error (_("Address range too large."));
10297 else if (length == 1)
10298 {
10299 /* This range is simple enough to be handled by
10300 the `hbreak' command. */
10301 hbreak_command (addr_string_start, 1);
10302
10303 do_cleanups (cleanup_bkpt);
10304
10305 return;
10306 }
10307
10308 /* Now set up the breakpoint. */
10309 b = set_raw_breakpoint (get_current_arch (), sal_start,
10310 bp_hardware_breakpoint, &ranged_breakpoint_ops);
10311 set_breakpoint_count (breakpoint_count + 1);
10312 b->number = breakpoint_count;
10313 b->disposition = disp_donttouch;
10314 b->addr_string = xstrdup (addr_string_start);
10315 b->addr_string_range_end = xstrdup (addr_string_end);
10316 b->loc->length = length;
10317
10318 do_cleanups (cleanup_bkpt);
10319
10320 mention (b);
10321 observer_notify_breakpoint_created (b);
10322 update_global_location_list (UGLL_MAY_INSERT);
10323 }
10324
10325 /* Return non-zero if EXP is verified as constant. Returned zero
10326 means EXP is variable. Also the constant detection may fail for
10327 some constant expressions and in such case still falsely return
10328 zero. */
10329
10330 static int
10331 watchpoint_exp_is_const (const struct expression *exp)
10332 {
10333 int i = exp->nelts;
10334
10335 while (i > 0)
10336 {
10337 int oplenp, argsp;
10338
10339 /* We are only interested in the descriptor of each element. */
10340 operator_length (exp, i, &oplenp, &argsp);
10341 i -= oplenp;
10342
10343 switch (exp->elts[i].opcode)
10344 {
10345 case BINOP_ADD:
10346 case BINOP_SUB:
10347 case BINOP_MUL:
10348 case BINOP_DIV:
10349 case BINOP_REM:
10350 case BINOP_MOD:
10351 case BINOP_LSH:
10352 case BINOP_RSH:
10353 case BINOP_LOGICAL_AND:
10354 case BINOP_LOGICAL_OR:
10355 case BINOP_BITWISE_AND:
10356 case BINOP_BITWISE_IOR:
10357 case BINOP_BITWISE_XOR:
10358 case BINOP_EQUAL:
10359 case BINOP_NOTEQUAL:
10360 case BINOP_LESS:
10361 case BINOP_GTR:
10362 case BINOP_LEQ:
10363 case BINOP_GEQ:
10364 case BINOP_REPEAT:
10365 case BINOP_COMMA:
10366 case BINOP_EXP:
10367 case BINOP_MIN:
10368 case BINOP_MAX:
10369 case BINOP_INTDIV:
10370 case BINOP_CONCAT:
10371 case TERNOP_COND:
10372 case TERNOP_SLICE:
10373
10374 case OP_LONG:
10375 case OP_DOUBLE:
10376 case OP_DECFLOAT:
10377 case OP_LAST:
10378 case OP_COMPLEX:
10379 case OP_STRING:
10380 case OP_ARRAY:
10381 case OP_TYPE:
10382 case OP_TYPEOF:
10383 case OP_DECLTYPE:
10384 case OP_TYPEID:
10385 case OP_NAME:
10386 case OP_OBJC_NSSTRING:
10387
10388 case UNOP_NEG:
10389 case UNOP_LOGICAL_NOT:
10390 case UNOP_COMPLEMENT:
10391 case UNOP_ADDR:
10392 case UNOP_HIGH:
10393 case UNOP_CAST:
10394
10395 case UNOP_CAST_TYPE:
10396 case UNOP_REINTERPRET_CAST:
10397 case UNOP_DYNAMIC_CAST:
10398 /* Unary, binary and ternary operators: We have to check
10399 their operands. If they are constant, then so is the
10400 result of that operation. For instance, if A and B are
10401 determined to be constants, then so is "A + B".
10402
10403 UNOP_IND is one exception to the rule above, because the
10404 value of *ADDR is not necessarily a constant, even when
10405 ADDR is. */
10406 break;
10407
10408 case OP_VAR_VALUE:
10409 /* Check whether the associated symbol is a constant.
10410
10411 We use SYMBOL_CLASS rather than TYPE_CONST because it's
10412 possible that a buggy compiler could mark a variable as
10413 constant even when it is not, and TYPE_CONST would return
10414 true in this case, while SYMBOL_CLASS wouldn't.
10415
10416 We also have to check for function symbols because they
10417 are always constant. */
10418 {
10419 struct symbol *s = exp->elts[i + 2].symbol;
10420
10421 if (SYMBOL_CLASS (s) != LOC_BLOCK
10422 && SYMBOL_CLASS (s) != LOC_CONST
10423 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10424 return 0;
10425 break;
10426 }
10427
10428 /* The default action is to return 0 because we are using
10429 the optimistic approach here: If we don't know something,
10430 then it is not a constant. */
10431 default:
10432 return 0;
10433 }
10434 }
10435
10436 return 1;
10437 }
10438
10439 /* Implement the "dtor" breakpoint_ops method for watchpoints. */
10440
10441 static void
10442 dtor_watchpoint (struct breakpoint *self)
10443 {
10444 struct watchpoint *w = (struct watchpoint *) self;
10445
10446 xfree (w->cond_exp);
10447 xfree (w->exp);
10448 xfree (w->exp_string);
10449 xfree (w->exp_string_reparse);
10450 value_free (w->val);
10451
10452 base_breakpoint_ops.dtor (self);
10453 }
10454
10455 /* Implement the "re_set" breakpoint_ops method for watchpoints. */
10456
10457 static void
10458 re_set_watchpoint (struct breakpoint *b)
10459 {
10460 struct watchpoint *w = (struct watchpoint *) b;
10461
10462 /* Watchpoint can be either on expression using entirely global
10463 variables, or it can be on local variables.
10464
10465 Watchpoints of the first kind are never auto-deleted, and even
10466 persist across program restarts. Since they can use variables
10467 from shared libraries, we need to reparse expression as libraries
10468 are loaded and unloaded.
10469
10470 Watchpoints on local variables can also change meaning as result
10471 of solib event. For example, if a watchpoint uses both a local
10472 and a global variables in expression, it's a local watchpoint,
10473 but unloading of a shared library will make the expression
10474 invalid. This is not a very common use case, but we still
10475 re-evaluate expression, to avoid surprises to the user.
10476
10477 Note that for local watchpoints, we re-evaluate it only if
10478 watchpoints frame id is still valid. If it's not, it means the
10479 watchpoint is out of scope and will be deleted soon. In fact,
10480 I'm not sure we'll ever be called in this case.
10481
10482 If a local watchpoint's frame id is still valid, then
10483 w->exp_valid_block is likewise valid, and we can safely use it.
10484
10485 Don't do anything about disabled watchpoints, since they will be
10486 reevaluated again when enabled. */
10487 update_watchpoint (w, 1 /* reparse */);
10488 }
10489
10490 /* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10491
10492 static int
10493 insert_watchpoint (struct bp_location *bl)
10494 {
10495 struct watchpoint *w = (struct watchpoint *) bl->owner;
10496 int length = w->exact ? 1 : bl->length;
10497
10498 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
10499 w->cond_exp);
10500 }
10501
10502 /* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10503
10504 static int
10505 remove_watchpoint (struct bp_location *bl)
10506 {
10507 struct watchpoint *w = (struct watchpoint *) bl->owner;
10508 int length = w->exact ? 1 : bl->length;
10509
10510 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
10511 w->cond_exp);
10512 }
10513
10514 static int
10515 breakpoint_hit_watchpoint (const struct bp_location *bl,
10516 struct address_space *aspace, CORE_ADDR bp_addr,
10517 const struct target_waitstatus *ws)
10518 {
10519 struct breakpoint *b = bl->owner;
10520 struct watchpoint *w = (struct watchpoint *) b;
10521
10522 /* Continuable hardware watchpoints are treated as non-existent if the
10523 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10524 some data address). Otherwise gdb won't stop on a break instruction
10525 in the code (not from a breakpoint) when a hardware watchpoint has
10526 been defined. Also skip watchpoints which we know did not trigger
10527 (did not match the data address). */
10528 if (is_hardware_watchpoint (b)
10529 && w->watchpoint_triggered == watch_triggered_no)
10530 return 0;
10531
10532 return 1;
10533 }
10534
10535 static void
10536 check_status_watchpoint (bpstat bs)
10537 {
10538 gdb_assert (is_watchpoint (bs->breakpoint_at));
10539
10540 bpstat_check_watchpoint (bs);
10541 }
10542
10543 /* Implement the "resources_needed" breakpoint_ops method for
10544 hardware watchpoints. */
10545
10546 static int
10547 resources_needed_watchpoint (const struct bp_location *bl)
10548 {
10549 struct watchpoint *w = (struct watchpoint *) bl->owner;
10550 int length = w->exact? 1 : bl->length;
10551
10552 return target_region_ok_for_hw_watchpoint (bl->address, length);
10553 }
10554
10555 /* Implement the "works_in_software_mode" breakpoint_ops method for
10556 hardware watchpoints. */
10557
10558 static int
10559 works_in_software_mode_watchpoint (const struct breakpoint *b)
10560 {
10561 /* Read and access watchpoints only work with hardware support. */
10562 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
10563 }
10564
10565 static enum print_stop_action
10566 print_it_watchpoint (bpstat bs)
10567 {
10568 struct cleanup *old_chain;
10569 struct breakpoint *b;
10570 struct ui_file *stb;
10571 enum print_stop_action result;
10572 struct watchpoint *w;
10573 struct ui_out *uiout = current_uiout;
10574
10575 gdb_assert (bs->bp_location_at != NULL);
10576
10577 b = bs->breakpoint_at;
10578 w = (struct watchpoint *) b;
10579
10580 stb = mem_fileopen ();
10581 old_chain = make_cleanup_ui_file_delete (stb);
10582
10583 switch (b->type)
10584 {
10585 case bp_watchpoint:
10586 case bp_hardware_watchpoint:
10587 annotate_watchpoint (b->number);
10588 if (ui_out_is_mi_like_p (uiout))
10589 ui_out_field_string
10590 (uiout, "reason",
10591 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10592 mention (b);
10593 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10594 ui_out_text (uiout, "\nOld value = ");
10595 watchpoint_value_print (bs->old_val, stb);
10596 ui_out_field_stream (uiout, "old", stb);
10597 ui_out_text (uiout, "\nNew value = ");
10598 watchpoint_value_print (w->val, stb);
10599 ui_out_field_stream (uiout, "new", stb);
10600 ui_out_text (uiout, "\n");
10601 /* More than one watchpoint may have been triggered. */
10602 result = PRINT_UNKNOWN;
10603 break;
10604
10605 case bp_read_watchpoint:
10606 if (ui_out_is_mi_like_p (uiout))
10607 ui_out_field_string
10608 (uiout, "reason",
10609 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10610 mention (b);
10611 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10612 ui_out_text (uiout, "\nValue = ");
10613 watchpoint_value_print (w->val, stb);
10614 ui_out_field_stream (uiout, "value", stb);
10615 ui_out_text (uiout, "\n");
10616 result = PRINT_UNKNOWN;
10617 break;
10618
10619 case bp_access_watchpoint:
10620 if (bs->old_val != NULL)
10621 {
10622 annotate_watchpoint (b->number);
10623 if (ui_out_is_mi_like_p (uiout))
10624 ui_out_field_string
10625 (uiout, "reason",
10626 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10627 mention (b);
10628 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10629 ui_out_text (uiout, "\nOld value = ");
10630 watchpoint_value_print (bs->old_val, stb);
10631 ui_out_field_stream (uiout, "old", stb);
10632 ui_out_text (uiout, "\nNew value = ");
10633 }
10634 else
10635 {
10636 mention (b);
10637 if (ui_out_is_mi_like_p (uiout))
10638 ui_out_field_string
10639 (uiout, "reason",
10640 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10641 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10642 ui_out_text (uiout, "\nValue = ");
10643 }
10644 watchpoint_value_print (w->val, stb);
10645 ui_out_field_stream (uiout, "new", stb);
10646 ui_out_text (uiout, "\n");
10647 result = PRINT_UNKNOWN;
10648 break;
10649 default:
10650 result = PRINT_UNKNOWN;
10651 }
10652
10653 do_cleanups (old_chain);
10654 return result;
10655 }
10656
10657 /* Implement the "print_mention" breakpoint_ops method for hardware
10658 watchpoints. */
10659
10660 static void
10661 print_mention_watchpoint (struct breakpoint *b)
10662 {
10663 struct cleanup *ui_out_chain;
10664 struct watchpoint *w = (struct watchpoint *) b;
10665 struct ui_out *uiout = current_uiout;
10666
10667 switch (b->type)
10668 {
10669 case bp_watchpoint:
10670 ui_out_text (uiout, "Watchpoint ");
10671 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10672 break;
10673 case bp_hardware_watchpoint:
10674 ui_out_text (uiout, "Hardware watchpoint ");
10675 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10676 break;
10677 case bp_read_watchpoint:
10678 ui_out_text (uiout, "Hardware read watchpoint ");
10679 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
10680 break;
10681 case bp_access_watchpoint:
10682 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
10683 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
10684 break;
10685 default:
10686 internal_error (__FILE__, __LINE__,
10687 _("Invalid hardware watchpoint type."));
10688 }
10689
10690 ui_out_field_int (uiout, "number", b->number);
10691 ui_out_text (uiout, ": ");
10692 ui_out_field_string (uiout, "exp", w->exp_string);
10693 do_cleanups (ui_out_chain);
10694 }
10695
10696 /* Implement the "print_recreate" breakpoint_ops method for
10697 watchpoints. */
10698
10699 static void
10700 print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10701 {
10702 struct watchpoint *w = (struct watchpoint *) b;
10703
10704 switch (b->type)
10705 {
10706 case bp_watchpoint:
10707 case bp_hardware_watchpoint:
10708 fprintf_unfiltered (fp, "watch");
10709 break;
10710 case bp_read_watchpoint:
10711 fprintf_unfiltered (fp, "rwatch");
10712 break;
10713 case bp_access_watchpoint:
10714 fprintf_unfiltered (fp, "awatch");
10715 break;
10716 default:
10717 internal_error (__FILE__, __LINE__,
10718 _("Invalid watchpoint type."));
10719 }
10720
10721 fprintf_unfiltered (fp, " %s", w->exp_string);
10722 print_recreate_thread (b, fp);
10723 }
10724
10725 /* Implement the "explains_signal" breakpoint_ops method for
10726 watchpoints. */
10727
10728 static int
10729 explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10730 {
10731 /* A software watchpoint cannot cause a signal other than
10732 GDB_SIGNAL_TRAP. */
10733 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
10734 return 0;
10735
10736 return 1;
10737 }
10738
10739 /* The breakpoint_ops structure to be used in hardware watchpoints. */
10740
10741 static struct breakpoint_ops watchpoint_breakpoint_ops;
10742
10743 /* Implement the "insert" breakpoint_ops method for
10744 masked hardware watchpoints. */
10745
10746 static int
10747 insert_masked_watchpoint (struct bp_location *bl)
10748 {
10749 struct watchpoint *w = (struct watchpoint *) bl->owner;
10750
10751 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
10752 bl->watchpoint_type);
10753 }
10754
10755 /* Implement the "remove" breakpoint_ops method for
10756 masked hardware watchpoints. */
10757
10758 static int
10759 remove_masked_watchpoint (struct bp_location *bl)
10760 {
10761 struct watchpoint *w = (struct watchpoint *) bl->owner;
10762
10763 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
10764 bl->watchpoint_type);
10765 }
10766
10767 /* Implement the "resources_needed" breakpoint_ops method for
10768 masked hardware watchpoints. */
10769
10770 static int
10771 resources_needed_masked_watchpoint (const struct bp_location *bl)
10772 {
10773 struct watchpoint *w = (struct watchpoint *) bl->owner;
10774
10775 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
10776 }
10777
10778 /* Implement the "works_in_software_mode" breakpoint_ops method for
10779 masked hardware watchpoints. */
10780
10781 static int
10782 works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10783 {
10784 return 0;
10785 }
10786
10787 /* Implement the "print_it" breakpoint_ops method for
10788 masked hardware watchpoints. */
10789
10790 static enum print_stop_action
10791 print_it_masked_watchpoint (bpstat bs)
10792 {
10793 struct breakpoint *b = bs->breakpoint_at;
10794 struct ui_out *uiout = current_uiout;
10795
10796 /* Masked watchpoints have only one location. */
10797 gdb_assert (b->loc && b->loc->next == NULL);
10798
10799 switch (b->type)
10800 {
10801 case bp_hardware_watchpoint:
10802 annotate_watchpoint (b->number);
10803 if (ui_out_is_mi_like_p (uiout))
10804 ui_out_field_string
10805 (uiout, "reason",
10806 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10807 break;
10808
10809 case bp_read_watchpoint:
10810 if (ui_out_is_mi_like_p (uiout))
10811 ui_out_field_string
10812 (uiout, "reason",
10813 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10814 break;
10815
10816 case bp_access_watchpoint:
10817 if (ui_out_is_mi_like_p (uiout))
10818 ui_out_field_string
10819 (uiout, "reason",
10820 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10821 break;
10822 default:
10823 internal_error (__FILE__, __LINE__,
10824 _("Invalid hardware watchpoint type."));
10825 }
10826
10827 mention (b);
10828 ui_out_text (uiout, _("\n\
10829 Check the underlying instruction at PC for the memory\n\
10830 address and value which triggered this watchpoint.\n"));
10831 ui_out_text (uiout, "\n");
10832
10833 /* More than one watchpoint may have been triggered. */
10834 return PRINT_UNKNOWN;
10835 }
10836
10837 /* Implement the "print_one_detail" breakpoint_ops method for
10838 masked hardware watchpoints. */
10839
10840 static void
10841 print_one_detail_masked_watchpoint (const struct breakpoint *b,
10842 struct ui_out *uiout)
10843 {
10844 struct watchpoint *w = (struct watchpoint *) b;
10845
10846 /* Masked watchpoints have only one location. */
10847 gdb_assert (b->loc && b->loc->next == NULL);
10848
10849 ui_out_text (uiout, "\tmask ");
10850 ui_out_field_core_addr (uiout, "mask", b->loc->gdbarch, w->hw_wp_mask);
10851 ui_out_text (uiout, "\n");
10852 }
10853
10854 /* Implement the "print_mention" breakpoint_ops method for
10855 masked hardware watchpoints. */
10856
10857 static void
10858 print_mention_masked_watchpoint (struct breakpoint *b)
10859 {
10860 struct watchpoint *w = (struct watchpoint *) b;
10861 struct ui_out *uiout = current_uiout;
10862 struct cleanup *ui_out_chain;
10863
10864 switch (b->type)
10865 {
10866 case bp_hardware_watchpoint:
10867 ui_out_text (uiout, "Masked hardware watchpoint ");
10868 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10869 break;
10870 case bp_read_watchpoint:
10871 ui_out_text (uiout, "Masked hardware read watchpoint ");
10872 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
10873 break;
10874 case bp_access_watchpoint:
10875 ui_out_text (uiout, "Masked hardware access (read/write) watchpoint ");
10876 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
10877 break;
10878 default:
10879 internal_error (__FILE__, __LINE__,
10880 _("Invalid hardware watchpoint type."));
10881 }
10882
10883 ui_out_field_int (uiout, "number", b->number);
10884 ui_out_text (uiout, ": ");
10885 ui_out_field_string (uiout, "exp", w->exp_string);
10886 do_cleanups (ui_out_chain);
10887 }
10888
10889 /* Implement the "print_recreate" breakpoint_ops method for
10890 masked hardware watchpoints. */
10891
10892 static void
10893 print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10894 {
10895 struct watchpoint *w = (struct watchpoint *) b;
10896 char tmp[40];
10897
10898 switch (b->type)
10899 {
10900 case bp_hardware_watchpoint:
10901 fprintf_unfiltered (fp, "watch");
10902 break;
10903 case bp_read_watchpoint:
10904 fprintf_unfiltered (fp, "rwatch");
10905 break;
10906 case bp_access_watchpoint:
10907 fprintf_unfiltered (fp, "awatch");
10908 break;
10909 default:
10910 internal_error (__FILE__, __LINE__,
10911 _("Invalid hardware watchpoint type."));
10912 }
10913
10914 sprintf_vma (tmp, w->hw_wp_mask);
10915 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
10916 print_recreate_thread (b, fp);
10917 }
10918
10919 /* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10920
10921 static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
10922
10923 /* Tell whether the given watchpoint is a masked hardware watchpoint. */
10924
10925 static int
10926 is_masked_watchpoint (const struct breakpoint *b)
10927 {
10928 return b->ops == &masked_watchpoint_breakpoint_ops;
10929 }
10930
10931 /* accessflag: hw_write: watch write,
10932 hw_read: watch read,
10933 hw_access: watch access (read or write) */
10934 static void
10935 watch_command_1 (const char *arg, int accessflag, int from_tty,
10936 int just_location, int internal)
10937 {
10938 struct breakpoint *b, *scope_breakpoint = NULL;
10939 struct expression *exp;
10940 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
10941 struct value *val, *mark, *result;
10942 int saved_bitpos = 0, saved_bitsize = 0;
10943 struct frame_info *frame;
10944 const char *exp_start = NULL;
10945 const char *exp_end = NULL;
10946 const char *tok, *end_tok;
10947 int toklen = -1;
10948 const char *cond_start = NULL;
10949 const char *cond_end = NULL;
10950 enum bptype bp_type;
10951 int thread = -1;
10952 int pc = 0;
10953 /* Flag to indicate whether we are going to use masks for
10954 the hardware watchpoint. */
10955 int use_mask = 0;
10956 CORE_ADDR mask = 0;
10957 struct watchpoint *w;
10958 char *expression;
10959 struct cleanup *back_to;
10960
10961 /* Make sure that we actually have parameters to parse. */
10962 if (arg != NULL && arg[0] != '\0')
10963 {
10964 const char *value_start;
10965
10966 exp_end = arg + strlen (arg);
10967
10968 /* Look for "parameter value" pairs at the end
10969 of the arguments string. */
10970 for (tok = exp_end - 1; tok > arg; tok--)
10971 {
10972 /* Skip whitespace at the end of the argument list. */
10973 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10974 tok--;
10975
10976 /* Find the beginning of the last token.
10977 This is the value of the parameter. */
10978 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10979 tok--;
10980 value_start = tok + 1;
10981
10982 /* Skip whitespace. */
10983 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10984 tok--;
10985
10986 end_tok = tok;
10987
10988 /* Find the beginning of the second to last token.
10989 This is the parameter itself. */
10990 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10991 tok--;
10992 tok++;
10993 toklen = end_tok - tok + 1;
10994
10995 if (toklen == 6 && startswith (tok, "thread"))
10996 {
10997 /* At this point we've found a "thread" token, which means
10998 the user is trying to set a watchpoint that triggers
10999 only in a specific thread. */
11000 char *endp;
11001
11002 if (thread != -1)
11003 error(_("You can specify only one thread."));
11004
11005 /* Extract the thread ID from the next token. */
11006 thread = strtol (value_start, &endp, 0);
11007
11008 /* Check if the user provided a valid numeric value for the
11009 thread ID. */
11010 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
11011 error (_("Invalid thread ID specification %s."), value_start);
11012
11013 /* Check if the thread actually exists. */
11014 if (!valid_thread_id (thread))
11015 invalid_thread_id_error (thread);
11016 }
11017 else if (toklen == 4 && startswith (tok, "mask"))
11018 {
11019 /* We've found a "mask" token, which means the user wants to
11020 create a hardware watchpoint that is going to have the mask
11021 facility. */
11022 struct value *mask_value, *mark;
11023
11024 if (use_mask)
11025 error(_("You can specify only one mask."));
11026
11027 use_mask = just_location = 1;
11028
11029 mark = value_mark ();
11030 mask_value = parse_to_comma_and_eval (&value_start);
11031 mask = value_as_address (mask_value);
11032 value_free_to_mark (mark);
11033 }
11034 else
11035 /* We didn't recognize what we found. We should stop here. */
11036 break;
11037
11038 /* Truncate the string and get rid of the "parameter value" pair before
11039 the arguments string is parsed by the parse_exp_1 function. */
11040 exp_end = tok;
11041 }
11042 }
11043 else
11044 exp_end = arg;
11045
11046 /* Parse the rest of the arguments. From here on out, everything
11047 is in terms of a newly allocated string instead of the original
11048 ARG. */
11049 innermost_block = NULL;
11050 expression = savestring (arg, exp_end - arg);
11051 back_to = make_cleanup (xfree, expression);
11052 exp_start = arg = expression;
11053 exp = parse_exp_1 (&arg, 0, 0, 0);
11054 exp_end = arg;
11055 /* Remove trailing whitespace from the expression before saving it.
11056 This makes the eventual display of the expression string a bit
11057 prettier. */
11058 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
11059 --exp_end;
11060
11061 /* Checking if the expression is not constant. */
11062 if (watchpoint_exp_is_const (exp))
11063 {
11064 int len;
11065
11066 len = exp_end - exp_start;
11067 while (len > 0 && isspace (exp_start[len - 1]))
11068 len--;
11069 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
11070 }
11071
11072 exp_valid_block = innermost_block;
11073 mark = value_mark ();
11074 fetch_subexp_value (exp, &pc, &val, &result, NULL, just_location);
11075
11076 if (val != NULL && just_location)
11077 {
11078 saved_bitpos = value_bitpos (val);
11079 saved_bitsize = value_bitsize (val);
11080 }
11081
11082 if (just_location)
11083 {
11084 int ret;
11085
11086 exp_valid_block = NULL;
11087 val = value_addr (result);
11088 release_value (val);
11089 value_free_to_mark (mark);
11090
11091 if (use_mask)
11092 {
11093 ret = target_masked_watch_num_registers (value_as_address (val),
11094 mask);
11095 if (ret == -1)
11096 error (_("This target does not support masked watchpoints."));
11097 else if (ret == -2)
11098 error (_("Invalid mask or memory region."));
11099 }
11100 }
11101 else if (val != NULL)
11102 release_value (val);
11103
11104 tok = skip_spaces_const (arg);
11105 end_tok = skip_to_space_const (tok);
11106
11107 toklen = end_tok - tok;
11108 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
11109 {
11110 struct expression *cond;
11111
11112 innermost_block = NULL;
11113 tok = cond_start = end_tok + 1;
11114 cond = parse_exp_1 (&tok, 0, 0, 0);
11115
11116 /* The watchpoint expression may not be local, but the condition
11117 may still be. E.g.: `watch global if local > 0'. */
11118 cond_exp_valid_block = innermost_block;
11119
11120 xfree (cond);
11121 cond_end = tok;
11122 }
11123 if (*tok)
11124 error (_("Junk at end of command."));
11125
11126 frame = block_innermost_frame (exp_valid_block);
11127
11128 /* If the expression is "local", then set up a "watchpoint scope"
11129 breakpoint at the point where we've left the scope of the watchpoint
11130 expression. Create the scope breakpoint before the watchpoint, so
11131 that we will encounter it first in bpstat_stop_status. */
11132 if (exp_valid_block && frame)
11133 {
11134 if (frame_id_p (frame_unwind_caller_id (frame)))
11135 {
11136 scope_breakpoint
11137 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
11138 frame_unwind_caller_pc (frame),
11139 bp_watchpoint_scope,
11140 &momentary_breakpoint_ops);
11141
11142 scope_breakpoint->enable_state = bp_enabled;
11143
11144 /* Automatically delete the breakpoint when it hits. */
11145 scope_breakpoint->disposition = disp_del;
11146
11147 /* Only break in the proper frame (help with recursion). */
11148 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
11149
11150 /* Set the address at which we will stop. */
11151 scope_breakpoint->loc->gdbarch
11152 = frame_unwind_caller_arch (frame);
11153 scope_breakpoint->loc->requested_address
11154 = frame_unwind_caller_pc (frame);
11155 scope_breakpoint->loc->address
11156 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
11157 scope_breakpoint->loc->requested_address,
11158 scope_breakpoint->type);
11159 }
11160 }
11161
11162 /* Now set up the breakpoint. We create all watchpoints as hardware
11163 watchpoints here even if hardware watchpoints are turned off, a call
11164 to update_watchpoint later in this function will cause the type to
11165 drop back to bp_watchpoint (software watchpoint) if required. */
11166
11167 if (accessflag == hw_read)
11168 bp_type = bp_read_watchpoint;
11169 else if (accessflag == hw_access)
11170 bp_type = bp_access_watchpoint;
11171 else
11172 bp_type = bp_hardware_watchpoint;
11173
11174 w = XCNEW (struct watchpoint);
11175 b = &w->base;
11176 if (use_mask)
11177 init_raw_breakpoint_without_location (b, NULL, bp_type,
11178 &masked_watchpoint_breakpoint_ops);
11179 else
11180 init_raw_breakpoint_without_location (b, NULL, bp_type,
11181 &watchpoint_breakpoint_ops);
11182 b->thread = thread;
11183 b->disposition = disp_donttouch;
11184 b->pspace = current_program_space;
11185 w->exp = exp;
11186 w->exp_valid_block = exp_valid_block;
11187 w->cond_exp_valid_block = cond_exp_valid_block;
11188 if (just_location)
11189 {
11190 struct type *t = value_type (val);
11191 CORE_ADDR addr = value_as_address (val);
11192 char *name;
11193
11194 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
11195 name = type_to_string (t);
11196
11197 w->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
11198 core_addr_to_string (addr));
11199 xfree (name);
11200
11201 w->exp_string = xstrprintf ("-location %.*s",
11202 (int) (exp_end - exp_start), exp_start);
11203
11204 /* The above expression is in C. */
11205 b->language = language_c;
11206 }
11207 else
11208 w->exp_string = savestring (exp_start, exp_end - exp_start);
11209
11210 if (use_mask)
11211 {
11212 w->hw_wp_mask = mask;
11213 }
11214 else
11215 {
11216 w->val = val;
11217 w->val_bitpos = saved_bitpos;
11218 w->val_bitsize = saved_bitsize;
11219 w->val_valid = 1;
11220 }
11221
11222 if (cond_start)
11223 b->cond_string = savestring (cond_start, cond_end - cond_start);
11224 else
11225 b->cond_string = 0;
11226
11227 if (frame)
11228 {
11229 w->watchpoint_frame = get_frame_id (frame);
11230 w->watchpoint_thread = inferior_ptid;
11231 }
11232 else
11233 {
11234 w->watchpoint_frame = null_frame_id;
11235 w->watchpoint_thread = null_ptid;
11236 }
11237
11238 if (scope_breakpoint != NULL)
11239 {
11240 /* The scope breakpoint is related to the watchpoint. We will
11241 need to act on them together. */
11242 b->related_breakpoint = scope_breakpoint;
11243 scope_breakpoint->related_breakpoint = b;
11244 }
11245
11246 if (!just_location)
11247 value_free_to_mark (mark);
11248
11249 TRY
11250 {
11251 /* Finally update the new watchpoint. This creates the locations
11252 that should be inserted. */
11253 update_watchpoint (w, 1);
11254 }
11255 CATCH (e, RETURN_MASK_ALL)
11256 {
11257 delete_breakpoint (b);
11258 throw_exception (e);
11259 }
11260 END_CATCH
11261
11262 install_breakpoint (internal, b, 1);
11263 do_cleanups (back_to);
11264 }
11265
11266 /* Return count of debug registers needed to watch the given expression.
11267 If the watchpoint cannot be handled in hardware return zero. */
11268
11269 static int
11270 can_use_hardware_watchpoint (struct value *v)
11271 {
11272 int found_memory_cnt = 0;
11273 struct value *head = v;
11274
11275 /* Did the user specifically forbid us to use hardware watchpoints? */
11276 if (!can_use_hw_watchpoints)
11277 return 0;
11278
11279 /* Make sure that the value of the expression depends only upon
11280 memory contents, and values computed from them within GDB. If we
11281 find any register references or function calls, we can't use a
11282 hardware watchpoint.
11283
11284 The idea here is that evaluating an expression generates a series
11285 of values, one holding the value of every subexpression. (The
11286 expression a*b+c has five subexpressions: a, b, a*b, c, and
11287 a*b+c.) GDB's values hold almost enough information to establish
11288 the criteria given above --- they identify memory lvalues,
11289 register lvalues, computed values, etcetera. So we can evaluate
11290 the expression, and then scan the chain of values that leaves
11291 behind to decide whether we can detect any possible change to the
11292 expression's final value using only hardware watchpoints.
11293
11294 However, I don't think that the values returned by inferior
11295 function calls are special in any way. So this function may not
11296 notice that an expression involving an inferior function call
11297 can't be watched with hardware watchpoints. FIXME. */
11298 for (; v; v = value_next (v))
11299 {
11300 if (VALUE_LVAL (v) == lval_memory)
11301 {
11302 if (v != head && value_lazy (v))
11303 /* A lazy memory lvalue in the chain is one that GDB never
11304 needed to fetch; we either just used its address (e.g.,
11305 `a' in `a.b') or we never needed it at all (e.g., `a'
11306 in `a,b'). This doesn't apply to HEAD; if that is
11307 lazy then it was not readable, but watch it anyway. */
11308 ;
11309 else
11310 {
11311 /* Ahh, memory we actually used! Check if we can cover
11312 it with hardware watchpoints. */
11313 struct type *vtype = check_typedef (value_type (v));
11314
11315 /* We only watch structs and arrays if user asked for it
11316 explicitly, never if they just happen to appear in a
11317 middle of some value chain. */
11318 if (v == head
11319 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11320 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11321 {
11322 CORE_ADDR vaddr = value_address (v);
11323 int len;
11324 int num_regs;
11325
11326 len = (target_exact_watchpoints
11327 && is_scalar_type_recursive (vtype))?
11328 1 : TYPE_LENGTH (value_type (v));
11329
11330 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11331 if (!num_regs)
11332 return 0;
11333 else
11334 found_memory_cnt += num_regs;
11335 }
11336 }
11337 }
11338 else if (VALUE_LVAL (v) != not_lval
11339 && deprecated_value_modifiable (v) == 0)
11340 return 0; /* These are values from the history (e.g., $1). */
11341 else if (VALUE_LVAL (v) == lval_register)
11342 return 0; /* Cannot watch a register with a HW watchpoint. */
11343 }
11344
11345 /* The expression itself looks suitable for using a hardware
11346 watchpoint, but give the target machine a chance to reject it. */
11347 return found_memory_cnt;
11348 }
11349
11350 void
11351 watch_command_wrapper (char *arg, int from_tty, int internal)
11352 {
11353 watch_command_1 (arg, hw_write, from_tty, 0, internal);
11354 }
11355
11356 /* A helper function that looks for the "-location" argument and then
11357 calls watch_command_1. */
11358
11359 static void
11360 watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11361 {
11362 int just_location = 0;
11363
11364 if (arg
11365 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11366 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11367 {
11368 arg = skip_spaces (arg);
11369 just_location = 1;
11370 }
11371
11372 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
11373 }
11374
11375 static void
11376 watch_command (char *arg, int from_tty)
11377 {
11378 watch_maybe_just_location (arg, hw_write, from_tty);
11379 }
11380
11381 void
11382 rwatch_command_wrapper (char *arg, int from_tty, int internal)
11383 {
11384 watch_command_1 (arg, hw_read, from_tty, 0, internal);
11385 }
11386
11387 static void
11388 rwatch_command (char *arg, int from_tty)
11389 {
11390 watch_maybe_just_location (arg, hw_read, from_tty);
11391 }
11392
11393 void
11394 awatch_command_wrapper (char *arg, int from_tty, int internal)
11395 {
11396 watch_command_1 (arg, hw_access, from_tty, 0, internal);
11397 }
11398
11399 static void
11400 awatch_command (char *arg, int from_tty)
11401 {
11402 watch_maybe_just_location (arg, hw_access, from_tty);
11403 }
11404 \f
11405
11406 /* Helper routines for the until_command routine in infcmd.c. Here
11407 because it uses the mechanisms of breakpoints. */
11408
11409 struct until_break_command_continuation_args
11410 {
11411 struct breakpoint *breakpoint;
11412 struct breakpoint *breakpoint2;
11413 int thread_num;
11414 };
11415
11416 /* This function is called by fetch_inferior_event via the
11417 cmd_continuation pointer, to complete the until command. It takes
11418 care of cleaning up the temporary breakpoints set up by the until
11419 command. */
11420 static void
11421 until_break_command_continuation (void *arg, int err)
11422 {
11423 struct until_break_command_continuation_args *a = arg;
11424
11425 delete_breakpoint (a->breakpoint);
11426 if (a->breakpoint2)
11427 delete_breakpoint (a->breakpoint2);
11428 delete_longjmp_breakpoint (a->thread_num);
11429 }
11430
11431 void
11432 until_break_command (char *arg, int from_tty, int anywhere)
11433 {
11434 struct symtabs_and_lines sals;
11435 struct symtab_and_line sal;
11436 struct frame_info *frame;
11437 struct gdbarch *frame_gdbarch;
11438 struct frame_id stack_frame_id;
11439 struct frame_id caller_frame_id;
11440 struct breakpoint *breakpoint;
11441 struct breakpoint *breakpoint2 = NULL;
11442 struct cleanup *old_chain;
11443 int thread;
11444 struct thread_info *tp;
11445
11446 clear_proceed_status (0);
11447
11448 /* Set a breakpoint where the user wants it and at return from
11449 this function. */
11450
11451 if (last_displayed_sal_is_valid ())
11452 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
11453 get_last_displayed_symtab (),
11454 get_last_displayed_line ());
11455 else
11456 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
11457 (struct symtab *) NULL, 0);
11458
11459 if (sals.nelts != 1)
11460 error (_("Couldn't get information on specified line."));
11461
11462 sal = sals.sals[0];
11463 xfree (sals.sals); /* malloc'd, so freed. */
11464
11465 if (*arg)
11466 error (_("Junk at end of arguments."));
11467
11468 resolve_sal_pc (&sal);
11469
11470 tp = inferior_thread ();
11471 thread = tp->num;
11472
11473 old_chain = make_cleanup (null_cleanup, NULL);
11474
11475 /* Note linespec handling above invalidates the frame chain.
11476 Installing a breakpoint also invalidates the frame chain (as it
11477 may need to switch threads), so do any frame handling before
11478 that. */
11479
11480 frame = get_selected_frame (NULL);
11481 frame_gdbarch = get_frame_arch (frame);
11482 stack_frame_id = get_stack_frame_id (frame);
11483 caller_frame_id = frame_unwind_caller_id (frame);
11484
11485 /* Keep within the current frame, or in frames called by the current
11486 one. */
11487
11488 if (frame_id_p (caller_frame_id))
11489 {
11490 struct symtab_and_line sal2;
11491
11492 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11493 sal2.pc = frame_unwind_caller_pc (frame);
11494 breakpoint2 = set_momentary_breakpoint (frame_unwind_caller_arch (frame),
11495 sal2,
11496 caller_frame_id,
11497 bp_until);
11498 make_cleanup_delete_breakpoint (breakpoint2);
11499
11500 set_longjmp_breakpoint (tp, caller_frame_id);
11501 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
11502 }
11503
11504 /* set_momentary_breakpoint could invalidate FRAME. */
11505 frame = NULL;
11506
11507 if (anywhere)
11508 /* If the user told us to continue until a specified location,
11509 we don't specify a frame at which we need to stop. */
11510 breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11511 null_frame_id, bp_until);
11512 else
11513 /* Otherwise, specify the selected frame, because we want to stop
11514 only at the very same frame. */
11515 breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11516 stack_frame_id, bp_until);
11517 make_cleanup_delete_breakpoint (breakpoint);
11518
11519 proceed (-1, GDB_SIGNAL_DEFAULT);
11520
11521 /* If we are running asynchronously, and proceed call above has
11522 actually managed to start the target, arrange for breakpoints to
11523 be deleted when the target stops. Otherwise, we're already
11524 stopped and delete breakpoints via cleanup chain. */
11525
11526 if (target_can_async_p () && is_running (inferior_ptid))
11527 {
11528 struct until_break_command_continuation_args *args;
11529 args = xmalloc (sizeof (*args));
11530
11531 args->breakpoint = breakpoint;
11532 args->breakpoint2 = breakpoint2;
11533 args->thread_num = thread;
11534
11535 discard_cleanups (old_chain);
11536 add_continuation (inferior_thread (),
11537 until_break_command_continuation, args,
11538 xfree);
11539 }
11540 else
11541 do_cleanups (old_chain);
11542 }
11543
11544 /* This function attempts to parse an optional "if <cond>" clause
11545 from the arg string. If one is not found, it returns NULL.
11546
11547 Else, it returns a pointer to the condition string. (It does not
11548 attempt to evaluate the string against a particular block.) And,
11549 it updates arg to point to the first character following the parsed
11550 if clause in the arg string. */
11551
11552 char *
11553 ep_parse_optional_if_clause (char **arg)
11554 {
11555 char *cond_string;
11556
11557 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
11558 return NULL;
11559
11560 /* Skip the "if" keyword. */
11561 (*arg) += 2;
11562
11563 /* Skip any extra leading whitespace, and record the start of the
11564 condition string. */
11565 *arg = skip_spaces (*arg);
11566 cond_string = *arg;
11567
11568 /* Assume that the condition occupies the remainder of the arg
11569 string. */
11570 (*arg) += strlen (cond_string);
11571
11572 return cond_string;
11573 }
11574
11575 /* Commands to deal with catching events, such as signals, exceptions,
11576 process start/exit, etc. */
11577
11578 typedef enum
11579 {
11580 catch_fork_temporary, catch_vfork_temporary,
11581 catch_fork_permanent, catch_vfork_permanent
11582 }
11583 catch_fork_kind;
11584
11585 static void
11586 catch_fork_command_1 (char *arg, int from_tty,
11587 struct cmd_list_element *command)
11588 {
11589 struct gdbarch *gdbarch = get_current_arch ();
11590 char *cond_string = NULL;
11591 catch_fork_kind fork_kind;
11592 int tempflag;
11593
11594 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11595 tempflag = (fork_kind == catch_fork_temporary
11596 || fork_kind == catch_vfork_temporary);
11597
11598 if (!arg)
11599 arg = "";
11600 arg = skip_spaces (arg);
11601
11602 /* The allowed syntax is:
11603 catch [v]fork
11604 catch [v]fork if <cond>
11605
11606 First, check if there's an if clause. */
11607 cond_string = ep_parse_optional_if_clause (&arg);
11608
11609 if ((*arg != '\0') && !isspace (*arg))
11610 error (_("Junk at end of arguments."));
11611
11612 /* If this target supports it, create a fork or vfork catchpoint
11613 and enable reporting of such events. */
11614 switch (fork_kind)
11615 {
11616 case catch_fork_temporary:
11617 case catch_fork_permanent:
11618 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11619 &catch_fork_breakpoint_ops);
11620 break;
11621 case catch_vfork_temporary:
11622 case catch_vfork_permanent:
11623 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11624 &catch_vfork_breakpoint_ops);
11625 break;
11626 default:
11627 error (_("unsupported or unknown fork kind; cannot catch it"));
11628 break;
11629 }
11630 }
11631
11632 static void
11633 catch_exec_command_1 (char *arg, int from_tty,
11634 struct cmd_list_element *command)
11635 {
11636 struct exec_catchpoint *c;
11637 struct gdbarch *gdbarch = get_current_arch ();
11638 int tempflag;
11639 char *cond_string = NULL;
11640
11641 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11642
11643 if (!arg)
11644 arg = "";
11645 arg = skip_spaces (arg);
11646
11647 /* The allowed syntax is:
11648 catch exec
11649 catch exec if <cond>
11650
11651 First, check if there's an if clause. */
11652 cond_string = ep_parse_optional_if_clause (&arg);
11653
11654 if ((*arg != '\0') && !isspace (*arg))
11655 error (_("Junk at end of arguments."));
11656
11657 c = XNEW (struct exec_catchpoint);
11658 init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
11659 &catch_exec_breakpoint_ops);
11660 c->exec_pathname = NULL;
11661
11662 install_breakpoint (0, &c->base, 1);
11663 }
11664
11665 void
11666 init_ada_exception_breakpoint (struct breakpoint *b,
11667 struct gdbarch *gdbarch,
11668 struct symtab_and_line sal,
11669 char *addr_string,
11670 const struct breakpoint_ops *ops,
11671 int tempflag,
11672 int enabled,
11673 int from_tty)
11674 {
11675 if (from_tty)
11676 {
11677 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11678 if (!loc_gdbarch)
11679 loc_gdbarch = gdbarch;
11680
11681 describe_other_breakpoints (loc_gdbarch,
11682 sal.pspace, sal.pc, sal.section, -1);
11683 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11684 version for exception catchpoints, because two catchpoints
11685 used for different exception names will use the same address.
11686 In this case, a "breakpoint ... also set at..." warning is
11687 unproductive. Besides, the warning phrasing is also a bit
11688 inappropriate, we should use the word catchpoint, and tell
11689 the user what type of catchpoint it is. The above is good
11690 enough for now, though. */
11691 }
11692
11693 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
11694
11695 b->enable_state = enabled ? bp_enabled : bp_disabled;
11696 b->disposition = tempflag ? disp_del : disp_donttouch;
11697 b->addr_string = addr_string;
11698 b->language = language_ada;
11699 }
11700
11701 static void
11702 catch_command (char *arg, int from_tty)
11703 {
11704 error (_("Catch requires an event name."));
11705 }
11706 \f
11707
11708 static void
11709 tcatch_command (char *arg, int from_tty)
11710 {
11711 error (_("Catch requires an event name."));
11712 }
11713
11714 /* A qsort comparison function that sorts breakpoints in order. */
11715
11716 static int
11717 compare_breakpoints (const void *a, const void *b)
11718 {
11719 const breakpoint_p *ba = a;
11720 uintptr_t ua = (uintptr_t) *ba;
11721 const breakpoint_p *bb = b;
11722 uintptr_t ub = (uintptr_t) *bb;
11723
11724 if ((*ba)->number < (*bb)->number)
11725 return -1;
11726 else if ((*ba)->number > (*bb)->number)
11727 return 1;
11728
11729 /* Now sort by address, in case we see, e..g, two breakpoints with
11730 the number 0. */
11731 if (ua < ub)
11732 return -1;
11733 return ua > ub ? 1 : 0;
11734 }
11735
11736 /* Delete breakpoints by address or line. */
11737
11738 static void
11739 clear_command (char *arg, int from_tty)
11740 {
11741 struct breakpoint *b, *prev;
11742 VEC(breakpoint_p) *found = 0;
11743 int ix;
11744 int default_match;
11745 struct symtabs_and_lines sals;
11746 struct symtab_and_line sal;
11747 int i;
11748 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
11749
11750 if (arg)
11751 {
11752 sals = decode_line_with_current_source (arg,
11753 (DECODE_LINE_FUNFIRSTLINE
11754 | DECODE_LINE_LIST_MODE));
11755 make_cleanup (xfree, sals.sals);
11756 default_match = 0;
11757 }
11758 else
11759 {
11760 sals.sals = (struct symtab_and_line *)
11761 xmalloc (sizeof (struct symtab_and_line));
11762 make_cleanup (xfree, sals.sals);
11763 init_sal (&sal); /* Initialize to zeroes. */
11764
11765 /* Set sal's line, symtab, pc, and pspace to the values
11766 corresponding to the last call to print_frame_info. If the
11767 codepoint is not valid, this will set all the fields to 0. */
11768 get_last_displayed_sal (&sal);
11769 if (sal.symtab == 0)
11770 error (_("No source file specified."));
11771
11772 sals.sals[0] = sal;
11773 sals.nelts = 1;
11774
11775 default_match = 1;
11776 }
11777
11778 /* We don't call resolve_sal_pc here. That's not as bad as it
11779 seems, because all existing breakpoints typically have both
11780 file/line and pc set. So, if clear is given file/line, we can
11781 match this to existing breakpoint without obtaining pc at all.
11782
11783 We only support clearing given the address explicitly
11784 present in breakpoint table. Say, we've set breakpoint
11785 at file:line. There were several PC values for that file:line,
11786 due to optimization, all in one block.
11787
11788 We've picked one PC value. If "clear" is issued with another
11789 PC corresponding to the same file:line, the breakpoint won't
11790 be cleared. We probably can still clear the breakpoint, but
11791 since the other PC value is never presented to user, user
11792 can only find it by guessing, and it does not seem important
11793 to support that. */
11794
11795 /* For each line spec given, delete bps which correspond to it. Do
11796 it in two passes, solely to preserve the current behavior that
11797 from_tty is forced true if we delete more than one
11798 breakpoint. */
11799
11800 found = NULL;
11801 make_cleanup (VEC_cleanup (breakpoint_p), &found);
11802 for (i = 0; i < sals.nelts; i++)
11803 {
11804 const char *sal_fullname;
11805
11806 /* If exact pc given, clear bpts at that pc.
11807 If line given (pc == 0), clear all bpts on specified line.
11808 If defaulting, clear all bpts on default line
11809 or at default pc.
11810
11811 defaulting sal.pc != 0 tests to do
11812
11813 0 1 pc
11814 1 1 pc _and_ line
11815 0 0 line
11816 1 0 <can't happen> */
11817
11818 sal = sals.sals[i];
11819 sal_fullname = (sal.symtab == NULL
11820 ? NULL : symtab_to_fullname (sal.symtab));
11821
11822 /* Find all matching breakpoints and add them to 'found'. */
11823 ALL_BREAKPOINTS (b)
11824 {
11825 int match = 0;
11826 /* Are we going to delete b? */
11827 if (b->type != bp_none && !is_watchpoint (b))
11828 {
11829 struct bp_location *loc = b->loc;
11830 for (; loc; loc = loc->next)
11831 {
11832 /* If the user specified file:line, don't allow a PC
11833 match. This matches historical gdb behavior. */
11834 int pc_match = (!sal.explicit_line
11835 && sal.pc
11836 && (loc->pspace == sal.pspace)
11837 && (loc->address == sal.pc)
11838 && (!section_is_overlay (loc->section)
11839 || loc->section == sal.section));
11840 int line_match = 0;
11841
11842 if ((default_match || sal.explicit_line)
11843 && loc->symtab != NULL
11844 && sal_fullname != NULL
11845 && sal.pspace == loc->pspace
11846 && loc->line_number == sal.line
11847 && filename_cmp (symtab_to_fullname (loc->symtab),
11848 sal_fullname) == 0)
11849 line_match = 1;
11850
11851 if (pc_match || line_match)
11852 {
11853 match = 1;
11854 break;
11855 }
11856 }
11857 }
11858
11859 if (match)
11860 VEC_safe_push(breakpoint_p, found, b);
11861 }
11862 }
11863
11864 /* Now go thru the 'found' chain and delete them. */
11865 if (VEC_empty(breakpoint_p, found))
11866 {
11867 if (arg)
11868 error (_("No breakpoint at %s."), arg);
11869 else
11870 error (_("No breakpoint at this line."));
11871 }
11872
11873 /* Remove duplicates from the vec. */
11874 qsort (VEC_address (breakpoint_p, found),
11875 VEC_length (breakpoint_p, found),
11876 sizeof (breakpoint_p),
11877 compare_breakpoints);
11878 prev = VEC_index (breakpoint_p, found, 0);
11879 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
11880 {
11881 if (b == prev)
11882 {
11883 VEC_ordered_remove (breakpoint_p, found, ix);
11884 --ix;
11885 }
11886 }
11887
11888 if (VEC_length(breakpoint_p, found) > 1)
11889 from_tty = 1; /* Always report if deleted more than one. */
11890 if (from_tty)
11891 {
11892 if (VEC_length(breakpoint_p, found) == 1)
11893 printf_unfiltered (_("Deleted breakpoint "));
11894 else
11895 printf_unfiltered (_("Deleted breakpoints "));
11896 }
11897
11898 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
11899 {
11900 if (from_tty)
11901 printf_unfiltered ("%d ", b->number);
11902 delete_breakpoint (b);
11903 }
11904 if (from_tty)
11905 putchar_unfiltered ('\n');
11906
11907 do_cleanups (cleanups);
11908 }
11909 \f
11910 /* Delete breakpoint in BS if they are `delete' breakpoints and
11911 all breakpoints that are marked for deletion, whether hit or not.
11912 This is called after any breakpoint is hit, or after errors. */
11913
11914 void
11915 breakpoint_auto_delete (bpstat bs)
11916 {
11917 struct breakpoint *b, *b_tmp;
11918
11919 for (; bs; bs = bs->next)
11920 if (bs->breakpoint_at
11921 && bs->breakpoint_at->disposition == disp_del
11922 && bs->stop)
11923 delete_breakpoint (bs->breakpoint_at);
11924
11925 ALL_BREAKPOINTS_SAFE (b, b_tmp)
11926 {
11927 if (b->disposition == disp_del_at_next_stop)
11928 delete_breakpoint (b);
11929 }
11930 }
11931
11932 /* A comparison function for bp_location AP and BP being interfaced to
11933 qsort. Sort elements primarily by their ADDRESS (no matter what
11934 does breakpoint_address_is_meaningful say for its OWNER),
11935 secondarily by ordering first permanent elements and
11936 terciarily just ensuring the array is sorted stable way despite
11937 qsort being an unstable algorithm. */
11938
11939 static int
11940 bp_location_compare (const void *ap, const void *bp)
11941 {
11942 struct bp_location *a = *(void **) ap;
11943 struct bp_location *b = *(void **) bp;
11944
11945 if (a->address != b->address)
11946 return (a->address > b->address) - (a->address < b->address);
11947
11948 /* Sort locations at the same address by their pspace number, keeping
11949 locations of the same inferior (in a multi-inferior environment)
11950 grouped. */
11951
11952 if (a->pspace->num != b->pspace->num)
11953 return ((a->pspace->num > b->pspace->num)
11954 - (a->pspace->num < b->pspace->num));
11955
11956 /* Sort permanent breakpoints first. */
11957 if (a->permanent != b->permanent)
11958 return (a->permanent < b->permanent) - (a->permanent > b->permanent);
11959
11960 /* Make the internal GDB representation stable across GDB runs
11961 where A and B memory inside GDB can differ. Breakpoint locations of
11962 the same type at the same address can be sorted in arbitrary order. */
11963
11964 if (a->owner->number != b->owner->number)
11965 return ((a->owner->number > b->owner->number)
11966 - (a->owner->number < b->owner->number));
11967
11968 return (a > b) - (a < b);
11969 }
11970
11971 /* Set bp_location_placed_address_before_address_max and
11972 bp_location_shadow_len_after_address_max according to the current
11973 content of the bp_location array. */
11974
11975 static void
11976 bp_location_target_extensions_update (void)
11977 {
11978 struct bp_location *bl, **blp_tmp;
11979
11980 bp_location_placed_address_before_address_max = 0;
11981 bp_location_shadow_len_after_address_max = 0;
11982
11983 ALL_BP_LOCATIONS (bl, blp_tmp)
11984 {
11985 CORE_ADDR start, end, addr;
11986
11987 if (!bp_location_has_shadow (bl))
11988 continue;
11989
11990 start = bl->target_info.placed_address;
11991 end = start + bl->target_info.shadow_len;
11992
11993 gdb_assert (bl->address >= start);
11994 addr = bl->address - start;
11995 if (addr > bp_location_placed_address_before_address_max)
11996 bp_location_placed_address_before_address_max = addr;
11997
11998 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
11999
12000 gdb_assert (bl->address < end);
12001 addr = end - bl->address;
12002 if (addr > bp_location_shadow_len_after_address_max)
12003 bp_location_shadow_len_after_address_max = addr;
12004 }
12005 }
12006
12007 /* Download tracepoint locations if they haven't been. */
12008
12009 static void
12010 download_tracepoint_locations (void)
12011 {
12012 struct breakpoint *b;
12013 struct cleanup *old_chain;
12014
12015 if (!target_can_download_tracepoint ())
12016 return;
12017
12018 old_chain = save_current_space_and_thread ();
12019
12020 ALL_TRACEPOINTS (b)
12021 {
12022 struct bp_location *bl;
12023 struct tracepoint *t;
12024 int bp_location_downloaded = 0;
12025
12026 if ((b->type == bp_fast_tracepoint
12027 ? !may_insert_fast_tracepoints
12028 : !may_insert_tracepoints))
12029 continue;
12030
12031 for (bl = b->loc; bl; bl = bl->next)
12032 {
12033 /* In tracepoint, locations are _never_ duplicated, so
12034 should_be_inserted is equivalent to
12035 unduplicated_should_be_inserted. */
12036 if (!should_be_inserted (bl) || bl->inserted)
12037 continue;
12038
12039 switch_to_program_space_and_thread (bl->pspace);
12040
12041 target_download_tracepoint (bl);
12042
12043 bl->inserted = 1;
12044 bp_location_downloaded = 1;
12045 }
12046 t = (struct tracepoint *) b;
12047 t->number_on_target = b->number;
12048 if (bp_location_downloaded)
12049 observer_notify_breakpoint_modified (b);
12050 }
12051
12052 do_cleanups (old_chain);
12053 }
12054
12055 /* Swap the insertion/duplication state between two locations. */
12056
12057 static void
12058 swap_insertion (struct bp_location *left, struct bp_location *right)
12059 {
12060 const int left_inserted = left->inserted;
12061 const int left_duplicate = left->duplicate;
12062 const int left_needs_update = left->needs_update;
12063 const struct bp_target_info left_target_info = left->target_info;
12064
12065 /* Locations of tracepoints can never be duplicated. */
12066 if (is_tracepoint (left->owner))
12067 gdb_assert (!left->duplicate);
12068 if (is_tracepoint (right->owner))
12069 gdb_assert (!right->duplicate);
12070
12071 left->inserted = right->inserted;
12072 left->duplicate = right->duplicate;
12073 left->needs_update = right->needs_update;
12074 left->target_info = right->target_info;
12075 right->inserted = left_inserted;
12076 right->duplicate = left_duplicate;
12077 right->needs_update = left_needs_update;
12078 right->target_info = left_target_info;
12079 }
12080
12081 /* Force the re-insertion of the locations at ADDRESS. This is called
12082 once a new/deleted/modified duplicate location is found and we are evaluating
12083 conditions on the target's side. Such conditions need to be updated on
12084 the target. */
12085
12086 static void
12087 force_breakpoint_reinsertion (struct bp_location *bl)
12088 {
12089 struct bp_location **locp = NULL, **loc2p;
12090 struct bp_location *loc;
12091 CORE_ADDR address = 0;
12092 int pspace_num;
12093
12094 address = bl->address;
12095 pspace_num = bl->pspace->num;
12096
12097 /* This is only meaningful if the target is
12098 evaluating conditions and if the user has
12099 opted for condition evaluation on the target's
12100 side. */
12101 if (gdb_evaluates_breakpoint_condition_p ()
12102 || !target_supports_evaluation_of_breakpoint_conditions ())
12103 return;
12104
12105 /* Flag all breakpoint locations with this address and
12106 the same program space as the location
12107 as "its condition has changed". We need to
12108 update the conditions on the target's side. */
12109 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
12110 {
12111 loc = *loc2p;
12112
12113 if (!is_breakpoint (loc->owner)
12114 || pspace_num != loc->pspace->num)
12115 continue;
12116
12117 /* Flag the location appropriately. We use a different state to
12118 let everyone know that we already updated the set of locations
12119 with addr bl->address and program space bl->pspace. This is so
12120 we don't have to keep calling these functions just to mark locations
12121 that have already been marked. */
12122 loc->condition_changed = condition_updated;
12123
12124 /* Free the agent expression bytecode as well. We will compute
12125 it later on. */
12126 if (loc->cond_bytecode)
12127 {
12128 free_agent_expr (loc->cond_bytecode);
12129 loc->cond_bytecode = NULL;
12130 }
12131 }
12132 }
12133 /* Called whether new breakpoints are created, or existing breakpoints
12134 deleted, to update the global location list and recompute which
12135 locations are duplicate of which.
12136
12137 The INSERT_MODE flag determines whether locations may not, may, or
12138 shall be inserted now. See 'enum ugll_insert_mode' for more
12139 info. */
12140
12141 static void
12142 update_global_location_list (enum ugll_insert_mode insert_mode)
12143 {
12144 struct breakpoint *b;
12145 struct bp_location **locp, *loc;
12146 struct cleanup *cleanups;
12147 /* Last breakpoint location address that was marked for update. */
12148 CORE_ADDR last_addr = 0;
12149 /* Last breakpoint location program space that was marked for update. */
12150 int last_pspace_num = -1;
12151
12152 /* Used in the duplicates detection below. When iterating over all
12153 bp_locations, points to the first bp_location of a given address.
12154 Breakpoints and watchpoints of different types are never
12155 duplicates of each other. Keep one pointer for each type of
12156 breakpoint/watchpoint, so we only need to loop over all locations
12157 once. */
12158 struct bp_location *bp_loc_first; /* breakpoint */
12159 struct bp_location *wp_loc_first; /* hardware watchpoint */
12160 struct bp_location *awp_loc_first; /* access watchpoint */
12161 struct bp_location *rwp_loc_first; /* read watchpoint */
12162
12163 /* Saved former bp_location array which we compare against the newly
12164 built bp_location from the current state of ALL_BREAKPOINTS. */
12165 struct bp_location **old_location, **old_locp;
12166 unsigned old_location_count;
12167
12168 old_location = bp_location;
12169 old_location_count = bp_location_count;
12170 bp_location = NULL;
12171 bp_location_count = 0;
12172 cleanups = make_cleanup (xfree, old_location);
12173
12174 ALL_BREAKPOINTS (b)
12175 for (loc = b->loc; loc; loc = loc->next)
12176 bp_location_count++;
12177
12178 bp_location = xmalloc (sizeof (*bp_location) * bp_location_count);
12179 locp = bp_location;
12180 ALL_BREAKPOINTS (b)
12181 for (loc = b->loc; loc; loc = loc->next)
12182 *locp++ = loc;
12183 qsort (bp_location, bp_location_count, sizeof (*bp_location),
12184 bp_location_compare);
12185
12186 bp_location_target_extensions_update ();
12187
12188 /* Identify bp_location instances that are no longer present in the
12189 new list, and therefore should be freed. Note that it's not
12190 necessary that those locations should be removed from inferior --
12191 if there's another location at the same address (previously
12192 marked as duplicate), we don't need to remove/insert the
12193 location.
12194
12195 LOCP is kept in sync with OLD_LOCP, each pointing to the current
12196 and former bp_location array state respectively. */
12197
12198 locp = bp_location;
12199 for (old_locp = old_location; old_locp < old_location + old_location_count;
12200 old_locp++)
12201 {
12202 struct bp_location *old_loc = *old_locp;
12203 struct bp_location **loc2p;
12204
12205 /* Tells if 'old_loc' is found among the new locations. If
12206 not, we have to free it. */
12207 int found_object = 0;
12208 /* Tells if the location should remain inserted in the target. */
12209 int keep_in_target = 0;
12210 int removed = 0;
12211
12212 /* Skip LOCP entries which will definitely never be needed.
12213 Stop either at or being the one matching OLD_LOC. */
12214 while (locp < bp_location + bp_location_count
12215 && (*locp)->address < old_loc->address)
12216 locp++;
12217
12218 for (loc2p = locp;
12219 (loc2p < bp_location + bp_location_count
12220 && (*loc2p)->address == old_loc->address);
12221 loc2p++)
12222 {
12223 /* Check if this is a new/duplicated location or a duplicated
12224 location that had its condition modified. If so, we want to send
12225 its condition to the target if evaluation of conditions is taking
12226 place there. */
12227 if ((*loc2p)->condition_changed == condition_modified
12228 && (last_addr != old_loc->address
12229 || last_pspace_num != old_loc->pspace->num))
12230 {
12231 force_breakpoint_reinsertion (*loc2p);
12232 last_pspace_num = old_loc->pspace->num;
12233 }
12234
12235 if (*loc2p == old_loc)
12236 found_object = 1;
12237 }
12238
12239 /* We have already handled this address, update it so that we don't
12240 have to go through updates again. */
12241 last_addr = old_loc->address;
12242
12243 /* Target-side condition evaluation: Handle deleted locations. */
12244 if (!found_object)
12245 force_breakpoint_reinsertion (old_loc);
12246
12247 /* If this location is no longer present, and inserted, look if
12248 there's maybe a new location at the same address. If so,
12249 mark that one inserted, and don't remove this one. This is
12250 needed so that we don't have a time window where a breakpoint
12251 at certain location is not inserted. */
12252
12253 if (old_loc->inserted)
12254 {
12255 /* If the location is inserted now, we might have to remove
12256 it. */
12257
12258 if (found_object && should_be_inserted (old_loc))
12259 {
12260 /* The location is still present in the location list,
12261 and still should be inserted. Don't do anything. */
12262 keep_in_target = 1;
12263 }
12264 else
12265 {
12266 /* This location still exists, but it won't be kept in the
12267 target since it may have been disabled. We proceed to
12268 remove its target-side condition. */
12269
12270 /* The location is either no longer present, or got
12271 disabled. See if there's another location at the
12272 same address, in which case we don't need to remove
12273 this one from the target. */
12274
12275 /* OLD_LOC comes from existing struct breakpoint. */
12276 if (breakpoint_address_is_meaningful (old_loc->owner))
12277 {
12278 for (loc2p = locp;
12279 (loc2p < bp_location + bp_location_count
12280 && (*loc2p)->address == old_loc->address);
12281 loc2p++)
12282 {
12283 struct bp_location *loc2 = *loc2p;
12284
12285 if (breakpoint_locations_match (loc2, old_loc))
12286 {
12287 /* Read watchpoint locations are switched to
12288 access watchpoints, if the former are not
12289 supported, but the latter are. */
12290 if (is_hardware_watchpoint (old_loc->owner))
12291 {
12292 gdb_assert (is_hardware_watchpoint (loc2->owner));
12293 loc2->watchpoint_type = old_loc->watchpoint_type;
12294 }
12295
12296 /* loc2 is a duplicated location. We need to check
12297 if it should be inserted in case it will be
12298 unduplicated. */
12299 if (loc2 != old_loc
12300 && unduplicated_should_be_inserted (loc2))
12301 {
12302 swap_insertion (old_loc, loc2);
12303 keep_in_target = 1;
12304 break;
12305 }
12306 }
12307 }
12308 }
12309 }
12310
12311 if (!keep_in_target)
12312 {
12313 if (remove_breakpoint (old_loc, mark_uninserted))
12314 {
12315 /* This is just about all we can do. We could keep
12316 this location on the global list, and try to
12317 remove it next time, but there's no particular
12318 reason why we will succeed next time.
12319
12320 Note that at this point, old_loc->owner is still
12321 valid, as delete_breakpoint frees the breakpoint
12322 only after calling us. */
12323 printf_filtered (_("warning: Error removing "
12324 "breakpoint %d\n"),
12325 old_loc->owner->number);
12326 }
12327 removed = 1;
12328 }
12329 }
12330
12331 if (!found_object)
12332 {
12333 if (removed && target_is_non_stop_p ()
12334 && need_moribund_for_location_type (old_loc))
12335 {
12336 /* This location was removed from the target. In
12337 non-stop mode, a race condition is possible where
12338 we've removed a breakpoint, but stop events for that
12339 breakpoint are already queued and will arrive later.
12340 We apply an heuristic to be able to distinguish such
12341 SIGTRAPs from other random SIGTRAPs: we keep this
12342 breakpoint location for a bit, and will retire it
12343 after we see some number of events. The theory here
12344 is that reporting of events should, "on the average",
12345 be fair, so after a while we'll see events from all
12346 threads that have anything of interest, and no longer
12347 need to keep this breakpoint location around. We
12348 don't hold locations forever so to reduce chances of
12349 mistaking a non-breakpoint SIGTRAP for a breakpoint
12350 SIGTRAP.
12351
12352 The heuristic failing can be disastrous on
12353 decr_pc_after_break targets.
12354
12355 On decr_pc_after_break targets, like e.g., x86-linux,
12356 if we fail to recognize a late breakpoint SIGTRAP,
12357 because events_till_retirement has reached 0 too
12358 soon, we'll fail to do the PC adjustment, and report
12359 a random SIGTRAP to the user. When the user resumes
12360 the inferior, it will most likely immediately crash
12361 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
12362 corrupted, because of being resumed e.g., in the
12363 middle of a multi-byte instruction, or skipped a
12364 one-byte instruction. This was actually seen happen
12365 on native x86-linux, and should be less rare on
12366 targets that do not support new thread events, like
12367 remote, due to the heuristic depending on
12368 thread_count.
12369
12370 Mistaking a random SIGTRAP for a breakpoint trap
12371 causes similar symptoms (PC adjustment applied when
12372 it shouldn't), but then again, playing with SIGTRAPs
12373 behind the debugger's back is asking for trouble.
12374
12375 Since hardware watchpoint traps are always
12376 distinguishable from other traps, so we don't need to
12377 apply keep hardware watchpoint moribund locations
12378 around. We simply always ignore hardware watchpoint
12379 traps we can no longer explain. */
12380
12381 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12382 old_loc->owner = NULL;
12383
12384 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
12385 }
12386 else
12387 {
12388 old_loc->owner = NULL;
12389 decref_bp_location (&old_loc);
12390 }
12391 }
12392 }
12393
12394 /* Rescan breakpoints at the same address and section, marking the
12395 first one as "first" and any others as "duplicates". This is so
12396 that the bpt instruction is only inserted once. If we have a
12397 permanent breakpoint at the same place as BPT, make that one the
12398 official one, and the rest as duplicates. Permanent breakpoints
12399 are sorted first for the same address.
12400
12401 Do the same for hardware watchpoints, but also considering the
12402 watchpoint's type (regular/access/read) and length. */
12403
12404 bp_loc_first = NULL;
12405 wp_loc_first = NULL;
12406 awp_loc_first = NULL;
12407 rwp_loc_first = NULL;
12408 ALL_BP_LOCATIONS (loc, locp)
12409 {
12410 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12411 non-NULL. */
12412 struct bp_location **loc_first_p;
12413 b = loc->owner;
12414
12415 if (!unduplicated_should_be_inserted (loc)
12416 || !breakpoint_address_is_meaningful (b)
12417 /* Don't detect duplicate for tracepoint locations because they are
12418 never duplicated. See the comments in field `duplicate' of
12419 `struct bp_location'. */
12420 || is_tracepoint (b))
12421 {
12422 /* Clear the condition modification flag. */
12423 loc->condition_changed = condition_unchanged;
12424 continue;
12425 }
12426
12427 if (b->type == bp_hardware_watchpoint)
12428 loc_first_p = &wp_loc_first;
12429 else if (b->type == bp_read_watchpoint)
12430 loc_first_p = &rwp_loc_first;
12431 else if (b->type == bp_access_watchpoint)
12432 loc_first_p = &awp_loc_first;
12433 else
12434 loc_first_p = &bp_loc_first;
12435
12436 if (*loc_first_p == NULL
12437 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12438 || !breakpoint_locations_match (loc, *loc_first_p))
12439 {
12440 *loc_first_p = loc;
12441 loc->duplicate = 0;
12442
12443 if (is_breakpoint (loc->owner) && loc->condition_changed)
12444 {
12445 loc->needs_update = 1;
12446 /* Clear the condition modification flag. */
12447 loc->condition_changed = condition_unchanged;
12448 }
12449 continue;
12450 }
12451
12452
12453 /* This and the above ensure the invariant that the first location
12454 is not duplicated, and is the inserted one.
12455 All following are marked as duplicated, and are not inserted. */
12456 if (loc->inserted)
12457 swap_insertion (loc, *loc_first_p);
12458 loc->duplicate = 1;
12459
12460 /* Clear the condition modification flag. */
12461 loc->condition_changed = condition_unchanged;
12462 }
12463
12464 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
12465 {
12466 if (insert_mode != UGLL_DONT_INSERT)
12467 insert_breakpoint_locations ();
12468 else
12469 {
12470 /* Even though the caller told us to not insert new
12471 locations, we may still need to update conditions on the
12472 target's side of breakpoints that were already inserted
12473 if the target is evaluating breakpoint conditions. We
12474 only update conditions for locations that are marked
12475 "needs_update". */
12476 update_inserted_breakpoint_locations ();
12477 }
12478 }
12479
12480 if (insert_mode != UGLL_DONT_INSERT)
12481 download_tracepoint_locations ();
12482
12483 do_cleanups (cleanups);
12484 }
12485
12486 void
12487 breakpoint_retire_moribund (void)
12488 {
12489 struct bp_location *loc;
12490 int ix;
12491
12492 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12493 if (--(loc->events_till_retirement) == 0)
12494 {
12495 decref_bp_location (&loc);
12496 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12497 --ix;
12498 }
12499 }
12500
12501 static void
12502 update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
12503 {
12504
12505 TRY
12506 {
12507 update_global_location_list (insert_mode);
12508 }
12509 CATCH (e, RETURN_MASK_ERROR)
12510 {
12511 }
12512 END_CATCH
12513 }
12514
12515 /* Clear BKP from a BPS. */
12516
12517 static void
12518 bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12519 {
12520 bpstat bs;
12521
12522 for (bs = bps; bs; bs = bs->next)
12523 if (bs->breakpoint_at == bpt)
12524 {
12525 bs->breakpoint_at = NULL;
12526 bs->old_val = NULL;
12527 /* bs->commands will be freed later. */
12528 }
12529 }
12530
12531 /* Callback for iterate_over_threads. */
12532 static int
12533 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12534 {
12535 struct breakpoint *bpt = data;
12536
12537 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12538 return 0;
12539 }
12540
12541 /* Helper for breakpoint and tracepoint breakpoint_ops->mention
12542 callbacks. */
12543
12544 static void
12545 say_where (struct breakpoint *b)
12546 {
12547 struct value_print_options opts;
12548
12549 get_user_print_options (&opts);
12550
12551 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12552 single string. */
12553 if (b->loc == NULL)
12554 {
12555 printf_filtered (_(" (%s) pending."), b->addr_string);
12556 }
12557 else
12558 {
12559 if (opts.addressprint || b->loc->symtab == NULL)
12560 {
12561 printf_filtered (" at ");
12562 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12563 gdb_stdout);
12564 }
12565 if (b->loc->symtab != NULL)
12566 {
12567 /* If there is a single location, we can print the location
12568 more nicely. */
12569 if (b->loc->next == NULL)
12570 printf_filtered (": file %s, line %d.",
12571 symtab_to_filename_for_display (b->loc->symtab),
12572 b->loc->line_number);
12573 else
12574 /* This is not ideal, but each location may have a
12575 different file name, and this at least reflects the
12576 real situation somewhat. */
12577 printf_filtered (": %s.", b->addr_string);
12578 }
12579
12580 if (b->loc->next)
12581 {
12582 struct bp_location *loc = b->loc;
12583 int n = 0;
12584 for (; loc; loc = loc->next)
12585 ++n;
12586 printf_filtered (" (%d locations)", n);
12587 }
12588 }
12589 }
12590
12591 /* Default bp_location_ops methods. */
12592
12593 static void
12594 bp_location_dtor (struct bp_location *self)
12595 {
12596 xfree (self->cond);
12597 if (self->cond_bytecode)
12598 free_agent_expr (self->cond_bytecode);
12599 xfree (self->function_name);
12600
12601 VEC_free (agent_expr_p, self->target_info.conditions);
12602 VEC_free (agent_expr_p, self->target_info.tcommands);
12603 }
12604
12605 static const struct bp_location_ops bp_location_ops =
12606 {
12607 bp_location_dtor
12608 };
12609
12610 /* Default breakpoint_ops methods all breakpoint_ops ultimately
12611 inherit from. */
12612
12613 static void
12614 base_breakpoint_dtor (struct breakpoint *self)
12615 {
12616 decref_counted_command_line (&self->commands);
12617 xfree (self->cond_string);
12618 xfree (self->extra_string);
12619 xfree (self->addr_string);
12620 xfree (self->filter);
12621 xfree (self->addr_string_range_end);
12622 }
12623
12624 static struct bp_location *
12625 base_breakpoint_allocate_location (struct breakpoint *self)
12626 {
12627 struct bp_location *loc;
12628
12629 loc = XNEW (struct bp_location);
12630 init_bp_location (loc, &bp_location_ops, self);
12631 return loc;
12632 }
12633
12634 static void
12635 base_breakpoint_re_set (struct breakpoint *b)
12636 {
12637 /* Nothing to re-set. */
12638 }
12639
12640 #define internal_error_pure_virtual_called() \
12641 gdb_assert_not_reached ("pure virtual function called")
12642
12643 static int
12644 base_breakpoint_insert_location (struct bp_location *bl)
12645 {
12646 internal_error_pure_virtual_called ();
12647 }
12648
12649 static int
12650 base_breakpoint_remove_location (struct bp_location *bl)
12651 {
12652 internal_error_pure_virtual_called ();
12653 }
12654
12655 static int
12656 base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12657 struct address_space *aspace,
12658 CORE_ADDR bp_addr,
12659 const struct target_waitstatus *ws)
12660 {
12661 internal_error_pure_virtual_called ();
12662 }
12663
12664 static void
12665 base_breakpoint_check_status (bpstat bs)
12666 {
12667 /* Always stop. */
12668 }
12669
12670 /* A "works_in_software_mode" breakpoint_ops method that just internal
12671 errors. */
12672
12673 static int
12674 base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12675 {
12676 internal_error_pure_virtual_called ();
12677 }
12678
12679 /* A "resources_needed" breakpoint_ops method that just internal
12680 errors. */
12681
12682 static int
12683 base_breakpoint_resources_needed (const struct bp_location *bl)
12684 {
12685 internal_error_pure_virtual_called ();
12686 }
12687
12688 static enum print_stop_action
12689 base_breakpoint_print_it (bpstat bs)
12690 {
12691 internal_error_pure_virtual_called ();
12692 }
12693
12694 static void
12695 base_breakpoint_print_one_detail (const struct breakpoint *self,
12696 struct ui_out *uiout)
12697 {
12698 /* nothing */
12699 }
12700
12701 static void
12702 base_breakpoint_print_mention (struct breakpoint *b)
12703 {
12704 internal_error_pure_virtual_called ();
12705 }
12706
12707 static void
12708 base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12709 {
12710 internal_error_pure_virtual_called ();
12711 }
12712
12713 static void
12714 base_breakpoint_create_sals_from_address (char **arg,
12715 struct linespec_result *canonical,
12716 enum bptype type_wanted,
12717 char *addr_start,
12718 char **copy_arg)
12719 {
12720 internal_error_pure_virtual_called ();
12721 }
12722
12723 static void
12724 base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12725 struct linespec_result *c,
12726 char *cond_string,
12727 char *extra_string,
12728 enum bptype type_wanted,
12729 enum bpdisp disposition,
12730 int thread,
12731 int task, int ignore_count,
12732 const struct breakpoint_ops *o,
12733 int from_tty, int enabled,
12734 int internal, unsigned flags)
12735 {
12736 internal_error_pure_virtual_called ();
12737 }
12738
12739 static void
12740 base_breakpoint_decode_linespec (struct breakpoint *b, char **s,
12741 struct symtabs_and_lines *sals)
12742 {
12743 internal_error_pure_virtual_called ();
12744 }
12745
12746 /* The default 'explains_signal' method. */
12747
12748 static int
12749 base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
12750 {
12751 return 1;
12752 }
12753
12754 /* The default "after_condition_true" method. */
12755
12756 static void
12757 base_breakpoint_after_condition_true (struct bpstats *bs)
12758 {
12759 /* Nothing to do. */
12760 }
12761
12762 struct breakpoint_ops base_breakpoint_ops =
12763 {
12764 base_breakpoint_dtor,
12765 base_breakpoint_allocate_location,
12766 base_breakpoint_re_set,
12767 base_breakpoint_insert_location,
12768 base_breakpoint_remove_location,
12769 base_breakpoint_breakpoint_hit,
12770 base_breakpoint_check_status,
12771 base_breakpoint_resources_needed,
12772 base_breakpoint_works_in_software_mode,
12773 base_breakpoint_print_it,
12774 NULL,
12775 base_breakpoint_print_one_detail,
12776 base_breakpoint_print_mention,
12777 base_breakpoint_print_recreate,
12778 base_breakpoint_create_sals_from_address,
12779 base_breakpoint_create_breakpoints_sal,
12780 base_breakpoint_decode_linespec,
12781 base_breakpoint_explains_signal,
12782 base_breakpoint_after_condition_true,
12783 };
12784
12785 /* Default breakpoint_ops methods. */
12786
12787 static void
12788 bkpt_re_set (struct breakpoint *b)
12789 {
12790 /* FIXME: is this still reachable? */
12791 if (b->addr_string == NULL)
12792 {
12793 /* Anything without a string can't be re-set. */
12794 delete_breakpoint (b);
12795 return;
12796 }
12797
12798 breakpoint_re_set_default (b);
12799 }
12800
12801 static int
12802 bkpt_insert_location (struct bp_location *bl)
12803 {
12804 if (bl->loc_type == bp_loc_hardware_breakpoint)
12805 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
12806 else
12807 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
12808 }
12809
12810 static int
12811 bkpt_remove_location (struct bp_location *bl)
12812 {
12813 if (bl->loc_type == bp_loc_hardware_breakpoint)
12814 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12815 else
12816 return target_remove_breakpoint (bl->gdbarch, &bl->target_info);
12817 }
12818
12819 static int
12820 bkpt_breakpoint_hit (const struct bp_location *bl,
12821 struct address_space *aspace, CORE_ADDR bp_addr,
12822 const struct target_waitstatus *ws)
12823 {
12824 if (ws->kind != TARGET_WAITKIND_STOPPED
12825 || ws->value.sig != GDB_SIGNAL_TRAP)
12826 return 0;
12827
12828 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12829 aspace, bp_addr))
12830 return 0;
12831
12832 if (overlay_debugging /* unmapped overlay section */
12833 && section_is_overlay (bl->section)
12834 && !section_is_mapped (bl->section))
12835 return 0;
12836
12837 return 1;
12838 }
12839
12840 static int
12841 dprintf_breakpoint_hit (const struct bp_location *bl,
12842 struct address_space *aspace, CORE_ADDR bp_addr,
12843 const struct target_waitstatus *ws)
12844 {
12845 if (dprintf_style == dprintf_style_agent
12846 && target_can_run_breakpoint_commands ())
12847 {
12848 /* An agent-style dprintf never causes a stop. If we see a trap
12849 for this address it must be for a breakpoint that happens to
12850 be set at the same address. */
12851 return 0;
12852 }
12853
12854 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
12855 }
12856
12857 static int
12858 bkpt_resources_needed (const struct bp_location *bl)
12859 {
12860 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12861
12862 return 1;
12863 }
12864
12865 static enum print_stop_action
12866 bkpt_print_it (bpstat bs)
12867 {
12868 struct breakpoint *b;
12869 const struct bp_location *bl;
12870 int bp_temp;
12871 struct ui_out *uiout = current_uiout;
12872
12873 gdb_assert (bs->bp_location_at != NULL);
12874
12875 bl = bs->bp_location_at;
12876 b = bs->breakpoint_at;
12877
12878 bp_temp = b->disposition == disp_del;
12879 if (bl->address != bl->requested_address)
12880 breakpoint_adjustment_warning (bl->requested_address,
12881 bl->address,
12882 b->number, 1);
12883 annotate_breakpoint (b->number);
12884 if (bp_temp)
12885 ui_out_text (uiout, "\nTemporary breakpoint ");
12886 else
12887 ui_out_text (uiout, "\nBreakpoint ");
12888 if (ui_out_is_mi_like_p (uiout))
12889 {
12890 ui_out_field_string (uiout, "reason",
12891 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
12892 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
12893 }
12894 ui_out_field_int (uiout, "bkptno", b->number);
12895 ui_out_text (uiout, ", ");
12896
12897 return PRINT_SRC_AND_LOC;
12898 }
12899
12900 static void
12901 bkpt_print_mention (struct breakpoint *b)
12902 {
12903 if (ui_out_is_mi_like_p (current_uiout))
12904 return;
12905
12906 switch (b->type)
12907 {
12908 case bp_breakpoint:
12909 case bp_gnu_ifunc_resolver:
12910 if (b->disposition == disp_del)
12911 printf_filtered (_("Temporary breakpoint"));
12912 else
12913 printf_filtered (_("Breakpoint"));
12914 printf_filtered (_(" %d"), b->number);
12915 if (b->type == bp_gnu_ifunc_resolver)
12916 printf_filtered (_(" at gnu-indirect-function resolver"));
12917 break;
12918 case bp_hardware_breakpoint:
12919 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
12920 break;
12921 case bp_dprintf:
12922 printf_filtered (_("Dprintf %d"), b->number);
12923 break;
12924 }
12925
12926 say_where (b);
12927 }
12928
12929 static void
12930 bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12931 {
12932 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
12933 fprintf_unfiltered (fp, "tbreak");
12934 else if (tp->type == bp_breakpoint)
12935 fprintf_unfiltered (fp, "break");
12936 else if (tp->type == bp_hardware_breakpoint
12937 && tp->disposition == disp_del)
12938 fprintf_unfiltered (fp, "thbreak");
12939 else if (tp->type == bp_hardware_breakpoint)
12940 fprintf_unfiltered (fp, "hbreak");
12941 else
12942 internal_error (__FILE__, __LINE__,
12943 _("unhandled breakpoint type %d"), (int) tp->type);
12944
12945 fprintf_unfiltered (fp, " %s", tp->addr_string);
12946 print_recreate_thread (tp, fp);
12947 }
12948
12949 static void
12950 bkpt_create_sals_from_address (char **arg,
12951 struct linespec_result *canonical,
12952 enum bptype type_wanted,
12953 char *addr_start, char **copy_arg)
12954 {
12955 create_sals_from_address_default (arg, canonical, type_wanted,
12956 addr_start, copy_arg);
12957 }
12958
12959 static void
12960 bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
12961 struct linespec_result *canonical,
12962 char *cond_string,
12963 char *extra_string,
12964 enum bptype type_wanted,
12965 enum bpdisp disposition,
12966 int thread,
12967 int task, int ignore_count,
12968 const struct breakpoint_ops *ops,
12969 int from_tty, int enabled,
12970 int internal, unsigned flags)
12971 {
12972 create_breakpoints_sal_default (gdbarch, canonical,
12973 cond_string, extra_string,
12974 type_wanted,
12975 disposition, thread, task,
12976 ignore_count, ops, from_tty,
12977 enabled, internal, flags);
12978 }
12979
12980 static void
12981 bkpt_decode_linespec (struct breakpoint *b, char **s,
12982 struct symtabs_and_lines *sals)
12983 {
12984 decode_linespec_default (b, s, sals);
12985 }
12986
12987 /* Virtual table for internal breakpoints. */
12988
12989 static void
12990 internal_bkpt_re_set (struct breakpoint *b)
12991 {
12992 switch (b->type)
12993 {
12994 /* Delete overlay event and longjmp master breakpoints; they
12995 will be reset later by breakpoint_re_set. */
12996 case bp_overlay_event:
12997 case bp_longjmp_master:
12998 case bp_std_terminate_master:
12999 case bp_exception_master:
13000 delete_breakpoint (b);
13001 break;
13002
13003 /* This breakpoint is special, it's set up when the inferior
13004 starts and we really don't want to touch it. */
13005 case bp_shlib_event:
13006
13007 /* Like bp_shlib_event, this breakpoint type is special. Once
13008 it is set up, we do not want to touch it. */
13009 case bp_thread_event:
13010 break;
13011 }
13012 }
13013
13014 static void
13015 internal_bkpt_check_status (bpstat bs)
13016 {
13017 if (bs->breakpoint_at->type == bp_shlib_event)
13018 {
13019 /* If requested, stop when the dynamic linker notifies GDB of
13020 events. This allows the user to get control and place
13021 breakpoints in initializer routines for dynamically loaded
13022 objects (among other things). */
13023 bs->stop = stop_on_solib_events;
13024 bs->print = stop_on_solib_events;
13025 }
13026 else
13027 bs->stop = 0;
13028 }
13029
13030 static enum print_stop_action
13031 internal_bkpt_print_it (bpstat bs)
13032 {
13033 struct breakpoint *b;
13034
13035 b = bs->breakpoint_at;
13036
13037 switch (b->type)
13038 {
13039 case bp_shlib_event:
13040 /* Did we stop because the user set the stop_on_solib_events
13041 variable? (If so, we report this as a generic, "Stopped due
13042 to shlib event" message.) */
13043 print_solib_event (0);
13044 break;
13045
13046 case bp_thread_event:
13047 /* Not sure how we will get here.
13048 GDB should not stop for these breakpoints. */
13049 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
13050 break;
13051
13052 case bp_overlay_event:
13053 /* By analogy with the thread event, GDB should not stop for these. */
13054 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
13055 break;
13056
13057 case bp_longjmp_master:
13058 /* These should never be enabled. */
13059 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
13060 break;
13061
13062 case bp_std_terminate_master:
13063 /* These should never be enabled. */
13064 printf_filtered (_("std::terminate Master Breakpoint: "
13065 "gdb should not stop!\n"));
13066 break;
13067
13068 case bp_exception_master:
13069 /* These should never be enabled. */
13070 printf_filtered (_("Exception Master Breakpoint: "
13071 "gdb should not stop!\n"));
13072 break;
13073 }
13074
13075 return PRINT_NOTHING;
13076 }
13077
13078 static void
13079 internal_bkpt_print_mention (struct breakpoint *b)
13080 {
13081 /* Nothing to mention. These breakpoints are internal. */
13082 }
13083
13084 /* Virtual table for momentary breakpoints */
13085
13086 static void
13087 momentary_bkpt_re_set (struct breakpoint *b)
13088 {
13089 /* Keep temporary breakpoints, which can be encountered when we step
13090 over a dlopen call and solib_add is resetting the breakpoints.
13091 Otherwise these should have been blown away via the cleanup chain
13092 or by breakpoint_init_inferior when we rerun the executable. */
13093 }
13094
13095 static void
13096 momentary_bkpt_check_status (bpstat bs)
13097 {
13098 /* Nothing. The point of these breakpoints is causing a stop. */
13099 }
13100
13101 static enum print_stop_action
13102 momentary_bkpt_print_it (bpstat bs)
13103 {
13104 struct ui_out *uiout = current_uiout;
13105
13106 if (ui_out_is_mi_like_p (uiout))
13107 {
13108 struct breakpoint *b = bs->breakpoint_at;
13109
13110 switch (b->type)
13111 {
13112 case bp_finish:
13113 ui_out_field_string
13114 (uiout, "reason",
13115 async_reason_lookup (EXEC_ASYNC_FUNCTION_FINISHED));
13116 break;
13117
13118 case bp_until:
13119 ui_out_field_string
13120 (uiout, "reason",
13121 async_reason_lookup (EXEC_ASYNC_LOCATION_REACHED));
13122 break;
13123 }
13124 }
13125
13126 return PRINT_UNKNOWN;
13127 }
13128
13129 static void
13130 momentary_bkpt_print_mention (struct breakpoint *b)
13131 {
13132 /* Nothing to mention. These breakpoints are internal. */
13133 }
13134
13135 /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
13136
13137 It gets cleared already on the removal of the first one of such placed
13138 breakpoints. This is OK as they get all removed altogether. */
13139
13140 static void
13141 longjmp_bkpt_dtor (struct breakpoint *self)
13142 {
13143 struct thread_info *tp = find_thread_id (self->thread);
13144
13145 if (tp)
13146 tp->initiating_frame = null_frame_id;
13147
13148 momentary_breakpoint_ops.dtor (self);
13149 }
13150
13151 /* Specific methods for probe breakpoints. */
13152
13153 static int
13154 bkpt_probe_insert_location (struct bp_location *bl)
13155 {
13156 int v = bkpt_insert_location (bl);
13157
13158 if (v == 0)
13159 {
13160 /* The insertion was successful, now let's set the probe's semaphore
13161 if needed. */
13162 if (bl->probe.probe->pops->set_semaphore != NULL)
13163 bl->probe.probe->pops->set_semaphore (bl->probe.probe,
13164 bl->probe.objfile,
13165 bl->gdbarch);
13166 }
13167
13168 return v;
13169 }
13170
13171 static int
13172 bkpt_probe_remove_location (struct bp_location *bl)
13173 {
13174 /* Let's clear the semaphore before removing the location. */
13175 if (bl->probe.probe->pops->clear_semaphore != NULL)
13176 bl->probe.probe->pops->clear_semaphore (bl->probe.probe,
13177 bl->probe.objfile,
13178 bl->gdbarch);
13179
13180 return bkpt_remove_location (bl);
13181 }
13182
13183 static void
13184 bkpt_probe_create_sals_from_address (char **arg,
13185 struct linespec_result *canonical,
13186 enum bptype type_wanted,
13187 char *addr_start, char **copy_arg)
13188 {
13189 struct linespec_sals lsal;
13190
13191 lsal.sals = parse_probes (arg, canonical);
13192
13193 *copy_arg = xstrdup (canonical->addr_string);
13194 lsal.canonical = xstrdup (*copy_arg);
13195
13196 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13197 }
13198
13199 static void
13200 bkpt_probe_decode_linespec (struct breakpoint *b, char **s,
13201 struct symtabs_and_lines *sals)
13202 {
13203 *sals = parse_probes (s, NULL);
13204 if (!sals->sals)
13205 error (_("probe not found"));
13206 }
13207
13208 /* The breakpoint_ops structure to be used in tracepoints. */
13209
13210 static void
13211 tracepoint_re_set (struct breakpoint *b)
13212 {
13213 breakpoint_re_set_default (b);
13214 }
13215
13216 static int
13217 tracepoint_breakpoint_hit (const struct bp_location *bl,
13218 struct address_space *aspace, CORE_ADDR bp_addr,
13219 const struct target_waitstatus *ws)
13220 {
13221 /* By definition, the inferior does not report stops at
13222 tracepoints. */
13223 return 0;
13224 }
13225
13226 static void
13227 tracepoint_print_one_detail (const struct breakpoint *self,
13228 struct ui_out *uiout)
13229 {
13230 struct tracepoint *tp = (struct tracepoint *) self;
13231 if (tp->static_trace_marker_id)
13232 {
13233 gdb_assert (self->type == bp_static_tracepoint);
13234
13235 ui_out_text (uiout, "\tmarker id is ");
13236 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
13237 tp->static_trace_marker_id);
13238 ui_out_text (uiout, "\n");
13239 }
13240 }
13241
13242 static void
13243 tracepoint_print_mention (struct breakpoint *b)
13244 {
13245 if (ui_out_is_mi_like_p (current_uiout))
13246 return;
13247
13248 switch (b->type)
13249 {
13250 case bp_tracepoint:
13251 printf_filtered (_("Tracepoint"));
13252 printf_filtered (_(" %d"), b->number);
13253 break;
13254 case bp_fast_tracepoint:
13255 printf_filtered (_("Fast tracepoint"));
13256 printf_filtered (_(" %d"), b->number);
13257 break;
13258 case bp_static_tracepoint:
13259 printf_filtered (_("Static tracepoint"));
13260 printf_filtered (_(" %d"), b->number);
13261 break;
13262 default:
13263 internal_error (__FILE__, __LINE__,
13264 _("unhandled tracepoint type %d"), (int) b->type);
13265 }
13266
13267 say_where (b);
13268 }
13269
13270 static void
13271 tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
13272 {
13273 struct tracepoint *tp = (struct tracepoint *) self;
13274
13275 if (self->type == bp_fast_tracepoint)
13276 fprintf_unfiltered (fp, "ftrace");
13277 if (self->type == bp_static_tracepoint)
13278 fprintf_unfiltered (fp, "strace");
13279 else if (self->type == bp_tracepoint)
13280 fprintf_unfiltered (fp, "trace");
13281 else
13282 internal_error (__FILE__, __LINE__,
13283 _("unhandled tracepoint type %d"), (int) self->type);
13284
13285 fprintf_unfiltered (fp, " %s", self->addr_string);
13286 print_recreate_thread (self, fp);
13287
13288 if (tp->pass_count)
13289 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
13290 }
13291
13292 static void
13293 tracepoint_create_sals_from_address (char **arg,
13294 struct linespec_result *canonical,
13295 enum bptype type_wanted,
13296 char *addr_start, char **copy_arg)
13297 {
13298 create_sals_from_address_default (arg, canonical, type_wanted,
13299 addr_start, copy_arg);
13300 }
13301
13302 static void
13303 tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13304 struct linespec_result *canonical,
13305 char *cond_string,
13306 char *extra_string,
13307 enum bptype type_wanted,
13308 enum bpdisp disposition,
13309 int thread,
13310 int task, int ignore_count,
13311 const struct breakpoint_ops *ops,
13312 int from_tty, int enabled,
13313 int internal, unsigned flags)
13314 {
13315 create_breakpoints_sal_default (gdbarch, canonical,
13316 cond_string, extra_string,
13317 type_wanted,
13318 disposition, thread, task,
13319 ignore_count, ops, from_tty,
13320 enabled, internal, flags);
13321 }
13322
13323 static void
13324 tracepoint_decode_linespec (struct breakpoint *b, char **s,
13325 struct symtabs_and_lines *sals)
13326 {
13327 decode_linespec_default (b, s, sals);
13328 }
13329
13330 struct breakpoint_ops tracepoint_breakpoint_ops;
13331
13332 /* The breakpoint_ops structure to be use on tracepoints placed in a
13333 static probe. */
13334
13335 static void
13336 tracepoint_probe_create_sals_from_address (char **arg,
13337 struct linespec_result *canonical,
13338 enum bptype type_wanted,
13339 char *addr_start, char **copy_arg)
13340 {
13341 /* We use the same method for breakpoint on probes. */
13342 bkpt_probe_create_sals_from_address (arg, canonical, type_wanted,
13343 addr_start, copy_arg);
13344 }
13345
13346 static void
13347 tracepoint_probe_decode_linespec (struct breakpoint *b, char **s,
13348 struct symtabs_and_lines *sals)
13349 {
13350 /* We use the same method for breakpoint on probes. */
13351 bkpt_probe_decode_linespec (b, s, sals);
13352 }
13353
13354 static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13355
13356 /* Dprintf breakpoint_ops methods. */
13357
13358 static void
13359 dprintf_re_set (struct breakpoint *b)
13360 {
13361 breakpoint_re_set_default (b);
13362
13363 /* This breakpoint could have been pending, and be resolved now, and
13364 if so, we should now have the extra string. If we don't, the
13365 dprintf was malformed when created, but we couldn't tell because
13366 we can't extract the extra string until the location is
13367 resolved. */
13368 if (b->loc != NULL && b->extra_string == NULL)
13369 error (_("Format string required"));
13370
13371 /* 1 - connect to target 1, that can run breakpoint commands.
13372 2 - create a dprintf, which resolves fine.
13373 3 - disconnect from target 1
13374 4 - connect to target 2, that can NOT run breakpoint commands.
13375
13376 After steps #3/#4, you'll want the dprintf command list to
13377 be updated, because target 1 and 2 may well return different
13378 answers for target_can_run_breakpoint_commands().
13379 Given absence of finer grained resetting, we get to do
13380 it all the time. */
13381 if (b->extra_string != NULL)
13382 update_dprintf_command_list (b);
13383 }
13384
13385 /* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13386
13387 static void
13388 dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13389 {
13390 fprintf_unfiltered (fp, "dprintf %s%s", tp->addr_string,
13391 tp->extra_string);
13392 print_recreate_thread (tp, fp);
13393 }
13394
13395 /* Implement the "after_condition_true" breakpoint_ops method for
13396 dprintf.
13397
13398 dprintf's are implemented with regular commands in their command
13399 list, but we run the commands here instead of before presenting the
13400 stop to the user, as dprintf's don't actually cause a stop. This
13401 also makes it so that the commands of multiple dprintfs at the same
13402 address are all handled. */
13403
13404 static void
13405 dprintf_after_condition_true (struct bpstats *bs)
13406 {
13407 struct cleanup *old_chain;
13408 struct bpstats tmp_bs = { NULL };
13409 struct bpstats *tmp_bs_p = &tmp_bs;
13410
13411 /* dprintf's never cause a stop. This wasn't set in the
13412 check_status hook instead because that would make the dprintf's
13413 condition not be evaluated. */
13414 bs->stop = 0;
13415
13416 /* Run the command list here. Take ownership of it instead of
13417 copying. We never want these commands to run later in
13418 bpstat_do_actions, if a breakpoint that causes a stop happens to
13419 be set at same address as this dprintf, or even if running the
13420 commands here throws. */
13421 tmp_bs.commands = bs->commands;
13422 bs->commands = NULL;
13423 old_chain = make_cleanup_decref_counted_command_line (&tmp_bs.commands);
13424
13425 bpstat_do_actions_1 (&tmp_bs_p);
13426
13427 /* 'tmp_bs.commands' will usually be NULL by now, but
13428 bpstat_do_actions_1 may return early without processing the whole
13429 list. */
13430 do_cleanups (old_chain);
13431 }
13432
13433 /* The breakpoint_ops structure to be used on static tracepoints with
13434 markers (`-m'). */
13435
13436 static void
13437 strace_marker_create_sals_from_address (char **arg,
13438 struct linespec_result *canonical,
13439 enum bptype type_wanted,
13440 char *addr_start, char **copy_arg)
13441 {
13442 struct linespec_sals lsal;
13443
13444 lsal.sals = decode_static_tracepoint_spec (arg);
13445
13446 *copy_arg = savestring (addr_start, *arg - addr_start);
13447
13448 canonical->addr_string = xstrdup (*copy_arg);
13449 lsal.canonical = xstrdup (*copy_arg);
13450 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13451 }
13452
13453 static void
13454 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13455 struct linespec_result *canonical,
13456 char *cond_string,
13457 char *extra_string,
13458 enum bptype type_wanted,
13459 enum bpdisp disposition,
13460 int thread,
13461 int task, int ignore_count,
13462 const struct breakpoint_ops *ops,
13463 int from_tty, int enabled,
13464 int internal, unsigned flags)
13465 {
13466 int i;
13467 struct linespec_sals *lsal = VEC_index (linespec_sals,
13468 canonical->sals, 0);
13469
13470 /* If the user is creating a static tracepoint by marker id
13471 (strace -m MARKER_ID), then store the sals index, so that
13472 breakpoint_re_set can try to match up which of the newly
13473 found markers corresponds to this one, and, don't try to
13474 expand multiple locations for each sal, given than SALS
13475 already should contain all sals for MARKER_ID. */
13476
13477 for (i = 0; i < lsal->sals.nelts; ++i)
13478 {
13479 struct symtabs_and_lines expanded;
13480 struct tracepoint *tp;
13481 struct cleanup *old_chain;
13482 char *addr_string;
13483
13484 expanded.nelts = 1;
13485 expanded.sals = &lsal->sals.sals[i];
13486
13487 addr_string = xstrdup (canonical->addr_string);
13488 old_chain = make_cleanup (xfree, addr_string);
13489
13490 tp = XCNEW (struct tracepoint);
13491 init_breakpoint_sal (&tp->base, gdbarch, expanded,
13492 addr_string, NULL,
13493 cond_string, extra_string,
13494 type_wanted, disposition,
13495 thread, task, ignore_count, ops,
13496 from_tty, enabled, internal, flags,
13497 canonical->special_display);
13498 /* Given that its possible to have multiple markers with
13499 the same string id, if the user is creating a static
13500 tracepoint by marker id ("strace -m MARKER_ID"), then
13501 store the sals index, so that breakpoint_re_set can
13502 try to match up which of the newly found markers
13503 corresponds to this one */
13504 tp->static_trace_marker_id_idx = i;
13505
13506 install_breakpoint (internal, &tp->base, 0);
13507
13508 discard_cleanups (old_chain);
13509 }
13510 }
13511
13512 static void
13513 strace_marker_decode_linespec (struct breakpoint *b, char **s,
13514 struct symtabs_and_lines *sals)
13515 {
13516 struct tracepoint *tp = (struct tracepoint *) b;
13517
13518 *sals = decode_static_tracepoint_spec (s);
13519 if (sals->nelts > tp->static_trace_marker_id_idx)
13520 {
13521 sals->sals[0] = sals->sals[tp->static_trace_marker_id_idx];
13522 sals->nelts = 1;
13523 }
13524 else
13525 error (_("marker %s not found"), tp->static_trace_marker_id);
13526 }
13527
13528 static struct breakpoint_ops strace_marker_breakpoint_ops;
13529
13530 static int
13531 strace_marker_p (struct breakpoint *b)
13532 {
13533 return b->ops == &strace_marker_breakpoint_ops;
13534 }
13535
13536 /* Delete a breakpoint and clean up all traces of it in the data
13537 structures. */
13538
13539 void
13540 delete_breakpoint (struct breakpoint *bpt)
13541 {
13542 struct breakpoint *b;
13543
13544 gdb_assert (bpt != NULL);
13545
13546 /* Has this bp already been deleted? This can happen because
13547 multiple lists can hold pointers to bp's. bpstat lists are
13548 especial culprits.
13549
13550 One example of this happening is a watchpoint's scope bp. When
13551 the scope bp triggers, we notice that the watchpoint is out of
13552 scope, and delete it. We also delete its scope bp. But the
13553 scope bp is marked "auto-deleting", and is already on a bpstat.
13554 That bpstat is then checked for auto-deleting bp's, which are
13555 deleted.
13556
13557 A real solution to this problem might involve reference counts in
13558 bp's, and/or giving them pointers back to their referencing
13559 bpstat's, and teaching delete_breakpoint to only free a bp's
13560 storage when no more references were extent. A cheaper bandaid
13561 was chosen. */
13562 if (bpt->type == bp_none)
13563 return;
13564
13565 /* At least avoid this stale reference until the reference counting
13566 of breakpoints gets resolved. */
13567 if (bpt->related_breakpoint != bpt)
13568 {
13569 struct breakpoint *related;
13570 struct watchpoint *w;
13571
13572 if (bpt->type == bp_watchpoint_scope)
13573 w = (struct watchpoint *) bpt->related_breakpoint;
13574 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
13575 w = (struct watchpoint *) bpt;
13576 else
13577 w = NULL;
13578 if (w != NULL)
13579 watchpoint_del_at_next_stop (w);
13580
13581 /* Unlink bpt from the bpt->related_breakpoint ring. */
13582 for (related = bpt; related->related_breakpoint != bpt;
13583 related = related->related_breakpoint);
13584 related->related_breakpoint = bpt->related_breakpoint;
13585 bpt->related_breakpoint = bpt;
13586 }
13587
13588 /* watch_command_1 creates a watchpoint but only sets its number if
13589 update_watchpoint succeeds in creating its bp_locations. If there's
13590 a problem in that process, we'll be asked to delete the half-created
13591 watchpoint. In that case, don't announce the deletion. */
13592 if (bpt->number)
13593 observer_notify_breakpoint_deleted (bpt);
13594
13595 if (breakpoint_chain == bpt)
13596 breakpoint_chain = bpt->next;
13597
13598 ALL_BREAKPOINTS (b)
13599 if (b->next == bpt)
13600 {
13601 b->next = bpt->next;
13602 break;
13603 }
13604
13605 /* Be sure no bpstat's are pointing at the breakpoint after it's
13606 been freed. */
13607 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
13608 in all threads for now. Note that we cannot just remove bpstats
13609 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13610 commands are associated with the bpstat; if we remove it here,
13611 then the later call to bpstat_do_actions (&stop_bpstat); in
13612 event-top.c won't do anything, and temporary breakpoints with
13613 commands won't work. */
13614
13615 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13616
13617 /* Now that breakpoint is removed from breakpoint list, update the
13618 global location list. This will remove locations that used to
13619 belong to this breakpoint. Do this before freeing the breakpoint
13620 itself, since remove_breakpoint looks at location's owner. It
13621 might be better design to have location completely
13622 self-contained, but it's not the case now. */
13623 update_global_location_list (UGLL_DONT_INSERT);
13624
13625 bpt->ops->dtor (bpt);
13626 /* On the chance that someone will soon try again to delete this
13627 same bp, we mark it as deleted before freeing its storage. */
13628 bpt->type = bp_none;
13629 xfree (bpt);
13630 }
13631
13632 static void
13633 do_delete_breakpoint_cleanup (void *b)
13634 {
13635 delete_breakpoint (b);
13636 }
13637
13638 struct cleanup *
13639 make_cleanup_delete_breakpoint (struct breakpoint *b)
13640 {
13641 return make_cleanup (do_delete_breakpoint_cleanup, b);
13642 }
13643
13644 /* Iterator function to call a user-provided callback function once
13645 for each of B and its related breakpoints. */
13646
13647 static void
13648 iterate_over_related_breakpoints (struct breakpoint *b,
13649 void (*function) (struct breakpoint *,
13650 void *),
13651 void *data)
13652 {
13653 struct breakpoint *related;
13654
13655 related = b;
13656 do
13657 {
13658 struct breakpoint *next;
13659
13660 /* FUNCTION may delete RELATED. */
13661 next = related->related_breakpoint;
13662
13663 if (next == related)
13664 {
13665 /* RELATED is the last ring entry. */
13666 function (related, data);
13667
13668 /* FUNCTION may have deleted it, so we'd never reach back to
13669 B. There's nothing left to do anyway, so just break
13670 out. */
13671 break;
13672 }
13673 else
13674 function (related, data);
13675
13676 related = next;
13677 }
13678 while (related != b);
13679 }
13680
13681 static void
13682 do_delete_breakpoint (struct breakpoint *b, void *ignore)
13683 {
13684 delete_breakpoint (b);
13685 }
13686
13687 /* A callback for map_breakpoint_numbers that calls
13688 delete_breakpoint. */
13689
13690 static void
13691 do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
13692 {
13693 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
13694 }
13695
13696 void
13697 delete_command (char *arg, int from_tty)
13698 {
13699 struct breakpoint *b, *b_tmp;
13700
13701 dont_repeat ();
13702
13703 if (arg == 0)
13704 {
13705 int breaks_to_delete = 0;
13706
13707 /* Delete all breakpoints if no argument. Do not delete
13708 internal breakpoints, these have to be deleted with an
13709 explicit breakpoint number argument. */
13710 ALL_BREAKPOINTS (b)
13711 if (user_breakpoint_p (b))
13712 {
13713 breaks_to_delete = 1;
13714 break;
13715 }
13716
13717 /* Ask user only if there are some breakpoints to delete. */
13718 if (!from_tty
13719 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
13720 {
13721 ALL_BREAKPOINTS_SAFE (b, b_tmp)
13722 if (user_breakpoint_p (b))
13723 delete_breakpoint (b);
13724 }
13725 }
13726 else
13727 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
13728 }
13729
13730 static int
13731 all_locations_are_pending (struct bp_location *loc)
13732 {
13733 for (; loc; loc = loc->next)
13734 if (!loc->shlib_disabled
13735 && !loc->pspace->executing_startup)
13736 return 0;
13737 return 1;
13738 }
13739
13740 /* Subroutine of update_breakpoint_locations to simplify it.
13741 Return non-zero if multiple fns in list LOC have the same name.
13742 Null names are ignored. */
13743
13744 static int
13745 ambiguous_names_p (struct bp_location *loc)
13746 {
13747 struct bp_location *l;
13748 htab_t htab = htab_create_alloc (13, htab_hash_string,
13749 (int (*) (const void *,
13750 const void *)) streq,
13751 NULL, xcalloc, xfree);
13752
13753 for (l = loc; l != NULL; l = l->next)
13754 {
13755 const char **slot;
13756 const char *name = l->function_name;
13757
13758 /* Allow for some names to be NULL, ignore them. */
13759 if (name == NULL)
13760 continue;
13761
13762 slot = (const char **) htab_find_slot (htab, (const void *) name,
13763 INSERT);
13764 /* NOTE: We can assume slot != NULL here because xcalloc never
13765 returns NULL. */
13766 if (*slot != NULL)
13767 {
13768 htab_delete (htab);
13769 return 1;
13770 }
13771 *slot = name;
13772 }
13773
13774 htab_delete (htab);
13775 return 0;
13776 }
13777
13778 /* When symbols change, it probably means the sources changed as well,
13779 and it might mean the static tracepoint markers are no longer at
13780 the same address or line numbers they used to be at last we
13781 checked. Losing your static tracepoints whenever you rebuild is
13782 undesirable. This function tries to resync/rematch gdb static
13783 tracepoints with the markers on the target, for static tracepoints
13784 that have not been set by marker id. Static tracepoint that have
13785 been set by marker id are reset by marker id in breakpoint_re_set.
13786 The heuristic is:
13787
13788 1) For a tracepoint set at a specific address, look for a marker at
13789 the old PC. If one is found there, assume to be the same marker.
13790 If the name / string id of the marker found is different from the
13791 previous known name, assume that means the user renamed the marker
13792 in the sources, and output a warning.
13793
13794 2) For a tracepoint set at a given line number, look for a marker
13795 at the new address of the old line number. If one is found there,
13796 assume to be the same marker. If the name / string id of the
13797 marker found is different from the previous known name, assume that
13798 means the user renamed the marker in the sources, and output a
13799 warning.
13800
13801 3) If a marker is no longer found at the same address or line, it
13802 may mean the marker no longer exists. But it may also just mean
13803 the code changed a bit. Maybe the user added a few lines of code
13804 that made the marker move up or down (in line number terms). Ask
13805 the target for info about the marker with the string id as we knew
13806 it. If found, update line number and address in the matching
13807 static tracepoint. This will get confused if there's more than one
13808 marker with the same ID (possible in UST, although unadvised
13809 precisely because it confuses tools). */
13810
13811 static struct symtab_and_line
13812 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13813 {
13814 struct tracepoint *tp = (struct tracepoint *) b;
13815 struct static_tracepoint_marker marker;
13816 CORE_ADDR pc;
13817
13818 pc = sal.pc;
13819 if (sal.line)
13820 find_line_pc (sal.symtab, sal.line, &pc);
13821
13822 if (target_static_tracepoint_marker_at (pc, &marker))
13823 {
13824 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
13825 warning (_("static tracepoint %d changed probed marker from %s to %s"),
13826 b->number,
13827 tp->static_trace_marker_id, marker.str_id);
13828
13829 xfree (tp->static_trace_marker_id);
13830 tp->static_trace_marker_id = xstrdup (marker.str_id);
13831 release_static_tracepoint_marker (&marker);
13832
13833 return sal;
13834 }
13835
13836 /* Old marker wasn't found on target at lineno. Try looking it up
13837 by string ID. */
13838 if (!sal.explicit_pc
13839 && sal.line != 0
13840 && sal.symtab != NULL
13841 && tp->static_trace_marker_id != NULL)
13842 {
13843 VEC(static_tracepoint_marker_p) *markers;
13844
13845 markers
13846 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
13847
13848 if (!VEC_empty(static_tracepoint_marker_p, markers))
13849 {
13850 struct symtab_and_line sal2;
13851 struct symbol *sym;
13852 struct static_tracepoint_marker *tpmarker;
13853 struct ui_out *uiout = current_uiout;
13854
13855 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
13856
13857 xfree (tp->static_trace_marker_id);
13858 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
13859
13860 warning (_("marker for static tracepoint %d (%s) not "
13861 "found at previous line number"),
13862 b->number, tp->static_trace_marker_id);
13863
13864 init_sal (&sal2);
13865
13866 sal2.pc = tpmarker->address;
13867
13868 sal2 = find_pc_line (tpmarker->address, 0);
13869 sym = find_pc_sect_function (tpmarker->address, NULL);
13870 ui_out_text (uiout, "Now in ");
13871 if (sym)
13872 {
13873 ui_out_field_string (uiout, "func",
13874 SYMBOL_PRINT_NAME (sym));
13875 ui_out_text (uiout, " at ");
13876 }
13877 ui_out_field_string (uiout, "file",
13878 symtab_to_filename_for_display (sal2.symtab));
13879 ui_out_text (uiout, ":");
13880
13881 if (ui_out_is_mi_like_p (uiout))
13882 {
13883 const char *fullname = symtab_to_fullname (sal2.symtab);
13884
13885 ui_out_field_string (uiout, "fullname", fullname);
13886 }
13887
13888 ui_out_field_int (uiout, "line", sal2.line);
13889 ui_out_text (uiout, "\n");
13890
13891 b->loc->line_number = sal2.line;
13892 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
13893
13894 xfree (b->addr_string);
13895 b->addr_string = xstrprintf ("%s:%d",
13896 symtab_to_filename_for_display (sal2.symtab),
13897 b->loc->line_number);
13898
13899 /* Might be nice to check if function changed, and warn if
13900 so. */
13901
13902 release_static_tracepoint_marker (tpmarker);
13903 }
13904 }
13905 return sal;
13906 }
13907
13908 /* Returns 1 iff locations A and B are sufficiently same that
13909 we don't need to report breakpoint as changed. */
13910
13911 static int
13912 locations_are_equal (struct bp_location *a, struct bp_location *b)
13913 {
13914 while (a && b)
13915 {
13916 if (a->address != b->address)
13917 return 0;
13918
13919 if (a->shlib_disabled != b->shlib_disabled)
13920 return 0;
13921
13922 if (a->enabled != b->enabled)
13923 return 0;
13924
13925 a = a->next;
13926 b = b->next;
13927 }
13928
13929 if ((a == NULL) != (b == NULL))
13930 return 0;
13931
13932 return 1;
13933 }
13934
13935 /* Create new breakpoint locations for B (a hardware or software breakpoint)
13936 based on SALS and SALS_END. If SALS_END.NELTS is not zero, then B is
13937 a ranged breakpoint. */
13938
13939 void
13940 update_breakpoint_locations (struct breakpoint *b,
13941 struct symtabs_and_lines sals,
13942 struct symtabs_and_lines sals_end)
13943 {
13944 int i;
13945 struct bp_location *existing_locations = b->loc;
13946
13947 if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
13948 {
13949 /* Ranged breakpoints have only one start location and one end
13950 location. */
13951 b->enable_state = bp_disabled;
13952 update_global_location_list (UGLL_MAY_INSERT);
13953 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
13954 "multiple locations found\n"),
13955 b->number);
13956 return;
13957 }
13958
13959 /* If there's no new locations, and all existing locations are
13960 pending, don't do anything. This optimizes the common case where
13961 all locations are in the same shared library, that was unloaded.
13962 We'd like to retain the location, so that when the library is
13963 loaded again, we don't loose the enabled/disabled status of the
13964 individual locations. */
13965 if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
13966 return;
13967
13968 b->loc = NULL;
13969
13970 for (i = 0; i < sals.nelts; ++i)
13971 {
13972 struct bp_location *new_loc;
13973
13974 switch_to_program_space_and_thread (sals.sals[i].pspace);
13975
13976 new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
13977
13978 /* Reparse conditions, they might contain references to the
13979 old symtab. */
13980 if (b->cond_string != NULL)
13981 {
13982 const char *s;
13983
13984 s = b->cond_string;
13985 TRY
13986 {
13987 new_loc->cond = parse_exp_1 (&s, sals.sals[i].pc,
13988 block_for_pc (sals.sals[i].pc),
13989 0);
13990 }
13991 CATCH (e, RETURN_MASK_ERROR)
13992 {
13993 warning (_("failed to reevaluate condition "
13994 "for breakpoint %d: %s"),
13995 b->number, e.message);
13996 new_loc->enabled = 0;
13997 }
13998 END_CATCH
13999 }
14000
14001 if (sals_end.nelts)
14002 {
14003 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
14004
14005 new_loc->length = end - sals.sals[0].pc + 1;
14006 }
14007 }
14008
14009 /* If possible, carry over 'disable' status from existing
14010 breakpoints. */
14011 {
14012 struct bp_location *e = existing_locations;
14013 /* If there are multiple breakpoints with the same function name,
14014 e.g. for inline functions, comparing function names won't work.
14015 Instead compare pc addresses; this is just a heuristic as things
14016 may have moved, but in practice it gives the correct answer
14017 often enough until a better solution is found. */
14018 int have_ambiguous_names = ambiguous_names_p (b->loc);
14019
14020 for (; e; e = e->next)
14021 {
14022 if (!e->enabled && e->function_name)
14023 {
14024 struct bp_location *l = b->loc;
14025 if (have_ambiguous_names)
14026 {
14027 for (; l; l = l->next)
14028 if (breakpoint_locations_match (e, l))
14029 {
14030 l->enabled = 0;
14031 break;
14032 }
14033 }
14034 else
14035 {
14036 for (; l; l = l->next)
14037 if (l->function_name
14038 && strcmp (e->function_name, l->function_name) == 0)
14039 {
14040 l->enabled = 0;
14041 break;
14042 }
14043 }
14044 }
14045 }
14046 }
14047
14048 if (!locations_are_equal (existing_locations, b->loc))
14049 observer_notify_breakpoint_modified (b);
14050
14051 update_global_location_list (UGLL_MAY_INSERT);
14052 }
14053
14054 /* Find the SaL locations corresponding to the given ADDR_STRING.
14055 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
14056
14057 static struct symtabs_and_lines
14058 addr_string_to_sals (struct breakpoint *b, char *addr_string, int *found)
14059 {
14060 char *s;
14061 struct symtabs_and_lines sals = {0};
14062 struct gdb_exception exception = exception_none;
14063
14064 gdb_assert (b->ops != NULL);
14065 s = addr_string;
14066
14067 TRY
14068 {
14069 b->ops->decode_linespec (b, &s, &sals);
14070 }
14071 CATCH (e, RETURN_MASK_ERROR)
14072 {
14073 int not_found_and_ok = 0;
14074
14075 exception = e;
14076
14077 /* For pending breakpoints, it's expected that parsing will
14078 fail until the right shared library is loaded. User has
14079 already told to create pending breakpoints and don't need
14080 extra messages. If breakpoint is in bp_shlib_disabled
14081 state, then user already saw the message about that
14082 breakpoint being disabled, and don't want to see more
14083 errors. */
14084 if (e.error == NOT_FOUND_ERROR
14085 && (b->condition_not_parsed
14086 || (b->loc && b->loc->shlib_disabled)
14087 || (b->loc && b->loc->pspace->executing_startup)
14088 || b->enable_state == bp_disabled))
14089 not_found_and_ok = 1;
14090
14091 if (!not_found_and_ok)
14092 {
14093 /* We surely don't want to warn about the same breakpoint
14094 10 times. One solution, implemented here, is disable
14095 the breakpoint on error. Another solution would be to
14096 have separate 'warning emitted' flag. Since this
14097 happens only when a binary has changed, I don't know
14098 which approach is better. */
14099 b->enable_state = bp_disabled;
14100 throw_exception (e);
14101 }
14102 }
14103 END_CATCH
14104
14105 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
14106 {
14107 int i;
14108
14109 for (i = 0; i < sals.nelts; ++i)
14110 resolve_sal_pc (&sals.sals[i]);
14111 if (b->condition_not_parsed && s && s[0])
14112 {
14113 char *cond_string, *extra_string;
14114 int thread, task;
14115
14116 find_condition_and_thread (s, sals.sals[0].pc,
14117 &cond_string, &thread, &task,
14118 &extra_string);
14119 if (cond_string)
14120 b->cond_string = cond_string;
14121 b->thread = thread;
14122 b->task = task;
14123 if (extra_string)
14124 b->extra_string = extra_string;
14125 b->condition_not_parsed = 0;
14126 }
14127
14128 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
14129 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
14130
14131 *found = 1;
14132 }
14133 else
14134 *found = 0;
14135
14136 return sals;
14137 }
14138
14139 /* The default re_set method, for typical hardware or software
14140 breakpoints. Reevaluate the breakpoint and recreate its
14141 locations. */
14142
14143 static void
14144 breakpoint_re_set_default (struct breakpoint *b)
14145 {
14146 int found;
14147 struct symtabs_and_lines sals, sals_end;
14148 struct symtabs_and_lines expanded = {0};
14149 struct symtabs_and_lines expanded_end = {0};
14150
14151 sals = addr_string_to_sals (b, b->addr_string, &found);
14152 if (found)
14153 {
14154 make_cleanup (xfree, sals.sals);
14155 expanded = sals;
14156 }
14157
14158 if (b->addr_string_range_end)
14159 {
14160 sals_end = addr_string_to_sals (b, b->addr_string_range_end, &found);
14161 if (found)
14162 {
14163 make_cleanup (xfree, sals_end.sals);
14164 expanded_end = sals_end;
14165 }
14166 }
14167
14168 update_breakpoint_locations (b, expanded, expanded_end);
14169 }
14170
14171 /* Default method for creating SALs from an address string. It basically
14172 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
14173
14174 static void
14175 create_sals_from_address_default (char **arg,
14176 struct linespec_result *canonical,
14177 enum bptype type_wanted,
14178 char *addr_start, char **copy_arg)
14179 {
14180 parse_breakpoint_sals (arg, canonical);
14181 }
14182
14183 /* Call create_breakpoints_sal for the given arguments. This is the default
14184 function for the `create_breakpoints_sal' method of
14185 breakpoint_ops. */
14186
14187 static void
14188 create_breakpoints_sal_default (struct gdbarch *gdbarch,
14189 struct linespec_result *canonical,
14190 char *cond_string,
14191 char *extra_string,
14192 enum bptype type_wanted,
14193 enum bpdisp disposition,
14194 int thread,
14195 int task, int ignore_count,
14196 const struct breakpoint_ops *ops,
14197 int from_tty, int enabled,
14198 int internal, unsigned flags)
14199 {
14200 create_breakpoints_sal (gdbarch, canonical, cond_string,
14201 extra_string,
14202 type_wanted, disposition,
14203 thread, task, ignore_count, ops, from_tty,
14204 enabled, internal, flags);
14205 }
14206
14207 /* Decode the line represented by S by calling decode_line_full. This is the
14208 default function for the `decode_linespec' method of breakpoint_ops. */
14209
14210 static void
14211 decode_linespec_default (struct breakpoint *b, char **s,
14212 struct symtabs_and_lines *sals)
14213 {
14214 struct linespec_result canonical;
14215
14216 init_linespec_result (&canonical);
14217 decode_line_full (s, DECODE_LINE_FUNFIRSTLINE,
14218 (struct symtab *) NULL, 0,
14219 &canonical, multiple_symbols_all,
14220 b->filter);
14221
14222 /* We should get 0 or 1 resulting SALs. */
14223 gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
14224
14225 if (VEC_length (linespec_sals, canonical.sals) > 0)
14226 {
14227 struct linespec_sals *lsal;
14228
14229 lsal = VEC_index (linespec_sals, canonical.sals, 0);
14230 *sals = lsal->sals;
14231 /* Arrange it so the destructor does not free the
14232 contents. */
14233 lsal->sals.sals = NULL;
14234 }
14235
14236 destroy_linespec_result (&canonical);
14237 }
14238
14239 /* Prepare the global context for a re-set of breakpoint B. */
14240
14241 static struct cleanup *
14242 prepare_re_set_context (struct breakpoint *b)
14243 {
14244 struct cleanup *cleanups;
14245
14246 input_radix = b->input_radix;
14247 cleanups = save_current_space_and_thread ();
14248 if (b->pspace != NULL)
14249 switch_to_program_space_and_thread (b->pspace);
14250 set_language (b->language);
14251
14252 return cleanups;
14253 }
14254
14255 /* Reset a breakpoint given it's struct breakpoint * BINT.
14256 The value we return ends up being the return value from catch_errors.
14257 Unused in this case. */
14258
14259 static int
14260 breakpoint_re_set_one (void *bint)
14261 {
14262 /* Get past catch_errs. */
14263 struct breakpoint *b = (struct breakpoint *) bint;
14264 struct cleanup *cleanups;
14265
14266 cleanups = prepare_re_set_context (b);
14267 b->ops->re_set (b);
14268 do_cleanups (cleanups);
14269 return 0;
14270 }
14271
14272 /* Re-set all breakpoints after symbols have been re-loaded. */
14273 void
14274 breakpoint_re_set (void)
14275 {
14276 struct breakpoint *b, *b_tmp;
14277 enum language save_language;
14278 int save_input_radix;
14279 struct cleanup *old_chain;
14280
14281 save_language = current_language->la_language;
14282 save_input_radix = input_radix;
14283 old_chain = save_current_program_space ();
14284
14285 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14286 {
14287 /* Format possible error msg. */
14288 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
14289 b->number);
14290 struct cleanup *cleanups = make_cleanup (xfree, message);
14291 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
14292 do_cleanups (cleanups);
14293 }
14294 set_language (save_language);
14295 input_radix = save_input_radix;
14296
14297 jit_breakpoint_re_set ();
14298
14299 do_cleanups (old_chain);
14300
14301 create_overlay_event_breakpoint ();
14302 create_longjmp_master_breakpoint ();
14303 create_std_terminate_master_breakpoint ();
14304 create_exception_master_breakpoint ();
14305 }
14306 \f
14307 /* Reset the thread number of this breakpoint:
14308
14309 - If the breakpoint is for all threads, leave it as-is.
14310 - Else, reset it to the current thread for inferior_ptid. */
14311 void
14312 breakpoint_re_set_thread (struct breakpoint *b)
14313 {
14314 if (b->thread != -1)
14315 {
14316 if (in_thread_list (inferior_ptid))
14317 b->thread = pid_to_thread_id (inferior_ptid);
14318
14319 /* We're being called after following a fork. The new fork is
14320 selected as current, and unless this was a vfork will have a
14321 different program space from the original thread. Reset that
14322 as well. */
14323 b->loc->pspace = current_program_space;
14324 }
14325 }
14326
14327 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14328 If from_tty is nonzero, it prints a message to that effect,
14329 which ends with a period (no newline). */
14330
14331 void
14332 set_ignore_count (int bptnum, int count, int from_tty)
14333 {
14334 struct breakpoint *b;
14335
14336 if (count < 0)
14337 count = 0;
14338
14339 ALL_BREAKPOINTS (b)
14340 if (b->number == bptnum)
14341 {
14342 if (is_tracepoint (b))
14343 {
14344 if (from_tty && count != 0)
14345 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14346 bptnum);
14347 return;
14348 }
14349
14350 b->ignore_count = count;
14351 if (from_tty)
14352 {
14353 if (count == 0)
14354 printf_filtered (_("Will stop next time "
14355 "breakpoint %d is reached."),
14356 bptnum);
14357 else if (count == 1)
14358 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
14359 bptnum);
14360 else
14361 printf_filtered (_("Will ignore next %d "
14362 "crossings of breakpoint %d."),
14363 count, bptnum);
14364 }
14365 observer_notify_breakpoint_modified (b);
14366 return;
14367 }
14368
14369 error (_("No breakpoint number %d."), bptnum);
14370 }
14371
14372 /* Command to set ignore-count of breakpoint N to COUNT. */
14373
14374 static void
14375 ignore_command (char *args, int from_tty)
14376 {
14377 char *p = args;
14378 int num;
14379
14380 if (p == 0)
14381 error_no_arg (_("a breakpoint number"));
14382
14383 num = get_number (&p);
14384 if (num == 0)
14385 error (_("bad breakpoint number: '%s'"), args);
14386 if (*p == 0)
14387 error (_("Second argument (specified ignore-count) is missing."));
14388
14389 set_ignore_count (num,
14390 longest_to_int (value_as_long (parse_and_eval (p))),
14391 from_tty);
14392 if (from_tty)
14393 printf_filtered ("\n");
14394 }
14395 \f
14396 /* Call FUNCTION on each of the breakpoints
14397 whose numbers are given in ARGS. */
14398
14399 static void
14400 map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
14401 void *),
14402 void *data)
14403 {
14404 int num;
14405 struct breakpoint *b, *tmp;
14406 int match;
14407 struct get_number_or_range_state state;
14408
14409 if (args == 0 || *args == '\0')
14410 error_no_arg (_("one or more breakpoint numbers"));
14411
14412 init_number_or_range (&state, args);
14413
14414 while (!state.finished)
14415 {
14416 const char *p = state.string;
14417
14418 match = 0;
14419
14420 num = get_number_or_range (&state);
14421 if (num == 0)
14422 {
14423 warning (_("bad breakpoint number at or near '%s'"), p);
14424 }
14425 else
14426 {
14427 ALL_BREAKPOINTS_SAFE (b, tmp)
14428 if (b->number == num)
14429 {
14430 match = 1;
14431 function (b, data);
14432 break;
14433 }
14434 if (match == 0)
14435 printf_unfiltered (_("No breakpoint number %d.\n"), num);
14436 }
14437 }
14438 }
14439
14440 static struct bp_location *
14441 find_location_by_number (char *number)
14442 {
14443 char *dot = strchr (number, '.');
14444 char *p1;
14445 int bp_num;
14446 int loc_num;
14447 struct breakpoint *b;
14448 struct bp_location *loc;
14449
14450 *dot = '\0';
14451
14452 p1 = number;
14453 bp_num = get_number (&p1);
14454 if (bp_num == 0)
14455 error (_("Bad breakpoint number '%s'"), number);
14456
14457 ALL_BREAKPOINTS (b)
14458 if (b->number == bp_num)
14459 {
14460 break;
14461 }
14462
14463 if (!b || b->number != bp_num)
14464 error (_("Bad breakpoint number '%s'"), number);
14465
14466 p1 = dot+1;
14467 loc_num = get_number (&p1);
14468 if (loc_num == 0)
14469 error (_("Bad breakpoint location number '%s'"), number);
14470
14471 --loc_num;
14472 loc = b->loc;
14473 for (;loc_num && loc; --loc_num, loc = loc->next)
14474 ;
14475 if (!loc)
14476 error (_("Bad breakpoint location number '%s'"), dot+1);
14477
14478 return loc;
14479 }
14480
14481
14482 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14483 If from_tty is nonzero, it prints a message to that effect,
14484 which ends with a period (no newline). */
14485
14486 void
14487 disable_breakpoint (struct breakpoint *bpt)
14488 {
14489 /* Never disable a watchpoint scope breakpoint; we want to
14490 hit them when we leave scope so we can delete both the
14491 watchpoint and its scope breakpoint at that time. */
14492 if (bpt->type == bp_watchpoint_scope)
14493 return;
14494
14495 bpt->enable_state = bp_disabled;
14496
14497 /* Mark breakpoint locations modified. */
14498 mark_breakpoint_modified (bpt);
14499
14500 if (target_supports_enable_disable_tracepoint ()
14501 && current_trace_status ()->running && is_tracepoint (bpt))
14502 {
14503 struct bp_location *location;
14504
14505 for (location = bpt->loc; location; location = location->next)
14506 target_disable_tracepoint (location);
14507 }
14508
14509 update_global_location_list (UGLL_DONT_INSERT);
14510
14511 observer_notify_breakpoint_modified (bpt);
14512 }
14513
14514 /* A callback for iterate_over_related_breakpoints. */
14515
14516 static void
14517 do_disable_breakpoint (struct breakpoint *b, void *ignore)
14518 {
14519 disable_breakpoint (b);
14520 }
14521
14522 /* A callback for map_breakpoint_numbers that calls
14523 disable_breakpoint. */
14524
14525 static void
14526 do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
14527 {
14528 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
14529 }
14530
14531 static void
14532 disable_command (char *args, int from_tty)
14533 {
14534 if (args == 0)
14535 {
14536 struct breakpoint *bpt;
14537
14538 ALL_BREAKPOINTS (bpt)
14539 if (user_breakpoint_p (bpt))
14540 disable_breakpoint (bpt);
14541 }
14542 else
14543 {
14544 char *num = extract_arg (&args);
14545
14546 while (num)
14547 {
14548 if (strchr (num, '.'))
14549 {
14550 struct bp_location *loc = find_location_by_number (num);
14551
14552 if (loc)
14553 {
14554 if (loc->enabled)
14555 {
14556 loc->enabled = 0;
14557 mark_breakpoint_location_modified (loc);
14558 }
14559 if (target_supports_enable_disable_tracepoint ()
14560 && current_trace_status ()->running && loc->owner
14561 && is_tracepoint (loc->owner))
14562 target_disable_tracepoint (loc);
14563 }
14564 update_global_location_list (UGLL_DONT_INSERT);
14565 }
14566 else
14567 map_breakpoint_numbers (num, do_map_disable_breakpoint, NULL);
14568 num = extract_arg (&args);
14569 }
14570 }
14571 }
14572
14573 static void
14574 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14575 int count)
14576 {
14577 int target_resources_ok;
14578
14579 if (bpt->type == bp_hardware_breakpoint)
14580 {
14581 int i;
14582 i = hw_breakpoint_used_count ();
14583 target_resources_ok =
14584 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
14585 i + 1, 0);
14586 if (target_resources_ok == 0)
14587 error (_("No hardware breakpoint support in the target."));
14588 else if (target_resources_ok < 0)
14589 error (_("Hardware breakpoints used exceeds limit."));
14590 }
14591
14592 if (is_watchpoint (bpt))
14593 {
14594 /* Initialize it just to avoid a GCC false warning. */
14595 enum enable_state orig_enable_state = bp_disabled;
14596
14597 TRY
14598 {
14599 struct watchpoint *w = (struct watchpoint *) bpt;
14600
14601 orig_enable_state = bpt->enable_state;
14602 bpt->enable_state = bp_enabled;
14603 update_watchpoint (w, 1 /* reparse */);
14604 }
14605 CATCH (e, RETURN_MASK_ALL)
14606 {
14607 bpt->enable_state = orig_enable_state;
14608 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14609 bpt->number);
14610 return;
14611 }
14612 END_CATCH
14613 }
14614
14615 bpt->enable_state = bp_enabled;
14616
14617 /* Mark breakpoint locations modified. */
14618 mark_breakpoint_modified (bpt);
14619
14620 if (target_supports_enable_disable_tracepoint ()
14621 && current_trace_status ()->running && is_tracepoint (bpt))
14622 {
14623 struct bp_location *location;
14624
14625 for (location = bpt->loc; location; location = location->next)
14626 target_enable_tracepoint (location);
14627 }
14628
14629 bpt->disposition = disposition;
14630 bpt->enable_count = count;
14631 update_global_location_list (UGLL_MAY_INSERT);
14632
14633 observer_notify_breakpoint_modified (bpt);
14634 }
14635
14636
14637 void
14638 enable_breakpoint (struct breakpoint *bpt)
14639 {
14640 enable_breakpoint_disp (bpt, bpt->disposition, 0);
14641 }
14642
14643 static void
14644 do_enable_breakpoint (struct breakpoint *bpt, void *arg)
14645 {
14646 enable_breakpoint (bpt);
14647 }
14648
14649 /* A callback for map_breakpoint_numbers that calls
14650 enable_breakpoint. */
14651
14652 static void
14653 do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
14654 {
14655 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
14656 }
14657
14658 /* The enable command enables the specified breakpoints (or all defined
14659 breakpoints) so they once again become (or continue to be) effective
14660 in stopping the inferior. */
14661
14662 static void
14663 enable_command (char *args, int from_tty)
14664 {
14665 if (args == 0)
14666 {
14667 struct breakpoint *bpt;
14668
14669 ALL_BREAKPOINTS (bpt)
14670 if (user_breakpoint_p (bpt))
14671 enable_breakpoint (bpt);
14672 }
14673 else
14674 {
14675 char *num = extract_arg (&args);
14676
14677 while (num)
14678 {
14679 if (strchr (num, '.'))
14680 {
14681 struct bp_location *loc = find_location_by_number (num);
14682
14683 if (loc)
14684 {
14685 if (!loc->enabled)
14686 {
14687 loc->enabled = 1;
14688 mark_breakpoint_location_modified (loc);
14689 }
14690 if (target_supports_enable_disable_tracepoint ()
14691 && current_trace_status ()->running && loc->owner
14692 && is_tracepoint (loc->owner))
14693 target_enable_tracepoint (loc);
14694 }
14695 update_global_location_list (UGLL_MAY_INSERT);
14696 }
14697 else
14698 map_breakpoint_numbers (num, do_map_enable_breakpoint, NULL);
14699 num = extract_arg (&args);
14700 }
14701 }
14702 }
14703
14704 /* This struct packages up disposition data for application to multiple
14705 breakpoints. */
14706
14707 struct disp_data
14708 {
14709 enum bpdisp disp;
14710 int count;
14711 };
14712
14713 static void
14714 do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
14715 {
14716 struct disp_data disp_data = *(struct disp_data *) arg;
14717
14718 enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
14719 }
14720
14721 static void
14722 do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
14723 {
14724 struct disp_data disp = { disp_disable, 1 };
14725
14726 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14727 }
14728
14729 static void
14730 enable_once_command (char *args, int from_tty)
14731 {
14732 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
14733 }
14734
14735 static void
14736 do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
14737 {
14738 struct disp_data disp = { disp_disable, *(int *) countptr };
14739
14740 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14741 }
14742
14743 static void
14744 enable_count_command (char *args, int from_tty)
14745 {
14746 int count;
14747
14748 if (args == NULL)
14749 error_no_arg (_("hit count"));
14750
14751 count = get_number (&args);
14752
14753 map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
14754 }
14755
14756 static void
14757 do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
14758 {
14759 struct disp_data disp = { disp_del, 1 };
14760
14761 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14762 }
14763
14764 static void
14765 enable_delete_command (char *args, int from_tty)
14766 {
14767 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
14768 }
14769 \f
14770 static void
14771 set_breakpoint_cmd (char *args, int from_tty)
14772 {
14773 }
14774
14775 static void
14776 show_breakpoint_cmd (char *args, int from_tty)
14777 {
14778 }
14779
14780 /* Invalidate last known value of any hardware watchpoint if
14781 the memory which that value represents has been written to by
14782 GDB itself. */
14783
14784 static void
14785 invalidate_bp_value_on_memory_change (struct inferior *inferior,
14786 CORE_ADDR addr, ssize_t len,
14787 const bfd_byte *data)
14788 {
14789 struct breakpoint *bp;
14790
14791 ALL_BREAKPOINTS (bp)
14792 if (bp->enable_state == bp_enabled
14793 && bp->type == bp_hardware_watchpoint)
14794 {
14795 struct watchpoint *wp = (struct watchpoint *) bp;
14796
14797 if (wp->val_valid && wp->val)
14798 {
14799 struct bp_location *loc;
14800
14801 for (loc = bp->loc; loc != NULL; loc = loc->next)
14802 if (loc->loc_type == bp_loc_hardware_watchpoint
14803 && loc->address + loc->length > addr
14804 && addr + len > loc->address)
14805 {
14806 value_free (wp->val);
14807 wp->val = NULL;
14808 wp->val_valid = 0;
14809 }
14810 }
14811 }
14812 }
14813
14814 /* Create and insert a breakpoint for software single step. */
14815
14816 void
14817 insert_single_step_breakpoint (struct gdbarch *gdbarch,
14818 struct address_space *aspace,
14819 CORE_ADDR next_pc)
14820 {
14821 struct thread_info *tp = inferior_thread ();
14822 struct symtab_and_line sal;
14823 CORE_ADDR pc = next_pc;
14824
14825 if (tp->control.single_step_breakpoints == NULL)
14826 {
14827 tp->control.single_step_breakpoints
14828 = new_single_step_breakpoint (tp->num, gdbarch);
14829 }
14830
14831 sal = find_pc_line (pc, 0);
14832 sal.pc = pc;
14833 sal.section = find_pc_overlay (pc);
14834 sal.explicit_pc = 1;
14835 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
14836
14837 update_global_location_list (UGLL_INSERT);
14838 }
14839
14840 /* See breakpoint.h. */
14841
14842 int
14843 breakpoint_has_location_inserted_here (struct breakpoint *bp,
14844 struct address_space *aspace,
14845 CORE_ADDR pc)
14846 {
14847 struct bp_location *loc;
14848
14849 for (loc = bp->loc; loc != NULL; loc = loc->next)
14850 if (loc->inserted
14851 && breakpoint_location_address_match (loc, aspace, pc))
14852 return 1;
14853
14854 return 0;
14855 }
14856
14857 /* Check whether a software single-step breakpoint is inserted at
14858 PC. */
14859
14860 int
14861 single_step_breakpoint_inserted_here_p (struct address_space *aspace,
14862 CORE_ADDR pc)
14863 {
14864 struct breakpoint *bpt;
14865
14866 ALL_BREAKPOINTS (bpt)
14867 {
14868 if (bpt->type == bp_single_step
14869 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
14870 return 1;
14871 }
14872 return 0;
14873 }
14874
14875 /* Tracepoint-specific operations. */
14876
14877 /* Set tracepoint count to NUM. */
14878 static void
14879 set_tracepoint_count (int num)
14880 {
14881 tracepoint_count = num;
14882 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
14883 }
14884
14885 static void
14886 trace_command (char *arg, int from_tty)
14887 {
14888 struct breakpoint_ops *ops;
14889 const char *arg_cp = arg;
14890
14891 if (arg && probe_linespec_to_ops (&arg_cp))
14892 ops = &tracepoint_probe_breakpoint_ops;
14893 else
14894 ops = &tracepoint_breakpoint_ops;
14895
14896 create_breakpoint (get_current_arch (),
14897 arg,
14898 NULL, 0, NULL, 1 /* parse arg */,
14899 0 /* tempflag */,
14900 bp_tracepoint /* type_wanted */,
14901 0 /* Ignore count */,
14902 pending_break_support,
14903 ops,
14904 from_tty,
14905 1 /* enabled */,
14906 0 /* internal */, 0);
14907 }
14908
14909 static void
14910 ftrace_command (char *arg, int from_tty)
14911 {
14912 create_breakpoint (get_current_arch (),
14913 arg,
14914 NULL, 0, NULL, 1 /* parse arg */,
14915 0 /* tempflag */,
14916 bp_fast_tracepoint /* type_wanted */,
14917 0 /* Ignore count */,
14918 pending_break_support,
14919 &tracepoint_breakpoint_ops,
14920 from_tty,
14921 1 /* enabled */,
14922 0 /* internal */, 0);
14923 }
14924
14925 /* strace command implementation. Creates a static tracepoint. */
14926
14927 static void
14928 strace_command (char *arg, int from_tty)
14929 {
14930 struct breakpoint_ops *ops;
14931
14932 /* Decide if we are dealing with a static tracepoint marker (`-m'),
14933 or with a normal static tracepoint. */
14934 if (arg && startswith (arg, "-m") && isspace (arg[2]))
14935 ops = &strace_marker_breakpoint_ops;
14936 else
14937 ops = &tracepoint_breakpoint_ops;
14938
14939 create_breakpoint (get_current_arch (),
14940 arg,
14941 NULL, 0, NULL, 1 /* parse arg */,
14942 0 /* tempflag */,
14943 bp_static_tracepoint /* type_wanted */,
14944 0 /* Ignore count */,
14945 pending_break_support,
14946 ops,
14947 from_tty,
14948 1 /* enabled */,
14949 0 /* internal */, 0);
14950 }
14951
14952 /* Set up a fake reader function that gets command lines from a linked
14953 list that was acquired during tracepoint uploading. */
14954
14955 static struct uploaded_tp *this_utp;
14956 static int next_cmd;
14957
14958 static char *
14959 read_uploaded_action (void)
14960 {
14961 char *rslt;
14962
14963 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
14964
14965 next_cmd++;
14966
14967 return rslt;
14968 }
14969
14970 /* Given information about a tracepoint as recorded on a target (which
14971 can be either a live system or a trace file), attempt to create an
14972 equivalent GDB tracepoint. This is not a reliable process, since
14973 the target does not necessarily have all the information used when
14974 the tracepoint was originally defined. */
14975
14976 struct tracepoint *
14977 create_tracepoint_from_upload (struct uploaded_tp *utp)
14978 {
14979 char *addr_str, small_buf[100];
14980 struct tracepoint *tp;
14981
14982 if (utp->at_string)
14983 addr_str = utp->at_string;
14984 else
14985 {
14986 /* In the absence of a source location, fall back to raw
14987 address. Since there is no way to confirm that the address
14988 means the same thing as when the trace was started, warn the
14989 user. */
14990 warning (_("Uploaded tracepoint %d has no "
14991 "source location, using raw address"),
14992 utp->number);
14993 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
14994 addr_str = small_buf;
14995 }
14996
14997 /* There's not much we can do with a sequence of bytecodes. */
14998 if (utp->cond && !utp->cond_string)
14999 warning (_("Uploaded tracepoint %d condition "
15000 "has no source form, ignoring it"),
15001 utp->number);
15002
15003 if (!create_breakpoint (get_current_arch (),
15004 addr_str,
15005 utp->cond_string, -1, NULL,
15006 0 /* parse cond/thread */,
15007 0 /* tempflag */,
15008 utp->type /* type_wanted */,
15009 0 /* Ignore count */,
15010 pending_break_support,
15011 &tracepoint_breakpoint_ops,
15012 0 /* from_tty */,
15013 utp->enabled /* enabled */,
15014 0 /* internal */,
15015 CREATE_BREAKPOINT_FLAGS_INSERTED))
15016 return NULL;
15017
15018 /* Get the tracepoint we just created. */
15019 tp = get_tracepoint (tracepoint_count);
15020 gdb_assert (tp != NULL);
15021
15022 if (utp->pass > 0)
15023 {
15024 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
15025 tp->base.number);
15026
15027 trace_pass_command (small_buf, 0);
15028 }
15029
15030 /* If we have uploaded versions of the original commands, set up a
15031 special-purpose "reader" function and call the usual command line
15032 reader, then pass the result to the breakpoint command-setting
15033 function. */
15034 if (!VEC_empty (char_ptr, utp->cmd_strings))
15035 {
15036 struct command_line *cmd_list;
15037
15038 this_utp = utp;
15039 next_cmd = 0;
15040
15041 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
15042
15043 breakpoint_set_commands (&tp->base, cmd_list);
15044 }
15045 else if (!VEC_empty (char_ptr, utp->actions)
15046 || !VEC_empty (char_ptr, utp->step_actions))
15047 warning (_("Uploaded tracepoint %d actions "
15048 "have no source form, ignoring them"),
15049 utp->number);
15050
15051 /* Copy any status information that might be available. */
15052 tp->base.hit_count = utp->hit_count;
15053 tp->traceframe_usage = utp->traceframe_usage;
15054
15055 return tp;
15056 }
15057
15058 /* Print information on tracepoint number TPNUM_EXP, or all if
15059 omitted. */
15060
15061 static void
15062 tracepoints_info (char *args, int from_tty)
15063 {
15064 struct ui_out *uiout = current_uiout;
15065 int num_printed;
15066
15067 num_printed = breakpoint_1 (args, 0, is_tracepoint);
15068
15069 if (num_printed == 0)
15070 {
15071 if (args == NULL || *args == '\0')
15072 ui_out_message (uiout, 0, "No tracepoints.\n");
15073 else
15074 ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
15075 }
15076
15077 default_collect_info ();
15078 }
15079
15080 /* The 'enable trace' command enables tracepoints.
15081 Not supported by all targets. */
15082 static void
15083 enable_trace_command (char *args, int from_tty)
15084 {
15085 enable_command (args, from_tty);
15086 }
15087
15088 /* The 'disable trace' command disables tracepoints.
15089 Not supported by all targets. */
15090 static void
15091 disable_trace_command (char *args, int from_tty)
15092 {
15093 disable_command (args, from_tty);
15094 }
15095
15096 /* Remove a tracepoint (or all if no argument). */
15097 static void
15098 delete_trace_command (char *arg, int from_tty)
15099 {
15100 struct breakpoint *b, *b_tmp;
15101
15102 dont_repeat ();
15103
15104 if (arg == 0)
15105 {
15106 int breaks_to_delete = 0;
15107
15108 /* Delete all breakpoints if no argument.
15109 Do not delete internal or call-dummy breakpoints, these
15110 have to be deleted with an explicit breakpoint number
15111 argument. */
15112 ALL_TRACEPOINTS (b)
15113 if (is_tracepoint (b) && user_breakpoint_p (b))
15114 {
15115 breaks_to_delete = 1;
15116 break;
15117 }
15118
15119 /* Ask user only if there are some breakpoints to delete. */
15120 if (!from_tty
15121 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
15122 {
15123 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15124 if (is_tracepoint (b) && user_breakpoint_p (b))
15125 delete_breakpoint (b);
15126 }
15127 }
15128 else
15129 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
15130 }
15131
15132 /* Helper function for trace_pass_command. */
15133
15134 static void
15135 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
15136 {
15137 tp->pass_count = count;
15138 observer_notify_breakpoint_modified (&tp->base);
15139 if (from_tty)
15140 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
15141 tp->base.number, count);
15142 }
15143
15144 /* Set passcount for tracepoint.
15145
15146 First command argument is passcount, second is tracepoint number.
15147 If tracepoint number omitted, apply to most recently defined.
15148 Also accepts special argument "all". */
15149
15150 static void
15151 trace_pass_command (char *args, int from_tty)
15152 {
15153 struct tracepoint *t1;
15154 unsigned int count;
15155
15156 if (args == 0 || *args == 0)
15157 error (_("passcount command requires an "
15158 "argument (count + optional TP num)"));
15159
15160 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
15161
15162 args = skip_spaces (args);
15163 if (*args && strncasecmp (args, "all", 3) == 0)
15164 {
15165 struct breakpoint *b;
15166
15167 args += 3; /* Skip special argument "all". */
15168 if (*args)
15169 error (_("Junk at end of arguments."));
15170
15171 ALL_TRACEPOINTS (b)
15172 {
15173 t1 = (struct tracepoint *) b;
15174 trace_pass_set_count (t1, count, from_tty);
15175 }
15176 }
15177 else if (*args == '\0')
15178 {
15179 t1 = get_tracepoint_by_number (&args, NULL);
15180 if (t1)
15181 trace_pass_set_count (t1, count, from_tty);
15182 }
15183 else
15184 {
15185 struct get_number_or_range_state state;
15186
15187 init_number_or_range (&state, args);
15188 while (!state.finished)
15189 {
15190 t1 = get_tracepoint_by_number (&args, &state);
15191 if (t1)
15192 trace_pass_set_count (t1, count, from_tty);
15193 }
15194 }
15195 }
15196
15197 struct tracepoint *
15198 get_tracepoint (int num)
15199 {
15200 struct breakpoint *t;
15201
15202 ALL_TRACEPOINTS (t)
15203 if (t->number == num)
15204 return (struct tracepoint *) t;
15205
15206 return NULL;
15207 }
15208
15209 /* Find the tracepoint with the given target-side number (which may be
15210 different from the tracepoint number after disconnecting and
15211 reconnecting). */
15212
15213 struct tracepoint *
15214 get_tracepoint_by_number_on_target (int num)
15215 {
15216 struct breakpoint *b;
15217
15218 ALL_TRACEPOINTS (b)
15219 {
15220 struct tracepoint *t = (struct tracepoint *) b;
15221
15222 if (t->number_on_target == num)
15223 return t;
15224 }
15225
15226 return NULL;
15227 }
15228
15229 /* Utility: parse a tracepoint number and look it up in the list.
15230 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
15231 If the argument is missing, the most recent tracepoint
15232 (tracepoint_count) is returned. */
15233
15234 struct tracepoint *
15235 get_tracepoint_by_number (char **arg,
15236 struct get_number_or_range_state *state)
15237 {
15238 struct breakpoint *t;
15239 int tpnum;
15240 char *instring = arg == NULL ? NULL : *arg;
15241
15242 if (state)
15243 {
15244 gdb_assert (!state->finished);
15245 tpnum = get_number_or_range (state);
15246 }
15247 else if (arg == NULL || *arg == NULL || ! **arg)
15248 tpnum = tracepoint_count;
15249 else
15250 tpnum = get_number (arg);
15251
15252 if (tpnum <= 0)
15253 {
15254 if (instring && *instring)
15255 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15256 instring);
15257 else
15258 printf_filtered (_("No previous tracepoint\n"));
15259 return NULL;
15260 }
15261
15262 ALL_TRACEPOINTS (t)
15263 if (t->number == tpnum)
15264 {
15265 return (struct tracepoint *) t;
15266 }
15267
15268 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15269 return NULL;
15270 }
15271
15272 void
15273 print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15274 {
15275 if (b->thread != -1)
15276 fprintf_unfiltered (fp, " thread %d", b->thread);
15277
15278 if (b->task != 0)
15279 fprintf_unfiltered (fp, " task %d", b->task);
15280
15281 fprintf_unfiltered (fp, "\n");
15282 }
15283
15284 /* Save information on user settable breakpoints (watchpoints, etc) to
15285 a new script file named FILENAME. If FILTER is non-NULL, call it
15286 on each breakpoint and only include the ones for which it returns
15287 non-zero. */
15288
15289 static void
15290 save_breakpoints (char *filename, int from_tty,
15291 int (*filter) (const struct breakpoint *))
15292 {
15293 struct breakpoint *tp;
15294 int any = 0;
15295 struct cleanup *cleanup;
15296 struct ui_file *fp;
15297 int extra_trace_bits = 0;
15298
15299 if (filename == 0 || *filename == 0)
15300 error (_("Argument required (file name in which to save)"));
15301
15302 /* See if we have anything to save. */
15303 ALL_BREAKPOINTS (tp)
15304 {
15305 /* Skip internal and momentary breakpoints. */
15306 if (!user_breakpoint_p (tp))
15307 continue;
15308
15309 /* If we have a filter, only save the breakpoints it accepts. */
15310 if (filter && !filter (tp))
15311 continue;
15312
15313 any = 1;
15314
15315 if (is_tracepoint (tp))
15316 {
15317 extra_trace_bits = 1;
15318
15319 /* We can stop searching. */
15320 break;
15321 }
15322 }
15323
15324 if (!any)
15325 {
15326 warning (_("Nothing to save."));
15327 return;
15328 }
15329
15330 filename = tilde_expand (filename);
15331 cleanup = make_cleanup (xfree, filename);
15332 fp = gdb_fopen (filename, "w");
15333 if (!fp)
15334 error (_("Unable to open file '%s' for saving (%s)"),
15335 filename, safe_strerror (errno));
15336 make_cleanup_ui_file_delete (fp);
15337
15338 if (extra_trace_bits)
15339 save_trace_state_variables (fp);
15340
15341 ALL_BREAKPOINTS (tp)
15342 {
15343 /* Skip internal and momentary breakpoints. */
15344 if (!user_breakpoint_p (tp))
15345 continue;
15346
15347 /* If we have a filter, only save the breakpoints it accepts. */
15348 if (filter && !filter (tp))
15349 continue;
15350
15351 tp->ops->print_recreate (tp, fp);
15352
15353 /* Note, we can't rely on tp->number for anything, as we can't
15354 assume the recreated breakpoint numbers will match. Use $bpnum
15355 instead. */
15356
15357 if (tp->cond_string)
15358 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
15359
15360 if (tp->ignore_count)
15361 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
15362
15363 if (tp->type != bp_dprintf && tp->commands)
15364 {
15365 struct gdb_exception exception;
15366
15367 fprintf_unfiltered (fp, " commands\n");
15368
15369 ui_out_redirect (current_uiout, fp);
15370 TRY
15371 {
15372 print_command_lines (current_uiout, tp->commands->commands, 2);
15373 }
15374 CATCH (ex, RETURN_MASK_ALL)
15375 {
15376 ui_out_redirect (current_uiout, NULL);
15377 throw_exception (ex);
15378 }
15379 END_CATCH
15380
15381 ui_out_redirect (current_uiout, NULL);
15382 fprintf_unfiltered (fp, " end\n");
15383 }
15384
15385 if (tp->enable_state == bp_disabled)
15386 fprintf_unfiltered (fp, "disable $bpnum\n");
15387
15388 /* If this is a multi-location breakpoint, check if the locations
15389 should be individually disabled. Watchpoint locations are
15390 special, and not user visible. */
15391 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15392 {
15393 struct bp_location *loc;
15394 int n = 1;
15395
15396 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15397 if (!loc->enabled)
15398 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
15399 }
15400 }
15401
15402 if (extra_trace_bits && *default_collect)
15403 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
15404
15405 if (from_tty)
15406 printf_filtered (_("Saved to file '%s'.\n"), filename);
15407 do_cleanups (cleanup);
15408 }
15409
15410 /* The `save breakpoints' command. */
15411
15412 static void
15413 save_breakpoints_command (char *args, int from_tty)
15414 {
15415 save_breakpoints (args, from_tty, NULL);
15416 }
15417
15418 /* The `save tracepoints' command. */
15419
15420 static void
15421 save_tracepoints_command (char *args, int from_tty)
15422 {
15423 save_breakpoints (args, from_tty, is_tracepoint);
15424 }
15425
15426 /* Create a vector of all tracepoints. */
15427
15428 VEC(breakpoint_p) *
15429 all_tracepoints (void)
15430 {
15431 VEC(breakpoint_p) *tp_vec = 0;
15432 struct breakpoint *tp;
15433
15434 ALL_TRACEPOINTS (tp)
15435 {
15436 VEC_safe_push (breakpoint_p, tp_vec, tp);
15437 }
15438
15439 return tp_vec;
15440 }
15441
15442 \f
15443 /* This help string is used for the break, hbreak, tbreak and thbreak
15444 commands. It is defined as a macro to prevent duplication.
15445 COMMAND should be a string constant containing the name of the
15446 command. */
15447 #define BREAK_ARGS_HELP(command) \
15448 command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15449 PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15450 probe point. Accepted values are `-probe' (for a generic, automatically\n\
15451 guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15452 `-probe-dtrace' (for a DTrace probe).\n\
15453 LOCATION may be a line number, function name, or \"*\" and an address.\n\
15454 If a line number is specified, break at start of code for that line.\n\
15455 If a function is specified, break at start of code for that function.\n\
15456 If an address is specified, break at that exact address.\n\
15457 With no LOCATION, uses current execution address of the selected\n\
15458 stack frame. This is useful for breaking on return to a stack frame.\n\
15459 \n\
15460 THREADNUM is the number from \"info threads\".\n\
15461 CONDITION is a boolean expression.\n\
15462 \n\
15463 Multiple breakpoints at one place are permitted, and useful if their\n\
15464 conditions are different.\n\
15465 \n\
15466 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15467
15468 /* List of subcommands for "catch". */
15469 static struct cmd_list_element *catch_cmdlist;
15470
15471 /* List of subcommands for "tcatch". */
15472 static struct cmd_list_element *tcatch_cmdlist;
15473
15474 void
15475 add_catch_command (char *name, char *docstring,
15476 cmd_sfunc_ftype *sfunc,
15477 completer_ftype *completer,
15478 void *user_data_catch,
15479 void *user_data_tcatch)
15480 {
15481 struct cmd_list_element *command;
15482
15483 command = add_cmd (name, class_breakpoint, NULL, docstring,
15484 &catch_cmdlist);
15485 set_cmd_sfunc (command, sfunc);
15486 set_cmd_context (command, user_data_catch);
15487 set_cmd_completer (command, completer);
15488
15489 command = add_cmd (name, class_breakpoint, NULL, docstring,
15490 &tcatch_cmdlist);
15491 set_cmd_sfunc (command, sfunc);
15492 set_cmd_context (command, user_data_tcatch);
15493 set_cmd_completer (command, completer);
15494 }
15495
15496 static void
15497 save_command (char *arg, int from_tty)
15498 {
15499 printf_unfiltered (_("\"save\" must be followed by "
15500 "the name of a save subcommand.\n"));
15501 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
15502 }
15503
15504 struct breakpoint *
15505 iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15506 void *data)
15507 {
15508 struct breakpoint *b, *b_tmp;
15509
15510 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15511 {
15512 if ((*callback) (b, data))
15513 return b;
15514 }
15515
15516 return NULL;
15517 }
15518
15519 /* Zero if any of the breakpoint's locations could be a location where
15520 functions have been inlined, nonzero otherwise. */
15521
15522 static int
15523 is_non_inline_function (struct breakpoint *b)
15524 {
15525 /* The shared library event breakpoint is set on the address of a
15526 non-inline function. */
15527 if (b->type == bp_shlib_event)
15528 return 1;
15529
15530 return 0;
15531 }
15532
15533 /* Nonzero if the specified PC cannot be a location where functions
15534 have been inlined. */
15535
15536 int
15537 pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
15538 const struct target_waitstatus *ws)
15539 {
15540 struct breakpoint *b;
15541 struct bp_location *bl;
15542
15543 ALL_BREAKPOINTS (b)
15544 {
15545 if (!is_non_inline_function (b))
15546 continue;
15547
15548 for (bl = b->loc; bl != NULL; bl = bl->next)
15549 {
15550 if (!bl->shlib_disabled
15551 && bpstat_check_location (bl, aspace, pc, ws))
15552 return 1;
15553 }
15554 }
15555
15556 return 0;
15557 }
15558
15559 /* Remove any references to OBJFILE which is going to be freed. */
15560
15561 void
15562 breakpoint_free_objfile (struct objfile *objfile)
15563 {
15564 struct bp_location **locp, *loc;
15565
15566 ALL_BP_LOCATIONS (loc, locp)
15567 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
15568 loc->symtab = NULL;
15569 }
15570
15571 void
15572 initialize_breakpoint_ops (void)
15573 {
15574 static int initialized = 0;
15575
15576 struct breakpoint_ops *ops;
15577
15578 if (initialized)
15579 return;
15580 initialized = 1;
15581
15582 /* The breakpoint_ops structure to be inherit by all kinds of
15583 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15584 internal and momentary breakpoints, etc.). */
15585 ops = &bkpt_base_breakpoint_ops;
15586 *ops = base_breakpoint_ops;
15587 ops->re_set = bkpt_re_set;
15588 ops->insert_location = bkpt_insert_location;
15589 ops->remove_location = bkpt_remove_location;
15590 ops->breakpoint_hit = bkpt_breakpoint_hit;
15591 ops->create_sals_from_address = bkpt_create_sals_from_address;
15592 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
15593 ops->decode_linespec = bkpt_decode_linespec;
15594
15595 /* The breakpoint_ops structure to be used in regular breakpoints. */
15596 ops = &bkpt_breakpoint_ops;
15597 *ops = bkpt_base_breakpoint_ops;
15598 ops->re_set = bkpt_re_set;
15599 ops->resources_needed = bkpt_resources_needed;
15600 ops->print_it = bkpt_print_it;
15601 ops->print_mention = bkpt_print_mention;
15602 ops->print_recreate = bkpt_print_recreate;
15603
15604 /* Ranged breakpoints. */
15605 ops = &ranged_breakpoint_ops;
15606 *ops = bkpt_breakpoint_ops;
15607 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15608 ops->resources_needed = resources_needed_ranged_breakpoint;
15609 ops->print_it = print_it_ranged_breakpoint;
15610 ops->print_one = print_one_ranged_breakpoint;
15611 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15612 ops->print_mention = print_mention_ranged_breakpoint;
15613 ops->print_recreate = print_recreate_ranged_breakpoint;
15614
15615 /* Internal breakpoints. */
15616 ops = &internal_breakpoint_ops;
15617 *ops = bkpt_base_breakpoint_ops;
15618 ops->re_set = internal_bkpt_re_set;
15619 ops->check_status = internal_bkpt_check_status;
15620 ops->print_it = internal_bkpt_print_it;
15621 ops->print_mention = internal_bkpt_print_mention;
15622
15623 /* Momentary breakpoints. */
15624 ops = &momentary_breakpoint_ops;
15625 *ops = bkpt_base_breakpoint_ops;
15626 ops->re_set = momentary_bkpt_re_set;
15627 ops->check_status = momentary_bkpt_check_status;
15628 ops->print_it = momentary_bkpt_print_it;
15629 ops->print_mention = momentary_bkpt_print_mention;
15630
15631 /* Momentary breakpoints for bp_longjmp and bp_exception. */
15632 ops = &longjmp_breakpoint_ops;
15633 *ops = momentary_breakpoint_ops;
15634 ops->dtor = longjmp_bkpt_dtor;
15635
15636 /* Probe breakpoints. */
15637 ops = &bkpt_probe_breakpoint_ops;
15638 *ops = bkpt_breakpoint_ops;
15639 ops->insert_location = bkpt_probe_insert_location;
15640 ops->remove_location = bkpt_probe_remove_location;
15641 ops->create_sals_from_address = bkpt_probe_create_sals_from_address;
15642 ops->decode_linespec = bkpt_probe_decode_linespec;
15643
15644 /* Watchpoints. */
15645 ops = &watchpoint_breakpoint_ops;
15646 *ops = base_breakpoint_ops;
15647 ops->dtor = dtor_watchpoint;
15648 ops->re_set = re_set_watchpoint;
15649 ops->insert_location = insert_watchpoint;
15650 ops->remove_location = remove_watchpoint;
15651 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15652 ops->check_status = check_status_watchpoint;
15653 ops->resources_needed = resources_needed_watchpoint;
15654 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15655 ops->print_it = print_it_watchpoint;
15656 ops->print_mention = print_mention_watchpoint;
15657 ops->print_recreate = print_recreate_watchpoint;
15658 ops->explains_signal = explains_signal_watchpoint;
15659
15660 /* Masked watchpoints. */
15661 ops = &masked_watchpoint_breakpoint_ops;
15662 *ops = watchpoint_breakpoint_ops;
15663 ops->insert_location = insert_masked_watchpoint;
15664 ops->remove_location = remove_masked_watchpoint;
15665 ops->resources_needed = resources_needed_masked_watchpoint;
15666 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15667 ops->print_it = print_it_masked_watchpoint;
15668 ops->print_one_detail = print_one_detail_masked_watchpoint;
15669 ops->print_mention = print_mention_masked_watchpoint;
15670 ops->print_recreate = print_recreate_masked_watchpoint;
15671
15672 /* Tracepoints. */
15673 ops = &tracepoint_breakpoint_ops;
15674 *ops = base_breakpoint_ops;
15675 ops->re_set = tracepoint_re_set;
15676 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15677 ops->print_one_detail = tracepoint_print_one_detail;
15678 ops->print_mention = tracepoint_print_mention;
15679 ops->print_recreate = tracepoint_print_recreate;
15680 ops->create_sals_from_address = tracepoint_create_sals_from_address;
15681 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
15682 ops->decode_linespec = tracepoint_decode_linespec;
15683
15684 /* Probe tracepoints. */
15685 ops = &tracepoint_probe_breakpoint_ops;
15686 *ops = tracepoint_breakpoint_ops;
15687 ops->create_sals_from_address = tracepoint_probe_create_sals_from_address;
15688 ops->decode_linespec = tracepoint_probe_decode_linespec;
15689
15690 /* Static tracepoints with marker (`-m'). */
15691 ops = &strace_marker_breakpoint_ops;
15692 *ops = tracepoint_breakpoint_ops;
15693 ops->create_sals_from_address = strace_marker_create_sals_from_address;
15694 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
15695 ops->decode_linespec = strace_marker_decode_linespec;
15696
15697 /* Fork catchpoints. */
15698 ops = &catch_fork_breakpoint_ops;
15699 *ops = base_breakpoint_ops;
15700 ops->insert_location = insert_catch_fork;
15701 ops->remove_location = remove_catch_fork;
15702 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15703 ops->print_it = print_it_catch_fork;
15704 ops->print_one = print_one_catch_fork;
15705 ops->print_mention = print_mention_catch_fork;
15706 ops->print_recreate = print_recreate_catch_fork;
15707
15708 /* Vfork catchpoints. */
15709 ops = &catch_vfork_breakpoint_ops;
15710 *ops = base_breakpoint_ops;
15711 ops->insert_location = insert_catch_vfork;
15712 ops->remove_location = remove_catch_vfork;
15713 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15714 ops->print_it = print_it_catch_vfork;
15715 ops->print_one = print_one_catch_vfork;
15716 ops->print_mention = print_mention_catch_vfork;
15717 ops->print_recreate = print_recreate_catch_vfork;
15718
15719 /* Exec catchpoints. */
15720 ops = &catch_exec_breakpoint_ops;
15721 *ops = base_breakpoint_ops;
15722 ops->dtor = dtor_catch_exec;
15723 ops->insert_location = insert_catch_exec;
15724 ops->remove_location = remove_catch_exec;
15725 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15726 ops->print_it = print_it_catch_exec;
15727 ops->print_one = print_one_catch_exec;
15728 ops->print_mention = print_mention_catch_exec;
15729 ops->print_recreate = print_recreate_catch_exec;
15730
15731 /* Solib-related catchpoints. */
15732 ops = &catch_solib_breakpoint_ops;
15733 *ops = base_breakpoint_ops;
15734 ops->dtor = dtor_catch_solib;
15735 ops->insert_location = insert_catch_solib;
15736 ops->remove_location = remove_catch_solib;
15737 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15738 ops->check_status = check_status_catch_solib;
15739 ops->print_it = print_it_catch_solib;
15740 ops->print_one = print_one_catch_solib;
15741 ops->print_mention = print_mention_catch_solib;
15742 ops->print_recreate = print_recreate_catch_solib;
15743
15744 ops = &dprintf_breakpoint_ops;
15745 *ops = bkpt_base_breakpoint_ops;
15746 ops->re_set = dprintf_re_set;
15747 ops->resources_needed = bkpt_resources_needed;
15748 ops->print_it = bkpt_print_it;
15749 ops->print_mention = bkpt_print_mention;
15750 ops->print_recreate = dprintf_print_recreate;
15751 ops->after_condition_true = dprintf_after_condition_true;
15752 ops->breakpoint_hit = dprintf_breakpoint_hit;
15753 }
15754
15755 /* Chain containing all defined "enable breakpoint" subcommands. */
15756
15757 static struct cmd_list_element *enablebreaklist = NULL;
15758
15759 void
15760 _initialize_breakpoint (void)
15761 {
15762 struct cmd_list_element *c;
15763
15764 initialize_breakpoint_ops ();
15765
15766 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
15767 observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
15768 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
15769
15770 breakpoint_objfile_key
15771 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
15772
15773 breakpoint_chain = 0;
15774 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
15775 before a breakpoint is set. */
15776 breakpoint_count = 0;
15777
15778 tracepoint_count = 0;
15779
15780 add_com ("ignore", class_breakpoint, ignore_command, _("\
15781 Set ignore-count of breakpoint number N to COUNT.\n\
15782 Usage is `ignore N COUNT'."));
15783
15784 add_com ("commands", class_breakpoint, commands_command, _("\
15785 Set commands to be executed when a breakpoint is hit.\n\
15786 Give breakpoint number as argument after \"commands\".\n\
15787 With no argument, the targeted breakpoint is the last one set.\n\
15788 The commands themselves follow starting on the next line.\n\
15789 Type a line containing \"end\" to indicate the end of them.\n\
15790 Give \"silent\" as the first line to make the breakpoint silent;\n\
15791 then no output is printed when it is hit, except what the commands print."));
15792
15793 c = add_com ("condition", class_breakpoint, condition_command, _("\
15794 Specify breakpoint number N to break only if COND is true.\n\
15795 Usage is `condition N COND', where N is an integer and COND is an\n\
15796 expression to be evaluated whenever breakpoint N is reached."));
15797 set_cmd_completer (c, condition_completer);
15798
15799 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
15800 Set a temporary breakpoint.\n\
15801 Like \"break\" except the breakpoint is only temporary,\n\
15802 so it will be deleted when hit. Equivalent to \"break\" followed\n\
15803 by using \"enable delete\" on the breakpoint number.\n\
15804 \n"
15805 BREAK_ARGS_HELP ("tbreak")));
15806 set_cmd_completer (c, location_completer);
15807
15808 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
15809 Set a hardware assisted breakpoint.\n\
15810 Like \"break\" except the breakpoint requires hardware support,\n\
15811 some target hardware may not have this support.\n\
15812 \n"
15813 BREAK_ARGS_HELP ("hbreak")));
15814 set_cmd_completer (c, location_completer);
15815
15816 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
15817 Set a temporary hardware assisted breakpoint.\n\
15818 Like \"hbreak\" except the breakpoint is only temporary,\n\
15819 so it will be deleted when hit.\n\
15820 \n"
15821 BREAK_ARGS_HELP ("thbreak")));
15822 set_cmd_completer (c, location_completer);
15823
15824 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
15825 Enable some breakpoints.\n\
15826 Give breakpoint numbers (separated by spaces) as arguments.\n\
15827 With no subcommand, breakpoints are enabled until you command otherwise.\n\
15828 This is used to cancel the effect of the \"disable\" command.\n\
15829 With a subcommand you can enable temporarily."),
15830 &enablelist, "enable ", 1, &cmdlist);
15831
15832 add_com_alias ("en", "enable", class_breakpoint, 1);
15833
15834 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
15835 Enable some breakpoints.\n\
15836 Give breakpoint numbers (separated by spaces) as arguments.\n\
15837 This is used to cancel the effect of the \"disable\" command.\n\
15838 May be abbreviated to simply \"enable\".\n"),
15839 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
15840
15841 add_cmd ("once", no_class, enable_once_command, _("\
15842 Enable breakpoints for one hit. Give breakpoint numbers.\n\
15843 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15844 &enablebreaklist);
15845
15846 add_cmd ("delete", no_class, enable_delete_command, _("\
15847 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
15848 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15849 &enablebreaklist);
15850
15851 add_cmd ("count", no_class, enable_count_command, _("\
15852 Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
15853 If a breakpoint is hit while enabled in this fashion,\n\
15854 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15855 &enablebreaklist);
15856
15857 add_cmd ("delete", no_class, enable_delete_command, _("\
15858 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
15859 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15860 &enablelist);
15861
15862 add_cmd ("once", no_class, enable_once_command, _("\
15863 Enable breakpoints for one hit. Give breakpoint numbers.\n\
15864 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15865 &enablelist);
15866
15867 add_cmd ("count", no_class, enable_count_command, _("\
15868 Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
15869 If a breakpoint is hit while enabled in this fashion,\n\
15870 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15871 &enablelist);
15872
15873 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
15874 Disable some breakpoints.\n\
15875 Arguments are breakpoint numbers with spaces in between.\n\
15876 To disable all breakpoints, give no argument.\n\
15877 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
15878 &disablelist, "disable ", 1, &cmdlist);
15879 add_com_alias ("dis", "disable", class_breakpoint, 1);
15880 add_com_alias ("disa", "disable", class_breakpoint, 1);
15881
15882 add_cmd ("breakpoints", class_alias, disable_command, _("\
15883 Disable some breakpoints.\n\
15884 Arguments are breakpoint numbers with spaces in between.\n\
15885 To disable all breakpoints, give no argument.\n\
15886 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
15887 This command may be abbreviated \"disable\"."),
15888 &disablelist);
15889
15890 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
15891 Delete some breakpoints or auto-display expressions.\n\
15892 Arguments are breakpoint numbers with spaces in between.\n\
15893 To delete all breakpoints, give no argument.\n\
15894 \n\
15895 Also a prefix command for deletion of other GDB objects.\n\
15896 The \"unset\" command is also an alias for \"delete\"."),
15897 &deletelist, "delete ", 1, &cmdlist);
15898 add_com_alias ("d", "delete", class_breakpoint, 1);
15899 add_com_alias ("del", "delete", class_breakpoint, 1);
15900
15901 add_cmd ("breakpoints", class_alias, delete_command, _("\
15902 Delete some breakpoints or auto-display expressions.\n\
15903 Arguments are breakpoint numbers with spaces in between.\n\
15904 To delete all breakpoints, give no argument.\n\
15905 This command may be abbreviated \"delete\"."),
15906 &deletelist);
15907
15908 add_com ("clear", class_breakpoint, clear_command, _("\
15909 Clear breakpoint at specified line or function.\n\
15910 Argument may be line number, function name, or \"*\" and an address.\n\
15911 If line number is specified, all breakpoints in that line are cleared.\n\
15912 If function is specified, breakpoints at beginning of function are cleared.\n\
15913 If an address is specified, breakpoints at that address are cleared.\n\
15914 \n\
15915 With no argument, clears all breakpoints in the line that the selected frame\n\
15916 is executing in.\n\
15917 \n\
15918 See also the \"delete\" command which clears breakpoints by number."));
15919 add_com_alias ("cl", "clear", class_breakpoint, 1);
15920
15921 c = add_com ("break", class_breakpoint, break_command, _("\
15922 Set breakpoint at specified line or function.\n"
15923 BREAK_ARGS_HELP ("break")));
15924 set_cmd_completer (c, location_completer);
15925
15926 add_com_alias ("b", "break", class_run, 1);
15927 add_com_alias ("br", "break", class_run, 1);
15928 add_com_alias ("bre", "break", class_run, 1);
15929 add_com_alias ("brea", "break", class_run, 1);
15930
15931 if (dbx_commands)
15932 {
15933 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
15934 Break in function/address or break at a line in the current file."),
15935 &stoplist, "stop ", 1, &cmdlist);
15936 add_cmd ("in", class_breakpoint, stopin_command,
15937 _("Break in function or address."), &stoplist);
15938 add_cmd ("at", class_breakpoint, stopat_command,
15939 _("Break at a line in the current file."), &stoplist);
15940 add_com ("status", class_info, breakpoints_info, _("\
15941 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
15942 The \"Type\" column indicates one of:\n\
15943 \tbreakpoint - normal breakpoint\n\
15944 \twatchpoint - watchpoint\n\
15945 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15946 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15947 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15948 address and file/line number respectively.\n\
15949 \n\
15950 Convenience variable \"$_\" and default examine address for \"x\"\n\
15951 are set to the address of the last breakpoint listed unless the command\n\
15952 is prefixed with \"server \".\n\n\
15953 Convenience variable \"$bpnum\" contains the number of the last\n\
15954 breakpoint set."));
15955 }
15956
15957 add_info ("breakpoints", breakpoints_info, _("\
15958 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
15959 The \"Type\" column indicates one of:\n\
15960 \tbreakpoint - normal breakpoint\n\
15961 \twatchpoint - watchpoint\n\
15962 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15963 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15964 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15965 address and file/line number respectively.\n\
15966 \n\
15967 Convenience variable \"$_\" and default examine address for \"x\"\n\
15968 are set to the address of the last breakpoint listed unless the command\n\
15969 is prefixed with \"server \".\n\n\
15970 Convenience variable \"$bpnum\" contains the number of the last\n\
15971 breakpoint set."));
15972
15973 add_info_alias ("b", "breakpoints", 1);
15974
15975 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
15976 Status of all breakpoints, or breakpoint number NUMBER.\n\
15977 The \"Type\" column indicates one of:\n\
15978 \tbreakpoint - normal breakpoint\n\
15979 \twatchpoint - watchpoint\n\
15980 \tlongjmp - internal breakpoint used to step through longjmp()\n\
15981 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
15982 \tuntil - internal breakpoint used by the \"until\" command\n\
15983 \tfinish - internal breakpoint used by the \"finish\" command\n\
15984 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15985 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15986 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15987 address and file/line number respectively.\n\
15988 \n\
15989 Convenience variable \"$_\" and default examine address for \"x\"\n\
15990 are set to the address of the last breakpoint listed unless the command\n\
15991 is prefixed with \"server \".\n\n\
15992 Convenience variable \"$bpnum\" contains the number of the last\n\
15993 breakpoint set."),
15994 &maintenanceinfolist);
15995
15996 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
15997 Set catchpoints to catch events."),
15998 &catch_cmdlist, "catch ",
15999 0/*allow-unknown*/, &cmdlist);
16000
16001 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
16002 Set temporary catchpoints to catch events."),
16003 &tcatch_cmdlist, "tcatch ",
16004 0/*allow-unknown*/, &cmdlist);
16005
16006 add_catch_command ("fork", _("Catch calls to fork."),
16007 catch_fork_command_1,
16008 NULL,
16009 (void *) (uintptr_t) catch_fork_permanent,
16010 (void *) (uintptr_t) catch_fork_temporary);
16011 add_catch_command ("vfork", _("Catch calls to vfork."),
16012 catch_fork_command_1,
16013 NULL,
16014 (void *) (uintptr_t) catch_vfork_permanent,
16015 (void *) (uintptr_t) catch_vfork_temporary);
16016 add_catch_command ("exec", _("Catch calls to exec."),
16017 catch_exec_command_1,
16018 NULL,
16019 CATCH_PERMANENT,
16020 CATCH_TEMPORARY);
16021 add_catch_command ("load", _("Catch loads of shared libraries.\n\
16022 Usage: catch load [REGEX]\n\
16023 If REGEX is given, only stop for libraries matching the regular expression."),
16024 catch_load_command_1,
16025 NULL,
16026 CATCH_PERMANENT,
16027 CATCH_TEMPORARY);
16028 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
16029 Usage: catch unload [REGEX]\n\
16030 If REGEX is given, only stop for libraries matching the regular expression."),
16031 catch_unload_command_1,
16032 NULL,
16033 CATCH_PERMANENT,
16034 CATCH_TEMPORARY);
16035
16036 c = add_com ("watch", class_breakpoint, watch_command, _("\
16037 Set a watchpoint for an expression.\n\
16038 Usage: watch [-l|-location] EXPRESSION\n\
16039 A watchpoint stops execution of your program whenever the value of\n\
16040 an expression changes.\n\
16041 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16042 the memory to which it refers."));
16043 set_cmd_completer (c, expression_completer);
16044
16045 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
16046 Set a read watchpoint for an expression.\n\
16047 Usage: rwatch [-l|-location] EXPRESSION\n\
16048 A watchpoint stops execution of your program whenever the value of\n\
16049 an expression is read.\n\
16050 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16051 the memory to which it refers."));
16052 set_cmd_completer (c, expression_completer);
16053
16054 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
16055 Set a watchpoint for an expression.\n\
16056 Usage: awatch [-l|-location] EXPRESSION\n\
16057 A watchpoint stops execution of your program whenever the value of\n\
16058 an expression is either read or written.\n\
16059 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16060 the memory to which it refers."));
16061 set_cmd_completer (c, expression_completer);
16062
16063 add_info ("watchpoints", watchpoints_info, _("\
16064 Status of specified watchpoints (all watchpoints if no argument)."));
16065
16066 /* XXX: cagney/2005-02-23: This should be a boolean, and should
16067 respond to changes - contrary to the description. */
16068 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
16069 &can_use_hw_watchpoints, _("\
16070 Set debugger's willingness to use watchpoint hardware."), _("\
16071 Show debugger's willingness to use watchpoint hardware."), _("\
16072 If zero, gdb will not use hardware for new watchpoints, even if\n\
16073 such is available. (However, any hardware watchpoints that were\n\
16074 created before setting this to nonzero, will continue to use watchpoint\n\
16075 hardware.)"),
16076 NULL,
16077 show_can_use_hw_watchpoints,
16078 &setlist, &showlist);
16079
16080 can_use_hw_watchpoints = 1;
16081
16082 /* Tracepoint manipulation commands. */
16083
16084 c = add_com ("trace", class_breakpoint, trace_command, _("\
16085 Set a tracepoint at specified line or function.\n\
16086 \n"
16087 BREAK_ARGS_HELP ("trace") "\n\
16088 Do \"help tracepoints\" for info on other tracepoint commands."));
16089 set_cmd_completer (c, location_completer);
16090
16091 add_com_alias ("tp", "trace", class_alias, 0);
16092 add_com_alias ("tr", "trace", class_alias, 1);
16093 add_com_alias ("tra", "trace", class_alias, 1);
16094 add_com_alias ("trac", "trace", class_alias, 1);
16095
16096 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
16097 Set a fast tracepoint at specified line or function.\n\
16098 \n"
16099 BREAK_ARGS_HELP ("ftrace") "\n\
16100 Do \"help tracepoints\" for info on other tracepoint commands."));
16101 set_cmd_completer (c, location_completer);
16102
16103 c = add_com ("strace", class_breakpoint, strace_command, _("\
16104 Set a static tracepoint at specified line, function or marker.\n\
16105 \n\
16106 strace [LOCATION] [if CONDITION]\n\
16107 LOCATION may be a line number, function name, \"*\" and an address,\n\
16108 or -m MARKER_ID.\n\
16109 If a line number is specified, probe the marker at start of code\n\
16110 for that line. If a function is specified, probe the marker at start\n\
16111 of code for that function. If an address is specified, probe the marker\n\
16112 at that exact address. If a marker id is specified, probe the marker\n\
16113 with that name. With no LOCATION, uses current execution address of\n\
16114 the selected stack frame.\n\
16115 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16116 This collects arbitrary user data passed in the probe point call to the\n\
16117 tracing library. You can inspect it when analyzing the trace buffer,\n\
16118 by printing the $_sdata variable like any other convenience variable.\n\
16119 \n\
16120 CONDITION is a boolean expression.\n\
16121 \n\
16122 Multiple tracepoints at one place are permitted, and useful if their\n\
16123 conditions are different.\n\
16124 \n\
16125 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16126 Do \"help tracepoints\" for info on other tracepoint commands."));
16127 set_cmd_completer (c, location_completer);
16128
16129 add_info ("tracepoints", tracepoints_info, _("\
16130 Status of specified tracepoints (all tracepoints if no argument).\n\
16131 Convenience variable \"$tpnum\" contains the number of the\n\
16132 last tracepoint set."));
16133
16134 add_info_alias ("tp", "tracepoints", 1);
16135
16136 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16137 Delete specified tracepoints.\n\
16138 Arguments are tracepoint numbers, separated by spaces.\n\
16139 No argument means delete all tracepoints."),
16140 &deletelist);
16141 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
16142
16143 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16144 Disable specified tracepoints.\n\
16145 Arguments are tracepoint numbers, separated by spaces.\n\
16146 No argument means disable all tracepoints."),
16147 &disablelist);
16148 deprecate_cmd (c, "disable");
16149
16150 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16151 Enable specified tracepoints.\n\
16152 Arguments are tracepoint numbers, separated by spaces.\n\
16153 No argument means enable all tracepoints."),
16154 &enablelist);
16155 deprecate_cmd (c, "enable");
16156
16157 add_com ("passcount", class_trace, trace_pass_command, _("\
16158 Set the passcount for a tracepoint.\n\
16159 The trace will end when the tracepoint has been passed 'count' times.\n\
16160 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16161 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16162
16163 add_prefix_cmd ("save", class_breakpoint, save_command,
16164 _("Save breakpoint definitions as a script."),
16165 &save_cmdlist, "save ",
16166 0/*allow-unknown*/, &cmdlist);
16167
16168 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16169 Save current breakpoint definitions as a script.\n\
16170 This includes all types of breakpoints (breakpoints, watchpoints,\n\
16171 catchpoints, tracepoints). Use the 'source' command in another debug\n\
16172 session to restore them."),
16173 &save_cmdlist);
16174 set_cmd_completer (c, filename_completer);
16175
16176 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
16177 Save current tracepoint definitions as a script.\n\
16178 Use the 'source' command in another debug session to restore them."),
16179 &save_cmdlist);
16180 set_cmd_completer (c, filename_completer);
16181
16182 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16183 deprecate_cmd (c, "save tracepoints");
16184
16185 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
16186 Breakpoint specific settings\n\
16187 Configure various breakpoint-specific variables such as\n\
16188 pending breakpoint behavior"),
16189 &breakpoint_set_cmdlist, "set breakpoint ",
16190 0/*allow-unknown*/, &setlist);
16191 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
16192 Breakpoint specific settings\n\
16193 Configure various breakpoint-specific variables such as\n\
16194 pending breakpoint behavior"),
16195 &breakpoint_show_cmdlist, "show breakpoint ",
16196 0/*allow-unknown*/, &showlist);
16197
16198 add_setshow_auto_boolean_cmd ("pending", no_class,
16199 &pending_break_support, _("\
16200 Set debugger's behavior regarding pending breakpoints."), _("\
16201 Show debugger's behavior regarding pending breakpoints."), _("\
16202 If on, an unrecognized breakpoint location will cause gdb to create a\n\
16203 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16204 an error. If auto, an unrecognized breakpoint location results in a\n\
16205 user-query to see if a pending breakpoint should be created."),
16206 NULL,
16207 show_pending_break_support,
16208 &breakpoint_set_cmdlist,
16209 &breakpoint_show_cmdlist);
16210
16211 pending_break_support = AUTO_BOOLEAN_AUTO;
16212
16213 add_setshow_boolean_cmd ("auto-hw", no_class,
16214 &automatic_hardware_breakpoints, _("\
16215 Set automatic usage of hardware breakpoints."), _("\
16216 Show automatic usage of hardware breakpoints."), _("\
16217 If set, the debugger will automatically use hardware breakpoints for\n\
16218 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16219 a warning will be emitted for such breakpoints."),
16220 NULL,
16221 show_automatic_hardware_breakpoints,
16222 &breakpoint_set_cmdlist,
16223 &breakpoint_show_cmdlist);
16224
16225 add_setshow_boolean_cmd ("always-inserted", class_support,
16226 &always_inserted_mode, _("\
16227 Set mode for inserting breakpoints."), _("\
16228 Show mode for inserting breakpoints."), _("\
16229 When this mode is on, breakpoints are inserted immediately as soon as\n\
16230 they're created, kept inserted even when execution stops, and removed\n\
16231 only when the user deletes them. When this mode is off (the default),\n\
16232 breakpoints are inserted only when execution continues, and removed\n\
16233 when execution stops."),
16234 NULL,
16235 &show_always_inserted_mode,
16236 &breakpoint_set_cmdlist,
16237 &breakpoint_show_cmdlist);
16238
16239 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16240 condition_evaluation_enums,
16241 &condition_evaluation_mode_1, _("\
16242 Set mode of breakpoint condition evaluation."), _("\
16243 Show mode of breakpoint condition evaluation."), _("\
16244 When this is set to \"host\", breakpoint conditions will be\n\
16245 evaluated on the host's side by GDB. When it is set to \"target\",\n\
16246 breakpoint conditions will be downloaded to the target (if the target\n\
16247 supports such feature) and conditions will be evaluated on the target's side.\n\
16248 If this is set to \"auto\" (default), this will be automatically set to\n\
16249 \"target\" if it supports condition evaluation, otherwise it will\n\
16250 be set to \"gdb\""),
16251 &set_condition_evaluation_mode,
16252 &show_condition_evaluation_mode,
16253 &breakpoint_set_cmdlist,
16254 &breakpoint_show_cmdlist);
16255
16256 add_com ("break-range", class_breakpoint, break_range_command, _("\
16257 Set a breakpoint for an address range.\n\
16258 break-range START-LOCATION, END-LOCATION\n\
16259 where START-LOCATION and END-LOCATION can be one of the following:\n\
16260 LINENUM, for that line in the current file,\n\
16261 FILE:LINENUM, for that line in that file,\n\
16262 +OFFSET, for that number of lines after the current line\n\
16263 or the start of the range\n\
16264 FUNCTION, for the first line in that function,\n\
16265 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16266 *ADDRESS, for the instruction at that address.\n\
16267 \n\
16268 The breakpoint will stop execution of the inferior whenever it executes\n\
16269 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16270 range (including START-LOCATION and END-LOCATION)."));
16271
16272 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
16273 Set a dynamic printf at specified line or function.\n\
16274 dprintf location,format string,arg1,arg2,...\n\
16275 location may be a line number, function name, or \"*\" and an address.\n\
16276 If a line number is specified, break at start of code for that line.\n\
16277 If a function is specified, break at start of code for that function."));
16278 set_cmd_completer (c, location_completer);
16279
16280 add_setshow_enum_cmd ("dprintf-style", class_support,
16281 dprintf_style_enums, &dprintf_style, _("\
16282 Set the style of usage for dynamic printf."), _("\
16283 Show the style of usage for dynamic printf."), _("\
16284 This setting chooses how GDB will do a dynamic printf.\n\
16285 If the value is \"gdb\", then the printing is done by GDB to its own\n\
16286 console, as with the \"printf\" command.\n\
16287 If the value is \"call\", the print is done by calling a function in your\n\
16288 program; by default printf(), but you can choose a different function or\n\
16289 output stream by setting dprintf-function and dprintf-channel."),
16290 update_dprintf_commands, NULL,
16291 &setlist, &showlist);
16292
16293 dprintf_function = xstrdup ("printf");
16294 add_setshow_string_cmd ("dprintf-function", class_support,
16295 &dprintf_function, _("\
16296 Set the function to use for dynamic printf"), _("\
16297 Show the function to use for dynamic printf"), NULL,
16298 update_dprintf_commands, NULL,
16299 &setlist, &showlist);
16300
16301 dprintf_channel = xstrdup ("");
16302 add_setshow_string_cmd ("dprintf-channel", class_support,
16303 &dprintf_channel, _("\
16304 Set the channel to use for dynamic printf"), _("\
16305 Show the channel to use for dynamic printf"), NULL,
16306 update_dprintf_commands, NULL,
16307 &setlist, &showlist);
16308
16309 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16310 &disconnected_dprintf, _("\
16311 Set whether dprintf continues after GDB disconnects."), _("\
16312 Show whether dprintf continues after GDB disconnects."), _("\
16313 Use this to let dprintf commands continue to hit and produce output\n\
16314 even if GDB disconnects or detaches from the target."),
16315 NULL,
16316 NULL,
16317 &setlist, &showlist);
16318
16319 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16320 agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16321 (target agent only) This is useful for formatted output in user-defined commands."));
16322
16323 automatic_hardware_breakpoints = 1;
16324
16325 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
16326 observer_attach_thread_exit (remove_threaded_breakpoints);
16327 }
This page took 0.396097 seconds and 5 git commands to generate.