]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/breakpoint.c
gdb: Remove support for obsolete OSABIs and a.out
[thirdparty/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 agent_expr_up
2260 parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2261 {
2262 if (cond == NULL)
2263 return NULL;
2264
2265 agent_expr_up aexpr;
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 }
2280 END_CATCH
2281
2282 /* We have a valid agent expression. */
2283 return aexpr;
2284 }
2285
2286 /* Based on location BL, create a list of breakpoint conditions to be
2287 passed on to the target. If we have duplicated locations with different
2288 conditions, we will add such conditions to the list. The idea is that the
2289 target will evaluate the list of conditions and will only notify GDB when
2290 one of them is true. */
2291
2292 static void
2293 build_target_condition_list (struct bp_location *bl)
2294 {
2295 struct bp_location **locp = NULL, **loc2p;
2296 int null_condition_or_parse_error = 0;
2297 int modified = bl->needs_update;
2298 struct bp_location *loc;
2299
2300 /* Release conditions left over from a previous insert. */
2301 bl->target_info.conditions.clear ();
2302
2303 /* This is only meaningful if the target is
2304 evaluating conditions and if the user has
2305 opted for condition evaluation on the target's
2306 side. */
2307 if (gdb_evaluates_breakpoint_condition_p ()
2308 || !target_supports_evaluation_of_breakpoint_conditions ())
2309 return;
2310
2311 /* Do a first pass to check for locations with no assigned
2312 conditions or conditions that fail to parse to a valid agent expression
2313 bytecode. If any of these happen, then it's no use to send conditions
2314 to the target since this location will always trigger and generate a
2315 response back to GDB. */
2316 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2317 {
2318 loc = (*loc2p);
2319 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2320 {
2321 if (modified)
2322 {
2323 /* Re-parse the conditions since something changed. In that
2324 case we already freed the condition bytecodes (see
2325 force_breakpoint_reinsertion). We just
2326 need to parse the condition to bytecodes again. */
2327 loc->cond_bytecode = parse_cond_to_aexpr (bl->address,
2328 loc->cond.get ());
2329 }
2330
2331 /* If we have a NULL bytecode expression, it means something
2332 went wrong or we have a null condition expression. */
2333 if (!loc->cond_bytecode)
2334 {
2335 null_condition_or_parse_error = 1;
2336 break;
2337 }
2338 }
2339 }
2340
2341 /* If any of these happened, it means we will have to evaluate the conditions
2342 for the location's address on gdb's side. It is no use keeping bytecodes
2343 for all the other duplicate locations, thus we free all of them here.
2344
2345 This is so we have a finer control over which locations' conditions are
2346 being evaluated by GDB or the remote stub. */
2347 if (null_condition_or_parse_error)
2348 {
2349 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2350 {
2351 loc = (*loc2p);
2352 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2353 {
2354 /* Only go as far as the first NULL bytecode is
2355 located. */
2356 if (!loc->cond_bytecode)
2357 return;
2358
2359 loc->cond_bytecode.reset ();
2360 }
2361 }
2362 }
2363
2364 /* No NULL conditions or failed bytecode generation. Build a condition list
2365 for this location's address. */
2366 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2367 {
2368 loc = (*loc2p);
2369 if (loc->cond
2370 && is_breakpoint (loc->owner)
2371 && loc->pspace->num == bl->pspace->num
2372 && loc->owner->enable_state == bp_enabled
2373 && loc->enabled)
2374 {
2375 /* Add the condition to the vector. This will be used later
2376 to send the conditions to the target. */
2377 bl->target_info.conditions.push_back (loc->cond_bytecode.get ());
2378 }
2379 }
2380
2381 return;
2382 }
2383
2384 /* Parses a command described by string CMD into an agent expression
2385 bytecode suitable for evaluation by the bytecode interpreter.
2386 Return NULL if there was any error during parsing. */
2387
2388 static agent_expr_up
2389 parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2390 {
2391 struct cleanup *old_cleanups = 0;
2392 struct expression **argvec;
2393 const char *cmdrest;
2394 const char *format_start, *format_end;
2395 struct format_piece *fpieces;
2396 int nargs;
2397 struct gdbarch *gdbarch = get_current_arch ();
2398
2399 if (cmd == NULL)
2400 return NULL;
2401
2402 cmdrest = cmd;
2403
2404 if (*cmdrest == ',')
2405 ++cmdrest;
2406 cmdrest = skip_spaces_const (cmdrest);
2407
2408 if (*cmdrest++ != '"')
2409 error (_("No format string following the location"));
2410
2411 format_start = cmdrest;
2412
2413 fpieces = parse_format_string (&cmdrest);
2414
2415 old_cleanups = make_cleanup (free_format_pieces_cleanup, &fpieces);
2416
2417 format_end = cmdrest;
2418
2419 if (*cmdrest++ != '"')
2420 error (_("Bad format string, non-terminated '\"'."));
2421
2422 cmdrest = skip_spaces_const (cmdrest);
2423
2424 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2425 error (_("Invalid argument syntax"));
2426
2427 if (*cmdrest == ',')
2428 cmdrest++;
2429 cmdrest = skip_spaces_const (cmdrest);
2430
2431 /* For each argument, make an expression. */
2432
2433 argvec = (struct expression **) alloca (strlen (cmd)
2434 * sizeof (struct expression *));
2435
2436 nargs = 0;
2437 while (*cmdrest != '\0')
2438 {
2439 const char *cmd1;
2440
2441 cmd1 = cmdrest;
2442 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2443 argvec[nargs++] = expr.release ();
2444 cmdrest = cmd1;
2445 if (*cmdrest == ',')
2446 ++cmdrest;
2447 }
2448
2449 agent_expr_up aexpr;
2450
2451 /* We don't want to stop processing, so catch any errors
2452 that may show up. */
2453 TRY
2454 {
2455 aexpr = gen_printf (scope, gdbarch, 0, 0,
2456 format_start, format_end - format_start,
2457 fpieces, nargs, argvec);
2458 }
2459 CATCH (ex, RETURN_MASK_ERROR)
2460 {
2461 /* If we got here, it means the command could not be parsed to a valid
2462 bytecode expression and thus can't be evaluated on the target's side.
2463 It's no use iterating through the other commands. */
2464 }
2465 END_CATCH
2466
2467 do_cleanups (old_cleanups);
2468
2469 /* We have a valid agent expression, return it. */
2470 return aexpr;
2471 }
2472
2473 /* Based on location BL, create a list of breakpoint commands to be
2474 passed on to the target. If we have duplicated locations with
2475 different commands, we will add any such to the list. */
2476
2477 static void
2478 build_target_command_list (struct bp_location *bl)
2479 {
2480 struct bp_location **locp = NULL, **loc2p;
2481 int null_command_or_parse_error = 0;
2482 int modified = bl->needs_update;
2483 struct bp_location *loc;
2484
2485 /* Clear commands left over from a previous insert. */
2486 bl->target_info.tcommands.clear ();
2487
2488 if (!target_can_run_breakpoint_commands ())
2489 return;
2490
2491 /* For now, limit to agent-style dprintf breakpoints. */
2492 if (dprintf_style != dprintf_style_agent)
2493 return;
2494
2495 /* For now, if we have any duplicate location that isn't a dprintf,
2496 don't install the target-side commands, as that would make the
2497 breakpoint not be reported to the core, and we'd lose
2498 control. */
2499 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2500 {
2501 loc = (*loc2p);
2502 if (is_breakpoint (loc->owner)
2503 && loc->pspace->num == bl->pspace->num
2504 && loc->owner->type != bp_dprintf)
2505 return;
2506 }
2507
2508 /* Do a first pass to check for locations with no assigned
2509 conditions or conditions that fail to parse to a valid agent expression
2510 bytecode. If any of these happen, then it's no use to send conditions
2511 to the target since this location will always trigger and generate a
2512 response back to GDB. */
2513 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2514 {
2515 loc = (*loc2p);
2516 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2517 {
2518 if (modified)
2519 {
2520 /* Re-parse the commands since something changed. In that
2521 case we already freed the command bytecodes (see
2522 force_breakpoint_reinsertion). We just
2523 need to parse the command to bytecodes again. */
2524 loc->cmd_bytecode
2525 = parse_cmd_to_aexpr (bl->address,
2526 loc->owner->extra_string);
2527 }
2528
2529 /* If we have a NULL bytecode expression, it means something
2530 went wrong or we have a null command expression. */
2531 if (!loc->cmd_bytecode)
2532 {
2533 null_command_or_parse_error = 1;
2534 break;
2535 }
2536 }
2537 }
2538
2539 /* If anything failed, then we're not doing target-side commands,
2540 and so clean up. */
2541 if (null_command_or_parse_error)
2542 {
2543 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2544 {
2545 loc = (*loc2p);
2546 if (is_breakpoint (loc->owner)
2547 && loc->pspace->num == bl->pspace->num)
2548 {
2549 /* Only go as far as the first NULL bytecode is
2550 located. */
2551 if (loc->cmd_bytecode == NULL)
2552 return;
2553
2554 loc->cmd_bytecode.reset ();
2555 }
2556 }
2557 }
2558
2559 /* No NULL commands or failed bytecode generation. Build a command list
2560 for this location's address. */
2561 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2562 {
2563 loc = (*loc2p);
2564 if (loc->owner->extra_string
2565 && is_breakpoint (loc->owner)
2566 && loc->pspace->num == bl->pspace->num
2567 && loc->owner->enable_state == bp_enabled
2568 && loc->enabled)
2569 {
2570 /* Add the command to the vector. This will be used later
2571 to send the commands to the target. */
2572 bl->target_info.tcommands.push_back (loc->cmd_bytecode.get ());
2573 }
2574 }
2575
2576 bl->target_info.persist = 0;
2577 /* Maybe flag this location as persistent. */
2578 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2579 bl->target_info.persist = 1;
2580 }
2581
2582 /* Return the kind of breakpoint on address *ADDR. Get the kind
2583 of breakpoint according to ADDR except single-step breakpoint.
2584 Get the kind of single-step breakpoint according to the current
2585 registers state. */
2586
2587 static int
2588 breakpoint_kind (struct bp_location *bl, CORE_ADDR *addr)
2589 {
2590 if (bl->owner->type == bp_single_step)
2591 {
2592 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2593 struct regcache *regcache;
2594
2595 regcache = get_thread_regcache (thr->ptid);
2596
2597 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2598 regcache, addr);
2599 }
2600 else
2601 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
2602 }
2603
2604 /* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2605 location. Any error messages are printed to TMP_ERROR_STREAM; and
2606 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
2607 Returns 0 for success, 1 if the bp_location type is not supported or
2608 -1 for failure.
2609
2610 NOTE drow/2003-09-09: This routine could be broken down to an
2611 object-style method for each breakpoint or catchpoint type. */
2612 static int
2613 insert_bp_location (struct bp_location *bl,
2614 struct ui_file *tmp_error_stream,
2615 int *disabled_breaks,
2616 int *hw_breakpoint_error,
2617 int *hw_bp_error_explained_already)
2618 {
2619 enum errors bp_err = GDB_NO_ERROR;
2620 const char *bp_err_message = NULL;
2621
2622 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2623 return 0;
2624
2625 /* Note we don't initialize bl->target_info, as that wipes out
2626 the breakpoint location's shadow_contents if the breakpoint
2627 is still inserted at that location. This in turn breaks
2628 target_read_memory which depends on these buffers when
2629 a memory read is requested at the breakpoint location:
2630 Once the target_info has been wiped, we fail to see that
2631 we have a breakpoint inserted at that address and thus
2632 read the breakpoint instead of returning the data saved in
2633 the breakpoint location's shadow contents. */
2634 bl->target_info.reqstd_address = bl->address;
2635 bl->target_info.placed_address_space = bl->pspace->aspace;
2636 bl->target_info.length = bl->length;
2637
2638 /* When working with target-side conditions, we must pass all the conditions
2639 for the same breakpoint address down to the target since GDB will not
2640 insert those locations. With a list of breakpoint conditions, the target
2641 can decide when to stop and notify GDB. */
2642
2643 if (is_breakpoint (bl->owner))
2644 {
2645 build_target_condition_list (bl);
2646 build_target_command_list (bl);
2647 /* Reset the modification marker. */
2648 bl->needs_update = 0;
2649 }
2650
2651 if (bl->loc_type == bp_loc_software_breakpoint
2652 || bl->loc_type == bp_loc_hardware_breakpoint)
2653 {
2654 if (bl->owner->type != bp_hardware_breakpoint)
2655 {
2656 /* If the explicitly specified breakpoint type
2657 is not hardware breakpoint, check the memory map to see
2658 if the breakpoint address is in read only memory or not.
2659
2660 Two important cases are:
2661 - location type is not hardware breakpoint, memory
2662 is readonly. We change the type of the location to
2663 hardware breakpoint.
2664 - location type is hardware breakpoint, memory is
2665 read-write. This means we've previously made the
2666 location hardware one, but then the memory map changed,
2667 so we undo.
2668
2669 When breakpoints are removed, remove_breakpoints will use
2670 location types we've just set here, the only possible
2671 problem is that memory map has changed during running
2672 program, but it's not going to work anyway with current
2673 gdb. */
2674 struct mem_region *mr
2675 = lookup_mem_region (bl->target_info.reqstd_address);
2676
2677 if (mr)
2678 {
2679 if (automatic_hardware_breakpoints)
2680 {
2681 enum bp_loc_type new_type;
2682
2683 if (mr->attrib.mode != MEM_RW)
2684 new_type = bp_loc_hardware_breakpoint;
2685 else
2686 new_type = bp_loc_software_breakpoint;
2687
2688 if (new_type != bl->loc_type)
2689 {
2690 static int said = 0;
2691
2692 bl->loc_type = new_type;
2693 if (!said)
2694 {
2695 fprintf_filtered (gdb_stdout,
2696 _("Note: automatically using "
2697 "hardware breakpoints for "
2698 "read-only addresses.\n"));
2699 said = 1;
2700 }
2701 }
2702 }
2703 else if (bl->loc_type == bp_loc_software_breakpoint
2704 && mr->attrib.mode != MEM_RW)
2705 {
2706 fprintf_unfiltered (tmp_error_stream,
2707 _("Cannot insert breakpoint %d.\n"
2708 "Cannot set software breakpoint "
2709 "at read-only address %s\n"),
2710 bl->owner->number,
2711 paddress (bl->gdbarch, bl->address));
2712 return 1;
2713 }
2714 }
2715 }
2716
2717 /* First check to see if we have to handle an overlay. */
2718 if (overlay_debugging == ovly_off
2719 || bl->section == NULL
2720 || !(section_is_overlay (bl->section)))
2721 {
2722 /* No overlay handling: just set the breakpoint. */
2723 TRY
2724 {
2725 int val;
2726
2727 val = bl->owner->ops->insert_location (bl);
2728 if (val)
2729 bp_err = GENERIC_ERROR;
2730 }
2731 CATCH (e, RETURN_MASK_ALL)
2732 {
2733 bp_err = e.error;
2734 bp_err_message = e.message;
2735 }
2736 END_CATCH
2737 }
2738 else
2739 {
2740 /* This breakpoint is in an overlay section.
2741 Shall we set a breakpoint at the LMA? */
2742 if (!overlay_events_enabled)
2743 {
2744 /* Yes -- overlay event support is not active,
2745 so we must try to set a breakpoint at the LMA.
2746 This will not work for a hardware breakpoint. */
2747 if (bl->loc_type == bp_loc_hardware_breakpoint)
2748 warning (_("hardware breakpoint %d not supported in overlay!"),
2749 bl->owner->number);
2750 else
2751 {
2752 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2753 bl->section);
2754 /* Set a software (trap) breakpoint at the LMA. */
2755 bl->overlay_target_info = bl->target_info;
2756 bl->overlay_target_info.reqstd_address = addr;
2757
2758 /* No overlay handling: just set the breakpoint. */
2759 TRY
2760 {
2761 int val;
2762
2763 bl->overlay_target_info.kind
2764 = breakpoint_kind (bl, &addr);
2765 bl->overlay_target_info.placed_address = addr;
2766 val = target_insert_breakpoint (bl->gdbarch,
2767 &bl->overlay_target_info);
2768 if (val)
2769 bp_err = GENERIC_ERROR;
2770 }
2771 CATCH (e, RETURN_MASK_ALL)
2772 {
2773 bp_err = e.error;
2774 bp_err_message = e.message;
2775 }
2776 END_CATCH
2777
2778 if (bp_err != GDB_NO_ERROR)
2779 fprintf_unfiltered (tmp_error_stream,
2780 "Overlay breakpoint %d "
2781 "failed: in ROM?\n",
2782 bl->owner->number);
2783 }
2784 }
2785 /* Shall we set a breakpoint at the VMA? */
2786 if (section_is_mapped (bl->section))
2787 {
2788 /* Yes. This overlay section is mapped into memory. */
2789 TRY
2790 {
2791 int val;
2792
2793 val = bl->owner->ops->insert_location (bl);
2794 if (val)
2795 bp_err = GENERIC_ERROR;
2796 }
2797 CATCH (e, RETURN_MASK_ALL)
2798 {
2799 bp_err = e.error;
2800 bp_err_message = e.message;
2801 }
2802 END_CATCH
2803 }
2804 else
2805 {
2806 /* No. This breakpoint will not be inserted.
2807 No error, but do not mark the bp as 'inserted'. */
2808 return 0;
2809 }
2810 }
2811
2812 if (bp_err != GDB_NO_ERROR)
2813 {
2814 /* Can't set the breakpoint. */
2815
2816 /* In some cases, we might not be able to insert a
2817 breakpoint in a shared library that has already been
2818 removed, but we have not yet processed the shlib unload
2819 event. Unfortunately, some targets that implement
2820 breakpoint insertion themselves can't tell why the
2821 breakpoint insertion failed (e.g., the remote target
2822 doesn't define error codes), so we must treat generic
2823 errors as memory errors. */
2824 if ((bp_err == GENERIC_ERROR || bp_err == MEMORY_ERROR)
2825 && bl->loc_type == bp_loc_software_breakpoint
2826 && (solib_name_from_address (bl->pspace, bl->address)
2827 || shared_objfile_contains_address_p (bl->pspace,
2828 bl->address)))
2829 {
2830 /* See also: disable_breakpoints_in_shlibs. */
2831 bl->shlib_disabled = 1;
2832 observer_notify_breakpoint_modified (bl->owner);
2833 if (!*disabled_breaks)
2834 {
2835 fprintf_unfiltered (tmp_error_stream,
2836 "Cannot insert breakpoint %d.\n",
2837 bl->owner->number);
2838 fprintf_unfiltered (tmp_error_stream,
2839 "Temporarily disabling shared "
2840 "library breakpoints:\n");
2841 }
2842 *disabled_breaks = 1;
2843 fprintf_unfiltered (tmp_error_stream,
2844 "breakpoint #%d\n", bl->owner->number);
2845 return 0;
2846 }
2847 else
2848 {
2849 if (bl->loc_type == bp_loc_hardware_breakpoint)
2850 {
2851 *hw_breakpoint_error = 1;
2852 *hw_bp_error_explained_already = bp_err_message != NULL;
2853 fprintf_unfiltered (tmp_error_stream,
2854 "Cannot insert hardware breakpoint %d%s",
2855 bl->owner->number, bp_err_message ? ":" : ".\n");
2856 if (bp_err_message != NULL)
2857 fprintf_unfiltered (tmp_error_stream, "%s.\n", bp_err_message);
2858 }
2859 else
2860 {
2861 if (bp_err_message == NULL)
2862 {
2863 char *message
2864 = memory_error_message (TARGET_XFER_E_IO,
2865 bl->gdbarch, bl->address);
2866 struct cleanup *old_chain = make_cleanup (xfree, message);
2867
2868 fprintf_unfiltered (tmp_error_stream,
2869 "Cannot insert breakpoint %d.\n"
2870 "%s\n",
2871 bl->owner->number, message);
2872 do_cleanups (old_chain);
2873 }
2874 else
2875 {
2876 fprintf_unfiltered (tmp_error_stream,
2877 "Cannot insert breakpoint %d: %s\n",
2878 bl->owner->number,
2879 bp_err_message);
2880 }
2881 }
2882 return 1;
2883
2884 }
2885 }
2886 else
2887 bl->inserted = 1;
2888
2889 return 0;
2890 }
2891
2892 else if (bl->loc_type == bp_loc_hardware_watchpoint
2893 /* NOTE drow/2003-09-08: This state only exists for removing
2894 watchpoints. It's not clear that it's necessary... */
2895 && bl->owner->disposition != disp_del_at_next_stop)
2896 {
2897 int val;
2898
2899 gdb_assert (bl->owner->ops != NULL
2900 && bl->owner->ops->insert_location != NULL);
2901
2902 val = bl->owner->ops->insert_location (bl);
2903
2904 /* If trying to set a read-watchpoint, and it turns out it's not
2905 supported, try emulating one with an access watchpoint. */
2906 if (val == 1 && bl->watchpoint_type == hw_read)
2907 {
2908 struct bp_location *loc, **loc_temp;
2909
2910 /* But don't try to insert it, if there's already another
2911 hw_access location that would be considered a duplicate
2912 of this one. */
2913 ALL_BP_LOCATIONS (loc, loc_temp)
2914 if (loc != bl
2915 && loc->watchpoint_type == hw_access
2916 && watchpoint_locations_match (bl, loc))
2917 {
2918 bl->duplicate = 1;
2919 bl->inserted = 1;
2920 bl->target_info = loc->target_info;
2921 bl->watchpoint_type = hw_access;
2922 val = 0;
2923 break;
2924 }
2925
2926 if (val == 1)
2927 {
2928 bl->watchpoint_type = hw_access;
2929 val = bl->owner->ops->insert_location (bl);
2930
2931 if (val)
2932 /* Back to the original value. */
2933 bl->watchpoint_type = hw_read;
2934 }
2935 }
2936
2937 bl->inserted = (val == 0);
2938 }
2939
2940 else if (bl->owner->type == bp_catchpoint)
2941 {
2942 int val;
2943
2944 gdb_assert (bl->owner->ops != NULL
2945 && bl->owner->ops->insert_location != NULL);
2946
2947 val = bl->owner->ops->insert_location (bl);
2948 if (val)
2949 {
2950 bl->owner->enable_state = bp_disabled;
2951
2952 if (val == 1)
2953 warning (_("\
2954 Error inserting catchpoint %d: Your system does not support this type\n\
2955 of catchpoint."), bl->owner->number);
2956 else
2957 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2958 }
2959
2960 bl->inserted = (val == 0);
2961
2962 /* We've already printed an error message if there was a problem
2963 inserting this catchpoint, and we've disabled the catchpoint,
2964 so just return success. */
2965 return 0;
2966 }
2967
2968 return 0;
2969 }
2970
2971 /* This function is called when program space PSPACE is about to be
2972 deleted. It takes care of updating breakpoints to not reference
2973 PSPACE anymore. */
2974
2975 void
2976 breakpoint_program_space_exit (struct program_space *pspace)
2977 {
2978 struct breakpoint *b, *b_temp;
2979 struct bp_location *loc, **loc_temp;
2980
2981 /* Remove any breakpoint that was set through this program space. */
2982 ALL_BREAKPOINTS_SAFE (b, b_temp)
2983 {
2984 if (b->pspace == pspace)
2985 delete_breakpoint (b);
2986 }
2987
2988 /* Breakpoints set through other program spaces could have locations
2989 bound to PSPACE as well. Remove those. */
2990 ALL_BP_LOCATIONS (loc, loc_temp)
2991 {
2992 struct bp_location *tmp;
2993
2994 if (loc->pspace == pspace)
2995 {
2996 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
2997 if (loc->owner->loc == loc)
2998 loc->owner->loc = loc->next;
2999 else
3000 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
3001 if (tmp->next == loc)
3002 {
3003 tmp->next = loc->next;
3004 break;
3005 }
3006 }
3007 }
3008
3009 /* Now update the global location list to permanently delete the
3010 removed locations above. */
3011 update_global_location_list (UGLL_DONT_INSERT);
3012 }
3013
3014 /* Make sure all breakpoints are inserted in inferior.
3015 Throws exception on any error.
3016 A breakpoint that is already inserted won't be inserted
3017 again, so calling this function twice is safe. */
3018 void
3019 insert_breakpoints (void)
3020 {
3021 struct breakpoint *bpt;
3022
3023 ALL_BREAKPOINTS (bpt)
3024 if (is_hardware_watchpoint (bpt))
3025 {
3026 struct watchpoint *w = (struct watchpoint *) bpt;
3027
3028 update_watchpoint (w, 0 /* don't reparse. */);
3029 }
3030
3031 /* Updating watchpoints creates new locations, so update the global
3032 location list. Explicitly tell ugll to insert locations and
3033 ignore breakpoints_always_inserted_mode. */
3034 update_global_location_list (UGLL_INSERT);
3035 }
3036
3037 /* Invoke CALLBACK for each of bp_location. */
3038
3039 void
3040 iterate_over_bp_locations (walk_bp_location_callback callback)
3041 {
3042 struct bp_location *loc, **loc_tmp;
3043
3044 ALL_BP_LOCATIONS (loc, loc_tmp)
3045 {
3046 callback (loc, NULL);
3047 }
3048 }
3049
3050 /* This is used when we need to synch breakpoint conditions between GDB and the
3051 target. It is the case with deleting and disabling of breakpoints when using
3052 always-inserted mode. */
3053
3054 static void
3055 update_inserted_breakpoint_locations (void)
3056 {
3057 struct bp_location *bl, **blp_tmp;
3058 int error_flag = 0;
3059 int val = 0;
3060 int disabled_breaks = 0;
3061 int hw_breakpoint_error = 0;
3062 int hw_bp_details_reported = 0;
3063
3064 struct ui_file *tmp_error_stream = mem_fileopen ();
3065 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
3066
3067 /* Explicitly mark the warning -- this will only be printed if
3068 there was an error. */
3069 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
3070
3071 save_current_space_and_thread ();
3072
3073 ALL_BP_LOCATIONS (bl, blp_tmp)
3074 {
3075 /* We only want to update software breakpoints and hardware
3076 breakpoints. */
3077 if (!is_breakpoint (bl->owner))
3078 continue;
3079
3080 /* We only want to update locations that are already inserted
3081 and need updating. This is to avoid unwanted insertion during
3082 deletion of breakpoints. */
3083 if (!bl->inserted || (bl->inserted && !bl->needs_update))
3084 continue;
3085
3086 switch_to_program_space_and_thread (bl->pspace);
3087
3088 /* For targets that support global breakpoints, there's no need
3089 to select an inferior to insert breakpoint to. In fact, even
3090 if we aren't attached to any process yet, we should still
3091 insert breakpoints. */
3092 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3093 && ptid_equal (inferior_ptid, null_ptid))
3094 continue;
3095
3096 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
3097 &hw_breakpoint_error, &hw_bp_details_reported);
3098 if (val)
3099 error_flag = val;
3100 }
3101
3102 if (error_flag)
3103 {
3104 target_terminal_ours_for_output ();
3105 error_stream (tmp_error_stream);
3106 }
3107
3108 do_cleanups (cleanups);
3109 }
3110
3111 /* Used when starting or continuing the program. */
3112
3113 static void
3114 insert_breakpoint_locations (void)
3115 {
3116 struct breakpoint *bpt;
3117 struct bp_location *bl, **blp_tmp;
3118 int error_flag = 0;
3119 int val = 0;
3120 int disabled_breaks = 0;
3121 int hw_breakpoint_error = 0;
3122 int hw_bp_error_explained_already = 0;
3123
3124 struct ui_file *tmp_error_stream = mem_fileopen ();
3125 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
3126
3127 /* Explicitly mark the warning -- this will only be printed if
3128 there was an error. */
3129 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
3130
3131 save_current_space_and_thread ();
3132
3133 ALL_BP_LOCATIONS (bl, blp_tmp)
3134 {
3135 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
3136 continue;
3137
3138 /* There is no point inserting thread-specific breakpoints if
3139 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3140 has BL->OWNER always non-NULL. */
3141 if (bl->owner->thread != -1
3142 && !valid_global_thread_id (bl->owner->thread))
3143 continue;
3144
3145 switch_to_program_space_and_thread (bl->pspace);
3146
3147 /* For targets that support global breakpoints, there's no need
3148 to select an inferior to insert breakpoint to. In fact, even
3149 if we aren't attached to any process yet, we should still
3150 insert breakpoints. */
3151 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3152 && ptid_equal (inferior_ptid, null_ptid))
3153 continue;
3154
3155 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
3156 &hw_breakpoint_error, &hw_bp_error_explained_already);
3157 if (val)
3158 error_flag = val;
3159 }
3160
3161 /* If we failed to insert all locations of a watchpoint, remove
3162 them, as half-inserted watchpoint is of limited use. */
3163 ALL_BREAKPOINTS (bpt)
3164 {
3165 int some_failed = 0;
3166 struct bp_location *loc;
3167
3168 if (!is_hardware_watchpoint (bpt))
3169 continue;
3170
3171 if (!breakpoint_enabled (bpt))
3172 continue;
3173
3174 if (bpt->disposition == disp_del_at_next_stop)
3175 continue;
3176
3177 for (loc = bpt->loc; loc; loc = loc->next)
3178 if (!loc->inserted && should_be_inserted (loc))
3179 {
3180 some_failed = 1;
3181 break;
3182 }
3183 if (some_failed)
3184 {
3185 for (loc = bpt->loc; loc; loc = loc->next)
3186 if (loc->inserted)
3187 remove_breakpoint (loc);
3188
3189 hw_breakpoint_error = 1;
3190 fprintf_unfiltered (tmp_error_stream,
3191 "Could not insert hardware watchpoint %d.\n",
3192 bpt->number);
3193 error_flag = -1;
3194 }
3195 }
3196
3197 if (error_flag)
3198 {
3199 /* If a hardware breakpoint or watchpoint was inserted, add a
3200 message about possibly exhausted resources. */
3201 if (hw_breakpoint_error && !hw_bp_error_explained_already)
3202 {
3203 fprintf_unfiltered (tmp_error_stream,
3204 "Could not insert hardware breakpoints:\n\
3205 You may have requested too many hardware breakpoints/watchpoints.\n");
3206 }
3207 target_terminal_ours_for_output ();
3208 error_stream (tmp_error_stream);
3209 }
3210
3211 do_cleanups (cleanups);
3212 }
3213
3214 /* Used when the program stops.
3215 Returns zero if successful, or non-zero if there was a problem
3216 removing a breakpoint location. */
3217
3218 int
3219 remove_breakpoints (void)
3220 {
3221 struct bp_location *bl, **blp_tmp;
3222 int val = 0;
3223
3224 ALL_BP_LOCATIONS (bl, blp_tmp)
3225 {
3226 if (bl->inserted && !is_tracepoint (bl->owner))
3227 val |= remove_breakpoint (bl);
3228 }
3229 return val;
3230 }
3231
3232 /* When a thread exits, remove breakpoints that are related to
3233 that thread. */
3234
3235 static void
3236 remove_threaded_breakpoints (struct thread_info *tp, int silent)
3237 {
3238 struct breakpoint *b, *b_tmp;
3239
3240 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3241 {
3242 if (b->thread == tp->global_num && user_breakpoint_p (b))
3243 {
3244 b->disposition = disp_del_at_next_stop;
3245
3246 printf_filtered (_("\
3247 Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3248 b->number, print_thread_id (tp));
3249
3250 /* Hide it from the user. */
3251 b->number = 0;
3252 }
3253 }
3254 }
3255
3256 /* Remove breakpoints of process PID. */
3257
3258 int
3259 remove_breakpoints_pid (int pid)
3260 {
3261 struct bp_location *bl, **blp_tmp;
3262 int val;
3263 struct inferior *inf = find_inferior_pid (pid);
3264
3265 ALL_BP_LOCATIONS (bl, blp_tmp)
3266 {
3267 if (bl->pspace != inf->pspace)
3268 continue;
3269
3270 if (bl->inserted && !bl->target_info.persist)
3271 {
3272 val = remove_breakpoint (bl);
3273 if (val != 0)
3274 return val;
3275 }
3276 }
3277 return 0;
3278 }
3279
3280 int
3281 reattach_breakpoints (int pid)
3282 {
3283 struct cleanup *old_chain;
3284 struct bp_location *bl, **blp_tmp;
3285 int val;
3286 struct ui_file *tmp_error_stream;
3287 int dummy1 = 0, dummy2 = 0, dummy3 = 0;
3288 struct inferior *inf;
3289 struct thread_info *tp;
3290
3291 tp = any_live_thread_of_process (pid);
3292 if (tp == NULL)
3293 return 1;
3294
3295 inf = find_inferior_pid (pid);
3296 old_chain = save_inferior_ptid ();
3297
3298 inferior_ptid = tp->ptid;
3299
3300 tmp_error_stream = mem_fileopen ();
3301 make_cleanup_ui_file_delete (tmp_error_stream);
3302
3303 ALL_BP_LOCATIONS (bl, blp_tmp)
3304 {
3305 if (bl->pspace != inf->pspace)
3306 continue;
3307
3308 if (bl->inserted)
3309 {
3310 bl->inserted = 0;
3311 val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2, &dummy3);
3312 if (val != 0)
3313 {
3314 do_cleanups (old_chain);
3315 return val;
3316 }
3317 }
3318 }
3319 do_cleanups (old_chain);
3320 return 0;
3321 }
3322
3323 static int internal_breakpoint_number = -1;
3324
3325 /* Set the breakpoint number of B, depending on the value of INTERNAL.
3326 If INTERNAL is non-zero, the breakpoint number will be populated
3327 from internal_breakpoint_number and that variable decremented.
3328 Otherwise the breakpoint number will be populated from
3329 breakpoint_count and that value incremented. Internal breakpoints
3330 do not set the internal var bpnum. */
3331 static void
3332 set_breakpoint_number (int internal, struct breakpoint *b)
3333 {
3334 if (internal)
3335 b->number = internal_breakpoint_number--;
3336 else
3337 {
3338 set_breakpoint_count (breakpoint_count + 1);
3339 b->number = breakpoint_count;
3340 }
3341 }
3342
3343 static struct breakpoint *
3344 create_internal_breakpoint (struct gdbarch *gdbarch,
3345 CORE_ADDR address, enum bptype type,
3346 const struct breakpoint_ops *ops)
3347 {
3348 struct symtab_and_line sal;
3349 struct breakpoint *b;
3350
3351 init_sal (&sal); /* Initialize to zeroes. */
3352
3353 sal.pc = address;
3354 sal.section = find_pc_overlay (sal.pc);
3355 sal.pspace = current_program_space;
3356
3357 b = set_raw_breakpoint (gdbarch, sal, type, ops);
3358 b->number = internal_breakpoint_number--;
3359 b->disposition = disp_donttouch;
3360
3361 return b;
3362 }
3363
3364 static const char *const longjmp_names[] =
3365 {
3366 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3367 };
3368 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3369
3370 /* Per-objfile data private to breakpoint.c. */
3371 struct breakpoint_objfile_data
3372 {
3373 /* Minimal symbol for "_ovly_debug_event" (if any). */
3374 struct bound_minimal_symbol overlay_msym;
3375
3376 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3377 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
3378
3379 /* True if we have looked for longjmp probes. */
3380 int longjmp_searched;
3381
3382 /* SystemTap probe points for longjmp (if any). */
3383 VEC (probe_p) *longjmp_probes;
3384
3385 /* Minimal symbol for "std::terminate()" (if any). */
3386 struct bound_minimal_symbol terminate_msym;
3387
3388 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3389 struct bound_minimal_symbol exception_msym;
3390
3391 /* True if we have looked for exception probes. */
3392 int exception_searched;
3393
3394 /* SystemTap probe points for unwinding (if any). */
3395 VEC (probe_p) *exception_probes;
3396 };
3397
3398 static const struct objfile_data *breakpoint_objfile_key;
3399
3400 /* Minimal symbol not found sentinel. */
3401 static struct minimal_symbol msym_not_found;
3402
3403 /* Returns TRUE if MSYM point to the "not found" sentinel. */
3404
3405 static int
3406 msym_not_found_p (const struct minimal_symbol *msym)
3407 {
3408 return msym == &msym_not_found;
3409 }
3410
3411 /* Return per-objfile data needed by breakpoint.c.
3412 Allocate the data if necessary. */
3413
3414 static struct breakpoint_objfile_data *
3415 get_breakpoint_objfile_data (struct objfile *objfile)
3416 {
3417 struct breakpoint_objfile_data *bp_objfile_data;
3418
3419 bp_objfile_data = ((struct breakpoint_objfile_data *)
3420 objfile_data (objfile, breakpoint_objfile_key));
3421 if (bp_objfile_data == NULL)
3422 {
3423 bp_objfile_data =
3424 XOBNEW (&objfile->objfile_obstack, struct breakpoint_objfile_data);
3425
3426 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
3427 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3428 }
3429 return bp_objfile_data;
3430 }
3431
3432 static void
3433 free_breakpoint_probes (struct objfile *obj, void *data)
3434 {
3435 struct breakpoint_objfile_data *bp_objfile_data
3436 = (struct breakpoint_objfile_data *) data;
3437
3438 VEC_free (probe_p, bp_objfile_data->longjmp_probes);
3439 VEC_free (probe_p, bp_objfile_data->exception_probes);
3440 }
3441
3442 static void
3443 create_overlay_event_breakpoint (void)
3444 {
3445 struct objfile *objfile;
3446 const char *const func_name = "_ovly_debug_event";
3447
3448 ALL_OBJFILES (objfile)
3449 {
3450 struct breakpoint *b;
3451 struct breakpoint_objfile_data *bp_objfile_data;
3452 CORE_ADDR addr;
3453 struct explicit_location explicit_loc;
3454
3455 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3456
3457 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
3458 continue;
3459
3460 if (bp_objfile_data->overlay_msym.minsym == NULL)
3461 {
3462 struct bound_minimal_symbol m;
3463
3464 m = lookup_minimal_symbol_text (func_name, objfile);
3465 if (m.minsym == NULL)
3466 {
3467 /* Avoid future lookups in this objfile. */
3468 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
3469 continue;
3470 }
3471 bp_objfile_data->overlay_msym = m;
3472 }
3473
3474 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
3475 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3476 bp_overlay_event,
3477 &internal_breakpoint_ops);
3478 initialize_explicit_location (&explicit_loc);
3479 explicit_loc.function_name = ASTRDUP (func_name);
3480 b->location = new_explicit_location (&explicit_loc);
3481
3482 if (overlay_debugging == ovly_auto)
3483 {
3484 b->enable_state = bp_enabled;
3485 overlay_events_enabled = 1;
3486 }
3487 else
3488 {
3489 b->enable_state = bp_disabled;
3490 overlay_events_enabled = 0;
3491 }
3492 }
3493 }
3494
3495 static void
3496 create_longjmp_master_breakpoint (void)
3497 {
3498 struct program_space *pspace;
3499 struct cleanup *old_chain;
3500
3501 old_chain = save_current_program_space ();
3502
3503 ALL_PSPACES (pspace)
3504 {
3505 struct objfile *objfile;
3506
3507 set_current_program_space (pspace);
3508
3509 ALL_OBJFILES (objfile)
3510 {
3511 int i;
3512 struct gdbarch *gdbarch;
3513 struct breakpoint_objfile_data *bp_objfile_data;
3514
3515 gdbarch = get_objfile_arch (objfile);
3516
3517 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3518
3519 if (!bp_objfile_data->longjmp_searched)
3520 {
3521 VEC (probe_p) *ret;
3522
3523 ret = find_probes_in_objfile (objfile, "libc", "longjmp");
3524 if (ret != NULL)
3525 {
3526 /* We are only interested in checking one element. */
3527 struct probe *p = VEC_index (probe_p, ret, 0);
3528
3529 if (!can_evaluate_probe_arguments (p))
3530 {
3531 /* We cannot use the probe interface here, because it does
3532 not know how to evaluate arguments. */
3533 VEC_free (probe_p, ret);
3534 ret = NULL;
3535 }
3536 }
3537 bp_objfile_data->longjmp_probes = ret;
3538 bp_objfile_data->longjmp_searched = 1;
3539 }
3540
3541 if (bp_objfile_data->longjmp_probes != NULL)
3542 {
3543 int i;
3544 struct probe *probe;
3545 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3546
3547 for (i = 0;
3548 VEC_iterate (probe_p,
3549 bp_objfile_data->longjmp_probes,
3550 i, probe);
3551 ++i)
3552 {
3553 struct breakpoint *b;
3554
3555 b = create_internal_breakpoint (gdbarch,
3556 get_probe_address (probe,
3557 objfile),
3558 bp_longjmp_master,
3559 &internal_breakpoint_ops);
3560 b->location
3561 = new_probe_location ("-probe-stap libc:longjmp");
3562 b->enable_state = bp_disabled;
3563 }
3564
3565 continue;
3566 }
3567
3568 if (!gdbarch_get_longjmp_target_p (gdbarch))
3569 continue;
3570
3571 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
3572 {
3573 struct breakpoint *b;
3574 const char *func_name;
3575 CORE_ADDR addr;
3576 struct explicit_location explicit_loc;
3577
3578 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3579 continue;
3580
3581 func_name = longjmp_names[i];
3582 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3583 {
3584 struct bound_minimal_symbol m;
3585
3586 m = lookup_minimal_symbol_text (func_name, objfile);
3587 if (m.minsym == NULL)
3588 {
3589 /* Prevent future lookups in this objfile. */
3590 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
3591 continue;
3592 }
3593 bp_objfile_data->longjmp_msym[i] = m;
3594 }
3595
3596 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
3597 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3598 &internal_breakpoint_ops);
3599 initialize_explicit_location (&explicit_loc);
3600 explicit_loc.function_name = ASTRDUP (func_name);
3601 b->location = new_explicit_location (&explicit_loc);
3602 b->enable_state = bp_disabled;
3603 }
3604 }
3605 }
3606
3607 do_cleanups (old_chain);
3608 }
3609
3610 /* Create a master std::terminate breakpoint. */
3611 static void
3612 create_std_terminate_master_breakpoint (void)
3613 {
3614 struct program_space *pspace;
3615 struct cleanup *old_chain;
3616 const char *const func_name = "std::terminate()";
3617
3618 old_chain = save_current_program_space ();
3619
3620 ALL_PSPACES (pspace)
3621 {
3622 struct objfile *objfile;
3623 CORE_ADDR addr;
3624
3625 set_current_program_space (pspace);
3626
3627 ALL_OBJFILES (objfile)
3628 {
3629 struct breakpoint *b;
3630 struct breakpoint_objfile_data *bp_objfile_data;
3631 struct explicit_location explicit_loc;
3632
3633 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3634
3635 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3636 continue;
3637
3638 if (bp_objfile_data->terminate_msym.minsym == NULL)
3639 {
3640 struct bound_minimal_symbol m;
3641
3642 m = lookup_minimal_symbol (func_name, NULL, objfile);
3643 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3644 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
3645 {
3646 /* Prevent future lookups in this objfile. */
3647 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3648 continue;
3649 }
3650 bp_objfile_data->terminate_msym = m;
3651 }
3652
3653 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3654 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3655 bp_std_terminate_master,
3656 &internal_breakpoint_ops);
3657 initialize_explicit_location (&explicit_loc);
3658 explicit_loc.function_name = ASTRDUP (func_name);
3659 b->location = new_explicit_location (&explicit_loc);
3660 b->enable_state = bp_disabled;
3661 }
3662 }
3663
3664 do_cleanups (old_chain);
3665 }
3666
3667 /* Install a master breakpoint on the unwinder's debug hook. */
3668
3669 static void
3670 create_exception_master_breakpoint (void)
3671 {
3672 struct objfile *objfile;
3673 const char *const func_name = "_Unwind_DebugHook";
3674
3675 ALL_OBJFILES (objfile)
3676 {
3677 struct breakpoint *b;
3678 struct gdbarch *gdbarch;
3679 struct breakpoint_objfile_data *bp_objfile_data;
3680 CORE_ADDR addr;
3681 struct explicit_location explicit_loc;
3682
3683 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3684
3685 /* We prefer the SystemTap probe point if it exists. */
3686 if (!bp_objfile_data->exception_searched)
3687 {
3688 VEC (probe_p) *ret;
3689
3690 ret = find_probes_in_objfile (objfile, "libgcc", "unwind");
3691
3692 if (ret != NULL)
3693 {
3694 /* We are only interested in checking one element. */
3695 struct probe *p = VEC_index (probe_p, ret, 0);
3696
3697 if (!can_evaluate_probe_arguments (p))
3698 {
3699 /* We cannot use the probe interface here, because it does
3700 not know how to evaluate arguments. */
3701 VEC_free (probe_p, ret);
3702 ret = NULL;
3703 }
3704 }
3705 bp_objfile_data->exception_probes = ret;
3706 bp_objfile_data->exception_searched = 1;
3707 }
3708
3709 if (bp_objfile_data->exception_probes != NULL)
3710 {
3711 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3712 int i;
3713 struct probe *probe;
3714
3715 for (i = 0;
3716 VEC_iterate (probe_p,
3717 bp_objfile_data->exception_probes,
3718 i, probe);
3719 ++i)
3720 {
3721 struct breakpoint *b;
3722
3723 b = create_internal_breakpoint (gdbarch,
3724 get_probe_address (probe,
3725 objfile),
3726 bp_exception_master,
3727 &internal_breakpoint_ops);
3728 b->location
3729 = new_probe_location ("-probe-stap libgcc:unwind");
3730 b->enable_state = bp_disabled;
3731 }
3732
3733 continue;
3734 }
3735
3736 /* Otherwise, try the hook function. */
3737
3738 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3739 continue;
3740
3741 gdbarch = get_objfile_arch (objfile);
3742
3743 if (bp_objfile_data->exception_msym.minsym == NULL)
3744 {
3745 struct bound_minimal_symbol debug_hook;
3746
3747 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3748 if (debug_hook.minsym == NULL)
3749 {
3750 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3751 continue;
3752 }
3753
3754 bp_objfile_data->exception_msym = debug_hook;
3755 }
3756
3757 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
3758 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3759 &current_target);
3760 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3761 &internal_breakpoint_ops);
3762 initialize_explicit_location (&explicit_loc);
3763 explicit_loc.function_name = ASTRDUP (func_name);
3764 b->location = new_explicit_location (&explicit_loc);
3765 b->enable_state = bp_disabled;
3766 }
3767 }
3768
3769 /* Does B have a location spec? */
3770
3771 static int
3772 breakpoint_event_location_empty_p (const struct breakpoint *b)
3773 {
3774 return b->location != NULL && event_location_empty_p (b->location);
3775 }
3776
3777 void
3778 update_breakpoints_after_exec (void)
3779 {
3780 struct breakpoint *b, *b_tmp;
3781 struct bp_location *bploc, **bplocp_tmp;
3782
3783 /* We're about to delete breakpoints from GDB's lists. If the
3784 INSERTED flag is true, GDB will try to lift the breakpoints by
3785 writing the breakpoints' "shadow contents" back into memory. The
3786 "shadow contents" are NOT valid after an exec, so GDB should not
3787 do that. Instead, the target is responsible from marking
3788 breakpoints out as soon as it detects an exec. We don't do that
3789 here instead, because there may be other attempts to delete
3790 breakpoints after detecting an exec and before reaching here. */
3791 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
3792 if (bploc->pspace == current_program_space)
3793 gdb_assert (!bploc->inserted);
3794
3795 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3796 {
3797 if (b->pspace != current_program_space)
3798 continue;
3799
3800 /* Solib breakpoints must be explicitly reset after an exec(). */
3801 if (b->type == bp_shlib_event)
3802 {
3803 delete_breakpoint (b);
3804 continue;
3805 }
3806
3807 /* JIT breakpoints must be explicitly reset after an exec(). */
3808 if (b->type == bp_jit_event)
3809 {
3810 delete_breakpoint (b);
3811 continue;
3812 }
3813
3814 /* Thread event breakpoints must be set anew after an exec(),
3815 as must overlay event and longjmp master breakpoints. */
3816 if (b->type == bp_thread_event || b->type == bp_overlay_event
3817 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3818 || b->type == bp_exception_master)
3819 {
3820 delete_breakpoint (b);
3821 continue;
3822 }
3823
3824 /* Step-resume breakpoints are meaningless after an exec(). */
3825 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3826 {
3827 delete_breakpoint (b);
3828 continue;
3829 }
3830
3831 /* Just like single-step breakpoints. */
3832 if (b->type == bp_single_step)
3833 {
3834 delete_breakpoint (b);
3835 continue;
3836 }
3837
3838 /* Longjmp and longjmp-resume breakpoints are also meaningless
3839 after an exec. */
3840 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3841 || b->type == bp_longjmp_call_dummy
3842 || b->type == bp_exception || b->type == bp_exception_resume)
3843 {
3844 delete_breakpoint (b);
3845 continue;
3846 }
3847
3848 if (b->type == bp_catchpoint)
3849 {
3850 /* For now, none of the bp_catchpoint breakpoints need to
3851 do anything at this point. In the future, if some of
3852 the catchpoints need to something, we will need to add
3853 a new method, and call this method from here. */
3854 continue;
3855 }
3856
3857 /* bp_finish is a special case. The only way we ought to be able
3858 to see one of these when an exec() has happened, is if the user
3859 caught a vfork, and then said "finish". Ordinarily a finish just
3860 carries them to the call-site of the current callee, by setting
3861 a temporary bp there and resuming. But in this case, the finish
3862 will carry them entirely through the vfork & exec.
3863
3864 We don't want to allow a bp_finish to remain inserted now. But
3865 we can't safely delete it, 'cause finish_command has a handle to
3866 the bp on a bpstat, and will later want to delete it. There's a
3867 chance (and I've seen it happen) that if we delete the bp_finish
3868 here, that its storage will get reused by the time finish_command
3869 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3870 We really must allow finish_command to delete a bp_finish.
3871
3872 In the absence of a general solution for the "how do we know
3873 it's safe to delete something others may have handles to?"
3874 problem, what we'll do here is just uninsert the bp_finish, and
3875 let finish_command delete it.
3876
3877 (We know the bp_finish is "doomed" in the sense that it's
3878 momentary, and will be deleted as soon as finish_command sees
3879 the inferior stopped. So it doesn't matter that the bp's
3880 address is probably bogus in the new a.out, unlike e.g., the
3881 solib breakpoints.) */
3882
3883 if (b->type == bp_finish)
3884 {
3885 continue;
3886 }
3887
3888 /* Without a symbolic address, we have little hope of the
3889 pre-exec() address meaning the same thing in the post-exec()
3890 a.out. */
3891 if (breakpoint_event_location_empty_p (b))
3892 {
3893 delete_breakpoint (b);
3894 continue;
3895 }
3896 }
3897 }
3898
3899 int
3900 detach_breakpoints (ptid_t ptid)
3901 {
3902 struct bp_location *bl, **blp_tmp;
3903 int val = 0;
3904 struct cleanup *old_chain = save_inferior_ptid ();
3905 struct inferior *inf = current_inferior ();
3906
3907 if (ptid_get_pid (ptid) == ptid_get_pid (inferior_ptid))
3908 error (_("Cannot detach breakpoints of inferior_ptid"));
3909
3910 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
3911 inferior_ptid = ptid;
3912 ALL_BP_LOCATIONS (bl, blp_tmp)
3913 {
3914 if (bl->pspace != inf->pspace)
3915 continue;
3916
3917 /* This function must physically remove breakpoints locations
3918 from the specified ptid, without modifying the breakpoint
3919 package's state. Locations of type bp_loc_other are only
3920 maintained at GDB side. So, there is no need to remove
3921 these bp_loc_other locations. Moreover, removing these
3922 would modify the breakpoint package's state. */
3923 if (bl->loc_type == bp_loc_other)
3924 continue;
3925
3926 if (bl->inserted)
3927 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
3928 }
3929
3930 do_cleanups (old_chain);
3931 return val;
3932 }
3933
3934 /* Remove the breakpoint location BL from the current address space.
3935 Note that this is used to detach breakpoints from a child fork.
3936 When we get here, the child isn't in the inferior list, and neither
3937 do we have objects to represent its address space --- we should
3938 *not* look at bl->pspace->aspace here. */
3939
3940 static int
3941 remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
3942 {
3943 int val;
3944
3945 /* BL is never in moribund_locations by our callers. */
3946 gdb_assert (bl->owner != NULL);
3947
3948 /* The type of none suggests that owner is actually deleted.
3949 This should not ever happen. */
3950 gdb_assert (bl->owner->type != bp_none);
3951
3952 if (bl->loc_type == bp_loc_software_breakpoint
3953 || bl->loc_type == bp_loc_hardware_breakpoint)
3954 {
3955 /* "Normal" instruction breakpoint: either the standard
3956 trap-instruction bp (bp_breakpoint), or a
3957 bp_hardware_breakpoint. */
3958
3959 /* First check to see if we have to handle an overlay. */
3960 if (overlay_debugging == ovly_off
3961 || bl->section == NULL
3962 || !(section_is_overlay (bl->section)))
3963 {
3964 /* No overlay handling: just remove the breakpoint. */
3965
3966 /* If we're trying to uninsert a memory breakpoint that we
3967 know is set in a dynamic object that is marked
3968 shlib_disabled, then either the dynamic object was
3969 removed with "remove-symbol-file" or with
3970 "nosharedlibrary". In the former case, we don't know
3971 whether another dynamic object might have loaded over the
3972 breakpoint's address -- the user might well let us know
3973 about it next with add-symbol-file (the whole point of
3974 add-symbol-file is letting the user manually maintain a
3975 list of dynamically loaded objects). If we have the
3976 breakpoint's shadow memory, that is, this is a software
3977 breakpoint managed by GDB, check whether the breakpoint
3978 is still inserted in memory, to avoid overwriting wrong
3979 code with stale saved shadow contents. Note that HW
3980 breakpoints don't have shadow memory, as they're
3981 implemented using a mechanism that is not dependent on
3982 being able to modify the target's memory, and as such
3983 they should always be removed. */
3984 if (bl->shlib_disabled
3985 && bl->target_info.shadow_len != 0
3986 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3987 val = 0;
3988 else
3989 val = bl->owner->ops->remove_location (bl, reason);
3990 }
3991 else
3992 {
3993 /* This breakpoint is in an overlay section.
3994 Did we set a breakpoint at the LMA? */
3995 if (!overlay_events_enabled)
3996 {
3997 /* Yes -- overlay event support is not active, so we
3998 should have set a breakpoint at the LMA. Remove it.
3999 */
4000 /* Ignore any failures: if the LMA is in ROM, we will
4001 have already warned when we failed to insert it. */
4002 if (bl->loc_type == bp_loc_hardware_breakpoint)
4003 target_remove_hw_breakpoint (bl->gdbarch,
4004 &bl->overlay_target_info);
4005 else
4006 target_remove_breakpoint (bl->gdbarch,
4007 &bl->overlay_target_info,
4008 reason);
4009 }
4010 /* Did we set a breakpoint at the VMA?
4011 If so, we will have marked the breakpoint 'inserted'. */
4012 if (bl->inserted)
4013 {
4014 /* Yes -- remove it. Previously we did not bother to
4015 remove the breakpoint if the section had been
4016 unmapped, but let's not rely on that being safe. We
4017 don't know what the overlay manager might do. */
4018
4019 /* However, we should remove *software* breakpoints only
4020 if the section is still mapped, or else we overwrite
4021 wrong code with the saved shadow contents. */
4022 if (bl->loc_type == bp_loc_hardware_breakpoint
4023 || section_is_mapped (bl->section))
4024 val = bl->owner->ops->remove_location (bl, reason);
4025 else
4026 val = 0;
4027 }
4028 else
4029 {
4030 /* No -- not inserted, so no need to remove. No error. */
4031 val = 0;
4032 }
4033 }
4034
4035 /* In some cases, we might not be able to remove a breakpoint in
4036 a shared library that has already been removed, but we have
4037 not yet processed the shlib unload event. Similarly for an
4038 unloaded add-symbol-file object - the user might not yet have
4039 had the chance to remove-symbol-file it. shlib_disabled will
4040 be set if the library/object has already been removed, but
4041 the breakpoint hasn't been uninserted yet, e.g., after
4042 "nosharedlibrary" or "remove-symbol-file" with breakpoints
4043 always-inserted mode. */
4044 if (val
4045 && (bl->loc_type == bp_loc_software_breakpoint
4046 && (bl->shlib_disabled
4047 || solib_name_from_address (bl->pspace, bl->address)
4048 || shared_objfile_contains_address_p (bl->pspace,
4049 bl->address))))
4050 val = 0;
4051
4052 if (val)
4053 return val;
4054 bl->inserted = (reason == DETACH_BREAKPOINT);
4055 }
4056 else if (bl->loc_type == bp_loc_hardware_watchpoint)
4057 {
4058 gdb_assert (bl->owner->ops != NULL
4059 && bl->owner->ops->remove_location != NULL);
4060
4061 bl->inserted = (reason == DETACH_BREAKPOINT);
4062 bl->owner->ops->remove_location (bl, reason);
4063
4064 /* Failure to remove any of the hardware watchpoints comes here. */
4065 if (reason == REMOVE_BREAKPOINT && bl->inserted)
4066 warning (_("Could not remove hardware watchpoint %d."),
4067 bl->owner->number);
4068 }
4069 else if (bl->owner->type == bp_catchpoint
4070 && breakpoint_enabled (bl->owner)
4071 && !bl->duplicate)
4072 {
4073 gdb_assert (bl->owner->ops != NULL
4074 && bl->owner->ops->remove_location != NULL);
4075
4076 val = bl->owner->ops->remove_location (bl, reason);
4077 if (val)
4078 return val;
4079
4080 bl->inserted = (reason == DETACH_BREAKPOINT);
4081 }
4082
4083 return 0;
4084 }
4085
4086 static int
4087 remove_breakpoint (struct bp_location *bl)
4088 {
4089 int ret;
4090 struct cleanup *old_chain;
4091
4092 /* BL is never in moribund_locations by our callers. */
4093 gdb_assert (bl->owner != NULL);
4094
4095 /* The type of none suggests that owner is actually deleted.
4096 This should not ever happen. */
4097 gdb_assert (bl->owner->type != bp_none);
4098
4099 old_chain = save_current_space_and_thread ();
4100
4101 switch_to_program_space_and_thread (bl->pspace);
4102
4103 ret = remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
4104
4105 do_cleanups (old_chain);
4106 return ret;
4107 }
4108
4109 /* Clear the "inserted" flag in all breakpoints. */
4110
4111 void
4112 mark_breakpoints_out (void)
4113 {
4114 struct bp_location *bl, **blp_tmp;
4115
4116 ALL_BP_LOCATIONS (bl, blp_tmp)
4117 if (bl->pspace == current_program_space)
4118 bl->inserted = 0;
4119 }
4120
4121 /* Clear the "inserted" flag in all breakpoints and delete any
4122 breakpoints which should go away between runs of the program.
4123
4124 Plus other such housekeeping that has to be done for breakpoints
4125 between runs.
4126
4127 Note: this function gets called at the end of a run (by
4128 generic_mourn_inferior) and when a run begins (by
4129 init_wait_for_inferior). */
4130
4131
4132
4133 void
4134 breakpoint_init_inferior (enum inf_context context)
4135 {
4136 struct breakpoint *b, *b_tmp;
4137 struct bp_location *bl;
4138 int ix;
4139 struct program_space *pspace = current_program_space;
4140
4141 /* If breakpoint locations are shared across processes, then there's
4142 nothing to do. */
4143 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
4144 return;
4145
4146 mark_breakpoints_out ();
4147
4148 ALL_BREAKPOINTS_SAFE (b, b_tmp)
4149 {
4150 if (b->loc && b->loc->pspace != pspace)
4151 continue;
4152
4153 switch (b->type)
4154 {
4155 case bp_call_dummy:
4156 case bp_longjmp_call_dummy:
4157
4158 /* If the call dummy breakpoint is at the entry point it will
4159 cause problems when the inferior is rerun, so we better get
4160 rid of it. */
4161
4162 case bp_watchpoint_scope:
4163
4164 /* Also get rid of scope breakpoints. */
4165
4166 case bp_shlib_event:
4167
4168 /* Also remove solib event breakpoints. Their addresses may
4169 have changed since the last time we ran the program.
4170 Actually we may now be debugging against different target;
4171 and so the solib backend that installed this breakpoint may
4172 not be used in by the target. E.g.,
4173
4174 (gdb) file prog-linux
4175 (gdb) run # native linux target
4176 ...
4177 (gdb) kill
4178 (gdb) file prog-win.exe
4179 (gdb) tar rem :9999 # remote Windows gdbserver.
4180 */
4181
4182 case bp_step_resume:
4183
4184 /* Also remove step-resume breakpoints. */
4185
4186 case bp_single_step:
4187
4188 /* Also remove single-step breakpoints. */
4189
4190 delete_breakpoint (b);
4191 break;
4192
4193 case bp_watchpoint:
4194 case bp_hardware_watchpoint:
4195 case bp_read_watchpoint:
4196 case bp_access_watchpoint:
4197 {
4198 struct watchpoint *w = (struct watchpoint *) b;
4199
4200 /* Likewise for watchpoints on local expressions. */
4201 if (w->exp_valid_block != NULL)
4202 delete_breakpoint (b);
4203 else
4204 {
4205 /* Get rid of existing locations, which are no longer
4206 valid. New ones will be created in
4207 update_watchpoint, when the inferior is restarted.
4208 The next update_global_location_list call will
4209 garbage collect them. */
4210 b->loc = NULL;
4211
4212 if (context == inf_starting)
4213 {
4214 /* Reset val field to force reread of starting value in
4215 insert_breakpoints. */
4216 if (w->val)
4217 value_free (w->val);
4218 w->val = NULL;
4219 w->val_valid = 0;
4220 }
4221 }
4222 }
4223 break;
4224 default:
4225 break;
4226 }
4227 }
4228
4229 /* Get rid of the moribund locations. */
4230 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
4231 decref_bp_location (&bl);
4232 VEC_free (bp_location_p, moribund_locations);
4233 }
4234
4235 /* These functions concern about actual breakpoints inserted in the
4236 target --- to e.g. check if we need to do decr_pc adjustment or if
4237 we need to hop over the bkpt --- so we check for address space
4238 match, not program space. */
4239
4240 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4241 exists at PC. It returns ordinary_breakpoint_here if it's an
4242 ordinary breakpoint, or permanent_breakpoint_here if it's a
4243 permanent breakpoint.
4244 - When continuing from a location with an ordinary breakpoint, we
4245 actually single step once before calling insert_breakpoints.
4246 - When continuing from a location with a permanent breakpoint, we
4247 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4248 the target, to advance the PC past the breakpoint. */
4249
4250 enum breakpoint_here
4251 breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
4252 {
4253 struct bp_location *bl, **blp_tmp;
4254 int any_breakpoint_here = 0;
4255
4256 ALL_BP_LOCATIONS (bl, blp_tmp)
4257 {
4258 if (bl->loc_type != bp_loc_software_breakpoint
4259 && bl->loc_type != bp_loc_hardware_breakpoint)
4260 continue;
4261
4262 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
4263 if ((breakpoint_enabled (bl->owner)
4264 || bl->permanent)
4265 && breakpoint_location_address_match (bl, aspace, pc))
4266 {
4267 if (overlay_debugging
4268 && section_is_overlay (bl->section)
4269 && !section_is_mapped (bl->section))
4270 continue; /* unmapped overlay -- can't be a match */
4271 else if (bl->permanent)
4272 return permanent_breakpoint_here;
4273 else
4274 any_breakpoint_here = 1;
4275 }
4276 }
4277
4278 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
4279 }
4280
4281 /* See breakpoint.h. */
4282
4283 int
4284 breakpoint_in_range_p (struct address_space *aspace,
4285 CORE_ADDR addr, ULONGEST len)
4286 {
4287 struct bp_location *bl, **blp_tmp;
4288
4289 ALL_BP_LOCATIONS (bl, blp_tmp)
4290 {
4291 if (bl->loc_type != bp_loc_software_breakpoint
4292 && bl->loc_type != bp_loc_hardware_breakpoint)
4293 continue;
4294
4295 if ((breakpoint_enabled (bl->owner)
4296 || bl->permanent)
4297 && breakpoint_location_address_range_overlap (bl, aspace,
4298 addr, len))
4299 {
4300 if (overlay_debugging
4301 && section_is_overlay (bl->section)
4302 && !section_is_mapped (bl->section))
4303 {
4304 /* Unmapped overlay -- can't be a match. */
4305 continue;
4306 }
4307
4308 return 1;
4309 }
4310 }
4311
4312 return 0;
4313 }
4314
4315 /* Return true if there's a moribund breakpoint at PC. */
4316
4317 int
4318 moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
4319 {
4320 struct bp_location *loc;
4321 int ix;
4322
4323 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
4324 if (breakpoint_location_address_match (loc, aspace, pc))
4325 return 1;
4326
4327 return 0;
4328 }
4329
4330 /* Returns non-zero iff BL is inserted at PC, in address space
4331 ASPACE. */
4332
4333 static int
4334 bp_location_inserted_here_p (struct bp_location *bl,
4335 struct address_space *aspace, CORE_ADDR pc)
4336 {
4337 if (bl->inserted
4338 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4339 aspace, pc))
4340 {
4341 if (overlay_debugging
4342 && section_is_overlay (bl->section)
4343 && !section_is_mapped (bl->section))
4344 return 0; /* unmapped overlay -- can't be a match */
4345 else
4346 return 1;
4347 }
4348 return 0;
4349 }
4350
4351 /* Returns non-zero iff there's a breakpoint inserted at PC. */
4352
4353 int
4354 breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
4355 {
4356 struct bp_location **blp, **blp_tmp = NULL;
4357
4358 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4359 {
4360 struct bp_location *bl = *blp;
4361
4362 if (bl->loc_type != bp_loc_software_breakpoint
4363 && bl->loc_type != bp_loc_hardware_breakpoint)
4364 continue;
4365
4366 if (bp_location_inserted_here_p (bl, aspace, pc))
4367 return 1;
4368 }
4369 return 0;
4370 }
4371
4372 /* This function returns non-zero iff there is a software breakpoint
4373 inserted at PC. */
4374
4375 int
4376 software_breakpoint_inserted_here_p (struct address_space *aspace,
4377 CORE_ADDR pc)
4378 {
4379 struct bp_location **blp, **blp_tmp = NULL;
4380
4381 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4382 {
4383 struct bp_location *bl = *blp;
4384
4385 if (bl->loc_type != bp_loc_software_breakpoint)
4386 continue;
4387
4388 if (bp_location_inserted_here_p (bl, aspace, pc))
4389 return 1;
4390 }
4391
4392 return 0;
4393 }
4394
4395 /* See breakpoint.h. */
4396
4397 int
4398 hardware_breakpoint_inserted_here_p (struct address_space *aspace,
4399 CORE_ADDR pc)
4400 {
4401 struct bp_location **blp, **blp_tmp = NULL;
4402
4403 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4404 {
4405 struct bp_location *bl = *blp;
4406
4407 if (bl->loc_type != bp_loc_hardware_breakpoint)
4408 continue;
4409
4410 if (bp_location_inserted_here_p (bl, aspace, pc))
4411 return 1;
4412 }
4413
4414 return 0;
4415 }
4416
4417 int
4418 hardware_watchpoint_inserted_in_range (struct address_space *aspace,
4419 CORE_ADDR addr, ULONGEST len)
4420 {
4421 struct breakpoint *bpt;
4422
4423 ALL_BREAKPOINTS (bpt)
4424 {
4425 struct bp_location *loc;
4426
4427 if (bpt->type != bp_hardware_watchpoint
4428 && bpt->type != bp_access_watchpoint)
4429 continue;
4430
4431 if (!breakpoint_enabled (bpt))
4432 continue;
4433
4434 for (loc = bpt->loc; loc; loc = loc->next)
4435 if (loc->pspace->aspace == aspace && loc->inserted)
4436 {
4437 CORE_ADDR l, h;
4438
4439 /* Check for intersection. */
4440 l = std::max<CORE_ADDR> (loc->address, addr);
4441 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
4442 if (l < h)
4443 return 1;
4444 }
4445 }
4446 return 0;
4447 }
4448 \f
4449
4450 /* bpstat stuff. External routines' interfaces are documented
4451 in breakpoint.h. */
4452
4453 int
4454 is_catchpoint (struct breakpoint *ep)
4455 {
4456 return (ep->type == bp_catchpoint);
4457 }
4458
4459 /* Frees any storage that is part of a bpstat. Does not walk the
4460 'next' chain. */
4461
4462 static void
4463 bpstat_free (bpstat bs)
4464 {
4465 if (bs->old_val != NULL)
4466 value_free (bs->old_val);
4467 decref_counted_command_line (&bs->commands);
4468 decref_bp_location (&bs->bp_location_at);
4469 xfree (bs);
4470 }
4471
4472 /* Clear a bpstat so that it says we are not at any breakpoint.
4473 Also free any storage that is part of a bpstat. */
4474
4475 void
4476 bpstat_clear (bpstat *bsp)
4477 {
4478 bpstat p;
4479 bpstat q;
4480
4481 if (bsp == 0)
4482 return;
4483 p = *bsp;
4484 while (p != NULL)
4485 {
4486 q = p->next;
4487 bpstat_free (p);
4488 p = q;
4489 }
4490 *bsp = NULL;
4491 }
4492
4493 /* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4494 is part of the bpstat is copied as well. */
4495
4496 bpstat
4497 bpstat_copy (bpstat bs)
4498 {
4499 bpstat p = NULL;
4500 bpstat tmp;
4501 bpstat retval = NULL;
4502
4503 if (bs == NULL)
4504 return bs;
4505
4506 for (; bs != NULL; bs = bs->next)
4507 {
4508 tmp = (bpstat) xmalloc (sizeof (*tmp));
4509 memcpy (tmp, bs, sizeof (*tmp));
4510 incref_counted_command_line (tmp->commands);
4511 incref_bp_location (tmp->bp_location_at);
4512 if (bs->old_val != NULL)
4513 {
4514 tmp->old_val = value_copy (bs->old_val);
4515 release_value (tmp->old_val);
4516 }
4517
4518 if (p == NULL)
4519 /* This is the first thing in the chain. */
4520 retval = tmp;
4521 else
4522 p->next = tmp;
4523 p = tmp;
4524 }
4525 p->next = NULL;
4526 return retval;
4527 }
4528
4529 /* Find the bpstat associated with this breakpoint. */
4530
4531 bpstat
4532 bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
4533 {
4534 if (bsp == NULL)
4535 return NULL;
4536
4537 for (; bsp != NULL; bsp = bsp->next)
4538 {
4539 if (bsp->breakpoint_at == breakpoint)
4540 return bsp;
4541 }
4542 return NULL;
4543 }
4544
4545 /* See breakpoint.h. */
4546
4547 int
4548 bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
4549 {
4550 for (; bsp != NULL; bsp = bsp->next)
4551 {
4552 if (bsp->breakpoint_at == NULL)
4553 {
4554 /* A moribund location can never explain a signal other than
4555 GDB_SIGNAL_TRAP. */
4556 if (sig == GDB_SIGNAL_TRAP)
4557 return 1;
4558 }
4559 else
4560 {
4561 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4562 sig))
4563 return 1;
4564 }
4565 }
4566
4567 return 0;
4568 }
4569
4570 /* Put in *NUM the breakpoint number of the first breakpoint we are
4571 stopped at. *BSP upon return is a bpstat which points to the
4572 remaining breakpoints stopped at (but which is not guaranteed to be
4573 good for anything but further calls to bpstat_num).
4574
4575 Return 0 if passed a bpstat which does not indicate any breakpoints.
4576 Return -1 if stopped at a breakpoint that has been deleted since
4577 we set it.
4578 Return 1 otherwise. */
4579
4580 int
4581 bpstat_num (bpstat *bsp, int *num)
4582 {
4583 struct breakpoint *b;
4584
4585 if ((*bsp) == NULL)
4586 return 0; /* No more breakpoint values */
4587
4588 /* We assume we'll never have several bpstats that correspond to a
4589 single breakpoint -- otherwise, this function might return the
4590 same number more than once and this will look ugly. */
4591 b = (*bsp)->breakpoint_at;
4592 *bsp = (*bsp)->next;
4593 if (b == NULL)
4594 return -1; /* breakpoint that's been deleted since */
4595
4596 *num = b->number; /* We have its number */
4597 return 1;
4598 }
4599
4600 /* See breakpoint.h. */
4601
4602 void
4603 bpstat_clear_actions (void)
4604 {
4605 struct thread_info *tp;
4606 bpstat bs;
4607
4608 if (ptid_equal (inferior_ptid, null_ptid))
4609 return;
4610
4611 tp = find_thread_ptid (inferior_ptid);
4612 if (tp == NULL)
4613 return;
4614
4615 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
4616 {
4617 decref_counted_command_line (&bs->commands);
4618
4619 if (bs->old_val != NULL)
4620 {
4621 value_free (bs->old_val);
4622 bs->old_val = NULL;
4623 }
4624 }
4625 }
4626
4627 /* Called when a command is about to proceed the inferior. */
4628
4629 static void
4630 breakpoint_about_to_proceed (void)
4631 {
4632 if (!ptid_equal (inferior_ptid, null_ptid))
4633 {
4634 struct thread_info *tp = inferior_thread ();
4635
4636 /* Allow inferior function calls in breakpoint commands to not
4637 interrupt the command list. When the call finishes
4638 successfully, the inferior will be standing at the same
4639 breakpoint as if nothing happened. */
4640 if (tp->control.in_infcall)
4641 return;
4642 }
4643
4644 breakpoint_proceeded = 1;
4645 }
4646
4647 /* Stub for cleaning up our state if we error-out of a breakpoint
4648 command. */
4649 static void
4650 cleanup_executing_breakpoints (void *ignore)
4651 {
4652 executing_breakpoint_commands = 0;
4653 }
4654
4655 /* Return non-zero iff CMD as the first line of a command sequence is `silent'
4656 or its equivalent. */
4657
4658 static int
4659 command_line_is_silent (struct command_line *cmd)
4660 {
4661 return cmd && (strcmp ("silent", cmd->line) == 0);
4662 }
4663
4664 /* Execute all the commands associated with all the breakpoints at
4665 this location. Any of these commands could cause the process to
4666 proceed beyond this point, etc. We look out for such changes by
4667 checking the global "breakpoint_proceeded" after each command.
4668
4669 Returns true if a breakpoint command resumed the inferior. In that
4670 case, it is the caller's responsibility to recall it again with the
4671 bpstat of the current thread. */
4672
4673 static int
4674 bpstat_do_actions_1 (bpstat *bsp)
4675 {
4676 bpstat bs;
4677 struct cleanup *old_chain;
4678 int again = 0;
4679
4680 /* Avoid endless recursion if a `source' command is contained
4681 in bs->commands. */
4682 if (executing_breakpoint_commands)
4683 return 0;
4684
4685 executing_breakpoint_commands = 1;
4686 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
4687
4688 prevent_dont_repeat ();
4689
4690 /* This pointer will iterate over the list of bpstat's. */
4691 bs = *bsp;
4692
4693 breakpoint_proceeded = 0;
4694 for (; bs != NULL; bs = bs->next)
4695 {
4696 struct counted_command_line *ccmd;
4697 struct command_line *cmd;
4698 struct cleanup *this_cmd_tree_chain;
4699
4700 /* Take ownership of the BSP's command tree, if it has one.
4701
4702 The command tree could legitimately contain commands like
4703 'step' and 'next', which call clear_proceed_status, which
4704 frees stop_bpstat's command tree. To make sure this doesn't
4705 free the tree we're executing out from under us, we need to
4706 take ownership of the tree ourselves. Since a given bpstat's
4707 commands are only executed once, we don't need to copy it; we
4708 can clear the pointer in the bpstat, and make sure we free
4709 the tree when we're done. */
4710 ccmd = bs->commands;
4711 bs->commands = NULL;
4712 this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
4713 cmd = ccmd ? ccmd->commands : NULL;
4714 if (command_line_is_silent (cmd))
4715 {
4716 /* The action has been already done by bpstat_stop_status. */
4717 cmd = cmd->next;
4718 }
4719
4720 while (cmd != NULL)
4721 {
4722 execute_control_command (cmd);
4723
4724 if (breakpoint_proceeded)
4725 break;
4726 else
4727 cmd = cmd->next;
4728 }
4729
4730 /* We can free this command tree now. */
4731 do_cleanups (this_cmd_tree_chain);
4732
4733 if (breakpoint_proceeded)
4734 {
4735 if (current_ui->async)
4736 /* If we are in async mode, then the target might be still
4737 running, not stopped at any breakpoint, so nothing for
4738 us to do here -- just return to the event loop. */
4739 ;
4740 else
4741 /* In sync mode, when execute_control_command returns
4742 we're already standing on the next breakpoint.
4743 Breakpoint commands for that stop were not run, since
4744 execute_command does not run breakpoint commands --
4745 only command_line_handler does, but that one is not
4746 involved in execution of breakpoint commands. So, we
4747 can now execute breakpoint commands. It should be
4748 noted that making execute_command do bpstat actions is
4749 not an option -- in this case we'll have recursive
4750 invocation of bpstat for each breakpoint with a
4751 command, and can easily blow up GDB stack. Instead, we
4752 return true, which will trigger the caller to recall us
4753 with the new stop_bpstat. */
4754 again = 1;
4755 break;
4756 }
4757 }
4758 do_cleanups (old_chain);
4759 return again;
4760 }
4761
4762 void
4763 bpstat_do_actions (void)
4764 {
4765 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4766
4767 /* Do any commands attached to breakpoint we are stopped at. */
4768 while (!ptid_equal (inferior_ptid, null_ptid)
4769 && target_has_execution
4770 && !is_exited (inferior_ptid)
4771 && !is_executing (inferior_ptid))
4772 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4773 and only return when it is stopped at the next breakpoint, we
4774 keep doing breakpoint actions until it returns false to
4775 indicate the inferior was not resumed. */
4776 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
4777 break;
4778
4779 discard_cleanups (cleanup_if_error);
4780 }
4781
4782 /* Print out the (old or new) value associated with a watchpoint. */
4783
4784 static void
4785 watchpoint_value_print (struct value *val, struct ui_file *stream)
4786 {
4787 if (val == NULL)
4788 fprintf_unfiltered (stream, _("<unreadable>"));
4789 else
4790 {
4791 struct value_print_options opts;
4792 get_user_print_options (&opts);
4793 value_print (val, stream, &opts);
4794 }
4795 }
4796
4797 /* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4798 debugging multiple threads. */
4799
4800 void
4801 maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4802 {
4803 if (ui_out_is_mi_like_p (uiout))
4804 return;
4805
4806 ui_out_text (uiout, "\n");
4807
4808 if (show_thread_that_caused_stop ())
4809 {
4810 const char *name;
4811 struct thread_info *thr = inferior_thread ();
4812
4813 ui_out_text (uiout, "Thread ");
4814 ui_out_field_fmt (uiout, "thread-id", "%s", print_thread_id (thr));
4815
4816 name = thr->name != NULL ? thr->name : target_thread_name (thr);
4817 if (name != NULL)
4818 {
4819 ui_out_text (uiout, " \"");
4820 ui_out_field_fmt (uiout, "name", "%s", name);
4821 ui_out_text (uiout, "\"");
4822 }
4823
4824 ui_out_text (uiout, " hit ");
4825 }
4826 }
4827
4828 /* Generic routine for printing messages indicating why we
4829 stopped. The behavior of this function depends on the value
4830 'print_it' in the bpstat structure. Under some circumstances we
4831 may decide not to print anything here and delegate the task to
4832 normal_stop(). */
4833
4834 static enum print_stop_action
4835 print_bp_stop_message (bpstat bs)
4836 {
4837 switch (bs->print_it)
4838 {
4839 case print_it_noop:
4840 /* Nothing should be printed for this bpstat entry. */
4841 return PRINT_UNKNOWN;
4842 break;
4843
4844 case print_it_done:
4845 /* We still want to print the frame, but we already printed the
4846 relevant messages. */
4847 return PRINT_SRC_AND_LOC;
4848 break;
4849
4850 case print_it_normal:
4851 {
4852 struct breakpoint *b = bs->breakpoint_at;
4853
4854 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4855 which has since been deleted. */
4856 if (b == NULL)
4857 return PRINT_UNKNOWN;
4858
4859 /* Normal case. Call the breakpoint's print_it method. */
4860 return b->ops->print_it (bs);
4861 }
4862 break;
4863
4864 default:
4865 internal_error (__FILE__, __LINE__,
4866 _("print_bp_stop_message: unrecognized enum value"));
4867 break;
4868 }
4869 }
4870
4871 /* A helper function that prints a shared library stopped event. */
4872
4873 static void
4874 print_solib_event (int is_catchpoint)
4875 {
4876 int any_deleted
4877 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4878 int any_added
4879 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4880
4881 if (!is_catchpoint)
4882 {
4883 if (any_added || any_deleted)
4884 ui_out_text (current_uiout,
4885 _("Stopped due to shared library event:\n"));
4886 else
4887 ui_out_text (current_uiout,
4888 _("Stopped due to shared library event (no "
4889 "libraries added or removed)\n"));
4890 }
4891
4892 if (ui_out_is_mi_like_p (current_uiout))
4893 ui_out_field_string (current_uiout, "reason",
4894 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4895
4896 if (any_deleted)
4897 {
4898 struct cleanup *cleanup;
4899 char *name;
4900 int ix;
4901
4902 ui_out_text (current_uiout, _(" Inferior unloaded "));
4903 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4904 "removed");
4905 for (ix = 0;
4906 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4907 ix, name);
4908 ++ix)
4909 {
4910 if (ix > 0)
4911 ui_out_text (current_uiout, " ");
4912 ui_out_field_string (current_uiout, "library", name);
4913 ui_out_text (current_uiout, "\n");
4914 }
4915
4916 do_cleanups (cleanup);
4917 }
4918
4919 if (any_added)
4920 {
4921 struct so_list *iter;
4922 int ix;
4923 struct cleanup *cleanup;
4924
4925 ui_out_text (current_uiout, _(" Inferior loaded "));
4926 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4927 "added");
4928 for (ix = 0;
4929 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4930 ix, iter);
4931 ++ix)
4932 {
4933 if (ix > 0)
4934 ui_out_text (current_uiout, " ");
4935 ui_out_field_string (current_uiout, "library", iter->so_name);
4936 ui_out_text (current_uiout, "\n");
4937 }
4938
4939 do_cleanups (cleanup);
4940 }
4941 }
4942
4943 /* Print a message indicating what happened. This is called from
4944 normal_stop(). The input to this routine is the head of the bpstat
4945 list - a list of the eventpoints that caused this stop. KIND is
4946 the target_waitkind for the stopping event. This
4947 routine calls the generic print routine for printing a message
4948 about reasons for stopping. This will print (for example) the
4949 "Breakpoint n," part of the output. The return value of this
4950 routine is one of:
4951
4952 PRINT_UNKNOWN: Means we printed nothing.
4953 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4954 code to print the location. An example is
4955 "Breakpoint 1, " which should be followed by
4956 the location.
4957 PRINT_SRC_ONLY: Means we printed something, but there is no need
4958 to also print the location part of the message.
4959 An example is the catch/throw messages, which
4960 don't require a location appended to the end.
4961 PRINT_NOTHING: We have done some printing and we don't need any
4962 further info to be printed. */
4963
4964 enum print_stop_action
4965 bpstat_print (bpstat bs, int kind)
4966 {
4967 enum print_stop_action val;
4968
4969 /* Maybe another breakpoint in the chain caused us to stop.
4970 (Currently all watchpoints go on the bpstat whether hit or not.
4971 That probably could (should) be changed, provided care is taken
4972 with respect to bpstat_explains_signal). */
4973 for (; bs; bs = bs->next)
4974 {
4975 val = print_bp_stop_message (bs);
4976 if (val == PRINT_SRC_ONLY
4977 || val == PRINT_SRC_AND_LOC
4978 || val == PRINT_NOTHING)
4979 return val;
4980 }
4981
4982 /* If we had hit a shared library event breakpoint,
4983 print_bp_stop_message would print out this message. If we hit an
4984 OS-level shared library event, do the same thing. */
4985 if (kind == TARGET_WAITKIND_LOADED)
4986 {
4987 print_solib_event (0);
4988 return PRINT_NOTHING;
4989 }
4990
4991 /* We reached the end of the chain, or we got a null BS to start
4992 with and nothing was printed. */
4993 return PRINT_UNKNOWN;
4994 }
4995
4996 /* Evaluate the expression EXP and return 1 if value is zero.
4997 This returns the inverse of the condition because it is called
4998 from catch_errors which returns 0 if an exception happened, and if an
4999 exception happens we want execution to stop.
5000 The argument is a "struct expression *" that has been cast to a
5001 "void *" to make it pass through catch_errors. */
5002
5003 static int
5004 breakpoint_cond_eval (void *exp)
5005 {
5006 struct value *mark = value_mark ();
5007 int i = !value_true (evaluate_expression ((struct expression *) exp));
5008
5009 value_free_to_mark (mark);
5010 return i;
5011 }
5012
5013 /* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
5014
5015 static bpstat
5016 bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
5017 {
5018 bpstat bs;
5019
5020 bs = (bpstat) xmalloc (sizeof (*bs));
5021 bs->next = NULL;
5022 **bs_link_pointer = bs;
5023 *bs_link_pointer = &bs->next;
5024 bs->breakpoint_at = bl->owner;
5025 bs->bp_location_at = bl;
5026 incref_bp_location (bl);
5027 /* If the condition is false, etc., don't do the commands. */
5028 bs->commands = NULL;
5029 bs->old_val = NULL;
5030 bs->print_it = print_it_normal;
5031 return bs;
5032 }
5033 \f
5034 /* The target has stopped with waitstatus WS. Check if any hardware
5035 watchpoints have triggered, according to the target. */
5036
5037 int
5038 watchpoints_triggered (struct target_waitstatus *ws)
5039 {
5040 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
5041 CORE_ADDR addr;
5042 struct breakpoint *b;
5043
5044 if (!stopped_by_watchpoint)
5045 {
5046 /* We were not stopped by a watchpoint. Mark all watchpoints
5047 as not triggered. */
5048 ALL_BREAKPOINTS (b)
5049 if (is_hardware_watchpoint (b))
5050 {
5051 struct watchpoint *w = (struct watchpoint *) b;
5052
5053 w->watchpoint_triggered = watch_triggered_no;
5054 }
5055
5056 return 0;
5057 }
5058
5059 if (!target_stopped_data_address (&current_target, &addr))
5060 {
5061 /* We were stopped by a watchpoint, but we don't know where.
5062 Mark all watchpoints as unknown. */
5063 ALL_BREAKPOINTS (b)
5064 if (is_hardware_watchpoint (b))
5065 {
5066 struct watchpoint *w = (struct watchpoint *) b;
5067
5068 w->watchpoint_triggered = watch_triggered_unknown;
5069 }
5070
5071 return 1;
5072 }
5073
5074 /* The target could report the data address. Mark watchpoints
5075 affected by this data address as triggered, and all others as not
5076 triggered. */
5077
5078 ALL_BREAKPOINTS (b)
5079 if (is_hardware_watchpoint (b))
5080 {
5081 struct watchpoint *w = (struct watchpoint *) b;
5082 struct bp_location *loc;
5083
5084 w->watchpoint_triggered = watch_triggered_no;
5085 for (loc = b->loc; loc; loc = loc->next)
5086 {
5087 if (is_masked_watchpoint (b))
5088 {
5089 CORE_ADDR newaddr = addr & w->hw_wp_mask;
5090 CORE_ADDR start = loc->address & w->hw_wp_mask;
5091
5092 if (newaddr == start)
5093 {
5094 w->watchpoint_triggered = watch_triggered_yes;
5095 break;
5096 }
5097 }
5098 /* Exact match not required. Within range is sufficient. */
5099 else if (target_watchpoint_addr_within_range (&current_target,
5100 addr, loc->address,
5101 loc->length))
5102 {
5103 w->watchpoint_triggered = watch_triggered_yes;
5104 break;
5105 }
5106 }
5107 }
5108
5109 return 1;
5110 }
5111
5112 /* Possible return values for watchpoint_check (this can't be an enum
5113 because of check_errors). */
5114 /* The watchpoint has been deleted. */
5115 #define WP_DELETED 1
5116 /* The value has changed. */
5117 #define WP_VALUE_CHANGED 2
5118 /* The value has not changed. */
5119 #define WP_VALUE_NOT_CHANGED 3
5120 /* Ignore this watchpoint, no matter if the value changed or not. */
5121 #define WP_IGNORE 4
5122
5123 #define BP_TEMPFLAG 1
5124 #define BP_HARDWAREFLAG 2
5125
5126 /* Evaluate watchpoint condition expression and check if its value
5127 changed.
5128
5129 P should be a pointer to struct bpstat, but is defined as a void *
5130 in order for this function to be usable with catch_errors. */
5131
5132 static int
5133 watchpoint_check (void *p)
5134 {
5135 bpstat bs = (bpstat) p;
5136 struct watchpoint *b;
5137 struct frame_info *fr;
5138 int within_current_scope;
5139
5140 /* BS is built from an existing struct breakpoint. */
5141 gdb_assert (bs->breakpoint_at != NULL);
5142 b = (struct watchpoint *) bs->breakpoint_at;
5143
5144 /* If this is a local watchpoint, we only want to check if the
5145 watchpoint frame is in scope if the current thread is the thread
5146 that was used to create the watchpoint. */
5147 if (!watchpoint_in_thread_scope (b))
5148 return WP_IGNORE;
5149
5150 if (b->exp_valid_block == NULL)
5151 within_current_scope = 1;
5152 else
5153 {
5154 struct frame_info *frame = get_current_frame ();
5155 struct gdbarch *frame_arch = get_frame_arch (frame);
5156 CORE_ADDR frame_pc = get_frame_pc (frame);
5157
5158 /* stack_frame_destroyed_p() returns a non-zero value if we're
5159 still in the function but the stack frame has already been
5160 invalidated. Since we can't rely on the values of local
5161 variables after the stack has been destroyed, we are treating
5162 the watchpoint in that state as `not changed' without further
5163 checking. Don't mark watchpoints as changed if the current
5164 frame is in an epilogue - even if they are in some other
5165 frame, our view of the stack is likely to be wrong and
5166 frame_find_by_id could error out. */
5167 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
5168 return WP_IGNORE;
5169
5170 fr = frame_find_by_id (b->watchpoint_frame);
5171 within_current_scope = (fr != NULL);
5172
5173 /* If we've gotten confused in the unwinder, we might have
5174 returned a frame that can't describe this variable. */
5175 if (within_current_scope)
5176 {
5177 struct symbol *function;
5178
5179 function = get_frame_function (fr);
5180 if (function == NULL
5181 || !contained_in (b->exp_valid_block,
5182 SYMBOL_BLOCK_VALUE (function)))
5183 within_current_scope = 0;
5184 }
5185
5186 if (within_current_scope)
5187 /* If we end up stopping, the current frame will get selected
5188 in normal_stop. So this call to select_frame won't affect
5189 the user. */
5190 select_frame (fr);
5191 }
5192
5193 if (within_current_scope)
5194 {
5195 /* We use value_{,free_to_}mark because it could be a *long*
5196 time before we return to the command level and call
5197 free_all_values. We can't call free_all_values because we
5198 might be in the middle of evaluating a function call. */
5199
5200 int pc = 0;
5201 struct value *mark;
5202 struct value *new_val;
5203
5204 if (is_masked_watchpoint (&b->base))
5205 /* Since we don't know the exact trigger address (from
5206 stopped_data_address), just tell the user we've triggered
5207 a mask watchpoint. */
5208 return WP_VALUE_CHANGED;
5209
5210 mark = value_mark ();
5211 fetch_subexp_value (b->exp.get (), &pc, &new_val, NULL, NULL, 0);
5212
5213 if (b->val_bitsize != 0)
5214 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5215
5216 /* We use value_equal_contents instead of value_equal because
5217 the latter coerces an array to a pointer, thus comparing just
5218 the address of the array instead of its contents. This is
5219 not what we want. */
5220 if ((b->val != NULL) != (new_val != NULL)
5221 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
5222 {
5223 if (new_val != NULL)
5224 {
5225 release_value (new_val);
5226 value_free_to_mark (mark);
5227 }
5228 bs->old_val = b->val;
5229 b->val = new_val;
5230 b->val_valid = 1;
5231 return WP_VALUE_CHANGED;
5232 }
5233 else
5234 {
5235 /* Nothing changed. */
5236 value_free_to_mark (mark);
5237 return WP_VALUE_NOT_CHANGED;
5238 }
5239 }
5240 else
5241 {
5242 /* This seems like the only logical thing to do because
5243 if we temporarily ignored the watchpoint, then when
5244 we reenter the block in which it is valid it contains
5245 garbage (in the case of a function, it may have two
5246 garbage values, one before and one after the prologue).
5247 So we can't even detect the first assignment to it and
5248 watch after that (since the garbage may or may not equal
5249 the first value assigned). */
5250 /* We print all the stop information in
5251 breakpoint_ops->print_it, but in this case, by the time we
5252 call breakpoint_ops->print_it this bp will be deleted
5253 already. So we have no choice but print the information
5254 here. */
5255
5256 SWITCH_THRU_ALL_UIS ()
5257 {
5258 struct ui_out *uiout = current_uiout;
5259
5260 if (ui_out_is_mi_like_p (uiout))
5261 ui_out_field_string
5262 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
5263 ui_out_text (uiout, "\nWatchpoint ");
5264 ui_out_field_int (uiout, "wpnum", b->base.number);
5265 ui_out_text (uiout,
5266 " deleted because the program has left the block in\n"
5267 "which its expression is valid.\n");
5268 }
5269
5270 /* Make sure the watchpoint's commands aren't executed. */
5271 decref_counted_command_line (&b->base.commands);
5272 watchpoint_del_at_next_stop (b);
5273
5274 return WP_DELETED;
5275 }
5276 }
5277
5278 /* Return true if it looks like target has stopped due to hitting
5279 breakpoint location BL. This function does not check if we should
5280 stop, only if BL explains the stop. */
5281
5282 static int
5283 bpstat_check_location (const struct bp_location *bl,
5284 struct address_space *aspace, CORE_ADDR bp_addr,
5285 const struct target_waitstatus *ws)
5286 {
5287 struct breakpoint *b = bl->owner;
5288
5289 /* BL is from an existing breakpoint. */
5290 gdb_assert (b != NULL);
5291
5292 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
5293 }
5294
5295 /* Determine if the watched values have actually changed, and we
5296 should stop. If not, set BS->stop to 0. */
5297
5298 static void
5299 bpstat_check_watchpoint (bpstat bs)
5300 {
5301 const struct bp_location *bl;
5302 struct watchpoint *b;
5303
5304 /* BS is built for existing struct breakpoint. */
5305 bl = bs->bp_location_at;
5306 gdb_assert (bl != NULL);
5307 b = (struct watchpoint *) bs->breakpoint_at;
5308 gdb_assert (b != NULL);
5309
5310 {
5311 int must_check_value = 0;
5312
5313 if (b->base.type == bp_watchpoint)
5314 /* For a software watchpoint, we must always check the
5315 watched value. */
5316 must_check_value = 1;
5317 else if (b->watchpoint_triggered == watch_triggered_yes)
5318 /* We have a hardware watchpoint (read, write, or access)
5319 and the target earlier reported an address watched by
5320 this watchpoint. */
5321 must_check_value = 1;
5322 else if (b->watchpoint_triggered == watch_triggered_unknown
5323 && b->base.type == bp_hardware_watchpoint)
5324 /* We were stopped by a hardware watchpoint, but the target could
5325 not report the data address. We must check the watchpoint's
5326 value. Access and read watchpoints are out of luck; without
5327 a data address, we can't figure it out. */
5328 must_check_value = 1;
5329
5330 if (must_check_value)
5331 {
5332 char *message
5333 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
5334 b->base.number);
5335 struct cleanup *cleanups = make_cleanup (xfree, message);
5336 int e = catch_errors (watchpoint_check, bs, message,
5337 RETURN_MASK_ALL);
5338 do_cleanups (cleanups);
5339 switch (e)
5340 {
5341 case WP_DELETED:
5342 /* We've already printed what needs to be printed. */
5343 bs->print_it = print_it_done;
5344 /* Stop. */
5345 break;
5346 case WP_IGNORE:
5347 bs->print_it = print_it_noop;
5348 bs->stop = 0;
5349 break;
5350 case WP_VALUE_CHANGED:
5351 if (b->base.type == bp_read_watchpoint)
5352 {
5353 /* There are two cases to consider here:
5354
5355 1. We're watching the triggered memory for reads.
5356 In that case, trust the target, and always report
5357 the watchpoint hit to the user. Even though
5358 reads don't cause value changes, the value may
5359 have changed since the last time it was read, and
5360 since we're not trapping writes, we will not see
5361 those, and as such we should ignore our notion of
5362 old value.
5363
5364 2. We're watching the triggered memory for both
5365 reads and writes. There are two ways this may
5366 happen:
5367
5368 2.1. This is a target that can't break on data
5369 reads only, but can break on accesses (reads or
5370 writes), such as e.g., x86. We detect this case
5371 at the time we try to insert read watchpoints.
5372
5373 2.2. Otherwise, the target supports read
5374 watchpoints, but, the user set an access or write
5375 watchpoint watching the same memory as this read
5376 watchpoint.
5377
5378 If we're watching memory writes as well as reads,
5379 ignore watchpoint hits when we find that the
5380 value hasn't changed, as reads don't cause
5381 changes. This still gives false positives when
5382 the program writes the same value to memory as
5383 what there was already in memory (we will confuse
5384 it for a read), but it's much better than
5385 nothing. */
5386
5387 int other_write_watchpoint = 0;
5388
5389 if (bl->watchpoint_type == hw_read)
5390 {
5391 struct breakpoint *other_b;
5392
5393 ALL_BREAKPOINTS (other_b)
5394 if (other_b->type == bp_hardware_watchpoint
5395 || other_b->type == bp_access_watchpoint)
5396 {
5397 struct watchpoint *other_w =
5398 (struct watchpoint *) other_b;
5399
5400 if (other_w->watchpoint_triggered
5401 == watch_triggered_yes)
5402 {
5403 other_write_watchpoint = 1;
5404 break;
5405 }
5406 }
5407 }
5408
5409 if (other_write_watchpoint
5410 || bl->watchpoint_type == hw_access)
5411 {
5412 /* We're watching the same memory for writes,
5413 and the value changed since the last time we
5414 updated it, so this trap must be for a write.
5415 Ignore it. */
5416 bs->print_it = print_it_noop;
5417 bs->stop = 0;
5418 }
5419 }
5420 break;
5421 case WP_VALUE_NOT_CHANGED:
5422 if (b->base.type == bp_hardware_watchpoint
5423 || b->base.type == bp_watchpoint)
5424 {
5425 /* Don't stop: write watchpoints shouldn't fire if
5426 the value hasn't changed. */
5427 bs->print_it = print_it_noop;
5428 bs->stop = 0;
5429 }
5430 /* Stop. */
5431 break;
5432 default:
5433 /* Can't happen. */
5434 case 0:
5435 /* Error from catch_errors. */
5436 {
5437 SWITCH_THRU_ALL_UIS ()
5438 {
5439 printf_filtered (_("Watchpoint %d deleted.\n"),
5440 b->base.number);
5441 }
5442 watchpoint_del_at_next_stop (b);
5443 /* We've already printed what needs to be printed. */
5444 bs->print_it = print_it_done;
5445 }
5446 break;
5447 }
5448 }
5449 else /* must_check_value == 0 */
5450 {
5451 /* This is a case where some watchpoint(s) triggered, but
5452 not at the address of this watchpoint, or else no
5453 watchpoint triggered after all. So don't print
5454 anything for this watchpoint. */
5455 bs->print_it = print_it_noop;
5456 bs->stop = 0;
5457 }
5458 }
5459 }
5460
5461 /* For breakpoints that are currently marked as telling gdb to stop,
5462 check conditions (condition proper, frame, thread and ignore count)
5463 of breakpoint referred to by BS. If we should not stop for this
5464 breakpoint, set BS->stop to 0. */
5465
5466 static void
5467 bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5468 {
5469 const struct bp_location *bl;
5470 struct breakpoint *b;
5471 int value_is_zero = 0;
5472 struct expression *cond;
5473
5474 gdb_assert (bs->stop);
5475
5476 /* BS is built for existing struct breakpoint. */
5477 bl = bs->bp_location_at;
5478 gdb_assert (bl != NULL);
5479 b = bs->breakpoint_at;
5480 gdb_assert (b != NULL);
5481
5482 /* Even if the target evaluated the condition on its end and notified GDB, we
5483 need to do so again since GDB does not know if we stopped due to a
5484 breakpoint or a single step breakpoint. */
5485
5486 if (frame_id_p (b->frame_id)
5487 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
5488 {
5489 bs->stop = 0;
5490 return;
5491 }
5492
5493 /* If this is a thread/task-specific breakpoint, don't waste cpu
5494 evaluating the condition if this isn't the specified
5495 thread/task. */
5496 if ((b->thread != -1 && b->thread != ptid_to_global_thread_id (ptid))
5497 || (b->task != 0 && b->task != ada_get_task_number (ptid)))
5498
5499 {
5500 bs->stop = 0;
5501 return;
5502 }
5503
5504 /* Evaluate extension language breakpoints that have a "stop" method
5505 implemented. */
5506 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
5507
5508 if (is_watchpoint (b))
5509 {
5510 struct watchpoint *w = (struct watchpoint *) b;
5511
5512 cond = w->cond_exp.get ();
5513 }
5514 else
5515 cond = bl->cond.get ();
5516
5517 if (cond && b->disposition != disp_del_at_next_stop)
5518 {
5519 int within_current_scope = 1;
5520 struct watchpoint * w;
5521
5522 /* We use value_mark and value_free_to_mark because it could
5523 be a long time before we return to the command level and
5524 call free_all_values. We can't call free_all_values
5525 because we might be in the middle of evaluating a
5526 function call. */
5527 struct value *mark = value_mark ();
5528
5529 if (is_watchpoint (b))
5530 w = (struct watchpoint *) b;
5531 else
5532 w = NULL;
5533
5534 /* Need to select the frame, with all that implies so that
5535 the conditions will have the right context. Because we
5536 use the frame, we will not see an inlined function's
5537 variables when we arrive at a breakpoint at the start
5538 of the inlined function; the current frame will be the
5539 call site. */
5540 if (w == NULL || w->cond_exp_valid_block == NULL)
5541 select_frame (get_current_frame ());
5542 else
5543 {
5544 struct frame_info *frame;
5545
5546 /* For local watchpoint expressions, which particular
5547 instance of a local is being watched matters, so we
5548 keep track of the frame to evaluate the expression
5549 in. To evaluate the condition however, it doesn't
5550 really matter which instantiation of the function
5551 where the condition makes sense triggers the
5552 watchpoint. This allows an expression like "watch
5553 global if q > 10" set in `func', catch writes to
5554 global on all threads that call `func', or catch
5555 writes on all recursive calls of `func' by a single
5556 thread. We simply always evaluate the condition in
5557 the innermost frame that's executing where it makes
5558 sense to evaluate the condition. It seems
5559 intuitive. */
5560 frame = block_innermost_frame (w->cond_exp_valid_block);
5561 if (frame != NULL)
5562 select_frame (frame);
5563 else
5564 within_current_scope = 0;
5565 }
5566 if (within_current_scope)
5567 value_is_zero
5568 = catch_errors (breakpoint_cond_eval, cond,
5569 "Error in testing breakpoint condition:\n",
5570 RETURN_MASK_ALL);
5571 else
5572 {
5573 warning (_("Watchpoint condition cannot be tested "
5574 "in the current scope"));
5575 /* If we failed to set the right context for this
5576 watchpoint, unconditionally report it. */
5577 value_is_zero = 0;
5578 }
5579 /* FIXME-someday, should give breakpoint #. */
5580 value_free_to_mark (mark);
5581 }
5582
5583 if (cond && value_is_zero)
5584 {
5585 bs->stop = 0;
5586 }
5587 else if (b->ignore_count > 0)
5588 {
5589 b->ignore_count--;
5590 bs->stop = 0;
5591 /* Increase the hit count even though we don't stop. */
5592 ++(b->hit_count);
5593 observer_notify_breakpoint_modified (b);
5594 }
5595 }
5596
5597 /* Returns true if we need to track moribund locations of LOC's type
5598 on the current target. */
5599
5600 static int
5601 need_moribund_for_location_type (struct bp_location *loc)
5602 {
5603 return ((loc->loc_type == bp_loc_software_breakpoint
5604 && !target_supports_stopped_by_sw_breakpoint ())
5605 || (loc->loc_type == bp_loc_hardware_breakpoint
5606 && !target_supports_stopped_by_hw_breakpoint ()));
5607 }
5608
5609
5610 /* Get a bpstat associated with having just stopped at address
5611 BP_ADDR in thread PTID.
5612
5613 Determine whether we stopped at a breakpoint, etc, or whether we
5614 don't understand this stop. Result is a chain of bpstat's such
5615 that:
5616
5617 if we don't understand the stop, the result is a null pointer.
5618
5619 if we understand why we stopped, the result is not null.
5620
5621 Each element of the chain refers to a particular breakpoint or
5622 watchpoint at which we have stopped. (We may have stopped for
5623 several reasons concurrently.)
5624
5625 Each element of the chain has valid next, breakpoint_at,
5626 commands, FIXME??? fields. */
5627
5628 bpstat
5629 bpstat_stop_status (struct address_space *aspace,
5630 CORE_ADDR bp_addr, ptid_t ptid,
5631 const struct target_waitstatus *ws)
5632 {
5633 struct breakpoint *b = NULL;
5634 struct bp_location *bl;
5635 struct bp_location *loc;
5636 /* First item of allocated bpstat's. */
5637 bpstat bs_head = NULL, *bs_link = &bs_head;
5638 /* Pointer to the last thing in the chain currently. */
5639 bpstat bs;
5640 int ix;
5641 int need_remove_insert;
5642 int removed_any;
5643
5644 /* First, build the bpstat chain with locations that explain a
5645 target stop, while being careful to not set the target running,
5646 as that may invalidate locations (in particular watchpoint
5647 locations are recreated). Resuming will happen here with
5648 breakpoint conditions or watchpoint expressions that include
5649 inferior function calls. */
5650
5651 ALL_BREAKPOINTS (b)
5652 {
5653 if (!breakpoint_enabled (b))
5654 continue;
5655
5656 for (bl = b->loc; bl != NULL; bl = bl->next)
5657 {
5658 /* For hardware watchpoints, we look only at the first
5659 location. The watchpoint_check function will work on the
5660 entire expression, not the individual locations. For
5661 read watchpoints, the watchpoints_triggered function has
5662 checked all locations already. */
5663 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5664 break;
5665
5666 if (!bl->enabled || bl->shlib_disabled)
5667 continue;
5668
5669 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
5670 continue;
5671
5672 /* Come here if it's a watchpoint, or if the break address
5673 matches. */
5674
5675 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
5676 explain stop. */
5677
5678 /* Assume we stop. Should we find a watchpoint that is not
5679 actually triggered, or if the condition of the breakpoint
5680 evaluates as false, we'll reset 'stop' to 0. */
5681 bs->stop = 1;
5682 bs->print = 1;
5683
5684 /* If this is a scope breakpoint, mark the associated
5685 watchpoint as triggered so that we will handle the
5686 out-of-scope event. We'll get to the watchpoint next
5687 iteration. */
5688 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
5689 {
5690 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5691
5692 w->watchpoint_triggered = watch_triggered_yes;
5693 }
5694 }
5695 }
5696
5697 /* Check if a moribund breakpoint explains the stop. */
5698 if (!target_supports_stopped_by_sw_breakpoint ()
5699 || !target_supports_stopped_by_hw_breakpoint ())
5700 {
5701 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
5702 {
5703 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5704 && need_moribund_for_location_type (loc))
5705 {
5706 bs = bpstat_alloc (loc, &bs_link);
5707 /* For hits of moribund locations, we should just proceed. */
5708 bs->stop = 0;
5709 bs->print = 0;
5710 bs->print_it = print_it_noop;
5711 }
5712 }
5713 }
5714
5715 /* A bit of special processing for shlib breakpoints. We need to
5716 process solib loading here, so that the lists of loaded and
5717 unloaded libraries are correct before we handle "catch load" and
5718 "catch unload". */
5719 for (bs = bs_head; bs != NULL; bs = bs->next)
5720 {
5721 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
5722 {
5723 handle_solib_event ();
5724 break;
5725 }
5726 }
5727
5728 /* Now go through the locations that caused the target to stop, and
5729 check whether we're interested in reporting this stop to higher
5730 layers, or whether we should resume the target transparently. */
5731
5732 removed_any = 0;
5733
5734 for (bs = bs_head; bs != NULL; bs = bs->next)
5735 {
5736 if (!bs->stop)
5737 continue;
5738
5739 b = bs->breakpoint_at;
5740 b->ops->check_status (bs);
5741 if (bs->stop)
5742 {
5743 bpstat_check_breakpoint_conditions (bs, ptid);
5744
5745 if (bs->stop)
5746 {
5747 ++(b->hit_count);
5748 observer_notify_breakpoint_modified (b);
5749
5750 /* We will stop here. */
5751 if (b->disposition == disp_disable)
5752 {
5753 --(b->enable_count);
5754 if (b->enable_count <= 0)
5755 b->enable_state = bp_disabled;
5756 removed_any = 1;
5757 }
5758 if (b->silent)
5759 bs->print = 0;
5760 bs->commands = b->commands;
5761 incref_counted_command_line (bs->commands);
5762 if (command_line_is_silent (bs->commands
5763 ? bs->commands->commands : NULL))
5764 bs->print = 0;
5765
5766 b->ops->after_condition_true (bs);
5767 }
5768
5769 }
5770
5771 /* Print nothing for this entry if we don't stop or don't
5772 print. */
5773 if (!bs->stop || !bs->print)
5774 bs->print_it = print_it_noop;
5775 }
5776
5777 /* If we aren't stopping, the value of some hardware watchpoint may
5778 not have changed, but the intermediate memory locations we are
5779 watching may have. Don't bother if we're stopping; this will get
5780 done later. */
5781 need_remove_insert = 0;
5782 if (! bpstat_causes_stop (bs_head))
5783 for (bs = bs_head; bs != NULL; bs = bs->next)
5784 if (!bs->stop
5785 && bs->breakpoint_at
5786 && is_hardware_watchpoint (bs->breakpoint_at))
5787 {
5788 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5789
5790 update_watchpoint (w, 0 /* don't reparse. */);
5791 need_remove_insert = 1;
5792 }
5793
5794 if (need_remove_insert)
5795 update_global_location_list (UGLL_MAY_INSERT);
5796 else if (removed_any)
5797 update_global_location_list (UGLL_DONT_INSERT);
5798
5799 return bs_head;
5800 }
5801
5802 static void
5803 handle_jit_event (void)
5804 {
5805 struct frame_info *frame;
5806 struct gdbarch *gdbarch;
5807
5808 if (debug_infrun)
5809 fprintf_unfiltered (gdb_stdlog, "handling bp_jit_event\n");
5810
5811 /* Switch terminal for any messages produced by
5812 breakpoint_re_set. */
5813 target_terminal_ours_for_output ();
5814
5815 frame = get_current_frame ();
5816 gdbarch = get_frame_arch (frame);
5817
5818 jit_event_handler (gdbarch);
5819
5820 target_terminal_inferior ();
5821 }
5822
5823 /* Prepare WHAT final decision for infrun. */
5824
5825 /* Decide what infrun needs to do with this bpstat. */
5826
5827 struct bpstat_what
5828 bpstat_what (bpstat bs_head)
5829 {
5830 struct bpstat_what retval;
5831 bpstat bs;
5832
5833 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
5834 retval.call_dummy = STOP_NONE;
5835 retval.is_longjmp = 0;
5836
5837 for (bs = bs_head; bs != NULL; bs = bs->next)
5838 {
5839 /* Extract this BS's action. After processing each BS, we check
5840 if its action overrides all we've seem so far. */
5841 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5842 enum bptype bptype;
5843
5844 if (bs->breakpoint_at == NULL)
5845 {
5846 /* I suspect this can happen if it was a momentary
5847 breakpoint which has since been deleted. */
5848 bptype = bp_none;
5849 }
5850 else
5851 bptype = bs->breakpoint_at->type;
5852
5853 switch (bptype)
5854 {
5855 case bp_none:
5856 break;
5857 case bp_breakpoint:
5858 case bp_hardware_breakpoint:
5859 case bp_single_step:
5860 case bp_until:
5861 case bp_finish:
5862 case bp_shlib_event:
5863 if (bs->stop)
5864 {
5865 if (bs->print)
5866 this_action = BPSTAT_WHAT_STOP_NOISY;
5867 else
5868 this_action = BPSTAT_WHAT_STOP_SILENT;
5869 }
5870 else
5871 this_action = BPSTAT_WHAT_SINGLE;
5872 break;
5873 case bp_watchpoint:
5874 case bp_hardware_watchpoint:
5875 case bp_read_watchpoint:
5876 case bp_access_watchpoint:
5877 if (bs->stop)
5878 {
5879 if (bs->print)
5880 this_action = BPSTAT_WHAT_STOP_NOISY;
5881 else
5882 this_action = BPSTAT_WHAT_STOP_SILENT;
5883 }
5884 else
5885 {
5886 /* There was a watchpoint, but we're not stopping.
5887 This requires no further action. */
5888 }
5889 break;
5890 case bp_longjmp:
5891 case bp_longjmp_call_dummy:
5892 case bp_exception:
5893 if (bs->stop)
5894 {
5895 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5896 retval.is_longjmp = bptype != bp_exception;
5897 }
5898 else
5899 this_action = BPSTAT_WHAT_SINGLE;
5900 break;
5901 case bp_longjmp_resume:
5902 case bp_exception_resume:
5903 if (bs->stop)
5904 {
5905 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5906 retval.is_longjmp = bptype == bp_longjmp_resume;
5907 }
5908 else
5909 this_action = BPSTAT_WHAT_SINGLE;
5910 break;
5911 case bp_step_resume:
5912 if (bs->stop)
5913 this_action = BPSTAT_WHAT_STEP_RESUME;
5914 else
5915 {
5916 /* It is for the wrong frame. */
5917 this_action = BPSTAT_WHAT_SINGLE;
5918 }
5919 break;
5920 case bp_hp_step_resume:
5921 if (bs->stop)
5922 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5923 else
5924 {
5925 /* It is for the wrong frame. */
5926 this_action = BPSTAT_WHAT_SINGLE;
5927 }
5928 break;
5929 case bp_watchpoint_scope:
5930 case bp_thread_event:
5931 case bp_overlay_event:
5932 case bp_longjmp_master:
5933 case bp_std_terminate_master:
5934 case bp_exception_master:
5935 this_action = BPSTAT_WHAT_SINGLE;
5936 break;
5937 case bp_catchpoint:
5938 if (bs->stop)
5939 {
5940 if (bs->print)
5941 this_action = BPSTAT_WHAT_STOP_NOISY;
5942 else
5943 this_action = BPSTAT_WHAT_STOP_SILENT;
5944 }
5945 else
5946 {
5947 /* There was a catchpoint, but we're not stopping.
5948 This requires no further action. */
5949 }
5950 break;
5951 case bp_jit_event:
5952 this_action = BPSTAT_WHAT_SINGLE;
5953 break;
5954 case bp_call_dummy:
5955 /* Make sure the action is stop (silent or noisy),
5956 so infrun.c pops the dummy frame. */
5957 retval.call_dummy = STOP_STACK_DUMMY;
5958 this_action = BPSTAT_WHAT_STOP_SILENT;
5959 break;
5960 case bp_std_terminate:
5961 /* Make sure the action is stop (silent or noisy),
5962 so infrun.c pops the dummy frame. */
5963 retval.call_dummy = STOP_STD_TERMINATE;
5964 this_action = BPSTAT_WHAT_STOP_SILENT;
5965 break;
5966 case bp_tracepoint:
5967 case bp_fast_tracepoint:
5968 case bp_static_tracepoint:
5969 /* Tracepoint hits should not be reported back to GDB, and
5970 if one got through somehow, it should have been filtered
5971 out already. */
5972 internal_error (__FILE__, __LINE__,
5973 _("bpstat_what: tracepoint encountered"));
5974 break;
5975 case bp_gnu_ifunc_resolver:
5976 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5977 this_action = BPSTAT_WHAT_SINGLE;
5978 break;
5979 case bp_gnu_ifunc_resolver_return:
5980 /* The breakpoint will be removed, execution will restart from the
5981 PC of the former breakpoint. */
5982 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5983 break;
5984
5985 case bp_dprintf:
5986 if (bs->stop)
5987 this_action = BPSTAT_WHAT_STOP_SILENT;
5988 else
5989 this_action = BPSTAT_WHAT_SINGLE;
5990 break;
5991
5992 default:
5993 internal_error (__FILE__, __LINE__,
5994 _("bpstat_what: unhandled bptype %d"), (int) bptype);
5995 }
5996
5997 retval.main_action = std::max (retval.main_action, this_action);
5998 }
5999
6000 return retval;
6001 }
6002
6003 void
6004 bpstat_run_callbacks (bpstat bs_head)
6005 {
6006 bpstat bs;
6007
6008 for (bs = bs_head; bs != NULL; bs = bs->next)
6009 {
6010 struct breakpoint *b = bs->breakpoint_at;
6011
6012 if (b == NULL)
6013 continue;
6014 switch (b->type)
6015 {
6016 case bp_jit_event:
6017 handle_jit_event ();
6018 break;
6019 case bp_gnu_ifunc_resolver:
6020 gnu_ifunc_resolver_stop (b);
6021 break;
6022 case bp_gnu_ifunc_resolver_return:
6023 gnu_ifunc_resolver_return_stop (b);
6024 break;
6025 }
6026 }
6027 }
6028
6029 /* Nonzero if we should step constantly (e.g. watchpoints on machines
6030 without hardware support). This isn't related to a specific bpstat,
6031 just to things like whether watchpoints are set. */
6032
6033 int
6034 bpstat_should_step (void)
6035 {
6036 struct breakpoint *b;
6037
6038 ALL_BREAKPOINTS (b)
6039 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
6040 return 1;
6041 return 0;
6042 }
6043
6044 int
6045 bpstat_causes_stop (bpstat bs)
6046 {
6047 for (; bs != NULL; bs = bs->next)
6048 if (bs->stop)
6049 return 1;
6050
6051 return 0;
6052 }
6053
6054 \f
6055
6056 /* Compute a string of spaces suitable to indent the next line
6057 so it starts at the position corresponding to the table column
6058 named COL_NAME in the currently active table of UIOUT. */
6059
6060 static char *
6061 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
6062 {
6063 static char wrap_indent[80];
6064 int i, total_width, width, align;
6065 const char *text;
6066
6067 total_width = 0;
6068 for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
6069 {
6070 if (strcmp (text, col_name) == 0)
6071 {
6072 gdb_assert (total_width < sizeof wrap_indent);
6073 memset (wrap_indent, ' ', total_width);
6074 wrap_indent[total_width] = 0;
6075
6076 return wrap_indent;
6077 }
6078
6079 total_width += width + 1;
6080 }
6081
6082 return NULL;
6083 }
6084
6085 /* Determine if the locations of this breakpoint will have their conditions
6086 evaluated by the target, host or a mix of both. Returns the following:
6087
6088 "host": Host evals condition.
6089 "host or target": Host or Target evals condition.
6090 "target": Target evals condition.
6091 */
6092
6093 static const char *
6094 bp_condition_evaluator (struct breakpoint *b)
6095 {
6096 struct bp_location *bl;
6097 char host_evals = 0;
6098 char target_evals = 0;
6099
6100 if (!b)
6101 return NULL;
6102
6103 if (!is_breakpoint (b))
6104 return NULL;
6105
6106 if (gdb_evaluates_breakpoint_condition_p ()
6107 || !target_supports_evaluation_of_breakpoint_conditions ())
6108 return condition_evaluation_host;
6109
6110 for (bl = b->loc; bl; bl = bl->next)
6111 {
6112 if (bl->cond_bytecode)
6113 target_evals++;
6114 else
6115 host_evals++;
6116 }
6117
6118 if (host_evals && target_evals)
6119 return condition_evaluation_both;
6120 else if (target_evals)
6121 return condition_evaluation_target;
6122 else
6123 return condition_evaluation_host;
6124 }
6125
6126 /* Determine the breakpoint location's condition evaluator. This is
6127 similar to bp_condition_evaluator, but for locations. */
6128
6129 static const char *
6130 bp_location_condition_evaluator (struct bp_location *bl)
6131 {
6132 if (bl && !is_breakpoint (bl->owner))
6133 return NULL;
6134
6135 if (gdb_evaluates_breakpoint_condition_p ()
6136 || !target_supports_evaluation_of_breakpoint_conditions ())
6137 return condition_evaluation_host;
6138
6139 if (bl && bl->cond_bytecode)
6140 return condition_evaluation_target;
6141 else
6142 return condition_evaluation_host;
6143 }
6144
6145 /* Print the LOC location out of the list of B->LOC locations. */
6146
6147 static void
6148 print_breakpoint_location (struct breakpoint *b,
6149 struct bp_location *loc)
6150 {
6151 struct ui_out *uiout = current_uiout;
6152 struct cleanup *old_chain = save_current_program_space ();
6153
6154 if (loc != NULL && loc->shlib_disabled)
6155 loc = NULL;
6156
6157 if (loc != NULL)
6158 set_current_program_space (loc->pspace);
6159
6160 if (b->display_canonical)
6161 ui_out_field_string (uiout, "what",
6162 event_location_to_string (b->location));
6163 else if (loc && loc->symtab)
6164 {
6165 struct symbol *sym
6166 = find_pc_sect_function (loc->address, loc->section);
6167 if (sym)
6168 {
6169 ui_out_text (uiout, "in ");
6170 ui_out_field_string (uiout, "func",
6171 SYMBOL_PRINT_NAME (sym));
6172 ui_out_text (uiout, " ");
6173 ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
6174 ui_out_text (uiout, "at ");
6175 }
6176 ui_out_field_string (uiout, "file",
6177 symtab_to_filename_for_display (loc->symtab));
6178 ui_out_text (uiout, ":");
6179
6180 if (ui_out_is_mi_like_p (uiout))
6181 ui_out_field_string (uiout, "fullname",
6182 symtab_to_fullname (loc->symtab));
6183
6184 ui_out_field_int (uiout, "line", loc->line_number);
6185 }
6186 else if (loc)
6187 {
6188 struct ui_file *stb = mem_fileopen ();
6189 struct cleanup *stb_chain = make_cleanup_ui_file_delete (stb);
6190
6191 print_address_symbolic (loc->gdbarch, loc->address, stb,
6192 demangle, "");
6193 ui_out_field_stream (uiout, "at", stb);
6194
6195 do_cleanups (stb_chain);
6196 }
6197 else
6198 {
6199 ui_out_field_string (uiout, "pending",
6200 event_location_to_string (b->location));
6201 /* If extra_string is available, it could be holding a condition
6202 or dprintf arguments. In either case, make sure it is printed,
6203 too, but only for non-MI streams. */
6204 if (!ui_out_is_mi_like_p (uiout) && b->extra_string != NULL)
6205 {
6206 if (b->type == bp_dprintf)
6207 ui_out_text (uiout, ",");
6208 else
6209 ui_out_text (uiout, " ");
6210 ui_out_text (uiout, b->extra_string);
6211 }
6212 }
6213
6214 if (loc && is_breakpoint (b)
6215 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6216 && bp_condition_evaluator (b) == condition_evaluation_both)
6217 {
6218 ui_out_text (uiout, " (");
6219 ui_out_field_string (uiout, "evaluated-by",
6220 bp_location_condition_evaluator (loc));
6221 ui_out_text (uiout, ")");
6222 }
6223
6224 do_cleanups (old_chain);
6225 }
6226
6227 static const char *
6228 bptype_string (enum bptype type)
6229 {
6230 struct ep_type_description
6231 {
6232 enum bptype type;
6233 char *description;
6234 };
6235 static struct ep_type_description bptypes[] =
6236 {
6237 {bp_none, "?deleted?"},
6238 {bp_breakpoint, "breakpoint"},
6239 {bp_hardware_breakpoint, "hw breakpoint"},
6240 {bp_single_step, "sw single-step"},
6241 {bp_until, "until"},
6242 {bp_finish, "finish"},
6243 {bp_watchpoint, "watchpoint"},
6244 {bp_hardware_watchpoint, "hw watchpoint"},
6245 {bp_read_watchpoint, "read watchpoint"},
6246 {bp_access_watchpoint, "acc watchpoint"},
6247 {bp_longjmp, "longjmp"},
6248 {bp_longjmp_resume, "longjmp resume"},
6249 {bp_longjmp_call_dummy, "longjmp for call dummy"},
6250 {bp_exception, "exception"},
6251 {bp_exception_resume, "exception resume"},
6252 {bp_step_resume, "step resume"},
6253 {bp_hp_step_resume, "high-priority step resume"},
6254 {bp_watchpoint_scope, "watchpoint scope"},
6255 {bp_call_dummy, "call dummy"},
6256 {bp_std_terminate, "std::terminate"},
6257 {bp_shlib_event, "shlib events"},
6258 {bp_thread_event, "thread events"},
6259 {bp_overlay_event, "overlay events"},
6260 {bp_longjmp_master, "longjmp master"},
6261 {bp_std_terminate_master, "std::terminate master"},
6262 {bp_exception_master, "exception master"},
6263 {bp_catchpoint, "catchpoint"},
6264 {bp_tracepoint, "tracepoint"},
6265 {bp_fast_tracepoint, "fast tracepoint"},
6266 {bp_static_tracepoint, "static tracepoint"},
6267 {bp_dprintf, "dprintf"},
6268 {bp_jit_event, "jit events"},
6269 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6270 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
6271 };
6272
6273 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6274 || ((int) type != bptypes[(int) type].type))
6275 internal_error (__FILE__, __LINE__,
6276 _("bptypes table does not describe type #%d."),
6277 (int) type);
6278
6279 return bptypes[(int) type].description;
6280 }
6281
6282 /* For MI, output a field named 'thread-groups' with a list as the value.
6283 For CLI, prefix the list with the string 'inf'. */
6284
6285 static void
6286 output_thread_groups (struct ui_out *uiout,
6287 const char *field_name,
6288 VEC(int) *inf_num,
6289 int mi_only)
6290 {
6291 struct cleanup *back_to;
6292 int is_mi = ui_out_is_mi_like_p (uiout);
6293 int inf;
6294 int i;
6295
6296 /* For backward compatibility, don't display inferiors in CLI unless
6297 there are several. Always display them for MI. */
6298 if (!is_mi && mi_only)
6299 return;
6300
6301 back_to = make_cleanup_ui_out_list_begin_end (uiout, field_name);
6302
6303 for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
6304 {
6305 if (is_mi)
6306 {
6307 char mi_group[10];
6308
6309 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
6310 ui_out_field_string (uiout, NULL, mi_group);
6311 }
6312 else
6313 {
6314 if (i == 0)
6315 ui_out_text (uiout, " inf ");
6316 else
6317 ui_out_text (uiout, ", ");
6318
6319 ui_out_text (uiout, plongest (inf));
6320 }
6321 }
6322
6323 do_cleanups (back_to);
6324 }
6325
6326 /* Print B to gdb_stdout. */
6327
6328 static void
6329 print_one_breakpoint_location (struct breakpoint *b,
6330 struct bp_location *loc,
6331 int loc_number,
6332 struct bp_location **last_loc,
6333 int allflag)
6334 {
6335 struct command_line *l;
6336 static char bpenables[] = "nynny";
6337
6338 struct ui_out *uiout = current_uiout;
6339 int header_of_multiple = 0;
6340 int part_of_multiple = (loc != NULL);
6341 struct value_print_options opts;
6342
6343 get_user_print_options (&opts);
6344
6345 gdb_assert (!loc || loc_number != 0);
6346 /* See comment in print_one_breakpoint concerning treatment of
6347 breakpoints with single disabled location. */
6348 if (loc == NULL
6349 && (b->loc != NULL
6350 && (b->loc->next != NULL || !b->loc->enabled)))
6351 header_of_multiple = 1;
6352 if (loc == NULL)
6353 loc = b->loc;
6354
6355 annotate_record ();
6356
6357 /* 1 */
6358 annotate_field (0);
6359 if (part_of_multiple)
6360 {
6361 char *formatted;
6362 formatted = xstrprintf ("%d.%d", b->number, loc_number);
6363 ui_out_field_string (uiout, "number", formatted);
6364 xfree (formatted);
6365 }
6366 else
6367 {
6368 ui_out_field_int (uiout, "number", b->number);
6369 }
6370
6371 /* 2 */
6372 annotate_field (1);
6373 if (part_of_multiple)
6374 ui_out_field_skip (uiout, "type");
6375 else
6376 ui_out_field_string (uiout, "type", bptype_string (b->type));
6377
6378 /* 3 */
6379 annotate_field (2);
6380 if (part_of_multiple)
6381 ui_out_field_skip (uiout, "disp");
6382 else
6383 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
6384
6385
6386 /* 4 */
6387 annotate_field (3);
6388 if (part_of_multiple)
6389 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
6390 else
6391 ui_out_field_fmt (uiout, "enabled", "%c",
6392 bpenables[(int) b->enable_state]);
6393 ui_out_spaces (uiout, 2);
6394
6395
6396 /* 5 and 6 */
6397 if (b->ops != NULL && b->ops->print_one != NULL)
6398 {
6399 /* Although the print_one can possibly print all locations,
6400 calling it here is not likely to get any nice result. So,
6401 make sure there's just one location. */
6402 gdb_assert (b->loc == NULL || b->loc->next == NULL);
6403 b->ops->print_one (b, last_loc);
6404 }
6405 else
6406 switch (b->type)
6407 {
6408 case bp_none:
6409 internal_error (__FILE__, __LINE__,
6410 _("print_one_breakpoint: bp_none encountered\n"));
6411 break;
6412
6413 case bp_watchpoint:
6414 case bp_hardware_watchpoint:
6415 case bp_read_watchpoint:
6416 case bp_access_watchpoint:
6417 {
6418 struct watchpoint *w = (struct watchpoint *) b;
6419
6420 /* Field 4, the address, is omitted (which makes the columns
6421 not line up too nicely with the headers, but the effect
6422 is relatively readable). */
6423 if (opts.addressprint)
6424 ui_out_field_skip (uiout, "addr");
6425 annotate_field (5);
6426 ui_out_field_string (uiout, "what", w->exp_string);
6427 }
6428 break;
6429
6430 case bp_breakpoint:
6431 case bp_hardware_breakpoint:
6432 case bp_single_step:
6433 case bp_until:
6434 case bp_finish:
6435 case bp_longjmp:
6436 case bp_longjmp_resume:
6437 case bp_longjmp_call_dummy:
6438 case bp_exception:
6439 case bp_exception_resume:
6440 case bp_step_resume:
6441 case bp_hp_step_resume:
6442 case bp_watchpoint_scope:
6443 case bp_call_dummy:
6444 case bp_std_terminate:
6445 case bp_shlib_event:
6446 case bp_thread_event:
6447 case bp_overlay_event:
6448 case bp_longjmp_master:
6449 case bp_std_terminate_master:
6450 case bp_exception_master:
6451 case bp_tracepoint:
6452 case bp_fast_tracepoint:
6453 case bp_static_tracepoint:
6454 case bp_dprintf:
6455 case bp_jit_event:
6456 case bp_gnu_ifunc_resolver:
6457 case bp_gnu_ifunc_resolver_return:
6458 if (opts.addressprint)
6459 {
6460 annotate_field (4);
6461 if (header_of_multiple)
6462 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
6463 else if (b->loc == NULL || loc->shlib_disabled)
6464 ui_out_field_string (uiout, "addr", "<PENDING>");
6465 else
6466 ui_out_field_core_addr (uiout, "addr",
6467 loc->gdbarch, loc->address);
6468 }
6469 annotate_field (5);
6470 if (!header_of_multiple)
6471 print_breakpoint_location (b, loc);
6472 if (b->loc)
6473 *last_loc = b->loc;
6474 break;
6475 }
6476
6477
6478 if (loc != NULL && !header_of_multiple)
6479 {
6480 struct inferior *inf;
6481 VEC(int) *inf_num = NULL;
6482 int mi_only = 1;
6483
6484 ALL_INFERIORS (inf)
6485 {
6486 if (inf->pspace == loc->pspace)
6487 VEC_safe_push (int, inf_num, inf->num);
6488 }
6489
6490 /* For backward compatibility, don't display inferiors in CLI unless
6491 there are several. Always display for MI. */
6492 if (allflag
6493 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6494 && (number_of_program_spaces () > 1
6495 || number_of_inferiors () > 1)
6496 /* LOC is for existing B, it cannot be in
6497 moribund_locations and thus having NULL OWNER. */
6498 && loc->owner->type != bp_catchpoint))
6499 mi_only = 0;
6500 output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6501 VEC_free (int, inf_num);
6502 }
6503
6504 if (!part_of_multiple)
6505 {
6506 if (b->thread != -1)
6507 {
6508 /* FIXME: This seems to be redundant and lost here; see the
6509 "stop only in" line a little further down. */
6510 ui_out_text (uiout, " thread ");
6511 ui_out_field_int (uiout, "thread", b->thread);
6512 }
6513 else if (b->task != 0)
6514 {
6515 ui_out_text (uiout, " task ");
6516 ui_out_field_int (uiout, "task", b->task);
6517 }
6518 }
6519
6520 ui_out_text (uiout, "\n");
6521
6522 if (!part_of_multiple)
6523 b->ops->print_one_detail (b, uiout);
6524
6525 if (part_of_multiple && frame_id_p (b->frame_id))
6526 {
6527 annotate_field (6);
6528 ui_out_text (uiout, "\tstop only in stack frame at ");
6529 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
6530 the frame ID. */
6531 ui_out_field_core_addr (uiout, "frame",
6532 b->gdbarch, b->frame_id.stack_addr);
6533 ui_out_text (uiout, "\n");
6534 }
6535
6536 if (!part_of_multiple && b->cond_string)
6537 {
6538 annotate_field (7);
6539 if (is_tracepoint (b))
6540 ui_out_text (uiout, "\ttrace only if ");
6541 else
6542 ui_out_text (uiout, "\tstop only if ");
6543 ui_out_field_string (uiout, "cond", b->cond_string);
6544
6545 /* Print whether the target is doing the breakpoint's condition
6546 evaluation. If GDB is doing the evaluation, don't print anything. */
6547 if (is_breakpoint (b)
6548 && breakpoint_condition_evaluation_mode ()
6549 == condition_evaluation_target)
6550 {
6551 ui_out_text (uiout, " (");
6552 ui_out_field_string (uiout, "evaluated-by",
6553 bp_condition_evaluator (b));
6554 ui_out_text (uiout, " evals)");
6555 }
6556 ui_out_text (uiout, "\n");
6557 }
6558
6559 if (!part_of_multiple && b->thread != -1)
6560 {
6561 /* FIXME should make an annotation for this. */
6562 ui_out_text (uiout, "\tstop only in thread ");
6563 if (ui_out_is_mi_like_p (uiout))
6564 ui_out_field_int (uiout, "thread", b->thread);
6565 else
6566 {
6567 struct thread_info *thr = find_thread_global_id (b->thread);
6568
6569 ui_out_field_string (uiout, "thread", print_thread_id (thr));
6570 }
6571 ui_out_text (uiout, "\n");
6572 }
6573
6574 if (!part_of_multiple)
6575 {
6576 if (b->hit_count)
6577 {
6578 /* FIXME should make an annotation for this. */
6579 if (is_catchpoint (b))
6580 ui_out_text (uiout, "\tcatchpoint");
6581 else if (is_tracepoint (b))
6582 ui_out_text (uiout, "\ttracepoint");
6583 else
6584 ui_out_text (uiout, "\tbreakpoint");
6585 ui_out_text (uiout, " already hit ");
6586 ui_out_field_int (uiout, "times", b->hit_count);
6587 if (b->hit_count == 1)
6588 ui_out_text (uiout, " time\n");
6589 else
6590 ui_out_text (uiout, " times\n");
6591 }
6592 else
6593 {
6594 /* Output the count also if it is zero, but only if this is mi. */
6595 if (ui_out_is_mi_like_p (uiout))
6596 ui_out_field_int (uiout, "times", b->hit_count);
6597 }
6598 }
6599
6600 if (!part_of_multiple && b->ignore_count)
6601 {
6602 annotate_field (8);
6603 ui_out_text (uiout, "\tignore next ");
6604 ui_out_field_int (uiout, "ignore", b->ignore_count);
6605 ui_out_text (uiout, " hits\n");
6606 }
6607
6608 /* Note that an enable count of 1 corresponds to "enable once"
6609 behavior, which is reported by the combination of enablement and
6610 disposition, so we don't need to mention it here. */
6611 if (!part_of_multiple && b->enable_count > 1)
6612 {
6613 annotate_field (8);
6614 ui_out_text (uiout, "\tdisable after ");
6615 /* Tweak the wording to clarify that ignore and enable counts
6616 are distinct, and have additive effect. */
6617 if (b->ignore_count)
6618 ui_out_text (uiout, "additional ");
6619 else
6620 ui_out_text (uiout, "next ");
6621 ui_out_field_int (uiout, "enable", b->enable_count);
6622 ui_out_text (uiout, " hits\n");
6623 }
6624
6625 if (!part_of_multiple && is_tracepoint (b))
6626 {
6627 struct tracepoint *tp = (struct tracepoint *) b;
6628
6629 if (tp->traceframe_usage)
6630 {
6631 ui_out_text (uiout, "\ttrace buffer usage ");
6632 ui_out_field_int (uiout, "traceframe-usage", tp->traceframe_usage);
6633 ui_out_text (uiout, " bytes\n");
6634 }
6635 }
6636
6637 l = b->commands ? b->commands->commands : NULL;
6638 if (!part_of_multiple && l)
6639 {
6640 struct cleanup *script_chain;
6641
6642 annotate_field (9);
6643 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
6644 print_command_lines (uiout, l, 4);
6645 do_cleanups (script_chain);
6646 }
6647
6648 if (is_tracepoint (b))
6649 {
6650 struct tracepoint *t = (struct tracepoint *) b;
6651
6652 if (!part_of_multiple && t->pass_count)
6653 {
6654 annotate_field (10);
6655 ui_out_text (uiout, "\tpass count ");
6656 ui_out_field_int (uiout, "pass", t->pass_count);
6657 ui_out_text (uiout, " \n");
6658 }
6659
6660 /* Don't display it when tracepoint or tracepoint location is
6661 pending. */
6662 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6663 {
6664 annotate_field (11);
6665
6666 if (ui_out_is_mi_like_p (uiout))
6667 ui_out_field_string (uiout, "installed",
6668 loc->inserted ? "y" : "n");
6669 else
6670 {
6671 if (loc->inserted)
6672 ui_out_text (uiout, "\t");
6673 else
6674 ui_out_text (uiout, "\tnot ");
6675 ui_out_text (uiout, "installed on target\n");
6676 }
6677 }
6678 }
6679
6680 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
6681 {
6682 if (is_watchpoint (b))
6683 {
6684 struct watchpoint *w = (struct watchpoint *) b;
6685
6686 ui_out_field_string (uiout, "original-location", w->exp_string);
6687 }
6688 else if (b->location != NULL
6689 && event_location_to_string (b->location) != NULL)
6690 ui_out_field_string (uiout, "original-location",
6691 event_location_to_string (b->location));
6692 }
6693 }
6694
6695 static void
6696 print_one_breakpoint (struct breakpoint *b,
6697 struct bp_location **last_loc,
6698 int allflag)
6699 {
6700 struct cleanup *bkpt_chain;
6701 struct ui_out *uiout = current_uiout;
6702
6703 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
6704
6705 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
6706 do_cleanups (bkpt_chain);
6707
6708 /* If this breakpoint has custom print function,
6709 it's already printed. Otherwise, print individual
6710 locations, if any. */
6711 if (b->ops == NULL || b->ops->print_one == NULL)
6712 {
6713 /* If breakpoint has a single location that is disabled, we
6714 print it as if it had several locations, since otherwise it's
6715 hard to represent "breakpoint enabled, location disabled"
6716 situation.
6717
6718 Note that while hardware watchpoints have several locations
6719 internally, that's not a property exposed to user. */
6720 if (b->loc
6721 && !is_hardware_watchpoint (b)
6722 && (b->loc->next || !b->loc->enabled))
6723 {
6724 struct bp_location *loc;
6725 int n = 1;
6726
6727 for (loc = b->loc; loc; loc = loc->next, ++n)
6728 {
6729 struct cleanup *inner2 =
6730 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
6731 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
6732 do_cleanups (inner2);
6733 }
6734 }
6735 }
6736 }
6737
6738 static int
6739 breakpoint_address_bits (struct breakpoint *b)
6740 {
6741 int print_address_bits = 0;
6742 struct bp_location *loc;
6743
6744 /* Software watchpoints that aren't watching memory don't have an
6745 address to print. */
6746 if (is_no_memory_software_watchpoint (b))
6747 return 0;
6748
6749 for (loc = b->loc; loc; loc = loc->next)
6750 {
6751 int addr_bit;
6752
6753 addr_bit = gdbarch_addr_bit (loc->gdbarch);
6754 if (addr_bit > print_address_bits)
6755 print_address_bits = addr_bit;
6756 }
6757
6758 return print_address_bits;
6759 }
6760
6761 struct captured_breakpoint_query_args
6762 {
6763 int bnum;
6764 };
6765
6766 static int
6767 do_captured_breakpoint_query (struct ui_out *uiout, void *data)
6768 {
6769 struct captured_breakpoint_query_args *args
6770 = (struct captured_breakpoint_query_args *) data;
6771 struct breakpoint *b;
6772 struct bp_location *dummy_loc = NULL;
6773
6774 ALL_BREAKPOINTS (b)
6775 {
6776 if (args->bnum == b->number)
6777 {
6778 print_one_breakpoint (b, &dummy_loc, 0);
6779 return GDB_RC_OK;
6780 }
6781 }
6782 return GDB_RC_NONE;
6783 }
6784
6785 enum gdb_rc
6786 gdb_breakpoint_query (struct ui_out *uiout, int bnum,
6787 char **error_message)
6788 {
6789 struct captured_breakpoint_query_args args;
6790
6791 args.bnum = bnum;
6792 /* For the moment we don't trust print_one_breakpoint() to not throw
6793 an error. */
6794 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
6795 error_message, RETURN_MASK_ALL) < 0)
6796 return GDB_RC_FAIL;
6797 else
6798 return GDB_RC_OK;
6799 }
6800
6801 /* Return true if this breakpoint was set by the user, false if it is
6802 internal or momentary. */
6803
6804 int
6805 user_breakpoint_p (struct breakpoint *b)
6806 {
6807 return b->number > 0;
6808 }
6809
6810 /* See breakpoint.h. */
6811
6812 int
6813 pending_breakpoint_p (struct breakpoint *b)
6814 {
6815 return b->loc == NULL;
6816 }
6817
6818 /* Print information on user settable breakpoint (watchpoint, etc)
6819 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6820 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6821 FILTER is non-NULL, call it on each breakpoint and only include the
6822 ones for which it returns non-zero. Return the total number of
6823 breakpoints listed. */
6824
6825 static int
6826 breakpoint_1 (char *args, int allflag,
6827 int (*filter) (const struct breakpoint *))
6828 {
6829 struct breakpoint *b;
6830 struct bp_location *last_loc = NULL;
6831 int nr_printable_breakpoints;
6832 struct cleanup *bkpttbl_chain;
6833 struct value_print_options opts;
6834 int print_address_bits = 0;
6835 int print_type_col_width = 14;
6836 struct ui_out *uiout = current_uiout;
6837
6838 get_user_print_options (&opts);
6839
6840 /* Compute the number of rows in the table, as well as the size
6841 required for address fields. */
6842 nr_printable_breakpoints = 0;
6843 ALL_BREAKPOINTS (b)
6844 {
6845 /* If we have a filter, only list the breakpoints it accepts. */
6846 if (filter && !filter (b))
6847 continue;
6848
6849 /* If we have an "args" string, it is a list of breakpoints to
6850 accept. Skip the others. */
6851 if (args != NULL && *args != '\0')
6852 {
6853 if (allflag && parse_and_eval_long (args) != b->number)
6854 continue;
6855 if (!allflag && !number_is_in_list (args, b->number))
6856 continue;
6857 }
6858
6859 if (allflag || user_breakpoint_p (b))
6860 {
6861 int addr_bit, type_len;
6862
6863 addr_bit = breakpoint_address_bits (b);
6864 if (addr_bit > print_address_bits)
6865 print_address_bits = addr_bit;
6866
6867 type_len = strlen (bptype_string (b->type));
6868 if (type_len > print_type_col_width)
6869 print_type_col_width = type_len;
6870
6871 nr_printable_breakpoints++;
6872 }
6873 }
6874
6875 if (opts.addressprint)
6876 bkpttbl_chain
6877 = make_cleanup_ui_out_table_begin_end (uiout, 6,
6878 nr_printable_breakpoints,
6879 "BreakpointTable");
6880 else
6881 bkpttbl_chain
6882 = make_cleanup_ui_out_table_begin_end (uiout, 5,
6883 nr_printable_breakpoints,
6884 "BreakpointTable");
6885
6886 if (nr_printable_breakpoints > 0)
6887 annotate_breakpoints_headers ();
6888 if (nr_printable_breakpoints > 0)
6889 annotate_field (0);
6890 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
6891 if (nr_printable_breakpoints > 0)
6892 annotate_field (1);
6893 ui_out_table_header (uiout, print_type_col_width, ui_left,
6894 "type", "Type"); /* 2 */
6895 if (nr_printable_breakpoints > 0)
6896 annotate_field (2);
6897 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
6898 if (nr_printable_breakpoints > 0)
6899 annotate_field (3);
6900 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
6901 if (opts.addressprint)
6902 {
6903 if (nr_printable_breakpoints > 0)
6904 annotate_field (4);
6905 if (print_address_bits <= 32)
6906 ui_out_table_header (uiout, 10, ui_left,
6907 "addr", "Address"); /* 5 */
6908 else
6909 ui_out_table_header (uiout, 18, ui_left,
6910 "addr", "Address"); /* 5 */
6911 }
6912 if (nr_printable_breakpoints > 0)
6913 annotate_field (5);
6914 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
6915 ui_out_table_body (uiout);
6916 if (nr_printable_breakpoints > 0)
6917 annotate_breakpoints_table ();
6918
6919 ALL_BREAKPOINTS (b)
6920 {
6921 QUIT;
6922 /* If we have a filter, only list the breakpoints it accepts. */
6923 if (filter && !filter (b))
6924 continue;
6925
6926 /* If we have an "args" string, it is a list of breakpoints to
6927 accept. Skip the others. */
6928
6929 if (args != NULL && *args != '\0')
6930 {
6931 if (allflag) /* maintenance info breakpoint */
6932 {
6933 if (parse_and_eval_long (args) != b->number)
6934 continue;
6935 }
6936 else /* all others */
6937 {
6938 if (!number_is_in_list (args, b->number))
6939 continue;
6940 }
6941 }
6942 /* We only print out user settable breakpoints unless the
6943 allflag is set. */
6944 if (allflag || user_breakpoint_p (b))
6945 print_one_breakpoint (b, &last_loc, allflag);
6946 }
6947
6948 do_cleanups (bkpttbl_chain);
6949
6950 if (nr_printable_breakpoints == 0)
6951 {
6952 /* If there's a filter, let the caller decide how to report
6953 empty list. */
6954 if (!filter)
6955 {
6956 if (args == NULL || *args == '\0')
6957 ui_out_message (uiout, "No breakpoints or watchpoints.\n");
6958 else
6959 ui_out_message (uiout,
6960 "No breakpoint or watchpoint matching '%s'.\n",
6961 args);
6962 }
6963 }
6964 else
6965 {
6966 if (last_loc && !server_command)
6967 set_next_address (last_loc->gdbarch, last_loc->address);
6968 }
6969
6970 /* FIXME? Should this be moved up so that it is only called when
6971 there have been breakpoints? */
6972 annotate_breakpoints_table_end ();
6973
6974 return nr_printable_breakpoints;
6975 }
6976
6977 /* Display the value of default-collect in a way that is generally
6978 compatible with the breakpoint list. */
6979
6980 static void
6981 default_collect_info (void)
6982 {
6983 struct ui_out *uiout = current_uiout;
6984
6985 /* If it has no value (which is frequently the case), say nothing; a
6986 message like "No default-collect." gets in user's face when it's
6987 not wanted. */
6988 if (!*default_collect)
6989 return;
6990
6991 /* The following phrase lines up nicely with per-tracepoint collect
6992 actions. */
6993 ui_out_text (uiout, "default collect ");
6994 ui_out_field_string (uiout, "default-collect", default_collect);
6995 ui_out_text (uiout, " \n");
6996 }
6997
6998 static void
6999 breakpoints_info (char *args, int from_tty)
7000 {
7001 breakpoint_1 (args, 0, NULL);
7002
7003 default_collect_info ();
7004 }
7005
7006 static void
7007 watchpoints_info (char *args, int from_tty)
7008 {
7009 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
7010 struct ui_out *uiout = current_uiout;
7011
7012 if (num_printed == 0)
7013 {
7014 if (args == NULL || *args == '\0')
7015 ui_out_message (uiout, "No watchpoints.\n");
7016 else
7017 ui_out_message (uiout, "No watchpoint matching '%s'.\n", args);
7018 }
7019 }
7020
7021 static void
7022 maintenance_info_breakpoints (char *args, int from_tty)
7023 {
7024 breakpoint_1 (args, 1, NULL);
7025
7026 default_collect_info ();
7027 }
7028
7029 static int
7030 breakpoint_has_pc (struct breakpoint *b,
7031 struct program_space *pspace,
7032 CORE_ADDR pc, struct obj_section *section)
7033 {
7034 struct bp_location *bl = b->loc;
7035
7036 for (; bl; bl = bl->next)
7037 {
7038 if (bl->pspace == pspace
7039 && bl->address == pc
7040 && (!overlay_debugging || bl->section == section))
7041 return 1;
7042 }
7043 return 0;
7044 }
7045
7046 /* Print a message describing any user-breakpoints set at PC. This
7047 concerns with logical breakpoints, so we match program spaces, not
7048 address spaces. */
7049
7050 static void
7051 describe_other_breakpoints (struct gdbarch *gdbarch,
7052 struct program_space *pspace, CORE_ADDR pc,
7053 struct obj_section *section, int thread)
7054 {
7055 int others = 0;
7056 struct breakpoint *b;
7057
7058 ALL_BREAKPOINTS (b)
7059 others += (user_breakpoint_p (b)
7060 && breakpoint_has_pc (b, pspace, pc, section));
7061 if (others > 0)
7062 {
7063 if (others == 1)
7064 printf_filtered (_("Note: breakpoint "));
7065 else /* if (others == ???) */
7066 printf_filtered (_("Note: breakpoints "));
7067 ALL_BREAKPOINTS (b)
7068 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
7069 {
7070 others--;
7071 printf_filtered ("%d", b->number);
7072 if (b->thread == -1 && thread != -1)
7073 printf_filtered (" (all threads)");
7074 else if (b->thread != -1)
7075 printf_filtered (" (thread %d)", b->thread);
7076 printf_filtered ("%s%s ",
7077 ((b->enable_state == bp_disabled
7078 || b->enable_state == bp_call_disabled)
7079 ? " (disabled)"
7080 : ""),
7081 (others > 1) ? ","
7082 : ((others == 1) ? " and" : ""));
7083 }
7084 printf_filtered (_("also set at pc "));
7085 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
7086 printf_filtered (".\n");
7087 }
7088 }
7089 \f
7090
7091 /* Return true iff it is meaningful to use the address member of
7092 BPT locations. For some breakpoint types, the locations' address members
7093 are irrelevant and it makes no sense to attempt to compare them to other
7094 addresses (or use them for any other purpose either).
7095
7096 More specifically, each of the following breakpoint types will
7097 always have a zero valued location address and we don't want to mark
7098 breakpoints of any of these types to be a duplicate of an actual
7099 breakpoint location at address zero:
7100
7101 bp_watchpoint
7102 bp_catchpoint
7103
7104 */
7105
7106 static int
7107 breakpoint_address_is_meaningful (struct breakpoint *bpt)
7108 {
7109 enum bptype type = bpt->type;
7110
7111 return (type != bp_watchpoint && type != bp_catchpoint);
7112 }
7113
7114 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
7115 true if LOC1 and LOC2 represent the same watchpoint location. */
7116
7117 static int
7118 watchpoint_locations_match (struct bp_location *loc1,
7119 struct bp_location *loc2)
7120 {
7121 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
7122 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
7123
7124 /* Both of them must exist. */
7125 gdb_assert (w1 != NULL);
7126 gdb_assert (w2 != NULL);
7127
7128 /* If the target can evaluate the condition expression in hardware,
7129 then we we need to insert both watchpoints even if they are at
7130 the same place. Otherwise the watchpoint will only trigger when
7131 the condition of whichever watchpoint was inserted evaluates to
7132 true, not giving a chance for GDB to check the condition of the
7133 other watchpoint. */
7134 if ((w1->cond_exp
7135 && target_can_accel_watchpoint_condition (loc1->address,
7136 loc1->length,
7137 loc1->watchpoint_type,
7138 w1->cond_exp.get ()))
7139 || (w2->cond_exp
7140 && target_can_accel_watchpoint_condition (loc2->address,
7141 loc2->length,
7142 loc2->watchpoint_type,
7143 w2->cond_exp.get ())))
7144 return 0;
7145
7146 /* Note that this checks the owner's type, not the location's. In
7147 case the target does not support read watchpoints, but does
7148 support access watchpoints, we'll have bp_read_watchpoint
7149 watchpoints with hw_access locations. Those should be considered
7150 duplicates of hw_read locations. The hw_read locations will
7151 become hw_access locations later. */
7152 return (loc1->owner->type == loc2->owner->type
7153 && loc1->pspace->aspace == loc2->pspace->aspace
7154 && loc1->address == loc2->address
7155 && loc1->length == loc2->length);
7156 }
7157
7158 /* See breakpoint.h. */
7159
7160 int
7161 breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
7162 struct address_space *aspace2, CORE_ADDR addr2)
7163 {
7164 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
7165 || aspace1 == aspace2)
7166 && addr1 == addr2);
7167 }
7168
7169 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
7170 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
7171 matches ASPACE2. On targets that have global breakpoints, the address
7172 space doesn't really matter. */
7173
7174 static int
7175 breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
7176 int len1, struct address_space *aspace2,
7177 CORE_ADDR addr2)
7178 {
7179 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
7180 || aspace1 == aspace2)
7181 && addr2 >= addr1 && addr2 < addr1 + len1);
7182 }
7183
7184 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
7185 a ranged breakpoint. In most targets, a match happens only if ASPACE
7186 matches the breakpoint's address space. On targets that have global
7187 breakpoints, the address space doesn't really matter. */
7188
7189 static int
7190 breakpoint_location_address_match (struct bp_location *bl,
7191 struct address_space *aspace,
7192 CORE_ADDR addr)
7193 {
7194 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
7195 aspace, addr)
7196 || (bl->length
7197 && breakpoint_address_match_range (bl->pspace->aspace,
7198 bl->address, bl->length,
7199 aspace, addr)));
7200 }
7201
7202 /* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
7203 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
7204 match happens only if ASPACE matches the breakpoint's address
7205 space. On targets that have global breakpoints, the address space
7206 doesn't really matter. */
7207
7208 static int
7209 breakpoint_location_address_range_overlap (struct bp_location *bl,
7210 struct address_space *aspace,
7211 CORE_ADDR addr, int len)
7212 {
7213 if (gdbarch_has_global_breakpoints (target_gdbarch ())
7214 || bl->pspace->aspace == aspace)
7215 {
7216 int bl_len = bl->length != 0 ? bl->length : 1;
7217
7218 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
7219 return 1;
7220 }
7221 return 0;
7222 }
7223
7224 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
7225 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
7226 true, otherwise returns false. */
7227
7228 static int
7229 tracepoint_locations_match (struct bp_location *loc1,
7230 struct bp_location *loc2)
7231 {
7232 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
7233 /* Since tracepoint locations are never duplicated with others', tracepoint
7234 locations at the same address of different tracepoints are regarded as
7235 different locations. */
7236 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7237 else
7238 return 0;
7239 }
7240
7241 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
7242 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
7243 represent the same location. */
7244
7245 static int
7246 breakpoint_locations_match (struct bp_location *loc1,
7247 struct bp_location *loc2)
7248 {
7249 int hw_point1, hw_point2;
7250
7251 /* Both of them must not be in moribund_locations. */
7252 gdb_assert (loc1->owner != NULL);
7253 gdb_assert (loc2->owner != NULL);
7254
7255 hw_point1 = is_hardware_watchpoint (loc1->owner);
7256 hw_point2 = is_hardware_watchpoint (loc2->owner);
7257
7258 if (hw_point1 != hw_point2)
7259 return 0;
7260 else if (hw_point1)
7261 return watchpoint_locations_match (loc1, loc2);
7262 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7263 return tracepoint_locations_match (loc1, loc2);
7264 else
7265 /* We compare bp_location.length in order to cover ranged breakpoints. */
7266 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7267 loc2->pspace->aspace, loc2->address)
7268 && loc1->length == loc2->length);
7269 }
7270
7271 static void
7272 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7273 int bnum, int have_bnum)
7274 {
7275 /* The longest string possibly returned by hex_string_custom
7276 is 50 chars. These must be at least that big for safety. */
7277 char astr1[64];
7278 char astr2[64];
7279
7280 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7281 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
7282 if (have_bnum)
7283 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
7284 bnum, astr1, astr2);
7285 else
7286 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
7287 }
7288
7289 /* Adjust a breakpoint's address to account for architectural
7290 constraints on breakpoint placement. Return the adjusted address.
7291 Note: Very few targets require this kind of adjustment. For most
7292 targets, this function is simply the identity function. */
7293
7294 static CORE_ADDR
7295 adjust_breakpoint_address (struct gdbarch *gdbarch,
7296 CORE_ADDR bpaddr, enum bptype bptype)
7297 {
7298 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
7299 {
7300 /* Very few targets need any kind of breakpoint adjustment. */
7301 return bpaddr;
7302 }
7303 else if (bptype == bp_watchpoint
7304 || bptype == bp_hardware_watchpoint
7305 || bptype == bp_read_watchpoint
7306 || bptype == bp_access_watchpoint
7307 || bptype == bp_catchpoint)
7308 {
7309 /* Watchpoints and the various bp_catch_* eventpoints should not
7310 have their addresses modified. */
7311 return bpaddr;
7312 }
7313 else if (bptype == bp_single_step)
7314 {
7315 /* Single-step breakpoints should not have their addresses
7316 modified. If there's any architectural constrain that
7317 applies to this address, then it should have already been
7318 taken into account when the breakpoint was created in the
7319 first place. If we didn't do this, stepping through e.g.,
7320 Thumb-2 IT blocks would break. */
7321 return bpaddr;
7322 }
7323 else
7324 {
7325 CORE_ADDR adjusted_bpaddr;
7326
7327 /* Some targets have architectural constraints on the placement
7328 of breakpoint instructions. Obtain the adjusted address. */
7329 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
7330
7331 /* An adjusted breakpoint address can significantly alter
7332 a user's expectations. Print a warning if an adjustment
7333 is required. */
7334 if (adjusted_bpaddr != bpaddr)
7335 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7336
7337 return adjusted_bpaddr;
7338 }
7339 }
7340
7341 void
7342 init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
7343 struct breakpoint *owner)
7344 {
7345 memset (loc, 0, sizeof (*loc));
7346
7347 gdb_assert (ops != NULL);
7348
7349 loc->ops = ops;
7350 loc->owner = owner;
7351 loc->cond_bytecode = NULL;
7352 loc->shlib_disabled = 0;
7353 loc->enabled = 1;
7354
7355 switch (owner->type)
7356 {
7357 case bp_breakpoint:
7358 case bp_single_step:
7359 case bp_until:
7360 case bp_finish:
7361 case bp_longjmp:
7362 case bp_longjmp_resume:
7363 case bp_longjmp_call_dummy:
7364 case bp_exception:
7365 case bp_exception_resume:
7366 case bp_step_resume:
7367 case bp_hp_step_resume:
7368 case bp_watchpoint_scope:
7369 case bp_call_dummy:
7370 case bp_std_terminate:
7371 case bp_shlib_event:
7372 case bp_thread_event:
7373 case bp_overlay_event:
7374 case bp_jit_event:
7375 case bp_longjmp_master:
7376 case bp_std_terminate_master:
7377 case bp_exception_master:
7378 case bp_gnu_ifunc_resolver:
7379 case bp_gnu_ifunc_resolver_return:
7380 case bp_dprintf:
7381 loc->loc_type = bp_loc_software_breakpoint;
7382 mark_breakpoint_location_modified (loc);
7383 break;
7384 case bp_hardware_breakpoint:
7385 loc->loc_type = bp_loc_hardware_breakpoint;
7386 mark_breakpoint_location_modified (loc);
7387 break;
7388 case bp_hardware_watchpoint:
7389 case bp_read_watchpoint:
7390 case bp_access_watchpoint:
7391 loc->loc_type = bp_loc_hardware_watchpoint;
7392 break;
7393 case bp_watchpoint:
7394 case bp_catchpoint:
7395 case bp_tracepoint:
7396 case bp_fast_tracepoint:
7397 case bp_static_tracepoint:
7398 loc->loc_type = bp_loc_other;
7399 break;
7400 default:
7401 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
7402 }
7403
7404 loc->refc = 1;
7405 }
7406
7407 /* Allocate a struct bp_location. */
7408
7409 static struct bp_location *
7410 allocate_bp_location (struct breakpoint *bpt)
7411 {
7412 return bpt->ops->allocate_location (bpt);
7413 }
7414
7415 static void
7416 free_bp_location (struct bp_location *loc)
7417 {
7418 loc->ops->dtor (loc);
7419 delete loc;
7420 }
7421
7422 /* Increment reference count. */
7423
7424 static void
7425 incref_bp_location (struct bp_location *bl)
7426 {
7427 ++bl->refc;
7428 }
7429
7430 /* Decrement reference count. If the reference count reaches 0,
7431 destroy the bp_location. Sets *BLP to NULL. */
7432
7433 static void
7434 decref_bp_location (struct bp_location **blp)
7435 {
7436 gdb_assert ((*blp)->refc > 0);
7437
7438 if (--(*blp)->refc == 0)
7439 free_bp_location (*blp);
7440 *blp = NULL;
7441 }
7442
7443 /* Add breakpoint B at the end of the global breakpoint chain. */
7444
7445 static void
7446 add_to_breakpoint_chain (struct breakpoint *b)
7447 {
7448 struct breakpoint *b1;
7449
7450 /* Add this breakpoint to the end of the chain so that a list of
7451 breakpoints will come out in order of increasing numbers. */
7452
7453 b1 = breakpoint_chain;
7454 if (b1 == 0)
7455 breakpoint_chain = b;
7456 else
7457 {
7458 while (b1->next)
7459 b1 = b1->next;
7460 b1->next = b;
7461 }
7462 }
7463
7464 /* Initializes breakpoint B with type BPTYPE and no locations yet. */
7465
7466 static void
7467 init_raw_breakpoint_without_location (struct breakpoint *b,
7468 struct gdbarch *gdbarch,
7469 enum bptype bptype,
7470 const struct breakpoint_ops *ops)
7471 {
7472 memset (b, 0, sizeof (*b));
7473
7474 gdb_assert (ops != NULL);
7475
7476 b->ops = ops;
7477 b->type = bptype;
7478 b->gdbarch = gdbarch;
7479 b->language = current_language->la_language;
7480 b->input_radix = input_radix;
7481 b->thread = -1;
7482 b->enable_state = bp_enabled;
7483 b->next = 0;
7484 b->silent = 0;
7485 b->ignore_count = 0;
7486 b->commands = NULL;
7487 b->frame_id = null_frame_id;
7488 b->condition_not_parsed = 0;
7489 b->py_bp_object = NULL;
7490 b->related_breakpoint = b;
7491 b->location = NULL;
7492 }
7493
7494 /* Helper to set_raw_breakpoint below. Creates a breakpoint
7495 that has type BPTYPE and has no locations as yet. */
7496
7497 static struct breakpoint *
7498 set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
7499 enum bptype bptype,
7500 const struct breakpoint_ops *ops)
7501 {
7502 struct breakpoint *b = new breakpoint ();
7503
7504 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7505 add_to_breakpoint_chain (b);
7506 return b;
7507 }
7508
7509 /* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7510 resolutions should be made as the user specified the location explicitly
7511 enough. */
7512
7513 static void
7514 set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
7515 {
7516 gdb_assert (loc->owner != NULL);
7517
7518 if (loc->owner->type == bp_breakpoint
7519 || loc->owner->type == bp_hardware_breakpoint
7520 || is_tracepoint (loc->owner))
7521 {
7522 int is_gnu_ifunc;
7523 const char *function_name;
7524 CORE_ADDR func_addr;
7525
7526 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
7527 &func_addr, NULL, &is_gnu_ifunc);
7528
7529 if (is_gnu_ifunc && !explicit_loc)
7530 {
7531 struct breakpoint *b = loc->owner;
7532
7533 gdb_assert (loc->pspace == current_program_space);
7534 if (gnu_ifunc_resolve_name (function_name,
7535 &loc->requested_address))
7536 {
7537 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7538 loc->address = adjust_breakpoint_address (loc->gdbarch,
7539 loc->requested_address,
7540 b->type);
7541 }
7542 else if (b->type == bp_breakpoint && b->loc == loc
7543 && loc->next == NULL && b->related_breakpoint == b)
7544 {
7545 /* Create only the whole new breakpoint of this type but do not
7546 mess more complicated breakpoints with multiple locations. */
7547 b->type = bp_gnu_ifunc_resolver;
7548 /* Remember the resolver's address for use by the return
7549 breakpoint. */
7550 loc->related_address = func_addr;
7551 }
7552 }
7553
7554 if (function_name)
7555 loc->function_name = xstrdup (function_name);
7556 }
7557 }
7558
7559 /* Attempt to determine architecture of location identified by SAL. */
7560 struct gdbarch *
7561 get_sal_arch (struct symtab_and_line sal)
7562 {
7563 if (sal.section)
7564 return get_objfile_arch (sal.section->objfile);
7565 if (sal.symtab)
7566 return get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
7567
7568 return NULL;
7569 }
7570
7571 /* Low level routine for partially initializing a breakpoint of type
7572 BPTYPE. The newly created breakpoint's address, section, source
7573 file name, and line number are provided by SAL.
7574
7575 It is expected that the caller will complete the initialization of
7576 the newly created breakpoint struct as well as output any status
7577 information regarding the creation of a new breakpoint. */
7578
7579 static void
7580 init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
7581 struct symtab_and_line sal, enum bptype bptype,
7582 const struct breakpoint_ops *ops)
7583 {
7584 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7585
7586 add_location_to_breakpoint (b, &sal);
7587
7588 if (bptype != bp_catchpoint)
7589 gdb_assert (sal.pspace != NULL);
7590
7591 /* Store the program space that was used to set the breakpoint,
7592 except for ordinary breakpoints, which are independent of the
7593 program space. */
7594 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7595 b->pspace = sal.pspace;
7596 }
7597
7598 /* set_raw_breakpoint is a low level routine for allocating and
7599 partially initializing a breakpoint of type BPTYPE. The newly
7600 created breakpoint's address, section, source file name, and line
7601 number are provided by SAL. The newly created and partially
7602 initialized breakpoint is added to the breakpoint chain and
7603 is also returned as the value of this function.
7604
7605 It is expected that the caller will complete the initialization of
7606 the newly created breakpoint struct as well as output any status
7607 information regarding the creation of a new breakpoint. In
7608 particular, set_raw_breakpoint does NOT set the breakpoint
7609 number! Care should be taken to not allow an error to occur
7610 prior to completing the initialization of the breakpoint. If this
7611 should happen, a bogus breakpoint will be left on the chain. */
7612
7613 struct breakpoint *
7614 set_raw_breakpoint (struct gdbarch *gdbarch,
7615 struct symtab_and_line sal, enum bptype bptype,
7616 const struct breakpoint_ops *ops)
7617 {
7618 struct breakpoint *b = new breakpoint ();
7619
7620 init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
7621 add_to_breakpoint_chain (b);
7622 return b;
7623 }
7624
7625 /* Call this routine when stepping and nexting to enable a breakpoint
7626 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7627 initiated the operation. */
7628
7629 void
7630 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
7631 {
7632 struct breakpoint *b, *b_tmp;
7633 int thread = tp->global_num;
7634
7635 /* To avoid having to rescan all objfile symbols at every step,
7636 we maintain a list of continually-inserted but always disabled
7637 longjmp "master" breakpoints. Here, we simply create momentary
7638 clones of those and enable them for the requested thread. */
7639 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7640 if (b->pspace == current_program_space
7641 && (b->type == bp_longjmp_master
7642 || b->type == bp_exception_master))
7643 {
7644 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7645 struct breakpoint *clone;
7646
7647 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7648 after their removal. */
7649 clone = momentary_breakpoint_from_master (b, type,
7650 &longjmp_breakpoint_ops, 1);
7651 clone->thread = thread;
7652 }
7653
7654 tp->initiating_frame = frame;
7655 }
7656
7657 /* Delete all longjmp breakpoints from THREAD. */
7658 void
7659 delete_longjmp_breakpoint (int thread)
7660 {
7661 struct breakpoint *b, *b_tmp;
7662
7663 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7664 if (b->type == bp_longjmp || b->type == bp_exception)
7665 {
7666 if (b->thread == thread)
7667 delete_breakpoint (b);
7668 }
7669 }
7670
7671 void
7672 delete_longjmp_breakpoint_at_next_stop (int thread)
7673 {
7674 struct breakpoint *b, *b_tmp;
7675
7676 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7677 if (b->type == bp_longjmp || b->type == bp_exception)
7678 {
7679 if (b->thread == thread)
7680 b->disposition = disp_del_at_next_stop;
7681 }
7682 }
7683
7684 /* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7685 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7686 pointer to any of them. Return NULL if this system cannot place longjmp
7687 breakpoints. */
7688
7689 struct breakpoint *
7690 set_longjmp_breakpoint_for_call_dummy (void)
7691 {
7692 struct breakpoint *b, *retval = NULL;
7693
7694 ALL_BREAKPOINTS (b)
7695 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7696 {
7697 struct breakpoint *new_b;
7698
7699 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7700 &momentary_breakpoint_ops,
7701 1);
7702 new_b->thread = ptid_to_global_thread_id (inferior_ptid);
7703
7704 /* Link NEW_B into the chain of RETVAL breakpoints. */
7705
7706 gdb_assert (new_b->related_breakpoint == new_b);
7707 if (retval == NULL)
7708 retval = new_b;
7709 new_b->related_breakpoint = retval;
7710 while (retval->related_breakpoint != new_b->related_breakpoint)
7711 retval = retval->related_breakpoint;
7712 retval->related_breakpoint = new_b;
7713 }
7714
7715 return retval;
7716 }
7717
7718 /* Verify all existing dummy frames and their associated breakpoints for
7719 TP. Remove those which can no longer be found in the current frame
7720 stack.
7721
7722 You should call this function only at places where it is safe to currently
7723 unwind the whole stack. Failed stack unwind would discard live dummy
7724 frames. */
7725
7726 void
7727 check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
7728 {
7729 struct breakpoint *b, *b_tmp;
7730
7731 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7732 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
7733 {
7734 struct breakpoint *dummy_b = b->related_breakpoint;
7735
7736 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7737 dummy_b = dummy_b->related_breakpoint;
7738 if (dummy_b->type != bp_call_dummy
7739 || frame_find_by_id (dummy_b->frame_id) != NULL)
7740 continue;
7741
7742 dummy_frame_discard (dummy_b->frame_id, tp->ptid);
7743
7744 while (b->related_breakpoint != b)
7745 {
7746 if (b_tmp == b->related_breakpoint)
7747 b_tmp = b->related_breakpoint->next;
7748 delete_breakpoint (b->related_breakpoint);
7749 }
7750 delete_breakpoint (b);
7751 }
7752 }
7753
7754 void
7755 enable_overlay_breakpoints (void)
7756 {
7757 struct breakpoint *b;
7758
7759 ALL_BREAKPOINTS (b)
7760 if (b->type == bp_overlay_event)
7761 {
7762 b->enable_state = bp_enabled;
7763 update_global_location_list (UGLL_MAY_INSERT);
7764 overlay_events_enabled = 1;
7765 }
7766 }
7767
7768 void
7769 disable_overlay_breakpoints (void)
7770 {
7771 struct breakpoint *b;
7772
7773 ALL_BREAKPOINTS (b)
7774 if (b->type == bp_overlay_event)
7775 {
7776 b->enable_state = bp_disabled;
7777 update_global_location_list (UGLL_DONT_INSERT);
7778 overlay_events_enabled = 0;
7779 }
7780 }
7781
7782 /* Set an active std::terminate breakpoint for each std::terminate
7783 master breakpoint. */
7784 void
7785 set_std_terminate_breakpoint (void)
7786 {
7787 struct breakpoint *b, *b_tmp;
7788
7789 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7790 if (b->pspace == current_program_space
7791 && b->type == bp_std_terminate_master)
7792 {
7793 momentary_breakpoint_from_master (b, bp_std_terminate,
7794 &momentary_breakpoint_ops, 1);
7795 }
7796 }
7797
7798 /* Delete all the std::terminate breakpoints. */
7799 void
7800 delete_std_terminate_breakpoint (void)
7801 {
7802 struct breakpoint *b, *b_tmp;
7803
7804 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7805 if (b->type == bp_std_terminate)
7806 delete_breakpoint (b);
7807 }
7808
7809 struct breakpoint *
7810 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7811 {
7812 struct breakpoint *b;
7813
7814 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7815 &internal_breakpoint_ops);
7816
7817 b->enable_state = bp_enabled;
7818 /* location has to be used or breakpoint_re_set will delete me. */
7819 b->location = new_address_location (b->loc->address, NULL, 0);
7820
7821 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7822
7823 return b;
7824 }
7825
7826 struct lang_and_radix
7827 {
7828 enum language lang;
7829 int radix;
7830 };
7831
7832 /* Create a breakpoint for JIT code registration and unregistration. */
7833
7834 struct breakpoint *
7835 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7836 {
7837 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7838 &internal_breakpoint_ops);
7839 }
7840
7841 /* Remove JIT code registration and unregistration breakpoint(s). */
7842
7843 void
7844 remove_jit_event_breakpoints (void)
7845 {
7846 struct breakpoint *b, *b_tmp;
7847
7848 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7849 if (b->type == bp_jit_event
7850 && b->loc->pspace == current_program_space)
7851 delete_breakpoint (b);
7852 }
7853
7854 void
7855 remove_solib_event_breakpoints (void)
7856 {
7857 struct breakpoint *b, *b_tmp;
7858
7859 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7860 if (b->type == bp_shlib_event
7861 && b->loc->pspace == current_program_space)
7862 delete_breakpoint (b);
7863 }
7864
7865 /* See breakpoint.h. */
7866
7867 void
7868 remove_solib_event_breakpoints_at_next_stop (void)
7869 {
7870 struct breakpoint *b, *b_tmp;
7871
7872 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7873 if (b->type == bp_shlib_event
7874 && b->loc->pspace == current_program_space)
7875 b->disposition = disp_del_at_next_stop;
7876 }
7877
7878 /* Helper for create_solib_event_breakpoint /
7879 create_and_insert_solib_event_breakpoint. Allows specifying which
7880 INSERT_MODE to pass through to update_global_location_list. */
7881
7882 static struct breakpoint *
7883 create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7884 enum ugll_insert_mode insert_mode)
7885 {
7886 struct breakpoint *b;
7887
7888 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7889 &internal_breakpoint_ops);
7890 update_global_location_list_nothrow (insert_mode);
7891 return b;
7892 }
7893
7894 struct breakpoint *
7895 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7896 {
7897 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7898 }
7899
7900 /* See breakpoint.h. */
7901
7902 struct breakpoint *
7903 create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7904 {
7905 struct breakpoint *b;
7906
7907 /* Explicitly tell update_global_location_list to insert
7908 locations. */
7909 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
7910 if (!b->loc->inserted)
7911 {
7912 delete_breakpoint (b);
7913 return NULL;
7914 }
7915 return b;
7916 }
7917
7918 /* Disable any breakpoints that are on code in shared libraries. Only
7919 apply to enabled breakpoints, disabled ones can just stay disabled. */
7920
7921 void
7922 disable_breakpoints_in_shlibs (void)
7923 {
7924 struct bp_location *loc, **locp_tmp;
7925
7926 ALL_BP_LOCATIONS (loc, locp_tmp)
7927 {
7928 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7929 struct breakpoint *b = loc->owner;
7930
7931 /* We apply the check to all breakpoints, including disabled for
7932 those with loc->duplicate set. This is so that when breakpoint
7933 becomes enabled, or the duplicate is removed, gdb will try to
7934 insert all breakpoints. If we don't set shlib_disabled here,
7935 we'll try to insert those breakpoints and fail. */
7936 if (((b->type == bp_breakpoint)
7937 || (b->type == bp_jit_event)
7938 || (b->type == bp_hardware_breakpoint)
7939 || (is_tracepoint (b)))
7940 && loc->pspace == current_program_space
7941 && !loc->shlib_disabled
7942 && solib_name_from_address (loc->pspace, loc->address)
7943 )
7944 {
7945 loc->shlib_disabled = 1;
7946 }
7947 }
7948 }
7949
7950 /* Disable any breakpoints and tracepoints that are in SOLIB upon
7951 notification of unloaded_shlib. Only apply to enabled breakpoints,
7952 disabled ones can just stay disabled. */
7953
7954 static void
7955 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7956 {
7957 struct bp_location *loc, **locp_tmp;
7958 int disabled_shlib_breaks = 0;
7959
7960 ALL_BP_LOCATIONS (loc, locp_tmp)
7961 {
7962 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7963 struct breakpoint *b = loc->owner;
7964
7965 if (solib->pspace == loc->pspace
7966 && !loc->shlib_disabled
7967 && (((b->type == bp_breakpoint
7968 || b->type == bp_jit_event
7969 || b->type == bp_hardware_breakpoint)
7970 && (loc->loc_type == bp_loc_hardware_breakpoint
7971 || loc->loc_type == bp_loc_software_breakpoint))
7972 || is_tracepoint (b))
7973 && solib_contains_address_p (solib, loc->address))
7974 {
7975 loc->shlib_disabled = 1;
7976 /* At this point, we cannot rely on remove_breakpoint
7977 succeeding so we must mark the breakpoint as not inserted
7978 to prevent future errors occurring in remove_breakpoints. */
7979 loc->inserted = 0;
7980
7981 /* This may cause duplicate notifications for the same breakpoint. */
7982 observer_notify_breakpoint_modified (b);
7983
7984 if (!disabled_shlib_breaks)
7985 {
7986 target_terminal_ours_for_output ();
7987 warning (_("Temporarily disabling breakpoints "
7988 "for unloaded shared library \"%s\""),
7989 solib->so_name);
7990 }
7991 disabled_shlib_breaks = 1;
7992 }
7993 }
7994 }
7995
7996 /* Disable any breakpoints and tracepoints in OBJFILE upon
7997 notification of free_objfile. Only apply to enabled breakpoints,
7998 disabled ones can just stay disabled. */
7999
8000 static void
8001 disable_breakpoints_in_freed_objfile (struct objfile *objfile)
8002 {
8003 struct breakpoint *b;
8004
8005 if (objfile == NULL)
8006 return;
8007
8008 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
8009 managed by the user with add-symbol-file/remove-symbol-file.
8010 Similarly to how breakpoints in shared libraries are handled in
8011 response to "nosharedlibrary", mark breakpoints in such modules
8012 shlib_disabled so they end up uninserted on the next global
8013 location list update. Shared libraries not loaded by the user
8014 aren't handled here -- they're already handled in
8015 disable_breakpoints_in_unloaded_shlib, called by solib.c's
8016 solib_unloaded observer. We skip objfiles that are not
8017 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
8018 main objfile). */
8019 if ((objfile->flags & OBJF_SHARED) == 0
8020 || (objfile->flags & OBJF_USERLOADED) == 0)
8021 return;
8022
8023 ALL_BREAKPOINTS (b)
8024 {
8025 struct bp_location *loc;
8026 int bp_modified = 0;
8027
8028 if (!is_breakpoint (b) && !is_tracepoint (b))
8029 continue;
8030
8031 for (loc = b->loc; loc != NULL; loc = loc->next)
8032 {
8033 CORE_ADDR loc_addr = loc->address;
8034
8035 if (loc->loc_type != bp_loc_hardware_breakpoint
8036 && loc->loc_type != bp_loc_software_breakpoint)
8037 continue;
8038
8039 if (loc->shlib_disabled != 0)
8040 continue;
8041
8042 if (objfile->pspace != loc->pspace)
8043 continue;
8044
8045 if (loc->loc_type != bp_loc_hardware_breakpoint
8046 && loc->loc_type != bp_loc_software_breakpoint)
8047 continue;
8048
8049 if (is_addr_in_objfile (loc_addr, objfile))
8050 {
8051 loc->shlib_disabled = 1;
8052 /* At this point, we don't know whether the object was
8053 unmapped from the inferior or not, so leave the
8054 inserted flag alone. We'll handle failure to
8055 uninsert quietly, in case the object was indeed
8056 unmapped. */
8057
8058 mark_breakpoint_location_modified (loc);
8059
8060 bp_modified = 1;
8061 }
8062 }
8063
8064 if (bp_modified)
8065 observer_notify_breakpoint_modified (b);
8066 }
8067 }
8068
8069 /* FORK & VFORK catchpoints. */
8070
8071 /* An instance of this type is used to represent a fork or vfork
8072 catchpoint. It includes a "struct breakpoint" as a kind of base
8073 class; users downcast to "struct breakpoint *" when needed. A
8074 breakpoint is really of this type iff its ops pointer points to
8075 CATCH_FORK_BREAKPOINT_OPS. */
8076
8077 struct fork_catchpoint
8078 {
8079 /* The base class. */
8080 struct breakpoint base;
8081
8082 /* Process id of a child process whose forking triggered this
8083 catchpoint. This field is only valid immediately after this
8084 catchpoint has triggered. */
8085 ptid_t forked_inferior_pid;
8086 };
8087
8088 /* Implement the "insert" breakpoint_ops method for fork
8089 catchpoints. */
8090
8091 static int
8092 insert_catch_fork (struct bp_location *bl)
8093 {
8094 return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
8095 }
8096
8097 /* Implement the "remove" breakpoint_ops method for fork
8098 catchpoints. */
8099
8100 static int
8101 remove_catch_fork (struct bp_location *bl, enum remove_bp_reason reason)
8102 {
8103 return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
8104 }
8105
8106 /* Implement the "breakpoint_hit" breakpoint_ops method for fork
8107 catchpoints. */
8108
8109 static int
8110 breakpoint_hit_catch_fork (const struct bp_location *bl,
8111 struct address_space *aspace, CORE_ADDR bp_addr,
8112 const struct target_waitstatus *ws)
8113 {
8114 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8115
8116 if (ws->kind != TARGET_WAITKIND_FORKED)
8117 return 0;
8118
8119 c->forked_inferior_pid = ws->value.related_pid;
8120 return 1;
8121 }
8122
8123 /* Implement the "print_it" breakpoint_ops method for fork
8124 catchpoints. */
8125
8126 static enum print_stop_action
8127 print_it_catch_fork (bpstat bs)
8128 {
8129 struct ui_out *uiout = current_uiout;
8130 struct breakpoint *b = bs->breakpoint_at;
8131 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
8132
8133 annotate_catchpoint (b->number);
8134 maybe_print_thread_hit_breakpoint (uiout);
8135 if (b->disposition == disp_del)
8136 ui_out_text (uiout, "Temporary catchpoint ");
8137 else
8138 ui_out_text (uiout, "Catchpoint ");
8139 if (ui_out_is_mi_like_p (uiout))
8140 {
8141 ui_out_field_string (uiout, "reason",
8142 async_reason_lookup (EXEC_ASYNC_FORK));
8143 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8144 }
8145 ui_out_field_int (uiout, "bkptno", b->number);
8146 ui_out_text (uiout, " (forked process ");
8147 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
8148 ui_out_text (uiout, "), ");
8149 return PRINT_SRC_AND_LOC;
8150 }
8151
8152 /* Implement the "print_one" breakpoint_ops method for fork
8153 catchpoints. */
8154
8155 static void
8156 print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
8157 {
8158 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8159 struct value_print_options opts;
8160 struct ui_out *uiout = current_uiout;
8161
8162 get_user_print_options (&opts);
8163
8164 /* Field 4, the address, is omitted (which makes the columns not
8165 line up too nicely with the headers, but the effect is relatively
8166 readable). */
8167 if (opts.addressprint)
8168 ui_out_field_skip (uiout, "addr");
8169 annotate_field (5);
8170 ui_out_text (uiout, "fork");
8171 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
8172 {
8173 ui_out_text (uiout, ", process ");
8174 ui_out_field_int (uiout, "what",
8175 ptid_get_pid (c->forked_inferior_pid));
8176 ui_out_spaces (uiout, 1);
8177 }
8178
8179 if (ui_out_is_mi_like_p (uiout))
8180 ui_out_field_string (uiout, "catch-type", "fork");
8181 }
8182
8183 /* Implement the "print_mention" breakpoint_ops method for fork
8184 catchpoints. */
8185
8186 static void
8187 print_mention_catch_fork (struct breakpoint *b)
8188 {
8189 printf_filtered (_("Catchpoint %d (fork)"), b->number);
8190 }
8191
8192 /* Implement the "print_recreate" breakpoint_ops method for fork
8193 catchpoints. */
8194
8195 static void
8196 print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
8197 {
8198 fprintf_unfiltered (fp, "catch fork");
8199 print_recreate_thread (b, fp);
8200 }
8201
8202 /* The breakpoint_ops structure to be used in fork catchpoints. */
8203
8204 static struct breakpoint_ops catch_fork_breakpoint_ops;
8205
8206 /* Implement the "insert" breakpoint_ops method for vfork
8207 catchpoints. */
8208
8209 static int
8210 insert_catch_vfork (struct bp_location *bl)
8211 {
8212 return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
8213 }
8214
8215 /* Implement the "remove" breakpoint_ops method for vfork
8216 catchpoints. */
8217
8218 static int
8219 remove_catch_vfork (struct bp_location *bl, enum remove_bp_reason reason)
8220 {
8221 return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
8222 }
8223
8224 /* Implement the "breakpoint_hit" breakpoint_ops method for vfork
8225 catchpoints. */
8226
8227 static int
8228 breakpoint_hit_catch_vfork (const struct bp_location *bl,
8229 struct address_space *aspace, CORE_ADDR bp_addr,
8230 const struct target_waitstatus *ws)
8231 {
8232 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8233
8234 if (ws->kind != TARGET_WAITKIND_VFORKED)
8235 return 0;
8236
8237 c->forked_inferior_pid = ws->value.related_pid;
8238 return 1;
8239 }
8240
8241 /* Implement the "print_it" breakpoint_ops method for vfork
8242 catchpoints. */
8243
8244 static enum print_stop_action
8245 print_it_catch_vfork (bpstat bs)
8246 {
8247 struct ui_out *uiout = current_uiout;
8248 struct breakpoint *b = bs->breakpoint_at;
8249 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8250
8251 annotate_catchpoint (b->number);
8252 maybe_print_thread_hit_breakpoint (uiout);
8253 if (b->disposition == disp_del)
8254 ui_out_text (uiout, "Temporary catchpoint ");
8255 else
8256 ui_out_text (uiout, "Catchpoint ");
8257 if (ui_out_is_mi_like_p (uiout))
8258 {
8259 ui_out_field_string (uiout, "reason",
8260 async_reason_lookup (EXEC_ASYNC_VFORK));
8261 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8262 }
8263 ui_out_field_int (uiout, "bkptno", b->number);
8264 ui_out_text (uiout, " (vforked process ");
8265 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
8266 ui_out_text (uiout, "), ");
8267 return PRINT_SRC_AND_LOC;
8268 }
8269
8270 /* Implement the "print_one" breakpoint_ops method for vfork
8271 catchpoints. */
8272
8273 static void
8274 print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
8275 {
8276 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8277 struct value_print_options opts;
8278 struct ui_out *uiout = current_uiout;
8279
8280 get_user_print_options (&opts);
8281 /* Field 4, the address, is omitted (which makes the columns not
8282 line up too nicely with the headers, but the effect is relatively
8283 readable). */
8284 if (opts.addressprint)
8285 ui_out_field_skip (uiout, "addr");
8286 annotate_field (5);
8287 ui_out_text (uiout, "vfork");
8288 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
8289 {
8290 ui_out_text (uiout, ", process ");
8291 ui_out_field_int (uiout, "what",
8292 ptid_get_pid (c->forked_inferior_pid));
8293 ui_out_spaces (uiout, 1);
8294 }
8295
8296 if (ui_out_is_mi_like_p (uiout))
8297 ui_out_field_string (uiout, "catch-type", "vfork");
8298 }
8299
8300 /* Implement the "print_mention" breakpoint_ops method for vfork
8301 catchpoints. */
8302
8303 static void
8304 print_mention_catch_vfork (struct breakpoint *b)
8305 {
8306 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
8307 }
8308
8309 /* Implement the "print_recreate" breakpoint_ops method for vfork
8310 catchpoints. */
8311
8312 static void
8313 print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8314 {
8315 fprintf_unfiltered (fp, "catch vfork");
8316 print_recreate_thread (b, fp);
8317 }
8318
8319 /* The breakpoint_ops structure to be used in vfork catchpoints. */
8320
8321 static struct breakpoint_ops catch_vfork_breakpoint_ops;
8322
8323 /* An instance of this type is used to represent an solib catchpoint.
8324 It includes a "struct breakpoint" as a kind of base class; users
8325 downcast to "struct breakpoint *" when needed. A breakpoint is
8326 really of this type iff its ops pointer points to
8327 CATCH_SOLIB_BREAKPOINT_OPS. */
8328
8329 struct solib_catchpoint
8330 {
8331 /* The base class. */
8332 struct breakpoint base;
8333
8334 /* True for "catch load", false for "catch unload". */
8335 unsigned char is_load;
8336
8337 /* Regular expression to match, if any. COMPILED is only valid when
8338 REGEX is non-NULL. */
8339 char *regex;
8340 regex_t compiled;
8341 };
8342
8343 static void
8344 dtor_catch_solib (struct breakpoint *b)
8345 {
8346 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8347
8348 if (self->regex)
8349 regfree (&self->compiled);
8350 xfree (self->regex);
8351
8352 base_breakpoint_ops.dtor (b);
8353 }
8354
8355 static int
8356 insert_catch_solib (struct bp_location *ignore)
8357 {
8358 return 0;
8359 }
8360
8361 static int
8362 remove_catch_solib (struct bp_location *ignore, enum remove_bp_reason reason)
8363 {
8364 return 0;
8365 }
8366
8367 static int
8368 breakpoint_hit_catch_solib (const struct bp_location *bl,
8369 struct address_space *aspace,
8370 CORE_ADDR bp_addr,
8371 const struct target_waitstatus *ws)
8372 {
8373 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8374 struct breakpoint *other;
8375
8376 if (ws->kind == TARGET_WAITKIND_LOADED)
8377 return 1;
8378
8379 ALL_BREAKPOINTS (other)
8380 {
8381 struct bp_location *other_bl;
8382
8383 if (other == bl->owner)
8384 continue;
8385
8386 if (other->type != bp_shlib_event)
8387 continue;
8388
8389 if (self->base.pspace != NULL && other->pspace != self->base.pspace)
8390 continue;
8391
8392 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8393 {
8394 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8395 return 1;
8396 }
8397 }
8398
8399 return 0;
8400 }
8401
8402 static void
8403 check_status_catch_solib (struct bpstats *bs)
8404 {
8405 struct solib_catchpoint *self
8406 = (struct solib_catchpoint *) bs->breakpoint_at;
8407 int ix;
8408
8409 if (self->is_load)
8410 {
8411 struct so_list *iter;
8412
8413 for (ix = 0;
8414 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
8415 ix, iter);
8416 ++ix)
8417 {
8418 if (!self->regex
8419 || regexec (&self->compiled, iter->so_name, 0, NULL, 0) == 0)
8420 return;
8421 }
8422 }
8423 else
8424 {
8425 char *iter;
8426
8427 for (ix = 0;
8428 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
8429 ix, iter);
8430 ++ix)
8431 {
8432 if (!self->regex
8433 || regexec (&self->compiled, iter, 0, NULL, 0) == 0)
8434 return;
8435 }
8436 }
8437
8438 bs->stop = 0;
8439 bs->print_it = print_it_noop;
8440 }
8441
8442 static enum print_stop_action
8443 print_it_catch_solib (bpstat bs)
8444 {
8445 struct breakpoint *b = bs->breakpoint_at;
8446 struct ui_out *uiout = current_uiout;
8447
8448 annotate_catchpoint (b->number);
8449 maybe_print_thread_hit_breakpoint (uiout);
8450 if (b->disposition == disp_del)
8451 ui_out_text (uiout, "Temporary catchpoint ");
8452 else
8453 ui_out_text (uiout, "Catchpoint ");
8454 ui_out_field_int (uiout, "bkptno", b->number);
8455 ui_out_text (uiout, "\n");
8456 if (ui_out_is_mi_like_p (uiout))
8457 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8458 print_solib_event (1);
8459 return PRINT_SRC_AND_LOC;
8460 }
8461
8462 static void
8463 print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8464 {
8465 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8466 struct value_print_options opts;
8467 struct ui_out *uiout = current_uiout;
8468 char *msg;
8469
8470 get_user_print_options (&opts);
8471 /* Field 4, the address, is omitted (which makes the columns not
8472 line up too nicely with the headers, but the effect is relatively
8473 readable). */
8474 if (opts.addressprint)
8475 {
8476 annotate_field (4);
8477 ui_out_field_skip (uiout, "addr");
8478 }
8479
8480 annotate_field (5);
8481 if (self->is_load)
8482 {
8483 if (self->regex)
8484 msg = xstrprintf (_("load of library matching %s"), self->regex);
8485 else
8486 msg = xstrdup (_("load of library"));
8487 }
8488 else
8489 {
8490 if (self->regex)
8491 msg = xstrprintf (_("unload of library matching %s"), self->regex);
8492 else
8493 msg = xstrdup (_("unload of library"));
8494 }
8495 ui_out_field_string (uiout, "what", msg);
8496 xfree (msg);
8497
8498 if (ui_out_is_mi_like_p (uiout))
8499 ui_out_field_string (uiout, "catch-type",
8500 self->is_load ? "load" : "unload");
8501 }
8502
8503 static void
8504 print_mention_catch_solib (struct breakpoint *b)
8505 {
8506 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8507
8508 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8509 self->is_load ? "load" : "unload");
8510 }
8511
8512 static void
8513 print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8514 {
8515 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8516
8517 fprintf_unfiltered (fp, "%s %s",
8518 b->disposition == disp_del ? "tcatch" : "catch",
8519 self->is_load ? "load" : "unload");
8520 if (self->regex)
8521 fprintf_unfiltered (fp, " %s", self->regex);
8522 fprintf_unfiltered (fp, "\n");
8523 }
8524
8525 static struct breakpoint_ops catch_solib_breakpoint_ops;
8526
8527 /* Shared helper function (MI and CLI) for creating and installing
8528 a shared object event catchpoint. If IS_LOAD is non-zero then
8529 the events to be caught are load events, otherwise they are
8530 unload events. If IS_TEMP is non-zero the catchpoint is a
8531 temporary one. If ENABLED is non-zero the catchpoint is
8532 created in an enabled state. */
8533
8534 void
8535 add_solib_catchpoint (char *arg, int is_load, int is_temp, int enabled)
8536 {
8537 struct solib_catchpoint *c;
8538 struct gdbarch *gdbarch = get_current_arch ();
8539 struct cleanup *cleanup;
8540
8541 if (!arg)
8542 arg = "";
8543 arg = skip_spaces (arg);
8544
8545 c = new solib_catchpoint ();
8546 cleanup = make_cleanup (xfree, c);
8547
8548 if (*arg != '\0')
8549 {
8550 int errcode;
8551
8552 errcode = regcomp (&c->compiled, arg, REG_NOSUB);
8553 if (errcode != 0)
8554 {
8555 char *err = get_regcomp_error (errcode, &c->compiled);
8556
8557 make_cleanup (xfree, err);
8558 error (_("Invalid regexp (%s): %s"), err, arg);
8559 }
8560 c->regex = xstrdup (arg);
8561 }
8562
8563 c->is_load = is_load;
8564 init_catchpoint (&c->base, gdbarch, is_temp, NULL,
8565 &catch_solib_breakpoint_ops);
8566
8567 c->base.enable_state = enabled ? bp_enabled : bp_disabled;
8568
8569 discard_cleanups (cleanup);
8570 install_breakpoint (0, &c->base, 1);
8571 }
8572
8573 /* A helper function that does all the work for "catch load" and
8574 "catch unload". */
8575
8576 static void
8577 catch_load_or_unload (char *arg, int from_tty, int is_load,
8578 struct cmd_list_element *command)
8579 {
8580 int tempflag;
8581 const int enabled = 1;
8582
8583 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8584
8585 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8586 }
8587
8588 static void
8589 catch_load_command_1 (char *arg, int from_tty,
8590 struct cmd_list_element *command)
8591 {
8592 catch_load_or_unload (arg, from_tty, 1, command);
8593 }
8594
8595 static void
8596 catch_unload_command_1 (char *arg, int from_tty,
8597 struct cmd_list_element *command)
8598 {
8599 catch_load_or_unload (arg, from_tty, 0, command);
8600 }
8601
8602 /* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8603 is non-zero, then make the breakpoint temporary. If COND_STRING is
8604 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8605 the breakpoint_ops structure associated to the catchpoint. */
8606
8607 void
8608 init_catchpoint (struct breakpoint *b,
8609 struct gdbarch *gdbarch, int tempflag,
8610 char *cond_string,
8611 const struct breakpoint_ops *ops)
8612 {
8613 struct symtab_and_line sal;
8614
8615 init_sal (&sal);
8616 sal.pspace = current_program_space;
8617
8618 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
8619
8620 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
8621 b->disposition = tempflag ? disp_del : disp_donttouch;
8622 }
8623
8624 void
8625 install_breakpoint (int internal, struct breakpoint *b, int update_gll)
8626 {
8627 add_to_breakpoint_chain (b);
8628 set_breakpoint_number (internal, b);
8629 if (is_tracepoint (b))
8630 set_tracepoint_count (breakpoint_count);
8631 if (!internal)
8632 mention (b);
8633 observer_notify_breakpoint_created (b);
8634
8635 if (update_gll)
8636 update_global_location_list (UGLL_MAY_INSERT);
8637 }
8638
8639 static void
8640 create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8641 int tempflag, char *cond_string,
8642 const struct breakpoint_ops *ops)
8643 {
8644 struct fork_catchpoint *c = new fork_catchpoint ();
8645
8646 init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
8647
8648 c->forked_inferior_pid = null_ptid;
8649
8650 install_breakpoint (0, &c->base, 1);
8651 }
8652
8653 /* Exec catchpoints. */
8654
8655 /* An instance of this type is used to represent an exec catchpoint.
8656 It includes a "struct breakpoint" as a kind of base class; users
8657 downcast to "struct breakpoint *" when needed. A breakpoint is
8658 really of this type iff its ops pointer points to
8659 CATCH_EXEC_BREAKPOINT_OPS. */
8660
8661 struct exec_catchpoint
8662 {
8663 /* The base class. */
8664 struct breakpoint base;
8665
8666 /* Filename of a program whose exec triggered this catchpoint.
8667 This field is only valid immediately after this catchpoint has
8668 triggered. */
8669 char *exec_pathname;
8670 };
8671
8672 /* Implement the "dtor" breakpoint_ops method for exec
8673 catchpoints. */
8674
8675 static void
8676 dtor_catch_exec (struct breakpoint *b)
8677 {
8678 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8679
8680 xfree (c->exec_pathname);
8681
8682 base_breakpoint_ops.dtor (b);
8683 }
8684
8685 static int
8686 insert_catch_exec (struct bp_location *bl)
8687 {
8688 return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
8689 }
8690
8691 static int
8692 remove_catch_exec (struct bp_location *bl, enum remove_bp_reason reason)
8693 {
8694 return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
8695 }
8696
8697 static int
8698 breakpoint_hit_catch_exec (const struct bp_location *bl,
8699 struct address_space *aspace, CORE_ADDR bp_addr,
8700 const struct target_waitstatus *ws)
8701 {
8702 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8703
8704 if (ws->kind != TARGET_WAITKIND_EXECD)
8705 return 0;
8706
8707 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8708 return 1;
8709 }
8710
8711 static enum print_stop_action
8712 print_it_catch_exec (bpstat bs)
8713 {
8714 struct ui_out *uiout = current_uiout;
8715 struct breakpoint *b = bs->breakpoint_at;
8716 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8717
8718 annotate_catchpoint (b->number);
8719 maybe_print_thread_hit_breakpoint (uiout);
8720 if (b->disposition == disp_del)
8721 ui_out_text (uiout, "Temporary catchpoint ");
8722 else
8723 ui_out_text (uiout, "Catchpoint ");
8724 if (ui_out_is_mi_like_p (uiout))
8725 {
8726 ui_out_field_string (uiout, "reason",
8727 async_reason_lookup (EXEC_ASYNC_EXEC));
8728 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8729 }
8730 ui_out_field_int (uiout, "bkptno", b->number);
8731 ui_out_text (uiout, " (exec'd ");
8732 ui_out_field_string (uiout, "new-exec", c->exec_pathname);
8733 ui_out_text (uiout, "), ");
8734
8735 return PRINT_SRC_AND_LOC;
8736 }
8737
8738 static void
8739 print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
8740 {
8741 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8742 struct value_print_options opts;
8743 struct ui_out *uiout = current_uiout;
8744
8745 get_user_print_options (&opts);
8746
8747 /* Field 4, the address, is omitted (which makes the columns
8748 not line up too nicely with the headers, but the effect
8749 is relatively readable). */
8750 if (opts.addressprint)
8751 ui_out_field_skip (uiout, "addr");
8752 annotate_field (5);
8753 ui_out_text (uiout, "exec");
8754 if (c->exec_pathname != NULL)
8755 {
8756 ui_out_text (uiout, ", program \"");
8757 ui_out_field_string (uiout, "what", c->exec_pathname);
8758 ui_out_text (uiout, "\" ");
8759 }
8760
8761 if (ui_out_is_mi_like_p (uiout))
8762 ui_out_field_string (uiout, "catch-type", "exec");
8763 }
8764
8765 static void
8766 print_mention_catch_exec (struct breakpoint *b)
8767 {
8768 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8769 }
8770
8771 /* Implement the "print_recreate" breakpoint_ops method for exec
8772 catchpoints. */
8773
8774 static void
8775 print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8776 {
8777 fprintf_unfiltered (fp, "catch exec");
8778 print_recreate_thread (b, fp);
8779 }
8780
8781 static struct breakpoint_ops catch_exec_breakpoint_ops;
8782
8783 static int
8784 hw_breakpoint_used_count (void)
8785 {
8786 int i = 0;
8787 struct breakpoint *b;
8788 struct bp_location *bl;
8789
8790 ALL_BREAKPOINTS (b)
8791 {
8792 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
8793 for (bl = b->loc; bl; bl = bl->next)
8794 {
8795 /* Special types of hardware breakpoints may use more than
8796 one register. */
8797 i += b->ops->resources_needed (bl);
8798 }
8799 }
8800
8801 return i;
8802 }
8803
8804 /* Returns the resources B would use if it were a hardware
8805 watchpoint. */
8806
8807 static int
8808 hw_watchpoint_use_count (struct breakpoint *b)
8809 {
8810 int i = 0;
8811 struct bp_location *bl;
8812
8813 if (!breakpoint_enabled (b))
8814 return 0;
8815
8816 for (bl = b->loc; bl; bl = bl->next)
8817 {
8818 /* Special types of hardware watchpoints may use more than
8819 one register. */
8820 i += b->ops->resources_needed (bl);
8821 }
8822
8823 return i;
8824 }
8825
8826 /* Returns the sum the used resources of all hardware watchpoints of
8827 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8828 the sum of the used resources of all hardware watchpoints of other
8829 types _not_ TYPE. */
8830
8831 static int
8832 hw_watchpoint_used_count_others (struct breakpoint *except,
8833 enum bptype type, int *other_type_used)
8834 {
8835 int i = 0;
8836 struct breakpoint *b;
8837
8838 *other_type_used = 0;
8839 ALL_BREAKPOINTS (b)
8840 {
8841 if (b == except)
8842 continue;
8843 if (!breakpoint_enabled (b))
8844 continue;
8845
8846 if (b->type == type)
8847 i += hw_watchpoint_use_count (b);
8848 else if (is_hardware_watchpoint (b))
8849 *other_type_used = 1;
8850 }
8851
8852 return i;
8853 }
8854
8855 void
8856 disable_watchpoints_before_interactive_call_start (void)
8857 {
8858 struct breakpoint *b;
8859
8860 ALL_BREAKPOINTS (b)
8861 {
8862 if (is_watchpoint (b) && breakpoint_enabled (b))
8863 {
8864 b->enable_state = bp_call_disabled;
8865 update_global_location_list (UGLL_DONT_INSERT);
8866 }
8867 }
8868 }
8869
8870 void
8871 enable_watchpoints_after_interactive_call_stop (void)
8872 {
8873 struct breakpoint *b;
8874
8875 ALL_BREAKPOINTS (b)
8876 {
8877 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
8878 {
8879 b->enable_state = bp_enabled;
8880 update_global_location_list (UGLL_MAY_INSERT);
8881 }
8882 }
8883 }
8884
8885 void
8886 disable_breakpoints_before_startup (void)
8887 {
8888 current_program_space->executing_startup = 1;
8889 update_global_location_list (UGLL_DONT_INSERT);
8890 }
8891
8892 void
8893 enable_breakpoints_after_startup (void)
8894 {
8895 current_program_space->executing_startup = 0;
8896 breakpoint_re_set ();
8897 }
8898
8899 /* Create a new single-step breakpoint for thread THREAD, with no
8900 locations. */
8901
8902 static struct breakpoint *
8903 new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8904 {
8905 struct breakpoint *b = new breakpoint ();
8906
8907 init_raw_breakpoint_without_location (b, gdbarch, bp_single_step,
8908 &momentary_breakpoint_ops);
8909
8910 b->disposition = disp_donttouch;
8911 b->frame_id = null_frame_id;
8912
8913 b->thread = thread;
8914 gdb_assert (b->thread != 0);
8915
8916 add_to_breakpoint_chain (b);
8917
8918 return b;
8919 }
8920
8921 /* Set a momentary breakpoint of type TYPE at address specified by
8922 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8923 frame. */
8924
8925 struct breakpoint *
8926 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8927 struct frame_id frame_id, enum bptype type)
8928 {
8929 struct breakpoint *b;
8930
8931 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8932 tail-called one. */
8933 gdb_assert (!frame_id_artificial_p (frame_id));
8934
8935 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
8936 b->enable_state = bp_enabled;
8937 b->disposition = disp_donttouch;
8938 b->frame_id = frame_id;
8939
8940 /* If we're debugging a multi-threaded program, then we want
8941 momentary breakpoints to be active in only a single thread of
8942 control. */
8943 if (in_thread_list (inferior_ptid))
8944 b->thread = ptid_to_global_thread_id (inferior_ptid);
8945
8946 update_global_location_list_nothrow (UGLL_MAY_INSERT);
8947
8948 return b;
8949 }
8950
8951 /* Make a momentary breakpoint based on the master breakpoint ORIG.
8952 The new breakpoint will have type TYPE, use OPS as its
8953 breakpoint_ops, and will set enabled to LOC_ENABLED. */
8954
8955 static struct breakpoint *
8956 momentary_breakpoint_from_master (struct breakpoint *orig,
8957 enum bptype type,
8958 const struct breakpoint_ops *ops,
8959 int loc_enabled)
8960 {
8961 struct breakpoint *copy;
8962
8963 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
8964 copy->loc = allocate_bp_location (copy);
8965 set_breakpoint_location_function (copy->loc, 1);
8966
8967 copy->loc->gdbarch = orig->loc->gdbarch;
8968 copy->loc->requested_address = orig->loc->requested_address;
8969 copy->loc->address = orig->loc->address;
8970 copy->loc->section = orig->loc->section;
8971 copy->loc->pspace = orig->loc->pspace;
8972 copy->loc->probe = orig->loc->probe;
8973 copy->loc->line_number = orig->loc->line_number;
8974 copy->loc->symtab = orig->loc->symtab;
8975 copy->loc->enabled = loc_enabled;
8976 copy->frame_id = orig->frame_id;
8977 copy->thread = orig->thread;
8978 copy->pspace = orig->pspace;
8979
8980 copy->enable_state = bp_enabled;
8981 copy->disposition = disp_donttouch;
8982 copy->number = internal_breakpoint_number--;
8983
8984 update_global_location_list_nothrow (UGLL_DONT_INSERT);
8985 return copy;
8986 }
8987
8988 /* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8989 ORIG is NULL. */
8990
8991 struct breakpoint *
8992 clone_momentary_breakpoint (struct breakpoint *orig)
8993 {
8994 /* If there's nothing to clone, then return nothing. */
8995 if (orig == NULL)
8996 return NULL;
8997
8998 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
8999 }
9000
9001 struct breakpoint *
9002 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
9003 enum bptype type)
9004 {
9005 struct symtab_and_line sal;
9006
9007 sal = find_pc_line (pc, 0);
9008 sal.pc = pc;
9009 sal.section = find_pc_overlay (pc);
9010 sal.explicit_pc = 1;
9011
9012 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
9013 }
9014 \f
9015
9016 /* Tell the user we have just set a breakpoint B. */
9017
9018 static void
9019 mention (struct breakpoint *b)
9020 {
9021 b->ops->print_mention (b);
9022 if (ui_out_is_mi_like_p (current_uiout))
9023 return;
9024 printf_filtered ("\n");
9025 }
9026 \f
9027
9028 static int bp_loc_is_permanent (struct bp_location *loc);
9029
9030 static struct bp_location *
9031 add_location_to_breakpoint (struct breakpoint *b,
9032 const struct symtab_and_line *sal)
9033 {
9034 struct bp_location *loc, **tmp;
9035 CORE_ADDR adjusted_address;
9036 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
9037
9038 if (loc_gdbarch == NULL)
9039 loc_gdbarch = b->gdbarch;
9040
9041 /* Adjust the breakpoint's address prior to allocating a location.
9042 Once we call allocate_bp_location(), that mostly uninitialized
9043 location will be placed on the location chain. Adjustment of the
9044 breakpoint may cause target_read_memory() to be called and we do
9045 not want its scan of the location chain to find a breakpoint and
9046 location that's only been partially initialized. */
9047 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
9048 sal->pc, b->type);
9049
9050 /* Sort the locations by their ADDRESS. */
9051 loc = allocate_bp_location (b);
9052 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
9053 tmp = &((*tmp)->next))
9054 ;
9055 loc->next = *tmp;
9056 *tmp = loc;
9057
9058 loc->requested_address = sal->pc;
9059 loc->address = adjusted_address;
9060 loc->pspace = sal->pspace;
9061 loc->probe.probe = sal->probe;
9062 loc->probe.objfile = sal->objfile;
9063 gdb_assert (loc->pspace != NULL);
9064 loc->section = sal->section;
9065 loc->gdbarch = loc_gdbarch;
9066 loc->line_number = sal->line;
9067 loc->symtab = sal->symtab;
9068
9069 set_breakpoint_location_function (loc,
9070 sal->explicit_pc || sal->explicit_line);
9071
9072 /* While by definition, permanent breakpoints are already present in the
9073 code, we don't mark the location as inserted. Normally one would expect
9074 that GDB could rely on that breakpoint instruction to stop the program,
9075 thus removing the need to insert its own breakpoint, except that executing
9076 the breakpoint instruction can kill the target instead of reporting a
9077 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
9078 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
9079 with "Trap 0x02 while interrupts disabled, Error state". Letting the
9080 breakpoint be inserted normally results in QEMU knowing about the GDB
9081 breakpoint, and thus trap before the breakpoint instruction is executed.
9082 (If GDB later needs to continue execution past the permanent breakpoint,
9083 it manually increments the PC, thus avoiding executing the breakpoint
9084 instruction.) */
9085 if (bp_loc_is_permanent (loc))
9086 loc->permanent = 1;
9087
9088 return loc;
9089 }
9090 \f
9091
9092 /* See breakpoint.h. */
9093
9094 int
9095 program_breakpoint_here_p (struct gdbarch *gdbarch, CORE_ADDR address)
9096 {
9097 int len;
9098 CORE_ADDR addr;
9099 const gdb_byte *bpoint;
9100 gdb_byte *target_mem;
9101 struct cleanup *cleanup;
9102 int retval = 0;
9103
9104 addr = address;
9105 bpoint = gdbarch_breakpoint_from_pc (gdbarch, &addr, &len);
9106
9107 /* Software breakpoints unsupported? */
9108 if (bpoint == NULL)
9109 return 0;
9110
9111 target_mem = (gdb_byte *) alloca (len);
9112
9113 /* Enable the automatic memory restoration from breakpoints while
9114 we read the memory. Otherwise we could say about our temporary
9115 breakpoints they are permanent. */
9116 cleanup = make_show_memory_breakpoints_cleanup (0);
9117
9118 if (target_read_memory (address, target_mem, len) == 0
9119 && memcmp (target_mem, bpoint, len) == 0)
9120 retval = 1;
9121
9122 do_cleanups (cleanup);
9123
9124 return retval;
9125 }
9126
9127 /* Return 1 if LOC is pointing to a permanent breakpoint,
9128 return 0 otherwise. */
9129
9130 static int
9131 bp_loc_is_permanent (struct bp_location *loc)
9132 {
9133 struct cleanup *cleanup;
9134 int retval;
9135
9136 gdb_assert (loc != NULL);
9137
9138 /* If we have a catchpoint or a watchpoint, just return 0. We should not
9139 attempt to read from the addresses the locations of these breakpoint types
9140 point to. program_breakpoint_here_p, below, will attempt to read
9141 memory. */
9142 if (!breakpoint_address_is_meaningful (loc->owner))
9143 return 0;
9144
9145 cleanup = save_current_space_and_thread ();
9146 switch_to_program_space_and_thread (loc->pspace);
9147
9148 retval = program_breakpoint_here_p (loc->gdbarch, loc->address);
9149
9150 do_cleanups (cleanup);
9151
9152 return retval;
9153 }
9154
9155 /* Build a command list for the dprintf corresponding to the current
9156 settings of the dprintf style options. */
9157
9158 static void
9159 update_dprintf_command_list (struct breakpoint *b)
9160 {
9161 char *dprintf_args = b->extra_string;
9162 char *printf_line = NULL;
9163
9164 if (!dprintf_args)
9165 return;
9166
9167 dprintf_args = skip_spaces (dprintf_args);
9168
9169 /* Allow a comma, as it may have terminated a location, but don't
9170 insist on it. */
9171 if (*dprintf_args == ',')
9172 ++dprintf_args;
9173 dprintf_args = skip_spaces (dprintf_args);
9174
9175 if (*dprintf_args != '"')
9176 error (_("Bad format string, missing '\"'."));
9177
9178 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
9179 printf_line = xstrprintf ("printf %s", dprintf_args);
9180 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
9181 {
9182 if (!dprintf_function)
9183 error (_("No function supplied for dprintf call"));
9184
9185 if (dprintf_channel && strlen (dprintf_channel) > 0)
9186 printf_line = xstrprintf ("call (void) %s (%s,%s)",
9187 dprintf_function,
9188 dprintf_channel,
9189 dprintf_args);
9190 else
9191 printf_line = xstrprintf ("call (void) %s (%s)",
9192 dprintf_function,
9193 dprintf_args);
9194 }
9195 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
9196 {
9197 if (target_can_run_breakpoint_commands ())
9198 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
9199 else
9200 {
9201 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
9202 printf_line = xstrprintf ("printf %s", dprintf_args);
9203 }
9204 }
9205 else
9206 internal_error (__FILE__, __LINE__,
9207 _("Invalid dprintf style."));
9208
9209 gdb_assert (printf_line != NULL);
9210 /* Manufacture a printf sequence. */
9211 {
9212 struct command_line *printf_cmd_line = XNEW (struct command_line);
9213
9214 printf_cmd_line->control_type = simple_control;
9215 printf_cmd_line->body_count = 0;
9216 printf_cmd_line->body_list = NULL;
9217 printf_cmd_line->next = NULL;
9218 printf_cmd_line->line = printf_line;
9219
9220 breakpoint_set_commands (b, printf_cmd_line);
9221 }
9222 }
9223
9224 /* Update all dprintf commands, making their command lists reflect
9225 current style settings. */
9226
9227 static void
9228 update_dprintf_commands (char *args, int from_tty,
9229 struct cmd_list_element *c)
9230 {
9231 struct breakpoint *b;
9232
9233 ALL_BREAKPOINTS (b)
9234 {
9235 if (b->type == bp_dprintf)
9236 update_dprintf_command_list (b);
9237 }
9238 }
9239
9240 /* Create a breakpoint with SAL as location. Use LOCATION
9241 as a description of the location, and COND_STRING
9242 as condition expression. If LOCATION is NULL then create an
9243 "address location" from the address in the SAL. */
9244
9245 static void
9246 init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
9247 struct symtabs_and_lines sals,
9248 struct event_location *location,
9249 char *filter, char *cond_string,
9250 char *extra_string,
9251 enum bptype type, enum bpdisp disposition,
9252 int thread, int task, int ignore_count,
9253 const struct breakpoint_ops *ops, int from_tty,
9254 int enabled, int internal, unsigned flags,
9255 int display_canonical)
9256 {
9257 int i;
9258
9259 if (type == bp_hardware_breakpoint)
9260 {
9261 int target_resources_ok;
9262
9263 i = hw_breakpoint_used_count ();
9264 target_resources_ok =
9265 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9266 i + 1, 0);
9267 if (target_resources_ok == 0)
9268 error (_("No hardware breakpoint support in the target."));
9269 else if (target_resources_ok < 0)
9270 error (_("Hardware breakpoints used exceeds limit."));
9271 }
9272
9273 gdb_assert (sals.nelts > 0);
9274
9275 for (i = 0; i < sals.nelts; ++i)
9276 {
9277 struct symtab_and_line sal = sals.sals[i];
9278 struct bp_location *loc;
9279
9280 if (from_tty)
9281 {
9282 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9283 if (!loc_gdbarch)
9284 loc_gdbarch = gdbarch;
9285
9286 describe_other_breakpoints (loc_gdbarch,
9287 sal.pspace, sal.pc, sal.section, thread);
9288 }
9289
9290 if (i == 0)
9291 {
9292 init_raw_breakpoint (b, gdbarch, sal, type, ops);
9293 b->thread = thread;
9294 b->task = task;
9295
9296 b->cond_string = cond_string;
9297 b->extra_string = extra_string;
9298 b->ignore_count = ignore_count;
9299 b->enable_state = enabled ? bp_enabled : bp_disabled;
9300 b->disposition = disposition;
9301
9302 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9303 b->loc->inserted = 1;
9304
9305 if (type == bp_static_tracepoint)
9306 {
9307 struct tracepoint *t = (struct tracepoint *) b;
9308 struct static_tracepoint_marker marker;
9309
9310 if (strace_marker_p (b))
9311 {
9312 /* We already know the marker exists, otherwise, we
9313 wouldn't see a sal for it. */
9314 const char *p = &event_location_to_string (b->location)[3];
9315 const char *endp;
9316 char *marker_str;
9317
9318 p = skip_spaces_const (p);
9319
9320 endp = skip_to_space_const (p);
9321
9322 marker_str = savestring (p, endp - p);
9323 t->static_trace_marker_id = marker_str;
9324
9325 printf_filtered (_("Probed static tracepoint "
9326 "marker \"%s\"\n"),
9327 t->static_trace_marker_id);
9328 }
9329 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
9330 {
9331 t->static_trace_marker_id = xstrdup (marker.str_id);
9332 release_static_tracepoint_marker (&marker);
9333
9334 printf_filtered (_("Probed static tracepoint "
9335 "marker \"%s\"\n"),
9336 t->static_trace_marker_id);
9337 }
9338 else
9339 warning (_("Couldn't determine the static "
9340 "tracepoint marker to probe"));
9341 }
9342
9343 loc = b->loc;
9344 }
9345 else
9346 {
9347 loc = add_location_to_breakpoint (b, &sal);
9348 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9349 loc->inserted = 1;
9350 }
9351
9352 if (b->cond_string)
9353 {
9354 const char *arg = b->cond_string;
9355
9356 loc->cond = parse_exp_1 (&arg, loc->address,
9357 block_for_pc (loc->address), 0);
9358 if (*arg)
9359 error (_("Garbage '%s' follows condition"), arg);
9360 }
9361
9362 /* Dynamic printf requires and uses additional arguments on the
9363 command line, otherwise it's an error. */
9364 if (type == bp_dprintf)
9365 {
9366 if (b->extra_string)
9367 update_dprintf_command_list (b);
9368 else
9369 error (_("Format string required"));
9370 }
9371 else if (b->extra_string)
9372 error (_("Garbage '%s' at end of command"), b->extra_string);
9373 }
9374
9375 b->display_canonical = display_canonical;
9376 if (location != NULL)
9377 b->location = location;
9378 else
9379 {
9380 const char *addr_string = NULL;
9381 int addr_string_len = 0;
9382
9383 if (location != NULL)
9384 addr_string = event_location_to_string (location);
9385 if (addr_string != NULL)
9386 addr_string_len = strlen (addr_string);
9387
9388 b->location = new_address_location (b->loc->address,
9389 addr_string, addr_string_len);
9390 }
9391 b->filter = filter;
9392 }
9393
9394 static void
9395 create_breakpoint_sal (struct gdbarch *gdbarch,
9396 struct symtabs_and_lines sals,
9397 struct event_location *location,
9398 char *filter, char *cond_string,
9399 char *extra_string,
9400 enum bptype type, enum bpdisp disposition,
9401 int thread, int task, int ignore_count,
9402 const struct breakpoint_ops *ops, int from_tty,
9403 int enabled, int internal, unsigned flags,
9404 int display_canonical)
9405 {
9406 struct breakpoint *b;
9407 struct cleanup *old_chain;
9408
9409 if (is_tracepoint_type (type))
9410 {
9411 struct tracepoint *t;
9412
9413 t = new tracepoint ();
9414 b = &t->base;
9415 }
9416 else
9417 b = new breakpoint ();
9418
9419 old_chain = make_cleanup (xfree, b);
9420
9421 init_breakpoint_sal (b, gdbarch,
9422 sals, location,
9423 filter, cond_string, extra_string,
9424 type, disposition,
9425 thread, task, ignore_count,
9426 ops, from_tty,
9427 enabled, internal, flags,
9428 display_canonical);
9429 discard_cleanups (old_chain);
9430
9431 install_breakpoint (internal, b, 0);
9432 }
9433
9434 /* Add SALS.nelts breakpoints to the breakpoint table. For each
9435 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9436 value. COND_STRING, if not NULL, specified the condition to be
9437 used for all breakpoints. Essentially the only case where
9438 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9439 function. In that case, it's still not possible to specify
9440 separate conditions for different overloaded functions, so
9441 we take just a single condition string.
9442
9443 NOTE: If the function succeeds, the caller is expected to cleanup
9444 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
9445 array contents). If the function fails (error() is called), the
9446 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
9447 COND and SALS arrays and each of those arrays contents. */
9448
9449 static void
9450 create_breakpoints_sal (struct gdbarch *gdbarch,
9451 struct linespec_result *canonical,
9452 char *cond_string, char *extra_string,
9453 enum bptype type, enum bpdisp disposition,
9454 int thread, int task, int ignore_count,
9455 const struct breakpoint_ops *ops, int from_tty,
9456 int enabled, int internal, unsigned flags)
9457 {
9458 int i;
9459 struct linespec_sals *lsal;
9460
9461 if (canonical->pre_expanded)
9462 gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
9463
9464 for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
9465 {
9466 /* Note that 'location' can be NULL in the case of a plain
9467 'break', without arguments. */
9468 struct event_location *location
9469 = (canonical->location != NULL
9470 ? copy_event_location (canonical->location) : NULL);
9471 char *filter_string = lsal->canonical ? xstrdup (lsal->canonical) : NULL;
9472 struct cleanup *inner = make_cleanup_delete_event_location (location);
9473
9474 make_cleanup (xfree, filter_string);
9475 create_breakpoint_sal (gdbarch, lsal->sals,
9476 location,
9477 filter_string,
9478 cond_string, extra_string,
9479 type, disposition,
9480 thread, task, ignore_count, ops,
9481 from_tty, enabled, internal, flags,
9482 canonical->special_display);
9483 discard_cleanups (inner);
9484 }
9485 }
9486
9487 /* Parse LOCATION which is assumed to be a SAL specification possibly
9488 followed by conditionals. On return, SALS contains an array of SAL
9489 addresses found. LOCATION points to the end of the SAL (for
9490 linespec locations).
9491
9492 The array and the line spec strings are allocated on the heap, it is
9493 the caller's responsibility to free them. */
9494
9495 static void
9496 parse_breakpoint_sals (const struct event_location *location,
9497 struct linespec_result *canonical)
9498 {
9499 struct symtab_and_line cursal;
9500
9501 if (event_location_type (location) == LINESPEC_LOCATION)
9502 {
9503 const char *address = get_linespec_location (location);
9504
9505 if (address == NULL)
9506 {
9507 /* The last displayed codepoint, if it's valid, is our default
9508 breakpoint address. */
9509 if (last_displayed_sal_is_valid ())
9510 {
9511 struct linespec_sals lsal;
9512 struct symtab_and_line sal;
9513 CORE_ADDR pc;
9514
9515 init_sal (&sal); /* Initialize to zeroes. */
9516 lsal.sals.sals = XNEW (struct symtab_and_line);
9517
9518 /* Set sal's pspace, pc, symtab, and line to the values
9519 corresponding to the last call to print_frame_info.
9520 Be sure to reinitialize LINE with NOTCURRENT == 0
9521 as the breakpoint line number is inappropriate otherwise.
9522 find_pc_line would adjust PC, re-set it back. */
9523 get_last_displayed_sal (&sal);
9524 pc = sal.pc;
9525 sal = find_pc_line (pc, 0);
9526
9527 /* "break" without arguments is equivalent to "break *PC"
9528 where PC is the last displayed codepoint's address. So
9529 make sure to set sal.explicit_pc to prevent GDB from
9530 trying to expand the list of sals to include all other
9531 instances with the same symtab and line. */
9532 sal.pc = pc;
9533 sal.explicit_pc = 1;
9534
9535 lsal.sals.sals[0] = sal;
9536 lsal.sals.nelts = 1;
9537 lsal.canonical = NULL;
9538
9539 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
9540 return;
9541 }
9542 else
9543 error (_("No default breakpoint address now."));
9544 }
9545 }
9546
9547 /* Force almost all breakpoints to be in terms of the
9548 current_source_symtab (which is decode_line_1's default).
9549 This should produce the results we want almost all of the
9550 time while leaving default_breakpoint_* alone.
9551
9552 ObjC: However, don't match an Objective-C method name which
9553 may have a '+' or '-' succeeded by a '['. */
9554 cursal = get_current_source_symtab_and_line ();
9555 if (last_displayed_sal_is_valid ())
9556 {
9557 const char *address = NULL;
9558
9559 if (event_location_type (location) == LINESPEC_LOCATION)
9560 address = get_linespec_location (location);
9561
9562 if (!cursal.symtab
9563 || (address != NULL
9564 && strchr ("+-", address[0]) != NULL
9565 && address[1] != '['))
9566 {
9567 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9568 get_last_displayed_symtab (),
9569 get_last_displayed_line (),
9570 canonical, NULL, NULL);
9571 return;
9572 }
9573 }
9574
9575 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9576 cursal.symtab, cursal.line, canonical, NULL, NULL);
9577 }
9578
9579
9580 /* Convert each SAL into a real PC. Verify that the PC can be
9581 inserted as a breakpoint. If it can't throw an error. */
9582
9583 static void
9584 breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
9585 {
9586 int i;
9587
9588 for (i = 0; i < sals->nelts; i++)
9589 resolve_sal_pc (&sals->sals[i]);
9590 }
9591
9592 /* Fast tracepoints may have restrictions on valid locations. For
9593 instance, a fast tracepoint using a jump instead of a trap will
9594 likely have to overwrite more bytes than a trap would, and so can
9595 only be placed where the instruction is longer than the jump, or a
9596 multi-instruction sequence does not have a jump into the middle of
9597 it, etc. */
9598
9599 static void
9600 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9601 struct symtabs_and_lines *sals)
9602 {
9603 int i, rslt;
9604 struct symtab_and_line *sal;
9605 char *msg;
9606 struct cleanup *old_chain;
9607
9608 for (i = 0; i < sals->nelts; i++)
9609 {
9610 struct gdbarch *sarch;
9611
9612 sal = &sals->sals[i];
9613
9614 sarch = get_sal_arch (*sal);
9615 /* We fall back to GDBARCH if there is no architecture
9616 associated with SAL. */
9617 if (sarch == NULL)
9618 sarch = gdbarch;
9619 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc, &msg);
9620 old_chain = make_cleanup (xfree, msg);
9621
9622 if (!rslt)
9623 error (_("May not have a fast tracepoint at %s%s"),
9624 paddress (sarch, sal->pc), (msg ? msg : ""));
9625
9626 do_cleanups (old_chain);
9627 }
9628 }
9629
9630 /* Given TOK, a string specification of condition and thread, as
9631 accepted by the 'break' command, extract the condition
9632 string and thread number and set *COND_STRING and *THREAD.
9633 PC identifies the context at which the condition should be parsed.
9634 If no condition is found, *COND_STRING is set to NULL.
9635 If no thread is found, *THREAD is set to -1. */
9636
9637 static void
9638 find_condition_and_thread (const char *tok, CORE_ADDR pc,
9639 char **cond_string, int *thread, int *task,
9640 char **rest)
9641 {
9642 *cond_string = NULL;
9643 *thread = -1;
9644 *task = 0;
9645 *rest = NULL;
9646
9647 while (tok && *tok)
9648 {
9649 const char *end_tok;
9650 int toklen;
9651 const char *cond_start = NULL;
9652 const char *cond_end = NULL;
9653
9654 tok = skip_spaces_const (tok);
9655
9656 if ((*tok == '"' || *tok == ',') && rest)
9657 {
9658 *rest = savestring (tok, strlen (tok));
9659 return;
9660 }
9661
9662 end_tok = skip_to_space_const (tok);
9663
9664 toklen = end_tok - tok;
9665
9666 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9667 {
9668 tok = cond_start = end_tok + 1;
9669 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
9670 cond_end = tok;
9671 *cond_string = savestring (cond_start, cond_end - cond_start);
9672 }
9673 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9674 {
9675 const char *tmptok;
9676 struct thread_info *thr;
9677
9678 tok = end_tok + 1;
9679 thr = parse_thread_id (tok, &tmptok);
9680 if (tok == tmptok)
9681 error (_("Junk after thread keyword."));
9682 *thread = thr->global_num;
9683 tok = tmptok;
9684 }
9685 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9686 {
9687 char *tmptok;
9688
9689 tok = end_tok + 1;
9690 *task = strtol (tok, &tmptok, 0);
9691 if (tok == tmptok)
9692 error (_("Junk after task keyword."));
9693 if (!valid_task_id (*task))
9694 error (_("Unknown task %d."), *task);
9695 tok = tmptok;
9696 }
9697 else if (rest)
9698 {
9699 *rest = savestring (tok, strlen (tok));
9700 return;
9701 }
9702 else
9703 error (_("Junk at end of arguments."));
9704 }
9705 }
9706
9707 /* Decode a static tracepoint marker spec. */
9708
9709 static struct symtabs_and_lines
9710 decode_static_tracepoint_spec (const char **arg_p)
9711 {
9712 VEC(static_tracepoint_marker_p) *markers = NULL;
9713 struct symtabs_and_lines sals;
9714 struct cleanup *old_chain;
9715 const char *p = &(*arg_p)[3];
9716 const char *endp;
9717 char *marker_str;
9718 int i;
9719
9720 p = skip_spaces_const (p);
9721
9722 endp = skip_to_space_const (p);
9723
9724 marker_str = savestring (p, endp - p);
9725 old_chain = make_cleanup (xfree, marker_str);
9726
9727 markers = target_static_tracepoint_markers_by_strid (marker_str);
9728 if (VEC_empty(static_tracepoint_marker_p, markers))
9729 error (_("No known static tracepoint marker named %s"), marker_str);
9730
9731 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
9732 sals.sals = XNEWVEC (struct symtab_and_line, sals.nelts);
9733
9734 for (i = 0; i < sals.nelts; i++)
9735 {
9736 struct static_tracepoint_marker *marker;
9737
9738 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9739
9740 init_sal (&sals.sals[i]);
9741
9742 sals.sals[i] = find_pc_line (marker->address, 0);
9743 sals.sals[i].pc = marker->address;
9744
9745 release_static_tracepoint_marker (marker);
9746 }
9747
9748 do_cleanups (old_chain);
9749
9750 *arg_p = endp;
9751 return sals;
9752 }
9753
9754 /* See breakpoint.h. */
9755
9756 int
9757 create_breakpoint (struct gdbarch *gdbarch,
9758 const struct event_location *location, char *cond_string,
9759 int thread, char *extra_string,
9760 int parse_extra,
9761 int tempflag, enum bptype type_wanted,
9762 int ignore_count,
9763 enum auto_boolean pending_break_support,
9764 const struct breakpoint_ops *ops,
9765 int from_tty, int enabled, int internal,
9766 unsigned flags)
9767 {
9768 struct linespec_result canonical;
9769 struct cleanup *old_chain;
9770 struct cleanup *bkpt_chain = NULL;
9771 int pending = 0;
9772 int task = 0;
9773 int prev_bkpt_count = breakpoint_count;
9774
9775 gdb_assert (ops != NULL);
9776
9777 /* If extra_string isn't useful, set it to NULL. */
9778 if (extra_string != NULL && *extra_string == '\0')
9779 extra_string = NULL;
9780
9781 init_linespec_result (&canonical);
9782
9783 TRY
9784 {
9785 ops->create_sals_from_location (location, &canonical, type_wanted);
9786 }
9787 CATCH (e, RETURN_MASK_ERROR)
9788 {
9789 /* If caller is interested in rc value from parse, set
9790 value. */
9791 if (e.error == NOT_FOUND_ERROR)
9792 {
9793 /* If pending breakpoint support is turned off, throw
9794 error. */
9795
9796 if (pending_break_support == AUTO_BOOLEAN_FALSE)
9797 throw_exception (e);
9798
9799 exception_print (gdb_stderr, e);
9800
9801 /* If pending breakpoint support is auto query and the user
9802 selects no, then simply return the error code. */
9803 if (pending_break_support == AUTO_BOOLEAN_AUTO
9804 && !nquery (_("Make %s pending on future shared library load? "),
9805 bptype_string (type_wanted)))
9806 return 0;
9807
9808 /* At this point, either the user was queried about setting
9809 a pending breakpoint and selected yes, or pending
9810 breakpoint behavior is on and thus a pending breakpoint
9811 is defaulted on behalf of the user. */
9812 pending = 1;
9813 }
9814 else
9815 throw_exception (e);
9816 }
9817 END_CATCH
9818
9819 if (!pending && VEC_empty (linespec_sals, canonical.sals))
9820 return 0;
9821
9822 /* Create a chain of things that always need to be cleaned up. */
9823 old_chain = make_cleanup_destroy_linespec_result (&canonical);
9824
9825 /* ----------------------------- SNIP -----------------------------
9826 Anything added to the cleanup chain beyond this point is assumed
9827 to be part of a breakpoint. If the breakpoint create succeeds
9828 then the memory is not reclaimed. */
9829 bkpt_chain = make_cleanup (null_cleanup, 0);
9830
9831 /* Resolve all line numbers to PC's and verify that the addresses
9832 are ok for the target. */
9833 if (!pending)
9834 {
9835 int ix;
9836 struct linespec_sals *iter;
9837
9838 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9839 breakpoint_sals_to_pc (&iter->sals);
9840 }
9841
9842 /* Fast tracepoints may have additional restrictions on location. */
9843 if (!pending && type_wanted == bp_fast_tracepoint)
9844 {
9845 int ix;
9846 struct linespec_sals *iter;
9847
9848 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9849 check_fast_tracepoint_sals (gdbarch, &iter->sals);
9850 }
9851
9852 /* Verify that condition can be parsed, before setting any
9853 breakpoints. Allocate a separate condition expression for each
9854 breakpoint. */
9855 if (!pending)
9856 {
9857 if (parse_extra)
9858 {
9859 char *rest;
9860 struct linespec_sals *lsal;
9861
9862 lsal = VEC_index (linespec_sals, canonical.sals, 0);
9863
9864 /* Here we only parse 'arg' to separate condition
9865 from thread number, so parsing in context of first
9866 sal is OK. When setting the breakpoint we'll
9867 re-parse it in context of each sal. */
9868
9869 find_condition_and_thread (extra_string, lsal->sals.sals[0].pc,
9870 &cond_string, &thread, &task, &rest);
9871 if (cond_string)
9872 make_cleanup (xfree, cond_string);
9873 if (rest)
9874 make_cleanup (xfree, rest);
9875 if (rest)
9876 extra_string = rest;
9877 else
9878 extra_string = NULL;
9879 }
9880 else
9881 {
9882 if (type_wanted != bp_dprintf
9883 && extra_string != NULL && *extra_string != '\0')
9884 error (_("Garbage '%s' at end of location"), extra_string);
9885
9886 /* Create a private copy of condition string. */
9887 if (cond_string)
9888 {
9889 cond_string = xstrdup (cond_string);
9890 make_cleanup (xfree, cond_string);
9891 }
9892 /* Create a private copy of any extra string. */
9893 if (extra_string)
9894 {
9895 extra_string = xstrdup (extra_string);
9896 make_cleanup (xfree, extra_string);
9897 }
9898 }
9899
9900 ops->create_breakpoints_sal (gdbarch, &canonical,
9901 cond_string, extra_string, type_wanted,
9902 tempflag ? disp_del : disp_donttouch,
9903 thread, task, ignore_count, ops,
9904 from_tty, enabled, internal, flags);
9905 }
9906 else
9907 {
9908 struct breakpoint *b;
9909
9910 if (is_tracepoint_type (type_wanted))
9911 {
9912 struct tracepoint *t;
9913
9914 t = new tracepoint ();
9915 b = &t->base;
9916 }
9917 else
9918 b = new breakpoint ();
9919
9920 init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops);
9921 b->location = copy_event_location (location);
9922
9923 if (parse_extra)
9924 b->cond_string = NULL;
9925 else
9926 {
9927 /* Create a private copy of condition string. */
9928 if (cond_string)
9929 {
9930 cond_string = xstrdup (cond_string);
9931 make_cleanup (xfree, cond_string);
9932 }
9933 b->cond_string = cond_string;
9934 b->thread = thread;
9935 }
9936
9937 /* Create a private copy of any extra string. */
9938 if (extra_string != NULL)
9939 {
9940 extra_string = xstrdup (extra_string);
9941 make_cleanup (xfree, extra_string);
9942 }
9943 b->extra_string = extra_string;
9944 b->ignore_count = ignore_count;
9945 b->disposition = tempflag ? disp_del : disp_donttouch;
9946 b->condition_not_parsed = 1;
9947 b->enable_state = enabled ? bp_enabled : bp_disabled;
9948 if ((type_wanted != bp_breakpoint
9949 && type_wanted != bp_hardware_breakpoint) || thread != -1)
9950 b->pspace = current_program_space;
9951
9952 install_breakpoint (internal, b, 0);
9953 }
9954
9955 if (VEC_length (linespec_sals, canonical.sals) > 1)
9956 {
9957 warning (_("Multiple breakpoints were set.\nUse the "
9958 "\"delete\" command to delete unwanted breakpoints."));
9959 prev_breakpoint_count = prev_bkpt_count;
9960 }
9961
9962 /* That's it. Discard the cleanups for data inserted into the
9963 breakpoint. */
9964 discard_cleanups (bkpt_chain);
9965 /* But cleanup everything else. */
9966 do_cleanups (old_chain);
9967
9968 /* error call may happen here - have BKPT_CHAIN already discarded. */
9969 update_global_location_list (UGLL_MAY_INSERT);
9970
9971 return 1;
9972 }
9973
9974 /* Set a breakpoint.
9975 ARG is a string describing breakpoint address,
9976 condition, and thread.
9977 FLAG specifies if a breakpoint is hardware on,
9978 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9979 and BP_TEMPFLAG. */
9980
9981 static void
9982 break_command_1 (char *arg, int flag, int from_tty)
9983 {
9984 int tempflag = flag & BP_TEMPFLAG;
9985 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9986 ? bp_hardware_breakpoint
9987 : bp_breakpoint);
9988 struct breakpoint_ops *ops;
9989 struct event_location *location;
9990 struct cleanup *cleanup;
9991
9992 location = string_to_event_location (&arg, current_language);
9993 cleanup = make_cleanup_delete_event_location (location);
9994
9995 /* Matching breakpoints on probes. */
9996 if (location != NULL
9997 && event_location_type (location) == PROBE_LOCATION)
9998 ops = &bkpt_probe_breakpoint_ops;
9999 else
10000 ops = &bkpt_breakpoint_ops;
10001
10002 create_breakpoint (get_current_arch (),
10003 location,
10004 NULL, 0, arg, 1 /* parse arg */,
10005 tempflag, type_wanted,
10006 0 /* Ignore count */,
10007 pending_break_support,
10008 ops,
10009 from_tty,
10010 1 /* enabled */,
10011 0 /* internal */,
10012 0);
10013 do_cleanups (cleanup);
10014 }
10015
10016 /* Helper function for break_command_1 and disassemble_command. */
10017
10018 void
10019 resolve_sal_pc (struct symtab_and_line *sal)
10020 {
10021 CORE_ADDR pc;
10022
10023 if (sal->pc == 0 && sal->symtab != NULL)
10024 {
10025 if (!find_line_pc (sal->symtab, sal->line, &pc))
10026 error (_("No line %d in file \"%s\"."),
10027 sal->line, symtab_to_filename_for_display (sal->symtab));
10028 sal->pc = pc;
10029
10030 /* If this SAL corresponds to a breakpoint inserted using a line
10031 number, then skip the function prologue if necessary. */
10032 if (sal->explicit_line)
10033 skip_prologue_sal (sal);
10034 }
10035
10036 if (sal->section == 0 && sal->symtab != NULL)
10037 {
10038 const struct blockvector *bv;
10039 const struct block *b;
10040 struct symbol *sym;
10041
10042 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
10043 SYMTAB_COMPUNIT (sal->symtab));
10044 if (bv != NULL)
10045 {
10046 sym = block_linkage_function (b);
10047 if (sym != NULL)
10048 {
10049 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
10050 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
10051 sym);
10052 }
10053 else
10054 {
10055 /* It really is worthwhile to have the section, so we'll
10056 just have to look harder. This case can be executed
10057 if we have line numbers but no functions (as can
10058 happen in assembly source). */
10059
10060 struct bound_minimal_symbol msym;
10061 struct cleanup *old_chain = save_current_space_and_thread ();
10062
10063 switch_to_program_space_and_thread (sal->pspace);
10064
10065 msym = lookup_minimal_symbol_by_pc (sal->pc);
10066 if (msym.minsym)
10067 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
10068
10069 do_cleanups (old_chain);
10070 }
10071 }
10072 }
10073 }
10074
10075 void
10076 break_command (char *arg, int from_tty)
10077 {
10078 break_command_1 (arg, 0, from_tty);
10079 }
10080
10081 void
10082 tbreak_command (char *arg, int from_tty)
10083 {
10084 break_command_1 (arg, BP_TEMPFLAG, from_tty);
10085 }
10086
10087 static void
10088 hbreak_command (char *arg, int from_tty)
10089 {
10090 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
10091 }
10092
10093 static void
10094 thbreak_command (char *arg, int from_tty)
10095 {
10096 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
10097 }
10098
10099 static void
10100 stop_command (char *arg, int from_tty)
10101 {
10102 printf_filtered (_("Specify the type of breakpoint to set.\n\
10103 Usage: stop in <function | address>\n\
10104 stop at <line>\n"));
10105 }
10106
10107 static void
10108 stopin_command (char *arg, int from_tty)
10109 {
10110 int badInput = 0;
10111
10112 if (arg == (char *) NULL)
10113 badInput = 1;
10114 else if (*arg != '*')
10115 {
10116 char *argptr = arg;
10117 int hasColon = 0;
10118
10119 /* Look for a ':'. If this is a line number specification, then
10120 say it is bad, otherwise, it should be an address or
10121 function/method name. */
10122 while (*argptr && !hasColon)
10123 {
10124 hasColon = (*argptr == ':');
10125 argptr++;
10126 }
10127
10128 if (hasColon)
10129 badInput = (*argptr != ':'); /* Not a class::method */
10130 else
10131 badInput = isdigit (*arg); /* a simple line number */
10132 }
10133
10134 if (badInput)
10135 printf_filtered (_("Usage: stop in <function | address>\n"));
10136 else
10137 break_command_1 (arg, 0, from_tty);
10138 }
10139
10140 static void
10141 stopat_command (char *arg, int from_tty)
10142 {
10143 int badInput = 0;
10144
10145 if (arg == (char *) NULL || *arg == '*') /* no line number */
10146 badInput = 1;
10147 else
10148 {
10149 char *argptr = arg;
10150 int hasColon = 0;
10151
10152 /* Look for a ':'. If there is a '::' then get out, otherwise
10153 it is probably a line number. */
10154 while (*argptr && !hasColon)
10155 {
10156 hasColon = (*argptr == ':');
10157 argptr++;
10158 }
10159
10160 if (hasColon)
10161 badInput = (*argptr == ':'); /* we have class::method */
10162 else
10163 badInput = !isdigit (*arg); /* not a line number */
10164 }
10165
10166 if (badInput)
10167 printf_filtered (_("Usage: stop at <line>\n"));
10168 else
10169 break_command_1 (arg, 0, from_tty);
10170 }
10171
10172 /* The dynamic printf command is mostly like a regular breakpoint, but
10173 with a prewired command list consisting of a single output command,
10174 built from extra arguments supplied on the dprintf command
10175 line. */
10176
10177 static void
10178 dprintf_command (char *arg, int from_tty)
10179 {
10180 struct event_location *location;
10181 struct cleanup *cleanup;
10182
10183 location = string_to_event_location (&arg, current_language);
10184 cleanup = make_cleanup_delete_event_location (location);
10185
10186 /* If non-NULL, ARG should have been advanced past the location;
10187 the next character must be ','. */
10188 if (arg != NULL)
10189 {
10190 if (arg[0] != ',' || arg[1] == '\0')
10191 error (_("Format string required"));
10192 else
10193 {
10194 /* Skip the comma. */
10195 ++arg;
10196 }
10197 }
10198
10199 create_breakpoint (get_current_arch (),
10200 location,
10201 NULL, 0, arg, 1 /* parse arg */,
10202 0, bp_dprintf,
10203 0 /* Ignore count */,
10204 pending_break_support,
10205 &dprintf_breakpoint_ops,
10206 from_tty,
10207 1 /* enabled */,
10208 0 /* internal */,
10209 0);
10210 do_cleanups (cleanup);
10211 }
10212
10213 static void
10214 agent_printf_command (char *arg, int from_tty)
10215 {
10216 error (_("May only run agent-printf on the target"));
10217 }
10218
10219 /* Implement the "breakpoint_hit" breakpoint_ops method for
10220 ranged breakpoints. */
10221
10222 static int
10223 breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
10224 struct address_space *aspace,
10225 CORE_ADDR bp_addr,
10226 const struct target_waitstatus *ws)
10227 {
10228 if (ws->kind != TARGET_WAITKIND_STOPPED
10229 || ws->value.sig != GDB_SIGNAL_TRAP)
10230 return 0;
10231
10232 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
10233 bl->length, aspace, bp_addr);
10234 }
10235
10236 /* Implement the "resources_needed" breakpoint_ops method for
10237 ranged breakpoints. */
10238
10239 static int
10240 resources_needed_ranged_breakpoint (const struct bp_location *bl)
10241 {
10242 return target_ranged_break_num_registers ();
10243 }
10244
10245 /* Implement the "print_it" breakpoint_ops method for
10246 ranged breakpoints. */
10247
10248 static enum print_stop_action
10249 print_it_ranged_breakpoint (bpstat bs)
10250 {
10251 struct breakpoint *b = bs->breakpoint_at;
10252 struct bp_location *bl = b->loc;
10253 struct ui_out *uiout = current_uiout;
10254
10255 gdb_assert (b->type == bp_hardware_breakpoint);
10256
10257 /* Ranged breakpoints have only one location. */
10258 gdb_assert (bl && bl->next == NULL);
10259
10260 annotate_breakpoint (b->number);
10261
10262 maybe_print_thread_hit_breakpoint (uiout);
10263
10264 if (b->disposition == disp_del)
10265 ui_out_text (uiout, "Temporary ranged breakpoint ");
10266 else
10267 ui_out_text (uiout, "Ranged breakpoint ");
10268 if (ui_out_is_mi_like_p (uiout))
10269 {
10270 ui_out_field_string (uiout, "reason",
10271 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
10272 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
10273 }
10274 ui_out_field_int (uiout, "bkptno", b->number);
10275 ui_out_text (uiout, ", ");
10276
10277 return PRINT_SRC_AND_LOC;
10278 }
10279
10280 /* Implement the "print_one" breakpoint_ops method for
10281 ranged breakpoints. */
10282
10283 static void
10284 print_one_ranged_breakpoint (struct breakpoint *b,
10285 struct bp_location **last_loc)
10286 {
10287 struct bp_location *bl = b->loc;
10288 struct value_print_options opts;
10289 struct ui_out *uiout = current_uiout;
10290
10291 /* Ranged breakpoints have only one location. */
10292 gdb_assert (bl && bl->next == NULL);
10293
10294 get_user_print_options (&opts);
10295
10296 if (opts.addressprint)
10297 /* We don't print the address range here, it will be printed later
10298 by print_one_detail_ranged_breakpoint. */
10299 ui_out_field_skip (uiout, "addr");
10300 annotate_field (5);
10301 print_breakpoint_location (b, bl);
10302 *last_loc = bl;
10303 }
10304
10305 /* Implement the "print_one_detail" breakpoint_ops method for
10306 ranged breakpoints. */
10307
10308 static void
10309 print_one_detail_ranged_breakpoint (const struct breakpoint *b,
10310 struct ui_out *uiout)
10311 {
10312 CORE_ADDR address_start, address_end;
10313 struct bp_location *bl = b->loc;
10314 struct ui_file *stb = mem_fileopen ();
10315 struct cleanup *cleanup = make_cleanup_ui_file_delete (stb);
10316
10317 gdb_assert (bl);
10318
10319 address_start = bl->address;
10320 address_end = address_start + bl->length - 1;
10321
10322 ui_out_text (uiout, "\taddress range: ");
10323 fprintf_unfiltered (stb, "[%s, %s]",
10324 print_core_address (bl->gdbarch, address_start),
10325 print_core_address (bl->gdbarch, address_end));
10326 ui_out_field_stream (uiout, "addr", stb);
10327 ui_out_text (uiout, "\n");
10328
10329 do_cleanups (cleanup);
10330 }
10331
10332 /* Implement the "print_mention" breakpoint_ops method for
10333 ranged breakpoints. */
10334
10335 static void
10336 print_mention_ranged_breakpoint (struct breakpoint *b)
10337 {
10338 struct bp_location *bl = b->loc;
10339 struct ui_out *uiout = current_uiout;
10340
10341 gdb_assert (bl);
10342 gdb_assert (b->type == bp_hardware_breakpoint);
10343
10344 if (ui_out_is_mi_like_p (uiout))
10345 return;
10346
10347 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
10348 b->number, paddress (bl->gdbarch, bl->address),
10349 paddress (bl->gdbarch, bl->address + bl->length - 1));
10350 }
10351
10352 /* Implement the "print_recreate" breakpoint_ops method for
10353 ranged breakpoints. */
10354
10355 static void
10356 print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
10357 {
10358 fprintf_unfiltered (fp, "break-range %s, %s",
10359 event_location_to_string (b->location),
10360 event_location_to_string (b->location_range_end));
10361 print_recreate_thread (b, fp);
10362 }
10363
10364 /* The breakpoint_ops structure to be used in ranged breakpoints. */
10365
10366 static struct breakpoint_ops ranged_breakpoint_ops;
10367
10368 /* Find the address where the end of the breakpoint range should be
10369 placed, given the SAL of the end of the range. This is so that if
10370 the user provides a line number, the end of the range is set to the
10371 last instruction of the given line. */
10372
10373 static CORE_ADDR
10374 find_breakpoint_range_end (struct symtab_and_line sal)
10375 {
10376 CORE_ADDR end;
10377
10378 /* If the user provided a PC value, use it. Otherwise,
10379 find the address of the end of the given location. */
10380 if (sal.explicit_pc)
10381 end = sal.pc;
10382 else
10383 {
10384 int ret;
10385 CORE_ADDR start;
10386
10387 ret = find_line_pc_range (sal, &start, &end);
10388 if (!ret)
10389 error (_("Could not find location of the end of the range."));
10390
10391 /* find_line_pc_range returns the start of the next line. */
10392 end--;
10393 }
10394
10395 return end;
10396 }
10397
10398 /* Implement the "break-range" CLI command. */
10399
10400 static void
10401 break_range_command (char *arg, int from_tty)
10402 {
10403 char *arg_start, *addr_string_start;
10404 struct linespec_result canonical_start, canonical_end;
10405 int bp_count, can_use_bp, length;
10406 CORE_ADDR end;
10407 struct breakpoint *b;
10408 struct symtab_and_line sal_start, sal_end;
10409 struct cleanup *cleanup_bkpt;
10410 struct linespec_sals *lsal_start, *lsal_end;
10411 struct event_location *start_location, *end_location;
10412
10413 /* We don't support software ranged breakpoints. */
10414 if (target_ranged_break_num_registers () < 0)
10415 error (_("This target does not support hardware ranged breakpoints."));
10416
10417 bp_count = hw_breakpoint_used_count ();
10418 bp_count += target_ranged_break_num_registers ();
10419 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10420 bp_count, 0);
10421 if (can_use_bp < 0)
10422 error (_("Hardware breakpoints used exceeds limit."));
10423
10424 arg = skip_spaces (arg);
10425 if (arg == NULL || arg[0] == '\0')
10426 error(_("No address range specified."));
10427
10428 init_linespec_result (&canonical_start);
10429
10430 arg_start = arg;
10431 start_location = string_to_event_location (&arg, current_language);
10432 cleanup_bkpt = make_cleanup_delete_event_location (start_location);
10433 parse_breakpoint_sals (start_location, &canonical_start);
10434 make_cleanup_destroy_linespec_result (&canonical_start);
10435
10436 if (arg[0] != ',')
10437 error (_("Too few arguments."));
10438 else if (VEC_empty (linespec_sals, canonical_start.sals))
10439 error (_("Could not find location of the beginning of the range."));
10440
10441 lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
10442
10443 if (VEC_length (linespec_sals, canonical_start.sals) > 1
10444 || lsal_start->sals.nelts != 1)
10445 error (_("Cannot create a ranged breakpoint with multiple locations."));
10446
10447 sal_start = lsal_start->sals.sals[0];
10448 addr_string_start = savestring (arg_start, arg - arg_start);
10449 make_cleanup (xfree, addr_string_start);
10450
10451 arg++; /* Skip the comma. */
10452 arg = skip_spaces (arg);
10453
10454 /* Parse the end location. */
10455
10456 init_linespec_result (&canonical_end);
10457 arg_start = arg;
10458
10459 /* We call decode_line_full directly here instead of using
10460 parse_breakpoint_sals because we need to specify the start location's
10461 symtab and line as the default symtab and line for the end of the
10462 range. This makes it possible to have ranges like "foo.c:27, +14",
10463 where +14 means 14 lines from the start location. */
10464 end_location = string_to_event_location (&arg, current_language);
10465 make_cleanup_delete_event_location (end_location);
10466 decode_line_full (end_location, DECODE_LINE_FUNFIRSTLINE, NULL,
10467 sal_start.symtab, sal_start.line,
10468 &canonical_end, NULL, NULL);
10469
10470 make_cleanup_destroy_linespec_result (&canonical_end);
10471
10472 if (VEC_empty (linespec_sals, canonical_end.sals))
10473 error (_("Could not find location of the end of the range."));
10474
10475 lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
10476 if (VEC_length (linespec_sals, canonical_end.sals) > 1
10477 || lsal_end->sals.nelts != 1)
10478 error (_("Cannot create a ranged breakpoint with multiple locations."));
10479
10480 sal_end = lsal_end->sals.sals[0];
10481
10482 end = find_breakpoint_range_end (sal_end);
10483 if (sal_start.pc > end)
10484 error (_("Invalid address range, end precedes start."));
10485
10486 length = end - sal_start.pc + 1;
10487 if (length < 0)
10488 /* Length overflowed. */
10489 error (_("Address range too large."));
10490 else if (length == 1)
10491 {
10492 /* This range is simple enough to be handled by
10493 the `hbreak' command. */
10494 hbreak_command (addr_string_start, 1);
10495
10496 do_cleanups (cleanup_bkpt);
10497
10498 return;
10499 }
10500
10501 /* Now set up the breakpoint. */
10502 b = set_raw_breakpoint (get_current_arch (), sal_start,
10503 bp_hardware_breakpoint, &ranged_breakpoint_ops);
10504 set_breakpoint_count (breakpoint_count + 1);
10505 b->number = breakpoint_count;
10506 b->disposition = disp_donttouch;
10507 b->location = copy_event_location (start_location);
10508 b->location_range_end = copy_event_location (end_location);
10509 b->loc->length = length;
10510
10511 do_cleanups (cleanup_bkpt);
10512
10513 mention (b);
10514 observer_notify_breakpoint_created (b);
10515 update_global_location_list (UGLL_MAY_INSERT);
10516 }
10517
10518 /* Return non-zero if EXP is verified as constant. Returned zero
10519 means EXP is variable. Also the constant detection may fail for
10520 some constant expressions and in such case still falsely return
10521 zero. */
10522
10523 static int
10524 watchpoint_exp_is_const (const struct expression *exp)
10525 {
10526 int i = exp->nelts;
10527
10528 while (i > 0)
10529 {
10530 int oplenp, argsp;
10531
10532 /* We are only interested in the descriptor of each element. */
10533 operator_length (exp, i, &oplenp, &argsp);
10534 i -= oplenp;
10535
10536 switch (exp->elts[i].opcode)
10537 {
10538 case BINOP_ADD:
10539 case BINOP_SUB:
10540 case BINOP_MUL:
10541 case BINOP_DIV:
10542 case BINOP_REM:
10543 case BINOP_MOD:
10544 case BINOP_LSH:
10545 case BINOP_RSH:
10546 case BINOP_LOGICAL_AND:
10547 case BINOP_LOGICAL_OR:
10548 case BINOP_BITWISE_AND:
10549 case BINOP_BITWISE_IOR:
10550 case BINOP_BITWISE_XOR:
10551 case BINOP_EQUAL:
10552 case BINOP_NOTEQUAL:
10553 case BINOP_LESS:
10554 case BINOP_GTR:
10555 case BINOP_LEQ:
10556 case BINOP_GEQ:
10557 case BINOP_REPEAT:
10558 case BINOP_COMMA:
10559 case BINOP_EXP:
10560 case BINOP_MIN:
10561 case BINOP_MAX:
10562 case BINOP_INTDIV:
10563 case BINOP_CONCAT:
10564 case TERNOP_COND:
10565 case TERNOP_SLICE:
10566
10567 case OP_LONG:
10568 case OP_DOUBLE:
10569 case OP_DECFLOAT:
10570 case OP_LAST:
10571 case OP_COMPLEX:
10572 case OP_STRING:
10573 case OP_ARRAY:
10574 case OP_TYPE:
10575 case OP_TYPEOF:
10576 case OP_DECLTYPE:
10577 case OP_TYPEID:
10578 case OP_NAME:
10579 case OP_OBJC_NSSTRING:
10580
10581 case UNOP_NEG:
10582 case UNOP_LOGICAL_NOT:
10583 case UNOP_COMPLEMENT:
10584 case UNOP_ADDR:
10585 case UNOP_HIGH:
10586 case UNOP_CAST:
10587
10588 case UNOP_CAST_TYPE:
10589 case UNOP_REINTERPRET_CAST:
10590 case UNOP_DYNAMIC_CAST:
10591 /* Unary, binary and ternary operators: We have to check
10592 their operands. If they are constant, then so is the
10593 result of that operation. For instance, if A and B are
10594 determined to be constants, then so is "A + B".
10595
10596 UNOP_IND is one exception to the rule above, because the
10597 value of *ADDR is not necessarily a constant, even when
10598 ADDR is. */
10599 break;
10600
10601 case OP_VAR_VALUE:
10602 /* Check whether the associated symbol is a constant.
10603
10604 We use SYMBOL_CLASS rather than TYPE_CONST because it's
10605 possible that a buggy compiler could mark a variable as
10606 constant even when it is not, and TYPE_CONST would return
10607 true in this case, while SYMBOL_CLASS wouldn't.
10608
10609 We also have to check for function symbols because they
10610 are always constant. */
10611 {
10612 struct symbol *s = exp->elts[i + 2].symbol;
10613
10614 if (SYMBOL_CLASS (s) != LOC_BLOCK
10615 && SYMBOL_CLASS (s) != LOC_CONST
10616 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10617 return 0;
10618 break;
10619 }
10620
10621 /* The default action is to return 0 because we are using
10622 the optimistic approach here: If we don't know something,
10623 then it is not a constant. */
10624 default:
10625 return 0;
10626 }
10627 }
10628
10629 return 1;
10630 }
10631
10632 /* Implement the "dtor" breakpoint_ops method for watchpoints. */
10633
10634 static void
10635 dtor_watchpoint (struct breakpoint *self)
10636 {
10637 struct watchpoint *w = (struct watchpoint *) self;
10638
10639 xfree (w->exp_string);
10640 xfree (w->exp_string_reparse);
10641 value_free (w->val);
10642
10643 base_breakpoint_ops.dtor (self);
10644 }
10645
10646 /* Implement the "re_set" breakpoint_ops method for watchpoints. */
10647
10648 static void
10649 re_set_watchpoint (struct breakpoint *b)
10650 {
10651 struct watchpoint *w = (struct watchpoint *) b;
10652
10653 /* Watchpoint can be either on expression using entirely global
10654 variables, or it can be on local variables.
10655
10656 Watchpoints of the first kind are never auto-deleted, and even
10657 persist across program restarts. Since they can use variables
10658 from shared libraries, we need to reparse expression as libraries
10659 are loaded and unloaded.
10660
10661 Watchpoints on local variables can also change meaning as result
10662 of solib event. For example, if a watchpoint uses both a local
10663 and a global variables in expression, it's a local watchpoint,
10664 but unloading of a shared library will make the expression
10665 invalid. This is not a very common use case, but we still
10666 re-evaluate expression, to avoid surprises to the user.
10667
10668 Note that for local watchpoints, we re-evaluate it only if
10669 watchpoints frame id is still valid. If it's not, it means the
10670 watchpoint is out of scope and will be deleted soon. In fact,
10671 I'm not sure we'll ever be called in this case.
10672
10673 If a local watchpoint's frame id is still valid, then
10674 w->exp_valid_block is likewise valid, and we can safely use it.
10675
10676 Don't do anything about disabled watchpoints, since they will be
10677 reevaluated again when enabled. */
10678 update_watchpoint (w, 1 /* reparse */);
10679 }
10680
10681 /* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10682
10683 static int
10684 insert_watchpoint (struct bp_location *bl)
10685 {
10686 struct watchpoint *w = (struct watchpoint *) bl->owner;
10687 int length = w->exact ? 1 : bl->length;
10688
10689 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
10690 w->cond_exp.get ());
10691 }
10692
10693 /* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10694
10695 static int
10696 remove_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10697 {
10698 struct watchpoint *w = (struct watchpoint *) bl->owner;
10699 int length = w->exact ? 1 : bl->length;
10700
10701 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
10702 w->cond_exp.get ());
10703 }
10704
10705 static int
10706 breakpoint_hit_watchpoint (const struct bp_location *bl,
10707 struct address_space *aspace, CORE_ADDR bp_addr,
10708 const struct target_waitstatus *ws)
10709 {
10710 struct breakpoint *b = bl->owner;
10711 struct watchpoint *w = (struct watchpoint *) b;
10712
10713 /* Continuable hardware watchpoints are treated as non-existent if the
10714 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10715 some data address). Otherwise gdb won't stop on a break instruction
10716 in the code (not from a breakpoint) when a hardware watchpoint has
10717 been defined. Also skip watchpoints which we know did not trigger
10718 (did not match the data address). */
10719 if (is_hardware_watchpoint (b)
10720 && w->watchpoint_triggered == watch_triggered_no)
10721 return 0;
10722
10723 return 1;
10724 }
10725
10726 static void
10727 check_status_watchpoint (bpstat bs)
10728 {
10729 gdb_assert (is_watchpoint (bs->breakpoint_at));
10730
10731 bpstat_check_watchpoint (bs);
10732 }
10733
10734 /* Implement the "resources_needed" breakpoint_ops method for
10735 hardware watchpoints. */
10736
10737 static int
10738 resources_needed_watchpoint (const struct bp_location *bl)
10739 {
10740 struct watchpoint *w = (struct watchpoint *) bl->owner;
10741 int length = w->exact? 1 : bl->length;
10742
10743 return target_region_ok_for_hw_watchpoint (bl->address, length);
10744 }
10745
10746 /* Implement the "works_in_software_mode" breakpoint_ops method for
10747 hardware watchpoints. */
10748
10749 static int
10750 works_in_software_mode_watchpoint (const struct breakpoint *b)
10751 {
10752 /* Read and access watchpoints only work with hardware support. */
10753 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
10754 }
10755
10756 static enum print_stop_action
10757 print_it_watchpoint (bpstat bs)
10758 {
10759 struct cleanup *old_chain;
10760 struct breakpoint *b;
10761 struct ui_file *stb;
10762 enum print_stop_action result;
10763 struct watchpoint *w;
10764 struct ui_out *uiout = current_uiout;
10765
10766 gdb_assert (bs->bp_location_at != NULL);
10767
10768 b = bs->breakpoint_at;
10769 w = (struct watchpoint *) b;
10770
10771 stb = mem_fileopen ();
10772 old_chain = make_cleanup_ui_file_delete (stb);
10773
10774 annotate_watchpoint (b->number);
10775 maybe_print_thread_hit_breakpoint (uiout);
10776
10777 switch (b->type)
10778 {
10779 case bp_watchpoint:
10780 case bp_hardware_watchpoint:
10781 if (ui_out_is_mi_like_p (uiout))
10782 ui_out_field_string
10783 (uiout, "reason",
10784 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10785 mention (b);
10786 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10787 ui_out_text (uiout, "\nOld value = ");
10788 watchpoint_value_print (bs->old_val, stb);
10789 ui_out_field_stream (uiout, "old", stb);
10790 ui_out_text (uiout, "\nNew value = ");
10791 watchpoint_value_print (w->val, stb);
10792 ui_out_field_stream (uiout, "new", stb);
10793 ui_out_text (uiout, "\n");
10794 /* More than one watchpoint may have been triggered. */
10795 result = PRINT_UNKNOWN;
10796 break;
10797
10798 case bp_read_watchpoint:
10799 if (ui_out_is_mi_like_p (uiout))
10800 ui_out_field_string
10801 (uiout, "reason",
10802 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10803 mention (b);
10804 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10805 ui_out_text (uiout, "\nValue = ");
10806 watchpoint_value_print (w->val, stb);
10807 ui_out_field_stream (uiout, "value", stb);
10808 ui_out_text (uiout, "\n");
10809 result = PRINT_UNKNOWN;
10810 break;
10811
10812 case bp_access_watchpoint:
10813 if (bs->old_val != NULL)
10814 {
10815 if (ui_out_is_mi_like_p (uiout))
10816 ui_out_field_string
10817 (uiout, "reason",
10818 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10819 mention (b);
10820 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10821 ui_out_text (uiout, "\nOld value = ");
10822 watchpoint_value_print (bs->old_val, stb);
10823 ui_out_field_stream (uiout, "old", stb);
10824 ui_out_text (uiout, "\nNew value = ");
10825 }
10826 else
10827 {
10828 mention (b);
10829 if (ui_out_is_mi_like_p (uiout))
10830 ui_out_field_string
10831 (uiout, "reason",
10832 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10833 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10834 ui_out_text (uiout, "\nValue = ");
10835 }
10836 watchpoint_value_print (w->val, stb);
10837 ui_out_field_stream (uiout, "new", stb);
10838 ui_out_text (uiout, "\n");
10839 result = PRINT_UNKNOWN;
10840 break;
10841 default:
10842 result = PRINT_UNKNOWN;
10843 }
10844
10845 do_cleanups (old_chain);
10846 return result;
10847 }
10848
10849 /* Implement the "print_mention" breakpoint_ops method for hardware
10850 watchpoints. */
10851
10852 static void
10853 print_mention_watchpoint (struct breakpoint *b)
10854 {
10855 struct cleanup *ui_out_chain;
10856 struct watchpoint *w = (struct watchpoint *) b;
10857 struct ui_out *uiout = current_uiout;
10858
10859 switch (b->type)
10860 {
10861 case bp_watchpoint:
10862 ui_out_text (uiout, "Watchpoint ");
10863 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10864 break;
10865 case bp_hardware_watchpoint:
10866 ui_out_text (uiout, "Hardware watchpoint ");
10867 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10868 break;
10869 case bp_read_watchpoint:
10870 ui_out_text (uiout, "Hardware read watchpoint ");
10871 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
10872 break;
10873 case bp_access_watchpoint:
10874 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
10875 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
10876 break;
10877 default:
10878 internal_error (__FILE__, __LINE__,
10879 _("Invalid hardware watchpoint type."));
10880 }
10881
10882 ui_out_field_int (uiout, "number", b->number);
10883 ui_out_text (uiout, ": ");
10884 ui_out_field_string (uiout, "exp", w->exp_string);
10885 do_cleanups (ui_out_chain);
10886 }
10887
10888 /* Implement the "print_recreate" breakpoint_ops method for
10889 watchpoints. */
10890
10891 static void
10892 print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10893 {
10894 struct watchpoint *w = (struct watchpoint *) b;
10895
10896 switch (b->type)
10897 {
10898 case bp_watchpoint:
10899 case bp_hardware_watchpoint:
10900 fprintf_unfiltered (fp, "watch");
10901 break;
10902 case bp_read_watchpoint:
10903 fprintf_unfiltered (fp, "rwatch");
10904 break;
10905 case bp_access_watchpoint:
10906 fprintf_unfiltered (fp, "awatch");
10907 break;
10908 default:
10909 internal_error (__FILE__, __LINE__,
10910 _("Invalid watchpoint type."));
10911 }
10912
10913 fprintf_unfiltered (fp, " %s", w->exp_string);
10914 print_recreate_thread (b, fp);
10915 }
10916
10917 /* Implement the "explains_signal" breakpoint_ops method for
10918 watchpoints. */
10919
10920 static int
10921 explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10922 {
10923 /* A software watchpoint cannot cause a signal other than
10924 GDB_SIGNAL_TRAP. */
10925 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
10926 return 0;
10927
10928 return 1;
10929 }
10930
10931 /* The breakpoint_ops structure to be used in hardware watchpoints. */
10932
10933 static struct breakpoint_ops watchpoint_breakpoint_ops;
10934
10935 /* Implement the "insert" breakpoint_ops method for
10936 masked hardware watchpoints. */
10937
10938 static int
10939 insert_masked_watchpoint (struct bp_location *bl)
10940 {
10941 struct watchpoint *w = (struct watchpoint *) bl->owner;
10942
10943 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
10944 bl->watchpoint_type);
10945 }
10946
10947 /* Implement the "remove" breakpoint_ops method for
10948 masked hardware watchpoints. */
10949
10950 static int
10951 remove_masked_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10952 {
10953 struct watchpoint *w = (struct watchpoint *) bl->owner;
10954
10955 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
10956 bl->watchpoint_type);
10957 }
10958
10959 /* Implement the "resources_needed" breakpoint_ops method for
10960 masked hardware watchpoints. */
10961
10962 static int
10963 resources_needed_masked_watchpoint (const struct bp_location *bl)
10964 {
10965 struct watchpoint *w = (struct watchpoint *) bl->owner;
10966
10967 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
10968 }
10969
10970 /* Implement the "works_in_software_mode" breakpoint_ops method for
10971 masked hardware watchpoints. */
10972
10973 static int
10974 works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10975 {
10976 return 0;
10977 }
10978
10979 /* Implement the "print_it" breakpoint_ops method for
10980 masked hardware watchpoints. */
10981
10982 static enum print_stop_action
10983 print_it_masked_watchpoint (bpstat bs)
10984 {
10985 struct breakpoint *b = bs->breakpoint_at;
10986 struct ui_out *uiout = current_uiout;
10987
10988 /* Masked watchpoints have only one location. */
10989 gdb_assert (b->loc && b->loc->next == NULL);
10990
10991 annotate_watchpoint (b->number);
10992 maybe_print_thread_hit_breakpoint (uiout);
10993
10994 switch (b->type)
10995 {
10996 case bp_hardware_watchpoint:
10997 if (ui_out_is_mi_like_p (uiout))
10998 ui_out_field_string
10999 (uiout, "reason",
11000 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
11001 break;
11002
11003 case bp_read_watchpoint:
11004 if (ui_out_is_mi_like_p (uiout))
11005 ui_out_field_string
11006 (uiout, "reason",
11007 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
11008 break;
11009
11010 case bp_access_watchpoint:
11011 if (ui_out_is_mi_like_p (uiout))
11012 ui_out_field_string
11013 (uiout, "reason",
11014 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
11015 break;
11016 default:
11017 internal_error (__FILE__, __LINE__,
11018 _("Invalid hardware watchpoint type."));
11019 }
11020
11021 mention (b);
11022 ui_out_text (uiout, _("\n\
11023 Check the underlying instruction at PC for the memory\n\
11024 address and value which triggered this watchpoint.\n"));
11025 ui_out_text (uiout, "\n");
11026
11027 /* More than one watchpoint may have been triggered. */
11028 return PRINT_UNKNOWN;
11029 }
11030
11031 /* Implement the "print_one_detail" breakpoint_ops method for
11032 masked hardware watchpoints. */
11033
11034 static void
11035 print_one_detail_masked_watchpoint (const struct breakpoint *b,
11036 struct ui_out *uiout)
11037 {
11038 struct watchpoint *w = (struct watchpoint *) b;
11039
11040 /* Masked watchpoints have only one location. */
11041 gdb_assert (b->loc && b->loc->next == NULL);
11042
11043 ui_out_text (uiout, "\tmask ");
11044 ui_out_field_core_addr (uiout, "mask", b->loc->gdbarch, w->hw_wp_mask);
11045 ui_out_text (uiout, "\n");
11046 }
11047
11048 /* Implement the "print_mention" breakpoint_ops method for
11049 masked hardware watchpoints. */
11050
11051 static void
11052 print_mention_masked_watchpoint (struct breakpoint *b)
11053 {
11054 struct watchpoint *w = (struct watchpoint *) b;
11055 struct ui_out *uiout = current_uiout;
11056 struct cleanup *ui_out_chain;
11057
11058 switch (b->type)
11059 {
11060 case bp_hardware_watchpoint:
11061 ui_out_text (uiout, "Masked hardware watchpoint ");
11062 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
11063 break;
11064 case bp_read_watchpoint:
11065 ui_out_text (uiout, "Masked hardware read watchpoint ");
11066 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
11067 break;
11068 case bp_access_watchpoint:
11069 ui_out_text (uiout, "Masked hardware access (read/write) watchpoint ");
11070 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
11071 break;
11072 default:
11073 internal_error (__FILE__, __LINE__,
11074 _("Invalid hardware watchpoint type."));
11075 }
11076
11077 ui_out_field_int (uiout, "number", b->number);
11078 ui_out_text (uiout, ": ");
11079 ui_out_field_string (uiout, "exp", w->exp_string);
11080 do_cleanups (ui_out_chain);
11081 }
11082
11083 /* Implement the "print_recreate" breakpoint_ops method for
11084 masked hardware watchpoints. */
11085
11086 static void
11087 print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
11088 {
11089 struct watchpoint *w = (struct watchpoint *) b;
11090 char tmp[40];
11091
11092 switch (b->type)
11093 {
11094 case bp_hardware_watchpoint:
11095 fprintf_unfiltered (fp, "watch");
11096 break;
11097 case bp_read_watchpoint:
11098 fprintf_unfiltered (fp, "rwatch");
11099 break;
11100 case bp_access_watchpoint:
11101 fprintf_unfiltered (fp, "awatch");
11102 break;
11103 default:
11104 internal_error (__FILE__, __LINE__,
11105 _("Invalid hardware watchpoint type."));
11106 }
11107
11108 sprintf_vma (tmp, w->hw_wp_mask);
11109 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
11110 print_recreate_thread (b, fp);
11111 }
11112
11113 /* The breakpoint_ops structure to be used in masked hardware watchpoints. */
11114
11115 static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
11116
11117 /* Tell whether the given watchpoint is a masked hardware watchpoint. */
11118
11119 static int
11120 is_masked_watchpoint (const struct breakpoint *b)
11121 {
11122 return b->ops == &masked_watchpoint_breakpoint_ops;
11123 }
11124
11125 /* accessflag: hw_write: watch write,
11126 hw_read: watch read,
11127 hw_access: watch access (read or write) */
11128 static void
11129 watch_command_1 (const char *arg, int accessflag, int from_tty,
11130 int just_location, int internal)
11131 {
11132 struct breakpoint *b, *scope_breakpoint = NULL;
11133 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
11134 struct value *val, *mark, *result;
11135 int saved_bitpos = 0, saved_bitsize = 0;
11136 struct frame_info *frame;
11137 const char *exp_start = NULL;
11138 const char *exp_end = NULL;
11139 const char *tok, *end_tok;
11140 int toklen = -1;
11141 const char *cond_start = NULL;
11142 const char *cond_end = NULL;
11143 enum bptype bp_type;
11144 int thread = -1;
11145 int pc = 0;
11146 /* Flag to indicate whether we are going to use masks for
11147 the hardware watchpoint. */
11148 int use_mask = 0;
11149 CORE_ADDR mask = 0;
11150 struct watchpoint *w;
11151 char *expression;
11152 struct cleanup *back_to;
11153
11154 /* Make sure that we actually have parameters to parse. */
11155 if (arg != NULL && arg[0] != '\0')
11156 {
11157 const char *value_start;
11158
11159 exp_end = arg + strlen (arg);
11160
11161 /* Look for "parameter value" pairs at the end
11162 of the arguments string. */
11163 for (tok = exp_end - 1; tok > arg; tok--)
11164 {
11165 /* Skip whitespace at the end of the argument list. */
11166 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11167 tok--;
11168
11169 /* Find the beginning of the last token.
11170 This is the value of the parameter. */
11171 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11172 tok--;
11173 value_start = tok + 1;
11174
11175 /* Skip whitespace. */
11176 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11177 tok--;
11178
11179 end_tok = tok;
11180
11181 /* Find the beginning of the second to last token.
11182 This is the parameter itself. */
11183 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11184 tok--;
11185 tok++;
11186 toklen = end_tok - tok + 1;
11187
11188 if (toklen == 6 && startswith (tok, "thread"))
11189 {
11190 struct thread_info *thr;
11191 /* At this point we've found a "thread" token, which means
11192 the user is trying to set a watchpoint that triggers
11193 only in a specific thread. */
11194 const char *endp;
11195
11196 if (thread != -1)
11197 error(_("You can specify only one thread."));
11198
11199 /* Extract the thread ID from the next token. */
11200 thr = parse_thread_id (value_start, &endp);
11201
11202 /* Check if the user provided a valid thread ID. */
11203 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
11204 invalid_thread_id_error (value_start);
11205
11206 thread = thr->global_num;
11207 }
11208 else if (toklen == 4 && startswith (tok, "mask"))
11209 {
11210 /* We've found a "mask" token, which means the user wants to
11211 create a hardware watchpoint that is going to have the mask
11212 facility. */
11213 struct value *mask_value, *mark;
11214
11215 if (use_mask)
11216 error(_("You can specify only one mask."));
11217
11218 use_mask = just_location = 1;
11219
11220 mark = value_mark ();
11221 mask_value = parse_to_comma_and_eval (&value_start);
11222 mask = value_as_address (mask_value);
11223 value_free_to_mark (mark);
11224 }
11225 else
11226 /* We didn't recognize what we found. We should stop here. */
11227 break;
11228
11229 /* Truncate the string and get rid of the "parameter value" pair before
11230 the arguments string is parsed by the parse_exp_1 function. */
11231 exp_end = tok;
11232 }
11233 }
11234 else
11235 exp_end = arg;
11236
11237 /* Parse the rest of the arguments. From here on out, everything
11238 is in terms of a newly allocated string instead of the original
11239 ARG. */
11240 innermost_block = NULL;
11241 expression = savestring (arg, exp_end - arg);
11242 back_to = make_cleanup (xfree, expression);
11243 exp_start = arg = expression;
11244 expression_up exp = parse_exp_1 (&arg, 0, 0, 0);
11245 exp_end = arg;
11246 /* Remove trailing whitespace from the expression before saving it.
11247 This makes the eventual display of the expression string a bit
11248 prettier. */
11249 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
11250 --exp_end;
11251
11252 /* Checking if the expression is not constant. */
11253 if (watchpoint_exp_is_const (exp.get ()))
11254 {
11255 int len;
11256
11257 len = exp_end - exp_start;
11258 while (len > 0 && isspace (exp_start[len - 1]))
11259 len--;
11260 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
11261 }
11262
11263 exp_valid_block = innermost_block;
11264 mark = value_mark ();
11265 fetch_subexp_value (exp.get (), &pc, &val, &result, NULL, just_location);
11266
11267 if (val != NULL && just_location)
11268 {
11269 saved_bitpos = value_bitpos (val);
11270 saved_bitsize = value_bitsize (val);
11271 }
11272
11273 if (just_location)
11274 {
11275 int ret;
11276
11277 exp_valid_block = NULL;
11278 val = value_addr (result);
11279 release_value (val);
11280 value_free_to_mark (mark);
11281
11282 if (use_mask)
11283 {
11284 ret = target_masked_watch_num_registers (value_as_address (val),
11285 mask);
11286 if (ret == -1)
11287 error (_("This target does not support masked watchpoints."));
11288 else if (ret == -2)
11289 error (_("Invalid mask or memory region."));
11290 }
11291 }
11292 else if (val != NULL)
11293 release_value (val);
11294
11295 tok = skip_spaces_const (arg);
11296 end_tok = skip_to_space_const (tok);
11297
11298 toklen = end_tok - tok;
11299 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
11300 {
11301 innermost_block = NULL;
11302 tok = cond_start = end_tok + 1;
11303 parse_exp_1 (&tok, 0, 0, 0);
11304
11305 /* The watchpoint expression may not be local, but the condition
11306 may still be. E.g.: `watch global if local > 0'. */
11307 cond_exp_valid_block = innermost_block;
11308
11309 cond_end = tok;
11310 }
11311 if (*tok)
11312 error (_("Junk at end of command."));
11313
11314 frame = block_innermost_frame (exp_valid_block);
11315
11316 /* If the expression is "local", then set up a "watchpoint scope"
11317 breakpoint at the point where we've left the scope of the watchpoint
11318 expression. Create the scope breakpoint before the watchpoint, so
11319 that we will encounter it first in bpstat_stop_status. */
11320 if (exp_valid_block && frame)
11321 {
11322 if (frame_id_p (frame_unwind_caller_id (frame)))
11323 {
11324 scope_breakpoint
11325 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
11326 frame_unwind_caller_pc (frame),
11327 bp_watchpoint_scope,
11328 &momentary_breakpoint_ops);
11329
11330 scope_breakpoint->enable_state = bp_enabled;
11331
11332 /* Automatically delete the breakpoint when it hits. */
11333 scope_breakpoint->disposition = disp_del;
11334
11335 /* Only break in the proper frame (help with recursion). */
11336 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
11337
11338 /* Set the address at which we will stop. */
11339 scope_breakpoint->loc->gdbarch
11340 = frame_unwind_caller_arch (frame);
11341 scope_breakpoint->loc->requested_address
11342 = frame_unwind_caller_pc (frame);
11343 scope_breakpoint->loc->address
11344 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
11345 scope_breakpoint->loc->requested_address,
11346 scope_breakpoint->type);
11347 }
11348 }
11349
11350 /* Now set up the breakpoint. We create all watchpoints as hardware
11351 watchpoints here even if hardware watchpoints are turned off, a call
11352 to update_watchpoint later in this function will cause the type to
11353 drop back to bp_watchpoint (software watchpoint) if required. */
11354
11355 if (accessflag == hw_read)
11356 bp_type = bp_read_watchpoint;
11357 else if (accessflag == hw_access)
11358 bp_type = bp_access_watchpoint;
11359 else
11360 bp_type = bp_hardware_watchpoint;
11361
11362 w = new watchpoint ();
11363 b = &w->base;
11364 if (use_mask)
11365 init_raw_breakpoint_without_location (b, NULL, bp_type,
11366 &masked_watchpoint_breakpoint_ops);
11367 else
11368 init_raw_breakpoint_without_location (b, NULL, bp_type,
11369 &watchpoint_breakpoint_ops);
11370 b->thread = thread;
11371 b->disposition = disp_donttouch;
11372 b->pspace = current_program_space;
11373 w->exp = std::move (exp);
11374 w->exp_valid_block = exp_valid_block;
11375 w->cond_exp_valid_block = cond_exp_valid_block;
11376 if (just_location)
11377 {
11378 struct type *t = value_type (val);
11379 CORE_ADDR addr = value_as_address (val);
11380
11381 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
11382
11383 std::string name = type_to_string (t);
11384
11385 w->exp_string_reparse = xstrprintf ("* (%s *) %s", name.c_str (),
11386 core_addr_to_string (addr));
11387
11388 w->exp_string = xstrprintf ("-location %.*s",
11389 (int) (exp_end - exp_start), exp_start);
11390
11391 /* The above expression is in C. */
11392 b->language = language_c;
11393 }
11394 else
11395 w->exp_string = savestring (exp_start, exp_end - exp_start);
11396
11397 if (use_mask)
11398 {
11399 w->hw_wp_mask = mask;
11400 }
11401 else
11402 {
11403 w->val = val;
11404 w->val_bitpos = saved_bitpos;
11405 w->val_bitsize = saved_bitsize;
11406 w->val_valid = 1;
11407 }
11408
11409 if (cond_start)
11410 b->cond_string = savestring (cond_start, cond_end - cond_start);
11411 else
11412 b->cond_string = 0;
11413
11414 if (frame)
11415 {
11416 w->watchpoint_frame = get_frame_id (frame);
11417 w->watchpoint_thread = inferior_ptid;
11418 }
11419 else
11420 {
11421 w->watchpoint_frame = null_frame_id;
11422 w->watchpoint_thread = null_ptid;
11423 }
11424
11425 if (scope_breakpoint != NULL)
11426 {
11427 /* The scope breakpoint is related to the watchpoint. We will
11428 need to act on them together. */
11429 b->related_breakpoint = scope_breakpoint;
11430 scope_breakpoint->related_breakpoint = b;
11431 }
11432
11433 if (!just_location)
11434 value_free_to_mark (mark);
11435
11436 TRY
11437 {
11438 /* Finally update the new watchpoint. This creates the locations
11439 that should be inserted. */
11440 update_watchpoint (w, 1);
11441 }
11442 CATCH (e, RETURN_MASK_ALL)
11443 {
11444 delete_breakpoint (b);
11445 throw_exception (e);
11446 }
11447 END_CATCH
11448
11449 install_breakpoint (internal, b, 1);
11450 do_cleanups (back_to);
11451 }
11452
11453 /* Return count of debug registers needed to watch the given expression.
11454 If the watchpoint cannot be handled in hardware return zero. */
11455
11456 static int
11457 can_use_hardware_watchpoint (struct value *v)
11458 {
11459 int found_memory_cnt = 0;
11460 struct value *head = v;
11461
11462 /* Did the user specifically forbid us to use hardware watchpoints? */
11463 if (!can_use_hw_watchpoints)
11464 return 0;
11465
11466 /* Make sure that the value of the expression depends only upon
11467 memory contents, and values computed from them within GDB. If we
11468 find any register references or function calls, we can't use a
11469 hardware watchpoint.
11470
11471 The idea here is that evaluating an expression generates a series
11472 of values, one holding the value of every subexpression. (The
11473 expression a*b+c has five subexpressions: a, b, a*b, c, and
11474 a*b+c.) GDB's values hold almost enough information to establish
11475 the criteria given above --- they identify memory lvalues,
11476 register lvalues, computed values, etcetera. So we can evaluate
11477 the expression, and then scan the chain of values that leaves
11478 behind to decide whether we can detect any possible change to the
11479 expression's final value using only hardware watchpoints.
11480
11481 However, I don't think that the values returned by inferior
11482 function calls are special in any way. So this function may not
11483 notice that an expression involving an inferior function call
11484 can't be watched with hardware watchpoints. FIXME. */
11485 for (; v; v = value_next (v))
11486 {
11487 if (VALUE_LVAL (v) == lval_memory)
11488 {
11489 if (v != head && value_lazy (v))
11490 /* A lazy memory lvalue in the chain is one that GDB never
11491 needed to fetch; we either just used its address (e.g.,
11492 `a' in `a.b') or we never needed it at all (e.g., `a'
11493 in `a,b'). This doesn't apply to HEAD; if that is
11494 lazy then it was not readable, but watch it anyway. */
11495 ;
11496 else
11497 {
11498 /* Ahh, memory we actually used! Check if we can cover
11499 it with hardware watchpoints. */
11500 struct type *vtype = check_typedef (value_type (v));
11501
11502 /* We only watch structs and arrays if user asked for it
11503 explicitly, never if they just happen to appear in a
11504 middle of some value chain. */
11505 if (v == head
11506 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11507 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11508 {
11509 CORE_ADDR vaddr = value_address (v);
11510 int len;
11511 int num_regs;
11512
11513 len = (target_exact_watchpoints
11514 && is_scalar_type_recursive (vtype))?
11515 1 : TYPE_LENGTH (value_type (v));
11516
11517 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11518 if (!num_regs)
11519 return 0;
11520 else
11521 found_memory_cnt += num_regs;
11522 }
11523 }
11524 }
11525 else if (VALUE_LVAL (v) != not_lval
11526 && deprecated_value_modifiable (v) == 0)
11527 return 0; /* These are values from the history (e.g., $1). */
11528 else if (VALUE_LVAL (v) == lval_register)
11529 return 0; /* Cannot watch a register with a HW watchpoint. */
11530 }
11531
11532 /* The expression itself looks suitable for using a hardware
11533 watchpoint, but give the target machine a chance to reject it. */
11534 return found_memory_cnt;
11535 }
11536
11537 void
11538 watch_command_wrapper (char *arg, int from_tty, int internal)
11539 {
11540 watch_command_1 (arg, hw_write, from_tty, 0, internal);
11541 }
11542
11543 /* A helper function that looks for the "-location" argument and then
11544 calls watch_command_1. */
11545
11546 static void
11547 watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11548 {
11549 int just_location = 0;
11550
11551 if (arg
11552 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11553 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11554 {
11555 arg = skip_spaces (arg);
11556 just_location = 1;
11557 }
11558
11559 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
11560 }
11561
11562 static void
11563 watch_command (char *arg, int from_tty)
11564 {
11565 watch_maybe_just_location (arg, hw_write, from_tty);
11566 }
11567
11568 void
11569 rwatch_command_wrapper (char *arg, int from_tty, int internal)
11570 {
11571 watch_command_1 (arg, hw_read, from_tty, 0, internal);
11572 }
11573
11574 static void
11575 rwatch_command (char *arg, int from_tty)
11576 {
11577 watch_maybe_just_location (arg, hw_read, from_tty);
11578 }
11579
11580 void
11581 awatch_command_wrapper (char *arg, int from_tty, int internal)
11582 {
11583 watch_command_1 (arg, hw_access, from_tty, 0, internal);
11584 }
11585
11586 static void
11587 awatch_command (char *arg, int from_tty)
11588 {
11589 watch_maybe_just_location (arg, hw_access, from_tty);
11590 }
11591 \f
11592
11593 /* Data for the FSM that manages the until(location)/advance commands
11594 in infcmd.c. Here because it uses the mechanisms of
11595 breakpoints. */
11596
11597 struct until_break_fsm
11598 {
11599 /* The base class. */
11600 struct thread_fsm thread_fsm;
11601
11602 /* The thread that as current when the command was executed. */
11603 int thread;
11604
11605 /* The breakpoint set at the destination location. */
11606 struct breakpoint *location_breakpoint;
11607
11608 /* Breakpoint set at the return address in the caller frame. May be
11609 NULL. */
11610 struct breakpoint *caller_breakpoint;
11611 };
11612
11613 static void until_break_fsm_clean_up (struct thread_fsm *self,
11614 struct thread_info *thread);
11615 static int until_break_fsm_should_stop (struct thread_fsm *self,
11616 struct thread_info *thread);
11617 static enum async_reply_reason
11618 until_break_fsm_async_reply_reason (struct thread_fsm *self);
11619
11620 /* until_break_fsm's vtable. */
11621
11622 static struct thread_fsm_ops until_break_fsm_ops =
11623 {
11624 NULL, /* dtor */
11625 until_break_fsm_clean_up,
11626 until_break_fsm_should_stop,
11627 NULL, /* return_value */
11628 until_break_fsm_async_reply_reason,
11629 };
11630
11631 /* Allocate a new until_break_command_fsm. */
11632
11633 static struct until_break_fsm *
11634 new_until_break_fsm (struct interp *cmd_interp, int thread,
11635 struct breakpoint *location_breakpoint,
11636 struct breakpoint *caller_breakpoint)
11637 {
11638 struct until_break_fsm *sm;
11639
11640 sm = XCNEW (struct until_break_fsm);
11641 thread_fsm_ctor (&sm->thread_fsm, &until_break_fsm_ops, cmd_interp);
11642
11643 sm->thread = thread;
11644 sm->location_breakpoint = location_breakpoint;
11645 sm->caller_breakpoint = caller_breakpoint;
11646
11647 return sm;
11648 }
11649
11650 /* Implementation of the 'should_stop' FSM method for the
11651 until(location)/advance commands. */
11652
11653 static int
11654 until_break_fsm_should_stop (struct thread_fsm *self,
11655 struct thread_info *tp)
11656 {
11657 struct until_break_fsm *sm = (struct until_break_fsm *) self;
11658
11659 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
11660 sm->location_breakpoint) != NULL
11661 || (sm->caller_breakpoint != NULL
11662 && bpstat_find_breakpoint (tp->control.stop_bpstat,
11663 sm->caller_breakpoint) != NULL))
11664 thread_fsm_set_finished (self);
11665
11666 return 1;
11667 }
11668
11669 /* Implementation of the 'clean_up' FSM method for the
11670 until(location)/advance commands. */
11671
11672 static void
11673 until_break_fsm_clean_up (struct thread_fsm *self,
11674 struct thread_info *thread)
11675 {
11676 struct until_break_fsm *sm = (struct until_break_fsm *) self;
11677
11678 /* Clean up our temporary breakpoints. */
11679 if (sm->location_breakpoint != NULL)
11680 {
11681 delete_breakpoint (sm->location_breakpoint);
11682 sm->location_breakpoint = NULL;
11683 }
11684 if (sm->caller_breakpoint != NULL)
11685 {
11686 delete_breakpoint (sm->caller_breakpoint);
11687 sm->caller_breakpoint = NULL;
11688 }
11689 delete_longjmp_breakpoint (sm->thread);
11690 }
11691
11692 /* Implementation of the 'async_reply_reason' FSM method for the
11693 until(location)/advance commands. */
11694
11695 static enum async_reply_reason
11696 until_break_fsm_async_reply_reason (struct thread_fsm *self)
11697 {
11698 return EXEC_ASYNC_LOCATION_REACHED;
11699 }
11700
11701 void
11702 until_break_command (char *arg, int from_tty, int anywhere)
11703 {
11704 struct symtabs_and_lines sals;
11705 struct symtab_and_line sal;
11706 struct frame_info *frame;
11707 struct gdbarch *frame_gdbarch;
11708 struct frame_id stack_frame_id;
11709 struct frame_id caller_frame_id;
11710 struct breakpoint *location_breakpoint;
11711 struct breakpoint *caller_breakpoint = NULL;
11712 struct cleanup *old_chain, *cleanup;
11713 int thread;
11714 struct thread_info *tp;
11715 struct event_location *location;
11716 struct until_break_fsm *sm;
11717
11718 clear_proceed_status (0);
11719
11720 /* Set a breakpoint where the user wants it and at return from
11721 this function. */
11722
11723 location = string_to_event_location (&arg, current_language);
11724 cleanup = make_cleanup_delete_event_location (location);
11725
11726 if (last_displayed_sal_is_valid ())
11727 sals = decode_line_1 (location, DECODE_LINE_FUNFIRSTLINE, NULL,
11728 get_last_displayed_symtab (),
11729 get_last_displayed_line ());
11730 else
11731 sals = decode_line_1 (location, DECODE_LINE_FUNFIRSTLINE,
11732 NULL, (struct symtab *) NULL, 0);
11733
11734 if (sals.nelts != 1)
11735 error (_("Couldn't get information on specified line."));
11736
11737 sal = sals.sals[0];
11738 xfree (sals.sals); /* malloc'd, so freed. */
11739
11740 if (*arg)
11741 error (_("Junk at end of arguments."));
11742
11743 resolve_sal_pc (&sal);
11744
11745 tp = inferior_thread ();
11746 thread = tp->global_num;
11747
11748 old_chain = make_cleanup (null_cleanup, NULL);
11749
11750 /* Note linespec handling above invalidates the frame chain.
11751 Installing a breakpoint also invalidates the frame chain (as it
11752 may need to switch threads), so do any frame handling before
11753 that. */
11754
11755 frame = get_selected_frame (NULL);
11756 frame_gdbarch = get_frame_arch (frame);
11757 stack_frame_id = get_stack_frame_id (frame);
11758 caller_frame_id = frame_unwind_caller_id (frame);
11759
11760 /* Keep within the current frame, or in frames called by the current
11761 one. */
11762
11763 if (frame_id_p (caller_frame_id))
11764 {
11765 struct symtab_and_line sal2;
11766 struct gdbarch *caller_gdbarch;
11767
11768 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11769 sal2.pc = frame_unwind_caller_pc (frame);
11770 caller_gdbarch = frame_unwind_caller_arch (frame);
11771 caller_breakpoint = set_momentary_breakpoint (caller_gdbarch,
11772 sal2,
11773 caller_frame_id,
11774 bp_until);
11775 make_cleanup_delete_breakpoint (caller_breakpoint);
11776
11777 set_longjmp_breakpoint (tp, caller_frame_id);
11778 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
11779 }
11780
11781 /* set_momentary_breakpoint could invalidate FRAME. */
11782 frame = NULL;
11783
11784 if (anywhere)
11785 /* If the user told us to continue until a specified location,
11786 we don't specify a frame at which we need to stop. */
11787 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11788 null_frame_id, bp_until);
11789 else
11790 /* Otherwise, specify the selected frame, because we want to stop
11791 only at the very same frame. */
11792 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11793 stack_frame_id, bp_until);
11794 make_cleanup_delete_breakpoint (location_breakpoint);
11795
11796 sm = new_until_break_fsm (command_interp (), tp->global_num,
11797 location_breakpoint, caller_breakpoint);
11798 tp->thread_fsm = &sm->thread_fsm;
11799
11800 discard_cleanups (old_chain);
11801
11802 proceed (-1, GDB_SIGNAL_DEFAULT);
11803
11804 do_cleanups (cleanup);
11805 }
11806
11807 /* This function attempts to parse an optional "if <cond>" clause
11808 from the arg string. If one is not found, it returns NULL.
11809
11810 Else, it returns a pointer to the condition string. (It does not
11811 attempt to evaluate the string against a particular block.) And,
11812 it updates arg to point to the first character following the parsed
11813 if clause in the arg string. */
11814
11815 char *
11816 ep_parse_optional_if_clause (char **arg)
11817 {
11818 char *cond_string;
11819
11820 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
11821 return NULL;
11822
11823 /* Skip the "if" keyword. */
11824 (*arg) += 2;
11825
11826 /* Skip any extra leading whitespace, and record the start of the
11827 condition string. */
11828 *arg = skip_spaces (*arg);
11829 cond_string = *arg;
11830
11831 /* Assume that the condition occupies the remainder of the arg
11832 string. */
11833 (*arg) += strlen (cond_string);
11834
11835 return cond_string;
11836 }
11837
11838 /* Commands to deal with catching events, such as signals, exceptions,
11839 process start/exit, etc. */
11840
11841 typedef enum
11842 {
11843 catch_fork_temporary, catch_vfork_temporary,
11844 catch_fork_permanent, catch_vfork_permanent
11845 }
11846 catch_fork_kind;
11847
11848 static void
11849 catch_fork_command_1 (char *arg, int from_tty,
11850 struct cmd_list_element *command)
11851 {
11852 struct gdbarch *gdbarch = get_current_arch ();
11853 char *cond_string = NULL;
11854 catch_fork_kind fork_kind;
11855 int tempflag;
11856
11857 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11858 tempflag = (fork_kind == catch_fork_temporary
11859 || fork_kind == catch_vfork_temporary);
11860
11861 if (!arg)
11862 arg = "";
11863 arg = skip_spaces (arg);
11864
11865 /* The allowed syntax is:
11866 catch [v]fork
11867 catch [v]fork if <cond>
11868
11869 First, check if there's an if clause. */
11870 cond_string = ep_parse_optional_if_clause (&arg);
11871
11872 if ((*arg != '\0') && !isspace (*arg))
11873 error (_("Junk at end of arguments."));
11874
11875 /* If this target supports it, create a fork or vfork catchpoint
11876 and enable reporting of such events. */
11877 switch (fork_kind)
11878 {
11879 case catch_fork_temporary:
11880 case catch_fork_permanent:
11881 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11882 &catch_fork_breakpoint_ops);
11883 break;
11884 case catch_vfork_temporary:
11885 case catch_vfork_permanent:
11886 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11887 &catch_vfork_breakpoint_ops);
11888 break;
11889 default:
11890 error (_("unsupported or unknown fork kind; cannot catch it"));
11891 break;
11892 }
11893 }
11894
11895 static void
11896 catch_exec_command_1 (char *arg, int from_tty,
11897 struct cmd_list_element *command)
11898 {
11899 struct exec_catchpoint *c;
11900 struct gdbarch *gdbarch = get_current_arch ();
11901 int tempflag;
11902 char *cond_string = NULL;
11903
11904 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11905
11906 if (!arg)
11907 arg = "";
11908 arg = skip_spaces (arg);
11909
11910 /* The allowed syntax is:
11911 catch exec
11912 catch exec if <cond>
11913
11914 First, check if there's an if clause. */
11915 cond_string = ep_parse_optional_if_clause (&arg);
11916
11917 if ((*arg != '\0') && !isspace (*arg))
11918 error (_("Junk at end of arguments."));
11919
11920 c = new exec_catchpoint ();
11921 init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
11922 &catch_exec_breakpoint_ops);
11923 c->exec_pathname = NULL;
11924
11925 install_breakpoint (0, &c->base, 1);
11926 }
11927
11928 void
11929 init_ada_exception_breakpoint (struct breakpoint *b,
11930 struct gdbarch *gdbarch,
11931 struct symtab_and_line sal,
11932 char *addr_string,
11933 const struct breakpoint_ops *ops,
11934 int tempflag,
11935 int enabled,
11936 int from_tty)
11937 {
11938 if (from_tty)
11939 {
11940 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11941 if (!loc_gdbarch)
11942 loc_gdbarch = gdbarch;
11943
11944 describe_other_breakpoints (loc_gdbarch,
11945 sal.pspace, sal.pc, sal.section, -1);
11946 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11947 version for exception catchpoints, because two catchpoints
11948 used for different exception names will use the same address.
11949 In this case, a "breakpoint ... also set at..." warning is
11950 unproductive. Besides, the warning phrasing is also a bit
11951 inappropriate, we should use the word catchpoint, and tell
11952 the user what type of catchpoint it is. The above is good
11953 enough for now, though. */
11954 }
11955
11956 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
11957
11958 b->enable_state = enabled ? bp_enabled : bp_disabled;
11959 b->disposition = tempflag ? disp_del : disp_donttouch;
11960 b->location = string_to_event_location (&addr_string,
11961 language_def (language_ada));
11962 b->language = language_ada;
11963 }
11964
11965 static void
11966 catch_command (char *arg, int from_tty)
11967 {
11968 error (_("Catch requires an event name."));
11969 }
11970 \f
11971
11972 static void
11973 tcatch_command (char *arg, int from_tty)
11974 {
11975 error (_("Catch requires an event name."));
11976 }
11977
11978 /* A qsort comparison function that sorts breakpoints in order. */
11979
11980 static int
11981 compare_breakpoints (const void *a, const void *b)
11982 {
11983 const breakpoint_p *ba = (const breakpoint_p *) a;
11984 uintptr_t ua = (uintptr_t) *ba;
11985 const breakpoint_p *bb = (const breakpoint_p *) b;
11986 uintptr_t ub = (uintptr_t) *bb;
11987
11988 if ((*ba)->number < (*bb)->number)
11989 return -1;
11990 else if ((*ba)->number > (*bb)->number)
11991 return 1;
11992
11993 /* Now sort by address, in case we see, e..g, two breakpoints with
11994 the number 0. */
11995 if (ua < ub)
11996 return -1;
11997 return ua > ub ? 1 : 0;
11998 }
11999
12000 /* Delete breakpoints by address or line. */
12001
12002 static void
12003 clear_command (char *arg, int from_tty)
12004 {
12005 struct breakpoint *b, *prev;
12006 VEC(breakpoint_p) *found = 0;
12007 int ix;
12008 int default_match;
12009 struct symtabs_and_lines sals;
12010 struct symtab_and_line sal;
12011 int i;
12012 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
12013
12014 if (arg)
12015 {
12016 sals = decode_line_with_current_source (arg,
12017 (DECODE_LINE_FUNFIRSTLINE
12018 | DECODE_LINE_LIST_MODE));
12019 make_cleanup (xfree, sals.sals);
12020 default_match = 0;
12021 }
12022 else
12023 {
12024 sals.sals = XNEW (struct symtab_and_line);
12025 make_cleanup (xfree, sals.sals);
12026 init_sal (&sal); /* Initialize to zeroes. */
12027
12028 /* Set sal's line, symtab, pc, and pspace to the values
12029 corresponding to the last call to print_frame_info. If the
12030 codepoint is not valid, this will set all the fields to 0. */
12031 get_last_displayed_sal (&sal);
12032 if (sal.symtab == 0)
12033 error (_("No source file specified."));
12034
12035 sals.sals[0] = sal;
12036 sals.nelts = 1;
12037
12038 default_match = 1;
12039 }
12040
12041 /* We don't call resolve_sal_pc here. That's not as bad as it
12042 seems, because all existing breakpoints typically have both
12043 file/line and pc set. So, if clear is given file/line, we can
12044 match this to existing breakpoint without obtaining pc at all.
12045
12046 We only support clearing given the address explicitly
12047 present in breakpoint table. Say, we've set breakpoint
12048 at file:line. There were several PC values for that file:line,
12049 due to optimization, all in one block.
12050
12051 We've picked one PC value. If "clear" is issued with another
12052 PC corresponding to the same file:line, the breakpoint won't
12053 be cleared. We probably can still clear the breakpoint, but
12054 since the other PC value is never presented to user, user
12055 can only find it by guessing, and it does not seem important
12056 to support that. */
12057
12058 /* For each line spec given, delete bps which correspond to it. Do
12059 it in two passes, solely to preserve the current behavior that
12060 from_tty is forced true if we delete more than one
12061 breakpoint. */
12062
12063 found = NULL;
12064 make_cleanup (VEC_cleanup (breakpoint_p), &found);
12065 for (i = 0; i < sals.nelts; i++)
12066 {
12067 const char *sal_fullname;
12068
12069 /* If exact pc given, clear bpts at that pc.
12070 If line given (pc == 0), clear all bpts on specified line.
12071 If defaulting, clear all bpts on default line
12072 or at default pc.
12073
12074 defaulting sal.pc != 0 tests to do
12075
12076 0 1 pc
12077 1 1 pc _and_ line
12078 0 0 line
12079 1 0 <can't happen> */
12080
12081 sal = sals.sals[i];
12082 sal_fullname = (sal.symtab == NULL
12083 ? NULL : symtab_to_fullname (sal.symtab));
12084
12085 /* Find all matching breakpoints and add them to 'found'. */
12086 ALL_BREAKPOINTS (b)
12087 {
12088 int match = 0;
12089 /* Are we going to delete b? */
12090 if (b->type != bp_none && !is_watchpoint (b))
12091 {
12092 struct bp_location *loc = b->loc;
12093 for (; loc; loc = loc->next)
12094 {
12095 /* If the user specified file:line, don't allow a PC
12096 match. This matches historical gdb behavior. */
12097 int pc_match = (!sal.explicit_line
12098 && sal.pc
12099 && (loc->pspace == sal.pspace)
12100 && (loc->address == sal.pc)
12101 && (!section_is_overlay (loc->section)
12102 || loc->section == sal.section));
12103 int line_match = 0;
12104
12105 if ((default_match || sal.explicit_line)
12106 && loc->symtab != NULL
12107 && sal_fullname != NULL
12108 && sal.pspace == loc->pspace
12109 && loc->line_number == sal.line
12110 && filename_cmp (symtab_to_fullname (loc->symtab),
12111 sal_fullname) == 0)
12112 line_match = 1;
12113
12114 if (pc_match || line_match)
12115 {
12116 match = 1;
12117 break;
12118 }
12119 }
12120 }
12121
12122 if (match)
12123 VEC_safe_push(breakpoint_p, found, b);
12124 }
12125 }
12126
12127 /* Now go thru the 'found' chain and delete them. */
12128 if (VEC_empty(breakpoint_p, found))
12129 {
12130 if (arg)
12131 error (_("No breakpoint at %s."), arg);
12132 else
12133 error (_("No breakpoint at this line."));
12134 }
12135
12136 /* Remove duplicates from the vec. */
12137 qsort (VEC_address (breakpoint_p, found),
12138 VEC_length (breakpoint_p, found),
12139 sizeof (breakpoint_p),
12140 compare_breakpoints);
12141 prev = VEC_index (breakpoint_p, found, 0);
12142 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
12143 {
12144 if (b == prev)
12145 {
12146 VEC_ordered_remove (breakpoint_p, found, ix);
12147 --ix;
12148 }
12149 }
12150
12151 if (VEC_length(breakpoint_p, found) > 1)
12152 from_tty = 1; /* Always report if deleted more than one. */
12153 if (from_tty)
12154 {
12155 if (VEC_length(breakpoint_p, found) == 1)
12156 printf_unfiltered (_("Deleted breakpoint "));
12157 else
12158 printf_unfiltered (_("Deleted breakpoints "));
12159 }
12160
12161 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
12162 {
12163 if (from_tty)
12164 printf_unfiltered ("%d ", b->number);
12165 delete_breakpoint (b);
12166 }
12167 if (from_tty)
12168 putchar_unfiltered ('\n');
12169
12170 do_cleanups (cleanups);
12171 }
12172 \f
12173 /* Delete breakpoint in BS if they are `delete' breakpoints and
12174 all breakpoints that are marked for deletion, whether hit or not.
12175 This is called after any breakpoint is hit, or after errors. */
12176
12177 void
12178 breakpoint_auto_delete (bpstat bs)
12179 {
12180 struct breakpoint *b, *b_tmp;
12181
12182 for (; bs; bs = bs->next)
12183 if (bs->breakpoint_at
12184 && bs->breakpoint_at->disposition == disp_del
12185 && bs->stop)
12186 delete_breakpoint (bs->breakpoint_at);
12187
12188 ALL_BREAKPOINTS_SAFE (b, b_tmp)
12189 {
12190 if (b->disposition == disp_del_at_next_stop)
12191 delete_breakpoint (b);
12192 }
12193 }
12194
12195 /* A comparison function for bp_location AP and BP being interfaced to
12196 qsort. Sort elements primarily by their ADDRESS (no matter what
12197 does breakpoint_address_is_meaningful say for its OWNER),
12198 secondarily by ordering first permanent elements and
12199 terciarily just ensuring the array is sorted stable way despite
12200 qsort being an unstable algorithm. */
12201
12202 static int
12203 bp_location_compare (const void *ap, const void *bp)
12204 {
12205 const struct bp_location *a = *(const struct bp_location **) ap;
12206 const struct bp_location *b = *(const struct bp_location **) bp;
12207
12208 if (a->address != b->address)
12209 return (a->address > b->address) - (a->address < b->address);
12210
12211 /* Sort locations at the same address by their pspace number, keeping
12212 locations of the same inferior (in a multi-inferior environment)
12213 grouped. */
12214
12215 if (a->pspace->num != b->pspace->num)
12216 return ((a->pspace->num > b->pspace->num)
12217 - (a->pspace->num < b->pspace->num));
12218
12219 /* Sort permanent breakpoints first. */
12220 if (a->permanent != b->permanent)
12221 return (a->permanent < b->permanent) - (a->permanent > b->permanent);
12222
12223 /* Make the internal GDB representation stable across GDB runs
12224 where A and B memory inside GDB can differ. Breakpoint locations of
12225 the same type at the same address can be sorted in arbitrary order. */
12226
12227 if (a->owner->number != b->owner->number)
12228 return ((a->owner->number > b->owner->number)
12229 - (a->owner->number < b->owner->number));
12230
12231 return (a > b) - (a < b);
12232 }
12233
12234 /* Set bp_location_placed_address_before_address_max and
12235 bp_location_shadow_len_after_address_max according to the current
12236 content of the bp_location array. */
12237
12238 static void
12239 bp_location_target_extensions_update (void)
12240 {
12241 struct bp_location *bl, **blp_tmp;
12242
12243 bp_location_placed_address_before_address_max = 0;
12244 bp_location_shadow_len_after_address_max = 0;
12245
12246 ALL_BP_LOCATIONS (bl, blp_tmp)
12247 {
12248 CORE_ADDR start, end, addr;
12249
12250 if (!bp_location_has_shadow (bl))
12251 continue;
12252
12253 start = bl->target_info.placed_address;
12254 end = start + bl->target_info.shadow_len;
12255
12256 gdb_assert (bl->address >= start);
12257 addr = bl->address - start;
12258 if (addr > bp_location_placed_address_before_address_max)
12259 bp_location_placed_address_before_address_max = addr;
12260
12261 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
12262
12263 gdb_assert (bl->address < end);
12264 addr = end - bl->address;
12265 if (addr > bp_location_shadow_len_after_address_max)
12266 bp_location_shadow_len_after_address_max = addr;
12267 }
12268 }
12269
12270 /* Download tracepoint locations if they haven't been. */
12271
12272 static void
12273 download_tracepoint_locations (void)
12274 {
12275 struct breakpoint *b;
12276 struct cleanup *old_chain;
12277 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
12278
12279 old_chain = save_current_space_and_thread ();
12280
12281 ALL_TRACEPOINTS (b)
12282 {
12283 struct bp_location *bl;
12284 struct tracepoint *t;
12285 int bp_location_downloaded = 0;
12286
12287 if ((b->type == bp_fast_tracepoint
12288 ? !may_insert_fast_tracepoints
12289 : !may_insert_tracepoints))
12290 continue;
12291
12292 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
12293 {
12294 if (target_can_download_tracepoint ())
12295 can_download_tracepoint = TRIBOOL_TRUE;
12296 else
12297 can_download_tracepoint = TRIBOOL_FALSE;
12298 }
12299
12300 if (can_download_tracepoint == TRIBOOL_FALSE)
12301 break;
12302
12303 for (bl = b->loc; bl; bl = bl->next)
12304 {
12305 /* In tracepoint, locations are _never_ duplicated, so
12306 should_be_inserted is equivalent to
12307 unduplicated_should_be_inserted. */
12308 if (!should_be_inserted (bl) || bl->inserted)
12309 continue;
12310
12311 switch_to_program_space_and_thread (bl->pspace);
12312
12313 target_download_tracepoint (bl);
12314
12315 bl->inserted = 1;
12316 bp_location_downloaded = 1;
12317 }
12318 t = (struct tracepoint *) b;
12319 t->number_on_target = b->number;
12320 if (bp_location_downloaded)
12321 observer_notify_breakpoint_modified (b);
12322 }
12323
12324 do_cleanups (old_chain);
12325 }
12326
12327 /* Swap the insertion/duplication state between two locations. */
12328
12329 static void
12330 swap_insertion (struct bp_location *left, struct bp_location *right)
12331 {
12332 const int left_inserted = left->inserted;
12333 const int left_duplicate = left->duplicate;
12334 const int left_needs_update = left->needs_update;
12335 const struct bp_target_info left_target_info = left->target_info;
12336
12337 /* Locations of tracepoints can never be duplicated. */
12338 if (is_tracepoint (left->owner))
12339 gdb_assert (!left->duplicate);
12340 if (is_tracepoint (right->owner))
12341 gdb_assert (!right->duplicate);
12342
12343 left->inserted = right->inserted;
12344 left->duplicate = right->duplicate;
12345 left->needs_update = right->needs_update;
12346 left->target_info = right->target_info;
12347 right->inserted = left_inserted;
12348 right->duplicate = left_duplicate;
12349 right->needs_update = left_needs_update;
12350 right->target_info = left_target_info;
12351 }
12352
12353 /* Force the re-insertion of the locations at ADDRESS. This is called
12354 once a new/deleted/modified duplicate location is found and we are evaluating
12355 conditions on the target's side. Such conditions need to be updated on
12356 the target. */
12357
12358 static void
12359 force_breakpoint_reinsertion (struct bp_location *bl)
12360 {
12361 struct bp_location **locp = NULL, **loc2p;
12362 struct bp_location *loc;
12363 CORE_ADDR address = 0;
12364 int pspace_num;
12365
12366 address = bl->address;
12367 pspace_num = bl->pspace->num;
12368
12369 /* This is only meaningful if the target is
12370 evaluating conditions and if the user has
12371 opted for condition evaluation on the target's
12372 side. */
12373 if (gdb_evaluates_breakpoint_condition_p ()
12374 || !target_supports_evaluation_of_breakpoint_conditions ())
12375 return;
12376
12377 /* Flag all breakpoint locations with this address and
12378 the same program space as the location
12379 as "its condition has changed". We need to
12380 update the conditions on the target's side. */
12381 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
12382 {
12383 loc = *loc2p;
12384
12385 if (!is_breakpoint (loc->owner)
12386 || pspace_num != loc->pspace->num)
12387 continue;
12388
12389 /* Flag the location appropriately. We use a different state to
12390 let everyone know that we already updated the set of locations
12391 with addr bl->address and program space bl->pspace. This is so
12392 we don't have to keep calling these functions just to mark locations
12393 that have already been marked. */
12394 loc->condition_changed = condition_updated;
12395
12396 /* Free the agent expression bytecode as well. We will compute
12397 it later on. */
12398 loc->cond_bytecode.reset ();
12399 }
12400 }
12401 /* Called whether new breakpoints are created, or existing breakpoints
12402 deleted, to update the global location list and recompute which
12403 locations are duplicate of which.
12404
12405 The INSERT_MODE flag determines whether locations may not, may, or
12406 shall be inserted now. See 'enum ugll_insert_mode' for more
12407 info. */
12408
12409 static void
12410 update_global_location_list (enum ugll_insert_mode insert_mode)
12411 {
12412 struct breakpoint *b;
12413 struct bp_location **locp, *loc;
12414 struct cleanup *cleanups;
12415 /* Last breakpoint location address that was marked for update. */
12416 CORE_ADDR last_addr = 0;
12417 /* Last breakpoint location program space that was marked for update. */
12418 int last_pspace_num = -1;
12419
12420 /* Used in the duplicates detection below. When iterating over all
12421 bp_locations, points to the first bp_location of a given address.
12422 Breakpoints and watchpoints of different types are never
12423 duplicates of each other. Keep one pointer for each type of
12424 breakpoint/watchpoint, so we only need to loop over all locations
12425 once. */
12426 struct bp_location *bp_loc_first; /* breakpoint */
12427 struct bp_location *wp_loc_first; /* hardware watchpoint */
12428 struct bp_location *awp_loc_first; /* access watchpoint */
12429 struct bp_location *rwp_loc_first; /* read watchpoint */
12430
12431 /* Saved former bp_location array which we compare against the newly
12432 built bp_location from the current state of ALL_BREAKPOINTS. */
12433 struct bp_location **old_location, **old_locp;
12434 unsigned old_location_count;
12435
12436 old_location = bp_location;
12437 old_location_count = bp_location_count;
12438 bp_location = NULL;
12439 bp_location_count = 0;
12440 cleanups = make_cleanup (xfree, old_location);
12441
12442 ALL_BREAKPOINTS (b)
12443 for (loc = b->loc; loc; loc = loc->next)
12444 bp_location_count++;
12445
12446 bp_location = XNEWVEC (struct bp_location *, bp_location_count);
12447 locp = bp_location;
12448 ALL_BREAKPOINTS (b)
12449 for (loc = b->loc; loc; loc = loc->next)
12450 *locp++ = loc;
12451 qsort (bp_location, bp_location_count, sizeof (*bp_location),
12452 bp_location_compare);
12453
12454 bp_location_target_extensions_update ();
12455
12456 /* Identify bp_location instances that are no longer present in the
12457 new list, and therefore should be freed. Note that it's not
12458 necessary that those locations should be removed from inferior --
12459 if there's another location at the same address (previously
12460 marked as duplicate), we don't need to remove/insert the
12461 location.
12462
12463 LOCP is kept in sync with OLD_LOCP, each pointing to the current
12464 and former bp_location array state respectively. */
12465
12466 locp = bp_location;
12467 for (old_locp = old_location; old_locp < old_location + old_location_count;
12468 old_locp++)
12469 {
12470 struct bp_location *old_loc = *old_locp;
12471 struct bp_location **loc2p;
12472
12473 /* Tells if 'old_loc' is found among the new locations. If
12474 not, we have to free it. */
12475 int found_object = 0;
12476 /* Tells if the location should remain inserted in the target. */
12477 int keep_in_target = 0;
12478 int removed = 0;
12479
12480 /* Skip LOCP entries which will definitely never be needed.
12481 Stop either at or being the one matching OLD_LOC. */
12482 while (locp < bp_location + bp_location_count
12483 && (*locp)->address < old_loc->address)
12484 locp++;
12485
12486 for (loc2p = locp;
12487 (loc2p < bp_location + bp_location_count
12488 && (*loc2p)->address == old_loc->address);
12489 loc2p++)
12490 {
12491 /* Check if this is a new/duplicated location or a duplicated
12492 location that had its condition modified. If so, we want to send
12493 its condition to the target if evaluation of conditions is taking
12494 place there. */
12495 if ((*loc2p)->condition_changed == condition_modified
12496 && (last_addr != old_loc->address
12497 || last_pspace_num != old_loc->pspace->num))
12498 {
12499 force_breakpoint_reinsertion (*loc2p);
12500 last_pspace_num = old_loc->pspace->num;
12501 }
12502
12503 if (*loc2p == old_loc)
12504 found_object = 1;
12505 }
12506
12507 /* We have already handled this address, update it so that we don't
12508 have to go through updates again. */
12509 last_addr = old_loc->address;
12510
12511 /* Target-side condition evaluation: Handle deleted locations. */
12512 if (!found_object)
12513 force_breakpoint_reinsertion (old_loc);
12514
12515 /* If this location is no longer present, and inserted, look if
12516 there's maybe a new location at the same address. If so,
12517 mark that one inserted, and don't remove this one. This is
12518 needed so that we don't have a time window where a breakpoint
12519 at certain location is not inserted. */
12520
12521 if (old_loc->inserted)
12522 {
12523 /* If the location is inserted now, we might have to remove
12524 it. */
12525
12526 if (found_object && should_be_inserted (old_loc))
12527 {
12528 /* The location is still present in the location list,
12529 and still should be inserted. Don't do anything. */
12530 keep_in_target = 1;
12531 }
12532 else
12533 {
12534 /* This location still exists, but it won't be kept in the
12535 target since it may have been disabled. We proceed to
12536 remove its target-side condition. */
12537
12538 /* The location is either no longer present, or got
12539 disabled. See if there's another location at the
12540 same address, in which case we don't need to remove
12541 this one from the target. */
12542
12543 /* OLD_LOC comes from existing struct breakpoint. */
12544 if (breakpoint_address_is_meaningful (old_loc->owner))
12545 {
12546 for (loc2p = locp;
12547 (loc2p < bp_location + bp_location_count
12548 && (*loc2p)->address == old_loc->address);
12549 loc2p++)
12550 {
12551 struct bp_location *loc2 = *loc2p;
12552
12553 if (breakpoint_locations_match (loc2, old_loc))
12554 {
12555 /* Read watchpoint locations are switched to
12556 access watchpoints, if the former are not
12557 supported, but the latter are. */
12558 if (is_hardware_watchpoint (old_loc->owner))
12559 {
12560 gdb_assert (is_hardware_watchpoint (loc2->owner));
12561 loc2->watchpoint_type = old_loc->watchpoint_type;
12562 }
12563
12564 /* loc2 is a duplicated location. We need to check
12565 if it should be inserted in case it will be
12566 unduplicated. */
12567 if (loc2 != old_loc
12568 && unduplicated_should_be_inserted (loc2))
12569 {
12570 swap_insertion (old_loc, loc2);
12571 keep_in_target = 1;
12572 break;
12573 }
12574 }
12575 }
12576 }
12577 }
12578
12579 if (!keep_in_target)
12580 {
12581 if (remove_breakpoint (old_loc))
12582 {
12583 /* This is just about all we can do. We could keep
12584 this location on the global list, and try to
12585 remove it next time, but there's no particular
12586 reason why we will succeed next time.
12587
12588 Note that at this point, old_loc->owner is still
12589 valid, as delete_breakpoint frees the breakpoint
12590 only after calling us. */
12591 printf_filtered (_("warning: Error removing "
12592 "breakpoint %d\n"),
12593 old_loc->owner->number);
12594 }
12595 removed = 1;
12596 }
12597 }
12598
12599 if (!found_object)
12600 {
12601 if (removed && target_is_non_stop_p ()
12602 && need_moribund_for_location_type (old_loc))
12603 {
12604 /* This location was removed from the target. In
12605 non-stop mode, a race condition is possible where
12606 we've removed a breakpoint, but stop events for that
12607 breakpoint are already queued and will arrive later.
12608 We apply an heuristic to be able to distinguish such
12609 SIGTRAPs from other random SIGTRAPs: we keep this
12610 breakpoint location for a bit, and will retire it
12611 after we see some number of events. The theory here
12612 is that reporting of events should, "on the average",
12613 be fair, so after a while we'll see events from all
12614 threads that have anything of interest, and no longer
12615 need to keep this breakpoint location around. We
12616 don't hold locations forever so to reduce chances of
12617 mistaking a non-breakpoint SIGTRAP for a breakpoint
12618 SIGTRAP.
12619
12620 The heuristic failing can be disastrous on
12621 decr_pc_after_break targets.
12622
12623 On decr_pc_after_break targets, like e.g., x86-linux,
12624 if we fail to recognize a late breakpoint SIGTRAP,
12625 because events_till_retirement has reached 0 too
12626 soon, we'll fail to do the PC adjustment, and report
12627 a random SIGTRAP to the user. When the user resumes
12628 the inferior, it will most likely immediately crash
12629 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
12630 corrupted, because of being resumed e.g., in the
12631 middle of a multi-byte instruction, or skipped a
12632 one-byte instruction. This was actually seen happen
12633 on native x86-linux, and should be less rare on
12634 targets that do not support new thread events, like
12635 remote, due to the heuristic depending on
12636 thread_count.
12637
12638 Mistaking a random SIGTRAP for a breakpoint trap
12639 causes similar symptoms (PC adjustment applied when
12640 it shouldn't), but then again, playing with SIGTRAPs
12641 behind the debugger's back is asking for trouble.
12642
12643 Since hardware watchpoint traps are always
12644 distinguishable from other traps, so we don't need to
12645 apply keep hardware watchpoint moribund locations
12646 around. We simply always ignore hardware watchpoint
12647 traps we can no longer explain. */
12648
12649 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12650 old_loc->owner = NULL;
12651
12652 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
12653 }
12654 else
12655 {
12656 old_loc->owner = NULL;
12657 decref_bp_location (&old_loc);
12658 }
12659 }
12660 }
12661
12662 /* Rescan breakpoints at the same address and section, marking the
12663 first one as "first" and any others as "duplicates". This is so
12664 that the bpt instruction is only inserted once. If we have a
12665 permanent breakpoint at the same place as BPT, make that one the
12666 official one, and the rest as duplicates. Permanent breakpoints
12667 are sorted first for the same address.
12668
12669 Do the same for hardware watchpoints, but also considering the
12670 watchpoint's type (regular/access/read) and length. */
12671
12672 bp_loc_first = NULL;
12673 wp_loc_first = NULL;
12674 awp_loc_first = NULL;
12675 rwp_loc_first = NULL;
12676 ALL_BP_LOCATIONS (loc, locp)
12677 {
12678 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12679 non-NULL. */
12680 struct bp_location **loc_first_p;
12681 b = loc->owner;
12682
12683 if (!unduplicated_should_be_inserted (loc)
12684 || !breakpoint_address_is_meaningful (b)
12685 /* Don't detect duplicate for tracepoint locations because they are
12686 never duplicated. See the comments in field `duplicate' of
12687 `struct bp_location'. */
12688 || is_tracepoint (b))
12689 {
12690 /* Clear the condition modification flag. */
12691 loc->condition_changed = condition_unchanged;
12692 continue;
12693 }
12694
12695 if (b->type == bp_hardware_watchpoint)
12696 loc_first_p = &wp_loc_first;
12697 else if (b->type == bp_read_watchpoint)
12698 loc_first_p = &rwp_loc_first;
12699 else if (b->type == bp_access_watchpoint)
12700 loc_first_p = &awp_loc_first;
12701 else
12702 loc_first_p = &bp_loc_first;
12703
12704 if (*loc_first_p == NULL
12705 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12706 || !breakpoint_locations_match (loc, *loc_first_p))
12707 {
12708 *loc_first_p = loc;
12709 loc->duplicate = 0;
12710
12711 if (is_breakpoint (loc->owner) && loc->condition_changed)
12712 {
12713 loc->needs_update = 1;
12714 /* Clear the condition modification flag. */
12715 loc->condition_changed = condition_unchanged;
12716 }
12717 continue;
12718 }
12719
12720
12721 /* This and the above ensure the invariant that the first location
12722 is not duplicated, and is the inserted one.
12723 All following are marked as duplicated, and are not inserted. */
12724 if (loc->inserted)
12725 swap_insertion (loc, *loc_first_p);
12726 loc->duplicate = 1;
12727
12728 /* Clear the condition modification flag. */
12729 loc->condition_changed = condition_unchanged;
12730 }
12731
12732 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
12733 {
12734 if (insert_mode != UGLL_DONT_INSERT)
12735 insert_breakpoint_locations ();
12736 else
12737 {
12738 /* Even though the caller told us to not insert new
12739 locations, we may still need to update conditions on the
12740 target's side of breakpoints that were already inserted
12741 if the target is evaluating breakpoint conditions. We
12742 only update conditions for locations that are marked
12743 "needs_update". */
12744 update_inserted_breakpoint_locations ();
12745 }
12746 }
12747
12748 if (insert_mode != UGLL_DONT_INSERT)
12749 download_tracepoint_locations ();
12750
12751 do_cleanups (cleanups);
12752 }
12753
12754 void
12755 breakpoint_retire_moribund (void)
12756 {
12757 struct bp_location *loc;
12758 int ix;
12759
12760 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12761 if (--(loc->events_till_retirement) == 0)
12762 {
12763 decref_bp_location (&loc);
12764 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12765 --ix;
12766 }
12767 }
12768
12769 static void
12770 update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
12771 {
12772
12773 TRY
12774 {
12775 update_global_location_list (insert_mode);
12776 }
12777 CATCH (e, RETURN_MASK_ERROR)
12778 {
12779 }
12780 END_CATCH
12781 }
12782
12783 /* Clear BKP from a BPS. */
12784
12785 static void
12786 bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12787 {
12788 bpstat bs;
12789
12790 for (bs = bps; bs; bs = bs->next)
12791 if (bs->breakpoint_at == bpt)
12792 {
12793 bs->breakpoint_at = NULL;
12794 bs->old_val = NULL;
12795 /* bs->commands will be freed later. */
12796 }
12797 }
12798
12799 /* Callback for iterate_over_threads. */
12800 static int
12801 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12802 {
12803 struct breakpoint *bpt = (struct breakpoint *) data;
12804
12805 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12806 return 0;
12807 }
12808
12809 /* Helper for breakpoint and tracepoint breakpoint_ops->mention
12810 callbacks. */
12811
12812 static void
12813 say_where (struct breakpoint *b)
12814 {
12815 struct value_print_options opts;
12816
12817 get_user_print_options (&opts);
12818
12819 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12820 single string. */
12821 if (b->loc == NULL)
12822 {
12823 /* For pending locations, the output differs slightly based
12824 on b->extra_string. If this is non-NULL, it contains either
12825 a condition or dprintf arguments. */
12826 if (b->extra_string == NULL)
12827 {
12828 printf_filtered (_(" (%s) pending."),
12829 event_location_to_string (b->location));
12830 }
12831 else if (b->type == bp_dprintf)
12832 {
12833 printf_filtered (_(" (%s,%s) pending."),
12834 event_location_to_string (b->location),
12835 b->extra_string);
12836 }
12837 else
12838 {
12839 printf_filtered (_(" (%s %s) pending."),
12840 event_location_to_string (b->location),
12841 b->extra_string);
12842 }
12843 }
12844 else
12845 {
12846 if (opts.addressprint || b->loc->symtab == NULL)
12847 {
12848 printf_filtered (" at ");
12849 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12850 gdb_stdout);
12851 }
12852 if (b->loc->symtab != NULL)
12853 {
12854 /* If there is a single location, we can print the location
12855 more nicely. */
12856 if (b->loc->next == NULL)
12857 printf_filtered (": file %s, line %d.",
12858 symtab_to_filename_for_display (b->loc->symtab),
12859 b->loc->line_number);
12860 else
12861 /* This is not ideal, but each location may have a
12862 different file name, and this at least reflects the
12863 real situation somewhat. */
12864 printf_filtered (": %s.",
12865 event_location_to_string (b->location));
12866 }
12867
12868 if (b->loc->next)
12869 {
12870 struct bp_location *loc = b->loc;
12871 int n = 0;
12872 for (; loc; loc = loc->next)
12873 ++n;
12874 printf_filtered (" (%d locations)", n);
12875 }
12876 }
12877 }
12878
12879 /* Default bp_location_ops methods. */
12880
12881 static void
12882 bp_location_dtor (struct bp_location *self)
12883 {
12884 xfree (self->function_name);
12885 }
12886
12887 static const struct bp_location_ops bp_location_ops =
12888 {
12889 bp_location_dtor
12890 };
12891
12892 /* Default breakpoint_ops methods all breakpoint_ops ultimately
12893 inherit from. */
12894
12895 static void
12896 base_breakpoint_dtor (struct breakpoint *self)
12897 {
12898 decref_counted_command_line (&self->commands);
12899 xfree (self->cond_string);
12900 xfree (self->extra_string);
12901 xfree (self->filter);
12902 delete_event_location (self->location);
12903 delete_event_location (self->location_range_end);
12904 }
12905
12906 static struct bp_location *
12907 base_breakpoint_allocate_location (struct breakpoint *self)
12908 {
12909 struct bp_location *loc;
12910
12911 loc = new struct bp_location ();
12912 init_bp_location (loc, &bp_location_ops, self);
12913 return loc;
12914 }
12915
12916 static void
12917 base_breakpoint_re_set (struct breakpoint *b)
12918 {
12919 /* Nothing to re-set. */
12920 }
12921
12922 #define internal_error_pure_virtual_called() \
12923 gdb_assert_not_reached ("pure virtual function called")
12924
12925 static int
12926 base_breakpoint_insert_location (struct bp_location *bl)
12927 {
12928 internal_error_pure_virtual_called ();
12929 }
12930
12931 static int
12932 base_breakpoint_remove_location (struct bp_location *bl,
12933 enum remove_bp_reason reason)
12934 {
12935 internal_error_pure_virtual_called ();
12936 }
12937
12938 static int
12939 base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12940 struct address_space *aspace,
12941 CORE_ADDR bp_addr,
12942 const struct target_waitstatus *ws)
12943 {
12944 internal_error_pure_virtual_called ();
12945 }
12946
12947 static void
12948 base_breakpoint_check_status (bpstat bs)
12949 {
12950 /* Always stop. */
12951 }
12952
12953 /* A "works_in_software_mode" breakpoint_ops method that just internal
12954 errors. */
12955
12956 static int
12957 base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12958 {
12959 internal_error_pure_virtual_called ();
12960 }
12961
12962 /* A "resources_needed" breakpoint_ops method that just internal
12963 errors. */
12964
12965 static int
12966 base_breakpoint_resources_needed (const struct bp_location *bl)
12967 {
12968 internal_error_pure_virtual_called ();
12969 }
12970
12971 static enum print_stop_action
12972 base_breakpoint_print_it (bpstat bs)
12973 {
12974 internal_error_pure_virtual_called ();
12975 }
12976
12977 static void
12978 base_breakpoint_print_one_detail (const struct breakpoint *self,
12979 struct ui_out *uiout)
12980 {
12981 /* nothing */
12982 }
12983
12984 static void
12985 base_breakpoint_print_mention (struct breakpoint *b)
12986 {
12987 internal_error_pure_virtual_called ();
12988 }
12989
12990 static void
12991 base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12992 {
12993 internal_error_pure_virtual_called ();
12994 }
12995
12996 static void
12997 base_breakpoint_create_sals_from_location
12998 (const struct event_location *location,
12999 struct linespec_result *canonical,
13000 enum bptype type_wanted)
13001 {
13002 internal_error_pure_virtual_called ();
13003 }
13004
13005 static void
13006 base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13007 struct linespec_result *c,
13008 char *cond_string,
13009 char *extra_string,
13010 enum bptype type_wanted,
13011 enum bpdisp disposition,
13012 int thread,
13013 int task, int ignore_count,
13014 const struct breakpoint_ops *o,
13015 int from_tty, int enabled,
13016 int internal, unsigned flags)
13017 {
13018 internal_error_pure_virtual_called ();
13019 }
13020
13021 static void
13022 base_breakpoint_decode_location (struct breakpoint *b,
13023 const struct event_location *location,
13024 struct program_space *search_pspace,
13025 struct symtabs_and_lines *sals)
13026 {
13027 internal_error_pure_virtual_called ();
13028 }
13029
13030 /* The default 'explains_signal' method. */
13031
13032 static int
13033 base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
13034 {
13035 return 1;
13036 }
13037
13038 /* The default "after_condition_true" method. */
13039
13040 static void
13041 base_breakpoint_after_condition_true (struct bpstats *bs)
13042 {
13043 /* Nothing to do. */
13044 }
13045
13046 struct breakpoint_ops base_breakpoint_ops =
13047 {
13048 base_breakpoint_dtor,
13049 base_breakpoint_allocate_location,
13050 base_breakpoint_re_set,
13051 base_breakpoint_insert_location,
13052 base_breakpoint_remove_location,
13053 base_breakpoint_breakpoint_hit,
13054 base_breakpoint_check_status,
13055 base_breakpoint_resources_needed,
13056 base_breakpoint_works_in_software_mode,
13057 base_breakpoint_print_it,
13058 NULL,
13059 base_breakpoint_print_one_detail,
13060 base_breakpoint_print_mention,
13061 base_breakpoint_print_recreate,
13062 base_breakpoint_create_sals_from_location,
13063 base_breakpoint_create_breakpoints_sal,
13064 base_breakpoint_decode_location,
13065 base_breakpoint_explains_signal,
13066 base_breakpoint_after_condition_true,
13067 };
13068
13069 /* Default breakpoint_ops methods. */
13070
13071 static void
13072 bkpt_re_set (struct breakpoint *b)
13073 {
13074 /* FIXME: is this still reachable? */
13075 if (breakpoint_event_location_empty_p (b))
13076 {
13077 /* Anything without a location can't be re-set. */
13078 delete_breakpoint (b);
13079 return;
13080 }
13081
13082 breakpoint_re_set_default (b);
13083 }
13084
13085 static int
13086 bkpt_insert_location (struct bp_location *bl)
13087 {
13088 CORE_ADDR addr = bl->target_info.reqstd_address;
13089
13090 bl->target_info.kind = breakpoint_kind (bl, &addr);
13091 bl->target_info.placed_address = addr;
13092
13093 if (bl->loc_type == bp_loc_hardware_breakpoint)
13094 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
13095 else
13096 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
13097 }
13098
13099 static int
13100 bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
13101 {
13102 if (bl->loc_type == bp_loc_hardware_breakpoint)
13103 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
13104 else
13105 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
13106 }
13107
13108 static int
13109 bkpt_breakpoint_hit (const struct bp_location *bl,
13110 struct address_space *aspace, CORE_ADDR bp_addr,
13111 const struct target_waitstatus *ws)
13112 {
13113 if (ws->kind != TARGET_WAITKIND_STOPPED
13114 || ws->value.sig != GDB_SIGNAL_TRAP)
13115 return 0;
13116
13117 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
13118 aspace, bp_addr))
13119 return 0;
13120
13121 if (overlay_debugging /* unmapped overlay section */
13122 && section_is_overlay (bl->section)
13123 && !section_is_mapped (bl->section))
13124 return 0;
13125
13126 return 1;
13127 }
13128
13129 static int
13130 dprintf_breakpoint_hit (const struct bp_location *bl,
13131 struct address_space *aspace, CORE_ADDR bp_addr,
13132 const struct target_waitstatus *ws)
13133 {
13134 if (dprintf_style == dprintf_style_agent
13135 && target_can_run_breakpoint_commands ())
13136 {
13137 /* An agent-style dprintf never causes a stop. If we see a trap
13138 for this address it must be for a breakpoint that happens to
13139 be set at the same address. */
13140 return 0;
13141 }
13142
13143 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
13144 }
13145
13146 static int
13147 bkpt_resources_needed (const struct bp_location *bl)
13148 {
13149 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
13150
13151 return 1;
13152 }
13153
13154 static enum print_stop_action
13155 bkpt_print_it (bpstat bs)
13156 {
13157 struct breakpoint *b;
13158 const struct bp_location *bl;
13159 int bp_temp;
13160 struct ui_out *uiout = current_uiout;
13161
13162 gdb_assert (bs->bp_location_at != NULL);
13163
13164 bl = bs->bp_location_at;
13165 b = bs->breakpoint_at;
13166
13167 bp_temp = b->disposition == disp_del;
13168 if (bl->address != bl->requested_address)
13169 breakpoint_adjustment_warning (bl->requested_address,
13170 bl->address,
13171 b->number, 1);
13172 annotate_breakpoint (b->number);
13173 maybe_print_thread_hit_breakpoint (uiout);
13174
13175 if (bp_temp)
13176 ui_out_text (uiout, "Temporary breakpoint ");
13177 else
13178 ui_out_text (uiout, "Breakpoint ");
13179 if (ui_out_is_mi_like_p (uiout))
13180 {
13181 ui_out_field_string (uiout, "reason",
13182 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
13183 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
13184 }
13185 ui_out_field_int (uiout, "bkptno", b->number);
13186 ui_out_text (uiout, ", ");
13187
13188 return PRINT_SRC_AND_LOC;
13189 }
13190
13191 static void
13192 bkpt_print_mention (struct breakpoint *b)
13193 {
13194 if (ui_out_is_mi_like_p (current_uiout))
13195 return;
13196
13197 switch (b->type)
13198 {
13199 case bp_breakpoint:
13200 case bp_gnu_ifunc_resolver:
13201 if (b->disposition == disp_del)
13202 printf_filtered (_("Temporary breakpoint"));
13203 else
13204 printf_filtered (_("Breakpoint"));
13205 printf_filtered (_(" %d"), b->number);
13206 if (b->type == bp_gnu_ifunc_resolver)
13207 printf_filtered (_(" at gnu-indirect-function resolver"));
13208 break;
13209 case bp_hardware_breakpoint:
13210 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
13211 break;
13212 case bp_dprintf:
13213 printf_filtered (_("Dprintf %d"), b->number);
13214 break;
13215 }
13216
13217 say_where (b);
13218 }
13219
13220 static void
13221 bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13222 {
13223 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
13224 fprintf_unfiltered (fp, "tbreak");
13225 else if (tp->type == bp_breakpoint)
13226 fprintf_unfiltered (fp, "break");
13227 else if (tp->type == bp_hardware_breakpoint
13228 && tp->disposition == disp_del)
13229 fprintf_unfiltered (fp, "thbreak");
13230 else if (tp->type == bp_hardware_breakpoint)
13231 fprintf_unfiltered (fp, "hbreak");
13232 else
13233 internal_error (__FILE__, __LINE__,
13234 _("unhandled breakpoint type %d"), (int) tp->type);
13235
13236 fprintf_unfiltered (fp, " %s",
13237 event_location_to_string (tp->location));
13238
13239 /* Print out extra_string if this breakpoint is pending. It might
13240 contain, for example, conditions that were set by the user. */
13241 if (tp->loc == NULL && tp->extra_string != NULL)
13242 fprintf_unfiltered (fp, " %s", tp->extra_string);
13243
13244 print_recreate_thread (tp, fp);
13245 }
13246
13247 static void
13248 bkpt_create_sals_from_location (const struct event_location *location,
13249 struct linespec_result *canonical,
13250 enum bptype type_wanted)
13251 {
13252 create_sals_from_location_default (location, canonical, type_wanted);
13253 }
13254
13255 static void
13256 bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
13257 struct linespec_result *canonical,
13258 char *cond_string,
13259 char *extra_string,
13260 enum bptype type_wanted,
13261 enum bpdisp disposition,
13262 int thread,
13263 int task, int ignore_count,
13264 const struct breakpoint_ops *ops,
13265 int from_tty, int enabled,
13266 int internal, unsigned flags)
13267 {
13268 create_breakpoints_sal_default (gdbarch, canonical,
13269 cond_string, extra_string,
13270 type_wanted,
13271 disposition, thread, task,
13272 ignore_count, ops, from_tty,
13273 enabled, internal, flags);
13274 }
13275
13276 static void
13277 bkpt_decode_location (struct breakpoint *b,
13278 const struct event_location *location,
13279 struct program_space *search_pspace,
13280 struct symtabs_and_lines *sals)
13281 {
13282 decode_location_default (b, location, search_pspace, sals);
13283 }
13284
13285 /* Virtual table for internal breakpoints. */
13286
13287 static void
13288 internal_bkpt_re_set (struct breakpoint *b)
13289 {
13290 switch (b->type)
13291 {
13292 /* Delete overlay event and longjmp master breakpoints; they
13293 will be reset later by breakpoint_re_set. */
13294 case bp_overlay_event:
13295 case bp_longjmp_master:
13296 case bp_std_terminate_master:
13297 case bp_exception_master:
13298 delete_breakpoint (b);
13299 break;
13300
13301 /* This breakpoint is special, it's set up when the inferior
13302 starts and we really don't want to touch it. */
13303 case bp_shlib_event:
13304
13305 /* Like bp_shlib_event, this breakpoint type is special. Once
13306 it is set up, we do not want to touch it. */
13307 case bp_thread_event:
13308 break;
13309 }
13310 }
13311
13312 static void
13313 internal_bkpt_check_status (bpstat bs)
13314 {
13315 if (bs->breakpoint_at->type == bp_shlib_event)
13316 {
13317 /* If requested, stop when the dynamic linker notifies GDB of
13318 events. This allows the user to get control and place
13319 breakpoints in initializer routines for dynamically loaded
13320 objects (among other things). */
13321 bs->stop = stop_on_solib_events;
13322 bs->print = stop_on_solib_events;
13323 }
13324 else
13325 bs->stop = 0;
13326 }
13327
13328 static enum print_stop_action
13329 internal_bkpt_print_it (bpstat bs)
13330 {
13331 struct breakpoint *b;
13332
13333 b = bs->breakpoint_at;
13334
13335 switch (b->type)
13336 {
13337 case bp_shlib_event:
13338 /* Did we stop because the user set the stop_on_solib_events
13339 variable? (If so, we report this as a generic, "Stopped due
13340 to shlib event" message.) */
13341 print_solib_event (0);
13342 break;
13343
13344 case bp_thread_event:
13345 /* Not sure how we will get here.
13346 GDB should not stop for these breakpoints. */
13347 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
13348 break;
13349
13350 case bp_overlay_event:
13351 /* By analogy with the thread event, GDB should not stop for these. */
13352 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
13353 break;
13354
13355 case bp_longjmp_master:
13356 /* These should never be enabled. */
13357 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
13358 break;
13359
13360 case bp_std_terminate_master:
13361 /* These should never be enabled. */
13362 printf_filtered (_("std::terminate Master Breakpoint: "
13363 "gdb should not stop!\n"));
13364 break;
13365
13366 case bp_exception_master:
13367 /* These should never be enabled. */
13368 printf_filtered (_("Exception Master Breakpoint: "
13369 "gdb should not stop!\n"));
13370 break;
13371 }
13372
13373 return PRINT_NOTHING;
13374 }
13375
13376 static void
13377 internal_bkpt_print_mention (struct breakpoint *b)
13378 {
13379 /* Nothing to mention. These breakpoints are internal. */
13380 }
13381
13382 /* Virtual table for momentary breakpoints */
13383
13384 static void
13385 momentary_bkpt_re_set (struct breakpoint *b)
13386 {
13387 /* Keep temporary breakpoints, which can be encountered when we step
13388 over a dlopen call and solib_add is resetting the breakpoints.
13389 Otherwise these should have been blown away via the cleanup chain
13390 or by breakpoint_init_inferior when we rerun the executable. */
13391 }
13392
13393 static void
13394 momentary_bkpt_check_status (bpstat bs)
13395 {
13396 /* Nothing. The point of these breakpoints is causing a stop. */
13397 }
13398
13399 static enum print_stop_action
13400 momentary_bkpt_print_it (bpstat bs)
13401 {
13402 return PRINT_UNKNOWN;
13403 }
13404
13405 static void
13406 momentary_bkpt_print_mention (struct breakpoint *b)
13407 {
13408 /* Nothing to mention. These breakpoints are internal. */
13409 }
13410
13411 /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
13412
13413 It gets cleared already on the removal of the first one of such placed
13414 breakpoints. This is OK as they get all removed altogether. */
13415
13416 static void
13417 longjmp_bkpt_dtor (struct breakpoint *self)
13418 {
13419 struct thread_info *tp = find_thread_global_id (self->thread);
13420
13421 if (tp)
13422 tp->initiating_frame = null_frame_id;
13423
13424 momentary_breakpoint_ops.dtor (self);
13425 }
13426
13427 /* Specific methods for probe breakpoints. */
13428
13429 static int
13430 bkpt_probe_insert_location (struct bp_location *bl)
13431 {
13432 int v = bkpt_insert_location (bl);
13433
13434 if (v == 0)
13435 {
13436 /* The insertion was successful, now let's set the probe's semaphore
13437 if needed. */
13438 if (bl->probe.probe->pops->set_semaphore != NULL)
13439 bl->probe.probe->pops->set_semaphore (bl->probe.probe,
13440 bl->probe.objfile,
13441 bl->gdbarch);
13442 }
13443
13444 return v;
13445 }
13446
13447 static int
13448 bkpt_probe_remove_location (struct bp_location *bl,
13449 enum remove_bp_reason reason)
13450 {
13451 /* Let's clear the semaphore before removing the location. */
13452 if (bl->probe.probe->pops->clear_semaphore != NULL)
13453 bl->probe.probe->pops->clear_semaphore (bl->probe.probe,
13454 bl->probe.objfile,
13455 bl->gdbarch);
13456
13457 return bkpt_remove_location (bl, reason);
13458 }
13459
13460 static void
13461 bkpt_probe_create_sals_from_location (const struct event_location *location,
13462 struct linespec_result *canonical,
13463 enum bptype type_wanted)
13464 {
13465 struct linespec_sals lsal;
13466
13467 lsal.sals = parse_probes (location, NULL, canonical);
13468 lsal.canonical = xstrdup (event_location_to_string (canonical->location));
13469 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13470 }
13471
13472 static void
13473 bkpt_probe_decode_location (struct breakpoint *b,
13474 const struct event_location *location,
13475 struct program_space *search_pspace,
13476 struct symtabs_and_lines *sals)
13477 {
13478 *sals = parse_probes (location, search_pspace, NULL);
13479 if (!sals->sals)
13480 error (_("probe not found"));
13481 }
13482
13483 /* The breakpoint_ops structure to be used in tracepoints. */
13484
13485 static void
13486 tracepoint_re_set (struct breakpoint *b)
13487 {
13488 breakpoint_re_set_default (b);
13489 }
13490
13491 static int
13492 tracepoint_breakpoint_hit (const struct bp_location *bl,
13493 struct address_space *aspace, CORE_ADDR bp_addr,
13494 const struct target_waitstatus *ws)
13495 {
13496 /* By definition, the inferior does not report stops at
13497 tracepoints. */
13498 return 0;
13499 }
13500
13501 static void
13502 tracepoint_print_one_detail (const struct breakpoint *self,
13503 struct ui_out *uiout)
13504 {
13505 struct tracepoint *tp = (struct tracepoint *) self;
13506 if (tp->static_trace_marker_id)
13507 {
13508 gdb_assert (self->type == bp_static_tracepoint);
13509
13510 ui_out_text (uiout, "\tmarker id is ");
13511 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
13512 tp->static_trace_marker_id);
13513 ui_out_text (uiout, "\n");
13514 }
13515 }
13516
13517 static void
13518 tracepoint_print_mention (struct breakpoint *b)
13519 {
13520 if (ui_out_is_mi_like_p (current_uiout))
13521 return;
13522
13523 switch (b->type)
13524 {
13525 case bp_tracepoint:
13526 printf_filtered (_("Tracepoint"));
13527 printf_filtered (_(" %d"), b->number);
13528 break;
13529 case bp_fast_tracepoint:
13530 printf_filtered (_("Fast tracepoint"));
13531 printf_filtered (_(" %d"), b->number);
13532 break;
13533 case bp_static_tracepoint:
13534 printf_filtered (_("Static tracepoint"));
13535 printf_filtered (_(" %d"), b->number);
13536 break;
13537 default:
13538 internal_error (__FILE__, __LINE__,
13539 _("unhandled tracepoint type %d"), (int) b->type);
13540 }
13541
13542 say_where (b);
13543 }
13544
13545 static void
13546 tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
13547 {
13548 struct tracepoint *tp = (struct tracepoint *) self;
13549
13550 if (self->type == bp_fast_tracepoint)
13551 fprintf_unfiltered (fp, "ftrace");
13552 else if (self->type == bp_static_tracepoint)
13553 fprintf_unfiltered (fp, "strace");
13554 else if (self->type == bp_tracepoint)
13555 fprintf_unfiltered (fp, "trace");
13556 else
13557 internal_error (__FILE__, __LINE__,
13558 _("unhandled tracepoint type %d"), (int) self->type);
13559
13560 fprintf_unfiltered (fp, " %s",
13561 event_location_to_string (self->location));
13562 print_recreate_thread (self, fp);
13563
13564 if (tp->pass_count)
13565 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
13566 }
13567
13568 static void
13569 tracepoint_create_sals_from_location (const struct event_location *location,
13570 struct linespec_result *canonical,
13571 enum bptype type_wanted)
13572 {
13573 create_sals_from_location_default (location, canonical, type_wanted);
13574 }
13575
13576 static void
13577 tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13578 struct linespec_result *canonical,
13579 char *cond_string,
13580 char *extra_string,
13581 enum bptype type_wanted,
13582 enum bpdisp disposition,
13583 int thread,
13584 int task, int ignore_count,
13585 const struct breakpoint_ops *ops,
13586 int from_tty, int enabled,
13587 int internal, unsigned flags)
13588 {
13589 create_breakpoints_sal_default (gdbarch, canonical,
13590 cond_string, extra_string,
13591 type_wanted,
13592 disposition, thread, task,
13593 ignore_count, ops, from_tty,
13594 enabled, internal, flags);
13595 }
13596
13597 static void
13598 tracepoint_decode_location (struct breakpoint *b,
13599 const struct event_location *location,
13600 struct program_space *search_pspace,
13601 struct symtabs_and_lines *sals)
13602 {
13603 decode_location_default (b, location, search_pspace, sals);
13604 }
13605
13606 struct breakpoint_ops tracepoint_breakpoint_ops;
13607
13608 /* The breakpoint_ops structure to be use on tracepoints placed in a
13609 static probe. */
13610
13611 static void
13612 tracepoint_probe_create_sals_from_location
13613 (const struct event_location *location,
13614 struct linespec_result *canonical,
13615 enum bptype type_wanted)
13616 {
13617 /* We use the same method for breakpoint on probes. */
13618 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
13619 }
13620
13621 static void
13622 tracepoint_probe_decode_location (struct breakpoint *b,
13623 const struct event_location *location,
13624 struct program_space *search_pspace,
13625 struct symtabs_and_lines *sals)
13626 {
13627 /* We use the same method for breakpoint on probes. */
13628 bkpt_probe_decode_location (b, location, search_pspace, sals);
13629 }
13630
13631 static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13632
13633 /* Dprintf breakpoint_ops methods. */
13634
13635 static void
13636 dprintf_re_set (struct breakpoint *b)
13637 {
13638 breakpoint_re_set_default (b);
13639
13640 /* extra_string should never be non-NULL for dprintf. */
13641 gdb_assert (b->extra_string != NULL);
13642
13643 /* 1 - connect to target 1, that can run breakpoint commands.
13644 2 - create a dprintf, which resolves fine.
13645 3 - disconnect from target 1
13646 4 - connect to target 2, that can NOT run breakpoint commands.
13647
13648 After steps #3/#4, you'll want the dprintf command list to
13649 be updated, because target 1 and 2 may well return different
13650 answers for target_can_run_breakpoint_commands().
13651 Given absence of finer grained resetting, we get to do
13652 it all the time. */
13653 if (b->extra_string != NULL)
13654 update_dprintf_command_list (b);
13655 }
13656
13657 /* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13658
13659 static void
13660 dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13661 {
13662 fprintf_unfiltered (fp, "dprintf %s,%s",
13663 event_location_to_string (tp->location),
13664 tp->extra_string);
13665 print_recreate_thread (tp, fp);
13666 }
13667
13668 /* Implement the "after_condition_true" breakpoint_ops method for
13669 dprintf.
13670
13671 dprintf's are implemented with regular commands in their command
13672 list, but we run the commands here instead of before presenting the
13673 stop to the user, as dprintf's don't actually cause a stop. This
13674 also makes it so that the commands of multiple dprintfs at the same
13675 address are all handled. */
13676
13677 static void
13678 dprintf_after_condition_true (struct bpstats *bs)
13679 {
13680 struct cleanup *old_chain;
13681 struct bpstats tmp_bs = { NULL };
13682 struct bpstats *tmp_bs_p = &tmp_bs;
13683
13684 /* dprintf's never cause a stop. This wasn't set in the
13685 check_status hook instead because that would make the dprintf's
13686 condition not be evaluated. */
13687 bs->stop = 0;
13688
13689 /* Run the command list here. Take ownership of it instead of
13690 copying. We never want these commands to run later in
13691 bpstat_do_actions, if a breakpoint that causes a stop happens to
13692 be set at same address as this dprintf, or even if running the
13693 commands here throws. */
13694 tmp_bs.commands = bs->commands;
13695 bs->commands = NULL;
13696 old_chain = make_cleanup_decref_counted_command_line (&tmp_bs.commands);
13697
13698 bpstat_do_actions_1 (&tmp_bs_p);
13699
13700 /* 'tmp_bs.commands' will usually be NULL by now, but
13701 bpstat_do_actions_1 may return early without processing the whole
13702 list. */
13703 do_cleanups (old_chain);
13704 }
13705
13706 /* The breakpoint_ops structure to be used on static tracepoints with
13707 markers (`-m'). */
13708
13709 static void
13710 strace_marker_create_sals_from_location (const struct event_location *location,
13711 struct linespec_result *canonical,
13712 enum bptype type_wanted)
13713 {
13714 struct linespec_sals lsal;
13715 const char *arg_start, *arg;
13716 char *str;
13717 struct cleanup *cleanup;
13718
13719 arg = arg_start = get_linespec_location (location);
13720 lsal.sals = decode_static_tracepoint_spec (&arg);
13721
13722 str = savestring (arg_start, arg - arg_start);
13723 cleanup = make_cleanup (xfree, str);
13724 canonical->location = new_linespec_location (&str);
13725 do_cleanups (cleanup);
13726
13727 lsal.canonical = xstrdup (event_location_to_string (canonical->location));
13728 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13729 }
13730
13731 static void
13732 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13733 struct linespec_result *canonical,
13734 char *cond_string,
13735 char *extra_string,
13736 enum bptype type_wanted,
13737 enum bpdisp disposition,
13738 int thread,
13739 int task, int ignore_count,
13740 const struct breakpoint_ops *ops,
13741 int from_tty, int enabled,
13742 int internal, unsigned flags)
13743 {
13744 int i;
13745 struct linespec_sals *lsal = VEC_index (linespec_sals,
13746 canonical->sals, 0);
13747
13748 /* If the user is creating a static tracepoint by marker id
13749 (strace -m MARKER_ID), then store the sals index, so that
13750 breakpoint_re_set can try to match up which of the newly
13751 found markers corresponds to this one, and, don't try to
13752 expand multiple locations for each sal, given than SALS
13753 already should contain all sals for MARKER_ID. */
13754
13755 for (i = 0; i < lsal->sals.nelts; ++i)
13756 {
13757 struct symtabs_and_lines expanded;
13758 struct tracepoint *tp;
13759 struct cleanup *old_chain;
13760 struct event_location *location;
13761
13762 expanded.nelts = 1;
13763 expanded.sals = &lsal->sals.sals[i];
13764
13765 location = copy_event_location (canonical->location);
13766 old_chain = make_cleanup_delete_event_location (location);
13767
13768 tp = new tracepoint ();
13769 init_breakpoint_sal (&tp->base, gdbarch, expanded,
13770 location, NULL,
13771 cond_string, extra_string,
13772 type_wanted, disposition,
13773 thread, task, ignore_count, ops,
13774 from_tty, enabled, internal, flags,
13775 canonical->special_display);
13776 /* Given that its possible to have multiple markers with
13777 the same string id, if the user is creating a static
13778 tracepoint by marker id ("strace -m MARKER_ID"), then
13779 store the sals index, so that breakpoint_re_set can
13780 try to match up which of the newly found markers
13781 corresponds to this one */
13782 tp->static_trace_marker_id_idx = i;
13783
13784 install_breakpoint (internal, &tp->base, 0);
13785
13786 discard_cleanups (old_chain);
13787 }
13788 }
13789
13790 static void
13791 strace_marker_decode_location (struct breakpoint *b,
13792 const struct event_location *location,
13793 struct program_space *search_pspace,
13794 struct symtabs_and_lines *sals)
13795 {
13796 struct tracepoint *tp = (struct tracepoint *) b;
13797 const char *s = get_linespec_location (location);
13798
13799 *sals = decode_static_tracepoint_spec (&s);
13800 if (sals->nelts > tp->static_trace_marker_id_idx)
13801 {
13802 sals->sals[0] = sals->sals[tp->static_trace_marker_id_idx];
13803 sals->nelts = 1;
13804 }
13805 else
13806 error (_("marker %s not found"), tp->static_trace_marker_id);
13807 }
13808
13809 static struct breakpoint_ops strace_marker_breakpoint_ops;
13810
13811 static int
13812 strace_marker_p (struct breakpoint *b)
13813 {
13814 return b->ops == &strace_marker_breakpoint_ops;
13815 }
13816
13817 /* Delete a breakpoint and clean up all traces of it in the data
13818 structures. */
13819
13820 void
13821 delete_breakpoint (struct breakpoint *bpt)
13822 {
13823 struct breakpoint *b;
13824
13825 gdb_assert (bpt != NULL);
13826
13827 /* Has this bp already been deleted? This can happen because
13828 multiple lists can hold pointers to bp's. bpstat lists are
13829 especial culprits.
13830
13831 One example of this happening is a watchpoint's scope bp. When
13832 the scope bp triggers, we notice that the watchpoint is out of
13833 scope, and delete it. We also delete its scope bp. But the
13834 scope bp is marked "auto-deleting", and is already on a bpstat.
13835 That bpstat is then checked for auto-deleting bp's, which are
13836 deleted.
13837
13838 A real solution to this problem might involve reference counts in
13839 bp's, and/or giving them pointers back to their referencing
13840 bpstat's, and teaching delete_breakpoint to only free a bp's
13841 storage when no more references were extent. A cheaper bandaid
13842 was chosen. */
13843 if (bpt->type == bp_none)
13844 return;
13845
13846 /* At least avoid this stale reference until the reference counting
13847 of breakpoints gets resolved. */
13848 if (bpt->related_breakpoint != bpt)
13849 {
13850 struct breakpoint *related;
13851 struct watchpoint *w;
13852
13853 if (bpt->type == bp_watchpoint_scope)
13854 w = (struct watchpoint *) bpt->related_breakpoint;
13855 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
13856 w = (struct watchpoint *) bpt;
13857 else
13858 w = NULL;
13859 if (w != NULL)
13860 watchpoint_del_at_next_stop (w);
13861
13862 /* Unlink bpt from the bpt->related_breakpoint ring. */
13863 for (related = bpt; related->related_breakpoint != bpt;
13864 related = related->related_breakpoint);
13865 related->related_breakpoint = bpt->related_breakpoint;
13866 bpt->related_breakpoint = bpt;
13867 }
13868
13869 /* watch_command_1 creates a watchpoint but only sets its number if
13870 update_watchpoint succeeds in creating its bp_locations. If there's
13871 a problem in that process, we'll be asked to delete the half-created
13872 watchpoint. In that case, don't announce the deletion. */
13873 if (bpt->number)
13874 observer_notify_breakpoint_deleted (bpt);
13875
13876 if (breakpoint_chain == bpt)
13877 breakpoint_chain = bpt->next;
13878
13879 ALL_BREAKPOINTS (b)
13880 if (b->next == bpt)
13881 {
13882 b->next = bpt->next;
13883 break;
13884 }
13885
13886 /* Be sure no bpstat's are pointing at the breakpoint after it's
13887 been freed. */
13888 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
13889 in all threads for now. Note that we cannot just remove bpstats
13890 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13891 commands are associated with the bpstat; if we remove it here,
13892 then the later call to bpstat_do_actions (&stop_bpstat); in
13893 event-top.c won't do anything, and temporary breakpoints with
13894 commands won't work. */
13895
13896 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13897
13898 /* Now that breakpoint is removed from breakpoint list, update the
13899 global location list. This will remove locations that used to
13900 belong to this breakpoint. Do this before freeing the breakpoint
13901 itself, since remove_breakpoint looks at location's owner. It
13902 might be better design to have location completely
13903 self-contained, but it's not the case now. */
13904 update_global_location_list (UGLL_DONT_INSERT);
13905
13906 bpt->ops->dtor (bpt);
13907 /* On the chance that someone will soon try again to delete this
13908 same bp, we mark it as deleted before freeing its storage. */
13909 bpt->type = bp_none;
13910 delete bpt;
13911 }
13912
13913 static void
13914 do_delete_breakpoint_cleanup (void *b)
13915 {
13916 delete_breakpoint ((struct breakpoint *) b);
13917 }
13918
13919 struct cleanup *
13920 make_cleanup_delete_breakpoint (struct breakpoint *b)
13921 {
13922 return make_cleanup (do_delete_breakpoint_cleanup, b);
13923 }
13924
13925 /* Iterator function to call a user-provided callback function once
13926 for each of B and its related breakpoints. */
13927
13928 static void
13929 iterate_over_related_breakpoints (struct breakpoint *b,
13930 void (*function) (struct breakpoint *,
13931 void *),
13932 void *data)
13933 {
13934 struct breakpoint *related;
13935
13936 related = b;
13937 do
13938 {
13939 struct breakpoint *next;
13940
13941 /* FUNCTION may delete RELATED. */
13942 next = related->related_breakpoint;
13943
13944 if (next == related)
13945 {
13946 /* RELATED is the last ring entry. */
13947 function (related, data);
13948
13949 /* FUNCTION may have deleted it, so we'd never reach back to
13950 B. There's nothing left to do anyway, so just break
13951 out. */
13952 break;
13953 }
13954 else
13955 function (related, data);
13956
13957 related = next;
13958 }
13959 while (related != b);
13960 }
13961
13962 static void
13963 do_delete_breakpoint (struct breakpoint *b, void *ignore)
13964 {
13965 delete_breakpoint (b);
13966 }
13967
13968 /* A callback for map_breakpoint_numbers that calls
13969 delete_breakpoint. */
13970
13971 static void
13972 do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
13973 {
13974 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
13975 }
13976
13977 void
13978 delete_command (char *arg, int from_tty)
13979 {
13980 struct breakpoint *b, *b_tmp;
13981
13982 dont_repeat ();
13983
13984 if (arg == 0)
13985 {
13986 int breaks_to_delete = 0;
13987
13988 /* Delete all breakpoints if no argument. Do not delete
13989 internal breakpoints, these have to be deleted with an
13990 explicit breakpoint number argument. */
13991 ALL_BREAKPOINTS (b)
13992 if (user_breakpoint_p (b))
13993 {
13994 breaks_to_delete = 1;
13995 break;
13996 }
13997
13998 /* Ask user only if there are some breakpoints to delete. */
13999 if (!from_tty
14000 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
14001 {
14002 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14003 if (user_breakpoint_p (b))
14004 delete_breakpoint (b);
14005 }
14006 }
14007 else
14008 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
14009 }
14010
14011 /* Return true if all locations of B bound to PSPACE are pending. If
14012 PSPACE is NULL, all locations of all program spaces are
14013 considered. */
14014
14015 static int
14016 all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
14017 {
14018 struct bp_location *loc;
14019
14020 for (loc = b->loc; loc != NULL; loc = loc->next)
14021 if ((pspace == NULL
14022 || loc->pspace == pspace)
14023 && !loc->shlib_disabled
14024 && !loc->pspace->executing_startup)
14025 return 0;
14026 return 1;
14027 }
14028
14029 /* Subroutine of update_breakpoint_locations to simplify it.
14030 Return non-zero if multiple fns in list LOC have the same name.
14031 Null names are ignored. */
14032
14033 static int
14034 ambiguous_names_p (struct bp_location *loc)
14035 {
14036 struct bp_location *l;
14037 htab_t htab = htab_create_alloc (13, htab_hash_string,
14038 (int (*) (const void *,
14039 const void *)) streq,
14040 NULL, xcalloc, xfree);
14041
14042 for (l = loc; l != NULL; l = l->next)
14043 {
14044 const char **slot;
14045 const char *name = l->function_name;
14046
14047 /* Allow for some names to be NULL, ignore them. */
14048 if (name == NULL)
14049 continue;
14050
14051 slot = (const char **) htab_find_slot (htab, (const void *) name,
14052 INSERT);
14053 /* NOTE: We can assume slot != NULL here because xcalloc never
14054 returns NULL. */
14055 if (*slot != NULL)
14056 {
14057 htab_delete (htab);
14058 return 1;
14059 }
14060 *slot = name;
14061 }
14062
14063 htab_delete (htab);
14064 return 0;
14065 }
14066
14067 /* When symbols change, it probably means the sources changed as well,
14068 and it might mean the static tracepoint markers are no longer at
14069 the same address or line numbers they used to be at last we
14070 checked. Losing your static tracepoints whenever you rebuild is
14071 undesirable. This function tries to resync/rematch gdb static
14072 tracepoints with the markers on the target, for static tracepoints
14073 that have not been set by marker id. Static tracepoint that have
14074 been set by marker id are reset by marker id in breakpoint_re_set.
14075 The heuristic is:
14076
14077 1) For a tracepoint set at a specific address, look for a marker at
14078 the old PC. If one is found there, assume to be the same marker.
14079 If the name / string id of the marker found is different from the
14080 previous known name, assume that means the user renamed the marker
14081 in the sources, and output a warning.
14082
14083 2) For a tracepoint set at a given line number, look for a marker
14084 at the new address of the old line number. If one is found there,
14085 assume to be the same marker. If the name / string id of the
14086 marker found is different from the previous known name, assume that
14087 means the user renamed the marker in the sources, and output a
14088 warning.
14089
14090 3) If a marker is no longer found at the same address or line, it
14091 may mean the marker no longer exists. But it may also just mean
14092 the code changed a bit. Maybe the user added a few lines of code
14093 that made the marker move up or down (in line number terms). Ask
14094 the target for info about the marker with the string id as we knew
14095 it. If found, update line number and address in the matching
14096 static tracepoint. This will get confused if there's more than one
14097 marker with the same ID (possible in UST, although unadvised
14098 precisely because it confuses tools). */
14099
14100 static struct symtab_and_line
14101 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
14102 {
14103 struct tracepoint *tp = (struct tracepoint *) b;
14104 struct static_tracepoint_marker marker;
14105 CORE_ADDR pc;
14106
14107 pc = sal.pc;
14108 if (sal.line)
14109 find_line_pc (sal.symtab, sal.line, &pc);
14110
14111 if (target_static_tracepoint_marker_at (pc, &marker))
14112 {
14113 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
14114 warning (_("static tracepoint %d changed probed marker from %s to %s"),
14115 b->number,
14116 tp->static_trace_marker_id, marker.str_id);
14117
14118 xfree (tp->static_trace_marker_id);
14119 tp->static_trace_marker_id = xstrdup (marker.str_id);
14120 release_static_tracepoint_marker (&marker);
14121
14122 return sal;
14123 }
14124
14125 /* Old marker wasn't found on target at lineno. Try looking it up
14126 by string ID. */
14127 if (!sal.explicit_pc
14128 && sal.line != 0
14129 && sal.symtab != NULL
14130 && tp->static_trace_marker_id != NULL)
14131 {
14132 VEC(static_tracepoint_marker_p) *markers;
14133
14134 markers
14135 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
14136
14137 if (!VEC_empty(static_tracepoint_marker_p, markers))
14138 {
14139 struct symtab_and_line sal2;
14140 struct symbol *sym;
14141 struct static_tracepoint_marker *tpmarker;
14142 struct ui_out *uiout = current_uiout;
14143 struct explicit_location explicit_loc;
14144
14145 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
14146
14147 xfree (tp->static_trace_marker_id);
14148 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
14149
14150 warning (_("marker for static tracepoint %d (%s) not "
14151 "found at previous line number"),
14152 b->number, tp->static_trace_marker_id);
14153
14154 init_sal (&sal2);
14155
14156 sal2.pc = tpmarker->address;
14157
14158 sal2 = find_pc_line (tpmarker->address, 0);
14159 sym = find_pc_sect_function (tpmarker->address, NULL);
14160 ui_out_text (uiout, "Now in ");
14161 if (sym)
14162 {
14163 ui_out_field_string (uiout, "func",
14164 SYMBOL_PRINT_NAME (sym));
14165 ui_out_text (uiout, " at ");
14166 }
14167 ui_out_field_string (uiout, "file",
14168 symtab_to_filename_for_display (sal2.symtab));
14169 ui_out_text (uiout, ":");
14170
14171 if (ui_out_is_mi_like_p (uiout))
14172 {
14173 const char *fullname = symtab_to_fullname (sal2.symtab);
14174
14175 ui_out_field_string (uiout, "fullname", fullname);
14176 }
14177
14178 ui_out_field_int (uiout, "line", sal2.line);
14179 ui_out_text (uiout, "\n");
14180
14181 b->loc->line_number = sal2.line;
14182 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
14183
14184 delete_event_location (b->location);
14185 initialize_explicit_location (&explicit_loc);
14186 explicit_loc.source_filename
14187 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
14188 explicit_loc.line_offset.offset = b->loc->line_number;
14189 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
14190 b->location = new_explicit_location (&explicit_loc);
14191
14192 /* Might be nice to check if function changed, and warn if
14193 so. */
14194
14195 release_static_tracepoint_marker (tpmarker);
14196 }
14197 }
14198 return sal;
14199 }
14200
14201 /* Returns 1 iff locations A and B are sufficiently same that
14202 we don't need to report breakpoint as changed. */
14203
14204 static int
14205 locations_are_equal (struct bp_location *a, struct bp_location *b)
14206 {
14207 while (a && b)
14208 {
14209 if (a->address != b->address)
14210 return 0;
14211
14212 if (a->shlib_disabled != b->shlib_disabled)
14213 return 0;
14214
14215 if (a->enabled != b->enabled)
14216 return 0;
14217
14218 a = a->next;
14219 b = b->next;
14220 }
14221
14222 if ((a == NULL) != (b == NULL))
14223 return 0;
14224
14225 return 1;
14226 }
14227
14228 /* Split all locations of B that are bound to PSPACE out of B's
14229 location list to a separate list and return that list's head. If
14230 PSPACE is NULL, hoist out all locations of B. */
14231
14232 static struct bp_location *
14233 hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
14234 {
14235 struct bp_location head;
14236 struct bp_location *i = b->loc;
14237 struct bp_location **i_link = &b->loc;
14238 struct bp_location *hoisted = &head;
14239
14240 if (pspace == NULL)
14241 {
14242 i = b->loc;
14243 b->loc = NULL;
14244 return i;
14245 }
14246
14247 head.next = NULL;
14248
14249 while (i != NULL)
14250 {
14251 if (i->pspace == pspace)
14252 {
14253 *i_link = i->next;
14254 i->next = NULL;
14255 hoisted->next = i;
14256 hoisted = i;
14257 }
14258 else
14259 i_link = &i->next;
14260 i = *i_link;
14261 }
14262
14263 return head.next;
14264 }
14265
14266 /* Create new breakpoint locations for B (a hardware or software
14267 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
14268 zero, then B is a ranged breakpoint. Only recreates locations for
14269 FILTER_PSPACE. Locations of other program spaces are left
14270 untouched. */
14271
14272 void
14273 update_breakpoint_locations (struct breakpoint *b,
14274 struct program_space *filter_pspace,
14275 struct symtabs_and_lines sals,
14276 struct symtabs_and_lines sals_end)
14277 {
14278 int i;
14279 struct bp_location *existing_locations;
14280
14281 if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
14282 {
14283 /* Ranged breakpoints have only one start location and one end
14284 location. */
14285 b->enable_state = bp_disabled;
14286 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
14287 "multiple locations found\n"),
14288 b->number);
14289 return;
14290 }
14291
14292 /* If there's no new locations, and all existing locations are
14293 pending, don't do anything. This optimizes the common case where
14294 all locations are in the same shared library, that was unloaded.
14295 We'd like to retain the location, so that when the library is
14296 loaded again, we don't loose the enabled/disabled status of the
14297 individual locations. */
14298 if (all_locations_are_pending (b, filter_pspace) && sals.nelts == 0)
14299 return;
14300
14301 existing_locations = hoist_existing_locations (b, filter_pspace);
14302
14303 for (i = 0; i < sals.nelts; ++i)
14304 {
14305 struct bp_location *new_loc;
14306
14307 switch_to_program_space_and_thread (sals.sals[i].pspace);
14308
14309 new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
14310
14311 /* Reparse conditions, they might contain references to the
14312 old symtab. */
14313 if (b->cond_string != NULL)
14314 {
14315 const char *s;
14316
14317 s = b->cond_string;
14318 TRY
14319 {
14320 new_loc->cond = parse_exp_1 (&s, sals.sals[i].pc,
14321 block_for_pc (sals.sals[i].pc),
14322 0);
14323 }
14324 CATCH (e, RETURN_MASK_ERROR)
14325 {
14326 warning (_("failed to reevaluate condition "
14327 "for breakpoint %d: %s"),
14328 b->number, e.message);
14329 new_loc->enabled = 0;
14330 }
14331 END_CATCH
14332 }
14333
14334 if (sals_end.nelts)
14335 {
14336 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
14337
14338 new_loc->length = end - sals.sals[0].pc + 1;
14339 }
14340 }
14341
14342 /* If possible, carry over 'disable' status from existing
14343 breakpoints. */
14344 {
14345 struct bp_location *e = existing_locations;
14346 /* If there are multiple breakpoints with the same function name,
14347 e.g. for inline functions, comparing function names won't work.
14348 Instead compare pc addresses; this is just a heuristic as things
14349 may have moved, but in practice it gives the correct answer
14350 often enough until a better solution is found. */
14351 int have_ambiguous_names = ambiguous_names_p (b->loc);
14352
14353 for (; e; e = e->next)
14354 {
14355 if (!e->enabled && e->function_name)
14356 {
14357 struct bp_location *l = b->loc;
14358 if (have_ambiguous_names)
14359 {
14360 for (; l; l = l->next)
14361 if (breakpoint_locations_match (e, l))
14362 {
14363 l->enabled = 0;
14364 break;
14365 }
14366 }
14367 else
14368 {
14369 for (; l; l = l->next)
14370 if (l->function_name
14371 && strcmp (e->function_name, l->function_name) == 0)
14372 {
14373 l->enabled = 0;
14374 break;
14375 }
14376 }
14377 }
14378 }
14379 }
14380
14381 if (!locations_are_equal (existing_locations, b->loc))
14382 observer_notify_breakpoint_modified (b);
14383 }
14384
14385 /* Find the SaL locations corresponding to the given LOCATION.
14386 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
14387
14388 static struct symtabs_and_lines
14389 location_to_sals (struct breakpoint *b, struct event_location *location,
14390 struct program_space *search_pspace, int *found)
14391 {
14392 struct symtabs_and_lines sals = {0};
14393 struct gdb_exception exception = exception_none;
14394
14395 gdb_assert (b->ops != NULL);
14396
14397 TRY
14398 {
14399 b->ops->decode_location (b, location, search_pspace, &sals);
14400 }
14401 CATCH (e, RETURN_MASK_ERROR)
14402 {
14403 int not_found_and_ok = 0;
14404
14405 exception = e;
14406
14407 /* For pending breakpoints, it's expected that parsing will
14408 fail until the right shared library is loaded. User has
14409 already told to create pending breakpoints and don't need
14410 extra messages. If breakpoint is in bp_shlib_disabled
14411 state, then user already saw the message about that
14412 breakpoint being disabled, and don't want to see more
14413 errors. */
14414 if (e.error == NOT_FOUND_ERROR
14415 && (b->condition_not_parsed
14416 || (b->loc != NULL
14417 && search_pspace != NULL
14418 && b->loc->pspace != search_pspace)
14419 || (b->loc && b->loc->shlib_disabled)
14420 || (b->loc && b->loc->pspace->executing_startup)
14421 || b->enable_state == bp_disabled))
14422 not_found_and_ok = 1;
14423
14424 if (!not_found_and_ok)
14425 {
14426 /* We surely don't want to warn about the same breakpoint
14427 10 times. One solution, implemented here, is disable
14428 the breakpoint on error. Another solution would be to
14429 have separate 'warning emitted' flag. Since this
14430 happens only when a binary has changed, I don't know
14431 which approach is better. */
14432 b->enable_state = bp_disabled;
14433 throw_exception (e);
14434 }
14435 }
14436 END_CATCH
14437
14438 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
14439 {
14440 int i;
14441
14442 for (i = 0; i < sals.nelts; ++i)
14443 resolve_sal_pc (&sals.sals[i]);
14444 if (b->condition_not_parsed && b->extra_string != NULL)
14445 {
14446 char *cond_string, *extra_string;
14447 int thread, task;
14448
14449 find_condition_and_thread (b->extra_string, sals.sals[0].pc,
14450 &cond_string, &thread, &task,
14451 &extra_string);
14452 gdb_assert (b->cond_string == NULL);
14453 if (cond_string)
14454 b->cond_string = cond_string;
14455 b->thread = thread;
14456 b->task = task;
14457 if (extra_string)
14458 {
14459 xfree (b->extra_string);
14460 b->extra_string = extra_string;
14461 }
14462 b->condition_not_parsed = 0;
14463 }
14464
14465 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
14466 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
14467
14468 *found = 1;
14469 }
14470 else
14471 *found = 0;
14472
14473 return sals;
14474 }
14475
14476 /* The default re_set method, for typical hardware or software
14477 breakpoints. Reevaluate the breakpoint and recreate its
14478 locations. */
14479
14480 static void
14481 breakpoint_re_set_default (struct breakpoint *b)
14482 {
14483 int found;
14484 struct symtabs_and_lines sals, sals_end;
14485 struct symtabs_and_lines expanded = {0};
14486 struct symtabs_and_lines expanded_end = {0};
14487 struct program_space *filter_pspace = current_program_space;
14488
14489 sals = location_to_sals (b, b->location, filter_pspace, &found);
14490 if (found)
14491 {
14492 make_cleanup (xfree, sals.sals);
14493 expanded = sals;
14494 }
14495
14496 if (b->location_range_end != NULL)
14497 {
14498 sals_end = location_to_sals (b, b->location_range_end,
14499 filter_pspace, &found);
14500 if (found)
14501 {
14502 make_cleanup (xfree, sals_end.sals);
14503 expanded_end = sals_end;
14504 }
14505 }
14506
14507 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
14508 }
14509
14510 /* Default method for creating SALs from an address string. It basically
14511 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
14512
14513 static void
14514 create_sals_from_location_default (const struct event_location *location,
14515 struct linespec_result *canonical,
14516 enum bptype type_wanted)
14517 {
14518 parse_breakpoint_sals (location, canonical);
14519 }
14520
14521 /* Call create_breakpoints_sal for the given arguments. This is the default
14522 function for the `create_breakpoints_sal' method of
14523 breakpoint_ops. */
14524
14525 static void
14526 create_breakpoints_sal_default (struct gdbarch *gdbarch,
14527 struct linespec_result *canonical,
14528 char *cond_string,
14529 char *extra_string,
14530 enum bptype type_wanted,
14531 enum bpdisp disposition,
14532 int thread,
14533 int task, int ignore_count,
14534 const struct breakpoint_ops *ops,
14535 int from_tty, int enabled,
14536 int internal, unsigned flags)
14537 {
14538 create_breakpoints_sal (gdbarch, canonical, cond_string,
14539 extra_string,
14540 type_wanted, disposition,
14541 thread, task, ignore_count, ops, from_tty,
14542 enabled, internal, flags);
14543 }
14544
14545 /* Decode the line represented by S by calling decode_line_full. This is the
14546 default function for the `decode_location' method of breakpoint_ops. */
14547
14548 static void
14549 decode_location_default (struct breakpoint *b,
14550 const struct event_location *location,
14551 struct program_space *search_pspace,
14552 struct symtabs_and_lines *sals)
14553 {
14554 struct linespec_result canonical;
14555
14556 init_linespec_result (&canonical);
14557 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
14558 (struct symtab *) NULL, 0,
14559 &canonical, multiple_symbols_all,
14560 b->filter);
14561
14562 /* We should get 0 or 1 resulting SALs. */
14563 gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
14564
14565 if (VEC_length (linespec_sals, canonical.sals) > 0)
14566 {
14567 struct linespec_sals *lsal;
14568
14569 lsal = VEC_index (linespec_sals, canonical.sals, 0);
14570 *sals = lsal->sals;
14571 /* Arrange it so the destructor does not free the
14572 contents. */
14573 lsal->sals.sals = NULL;
14574 }
14575
14576 destroy_linespec_result (&canonical);
14577 }
14578
14579 /* Prepare the global context for a re-set of breakpoint B. */
14580
14581 static struct cleanup *
14582 prepare_re_set_context (struct breakpoint *b)
14583 {
14584 input_radix = b->input_radix;
14585 set_language (b->language);
14586
14587 return make_cleanup (null_cleanup, NULL);
14588 }
14589
14590 /* Reset a breakpoint given it's struct breakpoint * BINT.
14591 The value we return ends up being the return value from catch_errors.
14592 Unused in this case. */
14593
14594 static int
14595 breakpoint_re_set_one (void *bint)
14596 {
14597 /* Get past catch_errs. */
14598 struct breakpoint *b = (struct breakpoint *) bint;
14599 struct cleanup *cleanups;
14600
14601 cleanups = prepare_re_set_context (b);
14602 b->ops->re_set (b);
14603 do_cleanups (cleanups);
14604 return 0;
14605 }
14606
14607 /* Re-set breakpoint locations for the current program space.
14608 Locations bound to other program spaces are left untouched. */
14609
14610 void
14611 breakpoint_re_set (void)
14612 {
14613 struct breakpoint *b, *b_tmp;
14614 enum language save_language;
14615 int save_input_radix;
14616 struct cleanup *old_chain;
14617
14618 save_language = current_language->la_language;
14619 save_input_radix = input_radix;
14620 old_chain = save_current_space_and_thread ();
14621
14622 /* Note: we must not try to insert locations until after all
14623 breakpoints have been re-set. Otherwise, e.g., when re-setting
14624 breakpoint 1, we'd insert the locations of breakpoint 2, which
14625 hadn't been re-set yet, and thus may have stale locations. */
14626
14627 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14628 {
14629 /* Format possible error msg. */
14630 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
14631 b->number);
14632 struct cleanup *cleanups = make_cleanup (xfree, message);
14633 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
14634 do_cleanups (cleanups);
14635 }
14636 set_language (save_language);
14637 input_radix = save_input_radix;
14638
14639 jit_breakpoint_re_set ();
14640
14641 do_cleanups (old_chain);
14642
14643 create_overlay_event_breakpoint ();
14644 create_longjmp_master_breakpoint ();
14645 create_std_terminate_master_breakpoint ();
14646 create_exception_master_breakpoint ();
14647
14648 /* Now we can insert. */
14649 update_global_location_list (UGLL_MAY_INSERT);
14650 }
14651 \f
14652 /* Reset the thread number of this breakpoint:
14653
14654 - If the breakpoint is for all threads, leave it as-is.
14655 - Else, reset it to the current thread for inferior_ptid. */
14656 void
14657 breakpoint_re_set_thread (struct breakpoint *b)
14658 {
14659 if (b->thread != -1)
14660 {
14661 if (in_thread_list (inferior_ptid))
14662 b->thread = ptid_to_global_thread_id (inferior_ptid);
14663
14664 /* We're being called after following a fork. The new fork is
14665 selected as current, and unless this was a vfork will have a
14666 different program space from the original thread. Reset that
14667 as well. */
14668 b->loc->pspace = current_program_space;
14669 }
14670 }
14671
14672 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14673 If from_tty is nonzero, it prints a message to that effect,
14674 which ends with a period (no newline). */
14675
14676 void
14677 set_ignore_count (int bptnum, int count, int from_tty)
14678 {
14679 struct breakpoint *b;
14680
14681 if (count < 0)
14682 count = 0;
14683
14684 ALL_BREAKPOINTS (b)
14685 if (b->number == bptnum)
14686 {
14687 if (is_tracepoint (b))
14688 {
14689 if (from_tty && count != 0)
14690 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14691 bptnum);
14692 return;
14693 }
14694
14695 b->ignore_count = count;
14696 if (from_tty)
14697 {
14698 if (count == 0)
14699 printf_filtered (_("Will stop next time "
14700 "breakpoint %d is reached."),
14701 bptnum);
14702 else if (count == 1)
14703 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
14704 bptnum);
14705 else
14706 printf_filtered (_("Will ignore next %d "
14707 "crossings of breakpoint %d."),
14708 count, bptnum);
14709 }
14710 observer_notify_breakpoint_modified (b);
14711 return;
14712 }
14713
14714 error (_("No breakpoint number %d."), bptnum);
14715 }
14716
14717 /* Command to set ignore-count of breakpoint N to COUNT. */
14718
14719 static void
14720 ignore_command (char *args, int from_tty)
14721 {
14722 char *p = args;
14723 int num;
14724
14725 if (p == 0)
14726 error_no_arg (_("a breakpoint number"));
14727
14728 num = get_number (&p);
14729 if (num == 0)
14730 error (_("bad breakpoint number: '%s'"), args);
14731 if (*p == 0)
14732 error (_("Second argument (specified ignore-count) is missing."));
14733
14734 set_ignore_count (num,
14735 longest_to_int (value_as_long (parse_and_eval (p))),
14736 from_tty);
14737 if (from_tty)
14738 printf_filtered ("\n");
14739 }
14740 \f
14741 /* Call FUNCTION on each of the breakpoints
14742 whose numbers are given in ARGS. */
14743
14744 static void
14745 map_breakpoint_numbers (const char *args,
14746 void (*function) (struct breakpoint *,
14747 void *),
14748 void *data)
14749 {
14750 int num;
14751 struct breakpoint *b, *tmp;
14752
14753 if (args == 0 || *args == '\0')
14754 error_no_arg (_("one or more breakpoint numbers"));
14755
14756 number_or_range_parser parser (args);
14757
14758 while (!parser.finished ())
14759 {
14760 const char *p = parser.cur_tok ();
14761 bool match = false;
14762
14763 num = parser.get_number ();
14764 if (num == 0)
14765 {
14766 warning (_("bad breakpoint number at or near '%s'"), p);
14767 }
14768 else
14769 {
14770 ALL_BREAKPOINTS_SAFE (b, tmp)
14771 if (b->number == num)
14772 {
14773 match = true;
14774 function (b, data);
14775 break;
14776 }
14777 if (!match)
14778 printf_unfiltered (_("No breakpoint number %d.\n"), num);
14779 }
14780 }
14781 }
14782
14783 static struct bp_location *
14784 find_location_by_number (char *number)
14785 {
14786 char *dot = strchr (number, '.');
14787 char *p1;
14788 int bp_num;
14789 int loc_num;
14790 struct breakpoint *b;
14791 struct bp_location *loc;
14792
14793 *dot = '\0';
14794
14795 p1 = number;
14796 bp_num = get_number (&p1);
14797 if (bp_num == 0)
14798 error (_("Bad breakpoint number '%s'"), number);
14799
14800 ALL_BREAKPOINTS (b)
14801 if (b->number == bp_num)
14802 {
14803 break;
14804 }
14805
14806 if (!b || b->number != bp_num)
14807 error (_("Bad breakpoint number '%s'"), number);
14808
14809 p1 = dot+1;
14810 loc_num = get_number (&p1);
14811 if (loc_num == 0)
14812 error (_("Bad breakpoint location number '%s'"), number);
14813
14814 --loc_num;
14815 loc = b->loc;
14816 for (;loc_num && loc; --loc_num, loc = loc->next)
14817 ;
14818 if (!loc)
14819 error (_("Bad breakpoint location number '%s'"), dot+1);
14820
14821 return loc;
14822 }
14823
14824
14825 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14826 If from_tty is nonzero, it prints a message to that effect,
14827 which ends with a period (no newline). */
14828
14829 void
14830 disable_breakpoint (struct breakpoint *bpt)
14831 {
14832 /* Never disable a watchpoint scope breakpoint; we want to
14833 hit them when we leave scope so we can delete both the
14834 watchpoint and its scope breakpoint at that time. */
14835 if (bpt->type == bp_watchpoint_scope)
14836 return;
14837
14838 bpt->enable_state = bp_disabled;
14839
14840 /* Mark breakpoint locations modified. */
14841 mark_breakpoint_modified (bpt);
14842
14843 if (target_supports_enable_disable_tracepoint ()
14844 && current_trace_status ()->running && is_tracepoint (bpt))
14845 {
14846 struct bp_location *location;
14847
14848 for (location = bpt->loc; location; location = location->next)
14849 target_disable_tracepoint (location);
14850 }
14851
14852 update_global_location_list (UGLL_DONT_INSERT);
14853
14854 observer_notify_breakpoint_modified (bpt);
14855 }
14856
14857 /* A callback for iterate_over_related_breakpoints. */
14858
14859 static void
14860 do_disable_breakpoint (struct breakpoint *b, void *ignore)
14861 {
14862 disable_breakpoint (b);
14863 }
14864
14865 /* A callback for map_breakpoint_numbers that calls
14866 disable_breakpoint. */
14867
14868 static void
14869 do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
14870 {
14871 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
14872 }
14873
14874 static void
14875 disable_command (char *args, int from_tty)
14876 {
14877 if (args == 0)
14878 {
14879 struct breakpoint *bpt;
14880
14881 ALL_BREAKPOINTS (bpt)
14882 if (user_breakpoint_p (bpt))
14883 disable_breakpoint (bpt);
14884 }
14885 else
14886 {
14887 char *num = extract_arg (&args);
14888
14889 while (num)
14890 {
14891 if (strchr (num, '.'))
14892 {
14893 struct bp_location *loc = find_location_by_number (num);
14894
14895 if (loc)
14896 {
14897 if (loc->enabled)
14898 {
14899 loc->enabled = 0;
14900 mark_breakpoint_location_modified (loc);
14901 }
14902 if (target_supports_enable_disable_tracepoint ()
14903 && current_trace_status ()->running && loc->owner
14904 && is_tracepoint (loc->owner))
14905 target_disable_tracepoint (loc);
14906 }
14907 update_global_location_list (UGLL_DONT_INSERT);
14908 }
14909 else
14910 map_breakpoint_numbers (num, do_map_disable_breakpoint, NULL);
14911 num = extract_arg (&args);
14912 }
14913 }
14914 }
14915
14916 static void
14917 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14918 int count)
14919 {
14920 int target_resources_ok;
14921
14922 if (bpt->type == bp_hardware_breakpoint)
14923 {
14924 int i;
14925 i = hw_breakpoint_used_count ();
14926 target_resources_ok =
14927 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
14928 i + 1, 0);
14929 if (target_resources_ok == 0)
14930 error (_("No hardware breakpoint support in the target."));
14931 else if (target_resources_ok < 0)
14932 error (_("Hardware breakpoints used exceeds limit."));
14933 }
14934
14935 if (is_watchpoint (bpt))
14936 {
14937 /* Initialize it just to avoid a GCC false warning. */
14938 enum enable_state orig_enable_state = bp_disabled;
14939
14940 TRY
14941 {
14942 struct watchpoint *w = (struct watchpoint *) bpt;
14943
14944 orig_enable_state = bpt->enable_state;
14945 bpt->enable_state = bp_enabled;
14946 update_watchpoint (w, 1 /* reparse */);
14947 }
14948 CATCH (e, RETURN_MASK_ALL)
14949 {
14950 bpt->enable_state = orig_enable_state;
14951 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14952 bpt->number);
14953 return;
14954 }
14955 END_CATCH
14956 }
14957
14958 bpt->enable_state = bp_enabled;
14959
14960 /* Mark breakpoint locations modified. */
14961 mark_breakpoint_modified (bpt);
14962
14963 if (target_supports_enable_disable_tracepoint ()
14964 && current_trace_status ()->running && is_tracepoint (bpt))
14965 {
14966 struct bp_location *location;
14967
14968 for (location = bpt->loc; location; location = location->next)
14969 target_enable_tracepoint (location);
14970 }
14971
14972 bpt->disposition = disposition;
14973 bpt->enable_count = count;
14974 update_global_location_list (UGLL_MAY_INSERT);
14975
14976 observer_notify_breakpoint_modified (bpt);
14977 }
14978
14979
14980 void
14981 enable_breakpoint (struct breakpoint *bpt)
14982 {
14983 enable_breakpoint_disp (bpt, bpt->disposition, 0);
14984 }
14985
14986 static void
14987 do_enable_breakpoint (struct breakpoint *bpt, void *arg)
14988 {
14989 enable_breakpoint (bpt);
14990 }
14991
14992 /* A callback for map_breakpoint_numbers that calls
14993 enable_breakpoint. */
14994
14995 static void
14996 do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
14997 {
14998 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
14999 }
15000
15001 /* The enable command enables the specified breakpoints (or all defined
15002 breakpoints) so they once again become (or continue to be) effective
15003 in stopping the inferior. */
15004
15005 static void
15006 enable_command (char *args, int from_tty)
15007 {
15008 if (args == 0)
15009 {
15010 struct breakpoint *bpt;
15011
15012 ALL_BREAKPOINTS (bpt)
15013 if (user_breakpoint_p (bpt))
15014 enable_breakpoint (bpt);
15015 }
15016 else
15017 {
15018 char *num = extract_arg (&args);
15019
15020 while (num)
15021 {
15022 if (strchr (num, '.'))
15023 {
15024 struct bp_location *loc = find_location_by_number (num);
15025
15026 if (loc)
15027 {
15028 if (!loc->enabled)
15029 {
15030 loc->enabled = 1;
15031 mark_breakpoint_location_modified (loc);
15032 }
15033 if (target_supports_enable_disable_tracepoint ()
15034 && current_trace_status ()->running && loc->owner
15035 && is_tracepoint (loc->owner))
15036 target_enable_tracepoint (loc);
15037 }
15038 update_global_location_list (UGLL_MAY_INSERT);
15039 }
15040 else
15041 map_breakpoint_numbers (num, do_map_enable_breakpoint, NULL);
15042 num = extract_arg (&args);
15043 }
15044 }
15045 }
15046
15047 /* This struct packages up disposition data for application to multiple
15048 breakpoints. */
15049
15050 struct disp_data
15051 {
15052 enum bpdisp disp;
15053 int count;
15054 };
15055
15056 static void
15057 do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
15058 {
15059 struct disp_data disp_data = *(struct disp_data *) arg;
15060
15061 enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
15062 }
15063
15064 static void
15065 do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
15066 {
15067 struct disp_data disp = { disp_disable, 1 };
15068
15069 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
15070 }
15071
15072 static void
15073 enable_once_command (char *args, int from_tty)
15074 {
15075 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
15076 }
15077
15078 static void
15079 do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
15080 {
15081 struct disp_data disp = { disp_disable, *(int *) countptr };
15082
15083 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
15084 }
15085
15086 static void
15087 enable_count_command (char *args, int from_tty)
15088 {
15089 int count;
15090
15091 if (args == NULL)
15092 error_no_arg (_("hit count"));
15093
15094 count = get_number (&args);
15095
15096 map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
15097 }
15098
15099 static void
15100 do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
15101 {
15102 struct disp_data disp = { disp_del, 1 };
15103
15104 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
15105 }
15106
15107 static void
15108 enable_delete_command (char *args, int from_tty)
15109 {
15110 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
15111 }
15112 \f
15113 static void
15114 set_breakpoint_cmd (char *args, int from_tty)
15115 {
15116 }
15117
15118 static void
15119 show_breakpoint_cmd (char *args, int from_tty)
15120 {
15121 }
15122
15123 /* Invalidate last known value of any hardware watchpoint if
15124 the memory which that value represents has been written to by
15125 GDB itself. */
15126
15127 static void
15128 invalidate_bp_value_on_memory_change (struct inferior *inferior,
15129 CORE_ADDR addr, ssize_t len,
15130 const bfd_byte *data)
15131 {
15132 struct breakpoint *bp;
15133
15134 ALL_BREAKPOINTS (bp)
15135 if (bp->enable_state == bp_enabled
15136 && bp->type == bp_hardware_watchpoint)
15137 {
15138 struct watchpoint *wp = (struct watchpoint *) bp;
15139
15140 if (wp->val_valid && wp->val)
15141 {
15142 struct bp_location *loc;
15143
15144 for (loc = bp->loc; loc != NULL; loc = loc->next)
15145 if (loc->loc_type == bp_loc_hardware_watchpoint
15146 && loc->address + loc->length > addr
15147 && addr + len > loc->address)
15148 {
15149 value_free (wp->val);
15150 wp->val = NULL;
15151 wp->val_valid = 0;
15152 }
15153 }
15154 }
15155 }
15156
15157 /* Create and insert a breakpoint for software single step. */
15158
15159 void
15160 insert_single_step_breakpoint (struct gdbarch *gdbarch,
15161 struct address_space *aspace,
15162 CORE_ADDR next_pc)
15163 {
15164 struct thread_info *tp = inferior_thread ();
15165 struct symtab_and_line sal;
15166 CORE_ADDR pc = next_pc;
15167
15168 if (tp->control.single_step_breakpoints == NULL)
15169 {
15170 tp->control.single_step_breakpoints
15171 = new_single_step_breakpoint (tp->global_num, gdbarch);
15172 }
15173
15174 sal = find_pc_line (pc, 0);
15175 sal.pc = pc;
15176 sal.section = find_pc_overlay (pc);
15177 sal.explicit_pc = 1;
15178 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
15179
15180 update_global_location_list (UGLL_INSERT);
15181 }
15182
15183 /* Insert single step breakpoints according to the current state. */
15184
15185 int
15186 insert_single_step_breakpoints (struct gdbarch *gdbarch)
15187 {
15188 struct regcache *regcache = get_current_regcache ();
15189 VEC (CORE_ADDR) * next_pcs;
15190
15191 next_pcs = gdbarch_software_single_step (gdbarch, regcache);
15192
15193 if (next_pcs != NULL)
15194 {
15195 int i;
15196 CORE_ADDR pc;
15197 struct frame_info *frame = get_current_frame ();
15198 struct address_space *aspace = get_frame_address_space (frame);
15199
15200 for (i = 0; VEC_iterate (CORE_ADDR, next_pcs, i, pc); i++)
15201 insert_single_step_breakpoint (gdbarch, aspace, pc);
15202
15203 VEC_free (CORE_ADDR, next_pcs);
15204
15205 return 1;
15206 }
15207 else
15208 return 0;
15209 }
15210
15211 /* See breakpoint.h. */
15212
15213 int
15214 breakpoint_has_location_inserted_here (struct breakpoint *bp,
15215 struct address_space *aspace,
15216 CORE_ADDR pc)
15217 {
15218 struct bp_location *loc;
15219
15220 for (loc = bp->loc; loc != NULL; loc = loc->next)
15221 if (loc->inserted
15222 && breakpoint_location_address_match (loc, aspace, pc))
15223 return 1;
15224
15225 return 0;
15226 }
15227
15228 /* Check whether a software single-step breakpoint is inserted at
15229 PC. */
15230
15231 int
15232 single_step_breakpoint_inserted_here_p (struct address_space *aspace,
15233 CORE_ADDR pc)
15234 {
15235 struct breakpoint *bpt;
15236
15237 ALL_BREAKPOINTS (bpt)
15238 {
15239 if (bpt->type == bp_single_step
15240 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
15241 return 1;
15242 }
15243 return 0;
15244 }
15245
15246 /* Tracepoint-specific operations. */
15247
15248 /* Set tracepoint count to NUM. */
15249 static void
15250 set_tracepoint_count (int num)
15251 {
15252 tracepoint_count = num;
15253 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
15254 }
15255
15256 static void
15257 trace_command (char *arg, int from_tty)
15258 {
15259 struct breakpoint_ops *ops;
15260 struct event_location *location;
15261 struct cleanup *back_to;
15262
15263 location = string_to_event_location (&arg, current_language);
15264 back_to = make_cleanup_delete_event_location (location);
15265 if (location != NULL
15266 && event_location_type (location) == PROBE_LOCATION)
15267 ops = &tracepoint_probe_breakpoint_ops;
15268 else
15269 ops = &tracepoint_breakpoint_ops;
15270
15271 create_breakpoint (get_current_arch (),
15272 location,
15273 NULL, 0, arg, 1 /* parse arg */,
15274 0 /* tempflag */,
15275 bp_tracepoint /* type_wanted */,
15276 0 /* Ignore count */,
15277 pending_break_support,
15278 ops,
15279 from_tty,
15280 1 /* enabled */,
15281 0 /* internal */, 0);
15282 do_cleanups (back_to);
15283 }
15284
15285 static void
15286 ftrace_command (char *arg, int from_tty)
15287 {
15288 struct event_location *location;
15289 struct cleanup *back_to;
15290
15291 location = string_to_event_location (&arg, current_language);
15292 back_to = make_cleanup_delete_event_location (location);
15293 create_breakpoint (get_current_arch (),
15294 location,
15295 NULL, 0, arg, 1 /* parse arg */,
15296 0 /* tempflag */,
15297 bp_fast_tracepoint /* type_wanted */,
15298 0 /* Ignore count */,
15299 pending_break_support,
15300 &tracepoint_breakpoint_ops,
15301 from_tty,
15302 1 /* enabled */,
15303 0 /* internal */, 0);
15304 do_cleanups (back_to);
15305 }
15306
15307 /* strace command implementation. Creates a static tracepoint. */
15308
15309 static void
15310 strace_command (char *arg, int from_tty)
15311 {
15312 struct breakpoint_ops *ops;
15313 struct event_location *location;
15314 struct cleanup *back_to;
15315
15316 /* Decide if we are dealing with a static tracepoint marker (`-m'),
15317 or with a normal static tracepoint. */
15318 if (arg && startswith (arg, "-m") && isspace (arg[2]))
15319 {
15320 ops = &strace_marker_breakpoint_ops;
15321 location = new_linespec_location (&arg);
15322 }
15323 else
15324 {
15325 ops = &tracepoint_breakpoint_ops;
15326 location = string_to_event_location (&arg, current_language);
15327 }
15328
15329 back_to = make_cleanup_delete_event_location (location);
15330 create_breakpoint (get_current_arch (),
15331 location,
15332 NULL, 0, arg, 1 /* parse arg */,
15333 0 /* tempflag */,
15334 bp_static_tracepoint /* type_wanted */,
15335 0 /* Ignore count */,
15336 pending_break_support,
15337 ops,
15338 from_tty,
15339 1 /* enabled */,
15340 0 /* internal */, 0);
15341 do_cleanups (back_to);
15342 }
15343
15344 /* Set up a fake reader function that gets command lines from a linked
15345 list that was acquired during tracepoint uploading. */
15346
15347 static struct uploaded_tp *this_utp;
15348 static int next_cmd;
15349
15350 static char *
15351 read_uploaded_action (void)
15352 {
15353 char *rslt;
15354
15355 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
15356
15357 next_cmd++;
15358
15359 return rslt;
15360 }
15361
15362 /* Given information about a tracepoint as recorded on a target (which
15363 can be either a live system or a trace file), attempt to create an
15364 equivalent GDB tracepoint. This is not a reliable process, since
15365 the target does not necessarily have all the information used when
15366 the tracepoint was originally defined. */
15367
15368 struct tracepoint *
15369 create_tracepoint_from_upload (struct uploaded_tp *utp)
15370 {
15371 char *addr_str, small_buf[100];
15372 struct tracepoint *tp;
15373 struct event_location *location;
15374 struct cleanup *cleanup;
15375
15376 if (utp->at_string)
15377 addr_str = utp->at_string;
15378 else
15379 {
15380 /* In the absence of a source location, fall back to raw
15381 address. Since there is no way to confirm that the address
15382 means the same thing as when the trace was started, warn the
15383 user. */
15384 warning (_("Uploaded tracepoint %d has no "
15385 "source location, using raw address"),
15386 utp->number);
15387 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
15388 addr_str = small_buf;
15389 }
15390
15391 /* There's not much we can do with a sequence of bytecodes. */
15392 if (utp->cond && !utp->cond_string)
15393 warning (_("Uploaded tracepoint %d condition "
15394 "has no source form, ignoring it"),
15395 utp->number);
15396
15397 location = string_to_event_location (&addr_str, current_language);
15398 cleanup = make_cleanup_delete_event_location (location);
15399 if (!create_breakpoint (get_current_arch (),
15400 location,
15401 utp->cond_string, -1, addr_str,
15402 0 /* parse cond/thread */,
15403 0 /* tempflag */,
15404 utp->type /* type_wanted */,
15405 0 /* Ignore count */,
15406 pending_break_support,
15407 &tracepoint_breakpoint_ops,
15408 0 /* from_tty */,
15409 utp->enabled /* enabled */,
15410 0 /* internal */,
15411 CREATE_BREAKPOINT_FLAGS_INSERTED))
15412 {
15413 do_cleanups (cleanup);
15414 return NULL;
15415 }
15416
15417 do_cleanups (cleanup);
15418
15419 /* Get the tracepoint we just created. */
15420 tp = get_tracepoint (tracepoint_count);
15421 gdb_assert (tp != NULL);
15422
15423 if (utp->pass > 0)
15424 {
15425 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
15426 tp->base.number);
15427
15428 trace_pass_command (small_buf, 0);
15429 }
15430
15431 /* If we have uploaded versions of the original commands, set up a
15432 special-purpose "reader" function and call the usual command line
15433 reader, then pass the result to the breakpoint command-setting
15434 function. */
15435 if (!VEC_empty (char_ptr, utp->cmd_strings))
15436 {
15437 struct command_line *cmd_list;
15438
15439 this_utp = utp;
15440 next_cmd = 0;
15441
15442 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
15443
15444 breakpoint_set_commands (&tp->base, cmd_list);
15445 }
15446 else if (!VEC_empty (char_ptr, utp->actions)
15447 || !VEC_empty (char_ptr, utp->step_actions))
15448 warning (_("Uploaded tracepoint %d actions "
15449 "have no source form, ignoring them"),
15450 utp->number);
15451
15452 /* Copy any status information that might be available. */
15453 tp->base.hit_count = utp->hit_count;
15454 tp->traceframe_usage = utp->traceframe_usage;
15455
15456 return tp;
15457 }
15458
15459 /* Print information on tracepoint number TPNUM_EXP, or all if
15460 omitted. */
15461
15462 static void
15463 tracepoints_info (char *args, int from_tty)
15464 {
15465 struct ui_out *uiout = current_uiout;
15466 int num_printed;
15467
15468 num_printed = breakpoint_1 (args, 0, is_tracepoint);
15469
15470 if (num_printed == 0)
15471 {
15472 if (args == NULL || *args == '\0')
15473 ui_out_message (uiout, "No tracepoints.\n");
15474 else
15475 ui_out_message (uiout, "No tracepoint matching '%s'.\n", args);
15476 }
15477
15478 default_collect_info ();
15479 }
15480
15481 /* The 'enable trace' command enables tracepoints.
15482 Not supported by all targets. */
15483 static void
15484 enable_trace_command (char *args, int from_tty)
15485 {
15486 enable_command (args, from_tty);
15487 }
15488
15489 /* The 'disable trace' command disables tracepoints.
15490 Not supported by all targets. */
15491 static void
15492 disable_trace_command (char *args, int from_tty)
15493 {
15494 disable_command (args, from_tty);
15495 }
15496
15497 /* Remove a tracepoint (or all if no argument). */
15498 static void
15499 delete_trace_command (char *arg, int from_tty)
15500 {
15501 struct breakpoint *b, *b_tmp;
15502
15503 dont_repeat ();
15504
15505 if (arg == 0)
15506 {
15507 int breaks_to_delete = 0;
15508
15509 /* Delete all breakpoints if no argument.
15510 Do not delete internal or call-dummy breakpoints, these
15511 have to be deleted with an explicit breakpoint number
15512 argument. */
15513 ALL_TRACEPOINTS (b)
15514 if (is_tracepoint (b) && user_breakpoint_p (b))
15515 {
15516 breaks_to_delete = 1;
15517 break;
15518 }
15519
15520 /* Ask user only if there are some breakpoints to delete. */
15521 if (!from_tty
15522 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
15523 {
15524 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15525 if (is_tracepoint (b) && user_breakpoint_p (b))
15526 delete_breakpoint (b);
15527 }
15528 }
15529 else
15530 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
15531 }
15532
15533 /* Helper function for trace_pass_command. */
15534
15535 static void
15536 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
15537 {
15538 tp->pass_count = count;
15539 observer_notify_breakpoint_modified (&tp->base);
15540 if (from_tty)
15541 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
15542 tp->base.number, count);
15543 }
15544
15545 /* Set passcount for tracepoint.
15546
15547 First command argument is passcount, second is tracepoint number.
15548 If tracepoint number omitted, apply to most recently defined.
15549 Also accepts special argument "all". */
15550
15551 static void
15552 trace_pass_command (char *args, int from_tty)
15553 {
15554 struct tracepoint *t1;
15555 unsigned int count;
15556
15557 if (args == 0 || *args == 0)
15558 error (_("passcount command requires an "
15559 "argument (count + optional TP num)"));
15560
15561 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
15562
15563 args = skip_spaces (args);
15564 if (*args && strncasecmp (args, "all", 3) == 0)
15565 {
15566 struct breakpoint *b;
15567
15568 args += 3; /* Skip special argument "all". */
15569 if (*args)
15570 error (_("Junk at end of arguments."));
15571
15572 ALL_TRACEPOINTS (b)
15573 {
15574 t1 = (struct tracepoint *) b;
15575 trace_pass_set_count (t1, count, from_tty);
15576 }
15577 }
15578 else if (*args == '\0')
15579 {
15580 t1 = get_tracepoint_by_number (&args, NULL);
15581 if (t1)
15582 trace_pass_set_count (t1, count, from_tty);
15583 }
15584 else
15585 {
15586 number_or_range_parser parser (args);
15587 while (!parser.finished ())
15588 {
15589 t1 = get_tracepoint_by_number (&args, &parser);
15590 if (t1)
15591 trace_pass_set_count (t1, count, from_tty);
15592 }
15593 }
15594 }
15595
15596 struct tracepoint *
15597 get_tracepoint (int num)
15598 {
15599 struct breakpoint *t;
15600
15601 ALL_TRACEPOINTS (t)
15602 if (t->number == num)
15603 return (struct tracepoint *) t;
15604
15605 return NULL;
15606 }
15607
15608 /* Find the tracepoint with the given target-side number (which may be
15609 different from the tracepoint number after disconnecting and
15610 reconnecting). */
15611
15612 struct tracepoint *
15613 get_tracepoint_by_number_on_target (int num)
15614 {
15615 struct breakpoint *b;
15616
15617 ALL_TRACEPOINTS (b)
15618 {
15619 struct tracepoint *t = (struct tracepoint *) b;
15620
15621 if (t->number_on_target == num)
15622 return t;
15623 }
15624
15625 return NULL;
15626 }
15627
15628 /* Utility: parse a tracepoint number and look it up in the list.
15629 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
15630 If the argument is missing, the most recent tracepoint
15631 (tracepoint_count) is returned. */
15632
15633 struct tracepoint *
15634 get_tracepoint_by_number (char **arg,
15635 number_or_range_parser *parser)
15636 {
15637 struct breakpoint *t;
15638 int tpnum;
15639 char *instring = arg == NULL ? NULL : *arg;
15640
15641 if (parser != NULL)
15642 {
15643 gdb_assert (!parser->finished ());
15644 tpnum = parser->get_number ();
15645 }
15646 else if (arg == NULL || *arg == NULL || ! **arg)
15647 tpnum = tracepoint_count;
15648 else
15649 tpnum = get_number (arg);
15650
15651 if (tpnum <= 0)
15652 {
15653 if (instring && *instring)
15654 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15655 instring);
15656 else
15657 printf_filtered (_("No previous tracepoint\n"));
15658 return NULL;
15659 }
15660
15661 ALL_TRACEPOINTS (t)
15662 if (t->number == tpnum)
15663 {
15664 return (struct tracepoint *) t;
15665 }
15666
15667 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15668 return NULL;
15669 }
15670
15671 void
15672 print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15673 {
15674 if (b->thread != -1)
15675 fprintf_unfiltered (fp, " thread %d", b->thread);
15676
15677 if (b->task != 0)
15678 fprintf_unfiltered (fp, " task %d", b->task);
15679
15680 fprintf_unfiltered (fp, "\n");
15681 }
15682
15683 /* Save information on user settable breakpoints (watchpoints, etc) to
15684 a new script file named FILENAME. If FILTER is non-NULL, call it
15685 on each breakpoint and only include the ones for which it returns
15686 non-zero. */
15687
15688 static void
15689 save_breakpoints (char *filename, int from_tty,
15690 int (*filter) (const struct breakpoint *))
15691 {
15692 struct breakpoint *tp;
15693 int any = 0;
15694 struct cleanup *cleanup;
15695 struct ui_file *fp;
15696 int extra_trace_bits = 0;
15697
15698 if (filename == 0 || *filename == 0)
15699 error (_("Argument required (file name in which to save)"));
15700
15701 /* See if we have anything to save. */
15702 ALL_BREAKPOINTS (tp)
15703 {
15704 /* Skip internal and momentary breakpoints. */
15705 if (!user_breakpoint_p (tp))
15706 continue;
15707
15708 /* If we have a filter, only save the breakpoints it accepts. */
15709 if (filter && !filter (tp))
15710 continue;
15711
15712 any = 1;
15713
15714 if (is_tracepoint (tp))
15715 {
15716 extra_trace_bits = 1;
15717
15718 /* We can stop searching. */
15719 break;
15720 }
15721 }
15722
15723 if (!any)
15724 {
15725 warning (_("Nothing to save."));
15726 return;
15727 }
15728
15729 filename = tilde_expand (filename);
15730 cleanup = make_cleanup (xfree, filename);
15731 fp = gdb_fopen (filename, "w");
15732 if (!fp)
15733 error (_("Unable to open file '%s' for saving (%s)"),
15734 filename, safe_strerror (errno));
15735 make_cleanup_ui_file_delete (fp);
15736
15737 if (extra_trace_bits)
15738 save_trace_state_variables (fp);
15739
15740 ALL_BREAKPOINTS (tp)
15741 {
15742 /* Skip internal and momentary breakpoints. */
15743 if (!user_breakpoint_p (tp))
15744 continue;
15745
15746 /* If we have a filter, only save the breakpoints it accepts. */
15747 if (filter && !filter (tp))
15748 continue;
15749
15750 tp->ops->print_recreate (tp, fp);
15751
15752 /* Note, we can't rely on tp->number for anything, as we can't
15753 assume the recreated breakpoint numbers will match. Use $bpnum
15754 instead. */
15755
15756 if (tp->cond_string)
15757 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
15758
15759 if (tp->ignore_count)
15760 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
15761
15762 if (tp->type != bp_dprintf && tp->commands)
15763 {
15764 fprintf_unfiltered (fp, " commands\n");
15765
15766 ui_out_redirect (current_uiout, fp);
15767 TRY
15768 {
15769 print_command_lines (current_uiout, tp->commands->commands, 2);
15770 }
15771 CATCH (ex, RETURN_MASK_ALL)
15772 {
15773 ui_out_redirect (current_uiout, NULL);
15774 throw_exception (ex);
15775 }
15776 END_CATCH
15777
15778 ui_out_redirect (current_uiout, NULL);
15779 fprintf_unfiltered (fp, " end\n");
15780 }
15781
15782 if (tp->enable_state == bp_disabled)
15783 fprintf_unfiltered (fp, "disable $bpnum\n");
15784
15785 /* If this is a multi-location breakpoint, check if the locations
15786 should be individually disabled. Watchpoint locations are
15787 special, and not user visible. */
15788 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15789 {
15790 struct bp_location *loc;
15791 int n = 1;
15792
15793 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15794 if (!loc->enabled)
15795 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
15796 }
15797 }
15798
15799 if (extra_trace_bits && *default_collect)
15800 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
15801
15802 if (from_tty)
15803 printf_filtered (_("Saved to file '%s'.\n"), filename);
15804 do_cleanups (cleanup);
15805 }
15806
15807 /* The `save breakpoints' command. */
15808
15809 static void
15810 save_breakpoints_command (char *args, int from_tty)
15811 {
15812 save_breakpoints (args, from_tty, NULL);
15813 }
15814
15815 /* The `save tracepoints' command. */
15816
15817 static void
15818 save_tracepoints_command (char *args, int from_tty)
15819 {
15820 save_breakpoints (args, from_tty, is_tracepoint);
15821 }
15822
15823 /* Create a vector of all tracepoints. */
15824
15825 VEC(breakpoint_p) *
15826 all_tracepoints (void)
15827 {
15828 VEC(breakpoint_p) *tp_vec = 0;
15829 struct breakpoint *tp;
15830
15831 ALL_TRACEPOINTS (tp)
15832 {
15833 VEC_safe_push (breakpoint_p, tp_vec, tp);
15834 }
15835
15836 return tp_vec;
15837 }
15838
15839 \f
15840 /* This help string is used to consolidate all the help string for specifying
15841 locations used by several commands. */
15842
15843 #define LOCATION_HELP_STRING \
15844 "Linespecs are colon-separated lists of location parameters, such as\n\
15845 source filename, function name, label name, and line number.\n\
15846 Example: To specify the start of a label named \"the_top\" in the\n\
15847 function \"fact\" in the file \"factorial.c\", use\n\
15848 \"factorial.c:fact:the_top\".\n\
15849 \n\
15850 Address locations begin with \"*\" and specify an exact address in the\n\
15851 program. Example: To specify the fourth byte past the start function\n\
15852 \"main\", use \"*main + 4\".\n\
15853 \n\
15854 Explicit locations are similar to linespecs but use an option/argument\n\
15855 syntax to specify location parameters.\n\
15856 Example: To specify the start of the label named \"the_top\" in the\n\
15857 function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
15858 -function fact -label the_top\".\n"
15859
15860 /* This help string is used for the break, hbreak, tbreak and thbreak
15861 commands. It is defined as a macro to prevent duplication.
15862 COMMAND should be a string constant containing the name of the
15863 command. */
15864
15865 #define BREAK_ARGS_HELP(command) \
15866 command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15867 PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15868 probe point. Accepted values are `-probe' (for a generic, automatically\n\
15869 guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15870 `-probe-dtrace' (for a DTrace probe).\n\
15871 LOCATION may be a linespec, address, or explicit location as described\n\
15872 below.\n\
15873 \n\
15874 With no LOCATION, uses current execution address of the selected\n\
15875 stack frame. This is useful for breaking on return to a stack frame.\n\
15876 \n\
15877 THREADNUM is the number from \"info threads\".\n\
15878 CONDITION is a boolean expression.\n\
15879 \n" LOCATION_HELP_STRING "\n\
15880 Multiple breakpoints at one place are permitted, and useful if their\n\
15881 conditions are different.\n\
15882 \n\
15883 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15884
15885 /* List of subcommands for "catch". */
15886 static struct cmd_list_element *catch_cmdlist;
15887
15888 /* List of subcommands for "tcatch". */
15889 static struct cmd_list_element *tcatch_cmdlist;
15890
15891 void
15892 add_catch_command (char *name, char *docstring,
15893 cmd_sfunc_ftype *sfunc,
15894 completer_ftype *completer,
15895 void *user_data_catch,
15896 void *user_data_tcatch)
15897 {
15898 struct cmd_list_element *command;
15899
15900 command = add_cmd (name, class_breakpoint, NULL, docstring,
15901 &catch_cmdlist);
15902 set_cmd_sfunc (command, sfunc);
15903 set_cmd_context (command, user_data_catch);
15904 set_cmd_completer (command, completer);
15905
15906 command = add_cmd (name, class_breakpoint, NULL, docstring,
15907 &tcatch_cmdlist);
15908 set_cmd_sfunc (command, sfunc);
15909 set_cmd_context (command, user_data_tcatch);
15910 set_cmd_completer (command, completer);
15911 }
15912
15913 static void
15914 save_command (char *arg, int from_tty)
15915 {
15916 printf_unfiltered (_("\"save\" must be followed by "
15917 "the name of a save subcommand.\n"));
15918 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
15919 }
15920
15921 struct breakpoint *
15922 iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15923 void *data)
15924 {
15925 struct breakpoint *b, *b_tmp;
15926
15927 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15928 {
15929 if ((*callback) (b, data))
15930 return b;
15931 }
15932
15933 return NULL;
15934 }
15935
15936 /* Zero if any of the breakpoint's locations could be a location where
15937 functions have been inlined, nonzero otherwise. */
15938
15939 static int
15940 is_non_inline_function (struct breakpoint *b)
15941 {
15942 /* The shared library event breakpoint is set on the address of a
15943 non-inline function. */
15944 if (b->type == bp_shlib_event)
15945 return 1;
15946
15947 return 0;
15948 }
15949
15950 /* Nonzero if the specified PC cannot be a location where functions
15951 have been inlined. */
15952
15953 int
15954 pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
15955 const struct target_waitstatus *ws)
15956 {
15957 struct breakpoint *b;
15958 struct bp_location *bl;
15959
15960 ALL_BREAKPOINTS (b)
15961 {
15962 if (!is_non_inline_function (b))
15963 continue;
15964
15965 for (bl = b->loc; bl != NULL; bl = bl->next)
15966 {
15967 if (!bl->shlib_disabled
15968 && bpstat_check_location (bl, aspace, pc, ws))
15969 return 1;
15970 }
15971 }
15972
15973 return 0;
15974 }
15975
15976 /* Remove any references to OBJFILE which is going to be freed. */
15977
15978 void
15979 breakpoint_free_objfile (struct objfile *objfile)
15980 {
15981 struct bp_location **locp, *loc;
15982
15983 ALL_BP_LOCATIONS (loc, locp)
15984 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
15985 loc->symtab = NULL;
15986 }
15987
15988 void
15989 initialize_breakpoint_ops (void)
15990 {
15991 static int initialized = 0;
15992
15993 struct breakpoint_ops *ops;
15994
15995 if (initialized)
15996 return;
15997 initialized = 1;
15998
15999 /* The breakpoint_ops structure to be inherit by all kinds of
16000 breakpoints (real breakpoints, i.e., user "break" breakpoints,
16001 internal and momentary breakpoints, etc.). */
16002 ops = &bkpt_base_breakpoint_ops;
16003 *ops = base_breakpoint_ops;
16004 ops->re_set = bkpt_re_set;
16005 ops->insert_location = bkpt_insert_location;
16006 ops->remove_location = bkpt_remove_location;
16007 ops->breakpoint_hit = bkpt_breakpoint_hit;
16008 ops->create_sals_from_location = bkpt_create_sals_from_location;
16009 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
16010 ops->decode_location = bkpt_decode_location;
16011
16012 /* The breakpoint_ops structure to be used in regular breakpoints. */
16013 ops = &bkpt_breakpoint_ops;
16014 *ops = bkpt_base_breakpoint_ops;
16015 ops->re_set = bkpt_re_set;
16016 ops->resources_needed = bkpt_resources_needed;
16017 ops->print_it = bkpt_print_it;
16018 ops->print_mention = bkpt_print_mention;
16019 ops->print_recreate = bkpt_print_recreate;
16020
16021 /* Ranged breakpoints. */
16022 ops = &ranged_breakpoint_ops;
16023 *ops = bkpt_breakpoint_ops;
16024 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
16025 ops->resources_needed = resources_needed_ranged_breakpoint;
16026 ops->print_it = print_it_ranged_breakpoint;
16027 ops->print_one = print_one_ranged_breakpoint;
16028 ops->print_one_detail = print_one_detail_ranged_breakpoint;
16029 ops->print_mention = print_mention_ranged_breakpoint;
16030 ops->print_recreate = print_recreate_ranged_breakpoint;
16031
16032 /* Internal breakpoints. */
16033 ops = &internal_breakpoint_ops;
16034 *ops = bkpt_base_breakpoint_ops;
16035 ops->re_set = internal_bkpt_re_set;
16036 ops->check_status = internal_bkpt_check_status;
16037 ops->print_it = internal_bkpt_print_it;
16038 ops->print_mention = internal_bkpt_print_mention;
16039
16040 /* Momentary breakpoints. */
16041 ops = &momentary_breakpoint_ops;
16042 *ops = bkpt_base_breakpoint_ops;
16043 ops->re_set = momentary_bkpt_re_set;
16044 ops->check_status = momentary_bkpt_check_status;
16045 ops->print_it = momentary_bkpt_print_it;
16046 ops->print_mention = momentary_bkpt_print_mention;
16047
16048 /* Momentary breakpoints for bp_longjmp and bp_exception. */
16049 ops = &longjmp_breakpoint_ops;
16050 *ops = momentary_breakpoint_ops;
16051 ops->dtor = longjmp_bkpt_dtor;
16052
16053 /* Probe breakpoints. */
16054 ops = &bkpt_probe_breakpoint_ops;
16055 *ops = bkpt_breakpoint_ops;
16056 ops->insert_location = bkpt_probe_insert_location;
16057 ops->remove_location = bkpt_probe_remove_location;
16058 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
16059 ops->decode_location = bkpt_probe_decode_location;
16060
16061 /* Watchpoints. */
16062 ops = &watchpoint_breakpoint_ops;
16063 *ops = base_breakpoint_ops;
16064 ops->dtor = dtor_watchpoint;
16065 ops->re_set = re_set_watchpoint;
16066 ops->insert_location = insert_watchpoint;
16067 ops->remove_location = remove_watchpoint;
16068 ops->breakpoint_hit = breakpoint_hit_watchpoint;
16069 ops->check_status = check_status_watchpoint;
16070 ops->resources_needed = resources_needed_watchpoint;
16071 ops->works_in_software_mode = works_in_software_mode_watchpoint;
16072 ops->print_it = print_it_watchpoint;
16073 ops->print_mention = print_mention_watchpoint;
16074 ops->print_recreate = print_recreate_watchpoint;
16075 ops->explains_signal = explains_signal_watchpoint;
16076
16077 /* Masked watchpoints. */
16078 ops = &masked_watchpoint_breakpoint_ops;
16079 *ops = watchpoint_breakpoint_ops;
16080 ops->insert_location = insert_masked_watchpoint;
16081 ops->remove_location = remove_masked_watchpoint;
16082 ops->resources_needed = resources_needed_masked_watchpoint;
16083 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
16084 ops->print_it = print_it_masked_watchpoint;
16085 ops->print_one_detail = print_one_detail_masked_watchpoint;
16086 ops->print_mention = print_mention_masked_watchpoint;
16087 ops->print_recreate = print_recreate_masked_watchpoint;
16088
16089 /* Tracepoints. */
16090 ops = &tracepoint_breakpoint_ops;
16091 *ops = base_breakpoint_ops;
16092 ops->re_set = tracepoint_re_set;
16093 ops->breakpoint_hit = tracepoint_breakpoint_hit;
16094 ops->print_one_detail = tracepoint_print_one_detail;
16095 ops->print_mention = tracepoint_print_mention;
16096 ops->print_recreate = tracepoint_print_recreate;
16097 ops->create_sals_from_location = tracepoint_create_sals_from_location;
16098 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
16099 ops->decode_location = tracepoint_decode_location;
16100
16101 /* Probe tracepoints. */
16102 ops = &tracepoint_probe_breakpoint_ops;
16103 *ops = tracepoint_breakpoint_ops;
16104 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
16105 ops->decode_location = tracepoint_probe_decode_location;
16106
16107 /* Static tracepoints with marker (`-m'). */
16108 ops = &strace_marker_breakpoint_ops;
16109 *ops = tracepoint_breakpoint_ops;
16110 ops->create_sals_from_location = strace_marker_create_sals_from_location;
16111 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
16112 ops->decode_location = strace_marker_decode_location;
16113
16114 /* Fork catchpoints. */
16115 ops = &catch_fork_breakpoint_ops;
16116 *ops = base_breakpoint_ops;
16117 ops->insert_location = insert_catch_fork;
16118 ops->remove_location = remove_catch_fork;
16119 ops->breakpoint_hit = breakpoint_hit_catch_fork;
16120 ops->print_it = print_it_catch_fork;
16121 ops->print_one = print_one_catch_fork;
16122 ops->print_mention = print_mention_catch_fork;
16123 ops->print_recreate = print_recreate_catch_fork;
16124
16125 /* Vfork catchpoints. */
16126 ops = &catch_vfork_breakpoint_ops;
16127 *ops = base_breakpoint_ops;
16128 ops->insert_location = insert_catch_vfork;
16129 ops->remove_location = remove_catch_vfork;
16130 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
16131 ops->print_it = print_it_catch_vfork;
16132 ops->print_one = print_one_catch_vfork;
16133 ops->print_mention = print_mention_catch_vfork;
16134 ops->print_recreate = print_recreate_catch_vfork;
16135
16136 /* Exec catchpoints. */
16137 ops = &catch_exec_breakpoint_ops;
16138 *ops = base_breakpoint_ops;
16139 ops->dtor = dtor_catch_exec;
16140 ops->insert_location = insert_catch_exec;
16141 ops->remove_location = remove_catch_exec;
16142 ops->breakpoint_hit = breakpoint_hit_catch_exec;
16143 ops->print_it = print_it_catch_exec;
16144 ops->print_one = print_one_catch_exec;
16145 ops->print_mention = print_mention_catch_exec;
16146 ops->print_recreate = print_recreate_catch_exec;
16147
16148 /* Solib-related catchpoints. */
16149 ops = &catch_solib_breakpoint_ops;
16150 *ops = base_breakpoint_ops;
16151 ops->dtor = dtor_catch_solib;
16152 ops->insert_location = insert_catch_solib;
16153 ops->remove_location = remove_catch_solib;
16154 ops->breakpoint_hit = breakpoint_hit_catch_solib;
16155 ops->check_status = check_status_catch_solib;
16156 ops->print_it = print_it_catch_solib;
16157 ops->print_one = print_one_catch_solib;
16158 ops->print_mention = print_mention_catch_solib;
16159 ops->print_recreate = print_recreate_catch_solib;
16160
16161 ops = &dprintf_breakpoint_ops;
16162 *ops = bkpt_base_breakpoint_ops;
16163 ops->re_set = dprintf_re_set;
16164 ops->resources_needed = bkpt_resources_needed;
16165 ops->print_it = bkpt_print_it;
16166 ops->print_mention = bkpt_print_mention;
16167 ops->print_recreate = dprintf_print_recreate;
16168 ops->after_condition_true = dprintf_after_condition_true;
16169 ops->breakpoint_hit = dprintf_breakpoint_hit;
16170 }
16171
16172 /* Chain containing all defined "enable breakpoint" subcommands. */
16173
16174 static struct cmd_list_element *enablebreaklist = NULL;
16175
16176 void
16177 _initialize_breakpoint (void)
16178 {
16179 struct cmd_list_element *c;
16180
16181 initialize_breakpoint_ops ();
16182
16183 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
16184 observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
16185 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
16186
16187 breakpoint_objfile_key
16188 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
16189
16190 breakpoint_chain = 0;
16191 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
16192 before a breakpoint is set. */
16193 breakpoint_count = 0;
16194
16195 tracepoint_count = 0;
16196
16197 add_com ("ignore", class_breakpoint, ignore_command, _("\
16198 Set ignore-count of breakpoint number N to COUNT.\n\
16199 Usage is `ignore N COUNT'."));
16200
16201 add_com ("commands", class_breakpoint, commands_command, _("\
16202 Set commands to be executed when a breakpoint is hit.\n\
16203 Give breakpoint number as argument after \"commands\".\n\
16204 With no argument, the targeted breakpoint is the last one set.\n\
16205 The commands themselves follow starting on the next line.\n\
16206 Type a line containing \"end\" to indicate the end of them.\n\
16207 Give \"silent\" as the first line to make the breakpoint silent;\n\
16208 then no output is printed when it is hit, except what the commands print."));
16209
16210 c = add_com ("condition", class_breakpoint, condition_command, _("\
16211 Specify breakpoint number N to break only if COND is true.\n\
16212 Usage is `condition N COND', where N is an integer and COND is an\n\
16213 expression to be evaluated whenever breakpoint N is reached."));
16214 set_cmd_completer (c, condition_completer);
16215
16216 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
16217 Set a temporary breakpoint.\n\
16218 Like \"break\" except the breakpoint is only temporary,\n\
16219 so it will be deleted when hit. Equivalent to \"break\" followed\n\
16220 by using \"enable delete\" on the breakpoint number.\n\
16221 \n"
16222 BREAK_ARGS_HELP ("tbreak")));
16223 set_cmd_completer (c, location_completer);
16224
16225 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
16226 Set a hardware assisted breakpoint.\n\
16227 Like \"break\" except the breakpoint requires hardware support,\n\
16228 some target hardware may not have this support.\n\
16229 \n"
16230 BREAK_ARGS_HELP ("hbreak")));
16231 set_cmd_completer (c, location_completer);
16232
16233 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
16234 Set a temporary hardware assisted breakpoint.\n\
16235 Like \"hbreak\" except the breakpoint is only temporary,\n\
16236 so it will be deleted when hit.\n\
16237 \n"
16238 BREAK_ARGS_HELP ("thbreak")));
16239 set_cmd_completer (c, location_completer);
16240
16241 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
16242 Enable some breakpoints.\n\
16243 Give breakpoint numbers (separated by spaces) as arguments.\n\
16244 With no subcommand, breakpoints are enabled until you command otherwise.\n\
16245 This is used to cancel the effect of the \"disable\" command.\n\
16246 With a subcommand you can enable temporarily."),
16247 &enablelist, "enable ", 1, &cmdlist);
16248
16249 add_com_alias ("en", "enable", class_breakpoint, 1);
16250
16251 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
16252 Enable some breakpoints.\n\
16253 Give breakpoint numbers (separated by spaces) as arguments.\n\
16254 This is used to cancel the effect of the \"disable\" command.\n\
16255 May be abbreviated to simply \"enable\".\n"),
16256 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
16257
16258 add_cmd ("once", no_class, enable_once_command, _("\
16259 Enable breakpoints for one hit. Give breakpoint numbers.\n\
16260 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
16261 &enablebreaklist);
16262
16263 add_cmd ("delete", no_class, enable_delete_command, _("\
16264 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16265 If a breakpoint is hit while enabled in this fashion, it is deleted."),
16266 &enablebreaklist);
16267
16268 add_cmd ("count", no_class, enable_count_command, _("\
16269 Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16270 If a breakpoint is hit while enabled in this fashion,\n\
16271 the count is decremented; when it reaches zero, the breakpoint is disabled."),
16272 &enablebreaklist);
16273
16274 add_cmd ("delete", no_class, enable_delete_command, _("\
16275 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16276 If a breakpoint is hit while enabled in this fashion, it is deleted."),
16277 &enablelist);
16278
16279 add_cmd ("once", no_class, enable_once_command, _("\
16280 Enable breakpoints for one hit. Give breakpoint numbers.\n\
16281 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
16282 &enablelist);
16283
16284 add_cmd ("count", no_class, enable_count_command, _("\
16285 Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16286 If a breakpoint is hit while enabled in this fashion,\n\
16287 the count is decremented; when it reaches zero, the breakpoint is disabled."),
16288 &enablelist);
16289
16290 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
16291 Disable some breakpoints.\n\
16292 Arguments are breakpoint numbers with spaces in between.\n\
16293 To disable all breakpoints, give no argument.\n\
16294 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
16295 &disablelist, "disable ", 1, &cmdlist);
16296 add_com_alias ("dis", "disable", class_breakpoint, 1);
16297 add_com_alias ("disa", "disable", class_breakpoint, 1);
16298
16299 add_cmd ("breakpoints", class_alias, disable_command, _("\
16300 Disable some breakpoints.\n\
16301 Arguments are breakpoint numbers with spaces in between.\n\
16302 To disable all breakpoints, give no argument.\n\
16303 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
16304 This command may be abbreviated \"disable\"."),
16305 &disablelist);
16306
16307 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
16308 Delete some breakpoints or auto-display expressions.\n\
16309 Arguments are breakpoint numbers with spaces in between.\n\
16310 To delete all breakpoints, give no argument.\n\
16311 \n\
16312 Also a prefix command for deletion of other GDB objects.\n\
16313 The \"unset\" command is also an alias for \"delete\"."),
16314 &deletelist, "delete ", 1, &cmdlist);
16315 add_com_alias ("d", "delete", class_breakpoint, 1);
16316 add_com_alias ("del", "delete", class_breakpoint, 1);
16317
16318 add_cmd ("breakpoints", class_alias, delete_command, _("\
16319 Delete some breakpoints or auto-display expressions.\n\
16320 Arguments are breakpoint numbers with spaces in between.\n\
16321 To delete all breakpoints, give no argument.\n\
16322 This command may be abbreviated \"delete\"."),
16323 &deletelist);
16324
16325 add_com ("clear", class_breakpoint, clear_command, _("\
16326 Clear breakpoint at specified location.\n\
16327 Argument may be a linespec, explicit, or address location as described below.\n\
16328 \n\
16329 With no argument, clears all breakpoints in the line that the selected frame\n\
16330 is executing in.\n"
16331 "\n" LOCATION_HELP_STRING "\n\
16332 See also the \"delete\" command which clears breakpoints by number."));
16333 add_com_alias ("cl", "clear", class_breakpoint, 1);
16334
16335 c = add_com ("break", class_breakpoint, break_command, _("\
16336 Set breakpoint at specified location.\n"
16337 BREAK_ARGS_HELP ("break")));
16338 set_cmd_completer (c, location_completer);
16339
16340 add_com_alias ("b", "break", class_run, 1);
16341 add_com_alias ("br", "break", class_run, 1);
16342 add_com_alias ("bre", "break", class_run, 1);
16343 add_com_alias ("brea", "break", class_run, 1);
16344
16345 if (dbx_commands)
16346 {
16347 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
16348 Break in function/address or break at a line in the current file."),
16349 &stoplist, "stop ", 1, &cmdlist);
16350 add_cmd ("in", class_breakpoint, stopin_command,
16351 _("Break in function or address."), &stoplist);
16352 add_cmd ("at", class_breakpoint, stopat_command,
16353 _("Break at a line in the current file."), &stoplist);
16354 add_com ("status", class_info, breakpoints_info, _("\
16355 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
16356 The \"Type\" column indicates one of:\n\
16357 \tbreakpoint - normal breakpoint\n\
16358 \twatchpoint - watchpoint\n\
16359 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16360 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16361 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16362 address and file/line number respectively.\n\
16363 \n\
16364 Convenience variable \"$_\" and default examine address for \"x\"\n\
16365 are set to the address of the last breakpoint listed unless the command\n\
16366 is prefixed with \"server \".\n\n\
16367 Convenience variable \"$bpnum\" contains the number of the last\n\
16368 breakpoint set."));
16369 }
16370
16371 add_info ("breakpoints", breakpoints_info, _("\
16372 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
16373 The \"Type\" column indicates one of:\n\
16374 \tbreakpoint - normal breakpoint\n\
16375 \twatchpoint - watchpoint\n\
16376 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16377 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16378 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16379 address and file/line number respectively.\n\
16380 \n\
16381 Convenience variable \"$_\" and default examine address for \"x\"\n\
16382 are set to the address of the last breakpoint listed unless the command\n\
16383 is prefixed with \"server \".\n\n\
16384 Convenience variable \"$bpnum\" contains the number of the last\n\
16385 breakpoint set."));
16386
16387 add_info_alias ("b", "breakpoints", 1);
16388
16389 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
16390 Status of all breakpoints, or breakpoint number NUMBER.\n\
16391 The \"Type\" column indicates one of:\n\
16392 \tbreakpoint - normal breakpoint\n\
16393 \twatchpoint - watchpoint\n\
16394 \tlongjmp - internal breakpoint used to step through longjmp()\n\
16395 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
16396 \tuntil - internal breakpoint used by the \"until\" command\n\
16397 \tfinish - internal breakpoint used by the \"finish\" command\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 &maintenanceinfolist);
16409
16410 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
16411 Set catchpoints to catch events."),
16412 &catch_cmdlist, "catch ",
16413 0/*allow-unknown*/, &cmdlist);
16414
16415 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
16416 Set temporary catchpoints to catch events."),
16417 &tcatch_cmdlist, "tcatch ",
16418 0/*allow-unknown*/, &cmdlist);
16419
16420 add_catch_command ("fork", _("Catch calls to fork."),
16421 catch_fork_command_1,
16422 NULL,
16423 (void *) (uintptr_t) catch_fork_permanent,
16424 (void *) (uintptr_t) catch_fork_temporary);
16425 add_catch_command ("vfork", _("Catch calls to vfork."),
16426 catch_fork_command_1,
16427 NULL,
16428 (void *) (uintptr_t) catch_vfork_permanent,
16429 (void *) (uintptr_t) catch_vfork_temporary);
16430 add_catch_command ("exec", _("Catch calls to exec."),
16431 catch_exec_command_1,
16432 NULL,
16433 CATCH_PERMANENT,
16434 CATCH_TEMPORARY);
16435 add_catch_command ("load", _("Catch loads of shared libraries.\n\
16436 Usage: catch load [REGEX]\n\
16437 If REGEX is given, only stop for libraries matching the regular expression."),
16438 catch_load_command_1,
16439 NULL,
16440 CATCH_PERMANENT,
16441 CATCH_TEMPORARY);
16442 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
16443 Usage: catch unload [REGEX]\n\
16444 If REGEX is given, only stop for libraries matching the regular expression."),
16445 catch_unload_command_1,
16446 NULL,
16447 CATCH_PERMANENT,
16448 CATCH_TEMPORARY);
16449
16450 c = add_com ("watch", class_breakpoint, watch_command, _("\
16451 Set a watchpoint for an expression.\n\
16452 Usage: watch [-l|-location] EXPRESSION\n\
16453 A watchpoint stops execution of your program whenever the value of\n\
16454 an expression changes.\n\
16455 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16456 the memory to which it refers."));
16457 set_cmd_completer (c, expression_completer);
16458
16459 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
16460 Set a read watchpoint for an expression.\n\
16461 Usage: rwatch [-l|-location] EXPRESSION\n\
16462 A watchpoint stops execution of your program whenever the value of\n\
16463 an expression is read.\n\
16464 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16465 the memory to which it refers."));
16466 set_cmd_completer (c, expression_completer);
16467
16468 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
16469 Set a watchpoint for an expression.\n\
16470 Usage: awatch [-l|-location] EXPRESSION\n\
16471 A watchpoint stops execution of your program whenever the value of\n\
16472 an expression is either read or written.\n\
16473 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16474 the memory to which it refers."));
16475 set_cmd_completer (c, expression_completer);
16476
16477 add_info ("watchpoints", watchpoints_info, _("\
16478 Status of specified watchpoints (all watchpoints if no argument)."));
16479
16480 /* XXX: cagney/2005-02-23: This should be a boolean, and should
16481 respond to changes - contrary to the description. */
16482 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
16483 &can_use_hw_watchpoints, _("\
16484 Set debugger's willingness to use watchpoint hardware."), _("\
16485 Show debugger's willingness to use watchpoint hardware."), _("\
16486 If zero, gdb will not use hardware for new watchpoints, even if\n\
16487 such is available. (However, any hardware watchpoints that were\n\
16488 created before setting this to nonzero, will continue to use watchpoint\n\
16489 hardware.)"),
16490 NULL,
16491 show_can_use_hw_watchpoints,
16492 &setlist, &showlist);
16493
16494 can_use_hw_watchpoints = 1;
16495
16496 /* Tracepoint manipulation commands. */
16497
16498 c = add_com ("trace", class_breakpoint, trace_command, _("\
16499 Set a tracepoint at specified location.\n\
16500 \n"
16501 BREAK_ARGS_HELP ("trace") "\n\
16502 Do \"help tracepoints\" for info on other tracepoint commands."));
16503 set_cmd_completer (c, location_completer);
16504
16505 add_com_alias ("tp", "trace", class_alias, 0);
16506 add_com_alias ("tr", "trace", class_alias, 1);
16507 add_com_alias ("tra", "trace", class_alias, 1);
16508 add_com_alias ("trac", "trace", class_alias, 1);
16509
16510 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
16511 Set a fast tracepoint at specified location.\n\
16512 \n"
16513 BREAK_ARGS_HELP ("ftrace") "\n\
16514 Do \"help tracepoints\" for info on other tracepoint commands."));
16515 set_cmd_completer (c, location_completer);
16516
16517 c = add_com ("strace", class_breakpoint, strace_command, _("\
16518 Set a static tracepoint at location or marker.\n\
16519 \n\
16520 strace [LOCATION] [if CONDITION]\n\
16521 LOCATION may be a linespec, explicit, or address location (described below) \n\
16522 or -m MARKER_ID.\n\n\
16523 If a marker id is specified, probe the marker with that name. With\n\
16524 no LOCATION, uses current execution address of the selected stack frame.\n\
16525 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16526 This collects arbitrary user data passed in the probe point call to the\n\
16527 tracing library. You can inspect it when analyzing the trace buffer,\n\
16528 by printing the $_sdata variable like any other convenience variable.\n\
16529 \n\
16530 CONDITION is a boolean expression.\n\
16531 \n" LOCATION_HELP_STRING "\n\
16532 Multiple tracepoints at one place are permitted, and useful if their\n\
16533 conditions are different.\n\
16534 \n\
16535 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16536 Do \"help tracepoints\" for info on other tracepoint commands."));
16537 set_cmd_completer (c, location_completer);
16538
16539 add_info ("tracepoints", tracepoints_info, _("\
16540 Status of specified tracepoints (all tracepoints if no argument).\n\
16541 Convenience variable \"$tpnum\" contains the number of the\n\
16542 last tracepoint set."));
16543
16544 add_info_alias ("tp", "tracepoints", 1);
16545
16546 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16547 Delete specified tracepoints.\n\
16548 Arguments are tracepoint numbers, separated by spaces.\n\
16549 No argument means delete all tracepoints."),
16550 &deletelist);
16551 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
16552
16553 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16554 Disable specified tracepoints.\n\
16555 Arguments are tracepoint numbers, separated by spaces.\n\
16556 No argument means disable all tracepoints."),
16557 &disablelist);
16558 deprecate_cmd (c, "disable");
16559
16560 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16561 Enable specified tracepoints.\n\
16562 Arguments are tracepoint numbers, separated by spaces.\n\
16563 No argument means enable all tracepoints."),
16564 &enablelist);
16565 deprecate_cmd (c, "enable");
16566
16567 add_com ("passcount", class_trace, trace_pass_command, _("\
16568 Set the passcount for a tracepoint.\n\
16569 The trace will end when the tracepoint has been passed 'count' times.\n\
16570 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16571 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16572
16573 add_prefix_cmd ("save", class_breakpoint, save_command,
16574 _("Save breakpoint definitions as a script."),
16575 &save_cmdlist, "save ",
16576 0/*allow-unknown*/, &cmdlist);
16577
16578 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16579 Save current breakpoint definitions as a script.\n\
16580 This includes all types of breakpoints (breakpoints, watchpoints,\n\
16581 catchpoints, tracepoints). Use the 'source' command in another debug\n\
16582 session to restore them."),
16583 &save_cmdlist);
16584 set_cmd_completer (c, filename_completer);
16585
16586 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
16587 Save current tracepoint definitions as a script.\n\
16588 Use the 'source' command in another debug session to restore them."),
16589 &save_cmdlist);
16590 set_cmd_completer (c, filename_completer);
16591
16592 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16593 deprecate_cmd (c, "save tracepoints");
16594
16595 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
16596 Breakpoint specific settings\n\
16597 Configure various breakpoint-specific variables such as\n\
16598 pending breakpoint behavior"),
16599 &breakpoint_set_cmdlist, "set breakpoint ",
16600 0/*allow-unknown*/, &setlist);
16601 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
16602 Breakpoint specific settings\n\
16603 Configure various breakpoint-specific variables such as\n\
16604 pending breakpoint behavior"),
16605 &breakpoint_show_cmdlist, "show breakpoint ",
16606 0/*allow-unknown*/, &showlist);
16607
16608 add_setshow_auto_boolean_cmd ("pending", no_class,
16609 &pending_break_support, _("\
16610 Set debugger's behavior regarding pending breakpoints."), _("\
16611 Show debugger's behavior regarding pending breakpoints."), _("\
16612 If on, an unrecognized breakpoint location will cause gdb to create a\n\
16613 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16614 an error. If auto, an unrecognized breakpoint location results in a\n\
16615 user-query to see if a pending breakpoint should be created."),
16616 NULL,
16617 show_pending_break_support,
16618 &breakpoint_set_cmdlist,
16619 &breakpoint_show_cmdlist);
16620
16621 pending_break_support = AUTO_BOOLEAN_AUTO;
16622
16623 add_setshow_boolean_cmd ("auto-hw", no_class,
16624 &automatic_hardware_breakpoints, _("\
16625 Set automatic usage of hardware breakpoints."), _("\
16626 Show automatic usage of hardware breakpoints."), _("\
16627 If set, the debugger will automatically use hardware breakpoints for\n\
16628 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16629 a warning will be emitted for such breakpoints."),
16630 NULL,
16631 show_automatic_hardware_breakpoints,
16632 &breakpoint_set_cmdlist,
16633 &breakpoint_show_cmdlist);
16634
16635 add_setshow_boolean_cmd ("always-inserted", class_support,
16636 &always_inserted_mode, _("\
16637 Set mode for inserting breakpoints."), _("\
16638 Show mode for inserting breakpoints."), _("\
16639 When this mode is on, breakpoints are inserted immediately as soon as\n\
16640 they're created, kept inserted even when execution stops, and removed\n\
16641 only when the user deletes them. When this mode is off (the default),\n\
16642 breakpoints are inserted only when execution continues, and removed\n\
16643 when execution stops."),
16644 NULL,
16645 &show_always_inserted_mode,
16646 &breakpoint_set_cmdlist,
16647 &breakpoint_show_cmdlist);
16648
16649 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16650 condition_evaluation_enums,
16651 &condition_evaluation_mode_1, _("\
16652 Set mode of breakpoint condition evaluation."), _("\
16653 Show mode of breakpoint condition evaluation."), _("\
16654 When this is set to \"host\", breakpoint conditions will be\n\
16655 evaluated on the host's side by GDB. When it is set to \"target\",\n\
16656 breakpoint conditions will be downloaded to the target (if the target\n\
16657 supports such feature) and conditions will be evaluated on the target's side.\n\
16658 If this is set to \"auto\" (default), this will be automatically set to\n\
16659 \"target\" if it supports condition evaluation, otherwise it will\n\
16660 be set to \"gdb\""),
16661 &set_condition_evaluation_mode,
16662 &show_condition_evaluation_mode,
16663 &breakpoint_set_cmdlist,
16664 &breakpoint_show_cmdlist);
16665
16666 add_com ("break-range", class_breakpoint, break_range_command, _("\
16667 Set a breakpoint for an address range.\n\
16668 break-range START-LOCATION, END-LOCATION\n\
16669 where START-LOCATION and END-LOCATION can be one of the following:\n\
16670 LINENUM, for that line in the current file,\n\
16671 FILE:LINENUM, for that line in that file,\n\
16672 +OFFSET, for that number of lines after the current line\n\
16673 or the start of the range\n\
16674 FUNCTION, for the first line in that function,\n\
16675 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16676 *ADDRESS, for the instruction at that address.\n\
16677 \n\
16678 The breakpoint will stop execution of the inferior whenever it executes\n\
16679 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16680 range (including START-LOCATION and END-LOCATION)."));
16681
16682 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
16683 Set a dynamic printf at specified location.\n\
16684 dprintf location,format string,arg1,arg2,...\n\
16685 location may be a linespec, explicit, or address location.\n"
16686 "\n" LOCATION_HELP_STRING));
16687 set_cmd_completer (c, location_completer);
16688
16689 add_setshow_enum_cmd ("dprintf-style", class_support,
16690 dprintf_style_enums, &dprintf_style, _("\
16691 Set the style of usage for dynamic printf."), _("\
16692 Show the style of usage for dynamic printf."), _("\
16693 This setting chooses how GDB will do a dynamic printf.\n\
16694 If the value is \"gdb\", then the printing is done by GDB to its own\n\
16695 console, as with the \"printf\" command.\n\
16696 If the value is \"call\", the print is done by calling a function in your\n\
16697 program; by default printf(), but you can choose a different function or\n\
16698 output stream by setting dprintf-function and dprintf-channel."),
16699 update_dprintf_commands, NULL,
16700 &setlist, &showlist);
16701
16702 dprintf_function = xstrdup ("printf");
16703 add_setshow_string_cmd ("dprintf-function", class_support,
16704 &dprintf_function, _("\
16705 Set the function to use for dynamic printf"), _("\
16706 Show the function to use for dynamic printf"), NULL,
16707 update_dprintf_commands, NULL,
16708 &setlist, &showlist);
16709
16710 dprintf_channel = xstrdup ("");
16711 add_setshow_string_cmd ("dprintf-channel", class_support,
16712 &dprintf_channel, _("\
16713 Set the channel to use for dynamic printf"), _("\
16714 Show the channel to use for dynamic printf"), NULL,
16715 update_dprintf_commands, NULL,
16716 &setlist, &showlist);
16717
16718 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16719 &disconnected_dprintf, _("\
16720 Set whether dprintf continues after GDB disconnects."), _("\
16721 Show whether dprintf continues after GDB disconnects."), _("\
16722 Use this to let dprintf commands continue to hit and produce output\n\
16723 even if GDB disconnects or detaches from the target."),
16724 NULL,
16725 NULL,
16726 &setlist, &showlist);
16727
16728 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16729 agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16730 (target agent only) This is useful for formatted output in user-defined commands."));
16731
16732 automatic_hardware_breakpoints = 1;
16733
16734 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
16735 observer_attach_thread_exit (remove_threaded_breakpoints);
16736 }