c8831456d235ef34448b800d85293a493d56ef56
[deliverable/binutils-gdb.git] / gdb / breakpoint.c
1 /* Everything about breakpoints, for GDB.
2
3 Copyright (C) 1986-2016 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21 #include "arch-utils.h"
22 #include <ctype.h>
23 #include "hashtab.h"
24 #include "symtab.h"
25 #include "frame.h"
26 #include "breakpoint.h"
27 #include "tracepoint.h"
28 #include "gdbtypes.h"
29 #include "expression.h"
30 #include "gdbcore.h"
31 #include "gdbcmd.h"
32 #include "value.h"
33 #include "command.h"
34 #include "inferior.h"
35 #include "infrun.h"
36 #include "gdbthread.h"
37 #include "target.h"
38 #include "language.h"
39 #include "gdb-demangle.h"
40 #include "filenames.h"
41 #include "annotate.h"
42 #include "symfile.h"
43 #include "objfiles.h"
44 #include "source.h"
45 #include "linespec.h"
46 #include "completer.h"
47 #include "gdb.h"
48 #include "ui-out.h"
49 #include "cli/cli-script.h"
50 #include "block.h"
51 #include "solib.h"
52 #include "solist.h"
53 #include "observer.h"
54 #include "memattr.h"
55 #include "ada-lang.h"
56 #include "top.h"
57 #include "valprint.h"
58 #include "jit.h"
59 #include "parser-defs.h"
60 #include "gdb_regex.h"
61 #include "probe.h"
62 #include "cli/cli-utils.h"
63 #include "continuations.h"
64 #include "stack.h"
65 #include "skip.h"
66 #include "ax-gdb.h"
67 #include "dummy-frame.h"
68 #include "interps.h"
69 #include "format.h"
70 #include "location.h"
71 #include "thread-fsm.h"
72 #include "tid-parse.h"
73
74 /* readline include files */
75 #include "readline/readline.h"
76 #include "readline/history.h"
77
78 /* readline defines this. */
79 #undef savestring
80
81 #include "mi/mi-common.h"
82 #include "extension.h"
83 #include <algorithm>
84
85 /* Enums for exception-handling support. */
86 enum exception_event_kind
87 {
88 EX_EVENT_THROW,
89 EX_EVENT_RETHROW,
90 EX_EVENT_CATCH
91 };
92
93 /* Prototypes for local functions. */
94
95 static void enable_delete_command (char *, int);
96
97 static void enable_once_command (char *, int);
98
99 static void enable_count_command (char *, int);
100
101 static void disable_command (char *, int);
102
103 static void enable_command (char *, int);
104
105 static void map_breakpoint_numbers (const char *,
106 void (*) (struct breakpoint *,
107 void *),
108 void *);
109
110 static void ignore_command (char *, int);
111
112 static int breakpoint_re_set_one (void *);
113
114 static void breakpoint_re_set_default (struct breakpoint *);
115
116 static void
117 create_sals_from_location_default (const struct event_location *location,
118 struct linespec_result *canonical,
119 enum bptype type_wanted);
120
121 static void create_breakpoints_sal_default (struct gdbarch *,
122 struct linespec_result *,
123 char *, char *, enum bptype,
124 enum bpdisp, int, int,
125 int,
126 const struct breakpoint_ops *,
127 int, int, int, unsigned);
128
129 static void decode_location_default (struct breakpoint *b,
130 const struct event_location *location,
131 struct program_space *search_pspace,
132 struct symtabs_and_lines *sals);
133
134 static void clear_command (char *, int);
135
136 static void catch_command (char *, int);
137
138 static int can_use_hardware_watchpoint (struct value *);
139
140 static void break_command_1 (char *, int, int);
141
142 static void mention (struct breakpoint *);
143
144 static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
145 enum bptype,
146 const struct breakpoint_ops *);
147 static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
148 const struct symtab_and_line *);
149
150 /* This function is used in gdbtk sources and thus can not be made
151 static. */
152 struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
153 struct symtab_and_line,
154 enum bptype,
155 const struct breakpoint_ops *);
156
157 static struct breakpoint *
158 momentary_breakpoint_from_master (struct breakpoint *orig,
159 enum bptype type,
160 const struct breakpoint_ops *ops,
161 int loc_enabled);
162
163 static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
164
165 static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
166 CORE_ADDR bpaddr,
167 enum bptype bptype);
168
169 static void describe_other_breakpoints (struct gdbarch *,
170 struct program_space *, CORE_ADDR,
171 struct obj_section *, int);
172
173 static int watchpoint_locations_match (struct bp_location *loc1,
174 struct bp_location *loc2);
175
176 static int breakpoint_location_address_match (struct bp_location *bl,
177 struct address_space *aspace,
178 CORE_ADDR addr);
179
180 static int breakpoint_location_address_range_overlap (struct bp_location *,
181 struct address_space *,
182 CORE_ADDR, int);
183
184 static void breakpoints_info (char *, int);
185
186 static void watchpoints_info (char *, int);
187
188 static int breakpoint_1 (char *, int,
189 int (*) (const struct breakpoint *));
190
191 static int breakpoint_cond_eval (void *);
192
193 static void cleanup_executing_breakpoints (void *);
194
195 static void commands_command (char *, int);
196
197 static void condition_command (char *, int);
198
199 static int remove_breakpoint (struct bp_location *);
200 static int remove_breakpoint_1 (struct bp_location *, enum remove_bp_reason);
201
202 static enum print_stop_action print_bp_stop_message (bpstat bs);
203
204 static int watchpoint_check (void *);
205
206 static void maintenance_info_breakpoints (char *, int);
207
208 static int hw_breakpoint_used_count (void);
209
210 static int hw_watchpoint_use_count (struct breakpoint *);
211
212 static int hw_watchpoint_used_count_others (struct breakpoint *except,
213 enum bptype type,
214 int *other_type_used);
215
216 static void hbreak_command (char *, int);
217
218 static void thbreak_command (char *, int);
219
220 static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
221 int count);
222
223 static void stop_command (char *arg, int from_tty);
224
225 static void stopin_command (char *arg, int from_tty);
226
227 static void stopat_command (char *arg, int from_tty);
228
229 static void tcatch_command (char *arg, int from_tty);
230
231 static void free_bp_location (struct bp_location *loc);
232 static void incref_bp_location (struct bp_location *loc);
233 static void decref_bp_location (struct bp_location **loc);
234
235 static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
236
237 /* update_global_location_list's modes of operation wrt to whether to
238 insert locations now. */
239 enum ugll_insert_mode
240 {
241 /* Don't insert any breakpoint locations into the inferior, only
242 remove already-inserted locations that no longer should be
243 inserted. Functions that delete a breakpoint or breakpoints
244 should specify this mode, so that deleting a breakpoint doesn't
245 have the side effect of inserting the locations of other
246 breakpoints that are marked not-inserted, but should_be_inserted
247 returns true on them.
248
249 This behavior is useful is situations close to tear-down -- e.g.,
250 after an exec, while the target still has execution, but
251 breakpoint shadows of the previous executable image should *NOT*
252 be restored to the new image; or before detaching, where the
253 target still has execution and wants to delete breakpoints from
254 GDB's lists, and all breakpoints had already been removed from
255 the inferior. */
256 UGLL_DONT_INSERT,
257
258 /* May insert breakpoints iff breakpoints_should_be_inserted_now
259 claims breakpoints should be inserted now. */
260 UGLL_MAY_INSERT,
261
262 /* Insert locations now, irrespective of
263 breakpoints_should_be_inserted_now. E.g., say all threads are
264 stopped right now, and the user did "continue". We need to
265 insert breakpoints _before_ resuming the target, but
266 UGLL_MAY_INSERT wouldn't insert them, because
267 breakpoints_should_be_inserted_now returns false at that point,
268 as no thread is running yet. */
269 UGLL_INSERT
270 };
271
272 static void update_global_location_list (enum ugll_insert_mode);
273
274 static void update_global_location_list_nothrow (enum ugll_insert_mode);
275
276 static int is_hardware_watchpoint (const struct breakpoint *bpt);
277
278 static void insert_breakpoint_locations (void);
279
280 static void tracepoints_info (char *, int);
281
282 static void delete_trace_command (char *, int);
283
284 static void enable_trace_command (char *, int);
285
286 static void disable_trace_command (char *, int);
287
288 static void trace_pass_command (char *, int);
289
290 static void set_tracepoint_count (int num);
291
292 static int is_masked_watchpoint (const struct breakpoint *b);
293
294 static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
295
296 /* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
297 otherwise. */
298
299 static int strace_marker_p (struct breakpoint *b);
300
301 /* The breakpoint_ops structure to be inherited by all breakpoint_ops
302 that are implemented on top of software or hardware breakpoints
303 (user breakpoints, internal and momentary breakpoints, etc.). */
304 static struct breakpoint_ops bkpt_base_breakpoint_ops;
305
306 /* Internal breakpoints class type. */
307 static struct breakpoint_ops internal_breakpoint_ops;
308
309 /* Momentary breakpoints class type. */
310 static struct breakpoint_ops momentary_breakpoint_ops;
311
312 /* Momentary breakpoints for bp_longjmp and bp_exception class type. */
313 static struct breakpoint_ops longjmp_breakpoint_ops;
314
315 /* The breakpoint_ops structure to be used in regular user created
316 breakpoints. */
317 struct breakpoint_ops bkpt_breakpoint_ops;
318
319 /* Breakpoints set on probes. */
320 static struct breakpoint_ops bkpt_probe_breakpoint_ops;
321
322 /* Dynamic printf class type. */
323 struct breakpoint_ops dprintf_breakpoint_ops;
324
325 /* The style in which to perform a dynamic printf. This is a user
326 option because different output options have different tradeoffs;
327 if GDB does the printing, there is better error handling if there
328 is a problem with any of the arguments, but using an inferior
329 function lets you have special-purpose printers and sending of
330 output to the same place as compiled-in print functions. */
331
332 static const char dprintf_style_gdb[] = "gdb";
333 static const char dprintf_style_call[] = "call";
334 static const char dprintf_style_agent[] = "agent";
335 static const char *const dprintf_style_enums[] = {
336 dprintf_style_gdb,
337 dprintf_style_call,
338 dprintf_style_agent,
339 NULL
340 };
341 static const char *dprintf_style = dprintf_style_gdb;
342
343 /* The function to use for dynamic printf if the preferred style is to
344 call into the inferior. The value is simply a string that is
345 copied into the command, so it can be anything that GDB can
346 evaluate to a callable address, not necessarily a function name. */
347
348 static char *dprintf_function = "";
349
350 /* The channel to use for dynamic printf if the preferred style is to
351 call into the inferior; if a nonempty string, it will be passed to
352 the call as the first argument, with the format string as the
353 second. As with the dprintf function, this can be anything that
354 GDB knows how to evaluate, so in addition to common choices like
355 "stderr", this could be an app-specific expression like
356 "mystreams[curlogger]". */
357
358 static char *dprintf_channel = "";
359
360 /* True if dprintf commands should continue to operate even if GDB
361 has disconnected. */
362 static int disconnected_dprintf = 1;
363
364 /* A reference-counted struct command_line. This lets multiple
365 breakpoints share a single command list. */
366 struct counted_command_line
367 {
368 /* The reference count. */
369 int refc;
370
371 /* The command list. */
372 struct command_line *commands;
373 };
374
375 struct command_line *
376 breakpoint_commands (struct breakpoint *b)
377 {
378 return b->commands ? b->commands->commands : NULL;
379 }
380
381 /* Flag indicating that a command has proceeded the inferior past the
382 current breakpoint. */
383
384 static int breakpoint_proceeded;
385
386 const char *
387 bpdisp_text (enum bpdisp disp)
388 {
389 /* NOTE: the following values are a part of MI protocol and
390 represent values of 'disp' field returned when inferior stops at
391 a breakpoint. */
392 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
393
394 return bpdisps[(int) disp];
395 }
396
397 /* Prototypes for exported functions. */
398 /* If FALSE, gdb will not use hardware support for watchpoints, even
399 if such is available. */
400 static int can_use_hw_watchpoints;
401
402 static void
403 show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
404 struct cmd_list_element *c,
405 const char *value)
406 {
407 fprintf_filtered (file,
408 _("Debugger's willingness to use "
409 "watchpoint hardware is %s.\n"),
410 value);
411 }
412
413 /* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
414 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
415 for unrecognized breakpoint locations.
416 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
417 static enum auto_boolean pending_break_support;
418 static void
419 show_pending_break_support (struct ui_file *file, int from_tty,
420 struct cmd_list_element *c,
421 const char *value)
422 {
423 fprintf_filtered (file,
424 _("Debugger's behavior regarding "
425 "pending breakpoints is %s.\n"),
426 value);
427 }
428
429 /* If 1, gdb will automatically use hardware breakpoints for breakpoints
430 set with "break" but falling in read-only memory.
431 If 0, gdb will warn about such breakpoints, but won't automatically
432 use hardware breakpoints. */
433 static int automatic_hardware_breakpoints;
434 static void
435 show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
436 struct cmd_list_element *c,
437 const char *value)
438 {
439 fprintf_filtered (file,
440 _("Automatic usage of hardware breakpoints is %s.\n"),
441 value);
442 }
443
444 /* If on, GDB keeps breakpoints inserted even if the inferior is
445 stopped, and immediately inserts any new breakpoints as soon as
446 they're created. If off (default), GDB keeps breakpoints off of
447 the target as long as possible. That is, it delays inserting
448 breakpoints until the next resume, and removes them again when the
449 target fully stops. This is a bit safer in case GDB crashes while
450 processing user input. */
451 static int always_inserted_mode = 0;
452
453 static void
454 show_always_inserted_mode (struct ui_file *file, int from_tty,
455 struct cmd_list_element *c, const char *value)
456 {
457 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
458 value);
459 }
460
461 /* See breakpoint.h. */
462
463 int
464 breakpoints_should_be_inserted_now (void)
465 {
466 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
467 {
468 /* If breakpoints are global, they should be inserted even if no
469 thread under gdb's control is running, or even if there are
470 no threads under GDB's control yet. */
471 return 1;
472 }
473 else if (target_has_execution)
474 {
475 struct thread_info *tp;
476
477 if (always_inserted_mode)
478 {
479 /* The user wants breakpoints inserted even if all threads
480 are stopped. */
481 return 1;
482 }
483
484 if (threads_are_executing ())
485 return 1;
486
487 /* Don't remove breakpoints yet if, even though all threads are
488 stopped, we still have events to process. */
489 ALL_NON_EXITED_THREADS (tp)
490 if (tp->resumed
491 && tp->suspend.waitstatus_pending_p)
492 return 1;
493 }
494 return 0;
495 }
496
497 static const char condition_evaluation_both[] = "host or target";
498
499 /* Modes for breakpoint condition evaluation. */
500 static const char condition_evaluation_auto[] = "auto";
501 static const char condition_evaluation_host[] = "host";
502 static const char condition_evaluation_target[] = "target";
503 static const char *const condition_evaluation_enums[] = {
504 condition_evaluation_auto,
505 condition_evaluation_host,
506 condition_evaluation_target,
507 NULL
508 };
509
510 /* Global that holds the current mode for breakpoint condition evaluation. */
511 static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
512
513 /* Global that we use to display information to the user (gets its value from
514 condition_evaluation_mode_1. */
515 static const char *condition_evaluation_mode = condition_evaluation_auto;
516
517 /* Translate a condition evaluation mode MODE into either "host"
518 or "target". This is used mostly to translate from "auto" to the
519 real setting that is being used. It returns the translated
520 evaluation mode. */
521
522 static const char *
523 translate_condition_evaluation_mode (const char *mode)
524 {
525 if (mode == condition_evaluation_auto)
526 {
527 if (target_supports_evaluation_of_breakpoint_conditions ())
528 return condition_evaluation_target;
529 else
530 return condition_evaluation_host;
531 }
532 else
533 return mode;
534 }
535
536 /* Discovers what condition_evaluation_auto translates to. */
537
538 static const char *
539 breakpoint_condition_evaluation_mode (void)
540 {
541 return translate_condition_evaluation_mode (condition_evaluation_mode);
542 }
543
544 /* Return true if GDB should evaluate breakpoint conditions or false
545 otherwise. */
546
547 static int
548 gdb_evaluates_breakpoint_condition_p (void)
549 {
550 const char *mode = breakpoint_condition_evaluation_mode ();
551
552 return (mode == condition_evaluation_host);
553 }
554
555 void _initialize_breakpoint (void);
556
557 /* Are we executing breakpoint commands? */
558 static int executing_breakpoint_commands;
559
560 /* Are overlay event breakpoints enabled? */
561 static int overlay_events_enabled;
562
563 /* See description in breakpoint.h. */
564 int target_exact_watchpoints = 0;
565
566 /* Walk the following statement or block through all breakpoints.
567 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
568 current breakpoint. */
569
570 #define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
571
572 #define ALL_BREAKPOINTS_SAFE(B,TMP) \
573 for (B = breakpoint_chain; \
574 B ? (TMP=B->next, 1): 0; \
575 B = TMP)
576
577 /* Similar iterator for the low-level breakpoints. SAFE variant is
578 not provided so update_global_location_list must not be called
579 while executing the block of ALL_BP_LOCATIONS. */
580
581 #define ALL_BP_LOCATIONS(B,BP_TMP) \
582 for (BP_TMP = bp_location; \
583 BP_TMP < bp_location + bp_location_count && (B = *BP_TMP); \
584 BP_TMP++)
585
586 /* Iterates through locations with address ADDRESS for the currently selected
587 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
588 to where the loop should start from.
589 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
590 appropriate location to start with. */
591
592 #define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
593 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
594 BP_LOCP_TMP = BP_LOCP_START; \
595 BP_LOCP_START \
596 && (BP_LOCP_TMP < bp_location + bp_location_count \
597 && (*BP_LOCP_TMP)->address == ADDRESS); \
598 BP_LOCP_TMP++)
599
600 /* Iterator for tracepoints only. */
601
602 #define ALL_TRACEPOINTS(B) \
603 for (B = breakpoint_chain; B; B = B->next) \
604 if (is_tracepoint (B))
605
606 /* Chains of all breakpoints defined. */
607
608 struct breakpoint *breakpoint_chain;
609
610 /* Array is sorted by bp_location_compare - primarily by the ADDRESS. */
611
612 static struct bp_location **bp_location;
613
614 /* Number of elements of BP_LOCATION. */
615
616 static unsigned bp_location_count;
617
618 /* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
619 ADDRESS for the current elements of BP_LOCATION which get a valid
620 result from bp_location_has_shadow. You can use it for roughly
621 limiting the subrange of BP_LOCATION to scan for shadow bytes for
622 an address you need to read. */
623
624 static CORE_ADDR bp_location_placed_address_before_address_max;
625
626 /* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
627 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
628 BP_LOCATION which get a valid result from bp_location_has_shadow.
629 You can use it for roughly limiting the subrange of BP_LOCATION to
630 scan for shadow bytes for an address you need to read. */
631
632 static CORE_ADDR bp_location_shadow_len_after_address_max;
633
634 /* The locations that no longer correspond to any breakpoint, unlinked
635 from bp_location array, but for which a hit may still be reported
636 by a target. */
637 VEC(bp_location_p) *moribund_locations = NULL;
638
639 /* Number of last breakpoint made. */
640
641 static int breakpoint_count;
642
643 /* The value of `breakpoint_count' before the last command that
644 created breakpoints. If the last (break-like) command created more
645 than one breakpoint, then the difference between BREAKPOINT_COUNT
646 and PREV_BREAKPOINT_COUNT is more than one. */
647 static int prev_breakpoint_count;
648
649 /* Number of last tracepoint made. */
650
651 static int tracepoint_count;
652
653 static struct cmd_list_element *breakpoint_set_cmdlist;
654 static struct cmd_list_element *breakpoint_show_cmdlist;
655 struct cmd_list_element *save_cmdlist;
656
657 /* See declaration at breakpoint.h. */
658
659 struct breakpoint *
660 breakpoint_find_if (int (*func) (struct breakpoint *b, void *d),
661 void *user_data)
662 {
663 struct breakpoint *b = NULL;
664
665 ALL_BREAKPOINTS (b)
666 {
667 if (func (b, user_data) != 0)
668 break;
669 }
670
671 return b;
672 }
673
674 /* Return whether a breakpoint is an active enabled breakpoint. */
675 static int
676 breakpoint_enabled (struct breakpoint *b)
677 {
678 return (b->enable_state == bp_enabled);
679 }
680
681 /* Set breakpoint count to NUM. */
682
683 static void
684 set_breakpoint_count (int num)
685 {
686 prev_breakpoint_count = breakpoint_count;
687 breakpoint_count = num;
688 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
689 }
690
691 /* Used by `start_rbreak_breakpoints' below, to record the current
692 breakpoint count before "rbreak" creates any breakpoint. */
693 static int rbreak_start_breakpoint_count;
694
695 /* Called at the start an "rbreak" command to record the first
696 breakpoint made. */
697
698 void
699 start_rbreak_breakpoints (void)
700 {
701 rbreak_start_breakpoint_count = breakpoint_count;
702 }
703
704 /* Called at the end of an "rbreak" command to record the last
705 breakpoint made. */
706
707 void
708 end_rbreak_breakpoints (void)
709 {
710 prev_breakpoint_count = rbreak_start_breakpoint_count;
711 }
712
713 /* Used in run_command to zero the hit count when a new run starts. */
714
715 void
716 clear_breakpoint_hit_counts (void)
717 {
718 struct breakpoint *b;
719
720 ALL_BREAKPOINTS (b)
721 b->hit_count = 0;
722 }
723
724 /* Allocate a new counted_command_line with reference count of 1.
725 The new structure owns COMMANDS. */
726
727 static struct counted_command_line *
728 alloc_counted_command_line (struct command_line *commands)
729 {
730 struct counted_command_line *result = XNEW (struct counted_command_line);
731
732 result->refc = 1;
733 result->commands = commands;
734
735 return result;
736 }
737
738 /* Increment reference count. This does nothing if CMD is NULL. */
739
740 static void
741 incref_counted_command_line (struct counted_command_line *cmd)
742 {
743 if (cmd)
744 ++cmd->refc;
745 }
746
747 /* Decrement reference count. If the reference count reaches 0,
748 destroy the counted_command_line. Sets *CMDP to NULL. This does
749 nothing if *CMDP is NULL. */
750
751 static void
752 decref_counted_command_line (struct counted_command_line **cmdp)
753 {
754 if (*cmdp)
755 {
756 if (--(*cmdp)->refc == 0)
757 {
758 free_command_lines (&(*cmdp)->commands);
759 xfree (*cmdp);
760 }
761 *cmdp = NULL;
762 }
763 }
764
765 /* A cleanup function that calls decref_counted_command_line. */
766
767 static void
768 do_cleanup_counted_command_line (void *arg)
769 {
770 decref_counted_command_line ((struct counted_command_line **) arg);
771 }
772
773 /* Create a cleanup that calls decref_counted_command_line on the
774 argument. */
775
776 static struct cleanup *
777 make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
778 {
779 return make_cleanup (do_cleanup_counted_command_line, cmdp);
780 }
781
782 \f
783 /* Return the breakpoint with the specified number, or NULL
784 if the number does not refer to an existing breakpoint. */
785
786 struct breakpoint *
787 get_breakpoint (int num)
788 {
789 struct breakpoint *b;
790
791 ALL_BREAKPOINTS (b)
792 if (b->number == num)
793 return b;
794
795 return NULL;
796 }
797
798 \f
799
800 /* Mark locations as "conditions have changed" in case the target supports
801 evaluating conditions on its side. */
802
803 static void
804 mark_breakpoint_modified (struct breakpoint *b)
805 {
806 struct bp_location *loc;
807
808 /* This is only meaningful if the target is
809 evaluating conditions and if the user has
810 opted for condition evaluation on the target's
811 side. */
812 if (gdb_evaluates_breakpoint_condition_p ()
813 || !target_supports_evaluation_of_breakpoint_conditions ())
814 return;
815
816 if (!is_breakpoint (b))
817 return;
818
819 for (loc = b->loc; loc; loc = loc->next)
820 loc->condition_changed = condition_modified;
821 }
822
823 /* Mark location as "conditions have changed" in case the target supports
824 evaluating conditions on its side. */
825
826 static void
827 mark_breakpoint_location_modified (struct bp_location *loc)
828 {
829 /* This is only meaningful if the target is
830 evaluating conditions and if the user has
831 opted for condition evaluation on the target's
832 side. */
833 if (gdb_evaluates_breakpoint_condition_p ()
834 || !target_supports_evaluation_of_breakpoint_conditions ())
835
836 return;
837
838 if (!is_breakpoint (loc->owner))
839 return;
840
841 loc->condition_changed = condition_modified;
842 }
843
844 /* Sets the condition-evaluation mode using the static global
845 condition_evaluation_mode. */
846
847 static void
848 set_condition_evaluation_mode (char *args, int from_tty,
849 struct cmd_list_element *c)
850 {
851 const char *old_mode, *new_mode;
852
853 if ((condition_evaluation_mode_1 == condition_evaluation_target)
854 && !target_supports_evaluation_of_breakpoint_conditions ())
855 {
856 condition_evaluation_mode_1 = condition_evaluation_mode;
857 warning (_("Target does not support breakpoint condition evaluation.\n"
858 "Using host evaluation mode instead."));
859 return;
860 }
861
862 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
863 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
864
865 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
866 settings was "auto". */
867 condition_evaluation_mode = condition_evaluation_mode_1;
868
869 /* Only update the mode if the user picked a different one. */
870 if (new_mode != old_mode)
871 {
872 struct bp_location *loc, **loc_tmp;
873 /* If the user switched to a different evaluation mode, we
874 need to synch the changes with the target as follows:
875
876 "host" -> "target": Send all (valid) conditions to the target.
877 "target" -> "host": Remove all the conditions from the target.
878 */
879
880 if (new_mode == condition_evaluation_target)
881 {
882 /* Mark everything modified and synch conditions with the
883 target. */
884 ALL_BP_LOCATIONS (loc, loc_tmp)
885 mark_breakpoint_location_modified (loc);
886 }
887 else
888 {
889 /* Manually mark non-duplicate locations to synch conditions
890 with the target. We do this to remove all the conditions the
891 target knows about. */
892 ALL_BP_LOCATIONS (loc, loc_tmp)
893 if (is_breakpoint (loc->owner) && loc->inserted)
894 loc->needs_update = 1;
895 }
896
897 /* Do the update. */
898 update_global_location_list (UGLL_MAY_INSERT);
899 }
900
901 return;
902 }
903
904 /* Shows the current mode of breakpoint condition evaluation. Explicitly shows
905 what "auto" is translating to. */
906
907 static void
908 show_condition_evaluation_mode (struct ui_file *file, int from_tty,
909 struct cmd_list_element *c, const char *value)
910 {
911 if (condition_evaluation_mode == condition_evaluation_auto)
912 fprintf_filtered (file,
913 _("Breakpoint condition evaluation "
914 "mode is %s (currently %s).\n"),
915 value,
916 breakpoint_condition_evaluation_mode ());
917 else
918 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
919 value);
920 }
921
922 /* A comparison function for bp_location AP and BP that is used by
923 bsearch. This comparison function only cares about addresses, unlike
924 the more general bp_location_compare function. */
925
926 static int
927 bp_location_compare_addrs (const void *ap, const void *bp)
928 {
929 const struct bp_location *a = *(const struct bp_location **) ap;
930 const struct bp_location *b = *(const struct bp_location **) bp;
931
932 if (a->address == b->address)
933 return 0;
934 else
935 return ((a->address > b->address) - (a->address < b->address));
936 }
937
938 /* Helper function to skip all bp_locations with addresses
939 less than ADDRESS. It returns the first bp_location that
940 is greater than or equal to ADDRESS. If none is found, just
941 return NULL. */
942
943 static struct bp_location **
944 get_first_locp_gte_addr (CORE_ADDR address)
945 {
946 struct bp_location dummy_loc;
947 struct bp_location *dummy_locp = &dummy_loc;
948 struct bp_location **locp_found = NULL;
949
950 /* Initialize the dummy location's address field. */
951 memset (&dummy_loc, 0, sizeof (struct bp_location));
952 dummy_loc.address = address;
953
954 /* Find a close match to the first location at ADDRESS. */
955 locp_found = ((struct bp_location **)
956 bsearch (&dummy_locp, bp_location, bp_location_count,
957 sizeof (struct bp_location **),
958 bp_location_compare_addrs));
959
960 /* Nothing was found, nothing left to do. */
961 if (locp_found == NULL)
962 return NULL;
963
964 /* We may have found a location that is at ADDRESS but is not the first in the
965 location's list. Go backwards (if possible) and locate the first one. */
966 while ((locp_found - 1) >= bp_location
967 && (*(locp_found - 1))->address == address)
968 locp_found--;
969
970 return locp_found;
971 }
972
973 void
974 set_breakpoint_condition (struct breakpoint *b, const char *exp,
975 int from_tty)
976 {
977 xfree (b->cond_string);
978 b->cond_string = NULL;
979
980 if (is_watchpoint (b))
981 {
982 struct watchpoint *w = (struct watchpoint *) b;
983
984 w->cond_exp.reset ();
985 }
986 else
987 {
988 struct bp_location *loc;
989
990 for (loc = b->loc; loc; loc = loc->next)
991 {
992 loc->cond.reset ();
993
994 /* No need to free the condition agent expression
995 bytecode (if we have one). We will handle this
996 when we go through update_global_location_list. */
997 }
998 }
999
1000 if (*exp == 0)
1001 {
1002 if (from_tty)
1003 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
1004 }
1005 else
1006 {
1007 const char *arg = exp;
1008
1009 /* I don't know if it matters whether this is the string the user
1010 typed in or the decompiled expression. */
1011 b->cond_string = xstrdup (arg);
1012 b->condition_not_parsed = 0;
1013
1014 if (is_watchpoint (b))
1015 {
1016 struct watchpoint *w = (struct watchpoint *) b;
1017
1018 innermost_block = NULL;
1019 arg = exp;
1020 w->cond_exp = parse_exp_1 (&arg, 0, 0, 0);
1021 if (*arg)
1022 error (_("Junk at end of expression"));
1023 w->cond_exp_valid_block = innermost_block;
1024 }
1025 else
1026 {
1027 struct bp_location *loc;
1028
1029 for (loc = b->loc; loc; loc = loc->next)
1030 {
1031 arg = exp;
1032 loc->cond =
1033 parse_exp_1 (&arg, loc->address,
1034 block_for_pc (loc->address), 0);
1035 if (*arg)
1036 error (_("Junk at end of expression"));
1037 }
1038 }
1039 }
1040 mark_breakpoint_modified (b);
1041
1042 observer_notify_breakpoint_modified (b);
1043 }
1044
1045 /* Completion for the "condition" command. */
1046
1047 static VEC (char_ptr) *
1048 condition_completer (struct cmd_list_element *cmd,
1049 const char *text, const char *word)
1050 {
1051 const char *space;
1052
1053 text = skip_spaces_const (text);
1054 space = skip_to_space_const (text);
1055 if (*space == '\0')
1056 {
1057 int len;
1058 struct breakpoint *b;
1059 VEC (char_ptr) *result = NULL;
1060
1061 if (text[0] == '$')
1062 {
1063 /* We don't support completion of history indices. */
1064 if (isdigit (text[1]))
1065 return NULL;
1066 return complete_internalvar (&text[1]);
1067 }
1068
1069 /* We're completing the breakpoint number. */
1070 len = strlen (text);
1071
1072 ALL_BREAKPOINTS (b)
1073 {
1074 char number[50];
1075
1076 xsnprintf (number, sizeof (number), "%d", b->number);
1077
1078 if (strncmp (number, text, len) == 0)
1079 VEC_safe_push (char_ptr, result, xstrdup (number));
1080 }
1081
1082 return result;
1083 }
1084
1085 /* We're completing the expression part. */
1086 text = skip_spaces_const (space);
1087 return expression_completer (cmd, text, word);
1088 }
1089
1090 /* condition N EXP -- set break condition of breakpoint N to EXP. */
1091
1092 static void
1093 condition_command (char *arg, int from_tty)
1094 {
1095 struct breakpoint *b;
1096 char *p;
1097 int bnum;
1098
1099 if (arg == 0)
1100 error_no_arg (_("breakpoint number"));
1101
1102 p = arg;
1103 bnum = get_number (&p);
1104 if (bnum == 0)
1105 error (_("Bad breakpoint argument: '%s'"), arg);
1106
1107 ALL_BREAKPOINTS (b)
1108 if (b->number == bnum)
1109 {
1110 /* Check if this breakpoint has a "stop" method implemented in an
1111 extension language. This method and conditions entered into GDB
1112 from the CLI are mutually exclusive. */
1113 const struct extension_language_defn *extlang
1114 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1115
1116 if (extlang != NULL)
1117 {
1118 error (_("Only one stop condition allowed. There is currently"
1119 " a %s stop condition defined for this breakpoint."),
1120 ext_lang_capitalized_name (extlang));
1121 }
1122 set_breakpoint_condition (b, p, from_tty);
1123
1124 if (is_breakpoint (b))
1125 update_global_location_list (UGLL_MAY_INSERT);
1126
1127 return;
1128 }
1129
1130 error (_("No breakpoint number %d."), bnum);
1131 }
1132
1133 /* Check that COMMAND do not contain commands that are suitable
1134 only for tracepoints and not suitable for ordinary breakpoints.
1135 Throw if any such commands is found. */
1136
1137 static void
1138 check_no_tracepoint_commands (struct command_line *commands)
1139 {
1140 struct command_line *c;
1141
1142 for (c = commands; c; c = c->next)
1143 {
1144 int i;
1145
1146 if (c->control_type == while_stepping_control)
1147 error (_("The 'while-stepping' command can "
1148 "only be used for tracepoints"));
1149
1150 for (i = 0; i < c->body_count; ++i)
1151 check_no_tracepoint_commands ((c->body_list)[i]);
1152
1153 /* Not that command parsing removes leading whitespace and comment
1154 lines and also empty lines. So, we only need to check for
1155 command directly. */
1156 if (strstr (c->line, "collect ") == c->line)
1157 error (_("The 'collect' command can only be used for tracepoints"));
1158
1159 if (strstr (c->line, "teval ") == c->line)
1160 error (_("The 'teval' command can only be used for tracepoints"));
1161 }
1162 }
1163
1164 /* Encapsulate tests for different types of tracepoints. */
1165
1166 static int
1167 is_tracepoint_type (enum bptype type)
1168 {
1169 return (type == bp_tracepoint
1170 || type == bp_fast_tracepoint
1171 || type == bp_static_tracepoint);
1172 }
1173
1174 int
1175 is_tracepoint (const struct breakpoint *b)
1176 {
1177 return is_tracepoint_type (b->type);
1178 }
1179
1180 /* A helper function that validates that COMMANDS are valid for a
1181 breakpoint. This function will throw an exception if a problem is
1182 found. */
1183
1184 static void
1185 validate_commands_for_breakpoint (struct breakpoint *b,
1186 struct command_line *commands)
1187 {
1188 if (is_tracepoint (b))
1189 {
1190 struct tracepoint *t = (struct tracepoint *) b;
1191 struct command_line *c;
1192 struct command_line *while_stepping = 0;
1193
1194 /* Reset the while-stepping step count. The previous commands
1195 might have included a while-stepping action, while the new
1196 ones might not. */
1197 t->step_count = 0;
1198
1199 /* We need to verify that each top-level element of commands is
1200 valid for tracepoints, that there's at most one
1201 while-stepping element, and that the while-stepping's body
1202 has valid tracing commands excluding nested while-stepping.
1203 We also need to validate the tracepoint action line in the
1204 context of the tracepoint --- validate_actionline actually
1205 has side effects, like setting the tracepoint's
1206 while-stepping STEP_COUNT, in addition to checking if the
1207 collect/teval actions parse and make sense in the
1208 tracepoint's context. */
1209 for (c = commands; c; c = c->next)
1210 {
1211 if (c->control_type == while_stepping_control)
1212 {
1213 if (b->type == bp_fast_tracepoint)
1214 error (_("The 'while-stepping' command "
1215 "cannot be used for fast tracepoint"));
1216 else if (b->type == bp_static_tracepoint)
1217 error (_("The 'while-stepping' command "
1218 "cannot be used for static tracepoint"));
1219
1220 if (while_stepping)
1221 error (_("The 'while-stepping' command "
1222 "can be used only once"));
1223 else
1224 while_stepping = c;
1225 }
1226
1227 validate_actionline (c->line, b);
1228 }
1229 if (while_stepping)
1230 {
1231 struct command_line *c2;
1232
1233 gdb_assert (while_stepping->body_count == 1);
1234 c2 = while_stepping->body_list[0];
1235 for (; c2; c2 = c2->next)
1236 {
1237 if (c2->control_type == while_stepping_control)
1238 error (_("The 'while-stepping' command cannot be nested"));
1239 }
1240 }
1241 }
1242 else
1243 {
1244 check_no_tracepoint_commands (commands);
1245 }
1246 }
1247
1248 /* Return a vector of all the static tracepoints set at ADDR. The
1249 caller is responsible for releasing the vector. */
1250
1251 VEC(breakpoint_p) *
1252 static_tracepoints_here (CORE_ADDR addr)
1253 {
1254 struct breakpoint *b;
1255 VEC(breakpoint_p) *found = 0;
1256 struct bp_location *loc;
1257
1258 ALL_BREAKPOINTS (b)
1259 if (b->type == bp_static_tracepoint)
1260 {
1261 for (loc = b->loc; loc; loc = loc->next)
1262 if (loc->address == addr)
1263 VEC_safe_push(breakpoint_p, found, b);
1264 }
1265
1266 return found;
1267 }
1268
1269 /* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
1270 validate that only allowed commands are included. */
1271
1272 void
1273 breakpoint_set_commands (struct breakpoint *b,
1274 struct command_line *commands)
1275 {
1276 validate_commands_for_breakpoint (b, commands);
1277
1278 decref_counted_command_line (&b->commands);
1279 b->commands = alloc_counted_command_line (commands);
1280 observer_notify_breakpoint_modified (b);
1281 }
1282
1283 /* Set the internal `silent' flag on the breakpoint. Note that this
1284 is not the same as the "silent" that may appear in the breakpoint's
1285 commands. */
1286
1287 void
1288 breakpoint_set_silent (struct breakpoint *b, int silent)
1289 {
1290 int old_silent = b->silent;
1291
1292 b->silent = silent;
1293 if (old_silent != silent)
1294 observer_notify_breakpoint_modified (b);
1295 }
1296
1297 /* Set the thread for this breakpoint. If THREAD is -1, make the
1298 breakpoint work for any thread. */
1299
1300 void
1301 breakpoint_set_thread (struct breakpoint *b, int thread)
1302 {
1303 int old_thread = b->thread;
1304
1305 b->thread = thread;
1306 if (old_thread != thread)
1307 observer_notify_breakpoint_modified (b);
1308 }
1309
1310 /* Set the task for this breakpoint. If TASK is 0, make the
1311 breakpoint work for any task. */
1312
1313 void
1314 breakpoint_set_task (struct breakpoint *b, int task)
1315 {
1316 int old_task = b->task;
1317
1318 b->task = task;
1319 if (old_task != task)
1320 observer_notify_breakpoint_modified (b);
1321 }
1322
1323 void
1324 check_tracepoint_command (char *line, void *closure)
1325 {
1326 struct breakpoint *b = (struct breakpoint *) closure;
1327
1328 validate_actionline (line, b);
1329 }
1330
1331 /* A structure used to pass information through
1332 map_breakpoint_numbers. */
1333
1334 struct commands_info
1335 {
1336 /* True if the command was typed at a tty. */
1337 int from_tty;
1338
1339 /* The breakpoint range spec. */
1340 const char *arg;
1341
1342 /* Non-NULL if the body of the commands are being read from this
1343 already-parsed command. */
1344 struct command_line *control;
1345
1346 /* The command lines read from the user, or NULL if they have not
1347 yet been read. */
1348 struct counted_command_line *cmd;
1349 };
1350
1351 /* A callback for map_breakpoint_numbers that sets the commands for
1352 commands_command. */
1353
1354 static void
1355 do_map_commands_command (struct breakpoint *b, void *data)
1356 {
1357 struct commands_info *info = (struct commands_info *) data;
1358
1359 if (info->cmd == NULL)
1360 {
1361 struct command_line *l;
1362
1363 if (info->control != NULL)
1364 l = copy_command_lines (info->control->body_list[0]);
1365 else
1366 {
1367 struct cleanup *old_chain;
1368 char *str;
1369
1370 str = xstrprintf (_("Type commands for breakpoint(s) "
1371 "%s, one per line."),
1372 info->arg);
1373
1374 old_chain = make_cleanup (xfree, str);
1375
1376 l = read_command_lines (str,
1377 info->from_tty, 1,
1378 (is_tracepoint (b)
1379 ? check_tracepoint_command : 0),
1380 b);
1381
1382 do_cleanups (old_chain);
1383 }
1384
1385 info->cmd = alloc_counted_command_line (l);
1386 }
1387
1388 /* If a breakpoint was on the list more than once, we don't need to
1389 do anything. */
1390 if (b->commands != info->cmd)
1391 {
1392 validate_commands_for_breakpoint (b, info->cmd->commands);
1393 incref_counted_command_line (info->cmd);
1394 decref_counted_command_line (&b->commands);
1395 b->commands = info->cmd;
1396 observer_notify_breakpoint_modified (b);
1397 }
1398 }
1399
1400 static void
1401 commands_command_1 (const char *arg, int from_tty,
1402 struct command_line *control)
1403 {
1404 struct cleanup *cleanups;
1405 struct commands_info info;
1406
1407 info.from_tty = from_tty;
1408 info.control = control;
1409 info.cmd = NULL;
1410 /* If we read command lines from the user, then `info' will hold an
1411 extra reference to the commands that we must clean up. */
1412 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
1413
1414 std::string new_arg;
1415
1416 if (arg == NULL || !*arg)
1417 {
1418 if (breakpoint_count - prev_breakpoint_count > 1)
1419 new_arg = string_printf ("%d-%d", prev_breakpoint_count + 1,
1420 breakpoint_count);
1421 else if (breakpoint_count > 0)
1422 new_arg = string_printf ("%d", breakpoint_count);
1423 }
1424 else
1425 new_arg = arg;
1426
1427 info.arg = new_arg.c_str ();
1428
1429 map_breakpoint_numbers (info.arg, do_map_commands_command, &info);
1430
1431 if (info.cmd == NULL)
1432 error (_("No breakpoints specified."));
1433
1434 do_cleanups (cleanups);
1435 }
1436
1437 static void
1438 commands_command (char *arg, int from_tty)
1439 {
1440 commands_command_1 (arg, from_tty, NULL);
1441 }
1442
1443 /* Like commands_command, but instead of reading the commands from
1444 input stream, takes them from an already parsed command structure.
1445
1446 This is used by cli-script.c to DTRT with breakpoint commands
1447 that are part of if and while bodies. */
1448 enum command_control_type
1449 commands_from_control_command (const char *arg, struct command_line *cmd)
1450 {
1451 commands_command_1 (arg, 0, cmd);
1452 return simple_control;
1453 }
1454
1455 /* Return non-zero if BL->TARGET_INFO contains valid information. */
1456
1457 static int
1458 bp_location_has_shadow (struct bp_location *bl)
1459 {
1460 if (bl->loc_type != bp_loc_software_breakpoint)
1461 return 0;
1462 if (!bl->inserted)
1463 return 0;
1464 if (bl->target_info.shadow_len == 0)
1465 /* BL isn't valid, or doesn't shadow memory. */
1466 return 0;
1467 return 1;
1468 }
1469
1470 /* Update BUF, which is LEN bytes read from the target address
1471 MEMADDR, by replacing a memory breakpoint with its shadowed
1472 contents.
1473
1474 If READBUF is not NULL, this buffer must not overlap with the of
1475 the breakpoint location's shadow_contents buffer. Otherwise, a
1476 failed assertion internal error will be raised. */
1477
1478 static void
1479 one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1480 const gdb_byte *writebuf_org,
1481 ULONGEST memaddr, LONGEST len,
1482 struct bp_target_info *target_info,
1483 struct gdbarch *gdbarch)
1484 {
1485 /* Now do full processing of the found relevant range of elements. */
1486 CORE_ADDR bp_addr = 0;
1487 int bp_size = 0;
1488 int bptoffset = 0;
1489
1490 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1491 current_program_space->aspace, 0))
1492 {
1493 /* The breakpoint is inserted in a different address space. */
1494 return;
1495 }
1496
1497 /* Addresses and length of the part of the breakpoint that
1498 we need to copy. */
1499 bp_addr = target_info->placed_address;
1500 bp_size = target_info->shadow_len;
1501
1502 if (bp_addr + bp_size <= memaddr)
1503 {
1504 /* The breakpoint is entirely before the chunk of memory we are
1505 reading. */
1506 return;
1507 }
1508
1509 if (bp_addr >= memaddr + len)
1510 {
1511 /* The breakpoint is entirely after the chunk of memory we are
1512 reading. */
1513 return;
1514 }
1515
1516 /* Offset within shadow_contents. */
1517 if (bp_addr < memaddr)
1518 {
1519 /* Only copy the second part of the breakpoint. */
1520 bp_size -= memaddr - bp_addr;
1521 bptoffset = memaddr - bp_addr;
1522 bp_addr = memaddr;
1523 }
1524
1525 if (bp_addr + bp_size > memaddr + len)
1526 {
1527 /* Only copy the first part of the breakpoint. */
1528 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1529 }
1530
1531 if (readbuf != NULL)
1532 {
1533 /* Verify that the readbuf buffer does not overlap with the
1534 shadow_contents buffer. */
1535 gdb_assert (target_info->shadow_contents >= readbuf + len
1536 || readbuf >= (target_info->shadow_contents
1537 + target_info->shadow_len));
1538
1539 /* Update the read buffer with this inserted breakpoint's
1540 shadow. */
1541 memcpy (readbuf + bp_addr - memaddr,
1542 target_info->shadow_contents + bptoffset, bp_size);
1543 }
1544 else
1545 {
1546 const unsigned char *bp;
1547 CORE_ADDR addr = target_info->reqstd_address;
1548 int placed_size;
1549
1550 /* Update the shadow with what we want to write to memory. */
1551 memcpy (target_info->shadow_contents + bptoffset,
1552 writebuf_org + bp_addr - memaddr, bp_size);
1553
1554 /* Determine appropriate breakpoint contents and size for this
1555 address. */
1556 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
1557
1558 /* Update the final write buffer with this inserted
1559 breakpoint's INSN. */
1560 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1561 }
1562 }
1563
1564 /* Update BUF, which is LEN bytes read from the target address MEMADDR,
1565 by replacing any memory breakpoints with their shadowed contents.
1566
1567 If READBUF is not NULL, this buffer must not overlap with any of
1568 the breakpoint location's shadow_contents buffers. Otherwise,
1569 a failed assertion internal error will be raised.
1570
1571 The range of shadowed area by each bp_location is:
1572 bl->address - bp_location_placed_address_before_address_max
1573 up to bl->address + bp_location_shadow_len_after_address_max
1574 The range we were requested to resolve shadows for is:
1575 memaddr ... memaddr + len
1576 Thus the safe cutoff boundaries for performance optimization are
1577 memaddr + len <= (bl->address
1578 - bp_location_placed_address_before_address_max)
1579 and:
1580 bl->address + bp_location_shadow_len_after_address_max <= memaddr */
1581
1582 void
1583 breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1584 const gdb_byte *writebuf_org,
1585 ULONGEST memaddr, LONGEST len)
1586 {
1587 /* Left boundary, right boundary and median element of our binary
1588 search. */
1589 unsigned bc_l, bc_r, bc;
1590
1591 /* Find BC_L which is a leftmost element which may affect BUF
1592 content. It is safe to report lower value but a failure to
1593 report higher one. */
1594
1595 bc_l = 0;
1596 bc_r = bp_location_count;
1597 while (bc_l + 1 < bc_r)
1598 {
1599 struct bp_location *bl;
1600
1601 bc = (bc_l + bc_r) / 2;
1602 bl = bp_location[bc];
1603
1604 /* Check first BL->ADDRESS will not overflow due to the added
1605 constant. Then advance the left boundary only if we are sure
1606 the BC element can in no way affect the BUF content (MEMADDR
1607 to MEMADDR + LEN range).
1608
1609 Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1610 offset so that we cannot miss a breakpoint with its shadow
1611 range tail still reaching MEMADDR. */
1612
1613 if ((bl->address + bp_location_shadow_len_after_address_max
1614 >= bl->address)
1615 && (bl->address + bp_location_shadow_len_after_address_max
1616 <= memaddr))
1617 bc_l = bc;
1618 else
1619 bc_r = bc;
1620 }
1621
1622 /* Due to the binary search above, we need to make sure we pick the
1623 first location that's at BC_L's address. E.g., if there are
1624 multiple locations at the same address, BC_L may end up pointing
1625 at a duplicate location, and miss the "master"/"inserted"
1626 location. Say, given locations L1, L2 and L3 at addresses A and
1627 B:
1628
1629 L1@A, L2@A, L3@B, ...
1630
1631 BC_L could end up pointing at location L2, while the "master"
1632 location could be L1. Since the `loc->inserted' flag is only set
1633 on "master" locations, we'd forget to restore the shadow of L1
1634 and L2. */
1635 while (bc_l > 0
1636 && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1637 bc_l--;
1638
1639 /* Now do full processing of the found relevant range of elements. */
1640
1641 for (bc = bc_l; bc < bp_location_count; bc++)
1642 {
1643 struct bp_location *bl = bp_location[bc];
1644
1645 /* bp_location array has BL->OWNER always non-NULL. */
1646 if (bl->owner->type == bp_none)
1647 warning (_("reading through apparently deleted breakpoint #%d?"),
1648 bl->owner->number);
1649
1650 /* Performance optimization: any further element can no longer affect BUF
1651 content. */
1652
1653 if (bl->address >= bp_location_placed_address_before_address_max
1654 && memaddr + len <= (bl->address
1655 - bp_location_placed_address_before_address_max))
1656 break;
1657
1658 if (!bp_location_has_shadow (bl))
1659 continue;
1660
1661 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1662 memaddr, len, &bl->target_info, bl->gdbarch);
1663 }
1664 }
1665
1666 \f
1667
1668 /* Return true if BPT is either a software breakpoint or a hardware
1669 breakpoint. */
1670
1671 int
1672 is_breakpoint (const struct breakpoint *bpt)
1673 {
1674 return (bpt->type == bp_breakpoint
1675 || bpt->type == bp_hardware_breakpoint
1676 || bpt->type == bp_dprintf);
1677 }
1678
1679 /* Return true if BPT is of any hardware watchpoint kind. */
1680
1681 static int
1682 is_hardware_watchpoint (const struct breakpoint *bpt)
1683 {
1684 return (bpt->type == bp_hardware_watchpoint
1685 || bpt->type == bp_read_watchpoint
1686 || bpt->type == bp_access_watchpoint);
1687 }
1688
1689 /* Return true if BPT is of any watchpoint kind, hardware or
1690 software. */
1691
1692 int
1693 is_watchpoint (const struct breakpoint *bpt)
1694 {
1695 return (is_hardware_watchpoint (bpt)
1696 || bpt->type == bp_watchpoint);
1697 }
1698
1699 /* Returns true if the current thread and its running state are safe
1700 to evaluate or update watchpoint B. Watchpoints on local
1701 expressions need to be evaluated in the context of the thread that
1702 was current when the watchpoint was created, and, that thread needs
1703 to be stopped to be able to select the correct frame context.
1704 Watchpoints on global expressions can be evaluated on any thread,
1705 and in any state. It is presently left to the target allowing
1706 memory accesses when threads are running. */
1707
1708 static int
1709 watchpoint_in_thread_scope (struct watchpoint *b)
1710 {
1711 return (b->base.pspace == current_program_space
1712 && (ptid_equal (b->watchpoint_thread, null_ptid)
1713 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1714 && !is_executing (inferior_ptid))));
1715 }
1716
1717 /* Set watchpoint B to disp_del_at_next_stop, even including its possible
1718 associated bp_watchpoint_scope breakpoint. */
1719
1720 static void
1721 watchpoint_del_at_next_stop (struct watchpoint *w)
1722 {
1723 struct breakpoint *b = &w->base;
1724
1725 if (b->related_breakpoint != b)
1726 {
1727 gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1728 gdb_assert (b->related_breakpoint->related_breakpoint == b);
1729 b->related_breakpoint->disposition = disp_del_at_next_stop;
1730 b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1731 b->related_breakpoint = b;
1732 }
1733 b->disposition = disp_del_at_next_stop;
1734 }
1735
1736 /* Extract a bitfield value from value VAL using the bit parameters contained in
1737 watchpoint W. */
1738
1739 static struct value *
1740 extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1741 {
1742 struct value *bit_val;
1743
1744 if (val == NULL)
1745 return NULL;
1746
1747 bit_val = allocate_value (value_type (val));
1748
1749 unpack_value_bitfield (bit_val,
1750 w->val_bitpos,
1751 w->val_bitsize,
1752 value_contents_for_printing (val),
1753 value_offset (val),
1754 val);
1755
1756 return bit_val;
1757 }
1758
1759 /* Allocate a dummy location and add it to B, which must be a software
1760 watchpoint. This is required because even if a software watchpoint
1761 is not watching any memory, bpstat_stop_status requires a location
1762 to be able to report stops. */
1763
1764 static void
1765 software_watchpoint_add_no_memory_location (struct breakpoint *b,
1766 struct program_space *pspace)
1767 {
1768 gdb_assert (b->type == bp_watchpoint && b->loc == NULL);
1769
1770 b->loc = allocate_bp_location (b);
1771 b->loc->pspace = pspace;
1772 b->loc->address = -1;
1773 b->loc->length = -1;
1774 }
1775
1776 /* Returns true if B is a software watchpoint that is not watching any
1777 memory (e.g., "watch $pc"). */
1778
1779 static int
1780 is_no_memory_software_watchpoint (struct breakpoint *b)
1781 {
1782 return (b->type == bp_watchpoint
1783 && b->loc != NULL
1784 && b->loc->next == NULL
1785 && b->loc->address == -1
1786 && b->loc->length == -1);
1787 }
1788
1789 /* Assuming that B is a watchpoint:
1790 - Reparse watchpoint expression, if REPARSE is non-zero
1791 - Evaluate expression and store the result in B->val
1792 - Evaluate the condition if there is one, and store the result
1793 in b->loc->cond.
1794 - Update the list of values that must be watched in B->loc.
1795
1796 If the watchpoint disposition is disp_del_at_next_stop, then do
1797 nothing. If this is local watchpoint that is out of scope, delete
1798 it.
1799
1800 Even with `set breakpoint always-inserted on' the watchpoints are
1801 removed + inserted on each stop here. Normal breakpoints must
1802 never be removed because they might be missed by a running thread
1803 when debugging in non-stop mode. On the other hand, hardware
1804 watchpoints (is_hardware_watchpoint; processed here) are specific
1805 to each LWP since they are stored in each LWP's hardware debug
1806 registers. Therefore, such LWP must be stopped first in order to
1807 be able to modify its hardware watchpoints.
1808
1809 Hardware watchpoints must be reset exactly once after being
1810 presented to the user. It cannot be done sooner, because it would
1811 reset the data used to present the watchpoint hit to the user. And
1812 it must not be done later because it could display the same single
1813 watchpoint hit during multiple GDB stops. Note that the latter is
1814 relevant only to the hardware watchpoint types bp_read_watchpoint
1815 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1816 not user-visible - its hit is suppressed if the memory content has
1817 not changed.
1818
1819 The following constraints influence the location where we can reset
1820 hardware watchpoints:
1821
1822 * target_stopped_by_watchpoint and target_stopped_data_address are
1823 called several times when GDB stops.
1824
1825 [linux]
1826 * Multiple hardware watchpoints can be hit at the same time,
1827 causing GDB to stop. GDB only presents one hardware watchpoint
1828 hit at a time as the reason for stopping, and all the other hits
1829 are presented later, one after the other, each time the user
1830 requests the execution to be resumed. Execution is not resumed
1831 for the threads still having pending hit event stored in
1832 LWP_INFO->STATUS. While the watchpoint is already removed from
1833 the inferior on the first stop the thread hit event is kept being
1834 reported from its cached value by linux_nat_stopped_data_address
1835 until the real thread resume happens after the watchpoint gets
1836 presented and thus its LWP_INFO->STATUS gets reset.
1837
1838 Therefore the hardware watchpoint hit can get safely reset on the
1839 watchpoint removal from inferior. */
1840
1841 static void
1842 update_watchpoint (struct watchpoint *b, int reparse)
1843 {
1844 int within_current_scope;
1845 struct frame_id saved_frame_id;
1846 int frame_saved;
1847
1848 /* If this is a local watchpoint, we only want to check if the
1849 watchpoint frame is in scope if the current thread is the thread
1850 that was used to create the watchpoint. */
1851 if (!watchpoint_in_thread_scope (b))
1852 return;
1853
1854 if (b->base.disposition == disp_del_at_next_stop)
1855 return;
1856
1857 frame_saved = 0;
1858
1859 /* Determine if the watchpoint is within scope. */
1860 if (b->exp_valid_block == NULL)
1861 within_current_scope = 1;
1862 else
1863 {
1864 struct frame_info *fi = get_current_frame ();
1865 struct gdbarch *frame_arch = get_frame_arch (fi);
1866 CORE_ADDR frame_pc = get_frame_pc (fi);
1867
1868 /* If we're at a point where the stack has been destroyed
1869 (e.g. in a function epilogue), unwinding may not work
1870 properly. Do not attempt to recreate locations at this
1871 point. See similar comments in watchpoint_check. */
1872 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
1873 return;
1874
1875 /* Save the current frame's ID so we can restore it after
1876 evaluating the watchpoint expression on its own frame. */
1877 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1878 took a frame parameter, so that we didn't have to change the
1879 selected frame. */
1880 frame_saved = 1;
1881 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1882
1883 fi = frame_find_by_id (b->watchpoint_frame);
1884 within_current_scope = (fi != NULL);
1885 if (within_current_scope)
1886 select_frame (fi);
1887 }
1888
1889 /* We don't free locations. They are stored in the bp_location array
1890 and update_global_location_list will eventually delete them and
1891 remove breakpoints if needed. */
1892 b->base.loc = NULL;
1893
1894 if (within_current_scope && reparse)
1895 {
1896 const char *s;
1897
1898 b->exp.reset ();
1899 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1900 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
1901 /* If the meaning of expression itself changed, the old value is
1902 no longer relevant. We don't want to report a watchpoint hit
1903 to the user when the old value and the new value may actually
1904 be completely different objects. */
1905 value_free (b->val);
1906 b->val = NULL;
1907 b->val_valid = 0;
1908
1909 /* Note that unlike with breakpoints, the watchpoint's condition
1910 expression is stored in the breakpoint object, not in the
1911 locations (re)created below. */
1912 if (b->base.cond_string != NULL)
1913 {
1914 b->cond_exp.reset ();
1915
1916 s = b->base.cond_string;
1917 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
1918 }
1919 }
1920
1921 /* If we failed to parse the expression, for example because
1922 it refers to a global variable in a not-yet-loaded shared library,
1923 don't try to insert watchpoint. We don't automatically delete
1924 such watchpoint, though, since failure to parse expression
1925 is different from out-of-scope watchpoint. */
1926 if (!target_has_execution)
1927 {
1928 /* Without execution, memory can't change. No use to try and
1929 set watchpoint locations. The watchpoint will be reset when
1930 the target gains execution, through breakpoint_re_set. */
1931 if (!can_use_hw_watchpoints)
1932 {
1933 if (b->base.ops->works_in_software_mode (&b->base))
1934 b->base.type = bp_watchpoint;
1935 else
1936 error (_("Can't set read/access watchpoint when "
1937 "hardware watchpoints are disabled."));
1938 }
1939 }
1940 else if (within_current_scope && b->exp)
1941 {
1942 int pc = 0;
1943 struct value *val_chain, *v, *result, *next;
1944 struct program_space *frame_pspace;
1945
1946 fetch_subexp_value (b->exp.get (), &pc, &v, &result, &val_chain, 0);
1947
1948 /* Avoid setting b->val if it's already set. The meaning of
1949 b->val is 'the last value' user saw, and we should update
1950 it only if we reported that last value to user. As it
1951 happens, the code that reports it updates b->val directly.
1952 We don't keep track of the memory value for masked
1953 watchpoints. */
1954 if (!b->val_valid && !is_masked_watchpoint (&b->base))
1955 {
1956 if (b->val_bitsize != 0)
1957 {
1958 v = extract_bitfield_from_watchpoint_value (b, v);
1959 if (v != NULL)
1960 release_value (v);
1961 }
1962 b->val = v;
1963 b->val_valid = 1;
1964 }
1965
1966 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1967
1968 /* Look at each value on the value chain. */
1969 for (v = val_chain; v; v = value_next (v))
1970 {
1971 /* If it's a memory location, and GDB actually needed
1972 its contents to evaluate the expression, then we
1973 must watch it. If the first value returned is
1974 still lazy, that means an error occurred reading it;
1975 watch it anyway in case it becomes readable. */
1976 if (VALUE_LVAL (v) == lval_memory
1977 && (v == val_chain || ! value_lazy (v)))
1978 {
1979 struct type *vtype = check_typedef (value_type (v));
1980
1981 /* We only watch structs and arrays if user asked
1982 for it explicitly, never if they just happen to
1983 appear in the middle of some value chain. */
1984 if (v == result
1985 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1986 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1987 {
1988 CORE_ADDR addr;
1989 enum target_hw_bp_type type;
1990 struct bp_location *loc, **tmp;
1991 int bitpos = 0, bitsize = 0;
1992
1993 if (value_bitsize (v) != 0)
1994 {
1995 /* Extract the bit parameters out from the bitfield
1996 sub-expression. */
1997 bitpos = value_bitpos (v);
1998 bitsize = value_bitsize (v);
1999 }
2000 else if (v == result && b->val_bitsize != 0)
2001 {
2002 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
2003 lvalue whose bit parameters are saved in the fields
2004 VAL_BITPOS and VAL_BITSIZE. */
2005 bitpos = b->val_bitpos;
2006 bitsize = b->val_bitsize;
2007 }
2008
2009 addr = value_address (v);
2010 if (bitsize != 0)
2011 {
2012 /* Skip the bytes that don't contain the bitfield. */
2013 addr += bitpos / 8;
2014 }
2015
2016 type = hw_write;
2017 if (b->base.type == bp_read_watchpoint)
2018 type = hw_read;
2019 else if (b->base.type == bp_access_watchpoint)
2020 type = hw_access;
2021
2022 loc = allocate_bp_location (&b->base);
2023 for (tmp = &(b->base.loc); *tmp != NULL; tmp = &((*tmp)->next))
2024 ;
2025 *tmp = loc;
2026 loc->gdbarch = get_type_arch (value_type (v));
2027
2028 loc->pspace = frame_pspace;
2029 loc->address = addr;
2030
2031 if (bitsize != 0)
2032 {
2033 /* Just cover the bytes that make up the bitfield. */
2034 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2035 }
2036 else
2037 loc->length = TYPE_LENGTH (value_type (v));
2038
2039 loc->watchpoint_type = type;
2040 }
2041 }
2042 }
2043
2044 /* Change the type of breakpoint between hardware assisted or
2045 an ordinary watchpoint depending on the hardware support
2046 and free hardware slots. REPARSE is set when the inferior
2047 is started. */
2048 if (reparse)
2049 {
2050 int reg_cnt;
2051 enum bp_loc_type loc_type;
2052 struct bp_location *bl;
2053
2054 reg_cnt = can_use_hardware_watchpoint (val_chain);
2055
2056 if (reg_cnt)
2057 {
2058 int i, target_resources_ok, other_type_used;
2059 enum bptype type;
2060
2061 /* Use an exact watchpoint when there's only one memory region to be
2062 watched, and only one debug register is needed to watch it. */
2063 b->exact = target_exact_watchpoints && reg_cnt == 1;
2064
2065 /* We need to determine how many resources are already
2066 used for all other hardware watchpoints plus this one
2067 to see if we still have enough resources to also fit
2068 this watchpoint in as well. */
2069
2070 /* If this is a software watchpoint, we try to turn it
2071 to a hardware one -- count resources as if B was of
2072 hardware watchpoint type. */
2073 type = b->base.type;
2074 if (type == bp_watchpoint)
2075 type = bp_hardware_watchpoint;
2076
2077 /* This watchpoint may or may not have been placed on
2078 the list yet at this point (it won't be in the list
2079 if we're trying to create it for the first time,
2080 through watch_command), so always account for it
2081 manually. */
2082
2083 /* Count resources used by all watchpoints except B. */
2084 i = hw_watchpoint_used_count_others (&b->base, type, &other_type_used);
2085
2086 /* Add in the resources needed for B. */
2087 i += hw_watchpoint_use_count (&b->base);
2088
2089 target_resources_ok
2090 = target_can_use_hardware_watchpoint (type, i, other_type_used);
2091 if (target_resources_ok <= 0)
2092 {
2093 int sw_mode = b->base.ops->works_in_software_mode (&b->base);
2094
2095 if (target_resources_ok == 0 && !sw_mode)
2096 error (_("Target does not support this type of "
2097 "hardware watchpoint."));
2098 else if (target_resources_ok < 0 && !sw_mode)
2099 error (_("There are not enough available hardware "
2100 "resources for this watchpoint."));
2101
2102 /* Downgrade to software watchpoint. */
2103 b->base.type = bp_watchpoint;
2104 }
2105 else
2106 {
2107 /* If this was a software watchpoint, we've just
2108 found we have enough resources to turn it to a
2109 hardware watchpoint. Otherwise, this is a
2110 nop. */
2111 b->base.type = type;
2112 }
2113 }
2114 else if (!b->base.ops->works_in_software_mode (&b->base))
2115 {
2116 if (!can_use_hw_watchpoints)
2117 error (_("Can't set read/access watchpoint when "
2118 "hardware watchpoints are disabled."));
2119 else
2120 error (_("Expression cannot be implemented with "
2121 "read/access watchpoint."));
2122 }
2123 else
2124 b->base.type = bp_watchpoint;
2125
2126 loc_type = (b->base.type == bp_watchpoint? bp_loc_other
2127 : bp_loc_hardware_watchpoint);
2128 for (bl = b->base.loc; bl; bl = bl->next)
2129 bl->loc_type = loc_type;
2130 }
2131
2132 for (v = val_chain; v; v = next)
2133 {
2134 next = value_next (v);
2135 if (v != b->val)
2136 value_free (v);
2137 }
2138
2139 /* If a software watchpoint is not watching any memory, then the
2140 above left it without any location set up. But,
2141 bpstat_stop_status requires a location to be able to report
2142 stops, so make sure there's at least a dummy one. */
2143 if (b->base.type == bp_watchpoint && b->base.loc == NULL)
2144 software_watchpoint_add_no_memory_location (&b->base, frame_pspace);
2145 }
2146 else if (!within_current_scope)
2147 {
2148 printf_filtered (_("\
2149 Watchpoint %d deleted because the program has left the block\n\
2150 in which its expression is valid.\n"),
2151 b->base.number);
2152 watchpoint_del_at_next_stop (b);
2153 }
2154
2155 /* Restore the selected frame. */
2156 if (frame_saved)
2157 select_frame (frame_find_by_id (saved_frame_id));
2158 }
2159
2160
2161 /* Returns 1 iff breakpoint location should be
2162 inserted in the inferior. We don't differentiate the type of BL's owner
2163 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2164 breakpoint_ops is not defined, because in insert_bp_location,
2165 tracepoint's insert_location will not be called. */
2166 static int
2167 should_be_inserted (struct bp_location *bl)
2168 {
2169 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
2170 return 0;
2171
2172 if (bl->owner->disposition == disp_del_at_next_stop)
2173 return 0;
2174
2175 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
2176 return 0;
2177
2178 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2179 return 0;
2180
2181 /* This is set for example, when we're attached to the parent of a
2182 vfork, and have detached from the child. The child is running
2183 free, and we expect it to do an exec or exit, at which point the
2184 OS makes the parent schedulable again (and the target reports
2185 that the vfork is done). Until the child is done with the shared
2186 memory region, do not insert breakpoints in the parent, otherwise
2187 the child could still trip on the parent's breakpoints. Since
2188 the parent is blocked anyway, it won't miss any breakpoint. */
2189 if (bl->pspace->breakpoints_not_allowed)
2190 return 0;
2191
2192 /* Don't insert a breakpoint if we're trying to step past its
2193 location, except if the breakpoint is a single-step breakpoint,
2194 and the breakpoint's thread is the thread which is stepping past
2195 a breakpoint. */
2196 if ((bl->loc_type == bp_loc_software_breakpoint
2197 || bl->loc_type == bp_loc_hardware_breakpoint)
2198 && stepping_past_instruction_at (bl->pspace->aspace,
2199 bl->address)
2200 /* The single-step breakpoint may be inserted at the location
2201 we're trying to step if the instruction branches to itself.
2202 However, the instruction won't be executed at all and it may
2203 break the semantics of the instruction, for example, the
2204 instruction is a conditional branch or updates some flags.
2205 We can't fix it unless GDB is able to emulate the instruction
2206 or switch to displaced stepping. */
2207 && !(bl->owner->type == bp_single_step
2208 && thread_is_stepping_over_breakpoint (bl->owner->thread)))
2209 {
2210 if (debug_infrun)
2211 {
2212 fprintf_unfiltered (gdb_stdlog,
2213 "infrun: skipping breakpoint: "
2214 "stepping past insn at: %s\n",
2215 paddress (bl->gdbarch, bl->address));
2216 }
2217 return 0;
2218 }
2219
2220 /* Don't insert watchpoints if we're trying to step past the
2221 instruction that triggered one. */
2222 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2223 && stepping_past_nonsteppable_watchpoint ())
2224 {
2225 if (debug_infrun)
2226 {
2227 fprintf_unfiltered (gdb_stdlog,
2228 "infrun: stepping past non-steppable watchpoint. "
2229 "skipping watchpoint at %s:%d\n",
2230 paddress (bl->gdbarch, bl->address),
2231 bl->length);
2232 }
2233 return 0;
2234 }
2235
2236 return 1;
2237 }
2238
2239 /* Same as should_be_inserted but does the check assuming
2240 that the location is not duplicated. */
2241
2242 static int
2243 unduplicated_should_be_inserted (struct bp_location *bl)
2244 {
2245 int result;
2246 const int save_duplicate = bl->duplicate;
2247
2248 bl->duplicate = 0;
2249 result = should_be_inserted (bl);
2250 bl->duplicate = save_duplicate;
2251 return result;
2252 }
2253
2254 /* Parses a conditional described by an expression COND into an
2255 agent expression bytecode suitable for evaluation
2256 by the bytecode interpreter. Return NULL if there was
2257 any error during parsing. */
2258
2259 static 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 = gdb::move (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 VEC_free (agent_expr_p, bl->target_info.conditions);
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 /* Add the condition to the vector. This will be used later to send the
2375 conditions to the target. */
2376 VEC_safe_push (agent_expr_p, bl->target_info.conditions,
2377 loc->cond_bytecode.get ());
2378 }
2379
2380 return;
2381 }
2382
2383 /* Parses a command described by string CMD into an agent expression
2384 bytecode suitable for evaluation by the bytecode interpreter.
2385 Return NULL if there was any error during parsing. */
2386
2387 static agent_expr_up
2388 parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2389 {
2390 struct cleanup *old_cleanups = 0;
2391 struct expression **argvec;
2392 const char *cmdrest;
2393 const char *format_start, *format_end;
2394 struct format_piece *fpieces;
2395 int nargs;
2396 struct gdbarch *gdbarch = get_current_arch ();
2397
2398 if (cmd == NULL)
2399 return NULL;
2400
2401 cmdrest = cmd;
2402
2403 if (*cmdrest == ',')
2404 ++cmdrest;
2405 cmdrest = skip_spaces_const (cmdrest);
2406
2407 if (*cmdrest++ != '"')
2408 error (_("No format string following the location"));
2409
2410 format_start = cmdrest;
2411
2412 fpieces = parse_format_string (&cmdrest);
2413
2414 old_cleanups = make_cleanup (free_format_pieces_cleanup, &fpieces);
2415
2416 format_end = cmdrest;
2417
2418 if (*cmdrest++ != '"')
2419 error (_("Bad format string, non-terminated '\"'."));
2420
2421 cmdrest = skip_spaces_const (cmdrest);
2422
2423 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2424 error (_("Invalid argument syntax"));
2425
2426 if (*cmdrest == ',')
2427 cmdrest++;
2428 cmdrest = skip_spaces_const (cmdrest);
2429
2430 /* For each argument, make an expression. */
2431
2432 argvec = (struct expression **) alloca (strlen (cmd)
2433 * sizeof (struct expression *));
2434
2435 nargs = 0;
2436 while (*cmdrest != '\0')
2437 {
2438 const char *cmd1;
2439
2440 cmd1 = cmdrest;
2441 expression_up expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2442 argvec[nargs++] = expr.release ();
2443 cmdrest = cmd1;
2444 if (*cmdrest == ',')
2445 ++cmdrest;
2446 }
2447
2448 agent_expr_up aexpr;
2449
2450 /* We don't want to stop processing, so catch any errors
2451 that may show up. */
2452 TRY
2453 {
2454 aexpr = gdb::move (gen_printf (scope, gdbarch, 0, 0,
2455 format_start, format_end - format_start,
2456 fpieces, nargs, argvec));
2457 }
2458 CATCH (ex, RETURN_MASK_ERROR)
2459 {
2460 /* If we got here, it means the command could not be parsed to a valid
2461 bytecode expression and thus can't be evaluated on the target's side.
2462 It's no use iterating through the other commands. */
2463 }
2464 END_CATCH
2465
2466 do_cleanups (old_cleanups);
2467
2468 /* We have a valid agent expression, return it. */
2469 return aexpr;
2470 }
2471
2472 /* Based on location BL, create a list of breakpoint commands to be
2473 passed on to the target. If we have duplicated locations with
2474 different commands, we will add any such to the list. */
2475
2476 static void
2477 build_target_command_list (struct bp_location *bl)
2478 {
2479 struct bp_location **locp = NULL, **loc2p;
2480 int null_command_or_parse_error = 0;
2481 int modified = bl->needs_update;
2482 struct bp_location *loc;
2483
2484 /* Release commands left over from a previous insert. */
2485 VEC_free (agent_expr_p, bl->target_info.tcommands);
2486
2487 if (!target_can_run_breakpoint_commands ())
2488 return;
2489
2490 /* For now, limit to agent-style dprintf breakpoints. */
2491 if (dprintf_style != dprintf_style_agent)
2492 return;
2493
2494 /* For now, if we have any duplicate location that isn't a dprintf,
2495 don't install the target-side commands, as that would make the
2496 breakpoint not be reported to the core, and we'd lose
2497 control. */
2498 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2499 {
2500 loc = (*loc2p);
2501 if (is_breakpoint (loc->owner)
2502 && loc->pspace->num == bl->pspace->num
2503 && loc->owner->type != bp_dprintf)
2504 return;
2505 }
2506
2507 /* Do a first pass to check for locations with no assigned
2508 conditions or conditions that fail to parse to a valid agent expression
2509 bytecode. If any of these happen, then it's no use to send conditions
2510 to the target since this location will always trigger and generate a
2511 response back to GDB. */
2512 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2513 {
2514 loc = (*loc2p);
2515 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2516 {
2517 if (modified)
2518 {
2519 /* Re-parse the commands since something changed. In that
2520 case we already freed the command bytecodes (see
2521 force_breakpoint_reinsertion). We just
2522 need to parse the command to bytecodes again. */
2523 loc->cmd_bytecode
2524 = parse_cmd_to_aexpr (bl->address,
2525 loc->owner->extra_string);
2526 }
2527
2528 /* If we have a NULL bytecode expression, it means something
2529 went wrong or we have a null command expression. */
2530 if (!loc->cmd_bytecode)
2531 {
2532 null_command_or_parse_error = 1;
2533 break;
2534 }
2535 }
2536 }
2537
2538 /* If anything failed, then we're not doing target-side commands,
2539 and so clean up. */
2540 if (null_command_or_parse_error)
2541 {
2542 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2543 {
2544 loc = (*loc2p);
2545 if (is_breakpoint (loc->owner)
2546 && loc->pspace->num == bl->pspace->num)
2547 {
2548 /* Only go as far as the first NULL bytecode is
2549 located. */
2550 if (loc->cmd_bytecode == NULL)
2551 return;
2552
2553 loc->cmd_bytecode.reset ();
2554 }
2555 }
2556 }
2557
2558 /* No NULL commands or failed bytecode generation. Build a command list
2559 for this location's address. */
2560 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2561 {
2562 loc = (*loc2p);
2563 if (loc->owner->extra_string
2564 && is_breakpoint (loc->owner)
2565 && loc->pspace->num == bl->pspace->num
2566 && loc->owner->enable_state == bp_enabled
2567 && loc->enabled)
2568 /* Add the command to the vector. This will be used later
2569 to send the commands to the target. */
2570 VEC_safe_push (agent_expr_p, bl->target_info.tcommands,
2571 loc->cmd_bytecode.get ());
2572 }
2573
2574 bl->target_info.persist = 0;
2575 /* Maybe flag this location as persistent. */
2576 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2577 bl->target_info.persist = 1;
2578 }
2579
2580 /* Return the kind of breakpoint on address *ADDR. Get the kind
2581 of breakpoint according to ADDR except single-step breakpoint.
2582 Get the kind of single-step breakpoint according to the current
2583 registers state. */
2584
2585 static int
2586 breakpoint_kind (struct bp_location *bl, CORE_ADDR *addr)
2587 {
2588 if (bl->owner->type == bp_single_step)
2589 {
2590 struct thread_info *thr = find_thread_global_id (bl->owner->thread);
2591 struct regcache *regcache;
2592
2593 regcache = get_thread_regcache (thr->ptid);
2594
2595 return gdbarch_breakpoint_kind_from_current_state (bl->gdbarch,
2596 regcache, addr);
2597 }
2598 else
2599 return gdbarch_breakpoint_kind_from_pc (bl->gdbarch, addr);
2600 }
2601
2602 /* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2603 location. Any error messages are printed to TMP_ERROR_STREAM; and
2604 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
2605 Returns 0 for success, 1 if the bp_location type is not supported or
2606 -1 for failure.
2607
2608 NOTE drow/2003-09-09: This routine could be broken down to an
2609 object-style method for each breakpoint or catchpoint type. */
2610 static int
2611 insert_bp_location (struct bp_location *bl,
2612 struct ui_file *tmp_error_stream,
2613 int *disabled_breaks,
2614 int *hw_breakpoint_error,
2615 int *hw_bp_error_explained_already)
2616 {
2617 enum errors bp_err = GDB_NO_ERROR;
2618 const char *bp_err_message = NULL;
2619
2620 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2621 return 0;
2622
2623 /* Note we don't initialize bl->target_info, as that wipes out
2624 the breakpoint location's shadow_contents if the breakpoint
2625 is still inserted at that location. This in turn breaks
2626 target_read_memory which depends on these buffers when
2627 a memory read is requested at the breakpoint location:
2628 Once the target_info has been wiped, we fail to see that
2629 we have a breakpoint inserted at that address and thus
2630 read the breakpoint instead of returning the data saved in
2631 the breakpoint location's shadow contents. */
2632 bl->target_info.reqstd_address = bl->address;
2633 bl->target_info.placed_address_space = bl->pspace->aspace;
2634 bl->target_info.length = bl->length;
2635
2636 /* When working with target-side conditions, we must pass all the conditions
2637 for the same breakpoint address down to the target since GDB will not
2638 insert those locations. With a list of breakpoint conditions, the target
2639 can decide when to stop and notify GDB. */
2640
2641 if (is_breakpoint (bl->owner))
2642 {
2643 build_target_condition_list (bl);
2644 build_target_command_list (bl);
2645 /* Reset the modification marker. */
2646 bl->needs_update = 0;
2647 }
2648
2649 if (bl->loc_type == bp_loc_software_breakpoint
2650 || bl->loc_type == bp_loc_hardware_breakpoint)
2651 {
2652 if (bl->owner->type != bp_hardware_breakpoint)
2653 {
2654 /* If the explicitly specified breakpoint type
2655 is not hardware breakpoint, check the memory map to see
2656 if the breakpoint address is in read only memory or not.
2657
2658 Two important cases are:
2659 - location type is not hardware breakpoint, memory
2660 is readonly. We change the type of the location to
2661 hardware breakpoint.
2662 - location type is hardware breakpoint, memory is
2663 read-write. This means we've previously made the
2664 location hardware one, but then the memory map changed,
2665 so we undo.
2666
2667 When breakpoints are removed, remove_breakpoints will use
2668 location types we've just set here, the only possible
2669 problem is that memory map has changed during running
2670 program, but it's not going to work anyway with current
2671 gdb. */
2672 struct mem_region *mr
2673 = lookup_mem_region (bl->target_info.reqstd_address);
2674
2675 if (mr)
2676 {
2677 if (automatic_hardware_breakpoints)
2678 {
2679 enum bp_loc_type new_type;
2680
2681 if (mr->attrib.mode != MEM_RW)
2682 new_type = bp_loc_hardware_breakpoint;
2683 else
2684 new_type = bp_loc_software_breakpoint;
2685
2686 if (new_type != bl->loc_type)
2687 {
2688 static int said = 0;
2689
2690 bl->loc_type = new_type;
2691 if (!said)
2692 {
2693 fprintf_filtered (gdb_stdout,
2694 _("Note: automatically using "
2695 "hardware breakpoints for "
2696 "read-only addresses.\n"));
2697 said = 1;
2698 }
2699 }
2700 }
2701 else if (bl->loc_type == bp_loc_software_breakpoint
2702 && mr->attrib.mode != MEM_RW)
2703 {
2704 fprintf_unfiltered (tmp_error_stream,
2705 _("Cannot insert breakpoint %d.\n"
2706 "Cannot set software breakpoint "
2707 "at read-only address %s\n"),
2708 bl->owner->number,
2709 paddress (bl->gdbarch, bl->address));
2710 return 1;
2711 }
2712 }
2713 }
2714
2715 /* First check to see if we have to handle an overlay. */
2716 if (overlay_debugging == ovly_off
2717 || bl->section == NULL
2718 || !(section_is_overlay (bl->section)))
2719 {
2720 /* No overlay handling: just set the breakpoint. */
2721 TRY
2722 {
2723 int val;
2724
2725 val = bl->owner->ops->insert_location (bl);
2726 if (val)
2727 bp_err = GENERIC_ERROR;
2728 }
2729 CATCH (e, RETURN_MASK_ALL)
2730 {
2731 bp_err = e.error;
2732 bp_err_message = e.message;
2733 }
2734 END_CATCH
2735 }
2736 else
2737 {
2738 /* This breakpoint is in an overlay section.
2739 Shall we set a breakpoint at the LMA? */
2740 if (!overlay_events_enabled)
2741 {
2742 /* Yes -- overlay event support is not active,
2743 so we must try to set a breakpoint at the LMA.
2744 This will not work for a hardware breakpoint. */
2745 if (bl->loc_type == bp_loc_hardware_breakpoint)
2746 warning (_("hardware breakpoint %d not supported in overlay!"),
2747 bl->owner->number);
2748 else
2749 {
2750 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2751 bl->section);
2752 /* Set a software (trap) breakpoint at the LMA. */
2753 bl->overlay_target_info = bl->target_info;
2754 bl->overlay_target_info.reqstd_address = addr;
2755
2756 /* No overlay handling: just set the breakpoint. */
2757 TRY
2758 {
2759 int val;
2760
2761 bl->overlay_target_info.kind
2762 = breakpoint_kind (bl, &addr);
2763 bl->overlay_target_info.placed_address = addr;
2764 val = target_insert_breakpoint (bl->gdbarch,
2765 &bl->overlay_target_info);
2766 if (val)
2767 bp_err = GENERIC_ERROR;
2768 }
2769 CATCH (e, RETURN_MASK_ALL)
2770 {
2771 bp_err = e.error;
2772 bp_err_message = e.message;
2773 }
2774 END_CATCH
2775
2776 if (bp_err != GDB_NO_ERROR)
2777 fprintf_unfiltered (tmp_error_stream,
2778 "Overlay breakpoint %d "
2779 "failed: in ROM?\n",
2780 bl->owner->number);
2781 }
2782 }
2783 /* Shall we set a breakpoint at the VMA? */
2784 if (section_is_mapped (bl->section))
2785 {
2786 /* Yes. This overlay section is mapped into memory. */
2787 TRY
2788 {
2789 int val;
2790
2791 val = bl->owner->ops->insert_location (bl);
2792 if (val)
2793 bp_err = GENERIC_ERROR;
2794 }
2795 CATCH (e, RETURN_MASK_ALL)
2796 {
2797 bp_err = e.error;
2798 bp_err_message = e.message;
2799 }
2800 END_CATCH
2801 }
2802 else
2803 {
2804 /* No. This breakpoint will not be inserted.
2805 No error, but do not mark the bp as 'inserted'. */
2806 return 0;
2807 }
2808 }
2809
2810 if (bp_err != GDB_NO_ERROR)
2811 {
2812 /* Can't set the breakpoint. */
2813
2814 /* In some cases, we might not be able to insert a
2815 breakpoint in a shared library that has already been
2816 removed, but we have not yet processed the shlib unload
2817 event. Unfortunately, some targets that implement
2818 breakpoint insertion themselves can't tell why the
2819 breakpoint insertion failed (e.g., the remote target
2820 doesn't define error codes), so we must treat generic
2821 errors as memory errors. */
2822 if ((bp_err == GENERIC_ERROR || bp_err == MEMORY_ERROR)
2823 && bl->loc_type == bp_loc_software_breakpoint
2824 && (solib_name_from_address (bl->pspace, bl->address)
2825 || shared_objfile_contains_address_p (bl->pspace,
2826 bl->address)))
2827 {
2828 /* See also: disable_breakpoints_in_shlibs. */
2829 bl->shlib_disabled = 1;
2830 observer_notify_breakpoint_modified (bl->owner);
2831 if (!*disabled_breaks)
2832 {
2833 fprintf_unfiltered (tmp_error_stream,
2834 "Cannot insert breakpoint %d.\n",
2835 bl->owner->number);
2836 fprintf_unfiltered (tmp_error_stream,
2837 "Temporarily disabling shared "
2838 "library breakpoints:\n");
2839 }
2840 *disabled_breaks = 1;
2841 fprintf_unfiltered (tmp_error_stream,
2842 "breakpoint #%d\n", bl->owner->number);
2843 return 0;
2844 }
2845 else
2846 {
2847 if (bl->loc_type == bp_loc_hardware_breakpoint)
2848 {
2849 *hw_breakpoint_error = 1;
2850 *hw_bp_error_explained_already = bp_err_message != NULL;
2851 fprintf_unfiltered (tmp_error_stream,
2852 "Cannot insert hardware breakpoint %d%s",
2853 bl->owner->number, bp_err_message ? ":" : ".\n");
2854 if (bp_err_message != NULL)
2855 fprintf_unfiltered (tmp_error_stream, "%s.\n", bp_err_message);
2856 }
2857 else
2858 {
2859 if (bp_err_message == NULL)
2860 {
2861 char *message
2862 = memory_error_message (TARGET_XFER_E_IO,
2863 bl->gdbarch, bl->address);
2864 struct cleanup *old_chain = make_cleanup (xfree, message);
2865
2866 fprintf_unfiltered (tmp_error_stream,
2867 "Cannot insert breakpoint %d.\n"
2868 "%s\n",
2869 bl->owner->number, message);
2870 do_cleanups (old_chain);
2871 }
2872 else
2873 {
2874 fprintf_unfiltered (tmp_error_stream,
2875 "Cannot insert breakpoint %d: %s\n",
2876 bl->owner->number,
2877 bp_err_message);
2878 }
2879 }
2880 return 1;
2881
2882 }
2883 }
2884 else
2885 bl->inserted = 1;
2886
2887 return 0;
2888 }
2889
2890 else if (bl->loc_type == bp_loc_hardware_watchpoint
2891 /* NOTE drow/2003-09-08: This state only exists for removing
2892 watchpoints. It's not clear that it's necessary... */
2893 && bl->owner->disposition != disp_del_at_next_stop)
2894 {
2895 int val;
2896
2897 gdb_assert (bl->owner->ops != NULL
2898 && bl->owner->ops->insert_location != NULL);
2899
2900 val = bl->owner->ops->insert_location (bl);
2901
2902 /* If trying to set a read-watchpoint, and it turns out it's not
2903 supported, try emulating one with an access watchpoint. */
2904 if (val == 1 && bl->watchpoint_type == hw_read)
2905 {
2906 struct bp_location *loc, **loc_temp;
2907
2908 /* But don't try to insert it, if there's already another
2909 hw_access location that would be considered a duplicate
2910 of this one. */
2911 ALL_BP_LOCATIONS (loc, loc_temp)
2912 if (loc != bl
2913 && loc->watchpoint_type == hw_access
2914 && watchpoint_locations_match (bl, loc))
2915 {
2916 bl->duplicate = 1;
2917 bl->inserted = 1;
2918 bl->target_info = loc->target_info;
2919 bl->watchpoint_type = hw_access;
2920 val = 0;
2921 break;
2922 }
2923
2924 if (val == 1)
2925 {
2926 bl->watchpoint_type = hw_access;
2927 val = bl->owner->ops->insert_location (bl);
2928
2929 if (val)
2930 /* Back to the original value. */
2931 bl->watchpoint_type = hw_read;
2932 }
2933 }
2934
2935 bl->inserted = (val == 0);
2936 }
2937
2938 else if (bl->owner->type == bp_catchpoint)
2939 {
2940 int val;
2941
2942 gdb_assert (bl->owner->ops != NULL
2943 && bl->owner->ops->insert_location != NULL);
2944
2945 val = bl->owner->ops->insert_location (bl);
2946 if (val)
2947 {
2948 bl->owner->enable_state = bp_disabled;
2949
2950 if (val == 1)
2951 warning (_("\
2952 Error inserting catchpoint %d: Your system does not support this type\n\
2953 of catchpoint."), bl->owner->number);
2954 else
2955 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2956 }
2957
2958 bl->inserted = (val == 0);
2959
2960 /* We've already printed an error message if there was a problem
2961 inserting this catchpoint, and we've disabled the catchpoint,
2962 so just return success. */
2963 return 0;
2964 }
2965
2966 return 0;
2967 }
2968
2969 /* This function is called when program space PSPACE is about to be
2970 deleted. It takes care of updating breakpoints to not reference
2971 PSPACE anymore. */
2972
2973 void
2974 breakpoint_program_space_exit (struct program_space *pspace)
2975 {
2976 struct breakpoint *b, *b_temp;
2977 struct bp_location *loc, **loc_temp;
2978
2979 /* Remove any breakpoint that was set through this program space. */
2980 ALL_BREAKPOINTS_SAFE (b, b_temp)
2981 {
2982 if (b->pspace == pspace)
2983 delete_breakpoint (b);
2984 }
2985
2986 /* Breakpoints set through other program spaces could have locations
2987 bound to PSPACE as well. Remove those. */
2988 ALL_BP_LOCATIONS (loc, loc_temp)
2989 {
2990 struct bp_location *tmp;
2991
2992 if (loc->pspace == pspace)
2993 {
2994 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
2995 if (loc->owner->loc == loc)
2996 loc->owner->loc = loc->next;
2997 else
2998 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2999 if (tmp->next == loc)
3000 {
3001 tmp->next = loc->next;
3002 break;
3003 }
3004 }
3005 }
3006
3007 /* Now update the global location list to permanently delete the
3008 removed locations above. */
3009 update_global_location_list (UGLL_DONT_INSERT);
3010 }
3011
3012 /* Make sure all breakpoints are inserted in inferior.
3013 Throws exception on any error.
3014 A breakpoint that is already inserted won't be inserted
3015 again, so calling this function twice is safe. */
3016 void
3017 insert_breakpoints (void)
3018 {
3019 struct breakpoint *bpt;
3020
3021 ALL_BREAKPOINTS (bpt)
3022 if (is_hardware_watchpoint (bpt))
3023 {
3024 struct watchpoint *w = (struct watchpoint *) bpt;
3025
3026 update_watchpoint (w, 0 /* don't reparse. */);
3027 }
3028
3029 /* Updating watchpoints creates new locations, so update the global
3030 location list. Explicitly tell ugll to insert locations and
3031 ignore breakpoints_always_inserted_mode. */
3032 update_global_location_list (UGLL_INSERT);
3033 }
3034
3035 /* Invoke CALLBACK for each of bp_location. */
3036
3037 void
3038 iterate_over_bp_locations (walk_bp_location_callback callback)
3039 {
3040 struct bp_location *loc, **loc_tmp;
3041
3042 ALL_BP_LOCATIONS (loc, loc_tmp)
3043 {
3044 callback (loc, NULL);
3045 }
3046 }
3047
3048 /* This is used when we need to synch breakpoint conditions between GDB and the
3049 target. It is the case with deleting and disabling of breakpoints when using
3050 always-inserted mode. */
3051
3052 static void
3053 update_inserted_breakpoint_locations (void)
3054 {
3055 struct bp_location *bl, **blp_tmp;
3056 int error_flag = 0;
3057 int val = 0;
3058 int disabled_breaks = 0;
3059 int hw_breakpoint_error = 0;
3060 int hw_bp_details_reported = 0;
3061
3062 struct ui_file *tmp_error_stream = mem_fileopen ();
3063 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
3064
3065 /* Explicitly mark the warning -- this will only be printed if
3066 there was an error. */
3067 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
3068
3069 save_current_space_and_thread ();
3070
3071 ALL_BP_LOCATIONS (bl, blp_tmp)
3072 {
3073 /* We only want to update software breakpoints and hardware
3074 breakpoints. */
3075 if (!is_breakpoint (bl->owner))
3076 continue;
3077
3078 /* We only want to update locations that are already inserted
3079 and need updating. This is to avoid unwanted insertion during
3080 deletion of breakpoints. */
3081 if (!bl->inserted || (bl->inserted && !bl->needs_update))
3082 continue;
3083
3084 switch_to_program_space_and_thread (bl->pspace);
3085
3086 /* For targets that support global breakpoints, there's no need
3087 to select an inferior to insert breakpoint to. In fact, even
3088 if we aren't attached to any process yet, we should still
3089 insert breakpoints. */
3090 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3091 && ptid_equal (inferior_ptid, null_ptid))
3092 continue;
3093
3094 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
3095 &hw_breakpoint_error, &hw_bp_details_reported);
3096 if (val)
3097 error_flag = val;
3098 }
3099
3100 if (error_flag)
3101 {
3102 target_terminal_ours_for_output ();
3103 error_stream (tmp_error_stream);
3104 }
3105
3106 do_cleanups (cleanups);
3107 }
3108
3109 /* Used when starting or continuing the program. */
3110
3111 static void
3112 insert_breakpoint_locations (void)
3113 {
3114 struct breakpoint *bpt;
3115 struct bp_location *bl, **blp_tmp;
3116 int error_flag = 0;
3117 int val = 0;
3118 int disabled_breaks = 0;
3119 int hw_breakpoint_error = 0;
3120 int hw_bp_error_explained_already = 0;
3121
3122 struct ui_file *tmp_error_stream = mem_fileopen ();
3123 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
3124
3125 /* Explicitly mark the warning -- this will only be printed if
3126 there was an error. */
3127 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
3128
3129 save_current_space_and_thread ();
3130
3131 ALL_BP_LOCATIONS (bl, blp_tmp)
3132 {
3133 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
3134 continue;
3135
3136 /* There is no point inserting thread-specific breakpoints if
3137 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3138 has BL->OWNER always non-NULL. */
3139 if (bl->owner->thread != -1
3140 && !valid_global_thread_id (bl->owner->thread))
3141 continue;
3142
3143 switch_to_program_space_and_thread (bl->pspace);
3144
3145 /* For targets that support global breakpoints, there's no need
3146 to select an inferior to insert breakpoint to. In fact, even
3147 if we aren't attached to any process yet, we should still
3148 insert breakpoints. */
3149 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3150 && ptid_equal (inferior_ptid, null_ptid))
3151 continue;
3152
3153 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
3154 &hw_breakpoint_error, &hw_bp_error_explained_already);
3155 if (val)
3156 error_flag = val;
3157 }
3158
3159 /* If we failed to insert all locations of a watchpoint, remove
3160 them, as half-inserted watchpoint is of limited use. */
3161 ALL_BREAKPOINTS (bpt)
3162 {
3163 int some_failed = 0;
3164 struct bp_location *loc;
3165
3166 if (!is_hardware_watchpoint (bpt))
3167 continue;
3168
3169 if (!breakpoint_enabled (bpt))
3170 continue;
3171
3172 if (bpt->disposition == disp_del_at_next_stop)
3173 continue;
3174
3175 for (loc = bpt->loc; loc; loc = loc->next)
3176 if (!loc->inserted && should_be_inserted (loc))
3177 {
3178 some_failed = 1;
3179 break;
3180 }
3181 if (some_failed)
3182 {
3183 for (loc = bpt->loc; loc; loc = loc->next)
3184 if (loc->inserted)
3185 remove_breakpoint (loc);
3186
3187 hw_breakpoint_error = 1;
3188 fprintf_unfiltered (tmp_error_stream,
3189 "Could not insert hardware watchpoint %d.\n",
3190 bpt->number);
3191 error_flag = -1;
3192 }
3193 }
3194
3195 if (error_flag)
3196 {
3197 /* If a hardware breakpoint or watchpoint was inserted, add a
3198 message about possibly exhausted resources. */
3199 if (hw_breakpoint_error && !hw_bp_error_explained_already)
3200 {
3201 fprintf_unfiltered (tmp_error_stream,
3202 "Could not insert hardware breakpoints:\n\
3203 You may have requested too many hardware breakpoints/watchpoints.\n");
3204 }
3205 target_terminal_ours_for_output ();
3206 error_stream (tmp_error_stream);
3207 }
3208
3209 do_cleanups (cleanups);
3210 }
3211
3212 /* Used when the program stops.
3213 Returns zero if successful, or non-zero if there was a problem
3214 removing a breakpoint location. */
3215
3216 int
3217 remove_breakpoints (void)
3218 {
3219 struct bp_location *bl, **blp_tmp;
3220 int val = 0;
3221
3222 ALL_BP_LOCATIONS (bl, blp_tmp)
3223 {
3224 if (bl->inserted && !is_tracepoint (bl->owner))
3225 val |= remove_breakpoint (bl);
3226 }
3227 return val;
3228 }
3229
3230 /* When a thread exits, remove breakpoints that are related to
3231 that thread. */
3232
3233 static void
3234 remove_threaded_breakpoints (struct thread_info *tp, int silent)
3235 {
3236 struct breakpoint *b, *b_tmp;
3237
3238 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3239 {
3240 if (b->thread == tp->global_num && user_breakpoint_p (b))
3241 {
3242 b->disposition = disp_del_at_next_stop;
3243
3244 printf_filtered (_("\
3245 Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3246 b->number, print_thread_id (tp));
3247
3248 /* Hide it from the user. */
3249 b->number = 0;
3250 }
3251 }
3252 }
3253
3254 /* Remove breakpoints of process PID. */
3255
3256 int
3257 remove_breakpoints_pid (int pid)
3258 {
3259 struct bp_location *bl, **blp_tmp;
3260 int val;
3261 struct inferior *inf = find_inferior_pid (pid);
3262
3263 ALL_BP_LOCATIONS (bl, blp_tmp)
3264 {
3265 if (bl->pspace != inf->pspace)
3266 continue;
3267
3268 if (bl->inserted && !bl->target_info.persist)
3269 {
3270 val = remove_breakpoint (bl);
3271 if (val != 0)
3272 return val;
3273 }
3274 }
3275 return 0;
3276 }
3277
3278 int
3279 reattach_breakpoints (int pid)
3280 {
3281 struct cleanup *old_chain;
3282 struct bp_location *bl, **blp_tmp;
3283 int val;
3284 struct ui_file *tmp_error_stream;
3285 int dummy1 = 0, dummy2 = 0, dummy3 = 0;
3286 struct inferior *inf;
3287 struct thread_info *tp;
3288
3289 tp = any_live_thread_of_process (pid);
3290 if (tp == NULL)
3291 return 1;
3292
3293 inf = find_inferior_pid (pid);
3294 old_chain = save_inferior_ptid ();
3295
3296 inferior_ptid = tp->ptid;
3297
3298 tmp_error_stream = mem_fileopen ();
3299 make_cleanup_ui_file_delete (tmp_error_stream);
3300
3301 ALL_BP_LOCATIONS (bl, blp_tmp)
3302 {
3303 if (bl->pspace != inf->pspace)
3304 continue;
3305
3306 if (bl->inserted)
3307 {
3308 bl->inserted = 0;
3309 val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2, &dummy3);
3310 if (val != 0)
3311 {
3312 do_cleanups (old_chain);
3313 return val;
3314 }
3315 }
3316 }
3317 do_cleanups (old_chain);
3318 return 0;
3319 }
3320
3321 static int internal_breakpoint_number = -1;
3322
3323 /* Set the breakpoint number of B, depending on the value of INTERNAL.
3324 If INTERNAL is non-zero, the breakpoint number will be populated
3325 from internal_breakpoint_number and that variable decremented.
3326 Otherwise the breakpoint number will be populated from
3327 breakpoint_count and that value incremented. Internal breakpoints
3328 do not set the internal var bpnum. */
3329 static void
3330 set_breakpoint_number (int internal, struct breakpoint *b)
3331 {
3332 if (internal)
3333 b->number = internal_breakpoint_number--;
3334 else
3335 {
3336 set_breakpoint_count (breakpoint_count + 1);
3337 b->number = breakpoint_count;
3338 }
3339 }
3340
3341 static struct breakpoint *
3342 create_internal_breakpoint (struct gdbarch *gdbarch,
3343 CORE_ADDR address, enum bptype type,
3344 const struct breakpoint_ops *ops)
3345 {
3346 struct symtab_and_line sal;
3347 struct breakpoint *b;
3348
3349 init_sal (&sal); /* Initialize to zeroes. */
3350
3351 sal.pc = address;
3352 sal.section = find_pc_overlay (sal.pc);
3353 sal.pspace = current_program_space;
3354
3355 b = set_raw_breakpoint (gdbarch, sal, type, ops);
3356 b->number = internal_breakpoint_number--;
3357 b->disposition = disp_donttouch;
3358
3359 return b;
3360 }
3361
3362 static const char *const longjmp_names[] =
3363 {
3364 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3365 };
3366 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3367
3368 /* Per-objfile data private to breakpoint.c. */
3369 struct breakpoint_objfile_data
3370 {
3371 /* Minimal symbol for "_ovly_debug_event" (if any). */
3372 struct bound_minimal_symbol overlay_msym;
3373
3374 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3375 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
3376
3377 /* True if we have looked for longjmp probes. */
3378 int longjmp_searched;
3379
3380 /* SystemTap probe points for longjmp (if any). */
3381 VEC (probe_p) *longjmp_probes;
3382
3383 /* Minimal symbol for "std::terminate()" (if any). */
3384 struct bound_minimal_symbol terminate_msym;
3385
3386 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3387 struct bound_minimal_symbol exception_msym;
3388
3389 /* True if we have looked for exception probes. */
3390 int exception_searched;
3391
3392 /* SystemTap probe points for unwinding (if any). */
3393 VEC (probe_p) *exception_probes;
3394 };
3395
3396 static const struct objfile_data *breakpoint_objfile_key;
3397
3398 /* Minimal symbol not found sentinel. */
3399 static struct minimal_symbol msym_not_found;
3400
3401 /* Returns TRUE if MSYM point to the "not found" sentinel. */
3402
3403 static int
3404 msym_not_found_p (const struct minimal_symbol *msym)
3405 {
3406 return msym == &msym_not_found;
3407 }
3408
3409 /* Return per-objfile data needed by breakpoint.c.
3410 Allocate the data if necessary. */
3411
3412 static struct breakpoint_objfile_data *
3413 get_breakpoint_objfile_data (struct objfile *objfile)
3414 {
3415 struct breakpoint_objfile_data *bp_objfile_data;
3416
3417 bp_objfile_data = ((struct breakpoint_objfile_data *)
3418 objfile_data (objfile, breakpoint_objfile_key));
3419 if (bp_objfile_data == NULL)
3420 {
3421 bp_objfile_data =
3422 XOBNEW (&objfile->objfile_obstack, struct breakpoint_objfile_data);
3423
3424 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
3425 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3426 }
3427 return bp_objfile_data;
3428 }
3429
3430 static void
3431 free_breakpoint_probes (struct objfile *obj, void *data)
3432 {
3433 struct breakpoint_objfile_data *bp_objfile_data
3434 = (struct breakpoint_objfile_data *) data;
3435
3436 VEC_free (probe_p, bp_objfile_data->longjmp_probes);
3437 VEC_free (probe_p, bp_objfile_data->exception_probes);
3438 }
3439
3440 static void
3441 create_overlay_event_breakpoint (void)
3442 {
3443 struct objfile *objfile;
3444 const char *const func_name = "_ovly_debug_event";
3445
3446 ALL_OBJFILES (objfile)
3447 {
3448 struct breakpoint *b;
3449 struct breakpoint_objfile_data *bp_objfile_data;
3450 CORE_ADDR addr;
3451 struct explicit_location explicit_loc;
3452
3453 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3454
3455 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
3456 continue;
3457
3458 if (bp_objfile_data->overlay_msym.minsym == NULL)
3459 {
3460 struct bound_minimal_symbol m;
3461
3462 m = lookup_minimal_symbol_text (func_name, objfile);
3463 if (m.minsym == NULL)
3464 {
3465 /* Avoid future lookups in this objfile. */
3466 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
3467 continue;
3468 }
3469 bp_objfile_data->overlay_msym = m;
3470 }
3471
3472 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
3473 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3474 bp_overlay_event,
3475 &internal_breakpoint_ops);
3476 initialize_explicit_location (&explicit_loc);
3477 explicit_loc.function_name = ASTRDUP (func_name);
3478 b->location = new_explicit_location (&explicit_loc);
3479
3480 if (overlay_debugging == ovly_auto)
3481 {
3482 b->enable_state = bp_enabled;
3483 overlay_events_enabled = 1;
3484 }
3485 else
3486 {
3487 b->enable_state = bp_disabled;
3488 overlay_events_enabled = 0;
3489 }
3490 }
3491 }
3492
3493 static void
3494 create_longjmp_master_breakpoint (void)
3495 {
3496 struct program_space *pspace;
3497 struct cleanup *old_chain;
3498
3499 old_chain = save_current_program_space ();
3500
3501 ALL_PSPACES (pspace)
3502 {
3503 struct objfile *objfile;
3504
3505 set_current_program_space (pspace);
3506
3507 ALL_OBJFILES (objfile)
3508 {
3509 int i;
3510 struct gdbarch *gdbarch;
3511 struct breakpoint_objfile_data *bp_objfile_data;
3512
3513 gdbarch = get_objfile_arch (objfile);
3514
3515 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3516
3517 if (!bp_objfile_data->longjmp_searched)
3518 {
3519 VEC (probe_p) *ret;
3520
3521 ret = find_probes_in_objfile (objfile, "libc", "longjmp");
3522 if (ret != NULL)
3523 {
3524 /* We are only interested in checking one element. */
3525 struct probe *p = VEC_index (probe_p, ret, 0);
3526
3527 if (!can_evaluate_probe_arguments (p))
3528 {
3529 /* We cannot use the probe interface here, because it does
3530 not know how to evaluate arguments. */
3531 VEC_free (probe_p, ret);
3532 ret = NULL;
3533 }
3534 }
3535 bp_objfile_data->longjmp_probes = ret;
3536 bp_objfile_data->longjmp_searched = 1;
3537 }
3538
3539 if (bp_objfile_data->longjmp_probes != NULL)
3540 {
3541 int i;
3542 struct probe *probe;
3543 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3544
3545 for (i = 0;
3546 VEC_iterate (probe_p,
3547 bp_objfile_data->longjmp_probes,
3548 i, probe);
3549 ++i)
3550 {
3551 struct breakpoint *b;
3552
3553 b = create_internal_breakpoint (gdbarch,
3554 get_probe_address (probe,
3555 objfile),
3556 bp_longjmp_master,
3557 &internal_breakpoint_ops);
3558 b->location
3559 = new_probe_location ("-probe-stap libc:longjmp");
3560 b->enable_state = bp_disabled;
3561 }
3562
3563 continue;
3564 }
3565
3566 if (!gdbarch_get_longjmp_target_p (gdbarch))
3567 continue;
3568
3569 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
3570 {
3571 struct breakpoint *b;
3572 const char *func_name;
3573 CORE_ADDR addr;
3574 struct explicit_location explicit_loc;
3575
3576 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3577 continue;
3578
3579 func_name = longjmp_names[i];
3580 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3581 {
3582 struct bound_minimal_symbol m;
3583
3584 m = lookup_minimal_symbol_text (func_name, objfile);
3585 if (m.minsym == NULL)
3586 {
3587 /* Prevent future lookups in this objfile. */
3588 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
3589 continue;
3590 }
3591 bp_objfile_data->longjmp_msym[i] = m;
3592 }
3593
3594 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
3595 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3596 &internal_breakpoint_ops);
3597 initialize_explicit_location (&explicit_loc);
3598 explicit_loc.function_name = ASTRDUP (func_name);
3599 b->location = new_explicit_location (&explicit_loc);
3600 b->enable_state = bp_disabled;
3601 }
3602 }
3603 }
3604
3605 do_cleanups (old_chain);
3606 }
3607
3608 /* Create a master std::terminate breakpoint. */
3609 static void
3610 create_std_terminate_master_breakpoint (void)
3611 {
3612 struct program_space *pspace;
3613 struct cleanup *old_chain;
3614 const char *const func_name = "std::terminate()";
3615
3616 old_chain = save_current_program_space ();
3617
3618 ALL_PSPACES (pspace)
3619 {
3620 struct objfile *objfile;
3621 CORE_ADDR addr;
3622
3623 set_current_program_space (pspace);
3624
3625 ALL_OBJFILES (objfile)
3626 {
3627 struct breakpoint *b;
3628 struct breakpoint_objfile_data *bp_objfile_data;
3629 struct explicit_location explicit_loc;
3630
3631 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3632
3633 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3634 continue;
3635
3636 if (bp_objfile_data->terminate_msym.minsym == NULL)
3637 {
3638 struct bound_minimal_symbol m;
3639
3640 m = lookup_minimal_symbol (func_name, NULL, objfile);
3641 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3642 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
3643 {
3644 /* Prevent future lookups in this objfile. */
3645 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3646 continue;
3647 }
3648 bp_objfile_data->terminate_msym = m;
3649 }
3650
3651 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3652 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3653 bp_std_terminate_master,
3654 &internal_breakpoint_ops);
3655 initialize_explicit_location (&explicit_loc);
3656 explicit_loc.function_name = ASTRDUP (func_name);
3657 b->location = new_explicit_location (&explicit_loc);
3658 b->enable_state = bp_disabled;
3659 }
3660 }
3661
3662 do_cleanups (old_chain);
3663 }
3664
3665 /* Install a master breakpoint on the unwinder's debug hook. */
3666
3667 static void
3668 create_exception_master_breakpoint (void)
3669 {
3670 struct objfile *objfile;
3671 const char *const func_name = "_Unwind_DebugHook";
3672
3673 ALL_OBJFILES (objfile)
3674 {
3675 struct breakpoint *b;
3676 struct gdbarch *gdbarch;
3677 struct breakpoint_objfile_data *bp_objfile_data;
3678 CORE_ADDR addr;
3679 struct explicit_location explicit_loc;
3680
3681 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3682
3683 /* We prefer the SystemTap probe point if it exists. */
3684 if (!bp_objfile_data->exception_searched)
3685 {
3686 VEC (probe_p) *ret;
3687
3688 ret = find_probes_in_objfile (objfile, "libgcc", "unwind");
3689
3690 if (ret != NULL)
3691 {
3692 /* We are only interested in checking one element. */
3693 struct probe *p = VEC_index (probe_p, ret, 0);
3694
3695 if (!can_evaluate_probe_arguments (p))
3696 {
3697 /* We cannot use the probe interface here, because it does
3698 not know how to evaluate arguments. */
3699 VEC_free (probe_p, ret);
3700 ret = NULL;
3701 }
3702 }
3703 bp_objfile_data->exception_probes = ret;
3704 bp_objfile_data->exception_searched = 1;
3705 }
3706
3707 if (bp_objfile_data->exception_probes != NULL)
3708 {
3709 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3710 int i;
3711 struct probe *probe;
3712
3713 for (i = 0;
3714 VEC_iterate (probe_p,
3715 bp_objfile_data->exception_probes,
3716 i, probe);
3717 ++i)
3718 {
3719 struct breakpoint *b;
3720
3721 b = create_internal_breakpoint (gdbarch,
3722 get_probe_address (probe,
3723 objfile),
3724 bp_exception_master,
3725 &internal_breakpoint_ops);
3726 b->location
3727 = new_probe_location ("-probe-stap libgcc:unwind");
3728 b->enable_state = bp_disabled;
3729 }
3730
3731 continue;
3732 }
3733
3734 /* Otherwise, try the hook function. */
3735
3736 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3737 continue;
3738
3739 gdbarch = get_objfile_arch (objfile);
3740
3741 if (bp_objfile_data->exception_msym.minsym == NULL)
3742 {
3743 struct bound_minimal_symbol debug_hook;
3744
3745 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3746 if (debug_hook.minsym == NULL)
3747 {
3748 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3749 continue;
3750 }
3751
3752 bp_objfile_data->exception_msym = debug_hook;
3753 }
3754
3755 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
3756 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3757 &current_target);
3758 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3759 &internal_breakpoint_ops);
3760 initialize_explicit_location (&explicit_loc);
3761 explicit_loc.function_name = ASTRDUP (func_name);
3762 b->location = new_explicit_location (&explicit_loc);
3763 b->enable_state = bp_disabled;
3764 }
3765 }
3766
3767 /* Does B have a location spec? */
3768
3769 static int
3770 breakpoint_event_location_empty_p (const struct breakpoint *b)
3771 {
3772 return b->location != NULL && event_location_empty_p (b->location);
3773 }
3774
3775 void
3776 update_breakpoints_after_exec (void)
3777 {
3778 struct breakpoint *b, *b_tmp;
3779 struct bp_location *bploc, **bplocp_tmp;
3780
3781 /* We're about to delete breakpoints from GDB's lists. If the
3782 INSERTED flag is true, GDB will try to lift the breakpoints by
3783 writing the breakpoints' "shadow contents" back into memory. The
3784 "shadow contents" are NOT valid after an exec, so GDB should not
3785 do that. Instead, the target is responsible from marking
3786 breakpoints out as soon as it detects an exec. We don't do that
3787 here instead, because there may be other attempts to delete
3788 breakpoints after detecting an exec and before reaching here. */
3789 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
3790 if (bploc->pspace == current_program_space)
3791 gdb_assert (!bploc->inserted);
3792
3793 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3794 {
3795 if (b->pspace != current_program_space)
3796 continue;
3797
3798 /* Solib breakpoints must be explicitly reset after an exec(). */
3799 if (b->type == bp_shlib_event)
3800 {
3801 delete_breakpoint (b);
3802 continue;
3803 }
3804
3805 /* JIT breakpoints must be explicitly reset after an exec(). */
3806 if (b->type == bp_jit_event)
3807 {
3808 delete_breakpoint (b);
3809 continue;
3810 }
3811
3812 /* Thread event breakpoints must be set anew after an exec(),
3813 as must overlay event and longjmp master breakpoints. */
3814 if (b->type == bp_thread_event || b->type == bp_overlay_event
3815 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3816 || b->type == bp_exception_master)
3817 {
3818 delete_breakpoint (b);
3819 continue;
3820 }
3821
3822 /* Step-resume breakpoints are meaningless after an exec(). */
3823 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3824 {
3825 delete_breakpoint (b);
3826 continue;
3827 }
3828
3829 /* Just like single-step breakpoints. */
3830 if (b->type == bp_single_step)
3831 {
3832 delete_breakpoint (b);
3833 continue;
3834 }
3835
3836 /* Longjmp and longjmp-resume breakpoints are also meaningless
3837 after an exec. */
3838 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3839 || b->type == bp_longjmp_call_dummy
3840 || b->type == bp_exception || b->type == bp_exception_resume)
3841 {
3842 delete_breakpoint (b);
3843 continue;
3844 }
3845
3846 if (b->type == bp_catchpoint)
3847 {
3848 /* For now, none of the bp_catchpoint breakpoints need to
3849 do anything at this point. In the future, if some of
3850 the catchpoints need to something, we will need to add
3851 a new method, and call this method from here. */
3852 continue;
3853 }
3854
3855 /* bp_finish is a special case. The only way we ought to be able
3856 to see one of these when an exec() has happened, is if the user
3857 caught a vfork, and then said "finish". Ordinarily a finish just
3858 carries them to the call-site of the current callee, by setting
3859 a temporary bp there and resuming. But in this case, the finish
3860 will carry them entirely through the vfork & exec.
3861
3862 We don't want to allow a bp_finish to remain inserted now. But
3863 we can't safely delete it, 'cause finish_command has a handle to
3864 the bp on a bpstat, and will later want to delete it. There's a
3865 chance (and I've seen it happen) that if we delete the bp_finish
3866 here, that its storage will get reused by the time finish_command
3867 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3868 We really must allow finish_command to delete a bp_finish.
3869
3870 In the absence of a general solution for the "how do we know
3871 it's safe to delete something others may have handles to?"
3872 problem, what we'll do here is just uninsert the bp_finish, and
3873 let finish_command delete it.
3874
3875 (We know the bp_finish is "doomed" in the sense that it's
3876 momentary, and will be deleted as soon as finish_command sees
3877 the inferior stopped. So it doesn't matter that the bp's
3878 address is probably bogus in the new a.out, unlike e.g., the
3879 solib breakpoints.) */
3880
3881 if (b->type == bp_finish)
3882 {
3883 continue;
3884 }
3885
3886 /* Without a symbolic address, we have little hope of the
3887 pre-exec() address meaning the same thing in the post-exec()
3888 a.out. */
3889 if (breakpoint_event_location_empty_p (b))
3890 {
3891 delete_breakpoint (b);
3892 continue;
3893 }
3894 }
3895 }
3896
3897 int
3898 detach_breakpoints (ptid_t ptid)
3899 {
3900 struct bp_location *bl, **blp_tmp;
3901 int val = 0;
3902 struct cleanup *old_chain = save_inferior_ptid ();
3903 struct inferior *inf = current_inferior ();
3904
3905 if (ptid_get_pid (ptid) == ptid_get_pid (inferior_ptid))
3906 error (_("Cannot detach breakpoints of inferior_ptid"));
3907
3908 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
3909 inferior_ptid = ptid;
3910 ALL_BP_LOCATIONS (bl, blp_tmp)
3911 {
3912 if (bl->pspace != inf->pspace)
3913 continue;
3914
3915 /* This function must physically remove breakpoints locations
3916 from the specified ptid, without modifying the breakpoint
3917 package's state. Locations of type bp_loc_other are only
3918 maintained at GDB side. So, there is no need to remove
3919 these bp_loc_other locations. Moreover, removing these
3920 would modify the breakpoint package's state. */
3921 if (bl->loc_type == bp_loc_other)
3922 continue;
3923
3924 if (bl->inserted)
3925 val |= remove_breakpoint_1 (bl, DETACH_BREAKPOINT);
3926 }
3927
3928 do_cleanups (old_chain);
3929 return val;
3930 }
3931
3932 /* Remove the breakpoint location BL from the current address space.
3933 Note that this is used to detach breakpoints from a child fork.
3934 When we get here, the child isn't in the inferior list, and neither
3935 do we have objects to represent its address space --- we should
3936 *not* look at bl->pspace->aspace here. */
3937
3938 static int
3939 remove_breakpoint_1 (struct bp_location *bl, enum remove_bp_reason reason)
3940 {
3941 int val;
3942
3943 /* BL is never in moribund_locations by our callers. */
3944 gdb_assert (bl->owner != NULL);
3945
3946 /* The type of none suggests that owner is actually deleted.
3947 This should not ever happen. */
3948 gdb_assert (bl->owner->type != bp_none);
3949
3950 if (bl->loc_type == bp_loc_software_breakpoint
3951 || bl->loc_type == bp_loc_hardware_breakpoint)
3952 {
3953 /* "Normal" instruction breakpoint: either the standard
3954 trap-instruction bp (bp_breakpoint), or a
3955 bp_hardware_breakpoint. */
3956
3957 /* First check to see if we have to handle an overlay. */
3958 if (overlay_debugging == ovly_off
3959 || bl->section == NULL
3960 || !(section_is_overlay (bl->section)))
3961 {
3962 /* No overlay handling: just remove the breakpoint. */
3963
3964 /* If we're trying to uninsert a memory breakpoint that we
3965 know is set in a dynamic object that is marked
3966 shlib_disabled, then either the dynamic object was
3967 removed with "remove-symbol-file" or with
3968 "nosharedlibrary". In the former case, we don't know
3969 whether another dynamic object might have loaded over the
3970 breakpoint's address -- the user might well let us know
3971 about it next with add-symbol-file (the whole point of
3972 add-symbol-file is letting the user manually maintain a
3973 list of dynamically loaded objects). If we have the
3974 breakpoint's shadow memory, that is, this is a software
3975 breakpoint managed by GDB, check whether the breakpoint
3976 is still inserted in memory, to avoid overwriting wrong
3977 code with stale saved shadow contents. Note that HW
3978 breakpoints don't have shadow memory, as they're
3979 implemented using a mechanism that is not dependent on
3980 being able to modify the target's memory, and as such
3981 they should always be removed. */
3982 if (bl->shlib_disabled
3983 && bl->target_info.shadow_len != 0
3984 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3985 val = 0;
3986 else
3987 val = bl->owner->ops->remove_location (bl, reason);
3988 }
3989 else
3990 {
3991 /* This breakpoint is in an overlay section.
3992 Did we set a breakpoint at the LMA? */
3993 if (!overlay_events_enabled)
3994 {
3995 /* Yes -- overlay event support is not active, so we
3996 should have set a breakpoint at the LMA. Remove it.
3997 */
3998 /* Ignore any failures: if the LMA is in ROM, we will
3999 have already warned when we failed to insert it. */
4000 if (bl->loc_type == bp_loc_hardware_breakpoint)
4001 target_remove_hw_breakpoint (bl->gdbarch,
4002 &bl->overlay_target_info);
4003 else
4004 target_remove_breakpoint (bl->gdbarch,
4005 &bl->overlay_target_info,
4006 reason);
4007 }
4008 /* Did we set a breakpoint at the VMA?
4009 If so, we will have marked the breakpoint 'inserted'. */
4010 if (bl->inserted)
4011 {
4012 /* Yes -- remove it. Previously we did not bother to
4013 remove the breakpoint if the section had been
4014 unmapped, but let's not rely on that being safe. We
4015 don't know what the overlay manager might do. */
4016
4017 /* However, we should remove *software* breakpoints only
4018 if the section is still mapped, or else we overwrite
4019 wrong code with the saved shadow contents. */
4020 if (bl->loc_type == bp_loc_hardware_breakpoint
4021 || section_is_mapped (bl->section))
4022 val = bl->owner->ops->remove_location (bl, reason);
4023 else
4024 val = 0;
4025 }
4026 else
4027 {
4028 /* No -- not inserted, so no need to remove. No error. */
4029 val = 0;
4030 }
4031 }
4032
4033 /* In some cases, we might not be able to remove a breakpoint in
4034 a shared library that has already been removed, but we have
4035 not yet processed the shlib unload event. Similarly for an
4036 unloaded add-symbol-file object - the user might not yet have
4037 had the chance to remove-symbol-file it. shlib_disabled will
4038 be set if the library/object has already been removed, but
4039 the breakpoint hasn't been uninserted yet, e.g., after
4040 "nosharedlibrary" or "remove-symbol-file" with breakpoints
4041 always-inserted mode. */
4042 if (val
4043 && (bl->loc_type == bp_loc_software_breakpoint
4044 && (bl->shlib_disabled
4045 || solib_name_from_address (bl->pspace, bl->address)
4046 || shared_objfile_contains_address_p (bl->pspace,
4047 bl->address))))
4048 val = 0;
4049
4050 if (val)
4051 return val;
4052 bl->inserted = (reason == DETACH_BREAKPOINT);
4053 }
4054 else if (bl->loc_type == bp_loc_hardware_watchpoint)
4055 {
4056 gdb_assert (bl->owner->ops != NULL
4057 && bl->owner->ops->remove_location != NULL);
4058
4059 bl->inserted = (reason == DETACH_BREAKPOINT);
4060 bl->owner->ops->remove_location (bl, reason);
4061
4062 /* Failure to remove any of the hardware watchpoints comes here. */
4063 if (reason == REMOVE_BREAKPOINT && bl->inserted)
4064 warning (_("Could not remove hardware watchpoint %d."),
4065 bl->owner->number);
4066 }
4067 else if (bl->owner->type == bp_catchpoint
4068 && breakpoint_enabled (bl->owner)
4069 && !bl->duplicate)
4070 {
4071 gdb_assert (bl->owner->ops != NULL
4072 && bl->owner->ops->remove_location != NULL);
4073
4074 val = bl->owner->ops->remove_location (bl, reason);
4075 if (val)
4076 return val;
4077
4078 bl->inserted = (reason == DETACH_BREAKPOINT);
4079 }
4080
4081 return 0;
4082 }
4083
4084 static int
4085 remove_breakpoint (struct bp_location *bl)
4086 {
4087 int ret;
4088 struct cleanup *old_chain;
4089
4090 /* BL is never in moribund_locations by our callers. */
4091 gdb_assert (bl->owner != NULL);
4092
4093 /* The type of none suggests that owner is actually deleted.
4094 This should not ever happen. */
4095 gdb_assert (bl->owner->type != bp_none);
4096
4097 old_chain = save_current_space_and_thread ();
4098
4099 switch_to_program_space_and_thread (bl->pspace);
4100
4101 ret = remove_breakpoint_1 (bl, REMOVE_BREAKPOINT);
4102
4103 do_cleanups (old_chain);
4104 return ret;
4105 }
4106
4107 /* Clear the "inserted" flag in all breakpoints. */
4108
4109 void
4110 mark_breakpoints_out (void)
4111 {
4112 struct bp_location *bl, **blp_tmp;
4113
4114 ALL_BP_LOCATIONS (bl, blp_tmp)
4115 if (bl->pspace == current_program_space)
4116 bl->inserted = 0;
4117 }
4118
4119 /* Clear the "inserted" flag in all breakpoints and delete any
4120 breakpoints which should go away between runs of the program.
4121
4122 Plus other such housekeeping that has to be done for breakpoints
4123 between runs.
4124
4125 Note: this function gets called at the end of a run (by
4126 generic_mourn_inferior) and when a run begins (by
4127 init_wait_for_inferior). */
4128
4129
4130
4131 void
4132 breakpoint_init_inferior (enum inf_context context)
4133 {
4134 struct breakpoint *b, *b_tmp;
4135 struct bp_location *bl;
4136 int ix;
4137 struct program_space *pspace = current_program_space;
4138
4139 /* If breakpoint locations are shared across processes, then there's
4140 nothing to do. */
4141 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
4142 return;
4143
4144 mark_breakpoints_out ();
4145
4146 ALL_BREAKPOINTS_SAFE (b, b_tmp)
4147 {
4148 if (b->loc && b->loc->pspace != pspace)
4149 continue;
4150
4151 switch (b->type)
4152 {
4153 case bp_call_dummy:
4154 case bp_longjmp_call_dummy:
4155
4156 /* If the call dummy breakpoint is at the entry point it will
4157 cause problems when the inferior is rerun, so we better get
4158 rid of it. */
4159
4160 case bp_watchpoint_scope:
4161
4162 /* Also get rid of scope breakpoints. */
4163
4164 case bp_shlib_event:
4165
4166 /* Also remove solib event breakpoints. Their addresses may
4167 have changed since the last time we ran the program.
4168 Actually we may now be debugging against different target;
4169 and so the solib backend that installed this breakpoint may
4170 not be used in by the target. E.g.,
4171
4172 (gdb) file prog-linux
4173 (gdb) run # native linux target
4174 ...
4175 (gdb) kill
4176 (gdb) file prog-win.exe
4177 (gdb) tar rem :9999 # remote Windows gdbserver.
4178 */
4179
4180 case bp_step_resume:
4181
4182 /* Also remove step-resume breakpoints. */
4183
4184 case bp_single_step:
4185
4186 /* Also remove single-step breakpoints. */
4187
4188 delete_breakpoint (b);
4189 break;
4190
4191 case bp_watchpoint:
4192 case bp_hardware_watchpoint:
4193 case bp_read_watchpoint:
4194 case bp_access_watchpoint:
4195 {
4196 struct watchpoint *w = (struct watchpoint *) b;
4197
4198 /* Likewise for watchpoints on local expressions. */
4199 if (w->exp_valid_block != NULL)
4200 delete_breakpoint (b);
4201 else
4202 {
4203 /* Get rid of existing locations, which are no longer
4204 valid. New ones will be created in
4205 update_watchpoint, when the inferior is restarted.
4206 The next update_global_location_list call will
4207 garbage collect them. */
4208 b->loc = NULL;
4209
4210 if (context == inf_starting)
4211 {
4212 /* Reset val field to force reread of starting value in
4213 insert_breakpoints. */
4214 if (w->val)
4215 value_free (w->val);
4216 w->val = NULL;
4217 w->val_valid = 0;
4218 }
4219 }
4220 }
4221 break;
4222 default:
4223 break;
4224 }
4225 }
4226
4227 /* Get rid of the moribund locations. */
4228 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
4229 decref_bp_location (&bl);
4230 VEC_free (bp_location_p, moribund_locations);
4231 }
4232
4233 /* These functions concern about actual breakpoints inserted in the
4234 target --- to e.g. check if we need to do decr_pc adjustment or if
4235 we need to hop over the bkpt --- so we check for address space
4236 match, not program space. */
4237
4238 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4239 exists at PC. It returns ordinary_breakpoint_here if it's an
4240 ordinary breakpoint, or permanent_breakpoint_here if it's a
4241 permanent breakpoint.
4242 - When continuing from a location with an ordinary breakpoint, we
4243 actually single step once before calling insert_breakpoints.
4244 - When continuing from a location with a permanent breakpoint, we
4245 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4246 the target, to advance the PC past the breakpoint. */
4247
4248 enum breakpoint_here
4249 breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
4250 {
4251 struct bp_location *bl, **blp_tmp;
4252 int any_breakpoint_here = 0;
4253
4254 ALL_BP_LOCATIONS (bl, blp_tmp)
4255 {
4256 if (bl->loc_type != bp_loc_software_breakpoint
4257 && bl->loc_type != bp_loc_hardware_breakpoint)
4258 continue;
4259
4260 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
4261 if ((breakpoint_enabled (bl->owner)
4262 || bl->permanent)
4263 && breakpoint_location_address_match (bl, aspace, pc))
4264 {
4265 if (overlay_debugging
4266 && section_is_overlay (bl->section)
4267 && !section_is_mapped (bl->section))
4268 continue; /* unmapped overlay -- can't be a match */
4269 else if (bl->permanent)
4270 return permanent_breakpoint_here;
4271 else
4272 any_breakpoint_here = 1;
4273 }
4274 }
4275
4276 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
4277 }
4278
4279 /* See breakpoint.h. */
4280
4281 int
4282 breakpoint_in_range_p (struct address_space *aspace,
4283 CORE_ADDR addr, ULONGEST len)
4284 {
4285 struct bp_location *bl, **blp_tmp;
4286
4287 ALL_BP_LOCATIONS (bl, blp_tmp)
4288 {
4289 if (bl->loc_type != bp_loc_software_breakpoint
4290 && bl->loc_type != bp_loc_hardware_breakpoint)
4291 continue;
4292
4293 if ((breakpoint_enabled (bl->owner)
4294 || bl->permanent)
4295 && breakpoint_location_address_range_overlap (bl, aspace,
4296 addr, len))
4297 {
4298 if (overlay_debugging
4299 && section_is_overlay (bl->section)
4300 && !section_is_mapped (bl->section))
4301 {
4302 /* Unmapped overlay -- can't be a match. */
4303 continue;
4304 }
4305
4306 return 1;
4307 }
4308 }
4309
4310 return 0;
4311 }
4312
4313 /* Return true if there's a moribund breakpoint at PC. */
4314
4315 int
4316 moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
4317 {
4318 struct bp_location *loc;
4319 int ix;
4320
4321 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
4322 if (breakpoint_location_address_match (loc, aspace, pc))
4323 return 1;
4324
4325 return 0;
4326 }
4327
4328 /* Returns non-zero iff BL is inserted at PC, in address space
4329 ASPACE. */
4330
4331 static int
4332 bp_location_inserted_here_p (struct bp_location *bl,
4333 struct address_space *aspace, CORE_ADDR pc)
4334 {
4335 if (bl->inserted
4336 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4337 aspace, pc))
4338 {
4339 if (overlay_debugging
4340 && section_is_overlay (bl->section)
4341 && !section_is_mapped (bl->section))
4342 return 0; /* unmapped overlay -- can't be a match */
4343 else
4344 return 1;
4345 }
4346 return 0;
4347 }
4348
4349 /* Returns non-zero iff there's a breakpoint inserted at PC. */
4350
4351 int
4352 breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
4353 {
4354 struct bp_location **blp, **blp_tmp = NULL;
4355
4356 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4357 {
4358 struct bp_location *bl = *blp;
4359
4360 if (bl->loc_type != bp_loc_software_breakpoint
4361 && bl->loc_type != bp_loc_hardware_breakpoint)
4362 continue;
4363
4364 if (bp_location_inserted_here_p (bl, aspace, pc))
4365 return 1;
4366 }
4367 return 0;
4368 }
4369
4370 /* This function returns non-zero iff there is a software breakpoint
4371 inserted at PC. */
4372
4373 int
4374 software_breakpoint_inserted_here_p (struct address_space *aspace,
4375 CORE_ADDR pc)
4376 {
4377 struct bp_location **blp, **blp_tmp = NULL;
4378
4379 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4380 {
4381 struct bp_location *bl = *blp;
4382
4383 if (bl->loc_type != bp_loc_software_breakpoint)
4384 continue;
4385
4386 if (bp_location_inserted_here_p (bl, aspace, pc))
4387 return 1;
4388 }
4389
4390 return 0;
4391 }
4392
4393 /* See breakpoint.h. */
4394
4395 int
4396 hardware_breakpoint_inserted_here_p (struct address_space *aspace,
4397 CORE_ADDR pc)
4398 {
4399 struct bp_location **blp, **blp_tmp = NULL;
4400
4401 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4402 {
4403 struct bp_location *bl = *blp;
4404
4405 if (bl->loc_type != bp_loc_hardware_breakpoint)
4406 continue;
4407
4408 if (bp_location_inserted_here_p (bl, aspace, pc))
4409 return 1;
4410 }
4411
4412 return 0;
4413 }
4414
4415 int
4416 hardware_watchpoint_inserted_in_range (struct address_space *aspace,
4417 CORE_ADDR addr, ULONGEST len)
4418 {
4419 struct breakpoint *bpt;
4420
4421 ALL_BREAKPOINTS (bpt)
4422 {
4423 struct bp_location *loc;
4424
4425 if (bpt->type != bp_hardware_watchpoint
4426 && bpt->type != bp_access_watchpoint)
4427 continue;
4428
4429 if (!breakpoint_enabled (bpt))
4430 continue;
4431
4432 for (loc = bpt->loc; loc; loc = loc->next)
4433 if (loc->pspace->aspace == aspace && loc->inserted)
4434 {
4435 CORE_ADDR l, h;
4436
4437 /* Check for intersection. */
4438 l = std::max<CORE_ADDR> (loc->address, addr);
4439 h = std::min<CORE_ADDR> (loc->address + loc->length, addr + len);
4440 if (l < h)
4441 return 1;
4442 }
4443 }
4444 return 0;
4445 }
4446 \f
4447
4448 /* bpstat stuff. External routines' interfaces are documented
4449 in breakpoint.h. */
4450
4451 int
4452 is_catchpoint (struct breakpoint *ep)
4453 {
4454 return (ep->type == bp_catchpoint);
4455 }
4456
4457 /* Frees any storage that is part of a bpstat. Does not walk the
4458 'next' chain. */
4459
4460 static void
4461 bpstat_free (bpstat bs)
4462 {
4463 if (bs->old_val != NULL)
4464 value_free (bs->old_val);
4465 decref_counted_command_line (&bs->commands);
4466 decref_bp_location (&bs->bp_location_at);
4467 xfree (bs);
4468 }
4469
4470 /* Clear a bpstat so that it says we are not at any breakpoint.
4471 Also free any storage that is part of a bpstat. */
4472
4473 void
4474 bpstat_clear (bpstat *bsp)
4475 {
4476 bpstat p;
4477 bpstat q;
4478
4479 if (bsp == 0)
4480 return;
4481 p = *bsp;
4482 while (p != NULL)
4483 {
4484 q = p->next;
4485 bpstat_free (p);
4486 p = q;
4487 }
4488 *bsp = NULL;
4489 }
4490
4491 /* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4492 is part of the bpstat is copied as well. */
4493
4494 bpstat
4495 bpstat_copy (bpstat bs)
4496 {
4497 bpstat p = NULL;
4498 bpstat tmp;
4499 bpstat retval = NULL;
4500
4501 if (bs == NULL)
4502 return bs;
4503
4504 for (; bs != NULL; bs = bs->next)
4505 {
4506 tmp = (bpstat) xmalloc (sizeof (*tmp));
4507 memcpy (tmp, bs, sizeof (*tmp));
4508 incref_counted_command_line (tmp->commands);
4509 incref_bp_location (tmp->bp_location_at);
4510 if (bs->old_val != NULL)
4511 {
4512 tmp->old_val = value_copy (bs->old_val);
4513 release_value (tmp->old_val);
4514 }
4515
4516 if (p == NULL)
4517 /* This is the first thing in the chain. */
4518 retval = tmp;
4519 else
4520 p->next = tmp;
4521 p = tmp;
4522 }
4523 p->next = NULL;
4524 return retval;
4525 }
4526
4527 /* Find the bpstat associated with this breakpoint. */
4528
4529 bpstat
4530 bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
4531 {
4532 if (bsp == NULL)
4533 return NULL;
4534
4535 for (; bsp != NULL; bsp = bsp->next)
4536 {
4537 if (bsp->breakpoint_at == breakpoint)
4538 return bsp;
4539 }
4540 return NULL;
4541 }
4542
4543 /* See breakpoint.h. */
4544
4545 int
4546 bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
4547 {
4548 for (; bsp != NULL; bsp = bsp->next)
4549 {
4550 if (bsp->breakpoint_at == NULL)
4551 {
4552 /* A moribund location can never explain a signal other than
4553 GDB_SIGNAL_TRAP. */
4554 if (sig == GDB_SIGNAL_TRAP)
4555 return 1;
4556 }
4557 else
4558 {
4559 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4560 sig))
4561 return 1;
4562 }
4563 }
4564
4565 return 0;
4566 }
4567
4568 /* Put in *NUM the breakpoint number of the first breakpoint we are
4569 stopped at. *BSP upon return is a bpstat which points to the
4570 remaining breakpoints stopped at (but which is not guaranteed to be
4571 good for anything but further calls to bpstat_num).
4572
4573 Return 0 if passed a bpstat which does not indicate any breakpoints.
4574 Return -1 if stopped at a breakpoint that has been deleted since
4575 we set it.
4576 Return 1 otherwise. */
4577
4578 int
4579 bpstat_num (bpstat *bsp, int *num)
4580 {
4581 struct breakpoint *b;
4582
4583 if ((*bsp) == NULL)
4584 return 0; /* No more breakpoint values */
4585
4586 /* We assume we'll never have several bpstats that correspond to a
4587 single breakpoint -- otherwise, this function might return the
4588 same number more than once and this will look ugly. */
4589 b = (*bsp)->breakpoint_at;
4590 *bsp = (*bsp)->next;
4591 if (b == NULL)
4592 return -1; /* breakpoint that's been deleted since */
4593
4594 *num = b->number; /* We have its number */
4595 return 1;
4596 }
4597
4598 /* See breakpoint.h. */
4599
4600 void
4601 bpstat_clear_actions (void)
4602 {
4603 struct thread_info *tp;
4604 bpstat bs;
4605
4606 if (ptid_equal (inferior_ptid, null_ptid))
4607 return;
4608
4609 tp = find_thread_ptid (inferior_ptid);
4610 if (tp == NULL)
4611 return;
4612
4613 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
4614 {
4615 decref_counted_command_line (&bs->commands);
4616
4617 if (bs->old_val != NULL)
4618 {
4619 value_free (bs->old_val);
4620 bs->old_val = NULL;
4621 }
4622 }
4623 }
4624
4625 /* Called when a command is about to proceed the inferior. */
4626
4627 static void
4628 breakpoint_about_to_proceed (void)
4629 {
4630 if (!ptid_equal (inferior_ptid, null_ptid))
4631 {
4632 struct thread_info *tp = inferior_thread ();
4633
4634 /* Allow inferior function calls in breakpoint commands to not
4635 interrupt the command list. When the call finishes
4636 successfully, the inferior will be standing at the same
4637 breakpoint as if nothing happened. */
4638 if (tp->control.in_infcall)
4639 return;
4640 }
4641
4642 breakpoint_proceeded = 1;
4643 }
4644
4645 /* Stub for cleaning up our state if we error-out of a breakpoint
4646 command. */
4647 static void
4648 cleanup_executing_breakpoints (void *ignore)
4649 {
4650 executing_breakpoint_commands = 0;
4651 }
4652
4653 /* Return non-zero iff CMD as the first line of a command sequence is `silent'
4654 or its equivalent. */
4655
4656 static int
4657 command_line_is_silent (struct command_line *cmd)
4658 {
4659 return cmd && (strcmp ("silent", cmd->line) == 0);
4660 }
4661
4662 /* Execute all the commands associated with all the breakpoints at
4663 this location. Any of these commands could cause the process to
4664 proceed beyond this point, etc. We look out for such changes by
4665 checking the global "breakpoint_proceeded" after each command.
4666
4667 Returns true if a breakpoint command resumed the inferior. In that
4668 case, it is the caller's responsibility to recall it again with the
4669 bpstat of the current thread. */
4670
4671 static int
4672 bpstat_do_actions_1 (bpstat *bsp)
4673 {
4674 bpstat bs;
4675 struct cleanup *old_chain;
4676 int again = 0;
4677
4678 /* Avoid endless recursion if a `source' command is contained
4679 in bs->commands. */
4680 if (executing_breakpoint_commands)
4681 return 0;
4682
4683 executing_breakpoint_commands = 1;
4684 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
4685
4686 prevent_dont_repeat ();
4687
4688 /* This pointer will iterate over the list of bpstat's. */
4689 bs = *bsp;
4690
4691 breakpoint_proceeded = 0;
4692 for (; bs != NULL; bs = bs->next)
4693 {
4694 struct counted_command_line *ccmd;
4695 struct command_line *cmd;
4696 struct cleanup *this_cmd_tree_chain;
4697
4698 /* Take ownership of the BSP's command tree, if it has one.
4699
4700 The command tree could legitimately contain commands like
4701 'step' and 'next', which call clear_proceed_status, which
4702 frees stop_bpstat's command tree. To make sure this doesn't
4703 free the tree we're executing out from under us, we need to
4704 take ownership of the tree ourselves. Since a given bpstat's
4705 commands are only executed once, we don't need to copy it; we
4706 can clear the pointer in the bpstat, and make sure we free
4707 the tree when we're done. */
4708 ccmd = bs->commands;
4709 bs->commands = NULL;
4710 this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
4711 cmd = ccmd ? ccmd->commands : NULL;
4712 if (command_line_is_silent (cmd))
4713 {
4714 /* The action has been already done by bpstat_stop_status. */
4715 cmd = cmd->next;
4716 }
4717
4718 while (cmd != NULL)
4719 {
4720 execute_control_command (cmd);
4721
4722 if (breakpoint_proceeded)
4723 break;
4724 else
4725 cmd = cmd->next;
4726 }
4727
4728 /* We can free this command tree now. */
4729 do_cleanups (this_cmd_tree_chain);
4730
4731 if (breakpoint_proceeded)
4732 {
4733 if (current_ui->async)
4734 /* If we are in async mode, then the target might be still
4735 running, not stopped at any breakpoint, so nothing for
4736 us to do here -- just return to the event loop. */
4737 ;
4738 else
4739 /* In sync mode, when execute_control_command returns
4740 we're already standing on the next breakpoint.
4741 Breakpoint commands for that stop were not run, since
4742 execute_command does not run breakpoint commands --
4743 only command_line_handler does, but that one is not
4744 involved in execution of breakpoint commands. So, we
4745 can now execute breakpoint commands. It should be
4746 noted that making execute_command do bpstat actions is
4747 not an option -- in this case we'll have recursive
4748 invocation of bpstat for each breakpoint with a
4749 command, and can easily blow up GDB stack. Instead, we
4750 return true, which will trigger the caller to recall us
4751 with the new stop_bpstat. */
4752 again = 1;
4753 break;
4754 }
4755 }
4756 do_cleanups (old_chain);
4757 return again;
4758 }
4759
4760 void
4761 bpstat_do_actions (void)
4762 {
4763 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4764
4765 /* Do any commands attached to breakpoint we are stopped at. */
4766 while (!ptid_equal (inferior_ptid, null_ptid)
4767 && target_has_execution
4768 && !is_exited (inferior_ptid)
4769 && !is_executing (inferior_ptid))
4770 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4771 and only return when it is stopped at the next breakpoint, we
4772 keep doing breakpoint actions until it returns false to
4773 indicate the inferior was not resumed. */
4774 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
4775 break;
4776
4777 discard_cleanups (cleanup_if_error);
4778 }
4779
4780 /* Print out the (old or new) value associated with a watchpoint. */
4781
4782 static void
4783 watchpoint_value_print (struct value *val, struct ui_file *stream)
4784 {
4785 if (val == NULL)
4786 fprintf_unfiltered (stream, _("<unreadable>"));
4787 else
4788 {
4789 struct value_print_options opts;
4790 get_user_print_options (&opts);
4791 value_print (val, stream, &opts);
4792 }
4793 }
4794
4795 /* Print the "Thread ID hit" part of "Thread ID hit Breakpoint N" if
4796 debugging multiple threads. */
4797
4798 void
4799 maybe_print_thread_hit_breakpoint (struct ui_out *uiout)
4800 {
4801 if (ui_out_is_mi_like_p (uiout))
4802 return;
4803
4804 ui_out_text (uiout, "\n");
4805
4806 if (show_thread_that_caused_stop ())
4807 {
4808 const char *name;
4809 struct thread_info *thr = inferior_thread ();
4810
4811 ui_out_text (uiout, "Thread ");
4812 ui_out_field_fmt (uiout, "thread-id", "%s", print_thread_id (thr));
4813
4814 name = thr->name != NULL ? thr->name : target_thread_name (thr);
4815 if (name != NULL)
4816 {
4817 ui_out_text (uiout, " \"");
4818 ui_out_field_fmt (uiout, "name", "%s", name);
4819 ui_out_text (uiout, "\"");
4820 }
4821
4822 ui_out_text (uiout, " hit ");
4823 }
4824 }
4825
4826 /* Generic routine for printing messages indicating why we
4827 stopped. The behavior of this function depends on the value
4828 'print_it' in the bpstat structure. Under some circumstances we
4829 may decide not to print anything here and delegate the task to
4830 normal_stop(). */
4831
4832 static enum print_stop_action
4833 print_bp_stop_message (bpstat bs)
4834 {
4835 switch (bs->print_it)
4836 {
4837 case print_it_noop:
4838 /* Nothing should be printed for this bpstat entry. */
4839 return PRINT_UNKNOWN;
4840 break;
4841
4842 case print_it_done:
4843 /* We still want to print the frame, but we already printed the
4844 relevant messages. */
4845 return PRINT_SRC_AND_LOC;
4846 break;
4847
4848 case print_it_normal:
4849 {
4850 struct breakpoint *b = bs->breakpoint_at;
4851
4852 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4853 which has since been deleted. */
4854 if (b == NULL)
4855 return PRINT_UNKNOWN;
4856
4857 /* Normal case. Call the breakpoint's print_it method. */
4858 return b->ops->print_it (bs);
4859 }
4860 break;
4861
4862 default:
4863 internal_error (__FILE__, __LINE__,
4864 _("print_bp_stop_message: unrecognized enum value"));
4865 break;
4866 }
4867 }
4868
4869 /* A helper function that prints a shared library stopped event. */
4870
4871 static void
4872 print_solib_event (int is_catchpoint)
4873 {
4874 int any_deleted
4875 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4876 int any_added
4877 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4878
4879 if (!is_catchpoint)
4880 {
4881 if (any_added || any_deleted)
4882 ui_out_text (current_uiout,
4883 _("Stopped due to shared library event:\n"));
4884 else
4885 ui_out_text (current_uiout,
4886 _("Stopped due to shared library event (no "
4887 "libraries added or removed)\n"));
4888 }
4889
4890 if (ui_out_is_mi_like_p (current_uiout))
4891 ui_out_field_string (current_uiout, "reason",
4892 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4893
4894 if (any_deleted)
4895 {
4896 struct cleanup *cleanup;
4897 char *name;
4898 int ix;
4899
4900 ui_out_text (current_uiout, _(" Inferior unloaded "));
4901 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4902 "removed");
4903 for (ix = 0;
4904 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4905 ix, name);
4906 ++ix)
4907 {
4908 if (ix > 0)
4909 ui_out_text (current_uiout, " ");
4910 ui_out_field_string (current_uiout, "library", name);
4911 ui_out_text (current_uiout, "\n");
4912 }
4913
4914 do_cleanups (cleanup);
4915 }
4916
4917 if (any_added)
4918 {
4919 struct so_list *iter;
4920 int ix;
4921 struct cleanup *cleanup;
4922
4923 ui_out_text (current_uiout, _(" Inferior loaded "));
4924 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4925 "added");
4926 for (ix = 0;
4927 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4928 ix, iter);
4929 ++ix)
4930 {
4931 if (ix > 0)
4932 ui_out_text (current_uiout, " ");
4933 ui_out_field_string (current_uiout, "library", iter->so_name);
4934 ui_out_text (current_uiout, "\n");
4935 }
4936
4937 do_cleanups (cleanup);
4938 }
4939 }
4940
4941 /* Print a message indicating what happened. This is called from
4942 normal_stop(). The input to this routine is the head of the bpstat
4943 list - a list of the eventpoints that caused this stop. KIND is
4944 the target_waitkind for the stopping event. This
4945 routine calls the generic print routine for printing a message
4946 about reasons for stopping. This will print (for example) the
4947 "Breakpoint n," part of the output. The return value of this
4948 routine is one of:
4949
4950 PRINT_UNKNOWN: Means we printed nothing.
4951 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4952 code to print the location. An example is
4953 "Breakpoint 1, " which should be followed by
4954 the location.
4955 PRINT_SRC_ONLY: Means we printed something, but there is no need
4956 to also print the location part of the message.
4957 An example is the catch/throw messages, which
4958 don't require a location appended to the end.
4959 PRINT_NOTHING: We have done some printing and we don't need any
4960 further info to be printed. */
4961
4962 enum print_stop_action
4963 bpstat_print (bpstat bs, int kind)
4964 {
4965 enum print_stop_action val;
4966
4967 /* Maybe another breakpoint in the chain caused us to stop.
4968 (Currently all watchpoints go on the bpstat whether hit or not.
4969 That probably could (should) be changed, provided care is taken
4970 with respect to bpstat_explains_signal). */
4971 for (; bs; bs = bs->next)
4972 {
4973 val = print_bp_stop_message (bs);
4974 if (val == PRINT_SRC_ONLY
4975 || val == PRINT_SRC_AND_LOC
4976 || val == PRINT_NOTHING)
4977 return val;
4978 }
4979
4980 /* If we had hit a shared library event breakpoint,
4981 print_bp_stop_message would print out this message. If we hit an
4982 OS-level shared library event, do the same thing. */
4983 if (kind == TARGET_WAITKIND_LOADED)
4984 {
4985 print_solib_event (0);
4986 return PRINT_NOTHING;
4987 }
4988
4989 /* We reached the end of the chain, or we got a null BS to start
4990 with and nothing was printed. */
4991 return PRINT_UNKNOWN;
4992 }
4993
4994 /* Evaluate the expression EXP and return 1 if value is zero.
4995 This returns the inverse of the condition because it is called
4996 from catch_errors which returns 0 if an exception happened, and if an
4997 exception happens we want execution to stop.
4998 The argument is a "struct expression *" that has been cast to a
4999 "void *" to make it pass through catch_errors. */
5000
5001 static int
5002 breakpoint_cond_eval (void *exp)
5003 {
5004 struct value *mark = value_mark ();
5005 int i = !value_true (evaluate_expression ((struct expression *) exp));
5006
5007 value_free_to_mark (mark);
5008 return i;
5009 }
5010
5011 /* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
5012
5013 static bpstat
5014 bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
5015 {
5016 bpstat bs;
5017
5018 bs = (bpstat) xmalloc (sizeof (*bs));
5019 bs->next = NULL;
5020 **bs_link_pointer = bs;
5021 *bs_link_pointer = &bs->next;
5022 bs->breakpoint_at = bl->owner;
5023 bs->bp_location_at = bl;
5024 incref_bp_location (bl);
5025 /* If the condition is false, etc., don't do the commands. */
5026 bs->commands = NULL;
5027 bs->old_val = NULL;
5028 bs->print_it = print_it_normal;
5029 return bs;
5030 }
5031 \f
5032 /* The target has stopped with waitstatus WS. Check if any hardware
5033 watchpoints have triggered, according to the target. */
5034
5035 int
5036 watchpoints_triggered (struct target_waitstatus *ws)
5037 {
5038 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
5039 CORE_ADDR addr;
5040 struct breakpoint *b;
5041
5042 if (!stopped_by_watchpoint)
5043 {
5044 /* We were not stopped by a watchpoint. Mark all watchpoints
5045 as not triggered. */
5046 ALL_BREAKPOINTS (b)
5047 if (is_hardware_watchpoint (b))
5048 {
5049 struct watchpoint *w = (struct watchpoint *) b;
5050
5051 w->watchpoint_triggered = watch_triggered_no;
5052 }
5053
5054 return 0;
5055 }
5056
5057 if (!target_stopped_data_address (&current_target, &addr))
5058 {
5059 /* We were stopped by a watchpoint, but we don't know where.
5060 Mark all watchpoints as unknown. */
5061 ALL_BREAKPOINTS (b)
5062 if (is_hardware_watchpoint (b))
5063 {
5064 struct watchpoint *w = (struct watchpoint *) b;
5065
5066 w->watchpoint_triggered = watch_triggered_unknown;
5067 }
5068
5069 return 1;
5070 }
5071
5072 /* The target could report the data address. Mark watchpoints
5073 affected by this data address as triggered, and all others as not
5074 triggered. */
5075
5076 ALL_BREAKPOINTS (b)
5077 if (is_hardware_watchpoint (b))
5078 {
5079 struct watchpoint *w = (struct watchpoint *) b;
5080 struct bp_location *loc;
5081
5082 w->watchpoint_triggered = watch_triggered_no;
5083 for (loc = b->loc; loc; loc = loc->next)
5084 {
5085 if (is_masked_watchpoint (b))
5086 {
5087 CORE_ADDR newaddr = addr & w->hw_wp_mask;
5088 CORE_ADDR start = loc->address & w->hw_wp_mask;
5089
5090 if (newaddr == start)
5091 {
5092 w->watchpoint_triggered = watch_triggered_yes;
5093 break;
5094 }
5095 }
5096 /* Exact match not required. Within range is sufficient. */
5097 else if (target_watchpoint_addr_within_range (&current_target,
5098 addr, loc->address,
5099 loc->length))
5100 {
5101 w->watchpoint_triggered = watch_triggered_yes;
5102 break;
5103 }
5104 }
5105 }
5106
5107 return 1;
5108 }
5109
5110 /* Possible return values for watchpoint_check (this can't be an enum
5111 because of check_errors). */
5112 /* The watchpoint has been deleted. */
5113 #define WP_DELETED 1
5114 /* The value has changed. */
5115 #define WP_VALUE_CHANGED 2
5116 /* The value has not changed. */
5117 #define WP_VALUE_NOT_CHANGED 3
5118 /* Ignore this watchpoint, no matter if the value changed or not. */
5119 #define WP_IGNORE 4
5120
5121 #define BP_TEMPFLAG 1
5122 #define BP_HARDWAREFLAG 2
5123
5124 /* Evaluate watchpoint condition expression and check if its value
5125 changed.
5126
5127 P should be a pointer to struct bpstat, but is defined as a void *
5128 in order for this function to be usable with catch_errors. */
5129
5130 static int
5131 watchpoint_check (void *p)
5132 {
5133 bpstat bs = (bpstat) p;
5134 struct watchpoint *b;
5135 struct frame_info *fr;
5136 int within_current_scope;
5137
5138 /* BS is built from an existing struct breakpoint. */
5139 gdb_assert (bs->breakpoint_at != NULL);
5140 b = (struct watchpoint *) bs->breakpoint_at;
5141
5142 /* If this is a local watchpoint, we only want to check if the
5143 watchpoint frame is in scope if the current thread is the thread
5144 that was used to create the watchpoint. */
5145 if (!watchpoint_in_thread_scope (b))
5146 return WP_IGNORE;
5147
5148 if (b->exp_valid_block == NULL)
5149 within_current_scope = 1;
5150 else
5151 {
5152 struct frame_info *frame = get_current_frame ();
5153 struct gdbarch *frame_arch = get_frame_arch (frame);
5154 CORE_ADDR frame_pc = get_frame_pc (frame);
5155
5156 /* stack_frame_destroyed_p() returns a non-zero value if we're
5157 still in the function but the stack frame has already been
5158 invalidated. Since we can't rely on the values of local
5159 variables after the stack has been destroyed, we are treating
5160 the watchpoint in that state as `not changed' without further
5161 checking. Don't mark watchpoints as changed if the current
5162 frame is in an epilogue - even if they are in some other
5163 frame, our view of the stack is likely to be wrong and
5164 frame_find_by_id could error out. */
5165 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
5166 return WP_IGNORE;
5167
5168 fr = frame_find_by_id (b->watchpoint_frame);
5169 within_current_scope = (fr != NULL);
5170
5171 /* If we've gotten confused in the unwinder, we might have
5172 returned a frame that can't describe this variable. */
5173 if (within_current_scope)
5174 {
5175 struct symbol *function;
5176
5177 function = get_frame_function (fr);
5178 if (function == NULL
5179 || !contained_in (b->exp_valid_block,
5180 SYMBOL_BLOCK_VALUE (function)))
5181 within_current_scope = 0;
5182 }
5183
5184 if (within_current_scope)
5185 /* If we end up stopping, the current frame will get selected
5186 in normal_stop. So this call to select_frame won't affect
5187 the user. */
5188 select_frame (fr);
5189 }
5190
5191 if (within_current_scope)
5192 {
5193 /* We use value_{,free_to_}mark because it could be a *long*
5194 time before we return to the command level and call
5195 free_all_values. We can't call free_all_values because we
5196 might be in the middle of evaluating a function call. */
5197
5198 int pc = 0;
5199 struct value *mark;
5200 struct value *new_val;
5201
5202 if (is_masked_watchpoint (&b->base))
5203 /* Since we don't know the exact trigger address (from
5204 stopped_data_address), just tell the user we've triggered
5205 a mask watchpoint. */
5206 return WP_VALUE_CHANGED;
5207
5208 mark = value_mark ();
5209 fetch_subexp_value (b->exp.get (), &pc, &new_val, NULL, NULL, 0);
5210
5211 if (b->val_bitsize != 0)
5212 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5213
5214 /* We use value_equal_contents instead of value_equal because
5215 the latter coerces an array to a pointer, thus comparing just
5216 the address of the array instead of its contents. This is
5217 not what we want. */
5218 if ((b->val != NULL) != (new_val != NULL)
5219 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
5220 {
5221 if (new_val != NULL)
5222 {
5223 release_value (new_val);
5224 value_free_to_mark (mark);
5225 }
5226 bs->old_val = b->val;
5227 b->val = new_val;
5228 b->val_valid = 1;
5229 return WP_VALUE_CHANGED;
5230 }
5231 else
5232 {
5233 /* Nothing changed. */
5234 value_free_to_mark (mark);
5235 return WP_VALUE_NOT_CHANGED;
5236 }
5237 }
5238 else
5239 {
5240 /* This seems like the only logical thing to do because
5241 if we temporarily ignored the watchpoint, then when
5242 we reenter the block in which it is valid it contains
5243 garbage (in the case of a function, it may have two
5244 garbage values, one before and one after the prologue).
5245 So we can't even detect the first assignment to it and
5246 watch after that (since the garbage may or may not equal
5247 the first value assigned). */
5248 /* We print all the stop information in
5249 breakpoint_ops->print_it, but in this case, by the time we
5250 call breakpoint_ops->print_it this bp will be deleted
5251 already. So we have no choice but print the information
5252 here. */
5253
5254 SWITCH_THRU_ALL_UIS ()
5255 {
5256 struct ui_out *uiout = current_uiout;
5257
5258 if (ui_out_is_mi_like_p (uiout))
5259 ui_out_field_string
5260 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
5261 ui_out_text (uiout, "\nWatchpoint ");
5262 ui_out_field_int (uiout, "wpnum", b->base.number);
5263 ui_out_text (uiout,
5264 " deleted because the program has left the block in\n"
5265 "which its expression is valid.\n");
5266 }
5267
5268 /* Make sure the watchpoint's commands aren't executed. */
5269 decref_counted_command_line (&b->base.commands);
5270 watchpoint_del_at_next_stop (b);
5271
5272 return WP_DELETED;
5273 }
5274 }
5275
5276 /* Return true if it looks like target has stopped due to hitting
5277 breakpoint location BL. This function does not check if we should
5278 stop, only if BL explains the stop. */
5279
5280 static int
5281 bpstat_check_location (const struct bp_location *bl,
5282 struct address_space *aspace, CORE_ADDR bp_addr,
5283 const struct target_waitstatus *ws)
5284 {
5285 struct breakpoint *b = bl->owner;
5286
5287 /* BL is from an existing breakpoint. */
5288 gdb_assert (b != NULL);
5289
5290 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
5291 }
5292
5293 /* Determine if the watched values have actually changed, and we
5294 should stop. If not, set BS->stop to 0. */
5295
5296 static void
5297 bpstat_check_watchpoint (bpstat bs)
5298 {
5299 const struct bp_location *bl;
5300 struct watchpoint *b;
5301
5302 /* BS is built for existing struct breakpoint. */
5303 bl = bs->bp_location_at;
5304 gdb_assert (bl != NULL);
5305 b = (struct watchpoint *) bs->breakpoint_at;
5306 gdb_assert (b != NULL);
5307
5308 {
5309 int must_check_value = 0;
5310
5311 if (b->base.type == bp_watchpoint)
5312 /* For a software watchpoint, we must always check the
5313 watched value. */
5314 must_check_value = 1;
5315 else if (b->watchpoint_triggered == watch_triggered_yes)
5316 /* We have a hardware watchpoint (read, write, or access)
5317 and the target earlier reported an address watched by
5318 this watchpoint. */
5319 must_check_value = 1;
5320 else if (b->watchpoint_triggered == watch_triggered_unknown
5321 && b->base.type == bp_hardware_watchpoint)
5322 /* We were stopped by a hardware watchpoint, but the target could
5323 not report the data address. We must check the watchpoint's
5324 value. Access and read watchpoints are out of luck; without
5325 a data address, we can't figure it out. */
5326 must_check_value = 1;
5327
5328 if (must_check_value)
5329 {
5330 char *message
5331 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
5332 b->base.number);
5333 struct cleanup *cleanups = make_cleanup (xfree, message);
5334 int e = catch_errors (watchpoint_check, bs, message,
5335 RETURN_MASK_ALL);
5336 do_cleanups (cleanups);
5337 switch (e)
5338 {
5339 case WP_DELETED:
5340 /* We've already printed what needs to be printed. */
5341 bs->print_it = print_it_done;
5342 /* Stop. */
5343 break;
5344 case WP_IGNORE:
5345 bs->print_it = print_it_noop;
5346 bs->stop = 0;
5347 break;
5348 case WP_VALUE_CHANGED:
5349 if (b->base.type == bp_read_watchpoint)
5350 {
5351 /* There are two cases to consider here:
5352
5353 1. We're watching the triggered memory for reads.
5354 In that case, trust the target, and always report
5355 the watchpoint hit to the user. Even though
5356 reads don't cause value changes, the value may
5357 have changed since the last time it was read, and
5358 since we're not trapping writes, we will not see
5359 those, and as such we should ignore our notion of
5360 old value.
5361
5362 2. We're watching the triggered memory for both
5363 reads and writes. There are two ways this may
5364 happen:
5365
5366 2.1. This is a target that can't break on data
5367 reads only, but can break on accesses (reads or
5368 writes), such as e.g., x86. We detect this case
5369 at the time we try to insert read watchpoints.
5370
5371 2.2. Otherwise, the target supports read
5372 watchpoints, but, the user set an access or write
5373 watchpoint watching the same memory as this read
5374 watchpoint.
5375
5376 If we're watching memory writes as well as reads,
5377 ignore watchpoint hits when we find that the
5378 value hasn't changed, as reads don't cause
5379 changes. This still gives false positives when
5380 the program writes the same value to memory as
5381 what there was already in memory (we will confuse
5382 it for a read), but it's much better than
5383 nothing. */
5384
5385 int other_write_watchpoint = 0;
5386
5387 if (bl->watchpoint_type == hw_read)
5388 {
5389 struct breakpoint *other_b;
5390
5391 ALL_BREAKPOINTS (other_b)
5392 if (other_b->type == bp_hardware_watchpoint
5393 || other_b->type == bp_access_watchpoint)
5394 {
5395 struct watchpoint *other_w =
5396 (struct watchpoint *) other_b;
5397
5398 if (other_w->watchpoint_triggered
5399 == watch_triggered_yes)
5400 {
5401 other_write_watchpoint = 1;
5402 break;
5403 }
5404 }
5405 }
5406
5407 if (other_write_watchpoint
5408 || bl->watchpoint_type == hw_access)
5409 {
5410 /* We're watching the same memory for writes,
5411 and the value changed since the last time we
5412 updated it, so this trap must be for a write.
5413 Ignore it. */
5414 bs->print_it = print_it_noop;
5415 bs->stop = 0;
5416 }
5417 }
5418 break;
5419 case WP_VALUE_NOT_CHANGED:
5420 if (b->base.type == bp_hardware_watchpoint
5421 || b->base.type == bp_watchpoint)
5422 {
5423 /* Don't stop: write watchpoints shouldn't fire if
5424 the value hasn't changed. */
5425 bs->print_it = print_it_noop;
5426 bs->stop = 0;
5427 }
5428 /* Stop. */
5429 break;
5430 default:
5431 /* Can't happen. */
5432 case 0:
5433 /* Error from catch_errors. */
5434 {
5435 SWITCH_THRU_ALL_UIS ()
5436 {
5437 printf_filtered (_("Watchpoint %d deleted.\n"),
5438 b->base.number);
5439 }
5440 watchpoint_del_at_next_stop (b);
5441 /* We've already printed what needs to be printed. */
5442 bs->print_it = print_it_done;
5443 }
5444 break;
5445 }
5446 }
5447 else /* must_check_value == 0 */
5448 {
5449 /* This is a case where some watchpoint(s) triggered, but
5450 not at the address of this watchpoint, or else no
5451 watchpoint triggered after all. So don't print
5452 anything for this watchpoint. */
5453 bs->print_it = print_it_noop;
5454 bs->stop = 0;
5455 }
5456 }
5457 }
5458
5459 /* For breakpoints that are currently marked as telling gdb to stop,
5460 check conditions (condition proper, frame, thread and ignore count)
5461 of breakpoint referred to by BS. If we should not stop for this
5462 breakpoint, set BS->stop to 0. */
5463
5464 static void
5465 bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5466 {
5467 const struct bp_location *bl;
5468 struct breakpoint *b;
5469 int value_is_zero = 0;
5470 struct expression *cond;
5471
5472 gdb_assert (bs->stop);
5473
5474 /* BS is built for existing struct breakpoint. */
5475 bl = bs->bp_location_at;
5476 gdb_assert (bl != NULL);
5477 b = bs->breakpoint_at;
5478 gdb_assert (b != NULL);
5479
5480 /* Even if the target evaluated the condition on its end and notified GDB, we
5481 need to do so again since GDB does not know if we stopped due to a
5482 breakpoint or a single step breakpoint. */
5483
5484 if (frame_id_p (b->frame_id)
5485 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
5486 {
5487 bs->stop = 0;
5488 return;
5489 }
5490
5491 /* If this is a thread/task-specific breakpoint, don't waste cpu
5492 evaluating the condition if this isn't the specified
5493 thread/task. */
5494 if ((b->thread != -1 && b->thread != ptid_to_global_thread_id (ptid))
5495 || (b->task != 0 && b->task != ada_get_task_number (ptid)))
5496
5497 {
5498 bs->stop = 0;
5499 return;
5500 }
5501
5502 /* Evaluate extension language breakpoints that have a "stop" method
5503 implemented. */
5504 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
5505
5506 if (is_watchpoint (b))
5507 {
5508 struct watchpoint *w = (struct watchpoint *) b;
5509
5510 cond = w->cond_exp.get ();
5511 }
5512 else
5513 cond = bl->cond.get ();
5514
5515 if (cond && b->disposition != disp_del_at_next_stop)
5516 {
5517 int within_current_scope = 1;
5518 struct watchpoint * w;
5519
5520 /* We use value_mark and value_free_to_mark because it could
5521 be a long time before we return to the command level and
5522 call free_all_values. We can't call free_all_values
5523 because we might be in the middle of evaluating a
5524 function call. */
5525 struct value *mark = value_mark ();
5526
5527 if (is_watchpoint (b))
5528 w = (struct watchpoint *) b;
5529 else
5530 w = NULL;
5531
5532 /* Need to select the frame, with all that implies so that
5533 the conditions will have the right context. Because we
5534 use the frame, we will not see an inlined function's
5535 variables when we arrive at a breakpoint at the start
5536 of the inlined function; the current frame will be the
5537 call site. */
5538 if (w == NULL || w->cond_exp_valid_block == NULL)
5539 select_frame (get_current_frame ());
5540 else
5541 {
5542 struct frame_info *frame;
5543
5544 /* For local watchpoint expressions, which particular
5545 instance of a local is being watched matters, so we
5546 keep track of the frame to evaluate the expression
5547 in. To evaluate the condition however, it doesn't
5548 really matter which instantiation of the function
5549 where the condition makes sense triggers the
5550 watchpoint. This allows an expression like "watch
5551 global if q > 10" set in `func', catch writes to
5552 global on all threads that call `func', or catch
5553 writes on all recursive calls of `func' by a single
5554 thread. We simply always evaluate the condition in
5555 the innermost frame that's executing where it makes
5556 sense to evaluate the condition. It seems
5557 intuitive. */
5558 frame = block_innermost_frame (w->cond_exp_valid_block);
5559 if (frame != NULL)
5560 select_frame (frame);
5561 else
5562 within_current_scope = 0;
5563 }
5564 if (within_current_scope)
5565 value_is_zero
5566 = catch_errors (breakpoint_cond_eval, cond,
5567 "Error in testing breakpoint condition:\n",
5568 RETURN_MASK_ALL);
5569 else
5570 {
5571 warning (_("Watchpoint condition cannot be tested "
5572 "in the current scope"));
5573 /* If we failed to set the right context for this
5574 watchpoint, unconditionally report it. */
5575 value_is_zero = 0;
5576 }
5577 /* FIXME-someday, should give breakpoint #. */
5578 value_free_to_mark (mark);
5579 }
5580
5581 if (cond && value_is_zero)
5582 {
5583 bs->stop = 0;
5584 }
5585 else if (b->ignore_count > 0)
5586 {
5587 b->ignore_count--;
5588 bs->stop = 0;
5589 /* Increase the hit count even though we don't stop. */
5590 ++(b->hit_count);
5591 observer_notify_breakpoint_modified (b);
5592 }
5593 }
5594
5595 /* Returns true if we need to track moribund locations of LOC's type
5596 on the current target. */
5597
5598 static int
5599 need_moribund_for_location_type (struct bp_location *loc)
5600 {
5601 return ((loc->loc_type == bp_loc_software_breakpoint
5602 && !target_supports_stopped_by_sw_breakpoint ())
5603 || (loc->loc_type == bp_loc_hardware_breakpoint
5604 && !target_supports_stopped_by_hw_breakpoint ()));
5605 }
5606
5607
5608 /* Get a bpstat associated with having just stopped at address
5609 BP_ADDR in thread PTID.
5610
5611 Determine whether we stopped at a breakpoint, etc, or whether we
5612 don't understand this stop. Result is a chain of bpstat's such
5613 that:
5614
5615 if we don't understand the stop, the result is a null pointer.
5616
5617 if we understand why we stopped, the result is not null.
5618
5619 Each element of the chain refers to a particular breakpoint or
5620 watchpoint at which we have stopped. (We may have stopped for
5621 several reasons concurrently.)
5622
5623 Each element of the chain has valid next, breakpoint_at,
5624 commands, FIXME??? fields. */
5625
5626 bpstat
5627 bpstat_stop_status (struct address_space *aspace,
5628 CORE_ADDR bp_addr, ptid_t ptid,
5629 const struct target_waitstatus *ws)
5630 {
5631 struct breakpoint *b = NULL;
5632 struct bp_location *bl;
5633 struct bp_location *loc;
5634 /* First item of allocated bpstat's. */
5635 bpstat bs_head = NULL, *bs_link = &bs_head;
5636 /* Pointer to the last thing in the chain currently. */
5637 bpstat bs;
5638 int ix;
5639 int need_remove_insert;
5640 int removed_any;
5641
5642 /* First, build the bpstat chain with locations that explain a
5643 target stop, while being careful to not set the target running,
5644 as that may invalidate locations (in particular watchpoint
5645 locations are recreated). Resuming will happen here with
5646 breakpoint conditions or watchpoint expressions that include
5647 inferior function calls. */
5648
5649 ALL_BREAKPOINTS (b)
5650 {
5651 if (!breakpoint_enabled (b))
5652 continue;
5653
5654 for (bl = b->loc; bl != NULL; bl = bl->next)
5655 {
5656 /* For hardware watchpoints, we look only at the first
5657 location. The watchpoint_check function will work on the
5658 entire expression, not the individual locations. For
5659 read watchpoints, the watchpoints_triggered function has
5660 checked all locations already. */
5661 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5662 break;
5663
5664 if (!bl->enabled || bl->shlib_disabled)
5665 continue;
5666
5667 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
5668 continue;
5669
5670 /* Come here if it's a watchpoint, or if the break address
5671 matches. */
5672
5673 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
5674 explain stop. */
5675
5676 /* Assume we stop. Should we find a watchpoint that is not
5677 actually triggered, or if the condition of the breakpoint
5678 evaluates as false, we'll reset 'stop' to 0. */
5679 bs->stop = 1;
5680 bs->print = 1;
5681
5682 /* If this is a scope breakpoint, mark the associated
5683 watchpoint as triggered so that we will handle the
5684 out-of-scope event. We'll get to the watchpoint next
5685 iteration. */
5686 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
5687 {
5688 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5689
5690 w->watchpoint_triggered = watch_triggered_yes;
5691 }
5692 }
5693 }
5694
5695 /* Check if a moribund breakpoint explains the stop. */
5696 if (!target_supports_stopped_by_sw_breakpoint ()
5697 || !target_supports_stopped_by_hw_breakpoint ())
5698 {
5699 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
5700 {
5701 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5702 && need_moribund_for_location_type (loc))
5703 {
5704 bs = bpstat_alloc (loc, &bs_link);
5705 /* For hits of moribund locations, we should just proceed. */
5706 bs->stop = 0;
5707 bs->print = 0;
5708 bs->print_it = print_it_noop;
5709 }
5710 }
5711 }
5712
5713 /* A bit of special processing for shlib breakpoints. We need to
5714 process solib loading here, so that the lists of loaded and
5715 unloaded libraries are correct before we handle "catch load" and
5716 "catch unload". */
5717 for (bs = bs_head; bs != NULL; bs = bs->next)
5718 {
5719 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
5720 {
5721 handle_solib_event ();
5722 break;
5723 }
5724 }
5725
5726 /* Now go through the locations that caused the target to stop, and
5727 check whether we're interested in reporting this stop to higher
5728 layers, or whether we should resume the target transparently. */
5729
5730 removed_any = 0;
5731
5732 for (bs = bs_head; bs != NULL; bs = bs->next)
5733 {
5734 if (!bs->stop)
5735 continue;
5736
5737 b = bs->breakpoint_at;
5738 b->ops->check_status (bs);
5739 if (bs->stop)
5740 {
5741 bpstat_check_breakpoint_conditions (bs, ptid);
5742
5743 if (bs->stop)
5744 {
5745 ++(b->hit_count);
5746 observer_notify_breakpoint_modified (b);
5747
5748 /* We will stop here. */
5749 if (b->disposition == disp_disable)
5750 {
5751 --(b->enable_count);
5752 if (b->enable_count <= 0)
5753 b->enable_state = bp_disabled;
5754 removed_any = 1;
5755 }
5756 if (b->silent)
5757 bs->print = 0;
5758 bs->commands = b->commands;
5759 incref_counted_command_line (bs->commands);
5760 if (command_line_is_silent (bs->commands
5761 ? bs->commands->commands : NULL))
5762 bs->print = 0;
5763
5764 b->ops->after_condition_true (bs);
5765 }
5766
5767 }
5768
5769 /* Print nothing for this entry if we don't stop or don't
5770 print. */
5771 if (!bs->stop || !bs->print)
5772 bs->print_it = print_it_noop;
5773 }
5774
5775 /* If we aren't stopping, the value of some hardware watchpoint may
5776 not have changed, but the intermediate memory locations we are
5777 watching may have. Don't bother if we're stopping; this will get
5778 done later. */
5779 need_remove_insert = 0;
5780 if (! bpstat_causes_stop (bs_head))
5781 for (bs = bs_head; bs != NULL; bs = bs->next)
5782 if (!bs->stop
5783 && bs->breakpoint_at
5784 && is_hardware_watchpoint (bs->breakpoint_at))
5785 {
5786 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5787
5788 update_watchpoint (w, 0 /* don't reparse. */);
5789 need_remove_insert = 1;
5790 }
5791
5792 if (need_remove_insert)
5793 update_global_location_list (UGLL_MAY_INSERT);
5794 else if (removed_any)
5795 update_global_location_list (UGLL_DONT_INSERT);
5796
5797 return bs_head;
5798 }
5799
5800 static void
5801 handle_jit_event (void)
5802 {
5803 struct frame_info *frame;
5804 struct gdbarch *gdbarch;
5805
5806 if (debug_infrun)
5807 fprintf_unfiltered (gdb_stdlog, "handling bp_jit_event\n");
5808
5809 /* Switch terminal for any messages produced by
5810 breakpoint_re_set. */
5811 target_terminal_ours_for_output ();
5812
5813 frame = get_current_frame ();
5814 gdbarch = get_frame_arch (frame);
5815
5816 jit_event_handler (gdbarch);
5817
5818 target_terminal_inferior ();
5819 }
5820
5821 /* Prepare WHAT final decision for infrun. */
5822
5823 /* Decide what infrun needs to do with this bpstat. */
5824
5825 struct bpstat_what
5826 bpstat_what (bpstat bs_head)
5827 {
5828 struct bpstat_what retval;
5829 bpstat bs;
5830
5831 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
5832 retval.call_dummy = STOP_NONE;
5833 retval.is_longjmp = 0;
5834
5835 for (bs = bs_head; bs != NULL; bs = bs->next)
5836 {
5837 /* Extract this BS's action. After processing each BS, we check
5838 if its action overrides all we've seem so far. */
5839 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5840 enum bptype bptype;
5841
5842 if (bs->breakpoint_at == NULL)
5843 {
5844 /* I suspect this can happen if it was a momentary
5845 breakpoint which has since been deleted. */
5846 bptype = bp_none;
5847 }
5848 else
5849 bptype = bs->breakpoint_at->type;
5850
5851 switch (bptype)
5852 {
5853 case bp_none:
5854 break;
5855 case bp_breakpoint:
5856 case bp_hardware_breakpoint:
5857 case bp_single_step:
5858 case bp_until:
5859 case bp_finish:
5860 case bp_shlib_event:
5861 if (bs->stop)
5862 {
5863 if (bs->print)
5864 this_action = BPSTAT_WHAT_STOP_NOISY;
5865 else
5866 this_action = BPSTAT_WHAT_STOP_SILENT;
5867 }
5868 else
5869 this_action = BPSTAT_WHAT_SINGLE;
5870 break;
5871 case bp_watchpoint:
5872 case bp_hardware_watchpoint:
5873 case bp_read_watchpoint:
5874 case bp_access_watchpoint:
5875 if (bs->stop)
5876 {
5877 if (bs->print)
5878 this_action = BPSTAT_WHAT_STOP_NOISY;
5879 else
5880 this_action = BPSTAT_WHAT_STOP_SILENT;
5881 }
5882 else
5883 {
5884 /* There was a watchpoint, but we're not stopping.
5885 This requires no further action. */
5886 }
5887 break;
5888 case bp_longjmp:
5889 case bp_longjmp_call_dummy:
5890 case bp_exception:
5891 if (bs->stop)
5892 {
5893 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5894 retval.is_longjmp = bptype != bp_exception;
5895 }
5896 else
5897 this_action = BPSTAT_WHAT_SINGLE;
5898 break;
5899 case bp_longjmp_resume:
5900 case bp_exception_resume:
5901 if (bs->stop)
5902 {
5903 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5904 retval.is_longjmp = bptype == bp_longjmp_resume;
5905 }
5906 else
5907 this_action = BPSTAT_WHAT_SINGLE;
5908 break;
5909 case bp_step_resume:
5910 if (bs->stop)
5911 this_action = BPSTAT_WHAT_STEP_RESUME;
5912 else
5913 {
5914 /* It is for the wrong frame. */
5915 this_action = BPSTAT_WHAT_SINGLE;
5916 }
5917 break;
5918 case bp_hp_step_resume:
5919 if (bs->stop)
5920 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5921 else
5922 {
5923 /* It is for the wrong frame. */
5924 this_action = BPSTAT_WHAT_SINGLE;
5925 }
5926 break;
5927 case bp_watchpoint_scope:
5928 case bp_thread_event:
5929 case bp_overlay_event:
5930 case bp_longjmp_master:
5931 case bp_std_terminate_master:
5932 case bp_exception_master:
5933 this_action = BPSTAT_WHAT_SINGLE;
5934 break;
5935 case bp_catchpoint:
5936 if (bs->stop)
5937 {
5938 if (bs->print)
5939 this_action = BPSTAT_WHAT_STOP_NOISY;
5940 else
5941 this_action = BPSTAT_WHAT_STOP_SILENT;
5942 }
5943 else
5944 {
5945 /* There was a catchpoint, but we're not stopping.
5946 This requires no further action. */
5947 }
5948 break;
5949 case bp_jit_event:
5950 this_action = BPSTAT_WHAT_SINGLE;
5951 break;
5952 case bp_call_dummy:
5953 /* Make sure the action is stop (silent or noisy),
5954 so infrun.c pops the dummy frame. */
5955 retval.call_dummy = STOP_STACK_DUMMY;
5956 this_action = BPSTAT_WHAT_STOP_SILENT;
5957 break;
5958 case bp_std_terminate:
5959 /* Make sure the action is stop (silent or noisy),
5960 so infrun.c pops the dummy frame. */
5961 retval.call_dummy = STOP_STD_TERMINATE;
5962 this_action = BPSTAT_WHAT_STOP_SILENT;
5963 break;
5964 case bp_tracepoint:
5965 case bp_fast_tracepoint:
5966 case bp_static_tracepoint:
5967 /* Tracepoint hits should not be reported back to GDB, and
5968 if one got through somehow, it should have been filtered
5969 out already. */
5970 internal_error (__FILE__, __LINE__,
5971 _("bpstat_what: tracepoint encountered"));
5972 break;
5973 case bp_gnu_ifunc_resolver:
5974 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5975 this_action = BPSTAT_WHAT_SINGLE;
5976 break;
5977 case bp_gnu_ifunc_resolver_return:
5978 /* The breakpoint will be removed, execution will restart from the
5979 PC of the former breakpoint. */
5980 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5981 break;
5982
5983 case bp_dprintf:
5984 if (bs->stop)
5985 this_action = BPSTAT_WHAT_STOP_SILENT;
5986 else
5987 this_action = BPSTAT_WHAT_SINGLE;
5988 break;
5989
5990 default:
5991 internal_error (__FILE__, __LINE__,
5992 _("bpstat_what: unhandled bptype %d"), (int) bptype);
5993 }
5994
5995 retval.main_action = std::max (retval.main_action, this_action);
5996 }
5997
5998 return retval;
5999 }
6000
6001 void
6002 bpstat_run_callbacks (bpstat bs_head)
6003 {
6004 bpstat bs;
6005
6006 for (bs = bs_head; bs != NULL; bs = bs->next)
6007 {
6008 struct breakpoint *b = bs->breakpoint_at;
6009
6010 if (b == NULL)
6011 continue;
6012 switch (b->type)
6013 {
6014 case bp_jit_event:
6015 handle_jit_event ();
6016 break;
6017 case bp_gnu_ifunc_resolver:
6018 gnu_ifunc_resolver_stop (b);
6019 break;
6020 case bp_gnu_ifunc_resolver_return:
6021 gnu_ifunc_resolver_return_stop (b);
6022 break;
6023 }
6024 }
6025 }
6026
6027 /* Nonzero if we should step constantly (e.g. watchpoints on machines
6028 without hardware support). This isn't related to a specific bpstat,
6029 just to things like whether watchpoints are set. */
6030
6031 int
6032 bpstat_should_step (void)
6033 {
6034 struct breakpoint *b;
6035
6036 ALL_BREAKPOINTS (b)
6037 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
6038 return 1;
6039 return 0;
6040 }
6041
6042 int
6043 bpstat_causes_stop (bpstat bs)
6044 {
6045 for (; bs != NULL; bs = bs->next)
6046 if (bs->stop)
6047 return 1;
6048
6049 return 0;
6050 }
6051
6052 \f
6053
6054 /* Compute a string of spaces suitable to indent the next line
6055 so it starts at the position corresponding to the table column
6056 named COL_NAME in the currently active table of UIOUT. */
6057
6058 static char *
6059 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
6060 {
6061 static char wrap_indent[80];
6062 int i, total_width, width, align;
6063 char *text;
6064
6065 total_width = 0;
6066 for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
6067 {
6068 if (strcmp (text, col_name) == 0)
6069 {
6070 gdb_assert (total_width < sizeof wrap_indent);
6071 memset (wrap_indent, ' ', total_width);
6072 wrap_indent[total_width] = 0;
6073
6074 return wrap_indent;
6075 }
6076
6077 total_width += width + 1;
6078 }
6079
6080 return NULL;
6081 }
6082
6083 /* Determine if the locations of this breakpoint will have their conditions
6084 evaluated by the target, host or a mix of both. Returns the following:
6085
6086 "host": Host evals condition.
6087 "host or target": Host or Target evals condition.
6088 "target": Target evals condition.
6089 */
6090
6091 static const char *
6092 bp_condition_evaluator (struct breakpoint *b)
6093 {
6094 struct bp_location *bl;
6095 char host_evals = 0;
6096 char target_evals = 0;
6097
6098 if (!b)
6099 return NULL;
6100
6101 if (!is_breakpoint (b))
6102 return NULL;
6103
6104 if (gdb_evaluates_breakpoint_condition_p ()
6105 || !target_supports_evaluation_of_breakpoint_conditions ())
6106 return condition_evaluation_host;
6107
6108 for (bl = b->loc; bl; bl = bl->next)
6109 {
6110 if (bl->cond_bytecode)
6111 target_evals++;
6112 else
6113 host_evals++;
6114 }
6115
6116 if (host_evals && target_evals)
6117 return condition_evaluation_both;
6118 else if (target_evals)
6119 return condition_evaluation_target;
6120 else
6121 return condition_evaluation_host;
6122 }
6123
6124 /* Determine the breakpoint location's condition evaluator. This is
6125 similar to bp_condition_evaluator, but for locations. */
6126
6127 static const char *
6128 bp_location_condition_evaluator (struct bp_location *bl)
6129 {
6130 if (bl && !is_breakpoint (bl->owner))
6131 return NULL;
6132
6133 if (gdb_evaluates_breakpoint_condition_p ()
6134 || !target_supports_evaluation_of_breakpoint_conditions ())
6135 return condition_evaluation_host;
6136
6137 if (bl && bl->cond_bytecode)
6138 return condition_evaluation_target;
6139 else
6140 return condition_evaluation_host;
6141 }
6142
6143 /* Print the LOC location out of the list of B->LOC locations. */
6144
6145 static void
6146 print_breakpoint_location (struct breakpoint *b,
6147 struct bp_location *loc)
6148 {
6149 struct ui_out *uiout = current_uiout;
6150 struct cleanup *old_chain = save_current_program_space ();
6151
6152 if (loc != NULL && loc->shlib_disabled)
6153 loc = NULL;
6154
6155 if (loc != NULL)
6156 set_current_program_space (loc->pspace);
6157
6158 if (b->display_canonical)
6159 ui_out_field_string (uiout, "what",
6160 event_location_to_string (b->location));
6161 else if (loc && loc->symtab)
6162 {
6163 struct symbol *sym
6164 = find_pc_sect_function (loc->address, loc->section);
6165 if (sym)
6166 {
6167 ui_out_text (uiout, "in ");
6168 ui_out_field_string (uiout, "func",
6169 SYMBOL_PRINT_NAME (sym));
6170 ui_out_text (uiout, " ");
6171 ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
6172 ui_out_text (uiout, "at ");
6173 }
6174 ui_out_field_string (uiout, "file",
6175 symtab_to_filename_for_display (loc->symtab));
6176 ui_out_text (uiout, ":");
6177
6178 if (ui_out_is_mi_like_p (uiout))
6179 ui_out_field_string (uiout, "fullname",
6180 symtab_to_fullname (loc->symtab));
6181
6182 ui_out_field_int (uiout, "line", loc->line_number);
6183 }
6184 else if (loc)
6185 {
6186 struct ui_file *stb = mem_fileopen ();
6187 struct cleanup *stb_chain = make_cleanup_ui_file_delete (stb);
6188
6189 print_address_symbolic (loc->gdbarch, loc->address, stb,
6190 demangle, "");
6191 ui_out_field_stream (uiout, "at", stb);
6192
6193 do_cleanups (stb_chain);
6194 }
6195 else
6196 {
6197 ui_out_field_string (uiout, "pending",
6198 event_location_to_string (b->location));
6199 /* If extra_string is available, it could be holding a condition
6200 or dprintf arguments. In either case, make sure it is printed,
6201 too, but only for non-MI streams. */
6202 if (!ui_out_is_mi_like_p (uiout) && b->extra_string != NULL)
6203 {
6204 if (b->type == bp_dprintf)
6205 ui_out_text (uiout, ",");
6206 else
6207 ui_out_text (uiout, " ");
6208 ui_out_text (uiout, b->extra_string);
6209 }
6210 }
6211
6212 if (loc && is_breakpoint (b)
6213 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6214 && bp_condition_evaluator (b) == condition_evaluation_both)
6215 {
6216 ui_out_text (uiout, " (");
6217 ui_out_field_string (uiout, "evaluated-by",
6218 bp_location_condition_evaluator (loc));
6219 ui_out_text (uiout, ")");
6220 }
6221
6222 do_cleanups (old_chain);
6223 }
6224
6225 static const char *
6226 bptype_string (enum bptype type)
6227 {
6228 struct ep_type_description
6229 {
6230 enum bptype type;
6231 char *description;
6232 };
6233 static struct ep_type_description bptypes[] =
6234 {
6235 {bp_none, "?deleted?"},
6236 {bp_breakpoint, "breakpoint"},
6237 {bp_hardware_breakpoint, "hw breakpoint"},
6238 {bp_single_step, "sw single-step"},
6239 {bp_until, "until"},
6240 {bp_finish, "finish"},
6241 {bp_watchpoint, "watchpoint"},
6242 {bp_hardware_watchpoint, "hw watchpoint"},
6243 {bp_read_watchpoint, "read watchpoint"},
6244 {bp_access_watchpoint, "acc watchpoint"},
6245 {bp_longjmp, "longjmp"},
6246 {bp_longjmp_resume, "longjmp resume"},
6247 {bp_longjmp_call_dummy, "longjmp for call dummy"},
6248 {bp_exception, "exception"},
6249 {bp_exception_resume, "exception resume"},
6250 {bp_step_resume, "step resume"},
6251 {bp_hp_step_resume, "high-priority step resume"},
6252 {bp_watchpoint_scope, "watchpoint scope"},
6253 {bp_call_dummy, "call dummy"},
6254 {bp_std_terminate, "std::terminate"},
6255 {bp_shlib_event, "shlib events"},
6256 {bp_thread_event, "thread events"},
6257 {bp_overlay_event, "overlay events"},
6258 {bp_longjmp_master, "longjmp master"},
6259 {bp_std_terminate_master, "std::terminate master"},
6260 {bp_exception_master, "exception master"},
6261 {bp_catchpoint, "catchpoint"},
6262 {bp_tracepoint, "tracepoint"},
6263 {bp_fast_tracepoint, "fast tracepoint"},
6264 {bp_static_tracepoint, "static tracepoint"},
6265 {bp_dprintf, "dprintf"},
6266 {bp_jit_event, "jit events"},
6267 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6268 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
6269 };
6270
6271 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6272 || ((int) type != bptypes[(int) type].type))
6273 internal_error (__FILE__, __LINE__,
6274 _("bptypes table does not describe type #%d."),
6275 (int) type);
6276
6277 return bptypes[(int) type].description;
6278 }
6279
6280 /* For MI, output a field named 'thread-groups' with a list as the value.
6281 For CLI, prefix the list with the string 'inf'. */
6282
6283 static void
6284 output_thread_groups (struct ui_out *uiout,
6285 const char *field_name,
6286 VEC(int) *inf_num,
6287 int mi_only)
6288 {
6289 struct cleanup *back_to;
6290 int is_mi = ui_out_is_mi_like_p (uiout);
6291 int inf;
6292 int i;
6293
6294 /* For backward compatibility, don't display inferiors in CLI unless
6295 there are several. Always display them for MI. */
6296 if (!is_mi && mi_only)
6297 return;
6298
6299 back_to = make_cleanup_ui_out_list_begin_end (uiout, field_name);
6300
6301 for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
6302 {
6303 if (is_mi)
6304 {
6305 char mi_group[10];
6306
6307 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
6308 ui_out_field_string (uiout, NULL, mi_group);
6309 }
6310 else
6311 {
6312 if (i == 0)
6313 ui_out_text (uiout, " inf ");
6314 else
6315 ui_out_text (uiout, ", ");
6316
6317 ui_out_text (uiout, plongest (inf));
6318 }
6319 }
6320
6321 do_cleanups (back_to);
6322 }
6323
6324 /* Print B to gdb_stdout. */
6325
6326 static void
6327 print_one_breakpoint_location (struct breakpoint *b,
6328 struct bp_location *loc,
6329 int loc_number,
6330 struct bp_location **last_loc,
6331 int allflag)
6332 {
6333 struct command_line *l;
6334 static char bpenables[] = "nynny";
6335
6336 struct ui_out *uiout = current_uiout;
6337 int header_of_multiple = 0;
6338 int part_of_multiple = (loc != NULL);
6339 struct value_print_options opts;
6340
6341 get_user_print_options (&opts);
6342
6343 gdb_assert (!loc || loc_number != 0);
6344 /* See comment in print_one_breakpoint concerning treatment of
6345 breakpoints with single disabled location. */
6346 if (loc == NULL
6347 && (b->loc != NULL
6348 && (b->loc->next != NULL || !b->loc->enabled)))
6349 header_of_multiple = 1;
6350 if (loc == NULL)
6351 loc = b->loc;
6352
6353 annotate_record ();
6354
6355 /* 1 */
6356 annotate_field (0);
6357 if (part_of_multiple)
6358 {
6359 char *formatted;
6360 formatted = xstrprintf ("%d.%d", b->number, loc_number);
6361 ui_out_field_string (uiout, "number", formatted);
6362 xfree (formatted);
6363 }
6364 else
6365 {
6366 ui_out_field_int (uiout, "number", b->number);
6367 }
6368
6369 /* 2 */
6370 annotate_field (1);
6371 if (part_of_multiple)
6372 ui_out_field_skip (uiout, "type");
6373 else
6374 ui_out_field_string (uiout, "type", bptype_string (b->type));
6375
6376 /* 3 */
6377 annotate_field (2);
6378 if (part_of_multiple)
6379 ui_out_field_skip (uiout, "disp");
6380 else
6381 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
6382
6383
6384 /* 4 */
6385 annotate_field (3);
6386 if (part_of_multiple)
6387 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
6388 else
6389 ui_out_field_fmt (uiout, "enabled", "%c",
6390 bpenables[(int) b->enable_state]);
6391 ui_out_spaces (uiout, 2);
6392
6393
6394 /* 5 and 6 */
6395 if (b->ops != NULL && b->ops->print_one != NULL)
6396 {
6397 /* Although the print_one can possibly print all locations,
6398 calling it here is not likely to get any nice result. So,
6399 make sure there's just one location. */
6400 gdb_assert (b->loc == NULL || b->loc->next == NULL);
6401 b->ops->print_one (b, last_loc);
6402 }
6403 else
6404 switch (b->type)
6405 {
6406 case bp_none:
6407 internal_error (__FILE__, __LINE__,
6408 _("print_one_breakpoint: bp_none encountered\n"));
6409 break;
6410
6411 case bp_watchpoint:
6412 case bp_hardware_watchpoint:
6413 case bp_read_watchpoint:
6414 case bp_access_watchpoint:
6415 {
6416 struct watchpoint *w = (struct watchpoint *) b;
6417
6418 /* Field 4, the address, is omitted (which makes the columns
6419 not line up too nicely with the headers, but the effect
6420 is relatively readable). */
6421 if (opts.addressprint)
6422 ui_out_field_skip (uiout, "addr");
6423 annotate_field (5);
6424 ui_out_field_string (uiout, "what", w->exp_string);
6425 }
6426 break;
6427
6428 case bp_breakpoint:
6429 case bp_hardware_breakpoint:
6430 case bp_single_step:
6431 case bp_until:
6432 case bp_finish:
6433 case bp_longjmp:
6434 case bp_longjmp_resume:
6435 case bp_longjmp_call_dummy:
6436 case bp_exception:
6437 case bp_exception_resume:
6438 case bp_step_resume:
6439 case bp_hp_step_resume:
6440 case bp_watchpoint_scope:
6441 case bp_call_dummy:
6442 case bp_std_terminate:
6443 case bp_shlib_event:
6444 case bp_thread_event:
6445 case bp_overlay_event:
6446 case bp_longjmp_master:
6447 case bp_std_terminate_master:
6448 case bp_exception_master:
6449 case bp_tracepoint:
6450 case bp_fast_tracepoint:
6451 case bp_static_tracepoint:
6452 case bp_dprintf:
6453 case bp_jit_event:
6454 case bp_gnu_ifunc_resolver:
6455 case bp_gnu_ifunc_resolver_return:
6456 if (opts.addressprint)
6457 {
6458 annotate_field (4);
6459 if (header_of_multiple)
6460 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
6461 else if (b->loc == NULL || loc->shlib_disabled)
6462 ui_out_field_string (uiout, "addr", "<PENDING>");
6463 else
6464 ui_out_field_core_addr (uiout, "addr",
6465 loc->gdbarch, loc->address);
6466 }
6467 annotate_field (5);
6468 if (!header_of_multiple)
6469 print_breakpoint_location (b, loc);
6470 if (b->loc)
6471 *last_loc = b->loc;
6472 break;
6473 }
6474
6475
6476 if (loc != NULL && !header_of_multiple)
6477 {
6478 struct inferior *inf;
6479 VEC(int) *inf_num = NULL;
6480 int mi_only = 1;
6481
6482 ALL_INFERIORS (inf)
6483 {
6484 if (inf->pspace == loc->pspace)
6485 VEC_safe_push (int, inf_num, inf->num);
6486 }
6487
6488 /* For backward compatibility, don't display inferiors in CLI unless
6489 there are several. Always display for MI. */
6490 if (allflag
6491 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6492 && (number_of_program_spaces () > 1
6493 || number_of_inferiors () > 1)
6494 /* LOC is for existing B, it cannot be in
6495 moribund_locations and thus having NULL OWNER. */
6496 && loc->owner->type != bp_catchpoint))
6497 mi_only = 0;
6498 output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6499 VEC_free (int, inf_num);
6500 }
6501
6502 if (!part_of_multiple)
6503 {
6504 if (b->thread != -1)
6505 {
6506 /* FIXME: This seems to be redundant and lost here; see the
6507 "stop only in" line a little further down. */
6508 ui_out_text (uiout, " thread ");
6509 ui_out_field_int (uiout, "thread", b->thread);
6510 }
6511 else if (b->task != 0)
6512 {
6513 ui_out_text (uiout, " task ");
6514 ui_out_field_int (uiout, "task", b->task);
6515 }
6516 }
6517
6518 ui_out_text (uiout, "\n");
6519
6520 if (!part_of_multiple)
6521 b->ops->print_one_detail (b, uiout);
6522
6523 if (part_of_multiple && frame_id_p (b->frame_id))
6524 {
6525 annotate_field (6);
6526 ui_out_text (uiout, "\tstop only in stack frame at ");
6527 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
6528 the frame ID. */
6529 ui_out_field_core_addr (uiout, "frame",
6530 b->gdbarch, b->frame_id.stack_addr);
6531 ui_out_text (uiout, "\n");
6532 }
6533
6534 if (!part_of_multiple && b->cond_string)
6535 {
6536 annotate_field (7);
6537 if (is_tracepoint (b))
6538 ui_out_text (uiout, "\ttrace only if ");
6539 else
6540 ui_out_text (uiout, "\tstop only if ");
6541 ui_out_field_string (uiout, "cond", b->cond_string);
6542
6543 /* Print whether the target is doing the breakpoint's condition
6544 evaluation. If GDB is doing the evaluation, don't print anything. */
6545 if (is_breakpoint (b)
6546 && breakpoint_condition_evaluation_mode ()
6547 == condition_evaluation_target)
6548 {
6549 ui_out_text (uiout, " (");
6550 ui_out_field_string (uiout, "evaluated-by",
6551 bp_condition_evaluator (b));
6552 ui_out_text (uiout, " evals)");
6553 }
6554 ui_out_text (uiout, "\n");
6555 }
6556
6557 if (!part_of_multiple && b->thread != -1)
6558 {
6559 /* FIXME should make an annotation for this. */
6560 ui_out_text (uiout, "\tstop only in thread ");
6561 if (ui_out_is_mi_like_p (uiout))
6562 ui_out_field_int (uiout, "thread", b->thread);
6563 else
6564 {
6565 struct thread_info *thr = find_thread_global_id (b->thread);
6566
6567 ui_out_field_string (uiout, "thread", print_thread_id (thr));
6568 }
6569 ui_out_text (uiout, "\n");
6570 }
6571
6572 if (!part_of_multiple)
6573 {
6574 if (b->hit_count)
6575 {
6576 /* FIXME should make an annotation for this. */
6577 if (is_catchpoint (b))
6578 ui_out_text (uiout, "\tcatchpoint");
6579 else if (is_tracepoint (b))
6580 ui_out_text (uiout, "\ttracepoint");
6581 else
6582 ui_out_text (uiout, "\tbreakpoint");
6583 ui_out_text (uiout, " already hit ");
6584 ui_out_field_int (uiout, "times", b->hit_count);
6585 if (b->hit_count == 1)
6586 ui_out_text (uiout, " time\n");
6587 else
6588 ui_out_text (uiout, " times\n");
6589 }
6590 else
6591 {
6592 /* Output the count also if it is zero, but only if this is mi. */
6593 if (ui_out_is_mi_like_p (uiout))
6594 ui_out_field_int (uiout, "times", b->hit_count);
6595 }
6596 }
6597
6598 if (!part_of_multiple && b->ignore_count)
6599 {
6600 annotate_field (8);
6601 ui_out_text (uiout, "\tignore next ");
6602 ui_out_field_int (uiout, "ignore", b->ignore_count);
6603 ui_out_text (uiout, " hits\n");
6604 }
6605
6606 /* Note that an enable count of 1 corresponds to "enable once"
6607 behavior, which is reported by the combination of enablement and
6608 disposition, so we don't need to mention it here. */
6609 if (!part_of_multiple && b->enable_count > 1)
6610 {
6611 annotate_field (8);
6612 ui_out_text (uiout, "\tdisable after ");
6613 /* Tweak the wording to clarify that ignore and enable counts
6614 are distinct, and have additive effect. */
6615 if (b->ignore_count)
6616 ui_out_text (uiout, "additional ");
6617 else
6618 ui_out_text (uiout, "next ");
6619 ui_out_field_int (uiout, "enable", b->enable_count);
6620 ui_out_text (uiout, " hits\n");
6621 }
6622
6623 if (!part_of_multiple && is_tracepoint (b))
6624 {
6625 struct tracepoint *tp = (struct tracepoint *) b;
6626
6627 if (tp->traceframe_usage)
6628 {
6629 ui_out_text (uiout, "\ttrace buffer usage ");
6630 ui_out_field_int (uiout, "traceframe-usage", tp->traceframe_usage);
6631 ui_out_text (uiout, " bytes\n");
6632 }
6633 }
6634
6635 l = b->commands ? b->commands->commands : NULL;
6636 if (!part_of_multiple && l)
6637 {
6638 struct cleanup *script_chain;
6639
6640 annotate_field (9);
6641 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
6642 print_command_lines (uiout, l, 4);
6643 do_cleanups (script_chain);
6644 }
6645
6646 if (is_tracepoint (b))
6647 {
6648 struct tracepoint *t = (struct tracepoint *) b;
6649
6650 if (!part_of_multiple && t->pass_count)
6651 {
6652 annotate_field (10);
6653 ui_out_text (uiout, "\tpass count ");
6654 ui_out_field_int (uiout, "pass", t->pass_count);
6655 ui_out_text (uiout, " \n");
6656 }
6657
6658 /* Don't display it when tracepoint or tracepoint location is
6659 pending. */
6660 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6661 {
6662 annotate_field (11);
6663
6664 if (ui_out_is_mi_like_p (uiout))
6665 ui_out_field_string (uiout, "installed",
6666 loc->inserted ? "y" : "n");
6667 else
6668 {
6669 if (loc->inserted)
6670 ui_out_text (uiout, "\t");
6671 else
6672 ui_out_text (uiout, "\tnot ");
6673 ui_out_text (uiout, "installed on target\n");
6674 }
6675 }
6676 }
6677
6678 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
6679 {
6680 if (is_watchpoint (b))
6681 {
6682 struct watchpoint *w = (struct watchpoint *) b;
6683
6684 ui_out_field_string (uiout, "original-location", w->exp_string);
6685 }
6686 else if (b->location != NULL
6687 && event_location_to_string (b->location) != NULL)
6688 ui_out_field_string (uiout, "original-location",
6689 event_location_to_string (b->location));
6690 }
6691 }
6692
6693 static void
6694 print_one_breakpoint (struct breakpoint *b,
6695 struct bp_location **last_loc,
6696 int allflag)
6697 {
6698 struct cleanup *bkpt_chain;
6699 struct ui_out *uiout = current_uiout;
6700
6701 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
6702
6703 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
6704 do_cleanups (bkpt_chain);
6705
6706 /* If this breakpoint has custom print function,
6707 it's already printed. Otherwise, print individual
6708 locations, if any. */
6709 if (b->ops == NULL || b->ops->print_one == NULL)
6710 {
6711 /* If breakpoint has a single location that is disabled, we
6712 print it as if it had several locations, since otherwise it's
6713 hard to represent "breakpoint enabled, location disabled"
6714 situation.
6715
6716 Note that while hardware watchpoints have several locations
6717 internally, that's not a property exposed to user. */
6718 if (b->loc
6719 && !is_hardware_watchpoint (b)
6720 && (b->loc->next || !b->loc->enabled))
6721 {
6722 struct bp_location *loc;
6723 int n = 1;
6724
6725 for (loc = b->loc; loc; loc = loc->next, ++n)
6726 {
6727 struct cleanup *inner2 =
6728 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
6729 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
6730 do_cleanups (inner2);
6731 }
6732 }
6733 }
6734 }
6735
6736 static int
6737 breakpoint_address_bits (struct breakpoint *b)
6738 {
6739 int print_address_bits = 0;
6740 struct bp_location *loc;
6741
6742 /* Software watchpoints that aren't watching memory don't have an
6743 address to print. */
6744 if (is_no_memory_software_watchpoint (b))
6745 return 0;
6746
6747 for (loc = b->loc; loc; loc = loc->next)
6748 {
6749 int addr_bit;
6750
6751 addr_bit = gdbarch_addr_bit (loc->gdbarch);
6752 if (addr_bit > print_address_bits)
6753 print_address_bits = addr_bit;
6754 }
6755
6756 return print_address_bits;
6757 }
6758
6759 struct captured_breakpoint_query_args
6760 {
6761 int bnum;
6762 };
6763
6764 static int
6765 do_captured_breakpoint_query (struct ui_out *uiout, void *data)
6766 {
6767 struct captured_breakpoint_query_args *args
6768 = (struct captured_breakpoint_query_args *) data;
6769 struct breakpoint *b;
6770 struct bp_location *dummy_loc = NULL;
6771
6772 ALL_BREAKPOINTS (b)
6773 {
6774 if (args->bnum == b->number)
6775 {
6776 print_one_breakpoint (b, &dummy_loc, 0);
6777 return GDB_RC_OK;
6778 }
6779 }
6780 return GDB_RC_NONE;
6781 }
6782
6783 enum gdb_rc
6784 gdb_breakpoint_query (struct ui_out *uiout, int bnum,
6785 char **error_message)
6786 {
6787 struct captured_breakpoint_query_args args;
6788
6789 args.bnum = bnum;
6790 /* For the moment we don't trust print_one_breakpoint() to not throw
6791 an error. */
6792 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
6793 error_message, RETURN_MASK_ALL) < 0)
6794 return GDB_RC_FAIL;
6795 else
6796 return GDB_RC_OK;
6797 }
6798
6799 /* Return true if this breakpoint was set by the user, false if it is
6800 internal or momentary. */
6801
6802 int
6803 user_breakpoint_p (struct breakpoint *b)
6804 {
6805 return b->number > 0;
6806 }
6807
6808 /* See breakpoint.h. */
6809
6810 int
6811 pending_breakpoint_p (struct breakpoint *b)
6812 {
6813 return b->loc == NULL;
6814 }
6815
6816 /* Print information on user settable breakpoint (watchpoint, etc)
6817 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6818 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6819 FILTER is non-NULL, call it on each breakpoint and only include the
6820 ones for which it returns non-zero. Return the total number of
6821 breakpoints listed. */
6822
6823 static int
6824 breakpoint_1 (char *args, int allflag,
6825 int (*filter) (const struct breakpoint *))
6826 {
6827 struct breakpoint *b;
6828 struct bp_location *last_loc = NULL;
6829 int nr_printable_breakpoints;
6830 struct cleanup *bkpttbl_chain;
6831 struct value_print_options opts;
6832 int print_address_bits = 0;
6833 int print_type_col_width = 14;
6834 struct ui_out *uiout = current_uiout;
6835
6836 get_user_print_options (&opts);
6837
6838 /* Compute the number of rows in the table, as well as the size
6839 required for address fields. */
6840 nr_printable_breakpoints = 0;
6841 ALL_BREAKPOINTS (b)
6842 {
6843 /* If we have a filter, only list the breakpoints it accepts. */
6844 if (filter && !filter (b))
6845 continue;
6846
6847 /* If we have an "args" string, it is a list of breakpoints to
6848 accept. Skip the others. */
6849 if (args != NULL && *args != '\0')
6850 {
6851 if (allflag && parse_and_eval_long (args) != b->number)
6852 continue;
6853 if (!allflag && !number_is_in_list (args, b->number))
6854 continue;
6855 }
6856
6857 if (allflag || user_breakpoint_p (b))
6858 {
6859 int addr_bit, type_len;
6860
6861 addr_bit = breakpoint_address_bits (b);
6862 if (addr_bit > print_address_bits)
6863 print_address_bits = addr_bit;
6864
6865 type_len = strlen (bptype_string (b->type));
6866 if (type_len > print_type_col_width)
6867 print_type_col_width = type_len;
6868
6869 nr_printable_breakpoints++;
6870 }
6871 }
6872
6873 if (opts.addressprint)
6874 bkpttbl_chain
6875 = make_cleanup_ui_out_table_begin_end (uiout, 6,
6876 nr_printable_breakpoints,
6877 "BreakpointTable");
6878 else
6879 bkpttbl_chain
6880 = make_cleanup_ui_out_table_begin_end (uiout, 5,
6881 nr_printable_breakpoints,
6882 "BreakpointTable");
6883
6884 if (nr_printable_breakpoints > 0)
6885 annotate_breakpoints_headers ();
6886 if (nr_printable_breakpoints > 0)
6887 annotate_field (0);
6888 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
6889 if (nr_printable_breakpoints > 0)
6890 annotate_field (1);
6891 ui_out_table_header (uiout, print_type_col_width, ui_left,
6892 "type", "Type"); /* 2 */
6893 if (nr_printable_breakpoints > 0)
6894 annotate_field (2);
6895 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
6896 if (nr_printable_breakpoints > 0)
6897 annotate_field (3);
6898 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
6899 if (opts.addressprint)
6900 {
6901 if (nr_printable_breakpoints > 0)
6902 annotate_field (4);
6903 if (print_address_bits <= 32)
6904 ui_out_table_header (uiout, 10, ui_left,
6905 "addr", "Address"); /* 5 */
6906 else
6907 ui_out_table_header (uiout, 18, ui_left,
6908 "addr", "Address"); /* 5 */
6909 }
6910 if (nr_printable_breakpoints > 0)
6911 annotate_field (5);
6912 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
6913 ui_out_table_body (uiout);
6914 if (nr_printable_breakpoints > 0)
6915 annotate_breakpoints_table ();
6916
6917 ALL_BREAKPOINTS (b)
6918 {
6919 QUIT;
6920 /* If we have a filter, only list the breakpoints it accepts. */
6921 if (filter && !filter (b))
6922 continue;
6923
6924 /* If we have an "args" string, it is a list of breakpoints to
6925 accept. Skip the others. */
6926
6927 if (args != NULL && *args != '\0')
6928 {
6929 if (allflag) /* maintenance info breakpoint */
6930 {
6931 if (parse_and_eval_long (args) != b->number)
6932 continue;
6933 }
6934 else /* all others */
6935 {
6936 if (!number_is_in_list (args, b->number))
6937 continue;
6938 }
6939 }
6940 /* We only print out user settable breakpoints unless the
6941 allflag is set. */
6942 if (allflag || user_breakpoint_p (b))
6943 print_one_breakpoint (b, &last_loc, allflag);
6944 }
6945
6946 do_cleanups (bkpttbl_chain);
6947
6948 if (nr_printable_breakpoints == 0)
6949 {
6950 /* If there's a filter, let the caller decide how to report
6951 empty list. */
6952 if (!filter)
6953 {
6954 if (args == NULL || *args == '\0')
6955 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
6956 else
6957 ui_out_message (uiout, 0,
6958 "No breakpoint or watchpoint matching '%s'.\n",
6959 args);
6960 }
6961 }
6962 else
6963 {
6964 if (last_loc && !server_command)
6965 set_next_address (last_loc->gdbarch, last_loc->address);
6966 }
6967
6968 /* FIXME? Should this be moved up so that it is only called when
6969 there have been breakpoints? */
6970 annotate_breakpoints_table_end ();
6971
6972 return nr_printable_breakpoints;
6973 }
6974
6975 /* Display the value of default-collect in a way that is generally
6976 compatible with the breakpoint list. */
6977
6978 static void
6979 default_collect_info (void)
6980 {
6981 struct ui_out *uiout = current_uiout;
6982
6983 /* If it has no value (which is frequently the case), say nothing; a
6984 message like "No default-collect." gets in user's face when it's
6985 not wanted. */
6986 if (!*default_collect)
6987 return;
6988
6989 /* The following phrase lines up nicely with per-tracepoint collect
6990 actions. */
6991 ui_out_text (uiout, "default collect ");
6992 ui_out_field_string (uiout, "default-collect", default_collect);
6993 ui_out_text (uiout, " \n");
6994 }
6995
6996 static void
6997 breakpoints_info (char *args, int from_tty)
6998 {
6999 breakpoint_1 (args, 0, NULL);
7000
7001 default_collect_info ();
7002 }
7003
7004 static void
7005 watchpoints_info (char *args, int from_tty)
7006 {
7007 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
7008 struct ui_out *uiout = current_uiout;
7009
7010 if (num_printed == 0)
7011 {
7012 if (args == NULL || *args == '\0')
7013 ui_out_message (uiout, 0, "No watchpoints.\n");
7014 else
7015 ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
7016 }
7017 }
7018
7019 static void
7020 maintenance_info_breakpoints (char *args, int from_tty)
7021 {
7022 breakpoint_1 (args, 1, NULL);
7023
7024 default_collect_info ();
7025 }
7026
7027 static int
7028 breakpoint_has_pc (struct breakpoint *b,
7029 struct program_space *pspace,
7030 CORE_ADDR pc, struct obj_section *section)
7031 {
7032 struct bp_location *bl = b->loc;
7033
7034 for (; bl; bl = bl->next)
7035 {
7036 if (bl->pspace == pspace
7037 && bl->address == pc
7038 && (!overlay_debugging || bl->section == section))
7039 return 1;
7040 }
7041 return 0;
7042 }
7043
7044 /* Print a message describing any user-breakpoints set at PC. This
7045 concerns with logical breakpoints, so we match program spaces, not
7046 address spaces. */
7047
7048 static void
7049 describe_other_breakpoints (struct gdbarch *gdbarch,
7050 struct program_space *pspace, CORE_ADDR pc,
7051 struct obj_section *section, int thread)
7052 {
7053 int others = 0;
7054 struct breakpoint *b;
7055
7056 ALL_BREAKPOINTS (b)
7057 others += (user_breakpoint_p (b)
7058 && breakpoint_has_pc (b, pspace, pc, section));
7059 if (others > 0)
7060 {
7061 if (others == 1)
7062 printf_filtered (_("Note: breakpoint "));
7063 else /* if (others == ???) */
7064 printf_filtered (_("Note: breakpoints "));
7065 ALL_BREAKPOINTS (b)
7066 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
7067 {
7068 others--;
7069 printf_filtered ("%d", b->number);
7070 if (b->thread == -1 && thread != -1)
7071 printf_filtered (" (all threads)");
7072 else if (b->thread != -1)
7073 printf_filtered (" (thread %d)", b->thread);
7074 printf_filtered ("%s%s ",
7075 ((b->enable_state == bp_disabled
7076 || b->enable_state == bp_call_disabled)
7077 ? " (disabled)"
7078 : ""),
7079 (others > 1) ? ","
7080 : ((others == 1) ? " and" : ""));
7081 }
7082 printf_filtered (_("also set at pc "));
7083 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
7084 printf_filtered (".\n");
7085 }
7086 }
7087 \f
7088
7089 /* Return true iff it is meaningful to use the address member of
7090 BPT locations. For some breakpoint types, the locations' address members
7091 are irrelevant and it makes no sense to attempt to compare them to other
7092 addresses (or use them for any other purpose either).
7093
7094 More specifically, each of the following breakpoint types will
7095 always have a zero valued location address and we don't want to mark
7096 breakpoints of any of these types to be a duplicate of an actual
7097 breakpoint location at address zero:
7098
7099 bp_watchpoint
7100 bp_catchpoint
7101
7102 */
7103
7104 static int
7105 breakpoint_address_is_meaningful (struct breakpoint *bpt)
7106 {
7107 enum bptype type = bpt->type;
7108
7109 return (type != bp_watchpoint && type != bp_catchpoint);
7110 }
7111
7112 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
7113 true if LOC1 and LOC2 represent the same watchpoint location. */
7114
7115 static int
7116 watchpoint_locations_match (struct bp_location *loc1,
7117 struct bp_location *loc2)
7118 {
7119 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
7120 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
7121
7122 /* Both of them must exist. */
7123 gdb_assert (w1 != NULL);
7124 gdb_assert (w2 != NULL);
7125
7126 /* If the target can evaluate the condition expression in hardware,
7127 then we we need to insert both watchpoints even if they are at
7128 the same place. Otherwise the watchpoint will only trigger when
7129 the condition of whichever watchpoint was inserted evaluates to
7130 true, not giving a chance for GDB to check the condition of the
7131 other watchpoint. */
7132 if ((w1->cond_exp
7133 && target_can_accel_watchpoint_condition (loc1->address,
7134 loc1->length,
7135 loc1->watchpoint_type,
7136 w1->cond_exp.get ()))
7137 || (w2->cond_exp
7138 && target_can_accel_watchpoint_condition (loc2->address,
7139 loc2->length,
7140 loc2->watchpoint_type,
7141 w2->cond_exp.get ())))
7142 return 0;
7143
7144 /* Note that this checks the owner's type, not the location's. In
7145 case the target does not support read watchpoints, but does
7146 support access watchpoints, we'll have bp_read_watchpoint
7147 watchpoints with hw_access locations. Those should be considered
7148 duplicates of hw_read locations. The hw_read locations will
7149 become hw_access locations later. */
7150 return (loc1->owner->type == loc2->owner->type
7151 && loc1->pspace->aspace == loc2->pspace->aspace
7152 && loc1->address == loc2->address
7153 && loc1->length == loc2->length);
7154 }
7155
7156 /* See breakpoint.h. */
7157
7158 int
7159 breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
7160 struct address_space *aspace2, CORE_ADDR addr2)
7161 {
7162 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
7163 || aspace1 == aspace2)
7164 && addr1 == addr2);
7165 }
7166
7167 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
7168 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
7169 matches ASPACE2. On targets that have global breakpoints, the address
7170 space doesn't really matter. */
7171
7172 static int
7173 breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
7174 int len1, struct address_space *aspace2,
7175 CORE_ADDR addr2)
7176 {
7177 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
7178 || aspace1 == aspace2)
7179 && addr2 >= addr1 && addr2 < addr1 + len1);
7180 }
7181
7182 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
7183 a ranged breakpoint. In most targets, a match happens only if ASPACE
7184 matches the breakpoint's address space. On targets that have global
7185 breakpoints, the address space doesn't really matter. */
7186
7187 static int
7188 breakpoint_location_address_match (struct bp_location *bl,
7189 struct address_space *aspace,
7190 CORE_ADDR addr)
7191 {
7192 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
7193 aspace, addr)
7194 || (bl->length
7195 && breakpoint_address_match_range (bl->pspace->aspace,
7196 bl->address, bl->length,
7197 aspace, addr)));
7198 }
7199
7200 /* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
7201 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
7202 match happens only if ASPACE matches the breakpoint's address
7203 space. On targets that have global breakpoints, the address space
7204 doesn't really matter. */
7205
7206 static int
7207 breakpoint_location_address_range_overlap (struct bp_location *bl,
7208 struct address_space *aspace,
7209 CORE_ADDR addr, int len)
7210 {
7211 if (gdbarch_has_global_breakpoints (target_gdbarch ())
7212 || bl->pspace->aspace == aspace)
7213 {
7214 int bl_len = bl->length != 0 ? bl->length : 1;
7215
7216 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
7217 return 1;
7218 }
7219 return 0;
7220 }
7221
7222 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
7223 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
7224 true, otherwise returns false. */
7225
7226 static int
7227 tracepoint_locations_match (struct bp_location *loc1,
7228 struct bp_location *loc2)
7229 {
7230 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
7231 /* Since tracepoint locations are never duplicated with others', tracepoint
7232 locations at the same address of different tracepoints are regarded as
7233 different locations. */
7234 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7235 else
7236 return 0;
7237 }
7238
7239 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
7240 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
7241 represent the same location. */
7242
7243 static int
7244 breakpoint_locations_match (struct bp_location *loc1,
7245 struct bp_location *loc2)
7246 {
7247 int hw_point1, hw_point2;
7248
7249 /* Both of them must not be in moribund_locations. */
7250 gdb_assert (loc1->owner != NULL);
7251 gdb_assert (loc2->owner != NULL);
7252
7253 hw_point1 = is_hardware_watchpoint (loc1->owner);
7254 hw_point2 = is_hardware_watchpoint (loc2->owner);
7255
7256 if (hw_point1 != hw_point2)
7257 return 0;
7258 else if (hw_point1)
7259 return watchpoint_locations_match (loc1, loc2);
7260 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7261 return tracepoint_locations_match (loc1, loc2);
7262 else
7263 /* We compare bp_location.length in order to cover ranged breakpoints. */
7264 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7265 loc2->pspace->aspace, loc2->address)
7266 && loc1->length == loc2->length);
7267 }
7268
7269 static void
7270 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7271 int bnum, int have_bnum)
7272 {
7273 /* The longest string possibly returned by hex_string_custom
7274 is 50 chars. These must be at least that big for safety. */
7275 char astr1[64];
7276 char astr2[64];
7277
7278 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7279 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
7280 if (have_bnum)
7281 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
7282 bnum, astr1, astr2);
7283 else
7284 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
7285 }
7286
7287 /* Adjust a breakpoint's address to account for architectural
7288 constraints on breakpoint placement. Return the adjusted address.
7289 Note: Very few targets require this kind of adjustment. For most
7290 targets, this function is simply the identity function. */
7291
7292 static CORE_ADDR
7293 adjust_breakpoint_address (struct gdbarch *gdbarch,
7294 CORE_ADDR bpaddr, enum bptype bptype)
7295 {
7296 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
7297 {
7298 /* Very few targets need any kind of breakpoint adjustment. */
7299 return bpaddr;
7300 }
7301 else if (bptype == bp_watchpoint
7302 || bptype == bp_hardware_watchpoint
7303 || bptype == bp_read_watchpoint
7304 || bptype == bp_access_watchpoint
7305 || bptype == bp_catchpoint)
7306 {
7307 /* Watchpoints and the various bp_catch_* eventpoints should not
7308 have their addresses modified. */
7309 return bpaddr;
7310 }
7311 else if (bptype == bp_single_step)
7312 {
7313 /* Single-step breakpoints should not have their addresses
7314 modified. If there's any architectural constrain that
7315 applies to this address, then it should have already been
7316 taken into account when the breakpoint was created in the
7317 first place. If we didn't do this, stepping through e.g.,
7318 Thumb-2 IT blocks would break. */
7319 return bpaddr;
7320 }
7321 else
7322 {
7323 CORE_ADDR adjusted_bpaddr;
7324
7325 /* Some targets have architectural constraints on the placement
7326 of breakpoint instructions. Obtain the adjusted address. */
7327 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
7328
7329 /* An adjusted breakpoint address can significantly alter
7330 a user's expectations. Print a warning if an adjustment
7331 is required. */
7332 if (adjusted_bpaddr != bpaddr)
7333 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7334
7335 return adjusted_bpaddr;
7336 }
7337 }
7338
7339 void
7340 init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
7341 struct breakpoint *owner)
7342 {
7343 memset (loc, 0, sizeof (*loc));
7344
7345 gdb_assert (ops != NULL);
7346
7347 loc->ops = ops;
7348 loc->owner = owner;
7349 loc->cond_bytecode = NULL;
7350 loc->shlib_disabled = 0;
7351 loc->enabled = 1;
7352
7353 switch (owner->type)
7354 {
7355 case bp_breakpoint:
7356 case bp_single_step:
7357 case bp_until:
7358 case bp_finish:
7359 case bp_longjmp:
7360 case bp_longjmp_resume:
7361 case bp_longjmp_call_dummy:
7362 case bp_exception:
7363 case bp_exception_resume:
7364 case bp_step_resume:
7365 case bp_hp_step_resume:
7366 case bp_watchpoint_scope:
7367 case bp_call_dummy:
7368 case bp_std_terminate:
7369 case bp_shlib_event:
7370 case bp_thread_event:
7371 case bp_overlay_event:
7372 case bp_jit_event:
7373 case bp_longjmp_master:
7374 case bp_std_terminate_master:
7375 case bp_exception_master:
7376 case bp_gnu_ifunc_resolver:
7377 case bp_gnu_ifunc_resolver_return:
7378 case bp_dprintf:
7379 loc->loc_type = bp_loc_software_breakpoint;
7380 mark_breakpoint_location_modified (loc);
7381 break;
7382 case bp_hardware_breakpoint:
7383 loc->loc_type = bp_loc_hardware_breakpoint;
7384 mark_breakpoint_location_modified (loc);
7385 break;
7386 case bp_hardware_watchpoint:
7387 case bp_read_watchpoint:
7388 case bp_access_watchpoint:
7389 loc->loc_type = bp_loc_hardware_watchpoint;
7390 break;
7391 case bp_watchpoint:
7392 case bp_catchpoint:
7393 case bp_tracepoint:
7394 case bp_fast_tracepoint:
7395 case bp_static_tracepoint:
7396 loc->loc_type = bp_loc_other;
7397 break;
7398 default:
7399 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
7400 }
7401
7402 loc->refc = 1;
7403 }
7404
7405 /* Allocate a struct bp_location. */
7406
7407 static struct bp_location *
7408 allocate_bp_location (struct breakpoint *bpt)
7409 {
7410 return bpt->ops->allocate_location (bpt);
7411 }
7412
7413 static void
7414 free_bp_location (struct bp_location *loc)
7415 {
7416 loc->ops->dtor (loc);
7417 delete loc;
7418 }
7419
7420 /* Increment reference count. */
7421
7422 static void
7423 incref_bp_location (struct bp_location *bl)
7424 {
7425 ++bl->refc;
7426 }
7427
7428 /* Decrement reference count. If the reference count reaches 0,
7429 destroy the bp_location. Sets *BLP to NULL. */
7430
7431 static void
7432 decref_bp_location (struct bp_location **blp)
7433 {
7434 gdb_assert ((*blp)->refc > 0);
7435
7436 if (--(*blp)->refc == 0)
7437 free_bp_location (*blp);
7438 *blp = NULL;
7439 }
7440
7441 /* Add breakpoint B at the end of the global breakpoint chain. */
7442
7443 static void
7444 add_to_breakpoint_chain (struct breakpoint *b)
7445 {
7446 struct breakpoint *b1;
7447
7448 /* Add this breakpoint to the end of the chain so that a list of
7449 breakpoints will come out in order of increasing numbers. */
7450
7451 b1 = breakpoint_chain;
7452 if (b1 == 0)
7453 breakpoint_chain = b;
7454 else
7455 {
7456 while (b1->next)
7457 b1 = b1->next;
7458 b1->next = b;
7459 }
7460 }
7461
7462 /* Initializes breakpoint B with type BPTYPE and no locations yet. */
7463
7464 static void
7465 init_raw_breakpoint_without_location (struct breakpoint *b,
7466 struct gdbarch *gdbarch,
7467 enum bptype bptype,
7468 const struct breakpoint_ops *ops)
7469 {
7470 memset (b, 0, sizeof (*b));
7471
7472 gdb_assert (ops != NULL);
7473
7474 b->ops = ops;
7475 b->type = bptype;
7476 b->gdbarch = gdbarch;
7477 b->language = current_language->la_language;
7478 b->input_radix = input_radix;
7479 b->thread = -1;
7480 b->enable_state = bp_enabled;
7481 b->next = 0;
7482 b->silent = 0;
7483 b->ignore_count = 0;
7484 b->commands = NULL;
7485 b->frame_id = null_frame_id;
7486 b->condition_not_parsed = 0;
7487 b->py_bp_object = NULL;
7488 b->related_breakpoint = b;
7489 b->location = NULL;
7490 }
7491
7492 /* Helper to set_raw_breakpoint below. Creates a breakpoint
7493 that has type BPTYPE and has no locations as yet. */
7494
7495 static struct breakpoint *
7496 set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
7497 enum bptype bptype,
7498 const struct breakpoint_ops *ops)
7499 {
7500 struct breakpoint *b = new breakpoint ();
7501
7502 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7503 add_to_breakpoint_chain (b);
7504 return b;
7505 }
7506
7507 /* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7508 resolutions should be made as the user specified the location explicitly
7509 enough. */
7510
7511 static void
7512 set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
7513 {
7514 gdb_assert (loc->owner != NULL);
7515
7516 if (loc->owner->type == bp_breakpoint
7517 || loc->owner->type == bp_hardware_breakpoint
7518 || is_tracepoint (loc->owner))
7519 {
7520 int is_gnu_ifunc;
7521 const char *function_name;
7522 CORE_ADDR func_addr;
7523
7524 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
7525 &func_addr, NULL, &is_gnu_ifunc);
7526
7527 if (is_gnu_ifunc && !explicit_loc)
7528 {
7529 struct breakpoint *b = loc->owner;
7530
7531 gdb_assert (loc->pspace == current_program_space);
7532 if (gnu_ifunc_resolve_name (function_name,
7533 &loc->requested_address))
7534 {
7535 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7536 loc->address = adjust_breakpoint_address (loc->gdbarch,
7537 loc->requested_address,
7538 b->type);
7539 }
7540 else if (b->type == bp_breakpoint && b->loc == loc
7541 && loc->next == NULL && b->related_breakpoint == b)
7542 {
7543 /* Create only the whole new breakpoint of this type but do not
7544 mess more complicated breakpoints with multiple locations. */
7545 b->type = bp_gnu_ifunc_resolver;
7546 /* Remember the resolver's address for use by the return
7547 breakpoint. */
7548 loc->related_address = func_addr;
7549 }
7550 }
7551
7552 if (function_name)
7553 loc->function_name = xstrdup (function_name);
7554 }
7555 }
7556
7557 /* Attempt to determine architecture of location identified by SAL. */
7558 struct gdbarch *
7559 get_sal_arch (struct symtab_and_line sal)
7560 {
7561 if (sal.section)
7562 return get_objfile_arch (sal.section->objfile);
7563 if (sal.symtab)
7564 return get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
7565
7566 return NULL;
7567 }
7568
7569 /* Low level routine for partially initializing a breakpoint of type
7570 BPTYPE. The newly created breakpoint's address, section, source
7571 file name, and line number are provided by SAL.
7572
7573 It is expected that the caller will complete the initialization of
7574 the newly created breakpoint struct as well as output any status
7575 information regarding the creation of a new breakpoint. */
7576
7577 static void
7578 init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
7579 struct symtab_and_line sal, enum bptype bptype,
7580 const struct breakpoint_ops *ops)
7581 {
7582 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7583
7584 add_location_to_breakpoint (b, &sal);
7585
7586 if (bptype != bp_catchpoint)
7587 gdb_assert (sal.pspace != NULL);
7588
7589 /* Store the program space that was used to set the breakpoint,
7590 except for ordinary breakpoints, which are independent of the
7591 program space. */
7592 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7593 b->pspace = sal.pspace;
7594 }
7595
7596 /* set_raw_breakpoint is a low level routine for allocating and
7597 partially initializing a breakpoint of type BPTYPE. The newly
7598 created breakpoint's address, section, source file name, and line
7599 number are provided by SAL. The newly created and partially
7600 initialized breakpoint is added to the breakpoint chain and
7601 is also returned as the value of this function.
7602
7603 It is expected that the caller will complete the initialization of
7604 the newly created breakpoint struct as well as output any status
7605 information regarding the creation of a new breakpoint. In
7606 particular, set_raw_breakpoint does NOT set the breakpoint
7607 number! Care should be taken to not allow an error to occur
7608 prior to completing the initialization of the breakpoint. If this
7609 should happen, a bogus breakpoint will be left on the chain. */
7610
7611 struct breakpoint *
7612 set_raw_breakpoint (struct gdbarch *gdbarch,
7613 struct symtab_and_line sal, enum bptype bptype,
7614 const struct breakpoint_ops *ops)
7615 {
7616 struct breakpoint *b = new breakpoint ();
7617
7618 init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
7619 add_to_breakpoint_chain (b);
7620 return b;
7621 }
7622
7623 /* Call this routine when stepping and nexting to enable a breakpoint
7624 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7625 initiated the operation. */
7626
7627 void
7628 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
7629 {
7630 struct breakpoint *b, *b_tmp;
7631 int thread = tp->global_num;
7632
7633 /* To avoid having to rescan all objfile symbols at every step,
7634 we maintain a list of continually-inserted but always disabled
7635 longjmp "master" breakpoints. Here, we simply create momentary
7636 clones of those and enable them for the requested thread. */
7637 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7638 if (b->pspace == current_program_space
7639 && (b->type == bp_longjmp_master
7640 || b->type == bp_exception_master))
7641 {
7642 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7643 struct breakpoint *clone;
7644
7645 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7646 after their removal. */
7647 clone = momentary_breakpoint_from_master (b, type,
7648 &longjmp_breakpoint_ops, 1);
7649 clone->thread = thread;
7650 }
7651
7652 tp->initiating_frame = frame;
7653 }
7654
7655 /* Delete all longjmp breakpoints from THREAD. */
7656 void
7657 delete_longjmp_breakpoint (int thread)
7658 {
7659 struct breakpoint *b, *b_tmp;
7660
7661 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7662 if (b->type == bp_longjmp || b->type == bp_exception)
7663 {
7664 if (b->thread == thread)
7665 delete_breakpoint (b);
7666 }
7667 }
7668
7669 void
7670 delete_longjmp_breakpoint_at_next_stop (int thread)
7671 {
7672 struct breakpoint *b, *b_tmp;
7673
7674 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7675 if (b->type == bp_longjmp || b->type == bp_exception)
7676 {
7677 if (b->thread == thread)
7678 b->disposition = disp_del_at_next_stop;
7679 }
7680 }
7681
7682 /* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7683 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7684 pointer to any of them. Return NULL if this system cannot place longjmp
7685 breakpoints. */
7686
7687 struct breakpoint *
7688 set_longjmp_breakpoint_for_call_dummy (void)
7689 {
7690 struct breakpoint *b, *retval = NULL;
7691
7692 ALL_BREAKPOINTS (b)
7693 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7694 {
7695 struct breakpoint *new_b;
7696
7697 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7698 &momentary_breakpoint_ops,
7699 1);
7700 new_b->thread = ptid_to_global_thread_id (inferior_ptid);
7701
7702 /* Link NEW_B into the chain of RETVAL breakpoints. */
7703
7704 gdb_assert (new_b->related_breakpoint == new_b);
7705 if (retval == NULL)
7706 retval = new_b;
7707 new_b->related_breakpoint = retval;
7708 while (retval->related_breakpoint != new_b->related_breakpoint)
7709 retval = retval->related_breakpoint;
7710 retval->related_breakpoint = new_b;
7711 }
7712
7713 return retval;
7714 }
7715
7716 /* Verify all existing dummy frames and their associated breakpoints for
7717 TP. Remove those which can no longer be found in the current frame
7718 stack.
7719
7720 You should call this function only at places where it is safe to currently
7721 unwind the whole stack. Failed stack unwind would discard live dummy
7722 frames. */
7723
7724 void
7725 check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
7726 {
7727 struct breakpoint *b, *b_tmp;
7728
7729 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7730 if (b->type == bp_longjmp_call_dummy && b->thread == tp->global_num)
7731 {
7732 struct breakpoint *dummy_b = b->related_breakpoint;
7733
7734 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7735 dummy_b = dummy_b->related_breakpoint;
7736 if (dummy_b->type != bp_call_dummy
7737 || frame_find_by_id (dummy_b->frame_id) != NULL)
7738 continue;
7739
7740 dummy_frame_discard (dummy_b->frame_id, tp->ptid);
7741
7742 while (b->related_breakpoint != b)
7743 {
7744 if (b_tmp == b->related_breakpoint)
7745 b_tmp = b->related_breakpoint->next;
7746 delete_breakpoint (b->related_breakpoint);
7747 }
7748 delete_breakpoint (b);
7749 }
7750 }
7751
7752 void
7753 enable_overlay_breakpoints (void)
7754 {
7755 struct breakpoint *b;
7756
7757 ALL_BREAKPOINTS (b)
7758 if (b->type == bp_overlay_event)
7759 {
7760 b->enable_state = bp_enabled;
7761 update_global_location_list (UGLL_MAY_INSERT);
7762 overlay_events_enabled = 1;
7763 }
7764 }
7765
7766 void
7767 disable_overlay_breakpoints (void)
7768 {
7769 struct breakpoint *b;
7770
7771 ALL_BREAKPOINTS (b)
7772 if (b->type == bp_overlay_event)
7773 {
7774 b->enable_state = bp_disabled;
7775 update_global_location_list (UGLL_DONT_INSERT);
7776 overlay_events_enabled = 0;
7777 }
7778 }
7779
7780 /* Set an active std::terminate breakpoint for each std::terminate
7781 master breakpoint. */
7782 void
7783 set_std_terminate_breakpoint (void)
7784 {
7785 struct breakpoint *b, *b_tmp;
7786
7787 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7788 if (b->pspace == current_program_space
7789 && b->type == bp_std_terminate_master)
7790 {
7791 momentary_breakpoint_from_master (b, bp_std_terminate,
7792 &momentary_breakpoint_ops, 1);
7793 }
7794 }
7795
7796 /* Delete all the std::terminate breakpoints. */
7797 void
7798 delete_std_terminate_breakpoint (void)
7799 {
7800 struct breakpoint *b, *b_tmp;
7801
7802 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7803 if (b->type == bp_std_terminate)
7804 delete_breakpoint (b);
7805 }
7806
7807 struct breakpoint *
7808 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7809 {
7810 struct breakpoint *b;
7811
7812 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7813 &internal_breakpoint_ops);
7814
7815 b->enable_state = bp_enabled;
7816 /* location has to be used or breakpoint_re_set will delete me. */
7817 b->location = new_address_location (b->loc->address, NULL, 0);
7818
7819 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7820
7821 return b;
7822 }
7823
7824 struct lang_and_radix
7825 {
7826 enum language lang;
7827 int radix;
7828 };
7829
7830 /* Create a breakpoint for JIT code registration and unregistration. */
7831
7832 struct breakpoint *
7833 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7834 {
7835 return create_internal_breakpoint (gdbarch, address, bp_jit_event,
7836 &internal_breakpoint_ops);
7837 }
7838
7839 /* Remove JIT code registration and unregistration breakpoint(s). */
7840
7841 void
7842 remove_jit_event_breakpoints (void)
7843 {
7844 struct breakpoint *b, *b_tmp;
7845
7846 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7847 if (b->type == bp_jit_event
7848 && b->loc->pspace == current_program_space)
7849 delete_breakpoint (b);
7850 }
7851
7852 void
7853 remove_solib_event_breakpoints (void)
7854 {
7855 struct breakpoint *b, *b_tmp;
7856
7857 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7858 if (b->type == bp_shlib_event
7859 && b->loc->pspace == current_program_space)
7860 delete_breakpoint (b);
7861 }
7862
7863 /* See breakpoint.h. */
7864
7865 void
7866 remove_solib_event_breakpoints_at_next_stop (void)
7867 {
7868 struct breakpoint *b, *b_tmp;
7869
7870 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7871 if (b->type == bp_shlib_event
7872 && b->loc->pspace == current_program_space)
7873 b->disposition = disp_del_at_next_stop;
7874 }
7875
7876 /* Helper for create_solib_event_breakpoint /
7877 create_and_insert_solib_event_breakpoint. Allows specifying which
7878 INSERT_MODE to pass through to update_global_location_list. */
7879
7880 static struct breakpoint *
7881 create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7882 enum ugll_insert_mode insert_mode)
7883 {
7884 struct breakpoint *b;
7885
7886 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7887 &internal_breakpoint_ops);
7888 update_global_location_list_nothrow (insert_mode);
7889 return b;
7890 }
7891
7892 struct breakpoint *
7893 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7894 {
7895 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7896 }
7897
7898 /* See breakpoint.h. */
7899
7900 struct breakpoint *
7901 create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7902 {
7903 struct breakpoint *b;
7904
7905 /* Explicitly tell update_global_location_list to insert
7906 locations. */
7907 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
7908 if (!b->loc->inserted)
7909 {
7910 delete_breakpoint (b);
7911 return NULL;
7912 }
7913 return b;
7914 }
7915
7916 /* Disable any breakpoints that are on code in shared libraries. Only
7917 apply to enabled breakpoints, disabled ones can just stay disabled. */
7918
7919 void
7920 disable_breakpoints_in_shlibs (void)
7921 {
7922 struct bp_location *loc, **locp_tmp;
7923
7924 ALL_BP_LOCATIONS (loc, locp_tmp)
7925 {
7926 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7927 struct breakpoint *b = loc->owner;
7928
7929 /* We apply the check to all breakpoints, including disabled for
7930 those with loc->duplicate set. This is so that when breakpoint
7931 becomes enabled, or the duplicate is removed, gdb will try to
7932 insert all breakpoints. If we don't set shlib_disabled here,
7933 we'll try to insert those breakpoints and fail. */
7934 if (((b->type == bp_breakpoint)
7935 || (b->type == bp_jit_event)
7936 || (b->type == bp_hardware_breakpoint)
7937 || (is_tracepoint (b)))
7938 && loc->pspace == current_program_space
7939 && !loc->shlib_disabled
7940 && solib_name_from_address (loc->pspace, loc->address)
7941 )
7942 {
7943 loc->shlib_disabled = 1;
7944 }
7945 }
7946 }
7947
7948 /* Disable any breakpoints and tracepoints that are in SOLIB upon
7949 notification of unloaded_shlib. Only apply to enabled breakpoints,
7950 disabled ones can just stay disabled. */
7951
7952 static void
7953 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7954 {
7955 struct bp_location *loc, **locp_tmp;
7956 int disabled_shlib_breaks = 0;
7957
7958 /* SunOS a.out shared libraries are always mapped, so do not
7959 disable breakpoints; they will only be reported as unloaded
7960 through clear_solib when GDB discards its shared library
7961 list. See clear_solib for more information. */
7962 if (exec_bfd != NULL
7963 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
7964 return;
7965
7966 ALL_BP_LOCATIONS (loc, locp_tmp)
7967 {
7968 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7969 struct breakpoint *b = loc->owner;
7970
7971 if (solib->pspace == loc->pspace
7972 && !loc->shlib_disabled
7973 && (((b->type == bp_breakpoint
7974 || b->type == bp_jit_event
7975 || b->type == bp_hardware_breakpoint)
7976 && (loc->loc_type == bp_loc_hardware_breakpoint
7977 || loc->loc_type == bp_loc_software_breakpoint))
7978 || is_tracepoint (b))
7979 && solib_contains_address_p (solib, loc->address))
7980 {
7981 loc->shlib_disabled = 1;
7982 /* At this point, we cannot rely on remove_breakpoint
7983 succeeding so we must mark the breakpoint as not inserted
7984 to prevent future errors occurring in remove_breakpoints. */
7985 loc->inserted = 0;
7986
7987 /* This may cause duplicate notifications for the same breakpoint. */
7988 observer_notify_breakpoint_modified (b);
7989
7990 if (!disabled_shlib_breaks)
7991 {
7992 target_terminal_ours_for_output ();
7993 warning (_("Temporarily disabling breakpoints "
7994 "for unloaded shared library \"%s\""),
7995 solib->so_name);
7996 }
7997 disabled_shlib_breaks = 1;
7998 }
7999 }
8000 }
8001
8002 /* Disable any breakpoints and tracepoints in OBJFILE upon
8003 notification of free_objfile. Only apply to enabled breakpoints,
8004 disabled ones can just stay disabled. */
8005
8006 static void
8007 disable_breakpoints_in_freed_objfile (struct objfile *objfile)
8008 {
8009 struct breakpoint *b;
8010
8011 if (objfile == NULL)
8012 return;
8013
8014 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
8015 managed by the user with add-symbol-file/remove-symbol-file.
8016 Similarly to how breakpoints in shared libraries are handled in
8017 response to "nosharedlibrary", mark breakpoints in such modules
8018 shlib_disabled so they end up uninserted on the next global
8019 location list update. Shared libraries not loaded by the user
8020 aren't handled here -- they're already handled in
8021 disable_breakpoints_in_unloaded_shlib, called by solib.c's
8022 solib_unloaded observer. We skip objfiles that are not
8023 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
8024 main objfile). */
8025 if ((objfile->flags & OBJF_SHARED) == 0
8026 || (objfile->flags & OBJF_USERLOADED) == 0)
8027 return;
8028
8029 ALL_BREAKPOINTS (b)
8030 {
8031 struct bp_location *loc;
8032 int bp_modified = 0;
8033
8034 if (!is_breakpoint (b) && !is_tracepoint (b))
8035 continue;
8036
8037 for (loc = b->loc; loc != NULL; loc = loc->next)
8038 {
8039 CORE_ADDR loc_addr = loc->address;
8040
8041 if (loc->loc_type != bp_loc_hardware_breakpoint
8042 && loc->loc_type != bp_loc_software_breakpoint)
8043 continue;
8044
8045 if (loc->shlib_disabled != 0)
8046 continue;
8047
8048 if (objfile->pspace != loc->pspace)
8049 continue;
8050
8051 if (loc->loc_type != bp_loc_hardware_breakpoint
8052 && loc->loc_type != bp_loc_software_breakpoint)
8053 continue;
8054
8055 if (is_addr_in_objfile (loc_addr, objfile))
8056 {
8057 loc->shlib_disabled = 1;
8058 /* At this point, we don't know whether the object was
8059 unmapped from the inferior or not, so leave the
8060 inserted flag alone. We'll handle failure to
8061 uninsert quietly, in case the object was indeed
8062 unmapped. */
8063
8064 mark_breakpoint_location_modified (loc);
8065
8066 bp_modified = 1;
8067 }
8068 }
8069
8070 if (bp_modified)
8071 observer_notify_breakpoint_modified (b);
8072 }
8073 }
8074
8075 /* FORK & VFORK catchpoints. */
8076
8077 /* An instance of this type is used to represent a fork or vfork
8078 catchpoint. It includes a "struct breakpoint" as a kind of base
8079 class; users downcast to "struct breakpoint *" when needed. A
8080 breakpoint is really of this type iff its ops pointer points to
8081 CATCH_FORK_BREAKPOINT_OPS. */
8082
8083 struct fork_catchpoint
8084 {
8085 /* The base class. */
8086 struct breakpoint base;
8087
8088 /* Process id of a child process whose forking triggered this
8089 catchpoint. This field is only valid immediately after this
8090 catchpoint has triggered. */
8091 ptid_t forked_inferior_pid;
8092 };
8093
8094 /* Implement the "insert" breakpoint_ops method for fork
8095 catchpoints. */
8096
8097 static int
8098 insert_catch_fork (struct bp_location *bl)
8099 {
8100 return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
8101 }
8102
8103 /* Implement the "remove" breakpoint_ops method for fork
8104 catchpoints. */
8105
8106 static int
8107 remove_catch_fork (struct bp_location *bl, enum remove_bp_reason reason)
8108 {
8109 return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
8110 }
8111
8112 /* Implement the "breakpoint_hit" breakpoint_ops method for fork
8113 catchpoints. */
8114
8115 static int
8116 breakpoint_hit_catch_fork (const struct bp_location *bl,
8117 struct address_space *aspace, CORE_ADDR bp_addr,
8118 const struct target_waitstatus *ws)
8119 {
8120 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8121
8122 if (ws->kind != TARGET_WAITKIND_FORKED)
8123 return 0;
8124
8125 c->forked_inferior_pid = ws->value.related_pid;
8126 return 1;
8127 }
8128
8129 /* Implement the "print_it" breakpoint_ops method for fork
8130 catchpoints. */
8131
8132 static enum print_stop_action
8133 print_it_catch_fork (bpstat bs)
8134 {
8135 struct ui_out *uiout = current_uiout;
8136 struct breakpoint *b = bs->breakpoint_at;
8137 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
8138
8139 annotate_catchpoint (b->number);
8140 maybe_print_thread_hit_breakpoint (uiout);
8141 if (b->disposition == disp_del)
8142 ui_out_text (uiout, "Temporary catchpoint ");
8143 else
8144 ui_out_text (uiout, "Catchpoint ");
8145 if (ui_out_is_mi_like_p (uiout))
8146 {
8147 ui_out_field_string (uiout, "reason",
8148 async_reason_lookup (EXEC_ASYNC_FORK));
8149 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8150 }
8151 ui_out_field_int (uiout, "bkptno", b->number);
8152 ui_out_text (uiout, " (forked process ");
8153 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
8154 ui_out_text (uiout, "), ");
8155 return PRINT_SRC_AND_LOC;
8156 }
8157
8158 /* Implement the "print_one" breakpoint_ops method for fork
8159 catchpoints. */
8160
8161 static void
8162 print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
8163 {
8164 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8165 struct value_print_options opts;
8166 struct ui_out *uiout = current_uiout;
8167
8168 get_user_print_options (&opts);
8169
8170 /* Field 4, the address, is omitted (which makes the columns not
8171 line up too nicely with the headers, but the effect is relatively
8172 readable). */
8173 if (opts.addressprint)
8174 ui_out_field_skip (uiout, "addr");
8175 annotate_field (5);
8176 ui_out_text (uiout, "fork");
8177 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
8178 {
8179 ui_out_text (uiout, ", process ");
8180 ui_out_field_int (uiout, "what",
8181 ptid_get_pid (c->forked_inferior_pid));
8182 ui_out_spaces (uiout, 1);
8183 }
8184
8185 if (ui_out_is_mi_like_p (uiout))
8186 ui_out_field_string (uiout, "catch-type", "fork");
8187 }
8188
8189 /* Implement the "print_mention" breakpoint_ops method for fork
8190 catchpoints. */
8191
8192 static void
8193 print_mention_catch_fork (struct breakpoint *b)
8194 {
8195 printf_filtered (_("Catchpoint %d (fork)"), b->number);
8196 }
8197
8198 /* Implement the "print_recreate" breakpoint_ops method for fork
8199 catchpoints. */
8200
8201 static void
8202 print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
8203 {
8204 fprintf_unfiltered (fp, "catch fork");
8205 print_recreate_thread (b, fp);
8206 }
8207
8208 /* The breakpoint_ops structure to be used in fork catchpoints. */
8209
8210 static struct breakpoint_ops catch_fork_breakpoint_ops;
8211
8212 /* Implement the "insert" breakpoint_ops method for vfork
8213 catchpoints. */
8214
8215 static int
8216 insert_catch_vfork (struct bp_location *bl)
8217 {
8218 return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
8219 }
8220
8221 /* Implement the "remove" breakpoint_ops method for vfork
8222 catchpoints. */
8223
8224 static int
8225 remove_catch_vfork (struct bp_location *bl, enum remove_bp_reason reason)
8226 {
8227 return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
8228 }
8229
8230 /* Implement the "breakpoint_hit" breakpoint_ops method for vfork
8231 catchpoints. */
8232
8233 static int
8234 breakpoint_hit_catch_vfork (const struct bp_location *bl,
8235 struct address_space *aspace, CORE_ADDR bp_addr,
8236 const struct target_waitstatus *ws)
8237 {
8238 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8239
8240 if (ws->kind != TARGET_WAITKIND_VFORKED)
8241 return 0;
8242
8243 c->forked_inferior_pid = ws->value.related_pid;
8244 return 1;
8245 }
8246
8247 /* Implement the "print_it" breakpoint_ops method for vfork
8248 catchpoints. */
8249
8250 static enum print_stop_action
8251 print_it_catch_vfork (bpstat bs)
8252 {
8253 struct ui_out *uiout = current_uiout;
8254 struct breakpoint *b = bs->breakpoint_at;
8255 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8256
8257 annotate_catchpoint (b->number);
8258 maybe_print_thread_hit_breakpoint (uiout);
8259 if (b->disposition == disp_del)
8260 ui_out_text (uiout, "Temporary catchpoint ");
8261 else
8262 ui_out_text (uiout, "Catchpoint ");
8263 if (ui_out_is_mi_like_p (uiout))
8264 {
8265 ui_out_field_string (uiout, "reason",
8266 async_reason_lookup (EXEC_ASYNC_VFORK));
8267 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8268 }
8269 ui_out_field_int (uiout, "bkptno", b->number);
8270 ui_out_text (uiout, " (vforked process ");
8271 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
8272 ui_out_text (uiout, "), ");
8273 return PRINT_SRC_AND_LOC;
8274 }
8275
8276 /* Implement the "print_one" breakpoint_ops method for vfork
8277 catchpoints. */
8278
8279 static void
8280 print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
8281 {
8282 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8283 struct value_print_options opts;
8284 struct ui_out *uiout = current_uiout;
8285
8286 get_user_print_options (&opts);
8287 /* Field 4, the address, is omitted (which makes the columns not
8288 line up too nicely with the headers, but the effect is relatively
8289 readable). */
8290 if (opts.addressprint)
8291 ui_out_field_skip (uiout, "addr");
8292 annotate_field (5);
8293 ui_out_text (uiout, "vfork");
8294 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
8295 {
8296 ui_out_text (uiout, ", process ");
8297 ui_out_field_int (uiout, "what",
8298 ptid_get_pid (c->forked_inferior_pid));
8299 ui_out_spaces (uiout, 1);
8300 }
8301
8302 if (ui_out_is_mi_like_p (uiout))
8303 ui_out_field_string (uiout, "catch-type", "vfork");
8304 }
8305
8306 /* Implement the "print_mention" breakpoint_ops method for vfork
8307 catchpoints. */
8308
8309 static void
8310 print_mention_catch_vfork (struct breakpoint *b)
8311 {
8312 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
8313 }
8314
8315 /* Implement the "print_recreate" breakpoint_ops method for vfork
8316 catchpoints. */
8317
8318 static void
8319 print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8320 {
8321 fprintf_unfiltered (fp, "catch vfork");
8322 print_recreate_thread (b, fp);
8323 }
8324
8325 /* The breakpoint_ops structure to be used in vfork catchpoints. */
8326
8327 static struct breakpoint_ops catch_vfork_breakpoint_ops;
8328
8329 /* An instance of this type is used to represent an solib catchpoint.
8330 It includes a "struct breakpoint" as a kind of base class; users
8331 downcast to "struct breakpoint *" when needed. A breakpoint is
8332 really of this type iff its ops pointer points to
8333 CATCH_SOLIB_BREAKPOINT_OPS. */
8334
8335 struct solib_catchpoint
8336 {
8337 /* The base class. */
8338 struct breakpoint base;
8339
8340 /* True for "catch load", false for "catch unload". */
8341 unsigned char is_load;
8342
8343 /* Regular expression to match, if any. COMPILED is only valid when
8344 REGEX is non-NULL. */
8345 char *regex;
8346 regex_t compiled;
8347 };
8348
8349 static void
8350 dtor_catch_solib (struct breakpoint *b)
8351 {
8352 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8353
8354 if (self->regex)
8355 regfree (&self->compiled);
8356 xfree (self->regex);
8357
8358 base_breakpoint_ops.dtor (b);
8359 }
8360
8361 static int
8362 insert_catch_solib (struct bp_location *ignore)
8363 {
8364 return 0;
8365 }
8366
8367 static int
8368 remove_catch_solib (struct bp_location *ignore, enum remove_bp_reason reason)
8369 {
8370 return 0;
8371 }
8372
8373 static int
8374 breakpoint_hit_catch_solib (const struct bp_location *bl,
8375 struct address_space *aspace,
8376 CORE_ADDR bp_addr,
8377 const struct target_waitstatus *ws)
8378 {
8379 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8380 struct breakpoint *other;
8381
8382 if (ws->kind == TARGET_WAITKIND_LOADED)
8383 return 1;
8384
8385 ALL_BREAKPOINTS (other)
8386 {
8387 struct bp_location *other_bl;
8388
8389 if (other == bl->owner)
8390 continue;
8391
8392 if (other->type != bp_shlib_event)
8393 continue;
8394
8395 if (self->base.pspace != NULL && other->pspace != self->base.pspace)
8396 continue;
8397
8398 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8399 {
8400 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8401 return 1;
8402 }
8403 }
8404
8405 return 0;
8406 }
8407
8408 static void
8409 check_status_catch_solib (struct bpstats *bs)
8410 {
8411 struct solib_catchpoint *self
8412 = (struct solib_catchpoint *) bs->breakpoint_at;
8413 int ix;
8414
8415 if (self->is_load)
8416 {
8417 struct so_list *iter;
8418
8419 for (ix = 0;
8420 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
8421 ix, iter);
8422 ++ix)
8423 {
8424 if (!self->regex
8425 || regexec (&self->compiled, iter->so_name, 0, NULL, 0) == 0)
8426 return;
8427 }
8428 }
8429 else
8430 {
8431 char *iter;
8432
8433 for (ix = 0;
8434 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
8435 ix, iter);
8436 ++ix)
8437 {
8438 if (!self->regex
8439 || regexec (&self->compiled, iter, 0, NULL, 0) == 0)
8440 return;
8441 }
8442 }
8443
8444 bs->stop = 0;
8445 bs->print_it = print_it_noop;
8446 }
8447
8448 static enum print_stop_action
8449 print_it_catch_solib (bpstat bs)
8450 {
8451 struct breakpoint *b = bs->breakpoint_at;
8452 struct ui_out *uiout = current_uiout;
8453
8454 annotate_catchpoint (b->number);
8455 maybe_print_thread_hit_breakpoint (uiout);
8456 if (b->disposition == disp_del)
8457 ui_out_text (uiout, "Temporary catchpoint ");
8458 else
8459 ui_out_text (uiout, "Catchpoint ");
8460 ui_out_field_int (uiout, "bkptno", b->number);
8461 ui_out_text (uiout, "\n");
8462 if (ui_out_is_mi_like_p (uiout))
8463 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8464 print_solib_event (1);
8465 return PRINT_SRC_AND_LOC;
8466 }
8467
8468 static void
8469 print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8470 {
8471 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8472 struct value_print_options opts;
8473 struct ui_out *uiout = current_uiout;
8474 char *msg;
8475
8476 get_user_print_options (&opts);
8477 /* Field 4, the address, is omitted (which makes the columns not
8478 line up too nicely with the headers, but the effect is relatively
8479 readable). */
8480 if (opts.addressprint)
8481 {
8482 annotate_field (4);
8483 ui_out_field_skip (uiout, "addr");
8484 }
8485
8486 annotate_field (5);
8487 if (self->is_load)
8488 {
8489 if (self->regex)
8490 msg = xstrprintf (_("load of library matching %s"), self->regex);
8491 else
8492 msg = xstrdup (_("load of library"));
8493 }
8494 else
8495 {
8496 if (self->regex)
8497 msg = xstrprintf (_("unload of library matching %s"), self->regex);
8498 else
8499 msg = xstrdup (_("unload of library"));
8500 }
8501 ui_out_field_string (uiout, "what", msg);
8502 xfree (msg);
8503
8504 if (ui_out_is_mi_like_p (uiout))
8505 ui_out_field_string (uiout, "catch-type",
8506 self->is_load ? "load" : "unload");
8507 }
8508
8509 static void
8510 print_mention_catch_solib (struct breakpoint *b)
8511 {
8512 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8513
8514 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8515 self->is_load ? "load" : "unload");
8516 }
8517
8518 static void
8519 print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8520 {
8521 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8522
8523 fprintf_unfiltered (fp, "%s %s",
8524 b->disposition == disp_del ? "tcatch" : "catch",
8525 self->is_load ? "load" : "unload");
8526 if (self->regex)
8527 fprintf_unfiltered (fp, " %s", self->regex);
8528 fprintf_unfiltered (fp, "\n");
8529 }
8530
8531 static struct breakpoint_ops catch_solib_breakpoint_ops;
8532
8533 /* Shared helper function (MI and CLI) for creating and installing
8534 a shared object event catchpoint. If IS_LOAD is non-zero then
8535 the events to be caught are load events, otherwise they are
8536 unload events. If IS_TEMP is non-zero the catchpoint is a
8537 temporary one. If ENABLED is non-zero the catchpoint is
8538 created in an enabled state. */
8539
8540 void
8541 add_solib_catchpoint (char *arg, int is_load, int is_temp, int enabled)
8542 {
8543 struct solib_catchpoint *c;
8544 struct gdbarch *gdbarch = get_current_arch ();
8545 struct cleanup *cleanup;
8546
8547 if (!arg)
8548 arg = "";
8549 arg = skip_spaces (arg);
8550
8551 c = new solib_catchpoint ();
8552 cleanup = make_cleanup (xfree, c);
8553
8554 if (*arg != '\0')
8555 {
8556 int errcode;
8557
8558 errcode = regcomp (&c->compiled, arg, REG_NOSUB);
8559 if (errcode != 0)
8560 {
8561 char *err = get_regcomp_error (errcode, &c->compiled);
8562
8563 make_cleanup (xfree, err);
8564 error (_("Invalid regexp (%s): %s"), err, arg);
8565 }
8566 c->regex = xstrdup (arg);
8567 }
8568
8569 c->is_load = is_load;
8570 init_catchpoint (&c->base, gdbarch, is_temp, NULL,
8571 &catch_solib_breakpoint_ops);
8572
8573 c->base.enable_state = enabled ? bp_enabled : bp_disabled;
8574
8575 discard_cleanups (cleanup);
8576 install_breakpoint (0, &c->base, 1);
8577 }
8578
8579 /* A helper function that does all the work for "catch load" and
8580 "catch unload". */
8581
8582 static void
8583 catch_load_or_unload (char *arg, int from_tty, int is_load,
8584 struct cmd_list_element *command)
8585 {
8586 int tempflag;
8587 const int enabled = 1;
8588
8589 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8590
8591 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8592 }
8593
8594 static void
8595 catch_load_command_1 (char *arg, int from_tty,
8596 struct cmd_list_element *command)
8597 {
8598 catch_load_or_unload (arg, from_tty, 1, command);
8599 }
8600
8601 static void
8602 catch_unload_command_1 (char *arg, int from_tty,
8603 struct cmd_list_element *command)
8604 {
8605 catch_load_or_unload (arg, from_tty, 0, command);
8606 }
8607
8608 /* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8609 is non-zero, then make the breakpoint temporary. If COND_STRING is
8610 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8611 the breakpoint_ops structure associated to the catchpoint. */
8612
8613 void
8614 init_catchpoint (struct breakpoint *b,
8615 struct gdbarch *gdbarch, int tempflag,
8616 char *cond_string,
8617 const struct breakpoint_ops *ops)
8618 {
8619 struct symtab_and_line sal;
8620
8621 init_sal (&sal);
8622 sal.pspace = current_program_space;
8623
8624 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
8625
8626 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
8627 b->disposition = tempflag ? disp_del : disp_donttouch;
8628 }
8629
8630 void
8631 install_breakpoint (int internal, struct breakpoint *b, int update_gll)
8632 {
8633 add_to_breakpoint_chain (b);
8634 set_breakpoint_number (internal, b);
8635 if (is_tracepoint (b))
8636 set_tracepoint_count (breakpoint_count);
8637 if (!internal)
8638 mention (b);
8639 observer_notify_breakpoint_created (b);
8640
8641 if (update_gll)
8642 update_global_location_list (UGLL_MAY_INSERT);
8643 }
8644
8645 static void
8646 create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8647 int tempflag, char *cond_string,
8648 const struct breakpoint_ops *ops)
8649 {
8650 struct fork_catchpoint *c = new fork_catchpoint ();
8651
8652 init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
8653
8654 c->forked_inferior_pid = null_ptid;
8655
8656 install_breakpoint (0, &c->base, 1);
8657 }
8658
8659 /* Exec catchpoints. */
8660
8661 /* An instance of this type is used to represent an exec catchpoint.
8662 It includes a "struct breakpoint" as a kind of base class; users
8663 downcast to "struct breakpoint *" when needed. A breakpoint is
8664 really of this type iff its ops pointer points to
8665 CATCH_EXEC_BREAKPOINT_OPS. */
8666
8667 struct exec_catchpoint
8668 {
8669 /* The base class. */
8670 struct breakpoint base;
8671
8672 /* Filename of a program whose exec triggered this catchpoint.
8673 This field is only valid immediately after this catchpoint has
8674 triggered. */
8675 char *exec_pathname;
8676 };
8677
8678 /* Implement the "dtor" breakpoint_ops method for exec
8679 catchpoints. */
8680
8681 static void
8682 dtor_catch_exec (struct breakpoint *b)
8683 {
8684 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8685
8686 xfree (c->exec_pathname);
8687
8688 base_breakpoint_ops.dtor (b);
8689 }
8690
8691 static int
8692 insert_catch_exec (struct bp_location *bl)
8693 {
8694 return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
8695 }
8696
8697 static int
8698 remove_catch_exec (struct bp_location *bl, enum remove_bp_reason reason)
8699 {
8700 return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
8701 }
8702
8703 static int
8704 breakpoint_hit_catch_exec (const struct bp_location *bl,
8705 struct address_space *aspace, CORE_ADDR bp_addr,
8706 const struct target_waitstatus *ws)
8707 {
8708 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8709
8710 if (ws->kind != TARGET_WAITKIND_EXECD)
8711 return 0;
8712
8713 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8714 return 1;
8715 }
8716
8717 static enum print_stop_action
8718 print_it_catch_exec (bpstat bs)
8719 {
8720 struct ui_out *uiout = current_uiout;
8721 struct breakpoint *b = bs->breakpoint_at;
8722 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8723
8724 annotate_catchpoint (b->number);
8725 maybe_print_thread_hit_breakpoint (uiout);
8726 if (b->disposition == disp_del)
8727 ui_out_text (uiout, "Temporary catchpoint ");
8728 else
8729 ui_out_text (uiout, "Catchpoint ");
8730 if (ui_out_is_mi_like_p (uiout))
8731 {
8732 ui_out_field_string (uiout, "reason",
8733 async_reason_lookup (EXEC_ASYNC_EXEC));
8734 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8735 }
8736 ui_out_field_int (uiout, "bkptno", b->number);
8737 ui_out_text (uiout, " (exec'd ");
8738 ui_out_field_string (uiout, "new-exec", c->exec_pathname);
8739 ui_out_text (uiout, "), ");
8740
8741 return PRINT_SRC_AND_LOC;
8742 }
8743
8744 static void
8745 print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
8746 {
8747 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8748 struct value_print_options opts;
8749 struct ui_out *uiout = current_uiout;
8750
8751 get_user_print_options (&opts);
8752
8753 /* Field 4, the address, is omitted (which makes the columns
8754 not line up too nicely with the headers, but the effect
8755 is relatively readable). */
8756 if (opts.addressprint)
8757 ui_out_field_skip (uiout, "addr");
8758 annotate_field (5);
8759 ui_out_text (uiout, "exec");
8760 if (c->exec_pathname != NULL)
8761 {
8762 ui_out_text (uiout, ", program \"");
8763 ui_out_field_string (uiout, "what", c->exec_pathname);
8764 ui_out_text (uiout, "\" ");
8765 }
8766
8767 if (ui_out_is_mi_like_p (uiout))
8768 ui_out_field_string (uiout, "catch-type", "exec");
8769 }
8770
8771 static void
8772 print_mention_catch_exec (struct breakpoint *b)
8773 {
8774 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8775 }
8776
8777 /* Implement the "print_recreate" breakpoint_ops method for exec
8778 catchpoints. */
8779
8780 static void
8781 print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8782 {
8783 fprintf_unfiltered (fp, "catch exec");
8784 print_recreate_thread (b, fp);
8785 }
8786
8787 static struct breakpoint_ops catch_exec_breakpoint_ops;
8788
8789 static int
8790 hw_breakpoint_used_count (void)
8791 {
8792 int i = 0;
8793 struct breakpoint *b;
8794 struct bp_location *bl;
8795
8796 ALL_BREAKPOINTS (b)
8797 {
8798 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
8799 for (bl = b->loc; bl; bl = bl->next)
8800 {
8801 /* Special types of hardware breakpoints may use more than
8802 one register. */
8803 i += b->ops->resources_needed (bl);
8804 }
8805 }
8806
8807 return i;
8808 }
8809
8810 /* Returns the resources B would use if it were a hardware
8811 watchpoint. */
8812
8813 static int
8814 hw_watchpoint_use_count (struct breakpoint *b)
8815 {
8816 int i = 0;
8817 struct bp_location *bl;
8818
8819 if (!breakpoint_enabled (b))
8820 return 0;
8821
8822 for (bl = b->loc; bl; bl = bl->next)
8823 {
8824 /* Special types of hardware watchpoints may use more than
8825 one register. */
8826 i += b->ops->resources_needed (bl);
8827 }
8828
8829 return i;
8830 }
8831
8832 /* Returns the sum the used resources of all hardware watchpoints of
8833 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8834 the sum of the used resources of all hardware watchpoints of other
8835 types _not_ TYPE. */
8836
8837 static int
8838 hw_watchpoint_used_count_others (struct breakpoint *except,
8839 enum bptype type, int *other_type_used)
8840 {
8841 int i = 0;
8842 struct breakpoint *b;
8843
8844 *other_type_used = 0;
8845 ALL_BREAKPOINTS (b)
8846 {
8847 if (b == except)
8848 continue;
8849 if (!breakpoint_enabled (b))
8850 continue;
8851
8852 if (b->type == type)
8853 i += hw_watchpoint_use_count (b);
8854 else if (is_hardware_watchpoint (b))
8855 *other_type_used = 1;
8856 }
8857
8858 return i;
8859 }
8860
8861 void
8862 disable_watchpoints_before_interactive_call_start (void)
8863 {
8864 struct breakpoint *b;
8865
8866 ALL_BREAKPOINTS (b)
8867 {
8868 if (is_watchpoint (b) && breakpoint_enabled (b))
8869 {
8870 b->enable_state = bp_call_disabled;
8871 update_global_location_list (UGLL_DONT_INSERT);
8872 }
8873 }
8874 }
8875
8876 void
8877 enable_watchpoints_after_interactive_call_stop (void)
8878 {
8879 struct breakpoint *b;
8880
8881 ALL_BREAKPOINTS (b)
8882 {
8883 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
8884 {
8885 b->enable_state = bp_enabled;
8886 update_global_location_list (UGLL_MAY_INSERT);
8887 }
8888 }
8889 }
8890
8891 void
8892 disable_breakpoints_before_startup (void)
8893 {
8894 current_program_space->executing_startup = 1;
8895 update_global_location_list (UGLL_DONT_INSERT);
8896 }
8897
8898 void
8899 enable_breakpoints_after_startup (void)
8900 {
8901 current_program_space->executing_startup = 0;
8902 breakpoint_re_set ();
8903 }
8904
8905 /* Create a new single-step breakpoint for thread THREAD, with no
8906 locations. */
8907
8908 static struct breakpoint *
8909 new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8910 {
8911 struct breakpoint *b = new breakpoint ();
8912
8913 init_raw_breakpoint_without_location (b, gdbarch, bp_single_step,
8914 &momentary_breakpoint_ops);
8915
8916 b->disposition = disp_donttouch;
8917 b->frame_id = null_frame_id;
8918
8919 b->thread = thread;
8920 gdb_assert (b->thread != 0);
8921
8922 add_to_breakpoint_chain (b);
8923
8924 return b;
8925 }
8926
8927 /* Set a momentary breakpoint of type TYPE at address specified by
8928 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8929 frame. */
8930
8931 struct breakpoint *
8932 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8933 struct frame_id frame_id, enum bptype type)
8934 {
8935 struct breakpoint *b;
8936
8937 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8938 tail-called one. */
8939 gdb_assert (!frame_id_artificial_p (frame_id));
8940
8941 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
8942 b->enable_state = bp_enabled;
8943 b->disposition = disp_donttouch;
8944 b->frame_id = frame_id;
8945
8946 /* If we're debugging a multi-threaded program, then we want
8947 momentary breakpoints to be active in only a single thread of
8948 control. */
8949 if (in_thread_list (inferior_ptid))
8950 b->thread = ptid_to_global_thread_id (inferior_ptid);
8951
8952 update_global_location_list_nothrow (UGLL_MAY_INSERT);
8953
8954 return b;
8955 }
8956
8957 /* Make a momentary breakpoint based on the master breakpoint ORIG.
8958 The new breakpoint will have type TYPE, use OPS as its
8959 breakpoint_ops, and will set enabled to LOC_ENABLED. */
8960
8961 static struct breakpoint *
8962 momentary_breakpoint_from_master (struct breakpoint *orig,
8963 enum bptype type,
8964 const struct breakpoint_ops *ops,
8965 int loc_enabled)
8966 {
8967 struct breakpoint *copy;
8968
8969 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
8970 copy->loc = allocate_bp_location (copy);
8971 set_breakpoint_location_function (copy->loc, 1);
8972
8973 copy->loc->gdbarch = orig->loc->gdbarch;
8974 copy->loc->requested_address = orig->loc->requested_address;
8975 copy->loc->address = orig->loc->address;
8976 copy->loc->section = orig->loc->section;
8977 copy->loc->pspace = orig->loc->pspace;
8978 copy->loc->probe = orig->loc->probe;
8979 copy->loc->line_number = orig->loc->line_number;
8980 copy->loc->symtab = orig->loc->symtab;
8981 copy->loc->enabled = loc_enabled;
8982 copy->frame_id = orig->frame_id;
8983 copy->thread = orig->thread;
8984 copy->pspace = orig->pspace;
8985
8986 copy->enable_state = bp_enabled;
8987 copy->disposition = disp_donttouch;
8988 copy->number = internal_breakpoint_number--;
8989
8990 update_global_location_list_nothrow (UGLL_DONT_INSERT);
8991 return copy;
8992 }
8993
8994 /* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8995 ORIG is NULL. */
8996
8997 struct breakpoint *
8998 clone_momentary_breakpoint (struct breakpoint *orig)
8999 {
9000 /* If there's nothing to clone, then return nothing. */
9001 if (orig == NULL)
9002 return NULL;
9003
9004 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
9005 }
9006
9007 struct breakpoint *
9008 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
9009 enum bptype type)
9010 {
9011 struct symtab_and_line sal;
9012
9013 sal = find_pc_line (pc, 0);
9014 sal.pc = pc;
9015 sal.section = find_pc_overlay (pc);
9016 sal.explicit_pc = 1;
9017
9018 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
9019 }
9020 \f
9021
9022 /* Tell the user we have just set a breakpoint B. */
9023
9024 static void
9025 mention (struct breakpoint *b)
9026 {
9027 b->ops->print_mention (b);
9028 if (ui_out_is_mi_like_p (current_uiout))
9029 return;
9030 printf_filtered ("\n");
9031 }
9032 \f
9033
9034 static int bp_loc_is_permanent (struct bp_location *loc);
9035
9036 static struct bp_location *
9037 add_location_to_breakpoint (struct breakpoint *b,
9038 const struct symtab_and_line *sal)
9039 {
9040 struct bp_location *loc, **tmp;
9041 CORE_ADDR adjusted_address;
9042 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
9043
9044 if (loc_gdbarch == NULL)
9045 loc_gdbarch = b->gdbarch;
9046
9047 /* Adjust the breakpoint's address prior to allocating a location.
9048 Once we call allocate_bp_location(), that mostly uninitialized
9049 location will be placed on the location chain. Adjustment of the
9050 breakpoint may cause target_read_memory() to be called and we do
9051 not want its scan of the location chain to find a breakpoint and
9052 location that's only been partially initialized. */
9053 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
9054 sal->pc, b->type);
9055
9056 /* Sort the locations by their ADDRESS. */
9057 loc = allocate_bp_location (b);
9058 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
9059 tmp = &((*tmp)->next))
9060 ;
9061 loc->next = *tmp;
9062 *tmp = loc;
9063
9064 loc->requested_address = sal->pc;
9065 loc->address = adjusted_address;
9066 loc->pspace = sal->pspace;
9067 loc->probe.probe = sal->probe;
9068 loc->probe.objfile = sal->objfile;
9069 gdb_assert (loc->pspace != NULL);
9070 loc->section = sal->section;
9071 loc->gdbarch = loc_gdbarch;
9072 loc->line_number = sal->line;
9073 loc->symtab = sal->symtab;
9074
9075 set_breakpoint_location_function (loc,
9076 sal->explicit_pc || sal->explicit_line);
9077
9078 /* While by definition, permanent breakpoints are already present in the
9079 code, we don't mark the location as inserted. Normally one would expect
9080 that GDB could rely on that breakpoint instruction to stop the program,
9081 thus removing the need to insert its own breakpoint, except that executing
9082 the breakpoint instruction can kill the target instead of reporting a
9083 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
9084 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
9085 with "Trap 0x02 while interrupts disabled, Error state". Letting the
9086 breakpoint be inserted normally results in QEMU knowing about the GDB
9087 breakpoint, and thus trap before the breakpoint instruction is executed.
9088 (If GDB later needs to continue execution past the permanent breakpoint,
9089 it manually increments the PC, thus avoiding executing the breakpoint
9090 instruction.) */
9091 if (bp_loc_is_permanent (loc))
9092 loc->permanent = 1;
9093
9094 return loc;
9095 }
9096 \f
9097
9098 /* See breakpoint.h. */
9099
9100 int
9101 program_breakpoint_here_p (struct gdbarch *gdbarch, CORE_ADDR address)
9102 {
9103 int len;
9104 CORE_ADDR addr;
9105 const gdb_byte *bpoint;
9106 gdb_byte *target_mem;
9107 struct cleanup *cleanup;
9108 int retval = 0;
9109
9110 addr = address;
9111 bpoint = gdbarch_breakpoint_from_pc (gdbarch, &addr, &len);
9112
9113 /* Software breakpoints unsupported? */
9114 if (bpoint == NULL)
9115 return 0;
9116
9117 target_mem = (gdb_byte *) alloca (len);
9118
9119 /* Enable the automatic memory restoration from breakpoints while
9120 we read the memory. Otherwise we could say about our temporary
9121 breakpoints they are permanent. */
9122 cleanup = make_show_memory_breakpoints_cleanup (0);
9123
9124 if (target_read_memory (address, target_mem, len) == 0
9125 && memcmp (target_mem, bpoint, len) == 0)
9126 retval = 1;
9127
9128 do_cleanups (cleanup);
9129
9130 return retval;
9131 }
9132
9133 /* Return 1 if LOC is pointing to a permanent breakpoint,
9134 return 0 otherwise. */
9135
9136 static int
9137 bp_loc_is_permanent (struct bp_location *loc)
9138 {
9139 struct cleanup *cleanup;
9140 int retval;
9141
9142 gdb_assert (loc != NULL);
9143
9144 /* If we have a catchpoint or a watchpoint, just return 0. We should not
9145 attempt to read from the addresses the locations of these breakpoint types
9146 point to. program_breakpoint_here_p, below, will attempt to read
9147 memory. */
9148 if (!breakpoint_address_is_meaningful (loc->owner))
9149 return 0;
9150
9151 cleanup = save_current_space_and_thread ();
9152 switch_to_program_space_and_thread (loc->pspace);
9153
9154 retval = program_breakpoint_here_p (loc->gdbarch, loc->address);
9155
9156 do_cleanups (cleanup);
9157
9158 return retval;
9159 }
9160
9161 /* Build a command list for the dprintf corresponding to the current
9162 settings of the dprintf style options. */
9163
9164 static void
9165 update_dprintf_command_list (struct breakpoint *b)
9166 {
9167 char *dprintf_args = b->extra_string;
9168 char *printf_line = NULL;
9169
9170 if (!dprintf_args)
9171 return;
9172
9173 dprintf_args = skip_spaces (dprintf_args);
9174
9175 /* Allow a comma, as it may have terminated a location, but don't
9176 insist on it. */
9177 if (*dprintf_args == ',')
9178 ++dprintf_args;
9179 dprintf_args = skip_spaces (dprintf_args);
9180
9181 if (*dprintf_args != '"')
9182 error (_("Bad format string, missing '\"'."));
9183
9184 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
9185 printf_line = xstrprintf ("printf %s", dprintf_args);
9186 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
9187 {
9188 if (!dprintf_function)
9189 error (_("No function supplied for dprintf call"));
9190
9191 if (dprintf_channel && strlen (dprintf_channel) > 0)
9192 printf_line = xstrprintf ("call (void) %s (%s,%s)",
9193 dprintf_function,
9194 dprintf_channel,
9195 dprintf_args);
9196 else
9197 printf_line = xstrprintf ("call (void) %s (%s)",
9198 dprintf_function,
9199 dprintf_args);
9200 }
9201 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
9202 {
9203 if (target_can_run_breakpoint_commands ())
9204 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
9205 else
9206 {
9207 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
9208 printf_line = xstrprintf ("printf %s", dprintf_args);
9209 }
9210 }
9211 else
9212 internal_error (__FILE__, __LINE__,
9213 _("Invalid dprintf style."));
9214
9215 gdb_assert (printf_line != NULL);
9216 /* Manufacture a printf sequence. */
9217 {
9218 struct command_line *printf_cmd_line = XNEW (struct command_line);
9219
9220 printf_cmd_line->control_type = simple_control;
9221 printf_cmd_line->body_count = 0;
9222 printf_cmd_line->body_list = NULL;
9223 printf_cmd_line->next = NULL;
9224 printf_cmd_line->line = printf_line;
9225
9226 breakpoint_set_commands (b, printf_cmd_line);
9227 }
9228 }
9229
9230 /* Update all dprintf commands, making their command lists reflect
9231 current style settings. */
9232
9233 static void
9234 update_dprintf_commands (char *args, int from_tty,
9235 struct cmd_list_element *c)
9236 {
9237 struct breakpoint *b;
9238
9239 ALL_BREAKPOINTS (b)
9240 {
9241 if (b->type == bp_dprintf)
9242 update_dprintf_command_list (b);
9243 }
9244 }
9245
9246 /* Create a breakpoint with SAL as location. Use LOCATION
9247 as a description of the location, and COND_STRING
9248 as condition expression. If LOCATION is NULL then create an
9249 "address location" from the address in the SAL. */
9250
9251 static void
9252 init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
9253 struct symtabs_and_lines sals,
9254 struct event_location *location,
9255 char *filter, char *cond_string,
9256 char *extra_string,
9257 enum bptype type, enum bpdisp disposition,
9258 int thread, int task, int ignore_count,
9259 const struct breakpoint_ops *ops, int from_tty,
9260 int enabled, int internal, unsigned flags,
9261 int display_canonical)
9262 {
9263 int i;
9264
9265 if (type == bp_hardware_breakpoint)
9266 {
9267 int target_resources_ok;
9268
9269 i = hw_breakpoint_used_count ();
9270 target_resources_ok =
9271 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9272 i + 1, 0);
9273 if (target_resources_ok == 0)
9274 error (_("No hardware breakpoint support in the target."));
9275 else if (target_resources_ok < 0)
9276 error (_("Hardware breakpoints used exceeds limit."));
9277 }
9278
9279 gdb_assert (sals.nelts > 0);
9280
9281 for (i = 0; i < sals.nelts; ++i)
9282 {
9283 struct symtab_and_line sal = sals.sals[i];
9284 struct bp_location *loc;
9285
9286 if (from_tty)
9287 {
9288 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9289 if (!loc_gdbarch)
9290 loc_gdbarch = gdbarch;
9291
9292 describe_other_breakpoints (loc_gdbarch,
9293 sal.pspace, sal.pc, sal.section, thread);
9294 }
9295
9296 if (i == 0)
9297 {
9298 init_raw_breakpoint (b, gdbarch, sal, type, ops);
9299 b->thread = thread;
9300 b->task = task;
9301
9302 b->cond_string = cond_string;
9303 b->extra_string = extra_string;
9304 b->ignore_count = ignore_count;
9305 b->enable_state = enabled ? bp_enabled : bp_disabled;
9306 b->disposition = disposition;
9307
9308 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9309 b->loc->inserted = 1;
9310
9311 if (type == bp_static_tracepoint)
9312 {
9313 struct tracepoint *t = (struct tracepoint *) b;
9314 struct static_tracepoint_marker marker;
9315
9316 if (strace_marker_p (b))
9317 {
9318 /* We already know the marker exists, otherwise, we
9319 wouldn't see a sal for it. */
9320 const char *p = &event_location_to_string (b->location)[3];
9321 const char *endp;
9322 char *marker_str;
9323
9324 p = skip_spaces_const (p);
9325
9326 endp = skip_to_space_const (p);
9327
9328 marker_str = savestring (p, endp - p);
9329 t->static_trace_marker_id = marker_str;
9330
9331 printf_filtered (_("Probed static tracepoint "
9332 "marker \"%s\"\n"),
9333 t->static_trace_marker_id);
9334 }
9335 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
9336 {
9337 t->static_trace_marker_id = xstrdup (marker.str_id);
9338 release_static_tracepoint_marker (&marker);
9339
9340 printf_filtered (_("Probed static tracepoint "
9341 "marker \"%s\"\n"),
9342 t->static_trace_marker_id);
9343 }
9344 else
9345 warning (_("Couldn't determine the static "
9346 "tracepoint marker to probe"));
9347 }
9348
9349 loc = b->loc;
9350 }
9351 else
9352 {
9353 loc = add_location_to_breakpoint (b, &sal);
9354 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9355 loc->inserted = 1;
9356 }
9357
9358 if (b->cond_string)
9359 {
9360 const char *arg = b->cond_string;
9361
9362 loc->cond = parse_exp_1 (&arg, loc->address,
9363 block_for_pc (loc->address), 0);
9364 if (*arg)
9365 error (_("Garbage '%s' follows condition"), arg);
9366 }
9367
9368 /* Dynamic printf requires and uses additional arguments on the
9369 command line, otherwise it's an error. */
9370 if (type == bp_dprintf)
9371 {
9372 if (b->extra_string)
9373 update_dprintf_command_list (b);
9374 else
9375 error (_("Format string required"));
9376 }
9377 else if (b->extra_string)
9378 error (_("Garbage '%s' at end of command"), b->extra_string);
9379 }
9380
9381 b->display_canonical = display_canonical;
9382 if (location != NULL)
9383 b->location = location;
9384 else
9385 {
9386 const char *addr_string = NULL;
9387 int addr_string_len = 0;
9388
9389 if (location != NULL)
9390 addr_string = event_location_to_string (location);
9391 if (addr_string != NULL)
9392 addr_string_len = strlen (addr_string);
9393
9394 b->location = new_address_location (b->loc->address,
9395 addr_string, addr_string_len);
9396 }
9397 b->filter = filter;
9398 }
9399
9400 static void
9401 create_breakpoint_sal (struct gdbarch *gdbarch,
9402 struct symtabs_and_lines sals,
9403 struct event_location *location,
9404 char *filter, char *cond_string,
9405 char *extra_string,
9406 enum bptype type, enum bpdisp disposition,
9407 int thread, int task, int ignore_count,
9408 const struct breakpoint_ops *ops, int from_tty,
9409 int enabled, int internal, unsigned flags,
9410 int display_canonical)
9411 {
9412 struct breakpoint *b;
9413 struct cleanup *old_chain;
9414
9415 if (is_tracepoint_type (type))
9416 {
9417 struct tracepoint *t;
9418
9419 t = new tracepoint ();
9420 b = &t->base;
9421 }
9422 else
9423 b = new breakpoint ();
9424
9425 old_chain = make_cleanup (xfree, b);
9426
9427 init_breakpoint_sal (b, gdbarch,
9428 sals, location,
9429 filter, cond_string, extra_string,
9430 type, disposition,
9431 thread, task, ignore_count,
9432 ops, from_tty,
9433 enabled, internal, flags,
9434 display_canonical);
9435 discard_cleanups (old_chain);
9436
9437 install_breakpoint (internal, b, 0);
9438 }
9439
9440 /* Add SALS.nelts breakpoints to the breakpoint table. For each
9441 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9442 value. COND_STRING, if not NULL, specified the condition to be
9443 used for all breakpoints. Essentially the only case where
9444 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9445 function. In that case, it's still not possible to specify
9446 separate conditions for different overloaded functions, so
9447 we take just a single condition string.
9448
9449 NOTE: If the function succeeds, the caller is expected to cleanup
9450 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
9451 array contents). If the function fails (error() is called), the
9452 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
9453 COND and SALS arrays and each of those arrays contents. */
9454
9455 static void
9456 create_breakpoints_sal (struct gdbarch *gdbarch,
9457 struct linespec_result *canonical,
9458 char *cond_string, char *extra_string,
9459 enum bptype type, enum bpdisp disposition,
9460 int thread, int task, int ignore_count,
9461 const struct breakpoint_ops *ops, int from_tty,
9462 int enabled, int internal, unsigned flags)
9463 {
9464 int i;
9465 struct linespec_sals *lsal;
9466
9467 if (canonical->pre_expanded)
9468 gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
9469
9470 for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
9471 {
9472 /* Note that 'location' can be NULL in the case of a plain
9473 'break', without arguments. */
9474 struct event_location *location
9475 = (canonical->location != NULL
9476 ? copy_event_location (canonical->location) : NULL);
9477 char *filter_string = lsal->canonical ? xstrdup (lsal->canonical) : NULL;
9478 struct cleanup *inner = make_cleanup_delete_event_location (location);
9479
9480 make_cleanup (xfree, filter_string);
9481 create_breakpoint_sal (gdbarch, lsal->sals,
9482 location,
9483 filter_string,
9484 cond_string, extra_string,
9485 type, disposition,
9486 thread, task, ignore_count, ops,
9487 from_tty, enabled, internal, flags,
9488 canonical->special_display);
9489 discard_cleanups (inner);
9490 }
9491 }
9492
9493 /* Parse LOCATION which is assumed to be a SAL specification possibly
9494 followed by conditionals. On return, SALS contains an array of SAL
9495 addresses found. LOCATION points to the end of the SAL (for
9496 linespec locations).
9497
9498 The array and the line spec strings are allocated on the heap, it is
9499 the caller's responsibility to free them. */
9500
9501 static void
9502 parse_breakpoint_sals (const struct event_location *location,
9503 struct linespec_result *canonical)
9504 {
9505 struct symtab_and_line cursal;
9506
9507 if (event_location_type (location) == LINESPEC_LOCATION)
9508 {
9509 const char *address = get_linespec_location (location);
9510
9511 if (address == NULL)
9512 {
9513 /* The last displayed codepoint, if it's valid, is our default
9514 breakpoint address. */
9515 if (last_displayed_sal_is_valid ())
9516 {
9517 struct linespec_sals lsal;
9518 struct symtab_and_line sal;
9519 CORE_ADDR pc;
9520
9521 init_sal (&sal); /* Initialize to zeroes. */
9522 lsal.sals.sals = XNEW (struct symtab_and_line);
9523
9524 /* Set sal's pspace, pc, symtab, and line to the values
9525 corresponding to the last call to print_frame_info.
9526 Be sure to reinitialize LINE with NOTCURRENT == 0
9527 as the breakpoint line number is inappropriate otherwise.
9528 find_pc_line would adjust PC, re-set it back. */
9529 get_last_displayed_sal (&sal);
9530 pc = sal.pc;
9531 sal = find_pc_line (pc, 0);
9532
9533 /* "break" without arguments is equivalent to "break *PC"
9534 where PC is the last displayed codepoint's address. So
9535 make sure to set sal.explicit_pc to prevent GDB from
9536 trying to expand the list of sals to include all other
9537 instances with the same symtab and line. */
9538 sal.pc = pc;
9539 sal.explicit_pc = 1;
9540
9541 lsal.sals.sals[0] = sal;
9542 lsal.sals.nelts = 1;
9543 lsal.canonical = NULL;
9544
9545 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
9546 return;
9547 }
9548 else
9549 error (_("No default breakpoint address now."));
9550 }
9551 }
9552
9553 /* Force almost all breakpoints to be in terms of the
9554 current_source_symtab (which is decode_line_1's default).
9555 This should produce the results we want almost all of the
9556 time while leaving default_breakpoint_* alone.
9557
9558 ObjC: However, don't match an Objective-C method name which
9559 may have a '+' or '-' succeeded by a '['. */
9560 cursal = get_current_source_symtab_and_line ();
9561 if (last_displayed_sal_is_valid ())
9562 {
9563 const char *address = NULL;
9564
9565 if (event_location_type (location) == LINESPEC_LOCATION)
9566 address = get_linespec_location (location);
9567
9568 if (!cursal.symtab
9569 || (address != NULL
9570 && strchr ("+-", address[0]) != NULL
9571 && address[1] != '['))
9572 {
9573 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9574 get_last_displayed_symtab (),
9575 get_last_displayed_line (),
9576 canonical, NULL, NULL);
9577 return;
9578 }
9579 }
9580
9581 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, NULL,
9582 cursal.symtab, cursal.line, canonical, NULL, NULL);
9583 }
9584
9585
9586 /* Convert each SAL into a real PC. Verify that the PC can be
9587 inserted as a breakpoint. If it can't throw an error. */
9588
9589 static void
9590 breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
9591 {
9592 int i;
9593
9594 for (i = 0; i < sals->nelts; i++)
9595 resolve_sal_pc (&sals->sals[i]);
9596 }
9597
9598 /* Fast tracepoints may have restrictions on valid locations. For
9599 instance, a fast tracepoint using a jump instead of a trap will
9600 likely have to overwrite more bytes than a trap would, and so can
9601 only be placed where the instruction is longer than the jump, or a
9602 multi-instruction sequence does not have a jump into the middle of
9603 it, etc. */
9604
9605 static void
9606 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9607 struct symtabs_and_lines *sals)
9608 {
9609 int i, rslt;
9610 struct symtab_and_line *sal;
9611 char *msg;
9612 struct cleanup *old_chain;
9613
9614 for (i = 0; i < sals->nelts; i++)
9615 {
9616 struct gdbarch *sarch;
9617
9618 sal = &sals->sals[i];
9619
9620 sarch = get_sal_arch (*sal);
9621 /* We fall back to GDBARCH if there is no architecture
9622 associated with SAL. */
9623 if (sarch == NULL)
9624 sarch = gdbarch;
9625 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc, &msg);
9626 old_chain = make_cleanup (xfree, msg);
9627
9628 if (!rslt)
9629 error (_("May not have a fast tracepoint at %s%s"),
9630 paddress (sarch, sal->pc), (msg ? msg : ""));
9631
9632 do_cleanups (old_chain);
9633 }
9634 }
9635
9636 /* Given TOK, a string specification of condition and thread, as
9637 accepted by the 'break' command, extract the condition
9638 string and thread number and set *COND_STRING and *THREAD.
9639 PC identifies the context at which the condition should be parsed.
9640 If no condition is found, *COND_STRING is set to NULL.
9641 If no thread is found, *THREAD is set to -1. */
9642
9643 static void
9644 find_condition_and_thread (const char *tok, CORE_ADDR pc,
9645 char **cond_string, int *thread, int *task,
9646 char **rest)
9647 {
9648 *cond_string = NULL;
9649 *thread = -1;
9650 *task = 0;
9651 *rest = NULL;
9652
9653 while (tok && *tok)
9654 {
9655 const char *end_tok;
9656 int toklen;
9657 const char *cond_start = NULL;
9658 const char *cond_end = NULL;
9659
9660 tok = skip_spaces_const (tok);
9661
9662 if ((*tok == '"' || *tok == ',') && rest)
9663 {
9664 *rest = savestring (tok, strlen (tok));
9665 return;
9666 }
9667
9668 end_tok = skip_to_space_const (tok);
9669
9670 toklen = end_tok - tok;
9671
9672 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9673 {
9674 tok = cond_start = end_tok + 1;
9675 parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
9676 cond_end = tok;
9677 *cond_string = savestring (cond_start, cond_end - cond_start);
9678 }
9679 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9680 {
9681 const char *tmptok;
9682 struct thread_info *thr;
9683
9684 tok = end_tok + 1;
9685 thr = parse_thread_id (tok, &tmptok);
9686 if (tok == tmptok)
9687 error (_("Junk after thread keyword."));
9688 *thread = thr->global_num;
9689 tok = tmptok;
9690 }
9691 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9692 {
9693 char *tmptok;
9694
9695 tok = end_tok + 1;
9696 *task = strtol (tok, &tmptok, 0);
9697 if (tok == tmptok)
9698 error (_("Junk after task keyword."));
9699 if (!valid_task_id (*task))
9700 error (_("Unknown task %d."), *task);
9701 tok = tmptok;
9702 }
9703 else if (rest)
9704 {
9705 *rest = savestring (tok, strlen (tok));
9706 return;
9707 }
9708 else
9709 error (_("Junk at end of arguments."));
9710 }
9711 }
9712
9713 /* Decode a static tracepoint marker spec. */
9714
9715 static struct symtabs_and_lines
9716 decode_static_tracepoint_spec (const char **arg_p)
9717 {
9718 VEC(static_tracepoint_marker_p) *markers = NULL;
9719 struct symtabs_and_lines sals;
9720 struct cleanup *old_chain;
9721 const char *p = &(*arg_p)[3];
9722 const char *endp;
9723 char *marker_str;
9724 int i;
9725
9726 p = skip_spaces_const (p);
9727
9728 endp = skip_to_space_const (p);
9729
9730 marker_str = savestring (p, endp - p);
9731 old_chain = make_cleanup (xfree, marker_str);
9732
9733 markers = target_static_tracepoint_markers_by_strid (marker_str);
9734 if (VEC_empty(static_tracepoint_marker_p, markers))
9735 error (_("No known static tracepoint marker named %s"), marker_str);
9736
9737 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
9738 sals.sals = XNEWVEC (struct symtab_and_line, sals.nelts);
9739
9740 for (i = 0; i < sals.nelts; i++)
9741 {
9742 struct static_tracepoint_marker *marker;
9743
9744 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9745
9746 init_sal (&sals.sals[i]);
9747
9748 sals.sals[i] = find_pc_line (marker->address, 0);
9749 sals.sals[i].pc = marker->address;
9750
9751 release_static_tracepoint_marker (marker);
9752 }
9753
9754 do_cleanups (old_chain);
9755
9756 *arg_p = endp;
9757 return sals;
9758 }
9759
9760 /* See breakpoint.h. */
9761
9762 int
9763 create_breakpoint (struct gdbarch *gdbarch,
9764 const struct event_location *location, char *cond_string,
9765 int thread, char *extra_string,
9766 int parse_extra,
9767 int tempflag, enum bptype type_wanted,
9768 int ignore_count,
9769 enum auto_boolean pending_break_support,
9770 const struct breakpoint_ops *ops,
9771 int from_tty, int enabled, int internal,
9772 unsigned flags)
9773 {
9774 struct linespec_result canonical;
9775 struct cleanup *old_chain;
9776 struct cleanup *bkpt_chain = NULL;
9777 int pending = 0;
9778 int task = 0;
9779 int prev_bkpt_count = breakpoint_count;
9780
9781 gdb_assert (ops != NULL);
9782
9783 /* If extra_string isn't useful, set it to NULL. */
9784 if (extra_string != NULL && *extra_string == '\0')
9785 extra_string = NULL;
9786
9787 init_linespec_result (&canonical);
9788
9789 TRY
9790 {
9791 ops->create_sals_from_location (location, &canonical, type_wanted);
9792 }
9793 CATCH (e, RETURN_MASK_ERROR)
9794 {
9795 /* If caller is interested in rc value from parse, set
9796 value. */
9797 if (e.error == NOT_FOUND_ERROR)
9798 {
9799 /* If pending breakpoint support is turned off, throw
9800 error. */
9801
9802 if (pending_break_support == AUTO_BOOLEAN_FALSE)
9803 throw_exception (e);
9804
9805 exception_print (gdb_stderr, e);
9806
9807 /* If pending breakpoint support is auto query and the user
9808 selects no, then simply return the error code. */
9809 if (pending_break_support == AUTO_BOOLEAN_AUTO
9810 && !nquery (_("Make %s pending on future shared library load? "),
9811 bptype_string (type_wanted)))
9812 return 0;
9813
9814 /* At this point, either the user was queried about setting
9815 a pending breakpoint and selected yes, or pending
9816 breakpoint behavior is on and thus a pending breakpoint
9817 is defaulted on behalf of the user. */
9818 pending = 1;
9819 }
9820 else
9821 throw_exception (e);
9822 }
9823 END_CATCH
9824
9825 if (!pending && VEC_empty (linespec_sals, canonical.sals))
9826 return 0;
9827
9828 /* Create a chain of things that always need to be cleaned up. */
9829 old_chain = make_cleanup_destroy_linespec_result (&canonical);
9830
9831 /* ----------------------------- SNIP -----------------------------
9832 Anything added to the cleanup chain beyond this point is assumed
9833 to be part of a breakpoint. If the breakpoint create succeeds
9834 then the memory is not reclaimed. */
9835 bkpt_chain = make_cleanup (null_cleanup, 0);
9836
9837 /* Resolve all line numbers to PC's and verify that the addresses
9838 are ok for the target. */
9839 if (!pending)
9840 {
9841 int ix;
9842 struct linespec_sals *iter;
9843
9844 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9845 breakpoint_sals_to_pc (&iter->sals);
9846 }
9847
9848 /* Fast tracepoints may have additional restrictions on location. */
9849 if (!pending && type_wanted == bp_fast_tracepoint)
9850 {
9851 int ix;
9852 struct linespec_sals *iter;
9853
9854 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9855 check_fast_tracepoint_sals (gdbarch, &iter->sals);
9856 }
9857
9858 /* Verify that condition can be parsed, before setting any
9859 breakpoints. Allocate a separate condition expression for each
9860 breakpoint. */
9861 if (!pending)
9862 {
9863 if (parse_extra)
9864 {
9865 char *rest;
9866 struct linespec_sals *lsal;
9867
9868 lsal = VEC_index (linespec_sals, canonical.sals, 0);
9869
9870 /* Here we only parse 'arg' to separate condition
9871 from thread number, so parsing in context of first
9872 sal is OK. When setting the breakpoint we'll
9873 re-parse it in context of each sal. */
9874
9875 find_condition_and_thread (extra_string, lsal->sals.sals[0].pc,
9876 &cond_string, &thread, &task, &rest);
9877 if (cond_string)
9878 make_cleanup (xfree, cond_string);
9879 if (rest)
9880 make_cleanup (xfree, rest);
9881 if (rest)
9882 extra_string = rest;
9883 else
9884 extra_string = NULL;
9885 }
9886 else
9887 {
9888 if (type_wanted != bp_dprintf
9889 && extra_string != NULL && *extra_string != '\0')
9890 error (_("Garbage '%s' at end of location"), extra_string);
9891
9892 /* Create a private copy of condition string. */
9893 if (cond_string)
9894 {
9895 cond_string = xstrdup (cond_string);
9896 make_cleanup (xfree, cond_string);
9897 }
9898 /* Create a private copy of any extra string. */
9899 if (extra_string)
9900 {
9901 extra_string = xstrdup (extra_string);
9902 make_cleanup (xfree, extra_string);
9903 }
9904 }
9905
9906 ops->create_breakpoints_sal (gdbarch, &canonical,
9907 cond_string, extra_string, type_wanted,
9908 tempflag ? disp_del : disp_donttouch,
9909 thread, task, ignore_count, ops,
9910 from_tty, enabled, internal, flags);
9911 }
9912 else
9913 {
9914 struct breakpoint *b;
9915
9916 if (is_tracepoint_type (type_wanted))
9917 {
9918 struct tracepoint *t;
9919
9920 t = new tracepoint ();
9921 b = &t->base;
9922 }
9923 else
9924 b = new breakpoint ();
9925
9926 init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops);
9927 b->location = copy_event_location (location);
9928
9929 if (parse_extra)
9930 b->cond_string = NULL;
9931 else
9932 {
9933 /* Create a private copy of condition string. */
9934 if (cond_string)
9935 {
9936 cond_string = xstrdup (cond_string);
9937 make_cleanup (xfree, cond_string);
9938 }
9939 b->cond_string = cond_string;
9940 b->thread = thread;
9941 }
9942
9943 /* Create a private copy of any extra string. */
9944 if (extra_string != NULL)
9945 {
9946 extra_string = xstrdup (extra_string);
9947 make_cleanup (xfree, extra_string);
9948 }
9949 b->extra_string = extra_string;
9950 b->ignore_count = ignore_count;
9951 b->disposition = tempflag ? disp_del : disp_donttouch;
9952 b->condition_not_parsed = 1;
9953 b->enable_state = enabled ? bp_enabled : bp_disabled;
9954 if ((type_wanted != bp_breakpoint
9955 && type_wanted != bp_hardware_breakpoint) || thread != -1)
9956 b->pspace = current_program_space;
9957
9958 install_breakpoint (internal, b, 0);
9959 }
9960
9961 if (VEC_length (linespec_sals, canonical.sals) > 1)
9962 {
9963 warning (_("Multiple breakpoints were set.\nUse the "
9964 "\"delete\" command to delete unwanted breakpoints."));
9965 prev_breakpoint_count = prev_bkpt_count;
9966 }
9967
9968 /* That's it. Discard the cleanups for data inserted into the
9969 breakpoint. */
9970 discard_cleanups (bkpt_chain);
9971 /* But cleanup everything else. */
9972 do_cleanups (old_chain);
9973
9974 /* error call may happen here - have BKPT_CHAIN already discarded. */
9975 update_global_location_list (UGLL_MAY_INSERT);
9976
9977 return 1;
9978 }
9979
9980 /* Set a breakpoint.
9981 ARG is a string describing breakpoint address,
9982 condition, and thread.
9983 FLAG specifies if a breakpoint is hardware on,
9984 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9985 and BP_TEMPFLAG. */
9986
9987 static void
9988 break_command_1 (char *arg, int flag, int from_tty)
9989 {
9990 int tempflag = flag & BP_TEMPFLAG;
9991 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9992 ? bp_hardware_breakpoint
9993 : bp_breakpoint);
9994 struct breakpoint_ops *ops;
9995 struct event_location *location;
9996 struct cleanup *cleanup;
9997
9998 location = string_to_event_location (&arg, current_language);
9999 cleanup = make_cleanup_delete_event_location (location);
10000
10001 /* Matching breakpoints on probes. */
10002 if (location != NULL
10003 && event_location_type (location) == PROBE_LOCATION)
10004 ops = &bkpt_probe_breakpoint_ops;
10005 else
10006 ops = &bkpt_breakpoint_ops;
10007
10008 create_breakpoint (get_current_arch (),
10009 location,
10010 NULL, 0, arg, 1 /* parse arg */,
10011 tempflag, type_wanted,
10012 0 /* Ignore count */,
10013 pending_break_support,
10014 ops,
10015 from_tty,
10016 1 /* enabled */,
10017 0 /* internal */,
10018 0);
10019 do_cleanups (cleanup);
10020 }
10021
10022 /* Helper function for break_command_1 and disassemble_command. */
10023
10024 void
10025 resolve_sal_pc (struct symtab_and_line *sal)
10026 {
10027 CORE_ADDR pc;
10028
10029 if (sal->pc == 0 && sal->symtab != NULL)
10030 {
10031 if (!find_line_pc (sal->symtab, sal->line, &pc))
10032 error (_("No line %d in file \"%s\"."),
10033 sal->line, symtab_to_filename_for_display (sal->symtab));
10034 sal->pc = pc;
10035
10036 /* If this SAL corresponds to a breakpoint inserted using a line
10037 number, then skip the function prologue if necessary. */
10038 if (sal->explicit_line)
10039 skip_prologue_sal (sal);
10040 }
10041
10042 if (sal->section == 0 && sal->symtab != NULL)
10043 {
10044 const struct blockvector *bv;
10045 const struct block *b;
10046 struct symbol *sym;
10047
10048 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
10049 SYMTAB_COMPUNIT (sal->symtab));
10050 if (bv != NULL)
10051 {
10052 sym = block_linkage_function (b);
10053 if (sym != NULL)
10054 {
10055 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
10056 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
10057 sym);
10058 }
10059 else
10060 {
10061 /* It really is worthwhile to have the section, so we'll
10062 just have to look harder. This case can be executed
10063 if we have line numbers but no functions (as can
10064 happen in assembly source). */
10065
10066 struct bound_minimal_symbol msym;
10067 struct cleanup *old_chain = save_current_space_and_thread ();
10068
10069 switch_to_program_space_and_thread (sal->pspace);
10070
10071 msym = lookup_minimal_symbol_by_pc (sal->pc);
10072 if (msym.minsym)
10073 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
10074
10075 do_cleanups (old_chain);
10076 }
10077 }
10078 }
10079 }
10080
10081 void
10082 break_command (char *arg, int from_tty)
10083 {
10084 break_command_1 (arg, 0, from_tty);
10085 }
10086
10087 void
10088 tbreak_command (char *arg, int from_tty)
10089 {
10090 break_command_1 (arg, BP_TEMPFLAG, from_tty);
10091 }
10092
10093 static void
10094 hbreak_command (char *arg, int from_tty)
10095 {
10096 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
10097 }
10098
10099 static void
10100 thbreak_command (char *arg, int from_tty)
10101 {
10102 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
10103 }
10104
10105 static void
10106 stop_command (char *arg, int from_tty)
10107 {
10108 printf_filtered (_("Specify the type of breakpoint to set.\n\
10109 Usage: stop in <function | address>\n\
10110 stop at <line>\n"));
10111 }
10112
10113 static void
10114 stopin_command (char *arg, int from_tty)
10115 {
10116 int badInput = 0;
10117
10118 if (arg == (char *) NULL)
10119 badInput = 1;
10120 else if (*arg != '*')
10121 {
10122 char *argptr = arg;
10123 int hasColon = 0;
10124
10125 /* Look for a ':'. If this is a line number specification, then
10126 say it is bad, otherwise, it should be an address or
10127 function/method name. */
10128 while (*argptr && !hasColon)
10129 {
10130 hasColon = (*argptr == ':');
10131 argptr++;
10132 }
10133
10134 if (hasColon)
10135 badInput = (*argptr != ':'); /* Not a class::method */
10136 else
10137 badInput = isdigit (*arg); /* a simple line number */
10138 }
10139
10140 if (badInput)
10141 printf_filtered (_("Usage: stop in <function | address>\n"));
10142 else
10143 break_command_1 (arg, 0, from_tty);
10144 }
10145
10146 static void
10147 stopat_command (char *arg, int from_tty)
10148 {
10149 int badInput = 0;
10150
10151 if (arg == (char *) NULL || *arg == '*') /* no line number */
10152 badInput = 1;
10153 else
10154 {
10155 char *argptr = arg;
10156 int hasColon = 0;
10157
10158 /* Look for a ':'. If there is a '::' then get out, otherwise
10159 it is probably a line number. */
10160 while (*argptr && !hasColon)
10161 {
10162 hasColon = (*argptr == ':');
10163 argptr++;
10164 }
10165
10166 if (hasColon)
10167 badInput = (*argptr == ':'); /* we have class::method */
10168 else
10169 badInput = !isdigit (*arg); /* not a line number */
10170 }
10171
10172 if (badInput)
10173 printf_filtered (_("Usage: stop at <line>\n"));
10174 else
10175 break_command_1 (arg, 0, from_tty);
10176 }
10177
10178 /* The dynamic printf command is mostly like a regular breakpoint, but
10179 with a prewired command list consisting of a single output command,
10180 built from extra arguments supplied on the dprintf command
10181 line. */
10182
10183 static void
10184 dprintf_command (char *arg, int from_tty)
10185 {
10186 struct event_location *location;
10187 struct cleanup *cleanup;
10188
10189 location = string_to_event_location (&arg, current_language);
10190 cleanup = make_cleanup_delete_event_location (location);
10191
10192 /* If non-NULL, ARG should have been advanced past the location;
10193 the next character must be ','. */
10194 if (arg != NULL)
10195 {
10196 if (arg[0] != ',' || arg[1] == '\0')
10197 error (_("Format string required"));
10198 else
10199 {
10200 /* Skip the comma. */
10201 ++arg;
10202 }
10203 }
10204
10205 create_breakpoint (get_current_arch (),
10206 location,
10207 NULL, 0, arg, 1 /* parse arg */,
10208 0, bp_dprintf,
10209 0 /* Ignore count */,
10210 pending_break_support,
10211 &dprintf_breakpoint_ops,
10212 from_tty,
10213 1 /* enabled */,
10214 0 /* internal */,
10215 0);
10216 do_cleanups (cleanup);
10217 }
10218
10219 static void
10220 agent_printf_command (char *arg, int from_tty)
10221 {
10222 error (_("May only run agent-printf on the target"));
10223 }
10224
10225 /* Implement the "breakpoint_hit" breakpoint_ops method for
10226 ranged breakpoints. */
10227
10228 static int
10229 breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
10230 struct address_space *aspace,
10231 CORE_ADDR bp_addr,
10232 const struct target_waitstatus *ws)
10233 {
10234 if (ws->kind != TARGET_WAITKIND_STOPPED
10235 || ws->value.sig != GDB_SIGNAL_TRAP)
10236 return 0;
10237
10238 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
10239 bl->length, aspace, bp_addr);
10240 }
10241
10242 /* Implement the "resources_needed" breakpoint_ops method for
10243 ranged breakpoints. */
10244
10245 static int
10246 resources_needed_ranged_breakpoint (const struct bp_location *bl)
10247 {
10248 return target_ranged_break_num_registers ();
10249 }
10250
10251 /* Implement the "print_it" breakpoint_ops method for
10252 ranged breakpoints. */
10253
10254 static enum print_stop_action
10255 print_it_ranged_breakpoint (bpstat bs)
10256 {
10257 struct breakpoint *b = bs->breakpoint_at;
10258 struct bp_location *bl = b->loc;
10259 struct ui_out *uiout = current_uiout;
10260
10261 gdb_assert (b->type == bp_hardware_breakpoint);
10262
10263 /* Ranged breakpoints have only one location. */
10264 gdb_assert (bl && bl->next == NULL);
10265
10266 annotate_breakpoint (b->number);
10267
10268 maybe_print_thread_hit_breakpoint (uiout);
10269
10270 if (b->disposition == disp_del)
10271 ui_out_text (uiout, "Temporary ranged breakpoint ");
10272 else
10273 ui_out_text (uiout, "Ranged breakpoint ");
10274 if (ui_out_is_mi_like_p (uiout))
10275 {
10276 ui_out_field_string (uiout, "reason",
10277 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
10278 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
10279 }
10280 ui_out_field_int (uiout, "bkptno", b->number);
10281 ui_out_text (uiout, ", ");
10282
10283 return PRINT_SRC_AND_LOC;
10284 }
10285
10286 /* Implement the "print_one" breakpoint_ops method for
10287 ranged breakpoints. */
10288
10289 static void
10290 print_one_ranged_breakpoint (struct breakpoint *b,
10291 struct bp_location **last_loc)
10292 {
10293 struct bp_location *bl = b->loc;
10294 struct value_print_options opts;
10295 struct ui_out *uiout = current_uiout;
10296
10297 /* Ranged breakpoints have only one location. */
10298 gdb_assert (bl && bl->next == NULL);
10299
10300 get_user_print_options (&opts);
10301
10302 if (opts.addressprint)
10303 /* We don't print the address range here, it will be printed later
10304 by print_one_detail_ranged_breakpoint. */
10305 ui_out_field_skip (uiout, "addr");
10306 annotate_field (5);
10307 print_breakpoint_location (b, bl);
10308 *last_loc = bl;
10309 }
10310
10311 /* Implement the "print_one_detail" breakpoint_ops method for
10312 ranged breakpoints. */
10313
10314 static void
10315 print_one_detail_ranged_breakpoint (const struct breakpoint *b,
10316 struct ui_out *uiout)
10317 {
10318 CORE_ADDR address_start, address_end;
10319 struct bp_location *bl = b->loc;
10320 struct ui_file *stb = mem_fileopen ();
10321 struct cleanup *cleanup = make_cleanup_ui_file_delete (stb);
10322
10323 gdb_assert (bl);
10324
10325 address_start = bl->address;
10326 address_end = address_start + bl->length - 1;
10327
10328 ui_out_text (uiout, "\taddress range: ");
10329 fprintf_unfiltered (stb, "[%s, %s]",
10330 print_core_address (bl->gdbarch, address_start),
10331 print_core_address (bl->gdbarch, address_end));
10332 ui_out_field_stream (uiout, "addr", stb);
10333 ui_out_text (uiout, "\n");
10334
10335 do_cleanups (cleanup);
10336 }
10337
10338 /* Implement the "print_mention" breakpoint_ops method for
10339 ranged breakpoints. */
10340
10341 static void
10342 print_mention_ranged_breakpoint (struct breakpoint *b)
10343 {
10344 struct bp_location *bl = b->loc;
10345 struct ui_out *uiout = current_uiout;
10346
10347 gdb_assert (bl);
10348 gdb_assert (b->type == bp_hardware_breakpoint);
10349
10350 if (ui_out_is_mi_like_p (uiout))
10351 return;
10352
10353 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
10354 b->number, paddress (bl->gdbarch, bl->address),
10355 paddress (bl->gdbarch, bl->address + bl->length - 1));
10356 }
10357
10358 /* Implement the "print_recreate" breakpoint_ops method for
10359 ranged breakpoints. */
10360
10361 static void
10362 print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
10363 {
10364 fprintf_unfiltered (fp, "break-range %s, %s",
10365 event_location_to_string (b->location),
10366 event_location_to_string (b->location_range_end));
10367 print_recreate_thread (b, fp);
10368 }
10369
10370 /* The breakpoint_ops structure to be used in ranged breakpoints. */
10371
10372 static struct breakpoint_ops ranged_breakpoint_ops;
10373
10374 /* Find the address where the end of the breakpoint range should be
10375 placed, given the SAL of the end of the range. This is so that if
10376 the user provides a line number, the end of the range is set to the
10377 last instruction of the given line. */
10378
10379 static CORE_ADDR
10380 find_breakpoint_range_end (struct symtab_and_line sal)
10381 {
10382 CORE_ADDR end;
10383
10384 /* If the user provided a PC value, use it. Otherwise,
10385 find the address of the end of the given location. */
10386 if (sal.explicit_pc)
10387 end = sal.pc;
10388 else
10389 {
10390 int ret;
10391 CORE_ADDR start;
10392
10393 ret = find_line_pc_range (sal, &start, &end);
10394 if (!ret)
10395 error (_("Could not find location of the end of the range."));
10396
10397 /* find_line_pc_range returns the start of the next line. */
10398 end--;
10399 }
10400
10401 return end;
10402 }
10403
10404 /* Implement the "break-range" CLI command. */
10405
10406 static void
10407 break_range_command (char *arg, int from_tty)
10408 {
10409 char *arg_start, *addr_string_start;
10410 struct linespec_result canonical_start, canonical_end;
10411 int bp_count, can_use_bp, length;
10412 CORE_ADDR end;
10413 struct breakpoint *b;
10414 struct symtab_and_line sal_start, sal_end;
10415 struct cleanup *cleanup_bkpt;
10416 struct linespec_sals *lsal_start, *lsal_end;
10417 struct event_location *start_location, *end_location;
10418
10419 /* We don't support software ranged breakpoints. */
10420 if (target_ranged_break_num_registers () < 0)
10421 error (_("This target does not support hardware ranged breakpoints."));
10422
10423 bp_count = hw_breakpoint_used_count ();
10424 bp_count += target_ranged_break_num_registers ();
10425 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10426 bp_count, 0);
10427 if (can_use_bp < 0)
10428 error (_("Hardware breakpoints used exceeds limit."));
10429
10430 arg = skip_spaces (arg);
10431 if (arg == NULL || arg[0] == '\0')
10432 error(_("No address range specified."));
10433
10434 init_linespec_result (&canonical_start);
10435
10436 arg_start = arg;
10437 start_location = string_to_event_location (&arg, current_language);
10438 cleanup_bkpt = make_cleanup_delete_event_location (start_location);
10439 parse_breakpoint_sals (start_location, &canonical_start);
10440 make_cleanup_destroy_linespec_result (&canonical_start);
10441
10442 if (arg[0] != ',')
10443 error (_("Too few arguments."));
10444 else if (VEC_empty (linespec_sals, canonical_start.sals))
10445 error (_("Could not find location of the beginning of the range."));
10446
10447 lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
10448
10449 if (VEC_length (linespec_sals, canonical_start.sals) > 1
10450 || lsal_start->sals.nelts != 1)
10451 error (_("Cannot create a ranged breakpoint with multiple locations."));
10452
10453 sal_start = lsal_start->sals.sals[0];
10454 addr_string_start = savestring (arg_start, arg - arg_start);
10455 make_cleanup (xfree, addr_string_start);
10456
10457 arg++; /* Skip the comma. */
10458 arg = skip_spaces (arg);
10459
10460 /* Parse the end location. */
10461
10462 init_linespec_result (&canonical_end);
10463 arg_start = arg;
10464
10465 /* We call decode_line_full directly here instead of using
10466 parse_breakpoint_sals because we need to specify the start location's
10467 symtab and line as the default symtab and line for the end of the
10468 range. This makes it possible to have ranges like "foo.c:27, +14",
10469 where +14 means 14 lines from the start location. */
10470 end_location = string_to_event_location (&arg, current_language);
10471 make_cleanup_delete_event_location (end_location);
10472 decode_line_full (end_location, DECODE_LINE_FUNFIRSTLINE, NULL,
10473 sal_start.symtab, sal_start.line,
10474 &canonical_end, NULL, NULL);
10475
10476 make_cleanup_destroy_linespec_result (&canonical_end);
10477
10478 if (VEC_empty (linespec_sals, canonical_end.sals))
10479 error (_("Could not find location of the end of the range."));
10480
10481 lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
10482 if (VEC_length (linespec_sals, canonical_end.sals) > 1
10483 || lsal_end->sals.nelts != 1)
10484 error (_("Cannot create a ranged breakpoint with multiple locations."));
10485
10486 sal_end = lsal_end->sals.sals[0];
10487
10488 end = find_breakpoint_range_end (sal_end);
10489 if (sal_start.pc > end)
10490 error (_("Invalid address range, end precedes start."));
10491
10492 length = end - sal_start.pc + 1;
10493 if (length < 0)
10494 /* Length overflowed. */
10495 error (_("Address range too large."));
10496 else if (length == 1)
10497 {
10498 /* This range is simple enough to be handled by
10499 the `hbreak' command. */
10500 hbreak_command (addr_string_start, 1);
10501
10502 do_cleanups (cleanup_bkpt);
10503
10504 return;
10505 }
10506
10507 /* Now set up the breakpoint. */
10508 b = set_raw_breakpoint (get_current_arch (), sal_start,
10509 bp_hardware_breakpoint, &ranged_breakpoint_ops);
10510 set_breakpoint_count (breakpoint_count + 1);
10511 b->number = breakpoint_count;
10512 b->disposition = disp_donttouch;
10513 b->location = copy_event_location (start_location);
10514 b->location_range_end = copy_event_location (end_location);
10515 b->loc->length = length;
10516
10517 do_cleanups (cleanup_bkpt);
10518
10519 mention (b);
10520 observer_notify_breakpoint_created (b);
10521 update_global_location_list (UGLL_MAY_INSERT);
10522 }
10523
10524 /* Return non-zero if EXP is verified as constant. Returned zero
10525 means EXP is variable. Also the constant detection may fail for
10526 some constant expressions and in such case still falsely return
10527 zero. */
10528
10529 static int
10530 watchpoint_exp_is_const (const struct expression *exp)
10531 {
10532 int i = exp->nelts;
10533
10534 while (i > 0)
10535 {
10536 int oplenp, argsp;
10537
10538 /* We are only interested in the descriptor of each element. */
10539 operator_length (exp, i, &oplenp, &argsp);
10540 i -= oplenp;
10541
10542 switch (exp->elts[i].opcode)
10543 {
10544 case BINOP_ADD:
10545 case BINOP_SUB:
10546 case BINOP_MUL:
10547 case BINOP_DIV:
10548 case BINOP_REM:
10549 case BINOP_MOD:
10550 case BINOP_LSH:
10551 case BINOP_RSH:
10552 case BINOP_LOGICAL_AND:
10553 case BINOP_LOGICAL_OR:
10554 case BINOP_BITWISE_AND:
10555 case BINOP_BITWISE_IOR:
10556 case BINOP_BITWISE_XOR:
10557 case BINOP_EQUAL:
10558 case BINOP_NOTEQUAL:
10559 case BINOP_LESS:
10560 case BINOP_GTR:
10561 case BINOP_LEQ:
10562 case BINOP_GEQ:
10563 case BINOP_REPEAT:
10564 case BINOP_COMMA:
10565 case BINOP_EXP:
10566 case BINOP_MIN:
10567 case BINOP_MAX:
10568 case BINOP_INTDIV:
10569 case BINOP_CONCAT:
10570 case TERNOP_COND:
10571 case TERNOP_SLICE:
10572
10573 case OP_LONG:
10574 case OP_DOUBLE:
10575 case OP_DECFLOAT:
10576 case OP_LAST:
10577 case OP_COMPLEX:
10578 case OP_STRING:
10579 case OP_ARRAY:
10580 case OP_TYPE:
10581 case OP_TYPEOF:
10582 case OP_DECLTYPE:
10583 case OP_TYPEID:
10584 case OP_NAME:
10585 case OP_OBJC_NSSTRING:
10586
10587 case UNOP_NEG:
10588 case UNOP_LOGICAL_NOT:
10589 case UNOP_COMPLEMENT:
10590 case UNOP_ADDR:
10591 case UNOP_HIGH:
10592 case UNOP_CAST:
10593
10594 case UNOP_CAST_TYPE:
10595 case UNOP_REINTERPRET_CAST:
10596 case UNOP_DYNAMIC_CAST:
10597 /* Unary, binary and ternary operators: We have to check
10598 their operands. If they are constant, then so is the
10599 result of that operation. For instance, if A and B are
10600 determined to be constants, then so is "A + B".
10601
10602 UNOP_IND is one exception to the rule above, because the
10603 value of *ADDR is not necessarily a constant, even when
10604 ADDR is. */
10605 break;
10606
10607 case OP_VAR_VALUE:
10608 /* Check whether the associated symbol is a constant.
10609
10610 We use SYMBOL_CLASS rather than TYPE_CONST because it's
10611 possible that a buggy compiler could mark a variable as
10612 constant even when it is not, and TYPE_CONST would return
10613 true in this case, while SYMBOL_CLASS wouldn't.
10614
10615 We also have to check for function symbols because they
10616 are always constant. */
10617 {
10618 struct symbol *s = exp->elts[i + 2].symbol;
10619
10620 if (SYMBOL_CLASS (s) != LOC_BLOCK
10621 && SYMBOL_CLASS (s) != LOC_CONST
10622 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10623 return 0;
10624 break;
10625 }
10626
10627 /* The default action is to return 0 because we are using
10628 the optimistic approach here: If we don't know something,
10629 then it is not a constant. */
10630 default:
10631 return 0;
10632 }
10633 }
10634
10635 return 1;
10636 }
10637
10638 /* Implement the "dtor" breakpoint_ops method for watchpoints. */
10639
10640 static void
10641 dtor_watchpoint (struct breakpoint *self)
10642 {
10643 struct watchpoint *w = (struct watchpoint *) self;
10644
10645 xfree (w->exp_string);
10646 xfree (w->exp_string_reparse);
10647 value_free (w->val);
10648
10649 base_breakpoint_ops.dtor (self);
10650 }
10651
10652 /* Implement the "re_set" breakpoint_ops method for watchpoints. */
10653
10654 static void
10655 re_set_watchpoint (struct breakpoint *b)
10656 {
10657 struct watchpoint *w = (struct watchpoint *) b;
10658
10659 /* Watchpoint can be either on expression using entirely global
10660 variables, or it can be on local variables.
10661
10662 Watchpoints of the first kind are never auto-deleted, and even
10663 persist across program restarts. Since they can use variables
10664 from shared libraries, we need to reparse expression as libraries
10665 are loaded and unloaded.
10666
10667 Watchpoints on local variables can also change meaning as result
10668 of solib event. For example, if a watchpoint uses both a local
10669 and a global variables in expression, it's a local watchpoint,
10670 but unloading of a shared library will make the expression
10671 invalid. This is not a very common use case, but we still
10672 re-evaluate expression, to avoid surprises to the user.
10673
10674 Note that for local watchpoints, we re-evaluate it only if
10675 watchpoints frame id is still valid. If it's not, it means the
10676 watchpoint is out of scope and will be deleted soon. In fact,
10677 I'm not sure we'll ever be called in this case.
10678
10679 If a local watchpoint's frame id is still valid, then
10680 w->exp_valid_block is likewise valid, and we can safely use it.
10681
10682 Don't do anything about disabled watchpoints, since they will be
10683 reevaluated again when enabled. */
10684 update_watchpoint (w, 1 /* reparse */);
10685 }
10686
10687 /* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10688
10689 static int
10690 insert_watchpoint (struct bp_location *bl)
10691 {
10692 struct watchpoint *w = (struct watchpoint *) bl->owner;
10693 int length = w->exact ? 1 : bl->length;
10694
10695 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
10696 w->cond_exp.get ());
10697 }
10698
10699 /* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10700
10701 static int
10702 remove_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10703 {
10704 struct watchpoint *w = (struct watchpoint *) bl->owner;
10705 int length = w->exact ? 1 : bl->length;
10706
10707 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
10708 w->cond_exp.get ());
10709 }
10710
10711 static int
10712 breakpoint_hit_watchpoint (const struct bp_location *bl,
10713 struct address_space *aspace, CORE_ADDR bp_addr,
10714 const struct target_waitstatus *ws)
10715 {
10716 struct breakpoint *b = bl->owner;
10717 struct watchpoint *w = (struct watchpoint *) b;
10718
10719 /* Continuable hardware watchpoints are treated as non-existent if the
10720 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10721 some data address). Otherwise gdb won't stop on a break instruction
10722 in the code (not from a breakpoint) when a hardware watchpoint has
10723 been defined. Also skip watchpoints which we know did not trigger
10724 (did not match the data address). */
10725 if (is_hardware_watchpoint (b)
10726 && w->watchpoint_triggered == watch_triggered_no)
10727 return 0;
10728
10729 return 1;
10730 }
10731
10732 static void
10733 check_status_watchpoint (bpstat bs)
10734 {
10735 gdb_assert (is_watchpoint (bs->breakpoint_at));
10736
10737 bpstat_check_watchpoint (bs);
10738 }
10739
10740 /* Implement the "resources_needed" breakpoint_ops method for
10741 hardware watchpoints. */
10742
10743 static int
10744 resources_needed_watchpoint (const struct bp_location *bl)
10745 {
10746 struct watchpoint *w = (struct watchpoint *) bl->owner;
10747 int length = w->exact? 1 : bl->length;
10748
10749 return target_region_ok_for_hw_watchpoint (bl->address, length);
10750 }
10751
10752 /* Implement the "works_in_software_mode" breakpoint_ops method for
10753 hardware watchpoints. */
10754
10755 static int
10756 works_in_software_mode_watchpoint (const struct breakpoint *b)
10757 {
10758 /* Read and access watchpoints only work with hardware support. */
10759 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
10760 }
10761
10762 static enum print_stop_action
10763 print_it_watchpoint (bpstat bs)
10764 {
10765 struct cleanup *old_chain;
10766 struct breakpoint *b;
10767 struct ui_file *stb;
10768 enum print_stop_action result;
10769 struct watchpoint *w;
10770 struct ui_out *uiout = current_uiout;
10771
10772 gdb_assert (bs->bp_location_at != NULL);
10773
10774 b = bs->breakpoint_at;
10775 w = (struct watchpoint *) b;
10776
10777 stb = mem_fileopen ();
10778 old_chain = make_cleanup_ui_file_delete (stb);
10779
10780 annotate_watchpoint (b->number);
10781 maybe_print_thread_hit_breakpoint (uiout);
10782
10783 switch (b->type)
10784 {
10785 case bp_watchpoint:
10786 case bp_hardware_watchpoint:
10787 if (ui_out_is_mi_like_p (uiout))
10788 ui_out_field_string
10789 (uiout, "reason",
10790 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10791 mention (b);
10792 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10793 ui_out_text (uiout, "\nOld value = ");
10794 watchpoint_value_print (bs->old_val, stb);
10795 ui_out_field_stream (uiout, "old", stb);
10796 ui_out_text (uiout, "\nNew value = ");
10797 watchpoint_value_print (w->val, stb);
10798 ui_out_field_stream (uiout, "new", stb);
10799 ui_out_text (uiout, "\n");
10800 /* More than one watchpoint may have been triggered. */
10801 result = PRINT_UNKNOWN;
10802 break;
10803
10804 case bp_read_watchpoint:
10805 if (ui_out_is_mi_like_p (uiout))
10806 ui_out_field_string
10807 (uiout, "reason",
10808 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10809 mention (b);
10810 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10811 ui_out_text (uiout, "\nValue = ");
10812 watchpoint_value_print (w->val, stb);
10813 ui_out_field_stream (uiout, "value", stb);
10814 ui_out_text (uiout, "\n");
10815 result = PRINT_UNKNOWN;
10816 break;
10817
10818 case bp_access_watchpoint:
10819 if (bs->old_val != NULL)
10820 {
10821 if (ui_out_is_mi_like_p (uiout))
10822 ui_out_field_string
10823 (uiout, "reason",
10824 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10825 mention (b);
10826 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10827 ui_out_text (uiout, "\nOld value = ");
10828 watchpoint_value_print (bs->old_val, stb);
10829 ui_out_field_stream (uiout, "old", stb);
10830 ui_out_text (uiout, "\nNew value = ");
10831 }
10832 else
10833 {
10834 mention (b);
10835 if (ui_out_is_mi_like_p (uiout))
10836 ui_out_field_string
10837 (uiout, "reason",
10838 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10839 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10840 ui_out_text (uiout, "\nValue = ");
10841 }
10842 watchpoint_value_print (w->val, stb);
10843 ui_out_field_stream (uiout, "new", stb);
10844 ui_out_text (uiout, "\n");
10845 result = PRINT_UNKNOWN;
10846 break;
10847 default:
10848 result = PRINT_UNKNOWN;
10849 }
10850
10851 do_cleanups (old_chain);
10852 return result;
10853 }
10854
10855 /* Implement the "print_mention" breakpoint_ops method for hardware
10856 watchpoints. */
10857
10858 static void
10859 print_mention_watchpoint (struct breakpoint *b)
10860 {
10861 struct cleanup *ui_out_chain;
10862 struct watchpoint *w = (struct watchpoint *) b;
10863 struct ui_out *uiout = current_uiout;
10864
10865 switch (b->type)
10866 {
10867 case bp_watchpoint:
10868 ui_out_text (uiout, "Watchpoint ");
10869 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10870 break;
10871 case bp_hardware_watchpoint:
10872 ui_out_text (uiout, "Hardware watchpoint ");
10873 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10874 break;
10875 case bp_read_watchpoint:
10876 ui_out_text (uiout, "Hardware read watchpoint ");
10877 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
10878 break;
10879 case bp_access_watchpoint:
10880 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
10881 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
10882 break;
10883 default:
10884 internal_error (__FILE__, __LINE__,
10885 _("Invalid hardware watchpoint type."));
10886 }
10887
10888 ui_out_field_int (uiout, "number", b->number);
10889 ui_out_text (uiout, ": ");
10890 ui_out_field_string (uiout, "exp", w->exp_string);
10891 do_cleanups (ui_out_chain);
10892 }
10893
10894 /* Implement the "print_recreate" breakpoint_ops method for
10895 watchpoints. */
10896
10897 static void
10898 print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10899 {
10900 struct watchpoint *w = (struct watchpoint *) b;
10901
10902 switch (b->type)
10903 {
10904 case bp_watchpoint:
10905 case bp_hardware_watchpoint:
10906 fprintf_unfiltered (fp, "watch");
10907 break;
10908 case bp_read_watchpoint:
10909 fprintf_unfiltered (fp, "rwatch");
10910 break;
10911 case bp_access_watchpoint:
10912 fprintf_unfiltered (fp, "awatch");
10913 break;
10914 default:
10915 internal_error (__FILE__, __LINE__,
10916 _("Invalid watchpoint type."));
10917 }
10918
10919 fprintf_unfiltered (fp, " %s", w->exp_string);
10920 print_recreate_thread (b, fp);
10921 }
10922
10923 /* Implement the "explains_signal" breakpoint_ops method for
10924 watchpoints. */
10925
10926 static int
10927 explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10928 {
10929 /* A software watchpoint cannot cause a signal other than
10930 GDB_SIGNAL_TRAP. */
10931 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
10932 return 0;
10933
10934 return 1;
10935 }
10936
10937 /* The breakpoint_ops structure to be used in hardware watchpoints. */
10938
10939 static struct breakpoint_ops watchpoint_breakpoint_ops;
10940
10941 /* Implement the "insert" breakpoint_ops method for
10942 masked hardware watchpoints. */
10943
10944 static int
10945 insert_masked_watchpoint (struct bp_location *bl)
10946 {
10947 struct watchpoint *w = (struct watchpoint *) bl->owner;
10948
10949 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
10950 bl->watchpoint_type);
10951 }
10952
10953 /* Implement the "remove" breakpoint_ops method for
10954 masked hardware watchpoints. */
10955
10956 static int
10957 remove_masked_watchpoint (struct bp_location *bl, enum remove_bp_reason reason)
10958 {
10959 struct watchpoint *w = (struct watchpoint *) bl->owner;
10960
10961 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
10962 bl->watchpoint_type);
10963 }
10964
10965 /* Implement the "resources_needed" breakpoint_ops method for
10966 masked hardware watchpoints. */
10967
10968 static int
10969 resources_needed_masked_watchpoint (const struct bp_location *bl)
10970 {
10971 struct watchpoint *w = (struct watchpoint *) bl->owner;
10972
10973 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
10974 }
10975
10976 /* Implement the "works_in_software_mode" breakpoint_ops method for
10977 masked hardware watchpoints. */
10978
10979 static int
10980 works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10981 {
10982 return 0;
10983 }
10984
10985 /* Implement the "print_it" breakpoint_ops method for
10986 masked hardware watchpoints. */
10987
10988 static enum print_stop_action
10989 print_it_masked_watchpoint (bpstat bs)
10990 {
10991 struct breakpoint *b = bs->breakpoint_at;
10992 struct ui_out *uiout = current_uiout;
10993
10994 /* Masked watchpoints have only one location. */
10995 gdb_assert (b->loc && b->loc->next == NULL);
10996
10997 annotate_watchpoint (b->number);
10998 maybe_print_thread_hit_breakpoint (uiout);
10999
11000 switch (b->type)
11001 {
11002 case bp_hardware_watchpoint:
11003 if (ui_out_is_mi_like_p (uiout))
11004 ui_out_field_string
11005 (uiout, "reason",
11006 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
11007 break;
11008
11009 case bp_read_watchpoint:
11010 if (ui_out_is_mi_like_p (uiout))
11011 ui_out_field_string
11012 (uiout, "reason",
11013 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
11014 break;
11015
11016 case bp_access_watchpoint:
11017 if (ui_out_is_mi_like_p (uiout))
11018 ui_out_field_string
11019 (uiout, "reason",
11020 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
11021 break;
11022 default:
11023 internal_error (__FILE__, __LINE__,
11024 _("Invalid hardware watchpoint type."));
11025 }
11026
11027 mention (b);
11028 ui_out_text (uiout, _("\n\
11029 Check the underlying instruction at PC for the memory\n\
11030 address and value which triggered this watchpoint.\n"));
11031 ui_out_text (uiout, "\n");
11032
11033 /* More than one watchpoint may have been triggered. */
11034 return PRINT_UNKNOWN;
11035 }
11036
11037 /* Implement the "print_one_detail" breakpoint_ops method for
11038 masked hardware watchpoints. */
11039
11040 static void
11041 print_one_detail_masked_watchpoint (const struct breakpoint *b,
11042 struct ui_out *uiout)
11043 {
11044 struct watchpoint *w = (struct watchpoint *) b;
11045
11046 /* Masked watchpoints have only one location. */
11047 gdb_assert (b->loc && b->loc->next == NULL);
11048
11049 ui_out_text (uiout, "\tmask ");
11050 ui_out_field_core_addr (uiout, "mask", b->loc->gdbarch, w->hw_wp_mask);
11051 ui_out_text (uiout, "\n");
11052 }
11053
11054 /* Implement the "print_mention" breakpoint_ops method for
11055 masked hardware watchpoints. */
11056
11057 static void
11058 print_mention_masked_watchpoint (struct breakpoint *b)
11059 {
11060 struct watchpoint *w = (struct watchpoint *) b;
11061 struct ui_out *uiout = current_uiout;
11062 struct cleanup *ui_out_chain;
11063
11064 switch (b->type)
11065 {
11066 case bp_hardware_watchpoint:
11067 ui_out_text (uiout, "Masked hardware watchpoint ");
11068 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
11069 break;
11070 case bp_read_watchpoint:
11071 ui_out_text (uiout, "Masked hardware read watchpoint ");
11072 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
11073 break;
11074 case bp_access_watchpoint:
11075 ui_out_text (uiout, "Masked hardware access (read/write) watchpoint ");
11076 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
11077 break;
11078 default:
11079 internal_error (__FILE__, __LINE__,
11080 _("Invalid hardware watchpoint type."));
11081 }
11082
11083 ui_out_field_int (uiout, "number", b->number);
11084 ui_out_text (uiout, ": ");
11085 ui_out_field_string (uiout, "exp", w->exp_string);
11086 do_cleanups (ui_out_chain);
11087 }
11088
11089 /* Implement the "print_recreate" breakpoint_ops method for
11090 masked hardware watchpoints. */
11091
11092 static void
11093 print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
11094 {
11095 struct watchpoint *w = (struct watchpoint *) b;
11096 char tmp[40];
11097
11098 switch (b->type)
11099 {
11100 case bp_hardware_watchpoint:
11101 fprintf_unfiltered (fp, "watch");
11102 break;
11103 case bp_read_watchpoint:
11104 fprintf_unfiltered (fp, "rwatch");
11105 break;
11106 case bp_access_watchpoint:
11107 fprintf_unfiltered (fp, "awatch");
11108 break;
11109 default:
11110 internal_error (__FILE__, __LINE__,
11111 _("Invalid hardware watchpoint type."));
11112 }
11113
11114 sprintf_vma (tmp, w->hw_wp_mask);
11115 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
11116 print_recreate_thread (b, fp);
11117 }
11118
11119 /* The breakpoint_ops structure to be used in masked hardware watchpoints. */
11120
11121 static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
11122
11123 /* Tell whether the given watchpoint is a masked hardware watchpoint. */
11124
11125 static int
11126 is_masked_watchpoint (const struct breakpoint *b)
11127 {
11128 return b->ops == &masked_watchpoint_breakpoint_ops;
11129 }
11130
11131 /* accessflag: hw_write: watch write,
11132 hw_read: watch read,
11133 hw_access: watch access (read or write) */
11134 static void
11135 watch_command_1 (const char *arg, int accessflag, int from_tty,
11136 int just_location, int internal)
11137 {
11138 struct breakpoint *b, *scope_breakpoint = NULL;
11139 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
11140 struct value *val, *mark, *result;
11141 int saved_bitpos = 0, saved_bitsize = 0;
11142 struct frame_info *frame;
11143 const char *exp_start = NULL;
11144 const char *exp_end = NULL;
11145 const char *tok, *end_tok;
11146 int toklen = -1;
11147 const char *cond_start = NULL;
11148 const char *cond_end = NULL;
11149 enum bptype bp_type;
11150 int thread = -1;
11151 int pc = 0;
11152 /* Flag to indicate whether we are going to use masks for
11153 the hardware watchpoint. */
11154 int use_mask = 0;
11155 CORE_ADDR mask = 0;
11156 struct watchpoint *w;
11157 char *expression;
11158 struct cleanup *back_to;
11159
11160 /* Make sure that we actually have parameters to parse. */
11161 if (arg != NULL && arg[0] != '\0')
11162 {
11163 const char *value_start;
11164
11165 exp_end = arg + strlen (arg);
11166
11167 /* Look for "parameter value" pairs at the end
11168 of the arguments string. */
11169 for (tok = exp_end - 1; tok > arg; tok--)
11170 {
11171 /* Skip whitespace at the end of the argument list. */
11172 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11173 tok--;
11174
11175 /* Find the beginning of the last token.
11176 This is the value of the parameter. */
11177 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11178 tok--;
11179 value_start = tok + 1;
11180
11181 /* Skip whitespace. */
11182 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11183 tok--;
11184
11185 end_tok = tok;
11186
11187 /* Find the beginning of the second to last token.
11188 This is the parameter itself. */
11189 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11190 tok--;
11191 tok++;
11192 toklen = end_tok - tok + 1;
11193
11194 if (toklen == 6 && startswith (tok, "thread"))
11195 {
11196 struct thread_info *thr;
11197 /* At this point we've found a "thread" token, which means
11198 the user is trying to set a watchpoint that triggers
11199 only in a specific thread. */
11200 const char *endp;
11201
11202 if (thread != -1)
11203 error(_("You can specify only one thread."));
11204
11205 /* Extract the thread ID from the next token. */
11206 thr = parse_thread_id (value_start, &endp);
11207
11208 /* Check if the user provided a valid thread ID. */
11209 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
11210 invalid_thread_id_error (value_start);
11211
11212 thread = thr->global_num;
11213 }
11214 else if (toklen == 4 && startswith (tok, "mask"))
11215 {
11216 /* We've found a "mask" token, which means the user wants to
11217 create a hardware watchpoint that is going to have the mask
11218 facility. */
11219 struct value *mask_value, *mark;
11220
11221 if (use_mask)
11222 error(_("You can specify only one mask."));
11223
11224 use_mask = just_location = 1;
11225
11226 mark = value_mark ();
11227 mask_value = parse_to_comma_and_eval (&value_start);
11228 mask = value_as_address (mask_value);
11229 value_free_to_mark (mark);
11230 }
11231 else
11232 /* We didn't recognize what we found. We should stop here. */
11233 break;
11234
11235 /* Truncate the string and get rid of the "parameter value" pair before
11236 the arguments string is parsed by the parse_exp_1 function. */
11237 exp_end = tok;
11238 }
11239 }
11240 else
11241 exp_end = arg;
11242
11243 /* Parse the rest of the arguments. From here on out, everything
11244 is in terms of a newly allocated string instead of the original
11245 ARG. */
11246 innermost_block = NULL;
11247 expression = savestring (arg, exp_end - arg);
11248 back_to = make_cleanup (xfree, expression);
11249 exp_start = arg = expression;
11250 expression_up exp = parse_exp_1 (&arg, 0, 0, 0);
11251 exp_end = arg;
11252 /* Remove trailing whitespace from the expression before saving it.
11253 This makes the eventual display of the expression string a bit
11254 prettier. */
11255 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
11256 --exp_end;
11257
11258 /* Checking if the expression is not constant. */
11259 if (watchpoint_exp_is_const (exp.get ()))
11260 {
11261 int len;
11262
11263 len = exp_end - exp_start;
11264 while (len > 0 && isspace (exp_start[len - 1]))
11265 len--;
11266 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
11267 }
11268
11269 exp_valid_block = innermost_block;
11270 mark = value_mark ();
11271 fetch_subexp_value (exp.get (), &pc, &val, &result, NULL, just_location);
11272
11273 if (val != NULL && just_location)
11274 {
11275 saved_bitpos = value_bitpos (val);
11276 saved_bitsize = value_bitsize (val);
11277 }
11278
11279 if (just_location)
11280 {
11281 int ret;
11282
11283 exp_valid_block = NULL;
11284 val = value_addr (result);
11285 release_value (val);
11286 value_free_to_mark (mark);
11287
11288 if (use_mask)
11289 {
11290 ret = target_masked_watch_num_registers (value_as_address (val),
11291 mask);
11292 if (ret == -1)
11293 error (_("This target does not support masked watchpoints."));
11294 else if (ret == -2)
11295 error (_("Invalid mask or memory region."));
11296 }
11297 }
11298 else if (val != NULL)
11299 release_value (val);
11300
11301 tok = skip_spaces_const (arg);
11302 end_tok = skip_to_space_const (tok);
11303
11304 toklen = end_tok - tok;
11305 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
11306 {
11307 innermost_block = NULL;
11308 tok = cond_start = end_tok + 1;
11309 parse_exp_1 (&tok, 0, 0, 0);
11310
11311 /* The watchpoint expression may not be local, but the condition
11312 may still be. E.g.: `watch global if local > 0'. */
11313 cond_exp_valid_block = innermost_block;
11314
11315 cond_end = tok;
11316 }
11317 if (*tok)
11318 error (_("Junk at end of command."));
11319
11320 frame = block_innermost_frame (exp_valid_block);
11321
11322 /* If the expression is "local", then set up a "watchpoint scope"
11323 breakpoint at the point where we've left the scope of the watchpoint
11324 expression. Create the scope breakpoint before the watchpoint, so
11325 that we will encounter it first in bpstat_stop_status. */
11326 if (exp_valid_block && frame)
11327 {
11328 if (frame_id_p (frame_unwind_caller_id (frame)))
11329 {
11330 scope_breakpoint
11331 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
11332 frame_unwind_caller_pc (frame),
11333 bp_watchpoint_scope,
11334 &momentary_breakpoint_ops);
11335
11336 scope_breakpoint->enable_state = bp_enabled;
11337
11338 /* Automatically delete the breakpoint when it hits. */
11339 scope_breakpoint->disposition = disp_del;
11340
11341 /* Only break in the proper frame (help with recursion). */
11342 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
11343
11344 /* Set the address at which we will stop. */
11345 scope_breakpoint->loc->gdbarch
11346 = frame_unwind_caller_arch (frame);
11347 scope_breakpoint->loc->requested_address
11348 = frame_unwind_caller_pc (frame);
11349 scope_breakpoint->loc->address
11350 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
11351 scope_breakpoint->loc->requested_address,
11352 scope_breakpoint->type);
11353 }
11354 }
11355
11356 /* Now set up the breakpoint. We create all watchpoints as hardware
11357 watchpoints here even if hardware watchpoints are turned off, a call
11358 to update_watchpoint later in this function will cause the type to
11359 drop back to bp_watchpoint (software watchpoint) if required. */
11360
11361 if (accessflag == hw_read)
11362 bp_type = bp_read_watchpoint;
11363 else if (accessflag == hw_access)
11364 bp_type = bp_access_watchpoint;
11365 else
11366 bp_type = bp_hardware_watchpoint;
11367
11368 w = new watchpoint ();
11369 b = &w->base;
11370 if (use_mask)
11371 init_raw_breakpoint_without_location (b, NULL, bp_type,
11372 &masked_watchpoint_breakpoint_ops);
11373 else
11374 init_raw_breakpoint_without_location (b, NULL, bp_type,
11375 &watchpoint_breakpoint_ops);
11376 b->thread = thread;
11377 b->disposition = disp_donttouch;
11378 b->pspace = current_program_space;
11379 w->exp = gdb::move (exp);
11380 w->exp_valid_block = exp_valid_block;
11381 w->cond_exp_valid_block = cond_exp_valid_block;
11382 if (just_location)
11383 {
11384 struct type *t = value_type (val);
11385 CORE_ADDR addr = value_as_address (val);
11386
11387 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
11388
11389 std::string name = type_to_string (t);
11390
11391 w->exp_string_reparse = xstrprintf ("* (%s *) %s", name.c_str (),
11392 core_addr_to_string (addr));
11393
11394 w->exp_string = xstrprintf ("-location %.*s",
11395 (int) (exp_end - exp_start), exp_start);
11396
11397 /* The above expression is in C. */
11398 b->language = language_c;
11399 }
11400 else
11401 w->exp_string = savestring (exp_start, exp_end - exp_start);
11402
11403 if (use_mask)
11404 {
11405 w->hw_wp_mask = mask;
11406 }
11407 else
11408 {
11409 w->val = val;
11410 w->val_bitpos = saved_bitpos;
11411 w->val_bitsize = saved_bitsize;
11412 w->val_valid = 1;
11413 }
11414
11415 if (cond_start)
11416 b->cond_string = savestring (cond_start, cond_end - cond_start);
11417 else
11418 b->cond_string = 0;
11419
11420 if (frame)
11421 {
11422 w->watchpoint_frame = get_frame_id (frame);
11423 w->watchpoint_thread = inferior_ptid;
11424 }
11425 else
11426 {
11427 w->watchpoint_frame = null_frame_id;
11428 w->watchpoint_thread = null_ptid;
11429 }
11430
11431 if (scope_breakpoint != NULL)
11432 {
11433 /* The scope breakpoint is related to the watchpoint. We will
11434 need to act on them together. */
11435 b->related_breakpoint = scope_breakpoint;
11436 scope_breakpoint->related_breakpoint = b;
11437 }
11438
11439 if (!just_location)
11440 value_free_to_mark (mark);
11441
11442 TRY
11443 {
11444 /* Finally update the new watchpoint. This creates the locations
11445 that should be inserted. */
11446 update_watchpoint (w, 1);
11447 }
11448 CATCH (e, RETURN_MASK_ALL)
11449 {
11450 delete_breakpoint (b);
11451 throw_exception (e);
11452 }
11453 END_CATCH
11454
11455 install_breakpoint (internal, b, 1);
11456 do_cleanups (back_to);
11457 }
11458
11459 /* Return count of debug registers needed to watch the given expression.
11460 If the watchpoint cannot be handled in hardware return zero. */
11461
11462 static int
11463 can_use_hardware_watchpoint (struct value *v)
11464 {
11465 int found_memory_cnt = 0;
11466 struct value *head = v;
11467
11468 /* Did the user specifically forbid us to use hardware watchpoints? */
11469 if (!can_use_hw_watchpoints)
11470 return 0;
11471
11472 /* Make sure that the value of the expression depends only upon
11473 memory contents, and values computed from them within GDB. If we
11474 find any register references or function calls, we can't use a
11475 hardware watchpoint.
11476
11477 The idea here is that evaluating an expression generates a series
11478 of values, one holding the value of every subexpression. (The
11479 expression a*b+c has five subexpressions: a, b, a*b, c, and
11480 a*b+c.) GDB's values hold almost enough information to establish
11481 the criteria given above --- they identify memory lvalues,
11482 register lvalues, computed values, etcetera. So we can evaluate
11483 the expression, and then scan the chain of values that leaves
11484 behind to decide whether we can detect any possible change to the
11485 expression's final value using only hardware watchpoints.
11486
11487 However, I don't think that the values returned by inferior
11488 function calls are special in any way. So this function may not
11489 notice that an expression involving an inferior function call
11490 can't be watched with hardware watchpoints. FIXME. */
11491 for (; v; v = value_next (v))
11492 {
11493 if (VALUE_LVAL (v) == lval_memory)
11494 {
11495 if (v != head && value_lazy (v))
11496 /* A lazy memory lvalue in the chain is one that GDB never
11497 needed to fetch; we either just used its address (e.g.,
11498 `a' in `a.b') or we never needed it at all (e.g., `a'
11499 in `a,b'). This doesn't apply to HEAD; if that is
11500 lazy then it was not readable, but watch it anyway. */
11501 ;
11502 else
11503 {
11504 /* Ahh, memory we actually used! Check if we can cover
11505 it with hardware watchpoints. */
11506 struct type *vtype = check_typedef (value_type (v));
11507
11508 /* We only watch structs and arrays if user asked for it
11509 explicitly, never if they just happen to appear in a
11510 middle of some value chain. */
11511 if (v == head
11512 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11513 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11514 {
11515 CORE_ADDR vaddr = value_address (v);
11516 int len;
11517 int num_regs;
11518
11519 len = (target_exact_watchpoints
11520 && is_scalar_type_recursive (vtype))?
11521 1 : TYPE_LENGTH (value_type (v));
11522
11523 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11524 if (!num_regs)
11525 return 0;
11526 else
11527 found_memory_cnt += num_regs;
11528 }
11529 }
11530 }
11531 else if (VALUE_LVAL (v) != not_lval
11532 && deprecated_value_modifiable (v) == 0)
11533 return 0; /* These are values from the history (e.g., $1). */
11534 else if (VALUE_LVAL (v) == lval_register)
11535 return 0; /* Cannot watch a register with a HW watchpoint. */
11536 }
11537
11538 /* The expression itself looks suitable for using a hardware
11539 watchpoint, but give the target machine a chance to reject it. */
11540 return found_memory_cnt;
11541 }
11542
11543 void
11544 watch_command_wrapper (char *arg, int from_tty, int internal)
11545 {
11546 watch_command_1 (arg, hw_write, from_tty, 0, internal);
11547 }
11548
11549 /* A helper function that looks for the "-location" argument and then
11550 calls watch_command_1. */
11551
11552 static void
11553 watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11554 {
11555 int just_location = 0;
11556
11557 if (arg
11558 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11559 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11560 {
11561 arg = skip_spaces (arg);
11562 just_location = 1;
11563 }
11564
11565 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
11566 }
11567
11568 static void
11569 watch_command (char *arg, int from_tty)
11570 {
11571 watch_maybe_just_location (arg, hw_write, from_tty);
11572 }
11573
11574 void
11575 rwatch_command_wrapper (char *arg, int from_tty, int internal)
11576 {
11577 watch_command_1 (arg, hw_read, from_tty, 0, internal);
11578 }
11579
11580 static void
11581 rwatch_command (char *arg, int from_tty)
11582 {
11583 watch_maybe_just_location (arg, hw_read, from_tty);
11584 }
11585
11586 void
11587 awatch_command_wrapper (char *arg, int from_tty, int internal)
11588 {
11589 watch_command_1 (arg, hw_access, from_tty, 0, internal);
11590 }
11591
11592 static void
11593 awatch_command (char *arg, int from_tty)
11594 {
11595 watch_maybe_just_location (arg, hw_access, from_tty);
11596 }
11597 \f
11598
11599 /* Data for the FSM that manages the until(location)/advance commands
11600 in infcmd.c. Here because it uses the mechanisms of
11601 breakpoints. */
11602
11603 struct until_break_fsm
11604 {
11605 /* The base class. */
11606 struct thread_fsm thread_fsm;
11607
11608 /* The thread that as current when the command was executed. */
11609 int thread;
11610
11611 /* The breakpoint set at the destination location. */
11612 struct breakpoint *location_breakpoint;
11613
11614 /* Breakpoint set at the return address in the caller frame. May be
11615 NULL. */
11616 struct breakpoint *caller_breakpoint;
11617 };
11618
11619 static void until_break_fsm_clean_up (struct thread_fsm *self,
11620 struct thread_info *thread);
11621 static int until_break_fsm_should_stop (struct thread_fsm *self,
11622 struct thread_info *thread);
11623 static enum async_reply_reason
11624 until_break_fsm_async_reply_reason (struct thread_fsm *self);
11625
11626 /* until_break_fsm's vtable. */
11627
11628 static struct thread_fsm_ops until_break_fsm_ops =
11629 {
11630 NULL, /* dtor */
11631 until_break_fsm_clean_up,
11632 until_break_fsm_should_stop,
11633 NULL, /* return_value */
11634 until_break_fsm_async_reply_reason,
11635 };
11636
11637 /* Allocate a new until_break_command_fsm. */
11638
11639 static struct until_break_fsm *
11640 new_until_break_fsm (struct interp *cmd_interp, int thread,
11641 struct breakpoint *location_breakpoint,
11642 struct breakpoint *caller_breakpoint)
11643 {
11644 struct until_break_fsm *sm;
11645
11646 sm = XCNEW (struct until_break_fsm);
11647 thread_fsm_ctor (&sm->thread_fsm, &until_break_fsm_ops, cmd_interp);
11648
11649 sm->thread = thread;
11650 sm->location_breakpoint = location_breakpoint;
11651 sm->caller_breakpoint = caller_breakpoint;
11652
11653 return sm;
11654 }
11655
11656 /* Implementation of the 'should_stop' FSM method for the
11657 until(location)/advance commands. */
11658
11659 static int
11660 until_break_fsm_should_stop (struct thread_fsm *self,
11661 struct thread_info *tp)
11662 {
11663 struct until_break_fsm *sm = (struct until_break_fsm *) self;
11664
11665 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
11666 sm->location_breakpoint) != NULL
11667 || (sm->caller_breakpoint != NULL
11668 && bpstat_find_breakpoint (tp->control.stop_bpstat,
11669 sm->caller_breakpoint) != NULL))
11670 thread_fsm_set_finished (self);
11671
11672 return 1;
11673 }
11674
11675 /* Implementation of the 'clean_up' FSM method for the
11676 until(location)/advance commands. */
11677
11678 static void
11679 until_break_fsm_clean_up (struct thread_fsm *self,
11680 struct thread_info *thread)
11681 {
11682 struct until_break_fsm *sm = (struct until_break_fsm *) self;
11683
11684 /* Clean up our temporary breakpoints. */
11685 if (sm->location_breakpoint != NULL)
11686 {
11687 delete_breakpoint (sm->location_breakpoint);
11688 sm->location_breakpoint = NULL;
11689 }
11690 if (sm->caller_breakpoint != NULL)
11691 {
11692 delete_breakpoint (sm->caller_breakpoint);
11693 sm->caller_breakpoint = NULL;
11694 }
11695 delete_longjmp_breakpoint (sm->thread);
11696 }
11697
11698 /* Implementation of the 'async_reply_reason' FSM method for the
11699 until(location)/advance commands. */
11700
11701 static enum async_reply_reason
11702 until_break_fsm_async_reply_reason (struct thread_fsm *self)
11703 {
11704 return EXEC_ASYNC_LOCATION_REACHED;
11705 }
11706
11707 void
11708 until_break_command (char *arg, int from_tty, int anywhere)
11709 {
11710 struct symtabs_and_lines sals;
11711 struct symtab_and_line sal;
11712 struct frame_info *frame;
11713 struct gdbarch *frame_gdbarch;
11714 struct frame_id stack_frame_id;
11715 struct frame_id caller_frame_id;
11716 struct breakpoint *location_breakpoint;
11717 struct breakpoint *caller_breakpoint = NULL;
11718 struct cleanup *old_chain, *cleanup;
11719 int thread;
11720 struct thread_info *tp;
11721 struct event_location *location;
11722 struct until_break_fsm *sm;
11723
11724 clear_proceed_status (0);
11725
11726 /* Set a breakpoint where the user wants it and at return from
11727 this function. */
11728
11729 location = string_to_event_location (&arg, current_language);
11730 cleanup = make_cleanup_delete_event_location (location);
11731
11732 if (last_displayed_sal_is_valid ())
11733 sals = decode_line_1 (location, DECODE_LINE_FUNFIRSTLINE, NULL,
11734 get_last_displayed_symtab (),
11735 get_last_displayed_line ());
11736 else
11737 sals = decode_line_1 (location, DECODE_LINE_FUNFIRSTLINE,
11738 NULL, (struct symtab *) NULL, 0);
11739
11740 if (sals.nelts != 1)
11741 error (_("Couldn't get information on specified line."));
11742
11743 sal = sals.sals[0];
11744 xfree (sals.sals); /* malloc'd, so freed. */
11745
11746 if (*arg)
11747 error (_("Junk at end of arguments."));
11748
11749 resolve_sal_pc (&sal);
11750
11751 tp = inferior_thread ();
11752 thread = tp->global_num;
11753
11754 old_chain = make_cleanup (null_cleanup, NULL);
11755
11756 /* Note linespec handling above invalidates the frame chain.
11757 Installing a breakpoint also invalidates the frame chain (as it
11758 may need to switch threads), so do any frame handling before
11759 that. */
11760
11761 frame = get_selected_frame (NULL);
11762 frame_gdbarch = get_frame_arch (frame);
11763 stack_frame_id = get_stack_frame_id (frame);
11764 caller_frame_id = frame_unwind_caller_id (frame);
11765
11766 /* Keep within the current frame, or in frames called by the current
11767 one. */
11768
11769 if (frame_id_p (caller_frame_id))
11770 {
11771 struct symtab_and_line sal2;
11772 struct gdbarch *caller_gdbarch;
11773
11774 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11775 sal2.pc = frame_unwind_caller_pc (frame);
11776 caller_gdbarch = frame_unwind_caller_arch (frame);
11777 caller_breakpoint = set_momentary_breakpoint (caller_gdbarch,
11778 sal2,
11779 caller_frame_id,
11780 bp_until);
11781 make_cleanup_delete_breakpoint (caller_breakpoint);
11782
11783 set_longjmp_breakpoint (tp, caller_frame_id);
11784 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
11785 }
11786
11787 /* set_momentary_breakpoint could invalidate FRAME. */
11788 frame = NULL;
11789
11790 if (anywhere)
11791 /* If the user told us to continue until a specified location,
11792 we don't specify a frame at which we need to stop. */
11793 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11794 null_frame_id, bp_until);
11795 else
11796 /* Otherwise, specify the selected frame, because we want to stop
11797 only at the very same frame. */
11798 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11799 stack_frame_id, bp_until);
11800 make_cleanup_delete_breakpoint (location_breakpoint);
11801
11802 sm = new_until_break_fsm (command_interp (), tp->global_num,
11803 location_breakpoint, caller_breakpoint);
11804 tp->thread_fsm = &sm->thread_fsm;
11805
11806 discard_cleanups (old_chain);
11807
11808 proceed (-1, GDB_SIGNAL_DEFAULT);
11809
11810 do_cleanups (cleanup);
11811 }
11812
11813 /* This function attempts to parse an optional "if <cond>" clause
11814 from the arg string. If one is not found, it returns NULL.
11815
11816 Else, it returns a pointer to the condition string. (It does not
11817 attempt to evaluate the string against a particular block.) And,
11818 it updates arg to point to the first character following the parsed
11819 if clause in the arg string. */
11820
11821 char *
11822 ep_parse_optional_if_clause (char **arg)
11823 {
11824 char *cond_string;
11825
11826 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
11827 return NULL;
11828
11829 /* Skip the "if" keyword. */
11830 (*arg) += 2;
11831
11832 /* Skip any extra leading whitespace, and record the start of the
11833 condition string. */
11834 *arg = skip_spaces (*arg);
11835 cond_string = *arg;
11836
11837 /* Assume that the condition occupies the remainder of the arg
11838 string. */
11839 (*arg) += strlen (cond_string);
11840
11841 return cond_string;
11842 }
11843
11844 /* Commands to deal with catching events, such as signals, exceptions,
11845 process start/exit, etc. */
11846
11847 typedef enum
11848 {
11849 catch_fork_temporary, catch_vfork_temporary,
11850 catch_fork_permanent, catch_vfork_permanent
11851 }
11852 catch_fork_kind;
11853
11854 static void
11855 catch_fork_command_1 (char *arg, int from_tty,
11856 struct cmd_list_element *command)
11857 {
11858 struct gdbarch *gdbarch = get_current_arch ();
11859 char *cond_string = NULL;
11860 catch_fork_kind fork_kind;
11861 int tempflag;
11862
11863 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11864 tempflag = (fork_kind == catch_fork_temporary
11865 || fork_kind == catch_vfork_temporary);
11866
11867 if (!arg)
11868 arg = "";
11869 arg = skip_spaces (arg);
11870
11871 /* The allowed syntax is:
11872 catch [v]fork
11873 catch [v]fork if <cond>
11874
11875 First, check if there's an if clause. */
11876 cond_string = ep_parse_optional_if_clause (&arg);
11877
11878 if ((*arg != '\0') && !isspace (*arg))
11879 error (_("Junk at end of arguments."));
11880
11881 /* If this target supports it, create a fork or vfork catchpoint
11882 and enable reporting of such events. */
11883 switch (fork_kind)
11884 {
11885 case catch_fork_temporary:
11886 case catch_fork_permanent:
11887 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11888 &catch_fork_breakpoint_ops);
11889 break;
11890 case catch_vfork_temporary:
11891 case catch_vfork_permanent:
11892 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11893 &catch_vfork_breakpoint_ops);
11894 break;
11895 default:
11896 error (_("unsupported or unknown fork kind; cannot catch it"));
11897 break;
11898 }
11899 }
11900
11901 static void
11902 catch_exec_command_1 (char *arg, int from_tty,
11903 struct cmd_list_element *command)
11904 {
11905 struct exec_catchpoint *c;
11906 struct gdbarch *gdbarch = get_current_arch ();
11907 int tempflag;
11908 char *cond_string = NULL;
11909
11910 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11911
11912 if (!arg)
11913 arg = "";
11914 arg = skip_spaces (arg);
11915
11916 /* The allowed syntax is:
11917 catch exec
11918 catch exec if <cond>
11919
11920 First, check if there's an if clause. */
11921 cond_string = ep_parse_optional_if_clause (&arg);
11922
11923 if ((*arg != '\0') && !isspace (*arg))
11924 error (_("Junk at end of arguments."));
11925
11926 c = new exec_catchpoint ();
11927 init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
11928 &catch_exec_breakpoint_ops);
11929 c->exec_pathname = NULL;
11930
11931 install_breakpoint (0, &c->base, 1);
11932 }
11933
11934 void
11935 init_ada_exception_breakpoint (struct breakpoint *b,
11936 struct gdbarch *gdbarch,
11937 struct symtab_and_line sal,
11938 char *addr_string,
11939 const struct breakpoint_ops *ops,
11940 int tempflag,
11941 int enabled,
11942 int from_tty)
11943 {
11944 if (from_tty)
11945 {
11946 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11947 if (!loc_gdbarch)
11948 loc_gdbarch = gdbarch;
11949
11950 describe_other_breakpoints (loc_gdbarch,
11951 sal.pspace, sal.pc, sal.section, -1);
11952 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11953 version for exception catchpoints, because two catchpoints
11954 used for different exception names will use the same address.
11955 In this case, a "breakpoint ... also set at..." warning is
11956 unproductive. Besides, the warning phrasing is also a bit
11957 inappropriate, we should use the word catchpoint, and tell
11958 the user what type of catchpoint it is. The above is good
11959 enough for now, though. */
11960 }
11961
11962 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
11963
11964 b->enable_state = enabled ? bp_enabled : bp_disabled;
11965 b->disposition = tempflag ? disp_del : disp_donttouch;
11966 b->location = string_to_event_location (&addr_string,
11967 language_def (language_ada));
11968 b->language = language_ada;
11969 }
11970
11971 static void
11972 catch_command (char *arg, int from_tty)
11973 {
11974 error (_("Catch requires an event name."));
11975 }
11976 \f
11977
11978 static void
11979 tcatch_command (char *arg, int from_tty)
11980 {
11981 error (_("Catch requires an event name."));
11982 }
11983
11984 /* A qsort comparison function that sorts breakpoints in order. */
11985
11986 static int
11987 compare_breakpoints (const void *a, const void *b)
11988 {
11989 const breakpoint_p *ba = (const breakpoint_p *) a;
11990 uintptr_t ua = (uintptr_t) *ba;
11991 const breakpoint_p *bb = (const breakpoint_p *) b;
11992 uintptr_t ub = (uintptr_t) *bb;
11993
11994 if ((*ba)->number < (*bb)->number)
11995 return -1;
11996 else if ((*ba)->number > (*bb)->number)
11997 return 1;
11998
11999 /* Now sort by address, in case we see, e..g, two breakpoints with
12000 the number 0. */
12001 if (ua < ub)
12002 return -1;
12003 return ua > ub ? 1 : 0;
12004 }
12005
12006 /* Delete breakpoints by address or line. */
12007
12008 static void
12009 clear_command (char *arg, int from_tty)
12010 {
12011 struct breakpoint *b, *prev;
12012 VEC(breakpoint_p) *found = 0;
12013 int ix;
12014 int default_match;
12015 struct symtabs_and_lines sals;
12016 struct symtab_and_line sal;
12017 int i;
12018 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
12019
12020 if (arg)
12021 {
12022 sals = decode_line_with_current_source (arg,
12023 (DECODE_LINE_FUNFIRSTLINE
12024 | DECODE_LINE_LIST_MODE));
12025 make_cleanup (xfree, sals.sals);
12026 default_match = 0;
12027 }
12028 else
12029 {
12030 sals.sals = XNEW (struct symtab_and_line);
12031 make_cleanup (xfree, sals.sals);
12032 init_sal (&sal); /* Initialize to zeroes. */
12033
12034 /* Set sal's line, symtab, pc, and pspace to the values
12035 corresponding to the last call to print_frame_info. If the
12036 codepoint is not valid, this will set all the fields to 0. */
12037 get_last_displayed_sal (&sal);
12038 if (sal.symtab == 0)
12039 error (_("No source file specified."));
12040
12041 sals.sals[0] = sal;
12042 sals.nelts = 1;
12043
12044 default_match = 1;
12045 }
12046
12047 /* We don't call resolve_sal_pc here. That's not as bad as it
12048 seems, because all existing breakpoints typically have both
12049 file/line and pc set. So, if clear is given file/line, we can
12050 match this to existing breakpoint without obtaining pc at all.
12051
12052 We only support clearing given the address explicitly
12053 present in breakpoint table. Say, we've set breakpoint
12054 at file:line. There were several PC values for that file:line,
12055 due to optimization, all in one block.
12056
12057 We've picked one PC value. If "clear" is issued with another
12058 PC corresponding to the same file:line, the breakpoint won't
12059 be cleared. We probably can still clear the breakpoint, but
12060 since the other PC value is never presented to user, user
12061 can only find it by guessing, and it does not seem important
12062 to support that. */
12063
12064 /* For each line spec given, delete bps which correspond to it. Do
12065 it in two passes, solely to preserve the current behavior that
12066 from_tty is forced true if we delete more than one
12067 breakpoint. */
12068
12069 found = NULL;
12070 make_cleanup (VEC_cleanup (breakpoint_p), &found);
12071 for (i = 0; i < sals.nelts; i++)
12072 {
12073 const char *sal_fullname;
12074
12075 /* If exact pc given, clear bpts at that pc.
12076 If line given (pc == 0), clear all bpts on specified line.
12077 If defaulting, clear all bpts on default line
12078 or at default pc.
12079
12080 defaulting sal.pc != 0 tests to do
12081
12082 0 1 pc
12083 1 1 pc _and_ line
12084 0 0 line
12085 1 0 <can't happen> */
12086
12087 sal = sals.sals[i];
12088 sal_fullname = (sal.symtab == NULL
12089 ? NULL : symtab_to_fullname (sal.symtab));
12090
12091 /* Find all matching breakpoints and add them to 'found'. */
12092 ALL_BREAKPOINTS (b)
12093 {
12094 int match = 0;
12095 /* Are we going to delete b? */
12096 if (b->type != bp_none && !is_watchpoint (b))
12097 {
12098 struct bp_location *loc = b->loc;
12099 for (; loc; loc = loc->next)
12100 {
12101 /* If the user specified file:line, don't allow a PC
12102 match. This matches historical gdb behavior. */
12103 int pc_match = (!sal.explicit_line
12104 && sal.pc
12105 && (loc->pspace == sal.pspace)
12106 && (loc->address == sal.pc)
12107 && (!section_is_overlay (loc->section)
12108 || loc->section == sal.section));
12109 int line_match = 0;
12110
12111 if ((default_match || sal.explicit_line)
12112 && loc->symtab != NULL
12113 && sal_fullname != NULL
12114 && sal.pspace == loc->pspace
12115 && loc->line_number == sal.line
12116 && filename_cmp (symtab_to_fullname (loc->symtab),
12117 sal_fullname) == 0)
12118 line_match = 1;
12119
12120 if (pc_match || line_match)
12121 {
12122 match = 1;
12123 break;
12124 }
12125 }
12126 }
12127
12128 if (match)
12129 VEC_safe_push(breakpoint_p, found, b);
12130 }
12131 }
12132
12133 /* Now go thru the 'found' chain and delete them. */
12134 if (VEC_empty(breakpoint_p, found))
12135 {
12136 if (arg)
12137 error (_("No breakpoint at %s."), arg);
12138 else
12139 error (_("No breakpoint at this line."));
12140 }
12141
12142 /* Remove duplicates from the vec. */
12143 qsort (VEC_address (breakpoint_p, found),
12144 VEC_length (breakpoint_p, found),
12145 sizeof (breakpoint_p),
12146 compare_breakpoints);
12147 prev = VEC_index (breakpoint_p, found, 0);
12148 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
12149 {
12150 if (b == prev)
12151 {
12152 VEC_ordered_remove (breakpoint_p, found, ix);
12153 --ix;
12154 }
12155 }
12156
12157 if (VEC_length(breakpoint_p, found) > 1)
12158 from_tty = 1; /* Always report if deleted more than one. */
12159 if (from_tty)
12160 {
12161 if (VEC_length(breakpoint_p, found) == 1)
12162 printf_unfiltered (_("Deleted breakpoint "));
12163 else
12164 printf_unfiltered (_("Deleted breakpoints "));
12165 }
12166
12167 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
12168 {
12169 if (from_tty)
12170 printf_unfiltered ("%d ", b->number);
12171 delete_breakpoint (b);
12172 }
12173 if (from_tty)
12174 putchar_unfiltered ('\n');
12175
12176 do_cleanups (cleanups);
12177 }
12178 \f
12179 /* Delete breakpoint in BS if they are `delete' breakpoints and
12180 all breakpoints that are marked for deletion, whether hit or not.
12181 This is called after any breakpoint is hit, or after errors. */
12182
12183 void
12184 breakpoint_auto_delete (bpstat bs)
12185 {
12186 struct breakpoint *b, *b_tmp;
12187
12188 for (; bs; bs = bs->next)
12189 if (bs->breakpoint_at
12190 && bs->breakpoint_at->disposition == disp_del
12191 && bs->stop)
12192 delete_breakpoint (bs->breakpoint_at);
12193
12194 ALL_BREAKPOINTS_SAFE (b, b_tmp)
12195 {
12196 if (b->disposition == disp_del_at_next_stop)
12197 delete_breakpoint (b);
12198 }
12199 }
12200
12201 /* A comparison function for bp_location AP and BP being interfaced to
12202 qsort. Sort elements primarily by their ADDRESS (no matter what
12203 does breakpoint_address_is_meaningful say for its OWNER),
12204 secondarily by ordering first permanent elements and
12205 terciarily just ensuring the array is sorted stable way despite
12206 qsort being an unstable algorithm. */
12207
12208 static int
12209 bp_location_compare (const void *ap, const void *bp)
12210 {
12211 const struct bp_location *a = *(const struct bp_location **) ap;
12212 const struct bp_location *b = *(const struct bp_location **) bp;
12213
12214 if (a->address != b->address)
12215 return (a->address > b->address) - (a->address < b->address);
12216
12217 /* Sort locations at the same address by their pspace number, keeping
12218 locations of the same inferior (in a multi-inferior environment)
12219 grouped. */
12220
12221 if (a->pspace->num != b->pspace->num)
12222 return ((a->pspace->num > b->pspace->num)
12223 - (a->pspace->num < b->pspace->num));
12224
12225 /* Sort permanent breakpoints first. */
12226 if (a->permanent != b->permanent)
12227 return (a->permanent < b->permanent) - (a->permanent > b->permanent);
12228
12229 /* Make the internal GDB representation stable across GDB runs
12230 where A and B memory inside GDB can differ. Breakpoint locations of
12231 the same type at the same address can be sorted in arbitrary order. */
12232
12233 if (a->owner->number != b->owner->number)
12234 return ((a->owner->number > b->owner->number)
12235 - (a->owner->number < b->owner->number));
12236
12237 return (a > b) - (a < b);
12238 }
12239
12240 /* Set bp_location_placed_address_before_address_max and
12241 bp_location_shadow_len_after_address_max according to the current
12242 content of the bp_location array. */
12243
12244 static void
12245 bp_location_target_extensions_update (void)
12246 {
12247 struct bp_location *bl, **blp_tmp;
12248
12249 bp_location_placed_address_before_address_max = 0;
12250 bp_location_shadow_len_after_address_max = 0;
12251
12252 ALL_BP_LOCATIONS (bl, blp_tmp)
12253 {
12254 CORE_ADDR start, end, addr;
12255
12256 if (!bp_location_has_shadow (bl))
12257 continue;
12258
12259 start = bl->target_info.placed_address;
12260 end = start + bl->target_info.shadow_len;
12261
12262 gdb_assert (bl->address >= start);
12263 addr = bl->address - start;
12264 if (addr > bp_location_placed_address_before_address_max)
12265 bp_location_placed_address_before_address_max = addr;
12266
12267 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
12268
12269 gdb_assert (bl->address < end);
12270 addr = end - bl->address;
12271 if (addr > bp_location_shadow_len_after_address_max)
12272 bp_location_shadow_len_after_address_max = addr;
12273 }
12274 }
12275
12276 /* Download tracepoint locations if they haven't been. */
12277
12278 static void
12279 download_tracepoint_locations (void)
12280 {
12281 struct breakpoint *b;
12282 struct cleanup *old_chain;
12283 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
12284
12285 old_chain = save_current_space_and_thread ();
12286
12287 ALL_TRACEPOINTS (b)
12288 {
12289 struct bp_location *bl;
12290 struct tracepoint *t;
12291 int bp_location_downloaded = 0;
12292
12293 if ((b->type == bp_fast_tracepoint
12294 ? !may_insert_fast_tracepoints
12295 : !may_insert_tracepoints))
12296 continue;
12297
12298 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
12299 {
12300 if (target_can_download_tracepoint ())
12301 can_download_tracepoint = TRIBOOL_TRUE;
12302 else
12303 can_download_tracepoint = TRIBOOL_FALSE;
12304 }
12305
12306 if (can_download_tracepoint == TRIBOOL_FALSE)
12307 break;
12308
12309 for (bl = b->loc; bl; bl = bl->next)
12310 {
12311 /* In tracepoint, locations are _never_ duplicated, so
12312 should_be_inserted is equivalent to
12313 unduplicated_should_be_inserted. */
12314 if (!should_be_inserted (bl) || bl->inserted)
12315 continue;
12316
12317 switch_to_program_space_and_thread (bl->pspace);
12318
12319 target_download_tracepoint (bl);
12320
12321 bl->inserted = 1;
12322 bp_location_downloaded = 1;
12323 }
12324 t = (struct tracepoint *) b;
12325 t->number_on_target = b->number;
12326 if (bp_location_downloaded)
12327 observer_notify_breakpoint_modified (b);
12328 }
12329
12330 do_cleanups (old_chain);
12331 }
12332
12333 /* Swap the insertion/duplication state between two locations. */
12334
12335 static void
12336 swap_insertion (struct bp_location *left, struct bp_location *right)
12337 {
12338 const int left_inserted = left->inserted;
12339 const int left_duplicate = left->duplicate;
12340 const int left_needs_update = left->needs_update;
12341 const struct bp_target_info left_target_info = left->target_info;
12342
12343 /* Locations of tracepoints can never be duplicated. */
12344 if (is_tracepoint (left->owner))
12345 gdb_assert (!left->duplicate);
12346 if (is_tracepoint (right->owner))
12347 gdb_assert (!right->duplicate);
12348
12349 left->inserted = right->inserted;
12350 left->duplicate = right->duplicate;
12351 left->needs_update = right->needs_update;
12352 left->target_info = right->target_info;
12353 right->inserted = left_inserted;
12354 right->duplicate = left_duplicate;
12355 right->needs_update = left_needs_update;
12356 right->target_info = left_target_info;
12357 }
12358
12359 /* Force the re-insertion of the locations at ADDRESS. This is called
12360 once a new/deleted/modified duplicate location is found and we are evaluating
12361 conditions on the target's side. Such conditions need to be updated on
12362 the target. */
12363
12364 static void
12365 force_breakpoint_reinsertion (struct bp_location *bl)
12366 {
12367 struct bp_location **locp = NULL, **loc2p;
12368 struct bp_location *loc;
12369 CORE_ADDR address = 0;
12370 int pspace_num;
12371
12372 address = bl->address;
12373 pspace_num = bl->pspace->num;
12374
12375 /* This is only meaningful if the target is
12376 evaluating conditions and if the user has
12377 opted for condition evaluation on the target's
12378 side. */
12379 if (gdb_evaluates_breakpoint_condition_p ()
12380 || !target_supports_evaluation_of_breakpoint_conditions ())
12381 return;
12382
12383 /* Flag all breakpoint locations with this address and
12384 the same program space as the location
12385 as "its condition has changed". We need to
12386 update the conditions on the target's side. */
12387 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
12388 {
12389 loc = *loc2p;
12390
12391 if (!is_breakpoint (loc->owner)
12392 || pspace_num != loc->pspace->num)
12393 continue;
12394
12395 /* Flag the location appropriately. We use a different state to
12396 let everyone know that we already updated the set of locations
12397 with addr bl->address and program space bl->pspace. This is so
12398 we don't have to keep calling these functions just to mark locations
12399 that have already been marked. */
12400 loc->condition_changed = condition_updated;
12401
12402 /* Free the agent expression bytecode as well. We will compute
12403 it later on. */
12404 loc->cond_bytecode.reset ();
12405 }
12406 }
12407 /* Called whether new breakpoints are created, or existing breakpoints
12408 deleted, to update the global location list and recompute which
12409 locations are duplicate of which.
12410
12411 The INSERT_MODE flag determines whether locations may not, may, or
12412 shall be inserted now. See 'enum ugll_insert_mode' for more
12413 info. */
12414
12415 static void
12416 update_global_location_list (enum ugll_insert_mode insert_mode)
12417 {
12418 struct breakpoint *b;
12419 struct bp_location **locp, *loc;
12420 struct cleanup *cleanups;
12421 /* Last breakpoint location address that was marked for update. */
12422 CORE_ADDR last_addr = 0;
12423 /* Last breakpoint location program space that was marked for update. */
12424 int last_pspace_num = -1;
12425
12426 /* Used in the duplicates detection below. When iterating over all
12427 bp_locations, points to the first bp_location of a given address.
12428 Breakpoints and watchpoints of different types are never
12429 duplicates of each other. Keep one pointer for each type of
12430 breakpoint/watchpoint, so we only need to loop over all locations
12431 once. */
12432 struct bp_location *bp_loc_first; /* breakpoint */
12433 struct bp_location *wp_loc_first; /* hardware watchpoint */
12434 struct bp_location *awp_loc_first; /* access watchpoint */
12435 struct bp_location *rwp_loc_first; /* read watchpoint */
12436
12437 /* Saved former bp_location array which we compare against the newly
12438 built bp_location from the current state of ALL_BREAKPOINTS. */
12439 struct bp_location **old_location, **old_locp;
12440 unsigned old_location_count;
12441
12442 old_location = bp_location;
12443 old_location_count = bp_location_count;
12444 bp_location = NULL;
12445 bp_location_count = 0;
12446 cleanups = make_cleanup (xfree, old_location);
12447
12448 ALL_BREAKPOINTS (b)
12449 for (loc = b->loc; loc; loc = loc->next)
12450 bp_location_count++;
12451
12452 bp_location = XNEWVEC (struct bp_location *, bp_location_count);
12453 locp = bp_location;
12454 ALL_BREAKPOINTS (b)
12455 for (loc = b->loc; loc; loc = loc->next)
12456 *locp++ = loc;
12457 qsort (bp_location, bp_location_count, sizeof (*bp_location),
12458 bp_location_compare);
12459
12460 bp_location_target_extensions_update ();
12461
12462 /* Identify bp_location instances that are no longer present in the
12463 new list, and therefore should be freed. Note that it's not
12464 necessary that those locations should be removed from inferior --
12465 if there's another location at the same address (previously
12466 marked as duplicate), we don't need to remove/insert the
12467 location.
12468
12469 LOCP is kept in sync with OLD_LOCP, each pointing to the current
12470 and former bp_location array state respectively. */
12471
12472 locp = bp_location;
12473 for (old_locp = old_location; old_locp < old_location + old_location_count;
12474 old_locp++)
12475 {
12476 struct bp_location *old_loc = *old_locp;
12477 struct bp_location **loc2p;
12478
12479 /* Tells if 'old_loc' is found among the new locations. If
12480 not, we have to free it. */
12481 int found_object = 0;
12482 /* Tells if the location should remain inserted in the target. */
12483 int keep_in_target = 0;
12484 int removed = 0;
12485
12486 /* Skip LOCP entries which will definitely never be needed.
12487 Stop either at or being the one matching OLD_LOC. */
12488 while (locp < bp_location + bp_location_count
12489 && (*locp)->address < old_loc->address)
12490 locp++;
12491
12492 for (loc2p = locp;
12493 (loc2p < bp_location + bp_location_count
12494 && (*loc2p)->address == old_loc->address);
12495 loc2p++)
12496 {
12497 /* Check if this is a new/duplicated location or a duplicated
12498 location that had its condition modified. If so, we want to send
12499 its condition to the target if evaluation of conditions is taking
12500 place there. */
12501 if ((*loc2p)->condition_changed == condition_modified
12502 && (last_addr != old_loc->address
12503 || last_pspace_num != old_loc->pspace->num))
12504 {
12505 force_breakpoint_reinsertion (*loc2p);
12506 last_pspace_num = old_loc->pspace->num;
12507 }
12508
12509 if (*loc2p == old_loc)
12510 found_object = 1;
12511 }
12512
12513 /* We have already handled this address, update it so that we don't
12514 have to go through updates again. */
12515 last_addr = old_loc->address;
12516
12517 /* Target-side condition evaluation: Handle deleted locations. */
12518 if (!found_object)
12519 force_breakpoint_reinsertion (old_loc);
12520
12521 /* If this location is no longer present, and inserted, look if
12522 there's maybe a new location at the same address. If so,
12523 mark that one inserted, and don't remove this one. This is
12524 needed so that we don't have a time window where a breakpoint
12525 at certain location is not inserted. */
12526
12527 if (old_loc->inserted)
12528 {
12529 /* If the location is inserted now, we might have to remove
12530 it. */
12531
12532 if (found_object && should_be_inserted (old_loc))
12533 {
12534 /* The location is still present in the location list,
12535 and still should be inserted. Don't do anything. */
12536 keep_in_target = 1;
12537 }
12538 else
12539 {
12540 /* This location still exists, but it won't be kept in the
12541 target since it may have been disabled. We proceed to
12542 remove its target-side condition. */
12543
12544 /* The location is either no longer present, or got
12545 disabled. See if there's another location at the
12546 same address, in which case we don't need to remove
12547 this one from the target. */
12548
12549 /* OLD_LOC comes from existing struct breakpoint. */
12550 if (breakpoint_address_is_meaningful (old_loc->owner))
12551 {
12552 for (loc2p = locp;
12553 (loc2p < bp_location + bp_location_count
12554 && (*loc2p)->address == old_loc->address);
12555 loc2p++)
12556 {
12557 struct bp_location *loc2 = *loc2p;
12558
12559 if (breakpoint_locations_match (loc2, old_loc))
12560 {
12561 /* Read watchpoint locations are switched to
12562 access watchpoints, if the former are not
12563 supported, but the latter are. */
12564 if (is_hardware_watchpoint (old_loc->owner))
12565 {
12566 gdb_assert (is_hardware_watchpoint (loc2->owner));
12567 loc2->watchpoint_type = old_loc->watchpoint_type;
12568 }
12569
12570 /* loc2 is a duplicated location. We need to check
12571 if it should be inserted in case it will be
12572 unduplicated. */
12573 if (loc2 != old_loc
12574 && unduplicated_should_be_inserted (loc2))
12575 {
12576 swap_insertion (old_loc, loc2);
12577 keep_in_target = 1;
12578 break;
12579 }
12580 }
12581 }
12582 }
12583 }
12584
12585 if (!keep_in_target)
12586 {
12587 if (remove_breakpoint (old_loc))
12588 {
12589 /* This is just about all we can do. We could keep
12590 this location on the global list, and try to
12591 remove it next time, but there's no particular
12592 reason why we will succeed next time.
12593
12594 Note that at this point, old_loc->owner is still
12595 valid, as delete_breakpoint frees the breakpoint
12596 only after calling us. */
12597 printf_filtered (_("warning: Error removing "
12598 "breakpoint %d\n"),
12599 old_loc->owner->number);
12600 }
12601 removed = 1;
12602 }
12603 }
12604
12605 if (!found_object)
12606 {
12607 if (removed && target_is_non_stop_p ()
12608 && need_moribund_for_location_type (old_loc))
12609 {
12610 /* This location was removed from the target. In
12611 non-stop mode, a race condition is possible where
12612 we've removed a breakpoint, but stop events for that
12613 breakpoint are already queued and will arrive later.
12614 We apply an heuristic to be able to distinguish such
12615 SIGTRAPs from other random SIGTRAPs: we keep this
12616 breakpoint location for a bit, and will retire it
12617 after we see some number of events. The theory here
12618 is that reporting of events should, "on the average",
12619 be fair, so after a while we'll see events from all
12620 threads that have anything of interest, and no longer
12621 need to keep this breakpoint location around. We
12622 don't hold locations forever so to reduce chances of
12623 mistaking a non-breakpoint SIGTRAP for a breakpoint
12624 SIGTRAP.
12625
12626 The heuristic failing can be disastrous on
12627 decr_pc_after_break targets.
12628
12629 On decr_pc_after_break targets, like e.g., x86-linux,
12630 if we fail to recognize a late breakpoint SIGTRAP,
12631 because events_till_retirement has reached 0 too
12632 soon, we'll fail to do the PC adjustment, and report
12633 a random SIGTRAP to the user. When the user resumes
12634 the inferior, it will most likely immediately crash
12635 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
12636 corrupted, because of being resumed e.g., in the
12637 middle of a multi-byte instruction, or skipped a
12638 one-byte instruction. This was actually seen happen
12639 on native x86-linux, and should be less rare on
12640 targets that do not support new thread events, like
12641 remote, due to the heuristic depending on
12642 thread_count.
12643
12644 Mistaking a random SIGTRAP for a breakpoint trap
12645 causes similar symptoms (PC adjustment applied when
12646 it shouldn't), but then again, playing with SIGTRAPs
12647 behind the debugger's back is asking for trouble.
12648
12649 Since hardware watchpoint traps are always
12650 distinguishable from other traps, so we don't need to
12651 apply keep hardware watchpoint moribund locations
12652 around. We simply always ignore hardware watchpoint
12653 traps we can no longer explain. */
12654
12655 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12656 old_loc->owner = NULL;
12657
12658 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
12659 }
12660 else
12661 {
12662 old_loc->owner = NULL;
12663 decref_bp_location (&old_loc);
12664 }
12665 }
12666 }
12667
12668 /* Rescan breakpoints at the same address and section, marking the
12669 first one as "first" and any others as "duplicates". This is so
12670 that the bpt instruction is only inserted once. If we have a
12671 permanent breakpoint at the same place as BPT, make that one the
12672 official one, and the rest as duplicates. Permanent breakpoints
12673 are sorted first for the same address.
12674
12675 Do the same for hardware watchpoints, but also considering the
12676 watchpoint's type (regular/access/read) and length. */
12677
12678 bp_loc_first = NULL;
12679 wp_loc_first = NULL;
12680 awp_loc_first = NULL;
12681 rwp_loc_first = NULL;
12682 ALL_BP_LOCATIONS (loc, locp)
12683 {
12684 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12685 non-NULL. */
12686 struct bp_location **loc_first_p;
12687 b = loc->owner;
12688
12689 if (!unduplicated_should_be_inserted (loc)
12690 || !breakpoint_address_is_meaningful (b)
12691 /* Don't detect duplicate for tracepoint locations because they are
12692 never duplicated. See the comments in field `duplicate' of
12693 `struct bp_location'. */
12694 || is_tracepoint (b))
12695 {
12696 /* Clear the condition modification flag. */
12697 loc->condition_changed = condition_unchanged;
12698 continue;
12699 }
12700
12701 if (b->type == bp_hardware_watchpoint)
12702 loc_first_p = &wp_loc_first;
12703 else if (b->type == bp_read_watchpoint)
12704 loc_first_p = &rwp_loc_first;
12705 else if (b->type == bp_access_watchpoint)
12706 loc_first_p = &awp_loc_first;
12707 else
12708 loc_first_p = &bp_loc_first;
12709
12710 if (*loc_first_p == NULL
12711 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12712 || !breakpoint_locations_match (loc, *loc_first_p))
12713 {
12714 *loc_first_p = loc;
12715 loc->duplicate = 0;
12716
12717 if (is_breakpoint (loc->owner) && loc->condition_changed)
12718 {
12719 loc->needs_update = 1;
12720 /* Clear the condition modification flag. */
12721 loc->condition_changed = condition_unchanged;
12722 }
12723 continue;
12724 }
12725
12726
12727 /* This and the above ensure the invariant that the first location
12728 is not duplicated, and is the inserted one.
12729 All following are marked as duplicated, and are not inserted. */
12730 if (loc->inserted)
12731 swap_insertion (loc, *loc_first_p);
12732 loc->duplicate = 1;
12733
12734 /* Clear the condition modification flag. */
12735 loc->condition_changed = condition_unchanged;
12736 }
12737
12738 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
12739 {
12740 if (insert_mode != UGLL_DONT_INSERT)
12741 insert_breakpoint_locations ();
12742 else
12743 {
12744 /* Even though the caller told us to not insert new
12745 locations, we may still need to update conditions on the
12746 target's side of breakpoints that were already inserted
12747 if the target is evaluating breakpoint conditions. We
12748 only update conditions for locations that are marked
12749 "needs_update". */
12750 update_inserted_breakpoint_locations ();
12751 }
12752 }
12753
12754 if (insert_mode != UGLL_DONT_INSERT)
12755 download_tracepoint_locations ();
12756
12757 do_cleanups (cleanups);
12758 }
12759
12760 void
12761 breakpoint_retire_moribund (void)
12762 {
12763 struct bp_location *loc;
12764 int ix;
12765
12766 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12767 if (--(loc->events_till_retirement) == 0)
12768 {
12769 decref_bp_location (&loc);
12770 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12771 --ix;
12772 }
12773 }
12774
12775 static void
12776 update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
12777 {
12778
12779 TRY
12780 {
12781 update_global_location_list (insert_mode);
12782 }
12783 CATCH (e, RETURN_MASK_ERROR)
12784 {
12785 }
12786 END_CATCH
12787 }
12788
12789 /* Clear BKP from a BPS. */
12790
12791 static void
12792 bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12793 {
12794 bpstat bs;
12795
12796 for (bs = bps; bs; bs = bs->next)
12797 if (bs->breakpoint_at == bpt)
12798 {
12799 bs->breakpoint_at = NULL;
12800 bs->old_val = NULL;
12801 /* bs->commands will be freed later. */
12802 }
12803 }
12804
12805 /* Callback for iterate_over_threads. */
12806 static int
12807 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12808 {
12809 struct breakpoint *bpt = (struct breakpoint *) data;
12810
12811 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12812 return 0;
12813 }
12814
12815 /* Helper for breakpoint and tracepoint breakpoint_ops->mention
12816 callbacks. */
12817
12818 static void
12819 say_where (struct breakpoint *b)
12820 {
12821 struct value_print_options opts;
12822
12823 get_user_print_options (&opts);
12824
12825 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12826 single string. */
12827 if (b->loc == NULL)
12828 {
12829 /* For pending locations, the output differs slightly based
12830 on b->extra_string. If this is non-NULL, it contains either
12831 a condition or dprintf arguments. */
12832 if (b->extra_string == NULL)
12833 {
12834 printf_filtered (_(" (%s) pending."),
12835 event_location_to_string (b->location));
12836 }
12837 else if (b->type == bp_dprintf)
12838 {
12839 printf_filtered (_(" (%s,%s) pending."),
12840 event_location_to_string (b->location),
12841 b->extra_string);
12842 }
12843 else
12844 {
12845 printf_filtered (_(" (%s %s) pending."),
12846 event_location_to_string (b->location),
12847 b->extra_string);
12848 }
12849 }
12850 else
12851 {
12852 if (opts.addressprint || b->loc->symtab == NULL)
12853 {
12854 printf_filtered (" at ");
12855 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12856 gdb_stdout);
12857 }
12858 if (b->loc->symtab != NULL)
12859 {
12860 /* If there is a single location, we can print the location
12861 more nicely. */
12862 if (b->loc->next == NULL)
12863 printf_filtered (": file %s, line %d.",
12864 symtab_to_filename_for_display (b->loc->symtab),
12865 b->loc->line_number);
12866 else
12867 /* This is not ideal, but each location may have a
12868 different file name, and this at least reflects the
12869 real situation somewhat. */
12870 printf_filtered (": %s.",
12871 event_location_to_string (b->location));
12872 }
12873
12874 if (b->loc->next)
12875 {
12876 struct bp_location *loc = b->loc;
12877 int n = 0;
12878 for (; loc; loc = loc->next)
12879 ++n;
12880 printf_filtered (" (%d locations)", n);
12881 }
12882 }
12883 }
12884
12885 /* Default bp_location_ops methods. */
12886
12887 static void
12888 bp_location_dtor (struct bp_location *self)
12889 {
12890 xfree (self->function_name);
12891
12892 VEC_free (agent_expr_p, self->target_info.conditions);
12893 VEC_free (agent_expr_p, self->target_info.tcommands);
12894 }
12895
12896 static const struct bp_location_ops bp_location_ops =
12897 {
12898 bp_location_dtor
12899 };
12900
12901 /* Default breakpoint_ops methods all breakpoint_ops ultimately
12902 inherit from. */
12903
12904 static void
12905 base_breakpoint_dtor (struct breakpoint *self)
12906 {
12907 decref_counted_command_line (&self->commands);
12908 xfree (self->cond_string);
12909 xfree (self->extra_string);
12910 xfree (self->filter);
12911 delete_event_location (self->location);
12912 delete_event_location (self->location_range_end);
12913 }
12914
12915 static struct bp_location *
12916 base_breakpoint_allocate_location (struct breakpoint *self)
12917 {
12918 struct bp_location *loc;
12919
12920 loc = new struct bp_location ();
12921 init_bp_location (loc, &bp_location_ops, self);
12922 return loc;
12923 }
12924
12925 static void
12926 base_breakpoint_re_set (struct breakpoint *b)
12927 {
12928 /* Nothing to re-set. */
12929 }
12930
12931 #define internal_error_pure_virtual_called() \
12932 gdb_assert_not_reached ("pure virtual function called")
12933
12934 static int
12935 base_breakpoint_insert_location (struct bp_location *bl)
12936 {
12937 internal_error_pure_virtual_called ();
12938 }
12939
12940 static int
12941 base_breakpoint_remove_location (struct bp_location *bl,
12942 enum remove_bp_reason reason)
12943 {
12944 internal_error_pure_virtual_called ();
12945 }
12946
12947 static int
12948 base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12949 struct address_space *aspace,
12950 CORE_ADDR bp_addr,
12951 const struct target_waitstatus *ws)
12952 {
12953 internal_error_pure_virtual_called ();
12954 }
12955
12956 static void
12957 base_breakpoint_check_status (bpstat bs)
12958 {
12959 /* Always stop. */
12960 }
12961
12962 /* A "works_in_software_mode" breakpoint_ops method that just internal
12963 errors. */
12964
12965 static int
12966 base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12967 {
12968 internal_error_pure_virtual_called ();
12969 }
12970
12971 /* A "resources_needed" breakpoint_ops method that just internal
12972 errors. */
12973
12974 static int
12975 base_breakpoint_resources_needed (const struct bp_location *bl)
12976 {
12977 internal_error_pure_virtual_called ();
12978 }
12979
12980 static enum print_stop_action
12981 base_breakpoint_print_it (bpstat bs)
12982 {
12983 internal_error_pure_virtual_called ();
12984 }
12985
12986 static void
12987 base_breakpoint_print_one_detail (const struct breakpoint *self,
12988 struct ui_out *uiout)
12989 {
12990 /* nothing */
12991 }
12992
12993 static void
12994 base_breakpoint_print_mention (struct breakpoint *b)
12995 {
12996 internal_error_pure_virtual_called ();
12997 }
12998
12999 static void
13000 base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
13001 {
13002 internal_error_pure_virtual_called ();
13003 }
13004
13005 static void
13006 base_breakpoint_create_sals_from_location
13007 (const struct event_location *location,
13008 struct linespec_result *canonical,
13009 enum bptype type_wanted)
13010 {
13011 internal_error_pure_virtual_called ();
13012 }
13013
13014 static void
13015 base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13016 struct linespec_result *c,
13017 char *cond_string,
13018 char *extra_string,
13019 enum bptype type_wanted,
13020 enum bpdisp disposition,
13021 int thread,
13022 int task, int ignore_count,
13023 const struct breakpoint_ops *o,
13024 int from_tty, int enabled,
13025 int internal, unsigned flags)
13026 {
13027 internal_error_pure_virtual_called ();
13028 }
13029
13030 static void
13031 base_breakpoint_decode_location (struct breakpoint *b,
13032 const struct event_location *location,
13033 struct program_space *search_pspace,
13034 struct symtabs_and_lines *sals)
13035 {
13036 internal_error_pure_virtual_called ();
13037 }
13038
13039 /* The default 'explains_signal' method. */
13040
13041 static int
13042 base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
13043 {
13044 return 1;
13045 }
13046
13047 /* The default "after_condition_true" method. */
13048
13049 static void
13050 base_breakpoint_after_condition_true (struct bpstats *bs)
13051 {
13052 /* Nothing to do. */
13053 }
13054
13055 struct breakpoint_ops base_breakpoint_ops =
13056 {
13057 base_breakpoint_dtor,
13058 base_breakpoint_allocate_location,
13059 base_breakpoint_re_set,
13060 base_breakpoint_insert_location,
13061 base_breakpoint_remove_location,
13062 base_breakpoint_breakpoint_hit,
13063 base_breakpoint_check_status,
13064 base_breakpoint_resources_needed,
13065 base_breakpoint_works_in_software_mode,
13066 base_breakpoint_print_it,
13067 NULL,
13068 base_breakpoint_print_one_detail,
13069 base_breakpoint_print_mention,
13070 base_breakpoint_print_recreate,
13071 base_breakpoint_create_sals_from_location,
13072 base_breakpoint_create_breakpoints_sal,
13073 base_breakpoint_decode_location,
13074 base_breakpoint_explains_signal,
13075 base_breakpoint_after_condition_true,
13076 };
13077
13078 /* Default breakpoint_ops methods. */
13079
13080 static void
13081 bkpt_re_set (struct breakpoint *b)
13082 {
13083 /* FIXME: is this still reachable? */
13084 if (breakpoint_event_location_empty_p (b))
13085 {
13086 /* Anything without a location can't be re-set. */
13087 delete_breakpoint (b);
13088 return;
13089 }
13090
13091 breakpoint_re_set_default (b);
13092 }
13093
13094 static int
13095 bkpt_insert_location (struct bp_location *bl)
13096 {
13097 CORE_ADDR addr = bl->target_info.reqstd_address;
13098
13099 bl->target_info.kind = breakpoint_kind (bl, &addr);
13100 bl->target_info.placed_address = addr;
13101
13102 if (bl->loc_type == bp_loc_hardware_breakpoint)
13103 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
13104 else
13105 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
13106 }
13107
13108 static int
13109 bkpt_remove_location (struct bp_location *bl, enum remove_bp_reason reason)
13110 {
13111 if (bl->loc_type == bp_loc_hardware_breakpoint)
13112 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
13113 else
13114 return target_remove_breakpoint (bl->gdbarch, &bl->target_info, reason);
13115 }
13116
13117 static int
13118 bkpt_breakpoint_hit (const struct bp_location *bl,
13119 struct address_space *aspace, CORE_ADDR bp_addr,
13120 const struct target_waitstatus *ws)
13121 {
13122 if (ws->kind != TARGET_WAITKIND_STOPPED
13123 || ws->value.sig != GDB_SIGNAL_TRAP)
13124 return 0;
13125
13126 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
13127 aspace, bp_addr))
13128 return 0;
13129
13130 if (overlay_debugging /* unmapped overlay section */
13131 && section_is_overlay (bl->section)
13132 && !section_is_mapped (bl->section))
13133 return 0;
13134
13135 return 1;
13136 }
13137
13138 static int
13139 dprintf_breakpoint_hit (const struct bp_location *bl,
13140 struct address_space *aspace, CORE_ADDR bp_addr,
13141 const struct target_waitstatus *ws)
13142 {
13143 if (dprintf_style == dprintf_style_agent
13144 && target_can_run_breakpoint_commands ())
13145 {
13146 /* An agent-style dprintf never causes a stop. If we see a trap
13147 for this address it must be for a breakpoint that happens to
13148 be set at the same address. */
13149 return 0;
13150 }
13151
13152 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
13153 }
13154
13155 static int
13156 bkpt_resources_needed (const struct bp_location *bl)
13157 {
13158 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
13159
13160 return 1;
13161 }
13162
13163 static enum print_stop_action
13164 bkpt_print_it (bpstat bs)
13165 {
13166 struct breakpoint *b;
13167 const struct bp_location *bl;
13168 int bp_temp;
13169 struct ui_out *uiout = current_uiout;
13170
13171 gdb_assert (bs->bp_location_at != NULL);
13172
13173 bl = bs->bp_location_at;
13174 b = bs->breakpoint_at;
13175
13176 bp_temp = b->disposition == disp_del;
13177 if (bl->address != bl->requested_address)
13178 breakpoint_adjustment_warning (bl->requested_address,
13179 bl->address,
13180 b->number, 1);
13181 annotate_breakpoint (b->number);
13182 maybe_print_thread_hit_breakpoint (uiout);
13183
13184 if (bp_temp)
13185 ui_out_text (uiout, "Temporary breakpoint ");
13186 else
13187 ui_out_text (uiout, "Breakpoint ");
13188 if (ui_out_is_mi_like_p (uiout))
13189 {
13190 ui_out_field_string (uiout, "reason",
13191 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
13192 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
13193 }
13194 ui_out_field_int (uiout, "bkptno", b->number);
13195 ui_out_text (uiout, ", ");
13196
13197 return PRINT_SRC_AND_LOC;
13198 }
13199
13200 static void
13201 bkpt_print_mention (struct breakpoint *b)
13202 {
13203 if (ui_out_is_mi_like_p (current_uiout))
13204 return;
13205
13206 switch (b->type)
13207 {
13208 case bp_breakpoint:
13209 case bp_gnu_ifunc_resolver:
13210 if (b->disposition == disp_del)
13211 printf_filtered (_("Temporary breakpoint"));
13212 else
13213 printf_filtered (_("Breakpoint"));
13214 printf_filtered (_(" %d"), b->number);
13215 if (b->type == bp_gnu_ifunc_resolver)
13216 printf_filtered (_(" at gnu-indirect-function resolver"));
13217 break;
13218 case bp_hardware_breakpoint:
13219 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
13220 break;
13221 case bp_dprintf:
13222 printf_filtered (_("Dprintf %d"), b->number);
13223 break;
13224 }
13225
13226 say_where (b);
13227 }
13228
13229 static void
13230 bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13231 {
13232 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
13233 fprintf_unfiltered (fp, "tbreak");
13234 else if (tp->type == bp_breakpoint)
13235 fprintf_unfiltered (fp, "break");
13236 else if (tp->type == bp_hardware_breakpoint
13237 && tp->disposition == disp_del)
13238 fprintf_unfiltered (fp, "thbreak");
13239 else if (tp->type == bp_hardware_breakpoint)
13240 fprintf_unfiltered (fp, "hbreak");
13241 else
13242 internal_error (__FILE__, __LINE__,
13243 _("unhandled breakpoint type %d"), (int) tp->type);
13244
13245 fprintf_unfiltered (fp, " %s",
13246 event_location_to_string (tp->location));
13247
13248 /* Print out extra_string if this breakpoint is pending. It might
13249 contain, for example, conditions that were set by the user. */
13250 if (tp->loc == NULL && tp->extra_string != NULL)
13251 fprintf_unfiltered (fp, " %s", tp->extra_string);
13252
13253 print_recreate_thread (tp, fp);
13254 }
13255
13256 static void
13257 bkpt_create_sals_from_location (const struct event_location *location,
13258 struct linespec_result *canonical,
13259 enum bptype type_wanted)
13260 {
13261 create_sals_from_location_default (location, canonical, type_wanted);
13262 }
13263
13264 static void
13265 bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
13266 struct linespec_result *canonical,
13267 char *cond_string,
13268 char *extra_string,
13269 enum bptype type_wanted,
13270 enum bpdisp disposition,
13271 int thread,
13272 int task, int ignore_count,
13273 const struct breakpoint_ops *ops,
13274 int from_tty, int enabled,
13275 int internal, unsigned flags)
13276 {
13277 create_breakpoints_sal_default (gdbarch, canonical,
13278 cond_string, extra_string,
13279 type_wanted,
13280 disposition, thread, task,
13281 ignore_count, ops, from_tty,
13282 enabled, internal, flags);
13283 }
13284
13285 static void
13286 bkpt_decode_location (struct breakpoint *b,
13287 const struct event_location *location,
13288 struct program_space *search_pspace,
13289 struct symtabs_and_lines *sals)
13290 {
13291 decode_location_default (b, location, search_pspace, sals);
13292 }
13293
13294 /* Virtual table for internal breakpoints. */
13295
13296 static void
13297 internal_bkpt_re_set (struct breakpoint *b)
13298 {
13299 switch (b->type)
13300 {
13301 /* Delete overlay event and longjmp master breakpoints; they
13302 will be reset later by breakpoint_re_set. */
13303 case bp_overlay_event:
13304 case bp_longjmp_master:
13305 case bp_std_terminate_master:
13306 case bp_exception_master:
13307 delete_breakpoint (b);
13308 break;
13309
13310 /* This breakpoint is special, it's set up when the inferior
13311 starts and we really don't want to touch it. */
13312 case bp_shlib_event:
13313
13314 /* Like bp_shlib_event, this breakpoint type is special. Once
13315 it is set up, we do not want to touch it. */
13316 case bp_thread_event:
13317 break;
13318 }
13319 }
13320
13321 static void
13322 internal_bkpt_check_status (bpstat bs)
13323 {
13324 if (bs->breakpoint_at->type == bp_shlib_event)
13325 {
13326 /* If requested, stop when the dynamic linker notifies GDB of
13327 events. This allows the user to get control and place
13328 breakpoints in initializer routines for dynamically loaded
13329 objects (among other things). */
13330 bs->stop = stop_on_solib_events;
13331 bs->print = stop_on_solib_events;
13332 }
13333 else
13334 bs->stop = 0;
13335 }
13336
13337 static enum print_stop_action
13338 internal_bkpt_print_it (bpstat bs)
13339 {
13340 struct breakpoint *b;
13341
13342 b = bs->breakpoint_at;
13343
13344 switch (b->type)
13345 {
13346 case bp_shlib_event:
13347 /* Did we stop because the user set the stop_on_solib_events
13348 variable? (If so, we report this as a generic, "Stopped due
13349 to shlib event" message.) */
13350 print_solib_event (0);
13351 break;
13352
13353 case bp_thread_event:
13354 /* Not sure how we will get here.
13355 GDB should not stop for these breakpoints. */
13356 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
13357 break;
13358
13359 case bp_overlay_event:
13360 /* By analogy with the thread event, GDB should not stop for these. */
13361 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
13362 break;
13363
13364 case bp_longjmp_master:
13365 /* These should never be enabled. */
13366 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
13367 break;
13368
13369 case bp_std_terminate_master:
13370 /* These should never be enabled. */
13371 printf_filtered (_("std::terminate Master Breakpoint: "
13372 "gdb should not stop!\n"));
13373 break;
13374
13375 case bp_exception_master:
13376 /* These should never be enabled. */
13377 printf_filtered (_("Exception Master Breakpoint: "
13378 "gdb should not stop!\n"));
13379 break;
13380 }
13381
13382 return PRINT_NOTHING;
13383 }
13384
13385 static void
13386 internal_bkpt_print_mention (struct breakpoint *b)
13387 {
13388 /* Nothing to mention. These breakpoints are internal. */
13389 }
13390
13391 /* Virtual table for momentary breakpoints */
13392
13393 static void
13394 momentary_bkpt_re_set (struct breakpoint *b)
13395 {
13396 /* Keep temporary breakpoints, which can be encountered when we step
13397 over a dlopen call and solib_add is resetting the breakpoints.
13398 Otherwise these should have been blown away via the cleanup chain
13399 or by breakpoint_init_inferior when we rerun the executable. */
13400 }
13401
13402 static void
13403 momentary_bkpt_check_status (bpstat bs)
13404 {
13405 /* Nothing. The point of these breakpoints is causing a stop. */
13406 }
13407
13408 static enum print_stop_action
13409 momentary_bkpt_print_it (bpstat bs)
13410 {
13411 return PRINT_UNKNOWN;
13412 }
13413
13414 static void
13415 momentary_bkpt_print_mention (struct breakpoint *b)
13416 {
13417 /* Nothing to mention. These breakpoints are internal. */
13418 }
13419
13420 /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
13421
13422 It gets cleared already on the removal of the first one of such placed
13423 breakpoints. This is OK as they get all removed altogether. */
13424
13425 static void
13426 longjmp_bkpt_dtor (struct breakpoint *self)
13427 {
13428 struct thread_info *tp = find_thread_global_id (self->thread);
13429
13430 if (tp)
13431 tp->initiating_frame = null_frame_id;
13432
13433 momentary_breakpoint_ops.dtor (self);
13434 }
13435
13436 /* Specific methods for probe breakpoints. */
13437
13438 static int
13439 bkpt_probe_insert_location (struct bp_location *bl)
13440 {
13441 int v = bkpt_insert_location (bl);
13442
13443 if (v == 0)
13444 {
13445 /* The insertion was successful, now let's set the probe's semaphore
13446 if needed. */
13447 if (bl->probe.probe->pops->set_semaphore != NULL)
13448 bl->probe.probe->pops->set_semaphore (bl->probe.probe,
13449 bl->probe.objfile,
13450 bl->gdbarch);
13451 }
13452
13453 return v;
13454 }
13455
13456 static int
13457 bkpt_probe_remove_location (struct bp_location *bl,
13458 enum remove_bp_reason reason)
13459 {
13460 /* Let's clear the semaphore before removing the location. */
13461 if (bl->probe.probe->pops->clear_semaphore != NULL)
13462 bl->probe.probe->pops->clear_semaphore (bl->probe.probe,
13463 bl->probe.objfile,
13464 bl->gdbarch);
13465
13466 return bkpt_remove_location (bl, reason);
13467 }
13468
13469 static void
13470 bkpt_probe_create_sals_from_location (const struct event_location *location,
13471 struct linespec_result *canonical,
13472 enum bptype type_wanted)
13473 {
13474 struct linespec_sals lsal;
13475
13476 lsal.sals = parse_probes (location, NULL, canonical);
13477 lsal.canonical = xstrdup (event_location_to_string (canonical->location));
13478 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13479 }
13480
13481 static void
13482 bkpt_probe_decode_location (struct breakpoint *b,
13483 const struct event_location *location,
13484 struct program_space *search_pspace,
13485 struct symtabs_and_lines *sals)
13486 {
13487 *sals = parse_probes (location, search_pspace, NULL);
13488 if (!sals->sals)
13489 error (_("probe not found"));
13490 }
13491
13492 /* The breakpoint_ops structure to be used in tracepoints. */
13493
13494 static void
13495 tracepoint_re_set (struct breakpoint *b)
13496 {
13497 breakpoint_re_set_default (b);
13498 }
13499
13500 static int
13501 tracepoint_breakpoint_hit (const struct bp_location *bl,
13502 struct address_space *aspace, CORE_ADDR bp_addr,
13503 const struct target_waitstatus *ws)
13504 {
13505 /* By definition, the inferior does not report stops at
13506 tracepoints. */
13507 return 0;
13508 }
13509
13510 static void
13511 tracepoint_print_one_detail (const struct breakpoint *self,
13512 struct ui_out *uiout)
13513 {
13514 struct tracepoint *tp = (struct tracepoint *) self;
13515 if (tp->static_trace_marker_id)
13516 {
13517 gdb_assert (self->type == bp_static_tracepoint);
13518
13519 ui_out_text (uiout, "\tmarker id is ");
13520 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
13521 tp->static_trace_marker_id);
13522 ui_out_text (uiout, "\n");
13523 }
13524 }
13525
13526 static void
13527 tracepoint_print_mention (struct breakpoint *b)
13528 {
13529 if (ui_out_is_mi_like_p (current_uiout))
13530 return;
13531
13532 switch (b->type)
13533 {
13534 case bp_tracepoint:
13535 printf_filtered (_("Tracepoint"));
13536 printf_filtered (_(" %d"), b->number);
13537 break;
13538 case bp_fast_tracepoint:
13539 printf_filtered (_("Fast tracepoint"));
13540 printf_filtered (_(" %d"), b->number);
13541 break;
13542 case bp_static_tracepoint:
13543 printf_filtered (_("Static tracepoint"));
13544 printf_filtered (_(" %d"), b->number);
13545 break;
13546 default:
13547 internal_error (__FILE__, __LINE__,
13548 _("unhandled tracepoint type %d"), (int) b->type);
13549 }
13550
13551 say_where (b);
13552 }
13553
13554 static void
13555 tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
13556 {
13557 struct tracepoint *tp = (struct tracepoint *) self;
13558
13559 if (self->type == bp_fast_tracepoint)
13560 fprintf_unfiltered (fp, "ftrace");
13561 else if (self->type == bp_static_tracepoint)
13562 fprintf_unfiltered (fp, "strace");
13563 else if (self->type == bp_tracepoint)
13564 fprintf_unfiltered (fp, "trace");
13565 else
13566 internal_error (__FILE__, __LINE__,
13567 _("unhandled tracepoint type %d"), (int) self->type);
13568
13569 fprintf_unfiltered (fp, " %s",
13570 event_location_to_string (self->location));
13571 print_recreate_thread (self, fp);
13572
13573 if (tp->pass_count)
13574 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
13575 }
13576
13577 static void
13578 tracepoint_create_sals_from_location (const struct event_location *location,
13579 struct linespec_result *canonical,
13580 enum bptype type_wanted)
13581 {
13582 create_sals_from_location_default (location, canonical, type_wanted);
13583 }
13584
13585 static void
13586 tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13587 struct linespec_result *canonical,
13588 char *cond_string,
13589 char *extra_string,
13590 enum bptype type_wanted,
13591 enum bpdisp disposition,
13592 int thread,
13593 int task, int ignore_count,
13594 const struct breakpoint_ops *ops,
13595 int from_tty, int enabled,
13596 int internal, unsigned flags)
13597 {
13598 create_breakpoints_sal_default (gdbarch, canonical,
13599 cond_string, extra_string,
13600 type_wanted,
13601 disposition, thread, task,
13602 ignore_count, ops, from_tty,
13603 enabled, internal, flags);
13604 }
13605
13606 static void
13607 tracepoint_decode_location (struct breakpoint *b,
13608 const struct event_location *location,
13609 struct program_space *search_pspace,
13610 struct symtabs_and_lines *sals)
13611 {
13612 decode_location_default (b, location, search_pspace, sals);
13613 }
13614
13615 struct breakpoint_ops tracepoint_breakpoint_ops;
13616
13617 /* The breakpoint_ops structure to be use on tracepoints placed in a
13618 static probe. */
13619
13620 static void
13621 tracepoint_probe_create_sals_from_location
13622 (const struct event_location *location,
13623 struct linespec_result *canonical,
13624 enum bptype type_wanted)
13625 {
13626 /* We use the same method for breakpoint on probes. */
13627 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
13628 }
13629
13630 static void
13631 tracepoint_probe_decode_location (struct breakpoint *b,
13632 const struct event_location *location,
13633 struct program_space *search_pspace,
13634 struct symtabs_and_lines *sals)
13635 {
13636 /* We use the same method for breakpoint on probes. */
13637 bkpt_probe_decode_location (b, location, search_pspace, sals);
13638 }
13639
13640 static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13641
13642 /* Dprintf breakpoint_ops methods. */
13643
13644 static void
13645 dprintf_re_set (struct breakpoint *b)
13646 {
13647 breakpoint_re_set_default (b);
13648
13649 /* extra_string should never be non-NULL for dprintf. */
13650 gdb_assert (b->extra_string != NULL);
13651
13652 /* 1 - connect to target 1, that can run breakpoint commands.
13653 2 - create a dprintf, which resolves fine.
13654 3 - disconnect from target 1
13655 4 - connect to target 2, that can NOT run breakpoint commands.
13656
13657 After steps #3/#4, you'll want the dprintf command list to
13658 be updated, because target 1 and 2 may well return different
13659 answers for target_can_run_breakpoint_commands().
13660 Given absence of finer grained resetting, we get to do
13661 it all the time. */
13662 if (b->extra_string != NULL)
13663 update_dprintf_command_list (b);
13664 }
13665
13666 /* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13667
13668 static void
13669 dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13670 {
13671 fprintf_unfiltered (fp, "dprintf %s,%s",
13672 event_location_to_string (tp->location),
13673 tp->extra_string);
13674 print_recreate_thread (tp, fp);
13675 }
13676
13677 /* Implement the "after_condition_true" breakpoint_ops method for
13678 dprintf.
13679
13680 dprintf's are implemented with regular commands in their command
13681 list, but we run the commands here instead of before presenting the
13682 stop to the user, as dprintf's don't actually cause a stop. This
13683 also makes it so that the commands of multiple dprintfs at the same
13684 address are all handled. */
13685
13686 static void
13687 dprintf_after_condition_true (struct bpstats *bs)
13688 {
13689 struct cleanup *old_chain;
13690 struct bpstats tmp_bs = { NULL };
13691 struct bpstats *tmp_bs_p = &tmp_bs;
13692
13693 /* dprintf's never cause a stop. This wasn't set in the
13694 check_status hook instead because that would make the dprintf's
13695 condition not be evaluated. */
13696 bs->stop = 0;
13697
13698 /* Run the command list here. Take ownership of it instead of
13699 copying. We never want these commands to run later in
13700 bpstat_do_actions, if a breakpoint that causes a stop happens to
13701 be set at same address as this dprintf, or even if running the
13702 commands here throws. */
13703 tmp_bs.commands = bs->commands;
13704 bs->commands = NULL;
13705 old_chain = make_cleanup_decref_counted_command_line (&tmp_bs.commands);
13706
13707 bpstat_do_actions_1 (&tmp_bs_p);
13708
13709 /* 'tmp_bs.commands' will usually be NULL by now, but
13710 bpstat_do_actions_1 may return early without processing the whole
13711 list. */
13712 do_cleanups (old_chain);
13713 }
13714
13715 /* The breakpoint_ops structure to be used on static tracepoints with
13716 markers (`-m'). */
13717
13718 static void
13719 strace_marker_create_sals_from_location (const struct event_location *location,
13720 struct linespec_result *canonical,
13721 enum bptype type_wanted)
13722 {
13723 struct linespec_sals lsal;
13724 const char *arg_start, *arg;
13725 char *str;
13726 struct cleanup *cleanup;
13727
13728 arg = arg_start = get_linespec_location (location);
13729 lsal.sals = decode_static_tracepoint_spec (&arg);
13730
13731 str = savestring (arg_start, arg - arg_start);
13732 cleanup = make_cleanup (xfree, str);
13733 canonical->location = new_linespec_location (&str);
13734 do_cleanups (cleanup);
13735
13736 lsal.canonical = xstrdup (event_location_to_string (canonical->location));
13737 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13738 }
13739
13740 static void
13741 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13742 struct linespec_result *canonical,
13743 char *cond_string,
13744 char *extra_string,
13745 enum bptype type_wanted,
13746 enum bpdisp disposition,
13747 int thread,
13748 int task, int ignore_count,
13749 const struct breakpoint_ops *ops,
13750 int from_tty, int enabled,
13751 int internal, unsigned flags)
13752 {
13753 int i;
13754 struct linespec_sals *lsal = VEC_index (linespec_sals,
13755 canonical->sals, 0);
13756
13757 /* If the user is creating a static tracepoint by marker id
13758 (strace -m MARKER_ID), then store the sals index, so that
13759 breakpoint_re_set can try to match up which of the newly
13760 found markers corresponds to this one, and, don't try to
13761 expand multiple locations for each sal, given than SALS
13762 already should contain all sals for MARKER_ID. */
13763
13764 for (i = 0; i < lsal->sals.nelts; ++i)
13765 {
13766 struct symtabs_and_lines expanded;
13767 struct tracepoint *tp;
13768 struct cleanup *old_chain;
13769 struct event_location *location;
13770
13771 expanded.nelts = 1;
13772 expanded.sals = &lsal->sals.sals[i];
13773
13774 location = copy_event_location (canonical->location);
13775 old_chain = make_cleanup_delete_event_location (location);
13776
13777 tp = new tracepoint ();
13778 init_breakpoint_sal (&tp->base, gdbarch, expanded,
13779 location, NULL,
13780 cond_string, extra_string,
13781 type_wanted, disposition,
13782 thread, task, ignore_count, ops,
13783 from_tty, enabled, internal, flags,
13784 canonical->special_display);
13785 /* Given that its possible to have multiple markers with
13786 the same string id, if the user is creating a static
13787 tracepoint by marker id ("strace -m MARKER_ID"), then
13788 store the sals index, so that breakpoint_re_set can
13789 try to match up which of the newly found markers
13790 corresponds to this one */
13791 tp->static_trace_marker_id_idx = i;
13792
13793 install_breakpoint (internal, &tp->base, 0);
13794
13795 discard_cleanups (old_chain);
13796 }
13797 }
13798
13799 static void
13800 strace_marker_decode_location (struct breakpoint *b,
13801 const struct event_location *location,
13802 struct program_space *search_pspace,
13803 struct symtabs_and_lines *sals)
13804 {
13805 struct tracepoint *tp = (struct tracepoint *) b;
13806 const char *s = get_linespec_location (location);
13807
13808 *sals = decode_static_tracepoint_spec (&s);
13809 if (sals->nelts > tp->static_trace_marker_id_idx)
13810 {
13811 sals->sals[0] = sals->sals[tp->static_trace_marker_id_idx];
13812 sals->nelts = 1;
13813 }
13814 else
13815 error (_("marker %s not found"), tp->static_trace_marker_id);
13816 }
13817
13818 static struct breakpoint_ops strace_marker_breakpoint_ops;
13819
13820 static int
13821 strace_marker_p (struct breakpoint *b)
13822 {
13823 return b->ops == &strace_marker_breakpoint_ops;
13824 }
13825
13826 /* Delete a breakpoint and clean up all traces of it in the data
13827 structures. */
13828
13829 void
13830 delete_breakpoint (struct breakpoint *bpt)
13831 {
13832 struct breakpoint *b;
13833
13834 gdb_assert (bpt != NULL);
13835
13836 /* Has this bp already been deleted? This can happen because
13837 multiple lists can hold pointers to bp's. bpstat lists are
13838 especial culprits.
13839
13840 One example of this happening is a watchpoint's scope bp. When
13841 the scope bp triggers, we notice that the watchpoint is out of
13842 scope, and delete it. We also delete its scope bp. But the
13843 scope bp is marked "auto-deleting", and is already on a bpstat.
13844 That bpstat is then checked for auto-deleting bp's, which are
13845 deleted.
13846
13847 A real solution to this problem might involve reference counts in
13848 bp's, and/or giving them pointers back to their referencing
13849 bpstat's, and teaching delete_breakpoint to only free a bp's
13850 storage when no more references were extent. A cheaper bandaid
13851 was chosen. */
13852 if (bpt->type == bp_none)
13853 return;
13854
13855 /* At least avoid this stale reference until the reference counting
13856 of breakpoints gets resolved. */
13857 if (bpt->related_breakpoint != bpt)
13858 {
13859 struct breakpoint *related;
13860 struct watchpoint *w;
13861
13862 if (bpt->type == bp_watchpoint_scope)
13863 w = (struct watchpoint *) bpt->related_breakpoint;
13864 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
13865 w = (struct watchpoint *) bpt;
13866 else
13867 w = NULL;
13868 if (w != NULL)
13869 watchpoint_del_at_next_stop (w);
13870
13871 /* Unlink bpt from the bpt->related_breakpoint ring. */
13872 for (related = bpt; related->related_breakpoint != bpt;
13873 related = related->related_breakpoint);
13874 related->related_breakpoint = bpt->related_breakpoint;
13875 bpt->related_breakpoint = bpt;
13876 }
13877
13878 /* watch_command_1 creates a watchpoint but only sets its number if
13879 update_watchpoint succeeds in creating its bp_locations. If there's
13880 a problem in that process, we'll be asked to delete the half-created
13881 watchpoint. In that case, don't announce the deletion. */
13882 if (bpt->number)
13883 observer_notify_breakpoint_deleted (bpt);
13884
13885 if (breakpoint_chain == bpt)
13886 breakpoint_chain = bpt->next;
13887
13888 ALL_BREAKPOINTS (b)
13889 if (b->next == bpt)
13890 {
13891 b->next = bpt->next;
13892 break;
13893 }
13894
13895 /* Be sure no bpstat's are pointing at the breakpoint after it's
13896 been freed. */
13897 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
13898 in all threads for now. Note that we cannot just remove bpstats
13899 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13900 commands are associated with the bpstat; if we remove it here,
13901 then the later call to bpstat_do_actions (&stop_bpstat); in
13902 event-top.c won't do anything, and temporary breakpoints with
13903 commands won't work. */
13904
13905 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13906
13907 /* Now that breakpoint is removed from breakpoint list, update the
13908 global location list. This will remove locations that used to
13909 belong to this breakpoint. Do this before freeing the breakpoint
13910 itself, since remove_breakpoint looks at location's owner. It
13911 might be better design to have location completely
13912 self-contained, but it's not the case now. */
13913 update_global_location_list (UGLL_DONT_INSERT);
13914
13915 bpt->ops->dtor (bpt);
13916 /* On the chance that someone will soon try again to delete this
13917 same bp, we mark it as deleted before freeing its storage. */
13918 bpt->type = bp_none;
13919 delete bpt;
13920 }
13921
13922 static void
13923 do_delete_breakpoint_cleanup (void *b)
13924 {
13925 delete_breakpoint ((struct breakpoint *) b);
13926 }
13927
13928 struct cleanup *
13929 make_cleanup_delete_breakpoint (struct breakpoint *b)
13930 {
13931 return make_cleanup (do_delete_breakpoint_cleanup, b);
13932 }
13933
13934 /* Iterator function to call a user-provided callback function once
13935 for each of B and its related breakpoints. */
13936
13937 static void
13938 iterate_over_related_breakpoints (struct breakpoint *b,
13939 void (*function) (struct breakpoint *,
13940 void *),
13941 void *data)
13942 {
13943 struct breakpoint *related;
13944
13945 related = b;
13946 do
13947 {
13948 struct breakpoint *next;
13949
13950 /* FUNCTION may delete RELATED. */
13951 next = related->related_breakpoint;
13952
13953 if (next == related)
13954 {
13955 /* RELATED is the last ring entry. */
13956 function (related, data);
13957
13958 /* FUNCTION may have deleted it, so we'd never reach back to
13959 B. There's nothing left to do anyway, so just break
13960 out. */
13961 break;
13962 }
13963 else
13964 function (related, data);
13965
13966 related = next;
13967 }
13968 while (related != b);
13969 }
13970
13971 static void
13972 do_delete_breakpoint (struct breakpoint *b, void *ignore)
13973 {
13974 delete_breakpoint (b);
13975 }
13976
13977 /* A callback for map_breakpoint_numbers that calls
13978 delete_breakpoint. */
13979
13980 static void
13981 do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
13982 {
13983 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
13984 }
13985
13986 void
13987 delete_command (char *arg, int from_tty)
13988 {
13989 struct breakpoint *b, *b_tmp;
13990
13991 dont_repeat ();
13992
13993 if (arg == 0)
13994 {
13995 int breaks_to_delete = 0;
13996
13997 /* Delete all breakpoints if no argument. Do not delete
13998 internal breakpoints, these have to be deleted with an
13999 explicit breakpoint number argument. */
14000 ALL_BREAKPOINTS (b)
14001 if (user_breakpoint_p (b))
14002 {
14003 breaks_to_delete = 1;
14004 break;
14005 }
14006
14007 /* Ask user only if there are some breakpoints to delete. */
14008 if (!from_tty
14009 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
14010 {
14011 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14012 if (user_breakpoint_p (b))
14013 delete_breakpoint (b);
14014 }
14015 }
14016 else
14017 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
14018 }
14019
14020 /* Return true if all locations of B bound to PSPACE are pending. If
14021 PSPACE is NULL, all locations of all program spaces are
14022 considered. */
14023
14024 static int
14025 all_locations_are_pending (struct breakpoint *b, struct program_space *pspace)
14026 {
14027 struct bp_location *loc;
14028
14029 for (loc = b->loc; loc != NULL; loc = loc->next)
14030 if ((pspace == NULL
14031 || loc->pspace == pspace)
14032 && !loc->shlib_disabled
14033 && !loc->pspace->executing_startup)
14034 return 0;
14035 return 1;
14036 }
14037
14038 /* Subroutine of update_breakpoint_locations to simplify it.
14039 Return non-zero if multiple fns in list LOC have the same name.
14040 Null names are ignored. */
14041
14042 static int
14043 ambiguous_names_p (struct bp_location *loc)
14044 {
14045 struct bp_location *l;
14046 htab_t htab = htab_create_alloc (13, htab_hash_string,
14047 (int (*) (const void *,
14048 const void *)) streq,
14049 NULL, xcalloc, xfree);
14050
14051 for (l = loc; l != NULL; l = l->next)
14052 {
14053 const char **slot;
14054 const char *name = l->function_name;
14055
14056 /* Allow for some names to be NULL, ignore them. */
14057 if (name == NULL)
14058 continue;
14059
14060 slot = (const char **) htab_find_slot (htab, (const void *) name,
14061 INSERT);
14062 /* NOTE: We can assume slot != NULL here because xcalloc never
14063 returns NULL. */
14064 if (*slot != NULL)
14065 {
14066 htab_delete (htab);
14067 return 1;
14068 }
14069 *slot = name;
14070 }
14071
14072 htab_delete (htab);
14073 return 0;
14074 }
14075
14076 /* When symbols change, it probably means the sources changed as well,
14077 and it might mean the static tracepoint markers are no longer at
14078 the same address or line numbers they used to be at last we
14079 checked. Losing your static tracepoints whenever you rebuild is
14080 undesirable. This function tries to resync/rematch gdb static
14081 tracepoints with the markers on the target, for static tracepoints
14082 that have not been set by marker id. Static tracepoint that have
14083 been set by marker id are reset by marker id in breakpoint_re_set.
14084 The heuristic is:
14085
14086 1) For a tracepoint set at a specific address, look for a marker at
14087 the old PC. If one is found there, assume to be the same marker.
14088 If the name / string id of the marker found is different from the
14089 previous known name, assume that means the user renamed the marker
14090 in the sources, and output a warning.
14091
14092 2) For a tracepoint set at a given line number, look for a marker
14093 at the new address of the old line number. If one is found there,
14094 assume to be the same marker. If the name / string id of the
14095 marker found is different from the previous known name, assume that
14096 means the user renamed the marker in the sources, and output a
14097 warning.
14098
14099 3) If a marker is no longer found at the same address or line, it
14100 may mean the marker no longer exists. But it may also just mean
14101 the code changed a bit. Maybe the user added a few lines of code
14102 that made the marker move up or down (in line number terms). Ask
14103 the target for info about the marker with the string id as we knew
14104 it. If found, update line number and address in the matching
14105 static tracepoint. This will get confused if there's more than one
14106 marker with the same ID (possible in UST, although unadvised
14107 precisely because it confuses tools). */
14108
14109 static struct symtab_and_line
14110 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
14111 {
14112 struct tracepoint *tp = (struct tracepoint *) b;
14113 struct static_tracepoint_marker marker;
14114 CORE_ADDR pc;
14115
14116 pc = sal.pc;
14117 if (sal.line)
14118 find_line_pc (sal.symtab, sal.line, &pc);
14119
14120 if (target_static_tracepoint_marker_at (pc, &marker))
14121 {
14122 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
14123 warning (_("static tracepoint %d changed probed marker from %s to %s"),
14124 b->number,
14125 tp->static_trace_marker_id, marker.str_id);
14126
14127 xfree (tp->static_trace_marker_id);
14128 tp->static_trace_marker_id = xstrdup (marker.str_id);
14129 release_static_tracepoint_marker (&marker);
14130
14131 return sal;
14132 }
14133
14134 /* Old marker wasn't found on target at lineno. Try looking it up
14135 by string ID. */
14136 if (!sal.explicit_pc
14137 && sal.line != 0
14138 && sal.symtab != NULL
14139 && tp->static_trace_marker_id != NULL)
14140 {
14141 VEC(static_tracepoint_marker_p) *markers;
14142
14143 markers
14144 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
14145
14146 if (!VEC_empty(static_tracepoint_marker_p, markers))
14147 {
14148 struct symtab_and_line sal2;
14149 struct symbol *sym;
14150 struct static_tracepoint_marker *tpmarker;
14151 struct ui_out *uiout = current_uiout;
14152 struct explicit_location explicit_loc;
14153
14154 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
14155
14156 xfree (tp->static_trace_marker_id);
14157 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
14158
14159 warning (_("marker for static tracepoint %d (%s) not "
14160 "found at previous line number"),
14161 b->number, tp->static_trace_marker_id);
14162
14163 init_sal (&sal2);
14164
14165 sal2.pc = tpmarker->address;
14166
14167 sal2 = find_pc_line (tpmarker->address, 0);
14168 sym = find_pc_sect_function (tpmarker->address, NULL);
14169 ui_out_text (uiout, "Now in ");
14170 if (sym)
14171 {
14172 ui_out_field_string (uiout, "func",
14173 SYMBOL_PRINT_NAME (sym));
14174 ui_out_text (uiout, " at ");
14175 }
14176 ui_out_field_string (uiout, "file",
14177 symtab_to_filename_for_display (sal2.symtab));
14178 ui_out_text (uiout, ":");
14179
14180 if (ui_out_is_mi_like_p (uiout))
14181 {
14182 const char *fullname = symtab_to_fullname (sal2.symtab);
14183
14184 ui_out_field_string (uiout, "fullname", fullname);
14185 }
14186
14187 ui_out_field_int (uiout, "line", sal2.line);
14188 ui_out_text (uiout, "\n");
14189
14190 b->loc->line_number = sal2.line;
14191 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
14192
14193 delete_event_location (b->location);
14194 initialize_explicit_location (&explicit_loc);
14195 explicit_loc.source_filename
14196 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
14197 explicit_loc.line_offset.offset = b->loc->line_number;
14198 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
14199 b->location = new_explicit_location (&explicit_loc);
14200
14201 /* Might be nice to check if function changed, and warn if
14202 so. */
14203
14204 release_static_tracepoint_marker (tpmarker);
14205 }
14206 }
14207 return sal;
14208 }
14209
14210 /* Returns 1 iff locations A and B are sufficiently same that
14211 we don't need to report breakpoint as changed. */
14212
14213 static int
14214 locations_are_equal (struct bp_location *a, struct bp_location *b)
14215 {
14216 while (a && b)
14217 {
14218 if (a->address != b->address)
14219 return 0;
14220
14221 if (a->shlib_disabled != b->shlib_disabled)
14222 return 0;
14223
14224 if (a->enabled != b->enabled)
14225 return 0;
14226
14227 a = a->next;
14228 b = b->next;
14229 }
14230
14231 if ((a == NULL) != (b == NULL))
14232 return 0;
14233
14234 return 1;
14235 }
14236
14237 /* Split all locations of B that are bound to PSPACE out of B's
14238 location list to a separate list and return that list's head. If
14239 PSPACE is NULL, hoist out all locations of B. */
14240
14241 static struct bp_location *
14242 hoist_existing_locations (struct breakpoint *b, struct program_space *pspace)
14243 {
14244 struct bp_location head;
14245 struct bp_location *i = b->loc;
14246 struct bp_location **i_link = &b->loc;
14247 struct bp_location *hoisted = &head;
14248
14249 if (pspace == NULL)
14250 {
14251 i = b->loc;
14252 b->loc = NULL;
14253 return i;
14254 }
14255
14256 head.next = NULL;
14257
14258 while (i != NULL)
14259 {
14260 if (i->pspace == pspace)
14261 {
14262 *i_link = i->next;
14263 i->next = NULL;
14264 hoisted->next = i;
14265 hoisted = i;
14266 }
14267 else
14268 i_link = &i->next;
14269 i = *i_link;
14270 }
14271
14272 return head.next;
14273 }
14274
14275 /* Create new breakpoint locations for B (a hardware or software
14276 breakpoint) based on SALS and SALS_END. If SALS_END.NELTS is not
14277 zero, then B is a ranged breakpoint. Only recreates locations for
14278 FILTER_PSPACE. Locations of other program spaces are left
14279 untouched. */
14280
14281 void
14282 update_breakpoint_locations (struct breakpoint *b,
14283 struct program_space *filter_pspace,
14284 struct symtabs_and_lines sals,
14285 struct symtabs_and_lines sals_end)
14286 {
14287 int i;
14288 struct bp_location *existing_locations;
14289
14290 if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
14291 {
14292 /* Ranged breakpoints have only one start location and one end
14293 location. */
14294 b->enable_state = bp_disabled;
14295 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
14296 "multiple locations found\n"),
14297 b->number);
14298 return;
14299 }
14300
14301 /* If there's no new locations, and all existing locations are
14302 pending, don't do anything. This optimizes the common case where
14303 all locations are in the same shared library, that was unloaded.
14304 We'd like to retain the location, so that when the library is
14305 loaded again, we don't loose the enabled/disabled status of the
14306 individual locations. */
14307 if (all_locations_are_pending (b, filter_pspace) && sals.nelts == 0)
14308 return;
14309
14310 existing_locations = hoist_existing_locations (b, filter_pspace);
14311
14312 for (i = 0; i < sals.nelts; ++i)
14313 {
14314 struct bp_location *new_loc;
14315
14316 switch_to_program_space_and_thread (sals.sals[i].pspace);
14317
14318 new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
14319
14320 /* Reparse conditions, they might contain references to the
14321 old symtab. */
14322 if (b->cond_string != NULL)
14323 {
14324 const char *s;
14325
14326 s = b->cond_string;
14327 TRY
14328 {
14329 new_loc->cond = parse_exp_1 (&s, sals.sals[i].pc,
14330 block_for_pc (sals.sals[i].pc),
14331 0);
14332 }
14333 CATCH (e, RETURN_MASK_ERROR)
14334 {
14335 warning (_("failed to reevaluate condition "
14336 "for breakpoint %d: %s"),
14337 b->number, e.message);
14338 new_loc->enabled = 0;
14339 }
14340 END_CATCH
14341 }
14342
14343 if (sals_end.nelts)
14344 {
14345 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
14346
14347 new_loc->length = end - sals.sals[0].pc + 1;
14348 }
14349 }
14350
14351 /* If possible, carry over 'disable' status from existing
14352 breakpoints. */
14353 {
14354 struct bp_location *e = existing_locations;
14355 /* If there are multiple breakpoints with the same function name,
14356 e.g. for inline functions, comparing function names won't work.
14357 Instead compare pc addresses; this is just a heuristic as things
14358 may have moved, but in practice it gives the correct answer
14359 often enough until a better solution is found. */
14360 int have_ambiguous_names = ambiguous_names_p (b->loc);
14361
14362 for (; e; e = e->next)
14363 {
14364 if (!e->enabled && e->function_name)
14365 {
14366 struct bp_location *l = b->loc;
14367 if (have_ambiguous_names)
14368 {
14369 for (; l; l = l->next)
14370 if (breakpoint_locations_match (e, l))
14371 {
14372 l->enabled = 0;
14373 break;
14374 }
14375 }
14376 else
14377 {
14378 for (; l; l = l->next)
14379 if (l->function_name
14380 && strcmp (e->function_name, l->function_name) == 0)
14381 {
14382 l->enabled = 0;
14383 break;
14384 }
14385 }
14386 }
14387 }
14388 }
14389
14390 if (!locations_are_equal (existing_locations, b->loc))
14391 observer_notify_breakpoint_modified (b);
14392 }
14393
14394 /* Find the SaL locations corresponding to the given LOCATION.
14395 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
14396
14397 static struct symtabs_and_lines
14398 location_to_sals (struct breakpoint *b, struct event_location *location,
14399 struct program_space *search_pspace, int *found)
14400 {
14401 struct symtabs_and_lines sals = {0};
14402 struct gdb_exception exception = exception_none;
14403
14404 gdb_assert (b->ops != NULL);
14405
14406 TRY
14407 {
14408 b->ops->decode_location (b, location, search_pspace, &sals);
14409 }
14410 CATCH (e, RETURN_MASK_ERROR)
14411 {
14412 int not_found_and_ok = 0;
14413
14414 exception = e;
14415
14416 /* For pending breakpoints, it's expected that parsing will
14417 fail until the right shared library is loaded. User has
14418 already told to create pending breakpoints and don't need
14419 extra messages. If breakpoint is in bp_shlib_disabled
14420 state, then user already saw the message about that
14421 breakpoint being disabled, and don't want to see more
14422 errors. */
14423 if (e.error == NOT_FOUND_ERROR
14424 && (b->condition_not_parsed
14425 || (b->loc != NULL
14426 && search_pspace != NULL
14427 && b->loc->pspace != search_pspace)
14428 || (b->loc && b->loc->shlib_disabled)
14429 || (b->loc && b->loc->pspace->executing_startup)
14430 || b->enable_state == bp_disabled))
14431 not_found_and_ok = 1;
14432
14433 if (!not_found_and_ok)
14434 {
14435 /* We surely don't want to warn about the same breakpoint
14436 10 times. One solution, implemented here, is disable
14437 the breakpoint on error. Another solution would be to
14438 have separate 'warning emitted' flag. Since this
14439 happens only when a binary has changed, I don't know
14440 which approach is better. */
14441 b->enable_state = bp_disabled;
14442 throw_exception (e);
14443 }
14444 }
14445 END_CATCH
14446
14447 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
14448 {
14449 int i;
14450
14451 for (i = 0; i < sals.nelts; ++i)
14452 resolve_sal_pc (&sals.sals[i]);
14453 if (b->condition_not_parsed && b->extra_string != NULL)
14454 {
14455 char *cond_string, *extra_string;
14456 int thread, task;
14457
14458 find_condition_and_thread (b->extra_string, sals.sals[0].pc,
14459 &cond_string, &thread, &task,
14460 &extra_string);
14461 gdb_assert (b->cond_string == NULL);
14462 if (cond_string)
14463 b->cond_string = cond_string;
14464 b->thread = thread;
14465 b->task = task;
14466 if (extra_string)
14467 {
14468 xfree (b->extra_string);
14469 b->extra_string = extra_string;
14470 }
14471 b->condition_not_parsed = 0;
14472 }
14473
14474 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
14475 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
14476
14477 *found = 1;
14478 }
14479 else
14480 *found = 0;
14481
14482 return sals;
14483 }
14484
14485 /* The default re_set method, for typical hardware or software
14486 breakpoints. Reevaluate the breakpoint and recreate its
14487 locations. */
14488
14489 static void
14490 breakpoint_re_set_default (struct breakpoint *b)
14491 {
14492 int found;
14493 struct symtabs_and_lines sals, sals_end;
14494 struct symtabs_and_lines expanded = {0};
14495 struct symtabs_and_lines expanded_end = {0};
14496 struct program_space *filter_pspace = current_program_space;
14497
14498 sals = location_to_sals (b, b->location, filter_pspace, &found);
14499 if (found)
14500 {
14501 make_cleanup (xfree, sals.sals);
14502 expanded = sals;
14503 }
14504
14505 if (b->location_range_end != NULL)
14506 {
14507 sals_end = location_to_sals (b, b->location_range_end,
14508 filter_pspace, &found);
14509 if (found)
14510 {
14511 make_cleanup (xfree, sals_end.sals);
14512 expanded_end = sals_end;
14513 }
14514 }
14515
14516 update_breakpoint_locations (b, filter_pspace, expanded, expanded_end);
14517 }
14518
14519 /* Default method for creating SALs from an address string. It basically
14520 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
14521
14522 static void
14523 create_sals_from_location_default (const struct event_location *location,
14524 struct linespec_result *canonical,
14525 enum bptype type_wanted)
14526 {
14527 parse_breakpoint_sals (location, canonical);
14528 }
14529
14530 /* Call create_breakpoints_sal for the given arguments. This is the default
14531 function for the `create_breakpoints_sal' method of
14532 breakpoint_ops. */
14533
14534 static void
14535 create_breakpoints_sal_default (struct gdbarch *gdbarch,
14536 struct linespec_result *canonical,
14537 char *cond_string,
14538 char *extra_string,
14539 enum bptype type_wanted,
14540 enum bpdisp disposition,
14541 int thread,
14542 int task, int ignore_count,
14543 const struct breakpoint_ops *ops,
14544 int from_tty, int enabled,
14545 int internal, unsigned flags)
14546 {
14547 create_breakpoints_sal (gdbarch, canonical, cond_string,
14548 extra_string,
14549 type_wanted, disposition,
14550 thread, task, ignore_count, ops, from_tty,
14551 enabled, internal, flags);
14552 }
14553
14554 /* Decode the line represented by S by calling decode_line_full. This is the
14555 default function for the `decode_location' method of breakpoint_ops. */
14556
14557 static void
14558 decode_location_default (struct breakpoint *b,
14559 const struct event_location *location,
14560 struct program_space *search_pspace,
14561 struct symtabs_and_lines *sals)
14562 {
14563 struct linespec_result canonical;
14564
14565 init_linespec_result (&canonical);
14566 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE, search_pspace,
14567 (struct symtab *) NULL, 0,
14568 &canonical, multiple_symbols_all,
14569 b->filter);
14570
14571 /* We should get 0 or 1 resulting SALs. */
14572 gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
14573
14574 if (VEC_length (linespec_sals, canonical.sals) > 0)
14575 {
14576 struct linespec_sals *lsal;
14577
14578 lsal = VEC_index (linespec_sals, canonical.sals, 0);
14579 *sals = lsal->sals;
14580 /* Arrange it so the destructor does not free the
14581 contents. */
14582 lsal->sals.sals = NULL;
14583 }
14584
14585 destroy_linespec_result (&canonical);
14586 }
14587
14588 /* Prepare the global context for a re-set of breakpoint B. */
14589
14590 static struct cleanup *
14591 prepare_re_set_context (struct breakpoint *b)
14592 {
14593 input_radix = b->input_radix;
14594 set_language (b->language);
14595
14596 return make_cleanup (null_cleanup, NULL);
14597 }
14598
14599 /* Reset a breakpoint given it's struct breakpoint * BINT.
14600 The value we return ends up being the return value from catch_errors.
14601 Unused in this case. */
14602
14603 static int
14604 breakpoint_re_set_one (void *bint)
14605 {
14606 /* Get past catch_errs. */
14607 struct breakpoint *b = (struct breakpoint *) bint;
14608 struct cleanup *cleanups;
14609
14610 cleanups = prepare_re_set_context (b);
14611 b->ops->re_set (b);
14612 do_cleanups (cleanups);
14613 return 0;
14614 }
14615
14616 /* Re-set breakpoint locations for the current program space.
14617 Locations bound to other program spaces are left untouched. */
14618
14619 void
14620 breakpoint_re_set (void)
14621 {
14622 struct breakpoint *b, *b_tmp;
14623 enum language save_language;
14624 int save_input_radix;
14625 struct cleanup *old_chain;
14626
14627 save_language = current_language->la_language;
14628 save_input_radix = input_radix;
14629 old_chain = save_current_space_and_thread ();
14630
14631 /* Note: we must not try to insert locations until after all
14632 breakpoints have been re-set. Otherwise, e.g., when re-setting
14633 breakpoint 1, we'd insert the locations of breakpoint 2, which
14634 hadn't been re-set yet, and thus may have stale locations. */
14635
14636 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14637 {
14638 /* Format possible error msg. */
14639 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
14640 b->number);
14641 struct cleanup *cleanups = make_cleanup (xfree, message);
14642 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
14643 do_cleanups (cleanups);
14644 }
14645 set_language (save_language);
14646 input_radix = save_input_radix;
14647
14648 jit_breakpoint_re_set ();
14649
14650 do_cleanups (old_chain);
14651
14652 create_overlay_event_breakpoint ();
14653 create_longjmp_master_breakpoint ();
14654 create_std_terminate_master_breakpoint ();
14655 create_exception_master_breakpoint ();
14656
14657 /* Now we can insert. */
14658 update_global_location_list (UGLL_MAY_INSERT);
14659 }
14660 \f
14661 /* Reset the thread number of this breakpoint:
14662
14663 - If the breakpoint is for all threads, leave it as-is.
14664 - Else, reset it to the current thread for inferior_ptid. */
14665 void
14666 breakpoint_re_set_thread (struct breakpoint *b)
14667 {
14668 if (b->thread != -1)
14669 {
14670 if (in_thread_list (inferior_ptid))
14671 b->thread = ptid_to_global_thread_id (inferior_ptid);
14672
14673 /* We're being called after following a fork. The new fork is
14674 selected as current, and unless this was a vfork will have a
14675 different program space from the original thread. Reset that
14676 as well. */
14677 b->loc->pspace = current_program_space;
14678 }
14679 }
14680
14681 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14682 If from_tty is nonzero, it prints a message to that effect,
14683 which ends with a period (no newline). */
14684
14685 void
14686 set_ignore_count (int bptnum, int count, int from_tty)
14687 {
14688 struct breakpoint *b;
14689
14690 if (count < 0)
14691 count = 0;
14692
14693 ALL_BREAKPOINTS (b)
14694 if (b->number == bptnum)
14695 {
14696 if (is_tracepoint (b))
14697 {
14698 if (from_tty && count != 0)
14699 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14700 bptnum);
14701 return;
14702 }
14703
14704 b->ignore_count = count;
14705 if (from_tty)
14706 {
14707 if (count == 0)
14708 printf_filtered (_("Will stop next time "
14709 "breakpoint %d is reached."),
14710 bptnum);
14711 else if (count == 1)
14712 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
14713 bptnum);
14714 else
14715 printf_filtered (_("Will ignore next %d "
14716 "crossings of breakpoint %d."),
14717 count, bptnum);
14718 }
14719 observer_notify_breakpoint_modified (b);
14720 return;
14721 }
14722
14723 error (_("No breakpoint number %d."), bptnum);
14724 }
14725
14726 /* Command to set ignore-count of breakpoint N to COUNT. */
14727
14728 static void
14729 ignore_command (char *args, int from_tty)
14730 {
14731 char *p = args;
14732 int num;
14733
14734 if (p == 0)
14735 error_no_arg (_("a breakpoint number"));
14736
14737 num = get_number (&p);
14738 if (num == 0)
14739 error (_("bad breakpoint number: '%s'"), args);
14740 if (*p == 0)
14741 error (_("Second argument (specified ignore-count) is missing."));
14742
14743 set_ignore_count (num,
14744 longest_to_int (value_as_long (parse_and_eval (p))),
14745 from_tty);
14746 if (from_tty)
14747 printf_filtered ("\n");
14748 }
14749 \f
14750 /* Call FUNCTION on each of the breakpoints
14751 whose numbers are given in ARGS. */
14752
14753 static void
14754 map_breakpoint_numbers (const char *args,
14755 void (*function) (struct breakpoint *,
14756 void *),
14757 void *data)
14758 {
14759 int num;
14760 struct breakpoint *b, *tmp;
14761
14762 if (args == 0 || *args == '\0')
14763 error_no_arg (_("one or more breakpoint numbers"));
14764
14765 number_or_range_parser parser (args);
14766
14767 while (!parser.finished ())
14768 {
14769 const char *p = parser.cur_tok ();
14770 bool match = false;
14771
14772 num = parser.get_number ();
14773 if (num == 0)
14774 {
14775 warning (_("bad breakpoint number at or near '%s'"), p);
14776 }
14777 else
14778 {
14779 ALL_BREAKPOINTS_SAFE (b, tmp)
14780 if (b->number == num)
14781 {
14782 match = true;
14783 function (b, data);
14784 break;
14785 }
14786 if (!match)
14787 printf_unfiltered (_("No breakpoint number %d.\n"), num);
14788 }
14789 }
14790 }
14791
14792 static struct bp_location *
14793 find_location_by_number (char *number)
14794 {
14795 char *dot = strchr (number, '.');
14796 char *p1;
14797 int bp_num;
14798 int loc_num;
14799 struct breakpoint *b;
14800 struct bp_location *loc;
14801
14802 *dot = '\0';
14803
14804 p1 = number;
14805 bp_num = get_number (&p1);
14806 if (bp_num == 0)
14807 error (_("Bad breakpoint number '%s'"), number);
14808
14809 ALL_BREAKPOINTS (b)
14810 if (b->number == bp_num)
14811 {
14812 break;
14813 }
14814
14815 if (!b || b->number != bp_num)
14816 error (_("Bad breakpoint number '%s'"), number);
14817
14818 p1 = dot+1;
14819 loc_num = get_number (&p1);
14820 if (loc_num == 0)
14821 error (_("Bad breakpoint location number '%s'"), number);
14822
14823 --loc_num;
14824 loc = b->loc;
14825 for (;loc_num && loc; --loc_num, loc = loc->next)
14826 ;
14827 if (!loc)
14828 error (_("Bad breakpoint location number '%s'"), dot+1);
14829
14830 return loc;
14831 }
14832
14833
14834 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14835 If from_tty is nonzero, it prints a message to that effect,
14836 which ends with a period (no newline). */
14837
14838 void
14839 disable_breakpoint (struct breakpoint *bpt)
14840 {
14841 /* Never disable a watchpoint scope breakpoint; we want to
14842 hit them when we leave scope so we can delete both the
14843 watchpoint and its scope breakpoint at that time. */
14844 if (bpt->type == bp_watchpoint_scope)
14845 return;
14846
14847 bpt->enable_state = bp_disabled;
14848
14849 /* Mark breakpoint locations modified. */
14850 mark_breakpoint_modified (bpt);
14851
14852 if (target_supports_enable_disable_tracepoint ()
14853 && current_trace_status ()->running && is_tracepoint (bpt))
14854 {
14855 struct bp_location *location;
14856
14857 for (location = bpt->loc; location; location = location->next)
14858 target_disable_tracepoint (location);
14859 }
14860
14861 update_global_location_list (UGLL_DONT_INSERT);
14862
14863 observer_notify_breakpoint_modified (bpt);
14864 }
14865
14866 /* A callback for iterate_over_related_breakpoints. */
14867
14868 static void
14869 do_disable_breakpoint (struct breakpoint *b, void *ignore)
14870 {
14871 disable_breakpoint (b);
14872 }
14873
14874 /* A callback for map_breakpoint_numbers that calls
14875 disable_breakpoint. */
14876
14877 static void
14878 do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
14879 {
14880 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
14881 }
14882
14883 static void
14884 disable_command (char *args, int from_tty)
14885 {
14886 if (args == 0)
14887 {
14888 struct breakpoint *bpt;
14889
14890 ALL_BREAKPOINTS (bpt)
14891 if (user_breakpoint_p (bpt))
14892 disable_breakpoint (bpt);
14893 }
14894 else
14895 {
14896 char *num = extract_arg (&args);
14897
14898 while (num)
14899 {
14900 if (strchr (num, '.'))
14901 {
14902 struct bp_location *loc = find_location_by_number (num);
14903
14904 if (loc)
14905 {
14906 if (loc->enabled)
14907 {
14908 loc->enabled = 0;
14909 mark_breakpoint_location_modified (loc);
14910 }
14911 if (target_supports_enable_disable_tracepoint ()
14912 && current_trace_status ()->running && loc->owner
14913 && is_tracepoint (loc->owner))
14914 target_disable_tracepoint (loc);
14915 }
14916 update_global_location_list (UGLL_DONT_INSERT);
14917 }
14918 else
14919 map_breakpoint_numbers (num, do_map_disable_breakpoint, NULL);
14920 num = extract_arg (&args);
14921 }
14922 }
14923 }
14924
14925 static void
14926 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14927 int count)
14928 {
14929 int target_resources_ok;
14930
14931 if (bpt->type == bp_hardware_breakpoint)
14932 {
14933 int i;
14934 i = hw_breakpoint_used_count ();
14935 target_resources_ok =
14936 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
14937 i + 1, 0);
14938 if (target_resources_ok == 0)
14939 error (_("No hardware breakpoint support in the target."));
14940 else if (target_resources_ok < 0)
14941 error (_("Hardware breakpoints used exceeds limit."));
14942 }
14943
14944 if (is_watchpoint (bpt))
14945 {
14946 /* Initialize it just to avoid a GCC false warning. */
14947 enum enable_state orig_enable_state = bp_disabled;
14948
14949 TRY
14950 {
14951 struct watchpoint *w = (struct watchpoint *) bpt;
14952
14953 orig_enable_state = bpt->enable_state;
14954 bpt->enable_state = bp_enabled;
14955 update_watchpoint (w, 1 /* reparse */);
14956 }
14957 CATCH (e, RETURN_MASK_ALL)
14958 {
14959 bpt->enable_state = orig_enable_state;
14960 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14961 bpt->number);
14962 return;
14963 }
14964 END_CATCH
14965 }
14966
14967 bpt->enable_state = bp_enabled;
14968
14969 /* Mark breakpoint locations modified. */
14970 mark_breakpoint_modified (bpt);
14971
14972 if (target_supports_enable_disable_tracepoint ()
14973 && current_trace_status ()->running && is_tracepoint (bpt))
14974 {
14975 struct bp_location *location;
14976
14977 for (location = bpt->loc; location; location = location->next)
14978 target_enable_tracepoint (location);
14979 }
14980
14981 bpt->disposition = disposition;
14982 bpt->enable_count = count;
14983 update_global_location_list (UGLL_MAY_INSERT);
14984
14985 observer_notify_breakpoint_modified (bpt);
14986 }
14987
14988
14989 void
14990 enable_breakpoint (struct breakpoint *bpt)
14991 {
14992 enable_breakpoint_disp (bpt, bpt->disposition, 0);
14993 }
14994
14995 static void
14996 do_enable_breakpoint (struct breakpoint *bpt, void *arg)
14997 {
14998 enable_breakpoint (bpt);
14999 }
15000
15001 /* A callback for map_breakpoint_numbers that calls
15002 enable_breakpoint. */
15003
15004 static void
15005 do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
15006 {
15007 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
15008 }
15009
15010 /* The enable command enables the specified breakpoints (or all defined
15011 breakpoints) so they once again become (or continue to be) effective
15012 in stopping the inferior. */
15013
15014 static void
15015 enable_command (char *args, int from_tty)
15016 {
15017 if (args == 0)
15018 {
15019 struct breakpoint *bpt;
15020
15021 ALL_BREAKPOINTS (bpt)
15022 if (user_breakpoint_p (bpt))
15023 enable_breakpoint (bpt);
15024 }
15025 else
15026 {
15027 char *num = extract_arg (&args);
15028
15029 while (num)
15030 {
15031 if (strchr (num, '.'))
15032 {
15033 struct bp_location *loc = find_location_by_number (num);
15034
15035 if (loc)
15036 {
15037 if (!loc->enabled)
15038 {
15039 loc->enabled = 1;
15040 mark_breakpoint_location_modified (loc);
15041 }
15042 if (target_supports_enable_disable_tracepoint ()
15043 && current_trace_status ()->running && loc->owner
15044 && is_tracepoint (loc->owner))
15045 target_enable_tracepoint (loc);
15046 }
15047 update_global_location_list (UGLL_MAY_INSERT);
15048 }
15049 else
15050 map_breakpoint_numbers (num, do_map_enable_breakpoint, NULL);
15051 num = extract_arg (&args);
15052 }
15053 }
15054 }
15055
15056 /* This struct packages up disposition data for application to multiple
15057 breakpoints. */
15058
15059 struct disp_data
15060 {
15061 enum bpdisp disp;
15062 int count;
15063 };
15064
15065 static void
15066 do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
15067 {
15068 struct disp_data disp_data = *(struct disp_data *) arg;
15069
15070 enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
15071 }
15072
15073 static void
15074 do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
15075 {
15076 struct disp_data disp = { disp_disable, 1 };
15077
15078 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
15079 }
15080
15081 static void
15082 enable_once_command (char *args, int from_tty)
15083 {
15084 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
15085 }
15086
15087 static void
15088 do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
15089 {
15090 struct disp_data disp = { disp_disable, *(int *) countptr };
15091
15092 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
15093 }
15094
15095 static void
15096 enable_count_command (char *args, int from_tty)
15097 {
15098 int count;
15099
15100 if (args == NULL)
15101 error_no_arg (_("hit count"));
15102
15103 count = get_number (&args);
15104
15105 map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
15106 }
15107
15108 static void
15109 do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
15110 {
15111 struct disp_data disp = { disp_del, 1 };
15112
15113 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
15114 }
15115
15116 static void
15117 enable_delete_command (char *args, int from_tty)
15118 {
15119 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
15120 }
15121 \f
15122 static void
15123 set_breakpoint_cmd (char *args, int from_tty)
15124 {
15125 }
15126
15127 static void
15128 show_breakpoint_cmd (char *args, int from_tty)
15129 {
15130 }
15131
15132 /* Invalidate last known value of any hardware watchpoint if
15133 the memory which that value represents has been written to by
15134 GDB itself. */
15135
15136 static void
15137 invalidate_bp_value_on_memory_change (struct inferior *inferior,
15138 CORE_ADDR addr, ssize_t len,
15139 const bfd_byte *data)
15140 {
15141 struct breakpoint *bp;
15142
15143 ALL_BREAKPOINTS (bp)
15144 if (bp->enable_state == bp_enabled
15145 && bp->type == bp_hardware_watchpoint)
15146 {
15147 struct watchpoint *wp = (struct watchpoint *) bp;
15148
15149 if (wp->val_valid && wp->val)
15150 {
15151 struct bp_location *loc;
15152
15153 for (loc = bp->loc; loc != NULL; loc = loc->next)
15154 if (loc->loc_type == bp_loc_hardware_watchpoint
15155 && loc->address + loc->length > addr
15156 && addr + len > loc->address)
15157 {
15158 value_free (wp->val);
15159 wp->val = NULL;
15160 wp->val_valid = 0;
15161 }
15162 }
15163 }
15164 }
15165
15166 /* Create and insert a breakpoint for software single step. */
15167
15168 void
15169 insert_single_step_breakpoint (struct gdbarch *gdbarch,
15170 struct address_space *aspace,
15171 CORE_ADDR next_pc)
15172 {
15173 struct thread_info *tp = inferior_thread ();
15174 struct symtab_and_line sal;
15175 CORE_ADDR pc = next_pc;
15176
15177 if (tp->control.single_step_breakpoints == NULL)
15178 {
15179 tp->control.single_step_breakpoints
15180 = new_single_step_breakpoint (tp->global_num, gdbarch);
15181 }
15182
15183 sal = find_pc_line (pc, 0);
15184 sal.pc = pc;
15185 sal.section = find_pc_overlay (pc);
15186 sal.explicit_pc = 1;
15187 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
15188
15189 update_global_location_list (UGLL_INSERT);
15190 }
15191
15192 /* Insert single step breakpoints according to the current state. */
15193
15194 int
15195 insert_single_step_breakpoints (struct gdbarch *gdbarch)
15196 {
15197 struct frame_info *frame = get_current_frame ();
15198 VEC (CORE_ADDR) * next_pcs;
15199
15200 next_pcs = gdbarch_software_single_step (gdbarch, frame);
15201
15202 if (next_pcs != NULL)
15203 {
15204 int i;
15205 CORE_ADDR pc;
15206 struct address_space *aspace = get_frame_address_space (frame);
15207
15208 for (i = 0; VEC_iterate (CORE_ADDR, next_pcs, i, pc); i++)
15209 insert_single_step_breakpoint (gdbarch, aspace, pc);
15210
15211 VEC_free (CORE_ADDR, next_pcs);
15212
15213 return 1;
15214 }
15215 else
15216 return 0;
15217 }
15218
15219 /* See breakpoint.h. */
15220
15221 int
15222 breakpoint_has_location_inserted_here (struct breakpoint *bp,
15223 struct address_space *aspace,
15224 CORE_ADDR pc)
15225 {
15226 struct bp_location *loc;
15227
15228 for (loc = bp->loc; loc != NULL; loc = loc->next)
15229 if (loc->inserted
15230 && breakpoint_location_address_match (loc, aspace, pc))
15231 return 1;
15232
15233 return 0;
15234 }
15235
15236 /* Check whether a software single-step breakpoint is inserted at
15237 PC. */
15238
15239 int
15240 single_step_breakpoint_inserted_here_p (struct address_space *aspace,
15241 CORE_ADDR pc)
15242 {
15243 struct breakpoint *bpt;
15244
15245 ALL_BREAKPOINTS (bpt)
15246 {
15247 if (bpt->type == bp_single_step
15248 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
15249 return 1;
15250 }
15251 return 0;
15252 }
15253
15254 /* Tracepoint-specific operations. */
15255
15256 /* Set tracepoint count to NUM. */
15257 static void
15258 set_tracepoint_count (int num)
15259 {
15260 tracepoint_count = num;
15261 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
15262 }
15263
15264 static void
15265 trace_command (char *arg, int from_tty)
15266 {
15267 struct breakpoint_ops *ops;
15268 struct event_location *location;
15269 struct cleanup *back_to;
15270
15271 location = string_to_event_location (&arg, current_language);
15272 back_to = make_cleanup_delete_event_location (location);
15273 if (location != NULL
15274 && event_location_type (location) == PROBE_LOCATION)
15275 ops = &tracepoint_probe_breakpoint_ops;
15276 else
15277 ops = &tracepoint_breakpoint_ops;
15278
15279 create_breakpoint (get_current_arch (),
15280 location,
15281 NULL, 0, arg, 1 /* parse arg */,
15282 0 /* tempflag */,
15283 bp_tracepoint /* type_wanted */,
15284 0 /* Ignore count */,
15285 pending_break_support,
15286 ops,
15287 from_tty,
15288 1 /* enabled */,
15289 0 /* internal */, 0);
15290 do_cleanups (back_to);
15291 }
15292
15293 static void
15294 ftrace_command (char *arg, int from_tty)
15295 {
15296 struct event_location *location;
15297 struct cleanup *back_to;
15298
15299 location = string_to_event_location (&arg, current_language);
15300 back_to = make_cleanup_delete_event_location (location);
15301 create_breakpoint (get_current_arch (),
15302 location,
15303 NULL, 0, arg, 1 /* parse arg */,
15304 0 /* tempflag */,
15305 bp_fast_tracepoint /* type_wanted */,
15306 0 /* Ignore count */,
15307 pending_break_support,
15308 &tracepoint_breakpoint_ops,
15309 from_tty,
15310 1 /* enabled */,
15311 0 /* internal */, 0);
15312 do_cleanups (back_to);
15313 }
15314
15315 /* strace command implementation. Creates a static tracepoint. */
15316
15317 static void
15318 strace_command (char *arg, int from_tty)
15319 {
15320 struct breakpoint_ops *ops;
15321 struct event_location *location;
15322 struct cleanup *back_to;
15323
15324 /* Decide if we are dealing with a static tracepoint marker (`-m'),
15325 or with a normal static tracepoint. */
15326 if (arg && startswith (arg, "-m") && isspace (arg[2]))
15327 {
15328 ops = &strace_marker_breakpoint_ops;
15329 location = new_linespec_location (&arg);
15330 }
15331 else
15332 {
15333 ops = &tracepoint_breakpoint_ops;
15334 location = string_to_event_location (&arg, current_language);
15335 }
15336
15337 back_to = make_cleanup_delete_event_location (location);
15338 create_breakpoint (get_current_arch (),
15339 location,
15340 NULL, 0, arg, 1 /* parse arg */,
15341 0 /* tempflag */,
15342 bp_static_tracepoint /* type_wanted */,
15343 0 /* Ignore count */,
15344 pending_break_support,
15345 ops,
15346 from_tty,
15347 1 /* enabled */,
15348 0 /* internal */, 0);
15349 do_cleanups (back_to);
15350 }
15351
15352 /* Set up a fake reader function that gets command lines from a linked
15353 list that was acquired during tracepoint uploading. */
15354
15355 static struct uploaded_tp *this_utp;
15356 static int next_cmd;
15357
15358 static char *
15359 read_uploaded_action (void)
15360 {
15361 char *rslt;
15362
15363 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
15364
15365 next_cmd++;
15366
15367 return rslt;
15368 }
15369
15370 /* Given information about a tracepoint as recorded on a target (which
15371 can be either a live system or a trace file), attempt to create an
15372 equivalent GDB tracepoint. This is not a reliable process, since
15373 the target does not necessarily have all the information used when
15374 the tracepoint was originally defined. */
15375
15376 struct tracepoint *
15377 create_tracepoint_from_upload (struct uploaded_tp *utp)
15378 {
15379 char *addr_str, small_buf[100];
15380 struct tracepoint *tp;
15381 struct event_location *location;
15382 struct cleanup *cleanup;
15383
15384 if (utp->at_string)
15385 addr_str = utp->at_string;
15386 else
15387 {
15388 /* In the absence of a source location, fall back to raw
15389 address. Since there is no way to confirm that the address
15390 means the same thing as when the trace was started, warn the
15391 user. */
15392 warning (_("Uploaded tracepoint %d has no "
15393 "source location, using raw address"),
15394 utp->number);
15395 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
15396 addr_str = small_buf;
15397 }
15398
15399 /* There's not much we can do with a sequence of bytecodes. */
15400 if (utp->cond && !utp->cond_string)
15401 warning (_("Uploaded tracepoint %d condition "
15402 "has no source form, ignoring it"),
15403 utp->number);
15404
15405 location = string_to_event_location (&addr_str, current_language);
15406 cleanup = make_cleanup_delete_event_location (location);
15407 if (!create_breakpoint (get_current_arch (),
15408 location,
15409 utp->cond_string, -1, addr_str,
15410 0 /* parse cond/thread */,
15411 0 /* tempflag */,
15412 utp->type /* type_wanted */,
15413 0 /* Ignore count */,
15414 pending_break_support,
15415 &tracepoint_breakpoint_ops,
15416 0 /* from_tty */,
15417 utp->enabled /* enabled */,
15418 0 /* internal */,
15419 CREATE_BREAKPOINT_FLAGS_INSERTED))
15420 {
15421 do_cleanups (cleanup);
15422 return NULL;
15423 }
15424
15425 do_cleanups (cleanup);
15426
15427 /* Get the tracepoint we just created. */
15428 tp = get_tracepoint (tracepoint_count);
15429 gdb_assert (tp != NULL);
15430
15431 if (utp->pass > 0)
15432 {
15433 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
15434 tp->base.number);
15435
15436 trace_pass_command (small_buf, 0);
15437 }
15438
15439 /* If we have uploaded versions of the original commands, set up a
15440 special-purpose "reader" function and call the usual command line
15441 reader, then pass the result to the breakpoint command-setting
15442 function. */
15443 if (!VEC_empty (char_ptr, utp->cmd_strings))
15444 {
15445 struct command_line *cmd_list;
15446
15447 this_utp = utp;
15448 next_cmd = 0;
15449
15450 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
15451
15452 breakpoint_set_commands (&tp->base, cmd_list);
15453 }
15454 else if (!VEC_empty (char_ptr, utp->actions)
15455 || !VEC_empty (char_ptr, utp->step_actions))
15456 warning (_("Uploaded tracepoint %d actions "
15457 "have no source form, ignoring them"),
15458 utp->number);
15459
15460 /* Copy any status information that might be available. */
15461 tp->base.hit_count = utp->hit_count;
15462 tp->traceframe_usage = utp->traceframe_usage;
15463
15464 return tp;
15465 }
15466
15467 /* Print information on tracepoint number TPNUM_EXP, or all if
15468 omitted. */
15469
15470 static void
15471 tracepoints_info (char *args, int from_tty)
15472 {
15473 struct ui_out *uiout = current_uiout;
15474 int num_printed;
15475
15476 num_printed = breakpoint_1 (args, 0, is_tracepoint);
15477
15478 if (num_printed == 0)
15479 {
15480 if (args == NULL || *args == '\0')
15481 ui_out_message (uiout, 0, "No tracepoints.\n");
15482 else
15483 ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
15484 }
15485
15486 default_collect_info ();
15487 }
15488
15489 /* The 'enable trace' command enables tracepoints.
15490 Not supported by all targets. */
15491 static void
15492 enable_trace_command (char *args, int from_tty)
15493 {
15494 enable_command (args, from_tty);
15495 }
15496
15497 /* The 'disable trace' command disables tracepoints.
15498 Not supported by all targets. */
15499 static void
15500 disable_trace_command (char *args, int from_tty)
15501 {
15502 disable_command (args, from_tty);
15503 }
15504
15505 /* Remove a tracepoint (or all if no argument). */
15506 static void
15507 delete_trace_command (char *arg, int from_tty)
15508 {
15509 struct breakpoint *b, *b_tmp;
15510
15511 dont_repeat ();
15512
15513 if (arg == 0)
15514 {
15515 int breaks_to_delete = 0;
15516
15517 /* Delete all breakpoints if no argument.
15518 Do not delete internal or call-dummy breakpoints, these
15519 have to be deleted with an explicit breakpoint number
15520 argument. */
15521 ALL_TRACEPOINTS (b)
15522 if (is_tracepoint (b) && user_breakpoint_p (b))
15523 {
15524 breaks_to_delete = 1;
15525 break;
15526 }
15527
15528 /* Ask user only if there are some breakpoints to delete. */
15529 if (!from_tty
15530 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
15531 {
15532 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15533 if (is_tracepoint (b) && user_breakpoint_p (b))
15534 delete_breakpoint (b);
15535 }
15536 }
15537 else
15538 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
15539 }
15540
15541 /* Helper function for trace_pass_command. */
15542
15543 static void
15544 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
15545 {
15546 tp->pass_count = count;
15547 observer_notify_breakpoint_modified (&tp->base);
15548 if (from_tty)
15549 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
15550 tp->base.number, count);
15551 }
15552
15553 /* Set passcount for tracepoint.
15554
15555 First command argument is passcount, second is tracepoint number.
15556 If tracepoint number omitted, apply to most recently defined.
15557 Also accepts special argument "all". */
15558
15559 static void
15560 trace_pass_command (char *args, int from_tty)
15561 {
15562 struct tracepoint *t1;
15563 unsigned int count;
15564
15565 if (args == 0 || *args == 0)
15566 error (_("passcount command requires an "
15567 "argument (count + optional TP num)"));
15568
15569 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
15570
15571 args = skip_spaces (args);
15572 if (*args && strncasecmp (args, "all", 3) == 0)
15573 {
15574 struct breakpoint *b;
15575
15576 args += 3; /* Skip special argument "all". */
15577 if (*args)
15578 error (_("Junk at end of arguments."));
15579
15580 ALL_TRACEPOINTS (b)
15581 {
15582 t1 = (struct tracepoint *) b;
15583 trace_pass_set_count (t1, count, from_tty);
15584 }
15585 }
15586 else if (*args == '\0')
15587 {
15588 t1 = get_tracepoint_by_number (&args, NULL);
15589 if (t1)
15590 trace_pass_set_count (t1, count, from_tty);
15591 }
15592 else
15593 {
15594 number_or_range_parser parser (args);
15595 while (!parser.finished ())
15596 {
15597 t1 = get_tracepoint_by_number (&args, &parser);
15598 if (t1)
15599 trace_pass_set_count (t1, count, from_tty);
15600 }
15601 }
15602 }
15603
15604 struct tracepoint *
15605 get_tracepoint (int num)
15606 {
15607 struct breakpoint *t;
15608
15609 ALL_TRACEPOINTS (t)
15610 if (t->number == num)
15611 return (struct tracepoint *) t;
15612
15613 return NULL;
15614 }
15615
15616 /* Find the tracepoint with the given target-side number (which may be
15617 different from the tracepoint number after disconnecting and
15618 reconnecting). */
15619
15620 struct tracepoint *
15621 get_tracepoint_by_number_on_target (int num)
15622 {
15623 struct breakpoint *b;
15624
15625 ALL_TRACEPOINTS (b)
15626 {
15627 struct tracepoint *t = (struct tracepoint *) b;
15628
15629 if (t->number_on_target == num)
15630 return t;
15631 }
15632
15633 return NULL;
15634 }
15635
15636 /* Utility: parse a tracepoint number and look it up in the list.
15637 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
15638 If the argument is missing, the most recent tracepoint
15639 (tracepoint_count) is returned. */
15640
15641 struct tracepoint *
15642 get_tracepoint_by_number (char **arg,
15643 number_or_range_parser *parser)
15644 {
15645 struct breakpoint *t;
15646 int tpnum;
15647 char *instring = arg == NULL ? NULL : *arg;
15648
15649 if (parser != NULL)
15650 {
15651 gdb_assert (!parser->finished ());
15652 tpnum = parser->get_number ();
15653 }
15654 else if (arg == NULL || *arg == NULL || ! **arg)
15655 tpnum = tracepoint_count;
15656 else
15657 tpnum = get_number (arg);
15658
15659 if (tpnum <= 0)
15660 {
15661 if (instring && *instring)
15662 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15663 instring);
15664 else
15665 printf_filtered (_("No previous tracepoint\n"));
15666 return NULL;
15667 }
15668
15669 ALL_TRACEPOINTS (t)
15670 if (t->number == tpnum)
15671 {
15672 return (struct tracepoint *) t;
15673 }
15674
15675 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15676 return NULL;
15677 }
15678
15679 void
15680 print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15681 {
15682 if (b->thread != -1)
15683 fprintf_unfiltered (fp, " thread %d", b->thread);
15684
15685 if (b->task != 0)
15686 fprintf_unfiltered (fp, " task %d", b->task);
15687
15688 fprintf_unfiltered (fp, "\n");
15689 }
15690
15691 /* Save information on user settable breakpoints (watchpoints, etc) to
15692 a new script file named FILENAME. If FILTER is non-NULL, call it
15693 on each breakpoint and only include the ones for which it returns
15694 non-zero. */
15695
15696 static void
15697 save_breakpoints (char *filename, int from_tty,
15698 int (*filter) (const struct breakpoint *))
15699 {
15700 struct breakpoint *tp;
15701 int any = 0;
15702 struct cleanup *cleanup;
15703 struct ui_file *fp;
15704 int extra_trace_bits = 0;
15705
15706 if (filename == 0 || *filename == 0)
15707 error (_("Argument required (file name in which to save)"));
15708
15709 /* See if we have anything to save. */
15710 ALL_BREAKPOINTS (tp)
15711 {
15712 /* Skip internal and momentary breakpoints. */
15713 if (!user_breakpoint_p (tp))
15714 continue;
15715
15716 /* If we have a filter, only save the breakpoints it accepts. */
15717 if (filter && !filter (tp))
15718 continue;
15719
15720 any = 1;
15721
15722 if (is_tracepoint (tp))
15723 {
15724 extra_trace_bits = 1;
15725
15726 /* We can stop searching. */
15727 break;
15728 }
15729 }
15730
15731 if (!any)
15732 {
15733 warning (_("Nothing to save."));
15734 return;
15735 }
15736
15737 filename = tilde_expand (filename);
15738 cleanup = make_cleanup (xfree, filename);
15739 fp = gdb_fopen (filename, "w");
15740 if (!fp)
15741 error (_("Unable to open file '%s' for saving (%s)"),
15742 filename, safe_strerror (errno));
15743 make_cleanup_ui_file_delete (fp);
15744
15745 if (extra_trace_bits)
15746 save_trace_state_variables (fp);
15747
15748 ALL_BREAKPOINTS (tp)
15749 {
15750 /* Skip internal and momentary breakpoints. */
15751 if (!user_breakpoint_p (tp))
15752 continue;
15753
15754 /* If we have a filter, only save the breakpoints it accepts. */
15755 if (filter && !filter (tp))
15756 continue;
15757
15758 tp->ops->print_recreate (tp, fp);
15759
15760 /* Note, we can't rely on tp->number for anything, as we can't
15761 assume the recreated breakpoint numbers will match. Use $bpnum
15762 instead. */
15763
15764 if (tp->cond_string)
15765 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
15766
15767 if (tp->ignore_count)
15768 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
15769
15770 if (tp->type != bp_dprintf && tp->commands)
15771 {
15772 fprintf_unfiltered (fp, " commands\n");
15773
15774 ui_out_redirect (current_uiout, fp);
15775 TRY
15776 {
15777 print_command_lines (current_uiout, tp->commands->commands, 2);
15778 }
15779 CATCH (ex, RETURN_MASK_ALL)
15780 {
15781 ui_out_redirect (current_uiout, NULL);
15782 throw_exception (ex);
15783 }
15784 END_CATCH
15785
15786 ui_out_redirect (current_uiout, NULL);
15787 fprintf_unfiltered (fp, " end\n");
15788 }
15789
15790 if (tp->enable_state == bp_disabled)
15791 fprintf_unfiltered (fp, "disable $bpnum\n");
15792
15793 /* If this is a multi-location breakpoint, check if the locations
15794 should be individually disabled. Watchpoint locations are
15795 special, and not user visible. */
15796 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15797 {
15798 struct bp_location *loc;
15799 int n = 1;
15800
15801 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15802 if (!loc->enabled)
15803 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
15804 }
15805 }
15806
15807 if (extra_trace_bits && *default_collect)
15808 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
15809
15810 if (from_tty)
15811 printf_filtered (_("Saved to file '%s'.\n"), filename);
15812 do_cleanups (cleanup);
15813 }
15814
15815 /* The `save breakpoints' command. */
15816
15817 static void
15818 save_breakpoints_command (char *args, int from_tty)
15819 {
15820 save_breakpoints (args, from_tty, NULL);
15821 }
15822
15823 /* The `save tracepoints' command. */
15824
15825 static void
15826 save_tracepoints_command (char *args, int from_tty)
15827 {
15828 save_breakpoints (args, from_tty, is_tracepoint);
15829 }
15830
15831 /* Create a vector of all tracepoints. */
15832
15833 VEC(breakpoint_p) *
15834 all_tracepoints (void)
15835 {
15836 VEC(breakpoint_p) *tp_vec = 0;
15837 struct breakpoint *tp;
15838
15839 ALL_TRACEPOINTS (tp)
15840 {
15841 VEC_safe_push (breakpoint_p, tp_vec, tp);
15842 }
15843
15844 return tp_vec;
15845 }
15846
15847 \f
15848 /* This help string is used to consolidate all the help string for specifying
15849 locations used by several commands. */
15850
15851 #define LOCATION_HELP_STRING \
15852 "Linespecs are colon-separated lists of location parameters, such as\n\
15853 source filename, function name, label name, and line number.\n\
15854 Example: To specify the start of a label named \"the_top\" in the\n\
15855 function \"fact\" in the file \"factorial.c\", use\n\
15856 \"factorial.c:fact:the_top\".\n\
15857 \n\
15858 Address locations begin with \"*\" and specify an exact address in the\n\
15859 program. Example: To specify the fourth byte past the start function\n\
15860 \"main\", use \"*main + 4\".\n\
15861 \n\
15862 Explicit locations are similar to linespecs but use an option/argument\n\
15863 syntax to specify location parameters.\n\
15864 Example: To specify the start of the label named \"the_top\" in the\n\
15865 function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
15866 -function fact -label the_top\".\n"
15867
15868 /* This help string is used for the break, hbreak, tbreak and thbreak
15869 commands. It is defined as a macro to prevent duplication.
15870 COMMAND should be a string constant containing the name of the
15871 command. */
15872
15873 #define BREAK_ARGS_HELP(command) \
15874 command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15875 PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15876 probe point. Accepted values are `-probe' (for a generic, automatically\n\
15877 guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15878 `-probe-dtrace' (for a DTrace probe).\n\
15879 LOCATION may be a linespec, address, or explicit location as described\n\
15880 below.\n\
15881 \n\
15882 With no LOCATION, uses current execution address of the selected\n\
15883 stack frame. This is useful for breaking on return to a stack frame.\n\
15884 \n\
15885 THREADNUM is the number from \"info threads\".\n\
15886 CONDITION is a boolean expression.\n\
15887 \n" LOCATION_HELP_STRING "\n\
15888 Multiple breakpoints at one place are permitted, and useful if their\n\
15889 conditions are different.\n\
15890 \n\
15891 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15892
15893 /* List of subcommands for "catch". */
15894 static struct cmd_list_element *catch_cmdlist;
15895
15896 /* List of subcommands for "tcatch". */
15897 static struct cmd_list_element *tcatch_cmdlist;
15898
15899 void
15900 add_catch_command (char *name, char *docstring,
15901 cmd_sfunc_ftype *sfunc,
15902 completer_ftype *completer,
15903 void *user_data_catch,
15904 void *user_data_tcatch)
15905 {
15906 struct cmd_list_element *command;
15907
15908 command = add_cmd (name, class_breakpoint, NULL, docstring,
15909 &catch_cmdlist);
15910 set_cmd_sfunc (command, sfunc);
15911 set_cmd_context (command, user_data_catch);
15912 set_cmd_completer (command, completer);
15913
15914 command = add_cmd (name, class_breakpoint, NULL, docstring,
15915 &tcatch_cmdlist);
15916 set_cmd_sfunc (command, sfunc);
15917 set_cmd_context (command, user_data_tcatch);
15918 set_cmd_completer (command, completer);
15919 }
15920
15921 static void
15922 save_command (char *arg, int from_tty)
15923 {
15924 printf_unfiltered (_("\"save\" must be followed by "
15925 "the name of a save subcommand.\n"));
15926 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
15927 }
15928
15929 struct breakpoint *
15930 iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15931 void *data)
15932 {
15933 struct breakpoint *b, *b_tmp;
15934
15935 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15936 {
15937 if ((*callback) (b, data))
15938 return b;
15939 }
15940
15941 return NULL;
15942 }
15943
15944 /* Zero if any of the breakpoint's locations could be a location where
15945 functions have been inlined, nonzero otherwise. */
15946
15947 static int
15948 is_non_inline_function (struct breakpoint *b)
15949 {
15950 /* The shared library event breakpoint is set on the address of a
15951 non-inline function. */
15952 if (b->type == bp_shlib_event)
15953 return 1;
15954
15955 return 0;
15956 }
15957
15958 /* Nonzero if the specified PC cannot be a location where functions
15959 have been inlined. */
15960
15961 int
15962 pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
15963 const struct target_waitstatus *ws)
15964 {
15965 struct breakpoint *b;
15966 struct bp_location *bl;
15967
15968 ALL_BREAKPOINTS (b)
15969 {
15970 if (!is_non_inline_function (b))
15971 continue;
15972
15973 for (bl = b->loc; bl != NULL; bl = bl->next)
15974 {
15975 if (!bl->shlib_disabled
15976 && bpstat_check_location (bl, aspace, pc, ws))
15977 return 1;
15978 }
15979 }
15980
15981 return 0;
15982 }
15983
15984 /* Remove any references to OBJFILE which is going to be freed. */
15985
15986 void
15987 breakpoint_free_objfile (struct objfile *objfile)
15988 {
15989 struct bp_location **locp, *loc;
15990
15991 ALL_BP_LOCATIONS (loc, locp)
15992 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
15993 loc->symtab = NULL;
15994 }
15995
15996 void
15997 initialize_breakpoint_ops (void)
15998 {
15999 static int initialized = 0;
16000
16001 struct breakpoint_ops *ops;
16002
16003 if (initialized)
16004 return;
16005 initialized = 1;
16006
16007 /* The breakpoint_ops structure to be inherit by all kinds of
16008 breakpoints (real breakpoints, i.e., user "break" breakpoints,
16009 internal and momentary breakpoints, etc.). */
16010 ops = &bkpt_base_breakpoint_ops;
16011 *ops = base_breakpoint_ops;
16012 ops->re_set = bkpt_re_set;
16013 ops->insert_location = bkpt_insert_location;
16014 ops->remove_location = bkpt_remove_location;
16015 ops->breakpoint_hit = bkpt_breakpoint_hit;
16016 ops->create_sals_from_location = bkpt_create_sals_from_location;
16017 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
16018 ops->decode_location = bkpt_decode_location;
16019
16020 /* The breakpoint_ops structure to be used in regular breakpoints. */
16021 ops = &bkpt_breakpoint_ops;
16022 *ops = bkpt_base_breakpoint_ops;
16023 ops->re_set = bkpt_re_set;
16024 ops->resources_needed = bkpt_resources_needed;
16025 ops->print_it = bkpt_print_it;
16026 ops->print_mention = bkpt_print_mention;
16027 ops->print_recreate = bkpt_print_recreate;
16028
16029 /* Ranged breakpoints. */
16030 ops = &ranged_breakpoint_ops;
16031 *ops = bkpt_breakpoint_ops;
16032 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
16033 ops->resources_needed = resources_needed_ranged_breakpoint;
16034 ops->print_it = print_it_ranged_breakpoint;
16035 ops->print_one = print_one_ranged_breakpoint;
16036 ops->print_one_detail = print_one_detail_ranged_breakpoint;
16037 ops->print_mention = print_mention_ranged_breakpoint;
16038 ops->print_recreate = print_recreate_ranged_breakpoint;
16039
16040 /* Internal breakpoints. */
16041 ops = &internal_breakpoint_ops;
16042 *ops = bkpt_base_breakpoint_ops;
16043 ops->re_set = internal_bkpt_re_set;
16044 ops->check_status = internal_bkpt_check_status;
16045 ops->print_it = internal_bkpt_print_it;
16046 ops->print_mention = internal_bkpt_print_mention;
16047
16048 /* Momentary breakpoints. */
16049 ops = &momentary_breakpoint_ops;
16050 *ops = bkpt_base_breakpoint_ops;
16051 ops->re_set = momentary_bkpt_re_set;
16052 ops->check_status = momentary_bkpt_check_status;
16053 ops->print_it = momentary_bkpt_print_it;
16054 ops->print_mention = momentary_bkpt_print_mention;
16055
16056 /* Momentary breakpoints for bp_longjmp and bp_exception. */
16057 ops = &longjmp_breakpoint_ops;
16058 *ops = momentary_breakpoint_ops;
16059 ops->dtor = longjmp_bkpt_dtor;
16060
16061 /* Probe breakpoints. */
16062 ops = &bkpt_probe_breakpoint_ops;
16063 *ops = bkpt_breakpoint_ops;
16064 ops->insert_location = bkpt_probe_insert_location;
16065 ops->remove_location = bkpt_probe_remove_location;
16066 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
16067 ops->decode_location = bkpt_probe_decode_location;
16068
16069 /* Watchpoints. */
16070 ops = &watchpoint_breakpoint_ops;
16071 *ops = base_breakpoint_ops;
16072 ops->dtor = dtor_watchpoint;
16073 ops->re_set = re_set_watchpoint;
16074 ops->insert_location = insert_watchpoint;
16075 ops->remove_location = remove_watchpoint;
16076 ops->breakpoint_hit = breakpoint_hit_watchpoint;
16077 ops->check_status = check_status_watchpoint;
16078 ops->resources_needed = resources_needed_watchpoint;
16079 ops->works_in_software_mode = works_in_software_mode_watchpoint;
16080 ops->print_it = print_it_watchpoint;
16081 ops->print_mention = print_mention_watchpoint;
16082 ops->print_recreate = print_recreate_watchpoint;
16083 ops->explains_signal = explains_signal_watchpoint;
16084
16085 /* Masked watchpoints. */
16086 ops = &masked_watchpoint_breakpoint_ops;
16087 *ops = watchpoint_breakpoint_ops;
16088 ops->insert_location = insert_masked_watchpoint;
16089 ops->remove_location = remove_masked_watchpoint;
16090 ops->resources_needed = resources_needed_masked_watchpoint;
16091 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
16092 ops->print_it = print_it_masked_watchpoint;
16093 ops->print_one_detail = print_one_detail_masked_watchpoint;
16094 ops->print_mention = print_mention_masked_watchpoint;
16095 ops->print_recreate = print_recreate_masked_watchpoint;
16096
16097 /* Tracepoints. */
16098 ops = &tracepoint_breakpoint_ops;
16099 *ops = base_breakpoint_ops;
16100 ops->re_set = tracepoint_re_set;
16101 ops->breakpoint_hit = tracepoint_breakpoint_hit;
16102 ops->print_one_detail = tracepoint_print_one_detail;
16103 ops->print_mention = tracepoint_print_mention;
16104 ops->print_recreate = tracepoint_print_recreate;
16105 ops->create_sals_from_location = tracepoint_create_sals_from_location;
16106 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
16107 ops->decode_location = tracepoint_decode_location;
16108
16109 /* Probe tracepoints. */
16110 ops = &tracepoint_probe_breakpoint_ops;
16111 *ops = tracepoint_breakpoint_ops;
16112 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
16113 ops->decode_location = tracepoint_probe_decode_location;
16114
16115 /* Static tracepoints with marker (`-m'). */
16116 ops = &strace_marker_breakpoint_ops;
16117 *ops = tracepoint_breakpoint_ops;
16118 ops->create_sals_from_location = strace_marker_create_sals_from_location;
16119 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
16120 ops->decode_location = strace_marker_decode_location;
16121
16122 /* Fork catchpoints. */
16123 ops = &catch_fork_breakpoint_ops;
16124 *ops = base_breakpoint_ops;
16125 ops->insert_location = insert_catch_fork;
16126 ops->remove_location = remove_catch_fork;
16127 ops->breakpoint_hit = breakpoint_hit_catch_fork;
16128 ops->print_it = print_it_catch_fork;
16129 ops->print_one = print_one_catch_fork;
16130 ops->print_mention = print_mention_catch_fork;
16131 ops->print_recreate = print_recreate_catch_fork;
16132
16133 /* Vfork catchpoints. */
16134 ops = &catch_vfork_breakpoint_ops;
16135 *ops = base_breakpoint_ops;
16136 ops->insert_location = insert_catch_vfork;
16137 ops->remove_location = remove_catch_vfork;
16138 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
16139 ops->print_it = print_it_catch_vfork;
16140 ops->print_one = print_one_catch_vfork;
16141 ops->print_mention = print_mention_catch_vfork;
16142 ops->print_recreate = print_recreate_catch_vfork;
16143
16144 /* Exec catchpoints. */
16145 ops = &catch_exec_breakpoint_ops;
16146 *ops = base_breakpoint_ops;
16147 ops->dtor = dtor_catch_exec;
16148 ops->insert_location = insert_catch_exec;
16149 ops->remove_location = remove_catch_exec;
16150 ops->breakpoint_hit = breakpoint_hit_catch_exec;
16151 ops->print_it = print_it_catch_exec;
16152 ops->print_one = print_one_catch_exec;
16153 ops->print_mention = print_mention_catch_exec;
16154 ops->print_recreate = print_recreate_catch_exec;
16155
16156 /* Solib-related catchpoints. */
16157 ops = &catch_solib_breakpoint_ops;
16158 *ops = base_breakpoint_ops;
16159 ops->dtor = dtor_catch_solib;
16160 ops->insert_location = insert_catch_solib;
16161 ops->remove_location = remove_catch_solib;
16162 ops->breakpoint_hit = breakpoint_hit_catch_solib;
16163 ops->check_status = check_status_catch_solib;
16164 ops->print_it = print_it_catch_solib;
16165 ops->print_one = print_one_catch_solib;
16166 ops->print_mention = print_mention_catch_solib;
16167 ops->print_recreate = print_recreate_catch_solib;
16168
16169 ops = &dprintf_breakpoint_ops;
16170 *ops = bkpt_base_breakpoint_ops;
16171 ops->re_set = dprintf_re_set;
16172 ops->resources_needed = bkpt_resources_needed;
16173 ops->print_it = bkpt_print_it;
16174 ops->print_mention = bkpt_print_mention;
16175 ops->print_recreate = dprintf_print_recreate;
16176 ops->after_condition_true = dprintf_after_condition_true;
16177 ops->breakpoint_hit = dprintf_breakpoint_hit;
16178 }
16179
16180 /* Chain containing all defined "enable breakpoint" subcommands. */
16181
16182 static struct cmd_list_element *enablebreaklist = NULL;
16183
16184 void
16185 _initialize_breakpoint (void)
16186 {
16187 struct cmd_list_element *c;
16188
16189 initialize_breakpoint_ops ();
16190
16191 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
16192 observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
16193 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
16194
16195 breakpoint_objfile_key
16196 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
16197
16198 breakpoint_chain = 0;
16199 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
16200 before a breakpoint is set. */
16201 breakpoint_count = 0;
16202
16203 tracepoint_count = 0;
16204
16205 add_com ("ignore", class_breakpoint, ignore_command, _("\
16206 Set ignore-count of breakpoint number N to COUNT.\n\
16207 Usage is `ignore N COUNT'."));
16208
16209 add_com ("commands", class_breakpoint, commands_command, _("\
16210 Set commands to be executed when a breakpoint is hit.\n\
16211 Give breakpoint number as argument after \"commands\".\n\
16212 With no argument, the targeted breakpoint is the last one set.\n\
16213 The commands themselves follow starting on the next line.\n\
16214 Type a line containing \"end\" to indicate the end of them.\n\
16215 Give \"silent\" as the first line to make the breakpoint silent;\n\
16216 then no output is printed when it is hit, except what the commands print."));
16217
16218 c = add_com ("condition", class_breakpoint, condition_command, _("\
16219 Specify breakpoint number N to break only if COND is true.\n\
16220 Usage is `condition N COND', where N is an integer and COND is an\n\
16221 expression to be evaluated whenever breakpoint N is reached."));
16222 set_cmd_completer (c, condition_completer);
16223
16224 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
16225 Set a temporary breakpoint.\n\
16226 Like \"break\" except the breakpoint is only temporary,\n\
16227 so it will be deleted when hit. Equivalent to \"break\" followed\n\
16228 by using \"enable delete\" on the breakpoint number.\n\
16229 \n"
16230 BREAK_ARGS_HELP ("tbreak")));
16231 set_cmd_completer (c, location_completer);
16232
16233 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
16234 Set a hardware assisted breakpoint.\n\
16235 Like \"break\" except the breakpoint requires hardware support,\n\
16236 some target hardware may not have this support.\n\
16237 \n"
16238 BREAK_ARGS_HELP ("hbreak")));
16239 set_cmd_completer (c, location_completer);
16240
16241 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
16242 Set a temporary hardware assisted breakpoint.\n\
16243 Like \"hbreak\" except the breakpoint is only temporary,\n\
16244 so it will be deleted when hit.\n\
16245 \n"
16246 BREAK_ARGS_HELP ("thbreak")));
16247 set_cmd_completer (c, location_completer);
16248
16249 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
16250 Enable some breakpoints.\n\
16251 Give breakpoint numbers (separated by spaces) as arguments.\n\
16252 With no subcommand, breakpoints are enabled until you command otherwise.\n\
16253 This is used to cancel the effect of the \"disable\" command.\n\
16254 With a subcommand you can enable temporarily."),
16255 &enablelist, "enable ", 1, &cmdlist);
16256
16257 add_com_alias ("en", "enable", class_breakpoint, 1);
16258
16259 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
16260 Enable some breakpoints.\n\
16261 Give breakpoint numbers (separated by spaces) as arguments.\n\
16262 This is used to cancel the effect of the \"disable\" command.\n\
16263 May be abbreviated to simply \"enable\".\n"),
16264 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
16265
16266 add_cmd ("once", no_class, enable_once_command, _("\
16267 Enable breakpoints for one hit. Give breakpoint numbers.\n\
16268 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
16269 &enablebreaklist);
16270
16271 add_cmd ("delete", no_class, enable_delete_command, _("\
16272 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16273 If a breakpoint is hit while enabled in this fashion, it is deleted."),
16274 &enablebreaklist);
16275
16276 add_cmd ("count", no_class, enable_count_command, _("\
16277 Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16278 If a breakpoint is hit while enabled in this fashion,\n\
16279 the count is decremented; when it reaches zero, the breakpoint is disabled."),
16280 &enablebreaklist);
16281
16282 add_cmd ("delete", no_class, enable_delete_command, _("\
16283 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16284 If a breakpoint is hit while enabled in this fashion, it is deleted."),
16285 &enablelist);
16286
16287 add_cmd ("once", no_class, enable_once_command, _("\
16288 Enable breakpoints for one hit. Give breakpoint numbers.\n\
16289 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
16290 &enablelist);
16291
16292 add_cmd ("count", no_class, enable_count_command, _("\
16293 Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16294 If a breakpoint is hit while enabled in this fashion,\n\
16295 the count is decremented; when it reaches zero, the breakpoint is disabled."),
16296 &enablelist);
16297
16298 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
16299 Disable some breakpoints.\n\
16300 Arguments are breakpoint numbers with spaces in between.\n\
16301 To disable all breakpoints, give no argument.\n\
16302 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
16303 &disablelist, "disable ", 1, &cmdlist);
16304 add_com_alias ("dis", "disable", class_breakpoint, 1);
16305 add_com_alias ("disa", "disable", class_breakpoint, 1);
16306
16307 add_cmd ("breakpoints", class_alias, disable_command, _("\
16308 Disable some breakpoints.\n\
16309 Arguments are breakpoint numbers with spaces in between.\n\
16310 To disable all breakpoints, give no argument.\n\
16311 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
16312 This command may be abbreviated \"disable\"."),
16313 &disablelist);
16314
16315 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
16316 Delete some breakpoints or auto-display expressions.\n\
16317 Arguments are breakpoint numbers with spaces in between.\n\
16318 To delete all breakpoints, give no argument.\n\
16319 \n\
16320 Also a prefix command for deletion of other GDB objects.\n\
16321 The \"unset\" command is also an alias for \"delete\"."),
16322 &deletelist, "delete ", 1, &cmdlist);
16323 add_com_alias ("d", "delete", class_breakpoint, 1);
16324 add_com_alias ("del", "delete", class_breakpoint, 1);
16325
16326 add_cmd ("breakpoints", class_alias, delete_command, _("\
16327 Delete some breakpoints or auto-display expressions.\n\
16328 Arguments are breakpoint numbers with spaces in between.\n\
16329 To delete all breakpoints, give no argument.\n\
16330 This command may be abbreviated \"delete\"."),
16331 &deletelist);
16332
16333 add_com ("clear", class_breakpoint, clear_command, _("\
16334 Clear breakpoint at specified location.\n\
16335 Argument may be a linespec, explicit, or address location as described below.\n\
16336 \n\
16337 With no argument, clears all breakpoints in the line that the selected frame\n\
16338 is executing in.\n"
16339 "\n" LOCATION_HELP_STRING "\n\
16340 See also the \"delete\" command which clears breakpoints by number."));
16341 add_com_alias ("cl", "clear", class_breakpoint, 1);
16342
16343 c = add_com ("break", class_breakpoint, break_command, _("\
16344 Set breakpoint at specified location.\n"
16345 BREAK_ARGS_HELP ("break")));
16346 set_cmd_completer (c, location_completer);
16347
16348 add_com_alias ("b", "break", class_run, 1);
16349 add_com_alias ("br", "break", class_run, 1);
16350 add_com_alias ("bre", "break", class_run, 1);
16351 add_com_alias ("brea", "break", class_run, 1);
16352
16353 if (dbx_commands)
16354 {
16355 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
16356 Break in function/address or break at a line in the current file."),
16357 &stoplist, "stop ", 1, &cmdlist);
16358 add_cmd ("in", class_breakpoint, stopin_command,
16359 _("Break in function or address."), &stoplist);
16360 add_cmd ("at", class_breakpoint, stopat_command,
16361 _("Break at a line in the current file."), &stoplist);
16362 add_com ("status", class_info, breakpoints_info, _("\
16363 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
16364 The \"Type\" column indicates one of:\n\
16365 \tbreakpoint - normal breakpoint\n\
16366 \twatchpoint - watchpoint\n\
16367 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16368 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16369 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16370 address and file/line number respectively.\n\
16371 \n\
16372 Convenience variable \"$_\" and default examine address for \"x\"\n\
16373 are set to the address of the last breakpoint listed unless the command\n\
16374 is prefixed with \"server \".\n\n\
16375 Convenience variable \"$bpnum\" contains the number of the last\n\
16376 breakpoint set."));
16377 }
16378
16379 add_info ("breakpoints", breakpoints_info, _("\
16380 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
16381 The \"Type\" column indicates one of:\n\
16382 \tbreakpoint - normal breakpoint\n\
16383 \twatchpoint - watchpoint\n\
16384 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16385 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16386 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16387 address and file/line number respectively.\n\
16388 \n\
16389 Convenience variable \"$_\" and default examine address for \"x\"\n\
16390 are set to the address of the last breakpoint listed unless the command\n\
16391 is prefixed with \"server \".\n\n\
16392 Convenience variable \"$bpnum\" contains the number of the last\n\
16393 breakpoint set."));
16394
16395 add_info_alias ("b", "breakpoints", 1);
16396
16397 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
16398 Status of all breakpoints, or breakpoint number NUMBER.\n\
16399 The \"Type\" column indicates one of:\n\
16400 \tbreakpoint - normal breakpoint\n\
16401 \twatchpoint - watchpoint\n\
16402 \tlongjmp - internal breakpoint used to step through longjmp()\n\
16403 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
16404 \tuntil - internal breakpoint used by the \"until\" command\n\
16405 \tfinish - internal breakpoint used by the \"finish\" command\n\
16406 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16407 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16408 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16409 address and file/line number respectively.\n\
16410 \n\
16411 Convenience variable \"$_\" and default examine address for \"x\"\n\
16412 are set to the address of the last breakpoint listed unless the command\n\
16413 is prefixed with \"server \".\n\n\
16414 Convenience variable \"$bpnum\" contains the number of the last\n\
16415 breakpoint set."),
16416 &maintenanceinfolist);
16417
16418 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
16419 Set catchpoints to catch events."),
16420 &catch_cmdlist, "catch ",
16421 0/*allow-unknown*/, &cmdlist);
16422
16423 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
16424 Set temporary catchpoints to catch events."),
16425 &tcatch_cmdlist, "tcatch ",
16426 0/*allow-unknown*/, &cmdlist);
16427
16428 add_catch_command ("fork", _("Catch calls to fork."),
16429 catch_fork_command_1,
16430 NULL,
16431 (void *) (uintptr_t) catch_fork_permanent,
16432 (void *) (uintptr_t) catch_fork_temporary);
16433 add_catch_command ("vfork", _("Catch calls to vfork."),
16434 catch_fork_command_1,
16435 NULL,
16436 (void *) (uintptr_t) catch_vfork_permanent,
16437 (void *) (uintptr_t) catch_vfork_temporary);
16438 add_catch_command ("exec", _("Catch calls to exec."),
16439 catch_exec_command_1,
16440 NULL,
16441 CATCH_PERMANENT,
16442 CATCH_TEMPORARY);
16443 add_catch_command ("load", _("Catch loads of shared libraries.\n\
16444 Usage: catch load [REGEX]\n\
16445 If REGEX is given, only stop for libraries matching the regular expression."),
16446 catch_load_command_1,
16447 NULL,
16448 CATCH_PERMANENT,
16449 CATCH_TEMPORARY);
16450 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
16451 Usage: catch unload [REGEX]\n\
16452 If REGEX is given, only stop for libraries matching the regular expression."),
16453 catch_unload_command_1,
16454 NULL,
16455 CATCH_PERMANENT,
16456 CATCH_TEMPORARY);
16457
16458 c = add_com ("watch", class_breakpoint, watch_command, _("\
16459 Set a watchpoint for an expression.\n\
16460 Usage: watch [-l|-location] EXPRESSION\n\
16461 A watchpoint stops execution of your program whenever the value of\n\
16462 an expression changes.\n\
16463 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16464 the memory to which it refers."));
16465 set_cmd_completer (c, expression_completer);
16466
16467 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
16468 Set a read watchpoint for an expression.\n\
16469 Usage: rwatch [-l|-location] EXPRESSION\n\
16470 A watchpoint stops execution of your program whenever the value of\n\
16471 an expression is read.\n\
16472 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16473 the memory to which it refers."));
16474 set_cmd_completer (c, expression_completer);
16475
16476 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
16477 Set a watchpoint for an expression.\n\
16478 Usage: awatch [-l|-location] EXPRESSION\n\
16479 A watchpoint stops execution of your program whenever the value of\n\
16480 an expression is either read or written.\n\
16481 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16482 the memory to which it refers."));
16483 set_cmd_completer (c, expression_completer);
16484
16485 add_info ("watchpoints", watchpoints_info, _("\
16486 Status of specified watchpoints (all watchpoints if no argument)."));
16487
16488 /* XXX: cagney/2005-02-23: This should be a boolean, and should
16489 respond to changes - contrary to the description. */
16490 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
16491 &can_use_hw_watchpoints, _("\
16492 Set debugger's willingness to use watchpoint hardware."), _("\
16493 Show debugger's willingness to use watchpoint hardware."), _("\
16494 If zero, gdb will not use hardware for new watchpoints, even if\n\
16495 such is available. (However, any hardware watchpoints that were\n\
16496 created before setting this to nonzero, will continue to use watchpoint\n\
16497 hardware.)"),
16498 NULL,
16499 show_can_use_hw_watchpoints,
16500 &setlist, &showlist);
16501
16502 can_use_hw_watchpoints = 1;
16503
16504 /* Tracepoint manipulation commands. */
16505
16506 c = add_com ("trace", class_breakpoint, trace_command, _("\
16507 Set a tracepoint at specified location.\n\
16508 \n"
16509 BREAK_ARGS_HELP ("trace") "\n\
16510 Do \"help tracepoints\" for info on other tracepoint commands."));
16511 set_cmd_completer (c, location_completer);
16512
16513 add_com_alias ("tp", "trace", class_alias, 0);
16514 add_com_alias ("tr", "trace", class_alias, 1);
16515 add_com_alias ("tra", "trace", class_alias, 1);
16516 add_com_alias ("trac", "trace", class_alias, 1);
16517
16518 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
16519 Set a fast tracepoint at specified location.\n\
16520 \n"
16521 BREAK_ARGS_HELP ("ftrace") "\n\
16522 Do \"help tracepoints\" for info on other tracepoint commands."));
16523 set_cmd_completer (c, location_completer);
16524
16525 c = add_com ("strace", class_breakpoint, strace_command, _("\
16526 Set a static tracepoint at location or marker.\n\
16527 \n\
16528 strace [LOCATION] [if CONDITION]\n\
16529 LOCATION may be a linespec, explicit, or address location (described below) \n\
16530 or -m MARKER_ID.\n\n\
16531 If a marker id is specified, probe the marker with that name. With\n\
16532 no LOCATION, uses current execution address of the selected stack frame.\n\
16533 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16534 This collects arbitrary user data passed in the probe point call to the\n\
16535 tracing library. You can inspect it when analyzing the trace buffer,\n\
16536 by printing the $_sdata variable like any other convenience variable.\n\
16537 \n\
16538 CONDITION is a boolean expression.\n\
16539 \n" LOCATION_HELP_STRING "\n\
16540 Multiple tracepoints at one place are permitted, and useful if their\n\
16541 conditions are different.\n\
16542 \n\
16543 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16544 Do \"help tracepoints\" for info on other tracepoint commands."));
16545 set_cmd_completer (c, location_completer);
16546
16547 add_info ("tracepoints", tracepoints_info, _("\
16548 Status of specified tracepoints (all tracepoints if no argument).\n\
16549 Convenience variable \"$tpnum\" contains the number of the\n\
16550 last tracepoint set."));
16551
16552 add_info_alias ("tp", "tracepoints", 1);
16553
16554 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16555 Delete specified tracepoints.\n\
16556 Arguments are tracepoint numbers, separated by spaces.\n\
16557 No argument means delete all tracepoints."),
16558 &deletelist);
16559 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
16560
16561 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16562 Disable specified tracepoints.\n\
16563 Arguments are tracepoint numbers, separated by spaces.\n\
16564 No argument means disable all tracepoints."),
16565 &disablelist);
16566 deprecate_cmd (c, "disable");
16567
16568 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16569 Enable specified tracepoints.\n\
16570 Arguments are tracepoint numbers, separated by spaces.\n\
16571 No argument means enable all tracepoints."),
16572 &enablelist);
16573 deprecate_cmd (c, "enable");
16574
16575 add_com ("passcount", class_trace, trace_pass_command, _("\
16576 Set the passcount for a tracepoint.\n\
16577 The trace will end when the tracepoint has been passed 'count' times.\n\
16578 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16579 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16580
16581 add_prefix_cmd ("save", class_breakpoint, save_command,
16582 _("Save breakpoint definitions as a script."),
16583 &save_cmdlist, "save ",
16584 0/*allow-unknown*/, &cmdlist);
16585
16586 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16587 Save current breakpoint definitions as a script.\n\
16588 This includes all types of breakpoints (breakpoints, watchpoints,\n\
16589 catchpoints, tracepoints). Use the 'source' command in another debug\n\
16590 session to restore them."),
16591 &save_cmdlist);
16592 set_cmd_completer (c, filename_completer);
16593
16594 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
16595 Save current tracepoint definitions as a script.\n\
16596 Use the 'source' command in another debug session to restore them."),
16597 &save_cmdlist);
16598 set_cmd_completer (c, filename_completer);
16599
16600 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16601 deprecate_cmd (c, "save tracepoints");
16602
16603 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
16604 Breakpoint specific settings\n\
16605 Configure various breakpoint-specific variables such as\n\
16606 pending breakpoint behavior"),
16607 &breakpoint_set_cmdlist, "set breakpoint ",
16608 0/*allow-unknown*/, &setlist);
16609 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
16610 Breakpoint specific settings\n\
16611 Configure various breakpoint-specific variables such as\n\
16612 pending breakpoint behavior"),
16613 &breakpoint_show_cmdlist, "show breakpoint ",
16614 0/*allow-unknown*/, &showlist);
16615
16616 add_setshow_auto_boolean_cmd ("pending", no_class,
16617 &pending_break_support, _("\
16618 Set debugger's behavior regarding pending breakpoints."), _("\
16619 Show debugger's behavior regarding pending breakpoints."), _("\
16620 If on, an unrecognized breakpoint location will cause gdb to create a\n\
16621 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16622 an error. If auto, an unrecognized breakpoint location results in a\n\
16623 user-query to see if a pending breakpoint should be created."),
16624 NULL,
16625 show_pending_break_support,
16626 &breakpoint_set_cmdlist,
16627 &breakpoint_show_cmdlist);
16628
16629 pending_break_support = AUTO_BOOLEAN_AUTO;
16630
16631 add_setshow_boolean_cmd ("auto-hw", no_class,
16632 &automatic_hardware_breakpoints, _("\
16633 Set automatic usage of hardware breakpoints."), _("\
16634 Show automatic usage of hardware breakpoints."), _("\
16635 If set, the debugger will automatically use hardware breakpoints for\n\
16636 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16637 a warning will be emitted for such breakpoints."),
16638 NULL,
16639 show_automatic_hardware_breakpoints,
16640 &breakpoint_set_cmdlist,
16641 &breakpoint_show_cmdlist);
16642
16643 add_setshow_boolean_cmd ("always-inserted", class_support,
16644 &always_inserted_mode, _("\
16645 Set mode for inserting breakpoints."), _("\
16646 Show mode for inserting breakpoints."), _("\
16647 When this mode is on, breakpoints are inserted immediately as soon as\n\
16648 they're created, kept inserted even when execution stops, and removed\n\
16649 only when the user deletes them. When this mode is off (the default),\n\
16650 breakpoints are inserted only when execution continues, and removed\n\
16651 when execution stops."),
16652 NULL,
16653 &show_always_inserted_mode,
16654 &breakpoint_set_cmdlist,
16655 &breakpoint_show_cmdlist);
16656
16657 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16658 condition_evaluation_enums,
16659 &condition_evaluation_mode_1, _("\
16660 Set mode of breakpoint condition evaluation."), _("\
16661 Show mode of breakpoint condition evaluation."), _("\
16662 When this is set to \"host\", breakpoint conditions will be\n\
16663 evaluated on the host's side by GDB. When it is set to \"target\",\n\
16664 breakpoint conditions will be downloaded to the target (if the target\n\
16665 supports such feature) and conditions will be evaluated on the target's side.\n\
16666 If this is set to \"auto\" (default), this will be automatically set to\n\
16667 \"target\" if it supports condition evaluation, otherwise it will\n\
16668 be set to \"gdb\""),
16669 &set_condition_evaluation_mode,
16670 &show_condition_evaluation_mode,
16671 &breakpoint_set_cmdlist,
16672 &breakpoint_show_cmdlist);
16673
16674 add_com ("break-range", class_breakpoint, break_range_command, _("\
16675 Set a breakpoint for an address range.\n\
16676 break-range START-LOCATION, END-LOCATION\n\
16677 where START-LOCATION and END-LOCATION can be one of the following:\n\
16678 LINENUM, for that line in the current file,\n\
16679 FILE:LINENUM, for that line in that file,\n\
16680 +OFFSET, for that number of lines after the current line\n\
16681 or the start of the range\n\
16682 FUNCTION, for the first line in that function,\n\
16683 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16684 *ADDRESS, for the instruction at that address.\n\
16685 \n\
16686 The breakpoint will stop execution of the inferior whenever it executes\n\
16687 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16688 range (including START-LOCATION and END-LOCATION)."));
16689
16690 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
16691 Set a dynamic printf at specified location.\n\
16692 dprintf location,format string,arg1,arg2,...\n\
16693 location may be a linespec, explicit, or address location.\n"
16694 "\n" LOCATION_HELP_STRING));
16695 set_cmd_completer (c, location_completer);
16696
16697 add_setshow_enum_cmd ("dprintf-style", class_support,
16698 dprintf_style_enums, &dprintf_style, _("\
16699 Set the style of usage for dynamic printf."), _("\
16700 Show the style of usage for dynamic printf."), _("\
16701 This setting chooses how GDB will do a dynamic printf.\n\
16702 If the value is \"gdb\", then the printing is done by GDB to its own\n\
16703 console, as with the \"printf\" command.\n\
16704 If the value is \"call\", the print is done by calling a function in your\n\
16705 program; by default printf(), but you can choose a different function or\n\
16706 output stream by setting dprintf-function and dprintf-channel."),
16707 update_dprintf_commands, NULL,
16708 &setlist, &showlist);
16709
16710 dprintf_function = xstrdup ("printf");
16711 add_setshow_string_cmd ("dprintf-function", class_support,
16712 &dprintf_function, _("\
16713 Set the function to use for dynamic printf"), _("\
16714 Show the function to use for dynamic printf"), NULL,
16715 update_dprintf_commands, NULL,
16716 &setlist, &showlist);
16717
16718 dprintf_channel = xstrdup ("");
16719 add_setshow_string_cmd ("dprintf-channel", class_support,
16720 &dprintf_channel, _("\
16721 Set the channel to use for dynamic printf"), _("\
16722 Show the channel to use for dynamic printf"), NULL,
16723 update_dprintf_commands, NULL,
16724 &setlist, &showlist);
16725
16726 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16727 &disconnected_dprintf, _("\
16728 Set whether dprintf continues after GDB disconnects."), _("\
16729 Show whether dprintf continues after GDB disconnects."), _("\
16730 Use this to let dprintf commands continue to hit and produce output\n\
16731 even if GDB disconnects or detaches from the target."),
16732 NULL,
16733 NULL,
16734 &setlist, &showlist);
16735
16736 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16737 agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16738 (target agent only) This is useful for formatted output in user-defined commands."));
16739
16740 automatic_hardware_breakpoints = 1;
16741
16742 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
16743 observer_attach_thread_exit (remove_threaded_breakpoints);
16744 }
This page took 0.406584 seconds and 4 git commands to generate.