b2a3c4777272f66ff8df04c5f7b764b00a7c4bbc
[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
73 /* readline include files */
74 #include "readline/readline.h"
75 #include "readline/history.h"
76
77 /* readline defines this. */
78 #undef savestring
79
80 #include "mi/mi-common.h"
81 #include "extension.h"
82
83 /* Enums for exception-handling support. */
84 enum exception_event_kind
85 {
86 EX_EVENT_THROW,
87 EX_EVENT_RETHROW,
88 EX_EVENT_CATCH
89 };
90
91 /* Prototypes for local functions. */
92
93 static void enable_delete_command (char *, int);
94
95 static void enable_once_command (char *, int);
96
97 static void enable_count_command (char *, int);
98
99 static void disable_command (char *, int);
100
101 static void enable_command (char *, int);
102
103 static void map_breakpoint_numbers (char *, void (*) (struct breakpoint *,
104 void *),
105 void *);
106
107 static void ignore_command (char *, int);
108
109 static int breakpoint_re_set_one (void *);
110
111 static void breakpoint_re_set_default (struct breakpoint *);
112
113 static void
114 create_sals_from_location_default (const struct event_location *location,
115 struct linespec_result *canonical,
116 enum bptype type_wanted);
117
118 static void create_breakpoints_sal_default (struct gdbarch *,
119 struct linespec_result *,
120 char *, char *, enum bptype,
121 enum bpdisp, int, int,
122 int,
123 const struct breakpoint_ops *,
124 int, int, int, unsigned);
125
126 static void decode_location_default (struct breakpoint *b,
127 const struct event_location *location,
128 struct symtabs_and_lines *sals);
129
130 static void clear_command (char *, int);
131
132 static void catch_command (char *, int);
133
134 static int can_use_hardware_watchpoint (struct value *);
135
136 static void break_command_1 (char *, int, int);
137
138 static void mention (struct breakpoint *);
139
140 static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
141 enum bptype,
142 const struct breakpoint_ops *);
143 static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
144 const struct symtab_and_line *);
145
146 /* This function is used in gdbtk sources and thus can not be made
147 static. */
148 struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
149 struct symtab_and_line,
150 enum bptype,
151 const struct breakpoint_ops *);
152
153 static struct breakpoint *
154 momentary_breakpoint_from_master (struct breakpoint *orig,
155 enum bptype type,
156 const struct breakpoint_ops *ops,
157 int loc_enabled);
158
159 static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
160
161 static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
162 CORE_ADDR bpaddr,
163 enum bptype bptype);
164
165 static void describe_other_breakpoints (struct gdbarch *,
166 struct program_space *, CORE_ADDR,
167 struct obj_section *, int);
168
169 static int watchpoint_locations_match (struct bp_location *loc1,
170 struct bp_location *loc2);
171
172 static int breakpoint_location_address_match (struct bp_location *bl,
173 struct address_space *aspace,
174 CORE_ADDR addr);
175
176 static int breakpoint_location_address_range_overlap (struct bp_location *,
177 struct address_space *,
178 CORE_ADDR, int);
179
180 static void breakpoints_info (char *, int);
181
182 static void watchpoints_info (char *, int);
183
184 static int breakpoint_1 (char *, int,
185 int (*) (const struct breakpoint *));
186
187 static int breakpoint_cond_eval (void *);
188
189 static void cleanup_executing_breakpoints (void *);
190
191 static void commands_command (char *, int);
192
193 static void condition_command (char *, int);
194
195 typedef enum
196 {
197 mark_inserted,
198 mark_uninserted
199 }
200 insertion_state_t;
201
202 static int remove_breakpoint (struct bp_location *, insertion_state_t);
203 static int remove_breakpoint_1 (struct bp_location *, insertion_state_t);
204
205 static enum print_stop_action print_bp_stop_message (bpstat bs);
206
207 static int watchpoint_check (void *);
208
209 static void maintenance_info_breakpoints (char *, int);
210
211 static int hw_breakpoint_used_count (void);
212
213 static int hw_watchpoint_use_count (struct breakpoint *);
214
215 static int hw_watchpoint_used_count_others (struct breakpoint *except,
216 enum bptype type,
217 int *other_type_used);
218
219 static void hbreak_command (char *, int);
220
221 static void thbreak_command (char *, int);
222
223 static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
224 int count);
225
226 static void stop_command (char *arg, int from_tty);
227
228 static void stopin_command (char *arg, int from_tty);
229
230 static void stopat_command (char *arg, int from_tty);
231
232 static void tcatch_command (char *arg, int from_tty);
233
234 static void free_bp_location (struct bp_location *loc);
235 static void incref_bp_location (struct bp_location *loc);
236 static void decref_bp_location (struct bp_location **loc);
237
238 static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
239
240 /* update_global_location_list's modes of operation wrt to whether to
241 insert locations now. */
242 enum ugll_insert_mode
243 {
244 /* Don't insert any breakpoint locations into the inferior, only
245 remove already-inserted locations that no longer should be
246 inserted. Functions that delete a breakpoint or breakpoints
247 should specify this mode, so that deleting a breakpoint doesn't
248 have the side effect of inserting the locations of other
249 breakpoints that are marked not-inserted, but should_be_inserted
250 returns true on them.
251
252 This behavior is useful is situations close to tear-down -- e.g.,
253 after an exec, while the target still has execution, but
254 breakpoint shadows of the previous executable image should *NOT*
255 be restored to the new image; or before detaching, where the
256 target still has execution and wants to delete breakpoints from
257 GDB's lists, and all breakpoints had already been removed from
258 the inferior. */
259 UGLL_DONT_INSERT,
260
261 /* May insert breakpoints iff breakpoints_should_be_inserted_now
262 claims breakpoints should be inserted now. */
263 UGLL_MAY_INSERT,
264
265 /* Insert locations now, irrespective of
266 breakpoints_should_be_inserted_now. E.g., say all threads are
267 stopped right now, and the user did "continue". We need to
268 insert breakpoints _before_ resuming the target, but
269 UGLL_MAY_INSERT wouldn't insert them, because
270 breakpoints_should_be_inserted_now returns false at that point,
271 as no thread is running yet. */
272 UGLL_INSERT
273 };
274
275 static void update_global_location_list (enum ugll_insert_mode);
276
277 static void update_global_location_list_nothrow (enum ugll_insert_mode);
278
279 static int is_hardware_watchpoint (const struct breakpoint *bpt);
280
281 static void insert_breakpoint_locations (void);
282
283 static void tracepoints_info (char *, int);
284
285 static void delete_trace_command (char *, int);
286
287 static void enable_trace_command (char *, int);
288
289 static void disable_trace_command (char *, int);
290
291 static void trace_pass_command (char *, int);
292
293 static void set_tracepoint_count (int num);
294
295 static int is_masked_watchpoint (const struct breakpoint *b);
296
297 static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
298
299 /* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
300 otherwise. */
301
302 static int strace_marker_p (struct breakpoint *b);
303
304 /* The breakpoint_ops structure to be inherited by all breakpoint_ops
305 that are implemented on top of software or hardware breakpoints
306 (user breakpoints, internal and momentary breakpoints, etc.). */
307 static struct breakpoint_ops bkpt_base_breakpoint_ops;
308
309 /* Internal breakpoints class type. */
310 static struct breakpoint_ops internal_breakpoint_ops;
311
312 /* Momentary breakpoints class type. */
313 static struct breakpoint_ops momentary_breakpoint_ops;
314
315 /* Momentary breakpoints for bp_longjmp and bp_exception class type. */
316 static struct breakpoint_ops longjmp_breakpoint_ops;
317
318 /* The breakpoint_ops structure to be used in regular user created
319 breakpoints. */
320 struct breakpoint_ops bkpt_breakpoint_ops;
321
322 /* Breakpoints set on probes. */
323 static struct breakpoint_ops bkpt_probe_breakpoint_ops;
324
325 /* Dynamic printf class type. */
326 struct breakpoint_ops dprintf_breakpoint_ops;
327
328 /* The style in which to perform a dynamic printf. This is a user
329 option because different output options have different tradeoffs;
330 if GDB does the printing, there is better error handling if there
331 is a problem with any of the arguments, but using an inferior
332 function lets you have special-purpose printers and sending of
333 output to the same place as compiled-in print functions. */
334
335 static const char dprintf_style_gdb[] = "gdb";
336 static const char dprintf_style_call[] = "call";
337 static const char dprintf_style_agent[] = "agent";
338 static const char *const dprintf_style_enums[] = {
339 dprintf_style_gdb,
340 dprintf_style_call,
341 dprintf_style_agent,
342 NULL
343 };
344 static const char *dprintf_style = dprintf_style_gdb;
345
346 /* The function to use for dynamic printf if the preferred style is to
347 call into the inferior. The value is simply a string that is
348 copied into the command, so it can be anything that GDB can
349 evaluate to a callable address, not necessarily a function name. */
350
351 static char *dprintf_function = "";
352
353 /* The channel to use for dynamic printf if the preferred style is to
354 call into the inferior; if a nonempty string, it will be passed to
355 the call as the first argument, with the format string as the
356 second. As with the dprintf function, this can be anything that
357 GDB knows how to evaluate, so in addition to common choices like
358 "stderr", this could be an app-specific expression like
359 "mystreams[curlogger]". */
360
361 static char *dprintf_channel = "";
362
363 /* True if dprintf commands should continue to operate even if GDB
364 has disconnected. */
365 static int disconnected_dprintf = 1;
366
367 /* A reference-counted struct command_line. This lets multiple
368 breakpoints share a single command list. */
369 struct counted_command_line
370 {
371 /* The reference count. */
372 int refc;
373
374 /* The command list. */
375 struct command_line *commands;
376 };
377
378 struct command_line *
379 breakpoint_commands (struct breakpoint *b)
380 {
381 return b->commands ? b->commands->commands : NULL;
382 }
383
384 /* Flag indicating that a command has proceeded the inferior past the
385 current breakpoint. */
386
387 static int breakpoint_proceeded;
388
389 const char *
390 bpdisp_text (enum bpdisp disp)
391 {
392 /* NOTE: the following values are a part of MI protocol and
393 represent values of 'disp' field returned when inferior stops at
394 a breakpoint. */
395 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
396
397 return bpdisps[(int) disp];
398 }
399
400 /* Prototypes for exported functions. */
401 /* If FALSE, gdb will not use hardware support for watchpoints, even
402 if such is available. */
403 static int can_use_hw_watchpoints;
404
405 static void
406 show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
407 struct cmd_list_element *c,
408 const char *value)
409 {
410 fprintf_filtered (file,
411 _("Debugger's willingness to use "
412 "watchpoint hardware is %s.\n"),
413 value);
414 }
415
416 /* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
417 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
418 for unrecognized breakpoint locations.
419 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
420 static enum auto_boolean pending_break_support;
421 static void
422 show_pending_break_support (struct ui_file *file, int from_tty,
423 struct cmd_list_element *c,
424 const char *value)
425 {
426 fprintf_filtered (file,
427 _("Debugger's behavior regarding "
428 "pending breakpoints is %s.\n"),
429 value);
430 }
431
432 /* If 1, gdb will automatically use hardware breakpoints for breakpoints
433 set with "break" but falling in read-only memory.
434 If 0, gdb will warn about such breakpoints, but won't automatically
435 use hardware breakpoints. */
436 static int automatic_hardware_breakpoints;
437 static void
438 show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
439 struct cmd_list_element *c,
440 const char *value)
441 {
442 fprintf_filtered (file,
443 _("Automatic usage of hardware breakpoints is %s.\n"),
444 value);
445 }
446
447 /* If on, GDB keeps breakpoints inserted even if the inferior is
448 stopped, and immediately inserts any new breakpoints as soon as
449 they're created. If off (default), GDB keeps breakpoints off of
450 the target as long as possible. That is, it delays inserting
451 breakpoints until the next resume, and removes them again when the
452 target fully stops. This is a bit safer in case GDB crashes while
453 processing user input. */
454 static int always_inserted_mode = 0;
455
456 static void
457 show_always_inserted_mode (struct ui_file *file, int from_tty,
458 struct cmd_list_element *c, const char *value)
459 {
460 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
461 value);
462 }
463
464 /* See breakpoint.h. */
465
466 int
467 breakpoints_should_be_inserted_now (void)
468 {
469 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
470 {
471 /* If breakpoints are global, they should be inserted even if no
472 thread under gdb's control is running, or even if there are
473 no threads under GDB's control yet. */
474 return 1;
475 }
476 else if (target_has_execution)
477 {
478 struct thread_info *tp;
479
480 if (always_inserted_mode)
481 {
482 /* The user wants breakpoints inserted even if all threads
483 are stopped. */
484 return 1;
485 }
486
487 if (threads_are_executing ())
488 return 1;
489
490 /* Don't remove breakpoints yet if, even though all threads are
491 stopped, we still have events to process. */
492 ALL_NON_EXITED_THREADS (tp)
493 if (tp->resumed
494 && tp->suspend.waitstatus_pending_p)
495 return 1;
496 }
497 return 0;
498 }
499
500 static const char condition_evaluation_both[] = "host or target";
501
502 /* Modes for breakpoint condition evaluation. */
503 static const char condition_evaluation_auto[] = "auto";
504 static const char condition_evaluation_host[] = "host";
505 static const char condition_evaluation_target[] = "target";
506 static const char *const condition_evaluation_enums[] = {
507 condition_evaluation_auto,
508 condition_evaluation_host,
509 condition_evaluation_target,
510 NULL
511 };
512
513 /* Global that holds the current mode for breakpoint condition evaluation. */
514 static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
515
516 /* Global that we use to display information to the user (gets its value from
517 condition_evaluation_mode_1. */
518 static const char *condition_evaluation_mode = condition_evaluation_auto;
519
520 /* Translate a condition evaluation mode MODE into either "host"
521 or "target". This is used mostly to translate from "auto" to the
522 real setting that is being used. It returns the translated
523 evaluation mode. */
524
525 static const char *
526 translate_condition_evaluation_mode (const char *mode)
527 {
528 if (mode == condition_evaluation_auto)
529 {
530 if (target_supports_evaluation_of_breakpoint_conditions ())
531 return condition_evaluation_target;
532 else
533 return condition_evaluation_host;
534 }
535 else
536 return mode;
537 }
538
539 /* Discovers what condition_evaluation_auto translates to. */
540
541 static const char *
542 breakpoint_condition_evaluation_mode (void)
543 {
544 return translate_condition_evaluation_mode (condition_evaluation_mode);
545 }
546
547 /* Return true if GDB should evaluate breakpoint conditions or false
548 otherwise. */
549
550 static int
551 gdb_evaluates_breakpoint_condition_p (void)
552 {
553 const char *mode = breakpoint_condition_evaluation_mode ();
554
555 return (mode == condition_evaluation_host);
556 }
557
558 void _initialize_breakpoint (void);
559
560 /* Are we executing breakpoint commands? */
561 static int executing_breakpoint_commands;
562
563 /* Are overlay event breakpoints enabled? */
564 static int overlay_events_enabled;
565
566 /* See description in breakpoint.h. */
567 int target_exact_watchpoints = 0;
568
569 /* Walk the following statement or block through all breakpoints.
570 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
571 current breakpoint. */
572
573 #define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
574
575 #define ALL_BREAKPOINTS_SAFE(B,TMP) \
576 for (B = breakpoint_chain; \
577 B ? (TMP=B->next, 1): 0; \
578 B = TMP)
579
580 /* Similar iterator for the low-level breakpoints. SAFE variant is
581 not provided so update_global_location_list must not be called
582 while executing the block of ALL_BP_LOCATIONS. */
583
584 #define ALL_BP_LOCATIONS(B,BP_TMP) \
585 for (BP_TMP = bp_location; \
586 BP_TMP < bp_location + bp_location_count && (B = *BP_TMP); \
587 BP_TMP++)
588
589 /* Iterates through locations with address ADDRESS for the currently selected
590 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
591 to where the loop should start from.
592 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
593 appropriate location to start with. */
594
595 #define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
596 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
597 BP_LOCP_TMP = BP_LOCP_START; \
598 BP_LOCP_START \
599 && (BP_LOCP_TMP < bp_location + bp_location_count \
600 && (*BP_LOCP_TMP)->address == ADDRESS); \
601 BP_LOCP_TMP++)
602
603 /* Iterator for tracepoints only. */
604
605 #define ALL_TRACEPOINTS(B) \
606 for (B = breakpoint_chain; B; B = B->next) \
607 if (is_tracepoint (B))
608
609 /* Chains of all breakpoints defined. */
610
611 struct breakpoint *breakpoint_chain;
612
613 /* Array is sorted by bp_location_compare - primarily by the ADDRESS. */
614
615 static struct bp_location **bp_location;
616
617 /* Number of elements of BP_LOCATION. */
618
619 static unsigned bp_location_count;
620
621 /* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
622 ADDRESS for the current elements of BP_LOCATION which get a valid
623 result from bp_location_has_shadow. You can use it for roughly
624 limiting the subrange of BP_LOCATION to scan for shadow bytes for
625 an address you need to read. */
626
627 static CORE_ADDR bp_location_placed_address_before_address_max;
628
629 /* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
630 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
631 BP_LOCATION which get a valid result from bp_location_has_shadow.
632 You can use it for roughly limiting the subrange of BP_LOCATION to
633 scan for shadow bytes for an address you need to read. */
634
635 static CORE_ADDR bp_location_shadow_len_after_address_max;
636
637 /* The locations that no longer correspond to any breakpoint, unlinked
638 from bp_location array, but for which a hit may still be reported
639 by a target. */
640 VEC(bp_location_p) *moribund_locations = NULL;
641
642 /* Number of last breakpoint made. */
643
644 static int breakpoint_count;
645
646 /* The value of `breakpoint_count' before the last command that
647 created breakpoints. If the last (break-like) command created more
648 than one breakpoint, then the difference between BREAKPOINT_COUNT
649 and PREV_BREAKPOINT_COUNT is more than one. */
650 static int prev_breakpoint_count;
651
652 /* Number of last tracepoint made. */
653
654 static int tracepoint_count;
655
656 static struct cmd_list_element *breakpoint_set_cmdlist;
657 static struct cmd_list_element *breakpoint_show_cmdlist;
658 struct cmd_list_element *save_cmdlist;
659
660 /* See declaration at breakpoint.h. */
661
662 struct breakpoint *
663 breakpoint_find_if (int (*func) (struct breakpoint *b, void *d),
664 void *user_data)
665 {
666 struct breakpoint *b = NULL;
667
668 ALL_BREAKPOINTS (b)
669 {
670 if (func (b, user_data) != 0)
671 break;
672 }
673
674 return b;
675 }
676
677 /* Return whether a breakpoint is an active enabled breakpoint. */
678 static int
679 breakpoint_enabled (struct breakpoint *b)
680 {
681 return (b->enable_state == bp_enabled);
682 }
683
684 /* Set breakpoint count to NUM. */
685
686 static void
687 set_breakpoint_count (int num)
688 {
689 prev_breakpoint_count = breakpoint_count;
690 breakpoint_count = num;
691 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
692 }
693
694 /* Used by `start_rbreak_breakpoints' below, to record the current
695 breakpoint count before "rbreak" creates any breakpoint. */
696 static int rbreak_start_breakpoint_count;
697
698 /* Called at the start an "rbreak" command to record the first
699 breakpoint made. */
700
701 void
702 start_rbreak_breakpoints (void)
703 {
704 rbreak_start_breakpoint_count = breakpoint_count;
705 }
706
707 /* Called at the end of an "rbreak" command to record the last
708 breakpoint made. */
709
710 void
711 end_rbreak_breakpoints (void)
712 {
713 prev_breakpoint_count = rbreak_start_breakpoint_count;
714 }
715
716 /* Used in run_command to zero the hit count when a new run starts. */
717
718 void
719 clear_breakpoint_hit_counts (void)
720 {
721 struct breakpoint *b;
722
723 ALL_BREAKPOINTS (b)
724 b->hit_count = 0;
725 }
726
727 /* Allocate a new counted_command_line with reference count of 1.
728 The new structure owns COMMANDS. */
729
730 static struct counted_command_line *
731 alloc_counted_command_line (struct command_line *commands)
732 {
733 struct counted_command_line *result = XNEW (struct counted_command_line);
734
735 result->refc = 1;
736 result->commands = commands;
737
738 return result;
739 }
740
741 /* Increment reference count. This does nothing if CMD is NULL. */
742
743 static void
744 incref_counted_command_line (struct counted_command_line *cmd)
745 {
746 if (cmd)
747 ++cmd->refc;
748 }
749
750 /* Decrement reference count. If the reference count reaches 0,
751 destroy the counted_command_line. Sets *CMDP to NULL. This does
752 nothing if *CMDP is NULL. */
753
754 static void
755 decref_counted_command_line (struct counted_command_line **cmdp)
756 {
757 if (*cmdp)
758 {
759 if (--(*cmdp)->refc == 0)
760 {
761 free_command_lines (&(*cmdp)->commands);
762 xfree (*cmdp);
763 }
764 *cmdp = NULL;
765 }
766 }
767
768 /* A cleanup function that calls decref_counted_command_line. */
769
770 static void
771 do_cleanup_counted_command_line (void *arg)
772 {
773 decref_counted_command_line ((struct counted_command_line **) arg);
774 }
775
776 /* Create a cleanup that calls decref_counted_command_line on the
777 argument. */
778
779 static struct cleanup *
780 make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
781 {
782 return make_cleanup (do_cleanup_counted_command_line, cmdp);
783 }
784
785 \f
786 /* Return the breakpoint with the specified number, or NULL
787 if the number does not refer to an existing breakpoint. */
788
789 struct breakpoint *
790 get_breakpoint (int num)
791 {
792 struct breakpoint *b;
793
794 ALL_BREAKPOINTS (b)
795 if (b->number == num)
796 return b;
797
798 return NULL;
799 }
800
801 \f
802
803 /* Mark locations as "conditions have changed" in case the target supports
804 evaluating conditions on its side. */
805
806 static void
807 mark_breakpoint_modified (struct breakpoint *b)
808 {
809 struct bp_location *loc;
810
811 /* This is only meaningful if the target is
812 evaluating conditions and if the user has
813 opted for condition evaluation on the target's
814 side. */
815 if (gdb_evaluates_breakpoint_condition_p ()
816 || !target_supports_evaluation_of_breakpoint_conditions ())
817 return;
818
819 if (!is_breakpoint (b))
820 return;
821
822 for (loc = b->loc; loc; loc = loc->next)
823 loc->condition_changed = condition_modified;
824 }
825
826 /* Mark location as "conditions have changed" in case the target supports
827 evaluating conditions on its side. */
828
829 static void
830 mark_breakpoint_location_modified (struct bp_location *loc)
831 {
832 /* This is only meaningful if the target is
833 evaluating conditions and if the user has
834 opted for condition evaluation on the target's
835 side. */
836 if (gdb_evaluates_breakpoint_condition_p ()
837 || !target_supports_evaluation_of_breakpoint_conditions ())
838
839 return;
840
841 if (!is_breakpoint (loc->owner))
842 return;
843
844 loc->condition_changed = condition_modified;
845 }
846
847 /* Sets the condition-evaluation mode using the static global
848 condition_evaluation_mode. */
849
850 static void
851 set_condition_evaluation_mode (char *args, int from_tty,
852 struct cmd_list_element *c)
853 {
854 const char *old_mode, *new_mode;
855
856 if ((condition_evaluation_mode_1 == condition_evaluation_target)
857 && !target_supports_evaluation_of_breakpoint_conditions ())
858 {
859 condition_evaluation_mode_1 = condition_evaluation_mode;
860 warning (_("Target does not support breakpoint condition evaluation.\n"
861 "Using host evaluation mode instead."));
862 return;
863 }
864
865 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
866 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
867
868 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
869 settings was "auto". */
870 condition_evaluation_mode = condition_evaluation_mode_1;
871
872 /* Only update the mode if the user picked a different one. */
873 if (new_mode != old_mode)
874 {
875 struct bp_location *loc, **loc_tmp;
876 /* If the user switched to a different evaluation mode, we
877 need to synch the changes with the target as follows:
878
879 "host" -> "target": Send all (valid) conditions to the target.
880 "target" -> "host": Remove all the conditions from the target.
881 */
882
883 if (new_mode == condition_evaluation_target)
884 {
885 /* Mark everything modified and synch conditions with the
886 target. */
887 ALL_BP_LOCATIONS (loc, loc_tmp)
888 mark_breakpoint_location_modified (loc);
889 }
890 else
891 {
892 /* Manually mark non-duplicate locations to synch conditions
893 with the target. We do this to remove all the conditions the
894 target knows about. */
895 ALL_BP_LOCATIONS (loc, loc_tmp)
896 if (is_breakpoint (loc->owner) && loc->inserted)
897 loc->needs_update = 1;
898 }
899
900 /* Do the update. */
901 update_global_location_list (UGLL_MAY_INSERT);
902 }
903
904 return;
905 }
906
907 /* Shows the current mode of breakpoint condition evaluation. Explicitly shows
908 what "auto" is translating to. */
909
910 static void
911 show_condition_evaluation_mode (struct ui_file *file, int from_tty,
912 struct cmd_list_element *c, const char *value)
913 {
914 if (condition_evaluation_mode == condition_evaluation_auto)
915 fprintf_filtered (file,
916 _("Breakpoint condition evaluation "
917 "mode is %s (currently %s).\n"),
918 value,
919 breakpoint_condition_evaluation_mode ());
920 else
921 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
922 value);
923 }
924
925 /* A comparison function for bp_location AP and BP that is used by
926 bsearch. This comparison function only cares about addresses, unlike
927 the more general bp_location_compare function. */
928
929 static int
930 bp_location_compare_addrs (const void *ap, const void *bp)
931 {
932 const struct bp_location *a = *(const struct bp_location **) ap;
933 const struct bp_location *b = *(const struct bp_location **) bp;
934
935 if (a->address == b->address)
936 return 0;
937 else
938 return ((a->address > b->address) - (a->address < b->address));
939 }
940
941 /* Helper function to skip all bp_locations with addresses
942 less than ADDRESS. It returns the first bp_location that
943 is greater than or equal to ADDRESS. If none is found, just
944 return NULL. */
945
946 static struct bp_location **
947 get_first_locp_gte_addr (CORE_ADDR address)
948 {
949 struct bp_location dummy_loc;
950 struct bp_location *dummy_locp = &dummy_loc;
951 struct bp_location **locp_found = NULL;
952
953 /* Initialize the dummy location's address field. */
954 memset (&dummy_loc, 0, sizeof (struct bp_location));
955 dummy_loc.address = address;
956
957 /* Find a close match to the first location at ADDRESS. */
958 locp_found = ((struct bp_location **)
959 bsearch (&dummy_locp, bp_location, bp_location_count,
960 sizeof (struct bp_location **),
961 bp_location_compare_addrs));
962
963 /* Nothing was found, nothing left to do. */
964 if (locp_found == NULL)
965 return NULL;
966
967 /* We may have found a location that is at ADDRESS but is not the first in the
968 location's list. Go backwards (if possible) and locate the first one. */
969 while ((locp_found - 1) >= bp_location
970 && (*(locp_found - 1))->address == address)
971 locp_found--;
972
973 return locp_found;
974 }
975
976 void
977 set_breakpoint_condition (struct breakpoint *b, const char *exp,
978 int from_tty)
979 {
980 xfree (b->cond_string);
981 b->cond_string = NULL;
982
983 if (is_watchpoint (b))
984 {
985 struct watchpoint *w = (struct watchpoint *) b;
986
987 xfree (w->cond_exp);
988 w->cond_exp = NULL;
989 }
990 else
991 {
992 struct bp_location *loc;
993
994 for (loc = b->loc; loc; loc = loc->next)
995 {
996 xfree (loc->cond);
997 loc->cond = NULL;
998
999 /* No need to free the condition agent expression
1000 bytecode (if we have one). We will handle this
1001 when we go through update_global_location_list. */
1002 }
1003 }
1004
1005 if (*exp == 0)
1006 {
1007 if (from_tty)
1008 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
1009 }
1010 else
1011 {
1012 const char *arg = exp;
1013
1014 /* I don't know if it matters whether this is the string the user
1015 typed in or the decompiled expression. */
1016 b->cond_string = xstrdup (arg);
1017 b->condition_not_parsed = 0;
1018
1019 if (is_watchpoint (b))
1020 {
1021 struct watchpoint *w = (struct watchpoint *) b;
1022
1023 innermost_block = NULL;
1024 arg = exp;
1025 w->cond_exp = parse_exp_1 (&arg, 0, 0, 0);
1026 if (*arg)
1027 error (_("Junk at end of expression"));
1028 w->cond_exp_valid_block = innermost_block;
1029 }
1030 else
1031 {
1032 struct bp_location *loc;
1033
1034 for (loc = b->loc; loc; loc = loc->next)
1035 {
1036 arg = exp;
1037 loc->cond =
1038 parse_exp_1 (&arg, loc->address,
1039 block_for_pc (loc->address), 0);
1040 if (*arg)
1041 error (_("Junk at end of expression"));
1042 }
1043 }
1044 }
1045 mark_breakpoint_modified (b);
1046
1047 observer_notify_breakpoint_modified (b);
1048 }
1049
1050 /* Completion for the "condition" command. */
1051
1052 static VEC (char_ptr) *
1053 condition_completer (struct cmd_list_element *cmd,
1054 const char *text, const char *word)
1055 {
1056 const char *space;
1057
1058 text = skip_spaces_const (text);
1059 space = skip_to_space_const (text);
1060 if (*space == '\0')
1061 {
1062 int len;
1063 struct breakpoint *b;
1064 VEC (char_ptr) *result = NULL;
1065
1066 if (text[0] == '$')
1067 {
1068 /* We don't support completion of history indices. */
1069 if (isdigit (text[1]))
1070 return NULL;
1071 return complete_internalvar (&text[1]);
1072 }
1073
1074 /* We're completing the breakpoint number. */
1075 len = strlen (text);
1076
1077 ALL_BREAKPOINTS (b)
1078 {
1079 char number[50];
1080
1081 xsnprintf (number, sizeof (number), "%d", b->number);
1082
1083 if (strncmp (number, text, len) == 0)
1084 VEC_safe_push (char_ptr, result, xstrdup (number));
1085 }
1086
1087 return result;
1088 }
1089
1090 /* We're completing the expression part. */
1091 text = skip_spaces_const (space);
1092 return expression_completer (cmd, text, word);
1093 }
1094
1095 /* condition N EXP -- set break condition of breakpoint N to EXP. */
1096
1097 static void
1098 condition_command (char *arg, int from_tty)
1099 {
1100 struct breakpoint *b;
1101 char *p;
1102 int bnum;
1103
1104 if (arg == 0)
1105 error_no_arg (_("breakpoint number"));
1106
1107 p = arg;
1108 bnum = get_number (&p);
1109 if (bnum == 0)
1110 error (_("Bad breakpoint argument: '%s'"), arg);
1111
1112 ALL_BREAKPOINTS (b)
1113 if (b->number == bnum)
1114 {
1115 /* Check if this breakpoint has a "stop" method implemented in an
1116 extension language. This method and conditions entered into GDB
1117 from the CLI are mutually exclusive. */
1118 const struct extension_language_defn *extlang
1119 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1120
1121 if (extlang != NULL)
1122 {
1123 error (_("Only one stop condition allowed. There is currently"
1124 " a %s stop condition defined for this breakpoint."),
1125 ext_lang_capitalized_name (extlang));
1126 }
1127 set_breakpoint_condition (b, p, from_tty);
1128
1129 if (is_breakpoint (b))
1130 update_global_location_list (UGLL_MAY_INSERT);
1131
1132 return;
1133 }
1134
1135 error (_("No breakpoint number %d."), bnum);
1136 }
1137
1138 /* Check that COMMAND do not contain commands that are suitable
1139 only for tracepoints and not suitable for ordinary breakpoints.
1140 Throw if any such commands is found. */
1141
1142 static void
1143 check_no_tracepoint_commands (struct command_line *commands)
1144 {
1145 struct command_line *c;
1146
1147 for (c = commands; c; c = c->next)
1148 {
1149 int i;
1150
1151 if (c->control_type == while_stepping_control)
1152 error (_("The 'while-stepping' command can "
1153 "only be used for tracepoints"));
1154
1155 for (i = 0; i < c->body_count; ++i)
1156 check_no_tracepoint_commands ((c->body_list)[i]);
1157
1158 /* Not that command parsing removes leading whitespace and comment
1159 lines and also empty lines. So, we only need to check for
1160 command directly. */
1161 if (strstr (c->line, "collect ") == c->line)
1162 error (_("The 'collect' command can only be used for tracepoints"));
1163
1164 if (strstr (c->line, "teval ") == c->line)
1165 error (_("The 'teval' command can only be used for tracepoints"));
1166 }
1167 }
1168
1169 /* Encapsulate tests for different types of tracepoints. */
1170
1171 static int
1172 is_tracepoint_type (enum bptype type)
1173 {
1174 return (type == bp_tracepoint
1175 || type == bp_fast_tracepoint
1176 || type == bp_static_tracepoint);
1177 }
1178
1179 int
1180 is_tracepoint (const struct breakpoint *b)
1181 {
1182 return is_tracepoint_type (b->type);
1183 }
1184
1185 /* A helper function that validates that COMMANDS are valid for a
1186 breakpoint. This function will throw an exception if a problem is
1187 found. */
1188
1189 static void
1190 validate_commands_for_breakpoint (struct breakpoint *b,
1191 struct command_line *commands)
1192 {
1193 if (is_tracepoint (b))
1194 {
1195 struct tracepoint *t = (struct tracepoint *) b;
1196 struct command_line *c;
1197 struct command_line *while_stepping = 0;
1198
1199 /* Reset the while-stepping step count. The previous commands
1200 might have included a while-stepping action, while the new
1201 ones might not. */
1202 t->step_count = 0;
1203
1204 /* We need to verify that each top-level element of commands is
1205 valid for tracepoints, that there's at most one
1206 while-stepping element, and that the while-stepping's body
1207 has valid tracing commands excluding nested while-stepping.
1208 We also need to validate the tracepoint action line in the
1209 context of the tracepoint --- validate_actionline actually
1210 has side effects, like setting the tracepoint's
1211 while-stepping STEP_COUNT, in addition to checking if the
1212 collect/teval actions parse and make sense in the
1213 tracepoint's context. */
1214 for (c = commands; c; c = c->next)
1215 {
1216 if (c->control_type == while_stepping_control)
1217 {
1218 if (b->type == bp_fast_tracepoint)
1219 error (_("The 'while-stepping' command "
1220 "cannot be used for fast tracepoint"));
1221 else if (b->type == bp_static_tracepoint)
1222 error (_("The 'while-stepping' command "
1223 "cannot be used for static tracepoint"));
1224
1225 if (while_stepping)
1226 error (_("The 'while-stepping' command "
1227 "can be used only once"));
1228 else
1229 while_stepping = c;
1230 }
1231
1232 validate_actionline (c->line, b);
1233 }
1234 if (while_stepping)
1235 {
1236 struct command_line *c2;
1237
1238 gdb_assert (while_stepping->body_count == 1);
1239 c2 = while_stepping->body_list[0];
1240 for (; c2; c2 = c2->next)
1241 {
1242 if (c2->control_type == while_stepping_control)
1243 error (_("The 'while-stepping' command cannot be nested"));
1244 }
1245 }
1246 }
1247 else
1248 {
1249 check_no_tracepoint_commands (commands);
1250 }
1251 }
1252
1253 /* Return a vector of all the static tracepoints set at ADDR. The
1254 caller is responsible for releasing the vector. */
1255
1256 VEC(breakpoint_p) *
1257 static_tracepoints_here (CORE_ADDR addr)
1258 {
1259 struct breakpoint *b;
1260 VEC(breakpoint_p) *found = 0;
1261 struct bp_location *loc;
1262
1263 ALL_BREAKPOINTS (b)
1264 if (b->type == bp_static_tracepoint)
1265 {
1266 for (loc = b->loc; loc; loc = loc->next)
1267 if (loc->address == addr)
1268 VEC_safe_push(breakpoint_p, found, b);
1269 }
1270
1271 return found;
1272 }
1273
1274 /* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
1275 validate that only allowed commands are included. */
1276
1277 void
1278 breakpoint_set_commands (struct breakpoint *b,
1279 struct command_line *commands)
1280 {
1281 validate_commands_for_breakpoint (b, commands);
1282
1283 decref_counted_command_line (&b->commands);
1284 b->commands = alloc_counted_command_line (commands);
1285 observer_notify_breakpoint_modified (b);
1286 }
1287
1288 /* Set the internal `silent' flag on the breakpoint. Note that this
1289 is not the same as the "silent" that may appear in the breakpoint's
1290 commands. */
1291
1292 void
1293 breakpoint_set_silent (struct breakpoint *b, int silent)
1294 {
1295 int old_silent = b->silent;
1296
1297 b->silent = silent;
1298 if (old_silent != silent)
1299 observer_notify_breakpoint_modified (b);
1300 }
1301
1302 /* Set the thread for this breakpoint. If THREAD is -1, make the
1303 breakpoint work for any thread. */
1304
1305 void
1306 breakpoint_set_thread (struct breakpoint *b, int thread)
1307 {
1308 int old_thread = b->thread;
1309
1310 b->thread = thread;
1311 if (old_thread != thread)
1312 observer_notify_breakpoint_modified (b);
1313 }
1314
1315 /* Set the task for this breakpoint. If TASK is 0, make the
1316 breakpoint work for any task. */
1317
1318 void
1319 breakpoint_set_task (struct breakpoint *b, int task)
1320 {
1321 int old_task = b->task;
1322
1323 b->task = task;
1324 if (old_task != task)
1325 observer_notify_breakpoint_modified (b);
1326 }
1327
1328 void
1329 check_tracepoint_command (char *line, void *closure)
1330 {
1331 struct breakpoint *b = (struct breakpoint *) closure;
1332
1333 validate_actionline (line, b);
1334 }
1335
1336 /* A structure used to pass information through
1337 map_breakpoint_numbers. */
1338
1339 struct commands_info
1340 {
1341 /* True if the command was typed at a tty. */
1342 int from_tty;
1343
1344 /* The breakpoint range spec. */
1345 char *arg;
1346
1347 /* Non-NULL if the body of the commands are being read from this
1348 already-parsed command. */
1349 struct command_line *control;
1350
1351 /* The command lines read from the user, or NULL if they have not
1352 yet been read. */
1353 struct counted_command_line *cmd;
1354 };
1355
1356 /* A callback for map_breakpoint_numbers that sets the commands for
1357 commands_command. */
1358
1359 static void
1360 do_map_commands_command (struct breakpoint *b, void *data)
1361 {
1362 struct commands_info *info = (struct commands_info *) data;
1363
1364 if (info->cmd == NULL)
1365 {
1366 struct command_line *l;
1367
1368 if (info->control != NULL)
1369 l = copy_command_lines (info->control->body_list[0]);
1370 else
1371 {
1372 struct cleanup *old_chain;
1373 char *str;
1374
1375 str = xstrprintf (_("Type commands for breakpoint(s) "
1376 "%s, one per line."),
1377 info->arg);
1378
1379 old_chain = make_cleanup (xfree, str);
1380
1381 l = read_command_lines (str,
1382 info->from_tty, 1,
1383 (is_tracepoint (b)
1384 ? check_tracepoint_command : 0),
1385 b);
1386
1387 do_cleanups (old_chain);
1388 }
1389
1390 info->cmd = alloc_counted_command_line (l);
1391 }
1392
1393 /* If a breakpoint was on the list more than once, we don't need to
1394 do anything. */
1395 if (b->commands != info->cmd)
1396 {
1397 validate_commands_for_breakpoint (b, info->cmd->commands);
1398 incref_counted_command_line (info->cmd);
1399 decref_counted_command_line (&b->commands);
1400 b->commands = info->cmd;
1401 observer_notify_breakpoint_modified (b);
1402 }
1403 }
1404
1405 static void
1406 commands_command_1 (char *arg, int from_tty,
1407 struct command_line *control)
1408 {
1409 struct cleanup *cleanups;
1410 struct commands_info info;
1411
1412 info.from_tty = from_tty;
1413 info.control = control;
1414 info.cmd = NULL;
1415 /* If we read command lines from the user, then `info' will hold an
1416 extra reference to the commands that we must clean up. */
1417 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
1418
1419 if (arg == NULL || !*arg)
1420 {
1421 if (breakpoint_count - prev_breakpoint_count > 1)
1422 arg = xstrprintf ("%d-%d", prev_breakpoint_count + 1,
1423 breakpoint_count);
1424 else if (breakpoint_count > 0)
1425 arg = xstrprintf ("%d", breakpoint_count);
1426 else
1427 {
1428 /* So that we don't try to free the incoming non-NULL
1429 argument in the cleanup below. Mapping breakpoint
1430 numbers will fail in this case. */
1431 arg = NULL;
1432 }
1433 }
1434 else
1435 /* The command loop has some static state, so we need to preserve
1436 our argument. */
1437 arg = xstrdup (arg);
1438
1439 if (arg != NULL)
1440 make_cleanup (xfree, arg);
1441
1442 info.arg = arg;
1443
1444 map_breakpoint_numbers (arg, do_map_commands_command, &info);
1445
1446 if (info.cmd == NULL)
1447 error (_("No breakpoints specified."));
1448
1449 do_cleanups (cleanups);
1450 }
1451
1452 static void
1453 commands_command (char *arg, int from_tty)
1454 {
1455 commands_command_1 (arg, from_tty, NULL);
1456 }
1457
1458 /* Like commands_command, but instead of reading the commands from
1459 input stream, takes them from an already parsed command structure.
1460
1461 This is used by cli-script.c to DTRT with breakpoint commands
1462 that are part of if and while bodies. */
1463 enum command_control_type
1464 commands_from_control_command (char *arg, struct command_line *cmd)
1465 {
1466 commands_command_1 (arg, 0, cmd);
1467 return simple_control;
1468 }
1469
1470 /* Return non-zero if BL->TARGET_INFO contains valid information. */
1471
1472 static int
1473 bp_location_has_shadow (struct bp_location *bl)
1474 {
1475 if (bl->loc_type != bp_loc_software_breakpoint)
1476 return 0;
1477 if (!bl->inserted)
1478 return 0;
1479 if (bl->target_info.shadow_len == 0)
1480 /* BL isn't valid, or doesn't shadow memory. */
1481 return 0;
1482 return 1;
1483 }
1484
1485 /* Update BUF, which is LEN bytes read from the target address
1486 MEMADDR, by replacing a memory breakpoint with its shadowed
1487 contents.
1488
1489 If READBUF is not NULL, this buffer must not overlap with the of
1490 the breakpoint location's shadow_contents buffer. Otherwise, a
1491 failed assertion internal error will be raised. */
1492
1493 static void
1494 one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1495 const gdb_byte *writebuf_org,
1496 ULONGEST memaddr, LONGEST len,
1497 struct bp_target_info *target_info,
1498 struct gdbarch *gdbarch)
1499 {
1500 /* Now do full processing of the found relevant range of elements. */
1501 CORE_ADDR bp_addr = 0;
1502 int bp_size = 0;
1503 int bptoffset = 0;
1504
1505 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1506 current_program_space->aspace, 0))
1507 {
1508 /* The breakpoint is inserted in a different address space. */
1509 return;
1510 }
1511
1512 /* Addresses and length of the part of the breakpoint that
1513 we need to copy. */
1514 bp_addr = target_info->placed_address;
1515 bp_size = target_info->shadow_len;
1516
1517 if (bp_addr + bp_size <= memaddr)
1518 {
1519 /* The breakpoint is entirely before the chunk of memory we are
1520 reading. */
1521 return;
1522 }
1523
1524 if (bp_addr >= memaddr + len)
1525 {
1526 /* The breakpoint is entirely after the chunk of memory we are
1527 reading. */
1528 return;
1529 }
1530
1531 /* Offset within shadow_contents. */
1532 if (bp_addr < memaddr)
1533 {
1534 /* Only copy the second part of the breakpoint. */
1535 bp_size -= memaddr - bp_addr;
1536 bptoffset = memaddr - bp_addr;
1537 bp_addr = memaddr;
1538 }
1539
1540 if (bp_addr + bp_size > memaddr + len)
1541 {
1542 /* Only copy the first part of the breakpoint. */
1543 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1544 }
1545
1546 if (readbuf != NULL)
1547 {
1548 /* Verify that the readbuf buffer does not overlap with the
1549 shadow_contents buffer. */
1550 gdb_assert (target_info->shadow_contents >= readbuf + len
1551 || readbuf >= (target_info->shadow_contents
1552 + target_info->shadow_len));
1553
1554 /* Update the read buffer with this inserted breakpoint's
1555 shadow. */
1556 memcpy (readbuf + bp_addr - memaddr,
1557 target_info->shadow_contents + bptoffset, bp_size);
1558 }
1559 else
1560 {
1561 const unsigned char *bp;
1562 CORE_ADDR addr = target_info->reqstd_address;
1563 int placed_size;
1564
1565 /* Update the shadow with what we want to write to memory. */
1566 memcpy (target_info->shadow_contents + bptoffset,
1567 writebuf_org + bp_addr - memaddr, bp_size);
1568
1569 /* Determine appropriate breakpoint contents and size for this
1570 address. */
1571 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
1572
1573 /* Update the final write buffer with this inserted
1574 breakpoint's INSN. */
1575 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1576 }
1577 }
1578
1579 /* Update BUF, which is LEN bytes read from the target address MEMADDR,
1580 by replacing any memory breakpoints with their shadowed contents.
1581
1582 If READBUF is not NULL, this buffer must not overlap with any of
1583 the breakpoint location's shadow_contents buffers. Otherwise,
1584 a failed assertion internal error will be raised.
1585
1586 The range of shadowed area by each bp_location is:
1587 bl->address - bp_location_placed_address_before_address_max
1588 up to bl->address + bp_location_shadow_len_after_address_max
1589 The range we were requested to resolve shadows for is:
1590 memaddr ... memaddr + len
1591 Thus the safe cutoff boundaries for performance optimization are
1592 memaddr + len <= (bl->address
1593 - bp_location_placed_address_before_address_max)
1594 and:
1595 bl->address + bp_location_shadow_len_after_address_max <= memaddr */
1596
1597 void
1598 breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1599 const gdb_byte *writebuf_org,
1600 ULONGEST memaddr, LONGEST len)
1601 {
1602 /* Left boundary, right boundary and median element of our binary
1603 search. */
1604 unsigned bc_l, bc_r, bc;
1605 size_t i;
1606
1607 /* Find BC_L which is a leftmost element which may affect BUF
1608 content. It is safe to report lower value but a failure to
1609 report higher one. */
1610
1611 bc_l = 0;
1612 bc_r = bp_location_count;
1613 while (bc_l + 1 < bc_r)
1614 {
1615 struct bp_location *bl;
1616
1617 bc = (bc_l + bc_r) / 2;
1618 bl = bp_location[bc];
1619
1620 /* Check first BL->ADDRESS will not overflow due to the added
1621 constant. Then advance the left boundary only if we are sure
1622 the BC element can in no way affect the BUF content (MEMADDR
1623 to MEMADDR + LEN range).
1624
1625 Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1626 offset so that we cannot miss a breakpoint with its shadow
1627 range tail still reaching MEMADDR. */
1628
1629 if ((bl->address + bp_location_shadow_len_after_address_max
1630 >= bl->address)
1631 && (bl->address + bp_location_shadow_len_after_address_max
1632 <= memaddr))
1633 bc_l = bc;
1634 else
1635 bc_r = bc;
1636 }
1637
1638 /* Due to the binary search above, we need to make sure we pick the
1639 first location that's at BC_L's address. E.g., if there are
1640 multiple locations at the same address, BC_L may end up pointing
1641 at a duplicate location, and miss the "master"/"inserted"
1642 location. Say, given locations L1, L2 and L3 at addresses A and
1643 B:
1644
1645 L1@A, L2@A, L3@B, ...
1646
1647 BC_L could end up pointing at location L2, while the "master"
1648 location could be L1. Since the `loc->inserted' flag is only set
1649 on "master" locations, we'd forget to restore the shadow of L1
1650 and L2. */
1651 while (bc_l > 0
1652 && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1653 bc_l--;
1654
1655 /* Now do full processing of the found relevant range of elements. */
1656
1657 for (bc = bc_l; bc < bp_location_count; bc++)
1658 {
1659 struct bp_location *bl = bp_location[bc];
1660 CORE_ADDR bp_addr = 0;
1661 int bp_size = 0;
1662 int bptoffset = 0;
1663
1664 /* bp_location array has BL->OWNER always non-NULL. */
1665 if (bl->owner->type == bp_none)
1666 warning (_("reading through apparently deleted breakpoint #%d?"),
1667 bl->owner->number);
1668
1669 /* Performance optimization: any further element can no longer affect BUF
1670 content. */
1671
1672 if (bl->address >= bp_location_placed_address_before_address_max
1673 && memaddr + len <= (bl->address
1674 - bp_location_placed_address_before_address_max))
1675 break;
1676
1677 if (!bp_location_has_shadow (bl))
1678 continue;
1679
1680 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1681 memaddr, len, &bl->target_info, bl->gdbarch);
1682 }
1683 }
1684
1685 \f
1686
1687 /* Return true if BPT is either a software breakpoint or a hardware
1688 breakpoint. */
1689
1690 int
1691 is_breakpoint (const struct breakpoint *bpt)
1692 {
1693 return (bpt->type == bp_breakpoint
1694 || bpt->type == bp_hardware_breakpoint
1695 || bpt->type == bp_dprintf);
1696 }
1697
1698 /* Return true if BPT is of any hardware watchpoint kind. */
1699
1700 static int
1701 is_hardware_watchpoint (const struct breakpoint *bpt)
1702 {
1703 return (bpt->type == bp_hardware_watchpoint
1704 || bpt->type == bp_read_watchpoint
1705 || bpt->type == bp_access_watchpoint);
1706 }
1707
1708 /* Return true if BPT is of any watchpoint kind, hardware or
1709 software. */
1710
1711 int
1712 is_watchpoint (const struct breakpoint *bpt)
1713 {
1714 return (is_hardware_watchpoint (bpt)
1715 || bpt->type == bp_watchpoint);
1716 }
1717
1718 /* Returns true if the current thread and its running state are safe
1719 to evaluate or update watchpoint B. Watchpoints on local
1720 expressions need to be evaluated in the context of the thread that
1721 was current when the watchpoint was created, and, that thread needs
1722 to be stopped to be able to select the correct frame context.
1723 Watchpoints on global expressions can be evaluated on any thread,
1724 and in any state. It is presently left to the target allowing
1725 memory accesses when threads are running. */
1726
1727 static int
1728 watchpoint_in_thread_scope (struct watchpoint *b)
1729 {
1730 return (b->base.pspace == current_program_space
1731 && (ptid_equal (b->watchpoint_thread, null_ptid)
1732 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1733 && !is_executing (inferior_ptid))));
1734 }
1735
1736 /* Set watchpoint B to disp_del_at_next_stop, even including its possible
1737 associated bp_watchpoint_scope breakpoint. */
1738
1739 static void
1740 watchpoint_del_at_next_stop (struct watchpoint *w)
1741 {
1742 struct breakpoint *b = &w->base;
1743
1744 if (b->related_breakpoint != b)
1745 {
1746 gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1747 gdb_assert (b->related_breakpoint->related_breakpoint == b);
1748 b->related_breakpoint->disposition = disp_del_at_next_stop;
1749 b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1750 b->related_breakpoint = b;
1751 }
1752 b->disposition = disp_del_at_next_stop;
1753 }
1754
1755 /* Extract a bitfield value from value VAL using the bit parameters contained in
1756 watchpoint W. */
1757
1758 static struct value *
1759 extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1760 {
1761 struct value *bit_val;
1762
1763 if (val == NULL)
1764 return NULL;
1765
1766 bit_val = allocate_value (value_type (val));
1767
1768 unpack_value_bitfield (bit_val,
1769 w->val_bitpos,
1770 w->val_bitsize,
1771 value_contents_for_printing (val),
1772 value_offset (val),
1773 val);
1774
1775 return bit_val;
1776 }
1777
1778 /* Allocate a dummy location and add it to B, which must be a software
1779 watchpoint. This is required because even if a software watchpoint
1780 is not watching any memory, bpstat_stop_status requires a location
1781 to be able to report stops. */
1782
1783 static void
1784 software_watchpoint_add_no_memory_location (struct breakpoint *b,
1785 struct program_space *pspace)
1786 {
1787 gdb_assert (b->type == bp_watchpoint && b->loc == NULL);
1788
1789 b->loc = allocate_bp_location (b);
1790 b->loc->pspace = pspace;
1791 b->loc->address = -1;
1792 b->loc->length = -1;
1793 }
1794
1795 /* Returns true if B is a software watchpoint that is not watching any
1796 memory (e.g., "watch $pc"). */
1797
1798 static int
1799 is_no_memory_software_watchpoint (struct breakpoint *b)
1800 {
1801 return (b->type == bp_watchpoint
1802 && b->loc != NULL
1803 && b->loc->next == NULL
1804 && b->loc->address == -1
1805 && b->loc->length == -1);
1806 }
1807
1808 /* Assuming that B is a watchpoint:
1809 - Reparse watchpoint expression, if REPARSE is non-zero
1810 - Evaluate expression and store the result in B->val
1811 - Evaluate the condition if there is one, and store the result
1812 in b->loc->cond.
1813 - Update the list of values that must be watched in B->loc.
1814
1815 If the watchpoint disposition is disp_del_at_next_stop, then do
1816 nothing. If this is local watchpoint that is out of scope, delete
1817 it.
1818
1819 Even with `set breakpoint always-inserted on' the watchpoints are
1820 removed + inserted on each stop here. Normal breakpoints must
1821 never be removed because they might be missed by a running thread
1822 when debugging in non-stop mode. On the other hand, hardware
1823 watchpoints (is_hardware_watchpoint; processed here) are specific
1824 to each LWP since they are stored in each LWP's hardware debug
1825 registers. Therefore, such LWP must be stopped first in order to
1826 be able to modify its hardware watchpoints.
1827
1828 Hardware watchpoints must be reset exactly once after being
1829 presented to the user. It cannot be done sooner, because it would
1830 reset the data used to present the watchpoint hit to the user. And
1831 it must not be done later because it could display the same single
1832 watchpoint hit during multiple GDB stops. Note that the latter is
1833 relevant only to the hardware watchpoint types bp_read_watchpoint
1834 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1835 not user-visible - its hit is suppressed if the memory content has
1836 not changed.
1837
1838 The following constraints influence the location where we can reset
1839 hardware watchpoints:
1840
1841 * target_stopped_by_watchpoint and target_stopped_data_address are
1842 called several times when GDB stops.
1843
1844 [linux]
1845 * Multiple hardware watchpoints can be hit at the same time,
1846 causing GDB to stop. GDB only presents one hardware watchpoint
1847 hit at a time as the reason for stopping, and all the other hits
1848 are presented later, one after the other, each time the user
1849 requests the execution to be resumed. Execution is not resumed
1850 for the threads still having pending hit event stored in
1851 LWP_INFO->STATUS. While the watchpoint is already removed from
1852 the inferior on the first stop the thread hit event is kept being
1853 reported from its cached value by linux_nat_stopped_data_address
1854 until the real thread resume happens after the watchpoint gets
1855 presented and thus its LWP_INFO->STATUS gets reset.
1856
1857 Therefore the hardware watchpoint hit can get safely reset on the
1858 watchpoint removal from inferior. */
1859
1860 static void
1861 update_watchpoint (struct watchpoint *b, int reparse)
1862 {
1863 int within_current_scope;
1864 struct frame_id saved_frame_id;
1865 int frame_saved;
1866
1867 /* If this is a local watchpoint, we only want to check if the
1868 watchpoint frame is in scope if the current thread is the thread
1869 that was used to create the watchpoint. */
1870 if (!watchpoint_in_thread_scope (b))
1871 return;
1872
1873 if (b->base.disposition == disp_del_at_next_stop)
1874 return;
1875
1876 frame_saved = 0;
1877
1878 /* Determine if the watchpoint is within scope. */
1879 if (b->exp_valid_block == NULL)
1880 within_current_scope = 1;
1881 else
1882 {
1883 struct frame_info *fi = get_current_frame ();
1884 struct gdbarch *frame_arch = get_frame_arch (fi);
1885 CORE_ADDR frame_pc = get_frame_pc (fi);
1886
1887 /* If we're at a point where the stack has been destroyed
1888 (e.g. in a function epilogue), unwinding may not work
1889 properly. Do not attempt to recreate locations at this
1890 point. See similar comments in watchpoint_check. */
1891 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
1892 return;
1893
1894 /* Save the current frame's ID so we can restore it after
1895 evaluating the watchpoint expression on its own frame. */
1896 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1897 took a frame parameter, so that we didn't have to change the
1898 selected frame. */
1899 frame_saved = 1;
1900 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1901
1902 fi = frame_find_by_id (b->watchpoint_frame);
1903 within_current_scope = (fi != NULL);
1904 if (within_current_scope)
1905 select_frame (fi);
1906 }
1907
1908 /* We don't free locations. They are stored in the bp_location array
1909 and update_global_location_list will eventually delete them and
1910 remove breakpoints if needed. */
1911 b->base.loc = NULL;
1912
1913 if (within_current_scope && reparse)
1914 {
1915 const char *s;
1916
1917 if (b->exp)
1918 {
1919 xfree (b->exp);
1920 b->exp = NULL;
1921 }
1922 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1923 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
1924 /* If the meaning of expression itself changed, the old value is
1925 no longer relevant. We don't want to report a watchpoint hit
1926 to the user when the old value and the new value may actually
1927 be completely different objects. */
1928 value_free (b->val);
1929 b->val = NULL;
1930 b->val_valid = 0;
1931
1932 /* Note that unlike with breakpoints, the watchpoint's condition
1933 expression is stored in the breakpoint object, not in the
1934 locations (re)created below. */
1935 if (b->base.cond_string != NULL)
1936 {
1937 if (b->cond_exp != NULL)
1938 {
1939 xfree (b->cond_exp);
1940 b->cond_exp = NULL;
1941 }
1942
1943 s = b->base.cond_string;
1944 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
1945 }
1946 }
1947
1948 /* If we failed to parse the expression, for example because
1949 it refers to a global variable in a not-yet-loaded shared library,
1950 don't try to insert watchpoint. We don't automatically delete
1951 such watchpoint, though, since failure to parse expression
1952 is different from out-of-scope watchpoint. */
1953 if (!target_has_execution)
1954 {
1955 /* Without execution, memory can't change. No use to try and
1956 set watchpoint locations. The watchpoint will be reset when
1957 the target gains execution, through breakpoint_re_set. */
1958 if (!can_use_hw_watchpoints)
1959 {
1960 if (b->base.ops->works_in_software_mode (&b->base))
1961 b->base.type = bp_watchpoint;
1962 else
1963 error (_("Can't set read/access watchpoint when "
1964 "hardware watchpoints are disabled."));
1965 }
1966 }
1967 else if (within_current_scope && b->exp)
1968 {
1969 int pc = 0;
1970 struct value *val_chain, *v, *result, *next;
1971 struct program_space *frame_pspace;
1972
1973 fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain, 0);
1974
1975 /* Avoid setting b->val if it's already set. The meaning of
1976 b->val is 'the last value' user saw, and we should update
1977 it only if we reported that last value to user. As it
1978 happens, the code that reports it updates b->val directly.
1979 We don't keep track of the memory value for masked
1980 watchpoints. */
1981 if (!b->val_valid && !is_masked_watchpoint (&b->base))
1982 {
1983 if (b->val_bitsize != 0)
1984 {
1985 v = extract_bitfield_from_watchpoint_value (b, v);
1986 if (v != NULL)
1987 release_value (v);
1988 }
1989 b->val = v;
1990 b->val_valid = 1;
1991 }
1992
1993 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1994
1995 /* Look at each value on the value chain. */
1996 for (v = val_chain; v; v = value_next (v))
1997 {
1998 /* If it's a memory location, and GDB actually needed
1999 its contents to evaluate the expression, then we
2000 must watch it. If the first value returned is
2001 still lazy, that means an error occurred reading it;
2002 watch it anyway in case it becomes readable. */
2003 if (VALUE_LVAL (v) == lval_memory
2004 && (v == val_chain || ! value_lazy (v)))
2005 {
2006 struct type *vtype = check_typedef (value_type (v));
2007
2008 /* We only watch structs and arrays if user asked
2009 for it explicitly, never if they just happen to
2010 appear in the middle of some value chain. */
2011 if (v == result
2012 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
2013 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
2014 {
2015 CORE_ADDR addr;
2016 enum target_hw_bp_type type;
2017 struct bp_location *loc, **tmp;
2018 int bitpos = 0, bitsize = 0;
2019
2020 if (value_bitsize (v) != 0)
2021 {
2022 /* Extract the bit parameters out from the bitfield
2023 sub-expression. */
2024 bitpos = value_bitpos (v);
2025 bitsize = value_bitsize (v);
2026 }
2027 else if (v == result && b->val_bitsize != 0)
2028 {
2029 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
2030 lvalue whose bit parameters are saved in the fields
2031 VAL_BITPOS and VAL_BITSIZE. */
2032 bitpos = b->val_bitpos;
2033 bitsize = b->val_bitsize;
2034 }
2035
2036 addr = value_address (v);
2037 if (bitsize != 0)
2038 {
2039 /* Skip the bytes that don't contain the bitfield. */
2040 addr += bitpos / 8;
2041 }
2042
2043 type = hw_write;
2044 if (b->base.type == bp_read_watchpoint)
2045 type = hw_read;
2046 else if (b->base.type == bp_access_watchpoint)
2047 type = hw_access;
2048
2049 loc = allocate_bp_location (&b->base);
2050 for (tmp = &(b->base.loc); *tmp != NULL; tmp = &((*tmp)->next))
2051 ;
2052 *tmp = loc;
2053 loc->gdbarch = get_type_arch (value_type (v));
2054
2055 loc->pspace = frame_pspace;
2056 loc->address = addr;
2057
2058 if (bitsize != 0)
2059 {
2060 /* Just cover the bytes that make up the bitfield. */
2061 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2062 }
2063 else
2064 loc->length = TYPE_LENGTH (value_type (v));
2065
2066 loc->watchpoint_type = type;
2067 }
2068 }
2069 }
2070
2071 /* Change the type of breakpoint between hardware assisted or
2072 an ordinary watchpoint depending on the hardware support
2073 and free hardware slots. REPARSE is set when the inferior
2074 is started. */
2075 if (reparse)
2076 {
2077 int reg_cnt;
2078 enum bp_loc_type loc_type;
2079 struct bp_location *bl;
2080
2081 reg_cnt = can_use_hardware_watchpoint (val_chain);
2082
2083 if (reg_cnt)
2084 {
2085 int i, target_resources_ok, other_type_used;
2086 enum bptype type;
2087
2088 /* Use an exact watchpoint when there's only one memory region to be
2089 watched, and only one debug register is needed to watch it. */
2090 b->exact = target_exact_watchpoints && reg_cnt == 1;
2091
2092 /* We need to determine how many resources are already
2093 used for all other hardware watchpoints plus this one
2094 to see if we still have enough resources to also fit
2095 this watchpoint in as well. */
2096
2097 /* If this is a software watchpoint, we try to turn it
2098 to a hardware one -- count resources as if B was of
2099 hardware watchpoint type. */
2100 type = b->base.type;
2101 if (type == bp_watchpoint)
2102 type = bp_hardware_watchpoint;
2103
2104 /* This watchpoint may or may not have been placed on
2105 the list yet at this point (it won't be in the list
2106 if we're trying to create it for the first time,
2107 through watch_command), so always account for it
2108 manually. */
2109
2110 /* Count resources used by all watchpoints except B. */
2111 i = hw_watchpoint_used_count_others (&b->base, type, &other_type_used);
2112
2113 /* Add in the resources needed for B. */
2114 i += hw_watchpoint_use_count (&b->base);
2115
2116 target_resources_ok
2117 = target_can_use_hardware_watchpoint (type, i, other_type_used);
2118 if (target_resources_ok <= 0)
2119 {
2120 int sw_mode = b->base.ops->works_in_software_mode (&b->base);
2121
2122 if (target_resources_ok == 0 && !sw_mode)
2123 error (_("Target does not support this type of "
2124 "hardware watchpoint."));
2125 else if (target_resources_ok < 0 && !sw_mode)
2126 error (_("There are not enough available hardware "
2127 "resources for this watchpoint."));
2128
2129 /* Downgrade to software watchpoint. */
2130 b->base.type = bp_watchpoint;
2131 }
2132 else
2133 {
2134 /* If this was a software watchpoint, we've just
2135 found we have enough resources to turn it to a
2136 hardware watchpoint. Otherwise, this is a
2137 nop. */
2138 b->base.type = type;
2139 }
2140 }
2141 else if (!b->base.ops->works_in_software_mode (&b->base))
2142 {
2143 if (!can_use_hw_watchpoints)
2144 error (_("Can't set read/access watchpoint when "
2145 "hardware watchpoints are disabled."));
2146 else
2147 error (_("Expression cannot be implemented with "
2148 "read/access watchpoint."));
2149 }
2150 else
2151 b->base.type = bp_watchpoint;
2152
2153 loc_type = (b->base.type == bp_watchpoint? bp_loc_other
2154 : bp_loc_hardware_watchpoint);
2155 for (bl = b->base.loc; bl; bl = bl->next)
2156 bl->loc_type = loc_type;
2157 }
2158
2159 for (v = val_chain; v; v = next)
2160 {
2161 next = value_next (v);
2162 if (v != b->val)
2163 value_free (v);
2164 }
2165
2166 /* If a software watchpoint is not watching any memory, then the
2167 above left it without any location set up. But,
2168 bpstat_stop_status requires a location to be able to report
2169 stops, so make sure there's at least a dummy one. */
2170 if (b->base.type == bp_watchpoint && b->base.loc == NULL)
2171 software_watchpoint_add_no_memory_location (&b->base, frame_pspace);
2172 }
2173 else if (!within_current_scope)
2174 {
2175 printf_filtered (_("\
2176 Watchpoint %d deleted because the program has left the block\n\
2177 in which its expression is valid.\n"),
2178 b->base.number);
2179 watchpoint_del_at_next_stop (b);
2180 }
2181
2182 /* Restore the selected frame. */
2183 if (frame_saved)
2184 select_frame (frame_find_by_id (saved_frame_id));
2185 }
2186
2187
2188 /* Returns 1 iff breakpoint location should be
2189 inserted in the inferior. We don't differentiate the type of BL's owner
2190 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2191 breakpoint_ops is not defined, because in insert_bp_location,
2192 tracepoint's insert_location will not be called. */
2193 static int
2194 should_be_inserted (struct bp_location *bl)
2195 {
2196 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
2197 return 0;
2198
2199 if (bl->owner->disposition == disp_del_at_next_stop)
2200 return 0;
2201
2202 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
2203 return 0;
2204
2205 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2206 return 0;
2207
2208 /* This is set for example, when we're attached to the parent of a
2209 vfork, and have detached from the child. The child is running
2210 free, and we expect it to do an exec or exit, at which point the
2211 OS makes the parent schedulable again (and the target reports
2212 that the vfork is done). Until the child is done with the shared
2213 memory region, do not insert breakpoints in the parent, otherwise
2214 the child could still trip on the parent's breakpoints. Since
2215 the parent is blocked anyway, it won't miss any breakpoint. */
2216 if (bl->pspace->breakpoints_not_allowed)
2217 return 0;
2218
2219 /* Don't insert a breakpoint if we're trying to step past its
2220 location. */
2221 if ((bl->loc_type == bp_loc_software_breakpoint
2222 || bl->loc_type == bp_loc_hardware_breakpoint)
2223 && stepping_past_instruction_at (bl->pspace->aspace,
2224 bl->address))
2225 {
2226 if (debug_infrun)
2227 {
2228 fprintf_unfiltered (gdb_stdlog,
2229 "infrun: skipping breakpoint: "
2230 "stepping past insn at: %s\n",
2231 paddress (bl->gdbarch, bl->address));
2232 }
2233 return 0;
2234 }
2235
2236 /* Don't insert watchpoints if we're trying to step past the
2237 instruction that triggered one. */
2238 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2239 && stepping_past_nonsteppable_watchpoint ())
2240 {
2241 if (debug_infrun)
2242 {
2243 fprintf_unfiltered (gdb_stdlog,
2244 "infrun: stepping past non-steppable watchpoint. "
2245 "skipping watchpoint at %s:%d\n",
2246 paddress (bl->gdbarch, bl->address),
2247 bl->length);
2248 }
2249 return 0;
2250 }
2251
2252 return 1;
2253 }
2254
2255 /* Same as should_be_inserted but does the check assuming
2256 that the location is not duplicated. */
2257
2258 static int
2259 unduplicated_should_be_inserted (struct bp_location *bl)
2260 {
2261 int result;
2262 const int save_duplicate = bl->duplicate;
2263
2264 bl->duplicate = 0;
2265 result = should_be_inserted (bl);
2266 bl->duplicate = save_duplicate;
2267 return result;
2268 }
2269
2270 /* Parses a conditional described by an expression COND into an
2271 agent expression bytecode suitable for evaluation
2272 by the bytecode interpreter. Return NULL if there was
2273 any error during parsing. */
2274
2275 static struct agent_expr *
2276 parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2277 {
2278 struct agent_expr *aexpr = NULL;
2279
2280 if (!cond)
2281 return NULL;
2282
2283 /* We don't want to stop processing, so catch any errors
2284 that may show up. */
2285 TRY
2286 {
2287 aexpr = gen_eval_for_expr (scope, cond);
2288 }
2289
2290 CATCH (ex, RETURN_MASK_ERROR)
2291 {
2292 /* If we got here, it means the condition could not be parsed to a valid
2293 bytecode expression and thus can't be evaluated on the target's side.
2294 It's no use iterating through the conditions. */
2295 return NULL;
2296 }
2297 END_CATCH
2298
2299 /* We have a valid agent expression. */
2300 return aexpr;
2301 }
2302
2303 /* Based on location BL, create a list of breakpoint conditions to be
2304 passed on to the target. If we have duplicated locations with different
2305 conditions, we will add such conditions to the list. The idea is that the
2306 target will evaluate the list of conditions and will only notify GDB when
2307 one of them is true. */
2308
2309 static void
2310 build_target_condition_list (struct bp_location *bl)
2311 {
2312 struct bp_location **locp = NULL, **loc2p;
2313 int null_condition_or_parse_error = 0;
2314 int modified = bl->needs_update;
2315 struct bp_location *loc;
2316
2317 /* Release conditions left over from a previous insert. */
2318 VEC_free (agent_expr_p, bl->target_info.conditions);
2319
2320 /* This is only meaningful if the target is
2321 evaluating conditions and if the user has
2322 opted for condition evaluation on the target's
2323 side. */
2324 if (gdb_evaluates_breakpoint_condition_p ()
2325 || !target_supports_evaluation_of_breakpoint_conditions ())
2326 return;
2327
2328 /* Do a first pass to check for locations with no assigned
2329 conditions or conditions that fail to parse to a valid agent expression
2330 bytecode. If any of these happen, then it's no use to send conditions
2331 to the target since this location will always trigger and generate a
2332 response back to GDB. */
2333 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2334 {
2335 loc = (*loc2p);
2336 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2337 {
2338 if (modified)
2339 {
2340 struct agent_expr *aexpr;
2341
2342 /* Re-parse the conditions since something changed. In that
2343 case we already freed the condition bytecodes (see
2344 force_breakpoint_reinsertion). We just
2345 need to parse the condition to bytecodes again. */
2346 aexpr = parse_cond_to_aexpr (bl->address, loc->cond);
2347 loc->cond_bytecode = aexpr;
2348
2349 /* Check if we managed to parse the conditional expression
2350 correctly. If not, we will not send this condition
2351 to the target. */
2352 if (aexpr)
2353 continue;
2354 }
2355
2356 /* If we have a NULL bytecode expression, it means something
2357 went wrong or we have a null condition expression. */
2358 if (!loc->cond_bytecode)
2359 {
2360 null_condition_or_parse_error = 1;
2361 break;
2362 }
2363 }
2364 }
2365
2366 /* If any of these happened, it means we will have to evaluate the conditions
2367 for the location's address on gdb's side. It is no use keeping bytecodes
2368 for all the other duplicate locations, thus we free all of them here.
2369
2370 This is so we have a finer control over which locations' conditions are
2371 being evaluated by GDB or the remote stub. */
2372 if (null_condition_or_parse_error)
2373 {
2374 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2375 {
2376 loc = (*loc2p);
2377 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2378 {
2379 /* Only go as far as the first NULL bytecode is
2380 located. */
2381 if (!loc->cond_bytecode)
2382 return;
2383
2384 free_agent_expr (loc->cond_bytecode);
2385 loc->cond_bytecode = NULL;
2386 }
2387 }
2388 }
2389
2390 /* No NULL conditions or failed bytecode generation. Build a condition list
2391 for this location's address. */
2392 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2393 {
2394 loc = (*loc2p);
2395 if (loc->cond
2396 && is_breakpoint (loc->owner)
2397 && loc->pspace->num == bl->pspace->num
2398 && loc->owner->enable_state == bp_enabled
2399 && loc->enabled)
2400 /* Add the condition to the vector. This will be used later to send the
2401 conditions to the target. */
2402 VEC_safe_push (agent_expr_p, bl->target_info.conditions,
2403 loc->cond_bytecode);
2404 }
2405
2406 return;
2407 }
2408
2409 /* Parses a command described by string CMD into an agent expression
2410 bytecode suitable for evaluation by the bytecode interpreter.
2411 Return NULL if there was any error during parsing. */
2412
2413 static struct agent_expr *
2414 parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2415 {
2416 struct cleanup *old_cleanups = 0;
2417 struct expression *expr, **argvec;
2418 struct agent_expr *aexpr = NULL;
2419 const char *cmdrest;
2420 const char *format_start, *format_end;
2421 struct format_piece *fpieces;
2422 int nargs;
2423 struct gdbarch *gdbarch = get_current_arch ();
2424
2425 if (!cmd)
2426 return NULL;
2427
2428 cmdrest = cmd;
2429
2430 if (*cmdrest == ',')
2431 ++cmdrest;
2432 cmdrest = skip_spaces_const (cmdrest);
2433
2434 if (*cmdrest++ != '"')
2435 error (_("No format string following the location"));
2436
2437 format_start = cmdrest;
2438
2439 fpieces = parse_format_string (&cmdrest);
2440
2441 old_cleanups = make_cleanup (free_format_pieces_cleanup, &fpieces);
2442
2443 format_end = cmdrest;
2444
2445 if (*cmdrest++ != '"')
2446 error (_("Bad format string, non-terminated '\"'."));
2447
2448 cmdrest = skip_spaces_const (cmdrest);
2449
2450 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2451 error (_("Invalid argument syntax"));
2452
2453 if (*cmdrest == ',')
2454 cmdrest++;
2455 cmdrest = skip_spaces_const (cmdrest);
2456
2457 /* For each argument, make an expression. */
2458
2459 argvec = (struct expression **) alloca (strlen (cmd)
2460 * sizeof (struct expression *));
2461
2462 nargs = 0;
2463 while (*cmdrest != '\0')
2464 {
2465 const char *cmd1;
2466
2467 cmd1 = cmdrest;
2468 expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2469 argvec[nargs++] = expr;
2470 cmdrest = cmd1;
2471 if (*cmdrest == ',')
2472 ++cmdrest;
2473 }
2474
2475 /* We don't want to stop processing, so catch any errors
2476 that may show up. */
2477 TRY
2478 {
2479 aexpr = gen_printf (scope, gdbarch, 0, 0,
2480 format_start, format_end - format_start,
2481 fpieces, nargs, argvec);
2482 }
2483 CATCH (ex, RETURN_MASK_ERROR)
2484 {
2485 /* If we got here, it means the command could not be parsed to a valid
2486 bytecode expression and thus can't be evaluated on the target's side.
2487 It's no use iterating through the other commands. */
2488 aexpr = NULL;
2489 }
2490 END_CATCH
2491
2492 do_cleanups (old_cleanups);
2493
2494 /* We have a valid agent expression, return it. */
2495 return aexpr;
2496 }
2497
2498 /* Based on location BL, create a list of breakpoint commands to be
2499 passed on to the target. If we have duplicated locations with
2500 different commands, we will add any such to the list. */
2501
2502 static void
2503 build_target_command_list (struct bp_location *bl)
2504 {
2505 struct bp_location **locp = NULL, **loc2p;
2506 int null_command_or_parse_error = 0;
2507 int modified = bl->needs_update;
2508 struct bp_location *loc;
2509
2510 /* Release commands left over from a previous insert. */
2511 VEC_free (agent_expr_p, bl->target_info.tcommands);
2512
2513 if (!target_can_run_breakpoint_commands ())
2514 return;
2515
2516 /* For now, limit to agent-style dprintf breakpoints. */
2517 if (dprintf_style != dprintf_style_agent)
2518 return;
2519
2520 /* For now, if we have any duplicate location that isn't a dprintf,
2521 don't install the target-side commands, as that would make the
2522 breakpoint not be reported to the core, and we'd lose
2523 control. */
2524 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2525 {
2526 loc = (*loc2p);
2527 if (is_breakpoint (loc->owner)
2528 && loc->pspace->num == bl->pspace->num
2529 && loc->owner->type != bp_dprintf)
2530 return;
2531 }
2532
2533 /* Do a first pass to check for locations with no assigned
2534 conditions or conditions that fail to parse to a valid agent expression
2535 bytecode. If any of these happen, then it's no use to send conditions
2536 to the target since this location will always trigger and generate a
2537 response back to GDB. */
2538 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2539 {
2540 loc = (*loc2p);
2541 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2542 {
2543 if (modified)
2544 {
2545 struct agent_expr *aexpr;
2546
2547 /* Re-parse the commands since something changed. In that
2548 case we already freed the command bytecodes (see
2549 force_breakpoint_reinsertion). We just
2550 need to parse the command to bytecodes again. */
2551 aexpr = parse_cmd_to_aexpr (bl->address,
2552 loc->owner->extra_string);
2553 loc->cmd_bytecode = aexpr;
2554
2555 if (!aexpr)
2556 continue;
2557 }
2558
2559 /* If we have a NULL bytecode expression, it means something
2560 went wrong or we have a null command expression. */
2561 if (!loc->cmd_bytecode)
2562 {
2563 null_command_or_parse_error = 1;
2564 break;
2565 }
2566 }
2567 }
2568
2569 /* If anything failed, then we're not doing target-side commands,
2570 and so clean up. */
2571 if (null_command_or_parse_error)
2572 {
2573 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2574 {
2575 loc = (*loc2p);
2576 if (is_breakpoint (loc->owner)
2577 && loc->pspace->num == bl->pspace->num)
2578 {
2579 /* Only go as far as the first NULL bytecode is
2580 located. */
2581 if (loc->cmd_bytecode == NULL)
2582 return;
2583
2584 free_agent_expr (loc->cmd_bytecode);
2585 loc->cmd_bytecode = NULL;
2586 }
2587 }
2588 }
2589
2590 /* No NULL commands or failed bytecode generation. Build a command list
2591 for this location's address. */
2592 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2593 {
2594 loc = (*loc2p);
2595 if (loc->owner->extra_string
2596 && is_breakpoint (loc->owner)
2597 && loc->pspace->num == bl->pspace->num
2598 && loc->owner->enable_state == bp_enabled
2599 && loc->enabled)
2600 /* Add the command to the vector. This will be used later
2601 to send the commands to the target. */
2602 VEC_safe_push (agent_expr_p, bl->target_info.tcommands,
2603 loc->cmd_bytecode);
2604 }
2605
2606 bl->target_info.persist = 0;
2607 /* Maybe flag this location as persistent. */
2608 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2609 bl->target_info.persist = 1;
2610 }
2611
2612 /* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2613 location. Any error messages are printed to TMP_ERROR_STREAM; and
2614 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
2615 Returns 0 for success, 1 if the bp_location type is not supported or
2616 -1 for failure.
2617
2618 NOTE drow/2003-09-09: This routine could be broken down to an
2619 object-style method for each breakpoint or catchpoint type. */
2620 static int
2621 insert_bp_location (struct bp_location *bl,
2622 struct ui_file *tmp_error_stream,
2623 int *disabled_breaks,
2624 int *hw_breakpoint_error,
2625 int *hw_bp_error_explained_already)
2626 {
2627 enum errors bp_err = GDB_NO_ERROR;
2628 const char *bp_err_message = NULL;
2629
2630 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2631 return 0;
2632
2633 /* Note we don't initialize bl->target_info, as that wipes out
2634 the breakpoint location's shadow_contents if the breakpoint
2635 is still inserted at that location. This in turn breaks
2636 target_read_memory which depends on these buffers when
2637 a memory read is requested at the breakpoint location:
2638 Once the target_info has been wiped, we fail to see that
2639 we have a breakpoint inserted at that address and thus
2640 read the breakpoint instead of returning the data saved in
2641 the breakpoint location's shadow contents. */
2642 bl->target_info.reqstd_address = bl->address;
2643 bl->target_info.placed_address_space = bl->pspace->aspace;
2644 bl->target_info.length = bl->length;
2645
2646 /* When working with target-side conditions, we must pass all the conditions
2647 for the same breakpoint address down to the target since GDB will not
2648 insert those locations. With a list of breakpoint conditions, the target
2649 can decide when to stop and notify GDB. */
2650
2651 if (is_breakpoint (bl->owner))
2652 {
2653 build_target_condition_list (bl);
2654 build_target_command_list (bl);
2655 /* Reset the modification marker. */
2656 bl->needs_update = 0;
2657 }
2658
2659 if (bl->loc_type == bp_loc_software_breakpoint
2660 || bl->loc_type == bp_loc_hardware_breakpoint)
2661 {
2662 if (bl->owner->type != bp_hardware_breakpoint)
2663 {
2664 /* If the explicitly specified breakpoint type
2665 is not hardware breakpoint, check the memory map to see
2666 if the breakpoint address is in read only memory or not.
2667
2668 Two important cases are:
2669 - location type is not hardware breakpoint, memory
2670 is readonly. We change the type of the location to
2671 hardware breakpoint.
2672 - location type is hardware breakpoint, memory is
2673 read-write. This means we've previously made the
2674 location hardware one, but then the memory map changed,
2675 so we undo.
2676
2677 When breakpoints are removed, remove_breakpoints will use
2678 location types we've just set here, the only possible
2679 problem is that memory map has changed during running
2680 program, but it's not going to work anyway with current
2681 gdb. */
2682 struct mem_region *mr
2683 = lookup_mem_region (bl->target_info.reqstd_address);
2684
2685 if (mr)
2686 {
2687 if (automatic_hardware_breakpoints)
2688 {
2689 enum bp_loc_type new_type;
2690
2691 if (mr->attrib.mode != MEM_RW)
2692 new_type = bp_loc_hardware_breakpoint;
2693 else
2694 new_type = bp_loc_software_breakpoint;
2695
2696 if (new_type != bl->loc_type)
2697 {
2698 static int said = 0;
2699
2700 bl->loc_type = new_type;
2701 if (!said)
2702 {
2703 fprintf_filtered (gdb_stdout,
2704 _("Note: automatically using "
2705 "hardware breakpoints for "
2706 "read-only addresses.\n"));
2707 said = 1;
2708 }
2709 }
2710 }
2711 else if (bl->loc_type == bp_loc_software_breakpoint
2712 && mr->attrib.mode != MEM_RW)
2713 {
2714 fprintf_unfiltered (tmp_error_stream,
2715 _("Cannot insert breakpoint %d.\n"
2716 "Cannot set software breakpoint "
2717 "at read-only address %s\n"),
2718 bl->owner->number,
2719 paddress (bl->gdbarch, bl->address));
2720 return 1;
2721 }
2722 }
2723 }
2724
2725 /* First check to see if we have to handle an overlay. */
2726 if (overlay_debugging == ovly_off
2727 || bl->section == NULL
2728 || !(section_is_overlay (bl->section)))
2729 {
2730 /* No overlay handling: just set the breakpoint. */
2731 TRY
2732 {
2733 int val;
2734
2735 val = bl->owner->ops->insert_location (bl);
2736 if (val)
2737 bp_err = GENERIC_ERROR;
2738 }
2739 CATCH (e, RETURN_MASK_ALL)
2740 {
2741 bp_err = e.error;
2742 bp_err_message = e.message;
2743 }
2744 END_CATCH
2745 }
2746 else
2747 {
2748 /* This breakpoint is in an overlay section.
2749 Shall we set a breakpoint at the LMA? */
2750 if (!overlay_events_enabled)
2751 {
2752 /* Yes -- overlay event support is not active,
2753 so we must try to set a breakpoint at the LMA.
2754 This will not work for a hardware breakpoint. */
2755 if (bl->loc_type == bp_loc_hardware_breakpoint)
2756 warning (_("hardware breakpoint %d not supported in overlay!"),
2757 bl->owner->number);
2758 else
2759 {
2760 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2761 bl->section);
2762 /* Set a software (trap) breakpoint at the LMA. */
2763 bl->overlay_target_info = bl->target_info;
2764 bl->overlay_target_info.reqstd_address = addr;
2765
2766 /* No overlay handling: just set the breakpoint. */
2767 TRY
2768 {
2769 int val;
2770
2771 val = target_insert_breakpoint (bl->gdbarch,
2772 &bl->overlay_target_info);
2773 if (val)
2774 bp_err = GENERIC_ERROR;
2775 }
2776 CATCH (e, RETURN_MASK_ALL)
2777 {
2778 bp_err = e.error;
2779 bp_err_message = e.message;
2780 }
2781 END_CATCH
2782
2783 if (bp_err != GDB_NO_ERROR)
2784 fprintf_unfiltered (tmp_error_stream,
2785 "Overlay breakpoint %d "
2786 "failed: in ROM?\n",
2787 bl->owner->number);
2788 }
2789 }
2790 /* Shall we set a breakpoint at the VMA? */
2791 if (section_is_mapped (bl->section))
2792 {
2793 /* Yes. This overlay section is mapped into memory. */
2794 TRY
2795 {
2796 int val;
2797
2798 val = bl->owner->ops->insert_location (bl);
2799 if (val)
2800 bp_err = GENERIC_ERROR;
2801 }
2802 CATCH (e, RETURN_MASK_ALL)
2803 {
2804 bp_err = e.error;
2805 bp_err_message = e.message;
2806 }
2807 END_CATCH
2808 }
2809 else
2810 {
2811 /* No. This breakpoint will not be inserted.
2812 No error, but do not mark the bp as 'inserted'. */
2813 return 0;
2814 }
2815 }
2816
2817 if (bp_err != GDB_NO_ERROR)
2818 {
2819 /* Can't set the breakpoint. */
2820
2821 /* In some cases, we might not be able to insert a
2822 breakpoint in a shared library that has already been
2823 removed, but we have not yet processed the shlib unload
2824 event. Unfortunately, some targets that implement
2825 breakpoint insertion themselves can't tell why the
2826 breakpoint insertion failed (e.g., the remote target
2827 doesn't define error codes), so we must treat generic
2828 errors as memory errors. */
2829 if ((bp_err == GENERIC_ERROR || bp_err == MEMORY_ERROR)
2830 && bl->loc_type == bp_loc_software_breakpoint
2831 && (solib_name_from_address (bl->pspace, bl->address)
2832 || shared_objfile_contains_address_p (bl->pspace,
2833 bl->address)))
2834 {
2835 /* See also: disable_breakpoints_in_shlibs. */
2836 bl->shlib_disabled = 1;
2837 observer_notify_breakpoint_modified (bl->owner);
2838 if (!*disabled_breaks)
2839 {
2840 fprintf_unfiltered (tmp_error_stream,
2841 "Cannot insert breakpoint %d.\n",
2842 bl->owner->number);
2843 fprintf_unfiltered (tmp_error_stream,
2844 "Temporarily disabling shared "
2845 "library breakpoints:\n");
2846 }
2847 *disabled_breaks = 1;
2848 fprintf_unfiltered (tmp_error_stream,
2849 "breakpoint #%d\n", bl->owner->number);
2850 return 0;
2851 }
2852 else
2853 {
2854 if (bl->loc_type == bp_loc_hardware_breakpoint)
2855 {
2856 *hw_breakpoint_error = 1;
2857 *hw_bp_error_explained_already = bp_err_message != NULL;
2858 fprintf_unfiltered (tmp_error_stream,
2859 "Cannot insert hardware breakpoint %d%s",
2860 bl->owner->number, bp_err_message ? ":" : ".\n");
2861 if (bp_err_message != NULL)
2862 fprintf_unfiltered (tmp_error_stream, "%s.\n", bp_err_message);
2863 }
2864 else
2865 {
2866 if (bp_err_message == NULL)
2867 {
2868 char *message
2869 = memory_error_message (TARGET_XFER_E_IO,
2870 bl->gdbarch, bl->address);
2871 struct cleanup *old_chain = make_cleanup (xfree, message);
2872
2873 fprintf_unfiltered (tmp_error_stream,
2874 "Cannot insert breakpoint %d.\n"
2875 "%s\n",
2876 bl->owner->number, message);
2877 do_cleanups (old_chain);
2878 }
2879 else
2880 {
2881 fprintf_unfiltered (tmp_error_stream,
2882 "Cannot insert breakpoint %d: %s\n",
2883 bl->owner->number,
2884 bp_err_message);
2885 }
2886 }
2887 return 1;
2888
2889 }
2890 }
2891 else
2892 bl->inserted = 1;
2893
2894 return 0;
2895 }
2896
2897 else if (bl->loc_type == bp_loc_hardware_watchpoint
2898 /* NOTE drow/2003-09-08: This state only exists for removing
2899 watchpoints. It's not clear that it's necessary... */
2900 && bl->owner->disposition != disp_del_at_next_stop)
2901 {
2902 int val;
2903
2904 gdb_assert (bl->owner->ops != NULL
2905 && bl->owner->ops->insert_location != NULL);
2906
2907 val = bl->owner->ops->insert_location (bl);
2908
2909 /* If trying to set a read-watchpoint, and it turns out it's not
2910 supported, try emulating one with an access watchpoint. */
2911 if (val == 1 && bl->watchpoint_type == hw_read)
2912 {
2913 struct bp_location *loc, **loc_temp;
2914
2915 /* But don't try to insert it, if there's already another
2916 hw_access location that would be considered a duplicate
2917 of this one. */
2918 ALL_BP_LOCATIONS (loc, loc_temp)
2919 if (loc != bl
2920 && loc->watchpoint_type == hw_access
2921 && watchpoint_locations_match (bl, loc))
2922 {
2923 bl->duplicate = 1;
2924 bl->inserted = 1;
2925 bl->target_info = loc->target_info;
2926 bl->watchpoint_type = hw_access;
2927 val = 0;
2928 break;
2929 }
2930
2931 if (val == 1)
2932 {
2933 bl->watchpoint_type = hw_access;
2934 val = bl->owner->ops->insert_location (bl);
2935
2936 if (val)
2937 /* Back to the original value. */
2938 bl->watchpoint_type = hw_read;
2939 }
2940 }
2941
2942 bl->inserted = (val == 0);
2943 }
2944
2945 else if (bl->owner->type == bp_catchpoint)
2946 {
2947 int val;
2948
2949 gdb_assert (bl->owner->ops != NULL
2950 && bl->owner->ops->insert_location != NULL);
2951
2952 val = bl->owner->ops->insert_location (bl);
2953 if (val)
2954 {
2955 bl->owner->enable_state = bp_disabled;
2956
2957 if (val == 1)
2958 warning (_("\
2959 Error inserting catchpoint %d: Your system does not support this type\n\
2960 of catchpoint."), bl->owner->number);
2961 else
2962 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2963 }
2964
2965 bl->inserted = (val == 0);
2966
2967 /* We've already printed an error message if there was a problem
2968 inserting this catchpoint, and we've disabled the catchpoint,
2969 so just return success. */
2970 return 0;
2971 }
2972
2973 return 0;
2974 }
2975
2976 /* This function is called when program space PSPACE is about to be
2977 deleted. It takes care of updating breakpoints to not reference
2978 PSPACE anymore. */
2979
2980 void
2981 breakpoint_program_space_exit (struct program_space *pspace)
2982 {
2983 struct breakpoint *b, *b_temp;
2984 struct bp_location *loc, **loc_temp;
2985
2986 /* Remove any breakpoint that was set through this program space. */
2987 ALL_BREAKPOINTS_SAFE (b, b_temp)
2988 {
2989 if (b->pspace == pspace)
2990 delete_breakpoint (b);
2991 }
2992
2993 /* Breakpoints set through other program spaces could have locations
2994 bound to PSPACE as well. Remove those. */
2995 ALL_BP_LOCATIONS (loc, loc_temp)
2996 {
2997 struct bp_location *tmp;
2998
2999 if (loc->pspace == pspace)
3000 {
3001 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
3002 if (loc->owner->loc == loc)
3003 loc->owner->loc = loc->next;
3004 else
3005 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
3006 if (tmp->next == loc)
3007 {
3008 tmp->next = loc->next;
3009 break;
3010 }
3011 }
3012 }
3013
3014 /* Now update the global location list to permanently delete the
3015 removed locations above. */
3016 update_global_location_list (UGLL_DONT_INSERT);
3017 }
3018
3019 /* Make sure all breakpoints are inserted in inferior.
3020 Throws exception on any error.
3021 A breakpoint that is already inserted won't be inserted
3022 again, so calling this function twice is safe. */
3023 void
3024 insert_breakpoints (void)
3025 {
3026 struct breakpoint *bpt;
3027
3028 ALL_BREAKPOINTS (bpt)
3029 if (is_hardware_watchpoint (bpt))
3030 {
3031 struct watchpoint *w = (struct watchpoint *) bpt;
3032
3033 update_watchpoint (w, 0 /* don't reparse. */);
3034 }
3035
3036 /* Updating watchpoints creates new locations, so update the global
3037 location list. Explicitly tell ugll to insert locations and
3038 ignore breakpoints_always_inserted_mode. */
3039 update_global_location_list (UGLL_INSERT);
3040 }
3041
3042 /* Invoke CALLBACK for each of bp_location. */
3043
3044 void
3045 iterate_over_bp_locations (walk_bp_location_callback callback)
3046 {
3047 struct bp_location *loc, **loc_tmp;
3048
3049 ALL_BP_LOCATIONS (loc, loc_tmp)
3050 {
3051 callback (loc, NULL);
3052 }
3053 }
3054
3055 /* This is used when we need to synch breakpoint conditions between GDB and the
3056 target. It is the case with deleting and disabling of breakpoints when using
3057 always-inserted mode. */
3058
3059 static void
3060 update_inserted_breakpoint_locations (void)
3061 {
3062 struct bp_location *bl, **blp_tmp;
3063 int error_flag = 0;
3064 int val = 0;
3065 int disabled_breaks = 0;
3066 int hw_breakpoint_error = 0;
3067 int hw_bp_details_reported = 0;
3068
3069 struct ui_file *tmp_error_stream = mem_fileopen ();
3070 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
3071
3072 /* Explicitly mark the warning -- this will only be printed if
3073 there was an error. */
3074 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
3075
3076 save_current_space_and_thread ();
3077
3078 ALL_BP_LOCATIONS (bl, blp_tmp)
3079 {
3080 /* We only want to update software breakpoints and hardware
3081 breakpoints. */
3082 if (!is_breakpoint (bl->owner))
3083 continue;
3084
3085 /* We only want to update locations that are already inserted
3086 and need updating. This is to avoid unwanted insertion during
3087 deletion of breakpoints. */
3088 if (!bl->inserted || (bl->inserted && !bl->needs_update))
3089 continue;
3090
3091 switch_to_program_space_and_thread (bl->pspace);
3092
3093 /* For targets that support global breakpoints, there's no need
3094 to select an inferior to insert breakpoint to. In fact, even
3095 if we aren't attached to any process yet, we should still
3096 insert breakpoints. */
3097 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3098 && ptid_equal (inferior_ptid, null_ptid))
3099 continue;
3100
3101 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
3102 &hw_breakpoint_error, &hw_bp_details_reported);
3103 if (val)
3104 error_flag = val;
3105 }
3106
3107 if (error_flag)
3108 {
3109 target_terminal_ours_for_output ();
3110 error_stream (tmp_error_stream);
3111 }
3112
3113 do_cleanups (cleanups);
3114 }
3115
3116 /* Used when starting or continuing the program. */
3117
3118 static void
3119 insert_breakpoint_locations (void)
3120 {
3121 struct breakpoint *bpt;
3122 struct bp_location *bl, **blp_tmp;
3123 int error_flag = 0;
3124 int val = 0;
3125 int disabled_breaks = 0;
3126 int hw_breakpoint_error = 0;
3127 int hw_bp_error_explained_already = 0;
3128
3129 struct ui_file *tmp_error_stream = mem_fileopen ();
3130 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
3131
3132 /* Explicitly mark the warning -- this will only be printed if
3133 there was an error. */
3134 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
3135
3136 save_current_space_and_thread ();
3137
3138 ALL_BP_LOCATIONS (bl, blp_tmp)
3139 {
3140 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
3141 continue;
3142
3143 /* There is no point inserting thread-specific breakpoints if
3144 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3145 has BL->OWNER always non-NULL. */
3146 if (bl->owner->thread != -1
3147 && !valid_thread_id (bl->owner->thread))
3148 continue;
3149
3150 switch_to_program_space_and_thread (bl->pspace);
3151
3152 /* For targets that support global breakpoints, there's no need
3153 to select an inferior to insert breakpoint to. In fact, even
3154 if we aren't attached to any process yet, we should still
3155 insert breakpoints. */
3156 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3157 && ptid_equal (inferior_ptid, null_ptid))
3158 continue;
3159
3160 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
3161 &hw_breakpoint_error, &hw_bp_error_explained_already);
3162 if (val)
3163 error_flag = val;
3164 }
3165
3166 /* If we failed to insert all locations of a watchpoint, remove
3167 them, as half-inserted watchpoint is of limited use. */
3168 ALL_BREAKPOINTS (bpt)
3169 {
3170 int some_failed = 0;
3171 struct bp_location *loc;
3172
3173 if (!is_hardware_watchpoint (bpt))
3174 continue;
3175
3176 if (!breakpoint_enabled (bpt))
3177 continue;
3178
3179 if (bpt->disposition == disp_del_at_next_stop)
3180 continue;
3181
3182 for (loc = bpt->loc; loc; loc = loc->next)
3183 if (!loc->inserted && should_be_inserted (loc))
3184 {
3185 some_failed = 1;
3186 break;
3187 }
3188 if (some_failed)
3189 {
3190 for (loc = bpt->loc; loc; loc = loc->next)
3191 if (loc->inserted)
3192 remove_breakpoint (loc, mark_uninserted);
3193
3194 hw_breakpoint_error = 1;
3195 fprintf_unfiltered (tmp_error_stream,
3196 "Could not insert hardware watchpoint %d.\n",
3197 bpt->number);
3198 error_flag = -1;
3199 }
3200 }
3201
3202 if (error_flag)
3203 {
3204 /* If a hardware breakpoint or watchpoint was inserted, add a
3205 message about possibly exhausted resources. */
3206 if (hw_breakpoint_error && !hw_bp_error_explained_already)
3207 {
3208 fprintf_unfiltered (tmp_error_stream,
3209 "Could not insert hardware breakpoints:\n\
3210 You may have requested too many hardware breakpoints/watchpoints.\n");
3211 }
3212 target_terminal_ours_for_output ();
3213 error_stream (tmp_error_stream);
3214 }
3215
3216 do_cleanups (cleanups);
3217 }
3218
3219 /* Used when the program stops.
3220 Returns zero if successful, or non-zero if there was a problem
3221 removing a breakpoint location. */
3222
3223 int
3224 remove_breakpoints (void)
3225 {
3226 struct bp_location *bl, **blp_tmp;
3227 int val = 0;
3228
3229 ALL_BP_LOCATIONS (bl, blp_tmp)
3230 {
3231 if (bl->inserted && !is_tracepoint (bl->owner))
3232 val |= remove_breakpoint (bl, mark_uninserted);
3233 }
3234 return val;
3235 }
3236
3237 /* When a thread exits, remove breakpoints that are related to
3238 that thread. */
3239
3240 static void
3241 remove_threaded_breakpoints (struct thread_info *tp, int silent)
3242 {
3243 struct breakpoint *b, *b_tmp;
3244
3245 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3246 {
3247 if (b->thread == tp->num && user_breakpoint_p (b))
3248 {
3249 b->disposition = disp_del_at_next_stop;
3250
3251 printf_filtered (_("\
3252 Thread-specific breakpoint %d deleted - thread %s no longer in the thread list.\n"),
3253 b->number, print_thread_id (tp));
3254
3255 /* Hide it from the user. */
3256 b->number = 0;
3257 }
3258 }
3259 }
3260
3261 /* Remove breakpoints of process PID. */
3262
3263 int
3264 remove_breakpoints_pid (int pid)
3265 {
3266 struct bp_location *bl, **blp_tmp;
3267 int val;
3268 struct inferior *inf = find_inferior_pid (pid);
3269
3270 ALL_BP_LOCATIONS (bl, blp_tmp)
3271 {
3272 if (bl->pspace != inf->pspace)
3273 continue;
3274
3275 if (bl->inserted && !bl->target_info.persist)
3276 {
3277 val = remove_breakpoint (bl, mark_uninserted);
3278 if (val != 0)
3279 return val;
3280 }
3281 }
3282 return 0;
3283 }
3284
3285 int
3286 reattach_breakpoints (int pid)
3287 {
3288 struct cleanup *old_chain;
3289 struct bp_location *bl, **blp_tmp;
3290 int val;
3291 struct ui_file *tmp_error_stream;
3292 int dummy1 = 0, dummy2 = 0, dummy3 = 0;
3293 struct inferior *inf;
3294 struct thread_info *tp;
3295
3296 tp = any_live_thread_of_process (pid);
3297 if (tp == NULL)
3298 return 1;
3299
3300 inf = find_inferior_pid (pid);
3301 old_chain = save_inferior_ptid ();
3302
3303 inferior_ptid = tp->ptid;
3304
3305 tmp_error_stream = mem_fileopen ();
3306 make_cleanup_ui_file_delete (tmp_error_stream);
3307
3308 ALL_BP_LOCATIONS (bl, blp_tmp)
3309 {
3310 if (bl->pspace != inf->pspace)
3311 continue;
3312
3313 if (bl->inserted)
3314 {
3315 bl->inserted = 0;
3316 val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2, &dummy3);
3317 if (val != 0)
3318 {
3319 do_cleanups (old_chain);
3320 return val;
3321 }
3322 }
3323 }
3324 do_cleanups (old_chain);
3325 return 0;
3326 }
3327
3328 static int internal_breakpoint_number = -1;
3329
3330 /* Set the breakpoint number of B, depending on the value of INTERNAL.
3331 If INTERNAL is non-zero, the breakpoint number will be populated
3332 from internal_breakpoint_number and that variable decremented.
3333 Otherwise the breakpoint number will be populated from
3334 breakpoint_count and that value incremented. Internal breakpoints
3335 do not set the internal var bpnum. */
3336 static void
3337 set_breakpoint_number (int internal, struct breakpoint *b)
3338 {
3339 if (internal)
3340 b->number = internal_breakpoint_number--;
3341 else
3342 {
3343 set_breakpoint_count (breakpoint_count + 1);
3344 b->number = breakpoint_count;
3345 }
3346 }
3347
3348 static struct breakpoint *
3349 create_internal_breakpoint (struct gdbarch *gdbarch,
3350 CORE_ADDR address, enum bptype type,
3351 const struct breakpoint_ops *ops)
3352 {
3353 struct symtab_and_line sal;
3354 struct breakpoint *b;
3355
3356 init_sal (&sal); /* Initialize to zeroes. */
3357
3358 sal.pc = address;
3359 sal.section = find_pc_overlay (sal.pc);
3360 sal.pspace = current_program_space;
3361
3362 b = set_raw_breakpoint (gdbarch, sal, type, ops);
3363 b->number = internal_breakpoint_number--;
3364 b->disposition = disp_donttouch;
3365
3366 return b;
3367 }
3368
3369 static const char *const longjmp_names[] =
3370 {
3371 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3372 };
3373 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3374
3375 /* Per-objfile data private to breakpoint.c. */
3376 struct breakpoint_objfile_data
3377 {
3378 /* Minimal symbol for "_ovly_debug_event" (if any). */
3379 struct bound_minimal_symbol overlay_msym;
3380
3381 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3382 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
3383
3384 /* True if we have looked for longjmp probes. */
3385 int longjmp_searched;
3386
3387 /* SystemTap probe points for longjmp (if any). */
3388 VEC (probe_p) *longjmp_probes;
3389
3390 /* Minimal symbol for "std::terminate()" (if any). */
3391 struct bound_minimal_symbol terminate_msym;
3392
3393 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3394 struct bound_minimal_symbol exception_msym;
3395
3396 /* True if we have looked for exception probes. */
3397 int exception_searched;
3398
3399 /* SystemTap probe points for unwinding (if any). */
3400 VEC (probe_p) *exception_probes;
3401 };
3402
3403 static const struct objfile_data *breakpoint_objfile_key;
3404
3405 /* Minimal symbol not found sentinel. */
3406 static struct minimal_symbol msym_not_found;
3407
3408 /* Returns TRUE if MSYM point to the "not found" sentinel. */
3409
3410 static int
3411 msym_not_found_p (const struct minimal_symbol *msym)
3412 {
3413 return msym == &msym_not_found;
3414 }
3415
3416 /* Return per-objfile data needed by breakpoint.c.
3417 Allocate the data if necessary. */
3418
3419 static struct breakpoint_objfile_data *
3420 get_breakpoint_objfile_data (struct objfile *objfile)
3421 {
3422 struct breakpoint_objfile_data *bp_objfile_data;
3423
3424 bp_objfile_data = ((struct breakpoint_objfile_data *)
3425 objfile_data (objfile, breakpoint_objfile_key));
3426 if (bp_objfile_data == NULL)
3427 {
3428 bp_objfile_data =
3429 XOBNEW (&objfile->objfile_obstack, struct breakpoint_objfile_data);
3430
3431 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
3432 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3433 }
3434 return bp_objfile_data;
3435 }
3436
3437 static void
3438 free_breakpoint_probes (struct objfile *obj, void *data)
3439 {
3440 struct breakpoint_objfile_data *bp_objfile_data
3441 = (struct breakpoint_objfile_data *) data;
3442
3443 VEC_free (probe_p, bp_objfile_data->longjmp_probes);
3444 VEC_free (probe_p, bp_objfile_data->exception_probes);
3445 }
3446
3447 static void
3448 create_overlay_event_breakpoint (void)
3449 {
3450 struct objfile *objfile;
3451 const char *const func_name = "_ovly_debug_event";
3452
3453 ALL_OBJFILES (objfile)
3454 {
3455 struct breakpoint *b;
3456 struct breakpoint_objfile_data *bp_objfile_data;
3457 CORE_ADDR addr;
3458 struct explicit_location explicit_loc;
3459
3460 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3461
3462 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
3463 continue;
3464
3465 if (bp_objfile_data->overlay_msym.minsym == NULL)
3466 {
3467 struct bound_minimal_symbol m;
3468
3469 m = lookup_minimal_symbol_text (func_name, objfile);
3470 if (m.minsym == NULL)
3471 {
3472 /* Avoid future lookups in this objfile. */
3473 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
3474 continue;
3475 }
3476 bp_objfile_data->overlay_msym = m;
3477 }
3478
3479 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
3480 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3481 bp_overlay_event,
3482 &internal_breakpoint_ops);
3483 initialize_explicit_location (&explicit_loc);
3484 explicit_loc.function_name = ASTRDUP (func_name);
3485 b->location = new_explicit_location (&explicit_loc);
3486
3487 if (overlay_debugging == ovly_auto)
3488 {
3489 b->enable_state = bp_enabled;
3490 overlay_events_enabled = 1;
3491 }
3492 else
3493 {
3494 b->enable_state = bp_disabled;
3495 overlay_events_enabled = 0;
3496 }
3497 }
3498 update_global_location_list (UGLL_MAY_INSERT);
3499 }
3500
3501 static void
3502 create_longjmp_master_breakpoint (void)
3503 {
3504 struct program_space *pspace;
3505 struct cleanup *old_chain;
3506
3507 old_chain = save_current_program_space ();
3508
3509 ALL_PSPACES (pspace)
3510 {
3511 struct objfile *objfile;
3512
3513 set_current_program_space (pspace);
3514
3515 ALL_OBJFILES (objfile)
3516 {
3517 int i;
3518 struct gdbarch *gdbarch;
3519 struct breakpoint_objfile_data *bp_objfile_data;
3520
3521 gdbarch = get_objfile_arch (objfile);
3522
3523 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3524
3525 if (!bp_objfile_data->longjmp_searched)
3526 {
3527 VEC (probe_p) *ret;
3528
3529 ret = find_probes_in_objfile (objfile, "libc", "longjmp");
3530 if (ret != NULL)
3531 {
3532 /* We are only interested in checking one element. */
3533 struct probe *p = VEC_index (probe_p, ret, 0);
3534
3535 if (!can_evaluate_probe_arguments (p))
3536 {
3537 /* We cannot use the probe interface here, because it does
3538 not know how to evaluate arguments. */
3539 VEC_free (probe_p, ret);
3540 ret = NULL;
3541 }
3542 }
3543 bp_objfile_data->longjmp_probes = ret;
3544 bp_objfile_data->longjmp_searched = 1;
3545 }
3546
3547 if (bp_objfile_data->longjmp_probes != NULL)
3548 {
3549 int i;
3550 struct probe *probe;
3551 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3552
3553 for (i = 0;
3554 VEC_iterate (probe_p,
3555 bp_objfile_data->longjmp_probes,
3556 i, probe);
3557 ++i)
3558 {
3559 struct breakpoint *b;
3560
3561 b = create_internal_breakpoint (gdbarch,
3562 get_probe_address (probe,
3563 objfile),
3564 bp_longjmp_master,
3565 &internal_breakpoint_ops);
3566 b->location
3567 = new_probe_location ("-probe-stap libc:longjmp");
3568 b->enable_state = bp_disabled;
3569 }
3570
3571 continue;
3572 }
3573
3574 if (!gdbarch_get_longjmp_target_p (gdbarch))
3575 continue;
3576
3577 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
3578 {
3579 struct breakpoint *b;
3580 const char *func_name;
3581 CORE_ADDR addr;
3582 struct explicit_location explicit_loc;
3583
3584 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3585 continue;
3586
3587 func_name = longjmp_names[i];
3588 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3589 {
3590 struct bound_minimal_symbol m;
3591
3592 m = lookup_minimal_symbol_text (func_name, objfile);
3593 if (m.minsym == NULL)
3594 {
3595 /* Prevent future lookups in this objfile. */
3596 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
3597 continue;
3598 }
3599 bp_objfile_data->longjmp_msym[i] = m;
3600 }
3601
3602 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
3603 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3604 &internal_breakpoint_ops);
3605 initialize_explicit_location (&explicit_loc);
3606 explicit_loc.function_name = ASTRDUP (func_name);
3607 b->location = new_explicit_location (&explicit_loc);
3608 b->enable_state = bp_disabled;
3609 }
3610 }
3611 }
3612 update_global_location_list (UGLL_MAY_INSERT);
3613
3614 do_cleanups (old_chain);
3615 }
3616
3617 /* Create a master std::terminate breakpoint. */
3618 static void
3619 create_std_terminate_master_breakpoint (void)
3620 {
3621 struct program_space *pspace;
3622 struct cleanup *old_chain;
3623 const char *const func_name = "std::terminate()";
3624
3625 old_chain = save_current_program_space ();
3626
3627 ALL_PSPACES (pspace)
3628 {
3629 struct objfile *objfile;
3630 CORE_ADDR addr;
3631
3632 set_current_program_space (pspace);
3633
3634 ALL_OBJFILES (objfile)
3635 {
3636 struct breakpoint *b;
3637 struct breakpoint_objfile_data *bp_objfile_data;
3638 struct explicit_location explicit_loc;
3639
3640 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3641
3642 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3643 continue;
3644
3645 if (bp_objfile_data->terminate_msym.minsym == NULL)
3646 {
3647 struct bound_minimal_symbol m;
3648
3649 m = lookup_minimal_symbol (func_name, NULL, objfile);
3650 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3651 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
3652 {
3653 /* Prevent future lookups in this objfile. */
3654 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3655 continue;
3656 }
3657 bp_objfile_data->terminate_msym = m;
3658 }
3659
3660 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3661 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3662 bp_std_terminate_master,
3663 &internal_breakpoint_ops);
3664 initialize_explicit_location (&explicit_loc);
3665 explicit_loc.function_name = ASTRDUP (func_name);
3666 b->location = new_explicit_location (&explicit_loc);
3667 b->enable_state = bp_disabled;
3668 }
3669 }
3670
3671 update_global_location_list (UGLL_MAY_INSERT);
3672
3673 do_cleanups (old_chain);
3674 }
3675
3676 /* Install a master breakpoint on the unwinder's debug hook. */
3677
3678 static void
3679 create_exception_master_breakpoint (void)
3680 {
3681 struct objfile *objfile;
3682 const char *const func_name = "_Unwind_DebugHook";
3683
3684 ALL_OBJFILES (objfile)
3685 {
3686 struct breakpoint *b;
3687 struct gdbarch *gdbarch;
3688 struct breakpoint_objfile_data *bp_objfile_data;
3689 CORE_ADDR addr;
3690 struct explicit_location explicit_loc;
3691
3692 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3693
3694 /* We prefer the SystemTap probe point if it exists. */
3695 if (!bp_objfile_data->exception_searched)
3696 {
3697 VEC (probe_p) *ret;
3698
3699 ret = find_probes_in_objfile (objfile, "libgcc", "unwind");
3700
3701 if (ret != NULL)
3702 {
3703 /* We are only interested in checking one element. */
3704 struct probe *p = VEC_index (probe_p, ret, 0);
3705
3706 if (!can_evaluate_probe_arguments (p))
3707 {
3708 /* We cannot use the probe interface here, because it does
3709 not know how to evaluate arguments. */
3710 VEC_free (probe_p, ret);
3711 ret = NULL;
3712 }
3713 }
3714 bp_objfile_data->exception_probes = ret;
3715 bp_objfile_data->exception_searched = 1;
3716 }
3717
3718 if (bp_objfile_data->exception_probes != NULL)
3719 {
3720 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3721 int i;
3722 struct probe *probe;
3723
3724 for (i = 0;
3725 VEC_iterate (probe_p,
3726 bp_objfile_data->exception_probes,
3727 i, probe);
3728 ++i)
3729 {
3730 struct breakpoint *b;
3731
3732 b = create_internal_breakpoint (gdbarch,
3733 get_probe_address (probe,
3734 objfile),
3735 bp_exception_master,
3736 &internal_breakpoint_ops);
3737 b->location
3738 = new_probe_location ("-probe-stap libgcc:unwind");
3739 b->enable_state = bp_disabled;
3740 }
3741
3742 continue;
3743 }
3744
3745 /* Otherwise, try the hook function. */
3746
3747 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3748 continue;
3749
3750 gdbarch = get_objfile_arch (objfile);
3751
3752 if (bp_objfile_data->exception_msym.minsym == NULL)
3753 {
3754 struct bound_minimal_symbol debug_hook;
3755
3756 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3757 if (debug_hook.minsym == NULL)
3758 {
3759 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3760 continue;
3761 }
3762
3763 bp_objfile_data->exception_msym = debug_hook;
3764 }
3765
3766 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
3767 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3768 &current_target);
3769 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3770 &internal_breakpoint_ops);
3771 initialize_explicit_location (&explicit_loc);
3772 explicit_loc.function_name = ASTRDUP (func_name);
3773 b->location = new_explicit_location (&explicit_loc);
3774 b->enable_state = bp_disabled;
3775 }
3776
3777 update_global_location_list (UGLL_MAY_INSERT);
3778 }
3779
3780 void
3781 update_breakpoints_after_exec (void)
3782 {
3783 struct breakpoint *b, *b_tmp;
3784 struct bp_location *bploc, **bplocp_tmp;
3785
3786 /* We're about to delete breakpoints from GDB's lists. If the
3787 INSERTED flag is true, GDB will try to lift the breakpoints by
3788 writing the breakpoints' "shadow contents" back into memory. The
3789 "shadow contents" are NOT valid after an exec, so GDB should not
3790 do that. Instead, the target is responsible from marking
3791 breakpoints out as soon as it detects an exec. We don't do that
3792 here instead, because there may be other attempts to delete
3793 breakpoints after detecting an exec and before reaching here. */
3794 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
3795 if (bploc->pspace == current_program_space)
3796 gdb_assert (!bploc->inserted);
3797
3798 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3799 {
3800 if (b->pspace != current_program_space)
3801 continue;
3802
3803 /* Solib breakpoints must be explicitly reset after an exec(). */
3804 if (b->type == bp_shlib_event)
3805 {
3806 delete_breakpoint (b);
3807 continue;
3808 }
3809
3810 /* JIT breakpoints must be explicitly reset after an exec(). */
3811 if (b->type == bp_jit_event)
3812 {
3813 delete_breakpoint (b);
3814 continue;
3815 }
3816
3817 /* Thread event breakpoints must be set anew after an exec(),
3818 as must overlay event and longjmp master breakpoints. */
3819 if (b->type == bp_thread_event || b->type == bp_overlay_event
3820 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3821 || b->type == bp_exception_master)
3822 {
3823 delete_breakpoint (b);
3824 continue;
3825 }
3826
3827 /* Step-resume breakpoints are meaningless after an exec(). */
3828 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3829 {
3830 delete_breakpoint (b);
3831 continue;
3832 }
3833
3834 /* Just like single-step breakpoints. */
3835 if (b->type == bp_single_step)
3836 {
3837 delete_breakpoint (b);
3838 continue;
3839 }
3840
3841 /* Longjmp and longjmp-resume breakpoints are also meaningless
3842 after an exec. */
3843 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3844 || b->type == bp_longjmp_call_dummy
3845 || b->type == bp_exception || b->type == bp_exception_resume)
3846 {
3847 delete_breakpoint (b);
3848 continue;
3849 }
3850
3851 if (b->type == bp_catchpoint)
3852 {
3853 /* For now, none of the bp_catchpoint breakpoints need to
3854 do anything at this point. In the future, if some of
3855 the catchpoints need to something, we will need to add
3856 a new method, and call this method from here. */
3857 continue;
3858 }
3859
3860 /* bp_finish is a special case. The only way we ought to be able
3861 to see one of these when an exec() has happened, is if the user
3862 caught a vfork, and then said "finish". Ordinarily a finish just
3863 carries them to the call-site of the current callee, by setting
3864 a temporary bp there and resuming. But in this case, the finish
3865 will carry them entirely through the vfork & exec.
3866
3867 We don't want to allow a bp_finish to remain inserted now. But
3868 we can't safely delete it, 'cause finish_command has a handle to
3869 the bp on a bpstat, and will later want to delete it. There's a
3870 chance (and I've seen it happen) that if we delete the bp_finish
3871 here, that its storage will get reused by the time finish_command
3872 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3873 We really must allow finish_command to delete a bp_finish.
3874
3875 In the absence of a general solution for the "how do we know
3876 it's safe to delete something others may have handles to?"
3877 problem, what we'll do here is just uninsert the bp_finish, and
3878 let finish_command delete it.
3879
3880 (We know the bp_finish is "doomed" in the sense that it's
3881 momentary, and will be deleted as soon as finish_command sees
3882 the inferior stopped. So it doesn't matter that the bp's
3883 address is probably bogus in the new a.out, unlike e.g., the
3884 solib breakpoints.) */
3885
3886 if (b->type == bp_finish)
3887 {
3888 continue;
3889 }
3890
3891 /* Without a symbolic address, we have little hope of the
3892 pre-exec() address meaning the same thing in the post-exec()
3893 a.out. */
3894 if (event_location_empty_p (b->location))
3895 {
3896 delete_breakpoint (b);
3897 continue;
3898 }
3899 }
3900 }
3901
3902 int
3903 detach_breakpoints (ptid_t ptid)
3904 {
3905 struct bp_location *bl, **blp_tmp;
3906 int val = 0;
3907 struct cleanup *old_chain = save_inferior_ptid ();
3908 struct inferior *inf = current_inferior ();
3909
3910 if (ptid_get_pid (ptid) == ptid_get_pid (inferior_ptid))
3911 error (_("Cannot detach breakpoints of inferior_ptid"));
3912
3913 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
3914 inferior_ptid = ptid;
3915 ALL_BP_LOCATIONS (bl, blp_tmp)
3916 {
3917 if (bl->pspace != inf->pspace)
3918 continue;
3919
3920 /* This function must physically remove breakpoints locations
3921 from the specified ptid, without modifying the breakpoint
3922 package's state. Locations of type bp_loc_other are only
3923 maintained at GDB side. So, there is no need to remove
3924 these bp_loc_other locations. Moreover, removing these
3925 would modify the breakpoint package's state. */
3926 if (bl->loc_type == bp_loc_other)
3927 continue;
3928
3929 if (bl->inserted)
3930 val |= remove_breakpoint_1 (bl, mark_inserted);
3931 }
3932
3933 do_cleanups (old_chain);
3934 return val;
3935 }
3936
3937 /* Remove the breakpoint location BL from the current address space.
3938 Note that this is used to detach breakpoints from a child fork.
3939 When we get here, the child isn't in the inferior list, and neither
3940 do we have objects to represent its address space --- we should
3941 *not* look at bl->pspace->aspace here. */
3942
3943 static int
3944 remove_breakpoint_1 (struct bp_location *bl, insertion_state_t is)
3945 {
3946 int val;
3947
3948 /* BL is never in moribund_locations by our callers. */
3949 gdb_assert (bl->owner != NULL);
3950
3951 /* The type of none suggests that owner is actually deleted.
3952 This should not ever happen. */
3953 gdb_assert (bl->owner->type != bp_none);
3954
3955 if (bl->loc_type == bp_loc_software_breakpoint
3956 || bl->loc_type == bp_loc_hardware_breakpoint)
3957 {
3958 /* "Normal" instruction breakpoint: either the standard
3959 trap-instruction bp (bp_breakpoint), or a
3960 bp_hardware_breakpoint. */
3961
3962 /* First check to see if we have to handle an overlay. */
3963 if (overlay_debugging == ovly_off
3964 || bl->section == NULL
3965 || !(section_is_overlay (bl->section)))
3966 {
3967 /* No overlay handling: just remove the breakpoint. */
3968
3969 /* If we're trying to uninsert a memory breakpoint that we
3970 know is set in a dynamic object that is marked
3971 shlib_disabled, then either the dynamic object was
3972 removed with "remove-symbol-file" or with
3973 "nosharedlibrary". In the former case, we don't know
3974 whether another dynamic object might have loaded over the
3975 breakpoint's address -- the user might well let us know
3976 about it next with add-symbol-file (the whole point of
3977 add-symbol-file is letting the user manually maintain a
3978 list of dynamically loaded objects). If we have the
3979 breakpoint's shadow memory, that is, this is a software
3980 breakpoint managed by GDB, check whether the breakpoint
3981 is still inserted in memory, to avoid overwriting wrong
3982 code with stale saved shadow contents. Note that HW
3983 breakpoints don't have shadow memory, as they're
3984 implemented using a mechanism that is not dependent on
3985 being able to modify the target's memory, and as such
3986 they should always be removed. */
3987 if (bl->shlib_disabled
3988 && bl->target_info.shadow_len != 0
3989 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3990 val = 0;
3991 else
3992 val = bl->owner->ops->remove_location (bl);
3993 }
3994 else
3995 {
3996 /* This breakpoint is in an overlay section.
3997 Did we set a breakpoint at the LMA? */
3998 if (!overlay_events_enabled)
3999 {
4000 /* Yes -- overlay event support is not active, so we
4001 should have set a breakpoint at the LMA. Remove it.
4002 */
4003 /* Ignore any failures: if the LMA is in ROM, we will
4004 have already warned when we failed to insert it. */
4005 if (bl->loc_type == bp_loc_hardware_breakpoint)
4006 target_remove_hw_breakpoint (bl->gdbarch,
4007 &bl->overlay_target_info);
4008 else
4009 target_remove_breakpoint (bl->gdbarch,
4010 &bl->overlay_target_info);
4011 }
4012 /* Did we set a breakpoint at the VMA?
4013 If so, we will have marked the breakpoint 'inserted'. */
4014 if (bl->inserted)
4015 {
4016 /* Yes -- remove it. Previously we did not bother to
4017 remove the breakpoint if the section had been
4018 unmapped, but let's not rely on that being safe. We
4019 don't know what the overlay manager might do. */
4020
4021 /* However, we should remove *software* breakpoints only
4022 if the section is still mapped, or else we overwrite
4023 wrong code with the saved shadow contents. */
4024 if (bl->loc_type == bp_loc_hardware_breakpoint
4025 || section_is_mapped (bl->section))
4026 val = bl->owner->ops->remove_location (bl);
4027 else
4028 val = 0;
4029 }
4030 else
4031 {
4032 /* No -- not inserted, so no need to remove. No error. */
4033 val = 0;
4034 }
4035 }
4036
4037 /* In some cases, we might not be able to remove a breakpoint in
4038 a shared library that has already been removed, but we have
4039 not yet processed the shlib unload event. Similarly for an
4040 unloaded add-symbol-file object - the user might not yet have
4041 had the chance to remove-symbol-file it. shlib_disabled will
4042 be set if the library/object has already been removed, but
4043 the breakpoint hasn't been uninserted yet, e.g., after
4044 "nosharedlibrary" or "remove-symbol-file" with breakpoints
4045 always-inserted mode. */
4046 if (val
4047 && (bl->loc_type == bp_loc_software_breakpoint
4048 && (bl->shlib_disabled
4049 || solib_name_from_address (bl->pspace, bl->address)
4050 || shared_objfile_contains_address_p (bl->pspace,
4051 bl->address))))
4052 val = 0;
4053
4054 if (val)
4055 return val;
4056 bl->inserted = (is == mark_inserted);
4057 }
4058 else if (bl->loc_type == bp_loc_hardware_watchpoint)
4059 {
4060 gdb_assert (bl->owner->ops != NULL
4061 && bl->owner->ops->remove_location != NULL);
4062
4063 bl->inserted = (is == mark_inserted);
4064 bl->owner->ops->remove_location (bl);
4065
4066 /* Failure to remove any of the hardware watchpoints comes here. */
4067 if ((is == mark_uninserted) && (bl->inserted))
4068 warning (_("Could not remove hardware watchpoint %d."),
4069 bl->owner->number);
4070 }
4071 else if (bl->owner->type == bp_catchpoint
4072 && breakpoint_enabled (bl->owner)
4073 && !bl->duplicate)
4074 {
4075 gdb_assert (bl->owner->ops != NULL
4076 && bl->owner->ops->remove_location != NULL);
4077
4078 val = bl->owner->ops->remove_location (bl);
4079 if (val)
4080 return val;
4081
4082 bl->inserted = (is == mark_inserted);
4083 }
4084
4085 return 0;
4086 }
4087
4088 static int
4089 remove_breakpoint (struct bp_location *bl, insertion_state_t is)
4090 {
4091 int ret;
4092 struct cleanup *old_chain;
4093
4094 /* BL is never in moribund_locations by our callers. */
4095 gdb_assert (bl->owner != NULL);
4096
4097 /* The type of none suggests that owner is actually deleted.
4098 This should not ever happen. */
4099 gdb_assert (bl->owner->type != bp_none);
4100
4101 old_chain = save_current_space_and_thread ();
4102
4103 switch_to_program_space_and_thread (bl->pspace);
4104
4105 ret = remove_breakpoint_1 (bl, is);
4106
4107 do_cleanups (old_chain);
4108 return ret;
4109 }
4110
4111 /* Clear the "inserted" flag in all breakpoints. */
4112
4113 void
4114 mark_breakpoints_out (void)
4115 {
4116 struct bp_location *bl, **blp_tmp;
4117
4118 ALL_BP_LOCATIONS (bl, blp_tmp)
4119 if (bl->pspace == current_program_space)
4120 bl->inserted = 0;
4121 }
4122
4123 /* Clear the "inserted" flag in all breakpoints and delete any
4124 breakpoints which should go away between runs of the program.
4125
4126 Plus other such housekeeping that has to be done for breakpoints
4127 between runs.
4128
4129 Note: this function gets called at the end of a run (by
4130 generic_mourn_inferior) and when a run begins (by
4131 init_wait_for_inferior). */
4132
4133
4134
4135 void
4136 breakpoint_init_inferior (enum inf_context context)
4137 {
4138 struct breakpoint *b, *b_tmp;
4139 struct bp_location *bl, **blp_tmp;
4140 int ix;
4141 struct program_space *pspace = current_program_space;
4142
4143 /* If breakpoint locations are shared across processes, then there's
4144 nothing to do. */
4145 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
4146 return;
4147
4148 mark_breakpoints_out ();
4149
4150 ALL_BREAKPOINTS_SAFE (b, b_tmp)
4151 {
4152 if (b->loc && b->loc->pspace != pspace)
4153 continue;
4154
4155 switch (b->type)
4156 {
4157 case bp_call_dummy:
4158 case bp_longjmp_call_dummy:
4159
4160 /* If the call dummy breakpoint is at the entry point it will
4161 cause problems when the inferior is rerun, so we better get
4162 rid of it. */
4163
4164 case bp_watchpoint_scope:
4165
4166 /* Also get rid of scope breakpoints. */
4167
4168 case bp_shlib_event:
4169
4170 /* Also remove solib event breakpoints. Their addresses may
4171 have changed since the last time we ran the program.
4172 Actually we may now be debugging against different target;
4173 and so the solib backend that installed this breakpoint may
4174 not be used in by the target. E.g.,
4175
4176 (gdb) file prog-linux
4177 (gdb) run # native linux target
4178 ...
4179 (gdb) kill
4180 (gdb) file prog-win.exe
4181 (gdb) tar rem :9999 # remote Windows gdbserver.
4182 */
4183
4184 case bp_step_resume:
4185
4186 /* Also remove step-resume breakpoints. */
4187
4188 case bp_single_step:
4189
4190 /* Also remove single-step breakpoints. */
4191
4192 delete_breakpoint (b);
4193 break;
4194
4195 case bp_watchpoint:
4196 case bp_hardware_watchpoint:
4197 case bp_read_watchpoint:
4198 case bp_access_watchpoint:
4199 {
4200 struct watchpoint *w = (struct watchpoint *) b;
4201
4202 /* Likewise for watchpoints on local expressions. */
4203 if (w->exp_valid_block != NULL)
4204 delete_breakpoint (b);
4205 else if (context == inf_starting)
4206 {
4207 /* Reset val field to force reread of starting value in
4208 insert_breakpoints. */
4209 if (w->val)
4210 value_free (w->val);
4211 w->val = NULL;
4212 w->val_valid = 0;
4213 }
4214 }
4215 break;
4216 default:
4217 break;
4218 }
4219 }
4220
4221 /* Get rid of the moribund locations. */
4222 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
4223 decref_bp_location (&bl);
4224 VEC_free (bp_location_p, moribund_locations);
4225 }
4226
4227 /* These functions concern about actual breakpoints inserted in the
4228 target --- to e.g. check if we need to do decr_pc adjustment or if
4229 we need to hop over the bkpt --- so we check for address space
4230 match, not program space. */
4231
4232 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4233 exists at PC. It returns ordinary_breakpoint_here if it's an
4234 ordinary breakpoint, or permanent_breakpoint_here if it's a
4235 permanent breakpoint.
4236 - When continuing from a location with an ordinary breakpoint, we
4237 actually single step once before calling insert_breakpoints.
4238 - When continuing from a location with a permanent breakpoint, we
4239 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4240 the target, to advance the PC past the breakpoint. */
4241
4242 enum breakpoint_here
4243 breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
4244 {
4245 struct bp_location *bl, **blp_tmp;
4246 int any_breakpoint_here = 0;
4247
4248 ALL_BP_LOCATIONS (bl, blp_tmp)
4249 {
4250 if (bl->loc_type != bp_loc_software_breakpoint
4251 && bl->loc_type != bp_loc_hardware_breakpoint)
4252 continue;
4253
4254 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
4255 if ((breakpoint_enabled (bl->owner)
4256 || bl->permanent)
4257 && breakpoint_location_address_match (bl, aspace, pc))
4258 {
4259 if (overlay_debugging
4260 && section_is_overlay (bl->section)
4261 && !section_is_mapped (bl->section))
4262 continue; /* unmapped overlay -- can't be a match */
4263 else if (bl->permanent)
4264 return permanent_breakpoint_here;
4265 else
4266 any_breakpoint_here = 1;
4267 }
4268 }
4269
4270 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
4271 }
4272
4273 /* See breakpoint.h. */
4274
4275 int
4276 breakpoint_in_range_p (struct address_space *aspace,
4277 CORE_ADDR addr, ULONGEST len)
4278 {
4279 struct bp_location *bl, **blp_tmp;
4280
4281 ALL_BP_LOCATIONS (bl, blp_tmp)
4282 {
4283 if (bl->loc_type != bp_loc_software_breakpoint
4284 && bl->loc_type != bp_loc_hardware_breakpoint)
4285 continue;
4286
4287 if ((breakpoint_enabled (bl->owner)
4288 || bl->permanent)
4289 && breakpoint_location_address_range_overlap (bl, aspace,
4290 addr, len))
4291 {
4292 if (overlay_debugging
4293 && section_is_overlay (bl->section)
4294 && !section_is_mapped (bl->section))
4295 {
4296 /* Unmapped overlay -- can't be a match. */
4297 continue;
4298 }
4299
4300 return 1;
4301 }
4302 }
4303
4304 return 0;
4305 }
4306
4307 /* Return true if there's a moribund breakpoint at PC. */
4308
4309 int
4310 moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
4311 {
4312 struct bp_location *loc;
4313 int ix;
4314
4315 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
4316 if (breakpoint_location_address_match (loc, aspace, pc))
4317 return 1;
4318
4319 return 0;
4320 }
4321
4322 /* Returns non-zero iff BL is inserted at PC, in address space
4323 ASPACE. */
4324
4325 static int
4326 bp_location_inserted_here_p (struct bp_location *bl,
4327 struct address_space *aspace, CORE_ADDR pc)
4328 {
4329 if (bl->inserted
4330 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4331 aspace, pc))
4332 {
4333 if (overlay_debugging
4334 && section_is_overlay (bl->section)
4335 && !section_is_mapped (bl->section))
4336 return 0; /* unmapped overlay -- can't be a match */
4337 else
4338 return 1;
4339 }
4340 return 0;
4341 }
4342
4343 /* Returns non-zero iff there's a breakpoint inserted at PC. */
4344
4345 int
4346 breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
4347 {
4348 struct bp_location **blp, **blp_tmp = NULL;
4349 struct bp_location *bl;
4350
4351 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4352 {
4353 struct bp_location *bl = *blp;
4354
4355 if (bl->loc_type != bp_loc_software_breakpoint
4356 && bl->loc_type != bp_loc_hardware_breakpoint)
4357 continue;
4358
4359 if (bp_location_inserted_here_p (bl, aspace, pc))
4360 return 1;
4361 }
4362 return 0;
4363 }
4364
4365 /* This function returns non-zero iff there is a software breakpoint
4366 inserted at PC. */
4367
4368 int
4369 software_breakpoint_inserted_here_p (struct address_space *aspace,
4370 CORE_ADDR pc)
4371 {
4372 struct bp_location **blp, **blp_tmp = NULL;
4373 struct bp_location *bl;
4374
4375 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4376 {
4377 struct bp_location *bl = *blp;
4378
4379 if (bl->loc_type != bp_loc_software_breakpoint)
4380 continue;
4381
4382 if (bp_location_inserted_here_p (bl, aspace, pc))
4383 return 1;
4384 }
4385
4386 return 0;
4387 }
4388
4389 /* See breakpoint.h. */
4390
4391 int
4392 hardware_breakpoint_inserted_here_p (struct address_space *aspace,
4393 CORE_ADDR pc)
4394 {
4395 struct bp_location **blp, **blp_tmp = NULL;
4396 struct bp_location *bl;
4397
4398 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4399 {
4400 struct bp_location *bl = *blp;
4401
4402 if (bl->loc_type != bp_loc_hardware_breakpoint)
4403 continue;
4404
4405 if (bp_location_inserted_here_p (bl, aspace, pc))
4406 return 1;
4407 }
4408
4409 return 0;
4410 }
4411
4412 int
4413 hardware_watchpoint_inserted_in_range (struct address_space *aspace,
4414 CORE_ADDR addr, ULONGEST len)
4415 {
4416 struct breakpoint *bpt;
4417
4418 ALL_BREAKPOINTS (bpt)
4419 {
4420 struct bp_location *loc;
4421
4422 if (bpt->type != bp_hardware_watchpoint
4423 && bpt->type != bp_access_watchpoint)
4424 continue;
4425
4426 if (!breakpoint_enabled (bpt))
4427 continue;
4428
4429 for (loc = bpt->loc; loc; loc = loc->next)
4430 if (loc->pspace->aspace == aspace && loc->inserted)
4431 {
4432 CORE_ADDR l, h;
4433
4434 /* Check for intersection. */
4435 l = max (loc->address, addr);
4436 h = min (loc->address + loc->length, addr + len);
4437 if (l < h)
4438 return 1;
4439 }
4440 }
4441 return 0;
4442 }
4443 \f
4444
4445 /* bpstat stuff. External routines' interfaces are documented
4446 in breakpoint.h. */
4447
4448 int
4449 is_catchpoint (struct breakpoint *ep)
4450 {
4451 return (ep->type == bp_catchpoint);
4452 }
4453
4454 /* Frees any storage that is part of a bpstat. Does not walk the
4455 'next' chain. */
4456
4457 static void
4458 bpstat_free (bpstat bs)
4459 {
4460 if (bs->old_val != NULL)
4461 value_free (bs->old_val);
4462 decref_counted_command_line (&bs->commands);
4463 decref_bp_location (&bs->bp_location_at);
4464 xfree (bs);
4465 }
4466
4467 /* Clear a bpstat so that it says we are not at any breakpoint.
4468 Also free any storage that is part of a bpstat. */
4469
4470 void
4471 bpstat_clear (bpstat *bsp)
4472 {
4473 bpstat p;
4474 bpstat q;
4475
4476 if (bsp == 0)
4477 return;
4478 p = *bsp;
4479 while (p != NULL)
4480 {
4481 q = p->next;
4482 bpstat_free (p);
4483 p = q;
4484 }
4485 *bsp = NULL;
4486 }
4487
4488 /* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4489 is part of the bpstat is copied as well. */
4490
4491 bpstat
4492 bpstat_copy (bpstat bs)
4493 {
4494 bpstat p = NULL;
4495 bpstat tmp;
4496 bpstat retval = NULL;
4497
4498 if (bs == NULL)
4499 return bs;
4500
4501 for (; bs != NULL; bs = bs->next)
4502 {
4503 tmp = (bpstat) xmalloc (sizeof (*tmp));
4504 memcpy (tmp, bs, sizeof (*tmp));
4505 incref_counted_command_line (tmp->commands);
4506 incref_bp_location (tmp->bp_location_at);
4507 if (bs->old_val != NULL)
4508 {
4509 tmp->old_val = value_copy (bs->old_val);
4510 release_value (tmp->old_val);
4511 }
4512
4513 if (p == NULL)
4514 /* This is the first thing in the chain. */
4515 retval = tmp;
4516 else
4517 p->next = tmp;
4518 p = tmp;
4519 }
4520 p->next = NULL;
4521 return retval;
4522 }
4523
4524 /* Find the bpstat associated with this breakpoint. */
4525
4526 bpstat
4527 bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
4528 {
4529 if (bsp == NULL)
4530 return NULL;
4531
4532 for (; bsp != NULL; bsp = bsp->next)
4533 {
4534 if (bsp->breakpoint_at == breakpoint)
4535 return bsp;
4536 }
4537 return NULL;
4538 }
4539
4540 /* See breakpoint.h. */
4541
4542 int
4543 bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
4544 {
4545 for (; bsp != NULL; bsp = bsp->next)
4546 {
4547 if (bsp->breakpoint_at == NULL)
4548 {
4549 /* A moribund location can never explain a signal other than
4550 GDB_SIGNAL_TRAP. */
4551 if (sig == GDB_SIGNAL_TRAP)
4552 return 1;
4553 }
4554 else
4555 {
4556 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4557 sig))
4558 return 1;
4559 }
4560 }
4561
4562 return 0;
4563 }
4564
4565 /* Put in *NUM the breakpoint number of the first breakpoint we are
4566 stopped at. *BSP upon return is a bpstat which points to the
4567 remaining breakpoints stopped at (but which is not guaranteed to be
4568 good for anything but further calls to bpstat_num).
4569
4570 Return 0 if passed a bpstat which does not indicate any breakpoints.
4571 Return -1 if stopped at a breakpoint that has been deleted since
4572 we set it.
4573 Return 1 otherwise. */
4574
4575 int
4576 bpstat_num (bpstat *bsp, int *num)
4577 {
4578 struct breakpoint *b;
4579
4580 if ((*bsp) == NULL)
4581 return 0; /* No more breakpoint values */
4582
4583 /* We assume we'll never have several bpstats that correspond to a
4584 single breakpoint -- otherwise, this function might return the
4585 same number more than once and this will look ugly. */
4586 b = (*bsp)->breakpoint_at;
4587 *bsp = (*bsp)->next;
4588 if (b == NULL)
4589 return -1; /* breakpoint that's been deleted since */
4590
4591 *num = b->number; /* We have its number */
4592 return 1;
4593 }
4594
4595 /* See breakpoint.h. */
4596
4597 void
4598 bpstat_clear_actions (void)
4599 {
4600 struct thread_info *tp;
4601 bpstat bs;
4602
4603 if (ptid_equal (inferior_ptid, null_ptid))
4604 return;
4605
4606 tp = find_thread_ptid (inferior_ptid);
4607 if (tp == NULL)
4608 return;
4609
4610 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
4611 {
4612 decref_counted_command_line (&bs->commands);
4613
4614 if (bs->old_val != NULL)
4615 {
4616 value_free (bs->old_val);
4617 bs->old_val = NULL;
4618 }
4619 }
4620 }
4621
4622 /* Called when a command is about to proceed the inferior. */
4623
4624 static void
4625 breakpoint_about_to_proceed (void)
4626 {
4627 if (!ptid_equal (inferior_ptid, null_ptid))
4628 {
4629 struct thread_info *tp = inferior_thread ();
4630
4631 /* Allow inferior function calls in breakpoint commands to not
4632 interrupt the command list. When the call finishes
4633 successfully, the inferior will be standing at the same
4634 breakpoint as if nothing happened. */
4635 if (tp->control.in_infcall)
4636 return;
4637 }
4638
4639 breakpoint_proceeded = 1;
4640 }
4641
4642 /* Stub for cleaning up our state if we error-out of a breakpoint
4643 command. */
4644 static void
4645 cleanup_executing_breakpoints (void *ignore)
4646 {
4647 executing_breakpoint_commands = 0;
4648 }
4649
4650 /* Return non-zero iff CMD as the first line of a command sequence is `silent'
4651 or its equivalent. */
4652
4653 static int
4654 command_line_is_silent (struct command_line *cmd)
4655 {
4656 return cmd && (strcmp ("silent", cmd->line) == 0);
4657 }
4658
4659 /* Execute all the commands associated with all the breakpoints at
4660 this location. Any of these commands could cause the process to
4661 proceed beyond this point, etc. We look out for such changes by
4662 checking the global "breakpoint_proceeded" after each command.
4663
4664 Returns true if a breakpoint command resumed the inferior. In that
4665 case, it is the caller's responsibility to recall it again with the
4666 bpstat of the current thread. */
4667
4668 static int
4669 bpstat_do_actions_1 (bpstat *bsp)
4670 {
4671 bpstat bs;
4672 struct cleanup *old_chain;
4673 int again = 0;
4674
4675 /* Avoid endless recursion if a `source' command is contained
4676 in bs->commands. */
4677 if (executing_breakpoint_commands)
4678 return 0;
4679
4680 executing_breakpoint_commands = 1;
4681 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
4682
4683 prevent_dont_repeat ();
4684
4685 /* This pointer will iterate over the list of bpstat's. */
4686 bs = *bsp;
4687
4688 breakpoint_proceeded = 0;
4689 for (; bs != NULL; bs = bs->next)
4690 {
4691 struct counted_command_line *ccmd;
4692 struct command_line *cmd;
4693 struct cleanup *this_cmd_tree_chain;
4694
4695 /* Take ownership of the BSP's command tree, if it has one.
4696
4697 The command tree could legitimately contain commands like
4698 'step' and 'next', which call clear_proceed_status, which
4699 frees stop_bpstat's command tree. To make sure this doesn't
4700 free the tree we're executing out from under us, we need to
4701 take ownership of the tree ourselves. Since a given bpstat's
4702 commands are only executed once, we don't need to copy it; we
4703 can clear the pointer in the bpstat, and make sure we free
4704 the tree when we're done. */
4705 ccmd = bs->commands;
4706 bs->commands = NULL;
4707 this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
4708 cmd = ccmd ? ccmd->commands : NULL;
4709 if (command_line_is_silent (cmd))
4710 {
4711 /* The action has been already done by bpstat_stop_status. */
4712 cmd = cmd->next;
4713 }
4714
4715 while (cmd != NULL)
4716 {
4717 execute_control_command (cmd);
4718
4719 if (breakpoint_proceeded)
4720 break;
4721 else
4722 cmd = cmd->next;
4723 }
4724
4725 /* We can free this command tree now. */
4726 do_cleanups (this_cmd_tree_chain);
4727
4728 if (breakpoint_proceeded)
4729 {
4730 if (interpreter_async)
4731 /* If we are in async mode, then the target might be still
4732 running, not stopped at any breakpoint, so nothing for
4733 us to do here -- just return to the event loop. */
4734 ;
4735 else
4736 /* In sync mode, when execute_control_command returns
4737 we're already standing on the next breakpoint.
4738 Breakpoint commands for that stop were not run, since
4739 execute_command does not run breakpoint commands --
4740 only command_line_handler does, but that one is not
4741 involved in execution of breakpoint commands. So, we
4742 can now execute breakpoint commands. It should be
4743 noted that making execute_command do bpstat actions is
4744 not an option -- in this case we'll have recursive
4745 invocation of bpstat for each breakpoint with a
4746 command, and can easily blow up GDB stack. Instead, we
4747 return true, which will trigger the caller to recall us
4748 with the new stop_bpstat. */
4749 again = 1;
4750 break;
4751 }
4752 }
4753 do_cleanups (old_chain);
4754 return again;
4755 }
4756
4757 void
4758 bpstat_do_actions (void)
4759 {
4760 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4761
4762 /* Do any commands attached to breakpoint we are stopped at. */
4763 while (!ptid_equal (inferior_ptid, null_ptid)
4764 && target_has_execution
4765 && !is_exited (inferior_ptid)
4766 && !is_executing (inferior_ptid))
4767 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4768 and only return when it is stopped at the next breakpoint, we
4769 keep doing breakpoint actions until it returns false to
4770 indicate the inferior was not resumed. */
4771 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
4772 break;
4773
4774 discard_cleanups (cleanup_if_error);
4775 }
4776
4777 /* Print out the (old or new) value associated with a watchpoint. */
4778
4779 static void
4780 watchpoint_value_print (struct value *val, struct ui_file *stream)
4781 {
4782 if (val == NULL)
4783 fprintf_unfiltered (stream, _("<unreadable>"));
4784 else
4785 {
4786 struct value_print_options opts;
4787 get_user_print_options (&opts);
4788 value_print (val, stream, &opts);
4789 }
4790 }
4791
4792 /* Generic routine for printing messages indicating why we
4793 stopped. The behavior of this function depends on the value
4794 'print_it' in the bpstat structure. Under some circumstances we
4795 may decide not to print anything here and delegate the task to
4796 normal_stop(). */
4797
4798 static enum print_stop_action
4799 print_bp_stop_message (bpstat bs)
4800 {
4801 switch (bs->print_it)
4802 {
4803 case print_it_noop:
4804 /* Nothing should be printed for this bpstat entry. */
4805 return PRINT_UNKNOWN;
4806 break;
4807
4808 case print_it_done:
4809 /* We still want to print the frame, but we already printed the
4810 relevant messages. */
4811 return PRINT_SRC_AND_LOC;
4812 break;
4813
4814 case print_it_normal:
4815 {
4816 struct breakpoint *b = bs->breakpoint_at;
4817
4818 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4819 which has since been deleted. */
4820 if (b == NULL)
4821 return PRINT_UNKNOWN;
4822
4823 /* Normal case. Call the breakpoint's print_it method. */
4824 return b->ops->print_it (bs);
4825 }
4826 break;
4827
4828 default:
4829 internal_error (__FILE__, __LINE__,
4830 _("print_bp_stop_message: unrecognized enum value"));
4831 break;
4832 }
4833 }
4834
4835 /* A helper function that prints a shared library stopped event. */
4836
4837 static void
4838 print_solib_event (int is_catchpoint)
4839 {
4840 int any_deleted
4841 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4842 int any_added
4843 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4844
4845 if (!is_catchpoint)
4846 {
4847 if (any_added || any_deleted)
4848 ui_out_text (current_uiout,
4849 _("Stopped due to shared library event:\n"));
4850 else
4851 ui_out_text (current_uiout,
4852 _("Stopped due to shared library event (no "
4853 "libraries added or removed)\n"));
4854 }
4855
4856 if (ui_out_is_mi_like_p (current_uiout))
4857 ui_out_field_string (current_uiout, "reason",
4858 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4859
4860 if (any_deleted)
4861 {
4862 struct cleanup *cleanup;
4863 char *name;
4864 int ix;
4865
4866 ui_out_text (current_uiout, _(" Inferior unloaded "));
4867 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4868 "removed");
4869 for (ix = 0;
4870 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4871 ix, name);
4872 ++ix)
4873 {
4874 if (ix > 0)
4875 ui_out_text (current_uiout, " ");
4876 ui_out_field_string (current_uiout, "library", name);
4877 ui_out_text (current_uiout, "\n");
4878 }
4879
4880 do_cleanups (cleanup);
4881 }
4882
4883 if (any_added)
4884 {
4885 struct so_list *iter;
4886 int ix;
4887 struct cleanup *cleanup;
4888
4889 ui_out_text (current_uiout, _(" Inferior loaded "));
4890 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4891 "added");
4892 for (ix = 0;
4893 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4894 ix, iter);
4895 ++ix)
4896 {
4897 if (ix > 0)
4898 ui_out_text (current_uiout, " ");
4899 ui_out_field_string (current_uiout, "library", iter->so_name);
4900 ui_out_text (current_uiout, "\n");
4901 }
4902
4903 do_cleanups (cleanup);
4904 }
4905 }
4906
4907 /* Print a message indicating what happened. This is called from
4908 normal_stop(). The input to this routine is the head of the bpstat
4909 list - a list of the eventpoints that caused this stop. KIND is
4910 the target_waitkind for the stopping event. This
4911 routine calls the generic print routine for printing a message
4912 about reasons for stopping. This will print (for example) the
4913 "Breakpoint n," part of the output. The return value of this
4914 routine is one of:
4915
4916 PRINT_UNKNOWN: Means we printed nothing.
4917 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4918 code to print the location. An example is
4919 "Breakpoint 1, " which should be followed by
4920 the location.
4921 PRINT_SRC_ONLY: Means we printed something, but there is no need
4922 to also print the location part of the message.
4923 An example is the catch/throw messages, which
4924 don't require a location appended to the end.
4925 PRINT_NOTHING: We have done some printing and we don't need any
4926 further info to be printed. */
4927
4928 enum print_stop_action
4929 bpstat_print (bpstat bs, int kind)
4930 {
4931 enum print_stop_action val;
4932
4933 /* Maybe another breakpoint in the chain caused us to stop.
4934 (Currently all watchpoints go on the bpstat whether hit or not.
4935 That probably could (should) be changed, provided care is taken
4936 with respect to bpstat_explains_signal). */
4937 for (; bs; bs = bs->next)
4938 {
4939 val = print_bp_stop_message (bs);
4940 if (val == PRINT_SRC_ONLY
4941 || val == PRINT_SRC_AND_LOC
4942 || val == PRINT_NOTHING)
4943 return val;
4944 }
4945
4946 /* If we had hit a shared library event breakpoint,
4947 print_bp_stop_message would print out this message. If we hit an
4948 OS-level shared library event, do the same thing. */
4949 if (kind == TARGET_WAITKIND_LOADED)
4950 {
4951 print_solib_event (0);
4952 return PRINT_NOTHING;
4953 }
4954
4955 /* We reached the end of the chain, or we got a null BS to start
4956 with and nothing was printed. */
4957 return PRINT_UNKNOWN;
4958 }
4959
4960 /* Evaluate the expression EXP and return 1 if value is zero.
4961 This returns the inverse of the condition because it is called
4962 from catch_errors which returns 0 if an exception happened, and if an
4963 exception happens we want execution to stop.
4964 The argument is a "struct expression *" that has been cast to a
4965 "void *" to make it pass through catch_errors. */
4966
4967 static int
4968 breakpoint_cond_eval (void *exp)
4969 {
4970 struct value *mark = value_mark ();
4971 int i = !value_true (evaluate_expression ((struct expression *) exp));
4972
4973 value_free_to_mark (mark);
4974 return i;
4975 }
4976
4977 /* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
4978
4979 static bpstat
4980 bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
4981 {
4982 bpstat bs;
4983
4984 bs = (bpstat) xmalloc (sizeof (*bs));
4985 bs->next = NULL;
4986 **bs_link_pointer = bs;
4987 *bs_link_pointer = &bs->next;
4988 bs->breakpoint_at = bl->owner;
4989 bs->bp_location_at = bl;
4990 incref_bp_location (bl);
4991 /* If the condition is false, etc., don't do the commands. */
4992 bs->commands = NULL;
4993 bs->old_val = NULL;
4994 bs->print_it = print_it_normal;
4995 return bs;
4996 }
4997 \f
4998 /* The target has stopped with waitstatus WS. Check if any hardware
4999 watchpoints have triggered, according to the target. */
5000
5001 int
5002 watchpoints_triggered (struct target_waitstatus *ws)
5003 {
5004 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
5005 CORE_ADDR addr;
5006 struct breakpoint *b;
5007
5008 if (!stopped_by_watchpoint)
5009 {
5010 /* We were not stopped by a watchpoint. Mark all watchpoints
5011 as not triggered. */
5012 ALL_BREAKPOINTS (b)
5013 if (is_hardware_watchpoint (b))
5014 {
5015 struct watchpoint *w = (struct watchpoint *) b;
5016
5017 w->watchpoint_triggered = watch_triggered_no;
5018 }
5019
5020 return 0;
5021 }
5022
5023 if (!target_stopped_data_address (&current_target, &addr))
5024 {
5025 /* We were stopped by a watchpoint, but we don't know where.
5026 Mark all watchpoints as unknown. */
5027 ALL_BREAKPOINTS (b)
5028 if (is_hardware_watchpoint (b))
5029 {
5030 struct watchpoint *w = (struct watchpoint *) b;
5031
5032 w->watchpoint_triggered = watch_triggered_unknown;
5033 }
5034
5035 return 1;
5036 }
5037
5038 /* The target could report the data address. Mark watchpoints
5039 affected by this data address as triggered, and all others as not
5040 triggered. */
5041
5042 ALL_BREAKPOINTS (b)
5043 if (is_hardware_watchpoint (b))
5044 {
5045 struct watchpoint *w = (struct watchpoint *) b;
5046 struct bp_location *loc;
5047
5048 w->watchpoint_triggered = watch_triggered_no;
5049 for (loc = b->loc; loc; loc = loc->next)
5050 {
5051 if (is_masked_watchpoint (b))
5052 {
5053 CORE_ADDR newaddr = addr & w->hw_wp_mask;
5054 CORE_ADDR start = loc->address & w->hw_wp_mask;
5055
5056 if (newaddr == start)
5057 {
5058 w->watchpoint_triggered = watch_triggered_yes;
5059 break;
5060 }
5061 }
5062 /* Exact match not required. Within range is sufficient. */
5063 else if (target_watchpoint_addr_within_range (&current_target,
5064 addr, loc->address,
5065 loc->length))
5066 {
5067 w->watchpoint_triggered = watch_triggered_yes;
5068 break;
5069 }
5070 }
5071 }
5072
5073 return 1;
5074 }
5075
5076 /* Possible return values for watchpoint_check (this can't be an enum
5077 because of check_errors). */
5078 /* The watchpoint has been deleted. */
5079 #define WP_DELETED 1
5080 /* The value has changed. */
5081 #define WP_VALUE_CHANGED 2
5082 /* The value has not changed. */
5083 #define WP_VALUE_NOT_CHANGED 3
5084 /* Ignore this watchpoint, no matter if the value changed or not. */
5085 #define WP_IGNORE 4
5086
5087 #define BP_TEMPFLAG 1
5088 #define BP_HARDWAREFLAG 2
5089
5090 /* Evaluate watchpoint condition expression and check if its value
5091 changed.
5092
5093 P should be a pointer to struct bpstat, but is defined as a void *
5094 in order for this function to be usable with catch_errors. */
5095
5096 static int
5097 watchpoint_check (void *p)
5098 {
5099 bpstat bs = (bpstat) p;
5100 struct watchpoint *b;
5101 struct frame_info *fr;
5102 int within_current_scope;
5103
5104 /* BS is built from an existing struct breakpoint. */
5105 gdb_assert (bs->breakpoint_at != NULL);
5106 b = (struct watchpoint *) bs->breakpoint_at;
5107
5108 /* If this is a local watchpoint, we only want to check if the
5109 watchpoint frame is in scope if the current thread is the thread
5110 that was used to create the watchpoint. */
5111 if (!watchpoint_in_thread_scope (b))
5112 return WP_IGNORE;
5113
5114 if (b->exp_valid_block == NULL)
5115 within_current_scope = 1;
5116 else
5117 {
5118 struct frame_info *frame = get_current_frame ();
5119 struct gdbarch *frame_arch = get_frame_arch (frame);
5120 CORE_ADDR frame_pc = get_frame_pc (frame);
5121
5122 /* stack_frame_destroyed_p() returns a non-zero value if we're
5123 still in the function but the stack frame has already been
5124 invalidated. Since we can't rely on the values of local
5125 variables after the stack has been destroyed, we are treating
5126 the watchpoint in that state as `not changed' without further
5127 checking. Don't mark watchpoints as changed if the current
5128 frame is in an epilogue - even if they are in some other
5129 frame, our view of the stack is likely to be wrong and
5130 frame_find_by_id could error out. */
5131 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
5132 return WP_IGNORE;
5133
5134 fr = frame_find_by_id (b->watchpoint_frame);
5135 within_current_scope = (fr != NULL);
5136
5137 /* If we've gotten confused in the unwinder, we might have
5138 returned a frame that can't describe this variable. */
5139 if (within_current_scope)
5140 {
5141 struct symbol *function;
5142
5143 function = get_frame_function (fr);
5144 if (function == NULL
5145 || !contained_in (b->exp_valid_block,
5146 SYMBOL_BLOCK_VALUE (function)))
5147 within_current_scope = 0;
5148 }
5149
5150 if (within_current_scope)
5151 /* If we end up stopping, the current frame will get selected
5152 in normal_stop. So this call to select_frame won't affect
5153 the user. */
5154 select_frame (fr);
5155 }
5156
5157 if (within_current_scope)
5158 {
5159 /* We use value_{,free_to_}mark because it could be a *long*
5160 time before we return to the command level and call
5161 free_all_values. We can't call free_all_values because we
5162 might be in the middle of evaluating a function call. */
5163
5164 int pc = 0;
5165 struct value *mark;
5166 struct value *new_val;
5167
5168 if (is_masked_watchpoint (&b->base))
5169 /* Since we don't know the exact trigger address (from
5170 stopped_data_address), just tell the user we've triggered
5171 a mask watchpoint. */
5172 return WP_VALUE_CHANGED;
5173
5174 mark = value_mark ();
5175 fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL, 0);
5176
5177 if (b->val_bitsize != 0)
5178 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5179
5180 /* We use value_equal_contents instead of value_equal because
5181 the latter coerces an array to a pointer, thus comparing just
5182 the address of the array instead of its contents. This is
5183 not what we want. */
5184 if ((b->val != NULL) != (new_val != NULL)
5185 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
5186 {
5187 if (new_val != NULL)
5188 {
5189 release_value (new_val);
5190 value_free_to_mark (mark);
5191 }
5192 bs->old_val = b->val;
5193 b->val = new_val;
5194 b->val_valid = 1;
5195 return WP_VALUE_CHANGED;
5196 }
5197 else
5198 {
5199 /* Nothing changed. */
5200 value_free_to_mark (mark);
5201 return WP_VALUE_NOT_CHANGED;
5202 }
5203 }
5204 else
5205 {
5206 struct ui_out *uiout = current_uiout;
5207
5208 /* This seems like the only logical thing to do because
5209 if we temporarily ignored the watchpoint, then when
5210 we reenter the block in which it is valid it contains
5211 garbage (in the case of a function, it may have two
5212 garbage values, one before and one after the prologue).
5213 So we can't even detect the first assignment to it and
5214 watch after that (since the garbage may or may not equal
5215 the first value assigned). */
5216 /* We print all the stop information in
5217 breakpoint_ops->print_it, but in this case, by the time we
5218 call breakpoint_ops->print_it this bp will be deleted
5219 already. So we have no choice but print the information
5220 here. */
5221 if (ui_out_is_mi_like_p (uiout))
5222 ui_out_field_string
5223 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
5224 ui_out_text (uiout, "\nWatchpoint ");
5225 ui_out_field_int (uiout, "wpnum", b->base.number);
5226 ui_out_text (uiout,
5227 " deleted because the program has left the block in\n\
5228 which its expression is valid.\n");
5229
5230 /* Make sure the watchpoint's commands aren't executed. */
5231 decref_counted_command_line (&b->base.commands);
5232 watchpoint_del_at_next_stop (b);
5233
5234 return WP_DELETED;
5235 }
5236 }
5237
5238 /* Return true if it looks like target has stopped due to hitting
5239 breakpoint location BL. This function does not check if we should
5240 stop, only if BL explains the stop. */
5241
5242 static int
5243 bpstat_check_location (const struct bp_location *bl,
5244 struct address_space *aspace, CORE_ADDR bp_addr,
5245 const struct target_waitstatus *ws)
5246 {
5247 struct breakpoint *b = bl->owner;
5248
5249 /* BL is from an existing breakpoint. */
5250 gdb_assert (b != NULL);
5251
5252 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
5253 }
5254
5255 /* Determine if the watched values have actually changed, and we
5256 should stop. If not, set BS->stop to 0. */
5257
5258 static void
5259 bpstat_check_watchpoint (bpstat bs)
5260 {
5261 const struct bp_location *bl;
5262 struct watchpoint *b;
5263
5264 /* BS is built for existing struct breakpoint. */
5265 bl = bs->bp_location_at;
5266 gdb_assert (bl != NULL);
5267 b = (struct watchpoint *) bs->breakpoint_at;
5268 gdb_assert (b != NULL);
5269
5270 {
5271 int must_check_value = 0;
5272
5273 if (b->base.type == bp_watchpoint)
5274 /* For a software watchpoint, we must always check the
5275 watched value. */
5276 must_check_value = 1;
5277 else if (b->watchpoint_triggered == watch_triggered_yes)
5278 /* We have a hardware watchpoint (read, write, or access)
5279 and the target earlier reported an address watched by
5280 this watchpoint. */
5281 must_check_value = 1;
5282 else if (b->watchpoint_triggered == watch_triggered_unknown
5283 && b->base.type == bp_hardware_watchpoint)
5284 /* We were stopped by a hardware watchpoint, but the target could
5285 not report the data address. We must check the watchpoint's
5286 value. Access and read watchpoints are out of luck; without
5287 a data address, we can't figure it out. */
5288 must_check_value = 1;
5289
5290 if (must_check_value)
5291 {
5292 char *message
5293 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
5294 b->base.number);
5295 struct cleanup *cleanups = make_cleanup (xfree, message);
5296 int e = catch_errors (watchpoint_check, bs, message,
5297 RETURN_MASK_ALL);
5298 do_cleanups (cleanups);
5299 switch (e)
5300 {
5301 case WP_DELETED:
5302 /* We've already printed what needs to be printed. */
5303 bs->print_it = print_it_done;
5304 /* Stop. */
5305 break;
5306 case WP_IGNORE:
5307 bs->print_it = print_it_noop;
5308 bs->stop = 0;
5309 break;
5310 case WP_VALUE_CHANGED:
5311 if (b->base.type == bp_read_watchpoint)
5312 {
5313 /* There are two cases to consider here:
5314
5315 1. We're watching the triggered memory for reads.
5316 In that case, trust the target, and always report
5317 the watchpoint hit to the user. Even though
5318 reads don't cause value changes, the value may
5319 have changed since the last time it was read, and
5320 since we're not trapping writes, we will not see
5321 those, and as such we should ignore our notion of
5322 old value.
5323
5324 2. We're watching the triggered memory for both
5325 reads and writes. There are two ways this may
5326 happen:
5327
5328 2.1. This is a target that can't break on data
5329 reads only, but can break on accesses (reads or
5330 writes), such as e.g., x86. We detect this case
5331 at the time we try to insert read watchpoints.
5332
5333 2.2. Otherwise, the target supports read
5334 watchpoints, but, the user set an access or write
5335 watchpoint watching the same memory as this read
5336 watchpoint.
5337
5338 If we're watching memory writes as well as reads,
5339 ignore watchpoint hits when we find that the
5340 value hasn't changed, as reads don't cause
5341 changes. This still gives false positives when
5342 the program writes the same value to memory as
5343 what there was already in memory (we will confuse
5344 it for a read), but it's much better than
5345 nothing. */
5346
5347 int other_write_watchpoint = 0;
5348
5349 if (bl->watchpoint_type == hw_read)
5350 {
5351 struct breakpoint *other_b;
5352
5353 ALL_BREAKPOINTS (other_b)
5354 if (other_b->type == bp_hardware_watchpoint
5355 || other_b->type == bp_access_watchpoint)
5356 {
5357 struct watchpoint *other_w =
5358 (struct watchpoint *) other_b;
5359
5360 if (other_w->watchpoint_triggered
5361 == watch_triggered_yes)
5362 {
5363 other_write_watchpoint = 1;
5364 break;
5365 }
5366 }
5367 }
5368
5369 if (other_write_watchpoint
5370 || bl->watchpoint_type == hw_access)
5371 {
5372 /* We're watching the same memory for writes,
5373 and the value changed since the last time we
5374 updated it, so this trap must be for a write.
5375 Ignore it. */
5376 bs->print_it = print_it_noop;
5377 bs->stop = 0;
5378 }
5379 }
5380 break;
5381 case WP_VALUE_NOT_CHANGED:
5382 if (b->base.type == bp_hardware_watchpoint
5383 || b->base.type == bp_watchpoint)
5384 {
5385 /* Don't stop: write watchpoints shouldn't fire if
5386 the value hasn't changed. */
5387 bs->print_it = print_it_noop;
5388 bs->stop = 0;
5389 }
5390 /* Stop. */
5391 break;
5392 default:
5393 /* Can't happen. */
5394 case 0:
5395 /* Error from catch_errors. */
5396 printf_filtered (_("Watchpoint %d deleted.\n"), b->base.number);
5397 watchpoint_del_at_next_stop (b);
5398 /* We've already printed what needs to be printed. */
5399 bs->print_it = print_it_done;
5400 break;
5401 }
5402 }
5403 else /* must_check_value == 0 */
5404 {
5405 /* This is a case where some watchpoint(s) triggered, but
5406 not at the address of this watchpoint, or else no
5407 watchpoint triggered after all. So don't print
5408 anything for this watchpoint. */
5409 bs->print_it = print_it_noop;
5410 bs->stop = 0;
5411 }
5412 }
5413 }
5414
5415 /* For breakpoints that are currently marked as telling gdb to stop,
5416 check conditions (condition proper, frame, thread and ignore count)
5417 of breakpoint referred to by BS. If we should not stop for this
5418 breakpoint, set BS->stop to 0. */
5419
5420 static void
5421 bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5422 {
5423 const struct bp_location *bl;
5424 struct breakpoint *b;
5425 int value_is_zero = 0;
5426 struct expression *cond;
5427
5428 gdb_assert (bs->stop);
5429
5430 /* BS is built for existing struct breakpoint. */
5431 bl = bs->bp_location_at;
5432 gdb_assert (bl != NULL);
5433 b = bs->breakpoint_at;
5434 gdb_assert (b != NULL);
5435
5436 /* Even if the target evaluated the condition on its end and notified GDB, we
5437 need to do so again since GDB does not know if we stopped due to a
5438 breakpoint or a single step breakpoint. */
5439
5440 if (frame_id_p (b->frame_id)
5441 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
5442 {
5443 bs->stop = 0;
5444 return;
5445 }
5446
5447 /* If this is a thread/task-specific breakpoint, don't waste cpu
5448 evaluating the condition if this isn't the specified
5449 thread/task. */
5450 if ((b->thread != -1 && b->thread != pid_to_thread_id (ptid))
5451 || (b->task != 0 && b->task != ada_get_task_number (ptid)))
5452
5453 {
5454 bs->stop = 0;
5455 return;
5456 }
5457
5458 /* Evaluate extension language breakpoints that have a "stop" method
5459 implemented. */
5460 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
5461
5462 if (is_watchpoint (b))
5463 {
5464 struct watchpoint *w = (struct watchpoint *) b;
5465
5466 cond = w->cond_exp;
5467 }
5468 else
5469 cond = bl->cond;
5470
5471 if (cond && b->disposition != disp_del_at_next_stop)
5472 {
5473 int within_current_scope = 1;
5474 struct watchpoint * w;
5475
5476 /* We use value_mark and value_free_to_mark because it could
5477 be a long time before we return to the command level and
5478 call free_all_values. We can't call free_all_values
5479 because we might be in the middle of evaluating a
5480 function call. */
5481 struct value *mark = value_mark ();
5482
5483 if (is_watchpoint (b))
5484 w = (struct watchpoint *) b;
5485 else
5486 w = NULL;
5487
5488 /* Need to select the frame, with all that implies so that
5489 the conditions will have the right context. Because we
5490 use the frame, we will not see an inlined function's
5491 variables when we arrive at a breakpoint at the start
5492 of the inlined function; the current frame will be the
5493 call site. */
5494 if (w == NULL || w->cond_exp_valid_block == NULL)
5495 select_frame (get_current_frame ());
5496 else
5497 {
5498 struct frame_info *frame;
5499
5500 /* For local watchpoint expressions, which particular
5501 instance of a local is being watched matters, so we
5502 keep track of the frame to evaluate the expression
5503 in. To evaluate the condition however, it doesn't
5504 really matter which instantiation of the function
5505 where the condition makes sense triggers the
5506 watchpoint. This allows an expression like "watch
5507 global if q > 10" set in `func', catch writes to
5508 global on all threads that call `func', or catch
5509 writes on all recursive calls of `func' by a single
5510 thread. We simply always evaluate the condition in
5511 the innermost frame that's executing where it makes
5512 sense to evaluate the condition. It seems
5513 intuitive. */
5514 frame = block_innermost_frame (w->cond_exp_valid_block);
5515 if (frame != NULL)
5516 select_frame (frame);
5517 else
5518 within_current_scope = 0;
5519 }
5520 if (within_current_scope)
5521 value_is_zero
5522 = catch_errors (breakpoint_cond_eval, cond,
5523 "Error in testing breakpoint condition:\n",
5524 RETURN_MASK_ALL);
5525 else
5526 {
5527 warning (_("Watchpoint condition cannot be tested "
5528 "in the current scope"));
5529 /* If we failed to set the right context for this
5530 watchpoint, unconditionally report it. */
5531 value_is_zero = 0;
5532 }
5533 /* FIXME-someday, should give breakpoint #. */
5534 value_free_to_mark (mark);
5535 }
5536
5537 if (cond && value_is_zero)
5538 {
5539 bs->stop = 0;
5540 }
5541 else if (b->ignore_count > 0)
5542 {
5543 b->ignore_count--;
5544 bs->stop = 0;
5545 /* Increase the hit count even though we don't stop. */
5546 ++(b->hit_count);
5547 observer_notify_breakpoint_modified (b);
5548 }
5549 }
5550
5551 /* Returns true if we need to track moribund locations of LOC's type
5552 on the current target. */
5553
5554 static int
5555 need_moribund_for_location_type (struct bp_location *loc)
5556 {
5557 return ((loc->loc_type == bp_loc_software_breakpoint
5558 && !target_supports_stopped_by_sw_breakpoint ())
5559 || (loc->loc_type == bp_loc_hardware_breakpoint
5560 && !target_supports_stopped_by_hw_breakpoint ()));
5561 }
5562
5563
5564 /* Get a bpstat associated with having just stopped at address
5565 BP_ADDR in thread PTID.
5566
5567 Determine whether we stopped at a breakpoint, etc, or whether we
5568 don't understand this stop. Result is a chain of bpstat's such
5569 that:
5570
5571 if we don't understand the stop, the result is a null pointer.
5572
5573 if we understand why we stopped, the result is not null.
5574
5575 Each element of the chain refers to a particular breakpoint or
5576 watchpoint at which we have stopped. (We may have stopped for
5577 several reasons concurrently.)
5578
5579 Each element of the chain has valid next, breakpoint_at,
5580 commands, FIXME??? fields. */
5581
5582 bpstat
5583 bpstat_stop_status (struct address_space *aspace,
5584 CORE_ADDR bp_addr, ptid_t ptid,
5585 const struct target_waitstatus *ws)
5586 {
5587 struct breakpoint *b = NULL;
5588 struct bp_location *bl;
5589 struct bp_location *loc;
5590 /* First item of allocated bpstat's. */
5591 bpstat bs_head = NULL, *bs_link = &bs_head;
5592 /* Pointer to the last thing in the chain currently. */
5593 bpstat bs;
5594 int ix;
5595 int need_remove_insert;
5596 int removed_any;
5597
5598 /* First, build the bpstat chain with locations that explain a
5599 target stop, while being careful to not set the target running,
5600 as that may invalidate locations (in particular watchpoint
5601 locations are recreated). Resuming will happen here with
5602 breakpoint conditions or watchpoint expressions that include
5603 inferior function calls. */
5604
5605 ALL_BREAKPOINTS (b)
5606 {
5607 if (!breakpoint_enabled (b))
5608 continue;
5609
5610 for (bl = b->loc; bl != NULL; bl = bl->next)
5611 {
5612 /* For hardware watchpoints, we look only at the first
5613 location. The watchpoint_check function will work on the
5614 entire expression, not the individual locations. For
5615 read watchpoints, the watchpoints_triggered function has
5616 checked all locations already. */
5617 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5618 break;
5619
5620 if (!bl->enabled || bl->shlib_disabled)
5621 continue;
5622
5623 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
5624 continue;
5625
5626 /* Come here if it's a watchpoint, or if the break address
5627 matches. */
5628
5629 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
5630 explain stop. */
5631
5632 /* Assume we stop. Should we find a watchpoint that is not
5633 actually triggered, or if the condition of the breakpoint
5634 evaluates as false, we'll reset 'stop' to 0. */
5635 bs->stop = 1;
5636 bs->print = 1;
5637
5638 /* If this is a scope breakpoint, mark the associated
5639 watchpoint as triggered so that we will handle the
5640 out-of-scope event. We'll get to the watchpoint next
5641 iteration. */
5642 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
5643 {
5644 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5645
5646 w->watchpoint_triggered = watch_triggered_yes;
5647 }
5648 }
5649 }
5650
5651 /* Check if a moribund breakpoint explains the stop. */
5652 if (!target_supports_stopped_by_sw_breakpoint ()
5653 || !target_supports_stopped_by_hw_breakpoint ())
5654 {
5655 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
5656 {
5657 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5658 && need_moribund_for_location_type (loc))
5659 {
5660 bs = bpstat_alloc (loc, &bs_link);
5661 /* For hits of moribund locations, we should just proceed. */
5662 bs->stop = 0;
5663 bs->print = 0;
5664 bs->print_it = print_it_noop;
5665 }
5666 }
5667 }
5668
5669 /* A bit of special processing for shlib breakpoints. We need to
5670 process solib loading here, so that the lists of loaded and
5671 unloaded libraries are correct before we handle "catch load" and
5672 "catch unload". */
5673 for (bs = bs_head; bs != NULL; bs = bs->next)
5674 {
5675 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
5676 {
5677 handle_solib_event ();
5678 break;
5679 }
5680 }
5681
5682 /* Now go through the locations that caused the target to stop, and
5683 check whether we're interested in reporting this stop to higher
5684 layers, or whether we should resume the target transparently. */
5685
5686 removed_any = 0;
5687
5688 for (bs = bs_head; bs != NULL; bs = bs->next)
5689 {
5690 if (!bs->stop)
5691 continue;
5692
5693 b = bs->breakpoint_at;
5694 b->ops->check_status (bs);
5695 if (bs->stop)
5696 {
5697 bpstat_check_breakpoint_conditions (bs, ptid);
5698
5699 if (bs->stop)
5700 {
5701 ++(b->hit_count);
5702 observer_notify_breakpoint_modified (b);
5703
5704 /* We will stop here. */
5705 if (b->disposition == disp_disable)
5706 {
5707 --(b->enable_count);
5708 if (b->enable_count <= 0)
5709 b->enable_state = bp_disabled;
5710 removed_any = 1;
5711 }
5712 if (b->silent)
5713 bs->print = 0;
5714 bs->commands = b->commands;
5715 incref_counted_command_line (bs->commands);
5716 if (command_line_is_silent (bs->commands
5717 ? bs->commands->commands : NULL))
5718 bs->print = 0;
5719
5720 b->ops->after_condition_true (bs);
5721 }
5722
5723 }
5724
5725 /* Print nothing for this entry if we don't stop or don't
5726 print. */
5727 if (!bs->stop || !bs->print)
5728 bs->print_it = print_it_noop;
5729 }
5730
5731 /* If we aren't stopping, the value of some hardware watchpoint may
5732 not have changed, but the intermediate memory locations we are
5733 watching may have. Don't bother if we're stopping; this will get
5734 done later. */
5735 need_remove_insert = 0;
5736 if (! bpstat_causes_stop (bs_head))
5737 for (bs = bs_head; bs != NULL; bs = bs->next)
5738 if (!bs->stop
5739 && bs->breakpoint_at
5740 && is_hardware_watchpoint (bs->breakpoint_at))
5741 {
5742 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5743
5744 update_watchpoint (w, 0 /* don't reparse. */);
5745 need_remove_insert = 1;
5746 }
5747
5748 if (need_remove_insert)
5749 update_global_location_list (UGLL_MAY_INSERT);
5750 else if (removed_any)
5751 update_global_location_list (UGLL_DONT_INSERT);
5752
5753 return bs_head;
5754 }
5755
5756 static void
5757 handle_jit_event (void)
5758 {
5759 struct frame_info *frame;
5760 struct gdbarch *gdbarch;
5761
5762 if (debug_infrun)
5763 fprintf_unfiltered (gdb_stdlog, "handling bp_jit_event\n");
5764
5765 /* Switch terminal for any messages produced by
5766 breakpoint_re_set. */
5767 target_terminal_ours_for_output ();
5768
5769 frame = get_current_frame ();
5770 gdbarch = get_frame_arch (frame);
5771
5772 jit_event_handler (gdbarch);
5773
5774 target_terminal_inferior ();
5775 }
5776
5777 /* Prepare WHAT final decision for infrun. */
5778
5779 /* Decide what infrun needs to do with this bpstat. */
5780
5781 struct bpstat_what
5782 bpstat_what (bpstat bs_head)
5783 {
5784 struct bpstat_what retval;
5785 int jit_event = 0;
5786 bpstat bs;
5787
5788 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
5789 retval.call_dummy = STOP_NONE;
5790 retval.is_longjmp = 0;
5791
5792 for (bs = bs_head; bs != NULL; bs = bs->next)
5793 {
5794 /* Extract this BS's action. After processing each BS, we check
5795 if its action overrides all we've seem so far. */
5796 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5797 enum bptype bptype;
5798
5799 if (bs->breakpoint_at == NULL)
5800 {
5801 /* I suspect this can happen if it was a momentary
5802 breakpoint which has since been deleted. */
5803 bptype = bp_none;
5804 }
5805 else
5806 bptype = bs->breakpoint_at->type;
5807
5808 switch (bptype)
5809 {
5810 case bp_none:
5811 break;
5812 case bp_breakpoint:
5813 case bp_hardware_breakpoint:
5814 case bp_single_step:
5815 case bp_until:
5816 case bp_finish:
5817 case bp_shlib_event:
5818 if (bs->stop)
5819 {
5820 if (bs->print)
5821 this_action = BPSTAT_WHAT_STOP_NOISY;
5822 else
5823 this_action = BPSTAT_WHAT_STOP_SILENT;
5824 }
5825 else
5826 this_action = BPSTAT_WHAT_SINGLE;
5827 break;
5828 case bp_watchpoint:
5829 case bp_hardware_watchpoint:
5830 case bp_read_watchpoint:
5831 case bp_access_watchpoint:
5832 if (bs->stop)
5833 {
5834 if (bs->print)
5835 this_action = BPSTAT_WHAT_STOP_NOISY;
5836 else
5837 this_action = BPSTAT_WHAT_STOP_SILENT;
5838 }
5839 else
5840 {
5841 /* There was a watchpoint, but we're not stopping.
5842 This requires no further action. */
5843 }
5844 break;
5845 case bp_longjmp:
5846 case bp_longjmp_call_dummy:
5847 case bp_exception:
5848 if (bs->stop)
5849 {
5850 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5851 retval.is_longjmp = bptype != bp_exception;
5852 }
5853 else
5854 this_action = BPSTAT_WHAT_SINGLE;
5855 break;
5856 case bp_longjmp_resume:
5857 case bp_exception_resume:
5858 if (bs->stop)
5859 {
5860 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5861 retval.is_longjmp = bptype == bp_longjmp_resume;
5862 }
5863 else
5864 this_action = BPSTAT_WHAT_SINGLE;
5865 break;
5866 case bp_step_resume:
5867 if (bs->stop)
5868 this_action = BPSTAT_WHAT_STEP_RESUME;
5869 else
5870 {
5871 /* It is for the wrong frame. */
5872 this_action = BPSTAT_WHAT_SINGLE;
5873 }
5874 break;
5875 case bp_hp_step_resume:
5876 if (bs->stop)
5877 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5878 else
5879 {
5880 /* It is for the wrong frame. */
5881 this_action = BPSTAT_WHAT_SINGLE;
5882 }
5883 break;
5884 case bp_watchpoint_scope:
5885 case bp_thread_event:
5886 case bp_overlay_event:
5887 case bp_longjmp_master:
5888 case bp_std_terminate_master:
5889 case bp_exception_master:
5890 this_action = BPSTAT_WHAT_SINGLE;
5891 break;
5892 case bp_catchpoint:
5893 if (bs->stop)
5894 {
5895 if (bs->print)
5896 this_action = BPSTAT_WHAT_STOP_NOISY;
5897 else
5898 this_action = BPSTAT_WHAT_STOP_SILENT;
5899 }
5900 else
5901 {
5902 /* There was a catchpoint, but we're not stopping.
5903 This requires no further action. */
5904 }
5905 break;
5906 case bp_jit_event:
5907 jit_event = 1;
5908 this_action = BPSTAT_WHAT_SINGLE;
5909 break;
5910 case bp_call_dummy:
5911 /* Make sure the action is stop (silent or noisy),
5912 so infrun.c pops the dummy frame. */
5913 retval.call_dummy = STOP_STACK_DUMMY;
5914 this_action = BPSTAT_WHAT_STOP_SILENT;
5915 break;
5916 case bp_std_terminate:
5917 /* Make sure the action is stop (silent or noisy),
5918 so infrun.c pops the dummy frame. */
5919 retval.call_dummy = STOP_STD_TERMINATE;
5920 this_action = BPSTAT_WHAT_STOP_SILENT;
5921 break;
5922 case bp_tracepoint:
5923 case bp_fast_tracepoint:
5924 case bp_static_tracepoint:
5925 /* Tracepoint hits should not be reported back to GDB, and
5926 if one got through somehow, it should have been filtered
5927 out already. */
5928 internal_error (__FILE__, __LINE__,
5929 _("bpstat_what: tracepoint encountered"));
5930 break;
5931 case bp_gnu_ifunc_resolver:
5932 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5933 this_action = BPSTAT_WHAT_SINGLE;
5934 break;
5935 case bp_gnu_ifunc_resolver_return:
5936 /* The breakpoint will be removed, execution will restart from the
5937 PC of the former breakpoint. */
5938 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5939 break;
5940
5941 case bp_dprintf:
5942 if (bs->stop)
5943 this_action = BPSTAT_WHAT_STOP_SILENT;
5944 else
5945 this_action = BPSTAT_WHAT_SINGLE;
5946 break;
5947
5948 default:
5949 internal_error (__FILE__, __LINE__,
5950 _("bpstat_what: unhandled bptype %d"), (int) bptype);
5951 }
5952
5953 retval.main_action = max (retval.main_action, this_action);
5954 }
5955
5956 return retval;
5957 }
5958
5959 void
5960 bpstat_run_callbacks (bpstat bs_head)
5961 {
5962 bpstat bs;
5963
5964 for (bs = bs_head; bs != NULL; bs = bs->next)
5965 {
5966 struct breakpoint *b = bs->breakpoint_at;
5967
5968 if (b == NULL)
5969 continue;
5970 switch (b->type)
5971 {
5972 case bp_jit_event:
5973 handle_jit_event ();
5974 break;
5975 case bp_gnu_ifunc_resolver:
5976 gnu_ifunc_resolver_stop (b);
5977 break;
5978 case bp_gnu_ifunc_resolver_return:
5979 gnu_ifunc_resolver_return_stop (b);
5980 break;
5981 }
5982 }
5983 }
5984
5985 /* Nonzero if we should step constantly (e.g. watchpoints on machines
5986 without hardware support). This isn't related to a specific bpstat,
5987 just to things like whether watchpoints are set. */
5988
5989 int
5990 bpstat_should_step (void)
5991 {
5992 struct breakpoint *b;
5993
5994 ALL_BREAKPOINTS (b)
5995 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
5996 return 1;
5997 return 0;
5998 }
5999
6000 int
6001 bpstat_causes_stop (bpstat bs)
6002 {
6003 for (; bs != NULL; bs = bs->next)
6004 if (bs->stop)
6005 return 1;
6006
6007 return 0;
6008 }
6009
6010 \f
6011
6012 /* Compute a string of spaces suitable to indent the next line
6013 so it starts at the position corresponding to the table column
6014 named COL_NAME in the currently active table of UIOUT. */
6015
6016 static char *
6017 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
6018 {
6019 static char wrap_indent[80];
6020 int i, total_width, width, align;
6021 char *text;
6022
6023 total_width = 0;
6024 for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
6025 {
6026 if (strcmp (text, col_name) == 0)
6027 {
6028 gdb_assert (total_width < sizeof wrap_indent);
6029 memset (wrap_indent, ' ', total_width);
6030 wrap_indent[total_width] = 0;
6031
6032 return wrap_indent;
6033 }
6034
6035 total_width += width + 1;
6036 }
6037
6038 return NULL;
6039 }
6040
6041 /* Determine if the locations of this breakpoint will have their conditions
6042 evaluated by the target, host or a mix of both. Returns the following:
6043
6044 "host": Host evals condition.
6045 "host or target": Host or Target evals condition.
6046 "target": Target evals condition.
6047 */
6048
6049 static const char *
6050 bp_condition_evaluator (struct breakpoint *b)
6051 {
6052 struct bp_location *bl;
6053 char host_evals = 0;
6054 char target_evals = 0;
6055
6056 if (!b)
6057 return NULL;
6058
6059 if (!is_breakpoint (b))
6060 return NULL;
6061
6062 if (gdb_evaluates_breakpoint_condition_p ()
6063 || !target_supports_evaluation_of_breakpoint_conditions ())
6064 return condition_evaluation_host;
6065
6066 for (bl = b->loc; bl; bl = bl->next)
6067 {
6068 if (bl->cond_bytecode)
6069 target_evals++;
6070 else
6071 host_evals++;
6072 }
6073
6074 if (host_evals && target_evals)
6075 return condition_evaluation_both;
6076 else if (target_evals)
6077 return condition_evaluation_target;
6078 else
6079 return condition_evaluation_host;
6080 }
6081
6082 /* Determine the breakpoint location's condition evaluator. This is
6083 similar to bp_condition_evaluator, but for locations. */
6084
6085 static const char *
6086 bp_location_condition_evaluator (struct bp_location *bl)
6087 {
6088 if (bl && !is_breakpoint (bl->owner))
6089 return NULL;
6090
6091 if (gdb_evaluates_breakpoint_condition_p ()
6092 || !target_supports_evaluation_of_breakpoint_conditions ())
6093 return condition_evaluation_host;
6094
6095 if (bl && bl->cond_bytecode)
6096 return condition_evaluation_target;
6097 else
6098 return condition_evaluation_host;
6099 }
6100
6101 /* Print the LOC location out of the list of B->LOC locations. */
6102
6103 static void
6104 print_breakpoint_location (struct breakpoint *b,
6105 struct bp_location *loc)
6106 {
6107 struct ui_out *uiout = current_uiout;
6108 struct cleanup *old_chain = save_current_program_space ();
6109
6110 if (loc != NULL && loc->shlib_disabled)
6111 loc = NULL;
6112
6113 if (loc != NULL)
6114 set_current_program_space (loc->pspace);
6115
6116 if (b->display_canonical)
6117 ui_out_field_string (uiout, "what",
6118 event_location_to_string (b->location));
6119 else if (loc && loc->symtab)
6120 {
6121 struct symbol *sym
6122 = find_pc_sect_function (loc->address, loc->section);
6123 if (sym)
6124 {
6125 ui_out_text (uiout, "in ");
6126 ui_out_field_string (uiout, "func",
6127 SYMBOL_PRINT_NAME (sym));
6128 ui_out_text (uiout, " ");
6129 ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
6130 ui_out_text (uiout, "at ");
6131 }
6132 ui_out_field_string (uiout, "file",
6133 symtab_to_filename_for_display (loc->symtab));
6134 ui_out_text (uiout, ":");
6135
6136 if (ui_out_is_mi_like_p (uiout))
6137 ui_out_field_string (uiout, "fullname",
6138 symtab_to_fullname (loc->symtab));
6139
6140 ui_out_field_int (uiout, "line", loc->line_number);
6141 }
6142 else if (loc)
6143 {
6144 struct ui_file *stb = mem_fileopen ();
6145 struct cleanup *stb_chain = make_cleanup_ui_file_delete (stb);
6146
6147 print_address_symbolic (loc->gdbarch, loc->address, stb,
6148 demangle, "");
6149 ui_out_field_stream (uiout, "at", stb);
6150
6151 do_cleanups (stb_chain);
6152 }
6153 else
6154 {
6155 ui_out_field_string (uiout, "pending",
6156 event_location_to_string (b->location));
6157 /* If extra_string is available, it could be holding a condition
6158 or dprintf arguments. In either case, make sure it is printed,
6159 too, but only for non-MI streams. */
6160 if (!ui_out_is_mi_like_p (uiout) && b->extra_string != NULL)
6161 {
6162 if (b->type == bp_dprintf)
6163 ui_out_text (uiout, ",");
6164 else
6165 ui_out_text (uiout, " ");
6166 ui_out_text (uiout, b->extra_string);
6167 }
6168 }
6169
6170 if (loc && is_breakpoint (b)
6171 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6172 && bp_condition_evaluator (b) == condition_evaluation_both)
6173 {
6174 ui_out_text (uiout, " (");
6175 ui_out_field_string (uiout, "evaluated-by",
6176 bp_location_condition_evaluator (loc));
6177 ui_out_text (uiout, ")");
6178 }
6179
6180 do_cleanups (old_chain);
6181 }
6182
6183 static const char *
6184 bptype_string (enum bptype type)
6185 {
6186 struct ep_type_description
6187 {
6188 enum bptype type;
6189 char *description;
6190 };
6191 static struct ep_type_description bptypes[] =
6192 {
6193 {bp_none, "?deleted?"},
6194 {bp_breakpoint, "breakpoint"},
6195 {bp_hardware_breakpoint, "hw breakpoint"},
6196 {bp_single_step, "sw single-step"},
6197 {bp_until, "until"},
6198 {bp_finish, "finish"},
6199 {bp_watchpoint, "watchpoint"},
6200 {bp_hardware_watchpoint, "hw watchpoint"},
6201 {bp_read_watchpoint, "read watchpoint"},
6202 {bp_access_watchpoint, "acc watchpoint"},
6203 {bp_longjmp, "longjmp"},
6204 {bp_longjmp_resume, "longjmp resume"},
6205 {bp_longjmp_call_dummy, "longjmp for call dummy"},
6206 {bp_exception, "exception"},
6207 {bp_exception_resume, "exception resume"},
6208 {bp_step_resume, "step resume"},
6209 {bp_hp_step_resume, "high-priority step resume"},
6210 {bp_watchpoint_scope, "watchpoint scope"},
6211 {bp_call_dummy, "call dummy"},
6212 {bp_std_terminate, "std::terminate"},
6213 {bp_shlib_event, "shlib events"},
6214 {bp_thread_event, "thread events"},
6215 {bp_overlay_event, "overlay events"},
6216 {bp_longjmp_master, "longjmp master"},
6217 {bp_std_terminate_master, "std::terminate master"},
6218 {bp_exception_master, "exception master"},
6219 {bp_catchpoint, "catchpoint"},
6220 {bp_tracepoint, "tracepoint"},
6221 {bp_fast_tracepoint, "fast tracepoint"},
6222 {bp_static_tracepoint, "static tracepoint"},
6223 {bp_dprintf, "dprintf"},
6224 {bp_jit_event, "jit events"},
6225 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6226 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
6227 };
6228
6229 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6230 || ((int) type != bptypes[(int) type].type))
6231 internal_error (__FILE__, __LINE__,
6232 _("bptypes table does not describe type #%d."),
6233 (int) type);
6234
6235 return bptypes[(int) type].description;
6236 }
6237
6238 /* For MI, output a field named 'thread-groups' with a list as the value.
6239 For CLI, prefix the list with the string 'inf'. */
6240
6241 static void
6242 output_thread_groups (struct ui_out *uiout,
6243 const char *field_name,
6244 VEC(int) *inf_num,
6245 int mi_only)
6246 {
6247 struct cleanup *back_to;
6248 int is_mi = ui_out_is_mi_like_p (uiout);
6249 int inf;
6250 int i;
6251
6252 /* For backward compatibility, don't display inferiors in CLI unless
6253 there are several. Always display them for MI. */
6254 if (!is_mi && mi_only)
6255 return;
6256
6257 back_to = make_cleanup_ui_out_list_begin_end (uiout, field_name);
6258
6259 for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
6260 {
6261 if (is_mi)
6262 {
6263 char mi_group[10];
6264
6265 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
6266 ui_out_field_string (uiout, NULL, mi_group);
6267 }
6268 else
6269 {
6270 if (i == 0)
6271 ui_out_text (uiout, " inf ");
6272 else
6273 ui_out_text (uiout, ", ");
6274
6275 ui_out_text (uiout, plongest (inf));
6276 }
6277 }
6278
6279 do_cleanups (back_to);
6280 }
6281
6282 /* Print B to gdb_stdout. */
6283
6284 static void
6285 print_one_breakpoint_location (struct breakpoint *b,
6286 struct bp_location *loc,
6287 int loc_number,
6288 struct bp_location **last_loc,
6289 int allflag)
6290 {
6291 struct command_line *l;
6292 static char bpenables[] = "nynny";
6293
6294 struct ui_out *uiout = current_uiout;
6295 int header_of_multiple = 0;
6296 int part_of_multiple = (loc != NULL);
6297 struct value_print_options opts;
6298
6299 get_user_print_options (&opts);
6300
6301 gdb_assert (!loc || loc_number != 0);
6302 /* See comment in print_one_breakpoint concerning treatment of
6303 breakpoints with single disabled location. */
6304 if (loc == NULL
6305 && (b->loc != NULL
6306 && (b->loc->next != NULL || !b->loc->enabled)))
6307 header_of_multiple = 1;
6308 if (loc == NULL)
6309 loc = b->loc;
6310
6311 annotate_record ();
6312
6313 /* 1 */
6314 annotate_field (0);
6315 if (part_of_multiple)
6316 {
6317 char *formatted;
6318 formatted = xstrprintf ("%d.%d", b->number, loc_number);
6319 ui_out_field_string (uiout, "number", formatted);
6320 xfree (formatted);
6321 }
6322 else
6323 {
6324 ui_out_field_int (uiout, "number", b->number);
6325 }
6326
6327 /* 2 */
6328 annotate_field (1);
6329 if (part_of_multiple)
6330 ui_out_field_skip (uiout, "type");
6331 else
6332 ui_out_field_string (uiout, "type", bptype_string (b->type));
6333
6334 /* 3 */
6335 annotate_field (2);
6336 if (part_of_multiple)
6337 ui_out_field_skip (uiout, "disp");
6338 else
6339 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
6340
6341
6342 /* 4 */
6343 annotate_field (3);
6344 if (part_of_multiple)
6345 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
6346 else
6347 ui_out_field_fmt (uiout, "enabled", "%c",
6348 bpenables[(int) b->enable_state]);
6349 ui_out_spaces (uiout, 2);
6350
6351
6352 /* 5 and 6 */
6353 if (b->ops != NULL && b->ops->print_one != NULL)
6354 {
6355 /* Although the print_one can possibly print all locations,
6356 calling it here is not likely to get any nice result. So,
6357 make sure there's just one location. */
6358 gdb_assert (b->loc == NULL || b->loc->next == NULL);
6359 b->ops->print_one (b, last_loc);
6360 }
6361 else
6362 switch (b->type)
6363 {
6364 case bp_none:
6365 internal_error (__FILE__, __LINE__,
6366 _("print_one_breakpoint: bp_none encountered\n"));
6367 break;
6368
6369 case bp_watchpoint:
6370 case bp_hardware_watchpoint:
6371 case bp_read_watchpoint:
6372 case bp_access_watchpoint:
6373 {
6374 struct watchpoint *w = (struct watchpoint *) b;
6375
6376 /* Field 4, the address, is omitted (which makes the columns
6377 not line up too nicely with the headers, but the effect
6378 is relatively readable). */
6379 if (opts.addressprint)
6380 ui_out_field_skip (uiout, "addr");
6381 annotate_field (5);
6382 ui_out_field_string (uiout, "what", w->exp_string);
6383 }
6384 break;
6385
6386 case bp_breakpoint:
6387 case bp_hardware_breakpoint:
6388 case bp_single_step:
6389 case bp_until:
6390 case bp_finish:
6391 case bp_longjmp:
6392 case bp_longjmp_resume:
6393 case bp_longjmp_call_dummy:
6394 case bp_exception:
6395 case bp_exception_resume:
6396 case bp_step_resume:
6397 case bp_hp_step_resume:
6398 case bp_watchpoint_scope:
6399 case bp_call_dummy:
6400 case bp_std_terminate:
6401 case bp_shlib_event:
6402 case bp_thread_event:
6403 case bp_overlay_event:
6404 case bp_longjmp_master:
6405 case bp_std_terminate_master:
6406 case bp_exception_master:
6407 case bp_tracepoint:
6408 case bp_fast_tracepoint:
6409 case bp_static_tracepoint:
6410 case bp_dprintf:
6411 case bp_jit_event:
6412 case bp_gnu_ifunc_resolver:
6413 case bp_gnu_ifunc_resolver_return:
6414 if (opts.addressprint)
6415 {
6416 annotate_field (4);
6417 if (header_of_multiple)
6418 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
6419 else if (b->loc == NULL || loc->shlib_disabled)
6420 ui_out_field_string (uiout, "addr", "<PENDING>");
6421 else
6422 ui_out_field_core_addr (uiout, "addr",
6423 loc->gdbarch, loc->address);
6424 }
6425 annotate_field (5);
6426 if (!header_of_multiple)
6427 print_breakpoint_location (b, loc);
6428 if (b->loc)
6429 *last_loc = b->loc;
6430 break;
6431 }
6432
6433
6434 if (loc != NULL && !header_of_multiple)
6435 {
6436 struct inferior *inf;
6437 VEC(int) *inf_num = NULL;
6438 int mi_only = 1;
6439
6440 ALL_INFERIORS (inf)
6441 {
6442 if (inf->pspace == loc->pspace)
6443 VEC_safe_push (int, inf_num, inf->num);
6444 }
6445
6446 /* For backward compatibility, don't display inferiors in CLI unless
6447 there are several. Always display for MI. */
6448 if (allflag
6449 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6450 && (number_of_program_spaces () > 1
6451 || number_of_inferiors () > 1)
6452 /* LOC is for existing B, it cannot be in
6453 moribund_locations and thus having NULL OWNER. */
6454 && loc->owner->type != bp_catchpoint))
6455 mi_only = 0;
6456 output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6457 VEC_free (int, inf_num);
6458 }
6459
6460 if (!part_of_multiple)
6461 {
6462 if (b->thread != -1)
6463 {
6464 /* FIXME: This seems to be redundant and lost here; see the
6465 "stop only in" line a little further down. */
6466 ui_out_text (uiout, " thread ");
6467 ui_out_field_int (uiout, "thread", b->thread);
6468 }
6469 else if (b->task != 0)
6470 {
6471 ui_out_text (uiout, " task ");
6472 ui_out_field_int (uiout, "task", b->task);
6473 }
6474 }
6475
6476 ui_out_text (uiout, "\n");
6477
6478 if (!part_of_multiple)
6479 b->ops->print_one_detail (b, uiout);
6480
6481 if (part_of_multiple && frame_id_p (b->frame_id))
6482 {
6483 annotate_field (6);
6484 ui_out_text (uiout, "\tstop only in stack frame at ");
6485 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
6486 the frame ID. */
6487 ui_out_field_core_addr (uiout, "frame",
6488 b->gdbarch, b->frame_id.stack_addr);
6489 ui_out_text (uiout, "\n");
6490 }
6491
6492 if (!part_of_multiple && b->cond_string)
6493 {
6494 annotate_field (7);
6495 if (is_tracepoint (b))
6496 ui_out_text (uiout, "\ttrace only if ");
6497 else
6498 ui_out_text (uiout, "\tstop only if ");
6499 ui_out_field_string (uiout, "cond", b->cond_string);
6500
6501 /* Print whether the target is doing the breakpoint's condition
6502 evaluation. If GDB is doing the evaluation, don't print anything. */
6503 if (is_breakpoint (b)
6504 && breakpoint_condition_evaluation_mode ()
6505 == condition_evaluation_target)
6506 {
6507 ui_out_text (uiout, " (");
6508 ui_out_field_string (uiout, "evaluated-by",
6509 bp_condition_evaluator (b));
6510 ui_out_text (uiout, " evals)");
6511 }
6512 ui_out_text (uiout, "\n");
6513 }
6514
6515 if (!part_of_multiple && b->thread != -1)
6516 {
6517 struct thread_info *thr = find_thread_id (b->thread);
6518
6519 /* FIXME should make an annotation for this. */
6520 ui_out_text (uiout, "\tstop only in thread ");
6521 ui_out_field_string (uiout, "thread", print_thread_id (thr));
6522 ui_out_text (uiout, "\n");
6523 }
6524
6525 if (!part_of_multiple)
6526 {
6527 if (b->hit_count)
6528 {
6529 /* FIXME should make an annotation for this. */
6530 if (is_catchpoint (b))
6531 ui_out_text (uiout, "\tcatchpoint");
6532 else if (is_tracepoint (b))
6533 ui_out_text (uiout, "\ttracepoint");
6534 else
6535 ui_out_text (uiout, "\tbreakpoint");
6536 ui_out_text (uiout, " already hit ");
6537 ui_out_field_int (uiout, "times", b->hit_count);
6538 if (b->hit_count == 1)
6539 ui_out_text (uiout, " time\n");
6540 else
6541 ui_out_text (uiout, " times\n");
6542 }
6543 else
6544 {
6545 /* Output the count also if it is zero, but only if this is mi. */
6546 if (ui_out_is_mi_like_p (uiout))
6547 ui_out_field_int (uiout, "times", b->hit_count);
6548 }
6549 }
6550
6551 if (!part_of_multiple && b->ignore_count)
6552 {
6553 annotate_field (8);
6554 ui_out_text (uiout, "\tignore next ");
6555 ui_out_field_int (uiout, "ignore", b->ignore_count);
6556 ui_out_text (uiout, " hits\n");
6557 }
6558
6559 /* Note that an enable count of 1 corresponds to "enable once"
6560 behavior, which is reported by the combination of enablement and
6561 disposition, so we don't need to mention it here. */
6562 if (!part_of_multiple && b->enable_count > 1)
6563 {
6564 annotate_field (8);
6565 ui_out_text (uiout, "\tdisable after ");
6566 /* Tweak the wording to clarify that ignore and enable counts
6567 are distinct, and have additive effect. */
6568 if (b->ignore_count)
6569 ui_out_text (uiout, "additional ");
6570 else
6571 ui_out_text (uiout, "next ");
6572 ui_out_field_int (uiout, "enable", b->enable_count);
6573 ui_out_text (uiout, " hits\n");
6574 }
6575
6576 if (!part_of_multiple && is_tracepoint (b))
6577 {
6578 struct tracepoint *tp = (struct tracepoint *) b;
6579
6580 if (tp->traceframe_usage)
6581 {
6582 ui_out_text (uiout, "\ttrace buffer usage ");
6583 ui_out_field_int (uiout, "traceframe-usage", tp->traceframe_usage);
6584 ui_out_text (uiout, " bytes\n");
6585 }
6586 }
6587
6588 l = b->commands ? b->commands->commands : NULL;
6589 if (!part_of_multiple && l)
6590 {
6591 struct cleanup *script_chain;
6592
6593 annotate_field (9);
6594 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
6595 print_command_lines (uiout, l, 4);
6596 do_cleanups (script_chain);
6597 }
6598
6599 if (is_tracepoint (b))
6600 {
6601 struct tracepoint *t = (struct tracepoint *) b;
6602
6603 if (!part_of_multiple && t->pass_count)
6604 {
6605 annotate_field (10);
6606 ui_out_text (uiout, "\tpass count ");
6607 ui_out_field_int (uiout, "pass", t->pass_count);
6608 ui_out_text (uiout, " \n");
6609 }
6610
6611 /* Don't display it when tracepoint or tracepoint location is
6612 pending. */
6613 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6614 {
6615 annotate_field (11);
6616
6617 if (ui_out_is_mi_like_p (uiout))
6618 ui_out_field_string (uiout, "installed",
6619 loc->inserted ? "y" : "n");
6620 else
6621 {
6622 if (loc->inserted)
6623 ui_out_text (uiout, "\t");
6624 else
6625 ui_out_text (uiout, "\tnot ");
6626 ui_out_text (uiout, "installed on target\n");
6627 }
6628 }
6629 }
6630
6631 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
6632 {
6633 if (is_watchpoint (b))
6634 {
6635 struct watchpoint *w = (struct watchpoint *) b;
6636
6637 ui_out_field_string (uiout, "original-location", w->exp_string);
6638 }
6639 else if (b->location != NULL
6640 && event_location_to_string (b->location) != NULL)
6641 ui_out_field_string (uiout, "original-location",
6642 event_location_to_string (b->location));
6643 }
6644 }
6645
6646 static void
6647 print_one_breakpoint (struct breakpoint *b,
6648 struct bp_location **last_loc,
6649 int allflag)
6650 {
6651 struct cleanup *bkpt_chain;
6652 struct ui_out *uiout = current_uiout;
6653
6654 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
6655
6656 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
6657 do_cleanups (bkpt_chain);
6658
6659 /* If this breakpoint has custom print function,
6660 it's already printed. Otherwise, print individual
6661 locations, if any. */
6662 if (b->ops == NULL || b->ops->print_one == NULL)
6663 {
6664 /* If breakpoint has a single location that is disabled, we
6665 print it as if it had several locations, since otherwise it's
6666 hard to represent "breakpoint enabled, location disabled"
6667 situation.
6668
6669 Note that while hardware watchpoints have several locations
6670 internally, that's not a property exposed to user. */
6671 if (b->loc
6672 && !is_hardware_watchpoint (b)
6673 && (b->loc->next || !b->loc->enabled))
6674 {
6675 struct bp_location *loc;
6676 int n = 1;
6677
6678 for (loc = b->loc; loc; loc = loc->next, ++n)
6679 {
6680 struct cleanup *inner2 =
6681 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
6682 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
6683 do_cleanups (inner2);
6684 }
6685 }
6686 }
6687 }
6688
6689 static int
6690 breakpoint_address_bits (struct breakpoint *b)
6691 {
6692 int print_address_bits = 0;
6693 struct bp_location *loc;
6694
6695 /* Software watchpoints that aren't watching memory don't have an
6696 address to print. */
6697 if (is_no_memory_software_watchpoint (b))
6698 return 0;
6699
6700 for (loc = b->loc; loc; loc = loc->next)
6701 {
6702 int addr_bit;
6703
6704 addr_bit = gdbarch_addr_bit (loc->gdbarch);
6705 if (addr_bit > print_address_bits)
6706 print_address_bits = addr_bit;
6707 }
6708
6709 return print_address_bits;
6710 }
6711
6712 struct captured_breakpoint_query_args
6713 {
6714 int bnum;
6715 };
6716
6717 static int
6718 do_captured_breakpoint_query (struct ui_out *uiout, void *data)
6719 {
6720 struct captured_breakpoint_query_args *args
6721 = (struct captured_breakpoint_query_args *) data;
6722 struct breakpoint *b;
6723 struct bp_location *dummy_loc = NULL;
6724
6725 ALL_BREAKPOINTS (b)
6726 {
6727 if (args->bnum == b->number)
6728 {
6729 print_one_breakpoint (b, &dummy_loc, 0);
6730 return GDB_RC_OK;
6731 }
6732 }
6733 return GDB_RC_NONE;
6734 }
6735
6736 enum gdb_rc
6737 gdb_breakpoint_query (struct ui_out *uiout, int bnum,
6738 char **error_message)
6739 {
6740 struct captured_breakpoint_query_args args;
6741
6742 args.bnum = bnum;
6743 /* For the moment we don't trust print_one_breakpoint() to not throw
6744 an error. */
6745 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
6746 error_message, RETURN_MASK_ALL) < 0)
6747 return GDB_RC_FAIL;
6748 else
6749 return GDB_RC_OK;
6750 }
6751
6752 /* Return true if this breakpoint was set by the user, false if it is
6753 internal or momentary. */
6754
6755 int
6756 user_breakpoint_p (struct breakpoint *b)
6757 {
6758 return b->number > 0;
6759 }
6760
6761 /* Print information on user settable breakpoint (watchpoint, etc)
6762 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6763 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6764 FILTER is non-NULL, call it on each breakpoint and only include the
6765 ones for which it returns non-zero. Return the total number of
6766 breakpoints listed. */
6767
6768 static int
6769 breakpoint_1 (char *args, int allflag,
6770 int (*filter) (const struct breakpoint *))
6771 {
6772 struct breakpoint *b;
6773 struct bp_location *last_loc = NULL;
6774 int nr_printable_breakpoints;
6775 struct cleanup *bkpttbl_chain;
6776 struct value_print_options opts;
6777 int print_address_bits = 0;
6778 int print_type_col_width = 14;
6779 struct ui_out *uiout = current_uiout;
6780
6781 get_user_print_options (&opts);
6782
6783 /* Compute the number of rows in the table, as well as the size
6784 required for address fields. */
6785 nr_printable_breakpoints = 0;
6786 ALL_BREAKPOINTS (b)
6787 {
6788 /* If we have a filter, only list the breakpoints it accepts. */
6789 if (filter && !filter (b))
6790 continue;
6791
6792 /* If we have an "args" string, it is a list of breakpoints to
6793 accept. Skip the others. */
6794 if (args != NULL && *args != '\0')
6795 {
6796 if (allflag && parse_and_eval_long (args) != b->number)
6797 continue;
6798 if (!allflag && !number_is_in_list (args, b->number))
6799 continue;
6800 }
6801
6802 if (allflag || user_breakpoint_p (b))
6803 {
6804 int addr_bit, type_len;
6805
6806 addr_bit = breakpoint_address_bits (b);
6807 if (addr_bit > print_address_bits)
6808 print_address_bits = addr_bit;
6809
6810 type_len = strlen (bptype_string (b->type));
6811 if (type_len > print_type_col_width)
6812 print_type_col_width = type_len;
6813
6814 nr_printable_breakpoints++;
6815 }
6816 }
6817
6818 if (opts.addressprint)
6819 bkpttbl_chain
6820 = make_cleanup_ui_out_table_begin_end (uiout, 6,
6821 nr_printable_breakpoints,
6822 "BreakpointTable");
6823 else
6824 bkpttbl_chain
6825 = make_cleanup_ui_out_table_begin_end (uiout, 5,
6826 nr_printable_breakpoints,
6827 "BreakpointTable");
6828
6829 if (nr_printable_breakpoints > 0)
6830 annotate_breakpoints_headers ();
6831 if (nr_printable_breakpoints > 0)
6832 annotate_field (0);
6833 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
6834 if (nr_printable_breakpoints > 0)
6835 annotate_field (1);
6836 ui_out_table_header (uiout, print_type_col_width, ui_left,
6837 "type", "Type"); /* 2 */
6838 if (nr_printable_breakpoints > 0)
6839 annotate_field (2);
6840 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
6841 if (nr_printable_breakpoints > 0)
6842 annotate_field (3);
6843 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
6844 if (opts.addressprint)
6845 {
6846 if (nr_printable_breakpoints > 0)
6847 annotate_field (4);
6848 if (print_address_bits <= 32)
6849 ui_out_table_header (uiout, 10, ui_left,
6850 "addr", "Address"); /* 5 */
6851 else
6852 ui_out_table_header (uiout, 18, ui_left,
6853 "addr", "Address"); /* 5 */
6854 }
6855 if (nr_printable_breakpoints > 0)
6856 annotate_field (5);
6857 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
6858 ui_out_table_body (uiout);
6859 if (nr_printable_breakpoints > 0)
6860 annotate_breakpoints_table ();
6861
6862 ALL_BREAKPOINTS (b)
6863 {
6864 QUIT;
6865 /* If we have a filter, only list the breakpoints it accepts. */
6866 if (filter && !filter (b))
6867 continue;
6868
6869 /* If we have an "args" string, it is a list of breakpoints to
6870 accept. Skip the others. */
6871
6872 if (args != NULL && *args != '\0')
6873 {
6874 if (allflag) /* maintenance info breakpoint */
6875 {
6876 if (parse_and_eval_long (args) != b->number)
6877 continue;
6878 }
6879 else /* all others */
6880 {
6881 if (!number_is_in_list (args, b->number))
6882 continue;
6883 }
6884 }
6885 /* We only print out user settable breakpoints unless the
6886 allflag is set. */
6887 if (allflag || user_breakpoint_p (b))
6888 print_one_breakpoint (b, &last_loc, allflag);
6889 }
6890
6891 do_cleanups (bkpttbl_chain);
6892
6893 if (nr_printable_breakpoints == 0)
6894 {
6895 /* If there's a filter, let the caller decide how to report
6896 empty list. */
6897 if (!filter)
6898 {
6899 if (args == NULL || *args == '\0')
6900 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
6901 else
6902 ui_out_message (uiout, 0,
6903 "No breakpoint or watchpoint matching '%s'.\n",
6904 args);
6905 }
6906 }
6907 else
6908 {
6909 if (last_loc && !server_command)
6910 set_next_address (last_loc->gdbarch, last_loc->address);
6911 }
6912
6913 /* FIXME? Should this be moved up so that it is only called when
6914 there have been breakpoints? */
6915 annotate_breakpoints_table_end ();
6916
6917 return nr_printable_breakpoints;
6918 }
6919
6920 /* Display the value of default-collect in a way that is generally
6921 compatible with the breakpoint list. */
6922
6923 static void
6924 default_collect_info (void)
6925 {
6926 struct ui_out *uiout = current_uiout;
6927
6928 /* If it has no value (which is frequently the case), say nothing; a
6929 message like "No default-collect." gets in user's face when it's
6930 not wanted. */
6931 if (!*default_collect)
6932 return;
6933
6934 /* The following phrase lines up nicely with per-tracepoint collect
6935 actions. */
6936 ui_out_text (uiout, "default collect ");
6937 ui_out_field_string (uiout, "default-collect", default_collect);
6938 ui_out_text (uiout, " \n");
6939 }
6940
6941 static void
6942 breakpoints_info (char *args, int from_tty)
6943 {
6944 breakpoint_1 (args, 0, NULL);
6945
6946 default_collect_info ();
6947 }
6948
6949 static void
6950 watchpoints_info (char *args, int from_tty)
6951 {
6952 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
6953 struct ui_out *uiout = current_uiout;
6954
6955 if (num_printed == 0)
6956 {
6957 if (args == NULL || *args == '\0')
6958 ui_out_message (uiout, 0, "No watchpoints.\n");
6959 else
6960 ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
6961 }
6962 }
6963
6964 static void
6965 maintenance_info_breakpoints (char *args, int from_tty)
6966 {
6967 breakpoint_1 (args, 1, NULL);
6968
6969 default_collect_info ();
6970 }
6971
6972 static int
6973 breakpoint_has_pc (struct breakpoint *b,
6974 struct program_space *pspace,
6975 CORE_ADDR pc, struct obj_section *section)
6976 {
6977 struct bp_location *bl = b->loc;
6978
6979 for (; bl; bl = bl->next)
6980 {
6981 if (bl->pspace == pspace
6982 && bl->address == pc
6983 && (!overlay_debugging || bl->section == section))
6984 return 1;
6985 }
6986 return 0;
6987 }
6988
6989 /* Print a message describing any user-breakpoints set at PC. This
6990 concerns with logical breakpoints, so we match program spaces, not
6991 address spaces. */
6992
6993 static void
6994 describe_other_breakpoints (struct gdbarch *gdbarch,
6995 struct program_space *pspace, CORE_ADDR pc,
6996 struct obj_section *section, int thread)
6997 {
6998 int others = 0;
6999 struct breakpoint *b;
7000
7001 ALL_BREAKPOINTS (b)
7002 others += (user_breakpoint_p (b)
7003 && breakpoint_has_pc (b, pspace, pc, section));
7004 if (others > 0)
7005 {
7006 if (others == 1)
7007 printf_filtered (_("Note: breakpoint "));
7008 else /* if (others == ???) */
7009 printf_filtered (_("Note: breakpoints "));
7010 ALL_BREAKPOINTS (b)
7011 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
7012 {
7013 others--;
7014 printf_filtered ("%d", b->number);
7015 if (b->thread == -1 && thread != -1)
7016 printf_filtered (" (all threads)");
7017 else if (b->thread != -1)
7018 printf_filtered (" (thread %d)", b->thread);
7019 printf_filtered ("%s%s ",
7020 ((b->enable_state == bp_disabled
7021 || b->enable_state == bp_call_disabled)
7022 ? " (disabled)"
7023 : ""),
7024 (others > 1) ? ","
7025 : ((others == 1) ? " and" : ""));
7026 }
7027 printf_filtered (_("also set at pc "));
7028 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
7029 printf_filtered (".\n");
7030 }
7031 }
7032 \f
7033
7034 /* Return true iff it is meaningful to use the address member of
7035 BPT locations. For some breakpoint types, the locations' address members
7036 are irrelevant and it makes no sense to attempt to compare them to other
7037 addresses (or use them for any other purpose either).
7038
7039 More specifically, each of the following breakpoint types will
7040 always have a zero valued location address and we don't want to mark
7041 breakpoints of any of these types to be a duplicate of an actual
7042 breakpoint location at address zero:
7043
7044 bp_watchpoint
7045 bp_catchpoint
7046
7047 */
7048
7049 static int
7050 breakpoint_address_is_meaningful (struct breakpoint *bpt)
7051 {
7052 enum bptype type = bpt->type;
7053
7054 return (type != bp_watchpoint && type != bp_catchpoint);
7055 }
7056
7057 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
7058 true if LOC1 and LOC2 represent the same watchpoint location. */
7059
7060 static int
7061 watchpoint_locations_match (struct bp_location *loc1,
7062 struct bp_location *loc2)
7063 {
7064 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
7065 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
7066
7067 /* Both of them must exist. */
7068 gdb_assert (w1 != NULL);
7069 gdb_assert (w2 != NULL);
7070
7071 /* If the target can evaluate the condition expression in hardware,
7072 then we we need to insert both watchpoints even if they are at
7073 the same place. Otherwise the watchpoint will only trigger when
7074 the condition of whichever watchpoint was inserted evaluates to
7075 true, not giving a chance for GDB to check the condition of the
7076 other watchpoint. */
7077 if ((w1->cond_exp
7078 && target_can_accel_watchpoint_condition (loc1->address,
7079 loc1->length,
7080 loc1->watchpoint_type,
7081 w1->cond_exp))
7082 || (w2->cond_exp
7083 && target_can_accel_watchpoint_condition (loc2->address,
7084 loc2->length,
7085 loc2->watchpoint_type,
7086 w2->cond_exp)))
7087 return 0;
7088
7089 /* Note that this checks the owner's type, not the location's. In
7090 case the target does not support read watchpoints, but does
7091 support access watchpoints, we'll have bp_read_watchpoint
7092 watchpoints with hw_access locations. Those should be considered
7093 duplicates of hw_read locations. The hw_read locations will
7094 become hw_access locations later. */
7095 return (loc1->owner->type == loc2->owner->type
7096 && loc1->pspace->aspace == loc2->pspace->aspace
7097 && loc1->address == loc2->address
7098 && loc1->length == loc2->length);
7099 }
7100
7101 /* See breakpoint.h. */
7102
7103 int
7104 breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
7105 struct address_space *aspace2, CORE_ADDR addr2)
7106 {
7107 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
7108 || aspace1 == aspace2)
7109 && addr1 == addr2);
7110 }
7111
7112 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
7113 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
7114 matches ASPACE2. On targets that have global breakpoints, the address
7115 space doesn't really matter. */
7116
7117 static int
7118 breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
7119 int len1, struct address_space *aspace2,
7120 CORE_ADDR addr2)
7121 {
7122 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
7123 || aspace1 == aspace2)
7124 && addr2 >= addr1 && addr2 < addr1 + len1);
7125 }
7126
7127 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
7128 a ranged breakpoint. In most targets, a match happens only if ASPACE
7129 matches the breakpoint's address space. On targets that have global
7130 breakpoints, the address space doesn't really matter. */
7131
7132 static int
7133 breakpoint_location_address_match (struct bp_location *bl,
7134 struct address_space *aspace,
7135 CORE_ADDR addr)
7136 {
7137 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
7138 aspace, addr)
7139 || (bl->length
7140 && breakpoint_address_match_range (bl->pspace->aspace,
7141 bl->address, bl->length,
7142 aspace, addr)));
7143 }
7144
7145 /* Returns true if the [ADDR,ADDR+LEN) range in ASPACE overlaps
7146 breakpoint BL. BL may be a ranged breakpoint. In most targets, a
7147 match happens only if ASPACE matches the breakpoint's address
7148 space. On targets that have global breakpoints, the address space
7149 doesn't really matter. */
7150
7151 static int
7152 breakpoint_location_address_range_overlap (struct bp_location *bl,
7153 struct address_space *aspace,
7154 CORE_ADDR addr, int len)
7155 {
7156 if (gdbarch_has_global_breakpoints (target_gdbarch ())
7157 || bl->pspace->aspace == aspace)
7158 {
7159 int bl_len = bl->length != 0 ? bl->length : 1;
7160
7161 if (mem_ranges_overlap (addr, len, bl->address, bl_len))
7162 return 1;
7163 }
7164 return 0;
7165 }
7166
7167 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
7168 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
7169 true, otherwise returns false. */
7170
7171 static int
7172 tracepoint_locations_match (struct bp_location *loc1,
7173 struct bp_location *loc2)
7174 {
7175 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
7176 /* Since tracepoint locations are never duplicated with others', tracepoint
7177 locations at the same address of different tracepoints are regarded as
7178 different locations. */
7179 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7180 else
7181 return 0;
7182 }
7183
7184 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
7185 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
7186 represent the same location. */
7187
7188 static int
7189 breakpoint_locations_match (struct bp_location *loc1,
7190 struct bp_location *loc2)
7191 {
7192 int hw_point1, hw_point2;
7193
7194 /* Both of them must not be in moribund_locations. */
7195 gdb_assert (loc1->owner != NULL);
7196 gdb_assert (loc2->owner != NULL);
7197
7198 hw_point1 = is_hardware_watchpoint (loc1->owner);
7199 hw_point2 = is_hardware_watchpoint (loc2->owner);
7200
7201 if (hw_point1 != hw_point2)
7202 return 0;
7203 else if (hw_point1)
7204 return watchpoint_locations_match (loc1, loc2);
7205 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7206 return tracepoint_locations_match (loc1, loc2);
7207 else
7208 /* We compare bp_location.length in order to cover ranged breakpoints. */
7209 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7210 loc2->pspace->aspace, loc2->address)
7211 && loc1->length == loc2->length);
7212 }
7213
7214 static void
7215 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7216 int bnum, int have_bnum)
7217 {
7218 /* The longest string possibly returned by hex_string_custom
7219 is 50 chars. These must be at least that big for safety. */
7220 char astr1[64];
7221 char astr2[64];
7222
7223 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7224 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
7225 if (have_bnum)
7226 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
7227 bnum, astr1, astr2);
7228 else
7229 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
7230 }
7231
7232 /* Adjust a breakpoint's address to account for architectural
7233 constraints on breakpoint placement. Return the adjusted address.
7234 Note: Very few targets require this kind of adjustment. For most
7235 targets, this function is simply the identity function. */
7236
7237 static CORE_ADDR
7238 adjust_breakpoint_address (struct gdbarch *gdbarch,
7239 CORE_ADDR bpaddr, enum bptype bptype)
7240 {
7241 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
7242 {
7243 /* Very few targets need any kind of breakpoint adjustment. */
7244 return bpaddr;
7245 }
7246 else if (bptype == bp_watchpoint
7247 || bptype == bp_hardware_watchpoint
7248 || bptype == bp_read_watchpoint
7249 || bptype == bp_access_watchpoint
7250 || bptype == bp_catchpoint)
7251 {
7252 /* Watchpoints and the various bp_catch_* eventpoints should not
7253 have their addresses modified. */
7254 return bpaddr;
7255 }
7256 else if (bptype == bp_single_step)
7257 {
7258 /* Single-step breakpoints should not have their addresses
7259 modified. If there's any architectural constrain that
7260 applies to this address, then it should have already been
7261 taken into account when the breakpoint was created in the
7262 first place. If we didn't do this, stepping through e.g.,
7263 Thumb-2 IT blocks would break. */
7264 return bpaddr;
7265 }
7266 else
7267 {
7268 CORE_ADDR adjusted_bpaddr;
7269
7270 /* Some targets have architectural constraints on the placement
7271 of breakpoint instructions. Obtain the adjusted address. */
7272 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
7273
7274 /* An adjusted breakpoint address can significantly alter
7275 a user's expectations. Print a warning if an adjustment
7276 is required. */
7277 if (adjusted_bpaddr != bpaddr)
7278 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7279
7280 return adjusted_bpaddr;
7281 }
7282 }
7283
7284 void
7285 init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
7286 struct breakpoint *owner)
7287 {
7288 memset (loc, 0, sizeof (*loc));
7289
7290 gdb_assert (ops != NULL);
7291
7292 loc->ops = ops;
7293 loc->owner = owner;
7294 loc->cond = NULL;
7295 loc->cond_bytecode = NULL;
7296 loc->shlib_disabled = 0;
7297 loc->enabled = 1;
7298
7299 switch (owner->type)
7300 {
7301 case bp_breakpoint:
7302 case bp_single_step:
7303 case bp_until:
7304 case bp_finish:
7305 case bp_longjmp:
7306 case bp_longjmp_resume:
7307 case bp_longjmp_call_dummy:
7308 case bp_exception:
7309 case bp_exception_resume:
7310 case bp_step_resume:
7311 case bp_hp_step_resume:
7312 case bp_watchpoint_scope:
7313 case bp_call_dummy:
7314 case bp_std_terminate:
7315 case bp_shlib_event:
7316 case bp_thread_event:
7317 case bp_overlay_event:
7318 case bp_jit_event:
7319 case bp_longjmp_master:
7320 case bp_std_terminate_master:
7321 case bp_exception_master:
7322 case bp_gnu_ifunc_resolver:
7323 case bp_gnu_ifunc_resolver_return:
7324 case bp_dprintf:
7325 loc->loc_type = bp_loc_software_breakpoint;
7326 mark_breakpoint_location_modified (loc);
7327 break;
7328 case bp_hardware_breakpoint:
7329 loc->loc_type = bp_loc_hardware_breakpoint;
7330 mark_breakpoint_location_modified (loc);
7331 break;
7332 case bp_hardware_watchpoint:
7333 case bp_read_watchpoint:
7334 case bp_access_watchpoint:
7335 loc->loc_type = bp_loc_hardware_watchpoint;
7336 break;
7337 case bp_watchpoint:
7338 case bp_catchpoint:
7339 case bp_tracepoint:
7340 case bp_fast_tracepoint:
7341 case bp_static_tracepoint:
7342 loc->loc_type = bp_loc_other;
7343 break;
7344 default:
7345 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
7346 }
7347
7348 loc->refc = 1;
7349 }
7350
7351 /* Allocate a struct bp_location. */
7352
7353 static struct bp_location *
7354 allocate_bp_location (struct breakpoint *bpt)
7355 {
7356 return bpt->ops->allocate_location (bpt);
7357 }
7358
7359 static void
7360 free_bp_location (struct bp_location *loc)
7361 {
7362 loc->ops->dtor (loc);
7363 xfree (loc);
7364 }
7365
7366 /* Increment reference count. */
7367
7368 static void
7369 incref_bp_location (struct bp_location *bl)
7370 {
7371 ++bl->refc;
7372 }
7373
7374 /* Decrement reference count. If the reference count reaches 0,
7375 destroy the bp_location. Sets *BLP to NULL. */
7376
7377 static void
7378 decref_bp_location (struct bp_location **blp)
7379 {
7380 gdb_assert ((*blp)->refc > 0);
7381
7382 if (--(*blp)->refc == 0)
7383 free_bp_location (*blp);
7384 *blp = NULL;
7385 }
7386
7387 /* Add breakpoint B at the end of the global breakpoint chain. */
7388
7389 static void
7390 add_to_breakpoint_chain (struct breakpoint *b)
7391 {
7392 struct breakpoint *b1;
7393
7394 /* Add this breakpoint to the end of the chain so that a list of
7395 breakpoints will come out in order of increasing numbers. */
7396
7397 b1 = breakpoint_chain;
7398 if (b1 == 0)
7399 breakpoint_chain = b;
7400 else
7401 {
7402 while (b1->next)
7403 b1 = b1->next;
7404 b1->next = b;
7405 }
7406 }
7407
7408 /* Initializes breakpoint B with type BPTYPE and no locations yet. */
7409
7410 static void
7411 init_raw_breakpoint_without_location (struct breakpoint *b,
7412 struct gdbarch *gdbarch,
7413 enum bptype bptype,
7414 const struct breakpoint_ops *ops)
7415 {
7416 memset (b, 0, sizeof (*b));
7417
7418 gdb_assert (ops != NULL);
7419
7420 b->ops = ops;
7421 b->type = bptype;
7422 b->gdbarch = gdbarch;
7423 b->language = current_language->la_language;
7424 b->input_radix = input_radix;
7425 b->thread = -1;
7426 b->enable_state = bp_enabled;
7427 b->next = 0;
7428 b->silent = 0;
7429 b->ignore_count = 0;
7430 b->commands = NULL;
7431 b->frame_id = null_frame_id;
7432 b->condition_not_parsed = 0;
7433 b->py_bp_object = NULL;
7434 b->related_breakpoint = b;
7435 b->location = NULL;
7436 }
7437
7438 /* Helper to set_raw_breakpoint below. Creates a breakpoint
7439 that has type BPTYPE and has no locations as yet. */
7440
7441 static struct breakpoint *
7442 set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
7443 enum bptype bptype,
7444 const struct breakpoint_ops *ops)
7445 {
7446 struct breakpoint *b = XNEW (struct breakpoint);
7447
7448 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7449 add_to_breakpoint_chain (b);
7450 return b;
7451 }
7452
7453 /* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7454 resolutions should be made as the user specified the location explicitly
7455 enough. */
7456
7457 static void
7458 set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
7459 {
7460 gdb_assert (loc->owner != NULL);
7461
7462 if (loc->owner->type == bp_breakpoint
7463 || loc->owner->type == bp_hardware_breakpoint
7464 || is_tracepoint (loc->owner))
7465 {
7466 int is_gnu_ifunc;
7467 const char *function_name;
7468 CORE_ADDR func_addr;
7469
7470 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
7471 &func_addr, NULL, &is_gnu_ifunc);
7472
7473 if (is_gnu_ifunc && !explicit_loc)
7474 {
7475 struct breakpoint *b = loc->owner;
7476
7477 gdb_assert (loc->pspace == current_program_space);
7478 if (gnu_ifunc_resolve_name (function_name,
7479 &loc->requested_address))
7480 {
7481 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7482 loc->address = adjust_breakpoint_address (loc->gdbarch,
7483 loc->requested_address,
7484 b->type);
7485 }
7486 else if (b->type == bp_breakpoint && b->loc == loc
7487 && loc->next == NULL && b->related_breakpoint == b)
7488 {
7489 /* Create only the whole new breakpoint of this type but do not
7490 mess more complicated breakpoints with multiple locations. */
7491 b->type = bp_gnu_ifunc_resolver;
7492 /* Remember the resolver's address for use by the return
7493 breakpoint. */
7494 loc->related_address = func_addr;
7495 }
7496 }
7497
7498 if (function_name)
7499 loc->function_name = xstrdup (function_name);
7500 }
7501 }
7502
7503 /* Attempt to determine architecture of location identified by SAL. */
7504 struct gdbarch *
7505 get_sal_arch (struct symtab_and_line sal)
7506 {
7507 if (sal.section)
7508 return get_objfile_arch (sal.section->objfile);
7509 if (sal.symtab)
7510 return get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
7511
7512 return NULL;
7513 }
7514
7515 /* Low level routine for partially initializing a breakpoint of type
7516 BPTYPE. The newly created breakpoint's address, section, source
7517 file name, and line number are provided by SAL.
7518
7519 It is expected that the caller will complete the initialization of
7520 the newly created breakpoint struct as well as output any status
7521 information regarding the creation of a new breakpoint. */
7522
7523 static void
7524 init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
7525 struct symtab_and_line sal, enum bptype bptype,
7526 const struct breakpoint_ops *ops)
7527 {
7528 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7529
7530 add_location_to_breakpoint (b, &sal);
7531
7532 if (bptype != bp_catchpoint)
7533 gdb_assert (sal.pspace != NULL);
7534
7535 /* Store the program space that was used to set the breakpoint,
7536 except for ordinary breakpoints, which are independent of the
7537 program space. */
7538 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7539 b->pspace = sal.pspace;
7540 }
7541
7542 /* set_raw_breakpoint is a low level routine for allocating and
7543 partially initializing a breakpoint of type BPTYPE. The newly
7544 created breakpoint's address, section, source file name, and line
7545 number are provided by SAL. The newly created and partially
7546 initialized breakpoint is added to the breakpoint chain and
7547 is also returned as the value of this function.
7548
7549 It is expected that the caller will complete the initialization of
7550 the newly created breakpoint struct as well as output any status
7551 information regarding the creation of a new breakpoint. In
7552 particular, set_raw_breakpoint does NOT set the breakpoint
7553 number! Care should be taken to not allow an error to occur
7554 prior to completing the initialization of the breakpoint. If this
7555 should happen, a bogus breakpoint will be left on the chain. */
7556
7557 struct breakpoint *
7558 set_raw_breakpoint (struct gdbarch *gdbarch,
7559 struct symtab_and_line sal, enum bptype bptype,
7560 const struct breakpoint_ops *ops)
7561 {
7562 struct breakpoint *b = XNEW (struct breakpoint);
7563
7564 init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
7565 add_to_breakpoint_chain (b);
7566 return b;
7567 }
7568
7569 /* Call this routine when stepping and nexting to enable a breakpoint
7570 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7571 initiated the operation. */
7572
7573 void
7574 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
7575 {
7576 struct breakpoint *b, *b_tmp;
7577 int thread = tp->num;
7578
7579 /* To avoid having to rescan all objfile symbols at every step,
7580 we maintain a list of continually-inserted but always disabled
7581 longjmp "master" breakpoints. Here, we simply create momentary
7582 clones of those and enable them for the requested thread. */
7583 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7584 if (b->pspace == current_program_space
7585 && (b->type == bp_longjmp_master
7586 || b->type == bp_exception_master))
7587 {
7588 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7589 struct breakpoint *clone;
7590
7591 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7592 after their removal. */
7593 clone = momentary_breakpoint_from_master (b, type,
7594 &longjmp_breakpoint_ops, 1);
7595 clone->thread = thread;
7596 }
7597
7598 tp->initiating_frame = frame;
7599 }
7600
7601 /* Delete all longjmp breakpoints from THREAD. */
7602 void
7603 delete_longjmp_breakpoint (int thread)
7604 {
7605 struct breakpoint *b, *b_tmp;
7606
7607 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7608 if (b->type == bp_longjmp || b->type == bp_exception)
7609 {
7610 if (b->thread == thread)
7611 delete_breakpoint (b);
7612 }
7613 }
7614
7615 void
7616 delete_longjmp_breakpoint_at_next_stop (int thread)
7617 {
7618 struct breakpoint *b, *b_tmp;
7619
7620 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7621 if (b->type == bp_longjmp || b->type == bp_exception)
7622 {
7623 if (b->thread == thread)
7624 b->disposition = disp_del_at_next_stop;
7625 }
7626 }
7627
7628 /* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7629 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7630 pointer to any of them. Return NULL if this system cannot place longjmp
7631 breakpoints. */
7632
7633 struct breakpoint *
7634 set_longjmp_breakpoint_for_call_dummy (void)
7635 {
7636 struct breakpoint *b, *retval = NULL;
7637
7638 ALL_BREAKPOINTS (b)
7639 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7640 {
7641 struct breakpoint *new_b;
7642
7643 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7644 &momentary_breakpoint_ops,
7645 1);
7646 new_b->thread = pid_to_thread_id (inferior_ptid);
7647
7648 /* Link NEW_B into the chain of RETVAL breakpoints. */
7649
7650 gdb_assert (new_b->related_breakpoint == new_b);
7651 if (retval == NULL)
7652 retval = new_b;
7653 new_b->related_breakpoint = retval;
7654 while (retval->related_breakpoint != new_b->related_breakpoint)
7655 retval = retval->related_breakpoint;
7656 retval->related_breakpoint = new_b;
7657 }
7658
7659 return retval;
7660 }
7661
7662 /* Verify all existing dummy frames and their associated breakpoints for
7663 TP. Remove those which can no longer be found in the current frame
7664 stack.
7665
7666 You should call this function only at places where it is safe to currently
7667 unwind the whole stack. Failed stack unwind would discard live dummy
7668 frames. */
7669
7670 void
7671 check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
7672 {
7673 struct breakpoint *b, *b_tmp;
7674
7675 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7676 if (b->type == bp_longjmp_call_dummy && b->thread == tp->num)
7677 {
7678 struct breakpoint *dummy_b = b->related_breakpoint;
7679
7680 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7681 dummy_b = dummy_b->related_breakpoint;
7682 if (dummy_b->type != bp_call_dummy
7683 || frame_find_by_id (dummy_b->frame_id) != NULL)
7684 continue;
7685
7686 dummy_frame_discard (dummy_b->frame_id, tp->ptid);
7687
7688 while (b->related_breakpoint != b)
7689 {
7690 if (b_tmp == b->related_breakpoint)
7691 b_tmp = b->related_breakpoint->next;
7692 delete_breakpoint (b->related_breakpoint);
7693 }
7694 delete_breakpoint (b);
7695 }
7696 }
7697
7698 void
7699 enable_overlay_breakpoints (void)
7700 {
7701 struct breakpoint *b;
7702
7703 ALL_BREAKPOINTS (b)
7704 if (b->type == bp_overlay_event)
7705 {
7706 b->enable_state = bp_enabled;
7707 update_global_location_list (UGLL_MAY_INSERT);
7708 overlay_events_enabled = 1;
7709 }
7710 }
7711
7712 void
7713 disable_overlay_breakpoints (void)
7714 {
7715 struct breakpoint *b;
7716
7717 ALL_BREAKPOINTS (b)
7718 if (b->type == bp_overlay_event)
7719 {
7720 b->enable_state = bp_disabled;
7721 update_global_location_list (UGLL_DONT_INSERT);
7722 overlay_events_enabled = 0;
7723 }
7724 }
7725
7726 /* Set an active std::terminate breakpoint for each std::terminate
7727 master breakpoint. */
7728 void
7729 set_std_terminate_breakpoint (void)
7730 {
7731 struct breakpoint *b, *b_tmp;
7732
7733 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7734 if (b->pspace == current_program_space
7735 && b->type == bp_std_terminate_master)
7736 {
7737 momentary_breakpoint_from_master (b, bp_std_terminate,
7738 &momentary_breakpoint_ops, 1);
7739 }
7740 }
7741
7742 /* Delete all the std::terminate breakpoints. */
7743 void
7744 delete_std_terminate_breakpoint (void)
7745 {
7746 struct breakpoint *b, *b_tmp;
7747
7748 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7749 if (b->type == bp_std_terminate)
7750 delete_breakpoint (b);
7751 }
7752
7753 struct breakpoint *
7754 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7755 {
7756 struct breakpoint *b;
7757
7758 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7759 &internal_breakpoint_ops);
7760
7761 b->enable_state = bp_enabled;
7762 /* location has to be used or breakpoint_re_set will delete me. */
7763 b->location = new_address_location (b->loc->address);
7764
7765 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7766
7767 return b;
7768 }
7769
7770 struct lang_and_radix
7771 {
7772 enum language lang;
7773 int radix;
7774 };
7775
7776 /* Create a breakpoint for JIT code registration and unregistration. */
7777
7778 struct breakpoint *
7779 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7780 {
7781 struct breakpoint *b;
7782
7783 b = create_internal_breakpoint (gdbarch, address, bp_jit_event,
7784 &internal_breakpoint_ops);
7785 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7786 return b;
7787 }
7788
7789 /* Remove JIT code registration and unregistration breakpoint(s). */
7790
7791 void
7792 remove_jit_event_breakpoints (void)
7793 {
7794 struct breakpoint *b, *b_tmp;
7795
7796 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7797 if (b->type == bp_jit_event
7798 && b->loc->pspace == current_program_space)
7799 delete_breakpoint (b);
7800 }
7801
7802 void
7803 remove_solib_event_breakpoints (void)
7804 {
7805 struct breakpoint *b, *b_tmp;
7806
7807 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7808 if (b->type == bp_shlib_event
7809 && b->loc->pspace == current_program_space)
7810 delete_breakpoint (b);
7811 }
7812
7813 /* See breakpoint.h. */
7814
7815 void
7816 remove_solib_event_breakpoints_at_next_stop (void)
7817 {
7818 struct breakpoint *b, *b_tmp;
7819
7820 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7821 if (b->type == bp_shlib_event
7822 && b->loc->pspace == current_program_space)
7823 b->disposition = disp_del_at_next_stop;
7824 }
7825
7826 /* Helper for create_solib_event_breakpoint /
7827 create_and_insert_solib_event_breakpoint. Allows specifying which
7828 INSERT_MODE to pass through to update_global_location_list. */
7829
7830 static struct breakpoint *
7831 create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7832 enum ugll_insert_mode insert_mode)
7833 {
7834 struct breakpoint *b;
7835
7836 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7837 &internal_breakpoint_ops);
7838 update_global_location_list_nothrow (insert_mode);
7839 return b;
7840 }
7841
7842 struct breakpoint *
7843 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7844 {
7845 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7846 }
7847
7848 /* See breakpoint.h. */
7849
7850 struct breakpoint *
7851 create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7852 {
7853 struct breakpoint *b;
7854
7855 /* Explicitly tell update_global_location_list to insert
7856 locations. */
7857 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
7858 if (!b->loc->inserted)
7859 {
7860 delete_breakpoint (b);
7861 return NULL;
7862 }
7863 return b;
7864 }
7865
7866 /* Disable any breakpoints that are on code in shared libraries. Only
7867 apply to enabled breakpoints, disabled ones can just stay disabled. */
7868
7869 void
7870 disable_breakpoints_in_shlibs (void)
7871 {
7872 struct bp_location *loc, **locp_tmp;
7873
7874 ALL_BP_LOCATIONS (loc, locp_tmp)
7875 {
7876 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7877 struct breakpoint *b = loc->owner;
7878
7879 /* We apply the check to all breakpoints, including disabled for
7880 those with loc->duplicate set. This is so that when breakpoint
7881 becomes enabled, or the duplicate is removed, gdb will try to
7882 insert all breakpoints. If we don't set shlib_disabled here,
7883 we'll try to insert those breakpoints and fail. */
7884 if (((b->type == bp_breakpoint)
7885 || (b->type == bp_jit_event)
7886 || (b->type == bp_hardware_breakpoint)
7887 || (is_tracepoint (b)))
7888 && loc->pspace == current_program_space
7889 && !loc->shlib_disabled
7890 && solib_name_from_address (loc->pspace, loc->address)
7891 )
7892 {
7893 loc->shlib_disabled = 1;
7894 }
7895 }
7896 }
7897
7898 /* Disable any breakpoints and tracepoints that are in SOLIB upon
7899 notification of unloaded_shlib. Only apply to enabled breakpoints,
7900 disabled ones can just stay disabled. */
7901
7902 static void
7903 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7904 {
7905 struct bp_location *loc, **locp_tmp;
7906 int disabled_shlib_breaks = 0;
7907
7908 /* SunOS a.out shared libraries are always mapped, so do not
7909 disable breakpoints; they will only be reported as unloaded
7910 through clear_solib when GDB discards its shared library
7911 list. See clear_solib for more information. */
7912 if (exec_bfd != NULL
7913 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
7914 return;
7915
7916 ALL_BP_LOCATIONS (loc, locp_tmp)
7917 {
7918 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7919 struct breakpoint *b = loc->owner;
7920
7921 if (solib->pspace == loc->pspace
7922 && !loc->shlib_disabled
7923 && (((b->type == bp_breakpoint
7924 || b->type == bp_jit_event
7925 || b->type == bp_hardware_breakpoint)
7926 && (loc->loc_type == bp_loc_hardware_breakpoint
7927 || loc->loc_type == bp_loc_software_breakpoint))
7928 || is_tracepoint (b))
7929 && solib_contains_address_p (solib, loc->address))
7930 {
7931 loc->shlib_disabled = 1;
7932 /* At this point, we cannot rely on remove_breakpoint
7933 succeeding so we must mark the breakpoint as not inserted
7934 to prevent future errors occurring in remove_breakpoints. */
7935 loc->inserted = 0;
7936
7937 /* This may cause duplicate notifications for the same breakpoint. */
7938 observer_notify_breakpoint_modified (b);
7939
7940 if (!disabled_shlib_breaks)
7941 {
7942 target_terminal_ours_for_output ();
7943 warning (_("Temporarily disabling breakpoints "
7944 "for unloaded shared library \"%s\""),
7945 solib->so_name);
7946 }
7947 disabled_shlib_breaks = 1;
7948 }
7949 }
7950 }
7951
7952 /* Disable any breakpoints and tracepoints in OBJFILE upon
7953 notification of free_objfile. Only apply to enabled breakpoints,
7954 disabled ones can just stay disabled. */
7955
7956 static void
7957 disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7958 {
7959 struct breakpoint *b;
7960
7961 if (objfile == NULL)
7962 return;
7963
7964 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7965 managed by the user with add-symbol-file/remove-symbol-file.
7966 Similarly to how breakpoints in shared libraries are handled in
7967 response to "nosharedlibrary", mark breakpoints in such modules
7968 shlib_disabled so they end up uninserted on the next global
7969 location list update. Shared libraries not loaded by the user
7970 aren't handled here -- they're already handled in
7971 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7972 solib_unloaded observer. We skip objfiles that are not
7973 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7974 main objfile). */
7975 if ((objfile->flags & OBJF_SHARED) == 0
7976 || (objfile->flags & OBJF_USERLOADED) == 0)
7977 return;
7978
7979 ALL_BREAKPOINTS (b)
7980 {
7981 struct bp_location *loc;
7982 int bp_modified = 0;
7983
7984 if (!is_breakpoint (b) && !is_tracepoint (b))
7985 continue;
7986
7987 for (loc = b->loc; loc != NULL; loc = loc->next)
7988 {
7989 CORE_ADDR loc_addr = loc->address;
7990
7991 if (loc->loc_type != bp_loc_hardware_breakpoint
7992 && loc->loc_type != bp_loc_software_breakpoint)
7993 continue;
7994
7995 if (loc->shlib_disabled != 0)
7996 continue;
7997
7998 if (objfile->pspace != loc->pspace)
7999 continue;
8000
8001 if (loc->loc_type != bp_loc_hardware_breakpoint
8002 && loc->loc_type != bp_loc_software_breakpoint)
8003 continue;
8004
8005 if (is_addr_in_objfile (loc_addr, objfile))
8006 {
8007 loc->shlib_disabled = 1;
8008 /* At this point, we don't know whether the object was
8009 unmapped from the inferior or not, so leave the
8010 inserted flag alone. We'll handle failure to
8011 uninsert quietly, in case the object was indeed
8012 unmapped. */
8013
8014 mark_breakpoint_location_modified (loc);
8015
8016 bp_modified = 1;
8017 }
8018 }
8019
8020 if (bp_modified)
8021 observer_notify_breakpoint_modified (b);
8022 }
8023 }
8024
8025 /* FORK & VFORK catchpoints. */
8026
8027 /* An instance of this type is used to represent a fork or vfork
8028 catchpoint. It includes a "struct breakpoint" as a kind of base
8029 class; users downcast to "struct breakpoint *" when needed. A
8030 breakpoint is really of this type iff its ops pointer points to
8031 CATCH_FORK_BREAKPOINT_OPS. */
8032
8033 struct fork_catchpoint
8034 {
8035 /* The base class. */
8036 struct breakpoint base;
8037
8038 /* Process id of a child process whose forking triggered this
8039 catchpoint. This field is only valid immediately after this
8040 catchpoint has triggered. */
8041 ptid_t forked_inferior_pid;
8042 };
8043
8044 /* Implement the "insert" breakpoint_ops method for fork
8045 catchpoints. */
8046
8047 static int
8048 insert_catch_fork (struct bp_location *bl)
8049 {
8050 return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
8051 }
8052
8053 /* Implement the "remove" breakpoint_ops method for fork
8054 catchpoints. */
8055
8056 static int
8057 remove_catch_fork (struct bp_location *bl)
8058 {
8059 return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
8060 }
8061
8062 /* Implement the "breakpoint_hit" breakpoint_ops method for fork
8063 catchpoints. */
8064
8065 static int
8066 breakpoint_hit_catch_fork (const struct bp_location *bl,
8067 struct address_space *aspace, CORE_ADDR bp_addr,
8068 const struct target_waitstatus *ws)
8069 {
8070 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8071
8072 if (ws->kind != TARGET_WAITKIND_FORKED)
8073 return 0;
8074
8075 c->forked_inferior_pid = ws->value.related_pid;
8076 return 1;
8077 }
8078
8079 /* Implement the "print_it" breakpoint_ops method for fork
8080 catchpoints. */
8081
8082 static enum print_stop_action
8083 print_it_catch_fork (bpstat bs)
8084 {
8085 struct ui_out *uiout = current_uiout;
8086 struct breakpoint *b = bs->breakpoint_at;
8087 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
8088
8089 annotate_catchpoint (b->number);
8090 if (b->disposition == disp_del)
8091 ui_out_text (uiout, "\nTemporary catchpoint ");
8092 else
8093 ui_out_text (uiout, "\nCatchpoint ");
8094 if (ui_out_is_mi_like_p (uiout))
8095 {
8096 ui_out_field_string (uiout, "reason",
8097 async_reason_lookup (EXEC_ASYNC_FORK));
8098 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8099 }
8100 ui_out_field_int (uiout, "bkptno", b->number);
8101 ui_out_text (uiout, " (forked process ");
8102 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
8103 ui_out_text (uiout, "), ");
8104 return PRINT_SRC_AND_LOC;
8105 }
8106
8107 /* Implement the "print_one" breakpoint_ops method for fork
8108 catchpoints. */
8109
8110 static void
8111 print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
8112 {
8113 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8114 struct value_print_options opts;
8115 struct ui_out *uiout = current_uiout;
8116
8117 get_user_print_options (&opts);
8118
8119 /* Field 4, the address, is omitted (which makes the columns not
8120 line up too nicely with the headers, but the effect is relatively
8121 readable). */
8122 if (opts.addressprint)
8123 ui_out_field_skip (uiout, "addr");
8124 annotate_field (5);
8125 ui_out_text (uiout, "fork");
8126 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
8127 {
8128 ui_out_text (uiout, ", process ");
8129 ui_out_field_int (uiout, "what",
8130 ptid_get_pid (c->forked_inferior_pid));
8131 ui_out_spaces (uiout, 1);
8132 }
8133
8134 if (ui_out_is_mi_like_p (uiout))
8135 ui_out_field_string (uiout, "catch-type", "fork");
8136 }
8137
8138 /* Implement the "print_mention" breakpoint_ops method for fork
8139 catchpoints. */
8140
8141 static void
8142 print_mention_catch_fork (struct breakpoint *b)
8143 {
8144 printf_filtered (_("Catchpoint %d (fork)"), b->number);
8145 }
8146
8147 /* Implement the "print_recreate" breakpoint_ops method for fork
8148 catchpoints. */
8149
8150 static void
8151 print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
8152 {
8153 fprintf_unfiltered (fp, "catch fork");
8154 print_recreate_thread (b, fp);
8155 }
8156
8157 /* The breakpoint_ops structure to be used in fork catchpoints. */
8158
8159 static struct breakpoint_ops catch_fork_breakpoint_ops;
8160
8161 /* Implement the "insert" breakpoint_ops method for vfork
8162 catchpoints. */
8163
8164 static int
8165 insert_catch_vfork (struct bp_location *bl)
8166 {
8167 return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
8168 }
8169
8170 /* Implement the "remove" breakpoint_ops method for vfork
8171 catchpoints. */
8172
8173 static int
8174 remove_catch_vfork (struct bp_location *bl)
8175 {
8176 return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
8177 }
8178
8179 /* Implement the "breakpoint_hit" breakpoint_ops method for vfork
8180 catchpoints. */
8181
8182 static int
8183 breakpoint_hit_catch_vfork (const struct bp_location *bl,
8184 struct address_space *aspace, CORE_ADDR bp_addr,
8185 const struct target_waitstatus *ws)
8186 {
8187 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8188
8189 if (ws->kind != TARGET_WAITKIND_VFORKED)
8190 return 0;
8191
8192 c->forked_inferior_pid = ws->value.related_pid;
8193 return 1;
8194 }
8195
8196 /* Implement the "print_it" breakpoint_ops method for vfork
8197 catchpoints. */
8198
8199 static enum print_stop_action
8200 print_it_catch_vfork (bpstat bs)
8201 {
8202 struct ui_out *uiout = current_uiout;
8203 struct breakpoint *b = bs->breakpoint_at;
8204 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8205
8206 annotate_catchpoint (b->number);
8207 if (b->disposition == disp_del)
8208 ui_out_text (uiout, "\nTemporary catchpoint ");
8209 else
8210 ui_out_text (uiout, "\nCatchpoint ");
8211 if (ui_out_is_mi_like_p (uiout))
8212 {
8213 ui_out_field_string (uiout, "reason",
8214 async_reason_lookup (EXEC_ASYNC_VFORK));
8215 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8216 }
8217 ui_out_field_int (uiout, "bkptno", b->number);
8218 ui_out_text (uiout, " (vforked process ");
8219 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
8220 ui_out_text (uiout, "), ");
8221 return PRINT_SRC_AND_LOC;
8222 }
8223
8224 /* Implement the "print_one" breakpoint_ops method for vfork
8225 catchpoints. */
8226
8227 static void
8228 print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
8229 {
8230 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8231 struct value_print_options opts;
8232 struct ui_out *uiout = current_uiout;
8233
8234 get_user_print_options (&opts);
8235 /* Field 4, the address, is omitted (which makes the columns not
8236 line up too nicely with the headers, but the effect is relatively
8237 readable). */
8238 if (opts.addressprint)
8239 ui_out_field_skip (uiout, "addr");
8240 annotate_field (5);
8241 ui_out_text (uiout, "vfork");
8242 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
8243 {
8244 ui_out_text (uiout, ", process ");
8245 ui_out_field_int (uiout, "what",
8246 ptid_get_pid (c->forked_inferior_pid));
8247 ui_out_spaces (uiout, 1);
8248 }
8249
8250 if (ui_out_is_mi_like_p (uiout))
8251 ui_out_field_string (uiout, "catch-type", "vfork");
8252 }
8253
8254 /* Implement the "print_mention" breakpoint_ops method for vfork
8255 catchpoints. */
8256
8257 static void
8258 print_mention_catch_vfork (struct breakpoint *b)
8259 {
8260 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
8261 }
8262
8263 /* Implement the "print_recreate" breakpoint_ops method for vfork
8264 catchpoints. */
8265
8266 static void
8267 print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8268 {
8269 fprintf_unfiltered (fp, "catch vfork");
8270 print_recreate_thread (b, fp);
8271 }
8272
8273 /* The breakpoint_ops structure to be used in vfork catchpoints. */
8274
8275 static struct breakpoint_ops catch_vfork_breakpoint_ops;
8276
8277 /* An instance of this type is used to represent an solib catchpoint.
8278 It includes a "struct breakpoint" as a kind of base class; users
8279 downcast to "struct breakpoint *" when needed. A breakpoint is
8280 really of this type iff its ops pointer points to
8281 CATCH_SOLIB_BREAKPOINT_OPS. */
8282
8283 struct solib_catchpoint
8284 {
8285 /* The base class. */
8286 struct breakpoint base;
8287
8288 /* True for "catch load", false for "catch unload". */
8289 unsigned char is_load;
8290
8291 /* Regular expression to match, if any. COMPILED is only valid when
8292 REGEX is non-NULL. */
8293 char *regex;
8294 regex_t compiled;
8295 };
8296
8297 static void
8298 dtor_catch_solib (struct breakpoint *b)
8299 {
8300 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8301
8302 if (self->regex)
8303 regfree (&self->compiled);
8304 xfree (self->regex);
8305
8306 base_breakpoint_ops.dtor (b);
8307 }
8308
8309 static int
8310 insert_catch_solib (struct bp_location *ignore)
8311 {
8312 return 0;
8313 }
8314
8315 static int
8316 remove_catch_solib (struct bp_location *ignore)
8317 {
8318 return 0;
8319 }
8320
8321 static int
8322 breakpoint_hit_catch_solib (const struct bp_location *bl,
8323 struct address_space *aspace,
8324 CORE_ADDR bp_addr,
8325 const struct target_waitstatus *ws)
8326 {
8327 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8328 struct breakpoint *other;
8329
8330 if (ws->kind == TARGET_WAITKIND_LOADED)
8331 return 1;
8332
8333 ALL_BREAKPOINTS (other)
8334 {
8335 struct bp_location *other_bl;
8336
8337 if (other == bl->owner)
8338 continue;
8339
8340 if (other->type != bp_shlib_event)
8341 continue;
8342
8343 if (self->base.pspace != NULL && other->pspace != self->base.pspace)
8344 continue;
8345
8346 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8347 {
8348 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8349 return 1;
8350 }
8351 }
8352
8353 return 0;
8354 }
8355
8356 static void
8357 check_status_catch_solib (struct bpstats *bs)
8358 {
8359 struct solib_catchpoint *self
8360 = (struct solib_catchpoint *) bs->breakpoint_at;
8361 int ix;
8362
8363 if (self->is_load)
8364 {
8365 struct so_list *iter;
8366
8367 for (ix = 0;
8368 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
8369 ix, iter);
8370 ++ix)
8371 {
8372 if (!self->regex
8373 || regexec (&self->compiled, iter->so_name, 0, NULL, 0) == 0)
8374 return;
8375 }
8376 }
8377 else
8378 {
8379 char *iter;
8380
8381 for (ix = 0;
8382 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
8383 ix, iter);
8384 ++ix)
8385 {
8386 if (!self->regex
8387 || regexec (&self->compiled, iter, 0, NULL, 0) == 0)
8388 return;
8389 }
8390 }
8391
8392 bs->stop = 0;
8393 bs->print_it = print_it_noop;
8394 }
8395
8396 static enum print_stop_action
8397 print_it_catch_solib (bpstat bs)
8398 {
8399 struct breakpoint *b = bs->breakpoint_at;
8400 struct ui_out *uiout = current_uiout;
8401
8402 annotate_catchpoint (b->number);
8403 if (b->disposition == disp_del)
8404 ui_out_text (uiout, "\nTemporary catchpoint ");
8405 else
8406 ui_out_text (uiout, "\nCatchpoint ");
8407 ui_out_field_int (uiout, "bkptno", b->number);
8408 ui_out_text (uiout, "\n");
8409 if (ui_out_is_mi_like_p (uiout))
8410 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8411 print_solib_event (1);
8412 return PRINT_SRC_AND_LOC;
8413 }
8414
8415 static void
8416 print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8417 {
8418 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8419 struct value_print_options opts;
8420 struct ui_out *uiout = current_uiout;
8421 char *msg;
8422
8423 get_user_print_options (&opts);
8424 /* Field 4, the address, is omitted (which makes the columns not
8425 line up too nicely with the headers, but the effect is relatively
8426 readable). */
8427 if (opts.addressprint)
8428 {
8429 annotate_field (4);
8430 ui_out_field_skip (uiout, "addr");
8431 }
8432
8433 annotate_field (5);
8434 if (self->is_load)
8435 {
8436 if (self->regex)
8437 msg = xstrprintf (_("load of library matching %s"), self->regex);
8438 else
8439 msg = xstrdup (_("load of library"));
8440 }
8441 else
8442 {
8443 if (self->regex)
8444 msg = xstrprintf (_("unload of library matching %s"), self->regex);
8445 else
8446 msg = xstrdup (_("unload of library"));
8447 }
8448 ui_out_field_string (uiout, "what", msg);
8449 xfree (msg);
8450
8451 if (ui_out_is_mi_like_p (uiout))
8452 ui_out_field_string (uiout, "catch-type",
8453 self->is_load ? "load" : "unload");
8454 }
8455
8456 static void
8457 print_mention_catch_solib (struct breakpoint *b)
8458 {
8459 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8460
8461 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8462 self->is_load ? "load" : "unload");
8463 }
8464
8465 static void
8466 print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8467 {
8468 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8469
8470 fprintf_unfiltered (fp, "%s %s",
8471 b->disposition == disp_del ? "tcatch" : "catch",
8472 self->is_load ? "load" : "unload");
8473 if (self->regex)
8474 fprintf_unfiltered (fp, " %s", self->regex);
8475 fprintf_unfiltered (fp, "\n");
8476 }
8477
8478 static struct breakpoint_ops catch_solib_breakpoint_ops;
8479
8480 /* Shared helper function (MI and CLI) for creating and installing
8481 a shared object event catchpoint. If IS_LOAD is non-zero then
8482 the events to be caught are load events, otherwise they are
8483 unload events. If IS_TEMP is non-zero the catchpoint is a
8484 temporary one. If ENABLED is non-zero the catchpoint is
8485 created in an enabled state. */
8486
8487 void
8488 add_solib_catchpoint (char *arg, int is_load, int is_temp, int enabled)
8489 {
8490 struct solib_catchpoint *c;
8491 struct gdbarch *gdbarch = get_current_arch ();
8492 struct cleanup *cleanup;
8493
8494 if (!arg)
8495 arg = "";
8496 arg = skip_spaces (arg);
8497
8498 c = XCNEW (struct solib_catchpoint);
8499 cleanup = make_cleanup (xfree, c);
8500
8501 if (*arg != '\0')
8502 {
8503 int errcode;
8504
8505 errcode = regcomp (&c->compiled, arg, REG_NOSUB);
8506 if (errcode != 0)
8507 {
8508 char *err = get_regcomp_error (errcode, &c->compiled);
8509
8510 make_cleanup (xfree, err);
8511 error (_("Invalid regexp (%s): %s"), err, arg);
8512 }
8513 c->regex = xstrdup (arg);
8514 }
8515
8516 c->is_load = is_load;
8517 init_catchpoint (&c->base, gdbarch, is_temp, NULL,
8518 &catch_solib_breakpoint_ops);
8519
8520 c->base.enable_state = enabled ? bp_enabled : bp_disabled;
8521
8522 discard_cleanups (cleanup);
8523 install_breakpoint (0, &c->base, 1);
8524 }
8525
8526 /* A helper function that does all the work for "catch load" and
8527 "catch unload". */
8528
8529 static void
8530 catch_load_or_unload (char *arg, int from_tty, int is_load,
8531 struct cmd_list_element *command)
8532 {
8533 int tempflag;
8534 const int enabled = 1;
8535
8536 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8537
8538 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8539 }
8540
8541 static void
8542 catch_load_command_1 (char *arg, int from_tty,
8543 struct cmd_list_element *command)
8544 {
8545 catch_load_or_unload (arg, from_tty, 1, command);
8546 }
8547
8548 static void
8549 catch_unload_command_1 (char *arg, int from_tty,
8550 struct cmd_list_element *command)
8551 {
8552 catch_load_or_unload (arg, from_tty, 0, command);
8553 }
8554
8555 /* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8556 is non-zero, then make the breakpoint temporary. If COND_STRING is
8557 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8558 the breakpoint_ops structure associated to the catchpoint. */
8559
8560 void
8561 init_catchpoint (struct breakpoint *b,
8562 struct gdbarch *gdbarch, int tempflag,
8563 char *cond_string,
8564 const struct breakpoint_ops *ops)
8565 {
8566 struct symtab_and_line sal;
8567
8568 init_sal (&sal);
8569 sal.pspace = current_program_space;
8570
8571 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
8572
8573 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
8574 b->disposition = tempflag ? disp_del : disp_donttouch;
8575 }
8576
8577 void
8578 install_breakpoint (int internal, struct breakpoint *b, int update_gll)
8579 {
8580 add_to_breakpoint_chain (b);
8581 set_breakpoint_number (internal, b);
8582 if (is_tracepoint (b))
8583 set_tracepoint_count (breakpoint_count);
8584 if (!internal)
8585 mention (b);
8586 observer_notify_breakpoint_created (b);
8587
8588 if (update_gll)
8589 update_global_location_list (UGLL_MAY_INSERT);
8590 }
8591
8592 static void
8593 create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8594 int tempflag, char *cond_string,
8595 const struct breakpoint_ops *ops)
8596 {
8597 struct fork_catchpoint *c = XNEW (struct fork_catchpoint);
8598
8599 init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
8600
8601 c->forked_inferior_pid = null_ptid;
8602
8603 install_breakpoint (0, &c->base, 1);
8604 }
8605
8606 /* Exec catchpoints. */
8607
8608 /* An instance of this type is used to represent an exec catchpoint.
8609 It includes a "struct breakpoint" as a kind of base class; users
8610 downcast to "struct breakpoint *" when needed. A breakpoint is
8611 really of this type iff its ops pointer points to
8612 CATCH_EXEC_BREAKPOINT_OPS. */
8613
8614 struct exec_catchpoint
8615 {
8616 /* The base class. */
8617 struct breakpoint base;
8618
8619 /* Filename of a program whose exec triggered this catchpoint.
8620 This field is only valid immediately after this catchpoint has
8621 triggered. */
8622 char *exec_pathname;
8623 };
8624
8625 /* Implement the "dtor" breakpoint_ops method for exec
8626 catchpoints. */
8627
8628 static void
8629 dtor_catch_exec (struct breakpoint *b)
8630 {
8631 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8632
8633 xfree (c->exec_pathname);
8634
8635 base_breakpoint_ops.dtor (b);
8636 }
8637
8638 static int
8639 insert_catch_exec (struct bp_location *bl)
8640 {
8641 return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
8642 }
8643
8644 static int
8645 remove_catch_exec (struct bp_location *bl)
8646 {
8647 return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
8648 }
8649
8650 static int
8651 breakpoint_hit_catch_exec (const struct bp_location *bl,
8652 struct address_space *aspace, CORE_ADDR bp_addr,
8653 const struct target_waitstatus *ws)
8654 {
8655 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8656
8657 if (ws->kind != TARGET_WAITKIND_EXECD)
8658 return 0;
8659
8660 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8661 return 1;
8662 }
8663
8664 static enum print_stop_action
8665 print_it_catch_exec (bpstat bs)
8666 {
8667 struct ui_out *uiout = current_uiout;
8668 struct breakpoint *b = bs->breakpoint_at;
8669 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8670
8671 annotate_catchpoint (b->number);
8672 if (b->disposition == disp_del)
8673 ui_out_text (uiout, "\nTemporary catchpoint ");
8674 else
8675 ui_out_text (uiout, "\nCatchpoint ");
8676 if (ui_out_is_mi_like_p (uiout))
8677 {
8678 ui_out_field_string (uiout, "reason",
8679 async_reason_lookup (EXEC_ASYNC_EXEC));
8680 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8681 }
8682 ui_out_field_int (uiout, "bkptno", b->number);
8683 ui_out_text (uiout, " (exec'd ");
8684 ui_out_field_string (uiout, "new-exec", c->exec_pathname);
8685 ui_out_text (uiout, "), ");
8686
8687 return PRINT_SRC_AND_LOC;
8688 }
8689
8690 static void
8691 print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
8692 {
8693 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8694 struct value_print_options opts;
8695 struct ui_out *uiout = current_uiout;
8696
8697 get_user_print_options (&opts);
8698
8699 /* Field 4, the address, is omitted (which makes the columns
8700 not line up too nicely with the headers, but the effect
8701 is relatively readable). */
8702 if (opts.addressprint)
8703 ui_out_field_skip (uiout, "addr");
8704 annotate_field (5);
8705 ui_out_text (uiout, "exec");
8706 if (c->exec_pathname != NULL)
8707 {
8708 ui_out_text (uiout, ", program \"");
8709 ui_out_field_string (uiout, "what", c->exec_pathname);
8710 ui_out_text (uiout, "\" ");
8711 }
8712
8713 if (ui_out_is_mi_like_p (uiout))
8714 ui_out_field_string (uiout, "catch-type", "exec");
8715 }
8716
8717 static void
8718 print_mention_catch_exec (struct breakpoint *b)
8719 {
8720 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8721 }
8722
8723 /* Implement the "print_recreate" breakpoint_ops method for exec
8724 catchpoints. */
8725
8726 static void
8727 print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8728 {
8729 fprintf_unfiltered (fp, "catch exec");
8730 print_recreate_thread (b, fp);
8731 }
8732
8733 static struct breakpoint_ops catch_exec_breakpoint_ops;
8734
8735 static int
8736 hw_breakpoint_used_count (void)
8737 {
8738 int i = 0;
8739 struct breakpoint *b;
8740 struct bp_location *bl;
8741
8742 ALL_BREAKPOINTS (b)
8743 {
8744 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
8745 for (bl = b->loc; bl; bl = bl->next)
8746 {
8747 /* Special types of hardware breakpoints may use more than
8748 one register. */
8749 i += b->ops->resources_needed (bl);
8750 }
8751 }
8752
8753 return i;
8754 }
8755
8756 /* Returns the resources B would use if it were a hardware
8757 watchpoint. */
8758
8759 static int
8760 hw_watchpoint_use_count (struct breakpoint *b)
8761 {
8762 int i = 0;
8763 struct bp_location *bl;
8764
8765 if (!breakpoint_enabled (b))
8766 return 0;
8767
8768 for (bl = b->loc; bl; bl = bl->next)
8769 {
8770 /* Special types of hardware watchpoints may use more than
8771 one register. */
8772 i += b->ops->resources_needed (bl);
8773 }
8774
8775 return i;
8776 }
8777
8778 /* Returns the sum the used resources of all hardware watchpoints of
8779 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8780 the sum of the used resources of all hardware watchpoints of other
8781 types _not_ TYPE. */
8782
8783 static int
8784 hw_watchpoint_used_count_others (struct breakpoint *except,
8785 enum bptype type, int *other_type_used)
8786 {
8787 int i = 0;
8788 struct breakpoint *b;
8789
8790 *other_type_used = 0;
8791 ALL_BREAKPOINTS (b)
8792 {
8793 if (b == except)
8794 continue;
8795 if (!breakpoint_enabled (b))
8796 continue;
8797
8798 if (b->type == type)
8799 i += hw_watchpoint_use_count (b);
8800 else if (is_hardware_watchpoint (b))
8801 *other_type_used = 1;
8802 }
8803
8804 return i;
8805 }
8806
8807 void
8808 disable_watchpoints_before_interactive_call_start (void)
8809 {
8810 struct breakpoint *b;
8811
8812 ALL_BREAKPOINTS (b)
8813 {
8814 if (is_watchpoint (b) && breakpoint_enabled (b))
8815 {
8816 b->enable_state = bp_call_disabled;
8817 update_global_location_list (UGLL_DONT_INSERT);
8818 }
8819 }
8820 }
8821
8822 void
8823 enable_watchpoints_after_interactive_call_stop (void)
8824 {
8825 struct breakpoint *b;
8826
8827 ALL_BREAKPOINTS (b)
8828 {
8829 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
8830 {
8831 b->enable_state = bp_enabled;
8832 update_global_location_list (UGLL_MAY_INSERT);
8833 }
8834 }
8835 }
8836
8837 void
8838 disable_breakpoints_before_startup (void)
8839 {
8840 current_program_space->executing_startup = 1;
8841 update_global_location_list (UGLL_DONT_INSERT);
8842 }
8843
8844 void
8845 enable_breakpoints_after_startup (void)
8846 {
8847 current_program_space->executing_startup = 0;
8848 breakpoint_re_set ();
8849 }
8850
8851 /* Create a new single-step breakpoint for thread THREAD, with no
8852 locations. */
8853
8854 static struct breakpoint *
8855 new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8856 {
8857 struct breakpoint *b = XNEW (struct breakpoint);
8858
8859 init_raw_breakpoint_without_location (b, gdbarch, bp_single_step,
8860 &momentary_breakpoint_ops);
8861
8862 b->disposition = disp_donttouch;
8863 b->frame_id = null_frame_id;
8864
8865 b->thread = thread;
8866 gdb_assert (b->thread != 0);
8867
8868 add_to_breakpoint_chain (b);
8869
8870 return b;
8871 }
8872
8873 /* Set a momentary breakpoint of type TYPE at address specified by
8874 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8875 frame. */
8876
8877 struct breakpoint *
8878 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8879 struct frame_id frame_id, enum bptype type)
8880 {
8881 struct breakpoint *b;
8882
8883 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8884 tail-called one. */
8885 gdb_assert (!frame_id_artificial_p (frame_id));
8886
8887 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
8888 b->enable_state = bp_enabled;
8889 b->disposition = disp_donttouch;
8890 b->frame_id = frame_id;
8891
8892 /* If we're debugging a multi-threaded program, then we want
8893 momentary breakpoints to be active in only a single thread of
8894 control. */
8895 if (in_thread_list (inferior_ptid))
8896 b->thread = pid_to_thread_id (inferior_ptid);
8897
8898 update_global_location_list_nothrow (UGLL_MAY_INSERT);
8899
8900 return b;
8901 }
8902
8903 /* Make a momentary breakpoint based on the master breakpoint ORIG.
8904 The new breakpoint will have type TYPE, use OPS as its
8905 breakpoint_ops, and will set enabled to LOC_ENABLED. */
8906
8907 static struct breakpoint *
8908 momentary_breakpoint_from_master (struct breakpoint *orig,
8909 enum bptype type,
8910 const struct breakpoint_ops *ops,
8911 int loc_enabled)
8912 {
8913 struct breakpoint *copy;
8914
8915 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
8916 copy->loc = allocate_bp_location (copy);
8917 set_breakpoint_location_function (copy->loc, 1);
8918
8919 copy->loc->gdbarch = orig->loc->gdbarch;
8920 copy->loc->requested_address = orig->loc->requested_address;
8921 copy->loc->address = orig->loc->address;
8922 copy->loc->section = orig->loc->section;
8923 copy->loc->pspace = orig->loc->pspace;
8924 copy->loc->probe = orig->loc->probe;
8925 copy->loc->line_number = orig->loc->line_number;
8926 copy->loc->symtab = orig->loc->symtab;
8927 copy->loc->enabled = loc_enabled;
8928 copy->frame_id = orig->frame_id;
8929 copy->thread = orig->thread;
8930 copy->pspace = orig->pspace;
8931
8932 copy->enable_state = bp_enabled;
8933 copy->disposition = disp_donttouch;
8934 copy->number = internal_breakpoint_number--;
8935
8936 update_global_location_list_nothrow (UGLL_DONT_INSERT);
8937 return copy;
8938 }
8939
8940 /* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8941 ORIG is NULL. */
8942
8943 struct breakpoint *
8944 clone_momentary_breakpoint (struct breakpoint *orig)
8945 {
8946 /* If there's nothing to clone, then return nothing. */
8947 if (orig == NULL)
8948 return NULL;
8949
8950 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
8951 }
8952
8953 struct breakpoint *
8954 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8955 enum bptype type)
8956 {
8957 struct symtab_and_line sal;
8958
8959 sal = find_pc_line (pc, 0);
8960 sal.pc = pc;
8961 sal.section = find_pc_overlay (pc);
8962 sal.explicit_pc = 1;
8963
8964 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
8965 }
8966 \f
8967
8968 /* Tell the user we have just set a breakpoint B. */
8969
8970 static void
8971 mention (struct breakpoint *b)
8972 {
8973 b->ops->print_mention (b);
8974 if (ui_out_is_mi_like_p (current_uiout))
8975 return;
8976 printf_filtered ("\n");
8977 }
8978 \f
8979
8980 static int bp_loc_is_permanent (struct bp_location *loc);
8981
8982 static struct bp_location *
8983 add_location_to_breakpoint (struct breakpoint *b,
8984 const struct symtab_and_line *sal)
8985 {
8986 struct bp_location *loc, **tmp;
8987 CORE_ADDR adjusted_address;
8988 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8989
8990 if (loc_gdbarch == NULL)
8991 loc_gdbarch = b->gdbarch;
8992
8993 /* Adjust the breakpoint's address prior to allocating a location.
8994 Once we call allocate_bp_location(), that mostly uninitialized
8995 location will be placed on the location chain. Adjustment of the
8996 breakpoint may cause target_read_memory() to be called and we do
8997 not want its scan of the location chain to find a breakpoint and
8998 location that's only been partially initialized. */
8999 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
9000 sal->pc, b->type);
9001
9002 /* Sort the locations by their ADDRESS. */
9003 loc = allocate_bp_location (b);
9004 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
9005 tmp = &((*tmp)->next))
9006 ;
9007 loc->next = *tmp;
9008 *tmp = loc;
9009
9010 loc->requested_address = sal->pc;
9011 loc->address = adjusted_address;
9012 loc->pspace = sal->pspace;
9013 loc->probe.probe = sal->probe;
9014 loc->probe.objfile = sal->objfile;
9015 gdb_assert (loc->pspace != NULL);
9016 loc->section = sal->section;
9017 loc->gdbarch = loc_gdbarch;
9018 loc->line_number = sal->line;
9019 loc->symtab = sal->symtab;
9020
9021 set_breakpoint_location_function (loc,
9022 sal->explicit_pc || sal->explicit_line);
9023
9024 /* While by definition, permanent breakpoints are already present in the
9025 code, we don't mark the location as inserted. Normally one would expect
9026 that GDB could rely on that breakpoint instruction to stop the program,
9027 thus removing the need to insert its own breakpoint, except that executing
9028 the breakpoint instruction can kill the target instead of reporting a
9029 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
9030 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
9031 with "Trap 0x02 while interrupts disabled, Error state". Letting the
9032 breakpoint be inserted normally results in QEMU knowing about the GDB
9033 breakpoint, and thus trap before the breakpoint instruction is executed.
9034 (If GDB later needs to continue execution past the permanent breakpoint,
9035 it manually increments the PC, thus avoiding executing the breakpoint
9036 instruction.) */
9037 if (bp_loc_is_permanent (loc))
9038 loc->permanent = 1;
9039
9040 return loc;
9041 }
9042 \f
9043
9044 /* See breakpoint.h. */
9045
9046 int
9047 program_breakpoint_here_p (struct gdbarch *gdbarch, CORE_ADDR address)
9048 {
9049 int len;
9050 CORE_ADDR addr;
9051 const gdb_byte *bpoint;
9052 gdb_byte *target_mem;
9053 struct cleanup *cleanup;
9054 int retval = 0;
9055
9056 addr = address;
9057 bpoint = gdbarch_breakpoint_from_pc (gdbarch, &addr, &len);
9058
9059 /* Software breakpoints unsupported? */
9060 if (bpoint == NULL)
9061 return 0;
9062
9063 target_mem = (gdb_byte *) alloca (len);
9064
9065 /* Enable the automatic memory restoration from breakpoints while
9066 we read the memory. Otherwise we could say about our temporary
9067 breakpoints they are permanent. */
9068 cleanup = make_show_memory_breakpoints_cleanup (0);
9069
9070 if (target_read_memory (address, target_mem, len) == 0
9071 && memcmp (target_mem, bpoint, len) == 0)
9072 retval = 1;
9073
9074 do_cleanups (cleanup);
9075
9076 return retval;
9077 }
9078
9079 /* Return 1 if LOC is pointing to a permanent breakpoint,
9080 return 0 otherwise. */
9081
9082 static int
9083 bp_loc_is_permanent (struct bp_location *loc)
9084 {
9085 struct cleanup *cleanup;
9086 int retval;
9087
9088 gdb_assert (loc != NULL);
9089
9090 /* If we have a catchpoint or a watchpoint, just return 0. We should not
9091 attempt to read from the addresses the locations of these breakpoint types
9092 point to. program_breakpoint_here_p, below, will attempt to read
9093 memory. */
9094 if (!breakpoint_address_is_meaningful (loc->owner))
9095 return 0;
9096
9097 cleanup = save_current_space_and_thread ();
9098 switch_to_program_space_and_thread (loc->pspace);
9099
9100 retval = program_breakpoint_here_p (loc->gdbarch, loc->address);
9101
9102 do_cleanups (cleanup);
9103
9104 return retval;
9105 }
9106
9107 /* Build a command list for the dprintf corresponding to the current
9108 settings of the dprintf style options. */
9109
9110 static void
9111 update_dprintf_command_list (struct breakpoint *b)
9112 {
9113 char *dprintf_args = b->extra_string;
9114 char *printf_line = NULL;
9115
9116 if (!dprintf_args)
9117 return;
9118
9119 dprintf_args = skip_spaces (dprintf_args);
9120
9121 /* Allow a comma, as it may have terminated a location, but don't
9122 insist on it. */
9123 if (*dprintf_args == ',')
9124 ++dprintf_args;
9125 dprintf_args = skip_spaces (dprintf_args);
9126
9127 if (*dprintf_args != '"')
9128 error (_("Bad format string, missing '\"'."));
9129
9130 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
9131 printf_line = xstrprintf ("printf %s", dprintf_args);
9132 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
9133 {
9134 if (!dprintf_function)
9135 error (_("No function supplied for dprintf call"));
9136
9137 if (dprintf_channel && strlen (dprintf_channel) > 0)
9138 printf_line = xstrprintf ("call (void) %s (%s,%s)",
9139 dprintf_function,
9140 dprintf_channel,
9141 dprintf_args);
9142 else
9143 printf_line = xstrprintf ("call (void) %s (%s)",
9144 dprintf_function,
9145 dprintf_args);
9146 }
9147 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
9148 {
9149 if (target_can_run_breakpoint_commands ())
9150 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
9151 else
9152 {
9153 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
9154 printf_line = xstrprintf ("printf %s", dprintf_args);
9155 }
9156 }
9157 else
9158 internal_error (__FILE__, __LINE__,
9159 _("Invalid dprintf style."));
9160
9161 gdb_assert (printf_line != NULL);
9162 /* Manufacture a printf sequence. */
9163 {
9164 struct command_line *printf_cmd_line = XNEW (struct command_line);
9165
9166 printf_cmd_line->control_type = simple_control;
9167 printf_cmd_line->body_count = 0;
9168 printf_cmd_line->body_list = NULL;
9169 printf_cmd_line->next = NULL;
9170 printf_cmd_line->line = printf_line;
9171
9172 breakpoint_set_commands (b, printf_cmd_line);
9173 }
9174 }
9175
9176 /* Update all dprintf commands, making their command lists reflect
9177 current style settings. */
9178
9179 static void
9180 update_dprintf_commands (char *args, int from_tty,
9181 struct cmd_list_element *c)
9182 {
9183 struct breakpoint *b;
9184
9185 ALL_BREAKPOINTS (b)
9186 {
9187 if (b->type == bp_dprintf)
9188 update_dprintf_command_list (b);
9189 }
9190 }
9191
9192 /* Create a breakpoint with SAL as location. Use LOCATION
9193 as a description of the location, and COND_STRING
9194 as condition expression. */
9195
9196 static void
9197 init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
9198 struct symtabs_and_lines sals,
9199 struct event_location *location,
9200 char *filter, char *cond_string,
9201 char *extra_string,
9202 enum bptype type, enum bpdisp disposition,
9203 int thread, int task, int ignore_count,
9204 const struct breakpoint_ops *ops, int from_tty,
9205 int enabled, int internal, unsigned flags,
9206 int display_canonical)
9207 {
9208 int i;
9209
9210 if (type == bp_hardware_breakpoint)
9211 {
9212 int target_resources_ok;
9213
9214 i = hw_breakpoint_used_count ();
9215 target_resources_ok =
9216 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9217 i + 1, 0);
9218 if (target_resources_ok == 0)
9219 error (_("No hardware breakpoint support in the target."));
9220 else if (target_resources_ok < 0)
9221 error (_("Hardware breakpoints used exceeds limit."));
9222 }
9223
9224 gdb_assert (sals.nelts > 0);
9225
9226 for (i = 0; i < sals.nelts; ++i)
9227 {
9228 struct symtab_and_line sal = sals.sals[i];
9229 struct bp_location *loc;
9230
9231 if (from_tty)
9232 {
9233 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9234 if (!loc_gdbarch)
9235 loc_gdbarch = gdbarch;
9236
9237 describe_other_breakpoints (loc_gdbarch,
9238 sal.pspace, sal.pc, sal.section, thread);
9239 }
9240
9241 if (i == 0)
9242 {
9243 init_raw_breakpoint (b, gdbarch, sal, type, ops);
9244 b->thread = thread;
9245 b->task = task;
9246
9247 b->cond_string = cond_string;
9248 b->extra_string = extra_string;
9249 b->ignore_count = ignore_count;
9250 b->enable_state = enabled ? bp_enabled : bp_disabled;
9251 b->disposition = disposition;
9252
9253 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9254 b->loc->inserted = 1;
9255
9256 if (type == bp_static_tracepoint)
9257 {
9258 struct tracepoint *t = (struct tracepoint *) b;
9259 struct static_tracepoint_marker marker;
9260
9261 if (strace_marker_p (b))
9262 {
9263 /* We already know the marker exists, otherwise, we
9264 wouldn't see a sal for it. */
9265 const char *p = &event_location_to_string (b->location)[3];
9266 const char *endp;
9267 char *marker_str;
9268
9269 p = skip_spaces_const (p);
9270
9271 endp = skip_to_space_const (p);
9272
9273 marker_str = savestring (p, endp - p);
9274 t->static_trace_marker_id = marker_str;
9275
9276 printf_filtered (_("Probed static tracepoint "
9277 "marker \"%s\"\n"),
9278 t->static_trace_marker_id);
9279 }
9280 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
9281 {
9282 t->static_trace_marker_id = xstrdup (marker.str_id);
9283 release_static_tracepoint_marker (&marker);
9284
9285 printf_filtered (_("Probed static tracepoint "
9286 "marker \"%s\"\n"),
9287 t->static_trace_marker_id);
9288 }
9289 else
9290 warning (_("Couldn't determine the static "
9291 "tracepoint marker to probe"));
9292 }
9293
9294 loc = b->loc;
9295 }
9296 else
9297 {
9298 loc = add_location_to_breakpoint (b, &sal);
9299 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9300 loc->inserted = 1;
9301 }
9302
9303 if (b->cond_string)
9304 {
9305 const char *arg = b->cond_string;
9306
9307 loc->cond = parse_exp_1 (&arg, loc->address,
9308 block_for_pc (loc->address), 0);
9309 if (*arg)
9310 error (_("Garbage '%s' follows condition"), arg);
9311 }
9312
9313 /* Dynamic printf requires and uses additional arguments on the
9314 command line, otherwise it's an error. */
9315 if (type == bp_dprintf)
9316 {
9317 if (b->extra_string)
9318 update_dprintf_command_list (b);
9319 else
9320 error (_("Format string required"));
9321 }
9322 else if (b->extra_string)
9323 error (_("Garbage '%s' at end of command"), b->extra_string);
9324 }
9325
9326 b->display_canonical = display_canonical;
9327 if (location != NULL)
9328 b->location = location;
9329 else
9330 b->location = new_address_location (b->loc->address);
9331 b->filter = filter;
9332 }
9333
9334 static void
9335 create_breakpoint_sal (struct gdbarch *gdbarch,
9336 struct symtabs_and_lines sals,
9337 struct event_location *location,
9338 char *filter, char *cond_string,
9339 char *extra_string,
9340 enum bptype type, enum bpdisp disposition,
9341 int thread, int task, int ignore_count,
9342 const struct breakpoint_ops *ops, int from_tty,
9343 int enabled, int internal, unsigned flags,
9344 int display_canonical)
9345 {
9346 struct breakpoint *b;
9347 struct cleanup *old_chain;
9348
9349 if (is_tracepoint_type (type))
9350 {
9351 struct tracepoint *t;
9352
9353 t = XCNEW (struct tracepoint);
9354 b = &t->base;
9355 }
9356 else
9357 b = XNEW (struct breakpoint);
9358
9359 old_chain = make_cleanup (xfree, b);
9360
9361 init_breakpoint_sal (b, gdbarch,
9362 sals, location,
9363 filter, cond_string, extra_string,
9364 type, disposition,
9365 thread, task, ignore_count,
9366 ops, from_tty,
9367 enabled, internal, flags,
9368 display_canonical);
9369 discard_cleanups (old_chain);
9370
9371 install_breakpoint (internal, b, 0);
9372 }
9373
9374 /* Add SALS.nelts breakpoints to the breakpoint table. For each
9375 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9376 value. COND_STRING, if not NULL, specified the condition to be
9377 used for all breakpoints. Essentially the only case where
9378 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9379 function. In that case, it's still not possible to specify
9380 separate conditions for different overloaded functions, so
9381 we take just a single condition string.
9382
9383 NOTE: If the function succeeds, the caller is expected to cleanup
9384 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
9385 array contents). If the function fails (error() is called), the
9386 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
9387 COND and SALS arrays and each of those arrays contents. */
9388
9389 static void
9390 create_breakpoints_sal (struct gdbarch *gdbarch,
9391 struct linespec_result *canonical,
9392 char *cond_string, char *extra_string,
9393 enum bptype type, enum bpdisp disposition,
9394 int thread, int task, int ignore_count,
9395 const struct breakpoint_ops *ops, int from_tty,
9396 int enabled, int internal, unsigned flags)
9397 {
9398 int i;
9399 struct linespec_sals *lsal;
9400
9401 if (canonical->pre_expanded)
9402 gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
9403
9404 for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
9405 {
9406 /* Note that 'location' can be NULL in the case of a plain
9407 'break', without arguments. */
9408 struct event_location *location
9409 = (canonical->location != NULL
9410 ? copy_event_location (canonical->location) : NULL);
9411 char *filter_string = lsal->canonical ? xstrdup (lsal->canonical) : NULL;
9412 struct cleanup *inner = make_cleanup_delete_event_location (location);
9413
9414 make_cleanup (xfree, filter_string);
9415 create_breakpoint_sal (gdbarch, lsal->sals,
9416 location,
9417 filter_string,
9418 cond_string, extra_string,
9419 type, disposition,
9420 thread, task, ignore_count, ops,
9421 from_tty, enabled, internal, flags,
9422 canonical->special_display);
9423 discard_cleanups (inner);
9424 }
9425 }
9426
9427 /* Parse LOCATION which is assumed to be a SAL specification possibly
9428 followed by conditionals. On return, SALS contains an array of SAL
9429 addresses found. LOCATION points to the end of the SAL (for
9430 linespec locations).
9431
9432 The array and the line spec strings are allocated on the heap, it is
9433 the caller's responsibility to free them. */
9434
9435 static void
9436 parse_breakpoint_sals (const struct event_location *location,
9437 struct linespec_result *canonical)
9438 {
9439 struct symtab_and_line cursal;
9440
9441 if (event_location_type (location) == LINESPEC_LOCATION)
9442 {
9443 const char *address = get_linespec_location (location);
9444
9445 if (address == NULL)
9446 {
9447 /* The last displayed codepoint, if it's valid, is our default
9448 breakpoint address. */
9449 if (last_displayed_sal_is_valid ())
9450 {
9451 struct linespec_sals lsal;
9452 struct symtab_and_line sal;
9453 CORE_ADDR pc;
9454
9455 init_sal (&sal); /* Initialize to zeroes. */
9456 lsal.sals.sals = XNEW (struct symtab_and_line);
9457
9458 /* Set sal's pspace, pc, symtab, and line to the values
9459 corresponding to the last call to print_frame_info.
9460 Be sure to reinitialize LINE with NOTCURRENT == 0
9461 as the breakpoint line number is inappropriate otherwise.
9462 find_pc_line would adjust PC, re-set it back. */
9463 get_last_displayed_sal (&sal);
9464 pc = sal.pc;
9465 sal = find_pc_line (pc, 0);
9466
9467 /* "break" without arguments is equivalent to "break *PC"
9468 where PC is the last displayed codepoint's address. So
9469 make sure to set sal.explicit_pc to prevent GDB from
9470 trying to expand the list of sals to include all other
9471 instances with the same symtab and line. */
9472 sal.pc = pc;
9473 sal.explicit_pc = 1;
9474
9475 lsal.sals.sals[0] = sal;
9476 lsal.sals.nelts = 1;
9477 lsal.canonical = NULL;
9478
9479 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
9480 return;
9481 }
9482 else
9483 error (_("No default breakpoint address now."));
9484 }
9485 }
9486
9487 /* Force almost all breakpoints to be in terms of the
9488 current_source_symtab (which is decode_line_1's default).
9489 This should produce the results we want almost all of the
9490 time while leaving default_breakpoint_* alone.
9491
9492 ObjC: However, don't match an Objective-C method name which
9493 may have a '+' or '-' succeeded by a '['. */
9494 cursal = get_current_source_symtab_and_line ();
9495 if (last_displayed_sal_is_valid ())
9496 {
9497 const char *address = NULL;
9498
9499 if (event_location_type (location) == LINESPEC_LOCATION)
9500 address = get_linespec_location (location);
9501
9502 if (!cursal.symtab
9503 || (address != NULL
9504 && strchr ("+-", address[0]) != NULL
9505 && address[1] != '['))
9506 {
9507 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE,
9508 get_last_displayed_symtab (),
9509 get_last_displayed_line (),
9510 canonical, NULL, NULL);
9511 return;
9512 }
9513 }
9514
9515 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE,
9516 cursal.symtab, cursal.line, canonical, NULL, NULL);
9517 }
9518
9519
9520 /* Convert each SAL into a real PC. Verify that the PC can be
9521 inserted as a breakpoint. If it can't throw an error. */
9522
9523 static void
9524 breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
9525 {
9526 int i;
9527
9528 for (i = 0; i < sals->nelts; i++)
9529 resolve_sal_pc (&sals->sals[i]);
9530 }
9531
9532 /* Fast tracepoints may have restrictions on valid locations. For
9533 instance, a fast tracepoint using a jump instead of a trap will
9534 likely have to overwrite more bytes than a trap would, and so can
9535 only be placed where the instruction is longer than the jump, or a
9536 multi-instruction sequence does not have a jump into the middle of
9537 it, etc. */
9538
9539 static void
9540 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9541 struct symtabs_and_lines *sals)
9542 {
9543 int i, rslt;
9544 struct symtab_and_line *sal;
9545 char *msg;
9546 struct cleanup *old_chain;
9547
9548 for (i = 0; i < sals->nelts; i++)
9549 {
9550 struct gdbarch *sarch;
9551
9552 sal = &sals->sals[i];
9553
9554 sarch = get_sal_arch (*sal);
9555 /* We fall back to GDBARCH if there is no architecture
9556 associated with SAL. */
9557 if (sarch == NULL)
9558 sarch = gdbarch;
9559 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc, &msg);
9560 old_chain = make_cleanup (xfree, msg);
9561
9562 if (!rslt)
9563 error (_("May not have a fast tracepoint at 0x%s%s"),
9564 paddress (sarch, sal->pc), (msg ? msg : ""));
9565
9566 do_cleanups (old_chain);
9567 }
9568 }
9569
9570 /* Issue an invalid thread ID error. */
9571
9572 static void ATTRIBUTE_NORETURN
9573 invalid_thread_id_error (int id)
9574 {
9575 error (_("Unknown thread %d."), id);
9576 }
9577
9578 /* Given TOK, a string specification of condition and thread, as
9579 accepted by the 'break' command, extract the condition
9580 string and thread number and set *COND_STRING and *THREAD.
9581 PC identifies the context at which the condition should be parsed.
9582 If no condition is found, *COND_STRING is set to NULL.
9583 If no thread is found, *THREAD is set to -1. */
9584
9585 static void
9586 find_condition_and_thread (const char *tok, CORE_ADDR pc,
9587 char **cond_string, int *thread, int *task,
9588 char **rest)
9589 {
9590 *cond_string = NULL;
9591 *thread = -1;
9592 *task = 0;
9593 *rest = NULL;
9594
9595 while (tok && *tok)
9596 {
9597 const char *end_tok;
9598 int toklen;
9599 const char *cond_start = NULL;
9600 const char *cond_end = NULL;
9601
9602 tok = skip_spaces_const (tok);
9603
9604 if ((*tok == '"' || *tok == ',') && rest)
9605 {
9606 *rest = savestring (tok, strlen (tok));
9607 return;
9608 }
9609
9610 end_tok = skip_to_space_const (tok);
9611
9612 toklen = end_tok - tok;
9613
9614 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9615 {
9616 struct expression *expr;
9617
9618 tok = cond_start = end_tok + 1;
9619 expr = parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
9620 xfree (expr);
9621 cond_end = tok;
9622 *cond_string = savestring (cond_start, cond_end - cond_start);
9623 }
9624 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9625 {
9626 char *tmptok;
9627
9628 tok = end_tok + 1;
9629 *thread = strtol (tok, &tmptok, 0);
9630 if (tok == tmptok)
9631 error (_("Junk after thread keyword."));
9632 if (!valid_thread_id (*thread))
9633 invalid_thread_id_error (*thread);
9634 tok = tmptok;
9635 }
9636 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9637 {
9638 char *tmptok;
9639
9640 tok = end_tok + 1;
9641 *task = strtol (tok, &tmptok, 0);
9642 if (tok == tmptok)
9643 error (_("Junk after task keyword."));
9644 if (!valid_task_id (*task))
9645 error (_("Unknown task %d."), *task);
9646 tok = tmptok;
9647 }
9648 else if (rest)
9649 {
9650 *rest = savestring (tok, strlen (tok));
9651 return;
9652 }
9653 else
9654 error (_("Junk at end of arguments."));
9655 }
9656 }
9657
9658 /* Decode a static tracepoint marker spec. */
9659
9660 static struct symtabs_and_lines
9661 decode_static_tracepoint_spec (const char **arg_p)
9662 {
9663 VEC(static_tracepoint_marker_p) *markers = NULL;
9664 struct symtabs_and_lines sals;
9665 struct cleanup *old_chain;
9666 const char *p = &(*arg_p)[3];
9667 const char *endp;
9668 char *marker_str;
9669 int i;
9670
9671 p = skip_spaces_const (p);
9672
9673 endp = skip_to_space_const (p);
9674
9675 marker_str = savestring (p, endp - p);
9676 old_chain = make_cleanup (xfree, marker_str);
9677
9678 markers = target_static_tracepoint_markers_by_strid (marker_str);
9679 if (VEC_empty(static_tracepoint_marker_p, markers))
9680 error (_("No known static tracepoint marker named %s"), marker_str);
9681
9682 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
9683 sals.sals = XNEWVEC (struct symtab_and_line, sals.nelts);
9684
9685 for (i = 0; i < sals.nelts; i++)
9686 {
9687 struct static_tracepoint_marker *marker;
9688
9689 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9690
9691 init_sal (&sals.sals[i]);
9692
9693 sals.sals[i] = find_pc_line (marker->address, 0);
9694 sals.sals[i].pc = marker->address;
9695
9696 release_static_tracepoint_marker (marker);
9697 }
9698
9699 do_cleanups (old_chain);
9700
9701 *arg_p = endp;
9702 return sals;
9703 }
9704
9705 /* See breakpoint.h. */
9706
9707 int
9708 create_breakpoint (struct gdbarch *gdbarch,
9709 const struct event_location *location, char *cond_string,
9710 int thread, char *extra_string,
9711 int parse_extra,
9712 int tempflag, enum bptype type_wanted,
9713 int ignore_count,
9714 enum auto_boolean pending_break_support,
9715 const struct breakpoint_ops *ops,
9716 int from_tty, int enabled, int internal,
9717 unsigned flags)
9718 {
9719 struct linespec_result canonical;
9720 struct cleanup *old_chain;
9721 struct cleanup *bkpt_chain = NULL;
9722 int pending = 0;
9723 int task = 0;
9724 int prev_bkpt_count = breakpoint_count;
9725
9726 gdb_assert (ops != NULL);
9727
9728 /* If extra_string isn't useful, set it to NULL. */
9729 if (extra_string != NULL && *extra_string == '\0')
9730 extra_string = NULL;
9731
9732 init_linespec_result (&canonical);
9733
9734 TRY
9735 {
9736 ops->create_sals_from_location (location, &canonical, type_wanted);
9737 }
9738 CATCH (e, RETURN_MASK_ERROR)
9739 {
9740 /* If caller is interested in rc value from parse, set
9741 value. */
9742 if (e.error == NOT_FOUND_ERROR)
9743 {
9744 /* If pending breakpoint support is turned off, throw
9745 error. */
9746
9747 if (pending_break_support == AUTO_BOOLEAN_FALSE)
9748 throw_exception (e);
9749
9750 exception_print (gdb_stderr, e);
9751
9752 /* If pending breakpoint support is auto query and the user
9753 selects no, then simply return the error code. */
9754 if (pending_break_support == AUTO_BOOLEAN_AUTO
9755 && !nquery (_("Make %s pending on future shared library load? "),
9756 bptype_string (type_wanted)))
9757 return 0;
9758
9759 /* At this point, either the user was queried about setting
9760 a pending breakpoint and selected yes, or pending
9761 breakpoint behavior is on and thus a pending breakpoint
9762 is defaulted on behalf of the user. */
9763 pending = 1;
9764 }
9765 else
9766 throw_exception (e);
9767 }
9768 END_CATCH
9769
9770 if (!pending && VEC_empty (linespec_sals, canonical.sals))
9771 return 0;
9772
9773 /* Create a chain of things that always need to be cleaned up. */
9774 old_chain = make_cleanup_destroy_linespec_result (&canonical);
9775
9776 /* ----------------------------- SNIP -----------------------------
9777 Anything added to the cleanup chain beyond this point is assumed
9778 to be part of a breakpoint. If the breakpoint create succeeds
9779 then the memory is not reclaimed. */
9780 bkpt_chain = make_cleanup (null_cleanup, 0);
9781
9782 /* Resolve all line numbers to PC's and verify that the addresses
9783 are ok for the target. */
9784 if (!pending)
9785 {
9786 int ix;
9787 struct linespec_sals *iter;
9788
9789 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9790 breakpoint_sals_to_pc (&iter->sals);
9791 }
9792
9793 /* Fast tracepoints may have additional restrictions on location. */
9794 if (!pending && type_wanted == bp_fast_tracepoint)
9795 {
9796 int ix;
9797 struct linespec_sals *iter;
9798
9799 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9800 check_fast_tracepoint_sals (gdbarch, &iter->sals);
9801 }
9802
9803 /* Verify that condition can be parsed, before setting any
9804 breakpoints. Allocate a separate condition expression for each
9805 breakpoint. */
9806 if (!pending)
9807 {
9808 if (parse_extra)
9809 {
9810 char *rest;
9811 struct linespec_sals *lsal;
9812
9813 lsal = VEC_index (linespec_sals, canonical.sals, 0);
9814
9815 /* Here we only parse 'arg' to separate condition
9816 from thread number, so parsing in context of first
9817 sal is OK. When setting the breakpoint we'll
9818 re-parse it in context of each sal. */
9819
9820 find_condition_and_thread (extra_string, lsal->sals.sals[0].pc,
9821 &cond_string, &thread, &task, &rest);
9822 if (cond_string)
9823 make_cleanup (xfree, cond_string);
9824 if (rest)
9825 make_cleanup (xfree, rest);
9826 if (rest)
9827 extra_string = rest;
9828 else
9829 extra_string = NULL;
9830 }
9831 else
9832 {
9833 if (type_wanted != bp_dprintf
9834 && extra_string != NULL && *extra_string != '\0')
9835 error (_("Garbage '%s' at end of location"), extra_string);
9836
9837 /* Create a private copy of condition string. */
9838 if (cond_string)
9839 {
9840 cond_string = xstrdup (cond_string);
9841 make_cleanup (xfree, cond_string);
9842 }
9843 /* Create a private copy of any extra string. */
9844 if (extra_string)
9845 {
9846 extra_string = xstrdup (extra_string);
9847 make_cleanup (xfree, extra_string);
9848 }
9849 }
9850
9851 ops->create_breakpoints_sal (gdbarch, &canonical,
9852 cond_string, extra_string, type_wanted,
9853 tempflag ? disp_del : disp_donttouch,
9854 thread, task, ignore_count, ops,
9855 from_tty, enabled, internal, flags);
9856 }
9857 else
9858 {
9859 struct breakpoint *b;
9860
9861 if (is_tracepoint_type (type_wanted))
9862 {
9863 struct tracepoint *t;
9864
9865 t = XCNEW (struct tracepoint);
9866 b = &t->base;
9867 }
9868 else
9869 b = XNEW (struct breakpoint);
9870
9871 init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops);
9872 b->location = copy_event_location (location);
9873
9874 if (parse_extra)
9875 b->cond_string = NULL;
9876 else
9877 {
9878 /* Create a private copy of condition string. */
9879 if (cond_string)
9880 {
9881 cond_string = xstrdup (cond_string);
9882 make_cleanup (xfree, cond_string);
9883 }
9884 b->cond_string = cond_string;
9885 b->thread = thread;
9886 }
9887
9888 /* Create a private copy of any extra string. */
9889 if (extra_string != NULL)
9890 {
9891 extra_string = xstrdup (extra_string);
9892 make_cleanup (xfree, extra_string);
9893 }
9894 b->extra_string = extra_string;
9895 b->ignore_count = ignore_count;
9896 b->disposition = tempflag ? disp_del : disp_donttouch;
9897 b->condition_not_parsed = 1;
9898 b->enable_state = enabled ? bp_enabled : bp_disabled;
9899 if ((type_wanted != bp_breakpoint
9900 && type_wanted != bp_hardware_breakpoint) || thread != -1)
9901 b->pspace = current_program_space;
9902
9903 install_breakpoint (internal, b, 0);
9904 }
9905
9906 if (VEC_length (linespec_sals, canonical.sals) > 1)
9907 {
9908 warning (_("Multiple breakpoints were set.\nUse the "
9909 "\"delete\" command to delete unwanted breakpoints."));
9910 prev_breakpoint_count = prev_bkpt_count;
9911 }
9912
9913 /* That's it. Discard the cleanups for data inserted into the
9914 breakpoint. */
9915 discard_cleanups (bkpt_chain);
9916 /* But cleanup everything else. */
9917 do_cleanups (old_chain);
9918
9919 /* error call may happen here - have BKPT_CHAIN already discarded. */
9920 update_global_location_list (UGLL_MAY_INSERT);
9921
9922 return 1;
9923 }
9924
9925 /* Set a breakpoint.
9926 ARG is a string describing breakpoint address,
9927 condition, and thread.
9928 FLAG specifies if a breakpoint is hardware on,
9929 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9930 and BP_TEMPFLAG. */
9931
9932 static void
9933 break_command_1 (char *arg, int flag, int from_tty)
9934 {
9935 int tempflag = flag & BP_TEMPFLAG;
9936 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9937 ? bp_hardware_breakpoint
9938 : bp_breakpoint);
9939 struct breakpoint_ops *ops;
9940 struct event_location *location;
9941 struct cleanup *cleanup;
9942
9943 location = string_to_event_location (&arg, current_language);
9944 cleanup = make_cleanup_delete_event_location (location);
9945
9946 /* Matching breakpoints on probes. */
9947 if (location != NULL
9948 && event_location_type (location) == PROBE_LOCATION)
9949 ops = &bkpt_probe_breakpoint_ops;
9950 else
9951 ops = &bkpt_breakpoint_ops;
9952
9953 create_breakpoint (get_current_arch (),
9954 location,
9955 NULL, 0, arg, 1 /* parse arg */,
9956 tempflag, type_wanted,
9957 0 /* Ignore count */,
9958 pending_break_support,
9959 ops,
9960 from_tty,
9961 1 /* enabled */,
9962 0 /* internal */,
9963 0);
9964 do_cleanups (cleanup);
9965 }
9966
9967 /* Helper function for break_command_1 and disassemble_command. */
9968
9969 void
9970 resolve_sal_pc (struct symtab_and_line *sal)
9971 {
9972 CORE_ADDR pc;
9973
9974 if (sal->pc == 0 && sal->symtab != NULL)
9975 {
9976 if (!find_line_pc (sal->symtab, sal->line, &pc))
9977 error (_("No line %d in file \"%s\"."),
9978 sal->line, symtab_to_filename_for_display (sal->symtab));
9979 sal->pc = pc;
9980
9981 /* If this SAL corresponds to a breakpoint inserted using a line
9982 number, then skip the function prologue if necessary. */
9983 if (sal->explicit_line)
9984 skip_prologue_sal (sal);
9985 }
9986
9987 if (sal->section == 0 && sal->symtab != NULL)
9988 {
9989 const struct blockvector *bv;
9990 const struct block *b;
9991 struct symbol *sym;
9992
9993 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9994 SYMTAB_COMPUNIT (sal->symtab));
9995 if (bv != NULL)
9996 {
9997 sym = block_linkage_function (b);
9998 if (sym != NULL)
9999 {
10000 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
10001 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
10002 sym);
10003 }
10004 else
10005 {
10006 /* It really is worthwhile to have the section, so we'll
10007 just have to look harder. This case can be executed
10008 if we have line numbers but no functions (as can
10009 happen in assembly source). */
10010
10011 struct bound_minimal_symbol msym;
10012 struct cleanup *old_chain = save_current_space_and_thread ();
10013
10014 switch_to_program_space_and_thread (sal->pspace);
10015
10016 msym = lookup_minimal_symbol_by_pc (sal->pc);
10017 if (msym.minsym)
10018 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
10019
10020 do_cleanups (old_chain);
10021 }
10022 }
10023 }
10024 }
10025
10026 void
10027 break_command (char *arg, int from_tty)
10028 {
10029 break_command_1 (arg, 0, from_tty);
10030 }
10031
10032 void
10033 tbreak_command (char *arg, int from_tty)
10034 {
10035 break_command_1 (arg, BP_TEMPFLAG, from_tty);
10036 }
10037
10038 static void
10039 hbreak_command (char *arg, int from_tty)
10040 {
10041 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
10042 }
10043
10044 static void
10045 thbreak_command (char *arg, int from_tty)
10046 {
10047 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
10048 }
10049
10050 static void
10051 stop_command (char *arg, int from_tty)
10052 {
10053 printf_filtered (_("Specify the type of breakpoint to set.\n\
10054 Usage: stop in <function | address>\n\
10055 stop at <line>\n"));
10056 }
10057
10058 static void
10059 stopin_command (char *arg, int from_tty)
10060 {
10061 int badInput = 0;
10062
10063 if (arg == (char *) NULL)
10064 badInput = 1;
10065 else if (*arg != '*')
10066 {
10067 char *argptr = arg;
10068 int hasColon = 0;
10069
10070 /* Look for a ':'. If this is a line number specification, then
10071 say it is bad, otherwise, it should be an address or
10072 function/method name. */
10073 while (*argptr && !hasColon)
10074 {
10075 hasColon = (*argptr == ':');
10076 argptr++;
10077 }
10078
10079 if (hasColon)
10080 badInput = (*argptr != ':'); /* Not a class::method */
10081 else
10082 badInput = isdigit (*arg); /* a simple line number */
10083 }
10084
10085 if (badInput)
10086 printf_filtered (_("Usage: stop in <function | address>\n"));
10087 else
10088 break_command_1 (arg, 0, from_tty);
10089 }
10090
10091 static void
10092 stopat_command (char *arg, int from_tty)
10093 {
10094 int badInput = 0;
10095
10096 if (arg == (char *) NULL || *arg == '*') /* no line number */
10097 badInput = 1;
10098 else
10099 {
10100 char *argptr = arg;
10101 int hasColon = 0;
10102
10103 /* Look for a ':'. If there is a '::' then get out, otherwise
10104 it is probably a line number. */
10105 while (*argptr && !hasColon)
10106 {
10107 hasColon = (*argptr == ':');
10108 argptr++;
10109 }
10110
10111 if (hasColon)
10112 badInput = (*argptr == ':'); /* we have class::method */
10113 else
10114 badInput = !isdigit (*arg); /* not a line number */
10115 }
10116
10117 if (badInput)
10118 printf_filtered (_("Usage: stop at <line>\n"));
10119 else
10120 break_command_1 (arg, 0, from_tty);
10121 }
10122
10123 /* The dynamic printf command is mostly like a regular breakpoint, but
10124 with a prewired command list consisting of a single output command,
10125 built from extra arguments supplied on the dprintf command
10126 line. */
10127
10128 static void
10129 dprintf_command (char *arg, int from_tty)
10130 {
10131 struct event_location *location;
10132 struct cleanup *cleanup;
10133
10134 location = string_to_event_location (&arg, current_language);
10135 cleanup = make_cleanup_delete_event_location (location);
10136
10137 /* If non-NULL, ARG should have been advanced past the location;
10138 the next character must be ','. */
10139 if (arg != NULL)
10140 {
10141 if (arg[0] != ',' || arg[1] == '\0')
10142 error (_("Format string required"));
10143 else
10144 {
10145 /* Skip the comma. */
10146 ++arg;
10147 }
10148 }
10149
10150 create_breakpoint (get_current_arch (),
10151 location,
10152 NULL, 0, arg, 1 /* parse arg */,
10153 0, bp_dprintf,
10154 0 /* Ignore count */,
10155 pending_break_support,
10156 &dprintf_breakpoint_ops,
10157 from_tty,
10158 1 /* enabled */,
10159 0 /* internal */,
10160 0);
10161 do_cleanups (cleanup);
10162 }
10163
10164 static void
10165 agent_printf_command (char *arg, int from_tty)
10166 {
10167 error (_("May only run agent-printf on the target"));
10168 }
10169
10170 /* Implement the "breakpoint_hit" breakpoint_ops method for
10171 ranged breakpoints. */
10172
10173 static int
10174 breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
10175 struct address_space *aspace,
10176 CORE_ADDR bp_addr,
10177 const struct target_waitstatus *ws)
10178 {
10179 if (ws->kind != TARGET_WAITKIND_STOPPED
10180 || ws->value.sig != GDB_SIGNAL_TRAP)
10181 return 0;
10182
10183 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
10184 bl->length, aspace, bp_addr);
10185 }
10186
10187 /* Implement the "resources_needed" breakpoint_ops method for
10188 ranged breakpoints. */
10189
10190 static int
10191 resources_needed_ranged_breakpoint (const struct bp_location *bl)
10192 {
10193 return target_ranged_break_num_registers ();
10194 }
10195
10196 /* Implement the "print_it" breakpoint_ops method for
10197 ranged breakpoints. */
10198
10199 static enum print_stop_action
10200 print_it_ranged_breakpoint (bpstat bs)
10201 {
10202 struct breakpoint *b = bs->breakpoint_at;
10203 struct bp_location *bl = b->loc;
10204 struct ui_out *uiout = current_uiout;
10205
10206 gdb_assert (b->type == bp_hardware_breakpoint);
10207
10208 /* Ranged breakpoints have only one location. */
10209 gdb_assert (bl && bl->next == NULL);
10210
10211 annotate_breakpoint (b->number);
10212 if (b->disposition == disp_del)
10213 ui_out_text (uiout, "\nTemporary ranged breakpoint ");
10214 else
10215 ui_out_text (uiout, "\nRanged breakpoint ");
10216 if (ui_out_is_mi_like_p (uiout))
10217 {
10218 ui_out_field_string (uiout, "reason",
10219 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
10220 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
10221 }
10222 ui_out_field_int (uiout, "bkptno", b->number);
10223 ui_out_text (uiout, ", ");
10224
10225 return PRINT_SRC_AND_LOC;
10226 }
10227
10228 /* Implement the "print_one" breakpoint_ops method for
10229 ranged breakpoints. */
10230
10231 static void
10232 print_one_ranged_breakpoint (struct breakpoint *b,
10233 struct bp_location **last_loc)
10234 {
10235 struct bp_location *bl = b->loc;
10236 struct value_print_options opts;
10237 struct ui_out *uiout = current_uiout;
10238
10239 /* Ranged breakpoints have only one location. */
10240 gdb_assert (bl && bl->next == NULL);
10241
10242 get_user_print_options (&opts);
10243
10244 if (opts.addressprint)
10245 /* We don't print the address range here, it will be printed later
10246 by print_one_detail_ranged_breakpoint. */
10247 ui_out_field_skip (uiout, "addr");
10248 annotate_field (5);
10249 print_breakpoint_location (b, bl);
10250 *last_loc = bl;
10251 }
10252
10253 /* Implement the "print_one_detail" breakpoint_ops method for
10254 ranged breakpoints. */
10255
10256 static void
10257 print_one_detail_ranged_breakpoint (const struct breakpoint *b,
10258 struct ui_out *uiout)
10259 {
10260 CORE_ADDR address_start, address_end;
10261 struct bp_location *bl = b->loc;
10262 struct ui_file *stb = mem_fileopen ();
10263 struct cleanup *cleanup = make_cleanup_ui_file_delete (stb);
10264
10265 gdb_assert (bl);
10266
10267 address_start = bl->address;
10268 address_end = address_start + bl->length - 1;
10269
10270 ui_out_text (uiout, "\taddress range: ");
10271 fprintf_unfiltered (stb, "[%s, %s]",
10272 print_core_address (bl->gdbarch, address_start),
10273 print_core_address (bl->gdbarch, address_end));
10274 ui_out_field_stream (uiout, "addr", stb);
10275 ui_out_text (uiout, "\n");
10276
10277 do_cleanups (cleanup);
10278 }
10279
10280 /* Implement the "print_mention" breakpoint_ops method for
10281 ranged breakpoints. */
10282
10283 static void
10284 print_mention_ranged_breakpoint (struct breakpoint *b)
10285 {
10286 struct bp_location *bl = b->loc;
10287 struct ui_out *uiout = current_uiout;
10288
10289 gdb_assert (bl);
10290 gdb_assert (b->type == bp_hardware_breakpoint);
10291
10292 if (ui_out_is_mi_like_p (uiout))
10293 return;
10294
10295 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
10296 b->number, paddress (bl->gdbarch, bl->address),
10297 paddress (bl->gdbarch, bl->address + bl->length - 1));
10298 }
10299
10300 /* Implement the "print_recreate" breakpoint_ops method for
10301 ranged breakpoints. */
10302
10303 static void
10304 print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
10305 {
10306 fprintf_unfiltered (fp, "break-range %s, %s",
10307 event_location_to_string (b->location),
10308 event_location_to_string (b->location_range_end));
10309 print_recreate_thread (b, fp);
10310 }
10311
10312 /* The breakpoint_ops structure to be used in ranged breakpoints. */
10313
10314 static struct breakpoint_ops ranged_breakpoint_ops;
10315
10316 /* Find the address where the end of the breakpoint range should be
10317 placed, given the SAL of the end of the range. This is so that if
10318 the user provides a line number, the end of the range is set to the
10319 last instruction of the given line. */
10320
10321 static CORE_ADDR
10322 find_breakpoint_range_end (struct symtab_and_line sal)
10323 {
10324 CORE_ADDR end;
10325
10326 /* If the user provided a PC value, use it. Otherwise,
10327 find the address of the end of the given location. */
10328 if (sal.explicit_pc)
10329 end = sal.pc;
10330 else
10331 {
10332 int ret;
10333 CORE_ADDR start;
10334
10335 ret = find_line_pc_range (sal, &start, &end);
10336 if (!ret)
10337 error (_("Could not find location of the end of the range."));
10338
10339 /* find_line_pc_range returns the start of the next line. */
10340 end--;
10341 }
10342
10343 return end;
10344 }
10345
10346 /* Implement the "break-range" CLI command. */
10347
10348 static void
10349 break_range_command (char *arg, int from_tty)
10350 {
10351 char *arg_start, *addr_string_start, *addr_string_end;
10352 struct linespec_result canonical_start, canonical_end;
10353 int bp_count, can_use_bp, length;
10354 CORE_ADDR end;
10355 struct breakpoint *b;
10356 struct symtab_and_line sal_start, sal_end;
10357 struct cleanup *cleanup_bkpt;
10358 struct linespec_sals *lsal_start, *lsal_end;
10359 struct event_location *start_location, *end_location;
10360
10361 /* We don't support software ranged breakpoints. */
10362 if (target_ranged_break_num_registers () < 0)
10363 error (_("This target does not support hardware ranged breakpoints."));
10364
10365 bp_count = hw_breakpoint_used_count ();
10366 bp_count += target_ranged_break_num_registers ();
10367 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10368 bp_count, 0);
10369 if (can_use_bp < 0)
10370 error (_("Hardware breakpoints used exceeds limit."));
10371
10372 arg = skip_spaces (arg);
10373 if (arg == NULL || arg[0] == '\0')
10374 error(_("No address range specified."));
10375
10376 init_linespec_result (&canonical_start);
10377
10378 arg_start = arg;
10379 start_location = string_to_event_location (&arg, current_language);
10380 cleanup_bkpt = make_cleanup_delete_event_location (start_location);
10381 parse_breakpoint_sals (start_location, &canonical_start);
10382 make_cleanup_destroy_linespec_result (&canonical_start);
10383
10384 if (arg[0] != ',')
10385 error (_("Too few arguments."));
10386 else if (VEC_empty (linespec_sals, canonical_start.sals))
10387 error (_("Could not find location of the beginning of the range."));
10388
10389 lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
10390
10391 if (VEC_length (linespec_sals, canonical_start.sals) > 1
10392 || lsal_start->sals.nelts != 1)
10393 error (_("Cannot create a ranged breakpoint with multiple locations."));
10394
10395 sal_start = lsal_start->sals.sals[0];
10396 addr_string_start = savestring (arg_start, arg - arg_start);
10397 make_cleanup (xfree, addr_string_start);
10398
10399 arg++; /* Skip the comma. */
10400 arg = skip_spaces (arg);
10401
10402 /* Parse the end location. */
10403
10404 init_linespec_result (&canonical_end);
10405 arg_start = arg;
10406
10407 /* We call decode_line_full directly here instead of using
10408 parse_breakpoint_sals because we need to specify the start location's
10409 symtab and line as the default symtab and line for the end of the
10410 range. This makes it possible to have ranges like "foo.c:27, +14",
10411 where +14 means 14 lines from the start location. */
10412 end_location = string_to_event_location (&arg, current_language);
10413 make_cleanup_delete_event_location (end_location);
10414 decode_line_full (end_location, DECODE_LINE_FUNFIRSTLINE,
10415 sal_start.symtab, sal_start.line,
10416 &canonical_end, NULL, NULL);
10417
10418 make_cleanup_destroy_linespec_result (&canonical_end);
10419
10420 if (VEC_empty (linespec_sals, canonical_end.sals))
10421 error (_("Could not find location of the end of the range."));
10422
10423 lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
10424 if (VEC_length (linespec_sals, canonical_end.sals) > 1
10425 || lsal_end->sals.nelts != 1)
10426 error (_("Cannot create a ranged breakpoint with multiple locations."));
10427
10428 sal_end = lsal_end->sals.sals[0];
10429
10430 end = find_breakpoint_range_end (sal_end);
10431 if (sal_start.pc > end)
10432 error (_("Invalid address range, end precedes start."));
10433
10434 length = end - sal_start.pc + 1;
10435 if (length < 0)
10436 /* Length overflowed. */
10437 error (_("Address range too large."));
10438 else if (length == 1)
10439 {
10440 /* This range is simple enough to be handled by
10441 the `hbreak' command. */
10442 hbreak_command (addr_string_start, 1);
10443
10444 do_cleanups (cleanup_bkpt);
10445
10446 return;
10447 }
10448
10449 /* Now set up the breakpoint. */
10450 b = set_raw_breakpoint (get_current_arch (), sal_start,
10451 bp_hardware_breakpoint, &ranged_breakpoint_ops);
10452 set_breakpoint_count (breakpoint_count + 1);
10453 b->number = breakpoint_count;
10454 b->disposition = disp_donttouch;
10455 b->location = copy_event_location (start_location);
10456 b->location_range_end = copy_event_location (end_location);
10457 b->loc->length = length;
10458
10459 do_cleanups (cleanup_bkpt);
10460
10461 mention (b);
10462 observer_notify_breakpoint_created (b);
10463 update_global_location_list (UGLL_MAY_INSERT);
10464 }
10465
10466 /* Return non-zero if EXP is verified as constant. Returned zero
10467 means EXP is variable. Also the constant detection may fail for
10468 some constant expressions and in such case still falsely return
10469 zero. */
10470
10471 static int
10472 watchpoint_exp_is_const (const struct expression *exp)
10473 {
10474 int i = exp->nelts;
10475
10476 while (i > 0)
10477 {
10478 int oplenp, argsp;
10479
10480 /* We are only interested in the descriptor of each element. */
10481 operator_length (exp, i, &oplenp, &argsp);
10482 i -= oplenp;
10483
10484 switch (exp->elts[i].opcode)
10485 {
10486 case BINOP_ADD:
10487 case BINOP_SUB:
10488 case BINOP_MUL:
10489 case BINOP_DIV:
10490 case BINOP_REM:
10491 case BINOP_MOD:
10492 case BINOP_LSH:
10493 case BINOP_RSH:
10494 case BINOP_LOGICAL_AND:
10495 case BINOP_LOGICAL_OR:
10496 case BINOP_BITWISE_AND:
10497 case BINOP_BITWISE_IOR:
10498 case BINOP_BITWISE_XOR:
10499 case BINOP_EQUAL:
10500 case BINOP_NOTEQUAL:
10501 case BINOP_LESS:
10502 case BINOP_GTR:
10503 case BINOP_LEQ:
10504 case BINOP_GEQ:
10505 case BINOP_REPEAT:
10506 case BINOP_COMMA:
10507 case BINOP_EXP:
10508 case BINOP_MIN:
10509 case BINOP_MAX:
10510 case BINOP_INTDIV:
10511 case BINOP_CONCAT:
10512 case TERNOP_COND:
10513 case TERNOP_SLICE:
10514
10515 case OP_LONG:
10516 case OP_DOUBLE:
10517 case OP_DECFLOAT:
10518 case OP_LAST:
10519 case OP_COMPLEX:
10520 case OP_STRING:
10521 case OP_ARRAY:
10522 case OP_TYPE:
10523 case OP_TYPEOF:
10524 case OP_DECLTYPE:
10525 case OP_TYPEID:
10526 case OP_NAME:
10527 case OP_OBJC_NSSTRING:
10528
10529 case UNOP_NEG:
10530 case UNOP_LOGICAL_NOT:
10531 case UNOP_COMPLEMENT:
10532 case UNOP_ADDR:
10533 case UNOP_HIGH:
10534 case UNOP_CAST:
10535
10536 case UNOP_CAST_TYPE:
10537 case UNOP_REINTERPRET_CAST:
10538 case UNOP_DYNAMIC_CAST:
10539 /* Unary, binary and ternary operators: We have to check
10540 their operands. If they are constant, then so is the
10541 result of that operation. For instance, if A and B are
10542 determined to be constants, then so is "A + B".
10543
10544 UNOP_IND is one exception to the rule above, because the
10545 value of *ADDR is not necessarily a constant, even when
10546 ADDR is. */
10547 break;
10548
10549 case OP_VAR_VALUE:
10550 /* Check whether the associated symbol is a constant.
10551
10552 We use SYMBOL_CLASS rather than TYPE_CONST because it's
10553 possible that a buggy compiler could mark a variable as
10554 constant even when it is not, and TYPE_CONST would return
10555 true in this case, while SYMBOL_CLASS wouldn't.
10556
10557 We also have to check for function symbols because they
10558 are always constant. */
10559 {
10560 struct symbol *s = exp->elts[i + 2].symbol;
10561
10562 if (SYMBOL_CLASS (s) != LOC_BLOCK
10563 && SYMBOL_CLASS (s) != LOC_CONST
10564 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10565 return 0;
10566 break;
10567 }
10568
10569 /* The default action is to return 0 because we are using
10570 the optimistic approach here: If we don't know something,
10571 then it is not a constant. */
10572 default:
10573 return 0;
10574 }
10575 }
10576
10577 return 1;
10578 }
10579
10580 /* Implement the "dtor" breakpoint_ops method for watchpoints. */
10581
10582 static void
10583 dtor_watchpoint (struct breakpoint *self)
10584 {
10585 struct watchpoint *w = (struct watchpoint *) self;
10586
10587 xfree (w->cond_exp);
10588 xfree (w->exp);
10589 xfree (w->exp_string);
10590 xfree (w->exp_string_reparse);
10591 value_free (w->val);
10592
10593 base_breakpoint_ops.dtor (self);
10594 }
10595
10596 /* Implement the "re_set" breakpoint_ops method for watchpoints. */
10597
10598 static void
10599 re_set_watchpoint (struct breakpoint *b)
10600 {
10601 struct watchpoint *w = (struct watchpoint *) b;
10602
10603 /* Watchpoint can be either on expression using entirely global
10604 variables, or it can be on local variables.
10605
10606 Watchpoints of the first kind are never auto-deleted, and even
10607 persist across program restarts. Since they can use variables
10608 from shared libraries, we need to reparse expression as libraries
10609 are loaded and unloaded.
10610
10611 Watchpoints on local variables can also change meaning as result
10612 of solib event. For example, if a watchpoint uses both a local
10613 and a global variables in expression, it's a local watchpoint,
10614 but unloading of a shared library will make the expression
10615 invalid. This is not a very common use case, but we still
10616 re-evaluate expression, to avoid surprises to the user.
10617
10618 Note that for local watchpoints, we re-evaluate it only if
10619 watchpoints frame id is still valid. If it's not, it means the
10620 watchpoint is out of scope and will be deleted soon. In fact,
10621 I'm not sure we'll ever be called in this case.
10622
10623 If a local watchpoint's frame id is still valid, then
10624 w->exp_valid_block is likewise valid, and we can safely use it.
10625
10626 Don't do anything about disabled watchpoints, since they will be
10627 reevaluated again when enabled. */
10628 update_watchpoint (w, 1 /* reparse */);
10629 }
10630
10631 /* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10632
10633 static int
10634 insert_watchpoint (struct bp_location *bl)
10635 {
10636 struct watchpoint *w = (struct watchpoint *) bl->owner;
10637 int length = w->exact ? 1 : bl->length;
10638
10639 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
10640 w->cond_exp);
10641 }
10642
10643 /* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10644
10645 static int
10646 remove_watchpoint (struct bp_location *bl)
10647 {
10648 struct watchpoint *w = (struct watchpoint *) bl->owner;
10649 int length = w->exact ? 1 : bl->length;
10650
10651 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
10652 w->cond_exp);
10653 }
10654
10655 static int
10656 breakpoint_hit_watchpoint (const struct bp_location *bl,
10657 struct address_space *aspace, CORE_ADDR bp_addr,
10658 const struct target_waitstatus *ws)
10659 {
10660 struct breakpoint *b = bl->owner;
10661 struct watchpoint *w = (struct watchpoint *) b;
10662
10663 /* Continuable hardware watchpoints are treated as non-existent if the
10664 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10665 some data address). Otherwise gdb won't stop on a break instruction
10666 in the code (not from a breakpoint) when a hardware watchpoint has
10667 been defined. Also skip watchpoints which we know did not trigger
10668 (did not match the data address). */
10669 if (is_hardware_watchpoint (b)
10670 && w->watchpoint_triggered == watch_triggered_no)
10671 return 0;
10672
10673 return 1;
10674 }
10675
10676 static void
10677 check_status_watchpoint (bpstat bs)
10678 {
10679 gdb_assert (is_watchpoint (bs->breakpoint_at));
10680
10681 bpstat_check_watchpoint (bs);
10682 }
10683
10684 /* Implement the "resources_needed" breakpoint_ops method for
10685 hardware watchpoints. */
10686
10687 static int
10688 resources_needed_watchpoint (const struct bp_location *bl)
10689 {
10690 struct watchpoint *w = (struct watchpoint *) bl->owner;
10691 int length = w->exact? 1 : bl->length;
10692
10693 return target_region_ok_for_hw_watchpoint (bl->address, length);
10694 }
10695
10696 /* Implement the "works_in_software_mode" breakpoint_ops method for
10697 hardware watchpoints. */
10698
10699 static int
10700 works_in_software_mode_watchpoint (const struct breakpoint *b)
10701 {
10702 /* Read and access watchpoints only work with hardware support. */
10703 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
10704 }
10705
10706 static enum print_stop_action
10707 print_it_watchpoint (bpstat bs)
10708 {
10709 struct cleanup *old_chain;
10710 struct breakpoint *b;
10711 struct ui_file *stb;
10712 enum print_stop_action result;
10713 struct watchpoint *w;
10714 struct ui_out *uiout = current_uiout;
10715
10716 gdb_assert (bs->bp_location_at != NULL);
10717
10718 b = bs->breakpoint_at;
10719 w = (struct watchpoint *) b;
10720
10721 stb = mem_fileopen ();
10722 old_chain = make_cleanup_ui_file_delete (stb);
10723
10724 switch (b->type)
10725 {
10726 case bp_watchpoint:
10727 case bp_hardware_watchpoint:
10728 annotate_watchpoint (b->number);
10729 if (ui_out_is_mi_like_p (uiout))
10730 ui_out_field_string
10731 (uiout, "reason",
10732 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10733 mention (b);
10734 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10735 ui_out_text (uiout, "\nOld value = ");
10736 watchpoint_value_print (bs->old_val, stb);
10737 ui_out_field_stream (uiout, "old", stb);
10738 ui_out_text (uiout, "\nNew value = ");
10739 watchpoint_value_print (w->val, stb);
10740 ui_out_field_stream (uiout, "new", stb);
10741 ui_out_text (uiout, "\n");
10742 /* More than one watchpoint may have been triggered. */
10743 result = PRINT_UNKNOWN;
10744 break;
10745
10746 case bp_read_watchpoint:
10747 if (ui_out_is_mi_like_p (uiout))
10748 ui_out_field_string
10749 (uiout, "reason",
10750 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10751 mention (b);
10752 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10753 ui_out_text (uiout, "\nValue = ");
10754 watchpoint_value_print (w->val, stb);
10755 ui_out_field_stream (uiout, "value", stb);
10756 ui_out_text (uiout, "\n");
10757 result = PRINT_UNKNOWN;
10758 break;
10759
10760 case bp_access_watchpoint:
10761 if (bs->old_val != NULL)
10762 {
10763 annotate_watchpoint (b->number);
10764 if (ui_out_is_mi_like_p (uiout))
10765 ui_out_field_string
10766 (uiout, "reason",
10767 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10768 mention (b);
10769 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10770 ui_out_text (uiout, "\nOld value = ");
10771 watchpoint_value_print (bs->old_val, stb);
10772 ui_out_field_stream (uiout, "old", stb);
10773 ui_out_text (uiout, "\nNew value = ");
10774 }
10775 else
10776 {
10777 mention (b);
10778 if (ui_out_is_mi_like_p (uiout))
10779 ui_out_field_string
10780 (uiout, "reason",
10781 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10782 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10783 ui_out_text (uiout, "\nValue = ");
10784 }
10785 watchpoint_value_print (w->val, stb);
10786 ui_out_field_stream (uiout, "new", stb);
10787 ui_out_text (uiout, "\n");
10788 result = PRINT_UNKNOWN;
10789 break;
10790 default:
10791 result = PRINT_UNKNOWN;
10792 }
10793
10794 do_cleanups (old_chain);
10795 return result;
10796 }
10797
10798 /* Implement the "print_mention" breakpoint_ops method for hardware
10799 watchpoints. */
10800
10801 static void
10802 print_mention_watchpoint (struct breakpoint *b)
10803 {
10804 struct cleanup *ui_out_chain;
10805 struct watchpoint *w = (struct watchpoint *) b;
10806 struct ui_out *uiout = current_uiout;
10807
10808 switch (b->type)
10809 {
10810 case bp_watchpoint:
10811 ui_out_text (uiout, "Watchpoint ");
10812 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10813 break;
10814 case bp_hardware_watchpoint:
10815 ui_out_text (uiout, "Hardware watchpoint ");
10816 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10817 break;
10818 case bp_read_watchpoint:
10819 ui_out_text (uiout, "Hardware read watchpoint ");
10820 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
10821 break;
10822 case bp_access_watchpoint:
10823 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
10824 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
10825 break;
10826 default:
10827 internal_error (__FILE__, __LINE__,
10828 _("Invalid hardware watchpoint type."));
10829 }
10830
10831 ui_out_field_int (uiout, "number", b->number);
10832 ui_out_text (uiout, ": ");
10833 ui_out_field_string (uiout, "exp", w->exp_string);
10834 do_cleanups (ui_out_chain);
10835 }
10836
10837 /* Implement the "print_recreate" breakpoint_ops method for
10838 watchpoints. */
10839
10840 static void
10841 print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10842 {
10843 struct watchpoint *w = (struct watchpoint *) b;
10844
10845 switch (b->type)
10846 {
10847 case bp_watchpoint:
10848 case bp_hardware_watchpoint:
10849 fprintf_unfiltered (fp, "watch");
10850 break;
10851 case bp_read_watchpoint:
10852 fprintf_unfiltered (fp, "rwatch");
10853 break;
10854 case bp_access_watchpoint:
10855 fprintf_unfiltered (fp, "awatch");
10856 break;
10857 default:
10858 internal_error (__FILE__, __LINE__,
10859 _("Invalid watchpoint type."));
10860 }
10861
10862 fprintf_unfiltered (fp, " %s", w->exp_string);
10863 print_recreate_thread (b, fp);
10864 }
10865
10866 /* Implement the "explains_signal" breakpoint_ops method for
10867 watchpoints. */
10868
10869 static int
10870 explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10871 {
10872 /* A software watchpoint cannot cause a signal other than
10873 GDB_SIGNAL_TRAP. */
10874 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
10875 return 0;
10876
10877 return 1;
10878 }
10879
10880 /* The breakpoint_ops structure to be used in hardware watchpoints. */
10881
10882 static struct breakpoint_ops watchpoint_breakpoint_ops;
10883
10884 /* Implement the "insert" breakpoint_ops method for
10885 masked hardware watchpoints. */
10886
10887 static int
10888 insert_masked_watchpoint (struct bp_location *bl)
10889 {
10890 struct watchpoint *w = (struct watchpoint *) bl->owner;
10891
10892 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
10893 bl->watchpoint_type);
10894 }
10895
10896 /* Implement the "remove" breakpoint_ops method for
10897 masked hardware watchpoints. */
10898
10899 static int
10900 remove_masked_watchpoint (struct bp_location *bl)
10901 {
10902 struct watchpoint *w = (struct watchpoint *) bl->owner;
10903
10904 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
10905 bl->watchpoint_type);
10906 }
10907
10908 /* Implement the "resources_needed" breakpoint_ops method for
10909 masked hardware watchpoints. */
10910
10911 static int
10912 resources_needed_masked_watchpoint (const struct bp_location *bl)
10913 {
10914 struct watchpoint *w = (struct watchpoint *) bl->owner;
10915
10916 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
10917 }
10918
10919 /* Implement the "works_in_software_mode" breakpoint_ops method for
10920 masked hardware watchpoints. */
10921
10922 static int
10923 works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10924 {
10925 return 0;
10926 }
10927
10928 /* Implement the "print_it" breakpoint_ops method for
10929 masked hardware watchpoints. */
10930
10931 static enum print_stop_action
10932 print_it_masked_watchpoint (bpstat bs)
10933 {
10934 struct breakpoint *b = bs->breakpoint_at;
10935 struct ui_out *uiout = current_uiout;
10936
10937 /* Masked watchpoints have only one location. */
10938 gdb_assert (b->loc && b->loc->next == NULL);
10939
10940 switch (b->type)
10941 {
10942 case bp_hardware_watchpoint:
10943 annotate_watchpoint (b->number);
10944 if (ui_out_is_mi_like_p (uiout))
10945 ui_out_field_string
10946 (uiout, "reason",
10947 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10948 break;
10949
10950 case bp_read_watchpoint:
10951 if (ui_out_is_mi_like_p (uiout))
10952 ui_out_field_string
10953 (uiout, "reason",
10954 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10955 break;
10956
10957 case bp_access_watchpoint:
10958 if (ui_out_is_mi_like_p (uiout))
10959 ui_out_field_string
10960 (uiout, "reason",
10961 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10962 break;
10963 default:
10964 internal_error (__FILE__, __LINE__,
10965 _("Invalid hardware watchpoint type."));
10966 }
10967
10968 mention (b);
10969 ui_out_text (uiout, _("\n\
10970 Check the underlying instruction at PC for the memory\n\
10971 address and value which triggered this watchpoint.\n"));
10972 ui_out_text (uiout, "\n");
10973
10974 /* More than one watchpoint may have been triggered. */
10975 return PRINT_UNKNOWN;
10976 }
10977
10978 /* Implement the "print_one_detail" breakpoint_ops method for
10979 masked hardware watchpoints. */
10980
10981 static void
10982 print_one_detail_masked_watchpoint (const struct breakpoint *b,
10983 struct ui_out *uiout)
10984 {
10985 struct watchpoint *w = (struct watchpoint *) b;
10986
10987 /* Masked watchpoints have only one location. */
10988 gdb_assert (b->loc && b->loc->next == NULL);
10989
10990 ui_out_text (uiout, "\tmask ");
10991 ui_out_field_core_addr (uiout, "mask", b->loc->gdbarch, w->hw_wp_mask);
10992 ui_out_text (uiout, "\n");
10993 }
10994
10995 /* Implement the "print_mention" breakpoint_ops method for
10996 masked hardware watchpoints. */
10997
10998 static void
10999 print_mention_masked_watchpoint (struct breakpoint *b)
11000 {
11001 struct watchpoint *w = (struct watchpoint *) b;
11002 struct ui_out *uiout = current_uiout;
11003 struct cleanup *ui_out_chain;
11004
11005 switch (b->type)
11006 {
11007 case bp_hardware_watchpoint:
11008 ui_out_text (uiout, "Masked hardware watchpoint ");
11009 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
11010 break;
11011 case bp_read_watchpoint:
11012 ui_out_text (uiout, "Masked hardware read watchpoint ");
11013 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
11014 break;
11015 case bp_access_watchpoint:
11016 ui_out_text (uiout, "Masked hardware access (read/write) watchpoint ");
11017 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
11018 break;
11019 default:
11020 internal_error (__FILE__, __LINE__,
11021 _("Invalid hardware watchpoint type."));
11022 }
11023
11024 ui_out_field_int (uiout, "number", b->number);
11025 ui_out_text (uiout, ": ");
11026 ui_out_field_string (uiout, "exp", w->exp_string);
11027 do_cleanups (ui_out_chain);
11028 }
11029
11030 /* Implement the "print_recreate" breakpoint_ops method for
11031 masked hardware watchpoints. */
11032
11033 static void
11034 print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
11035 {
11036 struct watchpoint *w = (struct watchpoint *) b;
11037 char tmp[40];
11038
11039 switch (b->type)
11040 {
11041 case bp_hardware_watchpoint:
11042 fprintf_unfiltered (fp, "watch");
11043 break;
11044 case bp_read_watchpoint:
11045 fprintf_unfiltered (fp, "rwatch");
11046 break;
11047 case bp_access_watchpoint:
11048 fprintf_unfiltered (fp, "awatch");
11049 break;
11050 default:
11051 internal_error (__FILE__, __LINE__,
11052 _("Invalid hardware watchpoint type."));
11053 }
11054
11055 sprintf_vma (tmp, w->hw_wp_mask);
11056 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
11057 print_recreate_thread (b, fp);
11058 }
11059
11060 /* The breakpoint_ops structure to be used in masked hardware watchpoints. */
11061
11062 static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
11063
11064 /* Tell whether the given watchpoint is a masked hardware watchpoint. */
11065
11066 static int
11067 is_masked_watchpoint (const struct breakpoint *b)
11068 {
11069 return b->ops == &masked_watchpoint_breakpoint_ops;
11070 }
11071
11072 /* accessflag: hw_write: watch write,
11073 hw_read: watch read,
11074 hw_access: watch access (read or write) */
11075 static void
11076 watch_command_1 (const char *arg, int accessflag, int from_tty,
11077 int just_location, int internal)
11078 {
11079 struct breakpoint *b, *scope_breakpoint = NULL;
11080 struct expression *exp;
11081 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
11082 struct value *val, *mark, *result;
11083 int saved_bitpos = 0, saved_bitsize = 0;
11084 struct frame_info *frame;
11085 const char *exp_start = NULL;
11086 const char *exp_end = NULL;
11087 const char *tok, *end_tok;
11088 int toklen = -1;
11089 const char *cond_start = NULL;
11090 const char *cond_end = NULL;
11091 enum bptype bp_type;
11092 int thread = -1;
11093 int pc = 0;
11094 /* Flag to indicate whether we are going to use masks for
11095 the hardware watchpoint. */
11096 int use_mask = 0;
11097 CORE_ADDR mask = 0;
11098 struct watchpoint *w;
11099 char *expression;
11100 struct cleanup *back_to;
11101
11102 /* Make sure that we actually have parameters to parse. */
11103 if (arg != NULL && arg[0] != '\0')
11104 {
11105 const char *value_start;
11106
11107 exp_end = arg + strlen (arg);
11108
11109 /* Look for "parameter value" pairs at the end
11110 of the arguments string. */
11111 for (tok = exp_end - 1; tok > arg; tok--)
11112 {
11113 /* Skip whitespace at the end of the argument list. */
11114 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11115 tok--;
11116
11117 /* Find the beginning of the last token.
11118 This is the value of the parameter. */
11119 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11120 tok--;
11121 value_start = tok + 1;
11122
11123 /* Skip whitespace. */
11124 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11125 tok--;
11126
11127 end_tok = tok;
11128
11129 /* Find the beginning of the second to last token.
11130 This is the parameter itself. */
11131 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11132 tok--;
11133 tok++;
11134 toklen = end_tok - tok + 1;
11135
11136 if (toklen == 6 && startswith (tok, "thread"))
11137 {
11138 /* At this point we've found a "thread" token, which means
11139 the user is trying to set a watchpoint that triggers
11140 only in a specific thread. */
11141 char *endp;
11142
11143 if (thread != -1)
11144 error(_("You can specify only one thread."));
11145
11146 /* Extract the thread ID from the next token. */
11147 thread = strtol (value_start, &endp, 0);
11148
11149 /* Check if the user provided a valid numeric value for the
11150 thread ID. */
11151 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
11152 error (_("Invalid thread ID specification %s."), value_start);
11153
11154 /* Check if the thread actually exists. */
11155 if (!valid_thread_id (thread))
11156 invalid_thread_id_error (thread);
11157 }
11158 else if (toklen == 4 && startswith (tok, "mask"))
11159 {
11160 /* We've found a "mask" token, which means the user wants to
11161 create a hardware watchpoint that is going to have the mask
11162 facility. */
11163 struct value *mask_value, *mark;
11164
11165 if (use_mask)
11166 error(_("You can specify only one mask."));
11167
11168 use_mask = just_location = 1;
11169
11170 mark = value_mark ();
11171 mask_value = parse_to_comma_and_eval (&value_start);
11172 mask = value_as_address (mask_value);
11173 value_free_to_mark (mark);
11174 }
11175 else
11176 /* We didn't recognize what we found. We should stop here. */
11177 break;
11178
11179 /* Truncate the string and get rid of the "parameter value" pair before
11180 the arguments string is parsed by the parse_exp_1 function. */
11181 exp_end = tok;
11182 }
11183 }
11184 else
11185 exp_end = arg;
11186
11187 /* Parse the rest of the arguments. From here on out, everything
11188 is in terms of a newly allocated string instead of the original
11189 ARG. */
11190 innermost_block = NULL;
11191 expression = savestring (arg, exp_end - arg);
11192 back_to = make_cleanup (xfree, expression);
11193 exp_start = arg = expression;
11194 exp = parse_exp_1 (&arg, 0, 0, 0);
11195 exp_end = arg;
11196 /* Remove trailing whitespace from the expression before saving it.
11197 This makes the eventual display of the expression string a bit
11198 prettier. */
11199 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
11200 --exp_end;
11201
11202 /* Checking if the expression is not constant. */
11203 if (watchpoint_exp_is_const (exp))
11204 {
11205 int len;
11206
11207 len = exp_end - exp_start;
11208 while (len > 0 && isspace (exp_start[len - 1]))
11209 len--;
11210 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
11211 }
11212
11213 exp_valid_block = innermost_block;
11214 mark = value_mark ();
11215 fetch_subexp_value (exp, &pc, &val, &result, NULL, just_location);
11216
11217 if (val != NULL && just_location)
11218 {
11219 saved_bitpos = value_bitpos (val);
11220 saved_bitsize = value_bitsize (val);
11221 }
11222
11223 if (just_location)
11224 {
11225 int ret;
11226
11227 exp_valid_block = NULL;
11228 val = value_addr (result);
11229 release_value (val);
11230 value_free_to_mark (mark);
11231
11232 if (use_mask)
11233 {
11234 ret = target_masked_watch_num_registers (value_as_address (val),
11235 mask);
11236 if (ret == -1)
11237 error (_("This target does not support masked watchpoints."));
11238 else if (ret == -2)
11239 error (_("Invalid mask or memory region."));
11240 }
11241 }
11242 else if (val != NULL)
11243 release_value (val);
11244
11245 tok = skip_spaces_const (arg);
11246 end_tok = skip_to_space_const (tok);
11247
11248 toklen = end_tok - tok;
11249 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
11250 {
11251 struct expression *cond;
11252
11253 innermost_block = NULL;
11254 tok = cond_start = end_tok + 1;
11255 cond = parse_exp_1 (&tok, 0, 0, 0);
11256
11257 /* The watchpoint expression may not be local, but the condition
11258 may still be. E.g.: `watch global if local > 0'. */
11259 cond_exp_valid_block = innermost_block;
11260
11261 xfree (cond);
11262 cond_end = tok;
11263 }
11264 if (*tok)
11265 error (_("Junk at end of command."));
11266
11267 frame = block_innermost_frame (exp_valid_block);
11268
11269 /* If the expression is "local", then set up a "watchpoint scope"
11270 breakpoint at the point where we've left the scope of the watchpoint
11271 expression. Create the scope breakpoint before the watchpoint, so
11272 that we will encounter it first in bpstat_stop_status. */
11273 if (exp_valid_block && frame)
11274 {
11275 if (frame_id_p (frame_unwind_caller_id (frame)))
11276 {
11277 scope_breakpoint
11278 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
11279 frame_unwind_caller_pc (frame),
11280 bp_watchpoint_scope,
11281 &momentary_breakpoint_ops);
11282
11283 scope_breakpoint->enable_state = bp_enabled;
11284
11285 /* Automatically delete the breakpoint when it hits. */
11286 scope_breakpoint->disposition = disp_del;
11287
11288 /* Only break in the proper frame (help with recursion). */
11289 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
11290
11291 /* Set the address at which we will stop. */
11292 scope_breakpoint->loc->gdbarch
11293 = frame_unwind_caller_arch (frame);
11294 scope_breakpoint->loc->requested_address
11295 = frame_unwind_caller_pc (frame);
11296 scope_breakpoint->loc->address
11297 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
11298 scope_breakpoint->loc->requested_address,
11299 scope_breakpoint->type);
11300 }
11301 }
11302
11303 /* Now set up the breakpoint. We create all watchpoints as hardware
11304 watchpoints here even if hardware watchpoints are turned off, a call
11305 to update_watchpoint later in this function will cause the type to
11306 drop back to bp_watchpoint (software watchpoint) if required. */
11307
11308 if (accessflag == hw_read)
11309 bp_type = bp_read_watchpoint;
11310 else if (accessflag == hw_access)
11311 bp_type = bp_access_watchpoint;
11312 else
11313 bp_type = bp_hardware_watchpoint;
11314
11315 w = XCNEW (struct watchpoint);
11316 b = &w->base;
11317 if (use_mask)
11318 init_raw_breakpoint_without_location (b, NULL, bp_type,
11319 &masked_watchpoint_breakpoint_ops);
11320 else
11321 init_raw_breakpoint_without_location (b, NULL, bp_type,
11322 &watchpoint_breakpoint_ops);
11323 b->thread = thread;
11324 b->disposition = disp_donttouch;
11325 b->pspace = current_program_space;
11326 w->exp = exp;
11327 w->exp_valid_block = exp_valid_block;
11328 w->cond_exp_valid_block = cond_exp_valid_block;
11329 if (just_location)
11330 {
11331 struct type *t = value_type (val);
11332 CORE_ADDR addr = value_as_address (val);
11333 char *name;
11334
11335 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
11336 name = type_to_string (t);
11337
11338 w->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
11339 core_addr_to_string (addr));
11340 xfree (name);
11341
11342 w->exp_string = xstrprintf ("-location %.*s",
11343 (int) (exp_end - exp_start), exp_start);
11344
11345 /* The above expression is in C. */
11346 b->language = language_c;
11347 }
11348 else
11349 w->exp_string = savestring (exp_start, exp_end - exp_start);
11350
11351 if (use_mask)
11352 {
11353 w->hw_wp_mask = mask;
11354 }
11355 else
11356 {
11357 w->val = val;
11358 w->val_bitpos = saved_bitpos;
11359 w->val_bitsize = saved_bitsize;
11360 w->val_valid = 1;
11361 }
11362
11363 if (cond_start)
11364 b->cond_string = savestring (cond_start, cond_end - cond_start);
11365 else
11366 b->cond_string = 0;
11367
11368 if (frame)
11369 {
11370 w->watchpoint_frame = get_frame_id (frame);
11371 w->watchpoint_thread = inferior_ptid;
11372 }
11373 else
11374 {
11375 w->watchpoint_frame = null_frame_id;
11376 w->watchpoint_thread = null_ptid;
11377 }
11378
11379 if (scope_breakpoint != NULL)
11380 {
11381 /* The scope breakpoint is related to the watchpoint. We will
11382 need to act on them together. */
11383 b->related_breakpoint = scope_breakpoint;
11384 scope_breakpoint->related_breakpoint = b;
11385 }
11386
11387 if (!just_location)
11388 value_free_to_mark (mark);
11389
11390 TRY
11391 {
11392 /* Finally update the new watchpoint. This creates the locations
11393 that should be inserted. */
11394 update_watchpoint (w, 1);
11395 }
11396 CATCH (e, RETURN_MASK_ALL)
11397 {
11398 delete_breakpoint (b);
11399 throw_exception (e);
11400 }
11401 END_CATCH
11402
11403 install_breakpoint (internal, b, 1);
11404 do_cleanups (back_to);
11405 }
11406
11407 /* Return count of debug registers needed to watch the given expression.
11408 If the watchpoint cannot be handled in hardware return zero. */
11409
11410 static int
11411 can_use_hardware_watchpoint (struct value *v)
11412 {
11413 int found_memory_cnt = 0;
11414 struct value *head = v;
11415
11416 /* Did the user specifically forbid us to use hardware watchpoints? */
11417 if (!can_use_hw_watchpoints)
11418 return 0;
11419
11420 /* Make sure that the value of the expression depends only upon
11421 memory contents, and values computed from them within GDB. If we
11422 find any register references or function calls, we can't use a
11423 hardware watchpoint.
11424
11425 The idea here is that evaluating an expression generates a series
11426 of values, one holding the value of every subexpression. (The
11427 expression a*b+c has five subexpressions: a, b, a*b, c, and
11428 a*b+c.) GDB's values hold almost enough information to establish
11429 the criteria given above --- they identify memory lvalues,
11430 register lvalues, computed values, etcetera. So we can evaluate
11431 the expression, and then scan the chain of values that leaves
11432 behind to decide whether we can detect any possible change to the
11433 expression's final value using only hardware watchpoints.
11434
11435 However, I don't think that the values returned by inferior
11436 function calls are special in any way. So this function may not
11437 notice that an expression involving an inferior function call
11438 can't be watched with hardware watchpoints. FIXME. */
11439 for (; v; v = value_next (v))
11440 {
11441 if (VALUE_LVAL (v) == lval_memory)
11442 {
11443 if (v != head && value_lazy (v))
11444 /* A lazy memory lvalue in the chain is one that GDB never
11445 needed to fetch; we either just used its address (e.g.,
11446 `a' in `a.b') or we never needed it at all (e.g., `a'
11447 in `a,b'). This doesn't apply to HEAD; if that is
11448 lazy then it was not readable, but watch it anyway. */
11449 ;
11450 else
11451 {
11452 /* Ahh, memory we actually used! Check if we can cover
11453 it with hardware watchpoints. */
11454 struct type *vtype = check_typedef (value_type (v));
11455
11456 /* We only watch structs and arrays if user asked for it
11457 explicitly, never if they just happen to appear in a
11458 middle of some value chain. */
11459 if (v == head
11460 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11461 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11462 {
11463 CORE_ADDR vaddr = value_address (v);
11464 int len;
11465 int num_regs;
11466
11467 len = (target_exact_watchpoints
11468 && is_scalar_type_recursive (vtype))?
11469 1 : TYPE_LENGTH (value_type (v));
11470
11471 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11472 if (!num_regs)
11473 return 0;
11474 else
11475 found_memory_cnt += num_regs;
11476 }
11477 }
11478 }
11479 else if (VALUE_LVAL (v) != not_lval
11480 && deprecated_value_modifiable (v) == 0)
11481 return 0; /* These are values from the history (e.g., $1). */
11482 else if (VALUE_LVAL (v) == lval_register)
11483 return 0; /* Cannot watch a register with a HW watchpoint. */
11484 }
11485
11486 /* The expression itself looks suitable for using a hardware
11487 watchpoint, but give the target machine a chance to reject it. */
11488 return found_memory_cnt;
11489 }
11490
11491 void
11492 watch_command_wrapper (char *arg, int from_tty, int internal)
11493 {
11494 watch_command_1 (arg, hw_write, from_tty, 0, internal);
11495 }
11496
11497 /* A helper function that looks for the "-location" argument and then
11498 calls watch_command_1. */
11499
11500 static void
11501 watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11502 {
11503 int just_location = 0;
11504
11505 if (arg
11506 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11507 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11508 {
11509 arg = skip_spaces (arg);
11510 just_location = 1;
11511 }
11512
11513 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
11514 }
11515
11516 static void
11517 watch_command (char *arg, int from_tty)
11518 {
11519 watch_maybe_just_location (arg, hw_write, from_tty);
11520 }
11521
11522 void
11523 rwatch_command_wrapper (char *arg, int from_tty, int internal)
11524 {
11525 watch_command_1 (arg, hw_read, from_tty, 0, internal);
11526 }
11527
11528 static void
11529 rwatch_command (char *arg, int from_tty)
11530 {
11531 watch_maybe_just_location (arg, hw_read, from_tty);
11532 }
11533
11534 void
11535 awatch_command_wrapper (char *arg, int from_tty, int internal)
11536 {
11537 watch_command_1 (arg, hw_access, from_tty, 0, internal);
11538 }
11539
11540 static void
11541 awatch_command (char *arg, int from_tty)
11542 {
11543 watch_maybe_just_location (arg, hw_access, from_tty);
11544 }
11545 \f
11546
11547 /* Data for the FSM that manages the until(location)/advance commands
11548 in infcmd.c. Here because it uses the mechanisms of
11549 breakpoints. */
11550
11551 struct until_break_fsm
11552 {
11553 /* The base class. */
11554 struct thread_fsm thread_fsm;
11555
11556 /* The thread that as current when the command was executed. */
11557 int thread;
11558
11559 /* The breakpoint set at the destination location. */
11560 struct breakpoint *location_breakpoint;
11561
11562 /* Breakpoint set at the return address in the caller frame. May be
11563 NULL. */
11564 struct breakpoint *caller_breakpoint;
11565 };
11566
11567 static void until_break_fsm_clean_up (struct thread_fsm *self);
11568 static int until_break_fsm_should_stop (struct thread_fsm *self);
11569 static enum async_reply_reason
11570 until_break_fsm_async_reply_reason (struct thread_fsm *self);
11571
11572 /* until_break_fsm's vtable. */
11573
11574 static struct thread_fsm_ops until_break_fsm_ops =
11575 {
11576 NULL, /* dtor */
11577 until_break_fsm_clean_up,
11578 until_break_fsm_should_stop,
11579 NULL, /* return_value */
11580 until_break_fsm_async_reply_reason,
11581 };
11582
11583 /* Allocate a new until_break_command_fsm. */
11584
11585 static struct until_break_fsm *
11586 new_until_break_fsm (int thread,
11587 struct breakpoint *location_breakpoint,
11588 struct breakpoint *caller_breakpoint)
11589 {
11590 struct until_break_fsm *sm;
11591
11592 sm = XCNEW (struct until_break_fsm);
11593 thread_fsm_ctor (&sm->thread_fsm, &until_break_fsm_ops);
11594
11595 sm->thread = thread;
11596 sm->location_breakpoint = location_breakpoint;
11597 sm->caller_breakpoint = caller_breakpoint;
11598
11599 return sm;
11600 }
11601
11602 /* Implementation of the 'should_stop' FSM method for the
11603 until(location)/advance commands. */
11604
11605 static int
11606 until_break_fsm_should_stop (struct thread_fsm *self)
11607 {
11608 struct until_break_fsm *sm = (struct until_break_fsm *) self;
11609 struct thread_info *tp = inferior_thread ();
11610
11611 if (bpstat_find_breakpoint (tp->control.stop_bpstat,
11612 sm->location_breakpoint) != NULL
11613 || (sm->caller_breakpoint != NULL
11614 && bpstat_find_breakpoint (tp->control.stop_bpstat,
11615 sm->caller_breakpoint) != NULL))
11616 thread_fsm_set_finished (self);
11617
11618 return 1;
11619 }
11620
11621 /* Implementation of the 'clean_up' FSM method for the
11622 until(location)/advance commands. */
11623
11624 static void
11625 until_break_fsm_clean_up (struct thread_fsm *self)
11626 {
11627 struct until_break_fsm *sm = (struct until_break_fsm *) self;
11628
11629 /* Clean up our temporary breakpoints. */
11630 if (sm->location_breakpoint != NULL)
11631 {
11632 delete_breakpoint (sm->location_breakpoint);
11633 sm->location_breakpoint = NULL;
11634 }
11635 if (sm->caller_breakpoint != NULL)
11636 {
11637 delete_breakpoint (sm->caller_breakpoint);
11638 sm->caller_breakpoint = NULL;
11639 }
11640 delete_longjmp_breakpoint (sm->thread);
11641 }
11642
11643 /* Implementation of the 'async_reply_reason' FSM method for the
11644 until(location)/advance commands. */
11645
11646 static enum async_reply_reason
11647 until_break_fsm_async_reply_reason (struct thread_fsm *self)
11648 {
11649 return EXEC_ASYNC_LOCATION_REACHED;
11650 }
11651
11652 void
11653 until_break_command (char *arg, int from_tty, int anywhere)
11654 {
11655 struct symtabs_and_lines sals;
11656 struct symtab_and_line sal;
11657 struct frame_info *frame;
11658 struct gdbarch *frame_gdbarch;
11659 struct frame_id stack_frame_id;
11660 struct frame_id caller_frame_id;
11661 struct breakpoint *location_breakpoint;
11662 struct breakpoint *caller_breakpoint = NULL;
11663 struct cleanup *old_chain, *cleanup;
11664 int thread;
11665 struct thread_info *tp;
11666 struct event_location *location;
11667 struct until_break_fsm *sm;
11668
11669 clear_proceed_status (0);
11670
11671 /* Set a breakpoint where the user wants it and at return from
11672 this function. */
11673
11674 location = string_to_event_location (&arg, current_language);
11675 cleanup = make_cleanup_delete_event_location (location);
11676
11677 if (last_displayed_sal_is_valid ())
11678 sals = decode_line_1 (location, DECODE_LINE_FUNFIRSTLINE,
11679 get_last_displayed_symtab (),
11680 get_last_displayed_line ());
11681 else
11682 sals = decode_line_1 (location, DECODE_LINE_FUNFIRSTLINE,
11683 (struct symtab *) NULL, 0);
11684
11685 if (sals.nelts != 1)
11686 error (_("Couldn't get information on specified line."));
11687
11688 sal = sals.sals[0];
11689 xfree (sals.sals); /* malloc'd, so freed. */
11690
11691 if (*arg)
11692 error (_("Junk at end of arguments."));
11693
11694 resolve_sal_pc (&sal);
11695
11696 tp = inferior_thread ();
11697 thread = tp->num;
11698
11699 old_chain = make_cleanup (null_cleanup, NULL);
11700
11701 /* Note linespec handling above invalidates the frame chain.
11702 Installing a breakpoint also invalidates the frame chain (as it
11703 may need to switch threads), so do any frame handling before
11704 that. */
11705
11706 frame = get_selected_frame (NULL);
11707 frame_gdbarch = get_frame_arch (frame);
11708 stack_frame_id = get_stack_frame_id (frame);
11709 caller_frame_id = frame_unwind_caller_id (frame);
11710
11711 /* Keep within the current frame, or in frames called by the current
11712 one. */
11713
11714 if (frame_id_p (caller_frame_id))
11715 {
11716 struct symtab_and_line sal2;
11717 struct gdbarch *caller_gdbarch;
11718
11719 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11720 sal2.pc = frame_unwind_caller_pc (frame);
11721 caller_gdbarch = frame_unwind_caller_arch (frame);
11722 caller_breakpoint = set_momentary_breakpoint (caller_gdbarch,
11723 sal2,
11724 caller_frame_id,
11725 bp_until);
11726 make_cleanup_delete_breakpoint (caller_breakpoint);
11727
11728 set_longjmp_breakpoint (tp, caller_frame_id);
11729 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
11730 }
11731
11732 /* set_momentary_breakpoint could invalidate FRAME. */
11733 frame = NULL;
11734
11735 if (anywhere)
11736 /* If the user told us to continue until a specified location,
11737 we don't specify a frame at which we need to stop. */
11738 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11739 null_frame_id, bp_until);
11740 else
11741 /* Otherwise, specify the selected frame, because we want to stop
11742 only at the very same frame. */
11743 location_breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11744 stack_frame_id, bp_until);
11745 make_cleanup_delete_breakpoint (location_breakpoint);
11746
11747 sm = new_until_break_fsm (tp->num, location_breakpoint, caller_breakpoint);
11748 tp->thread_fsm = &sm->thread_fsm;
11749
11750 discard_cleanups (old_chain);
11751
11752 proceed (-1, GDB_SIGNAL_DEFAULT);
11753
11754 do_cleanups (cleanup);
11755 }
11756
11757 /* This function attempts to parse an optional "if <cond>" clause
11758 from the arg string. If one is not found, it returns NULL.
11759
11760 Else, it returns a pointer to the condition string. (It does not
11761 attempt to evaluate the string against a particular block.) And,
11762 it updates arg to point to the first character following the parsed
11763 if clause in the arg string. */
11764
11765 char *
11766 ep_parse_optional_if_clause (char **arg)
11767 {
11768 char *cond_string;
11769
11770 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
11771 return NULL;
11772
11773 /* Skip the "if" keyword. */
11774 (*arg) += 2;
11775
11776 /* Skip any extra leading whitespace, and record the start of the
11777 condition string. */
11778 *arg = skip_spaces (*arg);
11779 cond_string = *arg;
11780
11781 /* Assume that the condition occupies the remainder of the arg
11782 string. */
11783 (*arg) += strlen (cond_string);
11784
11785 return cond_string;
11786 }
11787
11788 /* Commands to deal with catching events, such as signals, exceptions,
11789 process start/exit, etc. */
11790
11791 typedef enum
11792 {
11793 catch_fork_temporary, catch_vfork_temporary,
11794 catch_fork_permanent, catch_vfork_permanent
11795 }
11796 catch_fork_kind;
11797
11798 static void
11799 catch_fork_command_1 (char *arg, int from_tty,
11800 struct cmd_list_element *command)
11801 {
11802 struct gdbarch *gdbarch = get_current_arch ();
11803 char *cond_string = NULL;
11804 catch_fork_kind fork_kind;
11805 int tempflag;
11806
11807 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11808 tempflag = (fork_kind == catch_fork_temporary
11809 || fork_kind == catch_vfork_temporary);
11810
11811 if (!arg)
11812 arg = "";
11813 arg = skip_spaces (arg);
11814
11815 /* The allowed syntax is:
11816 catch [v]fork
11817 catch [v]fork if <cond>
11818
11819 First, check if there's an if clause. */
11820 cond_string = ep_parse_optional_if_clause (&arg);
11821
11822 if ((*arg != '\0') && !isspace (*arg))
11823 error (_("Junk at end of arguments."));
11824
11825 /* If this target supports it, create a fork or vfork catchpoint
11826 and enable reporting of such events. */
11827 switch (fork_kind)
11828 {
11829 case catch_fork_temporary:
11830 case catch_fork_permanent:
11831 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11832 &catch_fork_breakpoint_ops);
11833 break;
11834 case catch_vfork_temporary:
11835 case catch_vfork_permanent:
11836 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11837 &catch_vfork_breakpoint_ops);
11838 break;
11839 default:
11840 error (_("unsupported or unknown fork kind; cannot catch it"));
11841 break;
11842 }
11843 }
11844
11845 static void
11846 catch_exec_command_1 (char *arg, int from_tty,
11847 struct cmd_list_element *command)
11848 {
11849 struct exec_catchpoint *c;
11850 struct gdbarch *gdbarch = get_current_arch ();
11851 int tempflag;
11852 char *cond_string = NULL;
11853
11854 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11855
11856 if (!arg)
11857 arg = "";
11858 arg = skip_spaces (arg);
11859
11860 /* The allowed syntax is:
11861 catch exec
11862 catch exec if <cond>
11863
11864 First, check if there's an if clause. */
11865 cond_string = ep_parse_optional_if_clause (&arg);
11866
11867 if ((*arg != '\0') && !isspace (*arg))
11868 error (_("Junk at end of arguments."));
11869
11870 c = XNEW (struct exec_catchpoint);
11871 init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
11872 &catch_exec_breakpoint_ops);
11873 c->exec_pathname = NULL;
11874
11875 install_breakpoint (0, &c->base, 1);
11876 }
11877
11878 void
11879 init_ada_exception_breakpoint (struct breakpoint *b,
11880 struct gdbarch *gdbarch,
11881 struct symtab_and_line sal,
11882 char *addr_string,
11883 const struct breakpoint_ops *ops,
11884 int tempflag,
11885 int enabled,
11886 int from_tty)
11887 {
11888 if (from_tty)
11889 {
11890 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11891 if (!loc_gdbarch)
11892 loc_gdbarch = gdbarch;
11893
11894 describe_other_breakpoints (loc_gdbarch,
11895 sal.pspace, sal.pc, sal.section, -1);
11896 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11897 version for exception catchpoints, because two catchpoints
11898 used for different exception names will use the same address.
11899 In this case, a "breakpoint ... also set at..." warning is
11900 unproductive. Besides, the warning phrasing is also a bit
11901 inappropriate, we should use the word catchpoint, and tell
11902 the user what type of catchpoint it is. The above is good
11903 enough for now, though. */
11904 }
11905
11906 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
11907
11908 b->enable_state = enabled ? bp_enabled : bp_disabled;
11909 b->disposition = tempflag ? disp_del : disp_donttouch;
11910 b->location = string_to_event_location (&addr_string,
11911 language_def (language_ada));
11912 b->language = language_ada;
11913 }
11914
11915 static void
11916 catch_command (char *arg, int from_tty)
11917 {
11918 error (_("Catch requires an event name."));
11919 }
11920 \f
11921
11922 static void
11923 tcatch_command (char *arg, int from_tty)
11924 {
11925 error (_("Catch requires an event name."));
11926 }
11927
11928 /* A qsort comparison function that sorts breakpoints in order. */
11929
11930 static int
11931 compare_breakpoints (const void *a, const void *b)
11932 {
11933 const breakpoint_p *ba = (const breakpoint_p *) a;
11934 uintptr_t ua = (uintptr_t) *ba;
11935 const breakpoint_p *bb = (const breakpoint_p *) b;
11936 uintptr_t ub = (uintptr_t) *bb;
11937
11938 if ((*ba)->number < (*bb)->number)
11939 return -1;
11940 else if ((*ba)->number > (*bb)->number)
11941 return 1;
11942
11943 /* Now sort by address, in case we see, e..g, two breakpoints with
11944 the number 0. */
11945 if (ua < ub)
11946 return -1;
11947 return ua > ub ? 1 : 0;
11948 }
11949
11950 /* Delete breakpoints by address or line. */
11951
11952 static void
11953 clear_command (char *arg, int from_tty)
11954 {
11955 struct breakpoint *b, *prev;
11956 VEC(breakpoint_p) *found = 0;
11957 int ix;
11958 int default_match;
11959 struct symtabs_and_lines sals;
11960 struct symtab_and_line sal;
11961 int i;
11962 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
11963
11964 if (arg)
11965 {
11966 sals = decode_line_with_current_source (arg,
11967 (DECODE_LINE_FUNFIRSTLINE
11968 | DECODE_LINE_LIST_MODE));
11969 make_cleanup (xfree, sals.sals);
11970 default_match = 0;
11971 }
11972 else
11973 {
11974 sals.sals = XNEW (struct symtab_and_line);
11975 make_cleanup (xfree, sals.sals);
11976 init_sal (&sal); /* Initialize to zeroes. */
11977
11978 /* Set sal's line, symtab, pc, and pspace to the values
11979 corresponding to the last call to print_frame_info. If the
11980 codepoint is not valid, this will set all the fields to 0. */
11981 get_last_displayed_sal (&sal);
11982 if (sal.symtab == 0)
11983 error (_("No source file specified."));
11984
11985 sals.sals[0] = sal;
11986 sals.nelts = 1;
11987
11988 default_match = 1;
11989 }
11990
11991 /* We don't call resolve_sal_pc here. That's not as bad as it
11992 seems, because all existing breakpoints typically have both
11993 file/line and pc set. So, if clear is given file/line, we can
11994 match this to existing breakpoint without obtaining pc at all.
11995
11996 We only support clearing given the address explicitly
11997 present in breakpoint table. Say, we've set breakpoint
11998 at file:line. There were several PC values for that file:line,
11999 due to optimization, all in one block.
12000
12001 We've picked one PC value. If "clear" is issued with another
12002 PC corresponding to the same file:line, the breakpoint won't
12003 be cleared. We probably can still clear the breakpoint, but
12004 since the other PC value is never presented to user, user
12005 can only find it by guessing, and it does not seem important
12006 to support that. */
12007
12008 /* For each line spec given, delete bps which correspond to it. Do
12009 it in two passes, solely to preserve the current behavior that
12010 from_tty is forced true if we delete more than one
12011 breakpoint. */
12012
12013 found = NULL;
12014 make_cleanup (VEC_cleanup (breakpoint_p), &found);
12015 for (i = 0; i < sals.nelts; i++)
12016 {
12017 const char *sal_fullname;
12018
12019 /* If exact pc given, clear bpts at that pc.
12020 If line given (pc == 0), clear all bpts on specified line.
12021 If defaulting, clear all bpts on default line
12022 or at default pc.
12023
12024 defaulting sal.pc != 0 tests to do
12025
12026 0 1 pc
12027 1 1 pc _and_ line
12028 0 0 line
12029 1 0 <can't happen> */
12030
12031 sal = sals.sals[i];
12032 sal_fullname = (sal.symtab == NULL
12033 ? NULL : symtab_to_fullname (sal.symtab));
12034
12035 /* Find all matching breakpoints and add them to 'found'. */
12036 ALL_BREAKPOINTS (b)
12037 {
12038 int match = 0;
12039 /* Are we going to delete b? */
12040 if (b->type != bp_none && !is_watchpoint (b))
12041 {
12042 struct bp_location *loc = b->loc;
12043 for (; loc; loc = loc->next)
12044 {
12045 /* If the user specified file:line, don't allow a PC
12046 match. This matches historical gdb behavior. */
12047 int pc_match = (!sal.explicit_line
12048 && sal.pc
12049 && (loc->pspace == sal.pspace)
12050 && (loc->address == sal.pc)
12051 && (!section_is_overlay (loc->section)
12052 || loc->section == sal.section));
12053 int line_match = 0;
12054
12055 if ((default_match || sal.explicit_line)
12056 && loc->symtab != NULL
12057 && sal_fullname != NULL
12058 && sal.pspace == loc->pspace
12059 && loc->line_number == sal.line
12060 && filename_cmp (symtab_to_fullname (loc->symtab),
12061 sal_fullname) == 0)
12062 line_match = 1;
12063
12064 if (pc_match || line_match)
12065 {
12066 match = 1;
12067 break;
12068 }
12069 }
12070 }
12071
12072 if (match)
12073 VEC_safe_push(breakpoint_p, found, b);
12074 }
12075 }
12076
12077 /* Now go thru the 'found' chain and delete them. */
12078 if (VEC_empty(breakpoint_p, found))
12079 {
12080 if (arg)
12081 error (_("No breakpoint at %s."), arg);
12082 else
12083 error (_("No breakpoint at this line."));
12084 }
12085
12086 /* Remove duplicates from the vec. */
12087 qsort (VEC_address (breakpoint_p, found),
12088 VEC_length (breakpoint_p, found),
12089 sizeof (breakpoint_p),
12090 compare_breakpoints);
12091 prev = VEC_index (breakpoint_p, found, 0);
12092 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
12093 {
12094 if (b == prev)
12095 {
12096 VEC_ordered_remove (breakpoint_p, found, ix);
12097 --ix;
12098 }
12099 }
12100
12101 if (VEC_length(breakpoint_p, found) > 1)
12102 from_tty = 1; /* Always report if deleted more than one. */
12103 if (from_tty)
12104 {
12105 if (VEC_length(breakpoint_p, found) == 1)
12106 printf_unfiltered (_("Deleted breakpoint "));
12107 else
12108 printf_unfiltered (_("Deleted breakpoints "));
12109 }
12110
12111 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
12112 {
12113 if (from_tty)
12114 printf_unfiltered ("%d ", b->number);
12115 delete_breakpoint (b);
12116 }
12117 if (from_tty)
12118 putchar_unfiltered ('\n');
12119
12120 do_cleanups (cleanups);
12121 }
12122 \f
12123 /* Delete breakpoint in BS if they are `delete' breakpoints and
12124 all breakpoints that are marked for deletion, whether hit or not.
12125 This is called after any breakpoint is hit, or after errors. */
12126
12127 void
12128 breakpoint_auto_delete (bpstat bs)
12129 {
12130 struct breakpoint *b, *b_tmp;
12131
12132 for (; bs; bs = bs->next)
12133 if (bs->breakpoint_at
12134 && bs->breakpoint_at->disposition == disp_del
12135 && bs->stop)
12136 delete_breakpoint (bs->breakpoint_at);
12137
12138 ALL_BREAKPOINTS_SAFE (b, b_tmp)
12139 {
12140 if (b->disposition == disp_del_at_next_stop)
12141 delete_breakpoint (b);
12142 }
12143 }
12144
12145 /* A comparison function for bp_location AP and BP being interfaced to
12146 qsort. Sort elements primarily by their ADDRESS (no matter what
12147 does breakpoint_address_is_meaningful say for its OWNER),
12148 secondarily by ordering first permanent elements and
12149 terciarily just ensuring the array is sorted stable way despite
12150 qsort being an unstable algorithm. */
12151
12152 static int
12153 bp_location_compare (const void *ap, const void *bp)
12154 {
12155 const struct bp_location *a = *(const struct bp_location **) ap;
12156 const struct bp_location *b = *(const struct bp_location **) bp;
12157
12158 if (a->address != b->address)
12159 return (a->address > b->address) - (a->address < b->address);
12160
12161 /* Sort locations at the same address by their pspace number, keeping
12162 locations of the same inferior (in a multi-inferior environment)
12163 grouped. */
12164
12165 if (a->pspace->num != b->pspace->num)
12166 return ((a->pspace->num > b->pspace->num)
12167 - (a->pspace->num < b->pspace->num));
12168
12169 /* Sort permanent breakpoints first. */
12170 if (a->permanent != b->permanent)
12171 return (a->permanent < b->permanent) - (a->permanent > b->permanent);
12172
12173 /* Make the internal GDB representation stable across GDB runs
12174 where A and B memory inside GDB can differ. Breakpoint locations of
12175 the same type at the same address can be sorted in arbitrary order. */
12176
12177 if (a->owner->number != b->owner->number)
12178 return ((a->owner->number > b->owner->number)
12179 - (a->owner->number < b->owner->number));
12180
12181 return (a > b) - (a < b);
12182 }
12183
12184 /* Set bp_location_placed_address_before_address_max and
12185 bp_location_shadow_len_after_address_max according to the current
12186 content of the bp_location array. */
12187
12188 static void
12189 bp_location_target_extensions_update (void)
12190 {
12191 struct bp_location *bl, **blp_tmp;
12192
12193 bp_location_placed_address_before_address_max = 0;
12194 bp_location_shadow_len_after_address_max = 0;
12195
12196 ALL_BP_LOCATIONS (bl, blp_tmp)
12197 {
12198 CORE_ADDR start, end, addr;
12199
12200 if (!bp_location_has_shadow (bl))
12201 continue;
12202
12203 start = bl->target_info.placed_address;
12204 end = start + bl->target_info.shadow_len;
12205
12206 gdb_assert (bl->address >= start);
12207 addr = bl->address - start;
12208 if (addr > bp_location_placed_address_before_address_max)
12209 bp_location_placed_address_before_address_max = addr;
12210
12211 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
12212
12213 gdb_assert (bl->address < end);
12214 addr = end - bl->address;
12215 if (addr > bp_location_shadow_len_after_address_max)
12216 bp_location_shadow_len_after_address_max = addr;
12217 }
12218 }
12219
12220 /* Download tracepoint locations if they haven't been. */
12221
12222 static void
12223 download_tracepoint_locations (void)
12224 {
12225 struct breakpoint *b;
12226 struct cleanup *old_chain;
12227 enum tribool can_download_tracepoint = TRIBOOL_UNKNOWN;
12228
12229 old_chain = save_current_space_and_thread ();
12230
12231 ALL_TRACEPOINTS (b)
12232 {
12233 struct bp_location *bl;
12234 struct tracepoint *t;
12235 int bp_location_downloaded = 0;
12236
12237 if ((b->type == bp_fast_tracepoint
12238 ? !may_insert_fast_tracepoints
12239 : !may_insert_tracepoints))
12240 continue;
12241
12242 if (can_download_tracepoint == TRIBOOL_UNKNOWN)
12243 {
12244 if (target_can_download_tracepoint ())
12245 can_download_tracepoint = TRIBOOL_TRUE;
12246 else
12247 can_download_tracepoint = TRIBOOL_FALSE;
12248 }
12249
12250 if (can_download_tracepoint == TRIBOOL_FALSE)
12251 break;
12252
12253 for (bl = b->loc; bl; bl = bl->next)
12254 {
12255 /* In tracepoint, locations are _never_ duplicated, so
12256 should_be_inserted is equivalent to
12257 unduplicated_should_be_inserted. */
12258 if (!should_be_inserted (bl) || bl->inserted)
12259 continue;
12260
12261 switch_to_program_space_and_thread (bl->pspace);
12262
12263 target_download_tracepoint (bl);
12264
12265 bl->inserted = 1;
12266 bp_location_downloaded = 1;
12267 }
12268 t = (struct tracepoint *) b;
12269 t->number_on_target = b->number;
12270 if (bp_location_downloaded)
12271 observer_notify_breakpoint_modified (b);
12272 }
12273
12274 do_cleanups (old_chain);
12275 }
12276
12277 /* Swap the insertion/duplication state between two locations. */
12278
12279 static void
12280 swap_insertion (struct bp_location *left, struct bp_location *right)
12281 {
12282 const int left_inserted = left->inserted;
12283 const int left_duplicate = left->duplicate;
12284 const int left_needs_update = left->needs_update;
12285 const struct bp_target_info left_target_info = left->target_info;
12286
12287 /* Locations of tracepoints can never be duplicated. */
12288 if (is_tracepoint (left->owner))
12289 gdb_assert (!left->duplicate);
12290 if (is_tracepoint (right->owner))
12291 gdb_assert (!right->duplicate);
12292
12293 left->inserted = right->inserted;
12294 left->duplicate = right->duplicate;
12295 left->needs_update = right->needs_update;
12296 left->target_info = right->target_info;
12297 right->inserted = left_inserted;
12298 right->duplicate = left_duplicate;
12299 right->needs_update = left_needs_update;
12300 right->target_info = left_target_info;
12301 }
12302
12303 /* Force the re-insertion of the locations at ADDRESS. This is called
12304 once a new/deleted/modified duplicate location is found and we are evaluating
12305 conditions on the target's side. Such conditions need to be updated on
12306 the target. */
12307
12308 static void
12309 force_breakpoint_reinsertion (struct bp_location *bl)
12310 {
12311 struct bp_location **locp = NULL, **loc2p;
12312 struct bp_location *loc;
12313 CORE_ADDR address = 0;
12314 int pspace_num;
12315
12316 address = bl->address;
12317 pspace_num = bl->pspace->num;
12318
12319 /* This is only meaningful if the target is
12320 evaluating conditions and if the user has
12321 opted for condition evaluation on the target's
12322 side. */
12323 if (gdb_evaluates_breakpoint_condition_p ()
12324 || !target_supports_evaluation_of_breakpoint_conditions ())
12325 return;
12326
12327 /* Flag all breakpoint locations with this address and
12328 the same program space as the location
12329 as "its condition has changed". We need to
12330 update the conditions on the target's side. */
12331 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
12332 {
12333 loc = *loc2p;
12334
12335 if (!is_breakpoint (loc->owner)
12336 || pspace_num != loc->pspace->num)
12337 continue;
12338
12339 /* Flag the location appropriately. We use a different state to
12340 let everyone know that we already updated the set of locations
12341 with addr bl->address and program space bl->pspace. This is so
12342 we don't have to keep calling these functions just to mark locations
12343 that have already been marked. */
12344 loc->condition_changed = condition_updated;
12345
12346 /* Free the agent expression bytecode as well. We will compute
12347 it later on. */
12348 if (loc->cond_bytecode)
12349 {
12350 free_agent_expr (loc->cond_bytecode);
12351 loc->cond_bytecode = NULL;
12352 }
12353 }
12354 }
12355 /* Called whether new breakpoints are created, or existing breakpoints
12356 deleted, to update the global location list and recompute which
12357 locations are duplicate of which.
12358
12359 The INSERT_MODE flag determines whether locations may not, may, or
12360 shall be inserted now. See 'enum ugll_insert_mode' for more
12361 info. */
12362
12363 static void
12364 update_global_location_list (enum ugll_insert_mode insert_mode)
12365 {
12366 struct breakpoint *b;
12367 struct bp_location **locp, *loc;
12368 struct cleanup *cleanups;
12369 /* Last breakpoint location address that was marked for update. */
12370 CORE_ADDR last_addr = 0;
12371 /* Last breakpoint location program space that was marked for update. */
12372 int last_pspace_num = -1;
12373
12374 /* Used in the duplicates detection below. When iterating over all
12375 bp_locations, points to the first bp_location of a given address.
12376 Breakpoints and watchpoints of different types are never
12377 duplicates of each other. Keep one pointer for each type of
12378 breakpoint/watchpoint, so we only need to loop over all locations
12379 once. */
12380 struct bp_location *bp_loc_first; /* breakpoint */
12381 struct bp_location *wp_loc_first; /* hardware watchpoint */
12382 struct bp_location *awp_loc_first; /* access watchpoint */
12383 struct bp_location *rwp_loc_first; /* read watchpoint */
12384
12385 /* Saved former bp_location array which we compare against the newly
12386 built bp_location from the current state of ALL_BREAKPOINTS. */
12387 struct bp_location **old_location, **old_locp;
12388 unsigned old_location_count;
12389
12390 old_location = bp_location;
12391 old_location_count = bp_location_count;
12392 bp_location = NULL;
12393 bp_location_count = 0;
12394 cleanups = make_cleanup (xfree, old_location);
12395
12396 ALL_BREAKPOINTS (b)
12397 for (loc = b->loc; loc; loc = loc->next)
12398 bp_location_count++;
12399
12400 bp_location = XNEWVEC (struct bp_location *, bp_location_count);
12401 locp = bp_location;
12402 ALL_BREAKPOINTS (b)
12403 for (loc = b->loc; loc; loc = loc->next)
12404 *locp++ = loc;
12405 qsort (bp_location, bp_location_count, sizeof (*bp_location),
12406 bp_location_compare);
12407
12408 bp_location_target_extensions_update ();
12409
12410 /* Identify bp_location instances that are no longer present in the
12411 new list, and therefore should be freed. Note that it's not
12412 necessary that those locations should be removed from inferior --
12413 if there's another location at the same address (previously
12414 marked as duplicate), we don't need to remove/insert the
12415 location.
12416
12417 LOCP is kept in sync with OLD_LOCP, each pointing to the current
12418 and former bp_location array state respectively. */
12419
12420 locp = bp_location;
12421 for (old_locp = old_location; old_locp < old_location + old_location_count;
12422 old_locp++)
12423 {
12424 struct bp_location *old_loc = *old_locp;
12425 struct bp_location **loc2p;
12426
12427 /* Tells if 'old_loc' is found among the new locations. If
12428 not, we have to free it. */
12429 int found_object = 0;
12430 /* Tells if the location should remain inserted in the target. */
12431 int keep_in_target = 0;
12432 int removed = 0;
12433
12434 /* Skip LOCP entries which will definitely never be needed.
12435 Stop either at or being the one matching OLD_LOC. */
12436 while (locp < bp_location + bp_location_count
12437 && (*locp)->address < old_loc->address)
12438 locp++;
12439
12440 for (loc2p = locp;
12441 (loc2p < bp_location + bp_location_count
12442 && (*loc2p)->address == old_loc->address);
12443 loc2p++)
12444 {
12445 /* Check if this is a new/duplicated location or a duplicated
12446 location that had its condition modified. If so, we want to send
12447 its condition to the target if evaluation of conditions is taking
12448 place there. */
12449 if ((*loc2p)->condition_changed == condition_modified
12450 && (last_addr != old_loc->address
12451 || last_pspace_num != old_loc->pspace->num))
12452 {
12453 force_breakpoint_reinsertion (*loc2p);
12454 last_pspace_num = old_loc->pspace->num;
12455 }
12456
12457 if (*loc2p == old_loc)
12458 found_object = 1;
12459 }
12460
12461 /* We have already handled this address, update it so that we don't
12462 have to go through updates again. */
12463 last_addr = old_loc->address;
12464
12465 /* Target-side condition evaluation: Handle deleted locations. */
12466 if (!found_object)
12467 force_breakpoint_reinsertion (old_loc);
12468
12469 /* If this location is no longer present, and inserted, look if
12470 there's maybe a new location at the same address. If so,
12471 mark that one inserted, and don't remove this one. This is
12472 needed so that we don't have a time window where a breakpoint
12473 at certain location is not inserted. */
12474
12475 if (old_loc->inserted)
12476 {
12477 /* If the location is inserted now, we might have to remove
12478 it. */
12479
12480 if (found_object && should_be_inserted (old_loc))
12481 {
12482 /* The location is still present in the location list,
12483 and still should be inserted. Don't do anything. */
12484 keep_in_target = 1;
12485 }
12486 else
12487 {
12488 /* This location still exists, but it won't be kept in the
12489 target since it may have been disabled. We proceed to
12490 remove its target-side condition. */
12491
12492 /* The location is either no longer present, or got
12493 disabled. See if there's another location at the
12494 same address, in which case we don't need to remove
12495 this one from the target. */
12496
12497 /* OLD_LOC comes from existing struct breakpoint. */
12498 if (breakpoint_address_is_meaningful (old_loc->owner))
12499 {
12500 for (loc2p = locp;
12501 (loc2p < bp_location + bp_location_count
12502 && (*loc2p)->address == old_loc->address);
12503 loc2p++)
12504 {
12505 struct bp_location *loc2 = *loc2p;
12506
12507 if (breakpoint_locations_match (loc2, old_loc))
12508 {
12509 /* Read watchpoint locations are switched to
12510 access watchpoints, if the former are not
12511 supported, but the latter are. */
12512 if (is_hardware_watchpoint (old_loc->owner))
12513 {
12514 gdb_assert (is_hardware_watchpoint (loc2->owner));
12515 loc2->watchpoint_type = old_loc->watchpoint_type;
12516 }
12517
12518 /* loc2 is a duplicated location. We need to check
12519 if it should be inserted in case it will be
12520 unduplicated. */
12521 if (loc2 != old_loc
12522 && unduplicated_should_be_inserted (loc2))
12523 {
12524 swap_insertion (old_loc, loc2);
12525 keep_in_target = 1;
12526 break;
12527 }
12528 }
12529 }
12530 }
12531 }
12532
12533 if (!keep_in_target)
12534 {
12535 if (remove_breakpoint (old_loc, mark_uninserted))
12536 {
12537 /* This is just about all we can do. We could keep
12538 this location on the global list, and try to
12539 remove it next time, but there's no particular
12540 reason why we will succeed next time.
12541
12542 Note that at this point, old_loc->owner is still
12543 valid, as delete_breakpoint frees the breakpoint
12544 only after calling us. */
12545 printf_filtered (_("warning: Error removing "
12546 "breakpoint %d\n"),
12547 old_loc->owner->number);
12548 }
12549 removed = 1;
12550 }
12551 }
12552
12553 if (!found_object)
12554 {
12555 if (removed && target_is_non_stop_p ()
12556 && need_moribund_for_location_type (old_loc))
12557 {
12558 /* This location was removed from the target. In
12559 non-stop mode, a race condition is possible where
12560 we've removed a breakpoint, but stop events for that
12561 breakpoint are already queued and will arrive later.
12562 We apply an heuristic to be able to distinguish such
12563 SIGTRAPs from other random SIGTRAPs: we keep this
12564 breakpoint location for a bit, and will retire it
12565 after we see some number of events. The theory here
12566 is that reporting of events should, "on the average",
12567 be fair, so after a while we'll see events from all
12568 threads that have anything of interest, and no longer
12569 need to keep this breakpoint location around. We
12570 don't hold locations forever so to reduce chances of
12571 mistaking a non-breakpoint SIGTRAP for a breakpoint
12572 SIGTRAP.
12573
12574 The heuristic failing can be disastrous on
12575 decr_pc_after_break targets.
12576
12577 On decr_pc_after_break targets, like e.g., x86-linux,
12578 if we fail to recognize a late breakpoint SIGTRAP,
12579 because events_till_retirement has reached 0 too
12580 soon, we'll fail to do the PC adjustment, and report
12581 a random SIGTRAP to the user. When the user resumes
12582 the inferior, it will most likely immediately crash
12583 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
12584 corrupted, because of being resumed e.g., in the
12585 middle of a multi-byte instruction, or skipped a
12586 one-byte instruction. This was actually seen happen
12587 on native x86-linux, and should be less rare on
12588 targets that do not support new thread events, like
12589 remote, due to the heuristic depending on
12590 thread_count.
12591
12592 Mistaking a random SIGTRAP for a breakpoint trap
12593 causes similar symptoms (PC adjustment applied when
12594 it shouldn't), but then again, playing with SIGTRAPs
12595 behind the debugger's back is asking for trouble.
12596
12597 Since hardware watchpoint traps are always
12598 distinguishable from other traps, so we don't need to
12599 apply keep hardware watchpoint moribund locations
12600 around. We simply always ignore hardware watchpoint
12601 traps we can no longer explain. */
12602
12603 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12604 old_loc->owner = NULL;
12605
12606 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
12607 }
12608 else
12609 {
12610 old_loc->owner = NULL;
12611 decref_bp_location (&old_loc);
12612 }
12613 }
12614 }
12615
12616 /* Rescan breakpoints at the same address and section, marking the
12617 first one as "first" and any others as "duplicates". This is so
12618 that the bpt instruction is only inserted once. If we have a
12619 permanent breakpoint at the same place as BPT, make that one the
12620 official one, and the rest as duplicates. Permanent breakpoints
12621 are sorted first for the same address.
12622
12623 Do the same for hardware watchpoints, but also considering the
12624 watchpoint's type (regular/access/read) and length. */
12625
12626 bp_loc_first = NULL;
12627 wp_loc_first = NULL;
12628 awp_loc_first = NULL;
12629 rwp_loc_first = NULL;
12630 ALL_BP_LOCATIONS (loc, locp)
12631 {
12632 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12633 non-NULL. */
12634 struct bp_location **loc_first_p;
12635 b = loc->owner;
12636
12637 if (!unduplicated_should_be_inserted (loc)
12638 || !breakpoint_address_is_meaningful (b)
12639 /* Don't detect duplicate for tracepoint locations because they are
12640 never duplicated. See the comments in field `duplicate' of
12641 `struct bp_location'. */
12642 || is_tracepoint (b))
12643 {
12644 /* Clear the condition modification flag. */
12645 loc->condition_changed = condition_unchanged;
12646 continue;
12647 }
12648
12649 if (b->type == bp_hardware_watchpoint)
12650 loc_first_p = &wp_loc_first;
12651 else if (b->type == bp_read_watchpoint)
12652 loc_first_p = &rwp_loc_first;
12653 else if (b->type == bp_access_watchpoint)
12654 loc_first_p = &awp_loc_first;
12655 else
12656 loc_first_p = &bp_loc_first;
12657
12658 if (*loc_first_p == NULL
12659 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12660 || !breakpoint_locations_match (loc, *loc_first_p))
12661 {
12662 *loc_first_p = loc;
12663 loc->duplicate = 0;
12664
12665 if (is_breakpoint (loc->owner) && loc->condition_changed)
12666 {
12667 loc->needs_update = 1;
12668 /* Clear the condition modification flag. */
12669 loc->condition_changed = condition_unchanged;
12670 }
12671 continue;
12672 }
12673
12674
12675 /* This and the above ensure the invariant that the first location
12676 is not duplicated, and is the inserted one.
12677 All following are marked as duplicated, and are not inserted. */
12678 if (loc->inserted)
12679 swap_insertion (loc, *loc_first_p);
12680 loc->duplicate = 1;
12681
12682 /* Clear the condition modification flag. */
12683 loc->condition_changed = condition_unchanged;
12684 }
12685
12686 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
12687 {
12688 if (insert_mode != UGLL_DONT_INSERT)
12689 insert_breakpoint_locations ();
12690 else
12691 {
12692 /* Even though the caller told us to not insert new
12693 locations, we may still need to update conditions on the
12694 target's side of breakpoints that were already inserted
12695 if the target is evaluating breakpoint conditions. We
12696 only update conditions for locations that are marked
12697 "needs_update". */
12698 update_inserted_breakpoint_locations ();
12699 }
12700 }
12701
12702 if (insert_mode != UGLL_DONT_INSERT)
12703 download_tracepoint_locations ();
12704
12705 do_cleanups (cleanups);
12706 }
12707
12708 void
12709 breakpoint_retire_moribund (void)
12710 {
12711 struct bp_location *loc;
12712 int ix;
12713
12714 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12715 if (--(loc->events_till_retirement) == 0)
12716 {
12717 decref_bp_location (&loc);
12718 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12719 --ix;
12720 }
12721 }
12722
12723 static void
12724 update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
12725 {
12726
12727 TRY
12728 {
12729 update_global_location_list (insert_mode);
12730 }
12731 CATCH (e, RETURN_MASK_ERROR)
12732 {
12733 }
12734 END_CATCH
12735 }
12736
12737 /* Clear BKP from a BPS. */
12738
12739 static void
12740 bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12741 {
12742 bpstat bs;
12743
12744 for (bs = bps; bs; bs = bs->next)
12745 if (bs->breakpoint_at == bpt)
12746 {
12747 bs->breakpoint_at = NULL;
12748 bs->old_val = NULL;
12749 /* bs->commands will be freed later. */
12750 }
12751 }
12752
12753 /* Callback for iterate_over_threads. */
12754 static int
12755 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12756 {
12757 struct breakpoint *bpt = (struct breakpoint *) data;
12758
12759 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12760 return 0;
12761 }
12762
12763 /* Helper for breakpoint and tracepoint breakpoint_ops->mention
12764 callbacks. */
12765
12766 static void
12767 say_where (struct breakpoint *b)
12768 {
12769 struct value_print_options opts;
12770
12771 get_user_print_options (&opts);
12772
12773 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12774 single string. */
12775 if (b->loc == NULL)
12776 {
12777 /* For pending locations, the output differs slightly based
12778 on b->extra_string. If this is non-NULL, it contains either
12779 a condition or dprintf arguments. */
12780 if (b->extra_string == NULL)
12781 {
12782 printf_filtered (_(" (%s) pending."),
12783 event_location_to_string (b->location));
12784 }
12785 else if (b->type == bp_dprintf)
12786 {
12787 printf_filtered (_(" (%s,%s) pending."),
12788 event_location_to_string (b->location),
12789 b->extra_string);
12790 }
12791 else
12792 {
12793 printf_filtered (_(" (%s %s) pending."),
12794 event_location_to_string (b->location),
12795 b->extra_string);
12796 }
12797 }
12798 else
12799 {
12800 if (opts.addressprint || b->loc->symtab == NULL)
12801 {
12802 printf_filtered (" at ");
12803 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12804 gdb_stdout);
12805 }
12806 if (b->loc->symtab != NULL)
12807 {
12808 /* If there is a single location, we can print the location
12809 more nicely. */
12810 if (b->loc->next == NULL)
12811 printf_filtered (": file %s, line %d.",
12812 symtab_to_filename_for_display (b->loc->symtab),
12813 b->loc->line_number);
12814 else
12815 /* This is not ideal, but each location may have a
12816 different file name, and this at least reflects the
12817 real situation somewhat. */
12818 printf_filtered (": %s.",
12819 event_location_to_string (b->location));
12820 }
12821
12822 if (b->loc->next)
12823 {
12824 struct bp_location *loc = b->loc;
12825 int n = 0;
12826 for (; loc; loc = loc->next)
12827 ++n;
12828 printf_filtered (" (%d locations)", n);
12829 }
12830 }
12831 }
12832
12833 /* Default bp_location_ops methods. */
12834
12835 static void
12836 bp_location_dtor (struct bp_location *self)
12837 {
12838 xfree (self->cond);
12839 if (self->cond_bytecode)
12840 free_agent_expr (self->cond_bytecode);
12841 xfree (self->function_name);
12842
12843 VEC_free (agent_expr_p, self->target_info.conditions);
12844 VEC_free (agent_expr_p, self->target_info.tcommands);
12845 }
12846
12847 static const struct bp_location_ops bp_location_ops =
12848 {
12849 bp_location_dtor
12850 };
12851
12852 /* Default breakpoint_ops methods all breakpoint_ops ultimately
12853 inherit from. */
12854
12855 static void
12856 base_breakpoint_dtor (struct breakpoint *self)
12857 {
12858 decref_counted_command_line (&self->commands);
12859 xfree (self->cond_string);
12860 xfree (self->extra_string);
12861 xfree (self->filter);
12862 delete_event_location (self->location);
12863 delete_event_location (self->location_range_end);
12864 }
12865
12866 static struct bp_location *
12867 base_breakpoint_allocate_location (struct breakpoint *self)
12868 {
12869 struct bp_location *loc;
12870
12871 loc = XNEW (struct bp_location);
12872 init_bp_location (loc, &bp_location_ops, self);
12873 return loc;
12874 }
12875
12876 static void
12877 base_breakpoint_re_set (struct breakpoint *b)
12878 {
12879 /* Nothing to re-set. */
12880 }
12881
12882 #define internal_error_pure_virtual_called() \
12883 gdb_assert_not_reached ("pure virtual function called")
12884
12885 static int
12886 base_breakpoint_insert_location (struct bp_location *bl)
12887 {
12888 internal_error_pure_virtual_called ();
12889 }
12890
12891 static int
12892 base_breakpoint_remove_location (struct bp_location *bl)
12893 {
12894 internal_error_pure_virtual_called ();
12895 }
12896
12897 static int
12898 base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12899 struct address_space *aspace,
12900 CORE_ADDR bp_addr,
12901 const struct target_waitstatus *ws)
12902 {
12903 internal_error_pure_virtual_called ();
12904 }
12905
12906 static void
12907 base_breakpoint_check_status (bpstat bs)
12908 {
12909 /* Always stop. */
12910 }
12911
12912 /* A "works_in_software_mode" breakpoint_ops method that just internal
12913 errors. */
12914
12915 static int
12916 base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12917 {
12918 internal_error_pure_virtual_called ();
12919 }
12920
12921 /* A "resources_needed" breakpoint_ops method that just internal
12922 errors. */
12923
12924 static int
12925 base_breakpoint_resources_needed (const struct bp_location *bl)
12926 {
12927 internal_error_pure_virtual_called ();
12928 }
12929
12930 static enum print_stop_action
12931 base_breakpoint_print_it (bpstat bs)
12932 {
12933 internal_error_pure_virtual_called ();
12934 }
12935
12936 static void
12937 base_breakpoint_print_one_detail (const struct breakpoint *self,
12938 struct ui_out *uiout)
12939 {
12940 /* nothing */
12941 }
12942
12943 static void
12944 base_breakpoint_print_mention (struct breakpoint *b)
12945 {
12946 internal_error_pure_virtual_called ();
12947 }
12948
12949 static void
12950 base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12951 {
12952 internal_error_pure_virtual_called ();
12953 }
12954
12955 static void
12956 base_breakpoint_create_sals_from_location
12957 (const struct event_location *location,
12958 struct linespec_result *canonical,
12959 enum bptype type_wanted)
12960 {
12961 internal_error_pure_virtual_called ();
12962 }
12963
12964 static void
12965 base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12966 struct linespec_result *c,
12967 char *cond_string,
12968 char *extra_string,
12969 enum bptype type_wanted,
12970 enum bpdisp disposition,
12971 int thread,
12972 int task, int ignore_count,
12973 const struct breakpoint_ops *o,
12974 int from_tty, int enabled,
12975 int internal, unsigned flags)
12976 {
12977 internal_error_pure_virtual_called ();
12978 }
12979
12980 static void
12981 base_breakpoint_decode_location (struct breakpoint *b,
12982 const struct event_location *location,
12983 struct symtabs_and_lines *sals)
12984 {
12985 internal_error_pure_virtual_called ();
12986 }
12987
12988 /* The default 'explains_signal' method. */
12989
12990 static int
12991 base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
12992 {
12993 return 1;
12994 }
12995
12996 /* The default "after_condition_true" method. */
12997
12998 static void
12999 base_breakpoint_after_condition_true (struct bpstats *bs)
13000 {
13001 /* Nothing to do. */
13002 }
13003
13004 struct breakpoint_ops base_breakpoint_ops =
13005 {
13006 base_breakpoint_dtor,
13007 base_breakpoint_allocate_location,
13008 base_breakpoint_re_set,
13009 base_breakpoint_insert_location,
13010 base_breakpoint_remove_location,
13011 base_breakpoint_breakpoint_hit,
13012 base_breakpoint_check_status,
13013 base_breakpoint_resources_needed,
13014 base_breakpoint_works_in_software_mode,
13015 base_breakpoint_print_it,
13016 NULL,
13017 base_breakpoint_print_one_detail,
13018 base_breakpoint_print_mention,
13019 base_breakpoint_print_recreate,
13020 base_breakpoint_create_sals_from_location,
13021 base_breakpoint_create_breakpoints_sal,
13022 base_breakpoint_decode_location,
13023 base_breakpoint_explains_signal,
13024 base_breakpoint_after_condition_true,
13025 };
13026
13027 /* Default breakpoint_ops methods. */
13028
13029 static void
13030 bkpt_re_set (struct breakpoint *b)
13031 {
13032 /* FIXME: is this still reachable? */
13033 if (event_location_empty_p (b->location))
13034 {
13035 /* Anything without a location can't be re-set. */
13036 delete_breakpoint (b);
13037 return;
13038 }
13039
13040 breakpoint_re_set_default (b);
13041 }
13042
13043 static int
13044 bkpt_insert_location (struct bp_location *bl)
13045 {
13046 if (bl->loc_type == bp_loc_hardware_breakpoint)
13047 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
13048 else
13049 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
13050 }
13051
13052 static int
13053 bkpt_remove_location (struct bp_location *bl)
13054 {
13055 if (bl->loc_type == bp_loc_hardware_breakpoint)
13056 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
13057 else
13058 return target_remove_breakpoint (bl->gdbarch, &bl->target_info);
13059 }
13060
13061 static int
13062 bkpt_breakpoint_hit (const struct bp_location *bl,
13063 struct address_space *aspace, CORE_ADDR bp_addr,
13064 const struct target_waitstatus *ws)
13065 {
13066 if (ws->kind != TARGET_WAITKIND_STOPPED
13067 || ws->value.sig != GDB_SIGNAL_TRAP)
13068 return 0;
13069
13070 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
13071 aspace, bp_addr))
13072 return 0;
13073
13074 if (overlay_debugging /* unmapped overlay section */
13075 && section_is_overlay (bl->section)
13076 && !section_is_mapped (bl->section))
13077 return 0;
13078
13079 return 1;
13080 }
13081
13082 static int
13083 dprintf_breakpoint_hit (const struct bp_location *bl,
13084 struct address_space *aspace, CORE_ADDR bp_addr,
13085 const struct target_waitstatus *ws)
13086 {
13087 if (dprintf_style == dprintf_style_agent
13088 && target_can_run_breakpoint_commands ())
13089 {
13090 /* An agent-style dprintf never causes a stop. If we see a trap
13091 for this address it must be for a breakpoint that happens to
13092 be set at the same address. */
13093 return 0;
13094 }
13095
13096 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
13097 }
13098
13099 static int
13100 bkpt_resources_needed (const struct bp_location *bl)
13101 {
13102 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
13103
13104 return 1;
13105 }
13106
13107 static enum print_stop_action
13108 bkpt_print_it (bpstat bs)
13109 {
13110 struct breakpoint *b;
13111 const struct bp_location *bl;
13112 int bp_temp;
13113 struct ui_out *uiout = current_uiout;
13114
13115 gdb_assert (bs->bp_location_at != NULL);
13116
13117 bl = bs->bp_location_at;
13118 b = bs->breakpoint_at;
13119
13120 bp_temp = b->disposition == disp_del;
13121 if (bl->address != bl->requested_address)
13122 breakpoint_adjustment_warning (bl->requested_address,
13123 bl->address,
13124 b->number, 1);
13125 annotate_breakpoint (b->number);
13126 if (bp_temp)
13127 ui_out_text (uiout, "\nTemporary breakpoint ");
13128 else
13129 ui_out_text (uiout, "\nBreakpoint ");
13130 if (ui_out_is_mi_like_p (uiout))
13131 {
13132 ui_out_field_string (uiout, "reason",
13133 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
13134 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
13135 }
13136 ui_out_field_int (uiout, "bkptno", b->number);
13137 ui_out_text (uiout, ", ");
13138
13139 return PRINT_SRC_AND_LOC;
13140 }
13141
13142 static void
13143 bkpt_print_mention (struct breakpoint *b)
13144 {
13145 if (ui_out_is_mi_like_p (current_uiout))
13146 return;
13147
13148 switch (b->type)
13149 {
13150 case bp_breakpoint:
13151 case bp_gnu_ifunc_resolver:
13152 if (b->disposition == disp_del)
13153 printf_filtered (_("Temporary breakpoint"));
13154 else
13155 printf_filtered (_("Breakpoint"));
13156 printf_filtered (_(" %d"), b->number);
13157 if (b->type == bp_gnu_ifunc_resolver)
13158 printf_filtered (_(" at gnu-indirect-function resolver"));
13159 break;
13160 case bp_hardware_breakpoint:
13161 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
13162 break;
13163 case bp_dprintf:
13164 printf_filtered (_("Dprintf %d"), b->number);
13165 break;
13166 }
13167
13168 say_where (b);
13169 }
13170
13171 static void
13172 bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13173 {
13174 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
13175 fprintf_unfiltered (fp, "tbreak");
13176 else if (tp->type == bp_breakpoint)
13177 fprintf_unfiltered (fp, "break");
13178 else if (tp->type == bp_hardware_breakpoint
13179 && tp->disposition == disp_del)
13180 fprintf_unfiltered (fp, "thbreak");
13181 else if (tp->type == bp_hardware_breakpoint)
13182 fprintf_unfiltered (fp, "hbreak");
13183 else
13184 internal_error (__FILE__, __LINE__,
13185 _("unhandled breakpoint type %d"), (int) tp->type);
13186
13187 fprintf_unfiltered (fp, " %s",
13188 event_location_to_string (tp->location));
13189
13190 /* Print out extra_string if this breakpoint is pending. It might
13191 contain, for example, conditions that were set by the user. */
13192 if (tp->loc == NULL && tp->extra_string != NULL)
13193 fprintf_unfiltered (fp, " %s", tp->extra_string);
13194
13195 print_recreate_thread (tp, fp);
13196 }
13197
13198 static void
13199 bkpt_create_sals_from_location (const struct event_location *location,
13200 struct linespec_result *canonical,
13201 enum bptype type_wanted)
13202 {
13203 create_sals_from_location_default (location, canonical, type_wanted);
13204 }
13205
13206 static void
13207 bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
13208 struct linespec_result *canonical,
13209 char *cond_string,
13210 char *extra_string,
13211 enum bptype type_wanted,
13212 enum bpdisp disposition,
13213 int thread,
13214 int task, int ignore_count,
13215 const struct breakpoint_ops *ops,
13216 int from_tty, int enabled,
13217 int internal, unsigned flags)
13218 {
13219 create_breakpoints_sal_default (gdbarch, canonical,
13220 cond_string, extra_string,
13221 type_wanted,
13222 disposition, thread, task,
13223 ignore_count, ops, from_tty,
13224 enabled, internal, flags);
13225 }
13226
13227 static void
13228 bkpt_decode_location (struct breakpoint *b,
13229 const struct event_location *location,
13230 struct symtabs_and_lines *sals)
13231 {
13232 decode_location_default (b, location, sals);
13233 }
13234
13235 /* Virtual table for internal breakpoints. */
13236
13237 static void
13238 internal_bkpt_re_set (struct breakpoint *b)
13239 {
13240 switch (b->type)
13241 {
13242 /* Delete overlay event and longjmp master breakpoints; they
13243 will be reset later by breakpoint_re_set. */
13244 case bp_overlay_event:
13245 case bp_longjmp_master:
13246 case bp_std_terminate_master:
13247 case bp_exception_master:
13248 delete_breakpoint (b);
13249 break;
13250
13251 /* This breakpoint is special, it's set up when the inferior
13252 starts and we really don't want to touch it. */
13253 case bp_shlib_event:
13254
13255 /* Like bp_shlib_event, this breakpoint type is special. Once
13256 it is set up, we do not want to touch it. */
13257 case bp_thread_event:
13258 break;
13259 }
13260 }
13261
13262 static void
13263 internal_bkpt_check_status (bpstat bs)
13264 {
13265 if (bs->breakpoint_at->type == bp_shlib_event)
13266 {
13267 /* If requested, stop when the dynamic linker notifies GDB of
13268 events. This allows the user to get control and place
13269 breakpoints in initializer routines for dynamically loaded
13270 objects (among other things). */
13271 bs->stop = stop_on_solib_events;
13272 bs->print = stop_on_solib_events;
13273 }
13274 else
13275 bs->stop = 0;
13276 }
13277
13278 static enum print_stop_action
13279 internal_bkpt_print_it (bpstat bs)
13280 {
13281 struct breakpoint *b;
13282
13283 b = bs->breakpoint_at;
13284
13285 switch (b->type)
13286 {
13287 case bp_shlib_event:
13288 /* Did we stop because the user set the stop_on_solib_events
13289 variable? (If so, we report this as a generic, "Stopped due
13290 to shlib event" message.) */
13291 print_solib_event (0);
13292 break;
13293
13294 case bp_thread_event:
13295 /* Not sure how we will get here.
13296 GDB should not stop for these breakpoints. */
13297 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
13298 break;
13299
13300 case bp_overlay_event:
13301 /* By analogy with the thread event, GDB should not stop for these. */
13302 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
13303 break;
13304
13305 case bp_longjmp_master:
13306 /* These should never be enabled. */
13307 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
13308 break;
13309
13310 case bp_std_terminate_master:
13311 /* These should never be enabled. */
13312 printf_filtered (_("std::terminate Master Breakpoint: "
13313 "gdb should not stop!\n"));
13314 break;
13315
13316 case bp_exception_master:
13317 /* These should never be enabled. */
13318 printf_filtered (_("Exception Master Breakpoint: "
13319 "gdb should not stop!\n"));
13320 break;
13321 }
13322
13323 return PRINT_NOTHING;
13324 }
13325
13326 static void
13327 internal_bkpt_print_mention (struct breakpoint *b)
13328 {
13329 /* Nothing to mention. These breakpoints are internal. */
13330 }
13331
13332 /* Virtual table for momentary breakpoints */
13333
13334 static void
13335 momentary_bkpt_re_set (struct breakpoint *b)
13336 {
13337 /* Keep temporary breakpoints, which can be encountered when we step
13338 over a dlopen call and solib_add is resetting the breakpoints.
13339 Otherwise these should have been blown away via the cleanup chain
13340 or by breakpoint_init_inferior when we rerun the executable. */
13341 }
13342
13343 static void
13344 momentary_bkpt_check_status (bpstat bs)
13345 {
13346 /* Nothing. The point of these breakpoints is causing a stop. */
13347 }
13348
13349 static enum print_stop_action
13350 momentary_bkpt_print_it (bpstat bs)
13351 {
13352 return PRINT_UNKNOWN;
13353 }
13354
13355 static void
13356 momentary_bkpt_print_mention (struct breakpoint *b)
13357 {
13358 /* Nothing to mention. These breakpoints are internal. */
13359 }
13360
13361 /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
13362
13363 It gets cleared already on the removal of the first one of such placed
13364 breakpoints. This is OK as they get all removed altogether. */
13365
13366 static void
13367 longjmp_bkpt_dtor (struct breakpoint *self)
13368 {
13369 struct thread_info *tp = find_thread_id (self->thread);
13370
13371 if (tp)
13372 tp->initiating_frame = null_frame_id;
13373
13374 momentary_breakpoint_ops.dtor (self);
13375 }
13376
13377 /* Specific methods for probe breakpoints. */
13378
13379 static int
13380 bkpt_probe_insert_location (struct bp_location *bl)
13381 {
13382 int v = bkpt_insert_location (bl);
13383
13384 if (v == 0)
13385 {
13386 /* The insertion was successful, now let's set the probe's semaphore
13387 if needed. */
13388 if (bl->probe.probe->pops->set_semaphore != NULL)
13389 bl->probe.probe->pops->set_semaphore (bl->probe.probe,
13390 bl->probe.objfile,
13391 bl->gdbarch);
13392 }
13393
13394 return v;
13395 }
13396
13397 static int
13398 bkpt_probe_remove_location (struct bp_location *bl)
13399 {
13400 /* Let's clear the semaphore before removing the location. */
13401 if (bl->probe.probe->pops->clear_semaphore != NULL)
13402 bl->probe.probe->pops->clear_semaphore (bl->probe.probe,
13403 bl->probe.objfile,
13404 bl->gdbarch);
13405
13406 return bkpt_remove_location (bl);
13407 }
13408
13409 static void
13410 bkpt_probe_create_sals_from_location (const struct event_location *location,
13411 struct linespec_result *canonical,
13412 enum bptype type_wanted)
13413 {
13414 struct linespec_sals lsal;
13415
13416 lsal.sals = parse_probes (location, canonical);
13417 lsal.canonical = xstrdup (event_location_to_string (canonical->location));
13418 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13419 }
13420
13421 static void
13422 bkpt_probe_decode_location (struct breakpoint *b,
13423 const struct event_location *location,
13424 struct symtabs_and_lines *sals)
13425 {
13426 *sals = parse_probes (location, NULL);
13427 if (!sals->sals)
13428 error (_("probe not found"));
13429 }
13430
13431 /* The breakpoint_ops structure to be used in tracepoints. */
13432
13433 static void
13434 tracepoint_re_set (struct breakpoint *b)
13435 {
13436 breakpoint_re_set_default (b);
13437 }
13438
13439 static int
13440 tracepoint_breakpoint_hit (const struct bp_location *bl,
13441 struct address_space *aspace, CORE_ADDR bp_addr,
13442 const struct target_waitstatus *ws)
13443 {
13444 /* By definition, the inferior does not report stops at
13445 tracepoints. */
13446 return 0;
13447 }
13448
13449 static void
13450 tracepoint_print_one_detail (const struct breakpoint *self,
13451 struct ui_out *uiout)
13452 {
13453 struct tracepoint *tp = (struct tracepoint *) self;
13454 if (tp->static_trace_marker_id)
13455 {
13456 gdb_assert (self->type == bp_static_tracepoint);
13457
13458 ui_out_text (uiout, "\tmarker id is ");
13459 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
13460 tp->static_trace_marker_id);
13461 ui_out_text (uiout, "\n");
13462 }
13463 }
13464
13465 static void
13466 tracepoint_print_mention (struct breakpoint *b)
13467 {
13468 if (ui_out_is_mi_like_p (current_uiout))
13469 return;
13470
13471 switch (b->type)
13472 {
13473 case bp_tracepoint:
13474 printf_filtered (_("Tracepoint"));
13475 printf_filtered (_(" %d"), b->number);
13476 break;
13477 case bp_fast_tracepoint:
13478 printf_filtered (_("Fast tracepoint"));
13479 printf_filtered (_(" %d"), b->number);
13480 break;
13481 case bp_static_tracepoint:
13482 printf_filtered (_("Static tracepoint"));
13483 printf_filtered (_(" %d"), b->number);
13484 break;
13485 default:
13486 internal_error (__FILE__, __LINE__,
13487 _("unhandled tracepoint type %d"), (int) b->type);
13488 }
13489
13490 say_where (b);
13491 }
13492
13493 static void
13494 tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
13495 {
13496 struct tracepoint *tp = (struct tracepoint *) self;
13497
13498 if (self->type == bp_fast_tracepoint)
13499 fprintf_unfiltered (fp, "ftrace");
13500 else if (self->type == bp_static_tracepoint)
13501 fprintf_unfiltered (fp, "strace");
13502 else if (self->type == bp_tracepoint)
13503 fprintf_unfiltered (fp, "trace");
13504 else
13505 internal_error (__FILE__, __LINE__,
13506 _("unhandled tracepoint type %d"), (int) self->type);
13507
13508 fprintf_unfiltered (fp, " %s",
13509 event_location_to_string (self->location));
13510 print_recreate_thread (self, fp);
13511
13512 if (tp->pass_count)
13513 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
13514 }
13515
13516 static void
13517 tracepoint_create_sals_from_location (const struct event_location *location,
13518 struct linespec_result *canonical,
13519 enum bptype type_wanted)
13520 {
13521 create_sals_from_location_default (location, canonical, type_wanted);
13522 }
13523
13524 static void
13525 tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13526 struct linespec_result *canonical,
13527 char *cond_string,
13528 char *extra_string,
13529 enum bptype type_wanted,
13530 enum bpdisp disposition,
13531 int thread,
13532 int task, int ignore_count,
13533 const struct breakpoint_ops *ops,
13534 int from_tty, int enabled,
13535 int internal, unsigned flags)
13536 {
13537 create_breakpoints_sal_default (gdbarch, canonical,
13538 cond_string, extra_string,
13539 type_wanted,
13540 disposition, thread, task,
13541 ignore_count, ops, from_tty,
13542 enabled, internal, flags);
13543 }
13544
13545 static void
13546 tracepoint_decode_location (struct breakpoint *b,
13547 const struct event_location *location,
13548 struct symtabs_and_lines *sals)
13549 {
13550 decode_location_default (b, location, sals);
13551 }
13552
13553 struct breakpoint_ops tracepoint_breakpoint_ops;
13554
13555 /* The breakpoint_ops structure to be use on tracepoints placed in a
13556 static probe. */
13557
13558 static void
13559 tracepoint_probe_create_sals_from_location
13560 (const struct event_location *location,
13561 struct linespec_result *canonical,
13562 enum bptype type_wanted)
13563 {
13564 /* We use the same method for breakpoint on probes. */
13565 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
13566 }
13567
13568 static void
13569 tracepoint_probe_decode_location (struct breakpoint *b,
13570 const struct event_location *location,
13571 struct symtabs_and_lines *sals)
13572 {
13573 /* We use the same method for breakpoint on probes. */
13574 bkpt_probe_decode_location (b, location, sals);
13575 }
13576
13577 static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13578
13579 /* Dprintf breakpoint_ops methods. */
13580
13581 static void
13582 dprintf_re_set (struct breakpoint *b)
13583 {
13584 breakpoint_re_set_default (b);
13585
13586 /* extra_string should never be non-NULL for dprintf. */
13587 gdb_assert (b->extra_string != NULL);
13588
13589 /* 1 - connect to target 1, that can run breakpoint commands.
13590 2 - create a dprintf, which resolves fine.
13591 3 - disconnect from target 1
13592 4 - connect to target 2, that can NOT run breakpoint commands.
13593
13594 After steps #3/#4, you'll want the dprintf command list to
13595 be updated, because target 1 and 2 may well return different
13596 answers for target_can_run_breakpoint_commands().
13597 Given absence of finer grained resetting, we get to do
13598 it all the time. */
13599 if (b->extra_string != NULL)
13600 update_dprintf_command_list (b);
13601 }
13602
13603 /* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13604
13605 static void
13606 dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13607 {
13608 fprintf_unfiltered (fp, "dprintf %s,%s",
13609 event_location_to_string (tp->location),
13610 tp->extra_string);
13611 print_recreate_thread (tp, fp);
13612 }
13613
13614 /* Implement the "after_condition_true" breakpoint_ops method for
13615 dprintf.
13616
13617 dprintf's are implemented with regular commands in their command
13618 list, but we run the commands here instead of before presenting the
13619 stop to the user, as dprintf's don't actually cause a stop. This
13620 also makes it so that the commands of multiple dprintfs at the same
13621 address are all handled. */
13622
13623 static void
13624 dprintf_after_condition_true (struct bpstats *bs)
13625 {
13626 struct cleanup *old_chain;
13627 struct bpstats tmp_bs = { NULL };
13628 struct bpstats *tmp_bs_p = &tmp_bs;
13629
13630 /* dprintf's never cause a stop. This wasn't set in the
13631 check_status hook instead because that would make the dprintf's
13632 condition not be evaluated. */
13633 bs->stop = 0;
13634
13635 /* Run the command list here. Take ownership of it instead of
13636 copying. We never want these commands to run later in
13637 bpstat_do_actions, if a breakpoint that causes a stop happens to
13638 be set at same address as this dprintf, or even if running the
13639 commands here throws. */
13640 tmp_bs.commands = bs->commands;
13641 bs->commands = NULL;
13642 old_chain = make_cleanup_decref_counted_command_line (&tmp_bs.commands);
13643
13644 bpstat_do_actions_1 (&tmp_bs_p);
13645
13646 /* 'tmp_bs.commands' will usually be NULL by now, but
13647 bpstat_do_actions_1 may return early without processing the whole
13648 list. */
13649 do_cleanups (old_chain);
13650 }
13651
13652 /* The breakpoint_ops structure to be used on static tracepoints with
13653 markers (`-m'). */
13654
13655 static void
13656 strace_marker_create_sals_from_location (const struct event_location *location,
13657 struct linespec_result *canonical,
13658 enum bptype type_wanted)
13659 {
13660 struct linespec_sals lsal;
13661 const char *arg_start, *arg;
13662 char *str;
13663 struct cleanup *cleanup;
13664
13665 arg = arg_start = get_linespec_location (location);
13666 lsal.sals = decode_static_tracepoint_spec (&arg);
13667
13668 str = savestring (arg_start, arg - arg_start);
13669 cleanup = make_cleanup (xfree, str);
13670 canonical->location = new_linespec_location (&str);
13671 do_cleanups (cleanup);
13672
13673 lsal.canonical = xstrdup (event_location_to_string (canonical->location));
13674 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13675 }
13676
13677 static void
13678 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13679 struct linespec_result *canonical,
13680 char *cond_string,
13681 char *extra_string,
13682 enum bptype type_wanted,
13683 enum bpdisp disposition,
13684 int thread,
13685 int task, int ignore_count,
13686 const struct breakpoint_ops *ops,
13687 int from_tty, int enabled,
13688 int internal, unsigned flags)
13689 {
13690 int i;
13691 struct linespec_sals *lsal = VEC_index (linespec_sals,
13692 canonical->sals, 0);
13693
13694 /* If the user is creating a static tracepoint by marker id
13695 (strace -m MARKER_ID), then store the sals index, so that
13696 breakpoint_re_set can try to match up which of the newly
13697 found markers corresponds to this one, and, don't try to
13698 expand multiple locations for each sal, given than SALS
13699 already should contain all sals for MARKER_ID. */
13700
13701 for (i = 0; i < lsal->sals.nelts; ++i)
13702 {
13703 struct symtabs_and_lines expanded;
13704 struct tracepoint *tp;
13705 struct cleanup *old_chain;
13706 struct event_location *location;
13707
13708 expanded.nelts = 1;
13709 expanded.sals = &lsal->sals.sals[i];
13710
13711 location = copy_event_location (canonical->location);
13712 old_chain = make_cleanup_delete_event_location (location);
13713
13714 tp = XCNEW (struct tracepoint);
13715 init_breakpoint_sal (&tp->base, gdbarch, expanded,
13716 location, NULL,
13717 cond_string, extra_string,
13718 type_wanted, disposition,
13719 thread, task, ignore_count, ops,
13720 from_tty, enabled, internal, flags,
13721 canonical->special_display);
13722 /* Given that its possible to have multiple markers with
13723 the same string id, if the user is creating a static
13724 tracepoint by marker id ("strace -m MARKER_ID"), then
13725 store the sals index, so that breakpoint_re_set can
13726 try to match up which of the newly found markers
13727 corresponds to this one */
13728 tp->static_trace_marker_id_idx = i;
13729
13730 install_breakpoint (internal, &tp->base, 0);
13731
13732 discard_cleanups (old_chain);
13733 }
13734 }
13735
13736 static void
13737 strace_marker_decode_location (struct breakpoint *b,
13738 const struct event_location *location,
13739 struct symtabs_and_lines *sals)
13740 {
13741 struct tracepoint *tp = (struct tracepoint *) b;
13742 const char *s = get_linespec_location (location);
13743
13744 *sals = decode_static_tracepoint_spec (&s);
13745 if (sals->nelts > tp->static_trace_marker_id_idx)
13746 {
13747 sals->sals[0] = sals->sals[tp->static_trace_marker_id_idx];
13748 sals->nelts = 1;
13749 }
13750 else
13751 error (_("marker %s not found"), tp->static_trace_marker_id);
13752 }
13753
13754 static struct breakpoint_ops strace_marker_breakpoint_ops;
13755
13756 static int
13757 strace_marker_p (struct breakpoint *b)
13758 {
13759 return b->ops == &strace_marker_breakpoint_ops;
13760 }
13761
13762 /* Delete a breakpoint and clean up all traces of it in the data
13763 structures. */
13764
13765 void
13766 delete_breakpoint (struct breakpoint *bpt)
13767 {
13768 struct breakpoint *b;
13769
13770 gdb_assert (bpt != NULL);
13771
13772 /* Has this bp already been deleted? This can happen because
13773 multiple lists can hold pointers to bp's. bpstat lists are
13774 especial culprits.
13775
13776 One example of this happening is a watchpoint's scope bp. When
13777 the scope bp triggers, we notice that the watchpoint is out of
13778 scope, and delete it. We also delete its scope bp. But the
13779 scope bp is marked "auto-deleting", and is already on a bpstat.
13780 That bpstat is then checked for auto-deleting bp's, which are
13781 deleted.
13782
13783 A real solution to this problem might involve reference counts in
13784 bp's, and/or giving them pointers back to their referencing
13785 bpstat's, and teaching delete_breakpoint to only free a bp's
13786 storage when no more references were extent. A cheaper bandaid
13787 was chosen. */
13788 if (bpt->type == bp_none)
13789 return;
13790
13791 /* At least avoid this stale reference until the reference counting
13792 of breakpoints gets resolved. */
13793 if (bpt->related_breakpoint != bpt)
13794 {
13795 struct breakpoint *related;
13796 struct watchpoint *w;
13797
13798 if (bpt->type == bp_watchpoint_scope)
13799 w = (struct watchpoint *) bpt->related_breakpoint;
13800 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
13801 w = (struct watchpoint *) bpt;
13802 else
13803 w = NULL;
13804 if (w != NULL)
13805 watchpoint_del_at_next_stop (w);
13806
13807 /* Unlink bpt from the bpt->related_breakpoint ring. */
13808 for (related = bpt; related->related_breakpoint != bpt;
13809 related = related->related_breakpoint);
13810 related->related_breakpoint = bpt->related_breakpoint;
13811 bpt->related_breakpoint = bpt;
13812 }
13813
13814 /* watch_command_1 creates a watchpoint but only sets its number if
13815 update_watchpoint succeeds in creating its bp_locations. If there's
13816 a problem in that process, we'll be asked to delete the half-created
13817 watchpoint. In that case, don't announce the deletion. */
13818 if (bpt->number)
13819 observer_notify_breakpoint_deleted (bpt);
13820
13821 if (breakpoint_chain == bpt)
13822 breakpoint_chain = bpt->next;
13823
13824 ALL_BREAKPOINTS (b)
13825 if (b->next == bpt)
13826 {
13827 b->next = bpt->next;
13828 break;
13829 }
13830
13831 /* Be sure no bpstat's are pointing at the breakpoint after it's
13832 been freed. */
13833 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
13834 in all threads for now. Note that we cannot just remove bpstats
13835 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13836 commands are associated with the bpstat; if we remove it here,
13837 then the later call to bpstat_do_actions (&stop_bpstat); in
13838 event-top.c won't do anything, and temporary breakpoints with
13839 commands won't work. */
13840
13841 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13842
13843 /* Now that breakpoint is removed from breakpoint list, update the
13844 global location list. This will remove locations that used to
13845 belong to this breakpoint. Do this before freeing the breakpoint
13846 itself, since remove_breakpoint looks at location's owner. It
13847 might be better design to have location completely
13848 self-contained, but it's not the case now. */
13849 update_global_location_list (UGLL_DONT_INSERT);
13850
13851 bpt->ops->dtor (bpt);
13852 /* On the chance that someone will soon try again to delete this
13853 same bp, we mark it as deleted before freeing its storage. */
13854 bpt->type = bp_none;
13855 xfree (bpt);
13856 }
13857
13858 static void
13859 do_delete_breakpoint_cleanup (void *b)
13860 {
13861 delete_breakpoint ((struct breakpoint *) b);
13862 }
13863
13864 struct cleanup *
13865 make_cleanup_delete_breakpoint (struct breakpoint *b)
13866 {
13867 return make_cleanup (do_delete_breakpoint_cleanup, b);
13868 }
13869
13870 /* Iterator function to call a user-provided callback function once
13871 for each of B and its related breakpoints. */
13872
13873 static void
13874 iterate_over_related_breakpoints (struct breakpoint *b,
13875 void (*function) (struct breakpoint *,
13876 void *),
13877 void *data)
13878 {
13879 struct breakpoint *related;
13880
13881 related = b;
13882 do
13883 {
13884 struct breakpoint *next;
13885
13886 /* FUNCTION may delete RELATED. */
13887 next = related->related_breakpoint;
13888
13889 if (next == related)
13890 {
13891 /* RELATED is the last ring entry. */
13892 function (related, data);
13893
13894 /* FUNCTION may have deleted it, so we'd never reach back to
13895 B. There's nothing left to do anyway, so just break
13896 out. */
13897 break;
13898 }
13899 else
13900 function (related, data);
13901
13902 related = next;
13903 }
13904 while (related != b);
13905 }
13906
13907 static void
13908 do_delete_breakpoint (struct breakpoint *b, void *ignore)
13909 {
13910 delete_breakpoint (b);
13911 }
13912
13913 /* A callback for map_breakpoint_numbers that calls
13914 delete_breakpoint. */
13915
13916 static void
13917 do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
13918 {
13919 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
13920 }
13921
13922 void
13923 delete_command (char *arg, int from_tty)
13924 {
13925 struct breakpoint *b, *b_tmp;
13926
13927 dont_repeat ();
13928
13929 if (arg == 0)
13930 {
13931 int breaks_to_delete = 0;
13932
13933 /* Delete all breakpoints if no argument. Do not delete
13934 internal breakpoints, these have to be deleted with an
13935 explicit breakpoint number argument. */
13936 ALL_BREAKPOINTS (b)
13937 if (user_breakpoint_p (b))
13938 {
13939 breaks_to_delete = 1;
13940 break;
13941 }
13942
13943 /* Ask user only if there are some breakpoints to delete. */
13944 if (!from_tty
13945 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
13946 {
13947 ALL_BREAKPOINTS_SAFE (b, b_tmp)
13948 if (user_breakpoint_p (b))
13949 delete_breakpoint (b);
13950 }
13951 }
13952 else
13953 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
13954 }
13955
13956 static int
13957 all_locations_are_pending (struct bp_location *loc)
13958 {
13959 for (; loc; loc = loc->next)
13960 if (!loc->shlib_disabled
13961 && !loc->pspace->executing_startup)
13962 return 0;
13963 return 1;
13964 }
13965
13966 /* Subroutine of update_breakpoint_locations to simplify it.
13967 Return non-zero if multiple fns in list LOC have the same name.
13968 Null names are ignored. */
13969
13970 static int
13971 ambiguous_names_p (struct bp_location *loc)
13972 {
13973 struct bp_location *l;
13974 htab_t htab = htab_create_alloc (13, htab_hash_string,
13975 (int (*) (const void *,
13976 const void *)) streq,
13977 NULL, xcalloc, xfree);
13978
13979 for (l = loc; l != NULL; l = l->next)
13980 {
13981 const char **slot;
13982 const char *name = l->function_name;
13983
13984 /* Allow for some names to be NULL, ignore them. */
13985 if (name == NULL)
13986 continue;
13987
13988 slot = (const char **) htab_find_slot (htab, (const void *) name,
13989 INSERT);
13990 /* NOTE: We can assume slot != NULL here because xcalloc never
13991 returns NULL. */
13992 if (*slot != NULL)
13993 {
13994 htab_delete (htab);
13995 return 1;
13996 }
13997 *slot = name;
13998 }
13999
14000 htab_delete (htab);
14001 return 0;
14002 }
14003
14004 /* When symbols change, it probably means the sources changed as well,
14005 and it might mean the static tracepoint markers are no longer at
14006 the same address or line numbers they used to be at last we
14007 checked. Losing your static tracepoints whenever you rebuild is
14008 undesirable. This function tries to resync/rematch gdb static
14009 tracepoints with the markers on the target, for static tracepoints
14010 that have not been set by marker id. Static tracepoint that have
14011 been set by marker id are reset by marker id in breakpoint_re_set.
14012 The heuristic is:
14013
14014 1) For a tracepoint set at a specific address, look for a marker at
14015 the old PC. If one is found there, assume to be the same marker.
14016 If the name / string id of the marker found is different from the
14017 previous known name, assume that means the user renamed the marker
14018 in the sources, and output a warning.
14019
14020 2) For a tracepoint set at a given line number, look for a marker
14021 at the new address of the old line number. If one is found there,
14022 assume to be the same marker. If the name / string id of the
14023 marker found is different from the previous known name, assume that
14024 means the user renamed the marker in the sources, and output a
14025 warning.
14026
14027 3) If a marker is no longer found at the same address or line, it
14028 may mean the marker no longer exists. But it may also just mean
14029 the code changed a bit. Maybe the user added a few lines of code
14030 that made the marker move up or down (in line number terms). Ask
14031 the target for info about the marker with the string id as we knew
14032 it. If found, update line number and address in the matching
14033 static tracepoint. This will get confused if there's more than one
14034 marker with the same ID (possible in UST, although unadvised
14035 precisely because it confuses tools). */
14036
14037 static struct symtab_and_line
14038 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
14039 {
14040 struct tracepoint *tp = (struct tracepoint *) b;
14041 struct static_tracepoint_marker marker;
14042 CORE_ADDR pc;
14043
14044 pc = sal.pc;
14045 if (sal.line)
14046 find_line_pc (sal.symtab, sal.line, &pc);
14047
14048 if (target_static_tracepoint_marker_at (pc, &marker))
14049 {
14050 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
14051 warning (_("static tracepoint %d changed probed marker from %s to %s"),
14052 b->number,
14053 tp->static_trace_marker_id, marker.str_id);
14054
14055 xfree (tp->static_trace_marker_id);
14056 tp->static_trace_marker_id = xstrdup (marker.str_id);
14057 release_static_tracepoint_marker (&marker);
14058
14059 return sal;
14060 }
14061
14062 /* Old marker wasn't found on target at lineno. Try looking it up
14063 by string ID. */
14064 if (!sal.explicit_pc
14065 && sal.line != 0
14066 && sal.symtab != NULL
14067 && tp->static_trace_marker_id != NULL)
14068 {
14069 VEC(static_tracepoint_marker_p) *markers;
14070
14071 markers
14072 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
14073
14074 if (!VEC_empty(static_tracepoint_marker_p, markers))
14075 {
14076 struct symtab_and_line sal2;
14077 struct symbol *sym;
14078 struct static_tracepoint_marker *tpmarker;
14079 struct ui_out *uiout = current_uiout;
14080 struct explicit_location explicit_loc;
14081
14082 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
14083
14084 xfree (tp->static_trace_marker_id);
14085 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
14086
14087 warning (_("marker for static tracepoint %d (%s) not "
14088 "found at previous line number"),
14089 b->number, tp->static_trace_marker_id);
14090
14091 init_sal (&sal2);
14092
14093 sal2.pc = tpmarker->address;
14094
14095 sal2 = find_pc_line (tpmarker->address, 0);
14096 sym = find_pc_sect_function (tpmarker->address, NULL);
14097 ui_out_text (uiout, "Now in ");
14098 if (sym)
14099 {
14100 ui_out_field_string (uiout, "func",
14101 SYMBOL_PRINT_NAME (sym));
14102 ui_out_text (uiout, " at ");
14103 }
14104 ui_out_field_string (uiout, "file",
14105 symtab_to_filename_for_display (sal2.symtab));
14106 ui_out_text (uiout, ":");
14107
14108 if (ui_out_is_mi_like_p (uiout))
14109 {
14110 const char *fullname = symtab_to_fullname (sal2.symtab);
14111
14112 ui_out_field_string (uiout, "fullname", fullname);
14113 }
14114
14115 ui_out_field_int (uiout, "line", sal2.line);
14116 ui_out_text (uiout, "\n");
14117
14118 b->loc->line_number = sal2.line;
14119 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
14120
14121 delete_event_location (b->location);
14122 initialize_explicit_location (&explicit_loc);
14123 explicit_loc.source_filename
14124 = ASTRDUP (symtab_to_filename_for_display (sal2.symtab));
14125 explicit_loc.line_offset.offset = b->loc->line_number;
14126 explicit_loc.line_offset.sign = LINE_OFFSET_NONE;
14127 b->location = new_explicit_location (&explicit_loc);
14128
14129 /* Might be nice to check if function changed, and warn if
14130 so. */
14131
14132 release_static_tracepoint_marker (tpmarker);
14133 }
14134 }
14135 return sal;
14136 }
14137
14138 /* Returns 1 iff locations A and B are sufficiently same that
14139 we don't need to report breakpoint as changed. */
14140
14141 static int
14142 locations_are_equal (struct bp_location *a, struct bp_location *b)
14143 {
14144 while (a && b)
14145 {
14146 if (a->address != b->address)
14147 return 0;
14148
14149 if (a->shlib_disabled != b->shlib_disabled)
14150 return 0;
14151
14152 if (a->enabled != b->enabled)
14153 return 0;
14154
14155 a = a->next;
14156 b = b->next;
14157 }
14158
14159 if ((a == NULL) != (b == NULL))
14160 return 0;
14161
14162 return 1;
14163 }
14164
14165 /* Create new breakpoint locations for B (a hardware or software breakpoint)
14166 based on SALS and SALS_END. If SALS_END.NELTS is not zero, then B is
14167 a ranged breakpoint. */
14168
14169 void
14170 update_breakpoint_locations (struct breakpoint *b,
14171 struct symtabs_and_lines sals,
14172 struct symtabs_and_lines sals_end)
14173 {
14174 int i;
14175 struct bp_location *existing_locations = b->loc;
14176
14177 if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
14178 {
14179 /* Ranged breakpoints have only one start location and one end
14180 location. */
14181 b->enable_state = bp_disabled;
14182 update_global_location_list (UGLL_MAY_INSERT);
14183 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
14184 "multiple locations found\n"),
14185 b->number);
14186 return;
14187 }
14188
14189 /* If there's no new locations, and all existing locations are
14190 pending, don't do anything. This optimizes the common case where
14191 all locations are in the same shared library, that was unloaded.
14192 We'd like to retain the location, so that when the library is
14193 loaded again, we don't loose the enabled/disabled status of the
14194 individual locations. */
14195 if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
14196 return;
14197
14198 b->loc = NULL;
14199
14200 for (i = 0; i < sals.nelts; ++i)
14201 {
14202 struct bp_location *new_loc;
14203
14204 switch_to_program_space_and_thread (sals.sals[i].pspace);
14205
14206 new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
14207
14208 /* Reparse conditions, they might contain references to the
14209 old symtab. */
14210 if (b->cond_string != NULL)
14211 {
14212 const char *s;
14213
14214 s = b->cond_string;
14215 TRY
14216 {
14217 new_loc->cond = parse_exp_1 (&s, sals.sals[i].pc,
14218 block_for_pc (sals.sals[i].pc),
14219 0);
14220 }
14221 CATCH (e, RETURN_MASK_ERROR)
14222 {
14223 warning (_("failed to reevaluate condition "
14224 "for breakpoint %d: %s"),
14225 b->number, e.message);
14226 new_loc->enabled = 0;
14227 }
14228 END_CATCH
14229 }
14230
14231 if (sals_end.nelts)
14232 {
14233 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
14234
14235 new_loc->length = end - sals.sals[0].pc + 1;
14236 }
14237 }
14238
14239 /* If possible, carry over 'disable' status from existing
14240 breakpoints. */
14241 {
14242 struct bp_location *e = existing_locations;
14243 /* If there are multiple breakpoints with the same function name,
14244 e.g. for inline functions, comparing function names won't work.
14245 Instead compare pc addresses; this is just a heuristic as things
14246 may have moved, but in practice it gives the correct answer
14247 often enough until a better solution is found. */
14248 int have_ambiguous_names = ambiguous_names_p (b->loc);
14249
14250 for (; e; e = e->next)
14251 {
14252 if (!e->enabled && e->function_name)
14253 {
14254 struct bp_location *l = b->loc;
14255 if (have_ambiguous_names)
14256 {
14257 for (; l; l = l->next)
14258 if (breakpoint_locations_match (e, l))
14259 {
14260 l->enabled = 0;
14261 break;
14262 }
14263 }
14264 else
14265 {
14266 for (; l; l = l->next)
14267 if (l->function_name
14268 && strcmp (e->function_name, l->function_name) == 0)
14269 {
14270 l->enabled = 0;
14271 break;
14272 }
14273 }
14274 }
14275 }
14276 }
14277
14278 if (!locations_are_equal (existing_locations, b->loc))
14279 observer_notify_breakpoint_modified (b);
14280
14281 update_global_location_list (UGLL_MAY_INSERT);
14282 }
14283
14284 /* Find the SaL locations corresponding to the given LOCATION.
14285 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
14286
14287 static struct symtabs_and_lines
14288 location_to_sals (struct breakpoint *b, struct event_location *location,
14289 int *found)
14290 {
14291 struct symtabs_and_lines sals = {0};
14292 struct gdb_exception exception = exception_none;
14293
14294 gdb_assert (b->ops != NULL);
14295
14296 TRY
14297 {
14298 b->ops->decode_location (b, location, &sals);
14299 }
14300 CATCH (e, RETURN_MASK_ERROR)
14301 {
14302 int not_found_and_ok = 0;
14303
14304 exception = e;
14305
14306 /* For pending breakpoints, it's expected that parsing will
14307 fail until the right shared library is loaded. User has
14308 already told to create pending breakpoints and don't need
14309 extra messages. If breakpoint is in bp_shlib_disabled
14310 state, then user already saw the message about that
14311 breakpoint being disabled, and don't want to see more
14312 errors. */
14313 if (e.error == NOT_FOUND_ERROR
14314 && (b->condition_not_parsed
14315 || (b->loc && b->loc->shlib_disabled)
14316 || (b->loc && b->loc->pspace->executing_startup)
14317 || b->enable_state == bp_disabled))
14318 not_found_and_ok = 1;
14319
14320 if (!not_found_and_ok)
14321 {
14322 /* We surely don't want to warn about the same breakpoint
14323 10 times. One solution, implemented here, is disable
14324 the breakpoint on error. Another solution would be to
14325 have separate 'warning emitted' flag. Since this
14326 happens only when a binary has changed, I don't know
14327 which approach is better. */
14328 b->enable_state = bp_disabled;
14329 throw_exception (e);
14330 }
14331 }
14332 END_CATCH
14333
14334 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
14335 {
14336 int i;
14337
14338 for (i = 0; i < sals.nelts; ++i)
14339 resolve_sal_pc (&sals.sals[i]);
14340 if (b->condition_not_parsed && b->extra_string != NULL)
14341 {
14342 char *cond_string, *extra_string;
14343 int thread, task;
14344
14345 find_condition_and_thread (b->extra_string, sals.sals[0].pc,
14346 &cond_string, &thread, &task,
14347 &extra_string);
14348 gdb_assert (b->cond_string == NULL);
14349 if (cond_string)
14350 b->cond_string = cond_string;
14351 b->thread = thread;
14352 b->task = task;
14353 if (extra_string)
14354 {
14355 xfree (b->extra_string);
14356 b->extra_string = extra_string;
14357 }
14358 b->condition_not_parsed = 0;
14359 }
14360
14361 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
14362 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
14363
14364 *found = 1;
14365 }
14366 else
14367 *found = 0;
14368
14369 return sals;
14370 }
14371
14372 /* The default re_set method, for typical hardware or software
14373 breakpoints. Reevaluate the breakpoint and recreate its
14374 locations. */
14375
14376 static void
14377 breakpoint_re_set_default (struct breakpoint *b)
14378 {
14379 int found;
14380 struct symtabs_and_lines sals, sals_end;
14381 struct symtabs_and_lines expanded = {0};
14382 struct symtabs_and_lines expanded_end = {0};
14383
14384 sals = location_to_sals (b, b->location, &found);
14385 if (found)
14386 {
14387 make_cleanup (xfree, sals.sals);
14388 expanded = sals;
14389 }
14390
14391 if (b->location_range_end != NULL)
14392 {
14393 sals_end = location_to_sals (b, b->location_range_end, &found);
14394 if (found)
14395 {
14396 make_cleanup (xfree, sals_end.sals);
14397 expanded_end = sals_end;
14398 }
14399 }
14400
14401 update_breakpoint_locations (b, expanded, expanded_end);
14402 }
14403
14404 /* Default method for creating SALs from an address string. It basically
14405 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
14406
14407 static void
14408 create_sals_from_location_default (const struct event_location *location,
14409 struct linespec_result *canonical,
14410 enum bptype type_wanted)
14411 {
14412 parse_breakpoint_sals (location, canonical);
14413 }
14414
14415 /* Call create_breakpoints_sal for the given arguments. This is the default
14416 function for the `create_breakpoints_sal' method of
14417 breakpoint_ops. */
14418
14419 static void
14420 create_breakpoints_sal_default (struct gdbarch *gdbarch,
14421 struct linespec_result *canonical,
14422 char *cond_string,
14423 char *extra_string,
14424 enum bptype type_wanted,
14425 enum bpdisp disposition,
14426 int thread,
14427 int task, int ignore_count,
14428 const struct breakpoint_ops *ops,
14429 int from_tty, int enabled,
14430 int internal, unsigned flags)
14431 {
14432 create_breakpoints_sal (gdbarch, canonical, cond_string,
14433 extra_string,
14434 type_wanted, disposition,
14435 thread, task, ignore_count, ops, from_tty,
14436 enabled, internal, flags);
14437 }
14438
14439 /* Decode the line represented by S by calling decode_line_full. This is the
14440 default function for the `decode_location' method of breakpoint_ops. */
14441
14442 static void
14443 decode_location_default (struct breakpoint *b,
14444 const struct event_location *location,
14445 struct symtabs_and_lines *sals)
14446 {
14447 struct linespec_result canonical;
14448
14449 init_linespec_result (&canonical);
14450 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE,
14451 (struct symtab *) NULL, 0,
14452 &canonical, multiple_symbols_all,
14453 b->filter);
14454
14455 /* We should get 0 or 1 resulting SALs. */
14456 gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
14457
14458 if (VEC_length (linespec_sals, canonical.sals) > 0)
14459 {
14460 struct linespec_sals *lsal;
14461
14462 lsal = VEC_index (linespec_sals, canonical.sals, 0);
14463 *sals = lsal->sals;
14464 /* Arrange it so the destructor does not free the
14465 contents. */
14466 lsal->sals.sals = NULL;
14467 }
14468
14469 destroy_linespec_result (&canonical);
14470 }
14471
14472 /* Prepare the global context for a re-set of breakpoint B. */
14473
14474 static struct cleanup *
14475 prepare_re_set_context (struct breakpoint *b)
14476 {
14477 struct cleanup *cleanups;
14478
14479 input_radix = b->input_radix;
14480 cleanups = save_current_space_and_thread ();
14481 if (b->pspace != NULL)
14482 switch_to_program_space_and_thread (b->pspace);
14483 set_language (b->language);
14484
14485 return cleanups;
14486 }
14487
14488 /* Reset a breakpoint given it's struct breakpoint * BINT.
14489 The value we return ends up being the return value from catch_errors.
14490 Unused in this case. */
14491
14492 static int
14493 breakpoint_re_set_one (void *bint)
14494 {
14495 /* Get past catch_errs. */
14496 struct breakpoint *b = (struct breakpoint *) bint;
14497 struct cleanup *cleanups;
14498
14499 cleanups = prepare_re_set_context (b);
14500 b->ops->re_set (b);
14501 do_cleanups (cleanups);
14502 return 0;
14503 }
14504
14505 /* Re-set all breakpoints after symbols have been re-loaded. */
14506 void
14507 breakpoint_re_set (void)
14508 {
14509 struct breakpoint *b, *b_tmp;
14510 enum language save_language;
14511 int save_input_radix;
14512 struct cleanup *old_chain;
14513
14514 save_language = current_language->la_language;
14515 save_input_radix = input_radix;
14516 old_chain = save_current_program_space ();
14517
14518 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14519 {
14520 /* Format possible error msg. */
14521 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
14522 b->number);
14523 struct cleanup *cleanups = make_cleanup (xfree, message);
14524 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
14525 do_cleanups (cleanups);
14526 }
14527 set_language (save_language);
14528 input_radix = save_input_radix;
14529
14530 jit_breakpoint_re_set ();
14531
14532 do_cleanups (old_chain);
14533
14534 create_overlay_event_breakpoint ();
14535 create_longjmp_master_breakpoint ();
14536 create_std_terminate_master_breakpoint ();
14537 create_exception_master_breakpoint ();
14538 }
14539 \f
14540 /* Reset the thread number of this breakpoint:
14541
14542 - If the breakpoint is for all threads, leave it as-is.
14543 - Else, reset it to the current thread for inferior_ptid. */
14544 void
14545 breakpoint_re_set_thread (struct breakpoint *b)
14546 {
14547 if (b->thread != -1)
14548 {
14549 if (in_thread_list (inferior_ptid))
14550 b->thread = pid_to_thread_id (inferior_ptid);
14551
14552 /* We're being called after following a fork. The new fork is
14553 selected as current, and unless this was a vfork will have a
14554 different program space from the original thread. Reset that
14555 as well. */
14556 b->loc->pspace = current_program_space;
14557 }
14558 }
14559
14560 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14561 If from_tty is nonzero, it prints a message to that effect,
14562 which ends with a period (no newline). */
14563
14564 void
14565 set_ignore_count (int bptnum, int count, int from_tty)
14566 {
14567 struct breakpoint *b;
14568
14569 if (count < 0)
14570 count = 0;
14571
14572 ALL_BREAKPOINTS (b)
14573 if (b->number == bptnum)
14574 {
14575 if (is_tracepoint (b))
14576 {
14577 if (from_tty && count != 0)
14578 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14579 bptnum);
14580 return;
14581 }
14582
14583 b->ignore_count = count;
14584 if (from_tty)
14585 {
14586 if (count == 0)
14587 printf_filtered (_("Will stop next time "
14588 "breakpoint %d is reached."),
14589 bptnum);
14590 else if (count == 1)
14591 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
14592 bptnum);
14593 else
14594 printf_filtered (_("Will ignore next %d "
14595 "crossings of breakpoint %d."),
14596 count, bptnum);
14597 }
14598 observer_notify_breakpoint_modified (b);
14599 return;
14600 }
14601
14602 error (_("No breakpoint number %d."), bptnum);
14603 }
14604
14605 /* Command to set ignore-count of breakpoint N to COUNT. */
14606
14607 static void
14608 ignore_command (char *args, int from_tty)
14609 {
14610 char *p = args;
14611 int num;
14612
14613 if (p == 0)
14614 error_no_arg (_("a breakpoint number"));
14615
14616 num = get_number (&p);
14617 if (num == 0)
14618 error (_("bad breakpoint number: '%s'"), args);
14619 if (*p == 0)
14620 error (_("Second argument (specified ignore-count) is missing."));
14621
14622 set_ignore_count (num,
14623 longest_to_int (value_as_long (parse_and_eval (p))),
14624 from_tty);
14625 if (from_tty)
14626 printf_filtered ("\n");
14627 }
14628 \f
14629 /* Call FUNCTION on each of the breakpoints
14630 whose numbers are given in ARGS. */
14631
14632 static void
14633 map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
14634 void *),
14635 void *data)
14636 {
14637 int num;
14638 struct breakpoint *b, *tmp;
14639 int match;
14640 struct get_number_or_range_state state;
14641
14642 if (args == 0 || *args == '\0')
14643 error_no_arg (_("one or more breakpoint numbers"));
14644
14645 init_number_or_range (&state, args);
14646
14647 while (!state.finished)
14648 {
14649 const char *p = state.string;
14650
14651 match = 0;
14652
14653 num = get_number_or_range (&state);
14654 if (num == 0)
14655 {
14656 warning (_("bad breakpoint number at or near '%s'"), p);
14657 }
14658 else
14659 {
14660 ALL_BREAKPOINTS_SAFE (b, tmp)
14661 if (b->number == num)
14662 {
14663 match = 1;
14664 function (b, data);
14665 break;
14666 }
14667 if (match == 0)
14668 printf_unfiltered (_("No breakpoint number %d.\n"), num);
14669 }
14670 }
14671 }
14672
14673 static struct bp_location *
14674 find_location_by_number (char *number)
14675 {
14676 char *dot = strchr (number, '.');
14677 char *p1;
14678 int bp_num;
14679 int loc_num;
14680 struct breakpoint *b;
14681 struct bp_location *loc;
14682
14683 *dot = '\0';
14684
14685 p1 = number;
14686 bp_num = get_number (&p1);
14687 if (bp_num == 0)
14688 error (_("Bad breakpoint number '%s'"), number);
14689
14690 ALL_BREAKPOINTS (b)
14691 if (b->number == bp_num)
14692 {
14693 break;
14694 }
14695
14696 if (!b || b->number != bp_num)
14697 error (_("Bad breakpoint number '%s'"), number);
14698
14699 p1 = dot+1;
14700 loc_num = get_number (&p1);
14701 if (loc_num == 0)
14702 error (_("Bad breakpoint location number '%s'"), number);
14703
14704 --loc_num;
14705 loc = b->loc;
14706 for (;loc_num && loc; --loc_num, loc = loc->next)
14707 ;
14708 if (!loc)
14709 error (_("Bad breakpoint location number '%s'"), dot+1);
14710
14711 return loc;
14712 }
14713
14714
14715 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14716 If from_tty is nonzero, it prints a message to that effect,
14717 which ends with a period (no newline). */
14718
14719 void
14720 disable_breakpoint (struct breakpoint *bpt)
14721 {
14722 /* Never disable a watchpoint scope breakpoint; we want to
14723 hit them when we leave scope so we can delete both the
14724 watchpoint and its scope breakpoint at that time. */
14725 if (bpt->type == bp_watchpoint_scope)
14726 return;
14727
14728 bpt->enable_state = bp_disabled;
14729
14730 /* Mark breakpoint locations modified. */
14731 mark_breakpoint_modified (bpt);
14732
14733 if (target_supports_enable_disable_tracepoint ()
14734 && current_trace_status ()->running && is_tracepoint (bpt))
14735 {
14736 struct bp_location *location;
14737
14738 for (location = bpt->loc; location; location = location->next)
14739 target_disable_tracepoint (location);
14740 }
14741
14742 update_global_location_list (UGLL_DONT_INSERT);
14743
14744 observer_notify_breakpoint_modified (bpt);
14745 }
14746
14747 /* A callback for iterate_over_related_breakpoints. */
14748
14749 static void
14750 do_disable_breakpoint (struct breakpoint *b, void *ignore)
14751 {
14752 disable_breakpoint (b);
14753 }
14754
14755 /* A callback for map_breakpoint_numbers that calls
14756 disable_breakpoint. */
14757
14758 static void
14759 do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
14760 {
14761 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
14762 }
14763
14764 static void
14765 disable_command (char *args, int from_tty)
14766 {
14767 if (args == 0)
14768 {
14769 struct breakpoint *bpt;
14770
14771 ALL_BREAKPOINTS (bpt)
14772 if (user_breakpoint_p (bpt))
14773 disable_breakpoint (bpt);
14774 }
14775 else
14776 {
14777 char *num = extract_arg (&args);
14778
14779 while (num)
14780 {
14781 if (strchr (num, '.'))
14782 {
14783 struct bp_location *loc = find_location_by_number (num);
14784
14785 if (loc)
14786 {
14787 if (loc->enabled)
14788 {
14789 loc->enabled = 0;
14790 mark_breakpoint_location_modified (loc);
14791 }
14792 if (target_supports_enable_disable_tracepoint ()
14793 && current_trace_status ()->running && loc->owner
14794 && is_tracepoint (loc->owner))
14795 target_disable_tracepoint (loc);
14796 }
14797 update_global_location_list (UGLL_DONT_INSERT);
14798 }
14799 else
14800 map_breakpoint_numbers (num, do_map_disable_breakpoint, NULL);
14801 num = extract_arg (&args);
14802 }
14803 }
14804 }
14805
14806 static void
14807 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14808 int count)
14809 {
14810 int target_resources_ok;
14811
14812 if (bpt->type == bp_hardware_breakpoint)
14813 {
14814 int i;
14815 i = hw_breakpoint_used_count ();
14816 target_resources_ok =
14817 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
14818 i + 1, 0);
14819 if (target_resources_ok == 0)
14820 error (_("No hardware breakpoint support in the target."));
14821 else if (target_resources_ok < 0)
14822 error (_("Hardware breakpoints used exceeds limit."));
14823 }
14824
14825 if (is_watchpoint (bpt))
14826 {
14827 /* Initialize it just to avoid a GCC false warning. */
14828 enum enable_state orig_enable_state = bp_disabled;
14829
14830 TRY
14831 {
14832 struct watchpoint *w = (struct watchpoint *) bpt;
14833
14834 orig_enable_state = bpt->enable_state;
14835 bpt->enable_state = bp_enabled;
14836 update_watchpoint (w, 1 /* reparse */);
14837 }
14838 CATCH (e, RETURN_MASK_ALL)
14839 {
14840 bpt->enable_state = orig_enable_state;
14841 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14842 bpt->number);
14843 return;
14844 }
14845 END_CATCH
14846 }
14847
14848 bpt->enable_state = bp_enabled;
14849
14850 /* Mark breakpoint locations modified. */
14851 mark_breakpoint_modified (bpt);
14852
14853 if (target_supports_enable_disable_tracepoint ()
14854 && current_trace_status ()->running && is_tracepoint (bpt))
14855 {
14856 struct bp_location *location;
14857
14858 for (location = bpt->loc; location; location = location->next)
14859 target_enable_tracepoint (location);
14860 }
14861
14862 bpt->disposition = disposition;
14863 bpt->enable_count = count;
14864 update_global_location_list (UGLL_MAY_INSERT);
14865
14866 observer_notify_breakpoint_modified (bpt);
14867 }
14868
14869
14870 void
14871 enable_breakpoint (struct breakpoint *bpt)
14872 {
14873 enable_breakpoint_disp (bpt, bpt->disposition, 0);
14874 }
14875
14876 static void
14877 do_enable_breakpoint (struct breakpoint *bpt, void *arg)
14878 {
14879 enable_breakpoint (bpt);
14880 }
14881
14882 /* A callback for map_breakpoint_numbers that calls
14883 enable_breakpoint. */
14884
14885 static void
14886 do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
14887 {
14888 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
14889 }
14890
14891 /* The enable command enables the specified breakpoints (or all defined
14892 breakpoints) so they once again become (or continue to be) effective
14893 in stopping the inferior. */
14894
14895 static void
14896 enable_command (char *args, int from_tty)
14897 {
14898 if (args == 0)
14899 {
14900 struct breakpoint *bpt;
14901
14902 ALL_BREAKPOINTS (bpt)
14903 if (user_breakpoint_p (bpt))
14904 enable_breakpoint (bpt);
14905 }
14906 else
14907 {
14908 char *num = extract_arg (&args);
14909
14910 while (num)
14911 {
14912 if (strchr (num, '.'))
14913 {
14914 struct bp_location *loc = find_location_by_number (num);
14915
14916 if (loc)
14917 {
14918 if (!loc->enabled)
14919 {
14920 loc->enabled = 1;
14921 mark_breakpoint_location_modified (loc);
14922 }
14923 if (target_supports_enable_disable_tracepoint ()
14924 && current_trace_status ()->running && loc->owner
14925 && is_tracepoint (loc->owner))
14926 target_enable_tracepoint (loc);
14927 }
14928 update_global_location_list (UGLL_MAY_INSERT);
14929 }
14930 else
14931 map_breakpoint_numbers (num, do_map_enable_breakpoint, NULL);
14932 num = extract_arg (&args);
14933 }
14934 }
14935 }
14936
14937 /* This struct packages up disposition data for application to multiple
14938 breakpoints. */
14939
14940 struct disp_data
14941 {
14942 enum bpdisp disp;
14943 int count;
14944 };
14945
14946 static void
14947 do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
14948 {
14949 struct disp_data disp_data = *(struct disp_data *) arg;
14950
14951 enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
14952 }
14953
14954 static void
14955 do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
14956 {
14957 struct disp_data disp = { disp_disable, 1 };
14958
14959 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14960 }
14961
14962 static void
14963 enable_once_command (char *args, int from_tty)
14964 {
14965 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
14966 }
14967
14968 static void
14969 do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
14970 {
14971 struct disp_data disp = { disp_disable, *(int *) countptr };
14972
14973 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14974 }
14975
14976 static void
14977 enable_count_command (char *args, int from_tty)
14978 {
14979 int count;
14980
14981 if (args == NULL)
14982 error_no_arg (_("hit count"));
14983
14984 count = get_number (&args);
14985
14986 map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
14987 }
14988
14989 static void
14990 do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
14991 {
14992 struct disp_data disp = { disp_del, 1 };
14993
14994 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14995 }
14996
14997 static void
14998 enable_delete_command (char *args, int from_tty)
14999 {
15000 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
15001 }
15002 \f
15003 static void
15004 set_breakpoint_cmd (char *args, int from_tty)
15005 {
15006 }
15007
15008 static void
15009 show_breakpoint_cmd (char *args, int from_tty)
15010 {
15011 }
15012
15013 /* Invalidate last known value of any hardware watchpoint if
15014 the memory which that value represents has been written to by
15015 GDB itself. */
15016
15017 static void
15018 invalidate_bp_value_on_memory_change (struct inferior *inferior,
15019 CORE_ADDR addr, ssize_t len,
15020 const bfd_byte *data)
15021 {
15022 struct breakpoint *bp;
15023
15024 ALL_BREAKPOINTS (bp)
15025 if (bp->enable_state == bp_enabled
15026 && bp->type == bp_hardware_watchpoint)
15027 {
15028 struct watchpoint *wp = (struct watchpoint *) bp;
15029
15030 if (wp->val_valid && wp->val)
15031 {
15032 struct bp_location *loc;
15033
15034 for (loc = bp->loc; loc != NULL; loc = loc->next)
15035 if (loc->loc_type == bp_loc_hardware_watchpoint
15036 && loc->address + loc->length > addr
15037 && addr + len > loc->address)
15038 {
15039 value_free (wp->val);
15040 wp->val = NULL;
15041 wp->val_valid = 0;
15042 }
15043 }
15044 }
15045 }
15046
15047 /* Create and insert a breakpoint for software single step. */
15048
15049 void
15050 insert_single_step_breakpoint (struct gdbarch *gdbarch,
15051 struct address_space *aspace,
15052 CORE_ADDR next_pc)
15053 {
15054 struct thread_info *tp = inferior_thread ();
15055 struct symtab_and_line sal;
15056 CORE_ADDR pc = next_pc;
15057
15058 if (tp->control.single_step_breakpoints == NULL)
15059 {
15060 tp->control.single_step_breakpoints
15061 = new_single_step_breakpoint (tp->num, gdbarch);
15062 }
15063
15064 sal = find_pc_line (pc, 0);
15065 sal.pc = pc;
15066 sal.section = find_pc_overlay (pc);
15067 sal.explicit_pc = 1;
15068 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
15069
15070 update_global_location_list (UGLL_INSERT);
15071 }
15072
15073 /* See breakpoint.h. */
15074
15075 int
15076 breakpoint_has_location_inserted_here (struct breakpoint *bp,
15077 struct address_space *aspace,
15078 CORE_ADDR pc)
15079 {
15080 struct bp_location *loc;
15081
15082 for (loc = bp->loc; loc != NULL; loc = loc->next)
15083 if (loc->inserted
15084 && breakpoint_location_address_match (loc, aspace, pc))
15085 return 1;
15086
15087 return 0;
15088 }
15089
15090 /* Check whether a software single-step breakpoint is inserted at
15091 PC. */
15092
15093 int
15094 single_step_breakpoint_inserted_here_p (struct address_space *aspace,
15095 CORE_ADDR pc)
15096 {
15097 struct breakpoint *bpt;
15098
15099 ALL_BREAKPOINTS (bpt)
15100 {
15101 if (bpt->type == bp_single_step
15102 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
15103 return 1;
15104 }
15105 return 0;
15106 }
15107
15108 /* Tracepoint-specific operations. */
15109
15110 /* Set tracepoint count to NUM. */
15111 static void
15112 set_tracepoint_count (int num)
15113 {
15114 tracepoint_count = num;
15115 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
15116 }
15117
15118 static void
15119 trace_command (char *arg, int from_tty)
15120 {
15121 struct breakpoint_ops *ops;
15122 struct event_location *location;
15123 struct cleanup *back_to;
15124
15125 location = string_to_event_location (&arg, current_language);
15126 back_to = make_cleanup_delete_event_location (location);
15127 if (location != NULL
15128 && event_location_type (location) == PROBE_LOCATION)
15129 ops = &tracepoint_probe_breakpoint_ops;
15130 else
15131 ops = &tracepoint_breakpoint_ops;
15132
15133 create_breakpoint (get_current_arch (),
15134 location,
15135 NULL, 0, arg, 1 /* parse arg */,
15136 0 /* tempflag */,
15137 bp_tracepoint /* type_wanted */,
15138 0 /* Ignore count */,
15139 pending_break_support,
15140 ops,
15141 from_tty,
15142 1 /* enabled */,
15143 0 /* internal */, 0);
15144 do_cleanups (back_to);
15145 }
15146
15147 static void
15148 ftrace_command (char *arg, int from_tty)
15149 {
15150 struct event_location *location;
15151 struct cleanup *back_to;
15152
15153 location = string_to_event_location (&arg, current_language);
15154 back_to = make_cleanup_delete_event_location (location);
15155 create_breakpoint (get_current_arch (),
15156 location,
15157 NULL, 0, arg, 1 /* parse arg */,
15158 0 /* tempflag */,
15159 bp_fast_tracepoint /* type_wanted */,
15160 0 /* Ignore count */,
15161 pending_break_support,
15162 &tracepoint_breakpoint_ops,
15163 from_tty,
15164 1 /* enabled */,
15165 0 /* internal */, 0);
15166 do_cleanups (back_to);
15167 }
15168
15169 /* strace command implementation. Creates a static tracepoint. */
15170
15171 static void
15172 strace_command (char *arg, int from_tty)
15173 {
15174 struct breakpoint_ops *ops;
15175 struct event_location *location;
15176 struct cleanup *back_to;
15177
15178 /* Decide if we are dealing with a static tracepoint marker (`-m'),
15179 or with a normal static tracepoint. */
15180 if (arg && startswith (arg, "-m") && isspace (arg[2]))
15181 {
15182 ops = &strace_marker_breakpoint_ops;
15183 location = new_linespec_location (&arg);
15184 }
15185 else
15186 {
15187 ops = &tracepoint_breakpoint_ops;
15188 location = string_to_event_location (&arg, current_language);
15189 }
15190
15191 back_to = make_cleanup_delete_event_location (location);
15192 create_breakpoint (get_current_arch (),
15193 location,
15194 NULL, 0, arg, 1 /* parse arg */,
15195 0 /* tempflag */,
15196 bp_static_tracepoint /* type_wanted */,
15197 0 /* Ignore count */,
15198 pending_break_support,
15199 ops,
15200 from_tty,
15201 1 /* enabled */,
15202 0 /* internal */, 0);
15203 do_cleanups (back_to);
15204 }
15205
15206 /* Set up a fake reader function that gets command lines from a linked
15207 list that was acquired during tracepoint uploading. */
15208
15209 static struct uploaded_tp *this_utp;
15210 static int next_cmd;
15211
15212 static char *
15213 read_uploaded_action (void)
15214 {
15215 char *rslt;
15216
15217 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
15218
15219 next_cmd++;
15220
15221 return rslt;
15222 }
15223
15224 /* Given information about a tracepoint as recorded on a target (which
15225 can be either a live system or a trace file), attempt to create an
15226 equivalent GDB tracepoint. This is not a reliable process, since
15227 the target does not necessarily have all the information used when
15228 the tracepoint was originally defined. */
15229
15230 struct tracepoint *
15231 create_tracepoint_from_upload (struct uploaded_tp *utp)
15232 {
15233 char *addr_str, small_buf[100];
15234 struct tracepoint *tp;
15235 struct event_location *location;
15236 struct cleanup *cleanup;
15237
15238 if (utp->at_string)
15239 addr_str = utp->at_string;
15240 else
15241 {
15242 /* In the absence of a source location, fall back to raw
15243 address. Since there is no way to confirm that the address
15244 means the same thing as when the trace was started, warn the
15245 user. */
15246 warning (_("Uploaded tracepoint %d has no "
15247 "source location, using raw address"),
15248 utp->number);
15249 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
15250 addr_str = small_buf;
15251 }
15252
15253 /* There's not much we can do with a sequence of bytecodes. */
15254 if (utp->cond && !utp->cond_string)
15255 warning (_("Uploaded tracepoint %d condition "
15256 "has no source form, ignoring it"),
15257 utp->number);
15258
15259 location = string_to_event_location (&addr_str, current_language);
15260 cleanup = make_cleanup_delete_event_location (location);
15261 if (!create_breakpoint (get_current_arch (),
15262 location,
15263 utp->cond_string, -1, addr_str,
15264 0 /* parse cond/thread */,
15265 0 /* tempflag */,
15266 utp->type /* type_wanted */,
15267 0 /* Ignore count */,
15268 pending_break_support,
15269 &tracepoint_breakpoint_ops,
15270 0 /* from_tty */,
15271 utp->enabled /* enabled */,
15272 0 /* internal */,
15273 CREATE_BREAKPOINT_FLAGS_INSERTED))
15274 {
15275 do_cleanups (cleanup);
15276 return NULL;
15277 }
15278
15279 do_cleanups (cleanup);
15280
15281 /* Get the tracepoint we just created. */
15282 tp = get_tracepoint (tracepoint_count);
15283 gdb_assert (tp != NULL);
15284
15285 if (utp->pass > 0)
15286 {
15287 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
15288 tp->base.number);
15289
15290 trace_pass_command (small_buf, 0);
15291 }
15292
15293 /* If we have uploaded versions of the original commands, set up a
15294 special-purpose "reader" function and call the usual command line
15295 reader, then pass the result to the breakpoint command-setting
15296 function. */
15297 if (!VEC_empty (char_ptr, utp->cmd_strings))
15298 {
15299 struct command_line *cmd_list;
15300
15301 this_utp = utp;
15302 next_cmd = 0;
15303
15304 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
15305
15306 breakpoint_set_commands (&tp->base, cmd_list);
15307 }
15308 else if (!VEC_empty (char_ptr, utp->actions)
15309 || !VEC_empty (char_ptr, utp->step_actions))
15310 warning (_("Uploaded tracepoint %d actions "
15311 "have no source form, ignoring them"),
15312 utp->number);
15313
15314 /* Copy any status information that might be available. */
15315 tp->base.hit_count = utp->hit_count;
15316 tp->traceframe_usage = utp->traceframe_usage;
15317
15318 return tp;
15319 }
15320
15321 /* Print information on tracepoint number TPNUM_EXP, or all if
15322 omitted. */
15323
15324 static void
15325 tracepoints_info (char *args, int from_tty)
15326 {
15327 struct ui_out *uiout = current_uiout;
15328 int num_printed;
15329
15330 num_printed = breakpoint_1 (args, 0, is_tracepoint);
15331
15332 if (num_printed == 0)
15333 {
15334 if (args == NULL || *args == '\0')
15335 ui_out_message (uiout, 0, "No tracepoints.\n");
15336 else
15337 ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
15338 }
15339
15340 default_collect_info ();
15341 }
15342
15343 /* The 'enable trace' command enables tracepoints.
15344 Not supported by all targets. */
15345 static void
15346 enable_trace_command (char *args, int from_tty)
15347 {
15348 enable_command (args, from_tty);
15349 }
15350
15351 /* The 'disable trace' command disables tracepoints.
15352 Not supported by all targets. */
15353 static void
15354 disable_trace_command (char *args, int from_tty)
15355 {
15356 disable_command (args, from_tty);
15357 }
15358
15359 /* Remove a tracepoint (or all if no argument). */
15360 static void
15361 delete_trace_command (char *arg, int from_tty)
15362 {
15363 struct breakpoint *b, *b_tmp;
15364
15365 dont_repeat ();
15366
15367 if (arg == 0)
15368 {
15369 int breaks_to_delete = 0;
15370
15371 /* Delete all breakpoints if no argument.
15372 Do not delete internal or call-dummy breakpoints, these
15373 have to be deleted with an explicit breakpoint number
15374 argument. */
15375 ALL_TRACEPOINTS (b)
15376 if (is_tracepoint (b) && user_breakpoint_p (b))
15377 {
15378 breaks_to_delete = 1;
15379 break;
15380 }
15381
15382 /* Ask user only if there are some breakpoints to delete. */
15383 if (!from_tty
15384 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
15385 {
15386 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15387 if (is_tracepoint (b) && user_breakpoint_p (b))
15388 delete_breakpoint (b);
15389 }
15390 }
15391 else
15392 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
15393 }
15394
15395 /* Helper function for trace_pass_command. */
15396
15397 static void
15398 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
15399 {
15400 tp->pass_count = count;
15401 observer_notify_breakpoint_modified (&tp->base);
15402 if (from_tty)
15403 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
15404 tp->base.number, count);
15405 }
15406
15407 /* Set passcount for tracepoint.
15408
15409 First command argument is passcount, second is tracepoint number.
15410 If tracepoint number omitted, apply to most recently defined.
15411 Also accepts special argument "all". */
15412
15413 static void
15414 trace_pass_command (char *args, int from_tty)
15415 {
15416 struct tracepoint *t1;
15417 unsigned int count;
15418
15419 if (args == 0 || *args == 0)
15420 error (_("passcount command requires an "
15421 "argument (count + optional TP num)"));
15422
15423 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
15424
15425 args = skip_spaces (args);
15426 if (*args && strncasecmp (args, "all", 3) == 0)
15427 {
15428 struct breakpoint *b;
15429
15430 args += 3; /* Skip special argument "all". */
15431 if (*args)
15432 error (_("Junk at end of arguments."));
15433
15434 ALL_TRACEPOINTS (b)
15435 {
15436 t1 = (struct tracepoint *) b;
15437 trace_pass_set_count (t1, count, from_tty);
15438 }
15439 }
15440 else if (*args == '\0')
15441 {
15442 t1 = get_tracepoint_by_number (&args, NULL);
15443 if (t1)
15444 trace_pass_set_count (t1, count, from_tty);
15445 }
15446 else
15447 {
15448 struct get_number_or_range_state state;
15449
15450 init_number_or_range (&state, args);
15451 while (!state.finished)
15452 {
15453 t1 = get_tracepoint_by_number (&args, &state);
15454 if (t1)
15455 trace_pass_set_count (t1, count, from_tty);
15456 }
15457 }
15458 }
15459
15460 struct tracepoint *
15461 get_tracepoint (int num)
15462 {
15463 struct breakpoint *t;
15464
15465 ALL_TRACEPOINTS (t)
15466 if (t->number == num)
15467 return (struct tracepoint *) t;
15468
15469 return NULL;
15470 }
15471
15472 /* Find the tracepoint with the given target-side number (which may be
15473 different from the tracepoint number after disconnecting and
15474 reconnecting). */
15475
15476 struct tracepoint *
15477 get_tracepoint_by_number_on_target (int num)
15478 {
15479 struct breakpoint *b;
15480
15481 ALL_TRACEPOINTS (b)
15482 {
15483 struct tracepoint *t = (struct tracepoint *) b;
15484
15485 if (t->number_on_target == num)
15486 return t;
15487 }
15488
15489 return NULL;
15490 }
15491
15492 /* Utility: parse a tracepoint number and look it up in the list.
15493 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
15494 If the argument is missing, the most recent tracepoint
15495 (tracepoint_count) is returned. */
15496
15497 struct tracepoint *
15498 get_tracepoint_by_number (char **arg,
15499 struct get_number_or_range_state *state)
15500 {
15501 struct breakpoint *t;
15502 int tpnum;
15503 char *instring = arg == NULL ? NULL : *arg;
15504
15505 if (state)
15506 {
15507 gdb_assert (!state->finished);
15508 tpnum = get_number_or_range (state);
15509 }
15510 else if (arg == NULL || *arg == NULL || ! **arg)
15511 tpnum = tracepoint_count;
15512 else
15513 tpnum = get_number (arg);
15514
15515 if (tpnum <= 0)
15516 {
15517 if (instring && *instring)
15518 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15519 instring);
15520 else
15521 printf_filtered (_("No previous tracepoint\n"));
15522 return NULL;
15523 }
15524
15525 ALL_TRACEPOINTS (t)
15526 if (t->number == tpnum)
15527 {
15528 return (struct tracepoint *) t;
15529 }
15530
15531 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15532 return NULL;
15533 }
15534
15535 void
15536 print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15537 {
15538 if (b->thread != -1)
15539 fprintf_unfiltered (fp, " thread %d", b->thread);
15540
15541 if (b->task != 0)
15542 fprintf_unfiltered (fp, " task %d", b->task);
15543
15544 fprintf_unfiltered (fp, "\n");
15545 }
15546
15547 /* Save information on user settable breakpoints (watchpoints, etc) to
15548 a new script file named FILENAME. If FILTER is non-NULL, call it
15549 on each breakpoint and only include the ones for which it returns
15550 non-zero. */
15551
15552 static void
15553 save_breakpoints (char *filename, int from_tty,
15554 int (*filter) (const struct breakpoint *))
15555 {
15556 struct breakpoint *tp;
15557 int any = 0;
15558 struct cleanup *cleanup;
15559 struct ui_file *fp;
15560 int extra_trace_bits = 0;
15561
15562 if (filename == 0 || *filename == 0)
15563 error (_("Argument required (file name in which to save)"));
15564
15565 /* See if we have anything to save. */
15566 ALL_BREAKPOINTS (tp)
15567 {
15568 /* Skip internal and momentary breakpoints. */
15569 if (!user_breakpoint_p (tp))
15570 continue;
15571
15572 /* If we have a filter, only save the breakpoints it accepts. */
15573 if (filter && !filter (tp))
15574 continue;
15575
15576 any = 1;
15577
15578 if (is_tracepoint (tp))
15579 {
15580 extra_trace_bits = 1;
15581
15582 /* We can stop searching. */
15583 break;
15584 }
15585 }
15586
15587 if (!any)
15588 {
15589 warning (_("Nothing to save."));
15590 return;
15591 }
15592
15593 filename = tilde_expand (filename);
15594 cleanup = make_cleanup (xfree, filename);
15595 fp = gdb_fopen (filename, "w");
15596 if (!fp)
15597 error (_("Unable to open file '%s' for saving (%s)"),
15598 filename, safe_strerror (errno));
15599 make_cleanup_ui_file_delete (fp);
15600
15601 if (extra_trace_bits)
15602 save_trace_state_variables (fp);
15603
15604 ALL_BREAKPOINTS (tp)
15605 {
15606 /* Skip internal and momentary breakpoints. */
15607 if (!user_breakpoint_p (tp))
15608 continue;
15609
15610 /* If we have a filter, only save the breakpoints it accepts. */
15611 if (filter && !filter (tp))
15612 continue;
15613
15614 tp->ops->print_recreate (tp, fp);
15615
15616 /* Note, we can't rely on tp->number for anything, as we can't
15617 assume the recreated breakpoint numbers will match. Use $bpnum
15618 instead. */
15619
15620 if (tp->cond_string)
15621 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
15622
15623 if (tp->ignore_count)
15624 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
15625
15626 if (tp->type != bp_dprintf && tp->commands)
15627 {
15628 struct gdb_exception exception;
15629
15630 fprintf_unfiltered (fp, " commands\n");
15631
15632 ui_out_redirect (current_uiout, fp);
15633 TRY
15634 {
15635 print_command_lines (current_uiout, tp->commands->commands, 2);
15636 }
15637 CATCH (ex, RETURN_MASK_ALL)
15638 {
15639 ui_out_redirect (current_uiout, NULL);
15640 throw_exception (ex);
15641 }
15642 END_CATCH
15643
15644 ui_out_redirect (current_uiout, NULL);
15645 fprintf_unfiltered (fp, " end\n");
15646 }
15647
15648 if (tp->enable_state == bp_disabled)
15649 fprintf_unfiltered (fp, "disable $bpnum\n");
15650
15651 /* If this is a multi-location breakpoint, check if the locations
15652 should be individually disabled. Watchpoint locations are
15653 special, and not user visible. */
15654 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15655 {
15656 struct bp_location *loc;
15657 int n = 1;
15658
15659 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15660 if (!loc->enabled)
15661 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
15662 }
15663 }
15664
15665 if (extra_trace_bits && *default_collect)
15666 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
15667
15668 if (from_tty)
15669 printf_filtered (_("Saved to file '%s'.\n"), filename);
15670 do_cleanups (cleanup);
15671 }
15672
15673 /* The `save breakpoints' command. */
15674
15675 static void
15676 save_breakpoints_command (char *args, int from_tty)
15677 {
15678 save_breakpoints (args, from_tty, NULL);
15679 }
15680
15681 /* The `save tracepoints' command. */
15682
15683 static void
15684 save_tracepoints_command (char *args, int from_tty)
15685 {
15686 save_breakpoints (args, from_tty, is_tracepoint);
15687 }
15688
15689 /* Create a vector of all tracepoints. */
15690
15691 VEC(breakpoint_p) *
15692 all_tracepoints (void)
15693 {
15694 VEC(breakpoint_p) *tp_vec = 0;
15695 struct breakpoint *tp;
15696
15697 ALL_TRACEPOINTS (tp)
15698 {
15699 VEC_safe_push (breakpoint_p, tp_vec, tp);
15700 }
15701
15702 return tp_vec;
15703 }
15704
15705 \f
15706 /* This help string is used to consolidate all the help string for specifying
15707 locations used by several commands. */
15708
15709 #define LOCATION_HELP_STRING \
15710 "Linespecs are colon-separated lists of location parameters, such as\n\
15711 source filename, function name, label name, and line number.\n\
15712 Example: To specify the start of a label named \"the_top\" in the\n\
15713 function \"fact\" in the file \"factorial.c\", use\n\
15714 \"factorial.c:fact:the_top\".\n\
15715 \n\
15716 Address locations begin with \"*\" and specify an exact address in the\n\
15717 program. Example: To specify the fourth byte past the start function\n\
15718 \"main\", use \"*main + 4\".\n\
15719 \n\
15720 Explicit locations are similar to linespecs but use an option/argument\n\
15721 syntax to specify location parameters.\n\
15722 Example: To specify the start of the label named \"the_top\" in the\n\
15723 function \"fact\" in the file \"factorial.c\", use \"-source factorial.c\n\
15724 -function fact -label the_top\".\n"
15725
15726 /* This help string is used for the break, hbreak, tbreak and thbreak
15727 commands. It is defined as a macro to prevent duplication.
15728 COMMAND should be a string constant containing the name of the
15729 command. */
15730
15731 #define BREAK_ARGS_HELP(command) \
15732 command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15733 PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15734 probe point. Accepted values are `-probe' (for a generic, automatically\n\
15735 guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15736 `-probe-dtrace' (for a DTrace probe).\n\
15737 LOCATION may be a linespec, address, or explicit location as described\n\
15738 below.\n\
15739 \n\
15740 With no LOCATION, uses current execution address of the selected\n\
15741 stack frame. This is useful for breaking on return to a stack frame.\n\
15742 \n\
15743 THREADNUM is the number from \"info threads\".\n\
15744 CONDITION is a boolean expression.\n\
15745 \n" LOCATION_HELP_STRING "\n\
15746 Multiple breakpoints at one place are permitted, and useful if their\n\
15747 conditions are different.\n\
15748 \n\
15749 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15750
15751 /* List of subcommands for "catch". */
15752 static struct cmd_list_element *catch_cmdlist;
15753
15754 /* List of subcommands for "tcatch". */
15755 static struct cmd_list_element *tcatch_cmdlist;
15756
15757 void
15758 add_catch_command (char *name, char *docstring,
15759 cmd_sfunc_ftype *sfunc,
15760 completer_ftype *completer,
15761 void *user_data_catch,
15762 void *user_data_tcatch)
15763 {
15764 struct cmd_list_element *command;
15765
15766 command = add_cmd (name, class_breakpoint, NULL, docstring,
15767 &catch_cmdlist);
15768 set_cmd_sfunc (command, sfunc);
15769 set_cmd_context (command, user_data_catch);
15770 set_cmd_completer (command, completer);
15771
15772 command = add_cmd (name, class_breakpoint, NULL, docstring,
15773 &tcatch_cmdlist);
15774 set_cmd_sfunc (command, sfunc);
15775 set_cmd_context (command, user_data_tcatch);
15776 set_cmd_completer (command, completer);
15777 }
15778
15779 static void
15780 save_command (char *arg, int from_tty)
15781 {
15782 printf_unfiltered (_("\"save\" must be followed by "
15783 "the name of a save subcommand.\n"));
15784 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
15785 }
15786
15787 struct breakpoint *
15788 iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15789 void *data)
15790 {
15791 struct breakpoint *b, *b_tmp;
15792
15793 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15794 {
15795 if ((*callback) (b, data))
15796 return b;
15797 }
15798
15799 return NULL;
15800 }
15801
15802 /* Zero if any of the breakpoint's locations could be a location where
15803 functions have been inlined, nonzero otherwise. */
15804
15805 static int
15806 is_non_inline_function (struct breakpoint *b)
15807 {
15808 /* The shared library event breakpoint is set on the address of a
15809 non-inline function. */
15810 if (b->type == bp_shlib_event)
15811 return 1;
15812
15813 return 0;
15814 }
15815
15816 /* Nonzero if the specified PC cannot be a location where functions
15817 have been inlined. */
15818
15819 int
15820 pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
15821 const struct target_waitstatus *ws)
15822 {
15823 struct breakpoint *b;
15824 struct bp_location *bl;
15825
15826 ALL_BREAKPOINTS (b)
15827 {
15828 if (!is_non_inline_function (b))
15829 continue;
15830
15831 for (bl = b->loc; bl != NULL; bl = bl->next)
15832 {
15833 if (!bl->shlib_disabled
15834 && bpstat_check_location (bl, aspace, pc, ws))
15835 return 1;
15836 }
15837 }
15838
15839 return 0;
15840 }
15841
15842 /* Remove any references to OBJFILE which is going to be freed. */
15843
15844 void
15845 breakpoint_free_objfile (struct objfile *objfile)
15846 {
15847 struct bp_location **locp, *loc;
15848
15849 ALL_BP_LOCATIONS (loc, locp)
15850 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
15851 loc->symtab = NULL;
15852 }
15853
15854 void
15855 initialize_breakpoint_ops (void)
15856 {
15857 static int initialized = 0;
15858
15859 struct breakpoint_ops *ops;
15860
15861 if (initialized)
15862 return;
15863 initialized = 1;
15864
15865 /* The breakpoint_ops structure to be inherit by all kinds of
15866 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15867 internal and momentary breakpoints, etc.). */
15868 ops = &bkpt_base_breakpoint_ops;
15869 *ops = base_breakpoint_ops;
15870 ops->re_set = bkpt_re_set;
15871 ops->insert_location = bkpt_insert_location;
15872 ops->remove_location = bkpt_remove_location;
15873 ops->breakpoint_hit = bkpt_breakpoint_hit;
15874 ops->create_sals_from_location = bkpt_create_sals_from_location;
15875 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
15876 ops->decode_location = bkpt_decode_location;
15877
15878 /* The breakpoint_ops structure to be used in regular breakpoints. */
15879 ops = &bkpt_breakpoint_ops;
15880 *ops = bkpt_base_breakpoint_ops;
15881 ops->re_set = bkpt_re_set;
15882 ops->resources_needed = bkpt_resources_needed;
15883 ops->print_it = bkpt_print_it;
15884 ops->print_mention = bkpt_print_mention;
15885 ops->print_recreate = bkpt_print_recreate;
15886
15887 /* Ranged breakpoints. */
15888 ops = &ranged_breakpoint_ops;
15889 *ops = bkpt_breakpoint_ops;
15890 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15891 ops->resources_needed = resources_needed_ranged_breakpoint;
15892 ops->print_it = print_it_ranged_breakpoint;
15893 ops->print_one = print_one_ranged_breakpoint;
15894 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15895 ops->print_mention = print_mention_ranged_breakpoint;
15896 ops->print_recreate = print_recreate_ranged_breakpoint;
15897
15898 /* Internal breakpoints. */
15899 ops = &internal_breakpoint_ops;
15900 *ops = bkpt_base_breakpoint_ops;
15901 ops->re_set = internal_bkpt_re_set;
15902 ops->check_status = internal_bkpt_check_status;
15903 ops->print_it = internal_bkpt_print_it;
15904 ops->print_mention = internal_bkpt_print_mention;
15905
15906 /* Momentary breakpoints. */
15907 ops = &momentary_breakpoint_ops;
15908 *ops = bkpt_base_breakpoint_ops;
15909 ops->re_set = momentary_bkpt_re_set;
15910 ops->check_status = momentary_bkpt_check_status;
15911 ops->print_it = momentary_bkpt_print_it;
15912 ops->print_mention = momentary_bkpt_print_mention;
15913
15914 /* Momentary breakpoints for bp_longjmp and bp_exception. */
15915 ops = &longjmp_breakpoint_ops;
15916 *ops = momentary_breakpoint_ops;
15917 ops->dtor = longjmp_bkpt_dtor;
15918
15919 /* Probe breakpoints. */
15920 ops = &bkpt_probe_breakpoint_ops;
15921 *ops = bkpt_breakpoint_ops;
15922 ops->insert_location = bkpt_probe_insert_location;
15923 ops->remove_location = bkpt_probe_remove_location;
15924 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
15925 ops->decode_location = bkpt_probe_decode_location;
15926
15927 /* Watchpoints. */
15928 ops = &watchpoint_breakpoint_ops;
15929 *ops = base_breakpoint_ops;
15930 ops->dtor = dtor_watchpoint;
15931 ops->re_set = re_set_watchpoint;
15932 ops->insert_location = insert_watchpoint;
15933 ops->remove_location = remove_watchpoint;
15934 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15935 ops->check_status = check_status_watchpoint;
15936 ops->resources_needed = resources_needed_watchpoint;
15937 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15938 ops->print_it = print_it_watchpoint;
15939 ops->print_mention = print_mention_watchpoint;
15940 ops->print_recreate = print_recreate_watchpoint;
15941 ops->explains_signal = explains_signal_watchpoint;
15942
15943 /* Masked watchpoints. */
15944 ops = &masked_watchpoint_breakpoint_ops;
15945 *ops = watchpoint_breakpoint_ops;
15946 ops->insert_location = insert_masked_watchpoint;
15947 ops->remove_location = remove_masked_watchpoint;
15948 ops->resources_needed = resources_needed_masked_watchpoint;
15949 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15950 ops->print_it = print_it_masked_watchpoint;
15951 ops->print_one_detail = print_one_detail_masked_watchpoint;
15952 ops->print_mention = print_mention_masked_watchpoint;
15953 ops->print_recreate = print_recreate_masked_watchpoint;
15954
15955 /* Tracepoints. */
15956 ops = &tracepoint_breakpoint_ops;
15957 *ops = base_breakpoint_ops;
15958 ops->re_set = tracepoint_re_set;
15959 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15960 ops->print_one_detail = tracepoint_print_one_detail;
15961 ops->print_mention = tracepoint_print_mention;
15962 ops->print_recreate = tracepoint_print_recreate;
15963 ops->create_sals_from_location = tracepoint_create_sals_from_location;
15964 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
15965 ops->decode_location = tracepoint_decode_location;
15966
15967 /* Probe tracepoints. */
15968 ops = &tracepoint_probe_breakpoint_ops;
15969 *ops = tracepoint_breakpoint_ops;
15970 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
15971 ops->decode_location = tracepoint_probe_decode_location;
15972
15973 /* Static tracepoints with marker (`-m'). */
15974 ops = &strace_marker_breakpoint_ops;
15975 *ops = tracepoint_breakpoint_ops;
15976 ops->create_sals_from_location = strace_marker_create_sals_from_location;
15977 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
15978 ops->decode_location = strace_marker_decode_location;
15979
15980 /* Fork catchpoints. */
15981 ops = &catch_fork_breakpoint_ops;
15982 *ops = base_breakpoint_ops;
15983 ops->insert_location = insert_catch_fork;
15984 ops->remove_location = remove_catch_fork;
15985 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15986 ops->print_it = print_it_catch_fork;
15987 ops->print_one = print_one_catch_fork;
15988 ops->print_mention = print_mention_catch_fork;
15989 ops->print_recreate = print_recreate_catch_fork;
15990
15991 /* Vfork catchpoints. */
15992 ops = &catch_vfork_breakpoint_ops;
15993 *ops = base_breakpoint_ops;
15994 ops->insert_location = insert_catch_vfork;
15995 ops->remove_location = remove_catch_vfork;
15996 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15997 ops->print_it = print_it_catch_vfork;
15998 ops->print_one = print_one_catch_vfork;
15999 ops->print_mention = print_mention_catch_vfork;
16000 ops->print_recreate = print_recreate_catch_vfork;
16001
16002 /* Exec catchpoints. */
16003 ops = &catch_exec_breakpoint_ops;
16004 *ops = base_breakpoint_ops;
16005 ops->dtor = dtor_catch_exec;
16006 ops->insert_location = insert_catch_exec;
16007 ops->remove_location = remove_catch_exec;
16008 ops->breakpoint_hit = breakpoint_hit_catch_exec;
16009 ops->print_it = print_it_catch_exec;
16010 ops->print_one = print_one_catch_exec;
16011 ops->print_mention = print_mention_catch_exec;
16012 ops->print_recreate = print_recreate_catch_exec;
16013
16014 /* Solib-related catchpoints. */
16015 ops = &catch_solib_breakpoint_ops;
16016 *ops = base_breakpoint_ops;
16017 ops->dtor = dtor_catch_solib;
16018 ops->insert_location = insert_catch_solib;
16019 ops->remove_location = remove_catch_solib;
16020 ops->breakpoint_hit = breakpoint_hit_catch_solib;
16021 ops->check_status = check_status_catch_solib;
16022 ops->print_it = print_it_catch_solib;
16023 ops->print_one = print_one_catch_solib;
16024 ops->print_mention = print_mention_catch_solib;
16025 ops->print_recreate = print_recreate_catch_solib;
16026
16027 ops = &dprintf_breakpoint_ops;
16028 *ops = bkpt_base_breakpoint_ops;
16029 ops->re_set = dprintf_re_set;
16030 ops->resources_needed = bkpt_resources_needed;
16031 ops->print_it = bkpt_print_it;
16032 ops->print_mention = bkpt_print_mention;
16033 ops->print_recreate = dprintf_print_recreate;
16034 ops->after_condition_true = dprintf_after_condition_true;
16035 ops->breakpoint_hit = dprintf_breakpoint_hit;
16036 }
16037
16038 /* Chain containing all defined "enable breakpoint" subcommands. */
16039
16040 static struct cmd_list_element *enablebreaklist = NULL;
16041
16042 void
16043 _initialize_breakpoint (void)
16044 {
16045 struct cmd_list_element *c;
16046
16047 initialize_breakpoint_ops ();
16048
16049 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
16050 observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
16051 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
16052
16053 breakpoint_objfile_key
16054 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
16055
16056 breakpoint_chain = 0;
16057 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
16058 before a breakpoint is set. */
16059 breakpoint_count = 0;
16060
16061 tracepoint_count = 0;
16062
16063 add_com ("ignore", class_breakpoint, ignore_command, _("\
16064 Set ignore-count of breakpoint number N to COUNT.\n\
16065 Usage is `ignore N COUNT'."));
16066
16067 add_com ("commands", class_breakpoint, commands_command, _("\
16068 Set commands to be executed when a breakpoint is hit.\n\
16069 Give breakpoint number as argument after \"commands\".\n\
16070 With no argument, the targeted breakpoint is the last one set.\n\
16071 The commands themselves follow starting on the next line.\n\
16072 Type a line containing \"end\" to indicate the end of them.\n\
16073 Give \"silent\" as the first line to make the breakpoint silent;\n\
16074 then no output is printed when it is hit, except what the commands print."));
16075
16076 c = add_com ("condition", class_breakpoint, condition_command, _("\
16077 Specify breakpoint number N to break only if COND is true.\n\
16078 Usage is `condition N COND', where N is an integer and COND is an\n\
16079 expression to be evaluated whenever breakpoint N is reached."));
16080 set_cmd_completer (c, condition_completer);
16081
16082 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
16083 Set a temporary breakpoint.\n\
16084 Like \"break\" except the breakpoint is only temporary,\n\
16085 so it will be deleted when hit. Equivalent to \"break\" followed\n\
16086 by using \"enable delete\" on the breakpoint number.\n\
16087 \n"
16088 BREAK_ARGS_HELP ("tbreak")));
16089 set_cmd_completer (c, location_completer);
16090
16091 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
16092 Set a hardware assisted breakpoint.\n\
16093 Like \"break\" except the breakpoint requires hardware support,\n\
16094 some target hardware may not have this support.\n\
16095 \n"
16096 BREAK_ARGS_HELP ("hbreak")));
16097 set_cmd_completer (c, location_completer);
16098
16099 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
16100 Set a temporary hardware assisted breakpoint.\n\
16101 Like \"hbreak\" except the breakpoint is only temporary,\n\
16102 so it will be deleted when hit.\n\
16103 \n"
16104 BREAK_ARGS_HELP ("thbreak")));
16105 set_cmd_completer (c, location_completer);
16106
16107 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
16108 Enable some breakpoints.\n\
16109 Give breakpoint numbers (separated by spaces) as arguments.\n\
16110 With no subcommand, breakpoints are enabled until you command otherwise.\n\
16111 This is used to cancel the effect of the \"disable\" command.\n\
16112 With a subcommand you can enable temporarily."),
16113 &enablelist, "enable ", 1, &cmdlist);
16114
16115 add_com_alias ("en", "enable", class_breakpoint, 1);
16116
16117 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
16118 Enable some breakpoints.\n\
16119 Give breakpoint numbers (separated by spaces) as arguments.\n\
16120 This is used to cancel the effect of the \"disable\" command.\n\
16121 May be abbreviated to simply \"enable\".\n"),
16122 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
16123
16124 add_cmd ("once", no_class, enable_once_command, _("\
16125 Enable breakpoints for one hit. Give breakpoint numbers.\n\
16126 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
16127 &enablebreaklist);
16128
16129 add_cmd ("delete", no_class, enable_delete_command, _("\
16130 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16131 If a breakpoint is hit while enabled in this fashion, it is deleted."),
16132 &enablebreaklist);
16133
16134 add_cmd ("count", no_class, enable_count_command, _("\
16135 Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16136 If a breakpoint is hit while enabled in this fashion,\n\
16137 the count is decremented; when it reaches zero, the breakpoint is disabled."),
16138 &enablebreaklist);
16139
16140 add_cmd ("delete", no_class, enable_delete_command, _("\
16141 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
16142 If a breakpoint is hit while enabled in this fashion, it is deleted."),
16143 &enablelist);
16144
16145 add_cmd ("once", no_class, enable_once_command, _("\
16146 Enable breakpoints for one hit. Give breakpoint numbers.\n\
16147 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
16148 &enablelist);
16149
16150 add_cmd ("count", no_class, enable_count_command, _("\
16151 Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
16152 If a breakpoint is hit while enabled in this fashion,\n\
16153 the count is decremented; when it reaches zero, the breakpoint is disabled."),
16154 &enablelist);
16155
16156 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
16157 Disable some breakpoints.\n\
16158 Arguments are breakpoint numbers with spaces in between.\n\
16159 To disable all breakpoints, give no argument.\n\
16160 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
16161 &disablelist, "disable ", 1, &cmdlist);
16162 add_com_alias ("dis", "disable", class_breakpoint, 1);
16163 add_com_alias ("disa", "disable", class_breakpoint, 1);
16164
16165 add_cmd ("breakpoints", class_alias, disable_command, _("\
16166 Disable some breakpoints.\n\
16167 Arguments are breakpoint numbers with spaces in between.\n\
16168 To disable all breakpoints, give no argument.\n\
16169 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
16170 This command may be abbreviated \"disable\"."),
16171 &disablelist);
16172
16173 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
16174 Delete some breakpoints or auto-display expressions.\n\
16175 Arguments are breakpoint numbers with spaces in between.\n\
16176 To delete all breakpoints, give no argument.\n\
16177 \n\
16178 Also a prefix command for deletion of other GDB objects.\n\
16179 The \"unset\" command is also an alias for \"delete\"."),
16180 &deletelist, "delete ", 1, &cmdlist);
16181 add_com_alias ("d", "delete", class_breakpoint, 1);
16182 add_com_alias ("del", "delete", class_breakpoint, 1);
16183
16184 add_cmd ("breakpoints", class_alias, delete_command, _("\
16185 Delete some breakpoints or auto-display expressions.\n\
16186 Arguments are breakpoint numbers with spaces in between.\n\
16187 To delete all breakpoints, give no argument.\n\
16188 This command may be abbreviated \"delete\"."),
16189 &deletelist);
16190
16191 add_com ("clear", class_breakpoint, clear_command, _("\
16192 Clear breakpoint at specified location.\n\
16193 Argument may be a linespec, explicit, or address location as described below.\n\
16194 \n\
16195 With no argument, clears all breakpoints in the line that the selected frame\n\
16196 is executing in.\n"
16197 "\n" LOCATION_HELP_STRING "\n\
16198 See also the \"delete\" command which clears breakpoints by number."));
16199 add_com_alias ("cl", "clear", class_breakpoint, 1);
16200
16201 c = add_com ("break", class_breakpoint, break_command, _("\
16202 Set breakpoint at specified location.\n"
16203 BREAK_ARGS_HELP ("break")));
16204 set_cmd_completer (c, location_completer);
16205
16206 add_com_alias ("b", "break", class_run, 1);
16207 add_com_alias ("br", "break", class_run, 1);
16208 add_com_alias ("bre", "break", class_run, 1);
16209 add_com_alias ("brea", "break", class_run, 1);
16210
16211 if (dbx_commands)
16212 {
16213 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
16214 Break in function/address or break at a line in the current file."),
16215 &stoplist, "stop ", 1, &cmdlist);
16216 add_cmd ("in", class_breakpoint, stopin_command,
16217 _("Break in function or address."), &stoplist);
16218 add_cmd ("at", class_breakpoint, stopat_command,
16219 _("Break at a line in the current file."), &stoplist);
16220 add_com ("status", class_info, breakpoints_info, _("\
16221 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
16222 The \"Type\" column indicates one of:\n\
16223 \tbreakpoint - normal breakpoint\n\
16224 \twatchpoint - watchpoint\n\
16225 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16226 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16227 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16228 address and file/line number respectively.\n\
16229 \n\
16230 Convenience variable \"$_\" and default examine address for \"x\"\n\
16231 are set to the address of the last breakpoint listed unless the command\n\
16232 is prefixed with \"server \".\n\n\
16233 Convenience variable \"$bpnum\" contains the number of the last\n\
16234 breakpoint set."));
16235 }
16236
16237 add_info ("breakpoints", breakpoints_info, _("\
16238 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
16239 The \"Type\" column indicates one of:\n\
16240 \tbreakpoint - normal breakpoint\n\
16241 \twatchpoint - watchpoint\n\
16242 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16243 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16244 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16245 address and file/line number respectively.\n\
16246 \n\
16247 Convenience variable \"$_\" and default examine address for \"x\"\n\
16248 are set to the address of the last breakpoint listed unless the command\n\
16249 is prefixed with \"server \".\n\n\
16250 Convenience variable \"$bpnum\" contains the number of the last\n\
16251 breakpoint set."));
16252
16253 add_info_alias ("b", "breakpoints", 1);
16254
16255 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
16256 Status of all breakpoints, or breakpoint number NUMBER.\n\
16257 The \"Type\" column indicates one of:\n\
16258 \tbreakpoint - normal breakpoint\n\
16259 \twatchpoint - watchpoint\n\
16260 \tlongjmp - internal breakpoint used to step through longjmp()\n\
16261 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
16262 \tuntil - internal breakpoint used by the \"until\" command\n\
16263 \tfinish - internal breakpoint used by the \"finish\" command\n\
16264 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16265 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16266 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16267 address and file/line number respectively.\n\
16268 \n\
16269 Convenience variable \"$_\" and default examine address for \"x\"\n\
16270 are set to the address of the last breakpoint listed unless the command\n\
16271 is prefixed with \"server \".\n\n\
16272 Convenience variable \"$bpnum\" contains the number of the last\n\
16273 breakpoint set."),
16274 &maintenanceinfolist);
16275
16276 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
16277 Set catchpoints to catch events."),
16278 &catch_cmdlist, "catch ",
16279 0/*allow-unknown*/, &cmdlist);
16280
16281 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
16282 Set temporary catchpoints to catch events."),
16283 &tcatch_cmdlist, "tcatch ",
16284 0/*allow-unknown*/, &cmdlist);
16285
16286 add_catch_command ("fork", _("Catch calls to fork."),
16287 catch_fork_command_1,
16288 NULL,
16289 (void *) (uintptr_t) catch_fork_permanent,
16290 (void *) (uintptr_t) catch_fork_temporary);
16291 add_catch_command ("vfork", _("Catch calls to vfork."),
16292 catch_fork_command_1,
16293 NULL,
16294 (void *) (uintptr_t) catch_vfork_permanent,
16295 (void *) (uintptr_t) catch_vfork_temporary);
16296 add_catch_command ("exec", _("Catch calls to exec."),
16297 catch_exec_command_1,
16298 NULL,
16299 CATCH_PERMANENT,
16300 CATCH_TEMPORARY);
16301 add_catch_command ("load", _("Catch loads of shared libraries.\n\
16302 Usage: catch load [REGEX]\n\
16303 If REGEX is given, only stop for libraries matching the regular expression."),
16304 catch_load_command_1,
16305 NULL,
16306 CATCH_PERMANENT,
16307 CATCH_TEMPORARY);
16308 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
16309 Usage: catch unload [REGEX]\n\
16310 If REGEX is given, only stop for libraries matching the regular expression."),
16311 catch_unload_command_1,
16312 NULL,
16313 CATCH_PERMANENT,
16314 CATCH_TEMPORARY);
16315
16316 c = add_com ("watch", class_breakpoint, watch_command, _("\
16317 Set a watchpoint for an expression.\n\
16318 Usage: watch [-l|-location] EXPRESSION\n\
16319 A watchpoint stops execution of your program whenever the value of\n\
16320 an expression changes.\n\
16321 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16322 the memory to which it refers."));
16323 set_cmd_completer (c, expression_completer);
16324
16325 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
16326 Set a read watchpoint for an expression.\n\
16327 Usage: rwatch [-l|-location] EXPRESSION\n\
16328 A watchpoint stops execution of your program whenever the value of\n\
16329 an expression is read.\n\
16330 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16331 the memory to which it refers."));
16332 set_cmd_completer (c, expression_completer);
16333
16334 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
16335 Set a watchpoint for an expression.\n\
16336 Usage: awatch [-l|-location] EXPRESSION\n\
16337 A watchpoint stops execution of your program whenever the value of\n\
16338 an expression is either read or written.\n\
16339 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16340 the memory to which it refers."));
16341 set_cmd_completer (c, expression_completer);
16342
16343 add_info ("watchpoints", watchpoints_info, _("\
16344 Status of specified watchpoints (all watchpoints if no argument)."));
16345
16346 /* XXX: cagney/2005-02-23: This should be a boolean, and should
16347 respond to changes - contrary to the description. */
16348 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
16349 &can_use_hw_watchpoints, _("\
16350 Set debugger's willingness to use watchpoint hardware."), _("\
16351 Show debugger's willingness to use watchpoint hardware."), _("\
16352 If zero, gdb will not use hardware for new watchpoints, even if\n\
16353 such is available. (However, any hardware watchpoints that were\n\
16354 created before setting this to nonzero, will continue to use watchpoint\n\
16355 hardware.)"),
16356 NULL,
16357 show_can_use_hw_watchpoints,
16358 &setlist, &showlist);
16359
16360 can_use_hw_watchpoints = 1;
16361
16362 /* Tracepoint manipulation commands. */
16363
16364 c = add_com ("trace", class_breakpoint, trace_command, _("\
16365 Set a tracepoint at specified location.\n\
16366 \n"
16367 BREAK_ARGS_HELP ("trace") "\n\
16368 Do \"help tracepoints\" for info on other tracepoint commands."));
16369 set_cmd_completer (c, location_completer);
16370
16371 add_com_alias ("tp", "trace", class_alias, 0);
16372 add_com_alias ("tr", "trace", class_alias, 1);
16373 add_com_alias ("tra", "trace", class_alias, 1);
16374 add_com_alias ("trac", "trace", class_alias, 1);
16375
16376 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
16377 Set a fast tracepoint at specified location.\n\
16378 \n"
16379 BREAK_ARGS_HELP ("ftrace") "\n\
16380 Do \"help tracepoints\" for info on other tracepoint commands."));
16381 set_cmd_completer (c, location_completer);
16382
16383 c = add_com ("strace", class_breakpoint, strace_command, _("\
16384 Set a static tracepoint at location or marker.\n\
16385 \n\
16386 strace [LOCATION] [if CONDITION]\n\
16387 LOCATION may be a linespec, explicit, or address location (described below) \n\
16388 or -m MARKER_ID.\n\n\
16389 If a marker id is specified, probe the marker with that name. With\n\
16390 no LOCATION, uses current execution address of the selected stack frame.\n\
16391 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16392 This collects arbitrary user data passed in the probe point call to the\n\
16393 tracing library. You can inspect it when analyzing the trace buffer,\n\
16394 by printing the $_sdata variable like any other convenience variable.\n\
16395 \n\
16396 CONDITION is a boolean expression.\n\
16397 \n" LOCATION_HELP_STRING "\n\
16398 Multiple tracepoints at one place are permitted, and useful if their\n\
16399 conditions are different.\n\
16400 \n\
16401 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16402 Do \"help tracepoints\" for info on other tracepoint commands."));
16403 set_cmd_completer (c, location_completer);
16404
16405 add_info ("tracepoints", tracepoints_info, _("\
16406 Status of specified tracepoints (all tracepoints if no argument).\n\
16407 Convenience variable \"$tpnum\" contains the number of the\n\
16408 last tracepoint set."));
16409
16410 add_info_alias ("tp", "tracepoints", 1);
16411
16412 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16413 Delete specified tracepoints.\n\
16414 Arguments are tracepoint numbers, separated by spaces.\n\
16415 No argument means delete all tracepoints."),
16416 &deletelist);
16417 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
16418
16419 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16420 Disable specified tracepoints.\n\
16421 Arguments are tracepoint numbers, separated by spaces.\n\
16422 No argument means disable all tracepoints."),
16423 &disablelist);
16424 deprecate_cmd (c, "disable");
16425
16426 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16427 Enable specified tracepoints.\n\
16428 Arguments are tracepoint numbers, separated by spaces.\n\
16429 No argument means enable all tracepoints."),
16430 &enablelist);
16431 deprecate_cmd (c, "enable");
16432
16433 add_com ("passcount", class_trace, trace_pass_command, _("\
16434 Set the passcount for a tracepoint.\n\
16435 The trace will end when the tracepoint has been passed 'count' times.\n\
16436 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16437 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16438
16439 add_prefix_cmd ("save", class_breakpoint, save_command,
16440 _("Save breakpoint definitions as a script."),
16441 &save_cmdlist, "save ",
16442 0/*allow-unknown*/, &cmdlist);
16443
16444 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16445 Save current breakpoint definitions as a script.\n\
16446 This includes all types of breakpoints (breakpoints, watchpoints,\n\
16447 catchpoints, tracepoints). Use the 'source' command in another debug\n\
16448 session to restore them."),
16449 &save_cmdlist);
16450 set_cmd_completer (c, filename_completer);
16451
16452 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
16453 Save current tracepoint definitions as a script.\n\
16454 Use the 'source' command in another debug session to restore them."),
16455 &save_cmdlist);
16456 set_cmd_completer (c, filename_completer);
16457
16458 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16459 deprecate_cmd (c, "save tracepoints");
16460
16461 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
16462 Breakpoint specific settings\n\
16463 Configure various breakpoint-specific variables such as\n\
16464 pending breakpoint behavior"),
16465 &breakpoint_set_cmdlist, "set breakpoint ",
16466 0/*allow-unknown*/, &setlist);
16467 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
16468 Breakpoint specific settings\n\
16469 Configure various breakpoint-specific variables such as\n\
16470 pending breakpoint behavior"),
16471 &breakpoint_show_cmdlist, "show breakpoint ",
16472 0/*allow-unknown*/, &showlist);
16473
16474 add_setshow_auto_boolean_cmd ("pending", no_class,
16475 &pending_break_support, _("\
16476 Set debugger's behavior regarding pending breakpoints."), _("\
16477 Show debugger's behavior regarding pending breakpoints."), _("\
16478 If on, an unrecognized breakpoint location will cause gdb to create a\n\
16479 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16480 an error. If auto, an unrecognized breakpoint location results in a\n\
16481 user-query to see if a pending breakpoint should be created."),
16482 NULL,
16483 show_pending_break_support,
16484 &breakpoint_set_cmdlist,
16485 &breakpoint_show_cmdlist);
16486
16487 pending_break_support = AUTO_BOOLEAN_AUTO;
16488
16489 add_setshow_boolean_cmd ("auto-hw", no_class,
16490 &automatic_hardware_breakpoints, _("\
16491 Set automatic usage of hardware breakpoints."), _("\
16492 Show automatic usage of hardware breakpoints."), _("\
16493 If set, the debugger will automatically use hardware breakpoints for\n\
16494 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16495 a warning will be emitted for such breakpoints."),
16496 NULL,
16497 show_automatic_hardware_breakpoints,
16498 &breakpoint_set_cmdlist,
16499 &breakpoint_show_cmdlist);
16500
16501 add_setshow_boolean_cmd ("always-inserted", class_support,
16502 &always_inserted_mode, _("\
16503 Set mode for inserting breakpoints."), _("\
16504 Show mode for inserting breakpoints."), _("\
16505 When this mode is on, breakpoints are inserted immediately as soon as\n\
16506 they're created, kept inserted even when execution stops, and removed\n\
16507 only when the user deletes them. When this mode is off (the default),\n\
16508 breakpoints are inserted only when execution continues, and removed\n\
16509 when execution stops."),
16510 NULL,
16511 &show_always_inserted_mode,
16512 &breakpoint_set_cmdlist,
16513 &breakpoint_show_cmdlist);
16514
16515 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16516 condition_evaluation_enums,
16517 &condition_evaluation_mode_1, _("\
16518 Set mode of breakpoint condition evaluation."), _("\
16519 Show mode of breakpoint condition evaluation."), _("\
16520 When this is set to \"host\", breakpoint conditions will be\n\
16521 evaluated on the host's side by GDB. When it is set to \"target\",\n\
16522 breakpoint conditions will be downloaded to the target (if the target\n\
16523 supports such feature) and conditions will be evaluated on the target's side.\n\
16524 If this is set to \"auto\" (default), this will be automatically set to\n\
16525 \"target\" if it supports condition evaluation, otherwise it will\n\
16526 be set to \"gdb\""),
16527 &set_condition_evaluation_mode,
16528 &show_condition_evaluation_mode,
16529 &breakpoint_set_cmdlist,
16530 &breakpoint_show_cmdlist);
16531
16532 add_com ("break-range", class_breakpoint, break_range_command, _("\
16533 Set a breakpoint for an address range.\n\
16534 break-range START-LOCATION, END-LOCATION\n\
16535 where START-LOCATION and END-LOCATION can be one of the following:\n\
16536 LINENUM, for that line in the current file,\n\
16537 FILE:LINENUM, for that line in that file,\n\
16538 +OFFSET, for that number of lines after the current line\n\
16539 or the start of the range\n\
16540 FUNCTION, for the first line in that function,\n\
16541 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16542 *ADDRESS, for the instruction at that address.\n\
16543 \n\
16544 The breakpoint will stop execution of the inferior whenever it executes\n\
16545 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16546 range (including START-LOCATION and END-LOCATION)."));
16547
16548 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
16549 Set a dynamic printf at specified location.\n\
16550 dprintf location,format string,arg1,arg2,...\n\
16551 location may be a linespec, explicit, or address location.\n"
16552 "\n" LOCATION_HELP_STRING));
16553 set_cmd_completer (c, location_completer);
16554
16555 add_setshow_enum_cmd ("dprintf-style", class_support,
16556 dprintf_style_enums, &dprintf_style, _("\
16557 Set the style of usage for dynamic printf."), _("\
16558 Show the style of usage for dynamic printf."), _("\
16559 This setting chooses how GDB will do a dynamic printf.\n\
16560 If the value is \"gdb\", then the printing is done by GDB to its own\n\
16561 console, as with the \"printf\" command.\n\
16562 If the value is \"call\", the print is done by calling a function in your\n\
16563 program; by default printf(), but you can choose a different function or\n\
16564 output stream by setting dprintf-function and dprintf-channel."),
16565 update_dprintf_commands, NULL,
16566 &setlist, &showlist);
16567
16568 dprintf_function = xstrdup ("printf");
16569 add_setshow_string_cmd ("dprintf-function", class_support,
16570 &dprintf_function, _("\
16571 Set the function to use for dynamic printf"), _("\
16572 Show the function to use for dynamic printf"), NULL,
16573 update_dprintf_commands, NULL,
16574 &setlist, &showlist);
16575
16576 dprintf_channel = xstrdup ("");
16577 add_setshow_string_cmd ("dprintf-channel", class_support,
16578 &dprintf_channel, _("\
16579 Set the channel to use for dynamic printf"), _("\
16580 Show the channel to use for dynamic printf"), NULL,
16581 update_dprintf_commands, NULL,
16582 &setlist, &showlist);
16583
16584 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16585 &disconnected_dprintf, _("\
16586 Set whether dprintf continues after GDB disconnects."), _("\
16587 Show whether dprintf continues after GDB disconnects."), _("\
16588 Use this to let dprintf commands continue to hit and produce output\n\
16589 even if GDB disconnects or detaches from the target."),
16590 NULL,
16591 NULL,
16592 &setlist, &showlist);
16593
16594 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16595 agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16596 (target agent only) This is useful for formatted output in user-defined commands."));
16597
16598 automatic_hardware_breakpoints = 1;
16599
16600 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
16601 observer_attach_thread_exit (remove_threaded_breakpoints);
16602 }
This page took 0.367822 seconds and 4 git commands to generate.