windows-nat: Replace __COPY_CONTEXT_SIZE conditional with __CYGWIN__
[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
71 /* readline include files */
72 #include "readline/readline.h"
73 #include "readline/history.h"
74
75 /* readline defines this. */
76 #undef savestring
77
78 #include "mi/mi-common.h"
79 #include "extension.h"
80
81 /* Enums for exception-handling support. */
82 enum exception_event_kind
83 {
84 EX_EVENT_THROW,
85 EX_EVENT_RETHROW,
86 EX_EVENT_CATCH
87 };
88
89 /* Prototypes for local functions. */
90
91 static void enable_delete_command (char *, int);
92
93 static void enable_once_command (char *, int);
94
95 static void enable_count_command (char *, int);
96
97 static void disable_command (char *, int);
98
99 static void enable_command (char *, int);
100
101 static void map_breakpoint_numbers (char *, void (*) (struct breakpoint *,
102 void *),
103 void *);
104
105 static void ignore_command (char *, int);
106
107 static int breakpoint_re_set_one (void *);
108
109 static void breakpoint_re_set_default (struct breakpoint *);
110
111 static void create_sals_from_address_default (char **,
112 struct linespec_result *,
113 enum bptype, char *,
114 char **);
115
116 static void create_breakpoints_sal_default (struct gdbarch *,
117 struct linespec_result *,
118 char *, char *, enum bptype,
119 enum bpdisp, int, int,
120 int,
121 const struct breakpoint_ops *,
122 int, int, int, unsigned);
123
124 static void decode_linespec_default (struct breakpoint *, char **,
125 struct symtabs_and_lines *);
126
127 static void clear_command (char *, int);
128
129 static void catch_command (char *, int);
130
131 static int can_use_hardware_watchpoint (struct value *);
132
133 static void break_command_1 (char *, int, int);
134
135 static void mention (struct breakpoint *);
136
137 static struct breakpoint *set_raw_breakpoint_without_location (struct gdbarch *,
138 enum bptype,
139 const struct breakpoint_ops *);
140 static struct bp_location *add_location_to_breakpoint (struct breakpoint *,
141 const struct symtab_and_line *);
142
143 /* This function is used in gdbtk sources and thus can not be made
144 static. */
145 struct breakpoint *set_raw_breakpoint (struct gdbarch *gdbarch,
146 struct symtab_and_line,
147 enum bptype,
148 const struct breakpoint_ops *);
149
150 static struct breakpoint *
151 momentary_breakpoint_from_master (struct breakpoint *orig,
152 enum bptype type,
153 const struct breakpoint_ops *ops,
154 int loc_enabled);
155
156 static void breakpoint_adjustment_warning (CORE_ADDR, CORE_ADDR, int, int);
157
158 static CORE_ADDR adjust_breakpoint_address (struct gdbarch *gdbarch,
159 CORE_ADDR bpaddr,
160 enum bptype bptype);
161
162 static void describe_other_breakpoints (struct gdbarch *,
163 struct program_space *, CORE_ADDR,
164 struct obj_section *, int);
165
166 static int watchpoint_locations_match (struct bp_location *loc1,
167 struct bp_location *loc2);
168
169 static int breakpoint_location_address_match (struct bp_location *bl,
170 struct address_space *aspace,
171 CORE_ADDR addr);
172
173 static void breakpoints_info (char *, int);
174
175 static void watchpoints_info (char *, int);
176
177 static int breakpoint_1 (char *, int,
178 int (*) (const struct breakpoint *));
179
180 static int breakpoint_cond_eval (void *);
181
182 static void cleanup_executing_breakpoints (void *);
183
184 static void commands_command (char *, int);
185
186 static void condition_command (char *, int);
187
188 typedef enum
189 {
190 mark_inserted,
191 mark_uninserted
192 }
193 insertion_state_t;
194
195 static int remove_breakpoint (struct bp_location *, insertion_state_t);
196 static int remove_breakpoint_1 (struct bp_location *, insertion_state_t);
197
198 static enum print_stop_action print_bp_stop_message (bpstat bs);
199
200 static int watchpoint_check (void *);
201
202 static void maintenance_info_breakpoints (char *, int);
203
204 static int hw_breakpoint_used_count (void);
205
206 static int hw_watchpoint_use_count (struct breakpoint *);
207
208 static int hw_watchpoint_used_count_others (struct breakpoint *except,
209 enum bptype type,
210 int *other_type_used);
211
212 static void hbreak_command (char *, int);
213
214 static void thbreak_command (char *, int);
215
216 static void enable_breakpoint_disp (struct breakpoint *, enum bpdisp,
217 int count);
218
219 static void stop_command (char *arg, int from_tty);
220
221 static void stopin_command (char *arg, int from_tty);
222
223 static void stopat_command (char *arg, int from_tty);
224
225 static void tcatch_command (char *arg, int from_tty);
226
227 static void free_bp_location (struct bp_location *loc);
228 static void incref_bp_location (struct bp_location *loc);
229 static void decref_bp_location (struct bp_location **loc);
230
231 static struct bp_location *allocate_bp_location (struct breakpoint *bpt);
232
233 /* update_global_location_list's modes of operation wrt to whether to
234 insert locations now. */
235 enum ugll_insert_mode
236 {
237 /* Don't insert any breakpoint locations into the inferior, only
238 remove already-inserted locations that no longer should be
239 inserted. Functions that delete a breakpoint or breakpoints
240 should specify this mode, so that deleting a breakpoint doesn't
241 have the side effect of inserting the locations of other
242 breakpoints that are marked not-inserted, but should_be_inserted
243 returns true on them.
244
245 This behavior is useful is situations close to tear-down -- e.g.,
246 after an exec, while the target still has execution, but
247 breakpoint shadows of the previous executable image should *NOT*
248 be restored to the new image; or before detaching, where the
249 target still has execution and wants to delete breakpoints from
250 GDB's lists, and all breakpoints had already been removed from
251 the inferior. */
252 UGLL_DONT_INSERT,
253
254 /* May insert breakpoints iff breakpoints_should_be_inserted_now
255 claims breakpoints should be inserted now. */
256 UGLL_MAY_INSERT,
257
258 /* Insert locations now, irrespective of
259 breakpoints_should_be_inserted_now. E.g., say all threads are
260 stopped right now, and the user did "continue". We need to
261 insert breakpoints _before_ resuming the target, but
262 UGLL_MAY_INSERT wouldn't insert them, because
263 breakpoints_should_be_inserted_now returns false at that point,
264 as no thread is running yet. */
265 UGLL_INSERT
266 };
267
268 static void update_global_location_list (enum ugll_insert_mode);
269
270 static void update_global_location_list_nothrow (enum ugll_insert_mode);
271
272 static int is_hardware_watchpoint (const struct breakpoint *bpt);
273
274 static void insert_breakpoint_locations (void);
275
276 static void tracepoints_info (char *, int);
277
278 static void delete_trace_command (char *, int);
279
280 static void enable_trace_command (char *, int);
281
282 static void disable_trace_command (char *, int);
283
284 static void trace_pass_command (char *, int);
285
286 static void set_tracepoint_count (int num);
287
288 static int is_masked_watchpoint (const struct breakpoint *b);
289
290 static struct bp_location **get_first_locp_gte_addr (CORE_ADDR address);
291
292 /* Return 1 if B refers to a static tracepoint set by marker ("-m"), zero
293 otherwise. */
294
295 static int strace_marker_p (struct breakpoint *b);
296
297 /* The breakpoint_ops structure to be inherited by all breakpoint_ops
298 that are implemented on top of software or hardware breakpoints
299 (user breakpoints, internal and momentary breakpoints, etc.). */
300 static struct breakpoint_ops bkpt_base_breakpoint_ops;
301
302 /* Internal breakpoints class type. */
303 static struct breakpoint_ops internal_breakpoint_ops;
304
305 /* Momentary breakpoints class type. */
306 static struct breakpoint_ops momentary_breakpoint_ops;
307
308 /* Momentary breakpoints for bp_longjmp and bp_exception class type. */
309 static struct breakpoint_ops longjmp_breakpoint_ops;
310
311 /* The breakpoint_ops structure to be used in regular user created
312 breakpoints. */
313 struct breakpoint_ops bkpt_breakpoint_ops;
314
315 /* Breakpoints set on probes. */
316 static struct breakpoint_ops bkpt_probe_breakpoint_ops;
317
318 /* Dynamic printf class type. */
319 struct breakpoint_ops dprintf_breakpoint_ops;
320
321 /* The style in which to perform a dynamic printf. This is a user
322 option because different output options have different tradeoffs;
323 if GDB does the printing, there is better error handling if there
324 is a problem with any of the arguments, but using an inferior
325 function lets you have special-purpose printers and sending of
326 output to the same place as compiled-in print functions. */
327
328 static const char dprintf_style_gdb[] = "gdb";
329 static const char dprintf_style_call[] = "call";
330 static const char dprintf_style_agent[] = "agent";
331 static const char *const dprintf_style_enums[] = {
332 dprintf_style_gdb,
333 dprintf_style_call,
334 dprintf_style_agent,
335 NULL
336 };
337 static const char *dprintf_style = dprintf_style_gdb;
338
339 /* The function to use for dynamic printf if the preferred style is to
340 call into the inferior. The value is simply a string that is
341 copied into the command, so it can be anything that GDB can
342 evaluate to a callable address, not necessarily a function name. */
343
344 static char *dprintf_function = "";
345
346 /* The channel to use for dynamic printf if the preferred style is to
347 call into the inferior; if a nonempty string, it will be passed to
348 the call as the first argument, with the format string as the
349 second. As with the dprintf function, this can be anything that
350 GDB knows how to evaluate, so in addition to common choices like
351 "stderr", this could be an app-specific expression like
352 "mystreams[curlogger]". */
353
354 static char *dprintf_channel = "";
355
356 /* True if dprintf commands should continue to operate even if GDB
357 has disconnected. */
358 static int disconnected_dprintf = 1;
359
360 /* A reference-counted struct command_line. This lets multiple
361 breakpoints share a single command list. */
362 struct counted_command_line
363 {
364 /* The reference count. */
365 int refc;
366
367 /* The command list. */
368 struct command_line *commands;
369 };
370
371 struct command_line *
372 breakpoint_commands (struct breakpoint *b)
373 {
374 return b->commands ? b->commands->commands : NULL;
375 }
376
377 /* Flag indicating that a command has proceeded the inferior past the
378 current breakpoint. */
379
380 static int breakpoint_proceeded;
381
382 const char *
383 bpdisp_text (enum bpdisp disp)
384 {
385 /* NOTE: the following values are a part of MI protocol and
386 represent values of 'disp' field returned when inferior stops at
387 a breakpoint. */
388 static const char * const bpdisps[] = {"del", "dstp", "dis", "keep"};
389
390 return bpdisps[(int) disp];
391 }
392
393 /* Prototypes for exported functions. */
394 /* If FALSE, gdb will not use hardware support for watchpoints, even
395 if such is available. */
396 static int can_use_hw_watchpoints;
397
398 static void
399 show_can_use_hw_watchpoints (struct ui_file *file, int from_tty,
400 struct cmd_list_element *c,
401 const char *value)
402 {
403 fprintf_filtered (file,
404 _("Debugger's willingness to use "
405 "watchpoint hardware is %s.\n"),
406 value);
407 }
408
409 /* If AUTO_BOOLEAN_FALSE, gdb will not attempt to create pending breakpoints.
410 If AUTO_BOOLEAN_TRUE, gdb will automatically create pending breakpoints
411 for unrecognized breakpoint locations.
412 If AUTO_BOOLEAN_AUTO, gdb will query when breakpoints are unrecognized. */
413 static enum auto_boolean pending_break_support;
414 static void
415 show_pending_break_support (struct ui_file *file, int from_tty,
416 struct cmd_list_element *c,
417 const char *value)
418 {
419 fprintf_filtered (file,
420 _("Debugger's behavior regarding "
421 "pending breakpoints is %s.\n"),
422 value);
423 }
424
425 /* If 1, gdb will automatically use hardware breakpoints for breakpoints
426 set with "break" but falling in read-only memory.
427 If 0, gdb will warn about such breakpoints, but won't automatically
428 use hardware breakpoints. */
429 static int automatic_hardware_breakpoints;
430 static void
431 show_automatic_hardware_breakpoints (struct ui_file *file, int from_tty,
432 struct cmd_list_element *c,
433 const char *value)
434 {
435 fprintf_filtered (file,
436 _("Automatic usage of hardware breakpoints is %s.\n"),
437 value);
438 }
439
440 /* If on, GDB keeps breakpoints inserted even if the inferior is
441 stopped, and immediately inserts any new breakpoints as soon as
442 they're created. If off (default), GDB keeps breakpoints off of
443 the target as long as possible. That is, it delays inserting
444 breakpoints until the next resume, and removes them again when the
445 target fully stops. This is a bit safer in case GDB crashes while
446 processing user input. */
447 static int always_inserted_mode = 0;
448
449 static void
450 show_always_inserted_mode (struct ui_file *file, int from_tty,
451 struct cmd_list_element *c, const char *value)
452 {
453 fprintf_filtered (file, _("Always inserted breakpoint mode is %s.\n"),
454 value);
455 }
456
457 /* See breakpoint.h. */
458
459 int
460 breakpoints_should_be_inserted_now (void)
461 {
462 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
463 {
464 /* If breakpoints are global, they should be inserted even if no
465 thread under gdb's control is running, or even if there are
466 no threads under GDB's control yet. */
467 return 1;
468 }
469 else if (target_has_execution)
470 {
471 if (always_inserted_mode)
472 {
473 /* The user wants breakpoints inserted even if all threads
474 are stopped. */
475 return 1;
476 }
477
478 if (threads_are_executing ())
479 return 1;
480 }
481 return 0;
482 }
483
484 static const char condition_evaluation_both[] = "host or target";
485
486 /* Modes for breakpoint condition evaluation. */
487 static const char condition_evaluation_auto[] = "auto";
488 static const char condition_evaluation_host[] = "host";
489 static const char condition_evaluation_target[] = "target";
490 static const char *const condition_evaluation_enums[] = {
491 condition_evaluation_auto,
492 condition_evaluation_host,
493 condition_evaluation_target,
494 NULL
495 };
496
497 /* Global that holds the current mode for breakpoint condition evaluation. */
498 static const char *condition_evaluation_mode_1 = condition_evaluation_auto;
499
500 /* Global that we use to display information to the user (gets its value from
501 condition_evaluation_mode_1. */
502 static const char *condition_evaluation_mode = condition_evaluation_auto;
503
504 /* Translate a condition evaluation mode MODE into either "host"
505 or "target". This is used mostly to translate from "auto" to the
506 real setting that is being used. It returns the translated
507 evaluation mode. */
508
509 static const char *
510 translate_condition_evaluation_mode (const char *mode)
511 {
512 if (mode == condition_evaluation_auto)
513 {
514 if (target_supports_evaluation_of_breakpoint_conditions ())
515 return condition_evaluation_target;
516 else
517 return condition_evaluation_host;
518 }
519 else
520 return mode;
521 }
522
523 /* Discovers what condition_evaluation_auto translates to. */
524
525 static const char *
526 breakpoint_condition_evaluation_mode (void)
527 {
528 return translate_condition_evaluation_mode (condition_evaluation_mode);
529 }
530
531 /* Return true if GDB should evaluate breakpoint conditions or false
532 otherwise. */
533
534 static int
535 gdb_evaluates_breakpoint_condition_p (void)
536 {
537 const char *mode = breakpoint_condition_evaluation_mode ();
538
539 return (mode == condition_evaluation_host);
540 }
541
542 void _initialize_breakpoint (void);
543
544 /* Are we executing breakpoint commands? */
545 static int executing_breakpoint_commands;
546
547 /* Are overlay event breakpoints enabled? */
548 static int overlay_events_enabled;
549
550 /* See description in breakpoint.h. */
551 int target_exact_watchpoints = 0;
552
553 /* Walk the following statement or block through all breakpoints.
554 ALL_BREAKPOINTS_SAFE does so even if the statement deletes the
555 current breakpoint. */
556
557 #define ALL_BREAKPOINTS(B) for (B = breakpoint_chain; B; B = B->next)
558
559 #define ALL_BREAKPOINTS_SAFE(B,TMP) \
560 for (B = breakpoint_chain; \
561 B ? (TMP=B->next, 1): 0; \
562 B = TMP)
563
564 /* Similar iterator for the low-level breakpoints. SAFE variant is
565 not provided so update_global_location_list must not be called
566 while executing the block of ALL_BP_LOCATIONS. */
567
568 #define ALL_BP_LOCATIONS(B,BP_TMP) \
569 for (BP_TMP = bp_location; \
570 BP_TMP < bp_location + bp_location_count && (B = *BP_TMP); \
571 BP_TMP++)
572
573 /* Iterates through locations with address ADDRESS for the currently selected
574 program space. BP_LOCP_TMP points to each object. BP_LOCP_START points
575 to where the loop should start from.
576 If BP_LOCP_START is a NULL pointer, the macro automatically seeks the
577 appropriate location to start with. */
578
579 #define ALL_BP_LOCATIONS_AT_ADDR(BP_LOCP_TMP, BP_LOCP_START, ADDRESS) \
580 for (BP_LOCP_START = BP_LOCP_START == NULL ? get_first_locp_gte_addr (ADDRESS) : BP_LOCP_START, \
581 BP_LOCP_TMP = BP_LOCP_START; \
582 BP_LOCP_START \
583 && (BP_LOCP_TMP < bp_location + bp_location_count \
584 && (*BP_LOCP_TMP)->address == ADDRESS); \
585 BP_LOCP_TMP++)
586
587 /* Iterator for tracepoints only. */
588
589 #define ALL_TRACEPOINTS(B) \
590 for (B = breakpoint_chain; B; B = B->next) \
591 if (is_tracepoint (B))
592
593 /* Chains of all breakpoints defined. */
594
595 struct breakpoint *breakpoint_chain;
596
597 /* Array is sorted by bp_location_compare - primarily by the ADDRESS. */
598
599 static struct bp_location **bp_location;
600
601 /* Number of elements of BP_LOCATION. */
602
603 static unsigned bp_location_count;
604
605 /* Maximum alignment offset between bp_target_info.PLACED_ADDRESS and
606 ADDRESS for the current elements of BP_LOCATION which get a valid
607 result from bp_location_has_shadow. You can use it for roughly
608 limiting the subrange of BP_LOCATION to scan for shadow bytes for
609 an address you need to read. */
610
611 static CORE_ADDR bp_location_placed_address_before_address_max;
612
613 /* Maximum offset plus alignment between bp_target_info.PLACED_ADDRESS
614 + bp_target_info.SHADOW_LEN and ADDRESS for the current elements of
615 BP_LOCATION which get a valid result from bp_location_has_shadow.
616 You can use it for roughly limiting the subrange of BP_LOCATION to
617 scan for shadow bytes for an address you need to read. */
618
619 static CORE_ADDR bp_location_shadow_len_after_address_max;
620
621 /* The locations that no longer correspond to any breakpoint, unlinked
622 from bp_location array, but for which a hit may still be reported
623 by a target. */
624 VEC(bp_location_p) *moribund_locations = NULL;
625
626 /* Number of last breakpoint made. */
627
628 static int breakpoint_count;
629
630 /* The value of `breakpoint_count' before the last command that
631 created breakpoints. If the last (break-like) command created more
632 than one breakpoint, then the difference between BREAKPOINT_COUNT
633 and PREV_BREAKPOINT_COUNT is more than one. */
634 static int prev_breakpoint_count;
635
636 /* Number of last tracepoint made. */
637
638 static int tracepoint_count;
639
640 static struct cmd_list_element *breakpoint_set_cmdlist;
641 static struct cmd_list_element *breakpoint_show_cmdlist;
642 struct cmd_list_element *save_cmdlist;
643
644 /* See declaration at breakpoint.h. */
645
646 struct breakpoint *
647 breakpoint_find_if (int (*func) (struct breakpoint *b, void *d),
648 void *user_data)
649 {
650 struct breakpoint *b = NULL;
651
652 ALL_BREAKPOINTS (b)
653 {
654 if (func (b, user_data) != 0)
655 break;
656 }
657
658 return b;
659 }
660
661 /* Return whether a breakpoint is an active enabled breakpoint. */
662 static int
663 breakpoint_enabled (struct breakpoint *b)
664 {
665 return (b->enable_state == bp_enabled);
666 }
667
668 /* Set breakpoint count to NUM. */
669
670 static void
671 set_breakpoint_count (int num)
672 {
673 prev_breakpoint_count = breakpoint_count;
674 breakpoint_count = num;
675 set_internalvar_integer (lookup_internalvar ("bpnum"), num);
676 }
677
678 /* Used by `start_rbreak_breakpoints' below, to record the current
679 breakpoint count before "rbreak" creates any breakpoint. */
680 static int rbreak_start_breakpoint_count;
681
682 /* Called at the start an "rbreak" command to record the first
683 breakpoint made. */
684
685 void
686 start_rbreak_breakpoints (void)
687 {
688 rbreak_start_breakpoint_count = breakpoint_count;
689 }
690
691 /* Called at the end of an "rbreak" command to record the last
692 breakpoint made. */
693
694 void
695 end_rbreak_breakpoints (void)
696 {
697 prev_breakpoint_count = rbreak_start_breakpoint_count;
698 }
699
700 /* Used in run_command to zero the hit count when a new run starts. */
701
702 void
703 clear_breakpoint_hit_counts (void)
704 {
705 struct breakpoint *b;
706
707 ALL_BREAKPOINTS (b)
708 b->hit_count = 0;
709 }
710
711 /* Allocate a new counted_command_line with reference count of 1.
712 The new structure owns COMMANDS. */
713
714 static struct counted_command_line *
715 alloc_counted_command_line (struct command_line *commands)
716 {
717 struct counted_command_line *result
718 = xmalloc (sizeof (struct counted_command_line));
719
720 result->refc = 1;
721 result->commands = commands;
722 return result;
723 }
724
725 /* Increment reference count. This does nothing if CMD is NULL. */
726
727 static void
728 incref_counted_command_line (struct counted_command_line *cmd)
729 {
730 if (cmd)
731 ++cmd->refc;
732 }
733
734 /* Decrement reference count. If the reference count reaches 0,
735 destroy the counted_command_line. Sets *CMDP to NULL. This does
736 nothing if *CMDP is NULL. */
737
738 static void
739 decref_counted_command_line (struct counted_command_line **cmdp)
740 {
741 if (*cmdp)
742 {
743 if (--(*cmdp)->refc == 0)
744 {
745 free_command_lines (&(*cmdp)->commands);
746 xfree (*cmdp);
747 }
748 *cmdp = NULL;
749 }
750 }
751
752 /* A cleanup function that calls decref_counted_command_line. */
753
754 static void
755 do_cleanup_counted_command_line (void *arg)
756 {
757 decref_counted_command_line (arg);
758 }
759
760 /* Create a cleanup that calls decref_counted_command_line on the
761 argument. */
762
763 static struct cleanup *
764 make_cleanup_decref_counted_command_line (struct counted_command_line **cmdp)
765 {
766 return make_cleanup (do_cleanup_counted_command_line, cmdp);
767 }
768
769 \f
770 /* Return the breakpoint with the specified number, or NULL
771 if the number does not refer to an existing breakpoint. */
772
773 struct breakpoint *
774 get_breakpoint (int num)
775 {
776 struct breakpoint *b;
777
778 ALL_BREAKPOINTS (b)
779 if (b->number == num)
780 return b;
781
782 return NULL;
783 }
784
785 \f
786
787 /* Mark locations as "conditions have changed" in case the target supports
788 evaluating conditions on its side. */
789
790 static void
791 mark_breakpoint_modified (struct breakpoint *b)
792 {
793 struct bp_location *loc;
794
795 /* This is only meaningful if the target is
796 evaluating conditions and if the user has
797 opted for condition evaluation on the target's
798 side. */
799 if (gdb_evaluates_breakpoint_condition_p ()
800 || !target_supports_evaluation_of_breakpoint_conditions ())
801 return;
802
803 if (!is_breakpoint (b))
804 return;
805
806 for (loc = b->loc; loc; loc = loc->next)
807 loc->condition_changed = condition_modified;
808 }
809
810 /* Mark location as "conditions have changed" in case the target supports
811 evaluating conditions on its side. */
812
813 static void
814 mark_breakpoint_location_modified (struct bp_location *loc)
815 {
816 /* This is only meaningful if the target is
817 evaluating conditions and if the user has
818 opted for condition evaluation on the target's
819 side. */
820 if (gdb_evaluates_breakpoint_condition_p ()
821 || !target_supports_evaluation_of_breakpoint_conditions ())
822
823 return;
824
825 if (!is_breakpoint (loc->owner))
826 return;
827
828 loc->condition_changed = condition_modified;
829 }
830
831 /* Sets the condition-evaluation mode using the static global
832 condition_evaluation_mode. */
833
834 static void
835 set_condition_evaluation_mode (char *args, int from_tty,
836 struct cmd_list_element *c)
837 {
838 const char *old_mode, *new_mode;
839
840 if ((condition_evaluation_mode_1 == condition_evaluation_target)
841 && !target_supports_evaluation_of_breakpoint_conditions ())
842 {
843 condition_evaluation_mode_1 = condition_evaluation_mode;
844 warning (_("Target does not support breakpoint condition evaluation.\n"
845 "Using host evaluation mode instead."));
846 return;
847 }
848
849 new_mode = translate_condition_evaluation_mode (condition_evaluation_mode_1);
850 old_mode = translate_condition_evaluation_mode (condition_evaluation_mode);
851
852 /* Flip the switch. Flip it even if OLD_MODE == NEW_MODE as one of the
853 settings was "auto". */
854 condition_evaluation_mode = condition_evaluation_mode_1;
855
856 /* Only update the mode if the user picked a different one. */
857 if (new_mode != old_mode)
858 {
859 struct bp_location *loc, **loc_tmp;
860 /* If the user switched to a different evaluation mode, we
861 need to synch the changes with the target as follows:
862
863 "host" -> "target": Send all (valid) conditions to the target.
864 "target" -> "host": Remove all the conditions from the target.
865 */
866
867 if (new_mode == condition_evaluation_target)
868 {
869 /* Mark everything modified and synch conditions with the
870 target. */
871 ALL_BP_LOCATIONS (loc, loc_tmp)
872 mark_breakpoint_location_modified (loc);
873 }
874 else
875 {
876 /* Manually mark non-duplicate locations to synch conditions
877 with the target. We do this to remove all the conditions the
878 target knows about. */
879 ALL_BP_LOCATIONS (loc, loc_tmp)
880 if (is_breakpoint (loc->owner) && loc->inserted)
881 loc->needs_update = 1;
882 }
883
884 /* Do the update. */
885 update_global_location_list (UGLL_MAY_INSERT);
886 }
887
888 return;
889 }
890
891 /* Shows the current mode of breakpoint condition evaluation. Explicitly shows
892 what "auto" is translating to. */
893
894 static void
895 show_condition_evaluation_mode (struct ui_file *file, int from_tty,
896 struct cmd_list_element *c, const char *value)
897 {
898 if (condition_evaluation_mode == condition_evaluation_auto)
899 fprintf_filtered (file,
900 _("Breakpoint condition evaluation "
901 "mode is %s (currently %s).\n"),
902 value,
903 breakpoint_condition_evaluation_mode ());
904 else
905 fprintf_filtered (file, _("Breakpoint condition evaluation mode is %s.\n"),
906 value);
907 }
908
909 /* A comparison function for bp_location AP and BP that is used by
910 bsearch. This comparison function only cares about addresses, unlike
911 the more general bp_location_compare function. */
912
913 static int
914 bp_location_compare_addrs (const void *ap, const void *bp)
915 {
916 struct bp_location *a = *(void **) ap;
917 struct bp_location *b = *(void **) bp;
918
919 if (a->address == b->address)
920 return 0;
921 else
922 return ((a->address > b->address) - (a->address < b->address));
923 }
924
925 /* Helper function to skip all bp_locations with addresses
926 less than ADDRESS. It returns the first bp_location that
927 is greater than or equal to ADDRESS. If none is found, just
928 return NULL. */
929
930 static struct bp_location **
931 get_first_locp_gte_addr (CORE_ADDR address)
932 {
933 struct bp_location dummy_loc;
934 struct bp_location *dummy_locp = &dummy_loc;
935 struct bp_location **locp_found = NULL;
936
937 /* Initialize the dummy location's address field. */
938 memset (&dummy_loc, 0, sizeof (struct bp_location));
939 dummy_loc.address = address;
940
941 /* Find a close match to the first location at ADDRESS. */
942 locp_found = bsearch (&dummy_locp, bp_location, bp_location_count,
943 sizeof (struct bp_location **),
944 bp_location_compare_addrs);
945
946 /* Nothing was found, nothing left to do. */
947 if (locp_found == NULL)
948 return NULL;
949
950 /* We may have found a location that is at ADDRESS but is not the first in the
951 location's list. Go backwards (if possible) and locate the first one. */
952 while ((locp_found - 1) >= bp_location
953 && (*(locp_found - 1))->address == address)
954 locp_found--;
955
956 return locp_found;
957 }
958
959 void
960 set_breakpoint_condition (struct breakpoint *b, const char *exp,
961 int from_tty)
962 {
963 xfree (b->cond_string);
964 b->cond_string = NULL;
965
966 if (is_watchpoint (b))
967 {
968 struct watchpoint *w = (struct watchpoint *) b;
969
970 xfree (w->cond_exp);
971 w->cond_exp = NULL;
972 }
973 else
974 {
975 struct bp_location *loc;
976
977 for (loc = b->loc; loc; loc = loc->next)
978 {
979 xfree (loc->cond);
980 loc->cond = NULL;
981
982 /* No need to free the condition agent expression
983 bytecode (if we have one). We will handle this
984 when we go through update_global_location_list. */
985 }
986 }
987
988 if (*exp == 0)
989 {
990 if (from_tty)
991 printf_filtered (_("Breakpoint %d now unconditional.\n"), b->number);
992 }
993 else
994 {
995 const char *arg = exp;
996
997 /* I don't know if it matters whether this is the string the user
998 typed in or the decompiled expression. */
999 b->cond_string = xstrdup (arg);
1000 b->condition_not_parsed = 0;
1001
1002 if (is_watchpoint (b))
1003 {
1004 struct watchpoint *w = (struct watchpoint *) b;
1005
1006 innermost_block = NULL;
1007 arg = exp;
1008 w->cond_exp = parse_exp_1 (&arg, 0, 0, 0);
1009 if (*arg)
1010 error (_("Junk at end of expression"));
1011 w->cond_exp_valid_block = innermost_block;
1012 }
1013 else
1014 {
1015 struct bp_location *loc;
1016
1017 for (loc = b->loc; loc; loc = loc->next)
1018 {
1019 arg = exp;
1020 loc->cond =
1021 parse_exp_1 (&arg, loc->address,
1022 block_for_pc (loc->address), 0);
1023 if (*arg)
1024 error (_("Junk at end of expression"));
1025 }
1026 }
1027 }
1028 mark_breakpoint_modified (b);
1029
1030 observer_notify_breakpoint_modified (b);
1031 }
1032
1033 /* Completion for the "condition" command. */
1034
1035 static VEC (char_ptr) *
1036 condition_completer (struct cmd_list_element *cmd,
1037 const char *text, const char *word)
1038 {
1039 const char *space;
1040
1041 text = skip_spaces_const (text);
1042 space = skip_to_space_const (text);
1043 if (*space == '\0')
1044 {
1045 int len;
1046 struct breakpoint *b;
1047 VEC (char_ptr) *result = NULL;
1048
1049 if (text[0] == '$')
1050 {
1051 /* We don't support completion of history indices. */
1052 if (isdigit (text[1]))
1053 return NULL;
1054 return complete_internalvar (&text[1]);
1055 }
1056
1057 /* We're completing the breakpoint number. */
1058 len = strlen (text);
1059
1060 ALL_BREAKPOINTS (b)
1061 {
1062 char number[50];
1063
1064 xsnprintf (number, sizeof (number), "%d", b->number);
1065
1066 if (strncmp (number, text, len) == 0)
1067 VEC_safe_push (char_ptr, result, xstrdup (number));
1068 }
1069
1070 return result;
1071 }
1072
1073 /* We're completing the expression part. */
1074 text = skip_spaces_const (space);
1075 return expression_completer (cmd, text, word);
1076 }
1077
1078 /* condition N EXP -- set break condition of breakpoint N to EXP. */
1079
1080 static void
1081 condition_command (char *arg, int from_tty)
1082 {
1083 struct breakpoint *b;
1084 char *p;
1085 int bnum;
1086
1087 if (arg == 0)
1088 error_no_arg (_("breakpoint number"));
1089
1090 p = arg;
1091 bnum = get_number (&p);
1092 if (bnum == 0)
1093 error (_("Bad breakpoint argument: '%s'"), arg);
1094
1095 ALL_BREAKPOINTS (b)
1096 if (b->number == bnum)
1097 {
1098 /* Check if this breakpoint has a "stop" method implemented in an
1099 extension language. This method and conditions entered into GDB
1100 from the CLI are mutually exclusive. */
1101 const struct extension_language_defn *extlang
1102 = get_breakpoint_cond_ext_lang (b, EXT_LANG_NONE);
1103
1104 if (extlang != NULL)
1105 {
1106 error (_("Only one stop condition allowed. There is currently"
1107 " a %s stop condition defined for this breakpoint."),
1108 ext_lang_capitalized_name (extlang));
1109 }
1110 set_breakpoint_condition (b, p, from_tty);
1111
1112 if (is_breakpoint (b))
1113 update_global_location_list (UGLL_MAY_INSERT);
1114
1115 return;
1116 }
1117
1118 error (_("No breakpoint number %d."), bnum);
1119 }
1120
1121 /* Check that COMMAND do not contain commands that are suitable
1122 only for tracepoints and not suitable for ordinary breakpoints.
1123 Throw if any such commands is found. */
1124
1125 static void
1126 check_no_tracepoint_commands (struct command_line *commands)
1127 {
1128 struct command_line *c;
1129
1130 for (c = commands; c; c = c->next)
1131 {
1132 int i;
1133
1134 if (c->control_type == while_stepping_control)
1135 error (_("The 'while-stepping' command can "
1136 "only be used for tracepoints"));
1137
1138 for (i = 0; i < c->body_count; ++i)
1139 check_no_tracepoint_commands ((c->body_list)[i]);
1140
1141 /* Not that command parsing removes leading whitespace and comment
1142 lines and also empty lines. So, we only need to check for
1143 command directly. */
1144 if (strstr (c->line, "collect ") == c->line)
1145 error (_("The 'collect' command can only be used for tracepoints"));
1146
1147 if (strstr (c->line, "teval ") == c->line)
1148 error (_("The 'teval' command can only be used for tracepoints"));
1149 }
1150 }
1151
1152 /* Encapsulate tests for different types of tracepoints. */
1153
1154 static int
1155 is_tracepoint_type (enum bptype type)
1156 {
1157 return (type == bp_tracepoint
1158 || type == bp_fast_tracepoint
1159 || type == bp_static_tracepoint);
1160 }
1161
1162 int
1163 is_tracepoint (const struct breakpoint *b)
1164 {
1165 return is_tracepoint_type (b->type);
1166 }
1167
1168 /* A helper function that validates that COMMANDS are valid for a
1169 breakpoint. This function will throw an exception if a problem is
1170 found. */
1171
1172 static void
1173 validate_commands_for_breakpoint (struct breakpoint *b,
1174 struct command_line *commands)
1175 {
1176 if (is_tracepoint (b))
1177 {
1178 struct tracepoint *t = (struct tracepoint *) b;
1179 struct command_line *c;
1180 struct command_line *while_stepping = 0;
1181
1182 /* Reset the while-stepping step count. The previous commands
1183 might have included a while-stepping action, while the new
1184 ones might not. */
1185 t->step_count = 0;
1186
1187 /* We need to verify that each top-level element of commands is
1188 valid for tracepoints, that there's at most one
1189 while-stepping element, and that the while-stepping's body
1190 has valid tracing commands excluding nested while-stepping.
1191 We also need to validate the tracepoint action line in the
1192 context of the tracepoint --- validate_actionline actually
1193 has side effects, like setting the tracepoint's
1194 while-stepping STEP_COUNT, in addition to checking if the
1195 collect/teval actions parse and make sense in the
1196 tracepoint's context. */
1197 for (c = commands; c; c = c->next)
1198 {
1199 if (c->control_type == while_stepping_control)
1200 {
1201 if (b->type == bp_fast_tracepoint)
1202 error (_("The 'while-stepping' command "
1203 "cannot be used for fast tracepoint"));
1204 else if (b->type == bp_static_tracepoint)
1205 error (_("The 'while-stepping' command "
1206 "cannot be used for static tracepoint"));
1207
1208 if (while_stepping)
1209 error (_("The 'while-stepping' command "
1210 "can be used only once"));
1211 else
1212 while_stepping = c;
1213 }
1214
1215 validate_actionline (c->line, b);
1216 }
1217 if (while_stepping)
1218 {
1219 struct command_line *c2;
1220
1221 gdb_assert (while_stepping->body_count == 1);
1222 c2 = while_stepping->body_list[0];
1223 for (; c2; c2 = c2->next)
1224 {
1225 if (c2->control_type == while_stepping_control)
1226 error (_("The 'while-stepping' command cannot be nested"));
1227 }
1228 }
1229 }
1230 else
1231 {
1232 check_no_tracepoint_commands (commands);
1233 }
1234 }
1235
1236 /* Return a vector of all the static tracepoints set at ADDR. The
1237 caller is responsible for releasing the vector. */
1238
1239 VEC(breakpoint_p) *
1240 static_tracepoints_here (CORE_ADDR addr)
1241 {
1242 struct breakpoint *b;
1243 VEC(breakpoint_p) *found = 0;
1244 struct bp_location *loc;
1245
1246 ALL_BREAKPOINTS (b)
1247 if (b->type == bp_static_tracepoint)
1248 {
1249 for (loc = b->loc; loc; loc = loc->next)
1250 if (loc->address == addr)
1251 VEC_safe_push(breakpoint_p, found, b);
1252 }
1253
1254 return found;
1255 }
1256
1257 /* Set the command list of B to COMMANDS. If breakpoint is tracepoint,
1258 validate that only allowed commands are included. */
1259
1260 void
1261 breakpoint_set_commands (struct breakpoint *b,
1262 struct command_line *commands)
1263 {
1264 validate_commands_for_breakpoint (b, commands);
1265
1266 decref_counted_command_line (&b->commands);
1267 b->commands = alloc_counted_command_line (commands);
1268 observer_notify_breakpoint_modified (b);
1269 }
1270
1271 /* Set the internal `silent' flag on the breakpoint. Note that this
1272 is not the same as the "silent" that may appear in the breakpoint's
1273 commands. */
1274
1275 void
1276 breakpoint_set_silent (struct breakpoint *b, int silent)
1277 {
1278 int old_silent = b->silent;
1279
1280 b->silent = silent;
1281 if (old_silent != silent)
1282 observer_notify_breakpoint_modified (b);
1283 }
1284
1285 /* Set the thread for this breakpoint. If THREAD is -1, make the
1286 breakpoint work for any thread. */
1287
1288 void
1289 breakpoint_set_thread (struct breakpoint *b, int thread)
1290 {
1291 int old_thread = b->thread;
1292
1293 b->thread = thread;
1294 if (old_thread != thread)
1295 observer_notify_breakpoint_modified (b);
1296 }
1297
1298 /* Set the task for this breakpoint. If TASK is 0, make the
1299 breakpoint work for any task. */
1300
1301 void
1302 breakpoint_set_task (struct breakpoint *b, int task)
1303 {
1304 int old_task = b->task;
1305
1306 b->task = task;
1307 if (old_task != task)
1308 observer_notify_breakpoint_modified (b);
1309 }
1310
1311 void
1312 check_tracepoint_command (char *line, void *closure)
1313 {
1314 struct breakpoint *b = closure;
1315
1316 validate_actionline (line, b);
1317 }
1318
1319 /* A structure used to pass information through
1320 map_breakpoint_numbers. */
1321
1322 struct commands_info
1323 {
1324 /* True if the command was typed at a tty. */
1325 int from_tty;
1326
1327 /* The breakpoint range spec. */
1328 char *arg;
1329
1330 /* Non-NULL if the body of the commands are being read from this
1331 already-parsed command. */
1332 struct command_line *control;
1333
1334 /* The command lines read from the user, or NULL if they have not
1335 yet been read. */
1336 struct counted_command_line *cmd;
1337 };
1338
1339 /* A callback for map_breakpoint_numbers that sets the commands for
1340 commands_command. */
1341
1342 static void
1343 do_map_commands_command (struct breakpoint *b, void *data)
1344 {
1345 struct commands_info *info = data;
1346
1347 if (info->cmd == NULL)
1348 {
1349 struct command_line *l;
1350
1351 if (info->control != NULL)
1352 l = copy_command_lines (info->control->body_list[0]);
1353 else
1354 {
1355 struct cleanup *old_chain;
1356 char *str;
1357
1358 str = xstrprintf (_("Type commands for breakpoint(s) "
1359 "%s, one per line."),
1360 info->arg);
1361
1362 old_chain = make_cleanup (xfree, str);
1363
1364 l = read_command_lines (str,
1365 info->from_tty, 1,
1366 (is_tracepoint (b)
1367 ? check_tracepoint_command : 0),
1368 b);
1369
1370 do_cleanups (old_chain);
1371 }
1372
1373 info->cmd = alloc_counted_command_line (l);
1374 }
1375
1376 /* If a breakpoint was on the list more than once, we don't need to
1377 do anything. */
1378 if (b->commands != info->cmd)
1379 {
1380 validate_commands_for_breakpoint (b, info->cmd->commands);
1381 incref_counted_command_line (info->cmd);
1382 decref_counted_command_line (&b->commands);
1383 b->commands = info->cmd;
1384 observer_notify_breakpoint_modified (b);
1385 }
1386 }
1387
1388 static void
1389 commands_command_1 (char *arg, int from_tty,
1390 struct command_line *control)
1391 {
1392 struct cleanup *cleanups;
1393 struct commands_info info;
1394
1395 info.from_tty = from_tty;
1396 info.control = control;
1397 info.cmd = NULL;
1398 /* If we read command lines from the user, then `info' will hold an
1399 extra reference to the commands that we must clean up. */
1400 cleanups = make_cleanup_decref_counted_command_line (&info.cmd);
1401
1402 if (arg == NULL || !*arg)
1403 {
1404 if (breakpoint_count - prev_breakpoint_count > 1)
1405 arg = xstrprintf ("%d-%d", prev_breakpoint_count + 1,
1406 breakpoint_count);
1407 else if (breakpoint_count > 0)
1408 arg = xstrprintf ("%d", breakpoint_count);
1409 else
1410 {
1411 /* So that we don't try to free the incoming non-NULL
1412 argument in the cleanup below. Mapping breakpoint
1413 numbers will fail in this case. */
1414 arg = NULL;
1415 }
1416 }
1417 else
1418 /* The command loop has some static state, so we need to preserve
1419 our argument. */
1420 arg = xstrdup (arg);
1421
1422 if (arg != NULL)
1423 make_cleanup (xfree, arg);
1424
1425 info.arg = arg;
1426
1427 map_breakpoint_numbers (arg, do_map_commands_command, &info);
1428
1429 if (info.cmd == NULL)
1430 error (_("No breakpoints specified."));
1431
1432 do_cleanups (cleanups);
1433 }
1434
1435 static void
1436 commands_command (char *arg, int from_tty)
1437 {
1438 commands_command_1 (arg, from_tty, NULL);
1439 }
1440
1441 /* Like commands_command, but instead of reading the commands from
1442 input stream, takes them from an already parsed command structure.
1443
1444 This is used by cli-script.c to DTRT with breakpoint commands
1445 that are part of if and while bodies. */
1446 enum command_control_type
1447 commands_from_control_command (char *arg, struct command_line *cmd)
1448 {
1449 commands_command_1 (arg, 0, cmd);
1450 return simple_control;
1451 }
1452
1453 /* Return non-zero if BL->TARGET_INFO contains valid information. */
1454
1455 static int
1456 bp_location_has_shadow (struct bp_location *bl)
1457 {
1458 if (bl->loc_type != bp_loc_software_breakpoint)
1459 return 0;
1460 if (!bl->inserted)
1461 return 0;
1462 if (bl->target_info.shadow_len == 0)
1463 /* BL isn't valid, or doesn't shadow memory. */
1464 return 0;
1465 return 1;
1466 }
1467
1468 /* Update BUF, which is LEN bytes read from the target address
1469 MEMADDR, by replacing a memory breakpoint with its shadowed
1470 contents.
1471
1472 If READBUF is not NULL, this buffer must not overlap with the of
1473 the breakpoint location's shadow_contents buffer. Otherwise, a
1474 failed assertion internal error will be raised. */
1475
1476 static void
1477 one_breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1478 const gdb_byte *writebuf_org,
1479 ULONGEST memaddr, LONGEST len,
1480 struct bp_target_info *target_info,
1481 struct gdbarch *gdbarch)
1482 {
1483 /* Now do full processing of the found relevant range of elements. */
1484 CORE_ADDR bp_addr = 0;
1485 int bp_size = 0;
1486 int bptoffset = 0;
1487
1488 if (!breakpoint_address_match (target_info->placed_address_space, 0,
1489 current_program_space->aspace, 0))
1490 {
1491 /* The breakpoint is inserted in a different address space. */
1492 return;
1493 }
1494
1495 /* Addresses and length of the part of the breakpoint that
1496 we need to copy. */
1497 bp_addr = target_info->placed_address;
1498 bp_size = target_info->shadow_len;
1499
1500 if (bp_addr + bp_size <= memaddr)
1501 {
1502 /* The breakpoint is entirely before the chunk of memory we are
1503 reading. */
1504 return;
1505 }
1506
1507 if (bp_addr >= memaddr + len)
1508 {
1509 /* The breakpoint is entirely after the chunk of memory we are
1510 reading. */
1511 return;
1512 }
1513
1514 /* Offset within shadow_contents. */
1515 if (bp_addr < memaddr)
1516 {
1517 /* Only copy the second part of the breakpoint. */
1518 bp_size -= memaddr - bp_addr;
1519 bptoffset = memaddr - bp_addr;
1520 bp_addr = memaddr;
1521 }
1522
1523 if (bp_addr + bp_size > memaddr + len)
1524 {
1525 /* Only copy the first part of the breakpoint. */
1526 bp_size -= (bp_addr + bp_size) - (memaddr + len);
1527 }
1528
1529 if (readbuf != NULL)
1530 {
1531 /* Verify that the readbuf buffer does not overlap with the
1532 shadow_contents buffer. */
1533 gdb_assert (target_info->shadow_contents >= readbuf + len
1534 || readbuf >= (target_info->shadow_contents
1535 + target_info->shadow_len));
1536
1537 /* Update the read buffer with this inserted breakpoint's
1538 shadow. */
1539 memcpy (readbuf + bp_addr - memaddr,
1540 target_info->shadow_contents + bptoffset, bp_size);
1541 }
1542 else
1543 {
1544 const unsigned char *bp;
1545 CORE_ADDR addr = target_info->reqstd_address;
1546 int placed_size;
1547
1548 /* Update the shadow with what we want to write to memory. */
1549 memcpy (target_info->shadow_contents + bptoffset,
1550 writebuf_org + bp_addr - memaddr, bp_size);
1551
1552 /* Determine appropriate breakpoint contents and size for this
1553 address. */
1554 bp = gdbarch_breakpoint_from_pc (gdbarch, &addr, &placed_size);
1555
1556 /* Update the final write buffer with this inserted
1557 breakpoint's INSN. */
1558 memcpy (writebuf + bp_addr - memaddr, bp + bptoffset, bp_size);
1559 }
1560 }
1561
1562 /* Update BUF, which is LEN bytes read from the target address MEMADDR,
1563 by replacing any memory breakpoints with their shadowed contents.
1564
1565 If READBUF is not NULL, this buffer must not overlap with any of
1566 the breakpoint location's shadow_contents buffers. Otherwise,
1567 a failed assertion internal error will be raised.
1568
1569 The range of shadowed area by each bp_location is:
1570 bl->address - bp_location_placed_address_before_address_max
1571 up to bl->address + bp_location_shadow_len_after_address_max
1572 The range we were requested to resolve shadows for is:
1573 memaddr ... memaddr + len
1574 Thus the safe cutoff boundaries for performance optimization are
1575 memaddr + len <= (bl->address
1576 - bp_location_placed_address_before_address_max)
1577 and:
1578 bl->address + bp_location_shadow_len_after_address_max <= memaddr */
1579
1580 void
1581 breakpoint_xfer_memory (gdb_byte *readbuf, gdb_byte *writebuf,
1582 const gdb_byte *writebuf_org,
1583 ULONGEST memaddr, LONGEST len)
1584 {
1585 /* Left boundary, right boundary and median element of our binary
1586 search. */
1587 unsigned bc_l, bc_r, bc;
1588 size_t i;
1589
1590 /* Find BC_L which is a leftmost element which may affect BUF
1591 content. It is safe to report lower value but a failure to
1592 report higher one. */
1593
1594 bc_l = 0;
1595 bc_r = bp_location_count;
1596 while (bc_l + 1 < bc_r)
1597 {
1598 struct bp_location *bl;
1599
1600 bc = (bc_l + bc_r) / 2;
1601 bl = bp_location[bc];
1602
1603 /* Check first BL->ADDRESS will not overflow due to the added
1604 constant. Then advance the left boundary only if we are sure
1605 the BC element can in no way affect the BUF content (MEMADDR
1606 to MEMADDR + LEN range).
1607
1608 Use the BP_LOCATION_SHADOW_LEN_AFTER_ADDRESS_MAX safety
1609 offset so that we cannot miss a breakpoint with its shadow
1610 range tail still reaching MEMADDR. */
1611
1612 if ((bl->address + bp_location_shadow_len_after_address_max
1613 >= bl->address)
1614 && (bl->address + bp_location_shadow_len_after_address_max
1615 <= memaddr))
1616 bc_l = bc;
1617 else
1618 bc_r = bc;
1619 }
1620
1621 /* Due to the binary search above, we need to make sure we pick the
1622 first location that's at BC_L's address. E.g., if there are
1623 multiple locations at the same address, BC_L may end up pointing
1624 at a duplicate location, and miss the "master"/"inserted"
1625 location. Say, given locations L1, L2 and L3 at addresses A and
1626 B:
1627
1628 L1@A, L2@A, L3@B, ...
1629
1630 BC_L could end up pointing at location L2, while the "master"
1631 location could be L1. Since the `loc->inserted' flag is only set
1632 on "master" locations, we'd forget to restore the shadow of L1
1633 and L2. */
1634 while (bc_l > 0
1635 && bp_location[bc_l]->address == bp_location[bc_l - 1]->address)
1636 bc_l--;
1637
1638 /* Now do full processing of the found relevant range of elements. */
1639
1640 for (bc = bc_l; bc < bp_location_count; bc++)
1641 {
1642 struct bp_location *bl = bp_location[bc];
1643 CORE_ADDR bp_addr = 0;
1644 int bp_size = 0;
1645 int bptoffset = 0;
1646
1647 /* bp_location array has BL->OWNER always non-NULL. */
1648 if (bl->owner->type == bp_none)
1649 warning (_("reading through apparently deleted breakpoint #%d?"),
1650 bl->owner->number);
1651
1652 /* Performance optimization: any further element can no longer affect BUF
1653 content. */
1654
1655 if (bl->address >= bp_location_placed_address_before_address_max
1656 && memaddr + len <= (bl->address
1657 - bp_location_placed_address_before_address_max))
1658 break;
1659
1660 if (!bp_location_has_shadow (bl))
1661 continue;
1662
1663 one_breakpoint_xfer_memory (readbuf, writebuf, writebuf_org,
1664 memaddr, len, &bl->target_info, bl->gdbarch);
1665 }
1666 }
1667
1668 \f
1669
1670 /* Return true if BPT is either a software breakpoint or a hardware
1671 breakpoint. */
1672
1673 int
1674 is_breakpoint (const struct breakpoint *bpt)
1675 {
1676 return (bpt->type == bp_breakpoint
1677 || bpt->type == bp_hardware_breakpoint
1678 || bpt->type == bp_dprintf);
1679 }
1680
1681 /* Return true if BPT is of any hardware watchpoint kind. */
1682
1683 static int
1684 is_hardware_watchpoint (const struct breakpoint *bpt)
1685 {
1686 return (bpt->type == bp_hardware_watchpoint
1687 || bpt->type == bp_read_watchpoint
1688 || bpt->type == bp_access_watchpoint);
1689 }
1690
1691 /* Return true if BPT is of any watchpoint kind, hardware or
1692 software. */
1693
1694 int
1695 is_watchpoint (const struct breakpoint *bpt)
1696 {
1697 return (is_hardware_watchpoint (bpt)
1698 || bpt->type == bp_watchpoint);
1699 }
1700
1701 /* Returns true if the current thread and its running state are safe
1702 to evaluate or update watchpoint B. Watchpoints on local
1703 expressions need to be evaluated in the context of the thread that
1704 was current when the watchpoint was created, and, that thread needs
1705 to be stopped to be able to select the correct frame context.
1706 Watchpoints on global expressions can be evaluated on any thread,
1707 and in any state. It is presently left to the target allowing
1708 memory accesses when threads are running. */
1709
1710 static int
1711 watchpoint_in_thread_scope (struct watchpoint *b)
1712 {
1713 return (b->base.pspace == current_program_space
1714 && (ptid_equal (b->watchpoint_thread, null_ptid)
1715 || (ptid_equal (inferior_ptid, b->watchpoint_thread)
1716 && !is_executing (inferior_ptid))));
1717 }
1718
1719 /* Set watchpoint B to disp_del_at_next_stop, even including its possible
1720 associated bp_watchpoint_scope breakpoint. */
1721
1722 static void
1723 watchpoint_del_at_next_stop (struct watchpoint *w)
1724 {
1725 struct breakpoint *b = &w->base;
1726
1727 if (b->related_breakpoint != b)
1728 {
1729 gdb_assert (b->related_breakpoint->type == bp_watchpoint_scope);
1730 gdb_assert (b->related_breakpoint->related_breakpoint == b);
1731 b->related_breakpoint->disposition = disp_del_at_next_stop;
1732 b->related_breakpoint->related_breakpoint = b->related_breakpoint;
1733 b->related_breakpoint = b;
1734 }
1735 b->disposition = disp_del_at_next_stop;
1736 }
1737
1738 /* Extract a bitfield value from value VAL using the bit parameters contained in
1739 watchpoint W. */
1740
1741 static struct value *
1742 extract_bitfield_from_watchpoint_value (struct watchpoint *w, struct value *val)
1743 {
1744 struct value *bit_val;
1745
1746 if (val == NULL)
1747 return NULL;
1748
1749 bit_val = allocate_value (value_type (val));
1750
1751 unpack_value_bitfield (bit_val,
1752 w->val_bitpos,
1753 w->val_bitsize,
1754 value_contents_for_printing (val),
1755 value_offset (val),
1756 val);
1757
1758 return bit_val;
1759 }
1760
1761 /* Assuming that B is a watchpoint:
1762 - Reparse watchpoint expression, if REPARSE is non-zero
1763 - Evaluate expression and store the result in B->val
1764 - Evaluate the condition if there is one, and store the result
1765 in b->loc->cond.
1766 - Update the list of values that must be watched in B->loc.
1767
1768 If the watchpoint disposition is disp_del_at_next_stop, then do
1769 nothing. If this is local watchpoint that is out of scope, delete
1770 it.
1771
1772 Even with `set breakpoint always-inserted on' the watchpoints are
1773 removed + inserted on each stop here. Normal breakpoints must
1774 never be removed because they might be missed by a running thread
1775 when debugging in non-stop mode. On the other hand, hardware
1776 watchpoints (is_hardware_watchpoint; processed here) are specific
1777 to each LWP since they are stored in each LWP's hardware debug
1778 registers. Therefore, such LWP must be stopped first in order to
1779 be able to modify its hardware watchpoints.
1780
1781 Hardware watchpoints must be reset exactly once after being
1782 presented to the user. It cannot be done sooner, because it would
1783 reset the data used to present the watchpoint hit to the user. And
1784 it must not be done later because it could display the same single
1785 watchpoint hit during multiple GDB stops. Note that the latter is
1786 relevant only to the hardware watchpoint types bp_read_watchpoint
1787 and bp_access_watchpoint. False hit by bp_hardware_watchpoint is
1788 not user-visible - its hit is suppressed if the memory content has
1789 not changed.
1790
1791 The following constraints influence the location where we can reset
1792 hardware watchpoints:
1793
1794 * target_stopped_by_watchpoint and target_stopped_data_address are
1795 called several times when GDB stops.
1796
1797 [linux]
1798 * Multiple hardware watchpoints can be hit at the same time,
1799 causing GDB to stop. GDB only presents one hardware watchpoint
1800 hit at a time as the reason for stopping, and all the other hits
1801 are presented later, one after the other, each time the user
1802 requests the execution to be resumed. Execution is not resumed
1803 for the threads still having pending hit event stored in
1804 LWP_INFO->STATUS. While the watchpoint is already removed from
1805 the inferior on the first stop the thread hit event is kept being
1806 reported from its cached value by linux_nat_stopped_data_address
1807 until the real thread resume happens after the watchpoint gets
1808 presented and thus its LWP_INFO->STATUS gets reset.
1809
1810 Therefore the hardware watchpoint hit can get safely reset on the
1811 watchpoint removal from inferior. */
1812
1813 static void
1814 update_watchpoint (struct watchpoint *b, int reparse)
1815 {
1816 int within_current_scope;
1817 struct frame_id saved_frame_id;
1818 int frame_saved;
1819
1820 /* If this is a local watchpoint, we only want to check if the
1821 watchpoint frame is in scope if the current thread is the thread
1822 that was used to create the watchpoint. */
1823 if (!watchpoint_in_thread_scope (b))
1824 return;
1825
1826 if (b->base.disposition == disp_del_at_next_stop)
1827 return;
1828
1829 frame_saved = 0;
1830
1831 /* Determine if the watchpoint is within scope. */
1832 if (b->exp_valid_block == NULL)
1833 within_current_scope = 1;
1834 else
1835 {
1836 struct frame_info *fi = get_current_frame ();
1837 struct gdbarch *frame_arch = get_frame_arch (fi);
1838 CORE_ADDR frame_pc = get_frame_pc (fi);
1839
1840 /* If we're at a point where the stack has been destroyed
1841 (e.g. in a function epilogue), unwinding may not work
1842 properly. Do not attempt to recreate locations at this
1843 point. See similar comments in watchpoint_check. */
1844 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
1845 return;
1846
1847 /* Save the current frame's ID so we can restore it after
1848 evaluating the watchpoint expression on its own frame. */
1849 /* FIXME drow/2003-09-09: It would be nice if evaluate_expression
1850 took a frame parameter, so that we didn't have to change the
1851 selected frame. */
1852 frame_saved = 1;
1853 saved_frame_id = get_frame_id (get_selected_frame (NULL));
1854
1855 fi = frame_find_by_id (b->watchpoint_frame);
1856 within_current_scope = (fi != NULL);
1857 if (within_current_scope)
1858 select_frame (fi);
1859 }
1860
1861 /* We don't free locations. They are stored in the bp_location array
1862 and update_global_location_list will eventually delete them and
1863 remove breakpoints if needed. */
1864 b->base.loc = NULL;
1865
1866 if (within_current_scope && reparse)
1867 {
1868 const char *s;
1869
1870 if (b->exp)
1871 {
1872 xfree (b->exp);
1873 b->exp = NULL;
1874 }
1875 s = b->exp_string_reparse ? b->exp_string_reparse : b->exp_string;
1876 b->exp = parse_exp_1 (&s, 0, b->exp_valid_block, 0);
1877 /* If the meaning of expression itself changed, the old value is
1878 no longer relevant. We don't want to report a watchpoint hit
1879 to the user when the old value and the new value may actually
1880 be completely different objects. */
1881 value_free (b->val);
1882 b->val = NULL;
1883 b->val_valid = 0;
1884
1885 /* Note that unlike with breakpoints, the watchpoint's condition
1886 expression is stored in the breakpoint object, not in the
1887 locations (re)created below. */
1888 if (b->base.cond_string != NULL)
1889 {
1890 if (b->cond_exp != NULL)
1891 {
1892 xfree (b->cond_exp);
1893 b->cond_exp = NULL;
1894 }
1895
1896 s = b->base.cond_string;
1897 b->cond_exp = parse_exp_1 (&s, 0, b->cond_exp_valid_block, 0);
1898 }
1899 }
1900
1901 /* If we failed to parse the expression, for example because
1902 it refers to a global variable in a not-yet-loaded shared library,
1903 don't try to insert watchpoint. We don't automatically delete
1904 such watchpoint, though, since failure to parse expression
1905 is different from out-of-scope watchpoint. */
1906 if (!target_has_execution)
1907 {
1908 /* Without execution, memory can't change. No use to try and
1909 set watchpoint locations. The watchpoint will be reset when
1910 the target gains execution, through breakpoint_re_set. */
1911 if (!can_use_hw_watchpoints)
1912 {
1913 if (b->base.ops->works_in_software_mode (&b->base))
1914 b->base.type = bp_watchpoint;
1915 else
1916 error (_("Can't set read/access watchpoint when "
1917 "hardware watchpoints are disabled."));
1918 }
1919 }
1920 else if (within_current_scope && b->exp)
1921 {
1922 int pc = 0;
1923 struct value *val_chain, *v, *result, *next;
1924 struct program_space *frame_pspace;
1925
1926 fetch_subexp_value (b->exp, &pc, &v, &result, &val_chain, 0);
1927
1928 /* Avoid setting b->val if it's already set. The meaning of
1929 b->val is 'the last value' user saw, and we should update
1930 it only if we reported that last value to user. As it
1931 happens, the code that reports it updates b->val directly.
1932 We don't keep track of the memory value for masked
1933 watchpoints. */
1934 if (!b->val_valid && !is_masked_watchpoint (&b->base))
1935 {
1936 if (b->val_bitsize != 0)
1937 {
1938 v = extract_bitfield_from_watchpoint_value (b, v);
1939 if (v != NULL)
1940 release_value (v);
1941 }
1942 b->val = v;
1943 b->val_valid = 1;
1944 }
1945
1946 frame_pspace = get_frame_program_space (get_selected_frame (NULL));
1947
1948 /* Look at each value on the value chain. */
1949 for (v = val_chain; v; v = value_next (v))
1950 {
1951 /* If it's a memory location, and GDB actually needed
1952 its contents to evaluate the expression, then we
1953 must watch it. If the first value returned is
1954 still lazy, that means an error occurred reading it;
1955 watch it anyway in case it becomes readable. */
1956 if (VALUE_LVAL (v) == lval_memory
1957 && (v == val_chain || ! value_lazy (v)))
1958 {
1959 struct type *vtype = check_typedef (value_type (v));
1960
1961 /* We only watch structs and arrays if user asked
1962 for it explicitly, never if they just happen to
1963 appear in the middle of some value chain. */
1964 if (v == result
1965 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
1966 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
1967 {
1968 CORE_ADDR addr;
1969 int type;
1970 struct bp_location *loc, **tmp;
1971 int bitpos = 0, bitsize = 0;
1972
1973 if (value_bitsize (v) != 0)
1974 {
1975 /* Extract the bit parameters out from the bitfield
1976 sub-expression. */
1977 bitpos = value_bitpos (v);
1978 bitsize = value_bitsize (v);
1979 }
1980 else if (v == result && b->val_bitsize != 0)
1981 {
1982 /* If VAL_BITSIZE != 0 then RESULT is actually a bitfield
1983 lvalue whose bit parameters are saved in the fields
1984 VAL_BITPOS and VAL_BITSIZE. */
1985 bitpos = b->val_bitpos;
1986 bitsize = b->val_bitsize;
1987 }
1988
1989 addr = value_address (v);
1990 if (bitsize != 0)
1991 {
1992 /* Skip the bytes that don't contain the bitfield. */
1993 addr += bitpos / 8;
1994 }
1995
1996 type = hw_write;
1997 if (b->base.type == bp_read_watchpoint)
1998 type = hw_read;
1999 else if (b->base.type == bp_access_watchpoint)
2000 type = hw_access;
2001
2002 loc = allocate_bp_location (&b->base);
2003 for (tmp = &(b->base.loc); *tmp != NULL; tmp = &((*tmp)->next))
2004 ;
2005 *tmp = loc;
2006 loc->gdbarch = get_type_arch (value_type (v));
2007
2008 loc->pspace = frame_pspace;
2009 loc->address = addr;
2010
2011 if (bitsize != 0)
2012 {
2013 /* Just cover the bytes that make up the bitfield. */
2014 loc->length = ((bitpos % 8) + bitsize + 7) / 8;
2015 }
2016 else
2017 loc->length = TYPE_LENGTH (value_type (v));
2018
2019 loc->watchpoint_type = type;
2020 }
2021 }
2022 }
2023
2024 /* Change the type of breakpoint between hardware assisted or
2025 an ordinary watchpoint depending on the hardware support
2026 and free hardware slots. REPARSE is set when the inferior
2027 is started. */
2028 if (reparse)
2029 {
2030 int reg_cnt;
2031 enum bp_loc_type loc_type;
2032 struct bp_location *bl;
2033
2034 reg_cnt = can_use_hardware_watchpoint (val_chain);
2035
2036 if (reg_cnt)
2037 {
2038 int i, target_resources_ok, other_type_used;
2039 enum bptype type;
2040
2041 /* Use an exact watchpoint when there's only one memory region to be
2042 watched, and only one debug register is needed to watch it. */
2043 b->exact = target_exact_watchpoints && reg_cnt == 1;
2044
2045 /* We need to determine how many resources are already
2046 used for all other hardware watchpoints plus this one
2047 to see if we still have enough resources to also fit
2048 this watchpoint in as well. */
2049
2050 /* If this is a software watchpoint, we try to turn it
2051 to a hardware one -- count resources as if B was of
2052 hardware watchpoint type. */
2053 type = b->base.type;
2054 if (type == bp_watchpoint)
2055 type = bp_hardware_watchpoint;
2056
2057 /* This watchpoint may or may not have been placed on
2058 the list yet at this point (it won't be in the list
2059 if we're trying to create it for the first time,
2060 through watch_command), so always account for it
2061 manually. */
2062
2063 /* Count resources used by all watchpoints except B. */
2064 i = hw_watchpoint_used_count_others (&b->base, type, &other_type_used);
2065
2066 /* Add in the resources needed for B. */
2067 i += hw_watchpoint_use_count (&b->base);
2068
2069 target_resources_ok
2070 = target_can_use_hardware_watchpoint (type, i, other_type_used);
2071 if (target_resources_ok <= 0)
2072 {
2073 int sw_mode = b->base.ops->works_in_software_mode (&b->base);
2074
2075 if (target_resources_ok == 0 && !sw_mode)
2076 error (_("Target does not support this type of "
2077 "hardware watchpoint."));
2078 else if (target_resources_ok < 0 && !sw_mode)
2079 error (_("There are not enough available hardware "
2080 "resources for this watchpoint."));
2081
2082 /* Downgrade to software watchpoint. */
2083 b->base.type = bp_watchpoint;
2084 }
2085 else
2086 {
2087 /* If this was a software watchpoint, we've just
2088 found we have enough resources to turn it to a
2089 hardware watchpoint. Otherwise, this is a
2090 nop. */
2091 b->base.type = type;
2092 }
2093 }
2094 else if (!b->base.ops->works_in_software_mode (&b->base))
2095 {
2096 if (!can_use_hw_watchpoints)
2097 error (_("Can't set read/access watchpoint when "
2098 "hardware watchpoints are disabled."));
2099 else
2100 error (_("Expression cannot be implemented with "
2101 "read/access watchpoint."));
2102 }
2103 else
2104 b->base.type = bp_watchpoint;
2105
2106 loc_type = (b->base.type == bp_watchpoint? bp_loc_other
2107 : bp_loc_hardware_watchpoint);
2108 for (bl = b->base.loc; bl; bl = bl->next)
2109 bl->loc_type = loc_type;
2110 }
2111
2112 for (v = val_chain; v; v = next)
2113 {
2114 next = value_next (v);
2115 if (v != b->val)
2116 value_free (v);
2117 }
2118
2119 /* If a software watchpoint is not watching any memory, then the
2120 above left it without any location set up. But,
2121 bpstat_stop_status requires a location to be able to report
2122 stops, so make sure there's at least a dummy one. */
2123 if (b->base.type == bp_watchpoint && b->base.loc == NULL)
2124 {
2125 struct breakpoint *base = &b->base;
2126 base->loc = allocate_bp_location (base);
2127 base->loc->pspace = frame_pspace;
2128 base->loc->address = -1;
2129 base->loc->length = -1;
2130 base->loc->watchpoint_type = -1;
2131 }
2132 }
2133 else if (!within_current_scope)
2134 {
2135 printf_filtered (_("\
2136 Watchpoint %d deleted because the program has left the block\n\
2137 in which its expression is valid.\n"),
2138 b->base.number);
2139 watchpoint_del_at_next_stop (b);
2140 }
2141
2142 /* Restore the selected frame. */
2143 if (frame_saved)
2144 select_frame (frame_find_by_id (saved_frame_id));
2145 }
2146
2147
2148 /* Returns 1 iff breakpoint location should be
2149 inserted in the inferior. We don't differentiate the type of BL's owner
2150 (breakpoint vs. tracepoint), although insert_location in tracepoint's
2151 breakpoint_ops is not defined, because in insert_bp_location,
2152 tracepoint's insert_location will not be called. */
2153 static int
2154 should_be_inserted (struct bp_location *bl)
2155 {
2156 if (bl->owner == NULL || !breakpoint_enabled (bl->owner))
2157 return 0;
2158
2159 if (bl->owner->disposition == disp_del_at_next_stop)
2160 return 0;
2161
2162 if (!bl->enabled || bl->shlib_disabled || bl->duplicate)
2163 return 0;
2164
2165 if (user_breakpoint_p (bl->owner) && bl->pspace->executing_startup)
2166 return 0;
2167
2168 /* This is set for example, when we're attached to the parent of a
2169 vfork, and have detached from the child. The child is running
2170 free, and we expect it to do an exec or exit, at which point the
2171 OS makes the parent schedulable again (and the target reports
2172 that the vfork is done). Until the child is done with the shared
2173 memory region, do not insert breakpoints in the parent, otherwise
2174 the child could still trip on the parent's breakpoints. Since
2175 the parent is blocked anyway, it won't miss any breakpoint. */
2176 if (bl->pspace->breakpoints_not_allowed)
2177 return 0;
2178
2179 /* Don't insert a breakpoint if we're trying to step past its
2180 location. */
2181 if ((bl->loc_type == bp_loc_software_breakpoint
2182 || bl->loc_type == bp_loc_hardware_breakpoint)
2183 && stepping_past_instruction_at (bl->pspace->aspace,
2184 bl->address))
2185 {
2186 if (debug_infrun)
2187 {
2188 fprintf_unfiltered (gdb_stdlog,
2189 "infrun: skipping breakpoint: "
2190 "stepping past insn at: %s\n",
2191 paddress (bl->gdbarch, bl->address));
2192 }
2193 return 0;
2194 }
2195
2196 /* Don't insert watchpoints if we're trying to step past the
2197 instruction that triggered one. */
2198 if ((bl->loc_type == bp_loc_hardware_watchpoint)
2199 && stepping_past_nonsteppable_watchpoint ())
2200 {
2201 if (debug_infrun)
2202 {
2203 fprintf_unfiltered (gdb_stdlog,
2204 "infrun: stepping past non-steppable watchpoint. "
2205 "skipping watchpoint at %s:%d\n",
2206 paddress (bl->gdbarch, bl->address),
2207 bl->length);
2208 }
2209 return 0;
2210 }
2211
2212 return 1;
2213 }
2214
2215 /* Same as should_be_inserted but does the check assuming
2216 that the location is not duplicated. */
2217
2218 static int
2219 unduplicated_should_be_inserted (struct bp_location *bl)
2220 {
2221 int result;
2222 const int save_duplicate = bl->duplicate;
2223
2224 bl->duplicate = 0;
2225 result = should_be_inserted (bl);
2226 bl->duplicate = save_duplicate;
2227 return result;
2228 }
2229
2230 /* Parses a conditional described by an expression COND into an
2231 agent expression bytecode suitable for evaluation
2232 by the bytecode interpreter. Return NULL if there was
2233 any error during parsing. */
2234
2235 static struct agent_expr *
2236 parse_cond_to_aexpr (CORE_ADDR scope, struct expression *cond)
2237 {
2238 struct agent_expr *aexpr = NULL;
2239
2240 if (!cond)
2241 return NULL;
2242
2243 /* We don't want to stop processing, so catch any errors
2244 that may show up. */
2245 TRY
2246 {
2247 aexpr = gen_eval_for_expr (scope, cond);
2248 }
2249
2250 CATCH (ex, RETURN_MASK_ERROR)
2251 {
2252 /* If we got here, it means the condition could not be parsed to a valid
2253 bytecode expression and thus can't be evaluated on the target's side.
2254 It's no use iterating through the conditions. */
2255 return NULL;
2256 }
2257 END_CATCH
2258
2259 /* We have a valid agent expression. */
2260 return aexpr;
2261 }
2262
2263 /* Based on location BL, create a list of breakpoint conditions to be
2264 passed on to the target. If we have duplicated locations with different
2265 conditions, we will add such conditions to the list. The idea is that the
2266 target will evaluate the list of conditions and will only notify GDB when
2267 one of them is true. */
2268
2269 static void
2270 build_target_condition_list (struct bp_location *bl)
2271 {
2272 struct bp_location **locp = NULL, **loc2p;
2273 int null_condition_or_parse_error = 0;
2274 int modified = bl->needs_update;
2275 struct bp_location *loc;
2276
2277 /* Release conditions left over from a previous insert. */
2278 VEC_free (agent_expr_p, bl->target_info.conditions);
2279
2280 /* This is only meaningful if the target is
2281 evaluating conditions and if the user has
2282 opted for condition evaluation on the target's
2283 side. */
2284 if (gdb_evaluates_breakpoint_condition_p ()
2285 || !target_supports_evaluation_of_breakpoint_conditions ())
2286 return;
2287
2288 /* Do a first pass to check for locations with no assigned
2289 conditions or conditions that fail to parse to a valid agent expression
2290 bytecode. If any of these happen, then it's no use to send conditions
2291 to the target since this location will always trigger and generate a
2292 response back to GDB. */
2293 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2294 {
2295 loc = (*loc2p);
2296 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2297 {
2298 if (modified)
2299 {
2300 struct agent_expr *aexpr;
2301
2302 /* Re-parse the conditions since something changed. In that
2303 case we already freed the condition bytecodes (see
2304 force_breakpoint_reinsertion). We just
2305 need to parse the condition to bytecodes again. */
2306 aexpr = parse_cond_to_aexpr (bl->address, loc->cond);
2307 loc->cond_bytecode = aexpr;
2308
2309 /* Check if we managed to parse the conditional expression
2310 correctly. If not, we will not send this condition
2311 to the target. */
2312 if (aexpr)
2313 continue;
2314 }
2315
2316 /* If we have a NULL bytecode expression, it means something
2317 went wrong or we have a null condition expression. */
2318 if (!loc->cond_bytecode)
2319 {
2320 null_condition_or_parse_error = 1;
2321 break;
2322 }
2323 }
2324 }
2325
2326 /* If any of these happened, it means we will have to evaluate the conditions
2327 for the location's address on gdb's side. It is no use keeping bytecodes
2328 for all the other duplicate locations, thus we free all of them here.
2329
2330 This is so we have a finer control over which locations' conditions are
2331 being evaluated by GDB or the remote stub. */
2332 if (null_condition_or_parse_error)
2333 {
2334 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2335 {
2336 loc = (*loc2p);
2337 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2338 {
2339 /* Only go as far as the first NULL bytecode is
2340 located. */
2341 if (!loc->cond_bytecode)
2342 return;
2343
2344 free_agent_expr (loc->cond_bytecode);
2345 loc->cond_bytecode = NULL;
2346 }
2347 }
2348 }
2349
2350 /* No NULL conditions or failed bytecode generation. Build a condition list
2351 for this location's address. */
2352 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2353 {
2354 loc = (*loc2p);
2355 if (loc->cond
2356 && is_breakpoint (loc->owner)
2357 && loc->pspace->num == bl->pspace->num
2358 && loc->owner->enable_state == bp_enabled
2359 && loc->enabled)
2360 /* Add the condition to the vector. This will be used later to send the
2361 conditions to the target. */
2362 VEC_safe_push (agent_expr_p, bl->target_info.conditions,
2363 loc->cond_bytecode);
2364 }
2365
2366 return;
2367 }
2368
2369 /* Parses a command described by string CMD into an agent expression
2370 bytecode suitable for evaluation by the bytecode interpreter.
2371 Return NULL if there was any error during parsing. */
2372
2373 static struct agent_expr *
2374 parse_cmd_to_aexpr (CORE_ADDR scope, char *cmd)
2375 {
2376 struct cleanup *old_cleanups = 0;
2377 struct expression *expr, **argvec;
2378 struct agent_expr *aexpr = NULL;
2379 const char *cmdrest;
2380 const char *format_start, *format_end;
2381 struct format_piece *fpieces;
2382 int nargs;
2383 struct gdbarch *gdbarch = get_current_arch ();
2384
2385 if (!cmd)
2386 return NULL;
2387
2388 cmdrest = cmd;
2389
2390 if (*cmdrest == ',')
2391 ++cmdrest;
2392 cmdrest = skip_spaces_const (cmdrest);
2393
2394 if (*cmdrest++ != '"')
2395 error (_("No format string following the location"));
2396
2397 format_start = cmdrest;
2398
2399 fpieces = parse_format_string (&cmdrest);
2400
2401 old_cleanups = make_cleanup (free_format_pieces_cleanup, &fpieces);
2402
2403 format_end = cmdrest;
2404
2405 if (*cmdrest++ != '"')
2406 error (_("Bad format string, non-terminated '\"'."));
2407
2408 cmdrest = skip_spaces_const (cmdrest);
2409
2410 if (!(*cmdrest == ',' || *cmdrest == '\0'))
2411 error (_("Invalid argument syntax"));
2412
2413 if (*cmdrest == ',')
2414 cmdrest++;
2415 cmdrest = skip_spaces_const (cmdrest);
2416
2417 /* For each argument, make an expression. */
2418
2419 argvec = (struct expression **) alloca (strlen (cmd)
2420 * sizeof (struct expression *));
2421
2422 nargs = 0;
2423 while (*cmdrest != '\0')
2424 {
2425 const char *cmd1;
2426
2427 cmd1 = cmdrest;
2428 expr = parse_exp_1 (&cmd1, scope, block_for_pc (scope), 1);
2429 argvec[nargs++] = expr;
2430 cmdrest = cmd1;
2431 if (*cmdrest == ',')
2432 ++cmdrest;
2433 }
2434
2435 /* We don't want to stop processing, so catch any errors
2436 that may show up. */
2437 TRY
2438 {
2439 aexpr = gen_printf (scope, gdbarch, 0, 0,
2440 format_start, format_end - format_start,
2441 fpieces, nargs, argvec);
2442 }
2443 CATCH (ex, RETURN_MASK_ERROR)
2444 {
2445 /* If we got here, it means the command could not be parsed to a valid
2446 bytecode expression and thus can't be evaluated on the target's side.
2447 It's no use iterating through the other commands. */
2448 aexpr = NULL;
2449 }
2450 END_CATCH
2451
2452 do_cleanups (old_cleanups);
2453
2454 /* We have a valid agent expression, return it. */
2455 return aexpr;
2456 }
2457
2458 /* Based on location BL, create a list of breakpoint commands to be
2459 passed on to the target. If we have duplicated locations with
2460 different commands, we will add any such to the list. */
2461
2462 static void
2463 build_target_command_list (struct bp_location *bl)
2464 {
2465 struct bp_location **locp = NULL, **loc2p;
2466 int null_command_or_parse_error = 0;
2467 int modified = bl->needs_update;
2468 struct bp_location *loc;
2469
2470 /* Release commands left over from a previous insert. */
2471 VEC_free (agent_expr_p, bl->target_info.tcommands);
2472
2473 if (!target_can_run_breakpoint_commands ())
2474 return;
2475
2476 /* For now, limit to agent-style dprintf breakpoints. */
2477 if (dprintf_style != dprintf_style_agent)
2478 return;
2479
2480 /* For now, if we have any duplicate location that isn't a dprintf,
2481 don't install the target-side commands, as that would make the
2482 breakpoint not be reported to the core, and we'd lose
2483 control. */
2484 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2485 {
2486 loc = (*loc2p);
2487 if (is_breakpoint (loc->owner)
2488 && loc->pspace->num == bl->pspace->num
2489 && loc->owner->type != bp_dprintf)
2490 return;
2491 }
2492
2493 /* Do a first pass to check for locations with no assigned
2494 conditions or conditions that fail to parse to a valid agent expression
2495 bytecode. If any of these happen, then it's no use to send conditions
2496 to the target since this location will always trigger and generate a
2497 response back to GDB. */
2498 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2499 {
2500 loc = (*loc2p);
2501 if (is_breakpoint (loc->owner) && loc->pspace->num == bl->pspace->num)
2502 {
2503 if (modified)
2504 {
2505 struct agent_expr *aexpr;
2506
2507 /* Re-parse the commands since something changed. In that
2508 case we already freed the command bytecodes (see
2509 force_breakpoint_reinsertion). We just
2510 need to parse the command to bytecodes again. */
2511 aexpr = parse_cmd_to_aexpr (bl->address,
2512 loc->owner->extra_string);
2513 loc->cmd_bytecode = aexpr;
2514
2515 if (!aexpr)
2516 continue;
2517 }
2518
2519 /* If we have a NULL bytecode expression, it means something
2520 went wrong or we have a null command expression. */
2521 if (!loc->cmd_bytecode)
2522 {
2523 null_command_or_parse_error = 1;
2524 break;
2525 }
2526 }
2527 }
2528
2529 /* If anything failed, then we're not doing target-side commands,
2530 and so clean up. */
2531 if (null_command_or_parse_error)
2532 {
2533 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2534 {
2535 loc = (*loc2p);
2536 if (is_breakpoint (loc->owner)
2537 && loc->pspace->num == bl->pspace->num)
2538 {
2539 /* Only go as far as the first NULL bytecode is
2540 located. */
2541 if (loc->cmd_bytecode == NULL)
2542 return;
2543
2544 free_agent_expr (loc->cmd_bytecode);
2545 loc->cmd_bytecode = NULL;
2546 }
2547 }
2548 }
2549
2550 /* No NULL commands or failed bytecode generation. Build a command list
2551 for this location's address. */
2552 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, bl->address)
2553 {
2554 loc = (*loc2p);
2555 if (loc->owner->extra_string
2556 && is_breakpoint (loc->owner)
2557 && loc->pspace->num == bl->pspace->num
2558 && loc->owner->enable_state == bp_enabled
2559 && loc->enabled)
2560 /* Add the command to the vector. This will be used later
2561 to send the commands to the target. */
2562 VEC_safe_push (agent_expr_p, bl->target_info.tcommands,
2563 loc->cmd_bytecode);
2564 }
2565
2566 bl->target_info.persist = 0;
2567 /* Maybe flag this location as persistent. */
2568 if (bl->owner->type == bp_dprintf && disconnected_dprintf)
2569 bl->target_info.persist = 1;
2570 }
2571
2572 /* Insert a low-level "breakpoint" of some type. BL is the breakpoint
2573 location. Any error messages are printed to TMP_ERROR_STREAM; and
2574 DISABLED_BREAKS, and HW_BREAKPOINT_ERROR are used to report problems.
2575 Returns 0 for success, 1 if the bp_location type is not supported or
2576 -1 for failure.
2577
2578 NOTE drow/2003-09-09: This routine could be broken down to an
2579 object-style method for each breakpoint or catchpoint type. */
2580 static int
2581 insert_bp_location (struct bp_location *bl,
2582 struct ui_file *tmp_error_stream,
2583 int *disabled_breaks,
2584 int *hw_breakpoint_error,
2585 int *hw_bp_error_explained_already)
2586 {
2587 enum errors bp_err = GDB_NO_ERROR;
2588 const char *bp_err_message = NULL;
2589
2590 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
2591 return 0;
2592
2593 /* Note we don't initialize bl->target_info, as that wipes out
2594 the breakpoint location's shadow_contents if the breakpoint
2595 is still inserted at that location. This in turn breaks
2596 target_read_memory which depends on these buffers when
2597 a memory read is requested at the breakpoint location:
2598 Once the target_info has been wiped, we fail to see that
2599 we have a breakpoint inserted at that address and thus
2600 read the breakpoint instead of returning the data saved in
2601 the breakpoint location's shadow contents. */
2602 bl->target_info.reqstd_address = bl->address;
2603 bl->target_info.placed_address_space = bl->pspace->aspace;
2604 bl->target_info.length = bl->length;
2605
2606 /* When working with target-side conditions, we must pass all the conditions
2607 for the same breakpoint address down to the target since GDB will not
2608 insert those locations. With a list of breakpoint conditions, the target
2609 can decide when to stop and notify GDB. */
2610
2611 if (is_breakpoint (bl->owner))
2612 {
2613 build_target_condition_list (bl);
2614 build_target_command_list (bl);
2615 /* Reset the modification marker. */
2616 bl->needs_update = 0;
2617 }
2618
2619 if (bl->loc_type == bp_loc_software_breakpoint
2620 || bl->loc_type == bp_loc_hardware_breakpoint)
2621 {
2622 if (bl->owner->type != bp_hardware_breakpoint)
2623 {
2624 /* If the explicitly specified breakpoint type
2625 is not hardware breakpoint, check the memory map to see
2626 if the breakpoint address is in read only memory or not.
2627
2628 Two important cases are:
2629 - location type is not hardware breakpoint, memory
2630 is readonly. We change the type of the location to
2631 hardware breakpoint.
2632 - location type is hardware breakpoint, memory is
2633 read-write. This means we've previously made the
2634 location hardware one, but then the memory map changed,
2635 so we undo.
2636
2637 When breakpoints are removed, remove_breakpoints will use
2638 location types we've just set here, the only possible
2639 problem is that memory map has changed during running
2640 program, but it's not going to work anyway with current
2641 gdb. */
2642 struct mem_region *mr
2643 = lookup_mem_region (bl->target_info.reqstd_address);
2644
2645 if (mr)
2646 {
2647 if (automatic_hardware_breakpoints)
2648 {
2649 enum bp_loc_type new_type;
2650
2651 if (mr->attrib.mode != MEM_RW)
2652 new_type = bp_loc_hardware_breakpoint;
2653 else
2654 new_type = bp_loc_software_breakpoint;
2655
2656 if (new_type != bl->loc_type)
2657 {
2658 static int said = 0;
2659
2660 bl->loc_type = new_type;
2661 if (!said)
2662 {
2663 fprintf_filtered (gdb_stdout,
2664 _("Note: automatically using "
2665 "hardware breakpoints for "
2666 "read-only addresses.\n"));
2667 said = 1;
2668 }
2669 }
2670 }
2671 else if (bl->loc_type == bp_loc_software_breakpoint
2672 && mr->attrib.mode != MEM_RW)
2673 {
2674 fprintf_unfiltered (tmp_error_stream,
2675 _("Cannot insert breakpoint %d.\n"
2676 "Cannot set software breakpoint "
2677 "at read-only address %s\n"),
2678 bl->owner->number,
2679 paddress (bl->gdbarch, bl->address));
2680 return 1;
2681 }
2682 }
2683 }
2684
2685 /* First check to see if we have to handle an overlay. */
2686 if (overlay_debugging == ovly_off
2687 || bl->section == NULL
2688 || !(section_is_overlay (bl->section)))
2689 {
2690 /* No overlay handling: just set the breakpoint. */
2691 TRY
2692 {
2693 int val;
2694
2695 val = bl->owner->ops->insert_location (bl);
2696 if (val)
2697 bp_err = GENERIC_ERROR;
2698 }
2699 CATCH (e, RETURN_MASK_ALL)
2700 {
2701 bp_err = e.error;
2702 bp_err_message = e.message;
2703 }
2704 END_CATCH
2705 }
2706 else
2707 {
2708 /* This breakpoint is in an overlay section.
2709 Shall we set a breakpoint at the LMA? */
2710 if (!overlay_events_enabled)
2711 {
2712 /* Yes -- overlay event support is not active,
2713 so we must try to set a breakpoint at the LMA.
2714 This will not work for a hardware breakpoint. */
2715 if (bl->loc_type == bp_loc_hardware_breakpoint)
2716 warning (_("hardware breakpoint %d not supported in overlay!"),
2717 bl->owner->number);
2718 else
2719 {
2720 CORE_ADDR addr = overlay_unmapped_address (bl->address,
2721 bl->section);
2722 /* Set a software (trap) breakpoint at the LMA. */
2723 bl->overlay_target_info = bl->target_info;
2724 bl->overlay_target_info.reqstd_address = addr;
2725
2726 /* No overlay handling: just set the breakpoint. */
2727 TRY
2728 {
2729 int val;
2730
2731 val = target_insert_breakpoint (bl->gdbarch,
2732 &bl->overlay_target_info);
2733 if (val)
2734 bp_err = GENERIC_ERROR;
2735 }
2736 CATCH (e, RETURN_MASK_ALL)
2737 {
2738 bp_err = e.error;
2739 bp_err_message = e.message;
2740 }
2741 END_CATCH
2742
2743 if (bp_err != GDB_NO_ERROR)
2744 fprintf_unfiltered (tmp_error_stream,
2745 "Overlay breakpoint %d "
2746 "failed: in ROM?\n",
2747 bl->owner->number);
2748 }
2749 }
2750 /* Shall we set a breakpoint at the VMA? */
2751 if (section_is_mapped (bl->section))
2752 {
2753 /* Yes. This overlay section is mapped into memory. */
2754 TRY
2755 {
2756 int val;
2757
2758 val = bl->owner->ops->insert_location (bl);
2759 if (val)
2760 bp_err = GENERIC_ERROR;
2761 }
2762 CATCH (e, RETURN_MASK_ALL)
2763 {
2764 bp_err = e.error;
2765 bp_err_message = e.message;
2766 }
2767 END_CATCH
2768 }
2769 else
2770 {
2771 /* No. This breakpoint will not be inserted.
2772 No error, but do not mark the bp as 'inserted'. */
2773 return 0;
2774 }
2775 }
2776
2777 if (bp_err != GDB_NO_ERROR)
2778 {
2779 /* Can't set the breakpoint. */
2780
2781 /* In some cases, we might not be able to insert a
2782 breakpoint in a shared library that has already been
2783 removed, but we have not yet processed the shlib unload
2784 event. Unfortunately, some targets that implement
2785 breakpoint insertion themselves can't tell why the
2786 breakpoint insertion failed (e.g., the remote target
2787 doesn't define error codes), so we must treat generic
2788 errors as memory errors. */
2789 if ((bp_err == GENERIC_ERROR || bp_err == MEMORY_ERROR)
2790 && bl->loc_type == bp_loc_software_breakpoint
2791 && (solib_name_from_address (bl->pspace, bl->address)
2792 || shared_objfile_contains_address_p (bl->pspace,
2793 bl->address)))
2794 {
2795 /* See also: disable_breakpoints_in_shlibs. */
2796 bl->shlib_disabled = 1;
2797 observer_notify_breakpoint_modified (bl->owner);
2798 if (!*disabled_breaks)
2799 {
2800 fprintf_unfiltered (tmp_error_stream,
2801 "Cannot insert breakpoint %d.\n",
2802 bl->owner->number);
2803 fprintf_unfiltered (tmp_error_stream,
2804 "Temporarily disabling shared "
2805 "library breakpoints:\n");
2806 }
2807 *disabled_breaks = 1;
2808 fprintf_unfiltered (tmp_error_stream,
2809 "breakpoint #%d\n", bl->owner->number);
2810 return 0;
2811 }
2812 else
2813 {
2814 if (bl->loc_type == bp_loc_hardware_breakpoint)
2815 {
2816 *hw_breakpoint_error = 1;
2817 *hw_bp_error_explained_already = bp_err_message != NULL;
2818 fprintf_unfiltered (tmp_error_stream,
2819 "Cannot insert hardware breakpoint %d%s",
2820 bl->owner->number, bp_err_message ? ":" : ".\n");
2821 if (bp_err_message != NULL)
2822 fprintf_unfiltered (tmp_error_stream, "%s.\n", bp_err_message);
2823 }
2824 else
2825 {
2826 if (bp_err_message == NULL)
2827 {
2828 char *message
2829 = memory_error_message (TARGET_XFER_E_IO,
2830 bl->gdbarch, bl->address);
2831 struct cleanup *old_chain = make_cleanup (xfree, message);
2832
2833 fprintf_unfiltered (tmp_error_stream,
2834 "Cannot insert breakpoint %d.\n"
2835 "%s\n",
2836 bl->owner->number, message);
2837 do_cleanups (old_chain);
2838 }
2839 else
2840 {
2841 fprintf_unfiltered (tmp_error_stream,
2842 "Cannot insert breakpoint %d: %s\n",
2843 bl->owner->number,
2844 bp_err_message);
2845 }
2846 }
2847 return 1;
2848
2849 }
2850 }
2851 else
2852 bl->inserted = 1;
2853
2854 return 0;
2855 }
2856
2857 else if (bl->loc_type == bp_loc_hardware_watchpoint
2858 /* NOTE drow/2003-09-08: This state only exists for removing
2859 watchpoints. It's not clear that it's necessary... */
2860 && bl->owner->disposition != disp_del_at_next_stop)
2861 {
2862 int val;
2863
2864 gdb_assert (bl->owner->ops != NULL
2865 && bl->owner->ops->insert_location != NULL);
2866
2867 val = bl->owner->ops->insert_location (bl);
2868
2869 /* If trying to set a read-watchpoint, and it turns out it's not
2870 supported, try emulating one with an access watchpoint. */
2871 if (val == 1 && bl->watchpoint_type == hw_read)
2872 {
2873 struct bp_location *loc, **loc_temp;
2874
2875 /* But don't try to insert it, if there's already another
2876 hw_access location that would be considered a duplicate
2877 of this one. */
2878 ALL_BP_LOCATIONS (loc, loc_temp)
2879 if (loc != bl
2880 && loc->watchpoint_type == hw_access
2881 && watchpoint_locations_match (bl, loc))
2882 {
2883 bl->duplicate = 1;
2884 bl->inserted = 1;
2885 bl->target_info = loc->target_info;
2886 bl->watchpoint_type = hw_access;
2887 val = 0;
2888 break;
2889 }
2890
2891 if (val == 1)
2892 {
2893 bl->watchpoint_type = hw_access;
2894 val = bl->owner->ops->insert_location (bl);
2895
2896 if (val)
2897 /* Back to the original value. */
2898 bl->watchpoint_type = hw_read;
2899 }
2900 }
2901
2902 bl->inserted = (val == 0);
2903 }
2904
2905 else if (bl->owner->type == bp_catchpoint)
2906 {
2907 int val;
2908
2909 gdb_assert (bl->owner->ops != NULL
2910 && bl->owner->ops->insert_location != NULL);
2911
2912 val = bl->owner->ops->insert_location (bl);
2913 if (val)
2914 {
2915 bl->owner->enable_state = bp_disabled;
2916
2917 if (val == 1)
2918 warning (_("\
2919 Error inserting catchpoint %d: Your system does not support this type\n\
2920 of catchpoint."), bl->owner->number);
2921 else
2922 warning (_("Error inserting catchpoint %d."), bl->owner->number);
2923 }
2924
2925 bl->inserted = (val == 0);
2926
2927 /* We've already printed an error message if there was a problem
2928 inserting this catchpoint, and we've disabled the catchpoint,
2929 so just return success. */
2930 return 0;
2931 }
2932
2933 return 0;
2934 }
2935
2936 /* This function is called when program space PSPACE is about to be
2937 deleted. It takes care of updating breakpoints to not reference
2938 PSPACE anymore. */
2939
2940 void
2941 breakpoint_program_space_exit (struct program_space *pspace)
2942 {
2943 struct breakpoint *b, *b_temp;
2944 struct bp_location *loc, **loc_temp;
2945
2946 /* Remove any breakpoint that was set through this program space. */
2947 ALL_BREAKPOINTS_SAFE (b, b_temp)
2948 {
2949 if (b->pspace == pspace)
2950 delete_breakpoint (b);
2951 }
2952
2953 /* Breakpoints set through other program spaces could have locations
2954 bound to PSPACE as well. Remove those. */
2955 ALL_BP_LOCATIONS (loc, loc_temp)
2956 {
2957 struct bp_location *tmp;
2958
2959 if (loc->pspace == pspace)
2960 {
2961 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
2962 if (loc->owner->loc == loc)
2963 loc->owner->loc = loc->next;
2964 else
2965 for (tmp = loc->owner->loc; tmp->next != NULL; tmp = tmp->next)
2966 if (tmp->next == loc)
2967 {
2968 tmp->next = loc->next;
2969 break;
2970 }
2971 }
2972 }
2973
2974 /* Now update the global location list to permanently delete the
2975 removed locations above. */
2976 update_global_location_list (UGLL_DONT_INSERT);
2977 }
2978
2979 /* Make sure all breakpoints are inserted in inferior.
2980 Throws exception on any error.
2981 A breakpoint that is already inserted won't be inserted
2982 again, so calling this function twice is safe. */
2983 void
2984 insert_breakpoints (void)
2985 {
2986 struct breakpoint *bpt;
2987
2988 ALL_BREAKPOINTS (bpt)
2989 if (is_hardware_watchpoint (bpt))
2990 {
2991 struct watchpoint *w = (struct watchpoint *) bpt;
2992
2993 update_watchpoint (w, 0 /* don't reparse. */);
2994 }
2995
2996 /* Updating watchpoints creates new locations, so update the global
2997 location list. Explicitly tell ugll to insert locations and
2998 ignore breakpoints_always_inserted_mode. */
2999 update_global_location_list (UGLL_INSERT);
3000 }
3001
3002 /* Invoke CALLBACK for each of bp_location. */
3003
3004 void
3005 iterate_over_bp_locations (walk_bp_location_callback callback)
3006 {
3007 struct bp_location *loc, **loc_tmp;
3008
3009 ALL_BP_LOCATIONS (loc, loc_tmp)
3010 {
3011 callback (loc, NULL);
3012 }
3013 }
3014
3015 /* This is used when we need to synch breakpoint conditions between GDB and the
3016 target. It is the case with deleting and disabling of breakpoints when using
3017 always-inserted mode. */
3018
3019 static void
3020 update_inserted_breakpoint_locations (void)
3021 {
3022 struct bp_location *bl, **blp_tmp;
3023 int error_flag = 0;
3024 int val = 0;
3025 int disabled_breaks = 0;
3026 int hw_breakpoint_error = 0;
3027 int hw_bp_details_reported = 0;
3028
3029 struct ui_file *tmp_error_stream = mem_fileopen ();
3030 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
3031
3032 /* Explicitly mark the warning -- this will only be printed if
3033 there was an error. */
3034 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
3035
3036 save_current_space_and_thread ();
3037
3038 ALL_BP_LOCATIONS (bl, blp_tmp)
3039 {
3040 /* We only want to update software breakpoints and hardware
3041 breakpoints. */
3042 if (!is_breakpoint (bl->owner))
3043 continue;
3044
3045 /* We only want to update locations that are already inserted
3046 and need updating. This is to avoid unwanted insertion during
3047 deletion of breakpoints. */
3048 if (!bl->inserted || (bl->inserted && !bl->needs_update))
3049 continue;
3050
3051 switch_to_program_space_and_thread (bl->pspace);
3052
3053 /* For targets that support global breakpoints, there's no need
3054 to select an inferior to insert breakpoint to. In fact, even
3055 if we aren't attached to any process yet, we should still
3056 insert breakpoints. */
3057 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3058 && ptid_equal (inferior_ptid, null_ptid))
3059 continue;
3060
3061 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
3062 &hw_breakpoint_error, &hw_bp_details_reported);
3063 if (val)
3064 error_flag = val;
3065 }
3066
3067 if (error_flag)
3068 {
3069 target_terminal_ours_for_output ();
3070 error_stream (tmp_error_stream);
3071 }
3072
3073 do_cleanups (cleanups);
3074 }
3075
3076 /* Used when starting or continuing the program. */
3077
3078 static void
3079 insert_breakpoint_locations (void)
3080 {
3081 struct breakpoint *bpt;
3082 struct bp_location *bl, **blp_tmp;
3083 int error_flag = 0;
3084 int val = 0;
3085 int disabled_breaks = 0;
3086 int hw_breakpoint_error = 0;
3087 int hw_bp_error_explained_already = 0;
3088
3089 struct ui_file *tmp_error_stream = mem_fileopen ();
3090 struct cleanup *cleanups = make_cleanup_ui_file_delete (tmp_error_stream);
3091
3092 /* Explicitly mark the warning -- this will only be printed if
3093 there was an error. */
3094 fprintf_unfiltered (tmp_error_stream, "Warning:\n");
3095
3096 save_current_space_and_thread ();
3097
3098 ALL_BP_LOCATIONS (bl, blp_tmp)
3099 {
3100 if (!should_be_inserted (bl) || (bl->inserted && !bl->needs_update))
3101 continue;
3102
3103 /* There is no point inserting thread-specific breakpoints if
3104 the thread no longer exists. ALL_BP_LOCATIONS bp_location
3105 has BL->OWNER always non-NULL. */
3106 if (bl->owner->thread != -1
3107 && !valid_thread_id (bl->owner->thread))
3108 continue;
3109
3110 switch_to_program_space_and_thread (bl->pspace);
3111
3112 /* For targets that support global breakpoints, there's no need
3113 to select an inferior to insert breakpoint to. In fact, even
3114 if we aren't attached to any process yet, we should still
3115 insert breakpoints. */
3116 if (!gdbarch_has_global_breakpoints (target_gdbarch ())
3117 && ptid_equal (inferior_ptid, null_ptid))
3118 continue;
3119
3120 val = insert_bp_location (bl, tmp_error_stream, &disabled_breaks,
3121 &hw_breakpoint_error, &hw_bp_error_explained_already);
3122 if (val)
3123 error_flag = val;
3124 }
3125
3126 /* If we failed to insert all locations of a watchpoint, remove
3127 them, as half-inserted watchpoint is of limited use. */
3128 ALL_BREAKPOINTS (bpt)
3129 {
3130 int some_failed = 0;
3131 struct bp_location *loc;
3132
3133 if (!is_hardware_watchpoint (bpt))
3134 continue;
3135
3136 if (!breakpoint_enabled (bpt))
3137 continue;
3138
3139 if (bpt->disposition == disp_del_at_next_stop)
3140 continue;
3141
3142 for (loc = bpt->loc; loc; loc = loc->next)
3143 if (!loc->inserted && should_be_inserted (loc))
3144 {
3145 some_failed = 1;
3146 break;
3147 }
3148 if (some_failed)
3149 {
3150 for (loc = bpt->loc; loc; loc = loc->next)
3151 if (loc->inserted)
3152 remove_breakpoint (loc, mark_uninserted);
3153
3154 hw_breakpoint_error = 1;
3155 fprintf_unfiltered (tmp_error_stream,
3156 "Could not insert hardware watchpoint %d.\n",
3157 bpt->number);
3158 error_flag = -1;
3159 }
3160 }
3161
3162 if (error_flag)
3163 {
3164 /* If a hardware breakpoint or watchpoint was inserted, add a
3165 message about possibly exhausted resources. */
3166 if (hw_breakpoint_error && !hw_bp_error_explained_already)
3167 {
3168 fprintf_unfiltered (tmp_error_stream,
3169 "Could not insert hardware breakpoints:\n\
3170 You may have requested too many hardware breakpoints/watchpoints.\n");
3171 }
3172 target_terminal_ours_for_output ();
3173 error_stream (tmp_error_stream);
3174 }
3175
3176 do_cleanups (cleanups);
3177 }
3178
3179 /* Used when the program stops.
3180 Returns zero if successful, or non-zero if there was a problem
3181 removing a breakpoint location. */
3182
3183 int
3184 remove_breakpoints (void)
3185 {
3186 struct bp_location *bl, **blp_tmp;
3187 int val = 0;
3188
3189 ALL_BP_LOCATIONS (bl, blp_tmp)
3190 {
3191 if (bl->inserted && !is_tracepoint (bl->owner))
3192 val |= remove_breakpoint (bl, mark_uninserted);
3193 }
3194 return val;
3195 }
3196
3197 /* When a thread exits, remove breakpoints that are related to
3198 that thread. */
3199
3200 static void
3201 remove_threaded_breakpoints (struct thread_info *tp, int silent)
3202 {
3203 struct breakpoint *b, *b_tmp;
3204
3205 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3206 {
3207 if (b->thread == tp->num && user_breakpoint_p (b))
3208 {
3209 b->disposition = disp_del_at_next_stop;
3210
3211 printf_filtered (_("\
3212 Thread-specific breakpoint %d deleted - thread %d no longer in the thread list.\n"),
3213 b->number, tp->num);
3214
3215 /* Hide it from the user. */
3216 b->number = 0;
3217 }
3218 }
3219 }
3220
3221 /* Remove breakpoints of process PID. */
3222
3223 int
3224 remove_breakpoints_pid (int pid)
3225 {
3226 struct bp_location *bl, **blp_tmp;
3227 int val;
3228 struct inferior *inf = find_inferior_pid (pid);
3229
3230 ALL_BP_LOCATIONS (bl, blp_tmp)
3231 {
3232 if (bl->pspace != inf->pspace)
3233 continue;
3234
3235 if (bl->inserted && !bl->target_info.persist)
3236 {
3237 val = remove_breakpoint (bl, mark_uninserted);
3238 if (val != 0)
3239 return val;
3240 }
3241 }
3242 return 0;
3243 }
3244
3245 int
3246 reattach_breakpoints (int pid)
3247 {
3248 struct cleanup *old_chain;
3249 struct bp_location *bl, **blp_tmp;
3250 int val;
3251 struct ui_file *tmp_error_stream;
3252 int dummy1 = 0, dummy2 = 0, dummy3 = 0;
3253 struct inferior *inf;
3254 struct thread_info *tp;
3255
3256 tp = any_live_thread_of_process (pid);
3257 if (tp == NULL)
3258 return 1;
3259
3260 inf = find_inferior_pid (pid);
3261 old_chain = save_inferior_ptid ();
3262
3263 inferior_ptid = tp->ptid;
3264
3265 tmp_error_stream = mem_fileopen ();
3266 make_cleanup_ui_file_delete (tmp_error_stream);
3267
3268 ALL_BP_LOCATIONS (bl, blp_tmp)
3269 {
3270 if (bl->pspace != inf->pspace)
3271 continue;
3272
3273 if (bl->inserted)
3274 {
3275 bl->inserted = 0;
3276 val = insert_bp_location (bl, tmp_error_stream, &dummy1, &dummy2, &dummy3);
3277 if (val != 0)
3278 {
3279 do_cleanups (old_chain);
3280 return val;
3281 }
3282 }
3283 }
3284 do_cleanups (old_chain);
3285 return 0;
3286 }
3287
3288 static int internal_breakpoint_number = -1;
3289
3290 /* Set the breakpoint number of B, depending on the value of INTERNAL.
3291 If INTERNAL is non-zero, the breakpoint number will be populated
3292 from internal_breakpoint_number and that variable decremented.
3293 Otherwise the breakpoint number will be populated from
3294 breakpoint_count and that value incremented. Internal breakpoints
3295 do not set the internal var bpnum. */
3296 static void
3297 set_breakpoint_number (int internal, struct breakpoint *b)
3298 {
3299 if (internal)
3300 b->number = internal_breakpoint_number--;
3301 else
3302 {
3303 set_breakpoint_count (breakpoint_count + 1);
3304 b->number = breakpoint_count;
3305 }
3306 }
3307
3308 static struct breakpoint *
3309 create_internal_breakpoint (struct gdbarch *gdbarch,
3310 CORE_ADDR address, enum bptype type,
3311 const struct breakpoint_ops *ops)
3312 {
3313 struct symtab_and_line sal;
3314 struct breakpoint *b;
3315
3316 init_sal (&sal); /* Initialize to zeroes. */
3317
3318 sal.pc = address;
3319 sal.section = find_pc_overlay (sal.pc);
3320 sal.pspace = current_program_space;
3321
3322 b = set_raw_breakpoint (gdbarch, sal, type, ops);
3323 b->number = internal_breakpoint_number--;
3324 b->disposition = disp_donttouch;
3325
3326 return b;
3327 }
3328
3329 static const char *const longjmp_names[] =
3330 {
3331 "longjmp", "_longjmp", "siglongjmp", "_siglongjmp"
3332 };
3333 #define NUM_LONGJMP_NAMES ARRAY_SIZE(longjmp_names)
3334
3335 /* Per-objfile data private to breakpoint.c. */
3336 struct breakpoint_objfile_data
3337 {
3338 /* Minimal symbol for "_ovly_debug_event" (if any). */
3339 struct bound_minimal_symbol overlay_msym;
3340
3341 /* Minimal symbol(s) for "longjmp", "siglongjmp", etc. (if any). */
3342 struct bound_minimal_symbol longjmp_msym[NUM_LONGJMP_NAMES];
3343
3344 /* True if we have looked for longjmp probes. */
3345 int longjmp_searched;
3346
3347 /* SystemTap probe points for longjmp (if any). */
3348 VEC (probe_p) *longjmp_probes;
3349
3350 /* Minimal symbol for "std::terminate()" (if any). */
3351 struct bound_minimal_symbol terminate_msym;
3352
3353 /* Minimal symbol for "_Unwind_DebugHook" (if any). */
3354 struct bound_minimal_symbol exception_msym;
3355
3356 /* True if we have looked for exception probes. */
3357 int exception_searched;
3358
3359 /* SystemTap probe points for unwinding (if any). */
3360 VEC (probe_p) *exception_probes;
3361 };
3362
3363 static const struct objfile_data *breakpoint_objfile_key;
3364
3365 /* Minimal symbol not found sentinel. */
3366 static struct minimal_symbol msym_not_found;
3367
3368 /* Returns TRUE if MSYM point to the "not found" sentinel. */
3369
3370 static int
3371 msym_not_found_p (const struct minimal_symbol *msym)
3372 {
3373 return msym == &msym_not_found;
3374 }
3375
3376 /* Return per-objfile data needed by breakpoint.c.
3377 Allocate the data if necessary. */
3378
3379 static struct breakpoint_objfile_data *
3380 get_breakpoint_objfile_data (struct objfile *objfile)
3381 {
3382 struct breakpoint_objfile_data *bp_objfile_data;
3383
3384 bp_objfile_data = objfile_data (objfile, breakpoint_objfile_key);
3385 if (bp_objfile_data == NULL)
3386 {
3387 bp_objfile_data = obstack_alloc (&objfile->objfile_obstack,
3388 sizeof (*bp_objfile_data));
3389
3390 memset (bp_objfile_data, 0, sizeof (*bp_objfile_data));
3391 set_objfile_data (objfile, breakpoint_objfile_key, bp_objfile_data);
3392 }
3393 return bp_objfile_data;
3394 }
3395
3396 static void
3397 free_breakpoint_probes (struct objfile *obj, void *data)
3398 {
3399 struct breakpoint_objfile_data *bp_objfile_data = data;
3400
3401 VEC_free (probe_p, bp_objfile_data->longjmp_probes);
3402 VEC_free (probe_p, bp_objfile_data->exception_probes);
3403 }
3404
3405 static void
3406 create_overlay_event_breakpoint (void)
3407 {
3408 struct objfile *objfile;
3409 const char *const func_name = "_ovly_debug_event";
3410
3411 ALL_OBJFILES (objfile)
3412 {
3413 struct breakpoint *b;
3414 struct breakpoint_objfile_data *bp_objfile_data;
3415 CORE_ADDR addr;
3416
3417 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3418
3419 if (msym_not_found_p (bp_objfile_data->overlay_msym.minsym))
3420 continue;
3421
3422 if (bp_objfile_data->overlay_msym.minsym == NULL)
3423 {
3424 struct bound_minimal_symbol m;
3425
3426 m = lookup_minimal_symbol_text (func_name, objfile);
3427 if (m.minsym == NULL)
3428 {
3429 /* Avoid future lookups in this objfile. */
3430 bp_objfile_data->overlay_msym.minsym = &msym_not_found;
3431 continue;
3432 }
3433 bp_objfile_data->overlay_msym = m;
3434 }
3435
3436 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->overlay_msym);
3437 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3438 bp_overlay_event,
3439 &internal_breakpoint_ops);
3440 b->addr_string = xstrdup (func_name);
3441
3442 if (overlay_debugging == ovly_auto)
3443 {
3444 b->enable_state = bp_enabled;
3445 overlay_events_enabled = 1;
3446 }
3447 else
3448 {
3449 b->enable_state = bp_disabled;
3450 overlay_events_enabled = 0;
3451 }
3452 }
3453 update_global_location_list (UGLL_MAY_INSERT);
3454 }
3455
3456 static void
3457 create_longjmp_master_breakpoint (void)
3458 {
3459 struct program_space *pspace;
3460 struct cleanup *old_chain;
3461
3462 old_chain = save_current_program_space ();
3463
3464 ALL_PSPACES (pspace)
3465 {
3466 struct objfile *objfile;
3467
3468 set_current_program_space (pspace);
3469
3470 ALL_OBJFILES (objfile)
3471 {
3472 int i;
3473 struct gdbarch *gdbarch;
3474 struct breakpoint_objfile_data *bp_objfile_data;
3475
3476 gdbarch = get_objfile_arch (objfile);
3477
3478 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3479
3480 if (!bp_objfile_data->longjmp_searched)
3481 {
3482 VEC (probe_p) *ret;
3483
3484 ret = find_probes_in_objfile (objfile, "libc", "longjmp");
3485 if (ret != NULL)
3486 {
3487 /* We are only interested in checking one element. */
3488 struct probe *p = VEC_index (probe_p, ret, 0);
3489
3490 if (!can_evaluate_probe_arguments (p))
3491 {
3492 /* We cannot use the probe interface here, because it does
3493 not know how to evaluate arguments. */
3494 VEC_free (probe_p, ret);
3495 ret = NULL;
3496 }
3497 }
3498 bp_objfile_data->longjmp_probes = ret;
3499 bp_objfile_data->longjmp_searched = 1;
3500 }
3501
3502 if (bp_objfile_data->longjmp_probes != NULL)
3503 {
3504 int i;
3505 struct probe *probe;
3506 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3507
3508 for (i = 0;
3509 VEC_iterate (probe_p,
3510 bp_objfile_data->longjmp_probes,
3511 i, probe);
3512 ++i)
3513 {
3514 struct breakpoint *b;
3515
3516 b = create_internal_breakpoint (gdbarch,
3517 get_probe_address (probe,
3518 objfile),
3519 bp_longjmp_master,
3520 &internal_breakpoint_ops);
3521 b->addr_string = xstrdup ("-probe-stap libc:longjmp");
3522 b->enable_state = bp_disabled;
3523 }
3524
3525 continue;
3526 }
3527
3528 if (!gdbarch_get_longjmp_target_p (gdbarch))
3529 continue;
3530
3531 for (i = 0; i < NUM_LONGJMP_NAMES; i++)
3532 {
3533 struct breakpoint *b;
3534 const char *func_name;
3535 CORE_ADDR addr;
3536
3537 if (msym_not_found_p (bp_objfile_data->longjmp_msym[i].minsym))
3538 continue;
3539
3540 func_name = longjmp_names[i];
3541 if (bp_objfile_data->longjmp_msym[i].minsym == NULL)
3542 {
3543 struct bound_minimal_symbol m;
3544
3545 m = lookup_minimal_symbol_text (func_name, objfile);
3546 if (m.minsym == NULL)
3547 {
3548 /* Prevent future lookups in this objfile. */
3549 bp_objfile_data->longjmp_msym[i].minsym = &msym_not_found;
3550 continue;
3551 }
3552 bp_objfile_data->longjmp_msym[i] = m;
3553 }
3554
3555 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->longjmp_msym[i]);
3556 b = create_internal_breakpoint (gdbarch, addr, bp_longjmp_master,
3557 &internal_breakpoint_ops);
3558 b->addr_string = xstrdup (func_name);
3559 b->enable_state = bp_disabled;
3560 }
3561 }
3562 }
3563 update_global_location_list (UGLL_MAY_INSERT);
3564
3565 do_cleanups (old_chain);
3566 }
3567
3568 /* Create a master std::terminate breakpoint. */
3569 static void
3570 create_std_terminate_master_breakpoint (void)
3571 {
3572 struct program_space *pspace;
3573 struct cleanup *old_chain;
3574 const char *const func_name = "std::terminate()";
3575
3576 old_chain = save_current_program_space ();
3577
3578 ALL_PSPACES (pspace)
3579 {
3580 struct objfile *objfile;
3581 CORE_ADDR addr;
3582
3583 set_current_program_space (pspace);
3584
3585 ALL_OBJFILES (objfile)
3586 {
3587 struct breakpoint *b;
3588 struct breakpoint_objfile_data *bp_objfile_data;
3589
3590 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3591
3592 if (msym_not_found_p (bp_objfile_data->terminate_msym.minsym))
3593 continue;
3594
3595 if (bp_objfile_data->terminate_msym.minsym == NULL)
3596 {
3597 struct bound_minimal_symbol m;
3598
3599 m = lookup_minimal_symbol (func_name, NULL, objfile);
3600 if (m.minsym == NULL || (MSYMBOL_TYPE (m.minsym) != mst_text
3601 && MSYMBOL_TYPE (m.minsym) != mst_file_text))
3602 {
3603 /* Prevent future lookups in this objfile. */
3604 bp_objfile_data->terminate_msym.minsym = &msym_not_found;
3605 continue;
3606 }
3607 bp_objfile_data->terminate_msym = m;
3608 }
3609
3610 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->terminate_msym);
3611 b = create_internal_breakpoint (get_objfile_arch (objfile), addr,
3612 bp_std_terminate_master,
3613 &internal_breakpoint_ops);
3614 b->addr_string = xstrdup (func_name);
3615 b->enable_state = bp_disabled;
3616 }
3617 }
3618
3619 update_global_location_list (UGLL_MAY_INSERT);
3620
3621 do_cleanups (old_chain);
3622 }
3623
3624 /* Install a master breakpoint on the unwinder's debug hook. */
3625
3626 static void
3627 create_exception_master_breakpoint (void)
3628 {
3629 struct objfile *objfile;
3630 const char *const func_name = "_Unwind_DebugHook";
3631
3632 ALL_OBJFILES (objfile)
3633 {
3634 struct breakpoint *b;
3635 struct gdbarch *gdbarch;
3636 struct breakpoint_objfile_data *bp_objfile_data;
3637 CORE_ADDR addr;
3638
3639 bp_objfile_data = get_breakpoint_objfile_data (objfile);
3640
3641 /* We prefer the SystemTap probe point if it exists. */
3642 if (!bp_objfile_data->exception_searched)
3643 {
3644 VEC (probe_p) *ret;
3645
3646 ret = find_probes_in_objfile (objfile, "libgcc", "unwind");
3647
3648 if (ret != NULL)
3649 {
3650 /* We are only interested in checking one element. */
3651 struct probe *p = VEC_index (probe_p, ret, 0);
3652
3653 if (!can_evaluate_probe_arguments (p))
3654 {
3655 /* We cannot use the probe interface here, because it does
3656 not know how to evaluate arguments. */
3657 VEC_free (probe_p, ret);
3658 ret = NULL;
3659 }
3660 }
3661 bp_objfile_data->exception_probes = ret;
3662 bp_objfile_data->exception_searched = 1;
3663 }
3664
3665 if (bp_objfile_data->exception_probes != NULL)
3666 {
3667 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3668 int i;
3669 struct probe *probe;
3670
3671 for (i = 0;
3672 VEC_iterate (probe_p,
3673 bp_objfile_data->exception_probes,
3674 i, probe);
3675 ++i)
3676 {
3677 struct breakpoint *b;
3678
3679 b = create_internal_breakpoint (gdbarch,
3680 get_probe_address (probe,
3681 objfile),
3682 bp_exception_master,
3683 &internal_breakpoint_ops);
3684 b->addr_string = xstrdup ("-probe-stap libgcc:unwind");
3685 b->enable_state = bp_disabled;
3686 }
3687
3688 continue;
3689 }
3690
3691 /* Otherwise, try the hook function. */
3692
3693 if (msym_not_found_p (bp_objfile_data->exception_msym.minsym))
3694 continue;
3695
3696 gdbarch = get_objfile_arch (objfile);
3697
3698 if (bp_objfile_data->exception_msym.minsym == NULL)
3699 {
3700 struct bound_minimal_symbol debug_hook;
3701
3702 debug_hook = lookup_minimal_symbol (func_name, NULL, objfile);
3703 if (debug_hook.minsym == NULL)
3704 {
3705 bp_objfile_data->exception_msym.minsym = &msym_not_found;
3706 continue;
3707 }
3708
3709 bp_objfile_data->exception_msym = debug_hook;
3710 }
3711
3712 addr = BMSYMBOL_VALUE_ADDRESS (bp_objfile_data->exception_msym);
3713 addr = gdbarch_convert_from_func_ptr_addr (gdbarch, addr,
3714 &current_target);
3715 b = create_internal_breakpoint (gdbarch, addr, bp_exception_master,
3716 &internal_breakpoint_ops);
3717 b->addr_string = xstrdup (func_name);
3718 b->enable_state = bp_disabled;
3719 }
3720
3721 update_global_location_list (UGLL_MAY_INSERT);
3722 }
3723
3724 void
3725 update_breakpoints_after_exec (void)
3726 {
3727 struct breakpoint *b, *b_tmp;
3728 struct bp_location *bploc, **bplocp_tmp;
3729
3730 /* We're about to delete breakpoints from GDB's lists. If the
3731 INSERTED flag is true, GDB will try to lift the breakpoints by
3732 writing the breakpoints' "shadow contents" back into memory. The
3733 "shadow contents" are NOT valid after an exec, so GDB should not
3734 do that. Instead, the target is responsible from marking
3735 breakpoints out as soon as it detects an exec. We don't do that
3736 here instead, because there may be other attempts to delete
3737 breakpoints after detecting an exec and before reaching here. */
3738 ALL_BP_LOCATIONS (bploc, bplocp_tmp)
3739 if (bploc->pspace == current_program_space)
3740 gdb_assert (!bploc->inserted);
3741
3742 ALL_BREAKPOINTS_SAFE (b, b_tmp)
3743 {
3744 if (b->pspace != current_program_space)
3745 continue;
3746
3747 /* Solib breakpoints must be explicitly reset after an exec(). */
3748 if (b->type == bp_shlib_event)
3749 {
3750 delete_breakpoint (b);
3751 continue;
3752 }
3753
3754 /* JIT breakpoints must be explicitly reset after an exec(). */
3755 if (b->type == bp_jit_event)
3756 {
3757 delete_breakpoint (b);
3758 continue;
3759 }
3760
3761 /* Thread event breakpoints must be set anew after an exec(),
3762 as must overlay event and longjmp master breakpoints. */
3763 if (b->type == bp_thread_event || b->type == bp_overlay_event
3764 || b->type == bp_longjmp_master || b->type == bp_std_terminate_master
3765 || b->type == bp_exception_master)
3766 {
3767 delete_breakpoint (b);
3768 continue;
3769 }
3770
3771 /* Step-resume breakpoints are meaningless after an exec(). */
3772 if (b->type == bp_step_resume || b->type == bp_hp_step_resume)
3773 {
3774 delete_breakpoint (b);
3775 continue;
3776 }
3777
3778 /* Just like single-step breakpoints. */
3779 if (b->type == bp_single_step)
3780 {
3781 delete_breakpoint (b);
3782 continue;
3783 }
3784
3785 /* Longjmp and longjmp-resume breakpoints are also meaningless
3786 after an exec. */
3787 if (b->type == bp_longjmp || b->type == bp_longjmp_resume
3788 || b->type == bp_longjmp_call_dummy
3789 || b->type == bp_exception || b->type == bp_exception_resume)
3790 {
3791 delete_breakpoint (b);
3792 continue;
3793 }
3794
3795 if (b->type == bp_catchpoint)
3796 {
3797 /* For now, none of the bp_catchpoint breakpoints need to
3798 do anything at this point. In the future, if some of
3799 the catchpoints need to something, we will need to add
3800 a new method, and call this method from here. */
3801 continue;
3802 }
3803
3804 /* bp_finish is a special case. The only way we ought to be able
3805 to see one of these when an exec() has happened, is if the user
3806 caught a vfork, and then said "finish". Ordinarily a finish just
3807 carries them to the call-site of the current callee, by setting
3808 a temporary bp there and resuming. But in this case, the finish
3809 will carry them entirely through the vfork & exec.
3810
3811 We don't want to allow a bp_finish to remain inserted now. But
3812 we can't safely delete it, 'cause finish_command has a handle to
3813 the bp on a bpstat, and will later want to delete it. There's a
3814 chance (and I've seen it happen) that if we delete the bp_finish
3815 here, that its storage will get reused by the time finish_command
3816 gets 'round to deleting the "use to be a bp_finish" breakpoint.
3817 We really must allow finish_command to delete a bp_finish.
3818
3819 In the absence of a general solution for the "how do we know
3820 it's safe to delete something others may have handles to?"
3821 problem, what we'll do here is just uninsert the bp_finish, and
3822 let finish_command delete it.
3823
3824 (We know the bp_finish is "doomed" in the sense that it's
3825 momentary, and will be deleted as soon as finish_command sees
3826 the inferior stopped. So it doesn't matter that the bp's
3827 address is probably bogus in the new a.out, unlike e.g., the
3828 solib breakpoints.) */
3829
3830 if (b->type == bp_finish)
3831 {
3832 continue;
3833 }
3834
3835 /* Without a symbolic address, we have little hope of the
3836 pre-exec() address meaning the same thing in the post-exec()
3837 a.out. */
3838 if (b->addr_string == NULL)
3839 {
3840 delete_breakpoint (b);
3841 continue;
3842 }
3843 }
3844 }
3845
3846 int
3847 detach_breakpoints (ptid_t ptid)
3848 {
3849 struct bp_location *bl, **blp_tmp;
3850 int val = 0;
3851 struct cleanup *old_chain = save_inferior_ptid ();
3852 struct inferior *inf = current_inferior ();
3853
3854 if (ptid_get_pid (ptid) == ptid_get_pid (inferior_ptid))
3855 error (_("Cannot detach breakpoints of inferior_ptid"));
3856
3857 /* Set inferior_ptid; remove_breakpoint_1 uses this global. */
3858 inferior_ptid = ptid;
3859 ALL_BP_LOCATIONS (bl, blp_tmp)
3860 {
3861 if (bl->pspace != inf->pspace)
3862 continue;
3863
3864 /* This function must physically remove breakpoints locations
3865 from the specified ptid, without modifying the breakpoint
3866 package's state. Locations of type bp_loc_other are only
3867 maintained at GDB side. So, there is no need to remove
3868 these bp_loc_other locations. Moreover, removing these
3869 would modify the breakpoint package's state. */
3870 if (bl->loc_type == bp_loc_other)
3871 continue;
3872
3873 if (bl->inserted)
3874 val |= remove_breakpoint_1 (bl, mark_inserted);
3875 }
3876
3877 do_cleanups (old_chain);
3878 return val;
3879 }
3880
3881 /* Remove the breakpoint location BL from the current address space.
3882 Note that this is used to detach breakpoints from a child fork.
3883 When we get here, the child isn't in the inferior list, and neither
3884 do we have objects to represent its address space --- we should
3885 *not* look at bl->pspace->aspace here. */
3886
3887 static int
3888 remove_breakpoint_1 (struct bp_location *bl, insertion_state_t is)
3889 {
3890 int val;
3891
3892 /* BL is never in moribund_locations by our callers. */
3893 gdb_assert (bl->owner != NULL);
3894
3895 if (bl->permanent)
3896 /* Permanent breakpoints cannot be inserted or removed. */
3897 return 0;
3898
3899 /* The type of none suggests that owner is actually deleted.
3900 This should not ever happen. */
3901 gdb_assert (bl->owner->type != bp_none);
3902
3903 if (bl->loc_type == bp_loc_software_breakpoint
3904 || bl->loc_type == bp_loc_hardware_breakpoint)
3905 {
3906 /* "Normal" instruction breakpoint: either the standard
3907 trap-instruction bp (bp_breakpoint), or a
3908 bp_hardware_breakpoint. */
3909
3910 /* First check to see if we have to handle an overlay. */
3911 if (overlay_debugging == ovly_off
3912 || bl->section == NULL
3913 || !(section_is_overlay (bl->section)))
3914 {
3915 /* No overlay handling: just remove the breakpoint. */
3916
3917 /* If we're trying to uninsert a memory breakpoint that we
3918 know is set in a dynamic object that is marked
3919 shlib_disabled, then either the dynamic object was
3920 removed with "remove-symbol-file" or with
3921 "nosharedlibrary". In the former case, we don't know
3922 whether another dynamic object might have loaded over the
3923 breakpoint's address -- the user might well let us know
3924 about it next with add-symbol-file (the whole point of
3925 add-symbol-file is letting the user manually maintain a
3926 list of dynamically loaded objects). If we have the
3927 breakpoint's shadow memory, that is, this is a software
3928 breakpoint managed by GDB, check whether the breakpoint
3929 is still inserted in memory, to avoid overwriting wrong
3930 code with stale saved shadow contents. Note that HW
3931 breakpoints don't have shadow memory, as they're
3932 implemented using a mechanism that is not dependent on
3933 being able to modify the target's memory, and as such
3934 they should always be removed. */
3935 if (bl->shlib_disabled
3936 && bl->target_info.shadow_len != 0
3937 && !memory_validate_breakpoint (bl->gdbarch, &bl->target_info))
3938 val = 0;
3939 else
3940 val = bl->owner->ops->remove_location (bl);
3941 }
3942 else
3943 {
3944 /* This breakpoint is in an overlay section.
3945 Did we set a breakpoint at the LMA? */
3946 if (!overlay_events_enabled)
3947 {
3948 /* Yes -- overlay event support is not active, so we
3949 should have set a breakpoint at the LMA. Remove it.
3950 */
3951 /* Ignore any failures: if the LMA is in ROM, we will
3952 have already warned when we failed to insert it. */
3953 if (bl->loc_type == bp_loc_hardware_breakpoint)
3954 target_remove_hw_breakpoint (bl->gdbarch,
3955 &bl->overlay_target_info);
3956 else
3957 target_remove_breakpoint (bl->gdbarch,
3958 &bl->overlay_target_info);
3959 }
3960 /* Did we set a breakpoint at the VMA?
3961 If so, we will have marked the breakpoint 'inserted'. */
3962 if (bl->inserted)
3963 {
3964 /* Yes -- remove it. Previously we did not bother to
3965 remove the breakpoint if the section had been
3966 unmapped, but let's not rely on that being safe. We
3967 don't know what the overlay manager might do. */
3968
3969 /* However, we should remove *software* breakpoints only
3970 if the section is still mapped, or else we overwrite
3971 wrong code with the saved shadow contents. */
3972 if (bl->loc_type == bp_loc_hardware_breakpoint
3973 || section_is_mapped (bl->section))
3974 val = bl->owner->ops->remove_location (bl);
3975 else
3976 val = 0;
3977 }
3978 else
3979 {
3980 /* No -- not inserted, so no need to remove. No error. */
3981 val = 0;
3982 }
3983 }
3984
3985 /* In some cases, we might not be able to remove a breakpoint in
3986 a shared library that has already been removed, but we have
3987 not yet processed the shlib unload event. Similarly for an
3988 unloaded add-symbol-file object - the user might not yet have
3989 had the chance to remove-symbol-file it. shlib_disabled will
3990 be set if the library/object has already been removed, but
3991 the breakpoint hasn't been uninserted yet, e.g., after
3992 "nosharedlibrary" or "remove-symbol-file" with breakpoints
3993 always-inserted mode. */
3994 if (val
3995 && (bl->loc_type == bp_loc_software_breakpoint
3996 && (bl->shlib_disabled
3997 || solib_name_from_address (bl->pspace, bl->address)
3998 || shared_objfile_contains_address_p (bl->pspace,
3999 bl->address))))
4000 val = 0;
4001
4002 if (val)
4003 return val;
4004 bl->inserted = (is == mark_inserted);
4005 }
4006 else if (bl->loc_type == bp_loc_hardware_watchpoint)
4007 {
4008 gdb_assert (bl->owner->ops != NULL
4009 && bl->owner->ops->remove_location != NULL);
4010
4011 bl->inserted = (is == mark_inserted);
4012 bl->owner->ops->remove_location (bl);
4013
4014 /* Failure to remove any of the hardware watchpoints comes here. */
4015 if ((is == mark_uninserted) && (bl->inserted))
4016 warning (_("Could not remove hardware watchpoint %d."),
4017 bl->owner->number);
4018 }
4019 else if (bl->owner->type == bp_catchpoint
4020 && breakpoint_enabled (bl->owner)
4021 && !bl->duplicate)
4022 {
4023 gdb_assert (bl->owner->ops != NULL
4024 && bl->owner->ops->remove_location != NULL);
4025
4026 val = bl->owner->ops->remove_location (bl);
4027 if (val)
4028 return val;
4029
4030 bl->inserted = (is == mark_inserted);
4031 }
4032
4033 return 0;
4034 }
4035
4036 static int
4037 remove_breakpoint (struct bp_location *bl, insertion_state_t is)
4038 {
4039 int ret;
4040 struct cleanup *old_chain;
4041
4042 /* BL is never in moribund_locations by our callers. */
4043 gdb_assert (bl->owner != NULL);
4044
4045 if (bl->permanent)
4046 /* Permanent breakpoints cannot be inserted or removed. */
4047 return 0;
4048
4049 /* The type of none suggests that owner is actually deleted.
4050 This should not ever happen. */
4051 gdb_assert (bl->owner->type != bp_none);
4052
4053 old_chain = save_current_space_and_thread ();
4054
4055 switch_to_program_space_and_thread (bl->pspace);
4056
4057 ret = remove_breakpoint_1 (bl, is);
4058
4059 do_cleanups (old_chain);
4060 return ret;
4061 }
4062
4063 /* Clear the "inserted" flag in all breakpoints. */
4064
4065 void
4066 mark_breakpoints_out (void)
4067 {
4068 struct bp_location *bl, **blp_tmp;
4069
4070 ALL_BP_LOCATIONS (bl, blp_tmp)
4071 if (bl->pspace == current_program_space
4072 && !bl->permanent)
4073 bl->inserted = 0;
4074 }
4075
4076 /* Clear the "inserted" flag in all breakpoints and delete any
4077 breakpoints which should go away between runs of the program.
4078
4079 Plus other such housekeeping that has to be done for breakpoints
4080 between runs.
4081
4082 Note: this function gets called at the end of a run (by
4083 generic_mourn_inferior) and when a run begins (by
4084 init_wait_for_inferior). */
4085
4086
4087
4088 void
4089 breakpoint_init_inferior (enum inf_context context)
4090 {
4091 struct breakpoint *b, *b_tmp;
4092 struct bp_location *bl, **blp_tmp;
4093 int ix;
4094 struct program_space *pspace = current_program_space;
4095
4096 /* If breakpoint locations are shared across processes, then there's
4097 nothing to do. */
4098 if (gdbarch_has_global_breakpoints (target_gdbarch ()))
4099 return;
4100
4101 mark_breakpoints_out ();
4102
4103 ALL_BREAKPOINTS_SAFE (b, b_tmp)
4104 {
4105 if (b->loc && b->loc->pspace != pspace)
4106 continue;
4107
4108 switch (b->type)
4109 {
4110 case bp_call_dummy:
4111 case bp_longjmp_call_dummy:
4112
4113 /* If the call dummy breakpoint is at the entry point it will
4114 cause problems when the inferior is rerun, so we better get
4115 rid of it. */
4116
4117 case bp_watchpoint_scope:
4118
4119 /* Also get rid of scope breakpoints. */
4120
4121 case bp_shlib_event:
4122
4123 /* Also remove solib event breakpoints. Their addresses may
4124 have changed since the last time we ran the program.
4125 Actually we may now be debugging against different target;
4126 and so the solib backend that installed this breakpoint may
4127 not be used in by the target. E.g.,
4128
4129 (gdb) file prog-linux
4130 (gdb) run # native linux target
4131 ...
4132 (gdb) kill
4133 (gdb) file prog-win.exe
4134 (gdb) tar rem :9999 # remote Windows gdbserver.
4135 */
4136
4137 case bp_step_resume:
4138
4139 /* Also remove step-resume breakpoints. */
4140
4141 case bp_single_step:
4142
4143 /* Also remove single-step breakpoints. */
4144
4145 delete_breakpoint (b);
4146 break;
4147
4148 case bp_watchpoint:
4149 case bp_hardware_watchpoint:
4150 case bp_read_watchpoint:
4151 case bp_access_watchpoint:
4152 {
4153 struct watchpoint *w = (struct watchpoint *) b;
4154
4155 /* Likewise for watchpoints on local expressions. */
4156 if (w->exp_valid_block != NULL)
4157 delete_breakpoint (b);
4158 else if (context == inf_starting)
4159 {
4160 /* Reset val field to force reread of starting value in
4161 insert_breakpoints. */
4162 if (w->val)
4163 value_free (w->val);
4164 w->val = NULL;
4165 w->val_valid = 0;
4166 }
4167 }
4168 break;
4169 default:
4170 break;
4171 }
4172 }
4173
4174 /* Get rid of the moribund locations. */
4175 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, bl); ++ix)
4176 decref_bp_location (&bl);
4177 VEC_free (bp_location_p, moribund_locations);
4178 }
4179
4180 /* These functions concern about actual breakpoints inserted in the
4181 target --- to e.g. check if we need to do decr_pc adjustment or if
4182 we need to hop over the bkpt --- so we check for address space
4183 match, not program space. */
4184
4185 /* breakpoint_here_p (PC) returns non-zero if an enabled breakpoint
4186 exists at PC. It returns ordinary_breakpoint_here if it's an
4187 ordinary breakpoint, or permanent_breakpoint_here if it's a
4188 permanent breakpoint.
4189 - When continuing from a location with an ordinary breakpoint, we
4190 actually single step once before calling insert_breakpoints.
4191 - When continuing from a location with a permanent breakpoint, we
4192 need to use the `SKIP_PERMANENT_BREAKPOINT' macro, provided by
4193 the target, to advance the PC past the breakpoint. */
4194
4195 enum breakpoint_here
4196 breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
4197 {
4198 struct bp_location *bl, **blp_tmp;
4199 int any_breakpoint_here = 0;
4200
4201 ALL_BP_LOCATIONS (bl, blp_tmp)
4202 {
4203 if (bl->loc_type != bp_loc_software_breakpoint
4204 && bl->loc_type != bp_loc_hardware_breakpoint)
4205 continue;
4206
4207 /* ALL_BP_LOCATIONS bp_location has BL->OWNER always non-NULL. */
4208 if ((breakpoint_enabled (bl->owner)
4209 || bl->permanent)
4210 && breakpoint_location_address_match (bl, aspace, pc))
4211 {
4212 if (overlay_debugging
4213 && section_is_overlay (bl->section)
4214 && !section_is_mapped (bl->section))
4215 continue; /* unmapped overlay -- can't be a match */
4216 else if (bl->permanent)
4217 return permanent_breakpoint_here;
4218 else
4219 any_breakpoint_here = 1;
4220 }
4221 }
4222
4223 return any_breakpoint_here ? ordinary_breakpoint_here : 0;
4224 }
4225
4226 /* Return true if there's a moribund breakpoint at PC. */
4227
4228 int
4229 moribund_breakpoint_here_p (struct address_space *aspace, CORE_ADDR pc)
4230 {
4231 struct bp_location *loc;
4232 int ix;
4233
4234 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
4235 if (breakpoint_location_address_match (loc, aspace, pc))
4236 return 1;
4237
4238 return 0;
4239 }
4240
4241 /* Returns non-zero iff BL is inserted at PC, in address space
4242 ASPACE. */
4243
4244 static int
4245 bp_location_inserted_here_p (struct bp_location *bl,
4246 struct address_space *aspace, CORE_ADDR pc)
4247 {
4248 if (bl->inserted
4249 && breakpoint_address_match (bl->pspace->aspace, bl->address,
4250 aspace, pc))
4251 {
4252 if (overlay_debugging
4253 && section_is_overlay (bl->section)
4254 && !section_is_mapped (bl->section))
4255 return 0; /* unmapped overlay -- can't be a match */
4256 else
4257 return 1;
4258 }
4259 return 0;
4260 }
4261
4262 /* Returns non-zero iff there's a breakpoint inserted at PC. */
4263
4264 int
4265 breakpoint_inserted_here_p (struct address_space *aspace, CORE_ADDR pc)
4266 {
4267 struct bp_location **blp, **blp_tmp = NULL;
4268 struct bp_location *bl;
4269
4270 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4271 {
4272 struct bp_location *bl = *blp;
4273
4274 if (bl->loc_type != bp_loc_software_breakpoint
4275 && bl->loc_type != bp_loc_hardware_breakpoint)
4276 continue;
4277
4278 if (bp_location_inserted_here_p (bl, aspace, pc))
4279 return 1;
4280 }
4281 return 0;
4282 }
4283
4284 /* This function returns non-zero iff there is a software breakpoint
4285 inserted at PC. */
4286
4287 int
4288 software_breakpoint_inserted_here_p (struct address_space *aspace,
4289 CORE_ADDR pc)
4290 {
4291 struct bp_location **blp, **blp_tmp = NULL;
4292 struct bp_location *bl;
4293
4294 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4295 {
4296 struct bp_location *bl = *blp;
4297
4298 if (bl->loc_type != bp_loc_software_breakpoint)
4299 continue;
4300
4301 if (bp_location_inserted_here_p (bl, aspace, pc))
4302 return 1;
4303 }
4304
4305 return 0;
4306 }
4307
4308 /* See breakpoint.h. */
4309
4310 int
4311 hardware_breakpoint_inserted_here_p (struct address_space *aspace,
4312 CORE_ADDR pc)
4313 {
4314 struct bp_location **blp, **blp_tmp = NULL;
4315 struct bp_location *bl;
4316
4317 ALL_BP_LOCATIONS_AT_ADDR (blp, blp_tmp, pc)
4318 {
4319 struct bp_location *bl = *blp;
4320
4321 if (bl->loc_type != bp_loc_hardware_breakpoint)
4322 continue;
4323
4324 if (bp_location_inserted_here_p (bl, aspace, pc))
4325 return 1;
4326 }
4327
4328 return 0;
4329 }
4330
4331 int
4332 hardware_watchpoint_inserted_in_range (struct address_space *aspace,
4333 CORE_ADDR addr, ULONGEST len)
4334 {
4335 struct breakpoint *bpt;
4336
4337 ALL_BREAKPOINTS (bpt)
4338 {
4339 struct bp_location *loc;
4340
4341 if (bpt->type != bp_hardware_watchpoint
4342 && bpt->type != bp_access_watchpoint)
4343 continue;
4344
4345 if (!breakpoint_enabled (bpt))
4346 continue;
4347
4348 for (loc = bpt->loc; loc; loc = loc->next)
4349 if (loc->pspace->aspace == aspace && loc->inserted)
4350 {
4351 CORE_ADDR l, h;
4352
4353 /* Check for intersection. */
4354 l = max (loc->address, addr);
4355 h = min (loc->address + loc->length, addr + len);
4356 if (l < h)
4357 return 1;
4358 }
4359 }
4360 return 0;
4361 }
4362 \f
4363
4364 /* bpstat stuff. External routines' interfaces are documented
4365 in breakpoint.h. */
4366
4367 int
4368 is_catchpoint (struct breakpoint *ep)
4369 {
4370 return (ep->type == bp_catchpoint);
4371 }
4372
4373 /* Frees any storage that is part of a bpstat. Does not walk the
4374 'next' chain. */
4375
4376 static void
4377 bpstat_free (bpstat bs)
4378 {
4379 if (bs->old_val != NULL)
4380 value_free (bs->old_val);
4381 decref_counted_command_line (&bs->commands);
4382 decref_bp_location (&bs->bp_location_at);
4383 xfree (bs);
4384 }
4385
4386 /* Clear a bpstat so that it says we are not at any breakpoint.
4387 Also free any storage that is part of a bpstat. */
4388
4389 void
4390 bpstat_clear (bpstat *bsp)
4391 {
4392 bpstat p;
4393 bpstat q;
4394
4395 if (bsp == 0)
4396 return;
4397 p = *bsp;
4398 while (p != NULL)
4399 {
4400 q = p->next;
4401 bpstat_free (p);
4402 p = q;
4403 }
4404 *bsp = NULL;
4405 }
4406
4407 /* Return a copy of a bpstat. Like "bs1 = bs2" but all storage that
4408 is part of the bpstat is copied as well. */
4409
4410 bpstat
4411 bpstat_copy (bpstat bs)
4412 {
4413 bpstat p = NULL;
4414 bpstat tmp;
4415 bpstat retval = NULL;
4416
4417 if (bs == NULL)
4418 return bs;
4419
4420 for (; bs != NULL; bs = bs->next)
4421 {
4422 tmp = (bpstat) xmalloc (sizeof (*tmp));
4423 memcpy (tmp, bs, sizeof (*tmp));
4424 incref_counted_command_line (tmp->commands);
4425 incref_bp_location (tmp->bp_location_at);
4426 if (bs->old_val != NULL)
4427 {
4428 tmp->old_val = value_copy (bs->old_val);
4429 release_value (tmp->old_val);
4430 }
4431
4432 if (p == NULL)
4433 /* This is the first thing in the chain. */
4434 retval = tmp;
4435 else
4436 p->next = tmp;
4437 p = tmp;
4438 }
4439 p->next = NULL;
4440 return retval;
4441 }
4442
4443 /* Find the bpstat associated with this breakpoint. */
4444
4445 bpstat
4446 bpstat_find_breakpoint (bpstat bsp, struct breakpoint *breakpoint)
4447 {
4448 if (bsp == NULL)
4449 return NULL;
4450
4451 for (; bsp != NULL; bsp = bsp->next)
4452 {
4453 if (bsp->breakpoint_at == breakpoint)
4454 return bsp;
4455 }
4456 return NULL;
4457 }
4458
4459 /* See breakpoint.h. */
4460
4461 int
4462 bpstat_explains_signal (bpstat bsp, enum gdb_signal sig)
4463 {
4464 for (; bsp != NULL; bsp = bsp->next)
4465 {
4466 if (bsp->breakpoint_at == NULL)
4467 {
4468 /* A moribund location can never explain a signal other than
4469 GDB_SIGNAL_TRAP. */
4470 if (sig == GDB_SIGNAL_TRAP)
4471 return 1;
4472 }
4473 else
4474 {
4475 if (bsp->breakpoint_at->ops->explains_signal (bsp->breakpoint_at,
4476 sig))
4477 return 1;
4478 }
4479 }
4480
4481 return 0;
4482 }
4483
4484 /* Put in *NUM the breakpoint number of the first breakpoint we are
4485 stopped at. *BSP upon return is a bpstat which points to the
4486 remaining breakpoints stopped at (but which is not guaranteed to be
4487 good for anything but further calls to bpstat_num).
4488
4489 Return 0 if passed a bpstat which does not indicate any breakpoints.
4490 Return -1 if stopped at a breakpoint that has been deleted since
4491 we set it.
4492 Return 1 otherwise. */
4493
4494 int
4495 bpstat_num (bpstat *bsp, int *num)
4496 {
4497 struct breakpoint *b;
4498
4499 if ((*bsp) == NULL)
4500 return 0; /* No more breakpoint values */
4501
4502 /* We assume we'll never have several bpstats that correspond to a
4503 single breakpoint -- otherwise, this function might return the
4504 same number more than once and this will look ugly. */
4505 b = (*bsp)->breakpoint_at;
4506 *bsp = (*bsp)->next;
4507 if (b == NULL)
4508 return -1; /* breakpoint that's been deleted since */
4509
4510 *num = b->number; /* We have its number */
4511 return 1;
4512 }
4513
4514 /* See breakpoint.h. */
4515
4516 void
4517 bpstat_clear_actions (void)
4518 {
4519 struct thread_info *tp;
4520 bpstat bs;
4521
4522 if (ptid_equal (inferior_ptid, null_ptid))
4523 return;
4524
4525 tp = find_thread_ptid (inferior_ptid);
4526 if (tp == NULL)
4527 return;
4528
4529 for (bs = tp->control.stop_bpstat; bs != NULL; bs = bs->next)
4530 {
4531 decref_counted_command_line (&bs->commands);
4532
4533 if (bs->old_val != NULL)
4534 {
4535 value_free (bs->old_val);
4536 bs->old_val = NULL;
4537 }
4538 }
4539 }
4540
4541 /* Called when a command is about to proceed the inferior. */
4542
4543 static void
4544 breakpoint_about_to_proceed (void)
4545 {
4546 if (!ptid_equal (inferior_ptid, null_ptid))
4547 {
4548 struct thread_info *tp = inferior_thread ();
4549
4550 /* Allow inferior function calls in breakpoint commands to not
4551 interrupt the command list. When the call finishes
4552 successfully, the inferior will be standing at the same
4553 breakpoint as if nothing happened. */
4554 if (tp->control.in_infcall)
4555 return;
4556 }
4557
4558 breakpoint_proceeded = 1;
4559 }
4560
4561 /* Stub for cleaning up our state if we error-out of a breakpoint
4562 command. */
4563 static void
4564 cleanup_executing_breakpoints (void *ignore)
4565 {
4566 executing_breakpoint_commands = 0;
4567 }
4568
4569 /* Return non-zero iff CMD as the first line of a command sequence is `silent'
4570 or its equivalent. */
4571
4572 static int
4573 command_line_is_silent (struct command_line *cmd)
4574 {
4575 return cmd && (strcmp ("silent", cmd->line) == 0);
4576 }
4577
4578 /* Execute all the commands associated with all the breakpoints at
4579 this location. Any of these commands could cause the process to
4580 proceed beyond this point, etc. We look out for such changes by
4581 checking the global "breakpoint_proceeded" after each command.
4582
4583 Returns true if a breakpoint command resumed the inferior. In that
4584 case, it is the caller's responsibility to recall it again with the
4585 bpstat of the current thread. */
4586
4587 static int
4588 bpstat_do_actions_1 (bpstat *bsp)
4589 {
4590 bpstat bs;
4591 struct cleanup *old_chain;
4592 int again = 0;
4593
4594 /* Avoid endless recursion if a `source' command is contained
4595 in bs->commands. */
4596 if (executing_breakpoint_commands)
4597 return 0;
4598
4599 executing_breakpoint_commands = 1;
4600 old_chain = make_cleanup (cleanup_executing_breakpoints, 0);
4601
4602 prevent_dont_repeat ();
4603
4604 /* This pointer will iterate over the list of bpstat's. */
4605 bs = *bsp;
4606
4607 breakpoint_proceeded = 0;
4608 for (; bs != NULL; bs = bs->next)
4609 {
4610 struct counted_command_line *ccmd;
4611 struct command_line *cmd;
4612 struct cleanup *this_cmd_tree_chain;
4613
4614 /* Take ownership of the BSP's command tree, if it has one.
4615
4616 The command tree could legitimately contain commands like
4617 'step' and 'next', which call clear_proceed_status, which
4618 frees stop_bpstat's command tree. To make sure this doesn't
4619 free the tree we're executing out from under us, we need to
4620 take ownership of the tree ourselves. Since a given bpstat's
4621 commands are only executed once, we don't need to copy it; we
4622 can clear the pointer in the bpstat, and make sure we free
4623 the tree when we're done. */
4624 ccmd = bs->commands;
4625 bs->commands = NULL;
4626 this_cmd_tree_chain = make_cleanup_decref_counted_command_line (&ccmd);
4627 cmd = ccmd ? ccmd->commands : NULL;
4628 if (command_line_is_silent (cmd))
4629 {
4630 /* The action has been already done by bpstat_stop_status. */
4631 cmd = cmd->next;
4632 }
4633
4634 while (cmd != NULL)
4635 {
4636 execute_control_command (cmd);
4637
4638 if (breakpoint_proceeded)
4639 break;
4640 else
4641 cmd = cmd->next;
4642 }
4643
4644 /* We can free this command tree now. */
4645 do_cleanups (this_cmd_tree_chain);
4646
4647 if (breakpoint_proceeded)
4648 {
4649 if (interpreter_async && target_can_async_p ())
4650 /* If we are in async mode, then the target might be still
4651 running, not stopped at any breakpoint, so nothing for
4652 us to do here -- just return to the event loop. */
4653 ;
4654 else
4655 /* In sync mode, when execute_control_command returns
4656 we're already standing on the next breakpoint.
4657 Breakpoint commands for that stop were not run, since
4658 execute_command does not run breakpoint commands --
4659 only command_line_handler does, but that one is not
4660 involved in execution of breakpoint commands. So, we
4661 can now execute breakpoint commands. It should be
4662 noted that making execute_command do bpstat actions is
4663 not an option -- in this case we'll have recursive
4664 invocation of bpstat for each breakpoint with a
4665 command, and can easily blow up GDB stack. Instead, we
4666 return true, which will trigger the caller to recall us
4667 with the new stop_bpstat. */
4668 again = 1;
4669 break;
4670 }
4671 }
4672 do_cleanups (old_chain);
4673 return again;
4674 }
4675
4676 void
4677 bpstat_do_actions (void)
4678 {
4679 struct cleanup *cleanup_if_error = make_bpstat_clear_actions_cleanup ();
4680
4681 /* Do any commands attached to breakpoint we are stopped at. */
4682 while (!ptid_equal (inferior_ptid, null_ptid)
4683 && target_has_execution
4684 && !is_exited (inferior_ptid)
4685 && !is_executing (inferior_ptid))
4686 /* Since in sync mode, bpstat_do_actions may resume the inferior,
4687 and only return when it is stopped at the next breakpoint, we
4688 keep doing breakpoint actions until it returns false to
4689 indicate the inferior was not resumed. */
4690 if (!bpstat_do_actions_1 (&inferior_thread ()->control.stop_bpstat))
4691 break;
4692
4693 discard_cleanups (cleanup_if_error);
4694 }
4695
4696 /* Print out the (old or new) value associated with a watchpoint. */
4697
4698 static void
4699 watchpoint_value_print (struct value *val, struct ui_file *stream)
4700 {
4701 if (val == NULL)
4702 fprintf_unfiltered (stream, _("<unreadable>"));
4703 else
4704 {
4705 struct value_print_options opts;
4706 get_user_print_options (&opts);
4707 value_print (val, stream, &opts);
4708 }
4709 }
4710
4711 /* Generic routine for printing messages indicating why we
4712 stopped. The behavior of this function depends on the value
4713 'print_it' in the bpstat structure. Under some circumstances we
4714 may decide not to print anything here and delegate the task to
4715 normal_stop(). */
4716
4717 static enum print_stop_action
4718 print_bp_stop_message (bpstat bs)
4719 {
4720 switch (bs->print_it)
4721 {
4722 case print_it_noop:
4723 /* Nothing should be printed for this bpstat entry. */
4724 return PRINT_UNKNOWN;
4725 break;
4726
4727 case print_it_done:
4728 /* We still want to print the frame, but we already printed the
4729 relevant messages. */
4730 return PRINT_SRC_AND_LOC;
4731 break;
4732
4733 case print_it_normal:
4734 {
4735 struct breakpoint *b = bs->breakpoint_at;
4736
4737 /* bs->breakpoint_at can be NULL if it was a momentary breakpoint
4738 which has since been deleted. */
4739 if (b == NULL)
4740 return PRINT_UNKNOWN;
4741
4742 /* Normal case. Call the breakpoint's print_it method. */
4743 return b->ops->print_it (bs);
4744 }
4745 break;
4746
4747 default:
4748 internal_error (__FILE__, __LINE__,
4749 _("print_bp_stop_message: unrecognized enum value"));
4750 break;
4751 }
4752 }
4753
4754 /* A helper function that prints a shared library stopped event. */
4755
4756 static void
4757 print_solib_event (int is_catchpoint)
4758 {
4759 int any_deleted
4760 = !VEC_empty (char_ptr, current_program_space->deleted_solibs);
4761 int any_added
4762 = !VEC_empty (so_list_ptr, current_program_space->added_solibs);
4763
4764 if (!is_catchpoint)
4765 {
4766 if (any_added || any_deleted)
4767 ui_out_text (current_uiout,
4768 _("Stopped due to shared library event:\n"));
4769 else
4770 ui_out_text (current_uiout,
4771 _("Stopped due to shared library event (no "
4772 "libraries added or removed)\n"));
4773 }
4774
4775 if (ui_out_is_mi_like_p (current_uiout))
4776 ui_out_field_string (current_uiout, "reason",
4777 async_reason_lookup (EXEC_ASYNC_SOLIB_EVENT));
4778
4779 if (any_deleted)
4780 {
4781 struct cleanup *cleanup;
4782 char *name;
4783 int ix;
4784
4785 ui_out_text (current_uiout, _(" Inferior unloaded "));
4786 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4787 "removed");
4788 for (ix = 0;
4789 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
4790 ix, name);
4791 ++ix)
4792 {
4793 if (ix > 0)
4794 ui_out_text (current_uiout, " ");
4795 ui_out_field_string (current_uiout, "library", name);
4796 ui_out_text (current_uiout, "\n");
4797 }
4798
4799 do_cleanups (cleanup);
4800 }
4801
4802 if (any_added)
4803 {
4804 struct so_list *iter;
4805 int ix;
4806 struct cleanup *cleanup;
4807
4808 ui_out_text (current_uiout, _(" Inferior loaded "));
4809 cleanup = make_cleanup_ui_out_list_begin_end (current_uiout,
4810 "added");
4811 for (ix = 0;
4812 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
4813 ix, iter);
4814 ++ix)
4815 {
4816 if (ix > 0)
4817 ui_out_text (current_uiout, " ");
4818 ui_out_field_string (current_uiout, "library", iter->so_name);
4819 ui_out_text (current_uiout, "\n");
4820 }
4821
4822 do_cleanups (cleanup);
4823 }
4824 }
4825
4826 /* Print a message indicating what happened. This is called from
4827 normal_stop(). The input to this routine is the head of the bpstat
4828 list - a list of the eventpoints that caused this stop. KIND is
4829 the target_waitkind for the stopping event. This
4830 routine calls the generic print routine for printing a message
4831 about reasons for stopping. This will print (for example) the
4832 "Breakpoint n," part of the output. The return value of this
4833 routine is one of:
4834
4835 PRINT_UNKNOWN: Means we printed nothing.
4836 PRINT_SRC_AND_LOC: Means we printed something, and expect subsequent
4837 code to print the location. An example is
4838 "Breakpoint 1, " which should be followed by
4839 the location.
4840 PRINT_SRC_ONLY: Means we printed something, but there is no need
4841 to also print the location part of the message.
4842 An example is the catch/throw messages, which
4843 don't require a location appended to the end.
4844 PRINT_NOTHING: We have done some printing and we don't need any
4845 further info to be printed. */
4846
4847 enum print_stop_action
4848 bpstat_print (bpstat bs, int kind)
4849 {
4850 int val;
4851
4852 /* Maybe another breakpoint in the chain caused us to stop.
4853 (Currently all watchpoints go on the bpstat whether hit or not.
4854 That probably could (should) be changed, provided care is taken
4855 with respect to bpstat_explains_signal). */
4856 for (; bs; bs = bs->next)
4857 {
4858 val = print_bp_stop_message (bs);
4859 if (val == PRINT_SRC_ONLY
4860 || val == PRINT_SRC_AND_LOC
4861 || val == PRINT_NOTHING)
4862 return val;
4863 }
4864
4865 /* If we had hit a shared library event breakpoint,
4866 print_bp_stop_message would print out this message. If we hit an
4867 OS-level shared library event, do the same thing. */
4868 if (kind == TARGET_WAITKIND_LOADED)
4869 {
4870 print_solib_event (0);
4871 return PRINT_NOTHING;
4872 }
4873
4874 /* We reached the end of the chain, or we got a null BS to start
4875 with and nothing was printed. */
4876 return PRINT_UNKNOWN;
4877 }
4878
4879 /* Evaluate the expression EXP and return 1 if value is zero.
4880 This returns the inverse of the condition because it is called
4881 from catch_errors which returns 0 if an exception happened, and if an
4882 exception happens we want execution to stop.
4883 The argument is a "struct expression *" that has been cast to a
4884 "void *" to make it pass through catch_errors. */
4885
4886 static int
4887 breakpoint_cond_eval (void *exp)
4888 {
4889 struct value *mark = value_mark ();
4890 int i = !value_true (evaluate_expression ((struct expression *) exp));
4891
4892 value_free_to_mark (mark);
4893 return i;
4894 }
4895
4896 /* Allocate a new bpstat. Link it to the FIFO list by BS_LINK_POINTER. */
4897
4898 static bpstat
4899 bpstat_alloc (struct bp_location *bl, bpstat **bs_link_pointer)
4900 {
4901 bpstat bs;
4902
4903 bs = (bpstat) xmalloc (sizeof (*bs));
4904 bs->next = NULL;
4905 **bs_link_pointer = bs;
4906 *bs_link_pointer = &bs->next;
4907 bs->breakpoint_at = bl->owner;
4908 bs->bp_location_at = bl;
4909 incref_bp_location (bl);
4910 /* If the condition is false, etc., don't do the commands. */
4911 bs->commands = NULL;
4912 bs->old_val = NULL;
4913 bs->print_it = print_it_normal;
4914 return bs;
4915 }
4916 \f
4917 /* The target has stopped with waitstatus WS. Check if any hardware
4918 watchpoints have triggered, according to the target. */
4919
4920 int
4921 watchpoints_triggered (struct target_waitstatus *ws)
4922 {
4923 int stopped_by_watchpoint = target_stopped_by_watchpoint ();
4924 CORE_ADDR addr;
4925 struct breakpoint *b;
4926
4927 if (!stopped_by_watchpoint)
4928 {
4929 /* We were not stopped by a watchpoint. Mark all watchpoints
4930 as not triggered. */
4931 ALL_BREAKPOINTS (b)
4932 if (is_hardware_watchpoint (b))
4933 {
4934 struct watchpoint *w = (struct watchpoint *) b;
4935
4936 w->watchpoint_triggered = watch_triggered_no;
4937 }
4938
4939 return 0;
4940 }
4941
4942 if (!target_stopped_data_address (&current_target, &addr))
4943 {
4944 /* We were stopped by a watchpoint, but we don't know where.
4945 Mark all watchpoints as unknown. */
4946 ALL_BREAKPOINTS (b)
4947 if (is_hardware_watchpoint (b))
4948 {
4949 struct watchpoint *w = (struct watchpoint *) b;
4950
4951 w->watchpoint_triggered = watch_triggered_unknown;
4952 }
4953
4954 return 1;
4955 }
4956
4957 /* The target could report the data address. Mark watchpoints
4958 affected by this data address as triggered, and all others as not
4959 triggered. */
4960
4961 ALL_BREAKPOINTS (b)
4962 if (is_hardware_watchpoint (b))
4963 {
4964 struct watchpoint *w = (struct watchpoint *) b;
4965 struct bp_location *loc;
4966
4967 w->watchpoint_triggered = watch_triggered_no;
4968 for (loc = b->loc; loc; loc = loc->next)
4969 {
4970 if (is_masked_watchpoint (b))
4971 {
4972 CORE_ADDR newaddr = addr & w->hw_wp_mask;
4973 CORE_ADDR start = loc->address & w->hw_wp_mask;
4974
4975 if (newaddr == start)
4976 {
4977 w->watchpoint_triggered = watch_triggered_yes;
4978 break;
4979 }
4980 }
4981 /* Exact match not required. Within range is sufficient. */
4982 else if (target_watchpoint_addr_within_range (&current_target,
4983 addr, loc->address,
4984 loc->length))
4985 {
4986 w->watchpoint_triggered = watch_triggered_yes;
4987 break;
4988 }
4989 }
4990 }
4991
4992 return 1;
4993 }
4994
4995 /* Possible return values for watchpoint_check (this can't be an enum
4996 because of check_errors). */
4997 /* The watchpoint has been deleted. */
4998 #define WP_DELETED 1
4999 /* The value has changed. */
5000 #define WP_VALUE_CHANGED 2
5001 /* The value has not changed. */
5002 #define WP_VALUE_NOT_CHANGED 3
5003 /* Ignore this watchpoint, no matter if the value changed or not. */
5004 #define WP_IGNORE 4
5005
5006 #define BP_TEMPFLAG 1
5007 #define BP_HARDWAREFLAG 2
5008
5009 /* Evaluate watchpoint condition expression and check if its value
5010 changed.
5011
5012 P should be a pointer to struct bpstat, but is defined as a void *
5013 in order for this function to be usable with catch_errors. */
5014
5015 static int
5016 watchpoint_check (void *p)
5017 {
5018 bpstat bs = (bpstat) p;
5019 struct watchpoint *b;
5020 struct frame_info *fr;
5021 int within_current_scope;
5022
5023 /* BS is built from an existing struct breakpoint. */
5024 gdb_assert (bs->breakpoint_at != NULL);
5025 b = (struct watchpoint *) bs->breakpoint_at;
5026
5027 /* If this is a local watchpoint, we only want to check if the
5028 watchpoint frame is in scope if the current thread is the thread
5029 that was used to create the watchpoint. */
5030 if (!watchpoint_in_thread_scope (b))
5031 return WP_IGNORE;
5032
5033 if (b->exp_valid_block == NULL)
5034 within_current_scope = 1;
5035 else
5036 {
5037 struct frame_info *frame = get_current_frame ();
5038 struct gdbarch *frame_arch = get_frame_arch (frame);
5039 CORE_ADDR frame_pc = get_frame_pc (frame);
5040
5041 /* stack_frame_destroyed_p() returns a non-zero value if we're
5042 still in the function but the stack frame has already been
5043 invalidated. Since we can't rely on the values of local
5044 variables after the stack has been destroyed, we are treating
5045 the watchpoint in that state as `not changed' without further
5046 checking. Don't mark watchpoints as changed if the current
5047 frame is in an epilogue - even if they are in some other
5048 frame, our view of the stack is likely to be wrong and
5049 frame_find_by_id could error out. */
5050 if (gdbarch_stack_frame_destroyed_p (frame_arch, frame_pc))
5051 return WP_IGNORE;
5052
5053 fr = frame_find_by_id (b->watchpoint_frame);
5054 within_current_scope = (fr != NULL);
5055
5056 /* If we've gotten confused in the unwinder, we might have
5057 returned a frame that can't describe this variable. */
5058 if (within_current_scope)
5059 {
5060 struct symbol *function;
5061
5062 function = get_frame_function (fr);
5063 if (function == NULL
5064 || !contained_in (b->exp_valid_block,
5065 SYMBOL_BLOCK_VALUE (function)))
5066 within_current_scope = 0;
5067 }
5068
5069 if (within_current_scope)
5070 /* If we end up stopping, the current frame will get selected
5071 in normal_stop. So this call to select_frame won't affect
5072 the user. */
5073 select_frame (fr);
5074 }
5075
5076 if (within_current_scope)
5077 {
5078 /* We use value_{,free_to_}mark because it could be a *long*
5079 time before we return to the command level and call
5080 free_all_values. We can't call free_all_values because we
5081 might be in the middle of evaluating a function call. */
5082
5083 int pc = 0;
5084 struct value *mark;
5085 struct value *new_val;
5086
5087 if (is_masked_watchpoint (&b->base))
5088 /* Since we don't know the exact trigger address (from
5089 stopped_data_address), just tell the user we've triggered
5090 a mask watchpoint. */
5091 return WP_VALUE_CHANGED;
5092
5093 mark = value_mark ();
5094 fetch_subexp_value (b->exp, &pc, &new_val, NULL, NULL, 0);
5095
5096 if (b->val_bitsize != 0)
5097 new_val = extract_bitfield_from_watchpoint_value (b, new_val);
5098
5099 /* We use value_equal_contents instead of value_equal because
5100 the latter coerces an array to a pointer, thus comparing just
5101 the address of the array instead of its contents. This is
5102 not what we want. */
5103 if ((b->val != NULL) != (new_val != NULL)
5104 || (b->val != NULL && !value_equal_contents (b->val, new_val)))
5105 {
5106 if (new_val != NULL)
5107 {
5108 release_value (new_val);
5109 value_free_to_mark (mark);
5110 }
5111 bs->old_val = b->val;
5112 b->val = new_val;
5113 b->val_valid = 1;
5114 return WP_VALUE_CHANGED;
5115 }
5116 else
5117 {
5118 /* Nothing changed. */
5119 value_free_to_mark (mark);
5120 return WP_VALUE_NOT_CHANGED;
5121 }
5122 }
5123 else
5124 {
5125 struct ui_out *uiout = current_uiout;
5126
5127 /* This seems like the only logical thing to do because
5128 if we temporarily ignored the watchpoint, then when
5129 we reenter the block in which it is valid it contains
5130 garbage (in the case of a function, it may have two
5131 garbage values, one before and one after the prologue).
5132 So we can't even detect the first assignment to it and
5133 watch after that (since the garbage may or may not equal
5134 the first value assigned). */
5135 /* We print all the stop information in
5136 breakpoint_ops->print_it, but in this case, by the time we
5137 call breakpoint_ops->print_it this bp will be deleted
5138 already. So we have no choice but print the information
5139 here. */
5140 if (ui_out_is_mi_like_p (uiout))
5141 ui_out_field_string
5142 (uiout, "reason", async_reason_lookup (EXEC_ASYNC_WATCHPOINT_SCOPE));
5143 ui_out_text (uiout, "\nWatchpoint ");
5144 ui_out_field_int (uiout, "wpnum", b->base.number);
5145 ui_out_text (uiout,
5146 " deleted because the program has left the block in\n\
5147 which its expression is valid.\n");
5148
5149 /* Make sure the watchpoint's commands aren't executed. */
5150 decref_counted_command_line (&b->base.commands);
5151 watchpoint_del_at_next_stop (b);
5152
5153 return WP_DELETED;
5154 }
5155 }
5156
5157 /* Return true if it looks like target has stopped due to hitting
5158 breakpoint location BL. This function does not check if we should
5159 stop, only if BL explains the stop. */
5160
5161 static int
5162 bpstat_check_location (const struct bp_location *bl,
5163 struct address_space *aspace, CORE_ADDR bp_addr,
5164 const struct target_waitstatus *ws)
5165 {
5166 struct breakpoint *b = bl->owner;
5167
5168 /* BL is from an existing breakpoint. */
5169 gdb_assert (b != NULL);
5170
5171 return b->ops->breakpoint_hit (bl, aspace, bp_addr, ws);
5172 }
5173
5174 /* Determine if the watched values have actually changed, and we
5175 should stop. If not, set BS->stop to 0. */
5176
5177 static void
5178 bpstat_check_watchpoint (bpstat bs)
5179 {
5180 const struct bp_location *bl;
5181 struct watchpoint *b;
5182
5183 /* BS is built for existing struct breakpoint. */
5184 bl = bs->bp_location_at;
5185 gdb_assert (bl != NULL);
5186 b = (struct watchpoint *) bs->breakpoint_at;
5187 gdb_assert (b != NULL);
5188
5189 {
5190 int must_check_value = 0;
5191
5192 if (b->base.type == bp_watchpoint)
5193 /* For a software watchpoint, we must always check the
5194 watched value. */
5195 must_check_value = 1;
5196 else if (b->watchpoint_triggered == watch_triggered_yes)
5197 /* We have a hardware watchpoint (read, write, or access)
5198 and the target earlier reported an address watched by
5199 this watchpoint. */
5200 must_check_value = 1;
5201 else if (b->watchpoint_triggered == watch_triggered_unknown
5202 && b->base.type == bp_hardware_watchpoint)
5203 /* We were stopped by a hardware watchpoint, but the target could
5204 not report the data address. We must check the watchpoint's
5205 value. Access and read watchpoints are out of luck; without
5206 a data address, we can't figure it out. */
5207 must_check_value = 1;
5208
5209 if (must_check_value)
5210 {
5211 char *message
5212 = xstrprintf ("Error evaluating expression for watchpoint %d\n",
5213 b->base.number);
5214 struct cleanup *cleanups = make_cleanup (xfree, message);
5215 int e = catch_errors (watchpoint_check, bs, message,
5216 RETURN_MASK_ALL);
5217 do_cleanups (cleanups);
5218 switch (e)
5219 {
5220 case WP_DELETED:
5221 /* We've already printed what needs to be printed. */
5222 bs->print_it = print_it_done;
5223 /* Stop. */
5224 break;
5225 case WP_IGNORE:
5226 bs->print_it = print_it_noop;
5227 bs->stop = 0;
5228 break;
5229 case WP_VALUE_CHANGED:
5230 if (b->base.type == bp_read_watchpoint)
5231 {
5232 /* There are two cases to consider here:
5233
5234 1. We're watching the triggered memory for reads.
5235 In that case, trust the target, and always report
5236 the watchpoint hit to the user. Even though
5237 reads don't cause value changes, the value may
5238 have changed since the last time it was read, and
5239 since we're not trapping writes, we will not see
5240 those, and as such we should ignore our notion of
5241 old value.
5242
5243 2. We're watching the triggered memory for both
5244 reads and writes. There are two ways this may
5245 happen:
5246
5247 2.1. This is a target that can't break on data
5248 reads only, but can break on accesses (reads or
5249 writes), such as e.g., x86. We detect this case
5250 at the time we try to insert read watchpoints.
5251
5252 2.2. Otherwise, the target supports read
5253 watchpoints, but, the user set an access or write
5254 watchpoint watching the same memory as this read
5255 watchpoint.
5256
5257 If we're watching memory writes as well as reads,
5258 ignore watchpoint hits when we find that the
5259 value hasn't changed, as reads don't cause
5260 changes. This still gives false positives when
5261 the program writes the same value to memory as
5262 what there was already in memory (we will confuse
5263 it for a read), but it's much better than
5264 nothing. */
5265
5266 int other_write_watchpoint = 0;
5267
5268 if (bl->watchpoint_type == hw_read)
5269 {
5270 struct breakpoint *other_b;
5271
5272 ALL_BREAKPOINTS (other_b)
5273 if (other_b->type == bp_hardware_watchpoint
5274 || other_b->type == bp_access_watchpoint)
5275 {
5276 struct watchpoint *other_w =
5277 (struct watchpoint *) other_b;
5278
5279 if (other_w->watchpoint_triggered
5280 == watch_triggered_yes)
5281 {
5282 other_write_watchpoint = 1;
5283 break;
5284 }
5285 }
5286 }
5287
5288 if (other_write_watchpoint
5289 || bl->watchpoint_type == hw_access)
5290 {
5291 /* We're watching the same memory for writes,
5292 and the value changed since the last time we
5293 updated it, so this trap must be for a write.
5294 Ignore it. */
5295 bs->print_it = print_it_noop;
5296 bs->stop = 0;
5297 }
5298 }
5299 break;
5300 case WP_VALUE_NOT_CHANGED:
5301 if (b->base.type == bp_hardware_watchpoint
5302 || b->base.type == bp_watchpoint)
5303 {
5304 /* Don't stop: write watchpoints shouldn't fire if
5305 the value hasn't changed. */
5306 bs->print_it = print_it_noop;
5307 bs->stop = 0;
5308 }
5309 /* Stop. */
5310 break;
5311 default:
5312 /* Can't happen. */
5313 case 0:
5314 /* Error from catch_errors. */
5315 printf_filtered (_("Watchpoint %d deleted.\n"), b->base.number);
5316 watchpoint_del_at_next_stop (b);
5317 /* We've already printed what needs to be printed. */
5318 bs->print_it = print_it_done;
5319 break;
5320 }
5321 }
5322 else /* must_check_value == 0 */
5323 {
5324 /* This is a case where some watchpoint(s) triggered, but
5325 not at the address of this watchpoint, or else no
5326 watchpoint triggered after all. So don't print
5327 anything for this watchpoint. */
5328 bs->print_it = print_it_noop;
5329 bs->stop = 0;
5330 }
5331 }
5332 }
5333
5334 /* For breakpoints that are currently marked as telling gdb to stop,
5335 check conditions (condition proper, frame, thread and ignore count)
5336 of breakpoint referred to by BS. If we should not stop for this
5337 breakpoint, set BS->stop to 0. */
5338
5339 static void
5340 bpstat_check_breakpoint_conditions (bpstat bs, ptid_t ptid)
5341 {
5342 const struct bp_location *bl;
5343 struct breakpoint *b;
5344 int value_is_zero = 0;
5345 struct expression *cond;
5346
5347 gdb_assert (bs->stop);
5348
5349 /* BS is built for existing struct breakpoint. */
5350 bl = bs->bp_location_at;
5351 gdb_assert (bl != NULL);
5352 b = bs->breakpoint_at;
5353 gdb_assert (b != NULL);
5354
5355 /* Even if the target evaluated the condition on its end and notified GDB, we
5356 need to do so again since GDB does not know if we stopped due to a
5357 breakpoint or a single step breakpoint. */
5358
5359 if (frame_id_p (b->frame_id)
5360 && !frame_id_eq (b->frame_id, get_stack_frame_id (get_current_frame ())))
5361 {
5362 bs->stop = 0;
5363 return;
5364 }
5365
5366 /* If this is a thread/task-specific breakpoint, don't waste cpu
5367 evaluating the condition if this isn't the specified
5368 thread/task. */
5369 if ((b->thread != -1 && b->thread != pid_to_thread_id (ptid))
5370 || (b->task != 0 && b->task != ada_get_task_number (ptid)))
5371
5372 {
5373 bs->stop = 0;
5374 return;
5375 }
5376
5377 /* Evaluate extension language breakpoints that have a "stop" method
5378 implemented. */
5379 bs->stop = breakpoint_ext_lang_cond_says_stop (b);
5380
5381 if (is_watchpoint (b))
5382 {
5383 struct watchpoint *w = (struct watchpoint *) b;
5384
5385 cond = w->cond_exp;
5386 }
5387 else
5388 cond = bl->cond;
5389
5390 if (cond && b->disposition != disp_del_at_next_stop)
5391 {
5392 int within_current_scope = 1;
5393 struct watchpoint * w;
5394
5395 /* We use value_mark and value_free_to_mark because it could
5396 be a long time before we return to the command level and
5397 call free_all_values. We can't call free_all_values
5398 because we might be in the middle of evaluating a
5399 function call. */
5400 struct value *mark = value_mark ();
5401
5402 if (is_watchpoint (b))
5403 w = (struct watchpoint *) b;
5404 else
5405 w = NULL;
5406
5407 /* Need to select the frame, with all that implies so that
5408 the conditions will have the right context. Because we
5409 use the frame, we will not see an inlined function's
5410 variables when we arrive at a breakpoint at the start
5411 of the inlined function; the current frame will be the
5412 call site. */
5413 if (w == NULL || w->cond_exp_valid_block == NULL)
5414 select_frame (get_current_frame ());
5415 else
5416 {
5417 struct frame_info *frame;
5418
5419 /* For local watchpoint expressions, which particular
5420 instance of a local is being watched matters, so we
5421 keep track of the frame to evaluate the expression
5422 in. To evaluate the condition however, it doesn't
5423 really matter which instantiation of the function
5424 where the condition makes sense triggers the
5425 watchpoint. This allows an expression like "watch
5426 global if q > 10" set in `func', catch writes to
5427 global on all threads that call `func', or catch
5428 writes on all recursive calls of `func' by a single
5429 thread. We simply always evaluate the condition in
5430 the innermost frame that's executing where it makes
5431 sense to evaluate the condition. It seems
5432 intuitive. */
5433 frame = block_innermost_frame (w->cond_exp_valid_block);
5434 if (frame != NULL)
5435 select_frame (frame);
5436 else
5437 within_current_scope = 0;
5438 }
5439 if (within_current_scope)
5440 value_is_zero
5441 = catch_errors (breakpoint_cond_eval, cond,
5442 "Error in testing breakpoint condition:\n",
5443 RETURN_MASK_ALL);
5444 else
5445 {
5446 warning (_("Watchpoint condition cannot be tested "
5447 "in the current scope"));
5448 /* If we failed to set the right context for this
5449 watchpoint, unconditionally report it. */
5450 value_is_zero = 0;
5451 }
5452 /* FIXME-someday, should give breakpoint #. */
5453 value_free_to_mark (mark);
5454 }
5455
5456 if (cond && value_is_zero)
5457 {
5458 bs->stop = 0;
5459 }
5460 else if (b->ignore_count > 0)
5461 {
5462 b->ignore_count--;
5463 bs->stop = 0;
5464 /* Increase the hit count even though we don't stop. */
5465 ++(b->hit_count);
5466 observer_notify_breakpoint_modified (b);
5467 }
5468 }
5469
5470 /* Returns true if we need to track moribund locations of LOC's type
5471 on the current target. */
5472
5473 static int
5474 need_moribund_for_location_type (struct bp_location *loc)
5475 {
5476 return ((loc->loc_type == bp_loc_software_breakpoint
5477 && !target_supports_stopped_by_sw_breakpoint ())
5478 || (loc->loc_type == bp_loc_hardware_breakpoint
5479 && !target_supports_stopped_by_hw_breakpoint ()));
5480 }
5481
5482
5483 /* Get a bpstat associated with having just stopped at address
5484 BP_ADDR in thread PTID.
5485
5486 Determine whether we stopped at a breakpoint, etc, or whether we
5487 don't understand this stop. Result is a chain of bpstat's such
5488 that:
5489
5490 if we don't understand the stop, the result is a null pointer.
5491
5492 if we understand why we stopped, the result is not null.
5493
5494 Each element of the chain refers to a particular breakpoint or
5495 watchpoint at which we have stopped. (We may have stopped for
5496 several reasons concurrently.)
5497
5498 Each element of the chain has valid next, breakpoint_at,
5499 commands, FIXME??? fields. */
5500
5501 bpstat
5502 bpstat_stop_status (struct address_space *aspace,
5503 CORE_ADDR bp_addr, ptid_t ptid,
5504 const struct target_waitstatus *ws)
5505 {
5506 struct breakpoint *b = NULL;
5507 struct bp_location *bl;
5508 struct bp_location *loc;
5509 /* First item of allocated bpstat's. */
5510 bpstat bs_head = NULL, *bs_link = &bs_head;
5511 /* Pointer to the last thing in the chain currently. */
5512 bpstat bs;
5513 int ix;
5514 int need_remove_insert;
5515 int removed_any;
5516
5517 /* First, build the bpstat chain with locations that explain a
5518 target stop, while being careful to not set the target running,
5519 as that may invalidate locations (in particular watchpoint
5520 locations are recreated). Resuming will happen here with
5521 breakpoint conditions or watchpoint expressions that include
5522 inferior function calls. */
5523
5524 ALL_BREAKPOINTS (b)
5525 {
5526 if (!breakpoint_enabled (b))
5527 continue;
5528
5529 for (bl = b->loc; bl != NULL; bl = bl->next)
5530 {
5531 /* For hardware watchpoints, we look only at the first
5532 location. The watchpoint_check function will work on the
5533 entire expression, not the individual locations. For
5534 read watchpoints, the watchpoints_triggered function has
5535 checked all locations already. */
5536 if (b->type == bp_hardware_watchpoint && bl != b->loc)
5537 break;
5538
5539 if (!bl->enabled || bl->shlib_disabled)
5540 continue;
5541
5542 if (!bpstat_check_location (bl, aspace, bp_addr, ws))
5543 continue;
5544
5545 /* Come here if it's a watchpoint, or if the break address
5546 matches. */
5547
5548 bs = bpstat_alloc (bl, &bs_link); /* Alloc a bpstat to
5549 explain stop. */
5550
5551 /* Assume we stop. Should we find a watchpoint that is not
5552 actually triggered, or if the condition of the breakpoint
5553 evaluates as false, we'll reset 'stop' to 0. */
5554 bs->stop = 1;
5555 bs->print = 1;
5556
5557 /* If this is a scope breakpoint, mark the associated
5558 watchpoint as triggered so that we will handle the
5559 out-of-scope event. We'll get to the watchpoint next
5560 iteration. */
5561 if (b->type == bp_watchpoint_scope && b->related_breakpoint != b)
5562 {
5563 struct watchpoint *w = (struct watchpoint *) b->related_breakpoint;
5564
5565 w->watchpoint_triggered = watch_triggered_yes;
5566 }
5567 }
5568 }
5569
5570 /* Check if a moribund breakpoint explains the stop. */
5571 if (!target_supports_stopped_by_sw_breakpoint ()
5572 || !target_supports_stopped_by_hw_breakpoint ())
5573 {
5574 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
5575 {
5576 if (breakpoint_location_address_match (loc, aspace, bp_addr)
5577 && need_moribund_for_location_type (loc))
5578 {
5579 bs = bpstat_alloc (loc, &bs_link);
5580 /* For hits of moribund locations, we should just proceed. */
5581 bs->stop = 0;
5582 bs->print = 0;
5583 bs->print_it = print_it_noop;
5584 }
5585 }
5586 }
5587
5588 /* A bit of special processing for shlib breakpoints. We need to
5589 process solib loading here, so that the lists of loaded and
5590 unloaded libraries are correct before we handle "catch load" and
5591 "catch unload". */
5592 for (bs = bs_head; bs != NULL; bs = bs->next)
5593 {
5594 if (bs->breakpoint_at && bs->breakpoint_at->type == bp_shlib_event)
5595 {
5596 handle_solib_event ();
5597 break;
5598 }
5599 }
5600
5601 /* Now go through the locations that caused the target to stop, and
5602 check whether we're interested in reporting this stop to higher
5603 layers, or whether we should resume the target transparently. */
5604
5605 removed_any = 0;
5606
5607 for (bs = bs_head; bs != NULL; bs = bs->next)
5608 {
5609 if (!bs->stop)
5610 continue;
5611
5612 b = bs->breakpoint_at;
5613 b->ops->check_status (bs);
5614 if (bs->stop)
5615 {
5616 bpstat_check_breakpoint_conditions (bs, ptid);
5617
5618 if (bs->stop)
5619 {
5620 ++(b->hit_count);
5621 observer_notify_breakpoint_modified (b);
5622
5623 /* We will stop here. */
5624 if (b->disposition == disp_disable)
5625 {
5626 --(b->enable_count);
5627 if (b->enable_count <= 0)
5628 b->enable_state = bp_disabled;
5629 removed_any = 1;
5630 }
5631 if (b->silent)
5632 bs->print = 0;
5633 bs->commands = b->commands;
5634 incref_counted_command_line (bs->commands);
5635 if (command_line_is_silent (bs->commands
5636 ? bs->commands->commands : NULL))
5637 bs->print = 0;
5638
5639 b->ops->after_condition_true (bs);
5640 }
5641
5642 }
5643
5644 /* Print nothing for this entry if we don't stop or don't
5645 print. */
5646 if (!bs->stop || !bs->print)
5647 bs->print_it = print_it_noop;
5648 }
5649
5650 /* If we aren't stopping, the value of some hardware watchpoint may
5651 not have changed, but the intermediate memory locations we are
5652 watching may have. Don't bother if we're stopping; this will get
5653 done later. */
5654 need_remove_insert = 0;
5655 if (! bpstat_causes_stop (bs_head))
5656 for (bs = bs_head; bs != NULL; bs = bs->next)
5657 if (!bs->stop
5658 && bs->breakpoint_at
5659 && is_hardware_watchpoint (bs->breakpoint_at))
5660 {
5661 struct watchpoint *w = (struct watchpoint *) bs->breakpoint_at;
5662
5663 update_watchpoint (w, 0 /* don't reparse. */);
5664 need_remove_insert = 1;
5665 }
5666
5667 if (need_remove_insert)
5668 update_global_location_list (UGLL_MAY_INSERT);
5669 else if (removed_any)
5670 update_global_location_list (UGLL_DONT_INSERT);
5671
5672 return bs_head;
5673 }
5674
5675 static void
5676 handle_jit_event (void)
5677 {
5678 struct frame_info *frame;
5679 struct gdbarch *gdbarch;
5680
5681 /* Switch terminal for any messages produced by
5682 breakpoint_re_set. */
5683 target_terminal_ours_for_output ();
5684
5685 frame = get_current_frame ();
5686 gdbarch = get_frame_arch (frame);
5687
5688 jit_event_handler (gdbarch);
5689
5690 target_terminal_inferior ();
5691 }
5692
5693 /* Prepare WHAT final decision for infrun. */
5694
5695 /* Decide what infrun needs to do with this bpstat. */
5696
5697 struct bpstat_what
5698 bpstat_what (bpstat bs_head)
5699 {
5700 struct bpstat_what retval;
5701 int jit_event = 0;
5702 bpstat bs;
5703
5704 retval.main_action = BPSTAT_WHAT_KEEP_CHECKING;
5705 retval.call_dummy = STOP_NONE;
5706 retval.is_longjmp = 0;
5707
5708 for (bs = bs_head; bs != NULL; bs = bs->next)
5709 {
5710 /* Extract this BS's action. After processing each BS, we check
5711 if its action overrides all we've seem so far. */
5712 enum bpstat_what_main_action this_action = BPSTAT_WHAT_KEEP_CHECKING;
5713 enum bptype bptype;
5714
5715 if (bs->breakpoint_at == NULL)
5716 {
5717 /* I suspect this can happen if it was a momentary
5718 breakpoint which has since been deleted. */
5719 bptype = bp_none;
5720 }
5721 else
5722 bptype = bs->breakpoint_at->type;
5723
5724 switch (bptype)
5725 {
5726 case bp_none:
5727 break;
5728 case bp_breakpoint:
5729 case bp_hardware_breakpoint:
5730 case bp_single_step:
5731 case bp_until:
5732 case bp_finish:
5733 case bp_shlib_event:
5734 if (bs->stop)
5735 {
5736 if (bs->print)
5737 this_action = BPSTAT_WHAT_STOP_NOISY;
5738 else
5739 this_action = BPSTAT_WHAT_STOP_SILENT;
5740 }
5741 else
5742 this_action = BPSTAT_WHAT_SINGLE;
5743 break;
5744 case bp_watchpoint:
5745 case bp_hardware_watchpoint:
5746 case bp_read_watchpoint:
5747 case bp_access_watchpoint:
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 {
5757 /* There was a watchpoint, but we're not stopping.
5758 This requires no further action. */
5759 }
5760 break;
5761 case bp_longjmp:
5762 case bp_longjmp_call_dummy:
5763 case bp_exception:
5764 this_action = BPSTAT_WHAT_SET_LONGJMP_RESUME;
5765 retval.is_longjmp = bptype != bp_exception;
5766 break;
5767 case bp_longjmp_resume:
5768 case bp_exception_resume:
5769 this_action = BPSTAT_WHAT_CLEAR_LONGJMP_RESUME;
5770 retval.is_longjmp = bptype == bp_longjmp_resume;
5771 break;
5772 case bp_step_resume:
5773 if (bs->stop)
5774 this_action = BPSTAT_WHAT_STEP_RESUME;
5775 else
5776 {
5777 /* It is for the wrong frame. */
5778 this_action = BPSTAT_WHAT_SINGLE;
5779 }
5780 break;
5781 case bp_hp_step_resume:
5782 if (bs->stop)
5783 this_action = BPSTAT_WHAT_HP_STEP_RESUME;
5784 else
5785 {
5786 /* It is for the wrong frame. */
5787 this_action = BPSTAT_WHAT_SINGLE;
5788 }
5789 break;
5790 case bp_watchpoint_scope:
5791 case bp_thread_event:
5792 case bp_overlay_event:
5793 case bp_longjmp_master:
5794 case bp_std_terminate_master:
5795 case bp_exception_master:
5796 this_action = BPSTAT_WHAT_SINGLE;
5797 break;
5798 case bp_catchpoint:
5799 if (bs->stop)
5800 {
5801 if (bs->print)
5802 this_action = BPSTAT_WHAT_STOP_NOISY;
5803 else
5804 this_action = BPSTAT_WHAT_STOP_SILENT;
5805 }
5806 else
5807 {
5808 /* There was a catchpoint, but we're not stopping.
5809 This requires no further action. */
5810 }
5811 break;
5812 case bp_jit_event:
5813 jit_event = 1;
5814 this_action = BPSTAT_WHAT_SINGLE;
5815 break;
5816 case bp_call_dummy:
5817 /* Make sure the action is stop (silent or noisy),
5818 so infrun.c pops the dummy frame. */
5819 retval.call_dummy = STOP_STACK_DUMMY;
5820 this_action = BPSTAT_WHAT_STOP_SILENT;
5821 break;
5822 case bp_std_terminate:
5823 /* Make sure the action is stop (silent or noisy),
5824 so infrun.c pops the dummy frame. */
5825 retval.call_dummy = STOP_STD_TERMINATE;
5826 this_action = BPSTAT_WHAT_STOP_SILENT;
5827 break;
5828 case bp_tracepoint:
5829 case bp_fast_tracepoint:
5830 case bp_static_tracepoint:
5831 /* Tracepoint hits should not be reported back to GDB, and
5832 if one got through somehow, it should have been filtered
5833 out already. */
5834 internal_error (__FILE__, __LINE__,
5835 _("bpstat_what: tracepoint encountered"));
5836 break;
5837 case bp_gnu_ifunc_resolver:
5838 /* Step over it (and insert bp_gnu_ifunc_resolver_return). */
5839 this_action = BPSTAT_WHAT_SINGLE;
5840 break;
5841 case bp_gnu_ifunc_resolver_return:
5842 /* The breakpoint will be removed, execution will restart from the
5843 PC of the former breakpoint. */
5844 this_action = BPSTAT_WHAT_KEEP_CHECKING;
5845 break;
5846
5847 case bp_dprintf:
5848 if (bs->stop)
5849 this_action = BPSTAT_WHAT_STOP_SILENT;
5850 else
5851 this_action = BPSTAT_WHAT_SINGLE;
5852 break;
5853
5854 default:
5855 internal_error (__FILE__, __LINE__,
5856 _("bpstat_what: unhandled bptype %d"), (int) bptype);
5857 }
5858
5859 retval.main_action = max (retval.main_action, this_action);
5860 }
5861
5862 /* These operations may affect the bs->breakpoint_at state so they are
5863 delayed after MAIN_ACTION is decided above. */
5864
5865 if (jit_event)
5866 {
5867 if (debug_infrun)
5868 fprintf_unfiltered (gdb_stdlog, "bpstat_what: bp_jit_event\n");
5869
5870 handle_jit_event ();
5871 }
5872
5873 for (bs = bs_head; bs != NULL; bs = bs->next)
5874 {
5875 struct breakpoint *b = bs->breakpoint_at;
5876
5877 if (b == NULL)
5878 continue;
5879 switch (b->type)
5880 {
5881 case bp_gnu_ifunc_resolver:
5882 gnu_ifunc_resolver_stop (b);
5883 break;
5884 case bp_gnu_ifunc_resolver_return:
5885 gnu_ifunc_resolver_return_stop (b);
5886 break;
5887 }
5888 }
5889
5890 return retval;
5891 }
5892
5893 /* Nonzero if we should step constantly (e.g. watchpoints on machines
5894 without hardware support). This isn't related to a specific bpstat,
5895 just to things like whether watchpoints are set. */
5896
5897 int
5898 bpstat_should_step (void)
5899 {
5900 struct breakpoint *b;
5901
5902 ALL_BREAKPOINTS (b)
5903 if (breakpoint_enabled (b) && b->type == bp_watchpoint && b->loc != NULL)
5904 return 1;
5905 return 0;
5906 }
5907
5908 int
5909 bpstat_causes_stop (bpstat bs)
5910 {
5911 for (; bs != NULL; bs = bs->next)
5912 if (bs->stop)
5913 return 1;
5914
5915 return 0;
5916 }
5917
5918 \f
5919
5920 /* Compute a string of spaces suitable to indent the next line
5921 so it starts at the position corresponding to the table column
5922 named COL_NAME in the currently active table of UIOUT. */
5923
5924 static char *
5925 wrap_indent_at_field (struct ui_out *uiout, const char *col_name)
5926 {
5927 static char wrap_indent[80];
5928 int i, total_width, width, align;
5929 char *text;
5930
5931 total_width = 0;
5932 for (i = 1; ui_out_query_field (uiout, i, &width, &align, &text); i++)
5933 {
5934 if (strcmp (text, col_name) == 0)
5935 {
5936 gdb_assert (total_width < sizeof wrap_indent);
5937 memset (wrap_indent, ' ', total_width);
5938 wrap_indent[total_width] = 0;
5939
5940 return wrap_indent;
5941 }
5942
5943 total_width += width + 1;
5944 }
5945
5946 return NULL;
5947 }
5948
5949 /* Determine if the locations of this breakpoint will have their conditions
5950 evaluated by the target, host or a mix of both. Returns the following:
5951
5952 "host": Host evals condition.
5953 "host or target": Host or Target evals condition.
5954 "target": Target evals condition.
5955 */
5956
5957 static const char *
5958 bp_condition_evaluator (struct breakpoint *b)
5959 {
5960 struct bp_location *bl;
5961 char host_evals = 0;
5962 char target_evals = 0;
5963
5964 if (!b)
5965 return NULL;
5966
5967 if (!is_breakpoint (b))
5968 return NULL;
5969
5970 if (gdb_evaluates_breakpoint_condition_p ()
5971 || !target_supports_evaluation_of_breakpoint_conditions ())
5972 return condition_evaluation_host;
5973
5974 for (bl = b->loc; bl; bl = bl->next)
5975 {
5976 if (bl->cond_bytecode)
5977 target_evals++;
5978 else
5979 host_evals++;
5980 }
5981
5982 if (host_evals && target_evals)
5983 return condition_evaluation_both;
5984 else if (target_evals)
5985 return condition_evaluation_target;
5986 else
5987 return condition_evaluation_host;
5988 }
5989
5990 /* Determine the breakpoint location's condition evaluator. This is
5991 similar to bp_condition_evaluator, but for locations. */
5992
5993 static const char *
5994 bp_location_condition_evaluator (struct bp_location *bl)
5995 {
5996 if (bl && !is_breakpoint (bl->owner))
5997 return NULL;
5998
5999 if (gdb_evaluates_breakpoint_condition_p ()
6000 || !target_supports_evaluation_of_breakpoint_conditions ())
6001 return condition_evaluation_host;
6002
6003 if (bl && bl->cond_bytecode)
6004 return condition_evaluation_target;
6005 else
6006 return condition_evaluation_host;
6007 }
6008
6009 /* Print the LOC location out of the list of B->LOC locations. */
6010
6011 static void
6012 print_breakpoint_location (struct breakpoint *b,
6013 struct bp_location *loc)
6014 {
6015 struct ui_out *uiout = current_uiout;
6016 struct cleanup *old_chain = save_current_program_space ();
6017
6018 if (loc != NULL && loc->shlib_disabled)
6019 loc = NULL;
6020
6021 if (loc != NULL)
6022 set_current_program_space (loc->pspace);
6023
6024 if (b->display_canonical)
6025 ui_out_field_string (uiout, "what", b->addr_string);
6026 else if (loc && loc->symtab)
6027 {
6028 struct symbol *sym
6029 = find_pc_sect_function (loc->address, loc->section);
6030 if (sym)
6031 {
6032 ui_out_text (uiout, "in ");
6033 ui_out_field_string (uiout, "func",
6034 SYMBOL_PRINT_NAME (sym));
6035 ui_out_text (uiout, " ");
6036 ui_out_wrap_hint (uiout, wrap_indent_at_field (uiout, "what"));
6037 ui_out_text (uiout, "at ");
6038 }
6039 ui_out_field_string (uiout, "file",
6040 symtab_to_filename_for_display (loc->symtab));
6041 ui_out_text (uiout, ":");
6042
6043 if (ui_out_is_mi_like_p (uiout))
6044 ui_out_field_string (uiout, "fullname",
6045 symtab_to_fullname (loc->symtab));
6046
6047 ui_out_field_int (uiout, "line", loc->line_number);
6048 }
6049 else if (loc)
6050 {
6051 struct ui_file *stb = mem_fileopen ();
6052 struct cleanup *stb_chain = make_cleanup_ui_file_delete (stb);
6053
6054 print_address_symbolic (loc->gdbarch, loc->address, stb,
6055 demangle, "");
6056 ui_out_field_stream (uiout, "at", stb);
6057
6058 do_cleanups (stb_chain);
6059 }
6060 else
6061 ui_out_field_string (uiout, "pending", b->addr_string);
6062
6063 if (loc && is_breakpoint (b)
6064 && breakpoint_condition_evaluation_mode () == condition_evaluation_target
6065 && bp_condition_evaluator (b) == condition_evaluation_both)
6066 {
6067 ui_out_text (uiout, " (");
6068 ui_out_field_string (uiout, "evaluated-by",
6069 bp_location_condition_evaluator (loc));
6070 ui_out_text (uiout, ")");
6071 }
6072
6073 do_cleanups (old_chain);
6074 }
6075
6076 static const char *
6077 bptype_string (enum bptype type)
6078 {
6079 struct ep_type_description
6080 {
6081 enum bptype type;
6082 char *description;
6083 };
6084 static struct ep_type_description bptypes[] =
6085 {
6086 {bp_none, "?deleted?"},
6087 {bp_breakpoint, "breakpoint"},
6088 {bp_hardware_breakpoint, "hw breakpoint"},
6089 {bp_single_step, "sw single-step"},
6090 {bp_until, "until"},
6091 {bp_finish, "finish"},
6092 {bp_watchpoint, "watchpoint"},
6093 {bp_hardware_watchpoint, "hw watchpoint"},
6094 {bp_read_watchpoint, "read watchpoint"},
6095 {bp_access_watchpoint, "acc watchpoint"},
6096 {bp_longjmp, "longjmp"},
6097 {bp_longjmp_resume, "longjmp resume"},
6098 {bp_longjmp_call_dummy, "longjmp for call dummy"},
6099 {bp_exception, "exception"},
6100 {bp_exception_resume, "exception resume"},
6101 {bp_step_resume, "step resume"},
6102 {bp_hp_step_resume, "high-priority step resume"},
6103 {bp_watchpoint_scope, "watchpoint scope"},
6104 {bp_call_dummy, "call dummy"},
6105 {bp_std_terminate, "std::terminate"},
6106 {bp_shlib_event, "shlib events"},
6107 {bp_thread_event, "thread events"},
6108 {bp_overlay_event, "overlay events"},
6109 {bp_longjmp_master, "longjmp master"},
6110 {bp_std_terminate_master, "std::terminate master"},
6111 {bp_exception_master, "exception master"},
6112 {bp_catchpoint, "catchpoint"},
6113 {bp_tracepoint, "tracepoint"},
6114 {bp_fast_tracepoint, "fast tracepoint"},
6115 {bp_static_tracepoint, "static tracepoint"},
6116 {bp_dprintf, "dprintf"},
6117 {bp_jit_event, "jit events"},
6118 {bp_gnu_ifunc_resolver, "STT_GNU_IFUNC resolver"},
6119 {bp_gnu_ifunc_resolver_return, "STT_GNU_IFUNC resolver return"},
6120 };
6121
6122 if (((int) type >= (sizeof (bptypes) / sizeof (bptypes[0])))
6123 || ((int) type != bptypes[(int) type].type))
6124 internal_error (__FILE__, __LINE__,
6125 _("bptypes table does not describe type #%d."),
6126 (int) type);
6127
6128 return bptypes[(int) type].description;
6129 }
6130
6131 /* For MI, output a field named 'thread-groups' with a list as the value.
6132 For CLI, prefix the list with the string 'inf'. */
6133
6134 static void
6135 output_thread_groups (struct ui_out *uiout,
6136 const char *field_name,
6137 VEC(int) *inf_num,
6138 int mi_only)
6139 {
6140 struct cleanup *back_to;
6141 int is_mi = ui_out_is_mi_like_p (uiout);
6142 int inf;
6143 int i;
6144
6145 /* For backward compatibility, don't display inferiors in CLI unless
6146 there are several. Always display them for MI. */
6147 if (!is_mi && mi_only)
6148 return;
6149
6150 back_to = make_cleanup_ui_out_list_begin_end (uiout, field_name);
6151
6152 for (i = 0; VEC_iterate (int, inf_num, i, inf); ++i)
6153 {
6154 if (is_mi)
6155 {
6156 char mi_group[10];
6157
6158 xsnprintf (mi_group, sizeof (mi_group), "i%d", inf);
6159 ui_out_field_string (uiout, NULL, mi_group);
6160 }
6161 else
6162 {
6163 if (i == 0)
6164 ui_out_text (uiout, " inf ");
6165 else
6166 ui_out_text (uiout, ", ");
6167
6168 ui_out_text (uiout, plongest (inf));
6169 }
6170 }
6171
6172 do_cleanups (back_to);
6173 }
6174
6175 /* Print B to gdb_stdout. */
6176
6177 static void
6178 print_one_breakpoint_location (struct breakpoint *b,
6179 struct bp_location *loc,
6180 int loc_number,
6181 struct bp_location **last_loc,
6182 int allflag)
6183 {
6184 struct command_line *l;
6185 static char bpenables[] = "nynny";
6186
6187 struct ui_out *uiout = current_uiout;
6188 int header_of_multiple = 0;
6189 int part_of_multiple = (loc != NULL);
6190 struct value_print_options opts;
6191
6192 get_user_print_options (&opts);
6193
6194 gdb_assert (!loc || loc_number != 0);
6195 /* See comment in print_one_breakpoint concerning treatment of
6196 breakpoints with single disabled location. */
6197 if (loc == NULL
6198 && (b->loc != NULL
6199 && (b->loc->next != NULL || !b->loc->enabled)))
6200 header_of_multiple = 1;
6201 if (loc == NULL)
6202 loc = b->loc;
6203
6204 annotate_record ();
6205
6206 /* 1 */
6207 annotate_field (0);
6208 if (part_of_multiple)
6209 {
6210 char *formatted;
6211 formatted = xstrprintf ("%d.%d", b->number, loc_number);
6212 ui_out_field_string (uiout, "number", formatted);
6213 xfree (formatted);
6214 }
6215 else
6216 {
6217 ui_out_field_int (uiout, "number", b->number);
6218 }
6219
6220 /* 2 */
6221 annotate_field (1);
6222 if (part_of_multiple)
6223 ui_out_field_skip (uiout, "type");
6224 else
6225 ui_out_field_string (uiout, "type", bptype_string (b->type));
6226
6227 /* 3 */
6228 annotate_field (2);
6229 if (part_of_multiple)
6230 ui_out_field_skip (uiout, "disp");
6231 else
6232 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
6233
6234
6235 /* 4 */
6236 annotate_field (3);
6237 if (part_of_multiple)
6238 ui_out_field_string (uiout, "enabled", loc->enabled ? "y" : "n");
6239 else
6240 ui_out_field_fmt (uiout, "enabled", "%c",
6241 bpenables[(int) b->enable_state]);
6242 ui_out_spaces (uiout, 2);
6243
6244
6245 /* 5 and 6 */
6246 if (b->ops != NULL && b->ops->print_one != NULL)
6247 {
6248 /* Although the print_one can possibly print all locations,
6249 calling it here is not likely to get any nice result. So,
6250 make sure there's just one location. */
6251 gdb_assert (b->loc == NULL || b->loc->next == NULL);
6252 b->ops->print_one (b, last_loc);
6253 }
6254 else
6255 switch (b->type)
6256 {
6257 case bp_none:
6258 internal_error (__FILE__, __LINE__,
6259 _("print_one_breakpoint: bp_none encountered\n"));
6260 break;
6261
6262 case bp_watchpoint:
6263 case bp_hardware_watchpoint:
6264 case bp_read_watchpoint:
6265 case bp_access_watchpoint:
6266 {
6267 struct watchpoint *w = (struct watchpoint *) b;
6268
6269 /* Field 4, the address, is omitted (which makes the columns
6270 not line up too nicely with the headers, but the effect
6271 is relatively readable). */
6272 if (opts.addressprint)
6273 ui_out_field_skip (uiout, "addr");
6274 annotate_field (5);
6275 ui_out_field_string (uiout, "what", w->exp_string);
6276 }
6277 break;
6278
6279 case bp_breakpoint:
6280 case bp_hardware_breakpoint:
6281 case bp_single_step:
6282 case bp_until:
6283 case bp_finish:
6284 case bp_longjmp:
6285 case bp_longjmp_resume:
6286 case bp_longjmp_call_dummy:
6287 case bp_exception:
6288 case bp_exception_resume:
6289 case bp_step_resume:
6290 case bp_hp_step_resume:
6291 case bp_watchpoint_scope:
6292 case bp_call_dummy:
6293 case bp_std_terminate:
6294 case bp_shlib_event:
6295 case bp_thread_event:
6296 case bp_overlay_event:
6297 case bp_longjmp_master:
6298 case bp_std_terminate_master:
6299 case bp_exception_master:
6300 case bp_tracepoint:
6301 case bp_fast_tracepoint:
6302 case bp_static_tracepoint:
6303 case bp_dprintf:
6304 case bp_jit_event:
6305 case bp_gnu_ifunc_resolver:
6306 case bp_gnu_ifunc_resolver_return:
6307 if (opts.addressprint)
6308 {
6309 annotate_field (4);
6310 if (header_of_multiple)
6311 ui_out_field_string (uiout, "addr", "<MULTIPLE>");
6312 else if (b->loc == NULL || loc->shlib_disabled)
6313 ui_out_field_string (uiout, "addr", "<PENDING>");
6314 else
6315 ui_out_field_core_addr (uiout, "addr",
6316 loc->gdbarch, loc->address);
6317 }
6318 annotate_field (5);
6319 if (!header_of_multiple)
6320 print_breakpoint_location (b, loc);
6321 if (b->loc)
6322 *last_loc = b->loc;
6323 break;
6324 }
6325
6326
6327 if (loc != NULL && !header_of_multiple)
6328 {
6329 struct inferior *inf;
6330 VEC(int) *inf_num = NULL;
6331 int mi_only = 1;
6332
6333 ALL_INFERIORS (inf)
6334 {
6335 if (inf->pspace == loc->pspace)
6336 VEC_safe_push (int, inf_num, inf->num);
6337 }
6338
6339 /* For backward compatibility, don't display inferiors in CLI unless
6340 there are several. Always display for MI. */
6341 if (allflag
6342 || (!gdbarch_has_global_breakpoints (target_gdbarch ())
6343 && (number_of_program_spaces () > 1
6344 || number_of_inferiors () > 1)
6345 /* LOC is for existing B, it cannot be in
6346 moribund_locations and thus having NULL OWNER. */
6347 && loc->owner->type != bp_catchpoint))
6348 mi_only = 0;
6349 output_thread_groups (uiout, "thread-groups", inf_num, mi_only);
6350 VEC_free (int, inf_num);
6351 }
6352
6353 if (!part_of_multiple)
6354 {
6355 if (b->thread != -1)
6356 {
6357 /* FIXME: This seems to be redundant and lost here; see the
6358 "stop only in" line a little further down. */
6359 ui_out_text (uiout, " thread ");
6360 ui_out_field_int (uiout, "thread", b->thread);
6361 }
6362 else if (b->task != 0)
6363 {
6364 ui_out_text (uiout, " task ");
6365 ui_out_field_int (uiout, "task", b->task);
6366 }
6367 }
6368
6369 ui_out_text (uiout, "\n");
6370
6371 if (!part_of_multiple)
6372 b->ops->print_one_detail (b, uiout);
6373
6374 if (part_of_multiple && frame_id_p (b->frame_id))
6375 {
6376 annotate_field (6);
6377 ui_out_text (uiout, "\tstop only in stack frame at ");
6378 /* FIXME: cagney/2002-12-01: Shouldn't be poking around inside
6379 the frame ID. */
6380 ui_out_field_core_addr (uiout, "frame",
6381 b->gdbarch, b->frame_id.stack_addr);
6382 ui_out_text (uiout, "\n");
6383 }
6384
6385 if (!part_of_multiple && b->cond_string)
6386 {
6387 annotate_field (7);
6388 if (is_tracepoint (b))
6389 ui_out_text (uiout, "\ttrace only if ");
6390 else
6391 ui_out_text (uiout, "\tstop only if ");
6392 ui_out_field_string (uiout, "cond", b->cond_string);
6393
6394 /* Print whether the target is doing the breakpoint's condition
6395 evaluation. If GDB is doing the evaluation, don't print anything. */
6396 if (is_breakpoint (b)
6397 && breakpoint_condition_evaluation_mode ()
6398 == condition_evaluation_target)
6399 {
6400 ui_out_text (uiout, " (");
6401 ui_out_field_string (uiout, "evaluated-by",
6402 bp_condition_evaluator (b));
6403 ui_out_text (uiout, " evals)");
6404 }
6405 ui_out_text (uiout, "\n");
6406 }
6407
6408 if (!part_of_multiple && b->thread != -1)
6409 {
6410 /* FIXME should make an annotation for this. */
6411 ui_out_text (uiout, "\tstop only in thread ");
6412 ui_out_field_int (uiout, "thread", b->thread);
6413 ui_out_text (uiout, "\n");
6414 }
6415
6416 if (!part_of_multiple)
6417 {
6418 if (b->hit_count)
6419 {
6420 /* FIXME should make an annotation for this. */
6421 if (is_catchpoint (b))
6422 ui_out_text (uiout, "\tcatchpoint");
6423 else if (is_tracepoint (b))
6424 ui_out_text (uiout, "\ttracepoint");
6425 else
6426 ui_out_text (uiout, "\tbreakpoint");
6427 ui_out_text (uiout, " already hit ");
6428 ui_out_field_int (uiout, "times", b->hit_count);
6429 if (b->hit_count == 1)
6430 ui_out_text (uiout, " time\n");
6431 else
6432 ui_out_text (uiout, " times\n");
6433 }
6434 else
6435 {
6436 /* Output the count also if it is zero, but only if this is mi. */
6437 if (ui_out_is_mi_like_p (uiout))
6438 ui_out_field_int (uiout, "times", b->hit_count);
6439 }
6440 }
6441
6442 if (!part_of_multiple && b->ignore_count)
6443 {
6444 annotate_field (8);
6445 ui_out_text (uiout, "\tignore next ");
6446 ui_out_field_int (uiout, "ignore", b->ignore_count);
6447 ui_out_text (uiout, " hits\n");
6448 }
6449
6450 /* Note that an enable count of 1 corresponds to "enable once"
6451 behavior, which is reported by the combination of enablement and
6452 disposition, so we don't need to mention it here. */
6453 if (!part_of_multiple && b->enable_count > 1)
6454 {
6455 annotate_field (8);
6456 ui_out_text (uiout, "\tdisable after ");
6457 /* Tweak the wording to clarify that ignore and enable counts
6458 are distinct, and have additive effect. */
6459 if (b->ignore_count)
6460 ui_out_text (uiout, "additional ");
6461 else
6462 ui_out_text (uiout, "next ");
6463 ui_out_field_int (uiout, "enable", b->enable_count);
6464 ui_out_text (uiout, " hits\n");
6465 }
6466
6467 if (!part_of_multiple && is_tracepoint (b))
6468 {
6469 struct tracepoint *tp = (struct tracepoint *) b;
6470
6471 if (tp->traceframe_usage)
6472 {
6473 ui_out_text (uiout, "\ttrace buffer usage ");
6474 ui_out_field_int (uiout, "traceframe-usage", tp->traceframe_usage);
6475 ui_out_text (uiout, " bytes\n");
6476 }
6477 }
6478
6479 l = b->commands ? b->commands->commands : NULL;
6480 if (!part_of_multiple && l)
6481 {
6482 struct cleanup *script_chain;
6483
6484 annotate_field (9);
6485 script_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "script");
6486 print_command_lines (uiout, l, 4);
6487 do_cleanups (script_chain);
6488 }
6489
6490 if (is_tracepoint (b))
6491 {
6492 struct tracepoint *t = (struct tracepoint *) b;
6493
6494 if (!part_of_multiple && t->pass_count)
6495 {
6496 annotate_field (10);
6497 ui_out_text (uiout, "\tpass count ");
6498 ui_out_field_int (uiout, "pass", t->pass_count);
6499 ui_out_text (uiout, " \n");
6500 }
6501
6502 /* Don't display it when tracepoint or tracepoint location is
6503 pending. */
6504 if (!header_of_multiple && loc != NULL && !loc->shlib_disabled)
6505 {
6506 annotate_field (11);
6507
6508 if (ui_out_is_mi_like_p (uiout))
6509 ui_out_field_string (uiout, "installed",
6510 loc->inserted ? "y" : "n");
6511 else
6512 {
6513 if (loc->inserted)
6514 ui_out_text (uiout, "\t");
6515 else
6516 ui_out_text (uiout, "\tnot ");
6517 ui_out_text (uiout, "installed on target\n");
6518 }
6519 }
6520 }
6521
6522 if (ui_out_is_mi_like_p (uiout) && !part_of_multiple)
6523 {
6524 if (is_watchpoint (b))
6525 {
6526 struct watchpoint *w = (struct watchpoint *) b;
6527
6528 ui_out_field_string (uiout, "original-location", w->exp_string);
6529 }
6530 else if (b->addr_string)
6531 ui_out_field_string (uiout, "original-location", b->addr_string);
6532 }
6533 }
6534
6535 static void
6536 print_one_breakpoint (struct breakpoint *b,
6537 struct bp_location **last_loc,
6538 int allflag)
6539 {
6540 struct cleanup *bkpt_chain;
6541 struct ui_out *uiout = current_uiout;
6542
6543 bkpt_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "bkpt");
6544
6545 print_one_breakpoint_location (b, NULL, 0, last_loc, allflag);
6546 do_cleanups (bkpt_chain);
6547
6548 /* If this breakpoint has custom print function,
6549 it's already printed. Otherwise, print individual
6550 locations, if any. */
6551 if (b->ops == NULL || b->ops->print_one == NULL)
6552 {
6553 /* If breakpoint has a single location that is disabled, we
6554 print it as if it had several locations, since otherwise it's
6555 hard to represent "breakpoint enabled, location disabled"
6556 situation.
6557
6558 Note that while hardware watchpoints have several locations
6559 internally, that's not a property exposed to user. */
6560 if (b->loc
6561 && !is_hardware_watchpoint (b)
6562 && (b->loc->next || !b->loc->enabled))
6563 {
6564 struct bp_location *loc;
6565 int n = 1;
6566
6567 for (loc = b->loc; loc; loc = loc->next, ++n)
6568 {
6569 struct cleanup *inner2 =
6570 make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
6571 print_one_breakpoint_location (b, loc, n, last_loc, allflag);
6572 do_cleanups (inner2);
6573 }
6574 }
6575 }
6576 }
6577
6578 static int
6579 breakpoint_address_bits (struct breakpoint *b)
6580 {
6581 int print_address_bits = 0;
6582 struct bp_location *loc;
6583
6584 for (loc = b->loc; loc; loc = loc->next)
6585 {
6586 int addr_bit;
6587
6588 /* Software watchpoints that aren't watching memory don't have
6589 an address to print. */
6590 if (b->type == bp_watchpoint && loc->watchpoint_type == -1)
6591 continue;
6592
6593 addr_bit = gdbarch_addr_bit (loc->gdbarch);
6594 if (addr_bit > print_address_bits)
6595 print_address_bits = addr_bit;
6596 }
6597
6598 return print_address_bits;
6599 }
6600
6601 struct captured_breakpoint_query_args
6602 {
6603 int bnum;
6604 };
6605
6606 static int
6607 do_captured_breakpoint_query (struct ui_out *uiout, void *data)
6608 {
6609 struct captured_breakpoint_query_args *args = data;
6610 struct breakpoint *b;
6611 struct bp_location *dummy_loc = NULL;
6612
6613 ALL_BREAKPOINTS (b)
6614 {
6615 if (args->bnum == b->number)
6616 {
6617 print_one_breakpoint (b, &dummy_loc, 0);
6618 return GDB_RC_OK;
6619 }
6620 }
6621 return GDB_RC_NONE;
6622 }
6623
6624 enum gdb_rc
6625 gdb_breakpoint_query (struct ui_out *uiout, int bnum,
6626 char **error_message)
6627 {
6628 struct captured_breakpoint_query_args args;
6629
6630 args.bnum = bnum;
6631 /* For the moment we don't trust print_one_breakpoint() to not throw
6632 an error. */
6633 if (catch_exceptions_with_msg (uiout, do_captured_breakpoint_query, &args,
6634 error_message, RETURN_MASK_ALL) < 0)
6635 return GDB_RC_FAIL;
6636 else
6637 return GDB_RC_OK;
6638 }
6639
6640 /* Return true if this breakpoint was set by the user, false if it is
6641 internal or momentary. */
6642
6643 int
6644 user_breakpoint_p (struct breakpoint *b)
6645 {
6646 return b->number > 0;
6647 }
6648
6649 /* Print information on user settable breakpoint (watchpoint, etc)
6650 number BNUM. If BNUM is -1 print all user-settable breakpoints.
6651 If ALLFLAG is non-zero, include non-user-settable breakpoints. If
6652 FILTER is non-NULL, call it on each breakpoint and only include the
6653 ones for which it returns non-zero. Return the total number of
6654 breakpoints listed. */
6655
6656 static int
6657 breakpoint_1 (char *args, int allflag,
6658 int (*filter) (const struct breakpoint *))
6659 {
6660 struct breakpoint *b;
6661 struct bp_location *last_loc = NULL;
6662 int nr_printable_breakpoints;
6663 struct cleanup *bkpttbl_chain;
6664 struct value_print_options opts;
6665 int print_address_bits = 0;
6666 int print_type_col_width = 14;
6667 struct ui_out *uiout = current_uiout;
6668
6669 get_user_print_options (&opts);
6670
6671 /* Compute the number of rows in the table, as well as the size
6672 required for address fields. */
6673 nr_printable_breakpoints = 0;
6674 ALL_BREAKPOINTS (b)
6675 {
6676 /* If we have a filter, only list the breakpoints it accepts. */
6677 if (filter && !filter (b))
6678 continue;
6679
6680 /* If we have an "args" string, it is a list of breakpoints to
6681 accept. Skip the others. */
6682 if (args != NULL && *args != '\0')
6683 {
6684 if (allflag && parse_and_eval_long (args) != b->number)
6685 continue;
6686 if (!allflag && !number_is_in_list (args, b->number))
6687 continue;
6688 }
6689
6690 if (allflag || user_breakpoint_p (b))
6691 {
6692 int addr_bit, type_len;
6693
6694 addr_bit = breakpoint_address_bits (b);
6695 if (addr_bit > print_address_bits)
6696 print_address_bits = addr_bit;
6697
6698 type_len = strlen (bptype_string (b->type));
6699 if (type_len > print_type_col_width)
6700 print_type_col_width = type_len;
6701
6702 nr_printable_breakpoints++;
6703 }
6704 }
6705
6706 if (opts.addressprint)
6707 bkpttbl_chain
6708 = make_cleanup_ui_out_table_begin_end (uiout, 6,
6709 nr_printable_breakpoints,
6710 "BreakpointTable");
6711 else
6712 bkpttbl_chain
6713 = make_cleanup_ui_out_table_begin_end (uiout, 5,
6714 nr_printable_breakpoints,
6715 "BreakpointTable");
6716
6717 if (nr_printable_breakpoints > 0)
6718 annotate_breakpoints_headers ();
6719 if (nr_printable_breakpoints > 0)
6720 annotate_field (0);
6721 ui_out_table_header (uiout, 7, ui_left, "number", "Num"); /* 1 */
6722 if (nr_printable_breakpoints > 0)
6723 annotate_field (1);
6724 ui_out_table_header (uiout, print_type_col_width, ui_left,
6725 "type", "Type"); /* 2 */
6726 if (nr_printable_breakpoints > 0)
6727 annotate_field (2);
6728 ui_out_table_header (uiout, 4, ui_left, "disp", "Disp"); /* 3 */
6729 if (nr_printable_breakpoints > 0)
6730 annotate_field (3);
6731 ui_out_table_header (uiout, 3, ui_left, "enabled", "Enb"); /* 4 */
6732 if (opts.addressprint)
6733 {
6734 if (nr_printable_breakpoints > 0)
6735 annotate_field (4);
6736 if (print_address_bits <= 32)
6737 ui_out_table_header (uiout, 10, ui_left,
6738 "addr", "Address"); /* 5 */
6739 else
6740 ui_out_table_header (uiout, 18, ui_left,
6741 "addr", "Address"); /* 5 */
6742 }
6743 if (nr_printable_breakpoints > 0)
6744 annotate_field (5);
6745 ui_out_table_header (uiout, 40, ui_noalign, "what", "What"); /* 6 */
6746 ui_out_table_body (uiout);
6747 if (nr_printable_breakpoints > 0)
6748 annotate_breakpoints_table ();
6749
6750 ALL_BREAKPOINTS (b)
6751 {
6752 QUIT;
6753 /* If we have a filter, only list the breakpoints it accepts. */
6754 if (filter && !filter (b))
6755 continue;
6756
6757 /* If we have an "args" string, it is a list of breakpoints to
6758 accept. Skip the others. */
6759
6760 if (args != NULL && *args != '\0')
6761 {
6762 if (allflag) /* maintenance info breakpoint */
6763 {
6764 if (parse_and_eval_long (args) != b->number)
6765 continue;
6766 }
6767 else /* all others */
6768 {
6769 if (!number_is_in_list (args, b->number))
6770 continue;
6771 }
6772 }
6773 /* We only print out user settable breakpoints unless the
6774 allflag is set. */
6775 if (allflag || user_breakpoint_p (b))
6776 print_one_breakpoint (b, &last_loc, allflag);
6777 }
6778
6779 do_cleanups (bkpttbl_chain);
6780
6781 if (nr_printable_breakpoints == 0)
6782 {
6783 /* If there's a filter, let the caller decide how to report
6784 empty list. */
6785 if (!filter)
6786 {
6787 if (args == NULL || *args == '\0')
6788 ui_out_message (uiout, 0, "No breakpoints or watchpoints.\n");
6789 else
6790 ui_out_message (uiout, 0,
6791 "No breakpoint or watchpoint matching '%s'.\n",
6792 args);
6793 }
6794 }
6795 else
6796 {
6797 if (last_loc && !server_command)
6798 set_next_address (last_loc->gdbarch, last_loc->address);
6799 }
6800
6801 /* FIXME? Should this be moved up so that it is only called when
6802 there have been breakpoints? */
6803 annotate_breakpoints_table_end ();
6804
6805 return nr_printable_breakpoints;
6806 }
6807
6808 /* Display the value of default-collect in a way that is generally
6809 compatible with the breakpoint list. */
6810
6811 static void
6812 default_collect_info (void)
6813 {
6814 struct ui_out *uiout = current_uiout;
6815
6816 /* If it has no value (which is frequently the case), say nothing; a
6817 message like "No default-collect." gets in user's face when it's
6818 not wanted. */
6819 if (!*default_collect)
6820 return;
6821
6822 /* The following phrase lines up nicely with per-tracepoint collect
6823 actions. */
6824 ui_out_text (uiout, "default collect ");
6825 ui_out_field_string (uiout, "default-collect", default_collect);
6826 ui_out_text (uiout, " \n");
6827 }
6828
6829 static void
6830 breakpoints_info (char *args, int from_tty)
6831 {
6832 breakpoint_1 (args, 0, NULL);
6833
6834 default_collect_info ();
6835 }
6836
6837 static void
6838 watchpoints_info (char *args, int from_tty)
6839 {
6840 int num_printed = breakpoint_1 (args, 0, is_watchpoint);
6841 struct ui_out *uiout = current_uiout;
6842
6843 if (num_printed == 0)
6844 {
6845 if (args == NULL || *args == '\0')
6846 ui_out_message (uiout, 0, "No watchpoints.\n");
6847 else
6848 ui_out_message (uiout, 0, "No watchpoint matching '%s'.\n", args);
6849 }
6850 }
6851
6852 static void
6853 maintenance_info_breakpoints (char *args, int from_tty)
6854 {
6855 breakpoint_1 (args, 1, NULL);
6856
6857 default_collect_info ();
6858 }
6859
6860 static int
6861 breakpoint_has_pc (struct breakpoint *b,
6862 struct program_space *pspace,
6863 CORE_ADDR pc, struct obj_section *section)
6864 {
6865 struct bp_location *bl = b->loc;
6866
6867 for (; bl; bl = bl->next)
6868 {
6869 if (bl->pspace == pspace
6870 && bl->address == pc
6871 && (!overlay_debugging || bl->section == section))
6872 return 1;
6873 }
6874 return 0;
6875 }
6876
6877 /* Print a message describing any user-breakpoints set at PC. This
6878 concerns with logical breakpoints, so we match program spaces, not
6879 address spaces. */
6880
6881 static void
6882 describe_other_breakpoints (struct gdbarch *gdbarch,
6883 struct program_space *pspace, CORE_ADDR pc,
6884 struct obj_section *section, int thread)
6885 {
6886 int others = 0;
6887 struct breakpoint *b;
6888
6889 ALL_BREAKPOINTS (b)
6890 others += (user_breakpoint_p (b)
6891 && breakpoint_has_pc (b, pspace, pc, section));
6892 if (others > 0)
6893 {
6894 if (others == 1)
6895 printf_filtered (_("Note: breakpoint "));
6896 else /* if (others == ???) */
6897 printf_filtered (_("Note: breakpoints "));
6898 ALL_BREAKPOINTS (b)
6899 if (user_breakpoint_p (b) && breakpoint_has_pc (b, pspace, pc, section))
6900 {
6901 others--;
6902 printf_filtered ("%d", b->number);
6903 if (b->thread == -1 && thread != -1)
6904 printf_filtered (" (all threads)");
6905 else if (b->thread != -1)
6906 printf_filtered (" (thread %d)", b->thread);
6907 printf_filtered ("%s%s ",
6908 ((b->enable_state == bp_disabled
6909 || b->enable_state == bp_call_disabled)
6910 ? " (disabled)"
6911 : ""),
6912 (others > 1) ? ","
6913 : ((others == 1) ? " and" : ""));
6914 }
6915 printf_filtered (_("also set at pc "));
6916 fputs_filtered (paddress (gdbarch, pc), gdb_stdout);
6917 printf_filtered (".\n");
6918 }
6919 }
6920 \f
6921
6922 /* Return true iff it is meaningful to use the address member of
6923 BPT. For some breakpoint types, the address member is irrelevant
6924 and it makes no sense to attempt to compare it to other addresses
6925 (or use it for any other purpose either).
6926
6927 More specifically, each of the following breakpoint types will
6928 always have a zero valued address and we don't want to mark
6929 breakpoints of any of these types to be a duplicate of an actual
6930 breakpoint at address zero:
6931
6932 bp_watchpoint
6933 bp_catchpoint
6934
6935 */
6936
6937 static int
6938 breakpoint_address_is_meaningful (struct breakpoint *bpt)
6939 {
6940 enum bptype type = bpt->type;
6941
6942 return (type != bp_watchpoint && type != bp_catchpoint);
6943 }
6944
6945 /* Assuming LOC1 and LOC2's owners are hardware watchpoints, returns
6946 true if LOC1 and LOC2 represent the same watchpoint location. */
6947
6948 static int
6949 watchpoint_locations_match (struct bp_location *loc1,
6950 struct bp_location *loc2)
6951 {
6952 struct watchpoint *w1 = (struct watchpoint *) loc1->owner;
6953 struct watchpoint *w2 = (struct watchpoint *) loc2->owner;
6954
6955 /* Both of them must exist. */
6956 gdb_assert (w1 != NULL);
6957 gdb_assert (w2 != NULL);
6958
6959 /* If the target can evaluate the condition expression in hardware,
6960 then we we need to insert both watchpoints even if they are at
6961 the same place. Otherwise the watchpoint will only trigger when
6962 the condition of whichever watchpoint was inserted evaluates to
6963 true, not giving a chance for GDB to check the condition of the
6964 other watchpoint. */
6965 if ((w1->cond_exp
6966 && target_can_accel_watchpoint_condition (loc1->address,
6967 loc1->length,
6968 loc1->watchpoint_type,
6969 w1->cond_exp))
6970 || (w2->cond_exp
6971 && target_can_accel_watchpoint_condition (loc2->address,
6972 loc2->length,
6973 loc2->watchpoint_type,
6974 w2->cond_exp)))
6975 return 0;
6976
6977 /* Note that this checks the owner's type, not the location's. In
6978 case the target does not support read watchpoints, but does
6979 support access watchpoints, we'll have bp_read_watchpoint
6980 watchpoints with hw_access locations. Those should be considered
6981 duplicates of hw_read locations. The hw_read locations will
6982 become hw_access locations later. */
6983 return (loc1->owner->type == loc2->owner->type
6984 && loc1->pspace->aspace == loc2->pspace->aspace
6985 && loc1->address == loc2->address
6986 && loc1->length == loc2->length);
6987 }
6988
6989 /* See breakpoint.h. */
6990
6991 int
6992 breakpoint_address_match (struct address_space *aspace1, CORE_ADDR addr1,
6993 struct address_space *aspace2, CORE_ADDR addr2)
6994 {
6995 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
6996 || aspace1 == aspace2)
6997 && addr1 == addr2);
6998 }
6999
7000 /* Returns true if {ASPACE2,ADDR2} falls within the range determined by
7001 {ASPACE1,ADDR1,LEN1}. In most targets, this can only be true if ASPACE1
7002 matches ASPACE2. On targets that have global breakpoints, the address
7003 space doesn't really matter. */
7004
7005 static int
7006 breakpoint_address_match_range (struct address_space *aspace1, CORE_ADDR addr1,
7007 int len1, struct address_space *aspace2,
7008 CORE_ADDR addr2)
7009 {
7010 return ((gdbarch_has_global_breakpoints (target_gdbarch ())
7011 || aspace1 == aspace2)
7012 && addr2 >= addr1 && addr2 < addr1 + len1);
7013 }
7014
7015 /* Returns true if {ASPACE,ADDR} matches the breakpoint BL. BL may be
7016 a ranged breakpoint. In most targets, a match happens only if ASPACE
7017 matches the breakpoint's address space. On targets that have global
7018 breakpoints, the address space doesn't really matter. */
7019
7020 static int
7021 breakpoint_location_address_match (struct bp_location *bl,
7022 struct address_space *aspace,
7023 CORE_ADDR addr)
7024 {
7025 return (breakpoint_address_match (bl->pspace->aspace, bl->address,
7026 aspace, addr)
7027 || (bl->length
7028 && breakpoint_address_match_range (bl->pspace->aspace,
7029 bl->address, bl->length,
7030 aspace, addr)));
7031 }
7032
7033 /* If LOC1 and LOC2's owners are not tracepoints, returns false directly.
7034 Then, if LOC1 and LOC2 represent the same tracepoint location, returns
7035 true, otherwise returns false. */
7036
7037 static int
7038 tracepoint_locations_match (struct bp_location *loc1,
7039 struct bp_location *loc2)
7040 {
7041 if (is_tracepoint (loc1->owner) && is_tracepoint (loc2->owner))
7042 /* Since tracepoint locations are never duplicated with others', tracepoint
7043 locations at the same address of different tracepoints are regarded as
7044 different locations. */
7045 return (loc1->address == loc2->address && loc1->owner == loc2->owner);
7046 else
7047 return 0;
7048 }
7049
7050 /* Assuming LOC1 and LOC2's types' have meaningful target addresses
7051 (breakpoint_address_is_meaningful), returns true if LOC1 and LOC2
7052 represent the same location. */
7053
7054 static int
7055 breakpoint_locations_match (struct bp_location *loc1,
7056 struct bp_location *loc2)
7057 {
7058 int hw_point1, hw_point2;
7059
7060 /* Both of them must not be in moribund_locations. */
7061 gdb_assert (loc1->owner != NULL);
7062 gdb_assert (loc2->owner != NULL);
7063
7064 hw_point1 = is_hardware_watchpoint (loc1->owner);
7065 hw_point2 = is_hardware_watchpoint (loc2->owner);
7066
7067 if (hw_point1 != hw_point2)
7068 return 0;
7069 else if (hw_point1)
7070 return watchpoint_locations_match (loc1, loc2);
7071 else if (is_tracepoint (loc1->owner) || is_tracepoint (loc2->owner))
7072 return tracepoint_locations_match (loc1, loc2);
7073 else
7074 /* We compare bp_location.length in order to cover ranged breakpoints. */
7075 return (breakpoint_address_match (loc1->pspace->aspace, loc1->address,
7076 loc2->pspace->aspace, loc2->address)
7077 && loc1->length == loc2->length);
7078 }
7079
7080 static void
7081 breakpoint_adjustment_warning (CORE_ADDR from_addr, CORE_ADDR to_addr,
7082 int bnum, int have_bnum)
7083 {
7084 /* The longest string possibly returned by hex_string_custom
7085 is 50 chars. These must be at least that big for safety. */
7086 char astr1[64];
7087 char astr2[64];
7088
7089 strcpy (astr1, hex_string_custom ((unsigned long) from_addr, 8));
7090 strcpy (astr2, hex_string_custom ((unsigned long) to_addr, 8));
7091 if (have_bnum)
7092 warning (_("Breakpoint %d address previously adjusted from %s to %s."),
7093 bnum, astr1, astr2);
7094 else
7095 warning (_("Breakpoint address adjusted from %s to %s."), astr1, astr2);
7096 }
7097
7098 /* Adjust a breakpoint's address to account for architectural
7099 constraints on breakpoint placement. Return the adjusted address.
7100 Note: Very few targets require this kind of adjustment. For most
7101 targets, this function is simply the identity function. */
7102
7103 static CORE_ADDR
7104 adjust_breakpoint_address (struct gdbarch *gdbarch,
7105 CORE_ADDR bpaddr, enum bptype bptype)
7106 {
7107 if (!gdbarch_adjust_breakpoint_address_p (gdbarch))
7108 {
7109 /* Very few targets need any kind of breakpoint adjustment. */
7110 return bpaddr;
7111 }
7112 else if (bptype == bp_watchpoint
7113 || bptype == bp_hardware_watchpoint
7114 || bptype == bp_read_watchpoint
7115 || bptype == bp_access_watchpoint
7116 || bptype == bp_catchpoint)
7117 {
7118 /* Watchpoints and the various bp_catch_* eventpoints should not
7119 have their addresses modified. */
7120 return bpaddr;
7121 }
7122 else if (bptype == bp_single_step)
7123 {
7124 /* Single-step breakpoints should not have their addresses
7125 modified. If there's any architectural constrain that
7126 applies to this address, then it should have already been
7127 taken into account when the breakpoint was created in the
7128 first place. If we didn't do this, stepping through e.g.,
7129 Thumb-2 IT blocks would break. */
7130 return bpaddr;
7131 }
7132 else
7133 {
7134 CORE_ADDR adjusted_bpaddr;
7135
7136 /* Some targets have architectural constraints on the placement
7137 of breakpoint instructions. Obtain the adjusted address. */
7138 adjusted_bpaddr = gdbarch_adjust_breakpoint_address (gdbarch, bpaddr);
7139
7140 /* An adjusted breakpoint address can significantly alter
7141 a user's expectations. Print a warning if an adjustment
7142 is required. */
7143 if (adjusted_bpaddr != bpaddr)
7144 breakpoint_adjustment_warning (bpaddr, adjusted_bpaddr, 0, 0);
7145
7146 return adjusted_bpaddr;
7147 }
7148 }
7149
7150 void
7151 init_bp_location (struct bp_location *loc, const struct bp_location_ops *ops,
7152 struct breakpoint *owner)
7153 {
7154 memset (loc, 0, sizeof (*loc));
7155
7156 gdb_assert (ops != NULL);
7157
7158 loc->ops = ops;
7159 loc->owner = owner;
7160 loc->cond = NULL;
7161 loc->cond_bytecode = NULL;
7162 loc->shlib_disabled = 0;
7163 loc->enabled = 1;
7164
7165 switch (owner->type)
7166 {
7167 case bp_breakpoint:
7168 case bp_single_step:
7169 case bp_until:
7170 case bp_finish:
7171 case bp_longjmp:
7172 case bp_longjmp_resume:
7173 case bp_longjmp_call_dummy:
7174 case bp_exception:
7175 case bp_exception_resume:
7176 case bp_step_resume:
7177 case bp_hp_step_resume:
7178 case bp_watchpoint_scope:
7179 case bp_call_dummy:
7180 case bp_std_terminate:
7181 case bp_shlib_event:
7182 case bp_thread_event:
7183 case bp_overlay_event:
7184 case bp_jit_event:
7185 case bp_longjmp_master:
7186 case bp_std_terminate_master:
7187 case bp_exception_master:
7188 case bp_gnu_ifunc_resolver:
7189 case bp_gnu_ifunc_resolver_return:
7190 case bp_dprintf:
7191 loc->loc_type = bp_loc_software_breakpoint;
7192 mark_breakpoint_location_modified (loc);
7193 break;
7194 case bp_hardware_breakpoint:
7195 loc->loc_type = bp_loc_hardware_breakpoint;
7196 mark_breakpoint_location_modified (loc);
7197 break;
7198 case bp_hardware_watchpoint:
7199 case bp_read_watchpoint:
7200 case bp_access_watchpoint:
7201 loc->loc_type = bp_loc_hardware_watchpoint;
7202 break;
7203 case bp_watchpoint:
7204 case bp_catchpoint:
7205 case bp_tracepoint:
7206 case bp_fast_tracepoint:
7207 case bp_static_tracepoint:
7208 loc->loc_type = bp_loc_other;
7209 break;
7210 default:
7211 internal_error (__FILE__, __LINE__, _("unknown breakpoint type"));
7212 }
7213
7214 loc->refc = 1;
7215 }
7216
7217 /* Allocate a struct bp_location. */
7218
7219 static struct bp_location *
7220 allocate_bp_location (struct breakpoint *bpt)
7221 {
7222 return bpt->ops->allocate_location (bpt);
7223 }
7224
7225 static void
7226 free_bp_location (struct bp_location *loc)
7227 {
7228 loc->ops->dtor (loc);
7229 xfree (loc);
7230 }
7231
7232 /* Increment reference count. */
7233
7234 static void
7235 incref_bp_location (struct bp_location *bl)
7236 {
7237 ++bl->refc;
7238 }
7239
7240 /* Decrement reference count. If the reference count reaches 0,
7241 destroy the bp_location. Sets *BLP to NULL. */
7242
7243 static void
7244 decref_bp_location (struct bp_location **blp)
7245 {
7246 gdb_assert ((*blp)->refc > 0);
7247
7248 if (--(*blp)->refc == 0)
7249 free_bp_location (*blp);
7250 *blp = NULL;
7251 }
7252
7253 /* Add breakpoint B at the end of the global breakpoint chain. */
7254
7255 static void
7256 add_to_breakpoint_chain (struct breakpoint *b)
7257 {
7258 struct breakpoint *b1;
7259
7260 /* Add this breakpoint to the end of the chain so that a list of
7261 breakpoints will come out in order of increasing numbers. */
7262
7263 b1 = breakpoint_chain;
7264 if (b1 == 0)
7265 breakpoint_chain = b;
7266 else
7267 {
7268 while (b1->next)
7269 b1 = b1->next;
7270 b1->next = b;
7271 }
7272 }
7273
7274 /* Initializes breakpoint B with type BPTYPE and no locations yet. */
7275
7276 static void
7277 init_raw_breakpoint_without_location (struct breakpoint *b,
7278 struct gdbarch *gdbarch,
7279 enum bptype bptype,
7280 const struct breakpoint_ops *ops)
7281 {
7282 memset (b, 0, sizeof (*b));
7283
7284 gdb_assert (ops != NULL);
7285
7286 b->ops = ops;
7287 b->type = bptype;
7288 b->gdbarch = gdbarch;
7289 b->language = current_language->la_language;
7290 b->input_radix = input_radix;
7291 b->thread = -1;
7292 b->enable_state = bp_enabled;
7293 b->next = 0;
7294 b->silent = 0;
7295 b->ignore_count = 0;
7296 b->commands = NULL;
7297 b->frame_id = null_frame_id;
7298 b->condition_not_parsed = 0;
7299 b->py_bp_object = NULL;
7300 b->related_breakpoint = b;
7301 }
7302
7303 /* Helper to set_raw_breakpoint below. Creates a breakpoint
7304 that has type BPTYPE and has no locations as yet. */
7305
7306 static struct breakpoint *
7307 set_raw_breakpoint_without_location (struct gdbarch *gdbarch,
7308 enum bptype bptype,
7309 const struct breakpoint_ops *ops)
7310 {
7311 struct breakpoint *b = XNEW (struct breakpoint);
7312
7313 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7314 add_to_breakpoint_chain (b);
7315 return b;
7316 }
7317
7318 /* Initialize loc->function_name. EXPLICIT_LOC says no indirect function
7319 resolutions should be made as the user specified the location explicitly
7320 enough. */
7321
7322 static void
7323 set_breakpoint_location_function (struct bp_location *loc, int explicit_loc)
7324 {
7325 gdb_assert (loc->owner != NULL);
7326
7327 if (loc->owner->type == bp_breakpoint
7328 || loc->owner->type == bp_hardware_breakpoint
7329 || is_tracepoint (loc->owner))
7330 {
7331 int is_gnu_ifunc;
7332 const char *function_name;
7333 CORE_ADDR func_addr;
7334
7335 find_pc_partial_function_gnu_ifunc (loc->address, &function_name,
7336 &func_addr, NULL, &is_gnu_ifunc);
7337
7338 if (is_gnu_ifunc && !explicit_loc)
7339 {
7340 struct breakpoint *b = loc->owner;
7341
7342 gdb_assert (loc->pspace == current_program_space);
7343 if (gnu_ifunc_resolve_name (function_name,
7344 &loc->requested_address))
7345 {
7346 /* Recalculate ADDRESS based on new REQUESTED_ADDRESS. */
7347 loc->address = adjust_breakpoint_address (loc->gdbarch,
7348 loc->requested_address,
7349 b->type);
7350 }
7351 else if (b->type == bp_breakpoint && b->loc == loc
7352 && loc->next == NULL && b->related_breakpoint == b)
7353 {
7354 /* Create only the whole new breakpoint of this type but do not
7355 mess more complicated breakpoints with multiple locations. */
7356 b->type = bp_gnu_ifunc_resolver;
7357 /* Remember the resolver's address for use by the return
7358 breakpoint. */
7359 loc->related_address = func_addr;
7360 }
7361 }
7362
7363 if (function_name)
7364 loc->function_name = xstrdup (function_name);
7365 }
7366 }
7367
7368 /* Attempt to determine architecture of location identified by SAL. */
7369 struct gdbarch *
7370 get_sal_arch (struct symtab_and_line sal)
7371 {
7372 if (sal.section)
7373 return get_objfile_arch (sal.section->objfile);
7374 if (sal.symtab)
7375 return get_objfile_arch (SYMTAB_OBJFILE (sal.symtab));
7376
7377 return NULL;
7378 }
7379
7380 /* Low level routine for partially initializing a breakpoint of type
7381 BPTYPE. The newly created breakpoint's address, section, source
7382 file name, and line number are provided by SAL.
7383
7384 It is expected that the caller will complete the initialization of
7385 the newly created breakpoint struct as well as output any status
7386 information regarding the creation of a new breakpoint. */
7387
7388 static void
7389 init_raw_breakpoint (struct breakpoint *b, struct gdbarch *gdbarch,
7390 struct symtab_and_line sal, enum bptype bptype,
7391 const struct breakpoint_ops *ops)
7392 {
7393 init_raw_breakpoint_without_location (b, gdbarch, bptype, ops);
7394
7395 add_location_to_breakpoint (b, &sal);
7396
7397 if (bptype != bp_catchpoint)
7398 gdb_assert (sal.pspace != NULL);
7399
7400 /* Store the program space that was used to set the breakpoint,
7401 except for ordinary breakpoints, which are independent of the
7402 program space. */
7403 if (bptype != bp_breakpoint && bptype != bp_hardware_breakpoint)
7404 b->pspace = sal.pspace;
7405 }
7406
7407 /* set_raw_breakpoint is a low level routine for allocating and
7408 partially initializing a breakpoint of type BPTYPE. The newly
7409 created breakpoint's address, section, source file name, and line
7410 number are provided by SAL. The newly created and partially
7411 initialized breakpoint is added to the breakpoint chain and
7412 is also returned as the value of this function.
7413
7414 It is expected that the caller will complete the initialization of
7415 the newly created breakpoint struct as well as output any status
7416 information regarding the creation of a new breakpoint. In
7417 particular, set_raw_breakpoint does NOT set the breakpoint
7418 number! Care should be taken to not allow an error to occur
7419 prior to completing the initialization of the breakpoint. If this
7420 should happen, a bogus breakpoint will be left on the chain. */
7421
7422 struct breakpoint *
7423 set_raw_breakpoint (struct gdbarch *gdbarch,
7424 struct symtab_and_line sal, enum bptype bptype,
7425 const struct breakpoint_ops *ops)
7426 {
7427 struct breakpoint *b = XNEW (struct breakpoint);
7428
7429 init_raw_breakpoint (b, gdbarch, sal, bptype, ops);
7430 add_to_breakpoint_chain (b);
7431 return b;
7432 }
7433
7434
7435 /* Note that the breakpoint object B describes a permanent breakpoint
7436 instruction, hard-wired into the inferior's code. */
7437 void
7438 make_breakpoint_permanent (struct breakpoint *b)
7439 {
7440 struct bp_location *bl;
7441
7442 /* By definition, permanent breakpoints are already present in the
7443 code. Mark all locations as inserted. For now,
7444 make_breakpoint_permanent is called in just one place, so it's
7445 hard to say if it's reasonable to have permanent breakpoint with
7446 multiple locations or not, but it's easy to implement. */
7447 for (bl = b->loc; bl; bl = bl->next)
7448 {
7449 bl->permanent = 1;
7450 bl->inserted = 1;
7451 }
7452 }
7453
7454 /* Call this routine when stepping and nexting to enable a breakpoint
7455 if we do a longjmp() or 'throw' in TP. FRAME is the frame which
7456 initiated the operation. */
7457
7458 void
7459 set_longjmp_breakpoint (struct thread_info *tp, struct frame_id frame)
7460 {
7461 struct breakpoint *b, *b_tmp;
7462 int thread = tp->num;
7463
7464 /* To avoid having to rescan all objfile symbols at every step,
7465 we maintain a list of continually-inserted but always disabled
7466 longjmp "master" breakpoints. Here, we simply create momentary
7467 clones of those and enable them for the requested thread. */
7468 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7469 if (b->pspace == current_program_space
7470 && (b->type == bp_longjmp_master
7471 || b->type == bp_exception_master))
7472 {
7473 enum bptype type = b->type == bp_longjmp_master ? bp_longjmp : bp_exception;
7474 struct breakpoint *clone;
7475
7476 /* longjmp_breakpoint_ops ensures INITIATING_FRAME is cleared again
7477 after their removal. */
7478 clone = momentary_breakpoint_from_master (b, type,
7479 &longjmp_breakpoint_ops, 1);
7480 clone->thread = thread;
7481 }
7482
7483 tp->initiating_frame = frame;
7484 }
7485
7486 /* Delete all longjmp breakpoints from THREAD. */
7487 void
7488 delete_longjmp_breakpoint (int thread)
7489 {
7490 struct breakpoint *b, *b_tmp;
7491
7492 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7493 if (b->type == bp_longjmp || b->type == bp_exception)
7494 {
7495 if (b->thread == thread)
7496 delete_breakpoint (b);
7497 }
7498 }
7499
7500 void
7501 delete_longjmp_breakpoint_at_next_stop (int thread)
7502 {
7503 struct breakpoint *b, *b_tmp;
7504
7505 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7506 if (b->type == bp_longjmp || b->type == bp_exception)
7507 {
7508 if (b->thread == thread)
7509 b->disposition = disp_del_at_next_stop;
7510 }
7511 }
7512
7513 /* Place breakpoints of type bp_longjmp_call_dummy to catch longjmp for
7514 INFERIOR_PTID thread. Chain them all by RELATED_BREAKPOINT and return
7515 pointer to any of them. Return NULL if this system cannot place longjmp
7516 breakpoints. */
7517
7518 struct breakpoint *
7519 set_longjmp_breakpoint_for_call_dummy (void)
7520 {
7521 struct breakpoint *b, *retval = NULL;
7522
7523 ALL_BREAKPOINTS (b)
7524 if (b->pspace == current_program_space && b->type == bp_longjmp_master)
7525 {
7526 struct breakpoint *new_b;
7527
7528 new_b = momentary_breakpoint_from_master (b, bp_longjmp_call_dummy,
7529 &momentary_breakpoint_ops,
7530 1);
7531 new_b->thread = pid_to_thread_id (inferior_ptid);
7532
7533 /* Link NEW_B into the chain of RETVAL breakpoints. */
7534
7535 gdb_assert (new_b->related_breakpoint == new_b);
7536 if (retval == NULL)
7537 retval = new_b;
7538 new_b->related_breakpoint = retval;
7539 while (retval->related_breakpoint != new_b->related_breakpoint)
7540 retval = retval->related_breakpoint;
7541 retval->related_breakpoint = new_b;
7542 }
7543
7544 return retval;
7545 }
7546
7547 /* Verify all existing dummy frames and their associated breakpoints for
7548 TP. Remove those which can no longer be found in the current frame
7549 stack.
7550
7551 You should call this function only at places where it is safe to currently
7552 unwind the whole stack. Failed stack unwind would discard live dummy
7553 frames. */
7554
7555 void
7556 check_longjmp_breakpoint_for_call_dummy (struct thread_info *tp)
7557 {
7558 struct breakpoint *b, *b_tmp;
7559
7560 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7561 if (b->type == bp_longjmp_call_dummy && b->thread == tp->num)
7562 {
7563 struct breakpoint *dummy_b = b->related_breakpoint;
7564
7565 while (dummy_b != b && dummy_b->type != bp_call_dummy)
7566 dummy_b = dummy_b->related_breakpoint;
7567 if (dummy_b->type != bp_call_dummy
7568 || frame_find_by_id (dummy_b->frame_id) != NULL)
7569 continue;
7570
7571 dummy_frame_discard (dummy_b->frame_id, tp->ptid);
7572
7573 while (b->related_breakpoint != b)
7574 {
7575 if (b_tmp == b->related_breakpoint)
7576 b_tmp = b->related_breakpoint->next;
7577 delete_breakpoint (b->related_breakpoint);
7578 }
7579 delete_breakpoint (b);
7580 }
7581 }
7582
7583 void
7584 enable_overlay_breakpoints (void)
7585 {
7586 struct breakpoint *b;
7587
7588 ALL_BREAKPOINTS (b)
7589 if (b->type == bp_overlay_event)
7590 {
7591 b->enable_state = bp_enabled;
7592 update_global_location_list (UGLL_MAY_INSERT);
7593 overlay_events_enabled = 1;
7594 }
7595 }
7596
7597 void
7598 disable_overlay_breakpoints (void)
7599 {
7600 struct breakpoint *b;
7601
7602 ALL_BREAKPOINTS (b)
7603 if (b->type == bp_overlay_event)
7604 {
7605 b->enable_state = bp_disabled;
7606 update_global_location_list (UGLL_DONT_INSERT);
7607 overlay_events_enabled = 0;
7608 }
7609 }
7610
7611 /* Set an active std::terminate breakpoint for each std::terminate
7612 master breakpoint. */
7613 void
7614 set_std_terminate_breakpoint (void)
7615 {
7616 struct breakpoint *b, *b_tmp;
7617
7618 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7619 if (b->pspace == current_program_space
7620 && b->type == bp_std_terminate_master)
7621 {
7622 momentary_breakpoint_from_master (b, bp_std_terminate,
7623 &momentary_breakpoint_ops, 1);
7624 }
7625 }
7626
7627 /* Delete all the std::terminate breakpoints. */
7628 void
7629 delete_std_terminate_breakpoint (void)
7630 {
7631 struct breakpoint *b, *b_tmp;
7632
7633 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7634 if (b->type == bp_std_terminate)
7635 delete_breakpoint (b);
7636 }
7637
7638 struct breakpoint *
7639 create_thread_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7640 {
7641 struct breakpoint *b;
7642
7643 b = create_internal_breakpoint (gdbarch, address, bp_thread_event,
7644 &internal_breakpoint_ops);
7645
7646 b->enable_state = bp_enabled;
7647 /* addr_string has to be used or breakpoint_re_set will delete me. */
7648 b->addr_string
7649 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
7650
7651 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7652
7653 return b;
7654 }
7655
7656 void
7657 remove_thread_event_breakpoints (void)
7658 {
7659 struct breakpoint *b, *b_tmp;
7660
7661 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7662 if (b->type == bp_thread_event
7663 && b->loc->pspace == current_program_space)
7664 delete_breakpoint (b);
7665 }
7666
7667 struct lang_and_radix
7668 {
7669 enum language lang;
7670 int radix;
7671 };
7672
7673 /* Create a breakpoint for JIT code registration and unregistration. */
7674
7675 struct breakpoint *
7676 create_jit_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7677 {
7678 struct breakpoint *b;
7679
7680 b = create_internal_breakpoint (gdbarch, address, bp_jit_event,
7681 &internal_breakpoint_ops);
7682 update_global_location_list_nothrow (UGLL_MAY_INSERT);
7683 return b;
7684 }
7685
7686 /* Remove JIT code registration and unregistration breakpoint(s). */
7687
7688 void
7689 remove_jit_event_breakpoints (void)
7690 {
7691 struct breakpoint *b, *b_tmp;
7692
7693 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7694 if (b->type == bp_jit_event
7695 && b->loc->pspace == current_program_space)
7696 delete_breakpoint (b);
7697 }
7698
7699 void
7700 remove_solib_event_breakpoints (void)
7701 {
7702 struct breakpoint *b, *b_tmp;
7703
7704 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7705 if (b->type == bp_shlib_event
7706 && b->loc->pspace == current_program_space)
7707 delete_breakpoint (b);
7708 }
7709
7710 /* See breakpoint.h. */
7711
7712 void
7713 remove_solib_event_breakpoints_at_next_stop (void)
7714 {
7715 struct breakpoint *b, *b_tmp;
7716
7717 ALL_BREAKPOINTS_SAFE (b, b_tmp)
7718 if (b->type == bp_shlib_event
7719 && b->loc->pspace == current_program_space)
7720 b->disposition = disp_del_at_next_stop;
7721 }
7722
7723 /* Helper for create_solib_event_breakpoint /
7724 create_and_insert_solib_event_breakpoint. Allows specifying which
7725 INSERT_MODE to pass through to update_global_location_list. */
7726
7727 static struct breakpoint *
7728 create_solib_event_breakpoint_1 (struct gdbarch *gdbarch, CORE_ADDR address,
7729 enum ugll_insert_mode insert_mode)
7730 {
7731 struct breakpoint *b;
7732
7733 b = create_internal_breakpoint (gdbarch, address, bp_shlib_event,
7734 &internal_breakpoint_ops);
7735 update_global_location_list_nothrow (insert_mode);
7736 return b;
7737 }
7738
7739 struct breakpoint *
7740 create_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7741 {
7742 return create_solib_event_breakpoint_1 (gdbarch, address, UGLL_MAY_INSERT);
7743 }
7744
7745 /* See breakpoint.h. */
7746
7747 struct breakpoint *
7748 create_and_insert_solib_event_breakpoint (struct gdbarch *gdbarch, CORE_ADDR address)
7749 {
7750 struct breakpoint *b;
7751
7752 /* Explicitly tell update_global_location_list to insert
7753 locations. */
7754 b = create_solib_event_breakpoint_1 (gdbarch, address, UGLL_INSERT);
7755 if (!b->loc->inserted)
7756 {
7757 delete_breakpoint (b);
7758 return NULL;
7759 }
7760 return b;
7761 }
7762
7763 /* Disable any breakpoints that are on code in shared libraries. Only
7764 apply to enabled breakpoints, disabled ones can just stay disabled. */
7765
7766 void
7767 disable_breakpoints_in_shlibs (void)
7768 {
7769 struct bp_location *loc, **locp_tmp;
7770
7771 ALL_BP_LOCATIONS (loc, locp_tmp)
7772 {
7773 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7774 struct breakpoint *b = loc->owner;
7775
7776 /* We apply the check to all breakpoints, including disabled for
7777 those with loc->duplicate set. This is so that when breakpoint
7778 becomes enabled, or the duplicate is removed, gdb will try to
7779 insert all breakpoints. If we don't set shlib_disabled here,
7780 we'll try to insert those breakpoints and fail. */
7781 if (((b->type == bp_breakpoint)
7782 || (b->type == bp_jit_event)
7783 || (b->type == bp_hardware_breakpoint)
7784 || (is_tracepoint (b)))
7785 && loc->pspace == current_program_space
7786 && !loc->shlib_disabled
7787 && solib_name_from_address (loc->pspace, loc->address)
7788 )
7789 {
7790 loc->shlib_disabled = 1;
7791 }
7792 }
7793 }
7794
7795 /* Disable any breakpoints and tracepoints that are in SOLIB upon
7796 notification of unloaded_shlib. Only apply to enabled breakpoints,
7797 disabled ones can just stay disabled. */
7798
7799 static void
7800 disable_breakpoints_in_unloaded_shlib (struct so_list *solib)
7801 {
7802 struct bp_location *loc, **locp_tmp;
7803 int disabled_shlib_breaks = 0;
7804
7805 /* SunOS a.out shared libraries are always mapped, so do not
7806 disable breakpoints; they will only be reported as unloaded
7807 through clear_solib when GDB discards its shared library
7808 list. See clear_solib for more information. */
7809 if (exec_bfd != NULL
7810 && bfd_get_flavour (exec_bfd) == bfd_target_aout_flavour)
7811 return;
7812
7813 ALL_BP_LOCATIONS (loc, locp_tmp)
7814 {
7815 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always non-NULL. */
7816 struct breakpoint *b = loc->owner;
7817
7818 if (solib->pspace == loc->pspace
7819 && !loc->shlib_disabled
7820 && (((b->type == bp_breakpoint
7821 || b->type == bp_jit_event
7822 || b->type == bp_hardware_breakpoint)
7823 && (loc->loc_type == bp_loc_hardware_breakpoint
7824 || loc->loc_type == bp_loc_software_breakpoint))
7825 || is_tracepoint (b))
7826 && solib_contains_address_p (solib, loc->address))
7827 {
7828 loc->shlib_disabled = 1;
7829 /* At this point, we cannot rely on remove_breakpoint
7830 succeeding so we must mark the breakpoint as not inserted
7831 to prevent future errors occurring in remove_breakpoints. */
7832 loc->inserted = 0;
7833
7834 /* This may cause duplicate notifications for the same breakpoint. */
7835 observer_notify_breakpoint_modified (b);
7836
7837 if (!disabled_shlib_breaks)
7838 {
7839 target_terminal_ours_for_output ();
7840 warning (_("Temporarily disabling breakpoints "
7841 "for unloaded shared library \"%s\""),
7842 solib->so_name);
7843 }
7844 disabled_shlib_breaks = 1;
7845 }
7846 }
7847 }
7848
7849 /* Disable any breakpoints and tracepoints in OBJFILE upon
7850 notification of free_objfile. Only apply to enabled breakpoints,
7851 disabled ones can just stay disabled. */
7852
7853 static void
7854 disable_breakpoints_in_freed_objfile (struct objfile *objfile)
7855 {
7856 struct breakpoint *b;
7857
7858 if (objfile == NULL)
7859 return;
7860
7861 /* OBJF_SHARED|OBJF_USERLOADED objfiles are dynamic modules manually
7862 managed by the user with add-symbol-file/remove-symbol-file.
7863 Similarly to how breakpoints in shared libraries are handled in
7864 response to "nosharedlibrary", mark breakpoints in such modules
7865 shlib_disabled so they end up uninserted on the next global
7866 location list update. Shared libraries not loaded by the user
7867 aren't handled here -- they're already handled in
7868 disable_breakpoints_in_unloaded_shlib, called by solib.c's
7869 solib_unloaded observer. We skip objfiles that are not
7870 OBJF_SHARED as those aren't considered dynamic objects (e.g. the
7871 main objfile). */
7872 if ((objfile->flags & OBJF_SHARED) == 0
7873 || (objfile->flags & OBJF_USERLOADED) == 0)
7874 return;
7875
7876 ALL_BREAKPOINTS (b)
7877 {
7878 struct bp_location *loc;
7879 int bp_modified = 0;
7880
7881 if (!is_breakpoint (b) && !is_tracepoint (b))
7882 continue;
7883
7884 for (loc = b->loc; loc != NULL; loc = loc->next)
7885 {
7886 CORE_ADDR loc_addr = loc->address;
7887
7888 if (loc->loc_type != bp_loc_hardware_breakpoint
7889 && loc->loc_type != bp_loc_software_breakpoint)
7890 continue;
7891
7892 if (loc->shlib_disabled != 0)
7893 continue;
7894
7895 if (objfile->pspace != loc->pspace)
7896 continue;
7897
7898 if (loc->loc_type != bp_loc_hardware_breakpoint
7899 && loc->loc_type != bp_loc_software_breakpoint)
7900 continue;
7901
7902 if (is_addr_in_objfile (loc_addr, objfile))
7903 {
7904 loc->shlib_disabled = 1;
7905 /* At this point, we don't know whether the object was
7906 unmapped from the inferior or not, so leave the
7907 inserted flag alone. We'll handle failure to
7908 uninsert quietly, in case the object was indeed
7909 unmapped. */
7910
7911 mark_breakpoint_location_modified (loc);
7912
7913 bp_modified = 1;
7914 }
7915 }
7916
7917 if (bp_modified)
7918 observer_notify_breakpoint_modified (b);
7919 }
7920 }
7921
7922 /* FORK & VFORK catchpoints. */
7923
7924 /* An instance of this type is used to represent a fork or vfork
7925 catchpoint. It includes a "struct breakpoint" as a kind of base
7926 class; users downcast to "struct breakpoint *" when needed. A
7927 breakpoint is really of this type iff its ops pointer points to
7928 CATCH_FORK_BREAKPOINT_OPS. */
7929
7930 struct fork_catchpoint
7931 {
7932 /* The base class. */
7933 struct breakpoint base;
7934
7935 /* Process id of a child process whose forking triggered this
7936 catchpoint. This field is only valid immediately after this
7937 catchpoint has triggered. */
7938 ptid_t forked_inferior_pid;
7939 };
7940
7941 /* Implement the "insert" breakpoint_ops method for fork
7942 catchpoints. */
7943
7944 static int
7945 insert_catch_fork (struct bp_location *bl)
7946 {
7947 return target_insert_fork_catchpoint (ptid_get_pid (inferior_ptid));
7948 }
7949
7950 /* Implement the "remove" breakpoint_ops method for fork
7951 catchpoints. */
7952
7953 static int
7954 remove_catch_fork (struct bp_location *bl)
7955 {
7956 return target_remove_fork_catchpoint (ptid_get_pid (inferior_ptid));
7957 }
7958
7959 /* Implement the "breakpoint_hit" breakpoint_ops method for fork
7960 catchpoints. */
7961
7962 static int
7963 breakpoint_hit_catch_fork (const struct bp_location *bl,
7964 struct address_space *aspace, CORE_ADDR bp_addr,
7965 const struct target_waitstatus *ws)
7966 {
7967 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
7968
7969 if (ws->kind != TARGET_WAITKIND_FORKED)
7970 return 0;
7971
7972 c->forked_inferior_pid = ws->value.related_pid;
7973 return 1;
7974 }
7975
7976 /* Implement the "print_it" breakpoint_ops method for fork
7977 catchpoints. */
7978
7979 static enum print_stop_action
7980 print_it_catch_fork (bpstat bs)
7981 {
7982 struct ui_out *uiout = current_uiout;
7983 struct breakpoint *b = bs->breakpoint_at;
7984 struct fork_catchpoint *c = (struct fork_catchpoint *) bs->breakpoint_at;
7985
7986 annotate_catchpoint (b->number);
7987 if (b->disposition == disp_del)
7988 ui_out_text (uiout, "\nTemporary catchpoint ");
7989 else
7990 ui_out_text (uiout, "\nCatchpoint ");
7991 if (ui_out_is_mi_like_p (uiout))
7992 {
7993 ui_out_field_string (uiout, "reason",
7994 async_reason_lookup (EXEC_ASYNC_FORK));
7995 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
7996 }
7997 ui_out_field_int (uiout, "bkptno", b->number);
7998 ui_out_text (uiout, " (forked process ");
7999 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
8000 ui_out_text (uiout, "), ");
8001 return PRINT_SRC_AND_LOC;
8002 }
8003
8004 /* Implement the "print_one" breakpoint_ops method for fork
8005 catchpoints. */
8006
8007 static void
8008 print_one_catch_fork (struct breakpoint *b, struct bp_location **last_loc)
8009 {
8010 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8011 struct value_print_options opts;
8012 struct ui_out *uiout = current_uiout;
8013
8014 get_user_print_options (&opts);
8015
8016 /* Field 4, the address, is omitted (which makes the columns not
8017 line up too nicely with the headers, but the effect is relatively
8018 readable). */
8019 if (opts.addressprint)
8020 ui_out_field_skip (uiout, "addr");
8021 annotate_field (5);
8022 ui_out_text (uiout, "fork");
8023 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
8024 {
8025 ui_out_text (uiout, ", process ");
8026 ui_out_field_int (uiout, "what",
8027 ptid_get_pid (c->forked_inferior_pid));
8028 ui_out_spaces (uiout, 1);
8029 }
8030
8031 if (ui_out_is_mi_like_p (uiout))
8032 ui_out_field_string (uiout, "catch-type", "fork");
8033 }
8034
8035 /* Implement the "print_mention" breakpoint_ops method for fork
8036 catchpoints. */
8037
8038 static void
8039 print_mention_catch_fork (struct breakpoint *b)
8040 {
8041 printf_filtered (_("Catchpoint %d (fork)"), b->number);
8042 }
8043
8044 /* Implement the "print_recreate" breakpoint_ops method for fork
8045 catchpoints. */
8046
8047 static void
8048 print_recreate_catch_fork (struct breakpoint *b, struct ui_file *fp)
8049 {
8050 fprintf_unfiltered (fp, "catch fork");
8051 print_recreate_thread (b, fp);
8052 }
8053
8054 /* The breakpoint_ops structure to be used in fork catchpoints. */
8055
8056 static struct breakpoint_ops catch_fork_breakpoint_ops;
8057
8058 /* Implement the "insert" breakpoint_ops method for vfork
8059 catchpoints. */
8060
8061 static int
8062 insert_catch_vfork (struct bp_location *bl)
8063 {
8064 return target_insert_vfork_catchpoint (ptid_get_pid (inferior_ptid));
8065 }
8066
8067 /* Implement the "remove" breakpoint_ops method for vfork
8068 catchpoints. */
8069
8070 static int
8071 remove_catch_vfork (struct bp_location *bl)
8072 {
8073 return target_remove_vfork_catchpoint (ptid_get_pid (inferior_ptid));
8074 }
8075
8076 /* Implement the "breakpoint_hit" breakpoint_ops method for vfork
8077 catchpoints. */
8078
8079 static int
8080 breakpoint_hit_catch_vfork (const struct bp_location *bl,
8081 struct address_space *aspace, CORE_ADDR bp_addr,
8082 const struct target_waitstatus *ws)
8083 {
8084 struct fork_catchpoint *c = (struct fork_catchpoint *) bl->owner;
8085
8086 if (ws->kind != TARGET_WAITKIND_VFORKED)
8087 return 0;
8088
8089 c->forked_inferior_pid = ws->value.related_pid;
8090 return 1;
8091 }
8092
8093 /* Implement the "print_it" breakpoint_ops method for vfork
8094 catchpoints. */
8095
8096 static enum print_stop_action
8097 print_it_catch_vfork (bpstat bs)
8098 {
8099 struct ui_out *uiout = current_uiout;
8100 struct breakpoint *b = bs->breakpoint_at;
8101 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8102
8103 annotate_catchpoint (b->number);
8104 if (b->disposition == disp_del)
8105 ui_out_text (uiout, "\nTemporary catchpoint ");
8106 else
8107 ui_out_text (uiout, "\nCatchpoint ");
8108 if (ui_out_is_mi_like_p (uiout))
8109 {
8110 ui_out_field_string (uiout, "reason",
8111 async_reason_lookup (EXEC_ASYNC_VFORK));
8112 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8113 }
8114 ui_out_field_int (uiout, "bkptno", b->number);
8115 ui_out_text (uiout, " (vforked process ");
8116 ui_out_field_int (uiout, "newpid", ptid_get_pid (c->forked_inferior_pid));
8117 ui_out_text (uiout, "), ");
8118 return PRINT_SRC_AND_LOC;
8119 }
8120
8121 /* Implement the "print_one" breakpoint_ops method for vfork
8122 catchpoints. */
8123
8124 static void
8125 print_one_catch_vfork (struct breakpoint *b, struct bp_location **last_loc)
8126 {
8127 struct fork_catchpoint *c = (struct fork_catchpoint *) b;
8128 struct value_print_options opts;
8129 struct ui_out *uiout = current_uiout;
8130
8131 get_user_print_options (&opts);
8132 /* Field 4, the address, is omitted (which makes the columns not
8133 line up too nicely with the headers, but the effect is relatively
8134 readable). */
8135 if (opts.addressprint)
8136 ui_out_field_skip (uiout, "addr");
8137 annotate_field (5);
8138 ui_out_text (uiout, "vfork");
8139 if (!ptid_equal (c->forked_inferior_pid, null_ptid))
8140 {
8141 ui_out_text (uiout, ", process ");
8142 ui_out_field_int (uiout, "what",
8143 ptid_get_pid (c->forked_inferior_pid));
8144 ui_out_spaces (uiout, 1);
8145 }
8146
8147 if (ui_out_is_mi_like_p (uiout))
8148 ui_out_field_string (uiout, "catch-type", "vfork");
8149 }
8150
8151 /* Implement the "print_mention" breakpoint_ops method for vfork
8152 catchpoints. */
8153
8154 static void
8155 print_mention_catch_vfork (struct breakpoint *b)
8156 {
8157 printf_filtered (_("Catchpoint %d (vfork)"), b->number);
8158 }
8159
8160 /* Implement the "print_recreate" breakpoint_ops method for vfork
8161 catchpoints. */
8162
8163 static void
8164 print_recreate_catch_vfork (struct breakpoint *b, struct ui_file *fp)
8165 {
8166 fprintf_unfiltered (fp, "catch vfork");
8167 print_recreate_thread (b, fp);
8168 }
8169
8170 /* The breakpoint_ops structure to be used in vfork catchpoints. */
8171
8172 static struct breakpoint_ops catch_vfork_breakpoint_ops;
8173
8174 /* An instance of this type is used to represent an solib catchpoint.
8175 It includes a "struct breakpoint" as a kind of base class; users
8176 downcast to "struct breakpoint *" when needed. A breakpoint is
8177 really of this type iff its ops pointer points to
8178 CATCH_SOLIB_BREAKPOINT_OPS. */
8179
8180 struct solib_catchpoint
8181 {
8182 /* The base class. */
8183 struct breakpoint base;
8184
8185 /* True for "catch load", false for "catch unload". */
8186 unsigned char is_load;
8187
8188 /* Regular expression to match, if any. COMPILED is only valid when
8189 REGEX is non-NULL. */
8190 char *regex;
8191 regex_t compiled;
8192 };
8193
8194 static void
8195 dtor_catch_solib (struct breakpoint *b)
8196 {
8197 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8198
8199 if (self->regex)
8200 regfree (&self->compiled);
8201 xfree (self->regex);
8202
8203 base_breakpoint_ops.dtor (b);
8204 }
8205
8206 static int
8207 insert_catch_solib (struct bp_location *ignore)
8208 {
8209 return 0;
8210 }
8211
8212 static int
8213 remove_catch_solib (struct bp_location *ignore)
8214 {
8215 return 0;
8216 }
8217
8218 static int
8219 breakpoint_hit_catch_solib (const struct bp_location *bl,
8220 struct address_space *aspace,
8221 CORE_ADDR bp_addr,
8222 const struct target_waitstatus *ws)
8223 {
8224 struct solib_catchpoint *self = (struct solib_catchpoint *) bl->owner;
8225 struct breakpoint *other;
8226
8227 if (ws->kind == TARGET_WAITKIND_LOADED)
8228 return 1;
8229
8230 ALL_BREAKPOINTS (other)
8231 {
8232 struct bp_location *other_bl;
8233
8234 if (other == bl->owner)
8235 continue;
8236
8237 if (other->type != bp_shlib_event)
8238 continue;
8239
8240 if (self->base.pspace != NULL && other->pspace != self->base.pspace)
8241 continue;
8242
8243 for (other_bl = other->loc; other_bl != NULL; other_bl = other_bl->next)
8244 {
8245 if (other->ops->breakpoint_hit (other_bl, aspace, bp_addr, ws))
8246 return 1;
8247 }
8248 }
8249
8250 return 0;
8251 }
8252
8253 static void
8254 check_status_catch_solib (struct bpstats *bs)
8255 {
8256 struct solib_catchpoint *self
8257 = (struct solib_catchpoint *) bs->breakpoint_at;
8258 int ix;
8259
8260 if (self->is_load)
8261 {
8262 struct so_list *iter;
8263
8264 for (ix = 0;
8265 VEC_iterate (so_list_ptr, current_program_space->added_solibs,
8266 ix, iter);
8267 ++ix)
8268 {
8269 if (!self->regex
8270 || regexec (&self->compiled, iter->so_name, 0, NULL, 0) == 0)
8271 return;
8272 }
8273 }
8274 else
8275 {
8276 char *iter;
8277
8278 for (ix = 0;
8279 VEC_iterate (char_ptr, current_program_space->deleted_solibs,
8280 ix, iter);
8281 ++ix)
8282 {
8283 if (!self->regex
8284 || regexec (&self->compiled, iter, 0, NULL, 0) == 0)
8285 return;
8286 }
8287 }
8288
8289 bs->stop = 0;
8290 bs->print_it = print_it_noop;
8291 }
8292
8293 static enum print_stop_action
8294 print_it_catch_solib (bpstat bs)
8295 {
8296 struct breakpoint *b = bs->breakpoint_at;
8297 struct ui_out *uiout = current_uiout;
8298
8299 annotate_catchpoint (b->number);
8300 if (b->disposition == disp_del)
8301 ui_out_text (uiout, "\nTemporary catchpoint ");
8302 else
8303 ui_out_text (uiout, "\nCatchpoint ");
8304 ui_out_field_int (uiout, "bkptno", b->number);
8305 ui_out_text (uiout, "\n");
8306 if (ui_out_is_mi_like_p (uiout))
8307 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8308 print_solib_event (1);
8309 return PRINT_SRC_AND_LOC;
8310 }
8311
8312 static void
8313 print_one_catch_solib (struct breakpoint *b, struct bp_location **locs)
8314 {
8315 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8316 struct value_print_options opts;
8317 struct ui_out *uiout = current_uiout;
8318 char *msg;
8319
8320 get_user_print_options (&opts);
8321 /* Field 4, the address, is omitted (which makes the columns not
8322 line up too nicely with the headers, but the effect is relatively
8323 readable). */
8324 if (opts.addressprint)
8325 {
8326 annotate_field (4);
8327 ui_out_field_skip (uiout, "addr");
8328 }
8329
8330 annotate_field (5);
8331 if (self->is_load)
8332 {
8333 if (self->regex)
8334 msg = xstrprintf (_("load of library matching %s"), self->regex);
8335 else
8336 msg = xstrdup (_("load of library"));
8337 }
8338 else
8339 {
8340 if (self->regex)
8341 msg = xstrprintf (_("unload of library matching %s"), self->regex);
8342 else
8343 msg = xstrdup (_("unload of library"));
8344 }
8345 ui_out_field_string (uiout, "what", msg);
8346 xfree (msg);
8347
8348 if (ui_out_is_mi_like_p (uiout))
8349 ui_out_field_string (uiout, "catch-type",
8350 self->is_load ? "load" : "unload");
8351 }
8352
8353 static void
8354 print_mention_catch_solib (struct breakpoint *b)
8355 {
8356 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8357
8358 printf_filtered (_("Catchpoint %d (%s)"), b->number,
8359 self->is_load ? "load" : "unload");
8360 }
8361
8362 static void
8363 print_recreate_catch_solib (struct breakpoint *b, struct ui_file *fp)
8364 {
8365 struct solib_catchpoint *self = (struct solib_catchpoint *) b;
8366
8367 fprintf_unfiltered (fp, "%s %s",
8368 b->disposition == disp_del ? "tcatch" : "catch",
8369 self->is_load ? "load" : "unload");
8370 if (self->regex)
8371 fprintf_unfiltered (fp, " %s", self->regex);
8372 fprintf_unfiltered (fp, "\n");
8373 }
8374
8375 static struct breakpoint_ops catch_solib_breakpoint_ops;
8376
8377 /* Shared helper function (MI and CLI) for creating and installing
8378 a shared object event catchpoint. If IS_LOAD is non-zero then
8379 the events to be caught are load events, otherwise they are
8380 unload events. If IS_TEMP is non-zero the catchpoint is a
8381 temporary one. If ENABLED is non-zero the catchpoint is
8382 created in an enabled state. */
8383
8384 void
8385 add_solib_catchpoint (char *arg, int is_load, int is_temp, int enabled)
8386 {
8387 struct solib_catchpoint *c;
8388 struct gdbarch *gdbarch = get_current_arch ();
8389 struct cleanup *cleanup;
8390
8391 if (!arg)
8392 arg = "";
8393 arg = skip_spaces (arg);
8394
8395 c = XCNEW (struct solib_catchpoint);
8396 cleanup = make_cleanup (xfree, c);
8397
8398 if (*arg != '\0')
8399 {
8400 int errcode;
8401
8402 errcode = regcomp (&c->compiled, arg, REG_NOSUB);
8403 if (errcode != 0)
8404 {
8405 char *err = get_regcomp_error (errcode, &c->compiled);
8406
8407 make_cleanup (xfree, err);
8408 error (_("Invalid regexp (%s): %s"), err, arg);
8409 }
8410 c->regex = xstrdup (arg);
8411 }
8412
8413 c->is_load = is_load;
8414 init_catchpoint (&c->base, gdbarch, is_temp, NULL,
8415 &catch_solib_breakpoint_ops);
8416
8417 c->base.enable_state = enabled ? bp_enabled : bp_disabled;
8418
8419 discard_cleanups (cleanup);
8420 install_breakpoint (0, &c->base, 1);
8421 }
8422
8423 /* A helper function that does all the work for "catch load" and
8424 "catch unload". */
8425
8426 static void
8427 catch_load_or_unload (char *arg, int from_tty, int is_load,
8428 struct cmd_list_element *command)
8429 {
8430 int tempflag;
8431 const int enabled = 1;
8432
8433 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
8434
8435 add_solib_catchpoint (arg, is_load, tempflag, enabled);
8436 }
8437
8438 static void
8439 catch_load_command_1 (char *arg, int from_tty,
8440 struct cmd_list_element *command)
8441 {
8442 catch_load_or_unload (arg, from_tty, 1, command);
8443 }
8444
8445 static void
8446 catch_unload_command_1 (char *arg, int from_tty,
8447 struct cmd_list_element *command)
8448 {
8449 catch_load_or_unload (arg, from_tty, 0, command);
8450 }
8451
8452 /* Initialize a new breakpoint of the bp_catchpoint kind. If TEMPFLAG
8453 is non-zero, then make the breakpoint temporary. If COND_STRING is
8454 not NULL, then store it in the breakpoint. OPS, if not NULL, is
8455 the breakpoint_ops structure associated to the catchpoint. */
8456
8457 void
8458 init_catchpoint (struct breakpoint *b,
8459 struct gdbarch *gdbarch, int tempflag,
8460 char *cond_string,
8461 const struct breakpoint_ops *ops)
8462 {
8463 struct symtab_and_line sal;
8464
8465 init_sal (&sal);
8466 sal.pspace = current_program_space;
8467
8468 init_raw_breakpoint (b, gdbarch, sal, bp_catchpoint, ops);
8469
8470 b->cond_string = (cond_string == NULL) ? NULL : xstrdup (cond_string);
8471 b->disposition = tempflag ? disp_del : disp_donttouch;
8472 }
8473
8474 void
8475 install_breakpoint (int internal, struct breakpoint *b, int update_gll)
8476 {
8477 add_to_breakpoint_chain (b);
8478 set_breakpoint_number (internal, b);
8479 if (is_tracepoint (b))
8480 set_tracepoint_count (breakpoint_count);
8481 if (!internal)
8482 mention (b);
8483 observer_notify_breakpoint_created (b);
8484
8485 if (update_gll)
8486 update_global_location_list (UGLL_MAY_INSERT);
8487 }
8488
8489 static void
8490 create_fork_vfork_event_catchpoint (struct gdbarch *gdbarch,
8491 int tempflag, char *cond_string,
8492 const struct breakpoint_ops *ops)
8493 {
8494 struct fork_catchpoint *c = XNEW (struct fork_catchpoint);
8495
8496 init_catchpoint (&c->base, gdbarch, tempflag, cond_string, ops);
8497
8498 c->forked_inferior_pid = null_ptid;
8499
8500 install_breakpoint (0, &c->base, 1);
8501 }
8502
8503 /* Exec catchpoints. */
8504
8505 /* An instance of this type is used to represent an exec catchpoint.
8506 It includes a "struct breakpoint" as a kind of base class; users
8507 downcast to "struct breakpoint *" when needed. A breakpoint is
8508 really of this type iff its ops pointer points to
8509 CATCH_EXEC_BREAKPOINT_OPS. */
8510
8511 struct exec_catchpoint
8512 {
8513 /* The base class. */
8514 struct breakpoint base;
8515
8516 /* Filename of a program whose exec triggered this catchpoint.
8517 This field is only valid immediately after this catchpoint has
8518 triggered. */
8519 char *exec_pathname;
8520 };
8521
8522 /* Implement the "dtor" breakpoint_ops method for exec
8523 catchpoints. */
8524
8525 static void
8526 dtor_catch_exec (struct breakpoint *b)
8527 {
8528 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8529
8530 xfree (c->exec_pathname);
8531
8532 base_breakpoint_ops.dtor (b);
8533 }
8534
8535 static int
8536 insert_catch_exec (struct bp_location *bl)
8537 {
8538 return target_insert_exec_catchpoint (ptid_get_pid (inferior_ptid));
8539 }
8540
8541 static int
8542 remove_catch_exec (struct bp_location *bl)
8543 {
8544 return target_remove_exec_catchpoint (ptid_get_pid (inferior_ptid));
8545 }
8546
8547 static int
8548 breakpoint_hit_catch_exec (const struct bp_location *bl,
8549 struct address_space *aspace, CORE_ADDR bp_addr,
8550 const struct target_waitstatus *ws)
8551 {
8552 struct exec_catchpoint *c = (struct exec_catchpoint *) bl->owner;
8553
8554 if (ws->kind != TARGET_WAITKIND_EXECD)
8555 return 0;
8556
8557 c->exec_pathname = xstrdup (ws->value.execd_pathname);
8558 return 1;
8559 }
8560
8561 static enum print_stop_action
8562 print_it_catch_exec (bpstat bs)
8563 {
8564 struct ui_out *uiout = current_uiout;
8565 struct breakpoint *b = bs->breakpoint_at;
8566 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8567
8568 annotate_catchpoint (b->number);
8569 if (b->disposition == disp_del)
8570 ui_out_text (uiout, "\nTemporary catchpoint ");
8571 else
8572 ui_out_text (uiout, "\nCatchpoint ");
8573 if (ui_out_is_mi_like_p (uiout))
8574 {
8575 ui_out_field_string (uiout, "reason",
8576 async_reason_lookup (EXEC_ASYNC_EXEC));
8577 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
8578 }
8579 ui_out_field_int (uiout, "bkptno", b->number);
8580 ui_out_text (uiout, " (exec'd ");
8581 ui_out_field_string (uiout, "new-exec", c->exec_pathname);
8582 ui_out_text (uiout, "), ");
8583
8584 return PRINT_SRC_AND_LOC;
8585 }
8586
8587 static void
8588 print_one_catch_exec (struct breakpoint *b, struct bp_location **last_loc)
8589 {
8590 struct exec_catchpoint *c = (struct exec_catchpoint *) b;
8591 struct value_print_options opts;
8592 struct ui_out *uiout = current_uiout;
8593
8594 get_user_print_options (&opts);
8595
8596 /* Field 4, the address, is omitted (which makes the columns
8597 not line up too nicely with the headers, but the effect
8598 is relatively readable). */
8599 if (opts.addressprint)
8600 ui_out_field_skip (uiout, "addr");
8601 annotate_field (5);
8602 ui_out_text (uiout, "exec");
8603 if (c->exec_pathname != NULL)
8604 {
8605 ui_out_text (uiout, ", program \"");
8606 ui_out_field_string (uiout, "what", c->exec_pathname);
8607 ui_out_text (uiout, "\" ");
8608 }
8609
8610 if (ui_out_is_mi_like_p (uiout))
8611 ui_out_field_string (uiout, "catch-type", "exec");
8612 }
8613
8614 static void
8615 print_mention_catch_exec (struct breakpoint *b)
8616 {
8617 printf_filtered (_("Catchpoint %d (exec)"), b->number);
8618 }
8619
8620 /* Implement the "print_recreate" breakpoint_ops method for exec
8621 catchpoints. */
8622
8623 static void
8624 print_recreate_catch_exec (struct breakpoint *b, struct ui_file *fp)
8625 {
8626 fprintf_unfiltered (fp, "catch exec");
8627 print_recreate_thread (b, fp);
8628 }
8629
8630 static struct breakpoint_ops catch_exec_breakpoint_ops;
8631
8632 static int
8633 hw_breakpoint_used_count (void)
8634 {
8635 int i = 0;
8636 struct breakpoint *b;
8637 struct bp_location *bl;
8638
8639 ALL_BREAKPOINTS (b)
8640 {
8641 if (b->type == bp_hardware_breakpoint && breakpoint_enabled (b))
8642 for (bl = b->loc; bl; bl = bl->next)
8643 {
8644 /* Special types of hardware breakpoints may use more than
8645 one register. */
8646 i += b->ops->resources_needed (bl);
8647 }
8648 }
8649
8650 return i;
8651 }
8652
8653 /* Returns the resources B would use if it were a hardware
8654 watchpoint. */
8655
8656 static int
8657 hw_watchpoint_use_count (struct breakpoint *b)
8658 {
8659 int i = 0;
8660 struct bp_location *bl;
8661
8662 if (!breakpoint_enabled (b))
8663 return 0;
8664
8665 for (bl = b->loc; bl; bl = bl->next)
8666 {
8667 /* Special types of hardware watchpoints may use more than
8668 one register. */
8669 i += b->ops->resources_needed (bl);
8670 }
8671
8672 return i;
8673 }
8674
8675 /* Returns the sum the used resources of all hardware watchpoints of
8676 type TYPE in the breakpoints list. Also returns in OTHER_TYPE_USED
8677 the sum of the used resources of all hardware watchpoints of other
8678 types _not_ TYPE. */
8679
8680 static int
8681 hw_watchpoint_used_count_others (struct breakpoint *except,
8682 enum bptype type, int *other_type_used)
8683 {
8684 int i = 0;
8685 struct breakpoint *b;
8686
8687 *other_type_used = 0;
8688 ALL_BREAKPOINTS (b)
8689 {
8690 if (b == except)
8691 continue;
8692 if (!breakpoint_enabled (b))
8693 continue;
8694
8695 if (b->type == type)
8696 i += hw_watchpoint_use_count (b);
8697 else if (is_hardware_watchpoint (b))
8698 *other_type_used = 1;
8699 }
8700
8701 return i;
8702 }
8703
8704 void
8705 disable_watchpoints_before_interactive_call_start (void)
8706 {
8707 struct breakpoint *b;
8708
8709 ALL_BREAKPOINTS (b)
8710 {
8711 if (is_watchpoint (b) && breakpoint_enabled (b))
8712 {
8713 b->enable_state = bp_call_disabled;
8714 update_global_location_list (UGLL_DONT_INSERT);
8715 }
8716 }
8717 }
8718
8719 void
8720 enable_watchpoints_after_interactive_call_stop (void)
8721 {
8722 struct breakpoint *b;
8723
8724 ALL_BREAKPOINTS (b)
8725 {
8726 if (is_watchpoint (b) && b->enable_state == bp_call_disabled)
8727 {
8728 b->enable_state = bp_enabled;
8729 update_global_location_list (UGLL_MAY_INSERT);
8730 }
8731 }
8732 }
8733
8734 void
8735 disable_breakpoints_before_startup (void)
8736 {
8737 current_program_space->executing_startup = 1;
8738 update_global_location_list (UGLL_DONT_INSERT);
8739 }
8740
8741 void
8742 enable_breakpoints_after_startup (void)
8743 {
8744 current_program_space->executing_startup = 0;
8745 breakpoint_re_set ();
8746 }
8747
8748 /* Create a new single-step breakpoint for thread THREAD, with no
8749 locations. */
8750
8751 static struct breakpoint *
8752 new_single_step_breakpoint (int thread, struct gdbarch *gdbarch)
8753 {
8754 struct breakpoint *b = XNEW (struct breakpoint);
8755
8756 init_raw_breakpoint_without_location (b, gdbarch, bp_single_step,
8757 &momentary_breakpoint_ops);
8758
8759 b->disposition = disp_donttouch;
8760 b->frame_id = null_frame_id;
8761
8762 b->thread = thread;
8763 gdb_assert (b->thread != 0);
8764
8765 add_to_breakpoint_chain (b);
8766
8767 return b;
8768 }
8769
8770 /* Set a momentary breakpoint of type TYPE at address specified by
8771 SAL. If FRAME_ID is valid, the breakpoint is restricted to that
8772 frame. */
8773
8774 struct breakpoint *
8775 set_momentary_breakpoint (struct gdbarch *gdbarch, struct symtab_and_line sal,
8776 struct frame_id frame_id, enum bptype type)
8777 {
8778 struct breakpoint *b;
8779
8780 /* If FRAME_ID is valid, it should be a real frame, not an inlined or
8781 tail-called one. */
8782 gdb_assert (!frame_id_artificial_p (frame_id));
8783
8784 b = set_raw_breakpoint (gdbarch, sal, type, &momentary_breakpoint_ops);
8785 b->enable_state = bp_enabled;
8786 b->disposition = disp_donttouch;
8787 b->frame_id = frame_id;
8788
8789 /* If we're debugging a multi-threaded program, then we want
8790 momentary breakpoints to be active in only a single thread of
8791 control. */
8792 if (in_thread_list (inferior_ptid))
8793 b->thread = pid_to_thread_id (inferior_ptid);
8794
8795 update_global_location_list_nothrow (UGLL_MAY_INSERT);
8796
8797 return b;
8798 }
8799
8800 /* Make a momentary breakpoint based on the master breakpoint ORIG.
8801 The new breakpoint will have type TYPE, use OPS as its
8802 breakpoint_ops, and will set enabled to LOC_ENABLED. */
8803
8804 static struct breakpoint *
8805 momentary_breakpoint_from_master (struct breakpoint *orig,
8806 enum bptype type,
8807 const struct breakpoint_ops *ops,
8808 int loc_enabled)
8809 {
8810 struct breakpoint *copy;
8811
8812 copy = set_raw_breakpoint_without_location (orig->gdbarch, type, ops);
8813 copy->loc = allocate_bp_location (copy);
8814 set_breakpoint_location_function (copy->loc, 1);
8815
8816 copy->loc->gdbarch = orig->loc->gdbarch;
8817 copy->loc->requested_address = orig->loc->requested_address;
8818 copy->loc->address = orig->loc->address;
8819 copy->loc->section = orig->loc->section;
8820 copy->loc->pspace = orig->loc->pspace;
8821 copy->loc->probe = orig->loc->probe;
8822 copy->loc->line_number = orig->loc->line_number;
8823 copy->loc->symtab = orig->loc->symtab;
8824 copy->loc->enabled = loc_enabled;
8825 copy->frame_id = orig->frame_id;
8826 copy->thread = orig->thread;
8827 copy->pspace = orig->pspace;
8828
8829 copy->enable_state = bp_enabled;
8830 copy->disposition = disp_donttouch;
8831 copy->number = internal_breakpoint_number--;
8832
8833 update_global_location_list_nothrow (UGLL_DONT_INSERT);
8834 return copy;
8835 }
8836
8837 /* Make a deep copy of momentary breakpoint ORIG. Returns NULL if
8838 ORIG is NULL. */
8839
8840 struct breakpoint *
8841 clone_momentary_breakpoint (struct breakpoint *orig)
8842 {
8843 /* If there's nothing to clone, then return nothing. */
8844 if (orig == NULL)
8845 return NULL;
8846
8847 return momentary_breakpoint_from_master (orig, orig->type, orig->ops, 0);
8848 }
8849
8850 struct breakpoint *
8851 set_momentary_breakpoint_at_pc (struct gdbarch *gdbarch, CORE_ADDR pc,
8852 enum bptype type)
8853 {
8854 struct symtab_and_line sal;
8855
8856 sal = find_pc_line (pc, 0);
8857 sal.pc = pc;
8858 sal.section = find_pc_overlay (pc);
8859 sal.explicit_pc = 1;
8860
8861 return set_momentary_breakpoint (gdbarch, sal, null_frame_id, type);
8862 }
8863 \f
8864
8865 /* Tell the user we have just set a breakpoint B. */
8866
8867 static void
8868 mention (struct breakpoint *b)
8869 {
8870 b->ops->print_mention (b);
8871 if (ui_out_is_mi_like_p (current_uiout))
8872 return;
8873 printf_filtered ("\n");
8874 }
8875 \f
8876
8877 static int bp_loc_is_permanent (struct bp_location *loc);
8878
8879 static struct bp_location *
8880 add_location_to_breakpoint (struct breakpoint *b,
8881 const struct symtab_and_line *sal)
8882 {
8883 struct bp_location *loc, **tmp;
8884 CORE_ADDR adjusted_address;
8885 struct gdbarch *loc_gdbarch = get_sal_arch (*sal);
8886
8887 if (loc_gdbarch == NULL)
8888 loc_gdbarch = b->gdbarch;
8889
8890 /* Adjust the breakpoint's address prior to allocating a location.
8891 Once we call allocate_bp_location(), that mostly uninitialized
8892 location will be placed on the location chain. Adjustment of the
8893 breakpoint may cause target_read_memory() to be called and we do
8894 not want its scan of the location chain to find a breakpoint and
8895 location that's only been partially initialized. */
8896 adjusted_address = adjust_breakpoint_address (loc_gdbarch,
8897 sal->pc, b->type);
8898
8899 /* Sort the locations by their ADDRESS. */
8900 loc = allocate_bp_location (b);
8901 for (tmp = &(b->loc); *tmp != NULL && (*tmp)->address <= adjusted_address;
8902 tmp = &((*tmp)->next))
8903 ;
8904 loc->next = *tmp;
8905 *tmp = loc;
8906
8907 loc->requested_address = sal->pc;
8908 loc->address = adjusted_address;
8909 loc->pspace = sal->pspace;
8910 loc->probe.probe = sal->probe;
8911 loc->probe.objfile = sal->objfile;
8912 gdb_assert (loc->pspace != NULL);
8913 loc->section = sal->section;
8914 loc->gdbarch = loc_gdbarch;
8915 loc->line_number = sal->line;
8916 loc->symtab = sal->symtab;
8917
8918 set_breakpoint_location_function (loc,
8919 sal->explicit_pc || sal->explicit_line);
8920
8921 if (bp_loc_is_permanent (loc))
8922 {
8923 loc->inserted = 1;
8924 loc->permanent = 1;
8925 }
8926
8927 return loc;
8928 }
8929 \f
8930
8931 /* See breakpoint.h. */
8932
8933 int
8934 program_breakpoint_here_p (struct gdbarch *gdbarch, CORE_ADDR address)
8935 {
8936 int len;
8937 CORE_ADDR addr;
8938 const gdb_byte *bpoint;
8939 gdb_byte *target_mem;
8940 struct cleanup *cleanup;
8941 int retval = 0;
8942
8943 addr = address;
8944 bpoint = gdbarch_breakpoint_from_pc (gdbarch, &addr, &len);
8945
8946 /* Software breakpoints unsupported? */
8947 if (bpoint == NULL)
8948 return 0;
8949
8950 target_mem = alloca (len);
8951
8952 /* Enable the automatic memory restoration from breakpoints while
8953 we read the memory. Otherwise we could say about our temporary
8954 breakpoints they are permanent. */
8955 cleanup = make_show_memory_breakpoints_cleanup (0);
8956
8957 if (target_read_memory (address, target_mem, len) == 0
8958 && memcmp (target_mem, bpoint, len) == 0)
8959 retval = 1;
8960
8961 do_cleanups (cleanup);
8962
8963 return retval;
8964 }
8965
8966 /* Return 1 if LOC is pointing to a permanent breakpoint,
8967 return 0 otherwise. */
8968
8969 static int
8970 bp_loc_is_permanent (struct bp_location *loc)
8971 {
8972 struct cleanup *cleanup;
8973 int retval;
8974
8975 gdb_assert (loc != NULL);
8976
8977 /* bp_call_dummy breakpoint locations are usually memory locations
8978 where GDB just wrote a breakpoint instruction, making it look
8979 as if there is a permanent breakpoint at that location. Considering
8980 it permanent makes GDB rely on that breakpoint instruction to stop
8981 the program, thus removing the need to insert its own breakpoint
8982 there. This is normally expected to work, except that some versions
8983 of QEMU (Eg: QEMU 2.0.0 for SPARC) just report a fatal problem (Trap
8984 0x02 while interrupts disabled, Error state) instead of reporting
8985 a SIGTRAP. QEMU should probably be fixed, but in the interest of
8986 compatibility with versions that behave this way, we always consider
8987 bp_call_dummy breakpoint locations as non-permanent. */
8988 if (loc->owner->type == bp_call_dummy)
8989 return 0;
8990
8991 cleanup = save_current_space_and_thread ();
8992 switch_to_program_space_and_thread (loc->pspace);
8993
8994 retval = program_breakpoint_here_p (loc->gdbarch, loc->address);
8995
8996 do_cleanups (cleanup);
8997
8998 return retval;
8999 }
9000
9001 /* Build a command list for the dprintf corresponding to the current
9002 settings of the dprintf style options. */
9003
9004 static void
9005 update_dprintf_command_list (struct breakpoint *b)
9006 {
9007 char *dprintf_args = b->extra_string;
9008 char *printf_line = NULL;
9009
9010 if (!dprintf_args)
9011 return;
9012
9013 dprintf_args = skip_spaces (dprintf_args);
9014
9015 /* Allow a comma, as it may have terminated a location, but don't
9016 insist on it. */
9017 if (*dprintf_args == ',')
9018 ++dprintf_args;
9019 dprintf_args = skip_spaces (dprintf_args);
9020
9021 if (*dprintf_args != '"')
9022 error (_("Bad format string, missing '\"'."));
9023
9024 if (strcmp (dprintf_style, dprintf_style_gdb) == 0)
9025 printf_line = xstrprintf ("printf %s", dprintf_args);
9026 else if (strcmp (dprintf_style, dprintf_style_call) == 0)
9027 {
9028 if (!dprintf_function)
9029 error (_("No function supplied for dprintf call"));
9030
9031 if (dprintf_channel && strlen (dprintf_channel) > 0)
9032 printf_line = xstrprintf ("call (void) %s (%s,%s)",
9033 dprintf_function,
9034 dprintf_channel,
9035 dprintf_args);
9036 else
9037 printf_line = xstrprintf ("call (void) %s (%s)",
9038 dprintf_function,
9039 dprintf_args);
9040 }
9041 else if (strcmp (dprintf_style, dprintf_style_agent) == 0)
9042 {
9043 if (target_can_run_breakpoint_commands ())
9044 printf_line = xstrprintf ("agent-printf %s", dprintf_args);
9045 else
9046 {
9047 warning (_("Target cannot run dprintf commands, falling back to GDB printf"));
9048 printf_line = xstrprintf ("printf %s", dprintf_args);
9049 }
9050 }
9051 else
9052 internal_error (__FILE__, __LINE__,
9053 _("Invalid dprintf style."));
9054
9055 gdb_assert (printf_line != NULL);
9056 /* Manufacture a printf sequence. */
9057 {
9058 struct command_line *printf_cmd_line
9059 = xmalloc (sizeof (struct command_line));
9060
9061 printf_cmd_line->control_type = simple_control;
9062 printf_cmd_line->body_count = 0;
9063 printf_cmd_line->body_list = NULL;
9064 printf_cmd_line->next = NULL;
9065 printf_cmd_line->line = printf_line;
9066
9067 breakpoint_set_commands (b, printf_cmd_line);
9068 }
9069 }
9070
9071 /* Update all dprintf commands, making their command lists reflect
9072 current style settings. */
9073
9074 static void
9075 update_dprintf_commands (char *args, int from_tty,
9076 struct cmd_list_element *c)
9077 {
9078 struct breakpoint *b;
9079
9080 ALL_BREAKPOINTS (b)
9081 {
9082 if (b->type == bp_dprintf)
9083 update_dprintf_command_list (b);
9084 }
9085 }
9086
9087 /* Create a breakpoint with SAL as location. Use ADDR_STRING
9088 as textual description of the location, and COND_STRING
9089 as condition expression. */
9090
9091 static void
9092 init_breakpoint_sal (struct breakpoint *b, struct gdbarch *gdbarch,
9093 struct symtabs_and_lines sals, char *addr_string,
9094 char *filter, char *cond_string,
9095 char *extra_string,
9096 enum bptype type, enum bpdisp disposition,
9097 int thread, int task, int ignore_count,
9098 const struct breakpoint_ops *ops, int from_tty,
9099 int enabled, int internal, unsigned flags,
9100 int display_canonical)
9101 {
9102 int i;
9103
9104 if (type == bp_hardware_breakpoint)
9105 {
9106 int target_resources_ok;
9107
9108 i = hw_breakpoint_used_count ();
9109 target_resources_ok =
9110 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
9111 i + 1, 0);
9112 if (target_resources_ok == 0)
9113 error (_("No hardware breakpoint support in the target."));
9114 else if (target_resources_ok < 0)
9115 error (_("Hardware breakpoints used exceeds limit."));
9116 }
9117
9118 gdb_assert (sals.nelts > 0);
9119
9120 for (i = 0; i < sals.nelts; ++i)
9121 {
9122 struct symtab_and_line sal = sals.sals[i];
9123 struct bp_location *loc;
9124
9125 if (from_tty)
9126 {
9127 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
9128 if (!loc_gdbarch)
9129 loc_gdbarch = gdbarch;
9130
9131 describe_other_breakpoints (loc_gdbarch,
9132 sal.pspace, sal.pc, sal.section, thread);
9133 }
9134
9135 if (i == 0)
9136 {
9137 init_raw_breakpoint (b, gdbarch, sal, type, ops);
9138 b->thread = thread;
9139 b->task = task;
9140
9141 b->cond_string = cond_string;
9142 b->extra_string = extra_string;
9143 b->ignore_count = ignore_count;
9144 b->enable_state = enabled ? bp_enabled : bp_disabled;
9145 b->disposition = disposition;
9146
9147 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9148 b->loc->inserted = 1;
9149
9150 if (type == bp_static_tracepoint)
9151 {
9152 struct tracepoint *t = (struct tracepoint *) b;
9153 struct static_tracepoint_marker marker;
9154
9155 if (strace_marker_p (b))
9156 {
9157 /* We already know the marker exists, otherwise, we
9158 wouldn't see a sal for it. */
9159 char *p = &addr_string[3];
9160 char *endp;
9161 char *marker_str;
9162
9163 p = skip_spaces (p);
9164
9165 endp = skip_to_space (p);
9166
9167 marker_str = savestring (p, endp - p);
9168 t->static_trace_marker_id = marker_str;
9169
9170 printf_filtered (_("Probed static tracepoint "
9171 "marker \"%s\"\n"),
9172 t->static_trace_marker_id);
9173 }
9174 else if (target_static_tracepoint_marker_at (sal.pc, &marker))
9175 {
9176 t->static_trace_marker_id = xstrdup (marker.str_id);
9177 release_static_tracepoint_marker (&marker);
9178
9179 printf_filtered (_("Probed static tracepoint "
9180 "marker \"%s\"\n"),
9181 t->static_trace_marker_id);
9182 }
9183 else
9184 warning (_("Couldn't determine the static "
9185 "tracepoint marker to probe"));
9186 }
9187
9188 loc = b->loc;
9189 }
9190 else
9191 {
9192 loc = add_location_to_breakpoint (b, &sal);
9193 if ((flags & CREATE_BREAKPOINT_FLAGS_INSERTED) != 0)
9194 loc->inserted = 1;
9195 }
9196
9197 if (b->cond_string)
9198 {
9199 const char *arg = b->cond_string;
9200
9201 loc->cond = parse_exp_1 (&arg, loc->address,
9202 block_for_pc (loc->address), 0);
9203 if (*arg)
9204 error (_("Garbage '%s' follows condition"), arg);
9205 }
9206
9207 /* Dynamic printf requires and uses additional arguments on the
9208 command line, otherwise it's an error. */
9209 if (type == bp_dprintf)
9210 {
9211 if (b->extra_string)
9212 update_dprintf_command_list (b);
9213 else
9214 error (_("Format string required"));
9215 }
9216 else if (b->extra_string)
9217 error (_("Garbage '%s' at end of command"), b->extra_string);
9218 }
9219
9220 b->display_canonical = display_canonical;
9221 if (addr_string)
9222 b->addr_string = addr_string;
9223 else
9224 /* addr_string has to be used or breakpoint_re_set will delete
9225 me. */
9226 b->addr_string
9227 = xstrprintf ("*%s", paddress (b->loc->gdbarch, b->loc->address));
9228 b->filter = filter;
9229 }
9230
9231 static void
9232 create_breakpoint_sal (struct gdbarch *gdbarch,
9233 struct symtabs_and_lines sals, char *addr_string,
9234 char *filter, char *cond_string,
9235 char *extra_string,
9236 enum bptype type, enum bpdisp disposition,
9237 int thread, int task, int ignore_count,
9238 const struct breakpoint_ops *ops, int from_tty,
9239 int enabled, int internal, unsigned flags,
9240 int display_canonical)
9241 {
9242 struct breakpoint *b;
9243 struct cleanup *old_chain;
9244
9245 if (is_tracepoint_type (type))
9246 {
9247 struct tracepoint *t;
9248
9249 t = XCNEW (struct tracepoint);
9250 b = &t->base;
9251 }
9252 else
9253 b = XNEW (struct breakpoint);
9254
9255 old_chain = make_cleanup (xfree, b);
9256
9257 init_breakpoint_sal (b, gdbarch,
9258 sals, addr_string,
9259 filter, cond_string, extra_string,
9260 type, disposition,
9261 thread, task, ignore_count,
9262 ops, from_tty,
9263 enabled, internal, flags,
9264 display_canonical);
9265 discard_cleanups (old_chain);
9266
9267 install_breakpoint (internal, b, 0);
9268 }
9269
9270 /* Add SALS.nelts breakpoints to the breakpoint table. For each
9271 SALS.sal[i] breakpoint, include the corresponding ADDR_STRING[i]
9272 value. COND_STRING, if not NULL, specified the condition to be
9273 used for all breakpoints. Essentially the only case where
9274 SALS.nelts is not 1 is when we set a breakpoint on an overloaded
9275 function. In that case, it's still not possible to specify
9276 separate conditions for different overloaded functions, so
9277 we take just a single condition string.
9278
9279 NOTE: If the function succeeds, the caller is expected to cleanup
9280 the arrays ADDR_STRING, COND_STRING, and SALS (but not the
9281 array contents). If the function fails (error() is called), the
9282 caller is expected to cleanups both the ADDR_STRING, COND_STRING,
9283 COND and SALS arrays and each of those arrays contents. */
9284
9285 static void
9286 create_breakpoints_sal (struct gdbarch *gdbarch,
9287 struct linespec_result *canonical,
9288 char *cond_string, char *extra_string,
9289 enum bptype type, enum bpdisp disposition,
9290 int thread, int task, int ignore_count,
9291 const struct breakpoint_ops *ops, int from_tty,
9292 int enabled, int internal, unsigned flags)
9293 {
9294 int i;
9295 struct linespec_sals *lsal;
9296
9297 if (canonical->pre_expanded)
9298 gdb_assert (VEC_length (linespec_sals, canonical->sals) == 1);
9299
9300 for (i = 0; VEC_iterate (linespec_sals, canonical->sals, i, lsal); ++i)
9301 {
9302 /* Note that 'addr_string' can be NULL in the case of a plain
9303 'break', without arguments. */
9304 char *addr_string = (canonical->addr_string
9305 ? xstrdup (canonical->addr_string)
9306 : NULL);
9307 char *filter_string = lsal->canonical ? xstrdup (lsal->canonical) : NULL;
9308 struct cleanup *inner = make_cleanup (xfree, addr_string);
9309
9310 make_cleanup (xfree, filter_string);
9311 create_breakpoint_sal (gdbarch, lsal->sals,
9312 addr_string,
9313 filter_string,
9314 cond_string, extra_string,
9315 type, disposition,
9316 thread, task, ignore_count, ops,
9317 from_tty, enabled, internal, flags,
9318 canonical->special_display);
9319 discard_cleanups (inner);
9320 }
9321 }
9322
9323 /* Parse ADDRESS which is assumed to be a SAL specification possibly
9324 followed by conditionals. On return, SALS contains an array of SAL
9325 addresses found. ADDR_STRING contains a vector of (canonical)
9326 address strings. ADDRESS points to the end of the SAL.
9327
9328 The array and the line spec strings are allocated on the heap, it is
9329 the caller's responsibility to free them. */
9330
9331 static void
9332 parse_breakpoint_sals (char **address,
9333 struct linespec_result *canonical)
9334 {
9335 /* If no arg given, or if first arg is 'if ', use the default
9336 breakpoint. */
9337 if ((*address) == NULL || linespec_lexer_lex_keyword (*address))
9338 {
9339 /* The last displayed codepoint, if it's valid, is our default breakpoint
9340 address. */
9341 if (last_displayed_sal_is_valid ())
9342 {
9343 struct linespec_sals lsal;
9344 struct symtab_and_line sal;
9345 CORE_ADDR pc;
9346
9347 init_sal (&sal); /* Initialize to zeroes. */
9348 lsal.sals.sals = (struct symtab_and_line *)
9349 xmalloc (sizeof (struct symtab_and_line));
9350
9351 /* Set sal's pspace, pc, symtab, and line to the values
9352 corresponding to the last call to print_frame_info.
9353 Be sure to reinitialize LINE with NOTCURRENT == 0
9354 as the breakpoint line number is inappropriate otherwise.
9355 find_pc_line would adjust PC, re-set it back. */
9356 get_last_displayed_sal (&sal);
9357 pc = sal.pc;
9358 sal = find_pc_line (pc, 0);
9359
9360 /* "break" without arguments is equivalent to "break *PC"
9361 where PC is the last displayed codepoint's address. So
9362 make sure to set sal.explicit_pc to prevent GDB from
9363 trying to expand the list of sals to include all other
9364 instances with the same symtab and line. */
9365 sal.pc = pc;
9366 sal.explicit_pc = 1;
9367
9368 lsal.sals.sals[0] = sal;
9369 lsal.sals.nelts = 1;
9370 lsal.canonical = NULL;
9371
9372 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
9373 }
9374 else
9375 error (_("No default breakpoint address now."));
9376 }
9377 else
9378 {
9379 struct symtab_and_line cursal = get_current_source_symtab_and_line ();
9380
9381 /* Force almost all breakpoints to be in terms of the
9382 current_source_symtab (which is decode_line_1's default).
9383 This should produce the results we want almost all of the
9384 time while leaving default_breakpoint_* alone.
9385
9386 ObjC: However, don't match an Objective-C method name which
9387 may have a '+' or '-' succeeded by a '['. */
9388 if (last_displayed_sal_is_valid ()
9389 && (!cursal.symtab
9390 || ((strchr ("+-", (*address)[0]) != NULL)
9391 && ((*address)[1] != '['))))
9392 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
9393 get_last_displayed_symtab (),
9394 get_last_displayed_line (),
9395 canonical, NULL, NULL);
9396 else
9397 decode_line_full (address, DECODE_LINE_FUNFIRSTLINE,
9398 cursal.symtab, cursal.line, canonical, NULL, NULL);
9399 }
9400 }
9401
9402
9403 /* Convert each SAL into a real PC. Verify that the PC can be
9404 inserted as a breakpoint. If it can't throw an error. */
9405
9406 static void
9407 breakpoint_sals_to_pc (struct symtabs_and_lines *sals)
9408 {
9409 int i;
9410
9411 for (i = 0; i < sals->nelts; i++)
9412 resolve_sal_pc (&sals->sals[i]);
9413 }
9414
9415 /* Fast tracepoints may have restrictions on valid locations. For
9416 instance, a fast tracepoint using a jump instead of a trap will
9417 likely have to overwrite more bytes than a trap would, and so can
9418 only be placed where the instruction is longer than the jump, or a
9419 multi-instruction sequence does not have a jump into the middle of
9420 it, etc. */
9421
9422 static void
9423 check_fast_tracepoint_sals (struct gdbarch *gdbarch,
9424 struct symtabs_and_lines *sals)
9425 {
9426 int i, rslt;
9427 struct symtab_and_line *sal;
9428 char *msg;
9429 struct cleanup *old_chain;
9430
9431 for (i = 0; i < sals->nelts; i++)
9432 {
9433 struct gdbarch *sarch;
9434
9435 sal = &sals->sals[i];
9436
9437 sarch = get_sal_arch (*sal);
9438 /* We fall back to GDBARCH if there is no architecture
9439 associated with SAL. */
9440 if (sarch == NULL)
9441 sarch = gdbarch;
9442 rslt = gdbarch_fast_tracepoint_valid_at (sarch, sal->pc,
9443 NULL, &msg);
9444 old_chain = make_cleanup (xfree, msg);
9445
9446 if (!rslt)
9447 error (_("May not have a fast tracepoint at 0x%s%s"),
9448 paddress (sarch, sal->pc), (msg ? msg : ""));
9449
9450 do_cleanups (old_chain);
9451 }
9452 }
9453
9454 /* Issue an invalid thread ID error. */
9455
9456 static void ATTRIBUTE_NORETURN
9457 invalid_thread_id_error (int id)
9458 {
9459 error (_("Unknown thread %d."), id);
9460 }
9461
9462 /* Given TOK, a string specification of condition and thread, as
9463 accepted by the 'break' command, extract the condition
9464 string and thread number and set *COND_STRING and *THREAD.
9465 PC identifies the context at which the condition should be parsed.
9466 If no condition is found, *COND_STRING is set to NULL.
9467 If no thread is found, *THREAD is set to -1. */
9468
9469 static void
9470 find_condition_and_thread (const char *tok, CORE_ADDR pc,
9471 char **cond_string, int *thread, int *task,
9472 char **rest)
9473 {
9474 *cond_string = NULL;
9475 *thread = -1;
9476 *task = 0;
9477 *rest = NULL;
9478
9479 while (tok && *tok)
9480 {
9481 const char *end_tok;
9482 int toklen;
9483 const char *cond_start = NULL;
9484 const char *cond_end = NULL;
9485
9486 tok = skip_spaces_const (tok);
9487
9488 if ((*tok == '"' || *tok == ',') && rest)
9489 {
9490 *rest = savestring (tok, strlen (tok));
9491 return;
9492 }
9493
9494 end_tok = skip_to_space_const (tok);
9495
9496 toklen = end_tok - tok;
9497
9498 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
9499 {
9500 struct expression *expr;
9501
9502 tok = cond_start = end_tok + 1;
9503 expr = parse_exp_1 (&tok, pc, block_for_pc (pc), 0);
9504 xfree (expr);
9505 cond_end = tok;
9506 *cond_string = savestring (cond_start, cond_end - cond_start);
9507 }
9508 else if (toklen >= 1 && strncmp (tok, "thread", toklen) == 0)
9509 {
9510 char *tmptok;
9511
9512 tok = end_tok + 1;
9513 *thread = strtol (tok, &tmptok, 0);
9514 if (tok == tmptok)
9515 error (_("Junk after thread keyword."));
9516 if (!valid_thread_id (*thread))
9517 invalid_thread_id_error (*thread);
9518 tok = tmptok;
9519 }
9520 else if (toklen >= 1 && strncmp (tok, "task", toklen) == 0)
9521 {
9522 char *tmptok;
9523
9524 tok = end_tok + 1;
9525 *task = strtol (tok, &tmptok, 0);
9526 if (tok == tmptok)
9527 error (_("Junk after task keyword."));
9528 if (!valid_task_id (*task))
9529 error (_("Unknown task %d."), *task);
9530 tok = tmptok;
9531 }
9532 else if (rest)
9533 {
9534 *rest = savestring (tok, strlen (tok));
9535 return;
9536 }
9537 else
9538 error (_("Junk at end of arguments."));
9539 }
9540 }
9541
9542 /* Decode a static tracepoint marker spec. */
9543
9544 static struct symtabs_and_lines
9545 decode_static_tracepoint_spec (char **arg_p)
9546 {
9547 VEC(static_tracepoint_marker_p) *markers = NULL;
9548 struct symtabs_and_lines sals;
9549 struct cleanup *old_chain;
9550 char *p = &(*arg_p)[3];
9551 char *endp;
9552 char *marker_str;
9553 int i;
9554
9555 p = skip_spaces (p);
9556
9557 endp = skip_to_space (p);
9558
9559 marker_str = savestring (p, endp - p);
9560 old_chain = make_cleanup (xfree, marker_str);
9561
9562 markers = target_static_tracepoint_markers_by_strid (marker_str);
9563 if (VEC_empty(static_tracepoint_marker_p, markers))
9564 error (_("No known static tracepoint marker named %s"), marker_str);
9565
9566 sals.nelts = VEC_length(static_tracepoint_marker_p, markers);
9567 sals.sals = xmalloc (sizeof *sals.sals * sals.nelts);
9568
9569 for (i = 0; i < sals.nelts; i++)
9570 {
9571 struct static_tracepoint_marker *marker;
9572
9573 marker = VEC_index (static_tracepoint_marker_p, markers, i);
9574
9575 init_sal (&sals.sals[i]);
9576
9577 sals.sals[i] = find_pc_line (marker->address, 0);
9578 sals.sals[i].pc = marker->address;
9579
9580 release_static_tracepoint_marker (marker);
9581 }
9582
9583 do_cleanups (old_chain);
9584
9585 *arg_p = endp;
9586 return sals;
9587 }
9588
9589 /* Set a breakpoint. This function is shared between CLI and MI
9590 functions for setting a breakpoint. This function has two major
9591 modes of operations, selected by the PARSE_ARG parameter. If
9592 non-zero, the function will parse ARG, extracting location,
9593 condition, thread and extra string. Otherwise, ARG is just the
9594 breakpoint's location, with condition, thread, and extra string
9595 specified by the COND_STRING, THREAD and EXTRA_STRING parameters.
9596 If INTERNAL is non-zero, the breakpoint number will be allocated
9597 from the internal breakpoint count. Returns true if any breakpoint
9598 was created; false otherwise. */
9599
9600 int
9601 create_breakpoint (struct gdbarch *gdbarch,
9602 char *arg, char *cond_string,
9603 int thread, char *extra_string,
9604 int parse_arg,
9605 int tempflag, enum bptype type_wanted,
9606 int ignore_count,
9607 enum auto_boolean pending_break_support,
9608 const struct breakpoint_ops *ops,
9609 int from_tty, int enabled, int internal,
9610 unsigned flags)
9611 {
9612 char *copy_arg = NULL;
9613 char *addr_start = arg;
9614 struct linespec_result canonical;
9615 struct cleanup *old_chain;
9616 struct cleanup *bkpt_chain = NULL;
9617 int pending = 0;
9618 int task = 0;
9619 int prev_bkpt_count = breakpoint_count;
9620
9621 gdb_assert (ops != NULL);
9622
9623 init_linespec_result (&canonical);
9624
9625 TRY
9626 {
9627 ops->create_sals_from_address (&arg, &canonical, type_wanted,
9628 addr_start, &copy_arg);
9629 }
9630 CATCH (e, RETURN_MASK_ERROR)
9631 {
9632 /* If caller is interested in rc value from parse, set
9633 value. */
9634 if (e.error == NOT_FOUND_ERROR)
9635 {
9636 /* If pending breakpoint support is turned off, throw
9637 error. */
9638
9639 if (pending_break_support == AUTO_BOOLEAN_FALSE)
9640 throw_exception (e);
9641
9642 exception_print (gdb_stderr, e);
9643
9644 /* If pending breakpoint support is auto query and the user
9645 selects no, then simply return the error code. */
9646 if (pending_break_support == AUTO_BOOLEAN_AUTO
9647 && !nquery (_("Make %s pending on future shared library load? "),
9648 bptype_string (type_wanted)))
9649 return 0;
9650
9651 /* At this point, either the user was queried about setting
9652 a pending breakpoint and selected yes, or pending
9653 breakpoint behavior is on and thus a pending breakpoint
9654 is defaulted on behalf of the user. */
9655 {
9656 struct linespec_sals lsal;
9657
9658 copy_arg = xstrdup (addr_start);
9659 lsal.canonical = xstrdup (copy_arg);
9660 lsal.sals.nelts = 1;
9661 lsal.sals.sals = XNEW (struct symtab_and_line);
9662 init_sal (&lsal.sals.sals[0]);
9663 pending = 1;
9664 VEC_safe_push (linespec_sals, canonical.sals, &lsal);
9665 }
9666 }
9667 else
9668 throw_exception (e);
9669 }
9670 END_CATCH
9671
9672 if (VEC_empty (linespec_sals, canonical.sals))
9673 return 0;
9674
9675 /* Create a chain of things that always need to be cleaned up. */
9676 old_chain = make_cleanup_destroy_linespec_result (&canonical);
9677
9678 /* ----------------------------- SNIP -----------------------------
9679 Anything added to the cleanup chain beyond this point is assumed
9680 to be part of a breakpoint. If the breakpoint create succeeds
9681 then the memory is not reclaimed. */
9682 bkpt_chain = make_cleanup (null_cleanup, 0);
9683
9684 /* Resolve all line numbers to PC's and verify that the addresses
9685 are ok for the target. */
9686 if (!pending)
9687 {
9688 int ix;
9689 struct linespec_sals *iter;
9690
9691 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9692 breakpoint_sals_to_pc (&iter->sals);
9693 }
9694
9695 /* Fast tracepoints may have additional restrictions on location. */
9696 if (!pending && type_wanted == bp_fast_tracepoint)
9697 {
9698 int ix;
9699 struct linespec_sals *iter;
9700
9701 for (ix = 0; VEC_iterate (linespec_sals, canonical.sals, ix, iter); ++ix)
9702 check_fast_tracepoint_sals (gdbarch, &iter->sals);
9703 }
9704
9705 /* Verify that condition can be parsed, before setting any
9706 breakpoints. Allocate a separate condition expression for each
9707 breakpoint. */
9708 if (!pending)
9709 {
9710 if (parse_arg)
9711 {
9712 char *rest;
9713 struct linespec_sals *lsal;
9714
9715 lsal = VEC_index (linespec_sals, canonical.sals, 0);
9716
9717 /* Here we only parse 'arg' to separate condition
9718 from thread number, so parsing in context of first
9719 sal is OK. When setting the breakpoint we'll
9720 re-parse it in context of each sal. */
9721
9722 find_condition_and_thread (arg, lsal->sals.sals[0].pc, &cond_string,
9723 &thread, &task, &rest);
9724 if (cond_string)
9725 make_cleanup (xfree, cond_string);
9726 if (rest)
9727 make_cleanup (xfree, rest);
9728 if (rest)
9729 extra_string = rest;
9730 }
9731 else
9732 {
9733 if (*arg != '\0')
9734 error (_("Garbage '%s' at end of location"), arg);
9735
9736 /* Create a private copy of condition string. */
9737 if (cond_string)
9738 {
9739 cond_string = xstrdup (cond_string);
9740 make_cleanup (xfree, cond_string);
9741 }
9742 /* Create a private copy of any extra string. */
9743 if (extra_string)
9744 {
9745 extra_string = xstrdup (extra_string);
9746 make_cleanup (xfree, extra_string);
9747 }
9748 }
9749
9750 ops->create_breakpoints_sal (gdbarch, &canonical,
9751 cond_string, extra_string, type_wanted,
9752 tempflag ? disp_del : disp_donttouch,
9753 thread, task, ignore_count, ops,
9754 from_tty, enabled, internal, flags);
9755 }
9756 else
9757 {
9758 struct breakpoint *b;
9759
9760 make_cleanup (xfree, copy_arg);
9761
9762 if (is_tracepoint_type (type_wanted))
9763 {
9764 struct tracepoint *t;
9765
9766 t = XCNEW (struct tracepoint);
9767 b = &t->base;
9768 }
9769 else
9770 b = XNEW (struct breakpoint);
9771
9772 init_raw_breakpoint_without_location (b, gdbarch, type_wanted, ops);
9773
9774 b->addr_string = copy_arg;
9775 if (parse_arg)
9776 {
9777 b->cond_string = NULL;
9778 b->extra_string = NULL;
9779 }
9780 else
9781 {
9782 /* Create a private copy of condition string. */
9783 if (cond_string)
9784 {
9785 cond_string = xstrdup (cond_string);
9786 make_cleanup (xfree, cond_string);
9787 }
9788 /* Create a private copy of any extra string. */
9789 if (extra_string != NULL)
9790 {
9791 extra_string = xstrdup (extra_string);
9792 make_cleanup (xfree, extra_string);
9793 }
9794 b->cond_string = cond_string;
9795 b->extra_string = extra_string;
9796 b->thread = thread;
9797 }
9798 b->ignore_count = ignore_count;
9799 b->disposition = tempflag ? disp_del : disp_donttouch;
9800 b->condition_not_parsed = 1;
9801 b->enable_state = enabled ? bp_enabled : bp_disabled;
9802 if ((type_wanted != bp_breakpoint
9803 && type_wanted != bp_hardware_breakpoint) || thread != -1)
9804 b->pspace = current_program_space;
9805
9806 install_breakpoint (internal, b, 0);
9807 }
9808
9809 if (VEC_length (linespec_sals, canonical.sals) > 1)
9810 {
9811 warning (_("Multiple breakpoints were set.\nUse the "
9812 "\"delete\" command to delete unwanted breakpoints."));
9813 prev_breakpoint_count = prev_bkpt_count;
9814 }
9815
9816 /* That's it. Discard the cleanups for data inserted into the
9817 breakpoint. */
9818 discard_cleanups (bkpt_chain);
9819 /* But cleanup everything else. */
9820 do_cleanups (old_chain);
9821
9822 /* error call may happen here - have BKPT_CHAIN already discarded. */
9823 update_global_location_list (UGLL_MAY_INSERT);
9824
9825 return 1;
9826 }
9827
9828 /* Set a breakpoint.
9829 ARG is a string describing breakpoint address,
9830 condition, and thread.
9831 FLAG specifies if a breakpoint is hardware on,
9832 and if breakpoint is temporary, using BP_HARDWARE_FLAG
9833 and BP_TEMPFLAG. */
9834
9835 static void
9836 break_command_1 (char *arg, int flag, int from_tty)
9837 {
9838 int tempflag = flag & BP_TEMPFLAG;
9839 enum bptype type_wanted = (flag & BP_HARDWAREFLAG
9840 ? bp_hardware_breakpoint
9841 : bp_breakpoint);
9842 struct breakpoint_ops *ops;
9843 const char *arg_cp = arg;
9844
9845 /* Matching breakpoints on probes. */
9846 if (arg && probe_linespec_to_ops (&arg_cp) != NULL)
9847 ops = &bkpt_probe_breakpoint_ops;
9848 else
9849 ops = &bkpt_breakpoint_ops;
9850
9851 create_breakpoint (get_current_arch (),
9852 arg,
9853 NULL, 0, NULL, 1 /* parse arg */,
9854 tempflag, type_wanted,
9855 0 /* Ignore count */,
9856 pending_break_support,
9857 ops,
9858 from_tty,
9859 1 /* enabled */,
9860 0 /* internal */,
9861 0);
9862 }
9863
9864 /* Helper function for break_command_1 and disassemble_command. */
9865
9866 void
9867 resolve_sal_pc (struct symtab_and_line *sal)
9868 {
9869 CORE_ADDR pc;
9870
9871 if (sal->pc == 0 && sal->symtab != NULL)
9872 {
9873 if (!find_line_pc (sal->symtab, sal->line, &pc))
9874 error (_("No line %d in file \"%s\"."),
9875 sal->line, symtab_to_filename_for_display (sal->symtab));
9876 sal->pc = pc;
9877
9878 /* If this SAL corresponds to a breakpoint inserted using a line
9879 number, then skip the function prologue if necessary. */
9880 if (sal->explicit_line)
9881 skip_prologue_sal (sal);
9882 }
9883
9884 if (sal->section == 0 && sal->symtab != NULL)
9885 {
9886 const struct blockvector *bv;
9887 const struct block *b;
9888 struct symbol *sym;
9889
9890 bv = blockvector_for_pc_sect (sal->pc, 0, &b,
9891 SYMTAB_COMPUNIT (sal->symtab));
9892 if (bv != NULL)
9893 {
9894 sym = block_linkage_function (b);
9895 if (sym != NULL)
9896 {
9897 fixup_symbol_section (sym, SYMTAB_OBJFILE (sal->symtab));
9898 sal->section = SYMBOL_OBJ_SECTION (SYMTAB_OBJFILE (sal->symtab),
9899 sym);
9900 }
9901 else
9902 {
9903 /* It really is worthwhile to have the section, so we'll
9904 just have to look harder. This case can be executed
9905 if we have line numbers but no functions (as can
9906 happen in assembly source). */
9907
9908 struct bound_minimal_symbol msym;
9909 struct cleanup *old_chain = save_current_space_and_thread ();
9910
9911 switch_to_program_space_and_thread (sal->pspace);
9912
9913 msym = lookup_minimal_symbol_by_pc (sal->pc);
9914 if (msym.minsym)
9915 sal->section = MSYMBOL_OBJ_SECTION (msym.objfile, msym.minsym);
9916
9917 do_cleanups (old_chain);
9918 }
9919 }
9920 }
9921 }
9922
9923 void
9924 break_command (char *arg, int from_tty)
9925 {
9926 break_command_1 (arg, 0, from_tty);
9927 }
9928
9929 void
9930 tbreak_command (char *arg, int from_tty)
9931 {
9932 break_command_1 (arg, BP_TEMPFLAG, from_tty);
9933 }
9934
9935 static void
9936 hbreak_command (char *arg, int from_tty)
9937 {
9938 break_command_1 (arg, BP_HARDWAREFLAG, from_tty);
9939 }
9940
9941 static void
9942 thbreak_command (char *arg, int from_tty)
9943 {
9944 break_command_1 (arg, (BP_TEMPFLAG | BP_HARDWAREFLAG), from_tty);
9945 }
9946
9947 static void
9948 stop_command (char *arg, int from_tty)
9949 {
9950 printf_filtered (_("Specify the type of breakpoint to set.\n\
9951 Usage: stop in <function | address>\n\
9952 stop at <line>\n"));
9953 }
9954
9955 static void
9956 stopin_command (char *arg, int from_tty)
9957 {
9958 int badInput = 0;
9959
9960 if (arg == (char *) NULL)
9961 badInput = 1;
9962 else if (*arg != '*')
9963 {
9964 char *argptr = arg;
9965 int hasColon = 0;
9966
9967 /* Look for a ':'. If this is a line number specification, then
9968 say it is bad, otherwise, it should be an address or
9969 function/method name. */
9970 while (*argptr && !hasColon)
9971 {
9972 hasColon = (*argptr == ':');
9973 argptr++;
9974 }
9975
9976 if (hasColon)
9977 badInput = (*argptr != ':'); /* Not a class::method */
9978 else
9979 badInput = isdigit (*arg); /* a simple line number */
9980 }
9981
9982 if (badInput)
9983 printf_filtered (_("Usage: stop in <function | address>\n"));
9984 else
9985 break_command_1 (arg, 0, from_tty);
9986 }
9987
9988 static void
9989 stopat_command (char *arg, int from_tty)
9990 {
9991 int badInput = 0;
9992
9993 if (arg == (char *) NULL || *arg == '*') /* no line number */
9994 badInput = 1;
9995 else
9996 {
9997 char *argptr = arg;
9998 int hasColon = 0;
9999
10000 /* Look for a ':'. If there is a '::' then get out, otherwise
10001 it is probably a line number. */
10002 while (*argptr && !hasColon)
10003 {
10004 hasColon = (*argptr == ':');
10005 argptr++;
10006 }
10007
10008 if (hasColon)
10009 badInput = (*argptr == ':'); /* we have class::method */
10010 else
10011 badInput = !isdigit (*arg); /* not a line number */
10012 }
10013
10014 if (badInput)
10015 printf_filtered (_("Usage: stop at <line>\n"));
10016 else
10017 break_command_1 (arg, 0, from_tty);
10018 }
10019
10020 /* The dynamic printf command is mostly like a regular breakpoint, but
10021 with a prewired command list consisting of a single output command,
10022 built from extra arguments supplied on the dprintf command
10023 line. */
10024
10025 static void
10026 dprintf_command (char *arg, int from_tty)
10027 {
10028 create_breakpoint (get_current_arch (),
10029 arg,
10030 NULL, 0, NULL, 1 /* parse arg */,
10031 0, bp_dprintf,
10032 0 /* Ignore count */,
10033 pending_break_support,
10034 &dprintf_breakpoint_ops,
10035 from_tty,
10036 1 /* enabled */,
10037 0 /* internal */,
10038 0);
10039 }
10040
10041 static void
10042 agent_printf_command (char *arg, int from_tty)
10043 {
10044 error (_("May only run agent-printf on the target"));
10045 }
10046
10047 /* Implement the "breakpoint_hit" breakpoint_ops method for
10048 ranged breakpoints. */
10049
10050 static int
10051 breakpoint_hit_ranged_breakpoint (const struct bp_location *bl,
10052 struct address_space *aspace,
10053 CORE_ADDR bp_addr,
10054 const struct target_waitstatus *ws)
10055 {
10056 if (ws->kind != TARGET_WAITKIND_STOPPED
10057 || ws->value.sig != GDB_SIGNAL_TRAP)
10058 return 0;
10059
10060 return breakpoint_address_match_range (bl->pspace->aspace, bl->address,
10061 bl->length, aspace, bp_addr);
10062 }
10063
10064 /* Implement the "resources_needed" breakpoint_ops method for
10065 ranged breakpoints. */
10066
10067 static int
10068 resources_needed_ranged_breakpoint (const struct bp_location *bl)
10069 {
10070 return target_ranged_break_num_registers ();
10071 }
10072
10073 /* Implement the "print_it" breakpoint_ops method for
10074 ranged breakpoints. */
10075
10076 static enum print_stop_action
10077 print_it_ranged_breakpoint (bpstat bs)
10078 {
10079 struct breakpoint *b = bs->breakpoint_at;
10080 struct bp_location *bl = b->loc;
10081 struct ui_out *uiout = current_uiout;
10082
10083 gdb_assert (b->type == bp_hardware_breakpoint);
10084
10085 /* Ranged breakpoints have only one location. */
10086 gdb_assert (bl && bl->next == NULL);
10087
10088 annotate_breakpoint (b->number);
10089 if (b->disposition == disp_del)
10090 ui_out_text (uiout, "\nTemporary ranged breakpoint ");
10091 else
10092 ui_out_text (uiout, "\nRanged breakpoint ");
10093 if (ui_out_is_mi_like_p (uiout))
10094 {
10095 ui_out_field_string (uiout, "reason",
10096 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
10097 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
10098 }
10099 ui_out_field_int (uiout, "bkptno", b->number);
10100 ui_out_text (uiout, ", ");
10101
10102 return PRINT_SRC_AND_LOC;
10103 }
10104
10105 /* Implement the "print_one" breakpoint_ops method for
10106 ranged breakpoints. */
10107
10108 static void
10109 print_one_ranged_breakpoint (struct breakpoint *b,
10110 struct bp_location **last_loc)
10111 {
10112 struct bp_location *bl = b->loc;
10113 struct value_print_options opts;
10114 struct ui_out *uiout = current_uiout;
10115
10116 /* Ranged breakpoints have only one location. */
10117 gdb_assert (bl && bl->next == NULL);
10118
10119 get_user_print_options (&opts);
10120
10121 if (opts.addressprint)
10122 /* We don't print the address range here, it will be printed later
10123 by print_one_detail_ranged_breakpoint. */
10124 ui_out_field_skip (uiout, "addr");
10125 annotate_field (5);
10126 print_breakpoint_location (b, bl);
10127 *last_loc = bl;
10128 }
10129
10130 /* Implement the "print_one_detail" breakpoint_ops method for
10131 ranged breakpoints. */
10132
10133 static void
10134 print_one_detail_ranged_breakpoint (const struct breakpoint *b,
10135 struct ui_out *uiout)
10136 {
10137 CORE_ADDR address_start, address_end;
10138 struct bp_location *bl = b->loc;
10139 struct ui_file *stb = mem_fileopen ();
10140 struct cleanup *cleanup = make_cleanup_ui_file_delete (stb);
10141
10142 gdb_assert (bl);
10143
10144 address_start = bl->address;
10145 address_end = address_start + bl->length - 1;
10146
10147 ui_out_text (uiout, "\taddress range: ");
10148 fprintf_unfiltered (stb, "[%s, %s]",
10149 print_core_address (bl->gdbarch, address_start),
10150 print_core_address (bl->gdbarch, address_end));
10151 ui_out_field_stream (uiout, "addr", stb);
10152 ui_out_text (uiout, "\n");
10153
10154 do_cleanups (cleanup);
10155 }
10156
10157 /* Implement the "print_mention" breakpoint_ops method for
10158 ranged breakpoints. */
10159
10160 static void
10161 print_mention_ranged_breakpoint (struct breakpoint *b)
10162 {
10163 struct bp_location *bl = b->loc;
10164 struct ui_out *uiout = current_uiout;
10165
10166 gdb_assert (bl);
10167 gdb_assert (b->type == bp_hardware_breakpoint);
10168
10169 if (ui_out_is_mi_like_p (uiout))
10170 return;
10171
10172 printf_filtered (_("Hardware assisted ranged breakpoint %d from %s to %s."),
10173 b->number, paddress (bl->gdbarch, bl->address),
10174 paddress (bl->gdbarch, bl->address + bl->length - 1));
10175 }
10176
10177 /* Implement the "print_recreate" breakpoint_ops method for
10178 ranged breakpoints. */
10179
10180 static void
10181 print_recreate_ranged_breakpoint (struct breakpoint *b, struct ui_file *fp)
10182 {
10183 fprintf_unfiltered (fp, "break-range %s, %s", b->addr_string,
10184 b->addr_string_range_end);
10185 print_recreate_thread (b, fp);
10186 }
10187
10188 /* The breakpoint_ops structure to be used in ranged breakpoints. */
10189
10190 static struct breakpoint_ops ranged_breakpoint_ops;
10191
10192 /* Find the address where the end of the breakpoint range should be
10193 placed, given the SAL of the end of the range. This is so that if
10194 the user provides a line number, the end of the range is set to the
10195 last instruction of the given line. */
10196
10197 static CORE_ADDR
10198 find_breakpoint_range_end (struct symtab_and_line sal)
10199 {
10200 CORE_ADDR end;
10201
10202 /* If the user provided a PC value, use it. Otherwise,
10203 find the address of the end of the given location. */
10204 if (sal.explicit_pc)
10205 end = sal.pc;
10206 else
10207 {
10208 int ret;
10209 CORE_ADDR start;
10210
10211 ret = find_line_pc_range (sal, &start, &end);
10212 if (!ret)
10213 error (_("Could not find location of the end of the range."));
10214
10215 /* find_line_pc_range returns the start of the next line. */
10216 end--;
10217 }
10218
10219 return end;
10220 }
10221
10222 /* Implement the "break-range" CLI command. */
10223
10224 static void
10225 break_range_command (char *arg, int from_tty)
10226 {
10227 char *arg_start, *addr_string_start, *addr_string_end;
10228 struct linespec_result canonical_start, canonical_end;
10229 int bp_count, can_use_bp, length;
10230 CORE_ADDR end;
10231 struct breakpoint *b;
10232 struct symtab_and_line sal_start, sal_end;
10233 struct cleanup *cleanup_bkpt;
10234 struct linespec_sals *lsal_start, *lsal_end;
10235
10236 /* We don't support software ranged breakpoints. */
10237 if (target_ranged_break_num_registers () < 0)
10238 error (_("This target does not support hardware ranged breakpoints."));
10239
10240 bp_count = hw_breakpoint_used_count ();
10241 bp_count += target_ranged_break_num_registers ();
10242 can_use_bp = target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
10243 bp_count, 0);
10244 if (can_use_bp < 0)
10245 error (_("Hardware breakpoints used exceeds limit."));
10246
10247 arg = skip_spaces (arg);
10248 if (arg == NULL || arg[0] == '\0')
10249 error(_("No address range specified."));
10250
10251 init_linespec_result (&canonical_start);
10252
10253 arg_start = arg;
10254 parse_breakpoint_sals (&arg, &canonical_start);
10255
10256 cleanup_bkpt = make_cleanup_destroy_linespec_result (&canonical_start);
10257
10258 if (arg[0] != ',')
10259 error (_("Too few arguments."));
10260 else if (VEC_empty (linespec_sals, canonical_start.sals))
10261 error (_("Could not find location of the beginning of the range."));
10262
10263 lsal_start = VEC_index (linespec_sals, canonical_start.sals, 0);
10264
10265 if (VEC_length (linespec_sals, canonical_start.sals) > 1
10266 || lsal_start->sals.nelts != 1)
10267 error (_("Cannot create a ranged breakpoint with multiple locations."));
10268
10269 sal_start = lsal_start->sals.sals[0];
10270 addr_string_start = savestring (arg_start, arg - arg_start);
10271 make_cleanup (xfree, addr_string_start);
10272
10273 arg++; /* Skip the comma. */
10274 arg = skip_spaces (arg);
10275
10276 /* Parse the end location. */
10277
10278 init_linespec_result (&canonical_end);
10279 arg_start = arg;
10280
10281 /* We call decode_line_full directly here instead of using
10282 parse_breakpoint_sals because we need to specify the start location's
10283 symtab and line as the default symtab and line for the end of the
10284 range. This makes it possible to have ranges like "foo.c:27, +14",
10285 where +14 means 14 lines from the start location. */
10286 decode_line_full (&arg, DECODE_LINE_FUNFIRSTLINE,
10287 sal_start.symtab, sal_start.line,
10288 &canonical_end, NULL, NULL);
10289
10290 make_cleanup_destroy_linespec_result (&canonical_end);
10291
10292 if (VEC_empty (linespec_sals, canonical_end.sals))
10293 error (_("Could not find location of the end of the range."));
10294
10295 lsal_end = VEC_index (linespec_sals, canonical_end.sals, 0);
10296 if (VEC_length (linespec_sals, canonical_end.sals) > 1
10297 || lsal_end->sals.nelts != 1)
10298 error (_("Cannot create a ranged breakpoint with multiple locations."));
10299
10300 sal_end = lsal_end->sals.sals[0];
10301 addr_string_end = savestring (arg_start, arg - arg_start);
10302 make_cleanup (xfree, addr_string_end);
10303
10304 end = find_breakpoint_range_end (sal_end);
10305 if (sal_start.pc > end)
10306 error (_("Invalid address range, end precedes start."));
10307
10308 length = end - sal_start.pc + 1;
10309 if (length < 0)
10310 /* Length overflowed. */
10311 error (_("Address range too large."));
10312 else if (length == 1)
10313 {
10314 /* This range is simple enough to be handled by
10315 the `hbreak' command. */
10316 hbreak_command (addr_string_start, 1);
10317
10318 do_cleanups (cleanup_bkpt);
10319
10320 return;
10321 }
10322
10323 /* Now set up the breakpoint. */
10324 b = set_raw_breakpoint (get_current_arch (), sal_start,
10325 bp_hardware_breakpoint, &ranged_breakpoint_ops);
10326 set_breakpoint_count (breakpoint_count + 1);
10327 b->number = breakpoint_count;
10328 b->disposition = disp_donttouch;
10329 b->addr_string = xstrdup (addr_string_start);
10330 b->addr_string_range_end = xstrdup (addr_string_end);
10331 b->loc->length = length;
10332
10333 do_cleanups (cleanup_bkpt);
10334
10335 mention (b);
10336 observer_notify_breakpoint_created (b);
10337 update_global_location_list (UGLL_MAY_INSERT);
10338 }
10339
10340 /* Return non-zero if EXP is verified as constant. Returned zero
10341 means EXP is variable. Also the constant detection may fail for
10342 some constant expressions and in such case still falsely return
10343 zero. */
10344
10345 static int
10346 watchpoint_exp_is_const (const struct expression *exp)
10347 {
10348 int i = exp->nelts;
10349
10350 while (i > 0)
10351 {
10352 int oplenp, argsp;
10353
10354 /* We are only interested in the descriptor of each element. */
10355 operator_length (exp, i, &oplenp, &argsp);
10356 i -= oplenp;
10357
10358 switch (exp->elts[i].opcode)
10359 {
10360 case BINOP_ADD:
10361 case BINOP_SUB:
10362 case BINOP_MUL:
10363 case BINOP_DIV:
10364 case BINOP_REM:
10365 case BINOP_MOD:
10366 case BINOP_LSH:
10367 case BINOP_RSH:
10368 case BINOP_LOGICAL_AND:
10369 case BINOP_LOGICAL_OR:
10370 case BINOP_BITWISE_AND:
10371 case BINOP_BITWISE_IOR:
10372 case BINOP_BITWISE_XOR:
10373 case BINOP_EQUAL:
10374 case BINOP_NOTEQUAL:
10375 case BINOP_LESS:
10376 case BINOP_GTR:
10377 case BINOP_LEQ:
10378 case BINOP_GEQ:
10379 case BINOP_REPEAT:
10380 case BINOP_COMMA:
10381 case BINOP_EXP:
10382 case BINOP_MIN:
10383 case BINOP_MAX:
10384 case BINOP_INTDIV:
10385 case BINOP_CONCAT:
10386 case TERNOP_COND:
10387 case TERNOP_SLICE:
10388
10389 case OP_LONG:
10390 case OP_DOUBLE:
10391 case OP_DECFLOAT:
10392 case OP_LAST:
10393 case OP_COMPLEX:
10394 case OP_STRING:
10395 case OP_ARRAY:
10396 case OP_TYPE:
10397 case OP_TYPEOF:
10398 case OP_DECLTYPE:
10399 case OP_TYPEID:
10400 case OP_NAME:
10401 case OP_OBJC_NSSTRING:
10402
10403 case UNOP_NEG:
10404 case UNOP_LOGICAL_NOT:
10405 case UNOP_COMPLEMENT:
10406 case UNOP_ADDR:
10407 case UNOP_HIGH:
10408 case UNOP_CAST:
10409
10410 case UNOP_CAST_TYPE:
10411 case UNOP_REINTERPRET_CAST:
10412 case UNOP_DYNAMIC_CAST:
10413 /* Unary, binary and ternary operators: We have to check
10414 their operands. If they are constant, then so is the
10415 result of that operation. For instance, if A and B are
10416 determined to be constants, then so is "A + B".
10417
10418 UNOP_IND is one exception to the rule above, because the
10419 value of *ADDR is not necessarily a constant, even when
10420 ADDR is. */
10421 break;
10422
10423 case OP_VAR_VALUE:
10424 /* Check whether the associated symbol is a constant.
10425
10426 We use SYMBOL_CLASS rather than TYPE_CONST because it's
10427 possible that a buggy compiler could mark a variable as
10428 constant even when it is not, and TYPE_CONST would return
10429 true in this case, while SYMBOL_CLASS wouldn't.
10430
10431 We also have to check for function symbols because they
10432 are always constant. */
10433 {
10434 struct symbol *s = exp->elts[i + 2].symbol;
10435
10436 if (SYMBOL_CLASS (s) != LOC_BLOCK
10437 && SYMBOL_CLASS (s) != LOC_CONST
10438 && SYMBOL_CLASS (s) != LOC_CONST_BYTES)
10439 return 0;
10440 break;
10441 }
10442
10443 /* The default action is to return 0 because we are using
10444 the optimistic approach here: If we don't know something,
10445 then it is not a constant. */
10446 default:
10447 return 0;
10448 }
10449 }
10450
10451 return 1;
10452 }
10453
10454 /* Implement the "dtor" breakpoint_ops method for watchpoints. */
10455
10456 static void
10457 dtor_watchpoint (struct breakpoint *self)
10458 {
10459 struct watchpoint *w = (struct watchpoint *) self;
10460
10461 xfree (w->cond_exp);
10462 xfree (w->exp);
10463 xfree (w->exp_string);
10464 xfree (w->exp_string_reparse);
10465 value_free (w->val);
10466
10467 base_breakpoint_ops.dtor (self);
10468 }
10469
10470 /* Implement the "re_set" breakpoint_ops method for watchpoints. */
10471
10472 static void
10473 re_set_watchpoint (struct breakpoint *b)
10474 {
10475 struct watchpoint *w = (struct watchpoint *) b;
10476
10477 /* Watchpoint can be either on expression using entirely global
10478 variables, or it can be on local variables.
10479
10480 Watchpoints of the first kind are never auto-deleted, and even
10481 persist across program restarts. Since they can use variables
10482 from shared libraries, we need to reparse expression as libraries
10483 are loaded and unloaded.
10484
10485 Watchpoints on local variables can also change meaning as result
10486 of solib event. For example, if a watchpoint uses both a local
10487 and a global variables in expression, it's a local watchpoint,
10488 but unloading of a shared library will make the expression
10489 invalid. This is not a very common use case, but we still
10490 re-evaluate expression, to avoid surprises to the user.
10491
10492 Note that for local watchpoints, we re-evaluate it only if
10493 watchpoints frame id is still valid. If it's not, it means the
10494 watchpoint is out of scope and will be deleted soon. In fact,
10495 I'm not sure we'll ever be called in this case.
10496
10497 If a local watchpoint's frame id is still valid, then
10498 w->exp_valid_block is likewise valid, and we can safely use it.
10499
10500 Don't do anything about disabled watchpoints, since they will be
10501 reevaluated again when enabled. */
10502 update_watchpoint (w, 1 /* reparse */);
10503 }
10504
10505 /* Implement the "insert" breakpoint_ops method for hardware watchpoints. */
10506
10507 static int
10508 insert_watchpoint (struct bp_location *bl)
10509 {
10510 struct watchpoint *w = (struct watchpoint *) bl->owner;
10511 int length = w->exact ? 1 : bl->length;
10512
10513 return target_insert_watchpoint (bl->address, length, bl->watchpoint_type,
10514 w->cond_exp);
10515 }
10516
10517 /* Implement the "remove" breakpoint_ops method for hardware watchpoints. */
10518
10519 static int
10520 remove_watchpoint (struct bp_location *bl)
10521 {
10522 struct watchpoint *w = (struct watchpoint *) bl->owner;
10523 int length = w->exact ? 1 : bl->length;
10524
10525 return target_remove_watchpoint (bl->address, length, bl->watchpoint_type,
10526 w->cond_exp);
10527 }
10528
10529 static int
10530 breakpoint_hit_watchpoint (const struct bp_location *bl,
10531 struct address_space *aspace, CORE_ADDR bp_addr,
10532 const struct target_waitstatus *ws)
10533 {
10534 struct breakpoint *b = bl->owner;
10535 struct watchpoint *w = (struct watchpoint *) b;
10536
10537 /* Continuable hardware watchpoints are treated as non-existent if the
10538 reason we stopped wasn't a hardware watchpoint (we didn't stop on
10539 some data address). Otherwise gdb won't stop on a break instruction
10540 in the code (not from a breakpoint) when a hardware watchpoint has
10541 been defined. Also skip watchpoints which we know did not trigger
10542 (did not match the data address). */
10543 if (is_hardware_watchpoint (b)
10544 && w->watchpoint_triggered == watch_triggered_no)
10545 return 0;
10546
10547 return 1;
10548 }
10549
10550 static void
10551 check_status_watchpoint (bpstat bs)
10552 {
10553 gdb_assert (is_watchpoint (bs->breakpoint_at));
10554
10555 bpstat_check_watchpoint (bs);
10556 }
10557
10558 /* Implement the "resources_needed" breakpoint_ops method for
10559 hardware watchpoints. */
10560
10561 static int
10562 resources_needed_watchpoint (const struct bp_location *bl)
10563 {
10564 struct watchpoint *w = (struct watchpoint *) bl->owner;
10565 int length = w->exact? 1 : bl->length;
10566
10567 return target_region_ok_for_hw_watchpoint (bl->address, length);
10568 }
10569
10570 /* Implement the "works_in_software_mode" breakpoint_ops method for
10571 hardware watchpoints. */
10572
10573 static int
10574 works_in_software_mode_watchpoint (const struct breakpoint *b)
10575 {
10576 /* Read and access watchpoints only work with hardware support. */
10577 return b->type == bp_watchpoint || b->type == bp_hardware_watchpoint;
10578 }
10579
10580 static enum print_stop_action
10581 print_it_watchpoint (bpstat bs)
10582 {
10583 struct cleanup *old_chain;
10584 struct breakpoint *b;
10585 struct ui_file *stb;
10586 enum print_stop_action result;
10587 struct watchpoint *w;
10588 struct ui_out *uiout = current_uiout;
10589
10590 gdb_assert (bs->bp_location_at != NULL);
10591
10592 b = bs->breakpoint_at;
10593 w = (struct watchpoint *) b;
10594
10595 stb = mem_fileopen ();
10596 old_chain = make_cleanup_ui_file_delete (stb);
10597
10598 switch (b->type)
10599 {
10600 case bp_watchpoint:
10601 case bp_hardware_watchpoint:
10602 annotate_watchpoint (b->number);
10603 if (ui_out_is_mi_like_p (uiout))
10604 ui_out_field_string
10605 (uiout, "reason",
10606 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10607 mention (b);
10608 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10609 ui_out_text (uiout, "\nOld value = ");
10610 watchpoint_value_print (bs->old_val, stb);
10611 ui_out_field_stream (uiout, "old", stb);
10612 ui_out_text (uiout, "\nNew value = ");
10613 watchpoint_value_print (w->val, stb);
10614 ui_out_field_stream (uiout, "new", stb);
10615 ui_out_text (uiout, "\n");
10616 /* More than one watchpoint may have been triggered. */
10617 result = PRINT_UNKNOWN;
10618 break;
10619
10620 case bp_read_watchpoint:
10621 if (ui_out_is_mi_like_p (uiout))
10622 ui_out_field_string
10623 (uiout, "reason",
10624 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10625 mention (b);
10626 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10627 ui_out_text (uiout, "\nValue = ");
10628 watchpoint_value_print (w->val, stb);
10629 ui_out_field_stream (uiout, "value", stb);
10630 ui_out_text (uiout, "\n");
10631 result = PRINT_UNKNOWN;
10632 break;
10633
10634 case bp_access_watchpoint:
10635 if (bs->old_val != NULL)
10636 {
10637 annotate_watchpoint (b->number);
10638 if (ui_out_is_mi_like_p (uiout))
10639 ui_out_field_string
10640 (uiout, "reason",
10641 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10642 mention (b);
10643 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10644 ui_out_text (uiout, "\nOld value = ");
10645 watchpoint_value_print (bs->old_val, stb);
10646 ui_out_field_stream (uiout, "old", stb);
10647 ui_out_text (uiout, "\nNew value = ");
10648 }
10649 else
10650 {
10651 mention (b);
10652 if (ui_out_is_mi_like_p (uiout))
10653 ui_out_field_string
10654 (uiout, "reason",
10655 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10656 make_cleanup_ui_out_tuple_begin_end (uiout, "value");
10657 ui_out_text (uiout, "\nValue = ");
10658 }
10659 watchpoint_value_print (w->val, stb);
10660 ui_out_field_stream (uiout, "new", stb);
10661 ui_out_text (uiout, "\n");
10662 result = PRINT_UNKNOWN;
10663 break;
10664 default:
10665 result = PRINT_UNKNOWN;
10666 }
10667
10668 do_cleanups (old_chain);
10669 return result;
10670 }
10671
10672 /* Implement the "print_mention" breakpoint_ops method for hardware
10673 watchpoints. */
10674
10675 static void
10676 print_mention_watchpoint (struct breakpoint *b)
10677 {
10678 struct cleanup *ui_out_chain;
10679 struct watchpoint *w = (struct watchpoint *) b;
10680 struct ui_out *uiout = current_uiout;
10681
10682 switch (b->type)
10683 {
10684 case bp_watchpoint:
10685 ui_out_text (uiout, "Watchpoint ");
10686 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10687 break;
10688 case bp_hardware_watchpoint:
10689 ui_out_text (uiout, "Hardware watchpoint ");
10690 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10691 break;
10692 case bp_read_watchpoint:
10693 ui_out_text (uiout, "Hardware read watchpoint ");
10694 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
10695 break;
10696 case bp_access_watchpoint:
10697 ui_out_text (uiout, "Hardware access (read/write) watchpoint ");
10698 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
10699 break;
10700 default:
10701 internal_error (__FILE__, __LINE__,
10702 _("Invalid hardware watchpoint type."));
10703 }
10704
10705 ui_out_field_int (uiout, "number", b->number);
10706 ui_out_text (uiout, ": ");
10707 ui_out_field_string (uiout, "exp", w->exp_string);
10708 do_cleanups (ui_out_chain);
10709 }
10710
10711 /* Implement the "print_recreate" breakpoint_ops method for
10712 watchpoints. */
10713
10714 static void
10715 print_recreate_watchpoint (struct breakpoint *b, struct ui_file *fp)
10716 {
10717 struct watchpoint *w = (struct watchpoint *) b;
10718
10719 switch (b->type)
10720 {
10721 case bp_watchpoint:
10722 case bp_hardware_watchpoint:
10723 fprintf_unfiltered (fp, "watch");
10724 break;
10725 case bp_read_watchpoint:
10726 fprintf_unfiltered (fp, "rwatch");
10727 break;
10728 case bp_access_watchpoint:
10729 fprintf_unfiltered (fp, "awatch");
10730 break;
10731 default:
10732 internal_error (__FILE__, __LINE__,
10733 _("Invalid watchpoint type."));
10734 }
10735
10736 fprintf_unfiltered (fp, " %s", w->exp_string);
10737 print_recreate_thread (b, fp);
10738 }
10739
10740 /* Implement the "explains_signal" breakpoint_ops method for
10741 watchpoints. */
10742
10743 static int
10744 explains_signal_watchpoint (struct breakpoint *b, enum gdb_signal sig)
10745 {
10746 /* A software watchpoint cannot cause a signal other than
10747 GDB_SIGNAL_TRAP. */
10748 if (b->type == bp_watchpoint && sig != GDB_SIGNAL_TRAP)
10749 return 0;
10750
10751 return 1;
10752 }
10753
10754 /* The breakpoint_ops structure to be used in hardware watchpoints. */
10755
10756 static struct breakpoint_ops watchpoint_breakpoint_ops;
10757
10758 /* Implement the "insert" breakpoint_ops method for
10759 masked hardware watchpoints. */
10760
10761 static int
10762 insert_masked_watchpoint (struct bp_location *bl)
10763 {
10764 struct watchpoint *w = (struct watchpoint *) bl->owner;
10765
10766 return target_insert_mask_watchpoint (bl->address, w->hw_wp_mask,
10767 bl->watchpoint_type);
10768 }
10769
10770 /* Implement the "remove" breakpoint_ops method for
10771 masked hardware watchpoints. */
10772
10773 static int
10774 remove_masked_watchpoint (struct bp_location *bl)
10775 {
10776 struct watchpoint *w = (struct watchpoint *) bl->owner;
10777
10778 return target_remove_mask_watchpoint (bl->address, w->hw_wp_mask,
10779 bl->watchpoint_type);
10780 }
10781
10782 /* Implement the "resources_needed" breakpoint_ops method for
10783 masked hardware watchpoints. */
10784
10785 static int
10786 resources_needed_masked_watchpoint (const struct bp_location *bl)
10787 {
10788 struct watchpoint *w = (struct watchpoint *) bl->owner;
10789
10790 return target_masked_watch_num_registers (bl->address, w->hw_wp_mask);
10791 }
10792
10793 /* Implement the "works_in_software_mode" breakpoint_ops method for
10794 masked hardware watchpoints. */
10795
10796 static int
10797 works_in_software_mode_masked_watchpoint (const struct breakpoint *b)
10798 {
10799 return 0;
10800 }
10801
10802 /* Implement the "print_it" breakpoint_ops method for
10803 masked hardware watchpoints. */
10804
10805 static enum print_stop_action
10806 print_it_masked_watchpoint (bpstat bs)
10807 {
10808 struct breakpoint *b = bs->breakpoint_at;
10809 struct ui_out *uiout = current_uiout;
10810
10811 /* Masked watchpoints have only one location. */
10812 gdb_assert (b->loc && b->loc->next == NULL);
10813
10814 switch (b->type)
10815 {
10816 case bp_hardware_watchpoint:
10817 annotate_watchpoint (b->number);
10818 if (ui_out_is_mi_like_p (uiout))
10819 ui_out_field_string
10820 (uiout, "reason",
10821 async_reason_lookup (EXEC_ASYNC_WATCHPOINT_TRIGGER));
10822 break;
10823
10824 case bp_read_watchpoint:
10825 if (ui_out_is_mi_like_p (uiout))
10826 ui_out_field_string
10827 (uiout, "reason",
10828 async_reason_lookup (EXEC_ASYNC_READ_WATCHPOINT_TRIGGER));
10829 break;
10830
10831 case bp_access_watchpoint:
10832 if (ui_out_is_mi_like_p (uiout))
10833 ui_out_field_string
10834 (uiout, "reason",
10835 async_reason_lookup (EXEC_ASYNC_ACCESS_WATCHPOINT_TRIGGER));
10836 break;
10837 default:
10838 internal_error (__FILE__, __LINE__,
10839 _("Invalid hardware watchpoint type."));
10840 }
10841
10842 mention (b);
10843 ui_out_text (uiout, _("\n\
10844 Check the underlying instruction at PC for the memory\n\
10845 address and value which triggered this watchpoint.\n"));
10846 ui_out_text (uiout, "\n");
10847
10848 /* More than one watchpoint may have been triggered. */
10849 return PRINT_UNKNOWN;
10850 }
10851
10852 /* Implement the "print_one_detail" breakpoint_ops method for
10853 masked hardware watchpoints. */
10854
10855 static void
10856 print_one_detail_masked_watchpoint (const struct breakpoint *b,
10857 struct ui_out *uiout)
10858 {
10859 struct watchpoint *w = (struct watchpoint *) b;
10860
10861 /* Masked watchpoints have only one location. */
10862 gdb_assert (b->loc && b->loc->next == NULL);
10863
10864 ui_out_text (uiout, "\tmask ");
10865 ui_out_field_core_addr (uiout, "mask", b->loc->gdbarch, w->hw_wp_mask);
10866 ui_out_text (uiout, "\n");
10867 }
10868
10869 /* Implement the "print_mention" breakpoint_ops method for
10870 masked hardware watchpoints. */
10871
10872 static void
10873 print_mention_masked_watchpoint (struct breakpoint *b)
10874 {
10875 struct watchpoint *w = (struct watchpoint *) b;
10876 struct ui_out *uiout = current_uiout;
10877 struct cleanup *ui_out_chain;
10878
10879 switch (b->type)
10880 {
10881 case bp_hardware_watchpoint:
10882 ui_out_text (uiout, "Masked hardware watchpoint ");
10883 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "wpt");
10884 break;
10885 case bp_read_watchpoint:
10886 ui_out_text (uiout, "Masked hardware read watchpoint ");
10887 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-rwpt");
10888 break;
10889 case bp_access_watchpoint:
10890 ui_out_text (uiout, "Masked hardware access (read/write) watchpoint ");
10891 ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, "hw-awpt");
10892 break;
10893 default:
10894 internal_error (__FILE__, __LINE__,
10895 _("Invalid hardware watchpoint type."));
10896 }
10897
10898 ui_out_field_int (uiout, "number", b->number);
10899 ui_out_text (uiout, ": ");
10900 ui_out_field_string (uiout, "exp", w->exp_string);
10901 do_cleanups (ui_out_chain);
10902 }
10903
10904 /* Implement the "print_recreate" breakpoint_ops method for
10905 masked hardware watchpoints. */
10906
10907 static void
10908 print_recreate_masked_watchpoint (struct breakpoint *b, struct ui_file *fp)
10909 {
10910 struct watchpoint *w = (struct watchpoint *) b;
10911 char tmp[40];
10912
10913 switch (b->type)
10914 {
10915 case bp_hardware_watchpoint:
10916 fprintf_unfiltered (fp, "watch");
10917 break;
10918 case bp_read_watchpoint:
10919 fprintf_unfiltered (fp, "rwatch");
10920 break;
10921 case bp_access_watchpoint:
10922 fprintf_unfiltered (fp, "awatch");
10923 break;
10924 default:
10925 internal_error (__FILE__, __LINE__,
10926 _("Invalid hardware watchpoint type."));
10927 }
10928
10929 sprintf_vma (tmp, w->hw_wp_mask);
10930 fprintf_unfiltered (fp, " %s mask 0x%s", w->exp_string, tmp);
10931 print_recreate_thread (b, fp);
10932 }
10933
10934 /* The breakpoint_ops structure to be used in masked hardware watchpoints. */
10935
10936 static struct breakpoint_ops masked_watchpoint_breakpoint_ops;
10937
10938 /* Tell whether the given watchpoint is a masked hardware watchpoint. */
10939
10940 static int
10941 is_masked_watchpoint (const struct breakpoint *b)
10942 {
10943 return b->ops == &masked_watchpoint_breakpoint_ops;
10944 }
10945
10946 /* accessflag: hw_write: watch write,
10947 hw_read: watch read,
10948 hw_access: watch access (read or write) */
10949 static void
10950 watch_command_1 (const char *arg, int accessflag, int from_tty,
10951 int just_location, int internal)
10952 {
10953 struct breakpoint *b, *scope_breakpoint = NULL;
10954 struct expression *exp;
10955 const struct block *exp_valid_block = NULL, *cond_exp_valid_block = NULL;
10956 struct value *val, *mark, *result;
10957 int saved_bitpos = 0, saved_bitsize = 0;
10958 struct frame_info *frame;
10959 const char *exp_start = NULL;
10960 const char *exp_end = NULL;
10961 const char *tok, *end_tok;
10962 int toklen = -1;
10963 const char *cond_start = NULL;
10964 const char *cond_end = NULL;
10965 enum bptype bp_type;
10966 int thread = -1;
10967 int pc = 0;
10968 /* Flag to indicate whether we are going to use masks for
10969 the hardware watchpoint. */
10970 int use_mask = 0;
10971 CORE_ADDR mask = 0;
10972 struct watchpoint *w;
10973 char *expression;
10974 struct cleanup *back_to;
10975
10976 /* Make sure that we actually have parameters to parse. */
10977 if (arg != NULL && arg[0] != '\0')
10978 {
10979 const char *value_start;
10980
10981 exp_end = arg + strlen (arg);
10982
10983 /* Look for "parameter value" pairs at the end
10984 of the arguments string. */
10985 for (tok = exp_end - 1; tok > arg; tok--)
10986 {
10987 /* Skip whitespace at the end of the argument list. */
10988 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10989 tok--;
10990
10991 /* Find the beginning of the last token.
10992 This is the value of the parameter. */
10993 while (tok > arg && (*tok != ' ' && *tok != '\t'))
10994 tok--;
10995 value_start = tok + 1;
10996
10997 /* Skip whitespace. */
10998 while (tok > arg && (*tok == ' ' || *tok == '\t'))
10999 tok--;
11000
11001 end_tok = tok;
11002
11003 /* Find the beginning of the second to last token.
11004 This is the parameter itself. */
11005 while (tok > arg && (*tok != ' ' && *tok != '\t'))
11006 tok--;
11007 tok++;
11008 toklen = end_tok - tok + 1;
11009
11010 if (toklen == 6 && startswith (tok, "thread"))
11011 {
11012 /* At this point we've found a "thread" token, which means
11013 the user is trying to set a watchpoint that triggers
11014 only in a specific thread. */
11015 char *endp;
11016
11017 if (thread != -1)
11018 error(_("You can specify only one thread."));
11019
11020 /* Extract the thread ID from the next token. */
11021 thread = strtol (value_start, &endp, 0);
11022
11023 /* Check if the user provided a valid numeric value for the
11024 thread ID. */
11025 if (*endp != ' ' && *endp != '\t' && *endp != '\0')
11026 error (_("Invalid thread ID specification %s."), value_start);
11027
11028 /* Check if the thread actually exists. */
11029 if (!valid_thread_id (thread))
11030 invalid_thread_id_error (thread);
11031 }
11032 else if (toklen == 4 && startswith (tok, "mask"))
11033 {
11034 /* We've found a "mask" token, which means the user wants to
11035 create a hardware watchpoint that is going to have the mask
11036 facility. */
11037 struct value *mask_value, *mark;
11038
11039 if (use_mask)
11040 error(_("You can specify only one mask."));
11041
11042 use_mask = just_location = 1;
11043
11044 mark = value_mark ();
11045 mask_value = parse_to_comma_and_eval (&value_start);
11046 mask = value_as_address (mask_value);
11047 value_free_to_mark (mark);
11048 }
11049 else
11050 /* We didn't recognize what we found. We should stop here. */
11051 break;
11052
11053 /* Truncate the string and get rid of the "parameter value" pair before
11054 the arguments string is parsed by the parse_exp_1 function. */
11055 exp_end = tok;
11056 }
11057 }
11058 else
11059 exp_end = arg;
11060
11061 /* Parse the rest of the arguments. From here on out, everything
11062 is in terms of a newly allocated string instead of the original
11063 ARG. */
11064 innermost_block = NULL;
11065 expression = savestring (arg, exp_end - arg);
11066 back_to = make_cleanup (xfree, expression);
11067 exp_start = arg = expression;
11068 exp = parse_exp_1 (&arg, 0, 0, 0);
11069 exp_end = arg;
11070 /* Remove trailing whitespace from the expression before saving it.
11071 This makes the eventual display of the expression string a bit
11072 prettier. */
11073 while (exp_end > exp_start && (exp_end[-1] == ' ' || exp_end[-1] == '\t'))
11074 --exp_end;
11075
11076 /* Checking if the expression is not constant. */
11077 if (watchpoint_exp_is_const (exp))
11078 {
11079 int len;
11080
11081 len = exp_end - exp_start;
11082 while (len > 0 && isspace (exp_start[len - 1]))
11083 len--;
11084 error (_("Cannot watch constant value `%.*s'."), len, exp_start);
11085 }
11086
11087 exp_valid_block = innermost_block;
11088 mark = value_mark ();
11089 fetch_subexp_value (exp, &pc, &val, &result, NULL, just_location);
11090
11091 if (val != NULL && just_location)
11092 {
11093 saved_bitpos = value_bitpos (val);
11094 saved_bitsize = value_bitsize (val);
11095 }
11096
11097 if (just_location)
11098 {
11099 int ret;
11100
11101 exp_valid_block = NULL;
11102 val = value_addr (result);
11103 release_value (val);
11104 value_free_to_mark (mark);
11105
11106 if (use_mask)
11107 {
11108 ret = target_masked_watch_num_registers (value_as_address (val),
11109 mask);
11110 if (ret == -1)
11111 error (_("This target does not support masked watchpoints."));
11112 else if (ret == -2)
11113 error (_("Invalid mask or memory region."));
11114 }
11115 }
11116 else if (val != NULL)
11117 release_value (val);
11118
11119 tok = skip_spaces_const (arg);
11120 end_tok = skip_to_space_const (tok);
11121
11122 toklen = end_tok - tok;
11123 if (toklen >= 1 && strncmp (tok, "if", toklen) == 0)
11124 {
11125 struct expression *cond;
11126
11127 innermost_block = NULL;
11128 tok = cond_start = end_tok + 1;
11129 cond = parse_exp_1 (&tok, 0, 0, 0);
11130
11131 /* The watchpoint expression may not be local, but the condition
11132 may still be. E.g.: `watch global if local > 0'. */
11133 cond_exp_valid_block = innermost_block;
11134
11135 xfree (cond);
11136 cond_end = tok;
11137 }
11138 if (*tok)
11139 error (_("Junk at end of command."));
11140
11141 frame = block_innermost_frame (exp_valid_block);
11142
11143 /* If the expression is "local", then set up a "watchpoint scope"
11144 breakpoint at the point where we've left the scope of the watchpoint
11145 expression. Create the scope breakpoint before the watchpoint, so
11146 that we will encounter it first in bpstat_stop_status. */
11147 if (exp_valid_block && frame)
11148 {
11149 if (frame_id_p (frame_unwind_caller_id (frame)))
11150 {
11151 scope_breakpoint
11152 = create_internal_breakpoint (frame_unwind_caller_arch (frame),
11153 frame_unwind_caller_pc (frame),
11154 bp_watchpoint_scope,
11155 &momentary_breakpoint_ops);
11156
11157 scope_breakpoint->enable_state = bp_enabled;
11158
11159 /* Automatically delete the breakpoint when it hits. */
11160 scope_breakpoint->disposition = disp_del;
11161
11162 /* Only break in the proper frame (help with recursion). */
11163 scope_breakpoint->frame_id = frame_unwind_caller_id (frame);
11164
11165 /* Set the address at which we will stop. */
11166 scope_breakpoint->loc->gdbarch
11167 = frame_unwind_caller_arch (frame);
11168 scope_breakpoint->loc->requested_address
11169 = frame_unwind_caller_pc (frame);
11170 scope_breakpoint->loc->address
11171 = adjust_breakpoint_address (scope_breakpoint->loc->gdbarch,
11172 scope_breakpoint->loc->requested_address,
11173 scope_breakpoint->type);
11174 }
11175 }
11176
11177 /* Now set up the breakpoint. We create all watchpoints as hardware
11178 watchpoints here even if hardware watchpoints are turned off, a call
11179 to update_watchpoint later in this function will cause the type to
11180 drop back to bp_watchpoint (software watchpoint) if required. */
11181
11182 if (accessflag == hw_read)
11183 bp_type = bp_read_watchpoint;
11184 else if (accessflag == hw_access)
11185 bp_type = bp_access_watchpoint;
11186 else
11187 bp_type = bp_hardware_watchpoint;
11188
11189 w = XCNEW (struct watchpoint);
11190 b = &w->base;
11191 if (use_mask)
11192 init_raw_breakpoint_without_location (b, NULL, bp_type,
11193 &masked_watchpoint_breakpoint_ops);
11194 else
11195 init_raw_breakpoint_without_location (b, NULL, bp_type,
11196 &watchpoint_breakpoint_ops);
11197 b->thread = thread;
11198 b->disposition = disp_donttouch;
11199 b->pspace = current_program_space;
11200 w->exp = exp;
11201 w->exp_valid_block = exp_valid_block;
11202 w->cond_exp_valid_block = cond_exp_valid_block;
11203 if (just_location)
11204 {
11205 struct type *t = value_type (val);
11206 CORE_ADDR addr = value_as_address (val);
11207 char *name;
11208
11209 t = check_typedef (TYPE_TARGET_TYPE (check_typedef (t)));
11210 name = type_to_string (t);
11211
11212 w->exp_string_reparse = xstrprintf ("* (%s *) %s", name,
11213 core_addr_to_string (addr));
11214 xfree (name);
11215
11216 w->exp_string = xstrprintf ("-location %.*s",
11217 (int) (exp_end - exp_start), exp_start);
11218
11219 /* The above expression is in C. */
11220 b->language = language_c;
11221 }
11222 else
11223 w->exp_string = savestring (exp_start, exp_end - exp_start);
11224
11225 if (use_mask)
11226 {
11227 w->hw_wp_mask = mask;
11228 }
11229 else
11230 {
11231 w->val = val;
11232 w->val_bitpos = saved_bitpos;
11233 w->val_bitsize = saved_bitsize;
11234 w->val_valid = 1;
11235 }
11236
11237 if (cond_start)
11238 b->cond_string = savestring (cond_start, cond_end - cond_start);
11239 else
11240 b->cond_string = 0;
11241
11242 if (frame)
11243 {
11244 w->watchpoint_frame = get_frame_id (frame);
11245 w->watchpoint_thread = inferior_ptid;
11246 }
11247 else
11248 {
11249 w->watchpoint_frame = null_frame_id;
11250 w->watchpoint_thread = null_ptid;
11251 }
11252
11253 if (scope_breakpoint != NULL)
11254 {
11255 /* The scope breakpoint is related to the watchpoint. We will
11256 need to act on them together. */
11257 b->related_breakpoint = scope_breakpoint;
11258 scope_breakpoint->related_breakpoint = b;
11259 }
11260
11261 if (!just_location)
11262 value_free_to_mark (mark);
11263
11264 TRY
11265 {
11266 /* Finally update the new watchpoint. This creates the locations
11267 that should be inserted. */
11268 update_watchpoint (w, 1);
11269 }
11270 CATCH (e, RETURN_MASK_ALL)
11271 {
11272 delete_breakpoint (b);
11273 throw_exception (e);
11274 }
11275 END_CATCH
11276
11277 install_breakpoint (internal, b, 1);
11278 do_cleanups (back_to);
11279 }
11280
11281 /* Return count of debug registers needed to watch the given expression.
11282 If the watchpoint cannot be handled in hardware return zero. */
11283
11284 static int
11285 can_use_hardware_watchpoint (struct value *v)
11286 {
11287 int found_memory_cnt = 0;
11288 struct value *head = v;
11289
11290 /* Did the user specifically forbid us to use hardware watchpoints? */
11291 if (!can_use_hw_watchpoints)
11292 return 0;
11293
11294 /* Make sure that the value of the expression depends only upon
11295 memory contents, and values computed from them within GDB. If we
11296 find any register references or function calls, we can't use a
11297 hardware watchpoint.
11298
11299 The idea here is that evaluating an expression generates a series
11300 of values, one holding the value of every subexpression. (The
11301 expression a*b+c has five subexpressions: a, b, a*b, c, and
11302 a*b+c.) GDB's values hold almost enough information to establish
11303 the criteria given above --- they identify memory lvalues,
11304 register lvalues, computed values, etcetera. So we can evaluate
11305 the expression, and then scan the chain of values that leaves
11306 behind to decide whether we can detect any possible change to the
11307 expression's final value using only hardware watchpoints.
11308
11309 However, I don't think that the values returned by inferior
11310 function calls are special in any way. So this function may not
11311 notice that an expression involving an inferior function call
11312 can't be watched with hardware watchpoints. FIXME. */
11313 for (; v; v = value_next (v))
11314 {
11315 if (VALUE_LVAL (v) == lval_memory)
11316 {
11317 if (v != head && value_lazy (v))
11318 /* A lazy memory lvalue in the chain is one that GDB never
11319 needed to fetch; we either just used its address (e.g.,
11320 `a' in `a.b') or we never needed it at all (e.g., `a'
11321 in `a,b'). This doesn't apply to HEAD; if that is
11322 lazy then it was not readable, but watch it anyway. */
11323 ;
11324 else
11325 {
11326 /* Ahh, memory we actually used! Check if we can cover
11327 it with hardware watchpoints. */
11328 struct type *vtype = check_typedef (value_type (v));
11329
11330 /* We only watch structs and arrays if user asked for it
11331 explicitly, never if they just happen to appear in a
11332 middle of some value chain. */
11333 if (v == head
11334 || (TYPE_CODE (vtype) != TYPE_CODE_STRUCT
11335 && TYPE_CODE (vtype) != TYPE_CODE_ARRAY))
11336 {
11337 CORE_ADDR vaddr = value_address (v);
11338 int len;
11339 int num_regs;
11340
11341 len = (target_exact_watchpoints
11342 && is_scalar_type_recursive (vtype))?
11343 1 : TYPE_LENGTH (value_type (v));
11344
11345 num_regs = target_region_ok_for_hw_watchpoint (vaddr, len);
11346 if (!num_regs)
11347 return 0;
11348 else
11349 found_memory_cnt += num_regs;
11350 }
11351 }
11352 }
11353 else if (VALUE_LVAL (v) != not_lval
11354 && deprecated_value_modifiable (v) == 0)
11355 return 0; /* These are values from the history (e.g., $1). */
11356 else if (VALUE_LVAL (v) == lval_register)
11357 return 0; /* Cannot watch a register with a HW watchpoint. */
11358 }
11359
11360 /* The expression itself looks suitable for using a hardware
11361 watchpoint, but give the target machine a chance to reject it. */
11362 return found_memory_cnt;
11363 }
11364
11365 void
11366 watch_command_wrapper (char *arg, int from_tty, int internal)
11367 {
11368 watch_command_1 (arg, hw_write, from_tty, 0, internal);
11369 }
11370
11371 /* A helper function that looks for the "-location" argument and then
11372 calls watch_command_1. */
11373
11374 static void
11375 watch_maybe_just_location (char *arg, int accessflag, int from_tty)
11376 {
11377 int just_location = 0;
11378
11379 if (arg
11380 && (check_for_argument (&arg, "-location", sizeof ("-location") - 1)
11381 || check_for_argument (&arg, "-l", sizeof ("-l") - 1)))
11382 {
11383 arg = skip_spaces (arg);
11384 just_location = 1;
11385 }
11386
11387 watch_command_1 (arg, accessflag, from_tty, just_location, 0);
11388 }
11389
11390 static void
11391 watch_command (char *arg, int from_tty)
11392 {
11393 watch_maybe_just_location (arg, hw_write, from_tty);
11394 }
11395
11396 void
11397 rwatch_command_wrapper (char *arg, int from_tty, int internal)
11398 {
11399 watch_command_1 (arg, hw_read, from_tty, 0, internal);
11400 }
11401
11402 static void
11403 rwatch_command (char *arg, int from_tty)
11404 {
11405 watch_maybe_just_location (arg, hw_read, from_tty);
11406 }
11407
11408 void
11409 awatch_command_wrapper (char *arg, int from_tty, int internal)
11410 {
11411 watch_command_1 (arg, hw_access, from_tty, 0, internal);
11412 }
11413
11414 static void
11415 awatch_command (char *arg, int from_tty)
11416 {
11417 watch_maybe_just_location (arg, hw_access, from_tty);
11418 }
11419 \f
11420
11421 /* Helper routines for the until_command routine in infcmd.c. Here
11422 because it uses the mechanisms of breakpoints. */
11423
11424 struct until_break_command_continuation_args
11425 {
11426 struct breakpoint *breakpoint;
11427 struct breakpoint *breakpoint2;
11428 int thread_num;
11429 };
11430
11431 /* This function is called by fetch_inferior_event via the
11432 cmd_continuation pointer, to complete the until command. It takes
11433 care of cleaning up the temporary breakpoints set up by the until
11434 command. */
11435 static void
11436 until_break_command_continuation (void *arg, int err)
11437 {
11438 struct until_break_command_continuation_args *a = arg;
11439
11440 delete_breakpoint (a->breakpoint);
11441 if (a->breakpoint2)
11442 delete_breakpoint (a->breakpoint2);
11443 delete_longjmp_breakpoint (a->thread_num);
11444 }
11445
11446 void
11447 until_break_command (char *arg, int from_tty, int anywhere)
11448 {
11449 struct symtabs_and_lines sals;
11450 struct symtab_and_line sal;
11451 struct frame_info *frame;
11452 struct gdbarch *frame_gdbarch;
11453 struct frame_id stack_frame_id;
11454 struct frame_id caller_frame_id;
11455 struct breakpoint *breakpoint;
11456 struct breakpoint *breakpoint2 = NULL;
11457 struct cleanup *old_chain;
11458 int thread;
11459 struct thread_info *tp;
11460
11461 clear_proceed_status (0);
11462
11463 /* Set a breakpoint where the user wants it and at return from
11464 this function. */
11465
11466 if (last_displayed_sal_is_valid ())
11467 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
11468 get_last_displayed_symtab (),
11469 get_last_displayed_line ());
11470 else
11471 sals = decode_line_1 (&arg, DECODE_LINE_FUNFIRSTLINE,
11472 (struct symtab *) NULL, 0);
11473
11474 if (sals.nelts != 1)
11475 error (_("Couldn't get information on specified line."));
11476
11477 sal = sals.sals[0];
11478 xfree (sals.sals); /* malloc'd, so freed. */
11479
11480 if (*arg)
11481 error (_("Junk at end of arguments."));
11482
11483 resolve_sal_pc (&sal);
11484
11485 tp = inferior_thread ();
11486 thread = tp->num;
11487
11488 old_chain = make_cleanup (null_cleanup, NULL);
11489
11490 /* Note linespec handling above invalidates the frame chain.
11491 Installing a breakpoint also invalidates the frame chain (as it
11492 may need to switch threads), so do any frame handling before
11493 that. */
11494
11495 frame = get_selected_frame (NULL);
11496 frame_gdbarch = get_frame_arch (frame);
11497 stack_frame_id = get_stack_frame_id (frame);
11498 caller_frame_id = frame_unwind_caller_id (frame);
11499
11500 /* Keep within the current frame, or in frames called by the current
11501 one. */
11502
11503 if (frame_id_p (caller_frame_id))
11504 {
11505 struct symtab_and_line sal2;
11506
11507 sal2 = find_pc_line (frame_unwind_caller_pc (frame), 0);
11508 sal2.pc = frame_unwind_caller_pc (frame);
11509 breakpoint2 = set_momentary_breakpoint (frame_unwind_caller_arch (frame),
11510 sal2,
11511 caller_frame_id,
11512 bp_until);
11513 make_cleanup_delete_breakpoint (breakpoint2);
11514
11515 set_longjmp_breakpoint (tp, caller_frame_id);
11516 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
11517 }
11518
11519 /* set_momentary_breakpoint could invalidate FRAME. */
11520 frame = NULL;
11521
11522 if (anywhere)
11523 /* If the user told us to continue until a specified location,
11524 we don't specify a frame at which we need to stop. */
11525 breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11526 null_frame_id, bp_until);
11527 else
11528 /* Otherwise, specify the selected frame, because we want to stop
11529 only at the very same frame. */
11530 breakpoint = set_momentary_breakpoint (frame_gdbarch, sal,
11531 stack_frame_id, bp_until);
11532 make_cleanup_delete_breakpoint (breakpoint);
11533
11534 proceed (-1, GDB_SIGNAL_DEFAULT);
11535
11536 /* If we are running asynchronously, and proceed call above has
11537 actually managed to start the target, arrange for breakpoints to
11538 be deleted when the target stops. Otherwise, we're already
11539 stopped and delete breakpoints via cleanup chain. */
11540
11541 if (target_can_async_p () && is_running (inferior_ptid))
11542 {
11543 struct until_break_command_continuation_args *args;
11544 args = xmalloc (sizeof (*args));
11545
11546 args->breakpoint = breakpoint;
11547 args->breakpoint2 = breakpoint2;
11548 args->thread_num = thread;
11549
11550 discard_cleanups (old_chain);
11551 add_continuation (inferior_thread (),
11552 until_break_command_continuation, args,
11553 xfree);
11554 }
11555 else
11556 do_cleanups (old_chain);
11557 }
11558
11559 /* This function attempts to parse an optional "if <cond>" clause
11560 from the arg string. If one is not found, it returns NULL.
11561
11562 Else, it returns a pointer to the condition string. (It does not
11563 attempt to evaluate the string against a particular block.) And,
11564 it updates arg to point to the first character following the parsed
11565 if clause in the arg string. */
11566
11567 char *
11568 ep_parse_optional_if_clause (char **arg)
11569 {
11570 char *cond_string;
11571
11572 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
11573 return NULL;
11574
11575 /* Skip the "if" keyword. */
11576 (*arg) += 2;
11577
11578 /* Skip any extra leading whitespace, and record the start of the
11579 condition string. */
11580 *arg = skip_spaces (*arg);
11581 cond_string = *arg;
11582
11583 /* Assume that the condition occupies the remainder of the arg
11584 string. */
11585 (*arg) += strlen (cond_string);
11586
11587 return cond_string;
11588 }
11589
11590 /* Commands to deal with catching events, such as signals, exceptions,
11591 process start/exit, etc. */
11592
11593 typedef enum
11594 {
11595 catch_fork_temporary, catch_vfork_temporary,
11596 catch_fork_permanent, catch_vfork_permanent
11597 }
11598 catch_fork_kind;
11599
11600 static void
11601 catch_fork_command_1 (char *arg, int from_tty,
11602 struct cmd_list_element *command)
11603 {
11604 struct gdbarch *gdbarch = get_current_arch ();
11605 char *cond_string = NULL;
11606 catch_fork_kind fork_kind;
11607 int tempflag;
11608
11609 fork_kind = (catch_fork_kind) (uintptr_t) get_cmd_context (command);
11610 tempflag = (fork_kind == catch_fork_temporary
11611 || fork_kind == catch_vfork_temporary);
11612
11613 if (!arg)
11614 arg = "";
11615 arg = skip_spaces (arg);
11616
11617 /* The allowed syntax is:
11618 catch [v]fork
11619 catch [v]fork if <cond>
11620
11621 First, check if there's an if clause. */
11622 cond_string = ep_parse_optional_if_clause (&arg);
11623
11624 if ((*arg != '\0') && !isspace (*arg))
11625 error (_("Junk at end of arguments."));
11626
11627 /* If this target supports it, create a fork or vfork catchpoint
11628 and enable reporting of such events. */
11629 switch (fork_kind)
11630 {
11631 case catch_fork_temporary:
11632 case catch_fork_permanent:
11633 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11634 &catch_fork_breakpoint_ops);
11635 break;
11636 case catch_vfork_temporary:
11637 case catch_vfork_permanent:
11638 create_fork_vfork_event_catchpoint (gdbarch, tempflag, cond_string,
11639 &catch_vfork_breakpoint_ops);
11640 break;
11641 default:
11642 error (_("unsupported or unknown fork kind; cannot catch it"));
11643 break;
11644 }
11645 }
11646
11647 static void
11648 catch_exec_command_1 (char *arg, int from_tty,
11649 struct cmd_list_element *command)
11650 {
11651 struct exec_catchpoint *c;
11652 struct gdbarch *gdbarch = get_current_arch ();
11653 int tempflag;
11654 char *cond_string = NULL;
11655
11656 tempflag = get_cmd_context (command) == CATCH_TEMPORARY;
11657
11658 if (!arg)
11659 arg = "";
11660 arg = skip_spaces (arg);
11661
11662 /* The allowed syntax is:
11663 catch exec
11664 catch exec 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 c = XNEW (struct exec_catchpoint);
11673 init_catchpoint (&c->base, gdbarch, tempflag, cond_string,
11674 &catch_exec_breakpoint_ops);
11675 c->exec_pathname = NULL;
11676
11677 install_breakpoint (0, &c->base, 1);
11678 }
11679
11680 void
11681 init_ada_exception_breakpoint (struct breakpoint *b,
11682 struct gdbarch *gdbarch,
11683 struct symtab_and_line sal,
11684 char *addr_string,
11685 const struct breakpoint_ops *ops,
11686 int tempflag,
11687 int enabled,
11688 int from_tty)
11689 {
11690 if (from_tty)
11691 {
11692 struct gdbarch *loc_gdbarch = get_sal_arch (sal);
11693 if (!loc_gdbarch)
11694 loc_gdbarch = gdbarch;
11695
11696 describe_other_breakpoints (loc_gdbarch,
11697 sal.pspace, sal.pc, sal.section, -1);
11698 /* FIXME: brobecker/2006-12-28: Actually, re-implement a special
11699 version for exception catchpoints, because two catchpoints
11700 used for different exception names will use the same address.
11701 In this case, a "breakpoint ... also set at..." warning is
11702 unproductive. Besides, the warning phrasing is also a bit
11703 inappropriate, we should use the word catchpoint, and tell
11704 the user what type of catchpoint it is. The above is good
11705 enough for now, though. */
11706 }
11707
11708 init_raw_breakpoint (b, gdbarch, sal, bp_breakpoint, ops);
11709
11710 b->enable_state = enabled ? bp_enabled : bp_disabled;
11711 b->disposition = tempflag ? disp_del : disp_donttouch;
11712 b->addr_string = addr_string;
11713 b->language = language_ada;
11714 }
11715
11716 static void
11717 catch_command (char *arg, int from_tty)
11718 {
11719 error (_("Catch requires an event name."));
11720 }
11721 \f
11722
11723 static void
11724 tcatch_command (char *arg, int from_tty)
11725 {
11726 error (_("Catch requires an event name."));
11727 }
11728
11729 /* A qsort comparison function that sorts breakpoints in order. */
11730
11731 static int
11732 compare_breakpoints (const void *a, const void *b)
11733 {
11734 const breakpoint_p *ba = a;
11735 uintptr_t ua = (uintptr_t) *ba;
11736 const breakpoint_p *bb = b;
11737 uintptr_t ub = (uintptr_t) *bb;
11738
11739 if ((*ba)->number < (*bb)->number)
11740 return -1;
11741 else if ((*ba)->number > (*bb)->number)
11742 return 1;
11743
11744 /* Now sort by address, in case we see, e..g, two breakpoints with
11745 the number 0. */
11746 if (ua < ub)
11747 return -1;
11748 return ua > ub ? 1 : 0;
11749 }
11750
11751 /* Delete breakpoints by address or line. */
11752
11753 static void
11754 clear_command (char *arg, int from_tty)
11755 {
11756 struct breakpoint *b, *prev;
11757 VEC(breakpoint_p) *found = 0;
11758 int ix;
11759 int default_match;
11760 struct symtabs_and_lines sals;
11761 struct symtab_and_line sal;
11762 int i;
11763 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
11764
11765 if (arg)
11766 {
11767 sals = decode_line_with_current_source (arg,
11768 (DECODE_LINE_FUNFIRSTLINE
11769 | DECODE_LINE_LIST_MODE));
11770 make_cleanup (xfree, sals.sals);
11771 default_match = 0;
11772 }
11773 else
11774 {
11775 sals.sals = (struct symtab_and_line *)
11776 xmalloc (sizeof (struct symtab_and_line));
11777 make_cleanup (xfree, sals.sals);
11778 init_sal (&sal); /* Initialize to zeroes. */
11779
11780 /* Set sal's line, symtab, pc, and pspace to the values
11781 corresponding to the last call to print_frame_info. If the
11782 codepoint is not valid, this will set all the fields to 0. */
11783 get_last_displayed_sal (&sal);
11784 if (sal.symtab == 0)
11785 error (_("No source file specified."));
11786
11787 sals.sals[0] = sal;
11788 sals.nelts = 1;
11789
11790 default_match = 1;
11791 }
11792
11793 /* We don't call resolve_sal_pc here. That's not as bad as it
11794 seems, because all existing breakpoints typically have both
11795 file/line and pc set. So, if clear is given file/line, we can
11796 match this to existing breakpoint without obtaining pc at all.
11797
11798 We only support clearing given the address explicitly
11799 present in breakpoint table. Say, we've set breakpoint
11800 at file:line. There were several PC values for that file:line,
11801 due to optimization, all in one block.
11802
11803 We've picked one PC value. If "clear" is issued with another
11804 PC corresponding to the same file:line, the breakpoint won't
11805 be cleared. We probably can still clear the breakpoint, but
11806 since the other PC value is never presented to user, user
11807 can only find it by guessing, and it does not seem important
11808 to support that. */
11809
11810 /* For each line spec given, delete bps which correspond to it. Do
11811 it in two passes, solely to preserve the current behavior that
11812 from_tty is forced true if we delete more than one
11813 breakpoint. */
11814
11815 found = NULL;
11816 make_cleanup (VEC_cleanup (breakpoint_p), &found);
11817 for (i = 0; i < sals.nelts; i++)
11818 {
11819 const char *sal_fullname;
11820
11821 /* If exact pc given, clear bpts at that pc.
11822 If line given (pc == 0), clear all bpts on specified line.
11823 If defaulting, clear all bpts on default line
11824 or at default pc.
11825
11826 defaulting sal.pc != 0 tests to do
11827
11828 0 1 pc
11829 1 1 pc _and_ line
11830 0 0 line
11831 1 0 <can't happen> */
11832
11833 sal = sals.sals[i];
11834 sal_fullname = (sal.symtab == NULL
11835 ? NULL : symtab_to_fullname (sal.symtab));
11836
11837 /* Find all matching breakpoints and add them to 'found'. */
11838 ALL_BREAKPOINTS (b)
11839 {
11840 int match = 0;
11841 /* Are we going to delete b? */
11842 if (b->type != bp_none && !is_watchpoint (b))
11843 {
11844 struct bp_location *loc = b->loc;
11845 for (; loc; loc = loc->next)
11846 {
11847 /* If the user specified file:line, don't allow a PC
11848 match. This matches historical gdb behavior. */
11849 int pc_match = (!sal.explicit_line
11850 && sal.pc
11851 && (loc->pspace == sal.pspace)
11852 && (loc->address == sal.pc)
11853 && (!section_is_overlay (loc->section)
11854 || loc->section == sal.section));
11855 int line_match = 0;
11856
11857 if ((default_match || sal.explicit_line)
11858 && loc->symtab != NULL
11859 && sal_fullname != NULL
11860 && sal.pspace == loc->pspace
11861 && loc->line_number == sal.line
11862 && filename_cmp (symtab_to_fullname (loc->symtab),
11863 sal_fullname) == 0)
11864 line_match = 1;
11865
11866 if (pc_match || line_match)
11867 {
11868 match = 1;
11869 break;
11870 }
11871 }
11872 }
11873
11874 if (match)
11875 VEC_safe_push(breakpoint_p, found, b);
11876 }
11877 }
11878
11879 /* Now go thru the 'found' chain and delete them. */
11880 if (VEC_empty(breakpoint_p, found))
11881 {
11882 if (arg)
11883 error (_("No breakpoint at %s."), arg);
11884 else
11885 error (_("No breakpoint at this line."));
11886 }
11887
11888 /* Remove duplicates from the vec. */
11889 qsort (VEC_address (breakpoint_p, found),
11890 VEC_length (breakpoint_p, found),
11891 sizeof (breakpoint_p),
11892 compare_breakpoints);
11893 prev = VEC_index (breakpoint_p, found, 0);
11894 for (ix = 1; VEC_iterate (breakpoint_p, found, ix, b); ++ix)
11895 {
11896 if (b == prev)
11897 {
11898 VEC_ordered_remove (breakpoint_p, found, ix);
11899 --ix;
11900 }
11901 }
11902
11903 if (VEC_length(breakpoint_p, found) > 1)
11904 from_tty = 1; /* Always report if deleted more than one. */
11905 if (from_tty)
11906 {
11907 if (VEC_length(breakpoint_p, found) == 1)
11908 printf_unfiltered (_("Deleted breakpoint "));
11909 else
11910 printf_unfiltered (_("Deleted breakpoints "));
11911 }
11912
11913 for (ix = 0; VEC_iterate(breakpoint_p, found, ix, b); ix++)
11914 {
11915 if (from_tty)
11916 printf_unfiltered ("%d ", b->number);
11917 delete_breakpoint (b);
11918 }
11919 if (from_tty)
11920 putchar_unfiltered ('\n');
11921
11922 do_cleanups (cleanups);
11923 }
11924 \f
11925 /* Delete breakpoint in BS if they are `delete' breakpoints and
11926 all breakpoints that are marked for deletion, whether hit or not.
11927 This is called after any breakpoint is hit, or after errors. */
11928
11929 void
11930 breakpoint_auto_delete (bpstat bs)
11931 {
11932 struct breakpoint *b, *b_tmp;
11933
11934 for (; bs; bs = bs->next)
11935 if (bs->breakpoint_at
11936 && bs->breakpoint_at->disposition == disp_del
11937 && bs->stop)
11938 delete_breakpoint (bs->breakpoint_at);
11939
11940 ALL_BREAKPOINTS_SAFE (b, b_tmp)
11941 {
11942 if (b->disposition == disp_del_at_next_stop)
11943 delete_breakpoint (b);
11944 }
11945 }
11946
11947 /* A comparison function for bp_location AP and BP being interfaced to
11948 qsort. Sort elements primarily by their ADDRESS (no matter what
11949 does breakpoint_address_is_meaningful say for its OWNER),
11950 secondarily by ordering first permanent elements and
11951 terciarily just ensuring the array is sorted stable way despite
11952 qsort being an unstable algorithm. */
11953
11954 static int
11955 bp_location_compare (const void *ap, const void *bp)
11956 {
11957 struct bp_location *a = *(void **) ap;
11958 struct bp_location *b = *(void **) bp;
11959
11960 if (a->address != b->address)
11961 return (a->address > b->address) - (a->address < b->address);
11962
11963 /* Sort locations at the same address by their pspace number, keeping
11964 locations of the same inferior (in a multi-inferior environment)
11965 grouped. */
11966
11967 if (a->pspace->num != b->pspace->num)
11968 return ((a->pspace->num > b->pspace->num)
11969 - (a->pspace->num < b->pspace->num));
11970
11971 /* Sort permanent breakpoints first. */
11972 if (a->permanent != b->permanent)
11973 return (a->permanent < b->permanent) - (a->permanent > b->permanent);
11974
11975 /* Make the internal GDB representation stable across GDB runs
11976 where A and B memory inside GDB can differ. Breakpoint locations of
11977 the same type at the same address can be sorted in arbitrary order. */
11978
11979 if (a->owner->number != b->owner->number)
11980 return ((a->owner->number > b->owner->number)
11981 - (a->owner->number < b->owner->number));
11982
11983 return (a > b) - (a < b);
11984 }
11985
11986 /* Set bp_location_placed_address_before_address_max and
11987 bp_location_shadow_len_after_address_max according to the current
11988 content of the bp_location array. */
11989
11990 static void
11991 bp_location_target_extensions_update (void)
11992 {
11993 struct bp_location *bl, **blp_tmp;
11994
11995 bp_location_placed_address_before_address_max = 0;
11996 bp_location_shadow_len_after_address_max = 0;
11997
11998 ALL_BP_LOCATIONS (bl, blp_tmp)
11999 {
12000 CORE_ADDR start, end, addr;
12001
12002 if (!bp_location_has_shadow (bl))
12003 continue;
12004
12005 start = bl->target_info.placed_address;
12006 end = start + bl->target_info.shadow_len;
12007
12008 gdb_assert (bl->address >= start);
12009 addr = bl->address - start;
12010 if (addr > bp_location_placed_address_before_address_max)
12011 bp_location_placed_address_before_address_max = addr;
12012
12013 /* Zero SHADOW_LEN would not pass bp_location_has_shadow. */
12014
12015 gdb_assert (bl->address < end);
12016 addr = end - bl->address;
12017 if (addr > bp_location_shadow_len_after_address_max)
12018 bp_location_shadow_len_after_address_max = addr;
12019 }
12020 }
12021
12022 /* Download tracepoint locations if they haven't been. */
12023
12024 static void
12025 download_tracepoint_locations (void)
12026 {
12027 struct breakpoint *b;
12028 struct cleanup *old_chain;
12029
12030 if (!target_can_download_tracepoint ())
12031 return;
12032
12033 old_chain = save_current_space_and_thread ();
12034
12035 ALL_TRACEPOINTS (b)
12036 {
12037 struct bp_location *bl;
12038 struct tracepoint *t;
12039 int bp_location_downloaded = 0;
12040
12041 if ((b->type == bp_fast_tracepoint
12042 ? !may_insert_fast_tracepoints
12043 : !may_insert_tracepoints))
12044 continue;
12045
12046 for (bl = b->loc; bl; bl = bl->next)
12047 {
12048 /* In tracepoint, locations are _never_ duplicated, so
12049 should_be_inserted is equivalent to
12050 unduplicated_should_be_inserted. */
12051 if (!should_be_inserted (bl) || bl->inserted)
12052 continue;
12053
12054 switch_to_program_space_and_thread (bl->pspace);
12055
12056 target_download_tracepoint (bl);
12057
12058 bl->inserted = 1;
12059 bp_location_downloaded = 1;
12060 }
12061 t = (struct tracepoint *) b;
12062 t->number_on_target = b->number;
12063 if (bp_location_downloaded)
12064 observer_notify_breakpoint_modified (b);
12065 }
12066
12067 do_cleanups (old_chain);
12068 }
12069
12070 /* Swap the insertion/duplication state between two locations. */
12071
12072 static void
12073 swap_insertion (struct bp_location *left, struct bp_location *right)
12074 {
12075 const int left_inserted = left->inserted;
12076 const int left_duplicate = left->duplicate;
12077 const int left_needs_update = left->needs_update;
12078 const struct bp_target_info left_target_info = left->target_info;
12079
12080 /* Locations of tracepoints can never be duplicated. */
12081 if (is_tracepoint (left->owner))
12082 gdb_assert (!left->duplicate);
12083 if (is_tracepoint (right->owner))
12084 gdb_assert (!right->duplicate);
12085
12086 left->inserted = right->inserted;
12087 left->duplicate = right->duplicate;
12088 left->needs_update = right->needs_update;
12089 left->target_info = right->target_info;
12090 right->inserted = left_inserted;
12091 right->duplicate = left_duplicate;
12092 right->needs_update = left_needs_update;
12093 right->target_info = left_target_info;
12094 }
12095
12096 /* Force the re-insertion of the locations at ADDRESS. This is called
12097 once a new/deleted/modified duplicate location is found and we are evaluating
12098 conditions on the target's side. Such conditions need to be updated on
12099 the target. */
12100
12101 static void
12102 force_breakpoint_reinsertion (struct bp_location *bl)
12103 {
12104 struct bp_location **locp = NULL, **loc2p;
12105 struct bp_location *loc;
12106 CORE_ADDR address = 0;
12107 int pspace_num;
12108
12109 address = bl->address;
12110 pspace_num = bl->pspace->num;
12111
12112 /* This is only meaningful if the target is
12113 evaluating conditions and if the user has
12114 opted for condition evaluation on the target's
12115 side. */
12116 if (gdb_evaluates_breakpoint_condition_p ()
12117 || !target_supports_evaluation_of_breakpoint_conditions ())
12118 return;
12119
12120 /* Flag all breakpoint locations with this address and
12121 the same program space as the location
12122 as "its condition has changed". We need to
12123 update the conditions on the target's side. */
12124 ALL_BP_LOCATIONS_AT_ADDR (loc2p, locp, address)
12125 {
12126 loc = *loc2p;
12127
12128 if (!is_breakpoint (loc->owner)
12129 || pspace_num != loc->pspace->num)
12130 continue;
12131
12132 /* Flag the location appropriately. We use a different state to
12133 let everyone know that we already updated the set of locations
12134 with addr bl->address and program space bl->pspace. This is so
12135 we don't have to keep calling these functions just to mark locations
12136 that have already been marked. */
12137 loc->condition_changed = condition_updated;
12138
12139 /* Free the agent expression bytecode as well. We will compute
12140 it later on. */
12141 if (loc->cond_bytecode)
12142 {
12143 free_agent_expr (loc->cond_bytecode);
12144 loc->cond_bytecode = NULL;
12145 }
12146 }
12147 }
12148 /* Called whether new breakpoints are created, or existing breakpoints
12149 deleted, to update the global location list and recompute which
12150 locations are duplicate of which.
12151
12152 The INSERT_MODE flag determines whether locations may not, may, or
12153 shall be inserted now. See 'enum ugll_insert_mode' for more
12154 info. */
12155
12156 static void
12157 update_global_location_list (enum ugll_insert_mode insert_mode)
12158 {
12159 struct breakpoint *b;
12160 struct bp_location **locp, *loc;
12161 struct cleanup *cleanups;
12162 /* Last breakpoint location address that was marked for update. */
12163 CORE_ADDR last_addr = 0;
12164 /* Last breakpoint location program space that was marked for update. */
12165 int last_pspace_num = -1;
12166
12167 /* Used in the duplicates detection below. When iterating over all
12168 bp_locations, points to the first bp_location of a given address.
12169 Breakpoints and watchpoints of different types are never
12170 duplicates of each other. Keep one pointer for each type of
12171 breakpoint/watchpoint, so we only need to loop over all locations
12172 once. */
12173 struct bp_location *bp_loc_first; /* breakpoint */
12174 struct bp_location *wp_loc_first; /* hardware watchpoint */
12175 struct bp_location *awp_loc_first; /* access watchpoint */
12176 struct bp_location *rwp_loc_first; /* read watchpoint */
12177
12178 /* Saved former bp_location array which we compare against the newly
12179 built bp_location from the current state of ALL_BREAKPOINTS. */
12180 struct bp_location **old_location, **old_locp;
12181 unsigned old_location_count;
12182
12183 old_location = bp_location;
12184 old_location_count = bp_location_count;
12185 bp_location = NULL;
12186 bp_location_count = 0;
12187 cleanups = make_cleanup (xfree, old_location);
12188
12189 ALL_BREAKPOINTS (b)
12190 for (loc = b->loc; loc; loc = loc->next)
12191 bp_location_count++;
12192
12193 bp_location = xmalloc (sizeof (*bp_location) * bp_location_count);
12194 locp = bp_location;
12195 ALL_BREAKPOINTS (b)
12196 for (loc = b->loc; loc; loc = loc->next)
12197 *locp++ = loc;
12198 qsort (bp_location, bp_location_count, sizeof (*bp_location),
12199 bp_location_compare);
12200
12201 bp_location_target_extensions_update ();
12202
12203 /* Identify bp_location instances that are no longer present in the
12204 new list, and therefore should be freed. Note that it's not
12205 necessary that those locations should be removed from inferior --
12206 if there's another location at the same address (previously
12207 marked as duplicate), we don't need to remove/insert the
12208 location.
12209
12210 LOCP is kept in sync with OLD_LOCP, each pointing to the current
12211 and former bp_location array state respectively. */
12212
12213 locp = bp_location;
12214 for (old_locp = old_location; old_locp < old_location + old_location_count;
12215 old_locp++)
12216 {
12217 struct bp_location *old_loc = *old_locp;
12218 struct bp_location **loc2p;
12219
12220 /* Tells if 'old_loc' is found among the new locations. If
12221 not, we have to free it. */
12222 int found_object = 0;
12223 /* Tells if the location should remain inserted in the target. */
12224 int keep_in_target = 0;
12225 int removed = 0;
12226
12227 /* Skip LOCP entries which will definitely never be needed.
12228 Stop either at or being the one matching OLD_LOC. */
12229 while (locp < bp_location + bp_location_count
12230 && (*locp)->address < old_loc->address)
12231 locp++;
12232
12233 for (loc2p = locp;
12234 (loc2p < bp_location + bp_location_count
12235 && (*loc2p)->address == old_loc->address);
12236 loc2p++)
12237 {
12238 /* Check if this is a new/duplicated location or a duplicated
12239 location that had its condition modified. If so, we want to send
12240 its condition to the target if evaluation of conditions is taking
12241 place there. */
12242 if ((*loc2p)->condition_changed == condition_modified
12243 && (last_addr != old_loc->address
12244 || last_pspace_num != old_loc->pspace->num))
12245 {
12246 force_breakpoint_reinsertion (*loc2p);
12247 last_pspace_num = old_loc->pspace->num;
12248 }
12249
12250 if (*loc2p == old_loc)
12251 found_object = 1;
12252 }
12253
12254 /* We have already handled this address, update it so that we don't
12255 have to go through updates again. */
12256 last_addr = old_loc->address;
12257
12258 /* Target-side condition evaluation: Handle deleted locations. */
12259 if (!found_object)
12260 force_breakpoint_reinsertion (old_loc);
12261
12262 /* If this location is no longer present, and inserted, look if
12263 there's maybe a new location at the same address. If so,
12264 mark that one inserted, and don't remove this one. This is
12265 needed so that we don't have a time window where a breakpoint
12266 at certain location is not inserted. */
12267
12268 if (old_loc->inserted)
12269 {
12270 /* If the location is inserted now, we might have to remove
12271 it. */
12272
12273 if (found_object && should_be_inserted (old_loc))
12274 {
12275 /* The location is still present in the location list,
12276 and still should be inserted. Don't do anything. */
12277 keep_in_target = 1;
12278 }
12279 else
12280 {
12281 /* This location still exists, but it won't be kept in the
12282 target since it may have been disabled. We proceed to
12283 remove its target-side condition. */
12284
12285 /* The location is either no longer present, or got
12286 disabled. See if there's another location at the
12287 same address, in which case we don't need to remove
12288 this one from the target. */
12289
12290 /* OLD_LOC comes from existing struct breakpoint. */
12291 if (breakpoint_address_is_meaningful (old_loc->owner))
12292 {
12293 for (loc2p = locp;
12294 (loc2p < bp_location + bp_location_count
12295 && (*loc2p)->address == old_loc->address);
12296 loc2p++)
12297 {
12298 struct bp_location *loc2 = *loc2p;
12299
12300 if (breakpoint_locations_match (loc2, old_loc))
12301 {
12302 /* Read watchpoint locations are switched to
12303 access watchpoints, if the former are not
12304 supported, but the latter are. */
12305 if (is_hardware_watchpoint (old_loc->owner))
12306 {
12307 gdb_assert (is_hardware_watchpoint (loc2->owner));
12308 loc2->watchpoint_type = old_loc->watchpoint_type;
12309 }
12310
12311 /* loc2 is a duplicated location. We need to check
12312 if it should be inserted in case it will be
12313 unduplicated. */
12314 if (loc2 != old_loc
12315 && unduplicated_should_be_inserted (loc2))
12316 {
12317 swap_insertion (old_loc, loc2);
12318 keep_in_target = 1;
12319 break;
12320 }
12321 }
12322 }
12323 }
12324 }
12325
12326 if (!keep_in_target)
12327 {
12328 if (remove_breakpoint (old_loc, mark_uninserted))
12329 {
12330 /* This is just about all we can do. We could keep
12331 this location on the global list, and try to
12332 remove it next time, but there's no particular
12333 reason why we will succeed next time.
12334
12335 Note that at this point, old_loc->owner is still
12336 valid, as delete_breakpoint frees the breakpoint
12337 only after calling us. */
12338 printf_filtered (_("warning: Error removing "
12339 "breakpoint %d\n"),
12340 old_loc->owner->number);
12341 }
12342 removed = 1;
12343 }
12344 }
12345
12346 if (!found_object)
12347 {
12348 if (removed && non_stop
12349 && need_moribund_for_location_type (old_loc))
12350 {
12351 /* This location was removed from the target. In
12352 non-stop mode, a race condition is possible where
12353 we've removed a breakpoint, but stop events for that
12354 breakpoint are already queued and will arrive later.
12355 We apply an heuristic to be able to distinguish such
12356 SIGTRAPs from other random SIGTRAPs: we keep this
12357 breakpoint location for a bit, and will retire it
12358 after we see some number of events. The theory here
12359 is that reporting of events should, "on the average",
12360 be fair, so after a while we'll see events from all
12361 threads that have anything of interest, and no longer
12362 need to keep this breakpoint location around. We
12363 don't hold locations forever so to reduce chances of
12364 mistaking a non-breakpoint SIGTRAP for a breakpoint
12365 SIGTRAP.
12366
12367 The heuristic failing can be disastrous on
12368 decr_pc_after_break targets.
12369
12370 On decr_pc_after_break targets, like e.g., x86-linux,
12371 if we fail to recognize a late breakpoint SIGTRAP,
12372 because events_till_retirement has reached 0 too
12373 soon, we'll fail to do the PC adjustment, and report
12374 a random SIGTRAP to the user. When the user resumes
12375 the inferior, it will most likely immediately crash
12376 with SIGILL/SIGBUS/SIGSEGV, or worse, get silently
12377 corrupted, because of being resumed e.g., in the
12378 middle of a multi-byte instruction, or skipped a
12379 one-byte instruction. This was actually seen happen
12380 on native x86-linux, and should be less rare on
12381 targets that do not support new thread events, like
12382 remote, due to the heuristic depending on
12383 thread_count.
12384
12385 Mistaking a random SIGTRAP for a breakpoint trap
12386 causes similar symptoms (PC adjustment applied when
12387 it shouldn't), but then again, playing with SIGTRAPs
12388 behind the debugger's back is asking for trouble.
12389
12390 Since hardware watchpoint traps are always
12391 distinguishable from other traps, so we don't need to
12392 apply keep hardware watchpoint moribund locations
12393 around. We simply always ignore hardware watchpoint
12394 traps we can no longer explain. */
12395
12396 old_loc->events_till_retirement = 3 * (thread_count () + 1);
12397 old_loc->owner = NULL;
12398
12399 VEC_safe_push (bp_location_p, moribund_locations, old_loc);
12400 }
12401 else
12402 {
12403 old_loc->owner = NULL;
12404 decref_bp_location (&old_loc);
12405 }
12406 }
12407 }
12408
12409 /* Rescan breakpoints at the same address and section, marking the
12410 first one as "first" and any others as "duplicates". This is so
12411 that the bpt instruction is only inserted once. If we have a
12412 permanent breakpoint at the same place as BPT, make that one the
12413 official one, and the rest as duplicates. Permanent breakpoints
12414 are sorted first for the same address.
12415
12416 Do the same for hardware watchpoints, but also considering the
12417 watchpoint's type (regular/access/read) and length. */
12418
12419 bp_loc_first = NULL;
12420 wp_loc_first = NULL;
12421 awp_loc_first = NULL;
12422 rwp_loc_first = NULL;
12423 ALL_BP_LOCATIONS (loc, locp)
12424 {
12425 /* ALL_BP_LOCATIONS bp_location has LOC->OWNER always
12426 non-NULL. */
12427 struct bp_location **loc_first_p;
12428 b = loc->owner;
12429
12430 if (!unduplicated_should_be_inserted (loc)
12431 || !breakpoint_address_is_meaningful (b)
12432 /* Don't detect duplicate for tracepoint locations because they are
12433 never duplicated. See the comments in field `duplicate' of
12434 `struct bp_location'. */
12435 || is_tracepoint (b))
12436 {
12437 /* Clear the condition modification flag. */
12438 loc->condition_changed = condition_unchanged;
12439 continue;
12440 }
12441
12442 /* Permanent breakpoint should always be inserted. */
12443 if (loc->permanent && ! loc->inserted)
12444 internal_error (__FILE__, __LINE__,
12445 _("allegedly permanent breakpoint is not "
12446 "actually inserted"));
12447
12448 if (b->type == bp_hardware_watchpoint)
12449 loc_first_p = &wp_loc_first;
12450 else if (b->type == bp_read_watchpoint)
12451 loc_first_p = &rwp_loc_first;
12452 else if (b->type == bp_access_watchpoint)
12453 loc_first_p = &awp_loc_first;
12454 else
12455 loc_first_p = &bp_loc_first;
12456
12457 if (*loc_first_p == NULL
12458 || (overlay_debugging && loc->section != (*loc_first_p)->section)
12459 || !breakpoint_locations_match (loc, *loc_first_p))
12460 {
12461 *loc_first_p = loc;
12462 loc->duplicate = 0;
12463
12464 if (is_breakpoint (loc->owner) && loc->condition_changed)
12465 {
12466 loc->needs_update = 1;
12467 /* Clear the condition modification flag. */
12468 loc->condition_changed = condition_unchanged;
12469 }
12470 continue;
12471 }
12472
12473
12474 /* This and the above ensure the invariant that the first location
12475 is not duplicated, and is the inserted one.
12476 All following are marked as duplicated, and are not inserted. */
12477 if (loc->inserted)
12478 swap_insertion (loc, *loc_first_p);
12479 loc->duplicate = 1;
12480
12481 /* Clear the condition modification flag. */
12482 loc->condition_changed = condition_unchanged;
12483
12484 if (loc->inserted && !loc->permanent
12485 && (*loc_first_p)->permanent)
12486 internal_error (__FILE__, __LINE__,
12487 _("another breakpoint was inserted on top of "
12488 "a permanent breakpoint"));
12489 }
12490
12491 if (insert_mode == UGLL_INSERT || breakpoints_should_be_inserted_now ())
12492 {
12493 if (insert_mode != UGLL_DONT_INSERT)
12494 insert_breakpoint_locations ();
12495 else
12496 {
12497 /* Even though the caller told us to not insert new
12498 locations, we may still need to update conditions on the
12499 target's side of breakpoints that were already inserted
12500 if the target is evaluating breakpoint conditions. We
12501 only update conditions for locations that are marked
12502 "needs_update". */
12503 update_inserted_breakpoint_locations ();
12504 }
12505 }
12506
12507 if (insert_mode != UGLL_DONT_INSERT)
12508 download_tracepoint_locations ();
12509
12510 do_cleanups (cleanups);
12511 }
12512
12513 void
12514 breakpoint_retire_moribund (void)
12515 {
12516 struct bp_location *loc;
12517 int ix;
12518
12519 for (ix = 0; VEC_iterate (bp_location_p, moribund_locations, ix, loc); ++ix)
12520 if (--(loc->events_till_retirement) == 0)
12521 {
12522 decref_bp_location (&loc);
12523 VEC_unordered_remove (bp_location_p, moribund_locations, ix);
12524 --ix;
12525 }
12526 }
12527
12528 static void
12529 update_global_location_list_nothrow (enum ugll_insert_mode insert_mode)
12530 {
12531
12532 TRY
12533 {
12534 update_global_location_list (insert_mode);
12535 }
12536 CATCH (e, RETURN_MASK_ERROR)
12537 {
12538 }
12539 END_CATCH
12540 }
12541
12542 /* Clear BKP from a BPS. */
12543
12544 static void
12545 bpstat_remove_bp_location (bpstat bps, struct breakpoint *bpt)
12546 {
12547 bpstat bs;
12548
12549 for (bs = bps; bs; bs = bs->next)
12550 if (bs->breakpoint_at == bpt)
12551 {
12552 bs->breakpoint_at = NULL;
12553 bs->old_val = NULL;
12554 /* bs->commands will be freed later. */
12555 }
12556 }
12557
12558 /* Callback for iterate_over_threads. */
12559 static int
12560 bpstat_remove_breakpoint_callback (struct thread_info *th, void *data)
12561 {
12562 struct breakpoint *bpt = data;
12563
12564 bpstat_remove_bp_location (th->control.stop_bpstat, bpt);
12565 return 0;
12566 }
12567
12568 /* Helper for breakpoint and tracepoint breakpoint_ops->mention
12569 callbacks. */
12570
12571 static void
12572 say_where (struct breakpoint *b)
12573 {
12574 struct value_print_options opts;
12575
12576 get_user_print_options (&opts);
12577
12578 /* i18n: cagney/2005-02-11: Below needs to be merged into a
12579 single string. */
12580 if (b->loc == NULL)
12581 {
12582 printf_filtered (_(" (%s) pending."), b->addr_string);
12583 }
12584 else
12585 {
12586 if (opts.addressprint || b->loc->symtab == NULL)
12587 {
12588 printf_filtered (" at ");
12589 fputs_filtered (paddress (b->loc->gdbarch, b->loc->address),
12590 gdb_stdout);
12591 }
12592 if (b->loc->symtab != NULL)
12593 {
12594 /* If there is a single location, we can print the location
12595 more nicely. */
12596 if (b->loc->next == NULL)
12597 printf_filtered (": file %s, line %d.",
12598 symtab_to_filename_for_display (b->loc->symtab),
12599 b->loc->line_number);
12600 else
12601 /* This is not ideal, but each location may have a
12602 different file name, and this at least reflects the
12603 real situation somewhat. */
12604 printf_filtered (": %s.", b->addr_string);
12605 }
12606
12607 if (b->loc->next)
12608 {
12609 struct bp_location *loc = b->loc;
12610 int n = 0;
12611 for (; loc; loc = loc->next)
12612 ++n;
12613 printf_filtered (" (%d locations)", n);
12614 }
12615 }
12616 }
12617
12618 /* Default bp_location_ops methods. */
12619
12620 static void
12621 bp_location_dtor (struct bp_location *self)
12622 {
12623 xfree (self->cond);
12624 if (self->cond_bytecode)
12625 free_agent_expr (self->cond_bytecode);
12626 xfree (self->function_name);
12627
12628 VEC_free (agent_expr_p, self->target_info.conditions);
12629 VEC_free (agent_expr_p, self->target_info.tcommands);
12630 }
12631
12632 static const struct bp_location_ops bp_location_ops =
12633 {
12634 bp_location_dtor
12635 };
12636
12637 /* Default breakpoint_ops methods all breakpoint_ops ultimately
12638 inherit from. */
12639
12640 static void
12641 base_breakpoint_dtor (struct breakpoint *self)
12642 {
12643 decref_counted_command_line (&self->commands);
12644 xfree (self->cond_string);
12645 xfree (self->extra_string);
12646 xfree (self->addr_string);
12647 xfree (self->filter);
12648 xfree (self->addr_string_range_end);
12649 }
12650
12651 static struct bp_location *
12652 base_breakpoint_allocate_location (struct breakpoint *self)
12653 {
12654 struct bp_location *loc;
12655
12656 loc = XNEW (struct bp_location);
12657 init_bp_location (loc, &bp_location_ops, self);
12658 return loc;
12659 }
12660
12661 static void
12662 base_breakpoint_re_set (struct breakpoint *b)
12663 {
12664 /* Nothing to re-set. */
12665 }
12666
12667 #define internal_error_pure_virtual_called() \
12668 gdb_assert_not_reached ("pure virtual function called")
12669
12670 static int
12671 base_breakpoint_insert_location (struct bp_location *bl)
12672 {
12673 internal_error_pure_virtual_called ();
12674 }
12675
12676 static int
12677 base_breakpoint_remove_location (struct bp_location *bl)
12678 {
12679 internal_error_pure_virtual_called ();
12680 }
12681
12682 static int
12683 base_breakpoint_breakpoint_hit (const struct bp_location *bl,
12684 struct address_space *aspace,
12685 CORE_ADDR bp_addr,
12686 const struct target_waitstatus *ws)
12687 {
12688 internal_error_pure_virtual_called ();
12689 }
12690
12691 static void
12692 base_breakpoint_check_status (bpstat bs)
12693 {
12694 /* Always stop. */
12695 }
12696
12697 /* A "works_in_software_mode" breakpoint_ops method that just internal
12698 errors. */
12699
12700 static int
12701 base_breakpoint_works_in_software_mode (const struct breakpoint *b)
12702 {
12703 internal_error_pure_virtual_called ();
12704 }
12705
12706 /* A "resources_needed" breakpoint_ops method that just internal
12707 errors. */
12708
12709 static int
12710 base_breakpoint_resources_needed (const struct bp_location *bl)
12711 {
12712 internal_error_pure_virtual_called ();
12713 }
12714
12715 static enum print_stop_action
12716 base_breakpoint_print_it (bpstat bs)
12717 {
12718 internal_error_pure_virtual_called ();
12719 }
12720
12721 static void
12722 base_breakpoint_print_one_detail (const struct breakpoint *self,
12723 struct ui_out *uiout)
12724 {
12725 /* nothing */
12726 }
12727
12728 static void
12729 base_breakpoint_print_mention (struct breakpoint *b)
12730 {
12731 internal_error_pure_virtual_called ();
12732 }
12733
12734 static void
12735 base_breakpoint_print_recreate (struct breakpoint *b, struct ui_file *fp)
12736 {
12737 internal_error_pure_virtual_called ();
12738 }
12739
12740 static void
12741 base_breakpoint_create_sals_from_address (char **arg,
12742 struct linespec_result *canonical,
12743 enum bptype type_wanted,
12744 char *addr_start,
12745 char **copy_arg)
12746 {
12747 internal_error_pure_virtual_called ();
12748 }
12749
12750 static void
12751 base_breakpoint_create_breakpoints_sal (struct gdbarch *gdbarch,
12752 struct linespec_result *c,
12753 char *cond_string,
12754 char *extra_string,
12755 enum bptype type_wanted,
12756 enum bpdisp disposition,
12757 int thread,
12758 int task, int ignore_count,
12759 const struct breakpoint_ops *o,
12760 int from_tty, int enabled,
12761 int internal, unsigned flags)
12762 {
12763 internal_error_pure_virtual_called ();
12764 }
12765
12766 static void
12767 base_breakpoint_decode_linespec (struct breakpoint *b, char **s,
12768 struct symtabs_and_lines *sals)
12769 {
12770 internal_error_pure_virtual_called ();
12771 }
12772
12773 /* The default 'explains_signal' method. */
12774
12775 static int
12776 base_breakpoint_explains_signal (struct breakpoint *b, enum gdb_signal sig)
12777 {
12778 return 1;
12779 }
12780
12781 /* The default "after_condition_true" method. */
12782
12783 static void
12784 base_breakpoint_after_condition_true (struct bpstats *bs)
12785 {
12786 /* Nothing to do. */
12787 }
12788
12789 struct breakpoint_ops base_breakpoint_ops =
12790 {
12791 base_breakpoint_dtor,
12792 base_breakpoint_allocate_location,
12793 base_breakpoint_re_set,
12794 base_breakpoint_insert_location,
12795 base_breakpoint_remove_location,
12796 base_breakpoint_breakpoint_hit,
12797 base_breakpoint_check_status,
12798 base_breakpoint_resources_needed,
12799 base_breakpoint_works_in_software_mode,
12800 base_breakpoint_print_it,
12801 NULL,
12802 base_breakpoint_print_one_detail,
12803 base_breakpoint_print_mention,
12804 base_breakpoint_print_recreate,
12805 base_breakpoint_create_sals_from_address,
12806 base_breakpoint_create_breakpoints_sal,
12807 base_breakpoint_decode_linespec,
12808 base_breakpoint_explains_signal,
12809 base_breakpoint_after_condition_true,
12810 };
12811
12812 /* Default breakpoint_ops methods. */
12813
12814 static void
12815 bkpt_re_set (struct breakpoint *b)
12816 {
12817 /* FIXME: is this still reachable? */
12818 if (b->addr_string == NULL)
12819 {
12820 /* Anything without a string can't be re-set. */
12821 delete_breakpoint (b);
12822 return;
12823 }
12824
12825 breakpoint_re_set_default (b);
12826 }
12827
12828 static int
12829 bkpt_insert_location (struct bp_location *bl)
12830 {
12831 if (bl->loc_type == bp_loc_hardware_breakpoint)
12832 return target_insert_hw_breakpoint (bl->gdbarch, &bl->target_info);
12833 else
12834 return target_insert_breakpoint (bl->gdbarch, &bl->target_info);
12835 }
12836
12837 static int
12838 bkpt_remove_location (struct bp_location *bl)
12839 {
12840 if (bl->loc_type == bp_loc_hardware_breakpoint)
12841 return target_remove_hw_breakpoint (bl->gdbarch, &bl->target_info);
12842 else
12843 return target_remove_breakpoint (bl->gdbarch, &bl->target_info);
12844 }
12845
12846 static int
12847 bkpt_breakpoint_hit (const struct bp_location *bl,
12848 struct address_space *aspace, CORE_ADDR bp_addr,
12849 const struct target_waitstatus *ws)
12850 {
12851 if (ws->kind != TARGET_WAITKIND_STOPPED
12852 || ws->value.sig != GDB_SIGNAL_TRAP)
12853 return 0;
12854
12855 if (!breakpoint_address_match (bl->pspace->aspace, bl->address,
12856 aspace, bp_addr))
12857 return 0;
12858
12859 if (overlay_debugging /* unmapped overlay section */
12860 && section_is_overlay (bl->section)
12861 && !section_is_mapped (bl->section))
12862 return 0;
12863
12864 return 1;
12865 }
12866
12867 static int
12868 dprintf_breakpoint_hit (const struct bp_location *bl,
12869 struct address_space *aspace, CORE_ADDR bp_addr,
12870 const struct target_waitstatus *ws)
12871 {
12872 if (dprintf_style == dprintf_style_agent
12873 && target_can_run_breakpoint_commands ())
12874 {
12875 /* An agent-style dprintf never causes a stop. If we see a trap
12876 for this address it must be for a breakpoint that happens to
12877 be set at the same address. */
12878 return 0;
12879 }
12880
12881 return bkpt_breakpoint_hit (bl, aspace, bp_addr, ws);
12882 }
12883
12884 static int
12885 bkpt_resources_needed (const struct bp_location *bl)
12886 {
12887 gdb_assert (bl->owner->type == bp_hardware_breakpoint);
12888
12889 return 1;
12890 }
12891
12892 static enum print_stop_action
12893 bkpt_print_it (bpstat bs)
12894 {
12895 struct breakpoint *b;
12896 const struct bp_location *bl;
12897 int bp_temp;
12898 struct ui_out *uiout = current_uiout;
12899
12900 gdb_assert (bs->bp_location_at != NULL);
12901
12902 bl = bs->bp_location_at;
12903 b = bs->breakpoint_at;
12904
12905 bp_temp = b->disposition == disp_del;
12906 if (bl->address != bl->requested_address)
12907 breakpoint_adjustment_warning (bl->requested_address,
12908 bl->address,
12909 b->number, 1);
12910 annotate_breakpoint (b->number);
12911 if (bp_temp)
12912 ui_out_text (uiout, "\nTemporary breakpoint ");
12913 else
12914 ui_out_text (uiout, "\nBreakpoint ");
12915 if (ui_out_is_mi_like_p (uiout))
12916 {
12917 ui_out_field_string (uiout, "reason",
12918 async_reason_lookup (EXEC_ASYNC_BREAKPOINT_HIT));
12919 ui_out_field_string (uiout, "disp", bpdisp_text (b->disposition));
12920 }
12921 ui_out_field_int (uiout, "bkptno", b->number);
12922 ui_out_text (uiout, ", ");
12923
12924 return PRINT_SRC_AND_LOC;
12925 }
12926
12927 static void
12928 bkpt_print_mention (struct breakpoint *b)
12929 {
12930 if (ui_out_is_mi_like_p (current_uiout))
12931 return;
12932
12933 switch (b->type)
12934 {
12935 case bp_breakpoint:
12936 case bp_gnu_ifunc_resolver:
12937 if (b->disposition == disp_del)
12938 printf_filtered (_("Temporary breakpoint"));
12939 else
12940 printf_filtered (_("Breakpoint"));
12941 printf_filtered (_(" %d"), b->number);
12942 if (b->type == bp_gnu_ifunc_resolver)
12943 printf_filtered (_(" at gnu-indirect-function resolver"));
12944 break;
12945 case bp_hardware_breakpoint:
12946 printf_filtered (_("Hardware assisted breakpoint %d"), b->number);
12947 break;
12948 case bp_dprintf:
12949 printf_filtered (_("Dprintf %d"), b->number);
12950 break;
12951 }
12952
12953 say_where (b);
12954 }
12955
12956 static void
12957 bkpt_print_recreate (struct breakpoint *tp, struct ui_file *fp)
12958 {
12959 if (tp->type == bp_breakpoint && tp->disposition == disp_del)
12960 fprintf_unfiltered (fp, "tbreak");
12961 else if (tp->type == bp_breakpoint)
12962 fprintf_unfiltered (fp, "break");
12963 else if (tp->type == bp_hardware_breakpoint
12964 && tp->disposition == disp_del)
12965 fprintf_unfiltered (fp, "thbreak");
12966 else if (tp->type == bp_hardware_breakpoint)
12967 fprintf_unfiltered (fp, "hbreak");
12968 else
12969 internal_error (__FILE__, __LINE__,
12970 _("unhandled breakpoint type %d"), (int) tp->type);
12971
12972 fprintf_unfiltered (fp, " %s", tp->addr_string);
12973 print_recreate_thread (tp, fp);
12974 }
12975
12976 static void
12977 bkpt_create_sals_from_address (char **arg,
12978 struct linespec_result *canonical,
12979 enum bptype type_wanted,
12980 char *addr_start, char **copy_arg)
12981 {
12982 create_sals_from_address_default (arg, canonical, type_wanted,
12983 addr_start, copy_arg);
12984 }
12985
12986 static void
12987 bkpt_create_breakpoints_sal (struct gdbarch *gdbarch,
12988 struct linespec_result *canonical,
12989 char *cond_string,
12990 char *extra_string,
12991 enum bptype type_wanted,
12992 enum bpdisp disposition,
12993 int thread,
12994 int task, int ignore_count,
12995 const struct breakpoint_ops *ops,
12996 int from_tty, int enabled,
12997 int internal, unsigned flags)
12998 {
12999 create_breakpoints_sal_default (gdbarch, canonical,
13000 cond_string, extra_string,
13001 type_wanted,
13002 disposition, thread, task,
13003 ignore_count, ops, from_tty,
13004 enabled, internal, flags);
13005 }
13006
13007 static void
13008 bkpt_decode_linespec (struct breakpoint *b, char **s,
13009 struct symtabs_and_lines *sals)
13010 {
13011 decode_linespec_default (b, s, sals);
13012 }
13013
13014 /* Virtual table for internal breakpoints. */
13015
13016 static void
13017 internal_bkpt_re_set (struct breakpoint *b)
13018 {
13019 switch (b->type)
13020 {
13021 /* Delete overlay event and longjmp master breakpoints; they
13022 will be reset later by breakpoint_re_set. */
13023 case bp_overlay_event:
13024 case bp_longjmp_master:
13025 case bp_std_terminate_master:
13026 case bp_exception_master:
13027 delete_breakpoint (b);
13028 break;
13029
13030 /* This breakpoint is special, it's set up when the inferior
13031 starts and we really don't want to touch it. */
13032 case bp_shlib_event:
13033
13034 /* Like bp_shlib_event, this breakpoint type is special. Once
13035 it is set up, we do not want to touch it. */
13036 case bp_thread_event:
13037 break;
13038 }
13039 }
13040
13041 static void
13042 internal_bkpt_check_status (bpstat bs)
13043 {
13044 if (bs->breakpoint_at->type == bp_shlib_event)
13045 {
13046 /* If requested, stop when the dynamic linker notifies GDB of
13047 events. This allows the user to get control and place
13048 breakpoints in initializer routines for dynamically loaded
13049 objects (among other things). */
13050 bs->stop = stop_on_solib_events;
13051 bs->print = stop_on_solib_events;
13052 }
13053 else
13054 bs->stop = 0;
13055 }
13056
13057 static enum print_stop_action
13058 internal_bkpt_print_it (bpstat bs)
13059 {
13060 struct breakpoint *b;
13061
13062 b = bs->breakpoint_at;
13063
13064 switch (b->type)
13065 {
13066 case bp_shlib_event:
13067 /* Did we stop because the user set the stop_on_solib_events
13068 variable? (If so, we report this as a generic, "Stopped due
13069 to shlib event" message.) */
13070 print_solib_event (0);
13071 break;
13072
13073 case bp_thread_event:
13074 /* Not sure how we will get here.
13075 GDB should not stop for these breakpoints. */
13076 printf_filtered (_("Thread Event Breakpoint: gdb should not stop!\n"));
13077 break;
13078
13079 case bp_overlay_event:
13080 /* By analogy with the thread event, GDB should not stop for these. */
13081 printf_filtered (_("Overlay Event Breakpoint: gdb should not stop!\n"));
13082 break;
13083
13084 case bp_longjmp_master:
13085 /* These should never be enabled. */
13086 printf_filtered (_("Longjmp Master Breakpoint: gdb should not stop!\n"));
13087 break;
13088
13089 case bp_std_terminate_master:
13090 /* These should never be enabled. */
13091 printf_filtered (_("std::terminate Master Breakpoint: "
13092 "gdb should not stop!\n"));
13093 break;
13094
13095 case bp_exception_master:
13096 /* These should never be enabled. */
13097 printf_filtered (_("Exception Master Breakpoint: "
13098 "gdb should not stop!\n"));
13099 break;
13100 }
13101
13102 return PRINT_NOTHING;
13103 }
13104
13105 static void
13106 internal_bkpt_print_mention (struct breakpoint *b)
13107 {
13108 /* Nothing to mention. These breakpoints are internal. */
13109 }
13110
13111 /* Virtual table for momentary breakpoints */
13112
13113 static void
13114 momentary_bkpt_re_set (struct breakpoint *b)
13115 {
13116 /* Keep temporary breakpoints, which can be encountered when we step
13117 over a dlopen call and solib_add is resetting the breakpoints.
13118 Otherwise these should have been blown away via the cleanup chain
13119 or by breakpoint_init_inferior when we rerun the executable. */
13120 }
13121
13122 static void
13123 momentary_bkpt_check_status (bpstat bs)
13124 {
13125 /* Nothing. The point of these breakpoints is causing a stop. */
13126 }
13127
13128 static enum print_stop_action
13129 momentary_bkpt_print_it (bpstat bs)
13130 {
13131 struct ui_out *uiout = current_uiout;
13132
13133 if (ui_out_is_mi_like_p (uiout))
13134 {
13135 struct breakpoint *b = bs->breakpoint_at;
13136
13137 switch (b->type)
13138 {
13139 case bp_finish:
13140 ui_out_field_string
13141 (uiout, "reason",
13142 async_reason_lookup (EXEC_ASYNC_FUNCTION_FINISHED));
13143 break;
13144
13145 case bp_until:
13146 ui_out_field_string
13147 (uiout, "reason",
13148 async_reason_lookup (EXEC_ASYNC_LOCATION_REACHED));
13149 break;
13150 }
13151 }
13152
13153 return PRINT_UNKNOWN;
13154 }
13155
13156 static void
13157 momentary_bkpt_print_mention (struct breakpoint *b)
13158 {
13159 /* Nothing to mention. These breakpoints are internal. */
13160 }
13161
13162 /* Ensure INITIATING_FRAME is cleared when no such breakpoint exists.
13163
13164 It gets cleared already on the removal of the first one of such placed
13165 breakpoints. This is OK as they get all removed altogether. */
13166
13167 static void
13168 longjmp_bkpt_dtor (struct breakpoint *self)
13169 {
13170 struct thread_info *tp = find_thread_id (self->thread);
13171
13172 if (tp)
13173 tp->initiating_frame = null_frame_id;
13174
13175 momentary_breakpoint_ops.dtor (self);
13176 }
13177
13178 /* Specific methods for probe breakpoints. */
13179
13180 static int
13181 bkpt_probe_insert_location (struct bp_location *bl)
13182 {
13183 int v = bkpt_insert_location (bl);
13184
13185 if (v == 0)
13186 {
13187 /* The insertion was successful, now let's set the probe's semaphore
13188 if needed. */
13189 if (bl->probe.probe->pops->set_semaphore != NULL)
13190 bl->probe.probe->pops->set_semaphore (bl->probe.probe,
13191 bl->probe.objfile,
13192 bl->gdbarch);
13193 }
13194
13195 return v;
13196 }
13197
13198 static int
13199 bkpt_probe_remove_location (struct bp_location *bl)
13200 {
13201 /* Let's clear the semaphore before removing the location. */
13202 if (bl->probe.probe->pops->clear_semaphore != NULL)
13203 bl->probe.probe->pops->clear_semaphore (bl->probe.probe,
13204 bl->probe.objfile,
13205 bl->gdbarch);
13206
13207 return bkpt_remove_location (bl);
13208 }
13209
13210 static void
13211 bkpt_probe_create_sals_from_address (char **arg,
13212 struct linespec_result *canonical,
13213 enum bptype type_wanted,
13214 char *addr_start, char **copy_arg)
13215 {
13216 struct linespec_sals lsal;
13217
13218 lsal.sals = parse_probes (arg, canonical);
13219
13220 *copy_arg = xstrdup (canonical->addr_string);
13221 lsal.canonical = xstrdup (*copy_arg);
13222
13223 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13224 }
13225
13226 static void
13227 bkpt_probe_decode_linespec (struct breakpoint *b, char **s,
13228 struct symtabs_and_lines *sals)
13229 {
13230 *sals = parse_probes (s, NULL);
13231 if (!sals->sals)
13232 error (_("probe not found"));
13233 }
13234
13235 /* The breakpoint_ops structure to be used in tracepoints. */
13236
13237 static void
13238 tracepoint_re_set (struct breakpoint *b)
13239 {
13240 breakpoint_re_set_default (b);
13241 }
13242
13243 static int
13244 tracepoint_breakpoint_hit (const struct bp_location *bl,
13245 struct address_space *aspace, CORE_ADDR bp_addr,
13246 const struct target_waitstatus *ws)
13247 {
13248 /* By definition, the inferior does not report stops at
13249 tracepoints. */
13250 return 0;
13251 }
13252
13253 static void
13254 tracepoint_print_one_detail (const struct breakpoint *self,
13255 struct ui_out *uiout)
13256 {
13257 struct tracepoint *tp = (struct tracepoint *) self;
13258 if (tp->static_trace_marker_id)
13259 {
13260 gdb_assert (self->type == bp_static_tracepoint);
13261
13262 ui_out_text (uiout, "\tmarker id is ");
13263 ui_out_field_string (uiout, "static-tracepoint-marker-string-id",
13264 tp->static_trace_marker_id);
13265 ui_out_text (uiout, "\n");
13266 }
13267 }
13268
13269 static void
13270 tracepoint_print_mention (struct breakpoint *b)
13271 {
13272 if (ui_out_is_mi_like_p (current_uiout))
13273 return;
13274
13275 switch (b->type)
13276 {
13277 case bp_tracepoint:
13278 printf_filtered (_("Tracepoint"));
13279 printf_filtered (_(" %d"), b->number);
13280 break;
13281 case bp_fast_tracepoint:
13282 printf_filtered (_("Fast tracepoint"));
13283 printf_filtered (_(" %d"), b->number);
13284 break;
13285 case bp_static_tracepoint:
13286 printf_filtered (_("Static tracepoint"));
13287 printf_filtered (_(" %d"), b->number);
13288 break;
13289 default:
13290 internal_error (__FILE__, __LINE__,
13291 _("unhandled tracepoint type %d"), (int) b->type);
13292 }
13293
13294 say_where (b);
13295 }
13296
13297 static void
13298 tracepoint_print_recreate (struct breakpoint *self, struct ui_file *fp)
13299 {
13300 struct tracepoint *tp = (struct tracepoint *) self;
13301
13302 if (self->type == bp_fast_tracepoint)
13303 fprintf_unfiltered (fp, "ftrace");
13304 if (self->type == bp_static_tracepoint)
13305 fprintf_unfiltered (fp, "strace");
13306 else if (self->type == bp_tracepoint)
13307 fprintf_unfiltered (fp, "trace");
13308 else
13309 internal_error (__FILE__, __LINE__,
13310 _("unhandled tracepoint type %d"), (int) self->type);
13311
13312 fprintf_unfiltered (fp, " %s", self->addr_string);
13313 print_recreate_thread (self, fp);
13314
13315 if (tp->pass_count)
13316 fprintf_unfiltered (fp, " passcount %d\n", tp->pass_count);
13317 }
13318
13319 static void
13320 tracepoint_create_sals_from_address (char **arg,
13321 struct linespec_result *canonical,
13322 enum bptype type_wanted,
13323 char *addr_start, char **copy_arg)
13324 {
13325 create_sals_from_address_default (arg, canonical, type_wanted,
13326 addr_start, copy_arg);
13327 }
13328
13329 static void
13330 tracepoint_create_breakpoints_sal (struct gdbarch *gdbarch,
13331 struct linespec_result *canonical,
13332 char *cond_string,
13333 char *extra_string,
13334 enum bptype type_wanted,
13335 enum bpdisp disposition,
13336 int thread,
13337 int task, int ignore_count,
13338 const struct breakpoint_ops *ops,
13339 int from_tty, int enabled,
13340 int internal, unsigned flags)
13341 {
13342 create_breakpoints_sal_default (gdbarch, canonical,
13343 cond_string, extra_string,
13344 type_wanted,
13345 disposition, thread, task,
13346 ignore_count, ops, from_tty,
13347 enabled, internal, flags);
13348 }
13349
13350 static void
13351 tracepoint_decode_linespec (struct breakpoint *b, char **s,
13352 struct symtabs_and_lines *sals)
13353 {
13354 decode_linespec_default (b, s, sals);
13355 }
13356
13357 struct breakpoint_ops tracepoint_breakpoint_ops;
13358
13359 /* The breakpoint_ops structure to be use on tracepoints placed in a
13360 static probe. */
13361
13362 static void
13363 tracepoint_probe_create_sals_from_address (char **arg,
13364 struct linespec_result *canonical,
13365 enum bptype type_wanted,
13366 char *addr_start, char **copy_arg)
13367 {
13368 /* We use the same method for breakpoint on probes. */
13369 bkpt_probe_create_sals_from_address (arg, canonical, type_wanted,
13370 addr_start, copy_arg);
13371 }
13372
13373 static void
13374 tracepoint_probe_decode_linespec (struct breakpoint *b, char **s,
13375 struct symtabs_and_lines *sals)
13376 {
13377 /* We use the same method for breakpoint on probes. */
13378 bkpt_probe_decode_linespec (b, s, sals);
13379 }
13380
13381 static struct breakpoint_ops tracepoint_probe_breakpoint_ops;
13382
13383 /* Dprintf breakpoint_ops methods. */
13384
13385 static void
13386 dprintf_re_set (struct breakpoint *b)
13387 {
13388 breakpoint_re_set_default (b);
13389
13390 /* This breakpoint could have been pending, and be resolved now, and
13391 if so, we should now have the extra string. If we don't, the
13392 dprintf was malformed when created, but we couldn't tell because
13393 we can't extract the extra string until the location is
13394 resolved. */
13395 if (b->loc != NULL && b->extra_string == NULL)
13396 error (_("Format string required"));
13397
13398 /* 1 - connect to target 1, that can run breakpoint commands.
13399 2 - create a dprintf, which resolves fine.
13400 3 - disconnect from target 1
13401 4 - connect to target 2, that can NOT run breakpoint commands.
13402
13403 After steps #3/#4, you'll want the dprintf command list to
13404 be updated, because target 1 and 2 may well return different
13405 answers for target_can_run_breakpoint_commands().
13406 Given absence of finer grained resetting, we get to do
13407 it all the time. */
13408 if (b->extra_string != NULL)
13409 update_dprintf_command_list (b);
13410 }
13411
13412 /* Implement the "print_recreate" breakpoint_ops method for dprintf. */
13413
13414 static void
13415 dprintf_print_recreate (struct breakpoint *tp, struct ui_file *fp)
13416 {
13417 fprintf_unfiltered (fp, "dprintf %s%s", tp->addr_string,
13418 tp->extra_string);
13419 print_recreate_thread (tp, fp);
13420 }
13421
13422 /* Implement the "after_condition_true" breakpoint_ops method for
13423 dprintf.
13424
13425 dprintf's are implemented with regular commands in their command
13426 list, but we run the commands here instead of before presenting the
13427 stop to the user, as dprintf's don't actually cause a stop. This
13428 also makes it so that the commands of multiple dprintfs at the same
13429 address are all handled. */
13430
13431 static void
13432 dprintf_after_condition_true (struct bpstats *bs)
13433 {
13434 struct cleanup *old_chain;
13435 struct bpstats tmp_bs = { NULL };
13436 struct bpstats *tmp_bs_p = &tmp_bs;
13437
13438 /* dprintf's never cause a stop. This wasn't set in the
13439 check_status hook instead because that would make the dprintf's
13440 condition not be evaluated. */
13441 bs->stop = 0;
13442
13443 /* Run the command list here. Take ownership of it instead of
13444 copying. We never want these commands to run later in
13445 bpstat_do_actions, if a breakpoint that causes a stop happens to
13446 be set at same address as this dprintf, or even if running the
13447 commands here throws. */
13448 tmp_bs.commands = bs->commands;
13449 bs->commands = NULL;
13450 old_chain = make_cleanup_decref_counted_command_line (&tmp_bs.commands);
13451
13452 bpstat_do_actions_1 (&tmp_bs_p);
13453
13454 /* 'tmp_bs.commands' will usually be NULL by now, but
13455 bpstat_do_actions_1 may return early without processing the whole
13456 list. */
13457 do_cleanups (old_chain);
13458 }
13459
13460 /* The breakpoint_ops structure to be used on static tracepoints with
13461 markers (`-m'). */
13462
13463 static void
13464 strace_marker_create_sals_from_address (char **arg,
13465 struct linespec_result *canonical,
13466 enum bptype type_wanted,
13467 char *addr_start, char **copy_arg)
13468 {
13469 struct linespec_sals lsal;
13470
13471 lsal.sals = decode_static_tracepoint_spec (arg);
13472
13473 *copy_arg = savestring (addr_start, *arg - addr_start);
13474
13475 canonical->addr_string = xstrdup (*copy_arg);
13476 lsal.canonical = xstrdup (*copy_arg);
13477 VEC_safe_push (linespec_sals, canonical->sals, &lsal);
13478 }
13479
13480 static void
13481 strace_marker_create_breakpoints_sal (struct gdbarch *gdbarch,
13482 struct linespec_result *canonical,
13483 char *cond_string,
13484 char *extra_string,
13485 enum bptype type_wanted,
13486 enum bpdisp disposition,
13487 int thread,
13488 int task, int ignore_count,
13489 const struct breakpoint_ops *ops,
13490 int from_tty, int enabled,
13491 int internal, unsigned flags)
13492 {
13493 int i;
13494 struct linespec_sals *lsal = VEC_index (linespec_sals,
13495 canonical->sals, 0);
13496
13497 /* If the user is creating a static tracepoint by marker id
13498 (strace -m MARKER_ID), then store the sals index, so that
13499 breakpoint_re_set can try to match up which of the newly
13500 found markers corresponds to this one, and, don't try to
13501 expand multiple locations for each sal, given than SALS
13502 already should contain all sals for MARKER_ID. */
13503
13504 for (i = 0; i < lsal->sals.nelts; ++i)
13505 {
13506 struct symtabs_and_lines expanded;
13507 struct tracepoint *tp;
13508 struct cleanup *old_chain;
13509 char *addr_string;
13510
13511 expanded.nelts = 1;
13512 expanded.sals = &lsal->sals.sals[i];
13513
13514 addr_string = xstrdup (canonical->addr_string);
13515 old_chain = make_cleanup (xfree, addr_string);
13516
13517 tp = XCNEW (struct tracepoint);
13518 init_breakpoint_sal (&tp->base, gdbarch, expanded,
13519 addr_string, NULL,
13520 cond_string, extra_string,
13521 type_wanted, disposition,
13522 thread, task, ignore_count, ops,
13523 from_tty, enabled, internal, flags,
13524 canonical->special_display);
13525 /* Given that its possible to have multiple markers with
13526 the same string id, if the user is creating a static
13527 tracepoint by marker id ("strace -m MARKER_ID"), then
13528 store the sals index, so that breakpoint_re_set can
13529 try to match up which of the newly found markers
13530 corresponds to this one */
13531 tp->static_trace_marker_id_idx = i;
13532
13533 install_breakpoint (internal, &tp->base, 0);
13534
13535 discard_cleanups (old_chain);
13536 }
13537 }
13538
13539 static void
13540 strace_marker_decode_linespec (struct breakpoint *b, char **s,
13541 struct symtabs_and_lines *sals)
13542 {
13543 struct tracepoint *tp = (struct tracepoint *) b;
13544
13545 *sals = decode_static_tracepoint_spec (s);
13546 if (sals->nelts > tp->static_trace_marker_id_idx)
13547 {
13548 sals->sals[0] = sals->sals[tp->static_trace_marker_id_idx];
13549 sals->nelts = 1;
13550 }
13551 else
13552 error (_("marker %s not found"), tp->static_trace_marker_id);
13553 }
13554
13555 static struct breakpoint_ops strace_marker_breakpoint_ops;
13556
13557 static int
13558 strace_marker_p (struct breakpoint *b)
13559 {
13560 return b->ops == &strace_marker_breakpoint_ops;
13561 }
13562
13563 /* Delete a breakpoint and clean up all traces of it in the data
13564 structures. */
13565
13566 void
13567 delete_breakpoint (struct breakpoint *bpt)
13568 {
13569 struct breakpoint *b;
13570
13571 gdb_assert (bpt != NULL);
13572
13573 /* Has this bp already been deleted? This can happen because
13574 multiple lists can hold pointers to bp's. bpstat lists are
13575 especial culprits.
13576
13577 One example of this happening is a watchpoint's scope bp. When
13578 the scope bp triggers, we notice that the watchpoint is out of
13579 scope, and delete it. We also delete its scope bp. But the
13580 scope bp is marked "auto-deleting", and is already on a bpstat.
13581 That bpstat is then checked for auto-deleting bp's, which are
13582 deleted.
13583
13584 A real solution to this problem might involve reference counts in
13585 bp's, and/or giving them pointers back to their referencing
13586 bpstat's, and teaching delete_breakpoint to only free a bp's
13587 storage when no more references were extent. A cheaper bandaid
13588 was chosen. */
13589 if (bpt->type == bp_none)
13590 return;
13591
13592 /* At least avoid this stale reference until the reference counting
13593 of breakpoints gets resolved. */
13594 if (bpt->related_breakpoint != bpt)
13595 {
13596 struct breakpoint *related;
13597 struct watchpoint *w;
13598
13599 if (bpt->type == bp_watchpoint_scope)
13600 w = (struct watchpoint *) bpt->related_breakpoint;
13601 else if (bpt->related_breakpoint->type == bp_watchpoint_scope)
13602 w = (struct watchpoint *) bpt;
13603 else
13604 w = NULL;
13605 if (w != NULL)
13606 watchpoint_del_at_next_stop (w);
13607
13608 /* Unlink bpt from the bpt->related_breakpoint ring. */
13609 for (related = bpt; related->related_breakpoint != bpt;
13610 related = related->related_breakpoint);
13611 related->related_breakpoint = bpt->related_breakpoint;
13612 bpt->related_breakpoint = bpt;
13613 }
13614
13615 /* watch_command_1 creates a watchpoint but only sets its number if
13616 update_watchpoint succeeds in creating its bp_locations. If there's
13617 a problem in that process, we'll be asked to delete the half-created
13618 watchpoint. In that case, don't announce the deletion. */
13619 if (bpt->number)
13620 observer_notify_breakpoint_deleted (bpt);
13621
13622 if (breakpoint_chain == bpt)
13623 breakpoint_chain = bpt->next;
13624
13625 ALL_BREAKPOINTS (b)
13626 if (b->next == bpt)
13627 {
13628 b->next = bpt->next;
13629 break;
13630 }
13631
13632 /* Be sure no bpstat's are pointing at the breakpoint after it's
13633 been freed. */
13634 /* FIXME, how can we find all bpstat's? We just check stop_bpstat
13635 in all threads for now. Note that we cannot just remove bpstats
13636 pointing at bpt from the stop_bpstat list entirely, as breakpoint
13637 commands are associated with the bpstat; if we remove it here,
13638 then the later call to bpstat_do_actions (&stop_bpstat); in
13639 event-top.c won't do anything, and temporary breakpoints with
13640 commands won't work. */
13641
13642 iterate_over_threads (bpstat_remove_breakpoint_callback, bpt);
13643
13644 /* Now that breakpoint is removed from breakpoint list, update the
13645 global location list. This will remove locations that used to
13646 belong to this breakpoint. Do this before freeing the breakpoint
13647 itself, since remove_breakpoint looks at location's owner. It
13648 might be better design to have location completely
13649 self-contained, but it's not the case now. */
13650 update_global_location_list (UGLL_DONT_INSERT);
13651
13652 bpt->ops->dtor (bpt);
13653 /* On the chance that someone will soon try again to delete this
13654 same bp, we mark it as deleted before freeing its storage. */
13655 bpt->type = bp_none;
13656 xfree (bpt);
13657 }
13658
13659 static void
13660 do_delete_breakpoint_cleanup (void *b)
13661 {
13662 delete_breakpoint (b);
13663 }
13664
13665 struct cleanup *
13666 make_cleanup_delete_breakpoint (struct breakpoint *b)
13667 {
13668 return make_cleanup (do_delete_breakpoint_cleanup, b);
13669 }
13670
13671 /* Iterator function to call a user-provided callback function once
13672 for each of B and its related breakpoints. */
13673
13674 static void
13675 iterate_over_related_breakpoints (struct breakpoint *b,
13676 void (*function) (struct breakpoint *,
13677 void *),
13678 void *data)
13679 {
13680 struct breakpoint *related;
13681
13682 related = b;
13683 do
13684 {
13685 struct breakpoint *next;
13686
13687 /* FUNCTION may delete RELATED. */
13688 next = related->related_breakpoint;
13689
13690 if (next == related)
13691 {
13692 /* RELATED is the last ring entry. */
13693 function (related, data);
13694
13695 /* FUNCTION may have deleted it, so we'd never reach back to
13696 B. There's nothing left to do anyway, so just break
13697 out. */
13698 break;
13699 }
13700 else
13701 function (related, data);
13702
13703 related = next;
13704 }
13705 while (related != b);
13706 }
13707
13708 static void
13709 do_delete_breakpoint (struct breakpoint *b, void *ignore)
13710 {
13711 delete_breakpoint (b);
13712 }
13713
13714 /* A callback for map_breakpoint_numbers that calls
13715 delete_breakpoint. */
13716
13717 static void
13718 do_map_delete_breakpoint (struct breakpoint *b, void *ignore)
13719 {
13720 iterate_over_related_breakpoints (b, do_delete_breakpoint, NULL);
13721 }
13722
13723 void
13724 delete_command (char *arg, int from_tty)
13725 {
13726 struct breakpoint *b, *b_tmp;
13727
13728 dont_repeat ();
13729
13730 if (arg == 0)
13731 {
13732 int breaks_to_delete = 0;
13733
13734 /* Delete all breakpoints if no argument. Do not delete
13735 internal breakpoints, these have to be deleted with an
13736 explicit breakpoint number argument. */
13737 ALL_BREAKPOINTS (b)
13738 if (user_breakpoint_p (b))
13739 {
13740 breaks_to_delete = 1;
13741 break;
13742 }
13743
13744 /* Ask user only if there are some breakpoints to delete. */
13745 if (!from_tty
13746 || (breaks_to_delete && query (_("Delete all breakpoints? "))))
13747 {
13748 ALL_BREAKPOINTS_SAFE (b, b_tmp)
13749 if (user_breakpoint_p (b))
13750 delete_breakpoint (b);
13751 }
13752 }
13753 else
13754 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
13755 }
13756
13757 static int
13758 all_locations_are_pending (struct bp_location *loc)
13759 {
13760 for (; loc; loc = loc->next)
13761 if (!loc->shlib_disabled
13762 && !loc->pspace->executing_startup)
13763 return 0;
13764 return 1;
13765 }
13766
13767 /* Subroutine of update_breakpoint_locations to simplify it.
13768 Return non-zero if multiple fns in list LOC have the same name.
13769 Null names are ignored. */
13770
13771 static int
13772 ambiguous_names_p (struct bp_location *loc)
13773 {
13774 struct bp_location *l;
13775 htab_t htab = htab_create_alloc (13, htab_hash_string,
13776 (int (*) (const void *,
13777 const void *)) streq,
13778 NULL, xcalloc, xfree);
13779
13780 for (l = loc; l != NULL; l = l->next)
13781 {
13782 const char **slot;
13783 const char *name = l->function_name;
13784
13785 /* Allow for some names to be NULL, ignore them. */
13786 if (name == NULL)
13787 continue;
13788
13789 slot = (const char **) htab_find_slot (htab, (const void *) name,
13790 INSERT);
13791 /* NOTE: We can assume slot != NULL here because xcalloc never
13792 returns NULL. */
13793 if (*slot != NULL)
13794 {
13795 htab_delete (htab);
13796 return 1;
13797 }
13798 *slot = name;
13799 }
13800
13801 htab_delete (htab);
13802 return 0;
13803 }
13804
13805 /* When symbols change, it probably means the sources changed as well,
13806 and it might mean the static tracepoint markers are no longer at
13807 the same address or line numbers they used to be at last we
13808 checked. Losing your static tracepoints whenever you rebuild is
13809 undesirable. This function tries to resync/rematch gdb static
13810 tracepoints with the markers on the target, for static tracepoints
13811 that have not been set by marker id. Static tracepoint that have
13812 been set by marker id are reset by marker id in breakpoint_re_set.
13813 The heuristic is:
13814
13815 1) For a tracepoint set at a specific address, look for a marker at
13816 the old PC. If one is found there, assume to be the same marker.
13817 If the name / string id of the marker found is different from the
13818 previous known name, assume that means the user renamed the marker
13819 in the sources, and output a warning.
13820
13821 2) For a tracepoint set at a given line number, look for a marker
13822 at the new address of the old line number. If one is found there,
13823 assume to be the same marker. If the name / string id of the
13824 marker found is different from the previous known name, assume that
13825 means the user renamed the marker in the sources, and output a
13826 warning.
13827
13828 3) If a marker is no longer found at the same address or line, it
13829 may mean the marker no longer exists. But it may also just mean
13830 the code changed a bit. Maybe the user added a few lines of code
13831 that made the marker move up or down (in line number terms). Ask
13832 the target for info about the marker with the string id as we knew
13833 it. If found, update line number and address in the matching
13834 static tracepoint. This will get confused if there's more than one
13835 marker with the same ID (possible in UST, although unadvised
13836 precisely because it confuses tools). */
13837
13838 static struct symtab_and_line
13839 update_static_tracepoint (struct breakpoint *b, struct symtab_and_line sal)
13840 {
13841 struct tracepoint *tp = (struct tracepoint *) b;
13842 struct static_tracepoint_marker marker;
13843 CORE_ADDR pc;
13844
13845 pc = sal.pc;
13846 if (sal.line)
13847 find_line_pc (sal.symtab, sal.line, &pc);
13848
13849 if (target_static_tracepoint_marker_at (pc, &marker))
13850 {
13851 if (strcmp (tp->static_trace_marker_id, marker.str_id) != 0)
13852 warning (_("static tracepoint %d changed probed marker from %s to %s"),
13853 b->number,
13854 tp->static_trace_marker_id, marker.str_id);
13855
13856 xfree (tp->static_trace_marker_id);
13857 tp->static_trace_marker_id = xstrdup (marker.str_id);
13858 release_static_tracepoint_marker (&marker);
13859
13860 return sal;
13861 }
13862
13863 /* Old marker wasn't found on target at lineno. Try looking it up
13864 by string ID. */
13865 if (!sal.explicit_pc
13866 && sal.line != 0
13867 && sal.symtab != NULL
13868 && tp->static_trace_marker_id != NULL)
13869 {
13870 VEC(static_tracepoint_marker_p) *markers;
13871
13872 markers
13873 = target_static_tracepoint_markers_by_strid (tp->static_trace_marker_id);
13874
13875 if (!VEC_empty(static_tracepoint_marker_p, markers))
13876 {
13877 struct symtab_and_line sal2;
13878 struct symbol *sym;
13879 struct static_tracepoint_marker *tpmarker;
13880 struct ui_out *uiout = current_uiout;
13881
13882 tpmarker = VEC_index (static_tracepoint_marker_p, markers, 0);
13883
13884 xfree (tp->static_trace_marker_id);
13885 tp->static_trace_marker_id = xstrdup (tpmarker->str_id);
13886
13887 warning (_("marker for static tracepoint %d (%s) not "
13888 "found at previous line number"),
13889 b->number, tp->static_trace_marker_id);
13890
13891 init_sal (&sal2);
13892
13893 sal2.pc = tpmarker->address;
13894
13895 sal2 = find_pc_line (tpmarker->address, 0);
13896 sym = find_pc_sect_function (tpmarker->address, NULL);
13897 ui_out_text (uiout, "Now in ");
13898 if (sym)
13899 {
13900 ui_out_field_string (uiout, "func",
13901 SYMBOL_PRINT_NAME (sym));
13902 ui_out_text (uiout, " at ");
13903 }
13904 ui_out_field_string (uiout, "file",
13905 symtab_to_filename_for_display (sal2.symtab));
13906 ui_out_text (uiout, ":");
13907
13908 if (ui_out_is_mi_like_p (uiout))
13909 {
13910 const char *fullname = symtab_to_fullname (sal2.symtab);
13911
13912 ui_out_field_string (uiout, "fullname", fullname);
13913 }
13914
13915 ui_out_field_int (uiout, "line", sal2.line);
13916 ui_out_text (uiout, "\n");
13917
13918 b->loc->line_number = sal2.line;
13919 b->loc->symtab = sym != NULL ? sal2.symtab : NULL;
13920
13921 xfree (b->addr_string);
13922 b->addr_string = xstrprintf ("%s:%d",
13923 symtab_to_filename_for_display (sal2.symtab),
13924 b->loc->line_number);
13925
13926 /* Might be nice to check if function changed, and warn if
13927 so. */
13928
13929 release_static_tracepoint_marker (tpmarker);
13930 }
13931 }
13932 return sal;
13933 }
13934
13935 /* Returns 1 iff locations A and B are sufficiently same that
13936 we don't need to report breakpoint as changed. */
13937
13938 static int
13939 locations_are_equal (struct bp_location *a, struct bp_location *b)
13940 {
13941 while (a && b)
13942 {
13943 if (a->address != b->address)
13944 return 0;
13945
13946 if (a->shlib_disabled != b->shlib_disabled)
13947 return 0;
13948
13949 if (a->enabled != b->enabled)
13950 return 0;
13951
13952 a = a->next;
13953 b = b->next;
13954 }
13955
13956 if ((a == NULL) != (b == NULL))
13957 return 0;
13958
13959 return 1;
13960 }
13961
13962 /* Create new breakpoint locations for B (a hardware or software breakpoint)
13963 based on SALS and SALS_END. If SALS_END.NELTS is not zero, then B is
13964 a ranged breakpoint. */
13965
13966 void
13967 update_breakpoint_locations (struct breakpoint *b,
13968 struct symtabs_and_lines sals,
13969 struct symtabs_and_lines sals_end)
13970 {
13971 int i;
13972 struct bp_location *existing_locations = b->loc;
13973
13974 if (sals_end.nelts != 0 && (sals.nelts != 1 || sals_end.nelts != 1))
13975 {
13976 /* Ranged breakpoints have only one start location and one end
13977 location. */
13978 b->enable_state = bp_disabled;
13979 update_global_location_list (UGLL_MAY_INSERT);
13980 printf_unfiltered (_("Could not reset ranged breakpoint %d: "
13981 "multiple locations found\n"),
13982 b->number);
13983 return;
13984 }
13985
13986 /* If there's no new locations, and all existing locations are
13987 pending, don't do anything. This optimizes the common case where
13988 all locations are in the same shared library, that was unloaded.
13989 We'd like to retain the location, so that when the library is
13990 loaded again, we don't loose the enabled/disabled status of the
13991 individual locations. */
13992 if (all_locations_are_pending (existing_locations) && sals.nelts == 0)
13993 return;
13994
13995 b->loc = NULL;
13996
13997 for (i = 0; i < sals.nelts; ++i)
13998 {
13999 struct bp_location *new_loc;
14000
14001 switch_to_program_space_and_thread (sals.sals[i].pspace);
14002
14003 new_loc = add_location_to_breakpoint (b, &(sals.sals[i]));
14004
14005 /* Reparse conditions, they might contain references to the
14006 old symtab. */
14007 if (b->cond_string != NULL)
14008 {
14009 const char *s;
14010
14011 s = b->cond_string;
14012 TRY
14013 {
14014 new_loc->cond = parse_exp_1 (&s, sals.sals[i].pc,
14015 block_for_pc (sals.sals[i].pc),
14016 0);
14017 }
14018 CATCH (e, RETURN_MASK_ERROR)
14019 {
14020 warning (_("failed to reevaluate condition "
14021 "for breakpoint %d: %s"),
14022 b->number, e.message);
14023 new_loc->enabled = 0;
14024 }
14025 END_CATCH
14026 }
14027
14028 if (sals_end.nelts)
14029 {
14030 CORE_ADDR end = find_breakpoint_range_end (sals_end.sals[0]);
14031
14032 new_loc->length = end - sals.sals[0].pc + 1;
14033 }
14034 }
14035
14036 /* If possible, carry over 'disable' status from existing
14037 breakpoints. */
14038 {
14039 struct bp_location *e = existing_locations;
14040 /* If there are multiple breakpoints with the same function name,
14041 e.g. for inline functions, comparing function names won't work.
14042 Instead compare pc addresses; this is just a heuristic as things
14043 may have moved, but in practice it gives the correct answer
14044 often enough until a better solution is found. */
14045 int have_ambiguous_names = ambiguous_names_p (b->loc);
14046
14047 for (; e; e = e->next)
14048 {
14049 if (!e->enabled && e->function_name)
14050 {
14051 struct bp_location *l = b->loc;
14052 if (have_ambiguous_names)
14053 {
14054 for (; l; l = l->next)
14055 if (breakpoint_locations_match (e, l))
14056 {
14057 l->enabled = 0;
14058 break;
14059 }
14060 }
14061 else
14062 {
14063 for (; l; l = l->next)
14064 if (l->function_name
14065 && strcmp (e->function_name, l->function_name) == 0)
14066 {
14067 l->enabled = 0;
14068 break;
14069 }
14070 }
14071 }
14072 }
14073 }
14074
14075 if (!locations_are_equal (existing_locations, b->loc))
14076 observer_notify_breakpoint_modified (b);
14077
14078 update_global_location_list (UGLL_MAY_INSERT);
14079 }
14080
14081 /* Find the SaL locations corresponding to the given ADDR_STRING.
14082 On return, FOUND will be 1 if any SaL was found, zero otherwise. */
14083
14084 static struct symtabs_and_lines
14085 addr_string_to_sals (struct breakpoint *b, char *addr_string, int *found)
14086 {
14087 char *s;
14088 struct symtabs_and_lines sals = {0};
14089 struct gdb_exception exception = exception_none;
14090
14091 gdb_assert (b->ops != NULL);
14092 s = addr_string;
14093
14094 TRY
14095 {
14096 b->ops->decode_linespec (b, &s, &sals);
14097 }
14098 CATCH (e, RETURN_MASK_ERROR)
14099 {
14100 int not_found_and_ok = 0;
14101
14102 exception = e;
14103
14104 /* For pending breakpoints, it's expected that parsing will
14105 fail until the right shared library is loaded. User has
14106 already told to create pending breakpoints and don't need
14107 extra messages. If breakpoint is in bp_shlib_disabled
14108 state, then user already saw the message about that
14109 breakpoint being disabled, and don't want to see more
14110 errors. */
14111 if (e.error == NOT_FOUND_ERROR
14112 && (b->condition_not_parsed
14113 || (b->loc && b->loc->shlib_disabled)
14114 || (b->loc && b->loc->pspace->executing_startup)
14115 || b->enable_state == bp_disabled))
14116 not_found_and_ok = 1;
14117
14118 if (!not_found_and_ok)
14119 {
14120 /* We surely don't want to warn about the same breakpoint
14121 10 times. One solution, implemented here, is disable
14122 the breakpoint on error. Another solution would be to
14123 have separate 'warning emitted' flag. Since this
14124 happens only when a binary has changed, I don't know
14125 which approach is better. */
14126 b->enable_state = bp_disabled;
14127 throw_exception (e);
14128 }
14129 }
14130 END_CATCH
14131
14132 if (exception.reason == 0 || exception.error != NOT_FOUND_ERROR)
14133 {
14134 int i;
14135
14136 for (i = 0; i < sals.nelts; ++i)
14137 resolve_sal_pc (&sals.sals[i]);
14138 if (b->condition_not_parsed && s && s[0])
14139 {
14140 char *cond_string, *extra_string;
14141 int thread, task;
14142
14143 find_condition_and_thread (s, sals.sals[0].pc,
14144 &cond_string, &thread, &task,
14145 &extra_string);
14146 if (cond_string)
14147 b->cond_string = cond_string;
14148 b->thread = thread;
14149 b->task = task;
14150 if (extra_string)
14151 b->extra_string = extra_string;
14152 b->condition_not_parsed = 0;
14153 }
14154
14155 if (b->type == bp_static_tracepoint && !strace_marker_p (b))
14156 sals.sals[0] = update_static_tracepoint (b, sals.sals[0]);
14157
14158 *found = 1;
14159 }
14160 else
14161 *found = 0;
14162
14163 return sals;
14164 }
14165
14166 /* The default re_set method, for typical hardware or software
14167 breakpoints. Reevaluate the breakpoint and recreate its
14168 locations. */
14169
14170 static void
14171 breakpoint_re_set_default (struct breakpoint *b)
14172 {
14173 int found;
14174 struct symtabs_and_lines sals, sals_end;
14175 struct symtabs_and_lines expanded = {0};
14176 struct symtabs_and_lines expanded_end = {0};
14177
14178 sals = addr_string_to_sals (b, b->addr_string, &found);
14179 if (found)
14180 {
14181 make_cleanup (xfree, sals.sals);
14182 expanded = sals;
14183 }
14184
14185 if (b->addr_string_range_end)
14186 {
14187 sals_end = addr_string_to_sals (b, b->addr_string_range_end, &found);
14188 if (found)
14189 {
14190 make_cleanup (xfree, sals_end.sals);
14191 expanded_end = sals_end;
14192 }
14193 }
14194
14195 update_breakpoint_locations (b, expanded, expanded_end);
14196 }
14197
14198 /* Default method for creating SALs from an address string. It basically
14199 calls parse_breakpoint_sals. Return 1 for success, zero for failure. */
14200
14201 static void
14202 create_sals_from_address_default (char **arg,
14203 struct linespec_result *canonical,
14204 enum bptype type_wanted,
14205 char *addr_start, char **copy_arg)
14206 {
14207 parse_breakpoint_sals (arg, canonical);
14208 }
14209
14210 /* Call create_breakpoints_sal for the given arguments. This is the default
14211 function for the `create_breakpoints_sal' method of
14212 breakpoint_ops. */
14213
14214 static void
14215 create_breakpoints_sal_default (struct gdbarch *gdbarch,
14216 struct linespec_result *canonical,
14217 char *cond_string,
14218 char *extra_string,
14219 enum bptype type_wanted,
14220 enum bpdisp disposition,
14221 int thread,
14222 int task, int ignore_count,
14223 const struct breakpoint_ops *ops,
14224 int from_tty, int enabled,
14225 int internal, unsigned flags)
14226 {
14227 create_breakpoints_sal (gdbarch, canonical, cond_string,
14228 extra_string,
14229 type_wanted, disposition,
14230 thread, task, ignore_count, ops, from_tty,
14231 enabled, internal, flags);
14232 }
14233
14234 /* Decode the line represented by S by calling decode_line_full. This is the
14235 default function for the `decode_linespec' method of breakpoint_ops. */
14236
14237 static void
14238 decode_linespec_default (struct breakpoint *b, char **s,
14239 struct symtabs_and_lines *sals)
14240 {
14241 struct linespec_result canonical;
14242
14243 init_linespec_result (&canonical);
14244 decode_line_full (s, DECODE_LINE_FUNFIRSTLINE,
14245 (struct symtab *) NULL, 0,
14246 &canonical, multiple_symbols_all,
14247 b->filter);
14248
14249 /* We should get 0 or 1 resulting SALs. */
14250 gdb_assert (VEC_length (linespec_sals, canonical.sals) < 2);
14251
14252 if (VEC_length (linespec_sals, canonical.sals) > 0)
14253 {
14254 struct linespec_sals *lsal;
14255
14256 lsal = VEC_index (linespec_sals, canonical.sals, 0);
14257 *sals = lsal->sals;
14258 /* Arrange it so the destructor does not free the
14259 contents. */
14260 lsal->sals.sals = NULL;
14261 }
14262
14263 destroy_linespec_result (&canonical);
14264 }
14265
14266 /* Prepare the global context for a re-set of breakpoint B. */
14267
14268 static struct cleanup *
14269 prepare_re_set_context (struct breakpoint *b)
14270 {
14271 struct cleanup *cleanups;
14272
14273 input_radix = b->input_radix;
14274 cleanups = save_current_space_and_thread ();
14275 if (b->pspace != NULL)
14276 switch_to_program_space_and_thread (b->pspace);
14277 set_language (b->language);
14278
14279 return cleanups;
14280 }
14281
14282 /* Reset a breakpoint given it's struct breakpoint * BINT.
14283 The value we return ends up being the return value from catch_errors.
14284 Unused in this case. */
14285
14286 static int
14287 breakpoint_re_set_one (void *bint)
14288 {
14289 /* Get past catch_errs. */
14290 struct breakpoint *b = (struct breakpoint *) bint;
14291 struct cleanup *cleanups;
14292
14293 cleanups = prepare_re_set_context (b);
14294 b->ops->re_set (b);
14295 do_cleanups (cleanups);
14296 return 0;
14297 }
14298
14299 /* Re-set all breakpoints after symbols have been re-loaded. */
14300 void
14301 breakpoint_re_set (void)
14302 {
14303 struct breakpoint *b, *b_tmp;
14304 enum language save_language;
14305 int save_input_radix;
14306 struct cleanup *old_chain;
14307
14308 save_language = current_language->la_language;
14309 save_input_radix = input_radix;
14310 old_chain = save_current_program_space ();
14311
14312 ALL_BREAKPOINTS_SAFE (b, b_tmp)
14313 {
14314 /* Format possible error msg. */
14315 char *message = xstrprintf ("Error in re-setting breakpoint %d: ",
14316 b->number);
14317 struct cleanup *cleanups = make_cleanup (xfree, message);
14318 catch_errors (breakpoint_re_set_one, b, message, RETURN_MASK_ALL);
14319 do_cleanups (cleanups);
14320 }
14321 set_language (save_language);
14322 input_radix = save_input_radix;
14323
14324 jit_breakpoint_re_set ();
14325
14326 do_cleanups (old_chain);
14327
14328 create_overlay_event_breakpoint ();
14329 create_longjmp_master_breakpoint ();
14330 create_std_terminate_master_breakpoint ();
14331 create_exception_master_breakpoint ();
14332 }
14333 \f
14334 /* Reset the thread number of this breakpoint:
14335
14336 - If the breakpoint is for all threads, leave it as-is.
14337 - Else, reset it to the current thread for inferior_ptid. */
14338 void
14339 breakpoint_re_set_thread (struct breakpoint *b)
14340 {
14341 if (b->thread != -1)
14342 {
14343 if (in_thread_list (inferior_ptid))
14344 b->thread = pid_to_thread_id (inferior_ptid);
14345
14346 /* We're being called after following a fork. The new fork is
14347 selected as current, and unless this was a vfork will have a
14348 different program space from the original thread. Reset that
14349 as well. */
14350 b->loc->pspace = current_program_space;
14351 }
14352 }
14353
14354 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14355 If from_tty is nonzero, it prints a message to that effect,
14356 which ends with a period (no newline). */
14357
14358 void
14359 set_ignore_count (int bptnum, int count, int from_tty)
14360 {
14361 struct breakpoint *b;
14362
14363 if (count < 0)
14364 count = 0;
14365
14366 ALL_BREAKPOINTS (b)
14367 if (b->number == bptnum)
14368 {
14369 if (is_tracepoint (b))
14370 {
14371 if (from_tty && count != 0)
14372 printf_filtered (_("Ignore count ignored for tracepoint %d."),
14373 bptnum);
14374 return;
14375 }
14376
14377 b->ignore_count = count;
14378 if (from_tty)
14379 {
14380 if (count == 0)
14381 printf_filtered (_("Will stop next time "
14382 "breakpoint %d is reached."),
14383 bptnum);
14384 else if (count == 1)
14385 printf_filtered (_("Will ignore next crossing of breakpoint %d."),
14386 bptnum);
14387 else
14388 printf_filtered (_("Will ignore next %d "
14389 "crossings of breakpoint %d."),
14390 count, bptnum);
14391 }
14392 observer_notify_breakpoint_modified (b);
14393 return;
14394 }
14395
14396 error (_("No breakpoint number %d."), bptnum);
14397 }
14398
14399 /* Command to set ignore-count of breakpoint N to COUNT. */
14400
14401 static void
14402 ignore_command (char *args, int from_tty)
14403 {
14404 char *p = args;
14405 int num;
14406
14407 if (p == 0)
14408 error_no_arg (_("a breakpoint number"));
14409
14410 num = get_number (&p);
14411 if (num == 0)
14412 error (_("bad breakpoint number: '%s'"), args);
14413 if (*p == 0)
14414 error (_("Second argument (specified ignore-count) is missing."));
14415
14416 set_ignore_count (num,
14417 longest_to_int (value_as_long (parse_and_eval (p))),
14418 from_tty);
14419 if (from_tty)
14420 printf_filtered ("\n");
14421 }
14422 \f
14423 /* Call FUNCTION on each of the breakpoints
14424 whose numbers are given in ARGS. */
14425
14426 static void
14427 map_breakpoint_numbers (char *args, void (*function) (struct breakpoint *,
14428 void *),
14429 void *data)
14430 {
14431 int num;
14432 struct breakpoint *b, *tmp;
14433 int match;
14434 struct get_number_or_range_state state;
14435
14436 if (args == 0 || *args == '\0')
14437 error_no_arg (_("one or more breakpoint numbers"));
14438
14439 init_number_or_range (&state, args);
14440
14441 while (!state.finished)
14442 {
14443 const char *p = state.string;
14444
14445 match = 0;
14446
14447 num = get_number_or_range (&state);
14448 if (num == 0)
14449 {
14450 warning (_("bad breakpoint number at or near '%s'"), p);
14451 }
14452 else
14453 {
14454 ALL_BREAKPOINTS_SAFE (b, tmp)
14455 if (b->number == num)
14456 {
14457 match = 1;
14458 function (b, data);
14459 break;
14460 }
14461 if (match == 0)
14462 printf_unfiltered (_("No breakpoint number %d.\n"), num);
14463 }
14464 }
14465 }
14466
14467 static struct bp_location *
14468 find_location_by_number (char *number)
14469 {
14470 char *dot = strchr (number, '.');
14471 char *p1;
14472 int bp_num;
14473 int loc_num;
14474 struct breakpoint *b;
14475 struct bp_location *loc;
14476
14477 *dot = '\0';
14478
14479 p1 = number;
14480 bp_num = get_number (&p1);
14481 if (bp_num == 0)
14482 error (_("Bad breakpoint number '%s'"), number);
14483
14484 ALL_BREAKPOINTS (b)
14485 if (b->number == bp_num)
14486 {
14487 break;
14488 }
14489
14490 if (!b || b->number != bp_num)
14491 error (_("Bad breakpoint number '%s'"), number);
14492
14493 p1 = dot+1;
14494 loc_num = get_number (&p1);
14495 if (loc_num == 0)
14496 error (_("Bad breakpoint location number '%s'"), number);
14497
14498 --loc_num;
14499 loc = b->loc;
14500 for (;loc_num && loc; --loc_num, loc = loc->next)
14501 ;
14502 if (!loc)
14503 error (_("Bad breakpoint location number '%s'"), dot+1);
14504
14505 return loc;
14506 }
14507
14508
14509 /* Set ignore-count of breakpoint number BPTNUM to COUNT.
14510 If from_tty is nonzero, it prints a message to that effect,
14511 which ends with a period (no newline). */
14512
14513 void
14514 disable_breakpoint (struct breakpoint *bpt)
14515 {
14516 /* Never disable a watchpoint scope breakpoint; we want to
14517 hit them when we leave scope so we can delete both the
14518 watchpoint and its scope breakpoint at that time. */
14519 if (bpt->type == bp_watchpoint_scope)
14520 return;
14521
14522 bpt->enable_state = bp_disabled;
14523
14524 /* Mark breakpoint locations modified. */
14525 mark_breakpoint_modified (bpt);
14526
14527 if (target_supports_enable_disable_tracepoint ()
14528 && current_trace_status ()->running && is_tracepoint (bpt))
14529 {
14530 struct bp_location *location;
14531
14532 for (location = bpt->loc; location; location = location->next)
14533 target_disable_tracepoint (location);
14534 }
14535
14536 update_global_location_list (UGLL_DONT_INSERT);
14537
14538 observer_notify_breakpoint_modified (bpt);
14539 }
14540
14541 /* A callback for iterate_over_related_breakpoints. */
14542
14543 static void
14544 do_disable_breakpoint (struct breakpoint *b, void *ignore)
14545 {
14546 disable_breakpoint (b);
14547 }
14548
14549 /* A callback for map_breakpoint_numbers that calls
14550 disable_breakpoint. */
14551
14552 static void
14553 do_map_disable_breakpoint (struct breakpoint *b, void *ignore)
14554 {
14555 iterate_over_related_breakpoints (b, do_disable_breakpoint, NULL);
14556 }
14557
14558 static void
14559 disable_command (char *args, int from_tty)
14560 {
14561 if (args == 0)
14562 {
14563 struct breakpoint *bpt;
14564
14565 ALL_BREAKPOINTS (bpt)
14566 if (user_breakpoint_p (bpt))
14567 disable_breakpoint (bpt);
14568 }
14569 else
14570 {
14571 char *num = extract_arg (&args);
14572
14573 while (num)
14574 {
14575 if (strchr (num, '.'))
14576 {
14577 struct bp_location *loc = find_location_by_number (num);
14578
14579 if (loc)
14580 {
14581 if (loc->enabled)
14582 {
14583 loc->enabled = 0;
14584 mark_breakpoint_location_modified (loc);
14585 }
14586 if (target_supports_enable_disable_tracepoint ()
14587 && current_trace_status ()->running && loc->owner
14588 && is_tracepoint (loc->owner))
14589 target_disable_tracepoint (loc);
14590 }
14591 update_global_location_list (UGLL_DONT_INSERT);
14592 }
14593 else
14594 map_breakpoint_numbers (num, do_map_disable_breakpoint, NULL);
14595 num = extract_arg (&args);
14596 }
14597 }
14598 }
14599
14600 static void
14601 enable_breakpoint_disp (struct breakpoint *bpt, enum bpdisp disposition,
14602 int count)
14603 {
14604 int target_resources_ok;
14605
14606 if (bpt->type == bp_hardware_breakpoint)
14607 {
14608 int i;
14609 i = hw_breakpoint_used_count ();
14610 target_resources_ok =
14611 target_can_use_hardware_watchpoint (bp_hardware_breakpoint,
14612 i + 1, 0);
14613 if (target_resources_ok == 0)
14614 error (_("No hardware breakpoint support in the target."));
14615 else if (target_resources_ok < 0)
14616 error (_("Hardware breakpoints used exceeds limit."));
14617 }
14618
14619 if (is_watchpoint (bpt))
14620 {
14621 /* Initialize it just to avoid a GCC false warning. */
14622 enum enable_state orig_enable_state = 0;
14623
14624 TRY
14625 {
14626 struct watchpoint *w = (struct watchpoint *) bpt;
14627
14628 orig_enable_state = bpt->enable_state;
14629 bpt->enable_state = bp_enabled;
14630 update_watchpoint (w, 1 /* reparse */);
14631 }
14632 CATCH (e, RETURN_MASK_ALL)
14633 {
14634 bpt->enable_state = orig_enable_state;
14635 exception_fprintf (gdb_stderr, e, _("Cannot enable watchpoint %d: "),
14636 bpt->number);
14637 return;
14638 }
14639 END_CATCH
14640 }
14641
14642 bpt->enable_state = bp_enabled;
14643
14644 /* Mark breakpoint locations modified. */
14645 mark_breakpoint_modified (bpt);
14646
14647 if (target_supports_enable_disable_tracepoint ()
14648 && current_trace_status ()->running && is_tracepoint (bpt))
14649 {
14650 struct bp_location *location;
14651
14652 for (location = bpt->loc; location; location = location->next)
14653 target_enable_tracepoint (location);
14654 }
14655
14656 bpt->disposition = disposition;
14657 bpt->enable_count = count;
14658 update_global_location_list (UGLL_MAY_INSERT);
14659
14660 observer_notify_breakpoint_modified (bpt);
14661 }
14662
14663
14664 void
14665 enable_breakpoint (struct breakpoint *bpt)
14666 {
14667 enable_breakpoint_disp (bpt, bpt->disposition, 0);
14668 }
14669
14670 static void
14671 do_enable_breakpoint (struct breakpoint *bpt, void *arg)
14672 {
14673 enable_breakpoint (bpt);
14674 }
14675
14676 /* A callback for map_breakpoint_numbers that calls
14677 enable_breakpoint. */
14678
14679 static void
14680 do_map_enable_breakpoint (struct breakpoint *b, void *ignore)
14681 {
14682 iterate_over_related_breakpoints (b, do_enable_breakpoint, NULL);
14683 }
14684
14685 /* The enable command enables the specified breakpoints (or all defined
14686 breakpoints) so they once again become (or continue to be) effective
14687 in stopping the inferior. */
14688
14689 static void
14690 enable_command (char *args, int from_tty)
14691 {
14692 if (args == 0)
14693 {
14694 struct breakpoint *bpt;
14695
14696 ALL_BREAKPOINTS (bpt)
14697 if (user_breakpoint_p (bpt))
14698 enable_breakpoint (bpt);
14699 }
14700 else
14701 {
14702 char *num = extract_arg (&args);
14703
14704 while (num)
14705 {
14706 if (strchr (num, '.'))
14707 {
14708 struct bp_location *loc = find_location_by_number (num);
14709
14710 if (loc)
14711 {
14712 if (!loc->enabled)
14713 {
14714 loc->enabled = 1;
14715 mark_breakpoint_location_modified (loc);
14716 }
14717 if (target_supports_enable_disable_tracepoint ()
14718 && current_trace_status ()->running && loc->owner
14719 && is_tracepoint (loc->owner))
14720 target_enable_tracepoint (loc);
14721 }
14722 update_global_location_list (UGLL_MAY_INSERT);
14723 }
14724 else
14725 map_breakpoint_numbers (num, do_map_enable_breakpoint, NULL);
14726 num = extract_arg (&args);
14727 }
14728 }
14729 }
14730
14731 /* This struct packages up disposition data for application to multiple
14732 breakpoints. */
14733
14734 struct disp_data
14735 {
14736 enum bpdisp disp;
14737 int count;
14738 };
14739
14740 static void
14741 do_enable_breakpoint_disp (struct breakpoint *bpt, void *arg)
14742 {
14743 struct disp_data disp_data = *(struct disp_data *) arg;
14744
14745 enable_breakpoint_disp (bpt, disp_data.disp, disp_data.count);
14746 }
14747
14748 static void
14749 do_map_enable_once_breakpoint (struct breakpoint *bpt, void *ignore)
14750 {
14751 struct disp_data disp = { disp_disable, 1 };
14752
14753 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14754 }
14755
14756 static void
14757 enable_once_command (char *args, int from_tty)
14758 {
14759 map_breakpoint_numbers (args, do_map_enable_once_breakpoint, NULL);
14760 }
14761
14762 static void
14763 do_map_enable_count_breakpoint (struct breakpoint *bpt, void *countptr)
14764 {
14765 struct disp_data disp = { disp_disable, *(int *) countptr };
14766
14767 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14768 }
14769
14770 static void
14771 enable_count_command (char *args, int from_tty)
14772 {
14773 int count;
14774
14775 if (args == NULL)
14776 error_no_arg (_("hit count"));
14777
14778 count = get_number (&args);
14779
14780 map_breakpoint_numbers (args, do_map_enable_count_breakpoint, &count);
14781 }
14782
14783 static void
14784 do_map_enable_delete_breakpoint (struct breakpoint *bpt, void *ignore)
14785 {
14786 struct disp_data disp = { disp_del, 1 };
14787
14788 iterate_over_related_breakpoints (bpt, do_enable_breakpoint_disp, &disp);
14789 }
14790
14791 static void
14792 enable_delete_command (char *args, int from_tty)
14793 {
14794 map_breakpoint_numbers (args, do_map_enable_delete_breakpoint, NULL);
14795 }
14796 \f
14797 static void
14798 set_breakpoint_cmd (char *args, int from_tty)
14799 {
14800 }
14801
14802 static void
14803 show_breakpoint_cmd (char *args, int from_tty)
14804 {
14805 }
14806
14807 /* Invalidate last known value of any hardware watchpoint if
14808 the memory which that value represents has been written to by
14809 GDB itself. */
14810
14811 static void
14812 invalidate_bp_value_on_memory_change (struct inferior *inferior,
14813 CORE_ADDR addr, ssize_t len,
14814 const bfd_byte *data)
14815 {
14816 struct breakpoint *bp;
14817
14818 ALL_BREAKPOINTS (bp)
14819 if (bp->enable_state == bp_enabled
14820 && bp->type == bp_hardware_watchpoint)
14821 {
14822 struct watchpoint *wp = (struct watchpoint *) bp;
14823
14824 if (wp->val_valid && wp->val)
14825 {
14826 struct bp_location *loc;
14827
14828 for (loc = bp->loc; loc != NULL; loc = loc->next)
14829 if (loc->loc_type == bp_loc_hardware_watchpoint
14830 && loc->address + loc->length > addr
14831 && addr + len > loc->address)
14832 {
14833 value_free (wp->val);
14834 wp->val = NULL;
14835 wp->val_valid = 0;
14836 }
14837 }
14838 }
14839 }
14840
14841 /* Create and insert a breakpoint for software single step. */
14842
14843 void
14844 insert_single_step_breakpoint (struct gdbarch *gdbarch,
14845 struct address_space *aspace,
14846 CORE_ADDR next_pc)
14847 {
14848 struct thread_info *tp = inferior_thread ();
14849 struct symtab_and_line sal;
14850 CORE_ADDR pc = next_pc;
14851
14852 if (tp->control.single_step_breakpoints == NULL)
14853 {
14854 tp->control.single_step_breakpoints
14855 = new_single_step_breakpoint (tp->num, gdbarch);
14856 }
14857
14858 sal = find_pc_line (pc, 0);
14859 sal.pc = pc;
14860 sal.section = find_pc_overlay (pc);
14861 sal.explicit_pc = 1;
14862 add_location_to_breakpoint (tp->control.single_step_breakpoints, &sal);
14863
14864 update_global_location_list (UGLL_INSERT);
14865 }
14866
14867 /* See breakpoint.h. */
14868
14869 int
14870 breakpoint_has_location_inserted_here (struct breakpoint *bp,
14871 struct address_space *aspace,
14872 CORE_ADDR pc)
14873 {
14874 struct bp_location *loc;
14875
14876 for (loc = bp->loc; loc != NULL; loc = loc->next)
14877 if (loc->inserted
14878 && breakpoint_location_address_match (loc, aspace, pc))
14879 return 1;
14880
14881 return 0;
14882 }
14883
14884 /* Check whether a software single-step breakpoint is inserted at
14885 PC. */
14886
14887 int
14888 single_step_breakpoint_inserted_here_p (struct address_space *aspace,
14889 CORE_ADDR pc)
14890 {
14891 struct breakpoint *bpt;
14892
14893 ALL_BREAKPOINTS (bpt)
14894 {
14895 if (bpt->type == bp_single_step
14896 && breakpoint_has_location_inserted_here (bpt, aspace, pc))
14897 return 1;
14898 }
14899 return 0;
14900 }
14901
14902 /* Tracepoint-specific operations. */
14903
14904 /* Set tracepoint count to NUM. */
14905 static void
14906 set_tracepoint_count (int num)
14907 {
14908 tracepoint_count = num;
14909 set_internalvar_integer (lookup_internalvar ("tpnum"), num);
14910 }
14911
14912 static void
14913 trace_command (char *arg, int from_tty)
14914 {
14915 struct breakpoint_ops *ops;
14916 const char *arg_cp = arg;
14917
14918 if (arg && probe_linespec_to_ops (&arg_cp))
14919 ops = &tracepoint_probe_breakpoint_ops;
14920 else
14921 ops = &tracepoint_breakpoint_ops;
14922
14923 create_breakpoint (get_current_arch (),
14924 arg,
14925 NULL, 0, NULL, 1 /* parse arg */,
14926 0 /* tempflag */,
14927 bp_tracepoint /* type_wanted */,
14928 0 /* Ignore count */,
14929 pending_break_support,
14930 ops,
14931 from_tty,
14932 1 /* enabled */,
14933 0 /* internal */, 0);
14934 }
14935
14936 static void
14937 ftrace_command (char *arg, int from_tty)
14938 {
14939 create_breakpoint (get_current_arch (),
14940 arg,
14941 NULL, 0, NULL, 1 /* parse arg */,
14942 0 /* tempflag */,
14943 bp_fast_tracepoint /* type_wanted */,
14944 0 /* Ignore count */,
14945 pending_break_support,
14946 &tracepoint_breakpoint_ops,
14947 from_tty,
14948 1 /* enabled */,
14949 0 /* internal */, 0);
14950 }
14951
14952 /* strace command implementation. Creates a static tracepoint. */
14953
14954 static void
14955 strace_command (char *arg, int from_tty)
14956 {
14957 struct breakpoint_ops *ops;
14958
14959 /* Decide if we are dealing with a static tracepoint marker (`-m'),
14960 or with a normal static tracepoint. */
14961 if (arg && startswith (arg, "-m") && isspace (arg[2]))
14962 ops = &strace_marker_breakpoint_ops;
14963 else
14964 ops = &tracepoint_breakpoint_ops;
14965
14966 create_breakpoint (get_current_arch (),
14967 arg,
14968 NULL, 0, NULL, 1 /* parse arg */,
14969 0 /* tempflag */,
14970 bp_static_tracepoint /* type_wanted */,
14971 0 /* Ignore count */,
14972 pending_break_support,
14973 ops,
14974 from_tty,
14975 1 /* enabled */,
14976 0 /* internal */, 0);
14977 }
14978
14979 /* Set up a fake reader function that gets command lines from a linked
14980 list that was acquired during tracepoint uploading. */
14981
14982 static struct uploaded_tp *this_utp;
14983 static int next_cmd;
14984
14985 static char *
14986 read_uploaded_action (void)
14987 {
14988 char *rslt;
14989
14990 VEC_iterate (char_ptr, this_utp->cmd_strings, next_cmd, rslt);
14991
14992 next_cmd++;
14993
14994 return rslt;
14995 }
14996
14997 /* Given information about a tracepoint as recorded on a target (which
14998 can be either a live system or a trace file), attempt to create an
14999 equivalent GDB tracepoint. This is not a reliable process, since
15000 the target does not necessarily have all the information used when
15001 the tracepoint was originally defined. */
15002
15003 struct tracepoint *
15004 create_tracepoint_from_upload (struct uploaded_tp *utp)
15005 {
15006 char *addr_str, small_buf[100];
15007 struct tracepoint *tp;
15008
15009 if (utp->at_string)
15010 addr_str = utp->at_string;
15011 else
15012 {
15013 /* In the absence of a source location, fall back to raw
15014 address. Since there is no way to confirm that the address
15015 means the same thing as when the trace was started, warn the
15016 user. */
15017 warning (_("Uploaded tracepoint %d has no "
15018 "source location, using raw address"),
15019 utp->number);
15020 xsnprintf (small_buf, sizeof (small_buf), "*%s", hex_string (utp->addr));
15021 addr_str = small_buf;
15022 }
15023
15024 /* There's not much we can do with a sequence of bytecodes. */
15025 if (utp->cond && !utp->cond_string)
15026 warning (_("Uploaded tracepoint %d condition "
15027 "has no source form, ignoring it"),
15028 utp->number);
15029
15030 if (!create_breakpoint (get_current_arch (),
15031 addr_str,
15032 utp->cond_string, -1, NULL,
15033 0 /* parse cond/thread */,
15034 0 /* tempflag */,
15035 utp->type /* type_wanted */,
15036 0 /* Ignore count */,
15037 pending_break_support,
15038 &tracepoint_breakpoint_ops,
15039 0 /* from_tty */,
15040 utp->enabled /* enabled */,
15041 0 /* internal */,
15042 CREATE_BREAKPOINT_FLAGS_INSERTED))
15043 return NULL;
15044
15045 /* Get the tracepoint we just created. */
15046 tp = get_tracepoint (tracepoint_count);
15047 gdb_assert (tp != NULL);
15048
15049 if (utp->pass > 0)
15050 {
15051 xsnprintf (small_buf, sizeof (small_buf), "%d %d", utp->pass,
15052 tp->base.number);
15053
15054 trace_pass_command (small_buf, 0);
15055 }
15056
15057 /* If we have uploaded versions of the original commands, set up a
15058 special-purpose "reader" function and call the usual command line
15059 reader, then pass the result to the breakpoint command-setting
15060 function. */
15061 if (!VEC_empty (char_ptr, utp->cmd_strings))
15062 {
15063 struct command_line *cmd_list;
15064
15065 this_utp = utp;
15066 next_cmd = 0;
15067
15068 cmd_list = read_command_lines_1 (read_uploaded_action, 1, NULL, NULL);
15069
15070 breakpoint_set_commands (&tp->base, cmd_list);
15071 }
15072 else if (!VEC_empty (char_ptr, utp->actions)
15073 || !VEC_empty (char_ptr, utp->step_actions))
15074 warning (_("Uploaded tracepoint %d actions "
15075 "have no source form, ignoring them"),
15076 utp->number);
15077
15078 /* Copy any status information that might be available. */
15079 tp->base.hit_count = utp->hit_count;
15080 tp->traceframe_usage = utp->traceframe_usage;
15081
15082 return tp;
15083 }
15084
15085 /* Print information on tracepoint number TPNUM_EXP, or all if
15086 omitted. */
15087
15088 static void
15089 tracepoints_info (char *args, int from_tty)
15090 {
15091 struct ui_out *uiout = current_uiout;
15092 int num_printed;
15093
15094 num_printed = breakpoint_1 (args, 0, is_tracepoint);
15095
15096 if (num_printed == 0)
15097 {
15098 if (args == NULL || *args == '\0')
15099 ui_out_message (uiout, 0, "No tracepoints.\n");
15100 else
15101 ui_out_message (uiout, 0, "No tracepoint matching '%s'.\n", args);
15102 }
15103
15104 default_collect_info ();
15105 }
15106
15107 /* The 'enable trace' command enables tracepoints.
15108 Not supported by all targets. */
15109 static void
15110 enable_trace_command (char *args, int from_tty)
15111 {
15112 enable_command (args, from_tty);
15113 }
15114
15115 /* The 'disable trace' command disables tracepoints.
15116 Not supported by all targets. */
15117 static void
15118 disable_trace_command (char *args, int from_tty)
15119 {
15120 disable_command (args, from_tty);
15121 }
15122
15123 /* Remove a tracepoint (or all if no argument). */
15124 static void
15125 delete_trace_command (char *arg, int from_tty)
15126 {
15127 struct breakpoint *b, *b_tmp;
15128
15129 dont_repeat ();
15130
15131 if (arg == 0)
15132 {
15133 int breaks_to_delete = 0;
15134
15135 /* Delete all breakpoints if no argument.
15136 Do not delete internal or call-dummy breakpoints, these
15137 have to be deleted with an explicit breakpoint number
15138 argument. */
15139 ALL_TRACEPOINTS (b)
15140 if (is_tracepoint (b) && user_breakpoint_p (b))
15141 {
15142 breaks_to_delete = 1;
15143 break;
15144 }
15145
15146 /* Ask user only if there are some breakpoints to delete. */
15147 if (!from_tty
15148 || (breaks_to_delete && query (_("Delete all tracepoints? "))))
15149 {
15150 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15151 if (is_tracepoint (b) && user_breakpoint_p (b))
15152 delete_breakpoint (b);
15153 }
15154 }
15155 else
15156 map_breakpoint_numbers (arg, do_map_delete_breakpoint, NULL);
15157 }
15158
15159 /* Helper function for trace_pass_command. */
15160
15161 static void
15162 trace_pass_set_count (struct tracepoint *tp, int count, int from_tty)
15163 {
15164 tp->pass_count = count;
15165 observer_notify_breakpoint_modified (&tp->base);
15166 if (from_tty)
15167 printf_filtered (_("Setting tracepoint %d's passcount to %d\n"),
15168 tp->base.number, count);
15169 }
15170
15171 /* Set passcount for tracepoint.
15172
15173 First command argument is passcount, second is tracepoint number.
15174 If tracepoint number omitted, apply to most recently defined.
15175 Also accepts special argument "all". */
15176
15177 static void
15178 trace_pass_command (char *args, int from_tty)
15179 {
15180 struct tracepoint *t1;
15181 unsigned int count;
15182
15183 if (args == 0 || *args == 0)
15184 error (_("passcount command requires an "
15185 "argument (count + optional TP num)"));
15186
15187 count = strtoul (args, &args, 10); /* Count comes first, then TP num. */
15188
15189 args = skip_spaces (args);
15190 if (*args && strncasecmp (args, "all", 3) == 0)
15191 {
15192 struct breakpoint *b;
15193
15194 args += 3; /* Skip special argument "all". */
15195 if (*args)
15196 error (_("Junk at end of arguments."));
15197
15198 ALL_TRACEPOINTS (b)
15199 {
15200 t1 = (struct tracepoint *) b;
15201 trace_pass_set_count (t1, count, from_tty);
15202 }
15203 }
15204 else if (*args == '\0')
15205 {
15206 t1 = get_tracepoint_by_number (&args, NULL);
15207 if (t1)
15208 trace_pass_set_count (t1, count, from_tty);
15209 }
15210 else
15211 {
15212 struct get_number_or_range_state state;
15213
15214 init_number_or_range (&state, args);
15215 while (!state.finished)
15216 {
15217 t1 = get_tracepoint_by_number (&args, &state);
15218 if (t1)
15219 trace_pass_set_count (t1, count, from_tty);
15220 }
15221 }
15222 }
15223
15224 struct tracepoint *
15225 get_tracepoint (int num)
15226 {
15227 struct breakpoint *t;
15228
15229 ALL_TRACEPOINTS (t)
15230 if (t->number == num)
15231 return (struct tracepoint *) t;
15232
15233 return NULL;
15234 }
15235
15236 /* Find the tracepoint with the given target-side number (which may be
15237 different from the tracepoint number after disconnecting and
15238 reconnecting). */
15239
15240 struct tracepoint *
15241 get_tracepoint_by_number_on_target (int num)
15242 {
15243 struct breakpoint *b;
15244
15245 ALL_TRACEPOINTS (b)
15246 {
15247 struct tracepoint *t = (struct tracepoint *) b;
15248
15249 if (t->number_on_target == num)
15250 return t;
15251 }
15252
15253 return NULL;
15254 }
15255
15256 /* Utility: parse a tracepoint number and look it up in the list.
15257 If STATE is not NULL, use, get_number_or_range_state and ignore ARG.
15258 If the argument is missing, the most recent tracepoint
15259 (tracepoint_count) is returned. */
15260
15261 struct tracepoint *
15262 get_tracepoint_by_number (char **arg,
15263 struct get_number_or_range_state *state)
15264 {
15265 struct breakpoint *t;
15266 int tpnum;
15267 char *instring = arg == NULL ? NULL : *arg;
15268
15269 if (state)
15270 {
15271 gdb_assert (!state->finished);
15272 tpnum = get_number_or_range (state);
15273 }
15274 else if (arg == NULL || *arg == NULL || ! **arg)
15275 tpnum = tracepoint_count;
15276 else
15277 tpnum = get_number (arg);
15278
15279 if (tpnum <= 0)
15280 {
15281 if (instring && *instring)
15282 printf_filtered (_("bad tracepoint number at or near '%s'\n"),
15283 instring);
15284 else
15285 printf_filtered (_("No previous tracepoint\n"));
15286 return NULL;
15287 }
15288
15289 ALL_TRACEPOINTS (t)
15290 if (t->number == tpnum)
15291 {
15292 return (struct tracepoint *) t;
15293 }
15294
15295 printf_unfiltered ("No tracepoint number %d.\n", tpnum);
15296 return NULL;
15297 }
15298
15299 void
15300 print_recreate_thread (struct breakpoint *b, struct ui_file *fp)
15301 {
15302 if (b->thread != -1)
15303 fprintf_unfiltered (fp, " thread %d", b->thread);
15304
15305 if (b->task != 0)
15306 fprintf_unfiltered (fp, " task %d", b->task);
15307
15308 fprintf_unfiltered (fp, "\n");
15309 }
15310
15311 /* Save information on user settable breakpoints (watchpoints, etc) to
15312 a new script file named FILENAME. If FILTER is non-NULL, call it
15313 on each breakpoint and only include the ones for which it returns
15314 non-zero. */
15315
15316 static void
15317 save_breakpoints (char *filename, int from_tty,
15318 int (*filter) (const struct breakpoint *))
15319 {
15320 struct breakpoint *tp;
15321 int any = 0;
15322 struct cleanup *cleanup;
15323 struct ui_file *fp;
15324 int extra_trace_bits = 0;
15325
15326 if (filename == 0 || *filename == 0)
15327 error (_("Argument required (file name in which to save)"));
15328
15329 /* See if we have anything to save. */
15330 ALL_BREAKPOINTS (tp)
15331 {
15332 /* Skip internal and momentary breakpoints. */
15333 if (!user_breakpoint_p (tp))
15334 continue;
15335
15336 /* If we have a filter, only save the breakpoints it accepts. */
15337 if (filter && !filter (tp))
15338 continue;
15339
15340 any = 1;
15341
15342 if (is_tracepoint (tp))
15343 {
15344 extra_trace_bits = 1;
15345
15346 /* We can stop searching. */
15347 break;
15348 }
15349 }
15350
15351 if (!any)
15352 {
15353 warning (_("Nothing to save."));
15354 return;
15355 }
15356
15357 filename = tilde_expand (filename);
15358 cleanup = make_cleanup (xfree, filename);
15359 fp = gdb_fopen (filename, "w");
15360 if (!fp)
15361 error (_("Unable to open file '%s' for saving (%s)"),
15362 filename, safe_strerror (errno));
15363 make_cleanup_ui_file_delete (fp);
15364
15365 if (extra_trace_bits)
15366 save_trace_state_variables (fp);
15367
15368 ALL_BREAKPOINTS (tp)
15369 {
15370 /* Skip internal and momentary breakpoints. */
15371 if (!user_breakpoint_p (tp))
15372 continue;
15373
15374 /* If we have a filter, only save the breakpoints it accepts. */
15375 if (filter && !filter (tp))
15376 continue;
15377
15378 tp->ops->print_recreate (tp, fp);
15379
15380 /* Note, we can't rely on tp->number for anything, as we can't
15381 assume the recreated breakpoint numbers will match. Use $bpnum
15382 instead. */
15383
15384 if (tp->cond_string)
15385 fprintf_unfiltered (fp, " condition $bpnum %s\n", tp->cond_string);
15386
15387 if (tp->ignore_count)
15388 fprintf_unfiltered (fp, " ignore $bpnum %d\n", tp->ignore_count);
15389
15390 if (tp->type != bp_dprintf && tp->commands)
15391 {
15392 struct gdb_exception exception;
15393
15394 fprintf_unfiltered (fp, " commands\n");
15395
15396 ui_out_redirect (current_uiout, fp);
15397 TRY
15398 {
15399 print_command_lines (current_uiout, tp->commands->commands, 2);
15400 }
15401 CATCH (ex, RETURN_MASK_ALL)
15402 {
15403 ui_out_redirect (current_uiout, NULL);
15404 throw_exception (ex);
15405 }
15406 END_CATCH
15407
15408 ui_out_redirect (current_uiout, NULL);
15409 fprintf_unfiltered (fp, " end\n");
15410 }
15411
15412 if (tp->enable_state == bp_disabled)
15413 fprintf_unfiltered (fp, "disable $bpnum\n");
15414
15415 /* If this is a multi-location breakpoint, check if the locations
15416 should be individually disabled. Watchpoint locations are
15417 special, and not user visible. */
15418 if (!is_watchpoint (tp) && tp->loc && tp->loc->next)
15419 {
15420 struct bp_location *loc;
15421 int n = 1;
15422
15423 for (loc = tp->loc; loc != NULL; loc = loc->next, n++)
15424 if (!loc->enabled)
15425 fprintf_unfiltered (fp, "disable $bpnum.%d\n", n);
15426 }
15427 }
15428
15429 if (extra_trace_bits && *default_collect)
15430 fprintf_unfiltered (fp, "set default-collect %s\n", default_collect);
15431
15432 if (from_tty)
15433 printf_filtered (_("Saved to file '%s'.\n"), filename);
15434 do_cleanups (cleanup);
15435 }
15436
15437 /* The `save breakpoints' command. */
15438
15439 static void
15440 save_breakpoints_command (char *args, int from_tty)
15441 {
15442 save_breakpoints (args, from_tty, NULL);
15443 }
15444
15445 /* The `save tracepoints' command. */
15446
15447 static void
15448 save_tracepoints_command (char *args, int from_tty)
15449 {
15450 save_breakpoints (args, from_tty, is_tracepoint);
15451 }
15452
15453 /* Create a vector of all tracepoints. */
15454
15455 VEC(breakpoint_p) *
15456 all_tracepoints (void)
15457 {
15458 VEC(breakpoint_p) *tp_vec = 0;
15459 struct breakpoint *tp;
15460
15461 ALL_TRACEPOINTS (tp)
15462 {
15463 VEC_safe_push (breakpoint_p, tp_vec, tp);
15464 }
15465
15466 return tp_vec;
15467 }
15468
15469 \f
15470 /* This help string is used for the break, hbreak, tbreak and thbreak
15471 commands. It is defined as a macro to prevent duplication.
15472 COMMAND should be a string constant containing the name of the
15473 command. */
15474 #define BREAK_ARGS_HELP(command) \
15475 command" [PROBE_MODIFIER] [LOCATION] [thread THREADNUM] [if CONDITION]\n\
15476 PROBE_MODIFIER shall be present if the command is to be placed in a\n\
15477 probe point. Accepted values are `-probe' (for a generic, automatically\n\
15478 guessed probe type), `-probe-stap' (for a SystemTap probe) or \n\
15479 `-probe-dtrace' (for a DTrace probe).\n\
15480 LOCATION may be a line number, function name, or \"*\" and an address.\n\
15481 If a line number is specified, break at start of code for that line.\n\
15482 If a function is specified, break at start of code for that function.\n\
15483 If an address is specified, break at that exact address.\n\
15484 With no LOCATION, uses current execution address of the selected\n\
15485 stack frame. This is useful for breaking on return to a stack frame.\n\
15486 \n\
15487 THREADNUM is the number from \"info threads\".\n\
15488 CONDITION is a boolean expression.\n\
15489 \n\
15490 Multiple breakpoints at one place are permitted, and useful if their\n\
15491 conditions are different.\n\
15492 \n\
15493 Do \"help breakpoints\" for info on other commands dealing with breakpoints."
15494
15495 /* List of subcommands for "catch". */
15496 static struct cmd_list_element *catch_cmdlist;
15497
15498 /* List of subcommands for "tcatch". */
15499 static struct cmd_list_element *tcatch_cmdlist;
15500
15501 void
15502 add_catch_command (char *name, char *docstring,
15503 cmd_sfunc_ftype *sfunc,
15504 completer_ftype *completer,
15505 void *user_data_catch,
15506 void *user_data_tcatch)
15507 {
15508 struct cmd_list_element *command;
15509
15510 command = add_cmd (name, class_breakpoint, NULL, docstring,
15511 &catch_cmdlist);
15512 set_cmd_sfunc (command, sfunc);
15513 set_cmd_context (command, user_data_catch);
15514 set_cmd_completer (command, completer);
15515
15516 command = add_cmd (name, class_breakpoint, NULL, docstring,
15517 &tcatch_cmdlist);
15518 set_cmd_sfunc (command, sfunc);
15519 set_cmd_context (command, user_data_tcatch);
15520 set_cmd_completer (command, completer);
15521 }
15522
15523 static void
15524 save_command (char *arg, int from_tty)
15525 {
15526 printf_unfiltered (_("\"save\" must be followed by "
15527 "the name of a save subcommand.\n"));
15528 help_list (save_cmdlist, "save ", all_commands, gdb_stdout);
15529 }
15530
15531 struct breakpoint *
15532 iterate_over_breakpoints (int (*callback) (struct breakpoint *, void *),
15533 void *data)
15534 {
15535 struct breakpoint *b, *b_tmp;
15536
15537 ALL_BREAKPOINTS_SAFE (b, b_tmp)
15538 {
15539 if ((*callback) (b, data))
15540 return b;
15541 }
15542
15543 return NULL;
15544 }
15545
15546 /* Zero if any of the breakpoint's locations could be a location where
15547 functions have been inlined, nonzero otherwise. */
15548
15549 static int
15550 is_non_inline_function (struct breakpoint *b)
15551 {
15552 /* The shared library event breakpoint is set on the address of a
15553 non-inline function. */
15554 if (b->type == bp_shlib_event)
15555 return 1;
15556
15557 return 0;
15558 }
15559
15560 /* Nonzero if the specified PC cannot be a location where functions
15561 have been inlined. */
15562
15563 int
15564 pc_at_non_inline_function (struct address_space *aspace, CORE_ADDR pc,
15565 const struct target_waitstatus *ws)
15566 {
15567 struct breakpoint *b;
15568 struct bp_location *bl;
15569
15570 ALL_BREAKPOINTS (b)
15571 {
15572 if (!is_non_inline_function (b))
15573 continue;
15574
15575 for (bl = b->loc; bl != NULL; bl = bl->next)
15576 {
15577 if (!bl->shlib_disabled
15578 && bpstat_check_location (bl, aspace, pc, ws))
15579 return 1;
15580 }
15581 }
15582
15583 return 0;
15584 }
15585
15586 /* Remove any references to OBJFILE which is going to be freed. */
15587
15588 void
15589 breakpoint_free_objfile (struct objfile *objfile)
15590 {
15591 struct bp_location **locp, *loc;
15592
15593 ALL_BP_LOCATIONS (loc, locp)
15594 if (loc->symtab != NULL && SYMTAB_OBJFILE (loc->symtab) == objfile)
15595 loc->symtab = NULL;
15596 }
15597
15598 void
15599 initialize_breakpoint_ops (void)
15600 {
15601 static int initialized = 0;
15602
15603 struct breakpoint_ops *ops;
15604
15605 if (initialized)
15606 return;
15607 initialized = 1;
15608
15609 /* The breakpoint_ops structure to be inherit by all kinds of
15610 breakpoints (real breakpoints, i.e., user "break" breakpoints,
15611 internal and momentary breakpoints, etc.). */
15612 ops = &bkpt_base_breakpoint_ops;
15613 *ops = base_breakpoint_ops;
15614 ops->re_set = bkpt_re_set;
15615 ops->insert_location = bkpt_insert_location;
15616 ops->remove_location = bkpt_remove_location;
15617 ops->breakpoint_hit = bkpt_breakpoint_hit;
15618 ops->create_sals_from_address = bkpt_create_sals_from_address;
15619 ops->create_breakpoints_sal = bkpt_create_breakpoints_sal;
15620 ops->decode_linespec = bkpt_decode_linespec;
15621
15622 /* The breakpoint_ops structure to be used in regular breakpoints. */
15623 ops = &bkpt_breakpoint_ops;
15624 *ops = bkpt_base_breakpoint_ops;
15625 ops->re_set = bkpt_re_set;
15626 ops->resources_needed = bkpt_resources_needed;
15627 ops->print_it = bkpt_print_it;
15628 ops->print_mention = bkpt_print_mention;
15629 ops->print_recreate = bkpt_print_recreate;
15630
15631 /* Ranged breakpoints. */
15632 ops = &ranged_breakpoint_ops;
15633 *ops = bkpt_breakpoint_ops;
15634 ops->breakpoint_hit = breakpoint_hit_ranged_breakpoint;
15635 ops->resources_needed = resources_needed_ranged_breakpoint;
15636 ops->print_it = print_it_ranged_breakpoint;
15637 ops->print_one = print_one_ranged_breakpoint;
15638 ops->print_one_detail = print_one_detail_ranged_breakpoint;
15639 ops->print_mention = print_mention_ranged_breakpoint;
15640 ops->print_recreate = print_recreate_ranged_breakpoint;
15641
15642 /* Internal breakpoints. */
15643 ops = &internal_breakpoint_ops;
15644 *ops = bkpt_base_breakpoint_ops;
15645 ops->re_set = internal_bkpt_re_set;
15646 ops->check_status = internal_bkpt_check_status;
15647 ops->print_it = internal_bkpt_print_it;
15648 ops->print_mention = internal_bkpt_print_mention;
15649
15650 /* Momentary breakpoints. */
15651 ops = &momentary_breakpoint_ops;
15652 *ops = bkpt_base_breakpoint_ops;
15653 ops->re_set = momentary_bkpt_re_set;
15654 ops->check_status = momentary_bkpt_check_status;
15655 ops->print_it = momentary_bkpt_print_it;
15656 ops->print_mention = momentary_bkpt_print_mention;
15657
15658 /* Momentary breakpoints for bp_longjmp and bp_exception. */
15659 ops = &longjmp_breakpoint_ops;
15660 *ops = momentary_breakpoint_ops;
15661 ops->dtor = longjmp_bkpt_dtor;
15662
15663 /* Probe breakpoints. */
15664 ops = &bkpt_probe_breakpoint_ops;
15665 *ops = bkpt_breakpoint_ops;
15666 ops->insert_location = bkpt_probe_insert_location;
15667 ops->remove_location = bkpt_probe_remove_location;
15668 ops->create_sals_from_address = bkpt_probe_create_sals_from_address;
15669 ops->decode_linespec = bkpt_probe_decode_linespec;
15670
15671 /* Watchpoints. */
15672 ops = &watchpoint_breakpoint_ops;
15673 *ops = base_breakpoint_ops;
15674 ops->dtor = dtor_watchpoint;
15675 ops->re_set = re_set_watchpoint;
15676 ops->insert_location = insert_watchpoint;
15677 ops->remove_location = remove_watchpoint;
15678 ops->breakpoint_hit = breakpoint_hit_watchpoint;
15679 ops->check_status = check_status_watchpoint;
15680 ops->resources_needed = resources_needed_watchpoint;
15681 ops->works_in_software_mode = works_in_software_mode_watchpoint;
15682 ops->print_it = print_it_watchpoint;
15683 ops->print_mention = print_mention_watchpoint;
15684 ops->print_recreate = print_recreate_watchpoint;
15685 ops->explains_signal = explains_signal_watchpoint;
15686
15687 /* Masked watchpoints. */
15688 ops = &masked_watchpoint_breakpoint_ops;
15689 *ops = watchpoint_breakpoint_ops;
15690 ops->insert_location = insert_masked_watchpoint;
15691 ops->remove_location = remove_masked_watchpoint;
15692 ops->resources_needed = resources_needed_masked_watchpoint;
15693 ops->works_in_software_mode = works_in_software_mode_masked_watchpoint;
15694 ops->print_it = print_it_masked_watchpoint;
15695 ops->print_one_detail = print_one_detail_masked_watchpoint;
15696 ops->print_mention = print_mention_masked_watchpoint;
15697 ops->print_recreate = print_recreate_masked_watchpoint;
15698
15699 /* Tracepoints. */
15700 ops = &tracepoint_breakpoint_ops;
15701 *ops = base_breakpoint_ops;
15702 ops->re_set = tracepoint_re_set;
15703 ops->breakpoint_hit = tracepoint_breakpoint_hit;
15704 ops->print_one_detail = tracepoint_print_one_detail;
15705 ops->print_mention = tracepoint_print_mention;
15706 ops->print_recreate = tracepoint_print_recreate;
15707 ops->create_sals_from_address = tracepoint_create_sals_from_address;
15708 ops->create_breakpoints_sal = tracepoint_create_breakpoints_sal;
15709 ops->decode_linespec = tracepoint_decode_linespec;
15710
15711 /* Probe tracepoints. */
15712 ops = &tracepoint_probe_breakpoint_ops;
15713 *ops = tracepoint_breakpoint_ops;
15714 ops->create_sals_from_address = tracepoint_probe_create_sals_from_address;
15715 ops->decode_linespec = tracepoint_probe_decode_linespec;
15716
15717 /* Static tracepoints with marker (`-m'). */
15718 ops = &strace_marker_breakpoint_ops;
15719 *ops = tracepoint_breakpoint_ops;
15720 ops->create_sals_from_address = strace_marker_create_sals_from_address;
15721 ops->create_breakpoints_sal = strace_marker_create_breakpoints_sal;
15722 ops->decode_linespec = strace_marker_decode_linespec;
15723
15724 /* Fork catchpoints. */
15725 ops = &catch_fork_breakpoint_ops;
15726 *ops = base_breakpoint_ops;
15727 ops->insert_location = insert_catch_fork;
15728 ops->remove_location = remove_catch_fork;
15729 ops->breakpoint_hit = breakpoint_hit_catch_fork;
15730 ops->print_it = print_it_catch_fork;
15731 ops->print_one = print_one_catch_fork;
15732 ops->print_mention = print_mention_catch_fork;
15733 ops->print_recreate = print_recreate_catch_fork;
15734
15735 /* Vfork catchpoints. */
15736 ops = &catch_vfork_breakpoint_ops;
15737 *ops = base_breakpoint_ops;
15738 ops->insert_location = insert_catch_vfork;
15739 ops->remove_location = remove_catch_vfork;
15740 ops->breakpoint_hit = breakpoint_hit_catch_vfork;
15741 ops->print_it = print_it_catch_vfork;
15742 ops->print_one = print_one_catch_vfork;
15743 ops->print_mention = print_mention_catch_vfork;
15744 ops->print_recreate = print_recreate_catch_vfork;
15745
15746 /* Exec catchpoints. */
15747 ops = &catch_exec_breakpoint_ops;
15748 *ops = base_breakpoint_ops;
15749 ops->dtor = dtor_catch_exec;
15750 ops->insert_location = insert_catch_exec;
15751 ops->remove_location = remove_catch_exec;
15752 ops->breakpoint_hit = breakpoint_hit_catch_exec;
15753 ops->print_it = print_it_catch_exec;
15754 ops->print_one = print_one_catch_exec;
15755 ops->print_mention = print_mention_catch_exec;
15756 ops->print_recreate = print_recreate_catch_exec;
15757
15758 /* Solib-related catchpoints. */
15759 ops = &catch_solib_breakpoint_ops;
15760 *ops = base_breakpoint_ops;
15761 ops->dtor = dtor_catch_solib;
15762 ops->insert_location = insert_catch_solib;
15763 ops->remove_location = remove_catch_solib;
15764 ops->breakpoint_hit = breakpoint_hit_catch_solib;
15765 ops->check_status = check_status_catch_solib;
15766 ops->print_it = print_it_catch_solib;
15767 ops->print_one = print_one_catch_solib;
15768 ops->print_mention = print_mention_catch_solib;
15769 ops->print_recreate = print_recreate_catch_solib;
15770
15771 ops = &dprintf_breakpoint_ops;
15772 *ops = bkpt_base_breakpoint_ops;
15773 ops->re_set = dprintf_re_set;
15774 ops->resources_needed = bkpt_resources_needed;
15775 ops->print_it = bkpt_print_it;
15776 ops->print_mention = bkpt_print_mention;
15777 ops->print_recreate = dprintf_print_recreate;
15778 ops->after_condition_true = dprintf_after_condition_true;
15779 ops->breakpoint_hit = dprintf_breakpoint_hit;
15780 }
15781
15782 /* Chain containing all defined "enable breakpoint" subcommands. */
15783
15784 static struct cmd_list_element *enablebreaklist = NULL;
15785
15786 void
15787 _initialize_breakpoint (void)
15788 {
15789 struct cmd_list_element *c;
15790
15791 initialize_breakpoint_ops ();
15792
15793 observer_attach_solib_unloaded (disable_breakpoints_in_unloaded_shlib);
15794 observer_attach_free_objfile (disable_breakpoints_in_freed_objfile);
15795 observer_attach_memory_changed (invalidate_bp_value_on_memory_change);
15796
15797 breakpoint_objfile_key
15798 = register_objfile_data_with_cleanup (NULL, free_breakpoint_probes);
15799
15800 breakpoint_chain = 0;
15801 /* Don't bother to call set_breakpoint_count. $bpnum isn't useful
15802 before a breakpoint is set. */
15803 breakpoint_count = 0;
15804
15805 tracepoint_count = 0;
15806
15807 add_com ("ignore", class_breakpoint, ignore_command, _("\
15808 Set ignore-count of breakpoint number N to COUNT.\n\
15809 Usage is `ignore N COUNT'."));
15810
15811 add_com ("commands", class_breakpoint, commands_command, _("\
15812 Set commands to be executed when a breakpoint is hit.\n\
15813 Give breakpoint number as argument after \"commands\".\n\
15814 With no argument, the targeted breakpoint is the last one set.\n\
15815 The commands themselves follow starting on the next line.\n\
15816 Type a line containing \"end\" to indicate the end of them.\n\
15817 Give \"silent\" as the first line to make the breakpoint silent;\n\
15818 then no output is printed when it is hit, except what the commands print."));
15819
15820 c = add_com ("condition", class_breakpoint, condition_command, _("\
15821 Specify breakpoint number N to break only if COND is true.\n\
15822 Usage is `condition N COND', where N is an integer and COND is an\n\
15823 expression to be evaluated whenever breakpoint N is reached."));
15824 set_cmd_completer (c, condition_completer);
15825
15826 c = add_com ("tbreak", class_breakpoint, tbreak_command, _("\
15827 Set a temporary breakpoint.\n\
15828 Like \"break\" except the breakpoint is only temporary,\n\
15829 so it will be deleted when hit. Equivalent to \"break\" followed\n\
15830 by using \"enable delete\" on the breakpoint number.\n\
15831 \n"
15832 BREAK_ARGS_HELP ("tbreak")));
15833 set_cmd_completer (c, location_completer);
15834
15835 c = add_com ("hbreak", class_breakpoint, hbreak_command, _("\
15836 Set a hardware assisted breakpoint.\n\
15837 Like \"break\" except the breakpoint requires hardware support,\n\
15838 some target hardware may not have this support.\n\
15839 \n"
15840 BREAK_ARGS_HELP ("hbreak")));
15841 set_cmd_completer (c, location_completer);
15842
15843 c = add_com ("thbreak", class_breakpoint, thbreak_command, _("\
15844 Set a temporary hardware assisted breakpoint.\n\
15845 Like \"hbreak\" except the breakpoint is only temporary,\n\
15846 so it will be deleted when hit.\n\
15847 \n"
15848 BREAK_ARGS_HELP ("thbreak")));
15849 set_cmd_completer (c, location_completer);
15850
15851 add_prefix_cmd ("enable", class_breakpoint, enable_command, _("\
15852 Enable some breakpoints.\n\
15853 Give breakpoint numbers (separated by spaces) as arguments.\n\
15854 With no subcommand, breakpoints are enabled until you command otherwise.\n\
15855 This is used to cancel the effect of the \"disable\" command.\n\
15856 With a subcommand you can enable temporarily."),
15857 &enablelist, "enable ", 1, &cmdlist);
15858
15859 add_com_alias ("en", "enable", class_breakpoint, 1);
15860
15861 add_prefix_cmd ("breakpoints", class_breakpoint, enable_command, _("\
15862 Enable some breakpoints.\n\
15863 Give breakpoint numbers (separated by spaces) as arguments.\n\
15864 This is used to cancel the effect of the \"disable\" command.\n\
15865 May be abbreviated to simply \"enable\".\n"),
15866 &enablebreaklist, "enable breakpoints ", 1, &enablelist);
15867
15868 add_cmd ("once", no_class, enable_once_command, _("\
15869 Enable breakpoints for one hit. Give breakpoint numbers.\n\
15870 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15871 &enablebreaklist);
15872
15873 add_cmd ("delete", no_class, enable_delete_command, _("\
15874 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
15875 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15876 &enablebreaklist);
15877
15878 add_cmd ("count", no_class, enable_count_command, _("\
15879 Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
15880 If a breakpoint is hit while enabled in this fashion,\n\
15881 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15882 &enablebreaklist);
15883
15884 add_cmd ("delete", no_class, enable_delete_command, _("\
15885 Enable breakpoints and delete when hit. Give breakpoint numbers.\n\
15886 If a breakpoint is hit while enabled in this fashion, it is deleted."),
15887 &enablelist);
15888
15889 add_cmd ("once", no_class, enable_once_command, _("\
15890 Enable breakpoints for one hit. Give breakpoint numbers.\n\
15891 If a breakpoint is hit while enabled in this fashion, it becomes disabled."),
15892 &enablelist);
15893
15894 add_cmd ("count", no_class, enable_count_command, _("\
15895 Enable breakpoints for COUNT hits. Give count and then breakpoint numbers.\n\
15896 If a breakpoint is hit while enabled in this fashion,\n\
15897 the count is decremented; when it reaches zero, the breakpoint is disabled."),
15898 &enablelist);
15899
15900 add_prefix_cmd ("disable", class_breakpoint, disable_command, _("\
15901 Disable some breakpoints.\n\
15902 Arguments are breakpoint numbers with spaces in between.\n\
15903 To disable all breakpoints, give no argument.\n\
15904 A disabled breakpoint is not forgotten, but has no effect until re-enabled."),
15905 &disablelist, "disable ", 1, &cmdlist);
15906 add_com_alias ("dis", "disable", class_breakpoint, 1);
15907 add_com_alias ("disa", "disable", class_breakpoint, 1);
15908
15909 add_cmd ("breakpoints", class_alias, disable_command, _("\
15910 Disable some breakpoints.\n\
15911 Arguments are breakpoint numbers with spaces in between.\n\
15912 To disable all breakpoints, give no argument.\n\
15913 A disabled breakpoint is not forgotten, but has no effect until re-enabled.\n\
15914 This command may be abbreviated \"disable\"."),
15915 &disablelist);
15916
15917 add_prefix_cmd ("delete", class_breakpoint, delete_command, _("\
15918 Delete some breakpoints or auto-display expressions.\n\
15919 Arguments are breakpoint numbers with spaces in between.\n\
15920 To delete all breakpoints, give no argument.\n\
15921 \n\
15922 Also a prefix command for deletion of other GDB objects.\n\
15923 The \"unset\" command is also an alias for \"delete\"."),
15924 &deletelist, "delete ", 1, &cmdlist);
15925 add_com_alias ("d", "delete", class_breakpoint, 1);
15926 add_com_alias ("del", "delete", class_breakpoint, 1);
15927
15928 add_cmd ("breakpoints", class_alias, delete_command, _("\
15929 Delete some breakpoints or auto-display expressions.\n\
15930 Arguments are breakpoint numbers with spaces in between.\n\
15931 To delete all breakpoints, give no argument.\n\
15932 This command may be abbreviated \"delete\"."),
15933 &deletelist);
15934
15935 add_com ("clear", class_breakpoint, clear_command, _("\
15936 Clear breakpoint at specified line or function.\n\
15937 Argument may be line number, function name, or \"*\" and an address.\n\
15938 If line number is specified, all breakpoints in that line are cleared.\n\
15939 If function is specified, breakpoints at beginning of function are cleared.\n\
15940 If an address is specified, breakpoints at that address are cleared.\n\
15941 \n\
15942 With no argument, clears all breakpoints in the line that the selected frame\n\
15943 is executing in.\n\
15944 \n\
15945 See also the \"delete\" command which clears breakpoints by number."));
15946 add_com_alias ("cl", "clear", class_breakpoint, 1);
15947
15948 c = add_com ("break", class_breakpoint, break_command, _("\
15949 Set breakpoint at specified line or function.\n"
15950 BREAK_ARGS_HELP ("break")));
15951 set_cmd_completer (c, location_completer);
15952
15953 add_com_alias ("b", "break", class_run, 1);
15954 add_com_alias ("br", "break", class_run, 1);
15955 add_com_alias ("bre", "break", class_run, 1);
15956 add_com_alias ("brea", "break", class_run, 1);
15957
15958 if (dbx_commands)
15959 {
15960 add_abbrev_prefix_cmd ("stop", class_breakpoint, stop_command, _("\
15961 Break in function/address or break at a line in the current file."),
15962 &stoplist, "stop ", 1, &cmdlist);
15963 add_cmd ("in", class_breakpoint, stopin_command,
15964 _("Break in function or address."), &stoplist);
15965 add_cmd ("at", class_breakpoint, stopat_command,
15966 _("Break at a line in the current file."), &stoplist);
15967 add_com ("status", class_info, breakpoints_info, _("\
15968 Status of user-settable breakpoints, or breakpoint number NUMBER.\n\
15969 The \"Type\" column indicates one of:\n\
15970 \tbreakpoint - normal breakpoint\n\
15971 \twatchpoint - watchpoint\n\
15972 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15973 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15974 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15975 address and file/line number respectively.\n\
15976 \n\
15977 Convenience variable \"$_\" and default examine address for \"x\"\n\
15978 are set to the address of the last breakpoint listed unless the command\n\
15979 is prefixed with \"server \".\n\n\
15980 Convenience variable \"$bpnum\" contains the number of the last\n\
15981 breakpoint set."));
15982 }
15983
15984 add_info ("breakpoints", breakpoints_info, _("\
15985 Status of specified breakpoints (all user-settable breakpoints if no argument).\n\
15986 The \"Type\" column indicates one of:\n\
15987 \tbreakpoint - normal breakpoint\n\
15988 \twatchpoint - watchpoint\n\
15989 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
15990 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
15991 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
15992 address and file/line number respectively.\n\
15993 \n\
15994 Convenience variable \"$_\" and default examine address for \"x\"\n\
15995 are set to the address of the last breakpoint listed unless the command\n\
15996 is prefixed with \"server \".\n\n\
15997 Convenience variable \"$bpnum\" contains the number of the last\n\
15998 breakpoint set."));
15999
16000 add_info_alias ("b", "breakpoints", 1);
16001
16002 add_cmd ("breakpoints", class_maintenance, maintenance_info_breakpoints, _("\
16003 Status of all breakpoints, or breakpoint number NUMBER.\n\
16004 The \"Type\" column indicates one of:\n\
16005 \tbreakpoint - normal breakpoint\n\
16006 \twatchpoint - watchpoint\n\
16007 \tlongjmp - internal breakpoint used to step through longjmp()\n\
16008 \tlongjmp resume - internal breakpoint at the target of longjmp()\n\
16009 \tuntil - internal breakpoint used by the \"until\" command\n\
16010 \tfinish - internal breakpoint used by the \"finish\" command\n\
16011 The \"Disp\" column contains one of \"keep\", \"del\", or \"dis\" to indicate\n\
16012 the disposition of the breakpoint after it gets hit. \"dis\" means that the\n\
16013 breakpoint will be disabled. The \"Address\" and \"What\" columns indicate the\n\
16014 address and file/line number respectively.\n\
16015 \n\
16016 Convenience variable \"$_\" and default examine address for \"x\"\n\
16017 are set to the address of the last breakpoint listed unless the command\n\
16018 is prefixed with \"server \".\n\n\
16019 Convenience variable \"$bpnum\" contains the number of the last\n\
16020 breakpoint set."),
16021 &maintenanceinfolist);
16022
16023 add_prefix_cmd ("catch", class_breakpoint, catch_command, _("\
16024 Set catchpoints to catch events."),
16025 &catch_cmdlist, "catch ",
16026 0/*allow-unknown*/, &cmdlist);
16027
16028 add_prefix_cmd ("tcatch", class_breakpoint, tcatch_command, _("\
16029 Set temporary catchpoints to catch events."),
16030 &tcatch_cmdlist, "tcatch ",
16031 0/*allow-unknown*/, &cmdlist);
16032
16033 add_catch_command ("fork", _("Catch calls to fork."),
16034 catch_fork_command_1,
16035 NULL,
16036 (void *) (uintptr_t) catch_fork_permanent,
16037 (void *) (uintptr_t) catch_fork_temporary);
16038 add_catch_command ("vfork", _("Catch calls to vfork."),
16039 catch_fork_command_1,
16040 NULL,
16041 (void *) (uintptr_t) catch_vfork_permanent,
16042 (void *) (uintptr_t) catch_vfork_temporary);
16043 add_catch_command ("exec", _("Catch calls to exec."),
16044 catch_exec_command_1,
16045 NULL,
16046 CATCH_PERMANENT,
16047 CATCH_TEMPORARY);
16048 add_catch_command ("load", _("Catch loads of shared libraries.\n\
16049 Usage: catch load [REGEX]\n\
16050 If REGEX is given, only stop for libraries matching the regular expression."),
16051 catch_load_command_1,
16052 NULL,
16053 CATCH_PERMANENT,
16054 CATCH_TEMPORARY);
16055 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
16056 Usage: catch unload [REGEX]\n\
16057 If REGEX is given, only stop for libraries matching the regular expression."),
16058 catch_unload_command_1,
16059 NULL,
16060 CATCH_PERMANENT,
16061 CATCH_TEMPORARY);
16062
16063 c = add_com ("watch", class_breakpoint, watch_command, _("\
16064 Set a watchpoint for an expression.\n\
16065 Usage: watch [-l|-location] EXPRESSION\n\
16066 A watchpoint stops execution of your program whenever the value of\n\
16067 an expression changes.\n\
16068 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16069 the memory to which it refers."));
16070 set_cmd_completer (c, expression_completer);
16071
16072 c = add_com ("rwatch", class_breakpoint, rwatch_command, _("\
16073 Set a read watchpoint for an expression.\n\
16074 Usage: rwatch [-l|-location] EXPRESSION\n\
16075 A watchpoint stops execution of your program whenever the value of\n\
16076 an expression is read.\n\
16077 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16078 the memory to which it refers."));
16079 set_cmd_completer (c, expression_completer);
16080
16081 c = add_com ("awatch", class_breakpoint, awatch_command, _("\
16082 Set a watchpoint for an expression.\n\
16083 Usage: awatch [-l|-location] EXPRESSION\n\
16084 A watchpoint stops execution of your program whenever the value of\n\
16085 an expression is either read or written.\n\
16086 If -l or -location is given, this evaluates EXPRESSION and watches\n\
16087 the memory to which it refers."));
16088 set_cmd_completer (c, expression_completer);
16089
16090 add_info ("watchpoints", watchpoints_info, _("\
16091 Status of specified watchpoints (all watchpoints if no argument)."));
16092
16093 /* XXX: cagney/2005-02-23: This should be a boolean, and should
16094 respond to changes - contrary to the description. */
16095 add_setshow_zinteger_cmd ("can-use-hw-watchpoints", class_support,
16096 &can_use_hw_watchpoints, _("\
16097 Set debugger's willingness to use watchpoint hardware."), _("\
16098 Show debugger's willingness to use watchpoint hardware."), _("\
16099 If zero, gdb will not use hardware for new watchpoints, even if\n\
16100 such is available. (However, any hardware watchpoints that were\n\
16101 created before setting this to nonzero, will continue to use watchpoint\n\
16102 hardware.)"),
16103 NULL,
16104 show_can_use_hw_watchpoints,
16105 &setlist, &showlist);
16106
16107 can_use_hw_watchpoints = 1;
16108
16109 /* Tracepoint manipulation commands. */
16110
16111 c = add_com ("trace", class_breakpoint, trace_command, _("\
16112 Set a tracepoint at specified line or function.\n\
16113 \n"
16114 BREAK_ARGS_HELP ("trace") "\n\
16115 Do \"help tracepoints\" for info on other tracepoint commands."));
16116 set_cmd_completer (c, location_completer);
16117
16118 add_com_alias ("tp", "trace", class_alias, 0);
16119 add_com_alias ("tr", "trace", class_alias, 1);
16120 add_com_alias ("tra", "trace", class_alias, 1);
16121 add_com_alias ("trac", "trace", class_alias, 1);
16122
16123 c = add_com ("ftrace", class_breakpoint, ftrace_command, _("\
16124 Set a fast tracepoint at specified line or function.\n\
16125 \n"
16126 BREAK_ARGS_HELP ("ftrace") "\n\
16127 Do \"help tracepoints\" for info on other tracepoint commands."));
16128 set_cmd_completer (c, location_completer);
16129
16130 c = add_com ("strace", class_breakpoint, strace_command, _("\
16131 Set a static tracepoint at specified line, function or marker.\n\
16132 \n\
16133 strace [LOCATION] [if CONDITION]\n\
16134 LOCATION may be a line number, function name, \"*\" and an address,\n\
16135 or -m MARKER_ID.\n\
16136 If a line number is specified, probe the marker at start of code\n\
16137 for that line. If a function is specified, probe the marker at start\n\
16138 of code for that function. If an address is specified, probe the marker\n\
16139 at that exact address. If a marker id is specified, probe the marker\n\
16140 with that name. With no LOCATION, uses current execution address of\n\
16141 the selected stack frame.\n\
16142 Static tracepoints accept an extra collect action -- ``collect $_sdata''.\n\
16143 This collects arbitrary user data passed in the probe point call to the\n\
16144 tracing library. You can inspect it when analyzing the trace buffer,\n\
16145 by printing the $_sdata variable like any other convenience variable.\n\
16146 \n\
16147 CONDITION is a boolean expression.\n\
16148 \n\
16149 Multiple tracepoints at one place are permitted, and useful if their\n\
16150 conditions are different.\n\
16151 \n\
16152 Do \"help breakpoints\" for info on other commands dealing with breakpoints.\n\
16153 Do \"help tracepoints\" for info on other tracepoint commands."));
16154 set_cmd_completer (c, location_completer);
16155
16156 add_info ("tracepoints", tracepoints_info, _("\
16157 Status of specified tracepoints (all tracepoints if no argument).\n\
16158 Convenience variable \"$tpnum\" contains the number of the\n\
16159 last tracepoint set."));
16160
16161 add_info_alias ("tp", "tracepoints", 1);
16162
16163 add_cmd ("tracepoints", class_trace, delete_trace_command, _("\
16164 Delete specified tracepoints.\n\
16165 Arguments are tracepoint numbers, separated by spaces.\n\
16166 No argument means delete all tracepoints."),
16167 &deletelist);
16168 add_alias_cmd ("tr", "tracepoints", class_trace, 1, &deletelist);
16169
16170 c = add_cmd ("tracepoints", class_trace, disable_trace_command, _("\
16171 Disable specified tracepoints.\n\
16172 Arguments are tracepoint numbers, separated by spaces.\n\
16173 No argument means disable all tracepoints."),
16174 &disablelist);
16175 deprecate_cmd (c, "disable");
16176
16177 c = add_cmd ("tracepoints", class_trace, enable_trace_command, _("\
16178 Enable specified tracepoints.\n\
16179 Arguments are tracepoint numbers, separated by spaces.\n\
16180 No argument means enable all tracepoints."),
16181 &enablelist);
16182 deprecate_cmd (c, "enable");
16183
16184 add_com ("passcount", class_trace, trace_pass_command, _("\
16185 Set the passcount for a tracepoint.\n\
16186 The trace will end when the tracepoint has been passed 'count' times.\n\
16187 Usage: passcount COUNT TPNUM, where TPNUM may also be \"all\";\n\
16188 if TPNUM is omitted, passcount refers to the last tracepoint defined."));
16189
16190 add_prefix_cmd ("save", class_breakpoint, save_command,
16191 _("Save breakpoint definitions as a script."),
16192 &save_cmdlist, "save ",
16193 0/*allow-unknown*/, &cmdlist);
16194
16195 c = add_cmd ("breakpoints", class_breakpoint, save_breakpoints_command, _("\
16196 Save current breakpoint definitions as a script.\n\
16197 This includes all types of breakpoints (breakpoints, watchpoints,\n\
16198 catchpoints, tracepoints). Use the 'source' command in another debug\n\
16199 session to restore them."),
16200 &save_cmdlist);
16201 set_cmd_completer (c, filename_completer);
16202
16203 c = add_cmd ("tracepoints", class_trace, save_tracepoints_command, _("\
16204 Save current tracepoint definitions as a script.\n\
16205 Use the 'source' command in another debug session to restore them."),
16206 &save_cmdlist);
16207 set_cmd_completer (c, filename_completer);
16208
16209 c = add_com_alias ("save-tracepoints", "save tracepoints", class_trace, 0);
16210 deprecate_cmd (c, "save tracepoints");
16211
16212 add_prefix_cmd ("breakpoint", class_maintenance, set_breakpoint_cmd, _("\
16213 Breakpoint specific settings\n\
16214 Configure various breakpoint-specific variables such as\n\
16215 pending breakpoint behavior"),
16216 &breakpoint_set_cmdlist, "set breakpoint ",
16217 0/*allow-unknown*/, &setlist);
16218 add_prefix_cmd ("breakpoint", class_maintenance, show_breakpoint_cmd, _("\
16219 Breakpoint specific settings\n\
16220 Configure various breakpoint-specific variables such as\n\
16221 pending breakpoint behavior"),
16222 &breakpoint_show_cmdlist, "show breakpoint ",
16223 0/*allow-unknown*/, &showlist);
16224
16225 add_setshow_auto_boolean_cmd ("pending", no_class,
16226 &pending_break_support, _("\
16227 Set debugger's behavior regarding pending breakpoints."), _("\
16228 Show debugger's behavior regarding pending breakpoints."), _("\
16229 If on, an unrecognized breakpoint location will cause gdb to create a\n\
16230 pending breakpoint. If off, an unrecognized breakpoint location results in\n\
16231 an error. If auto, an unrecognized breakpoint location results in a\n\
16232 user-query to see if a pending breakpoint should be created."),
16233 NULL,
16234 show_pending_break_support,
16235 &breakpoint_set_cmdlist,
16236 &breakpoint_show_cmdlist);
16237
16238 pending_break_support = AUTO_BOOLEAN_AUTO;
16239
16240 add_setshow_boolean_cmd ("auto-hw", no_class,
16241 &automatic_hardware_breakpoints, _("\
16242 Set automatic usage of hardware breakpoints."), _("\
16243 Show automatic usage of hardware breakpoints."), _("\
16244 If set, the debugger will automatically use hardware breakpoints for\n\
16245 breakpoints set with \"break\" but falling in read-only memory. If not set,\n\
16246 a warning will be emitted for such breakpoints."),
16247 NULL,
16248 show_automatic_hardware_breakpoints,
16249 &breakpoint_set_cmdlist,
16250 &breakpoint_show_cmdlist);
16251
16252 add_setshow_boolean_cmd ("always-inserted", class_support,
16253 &always_inserted_mode, _("\
16254 Set mode for inserting breakpoints."), _("\
16255 Show mode for inserting breakpoints."), _("\
16256 When this mode is on, breakpoints are inserted immediately as soon as\n\
16257 they're created, kept inserted even when execution stops, and removed\n\
16258 only when the user deletes them. When this mode is off (the default),\n\
16259 breakpoints are inserted only when execution continues, and removed\n\
16260 when execution stops."),
16261 NULL,
16262 &show_always_inserted_mode,
16263 &breakpoint_set_cmdlist,
16264 &breakpoint_show_cmdlist);
16265
16266 add_setshow_enum_cmd ("condition-evaluation", class_breakpoint,
16267 condition_evaluation_enums,
16268 &condition_evaluation_mode_1, _("\
16269 Set mode of breakpoint condition evaluation."), _("\
16270 Show mode of breakpoint condition evaluation."), _("\
16271 When this is set to \"host\", breakpoint conditions will be\n\
16272 evaluated on the host's side by GDB. When it is set to \"target\",\n\
16273 breakpoint conditions will be downloaded to the target (if the target\n\
16274 supports such feature) and conditions will be evaluated on the target's side.\n\
16275 If this is set to \"auto\" (default), this will be automatically set to\n\
16276 \"target\" if it supports condition evaluation, otherwise it will\n\
16277 be set to \"gdb\""),
16278 &set_condition_evaluation_mode,
16279 &show_condition_evaluation_mode,
16280 &breakpoint_set_cmdlist,
16281 &breakpoint_show_cmdlist);
16282
16283 add_com ("break-range", class_breakpoint, break_range_command, _("\
16284 Set a breakpoint for an address range.\n\
16285 break-range START-LOCATION, END-LOCATION\n\
16286 where START-LOCATION and END-LOCATION can be one of the following:\n\
16287 LINENUM, for that line in the current file,\n\
16288 FILE:LINENUM, for that line in that file,\n\
16289 +OFFSET, for that number of lines after the current line\n\
16290 or the start of the range\n\
16291 FUNCTION, for the first line in that function,\n\
16292 FILE:FUNCTION, to distinguish among like-named static functions.\n\
16293 *ADDRESS, for the instruction at that address.\n\
16294 \n\
16295 The breakpoint will stop execution of the inferior whenever it executes\n\
16296 an instruction at any address within the [START-LOCATION, END-LOCATION]\n\
16297 range (including START-LOCATION and END-LOCATION)."));
16298
16299 c = add_com ("dprintf", class_breakpoint, dprintf_command, _("\
16300 Set a dynamic printf at specified line or function.\n\
16301 dprintf location,format string,arg1,arg2,...\n\
16302 location may be a line number, function name, or \"*\" and an address.\n\
16303 If a line number is specified, break at start of code for that line.\n\
16304 If a function is specified, break at start of code for that function."));
16305 set_cmd_completer (c, location_completer);
16306
16307 add_setshow_enum_cmd ("dprintf-style", class_support,
16308 dprintf_style_enums, &dprintf_style, _("\
16309 Set the style of usage for dynamic printf."), _("\
16310 Show the style of usage for dynamic printf."), _("\
16311 This setting chooses how GDB will do a dynamic printf.\n\
16312 If the value is \"gdb\", then the printing is done by GDB to its own\n\
16313 console, as with the \"printf\" command.\n\
16314 If the value is \"call\", the print is done by calling a function in your\n\
16315 program; by default printf(), but you can choose a different function or\n\
16316 output stream by setting dprintf-function and dprintf-channel."),
16317 update_dprintf_commands, NULL,
16318 &setlist, &showlist);
16319
16320 dprintf_function = xstrdup ("printf");
16321 add_setshow_string_cmd ("dprintf-function", class_support,
16322 &dprintf_function, _("\
16323 Set the function to use for dynamic printf"), _("\
16324 Show the function to use for dynamic printf"), NULL,
16325 update_dprintf_commands, NULL,
16326 &setlist, &showlist);
16327
16328 dprintf_channel = xstrdup ("");
16329 add_setshow_string_cmd ("dprintf-channel", class_support,
16330 &dprintf_channel, _("\
16331 Set the channel to use for dynamic printf"), _("\
16332 Show the channel to use for dynamic printf"), NULL,
16333 update_dprintf_commands, NULL,
16334 &setlist, &showlist);
16335
16336 add_setshow_boolean_cmd ("disconnected-dprintf", no_class,
16337 &disconnected_dprintf, _("\
16338 Set whether dprintf continues after GDB disconnects."), _("\
16339 Show whether dprintf continues after GDB disconnects."), _("\
16340 Use this to let dprintf commands continue to hit and produce output\n\
16341 even if GDB disconnects or detaches from the target."),
16342 NULL,
16343 NULL,
16344 &setlist, &showlist);
16345
16346 add_com ("agent-printf", class_vars, agent_printf_command, _("\
16347 agent-printf \"printf format string\", arg1, arg2, arg3, ..., argn\n\
16348 (target agent only) This is useful for formatted output in user-defined commands."));
16349
16350 automatic_hardware_breakpoints = 1;
16351
16352 observer_attach_about_to_proceed (breakpoint_about_to_proceed);
16353 observer_attach_thread_exit (remove_threaded_breakpoints);
16354 }