101f23092cae90366a3cc6f681e8d9ff2726e592
[deliverable/binutils-gdb.git] / gdb / breakpoint.c
1 /* Everything about breakpoints, for GDB.
2
3 Copyright (C) 1986-2016 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 #include "location.h"
71 #include "thread-fsm.h"
72 #include "tid-parse.h"
73
74 /* readline include files */
75 #include "readline/readline.h"
76 #include "readline/history.h"
77
78 /* readline defines this. */
79 #undef savestring
80
81 #include "mi/mi-common.h"
82 #include "extension.h"
83 #include <algorithm>
84
85 /* Enums for exception-handling support. */
86 enum exception_event_kind
87 {
88 EX_EVENT_THROW,
89 EX_EVENT_RETHROW,
90 EX_EVENT_CATCH
91 };
92
93 /* Prototypes for local functions. */
94
95 static void enable_delete_command (char *, int);
96
97 static void enable_once_command (char *, int);
98
99 static void enable_count_command (char *, int);
100
101 static void disable_command (char *, int);
102
103 static void enable_command (char *, int);
104
105 static void map_breakpoint_numbers (const char *,
106 void (*) (struct breakpoint *,
107 void *),
108 void *);
109
110 static void ignore_command (char *, int);
111
112 static int breakpoint_re_set_one (void *);
113
114 static void breakpoint_re_set_default (struct breakpoint *);
115
116 static void
117 create_sals_from_location_default (const struct event_location *location,
118 struct linespec_result *canonical,
119 enum bptype type_wanted);
120
121 static void create_breakpoints_sal_default (struct gdbarch *,
122 struct linespec_result *,
123 char *, char *, enum bptype,
124 enum bpdisp, int, int,
125 int,
126 const struct breakpoint_ops *,
127 int, int, int, unsigned);
128
129 static void decode_location_default (struct breakpoint *b,
130 const struct event_location *location,
131 struct program_space *search_pspace,
132 struct symtabs_and_lines *sals);
133
134 static void clear_command (char *, int);
135
136 static void catch_command (char *, int);
137
138 static int can_use_hardware_watchpoint (struct value *);
139
140 static void break_command_1 (char *, int, int);
141
142 static void mention (struct breakpoint *);
143
144 static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
145 enum bptype,
146 const struct breakpoint_ops *);
147 static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
148 const struct symtab_and_line *);
149
150 /* This function is used in gdbtk sources and thus can not be made
151 static. */
152 struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
153 struct symtab_and_line,
154 enum bptype,
155 const struct breakpoint_ops *);
156
157 static struct breakpoint *
158 momentary_breakpoint_from_master (struct breakpoint *orig,
159 enum bptype type,
160 const struct breakpoint_ops *ops,
161 int loc_enabled);
162
163 static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
164
165 static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
166 CORE_ADDR bpaddr,
167 enum bptype bptype);
168
169 static void describe_other_breakpoints (struct gdbarch *,
170 struct program_space *, CORE_ADDR,
171 struct obj_section *, int);
172
173 static int watchpoint_locations_match (struct bp_location *loc1,
174 struct bp_location *loc2);
175
176 static int breakpoint_location_address_match (struct bp_location *bl,
177 struct address_space *aspace,
178 CORE_ADDR addr);
179
180 static int breakpoint_location_address_range_overlap (struct bp_location *,
181 struct address_space *,
182 CORE_ADDR, int);
183
184 static void breakpoints_info (char *, int);
185
186 static void watchpoints_info (char *, int);
187
188 static int breakpoint_1 (char *, int,
189 int (*) (const struct breakpoint *));
190
191 static int breakpoint_cond_eval (void *);
192
193 static void cleanup_executing_breakpoints (void *);
194
195 static void commands_command (char *, int);
196
197 static void condition_command (char *, int);
198
199 static int remove_breakpoint (struct bp_location *);
200 static int remove_breakpoint_1 (struct bp_location *, enum remove_bp_reason);
201
202 static enum print_stop_action print_bp_stop_message (bpstat bs);
203
204 static int watchpoint_check (void *);
205
206 static void maintenance_info_breakpoints (char *, int);
207
208 static int hw_breakpoint_used_count (void);
209
210 static int hw_watchpoint_use_count (struct breakpoint *);
211
212 static int hw_watchpoint_used_count_others (struct breakpoint *except,
213 enum bptype type,
214 int *other_type_used);
215
216 static void hbreak_command (char *, int);
217
218 static void thbreak_command (char *, int);
219
220 static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
221 int count);
222
223 static void stop_command (char *arg, int from_tty);
224
225 static void stopin_command (char *arg, int from_tty);
226
227 static void stopat_command (char *arg, int from_tty);
228
229 static void tcatch_command (char *arg, int from_tty);
230
231 static void free_bp_location (struct bp_location *loc);
232 static void incref_bp_location (struct bp_location *loc);
233 static void decref_bp_location (struct bp_location **loc);
234
235 static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
236
237 /* update_global_location_list's modes of operation wrt to whether to
238 insert locations now. */
239 enum ugll_insert_mode
240 {
241 /* Don't insert any breakpoint locations into the inferior, only
242 remove already-inserted locations that no longer should be
243 inserted. Functions that delete a breakpoint or breakpoints
244 should specify this mode, so that deleting a breakpoint doesn't
245 have the side effect of inserting the locations of other
246 breakpoints that are marked not-inserted, but should_be_inserted
247 returns true on them.
248
249 This behavior is useful is situations close to tear-down -- e.g.,
250 after an exec, while the target still has execution, but
251 breakpoint shadows of the previous executable image should *NOT*
252 be restored to the new image; or before detaching, where the
253 target still has execution and wants to delete breakpoints from
254 GDB's lists, and all breakpoints had already been removed from
255 the inferior. */
256 UGLL_DONT_INSERT,
257
258 /* May insert breakpoints iff breakpoints_should_be_inserted_now
259 claims breakpoints should be inserted now. */
260 UGLL_MAY_INSERT,
261
262 /* Insert locations now, irrespective of
263 breakpoints_should_be_inserted_now. E.g., say all threads are
264 stopped right now, and the user did "continue". We need to
265 insert breakpoints _before_ resuming the target, but
266 UGLL_MAY_INSERT wouldn't insert them, because
267 breakpoints_should_be_inserted_now returns false at that point,
268 as no thread is running yet. */
269 UGLL_INSERT
270 };
271
272 static void update_global_location_list (enum ugll_insert_mode);
273
274 static void update_global_location_list_nothrow (enum ugll_insert_mode);
275
276 static int is_hardware_watchpoint (const struct breakpoint *bpt);
277
278 static void insert_breakpoint_locations (void);
279
280 static void tracepoints_info (char *, int);
281
282 static void delete_trace_command (char *, int);
283
284 static void enable_trace_command (char *, int);
285
286 static void disable_trace_command (char *, int);
287
288 static void trace_pass_command (char *, int);
289
290 static void set_tracepoint_count (int num);
291
292 static int is_masked_watchpoint (const struct breakpoint *b);
293
294 static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
295
296 /* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
297 otherwise. */
298
299 static int strace_marker_p (struct breakpoint *b);
300
301 /* The breakpoint_ops structure to be inherited by all breakpoint_ops
302 that are implemented on top of software or hardware breakpoints
303 (user breakpoints, internal and momentary breakpoints, etc.). */
304 static struct breakpoint_ops bkpt_base_breakpoint_ops;
305
306 /* Internal breakpoints class type. */
307 static struct breakpoint_ops internal_breakpoint_ops;
308
309 /* Momentary breakpoints class type. */
310 static struct breakpoint_ops momentary_breakpoint_ops;
311
312 /* Momentary breakpoints for bp_longjmp and bp_exception class type. */
313 static struct breakpoint_ops longjmp_breakpoint_ops;
314
315 /* The breakpoint_ops structure to be used in regular user created
316 breakpoints. */
317 struct breakpoint_ops bkpt_breakpoint_ops;
318
319 /* Breakpoints set on probes. */
320 static struct breakpoint_ops bkpt_probe_breakpoint_ops;
321
322 /* Dynamic printf class type. */
323 struct breakpoint_ops dprintf_breakpoint_ops;
324
325 /* The style in which to perform a dynamic printf. This is a user
326 option because different output options have different tradeoffs;
327 if GDB does the printing, there is better error handling if there
328 is a problem with any of the arguments, but using an inferior
329 function lets you have special-purpose printers and sending of
330 output to the same place as compiled-in print functions. */
331
332 static const char dprintf_style_gdb[] = "gdb";
333 static const char dprintf_style_call[] = "call";
334 static const char dprintf_style_agent[] = "agent";
335 static const char *const dprintf_style_enums[] = {
336 dprintf_style_gdb,
337 dprintf_style_call,
338 dprintf_style_agent,
339 NULL
340 };
341 static const char *dprintf_style = dprintf_style_gdb;
342
343 /* The function to use for dynamic printf if the preferred style is to
344 call into the inferior. The value is simply a string that is
345 copied into the command, so it can be anything that GDB can
346 evaluate to a callable address, not necessarily a function name. */
347
348 static char *dprintf_function = "";
349
350 /* The channel to use for dynamic printf if the preferred style is to
351 call into the inferior; if a nonempty string, it will be passed to
352 the call as the first argument, with the format string as the
353 second. As with the dprintf function, this can be anything that
354 GDB knows how to evaluate, so in addition to common choices like
355 "stderr", this could be an app-specific expression like
356 "mystreams[curlogger]". */
357
358 static char *dprintf_channel = "";
359
360 /* True if dprintf commands should continue to operate even if GDB
361 has disconnected. */
362 static int disconnected_dprintf = 1;
363
364 /* A reference-counted struct command_line. This lets multiple
365 breakpoints share a single command list. */
366 struct counted_command_line
367 {
368 /* The reference count. */
369 int refc;
370
371 /* The command list. */
372 struct command_line *commands;
373 };
374
375 struct command_line *
376 breakpoint_commands (struct breakpoint *b)
377 {
378 return b->commands ? b->commands->commands : NULL;
379 }
380
381 /* Flag indicating that a command has proceeded the inferior past the
382 current breakpoint. */
383
384 static int breakpoint_proceeded;
385
386 const char *
387 bpdisp_text (enum bpdisp disp)
388 {
389 /* NOTE: the following values are a part of MI protocol and
390 represent values of 'disp' field returned when inferior stops at
391 a breakpoint. */
392 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
393
394 return bpdisps[(int) disp];
395 }
396
397 /* Prototypes for exported functions. */
398 /* If FALSE, gdb will not use hardware support for watchpoints, even
399 if such is available. */
400 static int can_use_hw_watchpoints;
401
402 static void
403 show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
404 struct cmd_list_element *c,
405 const char *value)
406 {
407 fprintf_filtered (file,
408 _("Debugger's willingness to use "
409 "watchpoint hardware is %s.\n"),
410 value);
411 }
412
413 /* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
414 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
415 for unrecognized breakpoint locations.
416 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
417 static enum auto_boolean pending_break_support;
418 static void
419 show_pending_break_support (struct ui_file *file, int from_tty,
420 struct cmd_list_element *c,
421 const char *value)
422 {
423 fprintf_filtered (file,
424 _("Debugger's behavior regarding "
425 "pending breakpoints is %s.\n"),
426 value);
427 }
428
429 /* If 1, gdb will automatically use hardware breakpoints for breakpoints
430 set with "break" but falling in read-only memory.
431 If 0, gdb will warn about such breakpoints, but won't automatically
432 use hardware breakpoints. */
433 static int automatic_hardware_breakpoints;
434 static void
435 show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
436 struct cmd_list_element *c,
437 const char *value)
438 {
439 fprintf_filtered (file,
440 _("Automatic usage of hardware breakpoints is %s.\n"),
441 value);
442 }
443
444 /* If on, GDB keeps breakpoints inserted even if the inferior is
445 stopped, and immediately inserts any new breakpoints as soon as
446 they're created. If off (default), GDB keeps breakpoints off of
447 the target as long as possible. That is, it delays inserting
448 breakpoints until the next resume, and removes them again when the
449 target fully stops. This is a bit safer in case GDB crashes while
450 processing user input. */
451 static int always_inserted_mode = 0;
452
453 static void
454 show_always_inserted_mode (struct ui_file *file, int from_tty,
455 struct cmd_list_element *c, const char *value)
456 {
457 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
458 value);
459 }
460
461 /* See breakpoint.h. */
462
463 int
464 breakpoints_should_be_inserted_now (void)
465 {
466 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
467 {
468 /* If breakpoints are global, they should be inserted even if no
469 thread under gdb's control is running, or even if there are
470 no threads under GDB's control yet. */
471 return 1;
472 }
473 else if (target_has_execution)
474 {
475 struct thread_info *tp;
476
477 if (always_inserted_mode)
478 {
479 /* The user wants breakpoints inserted even if all threads
480 are stopped. */
481 return 1;
482 }
483
484 if (threads_are_executing ())
485 return 1;
486
487 /* Don't remove breakpoints yet if, even though all threads are
488 stopped, we still have events to process. */
489 ALL_NON_EXITED_THREADS (tp)
490 if (tp->resumed
491 && tp->suspend.waitstatus_pending_p)
492 return 1;
493 }
494 return 0;
495 }
496
497 static const char condition_evaluation_both[] = "host or target";
498
499 /* Modes for breakpoint condition evaluation. */
500 static const char condition_evaluation_auto[] = "auto";
501 static const char condition_evaluation_host[] = "host";
502 static const char condition_evaluation_target[] = "target";
503 static const char *const condition_evaluation_enums[] = {
504 condition_evaluation_auto,
505 condition_evaluation_host,
506 condition_evaluation_target,
507 NULL
508 };
509
510 /* Global that holds the current mode for breakpoint condition evaluation. */
511 static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
512
513 /* Global that we use to display information to the user (gets its value from
514 condition_evaluation_mode_1. */
515 static const char *condition_evaluation_mode = condition_evaluation_auto;
516
517 /* Translate a condition evaluation mode MODE into either "host"
518 or "target". This is used mostly to translate from "auto" to the
519 real setting that is being used. It returns the translated
520 evaluation mode. */
521
522 static const char *
523 translate_condition_evaluation_mode (const char *mode)
524 {
525 if (mode == condition_evaluation_auto)
526 {
527 if (target_supports_evaluation_of_breakpoint_conditions ())
528 return condition_evaluation_target;
529 else
530 return condition_evaluation_host;
531 }
532 else
533 return mode;
534 }
535
536 /* Discovers what condition_evaluation_auto translates to. */
537
538 static const char *
539 breakpoint_condition_evaluation_mode (void)
540 {
541 return translate_condition_evaluation_mode (condition_evaluation_mode);
542 }
543
544 /* Return true if GDB should evaluate breakpoint conditions or false
545 otherwise. */
546
547 static int
548 gdb_evaluates_breakpoint_condition_p (void)
549 {
550 const char *mode = breakpoint_condition_evaluation_mode ();
551
552 return (mode == condition_evaluation_host);
553 }
554
555 void _initialize_breakpoint (void);
556
557 /* Are we executing breakpoint commands? */
558 static int executing_breakpoint_commands;
559
560 /* Are overlay event breakpoints enabled? */
561 static int overlay_events_enabled;
562
563 /* See description in breakpoint.h. */
564 int target_exact_watchpoints = 0;
565
566 /* Walk the following statement or block through all breakpoints.
567 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
568 current breakpoint. */
569
570 #define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
571
572 #define ALL_BREAKPOINTS_SAFE(B,TMP) \
573 for (B = breakpoint_chain; \
574 B ? (TMP=B->next, 1): 0; \
575 B = TMP)
576
577 /* Similar iterator for the low-level breakpoints. SAFE variant is
578 not provided so update_global_location_list must not be called
579 while executing the block of ALL_BP_LOCATIONS. */
580
581 #define ALL_BP_LOCATIONS(B,BP_TMP) \
582 for (BP_TMP = bp_location; \
583 BP_TMP < bp_location + bp_location_count && (B = *BP_TMP); \
584 BP_TMP++)
585
586 /* Iterates through locations with address ADDRESS for the currently selected
587 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
588 to where the loop should start from.
589 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
590 appropriate location to start with. */
591
592 #define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
593 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
594 BP_LOCP_TMP = BP_LOCP_START; \
595 BP_LOCP_START \
596 && (BP_LOCP_TMP < bp_location + bp_location_count \
597 && (*BP_LOCP_TMP)->address == ADDRESS); \
598 BP_LOCP_TMP++)
599
600 /* Iterator for tracepoints only. */
601
602 #define ALL_TRACEPOINTS(B) \
603 for (B = breakpoint_chain; B; B = B->next) \
604 if (is_tracepoint (B))
605
606 /* Chains of all breakpoints defined. */
607
608 struct breakpoint *breakpoint_chain;
609
610 /* Array is sorted by bp_location_compare - primarily by the ADDRESS. */
611
612 static struct bp_location **bp_location;
613
614 /* Number of elements of BP_LOCATION. */
615
616 static unsigned bp_location_count;
617
618 /* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
619 ADDRESS for the current elements of BP_LOCATION which get a valid
620 result from bp_location_has_shadow. You can use it for roughly
621 limiting the subrange of BP_LOCATION to scan for shadow bytes for
622 an address you need to read. */
623
624 static CORE_ADDR bp_location_placed_address_before_address_max;
625
626 /* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
627 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
628 BP_LOCATION which get a valid result from bp_location_has_shadow.
629 You can use it for roughly limiting the subrange of BP_LOCATION to
630 scan for shadow bytes for an address you need to read. */
631
632 static CORE_ADDR bp_location_shadow_len_after_address_max;
633
634 /* The locations that no longer correspond to any breakpoint, unlinked
635 from bp_location array, but for which a hit may still be reported
636 by a target. */
637 VEC(bp_location_p) *moribund_locations = NULL;
638
639 /* Number of last breakpoint made. */
640
641 static int breakpoint_count;
642
643 /* The value of `breakpoint_count' before the last command that
644 created breakpoints. If the last (break-like) command created more
645 than one breakpoint, then the difference between BREAKPOINT_COUNT
646 and PREV_BREAKPOINT_COUNT is more than one. */
647 static int prev_breakpoint_count;
648
649 /* Number of last tracepoint made. */
650
651 static int tracepoint_count;
652
653 static struct cmd_list_element *breakpoint_set_cmdlist;
654 static struct cmd_list_element *breakpoint_show_cmdlist;
655 struct cmd_list_element *save_cmdlist;
656
657 /* See declaration at breakpoint.h. */
658
659 struct breakpoint *
660 breakpoint_find_if (int (*func) (struct breakpoint *b, void *d),
661 void *user_data)
662 {
663 struct breakpoint *b = NULL;
664
665 ALL_BREAKPOINTS (b)
666 {
667 if (func (b, user_data) != 0)
668 break;
669 }
670
671 return b;
672 }
673
674 /* Return whether a breakpoint is an active enabled breakpoint. */
675 static int
676 breakpoint_enabled (struct breakpoint *b)
677 {
678 return (b->enable_state == bp_enabled);
679 }
680
681 /* Set breakpoint count to NUM. */
682
683 static void
684 set_breakpoint_count (int num)
685 {
686 prev_breakpoint_count = breakpoint_count;
687 breakpoint_count = num;
688 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
689 }
690
691 /* Used by `start_rbreak_breakpoints' below, to record the current
692 breakpoint count before "rbreak" creates any breakpoint. */
693 static int rbreak_start_breakpoint_count;
694
695 /* Called at the start an "rbreak" command to record the first
696 breakpoint made. */
697
698 void
699 start_rbreak_breakpoints (void)
700 {
701 rbreak_start_breakpoint_count = breakpoint_count;
702 }
703
704 /* Called at the end of an "rbreak" command to record the last
705 breakpoint made. */
706
707 void
708 end_rbreak_breakpoints (void)
709 {
710 prev_breakpoint_count = rbreak_start_breakpoint_count;
711 }
712
713 /* Used in run_command to zero the hit count when a new run starts. */
714
715 void
716 clear_breakpoint_hit_counts (void)
717 {
718 struct breakpoint *b;
719
720 ALL_BREAKPOINTS (b)
721 b->hit_count = 0;
722 }
723
724 /* Allocate a new counted_command_line with reference count of 1.
725 The new structure owns COMMANDS. */
726
727 static struct counted_command_line *
728 alloc_counted_command_line (struct command_line *commands)
729 {
730 struct counted_command_line *result = XNEW (struct counted_command_line);
731
732 result->refc = 1;
733 result->commands = commands;
734
735 return result;
736 }
737
738 /* Increment reference count. This does nothing if CMD is NULL. */
739
740 static void
741 incref_counted_command_line (struct counted_command_line *cmd)
742 {
743 if (cmd)
744 ++cmd->refc;
745 }
746
747 /* Decrement reference count. If the reference count reaches 0,
748 destroy the counted_command_line. Sets *CMDP to NULL. This does
749 nothing if *CMDP is NULL. */
750
751 static void
752 decref_counted_command_line (struct counted_command_line **cmdp)
753 {
754 if (*cmdp)
755 {
756 if (--(*cmdp)->refc == 0)
757 {
758 free_command_lines (&(*cmdp)->commands);
759 xfree (*cmdp);
760 }
761 *cmdp = NULL;
762 }
763 }
764
765 /* A cleanup function that calls decref_counted_command_line. */
766
767 static void
768 do_cleanup_counted_command_line (void *arg)
769 {
770 decref_counted_command_line ((struct counted_command_line **) arg);
771 }
772
773 /* Create a cleanup that calls decref_counted_command_line on the
774 argument. */
775
776 static struct cleanup *
777 make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
778 {
779 return make_cleanup (do_cleanup_counted_command_line, cmdp);
780 }
781
782 \f
783 /* Return the breakpoint with the specified number, or NULL
784 if the number does not refer to an existing breakpoint. */
785
786 struct breakpoint *
787 get_breakpoint (int num)
788 {
789 struct breakpoint *b;
790
791 ALL_BREAKPOINTS (b)
792 if (b->number == num)
793 return b;
794
795 return NULL;
796 }
797
798 \f
799
800 /* Mark locations as "conditions have changed" in case the target supports
801 evaluating conditions on its side. */
802
803 static void
804 mark_breakpoint_modified (struct breakpoint *b)
805 {
806 struct bp_location *loc;
807
808 /* This is only meaningful if the target is
809 evaluating conditions and if the user has
810 opted for condition evaluation on the target's
811 side. */
812 if (gdb_evaluates_breakpoint_condition_p ()
813 || !target_supports_evaluation_of_breakpoint_conditions ())
814 return;
815
816 if (!is_breakpoint (b))
817 return;
818
819 for (loc = b->loc; loc; loc = loc->next)
820 loc->condition_changed = condition_modified;
821 }
822
823 /* Mark location as "conditions have changed" in case the target supports
824 evaluating conditions on its side. */
825
826 static void
827 mark_breakpoint_location_modified (struct bp_location *loc)
828 {
829 /* This is only meaningful if the target is
830 evaluating conditions and if the user has
831 opted for condition evaluation on the target's
832 side. */
833 if (gdb_evaluates_breakpoint_condition_p ()
834 || !target_supports_evaluation_of_breakpoint_conditions ())
835
836 return;
837
838 if (!is_breakpoint (loc->owner))
839 return;
840
841 loc->condition_changed = condition_modified;
842 }
843
844 /* Sets the condition-evaluation mode using the static global
845 condition_evaluation_mode. */
846
847 static void
848 set_condition_evaluation_mode (char *args, int from_tty,
849 struct cmd_list_element *c)
850 {
851 const char *old_mode, *new_mode;
852
853 if ((condition_evaluation_mode_1 == condition_evaluation_target)
854 && !target_supports_evaluation_of_breakpoint_conditions ())
855 {
856 condition_evaluation_mode_1 = condition_evaluation_mode;
857 warning (_("Target does not support breakpoint condition evaluation.\n"
858 "Using host evaluation mode instead."));
859 return;
860 }
861
862 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
863 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
864
865 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
866 settings was "auto". */
867 condition_evaluation_mode = condition_evaluation_mode_1;
868
869 /* Only update the mode if the user picked a different one. */
870 if (new_mode != old_mode)
871 {
872 struct bp_location *loc, **loc_tmp;
873 /* If the user switched to a different evaluation mode, we
874 need to synch the changes with the target as follows:
875
876 "host" -> "target": Send all (valid) conditions to the target.
877 "target" -> "host": Remove all the conditions from the target.
878 */
879
880 if (new_mode == condition_evaluation_target)
881 {
882 /* Mark everything modified and synch conditions with the
883 target. */
884 ALL_BP_LOCATIONS (loc, loc_tmp)
885 mark_breakpoint_location_modified (loc);
886 }
887 else
888 {
889 /* Manually mark non-duplicate locations to synch conditions
890 with the target. We do this to remove all the conditions the
891 target knows about. */
892 ALL_BP_LOCATIONS (loc, loc_tmp)
893 if (is_breakpoint (loc->owner) && loc->inserted)
894 loc->needs_update = 1;
895 }
896
897 /* Do the update. */
898 update_global_location_list (UGLL_MAY_INSERT);
899 }
900
901 return;
902 }
903
904 /* Shows the current mode of breakpoint condition evaluation. Explicitly shows
905 what "auto" is translating to. */
906
907 static void
908 show_condition_evaluation_mode (struct ui_file *file, int from_tty,
909 struct cmd_list_element *c, const char *value)
910 {
911 if (condition_evaluation_mode == condition_evaluation_auto)
912 fprintf_filtered (file,
913 _("Breakpoint condition evaluation "
914 "mode is %s (currently %s).\n"),
915 value,
916 breakpoint_condition_evaluation_mode ());
917 else
918 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
919 value);
920 }
921
922 /* A comparison function for bp_location AP and BP that is used by
923 bsearch. This comparison function only cares about addresses, unlike
924 the more general bp_location_compare function. */
925
926 static int
927 bp_location_compare_addrs (const void *ap, const void *bp)
928 {
929 const struct bp_location *a = *(const struct bp_location **) ap;
930 const struct bp_location *b = *(const struct bp_location **) bp;
931
932 if (a->address == b->address)
933 return 0;
934 else
935 return ((a->address > b->address) - (a->address < b->address));
936 }
937
938 /* Helper function to skip all bp_locations with addresses
939 less than ADDRESS. It returns the first bp_location that
940 is greater than or equal to ADDRESS. If none is found, just
941 return NULL. */
942
943 static struct bp_location **
944 get_first_locp_gte_addr (CORE_ADDR address)
945 {
946 struct bp_location dummy_loc;
947 struct bp_location *dummy_locp = &dummy_loc;
948 struct bp_location **locp_found = NULL;
949
950 /* Initialize the dummy location's address field. */
951 memset (&dummy_loc, 0, sizeof (struct bp_location));
952 dummy_loc.address = address;
953
954 /* Find a close match to the first location at ADDRESS. */
955 locp_found = ((struct bp_location **)
956 bsearch (&dummy_locp, bp_location, bp_location_count,
957 sizeof (struct bp_location **),
958 bp_location_compare_addrs));
959
960 /* Nothing was found, nothing left to do. */
961 if (locp_found == NULL)
962 return NULL;
963
964 /* We may have found a location that is at ADDRESS but is not the first in the
965 location's list. Go backwards (if possible) and locate the first one. */
966 while ((locp_found - 1) >= bp_location
967 && (*(locp_found - 1))->address == address)
968 locp_found--;
969
970 return locp_found;
971 }
972
973 void
974 set_breakpoint_condition (struct breakpoint *b, const char *exp,
975 int from_tty)
976 {
977 xfree (b->cond_string);
978 b->cond_string = NULL;
979
980 if (is_watchpoint (b))
981 {
982 struct watchpoint *w = (struct watchpoint *) b;
983
984 w->cond_exp.reset ();
985 }
986 else
987 {
988 struct bp_location *loc;
989
990 for (loc = b->loc; loc; loc = loc->next)
991 {
992 loc->cond.reset ();
993
994 /* No need to free the condition agent expression
995 bytecode (if we have one). We will handle this
996 when we go through update_global_location_list. */
997 }
998 }
999
1000 if (*exp == 0)
1001 {
1002 if (from_tty)
1003 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
1004 }
1005 else
1006 {
1007 const char *arg = exp;
1008
1009 /* I don't know if it matters whether this is the string the user
1010 typed in or the decompiled expression. */
1011 b->cond_string = xstrdup (arg);
1012 b->condition_not_parsed = 0;
1013
1014 if (is_watchpoint (b))
1015 {
1016 struct watchpoint *w = (struct watchpoint *) b;
1017
1018 innermost_block = NULL;
1019 arg = exp;
1020 w->cond_exp = parse_exp_1 (&arg, 0, 0, 0);
1021 if (*arg)
1022 error (_("Junk at end of expression"));
1023 w->cond_exp_valid_block = innermost_block;
1024 }
1025 else
1026 {
1027 struct bp_location *loc;
1028
1029 for (loc = b->loc; loc; loc = loc->next)
1030 {
1031 arg = exp;
1032 loc->cond =
1033 parse_exp_1 (&arg, loc->address,
1034 block_for_pc (loc->address), 0);
1035 if (*arg)
1036 error (_("Junk at end of expression"));
1037 }
1038 }
1039 }
1040 mark_breakpoint_modified (b);
1041
1042 observer_notify_breakpoint_modified (b);
1043 }
1044
1045 /* Completion for the "condition" command. */
1046
1047 static VEC (char_ptr) *
1048 condition_completer (struct cmd_list_element *cmd,
1049 const char *text, const char *word)
1050 {
1051 const char *space;
1052
1053 text = skip_spaces_const (text);
1054 space = skip_to_space_const (text);
1055 if (*space == '\0')
1056 {
1057 int len;
1058 struct breakpoint *b;
1059 VEC (char_ptr) *result = NULL;
1060
1061 if (text[0] == '$')
1062 {
1063 /* We don't support completion of history indices. */
1064 if (isdigit (text[1]))
1065 return NULL;
1066 return complete_internalvar (&text[1]);
1067 }
1068
1069 /* We're completing the breakpoint number. */
1070 len = strlen (text);
1071
1072 ALL_BREAKPOINTS (b)
1073 {
1074 char number[50];
1075
1076 xsnprintf (number, sizeof (number), "%d", b->number);
1077
1078 if (strncmp (number, text, len) == 0)
1079 VEC_safe_push (char_ptr, result, xstrdup (number));
1080 }
1081
1082 return result;
1083 }
1084
1085 /* We're completing the expression part. */
1086 text = skip_spaces_const (space);
1087 return expression_completer (cmd, text, word);
1088 }
1089
1090 /* condition N EXP -- set break condition of breakpoint N to EXP. */
1091
1092 static void
1093 condition_command (char *arg, int from_tty)
1094 {
1095 struct breakpoint *b;
1096 char *p;
1097 int bnum;
1098
1099 if (arg == 0)
1100 error_no_arg (_("breakpoint number"));
1101
1102 p = arg;
1103 bnum = get_number (&p);
1104 if (bnum == 0)
1105 error (_("Bad breakpoint argument: '%s'"), arg);
1106
1107 ALL_BREAKPOINTS (b)
1108 if (b->number == bnum)
1109 {
1110 /* Check if this breakpoint has a "stop" method implemented in an
1111 extension language. This method and conditions entered into GDB
1112 from the CLI are mutually exclusive. */
1113 const struct extension_language_defn *extlang
1114 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1115
1116 if (extlang != NULL)
1117 {
1118 error (_("Only one stop condition allowed. There is currently"
1119 " a %s stop condition defined for this breakpoint."),
1120 ext_lang_capitalized_name (extlang));
1121 }
1122 set_breakpoint_condition (b, p, from_tty);
1123
1124 if (is_breakpoint (b))
1125 update_global_location_list (UGLL_MAY_INSERT);
1126
1127 return;
1128 }
1129
1130 error (_("No breakpoint number %d."), bnum);
1131 }
1132
1133 /* Check that COMMAND do not contain commands that are suitable
1134 only for tracepoints and not suitable for ordinary breakpoints.
1135 Throw if any such commands is found. */
1136
1137 static void
1138 check_no_tracepoint_commands (struct command_line *commands)
1139 {
1140 struct command_line *c;
1141
1142 for (c = commands; c; c = c->next)
1143 {
1144 int i;
1145
1146 if (c->control_type == while_stepping_control)
1147 error (_("The 'while-stepping' command can "
1148 "only be used for tracepoints"));
1149
1150 for (i = 0; i < c->body_count; ++i)
1151 check_no_tracepoint_commands ((c->body_list)[i]);
1152
1153 /* Not that command parsing removes leading whitespace and comment
1154 lines and also empty lines. So, we only need to check for
1155 command directly. */
1156 if (strstr (c->line, "collect ") == c->line)
1157 error (_("The 'collect' command can only be used for tracepoints"));
1158
1159 if (strstr (c->line, "teval ") == c->line)
1160 error (_("The 'teval' command can only be used for tracepoints"));
1161 }
1162 }
1163
1164 /* Encapsulate tests for different types of tracepoints. */
1165
1166 static int
1167 is_tracepoint_type (enum bptype type)
1168 {
1169 return (type == bp_tracepoint
1170 || type == bp_fast_tracepoint
1171 || type == bp_static_tracepoint);
1172 }
1173
1174 int
1175 is_tracepoint (const struct breakpoint *b)
1176 {
1177 return is_tracepoint_type (b->type);
1178 }
1179
1180 /* A helper function that validates that COMMANDS are valid for a
1181 breakpoint. This function will throw an exception if a problem is
1182 found. */
1183
1184 static void
1185 validate_commands_for_breakpoint (struct breakpoint *b,
1186 struct command_line *commands)
1187 {
1188 if (is_tracepoint (b))
1189 {
1190 struct tracepoint *t = (struct tracepoint *) b;
1191 struct command_line *c;
1192 struct command_line *while_stepping = 0;
1193
1194 /* Reset the while-stepping step count. The previous commands
1195 might have included a while-stepping action, while the new
1196 ones might not. */
1197 t->step_count = 0;
1198
1199 /* We need to verify that each top-level element of commands is
1200 valid for tracepoints, that there's at most one
1201 while-stepping element, and that the while-stepping's body
1202 has valid tracing commands excluding nested while-stepping.
1203 We also need to validate the tracepoint action line in the
1204 context of the tracepoint --- validate_actionline actually
1205 has side effects, like setting the tracepoint's
1206 while-stepping STEP_COUNT, in addition to checking if the
1207 collect/teval actions parse and make sense in the
1208 tracepoint's context. */
1209 for (c = commands; c; c = c->next)
1210 {
1211 if (c->control_type == while_stepping_control)
1212 {
1213 if (b->type == bp_fast_tracepoint)
1214 error (_("The 'while-stepping' command "
1215 "cannot be used for fast tracepoint"));
1216 else if (b->type == bp_static_tracepoint)
1217 error (_("The 'while-stepping' command "
1218 "cannot be used for static tracepoint"));
1219
1220 if (while_stepping)
1221 error (_("The 'while-stepping' command "
1222 "can be used only once"));
1223 else
1224 while_stepping = c;
1225 }
1226
1227 validate_actionline (c->line, b);
1228 }
1229 if (while_stepping)
1230 {
1231 struct command_line *c2;
1232
1233 gdb_assert (while_stepping->body_count == 1);
1234 c2 = while_stepping->body_list[0];
1235 for (; c2; c2 = c2->next)
1236 {
1237 if (c2->control_type == while_stepping_control)
1238 error (_("The 'while-stepping' command cannot be nested"));
1239 }
1240 }
1241 }
1242 else
1243 {
1244 check_no_tracepoint_commands (commands);
1245 }
1246 }
1247
1248 /* Return a vector of all the static tracepoints set at ADDR. The
1249 caller is responsible for releasing the vector. */
1250
1251 VEC(breakpoint_p) *
1252 static_tracepoints_here (CORE_ADDR addr)
1253 {
1254 struct breakpoint *b;
1255 VEC(breakpoint_p) *found = 0;
1256 struct bp_location *loc;
1257
1258 ALL_BREAKPOINTS (b)
1259 if (b->type == bp_static_tracepoint)
1260 {
1261 for (loc = b->loc; loc; loc = loc->next)
1262 if (loc->address == addr)
1263 VEC_safe_push(breakpoint_p, found, b);
1264 }
1265
1266 return found;
1267 }
1268
1269 /* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
1270 validate that only allowed commands are included. */
1271
1272 void
1273 breakpoint_set_commands (struct breakpoint *b,
1274 struct command_line *commands)
1275 {
1276 validate_commands_for_breakpoint (b, commands);
1277
1278 decref_counted_command_line (&b->commands);
1279 b->commands = alloc_counted_command_line (commands);
1280 observer_notify_breakpoint_modified (b);
1281 }
1282
1283 /* Set the internal `silent' flag on the breakpoint. Note that this
1284 is not the same as the "silent" that may appear in the breakpoint's
1285 commands. */
1286
1287 void
1288 breakpoint_set_silent (struct breakpoint *b, int silent)
1289 {
1290 int old_silent = b->silent;
1291
1292 b->silent = silent;
1293 if (old_silent != silent)
1294 observer_notify_breakpoint_modified (b);
1295 }
1296
1297 /* Set the thread for this breakpoint. If THREAD is -1, make the
1298 breakpoint work for any thread. */
1299
1300 void
1301 breakpoint_set_thread (struct breakpoint *b, int thread)
1302 {
1303 int old_thread = b->thread;
1304
1305 b->thread = thread;
1306 if (old_thread != thread)
1307 observer_notify_breakpoint_modified (b);
1308 }
1309
1310 /* Set the task for this breakpoint. If TASK is 0, make the
1311 breakpoint work for any task. */
1312
1313 void
1314 breakpoint_set_task (struct breakpoint *b, int task)
1315 {
1316 int old_task = b->task;
1317
1318 b->task = task;
1319 if (old_task != task)
1320 observer_notify_breakpoint_modified (b);
1321 }
1322
1323 void
1324 check_tracepoint_command (char *line, void *closure)
1325 {
1326 struct breakpoint *b = (struct breakpoint *) closure;
1327
1328 validate_actionline (line, b);
1329 }
1330
1331 /* A structure used to pass information through
1332 map_breakpoint_numbers. */
1333
1334 struct commands_info
1335 {
1336 /* True if the command was typed at a tty. */
1337 int from_tty;
1338
1339 /* The breakpoint range spec. */
1340 const char *arg;
1341
1342 /* Non-NULL if the body of the commands are being read from this
1343 already-parsed command. */
1344 struct command_line *control;
1345
1346 /* The command lines read from the user, or NULL if they have not
1347 yet been read. */
1348 struct counted_command_line *cmd;
1349 };
1350
1351 /* A callback for map_breakpoint_numbers that sets the commands for
1352 commands_command. */
1353
1354 static void
1355 do_map_commands_command (struct breakpoint *b, void *data)
1356 {
1357 struct commands_info *info = (struct commands_info *) data;
1358
1359 if (info->cmd == NULL)
1360 {
1361 struct command_line *l;
1362
1363 if (info->control != NULL)
1364 l = copy_command_lines (info->control->body_list[0]);
1365 else
1366 {
1367 struct cleanup *old_chain;
1368 char *str;
1369
1370 str = xstrprintf (_("Type commands for breakpoint(s) "
1371 "%s, one per line."),
1372 info->arg);
1373
1374 old_chain = make_cleanup (xfree, str);
1375
1376 l = read_command_lines (str,
1377 info->from_tty, 1,
1378 (is_tracepoint (b)
1379 ? check_tracepoint_command : 0),
1380 b);
1381
1382 do_cleanups (old_chain);
1383 }
1384
1385 info->cmd = alloc_counted_command_line (l);
1386 }
1387
1388 /* If a breakpoint was on the list more than once, we don't need to
1389 do anything. */
1390 if (b->commands != info->cmd)
1391 {
1392 validate_commands_for_breakpoint (b, info->cmd->commands);
1393 incref_counted_command_line (info->cmd);
1394 decref_counted_command_line (&b->commands);
1395 b->commands = info->cmd;
1396 observer_notify_breakpoint_modified (b);
1397 }
1398 }
1399
1400 static void
1401 commands_command_1 (const char *arg, int from_tty,
1402 struct command_line *control)
1403 {
1404 struct cleanup *cleanups;
1405 struct commands_info info;
1406
1407 info.from_tty = from_tty;
1408 info.control = control;
1409 info.cmd = NULL;
1410 /* If we read command lines from the user, then `info' will hold an
1411 extra reference to the commands that we must clean up. */
1412 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
1413
1414 std::string new_arg;
1415
1416 if (arg == NULL || !*arg)
1417 {
1418 if (breakpoint_count - prev_breakpoint_count > 1)
1419 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1420 breakpoint_count);
1421 else if (breakpoint_count > 0)
1422 new_arg = string_printf ("%d", breakpoint_count);
1423 }
1424 else
1425 new_arg = arg;
1426
1427 info.arg = new_arg.c_str ();
1428
1429 map_breakpoint_numbers (info.arg, do_map_commands_command, &info);
1430
1431 if (info.cmd == NULL)
1432 error (_("No breakpoints specified."));
1433
1434 do_cleanups (cleanups);
1435 }
1436
1437 static void
1438 commands_command (char *arg, int from_tty)
1439 {
1440 commands_command_1 (arg, from_tty, NULL);
1441 }
1442
1443 /* Like commands_command, but instead of reading the commands from
1444 input stream, takes them from an already parsed command structure.
1445
1446 This is used by cli-script.c to DTRT with breakpoint commands
1447 that are part of if and while bodies. */
1448 enum command_control_type
1449 commands_from_control_command (const char *arg, struct command_line *cmd)
1450 {
1451 commands_command_1 (arg, 0, cmd);
1452 return simple_control;
1453 }
1454
1455 /* Return non-zero if BL->TARGET_INFO contains valid information. */
1456
1457 static int
1458 bp_location_has_shadow (struct bp_location *bl)
1459 {
1460 if (bl->loc_type != bp_loc_software_breakpoint)
1461 return 0;
1462 if (!bl->inserted)
1463 return 0;
1464 if (bl->target_info.shadow_len == 0)
1465 /* BL isn't valid, or doesn't shadow memory. */
1466 return 0;
1467 return 1;
1468 }
1469
1470 /* Update BUF, which is LEN bytes read from the target address
1471 MEMADDR, by replacing a memory breakpoint with its shadowed
1472 contents.
1473
1474 If READBUF is not NULL, this buffer must not overlap with the of
1475 the breakpoint location's shadow_contents buffer. Otherwise, a
1476 failed assertion internal error will be raised. */
1477
1478 static void
1479 one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1480 const gdb_byte *writebuf_org,
1481 ULONGEST memaddr, LONGEST len,
1482 struct bp_target_info *target_info,
1483 struct gdbarch *gdbarch)
1484 {
1485 /* Now do full processing of the found relevant range of elements. */
1486 CORE_ADDR bp_addr = 0;
1487 int bp_size = 0;
1488 int bptoffset = 0;
1489
1490 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1491 current_program_space->aspace, 0))
1492 {
1493 /* The breakpoint is inserted in a different address space. */
1494 return;
1495 }
1496
1497 /* Addresses and length of the part of the breakpoint that
1498 we need to copy. */
1499 bp_addr = target_info->placed_address;
1500 bp_size = target_info->shadow_len;
1501
1502 if (bp_addr + bp_size <= memaddr)
1503 {
1504 /* The breakpoint is entirely before the chunk of memory we are
1505 reading. */
1506 return;
1507 }
1508
1509 if (bp_addr >= memaddr + len)
1510 {
1511 /* The breakpoint is entirely after the chunk of memory we are
1512 reading. */
1513 return;
1514 }
1515
1516 /* Offset within shadow_contents. */
1517 if (bp_addr < memaddr)
1518 {
1519 /* Only copy the second part of the breakpoint. */
1520 bp_size -= memaddr - bp_addr;
1521 bptoffset = memaddr - bp_addr;
1522 bp_addr = memaddr;
1523 }
1524
1525 if (bp_addr + bp_size > memaddr + len)
1526 {
1527 /* Only copy the first part of the breakpoint. */
1528 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1529 }
1530
1531 if (readbuf != NULL)
1532 {
1533 /* Verify that the readbuf buffer does not overlap with the
1534 shadow_contents buffer. */
1535 gdb_assert (target_info->shadow_contents >= readbuf + len
1536 || readbuf >= (target_info->shadow_contents
1537 + target_info->shadow_len));
1538
1539 /* Update the read buffer with this inserted breakpoint's
1540 shadow. */
1541 memcpy (readbuf + bp_addr - memaddr,
1542 target_info->shadow_contents + bptoffset, bp_size);
1543 }
1544 else
1545 {
1546 const unsigned char *bp;
1547 CORE_ADDR addr = target_info->reqstd_address;
1548 int placed_size;
1549
1550 /* Update the shadow with what we want to write to memory. */
1551 memcpy (target_info->shadow_contents + bptoffset,
1552 writebuf_org + bp_addr - memaddr, bp_size);
1553
1554 /* Determine appropriate breakpoint contents and size for this
1555 address. */
1556 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
1557
1558 /* Update the final write buffer with this inserted
1559 breakpoint's INSN. */
1560 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1561 }
1562 }
1563
1564 /* Update BUF, which is LEN bytes read from the target address MEMADDR,
1565 by replacing any memory breakpoints with their shadowed contents.
1566
1567 If READBUF is not NULL, this buffer must not overlap with any of
1568 the breakpoint location's shadow_contents buffers. Otherwise,
1569 a failed assertion internal error will be raised.
1570
1571 The range of shadowed area by each bp_location is:
1572 bl->address - bp_location_placed_address_before_address_max
1573 up to bl->address + bp_location_shadow_len_after_address_max
1574 The range we were requested to resolve shadows for is:
1575 memaddr ... memaddr + len
1576 Thus the safe cutoff boundaries for performance optimization are
1577 memaddr + len <= (bl->address
1578 - bp_location_placed_address_before_address_max)
1579 and:
1580 bl->address + bp_location_shadow_len_after_address_max <= memaddr */
1581
1582 void
1583 breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1584 const gdb_byte *writebuf_org,
1585 ULONGEST memaddr, LONGEST len)
1586 {
1587 /* Left boundary, right boundary and median element of our binary
1588 search. */
1589 unsigned bc_l, bc_r, bc;
1590
1591 /* Find BC_L which is a leftmost element which may affect BUF
1592 content. It is safe to report lower value but a failure to
1593 report higher one. */
1594
1595 bc_l = 0;
1596 bc_r = bp_location_count;
1597 while (bc_l + 1 < bc_r)
1598 {
1599 struct bp_location *bl;
1600
1601 bc = (bc_l + bc_r) / 2;
1602 bl = bp_location[bc];
1603
1604 /* Check first BL->ADDRESS will not overflow due to the added
1605 constant. Then advance the left boundary only if we are sure
1606 the BC element can in no way affect the BUF content (MEMADDR
1607 to MEMADDR + LEN range).
1608
1609 Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1610 offset so that we cannot miss a breakpoint with its shadow
1611 range tail still reaching MEMADDR. */
1612
1613 if ((bl->address + bp_location_shadow_len_after_address_max
1614 >= bl->address)
1615 && (bl->address + bp_location_shadow_len_after_address_max
1616 <= memaddr))
1617 bc_l = bc;
1618 else
1619 bc_r = bc;
1620 }
1621
1622 /* Due to the binary search above, we need to make sure we pick the
1623 first location that's at BC_L's address. E.g., if there are
1624 multiple locations at the same address, BC_L may end up pointing
1625 at a duplicate location, and miss the "master"/"inserted"
1626 location. Say, given locations L1, L2 and L3 at addresses A and
1627 B:
1628
1629 L1@A, L2@A, L3@B, ...
1630
1631 BC_L could end up pointing at location L2, while the "master"
1632 location could be L1. Since the `loc->inserted' flag is only set
1633 on "master" locations, we'd forget to restore the shadow of L1
1634 and L2. */
1635 while (bc_l > 0
1636 && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1637 bc_l--;
1638
1639 /* Now do full processing of the found relevant range of elements. */
1640
1641 for (bc = bc_l; bc < bp_location_count; bc++)
1642 {
1643 struct bp_location *bl = bp_location[bc];
1644
1645 /* bp_location array has BL->OWNER always non-NULL. */
1646 if (bl->owner->type == bp_none)
1647 warning (_("reading through apparently deleted breakpoint #%d?"),
1648 bl->owner->number);
1649
1650 /* Performance optimization: any further element can no longer affect BUF
1651 content. */
1652
1653 if (bl->address >= bp_location_placed_address_before_address_max
1654 && memaddr + len <= (bl->address
1655 - bp_location_placed_address_before_address_max))
1656 break;
1657
1658 if (!bp_location_has_shadow (bl))
1659 continue;
1660
1661 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1662 memaddr, len, &bl->target_info, bl->gdbarch);
1663 }
1664 }
1665
1666 \f
1667
1668 /* Return true if BPT is either a software breakpoint or a hardware
1669 breakpoint. */
1670
1671 int
1672 is_breakpoint (const struct breakpoint *bpt)
1673 {
1674 return (bpt->type == bp_breakpoint
1675 || bpt->type == bp_hardware_breakpoint
1676 || bpt->type == bp_dprintf);
1677 }
1678
1679 /* Return true if BPT is of any hardware watchpoint kind. */
1680
1681 static int
1682 is_hardware_watchpoint (const struct breakpoint *bpt)
1683 {
1684 return (bpt->type == bp_hardware_watchpoint
1685 || bpt->type == bp_read_watchpoint
1686 || bpt->type == bp_access_watchpoint);
1687 }
1688
1689 /* Return true if BPT is of any watchpoint kind, hardware or
1690 software. */
1691
1692 int
1693 is_watchpoint (const struct breakpoint *bpt)
1694 {
1695 return (is_hardware_watchpoint (bpt)
1696 || bpt->type == bp_watchpoint);
1697 }
1698
1699 /* Returns true if the current thread and its running state are safe
1700 to evaluate or update watchpoint B. Watchpoints on local
1701 expressions need to be evaluated in the context of the thread that
1702 was current when the watchpoint was created, and, that thread needs
1703 to be stopped to be able to select the correct frame context.
1704 Watchpoints on global expressions can be evaluated on any thread,
1705 and in any state. It is presently left to the target allowing
1706 memory accesses when threads are running. */
1707
1708 static int
1709 watchpoint_in_thread_scope (struct watchpoint *b)
1710 {
1711 return (b->base.pspace == current_program_space
1712 && (ptid_equal (b->watchpoint_thread, null_ptid)
1713 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1714 && !is_executing (inferior_ptid))));
1715 }
1716
1717 /* Set watchpoint B to disp_del_at_next_stop, even including its possible
1718 associated bp_watchpoint_scope breakpoint. */
1719
1720 static void
1721 watchpoint_del_at_next_stop (struct watchpoint *w)
1722 {
1723 struct breakpoint *b = &w->base;
1724
1725 if (b->related_breakpoint != b)
1726 {
1727 gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1728 gdb_assert (b->related_breakpoint->related_breakpoint == b);
1729 b->related_breakpoint->disposition = disp_del_at_next_stop;
1730 b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1731 b->related_breakpoint = b;
1732 }
1733 b->disposition = disp_del_at_next_stop;
1734 }
1735
1736 /* Extract a bitfield value from value VAL using the bit parameters contained in
1737 watchpoint W. */
1738
1739 static struct value *
1740 extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1741 {
1742 struct value *bit_val;
1743
1744 if (val == NULL)
1745 return NULL;
1746
1747 bit_val = allocate_value (value_type (val));
1748
1749 unpack_value_bitfield (bit_val,
1750 w->val_bitpos,
1751 w->val_bitsize,
1752 value_contents_for_printing (val),
1753 value_offset (val),
1754 val);
1755
1756 return bit_val;
1757 }
1758
1759 /* Allocate a dummy location and add it to B, which must be a software
1760 watchpoint. This is required because even if a software watchpoint
1761 is not watching any memory, bpstat_stop_status requires a location
1762 to be able to report stops. */
1763
1764 static void
1765 software_watchpoint_add_no_memory_location (struct breakpoint *b,
1766 struct program_space *pspace)
1767 {
1768 gdb_assert (b->type == bp_watchpoint && b->loc == NULL);
1769
1770 b->loc = allocate_bp_location (b);
1771 b->loc->pspace = pspace;
1772 b->loc->address = -1;
1773 b->loc->length = -1;
1774 }
1775
1776 /* Returns true if B is a software watchpoint that is not watching any
1777 memory (e.g., "watch $pc"). */
1778
1779 static int
1780 is_no_memory_software_watchpoint (struct breakpoint *b)
1781 {
1782 return (b->type == bp_watchpoint
1783 && b->loc != NULL
1784 && b->loc->next == NULL
1785 && b->loc->address == -1
1786 && b->loc->length == -1);
1787 }
1788
1789 /* Assuming that B is a watchpoint:
1790 - Reparse watchpoint expression, if REPARSE is non-zero
1791 - Evaluate expression and store the result in B->val
1792 - Evaluate the condition if there is one, and store the result
1793 in b->loc->cond.
1794 - Update the list of values that must be watched in B->loc.
1795
1796 If the watchpoint disposition is disp_del_at_next_stop, then do
1797 nothing. If this is local watchpoint that is out of scope, delete
1798 it.
1799
1800 Even with `set breakpoint always-inserted on' the watchpoints are
1801 removed + inserted on each stop here. Normal breakpoints must
1802 never be removed because they might be missed by a running thread
1803 when debugging in non-stop mode. On the other hand, hardware
1804 watchpoints (is_hardware_watchpoint; processed here) are specific
1805 to each LWP since they are stored in each LWP's hardware debug
1806 registers. Therefore, such LWP must be stopped first in order to
1807 be able to modify its hardware watchpoints.
1808
1809 Hardware watchpoints must be reset exactly once after being
1810 presented to the user. It cannot be done sooner, because it would
1811 reset the data used to present the watchpoint hit to the user. And
1812 it must not be done later because it could display the same single
1813 watchpoint hit during multiple GDB stops. Note that the latter is
1814 relevant only to the hardware watchpoint types bp_read_watchpoint
1815 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1816 not user-visible - its hit is suppressed if the memory content has
1817 not changed.
1818
1819 The following constraints influence the location where we can reset
1820 hardware watchpoints:
1821
1822 * target_stopped_by_watchpoint and target_stopped_data_address are
1823 called several times when GDB stops.
1824
1825 [linux]
1826 * Multiple hardware watchpoints can be hit at the same time,
1827 causing GDB to stop. GDB only presents one hardware watchpoint
1828 hit at a time as the reason for stopping, and all the other hits
1829 are presented later, one after the other, each time the user
1830 requests the execution to be resumed. Execution is not resumed
1831 for the threads still having pending hit event stored in
1832 LWP_INFO->STATUS. While the watchpoint is already removed from
1833 the inferior on the first stop the thread hit event is kept being
1834 reported from its cached value by linux_nat_stopped_data_address
1835 until the real thread resume happens after the watchpoint gets
1836 presented and thus its LWP_INFO->STATUS gets reset.
1837
1838 Therefore the hardware watchpoint hit can get safely reset on the
1839 watchpoint removal from inferior. */
1840
1841 static void
1842 update_watchpoint (struct watchpoint *b, int reparse)
1843 {
1844 int within_current_scope;
1845 struct frame_id saved_frame_id;
1846 int frame_saved;
1847
1848 /* If this is a local watchpoint, we only want to check if the
1849 watchpoint frame is in scope if the current thread is the thread
1850 that was used to create the watchpoint. */
1851 if (!watchpoint_in_thread_scope (b))
1852 return;
1853
1854 if (b->base.disposition == disp_del_at_next_stop)
1855 return;
1856
1857 frame_saved = 0;
1858
1859 /* Determine if the watchpoint is within scope. */
1860 if (b->exp_valid_block == NULL)
1861 within_current_scope = 1;
1862 else
1863 {
1864 struct frame_info *fi = get_current_frame ();
1865 struct gdbarch *frame_arch = get_frame_arch (fi);
1866 CORE_ADDR frame_pc = get_frame_pc (fi);
1867
1868 /* If we're at a point where the stack has been destroyed
1869 (e.g. in a function epilogue), unwinding may not work
1870 properly. Do not attempt to recreate locations at this
1871 point. See similar comments in watchpoint_check. */
1872 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
1873 return;
1874
1875 /* Save the current frame's ID so we can restore it after
1876 evaluating the watchpoint expression on its own frame. */
1877 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1878 took a frame parameter, so that we didn't have to change the
1879 selected frame. */
1880 frame_saved = 1;
1881 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1882
1883 fi = frame_find_by_id (b->watchpoint_frame);
1884 within_current_scope = (fi != NULL);
1885 if (within_current_scope)
1886 select_frame (fi);
1887 }
1888
1889 /* We don't free locations. They are stored in the bp_location array
1890 and update_global_location_list will eventually delete them and
1891 remove breakpoints if needed. */
1892 b->base.loc = NULL;
1893
1894 if (within_current_scope && reparse)
1895 {
1896 const char *s;
1897
1898 b->exp.reset ();
1899 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1900 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
1901 /* If the meaning of expression itself changed, the old value is
1902 no longer relevant. We don't want to report a watchpoint hit
1903 to the user when the old value and the new value may actually
1904 be completely different objects. */
1905 value_free (b->val);
1906 b->val = NULL;
1907 b->val_valid = 0;
1908
1909 /* Note that unlike with breakpoints, the watchpoint's condition
1910 expression is stored in the breakpoint object, not in the
1911 locations (re)created below. */
1912 if (b->base.cond_string != NULL)
1913 {
1914 b->cond_exp.reset ();
1915
1916 s = b->base.cond_string;
1917 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
1918 }
1919 }
1920
1921 /* If we failed to parse the expression, for example because
1922 it refers to a global variable in a not-yet-loaded shared library,
1923 don't try to insert watchpoint. We don't automatically delete
1924 such watchpoint, though, since failure to parse expression
1925 is different from out-of-scope watchpoint. */
1926 if (!target_has_execution)
1927 {
1928 /* Without execution, memory can't change. No use to try and
1929 set watchpoint locations. The watchpoint will be reset when
1930 the target gains execution, through breakpoint_re_set. */
1931 if (!can_use_hw_watchpoints)
1932 {
1933 if (b->base.ops->works_in_software_mode (&b->base))
1934 b->base.type = bp_watchpoint;
1935 else
1936 error (_("Can't set read/access watchpoint when "
1937 "hardware watchpoints are disabled."));
1938 }
1939 }
1940 else if (within_current_scope && b->exp)
1941 {
1942 int pc = 0;
1943 struct value *val_chain, *v, *result, *next;
1944 struct program_space *frame_pspace;
1945
1946 fetch_subexp_value (b->exp.get (), &pc, &v, &result, &val_chain, 0);
1947
1948 /* Avoid setting b->val if it's already set. The meaning of
1949 b->val is 'the last value' user saw, and we should update
1950 it only if we reported that last value to user. As it
1951 happens, the code that reports it updates b->val directly.
1952 We don't keep track of the memory value for masked
1953 watchpoints. */
1954 if (!b->val_valid && !is_masked_watchpoint (&b->base))
1955 {
1956 if (b->val_bitsize != 0)
1957 {
1958 v = extract_bitfield_from_watchpoint_value (b, v);
1959 if (v != NULL)
1960 release_value (v);
1961 }
1962 b->val = v;
1963 b->val_valid = 1;
1964 }
1965
1966 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1967
1968 /* Look at each value on the value chain. */
1969 for (v = val_chain; v; v = value_next (v))
1970 {
1971 /* If it's a memory location, and GDB actually needed
1972 its contents to evaluate the expression, then we
1973 must watch it. If the first value returned is
1974 still lazy, that means an error occurred reading it;
1975 watch it anyway in case it becomes readable. */
1976 if (VALUE_LVAL (v) == lval_memory
1977 && (v == val_chain || ! value_lazy (v)))
1978 {
1979 struct type *vtype = check_typedef (value_type (v));
1980
1981 /* We only watch structs and arrays if user asked
1982 for it explicitly, never if they just happen to
1983 appear in the middle of some value chain. */
1984 if (v == result
1985 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1986 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1987 {
1988 CORE_ADDR addr;
1989 enum target_hw_bp_type type;
1990 struct bp_location *loc, **tmp;
1991 int bitpos = 0, bitsize = 0;
1992
1993 if (value_bitsize (v) != 0)
1994 {
1995 /* Extract the bit parameters out from the bitfield
1996 sub-expression. */
1997 bitpos = value_bitpos (v);
1998 bitsize = value_bitsize (v);
1999 }
2000 else if (v == result && b->val_bitsize != 0)
2001 {
2002 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
2003 lvalue whose bit parameters are saved in the fields
2004 VAL_BITPOS and VAL_BITSIZE. */
2005 bitpos = b->val_bitpos;
2006 bitsize = b->val_bitsize;
2007 }
2008
2009 addr = value_address (v);
2010 if (bitsize != 0)
2011 {
2012 /* Skip the bytes that don't contain the bitfield. */
2013 addr += bitpos / 8;
2014 }
2015
2016 type = hw_write;
2017 if (b->base.type == bp_read_watchpoint)
2018 type = hw_read;
2019 else if (b->base.type == bp_access_watchpoint)
2020 type = hw_access;
2021
2022 loc = allocate_bp_location (&b->base);
2023 for (tmp = &(b->base.loc); *tmp != NULL; tmp = &((*tmp)->next))
2024 ;
2025 *tmp = loc;
2026 loc->gdbarch = get_type_arch (value_type (v));
2027
2028 loc->pspace = frame_pspace;
2029 loc->address = addr;
2030
2031 if (bitsize != 0)
2032 {
2033 /* Just cover the bytes that make up the bitfield. */
2034 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2035 }
2036 else
2037 loc->length = TYPE_LENGTH (value_type (v));
2038
2039 loc->watchpoint_type = type;
2040 }
2041 }
2042 }
2043
2044 /* Change the type of breakpoint between hardware assisted or
2045 an ordinary watchpoint depending on the hardware support
2046 and free hardware slots. REPARSE is set when the inferior
2047 is started. */
2048 if (reparse)
2049 {
2050 int reg_cnt;
2051 enum bp_loc_type loc_type;
2052 struct bp_location *bl;
2053
2054 reg_cnt = can_use_hardware_watchpoint (val_chain);
2055
2056 if (reg_cnt)
2057 {
2058 int i, target_resources_ok, other_type_used;
2059 enum bptype type;
2060
2061 /* Use an exact watchpoint when there's only one memory region to be
2062 watched, and only one debug register is needed to watch it. */
2063 b->exact = target_exact_watchpoints && reg_cnt == 1;
2064
2065 /* We need to determine how many resources are already
2066 used for all other hardware watchpoints plus this one
2067 to see if we still have enough resources to also fit
2068 this watchpoint in as well. */
2069
2070 /* If this is a software watchpoint, we try to turn it
2071 to a hardware one -- count resources as if B was of
2072 hardware watchpoint type. */
2073 type = b->base.type;
2074 if (type == bp_watchpoint)
2075 type = bp_hardware_watchpoint;
2076
2077 /* This watchpoint may or may not have been placed on
2078 the list yet at this point (it won't be in the list
2079 if we're trying to create it for the first time,
2080 through watch_command), so always account for it
2081 manually. */
2082
2083 /* Count resources used by all watchpoints except B. */
2084 i = hw_watchpoint_used_count_others (&b->base, type, &other_type_used);
2085
2086 /* Add in the resources needed for B. */
2087 i += hw_watchpoint_use_count (&b->base);
2088
2089 target_resources_ok
2090 = target_can_use_hardware_watchpoint (type, i, other_type_used);
2091 if (target_resources_ok <= 0)
2092 {
2093 int sw_mode = b->base.ops->works_in_software_mode (&b->base);
2094
2095 if (target_resources_ok == 0 && !sw_mode)
2096 error (_("Target does not support this type of "
2097 "hardware watchpoint."));
2098 else if (target_resources_ok < 0 && !sw_mode)
2099 error (_("There are not enough available hardware "
2100 "resources for this watchpoint."));
2101
2102 /* Downgrade to software watchpoint. */
2103 b->base.type = bp_watchpoint;
2104 }
2105 else
2106 {
2107 /* If this was a software watchpoint, we've just
2108 found we have enough resources to turn it to a
2109 hardware watchpoint. Otherwise, this is a
2110 nop. */
2111 b->base.type = type;
2112 }
2113 }
2114 else if (!b->base.ops->works_in_software_mode (&b->base))
2115 {
2116 if (!can_use_hw_watchpoints)
2117 error (_("Can't set read/access watchpoint when "
2118 "hardware watchpoints are disabled."));
2119 else
2120 error (_("Expression cannot be implemented with "
2121 "read/access watchpoint."));
2122 }
2123 else
2124 b->base.type = bp_watchpoint;
2125
2126 loc_type = (b->base.type == bp_watchpoint? bp_loc_other
2127 : bp_loc_hardware_watchpoint);
2128 for (bl = b->base.loc; bl; bl = bl->next)
2129 bl->loc_type = loc_type;
2130 }
2131
2132 for (v = val_chain; v; v = next)
2133 {
2134 next = value_next (v);
2135 if (v != b->val)
2136 value_free (v);
2137 }
2138
2139 /* If a software watchpoint is not watching any memory, then the
2140 above left it without any location set up. But,
2141 bpstat_stop_status requires a location to be able to report
2142 stops, so make sure there's at least a dummy one. */
2143 if (b->base.type == bp_watchpoint && b->base.loc == NULL)
2144 software_watchpoint_add_no_memory_location (&b->base, frame_pspace);
2145 }
2146 else if (!within_current_scope)
2147 {
2148 printf_filtered (_("\
2149 Watchpoint %d deleted because the program has left the block\n\
2150 in which its expression is valid.\n"),
2151 b->base.number);
2152 watchpoint_del_at_next_stop (b);
2153 }
2154
2155 /* Restore the selected frame. */
2156 if (frame_saved)
2157 select_frame (frame_find_by_id (saved_frame_id));
2158 }
2159
2160
2161 /* Returns 1 iff breakpoint location should be
2162 inserted in the inferior. We don't differentiate the type of BL's owner
2163 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2164 breakpoint_ops is not defined, because in insert_bp_location,
2165 tracepoint's insert_location will not be called. */
2166 static int
2167 should_be_inserted (struct bp_location *bl)
2168 {
2169 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
2170 return 0;
2171
2172 if (bl->owner->disposition == disp_del_at_next_stop)
2173 return 0;
2174
2175 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
2176 return 0;
2177
2178 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2179 return 0;
2180
2181 /* This is set for example, when we're attached to the parent of a
2182 vfork, and have detached from the child. The child is running
2183 free, and we expect it to do an exec or exit, at which point the
2184 OS makes the parent schedulable again (and the target reports
2185 that the vfork is done). Until the child is done with the shared
2186 memory region, do not insert breakpoints in the parent, otherwise
2187 the child could still trip on the parent's breakpoints. Since
2188 the parent is blocked anyway, it won't miss any breakpoint. */
2189 if (bl->pspace->breakpoints_not_allowed)
2190 return 0;
2191
2192 /* Don't insert a breakpoint if we're trying to step past its
2193 location, except if the breakpoint is a single-step breakpoint,
2194 and the breakpoint's thread is the thread which is stepping past
2195 a breakpoint. */
2196 if ((bl->loc_type == bp_loc_software_breakpoint
2197 || bl->loc_type == bp_loc_hardware_breakpoint)
2198 && stepping_past_instruction_at (bl->pspace->aspace,
2199 bl->address)
2200 /* The single-step breakpoint may be inserted at the location
2201 we're trying to step if the instruction branches to itself.
2202 However, the instruction won't be executed at all and it may
2203 break the semantics of the instruction, for example, the
2204 instruction is a conditional branch or updates some flags.
2205 We can't fix it unless GDB is able to emulate the instruction
2206 or switch to displaced stepping. */
2207 && !(bl->owner->type == bp_single_step
2208 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
2209 {
2210 if (debug_infrun)
2211 {
2212 fprintf_unfiltered (gdb_stdlog,
2213 "infrun: skipping breakpoint: "
2214 "stepping past insn at: %s\n",
2215 paddress (bl->gdbarch, bl->address));
2216 }
2217 return 0;
2218 }
2219
2220 /* Don't insert watchpoints if we're trying to step past the
2221 instruction that triggered one. */
2222 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2223 && stepping_past_nonsteppable_watchpoint ())
2224 {
2225 if (debug_infrun)
2226 {
2227 fprintf_unfiltered (gdb_stdlog,
2228 "infrun: stepping past non-steppable watchpoint. "
2229 "skipping watchpoint at %s:%d\n",
2230 paddress (bl->gdbarch, bl->address),
2231 bl->length);
2232 }
2233 return 0;
2234 }
2235
2236 return 1;
2237 }
2238
2239 /* Same as should_be_inserted but does the check assuming
2240 that the location is not duplicated. */
2241
2242 static int
2243 unduplicated_should_be_inserted (struct bp_location *bl)
2244 {
2245 int result;
2246 const int save_duplicate = bl->duplicate;
2247
2248 bl->duplicate = 0;
2249 result = should_be_inserted (bl);
2250 bl->duplicate = save_duplicate;
2251 return result;
2252 }
2253
2254 /* Parses a conditional described by an expression COND into an
2255 agent expression bytecode suitable for evaluation
2256 by the bytecode interpreter. Return NULL if there was
2257 any error during parsing. */
2258
2259 static struct agent_expr *
2260 parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2261 {
2262 struct agent_expr *aexpr = NULL;
2263
2264 if (!cond)
2265 return NULL;
2266
2267 /* We don't want to stop processing, so catch any errors
2268 that may show up. */
2269 TRY
2270 {
2271 aexpr = gen_eval_for_expr (scope, cond);
2272 }
2273
2274 CATCH (ex, RETURN_MASK_ERROR)
2275 {
2276 /* If we got here, it means the condition could not be parsed to a valid
2277 bytecode expression and thus can't be evaluated on the target's side.
2278 It's no use iterating through the conditions. */
2279 return NULL;
2280 }
2281 END_CATCH
2282
2283 /* We have a valid agent expression. */
2284 return aexpr;
2285 }
2286
2287 /* Based on location BL, create a list of breakpoint conditions to be
2288 passed on to the target. If we have duplicated locations with different
2289 conditions, we will add such conditions to the list. The idea is that the
2290 target will evaluate the list of conditions and will only notify GDB when
2291 one of them is true. */
2292
2293 static void
2294 build_target_condition_list (struct bp_location *bl)
2295 {
2296 struct bp_location **locp = NULL, **loc2p;
2297 int null_condition_or_parse_error = 0;
2298 int modified = bl->needs_update;
2299 struct bp_location *loc;
2300
2301 /* Release conditions left over from a previous insert. */
2302 VEC_free (agent_expr_p, bl->target_info.conditions);
2303
2304 /* This is only meaningful if the target is
2305 evaluating conditions and if the user has
2306 opted for condition evaluation on the target's
2307 side. */
2308 if (gdb_evaluates_breakpoint_condition_p ()
2309 || !target_supports_evaluation_of_breakpoint_conditions ())
2310 return;
2311
2312 /* Do a first pass to check for locations with no assigned
2313 conditions or conditions that fail to parse to a valid agent expression
2314 bytecode. If any of these happen, then it's no use to send conditions
2315 to the target since this location will always trigger and generate a
2316 response back to GDB. */
2317 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2318 {
2319 loc = (*loc2p);
2320 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2321 {
2322 if (modified)
2323 {
2324 struct agent_expr *aexpr;
2325
2326 /* Re-parse the conditions since something changed. In that
2327 case we already freed the condition bytecodes (see
2328 force_breakpoint_reinsertion). We just
2329 need to parse the condition to bytecodes again. */
2330 aexpr = parse_cond_to_aexpr (bl->address, loc->cond.get ());
2331 loc->cond_bytecode = aexpr;
2332 }
2333
2334 /* If we have a NULL bytecode expression, it means something
2335 went wrong or we have a null condition expression. */
2336 if (!loc->cond_bytecode)
2337 {
2338 null_condition_or_parse_error = 1;
2339 break;
2340 }
2341 }
2342 }
2343
2344 /* If any of these happened, it means we will have to evaluate the conditions
2345 for the location's address on gdb's side. It is no use keeping bytecodes
2346 for all the other duplicate locations, thus we free all of them here.
2347
2348 This is so we have a finer control over which locations' conditions are
2349 being evaluated by GDB or the remote stub. */
2350 if (null_condition_or_parse_error)
2351 {
2352 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2353 {
2354 loc = (*loc2p);
2355 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2356 {
2357 /* Only go as far as the first NULL bytecode is
2358 located. */
2359 if (!loc->cond_bytecode)
2360 return;
2361
2362 free_agent_expr (loc->cond_bytecode);
2363 loc->cond_bytecode = NULL;
2364 }
2365 }
2366 }
2367
2368 /* No NULL conditions or failed bytecode generation. Build a condition list
2369 for this location's address. */
2370 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2371 {
2372 loc = (*loc2p);
2373 if (loc->cond
2374 && is_breakpoint (loc->owner)
2375 && loc->pspace->num == bl->pspace->num
2376 && loc->owner->enable_state == bp_enabled
2377 && loc->enabled)
2378 /* Add the condition to the vector. This will be used later to send the
2379 conditions to the target. */
2380 VEC_safe_push (agent_expr_p, bl->target_info.conditions,
2381 loc->cond_bytecode);
2382 }
2383
2384 return;
2385 }
2386
2387 /* Parses a command described by string CMD into an agent expression
2388 bytecode suitable for evaluation by the bytecode interpreter.
2389 Return NULL if there was any error during parsing. */
2390
2391 static struct agent_expr *
2392 parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2393 {
2394 struct cleanup *old_cleanups = 0;
2395 struct expression **argvec;
2396 struct agent_expr *aexpr = NULL;
2397 const char *cmdrest;
2398 const char *format_start, *format_end;
2399 struct format_piece *fpieces;
2400 int nargs;
2401 struct gdbarch *gdbarch = get_current_arch ();
2402
2403 if (!cmd)
2404 return NULL;
2405
2406 cmdrest = cmd;
2407
2408 if (*cmdrest == ',')
2409 ++cmdrest;
2410 cmdrest = skip_spaces_const (cmdrest);
2411
2412 if (*cmdrest++ != '"')
2413 error (_("No format string following the location"));
2414
2415 format_start = cmdrest;
2416
2417 fpieces = parse_format_string (&cmdrest);
2418
2419 old_cleanups = make_cleanup (free_format_pieces_cleanup, &fpieces);
2420
2421 format_end = cmdrest;
2422
2423 if (*cmdrest++ != '"')
2424 error (_("Bad format string, non-terminated '\"'."));
2425
2426 cmdrest = skip_spaces_const (cmdrest);
2427
2428 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2429 error (_("Invalid argument syntax"));
2430
2431 if (*cmdrest == ',')
2432 cmdrest++;
2433 cmdrest = skip_spaces_const (cmdrest);
2434
2435 /* For each argument, make an expression. */
2436
2437 argvec = (struct expression **) alloca (strlen (cmd)
2438 * sizeof (struct expression *));
2439
2440 nargs = 0;
2441 while (*cmdrest != '\0')
2442 {
2443 const char *cmd1;
2444
2445 cmd1 = cmdrest;
2446 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2447 argvec[nargs++] = expr.release ();
2448 cmdrest = cmd1;
2449 if (*cmdrest == ',')
2450 ++cmdrest;
2451 }
2452
2453 /* We don't want to stop processing, so catch any errors
2454 that may show up. */
2455 TRY
2456 {
2457 aexpr = gen_printf (scope, gdbarch, 0, 0,
2458 format_start, format_end - format_start,
2459 fpieces, nargs, argvec);
2460 }
2461 CATCH (ex, RETURN_MASK_ERROR)
2462 {
2463 /* If we got here, it means the command could not be parsed to a valid
2464 bytecode expression and thus can't be evaluated on the target's side.
2465 It's no use iterating through the other commands. */
2466 aexpr = NULL;
2467 }
2468 END_CATCH
2469
2470 do_cleanups (old_cleanups);
2471
2472 /* We have a valid agent expression, return it. */
2473 return aexpr;
2474 }
2475
2476 /* Based on location BL, create a list of breakpoint commands to be
2477 passed on to the target. If we have duplicated locations with
2478 different commands, we will add any such to the list. */
2479
2480 static void
2481 build_target_command_list (struct bp_location *bl)
2482 {
2483 struct bp_location **locp = NULL, **loc2p;
2484 int null_command_or_parse_error = 0;
2485 int modified = bl->needs_update;
2486 struct bp_location *loc;
2487
2488 /* Release commands left over from a previous insert. */
2489 VEC_free (agent_expr_p, bl->target_info.tcommands);
2490
2491 if (!target_can_run_breakpoint_commands ())
2492 return;
2493
2494 /* For now, limit to agent-style dprintf breakpoints. */
2495 if (dprintf_style != dprintf_style_agent)
2496 return;
2497
2498 /* For now, if we have any duplicate location that isn't a dprintf,
2499 don't install the target-side commands, as that would make the
2500 breakpoint not be reported to the core, and we'd lose
2501 control. */
2502 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2503 {
2504 loc = (*loc2p);
2505 if (is_breakpoint (loc->owner)
2506 && loc->pspace->num == bl->pspace->num
2507 && loc->owner->type != bp_dprintf)
2508 return;
2509 }
2510
2511 /* Do a first pass to check for locations with no assigned
2512 conditions or conditions that fail to parse to a valid agent expression
2513 bytecode. If any of these happen, then it's no use to send conditions
2514 to the target since this location will always trigger and generate a
2515 response back to GDB. */
2516 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2517 {
2518 loc = (*loc2p);
2519 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2520 {
2521 if (modified)
2522 {
2523 struct agent_expr *aexpr;
2524
2525 /* Re-parse the commands since something changed. In that
2526 case we already freed the command bytecodes (see
2527 force_breakpoint_reinsertion). We just
2528 need to parse the command to bytecodes again. */
2529 aexpr = parse_cmd_to_aexpr (bl->address,
2530 loc->owner->extra_string);
2531 loc->cmd_bytecode = aexpr;
2532 }
2533
2534 /* If we have a NULL bytecode expression, it means something
2535 went wrong or we have a null command expression. */
2536 if (!loc->cmd_bytecode)
2537 {
2538 null_command_or_parse_error = 1;
2539 break;
2540 }
2541 }
2542 }
2543
2544 /* If anything failed, then we're not doing target-side commands,
2545 and so clean up. */
2546 if (null_command_or_parse_error)
2547 {
2548 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2549 {
2550 loc = (*loc2p);
2551 if (is_breakpoint (loc->owner)
2552 && loc->pspace->num == bl->pspace->num)
2553 {
2554 /* Only go as far as the first NULL bytecode is
2555 located. */
2556 if (loc->cmd_bytecode == NULL)
2557 return;
2558
2559 free_agent_expr (loc->cmd_bytecode);
2560 loc->cmd_bytecode = NULL;
2561 }
2562 }
2563 }
2564
2565 /* No NULL commands or failed bytecode generation. Build a command list
2566 for this location's address. */
2567 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2568 {
2569 loc = (*loc2p);
2570 if (loc->owner->extra_string
2571 && is_breakpoint (loc->owner)
2572 && loc->pspace->num == bl->pspace->num
2573 && loc->owner->enable_state == bp_enabled
2574 && loc->enabled)
2575 /* Add the command to the vector. This will be used later
2576 to send the commands to the target. */
2577 VEC_safe_push (agent_expr_p, bl->target_info.tcommands,
2578 loc->cmd_bytecode);
2579 }
2580
2581 bl->target_info.persist = 0;
2582 /* Maybe flag this location as persistent. */
2583 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2584 bl->target_info.persist = 1;
2585 }
2586
2587 /* Return the kind of breakpoint on address *ADDR. Get the kind
2588 of breakpoint according to ADDR except single-step breakpoint.
2589 Get the kind of single-step breakpoint according to the current
2590 registers state. */
2591
2592 static int
2593 breakpoint_kind (struct bp_location *bl, CORE_ADDR *addr)
2594 {
2595 if (bl->owner->type == bp_single_step)
2596 {
2597 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2598 struct regcache *regcache;
2599
2600 regcache = get_thread_regcache (thr->ptid);
2601
2602 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2603 regcache, addr);
2604 }
2605 else
2606 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
2607 }
2608
2609 /* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2610 location. Any error messages are printed to TMP_ERROR_STREAM; and
2611 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
2612 Returns 0 for success, 1 if the bp_location type is not supported or
2613 -1 for failure.
2614
2615 NOTE drow/2003-09-09: This routine could be broken down to an
2616 object-style method for each breakpoint or catchpoint type. */
2617 static int
2618 insert_bp_location (struct bp_location *bl,
2619 struct ui_file *tmp_error_stream,
2620 int *disabled_breaks,
2621 int *hw_breakpoint_error,
2622 int *hw_bp_error_explained_already)
2623 {
2624 enum errors bp_err = GDB_NO_ERROR;
2625 const char *bp_err_message = NULL;
2626
2627 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2628 return 0;
2629
2630 /* Note we don't initialize bl->target_info, as that wipes out
2631 the breakpoint location's shadow_contents if the breakpoint
2632 is still inserted at that location. This in turn breaks
2633 target_read_memory which depends on these buffers when
2634 a memory read is requested at the breakpoint location:
2635 Once the target_info has been wiped, we fail to see that
2636 we have a breakpoint inserted at that address and thus
2637 read the breakpoint instead of returning the data saved in
2638 the breakpoint location's shadow contents. */
2639 bl->target_info.reqstd_address = bl->address;
2640 bl->target_info.placed_address_space = bl->pspace->aspace;
2641 bl->target_info.length = bl->length;
2642
2643 /* When working with target-side conditions, we must pass all the conditions
2644 for the same breakpoint address down to the target since GDB will not
2645 insert those locations. With a list of breakpoint conditions, the target
2646 can decide when to stop and notify GDB. */
2647
2648 if (is_breakpoint (bl->owner))
2649 {
2650 build_target_condition_list (bl);
2651 build_target_command_list (bl);
2652 /* Reset the modification marker. */
2653 bl->needs_update = 0;
2654 }
2655
2656 if (bl->loc_type == bp_loc_software_breakpoint
2657 || bl->loc_type == bp_loc_hardware_breakpoint)
2658 {
2659 if (bl->owner->type != bp_hardware_breakpoint)
2660 {
2661 /* If the explicitly specified breakpoint type
2662 is not hardware breakpoint, check the memory map to see
2663 if the breakpoint address is in read only memory or not.
2664
2665 Two important cases are:
2666 - location type is not hardware breakpoint, memory
2667 is readonly. We change the type of the location to
2668 hardware breakpoint.
2669 - location type is hardware breakpoint, memory is
2670 read-write. This means we've previously made the
2671 location hardware one, but then the memory map changed,
2672 so we undo.
2673
2674 When breakpoints are removed, remove_breakpoints will use
2675 location types we've just set here, the only possible
2676 problem is that memory map has changed during running
2677 program, but it's not going to work anyway with current
2678 gdb. */
2679 struct mem_region *mr
2680 = lookup_mem_region (bl->target_info.reqstd_address);
2681
2682 if (mr)
2683 {
2684 if (automatic_hardware_breakpoints)
2685 {
2686 enum bp_loc_type new_type;
2687
2688 if (mr->attrib.mode != MEM_RW)
2689 new_type = bp_loc_hardware_breakpoint;
2690 else
2691 new_type = bp_loc_software_breakpoint;
2692
2693 if (new_type != bl->loc_type)
2694 {
2695 static int said = 0;
2696
2697 bl->loc_type = new_type;
2698 if (!said)
2699 {
2700 fprintf_filtered (gdb_stdout,
2701 _("Note: automatically using "
2702 "hardware breakpoints for "
2703 "read-only addresses.\n"));
2704 said = 1;
2705 }
2706 }
2707 }
2708 else if (bl->loc_type == bp_loc_software_breakpoint
2709 && mr->attrib.mode != MEM_RW)
2710 {
2711 fprintf_unfiltered (tmp_error_stream,
2712 _("Cannot insert breakpoint %d.\n"
2713 "Cannot set software breakpoint "
2714 "at read-only address %s\n"),
2715 bl->owner->number,
2716 paddress (bl->gdbarch, bl->address));
2717 return 1;
2718 }
2719 }
2720 }
2721
2722 /* First check to see if we have to handle an overlay. */
2723 if (overlay_debugging == ovly_off
2724 || bl->section == NULL
2725 || !(section_is_overlay (bl->section)))
2726 {
2727 /* No overlay handling: just set the breakpoint. */
2728 TRY
2729 {
2730 int val;
2731
2732 val = bl->owner->ops->insert_location (bl);
2733 if (val)
2734 bp_err = GENERIC_ERROR;
2735 }
2736 CATCH (e, RETURN_MASK_ALL)
2737 {
2738 bp_err = e.error;
2739 bp_err_message = e.message;
2740 }
2741 END_CATCH
2742 }
2743 else
2744 {
2745 /* This breakpoint is in an overlay section.
2746 Shall we set a breakpoint at the LMA? */
2747 if (!overlay_events_enabled)
2748 {
2749 /* Yes -- overlay event support is not active,
2750 so we must try to set a breakpoint at the LMA.
2751 This will not work for a hardware breakpoint. */
2752 if (bl->loc_type == bp_loc_hardware_breakpoint)
2753 warning (_("hardware breakpoint %d not supported in overlay!"),
2754 bl->owner->number);
2755 else
2756 {
2757 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2758 bl->section);
2759 /* Set a software (trap) breakpoint at the LMA. */
2760 bl->overlay_target_info = bl->target_info;
2761 bl->overlay_target_info.reqstd_address = addr;
2762
2763 /* No overlay handling: just set the breakpoint. */
2764 TRY
2765 {
2766 int val;
2767
2768 bl->overlay_target_info.kind
2769 = breakpoint_kind (bl, &addr);
2770 bl->overlay_target_info.placed_address = addr;
2771 val = target_insert_breakpoint (bl->gdbarch,
2772 &bl->overlay_target_info);
2773 if (val)
2774 bp_err = GENERIC_ERROR;
2775 }
2776 CATCH (e, RETURN_MASK_ALL)
2777 {
2778 bp_err = e.error;
2779 bp_err_message = e.message;
2780 }
2781 END_CATCH
2782
2783 if (bp_err != GDB_NO_ERROR)
2784 fprintf_unfiltered (tmp_error_stream,
2785 "Overlay breakpoint %d "
2786 "failed: in ROM?\n",
2787 bl->owner->number);
2788 }
2789 }
2790 /* Shall we set a breakpoint at the VMA? */
2791 if (section_is_mapped (bl->section))
2792 {
2793 /* Yes. This overlay section is mapped into memory. */
2794 TRY
2795 {
2796 int val;
2797
2798 val = bl->owner->ops->insert_location (bl);
2799 if (val)
2800 bp_err = GENERIC_ERROR;
2801 }
2802 CATCH (e, RETURN_MASK_ALL)
2803 {
2804 bp_err = e.error;
2805 bp_err_message = e.message;
2806 }
2807 END_CATCH
2808 }
2809 else
2810 {
2811 /* No. This breakpoint will not be inserted.
2812 No error, but do not mark the bp as 'inserted'. */
2813 return 0;
2814 }
2815 }
2816
2817 if (bp_err != GDB_NO_ERROR)
2818 {
2819 /* Can't set the breakpoint. */
2820
2821 /* In some cases, we might not be able to insert a
2822 breakpoint in a shared library that has already been
2823 removed, but we have not yet processed the shlib unload
2824 event. Unfortunately, some targets that implement
2825 breakpoint insertion themselves can't tell why the
2826 breakpoint insertion failed (e.g., the remote target
2827 doesn't define error codes), so we must treat generic
2828 errors as memory errors. */
2829 if ((bp_err == GENERIC_ERROR || bp_err == MEMORY_ERROR)
2830 && bl->loc_type == bp_loc_software_breakpoint
2831 && (solib_name_from_address (bl->pspace, bl->address)
2832 || shared_objfile_contains_address_p (bl->pspace,
2833 bl->address)))
2834 {
2835 /* See also: disable_breakpoints_in_shlibs. */
2836 bl->shlib_disabled = 1;
2837 observer_notify_breakpoint_modified (bl->owner);
2838 if (!*disabled_breaks)
2839 {
2840 fprintf_unfiltered (tmp_error_stream,
2841 "Cannot insert breakpoint %d.\n",
2842 bl->owner->number);
2843 fprintf_unfiltered (tmp_error_stream,
2844 "Temporarily disabling shared "
2845 "library breakpoints:\n");
2846 }
2847 *disabled_breaks = 1;
2848 fprintf_unfiltered (tmp_error_stream,
2849 "breakpoint #%d\n", bl->owner->number);
2850 return 0;
2851 }
2852 else
2853 {
2854 if (bl->loc_type == bp_loc_hardware_breakpoint)
2855 {
2856 *hw_breakpoint_error = 1;
2857 *hw_bp_error_explained_already = bp_err_message != NULL;
2858 fprintf_unfiltered (tmp_error_stream,
2859 "Cannot insert hardware breakpoint %d%s",
2860 bl->owner->number, bp_err_message ? ":" : ".\n");
2861 if (bp_err_message != NULL)
2862 fprintf_unfiltered (tmp_error_stream, "%s.\n", bp_err_message);
2863 }
2864 else
2865 {
2866 if (bp_err_message == NULL)
2867 {
2868 char *message
2869 = memory_error_message (TARGET_XFER_E_IO,
2870 bl->gdbarch, bl->address);
2871 struct cleanup *old_chain = make_cleanup (xfree, message);
2872
2873 fprintf_unfiltered (tmp_error_stream,
2874 "Cannot insert breakpoint %d.\n"
2875 "%s\n",
2876 bl->owner->number, message);
2877 do_cleanups (old_chain);
2878 }
2879 else
2880 {
2881 fprintf_unfiltered (tmp_error_stream,
2882 "Cannot insert breakpoint %d: %s\n",
2883 bl->owner->number,
2884 bp_err_message);
2885 }
2886 }
2887 return 1;
2888
2889 }
2890 }
2891 else
2892 bl->inserted = 1;
2893
2894 return 0;
2895 }
2896
2897 else if (bl->loc_type == bp_loc_hardware_watchpoint
2898 /* NOTE drow/2003-09-08: This state only exists for removing
2899 watchpoints. It's not clear that it's necessary... */
2900 && bl->owner->disposition != disp_del_at_next_stop)
2901 {
2902 int val;
2903
2904 gdb_assert (bl->owner->ops != NULL
2905 && bl->owner->ops->insert_location != NULL);
2906
2907 val = bl->owner->ops->insert_location (bl);
2908
2909 /* If trying to set a read-watchpoint, and it turns out it's not
2910 supported, try emulating one with an access watchpoint. */
2911 if (val == 1 && bl->watchpoint_type == hw_read)
2912 {
2913 struct bp_location *loc, **loc_temp;
2914
2915 /* But don't try to insert it, if there's already another
2916 hw_access location that would be considered a duplicate
2917 of this one. */
2918 ALL_BP_LOCATIONS (loc, loc_temp)
2919 if (loc != bl
2920 && loc->watchpoint_type == hw_access
2921 && watchpoint_locations_match (bl, loc))
2922 {
2923 bl->duplicate = 1;
2924 bl->inserted = 1;
2925 bl->target_info = loc->target_info;
2926 bl->watchpoint_type = hw_access;
2927 val = 0;
2928 break;
2929 }
2930
2931 if (val == 1)
2932 {
2933 bl->watchpoint_type = hw_access;
2934 val = bl->owner->ops->insert_location (bl);
2935
2936 if (val)
2937 /* Back to the original value. */
2938 bl->watchpoint_type = hw_read;
2939 }
2940 }
2941
2942 bl->inserted = (val == 0);
2943 }
2944
2945 else if (bl->owner->type == bp_catchpoint)
2946 {
2947 int val;
2948
2949 gdb_assert (bl->owner->ops != NULL
2950 && bl->owner->ops->insert_location != NULL);
2951
2952 val = bl->owner->ops->insert_location (bl);
2953 if (val)
2954 {
2955 bl->owner->enable_state = bp_disabled;
2956
2957 if (val == 1)
2958 warning (_("\
2959 Error inserting catchpoint %d: Your system does not support this type\n\
2960 of catchpoint."), bl->owner->number);
2961 else
2962 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2963 }
2964
2965 bl->inserted = (val == 0);
2966
2967 /* We've already printed an error message if there was a problem
2968 inserting this catchpoint, and we've disabled the catchpoint,
2969 so just return success. */
2970 return 0;
2971 }
2972
2973 return 0;
2974 }
2975
2976 /* This function is called when program space PSPACE is about to be
2977 deleted. It takes care of updating breakpoints to not reference
2978 PSPACE anymore. */
2979
2980 void
2981 breakpoint_program_space_exit (struct program_space *pspace)
2982 {
2983 struct breakpoint *b, *b_temp;
2984 struct bp_location *loc, **loc_temp;
2985
2986 /* Remove any breakpoint that was set through this program space. */
2987 ALL_BREAKPOINTS_SAFE (b, b_temp)
2988 {
2989 if (b->pspace == pspace)
2990 delete_breakpoint (b);
2991 }
2992
2993 /* Breakpoints set through other program spaces could have locations
2994 bound to PSPACE as well. Remove those. */
2995 ALL_BP_LOCATIONS (loc, loc_temp)
2996 {
2997 struct bp_location *tmp;
2998
2999 if (loc->pspace == pspace)
3000 {
3001 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
3002 if (loc->owner->loc == loc)
3003 loc->owner->loc = loc->next;
3004 else
3005 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
3006 if (tmp->next == loc)
3007 {
3008 tmp->next = loc->next;
3009 break;
3010 }
3011 }
3012 }
3013
3014 /* Now update the global location list to permanently delete the
3015 removed locations above. */
3016 update_global_location_list (UGLL_DONT_INSERT);
3017 }
3018
3019 /* Make sure all breakpoints are inserted in inferior.
3020 Throws exception on any error.
3021 A breakpoint that is already inserted won't be inserted
3022 again, so calling this function twice is safe. */
3023 void
3024 insert_breakpoints (void)
3025 {
3026 struct breakpoint *bpt;
3027
3028 ALL_BREAKPOINTS (bpt)
3029 if (is_hardware_watchpoint (bpt))
3030 {
3031 struct watchpoint *w = (struct watchpoint *) bpt;
3032
3033 update_watchpoint (w, 0 /* don't reparse. */);
3034 }
3035
3036 /* Updating watchpoints creates new locations, so update the global
3037 location list. Explicitly tell ugll to insert locations and
3038 ignore breakpoints_always_inserted_mode. */
3039 update_global_location_list (UGLL_INSERT);
3040 }
3041
3042 /* Invoke CALLBACK for each of bp_location. */
3043
3044 void
3045 iterate_over_bp_locations (walk_bp_location_callback callback)
3046 {
3047 struct bp_location *loc, **loc_tmp;
3048
3049 ALL_BP_LOCATIONS (loc, loc_tmp)
3050 {
3051 callback (loc, NULL);
3052 }
3053 }
3054
3055 /* This is used when we need to synch breakpoint conditions between GDB and the
3056 target. It is the case with deleting and disabling of breakpoints when using
3057 always-inserted mode. */
3058
3059 static void
3060 update_inserted_breakpoint_locations (void)
3061 {
3062 struct bp_location *bl, **blp_tmp;
3063 int error_flag = 0;
3064 int val = 0;
3065 int disabled_breaks = 0;
3066 int hw_breakpoint_error = 0;
3067 int hw_bp_details_reported = 0;
3068
3069 struct ui_file *tmp_error_stream = mem_fileopen ();
3070 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
3071
3072 /* Explicitly mark the warning -- this will only be printed if
3073 there was an error. */
3074 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
3075
3076 save_current_space_and_thread ();
3077
3078 ALL_BP_LOCATIONS (bl, blp_tmp)
3079 {
3080 /* We only want to update software breakpoints and hardware
3081 breakpoints. */
3082 if (!is_breakpoint (bl->owner))
3083 continue;
3084
3085 /* We only want to update locations that are already inserted
3086 and need updating. This is to avoid unwanted insertion during
3087 deletion of breakpoints. */
3088 if (!bl->inserted || (bl->inserted && !bl->needs_update))
3089 continue;
3090
3091 switch_to_program_space_and_thread (bl->pspace);
3092
3093 /* For targets that support global breakpoints, there's no need
3094 to select an inferior to insert breakpoint to. In fact, even
3095 if we aren't attached to any process yet, we should still
3096 insert breakpoints. */
3097 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3098 && ptid_equal (inferior_ptid, null_ptid))
3099 continue;
3100
3101 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
3102 &hw_breakpoint_error, &hw_bp_details_reported);
3103 if (val)
3104 error_flag = val;
3105 }
3106
3107 if (error_flag)
3108 {
3109 target_terminal_ours_for_output ();
3110 error_stream (tmp_error_stream);
3111 }
3112
3113 do_cleanups (cleanups);
3114 }
3115
3116 /* Used when starting or continuing the program. */
3117
3118 static void
3119 insert_breakpoint_locations (void)
3120 {
3121 struct breakpoint *bpt;
3122 struct bp_location *bl, **blp_tmp;
3123 int error_flag = 0;
3124 int val = 0;
3125 int disabled_breaks = 0;
3126 int hw_breakpoint_error = 0;
3127 int hw_bp_error_explained_already = 0;
3128
3129 struct ui_file *tmp_error_stream = mem_fileopen ();
3130 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
3131
3132 /* Explicitly mark the warning -- this will only be printed if
3133 there was an error. */
3134 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
3135
3136 save_current_space_and_thread ();
3137
3138 ALL_BP_LOCATIONS (bl, blp_tmp)
3139 {
3140 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
3141 continue;
3142
3143 /* There is no point inserting thread-specific breakpoints if
3144 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3145 has BL->OWNER always non-NULL. */
3146 if (bl->owner->thread != -1
3147 && !valid_global_thread_id (bl->owner->thread))
3148 continue;
3149
3150 switch_to_program_space_and_thread (bl->pspace);
3151
3152 /* For targets that support global breakpoints, there's no need
3153 to select an inferior to insert breakpoint to. In fact, even
3154 if we aren't attached to any process yet, we should still
3155 insert breakpoints. */
3156 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3157 && ptid_equal (inferior_ptid, null_ptid))
3158 continue;
3159
3160 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
3161 &hw_breakpoint_error, &hw_bp_error_explained_already);
3162 if (val)
3163 error_flag = val;
3164 }
3165
3166 /* If we failed to insert all locations of a watchpoint, remove
3167 them, as half-inserted watchpoint is of limited use. */
3168 ALL_BREAKPOINTS (bpt)
3169 {
3170 int some_failed = 0;
3171 struct bp_location *loc;
3172
3173 if (!is_hardware_watchpoint (bpt))
3174 continue;
3175
3176 if (!breakpoint_enabled (bpt))
3177 continue;
3178
3179 if (bpt->disposition == disp_del_at_next_stop)
3180 continue;
3181
3182 for (loc = bpt->loc; loc; loc = loc->next)
3183 if (!loc->inserted && should_be_inserted (loc))
3184 {
3185 some_failed = 1;
3186 break;
3187 }
3188 if (some_failed)
3189 {
3190 for (loc = bpt->loc; loc; loc = loc->next)
3191 if (loc->inserted)
3192 remove_breakpoint (loc);
3193
3194 hw_breakpoint_error = 1;
3195 fprintf_unfiltered (tmp_error_stream,
3196 "Could not insert hardware watchpoint %d.\n",
3197 bpt->number);
3198 error_flag = -1;
3199 }
3200 }
3201
3202 if (error_flag)
3203 {
3204 /* If a hardware breakpoint or watchpoint was inserted, add a
3205 message about possibly exhausted resources. */
3206 if (hw_breakpoint_error && !hw_bp_error_explained_already)
3207 {
3208 fprintf_unfiltered (tmp_error_stream,
3209 "Could not insert hardware breakpoints:\n\
3210 You may have requested too many hardware breakpoints/watchpoints.\n");
3211 }
3212 target_terminal_ours_for_output ();
3213 error_stream (tmp_error_stream);
3214 }
3215
3216 do_cleanups (cleanups);
3217 }
3218
3219 /* Used when the program stops.
3220 Returns zero if successful, or non-zero if there was a problem
3221 removing a breakpoint location. */
3222
3223 int
3224 remove_breakpoints (void)
3225 {
3226 struct bp_location *bl, **blp_tmp;
3227 int val = 0;
3228
3229 ALL_BP_LOCATIONS (bl, blp_tmp)
3230 {
3231 if (bl->inserted && !is_tracepoint (bl->owner))
3232 val |= remove_breakpoint (bl);
3233 }
3234 return val;
3235 }
3236
3237 /* When a thread exits, remove breakpoints that are related to
3238 that thread. */
3239
3240 static void
3241 remove_threaded_breakpoints (struct thread_info *tp, int silent)
3242 {
3243 struct breakpoint *b, *b_tmp;
3244
3245 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3246 {
3247 if (b->thread == tp->global_num && user_breakpoint_p (b))
3248 {
3249 b->disposition = disp_del_at_next_stop;
3250
3251 printf_filtered (_("\
3252 Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3253 b->number, print_thread_id (tp));
3254
3255 /* Hide it from the user. */
3256 b->number = 0;
3257 }
3258 }
3259 }
3260
3261 /* Remove breakpoints of process PID. */
3262
3263 int
3264 remove_breakpoints_pid (int pid)
3265 {
3266 struct bp_location *bl, **blp_tmp;
3267 int val;
3268 struct inferior *inf = find_inferior_pid (pid);
3269
3270 ALL_BP_LOCATIONS (bl, blp_tmp)
3271 {
3272 if (bl->pspace != inf->pspace)
3273 continue;
3274
3275 if (bl->inserted && !bl->target_info.persist)
3276 {
3277 val = remove_breakpoint (bl);
3278 if (val != 0)
3279 return val;
3280 }
3281 }
3282 return 0;
3283 }
3284
3285 int
3286 reattach_breakpoints (int pid)
3287 {
3288 struct cleanup *old_chain;
3289 struct bp_location *bl, **blp_tmp;
3290 int val;
3291 struct ui_file *tmp_error_stream;
3292 int dummy1 = 0, dummy2 = 0, dummy3 = 0;
3293 struct inferior *inf;
3294 struct thread_info *tp;
3295
3296 tp = any_live_thread_of_process (pid);
3297 if (tp == NULL)
3298 return 1;
3299
3300 inf = find_inferior_pid (pid);
3301 old_chain = save_inferior_ptid ();
3302
3303 inferior_ptid = tp->ptid;
3304
3305 tmp_error_stream = mem_fileopen ();
3306 make_cleanup_ui_file_delete (tmp_error_stream);
3307
3308 ALL_BP_LOCATIONS (bl, blp_tmp)
3309 {
3310 if (bl->pspace != inf->pspace)
3311 continue;
3312
3313 if (bl->inserted)
3314 {
3315 bl->inserted = 0;
3316 val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2, &dummy3);
3317 if (val != 0)
3318 {
3319 do_cleanups (old_chain);
3320 return val;
3321 }
3322 }
3323 }
3324 do_cleanups (old_chain);
3325 return 0;
3326 }
3327
3328 static int internal_breakpoint_number = -1;
3329
3330 /* Set the breakpoint number of B, depending on the value of INTERNAL.
3331 If INTERNAL is non-zero, the breakpoint number will be populated
3332 from internal_breakpoint_number and that variable decremented.
3333 Otherwise the breakpoint number will be populated from
3334 breakpoint_count and that value incremented. Internal breakpoints
3335 do not set the internal var bpnum. */
3336 static void
3337 set_breakpoint_number (int internal, struct breakpoint *b)
3338 {
3339 if (internal)
3340 b->number = internal_breakpoint_number--;
3341 else
3342 {
3343 set_breakpoint_count (breakpoint_count + 1);
3344 b->number = breakpoint_count;
3345 }
3346 }
3347
3348 static struct breakpoint *
3349 create_internal_breakpoint (struct gdbarch *gdbarch,
3350 CORE_ADDR address, enum bptype type,
3351 const struct breakpoint_ops *ops)
3352 {
3353 struct symtab_and_line sal;
3354 struct breakpoint *b;
3355
3356 init_sal (&sal); /* Initialize to zeroes. */
3357
3358 sal.pc = address;
3359 sal.section = find_pc_overlay (sal.pc);
3360 sal.pspace = current_program_space;
3361
3362 b = set_raw_breakpoint (gdbarch, sal, type, ops);
3363 b->number = internal_breakpoint_number--;
3364 b->disposition = disp_donttouch;
3365
3366 return b;
3367 }
3368
3369 static const char *const longjmp_names[] =
3370 {
3371 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3372 };
3373 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3374
3375 /* Per-objfile data private to breakpoint.c. */
3376 struct breakpoint_objfile_data
3377 {
3378 /* Minimal symbol for "_ovly_debug_event" (if any). */
3379 struct bound_minimal_symbol overlay_msym;
3380
3381 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3382 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
3383
3384 /* True if we have looked for longjmp probes. */
3385 int longjmp_searched;
3386
3387 /* SystemTap probe points for longjmp (if any). */
3388 VEC (probe_p) *longjmp_probes;
3389
3390 /* Minimal symbol for "std::terminate()" (if any). */
3391 struct bound_minimal_symbol terminate_msym;
3392
3393 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3394 struct bound_minimal_symbol exception_msym;
3395
3396 /* True if we have looked for exception probes. */
3397 int exception_searched;
3398
3399 /* SystemTap probe points for unwinding (if any). */
3400 VEC (probe_p) *exception_probes;
3401 };
3402
3403 static const struct objfile_data *breakpoint_objfile_key;
3404
3405 /* Minimal symbol not found sentinel. */
3406 static struct minimal_symbol msym_not_found;
3407
3408 /* Returns TRUE if MSYM point to the "not found" sentinel. */
3409
3410 static int
3411 msym_not_found_p (const struct minimal_symbol *msym)
3412 {
3413 return msym == &msym_not_found;
3414 }
3415
3416 /* Return per-objfile data needed by breakpoint.c.
3417 Allocate the data if necessary. */
3418
3419 static struct breakpoint_objfile_data *
3420 get_breakpoint_objfile_data (struct objfile *objfile)
3421 {
3422 struct breakpoint_objfile_data *bp_objfile_data;
3423
3424 bp_objfile_data = ((struct breakpoint_objfile_data *)
3425 objfile_data (objfile, breakpoint_objfile_key));
3426 if (bp_objfile_data == NULL)
3427 {
3428 bp_objfile_data =
3429 XOBNEW (&objfile->objfile_obstack, struct breakpoint_objfile_data);
3430
3431 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
3432 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3433 }
3434 return bp_objfile_data;
3435 }
3436
3437 static void
3438 free_breakpoint_probes (struct objfile *obj, void *data)
3439 {
3440 struct breakpoint_objfile_data *bp_objfile_data
3441 = (struct breakpoint_objfile_data *) data;
3442
3443 VEC_free (probe_p, bp_objfile_data->longjmp_probes);
3444 VEC_free (probe_p, bp_objfile_data->exception_probes);
3445 }
3446
3447 static void
3448 create_overlay_event_breakpoint (void)
3449 {
3450 struct objfile *objfile;
3451 const char *const func_name = "_ovly_debug_event";
3452
3453 ALL_OBJFILES (objfile)
3454 {
3455 struct breakpoint *b;
3456 struct breakpoint_objfile_data *bp_objfile_data;
3457 CORE_ADDR addr;
3458 struct explicit_location explicit_loc;
3459
3460 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3461
3462 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
3463 continue;
3464
3465 if (bp_objfile_data->overlay_msym.minsym == NULL)
3466 {
3467 struct bound_minimal_symbol m;
3468
3469 m = lookup_minimal_symbol_text (func_name, objfile);
3470 if (m.minsym == NULL)
3471 {
3472 /* Avoid future lookups in this objfile. */
3473 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
3474 continue;
3475 }
3476 bp_objfile_data->overlay_msym = m;
3477 }
3478
3479 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
3480 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3481 bp_overlay_event,
3482 &internal_breakpoint_ops);
3483 initialize_explicit_location (&explicit_loc);
3484 explicit_loc.function_name = ASTRDUP (func_name);
3485 b->location = new_explicit_location (&explicit_loc);
3486
3487 if (overlay_debugging == ovly_auto)
3488 {
3489 b->enable_state = bp_enabled;
3490 overlay_events_enabled = 1;
3491 }
3492 else
3493 {
3494 b->enable_state = bp_disabled;
3495 overlay_events_enabled = 0;
3496 }
3497 }
3498 }
3499
3500 static void
3501 create_longjmp_master_breakpoint (void)
3502 {
3503 struct program_space *pspace;
3504 struct cleanup *old_chain;
3505
3506 old_chain = save_current_program_space ();
3507
3508 ALL_PSPACES (pspace)
3509 {
3510 struct objfile *objfile;
3511
3512 set_current_program_space (pspace);
3513
3514 ALL_OBJFILES (objfile)
3515 {
3516 int i;
3517 struct gdbarch *gdbarch;
3518 struct breakpoint_objfile_data *bp_objfile_data;
3519
3520 gdbarch = get_objfile_arch (objfile);
3521
3522 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3523
3524 if (!bp_objfile_data->longjmp_searched)
3525 {
3526 VEC (probe_p) *ret;
3527
3528 ret = find_probes_in_objfile (objfile, "libc", "longjmp");
3529 if (ret != NULL)
3530 {
3531 /* We are only interested in checking one element. */
3532 struct probe *p = VEC_index (probe_p, ret, 0);
3533
3534 if (!can_evaluate_probe_arguments (p))
3535 {
3536 /* We cannot use the probe interface here, because it does
3537 not know how to evaluate arguments. */
3538 VEC_free (probe_p, ret);
3539 ret = NULL;
3540 }
3541 }
3542 bp_objfile_data->longjmp_probes = ret;
3543 bp_objfile_data->longjmp_searched = 1;
3544 }
3545
3546 if (bp_objfile_data->longjmp_probes != NULL)
3547 {
3548 int i;
3549 struct probe *probe;
3550 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3551
3552 for (i = 0;
3553 VEC_iterate (probe_p,
3554 bp_objfile_data->longjmp_probes,
3555 i, probe);
3556 ++i)
3557 {
3558 struct breakpoint *b;
3559
3560 b = create_internal_breakpoint (gdbarch,
3561 get_probe_address (probe,
3562 objfile),
3563 bp_longjmp_master,
3564 &internal_breakpoint_ops);
3565 b->location
3566 = new_probe_location ("-probe-stap libc:longjmp");
3567 b->enable_state = bp_disabled;
3568 }
3569
3570 continue;
3571 }
3572
3573 if (!gdbarch_get_longjmp_target_p (gdbarch))
3574 continue;
3575
3576 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
3577 {
3578 struct breakpoint *b;
3579 const char *func_name;
3580 CORE_ADDR addr;
3581 struct explicit_location explicit_loc;
3582
3583 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3584 continue;
3585
3586 func_name = longjmp_names[i];
3587 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3588 {
3589 struct bound_minimal_symbol m;
3590
3591 m = lookup_minimal_symbol_text (func_name, objfile);
3592 if (m.minsym == NULL)
3593 {
3594 /* Prevent future lookups in this objfile. */
3595 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
3596 continue;
3597 }
3598 bp_objfile_data->longjmp_msym[i] = m;
3599 }
3600
3601 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
3602 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3603 &internal_breakpoint_ops);
3604 initialize_explicit_location (&explicit_loc);
3605 explicit_loc.function_name = ASTRDUP (func_name);
3606 b->location = new_explicit_location (&explicit_loc);
3607 b->enable_state = bp_disabled;
3608 }
3609 }
3610 }
3611
3612 do_cleanups (old_chain);
3613 }
3614
3615 /* Create a master std::terminate breakpoint. */
3616 static void
3617 create_std_terminate_master_breakpoint (void)
3618 {
3619 struct program_space *pspace;
3620 struct cleanup *old_chain;
3621 const char *const func_name = "std::terminate()";
3622
3623 old_chain = save_current_program_space ();
3624
3625 ALL_PSPACES (pspace)
3626 {
3627 struct objfile *objfile;
3628 CORE_ADDR addr;
3629
3630 set_current_program_space (pspace);
3631
3632 ALL_OBJFILES (objfile)
3633 {
3634 struct breakpoint *b;
3635 struct breakpoint_objfile_data *bp_objfile_data;
3636 struct explicit_location explicit_loc;
3637
3638 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3639
3640 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3641 continue;
3642
3643 if (bp_objfile_data->terminate_msym.minsym == NULL)
3644 {
3645 struct bound_minimal_symbol m;
3646
3647 m = lookup_minimal_symbol (func_name, NULL, objfile);
3648 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3649 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
3650 {
3651 /* Prevent future lookups in this objfile. */
3652 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3653 continue;
3654 }
3655 bp_objfile_data->terminate_msym = m;
3656 }
3657
3658 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3659 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3660 bp_std_terminate_master,
3661 &internal_breakpoint_ops);
3662 initialize_explicit_location (&explicit_loc);
3663 explicit_loc.function_name = ASTRDUP (func_name);
3664 b->location = new_explicit_location (&explicit_loc);
3665 b->enable_state = bp_disabled;
3666 }
3667 }
3668
3669 do_cleanups (old_chain);
3670 }
3671
3672 /* Install a master breakpoint on the unwinder's debug hook. */
3673
3674 static void
3675 create_exception_master_breakpoint (void)
3676 {
3677 struct objfile *objfile;
3678 const char *const func_name = "_Unwind_DebugHook";
3679
3680 ALL_OBJFILES (objfile)
3681 {
3682 struct breakpoint *b;
3683 struct gdbarch *gdbarch;
3684 struct breakpoint_objfile_data *bp_objfile_data;
3685 CORE_ADDR addr;
3686 struct explicit_location explicit_loc;
3687
3688 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3689
3690 /* We prefer the SystemTap probe point if it exists. */
3691 if (!bp_objfile_data->exception_searched)
3692 {
3693 VEC (probe_p) *ret;
3694
3695 ret = find_probes_in_objfile (objfile, "libgcc", "unwind");
3696
3697 if (ret != NULL)
3698 {
3699 /* We are only interested in checking one element. */
3700 struct probe *p = VEC_index (probe_p, ret, 0);
3701
3702 if (!can_evaluate_probe_arguments (p))
3703 {
3704 /* We cannot use the probe interface here, because it does
3705 not know how to evaluate arguments. */
3706 VEC_free (probe_p, ret);
3707 ret = NULL;
3708 }
3709 }
3710 bp_objfile_data->exception_probes = ret;
3711 bp_objfile_data->exception_searched = 1;
3712 }
3713
3714 if (bp_objfile_data->exception_probes != NULL)
3715 {
3716 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3717 int i;
3718 struct probe *probe;
3719
3720 for (i = 0;
3721 VEC_iterate (probe_p,
3722 bp_objfile_data->exception_probes,
3723 i, probe);
3724 ++i)
3725 {
3726 struct breakpoint *b;
3727
3728 b = create_internal_breakpoint (gdbarch,
3729 get_probe_address (probe,
3730 objfile),
3731 bp_exception_master,
3732 &internal_breakpoint_ops);
3733 b->location
3734 = new_probe_location ("-probe-stap libgcc:unwind");
3735 b->enable_state = bp_disabled;
3736 }
3737
3738 continue;
3739 }
3740
3741 /* Otherwise, try the hook function. */
3742
3743 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3744 continue;
3745
3746 gdbarch = get_objfile_arch (objfile);
3747
3748 if (bp_objfile_data->exception_msym.minsym == NULL)
3749 {
3750 struct bound_minimal_symbol debug_hook;
3751
3752 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3753 if (debug_hook.minsym == NULL)
3754 {
3755 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3756 continue;
3757 }
3758
3759 bp_objfile_data->exception_msym = debug_hook;
3760 }
3761
3762 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
3763 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3764 &current_target);
3765 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3766 &internal_breakpoint_ops);
3767 initialize_explicit_location (&explicit_loc);
3768 explicit_loc.function_name = ASTRDUP (func_name);
3769 b->location = new_explicit_location (&explicit_loc);
3770 b->enable_state = bp_disabled;
3771 }
3772 }
3773
3774 /* Does B have a location spec? */
3775
3776 static int
3777 breakpoint_event_location_empty_p (const struct breakpoint *b)
3778 {
3779 return b->location != NULL && event_location_empty_p (b->location);
3780 }
3781
3782 void
3783 update_breakpoints_after_exec (void)
3784 {
3785 struct breakpoint *b, *b_tmp;
3786 struct bp_location *bploc, **bplocp_tmp;
3787
3788 /* We're about to delete breakpoints from GDB's lists. If the
3789 INSERTED flag is true, GDB will try to lift the breakpoints by
3790 writing the breakpoints' "shadow contents" back into memory. The
3791 "shadow contents" are NOT valid after an exec, so GDB should not
3792 do that. Instead, the target is responsible from marking
3793 breakpoints out as soon as it detects an exec. We don't do that
3794 here instead, because there may be other attempts to delete
3795 breakpoints after detecting an exec and before reaching here. */
3796 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
3797 if (bploc->pspace == current_program_space)
3798 gdb_assert (!bploc->inserted);
3799
3800 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3801 {
3802 if (b->pspace != current_program_space)
3803 continue;
3804
3805 /* Solib breakpoints must be explicitly reset after an exec(). */
3806 if (b->type == bp_shlib_event)
3807 {
3808 delete_breakpoint (b);
3809 continue;
3810 }
3811
3812 /* JIT breakpoints must be explicitly reset after an exec(). */
3813 if (b->type == bp_jit_event)
3814 {
3815 delete_breakpoint (b);
3816 continue;
3817 }
3818
3819 /* Thread event breakpoints must be set anew after an exec(),
3820 as must overlay event and longjmp master breakpoints. */
3821 if (b->type == bp_thread_event || b->type == bp_overlay_event
3822 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3823 || b->type == bp_exception_master)
3824 {
3825 delete_breakpoint (b);
3826 continue;
3827 }
3828
3829 /* Step-resume breakpoints are meaningless after an exec(). */
3830 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3831 {
3832 delete_breakpoint (b);
3833 continue;
3834 }
3835
3836 /* Just like single-step breakpoints. */
3837 if (b->type == bp_single_step)
3838 {
3839 delete_breakpoint (b);
3840 continue;
3841 }
3842
3843 /* Longjmp and longjmp-resume breakpoints are also meaningless
3844 after an exec. */
3845 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3846 || b->type == bp_longjmp_call_dummy
3847 || b->type == bp_exception || b->type == bp_exception_resume)
3848 {
3849 delete_breakpoint (b);
3850 continue;
3851 }
3852
3853 if (b->type == bp_catchpoint)
3854 {
3855 /* For now, none of the bp_catchpoint breakpoints need to
3856 do anything at this point. In the future, if some of
3857 the catchpoints need to something, we will need to add
3858 a new method, and call this method from here. */
3859 continue;
3860 }
3861
3862 /* bp_finish is a special case. The only way we ought to be able
3863 to see one of these when an exec() has happened, is if the user
3864 caught a vfork, and then said "finish". Ordinarily a finish just
3865 carries them to the call-site of the current callee, by setting
3866 a temporary bp there and resuming. But in this case, the finish
3867 will carry them entirely through the vfork & exec.
3868
3869 We don't want to allow a bp_finish to remain inserted now. But
3870 we can't safely delete it, 'cause finish_command has a handle to
3871 the bp on a bpstat, and will later want to delete it. There's a
3872 chance (and I've seen it happen) that if we delete the bp_finish
3873 here, that its storage will get reused by the time finish_command
3874 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3875 We really must allow finish_command to delete a bp_finish.
3876
3877 In the absence of a general solution for the "how do we know
3878 it's safe to delete something others may have handles to?"
3879 problem, what we'll do here is just uninsert the bp_finish, and
3880 let finish_command delete it.
3881
3882 (We know the bp_finish is "doomed" in the sense that it's
3883 momentary, and will be deleted as soon as finish_command sees
3884 the inferior stopped. So it doesn't matter that the bp's
3885 address is probably bogus in the new a.out, unlike e.g., the
3886 solib breakpoints.) */
3887
3888 if (b->type == bp_finish)
3889 {
3890 continue;
3891 }
3892
3893 /* Without a symbolic address, we have little hope of the
3894 pre-exec() address meaning the same thing in the post-exec()
3895 a.out. */
3896 if (breakpoint_event_location_empty_p (b))
3897 {
3898 delete_breakpoint (b);
3899 continue;
3900 }
3901 }
3902 }
3903
3904 int
3905 detach_breakpoints (ptid_t ptid)
3906 {
3907 struct bp_location *bl, **blp_tmp;
3908 int val = 0;
3909 struct cleanup *old_chain = save_inferior_ptid ();
3910 struct inferior *inf = current_inferior ();
3911
3912 if (ptid_get_pid (ptid) == ptid_get_pid (inferior_ptid))
3913 error (_("Cannot detach breakpoints of inferior_ptid"));
3914
3915 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
3916 inferior_ptid = ptid;
3917 ALL_BP_LOCATIONS (bl, blp_tmp)
3918 {
3919 if (bl->pspace != inf->pspace)
3920 continue;
3921
3922 /* This function must physically remove breakpoints locations
3923 from the specified ptid, without modifying the breakpoint
3924 package's state. Locations of type bp_loc_other are only
3925 maintained at GDB side. So, there is no need to remove
3926 these bp_loc_other locations. Moreover, removing these
3927 would modify the breakpoint package's state. */
3928 if (bl->loc_type == bp_loc_other)
3929 continue;
3930
3931 if (bl->inserted)
3932 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
3933 }
3934
3935 do_cleanups (old_chain);
3936 return val;
3937 }
3938
3939 /* Remove the breakpoint location BL from the current address space.
3940 Note that this is used to detach breakpoints from a child fork.
3941 When we get here, the child isn't in the inferior list, and neither
3942 do we have objects to represent its address space --- we should
3943 *not* look at bl->pspace->aspace here. */
3944
3945 static int
3946 remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
3947 {
3948 int val;
3949
3950 /* BL is never in moribund_locations by our callers. */
3951 gdb_assert (bl->owner != NULL);
3952
3953 /* The type of none suggests that owner is actually deleted.
3954 This should not ever happen. */
3955 gdb_assert (bl->owner->type != bp_none);
3956
3957 if (bl->loc_type == bp_loc_software_breakpoint
3958 || bl->loc_type == bp_loc_hardware_breakpoint)
3959 {
3960 /* "Normal" instruction breakpoint: either the standard
3961 trap-instruction bp (bp_breakpoint), or a
3962 bp_hardware_breakpoint. */
3963
3964 /* First check to see if we have to handle an overlay. */
3965 if (overlay_debugging == ovly_off
3966 || bl->section == NULL
3967 || !(section_is_overlay (bl->section)))
3968 {
3969 /* No overlay handling: just remove the breakpoint. */
3970
3971 /* If we're trying to uninsert a memory breakpoint that we
3972 know is set in a dynamic object that is marked
3973 shlib_disabled, then either the dynamic object was
3974 removed with "remove-symbol-file" or with
3975 "nosharedlibrary". In the former case, we don't know
3976 whether another dynamic object might have loaded over the
3977 breakpoint's address -- the user might well let us know
3978 about it next with add-symbol-file (the whole point of
3979 add-symbol-file is letting the user manually maintain a
3980 list of dynamically loaded objects). If we have the
3981 breakpoint's shadow memory, that is, this is a software
3982 breakpoint managed by GDB, check whether the breakpoint
3983 is still inserted in memory, to avoid overwriting wrong
3984 code with stale saved shadow contents. Note that HW
3985 breakpoints don't have shadow memory, as they're
3986 implemented using a mechanism that is not dependent on
3987 being able to modify the target's memory, and as such
3988 they should always be removed. */
3989 if (bl->shlib_disabled
3990 && bl->target_info.shadow_len != 0
3991 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3992 val = 0;
3993 else
3994 val = bl->owner->ops->remove_location (bl, reason);
3995 }
3996 else
3997 {
3998 /* This breakpoint is in an overlay section.
3999 Did we set a breakpoint at the LMA? */
4000 if (!overlay_events_enabled)
4001 {
4002 /* Yes -- overlay event support is not active, so we
4003 should have set a breakpoint at the LMA. Remove it.
4004 */
4005 /* Ignore any failures: if the LMA is in ROM, we will
4006 have already warned when we failed to insert it. */
4007 if (bl->loc_type == bp_loc_hardware_breakpoint)
4008 target_remove_hw_breakpoint (bl->gdbarch,
4009 &bl->overlay_target_info);
4010 else
4011 target_remove_breakpoint (bl->gdbarch,
4012 &bl->overlay_target_info,
4013 reason);
4014 }
4015 /* Did we set a breakpoint at the VMA?
4016 If so, we will have marked the breakpoint 'inserted'. */
4017 if (bl->inserted)
4018 {
4019 /* Yes -- remove it. Previously we did not bother to
4020 remove the breakpoint if the section had been
4021 unmapped, but let's not rely on that being safe. We
4022 don't know what the overlay manager might do. */
4023
4024 /* However, we should remove *software* breakpoints only
4025 if the section is still mapped, or else we overwrite
4026 wrong code with the saved shadow contents. */
4027 if (bl->loc_type == bp_loc_hardware_breakpoint
4028 || section_is_mapped (bl->section))
4029 val = bl->owner->ops->remove_location (bl, reason);
4030 else
4031 val = 0;
4032 }
4033 else
4034 {
4035 /* No -- not inserted, so no need to remove. No error. */
4036 val = 0;
4037 }
4038 }
4039
4040 /* In some cases, we might not be able to remove a breakpoint in
4041 a shared library that has already been removed, but we have
4042 not yet processed the shlib unload event. Similarly for an
4043 unloaded add-symbol-file object - the user might not yet have
4044 had the chance to remove-symbol-file it. shlib_disabled will
4045 be set if the library/object has already been removed, but
4046 the breakpoint hasn't been uninserted yet, e.g., after
4047 "nosharedlibrary" or "remove-symbol-file" with breakpoints
4048 always-inserted mode. */
4049 if (val
4050 && (bl->loc_type == bp_loc_software_breakpoint
4051 && (bl->shlib_disabled
4052 || solib_name_from_address (bl->pspace, bl->address)
4053 || shared_objfile_contains_address_p (bl->pspace,
4054 bl->address))))
4055 val = 0;
4056
4057 if (val)
4058 return val;
4059 bl->inserted = (reason == DETACH_BREAKPOINT);
4060 }
4061 else if (bl->loc_type == bp_loc_hardware_watchpoint)
4062 {
4063 gdb_assert (bl->owner->ops != NULL
4064 && bl->owner->ops->remove_location != NULL);
4065
4066 bl->inserted = (reason == DETACH_BREAKPOINT);
4067 bl->owner->ops->remove_location (bl, reason);
4068
4069 /* Failure to remove any of the hardware watchpoints comes here. */
4070 if (reason == REMOVE_BREAKPOINT && bl->inserted)
4071 warning (_("Could not remove hardware watchpoint %d."),
4072 bl->owner->number);
4073 }
4074 else if (bl->owner->type == bp_catchpoint
4075 && breakpoint_enabled (bl->owner)
4076 && !bl->duplicate)
4077 {
4078 gdb_assert (bl->owner->ops != NULL
4079 && bl->owner->ops->remove_location != NULL);
4080
4081 val = bl->owner->ops->remove_location (bl, reason);
4082 if (val)
4083 return val;
4084
4085 bl->inserted = (reason == DETACH_BREAKPOINT);
4086 }
4087
4088 return 0;
4089 }
4090
4091 static int
4092 remove_breakpoint (struct bp_location *bl)
4093 {
4094 int ret;
4095 struct cleanup *old_chain;
4096
4097 /* BL is never in moribund_locations by our callers. */
4098 gdb_assert (bl->owner != NULL);
4099
4100 /* The type of none suggests that owner is actually deleted.
4101 This should not ever happen. */
4102 gdb_assert (bl->owner->type != bp_none);
4103
4104 old_chain = save_current_space_and_thread ();
4105
4106 switch_to_program_space_and_thread (bl->pspace);
4107
4108 ret = remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
4109
4110 do_cleanups (old_chain);
4111 return ret;
4112 }
4113
4114 /* Clear the "inserted" flag in all breakpoints. */
4115
4116 void
4117 mark_breakpoints_out (void)
4118 {
4119 struct bp_location *bl, **blp_tmp;
4120
4121 ALL_BP_LOCATIONS (bl, blp_tmp)
4122 if (bl->pspace == current_program_space)
4123 bl->inserted = 0;
4124 }
4125
4126 /* Clear the "inserted" flag in all breakpoints and delete any
4127 breakpoints which should go away between runs of the program.
4128
4129 Plus other such housekeeping that has to be done for breakpoints
4130 between runs.
4131
4132 Note: this function gets called at the end of a run (by
4133 generic_mourn_inferior) and when a run begins (by
4134 init_wait_for_inferior). */
4135
4136
4137
4138 void
4139 breakpoint_init_inferior (enum inf_context context)
4140 {
4141 struct breakpoint *b, *b_tmp;
4142 struct bp_location *bl;
4143 int ix;
4144 struct program_space *pspace = current_program_space;
4145
4146 /* If breakpoint locations are shared across processes, then there's
4147 nothing to do. */
4148 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
4149 return;
4150
4151 mark_breakpoints_out ();
4152
4153 ALL_BREAKPOINTS_SAFE (b, b_tmp)
4154 {
4155 if (b->loc && b->loc->pspace != pspace)
4156 continue;
4157
4158 switch (b->type)
4159 {
4160 case bp_call_dummy:
4161 case bp_longjmp_call_dummy:
4162
4163 /* If the call dummy breakpoint is at the entry point it will
4164 cause problems when the inferior is rerun, so we better get
4165 rid of it. */
4166
4167 case bp_watchpoint_scope:
4168
4169 /* Also get rid of scope breakpoints. */
4170
4171 case bp_shlib_event:
4172
4173 /* Also remove solib event breakpoints. Their addresses may
4174 have changed since the last time we ran the program.
4175 Actually we may now be debugging against different target;
4176 and so the solib backend that installed this breakpoint may
4177 not be used in by the target. E.g.,
4178
4179 (gdb) file prog-linux
4180 (gdb) run # native linux target
4181 ...
4182 (gdb) kill
4183 (gdb) file prog-win.exe
4184 (gdb) tar rem :9999 # remote Windows gdbserver.
4185 */
4186
4187 case bp_step_resume:
4188
4189 /* Also remove step-resume breakpoints. */
4190
4191 case bp_single_step:
4192
4193 /* Also remove single-step breakpoints. */
4194
4195 delete_breakpoint (b);
4196 break;
4197
4198 case bp_watchpoint:
4199 case bp_hardware_watchpoint:
4200 case bp_read_watchpoint:
4201 case bp_access_watchpoint:
4202 {
4203 struct watchpoint *w = (struct watchpoint *) b;
4204
4205 /* Likewise for watchpoints on local expressions. */
4206 if (w->exp_valid_block != NULL)
4207 delete_breakpoint (b);
4208 else
4209 {
4210 /* Get rid of existing locations, which are no longer
4211 valid. New ones will be created in
4212 update_watchpoint, when the inferior is restarted.
4213 The next update_global_location_list call will
4214 garbage collect them. */
4215 b->loc = NULL;
4216
4217 if (context == inf_starting)
4218 {
4219 /* Reset val field to force reread of starting value in
4220 insert_breakpoints. */
4221 if (w->val)
4222 value_free (w->val);
4223 w->val = NULL;
4224 w->val_valid = 0;
4225 }
4226 }
4227 }
4228 break;
4229 default:
4230 break;
4231 }
4232 }
4233
4234 /* Get rid of the moribund locations. */
4235 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
4236 decref_bp_location (&bl);
4237 VEC_free (bp_location_p, moribund_locations);
4238 }
4239
4240 /* These functions concern about actual breakpoints inserted in the
4241 target --- to e.g. check if we need to do decr_pc adjustment or if
4242 we need to hop over the bkpt --- so we check for address space
4243 match, not program space. */
4244
4245 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4246 exists at PC. It returns ordinary_breakpoint_here if it's an
4247 ordinary breakpoint, or permanent_breakpoint_here if it's a
4248 permanent breakpoint.
4249 - When continuing from a location with an ordinary breakpoint, we
4250 actually single step once before calling insert_breakpoints.
4251 - When continuing from a location with a permanent breakpoint, we
4252 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4253 the target, to advance the PC past the breakpoint. */
4254
4255 enum breakpoint_here
4256 breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
4257 {
4258 struct bp_location *bl, **blp_tmp;
4259 int any_breakpoint_here = 0;
4260
4261 ALL_BP_LOCATIONS (bl, blp_tmp)
4262 {
4263 if (bl->loc_type != bp_loc_software_breakpoint
4264 && bl->loc_type != bp_loc_hardware_breakpoint)
4265 continue;
4266
4267 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
4268 if ((breakpoint_enabled (bl->owner)
4269 || bl->permanent)
4270 && breakpoint_location_address_match (bl, aspace, pc))
4271 {
4272 if (overlay_debugging
4273 && section_is_overlay (bl->section)
4274 && !section_is_mapped (bl->section))
4275 continue; /* unmapped overlay -- can't be a match */
4276 else if (bl->permanent)
4277 return permanent_breakpoint_here;
4278 else
4279 any_breakpoint_here = 1;
4280 }
4281 }
4282
4283 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
4284 }
4285
4286 /* See breakpoint.h. */
4287
4288 int
4289 breakpoint_in_range_p (struct address_space *aspace,
4290 CORE_ADDR addr, ULONGEST len)
4291 {
4292 struct bp_location *bl, **blp_tmp;
4293
4294 ALL_BP_LOCATIONS (bl, blp_tmp)
4295 {
4296 if (bl->loc_type != bp_loc_software_breakpoint
4297 && bl->loc_type != bp_loc_hardware_breakpoint)
4298 continue;
4299
4300 if ((breakpoint_enabled (bl->owner)
4301 || bl->permanent)
4302 && breakpoint_location_address_range_overlap (bl, aspace,
4303 addr, len))
4304 {
4305 if (overlay_debugging
4306 && section_is_overlay (bl->section)
4307 && !section_is_mapped (bl->section))
4308 {
4309 /* Unmapped overlay -- can't be a match. */
4310 continue;
4311 }
4312
4313 return 1;
4314 }
4315 }
4316
4317 return 0;
4318 }
4319
4320 /* Return true if there's a moribund breakpoint at PC. */
4321
4322 int
4323 moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
4324 {
4325 struct bp_location *loc;
4326 int ix;
4327
4328 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
4329 if (breakpoint_location_address_match (loc, aspace, pc))
4330 return 1;
4331
4332 return 0;
4333 }
4334
4335 /* Returns non-zero iff BL is inserted at PC, in address space
4336 ASPACE. */
4337
4338 static int
4339 bp_location_inserted_here_p (struct bp_location *bl,
4340 struct address_space *aspace, CORE_ADDR pc)
4341 {
4342 if (bl->inserted
4343 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4344 aspace, pc))
4345 {
4346 if (overlay_debugging
4347 && section_is_overlay (bl->section)
4348 && !section_is_mapped (bl->section))
4349 return 0; /* unmapped overlay -- can't be a match */
4350 else
4351 return 1;
4352 }
4353 return 0;
4354 }
4355
4356 /* Returns non-zero iff there's a breakpoint inserted at PC. */
4357
4358 int
4359 breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
4360 {
4361 struct bp_location **blp, **blp_tmp = NULL;
4362
4363 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4364 {
4365 struct bp_location *bl = *blp;
4366
4367 if (bl->loc_type != bp_loc_software_breakpoint
4368 && bl->loc_type != bp_loc_hardware_breakpoint)
4369 continue;
4370
4371 if (bp_location_inserted_here_p (bl, aspace, pc))
4372 return 1;
4373 }
4374 return 0;
4375 }
4376
4377 /* This function returns non-zero iff there is a software breakpoint
4378 inserted at PC. */
4379
4380 int
4381 software_breakpoint_inserted_here_p (struct address_space *aspace,
4382 CORE_ADDR pc)
4383 {
4384 struct bp_location **blp, **blp_tmp = NULL;
4385
4386 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4387 {
4388 struct bp_location *bl = *blp;
4389
4390 if (bl->loc_type != bp_loc_software_breakpoint)
4391 continue;
4392
4393 if (bp_location_inserted_here_p (bl, aspace, pc))
4394 return 1;
4395 }
4396
4397 return 0;
4398 }
4399
4400 /* See breakpoint.h. */
4401
4402 int
4403 hardware_breakpoint_inserted_here_p (struct address_space *aspace,
4404 CORE_ADDR pc)
4405 {
4406 struct bp_location **blp, **blp_tmp = NULL;
4407
4408 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4409 {
4410 struct bp_location *bl = *blp;
4411
4412 if (bl->loc_type != bp_loc_hardware_breakpoint)
4413 continue;
4414
4415 if (bp_location_inserted_here_p (bl, aspace, pc))
4416 return 1;
4417 }
4418
4419 return 0;
4420 }
4421
4422 int
4423 hardware_watchpoint_inserted_in_range (struct address_space *aspace,
4424 CORE_ADDR addr, ULONGEST len)
4425 {
4426 struct breakpoint *bpt;
4427
4428 ALL_BREAKPOINTS (bpt)
4429 {
4430 struct bp_location *loc;
4431
4432 if (bpt->type != bp_hardware_watchpoint
4433 && bpt->type != bp_access_watchpoint)
4434 continue;
4435
4436 if (!breakpoint_enabled (bpt))
4437 continue;
4438
4439 for (loc = bpt->loc; loc; loc = loc->next)
4440 if (loc->pspace->aspace == aspace && loc->inserted)
4441 {
4442 CORE_ADDR l, h;
4443
4444 /* Check for intersection. */
4445 l = std::max<CORE_ADDR> (loc->address, addr);
4446 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
4447 if (l < h)
4448 return 1;
4449 }
4450 }
4451 return 0;
4452 }
4453 \f
4454
4455 /* bpstat stuff. External routines' interfaces are documented
4456 in breakpoint.h. */
4457
4458 int
4459 is_catchpoint (struct breakpoint *ep)
4460 {
4461 return (ep->type == bp_catchpoint);
4462 }
4463
4464 /* Frees any storage that is part of a bpstat. Does not walk the
4465 'next' chain. */
4466
4467 static void
4468 bpstat_free (bpstat bs)
4469 {
4470 if (bs->old_val != NULL)
4471 value_free (bs->old_val);
4472 decref_counted_command_line (&bs->commands);
4473 decref_bp_location (&bs->bp_location_at);
4474 xfree (bs);
4475 }
4476
4477 /* Clear a bpstat so that it says we are not at any breakpoint.
4478 Also free any storage that is part of a bpstat. */
4479
4480 void
4481 bpstat_clear (bpstat *bsp)
4482 {
4483 bpstat p;
4484 bpstat q;
4485
4486 if (bsp == 0)
4487 return;
4488 p = *bsp;
4489 while (p != NULL)
4490 {
4491 q = p->next;
4492 bpstat_free (p);
4493 p = q;
4494 }
4495 *bsp = NULL;
4496 }
4497
4498 /* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4499 is part of the bpstat is copied as well. */
4500
4501 bpstat
4502 bpstat_copy (bpstat bs)
4503 {
4504 bpstat p = NULL;
4505 bpstat tmp;
4506 bpstat retval = NULL;
4507
4508 if (bs == NULL)
4509 return bs;
4510
4511 for (; bs != NULL; bs = bs->next)
4512 {
4513 tmp = (bpstat) xmalloc (sizeof (*tmp));
4514 memcpy (tmp, bs, sizeof (*tmp));
4515 incref_counted_command_line (tmp->commands);
4516 incref_bp_location (tmp->bp_location_at);
4517 if (bs->old_val != NULL)
4518 {
4519 tmp->old_val = value_copy (bs->old_val);
4520 release_value (tmp->old_val);
4521 }
4522
4523 if (p == NULL)
4524 /* This is the first thing in the chain. */
4525 retval = tmp;
4526 else
4527 p->next = tmp;
4528 p = tmp;
4529 }
4530 p->next = NULL;
4531 return retval;
4532 }
4533
4534 /* Find the bpstat associated with this breakpoint. */
4535
4536 bpstat
4537 bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
4538 {
4539 if (bsp == NULL)
4540 return NULL;
4541
4542 for (; bsp != NULL; bsp = bsp->next)
4543 {
4544 if (bsp->breakpoint_at == breakpoint)
4545 return bsp;
4546 }
4547 return NULL;
4548 }
4549
4550 /* See breakpoint.h. */
4551
4552 int
4553 bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
4554 {
4555 for (; bsp != NULL; bsp = bsp->next)
4556 {
4557 if (bsp->breakpoint_at == NULL)
4558 {
4559 /* A moribund location can never explain a signal other than
4560 GDB_SIGNAL_TRAP. */
4561 if (sig == GDB_SIGNAL_TRAP)
4562 return 1;
4563 }
4564 else
4565 {
4566 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4567 sig))
4568 return 1;
4569 }
4570 }
4571
4572 return 0;
4573 }
4574
4575 /* Put in *NUM the breakpoint number of the first breakpoint we are
4576 stopped at. *BSP upon return is a bpstat which points to the
4577 remaining breakpoints stopped at (but which is not guaranteed to be
4578 good for anything but further calls to bpstat_num).
4579
4580 Return 0 if passed a bpstat which does not indicate any breakpoints.
4581 Return -1 if stopped at a breakpoint that has been deleted since
4582 we set it.
4583 Return 1 otherwise. */
4584
4585 int
4586 bpstat_num (bpstat *bsp, int *num)
4587 {
4588 struct breakpoint *b;
4589
4590 if ((*bsp) == NULL)
4591 return 0; /* No more breakpoint values */
4592
4593 /* We assume we'll never have several bpstats that correspond to a
4594 single breakpoint -- otherwise, this function might return the
4595 same number more than once and this will look ugly. */
4596 b = (*bsp)->breakpoint_at;
4597 *bsp = (*bsp)->next;
4598 if (b == NULL)
4599 return -1; /* breakpoint that's been deleted since */
4600
4601 *num = b->number; /* We have its number */
4602 return 1;
4603 }
4604
4605 /* See breakpoint.h. */
4606
4607 void
4608 bpstat_clear_actions (void)
4609 {
4610 struct thread_info *tp;
4611 bpstat bs;
4612
4613 if (ptid_equal (inferior_ptid, null_ptid))
4614 return;
4615
4616 tp = find_thread_ptid (inferior_ptid);
4617 if (tp == NULL)
4618 return;
4619
4620 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
4621 {
4622 decref_counted_command_line (&bs->commands);
4623
4624 if (bs->old_val != NULL)
4625 {
4626 value_free (bs->old_val);
4627 bs->old_val = NULL;
4628 }
4629 }
4630 }
4631
4632 /* Called when a command is about to proceed the inferior. */
4633
4634 static void
4635 breakpoint_about_to_proceed (void)
4636 {
4637 if (!ptid_equal (inferior_ptid, null_ptid))
4638 {
4639 struct thread_info *tp = inferior_thread ();
4640
4641 /* Allow inferior function calls in breakpoint commands to not
4642 interrupt the command list. When the call finishes
4643 successfully, the inferior will be standing at the same
4644 breakpoint as if nothing happened. */
4645 if (tp->control.in_infcall)
4646 return;
4647 }
4648
4649 breakpoint_proceeded = 1;
4650 }
4651
4652 /* Stub for cleaning up our state if we error-out of a breakpoint
4653 command. */
4654 static void
4655 cleanup_executing_breakpoints (void *ignore)
4656 {
4657 executing_breakpoint_commands = 0;
4658 }
4659
4660 /* Return non-zero iff CMD as the first line of a command sequence is `silent'
4661 or its equivalent. */
4662
4663 static int
4664 command_line_is_silent (struct command_line *cmd)
4665 {
4666 return cmd && (strcmp ("silent", cmd->line) == 0);
4667 }
4668
4669 /* Execute all the commands associated with all the breakpoints at
4670 this location. Any of these commands could cause the process to
4671 proceed beyond this point, etc. We look out for such changes by
4672 checking the global "breakpoint_proceeded" after each command.
4673
4674 Returns true if a breakpoint command resumed the inferior. In that
4675 case, it is the caller's responsibility to recall it again with the
4676 bpstat of the current thread. */
4677
4678 static int
4679 bpstat_do_actions_1 (bpstat *bsp)
4680 {
4681 bpstat bs;
4682 struct cleanup *old_chain;
4683 int again = 0;
4684
4685 /* Avoid endless recursion if a `source' command is contained
4686 in bs->commands. */
4687 if (executing_breakpoint_commands)
4688 return 0;
4689
4690 executing_breakpoint_commands = 1;
4691 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
4692
4693 prevent_dont_repeat ();
4694
4695 /* This pointer will iterate over the list of bpstat's. */
4696 bs = *bsp;
4697
4698 breakpoint_proceeded = 0;
4699 for (; bs != NULL; bs = bs->next)
4700 {
4701 struct counted_command_line *ccmd;
4702 struct command_line *cmd;
4703 struct cleanup *this_cmd_tree_chain;
4704
4705 /* Take ownership of the BSP's command tree, if it has one.
4706
4707 The command tree could legitimately contain commands like
4708 'step' and 'next', which call clear_proceed_status, which
4709 frees stop_bpstat's command tree. To make sure this doesn't
4710 free the tree we're executing out from under us, we need to
4711 take ownership of the tree ourselves. Since a given bpstat's
4712 commands are only executed once, we don't need to copy it; we
4713 can clear the pointer in the bpstat, and make sure we free
4714 the tree when we're done. */
4715 ccmd = bs->commands;
4716 bs->commands = NULL;
4717 this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
4718 cmd = ccmd ? ccmd->commands : NULL;
4719 if (command_line_is_silent (cmd))
4720 {
4721 /* The action has been already done by bpstat_stop_status. */
4722 cmd = cmd->next;
4723 }
4724
4725 while (cmd != NULL)
4726 {
4727 execute_control_command (cmd);
4728
4729 if (breakpoint_proceeded)
4730 break;
4731 else
4732 cmd = cmd->next;
4733 }
4734
4735 /* We can free this command tree now. */
4736 do_cleanups (this_cmd_tree_chain);
4737
4738 if (breakpoint_proceeded)
4739 {
4740 if (current_ui->async)
4741 /* If we are in async mode, then the target might be still
4742 running, not stopped at any breakpoint, so nothing for
4743 us to do here -- just return to the event loop. */
4744 ;
4745 else
4746 /* In sync mode, when execute_control_command returns
4747 we're already standing on the next breakpoint.
4748 Breakpoint commands for that stop were not run, since
4749 execute_command does not run breakpoint commands --
4750 only command_line_handler does, but that one is not
4751 involved in execution of breakpoint commands. So, we
4752 can now execute breakpoint commands. It should be
4753 noted that making execute_command do bpstat actions is
4754 not an option -- in this case we'll have recursive
4755 invocation of bpstat for each breakpoint with a
4756 command, and can easily blow up GDB stack. Instead, we
4757 return true, which will trigger the caller to recall us
4758 with the new stop_bpstat. */
4759 again = 1;
4760 break;
4761 }
4762 }
4763 do_cleanups (old_chain);
4764 return again;
4765 }
4766
4767 void
4768 bpstat_do_actions (void)
4769 {
4770 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4771
4772 /* Do any commands attached to breakpoint we are stopped at. */
4773 while (!ptid_equal (inferior_ptid, null_ptid)
4774 && target_has_execution
4775 && !is_exited (inferior_ptid)
4776 && !is_executing (inferior_ptid))
4777 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4778 and only return when it is stopped at the next breakpoint, we
4779 keep doing breakpoint actions until it returns false to
4780 indicate the inferior was not resumed. */
4781 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
4782 break;
4783
4784 discard_cleanups (cleanup_if_error);
4785 }
4786
4787 /* Print out the (old or new) value associated with a watchpoint. */
4788
4789 static void
4790 watchpoint_value_print (struct value *val, struct ui_file *stream)
4791 {
4792 if (val == NULL)
4793 fprintf_unfiltered (stream, _("<unreadable>"));
4794 else
4795 {
4796 struct value_print_options opts;
4797 get_user_print_options (&opts);
4798 value_print (val, stream, &opts);
4799 }
4800 }
4801
4802 /* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4803 debugging multiple threads. */
4804
4805 void
4806 maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4807 {
4808 if (ui_out_is_mi_like_p (uiout))
4809 return;
4810
4811 ui_out_text (uiout, "\n");
4812
4813 if (show_thread_that_caused_stop ())
4814 {
4815 const char *name;
4816 struct thread_info *thr = inferior_thread ();
4817
4818 ui_out_text (uiout, "Thread ");
4819 ui_out_field_fmt (uiout, "thread-id", "%s", print_thread_id (thr));
4820
4821 name = thr->name != NULL ? thr->name : target_thread_name (thr);
4822 if (name != NULL)
4823 {
4824 ui_out_text (uiout, " \"");
4825 ui_out_field_fmt (uiout, "name", "%s", name);
4826 ui_out_text (uiout, "\"");
4827 }
4828
4829 ui_out_text (uiout, " hit ");
4830 }
4831 }
4832
4833 /* Generic routine for printing messages indicating why we
4834 stopped. The behavior of this function depends on the value
4835 'print_it' in the bpstat structure. Under some circumstances we
4836 may decide not to print anything here and delegate the task to
4837 normal_stop(). */
4838
4839 static enum print_stop_action
4840 print_bp_stop_message (bpstat bs)
4841 {
4842 switch (bs->print_it)
4843 {
4844 case print_it_noop:
4845 /* Nothing should be printed for this bpstat entry. */
4846 return PRINT_UNKNOWN;
4847 break;
4848
4849 case print_it_done:
4850 /* We still want to print the frame, but we already printed the
4851 relevant messages. */
4852 return PRINT_SRC_AND_LOC;
4853 break;
4854
4855 case print_it_normal:
4856 {
4857 struct breakpoint *b = bs->breakpoint_at;
4858
4859 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4860 which has since been deleted. */
4861 if (b == NULL)
4862 return PRINT_UNKNOWN;
4863
4864 /* Normal case. Call the breakpoint's print_it method. */
4865 return b->ops->print_it (bs);
4866 }
4867 break;
4868
4869 default:
4870 internal_error (__FILE__, __LINE__,
4871 _("print_bp_stop_message: unrecognized enum value"));
4872 break;
4873 }
4874 }
4875
4876 /* A helper function that prints a shared library stopped event. */
4877
4878 static void
4879 print_solib_event (int is_catchpoint)
4880 {
4881 int any_deleted
4882 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4883 int any_added
4884 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4885
4886 if (!is_catchpoint)
4887 {
4888 if (any_added || any_deleted)
4889 ui_out_text (current_uiout,
4890 _("Stopped due to shared library event:\n"));
4891 else
4892 ui_out_text (current_uiout,
4893 _("Stopped due to shared library event (no "
4894 "libraries added or removed)\n"));
4895 }
4896
4897 if (ui_out_is_mi_like_p (current_uiout))
4898 ui_out_field_string (current_uiout, "reason",
4899 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4900
4901 if (any_deleted)
4902 {
4903 struct cleanup *cleanup;
4904 char *name;
4905 int ix;
4906
4907 ui_out_text (current_uiout, _(" Inferior unloaded "));
4908 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4909 "removed");
4910 for (ix = 0;
4911 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4912 ix, name);
4913 ++ix)
4914 {
4915 if (ix > 0)
4916 ui_out_text (current_uiout, " ");
4917 ui_out_field_string (current_uiout, "library", name);
4918 ui_out_text (current_uiout, "\n");
4919 }
4920
4921 do_cleanups (cleanup);
4922 }
4923
4924 if (any_added)
4925 {
4926 struct so_list *iter;
4927 int ix;
4928 struct cleanup *cleanup;
4929
4930 ui_out_text (current_uiout, _(" Inferior loaded "));
4931 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4932 "added");
4933 for (ix = 0;
4934 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4935 ix, iter);
4936 ++ix)
4937 {
4938 if (ix > 0)
4939 ui_out_text (current_uiout, " ");
4940 ui_out_field_string (current_uiout, "library", iter->so_name);
4941 ui_out_text (current_uiout, "\n");
4942 }
4943
4944 do_cleanups (cleanup);
4945 }
4946 }
4947
4948 /* Print a message indicating what happened. This is called from
4949 normal_stop(). The input to this routine is the head of the bpstat
4950 list - a list of the eventpoints that caused this stop. KIND is
4951 the target_waitkind for the stopping event. This
4952 routine calls the generic print routine for printing a message
4953 about reasons for stopping. This will print (for example) the
4954 "Breakpoint n," part of the output. The return value of this
4955 routine is one of:
4956
4957 PRINT_UNKNOWN: Means we printed nothing.
4958 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4959 code to print the location. An example is
4960 "Breakpoint 1, " which should be followed by
4961 the location.
4962 PRINT_SRC_ONLY: Means we printed something, but there is no need
4963 to also print the location part of the message.
4964 An example is the catch/throw messages, which
4965 don't require a location appended to the end.
4966 PRINT_NOTHING: We have done some printing and we don't need any
4967 further info to be printed. */
4968
4969 enum print_stop_action
4970 bpstat_print (bpstat bs, int kind)
4971 {
4972 enum print_stop_action val;
4973
4974 /* Maybe another breakpoint in the chain caused us to stop.
4975 (Currently all watchpoints go on the bpstat whether hit or not.
4976 That probably could (should) be changed, provided care is taken
4977 with respect to bpstat_explains_signal). */
4978 for (; bs; bs = bs->next)
4979 {
4980 val = print_bp_stop_message (bs);
4981 if (val == PRINT_SRC_ONLY
4982 || val == PRINT_SRC_AND_LOC
4983 || val == PRINT_NOTHING)
4984 return val;
4985 }
4986
4987 /* If we had hit a shared library event breakpoint,
4988 print_bp_stop_message would print out this message. If we hit an
4989 OS-level shared library event, do the same thing. */
4990 if (kind == TARGET_WAITKIND_LOADED)
4991 {
4992 print_solib_event (0);
4993 return PRINT_NOTHING;
4994 }
4995
4996 /* We reached the end of the chain, or we got a null BS to start
4997 with and nothing was printed. */
4998 return PRINT_UNKNOWN;
4999 }
5000
5001 /* Evaluate the expression EXP and return 1 if value is zero.
5002 This returns the inverse of the condition because it is called
5003 from catch_errors which returns 0 if an exception happened, and if an
5004 exception happens we want execution to stop.
5005 The argument is a "struct expression *" that has been cast to a
5006 "void *" to make it pass through catch_errors. */
5007
5008 static int
5009 breakpoint_cond_eval (void *exp)
5010 {
5011 struct value *mark = value_mark ();
5012 int i = !value_true (evaluate_expression ((struct expression *) exp));
5013
5014 value_free_to_mark (mark);
5015 return i;
5016 }
5017
5018 /* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
5019
5020 static bpstat
5021 bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
5022 {
5023 bpstat bs;
5024
5025 bs = (bpstat) xmalloc (sizeof (*bs));
5026 bs->next = NULL;
5027 **bs_link_pointer = bs;
5028 *bs_link_pointer = &bs->next;
5029 bs->breakpoint_at = bl->owner;
5030 bs->bp_location_at = bl;
5031 incref_bp_location (bl);
5032 /* If the condition is false, etc., don't do the commands. */
5033 bs->commands = NULL;
5034 bs->old_val = NULL;
5035 bs->print_it = print_it_normal;
5036 return bs;
5037 }
5038 \f
5039 /* The target has stopped with waitstatus WS. Check if any hardware
5040 watchpoints have triggered, according to the target. */
5041
5042 int
5043 watchpoints_triggered (struct target_waitstatus *ws)
5044 {
5045 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
5046 CORE_ADDR addr;
5047 struct breakpoint *b;
5048
5049 if (!stopped_by_watchpoint)
5050 {
5051 /* We were not stopped by a watchpoint. Mark all watchpoints
5052 as not triggered. */
5053 ALL_BREAKPOINTS (b)
5054 if (is_hardware_watchpoint (b))
5055 {
5056 struct watchpoint *w = (struct watchpoint *) b;
5057
5058 w->watchpoint_triggered = watch_triggered_no;
5059 }
5060
5061 return 0;
5062 }
5063
5064 if (!target_stopped_data_address (&current_target, &addr))
5065 {
5066 /* We were stopped by a watchpoint, but we don't know where.
5067 Mark all watchpoints as unknown. */
5068 ALL_BREAKPOINTS (b)
5069 if (is_hardware_watchpoint (b))
5070 {
5071 struct watchpoint *w = (struct watchpoint *) b;
5072
5073 w->watchpoint_triggered = watch_triggered_unknown;
5074 }
5075
5076 return 1;
5077 }
5078
5079 /* The target could report the data address. Mark watchpoints
5080 affected by this data address as triggered, and all others as not
5081 triggered. */
5082
5083 ALL_BREAKPOINTS (b)
5084 if (is_hardware_watchpoint (b))
5085 {
5086 struct watchpoint *w = (struct watchpoint *) b;
5087 struct bp_location *loc;
5088
5089 w->watchpoint_triggered = watch_triggered_no;
5090 for (loc = b->loc; loc; loc = loc->next)
5091 {
5092 if (is_masked_watchpoint (b))
5093 {
5094 CORE_ADDR newaddr = addr & w->hw_wp_mask;
5095 CORE_ADDR start = loc->address & w->hw_wp_mask;
5096
5097 if (newaddr == start)
5098 {
5099 w->watchpoint_triggered = watch_triggered_yes;
5100 break;
5101 }
5102 }
5103 /* Exact match not required. Within range is sufficient. */
5104 else if (target_watchpoint_addr_within_range (&current_target,
5105 addr, loc->address,
5106 loc->length))
5107 {
5108 w->watchpoint_triggered = watch_triggered_yes;
5109 break;
5110 }
5111 }
5112 }
5113
5114 return 1;
5115 }
5116
5117 /* Possible return values for watchpoint_check (this can't be an enum
5118 because of check_errors). */
5119 /* The watchpoint has been deleted. */
5120 #define WP_DELETED 1
5121 /* The value has changed. */
5122 #define WP_VALUE_CHANGED 2
5123 /* The value has not changed. */
5124 #define WP_VALUE_NOT_CHANGED 3
5125 /* Ignore this watchpoint, no matter if the value changed or not. */
5126 #define WP_IGNORE 4
5127
5128 #define BP_TEMPFLAG 1
5129 #define BP_HARDWAREFLAG 2
5130
5131 /* Evaluate watchpoint condition expression and check if its value
5132 changed.
5133
5134 P should be a pointer to struct bpstat, but is defined as a void *
5135 in order for this function to be usable with catch_errors. */
5136
5137 static int
5138 watchpoint_check (void *p)
5139 {
5140 bpstat bs = (bpstat) p;
5141 struct watchpoint *b;
5142 struct frame_info *fr;
5143 int within_current_scope;
5144
5145 /* BS is built from an existing struct breakpoint. */
5146 gdb_assert (bs->breakpoint_at != NULL);
5147 b = (struct watchpoint *) bs->breakpoint_at;
5148
5149 /* If this is a local watchpoint, we only want to check if the
5150 watchpoint frame is in scope if the current thread is the thread
5151 that was used to create the watchpoint. */
5152 if (!watchpoint_in_thread_scope (b))
5153 return WP_IGNORE;
5154
5155 if (b->exp_valid_block == NULL)
5156 within_current_scope = 1;
5157 else
5158 {
5159 struct frame_info *frame = get_current_frame ();
5160 struct gdbarch *frame_arch = get_frame_arch (frame);
5161 CORE_ADDR frame_pc = get_frame_pc (frame);
5162
5163 /* stack_frame_destroyed_p() returns a non-zero value if we're
5164 still in the function but the stack frame has already been
5165 invalidated. Since we can't rely on the values of local
5166 variables after the stack has been destroyed, we are treating
5167 the watchpoint in that state as `not changed' without further
5168 checking. Don't mark watchpoints as changed if the current
5169 frame is in an epilogue - even if they are in some other
5170 frame, our view of the stack is likely to be wrong and
5171 frame_find_by_id could error out. */
5172 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
5173 return WP_IGNORE;
5174
5175 fr = frame_find_by_id (b->watchpoint_frame);
5176 within_current_scope = (fr != NULL);
5177
5178 /* If we've gotten confused in the unwinder, we might have
5179 returned a frame that can't describe this variable. */
5180 if (within_current_scope)
5181 {
5182 struct symbol *function;
5183
5184 function = get_frame_function (fr);
5185 if (function == NULL
5186 || !contained_in (b->exp_valid_block,
5187 SYMBOL_BLOCK_VALUE (function)))
5188 within_current_scope = 0;
5189 }
5190
5191 if (within_current_scope)
5192 /* If we end up stopping, the current frame will get selected
5193 in normal_stop. So this call to select_frame won't affect
5194 the user. */
5195 select_frame (fr);
5196 }
5197
5198 if (within_current_scope)
5199 {
5200 /* We use value_{,free_to_}mark because it could be a *long*
5201 time before we return to the command level and call
5202 free_all_values. We can't call free_all_values because we
5203 might be in the middle of evaluating a function call. */
5204
5205 int pc = 0;
5206 struct value *mark;
5207 struct value *new_val;
5208
5209 if (is_masked_watchpoint (&b->base))
5210 /* Since we don't know the exact trigger address (from
5211 stopped_data_address), just tell the user we've triggered
5212 a mask watchpoint. */
5213 return WP_VALUE_CHANGED;
5214
5215 mark = value_mark ();
5216 fetch_subexp_value (b->exp.get (), &pc, &new_val, NULL, NULL, 0);
5217
5218 if (b->val_bitsize != 0)
5219 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5220
5221 /* We use value_equal_contents instead of value_equal because
5222 the latter coerces an array to a pointer, thus comparing just
5223 the address of the array instead of its contents. This is
5224 not what we want. */
5225 if ((b->val != NULL) != (new_val != NULL)
5226 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
5227 {
5228 if (new_val != NULL)
5229 {
5230 release_value (new_val);
5231 value_free_to_mark (mark);
5232 }
5233 bs->old_val = b->val;
5234 b->val = new_val;
5235 b->val_valid = 1;
5236 return WP_VALUE_CHANGED;
5237 }
5238 else
5239 {
5240 /* Nothing changed. */
5241 value_free_to_mark (mark);
5242 return WP_VALUE_NOT_CHANGED;
5243 }
5244 }
5245 else
5246 {
5247 /* This seems like the only logical thing to do because
5248 if we temporarily ignored the watchpoint, then when
5249 we reenter the block in which it is valid it contains
5250 garbage (in the case of a function, it may have two
5251 garbage values, one before and one after the prologue).
5252 So we can't even detect the first assignment to it and
5253 watch after that (since the garbage may or may not equal
5254 the first value assigned). */
5255 /* We print all the stop information in
5256 breakpoint_ops->print_it, but in this case, by the time we
5257 call breakpoint_ops->print_it this bp will be deleted
5258 already. So we have no choice but print the information
5259 here. */
5260
5261 SWITCH_THRU_ALL_UIS ()
5262 {
5263 struct ui_out *uiout = current_uiout;
5264
5265 if (ui_out_is_mi_like_p (uiout))
5266 ui_out_field_string
5267 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
5268 ui_out_text (uiout, "\nWatchpoint ");
5269 ui_out_field_int (uiout, "wpnum", b->base.number);
5270 ui_out_text (uiout,
5271 " deleted because the program has left the block in\n"
5272 "which its expression is valid.\n");
5273 }
5274
5275 /* Make sure the watchpoint's commands aren't executed. */
5276 decref_counted_command_line (&b->base.commands);
5277 watchpoint_del_at_next_stop (b);
5278
5279 return WP_DELETED;
5280 }
5281 }
5282
5283 /* Return true if it looks like target has stopped due to hitting
5284 breakpoint location BL. This function does not check if we should
5285 stop, only if BL explains the stop. */
5286
5287 static int
5288 bpstat_check_location (const struct bp_location *bl,
5289 struct address_space *aspace, CORE_ADDR bp_addr,
5290 const struct target_waitstatus *ws)
5291 {
5292 struct breakpoint *b = bl->owner;
5293
5294 /* BL is from an existing breakpoint. */
5295 gdb_assert (b != NULL);
5296
5297 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
5298 }
5299
5300 /* Determine if the watched values have actually changed, and we
5301 should stop. If not, set BS->stop to 0. */
5302
5303 static void
5304 bpstat_check_watchpoint (bpstat bs)
5305 {
5306 const struct bp_location *bl;
5307 struct watchpoint *b;
5308
5309 /* BS is built for existing struct breakpoint. */
5310 bl = bs->bp_location_at;
5311 gdb_assert (bl != NULL);
5312 b = (struct watchpoint *) bs->breakpoint_at;
5313 gdb_assert (b != NULL);
5314
5315 {
5316 int must_check_value = 0;
5317
5318 if (b->base.type == bp_watchpoint)
5319 /* For a software watchpoint, we must always check the
5320 watched value. */
5321 must_check_value = 1;
5322 else if (b->watchpoint_triggered == watch_triggered_yes)
5323 /* We have a hardware watchpoint (read, write, or access)
5324 and the target earlier reported an address watched by
5325 this watchpoint. */
5326 must_check_value = 1;
5327 else if (b->watchpoint_triggered == watch_triggered_unknown
5328 && b->base.type == bp_hardware_watchpoint)
5329 /* We were stopped by a hardware watchpoint, but the target could
5330 not report the data address. We must check the watchpoint's
5331 value. Access and read watchpoints are out of luck; without
5332 a data address, we can't figure it out. */
5333 must_check_value = 1;
5334
5335 if (must_check_value)
5336 {
5337 char *message
5338 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
5339 b->base.number);
5340 struct cleanup *cleanups = make_cleanup (xfree, message);
5341 int e = catch_errors (watchpoint_check, bs, message,
5342 RETURN_MASK_ALL);
5343 do_cleanups (cleanups);
5344 switch (e)
5345 {
5346 case WP_DELETED:
5347 /* We've already printed what needs to be printed. */
5348 bs->print_it = print_it_done;
5349 /* Stop. */
5350 break;
5351 case WP_IGNORE:
5352 bs->print_it = print_it_noop;
5353 bs->stop = 0;
5354 break;
5355 case WP_VALUE_CHANGED:
5356 if (b->base.type == bp_read_watchpoint)
5357 {
5358 /* There are two cases to consider here:
5359
5360 1. We're watching the triggered memory for reads.
5361 In that case, trust the target, and always report
5362 the watchpoint hit to the user. Even though
5363 reads don't cause value changes, the value may
5364 have changed since the last time it was read, and
5365 since we're not trapping writes, we will not see
5366 those, and as such we should ignore our notion of
5367 old value.
5368
5369 2. We're watching the triggered memory for both
5370 reads and writes. There are two ways this may
5371 happen:
5372
5373 2.1. This is a target that can't break on data
5374 reads only, but can break on accesses (reads or
5375 writes), such as e.g., x86. We detect this case
5376 at the time we try to insert read watchpoints.
5377
5378 2.2. Otherwise, the target supports read
5379 watchpoints, but, the user set an access or write
5380 watchpoint watching the same memory as this read
5381 watchpoint.
5382
5383 If we're watching memory writes as well as reads,
5384 ignore watchpoint hits when we find that the
5385 value hasn't changed, as reads don't cause
5386 changes. This still gives false positives when
5387 the program writes the same value to memory as
5388 what there was already in memory (we will confuse
5389 it for a read), but it's much better than
5390 nothing. */
5391
5392 int other_write_watchpoint = 0;
5393
5394 if (bl->watchpoint_type == hw_read)
5395 {
5396 struct breakpoint *other_b;
5397
5398 ALL_BREAKPOINTS (other_b)
5399 if (other_b->type == bp_hardware_watchpoint
5400 || other_b->type == bp_access_watchpoint)
5401 {
5402 struct watchpoint *other_w =
5403 (struct watchpoint *) other_b;
5404
5405 if (other_w->watchpoint_triggered
5406 == watch_triggered_yes)
5407 {
5408 other_write_watchpoint = 1;
5409 break;
5410 }
5411 }
5412 }
5413
5414 if (other_write_watchpoint
5415 || bl->watchpoint_type == hw_access)
5416 {
5417 /* We're watching the same memory for writes,
5418 and the value changed since the last time we
5419 updated it, so this trap must be for a write.
5420 Ignore it. */
5421 bs->print_it = print_it_noop;
5422 bs->stop = 0;
5423 }
5424 }
5425 break;
5426 case WP_VALUE_NOT_CHANGED:
5427 if (b->base.type == bp_hardware_watchpoint
5428 || b->base.type == bp_watchpoint)
5429 {
5430 /* Don't stop: write watchpoints shouldn't fire if
5431 the value hasn't changed. */
5432 bs->print_it = print_it_noop;
5433 bs->stop = 0;
5434 }
5435 /* Stop. */
5436 break;
5437 default:
5438 /* Can't happen. */
5439 case 0:
5440 /* Error from catch_errors. */
5441 {
5442 SWITCH_THRU_ALL_UIS ()
5443 {
5444 printf_filtered (_("Watchpoint %d deleted.\n"),
5445 b->base.number);
5446 }
5447 watchpoint_del_at_next_stop (b);
5448 /* We've already printed what needs to be printed. */
5449 bs->print_it = print_it_done;
5450 }
5451 break;
5452 }
5453 }
5454 else /* must_check_value == 0 */
5455 {
5456 /* This is a case where some watchpoint(s) triggered, but
5457 not at the address of this watchpoint, or else no
5458 watchpoint triggered after all. So don't print
5459 anything for this watchpoint. */
5460 bs->print_it = print_it_noop;
5461 bs->stop = 0;
5462 }
5463 }
5464 }
5465
5466 /* For breakpoints that are currently marked as telling gdb to stop,
5467 check conditions (condition proper, frame, thread and ignore count)
5468 of breakpoint referred to by BS. If we should not stop for this
5469 breakpoint, set BS->stop to 0. */
5470
5471 static void
5472 bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5473 {
5474 const struct bp_location *bl;
5475 struct breakpoint *b;
5476 int value_is_zero = 0;
5477 struct expression *cond;
5478
5479 gdb_assert (bs->stop);
5480
5481 /* BS is built for existing struct breakpoint. */
5482 bl = bs->bp_location_at;
5483 gdb_assert (bl != NULL);
5484 b = bs->breakpoint_at;
5485 gdb_assert (b != NULL);
5486
5487 /* Even if the target evaluated the condition on its end and notified GDB, we
5488 need to do so again since GDB does not know if we stopped due to a
5489 breakpoint or a single step breakpoint. */
5490
5491 if (frame_id_p (b->frame_id)
5492 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
5493 {
5494 bs->stop = 0;
5495 return;
5496 }
5497
5498 /* If this is a thread/task-specific breakpoint, don't waste cpu
5499 evaluating the condition if this isn't the specified
5500 thread/task. */
5501 if ((b->thread != -1 && b->thread != ptid_to_global_thread_id (ptid))
5502 || (b->task != 0 && b->task != ada_get_task_number (ptid)))
5503
5504 {
5505 bs->stop = 0;
5506 return;
5507 }
5508
5509 /* Evaluate extension language breakpoints that have a "stop" method
5510 implemented. */
5511 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
5512
5513 if (is_watchpoint (b))
5514 {
5515 struct watchpoint *w = (struct watchpoint *) b;
5516
5517 cond = w->cond_exp.get ();
5518 }
5519 else
5520 cond = bl->cond.get ();
5521
5522 if (cond && b->disposition != disp_del_at_next_stop)
5523 {
5524 int within_current_scope = 1;
5525 struct watchpoint * w;
5526
5527 /* We use value_mark and value_free_to_mark because it could
5528 be a long time before we return to the command level and
5529 call free_all_values. We can't call free_all_values
5530 because we might be in the middle of evaluating a
5531 function call. */
5532 struct value *mark = value_mark ();
5533
5534 if (is_watchpoint (b))
5535 w = (struct watchpoint *) b;
5536 else
5537 w = NULL;
5538
5539 /* Need to select the frame, with all that implies so that
5540 the conditions will have the right context. Because we
5541 use the frame, we will not see an inlined function's
5542 variables when we arrive at a breakpoint at the start
5543 of the inlined function; the current frame will be the
5544 call site. */
5545 if (w == NULL || w->cond_exp_valid_block == NULL)
5546 select_frame (get_current_frame ());
5547 else
5548 {
5549 struct frame_info *frame;
5550
5551 /* For local watchpoint expressions, which particular
5552 instance of a local is being watched matters, so we
5553 keep track of the frame to evaluate the expression
5554 in. To evaluate the condition however, it doesn't
5555 really matter which instantiation of the function
5556 where the condition makes sense triggers the
5557 watchpoint. This allows an expression like "watch
5558 global if q > 10" set in `func', catch writes to
5559 global on all threads that call `func', or catch
5560 writes on all recursive calls of `func' by a single
5561 thread. We simply always evaluate the condition in
5562 the innermost frame that's executing where it makes
5563 sense to evaluate the condition. It seems
5564 intuitive. */
5565 frame = block_innermost_frame (w->cond_exp_valid_block);
5566 if (frame != NULL)
5567 select_frame (frame);
5568 else
5569 within_current_scope = 0;
5570 }
5571 if (within_current_scope)
5572 value_is_zero
5573 = catch_errors (breakpoint_cond_eval, cond,
5574 "Error in testing breakpoint condition:\n",
5575 RETURN_MASK_ALL);
5576 else
5577 {
5578 warning (_("Watchpoint condition cannot be tested "
5579 "in the current scope"));
5580 /* If we failed to set the right context for this
5581 watchpoint, unconditionally report it. */
5582 value_is_zero = 0;
5583 }
5584 /* FIXME-someday, should give breakpoint #. */
5585 value_free_to_mark (mark);
5586 }
5587
5588 if (cond && value_is_zero)
5589 {
5590 bs->stop = 0;
5591 }
5592 else if (b->ignore_count > 0)
5593 {
5594 b->ignore_count--;
5595 bs->stop = 0;
5596 /* Increase the hit count even though we don't stop. */
5597 ++(b->hit_count);
5598 observer_notify_breakpoint_modified (b);
5599 }
5600 }
5601
5602 /* Returns true if we need to track moribund locations of LOC's type
5603 on the current target. */
5604
5605 static int
5606 need_moribund_for_location_type (struct bp_location *loc)
5607 {
5608 return ((loc->loc_type == bp_loc_software_breakpoint
5609 && !target_supports_stopped_by_sw_breakpoint ())
5610 || (loc->loc_type == bp_loc_hardware_breakpoint
5611 && !target_supports_stopped_by_hw_breakpoint ()));
5612 }
5613
5614
5615 /* Get a bpstat associated with having just stopped at address
5616 BP_ADDR in thread PTID.
5617
5618 Determine whether we stopped at a breakpoint, etc, or whether we
5619 don't understand this stop. Result is a chain of bpstat's such
5620 that:
5621
5622 if we don't understand the stop, the result is a null pointer.
5623
5624 if we understand why we stopped, the result is not null.
5625
5626 Each element of the chain refers to a particular breakpoint or
5627 watchpoint at which we have stopped. (We may have stopped for
5628 several reasons concurrently.)
5629
5630 Each element of the chain has valid next, breakpoint_at,
5631 commands, FIXME??? fields. */
5632
5633 bpstat
5634 bpstat_stop_status (struct address_space *aspace,
5635 CORE_ADDR bp_addr, ptid_t ptid,
5636 const struct target_waitstatus *ws)
5637 {
5638 struct breakpoint *b = NULL;
5639 struct bp_location *bl;
5640 struct bp_location *loc;
5641 /* First item of allocated bpstat's. */
5642 bpstat bs_head = NULL, *bs_link = &bs_head;
5643 /* Pointer to the last thing in the chain currently. */
5644 bpstat bs;
5645 int ix;
5646 int need_remove_insert;
5647 int removed_any;
5648
5649 /* First, build the bpstat chain with locations that explain a
5650 target stop, while being careful to not set the target running,
5651 as that may invalidate locations (in particular watchpoint
5652 locations are recreated). Resuming will happen here with
5653 breakpoint conditions or watchpoint expressions that include
5654 inferior function calls. */
5655
5656 ALL_BREAKPOINTS (b)
5657 {
5658 if (!breakpoint_enabled (b))
5659 continue;
5660
5661 for (bl = b->loc; bl != NULL; bl = bl->next)
5662 {
5663 /* For hardware watchpoints, we look only at the first
5664 location. The watchpoint_check function will work on the
5665 entire expression, not the individual locations. For
5666 read watchpoints, the watchpoints_triggered function has
5667 checked all locations already. */
5668 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5669 break;
5670
5671 if (!bl->enabled || bl->shlib_disabled)
5672 continue;
5673
5674 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
5675 continue;
5676
5677 /* Come here if it's a watchpoint, or if the break address
5678 matches. */
5679
5680 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
5681 explain stop. */
5682
5683 /* Assume we stop. Should we find a watchpoint that is not
5684 actually triggered, or if the condition of the breakpoint
5685 evaluates as false, we'll reset 'stop' to 0. */
5686 bs->stop = 1;
5687 bs->print = 1;
5688
5689 /* If this is a scope breakpoint, mark the associated
5690 watchpoint as triggered so that we will handle the
5691 out-of-scope event. We'll get to the watchpoint next
5692 iteration. */
5693 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
5694 {
5695 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5696
5697 w->watchpoint_triggered = watch_triggered_yes;
5698 }
5699 }
5700 }
5701
5702 /* Check if a moribund breakpoint explains the stop. */
5703 if (!target_supports_stopped_by_sw_breakpoint ()
5704 || !target_supports_stopped_by_hw_breakpoint ())
5705 {
5706 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
5707 {
5708 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5709 && need_moribund_for_location_type (loc))
5710 {
5711 bs = bpstat_alloc (loc, &bs_link);
5712 /* For hits of moribund locations, we should just proceed. */
5713 bs->stop = 0;
5714 bs->print = 0;
5715 bs->print_it = print_it_noop;
5716 }
5717 }
5718 }
5719
5720 /* A bit of special processing for shlib breakpoints. We need to
5721 process solib loading here, so that the lists of loaded and
5722 unloaded libraries are correct before we handle "catch load" and
5723 "catch unload". */
5724 for (bs = bs_head; bs != NULL; bs = bs->next)
5725 {
5726 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
5727 {
5728 handle_solib_event ();
5729 break;
5730 }
5731 }
5732
5733 /* Now go through the locations that caused the target to stop, and
5734 check whether we're interested in reporting this stop to higher
5735 layers, or whether we should resume the target transparently. */
5736
5737 removed_any = 0;
5738
5739 for (bs = bs_head; bs != NULL; bs = bs->next)
5740 {
5741 if (!bs->stop)
5742 continue;
5743
5744 b = bs->breakpoint_at;
5745 b->ops->check_status (bs);
5746 if (bs->stop)
5747 {
5748 bpstat_check_breakpoint_conditions (bs, ptid);
5749
5750 if (bs->stop)
5751 {
5752 ++(b->hit_count);
5753 observer_notify_breakpoint_modified (b);
5754
5755 /* We will stop here. */
5756 if (b->disposition == disp_disable)
5757 {
5758 --(b->enable_count);
5759 if (b->enable_count <= 0)
5760 b->enable_state = bp_disabled;
5761 removed_any = 1;
5762 }
5763 if (b->silent)
5764 bs->print = 0;
5765 bs->commands = b->commands;
5766 incref_counted_command_line (bs->commands);
5767 if (command_line_is_silent (bs->commands
5768 ? bs->commands->commands : NULL))
5769 bs->print = 0;
5770
5771 b->ops->after_condition_true (bs);
5772 }
5773
5774 }
5775
5776 /* Print nothing for this entry if we don't stop or don't
5777 print. */
5778 if (!bs->stop || !bs->print)
5779 bs->print_it = print_it_noop;
5780 }
5781
5782 /* If we aren't stopping, the value of some hardware watchpoint may
5783 not have changed, but the intermediate memory locations we are
5784 watching may have. Don't bother if we're stopping; this will get
5785 done later. */
5786 need_remove_insert = 0;
5787 if (! bpstat_causes_stop (bs_head))
5788 for (bs = bs_head; bs != NULL; bs = bs->next)
5789 if (!bs->stop
5790 && bs->breakpoint_at
5791 && is_hardware_watchpoint (bs->breakpoint_at))
5792 {
5793 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5794
5795 update_watchpoint (w, 0 /* don't reparse. */);
5796 need_remove_insert = 1;
5797 }
5798
5799 if (need_remove_insert)
5800 update_global_location_list (UGLL_MAY_INSERT);
5801 else if (removed_any)
5802 update_global_location_list (UGLL_DONT_INSERT);
5803
5804 return bs_head;
5805 }
5806
5807 static void
5808 handle_jit_event (void)
5809 {
5810 struct frame_info *frame;
5811 struct gdbarch *gdbarch;
5812
5813 if (debug_infrun)
5814 fprintf_unfiltered (gdb_stdlog, "handling bp_jit_event\n");
5815
5816 /* Switch terminal for any messages produced by
5817 breakpoint_re_set. */
5818 target_terminal_ours_for_output ();
5819
5820 frame = get_current_frame ();
5821 gdbarch = get_frame_arch (frame);
5822
5823 jit_event_handler (gdbarch);
5824
5825 target_terminal_inferior ();
5826 }
5827
5828 /* Prepare WHAT final decision for infrun. */
5829
5830 /* Decide what infrun needs to do with this bpstat. */
5831
5832 struct bpstat_what
5833 bpstat_what (bpstat bs_head)
5834 {
5835 struct bpstat_what retval;
5836 bpstat bs;
5837
5838 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
5839 retval.call_dummy = STOP_NONE;
5840 retval.is_longjmp = 0;
5841
5842 for (bs = bs_head; bs != NULL; bs = bs->next)
5843 {
5844 /* Extract this BS's action. After processing each BS, we check
5845 if its action overrides all we've seem so far. */
5846 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5847 enum bptype bptype;
5848
5849 if (bs->breakpoint_at == NULL)
5850 {
5851 /* I suspect this can happen if it was a momentary
5852 breakpoint which has since been deleted. */
5853 bptype = bp_none;
5854 }
5855 else
5856 bptype = bs->breakpoint_at->type;
5857
5858 switch (bptype)
5859 {
5860 case bp_none:
5861 break;
5862 case bp_breakpoint:
5863 case bp_hardware_breakpoint:
5864 case bp_single_step:
5865 case bp_until:
5866 case bp_finish:
5867 case bp_shlib_event:
5868 if (bs->stop)
5869 {
5870 if (bs->print)
5871 this_action = BPSTAT_WHAT_STOP_NOISY;
5872 else
5873 this_action = BPSTAT_WHAT_STOP_SILENT;
5874 }
5875 else
5876 this_action = BPSTAT_WHAT_SINGLE;
5877 break;
5878 case bp_watchpoint:
5879 case bp_hardware_watchpoint:
5880 case bp_read_watchpoint:
5881 case bp_access_watchpoint:
5882 if (bs->stop)
5883 {
5884 if (bs->print)
5885 this_action = BPSTAT_WHAT_STOP_NOISY;
5886 else
5887 this_action = BPSTAT_WHAT_STOP_SILENT;
5888 }
5889 else
5890 {
5891 /* There was a watchpoint, but we're not stopping.
5892 This requires no further action. */
5893 }
5894 break;
5895 case bp_longjmp:
5896 case bp_longjmp_call_dummy:
5897 case bp_exception:
5898 if (bs->stop)
5899 {
5900 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5901 retval.is_longjmp = bptype != bp_exception;
5902 }
5903 else
5904 this_action = BPSTAT_WHAT_SINGLE;
5905 break;
5906 case bp_longjmp_resume:
5907 case bp_exception_resume:
5908 if (bs->stop)
5909 {
5910 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5911 retval.is_longjmp = bptype == bp_longjmp_resume;
5912 }
5913 else
5914 this_action = BPSTAT_WHAT_SINGLE;
5915 break;
5916 case bp_step_resume:
5917 if (bs->stop)
5918 this_action = BPSTAT_WHAT_STEP_RESUME;
5919 else
5920 {
5921 /* It is for the wrong frame. */
5922 this_action = BPSTAT_WHAT_SINGLE;
5923 }
5924 break;
5925 case bp_hp_step_resume:
5926 if (bs->stop)
5927 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5928 else
5929 {
5930 /* It is for the wrong frame. */
5931 this_action = BPSTAT_WHAT_SINGLE;
5932 }
5933 break;
5934 case bp_watchpoint_scope:
5935 case bp_thread_event:
5936 case bp_overlay_event:
5937 case bp_longjmp_master:
5938 case bp_std_terminate_master:
5939 case bp_exception_master:
5940 this_action = BPSTAT_WHAT_SINGLE;
5941 break;
5942 case bp_catchpoint:
5943 if (bs->stop)
5944 {
5945 if (bs->print)
5946 this_action = BPSTAT_WHAT_STOP_NOISY;
5947 else
5948 this_action = BPSTAT_WHAT_STOP_SILENT;
5949 }
5950 else
5951 {
5952 /* There was a catchpoint, but we're not stopping.
5953 This requires no further action. */
5954 }
5955 break;
5956 case bp_jit_event:
5957 this_action = BPSTAT_WHAT_SINGLE;
5958 break;
5959 case bp_call_dummy:
5960 /* Make sure the action is stop (silent or noisy),
5961 so infrun.c pops the dummy frame. */
5962 retval.call_dummy = STOP_STACK_DUMMY;
5963 this_action = BPSTAT_WHAT_STOP_SILENT;
5964 break;
5965 case bp_std_terminate:
5966 /* Make sure the action is stop (silent or noisy),
5967 so infrun.c pops the dummy frame. */
5968 retval.call_dummy = STOP_STD_TERMINATE;
5969 this_action = BPSTAT_WHAT_STOP_SILENT;
5970 break;
5971 case bp_tracepoint:
5972 case bp_fast_tracepoint:
5973 case bp_static_tracepoint:
5974 /* Tracepoint hits should not be reported back to GDB, and
5975 if one got through somehow, it should have been filtered
5976 out already. */
5977 internal_error (__FILE__, __LINE__,
5978 _("bpstat_what: tracepoint encountered"));
5979 break;
5980 case bp_gnu_ifunc_resolver:
5981 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5982 this_action = BPSTAT_WHAT_SINGLE;
5983 break;
5984 case bp_gnu_ifunc_resolver_return:
5985 /* The breakpoint will be removed, execution will restart from the
5986 PC of the former breakpoint. */
5987 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5988 break;
5989
5990 case bp_dprintf:
5991 if (bs->stop)
5992 this_action = BPSTAT_WHAT_STOP_SILENT;
5993 else
5994 this_action = BPSTAT_WHAT_SINGLE;
5995 break;
5996
5997 default:
5998 internal_error (__FILE__, __LINE__,
5999 _("bpstat_what: unhandled bptype %d"), (int) bptype);
6000 }
6001
6002 retval.main_action = std::max (retval.main_action, this_action);
6003 }
6004
6005 return retval;
6006 }
6007
6008 void
6009 bpstat_run_callbacks (bpstat bs_head)
6010 {
6011 bpstat bs;
6012
6013 for (bs = bs_head; bs != NULL; bs = bs->next)
6014 {
6015 struct breakpoint *b = bs->breakpoint_at;
6016
6017 if (b == NULL)
6018 continue;
6019 switch (b->type)
6020 {
6021 case bp_jit_event:
6022 handle_jit_event ();
6023 break;
6024 case bp_gnu_ifunc_resolver:
6025 gnu_ifunc_resolver_stop (b);
6026 break;
6027 case bp_gnu_ifunc_resolver_return:
6028 gnu_ifunc_resolver_return_stop (b);
6029 break;
6030 }
6031 }
6032 }
6033
6034 /* Nonzero if we should step constantly (e.g. watchpoints on machines
6035 without hardware support). This isn't related to a specific bpstat,
6036 just to things like whether watchpoints are set. */
6037
6038 int
6039 bpstat_should_step (void)
6040 {
6041 struct breakpoint *b;
6042
6043 ALL_BREAKPOINTS (b)
6044 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
6045 return 1;
6046 return 0;
6047 }
6048
6049 int
6050 bpstat_causes_stop (bpstat bs)
6051 {
6052 for (; bs != NULL; bs = bs->next)
6053 if (bs->stop)
6054 return 1;
6055
6056 return 0;
6057 }
6058
6059 \f
6060
6061 /* Compute a string of spaces suitable to indent the next line
6062 so it starts at the position corresponding to the table column
6063 named COL_NAME in the currently active table of UIOUT. */
6064
6065 static char *
6066 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
6067 {
6068 static char wrap_indent[80];
6069 int i, total_width, width, align;
6070 char *text;
6071
6072 total_width = 0;
6073 for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
6074 {
6075 if (strcmp (text, col_name) == 0)
6076 {
6077 gdb_assert (total_width < sizeof wrap_indent);
6078 memset (wrap_indent, ' ', total_width);
6079 wrap_indent[total_width] = 0;
6080
6081 return wrap_indent;
6082 }
6083
6084 total_width += width + 1;
6085 }
6086
6087 return NULL;
6088 }
6089
6090 /* Determine if the locations of this breakpoint will have their conditions
6091 evaluated by the target, host or a mix of both. Returns the following:
6092
6093 "host": Host evals condition.
6094 "host or target": Host or Target evals condition.
6095 "target": Target evals condition.
6096 */
6097
6098 static const char *
6099 bp_condition_evaluator (struct breakpoint *b)
6100 {
6101 struct bp_location *bl;
6102 char host_evals = 0;
6103 char target_evals = 0;
6104
6105 if (!b)
6106 return NULL;
6107
6108 if (!is_breakpoint (b))
6109 return NULL;
6110
6111 if (gdb_evaluates_breakpoint_condition_p ()
6112 || !target_supports_evaluation_of_breakpoint_conditions ())
6113 return condition_evaluation_host;
6114
6115 for (bl = b->loc; bl; bl = bl->next)
6116 {
6117 if (bl->cond_bytecode)
6118 target_evals++;
6119 else
6120 host_evals++;
6121 }
6122
6123 if (host_evals && target_evals)
6124 return condition_evaluation_both;
6125 else if (target_evals)
6126 return condition_evaluation_target;
6127 else
6128 return condition_evaluation_host;
6129 }
6130
6131 /* Determine the breakpoint location's condition evaluator. This is
6132 similar to bp_condition_evaluator, but for locations. */
6133
6134 static const char *
6135 bp_location_condition_evaluator (struct bp_location *bl)
6136 {
6137 if (bl && !is_breakpoint (bl->owner))
6138 return NULL;
6139
6140 if (gdb_evaluates_breakpoint_condition_p ()
6141 || !target_supports_evaluation_of_breakpoint_conditions ())
6142 return condition_evaluation_host;
6143
6144 if (bl && bl->cond_bytecode)
6145 return condition_evaluation_target;
6146 else
6147 return condition_evaluation_host;
6148 }
6149
6150 /* Print the LOC location out of the list of B->LOC locations. */
6151
6152 static void
6153 print_breakpoint_location (struct breakpoint *b,
6154 struct bp_location *loc)
6155 {
6156 struct ui_out *uiout = current_uiout;
6157 struct cleanup *old_chain = save_current_program_space ();
6158
6159 if (loc != NULL && loc->shlib_disabled)
6160 loc = NULL;
6161
6162 if (loc != NULL)
6163 set_current_program_space (loc->pspace);
6164
6165 if (b->display_canonical)
6166 ui_out_field_string (uiout, "what",
6167 event_location_to_string (b->location));
6168 else if (loc && loc->symtab)
6169 {
6170 struct symbol *sym
6171 = find_pc_sect_function (loc->address, loc->section);
6172 if (sym)
6173 {
6174 ui_out_text (uiout, "in ");
6175 ui_out_field_string (uiout, "func",
6176 SYMBOL_PRINT_NAME (sym));
6177 ui_out_text (uiout, " ");
6178 ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
6179 ui_out_text (uiout, "at ");
6180 }
6181 ui_out_field_string (uiout, "file",
6182 symtab_to_filename_for_display (loc->symtab));
6183 ui_out_text (uiout, ":");
6184
6185 if (ui_out_is_mi_like_p (uiout))
6186 ui_out_field_string (uiout, "fullname",
6187 symtab_to_fullname (loc->symtab));
6188
6189 ui_out_field_int (uiout, "line", loc->line_number);
6190 }
6191 else if (loc)
6192 {
6193 struct ui_file *stb = mem_fileopen ();
6194 struct cleanup *stb_chain = make_cleanup_ui_file_delete (stb);
6195
6196 print_address_symbolic (loc->gdbarch, loc->address, stb,
6197 demangle, "");
6198 ui_out_field_stream (uiout, "at", stb);
6199
6200 do_cleanups (stb_chain);
6201 }
6202 else
6203 {
6204 ui_out_field_string (uiout, "pending",
6205 event_location_to_string (b->location));
6206 /* If extra_string is available, it could be holding a condition
6207 or dprintf arguments. In either case, make sure it is printed,
6208 too, but only for non-MI streams. */
6209 if (!ui_out_is_mi_like_p (uiout) && b->extra_string != NULL)
6210 {
6211 if (b->type == bp_dprintf)
6212 ui_out_text (uiout, ",");
6213 else
6214 ui_out_text (uiout, " ");
6215 ui_out_text (uiout, b->extra_string);
6216 }
6217 }
6218
6219 if (loc && is_breakpoint (b)
6220 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6221 && bp_condition_evaluator (b) == condition_evaluation_both)
6222 {
6223 ui_out_text (uiout, " (");
6224 ui_out_field_string (uiout, "evaluated-by",
6225 bp_location_condition_evaluator (loc));
6226 ui_out_text (uiout, ")");
6227 }
6228
6229 do_cleanups (old_chain);
6230 }
6231
6232 static const char *
6233 bptype_string (enum bptype type)
6234 {
6235 struct ep_type_description
6236 {
6237 enum bptype type;
6238 char *description;
6239 };
6240 static struct ep_type_description bptypes[] =
6241 {
6242 {bp_none, "?deleted?"},
6243 {bp_breakpoint, "breakpoint"},
6244 {bp_hardware_breakpoint, "hw breakpoint"},
6245 {bp_single_step, "sw single-step"},
6246 {bp_until, "until"},
6247 {bp_finish, "finish"},
6248 {bp_watchpoint, "watchpoint"},
6249 {bp_hardware_watchpoint, "hw watchpoint"},
6250 {bp_read_watchpoint, "read watchpoint"},
6251 {bp_access_watchpoint, "acc watchpoint"},
6252 {bp_longjmp, "longjmp"},
6253 {bp_longjmp_resume, "longjmp resume"},
6254 {bp_longjmp_call_dummy, "longjmp for call dummy"},
6255 {bp_exception, "exception"},
6256 {bp_exception_resume, "exception resume"},
6257 {bp_step_resume, "step resume"},
6258 {bp_hp_step_resume, "high-priority step resume"},
6259 {bp_watchpoint_scope, "watchpoint scope"},
6260 {bp_call_dummy, "call dummy"},
6261 {bp_std_terminate, "std::terminate"},
6262 {bp_shlib_event, "shlib events"},
6263 {bp_thread_event, "thread events"},
6264 {bp_overlay_event, "overlay events"},
6265 {bp_longjmp_master, "longjmp master"},
6266 {bp_std_terminate_master, "std::terminate master"},
6267 {bp_exception_master, "exception master"},
6268 {bp_catchpoint, "catchpoint"},
6269 {bp_tracepoint, "tracepoint"},
6270 {bp_fast_tracepoint, "fast tracepoint"},
6271 {bp_static_tracepoint, "static tracepoint"},
6272 {bp_dprintf, "dprintf"},
6273 {bp_jit_event, "jit events"},
6274 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6275 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
6276 };
6277
6278 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6279 || ((int) type != bptypes[(int) type].type))
6280 internal_error (__FILE__, __LINE__,
6281 _("bptypes table does not describe type #%d."),
6282 (int) type);
6283
6284 return bptypes[(int) type].description;
6285 }
6286
6287 /* For MI, output a field named 'thread-groups' with a list as the value.
6288 For CLI, prefix the list with the string 'inf'. */
6289
6290 static void
6291 output_thread_groups (struct ui_out *uiout,
6292 const char *field_name,
6293 VEC(int) *inf_num,
6294 int mi_only)
6295 {
6296 struct cleanup *back_to;
6297 int is_mi = ui_out_is_mi_like_p (uiout);
6298 int inf;
6299 int i;
6300
6301 /* For backward compatibility, don't display inferiors in CLI unless
6302 there are several. Always display them for MI. */
6303 if (!is_mi && mi_only)
6304 return;
6305
6306 back_to = make_cleanup_ui_out_list_begin_end (uiout, field_name);
6307
6308 for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
6309 {
6310 if (is_mi)
6311 {
6312 char mi_group[10];
6313
6314 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
6315 ui_out_field_string (uiout, NULL, mi_group);
6316 }
6317 else
6318 {
6319 if (i == 0)
6320 ui_out_text (uiout, " inf ");
6321 else
6322 ui_out_text (uiout, ", ");
6323
6324 ui_out_text (uiout, plongest (inf));
6325 }
6326 }
6327
6328 do_cleanups (back_to);
6329 }
6330
6331 /* Print B to gdb_stdout. */
6332
6333 static void
6334 print_one_breakpoint_location (struct breakpoint *b,
6335 struct bp_location *loc,
6336 int loc_number,
6337 struct bp_location **last_loc,
6338 int allflag)
6339 {
6340 struct command_line *l;
6341 static char bpenables[] = "nynny";
6342
6343 struct ui_out *uiout = current_uiout;
6344 int header_of_multiple = 0;
6345 int part_of_multiple = (loc != NULL);
6346 struct value_print_options opts;
6347
6348 get_user_print_options (&opts);
6349
6350 gdb_assert (!loc || loc_number != 0);
6351 /* See comment in print_one_breakpoint concerning treatment of
6352 breakpoints with single disabled location. */
6353 if (loc == NULL
6354 && (b->loc != NULL
6355 && (b->loc->next != NULL || !b->loc->enabled)))
6356 header_of_multiple = 1;
6357 if (loc == NULL)
6358 loc = b->loc;
6359
6360 annotate_record ();
6361
6362 /* 1 */
6363 annotate_field (0);
6364 if (part_of_multiple)
6365 {
6366 char *formatted;
6367 formatted = xstrprintf ("%d.%d", b->number, loc_number);
6368 ui_out_field_string (uiout, "number", formatted);
6369 xfree (formatted);
6370 }
6371 else
6372 {
6373 ui_out_field_int (uiout, "number", b->number);
6374 }
6375
6376 /* 2 */
6377 annotate_field (1);
6378 if (part_of_multiple)
6379 ui_out_field_skip (uiout, "type");
6380 else
6381 ui_out_field_string (uiout, "type", bptype_string (b->type));
6382
6383 /* 3 */
6384 annotate_field (2);
6385 if (part_of_multiple)
6386 ui_out_field_skip (uiout, "disp");
6387 else
6388 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
6389
6390
6391 /* 4 */
6392 annotate_field (3);
6393 if (part_of_multiple)
6394 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
6395 else
6396 ui_out_field_fmt (uiout, "enabled", "%c",
6397 bpenables[(int) b->enable_state]);
6398 ui_out_spaces (uiout, 2);
6399
6400
6401 /* 5 and 6 */
6402 if (b->ops != NULL && b->ops->print_one != NULL)
6403 {
6404 /* Although the print_one can possibly print all locations,
6405 calling it here is not likely to get any nice result. So,
6406 make sure there's just one location. */
6407 gdb_assert (b->loc == NULL || b->loc->next == NULL);
6408 b->ops->print_one (b, last_loc);
6409 }
6410 else
6411 switch (b->type)
6412 {
6413 case bp_none:
6414 internal_error (__FILE__, __LINE__,
6415 _("print_one_breakpoint: bp_none encountered\n"));
6416 break;
6417
6418 case bp_watchpoint:
6419 case bp_hardware_watchpoint:
6420 case bp_read_watchpoint:
6421 case bp_access_watchpoint:
6422 {
6423 struct watchpoint *w = (struct watchpoint *) b;
6424
6425 /* Field 4, the address, is omitted (which makes the columns
6426 not line up too nicely with the headers, but the effect
6427 is relatively readable). */
6428 if (opts.addressprint)
6429 ui_out_field_skip (uiout, "addr");
6430 annotate_field (5);
6431 ui_out_field_string (uiout, "what", w->exp_string);
6432 }
6433 break;
6434
6435 case bp_breakpoint:
6436 case bp_hardware_breakpoint:
6437 case bp_single_step:
6438 case bp_until:
6439 case bp_finish:
6440 case bp_longjmp:
6441 case bp_longjmp_resume:
6442 case bp_longjmp_call_dummy:
6443 case bp_exception:
6444 case bp_exception_resume:
6445 case bp_step_resume:
6446 case bp_hp_step_resume:
6447 case bp_watchpoint_scope:
6448 case bp_call_dummy:
6449 case bp_std_terminate:
6450 case bp_shlib_event:
6451 case bp_thread_event:
6452 case bp_overlay_event:
6453 case bp_longjmp_master:
6454 case bp_std_terminate_master:
6455 case bp_exception_master:
6456 case bp_tracepoint:
6457 case bp_fast_tracepoint:
6458 case bp_static_tracepoint:
6459 case bp_dprintf:
6460 case bp_jit_event:
6461 case bp_gnu_ifunc_resolver:
6462 case bp_gnu_ifunc_resolver_return:
6463 if (opts.addressprint)
6464 {
6465 annotate_field (4);
6466 if (header_of_multiple)
6467 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
6468 else if (b->loc == NULL || loc->shlib_disabled)
6469 ui_out_field_string (uiout, "addr", "<PENDING>");
6470 else
6471 ui_out_field_core_addr (uiout, "addr",
6472 loc->gdbarch, loc->address);
6473 }
6474 annotate_field (5);
6475 if (!header_of_multiple)
6476 print_breakpoint_location (b, loc);
6477 if (b->loc)
6478 *last_loc = b->loc;
6479 break;
6480 }
6481
6482
6483 if (loc != NULL && !header_of_multiple)
6484 {
6485 struct inferior *inf;
6486 VEC(int) *inf_num = NULL;
6487 int mi_only = 1;
6488
6489 ALL_INFERIORS (inf)
6490 {
6491 if (inf->pspace == loc->pspace)
6492 VEC_safe_push (int, inf_num, inf->num);
6493 }
6494
6495 /* For backward compatibility, don't display inferiors in CLI unless
6496 there are several. Always display for MI. */
6497 if (allflag
6498 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6499 && (number_of_program_spaces () > 1
6500 || number_of_inferiors () > 1)
6501 /* LOC is for existing B, it cannot be in
6502 moribund_locations and thus having NULL OWNER. */
6503 && loc->owner->type != bp_catchpoint))
6504 mi_only = 0;
6505 output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6506 VEC_free (int, inf_num);
6507 }
6508
6509 if (!part_of_multiple)
6510 {
6511 if (b->thread != -1)
6512 {
6513 /* FIXME: This seems to be redundant and lost here; see the
6514 "stop only in" line a little further down. */
6515 ui_out_text (uiout, " thread ");
6516 ui_out_field_int (uiout, "thread", b->thread);
6517 }
6518 else if (b->task != 0)
6519 {
6520 ui_out_text (uiout, " task ");
6521 ui_out_field_int (uiout, "task", b->task);
6522 }
6523 }
6524
6525 ui_out_text (uiout, "\n");
6526
6527 if (!part_of_multiple)
6528 b->ops->print_one_detail (b, uiout);
6529
6530 if (part_of_multiple && frame_id_p (b->frame_id))
6531 {
6532 annotate_field (6);
6533 ui_out_text (uiout, "\tstop only in stack frame at ");
6534 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
6535 the frame ID. */
6536 ui_out_field_core_addr (uiout, "frame",
6537 b->gdbarch, b->frame_id.stack_addr);
6538 ui_out_text (uiout, "\n");
6539 }
6540
6541 if (!part_of_multiple && b->cond_string)
6542 {
6543 annotate_field (7);
6544 if (is_tracepoint (b))
6545 ui_out_text (uiout, "\ttrace only if ");
6546 else
6547 ui_out_text (uiout, "\tstop only if ");
6548 ui_out_field_string (uiout, "cond", b->cond_string);
6549
6550 /* Print whether the target is doing the breakpoint's condition
6551 evaluation. If GDB is doing the evaluation, don't print anything. */
6552 if (is_breakpoint (b)
6553 && breakpoint_condition_evaluation_mode ()
6554 == condition_evaluation_target)
6555 {
6556 ui_out_text (uiout, " (");
6557 ui_out_field_string (uiout, "evaluated-by",
6558 bp_condition_evaluator (b));
6559 ui_out_text (uiout, " evals)");
6560 }
6561 ui_out_text (uiout, "\n");
6562 }
6563
6564 if (!part_of_multiple && b->thread != -1)
6565 {
6566 /* FIXME should make an annotation for this. */
6567 ui_out_text (uiout, "\tstop only in thread ");
6568 if (ui_out_is_mi_like_p (uiout))
6569 ui_out_field_int (uiout, "thread", b->thread);
6570 else
6571 {
6572 struct thread_info *thr = find_thread_global_id (b->thread);
6573
6574 ui_out_field_string (uiout, "thread", print_thread_id (thr));
6575 }
6576 ui_out_text (uiout, "\n");
6577 }
6578
6579 if (!part_of_multiple)
6580 {
6581 if (b->hit_count)
6582 {
6583 /* FIXME should make an annotation for this. */
6584 if (is_catchpoint (b))
6585 ui_out_text (uiout, "\tcatchpoint");
6586 else if (is_tracepoint (b))
6587 ui_out_text (uiout, "\ttracepoint");
6588 else
6589 ui_out_text (uiout, "\tbreakpoint");
6590 ui_out_text (uiout, " already hit ");
6591 ui_out_field_int (uiout, "times", b->hit_count);
6592 if (b->hit_count == 1)
6593 ui_out_text (uiout, " time\n");
6594 else
6595 ui_out_text (uiout, " times\n");
6596 }
6597 else
6598 {
6599 /* Output the count also if it is zero, but only if this is mi. */
6600 if (ui_out_is_mi_like_p (uiout))
6601 ui_out_field_int (uiout, "times", b->hit_count);
6602 }
6603 }
6604
6605 if (!part_of_multiple && b->ignore_count)
6606 {
6607 annotate_field (8);
6608 ui_out_text (uiout, "\tignore next ");
6609 ui_out_field_int (uiout, "ignore", b->ignore_count);
6610 ui_out_text (uiout, " hits\n");
6611 }
6612
6613 /* Note that an enable count of 1 corresponds to "enable once"
6614 behavior, which is reported by the combination of enablement and
6615 disposition, so we don't need to mention it here. */
6616 if (!part_of_multiple && b->enable_count > 1)
6617 {
6618 annotate_field (8);
6619 ui_out_text (uiout, "\tdisable after ");
6620 /* Tweak the wording to clarify that ignore and enable counts
6621 are distinct, and have additive effect. */
6622 if (b->ignore_count)
6623 ui_out_text (uiout, "additional ");
6624 else
6625 ui_out_text (uiout, "next ");
6626 ui_out_field_int (uiout, "enable", b->enable_count);
6627 ui_out_text (uiout, " hits\n");
6628 }
6629
6630 if (!part_of_multiple && is_tracepoint (b))
6631 {
6632 struct tracepoint *tp = (struct tracepoint *) b;
6633
6634 if (tp->traceframe_usage)
6635 {
6636 ui_out_text (uiout, "\ttrace buffer usage ");
6637 ui_out_field_int (uiout, "traceframe-usage", tp->traceframe_usage);
6638 ui_out_text (uiout, " bytes\n");
6639 }
6640 }
6641
6642 l = b->commands ? b->commands->commands : NULL;
6643 if (!part_of_multiple && l)
6644 {
6645 struct cleanup *script_chain;
6646
6647 annotate_field (9);
6648 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
6649 print_command_lines (uiout, l, 4);
6650 do_cleanups (script_chain);
6651 }
6652
6653 if (is_tracepoint (b))
6654 {
6655 struct tracepoint *t = (struct tracepoint *) b;
6656
6657 if (!part_of_multiple && t->pass_count)
6658 {
6659 annotate_field (10);
6660 ui_out_text (uiout, "\tpass count ");
6661 ui_out_field_int (uiout, "pass", t->pass_count);
6662 ui_out_text (uiout, " \n");
6663 }
6664
6665 /* Don't display it when tracepoint or tracepoint location is
6666 pending. */
6667 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6668 {
6669 annotate_field (11);
6670
6671 if (ui_out_is_mi_like_p (uiout))
6672 ui_out_field_string (uiout, "installed",
6673 loc->inserted ? "y" : "n");
6674 else
6675 {
6676 if (loc->inserted)
6677 ui_out_text (uiout, "\t");
6678 else
6679 ui_out_text (uiout, "\tnot ");
6680 ui_out_text (uiout, "installed on target\n");
6681 }
6682 }
6683 }
6684
6685 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
6686 {
6687 if (is_watchpoint (b))
6688 {
6689 struct watchpoint *w = (struct watchpoint *) b;
6690
6691 ui_out_field_string (uiout, "original-location", w->exp_string);
6692 }
6693 else if (b->location != NULL
6694 && event_location_to_string (b->location) != NULL)
6695 ui_out_field_string (uiout, "original-location",
6696 event_location_to_string (b->location));
6697 }
6698 }
6699
6700 static void
6701 print_one_breakpoint (struct breakpoint *b,
6702 struct bp_location **last_loc,
6703 int allflag)
6704 {
6705 struct cleanup *bkpt_chain;
6706 struct ui_out *uiout = current_uiout;
6707
6708 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
6709
6710 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
6711 do_cleanups (bkpt_chain);
6712
6713 /* If this breakpoint has custom print function,
6714 it's already printed. Otherwise, print individual
6715 locations, if any. */
6716 if (b->ops == NULL || b->ops->print_one == NULL)
6717 {
6718 /* If breakpoint has a single location that is disabled, we
6719 print it as if it had several locations, since otherwise it's
6720 hard to represent "breakpoint enabled, location disabled"
6721 situation.
6722
6723 Note that while hardware watchpoints have several locations
6724 internally, that's not a property exposed to user. */
6725 if (b->loc
6726 && !is_hardware_watchpoint (b)
6727 && (b->loc->next || !b->loc->enabled))
6728 {
6729 struct bp_location *loc;
6730 int n = 1;
6731
6732 for (loc = b->loc; loc; loc = loc->next, ++n)
6733 {
6734 struct cleanup *inner2 =
6735 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
6736 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
6737 do_cleanups (inner2);
6738 }
6739 }
6740 }
6741 }
6742
6743 static int
6744 breakpoint_address_bits (struct breakpoint *b)
6745 {
6746 int print_address_bits = 0;
6747 struct bp_location *loc;
6748
6749 /* Software watchpoints that aren't watching memory don't have an
6750 address to print. */
6751 if (is_no_memory_software_watchpoint (b))
6752 return 0;
6753
6754 for (loc = b->loc; loc; loc = loc->next)
6755 {
6756 int addr_bit;
6757
6758 addr_bit = gdbarch_addr_bit (loc->gdbarch);
6759 if (addr_bit > print_address_bits)
6760 print_address_bits = addr_bit;
6761 }
6762
6763 return print_address_bits;
6764 }
6765
6766 struct captured_breakpoint_query_args
6767 {
6768 int bnum;
6769 };
6770
6771 static int
6772 do_captured_breakpoint_query (struct ui_out *uiout, void *data)
6773 {
6774 struct captured_breakpoint_query_args *args
6775 = (struct captured_breakpoint_query_args *) data;
6776 struct breakpoint *b;
6777 struct bp_location *dummy_loc = NULL;
6778
6779 ALL_BREAKPOINTS (b)
6780 {
6781 if (args->bnum == b->number)
6782 {
6783 print_one_breakpoint (b, &dummy_loc, 0);
6784 return GDB_RC_OK;
6785 }
6786 }
6787 return GDB_RC_NONE;
6788 }
6789
6790 enum gdb_rc
6791 gdb_breakpoint_query (struct ui_out *uiout, int bnum,
6792 char **error_message)
6793 {
6794 struct captured_breakpoint_query_args args;
6795
6796 args.bnum = bnum;
6797 /* For the moment we don't trust print_one_breakpoint() to not throw
6798 an error. */
6799 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
6800 error_message, RETURN_MASK_ALL) < 0)
6801 return GDB_RC_FAIL;
6802 else
6803 return GDB_RC_OK;
6804 }
6805
6806 /* Return true if this breakpoint was set by the user, false if it is
6807 internal or momentary. */
6808
6809 int
6810 user_breakpoint_p (struct breakpoint *b)
6811 {
6812 return b->number > 0;
6813 }
6814
6815 /* See breakpoint.h. */
6816
6817 int
6818 pending_breakpoint_p (struct breakpoint *b)
6819 {
6820 return b->loc == NULL;
6821 }
6822
6823 /* Print information on user settable breakpoint (watchpoint, etc)
6824 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6825 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6826 FILTER is non-NULL, call it on each breakpoint and only include the
6827 ones for which it returns non-zero. Return the total number of
6828 breakpoints listed. */
6829
6830 static int
6831 breakpoint_1 (char *args, int allflag,
6832 int (*filter) (const struct breakpoint *))
6833 {
6834 struct breakpoint *b;
6835 struct bp_location *last_loc = NULL;
6836 int nr_printable_breakpoints;
6837 struct cleanup *bkpttbl_chain;
6838 struct value_print_options opts;
6839 int print_address_bits = 0;
6840 int print_type_col_width = 14;
6841 struct ui_out *uiout = current_uiout;
6842
6843 get_user_print_options (&opts);
6844
6845 /* Compute the number of rows in the table, as well as the size
6846 required for address fields. */
6847 nr_printable_breakpoints = 0;
6848 ALL_BREAKPOINTS (b)
6849 {
6850 /* If we have a filter, only list the breakpoints it accepts. */
6851 if (filter && !filter (b))
6852 continue;
6853
6854 /* If we have an "args" string, it is a list of breakpoints to
6855 accept. Skip the others. */
6856 if (args != NULL && *args != '\0')
6857 {
6858 if (allflag && parse_and_eval_long (args) != b->number)
6859 continue;
6860 if (!allflag && !number_is_in_list (args, b->number))
6861 continue;
6862 }
6863
6864 if (allflag || user_breakpoint_p (b))
6865 {
6866 int addr_bit, type_len;
6867
6868 addr_bit = breakpoint_address_bits (b);
6869 if (addr_bit > print_address_bits)
6870 print_address_bits = addr_bit;
6871
6872 type_len = strlen (bptype_string (b->type));
6873 if (type_len > print_type_col_width)
6874 print_type_col_width = type_len;
6875
6876 nr_printable_breakpoints++;
6877 }
6878 }
6879
6880 if (opts.addressprint)
6881 bkpttbl_chain
6882 = make_cleanup_ui_out_table_begin_end (uiout, 6,
6883 nr_printable_breakpoints,
6884 "BreakpointTable");
6885 else
6886 bkpttbl_chain
6887 = make_cleanup_ui_out_table_begin_end (uiout, 5,
6888 nr_printable_breakpoints,
6889 "BreakpointTable");
6890
6891 if (nr_printable_breakpoints > 0)
6892 annotate_breakpoints_headers ();
6893 if (nr_printable_breakpoints > 0)
6894 annotate_field (0);
6895 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
6896 if (nr_printable_breakpoints > 0)
6897 annotate_field (1);
6898 ui_out_table_header (uiout, print_type_col_width, ui_left,
6899 "type", "Type"); /* 2 */
6900 if (nr_printable_breakpoints > 0)
6901 annotate_field (2);
6902 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
6903 if (nr_printable_breakpoints > 0)
6904 annotate_field (3);
6905 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
6906 if (opts.addressprint)
6907 {
6908 if (nr_printable_breakpoints > 0)
6909 annotate_field (4);
6910 if (print_address_bits <= 32)
6911 ui_out_table_header (uiout, 10, ui_left,
6912 "addr", "Address"); /* 5 */
6913 else
6914 ui_out_table_header (uiout, 18, ui_left,
6915 "addr", "Address"); /* 5 */
6916 }
6917 if (nr_printable_breakpoints > 0)
6918 annotate_field (5);
6919 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
6920 ui_out_table_body (uiout);
6921 if (nr_printable_breakpoints > 0)
6922 annotate_breakpoints_table ();
6923
6924 ALL_BREAKPOINTS (b)
6925 {
6926 QUIT;
6927 /* If we have a filter, only list the breakpoints it accepts. */
6928 if (filter && !filter (b))
6929 continue;
6930
6931 /* If we have an "args" string, it is a list of breakpoints to
6932 accept. Skip the others. */
6933
6934 if (args != NULL && *args != '\0')
6935 {
6936 if (allflag) /* maintenance info breakpoint */
6937 {
6938 if (parse_and_eval_long (args) != b->number)
6939 continue;
6940 }
6941 else /* all others */
6942 {
6943 if (!number_is_in_list (args, b->number))
6944 continue;
6945 }
6946 }
6947 /* We only print out user settable breakpoints unless the
6948 allflag is set. */
6949 if (allflag || user_breakpoint_p (b))
6950 print_one_breakpoint (b, &last_loc, allflag);
6951 }
6952
6953 do_cleanups (bkpttbl_chain);
6954
6955 if (nr_printable_breakpoints == 0)
6956 {
6957 /* If there's a filter, let the caller decide how to report
6958 empty list. */
6959 if (!filter)
6960 {
6961 if (args == NULL || *args == '\0')
6962 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
6963 else
6964 ui_out_message (uiout, 0,
6965 "No breakpoint or watchpoint matching '%s'.\n",
6966 args);
6967 }
6968 }
6969 else
6970 {
6971 if (last_loc && !server_command)
6972 set_next_address (last_loc->gdbarch, last_loc->address);
6973 }
6974
6975 /* FIXME? Should this be moved up so that it is only called when
6976 there have been breakpoints? */
6977 annotate_breakpoints_table_end ();
6978
6979 return nr_printable_breakpoints;
6980 }
6981
6982 /* Display the value of default-collect in a way that is generally
6983 compatible with the breakpoint list. */
6984
6985 static void
6986 default_collect_info (void)
6987 {
6988 struct ui_out *uiout = current_uiout;
6989
6990 /* If it has no value (which is frequently the case), say nothing; a
6991 message like "No default-collect." gets in user's face when it's
6992 not wanted. */
6993 if (!*default_collect)
6994 return;
6995
6996 /* The following phrase lines up nicely with per-tracepoint collect
6997 actions. */
6998 ui_out_text (uiout, "default collect ");
6999 ui_out_field_string (uiout, "default-collect", default_collect);
7000 ui_out_text (uiout, " \n");
7001 }
7002
7003 static void
7004 breakpoints_info (char *args, int from_tty)
7005 {
7006 breakpoint_1 (args, 0, NULL);
7007
7008 default_collect_info ();
7009 }
7010
7011 static void
7012 watchpoints_info (char *args, int from_tty)
7013 {
7014 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
7015 struct ui_out *uiout = current_uiout;
7016
7017 if (num_printed == 0)
7018 {
7019 if (args == NULL || *args == '\0')
7020 ui_out_message (uiout, 0, "No watchpoints.\n");
7021 else
7022 ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
7023 }
7024 }
7025
7026 static void
7027 maintenance_info_breakpoints (char *args, int from_tty)
7028 {
7029 breakpoint_1 (args, 1, NULL);
7030
7031 default_collect_info ();
7032 }
7033
7034 static int
7035 breakpoint_has_pc (struct breakpoint *b,
7036 struct program_space *pspace,
7037 CORE_ADDR pc, struct obj_section *section)
7038 {
7039 struct bp_location *bl = b->loc;
7040
7041 for (; bl; bl = bl->next)
7042 {
7043 if (bl->pspace == pspace
7044 && bl->address == pc
7045 && (!overlay_debugging || bl->section == section))
7046 return 1;
7047 }
7048 return 0;
7049 }
7050
7051 /* Print a message describing any user-breakpoints set at PC. This
7052 concerns with logical breakpoints, so we match program spaces, not
7053 address spaces. */
7054
7055 static void
7056 describe_other_breakpoints (struct gdbarch *gdbarch,
7057 struct program_space *pspace, CORE_ADDR pc,
7058 struct obj_section *section, int thread)
7059 {
7060 int others = 0;
7061 struct breakpoint *b;
7062
7063 ALL_BREAKPOINTS (b)
7064 others += (user_breakpoint_p (b)
7065 && breakpoint_has_pc (b, pspace, pc, section));
7066 if (others > 0)
7067 {
7068 if (others == 1)
7069 printf_filtered (_("Note: breakpoint "));
7070 else /* if (others == ???) */
7071 printf_filtered (_("Note: breakpoints "));
7072 ALL_BREAKPOINTS (b)
7073 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
7074 {
7075 others--;
7076 printf_filtered ("%d", b->number);
7077 if (b->thread == -1 && thread != -1)
7078 printf_filtered (" (all threads)");
7079 else if (b->thread != -1)
7080 printf_filtered (" (thread %d)", b->thread);
7081 printf_filtered ("%s%s ",
7082 ((b->enable_state == bp_disabled
7083 || b->enable_state == bp_call_disabled)
7084 ? " (disabled)"
7085 : ""),
7086 (others > 1) ? ","
7087 : ((others == 1) ? " and" : ""));
7088 }
7089 printf_filtered (_("also set at pc "));
7090 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
7091 printf_filtered (".\n");
7092 }
7093 }
7094 \f
7095
7096 /* Return true iff it is meaningful to use the address member of
7097 BPT locations. For some breakpoint types, the locations' address members
7098 are irrelevant and it makes no sense to attempt to compare them to other
7099 addresses (or use them for any other purpose either).
7100
7101 More specifically, each of the following breakpoint types will
7102 always have a zero valued location address and we don't want to mark
7103 breakpoints of any of these types to be a duplicate of an actual
7104 breakpoint location at address zero:
7105
7106 bp_watchpoint
7107 bp_catchpoint
7108
7109 */
7110
7111 static int
7112 breakpoint_address_is_meaningful (struct breakpoint *bpt)
7113 {
7114 enum bptype type = bpt->type;
7115
7116 return (type != bp_watchpoint && type != bp_catchpoint);
7117 }
7118
7119 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
7120 true if LOC1 and LOC2 represent the same watchpoint location. */
7121
7122 static int
7123 watchpoint_locations_match (struct bp_location *loc1,
7124 struct bp_location *loc2)
7125 {
7126 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
7127 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
7128
7129 /* Both of them must exist. */
7130 gdb_assert (w1 != NULL);
7131 gdb_assert (w2 != NULL);
7132
7133 /* If the target can evaluate the condition expression in hardware,
7134 then we we need to insert both watchpoints even if they are at
7135 the same place. Otherwise the watchpoint will only trigger when
7136 the condition of whichever watchpoint was inserted evaluates to
7137 true, not giving a chance for GDB to check the condition of the
7138 other watchpoint. */
7139 if ((w1->cond_exp
7140 && target_can_accel_watchpoint_condition (loc1->address,
7141 loc1->length,
7142 loc1->watchpoint_type,
7143 w1->cond_exp.get ()))
7144 || (w2->cond_exp
7145 && target_can_accel_watchpoint_condition (loc2->address,
7146 loc2->length,
7147 loc2->watchpoint_type,
7148 w2->cond_exp.get ())))
7149 return 0;
7150
7151 /* Note that this checks the owner's type, not the location's. In
7152 case the target does not support read watchpoints, but does
7153 support access watchpoints, we'll have bp_read_watchpoint
7154 watchpoints with hw_access locations. Those should be considered
7155 duplicates of hw_read locations. The hw_read locations will
7156 become hw_access locations later. */
7157 return (loc1->owner->type == loc2->owner->type
7158 && loc1->pspace->aspace == loc2->pspace->aspace
7159 && loc1->address == loc2->address
7160 && loc1->length == loc2->length);
7161 }
7162
7163 /* See breakpoint.h. */
7164
7165 int
7166 breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
7167 struct address_space *aspace2, CORE_ADDR addr2)
7168 {
7169 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
7170 || aspace1 == aspace2)
7171 && addr1 == addr2);
7172 }
7173
7174 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
7175 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
7176 matches ASPACE2. On targets that have global breakpoints, the address
7177 space doesn't really matter. */
7178
7179 static int
7180 breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
7181 int len1, struct address_space *aspace2,
7182 CORE_ADDR addr2)
7183 {
7184 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
7185 || aspace1 == aspace2)
7186 && addr2 >= addr1 && addr2 < addr1 + len1);
7187 }
7188
7189 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
7190 a ranged breakpoint. In most targets, a match happens only if ASPACE
7191 matches the breakpoint's address space. On targets that have global
7192 breakpoints, the address space doesn't really matter. */
7193
7194 static int
7195 breakpoint_location_address_match (struct bp_location *bl,
7196 struct address_space *aspace,
7197 CORE_ADDR addr)
7198 {
7199 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
7200 aspace, addr)
7201 || (bl->length
7202 && breakpoint_address_match_range (bl->pspace->aspace,
7203 bl->address, bl->length,
7204 aspace, addr)));
7205 }
7206
7207 /* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
7208 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
7209 match happens only if ASPACE matches the breakpoint's address
7210 space. On targets that have global breakpoints, the address space
7211 doesn't really matter. */
7212
7213 static int
7214 breakpoint_location_address_range_overlap (struct bp_location *bl,
7215 struct address_space *aspace,
7216 CORE_ADDR addr, int len)
7217 {
7218 if (gdbarch_has_global_breakpoints (target_gdbarch ())
7219 || bl->pspace->aspace == aspace)
7220 {
7221 int bl_len = bl->length != 0 ? bl->length : 1;
7222
7223 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
7224 return 1;
7225 }
7226 return 0;
7227 }
7228
7229 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
7230 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
7231 true, otherwise returns false. */
7232
7233 static int
7234 tracepoint_locations_match (struct bp_location *loc1,
7235 struct bp_location *loc2)
7236 {
7237 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
7238 /* Since tracepoint locations are never duplicated with others', tracepoint
7239 locations at the same address of different tracepoints are regarded as
7240 different locations. */
7241 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7242 else
7243 return 0;
7244 }
7245
7246 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
7247 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
7248 represent the same location. */
7249
7250 static int
7251 breakpoint_locations_match (struct bp_location *loc1,
7252 struct bp_location *loc2)
7253 {
7254 int hw_point1, hw_point2;
7255
7256 /* Both of them must not be in moribund_locations. */
7257 gdb_assert (loc1->owner != NULL);
7258 gdb_assert (loc2->owner != NULL);
7259
7260 hw_point1 = is_hardware_watchpoint (loc1->owner);
7261 hw_point2 = is_hardware_watchpoint (loc2->owner);
7262
7263 if (hw_point1 != hw_point2)
7264 return 0;
7265 else if (hw_point1)
7266 return watchpoint_locations_match (loc1, loc2);
7267 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7268 return tracepoint_locations_match (loc1, loc2);
7269 else
7270 /* We compare bp_location.length in order to cover ranged breakpoints. */
7271 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7272 loc2->pspace->aspace, loc2->address)
7273 && loc1->length == loc2->length);
7274 }
7275
7276 static void
7277 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7278 int bnum, int have_bnum)
7279 {
7280 /* The longest string possibly returned by hex_string_custom
7281 is 50 chars. These must be at least that big for safety. */
7282 char astr1[64];
7283 char astr2[64];
7284
7285 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7286 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
7287 if (have_bnum)
7288 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
7289 bnum, astr1, astr2);
7290 else
7291 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
7292 }
7293
7294 /* Adjust a breakpoint's address to account for architectural
7295 constraints on breakpoint placement. Return the adjusted address.
7296 Note: Very few targets require this kind of adjustment. For most
7297 targets, this function is simply the identity function. */
7298
7299 static CORE_ADDR
7300 adjust_breakpoint_address (struct gdbarch *gdbarch,
7301 CORE_ADDR bpaddr, enum bptype bptype)
7302 {
7303 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
7304 {
7305 /* Very few targets need any kind of breakpoint adjustment. */
7306 return bpaddr;
7307 }
7308 else if (bptype == bp_watchpoint
7309 || bptype == bp_hardware_watchpoint
7310 || bptype == bp_read_watchpoint
7311 || bptype == bp_access_watchpoint
7312 || bptype == bp_catchpoint)
7313 {
7314 /* Watchpoints and the various bp_catch_* eventpoints should not
7315 have their addresses modified. */
7316 return bpaddr;
7317 }
7318 else if (bptype == bp_single_step)
7319 {
7320 /* Single-step breakpoints should not have their addresses
7321 modified. If there's any architectural constrain that
7322 applies to this address, then it should have already been
7323 taken into account when the breakpoint was created in the
7324 first place. If we didn't do this, stepping through e.g.,
7325 Thumb-2 IT blocks would break. */
7326 return bpaddr;
7327 }
7328 else
7329 {
7330 CORE_ADDR adjusted_bpaddr;
7331
7332 /* Some targets have architectural constraints on the placement
7333 of breakpoint instructions. Obtain the adjusted address. */
7334 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
7335
7336 /* An adjusted breakpoint address can significantly alter
7337 a user's expectations. Print a warning if an adjustment
7338 is required. */
7339 if (adjusted_bpaddr != bpaddr)
7340 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7341
7342 return adjusted_bpaddr;
7343 }
7344 }
7345
7346 void
7347 init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
7348 struct breakpoint *owner)
7349 {
7350 memset (loc, 0, sizeof (*loc));
7351
7352 gdb_assert (ops != NULL);
7353
7354 loc->ops = ops;
7355 loc->owner = owner;
7356 loc->cond_bytecode = NULL;
7357 loc->shlib_disabled = 0;
7358 loc->enabled = 1;
7359
7360 switch (owner->type)
7361 {
7362 case bp_breakpoint:
7363 case bp_single_step:
7364 case bp_until:
7365 case bp_finish:
7366 case bp_longjmp:
7367 case bp_longjmp_resume:
7368 case bp_longjmp_call_dummy:
7369 case bp_exception:
7370 case bp_exception_resume:
7371 case bp_step_resume:
7372 case bp_hp_step_resume:
7373 case bp_watchpoint_scope:
7374 case bp_call_dummy:
7375 case bp_std_terminate:
7376 case bp_shlib_event:
7377 case bp_thread_event:
7378 case bp_overlay_event:
7379 case bp_jit_event:
7380 case bp_longjmp_master:
7381 case bp_std_terminate_master:
7382 case bp_exception_master:
7383 case bp_gnu_ifunc_resolver:
7384 case bp_gnu_ifunc_resolver_return:
7385 case bp_dprintf:
7386 loc->loc_type = bp_loc_software_breakpoint;
7387 mark_breakpoint_location_modified (loc);
7388 break;
7389 case bp_hardware_breakpoint:
7390 loc->loc_type = bp_loc_hardware_breakpoint;
7391 mark_breakpoint_location_modified (loc);
7392 break;
7393 case bp_hardware_watchpoint:
7394 case bp_read_watchpoint:
7395 case bp_access_watchpoint:
7396 loc->loc_type = bp_loc_hardware_watchpoint;
7397 break;
7398 case bp_watchpoint:
7399 case bp_catchpoint:
7400 case bp_tracepoint:
7401 case bp_fast_tracepoint:
7402 case bp_static_tracepoint:
7403 loc->loc_type = bp_loc_other;
7404 break;
7405 default:
7406 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
7407 }
7408
7409 loc->refc = 1;
7410 }
7411
7412 /* Allocate a struct bp_location. */
7413
7414 static struct bp_location *
7415 allocate_bp_location (struct breakpoint *bpt)
7416 {
7417 return bpt->ops->allocate_location (bpt);
7418 }
7419
7420 static void
7421 free_bp_location (struct bp_location *loc)
7422 {
7423 loc->ops->dtor (loc);
7424 delete loc;
7425 }
7426
7427 /* Increment reference count. */
7428
7429 static void
7430 incref_bp_location (struct bp_location *bl)
7431 {
7432 ++bl->refc;
7433 }
7434
7435 /* Decrement reference count. If the reference count reaches 0,
7436 destroy the bp_location. Sets *BLP to NULL. */
7437
7438 static void
7439 decref_bp_location (struct bp_location **blp)
7440 {
7441 gdb_assert ((*blp)->refc > 0);
7442
7443 if (--(*blp)->refc == 0)
7444 free_bp_location (*blp);
7445 *blp = NULL;
7446 }
7447
7448 /* Add breakpoint B at the end of the global breakpoint chain. */
7449
7450 static void
7451 add_to_breakpoint_chain (struct breakpoint *b)
7452 {
7453 struct breakpoint *b1;
7454
7455 /* Add this breakpoint to the end of the chain so that a list of
7456 breakpoints will come out in order of increasing numbers. */
7457
7458 b1 = breakpoint_chain;
7459 if (b1 == 0)
7460 breakpoint_chain = b;
7461 else
7462 {
7463 while (b1->next)
7464 b1 = b1->next;
7465 b1->next = b;
7466 }
7467 }
7468
7469 /* Initializes breakpoint B with type BPTYPE and no locations yet. */
7470
7471 static void
7472 init_raw_breakpoint_without_location (struct breakpoint *b,
7473 struct gdbarch *gdbarch,
7474 enum bptype bptype,
7475 const struct breakpoint_ops *ops)
7476 {
7477 memset (b, 0, sizeof (*b));
7478
7479 gdb_assert (ops != NULL);
7480
7481 b->ops = ops;
7482 b->type = bptype;
7483 b->gdbarch = gdbarch;
7484 b->language = current_language->la_language;
7485 b->input_radix = input_radix;
7486 b->thread = -1;
7487 b->enable_state = bp_enabled;
7488 b->next = 0;
7489 b->silent = 0;
7490 b->ignore_count = 0;
7491 b->commands = NULL;
7492 b->frame_id = null_frame_id;
7493 b->condition_not_parsed = 0;
7494 b->py_bp_object = NULL;
7495 b->related_breakpoint = b;
7496 b->location = NULL;
7497 }
7498
7499 /* Helper to set_raw_breakpoint below. Creates a breakpoint
7500 that has type BPTYPE and has no locations as yet. */
7501
7502 static struct breakpoint *
7503 set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
7504 enum bptype bptype,
7505 const struct breakpoint_ops *ops)
7506 {
7507 struct breakpoint *b = new breakpoint ();
7508
7509 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7510 add_to_breakpoint_chain (b);
7511 return b;
7512 }
7513
7514 /* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7515 resolutions should be made as the user specified the location explicitly
7516 enough. */
7517
7518 static void
7519 set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
7520 {
7521 gdb_assert (loc->owner != NULL);
7522
7523 if (loc->owner->type == bp_breakpoint
7524 || loc->owner->type == bp_hardware_breakpoint
7525 || is_tracepoint (loc->owner))
7526 {
7527 int is_gnu_ifunc;
7528 const char *function_name;
7529 CORE_ADDR func_addr;
7530
7531 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
7532 &func_addr, NULL, &is_gnu_ifunc);
7533
7534 if (is_gnu_ifunc && !explicit_loc)
7535 {
7536 struct breakpoint *b = loc->owner;
7537
7538 gdb_assert (loc->pspace == current_program_space);
7539 if (gnu_ifunc_resolve_name (function_name,
7540 &loc->requested_address))
7541 {
7542 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7543 loc->address = adjust_breakpoint_address (loc->gdbarch,
7544 loc->requested_address,
7545 b->type);
7546 }
7547 else if (b->type == bp_breakpoint && b->loc == loc
7548 && loc->next == NULL && b->related_breakpoint == b)
7549 {
7550 /* Create only the whole new breakpoint of this type but do not
7551 mess more complicated breakpoints with multiple locations. */
7552 b->type = bp_gnu_ifunc_resolver;
7553 /* Remember the resolver's address for use by the return
7554 breakpoint. */
7555 loc->related_address = func_addr;
7556 }
7557 }
7558
7559 if (function_name)
7560 loc->function_name = xstrdup (function_name);
7561 }
7562 }
7563
7564 /* Attempt to determine architecture of location identified by SAL. */
7565 struct gdbarch *
7566 get_sal_arch (struct symtab_and_line sal)
7567 {
7568 if (sal.section)
7569 return get_objfile_arch (sal.section->objfile);
7570 if (sal.symtab)
7571 return get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
7572
7573 return NULL;
7574 }
7575
7576 /* Low level routine for partially initializing a breakpoint of type
7577 BPTYPE. The newly created breakpoint's address, section, source
7578 file name, and line number are provided by SAL.
7579
7580 It is expected that the caller will complete the initialization of
7581 the newly created breakpoint struct as well as output any status
7582 information regarding the creation of a new breakpoint. */
7583
7584 static void
7585 init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
7586 struct symtab_and_line sal, enum bptype bptype,
7587 const struct breakpoint_ops *ops)
7588 {
7589 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7590
7591 add_location_to_breakpoint (b, &sal);
7592
7593 if (bptype != bp_catchpoint)
7594 gdb_assert (sal.pspace != NULL);
7595
7596 /* Store the program space that was used to set the breakpoint,
7597 except for ordinary breakpoints, which are independent of the
7598 program space. */
7599 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7600 b->pspace = sal.pspace;
7601 }
7602
7603 /* set_raw_breakpoint is a low level routine for allocating and
7604 partially initializing a breakpoint of type BPTYPE. The newly
7605 created breakpoint's address, section, source file name, and line
7606 number are provided by SAL. The newly created and partially
7607 initialized breakpoint is added to the breakpoint chain and
7608 is also returned as the value of this function.
7609
7610 It is expected that the caller will complete the initialization of
7611 the newly created breakpoint struct as well as output any status
7612 information regarding the creation of a new breakpoint. In
7613 particular, set_raw_breakpoint does NOT set the breakpoint
7614 number! Care should be taken to not allow an error to occur
7615 prior to completing the initialization of the breakpoint. If this
7616 should happen, a bogus breakpoint will be left on the chain. */
7617
7618 struct breakpoint *
7619 set_raw_breakpoint (struct gdbarch *gdbarch,
7620 struct symtab_and_line sal, enum bptype bptype,
7621 const struct breakpoint_ops *ops)
7622 {
7623 struct breakpoint *b = new breakpoint ();
7624
7625 init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
7626 add_to_breakpoint_chain (b);
7627 return b;
7628 }
7629
7630 /* Call this routine when stepping and nexting to enable a breakpoint
7631 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7632 initiated the operation. */
7633
7634 void
7635 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
7636 {
7637 struct breakpoint *b, *b_tmp;
7638 int thread = tp->global_num;
7639
7640 /* To avoid having to rescan all objfile symbols at every step,
7641 we maintain a list of continually-inserted but always disabled
7642 longjmp "master" breakpoints. Here, we simply create momentary
7643 clones of those and enable them for the requested thread. */
7644 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7645 if (b->pspace == current_program_space
7646 && (b->type == bp_longjmp_master
7647 || b->type == bp_exception_master))
7648 {
7649 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7650 struct breakpoint *clone;
7651
7652 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7653 after their removal. */
7654 clone = momentary_breakpoint_from_master (b, type,
7655 &longjmp_breakpoint_ops, 1);
7656 clone->thread = thread;
7657 }
7658
7659 tp->initiating_frame = frame;
7660 }
7661
7662 /* Delete all longjmp breakpoints from THREAD. */
7663 void
7664 delete_longjmp_breakpoint (int thread)
7665 {
7666 struct breakpoint *b, *b_tmp;
7667
7668 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7669 if (b->type == bp_longjmp || b->type == bp_exception)
7670 {
7671 if (b->thread == thread)
7672 delete_breakpoint (b);
7673 }
7674 }
7675
7676 void
7677 delete_longjmp_breakpoint_at_next_stop (int thread)
7678 {
7679 struct breakpoint *b, *b_tmp;
7680
7681 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7682 if (b->type == bp_longjmp || b->type == bp_exception)
7683 {
7684 if (b->thread == thread)
7685 b->disposition = disp_del_at_next_stop;
7686 }
7687 }
7688
7689 /* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7690 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7691 pointer to any of them. Return NULL if this system cannot place longjmp
7692 breakpoints. */
7693
7694 struct breakpoint *
7695 set_longjmp_breakpoint_for_call_dummy (void)
7696 {
7697 struct breakpoint *b, *retval = NULL;
7698
7699 ALL_BREAKPOINTS (b)
7700 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7701 {
7702 struct breakpoint *new_b;
7703
7704 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7705 &momentary_breakpoint_ops,
7706 1);
7707 new_b->thread = ptid_to_global_thread_id (inferior_ptid);
7708
7709 /* Link NEW_B into the chain of RETVAL breakpoints. */
7710
7711 gdb_assert (new_b->related_breakpoint == new_b);
7712 if (retval == NULL)
7713 retval = new_b;
7714 new_b->related_breakpoint = retval;
7715 while (retval->related_breakpoint != new_b->related_breakpoint)
7716 retval = retval->related_breakpoint;
7717 retval->related_breakpoint = new_b;
7718 }
7719
7720 return retval;
7721 }
7722
7723 /* Verify all existing dummy frames and their associated breakpoints for
7724 TP. Remove those which can no longer be found in the current frame
7725 stack.
7726
7727 You should call this function only at places where it is safe to currently
7728 unwind the whole stack. Failed stack unwind would discard live dummy
7729 frames. */
7730
7731 void
7732 check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
7733 {
7734 struct breakpoint *b, *b_tmp;
7735
7736 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7737 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
7738 {
7739 struct breakpoint *dummy_b = b->related_breakpoint;
7740
7741 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7742 dummy_b = dummy_b->related_breakpoint;
7743 if (dummy_b->type != bp_call_dummy
7744 || frame_find_by_id (dummy_b->frame_id) != NULL)
7745 continue;
7746
7747 dummy_frame_discard (dummy_b->frame_id, tp->ptid);
7748
7749 while (b->related_breakpoint != b)
7750 {
7751 if (b_tmp == b->related_breakpoint)
7752 b_tmp = b->related_breakpoint->next;
7753 delete_breakpoint (b->related_breakpoint);
7754 }
7755 delete_breakpoint (b);
7756 }
7757 }
7758
7759 void
7760 enable_overlay_breakpoints (void)
7761 {
7762 struct breakpoint *b;
7763
7764 ALL_BREAKPOINTS (b)
7765 if (b->type == bp_overlay_event)
7766 {
7767 b->enable_state = bp_enabled;
7768 update_global_location_list (UGLL_MAY_INSERT);
7769 overlay_events_enabled = 1;
7770 }
7771 }
7772
7773 void
7774 disable_overlay_breakpoints (void)
7775 {
7776 struct breakpoint *b;
7777
7778 ALL_BREAKPOINTS (b)
7779 if (b->type == bp_overlay_event)
7780 {
7781 b->enable_state = bp_disabled;
7782 update_global_location_list (UGLL_DONT_INSERT);
7783 overlay_events_enabled = 0;
7784 }
7785 }
7786
7787 /* Set an active std::terminate breakpoint for each std::terminate
7788 master breakpoint. */
7789 void
7790 set_std_terminate_breakpoint (void)
7791 {
7792 struct breakpoint *b, *b_tmp;
7793
7794 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7795 if (b->pspace == current_program_space
7796 && b->type == bp_std_terminate_master)
7797 {
7798 momentary_breakpoint_from_master (b, bp_std_terminate,
7799 &momentary_breakpoint_ops, 1);
7800 }
7801 }
7802
7803 /* Delete all the std::terminate breakpoints. */
7804 void
7805 delete_std_terminate_breakpoint (void)
7806 {
7807 struct breakpoint *b, *b_tmp;
7808
7809 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7810 if (b->type == bp_std_terminate)
7811 delete_breakpoint (b);
7812 }
7813
7814 struct breakpoint *
7815 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7816 {
7817 struct breakpoint *b;
7818
7819 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7820 &internal_breakpoint_ops);
7821
7822 b->enable_state = bp_enabled;
7823 /* location has to be used or breakpoint_re_set will delete me. */
7824 b->location = new_address_location (b->loc->address, NULL, 0);
7825
7826 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7827
7828 return b;
7829 }
7830
7831 struct lang_and_radix
7832 {
7833 enum language lang;
7834 int radix;
7835 };
7836
7837 /* Create a breakpoint for JIT code registration and unregistration. */
7838
7839 struct breakpoint *
7840 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7841 {
7842 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7843 &internal_breakpoint_ops);
7844 }
7845
7846 /* Remove JIT code registration and unregistration breakpoint(s). */
7847
7848 void
7849 remove_jit_event_breakpoints (void)
7850 {
7851 struct breakpoint *b, *b_tmp;
7852
7853 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7854 if (b->type == bp_jit_event
7855 && b->loc->pspace == current_program_space)
7856 delete_breakpoint (b);
7857 }
7858
7859 void
7860 remove_solib_event_breakpoints (void)
7861 {
7862 struct breakpoint *b, *b_tmp;
7863
7864 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7865 if (b->type == bp_shlib_event
7866 && b->loc->pspace == current_program_space)
7867 delete_breakpoint (b);
7868 }
7869
7870 /* See breakpoint.h. */
7871
7872 void
7873 remove_solib_event_breakpoints_at_next_stop (void)
7874 {
7875 struct breakpoint *b, *b_tmp;
7876
7877 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7878 if (b->type == bp_shlib_event
7879 && b->loc->pspace == current_program_space)
7880 b->disposition = disp_del_at_next_stop;
7881 }
7882
7883 /* Helper for create_solib_event_breakpoint /
7884 create_and_insert_solib_event_breakpoint. Allows specifying which
7885 INSERT_MODE to pass through to update_global_location_list. */
7886
7887 static struct breakpoint *
7888 create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7889 enum ugll_insert_mode insert_mode)
7890 {
7891 struct breakpoint *b;
7892
7893 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7894 &internal_breakpoint_ops);
7895 update_global_location_list_nothrow (insert_mode);
7896 return b;
7897 }
7898
7899 struct breakpoint *
7900 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7901 {
7902 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7903 }
7904
7905 /* See breakpoint.h. */
7906
7907 struct breakpoint *
7908 create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7909 {
7910 struct breakpoint *b;
7911
7912 /* Explicitly tell update_global_location_list to insert
7913 locations. */
7914 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
7915 if (!b->loc->inserted)
7916 {
7917 delete_breakpoint (b);
7918 return NULL;
7919 }
7920 return b;
7921 }
7922
7923 /* Disable any breakpoints that are on code in shared libraries. Only
7924 apply to enabled breakpoints, disabled ones can just stay disabled. */
7925
7926 void
7927 disable_breakpoints_in_shlibs (void)
7928 {
7929 struct bp_location *loc, **locp_tmp;
7930
7931 ALL_BP_LOCATIONS (loc, locp_tmp)
7932 {
7933 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7934 struct breakpoint *b = loc->owner;
7935
7936 /* We apply the check to all breakpoints, including disabled for
7937 those with loc->duplicate set. This is so that when breakpoint
7938 becomes enabled, or the duplicate is removed, gdb will try to
7939 insert all breakpoints. If we don't set shlib_disabled here,
7940 we'll try to insert those breakpoints and fail. */
7941 if (((b->type == bp_breakpoint)
7942 || (b->type == bp_jit_event)
7943 || (b->type == bp_hardware_breakpoint)
7944 || (is_tracepoint (b)))
7945 && loc->pspace == current_program_space
7946 && !loc->shlib_disabled
7947 && solib_name_from_address (loc->pspace, loc->address)
7948 )
7949 {
7950 loc->shlib_disabled = 1;
7951 }
7952 }
7953 }
7954
7955 /* Disable any breakpoints and tracepoints that are in SOLIB upon
7956 notification of unloaded_shlib. Only apply to enabled breakpoints,
7957 disabled ones can just stay disabled. */
7958
7959 static void
7960 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7961 {
7962 struct bp_location *loc, **locp_tmp;
7963 int disabled_shlib_breaks = 0;
7964
7965 /* SunOS a.out shared libraries are always mapped, so do not
7966 disable breakpoints; they will only be reported as unloaded
7967 through clear_solib when GDB discards its shared library
7968 list. See clear_solib for more information. */
7969 if (exec_bfd != NULL
7970 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
7971 return;
7972
7973 ALL_BP_LOCATIONS (loc, locp_tmp)
7974 {
7975 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7976 struct breakpoint *b = loc->owner;
7977
7978 if (solib->pspace == loc->pspace
7979 && !loc->shlib_disabled
7980 && (((b->type == bp_breakpoint
7981 || b->type == bp_jit_event
7982 || b->type == bp_hardware_breakpoint)
7983 && (loc->loc_type == bp_loc_hardware_breakpoint
7984 || loc->loc_type == bp_loc_software_breakpoint))
7985 || is_tracepoint (b))
7986 && solib_contains_address_p (solib, loc->address))
7987 {
7988 loc->shlib_disabled = 1;
7989 /* At this point, we cannot rely on remove_breakpoint
7990 succeeding so we must mark the breakpoint as not inserted
7991 to prevent future errors occurring in remove_breakpoints. */
7992 loc->inserted = 0;
7993
7994 /* This may cause duplicate notifications for the same breakpoint. */
7995 observer_notify_breakpoint_modified (b);
7996
7997 if (!disabled_shlib_breaks)
7998 {
7999 target_terminal_ours_for_output ();
8000 warning (_("Temporarily disabling breakpoints "
8001 "for unloaded shared library \"%s\""),
8002 solib->so_name);
8003 }
8004 disabled_shlib_breaks = 1;
8005 }
8006 }
8007 }
8008
8009 /* Disable any breakpoints and tracepoints in OBJFILE upon
8010 notification of free_objfile. Only apply to enabled breakpoints,
8011 disabled ones can just stay disabled. */
8012
8013 static void
8014 disable_breakpoints_in_freed_objfile (struct objfile *objfile)
8015 {
8016 struct breakpoint *b;
8017
8018 if (objfile == NULL)
8019 return;
8020
8021 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
8022 managed by the user with add-symbol-file/remove-symbol-file.
8023 Similarly to how breakpoints in shared libraries are handled in
8024 response to "nosharedlibrary", mark breakpoints in such modules
8025 shlib_disabled so they end up uninserted on the next global
8026 location list update. Shared libraries not loaded by the user
8027 aren't handled here -- they're already handled in
8028 disable_breakpoints_in_unloaded_shlib, called by solib.c's
8029 solib_unloaded observer. We skip objfiles that are not
8030 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
8031 main objfile). */
8032 if ((objfile->flags & OBJF_SHARED) == 0
8033 || (objfile->flags & OBJF_USERLOADED) == 0)
8034 return;
8035
8036 ALL_BREAKPOINTS (b)
8037 {
8038 struct bp_location *loc;
8039 int bp_modified = 0;
8040
8041 if (!is_breakpoint (b) && !is_tracepoint (b))
8042 continue;
8043
8044 for (loc = b->loc; loc != NULL; loc = loc->next)
8045 {
8046 CORE_ADDR loc_addr = loc->address;
8047
8048 if (loc->loc_type != bp_loc_hardware_breakpoint
8049 && loc->loc_type != bp_loc_software_breakpoint)
8050 continue;
8051
8052 if (loc->shlib_disabled != 0)
8053 continue;
8054
8055 if (objfile->pspace != loc->pspace)
8056 continue;
8057
8058 if (loc->loc_type != bp_loc_hardware_breakpoint
8059 && loc->loc_type != bp_loc_software_breakpoint)
8060 continue;
8061
8062 if (is_addr_in_objfile (loc_addr, objfile))
8063 {
8064 loc->shlib_disabled = 1;
8065 /* At this point, we don't know whether the object was
8066 unmapped from the inferior or not, so leave the
8067 inserted flag alone. We'll handle failure to
8068 uninsert quietly, in case the object was indeed
8069 unmapped. */
8070
8071 mark_breakpoint_location_modified (loc);
8072
8073 bp_modified = 1;
8074 }
8075 }
8076
8077 if (bp_modified)
8078 observer_notify_breakpoint_modified (b);
8079 }
8080 }
8081
8082 /* FORK & VFORK catchpoints. */
8083
8084 /* An instance of this type is used to represent a fork or vfork
8085 catchpoint. It includes a "struct breakpoint" as a kind of base
8086 class; users downcast to "struct breakpoint *" when needed. A
8087 breakpoint is really of this type iff its ops pointer points to
8088 CATCH_FORK_BREAKPOINT_OPS. */
8089
8090 struct fork_catchpoint
8091 {
8092 /* The base class. */
8093 struct breakpoint base;
8094
8095 /* Process id of a child process whose forking triggered this
8096 catchpoint. This field is only valid immediately after this
8097 catchpoint has triggered. */
8098 ptid_t forked_inferior_pid;
8099 };
8100
8101 /* Implement the "insert" breakpoint_ops method for fork
8102 catchpoints. */
8103
8104 static int
8105 insert_catch_fork (struct bp_location *bl)
8106 {
8107 return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
8108 }
8109
8110 /* Implement the "remove" breakpoint_ops method for fork
8111 catchpoints. */
8112
8113 static int
8114 remove_catch_fork (struct bp_location *bl, enum remove_bp_reason reason)
8115 {
8116 return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
8117 }
8118
8119 /* Implement the "breakpoint_hit" breakpoint_ops method for fork
8120 catchpoints. */
8121
8122 static int
8123 breakpoint_hit_catch_fork (const struct bp_location *bl,
8124 struct address_space *aspace, CORE_ADDR bp_addr,
8125 const struct target_waitstatus *ws)
8126 {
8127 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8128
8129 if (ws->kind != TARGET_WAITKIND_FORKED)
8130 return 0;
8131
8132 c->forked_inferior_pid = ws->value.related_pid;
8133 return 1;
8134 }
8135
8136 /* Implement the "print_it" breakpoint_ops method for fork
8137 catchpoints. */
8138
8139 static enum print_stop_action
8140 print_it_catch_fork (bpstat bs)
8141 {
8142 struct ui_out *uiout = current_uiout;
8143 struct breakpoint *b = bs->breakpoint_at;
8144 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
8145
8146 annotate_catchpoint (b->number);
8147 maybe_print_thread_hit_breakpoint (uiout);
8148 if (b->disposition == disp_del)
8149 ui_out_text (uiout, "Temporary catchpoint ");
8150 else
8151 ui_out_text (uiout, "Catchpoint ");
8152 if (ui_out_is_mi_like_p (uiout))
8153 {
8154 ui_out_field_string (uiout, "reason",
8155 async_reason_lookup (EXEC_ASYNC_FORK));
8156 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8157 }
8158 ui_out_field_int (uiout, "bkptno", b->number);
8159 ui_out_text (uiout, " (forked process ");
8160 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
8161 ui_out_text (uiout, "), ");
8162 return PRINT_SRC_AND_LOC;
8163 }
8164
8165 /* Implement the "print_one" breakpoint_ops method for fork
8166 catchpoints. */
8167
8168 static void
8169 print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
8170 {
8171 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8172 struct value_print_options opts;
8173 struct ui_out *uiout = current_uiout;
8174
8175 get_user_print_options (&opts);
8176
8177 /* Field 4, the address, is omitted (which makes the columns not
8178 line up too nicely with the headers, but the effect is relatively
8179 readable). */
8180 if (opts.addressprint)
8181 ui_out_field_skip (uiout, "addr");
8182 annotate_field (5);
8183 ui_out_text (uiout, "fork");
8184 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
8185 {
8186 ui_out_text (uiout, ", process ");
8187 ui_out_field_int (uiout, "what",
8188 ptid_get_pid (c->forked_inferior_pid));
8189 ui_out_spaces (uiout, 1);
8190 }
8191
8192 if (ui_out_is_mi_like_p (uiout))
8193 ui_out_field_string (uiout, "catch-type", "fork");
8194 }
8195
8196 /* Implement the "print_mention" breakpoint_ops method for fork
8197 catchpoints. */
8198
8199 static void
8200 print_mention_catch_fork (struct breakpoint *b)
8201 {
8202 printf_filtered (_("Catchpoint %d (fork)"), b->number);
8203 }
8204
8205 /* Implement the "print_recreate" breakpoint_ops method for fork
8206 catchpoints. */
8207
8208 static void
8209 print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
8210 {
8211 fprintf_unfiltered (fp, "catch fork");
8212 print_recreate_thread (b, fp);
8213 }
8214
8215 /* The breakpoint_ops structure to be used in fork catchpoints. */
8216
8217 static struct breakpoint_ops catch_fork_breakpoint_ops;
8218
8219 /* Implement the "insert" breakpoint_ops method for vfork
8220 catchpoints. */
8221
8222 static int
8223 insert_catch_vfork (struct bp_location *bl)
8224 {
8225 return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
8226 }
8227
8228 /* Implement the "remove" breakpoint_ops method for vfork
8229 catchpoints. */
8230
8231 static int
8232 remove_catch_vfork (struct bp_location *bl, enum remove_bp_reason reason)
8233 {
8234 return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
8235 }
8236
8237 /* Implement the "breakpoint_hit" breakpoint_ops method for vfork
8238 catchpoints. */
8239
8240 static int
8241 breakpoint_hit_catch_vfork (const struct bp_location *bl,
8242 struct address_space *aspace, CORE_ADDR bp_addr,
8243 const struct target_waitstatus *ws)
8244 {
8245 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8246
8247 if (ws->kind != TARGET_WAITKIND_VFORKED)
8248 return 0;
8249
8250 c->forked_inferior_pid = ws->value.related_pid;
8251 return 1;
8252 }
8253
8254 /* Implement the "print_it" breakpoint_ops method for vfork
8255 catchpoints. */
8256
8257 static enum print_stop_action
8258 print_it_catch_vfork (bpstat bs)
8259 {
8260 struct ui_out *uiout = current_uiout;
8261 struct breakpoint *b = bs->breakpoint_at;
8262 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8263
8264 annotate_catchpoint (b->number);
8265 maybe_print_thread_hit_breakpoint (uiout);
8266 if (b->disposition == disp_del)
8267 ui_out_text (uiout, "Temporary catchpoint ");
8268 else
8269 ui_out_text (uiout, "Catchpoint ");
8270 if (ui_out_is_mi_like_p (uiout))
8271 {
8272 ui_out_field_string (uiout, "reason",
8273 async_reason_lookup (EXEC_ASYNC_VFORK));
8274 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8275 }
8276 ui_out_field_int (uiout, "bkptno", b->number);
8277 ui_out_text (uiout, " (vforked process ");
8278 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
8279 ui_out_text (uiout, "), ");
8280 return PRINT_SRC_AND_LOC;
8281 }
8282
8283 /* Implement the "print_one" breakpoint_ops method for vfork
8284 catchpoints. */
8285
8286 static void
8287 print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
8288 {
8289 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8290 struct value_print_options opts;
8291 struct ui_out *uiout = current_uiout;
8292
8293 get_user_print_options (&opts);
8294 /* Field 4, the address, is omitted (which makes the columns not
8295 line up too nicely with the headers, but the effect is relatively
8296 readable). */
8297 if (opts.addressprint)
8298 ui_out_field_skip (uiout, "addr");
8299 annotate_field (5);
8300 ui_out_text (uiout, "vfork");
8301 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
8302 {
8303 ui_out_text (uiout, ", process ");
8304 ui_out_field_int (uiout, "what",
8305 ptid_get_pid (c->forked_inferior_pid));
8306 ui_out_spaces (uiout, 1);
8307 }
8308
8309 if (ui_out_is_mi_like_p (uiout))
8310 ui_out_field_string (uiout, "catch-type", "vfork");
8311 }
8312
8313 /* Implement the "print_mention" breakpoint_ops method for vfork
8314 catchpoints. */
8315
8316 static void
8317 print_mention_catch_vfork (struct breakpoint *b)
8318 {
8319 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
8320 }
8321
8322 /* Implement the "print_recreate" breakpoint_ops method for vfork
8323 catchpoints. */
8324
8325 static void
8326 print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8327 {
8328 fprintf_unfiltered (fp, "catch vfork");
8329 print_recreate_thread (b, fp);
8330 }
8331
8332 /* The breakpoint_ops structure to be used in vfork catchpoints. */
8333
8334 static struct breakpoint_ops catch_vfork_breakpoint_ops;
8335
8336 /* An instance of this type is used to represent an solib catchpoint.
8337 It includes a "struct breakpoint" as a kind of base class; users
8338 downcast to "struct breakpoint *" when needed. A breakpoint is
8339 really of this type iff its ops pointer points to
8340 CATCH_SOLIB_BREAKPOINT_OPS. */
8341
8342 struct solib_catchpoint
8343 {
8344 /* The base class. */
8345 struct breakpoint base;
8346
8347 /* True for "catch load", false for "catch unload". */
8348 unsigned char is_load;
8349
8350 /* Regular expression to match, if any. COMPILED is only valid when
8351 REGEX is non-NULL. */
8352 char *regex;
8353 regex_t compiled;
8354 };
8355
8356 static void
8357 dtor_catch_solib (struct breakpoint *b)
8358 {
8359 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8360
8361 if (self->regex)
8362 regfree (&self->compiled);
8363 xfree (self->regex);
8364
8365 base_breakpoint_ops.dtor (b);
8366 }
8367
8368 static int
8369 insert_catch_solib (struct bp_location *ignore)
8370 {
8371 return 0;
8372 }
8373
8374 static int
8375 remove_catch_solib (struct bp_location *ignore, enum remove_bp_reason reason)
8376 {
8377 return 0;
8378 }
8379
8380 static int
8381 breakpoint_hit_catch_solib (const struct bp_location *bl,
8382 struct address_space *aspace,
8383 CORE_ADDR bp_addr,
8384 const struct target_waitstatus *ws)
8385 {
8386 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8387 struct breakpoint *other;
8388
8389 if (ws->kind == TARGET_WAITKIND_LOADED)
8390 return 1;
8391
8392 ALL_BREAKPOINTS (other)
8393 {
8394 struct bp_location *other_bl;
8395
8396 if (other == bl->owner)
8397 continue;
8398
8399 if (other->type != bp_shlib_event)
8400 continue;
8401
8402 if (self->base.pspace != NULL && other->pspace != self->base.pspace)
8403 continue;
8404
8405 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8406 {
8407 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8408 return 1;
8409 }
8410 }
8411
8412 return 0;
8413 }
8414
8415 static void
8416 check_status_catch_solib (struct bpstats *bs)
8417 {
8418 struct solib_catchpoint *self
8419 = (struct solib_catchpoint *) bs->breakpoint_at;
8420 int ix;
8421
8422 if (self->is_load)
8423 {
8424 struct so_list *iter;
8425
8426 for (ix = 0;
8427 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
8428 ix, iter);
8429 ++ix)
8430 {
8431 if (!self->regex
8432 || regexec (&self->compiled, iter->so_name, 0, NULL, 0) == 0)
8433 return;
8434 }
8435 }
8436 else
8437 {
8438 char *iter;
8439
8440 for (ix = 0;
8441 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
8442 ix, iter);
8443 ++ix)
8444 {
8445 if (!self->regex
8446 || regexec (&self->compiled, iter, 0, NULL, 0) == 0)
8447 return;
8448 }
8449 }
8450
8451 bs->stop = 0;
8452 bs->print_it = print_it_noop;
8453 }
8454
8455 static enum print_stop_action
8456 print_it_catch_solib (bpstat bs)
8457 {
8458 struct breakpoint *b = bs->breakpoint_at;
8459 struct ui_out *uiout = current_uiout;
8460
8461 annotate_catchpoint (b->number);
8462 maybe_print_thread_hit_breakpoint (uiout);
8463 if (b->disposition == disp_del)
8464 ui_out_text (uiout, "Temporary catchpoint ");
8465 else
8466 ui_out_text (uiout, "Catchpoint ");
8467 ui_out_field_int (uiout, "bkptno", b->number);
8468 ui_out_text (uiout, "\n");
8469 if (ui_out_is_mi_like_p (uiout))
8470 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8471 print_solib_event (1);
8472 return PRINT_SRC_AND_LOC;
8473 }
8474
8475 static void
8476 print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8477 {
8478 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8479 struct value_print_options opts;
8480 struct ui_out *uiout = current_uiout;
8481 char *msg;
8482
8483 get_user_print_options (&opts);
8484 /* Field 4, the address, is omitted (which makes the columns not
8485 line up too nicely with the headers, but the effect is relatively
8486 readable). */
8487 if (opts.addressprint)
8488 {
8489 annotate_field (4);
8490 ui_out_field_skip (uiout, "addr");
8491 }
8492
8493 annotate_field (5);
8494 if (self->is_load)
8495 {
8496 if (self->regex)
8497 msg = xstrprintf (_("load of library matching %s"), self->regex);
8498 else
8499 msg = xstrdup (_("load of library"));
8500 }
8501 else
8502 {
8503 if (self->regex)
8504 msg = xstrprintf (_("unload of library matching %s"), self->regex);
8505 else
8506 msg = xstrdup (_("unload of library"));
8507 }
8508 ui_out_field_string (uiout, "what", msg);
8509 xfree (msg);
8510
8511 if (ui_out_is_mi_like_p (uiout))
8512 ui_out_field_string (uiout, "catch-type",
8513 self->is_load ? "load" : "unload");
8514 }
8515
8516 static void
8517 print_mention_catch_solib (struct breakpoint *b)
8518 {
8519 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8520
8521 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8522 self->is_load ? "load" : "unload");
8523 }
8524
8525 static void
8526 print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8527 {
8528 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8529
8530 fprintf_unfiltered (fp, "%s %s",
8531 b->disposition == disp_del ? "tcatch" : "catch",
8532 self->is_load ? "load" : "unload");
8533 if (self->regex)
8534 fprintf_unfiltered (fp, " %s", self->regex);
8535 fprintf_unfiltered (fp, "\n");
8536 }
8537
8538 static struct breakpoint_ops catch_solib_breakpoint_ops;
8539
8540 /* Shared helper function (MI and CLI) for creating and installing
8541 a shared object event catchpoint. If IS_LOAD is non-zero then
8542 the events to be caught are load events, otherwise they are
8543 unload events. If IS_TEMP is non-zero the catchpoint is a
8544 temporary one. If ENABLED is non-zero the catchpoint is
8545 created in an enabled state. */
8546
8547 void
8548 add_solib_catchpoint (char *arg, int is_load, int is_temp, int enabled)
8549 {
8550 struct solib_catchpoint *c;
8551 struct gdbarch *gdbarch = get_current_arch ();
8552 struct cleanup *cleanup;
8553
8554 if (!arg)
8555 arg = "";
8556 arg = skip_spaces (arg);
8557
8558 c = new solib_catchpoint ();
8559 cleanup = make_cleanup (xfree, c);
8560
8561 if (*arg != '\0')
8562 {
8563 int errcode;
8564
8565 errcode = regcomp (&c->compiled, arg, REG_NOSUB);
8566 if (errcode != 0)
8567 {
8568 char *err = get_regcomp_error (errcode, &c->compiled);
8569
8570 make_cleanup (xfree, err);
8571 error (_("Invalid regexp (%s): %s"), err, arg);
8572 }
8573 c->regex = xstrdup (arg);
8574 }
8575
8576 c->is_load = is_load;
8577 init_catchpoint (&c->base, gdbarch, is_temp, NULL,
8578 &catch_solib_breakpoint_ops);
8579
8580 c->base.enable_state = enabled ? bp_enabled : bp_disabled;
8581
8582 discard_cleanups (cleanup);
8583 install_breakpoint (0, &c->base, 1);
8584 }
8585
8586 /* A helper function that does all the work for "catch load" and
8587 "catch unload". */
8588
8589 static void
8590 catch_load_or_unload (char *arg, int from_tty, int is_load,
8591 struct cmd_list_element *command)
8592 {
8593 int tempflag;
8594 const int enabled = 1;
8595
8596 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8597
8598 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8599 }
8600
8601 static void
8602 catch_load_command_1 (char *arg, int from_tty,
8603 struct cmd_list_element *command)
8604 {
8605 catch_load_or_unload (arg, from_tty, 1, command);
8606 }
8607
8608 static void
8609 catch_unload_command_1 (char *arg, int from_tty,
8610 struct cmd_list_element *command)
8611 {
8612 catch_load_or_unload (arg, from_tty, 0, command);
8613 }
8614
8615 /* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8616 is non-zero, then make the breakpoint temporary. If COND_STRING is
8617 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8618 the breakpoint_ops structure associated to the catchpoint. */
8619
8620 void
8621 init_catchpoint (struct breakpoint *b,
8622 struct gdbarch *gdbarch, int tempflag,
8623 char *cond_string,
8624 const struct breakpoint_ops *ops)
8625 {
8626 struct symtab_and_line sal;
8627
8628 init_sal (&sal);
8629 sal.pspace = current_program_space;
8630
8631 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
8632
8633 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
8634 b->disposition = tempflag ? disp_del : disp_donttouch;
8635 }
8636
8637 void
8638 install_breakpoint (int internal, struct breakpoint *b, int update_gll)
8639 {
8640 add_to_breakpoint_chain (b);
8641 set_breakpoint_number (internal, b);
8642 if (is_tracepoint (b))
8643 set_tracepoint_count (breakpoint_count);
8644 if (!internal)
8645 mention (b);
8646 observer_notify_breakpoint_created (b);
8647
8648 if (update_gll)
8649 update_global_location_list (UGLL_MAY_INSERT);
8650 }
8651
8652 static void
8653 create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8654 int tempflag, char *cond_string,
8655 const struct breakpoint_ops *ops)
8656 {
8657 struct fork_catchpoint *c = new fork_catchpoint ();
8658
8659 init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
8660
8661 c->forked_inferior_pid = null_ptid;
8662
8663 install_breakpoint (0, &c->base, 1);
8664 }
8665
8666 /* Exec catchpoints. */
8667
8668 /* An instance of this type is used to represent an exec catchpoint.
8669 It includes a "struct breakpoint" as a kind of base class; users
8670 downcast to "struct breakpoint *" when needed. A breakpoint is
8671 really of this type iff its ops pointer points to
8672 CATCH_EXEC_BREAKPOINT_OPS. */
8673
8674 struct exec_catchpoint
8675 {
8676 /* The base class. */
8677 struct breakpoint base;
8678
8679 /* Filename of a program whose exec triggered this catchpoint.
8680 This field is only valid immediately after this catchpoint has
8681 triggered. */
8682 char *exec_pathname;
8683 };
8684
8685 /* Implement the "dtor" breakpoint_ops method for exec
8686 catchpoints. */
8687
8688 static void
8689 dtor_catch_exec (struct breakpoint *b)
8690 {
8691 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8692
8693 xfree (c->exec_pathname);
8694
8695 base_breakpoint_ops.dtor (b);
8696 }
8697
8698 static int
8699 insert_catch_exec (struct bp_location *bl)
8700 {
8701 return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
8702 }
8703
8704 static int
8705 remove_catch_exec (struct bp_location *bl, enum remove_bp_reason reason)
8706 {
8707 return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
8708 }
8709
8710 static int
8711 breakpoint_hit_catch_exec (const struct bp_location *bl,
8712 struct address_space *aspace, CORE_ADDR bp_addr,
8713 const struct target_waitstatus *ws)
8714 {
8715 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8716
8717 if (ws->kind != TARGET_WAITKIND_EXECD)
8718 return 0;
8719
8720 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8721 return 1;
8722 }
8723
8724 static enum print_stop_action
8725 print_it_catch_exec (bpstat bs)
8726 {
8727 struct ui_out *uiout = current_uiout;
8728 struct breakpoint *b = bs->breakpoint_at;
8729 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8730
8731 annotate_catchpoint (b->number);
8732 maybe_print_thread_hit_breakpoint (uiout);
8733 if (b->disposition == disp_del)
8734 ui_out_text (uiout, "Temporary catchpoint ");
8735 else
8736 ui_out_text (uiout, "Catchpoint ");
8737 if (ui_out_is_mi_like_p (uiout))
8738 {
8739 ui_out_field_string (uiout, "reason",
8740 async_reason_lookup (EXEC_ASYNC_EXEC));
8741 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8742 }
8743 ui_out_field_int (uiout, "bkptno", b->number);
8744 ui_out_text (uiout, " (exec'd ");
8745 ui_out_field_string (uiout, "new-exec", c->exec_pathname);
8746 ui_out_text (uiout, "), ");
8747
8748 return PRINT_SRC_AND_LOC;
8749 }
8750
8751 static void
8752 print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
8753 {
8754 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8755 struct value_print_options opts;
8756 struct ui_out *uiout = current_uiout;
8757
8758 get_user_print_options (&opts);
8759
8760 /* Field 4, the address, is omitted (which makes the columns
8761 not line up too nicely with the headers, but the effect
8762 is relatively readable). */
8763 if (opts.addressprint)
8764 ui_out_field_skip (uiout, "addr");
8765 annotate_field (5);
8766 ui_out_text (uiout, "exec");
8767 if (c->exec_pathname != NULL)
8768 {
8769 ui_out_text (uiout, ", program \"");
8770 ui_out_field_string (uiout, "what", c->exec_pathname);
8771 ui_out_text (uiout, "\" ");
8772 }
8773
8774 if (ui_out_is_mi_like_p (uiout))
8775 ui_out_field_string (uiout, "catch-type", "exec");
8776 }
8777
8778 static void
8779 print_mention_catch_exec (struct breakpoint *b)
8780 {
8781 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8782 }
8783
8784 /* Implement the "print_recreate" breakpoint_ops method for exec
8785 catchpoints. */
8786
8787 static void
8788 print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8789 {
8790 fprintf_unfiltered (fp, "catch exec");
8791 print_recreate_thread (b, fp);
8792 }
8793
8794 static struct breakpoint_ops catch_exec_breakpoint_ops;
8795
8796 static int
8797 hw_breakpoint_used_count (void)
8798 {
8799 int i = 0;
8800 struct breakpoint *b;
8801 struct bp_location *bl;
8802
8803 ALL_BREAKPOINTS (b)
8804 {
8805 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
8806 for (bl = b->loc; bl; bl = bl->next)
8807 {
8808 /* Special types of hardware breakpoints may use more than
8809 one register. */
8810 i += b->ops->resources_needed (bl);
8811 }
8812 }
8813
8814 return i;
8815 }
8816
8817 /* Returns the resources B would use if it were a hardware
8818 watchpoint. */
8819
8820 static int
8821 hw_watchpoint_use_count (struct breakpoint *b)
8822 {
8823 int i = 0;
8824 struct bp_location *bl;
8825
8826 if (!breakpoint_enabled (b))
8827 return 0;
8828
8829 for (bl = b->loc; bl; bl = bl->next)
8830 {
8831 /* Special types of hardware watchpoints may use more than
8832 one register. */
8833 i += b->ops->resources_needed (bl);
8834 }
8835
8836 return i;
8837 }
8838
8839 /* Returns the sum the used resources of all hardware watchpoints of
8840 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8841 the sum of the used resources of all hardware watchpoints of other
8842 types _not_ TYPE. */
8843
8844 static int
8845 hw_watchpoint_used_count_others (struct breakpoint *except,
8846 enum bptype type, int *other_type_used)
8847 {
8848 int i = 0;
8849 struct breakpoint *b;
8850
8851 *other_type_used = 0;
8852 ALL_BREAKPOINTS (b)
8853 {
8854 if (b == except)
8855 continue;
8856 if (!breakpoint_enabled (b))
8857 continue;
8858
8859 if (b->type == type)
8860 i += hw_watchpoint_use_count (b);
8861 else if (is_hardware_watchpoint (b))
8862 *other_type_used = 1;
8863 }
8864
8865 return i;
8866 }
8867
8868 void
8869 disable_watchpoints_before_interactive_call_start (void)
8870 {
8871 struct breakpoint *b;
8872
8873 ALL_BREAKPOINTS (b)
8874 {
8875 if (is_watchpoint (b) && breakpoint_enabled (b))
8876 {
8877 b->enable_state = bp_call_disabled;
8878 update_global_location_list (UGLL_DONT_INSERT);
8879 }
8880 }
8881 }
8882
8883 void
8884 enable_watchpoints_after_interactive_call_stop (void)
8885 {
8886 struct breakpoint *b;
8887
8888 ALL_BREAKPOINTS (b)
8889 {
8890 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
8891 {
8892 b->enable_state = bp_enabled;
8893 update_global_location_list (UGLL_MAY_INSERT);
8894 }
8895 }
8896 }
8897
8898 void
8899 disable_breakpoints_before_startup (void)
8900 {
8901 current_program_space->executing_startup = 1;
8902 update_global_location_list (UGLL_DONT_INSERT);
8903 }
8904
8905 void
8906 enable_breakpoints_after_startup (void)
8907 {
8908 current_program_space->executing_startup = 0;
8909 breakpoint_re_set ();
8910 }
8911
8912 /* Create a new single-step breakpoint for thread THREAD, with no
8913 locations. */
8914
8915 static struct breakpoint *
8916 new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8917 {
8918 struct breakpoint *b = new breakpoint ();
8919
8920 init_raw_breakpoint_without_location (b, gdbarch, bp_single_step,
8921 &momentary_breakpoint_ops);
8922
8923 b->disposition = disp_donttouch;
8924 b->frame_id = null_frame_id;
8925
8926 b->thread = thread;
8927 gdb_assert (b->thread != 0);
8928
8929 add_to_breakpoint_chain (b);
8930
8931 return b;
8932 }
8933
8934 /* Set a momentary breakpoint of type TYPE at address specified by
8935 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8936 frame. */
8937
8938 struct breakpoint *
8939 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8940 struct frame_id frame_id, enum bptype type)
8941 {
8942 struct breakpoint *b;
8943
8944 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8945 tail-called one. */
8946 gdb_assert (!frame_id_artificial_p (frame_id));
8947
8948 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
8949 b->enable_state = bp_enabled;
8950 b->disposition = disp_donttouch;
8951 b->frame_id = frame_id;
8952
8953 /* If we're debugging a multi-threaded program, then we want
8954 momentary breakpoints to be active in only a single thread of
8955 control. */
8956 if (in_thread_list (inferior_ptid))
8957 b->thread = ptid_to_global_thread_id (inferior_ptid);
8958
8959 update_global_location_list_nothrow (UGLL_MAY_INSERT);
8960
8961 return b;
8962 }
8963
8964 /* Make a momentary breakpoint based on the master breakpoint ORIG.
8965 The new breakpoint will have type TYPE, use OPS as its
8966 breakpoint_ops, and will set enabled to LOC_ENABLED. */
8967
8968 static struct breakpoint *
8969 momentary_breakpoint_from_master (struct breakpoint *orig,
8970 enum bptype type,
8971 const struct breakpoint_ops *ops,
8972 int loc_enabled)
8973 {
8974 struct breakpoint *copy;
8975
8976 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
8977 copy->loc = allocate_bp_location (copy);
8978 set_breakpoint_location_function (copy->loc, 1);
8979
8980 copy->loc->gdbarch = orig->loc->gdbarch;
8981 copy->loc->requested_address = orig->loc->requested_address;
8982 copy->loc->address = orig->loc->address;
8983 copy->loc->section = orig->loc->section;
8984 copy->loc->pspace = orig->loc->pspace;
8985 copy->loc->probe = orig->loc->probe;
8986 copy->loc->line_number = orig->loc->line_number;
8987 copy->loc->symtab = orig->loc->symtab;
8988 copy->loc->enabled = loc_enabled;
8989 copy->frame_id = orig->frame_id;
8990 copy->thread = orig->thread;
8991 copy->pspace = orig->pspace;
8992
8993 copy->enable_state = bp_enabled;
8994 copy->disposition = disp_donttouch;
8995 copy->number = internal_breakpoint_number--;
8996
8997 update_global_location_list_nothrow (UGLL_DONT_INSERT);
8998 return copy;
8999 }
9000
9001 /* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
9002 ORIG is NULL. */
9003
9004 struct breakpoint *
9005 clone_momentary_breakpoint (struct breakpoint *orig)
9006 {
9007 /* If there's nothing to clone, then return nothing. */
9008 if (orig == NULL)
9009 return NULL;
9010
9011 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
9012 }
9013
9014 struct breakpoint *
9015 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
9016 enum bptype type)
9017 {
9018 struct symtab_and_line sal;
9019
9020 sal = find_pc_line (pc, 0);
9021 sal.pc = pc;
9022 sal.section = find_pc_overlay (pc);
9023 sal.explicit_pc = 1;
9024
9025 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
9026 }
9027 \f
9028
9029 /* Tell the user we have just set a breakpoint B. */
9030
9031 static void
9032 mention (struct breakpoint *b)
9033 {
9034 b->ops->print_mention (b);
9035 if (ui_out_is_mi_like_p (current_uiout))
9036 return;
9037 printf_filtered ("\n");
9038 }
9039 \f
9040
9041 static int bp_loc_is_permanent (struct bp_location *loc);
9042
9043 static struct bp_location *
9044 add_location_to_breakpoint (struct breakpoint *b,
9045 const struct symtab_and_line *sal)
9046 {
9047 struct bp_location *loc, **tmp;
9048 CORE_ADDR adjusted_address;
9049 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
9050
9051 if (loc_gdbarch == NULL)
9052 loc_gdbarch = b->gdbarch;
9053
9054 /* Adjust the breakpoint's address prior to allocating a location.
9055 Once we call allocate_bp_location(), that mostly uninitialized
9056 location will be placed on the location chain. Adjustment of the
9057 breakpoint may cause target_read_memory() to be called and we do
9058 not want its scan of the location chain to find a breakpoint and
9059 location that's only been partially initialized. */
9060 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
9061 sal->pc, b->type);
9062
9063 /* Sort the locations by their ADDRESS. */
9064 loc = allocate_bp_location (b);
9065 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
9066 tmp = &((*tmp)->next))
9067 ;
9068 loc->next = *tmp;
9069 *tmp = loc;
9070
9071 loc->requested_address = sal->pc;
9072 loc->address = adjusted_address;
9073 loc->pspace = sal->pspace;
9074 loc->probe.probe = sal->probe;
9075 loc->probe.objfile = sal->objfile;
9076 gdb_assert (loc->pspace != NULL);
9077 loc->section = sal->section;
9078 loc->gdbarch = loc_gdbarch;
9079 loc->line_number = sal->line;
9080 loc->symtab = sal->symtab;
9081
9082 set_breakpoint_location_function (loc,
9083 sal->explicit_pc || sal->explicit_line);
9084
9085 /* While by definition, permanent breakpoints are already present in the
9086 code, we don't mark the location as inserted. Normally one would expect
9087 that GDB could rely on that breakpoint instruction to stop the program,
9088 thus removing the need to insert its own breakpoint, except that executing
9089 the breakpoint instruction can kill the target instead of reporting a
9090 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
9091 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
9092 with "Trap 0x02 while interrupts disabled, Error state". Letting the
9093 breakpoint be inserted normally results in QEMU knowing about the GDB
9094 breakpoint, and thus trap before the breakpoint instruction is executed.
9095 (If GDB later needs to continue execution past the permanent breakpoint,
9096 it manually increments the PC, thus avoiding executing the breakpoint
9097 instruction.) */
9098 if (bp_loc_is_permanent (loc))
9099 loc->permanent = 1;
9100
9101 return loc;
9102 }
9103 \f
9104
9105 /* See breakpoint.h. */
9106
9107 int
9108 program_breakpoint_here_p (struct gdbarch *gdbarch, CORE_ADDR address)
9109 {
9110 int len;
9111 CORE_ADDR addr;
9112 const gdb_byte *bpoint;
9113 gdb_byte *target_mem;
9114 struct cleanup *cleanup;
9115 int retval = 0;
9116
9117 addr = address;
9118 bpoint = gdbarch_breakpoint_from_pc (gdbarch, &addr, &len);
9119
9120 /* Software breakpoints unsupported? */
9121 if (bpoint == NULL)
9122 return 0;
9123
9124 target_mem = (gdb_byte *) alloca (len);
9125
9126 /* Enable the automatic memory restoration from breakpoints while
9127 we read the memory. Otherwise we could say about our temporary
9128 breakpoints they are permanent. */
9129 cleanup = make_show_memory_breakpoints_cleanup (0);
9130
9131 if (target_read_memory (address, target_mem, len) == 0
9132 && memcmp (target_mem, bpoint, len) == 0)
9133 retval = 1;
9134
9135 do_cleanups (cleanup);
9136
9137 return retval;
9138 }
9139
9140 /* Return 1 if LOC is pointing to a permanent breakpoint,
9141 return 0 otherwise. */
9142
9143 static int
9144 bp_loc_is_permanent (struct bp_location *loc)
9145 {
9146 struct cleanup *cleanup;
9147 int retval;
9148
9149 gdb_assert (loc != NULL);
9150
9151 /* If we have a catchpoint or a watchpoint, just return 0. We should not
9152 attempt to read from the addresses the locations of these breakpoint types
9153 point to. program_breakpoint_here_p, below, will attempt to read
9154 memory. */
9155 if (!breakpoint_address_is_meaningful (loc->owner))
9156 return 0;
9157
9158 cleanup = save_current_space_and_thread ();
9159 switch_to_program_space_and_thread (loc->pspace);
9160
9161 retval = program_breakpoint_here_p (loc->gdbarch, loc->address);
9162
9163 do_cleanups (cleanup);
9164
9165 return retval;
9166 }
9167
9168 /* Build a command list for the dprintf corresponding to the current
9169 settings of the dprintf style options. */
9170
9171 static void
9172 update_dprintf_command_list (struct breakpoint *b)
9173 {
9174 char *dprintf_args = b->extra_string;
9175 char *printf_line = NULL;
9176
9177 if (!dprintf_args)
9178 return;
9179
9180 dprintf_args = skip_spaces (dprintf_args);
9181
9182 /* Allow a comma, as it may have terminated a location, but don't
9183 insist on it. */
9184 if (*dprintf_args == ',')
9185 ++dprintf_args;
9186 dprintf_args = skip_spaces (dprintf_args);
9187
9188 if (*dprintf_args != '"')
9189 error (_("Bad format string, missing '\"'."));
9190
9191 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
9192 printf_line = xstrprintf ("printf %s", dprintf_args);
9193 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
9194 {
9195 if (!dprintf_function)
9196 error (_("No function supplied for dprintf call"));
9197
9198 if (dprintf_channel && strlen (dprintf_channel) > 0)
9199 printf_line = xstrprintf ("call (void) %s (%s,%s)",
9200 dprintf_function,
9201 dprintf_channel,
9202 dprintf_args);
9203 else
9204 printf_line = xstrprintf ("call (void) %s (%s)",
9205 dprintf_function,
9206 dprintf_args);
9207 }
9208 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
9209 {
9210 if (target_can_run_breakpoint_commands ())
9211 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
9212 else
9213 {
9214 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
9215 printf_line = xstrprintf ("printf %s", dprintf_args);
9216 }
9217 }
9218 else
9219 internal_error (__FILE__, __LINE__,
9220 _("Invalid dprintf style."));
9221
9222 gdb_assert (printf_line != NULL);
9223 /* Manufacture a printf sequence. */
9224 {
9225 struct command_line *printf_cmd_line = XNEW (struct command_line);
9226
9227 printf_cmd_line->control_type = simple_control;
9228 printf_cmd_line->body_count = 0;
9229 printf_cmd_line->body_list = NULL;
9230 printf_cmd_line->next = NULL;
9231 printf_cmd_line->line = printf_line;
9232
9233 breakpoint_set_commands (b, printf_cmd_line);
9234 }
9235 }
9236
9237 /* Update all dprintf commands, making their command lists reflect
9238 current style settings. */
9239
9240 static void
9241 update_dprintf_commands (char *args, int from_tty,
9242 struct cmd_list_element *c)
9243 {
9244 struct breakpoint *b;
9245
9246 ALL_BREAKPOINTS (b)
9247 {
9248 if (b->type == bp_dprintf)
9249 update_dprintf_command_list (b);
9250 }
9251 }
9252
9253 /* Create a breakpoint with SAL as location. Use LOCATION
9254 as a description of the location, and COND_STRING
9255 as condition expression. If LOCATION is NULL then create an
9256 "address location" from the address in the SAL. */
9257
9258 static void
9259 init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
9260 struct symtabs_and_lines sals,
9261 struct event_location *location,
9262 char *filter, char *cond_string,
9263 char *extra_string,
9264 enum bptype type, enum bpdisp disposition,
9265 int thread, int task, int ignore_count,
9266 const struct breakpoint_ops *ops, int from_tty,
9267 int enabled, int internal, unsigned flags,
9268 int display_canonical)
9269 {
9270 int i;
9271
9272 if (type == bp_hardware_breakpoint)
9273 {
9274 int target_resources_ok;
9275
9276 i = hw_breakpoint_used_count ();
9277 target_resources_ok =
9278 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9279 i + 1, 0);
9280 if (target_resources_ok == 0)
9281 error (_("No hardware breakpoint support in the target."));
9282 else if (target_resources_ok < 0)
9283 error (_("Hardware breakpoints used exceeds limit."));
9284 }
9285
9286 gdb_assert (sals.nelts > 0);
9287
9288 for (i = 0; i < sals.nelts; ++i)
9289 {
9290 struct symtab_and_line sal = sals.sals[i];
9291 struct bp_location *loc;
9292
9293 if (from_tty)
9294 {
9295 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9296 if (!loc_gdbarch)
9297 loc_gdbarch = gdbarch;
9298
9299 describe_other_breakpoints (loc_gdbarch,
9300 sal.pspace, sal.pc, sal.section, thread);
9301 }
9302
9303 if (i == 0)
9304 {
9305 init_raw_breakpoint (b, gdbarch, sal, type, ops);
9306 b->thread = thread;
9307 b->task = task;
9308
9309 b->cond_string = cond_string;
9310 b->extra_string = extra_string;
9311 b->ignore_count = ignore_count;
9312 b->enable_state = enabled ? bp_enabled : bp_disabled;
9313 b->disposition = disposition;
9314
9315 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9316 b->loc->inserted = 1;
9317
9318 if (type == bp_static_tracepoint)
9319 {
9320 struct tracepoint *t = (struct tracepoint *) b;
9321 struct static_tracepoint_marker marker;
9322
9323 if (strace_marker_p (b))
9324 {
9325 /* We already know the marker exists, otherwise, we
9326 wouldn't see a sal for it. */
9327 const char *p = &event_location_to_string (b->location)[3];
9328 const char *endp;
9329 char *marker_str;
9330
9331 p = skip_spaces_const (p);
9332
9333 endp = skip_to_space_const (p);
9334
9335 marker_str = savestring (p, endp - p);
9336 t->static_trace_marker_id = marker_str;
9337
9338 printf_filtered (_("Probed static tracepoint "
9339 "marker \"%s\"\n"),
9340 t->static_trace_marker_id);
9341 }
9342 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
9343 {
9344 t->static_trace_marker_id = xstrdup (marker.str_id);
9345 release_static_tracepoint_marker (&marker);
9346
9347 printf_filtered (_("Probed static tracepoint "
9348 "marker \"%s\"\n"),
9349 t->static_trace_marker_id);
9350 }
9351 else
9352 warning (_("Couldn't determine the static "
9353 "tracepoint marker to probe"));
9354 }
9355
9356 loc = b->loc;
9357 }
9358 else
9359 {
9360 loc = add_location_to_breakpoint (b, &sal);
9361 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9362 loc->inserted = 1;
9363 }
9364
9365 if (b->cond_string)
9366 {
9367 const char *arg = b->cond_string;
9368
9369 loc->cond = parse_exp_1 (&arg, loc->address,
9370 block_for_pc (loc->address), 0);
9371 if (*arg)
9372 error (_("Garbage '%s' follows condition"), arg);
9373 }
9374
9375 /* Dynamic printf requires and uses additional arguments on the
9376 command line, otherwise it's an error. */
9377 if (type == bp_dprintf)
9378 {
9379 if (b->extra_string)
9380 update_dprintf_command_list (b);
9381 else
9382 error (_("Format string required"));
9383 }
9384 else if (b->extra_string)
9385 error (_("Garbage '%s' at end of command"), b->extra_string);
9386 }
9387
9388 b->display_canonical = display_canonical;
9389 if (location != NULL)
9390 b->location = location;
9391 else
9392 {
9393 const char *addr_string = NULL;
9394 int addr_string_len = 0;
9395
9396 if (location != NULL)
9397 addr_string = event_location_to_string (location);
9398 if (addr_string != NULL)
9399 addr_string_len = strlen (addr_string);
9400
9401 b->location = new_address_location (b->loc->address,
9402 addr_string, addr_string_len);
9403 }
9404 b->filter = filter;
9405 }
9406
9407 static void
9408 create_breakpoint_sal (struct gdbarch *gdbarch,
9409 struct symtabs_and_lines sals,
9410 struct event_location *location,
9411 char *filter, char *cond_string,
9412 char *extra_string,
9413 enum bptype type, enum bpdisp disposition,
9414 int thread, int task, int ignore_count,
9415 const struct breakpoint_ops *ops, int from_tty,
9416 int enabled, int internal, unsigned flags,
9417 int display_canonical)
9418 {
9419 struct breakpoint *b;
9420 struct cleanup *old_chain;
9421
9422 if (is_tracepoint_type (type))
9423 {
9424 struct tracepoint *t;
9425
9426 t = new tracepoint ();
9427 b = &t->base;
9428 }
9429 else
9430 b = new breakpoint ();
9431
9432 old_chain = make_cleanup (xfree, b);
9433
9434 init_breakpoint_sal (b, gdbarch,
9435 sals, location,
9436 filter, cond_string, extra_string,
9437 type, disposition,
9438 thread, task, ignore_count,
9439 ops, from_tty,
9440 enabled, internal, flags,
9441 display_canonical);
9442 discard_cleanups (old_chain);
9443
9444 install_breakpoint (internal, b, 0);
9445 }
9446
9447 /* Add SALS.nelts breakpoints to the breakpoint table. For each
9448 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9449 value. COND_STRING, if not NULL, specified the condition to be
9450 used for all breakpoints. Essentially the only case where
9451 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9452 function. In that case, it's still not possible to specify
9453 separate conditions for different overloaded functions, so
9454 we take just a single condition string.
9455
9456 NOTE: If the function succeeds, the caller is expected to cleanup
9457 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
9458 array contents). If the function fails (error() is called), the
9459 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
9460 COND and SALS arrays and each of those arrays contents. */
9461
9462 static void
9463 create_breakpoints_sal (struct gdbarch *gdbarch,
9464 struct linespec_result *canonical,
9465 char *cond_string, char *extra_string,
9466 enum bptype type, enum bpdisp disposition,
9467 int thread, int task, int ignore_count,
9468 const struct breakpoint_ops *ops, int from_tty,
9469 int enabled, int internal, unsigned flags)
9470 {
9471 int i;
9472 struct linespec_sals *lsal;
9473
9474 if (canonical->pre_expanded)
9475 gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
9476
9477 for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
9478 {
9479 /* Note that 'location' can be NULL in the case of a plain
9480 'break', without arguments. */
9481 struct event_location *location
9482 = (canonical->location != NULL
9483 ? copy_event_location (canonical->location) : NULL);
9484 char *filter_string = lsal->canonical ? xstrdup (lsal->canonical) : NULL;
9485 struct cleanup *inner = make_cleanup_delete_event_location (location);
9486
9487 make_cleanup (xfree, filter_string);
9488 create_breakpoint_sal (gdbarch, lsal->sals,
9489 location,
9490 filter_string,
9491 cond_string, extra_string,
9492 type, disposition,
9493 thread, task, ignore_count, ops,
9494 from_tty, enabled, internal, flags,
9495 canonical->special_display);
9496 discard_cleanups (inner);
9497 }
9498 }
9499
9500 /* Parse LOCATION which is assumed to be a SAL specification possibly
9501 followed by conditionals. On return, SALS contains an array of SAL
9502 addresses found. LOCATION points to the end of the SAL (for
9503 linespec locations).
9504
9505 The array and the line spec strings are allocated on the heap, it is
9506 the caller's responsibility to free them. */
9507
9508 static void
9509 parse_breakpoint_sals (const struct event_location *location,
9510 struct linespec_result *canonical)
9511 {
9512 struct symtab_and_line cursal;
9513
9514 if (event_location_type (location) == LINESPEC_LOCATION)
9515 {
9516 const char *address = get_linespec_location (location);
9517
9518 if (address == NULL)
9519 {
9520 /* The last displayed codepoint, if it's valid, is our default
9521 breakpoint address. */
9522 if (last_displayed_sal_is_valid ())
9523 {
9524 struct linespec_sals lsal;
9525 struct symtab_and_line sal;
9526 CORE_ADDR pc;
9527
9528 init_sal (&sal); /* Initialize to zeroes. */
9529 lsal.sals.sals = XNEW (struct symtab_and_line);
9530
9531 /* Set sal's pspace, pc, symtab, and line to the values
9532 corresponding to the last call to print_frame_info.
9533 Be sure to reinitialize LINE with NOTCURRENT == 0
9534 as the breakpoint line number is inappropriate otherwise.
9535 find_pc_line would adjust PC, re-set it back. */
9536 get_last_displayed_sal (&sal);
9537 pc = sal.pc;
9538 sal = find_pc_line (pc, 0);
9539
9540 /* "break" without arguments is equivalent to "break *PC"
9541 where PC is the last displayed codepoint's address. So
9542 make sure to set sal.explicit_pc to prevent GDB from
9543 trying to expand the list of sals to include all other
9544 instances with the same symtab and line. */
9545 sal.pc = pc;
9546 sal.explicit_pc = 1;
9547
9548 lsal.sals.sals[0] = sal;
9549 lsal.sals.nelts = 1;
9550 lsal.canonical = NULL;
9551
9552 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
9553 return;
9554 }
9555 else
9556 error (_("No default breakpoint address now."));
9557 }
9558 }
9559
9560 /* Force almost all breakpoints to be in terms of the
9561 current_source_symtab (which is decode_line_1's default).
9562 This should produce the results we want almost all of the
9563 time while leaving default_breakpoint_* alone.
9564
9565 ObjC: However, don't match an Objective-C method name which
9566 may have a '+' or '-' succeeded by a '['. */
9567 cursal = get_current_source_symtab_and_line ();
9568 if (last_displayed_sal_is_valid ())
9569 {
9570 const char *address = NULL;
9571
9572 if (event_location_type (location) == LINESPEC_LOCATION)
9573 address = get_linespec_location (location);
9574
9575 if (!cursal.symtab
9576 || (address != NULL
9577 && strchr ("+-", address[0]) != NULL
9578 && address[1] != '['))
9579 {
9580 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9581 get_last_displayed_symtab (),
9582 get_last_displayed_line (),
9583 canonical, NULL, NULL);
9584 return;
9585 }
9586 }
9587
9588 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9589 cursal.symtab, cursal.line, canonical, NULL, NULL);
9590 }
9591
9592
9593 /* Convert each SAL into a real PC. Verify that the PC can be
9594 inserted as a breakpoint. If it can't throw an error. */
9595
9596 static void
9597 breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
9598 {
9599 int i;
9600
9601 for (i = 0; i < sals->nelts; i++)
9602 resolve_sal_pc (&sals->sals[i]);
9603 }
9604
9605 /* Fast tracepoints may have restrictions on valid locations. For
9606 instance, a fast tracepoint using a jump instead of a trap will
9607 likely have to overwrite more bytes than a trap would, and so can
9608 only be placed where the instruction is longer than the jump, or a
9609 multi-instruction sequence does not have a jump into the middle of
9610 it, etc. */
9611
9612 static void
9613 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9614 struct symtabs_and_lines *sals)
9615 {
9616 int i, rslt;
9617 struct symtab_and_line *sal;
9618 char *msg;
9619 struct cleanup *old_chain;
9620
9621 for (i = 0; i < sals->nelts; i++)
9622 {
9623 struct gdbarch *sarch;
9624
9625 sal = &sals->sals[i];
9626
9627 sarch = get_sal_arch (*sal);
9628 /* We fall back to GDBARCH if there is no architecture
9629 associated with SAL. */
9630 if (sarch == NULL)
9631 sarch = gdbarch;
9632 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc, &msg);
9633 old_chain = make_cleanup (xfree, msg);
9634
9635 if (!rslt)
9636 error (_("May not have a fast tracepoint at %s%s"),
9637 paddress (sarch, sal->pc), (msg ? msg : ""));
9638
9639 do_cleanups (old_chain);
9640 }
9641 }
9642
9643 /* Given TOK, a string specification of condition and thread, as
9644 accepted by the 'break' command, extract the condition
9645 string and thread number and set *COND_STRING and *THREAD.
9646 PC identifies the context at which the condition should be parsed.
9647 If no condition is found, *COND_STRING is set to NULL.
9648 If no thread is found, *THREAD is set to -1. */
9649
9650 static void
9651 find_condition_and_thread (const char *tok, CORE_ADDR pc,
9652 char **cond_string, int *thread, int *task,
9653 char **rest)
9654 {
9655 *cond_string = NULL;
9656 *thread = -1;
9657 *task = 0;
9658 *rest = NULL;
9659
9660 while (tok && *tok)
9661 {
9662 const char *end_tok;
9663 int toklen;
9664 const char *cond_start = NULL;
9665 const char *cond_end = NULL;
9666
9667 tok = skip_spaces_const (tok);
9668
9669 if ((*tok == '"' || *tok == ',') && rest)
9670 {
9671 *rest = savestring (tok, strlen (tok));
9672 return;
9673 }
9674
9675 end_tok = skip_to_space_const (tok);
9676
9677 toklen = end_tok - tok;
9678
9679 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9680 {
9681 tok = cond_start = end_tok + 1;
9682 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
9683 cond_end = tok;
9684 *cond_string = savestring (cond_start, cond_end - cond_start);
9685 }
9686 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9687 {
9688 const char *tmptok;
9689 struct thread_info *thr;
9690
9691 tok = end_tok + 1;
9692 thr = parse_thread_id (tok, &tmptok);
9693 if (tok == tmptok)
9694 error (_("Junk after thread keyword."));
9695 *thread = thr->global_num;
9696 tok = tmptok;
9697 }
9698 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9699 {
9700 char *tmptok;
9701
9702 tok = end_tok + 1;
9703 *task = strtol (tok, &tmptok, 0);
9704 if (tok == tmptok)
9705 error (_("Junk after task keyword."));
9706 if (!valid_task_id (*task))
9707 error (_("Unknown task %d."), *task);
9708 tok = tmptok;
9709 }
9710 else if (rest)
9711 {
9712 *rest = savestring (tok, strlen (tok));
9713 return;
9714 }
9715 else
9716 error (_("Junk at end of arguments."));
9717 }
9718 }
9719
9720 /* Decode a static tracepoint marker spec. */
9721
9722 static struct symtabs_and_lines
9723 decode_static_tracepoint_spec (const char **arg_p)
9724 {
9725 VEC(static_tracepoint_marker_p) *markers = NULL;
9726 struct symtabs_and_lines sals;
9727 struct cleanup *old_chain;
9728 const char *p = &(*arg_p)[3];
9729 const char *endp;
9730 char *marker_str;
9731 int i;
9732
9733 p = skip_spaces_const (p);
9734
9735 endp = skip_to_space_const (p);
9736
9737 marker_str = savestring (p, endp - p);
9738 old_chain = make_cleanup (xfree, marker_str);
9739
9740 markers = target_static_tracepoint_markers_by_strid (marker_str);
9741 if (VEC_empty(static_tracepoint_marker_p, markers))
9742 error (_("No known static tracepoint marker named %s"), marker_str);
9743
9744 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
9745 sals.sals = XNEWVEC (struct symtab_and_line, sals.nelts);
9746
9747 for (i = 0; i < sals.nelts; i++)
9748 {
9749 struct static_tracepoint_marker *marker;
9750
9751 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9752
9753 init_sal (&sals.sals[i]);
9754
9755 sals.sals[i] = find_pc_line (marker->address, 0);
9756 sals.sals[i].pc = marker->address;
9757
9758 release_static_tracepoint_marker (marker);
9759 }
9760
9761 do_cleanups (old_chain);
9762
9763 *arg_p = endp;
9764 return sals;
9765 }
9766
9767 /* See breakpoint.h. */
9768
9769 int
9770 create_breakpoint (struct gdbarch *gdbarch,
9771 const struct event_location *location, char *cond_string,
9772 int thread, char *extra_string,
9773 int parse_extra,
9774 int tempflag, enum bptype type_wanted,
9775 int ignore_count,
9776 enum auto_boolean pending_break_support,
9777 const struct breakpoint_ops *ops,
9778 int from_tty, int enabled, int internal,
9779 unsigned flags)
9780 {
9781 struct linespec_result canonical;
9782 struct cleanup *old_chain;
9783 struct cleanup *bkpt_chain = NULL;
9784 int pending = 0;
9785 int task = 0;
9786 int prev_bkpt_count = breakpoint_count;
9787
9788 gdb_assert (ops != NULL);
9789
9790 /* If extra_string isn't useful, set it to NULL. */
9791 if (extra_string != NULL && *extra_string == '\0')
9792 extra_string = NULL;
9793
9794 init_linespec_result (&canonical);
9795
9796 TRY
9797 {
9798 ops->create_sals_from_location (location, &canonical, type_wanted);
9799 }
9800 CATCH (e, RETURN_MASK_ERROR)
9801 {
9802 /* If caller is interested in rc value from parse, set
9803 value. */
9804 if (e.error == NOT_FOUND_ERROR)
9805 {
9806 /* If pending breakpoint support is turned off, throw
9807 error. */
9808
9809 if (pending_break_support == AUTO_BOOLEAN_FALSE)
9810 throw_exception (e);
9811
9812 exception_print (gdb_stderr, e);
9813
9814 /* If pending breakpoint support is auto query and the user
9815 selects no, then simply return the error code. */
9816 if (pending_break_support == AUTO_BOOLEAN_AUTO
9817 && !nquery (_("Make %s pending on future shared library load? "),
9818 bptype_string (type_wanted)))
9819 return 0;
9820
9821 /* At this point, either the user was queried about setting
9822 a pending breakpoint and selected yes, or pending
9823 breakpoint behavior is on and thus a pending breakpoint
9824 is defaulted on behalf of the user. */
9825 pending = 1;
9826 }
9827 else
9828 throw_exception (e);
9829 }
9830 END_CATCH
9831
9832 if (!pending && VEC_empty (linespec_sals, canonical.sals))
9833 return 0;
9834
9835 /* Create a chain of things that always need to be cleaned up. */
9836 old_chain = make_cleanup_destroy_linespec_result (&canonical);
9837
9838 /* ----------------------------- SNIP -----------------------------
9839 Anything added to the cleanup chain beyond this point is assumed
9840 to be part of a breakpoint. If the breakpoint create succeeds
9841 then the memory is not reclaimed. */
9842 bkpt_chain = make_cleanup (null_cleanup, 0);
9843
9844 /* Resolve all line numbers to PC's and verify that the addresses
9845 are ok for the target. */
9846 if (!pending)
9847 {
9848 int ix;
9849 struct linespec_sals *iter;
9850
9851 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9852 breakpoint_sals_to_pc (&iter->sals);
9853 }
9854
9855 /* Fast tracepoints may have additional restrictions on location. */
9856 if (!pending && type_wanted == bp_fast_tracepoint)
9857 {
9858 int ix;
9859 struct linespec_sals *iter;
9860
9861 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9862 check_fast_tracepoint_sals (gdbarch, &iter->sals);
9863 }
9864
9865 /* Verify that condition can be parsed, before setting any
9866 breakpoints. Allocate a separate condition expression for each
9867 breakpoint. */
9868 if (!pending)
9869 {
9870 if (parse_extra)
9871 {
9872 char *rest;
9873 struct linespec_sals *lsal;
9874
9875 lsal = VEC_index (linespec_sals, canonical.sals, 0);
9876
9877 /* Here we only parse 'arg' to separate condition
9878 from thread number, so parsing in context of first
9879 sal is OK. When setting the breakpoint we'll
9880 re-parse it in context of each sal. */
9881
9882 find_condition_and_thread (extra_string, lsal->sals.sals[0].pc,
9883 &cond_string, &thread, &task, &rest);
9884 if (cond_string)
9885 make_cleanup (xfree, cond_string);
9886 if (rest)
9887 make_cleanup (xfree, rest);
9888 if (rest)
9889 extra_string = rest;
9890 else
9891 extra_string = NULL;
9892 }
9893 else
9894 {
9895 if (type_wanted != bp_dprintf
9896 && extra_string != NULL && *extra_string != '\0')
9897 error (_("Garbage '%s' at end of location"), extra_string);
9898
9899 /* Create a private copy of condition string. */
9900 if (cond_string)
9901 {
9902 cond_string = xstrdup (cond_string);
9903 make_cleanup (xfree, cond_string);
9904 }
9905 /* Create a private copy of any extra string. */
9906 if (extra_string)
9907 {
9908 extra_string = xstrdup (extra_string);
9909 make_cleanup (xfree, extra_string);
9910 }
9911 }
9912
9913 ops->create_breakpoints_sal (gdbarch, &canonical,
9914 cond_string, extra_string, type_wanted,
9915 tempflag ? disp_del : disp_donttouch,
9916 thread, task, ignore_count, ops,
9917 from_tty, enabled, internal, flags);
9918 }
9919 else
9920 {
9921 struct breakpoint *b;
9922
9923 if (is_tracepoint_type (type_wanted))
9924 {
9925 struct tracepoint *t;
9926
9927 t = new tracepoint ();
9928 b = &t->base;
9929 }
9930 else
9931 b = new breakpoint ();
9932
9933 init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops);
9934 b->location = copy_event_location (location);
9935
9936 if (parse_extra)
9937 b->cond_string = NULL;
9938 else
9939 {
9940 /* Create a private copy of condition string. */
9941 if (cond_string)
9942 {
9943 cond_string = xstrdup (cond_string);
9944 make_cleanup (xfree, cond_string);
9945 }
9946 b->cond_string = cond_string;
9947 b->thread = thread;
9948 }
9949
9950 /* Create a private copy of any extra string. */
9951 if (extra_string != NULL)
9952 {
9953 extra_string = xstrdup (extra_string);
9954 make_cleanup (xfree, extra_string);
9955 }
9956 b->extra_string = extra_string;
9957 b->ignore_count = ignore_count;
9958 b->disposition = tempflag ? disp_del : disp_donttouch;
9959 b->condition_not_parsed = 1;
9960 b->enable_state = enabled ? bp_enabled : bp_disabled;
9961 if ((type_wanted != bp_breakpoint
9962 && type_wanted != bp_hardware_breakpoint) || thread != -1)
9963 b->pspace = current_program_space;
9964
9965 install_breakpoint (internal, b, 0);
9966 }
9967
9968 if (VEC_length (linespec_sals, canonical.sals) > 1)
9969 {
9970 warning (_("Multiple breakpoints were set.\nUse the "
9971 "\"delete\" command to delete unwanted breakpoints."));
9972 prev_breakpoint_count = prev_bkpt_count;
9973 }
9974
9975 /* That's it. Discard the cleanups for data inserted into the
9976 breakpoint. */
9977 discard_cleanups (bkpt_chain);
9978 /* But cleanup everything else. */
9979 do_cleanups (old_chain);
9980
9981 /* error call may happen here - have BKPT_CHAIN already discarded. */
9982 update_global_location_list (UGLL_MAY_INSERT);
9983
9984 return 1;
9985 }
9986
9987 /* Set a breakpoint.
9988 ARG is a string describing breakpoint address,
9989 condition, and thread.
9990 FLAG specifies if a breakpoint is hardware on,
9991 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9992 and BP_TEMPFLAG. */
9993
9994 static void
9995 break_command_1 (char *arg, int flag, int from_tty)
9996 {
9997 int tempflag = flag & BP_TEMPFLAG;
9998 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9999 ? bp_hardware_breakpoint
10000 : bp_breakpoint);
10001 struct breakpoint_ops *ops;
10002 struct event_location *location;
10003 struct cleanup *cleanup;
10004
10005 location = string_to_event_location (&arg, current_language);
10006 cleanup = make_cleanup_delete_event_location (location);
10007
10008 /* Matching breakpoints on probes. */
10009 if (location != NULL
10010 && event_location_type (location) == PROBE_LOCATION)
10011 ops = &bkpt_probe_breakpoint_ops;
10012 else
10013 ops = &bkpt_breakpoint_ops;
10014
10015 create_breakpoint (get_current_arch (),
10016 location,
10017 NULL, 0, arg, 1 /* parse arg */,
10018 tempflag, type_wanted,
10019 0 /* Ignore count */,
10020 pending_break_support,
10021 ops,
10022 from_tty,
10023 1 /* enabled */,
10024 0 /* internal */,
10025 0);
10026 do_cleanups (cleanup);
10027 }
10028
10029 /* Helper function for break_command_1 and disassemble_command. */
10030
10031 void
10032 resolve_sal_pc (struct symtab_and_line *sal)
10033 {
10034 CORE_ADDR pc;
10035
10036 if (sal->pc == 0 && sal->symtab != NULL)
10037 {
10038 if (!find_line_pc (sal->symtab, sal->line, &pc))
10039 error (_("No line %d in file \"%s\"."),
10040 sal->line, symtab_to_filename_for_display (sal->symtab));
10041 sal->pc = pc;
10042
10043 /* If this SAL corresponds to a breakpoint inserted using a line
10044 number, then skip the function prologue if necessary. */
10045 if (sal->explicit_line)
10046 skip_prologue_sal (sal);
10047 }
10048
10049 if (sal->section == 0 && sal->symtab != NULL)
10050 {
10051 const struct blockvector *bv;
10052 const struct block *b;
10053 struct symbol *sym;
10054
10055 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
10056 SYMTAB_COMPUNIT (sal->symtab));
10057 if (bv != NULL)
10058 {
10059 sym = block_linkage_function (b);
10060 if (sym != NULL)
10061 {
10062 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
10063 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
10064 sym);
10065 }
10066 else
10067 {
10068 /* It really is worthwhile to have the section, so we'll
10069 just have to look harder. This case can be executed
10070 if we have line numbers but no functions (as can
10071 happen in assembly source). */
10072
10073 struct bound_minimal_symbol msym;
10074 struct cleanup *old_chain = save_current_space_and_thread ();
10075
10076 switch_to_program_space_and_thread (sal->pspace);
10077
10078 msym = lookup_minimal_symbol_by_pc (sal->pc);
10079 if (msym.minsym)
10080 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
10081
10082 do_cleanups (old_chain);
10083 }
10084 }
10085 }
10086 }
10087
10088 void
10089 break_command (char *arg, int from_tty)
10090 {
10091 break_command_1 (arg, 0, from_tty);
10092 }
10093
10094 void
10095 tbreak_command (char *arg, int from_tty)
10096 {
10097 break_command_1 (arg, BP_TEMPFLAG, from_tty);
10098 }
10099
10100 static void
10101 hbreak_command (char *arg, int from_tty)
10102 {
10103 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
10104 }
10105
10106 static void
10107 thbreak_command (char *arg, int from_tty)
10108 {
10109 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
10110 }
10111
10112 static void
10113 stop_command (char *arg, int from_tty)
10114 {
10115 printf_filtered (_("Specify the type of breakpoint to set.\n\
10116 Usage: stop in <function | address>\n\
10117 stop at <line>\n"));
10118 }
10119
10120 static void
10121 stopin_command (char *arg, int from_tty)
10122 {
10123 int badInput = 0;
10124
10125 if (arg == (char *) NULL)
10126 badInput = 1;
10127 else if (*arg != '*')
10128 {
10129 char *argptr = arg;
10130 int hasColon = 0;
10131
10132 /* Look for a ':'. If this is a line number specification, then
10133 say it is bad, otherwise, it should be an address or
10134 function/method name. */
10135 while (*argptr && !hasColon)
10136 {
10137 hasColon = (*argptr == ':');
10138 argptr++;
10139 }
10140
10141 if (hasColon)
10142 badInput = (*argptr != ':'); /* Not a class::method */
10143 else
10144 badInput = isdigit (*arg); /* a simple line number */
10145 }
10146
10147 if (badInput)
10148 printf_filtered (_("Usage: stop in <function | address>\n"));
10149 else
10150 break_command_1 (arg, 0, from_tty);
10151 }
10152
10153 static void
10154 stopat_command (char *arg, int from_tty)
10155 {
10156 int badInput = 0;
10157
10158 if (arg == (char *) NULL || *arg == '*') /* no line number */
10159 badInput = 1;
10160 else
10161 {
10162 char *argptr = arg;
10163 int hasColon = 0;
10164
10165 /* Look for a ':'. If there is a '::' then get out, otherwise
10166 it is probably a line number. */
10167 while (*argptr && !hasColon)
10168 {
10169 hasColon = (*argptr == ':');
10170 argptr++;
10171 }
10172
10173 if (hasColon)
10174 badInput = (*argptr == ':'); /* we have class::method */
10175 else
10176 badInput = !isdigit (*arg); /* not a line number */
10177 }
10178
10179 if (badInput)
10180 printf_filtered (_("Usage: stop at <line>\n"));
10181 else
10182 break_command_1 (arg, 0, from_tty);
10183 }
10184
10185 /* The dynamic printf command is mostly like a regular breakpoint, but
10186 with a prewired command list consisting of a single output command,
10187 built from extra arguments supplied on the dprintf command
10188 line. */
10189
10190 static void
10191 dprintf_command (char *arg, int from_tty)
10192 {
10193 struct event_location *location;
10194 struct cleanup *cleanup;
10195
10196 location = string_to_event_location (&arg, current_language);
10197 cleanup = make_cleanup_delete_event_location (location);
10198
10199 /* If non-NULL, ARG should have been advanced past the location;
10200 the next character must be ','. */
10201 if (arg != NULL)
10202 {
10203 if (arg[0] != ',' || arg[1] == '\0')
10204 error (_("Format string required"));
10205 else
10206 {
10207 /* Skip the comma. */
10208 ++arg;
10209 }
10210 }
10211
10212 create_breakpoint (get_current_arch (),
10213 location,
10214 NULL, 0, arg, 1 /* parse arg */,
10215 0, bp_dprintf,
10216 0 /* Ignore count */,
10217 pending_break_support,
10218 &dprintf_breakpoint_ops,
10219 from_tty,
10220 1 /* enabled */,
10221 0 /* internal */,
10222 0);
10223 do_cleanups (cleanup);
10224 }
10225
10226 static void
10227 agent_printf_command (char *arg, int from_tty)
10228 {
10229 error (_("May only run agent-printf on the target"));
10230 }
10231
10232 /* Implement the "breakpoint_hit" breakpoint_ops method for
10233 ranged breakpoints. */
10234
10235 static int
10236 breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
10237 struct address_space *aspace,
10238 CORE_ADDR bp_addr,
10239 const struct target_waitstatus *ws)
10240 {
10241 if (ws->kind != TARGET_WAITKIND_STOPPED
10242 || ws->value.sig != GDB_SIGNAL_TRAP)
10243 return 0;
10244
10245 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
10246 bl->length, aspace, bp_addr);
10247 }
10248
10249 /* Implement the "resources_needed" breakpoint_ops method for
10250 ranged breakpoints. */
10251
10252 static int
10253 resources_needed_ranged_breakpoint (const struct bp_location *bl)
10254 {
10255 return target_ranged_break_num_registers ();
10256 }
10257
10258 /* Implement the "print_it" breakpoint_ops method for
10259 ranged breakpoints. */
10260
10261 static enum print_stop_action
10262 print_it_ranged_breakpoint (bpstat bs)
10263 {
10264 struct breakpoint *b = bs->breakpoint_at;
10265 struct bp_location *bl = b->loc;
10266 struct ui_out *uiout = current_uiout;
10267
10268 gdb_assert (b->type == bp_hardware_breakpoint);
10269
10270 /* Ranged breakpoints have only one location. */
10271 gdb_assert (bl && bl->next == NULL);
10272
10273 annotate_breakpoint (b->number);
10274
10275 maybe_print_thread_hit_breakpoint (uiout);
10276
10277 if (b->disposition == disp_del)
10278 ui_out_text (uiout, "Temporary ranged breakpoint ");
10279 else
10280 ui_out_text (uiout, "Ranged breakpoint ");
10281 if (ui_out_is_mi_like_p (uiout))
10282 {
10283 ui_out_field_string (uiout, "reason",
10284 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
10285 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
10286 }
10287 ui_out_field_int (uiout, "bkptno", b->number);
10288 ui_out_text (uiout, ", ");
10289
10290 return PRINT_SRC_AND_LOC;
10291 }
10292
10293 /* Implement the "print_one" breakpoint_ops method for
10294 ranged breakpoints. */
10295
10296 static void
10297 print_one_ranged_breakpoint (struct breakpoint *b,
10298 struct bp_location **last_loc)
10299 {
10300 struct bp_location *bl = b->loc;
10301 struct value_print_options opts;
10302 struct ui_out *uiout = current_uiout;
10303
10304 /* Ranged breakpoints have only one location. */
10305 gdb_assert (bl && bl->next == NULL);
10306
10307 get_user_print_options (&opts);
10308
10309 if (opts.addressprint)
10310 /* We don't print the address range here, it will be printed later
10311 by print_one_detail_ranged_breakpoint. */
10312 ui_out_field_skip (uiout, "addr");
10313 annotate_field (5);
10314 print_breakpoint_location (b, bl);
10315 *last_loc = bl;
10316 }
10317
10318 /* Implement the "print_one_detail" breakpoint_ops method for
10319 ranged breakpoints. */
10320
10321 static void
10322 print_one_detail_ranged_breakpoint (const struct breakpoint *b,
10323 struct ui_out *uiout)
10324 {
10325 CORE_ADDR address_start, address_end;
10326 struct bp_location *bl = b->loc;
10327 struct ui_file *stb = mem_fileopen ();
10328 struct cleanup *cleanup = make_cleanup_ui_file_delete (stb);
10329
10330 gdb_assert (bl);
10331
10332 address_start = bl->address;
10333 address_end = address_start + bl->length - 1;
10334
10335 ui_out_text (uiout, "\taddress range: ");
10336 fprintf_unfiltered (stb, "[%s, %s]",
10337 print_core_address (bl->gdbarch, address_start),
10338 print_core_address (bl->gdbarch, address_end));
10339 ui_out_field_stream (uiout, "addr", stb);
10340 ui_out_text (uiout, "\n");
10341
10342 do_cleanups (cleanup);
10343 }
10344
10345 /* Implement the "print_mention" breakpoint_ops method for
10346 ranged breakpoints. */
10347
10348 static void
10349 print_mention_ranged_breakpoint (struct breakpoint *b)
10350 {
10351 struct bp_location *bl = b->loc;
10352 struct ui_out *uiout = current_uiout;
10353
10354 gdb_assert (bl);
10355 gdb_assert (b->type == bp_hardware_breakpoint);
10356
10357 if (ui_out_is_mi_like_p (uiout))
10358 return;
10359
10360 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
10361 b->number, paddress (bl->gdbarch, bl->address),
10362 paddress (bl->gdbarch, bl->address + bl->length - 1));
10363 }
10364
10365 /* Implement the "print_recreate" breakpoint_ops method for
10366 ranged breakpoints. */
10367
10368 static void
10369 print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
10370 {
10371 fprintf_unfiltered (fp, "break-range %s, %s",
10372 event_location_to_string (b->location),
10373 event_location_to_string (b->location_range_end));
10374 print_recreate_thread (b, fp);
10375 }
10376
10377 /* The breakpoint_ops structure to be used in ranged breakpoints. */
10378
10379 static struct breakpoint_ops ranged_breakpoint_ops;
10380
10381 /* Find the address where the end of the breakpoint range should be
10382 placed, given the SAL of the end of the range. This is so that if
10383 the user provides a line number, the end of the range is set to the
10384 last instruction of the given line. */
10385
10386 static CORE_ADDR
10387 find_breakpoint_range_end (struct symtab_and_line sal)
10388 {
10389 CORE_ADDR end;
10390
10391 /* If the user provided a PC value, use it. Otherwise,
10392 find the address of the end of the given location. */
10393 if (sal.explicit_pc)
10394 end = sal.pc;
10395 else
10396 {
10397 int ret;
10398 CORE_ADDR start;
10399
10400 ret = find_line_pc_range (sal, &start, &end);
10401 if (!ret)
10402 error (_("Could not find location of the end of the range."));
10403
10404 /* find_line_pc_range returns the start of the next line. */
10405 end--;
10406 }
10407
10408 return end;
10409 }
10410
10411 /* Implement the "break-range" CLI command. */
10412
10413 static void
10414 break_range_command (char *arg, int from_tty)
10415 {
10416 char *arg_start, *addr_string_start;
10417 struct linespec_result canonical_start, canonical_end;
10418 int bp_count, can_use_bp, length;
10419 CORE_ADDR end;
10420 struct breakpoint *b;
10421 struct symtab_and_line sal_start, sal_end;
10422 struct cleanup *cleanup_bkpt;
10423 struct linespec_sals *lsal_start, *lsal_end;
10424 struct event_location *start_location, *end_location;
10425
10426 /* We don't support software ranged breakpoints. */
10427 if (target_ranged_break_num_registers () < 0)
10428 error (_("This target does not support hardware ranged breakpoints."));
10429
10430 bp_count = hw_breakpoint_used_count ();
10431 bp_count += target_ranged_break_num_registers ();
10432 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10433 bp_count, 0);
10434 if (can_use_bp < 0)
10435 error (_("Hardware breakpoints used exceeds limit."));
10436
10437 arg = skip_spaces (arg);
10438 if (arg == NULL || arg[0] == '\0')
10439 error(_("No address range specified."));
10440
10441 init_linespec_result (&canonical_start);
10442
10443 arg_start = arg;
10444 start_location = string_to_event_location (&arg, current_language);
10445 cleanup_bkpt = make_cleanup_delete_event_location (start_location);
10446 parse_breakpoint_sals (start_location, &canonical_start);
10447 make_cleanup_destroy_linespec_result (&canonical_start);
10448
10449 if (arg[0] != ',')
10450 error (_("Too few arguments."));
10451 else if (VEC_empty (linespec_sals, canonical_start.sals))
10452 error (_("Could not find location of the beginning of the range."));
10453
10454 lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
10455
10456 if (VEC_length (linespec_sals, canonical_start.sals) > 1
10457 || lsal_start->sals.nelts != 1)
10458 error (_("Cannot create a ranged breakpoint with multiple locations."));
10459
10460 sal_start = lsal_start->sals.sals[0];
10461 addr_string_start = savestring (arg_start, arg - arg_start);
10462 make_cleanup (xfree, addr_string_start);
10463
10464 arg++; /* Skip the comma. */
10465 arg = skip_spaces (arg);
10466
10467 /* Parse the end location. */
10468
10469 init_linespec_result (&canonical_end);
10470 arg_start = arg;
10471
10472 /* We call decode_line_full directly here instead of using
10473 parse_breakpoint_sals because we need to specify the start location's
10474 symtab and line as the default symtab and line for the end of the
10475 range. This makes it possible to have ranges like "foo.c:27, +14",
10476 where +14 means 14 lines from the start location. */
10477 end_location = string_to_event_location (&arg, current_language);
10478 make_cleanup_delete_event_location (end_location);
10479 decode_line_full (end_location, DECODE_LINE_FUNFIRSTLINE, NULL,
10480 sal_start.symtab, sal_start.line,
10481 &canonical_end, NULL, NULL);
10482
10483 make_cleanup_destroy_linespec_result (&canonical_end);
10484
10485 if (VEC_empty (linespec_sals, canonical_end.sals))
10486 error (_("Could not find location of the end of the range."));
10487
10488 lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
10489 if (VEC_length (linespec_sals, canonical_end.sals) > 1
10490 || lsal_end->sals.nelts != 1)
10491 error (_("Cannot create a ranged breakpoint with multiple locations."));
10492
10493 sal_end = lsal_end->sals.sals[0];
10494
10495 end = find_breakpoint_range_end (sal_end);
10496 if (sal_start.pc > end)
10497 error (_("Invalid address range, end precedes start."));
10498
10499 length = end - sal_start.pc + 1;
10500 if (length < 0)
10501 /* Length overflowed. */
10502 error (_("Address range too large."));
10503 else if (length == 1)
10504 {
10505 /* This range is simple enough to be handled by
10506 the `hbreak' command. */
10507 hbreak_command (addr_string_start, 1);
10508
10509 do_cleanups (cleanup_bkpt);
10510
10511 return;
10512 }
10513
10514 /* Now set up the breakpoint. */
10515 b = set_raw_breakpoint (get_current_arch (), sal_start,
10516 bp_hardware_breakpoint, &ranged_breakpoint_ops);
10517 set_breakpoint_count (breakpoint_count + 1);
10518 b->number = breakpoint_count;
10519 b->disposition = disp_donttouch;
10520 b->location = copy_event_location (start_location);
10521 b->location_range_end = copy_event_location (end_location);
10522 b->loc->length = length;
10523
10524 do_cleanups (cleanup_bkpt);
10525
10526 mention (b);
10527 observer_notify_breakpoint_created (b);
10528 update_global_location_list (UGLL_MAY_INSERT);
10529 }
10530
10531 /* Return non-zero if EXP is verified as constant. Returned zero
10532 means EXP is variable. Also the constant detection may fail for
10533 some constant expressions and in such case still falsely return
10534 zero. */
10535
10536 static int
10537 watchpoint_exp_is_const (const struct expression *exp)
10538 {
10539 int i = exp->nelts;
10540
10541 while (i > 0)
10542 {
10543 int oplenp, argsp;
10544
10545 /* We are only interested in the descriptor of each element. */
10546 operator_length (exp, i, &oplenp, &argsp);
10547 i -= oplenp;
10548
10549 switch (exp->elts[i].opcode)
10550 {
10551 case BINOP_ADD:
10552 case BINOP_SUB:
10553 case BINOP_MUL:
10554 case BINOP_DIV:
10555 case BINOP_REM:
10556 case BINOP_MOD:
10557 case BINOP_LSH:
10558 case BINOP_RSH:
10559 case BINOP_LOGICAL_AND:
10560 case BINOP_LOGICAL_OR:
10561 case BINOP_BITWISE_AND:
10562 case BINOP_BITWISE_IOR:
10563 case BINOP_BITWISE_XOR:
10564 case BINOP_EQUAL:
10565 case BINOP_NOTEQUAL:
10566 case BINOP_LESS:
10567 case BINOP_GTR:
10568 case BINOP_LEQ:
10569 case BINOP_GEQ:
10570 case BINOP_REPEAT:
10571 case BINOP_COMMA:
10572 case BINOP_EXP:
10573 case BINOP_MIN:
10574 case BINOP_MAX:
10575 case BINOP_INTDIV:
10576 case BINOP_CONCAT:
10577 case TERNOP_COND:
10578 case TERNOP_SLICE:
10579
10580 case OP_LONG:
10581 case OP_DOUBLE:
10582 case OP_DECFLOAT:
10583 case OP_LAST:
10584 case OP_COMPLEX:
10585 case OP_STRING:
10586 case OP_ARRAY:
10587 case OP_TYPE:
10588 case OP_TYPEOF:
10589 case OP_DECLTYPE:
10590 case OP_TYPEID:
10591 case OP_NAME:
10592 case OP_OBJC_NSSTRING:
10593
10594 case UNOP_NEG:
10595 case UNOP_LOGICAL_NOT:
10596 case UNOP_COMPLEMENT:
10597 case UNOP_ADDR:
10598 case UNOP_HIGH:
10599 case UNOP_CAST:
10600
10601 case UNOP_CAST_TYPE:
10602 case UNOP_REINTERPRET_CAST:
10603 case UNOP_DYNAMIC_CAST:
10604 /* Unary, binary and ternary operators: We have to check
10605 their operands. If they are constant, then so is the
10606 result of that operation. For instance, if A and B are
10607 determined to be constants, then so is "A + B".
10608
10609 UNOP_IND is one exception to the rule above, because the
10610 value of *ADDR is not necessarily a constant, even when
10611 ADDR is. */
10612 break;
10613
10614 case OP_VAR_VALUE:
10615 /* Check whether the associated symbol is a constant.
10616
10617 We use SYMBOL_CLASS rather than TYPE_CONST because it's
10618 possible that a buggy compiler could mark a variable as
10619 constant even when it is not, and TYPE_CONST would return
10620 true in this case, while SYMBOL_CLASS wouldn't.
10621
10622 We also have to check for function symbols because they
10623 are always constant. */
10624 {
10625 struct symbol *s = exp->elts[i + 2].symbol;
10626
10627 if (SYMBOL_CLASS (s) != LOC_BLOCK
10628 && SYMBOL_CLASS (s) != LOC_CONST
10629 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10630 return 0;
10631 break;
10632 }
10633
10634 /* The default action is to return 0 because we are using
10635 the optimistic approach here: If we don't know something,
10636 then it is not a constant. */
10637 default:
10638 return 0;
10639 }
10640 }
10641
10642 return 1;
10643 }
10644
10645 /* Implement the "dtor" breakpoint_ops method for watchpoints. */
10646
10647 static void
10648 dtor_watchpoint (struct breakpoint *self)
10649 {
10650 struct watchpoint *w = (struct watchpoint *) self;
10651
10652 xfree (w->exp_string);
10653 xfree (w->exp_string_reparse);
10654 value_free (w->val);
10655
10656 base_breakpoint_ops.dtor (self);
10657 }
10658
10659 /* Implement the "re_set" breakpoint_ops method for watchpoints. */
10660
10661 static void
10662 re_set_watchpoint (struct breakpoint *b)
10663 {
10664 struct watchpoint *w = (struct watchpoint *) b;
10665
10666 /* Watchpoint can be either on expression using entirely global
10667 variables, or it can be on local variables.
10668
10669 Watchpoints of the first kind are never auto-deleted, and even
10670 persist across program restarts. Since they can use variables
10671 from shared libraries, we need to reparse expression as libraries
10672 are loaded and unloaded.
10673
10674 Watchpoints on local variables can also change meaning as result
10675 of solib event. For example, if a watchpoint uses both a local
10676 and a global variables in expression, it's a local watchpoint,
10677 but unloading of a shared library will make the expression
10678 invalid. This is not a very common use case, but we still
10679 re-evaluate expression, to avoid surprises to the user.
10680
10681 Note that for local watchpoints, we re-evaluate it only if
10682 watchpoints frame id is still valid. If it's not, it means the
10683 watchpoint is out of scope and will be deleted soon. In fact,
10684 I'm not sure we'll ever be called in this case.
10685
10686 If a local watchpoint's frame id is still valid, then
10687 w->exp_valid_block is likewise valid, and we can safely use it.
10688
10689 Don't do anything about disabled watchpoints, since they will be
10690 reevaluated again when enabled. */
10691 update_watchpoint (w, 1 /* reparse */);
10692 }
10693
10694 /* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10695
10696 static int
10697 insert_watchpoint (struct bp_location *bl)
10698 {
10699 struct watchpoint *w = (struct watchpoint *) bl->owner;
10700 int length = w->exact ? 1 : bl->length;
10701
10702 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
10703 w->cond_exp.get ());
10704 }
10705
10706 /* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10707
10708 static int
10709 remove_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10710 {
10711 struct watchpoint *w = (struct watchpoint *) bl->owner;
10712 int length = w->exact ? 1 : bl->length;
10713
10714 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
10715 w->cond_exp.get ());
10716 }
10717
10718 static int
10719 breakpoint_hit_watchpoint (const struct bp_location *bl,
10720 struct address_space *aspace, CORE_ADDR bp_addr,
10721 const struct target_waitstatus *ws)
10722 {
10723 struct breakpoint *b = bl->owner;
10724 struct watchpoint *w = (struct watchpoint *) b;
10725
10726 /* Continuable hardware watchpoints are treated as non-existent if the
10727 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10728 some data address). Otherwise gdb won't stop on a break instruction
10729 in the code (not from a breakpoint) when a hardware watchpoint has
10730 been defined. Also skip watchpoints which we know did not trigger
10731 (did not match the data address). */
10732 if (is_hardware_watchpoint (b)
10733 && w->watchpoint_triggered == watch_triggered_no)
10734 return 0;
10735
10736 return 1;
10737 }
10738
10739 static void
10740 check_status_watchpoint (bpstat bs)
10741 {
10742 gdb_assert (is_watchpoint (bs->breakpoint_at));
10743
10744 bpstat_check_watchpoint (bs);
10745 }
10746
10747 /* Implement the "resources_needed" breakpoint_ops method for
10748 hardware watchpoints. */
10749
10750 static int
10751 resources_needed_watchpoint (const struct bp_location *bl)
10752 {
10753 struct watchpoint *w = (struct watchpoint *) bl->owner;
10754 int length = w->exact? 1 : bl->length;
10755
10756 return target_region_ok_for_hw_watchpoint (bl->address, length);
10757 }
10758
10759 /* Implement the "works_in_software_mode" breakpoint_ops method for
10760 hardware watchpoints. */
10761
10762 static int
10763 works_in_software_mode_watchpoint (const struct breakpoint *b)
10764 {
10765 /* Read and access watchpoints only work with hardware support. */
10766 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
10767 }
10768
10769 static enum print_stop_action
10770 print_it_watchpoint (bpstat bs)
10771 {
10772 struct cleanup *old_chain;
10773 struct breakpoint *b;
10774 struct ui_file *stb;
10775 enum print_stop_action result;
10776 struct watchpoint *w;
10777 struct ui_out *uiout = current_uiout;
10778
10779 gdb_assert (bs->bp_location_at != NULL);
10780
10781 b = bs->breakpoint_at;
10782 w = (struct watchpoint *) b;
10783
10784 stb = mem_fileopen ();
10785 old_chain = make_cleanup_ui_file_delete (stb);
10786
10787 annotate_watchpoint (b->number);
10788 maybe_print_thread_hit_breakpoint (uiout);
10789
10790 switch (b->type)
10791 {
10792 case bp_watchpoint:
10793 case bp_hardware_watchpoint:
10794 if (ui_out_is_mi_like_p (uiout))
10795 ui_out_field_string
10796 (uiout, "reason",
10797 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10798 mention (b);
10799 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10800 ui_out_text (uiout, "\nOld value = ");
10801 watchpoint_value_print (bs->old_val, stb);
10802 ui_out_field_stream (uiout, "old", stb);
10803 ui_out_text (uiout, "\nNew value = ");
10804 watchpoint_value_print (w->val, stb);
10805 ui_out_field_stream (uiout, "new", stb);
10806 ui_out_text (uiout, "\n");
10807 /* More than one watchpoint may have been triggered. */
10808 result = PRINT_UNKNOWN;
10809 break;
10810
10811 case bp_read_watchpoint:
10812 if (ui_out_is_mi_like_p (uiout))
10813 ui_out_field_string
10814 (uiout, "reason",
10815 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10816 mention (b);
10817 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10818 ui_out_text (uiout, "\nValue = ");
10819 watchpoint_value_print (w->val, stb);
10820 ui_out_field_stream (uiout, "value", stb);
10821 ui_out_text (uiout, "\n");
10822 result = PRINT_UNKNOWN;
10823 break;
10824
10825 case bp_access_watchpoint:
10826 if (bs->old_val != NULL)
10827 {
10828 if (ui_out_is_mi_like_p (uiout))
10829 ui_out_field_string
10830 (uiout, "reason",
10831 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10832 mention (b);
10833 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10834 ui_out_text (uiout, "\nOld value = ");
10835 watchpoint_value_print (bs->old_val, stb);
10836 ui_out_field_stream (uiout, "old", stb);
10837 ui_out_text (uiout, "\nNew value = ");
10838 }
10839 else
10840 {
10841 mention (b);
10842 if (ui_out_is_mi_like_p (uiout))
10843 ui_out_field_string
10844 (uiout, "reason",
10845 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10846 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10847 ui_out_text (uiout, "\nValue = ");
10848 }
10849 watchpoint_value_print (w->val, stb);
10850 ui_out_field_stream (uiout, "new", stb);
10851 ui_out_text (uiout, "\n");
10852 result = PRINT_UNKNOWN;
10853 break;
10854 default:
10855 result = PRINT_UNKNOWN;
10856 }
10857
10858 do_cleanups (old_chain);
10859 return result;
10860 }
10861
10862 /* Implement the "print_mention" breakpoint_ops method for hardware
10863 watchpoints. */
10864
10865 static void
10866 print_mention_watchpoint (struct breakpoint *b)
10867 {
10868 struct cleanup *ui_out_chain;
10869 struct watchpoint *w = (struct watchpoint *) b;
10870 struct ui_out *uiout = current_uiout;
10871
10872 switch (b->type)
10873 {
10874 case bp_watchpoint:
10875 ui_out_text (uiout, "Watchpoint ");
10876 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10877 break;
10878 case bp_hardware_watchpoint:
10879 ui_out_text (uiout, "Hardware watchpoint ");
10880 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10881 break;
10882 case bp_read_watchpoint:
10883 ui_out_text (uiout, "Hardware read watchpoint ");
10884 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
10885 break;
10886 case bp_access_watchpoint:
10887 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
10888 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
10889 break;
10890 default:
10891 internal_error (__FILE__, __LINE__,
10892 _("Invalid hardware watchpoint type."));
10893 }
10894
10895 ui_out_field_int (uiout, "number", b->number);
10896 ui_out_text (uiout, ": ");
10897 ui_out_field_string (uiout, "exp", w->exp_string);
10898 do_cleanups (ui_out_chain);
10899 }
10900
10901 /* Implement the "print_recreate" breakpoint_ops method for
10902 watchpoints. */
10903
10904 static void
10905 print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10906 {
10907 struct watchpoint *w = (struct watchpoint *) b;
10908
10909 switch (b->type)
10910 {
10911 case bp_watchpoint:
10912 case bp_hardware_watchpoint:
10913 fprintf_unfiltered (fp, "watch");
10914 break;
10915 case bp_read_watchpoint:
10916 fprintf_unfiltered (fp, "rwatch");
10917 break;
10918 case bp_access_watchpoint:
10919 fprintf_unfiltered (fp, "awatch");
10920 break;
10921 default:
10922 internal_error (__FILE__, __LINE__,
10923 _("Invalid watchpoint type."));
10924 }
10925
10926 fprintf_unfiltered (fp, " %s", w->exp_string);
10927 print_recreate_thread (b, fp);
10928 }
10929
10930 /* Implement the "explains_signal" breakpoint_ops method for
10931 watchpoints. */
10932
10933 static int
10934 explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10935 {
10936 /* A software watchpoint cannot cause a signal other than
10937 GDB_SIGNAL_TRAP. */
10938 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
10939 return 0;
10940
10941 return 1;
10942 }
10943
10944 /* The breakpoint_ops structure to be used in hardware watchpoints. */
10945
10946 static struct breakpoint_ops watchpoint_breakpoint_ops;
10947
10948 /* Implement the "insert" breakpoint_ops method for
10949 masked hardware watchpoints. */
10950
10951 static int
10952 insert_masked_watchpoint (struct bp_location *bl)
10953 {
10954 struct watchpoint *w = (struct watchpoint *) bl->owner;
10955
10956 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
10957 bl->watchpoint_type);
10958 }
10959
10960 /* Implement the "remove" breakpoint_ops method for
10961 masked hardware watchpoints. */
10962
10963 static int
10964 remove_masked_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10965 {
10966 struct watchpoint *w = (struct watchpoint *) bl->owner;
10967
10968 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
10969 bl->watchpoint_type);
10970 }
10971
10972 /* Implement the "resources_needed" breakpoint_ops method for
10973 masked hardware watchpoints. */
10974
10975 static int
10976 resources_needed_masked_watchpoint (const struct bp_location *bl)
10977 {
10978 struct watchpoint *w = (struct watchpoint *) bl->owner;
10979
10980 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
10981 }
10982
10983 /* Implement the "works_in_software_mode" breakpoint_ops method for
10984 masked hardware watchpoints. */
10985
10986 static int
10987 works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10988 {
10989 return 0;
10990 }
10991
10992 /* Implement the "print_it" breakpoint_ops method for
10993 masked hardware watchpoints. */
10994
10995 static enum print_stop_action
10996 print_it_masked_watchpoint (bpstat bs)
10997 {
10998 struct breakpoint *b = bs->breakpoint_at;
10999 struct ui_out *uiout = current_uiout;
11000
11001 /* Masked watchpoints have only one location. */
11002 gdb_assert (b->loc && b->loc->next == NULL);
11003
11004 annotate_watchpoint (b->number);
11005 maybe_print_thread_hit_breakpoint (uiout);
11006
11007 switch (b->type)
11008 {
11009 case bp_hardware_watchpoint:
11010 if (ui_out_is_mi_like_p (uiout))
11011 ui_out_field_string
11012 (uiout, "reason",
11013 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
11014 break;
11015
11016 case bp_read_watchpoint:
11017 if (ui_out_is_mi_like_p (uiout))
11018 ui_out_field_string
11019 (uiout, "reason",
11020 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
11021 break;
11022
11023 case bp_access_watchpoint:
11024 if (ui_out_is_mi_like_p (uiout))
11025 ui_out_field_string
11026 (uiout, "reason",
11027 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
11028 break;
11029 default:
11030 internal_error (__FILE__, __LINE__,
11031 _("Invalid hardware watchpoint type."));
11032 }
11033
11034 mention (b);
11035 ui_out_text (uiout, _("\n\
11036 Check the underlying instruction at PC for the memory\n\
11037 address and value which triggered this watchpoint.\n"));
11038 ui_out_text (uiout, "\n");
11039
11040 /* More than one watchpoint may have been triggered. */
11041 return PRINT_UNKNOWN;
11042 }
11043
11044 /* Implement the "print_one_detail" breakpoint_ops method for
11045 masked hardware watchpoints. */
11046
11047 static void
11048 print_one_detail_masked_watchpoint (const struct breakpoint *b,
11049 struct ui_out *uiout)
11050 {
11051 struct watchpoint *w = (struct watchpoint *) b;
11052
11053 /* Masked watchpoints have only one location. */
11054 gdb_assert (b->loc && b->loc->next == NULL);
11055
11056 ui_out_text (uiout, "\tmask ");
11057 ui_out_field_core_addr (uiout, "mask", b->loc->gdbarch, w->hw_wp_mask);
11058 ui_out_text (uiout, "\n");
11059 }
11060
11061 /* Implement the "print_mention" breakpoint_ops method for
11062 masked hardware watchpoints. */
11063
11064 static void
11065 print_mention_masked_watchpoint (struct breakpoint *b)
11066 {
11067 struct watchpoint *w = (struct watchpoint *) b;
11068 struct ui_out *uiout = current_uiout;
11069 struct cleanup *ui_out_chain;
11070
11071 switch (b->type)
11072 {
11073 case bp_hardware_watchpoint:
11074 ui_out_text (uiout, "Masked hardware watchpoint ");
11075 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
11076 break;
11077 case bp_read_watchpoint:
11078 ui_out_text (uiout, "Masked hardware read watchpoint ");
11079 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
11080 break;
11081 case bp_access_watchpoint:
11082 ui_out_text (uiout, "Masked hardware access (read/write) watchpoint ");
11083 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
11084 break;
11085 default:
11086 internal_error (__FILE__, __LINE__,
11087 _("Invalid hardware watchpoint type."));
11088 }
11089
11090 ui_out_field_int (uiout, "number", b->number);
11091 ui_out_text (uiout, ": ");
11092 ui_out_field_string (uiout, "exp", w->exp_string);
11093 do_cleanups (ui_out_chain);
11094 }
11095
11096 /* Implement the "print_recreate" breakpoint_ops method for
11097 masked hardware watchpoints. */
11098
11099 static void
11100 print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
11101 {
11102 struct watchpoint *w = (struct watchpoint *) b;
11103 char tmp[40];
11104
11105 switch (b->type)
11106 {
11107 case bp_hardware_watchpoint:
11108 fprintf_unfiltered (fp, "watch");
11109 break;
11110 case bp_read_watchpoint:
11111 fprintf_unfiltered (fp, "rwatch");
11112 break;
11113 case bp_access_watchpoint:
11114 fprintf_unfiltered (fp, "awatch");
11115 break;
11116 default:
11117 internal_error (__FILE__, __LINE__,
11118 _("Invalid hardware watchpoint type."));
11119 }
11120
11121 sprintf_vma (tmp, w->hw_wp_mask);
11122 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
11123 print_recreate_thread (b, fp);
11124 }
11125
11126 /* The breakpoint_ops structure to be used in masked hardware watchpoints. */
11127
11128 static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
11129
11130 /* Tell whether the given watchpoint is a masked hardware watchpoint. */
11131
11132 static int
11133 is_masked_watchpoint (const struct breakpoint *b)
11134 {
11135 return b->ops == &masked_watchpoint_breakpoint_ops;
11136 }
11137
11138 /* accessflag: hw_write: watch write,
11139 hw_read: watch read,
11140 hw_access: watch access (read or write) */
11141 static void
11142 watch_command_1 (const char *arg, int accessflag, int from_tty,
11143 int just_location, int internal)
11144 {
11145 struct breakpoint *b, *scope_breakpoint = NULL;
11146 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
11147 struct value *val, *mark, *result;
11148 int saved_bitpos = 0, saved_bitsize = 0;
11149 struct frame_info *frame;
11150 const char *exp_start = NULL;
11151 const char *exp_end = NULL;
11152 const char *tok, *end_tok;
11153 int toklen = -1;
11154 const char *cond_start = NULL;
11155 const char *cond_end = NULL;
11156 enum bptype bp_type;
11157 int thread = -1;
11158 int pc = 0;
11159 /* Flag to indicate whether we are going to use masks for
11160 the hardware watchpoint. */
11161 int use_mask = 0;
11162 CORE_ADDR mask = 0;
11163 struct watchpoint *w;
11164 char *expression;
11165 struct cleanup *back_to;
11166
11167 /* Make sure that we actually have parameters to parse. */
11168 if (arg != NULL && arg[0] != '\0')
11169 {
11170 const char *value_start;
11171
11172 exp_end = arg + strlen (arg);
11173
11174 /* Look for "parameter value" pairs at the end
11175 of the arguments string. */
11176 for (tok = exp_end - 1; tok > arg; tok--)
11177 {
11178 /* Skip whitespace at the end of the argument list. */
11179 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11180 tok--;
11181
11182 /* Find the beginning of the last token.
11183 This is the value of the parameter. */
11184 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11185 tok--;
11186 value_start = tok + 1;
11187
11188 /* Skip whitespace. */
11189 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11190 tok--;
11191
11192 end_tok = tok;
11193
11194 /* Find the beginning of the second to last token.
11195 This is the parameter itself. */
11196 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11197 tok--;
11198 tok++;
11199 toklen = end_tok - tok + 1;
11200
11201 if (toklen == 6 && startswith (tok, "thread"))
11202 {
11203 struct thread_info *thr;
11204 /* At this point we've found a "thread" token, which means
11205 the user is trying to set a watchpoint that triggers
11206 only in a specific thread. */
11207 const char *endp;
11208
11209 if (thread != -1)
11210 error(_("You can specify only one thread."));
11211
11212 /* Extract the thread ID from the next token. */
11213 thr = parse_thread_id (value_start, &endp);
11214
11215 /* Check if the user provided a valid thread ID. */
11216 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
11217 invalid_thread_id_error (value_start);
11218
11219 thread = thr->global_num;
11220 }
11221 else if (toklen == 4 && startswith (tok, "mask"))
11222 {
11223 /* We've found a "mask" token, which means the user wants to
11224 create a hardware watchpoint that is going to have the mask
11225 facility. */
11226 struct value *mask_value, *mark;
11227
11228 if (use_mask)
11229 error(_("You can specify only one mask."));
11230
11231 use_mask = just_location = 1;
11232
11233 mark = value_mark ();
11234 mask_value = parse_to_comma_and_eval (&value_start);
11235 mask = value_as_address (mask_value);
11236 value_free_to_mark (mark);
11237 }
11238 else
11239 /* We didn't recognize what we found. We should stop here. */
11240 break;
11241
11242 /* Truncate the string and get rid of the "parameter value" pair before
11243 the arguments string is parsed by the parse_exp_1 function. */
11244 exp_end = tok;
11245 }
11246 }
11247 else
11248 exp_end = arg;
11249
11250 /* Parse the rest of the arguments. From here on out, everything
11251 is in terms of a newly allocated string instead of the original
11252 ARG. */
11253 innermost_block = NULL;
11254 expression = savestring (arg, exp_end - arg);
11255 back_to = make_cleanup (xfree, expression);
11256 exp_start = arg = expression;
11257 expression_up exp = parse_exp_1 (&arg, 0, 0, 0);
11258 exp_end = arg;
11259 /* Remove trailing whitespace from the expression before saving it.
11260 This makes the eventual display of the expression string a bit
11261 prettier. */
11262 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
11263 --exp_end;
11264
11265 /* Checking if the expression is not constant. */
11266 if (watchpoint_exp_is_const (exp.get ()))
11267 {
11268 int len;
11269
11270 len = exp_end - exp_start;
11271 while (len > 0 && isspace (exp_start[len - 1]))
11272 len--;
11273 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
11274 }
11275
11276 exp_valid_block = innermost_block;
11277 mark = value_mark ();
11278 fetch_subexp_value (exp.get (), &pc, &val, &result, NULL, just_location);
11279
11280 if (val != NULL && just_location)
11281 {
11282 saved_bitpos = value_bitpos (val);
11283 saved_bitsize = value_bitsize (val);
11284 }
11285
11286 if (just_location)
11287 {
11288 int ret;
11289
11290 exp_valid_block = NULL;
11291 val = value_addr (result);
11292 release_value (val);
11293 value_free_to_mark (mark);
11294
11295 if (use_mask)
11296 {
11297 ret = target_masked_watch_num_registers (value_as_address (val),
11298 mask);
11299 if (ret == -1)
11300 error (_("This target does not support masked watchpoints."));
11301 else if (ret == -2)
11302 error (_("Invalid mask or memory region."));
11303 }
11304 }
11305 else if (val != NULL)
11306 release_value (val);
11307
11308 tok = skip_spaces_const (arg);
11309 end_tok = skip_to_space_const (tok);
11310
11311 toklen = end_tok - tok;
11312 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
11313 {
11314 innermost_block = NULL;
11315 tok = cond_start = end_tok + 1;
11316 parse_exp_1 (&tok, 0, 0, 0);
11317
11318 /* The watchpoint expression may not be local, but the condition
11319 may still be. E.g.: `watch global if local > 0'. */
11320 cond_exp_valid_block = innermost_block;
11321
11322 cond_end = tok;
11323 }
11324 if (*tok)
11325 error (_("Junk at end of command."));
11326
11327 frame = block_innermost_frame (exp_valid_block);
11328
11329 /* If the expression is "local", then set up a "watchpoint scope"
11330 breakpoint at the point where we've left the scope of the watchpoint
11331 expression. Create the scope breakpoint before the watchpoint, so
11332 that we will encounter it first in bpstat_stop_status. */
11333 if (exp_valid_block && frame)
11334 {
11335 if (frame_id_p (frame_unwind_caller_id (frame)))
11336 {
11337 scope_breakpoint
11338 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
11339 frame_unwind_caller_pc (frame),
11340 bp_watchpoint_scope,
11341 &momentary_breakpoint_ops);
11342
11343 scope_breakpoint->enable_state = bp_enabled;
11344
11345 /* Automatically delete the breakpoint when it hits. */
11346 scope_breakpoint->disposition = disp_del;
11347
11348 /* Only break in the proper frame (help with recursion). */
11349 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
11350
11351 /* Set the address at which we will stop. */
11352 scope_breakpoint->loc->gdbarch
11353 = frame_unwind_caller_arch (frame);
11354 scope_breakpoint->loc->requested_address
11355 = frame_unwind_caller_pc (frame);
11356 scope_breakpoint->loc->address
11357 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
11358 scope_breakpoint->loc->requested_address,
11359 scope_breakpoint->type);
11360 }
11361 }
11362
11363 /* Now set up the breakpoint. We create all watchpoints as hardware
11364 watchpoints here even if hardware watchpoints are turned off, a call
11365 to update_watchpoint later in this function will cause the type to
11366 drop back to bp_watchpoint (software watchpoint) if required. */
11367
11368 if (accessflag == hw_read)
11369 bp_type = bp_read_watchpoint;
11370 else if (accessflag == hw_access)
11371 bp_type = bp_access_watchpoint;
11372 else
11373 bp_type = bp_hardware_watchpoint;
11374
11375 w = new watchpoint ();
11376 b = &w->base;
11377 if (use_mask)
11378 init_raw_breakpoint_without_location (b, NULL, bp_type,
11379 &masked_watchpoint_breakpoint_ops);
11380 else
11381 init_raw_breakpoint_without_location (b, NULL, bp_type,
11382 &watchpoint_breakpoint_ops);
11383 b->thread = thread;
11384 b->disposition = disp_donttouch;
11385 b->pspace = current_program_space;
11386 w->exp = gdb::move (exp);
11387 w->exp_valid_block = exp_valid_block;
11388 w->cond_exp_valid_block = cond_exp_valid_block;
11389 if (just_location)
11390 {
11391 struct type *t = value_type (val);
11392 CORE_ADDR addr = value_as_address (val);
11393 char *name;
11394
11395 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
11396 name = type_to_string (t);
11397
11398 w->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
11399 core_addr_to_string (addr));
11400 xfree (name);
11401
11402 w->exp_string = xstrprintf ("-location %.*s",
11403 (int) (exp_end - exp_start), exp_start);
11404
11405 /* The above expression is in C. */
11406 b->language = language_c;
11407 }
11408 else
11409 w->exp_string = savestring (exp_start, exp_end - exp_start);
11410
11411 if (use_mask)
11412 {
11413 w->hw_wp_mask = mask;
11414 }
11415 else
11416 {
11417 w->val = val;
11418 w->val_bitpos = saved_bitpos;
11419 w->val_bitsize = saved_bitsize;
11420 w->val_valid = 1;
11421 }
11422
11423 if (cond_start)
11424 b->cond_string = savestring (cond_start, cond_end - cond_start);
11425 else
11426 b->cond_string = 0;
11427
11428 if (frame)
11429 {
11430 w->watchpoint_frame = get_frame_id (frame);
11431 w->watchpoint_thread = inferior_ptid;
11432 }
11433 else
11434 {
11435 w->watchpoint_frame = null_frame_id;
11436 w->watchpoint_thread = null_ptid;
11437 }
11438
11439 if (scope_breakpoint != NULL)
11440 {
11441 /* The scope breakpoint is related to the watchpoint. We will
11442 need to act on them together. */
11443 b->related_breakpoint = scope_breakpoint;
11444 scope_breakpoint->related_breakpoint = b;
11445 }
11446
11447 if (!just_location)
11448 value_free_to_mark (mark);
11449
11450 TRY
11451 {
11452 /* Finally update the new watchpoint. This creates the locations
11453 that should be inserted. */
11454 update_watchpoint (w, 1);
11455 }
11456 CATCH (e, RETURN_MASK_ALL)
11457 {
11458 delete_breakpoint (b);
11459 throw_exception (e);
11460 }
11461 END_CATCH
11462
11463 install_breakpoint (internal, b, 1);
11464 do_cleanups (back_to);
11465 }
11466
11467 /* Return count of debug registers needed to watch the given expression.
11468 If the watchpoint cannot be handled in hardware return zero. */
11469
11470 static int
11471 can_use_hardware_watchpoint (struct value *v)
11472 {
11473 int found_memory_cnt = 0;
11474 struct value *head = v;
11475
11476 /* Did the user specifically forbid us to use hardware watchpoints? */
11477 if (!can_use_hw_watchpoints)
11478 return 0;
11479
11480 /* Make sure that the value of the expression depends only upon
11481 memory contents, and values computed from them within GDB. If we
11482 find any register references or function calls, we can't use a
11483 hardware watchpoint.
11484
11485 The idea here is that evaluating an expression generates a series
11486 of values, one holding the value of every subexpression. (The
11487 expression a*b+c has five subexpressions: a, b, a*b, c, and
11488 a*b+c.) GDB's values hold almost enough information to establish
11489 the criteria given above --- they identify memory lvalues,
11490 register lvalues, computed values, etcetera. So we can evaluate
11491 the expression, and then scan the chain of values that leaves
11492 behind to decide whether we can detect any possible change to the
11493 expression's final value using only hardware watchpoints.
11494
11495 However, I don't think that the values returned by inferior
11496 function calls are special in any way. So this function may not
11497 notice that an expression involving an inferior function call
11498 can't be watched with hardware watchpoints. FIXME. */
11499 for (; v; v = value_next (v))
11500 {
11501 if (VALUE_LVAL (v) == lval_memory)
11502 {
11503 if (v != head && value_lazy (v))
11504 /* A lazy memory lvalue in the chain is one that GDB never
11505 needed to fetch; we either just used its address (e.g.,
11506 `a' in `a.b') or we never needed it at all (e.g., `a'
11507 in `a,b'). This doesn't apply to HEAD; if that is
11508 lazy then it was not readable, but watch it anyway. */
11509 ;
11510 else
11511 {
11512 /* Ahh, memory we actually used! Check if we can cover
11513 it with hardware watchpoints. */
11514 struct type *vtype = check_typedef (value_type (v));
11515
11516 /* We only watch structs and arrays if user asked for it
11517 explicitly, never if they just happen to appear in a
11518 middle of some value chain. */
11519 if (v == head
11520 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11521 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11522 {
11523 CORE_ADDR vaddr = value_address (v);
11524 int len;
11525 int num_regs;
11526
11527 len = (target_exact_watchpoints
11528 && is_scalar_type_recursive (vtype))?
11529 1 : TYPE_LENGTH (value_type (v));
11530
11531 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11532 if (!num_regs)
11533 return 0;
11534 else
11535 found_memory_cnt += num_regs;
11536 }
11537 }
11538 }
11539 else if (VALUE_LVAL (v) != not_lval
11540 && deprecated_value_modifiable (v) == 0)
11541 return 0; /* These are values from the history (e.g., $1). */
11542 else if (VALUE_LVAL (v) == lval_register)
11543 return 0; /* Cannot watch a register with a HW watchpoint. */
11544 }
11545
11546 /* The expression itself looks suitable for using a hardware
11547 watchpoint, but give the target machine a chance to reject it. */
11548 return found_memory_cnt;
11549 }
11550
11551 void
11552 watch_command_wrapper (char *arg, int from_tty, int internal)
11553 {
11554 watch_command_1 (arg, hw_write, from_tty, 0, internal);
11555 }
11556
11557 /* A helper function that looks for the "-location" argument and then
11558 calls watch_command_1. */
11559
11560 static void
11561 watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11562 {
11563 int just_location = 0;
11564
11565 if (arg
11566 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11567 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11568 {
11569 arg = skip_spaces (arg);
11570 just_location = 1;
11571 }
11572
11573 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
11574 }
11575
11576 static void
11577 watch_command (char *arg, int from_tty)
11578 {
11579 watch_maybe_just_location (arg, hw_write, from_tty);
11580 }
11581
11582 void
11583 rwatch_command_wrapper (char *arg, int from_tty, int internal)
11584 {
11585 watch_command_1 (arg, hw_read, from_tty, 0, internal);
11586 }
11587
11588 static void
11589 rwatch_command (char *arg, int from_tty)
11590 {
11591 watch_maybe_just_location (arg, hw_read, from_tty);
11592 }
11593
11594 void
11595 awatch_command_wrapper (char *arg, int from_tty, int internal)
11596 {
11597 watch_command_1 (arg, hw_access, from_tty, 0, internal);
11598 }
11599
11600 static void
11601 awatch_command (char *arg, int from_tty)
11602 {
11603 watch_maybe_just_location (arg, hw_access, from_tty);
11604 }
11605 \f
11606
11607 /* Data for the FSM that manages the until(location)/advance commands
11608 in infcmd.c. Here because it uses the mechanisms of
11609 breakpoints. */
11610
11611 struct until_break_fsm
11612 {
11613 /* The base class. */
11614 struct thread_fsm thread_fsm;
11615
11616 /* The thread that as current when the command was executed. */
11617 int thread;
11618
11619 /* The breakpoint set at the destination location. */
11620 struct breakpoint *location_breakpoint;
11621
11622 /* Breakpoint set at the return address in the caller frame. May be
11623 NULL. */
11624 struct breakpoint *caller_breakpoint;
11625 };
11626
11627 static void until_break_fsm_clean_up (struct thread_fsm *self,
11628 struct thread_info *thread);
11629 static int until_break_fsm_should_stop (struct thread_fsm *self,
11630 struct thread_info *thread);
11631 static enum async_reply_reason
11632 until_break_fsm_async_reply_reason (struct thread_fsm *self);
11633
11634 /* until_break_fsm's vtable. */
11635
11636 static struct thread_fsm_ops until_break_fsm_ops =
11637 {
11638 NULL, /* dtor */
11639 until_break_fsm_clean_up,
11640 until_break_fsm_should_stop,
11641 NULL, /* return_value */
11642 until_break_fsm_async_reply_reason,
11643 };
11644
11645 /* Allocate a new until_break_command_fsm. */
11646
11647 static struct until_break_fsm *
11648 new_until_break_fsm (struct interp *cmd_interp, int thread,
11649 struct breakpoint *location_breakpoint,
11650 struct breakpoint *caller_breakpoint)
11651 {
11652 struct until_break_fsm *sm;
11653
11654 sm = XCNEW (struct until_break_fsm);
11655 thread_fsm_ctor (&sm->thread_fsm, &until_break_fsm_ops, cmd_interp);
11656
11657 sm->thread = thread;
11658 sm->location_breakpoint = location_breakpoint;
11659 sm->caller_breakpoint = caller_breakpoint;
11660
11661 return sm;
11662 }
11663
11664 /* Implementation of the 'should_stop' FSM method for the
11665 until(location)/advance commands. */
11666
11667 static int
11668 until_break_fsm_should_stop (struct thread_fsm *self,
11669 struct thread_info *tp)
11670 {
11671 struct until_break_fsm *sm = (struct until_break_fsm *) self;
11672
11673 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
11674 sm->location_breakpoint) != NULL
11675 || (sm->caller_breakpoint != NULL
11676 && bpstat_find_breakpoint (tp->control.stop_bpstat,
11677 sm->caller_breakpoint) != NULL))
11678 thread_fsm_set_finished (self);
11679
11680 return 1;
11681 }
11682
11683 /* Implementation of the 'clean_up' FSM method for the
11684 until(location)/advance commands. */
11685
11686 static void
11687 until_break_fsm_clean_up (struct thread_fsm *self,
11688 struct thread_info *thread)
11689 {
11690 struct until_break_fsm *sm = (struct until_break_fsm *) self;
11691
11692 /* Clean up our temporary breakpoints. */
11693 if (sm->location_breakpoint != NULL)
11694 {
11695 delete_breakpoint (sm->location_breakpoint);
11696 sm->location_breakpoint = NULL;
11697 }
11698 if (sm->caller_breakpoint != NULL)
11699 {
11700 delete_breakpoint (sm->caller_breakpoint);
11701 sm->caller_breakpoint = NULL;
11702 }
11703 delete_longjmp_breakpoint (sm->thread);
11704 }
11705
11706 /* Implementation of the 'async_reply_reason' FSM method for the
11707 until(location)/advance commands. */
11708
11709 static enum async_reply_reason
11710 until_break_fsm_async_reply_reason (struct thread_fsm *self)
11711 {
11712 return EXEC_ASYNC_LOCATION_REACHED;
11713 }
11714
11715 void
11716 until_break_command (char *arg, int from_tty, int anywhere)
11717 {
11718 struct symtabs_and_lines sals;
11719 struct symtab_and_line sal;
11720 struct frame_info *frame;
11721 struct gdbarch *frame_gdbarch;
11722 struct frame_id stack_frame_id;
11723 struct frame_id caller_frame_id;
11724 struct breakpoint *location_breakpoint;
11725 struct breakpoint *caller_breakpoint = NULL;
11726 struct cleanup *old_chain, *cleanup;
11727 int thread;
11728 struct thread_info *tp;
11729 struct event_location *location;
11730 struct until_break_fsm *sm;
11731
11732 clear_proceed_status (0);
11733
11734 /* Set a breakpoint where the user wants it and at return from
11735 this function. */
11736
11737 location = string_to_event_location (&arg, current_language);
11738 cleanup = make_cleanup_delete_event_location (location);
11739
11740 if (last_displayed_sal_is_valid ())
11741 sals = decode_line_1 (location, DECODE_LINE_FUNFIRSTLINE, NULL,
11742 get_last_displayed_symtab (),
11743 get_last_displayed_line ());
11744 else
11745 sals = decode_line_1 (location, DECODE_LINE_FUNFIRSTLINE,
11746 NULL, (struct symtab *) NULL, 0);
11747
11748 if (sals.nelts != 1)
11749 error (_("Couldn't get information on specified line."));
11750
11751 sal = sals.sals[0];
11752 xfree (sals.sals); /* malloc'd, so freed. */
11753
11754 if (*arg)
11755 error (_("Junk at end of arguments."));
11756
11757 resolve_sal_pc (&sal);
11758
11759 tp = inferior_thread ();
11760 thread = tp->global_num;
11761
11762 old_chain = make_cleanup (null_cleanup, NULL);
11763
11764 /* Note linespec handling above invalidates the frame chain.
11765 Installing a breakpoint also invalidates the frame chain (as it
11766 may need to switch threads), so do any frame handling before
11767 that. */
11768
11769 frame = get_selected_frame (NULL);
11770 frame_gdbarch = get_frame_arch (frame);
11771 stack_frame_id = get_stack_frame_id (frame);
11772 caller_frame_id = frame_unwind_caller_id (frame);
11773
11774 /* Keep within the current frame, or in frames called by the current
11775 one. */
11776
11777 if (frame_id_p (caller_frame_id))
11778 {
11779 struct symtab_and_line sal2;
11780 struct gdbarch *caller_gdbarch;
11781
11782 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11783 sal2.pc = frame_unwind_caller_pc (frame);
11784 caller_gdbarch = frame_unwind_caller_arch (frame);
11785 caller_breakpoint = set_momentary_breakpoint (caller_gdbarch,
11786 sal2,
11787 caller_frame_id,
11788 bp_until);
11789 make_cleanup_delete_breakpoint (caller_breakpoint);
11790
11791 set_longjmp_breakpoint (tp, caller_frame_id);
11792 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
11793 }
11794
11795 /* set_momentary_breakpoint could invalidate FRAME. */
11796 frame = NULL;
11797
11798 if (anywhere)
11799 /* If the user told us to continue until a specified location,
11800 we don't specify a frame at which we need to stop. */
11801 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11802 null_frame_id, bp_until);
11803 else
11804 /* Otherwise, specify the selected frame, because we want to stop
11805 only at the very same frame. */
11806 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11807 stack_frame_id, bp_until);
11808 make_cleanup_delete_breakpoint (location_breakpoint);
11809
11810 sm = new_until_break_fsm (command_interp (), tp->global_num,
11811 location_breakpoint, caller_breakpoint);
11812 tp->thread_fsm = &sm->thread_fsm;
11813
11814 discard_cleanups (old_chain);
11815
11816 proceed (-1, GDB_SIGNAL_DEFAULT);
11817
11818 do_cleanups (cleanup);
11819 }
11820
11821 /* This function attempts to parse an optional "if <cond>" clause
11822 from the arg string. If one is not found, it returns NULL.
11823
11824 Else, it returns a pointer to the condition string. (It does not
11825 attempt to evaluate the string against a particular block.) And,
11826 it updates arg to point to the first character following the parsed
11827 if clause in the arg string. */
11828
11829 char *
11830 ep_parse_optional_if_clause (char **arg)
11831 {
11832 char *cond_string;
11833
11834 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
11835 return NULL;
11836
11837 /* Skip the "if" keyword. */
11838 (*arg) += 2;
11839
11840 /* Skip any extra leading whitespace, and record the start of the
11841 condition string. */
11842 *arg = skip_spaces (*arg);
11843 cond_string = *arg;
11844
11845 /* Assume that the condition occupies the remainder of the arg
11846 string. */
11847 (*arg) += strlen (cond_string);
11848
11849 return cond_string;
11850 }
11851
11852 /* Commands to deal with catching events, such as signals, exceptions,
11853 process start/exit, etc. */
11854
11855 typedef enum
11856 {
11857 catch_fork_temporary, catch_vfork_temporary,
11858 catch_fork_permanent, catch_vfork_permanent
11859 }
11860 catch_fork_kind;
11861
11862 static void
11863 catch_fork_command_1 (char *arg, int from_tty,
11864 struct cmd_list_element *command)
11865 {
11866 struct gdbarch *gdbarch = get_current_arch ();
11867 char *cond_string = NULL;
11868 catch_fork_kind fork_kind;
11869 int tempflag;
11870
11871 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11872 tempflag = (fork_kind == catch_fork_temporary
11873 || fork_kind == catch_vfork_temporary);
11874
11875 if (!arg)
11876 arg = "";
11877 arg = skip_spaces (arg);
11878
11879 /* The allowed syntax is:
11880 catch [v]fork
11881 catch [v]fork if <cond>
11882
11883 First, check if there's an if clause. */
11884 cond_string = ep_parse_optional_if_clause (&arg);
11885
11886 if ((*arg != '\0') && !isspace (*arg))
11887 error (_("Junk at end of arguments."));
11888
11889 /* If this target supports it, create a fork or vfork catchpoint
11890 and enable reporting of such events. */
11891 switch (fork_kind)
11892 {
11893 case catch_fork_temporary:
11894 case catch_fork_permanent:
11895 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11896 &catch_fork_breakpoint_ops);
11897 break;
11898 case catch_vfork_temporary:
11899 case catch_vfork_permanent:
11900 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11901 &catch_vfork_breakpoint_ops);
11902 break;
11903 default:
11904 error (_("unsupported or unknown fork kind; cannot catch it"));
11905 break;
11906 }
11907 }
11908
11909 static void
11910 catch_exec_command_1 (char *arg, int from_tty,
11911 struct cmd_list_element *command)
11912 {
11913 struct exec_catchpoint *c;
11914 struct gdbarch *gdbarch = get_current_arch ();
11915 int tempflag;
11916 char *cond_string = NULL;
11917
11918 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11919
11920 if (!arg)
11921 arg = "";
11922 arg = skip_spaces (arg);
11923
11924 /* The allowed syntax is:
11925 catch exec
11926 catch exec if <cond>
11927
11928 First, check if there's an if clause. */
11929 cond_string = ep_parse_optional_if_clause (&arg);
11930
11931 if ((*arg != '\0') && !isspace (*arg))
11932 error (_("Junk at end of arguments."));
11933
11934 c = new exec_catchpoint ();
11935 init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
11936 &catch_exec_breakpoint_ops);
11937 c->exec_pathname = NULL;
11938
11939 install_breakpoint (0, &c->base, 1);
11940 }
11941
11942 void
11943 init_ada_exception_breakpoint (struct breakpoint *b,
11944 struct gdbarch *gdbarch,
11945 struct symtab_and_line sal,
11946 char *addr_string,
11947 const struct breakpoint_ops *ops,
11948 int tempflag,
11949 int enabled,
11950 int from_tty)
11951 {
11952 if (from_tty)
11953 {
11954 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11955 if (!loc_gdbarch)
11956 loc_gdbarch = gdbarch;
11957
11958 describe_other_breakpoints (loc_gdbarch,
11959 sal.pspace, sal.pc, sal.section, -1);
11960 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11961 version for exception catchpoints, because two catchpoints
11962 used for different exception names will use the same address.
11963 In this case, a "breakpoint ... also set at..." warning is
11964 unproductive. Besides, the warning phrasing is also a bit
11965 inappropriate, we should use the word catchpoint, and tell
11966 the user what type of catchpoint it is. The above is good
11967 enough for now, though. */
11968 }
11969
11970 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
11971
11972 b->enable_state = enabled ? bp_enabled : bp_disabled;
11973 b->disposition = tempflag ? disp_del : disp_donttouch;
11974 b->location = string_to_event_location (&addr_string,
11975 language_def (language_ada));
11976 b->language = language_ada;
11977 }
11978
11979 static void
11980 catch_command (char *arg, int from_tty)
11981 {
11982 error (_("Catch requires an event name."));
11983 }
11984 \f
11985
11986 static void
11987 tcatch_command (char *arg, int from_tty)
11988 {
11989 error (_("Catch requires an event name."));
11990 }
11991
11992 /* A qsort comparison function that sorts breakpoints in order. */
11993
11994 static int
11995 compare_breakpoints (const void *a, const void *b)
11996 {
11997 const breakpoint_p *ba = (const breakpoint_p *) a;
11998 uintptr_t ua = (uintptr_t) *ba;
11999 const breakpoint_p *bb = (const breakpoint_p *) b;
12000 uintptr_t ub = (uintptr_t) *bb;
12001
12002 if ((*ba)->number < (*bb)->number)
12003 return -1;
12004 else if ((*ba)->number > (*bb)->number)
12005 return 1;
12006
12007 /* Now sort by address, in case we see, e..g, two breakpoints with
12008 the number 0. */
12009 if (ua < ub)
12010 return -1;
12011 return ua > ub ? 1 : 0;
12012 }
12013
12014 /* Delete breakpoints by address or line. */
12015
12016 static void
12017 clear_command (char *arg, int from_tty)
12018 {
12019 struct breakpoint *b, *prev;
12020 VEC(breakpoint_p) *found = 0;
12021 int ix;
12022 int default_match;
12023 struct symtabs_and_lines sals;
12024 struct symtab_and_line sal;
12025 int i;
12026 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
12027
12028 if (arg)
12029 {
12030 sals = decode_line_with_current_source (arg,
12031 (DECODE_LINE_FUNFIRSTLINE
12032 | DECODE_LINE_LIST_MODE));
12033 make_cleanup (xfree, sals.sals);
12034 default_match = 0;
12035 }
12036 else
12037 {
12038 sals.sals = XNEW (struct symtab_and_line);
12039 make_cleanup (xfree, sals.sals);
12040 init_sal (&sal); /* Initialize to zeroes. */
12041
12042 /* Set sal's line, symtab, pc, and pspace to the values
12043 corresponding to the last call to print_frame_info. If the
12044 codepoint is not valid, this will set all the fields to 0. */
12045 get_last_displayed_sal (&sal);
12046 if (sal.symtab == 0)
12047 error (_("No source file specified."));
12048
12049 sals.sals[0] = sal;
12050 sals.nelts = 1;
12051
12052 default_match = 1;
12053 }
12054
12055 /* We don't call resolve_sal_pc here. That's not as bad as it
12056 seems, because all existing breakpoints typically have both
12057 file/line and pc set. So, if clear is given file/line, we can
12058 match this to existing breakpoint without obtaining pc at all.
12059
12060 We only support clearing given the address explicitly
12061 present in breakpoint table. Say, we've set breakpoint
12062 at file:line. There were several PC values for that file:line,
12063 due to optimization, all in one block.
12064
12065 We've picked one PC value. If "clear" is issued with another
12066 PC corresponding to the same file:line, the breakpoint won't
12067 be cleared. We probably can still clear the breakpoint, but
12068 since the other PC value is never presented to user, user
12069 can only find it by guessing, and it does not seem important
12070 to support that. */
12071
12072 /* For each line spec given, delete bps which correspond to it. Do
12073 it in two passes, solely to preserve the current behavior that
12074 from_tty is forced true if we delete more than one
12075 breakpoint. */
12076
12077 found = NULL;
12078 make_cleanup (VEC_cleanup (breakpoint_p), &found);
12079 for (i = 0; i < sals.nelts; i++)
12080 {
12081 const char *sal_fullname;
12082
12083 /* If exact pc given, clear bpts at that pc.
12084 If line given (pc == 0), clear all bpts on specified line.
12085 If defaulting, clear all bpts on default line
12086 or at default pc.
12087
12088 defaulting sal.pc != 0 tests to do
12089
12090 0 1 pc
12091 1 1 pc _and_ line
12092 0 0 line
12093 1 0 <can't happen> */
12094
12095 sal = sals.sals[i];
12096 sal_fullname = (sal.symtab == NULL
12097 ? NULL : symtab_to_fullname (sal.symtab));
12098
12099 /* Find all matching breakpoints and add them to 'found'. */
12100 ALL_BREAKPOINTS (b)
12101 {
12102 int match = 0;
12103 /* Are we going to delete b? */
12104 if (b->type != bp_none && !is_watchpoint (b))
12105 {
12106 struct bp_location *loc = b->loc;
12107 for (; loc; loc = loc->next)
12108 {
12109 /* If the user specified file:line, don't allow a PC
12110 match. This matches historical gdb behavior. */
12111 int pc_match = (!sal.explicit_line
12112 && sal.pc
12113 && (loc->pspace == sal.pspace)
12114 && (loc->address == sal.pc)
12115 && (!section_is_overlay (loc->section)
12116 || loc->section == sal.section));
12117 int line_match = 0;
12118
12119 if ((default_match || sal.explicit_line)
12120 && loc->symtab != NULL
12121 && sal_fullname != NULL
12122 && sal.pspace == loc->pspace
12123 && loc->line_number == sal.line
12124 && filename_cmp (symtab_to_fullname (loc->symtab),
12125 sal_fullname) == 0)
12126 line_match = 1;
12127
12128 if (pc_match || line_match)
12129 {
12130 match = 1;
12131 break;
12132 }
12133 }
12134 }
12135
12136 if (match)
12137 VEC_safe_push(breakpoint_p, found, b);
12138 }
12139 }
12140
12141 /* Now go thru the 'found' chain and delete them. */
12142 if (VEC_empty(breakpoint_p, found))
12143 {
12144 if (arg)
12145 error (_("No breakpoint at %s."), arg);
12146 else
12147 error (_("No breakpoint at this line."));
12148 }
12149
12150 /* Remove duplicates from the vec. */
12151 qsort (VEC_address (breakpoint_p, found),
12152 VEC_length (breakpoint_p, found),
12153 sizeof (breakpoint_p),
12154 compare_breakpoints);
12155 prev = VEC_index (breakpoint_p, found, 0);
12156 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
12157 {
12158 if (b == prev)
12159 {
12160 VEC_ordered_remove (breakpoint_p, found, ix);
12161 --ix;
12162 }
12163 }
12164
12165 if (VEC_length(breakpoint_p, found) > 1)
12166 from_tty = 1; /* Always report if deleted more than one. */
12167 if (from_tty)
12168 {
12169 if (VEC_length(breakpoint_p, found) == 1)
12170 printf_unfiltered (_("Deleted breakpoint "));
12171 else
12172 printf_unfiltered (_("Deleted breakpoints "));
12173 }
12174
12175 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
12176 {
12177 if (from_tty)
12178 printf_unfiltered ("%d ", b->number);
12179 delete_breakpoint (b);
12180 }
12181 if (from_tty)
12182 putchar_unfiltered ('\n');
12183
12184 do_cleanups (cleanups);
12185 }
12186 \f
12187 /* Delete breakpoint in BS if they are `delete' breakpoints and
12188 all breakpoints that are marked for deletion, whether hit or not.
12189 This is called after any breakpoint is hit, or after errors. */
12190
12191 void
12192 breakpoint_auto_delete (bpstat bs)
12193 {
12194 struct breakpoint *b, *b_tmp;
12195
12196 for (; bs; bs = bs->next)
12197 if (bs->breakpoint_at
12198 && bs->breakpoint_at->disposition == disp_del
12199 && bs->stop)
12200 delete_breakpoint (bs->breakpoint_at);
12201
12202 ALL_BREAKPOINTS_SAFE (b, b_tmp)
12203 {
12204 if (b->disposition == disp_del_at_next_stop)
12205 delete_breakpoint (b);
12206 }
12207 }
12208
12209 /* A comparison function for bp_location AP and BP being interfaced to
12210 qsort. Sort elements primarily by their ADDRESS (no matter what
12211 does breakpoint_address_is_meaningful say for its OWNER),
12212 secondarily by ordering first permanent elements and
12213 terciarily just ensuring the array is sorted stable way despite
12214 qsort being an unstable algorithm. */
12215
12216 static int
12217 bp_location_compare (const void *ap, const void *bp)
12218 {
12219 const struct bp_location *a = *(const struct bp_location **) ap;
12220 const struct bp_location *b = *(const struct bp_location **) bp;
12221
12222 if (a->address != b->address)
12223 return (a->address > b->address) - (a->address < b->address);
12224
12225 /* Sort locations at the same address by their pspace number, keeping
12226 locations of the same inferior (in a multi-inferior environment)
12227 grouped. */
12228
12229 if (a->pspace->num != b->pspace->num)
12230 return ((a->pspace->num > b->pspace->num)
12231 - (a->pspace->num < b->pspace->num));
12232
12233 /* Sort permanent breakpoints first. */
12234 if (a->permanent != b->permanent)
12235 return (a->permanent < b->permanent) - (a->permanent > b->permanent);
12236
12237 /* Make the internal GDB representation stable across GDB runs
12238 where A and B memory inside GDB can differ. Breakpoint locations of
12239 the same type at the same address can be sorted in arbitrary order. */
12240
12241 if (a->owner->number != b->owner->number)
12242 return ((a->owner->number > b->owner->number)
12243 - (a->owner->number < b->owner->number));
12244
12245 return (a > b) - (a < b);
12246 }
12247
12248 /* Set bp_location_placed_address_before_address_max and
12249 bp_location_shadow_len_after_address_max according to the current
12250 content of the bp_location array. */
12251
12252 static void
12253 bp_location_target_extensions_update (void)
12254 {
12255 struct bp_location *bl, **blp_tmp;
12256
12257 bp_location_placed_address_before_address_max = 0;
12258 bp_location_shadow_len_after_address_max = 0;
12259
12260 ALL_BP_LOCATIONS (bl, blp_tmp)
12261 {
12262 CORE_ADDR start, end, addr;
12263
12264 if (!bp_location_has_shadow (bl))
12265 continue;
12266
12267 start = bl->target_info.placed_address;
12268 end = start + bl->target_info.shadow_len;
12269
12270 gdb_assert (bl->address >= start);
12271 addr = bl->address - start;
12272 if (addr > bp_location_placed_address_before_address_max)
12273 bp_location_placed_address_before_address_max = addr;
12274
12275 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
12276
12277 gdb_assert (bl->address < end);
12278 addr = end - bl->address;
12279 if (addr > bp_location_shadow_len_after_address_max)
12280 bp_location_shadow_len_after_address_max = addr;
12281 }
12282 }
12283
12284 /* Download tracepoint locations if they haven't been. */
12285
12286 static void
12287 download_tracepoint_locations (void)
12288 {
12289 struct breakpoint *b;
12290 struct cleanup *old_chain;
12291 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
12292
12293 old_chain = save_current_space_and_thread ();
12294
12295 ALL_TRACEPOINTS (b)
12296 {
12297 struct bp_location *bl;
12298 struct tracepoint *t;
12299 int bp_location_downloaded = 0;
12300
12301 if ((b->type == bp_fast_tracepoint
12302 ? !may_insert_fast_tracepoints
12303 : !may_insert_tracepoints))
12304 continue;
12305
12306 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
12307 {
12308 if (target_can_download_tracepoint ())
12309 can_download_tracepoint = TRIBOOL_TRUE;
12310 else
12311 can_download_tracepoint = TRIBOOL_FALSE;
12312 }
12313
12314 if (can_download_tracepoint == TRIBOOL_FALSE)
12315 break;
12316
12317 for (bl = b->loc; bl; bl = bl->next)
12318 {
12319 /* In tracepoint, locations are _never_ duplicated, so
12320 should_be_inserted is equivalent to
12321 unduplicated_should_be_inserted. */
12322 if (!should_be_inserted (bl) || bl->inserted)
12323 continue;
12324
12325 switch_to_program_space_and_thread (bl->pspace);
12326
12327 target_download_tracepoint (bl);
12328
12329 bl->inserted = 1;
12330 bp_location_downloaded = 1;
12331 }
12332 t = (struct tracepoint *) b;
12333 t->number_on_target = b->number;
12334 if (bp_location_downloaded)
12335 observer_notify_breakpoint_modified (b);
12336 }
12337
12338 do_cleanups (old_chain);
12339 }
12340
12341 /* Swap the insertion/duplication state between two locations. */
12342
12343 static void
12344 swap_insertion (struct bp_location *left, struct bp_location *right)
12345 {
12346 const int left_inserted = left->inserted;
12347 const int left_duplicate = left->duplicate;
12348 const int left_needs_update = left->needs_update;
12349 const struct bp_target_info left_target_info = left->target_info;
12350
12351 /* Locations of tracepoints can never be duplicated. */
12352 if (is_tracepoint (left->owner))
12353 gdb_assert (!left->duplicate);
12354 if (is_tracepoint (right->owner))
12355 gdb_assert (!right->duplicate);
12356
12357 left->inserted = right->inserted;
12358 left->duplicate = right->duplicate;
12359 left->needs_update = right->needs_update;
12360 left->target_info = right->target_info;
12361 right->inserted = left_inserted;
12362 right->duplicate = left_duplicate;
12363 right->needs_update = left_needs_update;
12364 right->target_info = left_target_info;
12365 }
12366
12367 /* Force the re-insertion of the locations at ADDRESS. This is called
12368 once a new/deleted/modified duplicate location is found and we are evaluating
12369 conditions on the target's side. Such conditions need to be updated on
12370 the target. */
12371
12372 static void
12373 force_breakpoint_reinsertion (struct bp_location *bl)
12374 {
12375 struct bp_location **locp = NULL, **loc2p;
12376 struct bp_location *loc;
12377 CORE_ADDR address = 0;
12378 int pspace_num;
12379
12380 address = bl->address;
12381 pspace_num = bl->pspace->num;
12382
12383 /* This is only meaningful if the target is
12384 evaluating conditions and if the user has
12385 opted for condition evaluation on the target's
12386 side. */
12387 if (gdb_evaluates_breakpoint_condition_p ()
12388 || !target_supports_evaluation_of_breakpoint_conditions ())
12389 return;
12390
12391 /* Flag all breakpoint locations with this address and
12392 the same program space as the location
12393 as "its condition has changed". We need to
12394 update the conditions on the target's side. */
12395 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
12396 {
12397 loc = *loc2p;
12398
12399 if (!is_breakpoint (loc->owner)
12400 || pspace_num != loc->pspace->num)
12401 continue;
12402
12403 /* Flag the location appropriately. We use a different state to
12404 let everyone know that we already updated the set of locations
12405 with addr bl->address and program space bl->pspace. This is so
12406 we don't have to keep calling these functions just to mark locations
12407 that have already been marked. */
12408 loc->condition_changed = condition_updated;
12409
12410 /* Free the agent expression bytecode as well. We will compute
12411 it later on. */
12412 if (loc->cond_bytecode)
12413 {
12414 free_agent_expr (loc->cond_bytecode);
12415 loc->cond_bytecode = NULL;
12416 }
12417 }
12418 }
12419 /* Called whether new breakpoints are created, or existing breakpoints
12420 deleted, to update the global location list and recompute which
12421 locations are duplicate of which.
12422
12423 The INSERT_MODE flag determines whether locations may not, may, or
12424 shall be inserted now. See 'enum ugll_insert_mode' for more
12425 info. */
12426
12427 static void
12428 update_global_location_list (enum ugll_insert_mode insert_mode)
12429 {
12430 struct breakpoint *b;
12431 struct bp_location **locp, *loc;
12432 struct cleanup *cleanups;
12433 /* Last breakpoint location address that was marked for update. */
12434 CORE_ADDR last_addr = 0;
12435 /* Last breakpoint location program space that was marked for update. */
12436 int last_pspace_num = -1;
12437
12438 /* Used in the duplicates detection below. When iterating over all
12439 bp_locations, points to the first bp_location of a given address.
12440 Breakpoints and watchpoints of different types are never
12441 duplicates of each other. Keep one pointer for each type of
12442 breakpoint/watchpoint, so we only need to loop over all locations
12443 once. */
12444 struct bp_location *bp_loc_first; /* breakpoint */
12445 struct bp_location *wp_loc_first; /* hardware watchpoint */
12446 struct bp_location *awp_loc_first; /* access watchpoint */
12447 struct bp_location *rwp_loc_first; /* read watchpoint */
12448
12449 /* Saved former bp_location array which we compare against the newly
12450 built bp_location from the current state of ALL_BREAKPOINTS. */
12451 struct bp_location **old_location, **old_locp;
12452 unsigned old_location_count;
12453
12454 old_location = bp_location;
12455 old_location_count = bp_location_count;
12456 bp_location = NULL;
12457 bp_location_count = 0;
12458 cleanups = make_cleanup (xfree, old_location);
12459
12460 ALL_BREAKPOINTS (b)
12461 for (loc = b->loc; loc; loc = loc->next)
12462 bp_location_count++;
12463
12464 bp_location = XNEWVEC (struct bp_location *, bp_location_count);
12465 locp = bp_location;
12466 ALL_BREAKPOINTS (b)
12467 for (loc = b->loc; loc; loc = loc->next)
12468 *locp++ = loc;
12469 qsort (bp_location, bp_location_count, sizeof (*bp_location),
12470 bp_location_compare);
12471
12472 bp_location_target_extensions_update ();
12473
12474 /* Identify bp_location instances that are no longer present in the
12475 new list, and therefore should be freed. Note that it's not
12476 necessary that those locations should be removed from inferior --
12477 if there's another location at the same address (previously
12478 marked as duplicate), we don't need to remove/insert the
12479 location.
12480
12481 LOCP is kept in sync with OLD_LOCP, each pointing to the current
12482 and former bp_location array state respectively. */
12483
12484 locp = bp_location;
12485 for (old_locp = old_location; old_locp < old_location + old_location_count;
12486 old_locp++)
12487 {
12488 struct bp_location *old_loc = *old_locp;
12489 struct bp_location **loc2p;
12490
12491 /* Tells if 'old_loc' is found among the new locations. If
12492 not, we have to free it. */
12493 int found_object = 0;
12494 /* Tells if the location should remain inserted in the target. */
12495 int keep_in_target = 0;
12496 int removed = 0;
12497
12498 /* Skip LOCP entries which will definitely never be needed.
12499 Stop either at or being the one matching OLD_LOC. */
12500 while (locp < bp_location + bp_location_count
12501 && (*locp)->address < old_loc->address)
12502 locp++;
12503
12504 for (loc2p = locp;
12505 (loc2p < bp_location + bp_location_count
12506 && (*loc2p)->address == old_loc->address);
12507 loc2p++)
12508 {
12509 /* Check if this is a new/duplicated location or a duplicated
12510 location that had its condition modified. If so, we want to send
12511 its condition to the target if evaluation of conditions is taking
12512 place there. */
12513 if ((*loc2p)->condition_changed == condition_modified
12514 && (last_addr != old_loc->address
12515 || last_pspace_num != old_loc->pspace->num))
12516 {
12517 force_breakpoint_reinsertion (*loc2p);
12518 last_pspace_num = old_loc->pspace->num;
12519 }
12520
12521 if (*loc2p == old_loc)
12522 found_object = 1;
12523 }
12524
12525 /* We have already handled this address, update it so that we don't
12526 have to go through updates again. */
12527 last_addr = old_loc->address;
12528
12529 /* Target-side condition evaluation: Handle deleted locations. */
12530 if (!found_object)
12531 force_breakpoint_reinsertion (old_loc);
12532
12533 /* If this location is no longer present, and inserted, look if
12534 there's maybe a new location at the same address. If so,
12535 mark that one inserted, and don't remove this one. This is
12536 needed so that we don't have a time window where a breakpoint
12537 at certain location is not inserted. */
12538
12539 if (old_loc->inserted)
12540 {
12541 /* If the location is inserted now, we might have to remove
12542 it. */
12543
12544 if (found_object && should_be_inserted (old_loc))
12545 {
12546 /* The location is still present in the location list,
12547 and still should be inserted. Don't do anything. */
12548 keep_in_target = 1;
12549 }
12550 else
12551 {
12552 /* This location still exists, but it won't be kept in the
12553 target since it may have been disabled. We proceed to
12554 remove its target-side condition. */
12555
12556 /* The location is either no longer present, or got
12557 disabled. See if there's another location at the
12558 same address, in which case we don't need to remove
12559 this one from the target. */
12560
12561 /* OLD_LOC comes from existing struct breakpoint. */
12562 if (breakpoint_address_is_meaningful (old_loc->owner))
12563 {
12564 for (loc2p = locp;
12565 (loc2p < bp_location + bp_location_count
12566 && (*loc2p)->address == old_loc->address);
12567 loc2p++)
12568 {
12569 struct bp_location *loc2 = *loc2p;
12570
12571 if (breakpoint_locations_match (loc2, old_loc))
12572 {
12573 /* Read watchpoint locations are switched to
12574 access watchpoints, if the former are not
12575 supported, but the latter are. */
12576 if (is_hardware_watchpoint (old_loc->owner))
12577 {
12578 gdb_assert (is_hardware_watchpoint (loc2->owner));
12579 loc2->watchpoint_type = old_loc->watchpoint_type;
12580 }
12581
12582 /* loc2 is a duplicated location. We need to check
12583 if it should be inserted in case it will be
12584 unduplicated. */
12585 if (loc2 != old_loc
12586 && unduplicated_should_be_inserted (loc2))
12587 {
12588 swap_insertion (old_loc, loc2);
12589 keep_in_target = 1;
12590 break;
12591 }
12592 }
12593 }
12594 }
12595 }
12596
12597 if (!keep_in_target)
12598 {
12599 if (remove_breakpoint (old_loc))
12600 {
12601 /* This is just about all we can do. We could keep
12602 this location on the global list, and try to
12603 remove it next time, but there's no particular
12604 reason why we will succeed next time.
12605
12606 Note that at this point, old_loc->owner is still
12607 valid, as delete_breakpoint frees the breakpoint
12608 only after calling us. */
12609 printf_filtered (_("warning: Error removing "
12610 "breakpoint %d\n"),
12611 old_loc->owner->number);
12612 }
12613 removed = 1;
12614 }
12615 }
12616
12617 if (!found_object)
12618 {
12619 if (removed && target_is_non_stop_p ()
12620 && need_moribund_for_location_type (old_loc))
12621 {
12622 /* This location was removed from the target. In
12623 non-stop mode, a race condition is possible where
12624 we've removed a breakpoint, but stop events for that
12625 breakpoint are already queued and will arrive later.
12626 We apply an heuristic to be able to distinguish such
12627 SIGTRAPs from other random SIGTRAPs: we keep this
12628 breakpoint location for a bit, and will retire it
12629 after we see some number of events. The theory here
12630 is that reporting of events should, "on the average",
12631 be fair, so after a while we'll see events from all
12632 threads that have anything of interest, and no longer
12633 need to keep this breakpoint location around. We
12634 don't hold locations forever so to reduce chances of
12635 mistaking a non-breakpoint SIGTRAP for a breakpoint
12636 SIGTRAP.
12637
12638 The heuristic failing can be disastrous on
12639 decr_pc_after_break targets.
12640
12641 On decr_pc_after_break targets, like e.g., x86-linux,
12642 if we fail to recognize a late breakpoint SIGTRAP,
12643 because events_till_retirement has reached 0 too
12644 soon, we'll fail to do the PC adjustment, and report
12645 a random SIGTRAP to the user. When the user resumes
12646 the inferior, it will most likely immediately crash
12647 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
12648 corrupted, because of being resumed e.g., in the
12649 middle of a multi-byte instruction, or skipped a
12650 one-byte instruction. This was actually seen happen
12651 on native x86-linux, and should be less rare on
12652 targets that do not support new thread events, like
12653 remote, due to the heuristic depending on
12654 thread_count.
12655
12656 Mistaking a random SIGTRAP for a breakpoint trap
12657 causes similar symptoms (PC adjustment applied when
12658 it shouldn't), but then again, playing with SIGTRAPs
12659 behind the debugger's back is asking for trouble.
12660
12661 Since hardware watchpoint traps are always
12662 distinguishable from other traps, so we don't need to
12663 apply keep hardware watchpoint moribund locations
12664 around. We simply always ignore hardware watchpoint
12665 traps we can no longer explain. */
12666
12667 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12668 old_loc->owner = NULL;
12669
12670 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
12671 }
12672 else
12673 {
12674 old_loc->owner = NULL;
12675 decref_bp_location (&old_loc);
12676 }
12677 }
12678 }
12679
12680 /* Rescan breakpoints at the same address and section, marking the
12681 first one as "first" and any others as "duplicates". This is so
12682 that the bpt instruction is only inserted once. If we have a
12683 permanent breakpoint at the same place as BPT, make that one the
12684 official one, and the rest as duplicates. Permanent breakpoints
12685 are sorted first for the same address.
12686
12687 Do the same for hardware watchpoints, but also considering the
12688 watchpoint's type (regular/access/read) and length. */
12689
12690 bp_loc_first = NULL;
12691 wp_loc_first = NULL;
12692 awp_loc_first = NULL;
12693 rwp_loc_first = NULL;
12694 ALL_BP_LOCATIONS (loc, locp)
12695 {
12696 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12697 non-NULL. */
12698 struct bp_location **loc_first_p;
12699 b = loc->owner;
12700
12701 if (!unduplicated_should_be_inserted (loc)
12702 || !breakpoint_address_is_meaningful (b)
12703 /* Don't detect duplicate for tracepoint locations because they are
12704 never duplicated. See the comments in field `duplicate' of
12705 `struct bp_location'. */
12706 || is_tracepoint (b))
12707 {
12708 /* Clear the condition modification flag. */
12709 loc->condition_changed = condition_unchanged;
12710 continue;
12711 }
12712
12713 if (b->type == bp_hardware_watchpoint)
12714 loc_first_p = &wp_loc_first;
12715 else if (b->type == bp_read_watchpoint)
12716 loc_first_p = &rwp_loc_first;
12717 else if (b->type == bp_access_watchpoint)
12718 loc_first_p = &awp_loc_first;
12719 else
12720 loc_first_p = &bp_loc_first;
12721
12722 if (*loc_first_p == NULL
12723 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12724 || !breakpoint_locations_match (loc, *loc_first_p))
12725 {
12726 *loc_first_p = loc;
12727 loc->duplicate = 0;
12728
12729 if (is_breakpoint (loc->owner) && loc->condition_changed)
12730 {
12731 loc->needs_update = 1;
12732 /* Clear the condition modification flag. */
12733 loc->condition_changed = condition_unchanged;
12734 }
12735 continue;
12736 }
12737
12738
12739 /* This and the above ensure the invariant that the first location
12740 is not duplicated, and is the inserted one.
12741 All following are marked as duplicated, and are not inserted. */
12742 if (loc->inserted)
12743 swap_insertion (loc, *loc_first_p);
12744 loc->duplicate = 1;
12745
12746 /* Clear the condition modification flag. */
12747 loc->condition_changed = condition_unchanged;
12748 }
12749
12750 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
12751 {
12752 if (insert_mode != UGLL_DONT_INSERT)
12753 insert_breakpoint_locations ();
12754 else
12755 {
12756 /* Even though the caller told us to not insert new
12757 locations, we may still need to update conditions on the
12758 target's side of breakpoints that were already inserted
12759 if the target is evaluating breakpoint conditions. We
12760 only update conditions for locations that are marked
12761 "needs_update". */
12762 update_inserted_breakpoint_locations ();
12763 }
12764 }
12765
12766 if (insert_mode != UGLL_DONT_INSERT)
12767 download_tracepoint_locations ();
12768
12769 do_cleanups (cleanups);
12770 }
12771
12772 void
12773 breakpoint_retire_moribund (void)
12774 {
12775 struct bp_location *loc;
12776 int ix;
12777
12778 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12779 if (--(loc->events_till_retirement) == 0)
12780 {
12781 decref_bp_location (&loc);
12782 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12783 --ix;
12784 }
12785 }
12786
12787 static void
12788 update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
12789 {
12790
12791 TRY
12792 {
12793 update_global_location_list (insert_mode);
12794 }
12795 CATCH (e, RETURN_MASK_ERROR)
12796 {
12797 }
12798 END_CATCH
12799 }
12800
12801 /* Clear BKP from a BPS. */
12802
12803 static void
12804 bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12805 {
12806 bpstat bs;
12807
12808 for (bs = bps; bs; bs = bs->next)
12809 if (bs->breakpoint_at == bpt)
12810 {
12811 bs->breakpoint_at = NULL;
12812 bs->old_val = NULL;
12813 /* bs->commands will be freed later. */
12814 }
12815 }
12816
12817 /* Callback for iterate_over_threads. */
12818 static int
12819 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12820 {
12821 struct breakpoint *bpt = (struct breakpoint *) data;
12822
12823 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12824 return 0;
12825 }
12826
12827 /* Helper for breakpoint and tracepoint breakpoint_ops->mention
12828 callbacks. */
12829
12830 static void
12831 say_where (struct breakpoint *b)
12832 {
12833 struct value_print_options opts;
12834
12835 get_user_print_options (&opts);
12836
12837 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12838 single string. */
12839 if (b->loc == NULL)
12840 {
12841 /* For pending locations, the output differs slightly based
12842 on b->extra_string. If this is non-NULL, it contains either
12843 a condition or dprintf arguments. */
12844 if (b->extra_string == NULL)
12845 {
12846 printf_filtered (_(" (%s) pending."),
12847 event_location_to_string (b->location));
12848 }
12849 else if (b->type == bp_dprintf)
12850 {
12851 printf_filtered (_(" (%s,%s) pending."),
12852 event_location_to_string (b->location),
12853 b->extra_string);
12854 }
12855 else
12856 {
12857 printf_filtered (_(" (%s %s) pending."),
12858 event_location_to_string (b->location),
12859 b->extra_string);
12860 }
12861 }
12862 else
12863 {
12864 if (opts.addressprint || b->loc->symtab == NULL)
12865 {
12866 printf_filtered (" at ");
12867 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12868 gdb_stdout);
12869 }
12870 if (b->loc->symtab != NULL)
12871 {
12872 /* If there is a single location, we can print the location
12873 more nicely. */
12874 if (b->loc->next == NULL)
12875 printf_filtered (": file %s, line %d.",
12876 symtab_to_filename_for_display (b->loc->symtab),
12877 b->loc->line_number);
12878 else
12879 /* This is not ideal, but each location may have a
12880 different file name, and this at least reflects the
12881 real situation somewhat. */
12882 printf_filtered (": %s.",
12883 event_location_to_string (b->location));
12884 }
12885
12886 if (b->loc->next)
12887 {
12888 struct bp_location *loc = b->loc;
12889 int n = 0;
12890 for (; loc; loc = loc->next)
12891 ++n;
12892 printf_filtered (" (%d locations)", n);
12893 }
12894 }
12895 }
12896
12897 /* Default bp_location_ops methods. */
12898
12899 static void
12900 bp_location_dtor (struct bp_location *self)
12901 {
12902 if (self->cond_bytecode)
12903 free_agent_expr (self->cond_bytecode);
12904 xfree (self->function_name);
12905
12906 VEC_free (agent_expr_p, self->target_info.conditions);
12907 VEC_free (agent_expr_p, self->target_info.tcommands);
12908 }
12909
12910 static const struct bp_location_ops bp_location_ops =
12911 {
12912 bp_location_dtor
12913 };
12914
12915 /* Default breakpoint_ops methods all breakpoint_ops ultimately
12916 inherit from. */
12917
12918 static void
12919 base_breakpoint_dtor (struct breakpoint *self)
12920 {
12921 decref_counted_command_line (&self->commands);
12922 xfree (self->cond_string);
12923 xfree (self->extra_string);
12924 xfree (self->filter);
12925 delete_event_location (self->location);
12926 delete_event_location (self->location_range_end);
12927 }
12928
12929 static struct bp_location *
12930 base_breakpoint_allocate_location (struct breakpoint *self)
12931 {
12932 struct bp_location *loc;
12933
12934 loc = new struct bp_location ();
12935 init_bp_location (loc, &bp_location_ops, self);
12936 return loc;
12937 }
12938
12939 static void
12940 base_breakpoint_re_set (struct breakpoint *b)
12941 {
12942 /* Nothing to re-set. */
12943 }
12944
12945 #define internal_error_pure_virtual_called() \
12946 gdb_assert_not_reached ("pure virtual function called")
12947
12948 static int
12949 base_breakpoint_insert_location (struct bp_location *bl)
12950 {
12951 internal_error_pure_virtual_called ();
12952 }
12953
12954 static int
12955 base_breakpoint_remove_location (struct bp_location *bl,
12956 enum remove_bp_reason reason)
12957 {
12958 internal_error_pure_virtual_called ();
12959 }
12960
12961 static int
12962 base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12963 struct address_space *aspace,
12964 CORE_ADDR bp_addr,
12965 const struct target_waitstatus *ws)
12966 {
12967 internal_error_pure_virtual_called ();
12968 }
12969
12970 static void
12971 base_breakpoint_check_status (bpstat bs)
12972 {
12973 /* Always stop. */
12974 }
12975
12976 /* A "works_in_software_mode" breakpoint_ops method that just internal
12977 errors. */
12978
12979 static int
12980 base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12981 {
12982 internal_error_pure_virtual_called ();
12983 }
12984
12985 /* A "resources_needed" breakpoint_ops method that just internal
12986 errors. */
12987
12988 static int
12989 base_breakpoint_resources_needed (const struct bp_location *bl)
12990 {
12991 internal_error_pure_virtual_called ();
12992 }
12993
12994 static enum print_stop_action
12995 base_breakpoint_print_it (bpstat bs)
12996 {
12997 internal_error_pure_virtual_called ();
12998 }
12999
13000 static void
13001 base_breakpoint_print_one_detail (const struct breakpoint *self,
13002 struct ui_out *uiout)
13003 {
13004 /* nothing */
13005 }
13006
13007 static void
13008 base_breakpoint_print_mention (struct breakpoint *b)
13009 {
13010 internal_error_pure_virtual_called ();
13011 }
13012
13013 static void
13014 base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
13015 {
13016 internal_error_pure_virtual_called ();
13017 }
13018
13019 static void
13020 base_breakpoint_create_sals_from_location
13021 (const struct event_location *location,
13022 struct linespec_result *canonical,
13023 enum bptype type_wanted)
13024 {
13025 internal_error_pure_virtual_called ();
13026 }
13027
13028 static void
13029 base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13030 struct linespec_result *c,
13031 char *cond_string,
13032 char *extra_string,
13033 enum bptype type_wanted,
13034 enum bpdisp disposition,
13035 int thread,
13036 int task, int ignore_count,
13037 const struct breakpoint_ops *o,
13038 int from_tty, int enabled,
13039 int internal, unsigned flags)
13040 {
13041 internal_error_pure_virtual_called ();
13042 }
13043
13044 static void
13045 base_breakpoint_decode_location (struct breakpoint *b,
13046 const struct event_location *location,
13047 struct program_space *search_pspace,
13048 struct symtabs_and_lines *sals)
13049 {
13050 internal_error_pure_virtual_called ();
13051 }
13052
13053 /* The default 'explains_signal' method. */
13054
13055 static int
13056 base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
13057 {
13058 return 1;
13059 }
13060
13061 /* The default "after_condition_true" method. */
13062
13063 static void
13064 base_breakpoint_after_condition_true (struct bpstats *bs)
13065 {
13066 /* Nothing to do. */
13067 }
13068
13069 struct breakpoint_ops base_breakpoint_ops =
13070 {
13071 base_breakpoint_dtor,
13072 base_breakpoint_allocate_location,
13073 base_breakpoint_re_set,
13074 base_breakpoint_insert_location,
13075 base_breakpoint_remove_location,
13076 base_breakpoint_breakpoint_hit,
13077 base_breakpoint_check_status,
13078 base_breakpoint_resources_needed,
13079 base_breakpoint_works_in_software_mode,
13080 base_breakpoint_print_it,
13081 NULL,
13082 base_breakpoint_print_one_detail,
13083 base_breakpoint_print_mention,
13084 base_breakpoint_print_recreate,
13085 base_breakpoint_create_sals_from_location,
13086 base_breakpoint_create_breakpoints_sal,
13087 base_breakpoint_decode_location,
13088 base_breakpoint_explains_signal,
13089 base_breakpoint_after_condition_true,
13090 };
13091
13092 /* Default breakpoint_ops methods. */
13093
13094 static void
13095 bkpt_re_set (struct breakpoint *b)
13096 {
13097 /* FIXME: is this still reachable? */
13098 if (breakpoint_event_location_empty_p (b))
13099 {
13100 /* Anything without a location can't be re-set. */
13101 delete_breakpoint (b);
13102 return;
13103 }
13104
13105 breakpoint_re_set_default (b);
13106 }
13107
13108 static int
13109 bkpt_insert_location (struct bp_location *bl)
13110 {
13111 CORE_ADDR addr = bl->target_info.reqstd_address;
13112
13113 bl->target_info.kind = breakpoint_kind (bl, &addr);
13114 bl->target_info.placed_address = addr;
13115
13116 if (bl->loc_type == bp_loc_hardware_breakpoint)
13117 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
13118 else
13119 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
13120 }
13121
13122 static int
13123 bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
13124 {
13125 if (bl->loc_type == bp_loc_hardware_breakpoint)
13126 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
13127 else
13128 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
13129 }
13130
13131 static int
13132 bkpt_breakpoint_hit (const struct bp_location *bl,
13133 struct address_space *aspace, CORE_ADDR bp_addr,
13134 const struct target_waitstatus *ws)
13135 {
13136 if (ws->kind != TARGET_WAITKIND_STOPPED
13137 || ws->value.sig != GDB_SIGNAL_TRAP)
13138 return 0;
13139
13140 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
13141 aspace, bp_addr))
13142 return 0;
13143
13144 if (overlay_debugging /* unmapped overlay section */
13145 && section_is_overlay (bl->section)
13146 && !section_is_mapped (bl->section))
13147 return 0;
13148
13149 return 1;
13150 }
13151
13152 static int
13153 dprintf_breakpoint_hit (const struct bp_location *bl,
13154 struct address_space *aspace, CORE_ADDR bp_addr,
13155 const struct target_waitstatus *ws)
13156 {
13157 if (dprintf_style == dprintf_style_agent
13158 && target_can_run_breakpoint_commands ())
13159 {
13160 /* An agent-style dprintf never causes a stop. If we see a trap
13161 for this address it must be for a breakpoint that happens to
13162 be set at the same address. */
13163 return 0;
13164 }
13165
13166 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
13167 }
13168
13169 static int
13170 bkpt_resources_needed (const struct bp_location *bl)
13171 {
13172 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
13173
13174 return 1;
13175 }
13176
13177 static enum print_stop_action
13178 bkpt_print_it (bpstat bs)
13179 {
13180 struct breakpoint *b;
13181 const struct bp_location *bl;
13182 int bp_temp;
13183 struct ui_out *uiout = current_uiout;
13184
13185 gdb_assert (bs->bp_location_at != NULL);
13186
13187 bl = bs->bp_location_at;
13188 b = bs->breakpoint_at;
13189
13190 bp_temp = b->disposition == disp_del;
13191 if (bl->address != bl->requested_address)
13192 breakpoint_adjustment_warning (bl->requested_address,
13193 bl->address,
13194 b->number, 1);
13195 annotate_breakpoint (b->number);
13196 maybe_print_thread_hit_breakpoint (uiout);
13197
13198 if (bp_temp)
13199 ui_out_text (uiout, "Temporary breakpoint ");
13200 else
13201 ui_out_text (uiout, "Breakpoint ");
13202 if (ui_out_is_mi_like_p (uiout))
13203 {
13204 ui_out_field_string (uiout, "reason",
13205 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
13206 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
13207 }
13208 ui_out_field_int (uiout, "bkptno", b->number);
13209 ui_out_text (uiout, ", ");
13210
13211 return PRINT_SRC_AND_LOC;
13212 }
13213
13214 static void
13215 bkpt_print_mention (struct breakpoint *b)
13216 {
13217 if (ui_out_is_mi_like_p (current_uiout))
13218 return;
13219
13220 switch (b->type)
13221 {
13222 case bp_breakpoint:
13223 case bp_gnu_ifunc_resolver:
13224 if (b->disposition == disp_del)
13225 printf_filtered (_("Temporary breakpoint"));
13226 else
13227 printf_filtered (_("Breakpoint"));
13228 printf_filtered (_(" %d"), b->number);
13229 if (b->type == bp_gnu_ifunc_resolver)
13230 printf_filtered (_(" at gnu-indirect-function resolver"));
13231 break;
13232 case bp_hardware_breakpoint:
13233 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
13234 break;
13235 case bp_dprintf:
13236 printf_filtered (_("Dprintf %d"), b->number);
13237 break;
13238 }
13239
13240 say_where (b);
13241 }
13242
13243 static void
13244 bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13245 {
13246 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
13247 fprintf_unfiltered (fp, "tbreak");
13248 else if (tp->type == bp_breakpoint)
13249 fprintf_unfiltered (fp, "break");
13250 else if (tp->type == bp_hardware_breakpoint
13251 && tp->disposition == disp_del)
13252 fprintf_unfiltered (fp, "thbreak");
13253 else if (tp->type == bp_hardware_breakpoint)
13254 fprintf_unfiltered (fp, "hbreak");
13255 else
13256 internal_error (__FILE__, __LINE__,
13257 _("unhandled breakpoint type %d"), (int) tp->type);
13258
13259 fprintf_unfiltered (fp, " %s",
13260 event_location_to_string (tp->location));
13261
13262 /* Print out extra_string if this breakpoint is pending. It might
13263 contain, for example, conditions that were set by the user. */
13264 if (tp->loc == NULL && tp->extra_string != NULL)
13265 fprintf_unfiltered (fp, " %s", tp->extra_string);
13266
13267 print_recreate_thread (tp, fp);
13268 }
13269
13270 static void
13271 bkpt_create_sals_from_location (const struct event_location *location,
13272 struct linespec_result *canonical,
13273 enum bptype type_wanted)
13274 {
13275 create_sals_from_location_default (location, canonical, type_wanted);
13276 }
13277
13278 static void
13279 bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
13280 struct linespec_result *canonical,
13281 char *cond_string,
13282 char *extra_string,
13283 enum bptype type_wanted,
13284 enum bpdisp disposition,
13285 int thread,
13286 int task, int ignore_count,
13287 const struct breakpoint_ops *ops,
13288 int from_tty, int enabled,
13289 int internal, unsigned flags)
13290 {
13291 create_breakpoints_sal_default (gdbarch, canonical,
13292 cond_string, extra_string,
13293 type_wanted,
13294 disposition, thread, task,
13295 ignore_count, ops, from_tty,
13296 enabled, internal, flags);
13297 }
13298
13299 static void
13300 bkpt_decode_location (struct breakpoint *b,
13301 const struct event_location *location,
13302 struct program_space *search_pspace,
13303 struct symtabs_and_lines *sals)
13304 {
13305 decode_location_default (b, location, search_pspace, sals);
13306 }
13307
13308 /* Virtual table for internal breakpoints. */
13309
13310 static void
13311 internal_bkpt_re_set (struct breakpoint *b)
13312 {
13313 switch (b->type)
13314 {
13315 /* Delete overlay event and longjmp master breakpoints; they
13316 will be reset later by breakpoint_re_set. */
13317 case bp_overlay_event:
13318 case bp_longjmp_master:
13319 case bp_std_terminate_master:
13320 case bp_exception_master:
13321 delete_breakpoint (b);
13322 break;
13323
13324 /* This breakpoint is special, it's set up when the inferior
13325 starts and we really don't want to touch it. */
13326 case bp_shlib_event:
13327
13328 /* Like bp_shlib_event, this breakpoint type is special. Once
13329 it is set up, we do not want to touch it. */
13330 case bp_thread_event:
13331 break;
13332 }
13333 }
13334
13335 static void
13336 internal_bkpt_check_status (bpstat bs)
13337 {
13338 if (bs->breakpoint_at->type == bp_shlib_event)
13339 {
13340 /* If requested, stop when the dynamic linker notifies GDB of
13341 events. This allows the user to get control and place
13342 breakpoints in initializer routines for dynamically loaded
13343 objects (among other things). */
13344 bs->stop = stop_on_solib_events;
13345 bs->print = stop_on_solib_events;
13346 }
13347 else
13348 bs->stop = 0;
13349 }
13350
13351 static enum print_stop_action
13352 internal_bkpt_print_it (bpstat bs)
13353 {
13354 struct breakpoint *b;
13355
13356 b = bs->breakpoint_at;
13357
13358 switch (b->type)
13359 {
13360 case bp_shlib_event:
13361 /* Did we stop because the user set the stop_on_solib_events
13362 variable? (If so, we report this as a generic, "Stopped due
13363 to shlib event" message.) */
13364 print_solib_event (0);
13365 break;
13366
13367 case bp_thread_event:
13368 /* Not sure how we will get here.
13369 GDB should not stop for these breakpoints. */
13370 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
13371 break;
13372
13373 case bp_overlay_event:
13374 /* By analogy with the thread event, GDB should not stop for these. */
13375 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
13376 break;
13377
13378 case bp_longjmp_master:
13379 /* These should never be enabled. */
13380 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
13381 break;
13382
13383 case bp_std_terminate_master:
13384 /* These should never be enabled. */
13385 printf_filtered (_("std::terminate Master Breakpoint: "
13386 "gdb should not stop!\n"));
13387 break;
13388
13389 case bp_exception_master:
13390 /* These should never be enabled. */
13391 printf_filtered (_("Exception Master Breakpoint: "
13392 "gdb should not stop!\n"));
13393 break;
13394 }
13395
13396 return PRINT_NOTHING;
13397 }
13398
13399 static void
13400 internal_bkpt_print_mention (struct breakpoint *b)
13401 {
13402 /* Nothing to mention. These breakpoints are internal. */
13403 }
13404
13405 /* Virtual table for momentary breakpoints */
13406
13407 static void
13408 momentary_bkpt_re_set (struct breakpoint *b)
13409 {
13410 /* Keep temporary breakpoints, which can be encountered when we step
13411 over a dlopen call and solib_add is resetting the breakpoints.
13412 Otherwise these should have been blown away via the cleanup chain
13413 or by breakpoint_init_inferior when we rerun the executable. */
13414 }
13415
13416 static void
13417 momentary_bkpt_check_status (bpstat bs)
13418 {
13419 /* Nothing. The point of these breakpoints is causing a stop. */
13420 }
13421
13422 static enum print_stop_action
13423 momentary_bkpt_print_it (bpstat bs)
13424 {
13425 return PRINT_UNKNOWN;
13426 }
13427
13428 static void
13429 momentary_bkpt_print_mention (struct breakpoint *b)
13430 {
13431 /* Nothing to mention. These breakpoints are internal. */
13432 }
13433
13434 /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
13435
13436 It gets cleared already on the removal of the first one of such placed
13437 breakpoints. This is OK as they get all removed altogether. */
13438
13439 static void
13440 longjmp_bkpt_dtor (struct breakpoint *self)
13441 {
13442 struct thread_info *tp = find_thread_global_id (self->thread);
13443
13444 if (tp)
13445 tp->initiating_frame = null_frame_id;
13446
13447 momentary_breakpoint_ops.dtor (self);
13448 }
13449
13450 /* Specific methods for probe breakpoints. */
13451
13452 static int
13453 bkpt_probe_insert_location (struct bp_location *bl)
13454 {
13455 int v = bkpt_insert_location (bl);
13456
13457 if (v == 0)
13458 {
13459 /* The insertion was successful, now let's set the probe's semaphore
13460 if needed. */
13461 if (bl->probe.probe->pops->set_semaphore != NULL)
13462 bl->probe.probe->pops->set_semaphore (bl->probe.probe,
13463 bl->probe.objfile,
13464 bl->gdbarch);
13465 }
13466
13467 return v;
13468 }
13469
13470 static int
13471 bkpt_probe_remove_location (struct bp_location *bl,
13472 enum remove_bp_reason reason)
13473 {
13474 /* Let's clear the semaphore before removing the location. */
13475 if (bl->probe.probe->pops->clear_semaphore != NULL)
13476 bl->probe.probe->pops->clear_semaphore (bl->probe.probe,
13477 bl->probe.objfile,
13478 bl->gdbarch);
13479
13480 return bkpt_remove_location (bl, reason);
13481 }
13482
13483 static void
13484 bkpt_probe_create_sals_from_location (const struct event_location *location,
13485 struct linespec_result *canonical,
13486 enum bptype type_wanted)
13487 {
13488 struct linespec_sals lsal;
13489
13490 lsal.sals = parse_probes (location, NULL, canonical);
13491 lsal.canonical = xstrdup (event_location_to_string (canonical->location));
13492 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13493 }
13494
13495 static void
13496 bkpt_probe_decode_location (struct breakpoint *b,
13497 const struct event_location *location,
13498 struct program_space *search_pspace,
13499 struct symtabs_and_lines *sals)
13500 {
13501 *sals = parse_probes (location, search_pspace, NULL);
13502 if (!sals->sals)
13503 error (_("probe not found"));
13504 }
13505
13506 /* The breakpoint_ops structure to be used in tracepoints. */
13507
13508 static void
13509 tracepoint_re_set (struct breakpoint *b)
13510 {
13511 breakpoint_re_set_default (b);
13512 }
13513
13514 static int
13515 tracepoint_breakpoint_hit (const struct bp_location *bl,
13516 struct address_space *aspace, CORE_ADDR bp_addr,
13517 const struct target_waitstatus *ws)
13518 {
13519 /* By definition, the inferior does not report stops at
13520 tracepoints. */
13521 return 0;
13522 }
13523
13524 static void
13525 tracepoint_print_one_detail (const struct breakpoint *self,
13526 struct ui_out *uiout)
13527 {
13528 struct tracepoint *tp = (struct tracepoint *) self;
13529 if (tp->static_trace_marker_id)
13530 {
13531 gdb_assert (self->type == bp_static_tracepoint);
13532
13533 ui_out_text (uiout, "\tmarker id is ");
13534 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
13535 tp->static_trace_marker_id);
13536 ui_out_text (uiout, "\n");
13537 }
13538 }
13539
13540 static void
13541 tracepoint_print_mention (struct breakpoint *b)
13542 {
13543 if (ui_out_is_mi_like_p (current_uiout))
13544 return;
13545
13546 switch (b->type)
13547 {
13548 case bp_tracepoint:
13549 printf_filtered (_("Tracepoint"));
13550 printf_filtered (_(" %d"), b->number);
13551 break;
13552 case bp_fast_tracepoint:
13553 printf_filtered (_("Fast tracepoint"));
13554 printf_filtered (_(" %d"), b->number);
13555 break;
13556 case bp_static_tracepoint:
13557 printf_filtered (_("Static tracepoint"));
13558 printf_filtered (_(" %d"), b->number);
13559 break;
13560 default:
13561 internal_error (__FILE__, __LINE__,
13562 _("unhandled tracepoint type %d"), (int) b->type);
13563 }
13564
13565 say_where (b);
13566 }
13567
13568 static void
13569 tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
13570 {
13571 struct tracepoint *tp = (struct tracepoint *) self;
13572
13573 if (self->type == bp_fast_tracepoint)
13574 fprintf_unfiltered (fp, "ftrace");
13575 else if (self->type == bp_static_tracepoint)
13576 fprintf_unfiltered (fp, "strace");
13577 else if (self->type == bp_tracepoint)
13578 fprintf_unfiltered (fp, "trace");
13579 else
13580 internal_error (__FILE__, __LINE__,
13581 _("unhandled tracepoint type %d"), (int) self->type);
13582
13583 fprintf_unfiltered (fp, " %s",
13584 event_location_to_string (self->location));
13585 print_recreate_thread (self, fp);
13586
13587 if (tp->pass_count)
13588 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
13589 }
13590
13591 static void
13592 tracepoint_create_sals_from_location (const struct event_location *location,
13593 struct linespec_result *canonical,
13594 enum bptype type_wanted)
13595 {
13596 create_sals_from_location_default (location, canonical, type_wanted);
13597 }
13598
13599 static void
13600 tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13601 struct linespec_result *canonical,
13602 char *cond_string,
13603 char *extra_string,
13604 enum bptype type_wanted,
13605 enum bpdisp disposition,
13606 int thread,
13607 int task, int ignore_count,
13608 const struct breakpoint_ops *ops,
13609 int from_tty, int enabled,
13610 int internal, unsigned flags)
13611 {
13612 create_breakpoints_sal_default (gdbarch, canonical,
13613 cond_string, extra_string,
13614 type_wanted,
13615 disposition, thread, task,
13616 ignore_count, ops, from_tty,
13617 enabled, internal, flags);
13618 }
13619
13620 static void
13621 tracepoint_decode_location (struct breakpoint *b,
13622 const struct event_location *location,
13623 struct program_space *search_pspace,
13624 struct symtabs_and_lines *sals)
13625 {
13626 decode_location_default (b, location, search_pspace, sals);
13627 }
13628
13629 struct breakpoint_ops tracepoint_breakpoint_ops;
13630
13631 /* The breakpoint_ops structure to be use on tracepoints placed in a
13632 static probe. */
13633
13634 static void
13635 tracepoint_probe_create_sals_from_location
13636 (const struct event_location *location,
13637 struct linespec_result *canonical,
13638 enum bptype type_wanted)
13639 {
13640 /* We use the same method for breakpoint on probes. */
13641 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
13642 }
13643
13644 static void
13645 tracepoint_probe_decode_location (struct breakpoint *b,
13646 const struct event_location *location,
13647 struct program_space *search_pspace,
13648 struct symtabs_and_lines *sals)
13649 {
13650 /* We use the same method for breakpoint on probes. */
13651 bkpt_probe_decode_location (b, location, search_pspace, sals);
13652 }
13653
13654 static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13655
13656 /* Dprintf breakpoint_ops methods. */
13657
13658 static void
13659 dprintf_re_set (struct breakpoint *b)
13660 {
13661 breakpoint_re_set_default (b);
13662
13663 /* extra_string should never be non-NULL for dprintf. */
13664 gdb_assert (b->extra_string != NULL);
13665
13666 /* 1 - connect to target 1, that can run breakpoint commands.
13667 2 - create a dprintf, which resolves fine.
13668 3 - disconnect from target 1
13669 4 - connect to target 2, that can NOT run breakpoint commands.
13670
13671 After steps #3/#4, you'll want the dprintf command list to
13672 be updated, because target 1 and 2 may well return different
13673 answers for target_can_run_breakpoint_commands().
13674 Given absence of finer grained resetting, we get to do
13675 it all the time. */
13676 if (b->extra_string != NULL)
13677 update_dprintf_command_list (b);
13678 }
13679
13680 /* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13681
13682 static void
13683 dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13684 {
13685 fprintf_unfiltered (fp, "dprintf %s,%s",
13686 event_location_to_string (tp->location),
13687 tp->extra_string);
13688 print_recreate_thread (tp, fp);
13689 }
13690
13691 /* Implement the "after_condition_true" breakpoint_ops method for
13692 dprintf.
13693
13694 dprintf's are implemented with regular commands in their command
13695 list, but we run the commands here instead of before presenting the
13696 stop to the user, as dprintf's don't actually cause a stop. This
13697 also makes it so that the commands of multiple dprintfs at the same
13698 address are all handled. */
13699
13700 static void
13701 dprintf_after_condition_true (struct bpstats *bs)
13702 {
13703 struct cleanup *old_chain;
13704 struct bpstats tmp_bs = { NULL };
13705 struct bpstats *tmp_bs_p = &tmp_bs;
13706
13707 /* dprintf's never cause a stop. This wasn't set in the
13708 check_status hook instead because that would make the dprintf's
13709 condition not be evaluated. */
13710 bs->stop = 0;
13711
13712 /* Run the command list here. Take ownership of it instead of
13713 copying. We never want these commands to run later in
13714 bpstat_do_actions, if a breakpoint that causes a stop happens to
13715 be set at same address as this dprintf, or even if running the
13716 commands here throws. */
13717 tmp_bs.commands = bs->commands;
13718 bs->commands = NULL;
13719 old_chain = make_cleanup_decref_counted_command_line (&tmp_bs.commands);
13720
13721 bpstat_do_actions_1 (&tmp_bs_p);
13722
13723 /* 'tmp_bs.commands' will usually be NULL by now, but
13724 bpstat_do_actions_1 may return early without processing the whole
13725 list. */
13726 do_cleanups (old_chain);
13727 }
13728
13729 /* The breakpoint_ops structure to be used on static tracepoints with
13730 markers (`-m'). */
13731
13732 static void
13733 strace_marker_create_sals_from_location (const struct event_location *location,
13734 struct linespec_result *canonical,
13735 enum bptype type_wanted)
13736 {
13737 struct linespec_sals lsal;
13738 const char *arg_start, *arg;
13739 char *str;
13740 struct cleanup *cleanup;
13741
13742 arg = arg_start = get_linespec_location (location);
13743 lsal.sals = decode_static_tracepoint_spec (&arg);
13744
13745 str = savestring (arg_start, arg - arg_start);
13746 cleanup = make_cleanup (xfree, str);
13747 canonical->location = new_linespec_location (&str);
13748 do_cleanups (cleanup);
13749
13750 lsal.canonical = xstrdup (event_location_to_string (canonical->location));
13751 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13752 }
13753
13754 static void
13755 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13756 struct linespec_result *canonical,
13757 char *cond_string,
13758 char *extra_string,
13759 enum bptype type_wanted,
13760 enum bpdisp disposition,
13761 int thread,
13762 int task, int ignore_count,
13763 const struct breakpoint_ops *ops,
13764 int from_tty, int enabled,
13765 int internal, unsigned flags)
13766 {
13767 int i;
13768 struct linespec_sals *lsal = VEC_index (linespec_sals,
13769 canonical->sals, 0);
13770
13771 /* If the user is creating a static tracepoint by marker id
13772 (strace -m MARKER_ID), then store the sals index, so that
13773 breakpoint_re_set can try to match up which of the newly
13774 found markers corresponds to this one, and, don't try to
13775 expand multiple locations for each sal, given than SALS
13776 already should contain all sals for MARKER_ID. */
13777
13778 for (i = 0; i < lsal->sals.nelts; ++i)
13779 {
13780 struct symtabs_and_lines expanded;
13781 struct tracepoint *tp;
13782 struct cleanup *old_chain;
13783 struct event_location *location;
13784
13785 expanded.nelts = 1;
13786 expanded.sals = &lsal->sals.sals[i];
13787
13788 location = copy_event_location (canonical->location);
13789 old_chain = make_cleanup_delete_event_location (location);
13790
13791 tp = new tracepoint ();
13792 init_breakpoint_sal (&tp->base, gdbarch, expanded,
13793 location, NULL,
13794 cond_string, extra_string,
13795 type_wanted, disposition,
13796 thread, task, ignore_count, ops,
13797 from_tty, enabled, internal, flags,
13798 canonical->special_display);
13799 /* Given that its possible to have multiple markers with
13800 the same string id, if the user is creating a static
13801 tracepoint by marker id ("strace -m MARKER_ID"), then
13802 store the sals index, so that breakpoint_re_set can
13803 try to match up which of the newly found markers
13804 corresponds to this one */
13805 tp->static_trace_marker_id_idx = i;
13806
13807 install_breakpoint (internal, &tp->base, 0);
13808
13809 discard_cleanups (old_chain);
13810 }
13811 }
13812
13813 static void
13814 strace_marker_decode_location (struct breakpoint *b,
13815 const struct event_location *location,
13816 struct program_space *search_pspace,
13817 struct symtabs_and_lines *sals)
13818 {
13819 struct tracepoint *tp = (struct tracepoint *) b;
13820 const char *s = get_linespec_location (location);
13821
13822 *sals = decode_static_tracepoint_spec (&s);
13823 if (sals->nelts > tp->static_trace_marker_id_idx)
13824 {
13825 sals->sals[0] = sals->sals[tp->static_trace_marker_id_idx];
13826 sals->nelts = 1;
13827 }
13828 else
13829 error (_("marker %s not found"), tp->static_trace_marker_id);
13830 }
13831
13832 static struct breakpoint_ops strace_marker_breakpoint_ops;
13833
13834 static int
13835 strace_marker_p (struct breakpoint *b)
13836 {
13837 return b->ops == &strace_marker_breakpoint_ops;
13838 }
13839
13840 /* Delete a breakpoint and clean up all traces of it in the data
13841 structures. */
13842
13843 void
13844 delete_breakpoint (struct breakpoint *bpt)
13845 {
13846 struct breakpoint *b;
13847
13848 gdb_assert (bpt != NULL);
13849
13850 /* Has this bp already been deleted? This can happen because
13851 multiple lists can hold pointers to bp's. bpstat lists are
13852 especial culprits.
13853
13854 One example of this happening is a watchpoint's scope bp. When
13855 the scope bp triggers, we notice that the watchpoint is out of
13856 scope, and delete it. We also delete its scope bp. But the
13857 scope bp is marked "auto-deleting", and is already on a bpstat.
13858 That bpstat is then checked for auto-deleting bp's, which are
13859 deleted.
13860
13861 A real solution to this problem might involve reference counts in
13862 bp's, and/or giving them pointers back to their referencing
13863 bpstat's, and teaching delete_breakpoint to only free a bp's
13864 storage when no more references were extent. A cheaper bandaid
13865 was chosen. */
13866 if (bpt->type == bp_none)
13867 return;
13868
13869 /* At least avoid this stale reference until the reference counting
13870 of breakpoints gets resolved. */
13871 if (bpt->related_breakpoint != bpt)
13872 {
13873 struct breakpoint *related;
13874 struct watchpoint *w;
13875
13876 if (bpt->type == bp_watchpoint_scope)
13877 w = (struct watchpoint *) bpt->related_breakpoint;
13878 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
13879 w = (struct watchpoint *) bpt;
13880 else
13881 w = NULL;
13882 if (w != NULL)
13883 watchpoint_del_at_next_stop (w);
13884
13885 /* Unlink bpt from the bpt->related_breakpoint ring. */
13886 for (related = bpt; related->related_breakpoint != bpt;
13887 related = related->related_breakpoint);
13888 related->related_breakpoint = bpt->related_breakpoint;
13889 bpt->related_breakpoint = bpt;
13890 }
13891
13892 /* watch_command_1 creates a watchpoint but only sets its number if
13893 update_watchpoint succeeds in creating its bp_locations. If there's
13894 a problem in that process, we'll be asked to delete the half-created
13895 watchpoint. In that case, don't announce the deletion. */
13896 if (bpt->number)
13897 observer_notify_breakpoint_deleted (bpt);
13898
13899 if (breakpoint_chain == bpt)
13900 breakpoint_chain = bpt->next;
13901
13902 ALL_BREAKPOINTS (b)
13903 if (b->next == bpt)
13904 {
13905 b->next = bpt->next;
13906 break;
13907 }
13908
13909 /* Be sure no bpstat's are pointing at the breakpoint after it's
13910 been freed. */
13911 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
13912 in all threads for now. Note that we cannot just remove bpstats
13913 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13914 commands are associated with the bpstat; if we remove it here,
13915 then the later call to bpstat_do_actions (&stop_bpstat); in
13916 event-top.c won't do anything, and temporary breakpoints with
13917 commands won't work. */
13918
13919 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13920
13921 /* Now that breakpoint is removed from breakpoint list, update the
13922 global location list. This will remove locations that used to
13923 belong to this breakpoint. Do this before freeing the breakpoint
13924 itself, since remove_breakpoint looks at location's owner. It
13925 might be better design to have location completely
13926 self-contained, but it's not the case now. */
13927 update_global_location_list (UGLL_DONT_INSERT);
13928
13929 bpt->ops->dtor (bpt);
13930 /* On the chance that someone will soon try again to delete this
13931 same bp, we mark it as deleted before freeing its storage. */
13932 bpt->type = bp_none;
13933 delete bpt;
13934 }
13935
13936 static void
13937 do_delete_breakpoint_cleanup (void *b)
13938 {
13939 delete_breakpoint ((struct breakpoint *) b);
13940 }
13941
13942 struct cleanup *
13943 make_cleanup_delete_breakpoint (struct breakpoint *b)
13944 {
13945 return make_cleanup (do_delete_breakpoint_cleanup, b);
13946 }
13947
13948 /* Iterator function to call a user-provided callback function once
13949 for each of B and its related breakpoints. */
13950
13951 static void
13952 iterate_over_related_breakpoints (struct breakpoint *b,
13953 void (*function) (struct breakpoint *,
13954 void *),
13955 void *data)
13956 {
13957 struct breakpoint *related;
13958
13959 related = b;
13960 do
13961 {
13962 struct breakpoint *next;
13963
13964 /* FUNCTION may delete RELATED. */
13965 next = related->related_breakpoint;
13966
13967 if (next == related)
13968 {
13969 /* RELATED is the last ring entry. */
13970 function (related, data);
13971
13972 /* FUNCTION may have deleted it, so we'd never reach back to
13973 B. There's nothing left to do anyway, so just break
13974 out. */
13975 break;
13976 }
13977 else
13978 function (related, data);
13979
13980 related = next;
13981 }
13982 while (related != b);
13983 }
13984
13985 static void
13986 do_delete_breakpoint (struct breakpoint *b, void *ignore)
13987 {
13988 delete_breakpoint (b);
13989 }
13990
13991 /* A callback for map_breakpoint_numbers that calls
13992 delete_breakpoint. */
13993
13994 static void
13995 do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
13996 {
13997 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
13998 }
13999
14000 void
14001 delete_command (char *arg, int from_tty)
14002 {
14003 struct breakpoint *b, *b_tmp;
14004
14005 dont_repeat ();
14006
14007 if (arg == 0)
14008 {
14009 int breaks_to_delete = 0;
14010
14011 /* Delete all breakpoints if no argument. Do not delete
14012 internal breakpoints, these have to be deleted with an
14013 explicit breakpoint number argument. */
14014 ALL_BREAKPOINTS (b)
14015 if (user_breakpoint_p (b))
14016 {
14017 breaks_to_delete = 1;
14018 break;
14019 }
14020
14021 /* Ask user only if there are some breakpoints to delete. */
14022 if (!from_tty
14023 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
14024 {
14025 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14026 if (user_breakpoint_p (b))
14027 delete_breakpoint (b);
14028 }
14029 }
14030 else
14031 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
14032 }
14033
14034 /* Return true if all locations of B bound to PSPACE are pending. If
14035 PSPACE is NULL, all locations of all program spaces are
14036 considered. */
14037
14038 static int
14039 all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
14040 {
14041 struct bp_location *loc;
14042
14043 for (loc = b->loc; loc != NULL; loc = loc->next)
14044 if ((pspace == NULL
14045 || loc->pspace == pspace)
14046 && !loc->shlib_disabled
14047 && !loc->pspace->executing_startup)
14048 return 0;
14049 return 1;
14050 }
14051
14052 /* Subroutine of update_breakpoint_locations to simplify it.
14053 Return non-zero if multiple fns in list LOC have the same name.
14054 Null names are ignored. */
14055
14056 static int
14057 ambiguous_names_p (struct bp_location *loc)
14058 {
14059 struct bp_location *l;
14060 htab_t htab = htab_create_alloc (13, htab_hash_string,
14061 (int (*) (const void *,
14062 const void *)) streq,
14063 NULL, xcalloc, xfree);
14064
14065 for (l = loc; l != NULL; l = l->next)
14066 {
14067 const char **slot;
14068 const char *name = l->function_name;
14069
14070 /* Allow for some names to be NULL, ignore them. */
14071 if (name == NULL)
14072 continue;
14073
14074 slot = (const char **) htab_find_slot (htab, (const void *) name,
14075 INSERT);
14076 /* NOTE: We can assume slot != NULL here because xcalloc never
14077 returns NULL. */
14078 if (*slot != NULL)
14079 {
14080 htab_delete (htab);
14081 return 1;
14082 }
14083 *slot = name;
14084 }
14085
14086 htab_delete (htab);
14087 return 0;
14088 }
14089
14090 /* When symbols change, it probably means the sources changed as well,
14091 and it might mean the static tracepoint markers are no longer at
14092 the same address or line numbers they used to be at last we
14093 checked. Losing your static tracepoints whenever you rebuild is
14094 undesirable. This function tries to resync/rematch gdb static
14095 tracepoints with the markers on the target, for static tracepoints
14096 that have not been set by marker id. Static tracepoint that have
14097 been set by marker id are reset by marker id in breakpoint_re_set.
14098 The heuristic is:
14099
14100 1) For a tracepoint set at a specific address, look for a marker at
14101 the old PC. If one is found there, assume to be the same marker.
14102 If the name / string id of the marker found is different from the
14103 previous known name, assume that means the user renamed the marker
14104 in the sources, and output a warning.
14105
14106 2) For a tracepoint set at a given line number, look for a marker
14107 at the new address of the old line number. If one is found there,
14108 assume to be the same marker. If the name / string id of the
14109 marker found is different from the previous known name, assume that
14110 means the user renamed the marker in the sources, and output a
14111 warning.
14112
14113 3) If a marker is no longer found at the same address or line, it
14114 may mean the marker no longer exists. But it may also just mean
14115 the code changed a bit. Maybe the user added a few lines of code
14116 that made the marker move up or down (in line number terms). Ask
14117 the target for info about the marker with the string id as we knew
14118 it. If found, update line number and address in the matching
14119 static tracepoint. This will get confused if there's more than one
14120 marker with the same ID (possible in UST, although unadvised
14121 precisely because it confuses tools). */
14122
14123 static struct symtab_and_line
14124 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
14125 {
14126 struct tracepoint *tp = (struct tracepoint *) b;
14127 struct static_tracepoint_marker marker;
14128 CORE_ADDR pc;
14129
14130 pc = sal.pc;
14131 if (sal.line)
14132 find_line_pc (sal.symtab, sal.line, &pc);
14133
14134 if (target_static_tracepoint_marker_at (pc, &marker))
14135 {
14136 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
14137 warning (_("static tracepoint %d changed probed marker from %s to %s"),
14138 b->number,
14139 tp->static_trace_marker_id, marker.str_id);
14140
14141 xfree (tp->static_trace_marker_id);
14142 tp->static_trace_marker_id = xstrdup (marker.str_id);
14143 release_static_tracepoint_marker (&marker);
14144
14145 return sal;
14146 }
14147
14148 /* Old marker wasn't found on target at lineno. Try looking it up
14149 by string ID. */
14150 if (!sal.explicit_pc
14151 && sal.line != 0
14152 && sal.symtab != NULL
14153 && tp->static_trace_marker_id != NULL)
14154 {
14155 VEC(static_tracepoint_marker_p) *markers;
14156
14157 markers
14158 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
14159
14160 if (!VEC_empty(static_tracepoint_marker_p, markers))
14161 {
14162 struct symtab_and_line sal2;
14163 struct symbol *sym;
14164 struct static_tracepoint_marker *tpmarker;
14165 struct ui_out *uiout = current_uiout;
14166 struct explicit_location explicit_loc;
14167
14168 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
14169
14170 xfree (tp->static_trace_marker_id);
14171 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
14172
14173 warning (_("marker for static tracepoint %d (%s) not "
14174 "found at previous line number"),
14175 b->number, tp->static_trace_marker_id);
14176
14177 init_sal (&sal2);
14178
14179 sal2.pc = tpmarker->address;
14180
14181 sal2 = find_pc_line (tpmarker->address, 0);
14182 sym = find_pc_sect_function (tpmarker->address, NULL);
14183 ui_out_text (uiout, "Now in ");
14184 if (sym)
14185 {
14186 ui_out_field_string (uiout, "func",
14187 SYMBOL_PRINT_NAME (sym));
14188 ui_out_text (uiout, " at ");
14189 }
14190 ui_out_field_string (uiout, "file",
14191 symtab_to_filename_for_display (sal2.symtab));
14192 ui_out_text (uiout, ":");
14193
14194 if (ui_out_is_mi_like_p (uiout))
14195 {
14196 const char *fullname = symtab_to_fullname (sal2.symtab);
14197
14198 ui_out_field_string (uiout, "fullname", fullname);
14199 }
14200
14201 ui_out_field_int (uiout, "line", sal2.line);
14202 ui_out_text (uiout, "\n");
14203
14204 b->loc->line_number = sal2.line;
14205 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
14206
14207 delete_event_location (b->location);
14208 initialize_explicit_location (&explicit_loc);
14209 explicit_loc.source_filename
14210 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
14211 explicit_loc.line_offset.offset = b->loc->line_number;
14212 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
14213 b->location = new_explicit_location (&explicit_loc);
14214
14215 /* Might be nice to check if function changed, and warn if
14216 so. */
14217
14218 release_static_tracepoint_marker (tpmarker);
14219 }
14220 }
14221 return sal;
14222 }
14223
14224 /* Returns 1 iff locations A and B are sufficiently same that
14225 we don't need to report breakpoint as changed. */
14226
14227 static int
14228 locations_are_equal (struct bp_location *a, struct bp_location *b)
14229 {
14230 while (a && b)
14231 {
14232 if (a->address != b->address)
14233 return 0;
14234
14235 if (a->shlib_disabled != b->shlib_disabled)
14236 return 0;
14237
14238 if (a->enabled != b->enabled)
14239 return 0;
14240
14241 a = a->next;
14242 b = b->next;
14243 }
14244
14245 if ((a == NULL) != (b == NULL))
14246 return 0;
14247
14248 return 1;
14249 }
14250
14251 /* Split all locations of B that are bound to PSPACE out of B's
14252 location list to a separate list and return that list's head. If
14253 PSPACE is NULL, hoist out all locations of B. */
14254
14255 static struct bp_location *
14256 hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
14257 {
14258 struct bp_location head;
14259 struct bp_location *i = b->loc;
14260 struct bp_location **i_link = &b->loc;
14261 struct bp_location *hoisted = &head;
14262
14263 if (pspace == NULL)
14264 {
14265 i = b->loc;
14266 b->loc = NULL;
14267 return i;
14268 }
14269
14270 head.next = NULL;
14271
14272 while (i != NULL)
14273 {
14274 if (i->pspace == pspace)
14275 {
14276 *i_link = i->next;
14277 i->next = NULL;
14278 hoisted->next = i;
14279 hoisted = i;
14280 }
14281 else
14282 i_link = &i->next;
14283 i = *i_link;
14284 }
14285
14286 return head.next;
14287 }
14288
14289 /* Create new breakpoint locations for B (a hardware or software
14290 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
14291 zero, then B is a ranged breakpoint. Only recreates locations for
14292 FILTER_PSPACE. Locations of other program spaces are left
14293 untouched. */
14294
14295 void
14296 update_breakpoint_locations (struct breakpoint *b,
14297 struct program_space *filter_pspace,
14298 struct symtabs_and_lines sals,
14299 struct symtabs_and_lines sals_end)
14300 {
14301 int i;
14302 struct bp_location *existing_locations;
14303
14304 if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
14305 {
14306 /* Ranged breakpoints have only one start location and one end
14307 location. */
14308 b->enable_state = bp_disabled;
14309 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
14310 "multiple locations found\n"),
14311 b->number);
14312 return;
14313 }
14314
14315 /* If there's no new locations, and all existing locations are
14316 pending, don't do anything. This optimizes the common case where
14317 all locations are in the same shared library, that was unloaded.
14318 We'd like to retain the location, so that when the library is
14319 loaded again, we don't loose the enabled/disabled status of the
14320 individual locations. */
14321 if (all_locations_are_pending (b, filter_pspace) && sals.nelts == 0)
14322 return;
14323
14324 existing_locations = hoist_existing_locations (b, filter_pspace);
14325
14326 for (i = 0; i < sals.nelts; ++i)
14327 {
14328 struct bp_location *new_loc;
14329
14330 switch_to_program_space_and_thread (sals.sals[i].pspace);
14331
14332 new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
14333
14334 /* Reparse conditions, they might contain references to the
14335 old symtab. */
14336 if (b->cond_string != NULL)
14337 {
14338 const char *s;
14339
14340 s = b->cond_string;
14341 TRY
14342 {
14343 new_loc->cond = parse_exp_1 (&s, sals.sals[i].pc,
14344 block_for_pc (sals.sals[i].pc),
14345 0);
14346 }
14347 CATCH (e, RETURN_MASK_ERROR)
14348 {
14349 warning (_("failed to reevaluate condition "
14350 "for breakpoint %d: %s"),
14351 b->number, e.message);
14352 new_loc->enabled = 0;
14353 }
14354 END_CATCH
14355 }
14356
14357 if (sals_end.nelts)
14358 {
14359 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
14360
14361 new_loc->length = end - sals.sals[0].pc + 1;
14362 }
14363 }
14364
14365 /* If possible, carry over 'disable' status from existing
14366 breakpoints. */
14367 {
14368 struct bp_location *e = existing_locations;
14369 /* If there are multiple breakpoints with the same function name,
14370 e.g. for inline functions, comparing function names won't work.
14371 Instead compare pc addresses; this is just a heuristic as things
14372 may have moved, but in practice it gives the correct answer
14373 often enough until a better solution is found. */
14374 int have_ambiguous_names = ambiguous_names_p (b->loc);
14375
14376 for (; e; e = e->next)
14377 {
14378 if (!e->enabled && e->function_name)
14379 {
14380 struct bp_location *l = b->loc;
14381 if (have_ambiguous_names)
14382 {
14383 for (; l; l = l->next)
14384 if (breakpoint_locations_match (e, l))
14385 {
14386 l->enabled = 0;
14387 break;
14388 }
14389 }
14390 else
14391 {
14392 for (; l; l = l->next)
14393 if (l->function_name
14394 && strcmp (e->function_name, l->function_name) == 0)
14395 {
14396 l->enabled = 0;
14397 break;
14398 }
14399 }
14400 }
14401 }
14402 }
14403
14404 if (!locations_are_equal (existing_locations, b->loc))
14405 observer_notify_breakpoint_modified (b);
14406 }
14407
14408 /* Find the SaL locations corresponding to the given LOCATION.
14409 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
14410
14411 static struct symtabs_and_lines
14412 location_to_sals (struct breakpoint *b, struct event_location *location,
14413 struct program_space *search_pspace, int *found)
14414 {
14415 struct symtabs_and_lines sals = {0};
14416 struct gdb_exception exception = exception_none;
14417
14418 gdb_assert (b->ops != NULL);
14419
14420 TRY
14421 {
14422 b->ops->decode_location (b, location, search_pspace, &sals);
14423 }
14424 CATCH (e, RETURN_MASK_ERROR)
14425 {
14426 int not_found_and_ok = 0;
14427
14428 exception = e;
14429
14430 /* For pending breakpoints, it's expected that parsing will
14431 fail until the right shared library is loaded. User has
14432 already told to create pending breakpoints and don't need
14433 extra messages. If breakpoint is in bp_shlib_disabled
14434 state, then user already saw the message about that
14435 breakpoint being disabled, and don't want to see more
14436 errors. */
14437 if (e.error == NOT_FOUND_ERROR
14438 && (b->condition_not_parsed
14439 || (b->loc != NULL
14440 && search_pspace != NULL
14441 && b->loc->pspace != search_pspace)
14442 || (b->loc && b->loc->shlib_disabled)
14443 || (b->loc && b->loc->pspace->executing_startup)
14444 || b->enable_state == bp_disabled))
14445 not_found_and_ok = 1;
14446
14447 if (!not_found_and_ok)
14448 {
14449 /* We surely don't want to warn about the same breakpoint
14450 10 times. One solution, implemented here, is disable
14451 the breakpoint on error. Another solution would be to
14452 have separate 'warning emitted' flag. Since this
14453 happens only when a binary has changed, I don't know
14454 which approach is better. */
14455 b->enable_state = bp_disabled;
14456 throw_exception (e);
14457 }
14458 }
14459 END_CATCH
14460
14461 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
14462 {
14463 int i;
14464
14465 for (i = 0; i < sals.nelts; ++i)
14466 resolve_sal_pc (&sals.sals[i]);
14467 if (b->condition_not_parsed && b->extra_string != NULL)
14468 {
14469 char *cond_string, *extra_string;
14470 int thread, task;
14471
14472 find_condition_and_thread (b->extra_string, sals.sals[0].pc,
14473 &cond_string, &thread, &task,
14474 &extra_string);
14475 gdb_assert (b->cond_string == NULL);
14476 if (cond_string)
14477 b->cond_string = cond_string;
14478 b->thread = thread;
14479 b->task = task;
14480 if (extra_string)
14481 {
14482 xfree (b->extra_string);
14483 b->extra_string = extra_string;
14484 }
14485 b->condition_not_parsed = 0;
14486 }
14487
14488 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
14489 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
14490
14491 *found = 1;
14492 }
14493 else
14494 *found = 0;
14495
14496 return sals;
14497 }
14498
14499 /* The default re_set method, for typical hardware or software
14500 breakpoints. Reevaluate the breakpoint and recreate its
14501 locations. */
14502
14503 static void
14504 breakpoint_re_set_default (struct breakpoint *b)
14505 {
14506 int found;
14507 struct symtabs_and_lines sals, sals_end;
14508 struct symtabs_and_lines expanded = {0};
14509 struct symtabs_and_lines expanded_end = {0};
14510 struct program_space *filter_pspace = current_program_space;
14511
14512 sals = location_to_sals (b, b->location, filter_pspace, &found);
14513 if (found)
14514 {
14515 make_cleanup (xfree, sals.sals);
14516 expanded = sals;
14517 }
14518
14519 if (b->location_range_end != NULL)
14520 {
14521 sals_end = location_to_sals (b, b->location_range_end,
14522 filter_pspace, &found);
14523 if (found)
14524 {
14525 make_cleanup (xfree, sals_end.sals);
14526 expanded_end = sals_end;
14527 }
14528 }
14529
14530 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
14531 }
14532
14533 /* Default method for creating SALs from an address string. It basically
14534 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
14535
14536 static void
14537 create_sals_from_location_default (const struct event_location *location,
14538 struct linespec_result *canonical,
14539 enum bptype type_wanted)
14540 {
14541 parse_breakpoint_sals (location, canonical);
14542 }
14543
14544 /* Call create_breakpoints_sal for the given arguments. This is the default
14545 function for the `create_breakpoints_sal' method of
14546 breakpoint_ops. */
14547
14548 static void
14549 create_breakpoints_sal_default (struct gdbarch *gdbarch,
14550 struct linespec_result *canonical,
14551 char *cond_string,
14552 char *extra_string,
14553 enum bptype type_wanted,
14554 enum bpdisp disposition,
14555 int thread,
14556 int task, int ignore_count,
14557 const struct breakpoint_ops *ops,
14558 int from_tty, int enabled,
14559 int internal, unsigned flags)
14560 {
14561 create_breakpoints_sal (gdbarch, canonical, cond_string,
14562 extra_string,
14563 type_wanted, disposition,
14564 thread, task, ignore_count, ops, from_tty,
14565 enabled, internal, flags);
14566 }
14567
14568 /* Decode the line represented by S by calling decode_line_full. This is the
14569 default function for the `decode_location' method of breakpoint_ops. */
14570
14571 static void
14572 decode_location_default (struct breakpoint *b,
14573 const struct event_location *location,
14574 struct program_space *search_pspace,
14575 struct symtabs_and_lines *sals)
14576 {
14577 struct linespec_result canonical;
14578
14579 init_linespec_result (&canonical);
14580 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
14581 (struct symtab *) NULL, 0,
14582 &canonical, multiple_symbols_all,
14583 b->filter);
14584
14585 /* We should get 0 or 1 resulting SALs. */
14586 gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
14587
14588 if (VEC_length (linespec_sals, canonical.sals) > 0)
14589 {
14590 struct linespec_sals *lsal;
14591
14592 lsal = VEC_index (linespec_sals, canonical.sals, 0);
14593 *sals = lsal->sals;
14594 /* Arrange it so the destructor does not free the
14595 contents. */
14596 lsal->sals.sals = NULL;
14597 }
14598
14599 destroy_linespec_result (&canonical);
14600 }
14601
14602 /* Prepare the global context for a re-set of breakpoint B. */
14603
14604 static struct cleanup *
14605 prepare_re_set_context (struct breakpoint *b)
14606 {
14607 input_radix = b->input_radix;
14608 set_language (b->language);
14609
14610 return make_cleanup (null_cleanup, NULL);
14611 }
14612
14613 /* Reset a breakpoint given it's struct breakpoint * BINT.
14614 The value we return ends up being the return value from catch_errors.
14615 Unused in this case. */
14616
14617 static int
14618 breakpoint_re_set_one (void *bint)
14619 {
14620 /* Get past catch_errs. */
14621 struct breakpoint *b = (struct breakpoint *) bint;
14622 struct cleanup *cleanups;
14623
14624 cleanups = prepare_re_set_context (b);
14625 b->ops->re_set (b);
14626 do_cleanups (cleanups);
14627 return 0;
14628 }
14629
14630 /* Re-set breakpoint locations for the current program space.
14631 Locations bound to other program spaces are left untouched. */
14632
14633 void
14634 breakpoint_re_set (void)
14635 {
14636 struct breakpoint *b, *b_tmp;
14637 enum language save_language;
14638 int save_input_radix;
14639 struct cleanup *old_chain;
14640
14641 save_language = current_language->la_language;
14642 save_input_radix = input_radix;
14643 old_chain = save_current_space_and_thread ();
14644
14645 /* Note: we must not try to insert locations until after all
14646 breakpoints have been re-set. Otherwise, e.g., when re-setting
14647 breakpoint 1, we'd insert the locations of breakpoint 2, which
14648 hadn't been re-set yet, and thus may have stale locations. */
14649
14650 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14651 {
14652 /* Format possible error msg. */
14653 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
14654 b->number);
14655 struct cleanup *cleanups = make_cleanup (xfree, message);
14656 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
14657 do_cleanups (cleanups);
14658 }
14659 set_language (save_language);
14660 input_radix = save_input_radix;
14661
14662 jit_breakpoint_re_set ();
14663
14664 do_cleanups (old_chain);
14665
14666 create_overlay_event_breakpoint ();
14667 create_longjmp_master_breakpoint ();
14668 create_std_terminate_master_breakpoint ();
14669 create_exception_master_breakpoint ();
14670
14671 /* Now we can insert. */
14672 update_global_location_list (UGLL_MAY_INSERT);
14673 }
14674 \f
14675 /* Reset the thread number of this breakpoint:
14676
14677 - If the breakpoint is for all threads, leave it as-is.
14678 - Else, reset it to the current thread for inferior_ptid. */
14679 void
14680 breakpoint_re_set_thread (struct breakpoint *b)
14681 {
14682 if (b->thread != -1)
14683 {
14684 if (in_thread_list (inferior_ptid))
14685 b->thread = ptid_to_global_thread_id (inferior_ptid);
14686
14687 /* We're being called after following a fork. The new fork is
14688 selected as current, and unless this was a vfork will have a
14689 different program space from the original thread. Reset that
14690 as well. */
14691 b->loc->pspace = current_program_space;
14692 }
14693 }
14694
14695 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14696 If from_tty is nonzero, it prints a message to that effect,
14697 which ends with a period (no newline). */
14698
14699 void
14700 set_ignore_count (int bptnum, int count, int from_tty)
14701 {
14702 struct breakpoint *b;
14703
14704 if (count < 0)
14705 count = 0;
14706
14707 ALL_BREAKPOINTS (b)
14708 if (b->number == bptnum)
14709 {
14710 if (is_tracepoint (b))
14711 {
14712 if (from_tty && count != 0)
14713 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14714 bptnum);
14715 return;
14716 }
14717
14718 b->ignore_count = count;
14719 if (from_tty)
14720 {
14721 if (count == 0)
14722 printf_filtered (_("Will stop next time "
14723 "breakpoint %d is reached."),
14724 bptnum);
14725 else if (count == 1)
14726 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
14727 bptnum);
14728 else
14729 printf_filtered (_("Will ignore next %d "
14730 "crossings of breakpoint %d."),
14731 count, bptnum);
14732 }
14733 observer_notify_breakpoint_modified (b);
14734 return;
14735 }
14736
14737 error (_("No breakpoint number %d."), bptnum);
14738 }
14739
14740 /* Command to set ignore-count of breakpoint N to COUNT. */
14741
14742 static void
14743 ignore_command (char *args, int from_tty)
14744 {
14745 char *p = args;
14746 int num;
14747
14748 if (p == 0)
14749 error_no_arg (_("a breakpoint number"));
14750
14751 num = get_number (&p);
14752 if (num == 0)
14753 error (_("bad breakpoint number: '%s'"), args);
14754 if (*p == 0)
14755 error (_("Second argument (specified ignore-count) is missing."));
14756
14757 set_ignore_count (num,
14758 longest_to_int (value_as_long (parse_and_eval (p))),
14759 from_tty);
14760 if (from_tty)
14761 printf_filtered ("\n");
14762 }
14763 \f
14764 /* Call FUNCTION on each of the breakpoints
14765 whose numbers are given in ARGS. */
14766
14767 static void
14768 map_breakpoint_numbers (const char *args,
14769 void (*function) (struct breakpoint *,
14770 void *),
14771 void *data)
14772 {
14773 int num;
14774 struct breakpoint *b, *tmp;
14775
14776 if (args == 0 || *args == '\0')
14777 error_no_arg (_("one or more breakpoint numbers"));
14778
14779 number_or_range_parser parser (args);
14780
14781 while (!parser.finished ())
14782 {
14783 const char *p = parser.cur_tok ();
14784 bool match = false;
14785
14786 num = parser.get_number ();
14787 if (num == 0)
14788 {
14789 warning (_("bad breakpoint number at or near '%s'"), p);
14790 }
14791 else
14792 {
14793 ALL_BREAKPOINTS_SAFE (b, tmp)
14794 if (b->number == num)
14795 {
14796 match = true;
14797 function (b, data);
14798 break;
14799 }
14800 if (!match)
14801 printf_unfiltered (_("No breakpoint number %d.\n"), num);
14802 }
14803 }
14804 }
14805
14806 static struct bp_location *
14807 find_location_by_number (char *number)
14808 {
14809 char *dot = strchr (number, '.');
14810 char *p1;
14811 int bp_num;
14812 int loc_num;
14813 struct breakpoint *b;
14814 struct bp_location *loc;
14815
14816 *dot = '\0';
14817
14818 p1 = number;
14819 bp_num = get_number (&p1);
14820 if (bp_num == 0)
14821 error (_("Bad breakpoint number '%s'"), number);
14822
14823 ALL_BREAKPOINTS (b)
14824 if (b->number == bp_num)
14825 {
14826 break;
14827 }
14828
14829 if (!b || b->number != bp_num)
14830 error (_("Bad breakpoint number '%s'"), number);
14831
14832 p1 = dot+1;
14833 loc_num = get_number (&p1);
14834 if (loc_num == 0)
14835 error (_("Bad breakpoint location number '%s'"), number);
14836
14837 --loc_num;
14838 loc = b->loc;
14839 for (;loc_num && loc; --loc_num, loc = loc->next)
14840 ;
14841 if (!loc)
14842 error (_("Bad breakpoint location number '%s'"), dot+1);
14843
14844 return loc;
14845 }
14846
14847
14848 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14849 If from_tty is nonzero, it prints a message to that effect,
14850 which ends with a period (no newline). */
14851
14852 void
14853 disable_breakpoint (struct breakpoint *bpt)
14854 {
14855 /* Never disable a watchpoint scope breakpoint; we want to
14856 hit them when we leave scope so we can delete both the
14857 watchpoint and its scope breakpoint at that time. */
14858 if (bpt->type == bp_watchpoint_scope)
14859 return;
14860
14861 bpt->enable_state = bp_disabled;
14862
14863 /* Mark breakpoint locations modified. */
14864 mark_breakpoint_modified (bpt);
14865
14866 if (target_supports_enable_disable_tracepoint ()
14867 && current_trace_status ()->running && is_tracepoint (bpt))
14868 {
14869 struct bp_location *location;
14870
14871 for (location = bpt->loc; location; location = location->next)
14872 target_disable_tracepoint (location);
14873 }
14874
14875 update_global_location_list (UGLL_DONT_INSERT);
14876
14877 observer_notify_breakpoint_modified (bpt);
14878 }
14879
14880 /* A callback for iterate_over_related_breakpoints. */
14881
14882 static void
14883 do_disable_breakpoint (struct breakpoint *b, void *ignore)
14884 {
14885 disable_breakpoint (b);
14886 }
14887
14888 /* A callback for map_breakpoint_numbers that calls
14889 disable_breakpoint. */
14890
14891 static void
14892 do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
14893 {
14894 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
14895 }
14896
14897 static void
14898 disable_command (char *args, int from_tty)
14899 {
14900 if (args == 0)
14901 {
14902 struct breakpoint *bpt;
14903
14904 ALL_BREAKPOINTS (bpt)
14905 if (user_breakpoint_p (bpt))
14906 disable_breakpoint (bpt);
14907 }
14908 else
14909 {
14910 char *num = extract_arg (&args);
14911
14912 while (num)
14913 {
14914 if (strchr (num, '.'))
14915 {
14916 struct bp_location *loc = find_location_by_number (num);
14917
14918 if (loc)
14919 {
14920 if (loc->enabled)
14921 {
14922 loc->enabled = 0;
14923 mark_breakpoint_location_modified (loc);
14924 }
14925 if (target_supports_enable_disable_tracepoint ()
14926 && current_trace_status ()->running && loc->owner
14927 && is_tracepoint (loc->owner))
14928 target_disable_tracepoint (loc);
14929 }
14930 update_global_location_list (UGLL_DONT_INSERT);
14931 }
14932 else
14933 map_breakpoint_numbers (num, do_map_disable_breakpoint, NULL);
14934 num = extract_arg (&args);
14935 }
14936 }
14937 }
14938
14939 static void
14940 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14941 int count)
14942 {
14943 int target_resources_ok;
14944
14945 if (bpt->type == bp_hardware_breakpoint)
14946 {
14947 int i;
14948 i = hw_breakpoint_used_count ();
14949 target_resources_ok =
14950 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
14951 i + 1, 0);
14952 if (target_resources_ok == 0)
14953 error (_("No hardware breakpoint support in the target."));
14954 else if (target_resources_ok < 0)
14955 error (_("Hardware breakpoints used exceeds limit."));
14956 }
14957
14958 if (is_watchpoint (bpt))
14959 {
14960 /* Initialize it just to avoid a GCC false warning. */
14961 enum enable_state orig_enable_state = bp_disabled;
14962
14963 TRY
14964 {
14965 struct watchpoint *w = (struct watchpoint *) bpt;
14966
14967 orig_enable_state = bpt->enable_state;
14968 bpt->enable_state = bp_enabled;
14969 update_watchpoint (w, 1 /* reparse */);
14970 }
14971 CATCH (e, RETURN_MASK_ALL)
14972 {
14973 bpt->enable_state = orig_enable_state;
14974 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14975 bpt->number);
14976 return;
14977 }
14978 END_CATCH
14979 }
14980
14981 bpt->enable_state = bp_enabled;
14982
14983 /* Mark breakpoint locations modified. */
14984 mark_breakpoint_modified (bpt);
14985
14986 if (target_supports_enable_disable_tracepoint ()
14987 && current_trace_status ()->running && is_tracepoint (bpt))
14988 {
14989 struct bp_location *location;
14990
14991 for (location = bpt->loc; location; location = location->next)
14992 target_enable_tracepoint (location);
14993 }
14994
14995 bpt->disposition = disposition;
14996 bpt->enable_count = count;
14997 update_global_location_list (UGLL_MAY_INSERT);
14998
14999 observer_notify_breakpoint_modified (bpt);
15000 }
15001
15002
15003 void
15004 enable_breakpoint (struct breakpoint *bpt)
15005 {
15006 enable_breakpoint_disp (bpt, bpt->disposition, 0);
15007 }
15008
15009 static void
15010 do_enable_breakpoint (struct breakpoint *bpt, void *arg)
15011 {
15012 enable_breakpoint (bpt);
15013 }
15014
15015 /* A callback for map_breakpoint_numbers that calls
15016 enable_breakpoint. */
15017
15018 static void
15019 do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
15020 {
15021 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
15022 }
15023
15024 /* The enable command enables the specified breakpoints (or all defined
15025 breakpoints) so they once again become (or continue to be) effective
15026 in stopping the inferior. */
15027
15028 static void
15029 enable_command (char *args, int from_tty)
15030 {
15031 if (args == 0)
15032 {
15033 struct breakpoint *bpt;
15034
15035 ALL_BREAKPOINTS (bpt)
15036 if (user_breakpoint_p (bpt))
15037 enable_breakpoint (bpt);
15038 }
15039 else
15040 {
15041 char *num = extract_arg (&args);
15042
15043 while (num)
15044 {
15045 if (strchr (num, '.'))
15046 {
15047 struct bp_location *loc = find_location_by_number (num);
15048
15049 if (loc)
15050 {
15051 if (!loc->enabled)
15052 {
15053 loc->enabled = 1;
15054 mark_breakpoint_location_modified (loc);
15055 }
15056 if (target_supports_enable_disable_tracepoint ()
15057 && current_trace_status ()->running && loc->owner
15058 && is_tracepoint (loc->owner))
15059 target_enable_tracepoint (loc);
15060 }
15061 update_global_location_list (UGLL_MAY_INSERT);
15062 }
15063 else
15064 map_breakpoint_numbers (num, do_map_enable_breakpoint, NULL);
15065 num = extract_arg (&args);
15066 }
15067 }
15068 }
15069
15070 /* This struct packages up disposition data for application to multiple
15071 breakpoints. */
15072
15073 struct disp_data
15074 {
15075 enum bpdisp disp;
15076 int count;
15077 };
15078
15079 static void
15080 do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
15081 {
15082 struct disp_data disp_data = *(struct disp_data *) arg;
15083
15084 enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
15085 }
15086
15087 static void
15088 do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
15089 {
15090 struct disp_data disp = { disp_disable, 1 };
15091
15092 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
15093 }
15094
15095 static void
15096 enable_once_command (char *args, int from_tty)
15097 {
15098 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
15099 }
15100
15101 static void
15102 do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
15103 {
15104 struct disp_data disp = { disp_disable, *(int *) countptr };
15105
15106 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
15107 }
15108
15109 static void
15110 enable_count_command (char *args, int from_tty)
15111 {
15112 int count;
15113
15114 if (args == NULL)
15115 error_no_arg (_("hit count"));
15116
15117 count = get_number (&args);
15118
15119 map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
15120 }
15121
15122 static void
15123 do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
15124 {
15125 struct disp_data disp = { disp_del, 1 };
15126
15127 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
15128 }
15129
15130 static void
15131 enable_delete_command (char *args, int from_tty)
15132 {
15133 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
15134 }
15135 \f
15136 static void
15137 set_breakpoint_cmd (char *args, int from_tty)
15138 {
15139 }
15140
15141 static void
15142 show_breakpoint_cmd (char *args, int from_tty)
15143 {
15144 }
15145
15146 /* Invalidate last known value of any hardware watchpoint if
15147 the memory which that value represents has been written to by
15148 GDB itself. */
15149
15150 static void
15151 invalidate_bp_value_on_memory_change (struct inferior *inferior,
15152 CORE_ADDR addr, ssize_t len,
15153 const bfd_byte *data)
15154 {
15155 struct breakpoint *bp;
15156
15157 ALL_BREAKPOINTS (bp)
15158 if (bp->enable_state == bp_enabled
15159 && bp->type == bp_hardware_watchpoint)
15160 {
15161 struct watchpoint *wp = (struct watchpoint *) bp;
15162
15163 if (wp->val_valid && wp->val)
15164 {
15165 struct bp_location *loc;
15166
15167 for (loc = bp->loc; loc != NULL; loc = loc->next)
15168 if (loc->loc_type == bp_loc_hardware_watchpoint
15169 && loc->address + loc->length > addr
15170 && addr + len > loc->address)
15171 {
15172 value_free (wp->val);
15173 wp->val = NULL;
15174 wp->val_valid = 0;
15175 }
15176 }
15177 }
15178 }
15179
15180 /* Create and insert a breakpoint for software single step. */
15181
15182 void
15183 insert_single_step_breakpoint (struct gdbarch *gdbarch,
15184 struct address_space *aspace,
15185 CORE_ADDR next_pc)
15186 {
15187 struct thread_info *tp = inferior_thread ();
15188 struct symtab_and_line sal;
15189 CORE_ADDR pc = next_pc;
15190
15191 if (tp->control.single_step_breakpoints == NULL)
15192 {
15193 tp->control.single_step_breakpoints
15194 = new_single_step_breakpoint (tp->global_num, gdbarch);
15195 }
15196
15197 sal = find_pc_line (pc, 0);
15198 sal.pc = pc;
15199 sal.section = find_pc_overlay (pc);
15200 sal.explicit_pc = 1;
15201 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
15202
15203 update_global_location_list (UGLL_INSERT);
15204 }
15205
15206 /* Insert single step breakpoints according to the current state. */
15207
15208 int
15209 insert_single_step_breakpoints (struct gdbarch *gdbarch)
15210 {
15211 struct frame_info *frame = get_current_frame ();
15212 VEC (CORE_ADDR) * next_pcs;
15213
15214 next_pcs = gdbarch_software_single_step (gdbarch, frame);
15215
15216 if (next_pcs != NULL)
15217 {
15218 int i;
15219 CORE_ADDR pc;
15220 struct address_space *aspace = get_frame_address_space (frame);
15221
15222 for (i = 0; VEC_iterate (CORE_ADDR, next_pcs, i, pc); i++)
15223 insert_single_step_breakpoint (gdbarch, aspace, pc);
15224
15225 VEC_free (CORE_ADDR, next_pcs);
15226
15227 return 1;
15228 }
15229 else
15230 return 0;
15231 }
15232
15233 /* See breakpoint.h. */
15234
15235 int
15236 breakpoint_has_location_inserted_here (struct breakpoint *bp,
15237 struct address_space *aspace,
15238 CORE_ADDR pc)
15239 {
15240 struct bp_location *loc;
15241
15242 for (loc = bp->loc; loc != NULL; loc = loc->next)
15243 if (loc->inserted
15244 && breakpoint_location_address_match (loc, aspace, pc))
15245 return 1;
15246
15247 return 0;
15248 }
15249
15250 /* Check whether a software single-step breakpoint is inserted at
15251 PC. */
15252
15253 int
15254 single_step_breakpoint_inserted_here_p (struct address_space *aspace,
15255 CORE_ADDR pc)
15256 {
15257 struct breakpoint *bpt;
15258
15259 ALL_BREAKPOINTS (bpt)
15260 {
15261 if (bpt->type == bp_single_step
15262 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
15263 return 1;
15264 }
15265 return 0;
15266 }
15267
15268 /* Tracepoint-specific operations. */
15269
15270 /* Set tracepoint count to NUM. */
15271 static void
15272 set_tracepoint_count (int num)
15273 {
15274 tracepoint_count = num;
15275 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
15276 }
15277
15278 static void
15279 trace_command (char *arg, int from_tty)
15280 {
15281 struct breakpoint_ops *ops;
15282 struct event_location *location;
15283 struct cleanup *back_to;
15284
15285 location = string_to_event_location (&arg, current_language);
15286 back_to = make_cleanup_delete_event_location (location);
15287 if (location != NULL
15288 && event_location_type (location) == PROBE_LOCATION)
15289 ops = &tracepoint_probe_breakpoint_ops;
15290 else
15291 ops = &tracepoint_breakpoint_ops;
15292
15293 create_breakpoint (get_current_arch (),
15294 location,
15295 NULL, 0, arg, 1 /* parse arg */,
15296 0 /* tempflag */,
15297 bp_tracepoint /* type_wanted */,
15298 0 /* Ignore count */,
15299 pending_break_support,
15300 ops,
15301 from_tty,
15302 1 /* enabled */,
15303 0 /* internal */, 0);
15304 do_cleanups (back_to);
15305 }
15306
15307 static void
15308 ftrace_command (char *arg, int from_tty)
15309 {
15310 struct event_location *location;
15311 struct cleanup *back_to;
15312
15313 location = string_to_event_location (&arg, current_language);
15314 back_to = make_cleanup_delete_event_location (location);
15315 create_breakpoint (get_current_arch (),
15316 location,
15317 NULL, 0, arg, 1 /* parse arg */,
15318 0 /* tempflag */,
15319 bp_fast_tracepoint /* type_wanted */,
15320 0 /* Ignore count */,
15321 pending_break_support,
15322 &tracepoint_breakpoint_ops,
15323 from_tty,
15324 1 /* enabled */,
15325 0 /* internal */, 0);
15326 do_cleanups (back_to);
15327 }
15328
15329 /* strace command implementation. Creates a static tracepoint. */
15330
15331 static void
15332 strace_command (char *arg, int from_tty)
15333 {
15334 struct breakpoint_ops *ops;
15335 struct event_location *location;
15336 struct cleanup *back_to;
15337
15338 /* Decide if we are dealing with a static tracepoint marker (`-m'),
15339 or with a normal static tracepoint. */
15340 if (arg && startswith (arg, "-m") && isspace (arg[2]))
15341 {
15342 ops = &strace_marker_breakpoint_ops;
15343 location = new_linespec_location (&arg);
15344 }
15345 else
15346 {
15347 ops = &tracepoint_breakpoint_ops;
15348 location = string_to_event_location (&arg, current_language);
15349 }
15350
15351 back_to = make_cleanup_delete_event_location (location);
15352 create_breakpoint (get_current_arch (),
15353 location,
15354 NULL, 0, arg, 1 /* parse arg */,
15355 0 /* tempflag */,
15356 bp_static_tracepoint /* type_wanted */,
15357 0 /* Ignore count */,
15358 pending_break_support,
15359 ops,
15360 from_tty,
15361 1 /* enabled */,
15362 0 /* internal */, 0);
15363 do_cleanups (back_to);
15364 }
15365
15366 /* Set up a fake reader function that gets command lines from a linked
15367 list that was acquired during tracepoint uploading. */
15368
15369 static struct uploaded_tp *this_utp;
15370 static int next_cmd;
15371
15372 static char *
15373 read_uploaded_action (void)
15374 {
15375 char *rslt;
15376
15377 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
15378
15379 next_cmd++;
15380
15381 return rslt;
15382 }
15383
15384 /* Given information about a tracepoint as recorded on a target (which
15385 can be either a live system or a trace file), attempt to create an
15386 equivalent GDB tracepoint. This is not a reliable process, since
15387 the target does not necessarily have all the information used when
15388 the tracepoint was originally defined. */
15389
15390 struct tracepoint *
15391 create_tracepoint_from_upload (struct uploaded_tp *utp)
15392 {
15393 char *addr_str, small_buf[100];
15394 struct tracepoint *tp;
15395 struct event_location *location;
15396 struct cleanup *cleanup;
15397
15398 if (utp->at_string)
15399 addr_str = utp->at_string;
15400 else
15401 {
15402 /* In the absence of a source location, fall back to raw
15403 address. Since there is no way to confirm that the address
15404 means the same thing as when the trace was started, warn the
15405 user. */
15406 warning (_("Uploaded tracepoint %d has no "
15407 "source location, using raw address"),
15408 utp->number);
15409 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
15410 addr_str = small_buf;
15411 }
15412
15413 /* There's not much we can do with a sequence of bytecodes. */
15414 if (utp->cond && !utp->cond_string)
15415 warning (_("Uploaded tracepoint %d condition "
15416 "has no source form, ignoring it"),
15417 utp->number);
15418
15419 location = string_to_event_location (&addr_str, current_language);
15420 cleanup = make_cleanup_delete_event_location (location);
15421 if (!create_breakpoint (get_current_arch (),
15422 location,
15423 utp->cond_string, -1, addr_str,
15424 0 /* parse cond/thread */,
15425 0 /* tempflag */,
15426 utp->type /* type_wanted */,
15427 0 /* Ignore count */,
15428 pending_break_support,
15429 &tracepoint_breakpoint_ops,
15430 0 /* from_tty */,
15431 utp->enabled /* enabled */,
15432 0 /* internal */,
15433 CREATE_BREAKPOINT_FLAGS_INSERTED))
15434 {
15435 do_cleanups (cleanup);
15436 return NULL;
15437 }
15438
15439 do_cleanups (cleanup);
15440
15441 /* Get the tracepoint we just created. */
15442 tp = get_tracepoint (tracepoint_count);
15443 gdb_assert (tp != NULL);
15444
15445 if (utp->pass > 0)
15446 {
15447 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
15448 tp->base.number);
15449
15450 trace_pass_command (small_buf, 0);
15451 }
15452
15453 /* If we have uploaded versions of the original commands, set up a
15454 special-purpose "reader" function and call the usual command line
15455 reader, then pass the result to the breakpoint command-setting
15456 function. */
15457 if (!VEC_empty (char_ptr, utp->cmd_strings))
15458 {
15459 struct command_line *cmd_list;
15460
15461 this_utp = utp;
15462 next_cmd = 0;
15463
15464 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
15465
15466 breakpoint_set_commands (&tp->base, cmd_list);
15467 }
15468 else if (!VEC_empty (char_ptr, utp->actions)
15469 || !VEC_empty (char_ptr, utp->step_actions))
15470 warning (_("Uploaded tracepoint %d actions "
15471 "have no source form, ignoring them"),
15472 utp->number);
15473
15474 /* Copy any status information that might be available. */
15475 tp->base.hit_count = utp->hit_count;
15476 tp->traceframe_usage = utp->traceframe_usage;
15477
15478 return tp;
15479 }
15480
15481 /* Print information on tracepoint number TPNUM_EXP, or all if
15482 omitted. */
15483
15484 static void
15485 tracepoints_info (char *args, int from_tty)
15486 {
15487 struct ui_out *uiout = current_uiout;
15488 int num_printed;
15489
15490 num_printed = breakpoint_1 (args, 0, is_tracepoint);
15491
15492 if (num_printed == 0)
15493 {
15494 if (args == NULL || *args == '\0')
15495 ui_out_message (uiout, 0, "No tracepoints.\n");
15496 else
15497 ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
15498 }
15499
15500 default_collect_info ();
15501 }
15502
15503 /* The 'enable trace' command enables tracepoints.
15504 Not supported by all targets. */
15505 static void
15506 enable_trace_command (char *args, int from_tty)
15507 {
15508 enable_command (args, from_tty);
15509 }
15510
15511 /* The 'disable trace' command disables tracepoints.
15512 Not supported by all targets. */
15513 static void
15514 disable_trace_command (char *args, int from_tty)
15515 {
15516 disable_command (args, from_tty);
15517 }
15518
15519 /* Remove a tracepoint (or all if no argument). */
15520 static void
15521 delete_trace_command (char *arg, int from_tty)
15522 {
15523 struct breakpoint *b, *b_tmp;
15524
15525 dont_repeat ();
15526
15527 if (arg == 0)
15528 {
15529 int breaks_to_delete = 0;
15530
15531 /* Delete all breakpoints if no argument.
15532 Do not delete internal or call-dummy breakpoints, these
15533 have to be deleted with an explicit breakpoint number
15534 argument. */
15535 ALL_TRACEPOINTS (b)
15536 if (is_tracepoint (b) && user_breakpoint_p (b))
15537 {
15538 breaks_to_delete = 1;
15539 break;
15540 }
15541
15542 /* Ask user only if there are some breakpoints to delete. */
15543 if (!from_tty
15544 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
15545 {
15546 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15547 if (is_tracepoint (b) && user_breakpoint_p (b))
15548 delete_breakpoint (b);
15549 }
15550 }
15551 else
15552 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
15553 }
15554
15555 /* Helper function for trace_pass_command. */
15556
15557 static void
15558 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
15559 {
15560 tp->pass_count = count;
15561 observer_notify_breakpoint_modified (&tp->base);
15562 if (from_tty)
15563 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
15564 tp->base.number, count);
15565 }
15566
15567 /* Set passcount for tracepoint.
15568
15569 First command argument is passcount, second is tracepoint number.
15570 If tracepoint number omitted, apply to most recently defined.
15571 Also accepts special argument "all". */
15572
15573 static void
15574 trace_pass_command (char *args, int from_tty)
15575 {
15576 struct tracepoint *t1;
15577 unsigned int count;
15578
15579 if (args == 0 || *args == 0)
15580 error (_("passcount command requires an "
15581 "argument (count + optional TP num)"));
15582
15583 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
15584
15585 args = skip_spaces (args);
15586 if (*args && strncasecmp (args, "all", 3) == 0)
15587 {
15588 struct breakpoint *b;
15589
15590 args += 3; /* Skip special argument "all". */
15591 if (*args)
15592 error (_("Junk at end of arguments."));
15593
15594 ALL_TRACEPOINTS (b)
15595 {
15596 t1 = (struct tracepoint *) b;
15597 trace_pass_set_count (t1, count, from_tty);
15598 }
15599 }
15600 else if (*args == '\0')
15601 {
15602 t1 = get_tracepoint_by_number (&args, NULL);
15603 if (t1)
15604 trace_pass_set_count (t1, count, from_tty);
15605 }
15606 else
15607 {
15608 number_or_range_parser parser (args);
15609 while (!parser.finished ())
15610 {
15611 t1 = get_tracepoint_by_number (&args, &parser);
15612 if (t1)
15613 trace_pass_set_count (t1, count, from_tty);
15614 }
15615 }
15616 }
15617
15618 struct tracepoint *
15619 get_tracepoint (int num)
15620 {
15621 struct breakpoint *t;
15622
15623 ALL_TRACEPOINTS (t)
15624 if (t->number == num)
15625 return (struct tracepoint *) t;
15626
15627 return NULL;
15628 }
15629
15630 /* Find the tracepoint with the given target-side number (which may be
15631 different from the tracepoint number after disconnecting and
15632 reconnecting). */
15633
15634 struct tracepoint *
15635 get_tracepoint_by_number_on_target (int num)
15636 {
15637 struct breakpoint *b;
15638
15639 ALL_TRACEPOINTS (b)
15640 {
15641 struct tracepoint *t = (struct tracepoint *) b;
15642
15643 if (t->number_on_target == num)
15644 return t;
15645 }
15646
15647 return NULL;
15648 }
15649
15650 /* Utility: parse a tracepoint number and look it up in the list.
15651 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
15652 If the argument is missing, the most recent tracepoint
15653 (tracepoint_count) is returned. */
15654
15655 struct tracepoint *
15656 get_tracepoint_by_number (char **arg,
15657 number_or_range_parser *parser)
15658 {
15659 struct breakpoint *t;
15660 int tpnum;
15661 char *instring = arg == NULL ? NULL : *arg;
15662
15663 if (parser != NULL)
15664 {
15665 gdb_assert (!parser->finished ());
15666 tpnum = parser->get_number ();
15667 }
15668 else if (arg == NULL || *arg == NULL || ! **arg)
15669 tpnum = tracepoint_count;
15670 else
15671 tpnum = get_number (arg);
15672
15673 if (tpnum <= 0)
15674 {
15675 if (instring && *instring)
15676 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15677 instring);
15678 else
15679 printf_filtered (_("No previous tracepoint\n"));
15680 return NULL;
15681 }
15682
15683 ALL_TRACEPOINTS (t)
15684 if (t->number == tpnum)
15685 {
15686 return (struct tracepoint *) t;
15687 }
15688
15689 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15690 return NULL;
15691 }
15692
15693 void
15694 print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15695 {
15696 if (b->thread != -1)
15697 fprintf_unfiltered (fp, " thread %d", b->thread);
15698
15699 if (b->task != 0)
15700 fprintf_unfiltered (fp, " task %d", b->task);
15701
15702 fprintf_unfiltered (fp, "\n");
15703 }
15704
15705 /* Save information on user settable breakpoints (watchpoints, etc) to
15706 a new script file named FILENAME. If FILTER is non-NULL, call it
15707 on each breakpoint and only include the ones for which it returns
15708 non-zero. */
15709
15710 static void
15711 save_breakpoints (char *filename, int from_tty,
15712 int (*filter) (const struct breakpoint *))
15713 {
15714 struct breakpoint *tp;
15715 int any = 0;
15716 struct cleanup *cleanup;
15717 struct ui_file *fp;
15718 int extra_trace_bits = 0;
15719
15720 if (filename == 0 || *filename == 0)
15721 error (_("Argument required (file name in which to save)"));
15722
15723 /* See if we have anything to save. */
15724 ALL_BREAKPOINTS (tp)
15725 {
15726 /* Skip internal and momentary breakpoints. */
15727 if (!user_breakpoint_p (tp))
15728 continue;
15729
15730 /* If we have a filter, only save the breakpoints it accepts. */
15731 if (filter && !filter (tp))
15732 continue;
15733
15734 any = 1;
15735
15736 if (is_tracepoint (tp))
15737 {
15738 extra_trace_bits = 1;
15739
15740 /* We can stop searching. */
15741 break;
15742 }
15743 }
15744
15745 if (!any)
15746 {
15747 warning (_("Nothing to save."));
15748 return;
15749 }
15750
15751 filename = tilde_expand (filename);
15752 cleanup = make_cleanup (xfree, filename);
15753 fp = gdb_fopen (filename, "w");
15754 if (!fp)
15755 error (_("Unable to open file '%s' for saving (%s)"),
15756 filename, safe_strerror (errno));
15757 make_cleanup_ui_file_delete (fp);
15758
15759 if (extra_trace_bits)
15760 save_trace_state_variables (fp);
15761
15762 ALL_BREAKPOINTS (tp)
15763 {
15764 /* Skip internal and momentary breakpoints. */
15765 if (!user_breakpoint_p (tp))
15766 continue;
15767
15768 /* If we have a filter, only save the breakpoints it accepts. */
15769 if (filter && !filter (tp))
15770 continue;
15771
15772 tp->ops->print_recreate (tp, fp);
15773
15774 /* Note, we can't rely on tp->number for anything, as we can't
15775 assume the recreated breakpoint numbers will match. Use $bpnum
15776 instead. */
15777
15778 if (tp->cond_string)
15779 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
15780
15781 if (tp->ignore_count)
15782 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
15783
15784 if (tp->type != bp_dprintf && tp->commands)
15785 {
15786 fprintf_unfiltered (fp, " commands\n");
15787
15788 ui_out_redirect (current_uiout, fp);
15789 TRY
15790 {
15791 print_command_lines (current_uiout, tp->commands->commands, 2);
15792 }
15793 CATCH (ex, RETURN_MASK_ALL)
15794 {
15795 ui_out_redirect (current_uiout, NULL);
15796 throw_exception (ex);
15797 }
15798 END_CATCH
15799
15800 ui_out_redirect (current_uiout, NULL);
15801 fprintf_unfiltered (fp, " end\n");
15802 }
15803
15804 if (tp->enable_state == bp_disabled)
15805 fprintf_unfiltered (fp, "disable $bpnum\n");
15806
15807 /* If this is a multi-location breakpoint, check if the locations
15808 should be individually disabled. Watchpoint locations are
15809 special, and not user visible. */
15810 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15811 {
15812 struct bp_location *loc;
15813 int n = 1;
15814
15815 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15816 if (!loc->enabled)
15817 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
15818 }
15819 }
15820
15821 if (extra_trace_bits && *default_collect)
15822 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
15823
15824 if (from_tty)
15825 printf_filtered (_("Saved to file '%s'.\n"), filename);
15826 do_cleanups (cleanup);
15827 }
15828
15829 /* The `save breakpoints' command. */
15830
15831 static void
15832 save_breakpoints_command (char *args, int from_tty)
15833 {
15834 save_breakpoints (args, from_tty, NULL);
15835 }
15836
15837 /* The `save tracepoints' command. */
15838
15839 static void
15840 save_tracepoints_command (char *args, int from_tty)
15841 {
15842 save_breakpoints (args, from_tty, is_tracepoint);
15843 }
15844
15845 /* Create a vector of all tracepoints. */
15846
15847 VEC(breakpoint_p) *
15848 all_tracepoints (void)
15849 {
15850 VEC(breakpoint_p) *tp_vec = 0;
15851 struct breakpoint *tp;
15852
15853 ALL_TRACEPOINTS (tp)
15854 {
15855 VEC_safe_push (breakpoint_p, tp_vec, tp);
15856 }
15857
15858 return tp_vec;
15859 }
15860
15861 \f
15862 /* This help string is used to consolidate all the help string for specifying
15863 locations used by several commands. */
15864
15865 #define LOCATION_HELP_STRING \
15866 "Linespecs are colon-separated lists of location parameters, such as\n\
15867 source filename, function name, label name, and line number.\n\
15868 Example: To specify the start of a label named \"the_top\" in the\n\
15869 function \"fact\" in the file \"factorial.c\", use\n\
15870 \"factorial.c:fact:the_top\".\n\
15871 \n\
15872 Address locations begin with \"*\" and specify an exact address in the\n\
15873 program. Example: To specify the fourth byte past the start function\n\
15874 \"main\", use \"*main + 4\".\n\
15875 \n\
15876 Explicit locations are similar to linespecs but use an option/argument\n\
15877 syntax to specify location parameters.\n\
15878 Example: To specify the start of the label named \"the_top\" in the\n\
15879 function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
15880 -function fact -label the_top\".\n"
15881
15882 /* This help string is used for the break, hbreak, tbreak and thbreak
15883 commands. It is defined as a macro to prevent duplication.
15884 COMMAND should be a string constant containing the name of the
15885 command. */
15886
15887 #define BREAK_ARGS_HELP(command) \
15888 command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15889 PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15890 probe point. Accepted values are `-probe' (for a generic, automatically\n\
15891 guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15892 `-probe-dtrace' (for a DTrace probe).\n\
15893 LOCATION may be a linespec, address, or explicit location as described\n\
15894 below.\n\
15895 \n\
15896 With no LOCATION, uses current execution address of the selected\n\
15897 stack frame. This is useful for breaking on return to a stack frame.\n\
15898 \n\
15899 THREADNUM is the number from \"info threads\".\n\
15900 CONDITION is a boolean expression.\n\
15901 \n" LOCATION_HELP_STRING "\n\
15902 Multiple breakpoints at one place are permitted, and useful if their\n\
15903 conditions are different.\n\
15904 \n\
15905 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15906
15907 /* List of subcommands for "catch". */
15908 static struct cmd_list_element *catch_cmdlist;
15909
15910 /* List of subcommands for "tcatch". */
15911 static struct cmd_list_element *tcatch_cmdlist;
15912
15913 void
15914 add_catch_command (char *name, char *docstring,
15915 cmd_sfunc_ftype *sfunc,
15916 completer_ftype *completer,
15917 void *user_data_catch,
15918 void *user_data_tcatch)
15919 {
15920 struct cmd_list_element *command;
15921
15922 command = add_cmd (name, class_breakpoint, NULL, docstring,
15923 &catch_cmdlist);
15924 set_cmd_sfunc (command, sfunc);
15925 set_cmd_context (command, user_data_catch);
15926 set_cmd_completer (command, completer);
15927
15928 command = add_cmd (name, class_breakpoint, NULL, docstring,
15929 &tcatch_cmdlist);
15930 set_cmd_sfunc (command, sfunc);
15931 set_cmd_context (command, user_data_tcatch);
15932 set_cmd_completer (command, completer);
15933 }
15934
15935 static void
15936 save_command (char *arg, int from_tty)
15937 {
15938 printf_unfiltered (_("\"save\" must be followed by "
15939 "the name of a save subcommand.\n"));
15940 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
15941 }
15942
15943 struct breakpoint *
15944 iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15945 void *data)
15946 {
15947 struct breakpoint *b, *b_tmp;
15948
15949 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15950 {
15951 if ((*callback) (b, data))
15952 return b;
15953 }
15954
15955 return NULL;
15956 }
15957
15958 /* Zero if any of the breakpoint's locations could be a location where
15959 functions have been inlined, nonzero otherwise. */
15960
15961 static int
15962 is_non_inline_function (struct breakpoint *b)
15963 {
15964 /* The shared library event breakpoint is set on the address of a
15965 non-inline function. */
15966 if (b->type == bp_shlib_event)
15967 return 1;
15968
15969 return 0;
15970 }
15971
15972 /* Nonzero if the specified PC cannot be a location where functions
15973 have been inlined. */
15974
15975 int
15976 pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
15977 const struct target_waitstatus *ws)
15978 {
15979 struct breakpoint *b;
15980 struct bp_location *bl;
15981
15982 ALL_BREAKPOINTS (b)
15983 {
15984 if (!is_non_inline_function (b))
15985 continue;
15986
15987 for (bl = b->loc; bl != NULL; bl = bl->next)
15988 {
15989 if (!bl->shlib_disabled
15990 && bpstat_check_location (bl, aspace, pc, ws))
15991 return 1;
15992 }
15993 }
15994
15995 return 0;
15996 }
15997
15998 /* Remove any references to OBJFILE which is going to be freed. */
15999
16000 void
16001 breakpoint_free_objfile (struct objfile *objfile)
16002 {
16003 struct bp_location **locp, *loc;
16004
16005 ALL_BP_LOCATIONS (loc, locp)
16006 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
16007 loc->symtab = NULL;
16008 }
16009
16010 void
16011 initialize_breakpoint_ops (void)
16012 {
16013 static int initialized = 0;
16014
16015 struct breakpoint_ops *ops;
16016
16017 if (initialized)
16018 return;
16019 initialized = 1;
16020
16021 /* The breakpoint_ops structure to be inherit by all kinds of
16022 breakpoints (real breakpoints, i.e., user "break" breakpoints,
16023 internal and momentary breakpoints, etc.). */
16024 ops = &bkpt_base_breakpoint_ops;
16025 *ops = base_breakpoint_ops;
16026 ops->re_set = bkpt_re_set;
16027 ops->insert_location = bkpt_insert_location;
16028 ops->remove_location = bkpt_remove_location;
16029 ops->breakpoint_hit = bkpt_breakpoint_hit;
16030 ops->create_sals_from_location = bkpt_create_sals_from_location;
16031 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
16032 ops->decode_location = bkpt_decode_location;
16033
16034 /* The breakpoint_ops structure to be used in regular breakpoints. */
16035 ops = &bkpt_breakpoint_ops;
16036 *ops = bkpt_base_breakpoint_ops;
16037 ops->re_set = bkpt_re_set;
16038 ops->resources_needed = bkpt_resources_needed;
16039 ops->print_it = bkpt_print_it;
16040 ops->print_mention = bkpt_print_mention;
16041 ops->print_recreate = bkpt_print_recreate;
16042
16043 /* Ranged breakpoints. */
16044 ops = &ranged_breakpoint_ops;
16045 *ops = bkpt_breakpoint_ops;
16046 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
16047 ops->resources_needed = resources_needed_ranged_breakpoint;
16048 ops->print_it = print_it_ranged_breakpoint;
16049 ops->print_one = print_one_ranged_breakpoint;
16050 ops->print_one_detail = print_one_detail_ranged_breakpoint;
16051 ops->print_mention = print_mention_ranged_breakpoint;
16052 ops->print_recreate = print_recreate_ranged_breakpoint;
16053
16054 /* Internal breakpoints. */
16055 ops = &internal_breakpoint_ops;
16056 *ops = bkpt_base_breakpoint_ops;
16057 ops->re_set = internal_bkpt_re_set;
16058 ops->check_status = internal_bkpt_check_status;
16059 ops->print_it = internal_bkpt_print_it;
16060 ops->print_mention = internal_bkpt_print_mention;
16061
16062 /* Momentary breakpoints. */
16063 ops = &momentary_breakpoint_ops;
16064 *ops = bkpt_base_breakpoint_ops;
16065 ops->re_set = momentary_bkpt_re_set;
16066 ops->check_status = momentary_bkpt_check_status;
16067 ops->print_it = momentary_bkpt_print_it;
16068 ops->print_mention = momentary_bkpt_print_mention;
16069
16070 /* Momentary breakpoints for bp_longjmp and bp_exception. */
16071 ops = &longjmp_breakpoint_ops;
16072 *ops = momentary_breakpoint_ops;
16073 ops->dtor = longjmp_bkpt_dtor;
16074
16075 /* Probe breakpoints. */
16076 ops = &bkpt_probe_breakpoint_ops;
16077 *ops = bkpt_breakpoint_ops;
16078 ops->insert_location = bkpt_probe_insert_location;
16079 ops->remove_location = bkpt_probe_remove_location;
16080 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
16081 ops->decode_location = bkpt_probe_decode_location;
16082
16083 /* Watchpoints. */
16084 ops = &watchpoint_breakpoint_ops;
16085 *ops = base_breakpoint_ops;
16086 ops->dtor = dtor_watchpoint;
16087 ops->re_set = re_set_watchpoint;
16088 ops->insert_location = insert_watchpoint;
16089 ops->remove_location = remove_watchpoint;
16090 ops->breakpoint_hit = breakpoint_hit_watchpoint;
16091 ops->check_status = check_status_watchpoint;
16092 ops->resources_needed = resources_needed_watchpoint;
16093 ops->works_in_software_mode = works_in_software_mode_watchpoint;
16094 ops->print_it = print_it_watchpoint;
16095 ops->print_mention = print_mention_watchpoint;
16096 ops->print_recreate = print_recreate_watchpoint;
16097 ops->explains_signal = explains_signal_watchpoint;
16098
16099 /* Masked watchpoints. */
16100 ops = &masked_watchpoint_breakpoint_ops;
16101 *ops = watchpoint_breakpoint_ops;
16102 ops->insert_location = insert_masked_watchpoint;
16103 ops->remove_location = remove_masked_watchpoint;
16104 ops->resources_needed = resources_needed_masked_watchpoint;
16105 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
16106 ops->print_it = print_it_masked_watchpoint;
16107 ops->print_one_detail = print_one_detail_masked_watchpoint;
16108 ops->print_mention = print_mention_masked_watchpoint;
16109 ops->print_recreate = print_recreate_masked_watchpoint;
16110
16111 /* Tracepoints. */
16112 ops = &tracepoint_breakpoint_ops;
16113 *ops = base_breakpoint_ops;
16114 ops->re_set = tracepoint_re_set;
16115 ops->breakpoint_hit = tracepoint_breakpoint_hit;
16116 ops->print_one_detail = tracepoint_print_one_detail;
16117 ops->print_mention = tracepoint_print_mention;
16118 ops->print_recreate = tracepoint_print_recreate;
16119 ops->create_sals_from_location = tracepoint_create_sals_from_location;
16120 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
16121 ops->decode_location = tracepoint_decode_location;
16122
16123 /* Probe tracepoints. */
16124 ops = &tracepoint_probe_breakpoint_ops;
16125 *ops = tracepoint_breakpoint_ops;
16126 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
16127 ops->decode_location = tracepoint_probe_decode_location;
16128
16129 /* Static tracepoints with marker (`-m'). */
16130 ops = &strace_marker_breakpoint_ops;
16131 *ops = tracepoint_breakpoint_ops;
16132 ops->create_sals_from_location = strace_marker_create_sals_from_location;
16133 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
16134 ops->decode_location = strace_marker_decode_location;
16135
16136 /* Fork catchpoints. */
16137 ops = &catch_fork_breakpoint_ops;
16138 *ops = base_breakpoint_ops;
16139 ops->insert_location = insert_catch_fork;
16140 ops->remove_location = remove_catch_fork;
16141 ops->breakpoint_hit = breakpoint_hit_catch_fork;
16142 ops->print_it = print_it_catch_fork;
16143 ops->print_one = print_one_catch_fork;
16144 ops->print_mention = print_mention_catch_fork;
16145 ops->print_recreate = print_recreate_catch_fork;
16146
16147 /* Vfork catchpoints. */
16148 ops = &catch_vfork_breakpoint_ops;
16149 *ops = base_breakpoint_ops;
16150 ops->insert_location = insert_catch_vfork;
16151 ops->remove_location = remove_catch_vfork;
16152 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
16153 ops->print_it = print_it_catch_vfork;
16154 ops->print_one = print_one_catch_vfork;
16155 ops->print_mention = print_mention_catch_vfork;
16156 ops->print_recreate = print_recreate_catch_vfork;
16157
16158 /* Exec catchpoints. */
16159 ops = &catch_exec_breakpoint_ops;
16160 *ops = base_breakpoint_ops;
16161 ops->dtor = dtor_catch_exec;
16162 ops->insert_location = insert_catch_exec;
16163 ops->remove_location = remove_catch_exec;
16164 ops->breakpoint_hit = breakpoint_hit_catch_exec;
16165 ops->print_it = print_it_catch_exec;
16166 ops->print_one = print_one_catch_exec;
16167 ops->print_mention = print_mention_catch_exec;
16168 ops->print_recreate = print_recreate_catch_exec;
16169
16170 /* Solib-related catchpoints. */
16171 ops = &catch_solib_breakpoint_ops;
16172 *ops = base_breakpoint_ops;
16173 ops->dtor = dtor_catch_solib;
16174 ops->insert_location = insert_catch_solib;
16175 ops->remove_location = remove_catch_solib;
16176 ops->breakpoint_hit = breakpoint_hit_catch_solib;
16177 ops->check_status = check_status_catch_solib;
16178 ops->print_it = print_it_catch_solib;
16179 ops->print_one = print_one_catch_solib;
16180 ops->print_mention = print_mention_catch_solib;
16181 ops->print_recreate = print_recreate_catch_solib;
16182
16183 ops = &dprintf_breakpoint_ops;
16184 *ops = bkpt_base_breakpoint_ops;
16185 ops->re_set = dprintf_re_set;
16186 ops->resources_needed = bkpt_resources_needed;
16187 ops->print_it = bkpt_print_it;
16188 ops->print_mention = bkpt_print_mention;
16189 ops->print_recreate = dprintf_print_recreate;
16190 ops->after_condition_true = dprintf_after_condition_true;
16191 ops->breakpoint_hit = dprintf_breakpoint_hit;
16192 }
16193
16194 /* Chain containing all defined "enable breakpoint" subcommands. */
16195
16196 static struct cmd_list_element *enablebreaklist = NULL;
16197
16198 void
16199 _initialize_breakpoint (void)
16200 {
16201 struct cmd_list_element *c;
16202
16203 initialize_breakpoint_ops ();
16204
16205 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
16206 observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
16207 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
16208
16209 breakpoint_objfile_key
16210 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
16211
16212 breakpoint_chain = 0;
16213 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
16214 before a breakpoint is set. */
16215 breakpoint_count = 0;
16216
16217 tracepoint_count = 0;
16218
16219 add_com ("ignore", class_breakpoint, ignore_command, _("\
16220 Set ignore-count of breakpoint number N to COUNT.\n\
16221 Usage is `ignore N COUNT'."));
16222
16223 add_com ("commands", class_breakpoint, commands_command, _("\
16224 Set commands to be executed when a breakpoint is hit.\n\
16225 Give breakpoint number as argument after \"commands\".\n\
16226 With no argument, the targeted breakpoint is the last one set.\n\
16227 The commands themselves follow starting on the next line.\n\
16228 Type a line containing \"end\" to indicate the end of them.\n\
16229 Give \"silent\" as the first line to make the breakpoint silent;\n\
16230 then no output is printed when it is hit, except what the commands print."));
16231
16232 c = add_com ("condition", class_breakpoint, condition_command, _("\
16233 Specify breakpoint number N to break only if COND is true.\n\
16234 Usage is `condition N COND', where N is an integer and COND is an\n\
16235 expression to be evaluated whenever breakpoint N is reached."));
16236 set_cmd_completer (c, condition_completer);
16237
16238 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
16239 Set a temporary breakpoint.\n\
16240 Like \"break\" except the breakpoint is only temporary,\n\
16241 so it will be deleted when hit. Equivalent to \"break\" followed\n\
16242 by using \"enable delete\" on the breakpoint number.\n\
16243 \n"
16244 BREAK_ARGS_HELP ("tbreak")));
16245 set_cmd_completer (c, location_completer);
16246
16247 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
16248 Set a hardware assisted breakpoint.\n\
16249 Like \"break\" except the breakpoint requires hardware support,\n\
16250 some target hardware may not have this support.\n\
16251 \n"
16252 BREAK_ARGS_HELP ("hbreak")));
16253 set_cmd_completer (c, location_completer);
16254
16255 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
16256 Set a temporary hardware assisted breakpoint.\n\
16257 Like \"hbreak\" except the breakpoint is only temporary,\n\
16258 so it will be deleted when hit.\n\
16259 \n"
16260 BREAK_ARGS_HELP ("thbreak")));
16261 set_cmd_completer (c, location_completer);
16262
16263 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
16264 Enable some breakpoints.\n\
16265 Give breakpoint numbers (separated by spaces) as arguments.\n\
16266 With no subcommand, breakpoints are enabled until you command otherwise.\n\
16267 This is used to cancel the effect of the \"disable\" command.\n\
16268 With a subcommand you can enable temporarily."),
16269 &enablelist, "enable ", 1, &cmdlist);
16270
16271 add_com_alias ("en", "enable", class_breakpoint, 1);
16272
16273 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
16274 Enable some breakpoints.\n\
16275 Give breakpoint numbers (separated by spaces) as arguments.\n\
16276 This is used to cancel the effect of the \"disable\" command.\n\
16277 May be abbreviated to simply \"enable\".\n"),
16278 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
16279
16280 add_cmd ("once", no_class, enable_once_command, _("\
16281 Enable breakpoints for one hit. Give breakpoint numbers.\n\
16282 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
16283 &enablebreaklist);
16284
16285 add_cmd ("delete", no_class, enable_delete_command, _("\
16286 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16287 If a breakpoint is hit while enabled in this fashion, it is deleted."),
16288 &enablebreaklist);
16289
16290 add_cmd ("count", no_class, enable_count_command, _("\
16291 Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16292 If a breakpoint is hit while enabled in this fashion,\n\
16293 the count is decremented; when it reaches zero, the breakpoint is disabled."),
16294 &enablebreaklist);
16295
16296 add_cmd ("delete", no_class, enable_delete_command, _("\
16297 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16298 If a breakpoint is hit while enabled in this fashion, it is deleted."),
16299 &enablelist);
16300
16301 add_cmd ("once", no_class, enable_once_command, _("\
16302 Enable breakpoints for one hit. Give breakpoint numbers.\n\
16303 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
16304 &enablelist);
16305
16306 add_cmd ("count", no_class, enable_count_command, _("\
16307 Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16308 If a breakpoint is hit while enabled in this fashion,\n\
16309 the count is decremented; when it reaches zero, the breakpoint is disabled."),
16310 &enablelist);
16311
16312 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
16313 Disable some breakpoints.\n\
16314 Arguments are breakpoint numbers with spaces in between.\n\
16315 To disable all breakpoints, give no argument.\n\
16316 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
16317 &disablelist, "disable ", 1, &cmdlist);
16318 add_com_alias ("dis", "disable", class_breakpoint, 1);
16319 add_com_alias ("disa", "disable", class_breakpoint, 1);
16320
16321 add_cmd ("breakpoints", class_alias, disable_command, _("\
16322 Disable some breakpoints.\n\
16323 Arguments are breakpoint numbers with spaces in between.\n\
16324 To disable all breakpoints, give no argument.\n\
16325 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
16326 This command may be abbreviated \"disable\"."),
16327 &disablelist);
16328
16329 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
16330 Delete some breakpoints or auto-display expressions.\n\
16331 Arguments are breakpoint numbers with spaces in between.\n\
16332 To delete all breakpoints, give no argument.\n\
16333 \n\
16334 Also a prefix command for deletion of other GDB objects.\n\
16335 The \"unset\" command is also an alias for \"delete\"."),
16336 &deletelist, "delete ", 1, &cmdlist);
16337 add_com_alias ("d", "delete", class_breakpoint, 1);
16338 add_com_alias ("del", "delete", class_breakpoint, 1);
16339
16340 add_cmd ("breakpoints", class_alias, delete_command, _("\
16341 Delete some breakpoints or auto-display expressions.\n\
16342 Arguments are breakpoint numbers with spaces in between.\n\
16343 To delete all breakpoints, give no argument.\n\
16344 This command may be abbreviated \"delete\"."),
16345 &deletelist);
16346
16347 add_com ("clear", class_breakpoint, clear_command, _("\
16348 Clear breakpoint at specified location.\n\
16349 Argument may be a linespec, explicit, or address location as described below.\n\
16350 \n\
16351 With no argument, clears all breakpoints in the line that the selected frame\n\
16352 is executing in.\n"
16353 "\n" LOCATION_HELP_STRING "\n\
16354 See also the \"delete\" command which clears breakpoints by number."));
16355 add_com_alias ("cl", "clear", class_breakpoint, 1);
16356
16357 c = add_com ("break", class_breakpoint, break_command, _("\
16358 Set breakpoint at specified location.\n"
16359 BREAK_ARGS_HELP ("break")));
16360 set_cmd_completer (c, location_completer);
16361
16362 add_com_alias ("b", "break", class_run, 1);
16363 add_com_alias ("br", "break", class_run, 1);
16364 add_com_alias ("bre", "break", class_run, 1);
16365 add_com_alias ("brea", "break", class_run, 1);
16366
16367 if (dbx_commands)
16368 {
16369 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
16370 Break in function/address or break at a line in the current file."),
16371 &stoplist, "stop ", 1, &cmdlist);
16372 add_cmd ("in", class_breakpoint, stopin_command,
16373 _("Break in function or address."), &stoplist);
16374 add_cmd ("at", class_breakpoint, stopat_command,
16375 _("Break at a line in the current file."), &stoplist);
16376 add_com ("status", class_info, breakpoints_info, _("\
16377 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
16378 The \"Type\" column indicates one of:\n\
16379 \tbreakpoint - normal breakpoint\n\
16380 \twatchpoint - watchpoint\n\
16381 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16382 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16383 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16384 address and file/line number respectively.\n\
16385 \n\
16386 Convenience variable \"$_\" and default examine address for \"x\"\n\
16387 are set to the address of the last breakpoint listed unless the command\n\
16388 is prefixed with \"server \".\n\n\
16389 Convenience variable \"$bpnum\" contains the number of the last\n\
16390 breakpoint set."));
16391 }
16392
16393 add_info ("breakpoints", breakpoints_info, _("\
16394 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
16395 The \"Type\" column indicates one of:\n\
16396 \tbreakpoint - normal breakpoint\n\
16397 \twatchpoint - watchpoint\n\
16398 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16399 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16400 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16401 address and file/line number respectively.\n\
16402 \n\
16403 Convenience variable \"$_\" and default examine address for \"x\"\n\
16404 are set to the address of the last breakpoint listed unless the command\n\
16405 is prefixed with \"server \".\n\n\
16406 Convenience variable \"$bpnum\" contains the number of the last\n\
16407 breakpoint set."));
16408
16409 add_info_alias ("b", "breakpoints", 1);
16410
16411 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
16412 Status of all breakpoints, or breakpoint number NUMBER.\n\
16413 The \"Type\" column indicates one of:\n\
16414 \tbreakpoint - normal breakpoint\n\
16415 \twatchpoint - watchpoint\n\
16416 \tlongjmp - internal breakpoint used to step through longjmp()\n\
16417 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
16418 \tuntil - internal breakpoint used by the \"until\" command\n\
16419 \tfinish - internal breakpoint used by the \"finish\" command\n\
16420 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16421 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16422 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16423 address and file/line number respectively.\n\
16424 \n\
16425 Convenience variable \"$_\" and default examine address for \"x\"\n\
16426 are set to the address of the last breakpoint listed unless the command\n\
16427 is prefixed with \"server \".\n\n\
16428 Convenience variable \"$bpnum\" contains the number of the last\n\
16429 breakpoint set."),
16430 &maintenanceinfolist);
16431
16432 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
16433 Set catchpoints to catch events."),
16434 &catch_cmdlist, "catch ",
16435 0/*allow-unknown*/, &cmdlist);
16436
16437 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
16438 Set temporary catchpoints to catch events."),
16439 &tcatch_cmdlist, "tcatch ",
16440 0/*allow-unknown*/, &cmdlist);
16441
16442 add_catch_command ("fork", _("Catch calls to fork."),
16443 catch_fork_command_1,
16444 NULL,
16445 (void *) (uintptr_t) catch_fork_permanent,
16446 (void *) (uintptr_t) catch_fork_temporary);
16447 add_catch_command ("vfork", _("Catch calls to vfork."),
16448 catch_fork_command_1,
16449 NULL,
16450 (void *) (uintptr_t) catch_vfork_permanent,
16451 (void *) (uintptr_t) catch_vfork_temporary);
16452 add_catch_command ("exec", _("Catch calls to exec."),
16453 catch_exec_command_1,
16454 NULL,
16455 CATCH_PERMANENT,
16456 CATCH_TEMPORARY);
16457 add_catch_command ("load", _("Catch loads of shared libraries.\n\
16458 Usage: catch load [REGEX]\n\
16459 If REGEX is given, only stop for libraries matching the regular expression."),
16460 catch_load_command_1,
16461 NULL,
16462 CATCH_PERMANENT,
16463 CATCH_TEMPORARY);
16464 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
16465 Usage: catch unload [REGEX]\n\
16466 If REGEX is given, only stop for libraries matching the regular expression."),
16467 catch_unload_command_1,
16468 NULL,
16469 CATCH_PERMANENT,
16470 CATCH_TEMPORARY);
16471
16472 c = add_com ("watch", class_breakpoint, watch_command, _("\
16473 Set a watchpoint for an expression.\n\
16474 Usage: watch [-l|-location] EXPRESSION\n\
16475 A watchpoint stops execution of your program whenever the value of\n\
16476 an expression changes.\n\
16477 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16478 the memory to which it refers."));
16479 set_cmd_completer (c, expression_completer);
16480
16481 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
16482 Set a read watchpoint for an expression.\n\
16483 Usage: rwatch [-l|-location] EXPRESSION\n\
16484 A watchpoint stops execution of your program whenever the value of\n\
16485 an expression is read.\n\
16486 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16487 the memory to which it refers."));
16488 set_cmd_completer (c, expression_completer);
16489
16490 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
16491 Set a watchpoint for an expression.\n\
16492 Usage: awatch [-l|-location] EXPRESSION\n\
16493 A watchpoint stops execution of your program whenever the value of\n\
16494 an expression is either read or written.\n\
16495 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16496 the memory to which it refers."));
16497 set_cmd_completer (c, expression_completer);
16498
16499 add_info ("watchpoints", watchpoints_info, _("\
16500 Status of specified watchpoints (all watchpoints if no argument)."));
16501
16502 /* XXX: cagney/2005-02-23: This should be a boolean, and should
16503 respond to changes - contrary to the description. */
16504 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
16505 &can_use_hw_watchpoints, _("\
16506 Set debugger's willingness to use watchpoint hardware."), _("\
16507 Show debugger's willingness to use watchpoint hardware."), _("\
16508 If zero, gdb will not use hardware for new watchpoints, even if\n\
16509 such is available. (However, any hardware watchpoints that were\n\
16510 created before setting this to nonzero, will continue to use watchpoint\n\
16511 hardware.)"),
16512 NULL,
16513 show_can_use_hw_watchpoints,
16514 &setlist, &showlist);
16515
16516 can_use_hw_watchpoints = 1;
16517
16518 /* Tracepoint manipulation commands. */
16519
16520 c = add_com ("trace", class_breakpoint, trace_command, _("\
16521 Set a tracepoint at specified location.\n\
16522 \n"
16523 BREAK_ARGS_HELP ("trace") "\n\
16524 Do \"help tracepoints\" for info on other tracepoint commands."));
16525 set_cmd_completer (c, location_completer);
16526
16527 add_com_alias ("tp", "trace", class_alias, 0);
16528 add_com_alias ("tr", "trace", class_alias, 1);
16529 add_com_alias ("tra", "trace", class_alias, 1);
16530 add_com_alias ("trac", "trace", class_alias, 1);
16531
16532 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
16533 Set a fast tracepoint at specified location.\n\
16534 \n"
16535 BREAK_ARGS_HELP ("ftrace") "\n\
16536 Do \"help tracepoints\" for info on other tracepoint commands."));
16537 set_cmd_completer (c, location_completer);
16538
16539 c = add_com ("strace", class_breakpoint, strace_command, _("\
16540 Set a static tracepoint at location or marker.\n\
16541 \n\
16542 strace [LOCATION] [if CONDITION]\n\
16543 LOCATION may be a linespec, explicit, or address location (described below) \n\
16544 or -m MARKER_ID.\n\n\
16545 If a marker id is specified, probe the marker with that name. With\n\
16546 no LOCATION, uses current execution address of the selected stack frame.\n\
16547 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16548 This collects arbitrary user data passed in the probe point call to the\n\
16549 tracing library. You can inspect it when analyzing the trace buffer,\n\
16550 by printing the $_sdata variable like any other convenience variable.\n\
16551 \n\
16552 CONDITION is a boolean expression.\n\
16553 \n" LOCATION_HELP_STRING "\n\
16554 Multiple tracepoints at one place are permitted, and useful if their\n\
16555 conditions are different.\n\
16556 \n\
16557 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16558 Do \"help tracepoints\" for info on other tracepoint commands."));
16559 set_cmd_completer (c, location_completer);
16560
16561 add_info ("tracepoints", tracepoints_info, _("\
16562 Status of specified tracepoints (all tracepoints if no argument).\n\
16563 Convenience variable \"$tpnum\" contains the number of the\n\
16564 last tracepoint set."));
16565
16566 add_info_alias ("tp", "tracepoints", 1);
16567
16568 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16569 Delete specified tracepoints.\n\
16570 Arguments are tracepoint numbers, separated by spaces.\n\
16571 No argument means delete all tracepoints."),
16572 &deletelist);
16573 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
16574
16575 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16576 Disable specified tracepoints.\n\
16577 Arguments are tracepoint numbers, separated by spaces.\n\
16578 No argument means disable all tracepoints."),
16579 &disablelist);
16580 deprecate_cmd (c, "disable");
16581
16582 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16583 Enable specified tracepoints.\n\
16584 Arguments are tracepoint numbers, separated by spaces.\n\
16585 No argument means enable all tracepoints."),
16586 &enablelist);
16587 deprecate_cmd (c, "enable");
16588
16589 add_com ("passcount", class_trace, trace_pass_command, _("\
16590 Set the passcount for a tracepoint.\n\
16591 The trace will end when the tracepoint has been passed 'count' times.\n\
16592 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16593 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16594
16595 add_prefix_cmd ("save", class_breakpoint, save_command,
16596 _("Save breakpoint definitions as a script."),
16597 &save_cmdlist, "save ",
16598 0/*allow-unknown*/, &cmdlist);
16599
16600 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16601 Save current breakpoint definitions as a script.\n\
16602 This includes all types of breakpoints (breakpoints, watchpoints,\n\
16603 catchpoints, tracepoints). Use the 'source' command in another debug\n\
16604 session to restore them."),
16605 &save_cmdlist);
16606 set_cmd_completer (c, filename_completer);
16607
16608 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
16609 Save current tracepoint definitions as a script.\n\
16610 Use the 'source' command in another debug session to restore them."),
16611 &save_cmdlist);
16612 set_cmd_completer (c, filename_completer);
16613
16614 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16615 deprecate_cmd (c, "save tracepoints");
16616
16617 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
16618 Breakpoint specific settings\n\
16619 Configure various breakpoint-specific variables such as\n\
16620 pending breakpoint behavior"),
16621 &breakpoint_set_cmdlist, "set breakpoint ",
16622 0/*allow-unknown*/, &setlist);
16623 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
16624 Breakpoint specific settings\n\
16625 Configure various breakpoint-specific variables such as\n\
16626 pending breakpoint behavior"),
16627 &breakpoint_show_cmdlist, "show breakpoint ",
16628 0/*allow-unknown*/, &showlist);
16629
16630 add_setshow_auto_boolean_cmd ("pending", no_class,
16631 &pending_break_support, _("\
16632 Set debugger's behavior regarding pending breakpoints."), _("\
16633 Show debugger's behavior regarding pending breakpoints."), _("\
16634 If on, an unrecognized breakpoint location will cause gdb to create a\n\
16635 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16636 an error. If auto, an unrecognized breakpoint location results in a\n\
16637 user-query to see if a pending breakpoint should be created."),
16638 NULL,
16639 show_pending_break_support,
16640 &breakpoint_set_cmdlist,
16641 &breakpoint_show_cmdlist);
16642
16643 pending_break_support = AUTO_BOOLEAN_AUTO;
16644
16645 add_setshow_boolean_cmd ("auto-hw", no_class,
16646 &automatic_hardware_breakpoints, _("\
16647 Set automatic usage of hardware breakpoints."), _("\
16648 Show automatic usage of hardware breakpoints."), _("\
16649 If set, the debugger will automatically use hardware breakpoints for\n\
16650 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16651 a warning will be emitted for such breakpoints."),
16652 NULL,
16653 show_automatic_hardware_breakpoints,
16654 &breakpoint_set_cmdlist,
16655 &breakpoint_show_cmdlist);
16656
16657 add_setshow_boolean_cmd ("always-inserted", class_support,
16658 &always_inserted_mode, _("\
16659 Set mode for inserting breakpoints."), _("\
16660 Show mode for inserting breakpoints."), _("\
16661 When this mode is on, breakpoints are inserted immediately as soon as\n\
16662 they're created, kept inserted even when execution stops, and removed\n\
16663 only when the user deletes them. When this mode is off (the default),\n\
16664 breakpoints are inserted only when execution continues, and removed\n\
16665 when execution stops."),
16666 NULL,
16667 &show_always_inserted_mode,
16668 &breakpoint_set_cmdlist,
16669 &breakpoint_show_cmdlist);
16670
16671 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16672 condition_evaluation_enums,
16673 &condition_evaluation_mode_1, _("\
16674 Set mode of breakpoint condition evaluation."), _("\
16675 Show mode of breakpoint condition evaluation."), _("\
16676 When this is set to \"host\", breakpoint conditions will be\n\
16677 evaluated on the host's side by GDB. When it is set to \"target\",\n\
16678 breakpoint conditions will be downloaded to the target (if the target\n\
16679 supports such feature) and conditions will be evaluated on the target's side.\n\
16680 If this is set to \"auto\" (default), this will be automatically set to\n\
16681 \"target\" if it supports condition evaluation, otherwise it will\n\
16682 be set to \"gdb\""),
16683 &set_condition_evaluation_mode,
16684 &show_condition_evaluation_mode,
16685 &breakpoint_set_cmdlist,
16686 &breakpoint_show_cmdlist);
16687
16688 add_com ("break-range", class_breakpoint, break_range_command, _("\
16689 Set a breakpoint for an address range.\n\
16690 break-range START-LOCATION, END-LOCATION\n\
16691 where START-LOCATION and END-LOCATION can be one of the following:\n\
16692 LINENUM, for that line in the current file,\n\
16693 FILE:LINENUM, for that line in that file,\n\
16694 +OFFSET, for that number of lines after the current line\n\
16695 or the start of the range\n\
16696 FUNCTION, for the first line in that function,\n\
16697 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16698 *ADDRESS, for the instruction at that address.\n\
16699 \n\
16700 The breakpoint will stop execution of the inferior whenever it executes\n\
16701 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16702 range (including START-LOCATION and END-LOCATION)."));
16703
16704 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
16705 Set a dynamic printf at specified location.\n\
16706 dprintf location,format string,arg1,arg2,...\n\
16707 location may be a linespec, explicit, or address location.\n"
16708 "\n" LOCATION_HELP_STRING));
16709 set_cmd_completer (c, location_completer);
16710
16711 add_setshow_enum_cmd ("dprintf-style", class_support,
16712 dprintf_style_enums, &dprintf_style, _("\
16713 Set the style of usage for dynamic printf."), _("\
16714 Show the style of usage for dynamic printf."), _("\
16715 This setting chooses how GDB will do a dynamic printf.\n\
16716 If the value is \"gdb\", then the printing is done by GDB to its own\n\
16717 console, as with the \"printf\" command.\n\
16718 If the value is \"call\", the print is done by calling a function in your\n\
16719 program; by default printf(), but you can choose a different function or\n\
16720 output stream by setting dprintf-function and dprintf-channel."),
16721 update_dprintf_commands, NULL,
16722 &setlist, &showlist);
16723
16724 dprintf_function = xstrdup ("printf");
16725 add_setshow_string_cmd ("dprintf-function", class_support,
16726 &dprintf_function, _("\
16727 Set the function to use for dynamic printf"), _("\
16728 Show the function to use for dynamic printf"), NULL,
16729 update_dprintf_commands, NULL,
16730 &setlist, &showlist);
16731
16732 dprintf_channel = xstrdup ("");
16733 add_setshow_string_cmd ("dprintf-channel", class_support,
16734 &dprintf_channel, _("\
16735 Set the channel to use for dynamic printf"), _("\
16736 Show the channel to use for dynamic printf"), NULL,
16737 update_dprintf_commands, NULL,
16738 &setlist, &showlist);
16739
16740 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16741 &disconnected_dprintf, _("\
16742 Set whether dprintf continues after GDB disconnects."), _("\
16743 Show whether dprintf continues after GDB disconnects."), _("\
16744 Use this to let dprintf commands continue to hit and produce output\n\
16745 even if GDB disconnects or detaches from the target."),
16746 NULL,
16747 NULL,
16748 &setlist, &showlist);
16749
16750 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16751 agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16752 (target agent only) This is useful for formatted output in user-defined commands."));
16753
16754 automatic_hardware_breakpoints = 1;
16755
16756 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
16757 observer_attach_thread_exit (remove_threaded_breakpoints);
16758 }
This page took 0.414215 seconds and 3 git commands to generate.