Explicit locations: introduce address locations
[deliverable/binutils-gdb.git] / gdb / breakpoint.c
1 /* Everything about breakpoints, for GDB.
2
3 Copyright (C) 1986-2015 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21 #include "arch-utils.h"
22 #include <ctype.h>
23 #include "hashtab.h"
24 #include "symtab.h"
25 #include "frame.h"
26 #include "breakpoint.h"
27 #include "tracepoint.h"
28 #include "gdbtypes.h"
29 #include "expression.h"
30 #include "gdbcore.h"
31 #include "gdbcmd.h"
32 #include "value.h"
33 #include "command.h"
34 #include "inferior.h"
35 #include "infrun.h"
36 #include "gdbthread.h"
37 #include "target.h"
38 #include "language.h"
39 #include "gdb-demangle.h"
40 #include "filenames.h"
41 #include "annotate.h"
42 #include "symfile.h"
43 #include "objfiles.h"
44 #include "source.h"
45 #include "linespec.h"
46 #include "completer.h"
47 #include "gdb.h"
48 #include "ui-out.h"
49 #include "cli/cli-script.h"
50 #include "block.h"
51 #include "solib.h"
52 #include "solist.h"
53 #include "observer.h"
54 #include "memattr.h"
55 #include "ada-lang.h"
56 #include "top.h"
57 #include "valprint.h"
58 #include "jit.h"
59 #include "parser-defs.h"
60 #include "gdb_regex.h"
61 #include "probe.h"
62 #include "cli/cli-utils.h"
63 #include "continuations.h"
64 #include "stack.h"
65 #include "skip.h"
66 #include "ax-gdb.h"
67 #include "dummy-frame.h"
68 #include "interps.h"
69 #include "format.h"
70 #include "location.h"
71
72 /* readline include files */
73 #include "readline/readline.h"
74 #include "readline/history.h"
75
76 /* readline defines this. */
77 #undef savestring
78
79 #include "mi/mi-common.h"
80 #include "extension.h"
81
82 /* Enums for exception-handling support. */
83 enum exception_event_kind
84 {
85 EX_EVENT_THROW,
86 EX_EVENT_RETHROW,
87 EX_EVENT_CATCH
88 };
89
90 /* Prototypes for local functions. */
91
92 static void enable_delete_command (char *, int);
93
94 static void enable_once_command (char *, int);
95
96 static void enable_count_command (char *, int);
97
98 static void disable_command (char *, int);
99
100 static void enable_command (char *, int);
101
102 static void map_breakpoint_numbers (char *, void (*) (struct breakpoint *,
103 void *),
104 void *);
105
106 static void ignore_command (char *, int);
107
108 static int breakpoint_re_set_one (void *);
109
110 static void breakpoint_re_set_default (struct breakpoint *);
111
112 static void
113 create_sals_from_location_default (const struct event_location *location,
114 struct linespec_result *canonical,
115 enum bptype type_wanted);
116
117 static void create_breakpoints_sal_default (struct gdbarch *,
118 struct linespec_result *,
119 char *, char *, enum bptype,
120 enum bpdisp, int, int,
121 int,
122 const struct breakpoint_ops *,
123 int, int, int, unsigned);
124
125 static void decode_location_default (struct breakpoint *b,
126 const struct event_location *location,
127 struct symtabs_and_lines *sals);
128
129 static void clear_command (char *, int);
130
131 static void catch_command (char *, int);
132
133 static int can_use_hardware_watchpoint (struct value *);
134
135 static void break_command_1 (char *, int, int);
136
137 static void mention (struct breakpoint *);
138
139 static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
140 enum bptype,
141 const struct breakpoint_ops *);
142 static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
143 const struct symtab_and_line *);
144
145 /* This function is used in gdbtk sources and thus can not be made
146 static. */
147 struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
148 struct symtab_and_line,
149 enum bptype,
150 const struct breakpoint_ops *);
151
152 static struct breakpoint *
153 momentary_breakpoint_from_master (struct breakpoint *orig,
154 enum bptype type,
155 const struct breakpoint_ops *ops,
156 int loc_enabled);
157
158 static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
159
160 static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
161 CORE_ADDR bpaddr,
162 enum bptype bptype);
163
164 static void describe_other_breakpoints (struct gdbarch *,
165 struct program_space *, CORE_ADDR,
166 struct obj_section *, int);
167
168 static int watchpoint_locations_match (struct bp_location *loc1,
169 struct bp_location *loc2);
170
171 static int breakpoint_location_address_match (struct bp_location *bl,
172 struct address_space *aspace,
173 CORE_ADDR addr);
174
175 static void breakpoints_info (char *, int);
176
177 static void watchpoints_info (char *, int);
178
179 static int breakpoint_1 (char *, int,
180 int (*) (const struct breakpoint *));
181
182 static int breakpoint_cond_eval (void *);
183
184 static void cleanup_executing_breakpoints (void *);
185
186 static void commands_command (char *, int);
187
188 static void condition_command (char *, int);
189
190 typedef enum
191 {
192 mark_inserted,
193 mark_uninserted
194 }
195 insertion_state_t;
196
197 static int remove_breakpoint (struct bp_location *, insertion_state_t);
198 static int remove_breakpoint_1 (struct bp_location *, insertion_state_t);
199
200 static enum print_stop_action print_bp_stop_message (bpstat bs);
201
202 static int watchpoint_check (void *);
203
204 static void maintenance_info_breakpoints (char *, int);
205
206 static int hw_breakpoint_used_count (void);
207
208 static int hw_watchpoint_use_count (struct breakpoint *);
209
210 static int hw_watchpoint_used_count_others (struct breakpoint *except,
211 enum bptype type,
212 int *other_type_used);
213
214 static void hbreak_command (char *, int);
215
216 static void thbreak_command (char *, int);
217
218 static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
219 int count);
220
221 static void stop_command (char *arg, int from_tty);
222
223 static void stopin_command (char *arg, int from_tty);
224
225 static void stopat_command (char *arg, int from_tty);
226
227 static void tcatch_command (char *arg, int from_tty);
228
229 static void free_bp_location (struct bp_location *loc);
230 static void incref_bp_location (struct bp_location *loc);
231 static void decref_bp_location (struct bp_location **loc);
232
233 static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
234
235 /* update_global_location_list's modes of operation wrt to whether to
236 insert locations now. */
237 enum ugll_insert_mode
238 {
239 /* Don't insert any breakpoint locations into the inferior, only
240 remove already-inserted locations that no longer should be
241 inserted. Functions that delete a breakpoint or breakpoints
242 should specify this mode, so that deleting a breakpoint doesn't
243 have the side effect of inserting the locations of other
244 breakpoints that are marked not-inserted, but should_be_inserted
245 returns true on them.
246
247 This behavior is useful is situations close to tear-down -- e.g.,
248 after an exec, while the target still has execution, but
249 breakpoint shadows of the previous executable image should *NOT*
250 be restored to the new image; or before detaching, where the
251 target still has execution and wants to delete breakpoints from
252 GDB's lists, and all breakpoints had already been removed from
253 the inferior. */
254 UGLL_DONT_INSERT,
255
256 /* May insert breakpoints iff breakpoints_should_be_inserted_now
257 claims breakpoints should be inserted now. */
258 UGLL_MAY_INSERT,
259
260 /* Insert locations now, irrespective of
261 breakpoints_should_be_inserted_now. E.g., say all threads are
262 stopped right now, and the user did "continue". We need to
263 insert breakpoints _before_ resuming the target, but
264 UGLL_MAY_INSERT wouldn't insert them, because
265 breakpoints_should_be_inserted_now returns false at that point,
266 as no thread is running yet. */
267 UGLL_INSERT
268 };
269
270 static void update_global_location_list (enum ugll_insert_mode);
271
272 static void update_global_location_list_nothrow (enum ugll_insert_mode);
273
274 static int is_hardware_watchpoint (const struct breakpoint *bpt);
275
276 static void insert_breakpoint_locations (void);
277
278 static void tracepoints_info (char *, int);
279
280 static void delete_trace_command (char *, int);
281
282 static void enable_trace_command (char *, int);
283
284 static void disable_trace_command (char *, int);
285
286 static void trace_pass_command (char *, int);
287
288 static void set_tracepoint_count (int num);
289
290 static int is_masked_watchpoint (const struct breakpoint *b);
291
292 static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
293
294 /* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
295 otherwise. */
296
297 static int strace_marker_p (struct breakpoint *b);
298
299 /* The breakpoint_ops structure to be inherited by all breakpoint_ops
300 that are implemented on top of software or hardware breakpoints
301 (user breakpoints, internal and momentary breakpoints, etc.). */
302 static struct breakpoint_ops bkpt_base_breakpoint_ops;
303
304 /* Internal breakpoints class type. */
305 static struct breakpoint_ops internal_breakpoint_ops;
306
307 /* Momentary breakpoints class type. */
308 static struct breakpoint_ops momentary_breakpoint_ops;
309
310 /* Momentary breakpoints for bp_longjmp and bp_exception class type. */
311 static struct breakpoint_ops longjmp_breakpoint_ops;
312
313 /* The breakpoint_ops structure to be used in regular user created
314 breakpoints. */
315 struct breakpoint_ops bkpt_breakpoint_ops;
316
317 /* Breakpoints set on probes. */
318 static struct breakpoint_ops bkpt_probe_breakpoint_ops;
319
320 /* Dynamic printf class type. */
321 struct breakpoint_ops dprintf_breakpoint_ops;
322
323 /* The style in which to perform a dynamic printf. This is a user
324 option because different output options have different tradeoffs;
325 if GDB does the printing, there is better error handling if there
326 is a problem with any of the arguments, but using an inferior
327 function lets you have special-purpose printers and sending of
328 output to the same place as compiled-in print functions. */
329
330 static const char dprintf_style_gdb[] = "gdb";
331 static const char dprintf_style_call[] = "call";
332 static const char dprintf_style_agent[] = "agent";
333 static const char *const dprintf_style_enums[] = {
334 dprintf_style_gdb,
335 dprintf_style_call,
336 dprintf_style_agent,
337 NULL
338 };
339 static const char *dprintf_style = dprintf_style_gdb;
340
341 /* The function to use for dynamic printf if the preferred style is to
342 call into the inferior. The value is simply a string that is
343 copied into the command, so it can be anything that GDB can
344 evaluate to a callable address, not necessarily a function name. */
345
346 static char *dprintf_function = "";
347
348 /* The channel to use for dynamic printf if the preferred style is to
349 call into the inferior; if a nonempty string, it will be passed to
350 the call as the first argument, with the format string as the
351 second. As with the dprintf function, this can be anything that
352 GDB knows how to evaluate, so in addition to common choices like
353 "stderr", this could be an app-specific expression like
354 "mystreams[curlogger]". */
355
356 static char *dprintf_channel = "";
357
358 /* True if dprintf commands should continue to operate even if GDB
359 has disconnected. */
360 static int disconnected_dprintf = 1;
361
362 /* A reference-counted struct command_line. This lets multiple
363 breakpoints share a single command list. */
364 struct counted_command_line
365 {
366 /* The reference count. */
367 int refc;
368
369 /* The command list. */
370 struct command_line *commands;
371 };
372
373 struct command_line *
374 breakpoint_commands (struct breakpoint *b)
375 {
376 return b->commands ? b->commands->commands : NULL;
377 }
378
379 /* Flag indicating that a command has proceeded the inferior past the
380 current breakpoint. */
381
382 static int breakpoint_proceeded;
383
384 const char *
385 bpdisp_text (enum bpdisp disp)
386 {
387 /* NOTE: the following values are a part of MI protocol and
388 represent values of 'disp' field returned when inferior stops at
389 a breakpoint. */
390 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
391
392 return bpdisps[(int) disp];
393 }
394
395 /* Prototypes for exported functions. */
396 /* If FALSE, gdb will not use hardware support for watchpoints, even
397 if such is available. */
398 static int can_use_hw_watchpoints;
399
400 static void
401 show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
402 struct cmd_list_element *c,
403 const char *value)
404 {
405 fprintf_filtered (file,
406 _("Debugger's willingness to use "
407 "watchpoint hardware is %s.\n"),
408 value);
409 }
410
411 /* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
412 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
413 for unrecognized breakpoint locations.
414 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
415 static enum auto_boolean pending_break_support;
416 static void
417 show_pending_break_support (struct ui_file *file, int from_tty,
418 struct cmd_list_element *c,
419 const char *value)
420 {
421 fprintf_filtered (file,
422 _("Debugger's behavior regarding "
423 "pending breakpoints is %s.\n"),
424 value);
425 }
426
427 /* If 1, gdb will automatically use hardware breakpoints for breakpoints
428 set with "break" but falling in read-only memory.
429 If 0, gdb will warn about such breakpoints, but won't automatically
430 use hardware breakpoints. */
431 static int automatic_hardware_breakpoints;
432 static void
433 show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
434 struct cmd_list_element *c,
435 const char *value)
436 {
437 fprintf_filtered (file,
438 _("Automatic usage of hardware breakpoints is %s.\n"),
439 value);
440 }
441
442 /* If on, GDB keeps breakpoints inserted even if the inferior is
443 stopped, and immediately inserts any new breakpoints as soon as
444 they're created. If off (default), GDB keeps breakpoints off of
445 the target as long as possible. That is, it delays inserting
446 breakpoints until the next resume, and removes them again when the
447 target fully stops. This is a bit safer in case GDB crashes while
448 processing user input. */
449 static int always_inserted_mode = 0;
450
451 static void
452 show_always_inserted_mode (struct ui_file *file, int from_tty,
453 struct cmd_list_element *c, const char *value)
454 {
455 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
456 value);
457 }
458
459 /* See breakpoint.h. */
460
461 int
462 breakpoints_should_be_inserted_now (void)
463 {
464 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
465 {
466 /* If breakpoints are global, they should be inserted even if no
467 thread under gdb's control is running, or even if there are
468 no threads under GDB's control yet. */
469 return 1;
470 }
471 else if (target_has_execution)
472 {
473 struct thread_info *tp;
474
475 if (always_inserted_mode)
476 {
477 /* The user wants breakpoints inserted even if all threads
478 are stopped. */
479 return 1;
480 }
481
482 if (threads_are_executing ())
483 return 1;
484
485 /* Don't remove breakpoints yet if, even though all threads are
486 stopped, we still have events to process. */
487 ALL_NON_EXITED_THREADS (tp)
488 if (tp->resumed
489 && tp->suspend.waitstatus_pending_p)
490 return 1;
491 }
492 return 0;
493 }
494
495 static const char condition_evaluation_both[] = "host or target";
496
497 /* Modes for breakpoint condition evaluation. */
498 static const char condition_evaluation_auto[] = "auto";
499 static const char condition_evaluation_host[] = "host";
500 static const char condition_evaluation_target[] = "target";
501 static const char *const condition_evaluation_enums[] = {
502 condition_evaluation_auto,
503 condition_evaluation_host,
504 condition_evaluation_target,
505 NULL
506 };
507
508 /* Global that holds the current mode for breakpoint condition evaluation. */
509 static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
510
511 /* Global that we use to display information to the user (gets its value from
512 condition_evaluation_mode_1. */
513 static const char *condition_evaluation_mode = condition_evaluation_auto;
514
515 /* Translate a condition evaluation mode MODE into either "host"
516 or "target". This is used mostly to translate from "auto" to the
517 real setting that is being used. It returns the translated
518 evaluation mode. */
519
520 static const char *
521 translate_condition_evaluation_mode (const char *mode)
522 {
523 if (mode == condition_evaluation_auto)
524 {
525 if (target_supports_evaluation_of_breakpoint_conditions ())
526 return condition_evaluation_target;
527 else
528 return condition_evaluation_host;
529 }
530 else
531 return mode;
532 }
533
534 /* Discovers what condition_evaluation_auto translates to. */
535
536 static const char *
537 breakpoint_condition_evaluation_mode (void)
538 {
539 return translate_condition_evaluation_mode (condition_evaluation_mode);
540 }
541
542 /* Return true if GDB should evaluate breakpoint conditions or false
543 otherwise. */
544
545 static int
546 gdb_evaluates_breakpoint_condition_p (void)
547 {
548 const char *mode = breakpoint_condition_evaluation_mode ();
549
550 return (mode == condition_evaluation_host);
551 }
552
553 void _initialize_breakpoint (void);
554
555 /* Are we executing breakpoint commands? */
556 static int executing_breakpoint_commands;
557
558 /* Are overlay event breakpoints enabled? */
559 static int overlay_events_enabled;
560
561 /* See description in breakpoint.h. */
562 int target_exact_watchpoints = 0;
563
564 /* Walk the following statement or block through all breakpoints.
565 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
566 current breakpoint. */
567
568 #define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
569
570 #define ALL_BREAKPOINTS_SAFE(B,TMP) \
571 for (B = breakpoint_chain; \
572 B ? (TMP=B->next, 1): 0; \
573 B = TMP)
574
575 /* Similar iterator for the low-level breakpoints. SAFE variant is
576 not provided so update_global_location_list must not be called
577 while executing the block of ALL_BP_LOCATIONS. */
578
579 #define ALL_BP_LOCATIONS(B,BP_TMP) \
580 for (BP_TMP = bp_location; \
581 BP_TMP < bp_location + bp_location_count && (B = *BP_TMP); \
582 BP_TMP++)
583
584 /* Iterates through locations with address ADDRESS for the currently selected
585 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
586 to where the loop should start from.
587 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
588 appropriate location to start with. */
589
590 #define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
591 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
592 BP_LOCP_TMP = BP_LOCP_START; \
593 BP_LOCP_START \
594 && (BP_LOCP_TMP < bp_location + bp_location_count \
595 && (*BP_LOCP_TMP)->address == ADDRESS); \
596 BP_LOCP_TMP++)
597
598 /* Iterator for tracepoints only. */
599
600 #define ALL_TRACEPOINTS(B) \
601 for (B = breakpoint_chain; B; B = B->next) \
602 if (is_tracepoint (B))
603
604 /* Chains of all breakpoints defined. */
605
606 struct breakpoint *breakpoint_chain;
607
608 /* Array is sorted by bp_location_compare - primarily by the ADDRESS. */
609
610 static struct bp_location **bp_location;
611
612 /* Number of elements of BP_LOCATION. */
613
614 static unsigned bp_location_count;
615
616 /* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
617 ADDRESS for the current elements of BP_LOCATION which get a valid
618 result from bp_location_has_shadow. You can use it for roughly
619 limiting the subrange of BP_LOCATION to scan for shadow bytes for
620 an address you need to read. */
621
622 static CORE_ADDR bp_location_placed_address_before_address_max;
623
624 /* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
625 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
626 BP_LOCATION which get a valid result from bp_location_has_shadow.
627 You can use it for roughly limiting the subrange of BP_LOCATION to
628 scan for shadow bytes for an address you need to read. */
629
630 static CORE_ADDR bp_location_shadow_len_after_address_max;
631
632 /* The locations that no longer correspond to any breakpoint, unlinked
633 from bp_location array, but for which a hit may still be reported
634 by a target. */
635 VEC(bp_location_p) *moribund_locations = NULL;
636
637 /* Number of last breakpoint made. */
638
639 static int breakpoint_count;
640
641 /* The value of `breakpoint_count' before the last command that
642 created breakpoints. If the last (break-like) command created more
643 than one breakpoint, then the difference between BREAKPOINT_COUNT
644 and PREV_BREAKPOINT_COUNT is more than one. */
645 static int prev_breakpoint_count;
646
647 /* Number of last tracepoint made. */
648
649 static int tracepoint_count;
650
651 static struct cmd_list_element *breakpoint_set_cmdlist;
652 static struct cmd_list_element *breakpoint_show_cmdlist;
653 struct cmd_list_element *save_cmdlist;
654
655 /* See declaration at breakpoint.h. */
656
657 struct breakpoint *
658 breakpoint_find_if (int (*func) (struct breakpoint *b, void *d),
659 void *user_data)
660 {
661 struct breakpoint *b = NULL;
662
663 ALL_BREAKPOINTS (b)
664 {
665 if (func (b, user_data) != 0)
666 break;
667 }
668
669 return b;
670 }
671
672 /* Return whether a breakpoint is an active enabled breakpoint. */
673 static int
674 breakpoint_enabled (struct breakpoint *b)
675 {
676 return (b->enable_state == bp_enabled);
677 }
678
679 /* Set breakpoint count to NUM. */
680
681 static void
682 set_breakpoint_count (int num)
683 {
684 prev_breakpoint_count = breakpoint_count;
685 breakpoint_count = num;
686 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
687 }
688
689 /* Used by `start_rbreak_breakpoints' below, to record the current
690 breakpoint count before "rbreak" creates any breakpoint. */
691 static int rbreak_start_breakpoint_count;
692
693 /* Called at the start an "rbreak" command to record the first
694 breakpoint made. */
695
696 void
697 start_rbreak_breakpoints (void)
698 {
699 rbreak_start_breakpoint_count = breakpoint_count;
700 }
701
702 /* Called at the end of an "rbreak" command to record the last
703 breakpoint made. */
704
705 void
706 end_rbreak_breakpoints (void)
707 {
708 prev_breakpoint_count = rbreak_start_breakpoint_count;
709 }
710
711 /* Used in run_command to zero the hit count when a new run starts. */
712
713 void
714 clear_breakpoint_hit_counts (void)
715 {
716 struct breakpoint *b;
717
718 ALL_BREAKPOINTS (b)
719 b->hit_count = 0;
720 }
721
722 /* Allocate a new counted_command_line with reference count of 1.
723 The new structure owns COMMANDS. */
724
725 static struct counted_command_line *
726 alloc_counted_command_line (struct command_line *commands)
727 {
728 struct counted_command_line *result
729 = xmalloc (sizeof (struct counted_command_line));
730
731 result->refc = 1;
732 result->commands = commands;
733 return result;
734 }
735
736 /* Increment reference count. This does nothing if CMD is NULL. */
737
738 static void
739 incref_counted_command_line (struct counted_command_line *cmd)
740 {
741 if (cmd)
742 ++cmd->refc;
743 }
744
745 /* Decrement reference count. If the reference count reaches 0,
746 destroy the counted_command_line. Sets *CMDP to NULL. This does
747 nothing if *CMDP is NULL. */
748
749 static void
750 decref_counted_command_line (struct counted_command_line **cmdp)
751 {
752 if (*cmdp)
753 {
754 if (--(*cmdp)->refc == 0)
755 {
756 free_command_lines (&(*cmdp)->commands);
757 xfree (*cmdp);
758 }
759 *cmdp = NULL;
760 }
761 }
762
763 /* A cleanup function that calls decref_counted_command_line. */
764
765 static void
766 do_cleanup_counted_command_line (void *arg)
767 {
768 decref_counted_command_line (arg);
769 }
770
771 /* Create a cleanup that calls decref_counted_command_line on the
772 argument. */
773
774 static struct cleanup *
775 make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
776 {
777 return make_cleanup (do_cleanup_counted_command_line, cmdp);
778 }
779
780 \f
781 /* Return the breakpoint with the specified number, or NULL
782 if the number does not refer to an existing breakpoint. */
783
784 struct breakpoint *
785 get_breakpoint (int num)
786 {
787 struct breakpoint *b;
788
789 ALL_BREAKPOINTS (b)
790 if (b->number == num)
791 return b;
792
793 return NULL;
794 }
795
796 \f
797
798 /* Mark locations as "conditions have changed" in case the target supports
799 evaluating conditions on its side. */
800
801 static void
802 mark_breakpoint_modified (struct breakpoint *b)
803 {
804 struct bp_location *loc;
805
806 /* This is only meaningful if the target is
807 evaluating conditions and if the user has
808 opted for condition evaluation on the target's
809 side. */
810 if (gdb_evaluates_breakpoint_condition_p ()
811 || !target_supports_evaluation_of_breakpoint_conditions ())
812 return;
813
814 if (!is_breakpoint (b))
815 return;
816
817 for (loc = b->loc; loc; loc = loc->next)
818 loc->condition_changed = condition_modified;
819 }
820
821 /* Mark location as "conditions have changed" in case the target supports
822 evaluating conditions on its side. */
823
824 static void
825 mark_breakpoint_location_modified (struct bp_location *loc)
826 {
827 /* This is only meaningful if the target is
828 evaluating conditions and if the user has
829 opted for condition evaluation on the target's
830 side. */
831 if (gdb_evaluates_breakpoint_condition_p ()
832 || !target_supports_evaluation_of_breakpoint_conditions ())
833
834 return;
835
836 if (!is_breakpoint (loc->owner))
837 return;
838
839 loc->condition_changed = condition_modified;
840 }
841
842 /* Sets the condition-evaluation mode using the static global
843 condition_evaluation_mode. */
844
845 static void
846 set_condition_evaluation_mode (char *args, int from_tty,
847 struct cmd_list_element *c)
848 {
849 const char *old_mode, *new_mode;
850
851 if ((condition_evaluation_mode_1 == condition_evaluation_target)
852 && !target_supports_evaluation_of_breakpoint_conditions ())
853 {
854 condition_evaluation_mode_1 = condition_evaluation_mode;
855 warning (_("Target does not support breakpoint condition evaluation.\n"
856 "Using host evaluation mode instead."));
857 return;
858 }
859
860 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
861 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
862
863 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
864 settings was "auto". */
865 condition_evaluation_mode = condition_evaluation_mode_1;
866
867 /* Only update the mode if the user picked a different one. */
868 if (new_mode != old_mode)
869 {
870 struct bp_location *loc, **loc_tmp;
871 /* If the user switched to a different evaluation mode, we
872 need to synch the changes with the target as follows:
873
874 "host" -> "target": Send all (valid) conditions to the target.
875 "target" -> "host": Remove all the conditions from the target.
876 */
877
878 if (new_mode == condition_evaluation_target)
879 {
880 /* Mark everything modified and synch conditions with the
881 target. */
882 ALL_BP_LOCATIONS (loc, loc_tmp)
883 mark_breakpoint_location_modified (loc);
884 }
885 else
886 {
887 /* Manually mark non-duplicate locations to synch conditions
888 with the target. We do this to remove all the conditions the
889 target knows about. */
890 ALL_BP_LOCATIONS (loc, loc_tmp)
891 if (is_breakpoint (loc->owner) && loc->inserted)
892 loc->needs_update = 1;
893 }
894
895 /* Do the update. */
896 update_global_location_list (UGLL_MAY_INSERT);
897 }
898
899 return;
900 }
901
902 /* Shows the current mode of breakpoint condition evaluation. Explicitly shows
903 what "auto" is translating to. */
904
905 static void
906 show_condition_evaluation_mode (struct ui_file *file, int from_tty,
907 struct cmd_list_element *c, const char *value)
908 {
909 if (condition_evaluation_mode == condition_evaluation_auto)
910 fprintf_filtered (file,
911 _("Breakpoint condition evaluation "
912 "mode is %s (currently %s).\n"),
913 value,
914 breakpoint_condition_evaluation_mode ());
915 else
916 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
917 value);
918 }
919
920 /* A comparison function for bp_location AP and BP that is used by
921 bsearch. This comparison function only cares about addresses, unlike
922 the more general bp_location_compare function. */
923
924 static int
925 bp_location_compare_addrs (const void *ap, const void *bp)
926 {
927 struct bp_location *a = *(void **) ap;
928 struct bp_location *b = *(void **) bp;
929
930 if (a->address == b->address)
931 return 0;
932 else
933 return ((a->address > b->address) - (a->address < b->address));
934 }
935
936 /* Helper function to skip all bp_locations with addresses
937 less than ADDRESS. It returns the first bp_location that
938 is greater than or equal to ADDRESS. If none is found, just
939 return NULL. */
940
941 static struct bp_location **
942 get_first_locp_gte_addr (CORE_ADDR address)
943 {
944 struct bp_location dummy_loc;
945 struct bp_location *dummy_locp = &dummy_loc;
946 struct bp_location **locp_found = NULL;
947
948 /* Initialize the dummy location's address field. */
949 memset (&dummy_loc, 0, sizeof (struct bp_location));
950 dummy_loc.address = address;
951
952 /* Find a close match to the first location at ADDRESS. */
953 locp_found = bsearch (&dummy_locp, bp_location, bp_location_count,
954 sizeof (struct bp_location **),
955 bp_location_compare_addrs);
956
957 /* Nothing was found, nothing left to do. */
958 if (locp_found == NULL)
959 return NULL;
960
961 /* We may have found a location that is at ADDRESS but is not the first in the
962 location's list. Go backwards (if possible) and locate the first one. */
963 while ((locp_found - 1) >= bp_location
964 && (*(locp_found - 1))->address == address)
965 locp_found--;
966
967 return locp_found;
968 }
969
970 void
971 set_breakpoint_condition (struct breakpoint *b, const char *exp,
972 int from_tty)
973 {
974 xfree (b->cond_string);
975 b->cond_string = NULL;
976
977 if (is_watchpoint (b))
978 {
979 struct watchpoint *w = (struct watchpoint *) b;
980
981 xfree (w->cond_exp);
982 w->cond_exp = NULL;
983 }
984 else
985 {
986 struct bp_location *loc;
987
988 for (loc = b->loc; loc; loc = loc->next)
989 {
990 xfree (loc->cond);
991 loc->cond = NULL;
992
993 /* No need to free the condition agent expression
994 bytecode (if we have one). We will handle this
995 when we go through update_global_location_list. */
996 }
997 }
998
999 if (*exp == 0)
1000 {
1001 if (from_tty)
1002 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
1003 }
1004 else
1005 {
1006 const char *arg = exp;
1007
1008 /* I don't know if it matters whether this is the string the user
1009 typed in or the decompiled expression. */
1010 b->cond_string = xstrdup (arg);
1011 b->condition_not_parsed = 0;
1012
1013 if (is_watchpoint (b))
1014 {
1015 struct watchpoint *w = (struct watchpoint *) b;
1016
1017 innermost_block = NULL;
1018 arg = exp;
1019 w->cond_exp = parse_exp_1 (&arg, 0, 0, 0);
1020 if (*arg)
1021 error (_("Junk at end of expression"));
1022 w->cond_exp_valid_block = innermost_block;
1023 }
1024 else
1025 {
1026 struct bp_location *loc;
1027
1028 for (loc = b->loc; loc; loc = loc->next)
1029 {
1030 arg = exp;
1031 loc->cond =
1032 parse_exp_1 (&arg, loc->address,
1033 block_for_pc (loc->address), 0);
1034 if (*arg)
1035 error (_("Junk at end of expression"));
1036 }
1037 }
1038 }
1039 mark_breakpoint_modified (b);
1040
1041 observer_notify_breakpoint_modified (b);
1042 }
1043
1044 /* Completion for the "condition" command. */
1045
1046 static VEC (char_ptr) *
1047 condition_completer (struct cmd_list_element *cmd,
1048 const char *text, const char *word)
1049 {
1050 const char *space;
1051
1052 text = skip_spaces_const (text);
1053 space = skip_to_space_const (text);
1054 if (*space == '\0')
1055 {
1056 int len;
1057 struct breakpoint *b;
1058 VEC (char_ptr) *result = NULL;
1059
1060 if (text[0] == '$')
1061 {
1062 /* We don't support completion of history indices. */
1063 if (isdigit (text[1]))
1064 return NULL;
1065 return complete_internalvar (&text[1]);
1066 }
1067
1068 /* We're completing the breakpoint number. */
1069 len = strlen (text);
1070
1071 ALL_BREAKPOINTS (b)
1072 {
1073 char number[50];
1074
1075 xsnprintf (number, sizeof (number), "%d", b->number);
1076
1077 if (strncmp (number, text, len) == 0)
1078 VEC_safe_push (char_ptr, result, xstrdup (number));
1079 }
1080
1081 return result;
1082 }
1083
1084 /* We're completing the expression part. */
1085 text = skip_spaces_const (space);
1086 return expression_completer (cmd, text, word);
1087 }
1088
1089 /* condition N EXP -- set break condition of breakpoint N to EXP. */
1090
1091 static void
1092 condition_command (char *arg, int from_tty)
1093 {
1094 struct breakpoint *b;
1095 char *p;
1096 int bnum;
1097
1098 if (arg == 0)
1099 error_no_arg (_("breakpoint number"));
1100
1101 p = arg;
1102 bnum = get_number (&p);
1103 if (bnum == 0)
1104 error (_("Bad breakpoint argument: '%s'"), arg);
1105
1106 ALL_BREAKPOINTS (b)
1107 if (b->number == bnum)
1108 {
1109 /* Check if this breakpoint has a "stop" method implemented in an
1110 extension language. This method and conditions entered into GDB
1111 from the CLI are mutually exclusive. */
1112 const struct extension_language_defn *extlang
1113 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1114
1115 if (extlang != NULL)
1116 {
1117 error (_("Only one stop condition allowed. There is currently"
1118 " a %s stop condition defined for this breakpoint."),
1119 ext_lang_capitalized_name (extlang));
1120 }
1121 set_breakpoint_condition (b, p, from_tty);
1122
1123 if (is_breakpoint (b))
1124 update_global_location_list (UGLL_MAY_INSERT);
1125
1126 return;
1127 }
1128
1129 error (_("No breakpoint number %d."), bnum);
1130 }
1131
1132 /* Check that COMMAND do not contain commands that are suitable
1133 only for tracepoints and not suitable for ordinary breakpoints.
1134 Throw if any such commands is found. */
1135
1136 static void
1137 check_no_tracepoint_commands (struct command_line *commands)
1138 {
1139 struct command_line *c;
1140
1141 for (c = commands; c; c = c->next)
1142 {
1143 int i;
1144
1145 if (c->control_type == while_stepping_control)
1146 error (_("The 'while-stepping' command can "
1147 "only be used for tracepoints"));
1148
1149 for (i = 0; i < c->body_count; ++i)
1150 check_no_tracepoint_commands ((c->body_list)[i]);
1151
1152 /* Not that command parsing removes leading whitespace and comment
1153 lines and also empty lines. So, we only need to check for
1154 command directly. */
1155 if (strstr (c->line, "collect ") == c->line)
1156 error (_("The 'collect' command can only be used for tracepoints"));
1157
1158 if (strstr (c->line, "teval ") == c->line)
1159 error (_("The 'teval' command can only be used for tracepoints"));
1160 }
1161 }
1162
1163 /* Encapsulate tests for different types of tracepoints. */
1164
1165 static int
1166 is_tracepoint_type (enum bptype type)
1167 {
1168 return (type == bp_tracepoint
1169 || type == bp_fast_tracepoint
1170 || type == bp_static_tracepoint);
1171 }
1172
1173 int
1174 is_tracepoint (const struct breakpoint *b)
1175 {
1176 return is_tracepoint_type (b->type);
1177 }
1178
1179 /* A helper function that validates that COMMANDS are valid for a
1180 breakpoint. This function will throw an exception if a problem is
1181 found. */
1182
1183 static void
1184 validate_commands_for_breakpoint (struct breakpoint *b,
1185 struct command_line *commands)
1186 {
1187 if (is_tracepoint (b))
1188 {
1189 struct tracepoint *t = (struct tracepoint *) b;
1190 struct command_line *c;
1191 struct command_line *while_stepping = 0;
1192
1193 /* Reset the while-stepping step count. The previous commands
1194 might have included a while-stepping action, while the new
1195 ones might not. */
1196 t->step_count = 0;
1197
1198 /* We need to verify that each top-level element of commands is
1199 valid for tracepoints, that there's at most one
1200 while-stepping element, and that the while-stepping's body
1201 has valid tracing commands excluding nested while-stepping.
1202 We also need to validate the tracepoint action line in the
1203 context of the tracepoint --- validate_actionline actually
1204 has side effects, like setting the tracepoint's
1205 while-stepping STEP_COUNT, in addition to checking if the
1206 collect/teval actions parse and make sense in the
1207 tracepoint's context. */
1208 for (c = commands; c; c = c->next)
1209 {
1210 if (c->control_type == while_stepping_control)
1211 {
1212 if (b->type == bp_fast_tracepoint)
1213 error (_("The 'while-stepping' command "
1214 "cannot be used for fast tracepoint"));
1215 else if (b->type == bp_static_tracepoint)
1216 error (_("The 'while-stepping' command "
1217 "cannot be used for static tracepoint"));
1218
1219 if (while_stepping)
1220 error (_("The 'while-stepping' command "
1221 "can be used only once"));
1222 else
1223 while_stepping = c;
1224 }
1225
1226 validate_actionline (c->line, b);
1227 }
1228 if (while_stepping)
1229 {
1230 struct command_line *c2;
1231
1232 gdb_assert (while_stepping->body_count == 1);
1233 c2 = while_stepping->body_list[0];
1234 for (; c2; c2 = c2->next)
1235 {
1236 if (c2->control_type == while_stepping_control)
1237 error (_("The 'while-stepping' command cannot be nested"));
1238 }
1239 }
1240 }
1241 else
1242 {
1243 check_no_tracepoint_commands (commands);
1244 }
1245 }
1246
1247 /* Return a vector of all the static tracepoints set at ADDR. The
1248 caller is responsible for releasing the vector. */
1249
1250 VEC(breakpoint_p) *
1251 static_tracepoints_here (CORE_ADDR addr)
1252 {
1253 struct breakpoint *b;
1254 VEC(breakpoint_p) *found = 0;
1255 struct bp_location *loc;
1256
1257 ALL_BREAKPOINTS (b)
1258 if (b->type == bp_static_tracepoint)
1259 {
1260 for (loc = b->loc; loc; loc = loc->next)
1261 if (loc->address == addr)
1262 VEC_safe_push(breakpoint_p, found, b);
1263 }
1264
1265 return found;
1266 }
1267
1268 /* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
1269 validate that only allowed commands are included. */
1270
1271 void
1272 breakpoint_set_commands (struct breakpoint *b,
1273 struct command_line *commands)
1274 {
1275 validate_commands_for_breakpoint (b, commands);
1276
1277 decref_counted_command_line (&b->commands);
1278 b->commands = alloc_counted_command_line (commands);
1279 observer_notify_breakpoint_modified (b);
1280 }
1281
1282 /* Set the internal `silent' flag on the breakpoint. Note that this
1283 is not the same as the "silent" that may appear in the breakpoint's
1284 commands. */
1285
1286 void
1287 breakpoint_set_silent (struct breakpoint *b, int silent)
1288 {
1289 int old_silent = b->silent;
1290
1291 b->silent = silent;
1292 if (old_silent != silent)
1293 observer_notify_breakpoint_modified (b);
1294 }
1295
1296 /* Set the thread for this breakpoint. If THREAD is -1, make the
1297 breakpoint work for any thread. */
1298
1299 void
1300 breakpoint_set_thread (struct breakpoint *b, int thread)
1301 {
1302 int old_thread = b->thread;
1303
1304 b->thread = thread;
1305 if (old_thread != thread)
1306 observer_notify_breakpoint_modified (b);
1307 }
1308
1309 /* Set the task for this breakpoint. If TASK is 0, make the
1310 breakpoint work for any task. */
1311
1312 void
1313 breakpoint_set_task (struct breakpoint *b, int task)
1314 {
1315 int old_task = b->task;
1316
1317 b->task = task;
1318 if (old_task != task)
1319 observer_notify_breakpoint_modified (b);
1320 }
1321
1322 void
1323 check_tracepoint_command (char *line, void *closure)
1324 {
1325 struct breakpoint *b = closure;
1326
1327 validate_actionline (line, b);
1328 }
1329
1330 /* A structure used to pass information through
1331 map_breakpoint_numbers. */
1332
1333 struct commands_info
1334 {
1335 /* True if the command was typed at a tty. */
1336 int from_tty;
1337
1338 /* The breakpoint range spec. */
1339 char *arg;
1340
1341 /* Non-NULL if the body of the commands are being read from this
1342 already-parsed command. */
1343 struct command_line *control;
1344
1345 /* The command lines read from the user, or NULL if they have not
1346 yet been read. */
1347 struct counted_command_line *cmd;
1348 };
1349
1350 /* A callback for map_breakpoint_numbers that sets the commands for
1351 commands_command. */
1352
1353 static void
1354 do_map_commands_command (struct breakpoint *b, void *data)
1355 {
1356 struct commands_info *info = data;
1357
1358 if (info->cmd == NULL)
1359 {
1360 struct command_line *l;
1361
1362 if (info->control != NULL)
1363 l = copy_command_lines (info->control->body_list[0]);
1364 else
1365 {
1366 struct cleanup *old_chain;
1367 char *str;
1368
1369 str = xstrprintf (_("Type commands for breakpoint(s) "
1370 "%s, one per line."),
1371 info->arg);
1372
1373 old_chain = make_cleanup (xfree, str);
1374
1375 l = read_command_lines (str,
1376 info->from_tty, 1,
1377 (is_tracepoint (b)
1378 ? check_tracepoint_command : 0),
1379 b);
1380
1381 do_cleanups (old_chain);
1382 }
1383
1384 info->cmd = alloc_counted_command_line (l);
1385 }
1386
1387 /* If a breakpoint was on the list more than once, we don't need to
1388 do anything. */
1389 if (b->commands != info->cmd)
1390 {
1391 validate_commands_for_breakpoint (b, info->cmd->commands);
1392 incref_counted_command_line (info->cmd);
1393 decref_counted_command_line (&b->commands);
1394 b->commands = info->cmd;
1395 observer_notify_breakpoint_modified (b);
1396 }
1397 }
1398
1399 static void
1400 commands_command_1 (char *arg, int from_tty,
1401 struct command_line *control)
1402 {
1403 struct cleanup *cleanups;
1404 struct commands_info info;
1405
1406 info.from_tty = from_tty;
1407 info.control = control;
1408 info.cmd = NULL;
1409 /* If we read command lines from the user, then `info' will hold an
1410 extra reference to the commands that we must clean up. */
1411 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
1412
1413 if (arg == NULL || !*arg)
1414 {
1415 if (breakpoint_count - prev_breakpoint_count > 1)
1416 arg = xstrprintf ("%d-%d", prev_breakpoint_count + 1,
1417 breakpoint_count);
1418 else if (breakpoint_count > 0)
1419 arg = xstrprintf ("%d", breakpoint_count);
1420 else
1421 {
1422 /* So that we don't try to free the incoming non-NULL
1423 argument in the cleanup below. Mapping breakpoint
1424 numbers will fail in this case. */
1425 arg = NULL;
1426 }
1427 }
1428 else
1429 /* The command loop has some static state, so we need to preserve
1430 our argument. */
1431 arg = xstrdup (arg);
1432
1433 if (arg != NULL)
1434 make_cleanup (xfree, arg);
1435
1436 info.arg = arg;
1437
1438 map_breakpoint_numbers (arg, do_map_commands_command, &info);
1439
1440 if (info.cmd == NULL)
1441 error (_("No breakpoints specified."));
1442
1443 do_cleanups (cleanups);
1444 }
1445
1446 static void
1447 commands_command (char *arg, int from_tty)
1448 {
1449 commands_command_1 (arg, from_tty, NULL);
1450 }
1451
1452 /* Like commands_command, but instead of reading the commands from
1453 input stream, takes them from an already parsed command structure.
1454
1455 This is used by cli-script.c to DTRT with breakpoint commands
1456 that are part of if and while bodies. */
1457 enum command_control_type
1458 commands_from_control_command (char *arg, struct command_line *cmd)
1459 {
1460 commands_command_1 (arg, 0, cmd);
1461 return simple_control;
1462 }
1463
1464 /* Return non-zero if BL->TARGET_INFO contains valid information. */
1465
1466 static int
1467 bp_location_has_shadow (struct bp_location *bl)
1468 {
1469 if (bl->loc_type != bp_loc_software_breakpoint)
1470 return 0;
1471 if (!bl->inserted)
1472 return 0;
1473 if (bl->target_info.shadow_len == 0)
1474 /* BL isn't valid, or doesn't shadow memory. */
1475 return 0;
1476 return 1;
1477 }
1478
1479 /* Update BUF, which is LEN bytes read from the target address
1480 MEMADDR, by replacing a memory breakpoint with its shadowed
1481 contents.
1482
1483 If READBUF is not NULL, this buffer must not overlap with the of
1484 the breakpoint location's shadow_contents buffer. Otherwise, a
1485 failed assertion internal error will be raised. */
1486
1487 static void
1488 one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1489 const gdb_byte *writebuf_org,
1490 ULONGEST memaddr, LONGEST len,
1491 struct bp_target_info *target_info,
1492 struct gdbarch *gdbarch)
1493 {
1494 /* Now do full processing of the found relevant range of elements. */
1495 CORE_ADDR bp_addr = 0;
1496 int bp_size = 0;
1497 int bptoffset = 0;
1498
1499 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1500 current_program_space->aspace, 0))
1501 {
1502 /* The breakpoint is inserted in a different address space. */
1503 return;
1504 }
1505
1506 /* Addresses and length of the part of the breakpoint that
1507 we need to copy. */
1508 bp_addr = target_info->placed_address;
1509 bp_size = target_info->shadow_len;
1510
1511 if (bp_addr + bp_size <= memaddr)
1512 {
1513 /* The breakpoint is entirely before the chunk of memory we are
1514 reading. */
1515 return;
1516 }
1517
1518 if (bp_addr >= memaddr + len)
1519 {
1520 /* The breakpoint is entirely after the chunk of memory we are
1521 reading. */
1522 return;
1523 }
1524
1525 /* Offset within shadow_contents. */
1526 if (bp_addr < memaddr)
1527 {
1528 /* Only copy the second part of the breakpoint. */
1529 bp_size -= memaddr - bp_addr;
1530 bptoffset = memaddr - bp_addr;
1531 bp_addr = memaddr;
1532 }
1533
1534 if (bp_addr + bp_size > memaddr + len)
1535 {
1536 /* Only copy the first part of the breakpoint. */
1537 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1538 }
1539
1540 if (readbuf != NULL)
1541 {
1542 /* Verify that the readbuf buffer does not overlap with the
1543 shadow_contents buffer. */
1544 gdb_assert (target_info->shadow_contents >= readbuf + len
1545 || readbuf >= (target_info->shadow_contents
1546 + target_info->shadow_len));
1547
1548 /* Update the read buffer with this inserted breakpoint's
1549 shadow. */
1550 memcpy (readbuf + bp_addr - memaddr,
1551 target_info->shadow_contents + bptoffset, bp_size);
1552 }
1553 else
1554 {
1555 const unsigned char *bp;
1556 CORE_ADDR addr = target_info->reqstd_address;
1557 int placed_size;
1558
1559 /* Update the shadow with what we want to write to memory. */
1560 memcpy (target_info->shadow_contents + bptoffset,
1561 writebuf_org + bp_addr - memaddr, bp_size);
1562
1563 /* Determine appropriate breakpoint contents and size for this
1564 address. */
1565 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
1566
1567 /* Update the final write buffer with this inserted
1568 breakpoint's INSN. */
1569 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1570 }
1571 }
1572
1573 /* Update BUF, which is LEN bytes read from the target address MEMADDR,
1574 by replacing any memory breakpoints with their shadowed contents.
1575
1576 If READBUF is not NULL, this buffer must not overlap with any of
1577 the breakpoint location's shadow_contents buffers. Otherwise,
1578 a failed assertion internal error will be raised.
1579
1580 The range of shadowed area by each bp_location is:
1581 bl->address - bp_location_placed_address_before_address_max
1582 up to bl->address + bp_location_shadow_len_after_address_max
1583 The range we were requested to resolve shadows for is:
1584 memaddr ... memaddr + len
1585 Thus the safe cutoff boundaries for performance optimization are
1586 memaddr + len <= (bl->address
1587 - bp_location_placed_address_before_address_max)
1588 and:
1589 bl->address + bp_location_shadow_len_after_address_max <= memaddr */
1590
1591 void
1592 breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1593 const gdb_byte *writebuf_org,
1594 ULONGEST memaddr, LONGEST len)
1595 {
1596 /* Left boundary, right boundary and median element of our binary
1597 search. */
1598 unsigned bc_l, bc_r, bc;
1599 size_t i;
1600
1601 /* Find BC_L which is a leftmost element which may affect BUF
1602 content. It is safe to report lower value but a failure to
1603 report higher one. */
1604
1605 bc_l = 0;
1606 bc_r = bp_location_count;
1607 while (bc_l + 1 < bc_r)
1608 {
1609 struct bp_location *bl;
1610
1611 bc = (bc_l + bc_r) / 2;
1612 bl = bp_location[bc];
1613
1614 /* Check first BL->ADDRESS will not overflow due to the added
1615 constant. Then advance the left boundary only if we are sure
1616 the BC element can in no way affect the BUF content (MEMADDR
1617 to MEMADDR + LEN range).
1618
1619 Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1620 offset so that we cannot miss a breakpoint with its shadow
1621 range tail still reaching MEMADDR. */
1622
1623 if ((bl->address + bp_location_shadow_len_after_address_max
1624 >= bl->address)
1625 && (bl->address + bp_location_shadow_len_after_address_max
1626 <= memaddr))
1627 bc_l = bc;
1628 else
1629 bc_r = bc;
1630 }
1631
1632 /* Due to the binary search above, we need to make sure we pick the
1633 first location that's at BC_L's address. E.g., if there are
1634 multiple locations at the same address, BC_L may end up pointing
1635 at a duplicate location, and miss the "master"/"inserted"
1636 location. Say, given locations L1, L2 and L3 at addresses A and
1637 B:
1638
1639 L1@A, L2@A, L3@B, ...
1640
1641 BC_L could end up pointing at location L2, while the "master"
1642 location could be L1. Since the `loc->inserted' flag is only set
1643 on "master" locations, we'd forget to restore the shadow of L1
1644 and L2. */
1645 while (bc_l > 0
1646 && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1647 bc_l--;
1648
1649 /* Now do full processing of the found relevant range of elements. */
1650
1651 for (bc = bc_l; bc < bp_location_count; bc++)
1652 {
1653 struct bp_location *bl = bp_location[bc];
1654 CORE_ADDR bp_addr = 0;
1655 int bp_size = 0;
1656 int bptoffset = 0;
1657
1658 /* bp_location array has BL->OWNER always non-NULL. */
1659 if (bl->owner->type == bp_none)
1660 warning (_("reading through apparently deleted breakpoint #%d?"),
1661 bl->owner->number);
1662
1663 /* Performance optimization: any further element can no longer affect BUF
1664 content. */
1665
1666 if (bl->address >= bp_location_placed_address_before_address_max
1667 && memaddr + len <= (bl->address
1668 - bp_location_placed_address_before_address_max))
1669 break;
1670
1671 if (!bp_location_has_shadow (bl))
1672 continue;
1673
1674 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1675 memaddr, len, &bl->target_info, bl->gdbarch);
1676 }
1677 }
1678
1679 \f
1680
1681 /* Return true if BPT is either a software breakpoint or a hardware
1682 breakpoint. */
1683
1684 int
1685 is_breakpoint (const struct breakpoint *bpt)
1686 {
1687 return (bpt->type == bp_breakpoint
1688 || bpt->type == bp_hardware_breakpoint
1689 || bpt->type == bp_dprintf);
1690 }
1691
1692 /* Return true if BPT is of any hardware watchpoint kind. */
1693
1694 static int
1695 is_hardware_watchpoint (const struct breakpoint *bpt)
1696 {
1697 return (bpt->type == bp_hardware_watchpoint
1698 || bpt->type == bp_read_watchpoint
1699 || bpt->type == bp_access_watchpoint);
1700 }
1701
1702 /* Return true if BPT is of any watchpoint kind, hardware or
1703 software. */
1704
1705 int
1706 is_watchpoint (const struct breakpoint *bpt)
1707 {
1708 return (is_hardware_watchpoint (bpt)
1709 || bpt->type == bp_watchpoint);
1710 }
1711
1712 /* Returns true if the current thread and its running state are safe
1713 to evaluate or update watchpoint B. Watchpoints on local
1714 expressions need to be evaluated in the context of the thread that
1715 was current when the watchpoint was created, and, that thread needs
1716 to be stopped to be able to select the correct frame context.
1717 Watchpoints on global expressions can be evaluated on any thread,
1718 and in any state. It is presently left to the target allowing
1719 memory accesses when threads are running. */
1720
1721 static int
1722 watchpoint_in_thread_scope (struct watchpoint *b)
1723 {
1724 return (b->base.pspace == current_program_space
1725 && (ptid_equal (b->watchpoint_thread, null_ptid)
1726 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1727 && !is_executing (inferior_ptid))));
1728 }
1729
1730 /* Set watchpoint B to disp_del_at_next_stop, even including its possible
1731 associated bp_watchpoint_scope breakpoint. */
1732
1733 static void
1734 watchpoint_del_at_next_stop (struct watchpoint *w)
1735 {
1736 struct breakpoint *b = &w->base;
1737
1738 if (b->related_breakpoint != b)
1739 {
1740 gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1741 gdb_assert (b->related_breakpoint->related_breakpoint == b);
1742 b->related_breakpoint->disposition = disp_del_at_next_stop;
1743 b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1744 b->related_breakpoint = b;
1745 }
1746 b->disposition = disp_del_at_next_stop;
1747 }
1748
1749 /* Extract a bitfield value from value VAL using the bit parameters contained in
1750 watchpoint W. */
1751
1752 static struct value *
1753 extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1754 {
1755 struct value *bit_val;
1756
1757 if (val == NULL)
1758 return NULL;
1759
1760 bit_val = allocate_value (value_type (val));
1761
1762 unpack_value_bitfield (bit_val,
1763 w->val_bitpos,
1764 w->val_bitsize,
1765 value_contents_for_printing (val),
1766 value_offset (val),
1767 val);
1768
1769 return bit_val;
1770 }
1771
1772 /* Assuming that B is a watchpoint:
1773 - Reparse watchpoint expression, if REPARSE is non-zero
1774 - Evaluate expression and store the result in B->val
1775 - Evaluate the condition if there is one, and store the result
1776 in b->loc->cond.
1777 - Update the list of values that must be watched in B->loc.
1778
1779 If the watchpoint disposition is disp_del_at_next_stop, then do
1780 nothing. If this is local watchpoint that is out of scope, delete
1781 it.
1782
1783 Even with `set breakpoint always-inserted on' the watchpoints are
1784 removed + inserted on each stop here. Normal breakpoints must
1785 never be removed because they might be missed by a running thread
1786 when debugging in non-stop mode. On the other hand, hardware
1787 watchpoints (is_hardware_watchpoint; processed here) are specific
1788 to each LWP since they are stored in each LWP's hardware debug
1789 registers. Therefore, such LWP must be stopped first in order to
1790 be able to modify its hardware watchpoints.
1791
1792 Hardware watchpoints must be reset exactly once after being
1793 presented to the user. It cannot be done sooner, because it would
1794 reset the data used to present the watchpoint hit to the user. And
1795 it must not be done later because it could display the same single
1796 watchpoint hit during multiple GDB stops. Note that the latter is
1797 relevant only to the hardware watchpoint types bp_read_watchpoint
1798 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1799 not user-visible - its hit is suppressed if the memory content has
1800 not changed.
1801
1802 The following constraints influence the location where we can reset
1803 hardware watchpoints:
1804
1805 * target_stopped_by_watchpoint and target_stopped_data_address are
1806 called several times when GDB stops.
1807
1808 [linux]
1809 * Multiple hardware watchpoints can be hit at the same time,
1810 causing GDB to stop. GDB only presents one hardware watchpoint
1811 hit at a time as the reason for stopping, and all the other hits
1812 are presented later, one after the other, each time the user
1813 requests the execution to be resumed. Execution is not resumed
1814 for the threads still having pending hit event stored in
1815 LWP_INFO->STATUS. While the watchpoint is already removed from
1816 the inferior on the first stop the thread hit event is kept being
1817 reported from its cached value by linux_nat_stopped_data_address
1818 until the real thread resume happens after the watchpoint gets
1819 presented and thus its LWP_INFO->STATUS gets reset.
1820
1821 Therefore the hardware watchpoint hit can get safely reset on the
1822 watchpoint removal from inferior. */
1823
1824 static void
1825 update_watchpoint (struct watchpoint *b, int reparse)
1826 {
1827 int within_current_scope;
1828 struct frame_id saved_frame_id;
1829 int frame_saved;
1830
1831 /* If this is a local watchpoint, we only want to check if the
1832 watchpoint frame is in scope if the current thread is the thread
1833 that was used to create the watchpoint. */
1834 if (!watchpoint_in_thread_scope (b))
1835 return;
1836
1837 if (b->base.disposition == disp_del_at_next_stop)
1838 return;
1839
1840 frame_saved = 0;
1841
1842 /* Determine if the watchpoint is within scope. */
1843 if (b->exp_valid_block == NULL)
1844 within_current_scope = 1;
1845 else
1846 {
1847 struct frame_info *fi = get_current_frame ();
1848 struct gdbarch *frame_arch = get_frame_arch (fi);
1849 CORE_ADDR frame_pc = get_frame_pc (fi);
1850
1851 /* If we're at a point where the stack has been destroyed
1852 (e.g. in a function epilogue), unwinding may not work
1853 properly. Do not attempt to recreate locations at this
1854 point. See similar comments in watchpoint_check. */
1855 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
1856 return;
1857
1858 /* Save the current frame's ID so we can restore it after
1859 evaluating the watchpoint expression on its own frame. */
1860 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1861 took a frame parameter, so that we didn't have to change the
1862 selected frame. */
1863 frame_saved = 1;
1864 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1865
1866 fi = frame_find_by_id (b->watchpoint_frame);
1867 within_current_scope = (fi != NULL);
1868 if (within_current_scope)
1869 select_frame (fi);
1870 }
1871
1872 /* We don't free locations. They are stored in the bp_location array
1873 and update_global_location_list will eventually delete them and
1874 remove breakpoints if needed. */
1875 b->base.loc = NULL;
1876
1877 if (within_current_scope && reparse)
1878 {
1879 const char *s;
1880
1881 if (b->exp)
1882 {
1883 xfree (b->exp);
1884 b->exp = NULL;
1885 }
1886 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1887 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
1888 /* If the meaning of expression itself changed, the old value is
1889 no longer relevant. We don't want to report a watchpoint hit
1890 to the user when the old value and the new value may actually
1891 be completely different objects. */
1892 value_free (b->val);
1893 b->val = NULL;
1894 b->val_valid = 0;
1895
1896 /* Note that unlike with breakpoints, the watchpoint's condition
1897 expression is stored in the breakpoint object, not in the
1898 locations (re)created below. */
1899 if (b->base.cond_string != NULL)
1900 {
1901 if (b->cond_exp != NULL)
1902 {
1903 xfree (b->cond_exp);
1904 b->cond_exp = NULL;
1905 }
1906
1907 s = b->base.cond_string;
1908 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
1909 }
1910 }
1911
1912 /* If we failed to parse the expression, for example because
1913 it refers to a global variable in a not-yet-loaded shared library,
1914 don't try to insert watchpoint. We don't automatically delete
1915 such watchpoint, though, since failure to parse expression
1916 is different from out-of-scope watchpoint. */
1917 if (!target_has_execution)
1918 {
1919 /* Without execution, memory can't change. No use to try and
1920 set watchpoint locations. The watchpoint will be reset when
1921 the target gains execution, through breakpoint_re_set. */
1922 if (!can_use_hw_watchpoints)
1923 {
1924 if (b->base.ops->works_in_software_mode (&b->base))
1925 b->base.type = bp_watchpoint;
1926 else
1927 error (_("Can't set read/access watchpoint when "
1928 "hardware watchpoints are disabled."));
1929 }
1930 }
1931 else if (within_current_scope && b->exp)
1932 {
1933 int pc = 0;
1934 struct value *val_chain, *v, *result, *next;
1935 struct program_space *frame_pspace;
1936
1937 fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain, 0);
1938
1939 /* Avoid setting b->val if it's already set. The meaning of
1940 b->val is 'the last value' user saw, and we should update
1941 it only if we reported that last value to user. As it
1942 happens, the code that reports it updates b->val directly.
1943 We don't keep track of the memory value for masked
1944 watchpoints. */
1945 if (!b->val_valid && !is_masked_watchpoint (&b->base))
1946 {
1947 if (b->val_bitsize != 0)
1948 {
1949 v = extract_bitfield_from_watchpoint_value (b, v);
1950 if (v != NULL)
1951 release_value (v);
1952 }
1953 b->val = v;
1954 b->val_valid = 1;
1955 }
1956
1957 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1958
1959 /* Look at each value on the value chain. */
1960 for (v = val_chain; v; v = value_next (v))
1961 {
1962 /* If it's a memory location, and GDB actually needed
1963 its contents to evaluate the expression, then we
1964 must watch it. If the first value returned is
1965 still lazy, that means an error occurred reading it;
1966 watch it anyway in case it becomes readable. */
1967 if (VALUE_LVAL (v) == lval_memory
1968 && (v == val_chain || ! value_lazy (v)))
1969 {
1970 struct type *vtype = check_typedef (value_type (v));
1971
1972 /* We only watch structs and arrays if user asked
1973 for it explicitly, never if they just happen to
1974 appear in the middle of some value chain. */
1975 if (v == result
1976 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1977 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1978 {
1979 CORE_ADDR addr;
1980 enum target_hw_bp_type type;
1981 struct bp_location *loc, **tmp;
1982 int bitpos = 0, bitsize = 0;
1983
1984 if (value_bitsize (v) != 0)
1985 {
1986 /* Extract the bit parameters out from the bitfield
1987 sub-expression. */
1988 bitpos = value_bitpos (v);
1989 bitsize = value_bitsize (v);
1990 }
1991 else if (v == result && b->val_bitsize != 0)
1992 {
1993 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
1994 lvalue whose bit parameters are saved in the fields
1995 VAL_BITPOS and VAL_BITSIZE. */
1996 bitpos = b->val_bitpos;
1997 bitsize = b->val_bitsize;
1998 }
1999
2000 addr = value_address (v);
2001 if (bitsize != 0)
2002 {
2003 /* Skip the bytes that don't contain the bitfield. */
2004 addr += bitpos / 8;
2005 }
2006
2007 type = hw_write;
2008 if (b->base.type == bp_read_watchpoint)
2009 type = hw_read;
2010 else if (b->base.type == bp_access_watchpoint)
2011 type = hw_access;
2012
2013 loc = allocate_bp_location (&b->base);
2014 for (tmp = &(b->base.loc); *tmp != NULL; tmp = &((*tmp)->next))
2015 ;
2016 *tmp = loc;
2017 loc->gdbarch = get_type_arch (value_type (v));
2018
2019 loc->pspace = frame_pspace;
2020 loc->address = addr;
2021
2022 if (bitsize != 0)
2023 {
2024 /* Just cover the bytes that make up the bitfield. */
2025 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2026 }
2027 else
2028 loc->length = TYPE_LENGTH (value_type (v));
2029
2030 loc->watchpoint_type = type;
2031 }
2032 }
2033 }
2034
2035 /* Change the type of breakpoint between hardware assisted or
2036 an ordinary watchpoint depending on the hardware support
2037 and free hardware slots. REPARSE is set when the inferior
2038 is started. */
2039 if (reparse)
2040 {
2041 int reg_cnt;
2042 enum bp_loc_type loc_type;
2043 struct bp_location *bl;
2044
2045 reg_cnt = can_use_hardware_watchpoint (val_chain);
2046
2047 if (reg_cnt)
2048 {
2049 int i, target_resources_ok, other_type_used;
2050 enum bptype type;
2051
2052 /* Use an exact watchpoint when there's only one memory region to be
2053 watched, and only one debug register is needed to watch it. */
2054 b->exact = target_exact_watchpoints && reg_cnt == 1;
2055
2056 /* We need to determine how many resources are already
2057 used for all other hardware watchpoints plus this one
2058 to see if we still have enough resources to also fit
2059 this watchpoint in as well. */
2060
2061 /* If this is a software watchpoint, we try to turn it
2062 to a hardware one -- count resources as if B was of
2063 hardware watchpoint type. */
2064 type = b->base.type;
2065 if (type == bp_watchpoint)
2066 type = bp_hardware_watchpoint;
2067
2068 /* This watchpoint may or may not have been placed on
2069 the list yet at this point (it won't be in the list
2070 if we're trying to create it for the first time,
2071 through watch_command), so always account for it
2072 manually. */
2073
2074 /* Count resources used by all watchpoints except B. */
2075 i = hw_watchpoint_used_count_others (&b->base, type, &other_type_used);
2076
2077 /* Add in the resources needed for B. */
2078 i += hw_watchpoint_use_count (&b->base);
2079
2080 target_resources_ok
2081 = target_can_use_hardware_watchpoint (type, i, other_type_used);
2082 if (target_resources_ok <= 0)
2083 {
2084 int sw_mode = b->base.ops->works_in_software_mode (&b->base);
2085
2086 if (target_resources_ok == 0 && !sw_mode)
2087 error (_("Target does not support this type of "
2088 "hardware watchpoint."));
2089 else if (target_resources_ok < 0 && !sw_mode)
2090 error (_("There are not enough available hardware "
2091 "resources for this watchpoint."));
2092
2093 /* Downgrade to software watchpoint. */
2094 b->base.type = bp_watchpoint;
2095 }
2096 else
2097 {
2098 /* If this was a software watchpoint, we've just
2099 found we have enough resources to turn it to a
2100 hardware watchpoint. Otherwise, this is a
2101 nop. */
2102 b->base.type = type;
2103 }
2104 }
2105 else if (!b->base.ops->works_in_software_mode (&b->base))
2106 {
2107 if (!can_use_hw_watchpoints)
2108 error (_("Can't set read/access watchpoint when "
2109 "hardware watchpoints are disabled."));
2110 else
2111 error (_("Expression cannot be implemented with "
2112 "read/access watchpoint."));
2113 }
2114 else
2115 b->base.type = bp_watchpoint;
2116
2117 loc_type = (b->base.type == bp_watchpoint? bp_loc_other
2118 : bp_loc_hardware_watchpoint);
2119 for (bl = b->base.loc; bl; bl = bl->next)
2120 bl->loc_type = loc_type;
2121 }
2122
2123 for (v = val_chain; v; v = next)
2124 {
2125 next = value_next (v);
2126 if (v != b->val)
2127 value_free (v);
2128 }
2129
2130 /* If a software watchpoint is not watching any memory, then the
2131 above left it without any location set up. But,
2132 bpstat_stop_status requires a location to be able to report
2133 stops, so make sure there's at least a dummy one. */
2134 if (b->base.type == bp_watchpoint && b->base.loc == NULL)
2135 {
2136 struct breakpoint *base = &b->base;
2137 base->loc = allocate_bp_location (base);
2138 base->loc->pspace = frame_pspace;
2139 base->loc->address = -1;
2140 base->loc->length = -1;
2141 base->loc->watchpoint_type = -1;
2142 }
2143 }
2144 else if (!within_current_scope)
2145 {
2146 printf_filtered (_("\
2147 Watchpoint %d deleted because the program has left the block\n\
2148 in which its expression is valid.\n"),
2149 b->base.number);
2150 watchpoint_del_at_next_stop (b);
2151 }
2152
2153 /* Restore the selected frame. */
2154 if (frame_saved)
2155 select_frame (frame_find_by_id (saved_frame_id));
2156 }
2157
2158
2159 /* Returns 1 iff breakpoint location should be
2160 inserted in the inferior. We don't differentiate the type of BL's owner
2161 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2162 breakpoint_ops is not defined, because in insert_bp_location,
2163 tracepoint's insert_location will not be called. */
2164 static int
2165 should_be_inserted (struct bp_location *bl)
2166 {
2167 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
2168 return 0;
2169
2170 if (bl->owner->disposition == disp_del_at_next_stop)
2171 return 0;
2172
2173 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
2174 return 0;
2175
2176 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2177 return 0;
2178
2179 /* This is set for example, when we're attached to the parent of a
2180 vfork, and have detached from the child. The child is running
2181 free, and we expect it to do an exec or exit, at which point the
2182 OS makes the parent schedulable again (and the target reports
2183 that the vfork is done). Until the child is done with the shared
2184 memory region, do not insert breakpoints in the parent, otherwise
2185 the child could still trip on the parent's breakpoints. Since
2186 the parent is blocked anyway, it won't miss any breakpoint. */
2187 if (bl->pspace->breakpoints_not_allowed)
2188 return 0;
2189
2190 /* Don't insert a breakpoint if we're trying to step past its
2191 location. */
2192 if ((bl->loc_type == bp_loc_software_breakpoint
2193 || bl->loc_type == bp_loc_hardware_breakpoint)
2194 && stepping_past_instruction_at (bl->pspace->aspace,
2195 bl->address))
2196 {
2197 if (debug_infrun)
2198 {
2199 fprintf_unfiltered (gdb_stdlog,
2200 "infrun: skipping breakpoint: "
2201 "stepping past insn at: %s\n",
2202 paddress (bl->gdbarch, bl->address));
2203 }
2204 return 0;
2205 }
2206
2207 /* Don't insert watchpoints if we're trying to step past the
2208 instruction that triggered one. */
2209 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2210 && stepping_past_nonsteppable_watchpoint ())
2211 {
2212 if (debug_infrun)
2213 {
2214 fprintf_unfiltered (gdb_stdlog,
2215 "infrun: stepping past non-steppable watchpoint. "
2216 "skipping watchpoint at %s:%d\n",
2217 paddress (bl->gdbarch, bl->address),
2218 bl->length);
2219 }
2220 return 0;
2221 }
2222
2223 return 1;
2224 }
2225
2226 /* Same as should_be_inserted but does the check assuming
2227 that the location is not duplicated. */
2228
2229 static int
2230 unduplicated_should_be_inserted (struct bp_location *bl)
2231 {
2232 int result;
2233 const int save_duplicate = bl->duplicate;
2234
2235 bl->duplicate = 0;
2236 result = should_be_inserted (bl);
2237 bl->duplicate = save_duplicate;
2238 return result;
2239 }
2240
2241 /* Parses a conditional described by an expression COND into an
2242 agent expression bytecode suitable for evaluation
2243 by the bytecode interpreter. Return NULL if there was
2244 any error during parsing. */
2245
2246 static struct agent_expr *
2247 parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2248 {
2249 struct agent_expr *aexpr = NULL;
2250
2251 if (!cond)
2252 return NULL;
2253
2254 /* We don't want to stop processing, so catch any errors
2255 that may show up. */
2256 TRY
2257 {
2258 aexpr = gen_eval_for_expr (scope, cond);
2259 }
2260
2261 CATCH (ex, RETURN_MASK_ERROR)
2262 {
2263 /* If we got here, it means the condition could not be parsed to a valid
2264 bytecode expression and thus can't be evaluated on the target's side.
2265 It's no use iterating through the conditions. */
2266 return NULL;
2267 }
2268 END_CATCH
2269
2270 /* We have a valid agent expression. */
2271 return aexpr;
2272 }
2273
2274 /* Based on location BL, create a list of breakpoint conditions to be
2275 passed on to the target. If we have duplicated locations with different
2276 conditions, we will add such conditions to the list. The idea is that the
2277 target will evaluate the list of conditions and will only notify GDB when
2278 one of them is true. */
2279
2280 static void
2281 build_target_condition_list (struct bp_location *bl)
2282 {
2283 struct bp_location **locp = NULL, **loc2p;
2284 int null_condition_or_parse_error = 0;
2285 int modified = bl->needs_update;
2286 struct bp_location *loc;
2287
2288 /* Release conditions left over from a previous insert. */
2289 VEC_free (agent_expr_p, bl->target_info.conditions);
2290
2291 /* This is only meaningful if the target is
2292 evaluating conditions and if the user has
2293 opted for condition evaluation on the target's
2294 side. */
2295 if (gdb_evaluates_breakpoint_condition_p ()
2296 || !target_supports_evaluation_of_breakpoint_conditions ())
2297 return;
2298
2299 /* Do a first pass to check for locations with no assigned
2300 conditions or conditions that fail to parse to a valid agent expression
2301 bytecode. If any of these happen, then it's no use to send conditions
2302 to the target since this location will always trigger and generate a
2303 response back to GDB. */
2304 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2305 {
2306 loc = (*loc2p);
2307 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2308 {
2309 if (modified)
2310 {
2311 struct agent_expr *aexpr;
2312
2313 /* Re-parse the conditions since something changed. In that
2314 case we already freed the condition bytecodes (see
2315 force_breakpoint_reinsertion). We just
2316 need to parse the condition to bytecodes again. */
2317 aexpr = parse_cond_to_aexpr (bl->address, loc->cond);
2318 loc->cond_bytecode = aexpr;
2319
2320 /* Check if we managed to parse the conditional expression
2321 correctly. If not, we will not send this condition
2322 to the target. */
2323 if (aexpr)
2324 continue;
2325 }
2326
2327 /* If we have a NULL bytecode expression, it means something
2328 went wrong or we have a null condition expression. */
2329 if (!loc->cond_bytecode)
2330 {
2331 null_condition_or_parse_error = 1;
2332 break;
2333 }
2334 }
2335 }
2336
2337 /* If any of these happened, it means we will have to evaluate the conditions
2338 for the location's address on gdb's side. It is no use keeping bytecodes
2339 for all the other duplicate locations, thus we free all of them here.
2340
2341 This is so we have a finer control over which locations' conditions are
2342 being evaluated by GDB or the remote stub. */
2343 if (null_condition_or_parse_error)
2344 {
2345 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2346 {
2347 loc = (*loc2p);
2348 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2349 {
2350 /* Only go as far as the first NULL bytecode is
2351 located. */
2352 if (!loc->cond_bytecode)
2353 return;
2354
2355 free_agent_expr (loc->cond_bytecode);
2356 loc->cond_bytecode = NULL;
2357 }
2358 }
2359 }
2360
2361 /* No NULL conditions or failed bytecode generation. Build a condition list
2362 for this location's address. */
2363 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2364 {
2365 loc = (*loc2p);
2366 if (loc->cond
2367 && is_breakpoint (loc->owner)
2368 && loc->pspace->num == bl->pspace->num
2369 && loc->owner->enable_state == bp_enabled
2370 && loc->enabled)
2371 /* Add the condition to the vector. This will be used later to send the
2372 conditions to the target. */
2373 VEC_safe_push (agent_expr_p, bl->target_info.conditions,
2374 loc->cond_bytecode);
2375 }
2376
2377 return;
2378 }
2379
2380 /* Parses a command described by string CMD into an agent expression
2381 bytecode suitable for evaluation by the bytecode interpreter.
2382 Return NULL if there was any error during parsing. */
2383
2384 static struct agent_expr *
2385 parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2386 {
2387 struct cleanup *old_cleanups = 0;
2388 struct expression *expr, **argvec;
2389 struct agent_expr *aexpr = NULL;
2390 const char *cmdrest;
2391 const char *format_start, *format_end;
2392 struct format_piece *fpieces;
2393 int nargs;
2394 struct gdbarch *gdbarch = get_current_arch ();
2395
2396 if (!cmd)
2397 return NULL;
2398
2399 cmdrest = cmd;
2400
2401 if (*cmdrest == ',')
2402 ++cmdrest;
2403 cmdrest = skip_spaces_const (cmdrest);
2404
2405 if (*cmdrest++ != '"')
2406 error (_("No format string following the location"));
2407
2408 format_start = cmdrest;
2409
2410 fpieces = parse_format_string (&cmdrest);
2411
2412 old_cleanups = make_cleanup (free_format_pieces_cleanup, &fpieces);
2413
2414 format_end = cmdrest;
2415
2416 if (*cmdrest++ != '"')
2417 error (_("Bad format string, non-terminated '\"'."));
2418
2419 cmdrest = skip_spaces_const (cmdrest);
2420
2421 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2422 error (_("Invalid argument syntax"));
2423
2424 if (*cmdrest == ',')
2425 cmdrest++;
2426 cmdrest = skip_spaces_const (cmdrest);
2427
2428 /* For each argument, make an expression. */
2429
2430 argvec = (struct expression **) alloca (strlen (cmd)
2431 * sizeof (struct expression *));
2432
2433 nargs = 0;
2434 while (*cmdrest != '\0')
2435 {
2436 const char *cmd1;
2437
2438 cmd1 = cmdrest;
2439 expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2440 argvec[nargs++] = expr;
2441 cmdrest = cmd1;
2442 if (*cmdrest == ',')
2443 ++cmdrest;
2444 }
2445
2446 /* We don't want to stop processing, so catch any errors
2447 that may show up. */
2448 TRY
2449 {
2450 aexpr = gen_printf (scope, gdbarch, 0, 0,
2451 format_start, format_end - format_start,
2452 fpieces, nargs, argvec);
2453 }
2454 CATCH (ex, RETURN_MASK_ERROR)
2455 {
2456 /* If we got here, it means the command could not be parsed to a valid
2457 bytecode expression and thus can't be evaluated on the target's side.
2458 It's no use iterating through the other commands. */
2459 aexpr = NULL;
2460 }
2461 END_CATCH
2462
2463 do_cleanups (old_cleanups);
2464
2465 /* We have a valid agent expression, return it. */
2466 return aexpr;
2467 }
2468
2469 /* Based on location BL, create a list of breakpoint commands to be
2470 passed on to the target. If we have duplicated locations with
2471 different commands, we will add any such to the list. */
2472
2473 static void
2474 build_target_command_list (struct bp_location *bl)
2475 {
2476 struct bp_location **locp = NULL, **loc2p;
2477 int null_command_or_parse_error = 0;
2478 int modified = bl->needs_update;
2479 struct bp_location *loc;
2480
2481 /* Release commands left over from a previous insert. */
2482 VEC_free (agent_expr_p, bl->target_info.tcommands);
2483
2484 if (!target_can_run_breakpoint_commands ())
2485 return;
2486
2487 /* For now, limit to agent-style dprintf breakpoints. */
2488 if (dprintf_style != dprintf_style_agent)
2489 return;
2490
2491 /* For now, if we have any duplicate location that isn't a dprintf,
2492 don't install the target-side commands, as that would make the
2493 breakpoint not be reported to the core, and we'd lose
2494 control. */
2495 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2496 {
2497 loc = (*loc2p);
2498 if (is_breakpoint (loc->owner)
2499 && loc->pspace->num == bl->pspace->num
2500 && loc->owner->type != bp_dprintf)
2501 return;
2502 }
2503
2504 /* Do a first pass to check for locations with no assigned
2505 conditions or conditions that fail to parse to a valid agent expression
2506 bytecode. If any of these happen, then it's no use to send conditions
2507 to the target since this location will always trigger and generate a
2508 response back to GDB. */
2509 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2510 {
2511 loc = (*loc2p);
2512 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2513 {
2514 if (modified)
2515 {
2516 struct agent_expr *aexpr;
2517
2518 /* Re-parse the commands since something changed. In that
2519 case we already freed the command bytecodes (see
2520 force_breakpoint_reinsertion). We just
2521 need to parse the command to bytecodes again. */
2522 aexpr = parse_cmd_to_aexpr (bl->address,
2523 loc->owner->extra_string);
2524 loc->cmd_bytecode = aexpr;
2525
2526 if (!aexpr)
2527 continue;
2528 }
2529
2530 /* If we have a NULL bytecode expression, it means something
2531 went wrong or we have a null command expression. */
2532 if (!loc->cmd_bytecode)
2533 {
2534 null_command_or_parse_error = 1;
2535 break;
2536 }
2537 }
2538 }
2539
2540 /* If anything failed, then we're not doing target-side commands,
2541 and so clean up. */
2542 if (null_command_or_parse_error)
2543 {
2544 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2545 {
2546 loc = (*loc2p);
2547 if (is_breakpoint (loc->owner)
2548 && loc->pspace->num == bl->pspace->num)
2549 {
2550 /* Only go as far as the first NULL bytecode is
2551 located. */
2552 if (loc->cmd_bytecode == NULL)
2553 return;
2554
2555 free_agent_expr (loc->cmd_bytecode);
2556 loc->cmd_bytecode = NULL;
2557 }
2558 }
2559 }
2560
2561 /* No NULL commands or failed bytecode generation. Build a command list
2562 for this location's address. */
2563 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2564 {
2565 loc = (*loc2p);
2566 if (loc->owner->extra_string
2567 && is_breakpoint (loc->owner)
2568 && loc->pspace->num == bl->pspace->num
2569 && loc->owner->enable_state == bp_enabled
2570 && loc->enabled)
2571 /* Add the command to the vector. This will be used later
2572 to send the commands to the target. */
2573 VEC_safe_push (agent_expr_p, bl->target_info.tcommands,
2574 loc->cmd_bytecode);
2575 }
2576
2577 bl->target_info.persist = 0;
2578 /* Maybe flag this location as persistent. */
2579 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2580 bl->target_info.persist = 1;
2581 }
2582
2583 /* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2584 location. Any error messages are printed to TMP_ERROR_STREAM; and
2585 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
2586 Returns 0 for success, 1 if the bp_location type is not supported or
2587 -1 for failure.
2588
2589 NOTE drow/2003-09-09: This routine could be broken down to an
2590 object-style method for each breakpoint or catchpoint type. */
2591 static int
2592 insert_bp_location (struct bp_location *bl,
2593 struct ui_file *tmp_error_stream,
2594 int *disabled_breaks,
2595 int *hw_breakpoint_error,
2596 int *hw_bp_error_explained_already)
2597 {
2598 enum errors bp_err = GDB_NO_ERROR;
2599 const char *bp_err_message = NULL;
2600
2601 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2602 return 0;
2603
2604 /* Note we don't initialize bl->target_info, as that wipes out
2605 the breakpoint location's shadow_contents if the breakpoint
2606 is still inserted at that location. This in turn breaks
2607 target_read_memory which depends on these buffers when
2608 a memory read is requested at the breakpoint location:
2609 Once the target_info has been wiped, we fail to see that
2610 we have a breakpoint inserted at that address and thus
2611 read the breakpoint instead of returning the data saved in
2612 the breakpoint location's shadow contents. */
2613 bl->target_info.reqstd_address = bl->address;
2614 bl->target_info.placed_address_space = bl->pspace->aspace;
2615 bl->target_info.length = bl->length;
2616
2617 /* When working with target-side conditions, we must pass all the conditions
2618 for the same breakpoint address down to the target since GDB will not
2619 insert those locations. With a list of breakpoint conditions, the target
2620 can decide when to stop and notify GDB. */
2621
2622 if (is_breakpoint (bl->owner))
2623 {
2624 build_target_condition_list (bl);
2625 build_target_command_list (bl);
2626 /* Reset the modification marker. */
2627 bl->needs_update = 0;
2628 }
2629
2630 if (bl->loc_type == bp_loc_software_breakpoint
2631 || bl->loc_type == bp_loc_hardware_breakpoint)
2632 {
2633 if (bl->owner->type != bp_hardware_breakpoint)
2634 {
2635 /* If the explicitly specified breakpoint type
2636 is not hardware breakpoint, check the memory map to see
2637 if the breakpoint address is in read only memory or not.
2638
2639 Two important cases are:
2640 - location type is not hardware breakpoint, memory
2641 is readonly. We change the type of the location to
2642 hardware breakpoint.
2643 - location type is hardware breakpoint, memory is
2644 read-write. This means we've previously made the
2645 location hardware one, but then the memory map changed,
2646 so we undo.
2647
2648 When breakpoints are removed, remove_breakpoints will use
2649 location types we've just set here, the only possible
2650 problem is that memory map has changed during running
2651 program, but it's not going to work anyway with current
2652 gdb. */
2653 struct mem_region *mr
2654 = lookup_mem_region (bl->target_info.reqstd_address);
2655
2656 if (mr)
2657 {
2658 if (automatic_hardware_breakpoints)
2659 {
2660 enum bp_loc_type new_type;
2661
2662 if (mr->attrib.mode != MEM_RW)
2663 new_type = bp_loc_hardware_breakpoint;
2664 else
2665 new_type = bp_loc_software_breakpoint;
2666
2667 if (new_type != bl->loc_type)
2668 {
2669 static int said = 0;
2670
2671 bl->loc_type = new_type;
2672 if (!said)
2673 {
2674 fprintf_filtered (gdb_stdout,
2675 _("Note: automatically using "
2676 "hardware breakpoints for "
2677 "read-only addresses.\n"));
2678 said = 1;
2679 }
2680 }
2681 }
2682 else if (bl->loc_type == bp_loc_software_breakpoint
2683 && mr->attrib.mode != MEM_RW)
2684 {
2685 fprintf_unfiltered (tmp_error_stream,
2686 _("Cannot insert breakpoint %d.\n"
2687 "Cannot set software breakpoint "
2688 "at read-only address %s\n"),
2689 bl->owner->number,
2690 paddress (bl->gdbarch, bl->address));
2691 return 1;
2692 }
2693 }
2694 }
2695
2696 /* First check to see if we have to handle an overlay. */
2697 if (overlay_debugging == ovly_off
2698 || bl->section == NULL
2699 || !(section_is_overlay (bl->section)))
2700 {
2701 /* No overlay handling: just set the breakpoint. */
2702 TRY
2703 {
2704 int val;
2705
2706 val = bl->owner->ops->insert_location (bl);
2707 if (val)
2708 bp_err = GENERIC_ERROR;
2709 }
2710 CATCH (e, RETURN_MASK_ALL)
2711 {
2712 bp_err = e.error;
2713 bp_err_message = e.message;
2714 }
2715 END_CATCH
2716 }
2717 else
2718 {
2719 /* This breakpoint is in an overlay section.
2720 Shall we set a breakpoint at the LMA? */
2721 if (!overlay_events_enabled)
2722 {
2723 /* Yes -- overlay event support is not active,
2724 so we must try to set a breakpoint at the LMA.
2725 This will not work for a hardware breakpoint. */
2726 if (bl->loc_type == bp_loc_hardware_breakpoint)
2727 warning (_("hardware breakpoint %d not supported in overlay!"),
2728 bl->owner->number);
2729 else
2730 {
2731 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2732 bl->section);
2733 /* Set a software (trap) breakpoint at the LMA. */
2734 bl->overlay_target_info = bl->target_info;
2735 bl->overlay_target_info.reqstd_address = addr;
2736
2737 /* No overlay handling: just set the breakpoint. */
2738 TRY
2739 {
2740 int val;
2741
2742 val = target_insert_breakpoint (bl->gdbarch,
2743 &bl->overlay_target_info);
2744 if (val)
2745 bp_err = GENERIC_ERROR;
2746 }
2747 CATCH (e, RETURN_MASK_ALL)
2748 {
2749 bp_err = e.error;
2750 bp_err_message = e.message;
2751 }
2752 END_CATCH
2753
2754 if (bp_err != GDB_NO_ERROR)
2755 fprintf_unfiltered (tmp_error_stream,
2756 "Overlay breakpoint %d "
2757 "failed: in ROM?\n",
2758 bl->owner->number);
2759 }
2760 }
2761 /* Shall we set a breakpoint at the VMA? */
2762 if (section_is_mapped (bl->section))
2763 {
2764 /* Yes. This overlay section is mapped into memory. */
2765 TRY
2766 {
2767 int val;
2768
2769 val = bl->owner->ops->insert_location (bl);
2770 if (val)
2771 bp_err = GENERIC_ERROR;
2772 }
2773 CATCH (e, RETURN_MASK_ALL)
2774 {
2775 bp_err = e.error;
2776 bp_err_message = e.message;
2777 }
2778 END_CATCH
2779 }
2780 else
2781 {
2782 /* No. This breakpoint will not be inserted.
2783 No error, but do not mark the bp as 'inserted'. */
2784 return 0;
2785 }
2786 }
2787
2788 if (bp_err != GDB_NO_ERROR)
2789 {
2790 /* Can't set the breakpoint. */
2791
2792 /* In some cases, we might not be able to insert a
2793 breakpoint in a shared library that has already been
2794 removed, but we have not yet processed the shlib unload
2795 event. Unfortunately, some targets that implement
2796 breakpoint insertion themselves can't tell why the
2797 breakpoint insertion failed (e.g., the remote target
2798 doesn't define error codes), so we must treat generic
2799 errors as memory errors. */
2800 if ((bp_err == GENERIC_ERROR || bp_err == MEMORY_ERROR)
2801 && bl->loc_type == bp_loc_software_breakpoint
2802 && (solib_name_from_address (bl->pspace, bl->address)
2803 || shared_objfile_contains_address_p (bl->pspace,
2804 bl->address)))
2805 {
2806 /* See also: disable_breakpoints_in_shlibs. */
2807 bl->shlib_disabled = 1;
2808 observer_notify_breakpoint_modified (bl->owner);
2809 if (!*disabled_breaks)
2810 {
2811 fprintf_unfiltered (tmp_error_stream,
2812 "Cannot insert breakpoint %d.\n",
2813 bl->owner->number);
2814 fprintf_unfiltered (tmp_error_stream,
2815 "Temporarily disabling shared "
2816 "library breakpoints:\n");
2817 }
2818 *disabled_breaks = 1;
2819 fprintf_unfiltered (tmp_error_stream,
2820 "breakpoint #%d\n", bl->owner->number);
2821 return 0;
2822 }
2823 else
2824 {
2825 if (bl->loc_type == bp_loc_hardware_breakpoint)
2826 {
2827 *hw_breakpoint_error = 1;
2828 *hw_bp_error_explained_already = bp_err_message != NULL;
2829 fprintf_unfiltered (tmp_error_stream,
2830 "Cannot insert hardware breakpoint %d%s",
2831 bl->owner->number, bp_err_message ? ":" : ".\n");
2832 if (bp_err_message != NULL)
2833 fprintf_unfiltered (tmp_error_stream, "%s.\n", bp_err_message);
2834 }
2835 else
2836 {
2837 if (bp_err_message == NULL)
2838 {
2839 char *message
2840 = memory_error_message (TARGET_XFER_E_IO,
2841 bl->gdbarch, bl->address);
2842 struct cleanup *old_chain = make_cleanup (xfree, message);
2843
2844 fprintf_unfiltered (tmp_error_stream,
2845 "Cannot insert breakpoint %d.\n"
2846 "%s\n",
2847 bl->owner->number, message);
2848 do_cleanups (old_chain);
2849 }
2850 else
2851 {
2852 fprintf_unfiltered (tmp_error_stream,
2853 "Cannot insert breakpoint %d: %s\n",
2854 bl->owner->number,
2855 bp_err_message);
2856 }
2857 }
2858 return 1;
2859
2860 }
2861 }
2862 else
2863 bl->inserted = 1;
2864
2865 return 0;
2866 }
2867
2868 else if (bl->loc_type == bp_loc_hardware_watchpoint
2869 /* NOTE drow/2003-09-08: This state only exists for removing
2870 watchpoints. It's not clear that it's necessary... */
2871 && bl->owner->disposition != disp_del_at_next_stop)
2872 {
2873 int val;
2874
2875 gdb_assert (bl->owner->ops != NULL
2876 && bl->owner->ops->insert_location != NULL);
2877
2878 val = bl->owner->ops->insert_location (bl);
2879
2880 /* If trying to set a read-watchpoint, and it turns out it's not
2881 supported, try emulating one with an access watchpoint. */
2882 if (val == 1 && bl->watchpoint_type == hw_read)
2883 {
2884 struct bp_location *loc, **loc_temp;
2885
2886 /* But don't try to insert it, if there's already another
2887 hw_access location that would be considered a duplicate
2888 of this one. */
2889 ALL_BP_LOCATIONS (loc, loc_temp)
2890 if (loc != bl
2891 && loc->watchpoint_type == hw_access
2892 && watchpoint_locations_match (bl, loc))
2893 {
2894 bl->duplicate = 1;
2895 bl->inserted = 1;
2896 bl->target_info = loc->target_info;
2897 bl->watchpoint_type = hw_access;
2898 val = 0;
2899 break;
2900 }
2901
2902 if (val == 1)
2903 {
2904 bl->watchpoint_type = hw_access;
2905 val = bl->owner->ops->insert_location (bl);
2906
2907 if (val)
2908 /* Back to the original value. */
2909 bl->watchpoint_type = hw_read;
2910 }
2911 }
2912
2913 bl->inserted = (val == 0);
2914 }
2915
2916 else if (bl->owner->type == bp_catchpoint)
2917 {
2918 int val;
2919
2920 gdb_assert (bl->owner->ops != NULL
2921 && bl->owner->ops->insert_location != NULL);
2922
2923 val = bl->owner->ops->insert_location (bl);
2924 if (val)
2925 {
2926 bl->owner->enable_state = bp_disabled;
2927
2928 if (val == 1)
2929 warning (_("\
2930 Error inserting catchpoint %d: Your system does not support this type\n\
2931 of catchpoint."), bl->owner->number);
2932 else
2933 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2934 }
2935
2936 bl->inserted = (val == 0);
2937
2938 /* We've already printed an error message if there was a problem
2939 inserting this catchpoint, and we've disabled the catchpoint,
2940 so just return success. */
2941 return 0;
2942 }
2943
2944 return 0;
2945 }
2946
2947 /* This function is called when program space PSPACE is about to be
2948 deleted. It takes care of updating breakpoints to not reference
2949 PSPACE anymore. */
2950
2951 void
2952 breakpoint_program_space_exit (struct program_space *pspace)
2953 {
2954 struct breakpoint *b, *b_temp;
2955 struct bp_location *loc, **loc_temp;
2956
2957 /* Remove any breakpoint that was set through this program space. */
2958 ALL_BREAKPOINTS_SAFE (b, b_temp)
2959 {
2960 if (b->pspace == pspace)
2961 delete_breakpoint (b);
2962 }
2963
2964 /* Breakpoints set through other program spaces could have locations
2965 bound to PSPACE as well. Remove those. */
2966 ALL_BP_LOCATIONS (loc, loc_temp)
2967 {
2968 struct bp_location *tmp;
2969
2970 if (loc->pspace == pspace)
2971 {
2972 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
2973 if (loc->owner->loc == loc)
2974 loc->owner->loc = loc->next;
2975 else
2976 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2977 if (tmp->next == loc)
2978 {
2979 tmp->next = loc->next;
2980 break;
2981 }
2982 }
2983 }
2984
2985 /* Now update the global location list to permanently delete the
2986 removed locations above. */
2987 update_global_location_list (UGLL_DONT_INSERT);
2988 }
2989
2990 /* Make sure all breakpoints are inserted in inferior.
2991 Throws exception on any error.
2992 A breakpoint that is already inserted won't be inserted
2993 again, so calling this function twice is safe. */
2994 void
2995 insert_breakpoints (void)
2996 {
2997 struct breakpoint *bpt;
2998
2999 ALL_BREAKPOINTS (bpt)
3000 if (is_hardware_watchpoint (bpt))
3001 {
3002 struct watchpoint *w = (struct watchpoint *) bpt;
3003
3004 update_watchpoint (w, 0 /* don't reparse. */);
3005 }
3006
3007 /* Updating watchpoints creates new locations, so update the global
3008 location list. Explicitly tell ugll to insert locations and
3009 ignore breakpoints_always_inserted_mode. */
3010 update_global_location_list (UGLL_INSERT);
3011 }
3012
3013 /* Invoke CALLBACK for each of bp_location. */
3014
3015 void
3016 iterate_over_bp_locations (walk_bp_location_callback callback)
3017 {
3018 struct bp_location *loc, **loc_tmp;
3019
3020 ALL_BP_LOCATIONS (loc, loc_tmp)
3021 {
3022 callback (loc, NULL);
3023 }
3024 }
3025
3026 /* This is used when we need to synch breakpoint conditions between GDB and the
3027 target. It is the case with deleting and disabling of breakpoints when using
3028 always-inserted mode. */
3029
3030 static void
3031 update_inserted_breakpoint_locations (void)
3032 {
3033 struct bp_location *bl, **blp_tmp;
3034 int error_flag = 0;
3035 int val = 0;
3036 int disabled_breaks = 0;
3037 int hw_breakpoint_error = 0;
3038 int hw_bp_details_reported = 0;
3039
3040 struct ui_file *tmp_error_stream = mem_fileopen ();
3041 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
3042
3043 /* Explicitly mark the warning -- this will only be printed if
3044 there was an error. */
3045 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
3046
3047 save_current_space_and_thread ();
3048
3049 ALL_BP_LOCATIONS (bl, blp_tmp)
3050 {
3051 /* We only want to update software breakpoints and hardware
3052 breakpoints. */
3053 if (!is_breakpoint (bl->owner))
3054 continue;
3055
3056 /* We only want to update locations that are already inserted
3057 and need updating. This is to avoid unwanted insertion during
3058 deletion of breakpoints. */
3059 if (!bl->inserted || (bl->inserted && !bl->needs_update))
3060 continue;
3061
3062 switch_to_program_space_and_thread (bl->pspace);
3063
3064 /* For targets that support global breakpoints, there's no need
3065 to select an inferior to insert breakpoint to. In fact, even
3066 if we aren't attached to any process yet, we should still
3067 insert breakpoints. */
3068 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3069 && ptid_equal (inferior_ptid, null_ptid))
3070 continue;
3071
3072 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
3073 &hw_breakpoint_error, &hw_bp_details_reported);
3074 if (val)
3075 error_flag = val;
3076 }
3077
3078 if (error_flag)
3079 {
3080 target_terminal_ours_for_output ();
3081 error_stream (tmp_error_stream);
3082 }
3083
3084 do_cleanups (cleanups);
3085 }
3086
3087 /* Used when starting or continuing the program. */
3088
3089 static void
3090 insert_breakpoint_locations (void)
3091 {
3092 struct breakpoint *bpt;
3093 struct bp_location *bl, **blp_tmp;
3094 int error_flag = 0;
3095 int val = 0;
3096 int disabled_breaks = 0;
3097 int hw_breakpoint_error = 0;
3098 int hw_bp_error_explained_already = 0;
3099
3100 struct ui_file *tmp_error_stream = mem_fileopen ();
3101 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
3102
3103 /* Explicitly mark the warning -- this will only be printed if
3104 there was an error. */
3105 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
3106
3107 save_current_space_and_thread ();
3108
3109 ALL_BP_LOCATIONS (bl, blp_tmp)
3110 {
3111 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
3112 continue;
3113
3114 /* There is no point inserting thread-specific breakpoints if
3115 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3116 has BL->OWNER always non-NULL. */
3117 if (bl->owner->thread != -1
3118 && !valid_thread_id (bl->owner->thread))
3119 continue;
3120
3121 switch_to_program_space_and_thread (bl->pspace);
3122
3123 /* For targets that support global breakpoints, there's no need
3124 to select an inferior to insert breakpoint to. In fact, even
3125 if we aren't attached to any process yet, we should still
3126 insert breakpoints. */
3127 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3128 && ptid_equal (inferior_ptid, null_ptid))
3129 continue;
3130
3131 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
3132 &hw_breakpoint_error, &hw_bp_error_explained_already);
3133 if (val)
3134 error_flag = val;
3135 }
3136
3137 /* If we failed to insert all locations of a watchpoint, remove
3138 them, as half-inserted watchpoint is of limited use. */
3139 ALL_BREAKPOINTS (bpt)
3140 {
3141 int some_failed = 0;
3142 struct bp_location *loc;
3143
3144 if (!is_hardware_watchpoint (bpt))
3145 continue;
3146
3147 if (!breakpoint_enabled (bpt))
3148 continue;
3149
3150 if (bpt->disposition == disp_del_at_next_stop)
3151 continue;
3152
3153 for (loc = bpt->loc; loc; loc = loc->next)
3154 if (!loc->inserted && should_be_inserted (loc))
3155 {
3156 some_failed = 1;
3157 break;
3158 }
3159 if (some_failed)
3160 {
3161 for (loc = bpt->loc; loc; loc = loc->next)
3162 if (loc->inserted)
3163 remove_breakpoint (loc, mark_uninserted);
3164
3165 hw_breakpoint_error = 1;
3166 fprintf_unfiltered (tmp_error_stream,
3167 "Could not insert hardware watchpoint %d.\n",
3168 bpt->number);
3169 error_flag = -1;
3170 }
3171 }
3172
3173 if (error_flag)
3174 {
3175 /* If a hardware breakpoint or watchpoint was inserted, add a
3176 message about possibly exhausted resources. */
3177 if (hw_breakpoint_error && !hw_bp_error_explained_already)
3178 {
3179 fprintf_unfiltered (tmp_error_stream,
3180 "Could not insert hardware breakpoints:\n\
3181 You may have requested too many hardware breakpoints/watchpoints.\n");
3182 }
3183 target_terminal_ours_for_output ();
3184 error_stream (tmp_error_stream);
3185 }
3186
3187 do_cleanups (cleanups);
3188 }
3189
3190 /* Used when the program stops.
3191 Returns zero if successful, or non-zero if there was a problem
3192 removing a breakpoint location. */
3193
3194 int
3195 remove_breakpoints (void)
3196 {
3197 struct bp_location *bl, **blp_tmp;
3198 int val = 0;
3199
3200 ALL_BP_LOCATIONS (bl, blp_tmp)
3201 {
3202 if (bl->inserted && !is_tracepoint (bl->owner))
3203 val |= remove_breakpoint (bl, mark_uninserted);
3204 }
3205 return val;
3206 }
3207
3208 /* When a thread exits, remove breakpoints that are related to
3209 that thread. */
3210
3211 static void
3212 remove_threaded_breakpoints (struct thread_info *tp, int silent)
3213 {
3214 struct breakpoint *b, *b_tmp;
3215
3216 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3217 {
3218 if (b->thread == tp->num && user_breakpoint_p (b))
3219 {
3220 b->disposition = disp_del_at_next_stop;
3221
3222 printf_filtered (_("\
3223 Thread-specific breakpoint %d deleted - thread %d no longer in the thread list.\n"),
3224 b->number, tp->num);
3225
3226 /* Hide it from the user. */
3227 b->number = 0;
3228 }
3229 }
3230 }
3231
3232 /* Remove breakpoints of process PID. */
3233
3234 int
3235 remove_breakpoints_pid (int pid)
3236 {
3237 struct bp_location *bl, **blp_tmp;
3238 int val;
3239 struct inferior *inf = find_inferior_pid (pid);
3240
3241 ALL_BP_LOCATIONS (bl, blp_tmp)
3242 {
3243 if (bl->pspace != inf->pspace)
3244 continue;
3245
3246 if (bl->inserted && !bl->target_info.persist)
3247 {
3248 val = remove_breakpoint (bl, mark_uninserted);
3249 if (val != 0)
3250 return val;
3251 }
3252 }
3253 return 0;
3254 }
3255
3256 int
3257 reattach_breakpoints (int pid)
3258 {
3259 struct cleanup *old_chain;
3260 struct bp_location *bl, **blp_tmp;
3261 int val;
3262 struct ui_file *tmp_error_stream;
3263 int dummy1 = 0, dummy2 = 0, dummy3 = 0;
3264 struct inferior *inf;
3265 struct thread_info *tp;
3266
3267 tp = any_live_thread_of_process (pid);
3268 if (tp == NULL)
3269 return 1;
3270
3271 inf = find_inferior_pid (pid);
3272 old_chain = save_inferior_ptid ();
3273
3274 inferior_ptid = tp->ptid;
3275
3276 tmp_error_stream = mem_fileopen ();
3277 make_cleanup_ui_file_delete (tmp_error_stream);
3278
3279 ALL_BP_LOCATIONS (bl, blp_tmp)
3280 {
3281 if (bl->pspace != inf->pspace)
3282 continue;
3283
3284 if (bl->inserted)
3285 {
3286 bl->inserted = 0;
3287 val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2, &dummy3);
3288 if (val != 0)
3289 {
3290 do_cleanups (old_chain);
3291 return val;
3292 }
3293 }
3294 }
3295 do_cleanups (old_chain);
3296 return 0;
3297 }
3298
3299 static int internal_breakpoint_number = -1;
3300
3301 /* Set the breakpoint number of B, depending on the value of INTERNAL.
3302 If INTERNAL is non-zero, the breakpoint number will be populated
3303 from internal_breakpoint_number and that variable decremented.
3304 Otherwise the breakpoint number will be populated from
3305 breakpoint_count and that value incremented. Internal breakpoints
3306 do not set the internal var bpnum. */
3307 static void
3308 set_breakpoint_number (int internal, struct breakpoint *b)
3309 {
3310 if (internal)
3311 b->number = internal_breakpoint_number--;
3312 else
3313 {
3314 set_breakpoint_count (breakpoint_count + 1);
3315 b->number = breakpoint_count;
3316 }
3317 }
3318
3319 static struct breakpoint *
3320 create_internal_breakpoint (struct gdbarch *gdbarch,
3321 CORE_ADDR address, enum bptype type,
3322 const struct breakpoint_ops *ops)
3323 {
3324 struct symtab_and_line sal;
3325 struct breakpoint *b;
3326
3327 init_sal (&sal); /* Initialize to zeroes. */
3328
3329 sal.pc = address;
3330 sal.section = find_pc_overlay (sal.pc);
3331 sal.pspace = current_program_space;
3332
3333 b = set_raw_breakpoint (gdbarch, sal, type, ops);
3334 b->number = internal_breakpoint_number--;
3335 b->disposition = disp_donttouch;
3336
3337 return b;
3338 }
3339
3340 static const char *const longjmp_names[] =
3341 {
3342 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3343 };
3344 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3345
3346 /* Per-objfile data private to breakpoint.c. */
3347 struct breakpoint_objfile_data
3348 {
3349 /* Minimal symbol for "_ovly_debug_event" (if any). */
3350 struct bound_minimal_symbol overlay_msym;
3351
3352 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3353 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
3354
3355 /* True if we have looked for longjmp probes. */
3356 int longjmp_searched;
3357
3358 /* SystemTap probe points for longjmp (if any). */
3359 VEC (probe_p) *longjmp_probes;
3360
3361 /* Minimal symbol for "std::terminate()" (if any). */
3362 struct bound_minimal_symbol terminate_msym;
3363
3364 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3365 struct bound_minimal_symbol exception_msym;
3366
3367 /* True if we have looked for exception probes. */
3368 int exception_searched;
3369
3370 /* SystemTap probe points for unwinding (if any). */
3371 VEC (probe_p) *exception_probes;
3372 };
3373
3374 static const struct objfile_data *breakpoint_objfile_key;
3375
3376 /* Minimal symbol not found sentinel. */
3377 static struct minimal_symbol msym_not_found;
3378
3379 /* Returns TRUE if MSYM point to the "not found" sentinel. */
3380
3381 static int
3382 msym_not_found_p (const struct minimal_symbol *msym)
3383 {
3384 return msym == &msym_not_found;
3385 }
3386
3387 /* Return per-objfile data needed by breakpoint.c.
3388 Allocate the data if necessary. */
3389
3390 static struct breakpoint_objfile_data *
3391 get_breakpoint_objfile_data (struct objfile *objfile)
3392 {
3393 struct breakpoint_objfile_data *bp_objfile_data;
3394
3395 bp_objfile_data = objfile_data (objfile, breakpoint_objfile_key);
3396 if (bp_objfile_data == NULL)
3397 {
3398 bp_objfile_data = obstack_alloc (&objfile->objfile_obstack,
3399 sizeof (*bp_objfile_data));
3400
3401 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
3402 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3403 }
3404 return bp_objfile_data;
3405 }
3406
3407 static void
3408 free_breakpoint_probes (struct objfile *obj, void *data)
3409 {
3410 struct breakpoint_objfile_data *bp_objfile_data = data;
3411
3412 VEC_free (probe_p, bp_objfile_data->longjmp_probes);
3413 VEC_free (probe_p, bp_objfile_data->exception_probes);
3414 }
3415
3416 static void
3417 create_overlay_event_breakpoint (void)
3418 {
3419 struct objfile *objfile;
3420 const char *const func_name = "_ovly_debug_event";
3421
3422 ALL_OBJFILES (objfile)
3423 {
3424 struct breakpoint *b;
3425 struct breakpoint_objfile_data *bp_objfile_data;
3426 CORE_ADDR addr;
3427 char *p;
3428
3429 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3430
3431 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
3432 continue;
3433
3434 if (bp_objfile_data->overlay_msym.minsym == NULL)
3435 {
3436 struct bound_minimal_symbol m;
3437
3438 m = lookup_minimal_symbol_text (func_name, objfile);
3439 if (m.minsym == NULL)
3440 {
3441 /* Avoid future lookups in this objfile. */
3442 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
3443 continue;
3444 }
3445 bp_objfile_data->overlay_msym = m;
3446 }
3447
3448 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
3449 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3450 bp_overlay_event,
3451 &internal_breakpoint_ops);
3452 p = ASTRDUP (func_name);
3453 b->location = new_linespec_location (&p);
3454
3455 if (overlay_debugging == ovly_auto)
3456 {
3457 b->enable_state = bp_enabled;
3458 overlay_events_enabled = 1;
3459 }
3460 else
3461 {
3462 b->enable_state = bp_disabled;
3463 overlay_events_enabled = 0;
3464 }
3465 }
3466 update_global_location_list (UGLL_MAY_INSERT);
3467 }
3468
3469 static void
3470 create_longjmp_master_breakpoint (void)
3471 {
3472 struct program_space *pspace;
3473 struct cleanup *old_chain;
3474
3475 old_chain = save_current_program_space ();
3476
3477 ALL_PSPACES (pspace)
3478 {
3479 struct objfile *objfile;
3480
3481 set_current_program_space (pspace);
3482
3483 ALL_OBJFILES (objfile)
3484 {
3485 int i;
3486 struct gdbarch *gdbarch;
3487 struct breakpoint_objfile_data *bp_objfile_data;
3488
3489 gdbarch = get_objfile_arch (objfile);
3490
3491 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3492
3493 if (!bp_objfile_data->longjmp_searched)
3494 {
3495 VEC (probe_p) *ret;
3496
3497 ret = find_probes_in_objfile (objfile, "libc", "longjmp");
3498 if (ret != NULL)
3499 {
3500 /* We are only interested in checking one element. */
3501 struct probe *p = VEC_index (probe_p, ret, 0);
3502
3503 if (!can_evaluate_probe_arguments (p))
3504 {
3505 /* We cannot use the probe interface here, because it does
3506 not know how to evaluate arguments. */
3507 VEC_free (probe_p, ret);
3508 ret = NULL;
3509 }
3510 }
3511 bp_objfile_data->longjmp_probes = ret;
3512 bp_objfile_data->longjmp_searched = 1;
3513 }
3514
3515 if (bp_objfile_data->longjmp_probes != NULL)
3516 {
3517 int i;
3518 struct probe *probe;
3519 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3520 char *p;
3521
3522 for (i = 0;
3523 VEC_iterate (probe_p,
3524 bp_objfile_data->longjmp_probes,
3525 i, probe);
3526 ++i)
3527 {
3528 struct breakpoint *b;
3529
3530 b = create_internal_breakpoint (gdbarch,
3531 get_probe_address (probe,
3532 objfile),
3533 bp_longjmp_master,
3534 &internal_breakpoint_ops);
3535 p = ASTRDUP ("-probe-stap libc:longjmp");
3536 b->location = new_linespec_location (&p);
3537 b->enable_state = bp_disabled;
3538 }
3539
3540 continue;
3541 }
3542
3543 if (!gdbarch_get_longjmp_target_p (gdbarch))
3544 continue;
3545
3546 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
3547 {
3548 struct breakpoint *b;
3549 const char *func_name;
3550 CORE_ADDR addr;
3551 char *p;
3552
3553 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3554 continue;
3555
3556 func_name = longjmp_names[i];
3557 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3558 {
3559 struct bound_minimal_symbol m;
3560
3561 m = lookup_minimal_symbol_text (func_name, objfile);
3562 if (m.minsym == NULL)
3563 {
3564 /* Prevent future lookups in this objfile. */
3565 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
3566 continue;
3567 }
3568 bp_objfile_data->longjmp_msym[i] = m;
3569 }
3570
3571 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
3572 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3573 &internal_breakpoint_ops);
3574 p = ASTRDUP (func_name);
3575 b->location = new_linespec_location (&p);
3576 b->enable_state = bp_disabled;
3577 }
3578 }
3579 }
3580 update_global_location_list (UGLL_MAY_INSERT);
3581
3582 do_cleanups (old_chain);
3583 }
3584
3585 /* Create a master std::terminate breakpoint. */
3586 static void
3587 create_std_terminate_master_breakpoint (void)
3588 {
3589 struct program_space *pspace;
3590 struct cleanup *old_chain;
3591 const char *const func_name = "std::terminate()";
3592
3593 old_chain = save_current_program_space ();
3594
3595 ALL_PSPACES (pspace)
3596 {
3597 struct objfile *objfile;
3598 CORE_ADDR addr;
3599
3600 set_current_program_space (pspace);
3601
3602 ALL_OBJFILES (objfile)
3603 {
3604 struct breakpoint *b;
3605 struct breakpoint_objfile_data *bp_objfile_data;
3606 char *p;
3607
3608 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3609
3610 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3611 continue;
3612
3613 if (bp_objfile_data->terminate_msym.minsym == NULL)
3614 {
3615 struct bound_minimal_symbol m;
3616
3617 m = lookup_minimal_symbol (func_name, NULL, objfile);
3618 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3619 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
3620 {
3621 /* Prevent future lookups in this objfile. */
3622 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3623 continue;
3624 }
3625 bp_objfile_data->terminate_msym = m;
3626 }
3627
3628 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3629 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3630 bp_std_terminate_master,
3631 &internal_breakpoint_ops);
3632 p = ASTRDUP (func_name);
3633 b->location = new_linespec_location (&p);
3634 b->enable_state = bp_disabled;
3635 }
3636 }
3637
3638 update_global_location_list (UGLL_MAY_INSERT);
3639
3640 do_cleanups (old_chain);
3641 }
3642
3643 /* Install a master breakpoint on the unwinder's debug hook. */
3644
3645 static void
3646 create_exception_master_breakpoint (void)
3647 {
3648 struct objfile *objfile;
3649 const char *const func_name = "_Unwind_DebugHook";
3650
3651 ALL_OBJFILES (objfile)
3652 {
3653 struct breakpoint *b;
3654 struct gdbarch *gdbarch;
3655 struct breakpoint_objfile_data *bp_objfile_data;
3656 CORE_ADDR addr;
3657 char *p;
3658
3659 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3660
3661 /* We prefer the SystemTap probe point if it exists. */
3662 if (!bp_objfile_data->exception_searched)
3663 {
3664 VEC (probe_p) *ret;
3665
3666 ret = find_probes_in_objfile (objfile, "libgcc", "unwind");
3667
3668 if (ret != NULL)
3669 {
3670 /* We are only interested in checking one element. */
3671 struct probe *p = VEC_index (probe_p, ret, 0);
3672
3673 if (!can_evaluate_probe_arguments (p))
3674 {
3675 /* We cannot use the probe interface here, because it does
3676 not know how to evaluate arguments. */
3677 VEC_free (probe_p, ret);
3678 ret = NULL;
3679 }
3680 }
3681 bp_objfile_data->exception_probes = ret;
3682 bp_objfile_data->exception_searched = 1;
3683 }
3684
3685 if (bp_objfile_data->exception_probes != NULL)
3686 {
3687 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3688 int i;
3689 struct probe *probe;
3690
3691 for (i = 0;
3692 VEC_iterate (probe_p,
3693 bp_objfile_data->exception_probes,
3694 i, probe);
3695 ++i)
3696 {
3697 struct breakpoint *b;
3698 char *p;
3699
3700 b = create_internal_breakpoint (gdbarch,
3701 get_probe_address (probe,
3702 objfile),
3703 bp_exception_master,
3704 &internal_breakpoint_ops);
3705 p = ASTRDUP ("-probe-stap libgcc:unwind");
3706 b->location = new_linespec_location (&p);
3707 b->enable_state = bp_disabled;
3708 }
3709
3710 continue;
3711 }
3712
3713 /* Otherwise, try the hook function. */
3714
3715 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3716 continue;
3717
3718 gdbarch = get_objfile_arch (objfile);
3719
3720 if (bp_objfile_data->exception_msym.minsym == NULL)
3721 {
3722 struct bound_minimal_symbol debug_hook;
3723
3724 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3725 if (debug_hook.minsym == NULL)
3726 {
3727 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3728 continue;
3729 }
3730
3731 bp_objfile_data->exception_msym = debug_hook;
3732 }
3733
3734 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
3735 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3736 &current_target);
3737 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3738 &internal_breakpoint_ops);
3739 p = ASTRDUP (func_name);
3740 b->location = new_linespec_location (&p);
3741 b->enable_state = bp_disabled;
3742 }
3743
3744 update_global_location_list (UGLL_MAY_INSERT);
3745 }
3746
3747 void
3748 update_breakpoints_after_exec (void)
3749 {
3750 struct breakpoint *b, *b_tmp;
3751 struct bp_location *bploc, **bplocp_tmp;
3752
3753 /* We're about to delete breakpoints from GDB's lists. If the
3754 INSERTED flag is true, GDB will try to lift the breakpoints by
3755 writing the breakpoints' "shadow contents" back into memory. The
3756 "shadow contents" are NOT valid after an exec, so GDB should not
3757 do that. Instead, the target is responsible from marking
3758 breakpoints out as soon as it detects an exec. We don't do that
3759 here instead, because there may be other attempts to delete
3760 breakpoints after detecting an exec and before reaching here. */
3761 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
3762 if (bploc->pspace == current_program_space)
3763 gdb_assert (!bploc->inserted);
3764
3765 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3766 {
3767 if (b->pspace != current_program_space)
3768 continue;
3769
3770 /* Solib breakpoints must be explicitly reset after an exec(). */
3771 if (b->type == bp_shlib_event)
3772 {
3773 delete_breakpoint (b);
3774 continue;
3775 }
3776
3777 /* JIT breakpoints must be explicitly reset after an exec(). */
3778 if (b->type == bp_jit_event)
3779 {
3780 delete_breakpoint (b);
3781 continue;
3782 }
3783
3784 /* Thread event breakpoints must be set anew after an exec(),
3785 as must overlay event and longjmp master breakpoints. */
3786 if (b->type == bp_thread_event || b->type == bp_overlay_event
3787 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3788 || b->type == bp_exception_master)
3789 {
3790 delete_breakpoint (b);
3791 continue;
3792 }
3793
3794 /* Step-resume breakpoints are meaningless after an exec(). */
3795 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3796 {
3797 delete_breakpoint (b);
3798 continue;
3799 }
3800
3801 /* Just like single-step breakpoints. */
3802 if (b->type == bp_single_step)
3803 {
3804 delete_breakpoint (b);
3805 continue;
3806 }
3807
3808 /* Longjmp and longjmp-resume breakpoints are also meaningless
3809 after an exec. */
3810 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3811 || b->type == bp_longjmp_call_dummy
3812 || b->type == bp_exception || b->type == bp_exception_resume)
3813 {
3814 delete_breakpoint (b);
3815 continue;
3816 }
3817
3818 if (b->type == bp_catchpoint)
3819 {
3820 /* For now, none of the bp_catchpoint breakpoints need to
3821 do anything at this point. In the future, if some of
3822 the catchpoints need to something, we will need to add
3823 a new method, and call this method from here. */
3824 continue;
3825 }
3826
3827 /* bp_finish is a special case. The only way we ought to be able
3828 to see one of these when an exec() has happened, is if the user
3829 caught a vfork, and then said "finish". Ordinarily a finish just
3830 carries them to the call-site of the current callee, by setting
3831 a temporary bp there and resuming. But in this case, the finish
3832 will carry them entirely through the vfork & exec.
3833
3834 We don't want to allow a bp_finish to remain inserted now. But
3835 we can't safely delete it, 'cause finish_command has a handle to
3836 the bp on a bpstat, and will later want to delete it. There's a
3837 chance (and I've seen it happen) that if we delete the bp_finish
3838 here, that its storage will get reused by the time finish_command
3839 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3840 We really must allow finish_command to delete a bp_finish.
3841
3842 In the absence of a general solution for the "how do we know
3843 it's safe to delete something others may have handles to?"
3844 problem, what we'll do here is just uninsert the bp_finish, and
3845 let finish_command delete it.
3846
3847 (We know the bp_finish is "doomed" in the sense that it's
3848 momentary, and will be deleted as soon as finish_command sees
3849 the inferior stopped. So it doesn't matter that the bp's
3850 address is probably bogus in the new a.out, unlike e.g., the
3851 solib breakpoints.) */
3852
3853 if (b->type == bp_finish)
3854 {
3855 continue;
3856 }
3857
3858 /* Without a symbolic address, we have little hope of the
3859 pre-exec() address meaning the same thing in the post-exec()
3860 a.out. */
3861 if (event_location_empty_p (b->location))
3862 {
3863 delete_breakpoint (b);
3864 continue;
3865 }
3866 }
3867 }
3868
3869 int
3870 detach_breakpoints (ptid_t ptid)
3871 {
3872 struct bp_location *bl, **blp_tmp;
3873 int val = 0;
3874 struct cleanup *old_chain = save_inferior_ptid ();
3875 struct inferior *inf = current_inferior ();
3876
3877 if (ptid_get_pid (ptid) == ptid_get_pid (inferior_ptid))
3878 error (_("Cannot detach breakpoints of inferior_ptid"));
3879
3880 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
3881 inferior_ptid = ptid;
3882 ALL_BP_LOCATIONS (bl, blp_tmp)
3883 {
3884 if (bl->pspace != inf->pspace)
3885 continue;
3886
3887 /* This function must physically remove breakpoints locations
3888 from the specified ptid, without modifying the breakpoint
3889 package's state. Locations of type bp_loc_other are only
3890 maintained at GDB side. So, there is no need to remove
3891 these bp_loc_other locations. Moreover, removing these
3892 would modify the breakpoint package's state. */
3893 if (bl->loc_type == bp_loc_other)
3894 continue;
3895
3896 if (bl->inserted)
3897 val |= remove_breakpoint_1 (bl, mark_inserted);
3898 }
3899
3900 do_cleanups (old_chain);
3901 return val;
3902 }
3903
3904 /* Remove the breakpoint location BL from the current address space.
3905 Note that this is used to detach breakpoints from a child fork.
3906 When we get here, the child isn't in the inferior list, and neither
3907 do we have objects to represent its address space --- we should
3908 *not* look at bl->pspace->aspace here. */
3909
3910 static int
3911 remove_breakpoint_1 (struct bp_location *bl, insertion_state_t is)
3912 {
3913 int val;
3914
3915 /* BL is never in moribund_locations by our callers. */
3916 gdb_assert (bl->owner != NULL);
3917
3918 /* The type of none suggests that owner is actually deleted.
3919 This should not ever happen. */
3920 gdb_assert (bl->owner->type != bp_none);
3921
3922 if (bl->loc_type == bp_loc_software_breakpoint
3923 || bl->loc_type == bp_loc_hardware_breakpoint)
3924 {
3925 /* "Normal" instruction breakpoint: either the standard
3926 trap-instruction bp (bp_breakpoint), or a
3927 bp_hardware_breakpoint. */
3928
3929 /* First check to see if we have to handle an overlay. */
3930 if (overlay_debugging == ovly_off
3931 || bl->section == NULL
3932 || !(section_is_overlay (bl->section)))
3933 {
3934 /* No overlay handling: just remove the breakpoint. */
3935
3936 /* If we're trying to uninsert a memory breakpoint that we
3937 know is set in a dynamic object that is marked
3938 shlib_disabled, then either the dynamic object was
3939 removed with "remove-symbol-file" or with
3940 "nosharedlibrary". In the former case, we don't know
3941 whether another dynamic object might have loaded over the
3942 breakpoint's address -- the user might well let us know
3943 about it next with add-symbol-file (the whole point of
3944 add-symbol-file is letting the user manually maintain a
3945 list of dynamically loaded objects). If we have the
3946 breakpoint's shadow memory, that is, this is a software
3947 breakpoint managed by GDB, check whether the breakpoint
3948 is still inserted in memory, to avoid overwriting wrong
3949 code with stale saved shadow contents. Note that HW
3950 breakpoints don't have shadow memory, as they're
3951 implemented using a mechanism that is not dependent on
3952 being able to modify the target's memory, and as such
3953 they should always be removed. */
3954 if (bl->shlib_disabled
3955 && bl->target_info.shadow_len != 0
3956 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3957 val = 0;
3958 else
3959 val = bl->owner->ops->remove_location (bl);
3960 }
3961 else
3962 {
3963 /* This breakpoint is in an overlay section.
3964 Did we set a breakpoint at the LMA? */
3965 if (!overlay_events_enabled)
3966 {
3967 /* Yes -- overlay event support is not active, so we
3968 should have set a breakpoint at the LMA. Remove it.
3969 */
3970 /* Ignore any failures: if the LMA is in ROM, we will
3971 have already warned when we failed to insert it. */
3972 if (bl->loc_type == bp_loc_hardware_breakpoint)
3973 target_remove_hw_breakpoint (bl->gdbarch,
3974 &bl->overlay_target_info);
3975 else
3976 target_remove_breakpoint (bl->gdbarch,
3977 &bl->overlay_target_info);
3978 }
3979 /* Did we set a breakpoint at the VMA?
3980 If so, we will have marked the breakpoint 'inserted'. */
3981 if (bl->inserted)
3982 {
3983 /* Yes -- remove it. Previously we did not bother to
3984 remove the breakpoint if the section had been
3985 unmapped, but let's not rely on that being safe. We
3986 don't know what the overlay manager might do. */
3987
3988 /* However, we should remove *software* breakpoints only
3989 if the section is still mapped, or else we overwrite
3990 wrong code with the saved shadow contents. */
3991 if (bl->loc_type == bp_loc_hardware_breakpoint
3992 || section_is_mapped (bl->section))
3993 val = bl->owner->ops->remove_location (bl);
3994 else
3995 val = 0;
3996 }
3997 else
3998 {
3999 /* No -- not inserted, so no need to remove. No error. */
4000 val = 0;
4001 }
4002 }
4003
4004 /* In some cases, we might not be able to remove a breakpoint in
4005 a shared library that has already been removed, but we have
4006 not yet processed the shlib unload event. Similarly for an
4007 unloaded add-symbol-file object - the user might not yet have
4008 had the chance to remove-symbol-file it. shlib_disabled will
4009 be set if the library/object has already been removed, but
4010 the breakpoint hasn't been uninserted yet, e.g., after
4011 "nosharedlibrary" or "remove-symbol-file" with breakpoints
4012 always-inserted mode. */
4013 if (val
4014 && (bl->loc_type == bp_loc_software_breakpoint
4015 && (bl->shlib_disabled
4016 || solib_name_from_address (bl->pspace, bl->address)
4017 || shared_objfile_contains_address_p (bl->pspace,
4018 bl->address))))
4019 val = 0;
4020
4021 if (val)
4022 return val;
4023 bl->inserted = (is == mark_inserted);
4024 }
4025 else if (bl->loc_type == bp_loc_hardware_watchpoint)
4026 {
4027 gdb_assert (bl->owner->ops != NULL
4028 && bl->owner->ops->remove_location != NULL);
4029
4030 bl->inserted = (is == mark_inserted);
4031 bl->owner->ops->remove_location (bl);
4032
4033 /* Failure to remove any of the hardware watchpoints comes here. */
4034 if ((is == mark_uninserted) && (bl->inserted))
4035 warning (_("Could not remove hardware watchpoint %d."),
4036 bl->owner->number);
4037 }
4038 else if (bl->owner->type == bp_catchpoint
4039 && breakpoint_enabled (bl->owner)
4040 && !bl->duplicate)
4041 {
4042 gdb_assert (bl->owner->ops != NULL
4043 && bl->owner->ops->remove_location != NULL);
4044
4045 val = bl->owner->ops->remove_location (bl);
4046 if (val)
4047 return val;
4048
4049 bl->inserted = (is == mark_inserted);
4050 }
4051
4052 return 0;
4053 }
4054
4055 static int
4056 remove_breakpoint (struct bp_location *bl, insertion_state_t is)
4057 {
4058 int ret;
4059 struct cleanup *old_chain;
4060
4061 /* BL is never in moribund_locations by our callers. */
4062 gdb_assert (bl->owner != NULL);
4063
4064 /* The type of none suggests that owner is actually deleted.
4065 This should not ever happen. */
4066 gdb_assert (bl->owner->type != bp_none);
4067
4068 old_chain = save_current_space_and_thread ();
4069
4070 switch_to_program_space_and_thread (bl->pspace);
4071
4072 ret = remove_breakpoint_1 (bl, is);
4073
4074 do_cleanups (old_chain);
4075 return ret;
4076 }
4077
4078 /* Clear the "inserted" flag in all breakpoints. */
4079
4080 void
4081 mark_breakpoints_out (void)
4082 {
4083 struct bp_location *bl, **blp_tmp;
4084
4085 ALL_BP_LOCATIONS (bl, blp_tmp)
4086 if (bl->pspace == current_program_space)
4087 bl->inserted = 0;
4088 }
4089
4090 /* Clear the "inserted" flag in all breakpoints and delete any
4091 breakpoints which should go away between runs of the program.
4092
4093 Plus other such housekeeping that has to be done for breakpoints
4094 between runs.
4095
4096 Note: this function gets called at the end of a run (by
4097 generic_mourn_inferior) and when a run begins (by
4098 init_wait_for_inferior). */
4099
4100
4101
4102 void
4103 breakpoint_init_inferior (enum inf_context context)
4104 {
4105 struct breakpoint *b, *b_tmp;
4106 struct bp_location *bl, **blp_tmp;
4107 int ix;
4108 struct program_space *pspace = current_program_space;
4109
4110 /* If breakpoint locations are shared across processes, then there's
4111 nothing to do. */
4112 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
4113 return;
4114
4115 mark_breakpoints_out ();
4116
4117 ALL_BREAKPOINTS_SAFE (b, b_tmp)
4118 {
4119 if (b->loc && b->loc->pspace != pspace)
4120 continue;
4121
4122 switch (b->type)
4123 {
4124 case bp_call_dummy:
4125 case bp_longjmp_call_dummy:
4126
4127 /* If the call dummy breakpoint is at the entry point it will
4128 cause problems when the inferior is rerun, so we better get
4129 rid of it. */
4130
4131 case bp_watchpoint_scope:
4132
4133 /* Also get rid of scope breakpoints. */
4134
4135 case bp_shlib_event:
4136
4137 /* Also remove solib event breakpoints. Their addresses may
4138 have changed since the last time we ran the program.
4139 Actually we may now be debugging against different target;
4140 and so the solib backend that installed this breakpoint may
4141 not be used in by the target. E.g.,
4142
4143 (gdb) file prog-linux
4144 (gdb) run # native linux target
4145 ...
4146 (gdb) kill
4147 (gdb) file prog-win.exe
4148 (gdb) tar rem :9999 # remote Windows gdbserver.
4149 */
4150
4151 case bp_step_resume:
4152
4153 /* Also remove step-resume breakpoints. */
4154
4155 case bp_single_step:
4156
4157 /* Also remove single-step breakpoints. */
4158
4159 delete_breakpoint (b);
4160 break;
4161
4162 case bp_watchpoint:
4163 case bp_hardware_watchpoint:
4164 case bp_read_watchpoint:
4165 case bp_access_watchpoint:
4166 {
4167 struct watchpoint *w = (struct watchpoint *) b;
4168
4169 /* Likewise for watchpoints on local expressions. */
4170 if (w->exp_valid_block != NULL)
4171 delete_breakpoint (b);
4172 else if (context == inf_starting)
4173 {
4174 /* Reset val field to force reread of starting value in
4175 insert_breakpoints. */
4176 if (w->val)
4177 value_free (w->val);
4178 w->val = NULL;
4179 w->val_valid = 0;
4180 }
4181 }
4182 break;
4183 default:
4184 break;
4185 }
4186 }
4187
4188 /* Get rid of the moribund locations. */
4189 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
4190 decref_bp_location (&bl);
4191 VEC_free (bp_location_p, moribund_locations);
4192 }
4193
4194 /* These functions concern about actual breakpoints inserted in the
4195 target --- to e.g. check if we need to do decr_pc adjustment or if
4196 we need to hop over the bkpt --- so we check for address space
4197 match, not program space. */
4198
4199 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4200 exists at PC. It returns ordinary_breakpoint_here if it's an
4201 ordinary breakpoint, or permanent_breakpoint_here if it's a
4202 permanent breakpoint.
4203 - When continuing from a location with an ordinary breakpoint, we
4204 actually single step once before calling insert_breakpoints.
4205 - When continuing from a location with a permanent breakpoint, we
4206 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4207 the target, to advance the PC past the breakpoint. */
4208
4209 enum breakpoint_here
4210 breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
4211 {
4212 struct bp_location *bl, **blp_tmp;
4213 int any_breakpoint_here = 0;
4214
4215 ALL_BP_LOCATIONS (bl, blp_tmp)
4216 {
4217 if (bl->loc_type != bp_loc_software_breakpoint
4218 && bl->loc_type != bp_loc_hardware_breakpoint)
4219 continue;
4220
4221 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
4222 if ((breakpoint_enabled (bl->owner)
4223 || bl->permanent)
4224 && breakpoint_location_address_match (bl, aspace, pc))
4225 {
4226 if (overlay_debugging
4227 && section_is_overlay (bl->section)
4228 && !section_is_mapped (bl->section))
4229 continue; /* unmapped overlay -- can't be a match */
4230 else if (bl->permanent)
4231 return permanent_breakpoint_here;
4232 else
4233 any_breakpoint_here = 1;
4234 }
4235 }
4236
4237 return any_breakpoint_here ? ordinary_breakpoint_here : no_breakpoint_here;
4238 }
4239
4240 /* Return true if there's a moribund breakpoint at PC. */
4241
4242 int
4243 moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
4244 {
4245 struct bp_location *loc;
4246 int ix;
4247
4248 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
4249 if (breakpoint_location_address_match (loc, aspace, pc))
4250 return 1;
4251
4252 return 0;
4253 }
4254
4255 /* Returns non-zero iff BL is inserted at PC, in address space
4256 ASPACE. */
4257
4258 static int
4259 bp_location_inserted_here_p (struct bp_location *bl,
4260 struct address_space *aspace, CORE_ADDR pc)
4261 {
4262 if (bl->inserted
4263 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4264 aspace, pc))
4265 {
4266 if (overlay_debugging
4267 && section_is_overlay (bl->section)
4268 && !section_is_mapped (bl->section))
4269 return 0; /* unmapped overlay -- can't be a match */
4270 else
4271 return 1;
4272 }
4273 return 0;
4274 }
4275
4276 /* Returns non-zero iff there's a breakpoint inserted at PC. */
4277
4278 int
4279 breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
4280 {
4281 struct bp_location **blp, **blp_tmp = NULL;
4282 struct bp_location *bl;
4283
4284 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4285 {
4286 struct bp_location *bl = *blp;
4287
4288 if (bl->loc_type != bp_loc_software_breakpoint
4289 && bl->loc_type != bp_loc_hardware_breakpoint)
4290 continue;
4291
4292 if (bp_location_inserted_here_p (bl, aspace, pc))
4293 return 1;
4294 }
4295 return 0;
4296 }
4297
4298 /* This function returns non-zero iff there is a software breakpoint
4299 inserted at PC. */
4300
4301 int
4302 software_breakpoint_inserted_here_p (struct address_space *aspace,
4303 CORE_ADDR pc)
4304 {
4305 struct bp_location **blp, **blp_tmp = NULL;
4306 struct bp_location *bl;
4307
4308 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4309 {
4310 struct bp_location *bl = *blp;
4311
4312 if (bl->loc_type != bp_loc_software_breakpoint)
4313 continue;
4314
4315 if (bp_location_inserted_here_p (bl, aspace, pc))
4316 return 1;
4317 }
4318
4319 return 0;
4320 }
4321
4322 /* See breakpoint.h. */
4323
4324 int
4325 hardware_breakpoint_inserted_here_p (struct address_space *aspace,
4326 CORE_ADDR pc)
4327 {
4328 struct bp_location **blp, **blp_tmp = NULL;
4329 struct bp_location *bl;
4330
4331 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4332 {
4333 struct bp_location *bl = *blp;
4334
4335 if (bl->loc_type != bp_loc_hardware_breakpoint)
4336 continue;
4337
4338 if (bp_location_inserted_here_p (bl, aspace, pc))
4339 return 1;
4340 }
4341
4342 return 0;
4343 }
4344
4345 int
4346 hardware_watchpoint_inserted_in_range (struct address_space *aspace,
4347 CORE_ADDR addr, ULONGEST len)
4348 {
4349 struct breakpoint *bpt;
4350
4351 ALL_BREAKPOINTS (bpt)
4352 {
4353 struct bp_location *loc;
4354
4355 if (bpt->type != bp_hardware_watchpoint
4356 && bpt->type != bp_access_watchpoint)
4357 continue;
4358
4359 if (!breakpoint_enabled (bpt))
4360 continue;
4361
4362 for (loc = bpt->loc; loc; loc = loc->next)
4363 if (loc->pspace->aspace == aspace && loc->inserted)
4364 {
4365 CORE_ADDR l, h;
4366
4367 /* Check for intersection. */
4368 l = max (loc->address, addr);
4369 h = min (loc->address + loc->length, addr + len);
4370 if (l < h)
4371 return 1;
4372 }
4373 }
4374 return 0;
4375 }
4376 \f
4377
4378 /* bpstat stuff. External routines' interfaces are documented
4379 in breakpoint.h. */
4380
4381 int
4382 is_catchpoint (struct breakpoint *ep)
4383 {
4384 return (ep->type == bp_catchpoint);
4385 }
4386
4387 /* Frees any storage that is part of a bpstat. Does not walk the
4388 'next' chain. */
4389
4390 static void
4391 bpstat_free (bpstat bs)
4392 {
4393 if (bs->old_val != NULL)
4394 value_free (bs->old_val);
4395 decref_counted_command_line (&bs->commands);
4396 decref_bp_location (&bs->bp_location_at);
4397 xfree (bs);
4398 }
4399
4400 /* Clear a bpstat so that it says we are not at any breakpoint.
4401 Also free any storage that is part of a bpstat. */
4402
4403 void
4404 bpstat_clear (bpstat *bsp)
4405 {
4406 bpstat p;
4407 bpstat q;
4408
4409 if (bsp == 0)
4410 return;
4411 p = *bsp;
4412 while (p != NULL)
4413 {
4414 q = p->next;
4415 bpstat_free (p);
4416 p = q;
4417 }
4418 *bsp = NULL;
4419 }
4420
4421 /* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4422 is part of the bpstat is copied as well. */
4423
4424 bpstat
4425 bpstat_copy (bpstat bs)
4426 {
4427 bpstat p = NULL;
4428 bpstat tmp;
4429 bpstat retval = NULL;
4430
4431 if (bs == NULL)
4432 return bs;
4433
4434 for (; bs != NULL; bs = bs->next)
4435 {
4436 tmp = (bpstat) xmalloc (sizeof (*tmp));
4437 memcpy (tmp, bs, sizeof (*tmp));
4438 incref_counted_command_line (tmp->commands);
4439 incref_bp_location (tmp->bp_location_at);
4440 if (bs->old_val != NULL)
4441 {
4442 tmp->old_val = value_copy (bs->old_val);
4443 release_value (tmp->old_val);
4444 }
4445
4446 if (p == NULL)
4447 /* This is the first thing in the chain. */
4448 retval = tmp;
4449 else
4450 p->next = tmp;
4451 p = tmp;
4452 }
4453 p->next = NULL;
4454 return retval;
4455 }
4456
4457 /* Find the bpstat associated with this breakpoint. */
4458
4459 bpstat
4460 bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
4461 {
4462 if (bsp == NULL)
4463 return NULL;
4464
4465 for (; bsp != NULL; bsp = bsp->next)
4466 {
4467 if (bsp->breakpoint_at == breakpoint)
4468 return bsp;
4469 }
4470 return NULL;
4471 }
4472
4473 /* See breakpoint.h. */
4474
4475 int
4476 bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
4477 {
4478 for (; bsp != NULL; bsp = bsp->next)
4479 {
4480 if (bsp->breakpoint_at == NULL)
4481 {
4482 /* A moribund location can never explain a signal other than
4483 GDB_SIGNAL_TRAP. */
4484 if (sig == GDB_SIGNAL_TRAP)
4485 return 1;
4486 }
4487 else
4488 {
4489 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4490 sig))
4491 return 1;
4492 }
4493 }
4494
4495 return 0;
4496 }
4497
4498 /* Put in *NUM the breakpoint number of the first breakpoint we are
4499 stopped at. *BSP upon return is a bpstat which points to the
4500 remaining breakpoints stopped at (but which is not guaranteed to be
4501 good for anything but further calls to bpstat_num).
4502
4503 Return 0 if passed a bpstat which does not indicate any breakpoints.
4504 Return -1 if stopped at a breakpoint that has been deleted since
4505 we set it.
4506 Return 1 otherwise. */
4507
4508 int
4509 bpstat_num (bpstat *bsp, int *num)
4510 {
4511 struct breakpoint *b;
4512
4513 if ((*bsp) == NULL)
4514 return 0; /* No more breakpoint values */
4515
4516 /* We assume we'll never have several bpstats that correspond to a
4517 single breakpoint -- otherwise, this function might return the
4518 same number more than once and this will look ugly. */
4519 b = (*bsp)->breakpoint_at;
4520 *bsp = (*bsp)->next;
4521 if (b == NULL)
4522 return -1; /* breakpoint that's been deleted since */
4523
4524 *num = b->number; /* We have its number */
4525 return 1;
4526 }
4527
4528 /* See breakpoint.h. */
4529
4530 void
4531 bpstat_clear_actions (void)
4532 {
4533 struct thread_info *tp;
4534 bpstat bs;
4535
4536 if (ptid_equal (inferior_ptid, null_ptid))
4537 return;
4538
4539 tp = find_thread_ptid (inferior_ptid);
4540 if (tp == NULL)
4541 return;
4542
4543 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
4544 {
4545 decref_counted_command_line (&bs->commands);
4546
4547 if (bs->old_val != NULL)
4548 {
4549 value_free (bs->old_val);
4550 bs->old_val = NULL;
4551 }
4552 }
4553 }
4554
4555 /* Called when a command is about to proceed the inferior. */
4556
4557 static void
4558 breakpoint_about_to_proceed (void)
4559 {
4560 if (!ptid_equal (inferior_ptid, null_ptid))
4561 {
4562 struct thread_info *tp = inferior_thread ();
4563
4564 /* Allow inferior function calls in breakpoint commands to not
4565 interrupt the command list. When the call finishes
4566 successfully, the inferior will be standing at the same
4567 breakpoint as if nothing happened. */
4568 if (tp->control.in_infcall)
4569 return;
4570 }
4571
4572 breakpoint_proceeded = 1;
4573 }
4574
4575 /* Stub for cleaning up our state if we error-out of a breakpoint
4576 command. */
4577 static void
4578 cleanup_executing_breakpoints (void *ignore)
4579 {
4580 executing_breakpoint_commands = 0;
4581 }
4582
4583 /* Return non-zero iff CMD as the first line of a command sequence is `silent'
4584 or its equivalent. */
4585
4586 static int
4587 command_line_is_silent (struct command_line *cmd)
4588 {
4589 return cmd && (strcmp ("silent", cmd->line) == 0);
4590 }
4591
4592 /* Execute all the commands associated with all the breakpoints at
4593 this location. Any of these commands could cause the process to
4594 proceed beyond this point, etc. We look out for such changes by
4595 checking the global "breakpoint_proceeded" after each command.
4596
4597 Returns true if a breakpoint command resumed the inferior. In that
4598 case, it is the caller's responsibility to recall it again with the
4599 bpstat of the current thread. */
4600
4601 static int
4602 bpstat_do_actions_1 (bpstat *bsp)
4603 {
4604 bpstat bs;
4605 struct cleanup *old_chain;
4606 int again = 0;
4607
4608 /* Avoid endless recursion if a `source' command is contained
4609 in bs->commands. */
4610 if (executing_breakpoint_commands)
4611 return 0;
4612
4613 executing_breakpoint_commands = 1;
4614 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
4615
4616 prevent_dont_repeat ();
4617
4618 /* This pointer will iterate over the list of bpstat's. */
4619 bs = *bsp;
4620
4621 breakpoint_proceeded = 0;
4622 for (; bs != NULL; bs = bs->next)
4623 {
4624 struct counted_command_line *ccmd;
4625 struct command_line *cmd;
4626 struct cleanup *this_cmd_tree_chain;
4627
4628 /* Take ownership of the BSP's command tree, if it has one.
4629
4630 The command tree could legitimately contain commands like
4631 'step' and 'next', which call clear_proceed_status, which
4632 frees stop_bpstat's command tree. To make sure this doesn't
4633 free the tree we're executing out from under us, we need to
4634 take ownership of the tree ourselves. Since a given bpstat's
4635 commands are only executed once, we don't need to copy it; we
4636 can clear the pointer in the bpstat, and make sure we free
4637 the tree when we're done. */
4638 ccmd = bs->commands;
4639 bs->commands = NULL;
4640 this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
4641 cmd = ccmd ? ccmd->commands : NULL;
4642 if (command_line_is_silent (cmd))
4643 {
4644 /* The action has been already done by bpstat_stop_status. */
4645 cmd = cmd->next;
4646 }
4647
4648 while (cmd != NULL)
4649 {
4650 execute_control_command (cmd);
4651
4652 if (breakpoint_proceeded)
4653 break;
4654 else
4655 cmd = cmd->next;
4656 }
4657
4658 /* We can free this command tree now. */
4659 do_cleanups (this_cmd_tree_chain);
4660
4661 if (breakpoint_proceeded)
4662 {
4663 if (interpreter_async && target_can_async_p ())
4664 /* If we are in async mode, then the target might be still
4665 running, not stopped at any breakpoint, so nothing for
4666 us to do here -- just return to the event loop. */
4667 ;
4668 else
4669 /* In sync mode, when execute_control_command returns
4670 we're already standing on the next breakpoint.
4671 Breakpoint commands for that stop were not run, since
4672 execute_command does not run breakpoint commands --
4673 only command_line_handler does, but that one is not
4674 involved in execution of breakpoint commands. So, we
4675 can now execute breakpoint commands. It should be
4676 noted that making execute_command do bpstat actions is
4677 not an option -- in this case we'll have recursive
4678 invocation of bpstat for each breakpoint with a
4679 command, and can easily blow up GDB stack. Instead, we
4680 return true, which will trigger the caller to recall us
4681 with the new stop_bpstat. */
4682 again = 1;
4683 break;
4684 }
4685 }
4686 do_cleanups (old_chain);
4687 return again;
4688 }
4689
4690 void
4691 bpstat_do_actions (void)
4692 {
4693 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4694
4695 /* Do any commands attached to breakpoint we are stopped at. */
4696 while (!ptid_equal (inferior_ptid, null_ptid)
4697 && target_has_execution
4698 && !is_exited (inferior_ptid)
4699 && !is_executing (inferior_ptid))
4700 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4701 and only return when it is stopped at the next breakpoint, we
4702 keep doing breakpoint actions until it returns false to
4703 indicate the inferior was not resumed. */
4704 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
4705 break;
4706
4707 discard_cleanups (cleanup_if_error);
4708 }
4709
4710 /* Print out the (old or new) value associated with a watchpoint. */
4711
4712 static void
4713 watchpoint_value_print (struct value *val, struct ui_file *stream)
4714 {
4715 if (val == NULL)
4716 fprintf_unfiltered (stream, _("<unreadable>"));
4717 else
4718 {
4719 struct value_print_options opts;
4720 get_user_print_options (&opts);
4721 value_print (val, stream, &opts);
4722 }
4723 }
4724
4725 /* Generic routine for printing messages indicating why we
4726 stopped. The behavior of this function depends on the value
4727 'print_it' in the bpstat structure. Under some circumstances we
4728 may decide not to print anything here and delegate the task to
4729 normal_stop(). */
4730
4731 static enum print_stop_action
4732 print_bp_stop_message (bpstat bs)
4733 {
4734 switch (bs->print_it)
4735 {
4736 case print_it_noop:
4737 /* Nothing should be printed for this bpstat entry. */
4738 return PRINT_UNKNOWN;
4739 break;
4740
4741 case print_it_done:
4742 /* We still want to print the frame, but we already printed the
4743 relevant messages. */
4744 return PRINT_SRC_AND_LOC;
4745 break;
4746
4747 case print_it_normal:
4748 {
4749 struct breakpoint *b = bs->breakpoint_at;
4750
4751 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4752 which has since been deleted. */
4753 if (b == NULL)
4754 return PRINT_UNKNOWN;
4755
4756 /* Normal case. Call the breakpoint's print_it method. */
4757 return b->ops->print_it (bs);
4758 }
4759 break;
4760
4761 default:
4762 internal_error (__FILE__, __LINE__,
4763 _("print_bp_stop_message: unrecognized enum value"));
4764 break;
4765 }
4766 }
4767
4768 /* A helper function that prints a shared library stopped event. */
4769
4770 static void
4771 print_solib_event (int is_catchpoint)
4772 {
4773 int any_deleted
4774 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4775 int any_added
4776 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4777
4778 if (!is_catchpoint)
4779 {
4780 if (any_added || any_deleted)
4781 ui_out_text (current_uiout,
4782 _("Stopped due to shared library event:\n"));
4783 else
4784 ui_out_text (current_uiout,
4785 _("Stopped due to shared library event (no "
4786 "libraries added or removed)\n"));
4787 }
4788
4789 if (ui_out_is_mi_like_p (current_uiout))
4790 ui_out_field_string (current_uiout, "reason",
4791 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4792
4793 if (any_deleted)
4794 {
4795 struct cleanup *cleanup;
4796 char *name;
4797 int ix;
4798
4799 ui_out_text (current_uiout, _(" Inferior unloaded "));
4800 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4801 "removed");
4802 for (ix = 0;
4803 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4804 ix, name);
4805 ++ix)
4806 {
4807 if (ix > 0)
4808 ui_out_text (current_uiout, " ");
4809 ui_out_field_string (current_uiout, "library", name);
4810 ui_out_text (current_uiout, "\n");
4811 }
4812
4813 do_cleanups (cleanup);
4814 }
4815
4816 if (any_added)
4817 {
4818 struct so_list *iter;
4819 int ix;
4820 struct cleanup *cleanup;
4821
4822 ui_out_text (current_uiout, _(" Inferior loaded "));
4823 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4824 "added");
4825 for (ix = 0;
4826 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4827 ix, iter);
4828 ++ix)
4829 {
4830 if (ix > 0)
4831 ui_out_text (current_uiout, " ");
4832 ui_out_field_string (current_uiout, "library", iter->so_name);
4833 ui_out_text (current_uiout, "\n");
4834 }
4835
4836 do_cleanups (cleanup);
4837 }
4838 }
4839
4840 /* Print a message indicating what happened. This is called from
4841 normal_stop(). The input to this routine is the head of the bpstat
4842 list - a list of the eventpoints that caused this stop. KIND is
4843 the target_waitkind for the stopping event. This
4844 routine calls the generic print routine for printing a message
4845 about reasons for stopping. This will print (for example) the
4846 "Breakpoint n," part of the output. The return value of this
4847 routine is one of:
4848
4849 PRINT_UNKNOWN: Means we printed nothing.
4850 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4851 code to print the location. An example is
4852 "Breakpoint 1, " which should be followed by
4853 the location.
4854 PRINT_SRC_ONLY: Means we printed something, but there is no need
4855 to also print the location part of the message.
4856 An example is the catch/throw messages, which
4857 don't require a location appended to the end.
4858 PRINT_NOTHING: We have done some printing and we don't need any
4859 further info to be printed. */
4860
4861 enum print_stop_action
4862 bpstat_print (bpstat bs, int kind)
4863 {
4864 enum print_stop_action val;
4865
4866 /* Maybe another breakpoint in the chain caused us to stop.
4867 (Currently all watchpoints go on the bpstat whether hit or not.
4868 That probably could (should) be changed, provided care is taken
4869 with respect to bpstat_explains_signal). */
4870 for (; bs; bs = bs->next)
4871 {
4872 val = print_bp_stop_message (bs);
4873 if (val == PRINT_SRC_ONLY
4874 || val == PRINT_SRC_AND_LOC
4875 || val == PRINT_NOTHING)
4876 return val;
4877 }
4878
4879 /* If we had hit a shared library event breakpoint,
4880 print_bp_stop_message would print out this message. If we hit an
4881 OS-level shared library event, do the same thing. */
4882 if (kind == TARGET_WAITKIND_LOADED)
4883 {
4884 print_solib_event (0);
4885 return PRINT_NOTHING;
4886 }
4887
4888 /* We reached the end of the chain, or we got a null BS to start
4889 with and nothing was printed. */
4890 return PRINT_UNKNOWN;
4891 }
4892
4893 /* Evaluate the expression EXP and return 1 if value is zero.
4894 This returns the inverse of the condition because it is called
4895 from catch_errors which returns 0 if an exception happened, and if an
4896 exception happens we want execution to stop.
4897 The argument is a "struct expression *" that has been cast to a
4898 "void *" to make it pass through catch_errors. */
4899
4900 static int
4901 breakpoint_cond_eval (void *exp)
4902 {
4903 struct value *mark = value_mark ();
4904 int i = !value_true (evaluate_expression ((struct expression *) exp));
4905
4906 value_free_to_mark (mark);
4907 return i;
4908 }
4909
4910 /* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
4911
4912 static bpstat
4913 bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
4914 {
4915 bpstat bs;
4916
4917 bs = (bpstat) xmalloc (sizeof (*bs));
4918 bs->next = NULL;
4919 **bs_link_pointer = bs;
4920 *bs_link_pointer = &bs->next;
4921 bs->breakpoint_at = bl->owner;
4922 bs->bp_location_at = bl;
4923 incref_bp_location (bl);
4924 /* If the condition is false, etc., don't do the commands. */
4925 bs->commands = NULL;
4926 bs->old_val = NULL;
4927 bs->print_it = print_it_normal;
4928 return bs;
4929 }
4930 \f
4931 /* The target has stopped with waitstatus WS. Check if any hardware
4932 watchpoints have triggered, according to the target. */
4933
4934 int
4935 watchpoints_triggered (struct target_waitstatus *ws)
4936 {
4937 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
4938 CORE_ADDR addr;
4939 struct breakpoint *b;
4940
4941 if (!stopped_by_watchpoint)
4942 {
4943 /* We were not stopped by a watchpoint. Mark all watchpoints
4944 as not triggered. */
4945 ALL_BREAKPOINTS (b)
4946 if (is_hardware_watchpoint (b))
4947 {
4948 struct watchpoint *w = (struct watchpoint *) b;
4949
4950 w->watchpoint_triggered = watch_triggered_no;
4951 }
4952
4953 return 0;
4954 }
4955
4956 if (!target_stopped_data_address (&current_target, &addr))
4957 {
4958 /* We were stopped by a watchpoint, but we don't know where.
4959 Mark all watchpoints as unknown. */
4960 ALL_BREAKPOINTS (b)
4961 if (is_hardware_watchpoint (b))
4962 {
4963 struct watchpoint *w = (struct watchpoint *) b;
4964
4965 w->watchpoint_triggered = watch_triggered_unknown;
4966 }
4967
4968 return 1;
4969 }
4970
4971 /* The target could report the data address. Mark watchpoints
4972 affected by this data address as triggered, and all others as not
4973 triggered. */
4974
4975 ALL_BREAKPOINTS (b)
4976 if (is_hardware_watchpoint (b))
4977 {
4978 struct watchpoint *w = (struct watchpoint *) b;
4979 struct bp_location *loc;
4980
4981 w->watchpoint_triggered = watch_triggered_no;
4982 for (loc = b->loc; loc; loc = loc->next)
4983 {
4984 if (is_masked_watchpoint (b))
4985 {
4986 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4987 CORE_ADDR start = loc->address & w->hw_wp_mask;
4988
4989 if (newaddr == start)
4990 {
4991 w->watchpoint_triggered = watch_triggered_yes;
4992 break;
4993 }
4994 }
4995 /* Exact match not required. Within range is sufficient. */
4996 else if (target_watchpoint_addr_within_range (&current_target,
4997 addr, loc->address,
4998 loc->length))
4999 {
5000 w->watchpoint_triggered = watch_triggered_yes;
5001 break;
5002 }
5003 }
5004 }
5005
5006 return 1;
5007 }
5008
5009 /* Possible return values for watchpoint_check (this can't be an enum
5010 because of check_errors). */
5011 /* The watchpoint has been deleted. */
5012 #define WP_DELETED 1
5013 /* The value has changed. */
5014 #define WP_VALUE_CHANGED 2
5015 /* The value has not changed. */
5016 #define WP_VALUE_NOT_CHANGED 3
5017 /* Ignore this watchpoint, no matter if the value changed or not. */
5018 #define WP_IGNORE 4
5019
5020 #define BP_TEMPFLAG 1
5021 #define BP_HARDWAREFLAG 2
5022
5023 /* Evaluate watchpoint condition expression and check if its value
5024 changed.
5025
5026 P should be a pointer to struct bpstat, but is defined as a void *
5027 in order for this function to be usable with catch_errors. */
5028
5029 static int
5030 watchpoint_check (void *p)
5031 {
5032 bpstat bs = (bpstat) p;
5033 struct watchpoint *b;
5034 struct frame_info *fr;
5035 int within_current_scope;
5036
5037 /* BS is built from an existing struct breakpoint. */
5038 gdb_assert (bs->breakpoint_at != NULL);
5039 b = (struct watchpoint *) bs->breakpoint_at;
5040
5041 /* If this is a local watchpoint, we only want to check if the
5042 watchpoint frame is in scope if the current thread is the thread
5043 that was used to create the watchpoint. */
5044 if (!watchpoint_in_thread_scope (b))
5045 return WP_IGNORE;
5046
5047 if (b->exp_valid_block == NULL)
5048 within_current_scope = 1;
5049 else
5050 {
5051 struct frame_info *frame = get_current_frame ();
5052 struct gdbarch *frame_arch = get_frame_arch (frame);
5053 CORE_ADDR frame_pc = get_frame_pc (frame);
5054
5055 /* stack_frame_destroyed_p() returns a non-zero value if we're
5056 still in the function but the stack frame has already been
5057 invalidated. Since we can't rely on the values of local
5058 variables after the stack has been destroyed, we are treating
5059 the watchpoint in that state as `not changed' without further
5060 checking. Don't mark watchpoints as changed if the current
5061 frame is in an epilogue - even if they are in some other
5062 frame, our view of the stack is likely to be wrong and
5063 frame_find_by_id could error out. */
5064 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
5065 return WP_IGNORE;
5066
5067 fr = frame_find_by_id (b->watchpoint_frame);
5068 within_current_scope = (fr != NULL);
5069
5070 /* If we've gotten confused in the unwinder, we might have
5071 returned a frame that can't describe this variable. */
5072 if (within_current_scope)
5073 {
5074 struct symbol *function;
5075
5076 function = get_frame_function (fr);
5077 if (function == NULL
5078 || !contained_in (b->exp_valid_block,
5079 SYMBOL_BLOCK_VALUE (function)))
5080 within_current_scope = 0;
5081 }
5082
5083 if (within_current_scope)
5084 /* If we end up stopping, the current frame will get selected
5085 in normal_stop. So this call to select_frame won't affect
5086 the user. */
5087 select_frame (fr);
5088 }
5089
5090 if (within_current_scope)
5091 {
5092 /* We use value_{,free_to_}mark because it could be a *long*
5093 time before we return to the command level and call
5094 free_all_values. We can't call free_all_values because we
5095 might be in the middle of evaluating a function call. */
5096
5097 int pc = 0;
5098 struct value *mark;
5099 struct value *new_val;
5100
5101 if (is_masked_watchpoint (&b->base))
5102 /* Since we don't know the exact trigger address (from
5103 stopped_data_address), just tell the user we've triggered
5104 a mask watchpoint. */
5105 return WP_VALUE_CHANGED;
5106
5107 mark = value_mark ();
5108 fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL, 0);
5109
5110 if (b->val_bitsize != 0)
5111 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5112
5113 /* We use value_equal_contents instead of value_equal because
5114 the latter coerces an array to a pointer, thus comparing just
5115 the address of the array instead of its contents. This is
5116 not what we want. */
5117 if ((b->val != NULL) != (new_val != NULL)
5118 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
5119 {
5120 if (new_val != NULL)
5121 {
5122 release_value (new_val);
5123 value_free_to_mark (mark);
5124 }
5125 bs->old_val = b->val;
5126 b->val = new_val;
5127 b->val_valid = 1;
5128 return WP_VALUE_CHANGED;
5129 }
5130 else
5131 {
5132 /* Nothing changed. */
5133 value_free_to_mark (mark);
5134 return WP_VALUE_NOT_CHANGED;
5135 }
5136 }
5137 else
5138 {
5139 struct ui_out *uiout = current_uiout;
5140
5141 /* This seems like the only logical thing to do because
5142 if we temporarily ignored the watchpoint, then when
5143 we reenter the block in which it is valid it contains
5144 garbage (in the case of a function, it may have two
5145 garbage values, one before and one after the prologue).
5146 So we can't even detect the first assignment to it and
5147 watch after that (since the garbage may or may not equal
5148 the first value assigned). */
5149 /* We print all the stop information in
5150 breakpoint_ops->print_it, but in this case, by the time we
5151 call breakpoint_ops->print_it this bp will be deleted
5152 already. So we have no choice but print the information
5153 here. */
5154 if (ui_out_is_mi_like_p (uiout))
5155 ui_out_field_string
5156 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
5157 ui_out_text (uiout, "\nWatchpoint ");
5158 ui_out_field_int (uiout, "wpnum", b->base.number);
5159 ui_out_text (uiout,
5160 " deleted because the program has left the block in\n\
5161 which its expression is valid.\n");
5162
5163 /* Make sure the watchpoint's commands aren't executed. */
5164 decref_counted_command_line (&b->base.commands);
5165 watchpoint_del_at_next_stop (b);
5166
5167 return WP_DELETED;
5168 }
5169 }
5170
5171 /* Return true if it looks like target has stopped due to hitting
5172 breakpoint location BL. This function does not check if we should
5173 stop, only if BL explains the stop. */
5174
5175 static int
5176 bpstat_check_location (const struct bp_location *bl,
5177 struct address_space *aspace, CORE_ADDR bp_addr,
5178 const struct target_waitstatus *ws)
5179 {
5180 struct breakpoint *b = bl->owner;
5181
5182 /* BL is from an existing breakpoint. */
5183 gdb_assert (b != NULL);
5184
5185 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
5186 }
5187
5188 /* Determine if the watched values have actually changed, and we
5189 should stop. If not, set BS->stop to 0. */
5190
5191 static void
5192 bpstat_check_watchpoint (bpstat bs)
5193 {
5194 const struct bp_location *bl;
5195 struct watchpoint *b;
5196
5197 /* BS is built for existing struct breakpoint. */
5198 bl = bs->bp_location_at;
5199 gdb_assert (bl != NULL);
5200 b = (struct watchpoint *) bs->breakpoint_at;
5201 gdb_assert (b != NULL);
5202
5203 {
5204 int must_check_value = 0;
5205
5206 if (b->base.type == bp_watchpoint)
5207 /* For a software watchpoint, we must always check the
5208 watched value. */
5209 must_check_value = 1;
5210 else if (b->watchpoint_triggered == watch_triggered_yes)
5211 /* We have a hardware watchpoint (read, write, or access)
5212 and the target earlier reported an address watched by
5213 this watchpoint. */
5214 must_check_value = 1;
5215 else if (b->watchpoint_triggered == watch_triggered_unknown
5216 && b->base.type == bp_hardware_watchpoint)
5217 /* We were stopped by a hardware watchpoint, but the target could
5218 not report the data address. We must check the watchpoint's
5219 value. Access and read watchpoints are out of luck; without
5220 a data address, we can't figure it out. */
5221 must_check_value = 1;
5222
5223 if (must_check_value)
5224 {
5225 char *message
5226 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
5227 b->base.number);
5228 struct cleanup *cleanups = make_cleanup (xfree, message);
5229 int e = catch_errors (watchpoint_check, bs, message,
5230 RETURN_MASK_ALL);
5231 do_cleanups (cleanups);
5232 switch (e)
5233 {
5234 case WP_DELETED:
5235 /* We've already printed what needs to be printed. */
5236 bs->print_it = print_it_done;
5237 /* Stop. */
5238 break;
5239 case WP_IGNORE:
5240 bs->print_it = print_it_noop;
5241 bs->stop = 0;
5242 break;
5243 case WP_VALUE_CHANGED:
5244 if (b->base.type == bp_read_watchpoint)
5245 {
5246 /* There are two cases to consider here:
5247
5248 1. We're watching the triggered memory for reads.
5249 In that case, trust the target, and always report
5250 the watchpoint hit to the user. Even though
5251 reads don't cause value changes, the value may
5252 have changed since the last time it was read, and
5253 since we're not trapping writes, we will not see
5254 those, and as such we should ignore our notion of
5255 old value.
5256
5257 2. We're watching the triggered memory for both
5258 reads and writes. There are two ways this may
5259 happen:
5260
5261 2.1. This is a target that can't break on data
5262 reads only, but can break on accesses (reads or
5263 writes), such as e.g., x86. We detect this case
5264 at the time we try to insert read watchpoints.
5265
5266 2.2. Otherwise, the target supports read
5267 watchpoints, but, the user set an access or write
5268 watchpoint watching the same memory as this read
5269 watchpoint.
5270
5271 If we're watching memory writes as well as reads,
5272 ignore watchpoint hits when we find that the
5273 value hasn't changed, as reads don't cause
5274 changes. This still gives false positives when
5275 the program writes the same value to memory as
5276 what there was already in memory (we will confuse
5277 it for a read), but it's much better than
5278 nothing. */
5279
5280 int other_write_watchpoint = 0;
5281
5282 if (bl->watchpoint_type == hw_read)
5283 {
5284 struct breakpoint *other_b;
5285
5286 ALL_BREAKPOINTS (other_b)
5287 if (other_b->type == bp_hardware_watchpoint
5288 || other_b->type == bp_access_watchpoint)
5289 {
5290 struct watchpoint *other_w =
5291 (struct watchpoint *) other_b;
5292
5293 if (other_w->watchpoint_triggered
5294 == watch_triggered_yes)
5295 {
5296 other_write_watchpoint = 1;
5297 break;
5298 }
5299 }
5300 }
5301
5302 if (other_write_watchpoint
5303 || bl->watchpoint_type == hw_access)
5304 {
5305 /* We're watching the same memory for writes,
5306 and the value changed since the last time we
5307 updated it, so this trap must be for a write.
5308 Ignore it. */
5309 bs->print_it = print_it_noop;
5310 bs->stop = 0;
5311 }
5312 }
5313 break;
5314 case WP_VALUE_NOT_CHANGED:
5315 if (b->base.type == bp_hardware_watchpoint
5316 || b->base.type == bp_watchpoint)
5317 {
5318 /* Don't stop: write watchpoints shouldn't fire if
5319 the value hasn't changed. */
5320 bs->print_it = print_it_noop;
5321 bs->stop = 0;
5322 }
5323 /* Stop. */
5324 break;
5325 default:
5326 /* Can't happen. */
5327 case 0:
5328 /* Error from catch_errors. */
5329 printf_filtered (_("Watchpoint %d deleted.\n"), b->base.number);
5330 watchpoint_del_at_next_stop (b);
5331 /* We've already printed what needs to be printed. */
5332 bs->print_it = print_it_done;
5333 break;
5334 }
5335 }
5336 else /* must_check_value == 0 */
5337 {
5338 /* This is a case where some watchpoint(s) triggered, but
5339 not at the address of this watchpoint, or else no
5340 watchpoint triggered after all. So don't print
5341 anything for this watchpoint. */
5342 bs->print_it = print_it_noop;
5343 bs->stop = 0;
5344 }
5345 }
5346 }
5347
5348 /* For breakpoints that are currently marked as telling gdb to stop,
5349 check conditions (condition proper, frame, thread and ignore count)
5350 of breakpoint referred to by BS. If we should not stop for this
5351 breakpoint, set BS->stop to 0. */
5352
5353 static void
5354 bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5355 {
5356 const struct bp_location *bl;
5357 struct breakpoint *b;
5358 int value_is_zero = 0;
5359 struct expression *cond;
5360
5361 gdb_assert (bs->stop);
5362
5363 /* BS is built for existing struct breakpoint. */
5364 bl = bs->bp_location_at;
5365 gdb_assert (bl != NULL);
5366 b = bs->breakpoint_at;
5367 gdb_assert (b != NULL);
5368
5369 /* Even if the target evaluated the condition on its end and notified GDB, we
5370 need to do so again since GDB does not know if we stopped due to a
5371 breakpoint or a single step breakpoint. */
5372
5373 if (frame_id_p (b->frame_id)
5374 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
5375 {
5376 bs->stop = 0;
5377 return;
5378 }
5379
5380 /* If this is a thread/task-specific breakpoint, don't waste cpu
5381 evaluating the condition if this isn't the specified
5382 thread/task. */
5383 if ((b->thread != -1 && b->thread != pid_to_thread_id (ptid))
5384 || (b->task != 0 && b->task != ada_get_task_number (ptid)))
5385
5386 {
5387 bs->stop = 0;
5388 return;
5389 }
5390
5391 /* Evaluate extension language breakpoints that have a "stop" method
5392 implemented. */
5393 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
5394
5395 if (is_watchpoint (b))
5396 {
5397 struct watchpoint *w = (struct watchpoint *) b;
5398
5399 cond = w->cond_exp;
5400 }
5401 else
5402 cond = bl->cond;
5403
5404 if (cond && b->disposition != disp_del_at_next_stop)
5405 {
5406 int within_current_scope = 1;
5407 struct watchpoint * w;
5408
5409 /* We use value_mark and value_free_to_mark because it could
5410 be a long time before we return to the command level and
5411 call free_all_values. We can't call free_all_values
5412 because we might be in the middle of evaluating a
5413 function call. */
5414 struct value *mark = value_mark ();
5415
5416 if (is_watchpoint (b))
5417 w = (struct watchpoint *) b;
5418 else
5419 w = NULL;
5420
5421 /* Need to select the frame, with all that implies so that
5422 the conditions will have the right context. Because we
5423 use the frame, we will not see an inlined function's
5424 variables when we arrive at a breakpoint at the start
5425 of the inlined function; the current frame will be the
5426 call site. */
5427 if (w == NULL || w->cond_exp_valid_block == NULL)
5428 select_frame (get_current_frame ());
5429 else
5430 {
5431 struct frame_info *frame;
5432
5433 /* For local watchpoint expressions, which particular
5434 instance of a local is being watched matters, so we
5435 keep track of the frame to evaluate the expression
5436 in. To evaluate the condition however, it doesn't
5437 really matter which instantiation of the function
5438 where the condition makes sense triggers the
5439 watchpoint. This allows an expression like "watch
5440 global if q > 10" set in `func', catch writes to
5441 global on all threads that call `func', or catch
5442 writes on all recursive calls of `func' by a single
5443 thread. We simply always evaluate the condition in
5444 the innermost frame that's executing where it makes
5445 sense to evaluate the condition. It seems
5446 intuitive. */
5447 frame = block_innermost_frame (w->cond_exp_valid_block);
5448 if (frame != NULL)
5449 select_frame (frame);
5450 else
5451 within_current_scope = 0;
5452 }
5453 if (within_current_scope)
5454 value_is_zero
5455 = catch_errors (breakpoint_cond_eval, cond,
5456 "Error in testing breakpoint condition:\n",
5457 RETURN_MASK_ALL);
5458 else
5459 {
5460 warning (_("Watchpoint condition cannot be tested "
5461 "in the current scope"));
5462 /* If we failed to set the right context for this
5463 watchpoint, unconditionally report it. */
5464 value_is_zero = 0;
5465 }
5466 /* FIXME-someday, should give breakpoint #. */
5467 value_free_to_mark (mark);
5468 }
5469
5470 if (cond && value_is_zero)
5471 {
5472 bs->stop = 0;
5473 }
5474 else if (b->ignore_count > 0)
5475 {
5476 b->ignore_count--;
5477 bs->stop = 0;
5478 /* Increase the hit count even though we don't stop. */
5479 ++(b->hit_count);
5480 observer_notify_breakpoint_modified (b);
5481 }
5482 }
5483
5484 /* Returns true if we need to track moribund locations of LOC's type
5485 on the current target. */
5486
5487 static int
5488 need_moribund_for_location_type (struct bp_location *loc)
5489 {
5490 return ((loc->loc_type == bp_loc_software_breakpoint
5491 && !target_supports_stopped_by_sw_breakpoint ())
5492 || (loc->loc_type == bp_loc_hardware_breakpoint
5493 && !target_supports_stopped_by_hw_breakpoint ()));
5494 }
5495
5496
5497 /* Get a bpstat associated with having just stopped at address
5498 BP_ADDR in thread PTID.
5499
5500 Determine whether we stopped at a breakpoint, etc, or whether we
5501 don't understand this stop. Result is a chain of bpstat's such
5502 that:
5503
5504 if we don't understand the stop, the result is a null pointer.
5505
5506 if we understand why we stopped, the result is not null.
5507
5508 Each element of the chain refers to a particular breakpoint or
5509 watchpoint at which we have stopped. (We may have stopped for
5510 several reasons concurrently.)
5511
5512 Each element of the chain has valid next, breakpoint_at,
5513 commands, FIXME??? fields. */
5514
5515 bpstat
5516 bpstat_stop_status (struct address_space *aspace,
5517 CORE_ADDR bp_addr, ptid_t ptid,
5518 const struct target_waitstatus *ws)
5519 {
5520 struct breakpoint *b = NULL;
5521 struct bp_location *bl;
5522 struct bp_location *loc;
5523 /* First item of allocated bpstat's. */
5524 bpstat bs_head = NULL, *bs_link = &bs_head;
5525 /* Pointer to the last thing in the chain currently. */
5526 bpstat bs;
5527 int ix;
5528 int need_remove_insert;
5529 int removed_any;
5530
5531 /* First, build the bpstat chain with locations that explain a
5532 target stop, while being careful to not set the target running,
5533 as that may invalidate locations (in particular watchpoint
5534 locations are recreated). Resuming will happen here with
5535 breakpoint conditions or watchpoint expressions that include
5536 inferior function calls. */
5537
5538 ALL_BREAKPOINTS (b)
5539 {
5540 if (!breakpoint_enabled (b))
5541 continue;
5542
5543 for (bl = b->loc; bl != NULL; bl = bl->next)
5544 {
5545 /* For hardware watchpoints, we look only at the first
5546 location. The watchpoint_check function will work on the
5547 entire expression, not the individual locations. For
5548 read watchpoints, the watchpoints_triggered function has
5549 checked all locations already. */
5550 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5551 break;
5552
5553 if (!bl->enabled || bl->shlib_disabled)
5554 continue;
5555
5556 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
5557 continue;
5558
5559 /* Come here if it's a watchpoint, or if the break address
5560 matches. */
5561
5562 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
5563 explain stop. */
5564
5565 /* Assume we stop. Should we find a watchpoint that is not
5566 actually triggered, or if the condition of the breakpoint
5567 evaluates as false, we'll reset 'stop' to 0. */
5568 bs->stop = 1;
5569 bs->print = 1;
5570
5571 /* If this is a scope breakpoint, mark the associated
5572 watchpoint as triggered so that we will handle the
5573 out-of-scope event. We'll get to the watchpoint next
5574 iteration. */
5575 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
5576 {
5577 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5578
5579 w->watchpoint_triggered = watch_triggered_yes;
5580 }
5581 }
5582 }
5583
5584 /* Check if a moribund breakpoint explains the stop. */
5585 if (!target_supports_stopped_by_sw_breakpoint ()
5586 || !target_supports_stopped_by_hw_breakpoint ())
5587 {
5588 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
5589 {
5590 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5591 && need_moribund_for_location_type (loc))
5592 {
5593 bs = bpstat_alloc (loc, &bs_link);
5594 /* For hits of moribund locations, we should just proceed. */
5595 bs->stop = 0;
5596 bs->print = 0;
5597 bs->print_it = print_it_noop;
5598 }
5599 }
5600 }
5601
5602 /* A bit of special processing for shlib breakpoints. We need to
5603 process solib loading here, so that the lists of loaded and
5604 unloaded libraries are correct before we handle "catch load" and
5605 "catch unload". */
5606 for (bs = bs_head; bs != NULL; bs = bs->next)
5607 {
5608 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
5609 {
5610 handle_solib_event ();
5611 break;
5612 }
5613 }
5614
5615 /* Now go through the locations that caused the target to stop, and
5616 check whether we're interested in reporting this stop to higher
5617 layers, or whether we should resume the target transparently. */
5618
5619 removed_any = 0;
5620
5621 for (bs = bs_head; bs != NULL; bs = bs->next)
5622 {
5623 if (!bs->stop)
5624 continue;
5625
5626 b = bs->breakpoint_at;
5627 b->ops->check_status (bs);
5628 if (bs->stop)
5629 {
5630 bpstat_check_breakpoint_conditions (bs, ptid);
5631
5632 if (bs->stop)
5633 {
5634 ++(b->hit_count);
5635 observer_notify_breakpoint_modified (b);
5636
5637 /* We will stop here. */
5638 if (b->disposition == disp_disable)
5639 {
5640 --(b->enable_count);
5641 if (b->enable_count <= 0)
5642 b->enable_state = bp_disabled;
5643 removed_any = 1;
5644 }
5645 if (b->silent)
5646 bs->print = 0;
5647 bs->commands = b->commands;
5648 incref_counted_command_line (bs->commands);
5649 if (command_line_is_silent (bs->commands
5650 ? bs->commands->commands : NULL))
5651 bs->print = 0;
5652
5653 b->ops->after_condition_true (bs);
5654 }
5655
5656 }
5657
5658 /* Print nothing for this entry if we don't stop or don't
5659 print. */
5660 if (!bs->stop || !bs->print)
5661 bs->print_it = print_it_noop;
5662 }
5663
5664 /* If we aren't stopping, the value of some hardware watchpoint may
5665 not have changed, but the intermediate memory locations we are
5666 watching may have. Don't bother if we're stopping; this will get
5667 done later. */
5668 need_remove_insert = 0;
5669 if (! bpstat_causes_stop (bs_head))
5670 for (bs = bs_head; bs != NULL; bs = bs->next)
5671 if (!bs->stop
5672 && bs->breakpoint_at
5673 && is_hardware_watchpoint (bs->breakpoint_at))
5674 {
5675 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5676
5677 update_watchpoint (w, 0 /* don't reparse. */);
5678 need_remove_insert = 1;
5679 }
5680
5681 if (need_remove_insert)
5682 update_global_location_list (UGLL_MAY_INSERT);
5683 else if (removed_any)
5684 update_global_location_list (UGLL_DONT_INSERT);
5685
5686 return bs_head;
5687 }
5688
5689 static void
5690 handle_jit_event (void)
5691 {
5692 struct frame_info *frame;
5693 struct gdbarch *gdbarch;
5694
5695 /* Switch terminal for any messages produced by
5696 breakpoint_re_set. */
5697 target_terminal_ours_for_output ();
5698
5699 frame = get_current_frame ();
5700 gdbarch = get_frame_arch (frame);
5701
5702 jit_event_handler (gdbarch);
5703
5704 target_terminal_inferior ();
5705 }
5706
5707 /* Prepare WHAT final decision for infrun. */
5708
5709 /* Decide what infrun needs to do with this bpstat. */
5710
5711 struct bpstat_what
5712 bpstat_what (bpstat bs_head)
5713 {
5714 struct bpstat_what retval;
5715 int jit_event = 0;
5716 bpstat bs;
5717
5718 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
5719 retval.call_dummy = STOP_NONE;
5720 retval.is_longjmp = 0;
5721
5722 for (bs = bs_head; bs != NULL; bs = bs->next)
5723 {
5724 /* Extract this BS's action. After processing each BS, we check
5725 if its action overrides all we've seem so far. */
5726 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5727 enum bptype bptype;
5728
5729 if (bs->breakpoint_at == NULL)
5730 {
5731 /* I suspect this can happen if it was a momentary
5732 breakpoint which has since been deleted. */
5733 bptype = bp_none;
5734 }
5735 else
5736 bptype = bs->breakpoint_at->type;
5737
5738 switch (bptype)
5739 {
5740 case bp_none:
5741 break;
5742 case bp_breakpoint:
5743 case bp_hardware_breakpoint:
5744 case bp_single_step:
5745 case bp_until:
5746 case bp_finish:
5747 case bp_shlib_event:
5748 if (bs->stop)
5749 {
5750 if (bs->print)
5751 this_action = BPSTAT_WHAT_STOP_NOISY;
5752 else
5753 this_action = BPSTAT_WHAT_STOP_SILENT;
5754 }
5755 else
5756 this_action = BPSTAT_WHAT_SINGLE;
5757 break;
5758 case bp_watchpoint:
5759 case bp_hardware_watchpoint:
5760 case bp_read_watchpoint:
5761 case bp_access_watchpoint:
5762 if (bs->stop)
5763 {
5764 if (bs->print)
5765 this_action = BPSTAT_WHAT_STOP_NOISY;
5766 else
5767 this_action = BPSTAT_WHAT_STOP_SILENT;
5768 }
5769 else
5770 {
5771 /* There was a watchpoint, but we're not stopping.
5772 This requires no further action. */
5773 }
5774 break;
5775 case bp_longjmp:
5776 case bp_longjmp_call_dummy:
5777 case bp_exception:
5778 if (bs->stop)
5779 {
5780 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5781 retval.is_longjmp = bptype != bp_exception;
5782 }
5783 else
5784 this_action = BPSTAT_WHAT_SINGLE;
5785 break;
5786 case bp_longjmp_resume:
5787 case bp_exception_resume:
5788 if (bs->stop)
5789 {
5790 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5791 retval.is_longjmp = bptype == bp_longjmp_resume;
5792 }
5793 else
5794 this_action = BPSTAT_WHAT_SINGLE;
5795 break;
5796 case bp_step_resume:
5797 if (bs->stop)
5798 this_action = BPSTAT_WHAT_STEP_RESUME;
5799 else
5800 {
5801 /* It is for the wrong frame. */
5802 this_action = BPSTAT_WHAT_SINGLE;
5803 }
5804 break;
5805 case bp_hp_step_resume:
5806 if (bs->stop)
5807 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5808 else
5809 {
5810 /* It is for the wrong frame. */
5811 this_action = BPSTAT_WHAT_SINGLE;
5812 }
5813 break;
5814 case bp_watchpoint_scope:
5815 case bp_thread_event:
5816 case bp_overlay_event:
5817 case bp_longjmp_master:
5818 case bp_std_terminate_master:
5819 case bp_exception_master:
5820 this_action = BPSTAT_WHAT_SINGLE;
5821 break;
5822 case bp_catchpoint:
5823 if (bs->stop)
5824 {
5825 if (bs->print)
5826 this_action = BPSTAT_WHAT_STOP_NOISY;
5827 else
5828 this_action = BPSTAT_WHAT_STOP_SILENT;
5829 }
5830 else
5831 {
5832 /* There was a catchpoint, but we're not stopping.
5833 This requires no further action. */
5834 }
5835 break;
5836 case bp_jit_event:
5837 jit_event = 1;
5838 this_action = BPSTAT_WHAT_SINGLE;
5839 break;
5840 case bp_call_dummy:
5841 /* Make sure the action is stop (silent or noisy),
5842 so infrun.c pops the dummy frame. */
5843 retval.call_dummy = STOP_STACK_DUMMY;
5844 this_action = BPSTAT_WHAT_STOP_SILENT;
5845 break;
5846 case bp_std_terminate:
5847 /* Make sure the action is stop (silent or noisy),
5848 so infrun.c pops the dummy frame. */
5849 retval.call_dummy = STOP_STD_TERMINATE;
5850 this_action = BPSTAT_WHAT_STOP_SILENT;
5851 break;
5852 case bp_tracepoint:
5853 case bp_fast_tracepoint:
5854 case bp_static_tracepoint:
5855 /* Tracepoint hits should not be reported back to GDB, and
5856 if one got through somehow, it should have been filtered
5857 out already. */
5858 internal_error (__FILE__, __LINE__,
5859 _("bpstat_what: tracepoint encountered"));
5860 break;
5861 case bp_gnu_ifunc_resolver:
5862 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5863 this_action = BPSTAT_WHAT_SINGLE;
5864 break;
5865 case bp_gnu_ifunc_resolver_return:
5866 /* The breakpoint will be removed, execution will restart from the
5867 PC of the former breakpoint. */
5868 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5869 break;
5870
5871 case bp_dprintf:
5872 if (bs->stop)
5873 this_action = BPSTAT_WHAT_STOP_SILENT;
5874 else
5875 this_action = BPSTAT_WHAT_SINGLE;
5876 break;
5877
5878 default:
5879 internal_error (__FILE__, __LINE__,
5880 _("bpstat_what: unhandled bptype %d"), (int) bptype);
5881 }
5882
5883 retval.main_action = max (retval.main_action, this_action);
5884 }
5885
5886 /* These operations may affect the bs->breakpoint_at state so they are
5887 delayed after MAIN_ACTION is decided above. */
5888
5889 if (jit_event)
5890 {
5891 if (debug_infrun)
5892 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_jit_event\n");
5893
5894 handle_jit_event ();
5895 }
5896
5897 for (bs = bs_head; bs != NULL; bs = bs->next)
5898 {
5899 struct breakpoint *b = bs->breakpoint_at;
5900
5901 if (b == NULL)
5902 continue;
5903 switch (b->type)
5904 {
5905 case bp_gnu_ifunc_resolver:
5906 gnu_ifunc_resolver_stop (b);
5907 break;
5908 case bp_gnu_ifunc_resolver_return:
5909 gnu_ifunc_resolver_return_stop (b);
5910 break;
5911 }
5912 }
5913
5914 return retval;
5915 }
5916
5917 /* Nonzero if we should step constantly (e.g. watchpoints on machines
5918 without hardware support). This isn't related to a specific bpstat,
5919 just to things like whether watchpoints are set. */
5920
5921 int
5922 bpstat_should_step (void)
5923 {
5924 struct breakpoint *b;
5925
5926 ALL_BREAKPOINTS (b)
5927 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
5928 return 1;
5929 return 0;
5930 }
5931
5932 int
5933 bpstat_causes_stop (bpstat bs)
5934 {
5935 for (; bs != NULL; bs = bs->next)
5936 if (bs->stop)
5937 return 1;
5938
5939 return 0;
5940 }
5941
5942 \f
5943
5944 /* Compute a string of spaces suitable to indent the next line
5945 so it starts at the position corresponding to the table column
5946 named COL_NAME in the currently active table of UIOUT. */
5947
5948 static char *
5949 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5950 {
5951 static char wrap_indent[80];
5952 int i, total_width, width, align;
5953 char *text;
5954
5955 total_width = 0;
5956 for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
5957 {
5958 if (strcmp (text, col_name) == 0)
5959 {
5960 gdb_assert (total_width < sizeof wrap_indent);
5961 memset (wrap_indent, ' ', total_width);
5962 wrap_indent[total_width] = 0;
5963
5964 return wrap_indent;
5965 }
5966
5967 total_width += width + 1;
5968 }
5969
5970 return NULL;
5971 }
5972
5973 /* Determine if the locations of this breakpoint will have their conditions
5974 evaluated by the target, host or a mix of both. Returns the following:
5975
5976 "host": Host evals condition.
5977 "host or target": Host or Target evals condition.
5978 "target": Target evals condition.
5979 */
5980
5981 static const char *
5982 bp_condition_evaluator (struct breakpoint *b)
5983 {
5984 struct bp_location *bl;
5985 char host_evals = 0;
5986 char target_evals = 0;
5987
5988 if (!b)
5989 return NULL;
5990
5991 if (!is_breakpoint (b))
5992 return NULL;
5993
5994 if (gdb_evaluates_breakpoint_condition_p ()
5995 || !target_supports_evaluation_of_breakpoint_conditions ())
5996 return condition_evaluation_host;
5997
5998 for (bl = b->loc; bl; bl = bl->next)
5999 {
6000 if (bl->cond_bytecode)
6001 target_evals++;
6002 else
6003 host_evals++;
6004 }
6005
6006 if (host_evals && target_evals)
6007 return condition_evaluation_both;
6008 else if (target_evals)
6009 return condition_evaluation_target;
6010 else
6011 return condition_evaluation_host;
6012 }
6013
6014 /* Determine the breakpoint location's condition evaluator. This is
6015 similar to bp_condition_evaluator, but for locations. */
6016
6017 static const char *
6018 bp_location_condition_evaluator (struct bp_location *bl)
6019 {
6020 if (bl && !is_breakpoint (bl->owner))
6021 return NULL;
6022
6023 if (gdb_evaluates_breakpoint_condition_p ()
6024 || !target_supports_evaluation_of_breakpoint_conditions ())
6025 return condition_evaluation_host;
6026
6027 if (bl && bl->cond_bytecode)
6028 return condition_evaluation_target;
6029 else
6030 return condition_evaluation_host;
6031 }
6032
6033 /* Print the LOC location out of the list of B->LOC locations. */
6034
6035 static void
6036 print_breakpoint_location (struct breakpoint *b,
6037 struct bp_location *loc)
6038 {
6039 struct ui_out *uiout = current_uiout;
6040 struct cleanup *old_chain = save_current_program_space ();
6041
6042 if (loc != NULL && loc->shlib_disabled)
6043 loc = NULL;
6044
6045 if (loc != NULL)
6046 set_current_program_space (loc->pspace);
6047
6048 if (b->display_canonical)
6049 ui_out_field_string (uiout, "what",
6050 event_location_to_string (b->location));
6051 else if (loc && loc->symtab)
6052 {
6053 struct symbol *sym
6054 = find_pc_sect_function (loc->address, loc->section);
6055 if (sym)
6056 {
6057 ui_out_text (uiout, "in ");
6058 ui_out_field_string (uiout, "func",
6059 SYMBOL_PRINT_NAME (sym));
6060 ui_out_text (uiout, " ");
6061 ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
6062 ui_out_text (uiout, "at ");
6063 }
6064 ui_out_field_string (uiout, "file",
6065 symtab_to_filename_for_display (loc->symtab));
6066 ui_out_text (uiout, ":");
6067
6068 if (ui_out_is_mi_like_p (uiout))
6069 ui_out_field_string (uiout, "fullname",
6070 symtab_to_fullname (loc->symtab));
6071
6072 ui_out_field_int (uiout, "line", loc->line_number);
6073 }
6074 else if (loc)
6075 {
6076 struct ui_file *stb = mem_fileopen ();
6077 struct cleanup *stb_chain = make_cleanup_ui_file_delete (stb);
6078
6079 print_address_symbolic (loc->gdbarch, loc->address, stb,
6080 demangle, "");
6081 ui_out_field_stream (uiout, "at", stb);
6082
6083 do_cleanups (stb_chain);
6084 }
6085 else
6086 {
6087 ui_out_field_string (uiout, "pending",
6088 event_location_to_string (b->location));
6089 /* If extra_string is available, it could be holding a condition
6090 or dprintf arguments. In either case, make sure it is printed,
6091 too, but only for non-MI streams. */
6092 if (!ui_out_is_mi_like_p (uiout) && b->extra_string != NULL)
6093 {
6094 if (b->type == bp_dprintf)
6095 ui_out_text (uiout, ",");
6096 else
6097 ui_out_text (uiout, " ");
6098 ui_out_text (uiout, b->extra_string);
6099 }
6100 }
6101
6102 if (loc && is_breakpoint (b)
6103 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6104 && bp_condition_evaluator (b) == condition_evaluation_both)
6105 {
6106 ui_out_text (uiout, " (");
6107 ui_out_field_string (uiout, "evaluated-by",
6108 bp_location_condition_evaluator (loc));
6109 ui_out_text (uiout, ")");
6110 }
6111
6112 do_cleanups (old_chain);
6113 }
6114
6115 static const char *
6116 bptype_string (enum bptype type)
6117 {
6118 struct ep_type_description
6119 {
6120 enum bptype type;
6121 char *description;
6122 };
6123 static struct ep_type_description bptypes[] =
6124 {
6125 {bp_none, "?deleted?"},
6126 {bp_breakpoint, "breakpoint"},
6127 {bp_hardware_breakpoint, "hw breakpoint"},
6128 {bp_single_step, "sw single-step"},
6129 {bp_until, "until"},
6130 {bp_finish, "finish"},
6131 {bp_watchpoint, "watchpoint"},
6132 {bp_hardware_watchpoint, "hw watchpoint"},
6133 {bp_read_watchpoint, "read watchpoint"},
6134 {bp_access_watchpoint, "acc watchpoint"},
6135 {bp_longjmp, "longjmp"},
6136 {bp_longjmp_resume, "longjmp resume"},
6137 {bp_longjmp_call_dummy, "longjmp for call dummy"},
6138 {bp_exception, "exception"},
6139 {bp_exception_resume, "exception resume"},
6140 {bp_step_resume, "step resume"},
6141 {bp_hp_step_resume, "high-priority step resume"},
6142 {bp_watchpoint_scope, "watchpoint scope"},
6143 {bp_call_dummy, "call dummy"},
6144 {bp_std_terminate, "std::terminate"},
6145 {bp_shlib_event, "shlib events"},
6146 {bp_thread_event, "thread events"},
6147 {bp_overlay_event, "overlay events"},
6148 {bp_longjmp_master, "longjmp master"},
6149 {bp_std_terminate_master, "std::terminate master"},
6150 {bp_exception_master, "exception master"},
6151 {bp_catchpoint, "catchpoint"},
6152 {bp_tracepoint, "tracepoint"},
6153 {bp_fast_tracepoint, "fast tracepoint"},
6154 {bp_static_tracepoint, "static tracepoint"},
6155 {bp_dprintf, "dprintf"},
6156 {bp_jit_event, "jit events"},
6157 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6158 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
6159 };
6160
6161 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6162 || ((int) type != bptypes[(int) type].type))
6163 internal_error (__FILE__, __LINE__,
6164 _("bptypes table does not describe type #%d."),
6165 (int) type);
6166
6167 return bptypes[(int) type].description;
6168 }
6169
6170 /* For MI, output a field named 'thread-groups' with a list as the value.
6171 For CLI, prefix the list with the string 'inf'. */
6172
6173 static void
6174 output_thread_groups (struct ui_out *uiout,
6175 const char *field_name,
6176 VEC(int) *inf_num,
6177 int mi_only)
6178 {
6179 struct cleanup *back_to;
6180 int is_mi = ui_out_is_mi_like_p (uiout);
6181 int inf;
6182 int i;
6183
6184 /* For backward compatibility, don't display inferiors in CLI unless
6185 there are several. Always display them for MI. */
6186 if (!is_mi && mi_only)
6187 return;
6188
6189 back_to = make_cleanup_ui_out_list_begin_end (uiout, field_name);
6190
6191 for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
6192 {
6193 if (is_mi)
6194 {
6195 char mi_group[10];
6196
6197 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
6198 ui_out_field_string (uiout, NULL, mi_group);
6199 }
6200 else
6201 {
6202 if (i == 0)
6203 ui_out_text (uiout, " inf ");
6204 else
6205 ui_out_text (uiout, ", ");
6206
6207 ui_out_text (uiout, plongest (inf));
6208 }
6209 }
6210
6211 do_cleanups (back_to);
6212 }
6213
6214 /* Print B to gdb_stdout. */
6215
6216 static void
6217 print_one_breakpoint_location (struct breakpoint *b,
6218 struct bp_location *loc,
6219 int loc_number,
6220 struct bp_location **last_loc,
6221 int allflag)
6222 {
6223 struct command_line *l;
6224 static char bpenables[] = "nynny";
6225
6226 struct ui_out *uiout = current_uiout;
6227 int header_of_multiple = 0;
6228 int part_of_multiple = (loc != NULL);
6229 struct value_print_options opts;
6230
6231 get_user_print_options (&opts);
6232
6233 gdb_assert (!loc || loc_number != 0);
6234 /* See comment in print_one_breakpoint concerning treatment of
6235 breakpoints with single disabled location. */
6236 if (loc == NULL
6237 && (b->loc != NULL
6238 && (b->loc->next != NULL || !b->loc->enabled)))
6239 header_of_multiple = 1;
6240 if (loc == NULL)
6241 loc = b->loc;
6242
6243 annotate_record ();
6244
6245 /* 1 */
6246 annotate_field (0);
6247 if (part_of_multiple)
6248 {
6249 char *formatted;
6250 formatted = xstrprintf ("%d.%d", b->number, loc_number);
6251 ui_out_field_string (uiout, "number", formatted);
6252 xfree (formatted);
6253 }
6254 else
6255 {
6256 ui_out_field_int (uiout, "number", b->number);
6257 }
6258
6259 /* 2 */
6260 annotate_field (1);
6261 if (part_of_multiple)
6262 ui_out_field_skip (uiout, "type");
6263 else
6264 ui_out_field_string (uiout, "type", bptype_string (b->type));
6265
6266 /* 3 */
6267 annotate_field (2);
6268 if (part_of_multiple)
6269 ui_out_field_skip (uiout, "disp");
6270 else
6271 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
6272
6273
6274 /* 4 */
6275 annotate_field (3);
6276 if (part_of_multiple)
6277 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
6278 else
6279 ui_out_field_fmt (uiout, "enabled", "%c",
6280 bpenables[(int) b->enable_state]);
6281 ui_out_spaces (uiout, 2);
6282
6283
6284 /* 5 and 6 */
6285 if (b->ops != NULL && b->ops->print_one != NULL)
6286 {
6287 /* Although the print_one can possibly print all locations,
6288 calling it here is not likely to get any nice result. So,
6289 make sure there's just one location. */
6290 gdb_assert (b->loc == NULL || b->loc->next == NULL);
6291 b->ops->print_one (b, last_loc);
6292 }
6293 else
6294 switch (b->type)
6295 {
6296 case bp_none:
6297 internal_error (__FILE__, __LINE__,
6298 _("print_one_breakpoint: bp_none encountered\n"));
6299 break;
6300
6301 case bp_watchpoint:
6302 case bp_hardware_watchpoint:
6303 case bp_read_watchpoint:
6304 case bp_access_watchpoint:
6305 {
6306 struct watchpoint *w = (struct watchpoint *) b;
6307
6308 /* Field 4, the address, is omitted (which makes the columns
6309 not line up too nicely with the headers, but the effect
6310 is relatively readable). */
6311 if (opts.addressprint)
6312 ui_out_field_skip (uiout, "addr");
6313 annotate_field (5);
6314 ui_out_field_string (uiout, "what", w->exp_string);
6315 }
6316 break;
6317
6318 case bp_breakpoint:
6319 case bp_hardware_breakpoint:
6320 case bp_single_step:
6321 case bp_until:
6322 case bp_finish:
6323 case bp_longjmp:
6324 case bp_longjmp_resume:
6325 case bp_longjmp_call_dummy:
6326 case bp_exception:
6327 case bp_exception_resume:
6328 case bp_step_resume:
6329 case bp_hp_step_resume:
6330 case bp_watchpoint_scope:
6331 case bp_call_dummy:
6332 case bp_std_terminate:
6333 case bp_shlib_event:
6334 case bp_thread_event:
6335 case bp_overlay_event:
6336 case bp_longjmp_master:
6337 case bp_std_terminate_master:
6338 case bp_exception_master:
6339 case bp_tracepoint:
6340 case bp_fast_tracepoint:
6341 case bp_static_tracepoint:
6342 case bp_dprintf:
6343 case bp_jit_event:
6344 case bp_gnu_ifunc_resolver:
6345 case bp_gnu_ifunc_resolver_return:
6346 if (opts.addressprint)
6347 {
6348 annotate_field (4);
6349 if (header_of_multiple)
6350 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
6351 else if (b->loc == NULL || loc->shlib_disabled)
6352 ui_out_field_string (uiout, "addr", "<PENDING>");
6353 else
6354 ui_out_field_core_addr (uiout, "addr",
6355 loc->gdbarch, loc->address);
6356 }
6357 annotate_field (5);
6358 if (!header_of_multiple)
6359 print_breakpoint_location (b, loc);
6360 if (b->loc)
6361 *last_loc = b->loc;
6362 break;
6363 }
6364
6365
6366 if (loc != NULL && !header_of_multiple)
6367 {
6368 struct inferior *inf;
6369 VEC(int) *inf_num = NULL;
6370 int mi_only = 1;
6371
6372 ALL_INFERIORS (inf)
6373 {
6374 if (inf->pspace == loc->pspace)
6375 VEC_safe_push (int, inf_num, inf->num);
6376 }
6377
6378 /* For backward compatibility, don't display inferiors in CLI unless
6379 there are several. Always display for MI. */
6380 if (allflag
6381 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6382 && (number_of_program_spaces () > 1
6383 || number_of_inferiors () > 1)
6384 /* LOC is for existing B, it cannot be in
6385 moribund_locations and thus having NULL OWNER. */
6386 && loc->owner->type != bp_catchpoint))
6387 mi_only = 0;
6388 output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6389 VEC_free (int, inf_num);
6390 }
6391
6392 if (!part_of_multiple)
6393 {
6394 if (b->thread != -1)
6395 {
6396 /* FIXME: This seems to be redundant and lost here; see the
6397 "stop only in" line a little further down. */
6398 ui_out_text (uiout, " thread ");
6399 ui_out_field_int (uiout, "thread", b->thread);
6400 }
6401 else if (b->task != 0)
6402 {
6403 ui_out_text (uiout, " task ");
6404 ui_out_field_int (uiout, "task", b->task);
6405 }
6406 }
6407
6408 ui_out_text (uiout, "\n");
6409
6410 if (!part_of_multiple)
6411 b->ops->print_one_detail (b, uiout);
6412
6413 if (part_of_multiple && frame_id_p (b->frame_id))
6414 {
6415 annotate_field (6);
6416 ui_out_text (uiout, "\tstop only in stack frame at ");
6417 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
6418 the frame ID. */
6419 ui_out_field_core_addr (uiout, "frame",
6420 b->gdbarch, b->frame_id.stack_addr);
6421 ui_out_text (uiout, "\n");
6422 }
6423
6424 if (!part_of_multiple && b->cond_string)
6425 {
6426 annotate_field (7);
6427 if (is_tracepoint (b))
6428 ui_out_text (uiout, "\ttrace only if ");
6429 else
6430 ui_out_text (uiout, "\tstop only if ");
6431 ui_out_field_string (uiout, "cond", b->cond_string);
6432
6433 /* Print whether the target is doing the breakpoint's condition
6434 evaluation. If GDB is doing the evaluation, don't print anything. */
6435 if (is_breakpoint (b)
6436 && breakpoint_condition_evaluation_mode ()
6437 == condition_evaluation_target)
6438 {
6439 ui_out_text (uiout, " (");
6440 ui_out_field_string (uiout, "evaluated-by",
6441 bp_condition_evaluator (b));
6442 ui_out_text (uiout, " evals)");
6443 }
6444 ui_out_text (uiout, "\n");
6445 }
6446
6447 if (!part_of_multiple && b->thread != -1)
6448 {
6449 /* FIXME should make an annotation for this. */
6450 ui_out_text (uiout, "\tstop only in thread ");
6451 ui_out_field_int (uiout, "thread", b->thread);
6452 ui_out_text (uiout, "\n");
6453 }
6454
6455 if (!part_of_multiple)
6456 {
6457 if (b->hit_count)
6458 {
6459 /* FIXME should make an annotation for this. */
6460 if (is_catchpoint (b))
6461 ui_out_text (uiout, "\tcatchpoint");
6462 else if (is_tracepoint (b))
6463 ui_out_text (uiout, "\ttracepoint");
6464 else
6465 ui_out_text (uiout, "\tbreakpoint");
6466 ui_out_text (uiout, " already hit ");
6467 ui_out_field_int (uiout, "times", b->hit_count);
6468 if (b->hit_count == 1)
6469 ui_out_text (uiout, " time\n");
6470 else
6471 ui_out_text (uiout, " times\n");
6472 }
6473 else
6474 {
6475 /* Output the count also if it is zero, but only if this is mi. */
6476 if (ui_out_is_mi_like_p (uiout))
6477 ui_out_field_int (uiout, "times", b->hit_count);
6478 }
6479 }
6480
6481 if (!part_of_multiple && b->ignore_count)
6482 {
6483 annotate_field (8);
6484 ui_out_text (uiout, "\tignore next ");
6485 ui_out_field_int (uiout, "ignore", b->ignore_count);
6486 ui_out_text (uiout, " hits\n");
6487 }
6488
6489 /* Note that an enable count of 1 corresponds to "enable once"
6490 behavior, which is reported by the combination of enablement and
6491 disposition, so we don't need to mention it here. */
6492 if (!part_of_multiple && b->enable_count > 1)
6493 {
6494 annotate_field (8);
6495 ui_out_text (uiout, "\tdisable after ");
6496 /* Tweak the wording to clarify that ignore and enable counts
6497 are distinct, and have additive effect. */
6498 if (b->ignore_count)
6499 ui_out_text (uiout, "additional ");
6500 else
6501 ui_out_text (uiout, "next ");
6502 ui_out_field_int (uiout, "enable", b->enable_count);
6503 ui_out_text (uiout, " hits\n");
6504 }
6505
6506 if (!part_of_multiple && is_tracepoint (b))
6507 {
6508 struct tracepoint *tp = (struct tracepoint *) b;
6509
6510 if (tp->traceframe_usage)
6511 {
6512 ui_out_text (uiout, "\ttrace buffer usage ");
6513 ui_out_field_int (uiout, "traceframe-usage", tp->traceframe_usage);
6514 ui_out_text (uiout, " bytes\n");
6515 }
6516 }
6517
6518 l = b->commands ? b->commands->commands : NULL;
6519 if (!part_of_multiple && l)
6520 {
6521 struct cleanup *script_chain;
6522
6523 annotate_field (9);
6524 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
6525 print_command_lines (uiout, l, 4);
6526 do_cleanups (script_chain);
6527 }
6528
6529 if (is_tracepoint (b))
6530 {
6531 struct tracepoint *t = (struct tracepoint *) b;
6532
6533 if (!part_of_multiple && t->pass_count)
6534 {
6535 annotate_field (10);
6536 ui_out_text (uiout, "\tpass count ");
6537 ui_out_field_int (uiout, "pass", t->pass_count);
6538 ui_out_text (uiout, " \n");
6539 }
6540
6541 /* Don't display it when tracepoint or tracepoint location is
6542 pending. */
6543 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6544 {
6545 annotate_field (11);
6546
6547 if (ui_out_is_mi_like_p (uiout))
6548 ui_out_field_string (uiout, "installed",
6549 loc->inserted ? "y" : "n");
6550 else
6551 {
6552 if (loc->inserted)
6553 ui_out_text (uiout, "\t");
6554 else
6555 ui_out_text (uiout, "\tnot ");
6556 ui_out_text (uiout, "installed on target\n");
6557 }
6558 }
6559 }
6560
6561 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
6562 {
6563 if (is_watchpoint (b))
6564 {
6565 struct watchpoint *w = (struct watchpoint *) b;
6566
6567 ui_out_field_string (uiout, "original-location", w->exp_string);
6568 }
6569 else if (b->location != NULL
6570 && event_location_to_string (b->location) != NULL)
6571 ui_out_field_string (uiout, "original-location",
6572 event_location_to_string (b->location));
6573 }
6574 }
6575
6576 static void
6577 print_one_breakpoint (struct breakpoint *b,
6578 struct bp_location **last_loc,
6579 int allflag)
6580 {
6581 struct cleanup *bkpt_chain;
6582 struct ui_out *uiout = current_uiout;
6583
6584 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
6585
6586 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
6587 do_cleanups (bkpt_chain);
6588
6589 /* If this breakpoint has custom print function,
6590 it's already printed. Otherwise, print individual
6591 locations, if any. */
6592 if (b->ops == NULL || b->ops->print_one == NULL)
6593 {
6594 /* If breakpoint has a single location that is disabled, we
6595 print it as if it had several locations, since otherwise it's
6596 hard to represent "breakpoint enabled, location disabled"
6597 situation.
6598
6599 Note that while hardware watchpoints have several locations
6600 internally, that's not a property exposed to user. */
6601 if (b->loc
6602 && !is_hardware_watchpoint (b)
6603 && (b->loc->next || !b->loc->enabled))
6604 {
6605 struct bp_location *loc;
6606 int n = 1;
6607
6608 for (loc = b->loc; loc; loc = loc->next, ++n)
6609 {
6610 struct cleanup *inner2 =
6611 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
6612 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
6613 do_cleanups (inner2);
6614 }
6615 }
6616 }
6617 }
6618
6619 static int
6620 breakpoint_address_bits (struct breakpoint *b)
6621 {
6622 int print_address_bits = 0;
6623 struct bp_location *loc;
6624
6625 for (loc = b->loc; loc; loc = loc->next)
6626 {
6627 int addr_bit;
6628
6629 /* Software watchpoints that aren't watching memory don't have
6630 an address to print. */
6631 if (b->type == bp_watchpoint && loc->watchpoint_type == -1)
6632 continue;
6633
6634 addr_bit = gdbarch_addr_bit (loc->gdbarch);
6635 if (addr_bit > print_address_bits)
6636 print_address_bits = addr_bit;
6637 }
6638
6639 return print_address_bits;
6640 }
6641
6642 struct captured_breakpoint_query_args
6643 {
6644 int bnum;
6645 };
6646
6647 static int
6648 do_captured_breakpoint_query (struct ui_out *uiout, void *data)
6649 {
6650 struct captured_breakpoint_query_args *args = data;
6651 struct breakpoint *b;
6652 struct bp_location *dummy_loc = NULL;
6653
6654 ALL_BREAKPOINTS (b)
6655 {
6656 if (args->bnum == b->number)
6657 {
6658 print_one_breakpoint (b, &dummy_loc, 0);
6659 return GDB_RC_OK;
6660 }
6661 }
6662 return GDB_RC_NONE;
6663 }
6664
6665 enum gdb_rc
6666 gdb_breakpoint_query (struct ui_out *uiout, int bnum,
6667 char **error_message)
6668 {
6669 struct captured_breakpoint_query_args args;
6670
6671 args.bnum = bnum;
6672 /* For the moment we don't trust print_one_breakpoint() to not throw
6673 an error. */
6674 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
6675 error_message, RETURN_MASK_ALL) < 0)
6676 return GDB_RC_FAIL;
6677 else
6678 return GDB_RC_OK;
6679 }
6680
6681 /* Return true if this breakpoint was set by the user, false if it is
6682 internal or momentary. */
6683
6684 int
6685 user_breakpoint_p (struct breakpoint *b)
6686 {
6687 return b->number > 0;
6688 }
6689
6690 /* Print information on user settable breakpoint (watchpoint, etc)
6691 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6692 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6693 FILTER is non-NULL, call it on each breakpoint and only include the
6694 ones for which it returns non-zero. Return the total number of
6695 breakpoints listed. */
6696
6697 static int
6698 breakpoint_1 (char *args, int allflag,
6699 int (*filter) (const struct breakpoint *))
6700 {
6701 struct breakpoint *b;
6702 struct bp_location *last_loc = NULL;
6703 int nr_printable_breakpoints;
6704 struct cleanup *bkpttbl_chain;
6705 struct value_print_options opts;
6706 int print_address_bits = 0;
6707 int print_type_col_width = 14;
6708 struct ui_out *uiout = current_uiout;
6709
6710 get_user_print_options (&opts);
6711
6712 /* Compute the number of rows in the table, as well as the size
6713 required for address fields. */
6714 nr_printable_breakpoints = 0;
6715 ALL_BREAKPOINTS (b)
6716 {
6717 /* If we have a filter, only list the breakpoints it accepts. */
6718 if (filter && !filter (b))
6719 continue;
6720
6721 /* If we have an "args" string, it is a list of breakpoints to
6722 accept. Skip the others. */
6723 if (args != NULL && *args != '\0')
6724 {
6725 if (allflag && parse_and_eval_long (args) != b->number)
6726 continue;
6727 if (!allflag && !number_is_in_list (args, b->number))
6728 continue;
6729 }
6730
6731 if (allflag || user_breakpoint_p (b))
6732 {
6733 int addr_bit, type_len;
6734
6735 addr_bit = breakpoint_address_bits (b);
6736 if (addr_bit > print_address_bits)
6737 print_address_bits = addr_bit;
6738
6739 type_len = strlen (bptype_string (b->type));
6740 if (type_len > print_type_col_width)
6741 print_type_col_width = type_len;
6742
6743 nr_printable_breakpoints++;
6744 }
6745 }
6746
6747 if (opts.addressprint)
6748 bkpttbl_chain
6749 = make_cleanup_ui_out_table_begin_end (uiout, 6,
6750 nr_printable_breakpoints,
6751 "BreakpointTable");
6752 else
6753 bkpttbl_chain
6754 = make_cleanup_ui_out_table_begin_end (uiout, 5,
6755 nr_printable_breakpoints,
6756 "BreakpointTable");
6757
6758 if (nr_printable_breakpoints > 0)
6759 annotate_breakpoints_headers ();
6760 if (nr_printable_breakpoints > 0)
6761 annotate_field (0);
6762 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
6763 if (nr_printable_breakpoints > 0)
6764 annotate_field (1);
6765 ui_out_table_header (uiout, print_type_col_width, ui_left,
6766 "type", "Type"); /* 2 */
6767 if (nr_printable_breakpoints > 0)
6768 annotate_field (2);
6769 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
6770 if (nr_printable_breakpoints > 0)
6771 annotate_field (3);
6772 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
6773 if (opts.addressprint)
6774 {
6775 if (nr_printable_breakpoints > 0)
6776 annotate_field (4);
6777 if (print_address_bits <= 32)
6778 ui_out_table_header (uiout, 10, ui_left,
6779 "addr", "Address"); /* 5 */
6780 else
6781 ui_out_table_header (uiout, 18, ui_left,
6782 "addr", "Address"); /* 5 */
6783 }
6784 if (nr_printable_breakpoints > 0)
6785 annotate_field (5);
6786 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
6787 ui_out_table_body (uiout);
6788 if (nr_printable_breakpoints > 0)
6789 annotate_breakpoints_table ();
6790
6791 ALL_BREAKPOINTS (b)
6792 {
6793 QUIT;
6794 /* If we have a filter, only list the breakpoints it accepts. */
6795 if (filter && !filter (b))
6796 continue;
6797
6798 /* If we have an "args" string, it is a list of breakpoints to
6799 accept. Skip the others. */
6800
6801 if (args != NULL && *args != '\0')
6802 {
6803 if (allflag) /* maintenance info breakpoint */
6804 {
6805 if (parse_and_eval_long (args) != b->number)
6806 continue;
6807 }
6808 else /* all others */
6809 {
6810 if (!number_is_in_list (args, b->number))
6811 continue;
6812 }
6813 }
6814 /* We only print out user settable breakpoints unless the
6815 allflag is set. */
6816 if (allflag || user_breakpoint_p (b))
6817 print_one_breakpoint (b, &last_loc, allflag);
6818 }
6819
6820 do_cleanups (bkpttbl_chain);
6821
6822 if (nr_printable_breakpoints == 0)
6823 {
6824 /* If there's a filter, let the caller decide how to report
6825 empty list. */
6826 if (!filter)
6827 {
6828 if (args == NULL || *args == '\0')
6829 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
6830 else
6831 ui_out_message (uiout, 0,
6832 "No breakpoint or watchpoint matching '%s'.\n",
6833 args);
6834 }
6835 }
6836 else
6837 {
6838 if (last_loc && !server_command)
6839 set_next_address (last_loc->gdbarch, last_loc->address);
6840 }
6841
6842 /* FIXME? Should this be moved up so that it is only called when
6843 there have been breakpoints? */
6844 annotate_breakpoints_table_end ();
6845
6846 return nr_printable_breakpoints;
6847 }
6848
6849 /* Display the value of default-collect in a way that is generally
6850 compatible with the breakpoint list. */
6851
6852 static void
6853 default_collect_info (void)
6854 {
6855 struct ui_out *uiout = current_uiout;
6856
6857 /* If it has no value (which is frequently the case), say nothing; a
6858 message like "No default-collect." gets in user's face when it's
6859 not wanted. */
6860 if (!*default_collect)
6861 return;
6862
6863 /* The following phrase lines up nicely with per-tracepoint collect
6864 actions. */
6865 ui_out_text (uiout, "default collect ");
6866 ui_out_field_string (uiout, "default-collect", default_collect);
6867 ui_out_text (uiout, " \n");
6868 }
6869
6870 static void
6871 breakpoints_info (char *args, int from_tty)
6872 {
6873 breakpoint_1 (args, 0, NULL);
6874
6875 default_collect_info ();
6876 }
6877
6878 static void
6879 watchpoints_info (char *args, int from_tty)
6880 {
6881 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
6882 struct ui_out *uiout = current_uiout;
6883
6884 if (num_printed == 0)
6885 {
6886 if (args == NULL || *args == '\0')
6887 ui_out_message (uiout, 0, "No watchpoints.\n");
6888 else
6889 ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
6890 }
6891 }
6892
6893 static void
6894 maintenance_info_breakpoints (char *args, int from_tty)
6895 {
6896 breakpoint_1 (args, 1, NULL);
6897
6898 default_collect_info ();
6899 }
6900
6901 static int
6902 breakpoint_has_pc (struct breakpoint *b,
6903 struct program_space *pspace,
6904 CORE_ADDR pc, struct obj_section *section)
6905 {
6906 struct bp_location *bl = b->loc;
6907
6908 for (; bl; bl = bl->next)
6909 {
6910 if (bl->pspace == pspace
6911 && bl->address == pc
6912 && (!overlay_debugging || bl->section == section))
6913 return 1;
6914 }
6915 return 0;
6916 }
6917
6918 /* Print a message describing any user-breakpoints set at PC. This
6919 concerns with logical breakpoints, so we match program spaces, not
6920 address spaces. */
6921
6922 static void
6923 describe_other_breakpoints (struct gdbarch *gdbarch,
6924 struct program_space *pspace, CORE_ADDR pc,
6925 struct obj_section *section, int thread)
6926 {
6927 int others = 0;
6928 struct breakpoint *b;
6929
6930 ALL_BREAKPOINTS (b)
6931 others += (user_breakpoint_p (b)
6932 && breakpoint_has_pc (b, pspace, pc, section));
6933 if (others > 0)
6934 {
6935 if (others == 1)
6936 printf_filtered (_("Note: breakpoint "));
6937 else /* if (others == ???) */
6938 printf_filtered (_("Note: breakpoints "));
6939 ALL_BREAKPOINTS (b)
6940 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
6941 {
6942 others--;
6943 printf_filtered ("%d", b->number);
6944 if (b->thread == -1 && thread != -1)
6945 printf_filtered (" (all threads)");
6946 else if (b->thread != -1)
6947 printf_filtered (" (thread %d)", b->thread);
6948 printf_filtered ("%s%s ",
6949 ((b->enable_state == bp_disabled
6950 || b->enable_state == bp_call_disabled)
6951 ? " (disabled)"
6952 : ""),
6953 (others > 1) ? ","
6954 : ((others == 1) ? " and" : ""));
6955 }
6956 printf_filtered (_("also set at pc "));
6957 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
6958 printf_filtered (".\n");
6959 }
6960 }
6961 \f
6962
6963 /* Return true iff it is meaningful to use the address member of
6964 BPT. For some breakpoint types, the address member is irrelevant
6965 and it makes no sense to attempt to compare it to other addresses
6966 (or use it for any other purpose either).
6967
6968 More specifically, each of the following breakpoint types will
6969 always have a zero valued address and we don't want to mark
6970 breakpoints of any of these types to be a duplicate of an actual
6971 breakpoint at address zero:
6972
6973 bp_watchpoint
6974 bp_catchpoint
6975
6976 */
6977
6978 static int
6979 breakpoint_address_is_meaningful (struct breakpoint *bpt)
6980 {
6981 enum bptype type = bpt->type;
6982
6983 return (type != bp_watchpoint && type != bp_catchpoint);
6984 }
6985
6986 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6987 true if LOC1 and LOC2 represent the same watchpoint location. */
6988
6989 static int
6990 watchpoint_locations_match (struct bp_location *loc1,
6991 struct bp_location *loc2)
6992 {
6993 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6994 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6995
6996 /* Both of them must exist. */
6997 gdb_assert (w1 != NULL);
6998 gdb_assert (w2 != NULL);
6999
7000 /* If the target can evaluate the condition expression in hardware,
7001 then we we need to insert both watchpoints even if they are at
7002 the same place. Otherwise the watchpoint will only trigger when
7003 the condition of whichever watchpoint was inserted evaluates to
7004 true, not giving a chance for GDB to check the condition of the
7005 other watchpoint. */
7006 if ((w1->cond_exp
7007 && target_can_accel_watchpoint_condition (loc1->address,
7008 loc1->length,
7009 loc1->watchpoint_type,
7010 w1->cond_exp))
7011 || (w2->cond_exp
7012 && target_can_accel_watchpoint_condition (loc2->address,
7013 loc2->length,
7014 loc2->watchpoint_type,
7015 w2->cond_exp)))
7016 return 0;
7017
7018 /* Note that this checks the owner's type, not the location's. In
7019 case the target does not support read watchpoints, but does
7020 support access watchpoints, we'll have bp_read_watchpoint
7021 watchpoints with hw_access locations. Those should be considered
7022 duplicates of hw_read locations. The hw_read locations will
7023 become hw_access locations later. */
7024 return (loc1->owner->type == loc2->owner->type
7025 && loc1->pspace->aspace == loc2->pspace->aspace
7026 && loc1->address == loc2->address
7027 && loc1->length == loc2->length);
7028 }
7029
7030 /* See breakpoint.h. */
7031
7032 int
7033 breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
7034 struct address_space *aspace2, CORE_ADDR addr2)
7035 {
7036 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
7037 || aspace1 == aspace2)
7038 && addr1 == addr2);
7039 }
7040
7041 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
7042 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
7043 matches ASPACE2. On targets that have global breakpoints, the address
7044 space doesn't really matter. */
7045
7046 static int
7047 breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
7048 int len1, struct address_space *aspace2,
7049 CORE_ADDR addr2)
7050 {
7051 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
7052 || aspace1 == aspace2)
7053 && addr2 >= addr1 && addr2 < addr1 + len1);
7054 }
7055
7056 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
7057 a ranged breakpoint. In most targets, a match happens only if ASPACE
7058 matches the breakpoint's address space. On targets that have global
7059 breakpoints, the address space doesn't really matter. */
7060
7061 static int
7062 breakpoint_location_address_match (struct bp_location *bl,
7063 struct address_space *aspace,
7064 CORE_ADDR addr)
7065 {
7066 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
7067 aspace, addr)
7068 || (bl->length
7069 && breakpoint_address_match_range (bl->pspace->aspace,
7070 bl->address, bl->length,
7071 aspace, addr)));
7072 }
7073
7074 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
7075 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
7076 true, otherwise returns false. */
7077
7078 static int
7079 tracepoint_locations_match (struct bp_location *loc1,
7080 struct bp_location *loc2)
7081 {
7082 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
7083 /* Since tracepoint locations are never duplicated with others', tracepoint
7084 locations at the same address of different tracepoints are regarded as
7085 different locations. */
7086 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7087 else
7088 return 0;
7089 }
7090
7091 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
7092 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
7093 represent the same location. */
7094
7095 static int
7096 breakpoint_locations_match (struct bp_location *loc1,
7097 struct bp_location *loc2)
7098 {
7099 int hw_point1, hw_point2;
7100
7101 /* Both of them must not be in moribund_locations. */
7102 gdb_assert (loc1->owner != NULL);
7103 gdb_assert (loc2->owner != NULL);
7104
7105 hw_point1 = is_hardware_watchpoint (loc1->owner);
7106 hw_point2 = is_hardware_watchpoint (loc2->owner);
7107
7108 if (hw_point1 != hw_point2)
7109 return 0;
7110 else if (hw_point1)
7111 return watchpoint_locations_match (loc1, loc2);
7112 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7113 return tracepoint_locations_match (loc1, loc2);
7114 else
7115 /* We compare bp_location.length in order to cover ranged breakpoints. */
7116 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7117 loc2->pspace->aspace, loc2->address)
7118 && loc1->length == loc2->length);
7119 }
7120
7121 static void
7122 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7123 int bnum, int have_bnum)
7124 {
7125 /* The longest string possibly returned by hex_string_custom
7126 is 50 chars. These must be at least that big for safety. */
7127 char astr1[64];
7128 char astr2[64];
7129
7130 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7131 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
7132 if (have_bnum)
7133 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
7134 bnum, astr1, astr2);
7135 else
7136 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
7137 }
7138
7139 /* Adjust a breakpoint's address to account for architectural
7140 constraints on breakpoint placement. Return the adjusted address.
7141 Note: Very few targets require this kind of adjustment. For most
7142 targets, this function is simply the identity function. */
7143
7144 static CORE_ADDR
7145 adjust_breakpoint_address (struct gdbarch *gdbarch,
7146 CORE_ADDR bpaddr, enum bptype bptype)
7147 {
7148 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
7149 {
7150 /* Very few targets need any kind of breakpoint adjustment. */
7151 return bpaddr;
7152 }
7153 else if (bptype == bp_watchpoint
7154 || bptype == bp_hardware_watchpoint
7155 || bptype == bp_read_watchpoint
7156 || bptype == bp_access_watchpoint
7157 || bptype == bp_catchpoint)
7158 {
7159 /* Watchpoints and the various bp_catch_* eventpoints should not
7160 have their addresses modified. */
7161 return bpaddr;
7162 }
7163 else if (bptype == bp_single_step)
7164 {
7165 /* Single-step breakpoints should not have their addresses
7166 modified. If there's any architectural constrain that
7167 applies to this address, then it should have already been
7168 taken into account when the breakpoint was created in the
7169 first place. If we didn't do this, stepping through e.g.,
7170 Thumb-2 IT blocks would break. */
7171 return bpaddr;
7172 }
7173 else
7174 {
7175 CORE_ADDR adjusted_bpaddr;
7176
7177 /* Some targets have architectural constraints on the placement
7178 of breakpoint instructions. Obtain the adjusted address. */
7179 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
7180
7181 /* An adjusted breakpoint address can significantly alter
7182 a user's expectations. Print a warning if an adjustment
7183 is required. */
7184 if (adjusted_bpaddr != bpaddr)
7185 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7186
7187 return adjusted_bpaddr;
7188 }
7189 }
7190
7191 void
7192 init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
7193 struct breakpoint *owner)
7194 {
7195 memset (loc, 0, sizeof (*loc));
7196
7197 gdb_assert (ops != NULL);
7198
7199 loc->ops = ops;
7200 loc->owner = owner;
7201 loc->cond = NULL;
7202 loc->cond_bytecode = NULL;
7203 loc->shlib_disabled = 0;
7204 loc->enabled = 1;
7205
7206 switch (owner->type)
7207 {
7208 case bp_breakpoint:
7209 case bp_single_step:
7210 case bp_until:
7211 case bp_finish:
7212 case bp_longjmp:
7213 case bp_longjmp_resume:
7214 case bp_longjmp_call_dummy:
7215 case bp_exception:
7216 case bp_exception_resume:
7217 case bp_step_resume:
7218 case bp_hp_step_resume:
7219 case bp_watchpoint_scope:
7220 case bp_call_dummy:
7221 case bp_std_terminate:
7222 case bp_shlib_event:
7223 case bp_thread_event:
7224 case bp_overlay_event:
7225 case bp_jit_event:
7226 case bp_longjmp_master:
7227 case bp_std_terminate_master:
7228 case bp_exception_master:
7229 case bp_gnu_ifunc_resolver:
7230 case bp_gnu_ifunc_resolver_return:
7231 case bp_dprintf:
7232 loc->loc_type = bp_loc_software_breakpoint;
7233 mark_breakpoint_location_modified (loc);
7234 break;
7235 case bp_hardware_breakpoint:
7236 loc->loc_type = bp_loc_hardware_breakpoint;
7237 mark_breakpoint_location_modified (loc);
7238 break;
7239 case bp_hardware_watchpoint:
7240 case bp_read_watchpoint:
7241 case bp_access_watchpoint:
7242 loc->loc_type = bp_loc_hardware_watchpoint;
7243 break;
7244 case bp_watchpoint:
7245 case bp_catchpoint:
7246 case bp_tracepoint:
7247 case bp_fast_tracepoint:
7248 case bp_static_tracepoint:
7249 loc->loc_type = bp_loc_other;
7250 break;
7251 default:
7252 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
7253 }
7254
7255 loc->refc = 1;
7256 }
7257
7258 /* Allocate a struct bp_location. */
7259
7260 static struct bp_location *
7261 allocate_bp_location (struct breakpoint *bpt)
7262 {
7263 return bpt->ops->allocate_location (bpt);
7264 }
7265
7266 static void
7267 free_bp_location (struct bp_location *loc)
7268 {
7269 loc->ops->dtor (loc);
7270 xfree (loc);
7271 }
7272
7273 /* Increment reference count. */
7274
7275 static void
7276 incref_bp_location (struct bp_location *bl)
7277 {
7278 ++bl->refc;
7279 }
7280
7281 /* Decrement reference count. If the reference count reaches 0,
7282 destroy the bp_location. Sets *BLP to NULL. */
7283
7284 static void
7285 decref_bp_location (struct bp_location **blp)
7286 {
7287 gdb_assert ((*blp)->refc > 0);
7288
7289 if (--(*blp)->refc == 0)
7290 free_bp_location (*blp);
7291 *blp = NULL;
7292 }
7293
7294 /* Add breakpoint B at the end of the global breakpoint chain. */
7295
7296 static void
7297 add_to_breakpoint_chain (struct breakpoint *b)
7298 {
7299 struct breakpoint *b1;
7300
7301 /* Add this breakpoint to the end of the chain so that a list of
7302 breakpoints will come out in order of increasing numbers. */
7303
7304 b1 = breakpoint_chain;
7305 if (b1 == 0)
7306 breakpoint_chain = b;
7307 else
7308 {
7309 while (b1->next)
7310 b1 = b1->next;
7311 b1->next = b;
7312 }
7313 }
7314
7315 /* Initializes breakpoint B with type BPTYPE and no locations yet. */
7316
7317 static void
7318 init_raw_breakpoint_without_location (struct breakpoint *b,
7319 struct gdbarch *gdbarch,
7320 enum bptype bptype,
7321 const struct breakpoint_ops *ops)
7322 {
7323 memset (b, 0, sizeof (*b));
7324
7325 gdb_assert (ops != NULL);
7326
7327 b->ops = ops;
7328 b->type = bptype;
7329 b->gdbarch = gdbarch;
7330 b->language = current_language->la_language;
7331 b->input_radix = input_radix;
7332 b->thread = -1;
7333 b->enable_state = bp_enabled;
7334 b->next = 0;
7335 b->silent = 0;
7336 b->ignore_count = 0;
7337 b->commands = NULL;
7338 b->frame_id = null_frame_id;
7339 b->condition_not_parsed = 0;
7340 b->py_bp_object = NULL;
7341 b->related_breakpoint = b;
7342 b->location = NULL;
7343 }
7344
7345 /* Helper to set_raw_breakpoint below. Creates a breakpoint
7346 that has type BPTYPE and has no locations as yet. */
7347
7348 static struct breakpoint *
7349 set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
7350 enum bptype bptype,
7351 const struct breakpoint_ops *ops)
7352 {
7353 struct breakpoint *b = XNEW (struct breakpoint);
7354
7355 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7356 add_to_breakpoint_chain (b);
7357 return b;
7358 }
7359
7360 /* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7361 resolutions should be made as the user specified the location explicitly
7362 enough. */
7363
7364 static void
7365 set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
7366 {
7367 gdb_assert (loc->owner != NULL);
7368
7369 if (loc->owner->type == bp_breakpoint
7370 || loc->owner->type == bp_hardware_breakpoint
7371 || is_tracepoint (loc->owner))
7372 {
7373 int is_gnu_ifunc;
7374 const char *function_name;
7375 CORE_ADDR func_addr;
7376
7377 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
7378 &func_addr, NULL, &is_gnu_ifunc);
7379
7380 if (is_gnu_ifunc && !explicit_loc)
7381 {
7382 struct breakpoint *b = loc->owner;
7383
7384 gdb_assert (loc->pspace == current_program_space);
7385 if (gnu_ifunc_resolve_name (function_name,
7386 &loc->requested_address))
7387 {
7388 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7389 loc->address = adjust_breakpoint_address (loc->gdbarch,
7390 loc->requested_address,
7391 b->type);
7392 }
7393 else if (b->type == bp_breakpoint && b->loc == loc
7394 && loc->next == NULL && b->related_breakpoint == b)
7395 {
7396 /* Create only the whole new breakpoint of this type but do not
7397 mess more complicated breakpoints with multiple locations. */
7398 b->type = bp_gnu_ifunc_resolver;
7399 /* Remember the resolver's address for use by the return
7400 breakpoint. */
7401 loc->related_address = func_addr;
7402 }
7403 }
7404
7405 if (function_name)
7406 loc->function_name = xstrdup (function_name);
7407 }
7408 }
7409
7410 /* Attempt to determine architecture of location identified by SAL. */
7411 struct gdbarch *
7412 get_sal_arch (struct symtab_and_line sal)
7413 {
7414 if (sal.section)
7415 return get_objfile_arch (sal.section->objfile);
7416 if (sal.symtab)
7417 return get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
7418
7419 return NULL;
7420 }
7421
7422 /* Low level routine for partially initializing a breakpoint of type
7423 BPTYPE. The newly created breakpoint's address, section, source
7424 file name, and line number are provided by SAL.
7425
7426 It is expected that the caller will complete the initialization of
7427 the newly created breakpoint struct as well as output any status
7428 information regarding the creation of a new breakpoint. */
7429
7430 static void
7431 init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
7432 struct symtab_and_line sal, enum bptype bptype,
7433 const struct breakpoint_ops *ops)
7434 {
7435 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7436
7437 add_location_to_breakpoint (b, &sal);
7438
7439 if (bptype != bp_catchpoint)
7440 gdb_assert (sal.pspace != NULL);
7441
7442 /* Store the program space that was used to set the breakpoint,
7443 except for ordinary breakpoints, which are independent of the
7444 program space. */
7445 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7446 b->pspace = sal.pspace;
7447 }
7448
7449 /* set_raw_breakpoint is a low level routine for allocating and
7450 partially initializing a breakpoint of type BPTYPE. The newly
7451 created breakpoint's address, section, source file name, and line
7452 number are provided by SAL. The newly created and partially
7453 initialized breakpoint is added to the breakpoint chain and
7454 is also returned as the value of this function.
7455
7456 It is expected that the caller will complete the initialization of
7457 the newly created breakpoint struct as well as output any status
7458 information regarding the creation of a new breakpoint. In
7459 particular, set_raw_breakpoint does NOT set the breakpoint
7460 number! Care should be taken to not allow an error to occur
7461 prior to completing the initialization of the breakpoint. If this
7462 should happen, a bogus breakpoint will be left on the chain. */
7463
7464 struct breakpoint *
7465 set_raw_breakpoint (struct gdbarch *gdbarch,
7466 struct symtab_and_line sal, enum bptype bptype,
7467 const struct breakpoint_ops *ops)
7468 {
7469 struct breakpoint *b = XNEW (struct breakpoint);
7470
7471 init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
7472 add_to_breakpoint_chain (b);
7473 return b;
7474 }
7475
7476 /* Call this routine when stepping and nexting to enable a breakpoint
7477 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7478 initiated the operation. */
7479
7480 void
7481 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
7482 {
7483 struct breakpoint *b, *b_tmp;
7484 int thread = tp->num;
7485
7486 /* To avoid having to rescan all objfile symbols at every step,
7487 we maintain a list of continually-inserted but always disabled
7488 longjmp "master" breakpoints. Here, we simply create momentary
7489 clones of those and enable them for the requested thread. */
7490 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7491 if (b->pspace == current_program_space
7492 && (b->type == bp_longjmp_master
7493 || b->type == bp_exception_master))
7494 {
7495 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7496 struct breakpoint *clone;
7497
7498 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7499 after their removal. */
7500 clone = momentary_breakpoint_from_master (b, type,
7501 &longjmp_breakpoint_ops, 1);
7502 clone->thread = thread;
7503 }
7504
7505 tp->initiating_frame = frame;
7506 }
7507
7508 /* Delete all longjmp breakpoints from THREAD. */
7509 void
7510 delete_longjmp_breakpoint (int thread)
7511 {
7512 struct breakpoint *b, *b_tmp;
7513
7514 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7515 if (b->type == bp_longjmp || b->type == bp_exception)
7516 {
7517 if (b->thread == thread)
7518 delete_breakpoint (b);
7519 }
7520 }
7521
7522 void
7523 delete_longjmp_breakpoint_at_next_stop (int thread)
7524 {
7525 struct breakpoint *b, *b_tmp;
7526
7527 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7528 if (b->type == bp_longjmp || b->type == bp_exception)
7529 {
7530 if (b->thread == thread)
7531 b->disposition = disp_del_at_next_stop;
7532 }
7533 }
7534
7535 /* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7536 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7537 pointer to any of them. Return NULL if this system cannot place longjmp
7538 breakpoints. */
7539
7540 struct breakpoint *
7541 set_longjmp_breakpoint_for_call_dummy (void)
7542 {
7543 struct breakpoint *b, *retval = NULL;
7544
7545 ALL_BREAKPOINTS (b)
7546 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7547 {
7548 struct breakpoint *new_b;
7549
7550 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7551 &momentary_breakpoint_ops,
7552 1);
7553 new_b->thread = pid_to_thread_id (inferior_ptid);
7554
7555 /* Link NEW_B into the chain of RETVAL breakpoints. */
7556
7557 gdb_assert (new_b->related_breakpoint == new_b);
7558 if (retval == NULL)
7559 retval = new_b;
7560 new_b->related_breakpoint = retval;
7561 while (retval->related_breakpoint != new_b->related_breakpoint)
7562 retval = retval->related_breakpoint;
7563 retval->related_breakpoint = new_b;
7564 }
7565
7566 return retval;
7567 }
7568
7569 /* Verify all existing dummy frames and their associated breakpoints for
7570 TP. Remove those which can no longer be found in the current frame
7571 stack.
7572
7573 You should call this function only at places where it is safe to currently
7574 unwind the whole stack. Failed stack unwind would discard live dummy
7575 frames. */
7576
7577 void
7578 check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
7579 {
7580 struct breakpoint *b, *b_tmp;
7581
7582 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7583 if (b->type == bp_longjmp_call_dummy && b->thread == tp->num)
7584 {
7585 struct breakpoint *dummy_b = b->related_breakpoint;
7586
7587 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7588 dummy_b = dummy_b->related_breakpoint;
7589 if (dummy_b->type != bp_call_dummy
7590 || frame_find_by_id (dummy_b->frame_id) != NULL)
7591 continue;
7592
7593 dummy_frame_discard (dummy_b->frame_id, tp->ptid);
7594
7595 while (b->related_breakpoint != b)
7596 {
7597 if (b_tmp == b->related_breakpoint)
7598 b_tmp = b->related_breakpoint->next;
7599 delete_breakpoint (b->related_breakpoint);
7600 }
7601 delete_breakpoint (b);
7602 }
7603 }
7604
7605 void
7606 enable_overlay_breakpoints (void)
7607 {
7608 struct breakpoint *b;
7609
7610 ALL_BREAKPOINTS (b)
7611 if (b->type == bp_overlay_event)
7612 {
7613 b->enable_state = bp_enabled;
7614 update_global_location_list (UGLL_MAY_INSERT);
7615 overlay_events_enabled = 1;
7616 }
7617 }
7618
7619 void
7620 disable_overlay_breakpoints (void)
7621 {
7622 struct breakpoint *b;
7623
7624 ALL_BREAKPOINTS (b)
7625 if (b->type == bp_overlay_event)
7626 {
7627 b->enable_state = bp_disabled;
7628 update_global_location_list (UGLL_DONT_INSERT);
7629 overlay_events_enabled = 0;
7630 }
7631 }
7632
7633 /* Set an active std::terminate breakpoint for each std::terminate
7634 master breakpoint. */
7635 void
7636 set_std_terminate_breakpoint (void)
7637 {
7638 struct breakpoint *b, *b_tmp;
7639
7640 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7641 if (b->pspace == current_program_space
7642 && b->type == bp_std_terminate_master)
7643 {
7644 momentary_breakpoint_from_master (b, bp_std_terminate,
7645 &momentary_breakpoint_ops, 1);
7646 }
7647 }
7648
7649 /* Delete all the std::terminate breakpoints. */
7650 void
7651 delete_std_terminate_breakpoint (void)
7652 {
7653 struct breakpoint *b, *b_tmp;
7654
7655 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7656 if (b->type == bp_std_terminate)
7657 delete_breakpoint (b);
7658 }
7659
7660 struct breakpoint *
7661 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7662 {
7663 struct breakpoint *b;
7664
7665 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7666 &internal_breakpoint_ops);
7667
7668 b->enable_state = bp_enabled;
7669 /* location has to be used or breakpoint_re_set will delete me. */
7670 b->location = new_address_location (b->loc->address);
7671
7672 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7673
7674 return b;
7675 }
7676
7677 void
7678 remove_thread_event_breakpoints (void)
7679 {
7680 struct breakpoint *b, *b_tmp;
7681
7682 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7683 if (b->type == bp_thread_event
7684 && b->loc->pspace == current_program_space)
7685 delete_breakpoint (b);
7686 }
7687
7688 struct lang_and_radix
7689 {
7690 enum language lang;
7691 int radix;
7692 };
7693
7694 /* Create a breakpoint for JIT code registration and unregistration. */
7695
7696 struct breakpoint *
7697 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7698 {
7699 struct breakpoint *b;
7700
7701 b = create_internal_breakpoint (gdbarch, address, bp_jit_event,
7702 &internal_breakpoint_ops);
7703 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7704 return b;
7705 }
7706
7707 /* Remove JIT code registration and unregistration breakpoint(s). */
7708
7709 void
7710 remove_jit_event_breakpoints (void)
7711 {
7712 struct breakpoint *b, *b_tmp;
7713
7714 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7715 if (b->type == bp_jit_event
7716 && b->loc->pspace == current_program_space)
7717 delete_breakpoint (b);
7718 }
7719
7720 void
7721 remove_solib_event_breakpoints (void)
7722 {
7723 struct breakpoint *b, *b_tmp;
7724
7725 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7726 if (b->type == bp_shlib_event
7727 && b->loc->pspace == current_program_space)
7728 delete_breakpoint (b);
7729 }
7730
7731 /* See breakpoint.h. */
7732
7733 void
7734 remove_solib_event_breakpoints_at_next_stop (void)
7735 {
7736 struct breakpoint *b, *b_tmp;
7737
7738 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7739 if (b->type == bp_shlib_event
7740 && b->loc->pspace == current_program_space)
7741 b->disposition = disp_del_at_next_stop;
7742 }
7743
7744 /* Helper for create_solib_event_breakpoint /
7745 create_and_insert_solib_event_breakpoint. Allows specifying which
7746 INSERT_MODE to pass through to update_global_location_list. */
7747
7748 static struct breakpoint *
7749 create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7750 enum ugll_insert_mode insert_mode)
7751 {
7752 struct breakpoint *b;
7753
7754 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7755 &internal_breakpoint_ops);
7756 update_global_location_list_nothrow (insert_mode);
7757 return b;
7758 }
7759
7760 struct breakpoint *
7761 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7762 {
7763 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7764 }
7765
7766 /* See breakpoint.h. */
7767
7768 struct breakpoint *
7769 create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7770 {
7771 struct breakpoint *b;
7772
7773 /* Explicitly tell update_global_location_list to insert
7774 locations. */
7775 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
7776 if (!b->loc->inserted)
7777 {
7778 delete_breakpoint (b);
7779 return NULL;
7780 }
7781 return b;
7782 }
7783
7784 /* Disable any breakpoints that are on code in shared libraries. Only
7785 apply to enabled breakpoints, disabled ones can just stay disabled. */
7786
7787 void
7788 disable_breakpoints_in_shlibs (void)
7789 {
7790 struct bp_location *loc, **locp_tmp;
7791
7792 ALL_BP_LOCATIONS (loc, locp_tmp)
7793 {
7794 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7795 struct breakpoint *b = loc->owner;
7796
7797 /* We apply the check to all breakpoints, including disabled for
7798 those with loc->duplicate set. This is so that when breakpoint
7799 becomes enabled, or the duplicate is removed, gdb will try to
7800 insert all breakpoints. If we don't set shlib_disabled here,
7801 we'll try to insert those breakpoints and fail. */
7802 if (((b->type == bp_breakpoint)
7803 || (b->type == bp_jit_event)
7804 || (b->type == bp_hardware_breakpoint)
7805 || (is_tracepoint (b)))
7806 && loc->pspace == current_program_space
7807 && !loc->shlib_disabled
7808 && solib_name_from_address (loc->pspace, loc->address)
7809 )
7810 {
7811 loc->shlib_disabled = 1;
7812 }
7813 }
7814 }
7815
7816 /* Disable any breakpoints and tracepoints that are in SOLIB upon
7817 notification of unloaded_shlib. Only apply to enabled breakpoints,
7818 disabled ones can just stay disabled. */
7819
7820 static void
7821 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7822 {
7823 struct bp_location *loc, **locp_tmp;
7824 int disabled_shlib_breaks = 0;
7825
7826 /* SunOS a.out shared libraries are always mapped, so do not
7827 disable breakpoints; they will only be reported as unloaded
7828 through clear_solib when GDB discards its shared library
7829 list. See clear_solib for more information. */
7830 if (exec_bfd != NULL
7831 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
7832 return;
7833
7834 ALL_BP_LOCATIONS (loc, locp_tmp)
7835 {
7836 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7837 struct breakpoint *b = loc->owner;
7838
7839 if (solib->pspace == loc->pspace
7840 && !loc->shlib_disabled
7841 && (((b->type == bp_breakpoint
7842 || b->type == bp_jit_event
7843 || b->type == bp_hardware_breakpoint)
7844 && (loc->loc_type == bp_loc_hardware_breakpoint
7845 || loc->loc_type == bp_loc_software_breakpoint))
7846 || is_tracepoint (b))
7847 && solib_contains_address_p (solib, loc->address))
7848 {
7849 loc->shlib_disabled = 1;
7850 /* At this point, we cannot rely on remove_breakpoint
7851 succeeding so we must mark the breakpoint as not inserted
7852 to prevent future errors occurring in remove_breakpoints. */
7853 loc->inserted = 0;
7854
7855 /* This may cause duplicate notifications for the same breakpoint. */
7856 observer_notify_breakpoint_modified (b);
7857
7858 if (!disabled_shlib_breaks)
7859 {
7860 target_terminal_ours_for_output ();
7861 warning (_("Temporarily disabling breakpoints "
7862 "for unloaded shared library \"%s\""),
7863 solib->so_name);
7864 }
7865 disabled_shlib_breaks = 1;
7866 }
7867 }
7868 }
7869
7870 /* Disable any breakpoints and tracepoints in OBJFILE upon
7871 notification of free_objfile. Only apply to enabled breakpoints,
7872 disabled ones can just stay disabled. */
7873
7874 static void
7875 disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7876 {
7877 struct breakpoint *b;
7878
7879 if (objfile == NULL)
7880 return;
7881
7882 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7883 managed by the user with add-symbol-file/remove-symbol-file.
7884 Similarly to how breakpoints in shared libraries are handled in
7885 response to "nosharedlibrary", mark breakpoints in such modules
7886 shlib_disabled so they end up uninserted on the next global
7887 location list update. Shared libraries not loaded by the user
7888 aren't handled here -- they're already handled in
7889 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7890 solib_unloaded observer. We skip objfiles that are not
7891 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7892 main objfile). */
7893 if ((objfile->flags & OBJF_SHARED) == 0
7894 || (objfile->flags & OBJF_USERLOADED) == 0)
7895 return;
7896
7897 ALL_BREAKPOINTS (b)
7898 {
7899 struct bp_location *loc;
7900 int bp_modified = 0;
7901
7902 if (!is_breakpoint (b) && !is_tracepoint (b))
7903 continue;
7904
7905 for (loc = b->loc; loc != NULL; loc = loc->next)
7906 {
7907 CORE_ADDR loc_addr = loc->address;
7908
7909 if (loc->loc_type != bp_loc_hardware_breakpoint
7910 && loc->loc_type != bp_loc_software_breakpoint)
7911 continue;
7912
7913 if (loc->shlib_disabled != 0)
7914 continue;
7915
7916 if (objfile->pspace != loc->pspace)
7917 continue;
7918
7919 if (loc->loc_type != bp_loc_hardware_breakpoint
7920 && loc->loc_type != bp_loc_software_breakpoint)
7921 continue;
7922
7923 if (is_addr_in_objfile (loc_addr, objfile))
7924 {
7925 loc->shlib_disabled = 1;
7926 /* At this point, we don't know whether the object was
7927 unmapped from the inferior or not, so leave the
7928 inserted flag alone. We'll handle failure to
7929 uninsert quietly, in case the object was indeed
7930 unmapped. */
7931
7932 mark_breakpoint_location_modified (loc);
7933
7934 bp_modified = 1;
7935 }
7936 }
7937
7938 if (bp_modified)
7939 observer_notify_breakpoint_modified (b);
7940 }
7941 }
7942
7943 /* FORK & VFORK catchpoints. */
7944
7945 /* An instance of this type is used to represent a fork or vfork
7946 catchpoint. It includes a "struct breakpoint" as a kind of base
7947 class; users downcast to "struct breakpoint *" when needed. A
7948 breakpoint is really of this type iff its ops pointer points to
7949 CATCH_FORK_BREAKPOINT_OPS. */
7950
7951 struct fork_catchpoint
7952 {
7953 /* The base class. */
7954 struct breakpoint base;
7955
7956 /* Process id of a child process whose forking triggered this
7957 catchpoint. This field is only valid immediately after this
7958 catchpoint has triggered. */
7959 ptid_t forked_inferior_pid;
7960 };
7961
7962 /* Implement the "insert" breakpoint_ops method for fork
7963 catchpoints. */
7964
7965 static int
7966 insert_catch_fork (struct bp_location *bl)
7967 {
7968 return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
7969 }
7970
7971 /* Implement the "remove" breakpoint_ops method for fork
7972 catchpoints. */
7973
7974 static int
7975 remove_catch_fork (struct bp_location *bl)
7976 {
7977 return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
7978 }
7979
7980 /* Implement the "breakpoint_hit" breakpoint_ops method for fork
7981 catchpoints. */
7982
7983 static int
7984 breakpoint_hit_catch_fork (const struct bp_location *bl,
7985 struct address_space *aspace, CORE_ADDR bp_addr,
7986 const struct target_waitstatus *ws)
7987 {
7988 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7989
7990 if (ws->kind != TARGET_WAITKIND_FORKED)
7991 return 0;
7992
7993 c->forked_inferior_pid = ws->value.related_pid;
7994 return 1;
7995 }
7996
7997 /* Implement the "print_it" breakpoint_ops method for fork
7998 catchpoints. */
7999
8000 static enum print_stop_action
8001 print_it_catch_fork (bpstat bs)
8002 {
8003 struct ui_out *uiout = current_uiout;
8004 struct breakpoint *b = bs->breakpoint_at;
8005 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
8006
8007 annotate_catchpoint (b->number);
8008 if (b->disposition == disp_del)
8009 ui_out_text (uiout, "\nTemporary catchpoint ");
8010 else
8011 ui_out_text (uiout, "\nCatchpoint ");
8012 if (ui_out_is_mi_like_p (uiout))
8013 {
8014 ui_out_field_string (uiout, "reason",
8015 async_reason_lookup (EXEC_ASYNC_FORK));
8016 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8017 }
8018 ui_out_field_int (uiout, "bkptno", b->number);
8019 ui_out_text (uiout, " (forked process ");
8020 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
8021 ui_out_text (uiout, "), ");
8022 return PRINT_SRC_AND_LOC;
8023 }
8024
8025 /* Implement the "print_one" breakpoint_ops method for fork
8026 catchpoints. */
8027
8028 static void
8029 print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
8030 {
8031 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8032 struct value_print_options opts;
8033 struct ui_out *uiout = current_uiout;
8034
8035 get_user_print_options (&opts);
8036
8037 /* Field 4, the address, is omitted (which makes the columns not
8038 line up too nicely with the headers, but the effect is relatively
8039 readable). */
8040 if (opts.addressprint)
8041 ui_out_field_skip (uiout, "addr");
8042 annotate_field (5);
8043 ui_out_text (uiout, "fork");
8044 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
8045 {
8046 ui_out_text (uiout, ", process ");
8047 ui_out_field_int (uiout, "what",
8048 ptid_get_pid (c->forked_inferior_pid));
8049 ui_out_spaces (uiout, 1);
8050 }
8051
8052 if (ui_out_is_mi_like_p (uiout))
8053 ui_out_field_string (uiout, "catch-type", "fork");
8054 }
8055
8056 /* Implement the "print_mention" breakpoint_ops method for fork
8057 catchpoints. */
8058
8059 static void
8060 print_mention_catch_fork (struct breakpoint *b)
8061 {
8062 printf_filtered (_("Catchpoint %d (fork)"), b->number);
8063 }
8064
8065 /* Implement the "print_recreate" breakpoint_ops method for fork
8066 catchpoints. */
8067
8068 static void
8069 print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
8070 {
8071 fprintf_unfiltered (fp, "catch fork");
8072 print_recreate_thread (b, fp);
8073 }
8074
8075 /* The breakpoint_ops structure to be used in fork catchpoints. */
8076
8077 static struct breakpoint_ops catch_fork_breakpoint_ops;
8078
8079 /* Implement the "insert" breakpoint_ops method for vfork
8080 catchpoints. */
8081
8082 static int
8083 insert_catch_vfork (struct bp_location *bl)
8084 {
8085 return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
8086 }
8087
8088 /* Implement the "remove" breakpoint_ops method for vfork
8089 catchpoints. */
8090
8091 static int
8092 remove_catch_vfork (struct bp_location *bl)
8093 {
8094 return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
8095 }
8096
8097 /* Implement the "breakpoint_hit" breakpoint_ops method for vfork
8098 catchpoints. */
8099
8100 static int
8101 breakpoint_hit_catch_vfork (const struct bp_location *bl,
8102 struct address_space *aspace, CORE_ADDR bp_addr,
8103 const struct target_waitstatus *ws)
8104 {
8105 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8106
8107 if (ws->kind != TARGET_WAITKIND_VFORKED)
8108 return 0;
8109
8110 c->forked_inferior_pid = ws->value.related_pid;
8111 return 1;
8112 }
8113
8114 /* Implement the "print_it" breakpoint_ops method for vfork
8115 catchpoints. */
8116
8117 static enum print_stop_action
8118 print_it_catch_vfork (bpstat bs)
8119 {
8120 struct ui_out *uiout = current_uiout;
8121 struct breakpoint *b = bs->breakpoint_at;
8122 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8123
8124 annotate_catchpoint (b->number);
8125 if (b->disposition == disp_del)
8126 ui_out_text (uiout, "\nTemporary catchpoint ");
8127 else
8128 ui_out_text (uiout, "\nCatchpoint ");
8129 if (ui_out_is_mi_like_p (uiout))
8130 {
8131 ui_out_field_string (uiout, "reason",
8132 async_reason_lookup (EXEC_ASYNC_VFORK));
8133 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8134 }
8135 ui_out_field_int (uiout, "bkptno", b->number);
8136 ui_out_text (uiout, " (vforked process ");
8137 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
8138 ui_out_text (uiout, "), ");
8139 return PRINT_SRC_AND_LOC;
8140 }
8141
8142 /* Implement the "print_one" breakpoint_ops method for vfork
8143 catchpoints. */
8144
8145 static void
8146 print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
8147 {
8148 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8149 struct value_print_options opts;
8150 struct ui_out *uiout = current_uiout;
8151
8152 get_user_print_options (&opts);
8153 /* Field 4, the address, is omitted (which makes the columns not
8154 line up too nicely with the headers, but the effect is relatively
8155 readable). */
8156 if (opts.addressprint)
8157 ui_out_field_skip (uiout, "addr");
8158 annotate_field (5);
8159 ui_out_text (uiout, "vfork");
8160 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
8161 {
8162 ui_out_text (uiout, ", process ");
8163 ui_out_field_int (uiout, "what",
8164 ptid_get_pid (c->forked_inferior_pid));
8165 ui_out_spaces (uiout, 1);
8166 }
8167
8168 if (ui_out_is_mi_like_p (uiout))
8169 ui_out_field_string (uiout, "catch-type", "vfork");
8170 }
8171
8172 /* Implement the "print_mention" breakpoint_ops method for vfork
8173 catchpoints. */
8174
8175 static void
8176 print_mention_catch_vfork (struct breakpoint *b)
8177 {
8178 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
8179 }
8180
8181 /* Implement the "print_recreate" breakpoint_ops method for vfork
8182 catchpoints. */
8183
8184 static void
8185 print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8186 {
8187 fprintf_unfiltered (fp, "catch vfork");
8188 print_recreate_thread (b, fp);
8189 }
8190
8191 /* The breakpoint_ops structure to be used in vfork catchpoints. */
8192
8193 static struct breakpoint_ops catch_vfork_breakpoint_ops;
8194
8195 /* An instance of this type is used to represent an solib catchpoint.
8196 It includes a "struct breakpoint" as a kind of base class; users
8197 downcast to "struct breakpoint *" when needed. A breakpoint is
8198 really of this type iff its ops pointer points to
8199 CATCH_SOLIB_BREAKPOINT_OPS. */
8200
8201 struct solib_catchpoint
8202 {
8203 /* The base class. */
8204 struct breakpoint base;
8205
8206 /* True for "catch load", false for "catch unload". */
8207 unsigned char is_load;
8208
8209 /* Regular expression to match, if any. COMPILED is only valid when
8210 REGEX is non-NULL. */
8211 char *regex;
8212 regex_t compiled;
8213 };
8214
8215 static void
8216 dtor_catch_solib (struct breakpoint *b)
8217 {
8218 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8219
8220 if (self->regex)
8221 regfree (&self->compiled);
8222 xfree (self->regex);
8223
8224 base_breakpoint_ops.dtor (b);
8225 }
8226
8227 static int
8228 insert_catch_solib (struct bp_location *ignore)
8229 {
8230 return 0;
8231 }
8232
8233 static int
8234 remove_catch_solib (struct bp_location *ignore)
8235 {
8236 return 0;
8237 }
8238
8239 static int
8240 breakpoint_hit_catch_solib (const struct bp_location *bl,
8241 struct address_space *aspace,
8242 CORE_ADDR bp_addr,
8243 const struct target_waitstatus *ws)
8244 {
8245 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8246 struct breakpoint *other;
8247
8248 if (ws->kind == TARGET_WAITKIND_LOADED)
8249 return 1;
8250
8251 ALL_BREAKPOINTS (other)
8252 {
8253 struct bp_location *other_bl;
8254
8255 if (other == bl->owner)
8256 continue;
8257
8258 if (other->type != bp_shlib_event)
8259 continue;
8260
8261 if (self->base.pspace != NULL && other->pspace != self->base.pspace)
8262 continue;
8263
8264 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8265 {
8266 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8267 return 1;
8268 }
8269 }
8270
8271 return 0;
8272 }
8273
8274 static void
8275 check_status_catch_solib (struct bpstats *bs)
8276 {
8277 struct solib_catchpoint *self
8278 = (struct solib_catchpoint *) bs->breakpoint_at;
8279 int ix;
8280
8281 if (self->is_load)
8282 {
8283 struct so_list *iter;
8284
8285 for (ix = 0;
8286 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
8287 ix, iter);
8288 ++ix)
8289 {
8290 if (!self->regex
8291 || regexec (&self->compiled, iter->so_name, 0, NULL, 0) == 0)
8292 return;
8293 }
8294 }
8295 else
8296 {
8297 char *iter;
8298
8299 for (ix = 0;
8300 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
8301 ix, iter);
8302 ++ix)
8303 {
8304 if (!self->regex
8305 || regexec (&self->compiled, iter, 0, NULL, 0) == 0)
8306 return;
8307 }
8308 }
8309
8310 bs->stop = 0;
8311 bs->print_it = print_it_noop;
8312 }
8313
8314 static enum print_stop_action
8315 print_it_catch_solib (bpstat bs)
8316 {
8317 struct breakpoint *b = bs->breakpoint_at;
8318 struct ui_out *uiout = current_uiout;
8319
8320 annotate_catchpoint (b->number);
8321 if (b->disposition == disp_del)
8322 ui_out_text (uiout, "\nTemporary catchpoint ");
8323 else
8324 ui_out_text (uiout, "\nCatchpoint ");
8325 ui_out_field_int (uiout, "bkptno", b->number);
8326 ui_out_text (uiout, "\n");
8327 if (ui_out_is_mi_like_p (uiout))
8328 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8329 print_solib_event (1);
8330 return PRINT_SRC_AND_LOC;
8331 }
8332
8333 static void
8334 print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8335 {
8336 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8337 struct value_print_options opts;
8338 struct ui_out *uiout = current_uiout;
8339 char *msg;
8340
8341 get_user_print_options (&opts);
8342 /* Field 4, the address, is omitted (which makes the columns not
8343 line up too nicely with the headers, but the effect is relatively
8344 readable). */
8345 if (opts.addressprint)
8346 {
8347 annotate_field (4);
8348 ui_out_field_skip (uiout, "addr");
8349 }
8350
8351 annotate_field (5);
8352 if (self->is_load)
8353 {
8354 if (self->regex)
8355 msg = xstrprintf (_("load of library matching %s"), self->regex);
8356 else
8357 msg = xstrdup (_("load of library"));
8358 }
8359 else
8360 {
8361 if (self->regex)
8362 msg = xstrprintf (_("unload of library matching %s"), self->regex);
8363 else
8364 msg = xstrdup (_("unload of library"));
8365 }
8366 ui_out_field_string (uiout, "what", msg);
8367 xfree (msg);
8368
8369 if (ui_out_is_mi_like_p (uiout))
8370 ui_out_field_string (uiout, "catch-type",
8371 self->is_load ? "load" : "unload");
8372 }
8373
8374 static void
8375 print_mention_catch_solib (struct breakpoint *b)
8376 {
8377 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8378
8379 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8380 self->is_load ? "load" : "unload");
8381 }
8382
8383 static void
8384 print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8385 {
8386 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8387
8388 fprintf_unfiltered (fp, "%s %s",
8389 b->disposition == disp_del ? "tcatch" : "catch",
8390 self->is_load ? "load" : "unload");
8391 if (self->regex)
8392 fprintf_unfiltered (fp, " %s", self->regex);
8393 fprintf_unfiltered (fp, "\n");
8394 }
8395
8396 static struct breakpoint_ops catch_solib_breakpoint_ops;
8397
8398 /* Shared helper function (MI and CLI) for creating and installing
8399 a shared object event catchpoint. If IS_LOAD is non-zero then
8400 the events to be caught are load events, otherwise they are
8401 unload events. If IS_TEMP is non-zero the catchpoint is a
8402 temporary one. If ENABLED is non-zero the catchpoint is
8403 created in an enabled state. */
8404
8405 void
8406 add_solib_catchpoint (char *arg, int is_load, int is_temp, int enabled)
8407 {
8408 struct solib_catchpoint *c;
8409 struct gdbarch *gdbarch = get_current_arch ();
8410 struct cleanup *cleanup;
8411
8412 if (!arg)
8413 arg = "";
8414 arg = skip_spaces (arg);
8415
8416 c = XCNEW (struct solib_catchpoint);
8417 cleanup = make_cleanup (xfree, c);
8418
8419 if (*arg != '\0')
8420 {
8421 int errcode;
8422
8423 errcode = regcomp (&c->compiled, arg, REG_NOSUB);
8424 if (errcode != 0)
8425 {
8426 char *err = get_regcomp_error (errcode, &c->compiled);
8427
8428 make_cleanup (xfree, err);
8429 error (_("Invalid regexp (%s): %s"), err, arg);
8430 }
8431 c->regex = xstrdup (arg);
8432 }
8433
8434 c->is_load = is_load;
8435 init_catchpoint (&c->base, gdbarch, is_temp, NULL,
8436 &catch_solib_breakpoint_ops);
8437
8438 c->base.enable_state = enabled ? bp_enabled : bp_disabled;
8439
8440 discard_cleanups (cleanup);
8441 install_breakpoint (0, &c->base, 1);
8442 }
8443
8444 /* A helper function that does all the work for "catch load" and
8445 "catch unload". */
8446
8447 static void
8448 catch_load_or_unload (char *arg, int from_tty, int is_load,
8449 struct cmd_list_element *command)
8450 {
8451 int tempflag;
8452 const int enabled = 1;
8453
8454 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8455
8456 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8457 }
8458
8459 static void
8460 catch_load_command_1 (char *arg, int from_tty,
8461 struct cmd_list_element *command)
8462 {
8463 catch_load_or_unload (arg, from_tty, 1, command);
8464 }
8465
8466 static void
8467 catch_unload_command_1 (char *arg, int from_tty,
8468 struct cmd_list_element *command)
8469 {
8470 catch_load_or_unload (arg, from_tty, 0, command);
8471 }
8472
8473 /* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8474 is non-zero, then make the breakpoint temporary. If COND_STRING is
8475 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8476 the breakpoint_ops structure associated to the catchpoint. */
8477
8478 void
8479 init_catchpoint (struct breakpoint *b,
8480 struct gdbarch *gdbarch, int tempflag,
8481 char *cond_string,
8482 const struct breakpoint_ops *ops)
8483 {
8484 struct symtab_and_line sal;
8485
8486 init_sal (&sal);
8487 sal.pspace = current_program_space;
8488
8489 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
8490
8491 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
8492 b->disposition = tempflag ? disp_del : disp_donttouch;
8493 }
8494
8495 void
8496 install_breakpoint (int internal, struct breakpoint *b, int update_gll)
8497 {
8498 add_to_breakpoint_chain (b);
8499 set_breakpoint_number (internal, b);
8500 if (is_tracepoint (b))
8501 set_tracepoint_count (breakpoint_count);
8502 if (!internal)
8503 mention (b);
8504 observer_notify_breakpoint_created (b);
8505
8506 if (update_gll)
8507 update_global_location_list (UGLL_MAY_INSERT);
8508 }
8509
8510 static void
8511 create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8512 int tempflag, char *cond_string,
8513 const struct breakpoint_ops *ops)
8514 {
8515 struct fork_catchpoint *c = XNEW (struct fork_catchpoint);
8516
8517 init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
8518
8519 c->forked_inferior_pid = null_ptid;
8520
8521 install_breakpoint (0, &c->base, 1);
8522 }
8523
8524 /* Exec catchpoints. */
8525
8526 /* An instance of this type is used to represent an exec catchpoint.
8527 It includes a "struct breakpoint" as a kind of base class; users
8528 downcast to "struct breakpoint *" when needed. A breakpoint is
8529 really of this type iff its ops pointer points to
8530 CATCH_EXEC_BREAKPOINT_OPS. */
8531
8532 struct exec_catchpoint
8533 {
8534 /* The base class. */
8535 struct breakpoint base;
8536
8537 /* Filename of a program whose exec triggered this catchpoint.
8538 This field is only valid immediately after this catchpoint has
8539 triggered. */
8540 char *exec_pathname;
8541 };
8542
8543 /* Implement the "dtor" breakpoint_ops method for exec
8544 catchpoints. */
8545
8546 static void
8547 dtor_catch_exec (struct breakpoint *b)
8548 {
8549 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8550
8551 xfree (c->exec_pathname);
8552
8553 base_breakpoint_ops.dtor (b);
8554 }
8555
8556 static int
8557 insert_catch_exec (struct bp_location *bl)
8558 {
8559 return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
8560 }
8561
8562 static int
8563 remove_catch_exec (struct bp_location *bl)
8564 {
8565 return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
8566 }
8567
8568 static int
8569 breakpoint_hit_catch_exec (const struct bp_location *bl,
8570 struct address_space *aspace, CORE_ADDR bp_addr,
8571 const struct target_waitstatus *ws)
8572 {
8573 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8574
8575 if (ws->kind != TARGET_WAITKIND_EXECD)
8576 return 0;
8577
8578 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8579 return 1;
8580 }
8581
8582 static enum print_stop_action
8583 print_it_catch_exec (bpstat bs)
8584 {
8585 struct ui_out *uiout = current_uiout;
8586 struct breakpoint *b = bs->breakpoint_at;
8587 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8588
8589 annotate_catchpoint (b->number);
8590 if (b->disposition == disp_del)
8591 ui_out_text (uiout, "\nTemporary catchpoint ");
8592 else
8593 ui_out_text (uiout, "\nCatchpoint ");
8594 if (ui_out_is_mi_like_p (uiout))
8595 {
8596 ui_out_field_string (uiout, "reason",
8597 async_reason_lookup (EXEC_ASYNC_EXEC));
8598 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8599 }
8600 ui_out_field_int (uiout, "bkptno", b->number);
8601 ui_out_text (uiout, " (exec'd ");
8602 ui_out_field_string (uiout, "new-exec", c->exec_pathname);
8603 ui_out_text (uiout, "), ");
8604
8605 return PRINT_SRC_AND_LOC;
8606 }
8607
8608 static void
8609 print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
8610 {
8611 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8612 struct value_print_options opts;
8613 struct ui_out *uiout = current_uiout;
8614
8615 get_user_print_options (&opts);
8616
8617 /* Field 4, the address, is omitted (which makes the columns
8618 not line up too nicely with the headers, but the effect
8619 is relatively readable). */
8620 if (opts.addressprint)
8621 ui_out_field_skip (uiout, "addr");
8622 annotate_field (5);
8623 ui_out_text (uiout, "exec");
8624 if (c->exec_pathname != NULL)
8625 {
8626 ui_out_text (uiout, ", program \"");
8627 ui_out_field_string (uiout, "what", c->exec_pathname);
8628 ui_out_text (uiout, "\" ");
8629 }
8630
8631 if (ui_out_is_mi_like_p (uiout))
8632 ui_out_field_string (uiout, "catch-type", "exec");
8633 }
8634
8635 static void
8636 print_mention_catch_exec (struct breakpoint *b)
8637 {
8638 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8639 }
8640
8641 /* Implement the "print_recreate" breakpoint_ops method for exec
8642 catchpoints. */
8643
8644 static void
8645 print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8646 {
8647 fprintf_unfiltered (fp, "catch exec");
8648 print_recreate_thread (b, fp);
8649 }
8650
8651 static struct breakpoint_ops catch_exec_breakpoint_ops;
8652
8653 static int
8654 hw_breakpoint_used_count (void)
8655 {
8656 int i = 0;
8657 struct breakpoint *b;
8658 struct bp_location *bl;
8659
8660 ALL_BREAKPOINTS (b)
8661 {
8662 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
8663 for (bl = b->loc; bl; bl = bl->next)
8664 {
8665 /* Special types of hardware breakpoints may use more than
8666 one register. */
8667 i += b->ops->resources_needed (bl);
8668 }
8669 }
8670
8671 return i;
8672 }
8673
8674 /* Returns the resources B would use if it were a hardware
8675 watchpoint. */
8676
8677 static int
8678 hw_watchpoint_use_count (struct breakpoint *b)
8679 {
8680 int i = 0;
8681 struct bp_location *bl;
8682
8683 if (!breakpoint_enabled (b))
8684 return 0;
8685
8686 for (bl = b->loc; bl; bl = bl->next)
8687 {
8688 /* Special types of hardware watchpoints may use more than
8689 one register. */
8690 i += b->ops->resources_needed (bl);
8691 }
8692
8693 return i;
8694 }
8695
8696 /* Returns the sum the used resources of all hardware watchpoints of
8697 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8698 the sum of the used resources of all hardware watchpoints of other
8699 types _not_ TYPE. */
8700
8701 static int
8702 hw_watchpoint_used_count_others (struct breakpoint *except,
8703 enum bptype type, int *other_type_used)
8704 {
8705 int i = 0;
8706 struct breakpoint *b;
8707
8708 *other_type_used = 0;
8709 ALL_BREAKPOINTS (b)
8710 {
8711 if (b == except)
8712 continue;
8713 if (!breakpoint_enabled (b))
8714 continue;
8715
8716 if (b->type == type)
8717 i += hw_watchpoint_use_count (b);
8718 else if (is_hardware_watchpoint (b))
8719 *other_type_used = 1;
8720 }
8721
8722 return i;
8723 }
8724
8725 void
8726 disable_watchpoints_before_interactive_call_start (void)
8727 {
8728 struct breakpoint *b;
8729
8730 ALL_BREAKPOINTS (b)
8731 {
8732 if (is_watchpoint (b) && breakpoint_enabled (b))
8733 {
8734 b->enable_state = bp_call_disabled;
8735 update_global_location_list (UGLL_DONT_INSERT);
8736 }
8737 }
8738 }
8739
8740 void
8741 enable_watchpoints_after_interactive_call_stop (void)
8742 {
8743 struct breakpoint *b;
8744
8745 ALL_BREAKPOINTS (b)
8746 {
8747 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
8748 {
8749 b->enable_state = bp_enabled;
8750 update_global_location_list (UGLL_MAY_INSERT);
8751 }
8752 }
8753 }
8754
8755 void
8756 disable_breakpoints_before_startup (void)
8757 {
8758 current_program_space->executing_startup = 1;
8759 update_global_location_list (UGLL_DONT_INSERT);
8760 }
8761
8762 void
8763 enable_breakpoints_after_startup (void)
8764 {
8765 current_program_space->executing_startup = 0;
8766 breakpoint_re_set ();
8767 }
8768
8769 /* Create a new single-step breakpoint for thread THREAD, with no
8770 locations. */
8771
8772 static struct breakpoint *
8773 new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8774 {
8775 struct breakpoint *b = XNEW (struct breakpoint);
8776
8777 init_raw_breakpoint_without_location (b, gdbarch, bp_single_step,
8778 &momentary_breakpoint_ops);
8779
8780 b->disposition = disp_donttouch;
8781 b->frame_id = null_frame_id;
8782
8783 b->thread = thread;
8784 gdb_assert (b->thread != 0);
8785
8786 add_to_breakpoint_chain (b);
8787
8788 return b;
8789 }
8790
8791 /* Set a momentary breakpoint of type TYPE at address specified by
8792 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8793 frame. */
8794
8795 struct breakpoint *
8796 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8797 struct frame_id frame_id, enum bptype type)
8798 {
8799 struct breakpoint *b;
8800
8801 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8802 tail-called one. */
8803 gdb_assert (!frame_id_artificial_p (frame_id));
8804
8805 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
8806 b->enable_state = bp_enabled;
8807 b->disposition = disp_donttouch;
8808 b->frame_id = frame_id;
8809
8810 /* If we're debugging a multi-threaded program, then we want
8811 momentary breakpoints to be active in only a single thread of
8812 control. */
8813 if (in_thread_list (inferior_ptid))
8814 b->thread = pid_to_thread_id (inferior_ptid);
8815
8816 update_global_location_list_nothrow (UGLL_MAY_INSERT);
8817
8818 return b;
8819 }
8820
8821 /* Make a momentary breakpoint based on the master breakpoint ORIG.
8822 The new breakpoint will have type TYPE, use OPS as its
8823 breakpoint_ops, and will set enabled to LOC_ENABLED. */
8824
8825 static struct breakpoint *
8826 momentary_breakpoint_from_master (struct breakpoint *orig,
8827 enum bptype type,
8828 const struct breakpoint_ops *ops,
8829 int loc_enabled)
8830 {
8831 struct breakpoint *copy;
8832
8833 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
8834 copy->loc = allocate_bp_location (copy);
8835 set_breakpoint_location_function (copy->loc, 1);
8836
8837 copy->loc->gdbarch = orig->loc->gdbarch;
8838 copy->loc->requested_address = orig->loc->requested_address;
8839 copy->loc->address = orig->loc->address;
8840 copy->loc->section = orig->loc->section;
8841 copy->loc->pspace = orig->loc->pspace;
8842 copy->loc->probe = orig->loc->probe;
8843 copy->loc->line_number = orig->loc->line_number;
8844 copy->loc->symtab = orig->loc->symtab;
8845 copy->loc->enabled = loc_enabled;
8846 copy->frame_id = orig->frame_id;
8847 copy->thread = orig->thread;
8848 copy->pspace = orig->pspace;
8849
8850 copy->enable_state = bp_enabled;
8851 copy->disposition = disp_donttouch;
8852 copy->number = internal_breakpoint_number--;
8853
8854 update_global_location_list_nothrow (UGLL_DONT_INSERT);
8855 return copy;
8856 }
8857
8858 /* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8859 ORIG is NULL. */
8860
8861 struct breakpoint *
8862 clone_momentary_breakpoint (struct breakpoint *orig)
8863 {
8864 /* If there's nothing to clone, then return nothing. */
8865 if (orig == NULL)
8866 return NULL;
8867
8868 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
8869 }
8870
8871 struct breakpoint *
8872 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8873 enum bptype type)
8874 {
8875 struct symtab_and_line sal;
8876
8877 sal = find_pc_line (pc, 0);
8878 sal.pc = pc;
8879 sal.section = find_pc_overlay (pc);
8880 sal.explicit_pc = 1;
8881
8882 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
8883 }
8884 \f
8885
8886 /* Tell the user we have just set a breakpoint B. */
8887
8888 static void
8889 mention (struct breakpoint *b)
8890 {
8891 b->ops->print_mention (b);
8892 if (ui_out_is_mi_like_p (current_uiout))
8893 return;
8894 printf_filtered ("\n");
8895 }
8896 \f
8897
8898 static int bp_loc_is_permanent (struct bp_location *loc);
8899
8900 static struct bp_location *
8901 add_location_to_breakpoint (struct breakpoint *b,
8902 const struct symtab_and_line *sal)
8903 {
8904 struct bp_location *loc, **tmp;
8905 CORE_ADDR adjusted_address;
8906 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8907
8908 if (loc_gdbarch == NULL)
8909 loc_gdbarch = b->gdbarch;
8910
8911 /* Adjust the breakpoint's address prior to allocating a location.
8912 Once we call allocate_bp_location(), that mostly uninitialized
8913 location will be placed on the location chain. Adjustment of the
8914 breakpoint may cause target_read_memory() to be called and we do
8915 not want its scan of the location chain to find a breakpoint and
8916 location that's only been partially initialized. */
8917 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8918 sal->pc, b->type);
8919
8920 /* Sort the locations by their ADDRESS. */
8921 loc = allocate_bp_location (b);
8922 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8923 tmp = &((*tmp)->next))
8924 ;
8925 loc->next = *tmp;
8926 *tmp = loc;
8927
8928 loc->requested_address = sal->pc;
8929 loc->address = adjusted_address;
8930 loc->pspace = sal->pspace;
8931 loc->probe.probe = sal->probe;
8932 loc->probe.objfile = sal->objfile;
8933 gdb_assert (loc->pspace != NULL);
8934 loc->section = sal->section;
8935 loc->gdbarch = loc_gdbarch;
8936 loc->line_number = sal->line;
8937 loc->symtab = sal->symtab;
8938
8939 set_breakpoint_location_function (loc,
8940 sal->explicit_pc || sal->explicit_line);
8941
8942 /* While by definition, permanent breakpoints are already present in the
8943 code, we don't mark the location as inserted. Normally one would expect
8944 that GDB could rely on that breakpoint instruction to stop the program,
8945 thus removing the need to insert its own breakpoint, except that executing
8946 the breakpoint instruction can kill the target instead of reporting a
8947 SIGTRAP. E.g., on SPARC, when interrupts are disabled, executing the
8948 instruction resets the CPU, so QEMU 2.0.0 for SPARC correspondingly dies
8949 with "Trap 0x02 while interrupts disabled, Error state". Letting the
8950 breakpoint be inserted normally results in QEMU knowing about the GDB
8951 breakpoint, and thus trap before the breakpoint instruction is executed.
8952 (If GDB later needs to continue execution past the permanent breakpoint,
8953 it manually increments the PC, thus avoiding executing the breakpoint
8954 instruction.) */
8955 if (bp_loc_is_permanent (loc))
8956 loc->permanent = 1;
8957
8958 return loc;
8959 }
8960 \f
8961
8962 /* See breakpoint.h. */
8963
8964 int
8965 program_breakpoint_here_p (struct gdbarch *gdbarch, CORE_ADDR address)
8966 {
8967 int len;
8968 CORE_ADDR addr;
8969 const gdb_byte *bpoint;
8970 gdb_byte *target_mem;
8971 struct cleanup *cleanup;
8972 int retval = 0;
8973
8974 addr = address;
8975 bpoint = gdbarch_breakpoint_from_pc (gdbarch, &addr, &len);
8976
8977 /* Software breakpoints unsupported? */
8978 if (bpoint == NULL)
8979 return 0;
8980
8981 target_mem = alloca (len);
8982
8983 /* Enable the automatic memory restoration from breakpoints while
8984 we read the memory. Otherwise we could say about our temporary
8985 breakpoints they are permanent. */
8986 cleanup = make_show_memory_breakpoints_cleanup (0);
8987
8988 if (target_read_memory (address, target_mem, len) == 0
8989 && memcmp (target_mem, bpoint, len) == 0)
8990 retval = 1;
8991
8992 do_cleanups (cleanup);
8993
8994 return retval;
8995 }
8996
8997 /* Return 1 if LOC is pointing to a permanent breakpoint,
8998 return 0 otherwise. */
8999
9000 static int
9001 bp_loc_is_permanent (struct bp_location *loc)
9002 {
9003 struct cleanup *cleanup;
9004 int retval;
9005
9006 gdb_assert (loc != NULL);
9007
9008 cleanup = save_current_space_and_thread ();
9009 switch_to_program_space_and_thread (loc->pspace);
9010
9011 retval = program_breakpoint_here_p (loc->gdbarch, loc->address);
9012
9013 do_cleanups (cleanup);
9014
9015 return retval;
9016 }
9017
9018 /* Build a command list for the dprintf corresponding to the current
9019 settings of the dprintf style options. */
9020
9021 static void
9022 update_dprintf_command_list (struct breakpoint *b)
9023 {
9024 char *dprintf_args = b->extra_string;
9025 char *printf_line = NULL;
9026
9027 if (!dprintf_args)
9028 return;
9029
9030 dprintf_args = skip_spaces (dprintf_args);
9031
9032 /* Allow a comma, as it may have terminated a location, but don't
9033 insist on it. */
9034 if (*dprintf_args == ',')
9035 ++dprintf_args;
9036 dprintf_args = skip_spaces (dprintf_args);
9037
9038 if (*dprintf_args != '"')
9039 error (_("Bad format string, missing '\"'."));
9040
9041 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
9042 printf_line = xstrprintf ("printf %s", dprintf_args);
9043 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
9044 {
9045 if (!dprintf_function)
9046 error (_("No function supplied for dprintf call"));
9047
9048 if (dprintf_channel && strlen (dprintf_channel) > 0)
9049 printf_line = xstrprintf ("call (void) %s (%s,%s)",
9050 dprintf_function,
9051 dprintf_channel,
9052 dprintf_args);
9053 else
9054 printf_line = xstrprintf ("call (void) %s (%s)",
9055 dprintf_function,
9056 dprintf_args);
9057 }
9058 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
9059 {
9060 if (target_can_run_breakpoint_commands ())
9061 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
9062 else
9063 {
9064 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
9065 printf_line = xstrprintf ("printf %s", dprintf_args);
9066 }
9067 }
9068 else
9069 internal_error (__FILE__, __LINE__,
9070 _("Invalid dprintf style."));
9071
9072 gdb_assert (printf_line != NULL);
9073 /* Manufacture a printf sequence. */
9074 {
9075 struct command_line *printf_cmd_line
9076 = xmalloc (sizeof (struct command_line));
9077
9078 printf_cmd_line->control_type = simple_control;
9079 printf_cmd_line->body_count = 0;
9080 printf_cmd_line->body_list = NULL;
9081 printf_cmd_line->next = NULL;
9082 printf_cmd_line->line = printf_line;
9083
9084 breakpoint_set_commands (b, printf_cmd_line);
9085 }
9086 }
9087
9088 /* Update all dprintf commands, making their command lists reflect
9089 current style settings. */
9090
9091 static void
9092 update_dprintf_commands (char *args, int from_tty,
9093 struct cmd_list_element *c)
9094 {
9095 struct breakpoint *b;
9096
9097 ALL_BREAKPOINTS (b)
9098 {
9099 if (b->type == bp_dprintf)
9100 update_dprintf_command_list (b);
9101 }
9102 }
9103
9104 /* Create a breakpoint with SAL as location. Use LOCATION
9105 as a description of the location, and COND_STRING
9106 as condition expression. */
9107
9108 static void
9109 init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
9110 struct symtabs_and_lines sals,
9111 struct event_location *location,
9112 char *filter, char *cond_string,
9113 char *extra_string,
9114 enum bptype type, enum bpdisp disposition,
9115 int thread, int task, int ignore_count,
9116 const struct breakpoint_ops *ops, int from_tty,
9117 int enabled, int internal, unsigned flags,
9118 int display_canonical)
9119 {
9120 int i;
9121
9122 if (type == bp_hardware_breakpoint)
9123 {
9124 int target_resources_ok;
9125
9126 i = hw_breakpoint_used_count ();
9127 target_resources_ok =
9128 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9129 i + 1, 0);
9130 if (target_resources_ok == 0)
9131 error (_("No hardware breakpoint support in the target."));
9132 else if (target_resources_ok < 0)
9133 error (_("Hardware breakpoints used exceeds limit."));
9134 }
9135
9136 gdb_assert (sals.nelts > 0);
9137
9138 for (i = 0; i < sals.nelts; ++i)
9139 {
9140 struct symtab_and_line sal = sals.sals[i];
9141 struct bp_location *loc;
9142
9143 if (from_tty)
9144 {
9145 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9146 if (!loc_gdbarch)
9147 loc_gdbarch = gdbarch;
9148
9149 describe_other_breakpoints (loc_gdbarch,
9150 sal.pspace, sal.pc, sal.section, thread);
9151 }
9152
9153 if (i == 0)
9154 {
9155 init_raw_breakpoint (b, gdbarch, sal, type, ops);
9156 b->thread = thread;
9157 b->task = task;
9158
9159 b->cond_string = cond_string;
9160 b->extra_string = extra_string;
9161 b->ignore_count = ignore_count;
9162 b->enable_state = enabled ? bp_enabled : bp_disabled;
9163 b->disposition = disposition;
9164
9165 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9166 b->loc->inserted = 1;
9167
9168 if (type == bp_static_tracepoint)
9169 {
9170 struct tracepoint *t = (struct tracepoint *) b;
9171 struct static_tracepoint_marker marker;
9172
9173 if (strace_marker_p (b))
9174 {
9175 /* We already know the marker exists, otherwise, we
9176 wouldn't see a sal for it. */
9177 const char *p = &event_location_to_string (b->location)[3];
9178 const char *endp;
9179 char *marker_str;
9180
9181 p = skip_spaces_const (p);
9182
9183 endp = skip_to_space_const (p);
9184
9185 marker_str = savestring (p, endp - p);
9186 t->static_trace_marker_id = marker_str;
9187
9188 printf_filtered (_("Probed static tracepoint "
9189 "marker \"%s\"\n"),
9190 t->static_trace_marker_id);
9191 }
9192 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
9193 {
9194 t->static_trace_marker_id = xstrdup (marker.str_id);
9195 release_static_tracepoint_marker (&marker);
9196
9197 printf_filtered (_("Probed static tracepoint "
9198 "marker \"%s\"\n"),
9199 t->static_trace_marker_id);
9200 }
9201 else
9202 warning (_("Couldn't determine the static "
9203 "tracepoint marker to probe"));
9204 }
9205
9206 loc = b->loc;
9207 }
9208 else
9209 {
9210 loc = add_location_to_breakpoint (b, &sal);
9211 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9212 loc->inserted = 1;
9213 }
9214
9215 if (b->cond_string)
9216 {
9217 const char *arg = b->cond_string;
9218
9219 loc->cond = parse_exp_1 (&arg, loc->address,
9220 block_for_pc (loc->address), 0);
9221 if (*arg)
9222 error (_("Garbage '%s' follows condition"), arg);
9223 }
9224
9225 /* Dynamic printf requires and uses additional arguments on the
9226 command line, otherwise it's an error. */
9227 if (type == bp_dprintf)
9228 {
9229 if (b->extra_string)
9230 update_dprintf_command_list (b);
9231 else
9232 error (_("Format string required"));
9233 }
9234 else if (b->extra_string)
9235 error (_("Garbage '%s' at end of command"), b->extra_string);
9236 }
9237
9238 b->display_canonical = display_canonical;
9239 if (location != NULL)
9240 b->location = location;
9241 else
9242 b->location = new_address_location (b->loc->address);
9243 b->filter = filter;
9244 }
9245
9246 static void
9247 create_breakpoint_sal (struct gdbarch *gdbarch,
9248 struct symtabs_and_lines sals,
9249 struct event_location *location,
9250 char *filter, char *cond_string,
9251 char *extra_string,
9252 enum bptype type, enum bpdisp disposition,
9253 int thread, int task, int ignore_count,
9254 const struct breakpoint_ops *ops, int from_tty,
9255 int enabled, int internal, unsigned flags,
9256 int display_canonical)
9257 {
9258 struct breakpoint *b;
9259 struct cleanup *old_chain;
9260
9261 if (is_tracepoint_type (type))
9262 {
9263 struct tracepoint *t;
9264
9265 t = XCNEW (struct tracepoint);
9266 b = &t->base;
9267 }
9268 else
9269 b = XNEW (struct breakpoint);
9270
9271 old_chain = make_cleanup (xfree, b);
9272
9273 init_breakpoint_sal (b, gdbarch,
9274 sals, location,
9275 filter, cond_string, extra_string,
9276 type, disposition,
9277 thread, task, ignore_count,
9278 ops, from_tty,
9279 enabled, internal, flags,
9280 display_canonical);
9281 discard_cleanups (old_chain);
9282
9283 install_breakpoint (internal, b, 0);
9284 }
9285
9286 /* Add SALS.nelts breakpoints to the breakpoint table. For each
9287 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9288 value. COND_STRING, if not NULL, specified the condition to be
9289 used for all breakpoints. Essentially the only case where
9290 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9291 function. In that case, it's still not possible to specify
9292 separate conditions for different overloaded functions, so
9293 we take just a single condition string.
9294
9295 NOTE: If the function succeeds, the caller is expected to cleanup
9296 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
9297 array contents). If the function fails (error() is called), the
9298 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
9299 COND and SALS arrays and each of those arrays contents. */
9300
9301 static void
9302 create_breakpoints_sal (struct gdbarch *gdbarch,
9303 struct linespec_result *canonical,
9304 char *cond_string, char *extra_string,
9305 enum bptype type, enum bpdisp disposition,
9306 int thread, int task, int ignore_count,
9307 const struct breakpoint_ops *ops, int from_tty,
9308 int enabled, int internal, unsigned flags)
9309 {
9310 int i;
9311 struct linespec_sals *lsal;
9312
9313 if (canonical->pre_expanded)
9314 gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
9315
9316 for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
9317 {
9318 /* Note that 'location' can be NULL in the case of a plain
9319 'break', without arguments. */
9320 struct event_location *location
9321 = (canonical->location != NULL
9322 ? copy_event_location (canonical->location) : NULL);
9323 char *filter_string = lsal->canonical ? xstrdup (lsal->canonical) : NULL;
9324 struct cleanup *inner = make_cleanup_delete_event_location (location);
9325
9326 make_cleanup (xfree, filter_string);
9327 create_breakpoint_sal (gdbarch, lsal->sals,
9328 location,
9329 filter_string,
9330 cond_string, extra_string,
9331 type, disposition,
9332 thread, task, ignore_count, ops,
9333 from_tty, enabled, internal, flags,
9334 canonical->special_display);
9335 discard_cleanups (inner);
9336 }
9337 }
9338
9339 /* Parse LOCATION which is assumed to be a SAL specification possibly
9340 followed by conditionals. On return, SALS contains an array of SAL
9341 addresses found. LOCATION points to the end of the SAL (for
9342 linespec locations).
9343
9344 The array and the line spec strings are allocated on the heap, it is
9345 the caller's responsibility to free them. */
9346
9347 static void
9348 parse_breakpoint_sals (const struct event_location *location,
9349 struct linespec_result *canonical)
9350 {
9351 struct symtab_and_line cursal;
9352
9353 if (event_location_type (location) == LINESPEC_LOCATION)
9354 {
9355 const char *address = get_linespec_location (location);
9356
9357 if (address == NULL)
9358 {
9359 /* The last displayed codepoint, if it's valid, is our default
9360 breakpoint address. */
9361 if (last_displayed_sal_is_valid ())
9362 {
9363 struct linespec_sals lsal;
9364 struct symtab_and_line sal;
9365 CORE_ADDR pc;
9366
9367 init_sal (&sal); /* Initialize to zeroes. */
9368 lsal.sals.sals = (struct symtab_and_line *)
9369 xmalloc (sizeof (struct symtab_and_line));
9370
9371 /* Set sal's pspace, pc, symtab, and line to the values
9372 corresponding to the last call to print_frame_info.
9373 Be sure to reinitialize LINE with NOTCURRENT == 0
9374 as the breakpoint line number is inappropriate otherwise.
9375 find_pc_line would adjust PC, re-set it back. */
9376 get_last_displayed_sal (&sal);
9377 pc = sal.pc;
9378 sal = find_pc_line (pc, 0);
9379
9380 /* "break" without arguments is equivalent to "break *PC"
9381 where PC is the last displayed codepoint's address. So
9382 make sure to set sal.explicit_pc to prevent GDB from
9383 trying to expand the list of sals to include all other
9384 instances with the same symtab and line. */
9385 sal.pc = pc;
9386 sal.explicit_pc = 1;
9387
9388 lsal.sals.sals[0] = sal;
9389 lsal.sals.nelts = 1;
9390 lsal.canonical = NULL;
9391
9392 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
9393 return;
9394 }
9395 else
9396 error (_("No default breakpoint address now."));
9397 }
9398 }
9399
9400 /* Force almost all breakpoints to be in terms of the
9401 current_source_symtab (which is decode_line_1's default).
9402 This should produce the results we want almost all of the
9403 time while leaving default_breakpoint_* alone.
9404
9405 ObjC: However, don't match an Objective-C method name which
9406 may have a '+' or '-' succeeded by a '['. */
9407 cursal = get_current_source_symtab_and_line ();
9408 if (last_displayed_sal_is_valid ())
9409 {
9410 const char *address = NULL;
9411
9412 if (event_location_type (location) == LINESPEC_LOCATION)
9413 address = get_linespec_location (location);
9414
9415 if (!cursal.symtab
9416 || (address != NULL
9417 && strchr ("+-", address[0]) != NULL
9418 && address[1] != '['))
9419 {
9420 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE,
9421 get_last_displayed_symtab (),
9422 get_last_displayed_line (),
9423 canonical, NULL, NULL);
9424 return;
9425 }
9426 }
9427
9428 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE,
9429 cursal.symtab, cursal.line, canonical, NULL, NULL);
9430 }
9431
9432
9433 /* Convert each SAL into a real PC. Verify that the PC can be
9434 inserted as a breakpoint. If it can't throw an error. */
9435
9436 static void
9437 breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
9438 {
9439 int i;
9440
9441 for (i = 0; i < sals->nelts; i++)
9442 resolve_sal_pc (&sals->sals[i]);
9443 }
9444
9445 /* Fast tracepoints may have restrictions on valid locations. For
9446 instance, a fast tracepoint using a jump instead of a trap will
9447 likely have to overwrite more bytes than a trap would, and so can
9448 only be placed where the instruction is longer than the jump, or a
9449 multi-instruction sequence does not have a jump into the middle of
9450 it, etc. */
9451
9452 static void
9453 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9454 struct symtabs_and_lines *sals)
9455 {
9456 int i, rslt;
9457 struct symtab_and_line *sal;
9458 char *msg;
9459 struct cleanup *old_chain;
9460
9461 for (i = 0; i < sals->nelts; i++)
9462 {
9463 struct gdbarch *sarch;
9464
9465 sal = &sals->sals[i];
9466
9467 sarch = get_sal_arch (*sal);
9468 /* We fall back to GDBARCH if there is no architecture
9469 associated with SAL. */
9470 if (sarch == NULL)
9471 sarch = gdbarch;
9472 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc, &msg);
9473 old_chain = make_cleanup (xfree, msg);
9474
9475 if (!rslt)
9476 error (_("May not have a fast tracepoint at 0x%s%s"),
9477 paddress (sarch, sal->pc), (msg ? msg : ""));
9478
9479 do_cleanups (old_chain);
9480 }
9481 }
9482
9483 /* Issue an invalid thread ID error. */
9484
9485 static void ATTRIBUTE_NORETURN
9486 invalid_thread_id_error (int id)
9487 {
9488 error (_("Unknown thread %d."), id);
9489 }
9490
9491 /* Given TOK, a string specification of condition and thread, as
9492 accepted by the 'break' command, extract the condition
9493 string and thread number and set *COND_STRING and *THREAD.
9494 PC identifies the context at which the condition should be parsed.
9495 If no condition is found, *COND_STRING is set to NULL.
9496 If no thread is found, *THREAD is set to -1. */
9497
9498 static void
9499 find_condition_and_thread (const char *tok, CORE_ADDR pc,
9500 char **cond_string, int *thread, int *task,
9501 char **rest)
9502 {
9503 *cond_string = NULL;
9504 *thread = -1;
9505 *task = 0;
9506 *rest = NULL;
9507
9508 while (tok && *tok)
9509 {
9510 const char *end_tok;
9511 int toklen;
9512 const char *cond_start = NULL;
9513 const char *cond_end = NULL;
9514
9515 tok = skip_spaces_const (tok);
9516
9517 if ((*tok == '"' || *tok == ',') && rest)
9518 {
9519 *rest = savestring (tok, strlen (tok));
9520 return;
9521 }
9522
9523 end_tok = skip_to_space_const (tok);
9524
9525 toklen = end_tok - tok;
9526
9527 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9528 {
9529 struct expression *expr;
9530
9531 tok = cond_start = end_tok + 1;
9532 expr = parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
9533 xfree (expr);
9534 cond_end = tok;
9535 *cond_string = savestring (cond_start, cond_end - cond_start);
9536 }
9537 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9538 {
9539 char *tmptok;
9540
9541 tok = end_tok + 1;
9542 *thread = strtol (tok, &tmptok, 0);
9543 if (tok == tmptok)
9544 error (_("Junk after thread keyword."));
9545 if (!valid_thread_id (*thread))
9546 invalid_thread_id_error (*thread);
9547 tok = tmptok;
9548 }
9549 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9550 {
9551 char *tmptok;
9552
9553 tok = end_tok + 1;
9554 *task = strtol (tok, &tmptok, 0);
9555 if (tok == tmptok)
9556 error (_("Junk after task keyword."));
9557 if (!valid_task_id (*task))
9558 error (_("Unknown task %d."), *task);
9559 tok = tmptok;
9560 }
9561 else if (rest)
9562 {
9563 *rest = savestring (tok, strlen (tok));
9564 return;
9565 }
9566 else
9567 error (_("Junk at end of arguments."));
9568 }
9569 }
9570
9571 /* Decode a static tracepoint marker spec. */
9572
9573 static struct symtabs_and_lines
9574 decode_static_tracepoint_spec (const char **arg_p)
9575 {
9576 VEC(static_tracepoint_marker_p) *markers = NULL;
9577 struct symtabs_and_lines sals;
9578 struct cleanup *old_chain;
9579 const char *p = &(*arg_p)[3];
9580 const char *endp;
9581 char *marker_str;
9582 int i;
9583
9584 p = skip_spaces_const (p);
9585
9586 endp = skip_to_space_const (p);
9587
9588 marker_str = savestring (p, endp - p);
9589 old_chain = make_cleanup (xfree, marker_str);
9590
9591 markers = target_static_tracepoint_markers_by_strid (marker_str);
9592 if (VEC_empty(static_tracepoint_marker_p, markers))
9593 error (_("No known static tracepoint marker named %s"), marker_str);
9594
9595 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
9596 sals.sals = xmalloc (sizeof *sals.sals * sals.nelts);
9597
9598 for (i = 0; i < sals.nelts; i++)
9599 {
9600 struct static_tracepoint_marker *marker;
9601
9602 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9603
9604 init_sal (&sals.sals[i]);
9605
9606 sals.sals[i] = find_pc_line (marker->address, 0);
9607 sals.sals[i].pc = marker->address;
9608
9609 release_static_tracepoint_marker (marker);
9610 }
9611
9612 do_cleanups (old_chain);
9613
9614 *arg_p = endp;
9615 return sals;
9616 }
9617
9618 /* See breakpoint.h. */
9619
9620 int
9621 create_breakpoint (struct gdbarch *gdbarch,
9622 const struct event_location *location, char *cond_string,
9623 int thread, char *extra_string,
9624 int parse_extra,
9625 int tempflag, enum bptype type_wanted,
9626 int ignore_count,
9627 enum auto_boolean pending_break_support,
9628 const struct breakpoint_ops *ops,
9629 int from_tty, int enabled, int internal,
9630 unsigned flags)
9631 {
9632 struct linespec_result canonical;
9633 struct cleanup *old_chain;
9634 struct cleanup *bkpt_chain = NULL;
9635 int pending = 0;
9636 int task = 0;
9637 int prev_bkpt_count = breakpoint_count;
9638
9639 gdb_assert (ops != NULL);
9640
9641 /* If extra_string isn't useful, set it to NULL. */
9642 if (extra_string != NULL && *extra_string == '\0')
9643 extra_string = NULL;
9644
9645 init_linespec_result (&canonical);
9646
9647 TRY
9648 {
9649 ops->create_sals_from_location (location, &canonical, type_wanted);
9650 }
9651 CATCH (e, RETURN_MASK_ERROR)
9652 {
9653 /* If caller is interested in rc value from parse, set
9654 value. */
9655 if (e.error == NOT_FOUND_ERROR)
9656 {
9657 /* If pending breakpoint support is turned off, throw
9658 error. */
9659
9660 if (pending_break_support == AUTO_BOOLEAN_FALSE)
9661 throw_exception (e);
9662
9663 exception_print (gdb_stderr, e);
9664
9665 /* If pending breakpoint support is auto query and the user
9666 selects no, then simply return the error code. */
9667 if (pending_break_support == AUTO_BOOLEAN_AUTO
9668 && !nquery (_("Make %s pending on future shared library load? "),
9669 bptype_string (type_wanted)))
9670 return 0;
9671
9672 /* At this point, either the user was queried about setting
9673 a pending breakpoint and selected yes, or pending
9674 breakpoint behavior is on and thus a pending breakpoint
9675 is defaulted on behalf of the user. */
9676 pending = 1;
9677 }
9678 else
9679 throw_exception (e);
9680 }
9681 END_CATCH
9682
9683 if (!pending && VEC_empty (linespec_sals, canonical.sals))
9684 return 0;
9685
9686 /* Create a chain of things that always need to be cleaned up. */
9687 old_chain = make_cleanup_destroy_linespec_result (&canonical);
9688
9689 /* ----------------------------- SNIP -----------------------------
9690 Anything added to the cleanup chain beyond this point is assumed
9691 to be part of a breakpoint. If the breakpoint create succeeds
9692 then the memory is not reclaimed. */
9693 bkpt_chain = make_cleanup (null_cleanup, 0);
9694
9695 /* Resolve all line numbers to PC's and verify that the addresses
9696 are ok for the target. */
9697 if (!pending)
9698 {
9699 int ix;
9700 struct linespec_sals *iter;
9701
9702 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9703 breakpoint_sals_to_pc (&iter->sals);
9704 }
9705
9706 /* Fast tracepoints may have additional restrictions on location. */
9707 if (!pending && type_wanted == bp_fast_tracepoint)
9708 {
9709 int ix;
9710 struct linespec_sals *iter;
9711
9712 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9713 check_fast_tracepoint_sals (gdbarch, &iter->sals);
9714 }
9715
9716 /* Verify that condition can be parsed, before setting any
9717 breakpoints. Allocate a separate condition expression for each
9718 breakpoint. */
9719 if (!pending)
9720 {
9721 if (parse_extra)
9722 {
9723 char *rest;
9724 struct linespec_sals *lsal;
9725
9726 lsal = VEC_index (linespec_sals, canonical.sals, 0);
9727
9728 /* Here we only parse 'arg' to separate condition
9729 from thread number, so parsing in context of first
9730 sal is OK. When setting the breakpoint we'll
9731 re-parse it in context of each sal. */
9732
9733 find_condition_and_thread (extra_string, lsal->sals.sals[0].pc,
9734 &cond_string, &thread, &task, &rest);
9735 if (cond_string)
9736 make_cleanup (xfree, cond_string);
9737 if (rest)
9738 make_cleanup (xfree, rest);
9739 if (rest)
9740 extra_string = rest;
9741 else
9742 extra_string = NULL;
9743 }
9744 else
9745 {
9746 if (type_wanted != bp_dprintf
9747 && extra_string != NULL && *extra_string != '\0')
9748 error (_("Garbage '%s' at end of location"), extra_string);
9749
9750 /* Create a private copy of condition string. */
9751 if (cond_string)
9752 {
9753 cond_string = xstrdup (cond_string);
9754 make_cleanup (xfree, cond_string);
9755 }
9756 /* Create a private copy of any extra string. */
9757 if (extra_string)
9758 {
9759 extra_string = xstrdup (extra_string);
9760 make_cleanup (xfree, extra_string);
9761 }
9762 }
9763
9764 ops->create_breakpoints_sal (gdbarch, &canonical,
9765 cond_string, extra_string, type_wanted,
9766 tempflag ? disp_del : disp_donttouch,
9767 thread, task, ignore_count, ops,
9768 from_tty, enabled, internal, flags);
9769 }
9770 else
9771 {
9772 struct breakpoint *b;
9773
9774 if (is_tracepoint_type (type_wanted))
9775 {
9776 struct tracepoint *t;
9777
9778 t = XCNEW (struct tracepoint);
9779 b = &t->base;
9780 }
9781 else
9782 b = XNEW (struct breakpoint);
9783
9784 init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops);
9785 b->location = copy_event_location (location);
9786
9787 if (parse_extra)
9788 b->cond_string = NULL;
9789 else
9790 {
9791 /* Create a private copy of condition string. */
9792 if (cond_string)
9793 {
9794 cond_string = xstrdup (cond_string);
9795 make_cleanup (xfree, cond_string);
9796 }
9797 b->cond_string = cond_string;
9798 b->thread = thread;
9799 }
9800
9801 /* Create a private copy of any extra string. */
9802 if (extra_string != NULL)
9803 {
9804 extra_string = xstrdup (extra_string);
9805 make_cleanup (xfree, extra_string);
9806 }
9807 b->extra_string = extra_string;
9808 b->ignore_count = ignore_count;
9809 b->disposition = tempflag ? disp_del : disp_donttouch;
9810 b->condition_not_parsed = 1;
9811 b->enable_state = enabled ? bp_enabled : bp_disabled;
9812 if ((type_wanted != bp_breakpoint
9813 && type_wanted != bp_hardware_breakpoint) || thread != -1)
9814 b->pspace = current_program_space;
9815
9816 install_breakpoint (internal, b, 0);
9817 }
9818
9819 if (VEC_length (linespec_sals, canonical.sals) > 1)
9820 {
9821 warning (_("Multiple breakpoints were set.\nUse the "
9822 "\"delete\" command to delete unwanted breakpoints."));
9823 prev_breakpoint_count = prev_bkpt_count;
9824 }
9825
9826 /* That's it. Discard the cleanups for data inserted into the
9827 breakpoint. */
9828 discard_cleanups (bkpt_chain);
9829 /* But cleanup everything else. */
9830 do_cleanups (old_chain);
9831
9832 /* error call may happen here - have BKPT_CHAIN already discarded. */
9833 update_global_location_list (UGLL_MAY_INSERT);
9834
9835 return 1;
9836 }
9837
9838 /* Set a breakpoint.
9839 ARG is a string describing breakpoint address,
9840 condition, and thread.
9841 FLAG specifies if a breakpoint is hardware on,
9842 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9843 and BP_TEMPFLAG. */
9844
9845 static void
9846 break_command_1 (char *arg, int flag, int from_tty)
9847 {
9848 int tempflag = flag & BP_TEMPFLAG;
9849 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9850 ? bp_hardware_breakpoint
9851 : bp_breakpoint);
9852 struct breakpoint_ops *ops;
9853 const char *arg_cp = arg;
9854 struct event_location *location;
9855 struct cleanup *cleanup;
9856
9857 location = string_to_event_location (&arg, current_language);
9858 cleanup = make_cleanup_delete_event_location (location);
9859
9860 /* Matching breakpoints on probes. */
9861 if (arg_cp != NULL && probe_linespec_to_ops (&arg_cp) != NULL)
9862 ops = &bkpt_probe_breakpoint_ops;
9863 else
9864 ops = &bkpt_breakpoint_ops;
9865
9866 create_breakpoint (get_current_arch (),
9867 location,
9868 NULL, 0, arg, 1 /* parse arg */,
9869 tempflag, type_wanted,
9870 0 /* Ignore count */,
9871 pending_break_support,
9872 ops,
9873 from_tty,
9874 1 /* enabled */,
9875 0 /* internal */,
9876 0);
9877 do_cleanups (cleanup);
9878 }
9879
9880 /* Helper function for break_command_1 and disassemble_command. */
9881
9882 void
9883 resolve_sal_pc (struct symtab_and_line *sal)
9884 {
9885 CORE_ADDR pc;
9886
9887 if (sal->pc == 0 && sal->symtab != NULL)
9888 {
9889 if (!find_line_pc (sal->symtab, sal->line, &pc))
9890 error (_("No line %d in file \"%s\"."),
9891 sal->line, symtab_to_filename_for_display (sal->symtab));
9892 sal->pc = pc;
9893
9894 /* If this SAL corresponds to a breakpoint inserted using a line
9895 number, then skip the function prologue if necessary. */
9896 if (sal->explicit_line)
9897 skip_prologue_sal (sal);
9898 }
9899
9900 if (sal->section == 0 && sal->symtab != NULL)
9901 {
9902 const struct blockvector *bv;
9903 const struct block *b;
9904 struct symbol *sym;
9905
9906 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9907 SYMTAB_COMPUNIT (sal->symtab));
9908 if (bv != NULL)
9909 {
9910 sym = block_linkage_function (b);
9911 if (sym != NULL)
9912 {
9913 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
9914 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
9915 sym);
9916 }
9917 else
9918 {
9919 /* It really is worthwhile to have the section, so we'll
9920 just have to look harder. This case can be executed
9921 if we have line numbers but no functions (as can
9922 happen in assembly source). */
9923
9924 struct bound_minimal_symbol msym;
9925 struct cleanup *old_chain = save_current_space_and_thread ();
9926
9927 switch_to_program_space_and_thread (sal->pspace);
9928
9929 msym = lookup_minimal_symbol_by_pc (sal->pc);
9930 if (msym.minsym)
9931 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
9932
9933 do_cleanups (old_chain);
9934 }
9935 }
9936 }
9937 }
9938
9939 void
9940 break_command (char *arg, int from_tty)
9941 {
9942 break_command_1 (arg, 0, from_tty);
9943 }
9944
9945 void
9946 tbreak_command (char *arg, int from_tty)
9947 {
9948 break_command_1 (arg, BP_TEMPFLAG, from_tty);
9949 }
9950
9951 static void
9952 hbreak_command (char *arg, int from_tty)
9953 {
9954 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
9955 }
9956
9957 static void
9958 thbreak_command (char *arg, int from_tty)
9959 {
9960 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
9961 }
9962
9963 static void
9964 stop_command (char *arg, int from_tty)
9965 {
9966 printf_filtered (_("Specify the type of breakpoint to set.\n\
9967 Usage: stop in <function | address>\n\
9968 stop at <line>\n"));
9969 }
9970
9971 static void
9972 stopin_command (char *arg, int from_tty)
9973 {
9974 int badInput = 0;
9975
9976 if (arg == (char *) NULL)
9977 badInput = 1;
9978 else if (*arg != '*')
9979 {
9980 char *argptr = arg;
9981 int hasColon = 0;
9982
9983 /* Look for a ':'. If this is a line number specification, then
9984 say it is bad, otherwise, it should be an address or
9985 function/method name. */
9986 while (*argptr && !hasColon)
9987 {
9988 hasColon = (*argptr == ':');
9989 argptr++;
9990 }
9991
9992 if (hasColon)
9993 badInput = (*argptr != ':'); /* Not a class::method */
9994 else
9995 badInput = isdigit (*arg); /* a simple line number */
9996 }
9997
9998 if (badInput)
9999 printf_filtered (_("Usage: stop in <function | address>\n"));
10000 else
10001 break_command_1 (arg, 0, from_tty);
10002 }
10003
10004 static void
10005 stopat_command (char *arg, int from_tty)
10006 {
10007 int badInput = 0;
10008
10009 if (arg == (char *) NULL || *arg == '*') /* no line number */
10010 badInput = 1;
10011 else
10012 {
10013 char *argptr = arg;
10014 int hasColon = 0;
10015
10016 /* Look for a ':'. If there is a '::' then get out, otherwise
10017 it is probably a line number. */
10018 while (*argptr && !hasColon)
10019 {
10020 hasColon = (*argptr == ':');
10021 argptr++;
10022 }
10023
10024 if (hasColon)
10025 badInput = (*argptr == ':'); /* we have class::method */
10026 else
10027 badInput = !isdigit (*arg); /* not a line number */
10028 }
10029
10030 if (badInput)
10031 printf_filtered (_("Usage: stop at <line>\n"));
10032 else
10033 break_command_1 (arg, 0, from_tty);
10034 }
10035
10036 /* The dynamic printf command is mostly like a regular breakpoint, but
10037 with a prewired command list consisting of a single output command,
10038 built from extra arguments supplied on the dprintf command
10039 line. */
10040
10041 static void
10042 dprintf_command (char *arg, int from_tty)
10043 {
10044 struct event_location *location;
10045 struct cleanup *cleanup;
10046
10047 location = string_to_event_location (&arg, current_language);
10048 cleanup = make_cleanup_delete_event_location (location);
10049
10050 /* If non-NULL, ARG should have been advanced past the location;
10051 the next character must be ','. */
10052 if (arg != NULL)
10053 {
10054 if (arg[0] != ',' || arg[1] == '\0')
10055 error (_("Format string required"));
10056 else
10057 {
10058 /* Skip the comma. */
10059 ++arg;
10060 }
10061 }
10062
10063 create_breakpoint (get_current_arch (),
10064 location,
10065 NULL, 0, arg, 1 /* parse arg */,
10066 0, bp_dprintf,
10067 0 /* Ignore count */,
10068 pending_break_support,
10069 &dprintf_breakpoint_ops,
10070 from_tty,
10071 1 /* enabled */,
10072 0 /* internal */,
10073 0);
10074 do_cleanups (cleanup);
10075 }
10076
10077 static void
10078 agent_printf_command (char *arg, int from_tty)
10079 {
10080 error (_("May only run agent-printf on the target"));
10081 }
10082
10083 /* Implement the "breakpoint_hit" breakpoint_ops method for
10084 ranged breakpoints. */
10085
10086 static int
10087 breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
10088 struct address_space *aspace,
10089 CORE_ADDR bp_addr,
10090 const struct target_waitstatus *ws)
10091 {
10092 if (ws->kind != TARGET_WAITKIND_STOPPED
10093 || ws->value.sig != GDB_SIGNAL_TRAP)
10094 return 0;
10095
10096 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
10097 bl->length, aspace, bp_addr);
10098 }
10099
10100 /* Implement the "resources_needed" breakpoint_ops method for
10101 ranged breakpoints. */
10102
10103 static int
10104 resources_needed_ranged_breakpoint (const struct bp_location *bl)
10105 {
10106 return target_ranged_break_num_registers ();
10107 }
10108
10109 /* Implement the "print_it" breakpoint_ops method for
10110 ranged breakpoints. */
10111
10112 static enum print_stop_action
10113 print_it_ranged_breakpoint (bpstat bs)
10114 {
10115 struct breakpoint *b = bs->breakpoint_at;
10116 struct bp_location *bl = b->loc;
10117 struct ui_out *uiout = current_uiout;
10118
10119 gdb_assert (b->type == bp_hardware_breakpoint);
10120
10121 /* Ranged breakpoints have only one location. */
10122 gdb_assert (bl && bl->next == NULL);
10123
10124 annotate_breakpoint (b->number);
10125 if (b->disposition == disp_del)
10126 ui_out_text (uiout, "\nTemporary ranged breakpoint ");
10127 else
10128 ui_out_text (uiout, "\nRanged breakpoint ");
10129 if (ui_out_is_mi_like_p (uiout))
10130 {
10131 ui_out_field_string (uiout, "reason",
10132 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
10133 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
10134 }
10135 ui_out_field_int (uiout, "bkptno", b->number);
10136 ui_out_text (uiout, ", ");
10137
10138 return PRINT_SRC_AND_LOC;
10139 }
10140
10141 /* Implement the "print_one" breakpoint_ops method for
10142 ranged breakpoints. */
10143
10144 static void
10145 print_one_ranged_breakpoint (struct breakpoint *b,
10146 struct bp_location **last_loc)
10147 {
10148 struct bp_location *bl = b->loc;
10149 struct value_print_options opts;
10150 struct ui_out *uiout = current_uiout;
10151
10152 /* Ranged breakpoints have only one location. */
10153 gdb_assert (bl && bl->next == NULL);
10154
10155 get_user_print_options (&opts);
10156
10157 if (opts.addressprint)
10158 /* We don't print the address range here, it will be printed later
10159 by print_one_detail_ranged_breakpoint. */
10160 ui_out_field_skip (uiout, "addr");
10161 annotate_field (5);
10162 print_breakpoint_location (b, bl);
10163 *last_loc = bl;
10164 }
10165
10166 /* Implement the "print_one_detail" breakpoint_ops method for
10167 ranged breakpoints. */
10168
10169 static void
10170 print_one_detail_ranged_breakpoint (const struct breakpoint *b,
10171 struct ui_out *uiout)
10172 {
10173 CORE_ADDR address_start, address_end;
10174 struct bp_location *bl = b->loc;
10175 struct ui_file *stb = mem_fileopen ();
10176 struct cleanup *cleanup = make_cleanup_ui_file_delete (stb);
10177
10178 gdb_assert (bl);
10179
10180 address_start = bl->address;
10181 address_end = address_start + bl->length - 1;
10182
10183 ui_out_text (uiout, "\taddress range: ");
10184 fprintf_unfiltered (stb, "[%s, %s]",
10185 print_core_address (bl->gdbarch, address_start),
10186 print_core_address (bl->gdbarch, address_end));
10187 ui_out_field_stream (uiout, "addr", stb);
10188 ui_out_text (uiout, "\n");
10189
10190 do_cleanups (cleanup);
10191 }
10192
10193 /* Implement the "print_mention" breakpoint_ops method for
10194 ranged breakpoints. */
10195
10196 static void
10197 print_mention_ranged_breakpoint (struct breakpoint *b)
10198 {
10199 struct bp_location *bl = b->loc;
10200 struct ui_out *uiout = current_uiout;
10201
10202 gdb_assert (bl);
10203 gdb_assert (b->type == bp_hardware_breakpoint);
10204
10205 if (ui_out_is_mi_like_p (uiout))
10206 return;
10207
10208 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
10209 b->number, paddress (bl->gdbarch, bl->address),
10210 paddress (bl->gdbarch, bl->address + bl->length - 1));
10211 }
10212
10213 /* Implement the "print_recreate" breakpoint_ops method for
10214 ranged breakpoints. */
10215
10216 static void
10217 print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
10218 {
10219 fprintf_unfiltered (fp, "break-range %s, %s",
10220 event_location_to_string (b->location),
10221 event_location_to_string (b->location_range_end));
10222 print_recreate_thread (b, fp);
10223 }
10224
10225 /* The breakpoint_ops structure to be used in ranged breakpoints. */
10226
10227 static struct breakpoint_ops ranged_breakpoint_ops;
10228
10229 /* Find the address where the end of the breakpoint range should be
10230 placed, given the SAL of the end of the range. This is so that if
10231 the user provides a line number, the end of the range is set to the
10232 last instruction of the given line. */
10233
10234 static CORE_ADDR
10235 find_breakpoint_range_end (struct symtab_and_line sal)
10236 {
10237 CORE_ADDR end;
10238
10239 /* If the user provided a PC value, use it. Otherwise,
10240 find the address of the end of the given location. */
10241 if (sal.explicit_pc)
10242 end = sal.pc;
10243 else
10244 {
10245 int ret;
10246 CORE_ADDR start;
10247
10248 ret = find_line_pc_range (sal, &start, &end);
10249 if (!ret)
10250 error (_("Could not find location of the end of the range."));
10251
10252 /* find_line_pc_range returns the start of the next line. */
10253 end--;
10254 }
10255
10256 return end;
10257 }
10258
10259 /* Implement the "break-range" CLI command. */
10260
10261 static void
10262 break_range_command (char *arg, int from_tty)
10263 {
10264 char *arg_start, *addr_string_start, *addr_string_end;
10265 struct linespec_result canonical_start, canonical_end;
10266 int bp_count, can_use_bp, length;
10267 CORE_ADDR end;
10268 struct breakpoint *b;
10269 struct symtab_and_line sal_start, sal_end;
10270 struct cleanup *cleanup_bkpt;
10271 struct linespec_sals *lsal_start, *lsal_end;
10272 struct event_location *start_location, *end_location;
10273
10274 /* We don't support software ranged breakpoints. */
10275 if (target_ranged_break_num_registers () < 0)
10276 error (_("This target does not support hardware ranged breakpoints."));
10277
10278 bp_count = hw_breakpoint_used_count ();
10279 bp_count += target_ranged_break_num_registers ();
10280 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10281 bp_count, 0);
10282 if (can_use_bp < 0)
10283 error (_("Hardware breakpoints used exceeds limit."));
10284
10285 arg = skip_spaces (arg);
10286 if (arg == NULL || arg[0] == '\0')
10287 error(_("No address range specified."));
10288
10289 init_linespec_result (&canonical_start);
10290
10291 arg_start = arg;
10292 start_location = string_to_event_location (&arg, current_language);
10293 cleanup_bkpt = make_cleanup_delete_event_location (start_location);
10294 parse_breakpoint_sals (start_location, &canonical_start);
10295 make_cleanup_destroy_linespec_result (&canonical_start);
10296
10297 if (arg[0] != ',')
10298 error (_("Too few arguments."));
10299 else if (VEC_empty (linespec_sals, canonical_start.sals))
10300 error (_("Could not find location of the beginning of the range."));
10301
10302 lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
10303
10304 if (VEC_length (linespec_sals, canonical_start.sals) > 1
10305 || lsal_start->sals.nelts != 1)
10306 error (_("Cannot create a ranged breakpoint with multiple locations."));
10307
10308 sal_start = lsal_start->sals.sals[0];
10309 addr_string_start = savestring (arg_start, arg - arg_start);
10310 make_cleanup (xfree, addr_string_start);
10311
10312 arg++; /* Skip the comma. */
10313 arg = skip_spaces (arg);
10314
10315 /* Parse the end location. */
10316
10317 init_linespec_result (&canonical_end);
10318 arg_start = arg;
10319
10320 /* We call decode_line_full directly here instead of using
10321 parse_breakpoint_sals because we need to specify the start location's
10322 symtab and line as the default symtab and line for the end of the
10323 range. This makes it possible to have ranges like "foo.c:27, +14",
10324 where +14 means 14 lines from the start location. */
10325 end_location = string_to_event_location (&arg, current_language);
10326 make_cleanup_delete_event_location (end_location);
10327 decode_line_full (end_location, DECODE_LINE_FUNFIRSTLINE,
10328 sal_start.symtab, sal_start.line,
10329 &canonical_end, NULL, NULL);
10330
10331 make_cleanup_destroy_linespec_result (&canonical_end);
10332
10333 if (VEC_empty (linespec_sals, canonical_end.sals))
10334 error (_("Could not find location of the end of the range."));
10335
10336 lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
10337 if (VEC_length (linespec_sals, canonical_end.sals) > 1
10338 || lsal_end->sals.nelts != 1)
10339 error (_("Cannot create a ranged breakpoint with multiple locations."));
10340
10341 sal_end = lsal_end->sals.sals[0];
10342
10343 end = find_breakpoint_range_end (sal_end);
10344 if (sal_start.pc > end)
10345 error (_("Invalid address range, end precedes start."));
10346
10347 length = end - sal_start.pc + 1;
10348 if (length < 0)
10349 /* Length overflowed. */
10350 error (_("Address range too large."));
10351 else if (length == 1)
10352 {
10353 /* This range is simple enough to be handled by
10354 the `hbreak' command. */
10355 hbreak_command (addr_string_start, 1);
10356
10357 do_cleanups (cleanup_bkpt);
10358
10359 return;
10360 }
10361
10362 /* Now set up the breakpoint. */
10363 b = set_raw_breakpoint (get_current_arch (), sal_start,
10364 bp_hardware_breakpoint, &ranged_breakpoint_ops);
10365 set_breakpoint_count (breakpoint_count + 1);
10366 b->number = breakpoint_count;
10367 b->disposition = disp_donttouch;
10368 b->location = copy_event_location (start_location);
10369 b->location_range_end = copy_event_location (end_location);
10370 b->loc->length = length;
10371
10372 do_cleanups (cleanup_bkpt);
10373
10374 mention (b);
10375 observer_notify_breakpoint_created (b);
10376 update_global_location_list (UGLL_MAY_INSERT);
10377 }
10378
10379 /* Return non-zero if EXP is verified as constant. Returned zero
10380 means EXP is variable. Also the constant detection may fail for
10381 some constant expressions and in such case still falsely return
10382 zero. */
10383
10384 static int
10385 watchpoint_exp_is_const (const struct expression *exp)
10386 {
10387 int i = exp->nelts;
10388
10389 while (i > 0)
10390 {
10391 int oplenp, argsp;
10392
10393 /* We are only interested in the descriptor of each element. */
10394 operator_length (exp, i, &oplenp, &argsp);
10395 i -= oplenp;
10396
10397 switch (exp->elts[i].opcode)
10398 {
10399 case BINOP_ADD:
10400 case BINOP_SUB:
10401 case BINOP_MUL:
10402 case BINOP_DIV:
10403 case BINOP_REM:
10404 case BINOP_MOD:
10405 case BINOP_LSH:
10406 case BINOP_RSH:
10407 case BINOP_LOGICAL_AND:
10408 case BINOP_LOGICAL_OR:
10409 case BINOP_BITWISE_AND:
10410 case BINOP_BITWISE_IOR:
10411 case BINOP_BITWISE_XOR:
10412 case BINOP_EQUAL:
10413 case BINOP_NOTEQUAL:
10414 case BINOP_LESS:
10415 case BINOP_GTR:
10416 case BINOP_LEQ:
10417 case BINOP_GEQ:
10418 case BINOP_REPEAT:
10419 case BINOP_COMMA:
10420 case BINOP_EXP:
10421 case BINOP_MIN:
10422 case BINOP_MAX:
10423 case BINOP_INTDIV:
10424 case BINOP_CONCAT:
10425 case TERNOP_COND:
10426 case TERNOP_SLICE:
10427
10428 case OP_LONG:
10429 case OP_DOUBLE:
10430 case OP_DECFLOAT:
10431 case OP_LAST:
10432 case OP_COMPLEX:
10433 case OP_STRING:
10434 case OP_ARRAY:
10435 case OP_TYPE:
10436 case OP_TYPEOF:
10437 case OP_DECLTYPE:
10438 case OP_TYPEID:
10439 case OP_NAME:
10440 case OP_OBJC_NSSTRING:
10441
10442 case UNOP_NEG:
10443 case UNOP_LOGICAL_NOT:
10444 case UNOP_COMPLEMENT:
10445 case UNOP_ADDR:
10446 case UNOP_HIGH:
10447 case UNOP_CAST:
10448
10449 case UNOP_CAST_TYPE:
10450 case UNOP_REINTERPRET_CAST:
10451 case UNOP_DYNAMIC_CAST:
10452 /* Unary, binary and ternary operators: We have to check
10453 their operands. If they are constant, then so is the
10454 result of that operation. For instance, if A and B are
10455 determined to be constants, then so is "A + B".
10456
10457 UNOP_IND is one exception to the rule above, because the
10458 value of *ADDR is not necessarily a constant, even when
10459 ADDR is. */
10460 break;
10461
10462 case OP_VAR_VALUE:
10463 /* Check whether the associated symbol is a constant.
10464
10465 We use SYMBOL_CLASS rather than TYPE_CONST because it's
10466 possible that a buggy compiler could mark a variable as
10467 constant even when it is not, and TYPE_CONST would return
10468 true in this case, while SYMBOL_CLASS wouldn't.
10469
10470 We also have to check for function symbols because they
10471 are always constant. */
10472 {
10473 struct symbol *s = exp->elts[i + 2].symbol;
10474
10475 if (SYMBOL_CLASS (s) != LOC_BLOCK
10476 && SYMBOL_CLASS (s) != LOC_CONST
10477 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10478 return 0;
10479 break;
10480 }
10481
10482 /* The default action is to return 0 because we are using
10483 the optimistic approach here: If we don't know something,
10484 then it is not a constant. */
10485 default:
10486 return 0;
10487 }
10488 }
10489
10490 return 1;
10491 }
10492
10493 /* Implement the "dtor" breakpoint_ops method for watchpoints. */
10494
10495 static void
10496 dtor_watchpoint (struct breakpoint *self)
10497 {
10498 struct watchpoint *w = (struct watchpoint *) self;
10499
10500 xfree (w->cond_exp);
10501 xfree (w->exp);
10502 xfree (w->exp_string);
10503 xfree (w->exp_string_reparse);
10504 value_free (w->val);
10505
10506 base_breakpoint_ops.dtor (self);
10507 }
10508
10509 /* Implement the "re_set" breakpoint_ops method for watchpoints. */
10510
10511 static void
10512 re_set_watchpoint (struct breakpoint *b)
10513 {
10514 struct watchpoint *w = (struct watchpoint *) b;
10515
10516 /* Watchpoint can be either on expression using entirely global
10517 variables, or it can be on local variables.
10518
10519 Watchpoints of the first kind are never auto-deleted, and even
10520 persist across program restarts. Since they can use variables
10521 from shared libraries, we need to reparse expression as libraries
10522 are loaded and unloaded.
10523
10524 Watchpoints on local variables can also change meaning as result
10525 of solib event. For example, if a watchpoint uses both a local
10526 and a global variables in expression, it's a local watchpoint,
10527 but unloading of a shared library will make the expression
10528 invalid. This is not a very common use case, but we still
10529 re-evaluate expression, to avoid surprises to the user.
10530
10531 Note that for local watchpoints, we re-evaluate it only if
10532 watchpoints frame id is still valid. If it's not, it means the
10533 watchpoint is out of scope and will be deleted soon. In fact,
10534 I'm not sure we'll ever be called in this case.
10535
10536 If a local watchpoint's frame id is still valid, then
10537 w->exp_valid_block is likewise valid, and we can safely use it.
10538
10539 Don't do anything about disabled watchpoints, since they will be
10540 reevaluated again when enabled. */
10541 update_watchpoint (w, 1 /* reparse */);
10542 }
10543
10544 /* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10545
10546 static int
10547 insert_watchpoint (struct bp_location *bl)
10548 {
10549 struct watchpoint *w = (struct watchpoint *) bl->owner;
10550 int length = w->exact ? 1 : bl->length;
10551
10552 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
10553 w->cond_exp);
10554 }
10555
10556 /* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10557
10558 static int
10559 remove_watchpoint (struct bp_location *bl)
10560 {
10561 struct watchpoint *w = (struct watchpoint *) bl->owner;
10562 int length = w->exact ? 1 : bl->length;
10563
10564 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
10565 w->cond_exp);
10566 }
10567
10568 static int
10569 breakpoint_hit_watchpoint (const struct bp_location *bl,
10570 struct address_space *aspace, CORE_ADDR bp_addr,
10571 const struct target_waitstatus *ws)
10572 {
10573 struct breakpoint *b = bl->owner;
10574 struct watchpoint *w = (struct watchpoint *) b;
10575
10576 /* Continuable hardware watchpoints are treated as non-existent if the
10577 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10578 some data address). Otherwise gdb won't stop on a break instruction
10579 in the code (not from a breakpoint) when a hardware watchpoint has
10580 been defined. Also skip watchpoints which we know did not trigger
10581 (did not match the data address). */
10582 if (is_hardware_watchpoint (b)
10583 && w->watchpoint_triggered == watch_triggered_no)
10584 return 0;
10585
10586 return 1;
10587 }
10588
10589 static void
10590 check_status_watchpoint (bpstat bs)
10591 {
10592 gdb_assert (is_watchpoint (bs->breakpoint_at));
10593
10594 bpstat_check_watchpoint (bs);
10595 }
10596
10597 /* Implement the "resources_needed" breakpoint_ops method for
10598 hardware watchpoints. */
10599
10600 static int
10601 resources_needed_watchpoint (const struct bp_location *bl)
10602 {
10603 struct watchpoint *w = (struct watchpoint *) bl->owner;
10604 int length = w->exact? 1 : bl->length;
10605
10606 return target_region_ok_for_hw_watchpoint (bl->address, length);
10607 }
10608
10609 /* Implement the "works_in_software_mode" breakpoint_ops method for
10610 hardware watchpoints. */
10611
10612 static int
10613 works_in_software_mode_watchpoint (const struct breakpoint *b)
10614 {
10615 /* Read and access watchpoints only work with hardware support. */
10616 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
10617 }
10618
10619 static enum print_stop_action
10620 print_it_watchpoint (bpstat bs)
10621 {
10622 struct cleanup *old_chain;
10623 struct breakpoint *b;
10624 struct ui_file *stb;
10625 enum print_stop_action result;
10626 struct watchpoint *w;
10627 struct ui_out *uiout = current_uiout;
10628
10629 gdb_assert (bs->bp_location_at != NULL);
10630
10631 b = bs->breakpoint_at;
10632 w = (struct watchpoint *) b;
10633
10634 stb = mem_fileopen ();
10635 old_chain = make_cleanup_ui_file_delete (stb);
10636
10637 switch (b->type)
10638 {
10639 case bp_watchpoint:
10640 case bp_hardware_watchpoint:
10641 annotate_watchpoint (b->number);
10642 if (ui_out_is_mi_like_p (uiout))
10643 ui_out_field_string
10644 (uiout, "reason",
10645 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10646 mention (b);
10647 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10648 ui_out_text (uiout, "\nOld value = ");
10649 watchpoint_value_print (bs->old_val, stb);
10650 ui_out_field_stream (uiout, "old", stb);
10651 ui_out_text (uiout, "\nNew value = ");
10652 watchpoint_value_print (w->val, stb);
10653 ui_out_field_stream (uiout, "new", stb);
10654 ui_out_text (uiout, "\n");
10655 /* More than one watchpoint may have been triggered. */
10656 result = PRINT_UNKNOWN;
10657 break;
10658
10659 case bp_read_watchpoint:
10660 if (ui_out_is_mi_like_p (uiout))
10661 ui_out_field_string
10662 (uiout, "reason",
10663 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10664 mention (b);
10665 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10666 ui_out_text (uiout, "\nValue = ");
10667 watchpoint_value_print (w->val, stb);
10668 ui_out_field_stream (uiout, "value", stb);
10669 ui_out_text (uiout, "\n");
10670 result = PRINT_UNKNOWN;
10671 break;
10672
10673 case bp_access_watchpoint:
10674 if (bs->old_val != NULL)
10675 {
10676 annotate_watchpoint (b->number);
10677 if (ui_out_is_mi_like_p (uiout))
10678 ui_out_field_string
10679 (uiout, "reason",
10680 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10681 mention (b);
10682 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10683 ui_out_text (uiout, "\nOld value = ");
10684 watchpoint_value_print (bs->old_val, stb);
10685 ui_out_field_stream (uiout, "old", stb);
10686 ui_out_text (uiout, "\nNew value = ");
10687 }
10688 else
10689 {
10690 mention (b);
10691 if (ui_out_is_mi_like_p (uiout))
10692 ui_out_field_string
10693 (uiout, "reason",
10694 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10695 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10696 ui_out_text (uiout, "\nValue = ");
10697 }
10698 watchpoint_value_print (w->val, stb);
10699 ui_out_field_stream (uiout, "new", stb);
10700 ui_out_text (uiout, "\n");
10701 result = PRINT_UNKNOWN;
10702 break;
10703 default:
10704 result = PRINT_UNKNOWN;
10705 }
10706
10707 do_cleanups (old_chain);
10708 return result;
10709 }
10710
10711 /* Implement the "print_mention" breakpoint_ops method for hardware
10712 watchpoints. */
10713
10714 static void
10715 print_mention_watchpoint (struct breakpoint *b)
10716 {
10717 struct cleanup *ui_out_chain;
10718 struct watchpoint *w = (struct watchpoint *) b;
10719 struct ui_out *uiout = current_uiout;
10720
10721 switch (b->type)
10722 {
10723 case bp_watchpoint:
10724 ui_out_text (uiout, "Watchpoint ");
10725 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10726 break;
10727 case bp_hardware_watchpoint:
10728 ui_out_text (uiout, "Hardware watchpoint ");
10729 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10730 break;
10731 case bp_read_watchpoint:
10732 ui_out_text (uiout, "Hardware read watchpoint ");
10733 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
10734 break;
10735 case bp_access_watchpoint:
10736 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
10737 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
10738 break;
10739 default:
10740 internal_error (__FILE__, __LINE__,
10741 _("Invalid hardware watchpoint type."));
10742 }
10743
10744 ui_out_field_int (uiout, "number", b->number);
10745 ui_out_text (uiout, ": ");
10746 ui_out_field_string (uiout, "exp", w->exp_string);
10747 do_cleanups (ui_out_chain);
10748 }
10749
10750 /* Implement the "print_recreate" breakpoint_ops method for
10751 watchpoints. */
10752
10753 static void
10754 print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10755 {
10756 struct watchpoint *w = (struct watchpoint *) b;
10757
10758 switch (b->type)
10759 {
10760 case bp_watchpoint:
10761 case bp_hardware_watchpoint:
10762 fprintf_unfiltered (fp, "watch");
10763 break;
10764 case bp_read_watchpoint:
10765 fprintf_unfiltered (fp, "rwatch");
10766 break;
10767 case bp_access_watchpoint:
10768 fprintf_unfiltered (fp, "awatch");
10769 break;
10770 default:
10771 internal_error (__FILE__, __LINE__,
10772 _("Invalid watchpoint type."));
10773 }
10774
10775 fprintf_unfiltered (fp, " %s", w->exp_string);
10776 print_recreate_thread (b, fp);
10777 }
10778
10779 /* Implement the "explains_signal" breakpoint_ops method for
10780 watchpoints. */
10781
10782 static int
10783 explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10784 {
10785 /* A software watchpoint cannot cause a signal other than
10786 GDB_SIGNAL_TRAP. */
10787 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
10788 return 0;
10789
10790 return 1;
10791 }
10792
10793 /* The breakpoint_ops structure to be used in hardware watchpoints. */
10794
10795 static struct breakpoint_ops watchpoint_breakpoint_ops;
10796
10797 /* Implement the "insert" breakpoint_ops method for
10798 masked hardware watchpoints. */
10799
10800 static int
10801 insert_masked_watchpoint (struct bp_location *bl)
10802 {
10803 struct watchpoint *w = (struct watchpoint *) bl->owner;
10804
10805 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
10806 bl->watchpoint_type);
10807 }
10808
10809 /* Implement the "remove" breakpoint_ops method for
10810 masked hardware watchpoints. */
10811
10812 static int
10813 remove_masked_watchpoint (struct bp_location *bl)
10814 {
10815 struct watchpoint *w = (struct watchpoint *) bl->owner;
10816
10817 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
10818 bl->watchpoint_type);
10819 }
10820
10821 /* Implement the "resources_needed" breakpoint_ops method for
10822 masked hardware watchpoints. */
10823
10824 static int
10825 resources_needed_masked_watchpoint (const struct bp_location *bl)
10826 {
10827 struct watchpoint *w = (struct watchpoint *) bl->owner;
10828
10829 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
10830 }
10831
10832 /* Implement the "works_in_software_mode" breakpoint_ops method for
10833 masked hardware watchpoints. */
10834
10835 static int
10836 works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10837 {
10838 return 0;
10839 }
10840
10841 /* Implement the "print_it" breakpoint_ops method for
10842 masked hardware watchpoints. */
10843
10844 static enum print_stop_action
10845 print_it_masked_watchpoint (bpstat bs)
10846 {
10847 struct breakpoint *b = bs->breakpoint_at;
10848 struct ui_out *uiout = current_uiout;
10849
10850 /* Masked watchpoints have only one location. */
10851 gdb_assert (b->loc && b->loc->next == NULL);
10852
10853 switch (b->type)
10854 {
10855 case bp_hardware_watchpoint:
10856 annotate_watchpoint (b->number);
10857 if (ui_out_is_mi_like_p (uiout))
10858 ui_out_field_string
10859 (uiout, "reason",
10860 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10861 break;
10862
10863 case bp_read_watchpoint:
10864 if (ui_out_is_mi_like_p (uiout))
10865 ui_out_field_string
10866 (uiout, "reason",
10867 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10868 break;
10869
10870 case bp_access_watchpoint:
10871 if (ui_out_is_mi_like_p (uiout))
10872 ui_out_field_string
10873 (uiout, "reason",
10874 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10875 break;
10876 default:
10877 internal_error (__FILE__, __LINE__,
10878 _("Invalid hardware watchpoint type."));
10879 }
10880
10881 mention (b);
10882 ui_out_text (uiout, _("\n\
10883 Check the underlying instruction at PC for the memory\n\
10884 address and value which triggered this watchpoint.\n"));
10885 ui_out_text (uiout, "\n");
10886
10887 /* More than one watchpoint may have been triggered. */
10888 return PRINT_UNKNOWN;
10889 }
10890
10891 /* Implement the "print_one_detail" breakpoint_ops method for
10892 masked hardware watchpoints. */
10893
10894 static void
10895 print_one_detail_masked_watchpoint (const struct breakpoint *b,
10896 struct ui_out *uiout)
10897 {
10898 struct watchpoint *w = (struct watchpoint *) b;
10899
10900 /* Masked watchpoints have only one location. */
10901 gdb_assert (b->loc && b->loc->next == NULL);
10902
10903 ui_out_text (uiout, "\tmask ");
10904 ui_out_field_core_addr (uiout, "mask", b->loc->gdbarch, w->hw_wp_mask);
10905 ui_out_text (uiout, "\n");
10906 }
10907
10908 /* Implement the "print_mention" breakpoint_ops method for
10909 masked hardware watchpoints. */
10910
10911 static void
10912 print_mention_masked_watchpoint (struct breakpoint *b)
10913 {
10914 struct watchpoint *w = (struct watchpoint *) b;
10915 struct ui_out *uiout = current_uiout;
10916 struct cleanup *ui_out_chain;
10917
10918 switch (b->type)
10919 {
10920 case bp_hardware_watchpoint:
10921 ui_out_text (uiout, "Masked hardware watchpoint ");
10922 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10923 break;
10924 case bp_read_watchpoint:
10925 ui_out_text (uiout, "Masked hardware read watchpoint ");
10926 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
10927 break;
10928 case bp_access_watchpoint:
10929 ui_out_text (uiout, "Masked hardware access (read/write) watchpoint ");
10930 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
10931 break;
10932 default:
10933 internal_error (__FILE__, __LINE__,
10934 _("Invalid hardware watchpoint type."));
10935 }
10936
10937 ui_out_field_int (uiout, "number", b->number);
10938 ui_out_text (uiout, ": ");
10939 ui_out_field_string (uiout, "exp", w->exp_string);
10940 do_cleanups (ui_out_chain);
10941 }
10942
10943 /* Implement the "print_recreate" breakpoint_ops method for
10944 masked hardware watchpoints. */
10945
10946 static void
10947 print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10948 {
10949 struct watchpoint *w = (struct watchpoint *) b;
10950 char tmp[40];
10951
10952 switch (b->type)
10953 {
10954 case bp_hardware_watchpoint:
10955 fprintf_unfiltered (fp, "watch");
10956 break;
10957 case bp_read_watchpoint:
10958 fprintf_unfiltered (fp, "rwatch");
10959 break;
10960 case bp_access_watchpoint:
10961 fprintf_unfiltered (fp, "awatch");
10962 break;
10963 default:
10964 internal_error (__FILE__, __LINE__,
10965 _("Invalid hardware watchpoint type."));
10966 }
10967
10968 sprintf_vma (tmp, w->hw_wp_mask);
10969 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
10970 print_recreate_thread (b, fp);
10971 }
10972
10973 /* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10974
10975 static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
10976
10977 /* Tell whether the given watchpoint is a masked hardware watchpoint. */
10978
10979 static int
10980 is_masked_watchpoint (const struct breakpoint *b)
10981 {
10982 return b->ops == &masked_watchpoint_breakpoint_ops;
10983 }
10984
10985 /* accessflag: hw_write: watch write,
10986 hw_read: watch read,
10987 hw_access: watch access (read or write) */
10988 static void
10989 watch_command_1 (const char *arg, int accessflag, int from_tty,
10990 int just_location, int internal)
10991 {
10992 struct breakpoint *b, *scope_breakpoint = NULL;
10993 struct expression *exp;
10994 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
10995 struct value *val, *mark, *result;
10996 int saved_bitpos = 0, saved_bitsize = 0;
10997 struct frame_info *frame;
10998 const char *exp_start = NULL;
10999 const char *exp_end = NULL;
11000 const char *tok, *end_tok;
11001 int toklen = -1;
11002 const char *cond_start = NULL;
11003 const char *cond_end = NULL;
11004 enum bptype bp_type;
11005 int thread = -1;
11006 int pc = 0;
11007 /* Flag to indicate whether we are going to use masks for
11008 the hardware watchpoint. */
11009 int use_mask = 0;
11010 CORE_ADDR mask = 0;
11011 struct watchpoint *w;
11012 char *expression;
11013 struct cleanup *back_to;
11014
11015 /* Make sure that we actually have parameters to parse. */
11016 if (arg != NULL && arg[0] != '\0')
11017 {
11018 const char *value_start;
11019
11020 exp_end = arg + strlen (arg);
11021
11022 /* Look for "parameter value" pairs at the end
11023 of the arguments string. */
11024 for (tok = exp_end - 1; tok > arg; tok--)
11025 {
11026 /* Skip whitespace at the end of the argument list. */
11027 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11028 tok--;
11029
11030 /* Find the beginning of the last token.
11031 This is the value of the parameter. */
11032 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11033 tok--;
11034 value_start = tok + 1;
11035
11036 /* Skip whitespace. */
11037 while (tok > arg && (*tok == ' ' || *tok == '\t'))
11038 tok--;
11039
11040 end_tok = tok;
11041
11042 /* Find the beginning of the second to last token.
11043 This is the parameter itself. */
11044 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11045 tok--;
11046 tok++;
11047 toklen = end_tok - tok + 1;
11048
11049 if (toklen == 6 && startswith (tok, "thread"))
11050 {
11051 /* At this point we've found a "thread" token, which means
11052 the user is trying to set a watchpoint that triggers
11053 only in a specific thread. */
11054 char *endp;
11055
11056 if (thread != -1)
11057 error(_("You can specify only one thread."));
11058
11059 /* Extract the thread ID from the next token. */
11060 thread = strtol (value_start, &endp, 0);
11061
11062 /* Check if the user provided a valid numeric value for the
11063 thread ID. */
11064 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
11065 error (_("Invalid thread ID specification %s."), value_start);
11066
11067 /* Check if the thread actually exists. */
11068 if (!valid_thread_id (thread))
11069 invalid_thread_id_error (thread);
11070 }
11071 else if (toklen == 4 && startswith (tok, "mask"))
11072 {
11073 /* We've found a "mask" token, which means the user wants to
11074 create a hardware watchpoint that is going to have the mask
11075 facility. */
11076 struct value *mask_value, *mark;
11077
11078 if (use_mask)
11079 error(_("You can specify only one mask."));
11080
11081 use_mask = just_location = 1;
11082
11083 mark = value_mark ();
11084 mask_value = parse_to_comma_and_eval (&value_start);
11085 mask = value_as_address (mask_value);
11086 value_free_to_mark (mark);
11087 }
11088 else
11089 /* We didn't recognize what we found. We should stop here. */
11090 break;
11091
11092 /* Truncate the string and get rid of the "parameter value" pair before
11093 the arguments string is parsed by the parse_exp_1 function. */
11094 exp_end = tok;
11095 }
11096 }
11097 else
11098 exp_end = arg;
11099
11100 /* Parse the rest of the arguments. From here on out, everything
11101 is in terms of a newly allocated string instead of the original
11102 ARG. */
11103 innermost_block = NULL;
11104 expression = savestring (arg, exp_end - arg);
11105 back_to = make_cleanup (xfree, expression);
11106 exp_start = arg = expression;
11107 exp = parse_exp_1 (&arg, 0, 0, 0);
11108 exp_end = arg;
11109 /* Remove trailing whitespace from the expression before saving it.
11110 This makes the eventual display of the expression string a bit
11111 prettier. */
11112 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
11113 --exp_end;
11114
11115 /* Checking if the expression is not constant. */
11116 if (watchpoint_exp_is_const (exp))
11117 {
11118 int len;
11119
11120 len = exp_end - exp_start;
11121 while (len > 0 && isspace (exp_start[len - 1]))
11122 len--;
11123 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
11124 }
11125
11126 exp_valid_block = innermost_block;
11127 mark = value_mark ();
11128 fetch_subexp_value (exp, &pc, &val, &result, NULL, just_location);
11129
11130 if (val != NULL && just_location)
11131 {
11132 saved_bitpos = value_bitpos (val);
11133 saved_bitsize = value_bitsize (val);
11134 }
11135
11136 if (just_location)
11137 {
11138 int ret;
11139
11140 exp_valid_block = NULL;
11141 val = value_addr (result);
11142 release_value (val);
11143 value_free_to_mark (mark);
11144
11145 if (use_mask)
11146 {
11147 ret = target_masked_watch_num_registers (value_as_address (val),
11148 mask);
11149 if (ret == -1)
11150 error (_("This target does not support masked watchpoints."));
11151 else if (ret == -2)
11152 error (_("Invalid mask or memory region."));
11153 }
11154 }
11155 else if (val != NULL)
11156 release_value (val);
11157
11158 tok = skip_spaces_const (arg);
11159 end_tok = skip_to_space_const (tok);
11160
11161 toklen = end_tok - tok;
11162 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
11163 {
11164 struct expression *cond;
11165
11166 innermost_block = NULL;
11167 tok = cond_start = end_tok + 1;
11168 cond = parse_exp_1 (&tok, 0, 0, 0);
11169
11170 /* The watchpoint expression may not be local, but the condition
11171 may still be. E.g.: `watch global if local > 0'. */
11172 cond_exp_valid_block = innermost_block;
11173
11174 xfree (cond);
11175 cond_end = tok;
11176 }
11177 if (*tok)
11178 error (_("Junk at end of command."));
11179
11180 frame = block_innermost_frame (exp_valid_block);
11181
11182 /* If the expression is "local", then set up a "watchpoint scope"
11183 breakpoint at the point where we've left the scope of the watchpoint
11184 expression. Create the scope breakpoint before the watchpoint, so
11185 that we will encounter it first in bpstat_stop_status. */
11186 if (exp_valid_block && frame)
11187 {
11188 if (frame_id_p (frame_unwind_caller_id (frame)))
11189 {
11190 scope_breakpoint
11191 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
11192 frame_unwind_caller_pc (frame),
11193 bp_watchpoint_scope,
11194 &momentary_breakpoint_ops);
11195
11196 scope_breakpoint->enable_state = bp_enabled;
11197
11198 /* Automatically delete the breakpoint when it hits. */
11199 scope_breakpoint->disposition = disp_del;
11200
11201 /* Only break in the proper frame (help with recursion). */
11202 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
11203
11204 /* Set the address at which we will stop. */
11205 scope_breakpoint->loc->gdbarch
11206 = frame_unwind_caller_arch (frame);
11207 scope_breakpoint->loc->requested_address
11208 = frame_unwind_caller_pc (frame);
11209 scope_breakpoint->loc->address
11210 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
11211 scope_breakpoint->loc->requested_address,
11212 scope_breakpoint->type);
11213 }
11214 }
11215
11216 /* Now set up the breakpoint. We create all watchpoints as hardware
11217 watchpoints here even if hardware watchpoints are turned off, a call
11218 to update_watchpoint later in this function will cause the type to
11219 drop back to bp_watchpoint (software watchpoint) if required. */
11220
11221 if (accessflag == hw_read)
11222 bp_type = bp_read_watchpoint;
11223 else if (accessflag == hw_access)
11224 bp_type = bp_access_watchpoint;
11225 else
11226 bp_type = bp_hardware_watchpoint;
11227
11228 w = XCNEW (struct watchpoint);
11229 b = &w->base;
11230 if (use_mask)
11231 init_raw_breakpoint_without_location (b, NULL, bp_type,
11232 &masked_watchpoint_breakpoint_ops);
11233 else
11234 init_raw_breakpoint_without_location (b, NULL, bp_type,
11235 &watchpoint_breakpoint_ops);
11236 b->thread = thread;
11237 b->disposition = disp_donttouch;
11238 b->pspace = current_program_space;
11239 w->exp = exp;
11240 w->exp_valid_block = exp_valid_block;
11241 w->cond_exp_valid_block = cond_exp_valid_block;
11242 if (just_location)
11243 {
11244 struct type *t = value_type (val);
11245 CORE_ADDR addr = value_as_address (val);
11246 char *name;
11247
11248 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
11249 name = type_to_string (t);
11250
11251 w->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
11252 core_addr_to_string (addr));
11253 xfree (name);
11254
11255 w->exp_string = xstrprintf ("-location %.*s",
11256 (int) (exp_end - exp_start), exp_start);
11257
11258 /* The above expression is in C. */
11259 b->language = language_c;
11260 }
11261 else
11262 w->exp_string = savestring (exp_start, exp_end - exp_start);
11263
11264 if (use_mask)
11265 {
11266 w->hw_wp_mask = mask;
11267 }
11268 else
11269 {
11270 w->val = val;
11271 w->val_bitpos = saved_bitpos;
11272 w->val_bitsize = saved_bitsize;
11273 w->val_valid = 1;
11274 }
11275
11276 if (cond_start)
11277 b->cond_string = savestring (cond_start, cond_end - cond_start);
11278 else
11279 b->cond_string = 0;
11280
11281 if (frame)
11282 {
11283 w->watchpoint_frame = get_frame_id (frame);
11284 w->watchpoint_thread = inferior_ptid;
11285 }
11286 else
11287 {
11288 w->watchpoint_frame = null_frame_id;
11289 w->watchpoint_thread = null_ptid;
11290 }
11291
11292 if (scope_breakpoint != NULL)
11293 {
11294 /* The scope breakpoint is related to the watchpoint. We will
11295 need to act on them together. */
11296 b->related_breakpoint = scope_breakpoint;
11297 scope_breakpoint->related_breakpoint = b;
11298 }
11299
11300 if (!just_location)
11301 value_free_to_mark (mark);
11302
11303 TRY
11304 {
11305 /* Finally update the new watchpoint. This creates the locations
11306 that should be inserted. */
11307 update_watchpoint (w, 1);
11308 }
11309 CATCH (e, RETURN_MASK_ALL)
11310 {
11311 delete_breakpoint (b);
11312 throw_exception (e);
11313 }
11314 END_CATCH
11315
11316 install_breakpoint (internal, b, 1);
11317 do_cleanups (back_to);
11318 }
11319
11320 /* Return count of debug registers needed to watch the given expression.
11321 If the watchpoint cannot be handled in hardware return zero. */
11322
11323 static int
11324 can_use_hardware_watchpoint (struct value *v)
11325 {
11326 int found_memory_cnt = 0;
11327 struct value *head = v;
11328
11329 /* Did the user specifically forbid us to use hardware watchpoints? */
11330 if (!can_use_hw_watchpoints)
11331 return 0;
11332
11333 /* Make sure that the value of the expression depends only upon
11334 memory contents, and values computed from them within GDB. If we
11335 find any register references or function calls, we can't use a
11336 hardware watchpoint.
11337
11338 The idea here is that evaluating an expression generates a series
11339 of values, one holding the value of every subexpression. (The
11340 expression a*b+c has five subexpressions: a, b, a*b, c, and
11341 a*b+c.) GDB's values hold almost enough information to establish
11342 the criteria given above --- they identify memory lvalues,
11343 register lvalues, computed values, etcetera. So we can evaluate
11344 the expression, and then scan the chain of values that leaves
11345 behind to decide whether we can detect any possible change to the
11346 expression's final value using only hardware watchpoints.
11347
11348 However, I don't think that the values returned by inferior
11349 function calls are special in any way. So this function may not
11350 notice that an expression involving an inferior function call
11351 can't be watched with hardware watchpoints. FIXME. */
11352 for (; v; v = value_next (v))
11353 {
11354 if (VALUE_LVAL (v) == lval_memory)
11355 {
11356 if (v != head && value_lazy (v))
11357 /* A lazy memory lvalue in the chain is one that GDB never
11358 needed to fetch; we either just used its address (e.g.,
11359 `a' in `a.b') or we never needed it at all (e.g., `a'
11360 in `a,b'). This doesn't apply to HEAD; if that is
11361 lazy then it was not readable, but watch it anyway. */
11362 ;
11363 else
11364 {
11365 /* Ahh, memory we actually used! Check if we can cover
11366 it with hardware watchpoints. */
11367 struct type *vtype = check_typedef (value_type (v));
11368
11369 /* We only watch structs and arrays if user asked for it
11370 explicitly, never if they just happen to appear in a
11371 middle of some value chain. */
11372 if (v == head
11373 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11374 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11375 {
11376 CORE_ADDR vaddr = value_address (v);
11377 int len;
11378 int num_regs;
11379
11380 len = (target_exact_watchpoints
11381 && is_scalar_type_recursive (vtype))?
11382 1 : TYPE_LENGTH (value_type (v));
11383
11384 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11385 if (!num_regs)
11386 return 0;
11387 else
11388 found_memory_cnt += num_regs;
11389 }
11390 }
11391 }
11392 else if (VALUE_LVAL (v) != not_lval
11393 && deprecated_value_modifiable (v) == 0)
11394 return 0; /* These are values from the history (e.g., $1). */
11395 else if (VALUE_LVAL (v) == lval_register)
11396 return 0; /* Cannot watch a register with a HW watchpoint. */
11397 }
11398
11399 /* The expression itself looks suitable for using a hardware
11400 watchpoint, but give the target machine a chance to reject it. */
11401 return found_memory_cnt;
11402 }
11403
11404 void
11405 watch_command_wrapper (char *arg, int from_tty, int internal)
11406 {
11407 watch_command_1 (arg, hw_write, from_tty, 0, internal);
11408 }
11409
11410 /* A helper function that looks for the "-location" argument and then
11411 calls watch_command_1. */
11412
11413 static void
11414 watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11415 {
11416 int just_location = 0;
11417
11418 if (arg
11419 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11420 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11421 {
11422 arg = skip_spaces (arg);
11423 just_location = 1;
11424 }
11425
11426 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
11427 }
11428
11429 static void
11430 watch_command (char *arg, int from_tty)
11431 {
11432 watch_maybe_just_location (arg, hw_write, from_tty);
11433 }
11434
11435 void
11436 rwatch_command_wrapper (char *arg, int from_tty, int internal)
11437 {
11438 watch_command_1 (arg, hw_read, from_tty, 0, internal);
11439 }
11440
11441 static void
11442 rwatch_command (char *arg, int from_tty)
11443 {
11444 watch_maybe_just_location (arg, hw_read, from_tty);
11445 }
11446
11447 void
11448 awatch_command_wrapper (char *arg, int from_tty, int internal)
11449 {
11450 watch_command_1 (arg, hw_access, from_tty, 0, internal);
11451 }
11452
11453 static void
11454 awatch_command (char *arg, int from_tty)
11455 {
11456 watch_maybe_just_location (arg, hw_access, from_tty);
11457 }
11458 \f
11459
11460 /* Helper routines for the until_command routine in infcmd.c. Here
11461 because it uses the mechanisms of breakpoints. */
11462
11463 struct until_break_command_continuation_args
11464 {
11465 struct breakpoint *breakpoint;
11466 struct breakpoint *breakpoint2;
11467 int thread_num;
11468 };
11469
11470 /* This function is called by fetch_inferior_event via the
11471 cmd_continuation pointer, to complete the until command. It takes
11472 care of cleaning up the temporary breakpoints set up by the until
11473 command. */
11474 static void
11475 until_break_command_continuation (void *arg, int err)
11476 {
11477 struct until_break_command_continuation_args *a = arg;
11478
11479 delete_breakpoint (a->breakpoint);
11480 if (a->breakpoint2)
11481 delete_breakpoint (a->breakpoint2);
11482 delete_longjmp_breakpoint (a->thread_num);
11483 }
11484
11485 void
11486 until_break_command (char *arg, int from_tty, int anywhere)
11487 {
11488 struct symtabs_and_lines sals;
11489 struct symtab_and_line sal;
11490 struct frame_info *frame;
11491 struct gdbarch *frame_gdbarch;
11492 struct frame_id stack_frame_id;
11493 struct frame_id caller_frame_id;
11494 struct breakpoint *breakpoint;
11495 struct breakpoint *breakpoint2 = NULL;
11496 struct cleanup *old_chain, *cleanup;
11497 int thread;
11498 struct thread_info *tp;
11499 struct event_location *location;
11500
11501 clear_proceed_status (0);
11502
11503 /* Set a breakpoint where the user wants it and at return from
11504 this function. */
11505
11506 location = string_to_event_location (&arg, current_language);
11507 cleanup = make_cleanup_delete_event_location (location);
11508
11509 if (last_displayed_sal_is_valid ())
11510 sals = decode_line_1 (location, DECODE_LINE_FUNFIRSTLINE,
11511 get_last_displayed_symtab (),
11512 get_last_displayed_line ());
11513 else
11514 sals = decode_line_1 (location, DECODE_LINE_FUNFIRSTLINE,
11515 (struct symtab *) NULL, 0);
11516
11517 if (sals.nelts != 1)
11518 error (_("Couldn't get information on specified line."));
11519
11520 sal = sals.sals[0];
11521 xfree (sals.sals); /* malloc'd, so freed. */
11522
11523 if (*arg)
11524 error (_("Junk at end of arguments."));
11525
11526 resolve_sal_pc (&sal);
11527
11528 tp = inferior_thread ();
11529 thread = tp->num;
11530
11531 old_chain = make_cleanup (null_cleanup, NULL);
11532
11533 /* Note linespec handling above invalidates the frame chain.
11534 Installing a breakpoint also invalidates the frame chain (as it
11535 may need to switch threads), so do any frame handling before
11536 that. */
11537
11538 frame = get_selected_frame (NULL);
11539 frame_gdbarch = get_frame_arch (frame);
11540 stack_frame_id = get_stack_frame_id (frame);
11541 caller_frame_id = frame_unwind_caller_id (frame);
11542
11543 /* Keep within the current frame, or in frames called by the current
11544 one. */
11545
11546 if (frame_id_p (caller_frame_id))
11547 {
11548 struct symtab_and_line sal2;
11549
11550 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11551 sal2.pc = frame_unwind_caller_pc (frame);
11552 breakpoint2 = set_momentary_breakpoint (frame_unwind_caller_arch (frame),
11553 sal2,
11554 caller_frame_id,
11555 bp_until);
11556 make_cleanup_delete_breakpoint (breakpoint2);
11557
11558 set_longjmp_breakpoint (tp, caller_frame_id);
11559 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
11560 }
11561
11562 /* set_momentary_breakpoint could invalidate FRAME. */
11563 frame = NULL;
11564
11565 if (anywhere)
11566 /* If the user told us to continue until a specified location,
11567 we don't specify a frame at which we need to stop. */
11568 breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11569 null_frame_id, bp_until);
11570 else
11571 /* Otherwise, specify the selected frame, because we want to stop
11572 only at the very same frame. */
11573 breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11574 stack_frame_id, bp_until);
11575 make_cleanup_delete_breakpoint (breakpoint);
11576
11577 proceed (-1, GDB_SIGNAL_DEFAULT);
11578
11579 /* If we are running asynchronously, and proceed call above has
11580 actually managed to start the target, arrange for breakpoints to
11581 be deleted when the target stops. Otherwise, we're already
11582 stopped and delete breakpoints via cleanup chain. */
11583
11584 if (target_can_async_p () && is_running (inferior_ptid))
11585 {
11586 struct until_break_command_continuation_args *args;
11587 args = xmalloc (sizeof (*args));
11588
11589 args->breakpoint = breakpoint;
11590 args->breakpoint2 = breakpoint2;
11591 args->thread_num = thread;
11592
11593 discard_cleanups (old_chain);
11594 add_continuation (inferior_thread (),
11595 until_break_command_continuation, args,
11596 xfree);
11597 }
11598 else
11599 do_cleanups (old_chain);
11600
11601 do_cleanups (cleanup);
11602 }
11603
11604 /* This function attempts to parse an optional "if <cond>" clause
11605 from the arg string. If one is not found, it returns NULL.
11606
11607 Else, it returns a pointer to the condition string. (It does not
11608 attempt to evaluate the string against a particular block.) And,
11609 it updates arg to point to the first character following the parsed
11610 if clause in the arg string. */
11611
11612 char *
11613 ep_parse_optional_if_clause (char **arg)
11614 {
11615 char *cond_string;
11616
11617 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
11618 return NULL;
11619
11620 /* Skip the "if" keyword. */
11621 (*arg) += 2;
11622
11623 /* Skip any extra leading whitespace, and record the start of the
11624 condition string. */
11625 *arg = skip_spaces (*arg);
11626 cond_string = *arg;
11627
11628 /* Assume that the condition occupies the remainder of the arg
11629 string. */
11630 (*arg) += strlen (cond_string);
11631
11632 return cond_string;
11633 }
11634
11635 /* Commands to deal with catching events, such as signals, exceptions,
11636 process start/exit, etc. */
11637
11638 typedef enum
11639 {
11640 catch_fork_temporary, catch_vfork_temporary,
11641 catch_fork_permanent, catch_vfork_permanent
11642 }
11643 catch_fork_kind;
11644
11645 static void
11646 catch_fork_command_1 (char *arg, int from_tty,
11647 struct cmd_list_element *command)
11648 {
11649 struct gdbarch *gdbarch = get_current_arch ();
11650 char *cond_string = NULL;
11651 catch_fork_kind fork_kind;
11652 int tempflag;
11653
11654 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11655 tempflag = (fork_kind == catch_fork_temporary
11656 || fork_kind == catch_vfork_temporary);
11657
11658 if (!arg)
11659 arg = "";
11660 arg = skip_spaces (arg);
11661
11662 /* The allowed syntax is:
11663 catch [v]fork
11664 catch [v]fork if <cond>
11665
11666 First, check if there's an if clause. */
11667 cond_string = ep_parse_optional_if_clause (&arg);
11668
11669 if ((*arg != '\0') && !isspace (*arg))
11670 error (_("Junk at end of arguments."));
11671
11672 /* If this target supports it, create a fork or vfork catchpoint
11673 and enable reporting of such events. */
11674 switch (fork_kind)
11675 {
11676 case catch_fork_temporary:
11677 case catch_fork_permanent:
11678 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11679 &catch_fork_breakpoint_ops);
11680 break;
11681 case catch_vfork_temporary:
11682 case catch_vfork_permanent:
11683 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11684 &catch_vfork_breakpoint_ops);
11685 break;
11686 default:
11687 error (_("unsupported or unknown fork kind; cannot catch it"));
11688 break;
11689 }
11690 }
11691
11692 static void
11693 catch_exec_command_1 (char *arg, int from_tty,
11694 struct cmd_list_element *command)
11695 {
11696 struct exec_catchpoint *c;
11697 struct gdbarch *gdbarch = get_current_arch ();
11698 int tempflag;
11699 char *cond_string = NULL;
11700
11701 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11702
11703 if (!arg)
11704 arg = "";
11705 arg = skip_spaces (arg);
11706
11707 /* The allowed syntax is:
11708 catch exec
11709 catch exec if <cond>
11710
11711 First, check if there's an if clause. */
11712 cond_string = ep_parse_optional_if_clause (&arg);
11713
11714 if ((*arg != '\0') && !isspace (*arg))
11715 error (_("Junk at end of arguments."));
11716
11717 c = XNEW (struct exec_catchpoint);
11718 init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
11719 &catch_exec_breakpoint_ops);
11720 c->exec_pathname = NULL;
11721
11722 install_breakpoint (0, &c->base, 1);
11723 }
11724
11725 void
11726 init_ada_exception_breakpoint (struct breakpoint *b,
11727 struct gdbarch *gdbarch,
11728 struct symtab_and_line sal,
11729 char *addr_string,
11730 const struct breakpoint_ops *ops,
11731 int tempflag,
11732 int enabled,
11733 int from_tty)
11734 {
11735 if (from_tty)
11736 {
11737 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11738 if (!loc_gdbarch)
11739 loc_gdbarch = gdbarch;
11740
11741 describe_other_breakpoints (loc_gdbarch,
11742 sal.pspace, sal.pc, sal.section, -1);
11743 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11744 version for exception catchpoints, because two catchpoints
11745 used for different exception names will use the same address.
11746 In this case, a "breakpoint ... also set at..." warning is
11747 unproductive. Besides, the warning phrasing is also a bit
11748 inappropriate, we should use the word catchpoint, and tell
11749 the user what type of catchpoint it is. The above is good
11750 enough for now, though. */
11751 }
11752
11753 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
11754
11755 b->enable_state = enabled ? bp_enabled : bp_disabled;
11756 b->disposition = tempflag ? disp_del : disp_donttouch;
11757 b->location = string_to_event_location (&addr_string,
11758 language_def (language_ada));
11759 b->language = language_ada;
11760 }
11761
11762 static void
11763 catch_command (char *arg, int from_tty)
11764 {
11765 error (_("Catch requires an event name."));
11766 }
11767 \f
11768
11769 static void
11770 tcatch_command (char *arg, int from_tty)
11771 {
11772 error (_("Catch requires an event name."));
11773 }
11774
11775 /* A qsort comparison function that sorts breakpoints in order. */
11776
11777 static int
11778 compare_breakpoints (const void *a, const void *b)
11779 {
11780 const breakpoint_p *ba = a;
11781 uintptr_t ua = (uintptr_t) *ba;
11782 const breakpoint_p *bb = b;
11783 uintptr_t ub = (uintptr_t) *bb;
11784
11785 if ((*ba)->number < (*bb)->number)
11786 return -1;
11787 else if ((*ba)->number > (*bb)->number)
11788 return 1;
11789
11790 /* Now sort by address, in case we see, e..g, two breakpoints with
11791 the number 0. */
11792 if (ua < ub)
11793 return -1;
11794 return ua > ub ? 1 : 0;
11795 }
11796
11797 /* Delete breakpoints by address or line. */
11798
11799 static void
11800 clear_command (char *arg, int from_tty)
11801 {
11802 struct breakpoint *b, *prev;
11803 VEC(breakpoint_p) *found = 0;
11804 int ix;
11805 int default_match;
11806 struct symtabs_and_lines sals;
11807 struct symtab_and_line sal;
11808 int i;
11809 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
11810
11811 if (arg)
11812 {
11813 sals = decode_line_with_current_source (arg,
11814 (DECODE_LINE_FUNFIRSTLINE
11815 | DECODE_LINE_LIST_MODE));
11816 make_cleanup (xfree, sals.sals);
11817 default_match = 0;
11818 }
11819 else
11820 {
11821 sals.sals = (struct symtab_and_line *)
11822 xmalloc (sizeof (struct symtab_and_line));
11823 make_cleanup (xfree, sals.sals);
11824 init_sal (&sal); /* Initialize to zeroes. */
11825
11826 /* Set sal's line, symtab, pc, and pspace to the values
11827 corresponding to the last call to print_frame_info. If the
11828 codepoint is not valid, this will set all the fields to 0. */
11829 get_last_displayed_sal (&sal);
11830 if (sal.symtab == 0)
11831 error (_("No source file specified."));
11832
11833 sals.sals[0] = sal;
11834 sals.nelts = 1;
11835
11836 default_match = 1;
11837 }
11838
11839 /* We don't call resolve_sal_pc here. That's not as bad as it
11840 seems, because all existing breakpoints typically have both
11841 file/line and pc set. So, if clear is given file/line, we can
11842 match this to existing breakpoint without obtaining pc at all.
11843
11844 We only support clearing given the address explicitly
11845 present in breakpoint table. Say, we've set breakpoint
11846 at file:line. There were several PC values for that file:line,
11847 due to optimization, all in one block.
11848
11849 We've picked one PC value. If "clear" is issued with another
11850 PC corresponding to the same file:line, the breakpoint won't
11851 be cleared. We probably can still clear the breakpoint, but
11852 since the other PC value is never presented to user, user
11853 can only find it by guessing, and it does not seem important
11854 to support that. */
11855
11856 /* For each line spec given, delete bps which correspond to it. Do
11857 it in two passes, solely to preserve the current behavior that
11858 from_tty is forced true if we delete more than one
11859 breakpoint. */
11860
11861 found = NULL;
11862 make_cleanup (VEC_cleanup (breakpoint_p), &found);
11863 for (i = 0; i < sals.nelts; i++)
11864 {
11865 const char *sal_fullname;
11866
11867 /* If exact pc given, clear bpts at that pc.
11868 If line given (pc == 0), clear all bpts on specified line.
11869 If defaulting, clear all bpts on default line
11870 or at default pc.
11871
11872 defaulting sal.pc != 0 tests to do
11873
11874 0 1 pc
11875 1 1 pc _and_ line
11876 0 0 line
11877 1 0 <can't happen> */
11878
11879 sal = sals.sals[i];
11880 sal_fullname = (sal.symtab == NULL
11881 ? NULL : symtab_to_fullname (sal.symtab));
11882
11883 /* Find all matching breakpoints and add them to 'found'. */
11884 ALL_BREAKPOINTS (b)
11885 {
11886 int match = 0;
11887 /* Are we going to delete b? */
11888 if (b->type != bp_none && !is_watchpoint (b))
11889 {
11890 struct bp_location *loc = b->loc;
11891 for (; loc; loc = loc->next)
11892 {
11893 /* If the user specified file:line, don't allow a PC
11894 match. This matches historical gdb behavior. */
11895 int pc_match = (!sal.explicit_line
11896 && sal.pc
11897 && (loc->pspace == sal.pspace)
11898 && (loc->address == sal.pc)
11899 && (!section_is_overlay (loc->section)
11900 || loc->section == sal.section));
11901 int line_match = 0;
11902
11903 if ((default_match || sal.explicit_line)
11904 && loc->symtab != NULL
11905 && sal_fullname != NULL
11906 && sal.pspace == loc->pspace
11907 && loc->line_number == sal.line
11908 && filename_cmp (symtab_to_fullname (loc->symtab),
11909 sal_fullname) == 0)
11910 line_match = 1;
11911
11912 if (pc_match || line_match)
11913 {
11914 match = 1;
11915 break;
11916 }
11917 }
11918 }
11919
11920 if (match)
11921 VEC_safe_push(breakpoint_p, found, b);
11922 }
11923 }
11924
11925 /* Now go thru the 'found' chain and delete them. */
11926 if (VEC_empty(breakpoint_p, found))
11927 {
11928 if (arg)
11929 error (_("No breakpoint at %s."), arg);
11930 else
11931 error (_("No breakpoint at this line."));
11932 }
11933
11934 /* Remove duplicates from the vec. */
11935 qsort (VEC_address (breakpoint_p, found),
11936 VEC_length (breakpoint_p, found),
11937 sizeof (breakpoint_p),
11938 compare_breakpoints);
11939 prev = VEC_index (breakpoint_p, found, 0);
11940 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
11941 {
11942 if (b == prev)
11943 {
11944 VEC_ordered_remove (breakpoint_p, found, ix);
11945 --ix;
11946 }
11947 }
11948
11949 if (VEC_length(breakpoint_p, found) > 1)
11950 from_tty = 1; /* Always report if deleted more than one. */
11951 if (from_tty)
11952 {
11953 if (VEC_length(breakpoint_p, found) == 1)
11954 printf_unfiltered (_("Deleted breakpoint "));
11955 else
11956 printf_unfiltered (_("Deleted breakpoints "));
11957 }
11958
11959 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
11960 {
11961 if (from_tty)
11962 printf_unfiltered ("%d ", b->number);
11963 delete_breakpoint (b);
11964 }
11965 if (from_tty)
11966 putchar_unfiltered ('\n');
11967
11968 do_cleanups (cleanups);
11969 }
11970 \f
11971 /* Delete breakpoint in BS if they are `delete' breakpoints and
11972 all breakpoints that are marked for deletion, whether hit or not.
11973 This is called after any breakpoint is hit, or after errors. */
11974
11975 void
11976 breakpoint_auto_delete (bpstat bs)
11977 {
11978 struct breakpoint *b, *b_tmp;
11979
11980 for (; bs; bs = bs->next)
11981 if (bs->breakpoint_at
11982 && bs->breakpoint_at->disposition == disp_del
11983 && bs->stop)
11984 delete_breakpoint (bs->breakpoint_at);
11985
11986 ALL_BREAKPOINTS_SAFE (b, b_tmp)
11987 {
11988 if (b->disposition == disp_del_at_next_stop)
11989 delete_breakpoint (b);
11990 }
11991 }
11992
11993 /* A comparison function for bp_location AP and BP being interfaced to
11994 qsort. Sort elements primarily by their ADDRESS (no matter what
11995 does breakpoint_address_is_meaningful say for its OWNER),
11996 secondarily by ordering first permanent elements and
11997 terciarily just ensuring the array is sorted stable way despite
11998 qsort being an unstable algorithm. */
11999
12000 static int
12001 bp_location_compare (const void *ap, const void *bp)
12002 {
12003 struct bp_location *a = *(void **) ap;
12004 struct bp_location *b = *(void **) bp;
12005
12006 if (a->address != b->address)
12007 return (a->address > b->address) - (a->address < b->address);
12008
12009 /* Sort locations at the same address by their pspace number, keeping
12010 locations of the same inferior (in a multi-inferior environment)
12011 grouped. */
12012
12013 if (a->pspace->num != b->pspace->num)
12014 return ((a->pspace->num > b->pspace->num)
12015 - (a->pspace->num < b->pspace->num));
12016
12017 /* Sort permanent breakpoints first. */
12018 if (a->permanent != b->permanent)
12019 return (a->permanent < b->permanent) - (a->permanent > b->permanent);
12020
12021 /* Make the internal GDB representation stable across GDB runs
12022 where A and B memory inside GDB can differ. Breakpoint locations of
12023 the same type at the same address can be sorted in arbitrary order. */
12024
12025 if (a->owner->number != b->owner->number)
12026 return ((a->owner->number > b->owner->number)
12027 - (a->owner->number < b->owner->number));
12028
12029 return (a > b) - (a < b);
12030 }
12031
12032 /* Set bp_location_placed_address_before_address_max and
12033 bp_location_shadow_len_after_address_max according to the current
12034 content of the bp_location array. */
12035
12036 static void
12037 bp_location_target_extensions_update (void)
12038 {
12039 struct bp_location *bl, **blp_tmp;
12040
12041 bp_location_placed_address_before_address_max = 0;
12042 bp_location_shadow_len_after_address_max = 0;
12043
12044 ALL_BP_LOCATIONS (bl, blp_tmp)
12045 {
12046 CORE_ADDR start, end, addr;
12047
12048 if (!bp_location_has_shadow (bl))
12049 continue;
12050
12051 start = bl->target_info.placed_address;
12052 end = start + bl->target_info.shadow_len;
12053
12054 gdb_assert (bl->address >= start);
12055 addr = bl->address - start;
12056 if (addr > bp_location_placed_address_before_address_max)
12057 bp_location_placed_address_before_address_max = addr;
12058
12059 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
12060
12061 gdb_assert (bl->address < end);
12062 addr = end - bl->address;
12063 if (addr > bp_location_shadow_len_after_address_max)
12064 bp_location_shadow_len_after_address_max = addr;
12065 }
12066 }
12067
12068 /* Download tracepoint locations if they haven't been. */
12069
12070 static void
12071 download_tracepoint_locations (void)
12072 {
12073 struct breakpoint *b;
12074 struct cleanup *old_chain;
12075
12076 if (!target_can_download_tracepoint ())
12077 return;
12078
12079 old_chain = save_current_space_and_thread ();
12080
12081 ALL_TRACEPOINTS (b)
12082 {
12083 struct bp_location *bl;
12084 struct tracepoint *t;
12085 int bp_location_downloaded = 0;
12086
12087 if ((b->type == bp_fast_tracepoint
12088 ? !may_insert_fast_tracepoints
12089 : !may_insert_tracepoints))
12090 continue;
12091
12092 for (bl = b->loc; bl; bl = bl->next)
12093 {
12094 /* In tracepoint, locations are _never_ duplicated, so
12095 should_be_inserted is equivalent to
12096 unduplicated_should_be_inserted. */
12097 if (!should_be_inserted (bl) || bl->inserted)
12098 continue;
12099
12100 switch_to_program_space_and_thread (bl->pspace);
12101
12102 target_download_tracepoint (bl);
12103
12104 bl->inserted = 1;
12105 bp_location_downloaded = 1;
12106 }
12107 t = (struct tracepoint *) b;
12108 t->number_on_target = b->number;
12109 if (bp_location_downloaded)
12110 observer_notify_breakpoint_modified (b);
12111 }
12112
12113 do_cleanups (old_chain);
12114 }
12115
12116 /* Swap the insertion/duplication state between two locations. */
12117
12118 static void
12119 swap_insertion (struct bp_location *left, struct bp_location *right)
12120 {
12121 const int left_inserted = left->inserted;
12122 const int left_duplicate = left->duplicate;
12123 const int left_needs_update = left->needs_update;
12124 const struct bp_target_info left_target_info = left->target_info;
12125
12126 /* Locations of tracepoints can never be duplicated. */
12127 if (is_tracepoint (left->owner))
12128 gdb_assert (!left->duplicate);
12129 if (is_tracepoint (right->owner))
12130 gdb_assert (!right->duplicate);
12131
12132 left->inserted = right->inserted;
12133 left->duplicate = right->duplicate;
12134 left->needs_update = right->needs_update;
12135 left->target_info = right->target_info;
12136 right->inserted = left_inserted;
12137 right->duplicate = left_duplicate;
12138 right->needs_update = left_needs_update;
12139 right->target_info = left_target_info;
12140 }
12141
12142 /* Force the re-insertion of the locations at ADDRESS. This is called
12143 once a new/deleted/modified duplicate location is found and we are evaluating
12144 conditions on the target's side. Such conditions need to be updated on
12145 the target. */
12146
12147 static void
12148 force_breakpoint_reinsertion (struct bp_location *bl)
12149 {
12150 struct bp_location **locp = NULL, **loc2p;
12151 struct bp_location *loc;
12152 CORE_ADDR address = 0;
12153 int pspace_num;
12154
12155 address = bl->address;
12156 pspace_num = bl->pspace->num;
12157
12158 /* This is only meaningful if the target is
12159 evaluating conditions and if the user has
12160 opted for condition evaluation on the target's
12161 side. */
12162 if (gdb_evaluates_breakpoint_condition_p ()
12163 || !target_supports_evaluation_of_breakpoint_conditions ())
12164 return;
12165
12166 /* Flag all breakpoint locations with this address and
12167 the same program space as the location
12168 as "its condition has changed". We need to
12169 update the conditions on the target's side. */
12170 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
12171 {
12172 loc = *loc2p;
12173
12174 if (!is_breakpoint (loc->owner)
12175 || pspace_num != loc->pspace->num)
12176 continue;
12177
12178 /* Flag the location appropriately. We use a different state to
12179 let everyone know that we already updated the set of locations
12180 with addr bl->address and program space bl->pspace. This is so
12181 we don't have to keep calling these functions just to mark locations
12182 that have already been marked. */
12183 loc->condition_changed = condition_updated;
12184
12185 /* Free the agent expression bytecode as well. We will compute
12186 it later on. */
12187 if (loc->cond_bytecode)
12188 {
12189 free_agent_expr (loc->cond_bytecode);
12190 loc->cond_bytecode = NULL;
12191 }
12192 }
12193 }
12194 /* Called whether new breakpoints are created, or existing breakpoints
12195 deleted, to update the global location list and recompute which
12196 locations are duplicate of which.
12197
12198 The INSERT_MODE flag determines whether locations may not, may, or
12199 shall be inserted now. See 'enum ugll_insert_mode' for more
12200 info. */
12201
12202 static void
12203 update_global_location_list (enum ugll_insert_mode insert_mode)
12204 {
12205 struct breakpoint *b;
12206 struct bp_location **locp, *loc;
12207 struct cleanup *cleanups;
12208 /* Last breakpoint location address that was marked for update. */
12209 CORE_ADDR last_addr = 0;
12210 /* Last breakpoint location program space that was marked for update. */
12211 int last_pspace_num = -1;
12212
12213 /* Used in the duplicates detection below. When iterating over all
12214 bp_locations, points to the first bp_location of a given address.
12215 Breakpoints and watchpoints of different types are never
12216 duplicates of each other. Keep one pointer for each type of
12217 breakpoint/watchpoint, so we only need to loop over all locations
12218 once. */
12219 struct bp_location *bp_loc_first; /* breakpoint */
12220 struct bp_location *wp_loc_first; /* hardware watchpoint */
12221 struct bp_location *awp_loc_first; /* access watchpoint */
12222 struct bp_location *rwp_loc_first; /* read watchpoint */
12223
12224 /* Saved former bp_location array which we compare against the newly
12225 built bp_location from the current state of ALL_BREAKPOINTS. */
12226 struct bp_location **old_location, **old_locp;
12227 unsigned old_location_count;
12228
12229 old_location = bp_location;
12230 old_location_count = bp_location_count;
12231 bp_location = NULL;
12232 bp_location_count = 0;
12233 cleanups = make_cleanup (xfree, old_location);
12234
12235 ALL_BREAKPOINTS (b)
12236 for (loc = b->loc; loc; loc = loc->next)
12237 bp_location_count++;
12238
12239 bp_location = xmalloc (sizeof (*bp_location) * bp_location_count);
12240 locp = bp_location;
12241 ALL_BREAKPOINTS (b)
12242 for (loc = b->loc; loc; loc = loc->next)
12243 *locp++ = loc;
12244 qsort (bp_location, bp_location_count, sizeof (*bp_location),
12245 bp_location_compare);
12246
12247 bp_location_target_extensions_update ();
12248
12249 /* Identify bp_location instances that are no longer present in the
12250 new list, and therefore should be freed. Note that it's not
12251 necessary that those locations should be removed from inferior --
12252 if there's another location at the same address (previously
12253 marked as duplicate), we don't need to remove/insert the
12254 location.
12255
12256 LOCP is kept in sync with OLD_LOCP, each pointing to the current
12257 and former bp_location array state respectively. */
12258
12259 locp = bp_location;
12260 for (old_locp = old_location; old_locp < old_location + old_location_count;
12261 old_locp++)
12262 {
12263 struct bp_location *old_loc = *old_locp;
12264 struct bp_location **loc2p;
12265
12266 /* Tells if 'old_loc' is found among the new locations. If
12267 not, we have to free it. */
12268 int found_object = 0;
12269 /* Tells if the location should remain inserted in the target. */
12270 int keep_in_target = 0;
12271 int removed = 0;
12272
12273 /* Skip LOCP entries which will definitely never be needed.
12274 Stop either at or being the one matching OLD_LOC. */
12275 while (locp < bp_location + bp_location_count
12276 && (*locp)->address < old_loc->address)
12277 locp++;
12278
12279 for (loc2p = locp;
12280 (loc2p < bp_location + bp_location_count
12281 && (*loc2p)->address == old_loc->address);
12282 loc2p++)
12283 {
12284 /* Check if this is a new/duplicated location or a duplicated
12285 location that had its condition modified. If so, we want to send
12286 its condition to the target if evaluation of conditions is taking
12287 place there. */
12288 if ((*loc2p)->condition_changed == condition_modified
12289 && (last_addr != old_loc->address
12290 || last_pspace_num != old_loc->pspace->num))
12291 {
12292 force_breakpoint_reinsertion (*loc2p);
12293 last_pspace_num = old_loc->pspace->num;
12294 }
12295
12296 if (*loc2p == old_loc)
12297 found_object = 1;
12298 }
12299
12300 /* We have already handled this address, update it so that we don't
12301 have to go through updates again. */
12302 last_addr = old_loc->address;
12303
12304 /* Target-side condition evaluation: Handle deleted locations. */
12305 if (!found_object)
12306 force_breakpoint_reinsertion (old_loc);
12307
12308 /* If this location is no longer present, and inserted, look if
12309 there's maybe a new location at the same address. If so,
12310 mark that one inserted, and don't remove this one. This is
12311 needed so that we don't have a time window where a breakpoint
12312 at certain location is not inserted. */
12313
12314 if (old_loc->inserted)
12315 {
12316 /* If the location is inserted now, we might have to remove
12317 it. */
12318
12319 if (found_object && should_be_inserted (old_loc))
12320 {
12321 /* The location is still present in the location list,
12322 and still should be inserted. Don't do anything. */
12323 keep_in_target = 1;
12324 }
12325 else
12326 {
12327 /* This location still exists, but it won't be kept in the
12328 target since it may have been disabled. We proceed to
12329 remove its target-side condition. */
12330
12331 /* The location is either no longer present, or got
12332 disabled. See if there's another location at the
12333 same address, in which case we don't need to remove
12334 this one from the target. */
12335
12336 /* OLD_LOC comes from existing struct breakpoint. */
12337 if (breakpoint_address_is_meaningful (old_loc->owner))
12338 {
12339 for (loc2p = locp;
12340 (loc2p < bp_location + bp_location_count
12341 && (*loc2p)->address == old_loc->address);
12342 loc2p++)
12343 {
12344 struct bp_location *loc2 = *loc2p;
12345
12346 if (breakpoint_locations_match (loc2, old_loc))
12347 {
12348 /* Read watchpoint locations are switched to
12349 access watchpoints, if the former are not
12350 supported, but the latter are. */
12351 if (is_hardware_watchpoint (old_loc->owner))
12352 {
12353 gdb_assert (is_hardware_watchpoint (loc2->owner));
12354 loc2->watchpoint_type = old_loc->watchpoint_type;
12355 }
12356
12357 /* loc2 is a duplicated location. We need to check
12358 if it should be inserted in case it will be
12359 unduplicated. */
12360 if (loc2 != old_loc
12361 && unduplicated_should_be_inserted (loc2))
12362 {
12363 swap_insertion (old_loc, loc2);
12364 keep_in_target = 1;
12365 break;
12366 }
12367 }
12368 }
12369 }
12370 }
12371
12372 if (!keep_in_target)
12373 {
12374 if (remove_breakpoint (old_loc, mark_uninserted))
12375 {
12376 /* This is just about all we can do. We could keep
12377 this location on the global list, and try to
12378 remove it next time, but there's no particular
12379 reason why we will succeed next time.
12380
12381 Note that at this point, old_loc->owner is still
12382 valid, as delete_breakpoint frees the breakpoint
12383 only after calling us. */
12384 printf_filtered (_("warning: Error removing "
12385 "breakpoint %d\n"),
12386 old_loc->owner->number);
12387 }
12388 removed = 1;
12389 }
12390 }
12391
12392 if (!found_object)
12393 {
12394 if (removed && target_is_non_stop_p ()
12395 && need_moribund_for_location_type (old_loc))
12396 {
12397 /* This location was removed from the target. In
12398 non-stop mode, a race condition is possible where
12399 we've removed a breakpoint, but stop events for that
12400 breakpoint are already queued and will arrive later.
12401 We apply an heuristic to be able to distinguish such
12402 SIGTRAPs from other random SIGTRAPs: we keep this
12403 breakpoint location for a bit, and will retire it
12404 after we see some number of events. The theory here
12405 is that reporting of events should, "on the average",
12406 be fair, so after a while we'll see events from all
12407 threads that have anything of interest, and no longer
12408 need to keep this breakpoint location around. We
12409 don't hold locations forever so to reduce chances of
12410 mistaking a non-breakpoint SIGTRAP for a breakpoint
12411 SIGTRAP.
12412
12413 The heuristic failing can be disastrous on
12414 decr_pc_after_break targets.
12415
12416 On decr_pc_after_break targets, like e.g., x86-linux,
12417 if we fail to recognize a late breakpoint SIGTRAP,
12418 because events_till_retirement has reached 0 too
12419 soon, we'll fail to do the PC adjustment, and report
12420 a random SIGTRAP to the user. When the user resumes
12421 the inferior, it will most likely immediately crash
12422 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
12423 corrupted, because of being resumed e.g., in the
12424 middle of a multi-byte instruction, or skipped a
12425 one-byte instruction. This was actually seen happen
12426 on native x86-linux, and should be less rare on
12427 targets that do not support new thread events, like
12428 remote, due to the heuristic depending on
12429 thread_count.
12430
12431 Mistaking a random SIGTRAP for a breakpoint trap
12432 causes similar symptoms (PC adjustment applied when
12433 it shouldn't), but then again, playing with SIGTRAPs
12434 behind the debugger's back is asking for trouble.
12435
12436 Since hardware watchpoint traps are always
12437 distinguishable from other traps, so we don't need to
12438 apply keep hardware watchpoint moribund locations
12439 around. We simply always ignore hardware watchpoint
12440 traps we can no longer explain. */
12441
12442 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12443 old_loc->owner = NULL;
12444
12445 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
12446 }
12447 else
12448 {
12449 old_loc->owner = NULL;
12450 decref_bp_location (&old_loc);
12451 }
12452 }
12453 }
12454
12455 /* Rescan breakpoints at the same address and section, marking the
12456 first one as "first" and any others as "duplicates". This is so
12457 that the bpt instruction is only inserted once. If we have a
12458 permanent breakpoint at the same place as BPT, make that one the
12459 official one, and the rest as duplicates. Permanent breakpoints
12460 are sorted first for the same address.
12461
12462 Do the same for hardware watchpoints, but also considering the
12463 watchpoint's type (regular/access/read) and length. */
12464
12465 bp_loc_first = NULL;
12466 wp_loc_first = NULL;
12467 awp_loc_first = NULL;
12468 rwp_loc_first = NULL;
12469 ALL_BP_LOCATIONS (loc, locp)
12470 {
12471 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12472 non-NULL. */
12473 struct bp_location **loc_first_p;
12474 b = loc->owner;
12475
12476 if (!unduplicated_should_be_inserted (loc)
12477 || !breakpoint_address_is_meaningful (b)
12478 /* Don't detect duplicate for tracepoint locations because they are
12479 never duplicated. See the comments in field `duplicate' of
12480 `struct bp_location'. */
12481 || is_tracepoint (b))
12482 {
12483 /* Clear the condition modification flag. */
12484 loc->condition_changed = condition_unchanged;
12485 continue;
12486 }
12487
12488 if (b->type == bp_hardware_watchpoint)
12489 loc_first_p = &wp_loc_first;
12490 else if (b->type == bp_read_watchpoint)
12491 loc_first_p = &rwp_loc_first;
12492 else if (b->type == bp_access_watchpoint)
12493 loc_first_p = &awp_loc_first;
12494 else
12495 loc_first_p = &bp_loc_first;
12496
12497 if (*loc_first_p == NULL
12498 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12499 || !breakpoint_locations_match (loc, *loc_first_p))
12500 {
12501 *loc_first_p = loc;
12502 loc->duplicate = 0;
12503
12504 if (is_breakpoint (loc->owner) && loc->condition_changed)
12505 {
12506 loc->needs_update = 1;
12507 /* Clear the condition modification flag. */
12508 loc->condition_changed = condition_unchanged;
12509 }
12510 continue;
12511 }
12512
12513
12514 /* This and the above ensure the invariant that the first location
12515 is not duplicated, and is the inserted one.
12516 All following are marked as duplicated, and are not inserted. */
12517 if (loc->inserted)
12518 swap_insertion (loc, *loc_first_p);
12519 loc->duplicate = 1;
12520
12521 /* Clear the condition modification flag. */
12522 loc->condition_changed = condition_unchanged;
12523 }
12524
12525 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
12526 {
12527 if (insert_mode != UGLL_DONT_INSERT)
12528 insert_breakpoint_locations ();
12529 else
12530 {
12531 /* Even though the caller told us to not insert new
12532 locations, we may still need to update conditions on the
12533 target's side of breakpoints that were already inserted
12534 if the target is evaluating breakpoint conditions. We
12535 only update conditions for locations that are marked
12536 "needs_update". */
12537 update_inserted_breakpoint_locations ();
12538 }
12539 }
12540
12541 if (insert_mode != UGLL_DONT_INSERT)
12542 download_tracepoint_locations ();
12543
12544 do_cleanups (cleanups);
12545 }
12546
12547 void
12548 breakpoint_retire_moribund (void)
12549 {
12550 struct bp_location *loc;
12551 int ix;
12552
12553 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12554 if (--(loc->events_till_retirement) == 0)
12555 {
12556 decref_bp_location (&loc);
12557 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12558 --ix;
12559 }
12560 }
12561
12562 static void
12563 update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
12564 {
12565
12566 TRY
12567 {
12568 update_global_location_list (insert_mode);
12569 }
12570 CATCH (e, RETURN_MASK_ERROR)
12571 {
12572 }
12573 END_CATCH
12574 }
12575
12576 /* Clear BKP from a BPS. */
12577
12578 static void
12579 bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12580 {
12581 bpstat bs;
12582
12583 for (bs = bps; bs; bs = bs->next)
12584 if (bs->breakpoint_at == bpt)
12585 {
12586 bs->breakpoint_at = NULL;
12587 bs->old_val = NULL;
12588 /* bs->commands will be freed later. */
12589 }
12590 }
12591
12592 /* Callback for iterate_over_threads. */
12593 static int
12594 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12595 {
12596 struct breakpoint *bpt = data;
12597
12598 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12599 return 0;
12600 }
12601
12602 /* Helper for breakpoint and tracepoint breakpoint_ops->mention
12603 callbacks. */
12604
12605 static void
12606 say_where (struct breakpoint *b)
12607 {
12608 struct value_print_options opts;
12609
12610 get_user_print_options (&opts);
12611
12612 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12613 single string. */
12614 if (b->loc == NULL)
12615 {
12616 /* For pending locations, the output differs slightly based
12617 on b->extra_string. If this is non-NULL, it contains either
12618 a condition or dprintf arguments. */
12619 if (b->extra_string == NULL)
12620 {
12621 printf_filtered (_(" (%s) pending."),
12622 event_location_to_string (b->location));
12623 }
12624 else if (b->type == bp_dprintf)
12625 {
12626 printf_filtered (_(" (%s,%s) pending."),
12627 event_location_to_string (b->location),
12628 b->extra_string);
12629 }
12630 else
12631 {
12632 printf_filtered (_(" (%s %s) pending."),
12633 event_location_to_string (b->location),
12634 b->extra_string);
12635 }
12636 }
12637 else
12638 {
12639 if (opts.addressprint || b->loc->symtab == NULL)
12640 {
12641 printf_filtered (" at ");
12642 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12643 gdb_stdout);
12644 }
12645 if (b->loc->symtab != NULL)
12646 {
12647 /* If there is a single location, we can print the location
12648 more nicely. */
12649 if (b->loc->next == NULL)
12650 printf_filtered (": file %s, line %d.",
12651 symtab_to_filename_for_display (b->loc->symtab),
12652 b->loc->line_number);
12653 else
12654 /* This is not ideal, but each location may have a
12655 different file name, and this at least reflects the
12656 real situation somewhat. */
12657 printf_filtered (": %s.",
12658 event_location_to_string (b->location));
12659 }
12660
12661 if (b->loc->next)
12662 {
12663 struct bp_location *loc = b->loc;
12664 int n = 0;
12665 for (; loc; loc = loc->next)
12666 ++n;
12667 printf_filtered (" (%d locations)", n);
12668 }
12669 }
12670 }
12671
12672 /* Default bp_location_ops methods. */
12673
12674 static void
12675 bp_location_dtor (struct bp_location *self)
12676 {
12677 xfree (self->cond);
12678 if (self->cond_bytecode)
12679 free_agent_expr (self->cond_bytecode);
12680 xfree (self->function_name);
12681
12682 VEC_free (agent_expr_p, self->target_info.conditions);
12683 VEC_free (agent_expr_p, self->target_info.tcommands);
12684 }
12685
12686 static const struct bp_location_ops bp_location_ops =
12687 {
12688 bp_location_dtor
12689 };
12690
12691 /* Default breakpoint_ops methods all breakpoint_ops ultimately
12692 inherit from. */
12693
12694 static void
12695 base_breakpoint_dtor (struct breakpoint *self)
12696 {
12697 decref_counted_command_line (&self->commands);
12698 xfree (self->cond_string);
12699 xfree (self->extra_string);
12700 xfree (self->filter);
12701 delete_event_location (self->location);
12702 delete_event_location (self->location_range_end);
12703 }
12704
12705 static struct bp_location *
12706 base_breakpoint_allocate_location (struct breakpoint *self)
12707 {
12708 struct bp_location *loc;
12709
12710 loc = XNEW (struct bp_location);
12711 init_bp_location (loc, &bp_location_ops, self);
12712 return loc;
12713 }
12714
12715 static void
12716 base_breakpoint_re_set (struct breakpoint *b)
12717 {
12718 /* Nothing to re-set. */
12719 }
12720
12721 #define internal_error_pure_virtual_called() \
12722 gdb_assert_not_reached ("pure virtual function called")
12723
12724 static int
12725 base_breakpoint_insert_location (struct bp_location *bl)
12726 {
12727 internal_error_pure_virtual_called ();
12728 }
12729
12730 static int
12731 base_breakpoint_remove_location (struct bp_location *bl)
12732 {
12733 internal_error_pure_virtual_called ();
12734 }
12735
12736 static int
12737 base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12738 struct address_space *aspace,
12739 CORE_ADDR bp_addr,
12740 const struct target_waitstatus *ws)
12741 {
12742 internal_error_pure_virtual_called ();
12743 }
12744
12745 static void
12746 base_breakpoint_check_status (bpstat bs)
12747 {
12748 /* Always stop. */
12749 }
12750
12751 /* A "works_in_software_mode" breakpoint_ops method that just internal
12752 errors. */
12753
12754 static int
12755 base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12756 {
12757 internal_error_pure_virtual_called ();
12758 }
12759
12760 /* A "resources_needed" breakpoint_ops method that just internal
12761 errors. */
12762
12763 static int
12764 base_breakpoint_resources_needed (const struct bp_location *bl)
12765 {
12766 internal_error_pure_virtual_called ();
12767 }
12768
12769 static enum print_stop_action
12770 base_breakpoint_print_it (bpstat bs)
12771 {
12772 internal_error_pure_virtual_called ();
12773 }
12774
12775 static void
12776 base_breakpoint_print_one_detail (const struct breakpoint *self,
12777 struct ui_out *uiout)
12778 {
12779 /* nothing */
12780 }
12781
12782 static void
12783 base_breakpoint_print_mention (struct breakpoint *b)
12784 {
12785 internal_error_pure_virtual_called ();
12786 }
12787
12788 static void
12789 base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12790 {
12791 internal_error_pure_virtual_called ();
12792 }
12793
12794 static void
12795 base_breakpoint_create_sals_from_location
12796 (const struct event_location *location,
12797 struct linespec_result *canonical,
12798 enum bptype type_wanted)
12799 {
12800 internal_error_pure_virtual_called ();
12801 }
12802
12803 static void
12804 base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12805 struct linespec_result *c,
12806 char *cond_string,
12807 char *extra_string,
12808 enum bptype type_wanted,
12809 enum bpdisp disposition,
12810 int thread,
12811 int task, int ignore_count,
12812 const struct breakpoint_ops *o,
12813 int from_tty, int enabled,
12814 int internal, unsigned flags)
12815 {
12816 internal_error_pure_virtual_called ();
12817 }
12818
12819 static void
12820 base_breakpoint_decode_location (struct breakpoint *b,
12821 const struct event_location *location,
12822 struct symtabs_and_lines *sals)
12823 {
12824 internal_error_pure_virtual_called ();
12825 }
12826
12827 /* The default 'explains_signal' method. */
12828
12829 static int
12830 base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
12831 {
12832 return 1;
12833 }
12834
12835 /* The default "after_condition_true" method. */
12836
12837 static void
12838 base_breakpoint_after_condition_true (struct bpstats *bs)
12839 {
12840 /* Nothing to do. */
12841 }
12842
12843 struct breakpoint_ops base_breakpoint_ops =
12844 {
12845 base_breakpoint_dtor,
12846 base_breakpoint_allocate_location,
12847 base_breakpoint_re_set,
12848 base_breakpoint_insert_location,
12849 base_breakpoint_remove_location,
12850 base_breakpoint_breakpoint_hit,
12851 base_breakpoint_check_status,
12852 base_breakpoint_resources_needed,
12853 base_breakpoint_works_in_software_mode,
12854 base_breakpoint_print_it,
12855 NULL,
12856 base_breakpoint_print_one_detail,
12857 base_breakpoint_print_mention,
12858 base_breakpoint_print_recreate,
12859 base_breakpoint_create_sals_from_location,
12860 base_breakpoint_create_breakpoints_sal,
12861 base_breakpoint_decode_location,
12862 base_breakpoint_explains_signal,
12863 base_breakpoint_after_condition_true,
12864 };
12865
12866 /* Default breakpoint_ops methods. */
12867
12868 static void
12869 bkpt_re_set (struct breakpoint *b)
12870 {
12871 /* FIXME: is this still reachable? */
12872 if (event_location_empty_p (b->location))
12873 {
12874 /* Anything without a location can't be re-set. */
12875 delete_breakpoint (b);
12876 return;
12877 }
12878
12879 breakpoint_re_set_default (b);
12880 }
12881
12882 static int
12883 bkpt_insert_location (struct bp_location *bl)
12884 {
12885 if (bl->loc_type == bp_loc_hardware_breakpoint)
12886 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
12887 else
12888 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
12889 }
12890
12891 static int
12892 bkpt_remove_location (struct bp_location *bl)
12893 {
12894 if (bl->loc_type == bp_loc_hardware_breakpoint)
12895 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12896 else
12897 return target_remove_breakpoint (bl->gdbarch, &bl->target_info);
12898 }
12899
12900 static int
12901 bkpt_breakpoint_hit (const struct bp_location *bl,
12902 struct address_space *aspace, CORE_ADDR bp_addr,
12903 const struct target_waitstatus *ws)
12904 {
12905 if (ws->kind != TARGET_WAITKIND_STOPPED
12906 || ws->value.sig != GDB_SIGNAL_TRAP)
12907 return 0;
12908
12909 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12910 aspace, bp_addr))
12911 return 0;
12912
12913 if (overlay_debugging /* unmapped overlay section */
12914 && section_is_overlay (bl->section)
12915 && !section_is_mapped (bl->section))
12916 return 0;
12917
12918 return 1;
12919 }
12920
12921 static int
12922 dprintf_breakpoint_hit (const struct bp_location *bl,
12923 struct address_space *aspace, CORE_ADDR bp_addr,
12924 const struct target_waitstatus *ws)
12925 {
12926 if (dprintf_style == dprintf_style_agent
12927 && target_can_run_breakpoint_commands ())
12928 {
12929 /* An agent-style dprintf never causes a stop. If we see a trap
12930 for this address it must be for a breakpoint that happens to
12931 be set at the same address. */
12932 return 0;
12933 }
12934
12935 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
12936 }
12937
12938 static int
12939 bkpt_resources_needed (const struct bp_location *bl)
12940 {
12941 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12942
12943 return 1;
12944 }
12945
12946 static enum print_stop_action
12947 bkpt_print_it (bpstat bs)
12948 {
12949 struct breakpoint *b;
12950 const struct bp_location *bl;
12951 int bp_temp;
12952 struct ui_out *uiout = current_uiout;
12953
12954 gdb_assert (bs->bp_location_at != NULL);
12955
12956 bl = bs->bp_location_at;
12957 b = bs->breakpoint_at;
12958
12959 bp_temp = b->disposition == disp_del;
12960 if (bl->address != bl->requested_address)
12961 breakpoint_adjustment_warning (bl->requested_address,
12962 bl->address,
12963 b->number, 1);
12964 annotate_breakpoint (b->number);
12965 if (bp_temp)
12966 ui_out_text (uiout, "\nTemporary breakpoint ");
12967 else
12968 ui_out_text (uiout, "\nBreakpoint ");
12969 if (ui_out_is_mi_like_p (uiout))
12970 {
12971 ui_out_field_string (uiout, "reason",
12972 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
12973 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
12974 }
12975 ui_out_field_int (uiout, "bkptno", b->number);
12976 ui_out_text (uiout, ", ");
12977
12978 return PRINT_SRC_AND_LOC;
12979 }
12980
12981 static void
12982 bkpt_print_mention (struct breakpoint *b)
12983 {
12984 if (ui_out_is_mi_like_p (current_uiout))
12985 return;
12986
12987 switch (b->type)
12988 {
12989 case bp_breakpoint:
12990 case bp_gnu_ifunc_resolver:
12991 if (b->disposition == disp_del)
12992 printf_filtered (_("Temporary breakpoint"));
12993 else
12994 printf_filtered (_("Breakpoint"));
12995 printf_filtered (_(" %d"), b->number);
12996 if (b->type == bp_gnu_ifunc_resolver)
12997 printf_filtered (_(" at gnu-indirect-function resolver"));
12998 break;
12999 case bp_hardware_breakpoint:
13000 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
13001 break;
13002 case bp_dprintf:
13003 printf_filtered (_("Dprintf %d"), b->number);
13004 break;
13005 }
13006
13007 say_where (b);
13008 }
13009
13010 static void
13011 bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13012 {
13013 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
13014 fprintf_unfiltered (fp, "tbreak");
13015 else if (tp->type == bp_breakpoint)
13016 fprintf_unfiltered (fp, "break");
13017 else if (tp->type == bp_hardware_breakpoint
13018 && tp->disposition == disp_del)
13019 fprintf_unfiltered (fp, "thbreak");
13020 else if (tp->type == bp_hardware_breakpoint)
13021 fprintf_unfiltered (fp, "hbreak");
13022 else
13023 internal_error (__FILE__, __LINE__,
13024 _("unhandled breakpoint type %d"), (int) tp->type);
13025
13026 fprintf_unfiltered (fp, " %s",
13027 event_location_to_string (tp->location));
13028
13029 /* Print out extra_string if this breakpoint is pending. It might
13030 contain, for example, conditions that were set by the user. */
13031 if (tp->loc == NULL && tp->extra_string != NULL)
13032 fprintf_unfiltered (fp, " %s", tp->extra_string);
13033
13034 print_recreate_thread (tp, fp);
13035 }
13036
13037 static void
13038 bkpt_create_sals_from_location (const struct event_location *location,
13039 struct linespec_result *canonical,
13040 enum bptype type_wanted)
13041 {
13042 create_sals_from_location_default (location, canonical, type_wanted);
13043 }
13044
13045 static void
13046 bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
13047 struct linespec_result *canonical,
13048 char *cond_string,
13049 char *extra_string,
13050 enum bptype type_wanted,
13051 enum bpdisp disposition,
13052 int thread,
13053 int task, int ignore_count,
13054 const struct breakpoint_ops *ops,
13055 int from_tty, int enabled,
13056 int internal, unsigned flags)
13057 {
13058 create_breakpoints_sal_default (gdbarch, canonical,
13059 cond_string, extra_string,
13060 type_wanted,
13061 disposition, thread, task,
13062 ignore_count, ops, from_tty,
13063 enabled, internal, flags);
13064 }
13065
13066 static void
13067 bkpt_decode_location (struct breakpoint *b,
13068 const struct event_location *location,
13069 struct symtabs_and_lines *sals)
13070 {
13071 decode_location_default (b, location, sals);
13072 }
13073
13074 /* Virtual table for internal breakpoints. */
13075
13076 static void
13077 internal_bkpt_re_set (struct breakpoint *b)
13078 {
13079 switch (b->type)
13080 {
13081 /* Delete overlay event and longjmp master breakpoints; they
13082 will be reset later by breakpoint_re_set. */
13083 case bp_overlay_event:
13084 case bp_longjmp_master:
13085 case bp_std_terminate_master:
13086 case bp_exception_master:
13087 delete_breakpoint (b);
13088 break;
13089
13090 /* This breakpoint is special, it's set up when the inferior
13091 starts and we really don't want to touch it. */
13092 case bp_shlib_event:
13093
13094 /* Like bp_shlib_event, this breakpoint type is special. Once
13095 it is set up, we do not want to touch it. */
13096 case bp_thread_event:
13097 break;
13098 }
13099 }
13100
13101 static void
13102 internal_bkpt_check_status (bpstat bs)
13103 {
13104 if (bs->breakpoint_at->type == bp_shlib_event)
13105 {
13106 /* If requested, stop when the dynamic linker notifies GDB of
13107 events. This allows the user to get control and place
13108 breakpoints in initializer routines for dynamically loaded
13109 objects (among other things). */
13110 bs->stop = stop_on_solib_events;
13111 bs->print = stop_on_solib_events;
13112 }
13113 else
13114 bs->stop = 0;
13115 }
13116
13117 static enum print_stop_action
13118 internal_bkpt_print_it (bpstat bs)
13119 {
13120 struct breakpoint *b;
13121
13122 b = bs->breakpoint_at;
13123
13124 switch (b->type)
13125 {
13126 case bp_shlib_event:
13127 /* Did we stop because the user set the stop_on_solib_events
13128 variable? (If so, we report this as a generic, "Stopped due
13129 to shlib event" message.) */
13130 print_solib_event (0);
13131 break;
13132
13133 case bp_thread_event:
13134 /* Not sure how we will get here.
13135 GDB should not stop for these breakpoints. */
13136 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
13137 break;
13138
13139 case bp_overlay_event:
13140 /* By analogy with the thread event, GDB should not stop for these. */
13141 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
13142 break;
13143
13144 case bp_longjmp_master:
13145 /* These should never be enabled. */
13146 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
13147 break;
13148
13149 case bp_std_terminate_master:
13150 /* These should never be enabled. */
13151 printf_filtered (_("std::terminate Master Breakpoint: "
13152 "gdb should not stop!\n"));
13153 break;
13154
13155 case bp_exception_master:
13156 /* These should never be enabled. */
13157 printf_filtered (_("Exception Master Breakpoint: "
13158 "gdb should not stop!\n"));
13159 break;
13160 }
13161
13162 return PRINT_NOTHING;
13163 }
13164
13165 static void
13166 internal_bkpt_print_mention (struct breakpoint *b)
13167 {
13168 /* Nothing to mention. These breakpoints are internal. */
13169 }
13170
13171 /* Virtual table for momentary breakpoints */
13172
13173 static void
13174 momentary_bkpt_re_set (struct breakpoint *b)
13175 {
13176 /* Keep temporary breakpoints, which can be encountered when we step
13177 over a dlopen call and solib_add is resetting the breakpoints.
13178 Otherwise these should have been blown away via the cleanup chain
13179 or by breakpoint_init_inferior when we rerun the executable. */
13180 }
13181
13182 static void
13183 momentary_bkpt_check_status (bpstat bs)
13184 {
13185 /* Nothing. The point of these breakpoints is causing a stop. */
13186 }
13187
13188 static enum print_stop_action
13189 momentary_bkpt_print_it (bpstat bs)
13190 {
13191 struct ui_out *uiout = current_uiout;
13192
13193 if (ui_out_is_mi_like_p (uiout))
13194 {
13195 struct breakpoint *b = bs->breakpoint_at;
13196
13197 switch (b->type)
13198 {
13199 case bp_finish:
13200 ui_out_field_string
13201 (uiout, "reason",
13202 async_reason_lookup (EXEC_ASYNC_FUNCTION_FINISHED));
13203 break;
13204
13205 case bp_until:
13206 ui_out_field_string
13207 (uiout, "reason",
13208 async_reason_lookup (EXEC_ASYNC_LOCATION_REACHED));
13209 break;
13210 }
13211 }
13212
13213 return PRINT_UNKNOWN;
13214 }
13215
13216 static void
13217 momentary_bkpt_print_mention (struct breakpoint *b)
13218 {
13219 /* Nothing to mention. These breakpoints are internal. */
13220 }
13221
13222 /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
13223
13224 It gets cleared already on the removal of the first one of such placed
13225 breakpoints. This is OK as they get all removed altogether. */
13226
13227 static void
13228 longjmp_bkpt_dtor (struct breakpoint *self)
13229 {
13230 struct thread_info *tp = find_thread_id (self->thread);
13231
13232 if (tp)
13233 tp->initiating_frame = null_frame_id;
13234
13235 momentary_breakpoint_ops.dtor (self);
13236 }
13237
13238 /* Specific methods for probe breakpoints. */
13239
13240 static int
13241 bkpt_probe_insert_location (struct bp_location *bl)
13242 {
13243 int v = bkpt_insert_location (bl);
13244
13245 if (v == 0)
13246 {
13247 /* The insertion was successful, now let's set the probe's semaphore
13248 if needed. */
13249 if (bl->probe.probe->pops->set_semaphore != NULL)
13250 bl->probe.probe->pops->set_semaphore (bl->probe.probe,
13251 bl->probe.objfile,
13252 bl->gdbarch);
13253 }
13254
13255 return v;
13256 }
13257
13258 static int
13259 bkpt_probe_remove_location (struct bp_location *bl)
13260 {
13261 /* Let's clear the semaphore before removing the location. */
13262 if (bl->probe.probe->pops->clear_semaphore != NULL)
13263 bl->probe.probe->pops->clear_semaphore (bl->probe.probe,
13264 bl->probe.objfile,
13265 bl->gdbarch);
13266
13267 return bkpt_remove_location (bl);
13268 }
13269
13270 static void
13271 bkpt_probe_create_sals_from_location (const struct event_location *location,
13272 struct linespec_result *canonical,
13273 enum bptype type_wanted)
13274 {
13275 struct linespec_sals lsal;
13276
13277 lsal.sals = parse_probes (location, canonical);
13278 lsal.canonical = xstrdup (event_location_to_string (canonical->location));
13279 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13280 }
13281
13282 static void
13283 bkpt_probe_decode_location (struct breakpoint *b,
13284 const struct event_location *location,
13285 struct symtabs_and_lines *sals)
13286 {
13287 *sals = parse_probes (location, NULL);
13288 if (!sals->sals)
13289 error (_("probe not found"));
13290 }
13291
13292 /* The breakpoint_ops structure to be used in tracepoints. */
13293
13294 static void
13295 tracepoint_re_set (struct breakpoint *b)
13296 {
13297 breakpoint_re_set_default (b);
13298 }
13299
13300 static int
13301 tracepoint_breakpoint_hit (const struct bp_location *bl,
13302 struct address_space *aspace, CORE_ADDR bp_addr,
13303 const struct target_waitstatus *ws)
13304 {
13305 /* By definition, the inferior does not report stops at
13306 tracepoints. */
13307 return 0;
13308 }
13309
13310 static void
13311 tracepoint_print_one_detail (const struct breakpoint *self,
13312 struct ui_out *uiout)
13313 {
13314 struct tracepoint *tp = (struct tracepoint *) self;
13315 if (tp->static_trace_marker_id)
13316 {
13317 gdb_assert (self->type == bp_static_tracepoint);
13318
13319 ui_out_text (uiout, "\tmarker id is ");
13320 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
13321 tp->static_trace_marker_id);
13322 ui_out_text (uiout, "\n");
13323 }
13324 }
13325
13326 static void
13327 tracepoint_print_mention (struct breakpoint *b)
13328 {
13329 if (ui_out_is_mi_like_p (current_uiout))
13330 return;
13331
13332 switch (b->type)
13333 {
13334 case bp_tracepoint:
13335 printf_filtered (_("Tracepoint"));
13336 printf_filtered (_(" %d"), b->number);
13337 break;
13338 case bp_fast_tracepoint:
13339 printf_filtered (_("Fast tracepoint"));
13340 printf_filtered (_(" %d"), b->number);
13341 break;
13342 case bp_static_tracepoint:
13343 printf_filtered (_("Static tracepoint"));
13344 printf_filtered (_(" %d"), b->number);
13345 break;
13346 default:
13347 internal_error (__FILE__, __LINE__,
13348 _("unhandled tracepoint type %d"), (int) b->type);
13349 }
13350
13351 say_where (b);
13352 }
13353
13354 static void
13355 tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
13356 {
13357 struct tracepoint *tp = (struct tracepoint *) self;
13358
13359 if (self->type == bp_fast_tracepoint)
13360 fprintf_unfiltered (fp, "ftrace");
13361 if (self->type == bp_static_tracepoint)
13362 fprintf_unfiltered (fp, "strace");
13363 else if (self->type == bp_tracepoint)
13364 fprintf_unfiltered (fp, "trace");
13365 else
13366 internal_error (__FILE__, __LINE__,
13367 _("unhandled tracepoint type %d"), (int) self->type);
13368
13369 fprintf_unfiltered (fp, " %s",
13370 event_location_to_string (self->location));
13371 print_recreate_thread (self, fp);
13372
13373 if (tp->pass_count)
13374 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
13375 }
13376
13377 static void
13378 tracepoint_create_sals_from_location (const struct event_location *location,
13379 struct linespec_result *canonical,
13380 enum bptype type_wanted)
13381 {
13382 create_sals_from_location_default (location, canonical, type_wanted);
13383 }
13384
13385 static void
13386 tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13387 struct linespec_result *canonical,
13388 char *cond_string,
13389 char *extra_string,
13390 enum bptype type_wanted,
13391 enum bpdisp disposition,
13392 int thread,
13393 int task, int ignore_count,
13394 const struct breakpoint_ops *ops,
13395 int from_tty, int enabled,
13396 int internal, unsigned flags)
13397 {
13398 create_breakpoints_sal_default (gdbarch, canonical,
13399 cond_string, extra_string,
13400 type_wanted,
13401 disposition, thread, task,
13402 ignore_count, ops, from_tty,
13403 enabled, internal, flags);
13404 }
13405
13406 static void
13407 tracepoint_decode_location (struct breakpoint *b,
13408 const struct event_location *location,
13409 struct symtabs_and_lines *sals)
13410 {
13411 decode_location_default (b, location, sals);
13412 }
13413
13414 struct breakpoint_ops tracepoint_breakpoint_ops;
13415
13416 /* The breakpoint_ops structure to be use on tracepoints placed in a
13417 static probe. */
13418
13419 static void
13420 tracepoint_probe_create_sals_from_location
13421 (const struct event_location *location,
13422 struct linespec_result *canonical,
13423 enum bptype type_wanted)
13424 {
13425 /* We use the same method for breakpoint on probes. */
13426 bkpt_probe_create_sals_from_location (location, canonical, type_wanted);
13427 }
13428
13429 static void
13430 tracepoint_probe_decode_location (struct breakpoint *b,
13431 const struct event_location *location,
13432 struct symtabs_and_lines *sals)
13433 {
13434 /* We use the same method for breakpoint on probes. */
13435 bkpt_probe_decode_location (b, location, sals);
13436 }
13437
13438 static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13439
13440 /* Dprintf breakpoint_ops methods. */
13441
13442 static void
13443 dprintf_re_set (struct breakpoint *b)
13444 {
13445 breakpoint_re_set_default (b);
13446
13447 /* extra_string should never be non-NULL for dprintf. */
13448 gdb_assert (b->extra_string != NULL);
13449
13450 /* 1 - connect to target 1, that can run breakpoint commands.
13451 2 - create a dprintf, which resolves fine.
13452 3 - disconnect from target 1
13453 4 - connect to target 2, that can NOT run breakpoint commands.
13454
13455 After steps #3/#4, you'll want the dprintf command list to
13456 be updated, because target 1 and 2 may well return different
13457 answers for target_can_run_breakpoint_commands().
13458 Given absence of finer grained resetting, we get to do
13459 it all the time. */
13460 if (b->extra_string != NULL)
13461 update_dprintf_command_list (b);
13462 }
13463
13464 /* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13465
13466 static void
13467 dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13468 {
13469 fprintf_unfiltered (fp, "dprintf %s,%s",
13470 event_location_to_string (tp->location),
13471 tp->extra_string);
13472 print_recreate_thread (tp, fp);
13473 }
13474
13475 /* Implement the "after_condition_true" breakpoint_ops method for
13476 dprintf.
13477
13478 dprintf's are implemented with regular commands in their command
13479 list, but we run the commands here instead of before presenting the
13480 stop to the user, as dprintf's don't actually cause a stop. This
13481 also makes it so that the commands of multiple dprintfs at the same
13482 address are all handled. */
13483
13484 static void
13485 dprintf_after_condition_true (struct bpstats *bs)
13486 {
13487 struct cleanup *old_chain;
13488 struct bpstats tmp_bs = { NULL };
13489 struct bpstats *tmp_bs_p = &tmp_bs;
13490
13491 /* dprintf's never cause a stop. This wasn't set in the
13492 check_status hook instead because that would make the dprintf's
13493 condition not be evaluated. */
13494 bs->stop = 0;
13495
13496 /* Run the command list here. Take ownership of it instead of
13497 copying. We never want these commands to run later in
13498 bpstat_do_actions, if a breakpoint that causes a stop happens to
13499 be set at same address as this dprintf, or even if running the
13500 commands here throws. */
13501 tmp_bs.commands = bs->commands;
13502 bs->commands = NULL;
13503 old_chain = make_cleanup_decref_counted_command_line (&tmp_bs.commands);
13504
13505 bpstat_do_actions_1 (&tmp_bs_p);
13506
13507 /* 'tmp_bs.commands' will usually be NULL by now, but
13508 bpstat_do_actions_1 may return early without processing the whole
13509 list. */
13510 do_cleanups (old_chain);
13511 }
13512
13513 /* The breakpoint_ops structure to be used on static tracepoints with
13514 markers (`-m'). */
13515
13516 static void
13517 strace_marker_create_sals_from_location (const struct event_location *location,
13518 struct linespec_result *canonical,
13519 enum bptype type_wanted)
13520 {
13521 struct linespec_sals lsal;
13522 const char *arg_start, *arg;
13523 char *str;
13524 struct cleanup *cleanup;
13525
13526 arg = arg_start = get_linespec_location (location);
13527 lsal.sals = decode_static_tracepoint_spec (&arg);
13528
13529 str = savestring (arg_start, arg - arg_start);
13530 cleanup = make_cleanup (xfree, str);
13531 canonical->location = new_linespec_location (&str);
13532 do_cleanups (cleanup);
13533
13534 lsal.canonical = xstrdup (event_location_to_string (canonical->location));
13535 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13536 }
13537
13538 static void
13539 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13540 struct linespec_result *canonical,
13541 char *cond_string,
13542 char *extra_string,
13543 enum bptype type_wanted,
13544 enum bpdisp disposition,
13545 int thread,
13546 int task, int ignore_count,
13547 const struct breakpoint_ops *ops,
13548 int from_tty, int enabled,
13549 int internal, unsigned flags)
13550 {
13551 int i;
13552 struct linespec_sals *lsal = VEC_index (linespec_sals,
13553 canonical->sals, 0);
13554
13555 /* If the user is creating a static tracepoint by marker id
13556 (strace -m MARKER_ID), then store the sals index, so that
13557 breakpoint_re_set can try to match up which of the newly
13558 found markers corresponds to this one, and, don't try to
13559 expand multiple locations for each sal, given than SALS
13560 already should contain all sals for MARKER_ID. */
13561
13562 for (i = 0; i < lsal->sals.nelts; ++i)
13563 {
13564 struct symtabs_and_lines expanded;
13565 struct tracepoint *tp;
13566 struct cleanup *old_chain;
13567 struct event_location *location;
13568
13569 expanded.nelts = 1;
13570 expanded.sals = &lsal->sals.sals[i];
13571
13572 location = copy_event_location (canonical->location);
13573 old_chain = make_cleanup_delete_event_location (location);
13574
13575 tp = XCNEW (struct tracepoint);
13576 init_breakpoint_sal (&tp->base, gdbarch, expanded,
13577 location, NULL,
13578 cond_string, extra_string,
13579 type_wanted, disposition,
13580 thread, task, ignore_count, ops,
13581 from_tty, enabled, internal, flags,
13582 canonical->special_display);
13583 /* Given that its possible to have multiple markers with
13584 the same string id, if the user is creating a static
13585 tracepoint by marker id ("strace -m MARKER_ID"), then
13586 store the sals index, so that breakpoint_re_set can
13587 try to match up which of the newly found markers
13588 corresponds to this one */
13589 tp->static_trace_marker_id_idx = i;
13590
13591 install_breakpoint (internal, &tp->base, 0);
13592
13593 discard_cleanups (old_chain);
13594 }
13595 }
13596
13597 static void
13598 strace_marker_decode_location (struct breakpoint *b,
13599 const struct event_location *location,
13600 struct symtabs_and_lines *sals)
13601 {
13602 struct tracepoint *tp = (struct tracepoint *) b;
13603 const char *s = get_linespec_location (location);
13604
13605 *sals = decode_static_tracepoint_spec (&s);
13606 if (sals->nelts > tp->static_trace_marker_id_idx)
13607 {
13608 sals->sals[0] = sals->sals[tp->static_trace_marker_id_idx];
13609 sals->nelts = 1;
13610 }
13611 else
13612 error (_("marker %s not found"), tp->static_trace_marker_id);
13613 }
13614
13615 static struct breakpoint_ops strace_marker_breakpoint_ops;
13616
13617 static int
13618 strace_marker_p (struct breakpoint *b)
13619 {
13620 return b->ops == &strace_marker_breakpoint_ops;
13621 }
13622
13623 /* Delete a breakpoint and clean up all traces of it in the data
13624 structures. */
13625
13626 void
13627 delete_breakpoint (struct breakpoint *bpt)
13628 {
13629 struct breakpoint *b;
13630
13631 gdb_assert (bpt != NULL);
13632
13633 /* Has this bp already been deleted? This can happen because
13634 multiple lists can hold pointers to bp's. bpstat lists are
13635 especial culprits.
13636
13637 One example of this happening is a watchpoint's scope bp. When
13638 the scope bp triggers, we notice that the watchpoint is out of
13639 scope, and delete it. We also delete its scope bp. But the
13640 scope bp is marked "auto-deleting", and is already on a bpstat.
13641 That bpstat is then checked for auto-deleting bp's, which are
13642 deleted.
13643
13644 A real solution to this problem might involve reference counts in
13645 bp's, and/or giving them pointers back to their referencing
13646 bpstat's, and teaching delete_breakpoint to only free a bp's
13647 storage when no more references were extent. A cheaper bandaid
13648 was chosen. */
13649 if (bpt->type == bp_none)
13650 return;
13651
13652 /* At least avoid this stale reference until the reference counting
13653 of breakpoints gets resolved. */
13654 if (bpt->related_breakpoint != bpt)
13655 {
13656 struct breakpoint *related;
13657 struct watchpoint *w;
13658
13659 if (bpt->type == bp_watchpoint_scope)
13660 w = (struct watchpoint *) bpt->related_breakpoint;
13661 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
13662 w = (struct watchpoint *) bpt;
13663 else
13664 w = NULL;
13665 if (w != NULL)
13666 watchpoint_del_at_next_stop (w);
13667
13668 /* Unlink bpt from the bpt->related_breakpoint ring. */
13669 for (related = bpt; related->related_breakpoint != bpt;
13670 related = related->related_breakpoint);
13671 related->related_breakpoint = bpt->related_breakpoint;
13672 bpt->related_breakpoint = bpt;
13673 }
13674
13675 /* watch_command_1 creates a watchpoint but only sets its number if
13676 update_watchpoint succeeds in creating its bp_locations. If there's
13677 a problem in that process, we'll be asked to delete the half-created
13678 watchpoint. In that case, don't announce the deletion. */
13679 if (bpt->number)
13680 observer_notify_breakpoint_deleted (bpt);
13681
13682 if (breakpoint_chain == bpt)
13683 breakpoint_chain = bpt->next;
13684
13685 ALL_BREAKPOINTS (b)
13686 if (b->next == bpt)
13687 {
13688 b->next = bpt->next;
13689 break;
13690 }
13691
13692 /* Be sure no bpstat's are pointing at the breakpoint after it's
13693 been freed. */
13694 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
13695 in all threads for now. Note that we cannot just remove bpstats
13696 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13697 commands are associated with the bpstat; if we remove it here,
13698 then the later call to bpstat_do_actions (&stop_bpstat); in
13699 event-top.c won't do anything, and temporary breakpoints with
13700 commands won't work. */
13701
13702 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13703
13704 /* Now that breakpoint is removed from breakpoint list, update the
13705 global location list. This will remove locations that used to
13706 belong to this breakpoint. Do this before freeing the breakpoint
13707 itself, since remove_breakpoint looks at location's owner. It
13708 might be better design to have location completely
13709 self-contained, but it's not the case now. */
13710 update_global_location_list (UGLL_DONT_INSERT);
13711
13712 bpt->ops->dtor (bpt);
13713 /* On the chance that someone will soon try again to delete this
13714 same bp, we mark it as deleted before freeing its storage. */
13715 bpt->type = bp_none;
13716 xfree (bpt);
13717 }
13718
13719 static void
13720 do_delete_breakpoint_cleanup (void *b)
13721 {
13722 delete_breakpoint (b);
13723 }
13724
13725 struct cleanup *
13726 make_cleanup_delete_breakpoint (struct breakpoint *b)
13727 {
13728 return make_cleanup (do_delete_breakpoint_cleanup, b);
13729 }
13730
13731 /* Iterator function to call a user-provided callback function once
13732 for each of B and its related breakpoints. */
13733
13734 static void
13735 iterate_over_related_breakpoints (struct breakpoint *b,
13736 void (*function) (struct breakpoint *,
13737 void *),
13738 void *data)
13739 {
13740 struct breakpoint *related;
13741
13742 related = b;
13743 do
13744 {
13745 struct breakpoint *next;
13746
13747 /* FUNCTION may delete RELATED. */
13748 next = related->related_breakpoint;
13749
13750 if (next == related)
13751 {
13752 /* RELATED is the last ring entry. */
13753 function (related, data);
13754
13755 /* FUNCTION may have deleted it, so we'd never reach back to
13756 B. There's nothing left to do anyway, so just break
13757 out. */
13758 break;
13759 }
13760 else
13761 function (related, data);
13762
13763 related = next;
13764 }
13765 while (related != b);
13766 }
13767
13768 static void
13769 do_delete_breakpoint (struct breakpoint *b, void *ignore)
13770 {
13771 delete_breakpoint (b);
13772 }
13773
13774 /* A callback for map_breakpoint_numbers that calls
13775 delete_breakpoint. */
13776
13777 static void
13778 do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
13779 {
13780 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
13781 }
13782
13783 void
13784 delete_command (char *arg, int from_tty)
13785 {
13786 struct breakpoint *b, *b_tmp;
13787
13788 dont_repeat ();
13789
13790 if (arg == 0)
13791 {
13792 int breaks_to_delete = 0;
13793
13794 /* Delete all breakpoints if no argument. Do not delete
13795 internal breakpoints, these have to be deleted with an
13796 explicit breakpoint number argument. */
13797 ALL_BREAKPOINTS (b)
13798 if (user_breakpoint_p (b))
13799 {
13800 breaks_to_delete = 1;
13801 break;
13802 }
13803
13804 /* Ask user only if there are some breakpoints to delete. */
13805 if (!from_tty
13806 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
13807 {
13808 ALL_BREAKPOINTS_SAFE (b, b_tmp)
13809 if (user_breakpoint_p (b))
13810 delete_breakpoint (b);
13811 }
13812 }
13813 else
13814 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
13815 }
13816
13817 static int
13818 all_locations_are_pending (struct bp_location *loc)
13819 {
13820 for (; loc; loc = loc->next)
13821 if (!loc->shlib_disabled
13822 && !loc->pspace->executing_startup)
13823 return 0;
13824 return 1;
13825 }
13826
13827 /* Subroutine of update_breakpoint_locations to simplify it.
13828 Return non-zero if multiple fns in list LOC have the same name.
13829 Null names are ignored. */
13830
13831 static int
13832 ambiguous_names_p (struct bp_location *loc)
13833 {
13834 struct bp_location *l;
13835 htab_t htab = htab_create_alloc (13, htab_hash_string,
13836 (int (*) (const void *,
13837 const void *)) streq,
13838 NULL, xcalloc, xfree);
13839
13840 for (l = loc; l != NULL; l = l->next)
13841 {
13842 const char **slot;
13843 const char *name = l->function_name;
13844
13845 /* Allow for some names to be NULL, ignore them. */
13846 if (name == NULL)
13847 continue;
13848
13849 slot = (const char **) htab_find_slot (htab, (const void *) name,
13850 INSERT);
13851 /* NOTE: We can assume slot != NULL here because xcalloc never
13852 returns NULL. */
13853 if (*slot != NULL)
13854 {
13855 htab_delete (htab);
13856 return 1;
13857 }
13858 *slot = name;
13859 }
13860
13861 htab_delete (htab);
13862 return 0;
13863 }
13864
13865 /* When symbols change, it probably means the sources changed as well,
13866 and it might mean the static tracepoint markers are no longer at
13867 the same address or line numbers they used to be at last we
13868 checked. Losing your static tracepoints whenever you rebuild is
13869 undesirable. This function tries to resync/rematch gdb static
13870 tracepoints with the markers on the target, for static tracepoints
13871 that have not been set by marker id. Static tracepoint that have
13872 been set by marker id are reset by marker id in breakpoint_re_set.
13873 The heuristic is:
13874
13875 1) For a tracepoint set at a specific address, look for a marker at
13876 the old PC. If one is found there, assume to be the same marker.
13877 If the name / string id of the marker found is different from the
13878 previous known name, assume that means the user renamed the marker
13879 in the sources, and output a warning.
13880
13881 2) For a tracepoint set at a given line number, look for a marker
13882 at the new address of the old line number. If one is found there,
13883 assume to be the same marker. If the name / string id of the
13884 marker found is different from the previous known name, assume that
13885 means the user renamed the marker in the sources, and output a
13886 warning.
13887
13888 3) If a marker is no longer found at the same address or line, it
13889 may mean the marker no longer exists. But it may also just mean
13890 the code changed a bit. Maybe the user added a few lines of code
13891 that made the marker move up or down (in line number terms). Ask
13892 the target for info about the marker with the string id as we knew
13893 it. If found, update line number and address in the matching
13894 static tracepoint. This will get confused if there's more than one
13895 marker with the same ID (possible in UST, although unadvised
13896 precisely because it confuses tools). */
13897
13898 static struct symtab_and_line
13899 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13900 {
13901 struct tracepoint *tp = (struct tracepoint *) b;
13902 struct static_tracepoint_marker marker;
13903 CORE_ADDR pc;
13904
13905 pc = sal.pc;
13906 if (sal.line)
13907 find_line_pc (sal.symtab, sal.line, &pc);
13908
13909 if (target_static_tracepoint_marker_at (pc, &marker))
13910 {
13911 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
13912 warning (_("static tracepoint %d changed probed marker from %s to %s"),
13913 b->number,
13914 tp->static_trace_marker_id, marker.str_id);
13915
13916 xfree (tp->static_trace_marker_id);
13917 tp->static_trace_marker_id = xstrdup (marker.str_id);
13918 release_static_tracepoint_marker (&marker);
13919
13920 return sal;
13921 }
13922
13923 /* Old marker wasn't found on target at lineno. Try looking it up
13924 by string ID. */
13925 if (!sal.explicit_pc
13926 && sal.line != 0
13927 && sal.symtab != NULL
13928 && tp->static_trace_marker_id != NULL)
13929 {
13930 VEC(static_tracepoint_marker_p) *markers;
13931
13932 markers
13933 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
13934
13935 if (!VEC_empty(static_tracepoint_marker_p, markers))
13936 {
13937 char *p, *tmp;
13938 struct symtab_and_line sal2;
13939 struct symbol *sym;
13940 struct static_tracepoint_marker *tpmarker;
13941 struct ui_out *uiout = current_uiout;
13942 struct cleanup *cleanup;
13943
13944 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
13945
13946 xfree (tp->static_trace_marker_id);
13947 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
13948
13949 warning (_("marker for static tracepoint %d (%s) not "
13950 "found at previous line number"),
13951 b->number, tp->static_trace_marker_id);
13952
13953 init_sal (&sal2);
13954
13955 sal2.pc = tpmarker->address;
13956
13957 sal2 = find_pc_line (tpmarker->address, 0);
13958 sym = find_pc_sect_function (tpmarker->address, NULL);
13959 ui_out_text (uiout, "Now in ");
13960 if (sym)
13961 {
13962 ui_out_field_string (uiout, "func",
13963 SYMBOL_PRINT_NAME (sym));
13964 ui_out_text (uiout, " at ");
13965 }
13966 ui_out_field_string (uiout, "file",
13967 symtab_to_filename_for_display (sal2.symtab));
13968 ui_out_text (uiout, ":");
13969
13970 if (ui_out_is_mi_like_p (uiout))
13971 {
13972 const char *fullname = symtab_to_fullname (sal2.symtab);
13973
13974 ui_out_field_string (uiout, "fullname", fullname);
13975 }
13976
13977 ui_out_field_int (uiout, "line", sal2.line);
13978 ui_out_text (uiout, "\n");
13979
13980 b->loc->line_number = sal2.line;
13981 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
13982
13983 delete_event_location (b->location);
13984 p = tmp = xstrprintf ("%s:%d",
13985 symtab_to_filename_for_display (sal2.symtab),
13986 b->loc->line_number);
13987 cleanup = make_cleanup (xfree, tmp);
13988 b->location = new_linespec_location (&tmp);
13989 do_cleanups (cleanup);
13990
13991 /* Might be nice to check if function changed, and warn if
13992 so. */
13993
13994 release_static_tracepoint_marker (tpmarker);
13995 }
13996 }
13997 return sal;
13998 }
13999
14000 /* Returns 1 iff locations A and B are sufficiently same that
14001 we don't need to report breakpoint as changed. */
14002
14003 static int
14004 locations_are_equal (struct bp_location *a, struct bp_location *b)
14005 {
14006 while (a && b)
14007 {
14008 if (a->address != b->address)
14009 return 0;
14010
14011 if (a->shlib_disabled != b->shlib_disabled)
14012 return 0;
14013
14014 if (a->enabled != b->enabled)
14015 return 0;
14016
14017 a = a->next;
14018 b = b->next;
14019 }
14020
14021 if ((a == NULL) != (b == NULL))
14022 return 0;
14023
14024 return 1;
14025 }
14026
14027 /* Create new breakpoint locations for B (a hardware or software breakpoint)
14028 based on SALS and SALS_END. If SALS_END.NELTS is not zero, then B is
14029 a ranged breakpoint. */
14030
14031 void
14032 update_breakpoint_locations (struct breakpoint *b,
14033 struct symtabs_and_lines sals,
14034 struct symtabs_and_lines sals_end)
14035 {
14036 int i;
14037 struct bp_location *existing_locations = b->loc;
14038
14039 if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
14040 {
14041 /* Ranged breakpoints have only one start location and one end
14042 location. */
14043 b->enable_state = bp_disabled;
14044 update_global_location_list (UGLL_MAY_INSERT);
14045 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
14046 "multiple locations found\n"),
14047 b->number);
14048 return;
14049 }
14050
14051 /* If there's no new locations, and all existing locations are
14052 pending, don't do anything. This optimizes the common case where
14053 all locations are in the same shared library, that was unloaded.
14054 We'd like to retain the location, so that when the library is
14055 loaded again, we don't loose the enabled/disabled status of the
14056 individual locations. */
14057 if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
14058 return;
14059
14060 b->loc = NULL;
14061
14062 for (i = 0; i < sals.nelts; ++i)
14063 {
14064 struct bp_location *new_loc;
14065
14066 switch_to_program_space_and_thread (sals.sals[i].pspace);
14067
14068 new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
14069
14070 /* Reparse conditions, they might contain references to the
14071 old symtab. */
14072 if (b->cond_string != NULL)
14073 {
14074 const char *s;
14075
14076 s = b->cond_string;
14077 TRY
14078 {
14079 new_loc->cond = parse_exp_1 (&s, sals.sals[i].pc,
14080 block_for_pc (sals.sals[i].pc),
14081 0);
14082 }
14083 CATCH (e, RETURN_MASK_ERROR)
14084 {
14085 warning (_("failed to reevaluate condition "
14086 "for breakpoint %d: %s"),
14087 b->number, e.message);
14088 new_loc->enabled = 0;
14089 }
14090 END_CATCH
14091 }
14092
14093 if (sals_end.nelts)
14094 {
14095 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
14096
14097 new_loc->length = end - sals.sals[0].pc + 1;
14098 }
14099 }
14100
14101 /* If possible, carry over 'disable' status from existing
14102 breakpoints. */
14103 {
14104 struct bp_location *e = existing_locations;
14105 /* If there are multiple breakpoints with the same function name,
14106 e.g. for inline functions, comparing function names won't work.
14107 Instead compare pc addresses; this is just a heuristic as things
14108 may have moved, but in practice it gives the correct answer
14109 often enough until a better solution is found. */
14110 int have_ambiguous_names = ambiguous_names_p (b->loc);
14111
14112 for (; e; e = e->next)
14113 {
14114 if (!e->enabled && e->function_name)
14115 {
14116 struct bp_location *l = b->loc;
14117 if (have_ambiguous_names)
14118 {
14119 for (; l; l = l->next)
14120 if (breakpoint_locations_match (e, l))
14121 {
14122 l->enabled = 0;
14123 break;
14124 }
14125 }
14126 else
14127 {
14128 for (; l; l = l->next)
14129 if (l->function_name
14130 && strcmp (e->function_name, l->function_name) == 0)
14131 {
14132 l->enabled = 0;
14133 break;
14134 }
14135 }
14136 }
14137 }
14138 }
14139
14140 if (!locations_are_equal (existing_locations, b->loc))
14141 observer_notify_breakpoint_modified (b);
14142
14143 update_global_location_list (UGLL_MAY_INSERT);
14144 }
14145
14146 /* Find the SaL locations corresponding to the given LOCATION.
14147 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
14148
14149 static struct symtabs_and_lines
14150 location_to_sals (struct breakpoint *b, struct event_location *location,
14151 int *found)
14152 {
14153 struct symtabs_and_lines sals = {0};
14154 struct gdb_exception exception = exception_none;
14155
14156 gdb_assert (b->ops != NULL);
14157
14158 TRY
14159 {
14160 b->ops->decode_location (b, location, &sals);
14161 }
14162 CATCH (e, RETURN_MASK_ERROR)
14163 {
14164 int not_found_and_ok = 0;
14165
14166 exception = e;
14167
14168 /* For pending breakpoints, it's expected that parsing will
14169 fail until the right shared library is loaded. User has
14170 already told to create pending breakpoints and don't need
14171 extra messages. If breakpoint is in bp_shlib_disabled
14172 state, then user already saw the message about that
14173 breakpoint being disabled, and don't want to see more
14174 errors. */
14175 if (e.error == NOT_FOUND_ERROR
14176 && (b->condition_not_parsed
14177 || (b->loc && b->loc->shlib_disabled)
14178 || (b->loc && b->loc->pspace->executing_startup)
14179 || b->enable_state == bp_disabled))
14180 not_found_and_ok = 1;
14181
14182 if (!not_found_and_ok)
14183 {
14184 /* We surely don't want to warn about the same breakpoint
14185 10 times. One solution, implemented here, is disable
14186 the breakpoint on error. Another solution would be to
14187 have separate 'warning emitted' flag. Since this
14188 happens only when a binary has changed, I don't know
14189 which approach is better. */
14190 b->enable_state = bp_disabled;
14191 throw_exception (e);
14192 }
14193 }
14194 END_CATCH
14195
14196 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
14197 {
14198 int i;
14199
14200 for (i = 0; i < sals.nelts; ++i)
14201 resolve_sal_pc (&sals.sals[i]);
14202 if (b->condition_not_parsed && b->extra_string != NULL)
14203 {
14204 char *cond_string, *extra_string;
14205 int thread, task;
14206
14207 find_condition_and_thread (b->extra_string, sals.sals[0].pc,
14208 &cond_string, &thread, &task,
14209 &extra_string);
14210 gdb_assert (b->cond_string == NULL);
14211 if (cond_string)
14212 b->cond_string = cond_string;
14213 b->thread = thread;
14214 b->task = task;
14215 if (extra_string)
14216 {
14217 xfree (b->extra_string);
14218 b->extra_string = extra_string;
14219 }
14220 b->condition_not_parsed = 0;
14221 }
14222
14223 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
14224 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
14225
14226 *found = 1;
14227 }
14228 else
14229 *found = 0;
14230
14231 return sals;
14232 }
14233
14234 /* The default re_set method, for typical hardware or software
14235 breakpoints. Reevaluate the breakpoint and recreate its
14236 locations. */
14237
14238 static void
14239 breakpoint_re_set_default (struct breakpoint *b)
14240 {
14241 int found;
14242 struct symtabs_and_lines sals, sals_end;
14243 struct symtabs_and_lines expanded = {0};
14244 struct symtabs_and_lines expanded_end = {0};
14245
14246 sals = location_to_sals (b, b->location, &found);
14247 if (found)
14248 {
14249 make_cleanup (xfree, sals.sals);
14250 expanded = sals;
14251 }
14252
14253 if (b->location_range_end != NULL)
14254 {
14255 sals_end = location_to_sals (b, b->location_range_end, &found);
14256 if (found)
14257 {
14258 make_cleanup (xfree, sals_end.sals);
14259 expanded_end = sals_end;
14260 }
14261 }
14262
14263 update_breakpoint_locations (b, expanded, expanded_end);
14264 }
14265
14266 /* Default method for creating SALs from an address string. It basically
14267 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
14268
14269 static void
14270 create_sals_from_location_default (const struct event_location *location,
14271 struct linespec_result *canonical,
14272 enum bptype type_wanted)
14273 {
14274 parse_breakpoint_sals (location, canonical);
14275 }
14276
14277 /* Call create_breakpoints_sal for the given arguments. This is the default
14278 function for the `create_breakpoints_sal' method of
14279 breakpoint_ops. */
14280
14281 static void
14282 create_breakpoints_sal_default (struct gdbarch *gdbarch,
14283 struct linespec_result *canonical,
14284 char *cond_string,
14285 char *extra_string,
14286 enum bptype type_wanted,
14287 enum bpdisp disposition,
14288 int thread,
14289 int task, int ignore_count,
14290 const struct breakpoint_ops *ops,
14291 int from_tty, int enabled,
14292 int internal, unsigned flags)
14293 {
14294 create_breakpoints_sal (gdbarch, canonical, cond_string,
14295 extra_string,
14296 type_wanted, disposition,
14297 thread, task, ignore_count, ops, from_tty,
14298 enabled, internal, flags);
14299 }
14300
14301 /* Decode the line represented by S by calling decode_line_full. This is the
14302 default function for the `decode_location' method of breakpoint_ops. */
14303
14304 static void
14305 decode_location_default (struct breakpoint *b,
14306 const struct event_location *location,
14307 struct symtabs_and_lines *sals)
14308 {
14309 struct linespec_result canonical;
14310
14311 init_linespec_result (&canonical);
14312 decode_line_full (location, DECODE_LINE_FUNFIRSTLINE,
14313 (struct symtab *) NULL, 0,
14314 &canonical, multiple_symbols_all,
14315 b->filter);
14316
14317 /* We should get 0 or 1 resulting SALs. */
14318 gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
14319
14320 if (VEC_length (linespec_sals, canonical.sals) > 0)
14321 {
14322 struct linespec_sals *lsal;
14323
14324 lsal = VEC_index (linespec_sals, canonical.sals, 0);
14325 *sals = lsal->sals;
14326 /* Arrange it so the destructor does not free the
14327 contents. */
14328 lsal->sals.sals = NULL;
14329 }
14330
14331 destroy_linespec_result (&canonical);
14332 }
14333
14334 /* Prepare the global context for a re-set of breakpoint B. */
14335
14336 static struct cleanup *
14337 prepare_re_set_context (struct breakpoint *b)
14338 {
14339 struct cleanup *cleanups;
14340
14341 input_radix = b->input_radix;
14342 cleanups = save_current_space_and_thread ();
14343 if (b->pspace != NULL)
14344 switch_to_program_space_and_thread (b->pspace);
14345 set_language (b->language);
14346
14347 return cleanups;
14348 }
14349
14350 /* Reset a breakpoint given it's struct breakpoint * BINT.
14351 The value we return ends up being the return value from catch_errors.
14352 Unused in this case. */
14353
14354 static int
14355 breakpoint_re_set_one (void *bint)
14356 {
14357 /* Get past catch_errs. */
14358 struct breakpoint *b = (struct breakpoint *) bint;
14359 struct cleanup *cleanups;
14360
14361 cleanups = prepare_re_set_context (b);
14362 b->ops->re_set (b);
14363 do_cleanups (cleanups);
14364 return 0;
14365 }
14366
14367 /* Re-set all breakpoints after symbols have been re-loaded. */
14368 void
14369 breakpoint_re_set (void)
14370 {
14371 struct breakpoint *b, *b_tmp;
14372 enum language save_language;
14373 int save_input_radix;
14374 struct cleanup *old_chain;
14375
14376 save_language = current_language->la_language;
14377 save_input_radix = input_radix;
14378 old_chain = save_current_program_space ();
14379
14380 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14381 {
14382 /* Format possible error msg. */
14383 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
14384 b->number);
14385 struct cleanup *cleanups = make_cleanup (xfree, message);
14386 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
14387 do_cleanups (cleanups);
14388 }
14389 set_language (save_language);
14390 input_radix = save_input_radix;
14391
14392 jit_breakpoint_re_set ();
14393
14394 do_cleanups (old_chain);
14395
14396 create_overlay_event_breakpoint ();
14397 create_longjmp_master_breakpoint ();
14398 create_std_terminate_master_breakpoint ();
14399 create_exception_master_breakpoint ();
14400 }
14401 \f
14402 /* Reset the thread number of this breakpoint:
14403
14404 - If the breakpoint is for all threads, leave it as-is.
14405 - Else, reset it to the current thread for inferior_ptid. */
14406 void
14407 breakpoint_re_set_thread (struct breakpoint *b)
14408 {
14409 if (b->thread != -1)
14410 {
14411 if (in_thread_list (inferior_ptid))
14412 b->thread = pid_to_thread_id (inferior_ptid);
14413
14414 /* We're being called after following a fork. The new fork is
14415 selected as current, and unless this was a vfork will have a
14416 different program space from the original thread. Reset that
14417 as well. */
14418 b->loc->pspace = current_program_space;
14419 }
14420 }
14421
14422 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14423 If from_tty is nonzero, it prints a message to that effect,
14424 which ends with a period (no newline). */
14425
14426 void
14427 set_ignore_count (int bptnum, int count, int from_tty)
14428 {
14429 struct breakpoint *b;
14430
14431 if (count < 0)
14432 count = 0;
14433
14434 ALL_BREAKPOINTS (b)
14435 if (b->number == bptnum)
14436 {
14437 if (is_tracepoint (b))
14438 {
14439 if (from_tty && count != 0)
14440 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14441 bptnum);
14442 return;
14443 }
14444
14445 b->ignore_count = count;
14446 if (from_tty)
14447 {
14448 if (count == 0)
14449 printf_filtered (_("Will stop next time "
14450 "breakpoint %d is reached."),
14451 bptnum);
14452 else if (count == 1)
14453 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
14454 bptnum);
14455 else
14456 printf_filtered (_("Will ignore next %d "
14457 "crossings of breakpoint %d."),
14458 count, bptnum);
14459 }
14460 observer_notify_breakpoint_modified (b);
14461 return;
14462 }
14463
14464 error (_("No breakpoint number %d."), bptnum);
14465 }
14466
14467 /* Command to set ignore-count of breakpoint N to COUNT. */
14468
14469 static void
14470 ignore_command (char *args, int from_tty)
14471 {
14472 char *p = args;
14473 int num;
14474
14475 if (p == 0)
14476 error_no_arg (_("a breakpoint number"));
14477
14478 num = get_number (&p);
14479 if (num == 0)
14480 error (_("bad breakpoint number: '%s'"), args);
14481 if (*p == 0)
14482 error (_("Second argument (specified ignore-count) is missing."));
14483
14484 set_ignore_count (num,
14485 longest_to_int (value_as_long (parse_and_eval (p))),
14486 from_tty);
14487 if (from_tty)
14488 printf_filtered ("\n");
14489 }
14490 \f
14491 /* Call FUNCTION on each of the breakpoints
14492 whose numbers are given in ARGS. */
14493
14494 static void
14495 map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
14496 void *),
14497 void *data)
14498 {
14499 int num;
14500 struct breakpoint *b, *tmp;
14501 int match;
14502 struct get_number_or_range_state state;
14503
14504 if (args == 0 || *args == '\0')
14505 error_no_arg (_("one or more breakpoint numbers"));
14506
14507 init_number_or_range (&state, args);
14508
14509 while (!state.finished)
14510 {
14511 const char *p = state.string;
14512
14513 match = 0;
14514
14515 num = get_number_or_range (&state);
14516 if (num == 0)
14517 {
14518 warning (_("bad breakpoint number at or near '%s'"), p);
14519 }
14520 else
14521 {
14522 ALL_BREAKPOINTS_SAFE (b, tmp)
14523 if (b->number == num)
14524 {
14525 match = 1;
14526 function (b, data);
14527 break;
14528 }
14529 if (match == 0)
14530 printf_unfiltered (_("No breakpoint number %d.\n"), num);
14531 }
14532 }
14533 }
14534
14535 static struct bp_location *
14536 find_location_by_number (char *number)
14537 {
14538 char *dot = strchr (number, '.');
14539 char *p1;
14540 int bp_num;
14541 int loc_num;
14542 struct breakpoint *b;
14543 struct bp_location *loc;
14544
14545 *dot = '\0';
14546
14547 p1 = number;
14548 bp_num = get_number (&p1);
14549 if (bp_num == 0)
14550 error (_("Bad breakpoint number '%s'"), number);
14551
14552 ALL_BREAKPOINTS (b)
14553 if (b->number == bp_num)
14554 {
14555 break;
14556 }
14557
14558 if (!b || b->number != bp_num)
14559 error (_("Bad breakpoint number '%s'"), number);
14560
14561 p1 = dot+1;
14562 loc_num = get_number (&p1);
14563 if (loc_num == 0)
14564 error (_("Bad breakpoint location number '%s'"), number);
14565
14566 --loc_num;
14567 loc = b->loc;
14568 for (;loc_num && loc; --loc_num, loc = loc->next)
14569 ;
14570 if (!loc)
14571 error (_("Bad breakpoint location number '%s'"), dot+1);
14572
14573 return loc;
14574 }
14575
14576
14577 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14578 If from_tty is nonzero, it prints a message to that effect,
14579 which ends with a period (no newline). */
14580
14581 void
14582 disable_breakpoint (struct breakpoint *bpt)
14583 {
14584 /* Never disable a watchpoint scope breakpoint; we want to
14585 hit them when we leave scope so we can delete both the
14586 watchpoint and its scope breakpoint at that time. */
14587 if (bpt->type == bp_watchpoint_scope)
14588 return;
14589
14590 bpt->enable_state = bp_disabled;
14591
14592 /* Mark breakpoint locations modified. */
14593 mark_breakpoint_modified (bpt);
14594
14595 if (target_supports_enable_disable_tracepoint ()
14596 && current_trace_status ()->running && is_tracepoint (bpt))
14597 {
14598 struct bp_location *location;
14599
14600 for (location = bpt->loc; location; location = location->next)
14601 target_disable_tracepoint (location);
14602 }
14603
14604 update_global_location_list (UGLL_DONT_INSERT);
14605
14606 observer_notify_breakpoint_modified (bpt);
14607 }
14608
14609 /* A callback for iterate_over_related_breakpoints. */
14610
14611 static void
14612 do_disable_breakpoint (struct breakpoint *b, void *ignore)
14613 {
14614 disable_breakpoint (b);
14615 }
14616
14617 /* A callback for map_breakpoint_numbers that calls
14618 disable_breakpoint. */
14619
14620 static void
14621 do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
14622 {
14623 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
14624 }
14625
14626 static void
14627 disable_command (char *args, int from_tty)
14628 {
14629 if (args == 0)
14630 {
14631 struct breakpoint *bpt;
14632
14633 ALL_BREAKPOINTS (bpt)
14634 if (user_breakpoint_p (bpt))
14635 disable_breakpoint (bpt);
14636 }
14637 else
14638 {
14639 char *num = extract_arg (&args);
14640
14641 while (num)
14642 {
14643 if (strchr (num, '.'))
14644 {
14645 struct bp_location *loc = find_location_by_number (num);
14646
14647 if (loc)
14648 {
14649 if (loc->enabled)
14650 {
14651 loc->enabled = 0;
14652 mark_breakpoint_location_modified (loc);
14653 }
14654 if (target_supports_enable_disable_tracepoint ()
14655 && current_trace_status ()->running && loc->owner
14656 && is_tracepoint (loc->owner))
14657 target_disable_tracepoint (loc);
14658 }
14659 update_global_location_list (UGLL_DONT_INSERT);
14660 }
14661 else
14662 map_breakpoint_numbers (num, do_map_disable_breakpoint, NULL);
14663 num = extract_arg (&args);
14664 }
14665 }
14666 }
14667
14668 static void
14669 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14670 int count)
14671 {
14672 int target_resources_ok;
14673
14674 if (bpt->type == bp_hardware_breakpoint)
14675 {
14676 int i;
14677 i = hw_breakpoint_used_count ();
14678 target_resources_ok =
14679 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
14680 i + 1, 0);
14681 if (target_resources_ok == 0)
14682 error (_("No hardware breakpoint support in the target."));
14683 else if (target_resources_ok < 0)
14684 error (_("Hardware breakpoints used exceeds limit."));
14685 }
14686
14687 if (is_watchpoint (bpt))
14688 {
14689 /* Initialize it just to avoid a GCC false warning. */
14690 enum enable_state orig_enable_state = bp_disabled;
14691
14692 TRY
14693 {
14694 struct watchpoint *w = (struct watchpoint *) bpt;
14695
14696 orig_enable_state = bpt->enable_state;
14697 bpt->enable_state = bp_enabled;
14698 update_watchpoint (w, 1 /* reparse */);
14699 }
14700 CATCH (e, RETURN_MASK_ALL)
14701 {
14702 bpt->enable_state = orig_enable_state;
14703 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14704 bpt->number);
14705 return;
14706 }
14707 END_CATCH
14708 }
14709
14710 bpt->enable_state = bp_enabled;
14711
14712 /* Mark breakpoint locations modified. */
14713 mark_breakpoint_modified (bpt);
14714
14715 if (target_supports_enable_disable_tracepoint ()
14716 && current_trace_status ()->running && is_tracepoint (bpt))
14717 {
14718 struct bp_location *location;
14719
14720 for (location = bpt->loc; location; location = location->next)
14721 target_enable_tracepoint (location);
14722 }
14723
14724 bpt->disposition = disposition;
14725 bpt->enable_count = count;
14726 update_global_location_list (UGLL_MAY_INSERT);
14727
14728 observer_notify_breakpoint_modified (bpt);
14729 }
14730
14731
14732 void
14733 enable_breakpoint (struct breakpoint *bpt)
14734 {
14735 enable_breakpoint_disp (bpt, bpt->disposition, 0);
14736 }
14737
14738 static void
14739 do_enable_breakpoint (struct breakpoint *bpt, void *arg)
14740 {
14741 enable_breakpoint (bpt);
14742 }
14743
14744 /* A callback for map_breakpoint_numbers that calls
14745 enable_breakpoint. */
14746
14747 static void
14748 do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
14749 {
14750 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
14751 }
14752
14753 /* The enable command enables the specified breakpoints (or all defined
14754 breakpoints) so they once again become (or continue to be) effective
14755 in stopping the inferior. */
14756
14757 static void
14758 enable_command (char *args, int from_tty)
14759 {
14760 if (args == 0)
14761 {
14762 struct breakpoint *bpt;
14763
14764 ALL_BREAKPOINTS (bpt)
14765 if (user_breakpoint_p (bpt))
14766 enable_breakpoint (bpt);
14767 }
14768 else
14769 {
14770 char *num = extract_arg (&args);
14771
14772 while (num)
14773 {
14774 if (strchr (num, '.'))
14775 {
14776 struct bp_location *loc = find_location_by_number (num);
14777
14778 if (loc)
14779 {
14780 if (!loc->enabled)
14781 {
14782 loc->enabled = 1;
14783 mark_breakpoint_location_modified (loc);
14784 }
14785 if (target_supports_enable_disable_tracepoint ()
14786 && current_trace_status ()->running && loc->owner
14787 && is_tracepoint (loc->owner))
14788 target_enable_tracepoint (loc);
14789 }
14790 update_global_location_list (UGLL_MAY_INSERT);
14791 }
14792 else
14793 map_breakpoint_numbers (num, do_map_enable_breakpoint, NULL);
14794 num = extract_arg (&args);
14795 }
14796 }
14797 }
14798
14799 /* This struct packages up disposition data for application to multiple
14800 breakpoints. */
14801
14802 struct disp_data
14803 {
14804 enum bpdisp disp;
14805 int count;
14806 };
14807
14808 static void
14809 do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
14810 {
14811 struct disp_data disp_data = *(struct disp_data *) arg;
14812
14813 enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
14814 }
14815
14816 static void
14817 do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
14818 {
14819 struct disp_data disp = { disp_disable, 1 };
14820
14821 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14822 }
14823
14824 static void
14825 enable_once_command (char *args, int from_tty)
14826 {
14827 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
14828 }
14829
14830 static void
14831 do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
14832 {
14833 struct disp_data disp = { disp_disable, *(int *) countptr };
14834
14835 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14836 }
14837
14838 static void
14839 enable_count_command (char *args, int from_tty)
14840 {
14841 int count;
14842
14843 if (args == NULL)
14844 error_no_arg (_("hit count"));
14845
14846 count = get_number (&args);
14847
14848 map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
14849 }
14850
14851 static void
14852 do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
14853 {
14854 struct disp_data disp = { disp_del, 1 };
14855
14856 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14857 }
14858
14859 static void
14860 enable_delete_command (char *args, int from_tty)
14861 {
14862 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
14863 }
14864 \f
14865 static void
14866 set_breakpoint_cmd (char *args, int from_tty)
14867 {
14868 }
14869
14870 static void
14871 show_breakpoint_cmd (char *args, int from_tty)
14872 {
14873 }
14874
14875 /* Invalidate last known value of any hardware watchpoint if
14876 the memory which that value represents has been written to by
14877 GDB itself. */
14878
14879 static void
14880 invalidate_bp_value_on_memory_change (struct inferior *inferior,
14881 CORE_ADDR addr, ssize_t len,
14882 const bfd_byte *data)
14883 {
14884 struct breakpoint *bp;
14885
14886 ALL_BREAKPOINTS (bp)
14887 if (bp->enable_state == bp_enabled
14888 && bp->type == bp_hardware_watchpoint)
14889 {
14890 struct watchpoint *wp = (struct watchpoint *) bp;
14891
14892 if (wp->val_valid && wp->val)
14893 {
14894 struct bp_location *loc;
14895
14896 for (loc = bp->loc; loc != NULL; loc = loc->next)
14897 if (loc->loc_type == bp_loc_hardware_watchpoint
14898 && loc->address + loc->length > addr
14899 && addr + len > loc->address)
14900 {
14901 value_free (wp->val);
14902 wp->val = NULL;
14903 wp->val_valid = 0;
14904 }
14905 }
14906 }
14907 }
14908
14909 /* Create and insert a breakpoint for software single step. */
14910
14911 void
14912 insert_single_step_breakpoint (struct gdbarch *gdbarch,
14913 struct address_space *aspace,
14914 CORE_ADDR next_pc)
14915 {
14916 struct thread_info *tp = inferior_thread ();
14917 struct symtab_and_line sal;
14918 CORE_ADDR pc = next_pc;
14919
14920 if (tp->control.single_step_breakpoints == NULL)
14921 {
14922 tp->control.single_step_breakpoints
14923 = new_single_step_breakpoint (tp->num, gdbarch);
14924 }
14925
14926 sal = find_pc_line (pc, 0);
14927 sal.pc = pc;
14928 sal.section = find_pc_overlay (pc);
14929 sal.explicit_pc = 1;
14930 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
14931
14932 update_global_location_list (UGLL_INSERT);
14933 }
14934
14935 /* See breakpoint.h. */
14936
14937 int
14938 breakpoint_has_location_inserted_here (struct breakpoint *bp,
14939 struct address_space *aspace,
14940 CORE_ADDR pc)
14941 {
14942 struct bp_location *loc;
14943
14944 for (loc = bp->loc; loc != NULL; loc = loc->next)
14945 if (loc->inserted
14946 && breakpoint_location_address_match (loc, aspace, pc))
14947 return 1;
14948
14949 return 0;
14950 }
14951
14952 /* Check whether a software single-step breakpoint is inserted at
14953 PC. */
14954
14955 int
14956 single_step_breakpoint_inserted_here_p (struct address_space *aspace,
14957 CORE_ADDR pc)
14958 {
14959 struct breakpoint *bpt;
14960
14961 ALL_BREAKPOINTS (bpt)
14962 {
14963 if (bpt->type == bp_single_step
14964 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
14965 return 1;
14966 }
14967 return 0;
14968 }
14969
14970 /* Tracepoint-specific operations. */
14971
14972 /* Set tracepoint count to NUM. */
14973 static void
14974 set_tracepoint_count (int num)
14975 {
14976 tracepoint_count = num;
14977 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
14978 }
14979
14980 static void
14981 trace_command (char *arg, int from_tty)
14982 {
14983 struct breakpoint_ops *ops;
14984 struct event_location *location;
14985 struct cleanup *back_to;
14986 const char *arg_cp = arg;
14987
14988 location = string_to_event_location (&arg, current_language);
14989 back_to = make_cleanup_delete_event_location (location);
14990 if (arg_cp != NULL && probe_linespec_to_ops (&arg_cp) != NULL)
14991 ops = &tracepoint_probe_breakpoint_ops;
14992 else
14993 ops = &tracepoint_breakpoint_ops;
14994
14995 create_breakpoint (get_current_arch (),
14996 location,
14997 NULL, 0, arg, 1 /* parse arg */,
14998 0 /* tempflag */,
14999 bp_tracepoint /* type_wanted */,
15000 0 /* Ignore count */,
15001 pending_break_support,
15002 ops,
15003 from_tty,
15004 1 /* enabled */,
15005 0 /* internal */, 0);
15006 do_cleanups (back_to);
15007 }
15008
15009 static void
15010 ftrace_command (char *arg, int from_tty)
15011 {
15012 struct event_location *location;
15013 struct cleanup *back_to;
15014
15015 location = string_to_event_location (&arg, current_language);
15016 back_to = make_cleanup_delete_event_location (location);
15017 create_breakpoint (get_current_arch (),
15018 location,
15019 NULL, 0, arg, 1 /* parse arg */,
15020 0 /* tempflag */,
15021 bp_fast_tracepoint /* type_wanted */,
15022 0 /* Ignore count */,
15023 pending_break_support,
15024 &tracepoint_breakpoint_ops,
15025 from_tty,
15026 1 /* enabled */,
15027 0 /* internal */, 0);
15028 do_cleanups (back_to);
15029 }
15030
15031 /* strace command implementation. Creates a static tracepoint. */
15032
15033 static void
15034 strace_command (char *arg, int from_tty)
15035 {
15036 struct breakpoint_ops *ops;
15037 struct event_location *location;
15038 struct cleanup *back_to;
15039
15040 /* Decide if we are dealing with a static tracepoint marker (`-m'),
15041 or with a normal static tracepoint. */
15042 if (arg && startswith (arg, "-m") && isspace (arg[2]))
15043 {
15044 ops = &strace_marker_breakpoint_ops;
15045 location = new_linespec_location (&arg);
15046 }
15047 else
15048 {
15049 ops = &tracepoint_breakpoint_ops;
15050 location = string_to_event_location (&arg, current_language);
15051 }
15052
15053 back_to = make_cleanup_delete_event_location (location);
15054 create_breakpoint (get_current_arch (),
15055 location,
15056 NULL, 0, arg, 1 /* parse arg */,
15057 0 /* tempflag */,
15058 bp_static_tracepoint /* type_wanted */,
15059 0 /* Ignore count */,
15060 pending_break_support,
15061 ops,
15062 from_tty,
15063 1 /* enabled */,
15064 0 /* internal */, 0);
15065 do_cleanups (back_to);
15066 }
15067
15068 /* Set up a fake reader function that gets command lines from a linked
15069 list that was acquired during tracepoint uploading. */
15070
15071 static struct uploaded_tp *this_utp;
15072 static int next_cmd;
15073
15074 static char *
15075 read_uploaded_action (void)
15076 {
15077 char *rslt;
15078
15079 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
15080
15081 next_cmd++;
15082
15083 return rslt;
15084 }
15085
15086 /* Given information about a tracepoint as recorded on a target (which
15087 can be either a live system or a trace file), attempt to create an
15088 equivalent GDB tracepoint. This is not a reliable process, since
15089 the target does not necessarily have all the information used when
15090 the tracepoint was originally defined. */
15091
15092 struct tracepoint *
15093 create_tracepoint_from_upload (struct uploaded_tp *utp)
15094 {
15095 char *addr_str, small_buf[100];
15096 struct tracepoint *tp;
15097 struct event_location *location;
15098 struct cleanup *cleanup;
15099
15100 if (utp->at_string)
15101 addr_str = utp->at_string;
15102 else
15103 {
15104 /* In the absence of a source location, fall back to raw
15105 address. Since there is no way to confirm that the address
15106 means the same thing as when the trace was started, warn the
15107 user. */
15108 warning (_("Uploaded tracepoint %d has no "
15109 "source location, using raw address"),
15110 utp->number);
15111 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
15112 addr_str = small_buf;
15113 }
15114
15115 /* There's not much we can do with a sequence of bytecodes. */
15116 if (utp->cond && !utp->cond_string)
15117 warning (_("Uploaded tracepoint %d condition "
15118 "has no source form, ignoring it"),
15119 utp->number);
15120
15121 location = string_to_event_location (&addr_str, current_language);
15122 cleanup = make_cleanup_delete_event_location (location);
15123 if (!create_breakpoint (get_current_arch (),
15124 location,
15125 utp->cond_string, -1, addr_str,
15126 0 /* parse cond/thread */,
15127 0 /* tempflag */,
15128 utp->type /* type_wanted */,
15129 0 /* Ignore count */,
15130 pending_break_support,
15131 &tracepoint_breakpoint_ops,
15132 0 /* from_tty */,
15133 utp->enabled /* enabled */,
15134 0 /* internal */,
15135 CREATE_BREAKPOINT_FLAGS_INSERTED))
15136 {
15137 do_cleanups (cleanup);
15138 return NULL;
15139 }
15140
15141 do_cleanups (cleanup);
15142
15143 /* Get the tracepoint we just created. */
15144 tp = get_tracepoint (tracepoint_count);
15145 gdb_assert (tp != NULL);
15146
15147 if (utp->pass > 0)
15148 {
15149 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
15150 tp->base.number);
15151
15152 trace_pass_command (small_buf, 0);
15153 }
15154
15155 /* If we have uploaded versions of the original commands, set up a
15156 special-purpose "reader" function and call the usual command line
15157 reader, then pass the result to the breakpoint command-setting
15158 function. */
15159 if (!VEC_empty (char_ptr, utp->cmd_strings))
15160 {
15161 struct command_line *cmd_list;
15162
15163 this_utp = utp;
15164 next_cmd = 0;
15165
15166 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
15167
15168 breakpoint_set_commands (&tp->base, cmd_list);
15169 }
15170 else if (!VEC_empty (char_ptr, utp->actions)
15171 || !VEC_empty (char_ptr, utp->step_actions))
15172 warning (_("Uploaded tracepoint %d actions "
15173 "have no source form, ignoring them"),
15174 utp->number);
15175
15176 /* Copy any status information that might be available. */
15177 tp->base.hit_count = utp->hit_count;
15178 tp->traceframe_usage = utp->traceframe_usage;
15179
15180 return tp;
15181 }
15182
15183 /* Print information on tracepoint number TPNUM_EXP, or all if
15184 omitted. */
15185
15186 static void
15187 tracepoints_info (char *args, int from_tty)
15188 {
15189 struct ui_out *uiout = current_uiout;
15190 int num_printed;
15191
15192 num_printed = breakpoint_1 (args, 0, is_tracepoint);
15193
15194 if (num_printed == 0)
15195 {
15196 if (args == NULL || *args == '\0')
15197 ui_out_message (uiout, 0, "No tracepoints.\n");
15198 else
15199 ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
15200 }
15201
15202 default_collect_info ();
15203 }
15204
15205 /* The 'enable trace' command enables tracepoints.
15206 Not supported by all targets. */
15207 static void
15208 enable_trace_command (char *args, int from_tty)
15209 {
15210 enable_command (args, from_tty);
15211 }
15212
15213 /* The 'disable trace' command disables tracepoints.
15214 Not supported by all targets. */
15215 static void
15216 disable_trace_command (char *args, int from_tty)
15217 {
15218 disable_command (args, from_tty);
15219 }
15220
15221 /* Remove a tracepoint (or all if no argument). */
15222 static void
15223 delete_trace_command (char *arg, int from_tty)
15224 {
15225 struct breakpoint *b, *b_tmp;
15226
15227 dont_repeat ();
15228
15229 if (arg == 0)
15230 {
15231 int breaks_to_delete = 0;
15232
15233 /* Delete all breakpoints if no argument.
15234 Do not delete internal or call-dummy breakpoints, these
15235 have to be deleted with an explicit breakpoint number
15236 argument. */
15237 ALL_TRACEPOINTS (b)
15238 if (is_tracepoint (b) && user_breakpoint_p (b))
15239 {
15240 breaks_to_delete = 1;
15241 break;
15242 }
15243
15244 /* Ask user only if there are some breakpoints to delete. */
15245 if (!from_tty
15246 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
15247 {
15248 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15249 if (is_tracepoint (b) && user_breakpoint_p (b))
15250 delete_breakpoint (b);
15251 }
15252 }
15253 else
15254 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
15255 }
15256
15257 /* Helper function for trace_pass_command. */
15258
15259 static void
15260 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
15261 {
15262 tp->pass_count = count;
15263 observer_notify_breakpoint_modified (&tp->base);
15264 if (from_tty)
15265 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
15266 tp->base.number, count);
15267 }
15268
15269 /* Set passcount for tracepoint.
15270
15271 First command argument is passcount, second is tracepoint number.
15272 If tracepoint number omitted, apply to most recently defined.
15273 Also accepts special argument "all". */
15274
15275 static void
15276 trace_pass_command (char *args, int from_tty)
15277 {
15278 struct tracepoint *t1;
15279 unsigned int count;
15280
15281 if (args == 0 || *args == 0)
15282 error (_("passcount command requires an "
15283 "argument (count + optional TP num)"));
15284
15285 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
15286
15287 args = skip_spaces (args);
15288 if (*args && strncasecmp (args, "all", 3) == 0)
15289 {
15290 struct breakpoint *b;
15291
15292 args += 3; /* Skip special argument "all". */
15293 if (*args)
15294 error (_("Junk at end of arguments."));
15295
15296 ALL_TRACEPOINTS (b)
15297 {
15298 t1 = (struct tracepoint *) b;
15299 trace_pass_set_count (t1, count, from_tty);
15300 }
15301 }
15302 else if (*args == '\0')
15303 {
15304 t1 = get_tracepoint_by_number (&args, NULL);
15305 if (t1)
15306 trace_pass_set_count (t1, count, from_tty);
15307 }
15308 else
15309 {
15310 struct get_number_or_range_state state;
15311
15312 init_number_or_range (&state, args);
15313 while (!state.finished)
15314 {
15315 t1 = get_tracepoint_by_number (&args, &state);
15316 if (t1)
15317 trace_pass_set_count (t1, count, from_tty);
15318 }
15319 }
15320 }
15321
15322 struct tracepoint *
15323 get_tracepoint (int num)
15324 {
15325 struct breakpoint *t;
15326
15327 ALL_TRACEPOINTS (t)
15328 if (t->number == num)
15329 return (struct tracepoint *) t;
15330
15331 return NULL;
15332 }
15333
15334 /* Find the tracepoint with the given target-side number (which may be
15335 different from the tracepoint number after disconnecting and
15336 reconnecting). */
15337
15338 struct tracepoint *
15339 get_tracepoint_by_number_on_target (int num)
15340 {
15341 struct breakpoint *b;
15342
15343 ALL_TRACEPOINTS (b)
15344 {
15345 struct tracepoint *t = (struct tracepoint *) b;
15346
15347 if (t->number_on_target == num)
15348 return t;
15349 }
15350
15351 return NULL;
15352 }
15353
15354 /* Utility: parse a tracepoint number and look it up in the list.
15355 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
15356 If the argument is missing, the most recent tracepoint
15357 (tracepoint_count) is returned. */
15358
15359 struct tracepoint *
15360 get_tracepoint_by_number (char **arg,
15361 struct get_number_or_range_state *state)
15362 {
15363 struct breakpoint *t;
15364 int tpnum;
15365 char *instring = arg == NULL ? NULL : *arg;
15366
15367 if (state)
15368 {
15369 gdb_assert (!state->finished);
15370 tpnum = get_number_or_range (state);
15371 }
15372 else if (arg == NULL || *arg == NULL || ! **arg)
15373 tpnum = tracepoint_count;
15374 else
15375 tpnum = get_number (arg);
15376
15377 if (tpnum <= 0)
15378 {
15379 if (instring && *instring)
15380 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15381 instring);
15382 else
15383 printf_filtered (_("No previous tracepoint\n"));
15384 return NULL;
15385 }
15386
15387 ALL_TRACEPOINTS (t)
15388 if (t->number == tpnum)
15389 {
15390 return (struct tracepoint *) t;
15391 }
15392
15393 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15394 return NULL;
15395 }
15396
15397 void
15398 print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15399 {
15400 if (b->thread != -1)
15401 fprintf_unfiltered (fp, " thread %d", b->thread);
15402
15403 if (b->task != 0)
15404 fprintf_unfiltered (fp, " task %d", b->task);
15405
15406 fprintf_unfiltered (fp, "\n");
15407 }
15408
15409 /* Save information on user settable breakpoints (watchpoints, etc) to
15410 a new script file named FILENAME. If FILTER is non-NULL, call it
15411 on each breakpoint and only include the ones for which it returns
15412 non-zero. */
15413
15414 static void
15415 save_breakpoints (char *filename, int from_tty,
15416 int (*filter) (const struct breakpoint *))
15417 {
15418 struct breakpoint *tp;
15419 int any = 0;
15420 struct cleanup *cleanup;
15421 struct ui_file *fp;
15422 int extra_trace_bits = 0;
15423
15424 if (filename == 0 || *filename == 0)
15425 error (_("Argument required (file name in which to save)"));
15426
15427 /* See if we have anything to save. */
15428 ALL_BREAKPOINTS (tp)
15429 {
15430 /* Skip internal and momentary breakpoints. */
15431 if (!user_breakpoint_p (tp))
15432 continue;
15433
15434 /* If we have a filter, only save the breakpoints it accepts. */
15435 if (filter && !filter (tp))
15436 continue;
15437
15438 any = 1;
15439
15440 if (is_tracepoint (tp))
15441 {
15442 extra_trace_bits = 1;
15443
15444 /* We can stop searching. */
15445 break;
15446 }
15447 }
15448
15449 if (!any)
15450 {
15451 warning (_("Nothing to save."));
15452 return;
15453 }
15454
15455 filename = tilde_expand (filename);
15456 cleanup = make_cleanup (xfree, filename);
15457 fp = gdb_fopen (filename, "w");
15458 if (!fp)
15459 error (_("Unable to open file '%s' for saving (%s)"),
15460 filename, safe_strerror (errno));
15461 make_cleanup_ui_file_delete (fp);
15462
15463 if (extra_trace_bits)
15464 save_trace_state_variables (fp);
15465
15466 ALL_BREAKPOINTS (tp)
15467 {
15468 /* Skip internal and momentary breakpoints. */
15469 if (!user_breakpoint_p (tp))
15470 continue;
15471
15472 /* If we have a filter, only save the breakpoints it accepts. */
15473 if (filter && !filter (tp))
15474 continue;
15475
15476 tp->ops->print_recreate (tp, fp);
15477
15478 /* Note, we can't rely on tp->number for anything, as we can't
15479 assume the recreated breakpoint numbers will match. Use $bpnum
15480 instead. */
15481
15482 if (tp->cond_string)
15483 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
15484
15485 if (tp->ignore_count)
15486 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
15487
15488 if (tp->type != bp_dprintf && tp->commands)
15489 {
15490 struct gdb_exception exception;
15491
15492 fprintf_unfiltered (fp, " commands\n");
15493
15494 ui_out_redirect (current_uiout, fp);
15495 TRY
15496 {
15497 print_command_lines (current_uiout, tp->commands->commands, 2);
15498 }
15499 CATCH (ex, RETURN_MASK_ALL)
15500 {
15501 ui_out_redirect (current_uiout, NULL);
15502 throw_exception (ex);
15503 }
15504 END_CATCH
15505
15506 ui_out_redirect (current_uiout, NULL);
15507 fprintf_unfiltered (fp, " end\n");
15508 }
15509
15510 if (tp->enable_state == bp_disabled)
15511 fprintf_unfiltered (fp, "disable $bpnum\n");
15512
15513 /* If this is a multi-location breakpoint, check if the locations
15514 should be individually disabled. Watchpoint locations are
15515 special, and not user visible. */
15516 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15517 {
15518 struct bp_location *loc;
15519 int n = 1;
15520
15521 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15522 if (!loc->enabled)
15523 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
15524 }
15525 }
15526
15527 if (extra_trace_bits && *default_collect)
15528 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
15529
15530 if (from_tty)
15531 printf_filtered (_("Saved to file '%s'.\n"), filename);
15532 do_cleanups (cleanup);
15533 }
15534
15535 /* The `save breakpoints' command. */
15536
15537 static void
15538 save_breakpoints_command (char *args, int from_tty)
15539 {
15540 save_breakpoints (args, from_tty, NULL);
15541 }
15542
15543 /* The `save tracepoints' command. */
15544
15545 static void
15546 save_tracepoints_command (char *args, int from_tty)
15547 {
15548 save_breakpoints (args, from_tty, is_tracepoint);
15549 }
15550
15551 /* Create a vector of all tracepoints. */
15552
15553 VEC(breakpoint_p) *
15554 all_tracepoints (void)
15555 {
15556 VEC(breakpoint_p) *tp_vec = 0;
15557 struct breakpoint *tp;
15558
15559 ALL_TRACEPOINTS (tp)
15560 {
15561 VEC_safe_push (breakpoint_p, tp_vec, tp);
15562 }
15563
15564 return tp_vec;
15565 }
15566
15567 \f
15568 /* This help string is used for the break, hbreak, tbreak and thbreak
15569 commands. It is defined as a macro to prevent duplication.
15570 COMMAND should be a string constant containing the name of the
15571 command. */
15572 #define BREAK_ARGS_HELP(command) \
15573 command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15574 PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15575 probe point. Accepted values are `-probe' (for a generic, automatically\n\
15576 guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15577 `-probe-dtrace' (for a DTrace probe).\n\
15578 LOCATION may be a line number, function name, or \"*\" and an address.\n\
15579 If a line number is specified, break at start of code for that line.\n\
15580 If a function is specified, break at start of code for that function.\n\
15581 If an address is specified, break at that exact address.\n\
15582 With no LOCATION, uses current execution address of the selected\n\
15583 stack frame. This is useful for breaking on return to a stack frame.\n\
15584 \n\
15585 THREADNUM is the number from \"info threads\".\n\
15586 CONDITION is a boolean expression.\n\
15587 \n\
15588 Multiple breakpoints at one place are permitted, and useful if their\n\
15589 conditions are different.\n\
15590 \n\
15591 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15592
15593 /* List of subcommands for "catch". */
15594 static struct cmd_list_element *catch_cmdlist;
15595
15596 /* List of subcommands for "tcatch". */
15597 static struct cmd_list_element *tcatch_cmdlist;
15598
15599 void
15600 add_catch_command (char *name, char *docstring,
15601 cmd_sfunc_ftype *sfunc,
15602 completer_ftype *completer,
15603 void *user_data_catch,
15604 void *user_data_tcatch)
15605 {
15606 struct cmd_list_element *command;
15607
15608 command = add_cmd (name, class_breakpoint, NULL, docstring,
15609 &catch_cmdlist);
15610 set_cmd_sfunc (command, sfunc);
15611 set_cmd_context (command, user_data_catch);
15612 set_cmd_completer (command, completer);
15613
15614 command = add_cmd (name, class_breakpoint, NULL, docstring,
15615 &tcatch_cmdlist);
15616 set_cmd_sfunc (command, sfunc);
15617 set_cmd_context (command, user_data_tcatch);
15618 set_cmd_completer (command, completer);
15619 }
15620
15621 static void
15622 save_command (char *arg, int from_tty)
15623 {
15624 printf_unfiltered (_("\"save\" must be followed by "
15625 "the name of a save subcommand.\n"));
15626 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
15627 }
15628
15629 struct breakpoint *
15630 iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15631 void *data)
15632 {
15633 struct breakpoint *b, *b_tmp;
15634
15635 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15636 {
15637 if ((*callback) (b, data))
15638 return b;
15639 }
15640
15641 return NULL;
15642 }
15643
15644 /* Zero if any of the breakpoint's locations could be a location where
15645 functions have been inlined, nonzero otherwise. */
15646
15647 static int
15648 is_non_inline_function (struct breakpoint *b)
15649 {
15650 /* The shared library event breakpoint is set on the address of a
15651 non-inline function. */
15652 if (b->type == bp_shlib_event)
15653 return 1;
15654
15655 return 0;
15656 }
15657
15658 /* Nonzero if the specified PC cannot be a location where functions
15659 have been inlined. */
15660
15661 int
15662 pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
15663 const struct target_waitstatus *ws)
15664 {
15665 struct breakpoint *b;
15666 struct bp_location *bl;
15667
15668 ALL_BREAKPOINTS (b)
15669 {
15670 if (!is_non_inline_function (b))
15671 continue;
15672
15673 for (bl = b->loc; bl != NULL; bl = bl->next)
15674 {
15675 if (!bl->shlib_disabled
15676 && bpstat_check_location (bl, aspace, pc, ws))
15677 return 1;
15678 }
15679 }
15680
15681 return 0;
15682 }
15683
15684 /* Remove any references to OBJFILE which is going to be freed. */
15685
15686 void
15687 breakpoint_free_objfile (struct objfile *objfile)
15688 {
15689 struct bp_location **locp, *loc;
15690
15691 ALL_BP_LOCATIONS (loc, locp)
15692 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
15693 loc->symtab = NULL;
15694 }
15695
15696 void
15697 initialize_breakpoint_ops (void)
15698 {
15699 static int initialized = 0;
15700
15701 struct breakpoint_ops *ops;
15702
15703 if (initialized)
15704 return;
15705 initialized = 1;
15706
15707 /* The breakpoint_ops structure to be inherit by all kinds of
15708 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15709 internal and momentary breakpoints, etc.). */
15710 ops = &bkpt_base_breakpoint_ops;
15711 *ops = base_breakpoint_ops;
15712 ops->re_set = bkpt_re_set;
15713 ops->insert_location = bkpt_insert_location;
15714 ops->remove_location = bkpt_remove_location;
15715 ops->breakpoint_hit = bkpt_breakpoint_hit;
15716 ops->create_sals_from_location = bkpt_create_sals_from_location;
15717 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
15718 ops->decode_location = bkpt_decode_location;
15719
15720 /* The breakpoint_ops structure to be used in regular breakpoints. */
15721 ops = &bkpt_breakpoint_ops;
15722 *ops = bkpt_base_breakpoint_ops;
15723 ops->re_set = bkpt_re_set;
15724 ops->resources_needed = bkpt_resources_needed;
15725 ops->print_it = bkpt_print_it;
15726 ops->print_mention = bkpt_print_mention;
15727 ops->print_recreate = bkpt_print_recreate;
15728
15729 /* Ranged breakpoints. */
15730 ops = &ranged_breakpoint_ops;
15731 *ops = bkpt_breakpoint_ops;
15732 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15733 ops->resources_needed = resources_needed_ranged_breakpoint;
15734 ops->print_it = print_it_ranged_breakpoint;
15735 ops->print_one = print_one_ranged_breakpoint;
15736 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15737 ops->print_mention = print_mention_ranged_breakpoint;
15738 ops->print_recreate = print_recreate_ranged_breakpoint;
15739
15740 /* Internal breakpoints. */
15741 ops = &internal_breakpoint_ops;
15742 *ops = bkpt_base_breakpoint_ops;
15743 ops->re_set = internal_bkpt_re_set;
15744 ops->check_status = internal_bkpt_check_status;
15745 ops->print_it = internal_bkpt_print_it;
15746 ops->print_mention = internal_bkpt_print_mention;
15747
15748 /* Momentary breakpoints. */
15749 ops = &momentary_breakpoint_ops;
15750 *ops = bkpt_base_breakpoint_ops;
15751 ops->re_set = momentary_bkpt_re_set;
15752 ops->check_status = momentary_bkpt_check_status;
15753 ops->print_it = momentary_bkpt_print_it;
15754 ops->print_mention = momentary_bkpt_print_mention;
15755
15756 /* Momentary breakpoints for bp_longjmp and bp_exception. */
15757 ops = &longjmp_breakpoint_ops;
15758 *ops = momentary_breakpoint_ops;
15759 ops->dtor = longjmp_bkpt_dtor;
15760
15761 /* Probe breakpoints. */
15762 ops = &bkpt_probe_breakpoint_ops;
15763 *ops = bkpt_breakpoint_ops;
15764 ops->insert_location = bkpt_probe_insert_location;
15765 ops->remove_location = bkpt_probe_remove_location;
15766 ops->create_sals_from_location = bkpt_probe_create_sals_from_location;
15767 ops->decode_location = bkpt_probe_decode_location;
15768
15769 /* Watchpoints. */
15770 ops = &watchpoint_breakpoint_ops;
15771 *ops = base_breakpoint_ops;
15772 ops->dtor = dtor_watchpoint;
15773 ops->re_set = re_set_watchpoint;
15774 ops->insert_location = insert_watchpoint;
15775 ops->remove_location = remove_watchpoint;
15776 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15777 ops->check_status = check_status_watchpoint;
15778 ops->resources_needed = resources_needed_watchpoint;
15779 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15780 ops->print_it = print_it_watchpoint;
15781 ops->print_mention = print_mention_watchpoint;
15782 ops->print_recreate = print_recreate_watchpoint;
15783 ops->explains_signal = explains_signal_watchpoint;
15784
15785 /* Masked watchpoints. */
15786 ops = &masked_watchpoint_breakpoint_ops;
15787 *ops = watchpoint_breakpoint_ops;
15788 ops->insert_location = insert_masked_watchpoint;
15789 ops->remove_location = remove_masked_watchpoint;
15790 ops->resources_needed = resources_needed_masked_watchpoint;
15791 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15792 ops->print_it = print_it_masked_watchpoint;
15793 ops->print_one_detail = print_one_detail_masked_watchpoint;
15794 ops->print_mention = print_mention_masked_watchpoint;
15795 ops->print_recreate = print_recreate_masked_watchpoint;
15796
15797 /* Tracepoints. */
15798 ops = &tracepoint_breakpoint_ops;
15799 *ops = base_breakpoint_ops;
15800 ops->re_set = tracepoint_re_set;
15801 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15802 ops->print_one_detail = tracepoint_print_one_detail;
15803 ops->print_mention = tracepoint_print_mention;
15804 ops->print_recreate = tracepoint_print_recreate;
15805 ops->create_sals_from_location = tracepoint_create_sals_from_location;
15806 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
15807 ops->decode_location = tracepoint_decode_location;
15808
15809 /* Probe tracepoints. */
15810 ops = &tracepoint_probe_breakpoint_ops;
15811 *ops = tracepoint_breakpoint_ops;
15812 ops->create_sals_from_location = tracepoint_probe_create_sals_from_location;
15813 ops->decode_location = tracepoint_probe_decode_location;
15814
15815 /* Static tracepoints with marker (`-m'). */
15816 ops = &strace_marker_breakpoint_ops;
15817 *ops = tracepoint_breakpoint_ops;
15818 ops->create_sals_from_location = strace_marker_create_sals_from_location;
15819 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
15820 ops->decode_location = strace_marker_decode_location;
15821
15822 /* Fork catchpoints. */
15823 ops = &catch_fork_breakpoint_ops;
15824 *ops = base_breakpoint_ops;
15825 ops->insert_location = insert_catch_fork;
15826 ops->remove_location = remove_catch_fork;
15827 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15828 ops->print_it = print_it_catch_fork;
15829 ops->print_one = print_one_catch_fork;
15830 ops->print_mention = print_mention_catch_fork;
15831 ops->print_recreate = print_recreate_catch_fork;
15832
15833 /* Vfork catchpoints. */
15834 ops = &catch_vfork_breakpoint_ops;
15835 *ops = base_breakpoint_ops;
15836 ops->insert_location = insert_catch_vfork;
15837 ops->remove_location = remove_catch_vfork;
15838 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15839 ops->print_it = print_it_catch_vfork;
15840 ops->print_one = print_one_catch_vfork;
15841 ops->print_mention = print_mention_catch_vfork;
15842 ops->print_recreate = print_recreate_catch_vfork;
15843
15844 /* Exec catchpoints. */
15845 ops = &catch_exec_breakpoint_ops;
15846 *ops = base_breakpoint_ops;
15847 ops->dtor = dtor_catch_exec;
15848 ops->insert_location = insert_catch_exec;
15849 ops->remove_location = remove_catch_exec;
15850 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15851 ops->print_it = print_it_catch_exec;
15852 ops->print_one = print_one_catch_exec;
15853 ops->print_mention = print_mention_catch_exec;
15854 ops->print_recreate = print_recreate_catch_exec;
15855
15856 /* Solib-related catchpoints. */
15857 ops = &catch_solib_breakpoint_ops;
15858 *ops = base_breakpoint_ops;
15859 ops->dtor = dtor_catch_solib;
15860 ops->insert_location = insert_catch_solib;
15861 ops->remove_location = remove_catch_solib;
15862 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15863 ops->check_status = check_status_catch_solib;
15864 ops->print_it = print_it_catch_solib;
15865 ops->print_one = print_one_catch_solib;
15866 ops->print_mention = print_mention_catch_solib;
15867 ops->print_recreate = print_recreate_catch_solib;
15868
15869 ops = &dprintf_breakpoint_ops;
15870 *ops = bkpt_base_breakpoint_ops;
15871 ops->re_set = dprintf_re_set;
15872 ops->resources_needed = bkpt_resources_needed;
15873 ops->print_it = bkpt_print_it;
15874 ops->print_mention = bkpt_print_mention;
15875 ops->print_recreate = dprintf_print_recreate;
15876 ops->after_condition_true = dprintf_after_condition_true;
15877 ops->breakpoint_hit = dprintf_breakpoint_hit;
15878 }
15879
15880 /* Chain containing all defined "enable breakpoint" subcommands. */
15881
15882 static struct cmd_list_element *enablebreaklist = NULL;
15883
15884 void
15885 _initialize_breakpoint (void)
15886 {
15887 struct cmd_list_element *c;
15888
15889 initialize_breakpoint_ops ();
15890
15891 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
15892 observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
15893 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
15894
15895 breakpoint_objfile_key
15896 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
15897
15898 breakpoint_chain = 0;
15899 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
15900 before a breakpoint is set. */
15901 breakpoint_count = 0;
15902
15903 tracepoint_count = 0;
15904
15905 add_com ("ignore", class_breakpoint, ignore_command, _("\
15906 Set ignore-count of breakpoint number N to COUNT.\n\
15907 Usage is `ignore N COUNT'."));
15908
15909 add_com ("commands", class_breakpoint, commands_command, _("\
15910 Set commands to be executed when a breakpoint is hit.\n\
15911 Give breakpoint number as argument after \"commands\".\n\
15912 With no argument, the targeted breakpoint is the last one set.\n\
15913 The commands themselves follow starting on the next line.\n\
15914 Type a line containing \"end\" to indicate the end of them.\n\
15915 Give \"silent\" as the first line to make the breakpoint silent;\n\
15916 then no output is printed when it is hit, except what the commands print."));
15917
15918 c = add_com ("condition", class_breakpoint, condition_command, _("\
15919 Specify breakpoint number N to break only if COND is true.\n\
15920 Usage is `condition N COND', where N is an integer and COND is an\n\
15921 expression to be evaluated whenever breakpoint N is reached."));
15922 set_cmd_completer (c, condition_completer);
15923
15924 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
15925 Set a temporary breakpoint.\n\
15926 Like \"break\" except the breakpoint is only temporary,\n\
15927 so it will be deleted when hit. Equivalent to \"break\" followed\n\
15928 by using \"enable delete\" on the breakpoint number.\n\
15929 \n"
15930 BREAK_ARGS_HELP ("tbreak")));
15931 set_cmd_completer (c, location_completer);
15932
15933 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
15934 Set a hardware assisted breakpoint.\n\
15935 Like \"break\" except the breakpoint requires hardware support,\n\
15936 some target hardware may not have this support.\n\
15937 \n"
15938 BREAK_ARGS_HELP ("hbreak")));
15939 set_cmd_completer (c, location_completer);
15940
15941 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
15942 Set a temporary hardware assisted breakpoint.\n\
15943 Like \"hbreak\" except the breakpoint is only temporary,\n\
15944 so it will be deleted when hit.\n\
15945 \n"
15946 BREAK_ARGS_HELP ("thbreak")));
15947 set_cmd_completer (c, location_completer);
15948
15949 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
15950 Enable some breakpoints.\n\
15951 Give breakpoint numbers (separated by spaces) as arguments.\n\
15952 With no subcommand, breakpoints are enabled until you command otherwise.\n\
15953 This is used to cancel the effect of the \"disable\" command.\n\
15954 With a subcommand you can enable temporarily."),
15955 &enablelist, "enable ", 1, &cmdlist);
15956
15957 add_com_alias ("en", "enable", class_breakpoint, 1);
15958
15959 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
15960 Enable some breakpoints.\n\
15961 Give breakpoint numbers (separated by spaces) as arguments.\n\
15962 This is used to cancel the effect of the \"disable\" command.\n\
15963 May be abbreviated to simply \"enable\".\n"),
15964 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
15965
15966 add_cmd ("once", no_class, enable_once_command, _("\
15967 Enable breakpoints for one hit. Give breakpoint numbers.\n\
15968 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15969 &enablebreaklist);
15970
15971 add_cmd ("delete", no_class, enable_delete_command, _("\
15972 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
15973 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15974 &enablebreaklist);
15975
15976 add_cmd ("count", no_class, enable_count_command, _("\
15977 Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
15978 If a breakpoint is hit while enabled in this fashion,\n\
15979 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15980 &enablebreaklist);
15981
15982 add_cmd ("delete", no_class, enable_delete_command, _("\
15983 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
15984 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15985 &enablelist);
15986
15987 add_cmd ("once", no_class, enable_once_command, _("\
15988 Enable breakpoints for one hit. Give breakpoint numbers.\n\
15989 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15990 &enablelist);
15991
15992 add_cmd ("count", no_class, enable_count_command, _("\
15993 Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
15994 If a breakpoint is hit while enabled in this fashion,\n\
15995 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15996 &enablelist);
15997
15998 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
15999 Disable some breakpoints.\n\
16000 Arguments are breakpoint numbers with spaces in between.\n\
16001 To disable all breakpoints, give no argument.\n\
16002 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
16003 &disablelist, "disable ", 1, &cmdlist);
16004 add_com_alias ("dis", "disable", class_breakpoint, 1);
16005 add_com_alias ("disa", "disable", class_breakpoint, 1);
16006
16007 add_cmd ("breakpoints", class_alias, disable_command, _("\
16008 Disable some breakpoints.\n\
16009 Arguments are breakpoint numbers with spaces in between.\n\
16010 To disable all breakpoints, give no argument.\n\
16011 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
16012 This command may be abbreviated \"disable\"."),
16013 &disablelist);
16014
16015 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
16016 Delete some breakpoints or auto-display expressions.\n\
16017 Arguments are breakpoint numbers with spaces in between.\n\
16018 To delete all breakpoints, give no argument.\n\
16019 \n\
16020 Also a prefix command for deletion of other GDB objects.\n\
16021 The \"unset\" command is also an alias for \"delete\"."),
16022 &deletelist, "delete ", 1, &cmdlist);
16023 add_com_alias ("d", "delete", class_breakpoint, 1);
16024 add_com_alias ("del", "delete", class_breakpoint, 1);
16025
16026 add_cmd ("breakpoints", class_alias, delete_command, _("\
16027 Delete some breakpoints or auto-display expressions.\n\
16028 Arguments are breakpoint numbers with spaces in between.\n\
16029 To delete all breakpoints, give no argument.\n\
16030 This command may be abbreviated \"delete\"."),
16031 &deletelist);
16032
16033 add_com ("clear", class_breakpoint, clear_command, _("\
16034 Clear breakpoint at specified line or function.\n\
16035 Argument may be line number, function name, or \"*\" and an address.\n\
16036 If line number is specified, all breakpoints in that line are cleared.\n\
16037 If function is specified, breakpoints at beginning of function are cleared.\n\
16038 If an address is specified, breakpoints at that address are cleared.\n\
16039 \n\
16040 With no argument, clears all breakpoints in the line that the selected frame\n\
16041 is executing in.\n\
16042 \n\
16043 See also the \"delete\" command which clears breakpoints by number."));
16044 add_com_alias ("cl", "clear", class_breakpoint, 1);
16045
16046 c = add_com ("break", class_breakpoint, break_command, _("\
16047 Set breakpoint at specified line or function.\n"
16048 BREAK_ARGS_HELP ("break")));
16049 set_cmd_completer (c, location_completer);
16050
16051 add_com_alias ("b", "break", class_run, 1);
16052 add_com_alias ("br", "break", class_run, 1);
16053 add_com_alias ("bre", "break", class_run, 1);
16054 add_com_alias ("brea", "break", class_run, 1);
16055
16056 if (dbx_commands)
16057 {
16058 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
16059 Break in function/address or break at a line in the current file."),
16060 &stoplist, "stop ", 1, &cmdlist);
16061 add_cmd ("in", class_breakpoint, stopin_command,
16062 _("Break in function or address."), &stoplist);
16063 add_cmd ("at", class_breakpoint, stopat_command,
16064 _("Break at a line in the current file."), &stoplist);
16065 add_com ("status", class_info, breakpoints_info, _("\
16066 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
16067 The \"Type\" column indicates one of:\n\
16068 \tbreakpoint - normal breakpoint\n\
16069 \twatchpoint - watchpoint\n\
16070 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16071 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16072 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16073 address and file/line number respectively.\n\
16074 \n\
16075 Convenience variable \"$_\" and default examine address for \"x\"\n\
16076 are set to the address of the last breakpoint listed unless the command\n\
16077 is prefixed with \"server \".\n\n\
16078 Convenience variable \"$bpnum\" contains the number of the last\n\
16079 breakpoint set."));
16080 }
16081
16082 add_info ("breakpoints", breakpoints_info, _("\
16083 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
16084 The \"Type\" column indicates one of:\n\
16085 \tbreakpoint - normal breakpoint\n\
16086 \twatchpoint - watchpoint\n\
16087 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16088 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16089 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16090 address and file/line number respectively.\n\
16091 \n\
16092 Convenience variable \"$_\" and default examine address for \"x\"\n\
16093 are set to the address of the last breakpoint listed unless the command\n\
16094 is prefixed with \"server \".\n\n\
16095 Convenience variable \"$bpnum\" contains the number of the last\n\
16096 breakpoint set."));
16097
16098 add_info_alias ("b", "breakpoints", 1);
16099
16100 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
16101 Status of all breakpoints, or breakpoint number NUMBER.\n\
16102 The \"Type\" column indicates one of:\n\
16103 \tbreakpoint - normal breakpoint\n\
16104 \twatchpoint - watchpoint\n\
16105 \tlongjmp - internal breakpoint used to step through longjmp()\n\
16106 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
16107 \tuntil - internal breakpoint used by the \"until\" command\n\
16108 \tfinish - internal breakpoint used by the \"finish\" command\n\
16109 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16110 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16111 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16112 address and file/line number respectively.\n\
16113 \n\
16114 Convenience variable \"$_\" and default examine address for \"x\"\n\
16115 are set to the address of the last breakpoint listed unless the command\n\
16116 is prefixed with \"server \".\n\n\
16117 Convenience variable \"$bpnum\" contains the number of the last\n\
16118 breakpoint set."),
16119 &maintenanceinfolist);
16120
16121 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
16122 Set catchpoints to catch events."),
16123 &catch_cmdlist, "catch ",
16124 0/*allow-unknown*/, &cmdlist);
16125
16126 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
16127 Set temporary catchpoints to catch events."),
16128 &tcatch_cmdlist, "tcatch ",
16129 0/*allow-unknown*/, &cmdlist);
16130
16131 add_catch_command ("fork", _("Catch calls to fork."),
16132 catch_fork_command_1,
16133 NULL,
16134 (void *) (uintptr_t) catch_fork_permanent,
16135 (void *) (uintptr_t) catch_fork_temporary);
16136 add_catch_command ("vfork", _("Catch calls to vfork."),
16137 catch_fork_command_1,
16138 NULL,
16139 (void *) (uintptr_t) catch_vfork_permanent,
16140 (void *) (uintptr_t) catch_vfork_temporary);
16141 add_catch_command ("exec", _("Catch calls to exec."),
16142 catch_exec_command_1,
16143 NULL,
16144 CATCH_PERMANENT,
16145 CATCH_TEMPORARY);
16146 add_catch_command ("load", _("Catch loads of shared libraries.\n\
16147 Usage: catch load [REGEX]\n\
16148 If REGEX is given, only stop for libraries matching the regular expression."),
16149 catch_load_command_1,
16150 NULL,
16151 CATCH_PERMANENT,
16152 CATCH_TEMPORARY);
16153 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
16154 Usage: catch unload [REGEX]\n\
16155 If REGEX is given, only stop for libraries matching the regular expression."),
16156 catch_unload_command_1,
16157 NULL,
16158 CATCH_PERMANENT,
16159 CATCH_TEMPORARY);
16160
16161 c = add_com ("watch", class_breakpoint, watch_command, _("\
16162 Set a watchpoint for an expression.\n\
16163 Usage: watch [-l|-location] EXPRESSION\n\
16164 A watchpoint stops execution of your program whenever the value of\n\
16165 an expression changes.\n\
16166 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16167 the memory to which it refers."));
16168 set_cmd_completer (c, expression_completer);
16169
16170 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
16171 Set a read watchpoint for an expression.\n\
16172 Usage: rwatch [-l|-location] EXPRESSION\n\
16173 A watchpoint stops execution of your program whenever the value of\n\
16174 an expression is read.\n\
16175 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16176 the memory to which it refers."));
16177 set_cmd_completer (c, expression_completer);
16178
16179 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
16180 Set a watchpoint for an expression.\n\
16181 Usage: awatch [-l|-location] EXPRESSION\n\
16182 A watchpoint stops execution of your program whenever the value of\n\
16183 an expression is either read or written.\n\
16184 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16185 the memory to which it refers."));
16186 set_cmd_completer (c, expression_completer);
16187
16188 add_info ("watchpoints", watchpoints_info, _("\
16189 Status of specified watchpoints (all watchpoints if no argument)."));
16190
16191 /* XXX: cagney/2005-02-23: This should be a boolean, and should
16192 respond to changes - contrary to the description. */
16193 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
16194 &can_use_hw_watchpoints, _("\
16195 Set debugger's willingness to use watchpoint hardware."), _("\
16196 Show debugger's willingness to use watchpoint hardware."), _("\
16197 If zero, gdb will not use hardware for new watchpoints, even if\n\
16198 such is available. (However, any hardware watchpoints that were\n\
16199 created before setting this to nonzero, will continue to use watchpoint\n\
16200 hardware.)"),
16201 NULL,
16202 show_can_use_hw_watchpoints,
16203 &setlist, &showlist);
16204
16205 can_use_hw_watchpoints = 1;
16206
16207 /* Tracepoint manipulation commands. */
16208
16209 c = add_com ("trace", class_breakpoint, trace_command, _("\
16210 Set a tracepoint at specified line or function.\n\
16211 \n"
16212 BREAK_ARGS_HELP ("trace") "\n\
16213 Do \"help tracepoints\" for info on other tracepoint commands."));
16214 set_cmd_completer (c, location_completer);
16215
16216 add_com_alias ("tp", "trace", class_alias, 0);
16217 add_com_alias ("tr", "trace", class_alias, 1);
16218 add_com_alias ("tra", "trace", class_alias, 1);
16219 add_com_alias ("trac", "trace", class_alias, 1);
16220
16221 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
16222 Set a fast tracepoint at specified line or function.\n\
16223 \n"
16224 BREAK_ARGS_HELP ("ftrace") "\n\
16225 Do \"help tracepoints\" for info on other tracepoint commands."));
16226 set_cmd_completer (c, location_completer);
16227
16228 c = add_com ("strace", class_breakpoint, strace_command, _("\
16229 Set a static tracepoint at specified line, function or marker.\n\
16230 \n\
16231 strace [LOCATION] [if CONDITION]\n\
16232 LOCATION may be a line number, function name, \"*\" and an address,\n\
16233 or -m MARKER_ID.\n\
16234 If a line number is specified, probe the marker at start of code\n\
16235 for that line. If a function is specified, probe the marker at start\n\
16236 of code for that function. If an address is specified, probe the marker\n\
16237 at that exact address. If a marker id is specified, probe the marker\n\
16238 with that name. With no LOCATION, uses current execution address of\n\
16239 the selected stack frame.\n\
16240 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16241 This collects arbitrary user data passed in the probe point call to the\n\
16242 tracing library. You can inspect it when analyzing the trace buffer,\n\
16243 by printing the $_sdata variable like any other convenience variable.\n\
16244 \n\
16245 CONDITION is a boolean expression.\n\
16246 \n\
16247 Multiple tracepoints at one place are permitted, and useful if their\n\
16248 conditions are different.\n\
16249 \n\
16250 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16251 Do \"help tracepoints\" for info on other tracepoint commands."));
16252 set_cmd_completer (c, location_completer);
16253
16254 add_info ("tracepoints", tracepoints_info, _("\
16255 Status of specified tracepoints (all tracepoints if no argument).\n\
16256 Convenience variable \"$tpnum\" contains the number of the\n\
16257 last tracepoint set."));
16258
16259 add_info_alias ("tp", "tracepoints", 1);
16260
16261 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16262 Delete specified tracepoints.\n\
16263 Arguments are tracepoint numbers, separated by spaces.\n\
16264 No argument means delete all tracepoints."),
16265 &deletelist);
16266 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
16267
16268 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16269 Disable specified tracepoints.\n\
16270 Arguments are tracepoint numbers, separated by spaces.\n\
16271 No argument means disable all tracepoints."),
16272 &disablelist);
16273 deprecate_cmd (c, "disable");
16274
16275 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16276 Enable specified tracepoints.\n\
16277 Arguments are tracepoint numbers, separated by spaces.\n\
16278 No argument means enable all tracepoints."),
16279 &enablelist);
16280 deprecate_cmd (c, "enable");
16281
16282 add_com ("passcount", class_trace, trace_pass_command, _("\
16283 Set the passcount for a tracepoint.\n\
16284 The trace will end when the tracepoint has been passed 'count' times.\n\
16285 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16286 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16287
16288 add_prefix_cmd ("save", class_breakpoint, save_command,
16289 _("Save breakpoint definitions as a script."),
16290 &save_cmdlist, "save ",
16291 0/*allow-unknown*/, &cmdlist);
16292
16293 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16294 Save current breakpoint definitions as a script.\n\
16295 This includes all types of breakpoints (breakpoints, watchpoints,\n\
16296 catchpoints, tracepoints). Use the 'source' command in another debug\n\
16297 session to restore them."),
16298 &save_cmdlist);
16299 set_cmd_completer (c, filename_completer);
16300
16301 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
16302 Save current tracepoint definitions as a script.\n\
16303 Use the 'source' command in another debug session to restore them."),
16304 &save_cmdlist);
16305 set_cmd_completer (c, filename_completer);
16306
16307 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16308 deprecate_cmd (c, "save tracepoints");
16309
16310 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
16311 Breakpoint specific settings\n\
16312 Configure various breakpoint-specific variables such as\n\
16313 pending breakpoint behavior"),
16314 &breakpoint_set_cmdlist, "set breakpoint ",
16315 0/*allow-unknown*/, &setlist);
16316 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
16317 Breakpoint specific settings\n\
16318 Configure various breakpoint-specific variables such as\n\
16319 pending breakpoint behavior"),
16320 &breakpoint_show_cmdlist, "show breakpoint ",
16321 0/*allow-unknown*/, &showlist);
16322
16323 add_setshow_auto_boolean_cmd ("pending", no_class,
16324 &pending_break_support, _("\
16325 Set debugger's behavior regarding pending breakpoints."), _("\
16326 Show debugger's behavior regarding pending breakpoints."), _("\
16327 If on, an unrecognized breakpoint location will cause gdb to create a\n\
16328 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16329 an error. If auto, an unrecognized breakpoint location results in a\n\
16330 user-query to see if a pending breakpoint should be created."),
16331 NULL,
16332 show_pending_break_support,
16333 &breakpoint_set_cmdlist,
16334 &breakpoint_show_cmdlist);
16335
16336 pending_break_support = AUTO_BOOLEAN_AUTO;
16337
16338 add_setshow_boolean_cmd ("auto-hw", no_class,
16339 &automatic_hardware_breakpoints, _("\
16340 Set automatic usage of hardware breakpoints."), _("\
16341 Show automatic usage of hardware breakpoints."), _("\
16342 If set, the debugger will automatically use hardware breakpoints for\n\
16343 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16344 a warning will be emitted for such breakpoints."),
16345 NULL,
16346 show_automatic_hardware_breakpoints,
16347 &breakpoint_set_cmdlist,
16348 &breakpoint_show_cmdlist);
16349
16350 add_setshow_boolean_cmd ("always-inserted", class_support,
16351 &always_inserted_mode, _("\
16352 Set mode for inserting breakpoints."), _("\
16353 Show mode for inserting breakpoints."), _("\
16354 When this mode is on, breakpoints are inserted immediately as soon as\n\
16355 they're created, kept inserted even when execution stops, and removed\n\
16356 only when the user deletes them. When this mode is off (the default),\n\
16357 breakpoints are inserted only when execution continues, and removed\n\
16358 when execution stops."),
16359 NULL,
16360 &show_always_inserted_mode,
16361 &breakpoint_set_cmdlist,
16362 &breakpoint_show_cmdlist);
16363
16364 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16365 condition_evaluation_enums,
16366 &condition_evaluation_mode_1, _("\
16367 Set mode of breakpoint condition evaluation."), _("\
16368 Show mode of breakpoint condition evaluation."), _("\
16369 When this is set to \"host\", breakpoint conditions will be\n\
16370 evaluated on the host's side by GDB. When it is set to \"target\",\n\
16371 breakpoint conditions will be downloaded to the target (if the target\n\
16372 supports such feature) and conditions will be evaluated on the target's side.\n\
16373 If this is set to \"auto\" (default), this will be automatically set to\n\
16374 \"target\" if it supports condition evaluation, otherwise it will\n\
16375 be set to \"gdb\""),
16376 &set_condition_evaluation_mode,
16377 &show_condition_evaluation_mode,
16378 &breakpoint_set_cmdlist,
16379 &breakpoint_show_cmdlist);
16380
16381 add_com ("break-range", class_breakpoint, break_range_command, _("\
16382 Set a breakpoint for an address range.\n\
16383 break-range START-LOCATION, END-LOCATION\n\
16384 where START-LOCATION and END-LOCATION can be one of the following:\n\
16385 LINENUM, for that line in the current file,\n\
16386 FILE:LINENUM, for that line in that file,\n\
16387 +OFFSET, for that number of lines after the current line\n\
16388 or the start of the range\n\
16389 FUNCTION, for the first line in that function,\n\
16390 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16391 *ADDRESS, for the instruction at that address.\n\
16392 \n\
16393 The breakpoint will stop execution of the inferior whenever it executes\n\
16394 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16395 range (including START-LOCATION and END-LOCATION)."));
16396
16397 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
16398 Set a dynamic printf at specified line or function.\n\
16399 dprintf location,format string,arg1,arg2,...\n\
16400 location may be a line number, function name, or \"*\" and an address.\n\
16401 If a line number is specified, break at start of code for that line.\n\
16402 If a function is specified, break at start of code for that function."));
16403 set_cmd_completer (c, location_completer);
16404
16405 add_setshow_enum_cmd ("dprintf-style", class_support,
16406 dprintf_style_enums, &dprintf_style, _("\
16407 Set the style of usage for dynamic printf."), _("\
16408 Show the style of usage for dynamic printf."), _("\
16409 This setting chooses how GDB will do a dynamic printf.\n\
16410 If the value is \"gdb\", then the printing is done by GDB to its own\n\
16411 console, as with the \"printf\" command.\n\
16412 If the value is \"call\", the print is done by calling a function in your\n\
16413 program; by default printf(), but you can choose a different function or\n\
16414 output stream by setting dprintf-function and dprintf-channel."),
16415 update_dprintf_commands, NULL,
16416 &setlist, &showlist);
16417
16418 dprintf_function = xstrdup ("printf");
16419 add_setshow_string_cmd ("dprintf-function", class_support,
16420 &dprintf_function, _("\
16421 Set the function to use for dynamic printf"), _("\
16422 Show the function to use for dynamic printf"), NULL,
16423 update_dprintf_commands, NULL,
16424 &setlist, &showlist);
16425
16426 dprintf_channel = xstrdup ("");
16427 add_setshow_string_cmd ("dprintf-channel", class_support,
16428 &dprintf_channel, _("\
16429 Set the channel to use for dynamic printf"), _("\
16430 Show the channel to use for dynamic printf"), NULL,
16431 update_dprintf_commands, NULL,
16432 &setlist, &showlist);
16433
16434 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16435 &disconnected_dprintf, _("\
16436 Set whether dprintf continues after GDB disconnects."), _("\
16437 Show whether dprintf continues after GDB disconnects."), _("\
16438 Use this to let dprintf commands continue to hit and produce output\n\
16439 even if GDB disconnects or detaches from the target."),
16440 NULL,
16441 NULL,
16442 &setlist, &showlist);
16443
16444 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16445 agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16446 (target agent only) This is useful for formatted output in user-defined commands."));
16447
16448 automatic_hardware_breakpoints = 1;
16449
16450 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
16451 observer_attach_thread_exit (remove_threaded_breakpoints);
16452 }
This page took 0.415304 seconds and 4 git commands to generate.